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 | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 10 | //config: bool "free (2.4 kb)" |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: select PLATFORM_LINUX #sysinfo() |
| 13 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 14 | //config: free displays the total amount of free and used physical and swap |
| 15 | //config: memory in the system, as well as the buffers used by the kernel. |
| 16 | //config: The shared memory column should be ignored; it is obsolete. |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 17 | |
Denys Vlasenko | 248a67f | 2017-08-07 18:18:09 +0200 | [diff] [blame] | 18 | //applet:IF_FREE(APPLET_NOEXEC(free, free, BB_DIR_USR_BIN, BB_SUID_DROP, free)) |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 19 | |
| 20 | //kbuild:lib-$(CONFIG_FREE) += free.o |
Mark Whitley | 827e45c | 2001-03-09 23:59:51 +0000 | [diff] [blame] | 21 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 22 | //usage:#define free_trivial_usage |
| 23 | //usage: "" IF_DESKTOP("[-b/k/m/g]") |
| 24 | //usage:#define free_full_usage "\n\n" |
| 25 | //usage: "Display the amount of free and used system memory" |
| 26 | //usage: |
| 27 | //usage:#define free_example_usage |
| 28 | //usage: "$ free\n" |
| 29 | //usage: " total used free shared buffers\n" |
| 30 | //usage: " Mem: 257628 248724 8904 59644 93124\n" |
| 31 | //usage: " Swap: 128516 8404 120112\n" |
| 32 | //usage: "Total: 386144 257128 129016\n" |
| 33 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 34 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 35 | #include "common_bufsiz.h" |
Denys Vlasenko | 67905e2 | 2011-07-26 13:42:12 +0200 | [diff] [blame] | 36 | #ifdef __linux__ |
| 37 | # include <sys/sysinfo.h> |
| 38 | #endif |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 39 | |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 40 | struct globals { |
| 41 | unsigned mem_unit; |
| 42 | #if ENABLE_DESKTOP |
| 43 | unsigned unit_steps; |
| 44 | # define G_unit_steps G.unit_steps |
| 45 | #else |
| 46 | # define G_unit_steps 10 |
| 47 | #endif |
Denys Vlasenko | e8d0a14 | 2011-01-16 11:21:15 +0100 | [diff] [blame] | 48 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 49 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denys Vlasenko | 248a67f | 2017-08-07 18:18:09 +0200 | [diff] [blame] | 50 | #define INIT_G() do { \ |
| 51 | setup_common_bufsiz(); \ |
| 52 | /* NB: noexec applet - globals not zeroed */ \ |
| 53 | } while (0) |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 54 | |
| 55 | |
| 56 | static unsigned long long scale(unsigned long d) |
| 57 | { |
| 58 | return ((unsigned long long)d * G.mem_unit) >> G_unit_steps; |
| 59 | } |
| 60 | |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 61 | static unsigned long parse_cached_kb(void) |
| 62 | { |
| 63 | char buf[60]; /* actual lines we expect are ~30 chars or less */ |
| 64 | FILE *fp; |
| 65 | unsigned long cached = 0; |
| 66 | |
| 67 | fp = xfopen_for_read("/proc/meminfo"); |
| 68 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 69 | if (sscanf(buf, "Cached: %lu %*s\n", &cached) == 1) |
| 70 | break; |
| 71 | } |
| 72 | if (ENABLE_FEATURE_CLEAN_UP) |
| 73 | fclose(fp); |
| 74 | |
| 75 | return cached; |
| 76 | } |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 77 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 78 | int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | 7e7728c | 2010-02-26 09:28:30 +0100 | [diff] [blame] | 79 | 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] | 80 | { |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 81 | struct sysinfo info; |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 82 | unsigned long long cached; |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 83 | |
| 84 | INIT_G(); |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 85 | |
| 86 | #if ENABLE_DESKTOP |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 87 | G.unit_steps = 10; |
| 88 | if (argv[1] && argv[1][0] == '-') { |
| 89 | switch (argv[1][1]) { |
| 90 | case 'b': |
| 91 | G.unit_steps = 0; |
| 92 | break; |
| 93 | case 'k': /* 2^10 */ |
| 94 | /* G.unit_steps = 10; - already is */ |
| 95 | break; |
| 96 | case 'm': /* 2^(2*10) */ |
| 97 | G.unit_steps = 20; |
| 98 | break; |
| 99 | case 'g': /* 2^(3*10) */ |
| 100 | G.unit_steps = 30; |
| 101 | break; |
| 102 | default: |
| 103 | bb_show_usage(); |
| 104 | } |
| 105 | } |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 106 | #endif |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 107 | printf(" %11s%11s%11s%11s%11s%11s\n" |
| 108 | "Mem: ", |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 109 | "total", |
| 110 | "used", |
| 111 | "free", |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 112 | "shared", "buffers", "cached" /* swap and total don't have these columns */ |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 113 | ); |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 114 | |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 115 | sysinfo(&info); |
| 116 | /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */ |
| 117 | G.mem_unit = (info.mem_unit ? info.mem_unit : 1); |
| 118 | /* Extract cached from /proc/meminfo and convert to mem_units */ |
| 119 | cached = ((unsigned long long) parse_cached_kb() * 1024) / G.mem_unit; |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 120 | |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 121 | #define FIELDS_6 "%11llu%11llu%11llu%11llu%11llu%11llu\n" |
| 122 | #define FIELDS_3 (FIELDS_6 + 3*6) |
| 123 | #define FIELDS_2 (FIELDS_6 + 4*6) |
| 124 | |
| 125 | printf(FIELDS_6, |
| 126 | scale(info.totalram), //total |
| 127 | scale(info.totalram - info.freeram), //used |
| 128 | scale(info.freeram), //free |
| 129 | scale(info.sharedram), //shared |
| 130 | scale(info.bufferram), //buffers |
| 131 | scale(cached) //cached |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 132 | ); |
Denys Vlasenko | 7bfbbd4 | 2010-08-12 01:56:44 +0200 | [diff] [blame] | 133 | /* Show alternate, more meaningful busy/free numbers by counting |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 134 | * buffer cache as free memory. */ |
| 135 | printf("-/+ buffers/cache:"); |
| 136 | cached += info.freeram; |
| 137 | cached += info.bufferram; |
Denys Vlasenko | 7bfbbd4 | 2010-08-12 01:56:44 +0200 | [diff] [blame] | 138 | printf(FIELDS_2, |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 139 | scale(info.totalram - cached), //used |
| 140 | scale(cached) //free |
Denys Vlasenko | 7bfbbd4 | 2010-08-12 01:56:44 +0200 | [diff] [blame] | 141 | ); |
Denys Vlasenko | 153fcaa | 2010-02-21 05:17:41 +0100 | [diff] [blame] | 142 | #if BB_MMU |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 143 | printf("Swap: "); |
Denys Vlasenko | 7bfbbd4 | 2010-08-12 01:56:44 +0200 | [diff] [blame] | 144 | printf(FIELDS_3, |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 145 | scale(info.totalswap), //total |
| 146 | scale(info.totalswap - info.freeswap), //used |
| 147 | scale(info.freeswap) //free |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 148 | ); |
Eric Andersen | 1dcf218 | 2003-01-11 20:40:49 +0000 | [diff] [blame] | 149 | #endif |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 150 | return EXIT_SUCCESS; |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 151 | } |