-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcgroup.go
More file actions
160 lines (135 loc) · 4.08 KB
/
cgroup.go
File metadata and controls
160 lines (135 loc) · 4.08 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2017 Bobby Powers. All rights reserved.
// Use of this source code is governed by the ISC
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"github.com/containerd/cgroups/v3"
"github.com/containerd/cgroups/v3/cgroup1"
"github.com/containerd/cgroups/v3/cgroup2"
specs "github.com/opencontainers/runtime-spec/specs-go"
)
// MemoryStat holds memory statistics from the cgroup
type MemoryStat struct {
Usage uint64
Kernel uint64 // Separate kernel memory tracking (v1 only; in v2 it's included in Usage)
Hugetlb uint64 // Hugetlb usage (not included in standard RSS)
}
// CgroupManager provides an abstraction over cgroups v1 and v2
type CgroupManager interface {
Add(pid int) error
Stat() (*MemoryStat, error)
Delete() error
}
// cgroupV1Manager wraps cgroup1.Cgroup
type cgroupV1Manager struct {
cgroup cgroup1.Cgroup
}
// cgroupV2Manager wraps cgroup2.Manager
type cgroupV2Manager struct {
manager *cgroup2.Manager
}
// NewCgroupManager creates a cgroup manager for the current system
// It auto-detects whether to use v1 or v2 based on system configuration
func NewCgroupManager(path string, memLimit int64) (CgroupManager, error) {
mode := cgroups.Mode()
switch mode {
case cgroups.Unified:
return newV2Manager(path, memLimit)
case cgroups.Legacy, cgroups.Hybrid:
return newV1Manager(path, memLimit)
default:
return nil, fmt.Errorf("cgroups not available on this system (mode: %v)", mode)
}
}
// CgroupMode returns a string describing the cgroup mode
func CgroupMode() string {
mode := cgroups.Mode()
switch mode {
case cgroups.Unified:
return "unified (v2)"
case cgroups.Legacy:
return "legacy (v1)"
case cgroups.Hybrid:
return "hybrid (v1+v2)"
default:
return "unavailable"
}
}
func newV1Manager(path string, memLimit int64) (*cgroupV1Manager, error) {
resources := &specs.LinuxResources{
Memory: &specs.LinuxMemory{
Limit: &memLimit,
},
}
cg, err := cgroup1.New(cgroup1.StaticPath(path), resources)
if err != nil {
return nil, fmt.Errorf("cgroup1.New: %w", err)
}
return &cgroupV1Manager{cgroup: cg}, nil
}
func (m *cgroupV1Manager) Add(pid int) error {
return m.cgroup.Add(cgroup1.Process{Pid: pid})
}
func (m *cgroupV1Manager) Stat() (*MemoryStat, error) {
stats, err := m.cgroup.Stat(cgroup1.IgnoreNotExist)
if err != nil {
return nil, fmt.Errorf("cgroup.Stat: %w", err)
}
if stats == nil || stats.Memory == nil {
return nil, fmt.Errorf("cgroup.Stat returned nil memory stats")
}
return &MemoryStat{
Usage: stats.Memory.Usage.Usage,
Kernel: stats.Memory.Kernel.Usage,
}, nil
}
func (m *cgroupV1Manager) Delete() error {
return m.cgroup.Delete()
}
func newV2Manager(path string, memLimit int64) (*cgroupV2Manager, error) {
// In cgroups v2, we use the unified hierarchy under /sys/fs/cgroup
// The path should be relative to the cgroup root
resources := &cgroup2.Resources{
Memory: &cgroup2.Memory{
Max: &memLimit,
},
HugeTlb: &cgroup2.HugeTlb{},
}
// Remove leading slash if present for v2
if len(path) > 0 && path[0] == '/' {
path = path[1:]
}
mgr, err := cgroup2.NewManager("/sys/fs/cgroup", "/"+path, resources)
if err != nil {
return nil, fmt.Errorf("cgroup2.NewManager: %w", err)
}
return &cgroupV2Manager{manager: mgr}, nil
}
func (m *cgroupV2Manager) Add(pid int) error {
return m.manager.AddProc(uint64(pid))
}
func (m *cgroupV2Manager) Stat() (*MemoryStat, error) {
stats, err := m.manager.Stat()
if err != nil {
return nil, fmt.Errorf("manager.Stat: %w", err)
}
if stats == nil || stats.Memory == nil {
return nil, fmt.Errorf("manager.Stat returned nil memory stats")
}
// In cgroups v2, kernel memory is included in the total usage and cannot
// be queried separately. The separate kmem controller was removed in v2.
// See: https://github.com/opencontainers/runtime-spec/issues/1005
var hugetlb uint64
for _, h := range stats.Hugetlb {
hugetlb += h.Current
}
return &MemoryStat{
Usage: stats.Memory.Usage,
Kernel: 0, // Not separately tracked in v2 (included in Usage)
Hugetlb: hugetlb,
}, nil
}
func (m *cgroupV2Manager) Delete() error {
return m.manager.Delete()
}