Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini free implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 9 | //config:config FREE |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 10 | //config: bool "free (3.1 kb)" |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 11 | //config: default y |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: free displays the total amount of free and used physical and swap |
| 14 | //config: memory in the system, as well as the buffers used by the kernel. |
| 15 | //config: The shared memory column should be ignored; it is obsolete. |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 16 | |
Denys Vlasenko | 318c811 | 2017-10-05 14:06:49 +0200 | [diff] [blame] | 17 | //applet:IF_FREE(APPLET_NOFORK(free, free, BB_DIR_USR_BIN, BB_SUID_DROP, free)) |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 18 | |
| 19 | //kbuild:lib-$(CONFIG_FREE) += free.o |
Mark Whitley | 827e45c | 2001-03-09 23:59:51 +0000 | [diff] [blame] | 20 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 21 | //usage:#define free_trivial_usage |
Denys Vlasenko | dc30f3d | 2021-06-18 12:08:02 +0200 | [diff] [blame] | 22 | //usage: "" IF_DESKTOP("[-bkmgh]") |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 23 | //usage:#define free_full_usage "\n\n" |
Denys Vlasenko | dc30f3d | 2021-06-18 12:08:02 +0200 | [diff] [blame] | 24 | //usage: "Display free and used memory" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 25 | //usage: |
| 26 | //usage:#define free_example_usage |
| 27 | //usage: "$ free\n" |
| 28 | //usage: " total used free shared buffers\n" |
| 29 | //usage: " Mem: 257628 248724 8904 59644 93124\n" |
| 30 | //usage: " Swap: 128516 8404 120112\n" |
| 31 | //usage: "Total: 386144 257128 129016\n" |
Denys Vlasenko | dc30f3d | 2021-06-18 12:08:02 +0200 | [diff] [blame] | 32 | //procps-ng 3.3.15: |
| 33 | // -b, --bytes show output in bytes |
| 34 | // --kilo show output in kilobytes |
| 35 | // --mega show output in megabytes |
| 36 | // --giga show output in gigabytes |
| 37 | // --tera show output in terabytes |
| 38 | // --peta show output in petabytes |
| 39 | // -k, --kibi show output in kibibytes |
| 40 | // -m, --mebi show output in mebibytes |
| 41 | // -g, --gibi show output in gibibytes |
| 42 | // --tebi show output in tebibytes |
| 43 | // --pebi show output in pebibytes |
| 44 | // -h, --human show human-readable output |
| 45 | // --si use powers of 1000 not 1024 |
| 46 | // -l, --lohi show detailed low and high memory statistics |
| 47 | // -t, --total show total for RAM + swap |
| 48 | // -s N, --seconds N repeat printing every N seconds |
| 49 | // -c N, --count N repeat printing N times, then exit |
| 50 | // -w, --wide wide output |
| 51 | // |
| 52 | //NB: if we implement -s or -c, need to stop being NOFORK! |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 53 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 54 | #include "libbb.h" |
Denys Vlasenko | 67905e2 | 2011-07-26 13:42:12 +0200 | [diff] [blame] | 55 | #ifdef __linux__ |
| 56 | # include <sys/sysinfo.h> |
| 57 | #endif |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 58 | |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 59 | struct globals { |
| 60 | unsigned mem_unit; |
| 61 | #if ENABLE_DESKTOP |
Denys Vlasenko | dc30f3d | 2021-06-18 12:08:02 +0200 | [diff] [blame] | 62 | unsigned unit; |
| 63 | # define G_unit g->unit |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 64 | #else |
Denys Vlasenko | dc30f3d | 2021-06-18 12:08:02 +0200 | [diff] [blame] | 65 | # define G_unit (1 << 10) |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 66 | #endif |
Lukas Rusak | 9663bbd | 2019-06-25 18:52:33 +0200 | [diff] [blame] | 67 | unsigned long cached_kb, available_kb, reclaimable_kb; |
Denys Vlasenko | 318c811 | 2017-10-05 14:06:49 +0200 | [diff] [blame] | 68 | }; |
| 69 | /* Because of NOFORK, "globals" are not in global data */ |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 70 | |
Denys Vlasenko | dc30f3d | 2021-06-18 12:08:02 +0200 | [diff] [blame] | 71 | static const char *scale(struct globals *g, unsigned long d) |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 72 | { |
Denys Vlasenko | dc30f3d | 2021-06-18 12:08:02 +0200 | [diff] [blame] | 73 | /* Display (size * block_size) with one decimal digit. |
| 74 | * If display_unit == 0, show value no bigger than 1024 with suffix (K,M,G...), |
| 75 | * else divide by display_unit and do not use suffix. |
| 76 | * Returns "auto pointer" */ |
| 77 | return make_human_readable_str(d, g->mem_unit, G_unit); |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Denys Vlasenko | 318c811 | 2017-10-05 14:06:49 +0200 | [diff] [blame] | 80 | /* NOINLINE reduces main() stack usage, which makes code smaller (on x86 at least) */ |
Lukas Rusak | 9663bbd | 2019-06-25 18:52:33 +0200 | [diff] [blame] | 81 | static NOINLINE unsigned int parse_meminfo(struct globals *g) |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 82 | { |
| 83 | char buf[60]; /* actual lines we expect are ~30 chars or less */ |
| 84 | FILE *fp; |
Lukas Rusak | 9663bbd | 2019-06-25 18:52:33 +0200 | [diff] [blame] | 85 | int seen_cached_and_available_and_reclaimable; |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 86 | |
| 87 | fp = xfopen_for_read("/proc/meminfo"); |
Lukas Rusak | 9663bbd | 2019-06-25 18:52:33 +0200 | [diff] [blame] | 88 | g->cached_kb = g->available_kb = g->reclaimable_kb = 0; |
| 89 | seen_cached_and_available_and_reclaimable = 3; |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 90 | while (fgets(buf, sizeof(buf), fp)) { |
Lukas Rusak | 9663bbd | 2019-06-25 18:52:33 +0200 | [diff] [blame] | 91 | if (sscanf(buf, "Cached: %lu %*s\n", &g->cached_kb) == 1) |
| 92 | if (--seen_cached_and_available_and_reclaimable == 0) |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 93 | break; |
Lukas Rusak | 9663bbd | 2019-06-25 18:52:33 +0200 | [diff] [blame] | 94 | if (sscanf(buf, "MemAvailable: %lu %*s\n", &g->available_kb) == 1) |
| 95 | if (--seen_cached_and_available_and_reclaimable == 0) |
| 96 | break; |
| 97 | if (sscanf(buf, "SReclaimable: %lu %*s\n", &g->reclaimable_kb) == 1) |
| 98 | if (--seen_cached_and_available_and_reclaimable == 0) |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 99 | break; |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 100 | } |
Denys Vlasenko | 318c811 | 2017-10-05 14:06:49 +0200 | [diff] [blame] | 101 | /* Have to close because of NOFORK */ |
| 102 | fclose(fp); |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 103 | |
Lukas Rusak | 9663bbd | 2019-06-25 18:52:33 +0200 | [diff] [blame] | 104 | return seen_cached_and_available_and_reclaimable == 0; |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 105 | } |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 106 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 107 | int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | 7e7728c | 2010-02-26 09:28:30 +0100 | [diff] [blame] | 108 | int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 109 | { |
Denys Vlasenko | 318c811 | 2017-10-05 14:06:49 +0200 | [diff] [blame] | 110 | struct globals G; |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 111 | struct sysinfo info; |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 112 | unsigned long long cached, cached_plus_free, available; |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 113 | int seen_available; |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 114 | |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 115 | #if ENABLE_DESKTOP |
Denys Vlasenko | dc30f3d | 2021-06-18 12:08:02 +0200 | [diff] [blame] | 116 | G.unit = 1 << 10; |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 117 | if (argv[1] && argv[1][0] == '-') { |
| 118 | switch (argv[1][1]) { |
| 119 | case 'b': |
Denys Vlasenko | dc30f3d | 2021-06-18 12:08:02 +0200 | [diff] [blame] | 120 | G.unit = 1; |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 121 | break; |
| 122 | case 'k': /* 2^10 */ |
Denys Vlasenko | dc30f3d | 2021-06-18 12:08:02 +0200 | [diff] [blame] | 123 | /* G.unit = 1 << 10; - already is */ |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 124 | break; |
Denys Vlasenko | fc63549 | 2020-11-27 12:40:31 +0100 | [diff] [blame] | 125 | case 'm': /* 2^20 */ |
Denys Vlasenko | dc30f3d | 2021-06-18 12:08:02 +0200 | [diff] [blame] | 126 | G.unit = 1 << 20; |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 127 | break; |
Denys Vlasenko | fc63549 | 2020-11-27 12:40:31 +0100 | [diff] [blame] | 128 | case 'g': /* 2^30 */ |
Denys Vlasenko | dc30f3d | 2021-06-18 12:08:02 +0200 | [diff] [blame] | 129 | G.unit = 1 << 30; |
| 130 | break; |
| 131 | // case 't': |
| 132 | // -- WRONG, -t is not "terabytes" in procps-ng, it's --total |
| 133 | // G.unit = 1 << 40; |
| 134 | // break; |
| 135 | case 'h': |
| 136 | G.unit = 0; /* human readable */ |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 137 | break; |
| 138 | default: |
| 139 | bb_show_usage(); |
| 140 | } |
| 141 | } |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 142 | #endif |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 143 | printf(" %12s%12s%12s%12s%12s%12s\n" |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 144 | "Mem: ", |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 145 | "total", |
| 146 | "used", |
| 147 | "free", |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 148 | "shared", "buff/cache", "available" /* swap and total don't have these columns */ |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 149 | ); |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 150 | |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 151 | sysinfo(&info); |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 152 | /* Extract cached and memavailable from /proc/meminfo and convert to mem_units */ |
Lukas Rusak | 9663bbd | 2019-06-25 18:52:33 +0200 | [diff] [blame] | 153 | seen_available = parse_meminfo(&G); |
Denys Vlasenko | fc63549 | 2020-11-27 12:40:31 +0100 | [diff] [blame] | 154 | G.mem_unit = (info.mem_unit ? info.mem_unit : 1); /* kernels < 2.4.x return mem_unit==0, so cope */ |
Lukas Rusak | 9663bbd | 2019-06-25 18:52:33 +0200 | [diff] [blame] | 155 | available = ((unsigned long long) G.available_kb * 1024) / G.mem_unit; |
| 156 | cached = ((unsigned long long) G.cached_kb * 1024) / G.mem_unit; |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 157 | cached += info.bufferram; |
Lukas Rusak | 9663bbd | 2019-06-25 18:52:33 +0200 | [diff] [blame] | 158 | cached += ((unsigned long long) G.reclaimable_kb * 1024) / G.mem_unit; |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 159 | cached_plus_free = cached + info.freeram; |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 160 | |
Denys Vlasenko | dc30f3d | 2021-06-18 12:08:02 +0200 | [diff] [blame] | 161 | printf("%12s%12s%12s", |
Denys Vlasenko | 318c811 | 2017-10-05 14:06:49 +0200 | [diff] [blame] | 162 | scale(&G, info.totalram), //total |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 163 | scale(&G, info.totalram - cached_plus_free), //used |
Denys Vlasenko | dc30f3d | 2021-06-18 12:08:02 +0200 | [diff] [blame] | 164 | scale(&G, info.freeram) //free |
| 165 | ); |
| 166 | /* using two printf's: only 4 auto strings are supported, we need 6 */ |
| 167 | printf("%12s%12s%12s\n", |
Denys Vlasenko | 318c811 | 2017-10-05 14:06:49 +0200 | [diff] [blame] | 168 | scale(&G, info.sharedram), //shared |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 169 | scale(&G, cached), //buff/cache |
| 170 | scale(&G, available) //available |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 171 | ); |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 172 | /* On kernels < 3.14, MemAvailable is not provided. |
| 173 | * Show alternate, more meaningful busy/free numbers by counting |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 174 | * buffer cache as free memory. */ |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 175 | if (!seen_available) { |
| 176 | printf("-/+ buffers/cache: "); |
Denys Vlasenko | dc30f3d | 2021-06-18 12:08:02 +0200 | [diff] [blame] | 177 | printf("%12s%12s%12s\n" + 4, |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 178 | scale(&G, info.totalram - cached_plus_free), //used |
| 179 | scale(&G, cached_plus_free) //free |
| 180 | ); |
| 181 | } |
Denys Vlasenko | 153fcaa | 2010-02-21 05:17:41 +0100 | [diff] [blame] | 182 | #if BB_MMU |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 183 | printf("Swap: "); |
Denys Vlasenko | dc30f3d | 2021-06-18 12:08:02 +0200 | [diff] [blame] | 184 | printf("%12s%12s%12s\n", |
Denys Vlasenko | 318c811 | 2017-10-05 14:06:49 +0200 | [diff] [blame] | 185 | scale(&G, info.totalswap), //total |
| 186 | scale(&G, info.totalswap - info.freeswap), //used |
| 187 | scale(&G, info.freeswap) //free |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 188 | ); |
Eric Andersen | 1dcf218 | 2003-01-11 20:40:49 +0000 | [diff] [blame] | 189 | #endif |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 190 | return EXIT_SUCCESS; |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 191 | } |