Merge pull request #5976 from crosbymichael/getpids

Move get pid into cgroup implementation
This commit is contained in:
Victor Vieux 2014-05-21 19:09:50 -07:00
commit 89b64d33ee
5 changed files with 84 additions and 11 deletions

View file

@ -103,6 +103,36 @@ func GetStats(c *cgroups.Cgroup, subsystem string, pid int) (map[string]float64,
return sys.Stats(d) return sys.Stats(d)
} }
func GetPids(c *cgroups.Cgroup) ([]int, error) {
cgroupRoot, err := cgroups.FindCgroupMountpoint("cpu")
if err != nil {
return nil, err
}
cgroupRoot = filepath.Dir(cgroupRoot)
if _, err := os.Stat(cgroupRoot); err != nil {
return nil, fmt.Errorf("cgroup root %s not found", cgroupRoot)
}
cgroup := c.Name
if c.Parent != "" {
cgroup = filepath.Join(c.Parent, cgroup)
}
d := &data{
root: cgroupRoot,
cgroup: cgroup,
c: c,
}
dir, err := d.path("devices")
if err != nil {
return nil, err
}
return cgroups.ReadProcsFile(dir)
}
func (raw *data) parent(subsystem string) (string, error) { func (raw *data) parent(subsystem string) (string, error) {
initPath, err := cgroups.GetInitCgroupDir(subsystem) initPath, err := cgroups.GetInitCgroupDir(subsystem)
if err != nil { if err != nil {

View file

@ -1,9 +1,5 @@
package fs package fs
import (
"os"
)
type devicesGroup struct { type devicesGroup struct {
} }
@ -12,11 +8,6 @@ func (s *devicesGroup) Set(d *data) error {
if err != nil { if err != nil {
return err return err
} }
defer func() {
if err != nil {
os.RemoveAll(dir)
}
}()
if !d.c.DeviceAccess { if !d.c.DeviceAccess {
if err := writeFile(dir, "devices.deny", "a"); err != nil { if err := writeFile(dir, "devices.deny", "a"); err != nil {

View file

@ -12,6 +12,10 @@ func UseSystemd() bool {
return false return false
} }
func Apply(c *Cgroup, pid int) (cgroups.ActiveCgroup, error) { func Apply(c *cgroups.Cgroup, pid int) (cgroups.ActiveCgroup, error) {
return nil, fmt.Errorf("Systemd not supported")
}
func GetPids(c *cgroups.Cgroup) ([]int, error) {
return nil, fmt.Errorf("Systemd not supported") return nil, fmt.Errorf("Systemd not supported")
} }

View file

@ -3,6 +3,7 @@
package systemd package systemd
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -78,7 +79,7 @@ type cgroupArg struct {
func Apply(c *cgroups.Cgroup, pid int) (cgroups.ActiveCgroup, error) { func Apply(c *cgroups.Cgroup, pid int) (cgroups.ActiveCgroup, error) {
var ( var (
unitName = c.Parent + "-" + c.Name + ".scope" unitName = getUnitName(c)
slice = "system.slice" slice = "system.slice"
properties []systemd1.Property properties []systemd1.Property
cpuArgs []cgroupArg cpuArgs []cgroupArg
@ -303,3 +304,24 @@ func (c *systemdCgroup) Cleanup() error {
return nil return nil
} }
func GetPids(c *cgroups.Cgroup) ([]int, error) {
unitName := getUnitName(c)
mountpoint, err := cgroups.FindCgroupMountpoint("cpu")
if err != nil {
return nil, err
}
props, err := theConn.GetUnitTypeProperties(unitName, getIfaceForUnit(unitName))
if err != nil {
return nil, err
}
cgroup := props["ControlGroup"].(string)
return cgroups.ReadProcsFile(filepath.Join(mountpoint, cgroup))
}
func getUnitName(c *cgroups.Cgroup) string {
return fmt.Sprintf("%s-%s.scope", c.Parent, c.Name)
}

View file

@ -4,6 +4,8 @@ import (
"bufio" "bufio"
"io" "io"
"os" "os"
"path/filepath"
"strconv"
"strings" "strings"
"github.com/dotcloud/docker/pkg/mount" "github.com/dotcloud/docker/pkg/mount"
@ -49,6 +51,30 @@ func GetInitCgroupDir(subsystem string) (string, error) {
return parseCgroupFile(subsystem, f) return parseCgroupFile(subsystem, f)
} }
func ReadProcsFile(dir string) ([]int, error) {
f, err := os.Open(filepath.Join(dir, "cgroup.procs"))
if err != nil {
return nil, err
}
defer f.Close()
var (
s = bufio.NewScanner(f)
out = []int{}
)
for s.Scan() {
if t := s.Text(); t != "" {
pid, err := strconv.Atoi(t)
if err != nil {
return nil, err
}
out = append(out, pid)
}
}
return out, nil
}
func parseCgroupFile(subsystem string, r io.Reader) (string, error) { func parseCgroupFile(subsystem string, r io.Reader) (string, error) {
s := bufio.NewScanner(r) s := bufio.NewScanner(r)
for s.Scan() { for s.Scan() {