blob: 0b68e1b88abcb40df4d34293c705dc2ddc3d800c [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenabc0f4f1999-12-08 23:19:36 +00002/*
3 * Mini free implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenabc0f4f1999-12-08 23:19:36 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersenabc0f4f1999-12-08 23:19:36 +00008 */
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +01009//config:config FREE
Denys Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "free (3.1 kb)"
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010011//config: default y
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010012//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: free displays the total amount of free and used physical and swap
14//config: memory in the system, as well as the buffers used by the kernel.
15//config: The shared memory column should be ignored; it is obsolete.
Eric Andersenabc0f4f1999-12-08 23:19:36 +000016
Denys Vlasenko318c8112017-10-05 14:06:49 +020017//applet:IF_FREE(APPLET_NOFORK(free, free, BB_DIR_USR_BIN, BB_SUID_DROP, free))
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010018
19//kbuild:lib-$(CONFIG_FREE) += free.o
Mark Whitley827e45c2001-03-09 23:59:51 +000020
Pere Orga5bc8c002011-04-11 03:29:49 +020021//usage:#define free_trivial_usage
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +020022//usage: "" IF_DESKTOP("[-bkmgh]")
Pere Orga5bc8c002011-04-11 03:29:49 +020023//usage:#define free_full_usage "\n\n"
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +020024//usage: "Display free and used memory"
Pere Orga5bc8c002011-04-11 03:29:49 +020025//usage:
26//usage:#define free_example_usage
27//usage: "$ free\n"
28//usage: " total used free shared buffers\n"
29//usage: " Mem: 257628 248724 8904 59644 93124\n"
30//usage: " Swap: 128516 8404 120112\n"
31//usage: "Total: 386144 257128 129016\n"
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +020032//procps-ng 3.3.15:
33// -b, --bytes show output in bytes
34// --kilo show output in kilobytes
35// --mega show output in megabytes
36// --giga show output in gigabytes
37// --tera show output in terabytes
38// --peta show output in petabytes
39// -k, --kibi show output in kibibytes
40// -m, --mebi show output in mebibytes
41// -g, --gibi show output in gibibytes
42// --tebi show output in tebibytes
43// --pebi show output in pebibytes
44// -h, --human show human-readable output
45// --si use powers of 1000 not 1024
46// -l, --lohi show detailed low and high memory statistics
47// -t, --total show total for RAM + swap
48// -s N, --seconds N repeat printing every N seconds
49// -c N, --count N repeat printing N times, then exit
50// -w, --wide wide output
51//
52//NB: if we implement -s or -c, need to stop being NOFORK!
Pere Orga5bc8c002011-04-11 03:29:49 +020053
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000054#include "libbb.h"
Denys Vlasenko67905e22011-07-26 13:42:12 +020055#ifdef __linux__
56# include <sys/sysinfo.h>
57#endif
Eric Andersenabc0f4f1999-12-08 23:19:36 +000058
Denys Vlasenko55429342010-10-01 21:57:59 +020059struct globals {
60 unsigned mem_unit;
61#if ENABLE_DESKTOP
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +020062 unsigned unit;
63# define G_unit g->unit
Denys Vlasenko55429342010-10-01 21:57:59 +020064#else
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +020065# define G_unit (1 << 10)
Denys Vlasenko55429342010-10-01 21:57:59 +020066#endif
Lukas Rusak9663bbd2019-06-25 18:52:33 +020067 unsigned long cached_kb, available_kb, reclaimable_kb;
Denys Vlasenko318c8112017-10-05 14:06:49 +020068};
69/* Because of NOFORK, "globals" are not in global data */
Denys Vlasenko55429342010-10-01 21:57:59 +020070
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +020071static const char *scale(struct globals *g, unsigned long d)
Denys Vlasenko55429342010-10-01 21:57:59 +020072{
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +020073 /* Display (size * block_size) with one decimal digit.
74 * If display_unit == 0, show value no bigger than 1024 with suffix (K,M,G...),
75 * else divide by display_unit and do not use suffix.
76 * Returns "auto pointer" */
77 return make_human_readable_str(d, g->mem_unit, G_unit);
Denys Vlasenko55429342010-10-01 21:57:59 +020078}
79
Denys Vlasenko318c8112017-10-05 14:06:49 +020080/* NOINLINE reduces main() stack usage, which makes code smaller (on x86 at least) */
Lukas Rusak9663bbd2019-06-25 18:52:33 +020081static NOINLINE unsigned int parse_meminfo(struct globals *g)
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010082{
83 char buf[60]; /* actual lines we expect are ~30 chars or less */
84 FILE *fp;
Lukas Rusak9663bbd2019-06-25 18:52:33 +020085 int seen_cached_and_available_and_reclaimable;
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010086
87 fp = xfopen_for_read("/proc/meminfo");
Lukas Rusak9663bbd2019-06-25 18:52:33 +020088 g->cached_kb = g->available_kb = g->reclaimable_kb = 0;
89 seen_cached_and_available_and_reclaimable = 3;
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +010090 while (fgets(buf, sizeof(buf), fp)) {
Lukas Rusak9663bbd2019-06-25 18:52:33 +020091 if (sscanf(buf, "Cached: %lu %*s\n", &g->cached_kb) == 1)
92 if (--seen_cached_and_available_and_reclaimable == 0)
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +010093 break;
Lukas Rusak9663bbd2019-06-25 18:52:33 +020094 if (sscanf(buf, "MemAvailable: %lu %*s\n", &g->available_kb) == 1)
95 if (--seen_cached_and_available_and_reclaimable == 0)
96 break;
97 if (sscanf(buf, "SReclaimable: %lu %*s\n", &g->reclaimable_kb) == 1)
98 if (--seen_cached_and_available_and_reclaimable == 0)
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +010099 break;
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100100 }
Denys Vlasenko318c8112017-10-05 14:06:49 +0200101 /* Have to close because of NOFORK */
102 fclose(fp);
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100103
Lukas Rusak9663bbd2019-06-25 18:52:33 +0200104 return seen_cached_and_available_and_reclaimable == 0;
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100105}
Denys Vlasenko55429342010-10-01 21:57:59 +0200106
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000107int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischer7e7728c2010-02-26 09:28:30 +0100108int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000109{
Denys Vlasenko318c8112017-10-05 14:06:49 +0200110 struct globals G;
Erik Andersend07ee462000-02-21 21:26:32 +0000111 struct sysinfo info;
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100112 unsigned long long cached, cached_plus_free, available;
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100113 int seen_available;
Denys Vlasenko55429342010-10-01 21:57:59 +0200114
Denys Vlasenkobef57112010-02-21 05:39:59 +0100115#if ENABLE_DESKTOP
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +0200116 G.unit = 1 << 10;
Denys Vlasenko55429342010-10-01 21:57:59 +0200117 if (argv[1] && argv[1][0] == '-') {
118 switch (argv[1][1]) {
119 case 'b':
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +0200120 G.unit = 1;
Denys Vlasenko55429342010-10-01 21:57:59 +0200121 break;
122 case 'k': /* 2^10 */
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +0200123 /* G.unit = 1 << 10; - already is */
Denys Vlasenko55429342010-10-01 21:57:59 +0200124 break;
Denys Vlasenkofc635492020-11-27 12:40:31 +0100125 case 'm': /* 2^20 */
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +0200126 G.unit = 1 << 20;
Denys Vlasenko55429342010-10-01 21:57:59 +0200127 break;
Denys Vlasenkofc635492020-11-27 12:40:31 +0100128 case 'g': /* 2^30 */
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +0200129 G.unit = 1 << 30;
130 break;
131// case 't':
132// -- WRONG, -t is not "terabytes" in procps-ng, it's --total
133// G.unit = 1 << 40;
134// break;
135 case 'h':
136 G.unit = 0; /* human readable */
Denys Vlasenko55429342010-10-01 21:57:59 +0200137 break;
138 default:
139 bb_show_usage();
140 }
141 }
Denys Vlasenkobef57112010-02-21 05:39:59 +0100142#endif
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100143 printf(" %12s%12s%12s%12s%12s%12s\n"
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100144 "Mem: ",
Denys Vlasenkobef57112010-02-21 05:39:59 +0100145 "total",
146 "used",
147 "free",
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100148 "shared", "buff/cache", "available" /* swap and total don't have these columns */
Denys Vlasenkobef57112010-02-21 05:39:59 +0100149 );
Denys Vlasenko55429342010-10-01 21:57:59 +0200150
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100151 sysinfo(&info);
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100152 /* Extract cached and memavailable from /proc/meminfo and convert to mem_units */
Lukas Rusak9663bbd2019-06-25 18:52:33 +0200153 seen_available = parse_meminfo(&G);
Denys Vlasenkofc635492020-11-27 12:40:31 +0100154 G.mem_unit = (info.mem_unit ? info.mem_unit : 1); /* kernels < 2.4.x return mem_unit==0, so cope */
Lukas Rusak9663bbd2019-06-25 18:52:33 +0200155 available = ((unsigned long long) G.available_kb * 1024) / G.mem_unit;
156 cached = ((unsigned long long) G.cached_kb * 1024) / G.mem_unit;
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100157 cached += info.bufferram;
Lukas Rusak9663bbd2019-06-25 18:52:33 +0200158 cached += ((unsigned long long) G.reclaimable_kb * 1024) / G.mem_unit;
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100159 cached_plus_free = cached + info.freeram;
Denys Vlasenko55429342010-10-01 21:57:59 +0200160
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +0200161 printf("%12s%12s%12s",
Denys Vlasenko318c8112017-10-05 14:06:49 +0200162 scale(&G, info.totalram), //total
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100163 scale(&G, info.totalram - cached_plus_free), //used
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +0200164 scale(&G, info.freeram) //free
165 );
166 /* using two printf's: only 4 auto strings are supported, we need 6 */
167 printf("%12s%12s%12s\n",
Denys Vlasenko318c8112017-10-05 14:06:49 +0200168 scale(&G, info.sharedram), //shared
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100169 scale(&G, cached), //buff/cache
170 scale(&G, available) //available
Denys Vlasenkobef57112010-02-21 05:39:59 +0100171 );
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100172 /* On kernels < 3.14, MemAvailable is not provided.
173 * Show alternate, more meaningful busy/free numbers by counting
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100174 * buffer cache as free memory. */
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100175 if (!seen_available) {
176 printf("-/+ buffers/cache: ");
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +0200177 printf("%12s%12s%12s\n" + 4,
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100178 scale(&G, info.totalram - cached_plus_free), //used
179 scale(&G, cached_plus_free) //free
180 );
181 }
Denys Vlasenko153fcaa2010-02-21 05:17:41 +0100182#if BB_MMU
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100183 printf("Swap: ");
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +0200184 printf("%12s%12s%12s\n",
Denys Vlasenko318c8112017-10-05 14:06:49 +0200185 scale(&G, info.totalswap), //total
186 scale(&G, info.totalswap - info.freeswap), //used
187 scale(&G, info.freeswap) //free
Denys Vlasenkobef57112010-02-21 05:39:59 +0100188 );
Eric Andersen1dcf2182003-01-11 20:40:49 +0000189#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +0000190 return EXIT_SUCCESS;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000191}