aboutsummaryrefslogtreecommitdiff
path: root/procps/top.c
blob: ebfbcb80c90e9077544d12f401585b5a0713f266 (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/* vi: set sw=4 ts=4: */
/*
 * A tiny 'top' utility.
 *
 * This is written specifically for the linux /proc/<PID>/stat(m)
 * files format.

 * This reads the PIDs of all processes and their status and shows
 * the status of processes (first ones that fit to screen) at given
 * intervals.
 *
 * NOTES:
 * - At startup this changes to /proc, all the reads are then
 *   relative to that.
 *
 * (C) Eero Tamminen <oak at welho dot com>
 *
 * Rewritten by Vladimir Oleynik (C) 2002 <dzo@simtreas.ru>
 */

/* Original code Copyrights */
/*
 * Copyright (c) 1992 Branko Lankester
 * Copyright (c) 1992 Roger Binns
 * Copyright (C) 1994-1996 Charles L. Blake.
 * Copyright (C) 1992-1998 Michael K. Johnson
 * May be distributed under the conditions of the
 * GNU Library General Public License
 */

#include "busybox.h"

typedef int (*cmp_t)(procps_status_t *P, procps_status_t *Q);

static procps_status_t *top;   /* Hehe */
static int ntop;
static unsigned option_mask;
#define OPT_BATCH_MODE (option_mask & 0x4)

#ifdef CONFIG_FEATURE_USE_TERMIOS
static int pid_sort(procps_status_t *P, procps_status_t *Q)
{
	return (Q->pid - P->pid);
}
#endif

static int mem_sort(procps_status_t *P, procps_status_t *Q)
{
	return (int)(Q->rss - P->rss);
}

#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE

#define sort_depth 3
static cmp_t sort_function[sort_depth];

static int pcpu_sort(procps_status_t *P, procps_status_t *Q)
{
	return (Q->pcpu - P->pcpu);
}

static int time_sort(procps_status_t *P, procps_status_t *Q)
{
	return (int)((Q->stime + Q->utime) - (P->stime + P->utime));
}

static int mult_lvl_cmp(void* a, void* b) {
	int i, cmp_val;

	for (i = 0; i < sort_depth; i++) {
		cmp_val = (*sort_function[i])(a, b);
		if (cmp_val != 0)
			return cmp_val;
	}
	return 0;
}

/* This structure stores some critical information from one frame to
   the next. Mostly used for sorting. */
struct save_hist {
	int ticks;
	int pid;
};

/*
 * Calculates percent cpu usage for each task.
 */

static struct save_hist *prev_hist;
static int prev_hist_count;
/* static int hist_iterations; */


static unsigned total_pcpu;
/* static unsigned long total_rss; */

struct jiffy_counts {
	unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal;
	unsigned long long total;
	unsigned long long busy;
};
static struct jiffy_counts jif, prev_jif;

static void get_jiffy_counts(void)
{
	FILE* fp = xfopen("stat", "r");
	prev_jif = jif;
	if (fscanf(fp, "cpu  %lld %lld %lld %lld %lld %lld %lld %lld",
			&jif.usr,&jif.nic,&jif.sys,&jif.idle,
			&jif.iowait,&jif.irq,&jif.softirq,&jif.steal) < 4) {
		bb_error_msg_and_die("failed to read /proc/stat");
	}
	fclose(fp);
	jif.total = jif.usr + jif.nic + jif.sys + jif.idle
			+ jif.iowait + jif.irq + jif.softirq + jif.steal;
	/* procps 2.x does not count iowait as busy time */
	jif.busy = jif.total - jif.idle - jif.iowait;
}

static void do_stats(void)
{
	procps_status_t *cur;
	int pid, total_time, i, last_i, n;
	struct save_hist *new_hist;

	get_jiffy_counts();
	total_pcpu = 0;
	/* total_rss = 0; */
	new_hist = xmalloc(sizeof(struct save_hist)*ntop);
	/*
	 * Make a pass through the data to get stats.
	 */
	/* hist_iterations = 0; */
	i = 0;
	for (n = 0; n < ntop; n++) {
		cur = top + n;

		/*
		 * Calculate time in cur process.  Time is sum of user time
		 * and system time
		 */
		pid = cur->pid;
		total_time = cur->stime + cur->utime;
		new_hist[n].ticks = total_time;
		new_hist[n].pid = pid;

		/* find matching entry from previous pass */
		cur->pcpu = 0;
		/* do not start at index 0, continue at last used one
		 * (brought hist_iterations from ~14000 down to 172) */
		last_i = i;
		if (prev_hist_count) do {
			if (prev_hist[i].pid == pid) {
				cur->pcpu = total_time - prev_hist[i].ticks;
				break;
			}
			i = (i+1) % prev_hist_count;
			/* hist_iterations++; */
		} while (i != last_i);
		total_pcpu += cur->pcpu;
		/* total_rss += cur->rss; */
	}

	/*
	 * Save cur frame's information.
	 */
	free(prev_hist);
	prev_hist = new_hist;
	prev_hist_count = ntop;
}
#else
static cmp_t sort_function;
#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */

/* display generic info (meminfo / loadavg) */
static unsigned long display_generic(int scr_width)
{
	FILE *fp;
	char buf[80];
	char scrbuf[80];
	char *end;
	unsigned long total, used, mfree, shared, buffers, cached;
	unsigned int needs_conversion = 1;

	/* read memory info */
	fp = xfopen("meminfo", "r");

	/*
	 * 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);
	} else {
		/*
		 * Revert to manual parsing, which incidentally already has the
		 * sizes in kilobytes. This should be safe for both 2.4 and
		 * 2.6.
		 */
		needs_conversion = 0;

		fscanf(fp, "MemFree: %lu %s\n", &mfree, buf);

		/*
		 * 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);

		used = total - mfree;
	}
	fclose(fp);

	/* read load average as a string */
	fp = xfopen("loadavg", "r");
	buf[0] = '\0';
	fgets(buf, sizeof(buf), fp);
	end = strchr(buf, ' ');
	if (end) end = strchr(end+1, ' ');
	if (end) end = strchr(end+1, ' ');
	if (end) *end = '\0';
	fclose(fp);

	if (needs_conversion) {
		/* convert to kilobytes */
		used /= 1024;
		mfree /= 1024;
		shared /= 1024;
		buffers /= 1024;
		cached /= 1024;
		total /= 1024;
	}

	/* output memory info and load average */
	/* clear screen & go to top */
	if (scr_width > sizeof(scrbuf))
		scr_width = sizeof(scrbuf);
	snprintf(scrbuf, scr_width,
		"Mem: %ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached",
		used, mfree, shared, buffers, cached);

	printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf);
    
	snprintf(scrbuf, scr_width,
		"Load average: %s", buf);
	printf("%s\n", scrbuf);

	return total;
}


/* display process statuses */
static void display_status(int count, int scr_width)
{
	enum {
		bits_per_int = sizeof(int)*8
	};

	procps_status_t *s = top;
	char rss_str_buf[8];
	unsigned long total_memory = display_generic(scr_width); /* or use total_rss? */
	unsigned pmem_shift, pmem_scale;

#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
	unsigned pcpu_shift, pcpu_scale;

	/* what info of the processes is shown */
	printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
		"  PID USER     STATUS   RSS  PPID %CPU %MEM COMMAND");
#define MIN_WIDTH \
	sizeof( "  PID USER     STATUS   RSS  PPID %CPU %MEM C")
#else
	printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
		"  PID USER     STATUS   RSS  PPID %MEM COMMAND");
#define MIN_WIDTH \
	sizeof( "  PID USER     STATUS   RSS  PPID %MEM C")
#endif

	/*
	 * MEM% = s->rss/MemTotal
	 */
	pmem_shift = bits_per_int-11;
	pmem_scale = 1000*(1U<<(bits_per_int-11)) / total_memory;
	/* s->rss is in kb. we want (s->rss * pmem_scale) to never overflow */
	while (pmem_scale >= 512) {
		pmem_scale /= 4;
		pmem_shift -= 2;
	}
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
	/*
	 * CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks
	 * (pcpu is delta of sys+user time between samples)
	 */
	/* (jif.xxx - prev_jif.xxx) and s->pcpu are
	 * in 0..~64000 range (HZ*update_interval).
	 * we assume that unsigned is at least 32-bit.
	 */
	pcpu_shift = 6;
	pcpu_scale = (1000*64*(uint16_t)(jif.busy-prev_jif.busy) ? : 1);
	while (pcpu_scale < (1U<<(bits_per_int-2))) {
		pcpu_scale *= 4;
		pcpu_shift += 2;
	}
	pcpu_scale /= ( (uint16_t)(jif.total-prev_jif.total)*total_pcpu ? : 1);
	/* we want (s->pcpu * pcpu_scale) to never overflow */
	while (pcpu_scale >= 1024) {
		pcpu_scale /= 4;
		pcpu_shift -= 2;
	}
	/* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */
#endif
	if (OPT_BATCH_MODE) count--;
	while (count-- > 0) {
		div_t pmem = div((s->rss*pmem_scale) >> pmem_shift, 10);
		int col = scr_width+1;
		USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(div_t pcpu;)

		if (s->rss >= 100*1024)
			sprintf(rss_str_buf, "%6ldM", s->rss/1024);
		else
			sprintf(rss_str_buf, "%7ld", s->rss);
		USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu = div((s->pcpu*pcpu_scale) >> pcpu_shift, 10);)
		col -= printf("\n%5d %-8s %s  %s%6d%3u.%c" \
				USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE("%3u.%c") " ",
				s->pid, s->user, s->state, rss_str_buf, s->ppid,
				USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu.quot, '0'+pcpu.rem,)
				pmem.quot, '0'+pmem.rem);
		if (col>0)
			printf("%.*s", col, s->short_cmd);
		/* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu,
			jif.busy - prev_jif.busy, jif.total - prev_jif.total); */
		s++;
	}
	/* printf(" %d", hist_iterations); */
	putchar(OPT_BATCH_MODE ? '\n' : '\r');
	fflush(stdout);
}

static void clearmems(void)
{
	free(top);
	top = 0;
	ntop = 0;
}

#ifdef CONFIG_FEATURE_USE_TERMIOS
#include <termios.h>
#include <signal.h>


static struct termios initial_settings;

static void reset_term(void)
{
	tcsetattr(0, TCSANOW, (void *) &initial_settings);
#ifdef CONFIG_FEATURE_CLEAN_UP
	clearmems();
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
	free(prev_hist);
#endif
#endif /* CONFIG_FEATURE_CLEAN_UP */
}

static void sig_catcher(int sig ATTRIBUTE_UNUSED)
{
	reset_term();
	exit(1);
}
#endif /* CONFIG_FEATURE_USE_TERMIOS */


int top_main(int argc, char **argv)
{
	int count, lines, col;
	int interval = 5; /* default update rate is 5 seconds */
	int iterations = -1; /* 2^32 iterations by default :) */
	char *sinterval, *siterations;
#ifdef CONFIG_FEATURE_USE_TERMIOS
	struct termios new_settings;
	struct timeval tv;
	fd_set readfds;
	unsigned char c;
#endif /* CONFIG_FEATURE_USE_TERMIOS */

	/* do normal option parsing */
	interval = 5;
	opt_complementary = "-";
	option_mask = getopt32(argc, argv, "d:n:b", 
		&sinterval, &siterations);
	if (option_mask & 0x1) interval = atoi(sinterval); // -d
	if (option_mask & 0x2) iterations = atoi(siterations); // -n
	//if (option_mask & 0x4) // -b

	/* change to /proc */
	xchdir("/proc");
#ifdef CONFIG_FEATURE_USE_TERMIOS
	tcgetattr(0, (void *) &initial_settings);
	memcpy(&new_settings, &initial_settings, sizeof(struct termios));
	/* unbuffered input, turn off echo */
	new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL);

	signal(SIGTERM, sig_catcher);
	signal(SIGINT, sig_catcher);
	tcsetattr(0, TCSANOW, (void *) &new_settings);
	atexit(reset_term);
#endif /* CONFIG_FEATURE_USE_TERMIOS */

#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
	sort_function[0] = pcpu_sort;
	sort_function[1] = mem_sort;
	sort_function[2] = time_sort;
#else
	sort_function = mem_sort;
#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */

	while (1) {
		procps_status_t *p;

		/* Default to 25 lines - 5 lines for status */
		lines = 24 - 3;
		col = 79;
#ifdef CONFIG_FEATURE_USE_TERMIOS
		get_terminal_width_height(0, &col, &lines);
		if (lines < 5 || col < MIN_WIDTH) {
			sleep(interval);
			continue;
		}
		lines -= 3;
#endif /* CONFIG_FEATURE_USE_TERMIOS */

		/* read process IDs & status for all the processes */
		while ((p = procps_scan(0)) != 0) {
			int n = ntop;

			top = xrealloc(top, (++ntop)*sizeof(procps_status_t));
			memcpy(top + n, p, sizeof(procps_status_t));
		}
		if (ntop == 0) {
			bb_error_msg_and_die("Can't find process info in /proc");
		}
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
		if (!prev_hist_count) {
			do_stats();
			sleep(1);
			clearmems();
			continue;
		}
		do_stats();
		qsort(top, ntop, sizeof(procps_status_t), (void*)mult_lvl_cmp);
#else
		qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function);
#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
		count = lines;
		if (count > ntop) {
			count = ntop;
		}
		/* show status for each of the processes */
		display_status(count, col);
#ifdef CONFIG_FEATURE_USE_TERMIOS
		tv.tv_sec = interval;
		tv.tv_usec = 0;
		FD_ZERO(&readfds);
		FD_SET(0, &readfds);
		select(1, &readfds, NULL, NULL, &tv);
		if (FD_ISSET(0, &readfds)) {
			if (read(0, &c, 1) <= 0) {   /* signal */
				return EXIT_FAILURE;
			}
			if (c == 'q' || c == initial_settings.c_cc[VINTR])
				break;
			if (c == 'M') {
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
				sort_function[0] = mem_sort;
				sort_function[1] = pcpu_sort;
				sort_function[2] = time_sort;
#else
				sort_function = mem_sort;
#endif
			}
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
			if (c == 'P') {
				sort_function[0] = pcpu_sort;
				sort_function[1] = mem_sort;
				sort_function[2] = time_sort;
			}
			if (c == 'T') {
				sort_function[0] = time_sort;
				sort_function[1] = mem_sort;
				sort_function[2] = pcpu_sort;
			}
#endif
			if (c == 'N') {
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
				sort_function[0] = pid_sort;
#else
				sort_function = pid_sort;
#endif
			}
		}
		if (!--iterations)
			break;
#else
		sleep(interval);
#endif /* CONFIG_FEATURE_USE_TERMIOS */
		clearmems();
	}
	if (ENABLE_FEATURE_CLEAN_UP)
		clearmems();
	putchar('\n');
	return EXIT_SUCCESS;
}