2015-05-14 06:42:16 +08:00
|
|
|
// +build linux
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"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 CpusetGroup struct {
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2015-10-16 06:19:23 +08:00
|
|
|
func (s *CpusetGroup) Name() string {
|
|
|
|
return "cpuset"
|
|
|
|
}
|
|
|
|
|
2015-02-09 13:46:30 +08:00
|
|
|
func (s *CpusetGroup) Apply(d *data) error {
|
2014-11-18 11:05:58 +08:00
|
|
|
dir, err := d.path("cpuset")
|
2015-04-22 10:18:22 +08:00
|
|
|
if err != nil && !cgroups.IsNotFound(err) {
|
2015-03-17 04:46:28 +08:00
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2015-02-27 12:09:42 +08:00
|
|
|
return s.ApplyDir(dir, d.c, d.pid)
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-25 17:20:01 +08:00
|
|
|
func (s *CpusetGroup) Set(path string, cgroup *configs.Cgroup) error {
|
|
|
|
if cgroup.CpusetCpus != "" {
|
|
|
|
if err := writeFile(path, "cpuset.cpus", cgroup.CpusetCpus); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cgroup.CpusetMems != "" {
|
|
|
|
if err := writeFile(path, "cpuset.mems", cgroup.CpusetMems); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
func (s *CpusetGroup) Remove(d *data) error {
|
2014-05-15 06:21:44 +08:00
|
|
|
return removePath(d.path("cpuset"))
|
|
|
|
}
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
func (s *CpusetGroup) GetStats(path string, stats *cgroups.Stats) error {
|
2014-05-28 08:01:08 +08:00
|
|
|
return nil
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-27 12:09:42 +08:00
|
|
|
func (s *CpusetGroup) ApplyDir(dir string, cgroup *configs.Cgroup, pid int) error {
|
2015-04-22 10:18:22 +08:00
|
|
|
// This might happen if we have no cpuset cgroup mounted.
|
|
|
|
// Just do nothing and don't fail.
|
|
|
|
if dir == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2015-06-20 06:43:14 +08:00
|
|
|
root, err := getCgroupRoot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.ensureParent(dir, root); err != nil {
|
2014-08-14 09:00:15 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-02-27 12:09:42 +08:00
|
|
|
// the default values inherit from parent cgroup are already set in
|
|
|
|
// s.ensureParent, cover these if we have our own
|
|
|
|
if err := s.Set(dir, cgroup); err != nil {
|
|
|
|
return err
|
2014-08-14 09:00:15 +08:00
|
|
|
}
|
2015-10-15 11:16:56 +08:00
|
|
|
// because we are not using d.join we need to place the pid into the procs file
|
|
|
|
// unlike the other subsystems
|
|
|
|
if err := writeFile(dir, "cgroup.procs", strconv.Itoa(pid)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-14 09:00:15 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
func (s *CpusetGroup) getSubsystemSettings(parent string) (cpus []byte, mems []byte, err error) {
|
2014-05-15 06:21:44 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-06-20 06:43:14 +08:00
|
|
|
// 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 {
|
2014-05-15 06:21:44 +08:00
|
|
|
parent := filepath.Dir(current)
|
2015-06-20 06:43:14 +08:00
|
|
|
if filepath.Clean(parent) == root {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := s.ensureParent(parent, root); err != nil {
|
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
Simplify and fix os.MkdirAll() usage
TL;DR: check for IsExist(err) after a failed MkdirAll() is both
redundant and wrong -- so two reasons to remove it.
Quoting MkdirAll documentation:
> MkdirAll creates a directory named path, along with any necessary
> parents, and returns nil, or else returns an error. If path
> is already a directory, MkdirAll does nothing and returns nil.
This means two things:
1. If a directory to be created already exists, no error is
returned.
2. If the error returned is IsExist (EEXIST), it means there exists
a non-directory with the same name as MkdirAll need to use for
directory. Example: we want to MkdirAll("a/b"), but file "a"
(or "a/b") already exists, so MkdirAll fails.
The above is a theory, based on quoted documentation and my UNIX
knowledge.
3. In practice, though, current MkdirAll implementation [1] returns
ENOTDIR in most of cases described in #2, with the exception when
there is a race between MkdirAll and someone else creating the
last component of MkdirAll argument as a file. In this very case
MkdirAll() will indeed return EEXIST.
Because of #1, IsExist check after MkdirAll is not needed.
Because of #2 and #3, ignoring IsExist error is just plain wrong,
as directory we require is not created. It's cleaner to report
the error now.
Note this error is all over the tree, I guess due to copy-paste,
or trying to follow the same usage pattern as for Mkdir(),
or some not quite correct examples on the Internet.
[1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
2015-07-30 09:01:41 +08:00
|
|
|
if err := os.MkdirAll(current, 0755); err != nil {
|
2014-05-15 06:21:44 +08:00
|
|
|
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
|
2014-06-20 21:13:56 +08:00
|
|
|
func (s *CpusetGroup) copyIfNeeded(current, parent string) error {
|
2014-05-15 06:21:44 +08:00
|
|
|
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) {
|
|
|
|
if err := writeFile(current, "cpuset.cpus", string(parentCpus)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if s.isEmpty(currentMems) {
|
|
|
|
if err := writeFile(current, "cpuset.mems", string(parentMems)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
func (s *CpusetGroup) isEmpty(b []byte) bool {
|
2014-05-15 06:21:44 +08:00
|
|
|
return len(bytes.Trim(b, "\n")) == 0
|
|
|
|
}
|