From b4665ce0c3b5bd6d2ceca843b52bd70ae5fb639a Mon Sep 17 00:00:00 2001 From: Serge Hallyn Date: Sun, 21 Aug 2016 15:05:31 -0500 Subject: [PATCH 04/24] meminfo: don't show negative swapfree Also commonize some of the mem{,sw} free/used calculations. Closes #115 --- bindings.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bindings.c b/bindings.c index 2fb4acf..9bac10a 100644 --- a/bindings.c +++ b/bindings.c @@ -3155,8 +3155,10 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset, snprintf(lbuf, 100, "SwapTotal: %8lu kB\n", memswlimit - memlimit); printme = lbuf; } else if (startswith(line, "SwapFree:") && memswlimit > 0 && memswusage > 0) { - snprintf(lbuf, 100, "SwapFree: %8lu kB\n", - (memswlimit - memlimit) - (memswusage - memusage)); + unsigned long swaptotal = memswlimit - memlimit, + swapusage = memswusage - memusage, + swapfree = swapusage < swaptotal ? swaptotal - swapusage : 0; + snprintf(lbuf, 100, "SwapFree: %8lu kB\n", swapfree); printme = lbuf; } else if (startswith(line, "Slab:")) { snprintf(lbuf, 100, "Slab: %8lu kB\n", 0UL); @@ -3472,6 +3474,8 @@ static int proc_stat_read(char *buf, size_t size, off_t offset, char cpu_char[10]; /* That's a lot of cores */ char *c; + if (strlen(line) == 0) + continue; if (sscanf(line, "cpu%9[^ ]", cpu_char) != 1) { /* not a ^cpuN line containing a number N, just print it */ l = snprintf(cache, cache_size, "%s", line); -- 2.9.3