Merge pull request #464 from shishir-a412ed/cpu_shares_issue

Throw an error if cgroup tries to set cpu-shares more/less than the maximum/minimum permissible value.
This commit is contained in:
Alexander Morozov 2015-04-14 09:04:31 -07:00
commit fc470e199d
2 changed files with 52 additions and 0 deletions

View File

@ -1,8 +1,11 @@
package fs package fs
import ( import (
"fmt"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strconv" "strconv"
"sync" "sync"
@ -76,10 +79,13 @@ type data struct {
} }
func (m *Manager) Apply(pid int) error { func (m *Manager) Apply(pid int) error {
if m.Cgroups == nil { if m.Cgroups == nil {
return nil return nil
} }
var c = m.Cgroups
d, err := getCgroupData(m.Cgroups, pid) d, err := getCgroupData(m.Cgroups, pid)
if err != nil { if err != nil {
return err return err
@ -109,6 +115,28 @@ func (m *Manager) Apply(pid int) error {
} }
m.Paths = paths 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 return nil
} }

View File

@ -4,8 +4,10 @@ package systemd
import ( import (
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -239,6 +241,28 @@ func (m *Manager) Apply(pid int) error {
m.Paths = paths 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 return nil
} }