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 (3.1 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
47 unsigned long cached_kb, available_kb, reclaimable_kb;
49 /* Because of NOFORK, "globals" are not in global data */
51 static unsigned long long scale(struct globals *g, unsigned long d)
53 return ((unsigned long long)d * g->mem_unit) >> G_unit_steps;
56 /* NOINLINE reduces main() stack usage, which makes code smaller (on x86 at least) */
57 static NOINLINE unsigned int parse_meminfo(struct globals *g)
59 char buf[60]; /* actual lines we expect are ~30 chars or less */
61 int seen_cached_and_available_and_reclaimable;
63 fp = xfopen_for_read("/proc/meminfo");
64 g->cached_kb = g->available_kb = g->reclaimable_kb = 0;
65 seen_cached_and_available_and_reclaimable = 3;
66 while (fgets(buf, sizeof(buf), fp)) {
67 if (sscanf(buf, "Cached: %lu %*s\n", &g->cached_kb) == 1)
68 if (--seen_cached_and_available_and_reclaimable == 0)
70 if (sscanf(buf, "MemAvailable: %lu %*s\n", &g->available_kb) == 1)
71 if (--seen_cached_and_available_and_reclaimable == 0)
73 if (sscanf(buf, "SReclaimable: %lu %*s\n", &g->reclaimable_kb) == 1)
74 if (--seen_cached_and_available_and_reclaimable == 0)
77 /* Have to close because of NOFORK */
80 return seen_cached_and_available_and_reclaimable == 0;
83 int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
84 int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
88 unsigned long long cached, cached_plus_free, available;
93 if (argv[1] && argv[1][0] == '-') {
99 /* G.unit_steps = 10; - already is */
101 case 'm': /* 2^(2*10) */
104 case 'g': /* 2^(3*10) */
112 printf(" %12s%12s%12s%12s%12s%12s\n"
117 "shared", "buff/cache", "available" /* swap and total don't have these columns */
121 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
122 G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
123 /* Extract cached and memavailable from /proc/meminfo and convert to mem_units */
124 seen_available = parse_meminfo(&G);
125 available = ((unsigned long long) G.available_kb * 1024) / G.mem_unit;
126 cached = ((unsigned long long) G.cached_kb * 1024) / G.mem_unit;
127 cached += info.bufferram;
128 cached += ((unsigned long long) G.reclaimable_kb * 1024) / G.mem_unit;
129 cached_plus_free = cached + info.freeram;
131 #define FIELDS_6 "%12llu %11llu %11llu %11llu %11llu %11llu\n"
132 #define FIELDS_3 (FIELDS_6 + 6 + 7 + 7)
133 #define FIELDS_2 (FIELDS_6 + 6 + 7 + 7 + 7)
136 scale(&G, info.totalram), //total
137 scale(&G, info.totalram - cached_plus_free), //used
138 scale(&G, info.freeram), //free
139 scale(&G, info.sharedram), //shared
140 scale(&G, cached), //buff/cache
141 scale(&G, available) //available
143 /* On kernels < 3.14, MemAvailable is not provided.
144 * Show alternate, more meaningful busy/free numbers by counting
145 * buffer cache as free memory. */
146 if (!seen_available) {
147 printf("-/+ buffers/cache: ");
149 scale(&G, info.totalram - cached_plus_free), //used
150 scale(&G, cached_plus_free) //free
156 scale(&G, info.totalswap), //total
157 scale(&G, info.totalswap - info.freeswap), //used
158 scale(&G, info.freeswap) //free