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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 13 | |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 14 | struct globals { |
| 15 | unsigned mem_unit; |
| 16 | #if ENABLE_DESKTOP |
| 17 | unsigned unit_steps; |
| 18 | # define G_unit_steps G.unit_steps |
| 19 | #else |
| 20 | # define G_unit_steps 10 |
| 21 | #endif |
Denys Vlasenko | e8d0a14 | 2011-01-16 11:21:15 +0100 | [diff] [blame] | 22 | } FIX_ALIASING; |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 23 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 24 | #define INIT_G() do { } while (0) |
| 25 | |
| 26 | |
| 27 | static unsigned long long scale(unsigned long d) |
| 28 | { |
| 29 | return ((unsigned long long)d * G.mem_unit) >> G_unit_steps; |
| 30 | } |
| 31 | |
| 32 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 33 | int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | 7e7728c | 2010-02-26 09:28:30 +0100 | [diff] [blame] | 34 | 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] | 35 | { |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 36 | struct sysinfo info; |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 37 | |
| 38 | INIT_G(); |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 39 | |
| 40 | #if ENABLE_DESKTOP |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 41 | G.unit_steps = 10; |
| 42 | if (argv[1] && argv[1][0] == '-') { |
| 43 | switch (argv[1][1]) { |
| 44 | case 'b': |
| 45 | G.unit_steps = 0; |
| 46 | break; |
| 47 | case 'k': /* 2^10 */ |
| 48 | /* G.unit_steps = 10; - already is */ |
| 49 | break; |
| 50 | case 'm': /* 2^(2*10) */ |
| 51 | G.unit_steps = 20; |
| 52 | break; |
| 53 | case 'g': /* 2^(3*10) */ |
| 54 | G.unit_steps = 30; |
| 55 | break; |
| 56 | default: |
| 57 | bb_show_usage(); |
| 58 | } |
| 59 | } |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 60 | #endif |
| 61 | |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 62 | sysinfo(&info); |
Eric Andersen | 73de656 | 2000-09-10 16:10:41 +0000 | [diff] [blame] | 63 | |
| 64 | /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */ |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 65 | G.mem_unit = (info.mem_unit ? info.mem_unit : 1); |
Eric Andersen | 73de656 | 2000-09-10 16:10:41 +0000 | [diff] [blame] | 66 | |
Denys Vlasenko | 7bfbbd4 | 2010-08-12 01:56:44 +0200 | [diff] [blame] | 67 | printf(" %13s%13s%13s%13s%13s\n", |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 68 | "total", |
| 69 | "used", |
| 70 | "free", |
| 71 | "shared", "buffers" /* swap and total don't have these columns */ |
Denys Vlasenko | 7bfbbd4 | 2010-08-12 01:56:44 +0200 | [diff] [blame] | 72 | /* procps version 3.2.8 also shows "cached" column, but |
| 73 | * sysinfo() does not provide this value, need to parse |
| 74 | * /proc/meminfo instead and get "Cached: NNN kB" from there. |
| 75 | */ |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 76 | ); |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 77 | |
| 78 | #define FIELDS_5 "%13llu%13llu%13llu%13llu%13llu\n" |
| 79 | #define FIELDS_3 (FIELDS_5 + 2*6) |
| 80 | #define FIELDS_2 (FIELDS_5 + 3*6) |
| 81 | |
Denys Vlasenko | 7bfbbd4 | 2010-08-12 01:56:44 +0200 | [diff] [blame] | 82 | printf("Mem: "); |
| 83 | printf(FIELDS_5, |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 84 | scale(info.totalram), |
| 85 | scale(info.totalram - info.freeram), |
| 86 | scale(info.freeram), |
| 87 | scale(info.sharedram), |
| 88 | scale(info.bufferram) |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 89 | ); |
Denys Vlasenko | 7bfbbd4 | 2010-08-12 01:56:44 +0200 | [diff] [blame] | 90 | /* Show alternate, more meaningful busy/free numbers by counting |
| 91 | * buffer cache as free memory (make it "-/+ buffers/cache" |
| 92 | * if/when we add support for "cached" column): */ |
| 93 | printf("-/+ buffers: "); |
| 94 | printf(FIELDS_2, |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 95 | scale(info.totalram - info.freeram - info.bufferram), |
| 96 | scale(info.freeram + info.bufferram) |
Denys Vlasenko | 7bfbbd4 | 2010-08-12 01:56:44 +0200 | [diff] [blame] | 97 | ); |
Denys Vlasenko | 153fcaa | 2010-02-21 05:17:41 +0100 | [diff] [blame] | 98 | #if BB_MMU |
Denys Vlasenko | 7bfbbd4 | 2010-08-12 01:56:44 +0200 | [diff] [blame] | 99 | printf("Swap:"); |
| 100 | printf(FIELDS_3, |
Denys Vlasenko | 5542934 | 2010-10-01 21:57:59 +0200 | [diff] [blame] | 101 | scale(info.totalswap), |
| 102 | scale(info.totalswap - info.freeswap), |
| 103 | scale(info.freeswap) |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 104 | ); |
Eric Andersen | 1dcf218 | 2003-01-11 20:40:49 +0000 | [diff] [blame] | 105 | #endif |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 106 | return EXIT_SUCCESS; |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 107 | } |