2015-05-14 06:42:16 +08:00
|
|
|
// +build linux
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
package cgroups
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2014-05-07 22:48:54 +08:00
|
|
|
"fmt"
|
2016-01-22 04:56:53 +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"
|
2014-12-23 05:51:20 +08:00
|
|
|
"time"
|
2014-05-15 06:21:44 +08:00
|
|
|
|
2018-09-07 11:37:40 +08:00
|
|
|
units "github.com/docker/go-units"
|
2018-12-17 23:34:47 +08:00
|
|
|
"golang.org/x/sys/unix"
|
2014-05-15 06:21:44 +08:00
|
|
|
)
|
|
|
|
|
2016-07-20 07:33:09 +08:00
|
|
|
const (
|
2016-11-14 20:54:17 +08:00
|
|
|
CgroupNamePrefix = "name="
|
2016-07-20 07:33:09 +08:00
|
|
|
CgroupProcesses = "cgroup.procs"
|
|
|
|
)
|
2015-07-16 03:10:15 +08:00
|
|
|
|
2016-04-29 23:40:23 +08:00
|
|
|
// https://www.kernel.org/doc/Documentation/cgroup-v1/cgroups.txt
|
2018-08-17 23:18:18 +08:00
|
|
|
func FindCgroupMountpoint(cgroupPath, subsystem string) (string, error) {
|
|
|
|
mnt, _, err := FindCgroupMountpointAndRoot(cgroupPath, subsystem)
|
2017-01-03 19:46:42 +08:00
|
|
|
return mnt, err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2018-08-17 23:18:18 +08:00
|
|
|
func FindCgroupMountpointAndRoot(cgroupPath, subsystem string) (string, string, error) {
|
2017-01-03 19:46:42 +08:00
|
|
|
// We are not using mount.GetMounts() because it's super-inefficient,
|
|
|
|
// parsing it directly sped up x10 times because of not using Sscanf.
|
|
|
|
// It was one of two major performance drawbacks in container start.
|
2016-02-29 15:10:48 +08:00
|
|
|
if !isSubsystemAvailable(subsystem) {
|
|
|
|
return "", "", NewNotFoundError(subsystem)
|
|
|
|
}
|
2018-08-17 23:18:18 +08:00
|
|
|
|
2015-07-18 02:41:54 +08:00
|
|
|
f, err := os.Open("/proc/self/mountinfo")
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
2015-07-28 07:33:39 +08:00
|
|
|
defer f.Close()
|
|
|
|
|
2018-08-17 23:18:18 +08:00
|
|
|
return findCgroupMountpointAndRootFromReader(f, cgroupPath, subsystem)
|
|
|
|
}
|
|
|
|
|
|
|
|
func findCgroupMountpointAndRootFromReader(reader io.Reader, cgroupPath, subsystem string) (string, string, error) {
|
|
|
|
scanner := bufio.NewScanner(reader)
|
2015-07-18 02:41:54 +08:00
|
|
|
for scanner.Scan() {
|
|
|
|
txt := scanner.Text()
|
2018-08-17 23:18:18 +08:00
|
|
|
fields := strings.Fields(txt)
|
|
|
|
if len(fields) < 5 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(fields[4], cgroupPath) {
|
|
|
|
for _, opt := range strings.Split(fields[len(fields)-1], ",") {
|
|
|
|
if opt == subsystem {
|
|
|
|
return fields[4], fields[3], nil
|
|
|
|
}
|
2015-07-18 02:41:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", "", NewNotFoundError(subsystem)
|
|
|
|
}
|
|
|
|
|
2016-02-29 15:10:48 +08:00
|
|
|
func isSubsystemAvailable(subsystem string) bool {
|
|
|
|
cgroups, err := ParseCgroupFile("/proc/self/cgroup")
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, avail := cgroups[subsystem]
|
|
|
|
return avail
|
|
|
|
}
|
|
|
|
|
2017-03-15 18:10:30 +08:00
|
|
|
func GetClosestMountpointAncestor(dir, mountinfo string) string {
|
|
|
|
deepestMountPoint := ""
|
|
|
|
for _, mountInfoEntry := range strings.Split(mountinfo, "\n") {
|
|
|
|
mountInfoParts := strings.Fields(mountInfoEntry)
|
|
|
|
if len(mountInfoParts) < 5 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mountPoint := mountInfoParts[4]
|
|
|
|
if strings.HasPrefix(mountPoint, deepestMountPoint) && strings.HasPrefix(dir, mountPoint) {
|
|
|
|
deepestMountPoint = mountPoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return deepestMountPoint
|
|
|
|
}
|
|
|
|
|
2015-03-05 10:21:52 +08:00
|
|
|
func FindCgroupMountpointDir() (string, error) {
|
2015-09-09 09:28:50 +08:00
|
|
|
f, err := os.Open("/proc/self/mountinfo")
|
2015-03-05 10:21:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-09-09 09:28:50 +08:00
|
|
|
defer f.Close()
|
2015-03-05 10:21:52 +08:00
|
|
|
|
2015-09-09 09:28:50 +08:00
|
|
|
scanner := bufio.NewScanner(f)
|
|
|
|
for scanner.Scan() {
|
2015-09-10 08:29:12 +08:00
|
|
|
text := scanner.Text()
|
|
|
|
fields := strings.Split(text, " ")
|
|
|
|
// Safe as mountinfo encodes mountpoints with spaces as \040.
|
|
|
|
index := strings.Index(text, " - ")
|
|
|
|
postSeparatorFields := strings.Fields(text[index+3:])
|
2016-01-14 03:24:59 +08:00
|
|
|
numPostFields := len(postSeparatorFields)
|
|
|
|
|
|
|
|
// This is an error as we can't detect if the mount is for "cgroup"
|
|
|
|
if numPostFields == 0 {
|
|
|
|
return "", fmt.Errorf("Found no fields post '-' in %q", text)
|
2015-09-10 08:29:12 +08:00
|
|
|
}
|
2016-01-14 03:24:59 +08:00
|
|
|
|
2015-09-10 08:29:12 +08:00
|
|
|
if postSeparatorFields[0] == "cgroup" {
|
2018-09-07 11:37:40 +08:00
|
|
|
// Check that the mount is properly formatted.
|
2016-01-14 03:24:59 +08:00
|
|
|
if numPostFields < 3 {
|
|
|
|
return "", fmt.Errorf("Error found less than 3 fields post '-' in %q", text)
|
|
|
|
}
|
|
|
|
|
2015-09-09 09:28:50 +08:00
|
|
|
return filepath.Dir(fields[4]), nil
|
2015-03-05 10:21:52 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-09 09:28:50 +08:00
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-03-05 10:21:52 +08:00
|
|
|
|
|
|
|
return "", NewNotFoundError("cgroup")
|
|
|
|
}
|
|
|
|
|
2014-05-07 22:48:54 +08:00
|
|
|
type Mount struct {
|
|
|
|
Mountpoint string
|
2015-07-16 01:41:25 +08:00
|
|
|
Root string
|
2014-05-07 22:48:54 +08:00
|
|
|
Subsystems []string
|
|
|
|
}
|
|
|
|
|
2016-04-26 00:19:39 +08:00
|
|
|
func (m Mount) GetOwnCgroup(cgroups map[string]string) (string, error) {
|
2014-05-07 22:48:54 +08:00
|
|
|
if len(m.Subsystems) == 0 {
|
|
|
|
return "", fmt.Errorf("no subsystem for mount")
|
|
|
|
}
|
|
|
|
|
2015-09-10 16:58:08 +08:00
|
|
|
return getControllerPath(m.Subsystems[0], cgroups)
|
2014-05-07 22:48:54 +08:00
|
|
|
}
|
|
|
|
|
2016-09-16 01:24:39 +08:00
|
|
|
func getCgroupMountsHelper(ss map[string]bool, mi io.Reader, all bool) ([]Mount, error) {
|
2016-01-22 04:56:53 +08:00
|
|
|
res := make([]Mount, 0, len(ss))
|
|
|
|
scanner := bufio.NewScanner(mi)
|
2016-02-29 15:10:48 +08:00
|
|
|
numFound := 0
|
|
|
|
for scanner.Scan() && numFound < len(ss) {
|
2016-01-22 04:56:53 +08:00
|
|
|
txt := scanner.Text()
|
2016-02-19 01:58:29 +08:00
|
|
|
sepIdx := strings.Index(txt, " - ")
|
2016-01-22 04:56:53 +08:00
|
|
|
if sepIdx == -1 {
|
|
|
|
return nil, fmt.Errorf("invalid mountinfo format")
|
|
|
|
}
|
2017-01-11 07:13:28 +08:00
|
|
|
if txt[sepIdx+3:sepIdx+10] == "cgroup2" || txt[sepIdx+3:sepIdx+9] != "cgroup" {
|
2016-01-22 04:56:53 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
fields := strings.Split(txt, " ")
|
|
|
|
m := Mount{
|
|
|
|
Mountpoint: fields[4],
|
|
|
|
Root: fields[3],
|
|
|
|
}
|
|
|
|
for _, opt := range strings.Split(fields[len(fields)-1], ",") {
|
2018-06-24 06:53:42 +08:00
|
|
|
seen, known := ss[opt]
|
|
|
|
if !known || (!all && seen) {
|
2016-02-29 15:10:48 +08:00
|
|
|
continue
|
|
|
|
}
|
2018-08-01 10:42:49 +08:00
|
|
|
ss[opt] = true
|
2016-11-14 20:54:17 +08:00
|
|
|
if strings.HasPrefix(opt, CgroupNamePrefix) {
|
|
|
|
opt = opt[len(CgroupNamePrefix):]
|
2016-09-16 01:24:39 +08:00
|
|
|
}
|
2018-06-24 06:53:42 +08:00
|
|
|
m.Subsystems = append(m.Subsystems, opt)
|
|
|
|
numFound++
|
2016-01-22 04:56:53 +08:00
|
|
|
}
|
2018-08-01 10:42:49 +08:00
|
|
|
if len(m.Subsystems) > 0 || all {
|
|
|
|
res = append(res, m)
|
2016-01-22 04:56:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2016-09-16 01:24:39 +08:00
|
|
|
// GetCgroupMounts returns the mounts for the cgroup subsystems.
|
|
|
|
// all indicates whether to return just the first instance or all the mounts.
|
|
|
|
func GetCgroupMounts(all bool) ([]Mount, error) {
|
2016-01-22 04:56:53 +08:00
|
|
|
f, err := os.Open("/proc/self/mountinfo")
|
2014-05-07 22:48:54 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-22 04:56:53 +08:00
|
|
|
defer f.Close()
|
2014-05-07 22:48:54 +08:00
|
|
|
|
2016-09-16 01:24:39 +08:00
|
|
|
allSubsystems, err := ParseCgroupFile("/proc/self/cgroup")
|
2014-05-07 22:48:54 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
allMap := make(map[string]bool)
|
2016-09-16 01:24:39 +08:00
|
|
|
for s := range allSubsystems {
|
2018-06-24 06:53:42 +08:00
|
|
|
allMap[s] = false
|
2014-05-07 22:48:54 +08:00
|
|
|
}
|
2016-09-16 01:24:39 +08:00
|
|
|
return getCgroupMountsHelper(allMap, f, all)
|
2014-05-07 22:48:54 +08:00
|
|
|
}
|
|
|
|
|
2016-04-12 16:12:23 +08:00
|
|
|
// GetAllSubsystems returns all the cgroup subsystems supported by the kernel
|
2014-05-07 22:48:54 +08:00
|
|
|
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() {
|
|
|
|
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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-17 21:25:00 +08:00
|
|
|
if err := s.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-07 22:48:54 +08:00
|
|
|
return subsystems, nil
|
|
|
|
}
|
|
|
|
|
2016-04-26 00:19:39 +08:00
|
|
|
// GetOwnCgroup returns the relative path to the cgroup docker is running in.
|
|
|
|
func GetOwnCgroup(subsystem string) (string, error) {
|
2015-09-10 16:06:45 +08:00
|
|
|
cgroups, err := ParseCgroupFile("/proc/self/cgroup")
|
2014-05-15 06:21:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-09-10 16:06:45 +08:00
|
|
|
return getControllerPath(subsystem, cgroups)
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2016-04-26 00:19:39 +08:00
|
|
|
func GetOwnCgroupPath(subsystem string) (string, error) {
|
|
|
|
cgroup, err := GetOwnCgroup(subsystem)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-09-10 16:06:45 +08:00
|
|
|
|
2016-04-26 00:19:39 +08:00
|
|
|
return getCgroupPathHelper(subsystem, cgroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetInitCgroup(subsystem string) (string, error) {
|
2015-09-10 16:06:45 +08:00
|
|
|
cgroups, err := ParseCgroupFile("/proc/1/cgroup")
|
2014-05-15 06:21:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-09-10 16:06:45 +08:00
|
|
|
return getControllerPath(subsystem, cgroups)
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2016-04-26 00:19:39 +08:00
|
|
|
func GetInitCgroupPath(subsystem string) (string, error) {
|
|
|
|
cgroup, err := GetInitCgroup(subsystem)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return getCgroupPathHelper(subsystem, cgroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCgroupPathHelper(subsystem, cgroup string) (string, error) {
|
2018-08-17 23:18:18 +08:00
|
|
|
mnt, root, err := FindCgroupMountpointAndRoot("", subsystem)
|
2016-04-26 00:19:39 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is needed for nested containers, because in /proc/self/cgroup we
|
2018-09-07 11:37:40 +08:00
|
|
|
// see paths from host, which don't exist in container.
|
2016-04-26 00:19:39 +08:00
|
|
|
relCgroup, err := filepath.Rel(root, cgroup)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(mnt, relCgroup), nil
|
|
|
|
}
|
|
|
|
|
2015-10-13 05:28:31 +08:00
|
|
|
func readProcsFile(dir string) ([]int, error) {
|
2016-07-20 07:33:09 +08:00
|
|
|
f, err := os.Open(filepath.Join(dir, CgroupProcesses))
|
2014-05-22 04:48:06 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-06-11 03:08:57 +08:00
|
|
|
// ParseCgroupFile parses the given cgroup file, typically from
|
|
|
|
// /proc/<pid>/cgroup, into a map of subgroups to cgroup names.
|
2015-09-10 16:06:45 +08:00
|
|
|
func ParseCgroupFile(path string) (map[string]string, error) {
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2016-06-11 03:08:57 +08:00
|
|
|
return parseCgroupFromReader(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function for ParseCgroupFile to make testing easier
|
|
|
|
func parseCgroupFromReader(r io.Reader) (map[string]string, error) {
|
|
|
|
s := bufio.NewScanner(r)
|
2015-09-10 16:06:45 +08:00
|
|
|
cgroups := make(map[string]string)
|
2014-08-21 01:32:01 +08:00
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
for s.Scan() {
|
|
|
|
text := s.Text()
|
2016-06-11 03:08:57 +08:00
|
|
|
// from cgroups(7):
|
|
|
|
// /proc/[pid]/cgroup
|
|
|
|
// ...
|
|
|
|
// For each cgroup hierarchy ... there is one entry
|
|
|
|
// containing three colon-separated fields of the form:
|
|
|
|
// hierarchy-ID:subsystem-list:cgroup-path
|
|
|
|
parts := strings.SplitN(text, ":", 3)
|
|
|
|
if len(parts) < 3 {
|
|
|
|
return nil, fmt.Errorf("invalid cgroup entry: must contain at least two colons: %v", text)
|
|
|
|
}
|
2014-08-21 01:32:01 +08:00
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
for _, subs := range strings.Split(parts[1], ",") {
|
2015-09-10 16:06:45 +08:00
|
|
|
cgroups[subs] = parts[2]
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
}
|
2017-01-17 21:25:00 +08:00
|
|
|
if err := s.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-09-10 16:06:45 +08:00
|
|
|
return cgroups, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getControllerPath(subsystem string, cgroups map[string]string) (string, error) {
|
|
|
|
|
|
|
|
if p, ok := cgroups[subsystem]; ok {
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2016-11-14 20:54:17 +08:00
|
|
|
if p, ok := cgroups[CgroupNamePrefix+subsystem]; ok {
|
2015-09-10 16:06:45 +08:00
|
|
|
return p, 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-09 03:15:03 +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-09 03:15:03 +08:00
|
|
|
if PathExists(path) {
|
2016-07-20 07:33:09 +08:00
|
|
|
if err := WriteCgroupProc(path, pid); err != nil {
|
2014-08-13 14:18:55 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-11-18 03:55:40 +08:00
|
|
|
|
|
|
|
// RemovePaths iterates over the provided paths removing them.
|
2014-12-23 05:51:20 +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) {
|
2014-12-23 05:51:20 +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
|
|
|
}
|
|
|
|
}
|
2016-03-26 23:28:59 +08:00
|
|
|
return fmt.Errorf("Failed to remove paths: %v", paths)
|
2014-11-18 03:55:40 +08:00
|
|
|
}
|
2015-04-27 16:34:36 +08:00
|
|
|
|
|
|
|
func GetHugePageSize() ([]string, error) {
|
|
|
|
var pageSizes []string
|
|
|
|
sizeList := []string{"B", "kB", "MB", "GB", "TB", "PB"}
|
|
|
|
files, err := ioutil.ReadDir("/sys/kernel/mm/hugepages")
|
|
|
|
if err != nil {
|
|
|
|
return pageSizes, err
|
|
|
|
}
|
|
|
|
for _, st := range files {
|
|
|
|
nameArray := strings.Split(st.Name(), "-")
|
|
|
|
pageSize, err := units.RAMInBytes(nameArray[1])
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
sizeString := units.CustomSize("%g%s", float64(pageSize), 1024.0, sizeList)
|
|
|
|
pageSizes = append(pageSizes, sizeString)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pageSizes, nil
|
|
|
|
}
|
2015-10-13 05:28:31 +08:00
|
|
|
|
2016-01-09 03:37:18 +08:00
|
|
|
// GetPids returns all pids, that were added to cgroup at path.
|
2015-10-13 05:28:31 +08:00
|
|
|
func GetPids(path string) ([]int, error) {
|
2016-01-09 03:37:18 +08:00
|
|
|
return readProcsFile(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllPids returns all pids, that were added to cgroup at path and to all its
|
|
|
|
// subcgroups.
|
|
|
|
func GetAllPids(path string) ([]int, error) {
|
2015-10-13 05:28:31 +08:00
|
|
|
var pids []int
|
|
|
|
// collect pids from all sub-cgroups
|
|
|
|
err := filepath.Walk(path, func(p string, info os.FileInfo, iErr error) error {
|
2015-10-14 04:57:30 +08:00
|
|
|
dir, file := filepath.Split(p)
|
2016-07-20 07:33:09 +08:00
|
|
|
if file != CgroupProcesses {
|
2015-10-13 05:28:31 +08:00
|
|
|
return nil
|
|
|
|
}
|
2015-10-14 04:57:30 +08:00
|
|
|
if iErr != nil {
|
|
|
|
return iErr
|
|
|
|
}
|
|
|
|
cPids, err := readProcsFile(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-10-13 05:28:31 +08:00
|
|
|
}
|
2015-10-14 04:57:30 +08:00
|
|
|
pids = append(pids, cPids...)
|
2015-10-13 05:28:31 +08:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return pids, err
|
|
|
|
}
|
2016-07-20 07:33:09 +08:00
|
|
|
|
|
|
|
// WriteCgroupProc writes the specified pid into the cgroup's cgroup.procs file
|
|
|
|
func WriteCgroupProc(dir string, pid int) error {
|
|
|
|
// 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", CgroupProcesses)
|
|
|
|
}
|
|
|
|
|
2018-12-17 23:34:47 +08:00
|
|
|
// Dont attach any pid to the cgroup if -1 is specified as a pid
|
|
|
|
if pid == -1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cgroupProcessesFile, err := os.OpenFile(filepath.Join(dir, CgroupProcesses), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to write %v to %v: %v", pid, CgroupProcesses, err)
|
|
|
|
}
|
|
|
|
defer cgroupProcessesFile.Close()
|
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
_, err = cgroupProcessesFile.WriteString(strconv.Itoa(pid))
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
2016-07-20 07:33:09 +08:00
|
|
|
}
|
2018-12-17 23:34:47 +08:00
|
|
|
|
|
|
|
// EINVAL might mean that the task being added to cgroup.procs is in state
|
|
|
|
// TASK_NEW. We should attempt to do so again.
|
|
|
|
if isEINVAL(err) {
|
|
|
|
time.Sleep(30 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("failed to write %v to %v: %v", pid, CgroupProcesses, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func isEINVAL(err error) bool {
|
|
|
|
switch err := err.(type) {
|
|
|
|
case *os.PathError:
|
|
|
|
return err.Err == unix.EINVAL
|
|
|
|
default:
|
|
|
|
return false
|
2016-07-20 07:33:09 +08:00
|
|
|
}
|
|
|
|
}
|