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
|
|
|
*
|
2006-09-22 08:22:41 +05:30
|
|
|
* Licensed under the GPL version 2, see the file LICENSE in this tarball.
|
1999-12-09 04:49:36 +05:30
|
|
|
*/
|
|
|
|
|
2001-03-10 05:29:51 +05:30
|
|
|
/* getopt not needed */
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2000-06-26 16:15:52 +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
|
|
|
{
|
2000-02-22 02:56:32 +05:30
|
|
|
struct sysinfo info;
|
2010-02-21 10:09:59 +05:30
|
|
|
unsigned mem_unit;
|
|
|
|
|
|
|
|
#if ENABLE_DESKTOP
|
|
|
|
if (argv[1] && argv[1][0] == '-')
|
|
|
|
bb_show_usage();
|
|
|
|
#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-02-21 10:09:59 +05:30
|
|
|
mem_unit = 1;
|
|
|
|
if (info.mem_unit != 0) {
|
|
|
|
mem_unit = info.mem_unit;
|
2000-06-26 16:15:52 +05:30
|
|
|
}
|
2003-01-12 02:10:49 +05:30
|
|
|
|
2010-02-21 10:09:59 +05:30
|
|
|
/* Convert values to kbytes */
|
|
|
|
if (mem_unit == 1) {
|
|
|
|
info.totalram >>= 10;
|
|
|
|
info.freeram >>= 10;
|
2010-02-21 09:47:41 +05:30
|
|
|
#if BB_MMU
|
2010-02-21 10:09:59 +05:30
|
|
|
info.totalswap >>= 10;
|
|
|
|
info.freeswap >>= 10;
|
2003-01-12 02:10:49 +05:30
|
|
|
#endif
|
2010-02-21 10:09:59 +05:30
|
|
|
info.sharedram >>= 10;
|
|
|
|
info.bufferram >>= 10;
|
2003-01-12 02:10:49 +05:30
|
|
|
} else {
|
2010-02-21 10:09:59 +05:30
|
|
|
mem_unit >>= 10;
|
|
|
|
/* TODO: Make all this stuff not overflow when mem >= 4 Tb */
|
|
|
|
info.totalram *= mem_unit;
|
|
|
|
info.freeram *= mem_unit;
|
2010-02-21 09:47:41 +05:30
|
|
|
#if BB_MMU
|
2010-02-21 10:09:59 +05:30
|
|
|
info.totalswap *= mem_unit;
|
|
|
|
info.freeswap *= mem_unit;
|
2003-01-12 02:10:49 +05:30
|
|
|
#endif
|
2010-02-21 10:09:59 +05:30
|
|
|
info.sharedram *= mem_unit;
|
|
|
|
info.bufferram *= mem_unit;
|
2003-01-12 02:10:49 +05:30
|
|
|
}
|
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-08-12 05:26:44 +05:30
|
|
|
#define FIELDS_5 "%13lu%13lu%13lu%13lu%13lu\n"
|
|
|
|
#define FIELDS_3 (FIELDS_5 + 2*5)
|
|
|
|
#define FIELDS_2 (FIELDS_5 + 3*5)
|
|
|
|
printf("Mem: ");
|
|
|
|
printf(FIELDS_5,
|
2010-02-21 10:09:59 +05:30
|
|
|
info.totalram,
|
|
|
|
info.totalram - info.freeram,
|
|
|
|
info.freeram,
|
|
|
|
info.sharedram, info.bufferram
|
|
|
|
);
|
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,
|
|
|
|
info.totalram - info.freeram - info.bufferram,
|
|
|
|
info.freeram + info.bufferram
|
|
|
|
);
|
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-02-21 10:09:59 +05:30
|
|
|
info.totalswap,
|
|
|
|
info.totalswap - info.freeswap,
|
|
|
|
info.freeswap
|
|
|
|
);
|
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
|
|
|
}
|