让busybox里的free命令可以显示page cache

原创 2012年03月26日 21:05:14

busybox的free命令不能显示page cache,导致于其功能大打折扣,因为page cache大小很大,在用户申请内存时可以回收,打上如下补丁后,busybox的free命令变得完整:

commit 2219fd301a7f319258ad4be9217cd0d6db9240d4
Author: Barry Song <Baohua.Song@xxx.xxx>
Date:   Mon Mar 26 11:47:10 2012 +0800

    busybox: update free command to include page cache
   
    eg.
    sh-4.2$ free
                        total         used         free       shared      buffers       cached
    Mem:               402924        49232       353692            0          668        32160
    -/+ buffers/cache:              16404       386520
    Swap:                  0            0            0
   
    Change-Id: Ic9de9f8cd0051da8fd61ae8238a1d2573587223b
    Signed-off-by: Barry Song <Baohua.Song@xxx.xxx>

diff --git a/procps/free.c b/procps/free.c
index efbac5b..6abf1e8 100644
--- a/procps/free.c
+++ b/procps/free.c
@@ -29,11 +29,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();
 
@@ -60,43 +84,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),
+               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/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),



内存泄露问题分析方法

本文介绍了内存泄露的定位方法,可以帮助Android系统的开发者定位一些常见的内存泄露问题。本文暂时只从工具的角度讲解了如何定位,后面会补充如何从代码的角度去分析和定位,并且加入内存管理的优化方法。...

[Happy Coding] malloc/kmalloc/vmalloc/slab cache/__get_free_page

1. malloc/free malloc/free是C库函数,用来给用户态程序分配和释放内存,处理的内存地址是虚拟地址。能够保证虚拟地址是连续的,但不能保证物理地址是连续。glibc调用brk(sb...

从free到page cache

Free 我们经常用free查看服务器的内存使用情况,而free中的输出却有些让人困惑,如下:     图1-1 先看看各个数字的意义以及如何计算得到: free命令输出的第二...

zz : 从free到page cache

http://www.cnblogs.com/hustcat/archive/2011/10/27/2226995.html Free 我们经常用free查看服务器的内存使用情况,而f...

linux free命令中buffer与cache的区别

~$ free                     total             used           free     shared   buffers     cached ...
  • wzzushx
  • wzzushx
  • 2015年02月12日 19:52
  • 342

Linux的free命令以及cache与buffer的区别

Linux的free 命令相对于top命令提供了更简洁的查看系统内存使用情况: 或free -h: Mem:表示物理内存统计  -/+ buffers/cached:表示物理内存的缓存统计...

linux free命令中buffer和cache的区别

[root@linuxso.com ~]#free -m            total    used   free   shared    buffers   cached Mem: 503...

Linux Free命令与cache和buffer的主要区别

ree 命令相对于top 提供了更简洁的查看系统内存使用情况,用来显示内存的使用情况,使用权限是所有用户 语法 free [-b|-k|-m|-g] [-l] [-o] [-t] [-s ...

Linux命令 之 free (buffer与cache区别 )

buffer:    缓冲区,一个用于存储速度不同步的设备或优先级不同的设备之间传输数据 的区域。通过缓冲区,可以使进程之间的相互等待变少,从而使从速度慢的设备读入数据 时,速度快的...

free命令、buffer与cache的区别、交换分区swap

free命令:主要提供了简介的查看系统的内存情况;默认情况下这里的单位是kb; free命令总共输出四行: 第一行:就是参数名,不做多的解释。 第二行;输出是从OS来看的,从操作系统改的角度来看:计算...
  • gogoky
  • gogoky
  • 2013年08月16日 21:15
  • 835
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:让busybox里的free命令可以显示page cache
举报原因:
原因补充:

(最多只允许输入30个字)