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
|
|
|
*/
|
|
|
|
|
2001-03-10 05:29:51 +05:30
|
|
|
/* getopt not needed */
|
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//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-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2000-06-26 16:15:52 +05:30
|
|
|
|
2010-10-02 01:27:59 +05:30
|
|
|
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 15:51:15 +05:30
|
|
|
} FIX_ALIASING;
|
2010-10-02 01:27:59 +05:30
|
|
|
#define G (*(struct globals*)&bb_common_bufsiz1)
|
|
|
|
#define INIT_G() do { } while (0)
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned long long scale(unsigned long d)
|
|
|
|
{
|
|
|
|
return ((unsigned long long)d * G.mem_unit) >> G_unit_steps;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2000-02-22 02:56:32 +05:30
|
|
|
struct sysinfo info;
|
2010-10-02 01:27:59 +05:30
|
|
|
|
|
|
|
INIT_G();
|
2010-02-21 10:09:59 +05:30
|
|
|
|
|
|
|
#if ENABLE_DESKTOP
|
2010-10-02 01:27:59 +05:30
|
|
|
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 10:09:59 +05:30
|
|
|
#endif
|
|
|
|
|
2000-02-22 02:56:32 +05:30
|
|
|
sysinfo(&info);
|
2000-09-10 21:40:41 +05:30
|
|
|
|
|
|
|
/* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
|
2010-10-02 01:27:59 +05:30
|
|
|
G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
|
2000-09-10 21:40:41 +05:30
|
|
|
|
2010-08-12 05:26:44 +05:30
|
|
|
printf(" %13s%13s%13s%13s%13s\n",
|
2010-02-21 10:09:59 +05:30
|
|
|
"total",
|
|
|
|
"used",
|
|
|
|
"free",
|
|
|
|
"shared", "buffers" /* swap and total don't have these columns */
|
2010-08-12 05:26:44 +05:30
|
|
|
/* procps version 3.2.8 also shows "cached" column, but
|
|
|
|
* sysinfo() does not provide this value, need to parse
|
|
|
|
* /proc/meminfo instead and get "Cached: NNN kB" from there.
|
|
|
|
*/
|
2010-02-21 10:09:59 +05:30
|
|
|
);
|
2010-10-02 01:27:59 +05:30
|
|
|
|
|
|
|
#define FIELDS_5 "%13llu%13llu%13llu%13llu%13llu\n"
|
|
|
|
#define FIELDS_3 (FIELDS_5 + 2*6)
|
|
|
|
#define FIELDS_2 (FIELDS_5 + 3*6)
|
|
|
|
|
2010-08-12 05:26:44 +05:30
|
|
|
printf("Mem: ");
|
|
|
|
printf(FIELDS_5,
|
2010-10-02 01:27:59 +05:30
|
|
|
scale(info.totalram),
|
|
|
|
scale(info.totalram - info.freeram),
|
|
|
|
scale(info.freeram),
|
|
|
|
scale(info.sharedram),
|
|
|
|
scale(info.bufferram)
|
2010-02-21 10:09:59 +05:30
|
|
|
);
|
2010-08-12 05:26:44 +05:30
|
|
|
/* Show alternate, more meaningful busy/free numbers by counting
|
|
|
|
* buffer cache as free memory (make it "-/+ buffers/cache"
|
|
|
|
* if/when we add support for "cached" column): */
|
|
|
|
printf("-/+ buffers: ");
|
|
|
|
printf(FIELDS_2,
|
2010-10-02 01:27:59 +05:30
|
|
|
scale(info.totalram - info.freeram - info.bufferram),
|
|
|
|
scale(info.freeram + info.bufferram)
|
2010-08-12 05:26:44 +05:30
|
|
|
);
|
2010-02-21 09:47:41 +05:30
|
|
|
#if BB_MMU
|
2010-08-12 05:26:44 +05:30
|
|
|
printf("Swap:");
|
|
|
|
printf(FIELDS_3,
|
2010-10-02 01:27:59 +05:30
|
|
|
scale(info.totalswap),
|
|
|
|
scale(info.totalswap - info.freeswap),
|
|
|
|
scale(info.freeswap)
|
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
|
|
|
}
|