|
48 | 48 | #define GET_VM_FREE_PAGE_COUNT "vm.stats.vm.v_free_count"
|
49 | 49 | #define GET_VM_INACT_PAGE_COUNT "vm.stats.vm.v_inactive_count"
|
50 | 50 | #define GET_VM_ACT_PAGE_COUNT "vm.stats.vm.v_active_count"
|
| 51 | +#endif |
51 | 52 |
|
52 | 53 | #define BYTES_TO_KIB(x) (x >> 10)
|
53 |
| -#endif |
54 | 54 |
|
55 | 55 | #include "debug_priv.h"
|
56 | 56 | #include "memusage.h"
|
@@ -173,6 +173,26 @@ static int read_status(const char *source, void *base, struct stat_parser *spt,
|
173 | 173 | return processed == spt_size ? 0 : 1;
|
174 | 174 | }
|
175 | 175 |
|
| 176 | +static size_t get_sys_value(const char *source) |
| 177 | +{ |
| 178 | + FILE *f; |
| 179 | + size_t v; |
| 180 | + |
| 181 | + f = fopen(source, "r"); |
| 182 | + if (f == NULL) { |
| 183 | + return (size_t)-1; |
| 184 | + } |
| 185 | + |
| 186 | + if (fscanf(f, "%zu", &v) != 1) { |
| 187 | + fclose(f); |
| 188 | + return (size_t)-1; |
| 189 | + } |
| 190 | + |
| 191 | + fclose(f); |
| 192 | + |
| 193 | + return v; |
| 194 | +} |
| 195 | + |
176 | 196 | #define stat_sizet_field(name, stype, sfield) \
|
177 | 197 | { (name), &read_common_sizet, (ptrdiff_t)offsetof(stype, sfield) }
|
178 | 198 |
|
@@ -294,14 +314,31 @@ int oscap_sys_memusage(struct sys_memusage *mu)
|
294 | 314 | if (mu == NULL)
|
295 | 315 | return -1;
|
296 | 316 | #if defined(OS_LINUX)
|
297 |
| - if (read_status(MEMUSAGE_LINUX_SYS_STATUS, |
298 |
| - mu, __sys_stat_ptable, |
299 |
| - (sizeof __sys_stat_ptable)/sizeof(struct stat_parser)) != 0) |
300 |
| - { |
301 |
| - return -1; |
| 317 | + // cgroup |
| 318 | + size_t cgroup_memory_usage = get_sys_value(MEMUSAGE_LINUX_SYS_CGROUP_USAGE); |
| 319 | + size_t cgroup_memory_limit = get_sys_value(MEMUSAGE_LINUX_SYS_CGROUP_LIMIT); |
| 320 | + if (cgroup_memory_usage != (size_t)-1 && cgroup_memory_limit != (size_t)-1) { |
| 321 | + mu->mu_total = BYTES_TO_KIB(cgroup_memory_limit); |
| 322 | + mu->mu_realfree = BYTES_TO_KIB(cgroup_memory_limit) - BYTES_TO_KIB(cgroup_memory_usage); |
| 323 | + } else { |
| 324 | + // cgroup2 |
| 325 | + size_t cgroup_memory_current = get_sys_value(MEMUSAGE_LINUX_SYS_CGROUP2_CURRENT); |
| 326 | + size_t cgroup_memory_max = get_sys_value(MEMUSAGE_LINUX_SYS_CGROUP2_MAX); |
| 327 | + if (cgroup_memory_current != (size_t)-1 && cgroup_memory_max != (size_t)-1) { |
| 328 | + mu->mu_total = BYTES_TO_KIB(cgroup_memory_max); |
| 329 | + mu->mu_realfree = BYTES_TO_KIB(cgroup_memory_max) - BYTES_TO_KIB(cgroup_memory_current); |
| 330 | + } else { |
| 331 | + if (read_status(MEMUSAGE_LINUX_SYS_STATUS, |
| 332 | + mu, __sys_stat_ptable, |
| 333 | + (sizeof __sys_stat_ptable)/sizeof(struct stat_parser)) != 0) |
| 334 | + { |
| 335 | + return -1; |
| 336 | + } |
| 337 | + |
| 338 | + mu->mu_realfree = mu->mu_free + mu->mu_cached + mu->mu_buffers; |
| 339 | + } |
302 | 340 | }
|
303 | 341 |
|
304 |
| - mu->mu_realfree = mu->mu_free + mu->mu_cached + mu->mu_buffers; |
305 | 342 | #elif defined(OS_FREEBSD)
|
306 | 343 | if (freebsd_sys_memusage(mu))
|
307 | 344 | return -1;
|
|
0 commit comments