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 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 14 | int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | 7e7728c | 2010-02-26 09:28:30 +0100 | [diff] [blame] | 15 | 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] | 16 | { |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 17 | struct sysinfo info; |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 18 | unsigned mem_unit; |
| 19 | |
| 20 | #if ENABLE_DESKTOP |
| 21 | if (argv[1] && argv[1][0] == '-') |
| 22 | bb_show_usage(); |
| 23 | #endif |
| 24 | |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 25 | sysinfo(&info); |
Eric Andersen | 73de656 | 2000-09-10 16:10:41 +0000 | [diff] [blame] | 26 | |
| 27 | /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */ |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 28 | mem_unit = 1; |
| 29 | if (info.mem_unit != 0) { |
| 30 | mem_unit = info.mem_unit; |
Eric Andersen | 10dc9d4 | 2000-06-26 10:45:52 +0000 | [diff] [blame] | 31 | } |
Eric Andersen | 1dcf218 | 2003-01-11 20:40:49 +0000 | [diff] [blame] | 32 | |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 33 | /* Convert values to kbytes */ |
| 34 | if (mem_unit == 1) { |
| 35 | info.totalram >>= 10; |
| 36 | info.freeram >>= 10; |
Denys Vlasenko | 153fcaa | 2010-02-21 05:17:41 +0100 | [diff] [blame] | 37 | #if BB_MMU |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 38 | info.totalswap >>= 10; |
| 39 | info.freeswap >>= 10; |
Eric Andersen | 1dcf218 | 2003-01-11 20:40:49 +0000 | [diff] [blame] | 40 | #endif |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 41 | info.sharedram >>= 10; |
| 42 | info.bufferram >>= 10; |
Eric Andersen | 1dcf218 | 2003-01-11 20:40:49 +0000 | [diff] [blame] | 43 | } else { |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 44 | mem_unit >>= 10; |
| 45 | /* TODO: Make all this stuff not overflow when mem >= 4 Tb */ |
| 46 | info.totalram *= mem_unit; |
| 47 | info.freeram *= mem_unit; |
Denys Vlasenko | 153fcaa | 2010-02-21 05:17:41 +0100 | [diff] [blame] | 48 | #if BB_MMU |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 49 | info.totalswap *= mem_unit; |
| 50 | info.freeswap *= mem_unit; |
Eric Andersen | 1dcf218 | 2003-01-11 20:40:49 +0000 | [diff] [blame] | 51 | #endif |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 52 | info.sharedram *= mem_unit; |
| 53 | info.bufferram *= mem_unit; |
Eric Andersen | 1dcf218 | 2003-01-11 20:40:49 +0000 | [diff] [blame] | 54 | } |
Eric Andersen | 73de656 | 2000-09-10 16:10:41 +0000 | [diff] [blame] | 55 | |
Denys Vlasenko | 7bfbbd4 | 2010-08-12 01:56:44 +0200 | [diff] [blame] | 56 | printf(" %13s%13s%13s%13s%13s\n", |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 57 | "total", |
| 58 | "used", |
| 59 | "free", |
| 60 | "shared", "buffers" /* swap and total don't have these columns */ |
Denys Vlasenko | 7bfbbd4 | 2010-08-12 01:56:44 +0200 | [diff] [blame] | 61 | /* procps version 3.2.8 also shows "cached" column, but |
| 62 | * sysinfo() does not provide this value, need to parse |
| 63 | * /proc/meminfo instead and get "Cached: NNN kB" from there. |
| 64 | */ |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 65 | ); |
Denys Vlasenko | 7bfbbd4 | 2010-08-12 01:56:44 +0200 | [diff] [blame] | 66 | #define FIELDS_5 "%13lu%13lu%13lu%13lu%13lu\n" |
| 67 | #define FIELDS_3 (FIELDS_5 + 2*5) |
| 68 | #define FIELDS_2 (FIELDS_5 + 3*5) |
| 69 | printf("Mem: "); |
| 70 | printf(FIELDS_5, |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 71 | info.totalram, |
| 72 | info.totalram - info.freeram, |
| 73 | info.freeram, |
| 74 | info.sharedram, info.bufferram |
| 75 | ); |
Denys Vlasenko | 7bfbbd4 | 2010-08-12 01:56:44 +0200 | [diff] [blame] | 76 | /* Show alternate, more meaningful busy/free numbers by counting |
| 77 | * buffer cache as free memory (make it "-/+ buffers/cache" |
| 78 | * if/when we add support for "cached" column): */ |
| 79 | printf("-/+ buffers: "); |
| 80 | printf(FIELDS_2, |
| 81 | info.totalram - info.freeram - info.bufferram, |
| 82 | info.freeram + info.bufferram |
| 83 | ); |
Denys Vlasenko | 153fcaa | 2010-02-21 05:17:41 +0100 | [diff] [blame] | 84 | #if BB_MMU |
Denys Vlasenko | 7bfbbd4 | 2010-08-12 01:56:44 +0200 | [diff] [blame] | 85 | printf("Swap:"); |
| 86 | printf(FIELDS_3, |
Denys Vlasenko | bef5711 | 2010-02-21 05:39:59 +0100 | [diff] [blame] | 87 | info.totalswap, |
| 88 | info.totalswap - info.freeswap, |
| 89 | info.freeswap |
| 90 | ); |
Eric Andersen | 1dcf218 | 2003-01-11 20:40:49 +0000 | [diff] [blame] | 91 | #endif |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 92 | return EXIT_SUCCESS; |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 93 | } |