Skip to content

Commit

Permalink
docker: use correct effective cpuset filename on legacy cgroups v1 sy…
Browse files Browse the repository at this point in the history
…stems
  • Loading branch information
shoenig committed Apr 4, 2024
1 parent a71632e commit bbe9d21
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/20294.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
docker: Fixed a bug where cpuset cgroup would not be updated on cgroup v1 systems
```
14 changes: 12 additions & 2 deletions drivers/docker/cpuset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"time"

"github.com/hashicorp/nomad/client/lib/cgroupslib"
"github.com/hashicorp/nomad/helper"
)

Expand Down Expand Up @@ -49,8 +50,17 @@ func (c *cpuset) watch() {
}
}

func effectiveCpusetFile() string {
switch cgroupslib.GetMode() {
case cgroupslib.CG1:
return "cpuset.effective_cpus"
default:
return "cpuset.cpus.effective"
}
}

func (c *cpuset) copyCpuset(source, destination string) {
source = filepath.Join(source, "cpuset.cpus.effective")
source = filepath.Join(source, effectiveCpusetFile())
destination = filepath.Join(destination, "cpuset.cpus")

// read the current value of usable cores
Expand All @@ -67,7 +77,7 @@ func (c *cpuset) copyCpuset(source, destination string) {
}

// otherwise write the new value
err = os.WriteFile(destination, b, 0644)
err = os.WriteFile(destination, b, 0o644)
if err != nil {
return
}
Expand Down
19 changes: 19 additions & 0 deletions drivers/docker/cpuset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/hashicorp/nomad/ci"
"github.com/hashicorp/nomad/client/testutil"
"github.com/shoenig/test/must"
)

Expand Down Expand Up @@ -42,3 +43,21 @@ func Test_cpuset_watch(t *testing.T) {

must.Eq(t, 1, hits)
}

func Test_effectiveCpusetFile_cgroupsv1(t *testing.T) {
testutil.CgroupsCompatibleV1(t)

ci.Parallel(t)

result := effectiveCpusetFile()
must.Eq(t, "cpuset.effective_cpus", result)
}

func Test_effectiveCpusetFile_cgroupsv2(t *testing.T) {
testutil.CgroupsCompatibleV2(t)

ci.Parallel(t)

result := effectiveCpusetFile()
must.Eq(t, "cpuset.cpus.effective", result)
}

0 comments on commit bbe9d21

Please sign in to comment.