2014-04-19 04:55:06 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2014-05-13 20:34:31 +00:00
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
2014-04-19 04:55:06 +00:00
|
|
|
"os"
|
2014-05-13 20:34:31 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2014-04-19 04:55:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type cpusetGroup struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *cpusetGroup) Set(d *data) error {
|
|
|
|
// we don't want to join this cgroup unless it is specified
|
|
|
|
if d.c.CpusetCpus != "" {
|
2014-05-13 20:34:31 +00:00
|
|
|
dir, err := d.path("cpuset")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.ensureParent(dir); err != nil {
|
2014-04-19 04:55:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-05-13 20:34:31 +00: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(d.pid)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-19 04:55:06 +00:00
|
|
|
if err := writeFile(dir, "cpuset.cpus", d.c.CpusetCpus); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-04-19 05:17:31 +00:00
|
|
|
|
|
|
|
func (s *cpusetGroup) Remove(d *data) error {
|
|
|
|
return removePath(d.path("cpuset"))
|
|
|
|
}
|
2014-04-20 19:02:39 +00:00
|
|
|
|
2014-05-22 20:50:51 +00:00
|
|
|
func (s *cpusetGroup) Stats(d *data) (map[string]int64, error) {
|
2014-04-20 19:02:39 +00:00
|
|
|
return nil, ErrNotSupportStat
|
|
|
|
}
|
2014-05-13 20:34:31 +00:00
|
|
|
|
|
|
|
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 ensures that the parent directory of current is created
|
|
|
|
// with the proper cpus and mems files copied from it's parent if the values
|
|
|
|
// are a file with a new line char
|
|
|
|
func (s *cpusetGroup) ensureParent(current string) error {
|
|
|
|
parent := filepath.Dir(current)
|
|
|
|
|
|
|
|
if _, err := os.Stat(parent); err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.ensureParent(parent); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(current, 0755); err != nil && !os.IsExist(err) {
|
|
|
|
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) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *cpusetGroup) isEmpty(b []byte) bool {
|
|
|
|
return len(bytes.Trim(b, "\n")) == 0
|
|
|
|
}
|