Refactor cgroupData

The former cgroup entry is confusing, separate it to parent
and name.
Rename entry `c` to `config`.

Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
This commit is contained in:
Qiang Huang 2015-11-05 18:52:14 +08:00
parent a263afaf6c
commit 8c98ae27ac
20 changed files with 72 additions and 74 deletions

View File

@ -94,8 +94,9 @@ func getCgroupRoot() (string, error) {
type cgroupData struct { type cgroupData struct {
root string root string
cgroup string parent string
c *configs.Cgroup name string
config *configs.Cgroup
pid int pid int
} }
@ -235,15 +236,11 @@ func getCgroupData(c *configs.Cgroup, pid int) (*cgroupData, error) {
return nil, err return nil, err
} }
cgroup := c.Name
if c.Parent != "" {
cgroup = filepath.Join(c.Parent, cgroup)
}
return &cgroupData{ return &cgroupData{
root: root, root: root,
cgroup: cgroup, parent: c.Parent,
c: c, name: c.Name,
config: c,
pid: pid, pid: pid,
}, nil }, nil
} }
@ -267,9 +264,10 @@ func (raw *cgroupData) path(subsystem string) (string, error) {
return "", err 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 the cgroup name/path is absolute do not look relative to the cgroup of the init process.
if filepath.IsAbs(raw.cgroup) { if filepath.IsAbs(cgPath) {
return filepath.Join(raw.root, filepath.Base(mnt), raw.cgroup), nil return filepath.Join(raw.root, filepath.Base(mnt), cgPath), nil
} }
parentPath, err := raw.parentPath(subsystem, mnt, root) parentPath, err := raw.parentPath(subsystem, mnt, root)
@ -277,7 +275,7 @@ func (raw *cgroupData) path(subsystem string) (string, error) {
return "", err return "", err
} }
return filepath.Join(parentPath, raw.cgroup), nil return filepath.Join(parentPath, cgPath), nil
} }
func (raw *cgroupData) join(subsystem string) (string, error) { func (raw *cgroupData) join(subsystem string) (string, error) {

View File

@ -27,7 +27,7 @@ func (s *BlkioGroup) Apply(d *cgroupData) error {
return err return err
} }
if err := s.Set(dir, d.c); err != nil { if err := s.Set(dir, d.config); err != nil {
return err return err
} }

View File

@ -89,9 +89,9 @@ func TestBlkioSetWeight(t *testing.T) {
"blkio.weight": strconv.Itoa(weightBefore), "blkio.weight": strconv.Itoa(weightBefore),
}) })
helper.CgroupData.c.BlkioWeight = weightAfter helper.CgroupData.config.BlkioWeight = weightAfter
blkio := &BlkioGroup{} 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) t.Fatal(err)
} }
@ -120,9 +120,9 @@ func TestBlkioSetWeightDevice(t *testing.T) {
"blkio.weight_device": weightDeviceBefore, "blkio.weight_device": weightDeviceBefore,
}) })
helper.CgroupData.c.BlkioWeightDevice = []*configs.WeightDevice{wd} helper.CgroupData.config.BlkioWeightDevice = []*configs.WeightDevice{wd}
blkio := &BlkioGroup{} 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) t.Fatal(err)
} }
@ -157,9 +157,9 @@ func TestBlkioSetMultipleWeightDevice(t *testing.T) {
"blkio.weight_device": weightDeviceBefore, "blkio.weight_device": weightDeviceBefore,
}) })
helper.CgroupData.c.BlkioWeightDevice = []*configs.WeightDevice{wd1, wd2} helper.CgroupData.config.BlkioWeightDevice = []*configs.WeightDevice{wd1, wd2}
blkio := &BlkioGroup{} 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) t.Fatal(err)
} }
@ -529,9 +529,9 @@ func TestBlkioSetThrottleReadBpsDevice(t *testing.T) {
"blkio.throttle.read_bps_device": throttleBefore, "blkio.throttle.read_bps_device": throttleBefore,
}) })
helper.CgroupData.c.BlkioThrottleReadBpsDevice = []*configs.ThrottleDevice{td} helper.CgroupData.config.BlkioThrottleReadBpsDevice = []*configs.ThrottleDevice{td}
blkio := &BlkioGroup{} 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) t.Fatal(err)
} }
@ -559,9 +559,9 @@ func TestBlkioSetThrottleWriteBpsDevice(t *testing.T) {
"blkio.throttle.write_bps_device": throttleBefore, "blkio.throttle.write_bps_device": throttleBefore,
}) })
helper.CgroupData.c.BlkioThrottleWriteBpsDevice = []*configs.ThrottleDevice{td} helper.CgroupData.config.BlkioThrottleWriteBpsDevice = []*configs.ThrottleDevice{td}
blkio := &BlkioGroup{} 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) t.Fatal(err)
} }
@ -589,9 +589,9 @@ func TestBlkioSetThrottleReadIOpsDevice(t *testing.T) {
"blkio.throttle.read_iops_device": throttleBefore, "blkio.throttle.read_iops_device": throttleBefore,
}) })
helper.CgroupData.c.BlkioThrottleReadIOPSDevice = []*configs.ThrottleDevice{td} helper.CgroupData.config.BlkioThrottleReadIOPSDevice = []*configs.ThrottleDevice{td}
blkio := &BlkioGroup{} 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) t.Fatal(err)
} }
@ -619,9 +619,9 @@ func TestBlkioSetThrottleWriteIOpsDevice(t *testing.T) {
"blkio.throttle.write_iops_device": throttleBefore, "blkio.throttle.write_iops_device": throttleBefore,
}) })
helper.CgroupData.c.BlkioThrottleWriteIOPSDevice = []*configs.ThrottleDevice{td} helper.CgroupData.config.BlkioThrottleWriteIOPSDevice = []*configs.ThrottleDevice{td}
blkio := &BlkioGroup{} 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) t.Fatal(err)
} }

View File

@ -27,7 +27,7 @@ func (s *CpuGroup) Apply(d *cgroupData) error {
return err return err
} }
if err := s.Set(dir, d.c); err != nil { if err := s.Set(dir, d.config); err != nil {
return err return err
} }

View File

@ -23,9 +23,9 @@ func TestCpuSetShares(t *testing.T) {
"cpu.shares": strconv.Itoa(sharesBefore), "cpu.shares": strconv.Itoa(sharesBefore),
}) })
helper.CgroupData.c.CpuShares = sharesAfter helper.CgroupData.config.CpuShares = sharesAfter
cpu := &CpuGroup{} 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) t.Fatal(err)
} }
@ -61,12 +61,12 @@ func TestCpuSetBandWidth(t *testing.T) {
"cpu.rt_period_us": strconv.Itoa(rtPeriodBefore), "cpu.rt_period_us": strconv.Itoa(rtPeriodBefore),
}) })
helper.CgroupData.c.CpuQuota = quotaAfter helper.CgroupData.config.CpuQuota = quotaAfter
helper.CgroupData.c.CpuPeriod = periodAfter helper.CgroupData.config.CpuPeriod = periodAfter
helper.CgroupData.c.CpuRtRuntime = rtRuntimeAfter helper.CgroupData.config.CpuRtRuntime = rtRuntimeAfter
helper.CgroupData.c.CpuRtPeriod = rtPeriodAfter helper.CgroupData.config.CpuRtPeriod = rtPeriodAfter
cpu := &CpuGroup{} 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) t.Fatal(err)
} }

View File

@ -25,7 +25,7 @@ func (s *CpusetGroup) Apply(d *cgroupData) error {
if err != nil && !cgroups.IsNotFound(err) { if err != nil && !cgroups.IsNotFound(err) {
return 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 { func (s *CpusetGroup) Set(path string, cgroup *configs.Cgroup) error {

View File

@ -19,9 +19,9 @@ func TestCpusetSetCpus(t *testing.T) {
"cpuset.cpus": cpusBefore, "cpuset.cpus": cpusBefore,
}) })
helper.CgroupData.c.CpusetCpus = cpusAfter helper.CgroupData.config.CpusetCpus = cpusAfter
cpuset := &CpusetGroup{} 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) t.Fatal(err)
} }
@ -48,9 +48,9 @@ func TestCpusetSetMems(t *testing.T) {
"cpuset.mems": memsBefore, "cpuset.mems": memsBefore,
}) })
helper.CgroupData.c.CpusetMems = memsAfter helper.CgroupData.config.CpusetMems = memsAfter
cpuset := &CpusetGroup{} 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) t.Fatal(err)
} }

View File

@ -22,7 +22,7 @@ func (s *DevicesGroup) Apply(d *cgroupData) error {
return err return err
} }
if err := s.Set(dir, d.c); err != nil { if err := s.Set(dir, d.config); err != nil {
return err return err
} }

View File

@ -41,10 +41,10 @@ func TestDevicesSetAllow(t *testing.T) {
"devices.deny": "a", "devices.deny": "a",
}) })
helper.CgroupData.c.AllowAllDevices = false helper.CgroupData.config.AllowAllDevices = false
helper.CgroupData.c.AllowedDevices = allowedDevices helper.CgroupData.config.AllowedDevices = allowedDevices
devices := &DevicesGroup{} 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) t.Fatal(err)
} }
@ -66,10 +66,10 @@ func TestDevicesSetDeny(t *testing.T) {
"devices.allow": "a", "devices.allow": "a",
}) })
helper.CgroupData.c.AllowAllDevices = true helper.CgroupData.config.AllowAllDevices = true
helper.CgroupData.c.DeniedDevices = deniedDevices helper.CgroupData.config.DeniedDevices = deniedDevices
devices := &DevicesGroup{} 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) t.Fatal(err)
} }

View File

@ -24,7 +24,7 @@ func (s *FreezerGroup) Apply(d *cgroupData) error {
return err return err
} }
if err := s.Set(dir, d.c); err != nil { if err := s.Set(dir, d.config); err != nil {
return err return err
} }

View File

@ -16,9 +16,9 @@ func TestFreezerSetState(t *testing.T) {
"freezer.state": string(configs.Frozen), "freezer.state": string(configs.Frozen),
}) })
helper.CgroupData.c.Freezer = configs.Thawed helper.CgroupData.config.Freezer = configs.Thawed
freezer := &FreezerGroup{} 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) t.Fatal(err)
} }
@ -39,9 +39,9 @@ func TestFreezerSetInvalidState(t *testing.T) {
invalidArg configs.FreezerState = "Invalid" invalidArg configs.FreezerState = "Invalid"
) )
helper.CgroupData.c.Freezer = invalidArg helper.CgroupData.config.Freezer = invalidArg
freezer := &FreezerGroup{} 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") t.Fatal("Failed to return invalid argument error")
} }
} }

View File

@ -24,7 +24,7 @@ func (s *HugetlbGroup) Apply(d *cgroupData) error {
return err return err
} }
if err := s.Set(dir, d.c); err != nil { if err := s.Set(dir, d.config); err != nil {
return err return err
} }

View File

@ -40,14 +40,14 @@ func TestHugetlbSetHugetlb(t *testing.T) {
} }
for _, pageSize := range HugePageSizes { for _, pageSize := range HugePageSizes {
helper.CgroupData.c.HugetlbLimit = []*configs.HugepageLimit{ helper.CgroupData.config.HugetlbLimit = []*configs.HugepageLimit{
{ {
Pagesize: pageSize, Pagesize: pageSize,
Limit: hugetlbAfter, Limit: hugetlbAfter,
}, },
} }
hugetlb := &HugetlbGroup{} 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) t.Fatal(err)
} }
} }

View File

@ -26,14 +26,14 @@ func (s *MemoryGroup) Apply(d *cgroupData) (err error) {
if err != nil && !cgroups.IsNotFound(err) { if err != nil && !cgroups.IsNotFound(err) {
return err return err
} }
if memoryAssigned(d.c) { if memoryAssigned(d.config) {
if path != "" { if path != "" {
if err := os.MkdirAll(path, 0755); err != nil { if err := os.MkdirAll(path, 0755); err != nil {
return err return err
} }
} }
if err := s.Set(path, d.c); err != nil { if err := s.Set(path, d.config); err != nil {
return err return err
} }
} }

View File

@ -33,10 +33,10 @@ func TestMemorySetMemory(t *testing.T) {
"memory.soft_limit_in_bytes": strconv.Itoa(reservationBefore), "memory.soft_limit_in_bytes": strconv.Itoa(reservationBefore),
}) })
helper.CgroupData.c.Memory = memoryAfter helper.CgroupData.config.Memory = memoryAfter
helper.CgroupData.c.MemoryReservation = reservationAfter helper.CgroupData.config.MemoryReservation = reservationAfter
memory := &MemoryGroup{} 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) t.Fatal(err)
} }
@ -70,9 +70,9 @@ func TestMemorySetMemoryswap(t *testing.T) {
"memory.memsw.limit_in_bytes": strconv.Itoa(memoryswapBefore), "memory.memsw.limit_in_bytes": strconv.Itoa(memoryswapBefore),
}) })
helper.CgroupData.c.MemorySwap = memoryswapAfter helper.CgroupData.config.MemorySwap = memoryswapAfter
memory := &MemoryGroup{} 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) t.Fatal(err)
} }
@ -98,9 +98,9 @@ func TestMemorySetKernelMemory(t *testing.T) {
"memory.kmem.limit_in_bytes": strconv.Itoa(kernelMemoryBefore), "memory.kmem.limit_in_bytes": strconv.Itoa(kernelMemoryBefore),
}) })
helper.CgroupData.c.KernelMemory = kernelMemoryAfter helper.CgroupData.config.KernelMemory = kernelMemoryAfter
memory := &MemoryGroup{} 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) t.Fatal(err)
} }
@ -126,9 +126,9 @@ func TestMemorySetMemorySwappinessDefault(t *testing.T) {
"memory.swappiness": strconv.Itoa(swappinessBefore), "memory.swappiness": strconv.Itoa(swappinessBefore),
}) })
helper.CgroupData.c.Memory = swappinessAfter helper.CgroupData.config.Memory = swappinessAfter
memory := &MemoryGroup{} 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) t.Fatal(err)
} }
@ -279,7 +279,7 @@ func TestMemorySetOomControl(t *testing.T) {
}) })
memory := &MemoryGroup{} 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) t.Fatal(err)
} }

View File

@ -20,7 +20,7 @@ func (s *NetClsGroup) Apply(d *cgroupData) error {
return err return err
} }
if err := s.Set(dir, d.c); err != nil { if err := s.Set(dir, d.config); err != nil {
return err return err
} }

View File

@ -19,9 +19,9 @@ func TestNetClsSetClassid(t *testing.T) {
"net_cls.classid": classidBefore, "net_cls.classid": classidBefore,
}) })
helper.CgroupData.c.NetClsClassid = classidAfter helper.CgroupData.config.NetClsClassid = classidAfter
netcls := &NetClsGroup{} 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) t.Fatal(err)
} }

View File

@ -20,7 +20,7 @@ func (s *NetPrioGroup) Apply(d *cgroupData) error {
return err return err
} }
if err := s.Set(dir, d.c); err != nil { if err := s.Set(dir, d.config); err != nil {
return err return err
} }

View File

@ -22,9 +22,9 @@ func TestNetPrioSetIfPrio(t *testing.T) {
helper := NewCgroupTestUtil("net_prio", t) helper := NewCgroupTestUtil("net_prio", t)
defer helper.cleanup() defer helper.cleanup()
helper.CgroupData.c.NetPrioIfpriomap = prioMap helper.CgroupData.config.NetPrioIfpriomap = prioMap
netPrio := &NetPrioGroup{} 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) t.Fatal(err)
} }

View File

@ -31,7 +31,7 @@ type cgroupTestUtil struct {
// Creates a new test util for the specified subsystem // Creates a new test util for the specified subsystem
func NewCgroupTestUtil(subsystem string, t *testing.T) *cgroupTestUtil { func NewCgroupTestUtil(subsystem string, t *testing.T) *cgroupTestUtil {
d := &cgroupData{ d := &cgroupData{
c: &configs.Cgroup{}, config: &configs.Cgroup{},
} }
tempDir, err := ioutil.TempDir("", "cgroup_test") tempDir, err := ioutil.TempDir("", "cgroup_test")
if err != nil { if err != nil {