-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #446 from cyphar/18-add-pids-controller
cgroup: add PIDs cgroup controller support
- Loading branch information
Showing
22 changed files
with
415 additions
and
190 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
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
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
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
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
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
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
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
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
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
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,57 @@ | ||
// +build linux | ||
|
||
package fs | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/opencontainers/runc/libcontainer/cgroups" | ||
"github.com/opencontainers/runc/libcontainer/configs" | ||
) | ||
|
||
type PidsGroup struct { | ||
} | ||
|
||
func (s *PidsGroup) Name() string { | ||
return "pids" | ||
} | ||
|
||
func (s *PidsGroup) Apply(d *cgroupData) error { | ||
_, err := d.join("pids") | ||
if err != nil && !cgroups.IsNotFound(err) { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (s *PidsGroup) Set(path string, cgroup *configs.Cgroup) error { | ||
if cgroup.Resources.PidsLimit != 0 { | ||
// "max" is the fallback value. | ||
limit := "max" | ||
|
||
if cgroup.Resources.PidsLimit > 0 { | ||
limit = strconv.FormatInt(cgroup.Resources.PidsLimit, 10) | ||
} | ||
|
||
if err := writeFile(path, "pids.max", limit); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *PidsGroup) Remove(d *cgroupData) error { | ||
return removePath(d.path("pids")) | ||
} | ||
|
||
func (s *PidsGroup) GetStats(path string, stats *cgroups.Stats) error { | ||
value, err := getCgroupParamUint(path, "pids.current") | ||
if err != nil { | ||
return fmt.Errorf("failed to parse pids.current - %s", err) | ||
} | ||
|
||
stats.PidsStats.Current = value | ||
return nil | ||
} |
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,83 @@ | ||
// +build linux | ||
|
||
package fs | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/opencontainers/runc/libcontainer/cgroups" | ||
) | ||
|
||
const ( | ||
maxUnlimited = -1 | ||
maxLimited = 1024 | ||
) | ||
|
||
func TestPidsSetMax(t *testing.T) { | ||
helper := NewCgroupTestUtil("pids", t) | ||
defer helper.cleanup() | ||
|
||
helper.writeFileContents(map[string]string{ | ||
"pids.max": "max", | ||
}) | ||
|
||
helper.CgroupData.config.Resources.PidsLimit = maxLimited | ||
pids := &PidsGroup{} | ||
if err := pids.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
value, err := getCgroupParamUint(helper.CgroupPath, "pids.max") | ||
if err != nil { | ||
t.Fatalf("Failed to parse pids.max - %s", err) | ||
} | ||
|
||
if value != maxLimited { | ||
t.Fatalf("Expected %d, got %d for setting pids.max - limited", maxLimited, value) | ||
} | ||
} | ||
|
||
func TestPidsSetUnlimited(t *testing.T) { | ||
helper := NewCgroupTestUtil("pids", t) | ||
defer helper.cleanup() | ||
|
||
helper.writeFileContents(map[string]string{ | ||
"pids.max": strconv.Itoa(maxLimited), | ||
}) | ||
|
||
helper.CgroupData.config.Resources.PidsLimit = maxUnlimited | ||
pids := &PidsGroup{} | ||
if err := pids.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
value, err := getCgroupParamString(helper.CgroupPath, "pids.max") | ||
if err != nil { | ||
t.Fatalf("Failed to parse pids.max - %s", err) | ||
} | ||
|
||
if value != "max" { | ||
t.Fatalf("Expected %s, got %s for setting pids.max - unlimited", "max", value) | ||
} | ||
} | ||
|
||
func TestPidsStats(t *testing.T) { | ||
helper := NewCgroupTestUtil("pids", t) | ||
defer helper.cleanup() | ||
|
||
helper.writeFileContents(map[string]string{ | ||
"pids.current": strconv.Itoa(1337), | ||
"pids.max": strconv.Itoa(maxLimited), | ||
}) | ||
|
||
pids := &PidsGroup{} | ||
stats := *cgroups.NewStats() | ||
if err := pids.GetStats(helper.CgroupPath, &stats); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if stats.PidsStats.Current != 1337 { | ||
t.Fatalf("Expected %d, got %d for pids.current", 1337, stats.PidsStats.Current) | ||
} | ||
} |
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
Oops, something went wrong.