-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcgroups.go
59 lines (54 loc) · 1.22 KB
/
cgroups.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package ginit
import (
"bufio"
"os"
"strconv"
"strings"
)
// Controller represents a cgroup system controller
// see http://man7.org/linux/man-pages/man7/cgroups.7.html
type Controller struct {
Name string
Hierarchy int
NumCgroups int
Enabled bool
}
// ReadControllers returns Cgroup controllers listed at /proc/cgroups
// Modified from https://github.com/opencontainers/runc/blob/master/libcontainer/cgroups/utils.go
func ReadControllers() ([]Controller, error) {
f, err := os.Open("/proc/cgroups")
if err != nil {
return nil, err
}
defer f.Close()
controllers := []Controller{}
s := bufio.NewScanner(f)
for s.Scan() {
text := s.Text()
if text[0] != '#' {
parts := strings.Fields(text)
if len(parts) >= 4 && parts[3] != "0" {
cont := Controller{
Name: parts[0],
}
n, err := strconv.ParseInt(parts[1], 0, 64)
if err == nil {
cont.Hierarchy = int(n)
}
n, err = strconv.ParseInt(parts[2], 0, 64)
if err == nil {
cont.NumCgroups = int(n)
}
e, err := strconv.ParseBool(parts[3])
if err == nil {
cont.Enabled = e
}
controllers = append(controllers, cont)
}
}
}
if err := s.Err(); err != nil {
return nil, err
}
return controllers, nil
}