aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-10-01 21:57:59 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2010-10-01 21:57:59 +0200
commit5542934d65016e5eb29ed56f5cdea090e137b321 (patch)
tree92227b5e26964674e94027c4fa6a6a6537c771d3
parentee5ab8f321f7060c5720b50b1834f273e00f5859 (diff)
downloadbusybox-w32-5542934d65016e5eb29ed56f5cdea090e137b321.tar.gz
busybox-w32-5542934d65016e5eb29ed56f5cdea090e137b321.tar.bz2
busybox-w32-5542934d65016e5eb29ed56f5cdea090e137b321.zip
free: add -b/k/m/g options; remove 4 TB limitation. +100 bytes
Based on patch by Stefan Tomanek (stefan@pico.ruhr.de) Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--procps/free.c98
1 files changed, 56 insertions, 42 deletions
diff --git a/procps/free.c b/procps/free.c
index be65f46f1..efbac5ba6 100644
--- a/procps/free.c
+++ b/procps/free.c
@@ -11,47 +11,58 @@
11 11
12#include "libbb.h" 12#include "libbb.h"
13 13
14struct globals {
15 unsigned mem_unit;
16#if ENABLE_DESKTOP
17 unsigned unit_steps;
18# define G_unit_steps G.unit_steps
19#else
20# define G_unit_steps 10
21#endif
22};
23#define G (*(struct globals*)&bb_common_bufsiz1)
24#define INIT_G() do { } while (0)
25
26
27static unsigned long long scale(unsigned long d)
28{
29 return ((unsigned long long)d * G.mem_unit) >> G_unit_steps;
30}
31
32
14int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 33int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
15int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) 34int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
16{ 35{
17 struct sysinfo info; 36 struct sysinfo info;
18 unsigned mem_unit; 37
38 INIT_G();
19 39
20#if ENABLE_DESKTOP 40#if ENABLE_DESKTOP
21 if (argv[1] && argv[1][0] == '-') 41 G.unit_steps = 10;
22 bb_show_usage(); 42 if (argv[1] && argv[1][0] == '-') {
43 switch (argv[1][1]) {
44 case 'b':
45 G.unit_steps = 0;
46 break;
47 case 'k': /* 2^10 */
48 /* G.unit_steps = 10; - already is */
49 break;
50 case 'm': /* 2^(2*10) */
51 G.unit_steps = 20;
52 break;
53 case 'g': /* 2^(3*10) */
54 G.unit_steps = 30;
55 break;
56 default:
57 bb_show_usage();
58 }
59 }
23#endif 60#endif
24 61
25 sysinfo(&info); 62 sysinfo(&info);
26 63
27 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */ 64 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
28 mem_unit = 1; 65 G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
29 if (info.mem_unit != 0) {
30 mem_unit = info.mem_unit;
31 }
32
33 /* Convert values to kbytes */
34 if (mem_unit == 1) {
35 info.totalram >>= 10;
36 info.freeram >>= 10;
37#if BB_MMU
38 info.totalswap >>= 10;
39 info.freeswap >>= 10;
40#endif
41 info.sharedram >>= 10;
42 info.bufferram >>= 10;
43 } else {
44 mem_unit >>= 10;
45 /* TODO: Make all this stuff not overflow when mem >= 4 Tb */
46 info.totalram *= mem_unit;
47 info.freeram *= mem_unit;
48#if BB_MMU
49 info.totalswap *= mem_unit;
50 info.freeswap *= mem_unit;
51#endif
52 info.sharedram *= mem_unit;
53 info.bufferram *= mem_unit;
54 }
55 66
56 printf(" %13s%13s%13s%13s%13s\n", 67 printf(" %13s%13s%13s%13s%13s\n",
57 "total", 68 "total",
@@ -63,30 +74,33 @@ int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
63 * /proc/meminfo instead and get "Cached: NNN kB" from there. 74 * /proc/meminfo instead and get "Cached: NNN kB" from there.
64 */ 75 */
65 ); 76 );
66#define FIELDS_5 "%13lu%13lu%13lu%13lu%13lu\n" 77
67#define FIELDS_3 (FIELDS_5 + 2*5) 78#define FIELDS_5 "%13llu%13llu%13llu%13llu%13llu\n"
68#define FIELDS_2 (FIELDS_5 + 3*5) 79#define FIELDS_3 (FIELDS_5 + 2*6)
80#define FIELDS_2 (FIELDS_5 + 3*6)
81
69 printf("Mem: "); 82 printf("Mem: ");
70 printf(FIELDS_5, 83 printf(FIELDS_5,
71 info.totalram, 84 scale(info.totalram),
72 info.totalram - info.freeram, 85 scale(info.totalram - info.freeram),
73 info.freeram, 86 scale(info.freeram),
74 info.sharedram, info.bufferram 87 scale(info.sharedram),
88 scale(info.bufferram)
75 ); 89 );
76 /* Show alternate, more meaningful busy/free numbers by counting 90 /* Show alternate, more meaningful busy/free numbers by counting
77 * buffer cache as free memory (make it "-/+ buffers/cache" 91 * buffer cache as free memory (make it "-/+ buffers/cache"
78 * if/when we add support for "cached" column): */ 92 * if/when we add support for "cached" column): */
79 printf("-/+ buffers: "); 93 printf("-/+ buffers: ");
80 printf(FIELDS_2, 94 printf(FIELDS_2,
81 info.totalram - info.freeram - info.bufferram, 95 scale(info.totalram - info.freeram - info.bufferram),
82 info.freeram + info.bufferram 96 scale(info.freeram + info.bufferram)
83 ); 97 );
84#if BB_MMU 98#if BB_MMU
85 printf("Swap:"); 99 printf("Swap:");
86 printf(FIELDS_3, 100 printf(FIELDS_3,
87 info.totalswap, 101 scale(info.totalswap),
88 info.totalswap - info.freeswap, 102 scale(info.totalswap - info.freeswap),
89 info.freeswap 103 scale(info.freeswap)
90 ); 104 );
91#endif 105#endif
92 return EXIT_SUCCESS; 106 return EXIT_SUCCESS;