From 9663bbd17ba3ab9f7921d7c46f07d177cb4a1435 Mon Sep 17 00:00:00 2001 From: Lukas Rusak Date: Tue, 25 Jun 2019 18:52:33 +0200 Subject: [PATCH] free: include SReclaimable in cached value I noticed that the 'used' values from busybox free and procps-ng free differed so I looked into why. It turns out that procps-ng uses the "SReclaimable" value as part of the cached value. This was changed in procps-ng commit 05d751c4f076a2f0118b914c5e51cfbb4762ad8e function old new delta free_main 633 653 +20 Signed-off-by: Lukas Rusak Signed-off-by: Denys Vlasenko --- procps/free.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/procps/free.c b/procps/free.c index 3acfc4072..1e5d36742 100644 --- a/procps/free.c +++ b/procps/free.c @@ -44,6 +44,7 @@ struct globals { #else # define G_unit_steps 10 #endif + unsigned long cached_kb, available_kb, reclaimable_kb; }; /* Because of NOFORK, "globals" are not in global data */ @@ -53,27 +54,30 @@ static unsigned long long scale(struct globals *g, unsigned long d) } /* NOINLINE reduces main() stack usage, which makes code smaller (on x86 at least) */ -static NOINLINE unsigned int parse_meminfo(unsigned long *cached_kb, unsigned long *available_kb) +static NOINLINE unsigned int parse_meminfo(struct globals *g) { char buf[60]; /* actual lines we expect are ~30 chars or less */ FILE *fp; - int seen_cached_and_available; + int seen_cached_and_available_and_reclaimable; fp = xfopen_for_read("/proc/meminfo"); - *cached_kb = *available_kb = 0; - seen_cached_and_available = 2; + g->cached_kb = g->available_kb = g->reclaimable_kb = 0; + seen_cached_and_available_and_reclaimable = 3; while (fgets(buf, sizeof(buf), fp)) { - if (sscanf(buf, "Cached: %lu %*s\n", cached_kb) == 1) - if (--seen_cached_and_available == 0) + if (sscanf(buf, "Cached: %lu %*s\n", &g->cached_kb) == 1) + if (--seen_cached_and_available_and_reclaimable == 0) break; - if (sscanf(buf, "MemAvailable: %lu %*s\n", available_kb) == 1) - if (--seen_cached_and_available == 0) + if (sscanf(buf, "MemAvailable: %lu %*s\n", &g->available_kb) == 1) + if (--seen_cached_and_available_and_reclaimable == 0) + break; + if (sscanf(buf, "SReclaimable: %lu %*s\n", &g->reclaimable_kb) == 1) + if (--seen_cached_and_available_and_reclaimable == 0) break; } /* Have to close because of NOFORK */ fclose(fp); - return seen_cached_and_available == 0; + return seen_cached_and_available_and_reclaimable == 0; } int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; @@ -82,7 +86,6 @@ int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) struct globals G; struct sysinfo info; unsigned long long cached, cached_plus_free, available; - unsigned long cached_kb, available_kb; int seen_available; #if ENABLE_DESKTOP @@ -118,10 +121,11 @@ int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */ G.mem_unit = (info.mem_unit ? info.mem_unit : 1); /* Extract cached and memavailable from /proc/meminfo and convert to mem_units */ - seen_available = parse_meminfo(&cached_kb, &available_kb); - available = ((unsigned long long) available_kb * 1024) / G.mem_unit; - cached = ((unsigned long long) cached_kb * 1024) / G.mem_unit; + seen_available = parse_meminfo(&G); + available = ((unsigned long long) G.available_kb * 1024) / G.mem_unit; + cached = ((unsigned long long) G.cached_kb * 1024) / G.mem_unit; cached += info.bufferram; + cached += ((unsigned long long) G.reclaimable_kb * 1024) / G.mem_unit; cached_plus_free = cached + info.freeram; #define FIELDS_6 "%12llu %11llu %11llu %11llu %11llu %11llu\n"