blob: 0adef501f72a18b79fa16508595c92f53939ae7a [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 Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "free (3.1 kb)"
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010011//config: default y
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010012//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: free displays the total amount of free and used physical and swap
14//config: memory in the system, as well as the buffers used by the kernel.
15//config: The shared memory column should be ignored; it is obsolete.
Eric Andersenabc0f4f1999-12-08 23:19:36 +000016
Denys Vlasenko318c8112017-10-05 14:06:49 +020017//applet:IF_FREE(APPLET_NOFORK(free, free, BB_DIR_USR_BIN, BB_SUID_DROP, free))
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010018
19//kbuild:lib-$(CONFIG_FREE) += free.o
Mark Whitley827e45c2001-03-09 23:59:51 +000020
Pere Orga5bc8c002011-04-11 03:29:49 +020021//usage:#define free_trivial_usage
22//usage: "" IF_DESKTOP("[-b/k/m/g]")
23//usage:#define free_full_usage "\n\n"
24//usage: "Display the amount of free and used system memory"
25//usage:
26//usage:#define free_example_usage
27//usage: "$ free\n"
28//usage: " total used free shared buffers\n"
29//usage: " Mem: 257628 248724 8904 59644 93124\n"
30//usage: " Swap: 128516 8404 120112\n"
31//usage: "Total: 386144 257128 129016\n"
32
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000033#include "libbb.h"
Denys Vlasenko67905e22011-07-26 13:42:12 +020034#ifdef __linux__
35# include <sys/sysinfo.h>
36#endif
Eric Andersenabc0f4f1999-12-08 23:19:36 +000037
Denys Vlasenko55429342010-10-01 21:57:59 +020038struct globals {
39 unsigned mem_unit;
40#if ENABLE_DESKTOP
Denys Vlasenko318c8112017-10-05 14:06:49 +020041 uint8_t unit_steps;
42# define G_unit_steps g->unit_steps
Denys Vlasenko55429342010-10-01 21:57:59 +020043#else
44# define G_unit_steps 10
45#endif
Lukas Rusak9663bbd2019-06-25 18:52:33 +020046 unsigned long cached_kb, available_kb, reclaimable_kb;
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) */
Lukas Rusak9663bbd2019-06-25 18:52:33 +020056static NOINLINE unsigned int parse_meminfo(struct globals *g)
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010057{
58 char buf[60]; /* actual lines we expect are ~30 chars or less */
59 FILE *fp;
Lukas Rusak9663bbd2019-06-25 18:52:33 +020060 int seen_cached_and_available_and_reclaimable;
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010061
62 fp = xfopen_for_read("/proc/meminfo");
Lukas Rusak9663bbd2019-06-25 18:52:33 +020063 g->cached_kb = g->available_kb = g->reclaimable_kb = 0;
64 seen_cached_and_available_and_reclaimable = 3;
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +010065 while (fgets(buf, sizeof(buf), fp)) {
Lukas Rusak9663bbd2019-06-25 18:52:33 +020066 if (sscanf(buf, "Cached: %lu %*s\n", &g->cached_kb) == 1)
67 if (--seen_cached_and_available_and_reclaimable == 0)
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +010068 break;
Lukas Rusak9663bbd2019-06-25 18:52:33 +020069 if (sscanf(buf, "MemAvailable: %lu %*s\n", &g->available_kb) == 1)
70 if (--seen_cached_and_available_and_reclaimable == 0)
71 break;
72 if (sscanf(buf, "SReclaimable: %lu %*s\n", &g->reclaimable_kb) == 1)
73 if (--seen_cached_and_available_and_reclaimable == 0)
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +010074 break;
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010075 }
Denys Vlasenko318c8112017-10-05 14:06:49 +020076 /* Have to close because of NOFORK */
77 fclose(fp);
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010078
Lukas Rusak9663bbd2019-06-25 18:52:33 +020079 return seen_cached_and_available_and_reclaimable == 0;
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010080}
Denys Vlasenko55429342010-10-01 21:57:59 +020081
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000082int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischer7e7728c2010-02-26 09:28:30 +010083int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
Eric Andersenabc0f4f1999-12-08 23:19:36 +000084{
Denys Vlasenko318c8112017-10-05 14:06:49 +020085 struct globals G;
Erik Andersend07ee462000-02-21 21:26:32 +000086 struct sysinfo info;
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +010087 unsigned long long cached, cached_plus_free, available;
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +010088 int seen_available;
Denys Vlasenko55429342010-10-01 21:57:59 +020089
Denys Vlasenkobef57112010-02-21 05:39:59 +010090#if ENABLE_DESKTOP
Denys Vlasenko55429342010-10-01 21:57:59 +020091 G.unit_steps = 10;
92 if (argv[1] && argv[1][0] == '-') {
93 switch (argv[1][1]) {
94 case 'b':
95 G.unit_steps = 0;
96 break;
97 case 'k': /* 2^10 */
98 /* G.unit_steps = 10; - already is */
99 break;
Denys Vlasenkofc635492020-11-27 12:40:31 +0100100 case 'm': /* 2^20 */
Denys Vlasenko55429342010-10-01 21:57:59 +0200101 G.unit_steps = 20;
102 break;
Denys Vlasenkofc635492020-11-27 12:40:31 +0100103 case 'g': /* 2^30 */
Denys Vlasenko55429342010-10-01 21:57:59 +0200104 G.unit_steps = 30;
105 break;
106 default:
107 bb_show_usage();
108 }
109 }
Denys Vlasenkobef57112010-02-21 05:39:59 +0100110#endif
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100111 printf(" %12s%12s%12s%12s%12s%12s\n"
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100112 "Mem: ",
Denys Vlasenkobef57112010-02-21 05:39:59 +0100113 "total",
114 "used",
115 "free",
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100116 "shared", "buff/cache", "available" /* swap and total don't have these columns */
Denys Vlasenkobef57112010-02-21 05:39:59 +0100117 );
Denys Vlasenko55429342010-10-01 21:57:59 +0200118
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100119 sysinfo(&info);
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100120 /* Extract cached and memavailable from /proc/meminfo and convert to mem_units */
Lukas Rusak9663bbd2019-06-25 18:52:33 +0200121 seen_available = parse_meminfo(&G);
Denys Vlasenkofc635492020-11-27 12:40:31 +0100122 G.mem_unit = (info.mem_unit ? info.mem_unit : 1); /* kernels < 2.4.x return mem_unit==0, so cope */
Lukas Rusak9663bbd2019-06-25 18:52:33 +0200123 available = ((unsigned long long) G.available_kb * 1024) / G.mem_unit;
124 cached = ((unsigned long long) G.cached_kb * 1024) / G.mem_unit;
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100125 cached += info.bufferram;
Lukas Rusak9663bbd2019-06-25 18:52:33 +0200126 cached += ((unsigned long long) G.reclaimable_kb * 1024) / G.mem_unit;
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100127 cached_plus_free = cached + info.freeram;
Denys Vlasenko55429342010-10-01 21:57:59 +0200128
Denys Vlasenkofc635492020-11-27 12:40:31 +0100129/* In case (long long * G.mem_unit) can overflow, this can be used to reduce the chances */
130#if 0 //ENABLE_DESKTOP
131 while (!(G.mem_unit & 1) && G.unit_steps != 0) {
132 G.mem_unit >>= 1;
133 G.unit_steps--;
134 //bb_error_msg("mem_unit:%d unit_steps:%d", G.mem_unit, G.unit_steps);
135 }
136#endif
137
Denys Vlasenkof4709d72018-11-27 15:43:10 +0100138#define FIELDS_6 "%12llu %11llu %11llu %11llu %11llu %11llu\n"
139#define FIELDS_3 (FIELDS_6 + 6 + 7 + 7)
140#define FIELDS_2 (FIELDS_6 + 6 + 7 + 7 + 7)
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100141
142 printf(FIELDS_6,
Denys Vlasenko318c8112017-10-05 14:06:49 +0200143 scale(&G, info.totalram), //total
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100144 scale(&G, info.totalram - cached_plus_free), //used
Denys Vlasenko318c8112017-10-05 14:06:49 +0200145 scale(&G, info.freeram), //free
146 scale(&G, info.sharedram), //shared
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100147 scale(&G, cached), //buff/cache
148 scale(&G, available) //available
Denys Vlasenkobef57112010-02-21 05:39:59 +0100149 );
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100150 /* On kernels < 3.14, MemAvailable is not provided.
151 * Show alternate, more meaningful busy/free numbers by counting
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100152 * buffer cache as free memory. */
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100153 if (!seen_available) {
154 printf("-/+ buffers/cache: ");
155 printf(FIELDS_2,
156 scale(&G, info.totalram - cached_plus_free), //used
157 scale(&G, cached_plus_free) //free
158 );
159 }
Denys Vlasenko153fcaa2010-02-21 05:17:41 +0100160#if BB_MMU
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100161 printf("Swap: ");
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +0200162 printf(FIELDS_3,
Denys Vlasenko318c8112017-10-05 14:06:49 +0200163 scale(&G, info.totalswap), //total
164 scale(&G, info.totalswap - info.freeswap), //used
165 scale(&G, info.freeswap) //free
Denys Vlasenkobef57112010-02-21 05:39:59 +0100166 );
Eric Andersen1dcf2182003-01-11 20:40:49 +0000167#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +0000168 return EXIT_SUCCESS;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000169}