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 |
| 22 | //usage: "" IF_DESKTOP("[-b/k/m/g]") |
| 23 | //usage:#define free_full_usage "\n\n" |
| 24 | //usage: "Display the amount of free and used system memory" |
| 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" |
| 32 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 33 | #include "libbb.h" |
Denys Vlasenko | 67905e2 | 2011-07-26 13:42:12 +0200 | [diff] [blame] | 34 | #ifdef __linux__ |
| 35 | # include <sys/sysinfo.h> |
| 36 | #endif |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 37 | |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 38 | struct globals { |
| 39 | unsigned mem_unit; |
| 40 | #if ENABLE_DESKTOP |
Denys Vlasenko | 318c811 | 2017-10-05 14:06:49 +0200 | [diff] [blame] | 41 | uint8_t unit_steps; |
| 42 | # define G_unit_steps g->unit_steps |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 43 | #else |
| 44 | # define G_unit_steps 10 |
| 45 | #endif |
Lukas Rusak | 9663bbd | 2019-06-25 18:52:33 +0200 | [diff] [blame] | 46 | unsigned long cached_kb, available_kb, reclaimable_kb; |
Denys Vlasenko | 318c811 | 2017-10-05 14:06:49 +0200 | [diff] [blame] | 47 | }; |
| 48 | /* Because of NOFORK, "globals" are not in global data */ |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 49 | |
Denys Vlasenko | 318c811 | 2017-10-05 14:06:49 +0200 | [diff] [blame] | 50 | static unsigned long long scale(struct globals *g, unsigned long d) |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 51 | { |
Denys Vlasenko | 318c811 | 2017-10-05 14:06:49 +0200 | [diff] [blame] | 52 | return ((unsigned long long)d * g->mem_unit) >> G_unit_steps; |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Denys Vlasenko | 318c811 | 2017-10-05 14:06:49 +0200 | [diff] [blame] | 55 | /* 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] | 56 | static NOINLINE unsigned int parse_meminfo(struct globals *g) |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 57 | { |
| 58 | char buf[60]; /* actual lines we expect are ~30 chars or less */ |
| 59 | FILE *fp; |
Lukas Rusak | 9663bbd | 2019-06-25 18:52:33 +0200 | [diff] [blame] | 60 | int seen_cached_and_available_and_reclaimable; |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 61 | |
| 62 | fp = xfopen_for_read("/proc/meminfo"); |
Lukas Rusak | 9663bbd | 2019-06-25 18:52:33 +0200 | [diff] [blame] | 63 | g->cached_kb = g->available_kb = g->reclaimable_kb = 0; |
| 64 | seen_cached_and_available_and_reclaimable = 3; |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 65 | while (fgets(buf, sizeof(buf), fp)) { |
Lukas Rusak | 9663bbd | 2019-06-25 18:52:33 +0200 | [diff] [blame] | 66 | if (sscanf(buf, "Cached: %lu %*s\n", &g->cached_kb) == 1) |
| 67 | if (--seen_cached_and_available_and_reclaimable == 0) |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 68 | break; |
Lukas Rusak | 9663bbd | 2019-06-25 18:52:33 +0200 | [diff] [blame] | 69 | if (sscanf(buf, "MemAvailable: %lu %*s\n", &g->available_kb) == 1) |
| 70 | if (--seen_cached_and_available_and_reclaimable == 0) |
| 71 | break; |
| 72 | if (sscanf(buf, "SReclaimable: %lu %*s\n", &g->reclaimable_kb) == 1) |
| 73 | if (--seen_cached_and_available_and_reclaimable == 0) |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 74 | break; |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 75 | } |
Denys Vlasenko | 318c811 | 2017-10-05 14:06:49 +0200 | [diff] [blame] | 76 | /* Have to close because of NOFORK */ |
| 77 | fclose(fp); |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 78 | |
Lukas Rusak | 9663bbd | 2019-06-25 18:52:33 +0200 | [diff] [blame] | 79 | return seen_cached_and_available_and_reclaimable == 0; |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 80 | } |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 81 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 82 | int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | 7e7728c | 2010-02-26 09:28:30 +0100 | [diff] [blame] | 83 | 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] | 84 | { |
Denys Vlasenko | 318c811 | 2017-10-05 14:06:49 +0200 | [diff] [blame] | 85 | struct globals G; |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 86 | struct sysinfo info; |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 87 | unsigned long long cached, cached_plus_free, available; |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 88 | int seen_available; |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 89 | |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 90 | #if ENABLE_DESKTOP |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 91 | G.unit_steps = 10; |
| 92 | if (argv[1] && argv[1][0] == '-') { |
| 93 | switch (argv[1][1]) { |
| 94 | case 'b': |
| 95 | G.unit_steps = 0; |
| 96 | break; |
| 97 | case 'k': /* 2^10 */ |
| 98 | /* G.unit_steps = 10; - already is */ |
| 99 | break; |
Denys Vlasenko | fc63549 | 2020-11-27 12:40:31 +0100 | [diff] [blame^] | 100 | case 'm': /* 2^20 */ |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 101 | G.unit_steps = 20; |
| 102 | break; |
Denys Vlasenko | fc63549 | 2020-11-27 12:40:31 +0100 | [diff] [blame^] | 103 | case 'g': /* 2^30 */ |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 104 | G.unit_steps = 30; |
| 105 | break; |
| 106 | default: |
| 107 | bb_show_usage(); |
| 108 | } |
| 109 | } |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 110 | #endif |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 111 | printf(" %12s%12s%12s%12s%12s%12s\n" |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 112 | "Mem: ", |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 113 | "total", |
| 114 | "used", |
| 115 | "free", |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 116 | "shared", "buff/cache", "available" /* swap and total don't have these columns */ |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 117 | ); |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 118 | |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 119 | sysinfo(&info); |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 120 | /* Extract cached and memavailable from /proc/meminfo and convert to mem_units */ |
Lukas Rusak | 9663bbd | 2019-06-25 18:52:33 +0200 | [diff] [blame] | 121 | seen_available = parse_meminfo(&G); |
Denys Vlasenko | fc63549 | 2020-11-27 12:40:31 +0100 | [diff] [blame^] | 122 | 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] | 123 | available = ((unsigned long long) G.available_kb * 1024) / G.mem_unit; |
| 124 | cached = ((unsigned long long) G.cached_kb * 1024) / G.mem_unit; |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 125 | cached += info.bufferram; |
Lukas Rusak | 9663bbd | 2019-06-25 18:52:33 +0200 | [diff] [blame] | 126 | cached += ((unsigned long long) G.reclaimable_kb * 1024) / G.mem_unit; |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 127 | cached_plus_free = cached + info.freeram; |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 128 | |
Denys Vlasenko | fc63549 | 2020-11-27 12:40:31 +0100 | [diff] [blame^] | 129 | /* In case (long long * G.mem_unit) can overflow, this can be used to reduce the chances */ |
| 130 | #if 0 //ENABLE_DESKTOP |
| 131 | while (!(G.mem_unit & 1) && G.unit_steps != 0) { |
| 132 | G.mem_unit >>= 1; |
| 133 | G.unit_steps--; |
| 134 | //bb_error_msg("mem_unit:%d unit_steps:%d", G.mem_unit, G.unit_steps); |
| 135 | } |
| 136 | #endif |
| 137 | |
Denys Vlasenko | f4709d7 | 2018-11-27 15:43:10 +0100 | [diff] [blame] | 138 | #define FIELDS_6 "%12llu %11llu %11llu %11llu %11llu %11llu\n" |
| 139 | #define FIELDS_3 (FIELDS_6 + 6 + 7 + 7) |
| 140 | #define FIELDS_2 (FIELDS_6 + 6 + 7 + 7 + 7) |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 141 | |
| 142 | printf(FIELDS_6, |
Denys Vlasenko | 318c811 | 2017-10-05 14:06:49 +0200 | [diff] [blame] | 143 | scale(&G, info.totalram), //total |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 144 | scale(&G, info.totalram - cached_plus_free), //used |
Denys Vlasenko | 318c811 | 2017-10-05 14:06:49 +0200 | [diff] [blame] | 145 | scale(&G, info.freeram), //free |
| 146 | scale(&G, info.sharedram), //shared |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 147 | scale(&G, cached), //buff/cache |
| 148 | scale(&G, available) //available |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 149 | ); |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 150 | /* On kernels < 3.14, MemAvailable is not provided. |
| 151 | * Show alternate, more meaningful busy/free numbers by counting |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 152 | * buffer cache as free memory. */ |
Guillermo Rodriguez | c8e3922 | 2018-10-30 13:49:51 +0100 | [diff] [blame] | 153 | if (!seen_available) { |
| 154 | printf("-/+ buffers/cache: "); |
| 155 | printf(FIELDS_2, |
| 156 | scale(&G, info.totalram - cached_plus_free), //used |
| 157 | scale(&G, cached_plus_free) //free |
| 158 | ); |
| 159 | } |
Denys Vlasenko | 153fcaa | 2010-02-21 05:17:41 +0100 | [diff] [blame] | 160 | #if BB_MMU |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 161 | printf("Swap: "); |
Denys Vlasenko | 7bfbbd4 | 2010-08-12 01:56:44 +0200 | [diff] [blame] | 162 | printf(FIELDS_3, |
Denys Vlasenko | 318c811 | 2017-10-05 14:06:49 +0200 | [diff] [blame] | 163 | scale(&G, info.totalswap), //total |
| 164 | scale(&G, info.totalswap - info.freeswap), //used |
| 165 | scale(&G, info.freeswap) //free |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 166 | ); |
Eric Andersen | 1dcf218 | 2003-01-11 20:40:49 +0000 | [diff] [blame] | 167 | #endif |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 168 | return EXIT_SUCCESS; |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 169 | } |