brctl: tweak help text, fix comments
[oweals/busybox.git] / procps / free.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini free implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2, see file LICENSE in this source tree.
8  */
9 //config:config FREE
10 //config:       bool "free (3.1 kb)"
11 //config:       default y
12 //config:       select PLATFORM_LINUX #sysinfo()
13 //config:       help
14 //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.
17
18 //applet:IF_FREE(APPLET_NOFORK(free, free, BB_DIR_USR_BIN, BB_SUID_DROP, free))
19
20 //kbuild:lib-$(CONFIG_FREE) += free.o
21
22 //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
34 #include "libbb.h"
35 #ifdef __linux__
36 # include <sys/sysinfo.h>
37 #endif
38
39 struct globals {
40         unsigned mem_unit;
41 #if ENABLE_DESKTOP
42         uint8_t unit_steps;
43 # define G_unit_steps g->unit_steps
44 #else
45 # define G_unit_steps 10
46 #endif
47         unsigned long cached_kb, available_kb, reclaimable_kb;
48 };
49 /* Because of NOFORK, "globals" are not in global data */
50
51 static unsigned long long scale(struct globals *g, unsigned long d)
52 {
53         return ((unsigned long long)d * g->mem_unit) >> G_unit_steps;
54 }
55
56 /* NOINLINE reduces main() stack usage, which makes code smaller (on x86 at least) */
57 static NOINLINE unsigned int parse_meminfo(struct globals *g)
58 {
59         char buf[60]; /* actual lines we expect are ~30 chars or less */
60         FILE *fp;
61         int seen_cached_and_available_and_reclaimable;
62
63         fp = xfopen_for_read("/proc/meminfo");
64         g->cached_kb = g->available_kb = g->reclaimable_kb = 0;
65         seen_cached_and_available_and_reclaimable = 3;
66         while (fgets(buf, sizeof(buf), fp)) {
67                 if (sscanf(buf, "Cached: %lu %*s\n", &g->cached_kb) == 1)
68                         if (--seen_cached_and_available_and_reclaimable == 0)
69                                 break;
70                 if (sscanf(buf, "MemAvailable: %lu %*s\n", &g->available_kb) == 1)
71                         if (--seen_cached_and_available_and_reclaimable == 0)
72                                 break;
73                 if (sscanf(buf, "SReclaimable: %lu %*s\n", &g->reclaimable_kb) == 1)
74                         if (--seen_cached_and_available_and_reclaimable == 0)
75                                 break;
76         }
77         /* Have to close because of NOFORK */
78         fclose(fp);
79
80         return seen_cached_and_available_and_reclaimable == 0;
81 }
82
83 int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
84 int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
85 {
86         struct globals G;
87         struct sysinfo info;
88         unsigned long long cached, cached_plus_free, available;
89         int seen_available;
90
91 #if ENABLE_DESKTOP
92         G.unit_steps = 10;
93         if (argv[1] && argv[1][0] == '-') {
94                 switch (argv[1][1]) {
95                 case 'b':
96                         G.unit_steps = 0;
97                         break;
98                 case 'k': /* 2^10 */
99                         /* G.unit_steps = 10; - already is */
100                         break;
101                 case 'm': /* 2^(2*10) */
102                         G.unit_steps = 20;
103                         break;
104                 case 'g': /* 2^(3*10) */
105                         G.unit_steps = 30;
106                         break;
107                 default:
108                         bb_show_usage();
109                 }
110         }
111 #endif
112         printf("       %12s%12s%12s%12s%12s%12s\n"
113         "Mem:   ",
114                 "total",
115                 "used",
116                 "free",
117                 "shared", "buff/cache", "available" /* swap and total don't have these columns */
118         );
119
120         sysinfo(&info);
121         /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
122         G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
123         /* Extract cached and memavailable from /proc/meminfo and convert to mem_units */
124         seen_available = parse_meminfo(&G);
125         available = ((unsigned long long) G.available_kb * 1024) / G.mem_unit;
126         cached = ((unsigned long long) G.cached_kb * 1024) / G.mem_unit;
127         cached += info.bufferram;
128         cached += ((unsigned long long) G.reclaimable_kb * 1024) / G.mem_unit;
129         cached_plus_free = cached + info.freeram;
130
131 #define FIELDS_6 "%12llu %11llu %11llu %11llu %11llu %11llu\n"
132 #define FIELDS_3 (FIELDS_6 + 6 + 7 + 7)
133 #define FIELDS_2 (FIELDS_6 + 6 + 7 + 7 + 7)
134
135         printf(FIELDS_6,
136                 scale(&G, info.totalram),                //total
137                 scale(&G, info.totalram - cached_plus_free), //used
138                 scale(&G, info.freeram),                 //free
139                 scale(&G, info.sharedram),               //shared
140                 scale(&G, cached),                       //buff/cache
141                 scale(&G, available)                     //available
142         );
143         /* On kernels < 3.14, MemAvailable is not provided.
144          * Show alternate, more meaningful busy/free numbers by counting
145          * buffer cache as free memory. */
146         if (!seen_available) {
147                 printf("-/+ buffers/cache: ");
148                 printf(FIELDS_2,
149                         scale(&G, info.totalram - cached_plus_free), //used
150                         scale(&G, cached_plus_free)                  //free
151                 );
152         }
153 #if BB_MMU
154         printf("Swap:  ");
155         printf(FIELDS_3,
156                 scale(&G, info.totalswap),                 //total
157                 scale(&G, info.totalswap - info.freeswap), //used
158                 scale(&G, info.freeswap)                   //free
159         );
160 #endif
161         return EXIT_SUCCESS;
162 }