-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathcgroup_linux.go
57 lines (44 loc) · 1.14 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
// +build linux
package optimus
import (
"fmt"
"os"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sonm-io/core/insonmnia/cgroups"
)
const (
defaultCPUPeriod = uint64(100000)
)
func RestrictUsage(cfg *RestrictionsConfig) (Deleter, error) {
control, _, err := cgroups.NewCgroupManager(cfg.Name, convertLinuxResources(cfg))
if err != nil {
return nil, err
}
if err := control.Add(cgroups.Process{Pid: os.Getpid()}); err != nil {
return nil, fmt.Errorf("failed to add Optimus into cgroup: %v", err)
}
return control, nil
}
func convertLinuxResources(cfg *RestrictionsConfig) *specs.LinuxResources {
return &specs.LinuxResources{
CPU: convertCPUConfig(cfg),
Memory: convertMemoryConfig(cfg),
}
}
func convertCPUConfig(cfg *RestrictionsConfig) *specs.LinuxCPU {
quota := int64(float64(defaultCPUPeriod) * cfg.CPUCount)
period := defaultCPUPeriod
return &specs.LinuxCPU{
Quota: "a,
Period: &period,
}
}
func convertMemoryConfig(cfg *RestrictionsConfig) *specs.LinuxMemory {
if cfg.MemoryLimit == 0 {
return nil
}
v := int64(cfg.MemoryLimit * (1 << 20))
return &specs.LinuxMemory{
Limit: &v,
}
}