blob: e41601e08ee5bc64a67b9e091f5b2b80906f525d [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 */
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +01009//config:config FREE
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "free (2.4 kb)"
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010011//config: default y
12//config: select PLATFORM_LINUX #sysinfo()
13//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: free displays the total amount of free and used physical and swap
15//config: memory in the system, as well as the buffers used by the kernel.
16//config: The shared memory column should be ignored; it is obsolete.
Eric Andersenabc0f4f1999-12-08 23:19:36 +000017
Denys Vlasenko318c8112017-10-05 14:06:49 +020018//applet:IF_FREE(APPLET_NOFORK(free, free, BB_DIR_USR_BIN, BB_SUID_DROP, free))
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010019
20//kbuild:lib-$(CONFIG_FREE) += free.o
Mark Whitley827e45c2001-03-09 23:59:51 +000021
Pere Orga5bc8c002011-04-11 03:29:49 +020022//usage:#define free_trivial_usage
23//usage: "" IF_DESKTOP("[-b/k/m/g]")
24//usage:#define free_full_usage "\n\n"
25//usage: "Display the amount of free and used system memory"
26//usage:
27//usage:#define free_example_usage
28//usage: "$ free\n"
29//usage: " total used free shared buffers\n"
30//usage: " Mem: 257628 248724 8904 59644 93124\n"
31//usage: " Swap: 128516 8404 120112\n"
32//usage: "Total: 386144 257128 129016\n"
33
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000034#include "libbb.h"
Denys Vlasenko67905e22011-07-26 13:42:12 +020035#ifdef __linux__
36# include <sys/sysinfo.h>
37#endif
Eric Andersenabc0f4f1999-12-08 23:19:36 +000038
Denys Vlasenko55429342010-10-01 21:57:59 +020039struct globals {
40 unsigned mem_unit;
41#if ENABLE_DESKTOP
Denys Vlasenko318c8112017-10-05 14:06:49 +020042 uint8_t unit_steps;
43# define G_unit_steps g->unit_steps
Denys Vlasenko55429342010-10-01 21:57:59 +020044#else
45# define G_unit_steps 10
46#endif
Denys Vlasenko318c8112017-10-05 14:06:49 +020047};
48/* Because of NOFORK, "globals" are not in global data */
Denys Vlasenko55429342010-10-01 21:57:59 +020049
Denys Vlasenko318c8112017-10-05 14:06:49 +020050static unsigned long long scale(struct globals *g, unsigned long d)
Denys Vlasenko55429342010-10-01 21:57:59 +020051{
Denys Vlasenko318c8112017-10-05 14:06:49 +020052 return ((unsigned long long)d * g->mem_unit) >> G_unit_steps;
Denys Vlasenko55429342010-10-01 21:57:59 +020053}
54
Denys Vlasenko318c8112017-10-05 14:06:49 +020055/* NOINLINE reduces main() stack usage, which makes code smaller (on x86 at least) */
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +010056static NOINLINE unsigned int parse_meminfo(unsigned long *cached_kb, unsigned long *available_kb)
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010057{
58 char buf[60]; /* actual lines we expect are ~30 chars or less */
59 FILE *fp;
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +010060 int seen_cached_and_available;
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010061
62 fp = xfopen_for_read("/proc/meminfo");
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +010063 *cached_kb = *available_kb = 0;
64 seen_cached_and_available = 2;
65 while (fgets(buf, sizeof(buf), fp)) {
66 if (sscanf(buf, "Cached: %lu %*s\n", cached_kb) == 1)
67 if (--seen_cached_and_available == 0)
68 break;
69 if (sscanf(buf, "MemAvailable: %lu %*s\n", available_kb) == 1)
70 if (--seen_cached_and_available == 0)
71 break;
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010072 }
Denys Vlasenko318c8112017-10-05 14:06:49 +020073 /* Have to close because of NOFORK */
74 fclose(fp);
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010075
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +010076 return seen_cached_and_available == 0;
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010077}
Denys Vlasenko55429342010-10-01 21:57:59 +020078
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000079int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischer7e7728c2010-02-26 09:28:30 +010080int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
Eric Andersenabc0f4f1999-12-08 23:19:36 +000081{
Denys Vlasenko318c8112017-10-05 14:06:49 +020082 struct globals G;
Erik Andersend07ee462000-02-21 21:26:32 +000083 struct sysinfo info;
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +010084 unsigned long long cached, cached_plus_free, available;
85 unsigned long cached_kb, available_kb;
86 int seen_available;
Denys Vlasenko55429342010-10-01 21:57:59 +020087
Denys Vlasenkobef57112010-02-21 05:39:59 +010088#if ENABLE_DESKTOP
Denys Vlasenko55429342010-10-01 21:57:59 +020089 G.unit_steps = 10;
90 if (argv[1] && argv[1][0] == '-') {
91 switch (argv[1][1]) {
92 case 'b':
93 G.unit_steps = 0;
94 break;
95 case 'k': /* 2^10 */
96 /* G.unit_steps = 10; - already is */
97 break;
98 case 'm': /* 2^(2*10) */
99 G.unit_steps = 20;
100 break;
101 case 'g': /* 2^(3*10) */
102 G.unit_steps = 30;
103 break;
104 default:
105 bb_show_usage();
106 }
107 }
Denys Vlasenkobef57112010-02-21 05:39:59 +0100108#endif
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100109 printf(" %12s%12s%12s%12s%12s%12s\n"
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100110 "Mem: ",
Denys Vlasenkobef57112010-02-21 05:39:59 +0100111 "total",
112 "used",
113 "free",
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100114 "shared", "buff/cache", "available" /* swap and total don't have these columns */
Denys Vlasenkobef57112010-02-21 05:39:59 +0100115 );
Denys Vlasenko55429342010-10-01 21:57:59 +0200116
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100117 sysinfo(&info);
118 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
119 G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100120 /* Extract cached and memavailable from /proc/meminfo and convert to mem_units */
121 seen_available = parse_meminfo(&cached_kb, &available_kb);
122 available = ((unsigned long long) available_kb * 1024) / G.mem_unit;
123 cached = ((unsigned long long) cached_kb * 1024) / G.mem_unit;
124 cached += info.bufferram;
125 cached_plus_free = cached + info.freeram;
Denys Vlasenko55429342010-10-01 21:57:59 +0200126
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100127#define FIELDS_6 "%12llu%12llu%12llu%12llu%12llu%12llu\n"
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100128#define FIELDS_3 (FIELDS_6 + 3*6)
129#define FIELDS_2 (FIELDS_6 + 4*6)
130
131 printf(FIELDS_6,
Denys Vlasenko318c8112017-10-05 14:06:49 +0200132 scale(&G, info.totalram), //total
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100133 scale(&G, info.totalram - cached_plus_free), //used
Denys Vlasenko318c8112017-10-05 14:06:49 +0200134 scale(&G, info.freeram), //free
135 scale(&G, info.sharedram), //shared
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100136 scale(&G, cached), //buff/cache
137 scale(&G, available) //available
Denys Vlasenkobef57112010-02-21 05:39:59 +0100138 );
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100139 /* On kernels < 3.14, MemAvailable is not provided.
140 * Show alternate, more meaningful busy/free numbers by counting
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100141 * buffer cache as free memory. */
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100142 if (!seen_available) {
143 printf("-/+ buffers/cache: ");
144 printf(FIELDS_2,
145 scale(&G, info.totalram - cached_plus_free), //used
146 scale(&G, cached_plus_free) //free
147 );
148 }
Denys Vlasenko153fcaa2010-02-21 05:17:41 +0100149#if BB_MMU
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100150 printf("Swap: ");
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +0200151 printf(FIELDS_3,
Denys Vlasenko318c8112017-10-05 14:06:49 +0200152 scale(&G, info.totalswap), //total
153 scale(&G, info.totalswap - info.freeswap), //used
154 scale(&G, info.freeswap) //free
Denys Vlasenkobef57112010-02-21 05:39:59 +0100155 );
Eric Andersen1dcf2182003-01-11 20:40:49 +0000156#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +0000157 return EXIT_SUCCESS;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000158}