|
@@ -16,21 +16,38 @@ import (
|
|
|
systemd "github.com/coreos/go-systemd/dbus"
|
|
|
"github.com/docker/libcontainer/cgroups"
|
|
|
"github.com/docker/libcontainer/cgroups/fs"
|
|
|
+ "github.com/docker/libcontainer/configs"
|
|
|
"github.com/godbus/dbus"
|
|
|
)
|
|
|
|
|
|
-type systemdCgroup struct {
|
|
|
- cgroup *cgroups.Cgroup
|
|
|
+type Manager struct {
|
|
|
+ Cgroups *configs.Cgroup
|
|
|
+ Paths map[string]string
|
|
|
}
|
|
|
|
|
|
type subsystem interface {
|
|
|
- GetStats(string, *cgroups.Stats) error
|
|
|
+ // Returns the stats, as 'stats', corresponding to the cgroup under 'path'.
|
|
|
+ GetStats(path string, stats *cgroups.Stats) error
|
|
|
+ // Set the cgroup represented by cgroup.
|
|
|
+ Set(path string, cgroup *configs.Cgroup) error
|
|
|
+}
|
|
|
+
|
|
|
+var subsystems = map[string]subsystem{
|
|
|
+ "devices": &fs.DevicesGroup{},
|
|
|
+ "memory": &fs.MemoryGroup{},
|
|
|
+ "cpu": &fs.CpuGroup{},
|
|
|
+ "cpuset": &fs.CpusetGroup{},
|
|
|
+ "cpuacct": &fs.CpuacctGroup{},
|
|
|
+ "blkio": &fs.BlkioGroup{},
|
|
|
+ "perf_event": &fs.PerfEventGroup{},
|
|
|
+ "freezer": &fs.FreezerGroup{},
|
|
|
}
|
|
|
|
|
|
var (
|
|
|
- connLock sync.Mutex
|
|
|
- theConn *systemd.Conn
|
|
|
- hasStartTransientUnit bool
|
|
|
+ connLock sync.Mutex
|
|
|
+ theConn *systemd.Conn
|
|
|
+ hasStartTransientUnit bool
|
|
|
+ hasTransientDefaultDependencies bool
|
|
|
)
|
|
|
|
|
|
func newProp(name string, units interface{}) systemd.Property {
|
|
@@ -64,6 +81,18 @@ func UseSystemd() bool {
|
|
|
if dbusError, ok := err.(dbus.Error); ok {
|
|
|
if dbusError.Name == "org.freedesktop.DBus.Error.UnknownMethod" {
|
|
|
hasStartTransientUnit = false
|
|
|
+ return hasStartTransientUnit
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Assume StartTransientUnit on a scope allows DefaultDependencies
|
|
|
+ hasTransientDefaultDependencies = true
|
|
|
+ ddf := newProp("DefaultDependencies", false)
|
|
|
+ if _, err := theConn.StartTransientUnit("docker-systemd-test-default-dependencies.scope", "replace", ddf); err != nil {
|
|
|
+ if dbusError, ok := err.(dbus.Error); ok {
|
|
|
+ if dbusError.Name == "org.freedesktop.DBus.Error.PropertyReadOnly" {
|
|
|
+ hasTransientDefaultDependencies = false
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -81,16 +110,14 @@ func getIfaceForUnit(unitName string) string {
|
|
|
return "Unit"
|
|
|
}
|
|
|
|
|
|
-func Apply(c *cgroups.Cgroup, pid int) (map[string]string, error) {
|
|
|
+func (m *Manager) Apply(pid int) error {
|
|
|
var (
|
|
|
+ c = m.Cgroups
|
|
|
unitName = getUnitName(c)
|
|
|
slice = "system.slice"
|
|
|
properties []systemd.Property
|
|
|
- res = &systemdCgroup{}
|
|
|
)
|
|
|
|
|
|
- res.cgroup = c
|
|
|
-
|
|
|
if c.Slice != "" {
|
|
|
slice = c.Slice
|
|
|
}
|
|
@@ -108,6 +135,11 @@ func Apply(c *cgroups.Cgroup, pid int) (map[string]string, error) {
|
|
|
newProp("CPUAccounting", true),
|
|
|
newProp("BlockIOAccounting", true))
|
|
|
|
|
|
+ if hasTransientDefaultDependencies {
|
|
|
+ properties = append(properties,
|
|
|
+ newProp("DefaultDependencies", false))
|
|
|
+ }
|
|
|
+
|
|
|
if c.Memory != 0 {
|
|
|
properties = append(properties,
|
|
|
newProp("MemoryLimit", uint64(c.Memory)))
|
|
@@ -119,20 +151,29 @@ func Apply(c *cgroups.Cgroup, pid int) (map[string]string, error) {
|
|
|
newProp("CPUShares", uint64(c.CpuShares)))
|
|
|
}
|
|
|
|
|
|
+ if c.BlkioWeight != 0 {
|
|
|
+ properties = append(properties,
|
|
|
+ newProp("BlockIOWeight", uint64(c.BlkioWeight)))
|
|
|
+ }
|
|
|
+
|
|
|
if _, err := theConn.StartTransientUnit(unitName, "replace", properties...); err != nil {
|
|
|
- return nil, err
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
- if !c.AllowAllDevices {
|
|
|
- if err := joinDevices(c, pid); err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
+ if err := joinDevices(c, pid); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO: CpuQuota and CpuPeriod not available in systemd
|
|
|
+ // we need to manually join the cpu.cfs_quota_us and cpu.cfs_period_us
|
|
|
+ if err := joinCpu(c, pid); err != nil {
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
// -1 disables memorySwap
|
|
|
- if c.MemorySwap >= 0 && (c.Memory != 0 || c.MemorySwap > 0) {
|
|
|
+ if c.MemorySwap >= 0 && c.Memory != 0 {
|
|
|
if err := joinMemory(c, pid); err != nil {
|
|
|
- return nil, err
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -140,11 +181,11 @@ func Apply(c *cgroups.Cgroup, pid int) (map[string]string, error) {
|
|
|
// we need to manually join the freezer and cpuset cgroup in systemd
|
|
|
// because it does not currently support it via the dbus api.
|
|
|
if err := joinFreezer(c, pid); err != nil {
|
|
|
- return nil, err
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
if err := joinCpuset(c, pid); err != nil {
|
|
|
- return nil, err
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
paths := make(map[string]string)
|
|
@@ -158,24 +199,53 @@ func Apply(c *cgroups.Cgroup, pid int) (map[string]string, error) {
|
|
|
"perf_event",
|
|
|
"freezer",
|
|
|
} {
|
|
|
- subsystemPath, err := getSubsystemPath(res.cgroup, sysname)
|
|
|
+ subsystemPath, err := getSubsystemPath(m.Cgroups, sysname)
|
|
|
if err != nil {
|
|
|
// Don't fail if a cgroup hierarchy was not found, just skip this subsystem
|
|
|
if cgroups.IsNotFound(err) {
|
|
|
continue
|
|
|
}
|
|
|
- return nil, err
|
|
|
+ return err
|
|
|
}
|
|
|
paths[sysname] = subsystemPath
|
|
|
}
|
|
|
- return paths, nil
|
|
|
+
|
|
|
+ m.Paths = paths
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func (m *Manager) Destroy() error {
|
|
|
+ return cgroups.RemovePaths(m.Paths)
|
|
|
+}
|
|
|
+
|
|
|
+func (m *Manager) GetPaths() map[string]string {
|
|
|
+ return m.Paths
|
|
|
}
|
|
|
|
|
|
func writeFile(dir, file, data string) error {
|
|
|
return ioutil.WriteFile(filepath.Join(dir, file), []byte(data), 0700)
|
|
|
}
|
|
|
|
|
|
-func joinFreezer(c *cgroups.Cgroup, pid int) error {
|
|
|
+func joinCpu(c *configs.Cgroup, pid int) error {
|
|
|
+ path, err := getSubsystemPath(c, "cpu")
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if c.CpuQuota != 0 {
|
|
|
+ if err = ioutil.WriteFile(filepath.Join(path, "cpu.cfs_quota_us"), []byte(strconv.FormatInt(c.CpuQuota, 10)), 0700); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if c.CpuPeriod != 0 {
|
|
|
+ if err = ioutil.WriteFile(filepath.Join(path, "cpu.cfs_period_us"), []byte(strconv.FormatInt(c.CpuPeriod, 10)), 0700); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func joinFreezer(c *configs.Cgroup, pid int) error {
|
|
|
path, err := getSubsystemPath(c, "freezer")
|
|
|
if err != nil {
|
|
|
return err
|
|
@@ -188,7 +258,7 @@ func joinFreezer(c *cgroups.Cgroup, pid int) error {
|
|
|
return ioutil.WriteFile(filepath.Join(path, "cgroup.procs"), []byte(strconv.Itoa(pid)), 0700)
|
|
|
}
|
|
|
|
|
|
-func getSubsystemPath(c *cgroups.Cgroup, subsystem string) (string, error) {
|
|
|
+func getSubsystemPath(c *configs.Cgroup, subsystem string) (string, error) {
|
|
|
mountpoint, err := cgroups.FindCgroupMountpoint(subsystem)
|
|
|
if err != nil {
|
|
|
return "", err
|
|
@@ -207,8 +277,8 @@ func getSubsystemPath(c *cgroups.Cgroup, subsystem string) (string, error) {
|
|
|
return filepath.Join(mountpoint, initPath, slice, getUnitName(c)), nil
|
|
|
}
|
|
|
|
|
|
-func Freeze(c *cgroups.Cgroup, state cgroups.FreezerState) error {
|
|
|
- path, err := getSubsystemPath(c, "freezer")
|
|
|
+func (m *Manager) Freeze(state configs.FreezerState) error {
|
|
|
+ path, err := getSubsystemPath(m.Cgroups, "freezer")
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
@@ -226,11 +296,14 @@ func Freeze(c *cgroups.Cgroup, state cgroups.FreezerState) error {
|
|
|
}
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
}
|
|
|
+
|
|
|
+ m.Cgroups.Freezer = state
|
|
|
+
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func GetPids(c *cgroups.Cgroup) ([]int, error) {
|
|
|
- path, err := getSubsystemPath(c, "cpu")
|
|
|
+func (m *Manager) GetPids() ([]int, error) {
|
|
|
+ path, err := getSubsystemPath(m.Cgroups, "cpu")
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
@@ -238,7 +311,26 @@ func GetPids(c *cgroups.Cgroup) ([]int, error) {
|
|
|
return cgroups.ReadProcsFile(path)
|
|
|
}
|
|
|
|
|
|
-func getUnitName(c *cgroups.Cgroup) string {
|
|
|
+func (m *Manager) GetStats() (*cgroups.Stats, error) {
|
|
|
+ stats := cgroups.NewStats()
|
|
|
+ for name, path := range m.Paths {
|
|
|
+ sys, ok := subsystems[name]
|
|
|
+ if !ok || !cgroups.PathExists(path) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if err := sys.GetStats(path, stats); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return stats, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (m *Manager) Set(container *configs.Config) error {
|
|
|
+ panic("not implemented")
|
|
|
+}
|
|
|
+
|
|
|
+func getUnitName(c *configs.Cgroup) string {
|
|
|
return fmt.Sprintf("%s-%s.scope", c.Parent, c.Name)
|
|
|
}
|
|
|
|
|
@@ -253,7 +345,7 @@ func getUnitName(c *cgroups.Cgroup) string {
|
|
|
// Note: we can't use systemd to set up the initial limits, and then change the cgroup
|
|
|
// because systemd will re-write the device settings if it needs to re-apply the cgroup context.
|
|
|
// This happens at least for v208 when any sibling unit is started.
|
|
|
-func joinDevices(c *cgroups.Cgroup, pid int) error {
|
|
|
+func joinDevices(c *configs.Cgroup, pid int) error {
|
|
|
path, err := getSubsystemPath(c, "devices")
|
|
|
if err != nil {
|
|
|
return err
|
|
@@ -267,26 +359,26 @@ func joinDevices(c *cgroups.Cgroup, pid int) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- if err := writeFile(path, "devices.deny", "a"); err != nil {
|
|
|
- return err
|
|
|
+ if !c.AllowAllDevices {
|
|
|
+ if err := writeFile(path, "devices.deny", "a"); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
for _, dev := range c.AllowedDevices {
|
|
|
- if err := writeFile(path, "devices.allow", dev.GetCgroupAllowString()); err != nil {
|
|
|
+ if err := writeFile(path, "devices.allow", dev.CgroupString()); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
// Symmetrical public function to update device based cgroups. Also available
|
|
|
// in the fs implementation.
|
|
|
-func ApplyDevices(c *cgroups.Cgroup, pid int) error {
|
|
|
+func ApplyDevices(c *configs.Cgroup, pid int) error {
|
|
|
return joinDevices(c, pid)
|
|
|
}
|
|
|
|
|
|
-func joinMemory(c *cgroups.Cgroup, pid int) error {
|
|
|
+func joinMemory(c *configs.Cgroup, pid int) error {
|
|
|
memorySwap := c.MemorySwap
|
|
|
|
|
|
if memorySwap == 0 {
|
|
@@ -305,7 +397,7 @@ func joinMemory(c *cgroups.Cgroup, pid int) error {
|
|
|
// systemd does not atm set up the cpuset controller, so we must manually
|
|
|
// join it. Additionally that is a very finicky controller where each
|
|
|
// level must have a full setup as the default for a new directory is "no cpus"
|
|
|
-func joinCpuset(c *cgroups.Cgroup, pid int) error {
|
|
|
+func joinCpuset(c *configs.Cgroup, pid int) error {
|
|
|
path, err := getSubsystemPath(c, "cpuset")
|
|
|
if err != nil {
|
|
|
return err
|
|
@@ -313,5 +405,5 @@ func joinCpuset(c *cgroups.Cgroup, pid int) error {
|
|
|
|
|
|
s := &fs.CpusetGroup{}
|
|
|
|
|
|
- return s.SetDir(path, c.CpusetCpus, c.CpusetMems, pid)
|
|
|
+ return s.ApplyDir(path, c, pid)
|
|
|
}
|