blob: 9fde64b64b714e7d0c2d95033beaa0e51a9bf672 [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 Vlasenkoe6a2f4c2016-04-21 16:26:30 +020025#include "common_bufsiz.h"
Denys Vlasenko67905e22011-07-26 13:42:12 +020026#ifdef __linux__
27# include <sys/sysinfo.h>
28#endif
Eric Andersenabc0f4f1999-12-08 23:19:36 +000029
Denys Vlasenko55429342010-10-01 21:57:59 +020030struct globals {
31 unsigned mem_unit;
32#if ENABLE_DESKTOP
33 unsigned unit_steps;
34# define G_unit_steps G.unit_steps
35#else
36# define G_unit_steps 10
37#endif
Denys Vlasenkoe8d0a142011-01-16 11:21:15 +010038} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020039#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko55429342010-10-01 21:57:59 +020040#define INIT_G() do { } while (0)
41
42
43static unsigned long long scale(unsigned long d)
44{
45 return ((unsigned long long)d * G.mem_unit) >> G_unit_steps;
46}
47
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010048static unsigned long parse_cached_kb(void)
49{
50 char buf[60]; /* actual lines we expect are ~30 chars or less */
51 FILE *fp;
52 unsigned long cached = 0;
53
54 fp = xfopen_for_read("/proc/meminfo");
55 while (fgets(buf, sizeof(buf), fp) != NULL) {
56 if (sscanf(buf, "Cached: %lu %*s\n", &cached) == 1)
57 break;
58 }
59 if (ENABLE_FEATURE_CLEAN_UP)
60 fclose(fp);
61
62 return cached;
63}
Denys Vlasenko55429342010-10-01 21:57:59 +020064
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000065int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischer7e7728c2010-02-26 09:28:30 +010066int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
Eric Andersenabc0f4f1999-12-08 23:19:36 +000067{
Erik Andersend07ee462000-02-21 21:26:32 +000068 struct sysinfo info;
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010069 unsigned long long cached;
Denys Vlasenko55429342010-10-01 21:57:59 +020070
71 INIT_G();
Denys Vlasenkobef57112010-02-21 05:39:59 +010072
73#if ENABLE_DESKTOP
Denys Vlasenko55429342010-10-01 21:57:59 +020074 G.unit_steps = 10;
75 if (argv[1] && argv[1][0] == '-') {
76 switch (argv[1][1]) {
77 case 'b':
78 G.unit_steps = 0;
79 break;
80 case 'k': /* 2^10 */
81 /* G.unit_steps = 10; - already is */
82 break;
83 case 'm': /* 2^(2*10) */
84 G.unit_steps = 20;
85 break;
86 case 'g': /* 2^(3*10) */
87 G.unit_steps = 30;
88 break;
89 default:
90 bb_show_usage();
91 }
92 }
Denys Vlasenkobef57112010-02-21 05:39:59 +010093#endif
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010094 printf(" %11s%11s%11s%11s%11s%11s\n"
95 "Mem: ",
Denys Vlasenkobef57112010-02-21 05:39:59 +010096 "total",
97 "used",
98 "free",
Guillermo Rodriguez75a14032015-01-05 18:34:53 +010099 "shared", "buffers", "cached" /* swap and total don't have these columns */
Denys Vlasenkobef57112010-02-21 05:39:59 +0100100 );
Denys Vlasenko55429342010-10-01 21:57:59 +0200101
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100102 sysinfo(&info);
103 /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
104 G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
105 /* Extract cached from /proc/meminfo and convert to mem_units */
106 cached = ((unsigned long long) parse_cached_kb() * 1024) / G.mem_unit;
Denys Vlasenko55429342010-10-01 21:57:59 +0200107
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100108#define FIELDS_6 "%11llu%11llu%11llu%11llu%11llu%11llu\n"
109#define FIELDS_3 (FIELDS_6 + 3*6)
110#define FIELDS_2 (FIELDS_6 + 4*6)
111
112 printf(FIELDS_6,
113 scale(info.totalram), //total
114 scale(info.totalram - info.freeram), //used
115 scale(info.freeram), //free
116 scale(info.sharedram), //shared
117 scale(info.bufferram), //buffers
118 scale(cached) //cached
Denys Vlasenkobef57112010-02-21 05:39:59 +0100119 );
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +0200120 /* Show alternate, more meaningful busy/free numbers by counting
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100121 * buffer cache as free memory. */
122 printf("-/+ buffers/cache:");
123 cached += info.freeram;
124 cached += info.bufferram;
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +0200125 printf(FIELDS_2,
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100126 scale(info.totalram - cached), //used
127 scale(cached) //free
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +0200128 );
Denys Vlasenko153fcaa2010-02-21 05:17:41 +0100129#if BB_MMU
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100130 printf("Swap: ");
Denys Vlasenko7bfbbd42010-08-12 01:56:44 +0200131 printf(FIELDS_3,
Guillermo Rodriguez75a14032015-01-05 18:34:53 +0100132 scale(info.totalswap), //total
133 scale(info.totalswap - info.freeswap), //used
134 scale(info.freeswap) //free
Denys Vlasenkobef57112010-02-21 05:39:59 +0100135 );
Eric Andersen1dcf2182003-01-11 20:40:49 +0000136#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +0000137 return EXIT_SUCCESS;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000138}