2014-05-15 06:21:44 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package systemd
|
|
|
|
|
|
|
|
import (
|
2014-06-04 08:25:07 +08:00
|
|
|
"bytes"
|
2014-05-22 04:48:06 +08:00
|
|
|
"fmt"
|
2014-05-15 06:21:44 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2014-06-04 08:25:07 +08:00
|
|
|
"time"
|
2014-05-15 06:21:44 +08:00
|
|
|
|
2014-08-19 20:27:31 +08:00
|
|
|
systemd "github.com/coreos/go-systemd/dbus"
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer/cgroups"
|
2014-06-20 21:13:56 +08:00
|
|
|
"github.com/docker/libcontainer/cgroups/fs"
|
2015-02-01 11:56:27 +08:00
|
|
|
"github.com/docker/libcontainer/configs"
|
2014-05-15 06:21:44 +08:00
|
|
|
"github.com/godbus/dbus"
|
|
|
|
)
|
|
|
|
|
2015-01-13 05:54:00 +08:00
|
|
|
type Manager struct {
|
2015-02-01 11:56:27 +08:00
|
|
|
Cgroups *configs.Cgroup
|
2015-01-14 23:47:26 +08:00
|
|
|
Paths map[string]string
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
type subsystem interface {
|
2015-02-25 17:20:01 +08:00
|
|
|
// 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
|
2014-06-20 21:13:56 +08:00
|
|
|
}
|
|
|
|
|
2015-03-03 06:36:09 +08:00
|
|
|
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{},
|
|
|
|
}
|
|
|
|
|
2015-03-25 08:41:17 +08:00
|
|
|
const (
|
|
|
|
testScopeWait = 4
|
|
|
|
)
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
var (
|
2015-02-04 09:43:21 +08:00
|
|
|
connLock sync.Mutex
|
|
|
|
theConn *systemd.Conn
|
|
|
|
hasStartTransientUnit bool
|
|
|
|
hasTransientDefaultDependencies bool
|
2014-05-15 06:21:44 +08:00
|
|
|
)
|
|
|
|
|
2014-11-06 00:56:47 +08:00
|
|
|
func newProp(name string, units interface{}) systemd.Property {
|
|
|
|
return systemd.Property{
|
|
|
|
Name: name,
|
|
|
|
Value: dbus.MakeVariant(units),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
func UseSystemd() bool {
|
2014-08-19 20:27:31 +08:00
|
|
|
s, err := os.Stat("/run/systemd/system")
|
|
|
|
if err != nil || !s.IsDir() {
|
2014-05-15 06:21:44 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
connLock.Lock()
|
|
|
|
defer connLock.Unlock()
|
|
|
|
|
|
|
|
if theConn == nil {
|
|
|
|
var err error
|
2014-08-19 20:27:31 +08:00
|
|
|
theConn, err = systemd.New()
|
2014-05-15 06:21:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assume we have StartTransientUnit
|
|
|
|
hasStartTransientUnit = true
|
|
|
|
|
|
|
|
// But if we get UnknownMethod error we don't
|
|
|
|
if _, err := theConn.StartTransientUnit("test.scope", "invalid"); err != nil {
|
|
|
|
if dbusError, ok := err.(dbus.Error); ok {
|
|
|
|
if dbusError.Name == "org.freedesktop.DBus.Error.UnknownMethod" {
|
|
|
|
hasStartTransientUnit = false
|
2015-02-04 09:43:21 +08:00
|
|
|
return hasStartTransientUnit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 08:41:17 +08:00
|
|
|
// Ensure the scope name we use doesn't exist. Use the Pid to
|
|
|
|
// avoid collisions between multiple libcontainer users on a
|
|
|
|
// single host.
|
|
|
|
scope := fmt.Sprintf("libcontainer-%d-systemd-test-default-dependencies.scope", os.Getpid())
|
|
|
|
testScopeExists := true
|
|
|
|
for i := 0; i <= testScopeWait; i++ {
|
|
|
|
if _, err := theConn.StopUnit(scope, "replace"); err != nil {
|
|
|
|
if dbusError, ok := err.(dbus.Error); ok {
|
|
|
|
if strings.Contains(dbusError.Name, "org.freedesktop.systemd1.NoSuchUnit") {
|
|
|
|
testScopeExists = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bail out if we can't kill this scope without testing for DefaultDependencies
|
|
|
|
if testScopeExists {
|
|
|
|
return hasStartTransientUnit
|
|
|
|
}
|
|
|
|
|
2015-02-04 09:43:21 +08:00
|
|
|
// Assume StartTransientUnit on a scope allows DefaultDependencies
|
|
|
|
hasTransientDefaultDependencies = true
|
|
|
|
ddf := newProp("DefaultDependencies", false)
|
2015-03-25 08:41:17 +08:00
|
|
|
if _, err := theConn.StartTransientUnit(scope, "replace", ddf); err != nil {
|
2015-02-04 09:43:21 +08:00
|
|
|
if dbusError, ok := err.(dbus.Error); ok {
|
2015-03-20 20:01:55 +08:00
|
|
|
if strings.Contains(dbusError.Name, "org.freedesktop.DBus.Error.PropertyReadOnly") {
|
2015-02-04 09:43:21 +08:00
|
|
|
hasTransientDefaultDependencies = false
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-25 08:41:17 +08:00
|
|
|
|
|
|
|
// Not critical because of the stop unit logic above.
|
|
|
|
theConn.StopUnit(scope, "replace")
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
return hasStartTransientUnit
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIfaceForUnit(unitName string) string {
|
|
|
|
if strings.HasSuffix(unitName, ".scope") {
|
|
|
|
return "Scope"
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(unitName, ".service") {
|
|
|
|
return "Service"
|
|
|
|
}
|
|
|
|
return "Unit"
|
|
|
|
}
|
|
|
|
|
2015-01-13 05:54:00 +08:00
|
|
|
func (m *Manager) Apply(pid int) error {
|
2014-05-15 06:21:44 +08:00
|
|
|
var (
|
2015-01-13 05:54:00 +08:00
|
|
|
c = m.Cgroups
|
2014-05-22 04:48:06 +08:00
|
|
|
unitName = getUnitName(c)
|
2014-05-15 06:21:44 +08:00
|
|
|
slice = "system.slice"
|
2014-08-19 20:27:31 +08:00
|
|
|
properties []systemd.Property
|
2014-05-15 06:21:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if c.Slice != "" {
|
|
|
|
slice = c.Slice
|
|
|
|
}
|
|
|
|
|
|
|
|
properties = append(properties,
|
2014-11-06 00:56:47 +08:00
|
|
|
systemd.PropSlice(slice),
|
|
|
|
systemd.PropDescription("docker container "+c.Name),
|
|
|
|
newProp("PIDs", []uint32{uint32(pid)}),
|
2014-05-15 06:21:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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,
|
2014-11-06 00:56:47 +08:00
|
|
|
newProp("MemoryAccounting", true),
|
|
|
|
newProp("CPUAccounting", true),
|
|
|
|
newProp("BlockIOAccounting", true))
|
2014-05-15 06:21:44 +08:00
|
|
|
|
2015-02-04 09:43:21 +08:00
|
|
|
if hasTransientDefaultDependencies {
|
|
|
|
properties = append(properties,
|
|
|
|
newProp("DefaultDependencies", false))
|
|
|
|
}
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
if c.Memory != 0 {
|
|
|
|
properties = append(properties,
|
2014-11-06 00:56:47 +08:00
|
|
|
newProp("MemoryLimit", uint64(c.Memory)))
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
// TODO: MemoryReservation and MemorySwap not available in systemd
|
|
|
|
|
|
|
|
if c.CpuShares != 0 {
|
|
|
|
properties = append(properties,
|
2014-11-06 00:56:47 +08:00
|
|
|
newProp("CPUShares", uint64(c.CpuShares)))
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2015-01-27 20:54:19 +08:00
|
|
|
if c.BlkioWeight != 0 {
|
|
|
|
properties = append(properties,
|
|
|
|
newProp("BlockIOWeight", uint64(c.BlkioWeight)))
|
|
|
|
}
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
if _, err := theConn.StartTransientUnit(unitName, "replace", properties...); err != nil {
|
2015-01-13 05:54:00 +08:00
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2015-01-22 09:53:30 +08:00
|
|
|
if err := joinDevices(c, pid); err != nil {
|
2015-02-17 05:09:00 +08:00
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-13 14:33:03 +08:00
|
|
|
// 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 {
|
2015-02-20 07:59:50 +08:00
|
|
|
return err
|
2015-02-13 14:33:03 +08:00
|
|
|
}
|
|
|
|
|
2014-08-14 09:00:15 +08:00
|
|
|
// -1 disables memorySwap
|
2015-01-22 12:19:40 +08:00
|
|
|
if c.MemorySwap >= 0 && c.Memory != 0 {
|
2014-08-14 09:00:15 +08:00
|
|
|
if err := joinMemory(c, pid); err != nil {
|
2015-01-13 05:54:00 +08:00
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-11-14 08:15:47 +08:00
|
|
|
// we need to manually join the freezer and cpuset cgroup in systemd
|
|
|
|
// because it does not currently support it via the dbus api.
|
2014-08-14 09:00:15 +08:00
|
|
|
if err := joinFreezer(c, pid); err != nil {
|
2015-01-13 05:54:00 +08:00
|
|
|
return err
|
2014-05-31 06:09:07 +08:00
|
|
|
}
|
2014-05-15 06:21:44 +08:00
|
|
|
|
2014-11-14 08:15:47 +08:00
|
|
|
if err := joinCpuset(c, pid); err != nil {
|
2015-01-13 05:54:00 +08:00
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2014-08-14 07:25:18 +08:00
|
|
|
paths := make(map[string]string)
|
2014-11-15 09:03:40 +08:00
|
|
|
for _, sysname := range []string{
|
|
|
|
"devices",
|
|
|
|
"memory",
|
|
|
|
"cpu",
|
|
|
|
"cpuset",
|
|
|
|
"cpuacct",
|
|
|
|
"blkio",
|
|
|
|
"perf_event",
|
|
|
|
"freezer",
|
|
|
|
} {
|
2015-01-13 05:54:00 +08:00
|
|
|
subsystemPath, err := getSubsystemPath(m.Cgroups, sysname)
|
2014-08-14 07:25:18 +08:00
|
|
|
if err != nil {
|
|
|
|
// Don't fail if a cgroup hierarchy was not found, just skip this subsystem
|
2014-08-21 01:32:01 +08:00
|
|
|
if cgroups.IsNotFound(err) {
|
2014-08-14 07:25:18 +08:00
|
|
|
continue
|
|
|
|
}
|
2015-01-13 05:54:00 +08:00
|
|
|
return err
|
2014-08-14 07:25:18 +08:00
|
|
|
}
|
|
|
|
paths[sysname] = subsystemPath
|
|
|
|
}
|
2015-01-13 05:54:00 +08:00
|
|
|
|
2015-01-14 23:47:26 +08:00
|
|
|
m.Paths = paths
|
2015-01-13 05:54:00 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-14 23:23:42 +08:00
|
|
|
func (m *Manager) Destroy() error {
|
2015-01-14 23:47:26 +08:00
|
|
|
return cgroups.RemovePaths(m.Paths)
|
2015-01-13 05:54:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) GetPaths() map[string]string {
|
2015-01-14 23:47:26 +08:00
|
|
|
return m.Paths
|
2014-08-13 14:18:55 +08:00
|
|
|
}
|
|
|
|
|
2014-11-15 09:22:10 +08:00
|
|
|
func writeFile(dir, file, data string) error {
|
|
|
|
return ioutil.WriteFile(filepath.Join(dir, file), []byte(data), 0700)
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2014-05-22 04:48:06 +08:00
|
|
|
|
2015-02-20 07:59:50 +08:00
|
|
|
func joinCpu(c *configs.Cgroup, pid int) error {
|
2015-02-13 14:33:03 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-02-01 11:56:27 +08:00
|
|
|
func joinFreezer(c *configs.Cgroup, pid int) error {
|
2014-06-20 21:13:56 +08:00
|
|
|
path, err := getSubsystemPath(c, "freezer")
|
2014-05-31 06:09:07 +08:00
|
|
|
if err != nil {
|
2014-08-14 09:00:15 +08:00
|
|
|
return err
|
2014-05-31 06:09:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(path, 0755); err != nil && !os.IsExist(err) {
|
2014-08-14 09:00:15 +08:00
|
|
|
return err
|
2014-05-31 06:09:07 +08:00
|
|
|
}
|
|
|
|
|
2014-08-14 09:00:15 +08:00
|
|
|
return ioutil.WriteFile(filepath.Join(path, "cgroup.procs"), []byte(strconv.Itoa(pid)), 0700)
|
2014-05-31 06:09:07 +08:00
|
|
|
}
|
|
|
|
|
2015-02-01 11:56:27 +08:00
|
|
|
func getSubsystemPath(c *configs.Cgroup, subsystem string) (string, error) {
|
2014-06-20 21:13:56 +08:00
|
|
|
mountpoint, err := cgroups.FindCgroupMountpoint(subsystem)
|
2014-05-31 06:09:07 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
initPath, err := cgroups.GetInitCgroupDir(subsystem)
|
2014-05-31 06:09:07 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
slice := "system.slice"
|
|
|
|
if c.Slice != "" {
|
|
|
|
slice = c.Slice
|
|
|
|
}
|
2014-05-31 06:09:07 +08:00
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
return filepath.Join(mountpoint, initPath, slice, getUnitName(c)), nil
|
2014-05-31 06:09:07 +08:00
|
|
|
}
|
|
|
|
|
2015-02-01 11:56:27 +08:00
|
|
|
func (m *Manager) Freeze(state configs.FreezerState) error {
|
2015-01-13 19:52:14 +08:00
|
|
|
path, err := getSubsystemPath(m.Cgroups, "freezer")
|
2014-05-31 06:09:07 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-06-04 08:25:07 +08:00
|
|
|
if err := ioutil.WriteFile(filepath.Join(path, "freezer.state"), []byte(state), 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
state_, err := ioutil.ReadFile(filepath.Join(path, "freezer.state"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if string(state) == string(bytes.TrimSpace(state_)) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
}
|
2015-01-21 20:36:26 +08:00
|
|
|
|
|
|
|
m.Cgroups.Freezer = state
|
|
|
|
|
2014-06-04 08:25:07 +08:00
|
|
|
return nil
|
2014-05-31 06:09:07 +08:00
|
|
|
}
|
|
|
|
|
2015-01-13 05:54:00 +08:00
|
|
|
func (m *Manager) GetPids() ([]int, error) {
|
|
|
|
path, err := getSubsystemPath(m.Cgroups, "cpu")
|
2014-05-22 04:48:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-08-14 09:00:15 +08:00
|
|
|
return cgroups.ReadProcsFile(path)
|
2014-05-22 04:48:06 +08:00
|
|
|
}
|
|
|
|
|
2015-01-13 05:54:00 +08:00
|
|
|
func (m *Manager) GetStats() (*cgroups.Stats, error) {
|
2015-03-03 06:36:09 +08:00
|
|
|
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
|
2015-01-13 05:54:00 +08:00
|
|
|
}
|
|
|
|
|
2015-02-25 17:20:01 +08:00
|
|
|
func (m *Manager) Set(container *configs.Config) error {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
2015-02-01 11:56:27 +08:00
|
|
|
func getUnitName(c *configs.Cgroup) string {
|
2014-05-22 04:48:06 +08:00
|
|
|
return fmt.Sprintf("%s-%s.scope", c.Parent, c.Name)
|
|
|
|
}
|
2014-06-20 21:13:56 +08:00
|
|
|
|
2014-08-14 09:00:15 +08:00
|
|
|
// Atm we can't use the systemd device support because of two missing things:
|
|
|
|
// * Support for wildcards to allow mknod on any device
|
|
|
|
// * Support for wildcards to allow /dev/pts support
|
|
|
|
//
|
|
|
|
// The second is available in more recent systemd as "char-pts", but not in e.g. v208 which is
|
|
|
|
// in wide use. When both these are availalable we will be able to switch, but need to keep the old
|
|
|
|
// implementation for backwards compat.
|
|
|
|
//
|
|
|
|
// 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.
|
2015-02-01 11:56:27 +08:00
|
|
|
func joinDevices(c *configs.Cgroup, pid int) error {
|
2014-08-14 09:00:15 +08:00
|
|
|
path, err := getSubsystemPath(c, "devices")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(path, 0755); err != nil && !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(path, "cgroup.procs"), []byte(strconv.Itoa(pid)), 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-22 09:53:30 +08:00
|
|
|
if !c.AllowAllDevices {
|
|
|
|
if err := writeFile(path, "devices.deny", "a"); err != nil {
|
2014-08-14 09:00:15 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, dev := range c.AllowedDevices {
|
2015-02-01 11:56:27 +08:00
|
|
|
if err := writeFile(path, "devices.allow", dev.CgroupString()); err != nil {
|
2014-08-14 09:00:15 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-15 04:31:23 +08:00
|
|
|
// Symmetrical public function to update device based cgroups. Also available
|
|
|
|
// in the fs implementation.
|
2015-02-01 11:56:27 +08:00
|
|
|
func ApplyDevices(c *configs.Cgroup, pid int) error {
|
2014-10-15 04:31:23 +08:00
|
|
|
return joinDevices(c, pid)
|
|
|
|
}
|
|
|
|
|
2015-02-01 11:56:27 +08:00
|
|
|
func joinMemory(c *configs.Cgroup, pid int) error {
|
2014-08-14 09:00:15 +08:00
|
|
|
memorySwap := c.MemorySwap
|
|
|
|
|
|
|
|
if memorySwap == 0 {
|
|
|
|
// By default, MemorySwap is set to twice the size of RAM.
|
|
|
|
memorySwap = c.Memory * 2
|
|
|
|
}
|
|
|
|
|
|
|
|
path, err := getSubsystemPath(c, "memory")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(filepath.Join(path, "memory.memsw.limit_in_bytes"), []byte(strconv.FormatInt(memorySwap, 10)), 0700)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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"
|
2015-02-01 11:56:27 +08:00
|
|
|
func joinCpuset(c *configs.Cgroup, pid int) error {
|
2014-08-14 09:00:15 +08:00
|
|
|
path, err := getSubsystemPath(c, "cpuset")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &fs.CpusetGroup{}
|
|
|
|
|
2015-02-27 12:09:42 +08:00
|
|
|
return s.ApplyDir(path, c, pid)
|
2014-08-14 09:00:15 +08:00
|
|
|
}
|