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
|
|
|
}
|
2014-08-06 06:34:59 +08:00
|
|
|
CgroupProcesses = "cgroup.procs"
|
2014-05-15 06:21:44 +08:00
|
|
|
)
|
|
|
|
|
2015-01-13 05:54:00 +08:00
|
|
|
type Manager struct {
|
|
|
|
Cgroups *cgroups.Cgroup
|
|
|
|
paths map[string]string
|
|
|
|
}
|
|
|
|
|
2014-09-11 08:44:13 +08:00
|
|
|
// The absolute path to the root of the cgroup hierarchies.
|
|
|
|
var cgroupRoot string
|
|
|
|
|
|
|
|
// TODO(vmarmol): Report error here, we'll probably need to wait for the new API.
|
|
|
|
func init() {
|
|
|
|
// we can pick any subsystem to find the root
|
|
|
|
cpuRoot, err := cgroups.FindCgroupMountpoint("cpu")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cgroupRoot = filepath.Dir(cpuRoot)
|
|
|
|
|
|
|
|
if _, err := os.Stat(cgroupRoot); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
type subsystem interface {
|
2014-08-06 06:34:59 +08:00
|
|
|
// Returns the stats, as 'stats', corresponding to the cgroup under 'path'.
|
|
|
|
GetStats(path string, stats *cgroups.Stats) error
|
|
|
|
// Removes the cgroup represented by 'data'.
|
2014-08-06 06:12:28 +08:00
|
|
|
Remove(*data) error
|
2014-08-06 06:34:59 +08:00
|
|
|
// Creates and joins the cgroup represented by data.
|
2014-08-06 06:12:28 +08:00
|
|
|
Set(*data) error
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type data struct {
|
|
|
|
root string
|
|
|
|
cgroup string
|
|
|
|
c *cgroups.Cgroup
|
|
|
|
pid int
|
|
|
|
}
|
|
|
|
|
2015-01-13 05:54:00 +08:00
|
|
|
func (m *Manager) Apply(pid int) error {
|
|
|
|
if m.Cgroups == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := getCgroupData(m.Cgroups, pid)
|
2014-05-15 06:21:44 +08:00
|
|
|
if err != nil {
|
2015-01-13 05:54:00 +08:00
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2014-11-15 09:22:10 +08:00
|
|
|
paths := make(map[string]string)
|
2014-11-18 03:55:40 +08:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
cgroups.RemovePaths(paths)
|
|
|
|
}
|
|
|
|
}()
|
2014-11-15 09:22:10 +08:00
|
|
|
for name, sys := range subsystems {
|
2014-05-15 06:21:44 +08:00
|
|
|
if err := sys.Set(d); err != nil {
|
2015-01-13 05:54:00 +08:00
|
|
|
return err
|
2014-11-15 09:22:10 +08:00
|
|
|
}
|
2014-11-18 03:55:40 +08:00
|
|
|
// FIXME: Apply should, ideally, be reentrant or be broken up into a separate
|
|
|
|
// create and join phase so that the cgroup hierarchy for a container can be
|
|
|
|
// created then join consists of writing the process pids to cgroup.procs
|
2014-11-15 09:22:10 +08:00
|
|
|
p, err := d.path(name)
|
|
|
|
if err != nil {
|
|
|
|
if cgroups.IsNotFound(err) {
|
|
|
|
continue
|
|
|
|
}
|
2015-01-13 05:54:00 +08:00
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2014-11-15 09:22:10 +08:00
|
|
|
paths[name] = p
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2015-01-13 05:54:00 +08:00
|
|
|
m.paths = paths
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) RemovePaths() error {
|
|
|
|
return cgroups.RemovePaths(m.paths)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) GetPaths() map[string]string {
|
|
|
|
return m.paths
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) SetPaths(paths map[string]string) {
|
|
|
|
m.paths = paths
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2014-10-15 04:31:23 +08:00
|
|
|
// Symmetrical public function to update device based cgroups. Also available
|
|
|
|
// in the systemd implementation.
|
|
|
|
func ApplyDevices(c *cgroups.Cgroup, pid int) error {
|
|
|
|
d, err := getCgroupData(c, pid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
devices := subsystems["devices"]
|
|
|
|
|
|
|
|
return devices.Set(d)
|
|
|
|
}
|
|
|
|
|
2015-01-13 05:54:00 +08:00
|
|
|
func (m *Manager) GetStats() (*cgroups.Stats, error) {
|
2014-05-28 08:01:08 +08:00
|
|
|
stats := cgroups.NewStats()
|
2015-01-13 05:54:00 +08:00
|
|
|
for name, path := range m.paths {
|
2014-11-15 07:51:29 +08:00
|
|
|
sys, ok := subsystems[name]
|
|
|
|
if !ok {
|
|
|
|
continue
|
2014-06-20 21:13:56 +08:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-01-13 05:54:00 +08:00
|
|
|
func (m *Manager) GetPids() ([]int, error) {
|
|
|
|
d, err := getCgroupData(m.Cgroups, 0)
|
2014-05-31 06:09:07 +08:00
|
|
|
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) {
|
2014-09-11 08:44:13 +08:00
|
|
|
if cgroupRoot == "" {
|
|
|
|
return nil, fmt.Errorf("failed to find the cgroup root")
|
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) {
|
2014-08-21 01:14:56 +08:00
|
|
|
path := filepath.Join(raw.root, subsystem, raw.cgroup)
|
|
|
|
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2014-08-21 01:32:01 +08:00
|
|
|
return "", cgroups.NewNotFoundError(subsystem)
|
2014-08-21 01:14:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return path, nil
|
2014-07-29 09:41:52 +08:00
|
|
|
}
|
2014-08-21 01:14:56 +08:00
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
parent, err := raw.parent(subsystem)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-08-21 01:14:56 +08:00
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
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
|
|
|
|
}
|
2014-08-06 06:34:59 +08:00
|
|
|
if err := writeFile(path, CgroupProcesses, strconv.Itoa(raw.pid)); err != nil {
|
2014-05-15 06:21:44 +08:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return path, 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
|
|
|
|
}
|