[PATCH] free: include SReclaimable in cached value

Lukas Rusak lorusak at gmail.com
Mon Jun 10 20:35:35 UTC 2019


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

---
 procps/free.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/procps/free.c b/procps/free.c
index 3acfc4072..cf2e81a60 100644
--- a/procps/free.c
+++ b/procps/free.c
@@ -53,27 +53,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(unsigned long *cached_kb, unsigned long *available_kb, unsigned long *reclaimable_kb)
 {
 	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;
+	*cached_kb = *available_kb = *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 (--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 (--seen_cached_and_available_and_reclaimable == 0)
+				break;
+		if (sscanf(buf, "SReclaimable: %lu %*s\n", 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 +85,7 @@ 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;
+	unsigned long cached_kb, available_kb, reclaimable_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);
+	seen_available = parse_meminfo(&cached_kb, &available_kb, &reclaimable_kb);
 	available = ((unsigned long long) available_kb * 1024) / G.mem_unit;
 	cached = ((unsigned long long) cached_kb * 1024) / G.mem_unit;
 	cached += info.bufferram;
+	cached += ((unsigned long long) reclaimable_kb * 1024) / G.mem_unit;
 	cached_plus_free = cached + info.freeram;
 
 #define FIELDS_6 "%12llu %11llu %11llu %11llu %11llu %11llu\n"
-- 
2.21.0



More information about the busybox mailing list