blob: ad8711f8a08d0cbe64bb8b2abe999ff719ce69ef [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
Denys Vlasenko55429342010-10-01 21:57:59 +020014struct globals {
15 unsigned mem_unit;
16#if ENABLE_DESKTOP
17 unsigned unit_steps;
18# define G_unit_steps G.unit_steps
19#else
20# define G_unit_steps 10
21#endif
Denys Vlasenkoe8d0a142011-01-16 11:21:15 +010022} FIX_ALIASING;
Denys Vlasenko55429342010-10-01 21:57:59 +020023#define G (*(struct globals*)&bb_common_bufsiz1)
24#define INIT_G() do { } while (0)
25
26
27static unsigned long long scale(unsigned long d)
28{
29 return ((unsigned long long)d * G.mem_unit) >> G_unit_steps;
30}
31
32
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000033int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischer7e7728c2010-02-26 09:28:30 +010034int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
Eric Andersenabc0f4f1999-12-08 23:19:36 +000035{
Erik Andersend07ee462000-02-21 21:26:32 +000036 struct sysinfo info;
Denys Vlasenko55429342010-10-01 21:57:59 +020037
38 INIT_G();
Denys Vlasenkobef57112010-02-21 05:39:59 +010039
40#if ENABLE_DESKTOP
Denys Vlasenko55429342010-10-01 21:57:59 +020041 G.unit_steps = 10;
42 if (argv[1] && argv[1][0] == '-') {
43 switch (argv[1][1]) {
44 case 'b':
45 G.unit_steps = 0;
46 break;
47 case 'k': /* 2^10 */
48 /* G.unit_steps = 10; - already is */
49 break;
50 case 'm': /* 2^(2*10) */
51 G.unit_steps = 20;
52 break;
53 case 'g': /* 2^(3*10) */
54 G.unit_steps = 30;
55 break;
56 default:
57 bb_show_usage();
58 }
59 }
Denys Vlasenkobef57112010-02-21 05:39:59 +010060#endif
61
Erik Andersend07ee462000-02-21 21:26:32 +000062 sysinfo(&info);
Eric Andersen73de6562000-09-10 16:10:41 +000063
64 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
Denys Vlasenko55429342010-10-01 21:57:59 +020065 G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
Eric Andersen73de6562000-09-10 16:10:41 +000066
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +020067 printf(" %13s%13s%13s%13s%13s\n",
Denys Vlasenkobef57112010-02-21 05:39:59 +010068 "total",
69 "used",
70 "free",
71 "shared", "buffers" /* swap and total don't have these columns */
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +020072 /* procps version 3.2.8 also shows "cached" column, but
73 * sysinfo() does not provide this value, need to parse
74 * /proc/meminfo instead and get "Cached: NNN kB" from there.
75 */
Denys Vlasenkobef57112010-02-21 05:39:59 +010076 );
Denys Vlasenko55429342010-10-01 21:57:59 +020077
78#define FIELDS_5 "%13llu%13llu%13llu%13llu%13llu\n"
79#define FIELDS_3 (FIELDS_5 + 2*6)
80#define FIELDS_2 (FIELDS_5 + 3*6)
81
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +020082 printf("Mem: ");
83 printf(FIELDS_5,
Denys Vlasenko55429342010-10-01 21:57:59 +020084 scale(info.totalram),
85 scale(info.totalram - info.freeram),
86 scale(info.freeram),
87 scale(info.sharedram),
88 scale(info.bufferram)
Denys Vlasenkobef57112010-02-21 05:39:59 +010089 );
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +020090 /* Show alternate, more meaningful busy/free numbers by counting
91 * buffer cache as free memory (make it "-/+ buffers/cache"
92 * if/when we add support for "cached" column): */
93 printf("-/+ buffers: ");
94 printf(FIELDS_2,
Denys Vlasenko55429342010-10-01 21:57:59 +020095 scale(info.totalram - info.freeram - info.bufferram),
96 scale(info.freeram + info.bufferram)
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +020097 );
Denys Vlasenko153fcaa2010-02-21 05:17:41 +010098#if BB_MMU
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +020099 printf("Swap:");
100 printf(FIELDS_3,
Denys Vlasenko55429342010-10-01 21:57:59 +0200101 scale(info.totalswap),
102 scale(info.totalswap - info.freeswap),
103 scale(info.freeswap)
Denys Vlasenkobef57112010-02-21 05:39:59 +0100104 );
Eric Andersen1dcf2182003-01-11 20:40:49 +0000105#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +0000106 return EXIT_SUCCESS;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000107}