From: Barry Song <[email protected]>

"-/+ buffers/cache" is much more useful than "-/+ buffers". page cache is
the main source reclaimed for new allocation. so showing the cache is more
important to users than only showing buffers.

this patch reads cache information from /proc/meminfo and make free command
full.

Signed-off-by: Barry Song <[email protected]>
---
 procps/free.c |   54 ++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 40 insertions(+), 14 deletions(-)

diff --git a/procps/free.c b/procps/free.c
index 47f2fc3..0b6a518 100644
--- a/procps/free.c
+++ b/procps/free.c
@@ -16,8 +16,8 @@
 //usage:
 //usage:#define free_example_usage
 //usage:       "$ free\n"
-//usage:       "              total         used         free       shared     
 buffers\n"
-//usage:       "  Mem:       257628       248724         8904        59644     
   93124\n"
+//usage:       "              total         used         free       shared     
 buffers    cached\n"
+//usage:       "  Mem:       257628       248724         8904        59644     
   93124    102400\n"
 //usage:       " Swap:       128516         8404       120112\n"
 //usage:       "Total:       386144       257128       129016\n"
 
@@ -44,11 +44,35 @@ static unsigned long long scale(unsigned long d)
        return ((unsigned long long)d * G.mem_unit) >> G_unit_steps;
 }
 
+static unsigned long get_cache_from_meminfo(void)
+{
+#define LINE_LEN 256
+       FILE *fp;
+       char str[LINE_LEN];
+       unsigned long long cached;
+       if((fp = fopen("/proc/meminfo","rt")) == NULL) {
+               printf("Cantnot open /proc/meminfo");
+               exit(1);
+       }
+
+       while(1) {
+               fgets(str, LINE_LEN, fp);
+               if (!strncmp(str, "Cached:", strlen("Cached:")))
+                       break;
+       }
+
+       cached = atoi(str + strlen("Cached:"));
+
+       fclose(fp);
+
+       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();
 
@@ -75,43 +99,45 @@ int free_main(int argc UNUSED_PARAM, char **argv 
IF_NOT_DESKTOP(UNUSED_PARAM))
 #endif
 
        sysinfo(&info);
+       cached = get_cache_from_meminfo();
 
        /* 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",
+       printf("            %13s%13s%13s%13s%13s%13s\n",
                "total",
                "used",
                "free",
-               "shared", "buffers" /* swap and total don't have these columns 
*/
+               "shared", "buffers", "cached" /* 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.
                 */
        );
 
-#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 "%13llu%13llu%13llu%13llu%13llu%13llu\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:      ");
+       printf("-/+ buffers/cached:      ");
        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



Member of the CSR plc group of companies. CSR plc registered in England and 
Wales, registered number 4187346, registered office Churchill House, Cambridge 
Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom
More information can be found at www.csr.com. Follow CSR on Twitter at 
http://twitter.com/CSR_PLC and read our blog at www.csr.com/blog
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to