lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini free implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
| 6 | * |
| 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
| 8 | */ |
| 9 | |
| 10 | /* getopt not needed */ |
| 11 | |
| 12 | //usage:#define free_trivial_usage |
| 13 | //usage: "" IF_DESKTOP("[-b/k/m/g]") |
| 14 | //usage:#define free_full_usage "\n\n" |
| 15 | //usage: "Display the amount of free and used system memory" |
| 16 | //usage: |
| 17 | //usage:#define free_example_usage |
| 18 | //usage: "$ free\n" |
| 19 | //usage: " total used free shared buffers\n" |
| 20 | //usage: " Mem: 257628 248724 8904 59644 93124\n" |
| 21 | //usage: " Swap: 128516 8404 120112\n" |
| 22 | //usage: "Total: 386144 257128 129016\n" |
| 23 | |
| 24 | #include "libbb.h" |
| 25 | #ifdef __linux__ |
| 26 | # include <sys/sysinfo.h> |
| 27 | #endif |
| 28 | |
| 29 | struct globals { |
| 30 | unsigned mem_unit; |
| 31 | #if ENABLE_DESKTOP |
| 32 | uint8_t unit_steps; |
| 33 | # define G_unit_steps g->unit_steps |
| 34 | #else |
| 35 | # define G_unit_steps 10 |
| 36 | #endif |
| 37 | }; |
| 38 | /* Because of NOFORK, "globals" are not in global data */ |
| 39 | |
| 40 | static unsigned long long scale(struct globals *g, unsigned long d) |
| 41 | { |
| 42 | return ((unsigned long long)d * g->mem_unit) >> G_unit_steps; |
| 43 | } |
| 44 | |
| 45 | /* NOINLINE reduces main() stack usage, which makes code smaller (on x86 at least) */ |
| 46 | static NOINLINE unsigned long parse_cached_kb(void) |
| 47 | { |
| 48 | char buf[60]; /* actual lines we expect are ~30 chars or less */ |
| 49 | FILE *fp; |
| 50 | unsigned long cached = 0; |
| 51 | |
| 52 | fp = xfopen_for_read("/proc/meminfo"); |
| 53 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 54 | if (sscanf(buf, "Cached: %lu %*s\n", &cached) == 1) |
| 55 | break; |
| 56 | } |
| 57 | /* Have to close because of NOFORK */ |
| 58 | fclose(fp); |
| 59 | |
| 60 | return cached; |
| 61 | } |
| 62 | |
| 63 | int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 64 | int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) |
| 65 | { |
| 66 | struct globals G; |
| 67 | struct sysinfo info; |
| 68 | unsigned long long cached; |
| 69 | |
| 70 | #if ENABLE_DESKTOP |
| 71 | G.unit_steps = 10; |
| 72 | if (argv[1] && argv[1][0] == '-') { |
| 73 | switch (argv[1][1]) { |
| 74 | case 'b': |
| 75 | G.unit_steps = 0; |
| 76 | break; |
| 77 | case 'k': /* 2^10 */ |
| 78 | /* G.unit_steps = 10; - already is */ |
| 79 | break; |
| 80 | case 'm': /* 2^(2*10) */ |
| 81 | G.unit_steps = 20; |
| 82 | break; |
| 83 | case 'g': /* 2^(3*10) */ |
| 84 | G.unit_steps = 30; |
| 85 | break; |
| 86 | default: |
| 87 | bb_show_usage(); |
| 88 | } |
| 89 | } |
| 90 | #endif |
| 91 | printf(" %11s%11s%11s%11s%11s%11s\n" |
| 92 | "Mem: ", |
| 93 | "total", |
| 94 | "used", |
| 95 | "free", |
| 96 | "shared", "buffers", "cached" /* swap and total don't have these columns */ |
| 97 | ); |
| 98 | |
| 99 | sysinfo(&info); |
| 100 | /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */ |
| 101 | G.mem_unit = (info.mem_unit ? info.mem_unit : 1); |
| 102 | /* Extract cached from /proc/meminfo and convert to mem_units */ |
| 103 | cached = ((unsigned long long) parse_cached_kb() * 1024) / G.mem_unit; |
| 104 | |
| 105 | #define FIELDS_6 "%11llu%11llu%11llu%11llu%11llu%11llu\n" |
| 106 | #define FIELDS_3 (FIELDS_6 + 3*6) |
| 107 | #define FIELDS_2 (FIELDS_6 + 4*6) |
| 108 | |
| 109 | printf(FIELDS_6, |
| 110 | scale(&G, info.totalram), //total |
| 111 | scale(&G, info.totalram - info.freeram), //used |
| 112 | scale(&G, info.freeram), //free |
| 113 | scale(&G, info.sharedram), //shared |
| 114 | scale(&G, info.bufferram), //buffers |
| 115 | scale(&G, cached) //cached |
| 116 | ); |
| 117 | /* Show alternate, more meaningful busy/free numbers by counting |
| 118 | * buffer cache as free memory. */ |
| 119 | printf("-/+ buffers/cache:"); |
| 120 | cached += info.freeram; |
| 121 | cached += info.bufferram; |
| 122 | printf(FIELDS_2, |
| 123 | scale(&G, info.totalram - cached), //used |
| 124 | scale(&G, cached) //free |
| 125 | ); |
| 126 | #if BB_MMU |
| 127 | printf("Swap: "); |
| 128 | printf(FIELDS_3, |
| 129 | scale(&G, info.totalswap), //total |
| 130 | scale(&G, info.totalswap - info.freeswap), //used |
| 131 | scale(&G, info.freeswap) //free |
| 132 | ); |
| 133 | #endif |
| 134 | return EXIT_SUCCESS; |
| 135 | } |