1 /* vi: set sw=4 ts=4: */
3 * A tiny 'top' utility.
5 * This is written specifically for the linux /proc/<PID>/stat(m)
8 * This reads the PIDs of all processes and their status and shows
9 * the status of processes (first ones that fit to screen) at given
13 * - At startup this changes to /proc, all the reads are then
16 * (C) Eero Tamminen <oak at welho dot com>
18 * Rewritten by Vladimir Oleynik (C) 2002 <dzo@simtreas.ru>
21 /* Original code Copyrights */
23 * Copyright (c) 1992 Branko Lankester
24 * Copyright (c) 1992 Roger Binns
25 * Copyright (C) 1994-1996 Charles L. Blake.
26 * Copyright (C) 1992-1998 Michael K. Johnson
27 * May be distributed under the conditions of the
28 * GNU Library General Public License
34 typedef struct top_status_t {
36 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
38 unsigned pcpu; /* delta of ticks */
46 typedef struct jiffy_counts_t{
47 unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal;
48 unsigned long long total;
49 unsigned long long busy;
52 /* This structure stores some critical information from one frame to
53 the next. Used for finding deltas. */
54 typedef struct save_hist {
59 typedef int (*cmp_funcp)(top_status_t *P, top_status_t *Q);
61 enum { SORT_DEPTH = 3 };
66 #if ENABLE_FEATURE_USE_TERMIOS
67 struct termios initial_settings;
69 #if !ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
70 cmp_funcp sort_function;
72 cmp_funcp sort_function[SORT_DEPTH];
73 struct save_hist *prev_hist;
75 jiffy_counts_t jif, prev_jif;
76 /* int hist_iterations; */
78 /* unsigned long total_vsz; */
81 #define G (*(struct globals*)&bb_common_bufsiz1)
83 #define ntop (G.ntop )
84 #if ENABLE_FEATURE_USE_TERMIOS
85 #define initial_settings (G. initial_settings )
87 #define sort_function (G.sort_function )
88 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
89 #define prev_hist (G.prev_hist )
90 #define prev_hist_count (G.prev_hist_count )
92 #define prev_jif (G.prev_jif )
93 #define total_pcpu (G.total_pcpu )
96 #define OPT_BATCH_MODE (option_mask32 & 0x4)
99 #if ENABLE_FEATURE_USE_TERMIOS
100 static int pid_sort(top_status_t *P, top_status_t *Q)
102 /* Buggy wrt pids with high bit set */
103 /* (linux pids are in [1..2^15-1]) */
104 return (Q->pid - P->pid);
108 static int mem_sort(top_status_t *P, top_status_t *Q)
110 /* We want to avoid unsigned->signed and truncation errors */
111 if (Q->vsz < P->vsz) return -1;
112 return Q->vsz != P->vsz; /* 0 if ==, 1 if > */
116 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
118 static int pcpu_sort(top_status_t *P, top_status_t *Q)
120 /* Buggy wrt ticks with high bit set */
121 /* Affects only processes for which ticks overflow */
122 return (int)Q->pcpu - (int)P->pcpu;
125 static int time_sort(top_status_t *P, top_status_t *Q)
127 /* We want to avoid unsigned->signed and truncation errors */
128 if (Q->ticks < P->ticks) return -1;
129 return Q->ticks != P->ticks; /* 0 if ==, 1 if > */
132 static int mult_lvl_cmp(void* a, void* b)
136 for (i = 0; i < SORT_DEPTH; i++) {
137 cmp_val = (*sort_function[i])(a, b);
145 static void get_jiffy_counts(void)
147 FILE* fp = xfopen("stat", "r");
149 if (fscanf(fp, "cpu %lld %lld %lld %lld %lld %lld %lld %lld",
150 &jif.usr,&jif.nic,&jif.sys,&jif.idle,
151 &jif.iowait,&jif.irq,&jif.softirq,&jif.steal) < 4) {
152 bb_error_msg_and_die("failed to read /proc/stat");
155 jif.total = jif.usr + jif.nic + jif.sys + jif.idle
156 + jif.iowait + jif.irq + jif.softirq + jif.steal;
157 /* procps 2.x does not count iowait as busy time */
158 jif.busy = jif.total - jif.idle - jif.iowait;
162 static void do_stats(void)
167 struct save_hist *new_hist;
172 new_hist = xmalloc(sizeof(struct save_hist)*ntop);
174 * Make a pass through the data to get stats.
176 /* hist_iterations = 0; */
178 for (n = 0; n < ntop; n++) {
182 * Calculate time in cur process. Time is sum of user time
186 new_hist[n].ticks = cur->ticks;
187 new_hist[n].pid = pid;
189 /* find matching entry from previous pass */
191 /* do not start at index 0, continue at last used one
192 * (brought hist_iterations from ~14000 down to 172) */
194 if (prev_hist_count) do {
195 if (prev_hist[i].pid == pid) {
196 cur->pcpu = cur->ticks - prev_hist[i].ticks;
197 total_pcpu += cur->pcpu;
200 i = (i+1) % prev_hist_count;
201 /* hist_iterations++; */
202 } while (i != last_i);
203 /* total_vsz += cur->vsz; */
207 * Save cur frame's information.
210 prev_hist = new_hist;
211 prev_hist_count = ntop;
213 #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
216 /* display generic info (meminfo / loadavg) */
217 static unsigned long display_generic(int scr_width)
223 unsigned long total, used, mfree, shared, buffers, cached;
225 /* read memory info */
226 fp = xfopen("meminfo", "r");
229 * Old kernels (such as 2.4.x) had a nice summary of memory info that
230 * we could parse, however this is gone entirely in 2.6. Try parsing
231 * the old way first, and if that fails, parse each field manually.
233 * First, we read in the first line. Old kernels will have bogus
234 * strings we don't care about, whereas new kernels will start right
238 if (fscanf(fp, "MemTotal: %lu %s\n", &total, buf) != 2) {
239 fgets(buf, sizeof(buf), fp); /* skip first line */
241 fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu",
242 &total, &used, &mfree, &shared, &buffers, &cached);
243 /* convert to kilobytes */
252 * Revert to manual parsing, which incidentally already has the
253 * sizes in kilobytes. This should be safe for both 2.4 and
257 fscanf(fp, "MemFree: %lu %s\n", &mfree, buf);
260 * MemShared: is no longer present in 2.6. Report this as 0,
261 * to maintain consistent behavior with normal procps.
263 if (fscanf(fp, "MemShared: %lu %s\n", &shared, buf) != 2)
266 fscanf(fp, "Buffers: %lu %s\n", &buffers, buf);
267 fscanf(fp, "Cached: %lu %s\n", &cached, buf);
269 used = total - mfree;
273 /* read load average as a string */
275 open_read_close("loadavg", buf, sizeof(buf));
276 end = strchr(buf, ' ');
277 if (end) end = strchr(end+1, ' ');
278 if (end) end = strchr(end+1, ' ');
279 if (end) *end = '\0';
281 /* output memory info and load average */
282 /* clear screen & go to top */
283 if (scr_width > sizeof(scrbuf))
284 scr_width = sizeof(scrbuf);
285 snprintf(scrbuf, scr_width,
286 "Mem: %ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached",
287 used, mfree, shared, buffers, cached);
289 printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf);
291 if (ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS) {
293 * xxx% = (jif.xxx - prev_jif.xxx) / (jif.total - prev_jif.total) * 100%
295 /* using (unsigned) cast to make multiplication cheaper: */
296 #define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff
297 #define SHOW_STAT(xxx) xxx
299 // We can display fractional percents, but at least in glibc div() is a _function_
300 // and generated code is really awful and big (+0.5k more code):
301 //#define CALC_STAT(xxx) div_t xxx = div(1000 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff, 10)
302 //#define SHOW_STAT(xxx) xxx.quot, '0'+xxx.rem
303 //#define FMT "%3u.%c%%"
304 unsigned total_diff = (jif.total - prev_jif.total ? : 1);
314 snprintf(scrbuf, scr_width,
315 /* %3u in practice almost never displays "100"
316 * and thus has implicit leading space: " 99" */
317 "CPU:"FMT" usr"FMT" sys"FMT" nice"FMT" idle"FMT" wait"FMT" irq"FMT" softirq",
318 // FMT" steal", - what is this 'steal' thing?
319 // I doubt anyone needs to know it
320 SHOW_STAT(usr), SHOW_STAT(sys), SHOW_STAT(nic), SHOW_STAT(idle),
321 SHOW_STAT(iowait), SHOW_STAT(irq), SHOW_STAT(softirq)
330 snprintf(scrbuf, scr_width, "Load average: %s", buf);
337 /* display process statuses */
338 static void display_status(int count, int scr_width)
341 bits_per_int = sizeof(int)*8
344 top_status_t *s = top;
346 unsigned long total_memory = display_generic(scr_width); /* or use total_vsz? */
347 unsigned pmem_shift, pmem_scale;
349 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
350 unsigned pcpu_shift, pcpu_scale;
353 /* what info of the processes is shown */
354 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
355 " PID USER STATUS VSZ PPID %CPU %MEM COMMAND");
357 sizeof( " PID USER STATUS VSZ PPID %CPU %MEM C")
359 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
360 " PID USER STATUS VSZ PPID %MEM COMMAND");
362 sizeof( " PID USER STATUS VSZ PPID %MEM C")
366 * MEM% = s->vsz/MemTotal
368 pmem_shift = bits_per_int-11;
369 pmem_scale = 1000*(1U<<(bits_per_int-11)) / total_memory;
370 /* s->vsz is in kb. we want (s->vsz * pmem_scale) to never overflow */
371 while (pmem_scale >= 512) {
375 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
376 busy_jifs = jif.busy - prev_jif.busy;
377 /* This happens if there were lots of short-lived processes
378 * between two top updates (e.g. compilation) */
379 if (total_pcpu < busy_jifs) total_pcpu = busy_jifs;
382 * CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks
383 * (pcpu is delta of sys+user time between samples)
385 /* (jif.xxx - prev_jif.xxx) and s->pcpu are
386 * in 0..~64000 range (HZ*update_interval).
387 * we assume that unsigned is at least 32-bit.
390 pcpu_scale = (1000*64*(uint16_t)busy_jifs ? : 1);
391 while (pcpu_scale < (1U<<(bits_per_int-2))) {
395 pcpu_scale /= ( (uint16_t)(jif.total-prev_jif.total)*total_pcpu ? : 1);
396 /* we want (s->pcpu * pcpu_scale) to never overflow */
397 while (pcpu_scale >= 1024) {
401 /* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */
403 while (count-- > 0) {
404 div_t pmem = div((s->vsz*pmem_scale) >> pmem_shift, 10);
405 int col = scr_width+1;
406 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(div_t pcpu;)
408 if (s->vsz >= 100*1024)
409 sprintf(vsz_str_buf, "%6ldM", s->vsz/1024);
411 sprintf(vsz_str_buf, "%7ld", s->vsz);
412 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(
413 pcpu = div((s->pcpu*pcpu_scale) >> pcpu_shift, 10);
415 col -= printf("\n%5u %-8s %s "
417 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE("%3u.%c")
419 s->pid, get_cached_username(s->uid), s->state,
420 vsz_str_buf, s->ppid,
421 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu.quot, '0'+pcpu.rem,)
422 pmem.quot, '0'+pmem.rem);
424 printf("%.*s", col, s->comm);
425 /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu,
426 jif.busy - prev_jif.busy, jif.total - prev_jif.total); */
429 /* printf(" %d", hist_iterations); */
430 putchar(OPT_BATCH_MODE ? '\n' : '\r');
435 static void clearmems(void)
437 clear_username_cache();
444 #if ENABLE_FEATURE_USE_TERMIOS
448 static void reset_term(void)
450 tcsetattr(0, TCSANOW, (void *) &initial_settings);
451 #if ENABLE_FEATURE_CLEAN_UP
453 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
456 #endif /* FEATURE_CLEAN_UP */
459 static void sig_catcher(int sig ATTRIBUTE_UNUSED)
464 #endif /* FEATURE_USE_TERMIOS */
467 int top_main(int argc, char **argv);
468 int top_main(int argc, char **argv)
470 int count, lines, col;
471 unsigned interval = 5; /* default update rate is 5 seconds */
472 unsigned iterations = UINT_MAX; /* 2^32 iterations by default :) */
473 char *sinterval, *siterations;
474 #if ENABLE_FEATURE_USE_TERMIOS
475 struct termios new_settings;
479 #endif /* FEATURE_USE_TERMIOS */
483 /* do normal option parsing */
484 opt_complementary = "-";
485 getopt32(argc, argv, "d:n:b", &sinterval, &siterations);
486 if (option_mask32 & 0x1) interval = xatou(sinterval); // -d
487 if (option_mask32 & 0x2) iterations = xatou(siterations); // -n
488 //if (option_mask32 & 0x4) // -b
490 /* change to /proc */
492 #if ENABLE_FEATURE_USE_TERMIOS
493 tcgetattr(0, (void *) &initial_settings);
494 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
495 /* unbuffered input, turn off echo */
496 new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL);
498 signal(SIGTERM, sig_catcher);
499 signal(SIGINT, sig_catcher);
500 tcsetattr(0, TCSANOW, (void *) &new_settings);
502 #endif /* FEATURE_USE_TERMIOS */
504 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
505 sort_function[0] = pcpu_sort;
506 sort_function[1] = mem_sort;
507 sort_function[2] = time_sort;
509 sort_function = mem_sort;
510 #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
513 procps_status_t *p = NULL;
515 /* Default to 25 lines - 5 lines for status */
516 lines = 24 - 3 USE_FEATURE_TOP_CPU_GLOBAL_PERCENTS( - 1);
518 #if ENABLE_FEATURE_USE_TERMIOS
519 get_terminal_width_height(0, &col, &lines);
520 if (lines < 5 || col < MIN_WIDTH) {
524 lines -= 3 USE_FEATURE_TOP_CPU_GLOBAL_PERCENTS( + 1);
525 #endif /* FEATURE_USE_TERMIOS */
527 /* read process IDs & status for all the processes */
528 while ((p = procps_scan(p, 0
540 top = xrealloc(top, (++ntop)*sizeof(top_status_t));
542 top[n].ppid = p->ppid;
544 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
545 top[n].ticks = p->stime + p->utime;
548 strcpy(top[n].state, p->state);
549 strcpy(top[n].comm, p->comm);
552 bb_error_msg_and_die("can't find process info in /proc");
554 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
555 if (!prev_hist_count) {
562 qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp);
564 qsort(top, ntop, sizeof(top_status_t), (void*)sort_function);
565 #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
567 if (OPT_BATCH_MODE || count > ntop) {
570 /* show status for each of the processes */
571 display_status(count, col);
572 #if ENABLE_FEATURE_USE_TERMIOS
573 tv.tv_sec = interval;
577 select(1, &readfds, NULL, NULL, &tv);
578 if (FD_ISSET(0, &readfds)) {
579 if (read(0, &c, 1) <= 0) { /* signal */
582 if (c == 'q' || c == initial_settings.c_cc[VINTR])
585 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
586 sort_function[0] = mem_sort;
587 sort_function[1] = pcpu_sort;
588 sort_function[2] = time_sort;
590 sort_function = mem_sort;
593 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
595 sort_function[0] = pcpu_sort;
596 sort_function[1] = mem_sort;
597 sort_function[2] = time_sort;
600 sort_function[0] = time_sort;
601 sort_function[1] = mem_sort;
602 sort_function[2] = pcpu_sort;
606 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
607 sort_function[0] = pid_sort;
609 sort_function = pid_sort;
617 #endif /* FEATURE_USE_TERMIOS */
620 if (ENABLE_FEATURE_CLEAN_UP)