summaryrefslogtreecommitdiffstats
path: root/main/busybox/0001-top-fix-and-merge-code-to-parse-proc-meminfo.patch
blob: 86fa9707aa9616e986c2086a45e0a10b3747c566 (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
From 7df1f1dda1f997c44800d16a9a12cf6cae2ed7e7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timo=20Ter=C3=A4s?= <timo.teras@iki.fi>
Date: Mon, 21 Jul 2014 14:14:24 +0300
Subject: [PATCH] top: fix and merge code to parse /proc/meminfo
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

display_header() code to parse meminfo as is was buggy:
- uninitialized variables were used if meminfo was not as expected
- meminfo parsing failed on new kernels (3.14+) as new field 'MemAvailable'
  was introduced between MemFree and Buffers
- shared memory was handled only for ancient kernels (2.4.x and earlier)

as result Buffers and shared memory fields were shown with bogus values
on current kernels.

The new code does not try to parse the old style summary header, as the
separated fields are always present (it saves code size). Additionally,
both Shmem (2.6+) and MemShared (2.4 and earlier) fields are now parsed
and summed for shared memory usage; as only one of them exists depending
on kernel version.

display_topmem_header() parses also meminfo so this makes it use the
same code for code shrink.

function                                             old     new   delta
display_header                                         -     681    +681
display_topmem_process_list                          465     684    +219
parse_meminfo                                          -     189    +189
static.fields                                          -     106    +106
static.match                                         132       -    -132
.rodata                                           120254  120117    -137
display_topmem_header                                513       -    -513
display_process_list                                1705     667   -1038
------------------------------------------------------------------------------
(add/remove: 3/2 grow/shrink: 1/2 up/down: 1195/-1820)       Total: -625 bytes

Signed-off-by: Timo Teräs <timo.teras@iki.fi>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
---
 procps/top.c | 196 ++++++++++++++++++++++++++---------------------------------
 1 file changed, 86 insertions(+), 110 deletions(-)

diff --git a/procps/top.c b/procps/top.c
index 530f45f..62f9421 100644
--- a/procps/top.c
+++ b/procps/top.c
@@ -499,85 +499,94 @@ static void display_cpus(int scr_width, char *scrbuf, int *lines_rem_p)
 # define display_cpus(scr_width, scrbuf, lines_rem) ((void)0)
 #endif
 
-static unsigned long display_header(int scr_width, int *lines_rem_p)
-{
-	FILE *fp;
-	char buf[80];
-	char scrbuf[80];
-	unsigned long total, used, mfree, shared, buffers, cached;
+enum {
+	MI_MEMTOTAL,
+	MI_MEMFREE,
+	MI_MEMSHARED,
+	MI_SHMEM,
+	MI_BUFFERS,
+	MI_CACHED,
+	MI_SWAPTOTAL,
+	MI_SWAPFREE,
+	MI_DIRTY,
+	MI_WRITEBACK,
+	MI_ANONPAGES,
+	MI_MAPPED,
+	MI_SLAB,
+	MI_MAX
+};
 
-	/* read memory info */
-	fp = xfopen_for_read("meminfo");
+static void parse_meminfo(unsigned long meminfo[MI_MAX])
+{
+	static const char fields[] =
+		"MemTotal\0"
+		"MemFree\0"
+		"MemShared\0"
+		"Shmem\0"
+		"Buffers\0"
+		"Cached\0"
+		"SwapTotal\0"
+		"SwapFree\0"
+		"Dirty\0"
+		"Writeback\0"
+		"AnonPages\0"
+		"Mapped\0"
+		"Slab\0";
+	char buf[60]; /* actual lines we expect are ~30 chars or less */
+	FILE *f;
+	int i;
 
-	/*
-	 * Old kernels (such as 2.4.x) had a nice summary of memory info that
-	 * we could parse, however this is gone entirely in 2.6. Try parsing
-	 * the old way first, and if that fails, parse each field manually.
-	 *
-	 * First, we read in the first line. Old kernels will have bogus
-	 * strings we don't care about, whereas new kernels will start right
-	 * out with MemTotal:
-	 *                              -- PFM.
-	 */
-	if (fscanf(fp, "MemTotal: %lu %s\n", &total, buf) != 2) {
-		fgets(buf, sizeof(buf), fp);    /* skip first line */
-
-		fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu",
-			&total, &used, &mfree, &shared, &buffers, &cached);
-		/* convert to kilobytes */
-		used /= 1024;
-		mfree /= 1024;
-		shared /= 1024;
-		buffers /= 1024;
-		cached /= 1024;
-		total /= 1024;
-	} else {
-		/*
-		 * Revert to manual parsing, which incidentally already has the
-		 * sizes in kilobytes. This should be safe for both 2.4 and
-		 * 2.6.
-		 */
-		fscanf(fp, "MemFree: %lu %s\n", &mfree, buf);
+	memset(meminfo, 0, sizeof(meminfo));
+	f = xfopen_for_read("meminfo");
+	while (fgets(buf, sizeof(buf), f) != NULL) {
+		char *c = strchr(buf, ':');
+		if (!c)
+			continue;
+		*c = '\0';
+		i = index_in_strings(fields, buf);
+		if (i >= 0)
+			meminfo[i] = strtoul(c+1, NULL, 10);
+	}
+	fclose(f);
+}
 
-		/*
-		 * MemShared: is no longer present in 2.6. Report this as 0,
-		 * to maintain consistent behavior with normal procps.
-		 */
-		if (fscanf(fp, "MemShared: %lu %s\n", &shared, buf) != 2)
-			shared = 0;
 
-		fscanf(fp, "Buffers: %lu %s\n", &buffers, buf);
-		fscanf(fp, "Cached: %lu %s\n", &cached, buf);
+static unsigned long display_header(int scr_width, int *lines_rem_p)
+{
+	char scrbuf[100]; /* [80] was a bit too low on 8Gb ram box */
+	char *buf;
+	unsigned long meminfo[MI_MAX];
 
-		used = total - mfree;
-	}
-	fclose(fp);
+	parse_meminfo(meminfo);
 
-	/* output memory info */
+	/* Output memory info */
 	if (scr_width > (int)sizeof(scrbuf))
 		scr_width = sizeof(scrbuf);
 	snprintf(scrbuf, scr_width,
 		"Mem: %luK used, %luK free, %luK shrd, %luK buff, %luK cached",
-		used, mfree, shared, buffers, cached);
-	/* go to top & clear to the end of screen */
+		meminfo[MI_MEMTOTAL] - meminfo[MI_MEMFREE],
+		meminfo[MI_MEMFREE],
+		meminfo[MI_MEMSHARED] + meminfo[MI_SHMEM],
+		meminfo[MI_BUFFERS],
+		meminfo[MI_CACHED]);
+	/* Go to top & clear to the end of screen */
 	printf(OPT_BATCH_MODE ? "%s\n" : "\033[H\033[J%s\n", scrbuf);
 	(*lines_rem_p)--;
 
-	/* Display CPU time split as percentage of total time
-	 * This displays either a cumulative line or one line per CPU
+	/* Display CPU time split as percentage of total time.
+	 * This displays either a cumulative line or one line per CPU.
 	 */
 	display_cpus(scr_width, scrbuf, lines_rem_p);
 
-	/* read load average as a string */
-	buf[0] = '\0';
-	open_read_close("loadavg", buf, sizeof(buf) - 1);
-	buf[sizeof(buf) - 1] = '\n';
-	*strchr(buf, '\n') = '\0';
-	snprintf(scrbuf, scr_width, "Load average: %s", buf);
+	/* Read load average as a string */
+	buf = stpcpy(scrbuf, "Load average: ");
+	open_read_close("loadavg", buf, sizeof(scrbuf) - sizeof("Load average: "));
+	scrbuf[scr_width - 1] = '\0';
+	strchrnul(buf, '\n')[0] = '\0';
 	puts(scrbuf);
 	(*lines_rem_p)--;
 
-	return total;
+	return meminfo[MI_MEMTOTAL];
 }
 
 static NOINLINE void display_process_list(int lines_rem, int scr_width)
@@ -781,64 +790,31 @@ static int topmem_sort(char *a, char *b)
 /* display header info (meminfo / loadavg) */
 static void display_topmem_header(int scr_width, int *lines_rem_p)
 {
-	enum {
-		TOTAL = 0, MFREE, BUF, CACHE,
-		SWAPTOTAL, SWAPFREE, DIRTY,
-		MWRITE, ANON, MAP, SLAB,
-		NUM_FIELDS
-	};
-	static const char match[NUM_FIELDS][12] = {
-		"\x09" "MemTotal:",  // TOTAL
-		"\x08" "MemFree:",   // MFREE
-		"\x08" "Buffers:",   // BUF
-		"\x07" "Cached:",    // CACHE
-		"\x0a" "SwapTotal:", // SWAPTOTAL
-		"\x09" "SwapFree:",  // SWAPFREE
-		"\x06" "Dirty:",     // DIRTY
-		"\x0a" "Writeback:", // MWRITE
-		"\x0a" "AnonPages:", // ANON
-		"\x07" "Mapped:",    // MAP
-		"\x05" "Slab:",      // SLAB
-	};
-	char meminfo_buf[4 * 1024];
-	const char *Z[NUM_FIELDS];
-	unsigned i;
-	int sz;
-
-	for (i = 0; i < NUM_FIELDS; i++)
-		Z[i] = "?";
-
-	/* read memory info */
-	sz = open_read_close("meminfo", meminfo_buf, sizeof(meminfo_buf) - 1);
-	if (sz >= 0) {
-		char *p = meminfo_buf;
-		meminfo_buf[sz] = '\0';
-		/* Note that fields always appear in the match[] order */
-		for (i = 0; i < NUM_FIELDS; i++) {
-			char *found = strstr(p, match[i] + 1);
-			if (found) {
-				/* Cut "NNNN" out of "    NNNN kb" */
-				char *s = skip_whitespace(found + match[i][0]);
-				p = skip_non_whitespace(s);
-				*p++ = '\0';
-				Z[i] = s;
-			}
-		}
-	}
+	unsigned long meminfo[MI_MAX];
+
+	parse_meminfo(meminfo);
 
 	snprintf(line_buf, LINE_BUF_SIZE,
-		"Mem total:%s anon:%s map:%s free:%s",
-		Z[TOTAL], Z[ANON], Z[MAP], Z[MFREE]);
+		"Mem total:%lu anon:%lu map:%lu free:%lu",
+		meminfo[MI_MEMTOTAL],
+		meminfo[MI_ANONPAGES],
+		meminfo[MI_MAPPED],
+		meminfo[MI_MEMFREE]);
 	printf(OPT_BATCH_MODE ? "%.*s\n" : "\033[H\033[J%.*s\n", scr_width, line_buf);
 
 	snprintf(line_buf, LINE_BUF_SIZE,
-		" slab:%s buf:%s cache:%s dirty:%s write:%s",
-		Z[SLAB], Z[BUF], Z[CACHE], Z[DIRTY], Z[MWRITE]);
+		" slab:%lu buf:%lu cache:%lu dirty:%lu write:%lu",
+		meminfo[MI_SLAB],
+		meminfo[MI_BUFFERS],
+		meminfo[MI_CACHED],
+		meminfo[MI_DIRTY],
+		meminfo[MI_WRITEBACK]);
 	printf("%.*s\n", scr_width, line_buf);
 
 	snprintf(line_buf, LINE_BUF_SIZE,
-		"Swap total:%s free:%s", // TODO: % used?
-		Z[SWAPTOTAL], Z[SWAPFREE]);
+		"Swap total:%lu free:%lu", // TODO: % used?
+		meminfo[MI_SWAPTOTAL],
+		meminfo[MI_SWAPFREE]);
 	printf("%.*s\n", scr_width, line_buf);
 
 	(*lines_rem_p) -= 3;
-- 
2.0.3