blob: c734f757d146642725aa499f92d26ec71d5a7041 [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
Samuel Thibault77216c32022-10-16 02:04:59 +020012//config: select PLATFORM_LINUX
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010013//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
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +020023//usage: "" IF_DESKTOP("[-bkmgh]")
Pere Orga5bc8c002011-04-11 03:29:49 +020024//usage:#define free_full_usage "\n\n"
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +020025//usage: "Display free and used memory"
Pere Orga5bc8c002011-04-11 03:29:49 +020026//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"
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +020033//procps-ng 3.3.15:
34// -b, --bytes show output in bytes
35// --kilo show output in kilobytes
36// --mega show output in megabytes
37// --giga show output in gigabytes
38// --tera show output in terabytes
39// --peta show output in petabytes
40// -k, --kibi show output in kibibytes
41// -m, --mebi show output in mebibytes
42// -g, --gibi show output in gibibytes
43// --tebi show output in tebibytes
44// --pebi show output in pebibytes
45// -h, --human show human-readable output
46// --si use powers of 1000 not 1024
47// -l, --lohi show detailed low and high memory statistics
48// -t, --total show total for RAM + swap
49// -s N, --seconds N repeat printing every N seconds
50// -c N, --count N repeat printing N times, then exit
51// -w, --wide wide output
52//
53//NB: if we implement -s or -c, need to stop being NOFORK!
Pere Orga5bc8c002011-04-11 03:29:49 +020054
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000055#include "libbb.h"
Denys Vlasenko67905e22011-07-26 13:42:12 +020056#ifdef __linux__
57# include <sys/sysinfo.h>
58#endif
Eric Andersenabc0f4f1999-12-08 23:19:36 +000059
Denys Vlasenko55429342010-10-01 21:57:59 +020060struct globals {
61 unsigned mem_unit;
62#if ENABLE_DESKTOP
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +020063 unsigned unit;
64# define G_unit g->unit
Denys Vlasenko55429342010-10-01 21:57:59 +020065#else
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +020066# define G_unit (1 << 10)
Denys Vlasenko55429342010-10-01 21:57:59 +020067#endif
Lukas Rusak9663bbd2019-06-25 18:52:33 +020068 unsigned long cached_kb, available_kb, reclaimable_kb;
Denys Vlasenko318c8112017-10-05 14:06:49 +020069};
70/* Because of NOFORK, "globals" are not in global data */
Denys Vlasenko55429342010-10-01 21:57:59 +020071
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +020072static const char *scale(struct globals *g, unsigned long d)
Denys Vlasenko55429342010-10-01 21:57:59 +020073{
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +020074 /* Display (size * block_size) with one decimal digit.
75 * If display_unit == 0, show value no bigger than 1024 with suffix (K,M,G...),
76 * else divide by display_unit and do not use suffix.
77 * Returns "auto pointer" */
78 return make_human_readable_str(d, g->mem_unit, G_unit);
Denys Vlasenko55429342010-10-01 21:57:59 +020079}
80
Denys Vlasenko318c8112017-10-05 14:06:49 +020081/* NOINLINE reduces main() stack usage, which makes code smaller (on x86 at least) */
Lukas Rusak9663bbd2019-06-25 18:52:33 +020082static NOINLINE unsigned int parse_meminfo(struct globals *g)
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010083{
84 char buf[60]; /* actual lines we expect are ~30 chars or less */
85 FILE *fp;
Lukas Rusak9663bbd2019-06-25 18:52:33 +020086 int seen_cached_and_available_and_reclaimable;
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010087
88 fp = xfopen_for_read("/proc/meminfo");
Lukas Rusak9663bbd2019-06-25 18:52:33 +020089 g->cached_kb = g->available_kb = g->reclaimable_kb = 0;
90 seen_cached_and_available_and_reclaimable = 3;
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +010091 while (fgets(buf, sizeof(buf), fp)) {
Lukas Rusak9663bbd2019-06-25 18:52:33 +020092 if (sscanf(buf, "Cached: %lu %*s\n", &g->cached_kb) == 1)
93 if (--seen_cached_and_available_and_reclaimable == 0)
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +010094 break;
Lukas Rusak9663bbd2019-06-25 18:52:33 +020095 if (sscanf(buf, "MemAvailable: %lu %*s\n", &g->available_kb) == 1)
96 if (--seen_cached_and_available_and_reclaimable == 0)
97 break;
98 if (sscanf(buf, "SReclaimable: %lu %*s\n", &g->reclaimable_kb) == 1)
99 if (--seen_cached_and_available_and_reclaimable == 0)
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100100 break;
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100101 }
Denys Vlasenko318c8112017-10-05 14:06:49 +0200102 /* Have to close because of NOFORK */
103 fclose(fp);
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100104
Lukas Rusak9663bbd2019-06-25 18:52:33 +0200105 return seen_cached_and_available_and_reclaimable == 0;
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100106}
Denys Vlasenko55429342010-10-01 21:57:59 +0200107
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000108int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischer7e7728c2010-02-26 09:28:30 +0100109int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000110{
Denys Vlasenko318c8112017-10-05 14:06:49 +0200111 struct globals G;
Erik Andersend07ee462000-02-21 21:26:32 +0000112 struct sysinfo info;
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100113 unsigned long long cached, cached_plus_free, available;
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100114 int seen_available;
Denys Vlasenko55429342010-10-01 21:57:59 +0200115
Denys Vlasenkobef57112010-02-21 05:39:59 +0100116#if ENABLE_DESKTOP
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +0200117 G.unit = 1 << 10;
Denys Vlasenko55429342010-10-01 21:57:59 +0200118 if (argv[1] && argv[1][0] == '-') {
119 switch (argv[1][1]) {
120 case 'b':
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +0200121 G.unit = 1;
Denys Vlasenko55429342010-10-01 21:57:59 +0200122 break;
123 case 'k': /* 2^10 */
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +0200124 /* G.unit = 1 << 10; - already is */
Denys Vlasenko55429342010-10-01 21:57:59 +0200125 break;
Denys Vlasenkofc635492020-11-27 12:40:31 +0100126 case 'm': /* 2^20 */
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +0200127 G.unit = 1 << 20;
Denys Vlasenko55429342010-10-01 21:57:59 +0200128 break;
Denys Vlasenkofc635492020-11-27 12:40:31 +0100129 case 'g': /* 2^30 */
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +0200130 G.unit = 1 << 30;
131 break;
132// case 't':
133// -- WRONG, -t is not "terabytes" in procps-ng, it's --total
134// G.unit = 1 << 40;
135// break;
136 case 'h':
137 G.unit = 0; /* human readable */
Denys Vlasenko55429342010-10-01 21:57:59 +0200138 break;
139 default:
140 bb_show_usage();
141 }
142 }
Denys Vlasenkobef57112010-02-21 05:39:59 +0100143#endif
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100144 printf(" %12s%12s%12s%12s%12s%12s\n"
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100145 "Mem: ",
Denys Vlasenkobef57112010-02-21 05:39:59 +0100146 "total",
147 "used",
148 "free",
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100149 "shared", "buff/cache", "available" /* swap and total don't have these columns */
Denys Vlasenkobef57112010-02-21 05:39:59 +0100150 );
Denys Vlasenko55429342010-10-01 21:57:59 +0200151
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100152 sysinfo(&info);
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100153 /* Extract cached and memavailable from /proc/meminfo and convert to mem_units */
Lukas Rusak9663bbd2019-06-25 18:52:33 +0200154 seen_available = parse_meminfo(&G);
Denys Vlasenkofc635492020-11-27 12:40:31 +0100155 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 +0200156 available = ((unsigned long long) G.available_kb * 1024) / G.mem_unit;
157 cached = ((unsigned long long) G.cached_kb * 1024) / G.mem_unit;
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100158 cached += info.bufferram;
Lukas Rusak9663bbd2019-06-25 18:52:33 +0200159 cached += ((unsigned long long) G.reclaimable_kb * 1024) / G.mem_unit;
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100160 cached_plus_free = cached + info.freeram;
Denys Vlasenko55429342010-10-01 21:57:59 +0200161
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +0200162 printf("%12s%12s%12s",
Denys Vlasenko318c8112017-10-05 14:06:49 +0200163 scale(&G, info.totalram), //total
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100164 scale(&G, info.totalram - cached_plus_free), //used
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +0200165 scale(&G, info.freeram) //free
166 );
167 /* using two printf's: only 4 auto strings are supported, we need 6 */
168 printf("%12s%12s%12s\n",
Denys Vlasenko318c8112017-10-05 14:06:49 +0200169 scale(&G, info.sharedram), //shared
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100170 scale(&G, cached), //buff/cache
171 scale(&G, available) //available
Denys Vlasenkobef57112010-02-21 05:39:59 +0100172 );
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100173 /* On kernels < 3.14, MemAvailable is not provided.
174 * Show alternate, more meaningful busy/free numbers by counting
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100175 * buffer cache as free memory. */
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100176 if (!seen_available) {
177 printf("-/+ buffers/cache: ");
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +0200178 printf("%12s%12s%12s\n" + 4,
Guillermo Rodriguezc8e39222018-10-30 13:49:51 +0100179 scale(&G, info.totalram - cached_plus_free), //used
180 scale(&G, cached_plus_free) //free
181 );
182 }
Denys Vlasenko153fcaa2010-02-21 05:17:41 +0100183#if BB_MMU
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100184 printf("Swap: ");
Denys Vlasenkodc30f3d2021-06-18 12:08:02 +0200185 printf("%12s%12s%12s\n",
Denys Vlasenko318c8112017-10-05 14:06:49 +0200186 scale(&G, info.totalswap), //total
187 scale(&G, info.totalswap - info.freeswap), //used
188 scale(&G, info.freeswap) //free
Denys Vlasenkobef57112010-02-21 05:39:59 +0100189 );
Eric Andersen1dcf2182003-01-11 20:40:49 +0000190#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +0000191 return EXIT_SUCCESS;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000192}