2014-04-19 04:34:26 +00:00
|
|
|
package fs
|
2014-03-14 09:47:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-04-19 04:34:26 +00:00
|
|
|
"io/ioutil"
|
2014-03-14 09:47:49 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2014-04-19 04:34:26 +00:00
|
|
|
|
|
|
|
"github.com/dotcloud/docker/pkg/cgroups"
|
2014-03-14 09:47:49 +00:00
|
|
|
)
|
|
|
|
|
2014-04-19 04:55:06 +00:00
|
|
|
var (
|
|
|
|
subsystems = []subsystem{
|
|
|
|
&devicesGroup{},
|
|
|
|
&memoryGroup{},
|
|
|
|
&cpuGroup{},
|
|
|
|
&cpusetGroup{},
|
|
|
|
&cpuacctGroup{},
|
|
|
|
&blkioGroup{},
|
|
|
|
&perfEventGroup{},
|
|
|
|
&freezerGroup{},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type subsystem interface {
|
|
|
|
Set(*data) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type data struct {
|
2014-03-14 09:47:49 +00:00
|
|
|
root string
|
|
|
|
cgroup string
|
2014-04-19 04:55:06 +00:00
|
|
|
c *cgroups.Cgroup
|
|
|
|
pid int
|
2014-03-14 09:47:49 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 04:34:26 +00:00
|
|
|
func Apply(c *cgroups.Cgroup, pid int) (cgroups.ActiveCgroup, error) {
|
2014-03-14 09:47:49 +00:00
|
|
|
// We have two implementation of cgroups support, one is based on
|
|
|
|
// systemd and the dbus api, and one is based on raw cgroup fs operations
|
|
|
|
// following the pre-single-writer model docs at:
|
|
|
|
// http://www.freedesktop.org/wiki/Software/systemd/PaxControlGroups/
|
|
|
|
//
|
|
|
|
// we can pick any subsystem to find the root
|
|
|
|
|
2014-04-19 04:34:26 +00:00
|
|
|
cgroupRoot, err := cgroups.FindCgroupMountpoint("cpu")
|
2014-03-14 09:47:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cgroupRoot = filepath.Dir(cgroupRoot)
|
|
|
|
|
|
|
|
if _, err := os.Stat(cgroupRoot); err != nil {
|
|
|
|
return nil, fmt.Errorf("cgroups fs not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
cgroup := c.Name
|
|
|
|
if c.Parent != "" {
|
|
|
|
cgroup = filepath.Join(c.Parent, cgroup)
|
|
|
|
}
|
|
|
|
|
2014-04-19 04:55:06 +00:00
|
|
|
d := &data{
|
2014-03-14 09:47:49 +00:00
|
|
|
root: cgroupRoot,
|
|
|
|
cgroup: cgroup,
|
2014-04-19 04:55:06 +00:00
|
|
|
c: c,
|
|
|
|
pid: pid,
|
2014-03-14 09:47:49 +00:00
|
|
|
}
|
2014-04-19 04:55:06 +00:00
|
|
|
for _, sys := range subsystems {
|
|
|
|
if err := sys.Set(d); err != nil {
|
2014-04-11 17:27:19 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-03-27 08:25:01 +00:00
|
|
|
}
|
2014-04-19 04:55:06 +00:00
|
|
|
return d, nil
|
2014-03-14 09:47:49 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 04:55:06 +00:00
|
|
|
func (raw *data) path(subsystem string) (string, error) {
|
2014-04-19 04:34:26 +00:00
|
|
|
initPath, err := cgroups.GetInitCgroupDir(subsystem)
|
2014-03-14 09:47:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Join(raw.root, subsystem, initPath, raw.cgroup), nil
|
|
|
|
}
|
|
|
|
|
2014-04-19 04:55:06 +00:00
|
|
|
func (raw *data) join(subsystem string) (string, error) {
|
2014-03-14 09:47:49 +00:00
|
|
|
path, err := raw.path(subsystem)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(path, 0755); err != nil && !os.IsExist(err) {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-04-19 04:55:06 +00:00
|
|
|
if err := writeFile(path, "cgroup.procs", strconv.Itoa(raw.pid)); err != nil {
|
2014-03-14 09:47:49 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
2014-04-19 04:55:06 +00:00
|
|
|
func (raw *data) Cleanup() error {
|
2014-03-14 09:47:49 +00:00
|
|
|
get := func(subsystem string) string {
|
|
|
|
path, _ := raw.path(subsystem)
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, path := range []string{
|
|
|
|
get("memory"),
|
|
|
|
get("devices"),
|
|
|
|
get("cpu"),
|
2014-03-27 08:25:01 +00:00
|
|
|
get("cpuset"),
|
2014-04-11 17:27:19 +00:00
|
|
|
get("cpuacct"),
|
|
|
|
get("blkio"),
|
|
|
|
get("perf_event"),
|
|
|
|
get("freezer"),
|
2014-03-14 09:47:49 +00:00
|
|
|
} {
|
|
|
|
if path != "" {
|
|
|
|
os.RemoveAll(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-04-19 04:34:26 +00:00
|
|
|
|
|
|
|
func writeFile(dir, file, data string) error {
|
|
|
|
return ioutil.WriteFile(filepath.Join(dir, file), []byte(data), 0700)
|
|
|
|
}
|