1 /* vi: set sw=4 ts=4: */
3 * Mini free implementation for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2, see file LICENSE in this source tree.
10 /* getopt not needed */
18 # define G_unit_steps G.unit_steps
20 # define G_unit_steps 10
23 #define G (*(struct globals*)&bb_common_bufsiz1)
24 #define INIT_G() do { } while (0)
27 static unsigned long long scale(unsigned long d)
29 return ((unsigned long long)d * G.mem_unit) >> G_unit_steps;
33 int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
34 int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
42 if (argv[1] && argv[1][0] == '-') {
48 /* G.unit_steps = 10; - already is */
50 case 'm': /* 2^(2*10) */
53 case 'g': /* 2^(3*10) */
64 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
65 G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
67 printf(" %13s%13s%13s%13s%13s\n",
71 "shared", "buffers" /* swap and total don't have these columns */
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.
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)
85 scale(info.totalram - info.freeram),
87 scale(info.sharedram),
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: ");
95 scale(info.totalram - info.freeram - info.bufferram),
96 scale(info.freeram + info.bufferram)
101 scale(info.totalswap),
102 scale(info.totalswap - info.freeswap),