aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillermo Rodriguez <guille.rodriguez@gmail.com>2015-01-05 18:34:53 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2015-01-05 18:34:53 +0100
commit75a1403f266098bc4a498140473e1007075f459d (patch)
tree60f53a0ba9f980efee4989f2c143cb99518b00b9
parentda9212667c99f2f2121747c4715d067deb7c155b (diff)
downloadbusybox-w32-75a1403f266098bc4a498140473e1007075f459d.tar.gz
busybox-w32-75a1403f266098bc4a498140473e1007075f459d.tar.bz2
busybox-w32-75a1403f266098bc4a498140473e1007075f459d.zip
free: handle "cached" value too
function old new delta free_main 356 481 +125 Signed-off-by: Guillermo Rodriguez <guille.rodriguez@gmail.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--procps/free.c77
1 files changed, 46 insertions, 31 deletions
diff --git a/procps/free.c b/procps/free.c
index 47f2fc3b2..0d023f740 100644
--- a/procps/free.c
+++ b/procps/free.c
@@ -44,11 +44,28 @@ static unsigned long long scale(unsigned long d)
44 return ((unsigned long long)d * G.mem_unit) >> G_unit_steps; 44 return ((unsigned long long)d * G.mem_unit) >> G_unit_steps;
45} 45}
46 46
47static unsigned long parse_cached_kb(void)
48{
49 char buf[60]; /* actual lines we expect are ~30 chars or less */
50 FILE *fp;
51 unsigned long cached = 0;
52
53 fp = xfopen_for_read("/proc/meminfo");
54 while (fgets(buf, sizeof(buf), fp) != NULL) {
55 if (sscanf(buf, "Cached: %lu %*s\n", &cached) == 1)
56 break;
57 }
58 if (ENABLE_FEATURE_CLEAN_UP)
59 fclose(fp);
60
61 return cached;
62}
47 63
48int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 64int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
49int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) 65int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
50{ 66{
51 struct sysinfo info; 67 struct sysinfo info;
68 unsigned long long cached;
52 69
53 INIT_G(); 70 INIT_G();
54 71
@@ -73,49 +90,47 @@ int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
73 } 90 }
74 } 91 }
75#endif 92#endif
76 93 printf(" %11s%11s%11s%11s%11s%11s\n"
77 sysinfo(&info); 94 "Mem: ",
78
79 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
80 G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
81
82 printf(" %13s%13s%13s%13s%13s\n",
83 "total", 95 "total",
84 "used", 96 "used",
85 "free", 97 "free",
86 "shared", "buffers" /* swap and total don't have these columns */ 98 "shared", "buffers", "cached" /* swap and total don't have these columns */
87 /* procps version 3.2.8 also shows "cached" column, but
88 * sysinfo() does not provide this value, need to parse
89 * /proc/meminfo instead and get "Cached: NNN kB" from there.
90 */
91 ); 99 );
92 100
93#define FIELDS_5 "%13llu%13llu%13llu%13llu%13llu\n" 101 sysinfo(&info);
94#define FIELDS_3 (FIELDS_5 + 2*6) 102 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
95#define FIELDS_2 (FIELDS_5 + 3*6) 103 G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
104 /* Extract cached from /proc/meminfo and convert to mem_units */
105 cached = ((unsigned long long) parse_cached_kb() * 1024) / G.mem_unit;
106
107#define FIELDS_6 "%11llu%11llu%11llu%11llu%11llu%11llu\n"
108#define FIELDS_3 (FIELDS_6 + 3*6)
109#define FIELDS_2 (FIELDS_6 + 4*6)
96 110
97 printf("Mem: "); 111 printf(FIELDS_6,
98 printf(FIELDS_5, 112 scale(info.totalram), //total
99 scale(info.totalram), 113 scale(info.totalram - info.freeram), //used
100 scale(info.totalram - info.freeram), 114 scale(info.freeram), //free
101 scale(info.freeram), 115 scale(info.sharedram), //shared
102 scale(info.sharedram), 116 scale(info.bufferram), //buffers
103 scale(info.bufferram) 117 scale(cached) //cached
104 ); 118 );
105 /* Show alternate, more meaningful busy/free numbers by counting 119 /* Show alternate, more meaningful busy/free numbers by counting
106 * buffer cache as free memory (make it "-/+ buffers/cache" 120 * buffer cache as free memory. */
107 * if/when we add support for "cached" column): */ 121 printf("-/+ buffers/cache:");
108 printf("-/+ buffers: "); 122 cached += info.freeram;
123 cached += info.bufferram;
109 printf(FIELDS_2, 124 printf(FIELDS_2,
110 scale(info.totalram - info.freeram - info.bufferram), 125 scale(info.totalram - cached), //used
111 scale(info.freeram + info.bufferram) 126 scale(cached) //free
112 ); 127 );
113#if BB_MMU 128#if BB_MMU
114 printf("Swap:"); 129 printf("Swap: ");
115 printf(FIELDS_3, 130 printf(FIELDS_3,
116 scale(info.totalswap), 131 scale(info.totalswap), //total
117 scale(info.totalswap - info.freeswap), 132 scale(info.totalswap - info.freeswap), //used
118 scale(info.freeswap) 133 scale(info.freeswap) //free
119 ); 134 );
120#endif 135#endif
121 return EXIT_SUCCESS; 136 return EXIT_SUCCESS;