Skip to content

Commit

Permalink
Initialize and toggle cgroup controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrc committed Feb 24, 2022
1 parent 0d4775a commit 438de7e
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 21 deletions.
8 changes: 4 additions & 4 deletions agent/api/task/task_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (task *Task) initializeCgroupResourceSpec(cgroupPath string, cGroupCPUPerio

// BuildCgroupRoot helps build the task cgroup prefix
// Example v1: /ecs/task-id
// Example v2: ECSTask-$TASKID.slice
// Example v2: ecstasks-$TASKID.slice
func (task *Task) BuildCgroupRoot() (string, error) {
taskID, err := task.GetID()
if err != nil {
Expand All @@ -97,10 +97,10 @@ func buildCgroupV1Root(taskID string) string {
// buildCgroupV2Root creates a root cgroup using the systemd driver's special "-"
// character. The "-" specifies a parent slice, so tasks and their containers end up
// looking like this in the cgroup directory:
// /sys/fs/cgroup/ECSTasks.slice/
// ├── ECSTasks-XXXXf406f70c4c678073ae96944fXXXX.slice
// /sys/fs/cgroup/ecstasks.slice/
// ├── ecstasks-XXXXf406f70c4c678073ae96944fXXXX.slice
// │ └── docker-XXXX7c6dc81f2e9a8bf1c566dc769733ccba594b3007dd289a0f50ad7923XXXX.scope
// └── ECSTasks-XXXX30467358463ab6bbba4e73afXXXX.slice
// └── ecstasks-XXXX30467358463ab6bbba4e73afXXXX.slice
// └── docker-XXXX7ef4e942552437c96051356859c1df169f16e1cf9a9fc96fd30614e6XXXX.scope
func buildCgroupV2Root(taskID string) string {
return fmt.Sprintf("%s-%s.slice", config.DefaultTaskCgroupV2Prefix, taskID)
Expand Down
2 changes: 1 addition & 1 deletion agent/api/task/task_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func TestBuildCgroupV1Root(t *testing.T) {

func TestBuildCgroupV2Root(t *testing.T) {
cgroupRoot := buildCgroupV2Root("111mytaskid")
assert.Equal(t, "ECSTasks-111mytaskid.slice", cgroupRoot)
assert.Equal(t, "ecstasks-111mytaskid.slice", cgroupRoot)
}

// TestBuildLinuxResourceSpecCPUMem validates the linux resource spec builder
Expand Down
4 changes: 2 additions & 2 deletions agent/config/config_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ const (
// DefaultTaskCgroupV1Prefix is default cgroup v1 prefix for ECS tasks
DefaultTaskCgroupV1Prefix = "/ecs"
// DefaultTaskCgroupV2Prefix is default cgroup v2 prefix for ECS tasks
// ECSTasks is used because this creates a systemd "slice", and using just
// ecstasks is used because this creates a systemd "slice", and using just
// ecs would create a confusing name conflict with the ecs systemd service.
// (we would have both ecs.service and ecs.slice in /sys/fs/cgroup).
DefaultTaskCgroupV2Prefix = "ECSTasks"
DefaultTaskCgroupV2Prefix = "ecstasks"

// Default cgroup memory system root path, this is the default used if the
// path has not been configured through ECS_CGROUP_PATH
Expand Down
2 changes: 1 addition & 1 deletion agent/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/aws/aws-sdk-go v1.36.0
github.com/awslabs/go-config-generator-for-fluentd-and-fluentbit v0.0.0-20190829210224-55d4fd2e6f35
github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575
github.com/containerd/cgroups v1.0.4-0.20220217190539-cf1b326d3714
github.com/containerd/cgroups v1.0.4-0.20220221221032-e710ed6ebb1a
github.com/containerd/containerd v1.4.12 // indirect
github.com/containerd/continuity v0.0.0-20181023183536-c220ac4f01b8 // indirect
github.com/containernetworking/cni v0.7.1
Expand Down
2 changes: 2 additions & 0 deletions agent/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ github.com/containerd/cgroups v1.0.3 h1:ADZftAkglvCiD44c77s5YmMqaP2pzVCFZvBmAlBd
github.com/containerd/cgroups v1.0.3/go.mod h1:/ofk34relqNjSGyqPrmEULrO4Sc8LJhvJmWbUCUKqj8=
github.com/containerd/cgroups v1.0.4-0.20220217190539-cf1b326d3714 h1:jCXPr1wkugK8p7oEQjuP8hBB7Q8Abq3w4ELsSs/0IwU=
github.com/containerd/cgroups v1.0.4-0.20220217190539-cf1b326d3714/go.mod h1:/ofk34relqNjSGyqPrmEULrO4Sc8LJhvJmWbUCUKqj8=
github.com/containerd/cgroups v1.0.4-0.20220221221032-e710ed6ebb1a h1:e956Q1zTD3IIKTQPWb92C20PvU3S2ohP9jWqqD0JJWE=
github.com/containerd/cgroups v1.0.4-0.20220221221032-e710ed6ebb1a/go.mod h1:/ofk34relqNjSGyqPrmEULrO4Sc8LJhvJmWbUCUKqj8=
github.com/containerd/containerd v1.4.12 h1:V+SHzYmhng/iju6M5nFrpTTusrhidoxKTwdwLw+u4c4=
github.com/containerd/containerd v1.4.12/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtMXFTttgp+kVtyUA=
github.com/containerd/continuity v0.0.0-20181023183536-c220ac4f01b8 h1:lJeDcldQnYskl7krc3lTppg8NKomoQkmQg1AzOXtQbA=
Expand Down
30 changes: 25 additions & 5 deletions agent/taskresource/cgroup/control/cgroupv2_controller_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ func (c *controlv2) Create(cgroupSpec *Spec) error {
cgroupPath := cgroupSpec.Root
seelog.Infof("Creating cgroup cgroupv2root=%s parentSlice=%s cgroupPath=%s", defaultCgroupv2Path, parentCgroupSlice, cgroupPath)

_, err = cgroupsv2.NewSystemd(parentCgroupSlice, cgroupPath, generalSlicePID, cgroupsv2.ToResources(cgroupSpec.Specs))
m, err := cgroupsv2.NewSystemd(parentCgroupSlice, cgroupPath, generalSlicePID, cgroupsv2.ToResources(cgroupSpec.Specs))
if err != nil {
return fmt.Errorf("cgroupv2 create: unable to create v2 manager cgroupPath=%s err=%s", cgroupPath, err)
}

if err := initializeControllers(m); err != nil {
return fmt.Errorf("cgroupv2 create: unable initialize cgroup controllers cgroupPath=%s err=%s", cgroupPath, err)
}

return nil
}

Expand Down Expand Up @@ -91,11 +95,28 @@ func (c *controlv2) Exists(cgroupPath string) bool {
// Init is used to setup the cgroup root for ecs
func (c *controlv2) Init() error {
// Load the "root" cgroup and verify cpu and memory cgroup controllers are available.
m, err := cgroupsv2.LoadSystemd(parentCgroupSlice, "")
m, err := cgroupsv2.LoadSystemd("", "")
if err != nil {
return fmt.Errorf("cgroupv2 init: unable to load root cgroup: %s", err)
}
controllers, err := m.Controllers()

if err := initializeControllers(m); err != nil {
return err
}

seelog.Infof("ECS task resource limits cgroupv2 functionality initialized")
return nil
}

func initializeControllers(manager *cgroupsv2.Manager) error {
// enable cpu and memory cgroup controllers
err := manager.ToggleControllers([]string{"cpu", "memory"}, cgroupsv2.Enable)
if err != nil {
return fmt.Errorf("cgroupv2 init: error enabling cpu and memory controllers: %s", err)
}

// verify that cpu and memory controllers are available
controllers, err := manager.Controllers()
if err != nil {
return fmt.Errorf("cgroupv2 init: unable to get cgroup controllers: %s", err)
}
Expand All @@ -105,7 +126,6 @@ func (c *controlv2) Init() error {
if err := validateController("cpu", controllers); err != nil {
return fmt.Errorf("cgroupv2 init: unable to validate cgroup controllers: %s", err)
}
seelog.Infof("ECS task resource limits cgroupv2 functionality initialized")
return nil
}

Expand All @@ -119,7 +139,7 @@ func validateController(controller string, controllers []string) error {
}

// fullCgroupPath returns the full path on disk to a task cgroup slice.
// example: /sys/fs/cgroup/ECSTasks.slice/ECSTasks-529630467358463ab6bbba4e73afe704.slice
// example: /sys/fs/cgroup/ecstasks.slice/ecstasks-529630467358463ab6bbba4e73afe704.slice
func fullCgroupPath(cgroupPath string) string {
return filepath.Join(defaultCgroupv2Path, parentCgroupSlice, config.DefaultTaskCgroupV2Prefix+".slice", cgroupPath)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion agent/vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ github.com/cilium/ebpf/internal
github.com/cilium/ebpf/internal/btf
github.com/cilium/ebpf/internal/unix
github.com/cilium/ebpf/link
# github.com/containerd/cgroups v1.0.4-0.20220217190539-cf1b326d3714
# github.com/containerd/cgroups v1.0.4-0.20220221221032-e710ed6ebb1a
github.com/containerd/cgroups
github.com/containerd/cgroups/stats/v1
github.com/containerd/cgroups/v2
Expand Down

0 comments on commit 438de7e

Please sign in to comment.