-
Notifications
You must be signed in to change notification settings - Fork 24
/
cgroup_linux.go
81 lines (67 loc) · 2.15 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package watchdog
import (
"context"
"fmt"
"os"
"github.com/containerd/cgroups"
"google.golang.org/protobuf/proto"
"github.com/fluxninja/aperture/v2/pkg/log"
watchdogconfig "github.com/fluxninja/aperture/v2/pkg/watchdog/config"
)
type cgroupBase struct {
cgroup cgroups.Cgroup
}
func (cg *cgroupBase) load() error {
if cg.cgroup == nil {
// use self path unless our PID is 1, in which case we're running inside
// a container and our limits are in the root path.
path := cgroups.NestedPath("")
if pid := os.Getpid(); pid == 1 {
path = cgroups.RootPath
}
cgroup, err := cgroups.Load(cgroups.SingleSubsystem(cgroups.V1, cgroups.Memory), path)
if err != nil {
return fmt.Errorf("failed to load cgroup for process: %w", err)
}
cg.cgroup = cgroup
}
return nil
}
func (cg *cgroupBase) cgroupUsage() (uint64, uint64, error) {
if err := cg.load(); err != nil {
return 0, 0, err
}
stat, err := cg.cgroup.Stat()
if err != nil {
return 0, 0, fmt.Errorf("failed to load memory cgroup stats: %w", err)
} else if stat.Memory == nil || stat.Memory.Usage == nil {
return 0, 0, fmt.Errorf("cgroup memory stats are nil; aborting")
}
return stat.Memory.Usage.Limit, stat.Memory.Usage.Usage, nil
}
type cgroupWatermarks struct {
cgroupBase
watchdogconfig.WatermarksPolicy
}
type cgroupAdaptive struct {
cgroupBase
watchdogconfig.AdaptivePolicy
}
// Valid checks if the policy is valid in linux.
func (policy *cgroupWatermarks) Valid() bool {
return true
}
// Check evaluates the cgroup memory usage and runs GC at configured watermarks of memory utilization.
func (policy *cgroupWatermarks) Check(ctx context.Context) (proto.Message, error) {
log.Debug().Msg("CGroup watermarks check triggered")
return check(policy, ctx, policy.cgroupUsage)
}
// Valid checks if the policy is valid in linux.
func (policy *cgroupAdaptive) Valid() bool {
return true
}
// Check evaluates the cgroup memory usage and runs GC at configured adaptive thresholds of memory utilization.
func (policy *cgroupAdaptive) Check(ctx context.Context) (proto.Message, error) {
log.Debug().Msg("CGroup watermarks check triggered")
return check(policy, ctx, policy.cgroupUsage)
}