2014-05-15 06:21:44 +08:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer/cgroups"
|
2014-05-15 06:21:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
subsystems = map[string]subsystem{
|
2014-06-20 21:13:56 +08:00
|
|
|
"devices": &DevicesGroup{},
|
|
|
|
"memory": &MemoryGroup{},
|
|
|
|
"cpu": &CpuGroup{},
|
|
|
|
"cpuset": &CpusetGroup{},
|
|
|
|
"cpuacct": &CpuacctGroup{},
|
|
|
|
"blkio": &BlkioGroup{},
|
|
|
|
"perf_event": &PerfEventGroup{},
|
|
|
|
"freezer": &FreezerGroup{},
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type subsystem interface {
|
|
|
|
Set(*data) error
|
|
|
|
Remove(*data) error
|
2014-06-20 21:13:56 +08:00
|
|
|
GetStats(string, *cgroups.Stats) error
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type data struct {
|
|
|
|
root string
|
|
|
|
cgroup string
|
|
|
|
c *cgroups.Cgroup
|
|
|
|
pid int
|
|
|
|
}
|
|
|
|
|
|
|
|
func Apply(c *cgroups.Cgroup, pid int) (cgroups.ActiveCgroup, error) {
|
2014-05-31 06:09:07 +08:00
|
|
|
d, err := getCgroupData(c, pid)
|
2014-05-15 06:21:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sys := range subsystems {
|
|
|
|
if err := sys.Set(d); err != nil {
|
|
|
|
d.Cleanup()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2014-05-31 06:09:07 +08:00
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
2014-07-09 02:49:45 +08:00
|
|
|
func Cleanup(c *cgroups.Cgroup) error {
|
|
|
|
d, err := getCgroupData(c, 0)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not get Cgroup data %s", err)
|
|
|
|
}
|
|
|
|
return d.Cleanup()
|
|
|
|
}
|
|
|
|
|
2014-05-28 08:01:08 +08:00
|
|
|
func GetStats(c *cgroups.Cgroup) (*cgroups.Stats, error) {
|
|
|
|
stats := cgroups.NewStats()
|
2014-05-31 06:09:07 +08:00
|
|
|
|
|
|
|
d, err := getCgroupData(c, 0)
|
2014-05-15 06:21:44 +08:00
|
|
|
if err != nil {
|
2014-06-20 08:29:50 +08:00
|
|
|
return nil, fmt.Errorf("getting CgroupData %s", err)
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2014-05-28 08:01:08 +08:00
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
for sysname, sys := range subsystems {
|
|
|
|
path, err := d.path(sysname)
|
|
|
|
if err != nil {
|
|
|
|
// Don't fail if a cgroup hierarchy was not found, just skip this subsystem
|
|
|
|
if err == cgroups.ErrNotFound {
|
|
|
|
continue
|
|
|
|
}
|
2014-07-12 03:05:59 +08:00
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sys.GetStats(path, stats); err != nil {
|
|
|
|
return nil, err
|
2014-05-28 08:01:08 +08:00
|
|
|
}
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2014-05-28 08:01:08 +08:00
|
|
|
|
|
|
|
return stats, nil
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2014-05-31 06:09:07 +08:00
|
|
|
// Freeze toggles the container's freezer cgroup depending on the state
|
|
|
|
// provided
|
|
|
|
func Freeze(c *cgroups.Cgroup, state cgroups.FreezerState) error {
|
|
|
|
d, err := getCgroupData(c, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Freezer = state
|
|
|
|
|
|
|
|
freezer := subsystems["freezer"]
|
|
|
|
|
|
|
|
return freezer.Set(d)
|
|
|
|
}
|
|
|
|
|
2014-05-22 04:48:06 +08:00
|
|
|
func GetPids(c *cgroups.Cgroup) ([]int, error) {
|
2014-05-31 06:09:07 +08:00
|
|
|
d, err := getCgroupData(c, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := d.path("devices")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cgroups.ReadProcsFile(dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCgroupData(c *cgroups.Cgroup, pid int) (*data, error) {
|
|
|
|
// we can pick any subsystem to find the root
|
2014-05-22 04:48:06 +08:00
|
|
|
cgroupRoot, err := cgroups.FindCgroupMountpoint("cpu")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cgroupRoot = filepath.Dir(cgroupRoot)
|
|
|
|
|
|
|
|
if _, err := os.Stat(cgroupRoot); err != nil {
|
2014-05-31 06:09:07 +08:00
|
|
|
return nil, fmt.Errorf("cgroups fs not found")
|
2014-05-22 04:48:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cgroup := c.Name
|
|
|
|
if c.Parent != "" {
|
|
|
|
cgroup = filepath.Join(c.Parent, cgroup)
|
|
|
|
}
|
|
|
|
|
2014-05-31 06:09:07 +08:00
|
|
|
return &data{
|
2014-05-22 04:48:06 +08:00
|
|
|
root: cgroupRoot,
|
|
|
|
cgroup: cgroup,
|
|
|
|
c: c,
|
2014-05-31 06:09:07 +08:00
|
|
|
pid: pid,
|
|
|
|
}, nil
|
2014-05-22 04:48:06 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
func (raw *data) parent(subsystem string) (string, error) {
|
|
|
|
initPath, err := cgroups.GetInitCgroupDir(subsystem)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Join(raw.root, subsystem, initPath), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (raw *data) path(subsystem string) (string, error) {
|
2014-07-29 09:41:52 +08:00
|
|
|
// If the cgroup name/path is absolute do not look relative to the cgroup of the init process.
|
|
|
|
if filepath.IsAbs(raw.cgroup) {
|
|
|
|
return filepath.Join(raw.root, subsystem, raw.cgroup), nil
|
|
|
|
}
|
2014-05-15 06:21:44 +08:00
|
|
|
parent, err := raw.parent(subsystem)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Join(parent, raw.cgroup), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (raw *data) join(subsystem string) (string, error) {
|
|
|
|
path, err := raw.path(subsystem)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(path, 0755); err != nil && !os.IsExist(err) {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := writeFile(path, "cgroup.procs", strconv.Itoa(raw.pid)); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (raw *data) Cleanup() error {
|
|
|
|
for _, sys := range subsystems {
|
|
|
|
sys.Remove(raw)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeFile(dir, file, data string) error {
|
|
|
|
return ioutil.WriteFile(filepath.Join(dir, file), []byte(data), 0700)
|
|
|
|
}
|
|
|
|
|
2014-06-04 08:25:07 +08:00
|
|
|
func readFile(dir, file string) (string, error) {
|
|
|
|
data, err := ioutil.ReadFile(filepath.Join(dir, file))
|
|
|
|
return string(data), err
|
|
|
|
}
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
func removePath(p string, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if p != "" {
|
|
|
|
return os.RemoveAll(p)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|