Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/procps/free.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/procps/free.c
r1765 r2725 5 5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> 6 6 * 7 * Licensed under the GPL version 2, see the file LICENSE in this tarball.7 * Licensed under GPLv2, see file LICENSE in this source tree. 8 8 */ 9 9 … … 12 12 #include "libbb.h" 13 13 14 int free_main(int argc, char **argv); 15 int free_main(int argc, char **argv) 14 struct 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 27 static unsigned long long scale(unsigned long d) 28 { 29 return ((unsigned long long)d * G.mem_unit) >> G_unit_steps; 30 } 31 32 33 int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 34 int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) 16 35 { 17 36 struct sysinfo info; 37 38 INIT_G(); 39 40 #if ENABLE_DESKTOP 41 G.unit_steps = 10; 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 } 60 #endif 61 18 62 sysinfo(&info); 19 63 20 64 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */ 21 if (info.mem_unit == 0) { 22 info.mem_unit=1; 23 } 24 if (info.mem_unit == 1) { 25 info.mem_unit=1024; 65 G.mem_unit = (info.mem_unit ? info.mem_unit : 1); 26 66 27 /* TODO: Make all this stuff not overflow when mem >= 4 Gib */ 28 info.totalram/=info.mem_unit; 29 info.freeram/=info.mem_unit; 30 #ifndef __uClinux__ 31 info.totalswap/=info.mem_unit; 32 info.freeswap/=info.mem_unit; 33 #endif 34 info.sharedram/=info.mem_unit; 35 info.bufferram/=info.mem_unit; 36 } else { 37 info.mem_unit/=1024; 38 /* TODO: Make all this stuff not overflow when mem >= 4 Gib */ 39 info.totalram*=info.mem_unit; 40 info.freeram*=info.mem_unit; 41 #ifndef __uClinux__ 42 info.totalswap*=info.mem_unit; 43 info.freeswap*=info.mem_unit; 44 #endif 45 info.sharedram*=info.mem_unit; 46 info.bufferram*=info.mem_unit; 47 } 67 printf(" %13s%13s%13s%13s%13s\n", 68 "total", 69 "used", 70 "free", 71 "shared", "buffers" /* swap and total don't have these columns */ 72 /* procps version 3.2.8 also shows "cached" column, but 73 * sysinfo() does not provide this value, need to parse 74 * /proc/meminfo instead and get "Cached: NNN kB" from there. 75 */ 76 ); 48 77 49 if (argc > 1 && *argv[1] == '-') 50 bb_show_usage(); 78 #define FIELDS_5 "%13llu%13llu%13llu%13llu%13llu\n" 79 #define FIELDS_3 (FIELDS_5 + 2*6) 80 #define FIELDS_2 (FIELDS_5 + 3*6) 51 81 52 printf("%6s%13s%13s%13s%13s%13s\n", "", "total", "used", "free", 53 "shared", "buffers"); 54 55 printf("%6s%13ld%13ld%13ld%13ld%13ld\n", "Mem:", info.totalram, 56 info.totalram-info.freeram, info.freeram, 57 info.sharedram, info.bufferram); 58 59 #ifndef __uClinux__ 60 printf("%6s%13ld%13ld%13ld\n", "Swap:", info.totalswap, 61 info.totalswap-info.freeswap, info.freeswap); 62 63 printf("%6s%13ld%13ld%13ld\n", "Total:", info.totalram+info.totalswap, 64 (info.totalram-info.freeram)+(info.totalswap-info.freeswap), 65 info.freeram+info.freeswap); 82 printf("Mem: "); 83 printf(FIELDS_5, 84 scale(info.totalram), 85 scale(info.totalram - info.freeram), 86 scale(info.freeram), 87 scale(info.sharedram), 88 scale(info.bufferram) 89 ); 90 /* Show alternate, more meaningful busy/free numbers by counting 91 * buffer cache as free memory (make it "-/+ buffers/cache" 92 * if/when we add support for "cached" column): */ 93 printf("-/+ buffers: "); 94 printf(FIELDS_2, 95 scale(info.totalram - info.freeram - info.bufferram), 96 scale(info.freeram + info.bufferram) 97 ); 98 #if BB_MMU 99 printf("Swap:"); 100 printf(FIELDS_3, 101 scale(info.totalswap), 102 scale(info.totalswap - info.freeswap), 103 scale(info.freeswap) 104 ); 66 105 #endif 67 106 return EXIT_SUCCESS;
Note:
See TracChangeset
for help on using the changeset viewer.