Skip to content

Commit

Permalink
update per the spec
Browse files Browse the repository at this point in the history
Signed-off-by: Chanwit Kaewkasi <chanwit@gmail.com>
  • Loading branch information
chanwit committed Dec 19, 2015
1 parent d97d5e8 commit 69a1373
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 149 deletions.
10 changes: 5 additions & 5 deletions libcontainer/cgroups/fs/apply_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,10 @@ func removePath(p string, err error) error {
return nil
}

func CheckCpushares(path string, c int64) error {
var cpuShares int64
func CheckCpushares(path string, c *uint64) error {
var cpuShares uint64

if c == 0 {
if c == nil {
return nil
}

Expand All @@ -340,9 +340,9 @@ func CheckCpushares(path string, c int64) error {
return err
}

if c > cpuShares {
if *c > cpuShares {
return fmt.Errorf("The maximum allowed cpu-shares is %d", cpuShares)
} else if c < cpuShares {
} else if *c < cpuShares {
return fmt.Errorf("The minimum allowed cpu-shares is %d", cpuShares)
}

Expand Down
8 changes: 4 additions & 4 deletions libcontainer/cgroups/fs/blkio.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ func (s *BlkioGroup) Apply(d *cgroupData) error {
}

func (s *BlkioGroup) Set(path string, cgroup *configs.Cgroup) error {
if cgroup.Resources.BlkioWeight != 0 {
if err := writeFile(path, "blkio.weight", strconv.FormatUint(uint64(cgroup.Resources.BlkioWeight), 10)); err != nil {
if cgroup.Resources.BlkioWeight != nil {
if err := writeFile(path, "blkio.weight", strconv.FormatUint(uint64(*cgroup.Resources.BlkioWeight), 10)); err != nil {
return err
}
}

if cgroup.Resources.BlkioLeafWeight != 0 {
if err := writeFile(path, "blkio.leaf_weight", strconv.FormatUint(uint64(cgroup.Resources.BlkioLeafWeight), 10)); err != nil {
if cgroup.Resources.BlkioLeafWeight != nil {
if err := writeFile(path, "blkio.leaf_weight", strconv.FormatUint(uint64(*cgroup.Resources.BlkioLeafWeight), 10)); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/cgroups/fs/blkio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestBlkioSetWeight(t *testing.T) {
"blkio.weight": strconv.Itoa(weightBefore),
})

helper.CgroupData.config.Resources.BlkioWeight = weightAfter
*helper.CgroupData.config.Resources.BlkioWeight = weightAfter
blkio := &BlkioGroup{}
if err := blkio.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
t.Fatal(err)
Expand Down
20 changes: 10 additions & 10 deletions libcontainer/cgroups/fs/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,28 @@ func (s *CpuGroup) Apply(d *cgroupData) error {
}

func (s *CpuGroup) Set(path string, cgroup *configs.Cgroup) error {
if cgroup.Resources.CpuShares != 0 {
if err := writeFile(path, "cpu.shares", strconv.FormatInt(cgroup.Resources.CpuShares, 10)); err != nil {
if cgroup.Resources.CpuShares != nil {
if err := writeFile(path, "cpu.shares", strconv.FormatUint(*cgroup.Resources.CpuShares, 10)); err != nil {
return err
}
}
if cgroup.Resources.CpuPeriod != 0 {
if err := writeFile(path, "cpu.cfs_period_us", strconv.FormatInt(cgroup.Resources.CpuPeriod, 10)); err != nil {
if cgroup.Resources.CpuPeriod != nil {
if err := writeFile(path, "cpu.cfs_period_us", strconv.FormatUint(*cgroup.Resources.CpuPeriod, 10)); err != nil {
return err
}
}
if cgroup.Resources.CpuQuota != 0 {
if err := writeFile(path, "cpu.cfs_quota_us", strconv.FormatInt(cgroup.Resources.CpuQuota, 10)); err != nil {
if cgroup.Resources.CpuQuota != nil {
if err := writeFile(path, "cpu.cfs_quota_us", strconv.FormatUint(*cgroup.Resources.CpuQuota, 10)); err != nil {
return err
}
}
if cgroup.Resources.CpuRtPeriod != 0 {
if err := writeFile(path, "cpu.rt_period_us", strconv.FormatInt(cgroup.Resources.CpuRtPeriod, 10)); err != nil {
if cgroup.Resources.CpuRtPeriod != nil {
if err := writeFile(path, "cpu.rt_period_us", strconv.FormatUint(*cgroup.Resources.CpuRtPeriod, 10)); err != nil {
return err
}
}
if cgroup.Resources.CpuRtRuntime != 0 {
if err := writeFile(path, "cpu.rt_runtime_us", strconv.FormatInt(cgroup.Resources.CpuRtRuntime, 10)); err != nil {
if cgroup.Resources.CpuRtRuntime != nil {
if err := writeFile(path, "cpu.rt_runtime_us", strconv.FormatUint(*cgroup.Resources.CpuRtRuntime, 10)); err != nil {
return err
}
}
Expand Down
10 changes: 5 additions & 5 deletions libcontainer/cgroups/fs/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestCpuSetShares(t *testing.T) {
"cpu.shares": strconv.Itoa(sharesBefore),
})

helper.CgroupData.config.Resources.CpuShares = sharesAfter
*helper.CgroupData.config.Resources.CpuShares = sharesAfter
cpu := &CpuGroup{}
if err := cpu.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -61,10 +61,10 @@ func TestCpuSetBandWidth(t *testing.T) {
"cpu.rt_period_us": strconv.Itoa(rtPeriodBefore),
})

helper.CgroupData.config.Resources.CpuQuota = quotaAfter
helper.CgroupData.config.Resources.CpuPeriod = periodAfter
helper.CgroupData.config.Resources.CpuRtRuntime = rtRuntimeAfter
helper.CgroupData.config.Resources.CpuRtPeriod = rtPeriodAfter
*helper.CgroupData.config.Resources.CpuQuota = quotaAfter
*helper.CgroupData.config.Resources.CpuPeriod = periodAfter
*helper.CgroupData.config.Resources.CpuRtRuntime = rtRuntimeAfter
*helper.CgroupData.config.Resources.CpuRtPeriod = rtPeriodAfter
cpu := &CpuGroup{}
if err := cpu.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
t.Fatal(err)
Expand Down
8 changes: 4 additions & 4 deletions libcontainer/cgroups/fs/cpuset.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ func (s *CpusetGroup) Apply(d *cgroupData) error {
}

func (s *CpusetGroup) Set(path string, cgroup *configs.Cgroup) error {
if cgroup.Resources.CpusetCpus != "" {
if err := writeFile(path, "cpuset.cpus", cgroup.Resources.CpusetCpus); err != nil {
if cgroup.Resources.CpusetCpus != nil {
if err := writeFile(path, "cpuset.cpus", *cgroup.Resources.CpusetCpus); err != nil {
return err
}
}
if cgroup.Resources.CpusetMems != "" {
if err := writeFile(path, "cpuset.mems", cgroup.Resources.CpusetMems); err != nil {
if cgroup.Resources.CpusetMems != nil {
if err := writeFile(path, "cpuset.mems", *cgroup.Resources.CpusetMems); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions libcontainer/cgroups/fs/cpuset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestCpusetSetCpus(t *testing.T) {
"cpuset.cpus": cpusBefore,
})

helper.CgroupData.config.Resources.CpusetCpus = cpusAfter
*helper.CgroupData.config.Resources.CpusetCpus = cpusAfter
cpuset := &CpusetGroup{}
if err := cpuset.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -48,7 +48,7 @@ func TestCpusetSetMems(t *testing.T) {
"cpuset.mems": memsBefore,
})

helper.CgroupData.config.Resources.CpusetMems = memsAfter
*helper.CgroupData.config.Resources.CpusetMems = memsAfter
cpuset := &CpusetGroup{}
if err := cpuset.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
t.Fatal(err)
Expand Down
49 changes: 25 additions & 24 deletions libcontainer/cgroups/fs/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,40 +55,42 @@ func (s *MemoryGroup) Apply(d *cgroupData) (err error) {
}

func (s *MemoryGroup) Set(path string, cgroup *configs.Cgroup) error {
if cgroup.Resources.Memory != 0 {
if err := writeFile(path, "memory.limit_in_bytes", strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil {
if cgroup.Resources.Memory != nil {
if err := writeFile(path, "memory.limit_in_bytes", strconv.FormatUint(*cgroup.Resources.Memory, 10)); err != nil {
return err
}
}
if cgroup.Resources.MemoryReservation != 0 {
if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatInt(cgroup.Resources.MemoryReservation, 10)); err != nil {
if cgroup.Resources.MemoryReservation != nil {
if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatUint(*cgroup.Resources.MemoryReservation, 10)); err != nil {
return err
}
}
if cgroup.Resources.MemorySwap > 0 {
if err := writeFile(path, "memory.memsw.limit_in_bytes", strconv.FormatInt(cgroup.Resources.MemorySwap, 10)); err != nil {
if cgroup.Resources.MemorySwap != nil {
if err := writeFile(path, "memory.memsw.limit_in_bytes", strconv.FormatUint(*cgroup.Resources.MemorySwap, 10)); err != nil {
return err
}
}
if cgroup.Resources.KernelMemory > 0 {
if err := writeFile(path, "memory.kmem.limit_in_bytes", strconv.FormatInt(cgroup.Resources.KernelMemory, 10)); err != nil {
if cgroup.Resources.KernelMemory != nil {
if err := writeFile(path, "memory.kmem.limit_in_bytes", strconv.FormatUint(*cgroup.Resources.KernelMemory, 10)); err != nil {
return err
}
}

if cgroup.Resources.OomKillDisable {
if err := writeFile(path, "memory.oom_control", "1"); err != nil {
return err
if cgroup.Resources.OomKillDisable != nil {
if *cgroup.Resources.OomKillDisable {
if err := writeFile(path, "memory.oom_control", "1"); err != nil {
return err
}
}
}
if cgroup.Resources.MemorySwappiness >= 0 && cgroup.Resources.MemorySwappiness <= 100 {
if err := writeFile(path, "memory.swappiness", strconv.FormatInt(cgroup.Resources.MemorySwappiness, 10)); err != nil {
return err
if cgroup.Resources.MemorySwappiness != nil {
if *cgroup.Resources.MemorySwappiness >= 0 && *cgroup.Resources.MemorySwappiness <= 100 {
if err := writeFile(path, "memory.swappiness", strconv.FormatUint(*cgroup.Resources.MemorySwappiness, 10)); err != nil {
return err
}
} else {
return fmt.Errorf("invalid value:%d. valid memory swappiness range is 0-100", *cgroup.Resources.MemorySwappiness)
}
} else if cgroup.Resources.MemorySwappiness == -1 {
return nil
} else {
return fmt.Errorf("invalid value:%d. valid memory swappiness range is 0-100", cgroup.Resources.MemorySwappiness)
}

return nil
Expand Down Expand Up @@ -139,12 +141,11 @@ func (s *MemoryGroup) GetStats(path string, stats *cgroups.Stats) error {
}

func memoryAssigned(cgroup *configs.Cgroup) bool {
return cgroup.Resources.Memory != 0 ||
cgroup.Resources.MemoryReservation != 0 ||
cgroup.Resources.MemorySwap > 0 ||
cgroup.Resources.KernelMemory > 0 ||
cgroup.Resources.OomKillDisable ||
cgroup.Resources.MemorySwappiness != -1
return cgroup.Resources.Memory != nil ||
cgroup.Resources.MemoryReservation != nil ||
cgroup.Resources.MemorySwap != nil ||
cgroup.Resources.KernelMemory != nil ||
cgroup.Resources.OomKillDisable !=nil
}

func getMemoryData(path, name string) (cgroups.MemoryData, error) {
Expand Down
10 changes: 5 additions & 5 deletions libcontainer/cgroups/fs/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func TestMemorySetMemory(t *testing.T) {
"memory.soft_limit_in_bytes": strconv.Itoa(reservationBefore),
})

helper.CgroupData.config.Resources.Memory = memoryAfter
helper.CgroupData.config.Resources.MemoryReservation = reservationAfter
*helper.CgroupData.config.Resources.Memory = memoryAfter
*helper.CgroupData.config.Resources.MemoryReservation = reservationAfter
memory := &MemoryGroup{}
if err := memory.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestMemorySetMemoryswap(t *testing.T) {
"memory.memsw.limit_in_bytes": strconv.Itoa(memoryswapBefore),
})

helper.CgroupData.config.Resources.MemorySwap = memoryswapAfter
*helper.CgroupData.config.Resources.MemorySwap = memoryswapAfter
memory := &MemoryGroup{}
if err := memory.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestMemorySetKernelMemory(t *testing.T) {
"memory.kmem.limit_in_bytes": strconv.Itoa(kernelMemoryBefore),
})

helper.CgroupData.config.Resources.KernelMemory = kernelMemoryAfter
*helper.CgroupData.config.Resources.KernelMemory = kernelMemoryAfter
memory := &MemoryGroup{}
if err := memory.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -126,7 +126,7 @@ func TestMemorySetMemorySwappinessDefault(t *testing.T) {
"memory.swappiness": strconv.Itoa(swappinessBefore),
})

helper.CgroupData.config.Resources.Memory = swappinessAfter
*helper.CgroupData.config.Resources.Memory = swappinessAfter
memory := &MemoryGroup{}
if err := memory.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
t.Fatal(err)
Expand Down
56 changes: 27 additions & 29 deletions libcontainer/cgroups/systemd/apply_systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,26 +189,26 @@ func (m *Manager) Apply(pid int) error {
newProp("DefaultDependencies", false))
}

if c.Resources.Memory != 0 {
if *c.Resources.Memory != 0 {
properties = append(properties,
newProp("MemoryLimit", uint64(c.Resources.Memory)))
newProp("MemoryLimit", *c.Resources.Memory))
}

if c.Resources.CpuShares != 0 {
if *c.Resources.CpuShares != 0 {
properties = append(properties,
newProp("CPUShares", uint64(c.Resources.CpuShares)))
newProp("CPUShares", *c.Resources.CpuShares))
}

if c.Resources.BlkioWeight != 0 {
if *c.Resources.BlkioWeight != 0 {
properties = append(properties,
newProp("BlockIOWeight", uint64(c.Resources.BlkioWeight)))
newProp("BlockIOWeight", *c.Resources.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.Resources.KernelMemory > 0 {
if *c.Resources.KernelMemory > 0 {
if err := setKernelMemory(c); err != nil {
return err
}
Expand Down Expand Up @@ -334,23 +334,23 @@ func joinCpu(c *configs.Cgroup, pid int) error {
if err != nil && !cgroups.IsNotFound(err) {
return err
}
if c.Resources.CpuQuota != 0 {
if err = writeFile(path, "cpu.cfs_quota_us", strconv.FormatInt(c.Resources.CpuQuota, 10)); err != nil {
if *c.Resources.CpuQuota != 0 {
if err = writeFile(path, "cpu.cfs_quota_us", strconv.FormatUint(*c.Resources.CpuQuota, 10)); err != nil {
return err
}
}
if c.Resources.CpuPeriod != 0 {
if err = writeFile(path, "cpu.cfs_period_us", strconv.FormatInt(c.Resources.CpuPeriod, 10)); err != nil {
if *c.Resources.CpuPeriod != 0 {
if err = writeFile(path, "cpu.cfs_period_us", strconv.FormatUint(*c.Resources.CpuPeriod, 10)); err != nil {
return err
}
}
if c.Resources.CpuRtPeriod != 0 {
if err = writeFile(path, "cpu.rt_period_us", strconv.FormatInt(c.Resources.CpuRtPeriod, 10)); err != nil {
if *c.Resources.CpuRtPeriod != 0 {
if err = writeFile(path, "cpu.rt_period_us", strconv.FormatUint(*c.Resources.CpuRtPeriod, 10)); err != nil {
return err
}
}
if c.Resources.CpuRtRuntime != 0 {
if err = writeFile(path, "cpu.rt_runtime_us", strconv.FormatInt(c.Resources.CpuRtRuntime, 10)); err != nil {
if *c.Resources.CpuRtRuntime != 0 {
if err = writeFile(path, "cpu.rt_runtime_us", strconv.FormatUint(*c.Resources.CpuRtRuntime, 10)); err != nil {
return err
}
}
Expand Down Expand Up @@ -510,8 +510,8 @@ func setKernelMemory(c *configs.Cgroup) error {
return err
}

if c.Resources.KernelMemory > 0 {
err = writeFile(path, "memory.kmem.limit_in_bytes", strconv.FormatInt(c.Resources.KernelMemory, 10))
if *c.Resources.KernelMemory > 0 {
err = writeFile(path, "memory.kmem.limit_in_bytes", strconv.FormatUint(*c.Resources.KernelMemory, 10))
if err != nil {
return err
}
Expand All @@ -527,33 +527,31 @@ func joinMemory(c *configs.Cgroup, pid int) error {
}

// -1 disables memoryswap
if c.Resources.MemorySwap > 0 {
err = writeFile(path, "memory.memsw.limit_in_bytes", strconv.FormatInt(c.Resources.MemorySwap, 10))
if *c.Resources.MemorySwap > 0 {
err = writeFile(path, "memory.memsw.limit_in_bytes", strconv.FormatUint(*c.Resources.MemorySwap, 10))
if err != nil {
return err
}
}
if c.Resources.MemoryReservation > 0 {
err = writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatInt(c.Resources.MemoryReservation, 10))
if *c.Resources.MemoryReservation > 0 {
err = writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatUint(*c.Resources.MemoryReservation, 10))
if err != nil {
return err
}
}
if c.Resources.OomKillDisable {
if *c.Resources.OomKillDisable {
if err := writeFile(path, "memory.oom_control", "1"); err != nil {
return err
}
}

if c.Resources.MemorySwappiness >= 0 && c.Resources.MemorySwappiness <= 100 {
err = writeFile(path, "memory.swappiness", strconv.FormatInt(c.Resources.MemorySwappiness, 10))
if *c.Resources.MemorySwappiness >= 0 && *c.Resources.MemorySwappiness <= 100 {
err = writeFile(path, "memory.swappiness", strconv.FormatUint(*c.Resources.MemorySwappiness, 10))
if err != nil {
return err
}
} else if c.Resources.MemorySwappiness == -1 {
return nil
} else {
return fmt.Errorf("invalid value:%d. valid memory swappiness range is 0-100", c.Resources.MemorySwappiness)
return fmt.Errorf("invalid value:%d. valid memory swappiness range is 0-100", *c.Resources.MemorySwappiness)
}

return nil
Expand Down Expand Up @@ -582,8 +580,8 @@ func joinBlkio(c *configs.Cgroup, pid int) error {
return err
}
// systemd doesn't directly support this in the dbus properties
if c.Resources.BlkioLeafWeight != 0 {
if err := writeFile(path, "blkio.leaf_weight", strconv.FormatUint(uint64(c.Resources.BlkioLeafWeight), 10)); err != nil {
if *c.Resources.BlkioLeafWeight != 0 {
if err := writeFile(path, "blkio.leaf_weight", strconv.FormatUint(uint64(*c.Resources.BlkioLeafWeight), 10)); err != nil {
return err
}
}
Expand Down
Loading

0 comments on commit 69a1373

Please sign in to comment.