Skip to content

Commit 32432e1

Browse files
committed
share the memory.stat sum with memoryUsed
Signed-off-by: Benjamin Elder <[email protected]>
1 parent 506a5a6 commit 32432e1

2 files changed

Lines changed: 23 additions & 45 deletions

File tree

‎internal/ateworker/cgroup/actor.go‎

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
package cgroup
1818

1919
import (
20-
"bytes"
2120
"context"
2221
"errors"
2322
"fmt"
2423
"io/fs"
2524
"os"
2625
"path/filepath"
26+
"slices"
2727
"strconv"
2828
"strings"
2929
"syscall"
@@ -194,40 +194,11 @@ func podMemory(root string) (limit, used int64, err error) {
194194
return limit, used, nil
195195
}
196196

197-
// memoryUnreclaimable is what the cgroup holds besides clean page cache, from
198-
// one memory.stat read. memory.stat splits charged memory into anon, file,
199-
// kernel and sock, where file is page cache plus shmem (memfd guest RAM), so
200-
// this is anon + shmem + kernel + sock plus page cache that is dirty or under
201-
// writeback. The kernel only flushes memory.stat past a threshold of updates,
202-
// so mixing it with memory.current could count fresh page cache as used.
203-
// https://github.com/torvalds/linux/blob/v7.0/mm/memcontrol.c#L561-L564
197+
// memoryUnreclaimable is memoryUsed plus page cache that is dirty or under
198+
// writeback, which reclaim can't drop either.
204199
func memoryUnreclaimable(root string) (int64, error) {
205-
path := filepath.Join(root, "memory.stat")
206-
b, err := os.ReadFile(path)
207-
if err != nil {
208-
return 0, err
209-
}
210-
var used int64
211-
var anon bool
212-
for line := range bytes.Lines(b) {
213-
f := bytes.Fields(line)
214-
if len(f) != 2 {
215-
continue
216-
}
217-
switch k := string(f[0]); k {
218-
case "anon", "shmem", "kernel", "sock", "file_dirty", "file_writeback":
219-
v, err := strconv.ParseInt(string(f[1]), 10, 64)
220-
if err != nil {
221-
return 0, fmt.Errorf("while parsing %s in %q: %w", k, path, err)
222-
}
223-
used += v
224-
anon = anon || k == "anon"
225-
}
226-
}
227-
if !anon {
228-
return 0, fmt.Errorf("no anon in %q", path)
229-
}
230-
return used, nil
200+
used, err := sumMemoryStat(root, slices.Concat(memoryUsedStats, []string{"file_dirty", "file_writeback"})...)
201+
return int64(used), err
231202
}
232203

233204
func parseBytes(raw []byte) (int64, error) {

‎internal/ateworker/cgroup/memwatch.go‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"io/fs"
2323
"os"
2424
"path/filepath"
25+
"slices"
2526
"strconv"
2627
"strings"
2728
"time"
@@ -131,32 +132,38 @@ func memoryCurrent(dir string) (uint64, error) {
131132
// used.
132133
// https://github.com/torvalds/linux/blob/v7.0/mm/memcontrol.c#L561-L564
133134
func memoryUsed(dir string) (uint64, error) {
135+
return sumMemoryStat(dir, memoryUsedStats...)
136+
}
137+
138+
// memoryUsedStats are the memory.stat keys memoryUsed sums
139+
var memoryUsedStats = []string{"anon", "shmem", "kernel", "sock"}
140+
141+
// sumMemoryStat sums keys from one read of dir's memory.stat, which must have
142+
// anon.
143+
func sumMemoryStat(dir string, keys ...string) (uint64, error) {
134144
path := filepath.Join(dir, "memory.stat")
135145
b, err := os.ReadFile(path)
136146
if err != nil {
137147
return 0, err
138148
}
139-
var used uint64
149+
var sum uint64
140150
var anon bool
141151
for line := range bytes.Lines(b) {
142152
f := bytes.Fields(line)
143-
if len(f) != 2 {
153+
if len(f) != 2 || !slices.Contains(keys, string(f[0])) {
144154
continue
145155
}
146-
switch k := string(f[0]); k {
147-
case "anon", "shmem", "kernel", "sock":
148-
v, err := strconv.ParseUint(string(f[1]), 10, 64)
149-
if err != nil {
150-
return 0, fmt.Errorf("parsing %s in %q: %w", k, path, err)
151-
}
152-
used += v
153-
anon = anon || k == "anon"
156+
v, err := strconv.ParseUint(string(f[1]), 10, 64)
157+
if err != nil {
158+
return 0, fmt.Errorf("parsing %s in %q: %w", f[0], path, err)
154159
}
160+
sum += v
161+
anon = anon || string(f[0]) == "anon"
155162
}
156163
if !anon {
157164
return 0, fmt.Errorf("no anon in %q", path)
158165
}
159-
return used, nil
166+
return sum, nil
160167
}
161168

162169
// memoryStall is memory.pressure's some total: microseconds during which at

0 commit comments

Comments
 (0)