2019-01-12 04:58:27 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
"github.com/moby/sys/mountinfo"
|
2019-01-12 04:58:27 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
2020-05-04 20:46:48 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
2019-01-12 04:58:27 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
|
|
|
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
|
2020-08-10 17:43:49 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-01-12 04:58:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CpusetGroup struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CpusetGroup) Name() string {
|
|
|
|
return "cpuset"
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
func (s *CpusetGroup) Apply(path string, d *cgroupData) error {
|
|
|
|
return s.ApplyDir(path, d.config, d.pid)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CpusetGroup) Set(path string, cgroup *configs.Cgroup) error {
|
|
|
|
if cgroup.Resources.CpusetCpus != "" {
|
2020-05-04 20:46:48 +00:00
|
|
|
if err := fscommon.WriteFile(path, "cpuset.cpus", cgroup.Resources.CpusetCpus); err != nil {
|
2019-01-12 04:58:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cgroup.Resources.CpusetMems != "" {
|
2020-05-04 20:46:48 +00:00
|
|
|
if err := fscommon.WriteFile(path, "cpuset.mems", cgroup.Resources.CpusetMems); err != nil {
|
2019-01-12 04:58:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CpusetGroup) GetStats(path string, stats *cgroups.Stats) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
// Get the source mount point of directory passed in as argument.
|
|
|
|
func getMount(dir string) (string, error) {
|
|
|
|
mi, err := mountinfo.GetMounts(mountinfo.ParentsFilter(dir))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(mi) < 1 {
|
|
|
|
return "", errors.Errorf("Can't find mount point of %s", dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the longest mount point
|
|
|
|
var idx, maxlen int
|
|
|
|
for i := range mi {
|
|
|
|
if len(mi[i].Mountpoint) > maxlen {
|
|
|
|
maxlen = len(mi[i].Mountpoint)
|
|
|
|
idx = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mi[idx].Mountpoint, nil
|
|
|
|
}
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
func (s *CpusetGroup) ApplyDir(dir string, cgroup *configs.Cgroup, pid int) error {
|
|
|
|
// This might happen if we have no cpuset cgroup mounted.
|
|
|
|
// Just do nothing and don't fail.
|
|
|
|
if dir == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
root, err := getMount(dir)
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
root = filepath.Dir(root)
|
2019-01-12 04:58:27 +00:00
|
|
|
// 'ensureParent' start with parent because we don't want to
|
|
|
|
// explicitly inherit from parent, it could conflict with
|
|
|
|
// 'cpuset.cpu_exclusive'.
|
|
|
|
if err := s.ensureParent(filepath.Dir(dir), root); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// We didn't inherit cpuset configs from parent, but we have
|
|
|
|
// to ensure cpuset configs are set before moving task into the
|
|
|
|
// cgroup.
|
|
|
|
// The logic is, if user specified cpuset configs, use these
|
|
|
|
// specified configs, otherwise, inherit from parent. This makes
|
|
|
|
// cpuset configs work correctly with 'cpuset.cpu_exclusive', and
|
|
|
|
// keep backward compatibility.
|
|
|
|
if err := s.ensureCpusAndMems(dir, 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
|
|
|
|
return cgroups.WriteCgroupProc(dir, pid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CpusetGroup) getSubsystemSettings(parent string) (cpus []byte, mems []byte, err error) {
|
|
|
|
if cpus, err = ioutil.ReadFile(filepath.Join(parent, "cpuset.cpus")); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if mems, err = ioutil.ReadFile(filepath.Join(parent, "cpuset.mems")); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return cpus, mems, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensureParent makes sure that the parent directory of current is created
|
|
|
|
// and populated with the proper cpus and mems files copied from
|
|
|
|
// it's parent.
|
|
|
|
func (s *CpusetGroup) ensureParent(current, root string) error {
|
|
|
|
parent := filepath.Dir(current)
|
|
|
|
if libcontainerUtils.CleanPath(parent) == root {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Avoid infinite recursion.
|
|
|
|
if parent == current {
|
2020-08-10 17:43:49 +00:00
|
|
|
return errors.New("cpuset: cgroup parent path outside cgroup root")
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
if err := s.ensureParent(parent, root); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(current, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.copyIfNeeded(current, parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// copyIfNeeded copies the cpuset.cpus and cpuset.mems from the parent
|
|
|
|
// directory to the current directory if the file's contents are 0
|
|
|
|
func (s *CpusetGroup) copyIfNeeded(current, parent string) error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
currentCpus, currentMems []byte
|
|
|
|
parentCpus, parentMems []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
if currentCpus, currentMems, err = s.getSubsystemSettings(current); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if parentCpus, parentMems, err = s.getSubsystemSettings(parent); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.isEmpty(currentCpus) {
|
2020-05-04 20:46:48 +00:00
|
|
|
if err := fscommon.WriteFile(current, "cpuset.cpus", string(parentCpus)); err != nil {
|
2019-01-12 04:58:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if s.isEmpty(currentMems) {
|
2020-05-04 20:46:48 +00:00
|
|
|
if err := fscommon.WriteFile(current, "cpuset.mems", string(parentMems)); err != nil {
|
2019-01-12 04:58:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CpusetGroup) isEmpty(b []byte) bool {
|
|
|
|
return len(bytes.Trim(b, "\n")) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CpusetGroup) ensureCpusAndMems(path string, cgroup *configs.Cgroup) error {
|
|
|
|
if err := s.Set(path, cgroup); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.copyIfNeeded(path, filepath.Dir(path))
|
|
|
|
}
|