-
Notifications
You must be signed in to change notification settings - Fork 58
/
cgroup_linux.go
65 lines (54 loc) · 1.13 KB
/
cgroup_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
// +build linux
package netflow
import (
"fmt"
"github.com/containerd/cgroups"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/spf13/cast"
)
type cgroupsLimiter struct {
controls []cgroups.Cgroup
}
// configure
func (r *cgroupsLimiter) configure(pid int, core float64, mbn int) error {
const (
cpuUnit = 10000
memUnit = 1024 * 1024
)
if core <= 0 {
core = 1
}
var (
quota int64 = int64(core * cpuUnit) // core * 1u
period uint64 = 10000 // 1u
mem int64 = int64(mbn * memUnit)
)
cfg := &specs.LinuxResources{
CPU: &specs.LinuxCPU{
Period: &period,
Quota: "a,
},
}
if mem != 0 {
cfg.Memory = &specs.LinuxMemory{
Limit: &mem,
}
}
// file as /sys/fs/cgroup/cpu/netflow/...
cgroupPath := "/netflow"
control, err := cgroups.New(cgroups.V1, cgroups.StaticPath(cgroupPath), cfg)
if err != nil {
return err
}
r.controls = append(r.controls, control)
err = control.Add(cgroups.Process{Pid: cast.ToInt(pid)})
return err
}
// free
func (r *cgroupsLimiter) free() error {
for _, ctrl := range r.controls {
ctrl.Delete()
}
fmt.Println("exit")
return nil
}