-
Notifications
You must be signed in to change notification settings - Fork 25
/
v1controller_linux.go
69 lines (60 loc) · 1.75 KB
/
v1controller_linux.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
package cgroup
import (
"errors"
"path"
"strconv"
"strings"
)
// v1controller is the accessor for single cgroup resource with given path
type v1controller struct {
path string
}
// ErrNotInitialized returned when trying to read from not initialized cgroup
var ErrNotInitialized = errors.New("cgroup was not initialized")
// newV1Controller creates a cgroup accessor with given path (path needs to be created in advance)
func newV1Controller(p string) *v1controller {
return &v1controller{path: p}
}
// WriteUint writes uint64 into given file
func (c *v1controller) WriteUint(filename string, i uint64) error {
if c == nil || c.path == "" {
return nil
}
return c.WriteFile(filename, []byte(strconv.FormatUint(i, 10)))
}
// ReadUint read uint64 from given file
func (c *v1controller) ReadUint(filename string) (uint64, error) {
if c == nil || c.path == "" {
return 0, ErrNotInitialized
}
b, err := c.ReadFile(filename)
if err != nil {
return 0, err
}
s, err := strconv.ParseUint(strings.TrimSpace(string(b)), 10, 64)
if err != nil {
return 0, err
}
return s, nil
}
// WriteFile writes cgroup file and handles potential EINTR error while writes to
// the slow device (cgroup)
func (c *v1controller) WriteFile(name string, content []byte) error {
if c == nil || c.path == "" {
return ErrNotInitialized
}
p := path.Join(c.path, name)
return writeFile(p, content, filePerm)
}
// ReadFile reads cgroup file and handles potential EINTR error while read to
// the slow device (cgroup)
func (c *v1controller) ReadFile(name string) ([]byte, error) {
if c == nil || c.path == "" {
return nil, nil
}
p := path.Join(c.path, name)
return readFile(p)
}
func (c *v1controller) AddProc(pids ...int) error {
return AddProcesses(path.Join(c.path, cgroupProcs), pids)
}