-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmemoryusage.go
More file actions
35 lines (29 loc) · 975 Bytes
/
memoryusage.go
File metadata and controls
35 lines (29 loc) · 975 Bytes
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
// Package memoryusage provides memory usage reading from cgroup v2.
package memoryusage
import (
"fmt"
"os"
"strconv"
"strings"
)
const cgroupV2MemoryCurrentPath = "/sys/fs/cgroup/memory.current"
// ReadCurrent reads the current memory usage in bytes from the cgroup v2
// memory.current file at the default path. Returns an error if the file
// cannot be read or parsed.
func ReadCurrent() (int64, error) {
return ReadCurrentFromPath(cgroupV2MemoryCurrentPath)
}
// ReadCurrentFromPath reads the current memory usage in bytes from the
// provided cgroup v2 memory.current file path. Returns an error if the
// file cannot be read or parsed.
func ReadCurrentFromPath(path string) (int64, error) {
data, err := os.ReadFile(path)
if err != nil {
return 0, fmt.Errorf("reading %s: %w", path, err)
}
val, err := strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
if err != nil {
return 0, fmt.Errorf("parsing %s: %w", path, err)
}
return val, nil
}