blob: ca753134c102c6aab8a48f8b33bf8cad5e16bcc6 [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
Pere Orga5bc8c002011-04-11 03:29:49 +020012//usage:#define free_trivial_usage
13//usage: "" IF_DESKTOP("[-b/k/m/g]")
14//usage:#define free_full_usage "\n\n"
15//usage: "Display the amount of free and used system memory"
16//usage:
17//usage:#define free_example_usage
18//usage: "$ free\n"
19//usage: " total used free shared buffers\n"
20//usage: " Mem: 257628 248724 8904 59644 93124\n"
21//usage: " Swap: 128516 8404 120112\n"
22//usage: "Total: 386144 257128 129016\n"
23
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000024#include "libbb.h"
Eric Andersenabc0f4f1999-12-08 23:19:36 +000025
Denys Vlasenko55429342010-10-01 21:57:59 +020026struct globals {
27 unsigned mem_unit;
28#if ENABLE_DESKTOP
29 unsigned unit_steps;
30# define G_unit_steps G.unit_steps
31#else
32# define G_unit_steps 10
33#endif
Denys Vlasenkoe8d0a142011-01-16 11:21:15 +010034} FIX_ALIASING;
Denys Vlasenko55429342010-10-01 21:57:59 +020035#define G (*(struct globals*)&bb_common_bufsiz1)
36#define INIT_G() do { } while (0)
37
38
39static unsigned long long scale(unsigned long d)
40{
41 return ((unsigned long long)d * G.mem_unit) >> G_unit_steps;
42}
43
44
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000045int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischer7e7728c2010-02-26 09:28:30 +010046int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
Eric Andersenabc0f4f1999-12-08 23:19:36 +000047{
Erik Andersend07ee462000-02-21 21:26:32 +000048 struct sysinfo info;
Denys Vlasenko55429342010-10-01 21:57:59 +020049
50 INIT_G();
Denys Vlasenkobef57112010-02-21 05:39:59 +010051
52#if ENABLE_DESKTOP
Denys Vlasenko55429342010-10-01 21:57:59 +020053 G.unit_steps = 10;
54 if (argv[1] && argv[1][0] == '-') {
55 switch (argv[1][1]) {
56 case 'b':
57 G.unit_steps = 0;
58 break;
59 case 'k': /* 2^10 */
60 /* G.unit_steps = 10; - already is */
61 break;
62 case 'm': /* 2^(2*10) */
63 G.unit_steps = 20;
64 break;
65 case 'g': /* 2^(3*10) */
66 G.unit_steps = 30;
67 break;
68 default:
69 bb_show_usage();
70 }
71 }
Denys Vlasenkobef57112010-02-21 05:39:59 +010072#endif
73
Erik Andersend07ee462000-02-21 21:26:32 +000074 sysinfo(&info);
Eric Andersen73de6562000-09-10 16:10:41 +000075
76 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
Denys Vlasenko55429342010-10-01 21:57:59 +020077 G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
Eric Andersen73de6562000-09-10 16:10:41 +000078
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +020079 printf(" %13s%13s%13s%13s%13s\n",
Denys Vlasenkobef57112010-02-21 05:39:59 +010080 "total",
81 "used",
82 "free",
83 "shared", "buffers" /* swap and total don't have these columns */
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +020084 /* procps version 3.2.8 also shows "cached" column, but
85 * sysinfo() does not provide this value, need to parse
86 * /proc/meminfo instead and get "Cached: NNN kB" from there.
87 */
Denys Vlasenkobef57112010-02-21 05:39:59 +010088 );
Denys Vlasenko55429342010-10-01 21:57:59 +020089
90#define FIELDS_5 "%13llu%13llu%13llu%13llu%13llu\n"
91#define FIELDS_3 (FIELDS_5 + 2*6)
92#define FIELDS_2 (FIELDS_5 + 3*6)
93
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +020094 printf("Mem: ");
95 printf(FIELDS_5,
Denys Vlasenko55429342010-10-01 21:57:59 +020096 scale(info.totalram),
97 scale(info.totalram - info.freeram),
98 scale(info.freeram),
99 scale(info.sharedram),
100 scale(info.bufferram)
Denys Vlasenkobef57112010-02-21 05:39:59 +0100101 );
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +0200102 /* Show alternate, more meaningful busy/free numbers by counting
103 * buffer cache as free memory (make it "-/+ buffers/cache"
104 * if/when we add support for "cached" column): */
105 printf("-/+ buffers: ");
106 printf(FIELDS_2,
Denys Vlasenko55429342010-10-01 21:57:59 +0200107 scale(info.totalram - info.freeram - info.bufferram),
108 scale(info.freeram + info.bufferram)
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +0200109 );
Denys Vlasenko153fcaa2010-02-21 05:17:41 +0100110#if BB_MMU
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +0200111 printf("Swap:");
112 printf(FIELDS_3,
Denys Vlasenko55429342010-10-01 21:57:59 +0200113 scale(info.totalswap),
114 scale(info.totalswap - info.freeswap),
115 scale(info.freeswap)
Denys Vlasenkobef57112010-02-21 05:39:59 +0100116 );
Eric Andersen1dcf2182003-01-11 20:40:49 +0000117#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +0000118 return EXIT_SUCCESS;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000119}