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 | */ |
| 9 | |
Mark Whitley | 827e45c | 2001-03-09 23:59:51 +0000 | [diff] [blame] | 10 | /* getopt not needed */ |
| 11 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 24 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame^] | 25 | #include "common_bufsiz.h" |
Denys Vlasenko | 67905e2 | 2011-07-26 13:42:12 +0200 | [diff] [blame] | 26 | #ifdef __linux__ |
| 27 | # include <sys/sysinfo.h> |
| 28 | #endif |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 29 | |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 30 | struct globals { |
| 31 | unsigned mem_unit; |
| 32 | #if ENABLE_DESKTOP |
| 33 | unsigned unit_steps; |
| 34 | # define G_unit_steps G.unit_steps |
| 35 | #else |
| 36 | # define G_unit_steps 10 |
| 37 | #endif |
Denys Vlasenko | e8d0a14 | 2011-01-16 11:21:15 +0100 | [diff] [blame] | 38 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame^] | 39 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 40 | #define INIT_G() do { } while (0) |
| 41 | |
| 42 | |
| 43 | static unsigned long long scale(unsigned long d) |
| 44 | { |
| 45 | return ((unsigned long long)d * G.mem_unit) >> G_unit_steps; |
| 46 | } |
| 47 | |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 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 | } |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 64 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 65 | int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | 7e7728c | 2010-02-26 09:28:30 +0100 | [diff] [blame] | 66 | 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] | 67 | { |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 68 | struct sysinfo info; |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 69 | unsigned long long cached; |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 70 | |
| 71 | INIT_G(); |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 72 | |
| 73 | #if ENABLE_DESKTOP |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 74 | G.unit_steps = 10; |
| 75 | if (argv[1] && argv[1][0] == '-') { |
| 76 | switch (argv[1][1]) { |
| 77 | case 'b': |
| 78 | G.unit_steps = 0; |
| 79 | break; |
| 80 | case 'k': /* 2^10 */ |
| 81 | /* G.unit_steps = 10; - already is */ |
| 82 | break; |
| 83 | case 'm': /* 2^(2*10) */ |
| 84 | G.unit_steps = 20; |
| 85 | break; |
| 86 | case 'g': /* 2^(3*10) */ |
| 87 | G.unit_steps = 30; |
| 88 | break; |
| 89 | default: |
| 90 | bb_show_usage(); |
| 91 | } |
| 92 | } |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 93 | #endif |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 94 | printf(" %11s%11s%11s%11s%11s%11s\n" |
| 95 | "Mem: ", |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 96 | "total", |
| 97 | "used", |
| 98 | "free", |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 99 | "shared", "buffers", "cached" /* swap and total don't have these columns */ |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 100 | ); |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 101 | |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 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; |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 107 | |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 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 |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 119 | ); |
Denys Vlasenko | 7bfbbd4 | 2010-08-12 01:56:44 +0200 | [diff] [blame] | 120 | /* Show alternate, more meaningful busy/free numbers by counting |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 121 | * buffer cache as free memory. */ |
| 122 | printf("-/+ buffers/cache:"); |
| 123 | cached += info.freeram; |
| 124 | cached += info.bufferram; |
Denys Vlasenko | 7bfbbd4 | 2010-08-12 01:56:44 +0200 | [diff] [blame] | 125 | printf(FIELDS_2, |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 126 | scale(info.totalram - cached), //used |
| 127 | scale(cached) //free |
Denys Vlasenko | 7bfbbd4 | 2010-08-12 01:56:44 +0200 | [diff] [blame] | 128 | ); |
Denys Vlasenko | 153fcaa | 2010-02-21 05:17:41 +0100 | [diff] [blame] | 129 | #if BB_MMU |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 130 | printf("Swap: "); |
Denys Vlasenko | 7bfbbd4 | 2010-08-12 01:56:44 +0200 | [diff] [blame] | 131 | printf(FIELDS_3, |
Guillermo Rodriguez | 75a1403 | 2015-01-05 18:34:53 +0100 | [diff] [blame] | 132 | scale(info.totalswap), //total |
| 133 | scale(info.totalswap - info.freeswap), //used |
| 134 | scale(info.freeswap) //free |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 135 | ); |
Eric Andersen | 1dcf218 | 2003-01-11 20:40:49 +0000 | [diff] [blame] | 136 | #endif |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 137 | return EXIT_SUCCESS; |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 138 | } |