Skip to content

Commit aa4dc2f

Browse files
Adjust memory usage for inactive file. (#1332)
* Adjust memory usage for inactive file. Inactive file can be reclaimed if necessary. But, croup memory.usage_in_bytes shows a high value if the memory has grown and does not need to be reclaimed. So, using that inflates the actual memory usage. Adjust usage by discounting reclaimable inactive file memory. * generated protobuf * clean up * fix stupid mistakes, thank you @milos-lk --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent ff54940 commit aa4dc2f

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

utils/hwstats/memory_linux.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,25 @@
1717
package hwstats
1818

1919
import (
20+
"bufio"
21+
"fmt"
2022
"os"
2123
"strconv"
24+
"strings"
2225

2326
"github.com/livekit/protocol/logger"
2427
)
2528

2629
const (
27-
memUsagePathV1 = "/sys/fs/cgroup/memory/memory.usage_in_bytes"
28-
memLimitPathV1 = "/sys/fs/cgroup/memory/memory.limit_in_bytes"
30+
memUsagePathV1 = "/sys/fs/cgroup/memory/memory.usage_in_bytes"
31+
memLimitPathV1 = "/sys/fs/cgroup/memory/memory.limit_in_bytes"
32+
memStatPathV1 = "/sys/fs/cgroup/memory/memory.stat"
33+
totalInactiveFileV1 = "total_inactive_file"
2934

3035
memCurrentPathV2 = "/sys/fs/cgroup/memory.current"
3136
memMaxPathV2 = "/sys/fs/cgroup/memory.max"
37+
memStatPathV2 = "/sys/fs/cgroup/memory.stat"
38+
inactiveFileV2 = "inactive_file"
3239
)
3340

3441
type memInfoGetter interface {
@@ -95,6 +102,10 @@ func (cg *memInfoGetterV1) getMemory() (uint64, uint64, error) {
95102
if err != nil {
96103
return 0, 0, err
97104
}
105+
totalInactiveFile, err := readValueOfKeyFromFile(memStatPathV1, totalInactiveFileV1)
106+
if err == nil && usage > totalInactiveFile {
107+
usage -= totalInactiveFile
108+
}
98109

99110
total, err := readValueFromFile(memLimitPathV1)
100111
if err != nil {
@@ -131,6 +142,10 @@ func (cg *memInfoGetterV2) getMemory() (uint64, uint64, error) {
131142
if err != nil {
132143
return 0, 0, err
133144
}
145+
inactiveFile, err := readValueOfKeyFromFile(memStatPathV2, inactiveFileV2)
146+
if err == nil && usage > inactiveFile {
147+
usage -= inactiveFile
148+
}
134149

135150
total, err := readValueFromFile(memMaxPathV2)
136151
if err != nil {
@@ -155,3 +170,34 @@ func readValueFromFile(file string) (uint64, error) {
155170
// Skip the trailing EOL
156171
return strconv.ParseUint(string(b[:len(b)-1]), 10, 64)
157172
}
173+
174+
func readValueOfKeyFromFile(file string, key string) (uint64, error) {
175+
fd, err := os.Open(file)
176+
if err != nil {
177+
return 0, err
178+
}
179+
defer fd.Close()
180+
181+
scanner := bufio.NewScanner(fd)
182+
for scanner.Scan() {
183+
line := scanner.Text()
184+
parts := strings.Split(line, " ")
185+
if len(parts) != 2 {
186+
continue
187+
}
188+
189+
if parts[0] != key {
190+
continue
191+
}
192+
193+
val := strings.TrimSpace(parts[1])
194+
if v, err := strconv.ParseUint(val, 10, 64); err == nil {
195+
return v, nil
196+
}
197+
}
198+
if err := scanner.Err(); err != nil {
199+
return 0, fmt.Errorf("scan error for %s, key: %s: %s", file, key, err)
200+
}
201+
202+
return 0, nil
203+
}

0 commit comments

Comments
 (0)