diff --git a/cgroups/fs/apply_raw.go b/cgroups/fs/apply_raw.go index fee62d78..358e2eab 100644 --- a/cgroups/fs/apply_raw.go +++ b/cgroups/fs/apply_raw.go @@ -1,8 +1,11 @@ package fs import ( + "fmt" + "io" "io/ioutil" "os" + "path" "path/filepath" "strconv" "sync" @@ -76,10 +79,13 @@ type data struct { } func (m *Manager) Apply(pid int) error { + if m.Cgroups == nil { return nil } + var c = m.Cgroups + d, err := getCgroupData(m.Cgroups, pid) if err != nil { return err @@ -109,6 +115,28 @@ 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) + } + } + return nil } diff --git a/cgroups/systemd/apply_systemd.go b/cgroups/systemd/apply_systemd.go index c8ab23ad..bda81998 100644 --- a/cgroups/systemd/apply_systemd.go +++ b/cgroups/systemd/apply_systemd.go @@ -4,8 +4,10 @@ package systemd import ( "fmt" + "io" "io/ioutil" "os" + "path" "path/filepath" "strconv" "strings" @@ -239,6 +241,28 @@ 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) + } + } + return nil }