-
Notifications
You must be signed in to change notification settings - Fork 53
/
memory.go
64 lines (58 loc) · 1.73 KB
/
memory.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
package cgroup
import (
"path"
)
const maxMemory = 1 << 62
type MemoryStat struct {
RSS uint64
Cache uint64
Limit uint64
}
func (cg *Cgroup) MemoryStat() (*MemoryStat, error) {
if cg.Version == V1 {
return cg.memoryStatV1()
}
return cg.memoryStatV2()
}
func (cg *Cgroup) memoryStatV1() (*MemoryStat, error) {
vars, err := readVariablesFromFile(path.Join(cgRoot, "memory", cg.subsystems["memory"], "memory.stat"))
if err != nil {
return nil, err
}
limit, err := readUintFromFile(path.Join(cgRoot, "memory", cg.subsystems["memory"], "memory.limit_in_bytes"))
if err != nil {
return nil, err
}
if limit > maxMemory {
limit = 0
}
// Note from https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt:
// Only anonymous and swap cache memory is listed as part of 'rss' stat.
// This should not be confused with the true 'resident set size' or the
// amount of physical memory used by the cgroup.
// 'rss + mapped_file" will give you resident set size of cgroup.
// (Note: file and shmem may be shared among other cgroups. In that case,
// mapped_file is accounted only when the memory cgroup is owner of page
// cache.)
return &MemoryStat{
RSS: vars["rss"] + vars["mapped_file"],
Cache: vars["cache"],
Limit: limit,
}, nil
}
func (cg *Cgroup) memoryStatV2() (*MemoryStat, error) {
current, err := readUintFromFile(path.Join(cgRoot, cg.subsystems[""], "memory.current"))
if err != nil {
return nil, err
}
vars, err := readVariablesFromFile(path.Join(cgRoot, cg.subsystems[""], "memory.stat"))
if err != nil {
return nil, err
}
limit, _ := readUintFromFile(path.Join(cgRoot, cg.subsystems[""], "memory.max"))
return &MemoryStat{
RSS: current - vars["file"],
Cache: vars["file"],
Limit: limit,
}, nil
}