2000-02-09 01:28:47 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-12-09 04:49:36 +05:30
|
|
|
/*
|
|
|
|
* Mini free implementation for busybox
|
|
|
|
*
|
2004-03-15 13:59:22 +05:30
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
1999-12-09 04:49:36 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
1999-12-09 04:49:36 +05:30
|
|
|
*/
|
2016-11-23 10:53:44 +05:30
|
|
|
//config:config FREE
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "free (3.1 kb)"
|
2016-11-23 10:53:44 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: free displays the total amount of free and used physical and swap
|
|
|
|
//config: memory in the system, as well as the buffers used by the kernel.
|
|
|
|
//config: The shared memory column should be ignored; it is obsolete.
|
1999-12-09 04:49:36 +05:30
|
|
|
|
2017-10-05 17:36:49 +05:30
|
|
|
//applet:IF_FREE(APPLET_NOFORK(free, free, BB_DIR_USR_BIN, BB_SUID_DROP, free))
|
2016-11-23 10:53:44 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_FREE) += free.o
|
2001-03-10 05:29:51 +05:30
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define free_trivial_usage
|
2021-06-18 15:38:02 +05:30
|
|
|
//usage: "" IF_DESKTOP("[-bkmgh]")
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define free_full_usage "\n\n"
|
2021-06-18 15:38:02 +05:30
|
|
|
//usage: "Display free and used memory"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:
|
|
|
|
//usage:#define free_example_usage
|
|
|
|
//usage: "$ free\n"
|
|
|
|
//usage: " total used free shared buffers\n"
|
|
|
|
//usage: " Mem: 257628 248724 8904 59644 93124\n"
|
|
|
|
//usage: " Swap: 128516 8404 120112\n"
|
|
|
|
//usage: "Total: 386144 257128 129016\n"
|
2021-06-18 15:38:02 +05:30
|
|
|
//procps-ng 3.3.15:
|
|
|
|
// -b, --bytes show output in bytes
|
|
|
|
// --kilo show output in kilobytes
|
|
|
|
// --mega show output in megabytes
|
|
|
|
// --giga show output in gigabytes
|
|
|
|
// --tera show output in terabytes
|
|
|
|
// --peta show output in petabytes
|
|
|
|
// -k, --kibi show output in kibibytes
|
|
|
|
// -m, --mebi show output in mebibytes
|
|
|
|
// -g, --gibi show output in gibibytes
|
|
|
|
// --tebi show output in tebibytes
|
|
|
|
// --pebi show output in pebibytes
|
|
|
|
// -h, --human show human-readable output
|
|
|
|
// --si use powers of 1000 not 1024
|
|
|
|
// -l, --lohi show detailed low and high memory statistics
|
|
|
|
// -t, --total show total for RAM + swap
|
|
|
|
// -s N, --seconds N repeat printing every N seconds
|
|
|
|
// -c N, --count N repeat printing N times, then exit
|
|
|
|
// -w, --wide wide output
|
|
|
|
//
|
|
|
|
//NB: if we implement -s or -c, need to stop being NOFORK!
|
2011-04-11 06:59:49 +05:30
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2011-07-26 17:12:12 +05:30
|
|
|
#ifdef __linux__
|
|
|
|
# include <sys/sysinfo.h>
|
|
|
|
#endif
|
2000-06-26 16:15:52 +05:30
|
|
|
|
2010-10-02 01:27:59 +05:30
|
|
|
struct globals {
|
|
|
|
unsigned mem_unit;
|
|
|
|
#if ENABLE_DESKTOP
|
2021-06-18 15:38:02 +05:30
|
|
|
unsigned unit;
|
|
|
|
# define G_unit g->unit
|
2010-10-02 01:27:59 +05:30
|
|
|
#else
|
2021-06-18 15:38:02 +05:30
|
|
|
# define G_unit (1 << 10)
|
2010-10-02 01:27:59 +05:30
|
|
|
#endif
|
2019-06-25 22:22:33 +05:30
|
|
|
unsigned long cached_kb, available_kb, reclaimable_kb;
|
2017-10-05 17:36:49 +05:30
|
|
|
};
|
|
|
|
/* Because of NOFORK, "globals" are not in global data */
|
2010-10-02 01:27:59 +05:30
|
|
|
|
2021-06-18 15:38:02 +05:30
|
|
|
static const char *scale(struct globals *g, unsigned long d)
|
2010-10-02 01:27:59 +05:30
|
|
|
{
|
2021-06-18 15:38:02 +05:30
|
|
|
/* Display (size * block_size) with one decimal digit.
|
|
|
|
* If display_unit == 0, show value no bigger than 1024 with suffix (K,M,G...),
|
|
|
|
* else divide by display_unit and do not use suffix.
|
|
|
|
* Returns "auto pointer" */
|
|
|
|
return make_human_readable_str(d, g->mem_unit, G_unit);
|
2010-10-02 01:27:59 +05:30
|
|
|
}
|
|
|
|
|
2017-10-05 17:36:49 +05:30
|
|
|
/* NOINLINE reduces main() stack usage, which makes code smaller (on x86 at least) */
|
2019-06-25 22:22:33 +05:30
|
|
|
static NOINLINE unsigned int parse_meminfo(struct globals *g)
|
2015-01-05 23:04:53 +05:30
|
|
|
{
|
|
|
|
char buf[60]; /* actual lines we expect are ~30 chars or less */
|
|
|
|
FILE *fp;
|
2019-06-25 22:22:33 +05:30
|
|
|
int seen_cached_and_available_and_reclaimable;
|
2015-01-05 23:04:53 +05:30
|
|
|
|
|
|
|
fp = xfopen_for_read("/proc/meminfo");
|
2019-06-25 22:22:33 +05:30
|
|
|
g->cached_kb = g->available_kb = g->reclaimable_kb = 0;
|
|
|
|
seen_cached_and_available_and_reclaimable = 3;
|
2018-10-30 18:19:51 +05:30
|
|
|
while (fgets(buf, sizeof(buf), fp)) {
|
2019-06-25 22:22:33 +05:30
|
|
|
if (sscanf(buf, "Cached: %lu %*s\n", &g->cached_kb) == 1)
|
|
|
|
if (--seen_cached_and_available_and_reclaimable == 0)
|
2018-10-30 18:19:51 +05:30
|
|
|
break;
|
2019-06-25 22:22:33 +05:30
|
|
|
if (sscanf(buf, "MemAvailable: %lu %*s\n", &g->available_kb) == 1)
|
|
|
|
if (--seen_cached_and_available_and_reclaimable == 0)
|
|
|
|
break;
|
|
|
|
if (sscanf(buf, "SReclaimable: %lu %*s\n", &g->reclaimable_kb) == 1)
|
|
|
|
if (--seen_cached_and_available_and_reclaimable == 0)
|
2018-10-30 18:19:51 +05:30
|
|
|
break;
|
2015-01-05 23:04:53 +05:30
|
|
|
}
|
2017-10-05 17:36:49 +05:30
|
|
|
/* Have to close because of NOFORK */
|
|
|
|
fclose(fp);
|
2015-01-05 23:04:53 +05:30
|
|
|
|
2019-06-25 22:22:33 +05:30
|
|
|
return seen_cached_and_available_and_reclaimable == 0;
|
2015-01-05 23:04:53 +05:30
|
|
|
}
|
2010-10-02 01:27:59 +05:30
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2010-02-26 13:58:30 +05:30
|
|
|
int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
|
1999-12-09 04:49:36 +05:30
|
|
|
{
|
2017-10-05 17:36:49 +05:30
|
|
|
struct globals G;
|
2000-02-22 02:56:32 +05:30
|
|
|
struct sysinfo info;
|
2018-10-30 18:19:51 +05:30
|
|
|
unsigned long long cached, cached_plus_free, available;
|
|
|
|
int seen_available;
|
2010-10-02 01:27:59 +05:30
|
|
|
|
2010-02-21 10:09:59 +05:30
|
|
|
#if ENABLE_DESKTOP
|
2021-06-18 15:38:02 +05:30
|
|
|
G.unit = 1 << 10;
|
2010-10-02 01:27:59 +05:30
|
|
|
if (argv[1] && argv[1][0] == '-') {
|
|
|
|
switch (argv[1][1]) {
|
|
|
|
case 'b':
|
2021-06-18 15:38:02 +05:30
|
|
|
G.unit = 1;
|
2010-10-02 01:27:59 +05:30
|
|
|
break;
|
|
|
|
case 'k': /* 2^10 */
|
2021-06-18 15:38:02 +05:30
|
|
|
/* G.unit = 1 << 10; - already is */
|
2010-10-02 01:27:59 +05:30
|
|
|
break;
|
2020-11-27 17:10:31 +05:30
|
|
|
case 'm': /* 2^20 */
|
2021-06-18 15:38:02 +05:30
|
|
|
G.unit = 1 << 20;
|
2010-10-02 01:27:59 +05:30
|
|
|
break;
|
2020-11-27 17:10:31 +05:30
|
|
|
case 'g': /* 2^30 */
|
2021-06-18 15:38:02 +05:30
|
|
|
G.unit = 1 << 30;
|
|
|
|
break;
|
|
|
|
// case 't':
|
|
|
|
// -- WRONG, -t is not "terabytes" in procps-ng, it's --total
|
|
|
|
// G.unit = 1 << 40;
|
|
|
|
// break;
|
|
|
|
case 'h':
|
|
|
|
G.unit = 0; /* human readable */
|
2010-10-02 01:27:59 +05:30
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bb_show_usage();
|
|
|
|
}
|
|
|
|
}
|
2010-02-21 10:09:59 +05:30
|
|
|
#endif
|
2018-10-30 18:19:51 +05:30
|
|
|
printf(" %12s%12s%12s%12s%12s%12s\n"
|
2015-01-05 23:04:53 +05:30
|
|
|
"Mem: ",
|
2010-02-21 10:09:59 +05:30
|
|
|
"total",
|
|
|
|
"used",
|
|
|
|
"free",
|
2018-10-30 18:19:51 +05:30
|
|
|
"shared", "buff/cache", "available" /* swap and total don't have these columns */
|
2010-02-21 10:09:59 +05:30
|
|
|
);
|
2010-10-02 01:27:59 +05:30
|
|
|
|
2015-01-05 23:04:53 +05:30
|
|
|
sysinfo(&info);
|
2018-10-30 18:19:51 +05:30
|
|
|
/* Extract cached and memavailable from /proc/meminfo and convert to mem_units */
|
2019-06-25 22:22:33 +05:30
|
|
|
seen_available = parse_meminfo(&G);
|
2020-11-27 17:10:31 +05:30
|
|
|
G.mem_unit = (info.mem_unit ? info.mem_unit : 1); /* kernels < 2.4.x return mem_unit==0, so cope */
|
2019-06-25 22:22:33 +05:30
|
|
|
available = ((unsigned long long) G.available_kb * 1024) / G.mem_unit;
|
|
|
|
cached = ((unsigned long long) G.cached_kb * 1024) / G.mem_unit;
|
2018-10-30 18:19:51 +05:30
|
|
|
cached += info.bufferram;
|
2019-06-25 22:22:33 +05:30
|
|
|
cached += ((unsigned long long) G.reclaimable_kb * 1024) / G.mem_unit;
|
2018-10-30 18:19:51 +05:30
|
|
|
cached_plus_free = cached + info.freeram;
|
2015-01-05 23:04:53 +05:30
|
|
|
|
2021-06-18 15:38:02 +05:30
|
|
|
printf("%12s%12s%12s",
|
2017-10-05 17:36:49 +05:30
|
|
|
scale(&G, info.totalram), //total
|
2018-10-30 18:19:51 +05:30
|
|
|
scale(&G, info.totalram - cached_plus_free), //used
|
2021-06-18 15:38:02 +05:30
|
|
|
scale(&G, info.freeram) //free
|
|
|
|
);
|
|
|
|
/* using two printf's: only 4 auto strings are supported, we need 6 */
|
|
|
|
printf("%12s%12s%12s\n",
|
2017-10-05 17:36:49 +05:30
|
|
|
scale(&G, info.sharedram), //shared
|
2018-10-30 18:19:51 +05:30
|
|
|
scale(&G, cached), //buff/cache
|
|
|
|
scale(&G, available) //available
|
2010-02-21 10:09:59 +05:30
|
|
|
);
|
2018-10-30 18:19:51 +05:30
|
|
|
/* On kernels < 3.14, MemAvailable is not provided.
|
|
|
|
* Show alternate, more meaningful busy/free numbers by counting
|
2015-01-05 23:04:53 +05:30
|
|
|
* buffer cache as free memory. */
|
2018-10-30 18:19:51 +05:30
|
|
|
if (!seen_available) {
|
|
|
|
printf("-/+ buffers/cache: ");
|
2021-06-18 15:38:02 +05:30
|
|
|
printf("%12s%12s%12s\n" + 4,
|
2018-10-30 18:19:51 +05:30
|
|
|
scale(&G, info.totalram - cached_plus_free), //used
|
|
|
|
scale(&G, cached_plus_free) //free
|
|
|
|
);
|
|
|
|
}
|
2010-02-21 09:47:41 +05:30
|
|
|
#if BB_MMU
|
2015-01-05 23:04:53 +05:30
|
|
|
printf("Swap: ");
|
2021-06-18 15:38:02 +05:30
|
|
|
printf("%12s%12s%12s\n",
|
2017-10-05 17:36:49 +05:30
|
|
|
scale(&G, info.totalswap), //total
|
|
|
|
scale(&G, info.totalswap - info.freeswap), //used
|
|
|
|
scale(&G, info.freeswap) //free
|
2010-02-21 10:09:59 +05:30
|
|
|
);
|
2003-01-12 02:10:49 +05:30
|
|
|
#endif
|
2000-12-01 08:25:13 +05:30
|
|
|
return EXIT_SUCCESS;
|
1999-12-09 04:49:36 +05:30
|
|
|
}
|