1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54#include "libbb.h"
55#ifdef __linux__
56# include <sys/sysinfo.h>
57#endif
58
59struct globals {
60 unsigned mem_unit;
61#if ENABLE_DESKTOP
62 unsigned unit;
63# define G_unit g->unit
64#else
65# define G_unit (1 << 10)
66#endif
67 unsigned long cached_kb, available_kb, reclaimable_kb;
68};
69
70
71static const char *scale(struct globals *g, unsigned long d)
72{
73
74
75
76
77 return make_human_readable_str(d, g->mem_unit, G_unit);
78}
79
80
81static NOINLINE unsigned int parse_meminfo(struct globals *g)
82{
83 char buf[60];
84 FILE *fp;
85 int seen_cached_and_available_and_reclaimable;
86
87 fp = xfopen_for_read("/proc/meminfo");
88 g->cached_kb = g->available_kb = g->reclaimable_kb = 0;
89 seen_cached_and_available_and_reclaimable = 3;
90 while (fgets(buf, sizeof(buf), fp)) {
91 if (sscanf(buf, "Cached: %lu %*s\n", &g->cached_kb) == 1)
92 if (--seen_cached_and_available_and_reclaimable == 0)
93 break;
94 if (sscanf(buf, "MemAvailable: %lu %*s\n", &g->available_kb) == 1)
95 if (--seen_cached_and_available_and_reclaimable == 0)
96 break;
97 if (sscanf(buf, "SReclaimable: %lu %*s\n", &g->reclaimable_kb) == 1)
98 if (--seen_cached_and_available_and_reclaimable == 0)
99 break;
100 }
101
102 fclose(fp);
103
104 return seen_cached_and_available_and_reclaimable == 0;
105}
106
107int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
108int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
109{
110 struct globals G;
111 struct sysinfo info;
112 unsigned long long cached, cached_plus_free, available;
113 int seen_available;
114
115#if ENABLE_DESKTOP
116 G.unit = 1 << 10;
117 if (argv[1] && argv[1][0] == '-') {
118 switch (argv[1][1]) {
119 case 'b':
120 G.unit = 1;
121 break;
122 case 'k':
123
124 break;
125 case 'm':
126 G.unit = 1 << 20;
127 break;
128 case 'g':
129 G.unit = 1 << 30;
130 break;
131
132
133
134
135 case 'h':
136 G.unit = 0;
137 break;
138 default:
139 bb_show_usage();
140 }
141 }
142#endif
143 printf(" %12s%12s%12s%12s%12s%12s\n"
144 "Mem: ",
145 "total",
146 "used",
147 "free",
148 "shared", "buff/cache", "available"
149 );
150
151 sysinfo(&info);
152
153 seen_available = parse_meminfo(&G);
154 G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
155 available = ((unsigned long long) G.available_kb * 1024) / G.mem_unit;
156 cached = ((unsigned long long) G.cached_kb * 1024) / G.mem_unit;
157 cached += info.bufferram;
158 cached += ((unsigned long long) G.reclaimable_kb * 1024) / G.mem_unit;
159 cached_plus_free = cached + info.freeram;
160
161 printf("%12s%12s%12s",
162 scale(&G, info.totalram),
163 scale(&G, info.totalram - cached_plus_free),
164 scale(&G, info.freeram)
165 );
166
167 printf("%12s%12s%12s\n",
168 scale(&G, info.sharedram),
169 scale(&G, cached),
170 scale(&G, available)
171 );
172
173
174
175 if (!seen_available) {
176 printf("-/+ buffers/cache: ");
177 printf("%12s%12s%12s\n" + 4,
178 scale(&G, info.totalram - cached_plus_free),
179 scale(&G, cached_plus_free)
180 );
181 }
182#if BB_MMU
183 printf("Swap: ");
184 printf("%12s%12s%12s\n",
185 scale(&G, info.totalswap),
186 scale(&G, info.totalswap - info.freeswap),
187 scale(&G, info.freeswap)
188 );
189#endif
190 return EXIT_SUCCESS;
191}
192