2015-05-14 06:42:16 +08:00
|
|
|
// +build linux
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2014-05-28 08:01:08 +08:00
|
|
|
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
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
|
|
|
}
|
|
|
|
|
2015-10-16 06:19:23 +08:00
|
|
|
func (s *CpuGroup) Name() string {
|
|
|
|
return "cpu"
|
|
|
|
}
|
|
|
|
|
2015-11-05 18:41:08 +08:00
|
|
|
func (s *CpuGroup) Apply(d *cgroupData) 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
|
2016-05-31 16:01:04 +08:00
|
|
|
path, err := d.path("cpu")
|
2015-04-22 10:18:22 +08:00
|
|
|
if err != nil && !cgroups.IsNotFound(err) {
|
2015-03-24 03:14:03 +08:00
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2016-05-31 16:01:04 +08:00
|
|
|
return s.ApplyDir(path, d.config, d.pid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CpuGroup) ApplyDir(path string, cgroup *configs.Cgroup, pid int) error {
|
|
|
|
// This might happen if we have no cpu cgroup mounted.
|
|
|
|
// Just do nothing and don't fail.
|
|
|
|
if path == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(path, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// We should set the real-Time group scheduling settings before moving
|
|
|
|
// in the process because if the process is already in SCHED_RR mode
|
|
|
|
// and no RT bandwidth is set, adding it will fail.
|
|
|
|
if err := s.SetRtSched(path, cgroup); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// because we are not using d.join we need to place the pid into the procs file
|
|
|
|
// unlike the other subsystems
|
2016-09-21 18:53:52 +08:00
|
|
|
if err := cgroups.WriteCgroupProc(path, pid); err != nil {
|
2016-05-31 16:01:04 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CpuGroup) SetRtSched(path string, cgroup *configs.Cgroup) error {
|
|
|
|
if cgroup.Resources.CpuRtPeriod != 0 {
|
2017-03-20 18:51:39 +08:00
|
|
|
if err := writeFile(path, "cpu.rt_period_us", strconv.FormatUint(cgroup.Resources.CpuRtPeriod, 10)); err != nil {
|
2016-05-31 16:01:04 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cgroup.Resources.CpuRtRuntime != 0 {
|
|
|
|
if err := writeFile(path, "cpu.rt_runtime_us", strconv.FormatInt(cgroup.Resources.CpuRtRuntime, 10)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-05-15 06:21:44 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-25 17:20:01 +08:00
|
|
|
func (s *CpuGroup) Set(path string, cgroup *configs.Cgroup) error {
|
2015-12-15 08:26:29 +08:00
|
|
|
if cgroup.Resources.CpuShares != 0 {
|
2017-03-20 18:51:39 +08:00
|
|
|
if err := writeFile(path, "cpu.shares", strconv.FormatUint(cgroup.Resources.CpuShares, 10)); err != nil {
|
2015-02-25 17:20:01 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-12-15 08:26:29 +08:00
|
|
|
if cgroup.Resources.CpuPeriod != 0 {
|
2017-03-20 18:51:39 +08:00
|
|
|
if err := writeFile(path, "cpu.cfs_period_us", strconv.FormatUint(cgroup.Resources.CpuPeriod, 10)); err != nil {
|
2015-02-25 17:20:01 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-12-15 08:26:29 +08:00
|
|
|
if cgroup.Resources.CpuQuota != 0 {
|
|
|
|
if err := writeFile(path, "cpu.cfs_quota_us", strconv.FormatInt(cgroup.Resources.CpuQuota, 10)); err != nil {
|
2015-02-25 17:20:01 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-05-31 16:01:04 +08:00
|
|
|
if err := s.SetRtSched(path, cgroup); err != nil {
|
|
|
|
return err
|
2015-05-14 20:42:10 +08:00
|
|
|
}
|
2015-02-25 17:20:01 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-05 18:41:08 +08:00
|
|
|
func (s *CpuGroup) Remove(d *cgroupData) 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
|
|
|
}
|