-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgroup.go
104 lines (79 loc) · 2.79 KB
/
cgroup.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package subsystem
import (
"bufio"
"fmt"
"github.com/D3rus/go-library/logger"
"github.com/spf13/cast"
"os"
"strings"
)
//go:generate mockgen -package=subsystem -destination=mock_logger_test.go github.com/D3rus/go-library/logger Informer
const (
processCgroupMountPointFormat = "/proc/%d/cgroup"
cgroupSeparator = ":"
cgroupSubSystemSeparator = ","
cgroupIdFieldNumber uint8 = 0
cgroupSubSystemFieldNumber uint8 = 1
cgroupNameMountInfoFieldNumber uint8 = 2
cgroupCountFields uint8 = 3
)
type CGroupSubSystemName string
type CGroupSubSystem struct {
ID uint8
Name CGroupSubSystemName
MountPointRoot MountPointRootPath
}
type CGroupSubSystems map[CGroupSubSystemName]*CGroupSubSystem
func NewCGroupFromLine(line string, informer logger.Informer) ([]*CGroupSubSystem, error) {
fields := strings.SplitN(line, cgroupSeparator, int(cgroupCountFields))
if len(fields) != int(cgroupCountFields) {
return nil, fmt.Errorf(
"cgroup.subsystem: new from string '%s' failed, wrong number of fields, expected %d, found %d",
line,
cgroupCountFields,
len(fields),
)
}
id, err := cast.ToUint8E(fields[cgroupIdFieldNumber])
if err != nil {
return nil, fmt.Errorf("cgroup.subsystem: subsystem id parse failed: %w", err)
}
subSystemNames := strings.Split(fields[cgroupSubSystemFieldNumber], cgroupSubSystemSeparator)
subSystems := make([]*CGroupSubSystem, len(subSystemNames))
for index, subSystemName := range subSystemNames {
subSystems[index] = &CGroupSubSystem{
ID: id,
Name: CGroupSubSystemName(subSystemName),
MountPointRoot: MountPointRootPath(fields[cgroupNameMountInfoFieldNumber]),
}
informer.Infof("cgroup.parse: sub system %+v", *subSystems[index])
}
return subSystems, nil
}
func ParseCGroup(processCGroupFilePath string, informer logger.Informer) (CGroupSubSystems, error) {
informer.Infof("cgroup.parse: cgroup file path '%s'", processCGroupFilePath)
cgroupFile, err := os.OpenFile(processCGroupFilePath, os.O_RDONLY, 0)
if err != nil {
return nil, fmt.Errorf("cgroup.parse: open cgroup file '%s' failed: %w", processCGroupFilePath, err)
}
defer cgroupFile.Close()
lineScanner := bufio.NewScanner(cgroupFile)
lineScanner.Split(bufio.ScanLines)
subSystems := CGroupSubSystems{}
for lineScanner.Scan() {
parsedSubSystems, err := NewCGroupFromLine(lineScanner.Text(), informer)
if err != nil {
return nil, err
}
for _, subSystem := range parsedSubSystems {
subSystems[subSystem.Name] = subSystem
}
}
if err := lineScanner.Err(); err != nil {
return nil, fmt.Errorf("cgroup.parse: unable to read cgroup file '%s': %w", processCGroupFilePath, err)
}
return subSystems, nil
}
func CGroupPidPath(pid uint64) string {
return fmt.Sprintf(processCgroupMountPointFormat, pid)
}