containerd / cgroups

cgroups package for Go
https://containerd.io
Apache License 2.0
1.09k stars 237 forks source link

cpu.max not created with cgroup2 #333

Closed Xeway closed 7 months ago

Xeway commented 7 months ago
res := cgroup2.Resources{
    CPU: &cgroup2.CPU{
        Max: cgroup2.NewCPUMax(nil, &maxCPUValue),
    },
}
if err := m.Update(&res); err != nil {
    log.Fatal(err)
}

When I run: go build && sudo ./my-program, I get the error: open /sys/fs/cgroup/my_cgroup.slice/cpu.max: permission denied. And in reality, the cpu.max file doesn't exists. I tried to create it but I always get permission denied even when I change chmod to 777 for /sys/fs/cgroup/my_cgroup.slice directory.

axliupore commented 7 months ago

What environment are you running in?

Xeway commented 7 months ago

My bad, fixed it with echo '+cpu' | sudo tee /sys/fs/cgroup/cgroup.subtree_control.

Xeway commented 7 months ago

If anyone passes by, instead of directly writing to cgroup.subtree_control file, you can also add controllers with ToggleControllers function. Here's an example of how to use it to add memory and cpu controllers:

// Enable the memory and CPU controllers
if err := m.ToggleControllers([]string{"memory", "cpu"}, cgroup2.Enable); err != nil {
    log.Fatal(err)
}