2015-12-14 21:33:56 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-03-14 14:45:12 +08:00
|
|
|
"path/filepath"
|
2015-12-14 21:33:56 +08:00
|
|
|
"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 {
|
2015-12-20 19:30:35 +08:00
|
|
|
_, err := d.join("pids")
|
2015-12-14 21:33:56 +08:00
|
|
|
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 {
|
2016-03-13 01:53:20 +08:00
|
|
|
current, err := getCgroupParamUint(path, "pids.current")
|
2015-12-14 21:33:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse pids.current - %s", err)
|
|
|
|
}
|
|
|
|
|
2016-03-14 14:45:12 +08:00
|
|
|
maxString, err := getCgroupParamString(path, "pids.max")
|
2016-03-13 01:53:20 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse pids.max - %s", err)
|
|
|
|
}
|
|
|
|
|
2016-03-14 14:45:12 +08:00
|
|
|
// Default if pids.max == "max" is 0 -- which represents "no limit".
|
|
|
|
var max uint64
|
|
|
|
if maxString != "max" {
|
|
|
|
max, err = parseUint(maxString, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse pids.max - unable to parse %q as a uint from Cgroup file %q", maxString, filepath.Join(path, "pids.max"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-13 01:53:20 +08:00
|
|
|
stats.PidsStats.Current = current
|
2016-03-14 14:45:12 +08:00
|
|
|
stats.PidsStats.Limit = max
|
2015-12-14 21:33:56 +08:00
|
|
|
return nil
|
|
|
|
}
|