From 3a788dd7f3b9c13e6d167675ed364610e16e1848 Mon Sep 17 00:00:00 2001 From: Ma Shimiao Date: Thu, 14 May 2015 20:42:10 +0800 Subject: [PATCH] croup cpu: add support for realtime throttling Signed-off-by: Ma Shimiao --- cgroups/fs/cpu.go | 10 ++++++++++ cgroups/fs/cpu_test.go | 30 ++++++++++++++++++++++++++---- cgroups/systemd/apply_systemd.go | 11 +++++++++++ configs/cgroup.go | 6 ++++++ 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/cgroups/fs/cpu.go b/cgroups/fs/cpu.go index c9d4ad1a1..d471538a9 100644 --- a/cgroups/fs/cpu.go +++ b/cgroups/fs/cpu.go @@ -44,6 +44,16 @@ func (s *CpuGroup) Set(path string, cgroup *configs.Cgroup) error { return err } } + if cgroup.CpuRtPeriod != 0 { + if err := writeFile(path, "cpu.rt_period_us", strconv.FormatInt(cgroup.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 { + return err + } + } return nil } diff --git a/cgroups/fs/cpu_test.go b/cgroups/fs/cpu_test.go index bcf4ac4e8..d71f62cd8 100644 --- a/cgroups/fs/cpu_test.go +++ b/cgroups/fs/cpu_test.go @@ -42,19 +42,27 @@ func TestCpuSetBandWidth(t *testing.T) { defer helper.cleanup() const ( - quotaBefore = 8000 - quotaAfter = 5000 - periodBefore = 10000 - periodAfter = 7000 + quotaBefore = 8000 + quotaAfter = 5000 + periodBefore = 10000 + periodAfter = 7000 + rtRuntimeBefore = 8000 + rtRuntimeAfter = 5000 + rtPeriodBefore = 10000 + rtPeriodAfter = 7000 ) helper.writeFileContents(map[string]string{ "cpu.cfs_quota_us": strconv.Itoa(quotaBefore), "cpu.cfs_period_us": strconv.Itoa(periodBefore), + "cpu.rt_runtime_us": strconv.Itoa(rtRuntimeBefore), + "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 cpu := &CpuGroup{} if err := cpu.Set(helper.CgroupPath, helper.CgroupData.c); err != nil { t.Fatal(err) @@ -75,6 +83,20 @@ func TestCpuSetBandWidth(t *testing.T) { if period != periodAfter { t.Fatal("Got the wrong value, set cpu.cfs_period_us failed.") } + rtRuntime, err := getCgroupParamUint(helper.CgroupPath, "cpu.rt_runtime_us") + if err != nil { + t.Fatalf("Failed to parse cpu.rt_runtime_us - %s", err) + } + if rtRuntime != rtRuntimeAfter { + t.Fatal("Got the wrong value, set cpu.rt_runtime_us failed.") + } + rtPeriod, err := getCgroupParamUint(helper.CgroupPath, "cpu.rt_period_us") + if err != nil { + t.Fatalf("Failed to parse cpu.rt_period_us - %s", err) + } + if rtPeriod != rtPeriodAfter { + t.Fatal("Got the wrong value, set cpu.rt_period_us failed.") + } } func TestCpuStats(t *testing.T) { diff --git a/cgroups/systemd/apply_systemd.go b/cgroups/systemd/apply_systemd.go index 4fb8d8d5e..8737e69f1 100644 --- a/cgroups/systemd/apply_systemd.go +++ b/cgroups/systemd/apply_systemd.go @@ -294,6 +294,17 @@ func joinCpu(c *configs.Cgroup, pid int) error { return err } } + if c.CpuRtPeriod != 0 { + if err = writeFile(path, "cpu.rt_period_us", strconv.FormatInt(c.CpuRtPeriod, 10)); err != nil { + return err + } + } + if c.CpuRtRuntime != 0 { + if err = writeFile(path, "cpu.rt_runtime_us", strconv.FormatInt(c.CpuRtRuntime, 10)); err != nil { + return err + } + } + return nil } diff --git a/configs/cgroup.go b/configs/cgroup.go index 8a161fcff..5c546a4b6 100644 --- a/configs/cgroup.go +++ b/configs/cgroup.go @@ -39,6 +39,12 @@ type Cgroup struct { // CPU period to be used for hardcapping (in usecs). 0 to use system default. CpuPeriod int64 `json:"cpu_period"` + // How many time CPU will use in realtime scheduling (in usecs). + CpuRtRuntime int64 `json:"cpu_quota"` + + // CPU period to be used for realtime scheduling (in usecs). + CpuRtPeriod int64 `json:"cpu_period"` + // CPU to use CpusetCpus string `json:"cpuset_cpus"`