use inactive memory when calculating cgroups total memory (#19249)
* use inactive memory when calculating cgroups total memory * zero inactive if it is invalid * fix comments
Costa Tsaousis committed
Dec 19, 2024 at 15:23 UTC
decc5632e2a78d3d0fe1b3b4c279ba4ad33e138f
1 file changed
+50
-6
src/libnetdata/os/system_memory.c
+50
-6
@@ -68,7 +68,8 @@ OS_SYSTEM_MEMORY os_system_memory(bool query_total_ram) {
68
69
static OS_SYSTEM_MEMORY os_system_memory_cgroup_v1(bool query_total_ram __maybe_unused) {
70
static OS_SYSTEM_MEMORY sm = {0, 0};
71
- char buf[64];
71
+ char buf[4096];
72
+ uint64_t used = 0, inactive = 0;
73
74
if(query_total_ram || sm.ram_total_bytes == 0) {
75
if (read_txt_file("/sys/fs/cgroup/memory/memory.limit_in_bytes", buf, sizeof(buf)) != 0) {
@@ -89,14 +90,35 @@ static OS_SYSTEM_MEMORY os_system_memory_cgroup_v1(bool query_total_ram __maybe_
90
goto failed;
91
}
92
92
- uint64_t used = strtoull(buf, NULL, 10);
93
+ used = strtoull(buf, NULL, 10);
94
if(!used || used > sm.ram_total_bytes) {
95
// nd_log(NDLS_DAEMON, NDLP_ERR, "SYSTEM_MEMORY: cgroups v1: used is %llu, total is %llu: used is invalid",
96
// used, sm.ram_total_bytes);
97
goto failed;
98
}
99
99
- sm.ram_available_bytes = sm.ram_total_bytes - used;
100
+ if (read_txt_file("/sys/fs/cgroup/memory.stat", buf, sizeof(buf)) != 0) {
101
+// nd_log(NDLS_DAEMON, NDLP_ERR, "SYSTEM_MEMORY: cgroups v2: cannot read /sys/fs/cgroup/memory.stat");
102
+ goto done;
103
+ }
104
+
105
+ const char *inactive_str = strstr(buf, "total_inactive_file ");
106
+ if(!inactive_str) {
107
+// nd_log(NDLS_DAEMON, NDLP_ERR, "SYSTEM_MEMORY: cgroups v2: cannot file 'inactive_file ' in /sys/fs/cgroup/memory.stat");
108
+ goto done;
109
+ }
110
+ inactive_str += 20;
111
+
112
+ inactive = strtoull(inactive_str, NULL, 0);
113
+ if(!inactive || inactive > used) {
114
+// nd_log(NDLS_DAEMON, NDLP_ERR, "SYSTEM_MEMORY: cgroups v2: inactive is %llu, used is %llu: inactive is invalid",
115
+// inactive, used);
116
+ inactive = 0;
117
+ goto done;
118
+ }
119
+
120
+done:
121
+ sm.ram_available_bytes = sm.ram_total_bytes - (used - inactive);
122
return sm;
123
124
failed:
@@ -107,7 +129,8 @@ failed:
129
130
static OS_SYSTEM_MEMORY os_system_memory_cgroup_v2(bool query_total_ram __maybe_unused) {
131
static OS_SYSTEM_MEMORY sm = {0, 0};
110
- char buf[64];
132
+ char buf[4096];
133
+ uint64_t used = 0, inactive = 0;
134
135
if(query_total_ram || sm.ram_total_bytes == 0) {
136
if (read_txt_file("/sys/fs/cgroup/memory.max", buf, sizeof(buf)) != 0) {
@@ -132,14 +155,35 @@ static OS_SYSTEM_MEMORY os_system_memory_cgroup_v2(bool query_total_ram __maybe_
155
goto failed;
156
}
157
135
- uint64_t used = strtoull(buf, NULL, 0);
158
+ used = strtoull(buf, NULL, 0);
159
if(!used || used > sm.ram_total_bytes) {
160
// nd_log(NDLS_DAEMON, NDLP_ERR, "SYSTEM_MEMORY: cgroups v2: used is %llu, total is %llu: used is invalid",
161
// used, sm.ram_total_bytes);
162
goto failed;
163
}
164
142
- sm.ram_available_bytes = sm.ram_total_bytes - used;
165
+ if (read_txt_file("/sys/fs/cgroup/memory.stat", buf, sizeof(buf)) != 0) {
166
+// nd_log(NDLS_DAEMON, NDLP_ERR, "SYSTEM_MEMORY: cgroups v2: cannot read /sys/fs/cgroup/memory.stat");
167
+ goto done;
168
+ }
169
+
170
+ const char *inactive_str = strstr(buf, "inactive_file ");
171
+ if(!inactive_str) {
172
+// nd_log(NDLS_DAEMON, NDLP_ERR, "SYSTEM_MEMORY: cgroups v2: cannot file 'inactive_file ' in /sys/fs/cgroup/memory.stat");
173
+ goto done;
174
+ }
175
+ inactive_str += 14;
176
+
177
+ inactive = strtoull(inactive_str, NULL, 0);
178
+ if(!inactive || inactive > used) {
179
+// nd_log(NDLS_DAEMON, NDLP_ERR, "SYSTEM_MEMORY: cgroups v2: inactive is %llu, used is %llu: inactive is invalid",
180
+// inactive, used);
181
+ inactive = 0;
182
+ goto done;
183
+ }
184
+
185
+done:
186
+ sm.ram_available_bytes = sm.ram_total_bytes - (used - inactive);
187
return sm;
188
189
failed: