blob: 618664e082d1e502fe320f4cc2a96b308396bf1d [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 Vlasenkof8f81ed2016-11-23 06:23:44 +010018//applet:IF_FREE(APPLET(free, BB_DIR_USR_BIN, BB_SUID_DROP))
19
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 Vlasenkoe6a2f4c2016-04-21 16:26:30 +020035#include "common_bufsiz.h"
Denys Vlasenko67905e22011-07-26 13:42:12 +020036#ifdef __linux__
37# include <sys/sysinfo.h>
38#endif
Eric Andersenabc0f4f1999-12-08 23:19:36 +000039
Denys Vlasenko55429342010-10-01 21:57:59 +020040struct globals {
41 unsigned mem_unit;
42#if ENABLE_DESKTOP
43 unsigned unit_steps;
44# define G_unit_steps G.unit_steps
45#else
46# define G_unit_steps 10
47#endif
Denys Vlasenkoe8d0a142011-01-16 11:21:15 +010048} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020049#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020050#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denys Vlasenko55429342010-10-01 21:57:59 +020051
52
53static unsigned long long scale(unsigned long d)
54{
55 return ((unsigned long long)d * G.mem_unit) >> G_unit_steps;
56}
57
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010058static unsigned long parse_cached_kb(void)
59{
60 char buf[60]; /* actual lines we expect are ~30 chars or less */
61 FILE *fp;
62 unsigned long cached = 0;
63
64 fp = xfopen_for_read("/proc/meminfo");
65 while (fgets(buf, sizeof(buf), fp) != NULL) {
66 if (sscanf(buf, "Cached: %lu %*s\n", &cached) == 1)
67 break;
68 }
69 if (ENABLE_FEATURE_CLEAN_UP)
70 fclose(fp);
71
72 return cached;
73}
Denys Vlasenko55429342010-10-01 21:57:59 +020074
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000075int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischer7e7728c2010-02-26 09:28:30 +010076int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
Eric Andersenabc0f4f1999-12-08 23:19:36 +000077{
Erik Andersend07ee462000-02-21 21:26:32 +000078 struct sysinfo info;
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010079 unsigned long long cached;
Denys Vlasenko55429342010-10-01 21:57:59 +020080
81 INIT_G();
Denys Vlasenkobef57112010-02-21 05:39:59 +010082
83#if ENABLE_DESKTOP
Denys Vlasenko55429342010-10-01 21:57:59 +020084 G.unit_steps = 10;
85 if (argv[1] && argv[1][0] == '-') {
86 switch (argv[1][1]) {
87 case 'b':
88 G.unit_steps = 0;
89 break;
90 case 'k': /* 2^10 */
91 /* G.unit_steps = 10; - already is */
92 break;
93 case 'm': /* 2^(2*10) */
94 G.unit_steps = 20;
95 break;
96 case 'g': /* 2^(3*10) */
97 G.unit_steps = 30;
98 break;
99 default:
100 bb_show_usage();
101 }
102 }
Denys Vlasenkobef57112010-02-21 05:39:59 +0100103#endif
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100104 printf(" %11s%11s%11s%11s%11s%11s\n"
105 "Mem: ",
Denys Vlasenkobef57112010-02-21 05:39:59 +0100106 "total",
107 "used",
108 "free",
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100109 "shared", "buffers", "cached" /* swap and total don't have these columns */
Denys Vlasenkobef57112010-02-21 05:39:59 +0100110 );
Denys Vlasenko55429342010-10-01 21:57:59 +0200111
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100112 sysinfo(&info);
113 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
114 G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
115 /* Extract cached from /proc/meminfo and convert to mem_units */
116 cached = ((unsigned long long) parse_cached_kb() * 1024) / G.mem_unit;
Denys Vlasenko55429342010-10-01 21:57:59 +0200117
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100118#define FIELDS_6 "%11llu%11llu%11llu%11llu%11llu%11llu\n"
119#define FIELDS_3 (FIELDS_6 + 3*6)
120#define FIELDS_2 (FIELDS_6 + 4*6)
121
122 printf(FIELDS_6,
123 scale(info.totalram), //total
124 scale(info.totalram - info.freeram), //used
125 scale(info.freeram), //free
126 scale(info.sharedram), //shared
127 scale(info.bufferram), //buffers
128 scale(cached) //cached
Denys Vlasenkobef57112010-02-21 05:39:59 +0100129 );
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +0200130 /* Show alternate, more meaningful busy/free numbers by counting
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100131 * buffer cache as free memory. */
132 printf("-/+ buffers/cache:");
133 cached += info.freeram;
134 cached += info.bufferram;
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +0200135 printf(FIELDS_2,
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100136 scale(info.totalram - cached), //used
137 scale(cached) //free
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +0200138 );
Denys Vlasenko153fcaa2010-02-21 05:17:41 +0100139#if BB_MMU
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100140 printf("Swap: ");
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +0200141 printf(FIELDS_3,
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100142 scale(info.totalswap), //total
143 scale(info.totalswap - info.freeswap), //used
144 scale(info.freeswap) //free
Denys Vlasenkobef57112010-02-21 05:39:59 +0100145 );
Eric Andersen1dcf2182003-01-11 20:40:49 +0000146#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +0000147 return EXIT_SUCCESS;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000148}