2015-05-14 06:42:16 +08:00
|
|
|
// +build linux
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2015-03-21 03:29:14 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-05-15 06:21:44 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2015-02-07 01:54:52 +08:00
|
|
|
"sync"
|
2014-05-15 06:21:44 +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
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
subsystems = map[string]subsystem{
|
2014-06-20 21:13:56 +08:00
|
|
|
"devices": &DevicesGroup{},
|
|
|
|
"memory": &MemoryGroup{},
|
|
|
|
"cpu": &CpuGroup{},
|
|
|
|
"cpuset": &CpusetGroup{},
|
|
|
|
"cpuacct": &CpuacctGroup{},
|
|
|
|
"blkio": &BlkioGroup{},
|
2015-04-10 17:57:32 +08:00
|
|
|
"hugetlb": &HugetlbGroup{},
|
2015-05-14 09:09:14 +08:00
|
|
|
"net_cls": &NetClsGroup{},
|
|
|
|
"net_prio": &NetPrioGroup{},
|
2014-06-20 21:13:56 +08:00
|
|
|
"perf_event": &PerfEventGroup{},
|
|
|
|
"freezer": &FreezerGroup{},
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2015-06-16 17:26:27 +08:00
|
|
|
CgroupProcesses = "cgroup.procs"
|
2015-04-27 16:34:36 +08:00
|
|
|
HugePageSizes, _ = cgroups.GetHugePageSize()
|
2014-05-15 06:21:44 +08:00
|
|
|
)
|
|
|
|
|
2015-02-01 11:56:27 +08:00
|
|
|
type subsystem interface {
|
|
|
|
// Returns the stats, as 'stats', corresponding to the cgroup under 'path'.
|
|
|
|
GetStats(path string, stats *cgroups.Stats) error
|
|
|
|
// Removes the cgroup represented by 'data'.
|
|
|
|
Remove(*data) error
|
|
|
|
// Creates and joins the cgroup represented by data.
|
2015-02-09 13:46:30 +08:00
|
|
|
Apply(*data) error
|
2015-02-25 17:20:01 +08:00
|
|
|
// Set the cgroup represented by cgroup.
|
|
|
|
Set(path string, cgroup *configs.Cgroup) error
|
2015-02-01 11:56:27 +08:00
|
|
|
}
|
|
|
|
|
2015-01-13 05:54:00 +08:00
|
|
|
type Manager struct {
|
2015-05-26 02:29:09 +08:00
|
|
|
mu sync.Mutex
|
2015-02-01 11:56:27 +08:00
|
|
|
Cgroups *configs.Cgroup
|
2015-01-14 23:47:26 +08:00
|
|
|
Paths map[string]string
|
2015-01-13 05:54:00 +08:00
|
|
|
}
|
|
|
|
|
2014-09-11 08:44:13 +08:00
|
|
|
// The absolute path to the root of the cgroup hierarchies.
|
2015-02-07 01:54:52 +08:00
|
|
|
var cgroupRootLock sync.Mutex
|
2014-09-11 08:44:13 +08:00
|
|
|
var cgroupRoot string
|
|
|
|
|
2015-02-07 01:54:52 +08:00
|
|
|
// Gets the cgroupRoot.
|
|
|
|
func getCgroupRoot() (string, error) {
|
|
|
|
cgroupRootLock.Lock()
|
|
|
|
defer cgroupRootLock.Unlock()
|
|
|
|
|
|
|
|
if cgroupRoot != "" {
|
|
|
|
return cgroupRoot, nil
|
|
|
|
}
|
|
|
|
|
2015-03-05 10:21:52 +08:00
|
|
|
root, err := cgroups.FindCgroupMountpointDir()
|
2014-09-11 08:44:13 +08:00
|
|
|
if err != nil {
|
2015-02-07 01:54:52 +08:00
|
|
|
return "", err
|
2014-09-11 08:44:13 +08:00
|
|
|
}
|
|
|
|
|
2015-02-07 01:54:52 +08:00
|
|
|
if _, err := os.Stat(root); err != nil {
|
|
|
|
return "", err
|
2014-09-11 08:44:13 +08:00
|
|
|
}
|
2015-02-07 01:54:52 +08:00
|
|
|
|
|
|
|
cgroupRoot = root
|
|
|
|
return cgroupRoot, nil
|
2014-09-11 08:44:13 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
type data struct {
|
|
|
|
root string
|
|
|
|
cgroup string
|
2015-02-01 11:56:27 +08:00
|
|
|
c *configs.Cgroup
|
2014-05-15 06:21:44 +08:00
|
|
|
pid int
|
|
|
|
}
|
|
|
|
|
2015-01-13 05:54:00 +08:00
|
|
|
func (m *Manager) Apply(pid int) error {
|
|
|
|
if m.Cgroups == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-21 03:29:14 +08:00
|
|
|
var c = m.Cgroups
|
|
|
|
|
2015-01-13 05:54:00 +08:00
|
|
|
d, err := getCgroupData(m.Cgroups, pid)
|
2014-05-15 06:21:44 +08:00
|
|
|
if err != nil {
|
2015-01-13 05:54:00 +08:00
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2014-11-15 09:22:10 +08:00
|
|
|
paths := make(map[string]string)
|
2014-11-18 03:55:40 +08:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
cgroups.RemovePaths(paths)
|
|
|
|
}
|
|
|
|
}()
|
2014-11-15 09:22:10 +08:00
|
|
|
for name, sys := range subsystems {
|
2015-02-09 13:46:30 +08:00
|
|
|
if err := sys.Apply(d); err != nil {
|
2015-01-13 05:54:00 +08:00
|
|
|
return err
|
2014-11-15 09:22:10 +08:00
|
|
|
}
|
2015-02-04 09:44:58 +08:00
|
|
|
// TODO: Apply should, ideally, be reentrant or be broken up into a separate
|
2014-11-18 03:55:40 +08:00
|
|
|
// create and join phase so that the cgroup hierarchy for a container can be
|
|
|
|
// created then join consists of writing the process pids to cgroup.procs
|
2014-11-15 09:22:10 +08:00
|
|
|
p, err := d.path(name)
|
|
|
|
if err != nil {
|
2015-03-24 02:32:09 +08:00
|
|
|
if cgroups.IsNotFound(err) {
|
|
|
|
continue
|
|
|
|
}
|
2015-01-13 05:54:00 +08:00
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2014-11-15 09:22:10 +08:00
|
|
|
paths[name] = p
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2015-01-14 23:47:26 +08:00
|
|
|
m.Paths = paths
|
2015-01-13 05:54:00 +08:00
|
|
|
|
2015-04-17 13:18:44 +08:00
|
|
|
if paths["cpu"] != "" {
|
|
|
|
if err := CheckCpushares(paths["cpu"], c.CpuShares); err != nil {
|
|
|
|
return err
|
2015-03-21 03:29:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-13 05:54:00 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-14 23:23:42 +08:00
|
|
|
func (m *Manager) Destroy() error {
|
2015-05-26 02:29:09 +08:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
if err := cgroups.RemovePaths(m.Paths); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.Paths = make(map[string]string)
|
|
|
|
return nil
|
2015-01-13 05:54:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) GetPaths() map[string]string {
|
2015-05-26 02:29:09 +08:00
|
|
|
m.mu.Lock()
|
|
|
|
paths := m.Paths
|
|
|
|
m.mu.Unlock()
|
|
|
|
return paths
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2015-01-13 05:54:00 +08:00
|
|
|
func (m *Manager) GetStats() (*cgroups.Stats, error) {
|
2015-05-26 02:29:09 +08:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2014-05-28 08:01:08 +08:00
|
|
|
stats := cgroups.NewStats()
|
2015-01-14 23:47:26 +08:00
|
|
|
for name, path := range m.Paths {
|
2014-11-15 07:51:29 +08:00
|
|
|
sys, ok := subsystems[name]
|
2015-01-27 20:54:19 +08:00
|
|
|
if !ok || !cgroups.PathExists(path) {
|
2014-11-15 07:51:29 +08:00
|
|
|
continue
|
2014-06-20 21:13:56 +08:00
|
|
|
}
|
|
|
|
if err := sys.GetStats(path, stats); err != nil {
|
|
|
|
return nil, err
|
2014-05-28 08:01:08 +08:00
|
|
|
}
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2014-05-28 08:01:08 +08:00
|
|
|
|
|
|
|
return stats, nil
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-25 17:20:01 +08:00
|
|
|
func (m *Manager) Set(container *configs.Config) error {
|
|
|
|
for name, path := range m.Paths {
|
|
|
|
sys, ok := subsystems[name]
|
|
|
|
if !ok || !cgroups.PathExists(path) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := sys.Set(path, container.Cgroups); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-31 06:09:07 +08:00
|
|
|
// Freeze toggles the container's freezer cgroup depending on the state
|
|
|
|
// provided
|
2015-02-01 11:56:27 +08:00
|
|
|
func (m *Manager) Freeze(state configs.FreezerState) error {
|
2015-01-13 19:52:14 +08:00
|
|
|
d, err := getCgroupData(m.Cgroups, 0)
|
2014-05-31 06:09:07 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-04 13:45:44 +08:00
|
|
|
dir, err := d.path("freezer")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-19 22:04:14 +08:00
|
|
|
prevState := m.Cgroups.Freezer
|
|
|
|
m.Cgroups.Freezer = state
|
|
|
|
|
2014-05-31 06:09:07 +08:00
|
|
|
freezer := subsystems["freezer"]
|
2015-03-04 13:45:44 +08:00
|
|
|
err = freezer.Set(dir, m.Cgroups)
|
2015-01-15 00:39:29 +08:00
|
|
|
if err != nil {
|
2015-01-19 22:04:14 +08:00
|
|
|
m.Cgroups.Freezer = prevState
|
2015-01-15 00:39:29 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-05-31 06:09:07 +08:00
|
|
|
}
|
|
|
|
|
2015-01-13 05:54:00 +08:00
|
|
|
func (m *Manager) GetPids() ([]int, error) {
|
|
|
|
d, err := getCgroupData(m.Cgroups, 0)
|
2014-05-31 06:09:07 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := d.path("devices")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cgroups.ReadProcsFile(dir)
|
|
|
|
}
|
|
|
|
|
2015-02-01 11:56:27 +08:00
|
|
|
func getCgroupData(c *configs.Cgroup, pid int) (*data, error) {
|
2015-02-07 01:54:52 +08:00
|
|
|
root, err := getCgroupRoot()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-22 04:48:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cgroup := c.Name
|
|
|
|
if c.Parent != "" {
|
|
|
|
cgroup = filepath.Join(c.Parent, cgroup)
|
|
|
|
}
|
|
|
|
|
2014-05-31 06:09:07 +08:00
|
|
|
return &data{
|
2015-02-07 01:54:52 +08:00
|
|
|
root: root,
|
2014-05-22 04:48:06 +08:00
|
|
|
cgroup: cgroup,
|
|
|
|
c: c,
|
2014-05-31 06:09:07 +08:00
|
|
|
pid: pid,
|
|
|
|
}, nil
|
2014-05-22 04:48:06 +08:00
|
|
|
}
|
|
|
|
|
2015-07-18 02:41:54 +08:00
|
|
|
func (raw *data) parent(subsystem, mountpoint, src string) (string, error) {
|
2014-05-15 06:21:44 +08:00
|
|
|
initPath, err := cgroups.GetInitCgroupDir(subsystem)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-08-11 03:19:20 +08:00
|
|
|
relDir, err := filepath.Rel(initPath, src)
|
2015-07-18 02:41:54 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Join(mountpoint, relDir), nil
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (raw *data) path(subsystem string) (string, error) {
|
2015-07-18 02:41:54 +08:00
|
|
|
mnt, src, err := cgroups.FindCgroupMountpointAndSource(subsystem)
|
2015-03-09 09:16:56 +08:00
|
|
|
// If we didn't mount the subsystem, there is no point we make the path.
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-07-29 09:41:52 +08:00
|
|
|
// If the cgroup name/path is absolute do not look relative to the cgroup of the init process.
|
|
|
|
if filepath.IsAbs(raw.cgroup) {
|
2015-07-21 02:48:53 +08:00
|
|
|
return filepath.Join(raw.root, filepath.Base(mnt), raw.cgroup), nil
|
2014-07-29 09:41:52 +08:00
|
|
|
}
|
2014-08-21 01:14:56 +08:00
|
|
|
|
2015-07-18 02:41:54 +08:00
|
|
|
parent, err := raw.parent(subsystem, mnt, src)
|
2014-05-15 06:21:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-08-21 01:14:56 +08:00
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
return filepath.Join(parent, raw.cgroup), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (raw *data) join(subsystem string) (string, error) {
|
|
|
|
path, err := raw.path(subsystem)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
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(path, 0755); err != nil {
|
2014-05-15 06:21:44 +08:00
|
|
|
return "", err
|
|
|
|
}
|
2014-08-06 06:34:59 +08:00
|
|
|
if err := writeFile(path, CgroupProcesses, strconv.Itoa(raw.pid)); err != nil {
|
2014-05-15 06:21:44 +08:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeFile(dir, file, data string) error {
|
2015-04-22 10:18:22 +08:00
|
|
|
// Normally dir should not be empty, one case is that cgroup subsystem
|
|
|
|
// is not mounted, we will get empty dir, and we want it fail here.
|
|
|
|
if dir == "" {
|
|
|
|
return fmt.Errorf("no such directory for %s.", file)
|
|
|
|
}
|
2014-05-15 06:21:44 +08:00
|
|
|
return ioutil.WriteFile(filepath.Join(dir, file), []byte(data), 0700)
|
|
|
|
}
|
|
|
|
|
2014-06-04 08:25:07 +08:00
|
|
|
func readFile(dir, file string) (string, error) {
|
|
|
|
data, err := ioutil.ReadFile(filepath.Join(dir, file))
|
|
|
|
return string(data), err
|
|
|
|
}
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
func removePath(p string, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if p != "" {
|
|
|
|
return os.RemoveAll(p)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-04-17 13:18:44 +08:00
|
|
|
|
|
|
|
func CheckCpushares(path string, c int64) error {
|
|
|
|
var cpuShares int64
|
|
|
|
|
2015-06-18 21:15:52 +08:00
|
|
|
if c == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-17 13:18:44 +08:00
|
|
|
fd, err := os.Open(filepath.Join(path, "cpu.shares"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
_, err = fmt.Fscanf(fd, "%d", &cpuShares)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-18 21:15:52 +08:00
|
|
|
|
|
|
|
if c > cpuShares {
|
|
|
|
return fmt.Errorf("The maximum allowed cpu-shares is %d", cpuShares)
|
|
|
|
} else if c < cpuShares {
|
|
|
|
return fmt.Errorf("The minimum allowed cpu-shares is %d", cpuShares)
|
2015-04-17 13:18:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|