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 //config: bool "free (2.4 kb)"
12 //config: select PLATFORM_LINUX #sysinfo()
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.
18 //applet:IF_FREE(APPLET_NOFORK(free, free, BB_DIR_USR_BIN, BB_SUID_DROP, free))
20 //kbuild:lib-$(CONFIG_FREE) += free.o
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"
27 //usage:#define free_example_usage
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"
36 # include <sys/sysinfo.h>
43 # define G_unit_steps g->unit_steps
45 # define G_unit_steps 10
48 /* Because of NOFORK, "globals" are not in global data */
50 static unsigned long long scale(struct globals *g, unsigned long d)
52 return ((unsigned long long)d * g->mem_unit) >> G_unit_steps;
55 /* NOINLINE reduces main() stack usage, which makes code smaller (on x86 at least) */
56 static NOINLINE unsigned int parse_meminfo(unsigned long *cached_kb, unsigned long *available_kb)
58 char buf[60]; /* actual lines we expect are ~30 chars or less */
60 int seen_cached_and_available;
62 fp = xfopen_for_read("/proc/meminfo");
63 *cached_kb = *available_kb = 0;
64 seen_cached_and_available = 2;
65 while (fgets(buf, sizeof(buf), fp)) {
66 if (sscanf(buf, "Cached: %lu %*s\n", cached_kb) == 1)
67 if (--seen_cached_and_available == 0)
69 if (sscanf(buf, "MemAvailable: %lu %*s\n", available_kb) == 1)
70 if (--seen_cached_and_available == 0)
73 /* Have to close because of NOFORK */
76 return seen_cached_and_available == 0;
79 int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
80 int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
84 unsigned long long cached, cached_plus_free, available;
85 unsigned long cached_kb, available_kb;
90 if (argv[1] && argv[1][0] == '-') {
96 /* G.unit_steps = 10; - already is */
98 case 'm': /* 2^(2*10) */
101 case 'g': /* 2^(3*10) */
109 printf(" %12s%12s%12s%12s%12s%12s\n"
114 "shared", "buff/cache", "available" /* swap and total don't have these columns */
118 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
119 G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
120 /* Extract cached and memavailable from /proc/meminfo and convert to mem_units */
121 seen_available = parse_meminfo(&cached_kb, &available_kb);
122 available = ((unsigned long long) available_kb * 1024) / G.mem_unit;
123 cached = ((unsigned long long) cached_kb * 1024) / G.mem_unit;
124 cached += info.bufferram;
125 cached_plus_free = cached + info.freeram;
127 #define FIELDS_6 "%12llu %11llu %11llu %11llu %11llu %11llu\n"
128 #define FIELDS_3 (FIELDS_6 + 6 + 7 + 7)
129 #define FIELDS_2 (FIELDS_6 + 6 + 7 + 7 + 7)
132 scale(&G, info.totalram), //total
133 scale(&G, info.totalram - cached_plus_free), //used
134 scale(&G, info.freeram), //free
135 scale(&G, info.sharedram), //shared
136 scale(&G, cached), //buff/cache
137 scale(&G, available) //available
139 /* On kernels < 3.14, MemAvailable is not provided.
140 * Show alternate, more meaningful busy/free numbers by counting
141 * buffer cache as free memory. */
142 if (!seen_available) {
143 printf("-/+ buffers/cache: ");
145 scale(&G, info.totalram - cached_plus_free), //used
146 scale(&G, cached_plus_free) //free
152 scale(&G, info.totalswap), //total
153 scale(&G, info.totalswap - info.freeswap), //used
154 scale(&G, info.freeswap) //free