Changeset 3621 in MondoRescue for branches/3.3/mindi-busybox/procps/free.c
- Timestamp:
- Dec 20, 2016, 4:07:32 PM (7 years ago)
- Location:
- branches/3.3
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/3.3/mindi-busybox/procps/free.c
r3232 r3621 23 23 24 24 #include "libbb.h" 25 #include "common_bufsiz.h" 25 26 #ifdef __linux__ 26 27 # include <sys/sysinfo.h> … … 36 37 #endif 37 38 } FIX_ALIASING; 38 #define G (*(struct globals*) &bb_common_bufsiz1)39 #define INIT_G() do { } while (0)39 #define G (*(struct globals*)bb_common_bufsiz1) 40 #define INIT_G() do { setup_common_bufsiz(); } while (0) 40 41 41 42 … … 45 46 } 46 47 48 static unsigned long parse_cached_kb(void) 49 { 50 char buf[60]; /* actual lines we expect are ~30 chars or less */ 51 FILE *fp; 52 unsigned long cached = 0; 53 54 fp = xfopen_for_read("/proc/meminfo"); 55 while (fgets(buf, sizeof(buf), fp) != NULL) { 56 if (sscanf(buf, "Cached: %lu %*s\n", &cached) == 1) 57 break; 58 } 59 if (ENABLE_FEATURE_CLEAN_UP) 60 fclose(fp); 61 62 return cached; 63 } 47 64 48 65 int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; … … 50 67 { 51 68 struct sysinfo info; 69 unsigned long long cached; 52 70 53 71 INIT_G(); … … 74 92 } 75 93 #endif 76 77 sysinfo(&info); 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", 94 printf(" %11s%11s%11s%11s%11s%11s\n" 95 "Mem: ", 83 96 "total", 84 97 "used", 85 98 "free", 86 "shared", "buffers" /* 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 */ 99 "shared", "buffers", "cached" /* swap and total don't have these columns */ 91 100 ); 92 101 93 #define FIELDS_5 "%13llu%13llu%13llu%13llu%13llu\n" 94 #define FIELDS_3 (FIELDS_5 + 2*6) 95 #define FIELDS_2 (FIELDS_5 + 3*6) 102 sysinfo(&info); 103 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */ 104 G.mem_unit = (info.mem_unit ? info.mem_unit : 1); 105 /* Extract cached from /proc/meminfo and convert to mem_units */ 106 cached = ((unsigned long long) parse_cached_kb() * 1024) / G.mem_unit; 96 107 97 printf("Mem: "); 98 printf(FIELDS_5, 99 scale(info.totalram), 100 scale(info.totalram - info.freeram), 101 scale(info.freeram), 102 scale(info.sharedram), 103 scale(info.bufferram) 108 #define FIELDS_6 "%11llu%11llu%11llu%11llu%11llu%11llu\n" 109 #define FIELDS_3 (FIELDS_6 + 3*6) 110 #define FIELDS_2 (FIELDS_6 + 4*6) 111 112 printf(FIELDS_6, 113 scale(info.totalram), //total 114 scale(info.totalram - info.freeram), //used 115 scale(info.freeram), //free 116 scale(info.sharedram), //shared 117 scale(info.bufferram), //buffers 118 scale(cached) //cached 104 119 ); 105 120 /* Show alternate, more meaningful busy/free numbers by counting 106 * buffer cache as free memory (make it "-/+ buffers/cache" 107 * if/when we add support for "cached" column): */ 108 printf("-/+ buffers: "); 121 * buffer cache as free memory. */ 122 printf("-/+ buffers/cache:"); 123 cached += info.freeram; 124 cached += info.bufferram; 109 125 printf(FIELDS_2, 110 scale(info.totalram - info.freeram - info.bufferram),111 scale( info.freeram + info.bufferram)126 scale(info.totalram - cached), //used 127 scale(cached) //free 112 128 ); 113 129 #if BB_MMU 114 printf("Swap: ");130 printf("Swap: "); 115 131 printf(FIELDS_3, 116 scale(info.totalswap), 117 scale(info.totalswap - info.freeswap), 118 scale(info.freeswap) 132 scale(info.totalswap), //total 133 scale(info.totalswap - info.freeswap), //used 134 scale(info.freeswap) //free 119 135 ); 120 136 #endif
Note:
See TracChangeset
for help on using the changeset viewer.