-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cgroups: refactor v2 kill path to use cgroups.kill interface file
This PR refactors the cgroups v2 group kill code path to use the cgroups.kill interface file for destroying the cgroup. Previously we copied the freeze + sigkill + unfreeze pattern from the v1 code, but v2 provides a more efficient and more race-free way to handle this. Closes #14371
- Loading branch information
Showing
4 changed files
with
80 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:improvement | ||
cgroups: use cgroup.kill interface file when using cgroups v2 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//go:build linux | ||
|
||
package cgutil | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// editor provides a simple mechanism for reading and writing cgroup files. | ||
type editor struct { | ||
fromRoot string | ||
} | ||
|
||
func (e *editor) path(file string) string { | ||
return filepath.Join(CgroupRoot, e.fromRoot, file) | ||
} | ||
|
||
func (e *editor) write(file, content string) error { | ||
return os.WriteFile(e.path(file), []byte(content), 0o644) | ||
} | ||
|
||
func (e *editor) read(file string) (string, error) { | ||
b, err := os.ReadFile(e.path(file)) | ||
return strings.TrimSpace(string(b)), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//go:build linux | ||
|
||
package cgutil | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/hashicorp/nomad/client/testutil" | ||
"github.com/hashicorp/nomad/helper/uuid" | ||
"github.com/shoenig/test/must" | ||
) | ||
|
||
func createCG(t *testing.T) (string, func()) { | ||
name := uuid.Short() + ".scope" | ||
path := filepath.Join(CgroupRoot, name) | ||
err := os.Mkdir(path, 0o755) | ||
must.NoError(t, err) | ||
|
||
return name, func() { | ||
_ = os.Remove(path) | ||
} | ||
} | ||
|
||
func TestCG_editor(t *testing.T) { | ||
testutil.CgroupsCompatibleV2(t) | ||
|
||
cg, rm := createCG(t) | ||
t.Cleanup(rm) | ||
|
||
edits := &editor{cg} | ||
writeErr := edits.write("cpu.weight.nice", "13") | ||
must.NoError(t, writeErr) | ||
|
||
b, readErr := edits.read("cpu.weight.nice") | ||
must.NoError(t, readErr) | ||
must.Eq(t, "13", b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters