blob: 0d023f7401871f7a9be6f162bd2320b79e3b3dae [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"
Denys Vlasenko67905e22011-07-26 13:42:12 +020025#ifdef __linux__
26# include <sys/sysinfo.h>
27#endif
Eric Andersenabc0f4f1999-12-08 23:19:36 +000028
Denys Vlasenko55429342010-10-01 21:57:59 +020029struct globals {
30 unsigned mem_unit;
31#if ENABLE_DESKTOP
32 unsigned unit_steps;
33# define G_unit_steps G.unit_steps
34#else
35# define G_unit_steps 10
36#endif
Denys Vlasenkoe8d0a142011-01-16 11:21:15 +010037} FIX_ALIASING;
Denys Vlasenko55429342010-10-01 21:57:59 +020038#define G (*(struct globals*)&bb_common_bufsiz1)
39#define INIT_G() do { } while (0)
40
41
42static unsigned long long scale(unsigned long d)
43{
44 return ((unsigned long long)d * G.mem_unit) >> G_unit_steps;
45}
46
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010047static unsigned long parse_cached_kb(void)
48{
49 char buf[60]; /* actual lines we expect are ~30 chars or less */
50 FILE *fp;
51 unsigned long cached = 0;
52
53 fp = xfopen_for_read("/proc/meminfo");
54 while (fgets(buf, sizeof(buf), fp) != NULL) {
55 if (sscanf(buf, "Cached: %lu %*s\n", &cached) == 1)
56 break;
57 }
58 if (ENABLE_FEATURE_CLEAN_UP)
59 fclose(fp);
60
61 return cached;
62}
Denys Vlasenko55429342010-10-01 21:57:59 +020063
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000064int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischer7e7728c2010-02-26 09:28:30 +010065int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
Eric Andersenabc0f4f1999-12-08 23:19:36 +000066{
Erik Andersend07ee462000-02-21 21:26:32 +000067 struct sysinfo info;
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010068 unsigned long long cached;
Denys Vlasenko55429342010-10-01 21:57:59 +020069
70 INIT_G();
Denys Vlasenkobef57112010-02-21 05:39:59 +010071
72#if ENABLE_DESKTOP
Denys Vlasenko55429342010-10-01 21:57:59 +020073 G.unit_steps = 10;
74 if (argv[1] && argv[1][0] == '-') {
75 switch (argv[1][1]) {
76 case 'b':
77 G.unit_steps = 0;
78 break;
79 case 'k': /* 2^10 */
80 /* G.unit_steps = 10; - already is */
81 break;
82 case 'm': /* 2^(2*10) */
83 G.unit_steps = 20;
84 break;
85 case 'g': /* 2^(3*10) */
86 G.unit_steps = 30;
87 break;
88 default:
89 bb_show_usage();
90 }
91 }
Denys Vlasenkobef57112010-02-21 05:39:59 +010092#endif
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010093 printf(" %11s%11s%11s%11s%11s%11s\n"
94 "Mem: ",
Denys Vlasenkobef57112010-02-21 05:39:59 +010095 "total",
96 "used",
97 "free",
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010098 "shared", "buffers", "cached" /* swap and total don't have these columns */
Denys Vlasenkobef57112010-02-21 05:39:59 +010099 );
Denys Vlasenko55429342010-10-01 21:57:59 +0200100
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100101 sysinfo(&info);
102 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
103 G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
104 /* Extract cached from /proc/meminfo and convert to mem_units */
105 cached = ((unsigned long long) parse_cached_kb() * 1024) / G.mem_unit;
Denys Vlasenko55429342010-10-01 21:57:59 +0200106
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100107#define FIELDS_6 "%11llu%11llu%11llu%11llu%11llu%11llu\n"
108#define FIELDS_3 (FIELDS_6 + 3*6)
109#define FIELDS_2 (FIELDS_6 + 4*6)
110
111 printf(FIELDS_6,
112 scale(info.totalram), //total
113 scale(info.totalram - info.freeram), //used
114 scale(info.freeram), //free
115 scale(info.sharedram), //shared
116 scale(info.bufferram), //buffers
117 scale(cached) //cached
Denys Vlasenkobef57112010-02-21 05:39:59 +0100118 );
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +0200119 /* Show alternate, more meaningful busy/free numbers by counting
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100120 * buffer cache as free memory. */
121 printf("-/+ buffers/cache:");
122 cached += info.freeram;
123 cached += info.bufferram;
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +0200124 printf(FIELDS_2,
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100125 scale(info.totalram - cached), //used
126 scale(cached) //free
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +0200127 );
Denys Vlasenko153fcaa2010-02-21 05:17:41 +0100128#if BB_MMU
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100129 printf("Swap: ");
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +0200130 printf(FIELDS_3,
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100131 scale(info.totalswap), //total
132 scale(info.totalswap - info.freeswap), //used
133 scale(info.freeswap) //free
Denys Vlasenkobef57112010-02-21 05:39:59 +0100134 );
Eric Andersen1dcf2182003-01-11 20:40:49 +0000135#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +0000136 return EXIT_SUCCESS;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000137}