docker: use correct effective cpuset filename on legacy cgroups v1 systems (#20294)

This commit is contained in:
Seth Hoenig
2024-04-05 08:05:51 -05:00
committed by GitHub
parent 2382ab8776
commit 825efc3925
3 changed files with 34 additions and 2 deletions

3
.changelog/20294.txt Normal file
View File

@@ -0,0 +1,3 @@
```release-note:bug
docker: Fixed a bug where cpuset cgroup would not be updated on cgroup v1 systems
```

View File

@@ -8,6 +8,7 @@ import (
"path/filepath" "path/filepath"
"time" "time"
"github.com/hashicorp/nomad/client/lib/cgroupslib"
"github.com/hashicorp/nomad/helper" "github.com/hashicorp/nomad/helper"
) )
@@ -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) { 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") destination = filepath.Join(destination, "cpuset.cpus")
// read the current value of usable cores // read the current value of usable cores
@@ -67,7 +77,7 @@ func (c *cpuset) copyCpuset(source, destination string) {
} }
// otherwise write the new value // otherwise write the new value
err = os.WriteFile(destination, b, 0644) err = os.WriteFile(destination, b, 0o644)
if err != nil { if err != nil {
return return
} }

View File

@@ -10,6 +10,7 @@ import (
"time" "time"
"github.com/hashicorp/nomad/ci" "github.com/hashicorp/nomad/ci"
"github.com/hashicorp/nomad/client/testutil"
"github.com/shoenig/test/must" "github.com/shoenig/test/must"
) )
@@ -42,3 +43,21 @@ func Test_cpuset_watch(t *testing.T) {
must.Eq(t, 1, hits) 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)
}