2014-05-15 06:21:44 +08:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2014-05-28 08:01:08 +08:00
|
|
|
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer/cgroups"
|
2014-05-15 06:21:44 +08:00
|
|
|
)
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
type CpuGroup struct {
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
func (s *CpuGroup) Set(d *data) error {
|
2014-05-15 06:21:44 +08:00
|
|
|
// We always want to join the cpu group, to allow fair cpu scheduling
|
|
|
|
// on a container basis
|
|
|
|
dir, err := d.join("cpu")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if d.c.CpuShares != 0 {
|
|
|
|
if err := writeFile(dir, "cpu.shares", strconv.FormatInt(d.c.CpuShares, 10)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if d.c.CpuPeriod != 0 {
|
|
|
|
if err := writeFile(dir, "cpu.cfs_period_us", strconv.FormatInt(d.c.CpuPeriod, 10)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if d.c.CpuQuota != 0 {
|
|
|
|
if err := writeFile(dir, "cpu.cfs_quota_us", strconv.FormatInt(d.c.CpuQuota, 10)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
func (s *CpuGroup) Remove(d *data) error {
|
2014-05-15 06:21:44 +08:00
|
|
|
return removePath(d.path("cpu"))
|
|
|
|
}
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
func (s *CpuGroup) GetStats(path string, stats *cgroups.Stats) error {
|
2014-05-15 06:21:44 +08:00
|
|
|
f, err := os.Open(filepath.Join(path, "cpu.stat"))
|
|
|
|
if err != nil {
|
2014-07-10 00:39:38 +08:00
|
|
|
if os.IsNotExist(err) {
|
2014-05-30 08:23:18 +08:00
|
|
|
return nil
|
|
|
|
}
|
2014-05-28 08:01:08 +08:00
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
sc := bufio.NewScanner(f)
|
|
|
|
for sc.Scan() {
|
|
|
|
t, v, err := getCgroupParamKeyValue(sc.Text())
|
|
|
|
if err != nil {
|
2014-05-28 08:01:08 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch t {
|
|
|
|
case "nr_periods":
|
|
|
|
stats.CpuStats.ThrottlingData.Periods = v
|
|
|
|
|
|
|
|
case "nr_throttled":
|
|
|
|
stats.CpuStats.ThrottlingData.ThrottledPeriods = v
|
|
|
|
|
|
|
|
case "throttled_time":
|
|
|
|
stats.CpuStats.ThrottlingData.ThrottledTime = v
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
}
|
2014-05-28 08:01:08 +08:00
|
|
|
return nil
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|