[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH] free: Add cached memory information
From:       "Guillermo Rodriguez" <guille.rodriguez () gmail ! com>
Date:       2014-12-17 10:53:17
Message-ID: 1418813597-21977-1-git-send-email-guille.rodriguez () gmail ! com
[Download RAW message or body]

Hello all,

This is a small patch adding cached memory information to the free
command, just like the procps version. This information is parsed
from /proc/meminfo (see the parse_cached_kb function).

Without this, the amount of free memory is very misleading.

Any comments welcome.

Guillermo Rodriguez

---
 procps/free.c |   51 +++++++++++++++++++++++++++++++++------------------
 1 files changed, 33 insertions(+), 18 deletions(-)

diff --git a/procps/free.c b/procps/free.c
index 47f2fc3..0991713 100644
--- a/procps/free.c
+++ b/procps/free.c
@@ -44,11 +44,27 @@ static unsigned long long scale(unsigned long d)
 	return ((unsigned long long)d * G.mem_unit) >> G_unit_steps;
 }
 
+static unsigned long parse_cached_kb(void)
+{
+	char buf[60]; /* actual lines we expect are ~30 chars or less */
+	FILE *f;
+	unsigned long cached = 0;
+
+	f = xfopen_for_read("/proc/meminfo");
+	while (fgets(buf, sizeof(buf), f) != NULL) {
+		if (sscanf(buf, "Cached: %lu %*s\n", &cached) == 1)
+			break;
+	}
+	fclose(f);
+
+	return cached;
+}
 
 int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
 {
 	struct sysinfo info;
+	unsigned long long cached;
 
 	INIT_G();
 
@@ -79,39 +95,38 @@ 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);
 
-	printf("     %13s%13s%13s%13s%13s\n",
+	/* Extract cached from /proc/meminfo and convert to mem_units */
+	cached = ((unsigned long long) parse_cached_kb() * 1024) / G.mem_unit;
+
+	printf("       %11s%11s%11s%11s%11s%11s\n",
 		"total",
 		"used",
 		"free",
-		"shared", "buffers" /* swap and total don't have these columns */
-		/* procps version 3.2.8 also shows "cached" column, but
-		 * sysinfo() does not provide this value, need to parse
-		 * /proc/meminfo instead and get "Cached: NNN kB" from there.
-		 */
+		"shared", "buffers", "cached" /* swap and total don't have these columns */
 	);
 
-#define FIELDS_5 "%13llu%13llu%13llu%13llu%13llu\n"
-#define FIELDS_3 (FIELDS_5 + 2*6)
-#define FIELDS_2 (FIELDS_5 + 3*6)
+#define FIELDS_6 "%11llu%11llu%11llu%11llu%11llu%11llu\n"
+#define FIELDS_3 (FIELDS_6 + 3*6)
+#define FIELDS_2 (FIELDS_6 + 4*6)
 
-	printf("Mem: ");
-	printf(FIELDS_5,
+	printf("Mem:   ");
+	printf(FIELDS_6,
 		scale(info.totalram),
 		scale(info.totalram - info.freeram),
 		scale(info.freeram),
 		scale(info.sharedram),
-		scale(info.bufferram)
+		scale(info.bufferram),
+		scale(cached)
 	);
 	/* Show alternate, more meaningful busy/free numbers by counting
-	 * buffer cache as free memory (make it "-/+ buffers/cache"
-	 * if/when we add support for "cached" column): */
-	printf("-/+ buffers:      ");
+	 * buffer cache as free memory. */
+	printf("-/+ buffers/cache:");
 	printf(FIELDS_2,
-		scale(info.totalram - info.freeram - info.bufferram),
-		scale(info.freeram + info.bufferram)
+		scale(info.totalram - info.freeram - info.bufferram - cached),
+		scale(info.freeram + info.bufferram + cached)
 	);
 #if BB_MMU
-	printf("Swap:");
+	printf("Swap:  ");
 	printf(FIELDS_3,
 		scale(info.totalswap),
 		scale(info.totalswap - info.freeswap),
-- 
1.7.0.4

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic