-
Notifications
You must be signed in to change notification settings - Fork 0
/
v1.go
114 lines (97 loc) · 2.28 KB
/
v1.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
105
106
107
108
109
110
111
112
113
114
package cgroup
import (
"errors"
"fmt"
"github.com/containerd/cgroups"
"github.com/containerd/cgroups/v3/cgroup1"
"github.com/opencontainers/runtime-spec/specs-go"
)
type CgroupV1 interface {
ICgroup
}
type cgroupImplV1 struct {
cg cgroups.Cgroup
name string
version CgroupVersion
res *specs.LinuxResources
oomkill bool
pid int
debug bool
}
func newCgroupImplV1() *cgroupImplV1 {
return &cgroupImplV1{
version: V1,
res: &specs.LinuxResources{
CPU: &specs.LinuxCPU{},
Memory: &specs.LinuxMemory{},
BlockIO: &specs.LinuxBlockIO{},
Network: &specs.LinuxNetwork{},
Pids: &specs.LinuxPids{},
Devices: make([]specs.LinuxDeviceCgroup, 0),
Rdma: make(map[string]specs.LinuxRdma),
HugepageLimits: make([]specs.LinuxHugepageLimit, 0),
},
oomkill: true,
pid: 0,
debug: false,
}
}
func (c *cgroupImplV1) Version() CgroupVersion {
return c.version
}
func (c *cgroupImplV1) SetOptions(options ...Option) {
for _, opt := range options {
opt(c)
}
}
func (c *cgroupImplV1) SetDebug(debug bool) {
c.debug = debug
}
func (c *cgroupImplV1) Instance() any {
return c
}
func (c *cgroupImplV1) SetName(name string) {
c.name = name
}
func (c *cgroupImplV1) Close() error {
return c.cg.Delete()
}
func (c *cgroupImplV1) Load() error {
cgroup1.Default()
var err error
c.cg, err = cgroups.Load(cgroups.V1, cgroups.StaticPath(fmt.Sprintf("/%s", c.name)))
return err
}
func (c *cgroupImplV1) Create() error {
if c.name == "" {
return errors.New("name is empty")
}
var err error
c.cg, err = cgroups.New(cgroups.V1, cgroups.StaticPath(fmt.Sprintf("/%s", c.name)), c.res)
return err
}
func (c *cgroupImplV1) LimitPid(pid int) error {
c.pid = pid
return c.cg.Add(cgroups.Process{Pid: pid})
}
func (c *cgroupImplV1) disableOOMKiller() {
var oom *bool = new(bool)
*oom = true
c.res.Memory.DisableOOMKiller = oom
c.oomkill = false
}
func (c *cgroupImplV1) GetLimitPids() ([]uint64, error) {
procs, err := c.cg.Processes(cgroups.Name(c.name), true)
if err != nil {
return nil, err
}
wpids := []uint64{}
for _, p := range procs {
wpids = append(wpids, uint64(p.Pid))
}
return wpids, nil
}
func (c *cgroupImplV1) Stats() (any, error) {
// return *v1.Metrics
return c.cg.Stat()
}