From 8c98ae27acf6f271ec89c074dbfb53712e2865a0 Mon Sep 17 00:00:00 2001 From: Qiang Huang Date: Thu, 5 Nov 2015 18:52:14 +0800 Subject: [PATCH] Refactor cgroupData The former cgroup entry is confusing, separate it to parent and name. Rename entry `c` to `config`. Signed-off-by: Qiang Huang --- libcontainer/cgroups/fs/apply_raw.go | 22 +++++++++---------- libcontainer/cgroups/fs/blkio.go | 2 +- libcontainer/cgroups/fs/blkio_test.go | 28 ++++++++++++------------ libcontainer/cgroups/fs/cpu.go | 2 +- libcontainer/cgroups/fs/cpu_test.go | 14 ++++++------ libcontainer/cgroups/fs/cpuset.go | 2 +- libcontainer/cgroups/fs/cpuset_test.go | 8 +++---- libcontainer/cgroups/fs/devices.go | 2 +- libcontainer/cgroups/fs/devices_test.go | 12 +++++----- libcontainer/cgroups/fs/freezer.go | 2 +- libcontainer/cgroups/fs/freezer_test.go | 8 +++---- libcontainer/cgroups/fs/hugetlb.go | 2 +- libcontainer/cgroups/fs/hugetlb_test.go | 4 ++-- libcontainer/cgroups/fs/memory.go | 4 ++-- libcontainer/cgroups/fs/memory_test.go | 20 ++++++++--------- libcontainer/cgroups/fs/net_cls.go | 2 +- libcontainer/cgroups/fs/net_cls_test.go | 4 ++-- libcontainer/cgroups/fs/net_prio.go | 2 +- libcontainer/cgroups/fs/net_prio_test.go | 4 ++-- libcontainer/cgroups/fs/util_test.go | 2 +- 20 files changed, 72 insertions(+), 74 deletions(-) diff --git a/libcontainer/cgroups/fs/apply_raw.go b/libcontainer/cgroups/fs/apply_raw.go index 748bc0438..a6cf181ed 100644 --- a/libcontainer/cgroups/fs/apply_raw.go +++ b/libcontainer/cgroups/fs/apply_raw.go @@ -94,8 +94,9 @@ func getCgroupRoot() (string, error) { type cgroupData struct { root string - cgroup string - c *configs.Cgroup + parent string + name string + config *configs.Cgroup pid int } @@ -235,15 +236,11 @@ func getCgroupData(c *configs.Cgroup, pid int) (*cgroupData, error) { return nil, err } - cgroup := c.Name - if c.Parent != "" { - cgroup = filepath.Join(c.Parent, cgroup) - } - return &cgroupData{ root: root, - cgroup: cgroup, - c: c, + parent: c.Parent, + name: c.Name, + config: c, pid: pid, }, nil } @@ -267,9 +264,10 @@ func (raw *cgroupData) path(subsystem string) (string, error) { return "", err } + cgPath := filepath.Join(raw.parent, raw.name) // If the cgroup name/path is absolute do not look relative to the cgroup of the init process. - if filepath.IsAbs(raw.cgroup) { - return filepath.Join(raw.root, filepath.Base(mnt), raw.cgroup), nil + if filepath.IsAbs(cgPath) { + return filepath.Join(raw.root, filepath.Base(mnt), cgPath), nil } parentPath, err := raw.parentPath(subsystem, mnt, root) @@ -277,7 +275,7 @@ func (raw *cgroupData) path(subsystem string) (string, error) { return "", err } - return filepath.Join(parentPath, raw.cgroup), nil + return filepath.Join(parentPath, cgPath), nil } func (raw *cgroupData) join(subsystem string) (string, error) { diff --git a/libcontainer/cgroups/fs/blkio.go b/libcontainer/cgroups/fs/blkio.go index 8e51e887d..eddba0bf8 100644 --- a/libcontainer/cgroups/fs/blkio.go +++ b/libcontainer/cgroups/fs/blkio.go @@ -27,7 +27,7 @@ func (s *BlkioGroup) Apply(d *cgroupData) error { return err } - if err := s.Set(dir, d.c); err != nil { + if err := s.Set(dir, d.config); err != nil { return err } diff --git a/libcontainer/cgroups/fs/blkio_test.go b/libcontainer/cgroups/fs/blkio_test.go index ba312f3a8..def10795a 100644 --- a/libcontainer/cgroups/fs/blkio_test.go +++ b/libcontainer/cgroups/fs/blkio_test.go @@ -89,9 +89,9 @@ func TestBlkioSetWeight(t *testing.T) { "blkio.weight": strconv.Itoa(weightBefore), }) - helper.CgroupData.c.BlkioWeight = weightAfter + helper.CgroupData.config.BlkioWeight = weightAfter blkio := &BlkioGroup{} - if err := blkio.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := blkio.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } @@ -120,9 +120,9 @@ func TestBlkioSetWeightDevice(t *testing.T) { "blkio.weight_device": weightDeviceBefore, }) - helper.CgroupData.c.BlkioWeightDevice = []*configs.WeightDevice{wd} + helper.CgroupData.config.BlkioWeightDevice = []*configs.WeightDevice{wd} blkio := &BlkioGroup{} - if err := blkio.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := blkio.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } @@ -157,9 +157,9 @@ func TestBlkioSetMultipleWeightDevice(t *testing.T) { "blkio.weight_device": weightDeviceBefore, }) - helper.CgroupData.c.BlkioWeightDevice = []*configs.WeightDevice{wd1, wd2} + helper.CgroupData.config.BlkioWeightDevice = []*configs.WeightDevice{wd1, wd2} blkio := &BlkioGroup{} - if err := blkio.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := blkio.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } @@ -529,9 +529,9 @@ func TestBlkioSetThrottleReadBpsDevice(t *testing.T) { "blkio.throttle.read_bps_device": throttleBefore, }) - helper.CgroupData.c.BlkioThrottleReadBpsDevice = []*configs.ThrottleDevice{td} + helper.CgroupData.config.BlkioThrottleReadBpsDevice = []*configs.ThrottleDevice{td} blkio := &BlkioGroup{} - if err := blkio.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := blkio.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } @@ -559,9 +559,9 @@ func TestBlkioSetThrottleWriteBpsDevice(t *testing.T) { "blkio.throttle.write_bps_device": throttleBefore, }) - helper.CgroupData.c.BlkioThrottleWriteBpsDevice = []*configs.ThrottleDevice{td} + helper.CgroupData.config.BlkioThrottleWriteBpsDevice = []*configs.ThrottleDevice{td} blkio := &BlkioGroup{} - if err := blkio.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := blkio.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } @@ -589,9 +589,9 @@ func TestBlkioSetThrottleReadIOpsDevice(t *testing.T) { "blkio.throttle.read_iops_device": throttleBefore, }) - helper.CgroupData.c.BlkioThrottleReadIOPSDevice = []*configs.ThrottleDevice{td} + helper.CgroupData.config.BlkioThrottleReadIOPSDevice = []*configs.ThrottleDevice{td} blkio := &BlkioGroup{} - if err := blkio.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := blkio.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } @@ -619,9 +619,9 @@ func TestBlkioSetThrottleWriteIOpsDevice(t *testing.T) { "blkio.throttle.write_iops_device": throttleBefore, }) - helper.CgroupData.c.BlkioThrottleWriteIOPSDevice = []*configs.ThrottleDevice{td} + helper.CgroupData.config.BlkioThrottleWriteIOPSDevice = []*configs.ThrottleDevice{td} blkio := &BlkioGroup{} - if err := blkio.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := blkio.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } diff --git a/libcontainer/cgroups/fs/cpu.go b/libcontainer/cgroups/fs/cpu.go index 2eff183f3..762a68fe8 100644 --- a/libcontainer/cgroups/fs/cpu.go +++ b/libcontainer/cgroups/fs/cpu.go @@ -27,7 +27,7 @@ func (s *CpuGroup) Apply(d *cgroupData) error { return err } - if err := s.Set(dir, d.c); err != nil { + if err := s.Set(dir, d.config); err != nil { return err } diff --git a/libcontainer/cgroups/fs/cpu_test.go b/libcontainer/cgroups/fs/cpu_test.go index f3c1782ed..abbe17a25 100644 --- a/libcontainer/cgroups/fs/cpu_test.go +++ b/libcontainer/cgroups/fs/cpu_test.go @@ -23,9 +23,9 @@ func TestCpuSetShares(t *testing.T) { "cpu.shares": strconv.Itoa(sharesBefore), }) - helper.CgroupData.c.CpuShares = sharesAfter + helper.CgroupData.config.CpuShares = sharesAfter cpu := &CpuGroup{} - if err := cpu.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := cpu.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } @@ -61,12 +61,12 @@ func TestCpuSetBandWidth(t *testing.T) { "cpu.rt_period_us": strconv.Itoa(rtPeriodBefore), }) - helper.CgroupData.c.CpuQuota = quotaAfter - helper.CgroupData.c.CpuPeriod = periodAfter - helper.CgroupData.c.CpuRtRuntime = rtRuntimeAfter - helper.CgroupData.c.CpuRtPeriod = rtPeriodAfter + helper.CgroupData.config.CpuQuota = quotaAfter + helper.CgroupData.config.CpuPeriod = periodAfter + helper.CgroupData.config.CpuRtRuntime = rtRuntimeAfter + helper.CgroupData.config.CpuRtPeriod = rtPeriodAfter cpu := &CpuGroup{} - if err := cpu.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := cpu.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } diff --git a/libcontainer/cgroups/fs/cpuset.go b/libcontainer/cgroups/fs/cpuset.go index b0bc08b91..088a665b9 100644 --- a/libcontainer/cgroups/fs/cpuset.go +++ b/libcontainer/cgroups/fs/cpuset.go @@ -25,7 +25,7 @@ func (s *CpusetGroup) Apply(d *cgroupData) error { if err != nil && !cgroups.IsNotFound(err) { return err } - return s.ApplyDir(dir, d.c, d.pid) + return s.ApplyDir(dir, d.config, d.pid) } func (s *CpusetGroup) Set(path string, cgroup *configs.Cgroup) error { diff --git a/libcontainer/cgroups/fs/cpuset_test.go b/libcontainer/cgroups/fs/cpuset_test.go index 44b2f9433..80d9a4399 100644 --- a/libcontainer/cgroups/fs/cpuset_test.go +++ b/libcontainer/cgroups/fs/cpuset_test.go @@ -19,9 +19,9 @@ func TestCpusetSetCpus(t *testing.T) { "cpuset.cpus": cpusBefore, }) - helper.CgroupData.c.CpusetCpus = cpusAfter + helper.CgroupData.config.CpusetCpus = cpusAfter cpuset := &CpusetGroup{} - if err := cpuset.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := cpuset.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } @@ -48,9 +48,9 @@ func TestCpusetSetMems(t *testing.T) { "cpuset.mems": memsBefore, }) - helper.CgroupData.c.CpusetMems = memsAfter + helper.CgroupData.config.CpusetMems = memsAfter cpuset := &CpusetGroup{} - if err := cpuset.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := cpuset.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } diff --git a/libcontainer/cgroups/fs/devices.go b/libcontainer/cgroups/fs/devices.go index 39fa82b39..1e39618a4 100644 --- a/libcontainer/cgroups/fs/devices.go +++ b/libcontainer/cgroups/fs/devices.go @@ -22,7 +22,7 @@ func (s *DevicesGroup) Apply(d *cgroupData) error { return err } - if err := s.Set(dir, d.c); err != nil { + if err := s.Set(dir, d.config); err != nil { return err } diff --git a/libcontainer/cgroups/fs/devices_test.go b/libcontainer/cgroups/fs/devices_test.go index f84226445..b85cf7caf 100644 --- a/libcontainer/cgroups/fs/devices_test.go +++ b/libcontainer/cgroups/fs/devices_test.go @@ -41,10 +41,10 @@ func TestDevicesSetAllow(t *testing.T) { "devices.deny": "a", }) - helper.CgroupData.c.AllowAllDevices = false - helper.CgroupData.c.AllowedDevices = allowedDevices + helper.CgroupData.config.AllowAllDevices = false + helper.CgroupData.config.AllowedDevices = allowedDevices devices := &DevicesGroup{} - if err := devices.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := devices.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } @@ -66,10 +66,10 @@ func TestDevicesSetDeny(t *testing.T) { "devices.allow": "a", }) - helper.CgroupData.c.AllowAllDevices = true - helper.CgroupData.c.DeniedDevices = deniedDevices + helper.CgroupData.config.AllowAllDevices = true + helper.CgroupData.config.DeniedDevices = deniedDevices devices := &DevicesGroup{} - if err := devices.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := devices.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } diff --git a/libcontainer/cgroups/fs/freezer.go b/libcontainer/cgroups/fs/freezer.go index 79170cda4..8960ec789 100644 --- a/libcontainer/cgroups/fs/freezer.go +++ b/libcontainer/cgroups/fs/freezer.go @@ -24,7 +24,7 @@ func (s *FreezerGroup) Apply(d *cgroupData) error { return err } - if err := s.Set(dir, d.c); err != nil { + if err := s.Set(dir, d.config); err != nil { return err } diff --git a/libcontainer/cgroups/fs/freezer_test.go b/libcontainer/cgroups/fs/freezer_test.go index d5b107aff..c4cd7fba9 100644 --- a/libcontainer/cgroups/fs/freezer_test.go +++ b/libcontainer/cgroups/fs/freezer_test.go @@ -16,9 +16,9 @@ func TestFreezerSetState(t *testing.T) { "freezer.state": string(configs.Frozen), }) - helper.CgroupData.c.Freezer = configs.Thawed + helper.CgroupData.config.Freezer = configs.Thawed freezer := &FreezerGroup{} - if err := freezer.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := freezer.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } @@ -39,9 +39,9 @@ func TestFreezerSetInvalidState(t *testing.T) { invalidArg configs.FreezerState = "Invalid" ) - helper.CgroupData.c.Freezer = invalidArg + helper.CgroupData.config.Freezer = invalidArg freezer := &FreezerGroup{} - if err := freezer.Set(helper.CgroupPath, helper.CgroupData.c); err == nil { + if err := freezer.Set(helper.CgroupPath, helper.CgroupData.config); err == nil { t.Fatal("Failed to return invalid argument error") } } diff --git a/libcontainer/cgroups/fs/hugetlb.go b/libcontainer/cgroups/fs/hugetlb.go index e9ced13b9..b11365370 100644 --- a/libcontainer/cgroups/fs/hugetlb.go +++ b/libcontainer/cgroups/fs/hugetlb.go @@ -24,7 +24,7 @@ func (s *HugetlbGroup) Apply(d *cgroupData) error { return err } - if err := s.Set(dir, d.c); err != nil { + if err := s.Set(dir, d.config); err != nil { return err } diff --git a/libcontainer/cgroups/fs/hugetlb_test.go b/libcontainer/cgroups/fs/hugetlb_test.go index 4eeb3fca8..132a2640a 100644 --- a/libcontainer/cgroups/fs/hugetlb_test.go +++ b/libcontainer/cgroups/fs/hugetlb_test.go @@ -40,14 +40,14 @@ func TestHugetlbSetHugetlb(t *testing.T) { } for _, pageSize := range HugePageSizes { - helper.CgroupData.c.HugetlbLimit = []*configs.HugepageLimit{ + helper.CgroupData.config.HugetlbLimit = []*configs.HugepageLimit{ { Pagesize: pageSize, Limit: hugetlbAfter, }, } hugetlb := &HugetlbGroup{} - if err := hugetlb.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := hugetlb.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } } diff --git a/libcontainer/cgroups/fs/memory.go b/libcontainer/cgroups/fs/memory.go index 3e83bbac2..e5ffde4b6 100644 --- a/libcontainer/cgroups/fs/memory.go +++ b/libcontainer/cgroups/fs/memory.go @@ -26,14 +26,14 @@ func (s *MemoryGroup) Apply(d *cgroupData) (err error) { if err != nil && !cgroups.IsNotFound(err) { return err } - if memoryAssigned(d.c) { + if memoryAssigned(d.config) { if path != "" { if err := os.MkdirAll(path, 0755); err != nil { return err } } - if err := s.Set(path, d.c); err != nil { + if err := s.Set(path, d.config); err != nil { return err } } diff --git a/libcontainer/cgroups/fs/memory_test.go b/libcontainer/cgroups/fs/memory_test.go index d7b5e5f43..36b926849 100644 --- a/libcontainer/cgroups/fs/memory_test.go +++ b/libcontainer/cgroups/fs/memory_test.go @@ -33,10 +33,10 @@ func TestMemorySetMemory(t *testing.T) { "memory.soft_limit_in_bytes": strconv.Itoa(reservationBefore), }) - helper.CgroupData.c.Memory = memoryAfter - helper.CgroupData.c.MemoryReservation = reservationAfter + helper.CgroupData.config.Memory = memoryAfter + helper.CgroupData.config.MemoryReservation = reservationAfter memory := &MemoryGroup{} - if err := memory.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := memory.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } @@ -70,9 +70,9 @@ func TestMemorySetMemoryswap(t *testing.T) { "memory.memsw.limit_in_bytes": strconv.Itoa(memoryswapBefore), }) - helper.CgroupData.c.MemorySwap = memoryswapAfter + helper.CgroupData.config.MemorySwap = memoryswapAfter memory := &MemoryGroup{} - if err := memory.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := memory.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } @@ -98,9 +98,9 @@ func TestMemorySetKernelMemory(t *testing.T) { "memory.kmem.limit_in_bytes": strconv.Itoa(kernelMemoryBefore), }) - helper.CgroupData.c.KernelMemory = kernelMemoryAfter + helper.CgroupData.config.KernelMemory = kernelMemoryAfter memory := &MemoryGroup{} - if err := memory.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := memory.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } @@ -126,9 +126,9 @@ func TestMemorySetMemorySwappinessDefault(t *testing.T) { "memory.swappiness": strconv.Itoa(swappinessBefore), }) - helper.CgroupData.c.Memory = swappinessAfter + helper.CgroupData.config.Memory = swappinessAfter memory := &MemoryGroup{} - if err := memory.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := memory.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } @@ -279,7 +279,7 @@ func TestMemorySetOomControl(t *testing.T) { }) memory := &MemoryGroup{} - if err := memory.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := memory.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } diff --git a/libcontainer/cgroups/fs/net_cls.go b/libcontainer/cgroups/fs/net_cls.go index 985f80b45..b09a1760e 100644 --- a/libcontainer/cgroups/fs/net_cls.go +++ b/libcontainer/cgroups/fs/net_cls.go @@ -20,7 +20,7 @@ func (s *NetClsGroup) Apply(d *cgroupData) error { return err } - if err := s.Set(dir, d.c); err != nil { + if err := s.Set(dir, d.config); err != nil { return err } diff --git a/libcontainer/cgroups/fs/net_cls_test.go b/libcontainer/cgroups/fs/net_cls_test.go index 439e656d6..a9a7d08c1 100644 --- a/libcontainer/cgroups/fs/net_cls_test.go +++ b/libcontainer/cgroups/fs/net_cls_test.go @@ -19,9 +19,9 @@ func TestNetClsSetClassid(t *testing.T) { "net_cls.classid": classidBefore, }) - helper.CgroupData.c.NetClsClassid = classidAfter + helper.CgroupData.config.NetClsClassid = classidAfter netcls := &NetClsGroup{} - if err := netcls.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := netcls.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } diff --git a/libcontainer/cgroups/fs/net_prio.go b/libcontainer/cgroups/fs/net_prio.go index 90fc23f1b..59117cad9 100644 --- a/libcontainer/cgroups/fs/net_prio.go +++ b/libcontainer/cgroups/fs/net_prio.go @@ -20,7 +20,7 @@ func (s *NetPrioGroup) Apply(d *cgroupData) error { return err } - if err := s.Set(dir, d.c); err != nil { + if err := s.Set(dir, d.config); err != nil { return err } diff --git a/libcontainer/cgroups/fs/net_prio_test.go b/libcontainer/cgroups/fs/net_prio_test.go index 532392b4f..6722b4df2 100644 --- a/libcontainer/cgroups/fs/net_prio_test.go +++ b/libcontainer/cgroups/fs/net_prio_test.go @@ -22,9 +22,9 @@ func TestNetPrioSetIfPrio(t *testing.T) { helper := NewCgroupTestUtil("net_prio", t) defer helper.cleanup() - helper.CgroupData.c.NetPrioIfpriomap = prioMap + helper.CgroupData.config.NetPrioIfpriomap = prioMap netPrio := &NetPrioGroup{} - if err := netPrio.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { + if err := netPrio.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { t.Fatal(err) } diff --git a/libcontainer/cgroups/fs/util_test.go b/libcontainer/cgroups/fs/util_test.go index 01bf5f347..cec45babc 100644 --- a/libcontainer/cgroups/fs/util_test.go +++ b/libcontainer/cgroups/fs/util_test.go @@ -31,7 +31,7 @@ type cgroupTestUtil struct { // Creates a new test util for the specified subsystem func NewCgroupTestUtil(subsystem string, t *testing.T) *cgroupTestUtil { d := &cgroupData{ - c: &configs.Cgroup{}, + config: &configs.Cgroup{}, } tempDir, err := ioutil.TempDir("", "cgroup_test") if err != nil {