2000-02-08 19:58:47 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-12-08 23:19:36 +00:00
|
|
|
/*
|
|
|
|
* Mini free implementation for busybox
|
|
|
|
*
|
2004-03-15 08:29:22 +00:00
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
1999-12-08 23:19:36 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
1999-12-08 23:19:36 +00:00
|
|
|
*/
|
2016-11-23 05:23:44 +00:00
|
|
|
//config:config FREE
|
2017-07-18 20:01:24 +00:00
|
|
|
//config: bool "free (2.4 kb)"
|
2016-11-23 05:23:44 +00:00
|
|
|
//config: default y
|
|
|
|
//config: select PLATFORM_LINUX #sysinfo()
|
|
|
|
//config: help
|
2017-07-21 07:50:55 +00:00
|
|
|
//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-08 23:19:36 +00:00
|
|
|
|
2016-11-23 05:23:44 +00:00
|
|
|
//applet:IF_FREE(APPLET(free, BB_DIR_USR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_FREE) += free.o
|
2001-03-09 23:59:51 +00:00
|
|
|
|
2011-04-11 01:29:49 +00:00
|
|
|
//usage:#define free_trivial_usage
|
|
|
|
//usage: "" IF_DESKTOP("[-b/k/m/g]")
|
|
|
|
//usage:#define free_full_usage "\n\n"
|
|
|
|
//usage: "Display the amount of free and used system memory"
|
|
|
|
//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"
|
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2016-04-21 14:26:30 +00:00
|
|
|
#include "common_bufsiz.h"
|
2011-07-26 11:42:12 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
# include <sys/sysinfo.h>
|
|
|
|
#endif
|
2000-06-26 10:45:52 +00:00
|
|
|
|
2010-10-01 19:57:59 +00:00
|
|
|
struct globals {
|
|
|
|
unsigned mem_unit;
|
|
|
|
#if ENABLE_DESKTOP
|
|
|
|
unsigned unit_steps;
|
|
|
|
# define G_unit_steps G.unit_steps
|
|
|
|
#else
|
|
|
|
# define G_unit_steps 10
|
|
|
|
#endif
|
2011-01-16 10:21:15 +00:00
|
|
|
} FIX_ALIASING;
|
2016-04-21 14:26:30 +00:00
|
|
|
#define G (*(struct globals*)bb_common_bufsiz1)
|
2016-04-21 16:18:48 +00:00
|
|
|
#define INIT_G() do { setup_common_bufsiz(); } while (0)
|
2010-10-01 19:57:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
static unsigned long long scale(unsigned long d)
|
|
|
|
{
|
|
|
|
return ((unsigned long long)d * G.mem_unit) >> G_unit_steps;
|
|
|
|
}
|
|
|
|
|
2015-01-05 17:34:53 +00:00
|
|
|
static unsigned long parse_cached_kb(void)
|
|
|
|
{
|
|
|
|
char buf[60]; /* actual lines we expect are ~30 chars or less */
|
|
|
|
FILE *fp;
|
|
|
|
unsigned long cached = 0;
|
|
|
|
|
|
|
|
fp = xfopen_for_read("/proc/meminfo");
|
|
|
|
while (fgets(buf, sizeof(buf), fp) != NULL) {
|
|
|
|
if (sscanf(buf, "Cached: %lu %*s\n", &cached) == 1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return cached;
|
|
|
|
}
|
2010-10-01 19:57:59 +00:00
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2010-02-26 08:28:30 +00:00
|
|
|
int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
|
1999-12-08 23:19:36 +00:00
|
|
|
{
|
2000-02-21 21:26:32 +00:00
|
|
|
struct sysinfo info;
|
2015-01-05 17:34:53 +00:00
|
|
|
unsigned long long cached;
|
2010-10-01 19:57:59 +00:00
|
|
|
|
|
|
|
INIT_G();
|
2010-02-21 04:39:59 +00:00
|
|
|
|
|
|
|
#if ENABLE_DESKTOP
|
2010-10-01 19:57:59 +00:00
|
|
|
G.unit_steps = 10;
|
|
|
|
if (argv[1] && argv[1][0] == '-') {
|
|
|
|
switch (argv[1][1]) {
|
|
|
|
case 'b':
|
|
|
|
G.unit_steps = 0;
|
|
|
|
break;
|
|
|
|
case 'k': /* 2^10 */
|
|
|
|
/* G.unit_steps = 10; - already is */
|
|
|
|
break;
|
|
|
|
case 'm': /* 2^(2*10) */
|
|
|
|
G.unit_steps = 20;
|
|
|
|
break;
|
|
|
|
case 'g': /* 2^(3*10) */
|
|
|
|
G.unit_steps = 30;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bb_show_usage();
|
|
|
|
}
|
|
|
|
}
|
2010-02-21 04:39:59 +00:00
|
|
|
#endif
|
2015-01-05 17:34:53 +00:00
|
|
|
printf(" %11s%11s%11s%11s%11s%11s\n"
|
|
|
|
"Mem: ",
|
2010-02-21 04:39:59 +00:00
|
|
|
"total",
|
|
|
|
"used",
|
|
|
|
"free",
|
2015-01-05 17:34:53 +00:00
|
|
|
"shared", "buffers", "cached" /* swap and total don't have these columns */
|
2010-02-21 04:39:59 +00:00
|
|
|
);
|
2010-10-01 19:57:59 +00:00
|
|
|
|
2015-01-05 17:34:53 +00:00
|
|
|
sysinfo(&info);
|
|
|
|
/* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
|
|
|
|
G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
|
|
|
|
/* Extract cached from /proc/meminfo and convert to mem_units */
|
|
|
|
cached = ((unsigned long long) parse_cached_kb() * 1024) / G.mem_unit;
|
|
|
|
|
|
|
|
#define FIELDS_6 "%11llu%11llu%11llu%11llu%11llu%11llu\n"
|
|
|
|
#define FIELDS_3 (FIELDS_6 + 3*6)
|
|
|
|
#define FIELDS_2 (FIELDS_6 + 4*6)
|
2010-10-01 19:57:59 +00:00
|
|
|
|
2015-01-05 17:34:53 +00:00
|
|
|
printf(FIELDS_6,
|
|
|
|
scale(info.totalram), //total
|
|
|
|
scale(info.totalram - info.freeram), //used
|
|
|
|
scale(info.freeram), //free
|
|
|
|
scale(info.sharedram), //shared
|
|
|
|
scale(info.bufferram), //buffers
|
|
|
|
scale(cached) //cached
|
2010-02-21 04:39:59 +00:00
|
|
|
);
|
2010-08-11 23:56:44 +00:00
|
|
|
/* Show alternate, more meaningful busy/free numbers by counting
|
2015-01-05 17:34:53 +00:00
|
|
|
* buffer cache as free memory. */
|
|
|
|
printf("-/+ buffers/cache:");
|
|
|
|
cached += info.freeram;
|
|
|
|
cached += info.bufferram;
|
2010-08-11 23:56:44 +00:00
|
|
|
printf(FIELDS_2,
|
2015-01-05 17:34:53 +00:00
|
|
|
scale(info.totalram - cached), //used
|
|
|
|
scale(cached) //free
|
2010-08-11 23:56:44 +00:00
|
|
|
);
|
2010-02-21 04:17:41 +00:00
|
|
|
#if BB_MMU
|
2015-01-05 17:34:53 +00:00
|
|
|
printf("Swap: ");
|
2010-08-11 23:56:44 +00:00
|
|
|
printf(FIELDS_3,
|
2015-01-05 17:34:53 +00:00
|
|
|
scale(info.totalswap), //total
|
|
|
|
scale(info.totalswap - info.freeswap), //used
|
|
|
|
scale(info.freeswap) //free
|
2010-02-21 04:39:59 +00:00
|
|
|
);
|
2003-01-11 20:40:49 +00:00
|
|
|
#endif
|
2000-12-01 02:55:13 +00:00
|
|
|
return EXIT_SUCCESS;
|
1999-12-08 23:19:36 +00:00
|
|
|
}
|