aboutsummaryrefslogtreecommitdiff
path: root/procps
diff options
context:
space:
mode:
Diffstat (limited to 'procps')
-rw-r--r--procps/free.c52
1 files changed, 23 insertions, 29 deletions
diff --git a/procps/free.c b/procps/free.c
index b57e4a322..48139c4a3 100644
--- a/procps/free.c
+++ b/procps/free.c
@@ -15,7 +15,7 @@
15//config: memory in the system, as well as the buffers used by the kernel. 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. 16//config: The shared memory column should be ignored; it is obsolete.
17 17
18//applet:IF_FREE(APPLET_NOEXEC(free, free, BB_DIR_USR_BIN, BB_SUID_DROP, free)) 18//applet:IF_FREE(APPLET_NOFORK(free, free, BB_DIR_USR_BIN, BB_SUID_DROP, free))
19 19
20//kbuild:lib-$(CONFIG_FREE) += free.o 20//kbuild:lib-$(CONFIG_FREE) += free.o
21 21
@@ -32,7 +32,6 @@
32//usage: "Total: 386144 257128 129016\n" 32//usage: "Total: 386144 257128 129016\n"
33 33
34#include "libbb.h" 34#include "libbb.h"
35#include "common_bufsiz.h"
36#ifdef __linux__ 35#ifdef __linux__
37# include <sys/sysinfo.h> 36# include <sys/sysinfo.h>
38#endif 37#endif
@@ -40,25 +39,21 @@
40struct globals { 39struct globals {
41 unsigned mem_unit; 40 unsigned mem_unit;
42#if ENABLE_DESKTOP 41#if ENABLE_DESKTOP
43 unsigned unit_steps; 42 uint8_t unit_steps;
44# define G_unit_steps G.unit_steps 43# define G_unit_steps g->unit_steps
45#else 44#else
46# define G_unit_steps 10 45# define G_unit_steps 10
47#endif 46#endif
48} FIX_ALIASING; 47};
49#define G (*(struct globals*)bb_common_bufsiz1) 48/* Because of NOFORK, "globals" are not in global data */
50#define INIT_G() do { \
51 setup_common_bufsiz(); \
52 /* NB: noexec applet - globals not zeroed */ \
53} while (0)
54 49
55 50static unsigned long long scale(struct globals *g, unsigned long d)
56static unsigned long long scale(unsigned long d)
57{ 51{
58 return ((unsigned long long)d * G.mem_unit) >> G_unit_steps; 52 return ((unsigned long long)d * g->mem_unit) >> G_unit_steps;
59} 53}
60 54
61static unsigned long parse_cached_kb(void) 55/* NOINLINE reduces main() stack usage, which makes code smaller (on x86 at least) */
56static NOINLINE unsigned long parse_cached_kb(void)
62{ 57{
63 char buf[60]; /* actual lines we expect are ~30 chars or less */ 58 char buf[60]; /* actual lines we expect are ~30 chars or less */
64 FILE *fp; 59 FILE *fp;
@@ -69,8 +64,8 @@ static unsigned long parse_cached_kb(void)
69 if (sscanf(buf, "Cached: %lu %*s\n", &cached) == 1) 64 if (sscanf(buf, "Cached: %lu %*s\n", &cached) == 1)
70 break; 65 break;
71 } 66 }
72 if (ENABLE_FEATURE_CLEAN_UP) 67 /* Have to close because of NOFORK */
73 fclose(fp); 68 fclose(fp);
74 69
75 return cached; 70 return cached;
76} 71}
@@ -78,11 +73,10 @@ static unsigned long parse_cached_kb(void)
78int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 73int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
79int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) 74int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
80{ 75{
76 struct globals G;
81 struct sysinfo info; 77 struct sysinfo info;
82 unsigned long long cached; 78 unsigned long long cached;
83 79
84 INIT_G();
85
86#if ENABLE_DESKTOP 80#if ENABLE_DESKTOP
87 G.unit_steps = 10; 81 G.unit_steps = 10;
88 if (argv[1] && argv[1][0] == '-') { 82 if (argv[1] && argv[1][0] == '-') {
@@ -123,12 +117,12 @@ int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
123#define FIELDS_2 (FIELDS_6 + 4*6) 117#define FIELDS_2 (FIELDS_6 + 4*6)
124 118
125 printf(FIELDS_6, 119 printf(FIELDS_6,
126 scale(info.totalram), //total 120 scale(&G, info.totalram), //total
127 scale(info.totalram - info.freeram), //used 121 scale(&G, info.totalram - info.freeram), //used
128 scale(info.freeram), //free 122 scale(&G, info.freeram), //free
129 scale(info.sharedram), //shared 123 scale(&G, info.sharedram), //shared
130 scale(info.bufferram), //buffers 124 scale(&G, info.bufferram), //buffers
131 scale(cached) //cached 125 scale(&G, cached) //cached
132 ); 126 );
133 /* Show alternate, more meaningful busy/free numbers by counting 127 /* Show alternate, more meaningful busy/free numbers by counting
134 * buffer cache as free memory. */ 128 * buffer cache as free memory. */
@@ -136,15 +130,15 @@ int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
136 cached += info.freeram; 130 cached += info.freeram;
137 cached += info.bufferram; 131 cached += info.bufferram;
138 printf(FIELDS_2, 132 printf(FIELDS_2,
139 scale(info.totalram - cached), //used 133 scale(&G, info.totalram - cached), //used
140 scale(cached) //free 134 scale(&G, cached) //free
141 ); 135 );
142#if BB_MMU 136#if BB_MMU
143 printf("Swap: "); 137 printf("Swap: ");
144 printf(FIELDS_3, 138 printf(FIELDS_3,
145 scale(info.totalswap), //total 139 scale(&G, info.totalswap), //total
146 scale(info.totalswap - info.freeswap), //used 140 scale(&G, info.totalswap - info.freeswap), //used
147 scale(info.freeswap) //free 141 scale(&G, info.freeswap) //free
148 ); 142 );
149#endif 143#endif
150 return EXIT_SUCCESS; 144 return EXIT_SUCCESS;