2014-05-15 06:21:44 +08:00
|
|
|
package cgroups
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2014-05-07 22:48:54 +08:00
|
|
|
"fmt"
|
2014-05-15 06:21:44 +08:00
|
|
|
"io"
|
2014-08-13 14:18:55 +08:00
|
|
|
"io/ioutil"
|
2014-05-15 06:21:44 +08:00
|
|
|
"os"
|
2014-05-22 04:48:06 +08:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2014-05-15 06:21:44 +08:00
|
|
|
"strings"
|
2015-01-27 20:54:19 +08:00
|
|
|
"time"
|
2014-05-15 06:21:44 +08:00
|
|
|
|
2014-07-25 05:21:02 +08:00
|
|
|
"github.com/docker/docker/pkg/mount"
|
2014-05-15 06:21:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// https://www.kernel.org/doc/Documentation/cgroups/cgroups.txt
|
|
|
|
func FindCgroupMountpoint(subsystem string) (string, error) {
|
|
|
|
mounts, err := mount.GetMounts()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mount := range mounts {
|
|
|
|
if mount.Fstype == "cgroup" {
|
|
|
|
for _, opt := range strings.Split(mount.VfsOpts, ",") {
|
|
|
|
if opt == subsystem {
|
|
|
|
return mount.Mountpoint, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-21 01:32:01 +08:00
|
|
|
|
|
|
|
return "", NewNotFoundError(subsystem)
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2014-05-07 22:48:54 +08:00
|
|
|
type Mount struct {
|
|
|
|
Mountpoint string
|
|
|
|
Subsystems []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Mount) GetThisCgroupDir() (string, error) {
|
|
|
|
if len(m.Subsystems) == 0 {
|
|
|
|
return "", fmt.Errorf("no subsystem for mount")
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetThisCgroupDir(m.Subsystems[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetCgroupMounts() ([]Mount, error) {
|
|
|
|
mounts, err := mount.GetMounts()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
all, err := GetAllSubsystems()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
allMap := make(map[string]bool)
|
|
|
|
for _, s := range all {
|
|
|
|
allMap[s] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
res := []Mount{}
|
|
|
|
for _, mount := range mounts {
|
|
|
|
if mount.Fstype == "cgroup" {
|
|
|
|
m := Mount{Mountpoint: mount.Mountpoint}
|
|
|
|
|
|
|
|
for _, opt := range strings.Split(mount.VfsOpts, ",") {
|
|
|
|
if strings.HasPrefix(opt, "name=") {
|
|
|
|
m.Subsystems = append(m.Subsystems, opt)
|
|
|
|
}
|
|
|
|
if allMap[opt] {
|
|
|
|
m.Subsystems = append(m.Subsystems, opt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res = append(res, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns all the cgroup subsystems supported by the kernel
|
|
|
|
func GetAllSubsystems() ([]string, error) {
|
|
|
|
f, err := os.Open("/proc/cgroups")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
subsystems := []string{}
|
|
|
|
|
|
|
|
s := bufio.NewScanner(f)
|
|
|
|
for s.Scan() {
|
|
|
|
if err := s.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
text := s.Text()
|
|
|
|
if text[0] != '#' {
|
|
|
|
parts := strings.Fields(text)
|
2014-07-22 09:55:06 +08:00
|
|
|
if len(parts) >= 4 && parts[3] != "0" {
|
2014-05-07 22:48:54 +08:00
|
|
|
subsystems = append(subsystems, parts[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return subsystems, nil
|
|
|
|
}
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
// Returns the relative path to the cgroup docker is running in.
|
|
|
|
func GetThisCgroupDir(subsystem string) (string, error) {
|
|
|
|
f, err := os.Open("/proc/self/cgroup")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2014-09-23 00:01:58 +08:00
|
|
|
return ParseCgroupFile(subsystem, f)
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetInitCgroupDir(subsystem string) (string, error) {
|
|
|
|
f, err := os.Open("/proc/1/cgroup")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2014-09-23 00:01:58 +08:00
|
|
|
return ParseCgroupFile(subsystem, f)
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2014-05-22 04:48:06 +08:00
|
|
|
func ReadProcsFile(dir string) ([]int, error) {
|
|
|
|
f, err := os.Open(filepath.Join(dir, "cgroup.procs"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
var (
|
|
|
|
s = bufio.NewScanner(f)
|
|
|
|
out = []int{}
|
|
|
|
)
|
|
|
|
|
|
|
|
for s.Scan() {
|
|
|
|
if t := s.Text(); t != "" {
|
|
|
|
pid, err := strconv.Atoi(t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out = append(out, pid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2014-09-23 00:01:58 +08:00
|
|
|
func ParseCgroupFile(subsystem string, r io.Reader) (string, error) {
|
2014-05-15 06:21:44 +08:00
|
|
|
s := bufio.NewScanner(r)
|
2014-08-21 01:32:01 +08:00
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
for s.Scan() {
|
|
|
|
if err := s.Err(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-08-21 01:32:01 +08:00
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
text := s.Text()
|
|
|
|
parts := strings.Split(text, ":")
|
2014-08-21 01:32:01 +08:00
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
for _, subs := range strings.Split(parts[1], ",") {
|
|
|
|
if subs == subsystem {
|
|
|
|
return parts[2], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-21 01:32:01 +08:00
|
|
|
|
|
|
|
return "", NewNotFoundError(subsystem)
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2014-08-13 14:18:55 +08:00
|
|
|
|
2015-01-27 20:54:19 +08:00
|
|
|
func PathExists(path string) bool {
|
2014-08-13 14:18:55 +08:00
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-08-14 07:25:18 +08:00
|
|
|
func EnterPid(cgroupPaths map[string]string, pid int) error {
|
2014-08-13 14:18:55 +08:00
|
|
|
for _, path := range cgroupPaths {
|
2015-01-27 20:54:19 +08:00
|
|
|
if PathExists(path) {
|
2014-08-13 14:18:55 +08:00
|
|
|
if err := ioutil.WriteFile(filepath.Join(path, "cgroup.procs"),
|
|
|
|
[]byte(strconv.Itoa(pid)), 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-11-18 03:55:40 +08:00
|
|
|
|
|
|
|
// RemovePaths iterates over the provided paths removing them.
|
2015-01-27 20:54:19 +08:00
|
|
|
// We trying to remove all paths five times with increasing delay between tries.
|
|
|
|
// If after all there are not removed cgroups - appropriate error will be
|
|
|
|
// returned.
|
2014-11-18 03:55:40 +08:00
|
|
|
func RemovePaths(paths map[string]string) (err error) {
|
2015-01-27 20:54:19 +08:00
|
|
|
delay := 10 * time.Millisecond
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
if i != 0 {
|
|
|
|
time.Sleep(delay)
|
|
|
|
delay *= 2
|
|
|
|
}
|
|
|
|
for s, p := range paths {
|
|
|
|
os.RemoveAll(p)
|
|
|
|
// TODO: here probably should be logging
|
|
|
|
_, err := os.Stat(p)
|
|
|
|
// We need this strange way of checking cgroups existence because
|
|
|
|
// RemoveAll almost always returns error, even on already removed
|
|
|
|
// cgroups
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
delete(paths, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(paths) == 0 {
|
|
|
|
return nil
|
2014-11-18 03:55:40 +08:00
|
|
|
}
|
|
|
|
}
|
2015-01-27 20:54:19 +08:00
|
|
|
return fmt.Errorf("Failed to remove paths: %s", paths)
|
2014-11-18 03:55:40 +08:00
|
|
|
}
|