func joinCpu(c *configs.Cgroup, pid int) error { path, err := getSubsystemPath(c, "cpu") if err != nil && !cgroups.IsNotFound(err) { return err } if c.CpuQuota != 0 { if err = writeFile(path, "cpu.cfs_quota_us", strconv.FormatInt(c.CpuQuota, 10)); err != nil { return err } } if c.CpuPeriod != 0 { if err = writeFile(path, "cpu.cfs_period_us", strconv.FormatInt(c.CpuPeriod, 10)); err != nil { return err } } if c.CpuRtPeriod != 0 { if err = writeFile(path, "cpu.rt_period_us", strconv.FormatInt(c.CpuRtPeriod, 10)); err != nil { return err } } if c.CpuRtRuntime != 0 { if err = writeFile(path, "cpu.rt_runtime_us", strconv.FormatInt(c.CpuRtRuntime, 10)); err != nil { return err } } return nil }
func (s *MemoryGroup) Apply(d *data) error { path, err := d.path("memory") if err != nil && !cgroups.IsNotFound(err) { return err } if err := os.MkdirAll(path, 0755); err != nil && !os.IsExist(err) { return err } if err := s.Set(path, d.c); err != nil { return err } // We need to join memory cgroup after set memory limits, because // kmem.limit_in_bytes can only be set when the cgroup is empty. _, err = d.join("memory") if err != nil { return err } defer func() { if err != nil { os.RemoveAll(path) } }() return nil }
func (s *CpusetGroup) Apply(d *data) error { dir, err := d.path("cpuset") if err != nil && !cgroups.IsNotFound(err) { return err } return s.ApplyDir(dir, d.c, d.pid) }
func (s *PerfEventGroup) Apply(d *data) error { // we just want to join this group even though we don't set anything if _, err := d.join("perf_event"); err != nil && !cgroups.IsNotFound(err) { return err } return nil }
func joinHugetlb(c *configs.Cgroup, pid int) error { path, err := join(c, "hugetlb", pid) if err != nil && !cgroups.IsNotFound(err) { return err } hugetlb := subsystems["hugetlb"] return hugetlb.Set(path, c) }
func joinNetCls(c *configs.Cgroup, pid int) error { path, err := join(c, "net_cls", pid) if err != nil && !cgroups.IsNotFound(err) { return err } netcls := subsystems["net_cls"] return netcls.Set(path, c) }
func joinNetPrio(c *configs.Cgroup, pid int) error { path, err := join(c, "net_prio", pid) if err != nil && !cgroups.IsNotFound(err) { return err } netPrio := subsystems["net_prio"] return netPrio.Set(path, c) }
func joinFreezer(c *configs.Cgroup, pid int) error { path, err := join(c, "freezer", pid) if err != nil && !cgroups.IsNotFound(err) { return err } freezer := subsystems["freezer"] return freezer.Set(path, c) }
// 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 *configs.Cgroup, pid int) error { path, err := getSubsystemPath(c, "cpuset") if err != nil && !cgroups.IsNotFound(err) { return err } s := &fs.CpusetGroup{} return s.ApplyDir(path, c, pid) }
func (s *HugetlbGroup) Apply(d *data) error { dir, err := d.join("hugetlb") if err != nil && !cgroups.IsNotFound(err) { return err } if err := s.Set(dir, d.c); err != nil { return err } return nil }
func (s *NetClsGroup) Apply(d *data) error { dir, err := d.join("net_cls") if err != nil && !cgroups.IsNotFound(err) { return err } if err := s.Set(dir, d.c); err != nil { return err } return nil }
func (s *FreezerGroup) Apply(d *data) error { dir, err := d.join("freezer") if err != nil && !cgroups.IsNotFound(err) { return err } if err := s.Set(dir, d.c); err != nil { return err } return nil }
func (m *Manager) Apply(pid int) error { if m.Cgroups == nil { return nil } var c = m.Cgroups d, err := getCgroupData(m.Cgroups, pid) if err != nil { return err } paths := make(map[string]string) defer func() { if err != nil { cgroups.RemovePaths(paths) } }() for name, sys := range subsystems { if err := sys.Apply(d); err != nil { return err } // TODO: 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 p, err := d.path(name) if err != nil { if cgroups.IsNotFound(err) { continue } return err } paths[name] = p } m.Paths = paths if paths["cpu"] != "" { if err := CheckCpushares(paths["cpu"], c.CpuShares); err != nil { return err } } return nil }
func setKernelMemory(c *configs.Cgroup) error { path, err := getSubsystemPath(c, "memory") if err != nil && !cgroups.IsNotFound(err) { return err } if err := os.MkdirAll(path, 0755); err != nil && !os.IsExist(err) { return err } if c.KernelMemory > 0 { err = writeFile(path, "memory.kmem.limit_in_bytes", strconv.FormatInt(c.KernelMemory, 10)) if err != nil { return err } } return nil }
func joinMemory(c *configs.Cgroup, pid int) error { path, err := getSubsystemPath(c, "memory") if err != nil && !cgroups.IsNotFound(err) { return err } // -1 disables memoryswap if c.MemorySwap > 0 { err = writeFile(path, "memory.memsw.limit_in_bytes", strconv.FormatInt(c.MemorySwap, 10)) if err != nil { return err } } if c.MemorySwappiness >= 0 && c.MemorySwappiness <= 100 { err = writeFile(path, "memory.swappiness", strconv.FormatInt(c.MemorySwappiness, 10)) if err != nil { return err } } return nil }
func (m *Manager) Apply(pid int) error { var ( c = m.Cgroups unitName = getUnitName(c) slice = "system.slice" properties []systemd.Property ) if c.Slice != "" { slice = c.Slice } properties = append(properties, systemd.PropSlice(slice), systemd.PropDescription("docker container "+c.Name), newProp("PIDs", []uint32{uint32(pid)}), ) // Always enable accounting, this gets us the same behaviour as the fs implementation, // plus the kernel has some problems with joining the memory cgroup at a later time. properties = append(properties, newProp("MemoryAccounting", true), 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))) } // TODO: MemoryReservation and MemorySwap not available in systemd if c.CpuShares != 0 { properties = append(properties, newProp("CPUShares", uint64(c.CpuShares))) } if c.BlkioWeight != 0 { properties = append(properties, newProp("BlockIOWeight", uint64(c.BlkioWeight))) } // We need to set kernel memory before processes join cgroup because // kmem.limit_in_bytes can only be set when the cgroup is empty. // And swap memory limit needs to be set after memory limit, only // memory limit is handled by systemd, so it's kind of ugly here. if c.KernelMemory > 0 { if err := setKernelMemory(c); err != nil { return err } } if _, err := theConn.StartTransientUnit(unitName, "replace", properties...); err != nil { return 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 } if err := joinMemory(c, pid); err != nil { return err } // we need to manually join the freezer, net_cls, net_prio and cpuset cgroup in systemd // because it does not currently support it via the dbus api. if err := joinFreezer(c, pid); err != nil { return err } if err := joinNetPrio(c, pid); err != nil { return err } if err := joinNetCls(c, pid); err != nil { return err } if err := joinCpuset(c, pid); err != nil { return err } if err := joinHugetlb(c, pid); err != nil { return err } // FIXME: Systemd does have `BlockIODeviceWeight` property, but we got problem // using that (at least on systemd 208, see https://github.com/docker/libcontainer/pull/354), // so use fs work around for now. if err := joinBlkio(c, pid); err != nil { return err } paths := make(map[string]string) for sysname := range subsystems { 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 err } paths[sysname] = subsystemPath } m.Paths = paths if paths["cpu"] != "" { if err := fs.CheckCpushares(paths["cpu"], c.CpuShares); err != nil { return err } } return nil }