Skip to content

Commit 529b6cf

Browse files
committed
hugetlb: correctly parse hugetlb.<size>.events files
Signed-off-by: Tomer Lev <me@tomerlev.com>
1 parent f1e92d8 commit 529b6cf

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

cgroup2/utils.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,31 @@ func getStatFileContentUint64(filePath string) uint64 {
293293
return res
294294
}
295295

296+
// Gets uint64 parsed content of key-value cgroup stat file
297+
func getKVStatsFileContentUint64(filePath string, propertyName string) uint64 {
298+
f, err := os.Open(filePath)
299+
if err != nil {
300+
return 0
301+
}
302+
defer f.Close()
303+
304+
s := bufio.NewScanner(f)
305+
for s.Scan() {
306+
name, value, err := parseKV(s.Text())
307+
if name == propertyName {
308+
if err != nil {
309+
log.L.WithError(err).Errorf("unable to parse %q as a uint from Cgroup file %q", propertyName, filePath)
310+
return 0
311+
}
312+
return value
313+
}
314+
}
315+
if err = s.Err(); err != nil {
316+
log.L.WithError(err).Errorf("error reading Cgroup file %q for property %q", filePath, propertyName)
317+
}
318+
return 0
319+
}
320+
296321
func readIoStats(path string) []*stats.IOEntry {
297322
// more details on the io.stat file format: https://www.kernel.org/doc/Documentation/cgroup-v2.txt
298323
var usage []*stats.IOEntry
@@ -423,7 +448,7 @@ func readHugeTlbStats(path string) []*stats.HugeTlbStat {
423448
Max: getStatFileContentUint64(filepath.Join(path, "hugetlb."+pagesize+".max")),
424449
Current: getStatFileContentUint64(filepath.Join(path, "hugetlb."+pagesize+".current")),
425450
Pagesize: pagesize,
426-
Failcnt: getStatFileContentUint64(filepath.Join(path, "hugetlb."+pagesize+".events")),
451+
Failcnt: getKVStatsFileContentUint64(filepath.Join(path, "hugetlb."+pagesize+".events"), "max"),
427452
}
428453
}
429454
return usage

0 commit comments

Comments
 (0)