blob: be65f46f134cb1629465ad57b4fa63a132050d11 [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 */
9
Mark Whitley827e45c2001-03-09 23:59:51 +000010/* getopt not needed */
11
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.h"
Eric Andersenabc0f4f1999-12-08 23:19:36 +000013
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000014int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischer7e7728c2010-02-26 09:28:30 +010015int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
Eric Andersenabc0f4f1999-12-08 23:19:36 +000016{
Erik Andersend07ee462000-02-21 21:26:32 +000017 struct sysinfo info;
Denys Vlasenkobef57112010-02-21 05:39:59 +010018 unsigned mem_unit;
19
20#if ENABLE_DESKTOP
21 if (argv[1] && argv[1][0] == '-')
22 bb_show_usage();
23#endif
24
Erik Andersend07ee462000-02-21 21:26:32 +000025 sysinfo(&info);
Eric Andersen73de6562000-09-10 16:10:41 +000026
27 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
Denys Vlasenkobef57112010-02-21 05:39:59 +010028 mem_unit = 1;
29 if (info.mem_unit != 0) {
30 mem_unit = info.mem_unit;
Eric Andersen10dc9d42000-06-26 10:45:52 +000031 }
Eric Andersen1dcf2182003-01-11 20:40:49 +000032
Denys Vlasenkobef57112010-02-21 05:39:59 +010033 /* Convert values to kbytes */
34 if (mem_unit == 1) {
35 info.totalram >>= 10;
36 info.freeram >>= 10;
Denys Vlasenko153fcaa2010-02-21 05:17:41 +010037#if BB_MMU
Denys Vlasenkobef57112010-02-21 05:39:59 +010038 info.totalswap >>= 10;
39 info.freeswap >>= 10;
Eric Andersen1dcf2182003-01-11 20:40:49 +000040#endif
Denys Vlasenkobef57112010-02-21 05:39:59 +010041 info.sharedram >>= 10;
42 info.bufferram >>= 10;
Eric Andersen1dcf2182003-01-11 20:40:49 +000043 } else {
Denys Vlasenkobef57112010-02-21 05:39:59 +010044 mem_unit >>= 10;
45 /* TODO: Make all this stuff not overflow when mem >= 4 Tb */
46 info.totalram *= mem_unit;
47 info.freeram *= mem_unit;
Denys Vlasenko153fcaa2010-02-21 05:17:41 +010048#if BB_MMU
Denys Vlasenkobef57112010-02-21 05:39:59 +010049 info.totalswap *= mem_unit;
50 info.freeswap *= mem_unit;
Eric Andersen1dcf2182003-01-11 20:40:49 +000051#endif
Denys Vlasenkobef57112010-02-21 05:39:59 +010052 info.sharedram *= mem_unit;
53 info.bufferram *= mem_unit;
Eric Andersen1dcf2182003-01-11 20:40:49 +000054 }
Eric Andersen73de6562000-09-10 16:10:41 +000055
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +020056 printf(" %13s%13s%13s%13s%13s\n",
Denys Vlasenkobef57112010-02-21 05:39:59 +010057 "total",
58 "used",
59 "free",
60 "shared", "buffers" /* swap and total don't have these columns */
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +020061 /* procps version 3.2.8 also shows "cached" column, but
62 * sysinfo() does not provide this value, need to parse
63 * /proc/meminfo instead and get "Cached: NNN kB" from there.
64 */
Denys Vlasenkobef57112010-02-21 05:39:59 +010065 );
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +020066#define FIELDS_5 "%13lu%13lu%13lu%13lu%13lu\n"
67#define FIELDS_3 (FIELDS_5 + 2*5)
68#define FIELDS_2 (FIELDS_5 + 3*5)
69 printf("Mem: ");
70 printf(FIELDS_5,
Denys Vlasenkobef57112010-02-21 05:39:59 +010071 info.totalram,
72 info.totalram - info.freeram,
73 info.freeram,
74 info.sharedram, info.bufferram
75 );
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +020076 /* Show alternate, more meaningful busy/free numbers by counting
77 * buffer cache as free memory (make it "-/+ buffers/cache"
78 * if/when we add support for "cached" column): */
79 printf("-/+ buffers: ");
80 printf(FIELDS_2,
81 info.totalram - info.freeram - info.bufferram,
82 info.freeram + info.bufferram
83 );
Denys Vlasenko153fcaa2010-02-21 05:17:41 +010084#if BB_MMU
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +020085 printf("Swap:");
86 printf(FIELDS_3,
Denys Vlasenkobef57112010-02-21 05:39:59 +010087 info.totalswap,
88 info.totalswap - info.freeswap,
89 info.freeswap
90 );
Eric Andersen1dcf2182003-01-11 20:40:49 +000091#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +000092 return EXIT_SUCCESS;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000093}