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
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
}

View File

@ -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
}