From e161ceccbe02bd12da35a3864dd70c73d2f2d607 Mon Sep 17 00:00:00 2001 From: Qiang Huang Date: Fri, 17 Apr 2015 13:18:44 +0800 Subject: [PATCH 1/2] cleanup duplicate code for cpuShares check Signed-off-by: Qiang Huang --- cgroups/fs/apply_raw.go | 47 ++++++++++++++++++-------------- cgroups/systemd/apply_systemd.go | 25 ++--------------- 2 files changed, 30 insertions(+), 42 deletions(-) diff --git a/cgroups/fs/apply_raw.go b/cgroups/fs/apply_raw.go index 358e2eabc..d31f894b6 100644 --- a/cgroups/fs/apply_raw.go +++ b/cgroups/fs/apply_raw.go @@ -5,7 +5,6 @@ import ( "io" "io/ioutil" "os" - "path" "path/filepath" "strconv" "sync" @@ -115,25 +114,9 @@ func (m *Manager) Apply(pid int) error { } m.Paths = paths - var cpuShares int64 - - fd, err := os.Open(path.Join(m.Paths["cpu"], "cpu.shares")) - if err != nil { - return err - } - - _, err = fmt.Fscanf(fd, "%d", &cpuShares) - if err != nil && err != io.EOF { - return err - } - - fd.Close() - - if c.CpuShares != 0 { - if c.CpuShares > cpuShares { - return fmt.Errorf("The maximum allowed cpu-shares is %d", cpuShares) - } else if c.CpuShares < cpuShares { - return fmt.Errorf("The minimum allowed cpu-shares is %d", cpuShares) + if paths["cpu"] != "" { + if err := CheckCpushares(paths["cpu"], c.CpuShares); err != nil { + return err } } @@ -309,3 +292,27 @@ func removePath(p string, err error) error { } return nil } + +func CheckCpushares(path string, c int64) error { + var cpuShares int64 + + fd, err := os.Open(filepath.Join(path, "cpu.shares")) + if err != nil { + return err + } + defer fd.Close() + + _, err = fmt.Fscanf(fd, "%d", &cpuShares) + if err != nil && err != io.EOF { + return err + } + if c != 0 { + if c > cpuShares { + return fmt.Errorf("The maximum allowed cpu-shares is %d", cpuShares) + } else if c < cpuShares { + return fmt.Errorf("The minimum allowed cpu-shares is %d", cpuShares) + } + } + + return nil +} diff --git a/cgroups/systemd/apply_systemd.go b/cgroups/systemd/apply_systemd.go index 9d457cdf0..f770e6bf2 100644 --- a/cgroups/systemd/apply_systemd.go +++ b/cgroups/systemd/apply_systemd.go @@ -4,10 +4,8 @@ package systemd import ( "fmt" - "io" "io/ioutil" "os" - "path" "path/filepath" "strconv" "strings" @@ -238,28 +236,11 @@ func (m *Manager) Apply(pid int) error { } paths[sysname] = subsystemPath } - m.Paths = paths - var cpuShares int64 - - fd, err := os.Open(path.Join(m.Paths["cpu"], "cpu.shares")) - if err != nil { - return err - } - - _, err = fmt.Fscanf(fd, "%d", &cpuShares) - if err != nil && err != io.EOF { - return err - } - - fd.Close() - - if c.CpuShares != 0 { - if c.CpuShares > cpuShares { - return fmt.Errorf("The maximum allowed cpu-shares is %d", cpuShares) - } else if c.CpuShares < cpuShares { - return fmt.Errorf("The minimum allowed cpu-shares is %d", cpuShares) + if paths["cpu"] != "" { + if err := fs.CheckCpushares(paths["cpu"], c.CpuShares); err != nil { + return err } } From 62fccb3e1edebff1b1dd0db1e0d39dd63c366006 Mon Sep 17 00:00:00 2001 From: Qiang Huang Date: Fri, 17 Apr 2015 13:51:37 +0800 Subject: [PATCH 2/2] add test case for cpuShares check Signed-off-by: Qiang Huang --- integration/exec_test.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/integration/exec_test.go b/integration/exec_test.go index 8f6719d0a..fbfcaa18e 100644 --- a/integration/exec_test.go +++ b/integration/exec_test.go @@ -474,6 +474,37 @@ func testFreeze(t *testing.T, systemd bool) { } } +func TestCpuShares(t *testing.T) { + testCpuShares(t, false) +} + +func TestSystemdCpuShares(t *testing.T) { + if !systemd.UseSystemd() { + t.Skip("Systemd is unsupported") + } + testCpuShares(t, true) +} + +func testCpuShares(t *testing.T, systemd bool) { + if testing.Short() { + return + } + rootfs, err := newRootfs() + ok(t, err) + defer remove(rootfs) + + config := newTemplateConfig(rootfs) + if systemd { + config.Cgroups.Slice = "system.slice" + } + config.Cgroups.CpuShares = 1 + + _, _, err = runContainer(config, "", "ps") + if err == nil { + t.Fatalf("runContainer should failed with invalid CpuShares") + } +} + func TestContainerState(t *testing.T) { if testing.Short() { return