2013-12-21 11:02:06 -05:00
|
|
|
package cgroups
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2014-01-20 13:52:26 -05:00
|
|
|
"github.com/dotcloud/docker/pkg/mount"
|
2013-12-21 11:02:06 -05:00
|
|
|
"io"
|
2014-02-20 18:48:48 -05:00
|
|
|
"io/ioutil"
|
2013-12-21 11:02:06 -05:00
|
|
|
"os"
|
2014-02-20 18:48:48 -05:00
|
|
|
"path/filepath"
|
2013-12-21 11:02:06 -05:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-02-20 18:48:48 -05:00
|
|
|
type Cgroup struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Parent string `json:"parent,omitempty"`
|
|
|
|
|
2014-03-21 10:53:47 -04:00
|
|
|
DeviceAccess bool `json:"device_access,omitempty"` // name of parent cgroup or slice
|
|
|
|
Memory int64 `json:"memory,omitempty"` // Memory limit (in bytes)
|
|
|
|
MemorySwap int64 `json:"memory_swap,omitempty"` // Total memory usage (memory + swap); set `-1' to disable swap
|
|
|
|
CpuShares int64 `json:"cpu_shares,omitempty"` // CPU shares (relative weight vs. other containers)
|
|
|
|
CpusetCpus string `json:"cpuset_cpus,omitempty"` // CPU to use
|
2014-02-21 08:35:43 -05:00
|
|
|
|
|
|
|
UnitProperties [][2]string `json:"unit_properties,omitempty"` // systemd unit properties
|
2014-02-20 18:48:48 -05:00
|
|
|
}
|
|
|
|
|
2014-03-14 05:47:49 -04:00
|
|
|
type ActiveCgroup interface {
|
|
|
|
Cleanup() error
|
2014-02-20 18:48:48 -05:00
|
|
|
}
|
|
|
|
|
2014-01-15 20:26:04 -05:00
|
|
|
// https://www.kernel.org/doc/Documentation/cgroups/cgroups.txt
|
2013-12-21 11:02:06 -05:00
|
|
|
func FindCgroupMountpoint(subsystem string) (string, error) {
|
|
|
|
mounts, err := mount.GetMounts()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mount := range mounts {
|
|
|
|
if mount.Fstype == "cgroup" {
|
|
|
|
for _, opt := range strings.Split(mount.VfsOpts, ",") {
|
|
|
|
if opt == subsystem {
|
|
|
|
return mount.Mountpoint, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("cgroup mountpoint not found for %s", subsystem)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the relative path to the cgroup docker is running in.
|
2014-01-28 10:17:51 -05:00
|
|
|
func GetThisCgroupDir(subsystem string) (string, error) {
|
2013-12-21 11:02:06 -05:00
|
|
|
f, err := os.Open("/proc/self/cgroup")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return parseCgroupFile(subsystem, f)
|
|
|
|
}
|
|
|
|
|
2014-02-20 17:12:08 -05:00
|
|
|
func GetInitCgroupDir(subsystem string) (string, error) {
|
|
|
|
f, err := os.Open("/proc/1/cgroup")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return parseCgroupFile(subsystem, f)
|
|
|
|
}
|
|
|
|
|
2013-12-21 11:02:06 -05:00
|
|
|
func parseCgroupFile(subsystem string, r io.Reader) (string, error) {
|
|
|
|
s := bufio.NewScanner(r)
|
|
|
|
for s.Scan() {
|
|
|
|
if err := s.Err(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
text := s.Text()
|
|
|
|
parts := strings.Split(text, ":")
|
2014-02-20 17:12:08 -05:00
|
|
|
for _, subs := range strings.Split(parts[1], ",") {
|
|
|
|
if subs == subsystem {
|
|
|
|
return parts[2], nil
|
|
|
|
}
|
2013-12-21 11:02:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("cgroup '%s' not found in /proc/self/cgroup", subsystem)
|
|
|
|
}
|
2014-02-20 18:48:48 -05:00
|
|
|
|
|
|
|
func writeFile(dir, file, data string) error {
|
|
|
|
return ioutil.WriteFile(filepath.Join(dir, file), []byte(data), 0700)
|
|
|
|
}
|
2014-02-20 19:11:22 -05:00
|
|
|
|
2014-03-14 05:47:49 -04:00
|
|
|
func (c *Cgroup) Apply(pid int) (ActiveCgroup, error) {
|
2014-02-20 19:11:22 -05: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/
|
|
|
|
|
2014-02-21 08:35:43 -05:00
|
|
|
if useSystemd() {
|
|
|
|
return systemdApply(c, pid)
|
|
|
|
} else {
|
|
|
|
return rawApply(c, pid)
|
2014-02-20 19:11:22 -05:00
|
|
|
}
|
|
|
|
}
|