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#include "libbb.h"
35#ifdef __linux__
36# include <sys/sysinfo.h>
37#endif
38
39struct 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};
48
49
50static unsigned long long scale(struct globals *g, unsigned long d)
51{
52 return ((unsigned long long)d * g->mem_unit) >> G_unit_steps;
53}
54
55
56static NOINLINE unsigned long parse_cached_kb(void)
57{
58 char buf[60];
59 FILE *fp;
60 unsigned long cached = 0;
61
62 fp = xfopen_for_read("/proc/meminfo");
63 while (fgets(buf, sizeof(buf), fp) != NULL) {
64 if (sscanf(buf, "Cached: %lu %*s\n", &cached) == 1)
65 break;
66 }
67
68 fclose(fp);
69
70 return cached;
71}
72
73int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
74int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
75{
76 struct globals G;
77 struct sysinfo info;
78 unsigned long long cached;
79
80#if ENABLE_DESKTOP
81 G.unit_steps = 10;
82 if (argv[1] && argv[1][0] == '-') {
83 switch (argv[1][1]) {
84 case 'b':
85 G.unit_steps = 0;
86 break;
87 case 'k':
88
89 break;
90 case 'm':
91 G.unit_steps = 20;
92 break;
93 case 'g':
94 G.unit_steps = 30;
95 break;
96 default:
97 bb_show_usage();
98 }
99 }
100#endif
101 printf(" %11s%11s%11s%11s%11s%11s\n"
102 "Mem: ",
103 "total",
104 "used",
105 "free",
106 "shared", "buffers", "cached"
107 );
108
109 sysinfo(&info);
110
111 G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
112
113 cached = ((unsigned long long) parse_cached_kb() * 1024) / G.mem_unit;
114
115#define FIELDS_6 "%11llu%11llu%11llu%11llu%11llu%11llu\n"
116#define FIELDS_3 (FIELDS_6 + 3*6)
117#define FIELDS_2 (FIELDS_6 + 4*6)
118
119 printf(FIELDS_6,
120 scale(&G, info.totalram),
121 scale(&G, info.totalram - info.freeram),
122 scale(&G, info.freeram),
123 scale(&G, info.sharedram),
124 scale(&G, info.bufferram),
125 scale(&G, cached)
126 );
127
128
129 printf("-/+ buffers/cache:");
130 cached += info.freeram;
131 cached += info.bufferram;
132 printf(FIELDS_2,
133 scale(&G, info.totalram - cached),
134 scale(&G, cached)
135 );
136#if BB_MMU
137 printf("Swap: ");
138 printf(FIELDS_3,
139 scale(&G, info.totalswap),
140 scale(&G, info.totalswap - info.freeswap),
141 scale(&G, info.freeswap)
142 );
143#endif
144 return EXIT_SUCCESS;
145}
146