Skip to content

Commit

Permalink
Merge pull request opencontainers#434 from mrunalp/resources
Browse files Browse the repository at this point in the history
Move the cgroups setting into a Resources struct
  • Loading branch information
hqhq committed Dec 17, 2015
2 parents ac44881 + 55a49f2 commit 9d6ce71
Show file tree
Hide file tree
Showing 26 changed files with 168 additions and 159 deletions.
8 changes: 4 additions & 4 deletions libcontainer/cgroups/fs/apply_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (m *Manager) Apply(pid int) (err error) {
m.Paths = paths

if paths["cpu"] != "" {
if err := CheckCpushares(paths["cpu"], c.CpuShares); err != nil {
if err := CheckCpushares(paths["cpu"], c.Resources.CpuShares); err != nil {
return err
}
}
Expand Down Expand Up @@ -202,15 +202,15 @@ func (m *Manager) Freeze(state configs.FreezerState) error {
if err != nil {
return err
}
prevState := m.Cgroups.Freezer
m.Cgroups.Freezer = state
prevState := m.Cgroups.Resources.Freezer
m.Cgroups.Resources.Freezer = state
freezer, err := subsystems.Get("freezer")
if err != nil {
return err
}
err = freezer.Set(dir, m.Cgroups)
if err != nil {
m.Cgroups.Freezer = prevState
m.Cgroups.Resources.Freezer = prevState
return err
}
return nil
Expand Down
18 changes: 9 additions & 9 deletions libcontainer/cgroups/fs/blkio.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,41 @@ func (s *BlkioGroup) Apply(d *cgroupData) error {
}

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

if cgroup.BlkioLeafWeight != 0 {
if err := writeFile(path, "blkio.leaf_weight", strconv.FormatUint(uint64(cgroup.BlkioLeafWeight), 10)); err != nil {
if cgroup.Resources.BlkioLeafWeight != 0 {
if err := writeFile(path, "blkio.leaf_weight", strconv.FormatUint(uint64(cgroup.Resources.BlkioLeafWeight), 10)); err != nil {
return err
}
}
for _, wd := range cgroup.BlkioWeightDevice {
for _, wd := range cgroup.Resources.BlkioWeightDevice {
if err := writeFile(path, "blkio.weight_device", wd.WeightString()); err != nil {
return err
}
if err := writeFile(path, "blkio.leaf_weight_device", wd.LeafWeightString()); err != nil {
return err
}
}
for _, td := range cgroup.BlkioThrottleReadBpsDevice {
for _, td := range cgroup.Resources.BlkioThrottleReadBpsDevice {
if err := writeFile(path, "blkio.throttle.read_bps_device", td.String()); err != nil {
return err
}
}
for _, td := range cgroup.BlkioThrottleWriteBpsDevice {
for _, td := range cgroup.Resources.BlkioThrottleWriteBpsDevice {
if err := writeFile(path, "blkio.throttle.write_bps_device", td.String()); err != nil {
return err
}
}
for _, td := range cgroup.BlkioThrottleReadIOPSDevice {
for _, td := range cgroup.Resources.BlkioThrottleReadIOPSDevice {
if err := writeFile(path, "blkio.throttle.read_iops_device", td.String()); err != nil {
return err
}
}
for _, td := range cgroup.BlkioThrottleWriteIOPSDevice {
for _, td := range cgroup.Resources.BlkioThrottleWriteIOPSDevice {
if err := writeFile(path, "blkio.throttle.write_iops_device", td.String()); err != nil {
return err
}
Expand Down
14 changes: 7 additions & 7 deletions 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.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 Expand Up @@ -120,7 +120,7 @@ func TestBlkioSetWeightDevice(t *testing.T) {
"blkio.weight_device": weightDeviceBefore,
})

helper.CgroupData.config.BlkioWeightDevice = []*configs.WeightDevice{wd}
helper.CgroupData.config.Resources.BlkioWeightDevice = []*configs.WeightDevice{wd}
blkio := &BlkioGroup{}
if err := blkio.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -157,7 +157,7 @@ func TestBlkioSetMultipleWeightDevice(t *testing.T) {
"blkio.weight_device": weightDeviceBefore,
})

helper.CgroupData.config.BlkioWeightDevice = []*configs.WeightDevice{wd1, wd2}
helper.CgroupData.config.Resources.BlkioWeightDevice = []*configs.WeightDevice{wd1, wd2}
blkio := &BlkioGroup{}
if err := blkio.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -529,7 +529,7 @@ func TestBlkioSetThrottleReadBpsDevice(t *testing.T) {
"blkio.throttle.read_bps_device": throttleBefore,
})

helper.CgroupData.config.BlkioThrottleReadBpsDevice = []*configs.ThrottleDevice{td}
helper.CgroupData.config.Resources.BlkioThrottleReadBpsDevice = []*configs.ThrottleDevice{td}
blkio := &BlkioGroup{}
if err := blkio.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -559,7 +559,7 @@ func TestBlkioSetThrottleWriteBpsDevice(t *testing.T) {
"blkio.throttle.write_bps_device": throttleBefore,
})

helper.CgroupData.config.BlkioThrottleWriteBpsDevice = []*configs.ThrottleDevice{td}
helper.CgroupData.config.Resources.BlkioThrottleWriteBpsDevice = []*configs.ThrottleDevice{td}
blkio := &BlkioGroup{}
if err := blkio.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -589,7 +589,7 @@ func TestBlkioSetThrottleReadIOpsDevice(t *testing.T) {
"blkio.throttle.read_iops_device": throttleBefore,
})

helper.CgroupData.config.BlkioThrottleReadIOPSDevice = []*configs.ThrottleDevice{td}
helper.CgroupData.config.Resources.BlkioThrottleReadIOPSDevice = []*configs.ThrottleDevice{td}
blkio := &BlkioGroup{}
if err := blkio.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -619,7 +619,7 @@ func TestBlkioSetThrottleWriteIOpsDevice(t *testing.T) {
"blkio.throttle.write_iops_device": throttleBefore,
})

helper.CgroupData.config.BlkioThrottleWriteIOPSDevice = []*configs.ThrottleDevice{td}
helper.CgroupData.config.Resources.BlkioThrottleWriteIOPSDevice = []*configs.ThrottleDevice{td}
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.CpuShares != 0 {
if err := writeFile(path, "cpu.shares", strconv.FormatInt(cgroup.CpuShares, 10)); err != nil {
if cgroup.Resources.CpuShares != 0 {
if err := writeFile(path, "cpu.shares", strconv.FormatInt(cgroup.Resources.CpuShares, 10)); err != nil {
return err
}
}
if cgroup.CpuPeriod != 0 {
if err := writeFile(path, "cpu.cfs_period_us", strconv.FormatInt(cgroup.CpuPeriod, 10)); err != nil {
if cgroup.Resources.CpuPeriod != 0 {
if err := writeFile(path, "cpu.cfs_period_us", strconv.FormatInt(cgroup.Resources.CpuPeriod, 10)); err != nil {
return err
}
}
if cgroup.CpuQuota != 0 {
if err := writeFile(path, "cpu.cfs_quota_us", strconv.FormatInt(cgroup.CpuQuota, 10)); err != nil {
if cgroup.Resources.CpuQuota != 0 {
if err := writeFile(path, "cpu.cfs_quota_us", strconv.FormatInt(cgroup.Resources.CpuQuota, 10)); err != nil {
return err
}
}
if cgroup.CpuRtPeriod != 0 {
if err := writeFile(path, "cpu.rt_period_us", strconv.FormatInt(cgroup.CpuRtPeriod, 10)); err != nil {
if cgroup.Resources.CpuRtPeriod != 0 {
if err := writeFile(path, "cpu.rt_period_us", strconv.FormatInt(cgroup.Resources.CpuRtPeriod, 10)); err != nil {
return err
}
}
if cgroup.CpuRtRuntime != 0 {
if err := writeFile(path, "cpu.rt_runtime_us", strconv.FormatInt(cgroup.CpuRtRuntime, 10)); err != nil {
if cgroup.Resources.CpuRtRuntime != 0 {
if err := writeFile(path, "cpu.rt_runtime_us", strconv.FormatInt(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.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.CpuQuota = quotaAfter
helper.CgroupData.config.CpuPeriod = periodAfter
helper.CgroupData.config.CpuRtRuntime = rtRuntimeAfter
helper.CgroupData.config.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.CpusetCpus != "" {
if err := writeFile(path, "cpuset.cpus", cgroup.CpusetCpus); err != nil {
if cgroup.Resources.CpusetCpus != "" {
if err := writeFile(path, "cpuset.cpus", cgroup.Resources.CpusetCpus); err != nil {
return err
}
}
if cgroup.CpusetMems != "" {
if err := writeFile(path, "cpuset.mems", cgroup.CpusetMems); err != nil {
if cgroup.Resources.CpusetMems != "" {
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.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.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
6 changes: 3 additions & 3 deletions libcontainer/cgroups/fs/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ func (s *DevicesGroup) Apply(d *cgroupData) error {
}

func (s *DevicesGroup) Set(path string, cgroup *configs.Cgroup) error {
if !cgroup.AllowAllDevices {
if !cgroup.Resources.AllowAllDevices {
if err := writeFile(path, "devices.deny", "a"); err != nil {
return err
}

for _, dev := range cgroup.AllowedDevices {
for _, dev := range cgroup.Resources.AllowedDevices {
if err := writeFile(path, "devices.allow", dev.CgroupString()); err != nil {
return err
}
Expand All @@ -47,7 +47,7 @@ func (s *DevicesGroup) Set(path string, cgroup *configs.Cgroup) error {
return err
}

for _, dev := range cgroup.DeniedDevices {
for _, dev := range cgroup.Resources.DeniedDevices {
if err := writeFile(path, "devices.deny", dev.CgroupString()); err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions libcontainer/cgroups/fs/devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func TestDevicesSetAllow(t *testing.T) {
"devices.deny": "a",
})

helper.CgroupData.config.AllowAllDevices = false
helper.CgroupData.config.AllowedDevices = allowedDevices
helper.CgroupData.config.Resources.AllowAllDevices = false
helper.CgroupData.config.Resources.AllowedDevices = allowedDevices
devices := &DevicesGroup{}
if err := devices.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
t.Fatal(err)
Expand All @@ -66,8 +66,8 @@ func TestDevicesSetDeny(t *testing.T) {
"devices.allow": "a",
})

helper.CgroupData.config.AllowAllDevices = true
helper.CgroupData.config.DeniedDevices = deniedDevices
helper.CgroupData.config.Resources.AllowAllDevices = true
helper.CgroupData.config.Resources.DeniedDevices = deniedDevices
devices := &DevicesGroup{}
if err := devices.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
t.Fatal(err)
Expand Down
8 changes: 4 additions & 4 deletions libcontainer/cgroups/fs/freezer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func (s *FreezerGroup) Apply(d *cgroupData) error {
}

func (s *FreezerGroup) Set(path string, cgroup *configs.Cgroup) error {
switch cgroup.Freezer {
switch cgroup.Resources.Freezer {
case configs.Frozen, configs.Thawed:
if err := writeFile(path, "freezer.state", string(cgroup.Freezer)); err != nil {
if err := writeFile(path, "freezer.state", string(cgroup.Resources.Freezer)); err != nil {
return err
}

Expand All @@ -43,15 +43,15 @@ func (s *FreezerGroup) Set(path string, cgroup *configs.Cgroup) error {
if err != nil {
return err
}
if strings.TrimSpace(state) == string(cgroup.Freezer) {
if strings.TrimSpace(state) == string(cgroup.Resources.Freezer) {
break
}
time.Sleep(1 * time.Millisecond)
}
case configs.Undefined:
return nil
default:
return fmt.Errorf("Invalid argument '%s' to freezer.state", string(cgroup.Freezer))
return fmt.Errorf("Invalid argument '%s' to freezer.state", string(cgroup.Resources.Freezer))
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions libcontainer/cgroups/fs/freezer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestFreezerSetState(t *testing.T) {
"freezer.state": string(configs.Frozen),
})

helper.CgroupData.config.Freezer = configs.Thawed
helper.CgroupData.config.Resources.Freezer = configs.Thawed
freezer := &FreezerGroup{}
if err := freezer.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
t.Fatal(err)
Expand All @@ -39,7 +39,7 @@ func TestFreezerSetInvalidState(t *testing.T) {
invalidArg configs.FreezerState = "Invalid"
)

helper.CgroupData.config.Freezer = invalidArg
helper.CgroupData.config.Resources.Freezer = invalidArg
freezer := &FreezerGroup{}
if err := freezer.Set(helper.CgroupPath, helper.CgroupData.config); err == nil {
t.Fatal("Failed to return invalid argument error")
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/cgroups/fs/hugetlb.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s *HugetlbGroup) Apply(d *cgroupData) error {
}

func (s *HugetlbGroup) Set(path string, cgroup *configs.Cgroup) error {
for _, hugetlb := range cgroup.HugetlbLimit {
for _, hugetlb := range cgroup.Resources.HugetlbLimit {
if err := writeFile(path, strings.Join([]string{"hugetlb", hugetlb.Pagesize, "limit_in_bytes"}, "."), strconv.FormatUint(hugetlb.Limit, 10)); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/cgroups/fs/hugetlb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestHugetlbSetHugetlb(t *testing.T) {
}

for _, pageSize := range HugePageSizes {
helper.CgroupData.config.HugetlbLimit = []*configs.HugepageLimit{
helper.CgroupData.config.Resources.HugetlbLimit = []*configs.HugepageLimit{
{
Pagesize: pageSize,
Limit: hugetlbAfter,
Expand Down
Loading

0 comments on commit 9d6ce71

Please sign in to comment.