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);
62 enum { SORT_DEPTH = 3 };
68 #if ENABLE_FEATURE_TOPMEM
72 #if ENABLE_FEATURE_USE_TERMIOS
73 struct termios initial_settings;
75 #if !ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
76 cmp_funcp sort_function[1];
78 cmp_funcp sort_function[SORT_DEPTH];
79 struct save_hist *prev_hist;
81 jiffy_counts_t jif, prev_jif;
82 /* int hist_iterations; */
84 /* unsigned long total_vsz; */
89 enum { LINE_BUF_SIZE = COMMON_BUFSIZE - offsetof(struct globals, line_buf) };
91 #define G (*(struct globals*)&bb_common_bufsiz1)
92 #define INIT_G() do { \
93 struct G_sizecheck { \
94 char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
98 #define ntop (G.ntop )
99 #define sort_field (G.sort_field )
100 #define inverted (G.inverted )
101 #define initial_settings (G.initial_settings )
102 #define sort_function (G.sort_function )
103 #define prev_hist (G.prev_hist )
104 #define prev_hist_count (G.prev_hist_count )
106 #define prev_jif (G.prev_jif )
107 #define total_pcpu (G.total_pcpu )
108 #define line_buf (G.line_buf )
114 OPT_EOF = (1 << 3), /* pseudo: "we saw EOF in stdin" */
116 #define OPT_BATCH_MODE (option_mask32 & OPT_b)
119 #if ENABLE_FEATURE_USE_TERMIOS
120 static int pid_sort(top_status_t *P, top_status_t *Q)
122 /* Buggy wrt pids with high bit set */
123 /* (linux pids are in [1..2^15-1]) */
124 return (Q->pid - P->pid);
128 static int mem_sort(top_status_t *P, top_status_t *Q)
130 /* We want to avoid unsigned->signed and truncation errors */
131 if (Q->vsz < P->vsz) return -1;
132 return Q->vsz != P->vsz; /* 0 if ==, 1 if > */
136 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
138 static int pcpu_sort(top_status_t *P, top_status_t *Q)
140 /* Buggy wrt ticks with high bit set */
141 /* Affects only processes for which ticks overflow */
142 return (int)Q->pcpu - (int)P->pcpu;
145 static int time_sort(top_status_t *P, top_status_t *Q)
147 /* We want to avoid unsigned->signed and truncation errors */
148 if (Q->ticks < P->ticks) return -1;
149 return Q->ticks != P->ticks; /* 0 if ==, 1 if > */
152 static int mult_lvl_cmp(void* a, void* b)
156 for (i = 0; i < SORT_DEPTH; i++) {
157 cmp_val = (*sort_function[i])(a, b);
165 static void get_jiffy_counts(void)
167 FILE* fp = xfopen("stat", "r");
169 if (fscanf(fp, "cpu %lld %lld %lld %lld %lld %lld %lld %lld",
170 &jif.usr,&jif.nic,&jif.sys,&jif.idle,
171 &jif.iowait,&jif.irq,&jif.softirq,&jif.steal) < 4) {
172 bb_error_msg_and_die("can't read /proc/stat");
175 jif.total = jif.usr + jif.nic + jif.sys + jif.idle
176 + jif.iowait + jif.irq + jif.softirq + jif.steal;
177 /* procps 2.x does not count iowait as busy time */
178 jif.busy = jif.total - jif.idle - jif.iowait;
182 static void do_stats(void)
187 struct save_hist *new_hist;
192 new_hist = xmalloc(sizeof(struct save_hist)*ntop);
194 * Make a pass through the data to get stats.
196 /* hist_iterations = 0; */
198 for (n = 0; n < ntop; n++) {
202 * Calculate time in cur process. Time is sum of user time
206 new_hist[n].ticks = cur->ticks;
207 new_hist[n].pid = pid;
209 /* find matching entry from previous pass */
211 /* do not start at index 0, continue at last used one
212 * (brought hist_iterations from ~14000 down to 172) */
214 if (prev_hist_count) do {
215 if (prev_hist[i].pid == pid) {
216 cur->pcpu = cur->ticks - prev_hist[i].ticks;
217 total_pcpu += cur->pcpu;
220 i = (i+1) % prev_hist_count;
221 /* hist_iterations++; */
222 } while (i != last_i);
223 /* total_vsz += cur->vsz; */
227 * Save cur frame's information.
230 prev_hist = new_hist;
231 prev_hist_count = ntop;
233 #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
235 #if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS && ENABLE_FEATURE_TOP_DECIMALS
236 /* formats 7 char string (8 with terminating NUL) */
237 static char *fmt_100percent_8(char pbuf[8], unsigned value, unsigned total)
240 if (value >= total) { /* 100% ? */
241 strcpy(pbuf, " 100% ");
244 /* else generate " [N/space]N.N% " string */
245 value = 1000 * value / total;
249 pbuf[1] = t ? t + '0' : ' ';
250 pbuf[2] = '0' + (value / 10);
252 pbuf[4] = '0' + (value % 10);
260 static unsigned long display_header(int scr_width)
265 unsigned long total, used, mfree, shared, buffers, cached;
266 #if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS
270 /* read memory info */
271 fp = xfopen("meminfo", "r");
274 * Old kernels (such as 2.4.x) had a nice summary of memory info that
275 * we could parse, however this is gone entirely in 2.6. Try parsing
276 * the old way first, and if that fails, parse each field manually.
278 * First, we read in the first line. Old kernels will have bogus
279 * strings we don't care about, whereas new kernels will start right
283 if (fscanf(fp, "MemTotal: %lu %s\n", &total, buf) != 2) {
284 fgets(buf, sizeof(buf), fp); /* skip first line */
286 fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu",
287 &total, &used, &mfree, &shared, &buffers, &cached);
288 /* convert to kilobytes */
297 * Revert to manual parsing, which incidentally already has the
298 * sizes in kilobytes. This should be safe for both 2.4 and
302 fscanf(fp, "MemFree: %lu %s\n", &mfree, buf);
305 * MemShared: is no longer present in 2.6. Report this as 0,
306 * to maintain consistent behavior with normal procps.
308 if (fscanf(fp, "MemShared: %lu %s\n", &shared, buf) != 2)
311 fscanf(fp, "Buffers: %lu %s\n", &buffers, buf);
312 fscanf(fp, "Cached: %lu %s\n", &cached, buf);
314 used = total - mfree;
318 /* output memory info */
319 if (scr_width > (int)sizeof(scrbuf))
320 scr_width = sizeof(scrbuf);
321 snprintf(scrbuf, scr_width,
322 "Mem: %luK used, %luK free, %luK shrd, %luK buff, %luK cached",
323 used, mfree, shared, buffers, cached);
324 /* clear screen & go to top */
325 printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf);
327 #if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS
329 * xxx% = (jif.xxx - prev_jif.xxx) / (jif.total - prev_jif.total) * 100%
331 /* using (unsigned) casts to make operations cheaper */
332 total_diff = ((unsigned)(jif.total - prev_jif.total) ? : 1);
333 #if ENABLE_FEATURE_TOP_DECIMALS
334 /* Generated code is approx +0.3k */
335 #define CALC_STAT(xxx) char xxx[8]
336 #define SHOW_STAT(xxx) fmt_100percent_8(xxx, (unsigned)(jif.xxx - prev_jif.xxx), total_diff)
339 #define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff
340 #define SHOW_STAT(xxx) xxx
343 { /* need block: CALC_STAT are declarations */
353 snprintf(scrbuf, scr_width,
354 /* Barely fits in 79 chars when in "decimals" mode. */
355 "CPU:"FMT"usr"FMT"sys"FMT"nice"FMT"idle"FMT"io"FMT"irq"FMT"softirq",
356 SHOW_STAT(usr), SHOW_STAT(sys), SHOW_STAT(nic), SHOW_STAT(idle),
357 SHOW_STAT(iowait), SHOW_STAT(irq), SHOW_STAT(softirq)
358 //, SHOW_STAT(steal) - what is this 'steal' thing?
359 // I doubt anyone wants to know it
368 /* read load average as a string */
370 open_read_close("loadavg", buf, sizeof("N.NN N.NN N.NN")-1);
371 buf[sizeof("N.NN N.NN N.NN")-1] = '\0';
372 snprintf(scrbuf, scr_width, "Load average: %s", buf);
378 static NOINLINE void display_process_list(int count, int scr_width)
381 BITS_PER_INT = sizeof(int)*8
384 top_status_t *s = top;
386 unsigned long total_memory = display_header(scr_width); /* or use total_vsz? */
387 /* xxx_shift and xxx_scale variables allow us to replace
388 * expensive divides with multiply and shift */
389 unsigned pmem_shift, pmem_scale, pmem_half;
390 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
391 unsigned pcpu_shift, pcpu_scale, pcpu_half;
394 /* what info of the processes is shown */
395 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
396 " PID PPID USER STAT VSZ %MEM %CPU COMMAND");
399 /* !CPU_USAGE_PERCENTAGE */
400 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
401 " PID PPID USER STAT VSZ %MEM COMMAND");
404 #if ENABLE_FEATURE_TOP_DECIMALS
406 #define CALC_STAT(name, val) div_t name = div((val), 10)
407 #define SHOW_STAT(name) name.quot, '0'+name.rem
411 #define CALC_STAT(name, val) unsigned name = (val)
412 #define SHOW_STAT(name) name
416 * MEM% = s->vsz/MemTotal
418 pmem_shift = BITS_PER_INT-11;
419 pmem_scale = UPSCALE*(1U<<(BITS_PER_INT-11)) / total_memory;
420 /* s->vsz is in kb. we want (s->vsz * pmem_scale) to never overflow */
421 while (pmem_scale >= 512) {
425 pmem_half = (1U << pmem_shift) / (ENABLE_FEATURE_TOP_DECIMALS? 20 : 2);
426 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
427 busy_jifs = jif.busy - prev_jif.busy;
428 /* This happens if there were lots of short-lived processes
429 * between two top updates (e.g. compilation) */
430 if (total_pcpu < busy_jifs) total_pcpu = busy_jifs;
433 * CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks
434 * (pcpu is delta of sys+user time between samples)
436 /* (jif.xxx - prev_jif.xxx) and s->pcpu are
437 * in 0..~64000 range (HZ*update_interval).
438 * we assume that unsigned is at least 32-bit.
441 pcpu_scale = (UPSCALE*64*(uint16_t)busy_jifs ? : 1);
442 while (pcpu_scale < (1U<<(BITS_PER_INT-2))) {
446 pcpu_scale /= ( (uint16_t)(jif.total-prev_jif.total)*total_pcpu ? : 1);
447 /* we want (s->pcpu * pcpu_scale) to never overflow */
448 while (pcpu_scale >= 1024) {
452 pcpu_half = (1U << pcpu_shift) / (ENABLE_FEATURE_TOP_DECIMALS? 20 : 2);
453 /* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */
456 scr_width += 2; /* account for leading '\n' and trailing NUL */
457 /* Ok, all preliminary data is ready, go through the list */
458 while (count-- > 0) {
460 CALC_STAT(pmem, (s->vsz*pmem_scale + pmem_half) >> pmem_shift);
461 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
462 CALC_STAT(pcpu, (s->pcpu*pcpu_scale + pcpu_half) >> pcpu_shift);
465 if (s->vsz >= 100000)
466 sprintf(vsz_str_buf, "%6ldm", s->vsz/1024);
468 sprintf(vsz_str_buf, "%7ld", s->vsz);
469 // PID PPID USER STAT VSZ %MEM [%CPU] COMMAND
470 col = snprintf(line_buf, scr_width,
471 "\n" "%5u%6u %-8.8s %s%s" FMT
472 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
476 s->pid, s->ppid, get_cached_username(s->uid),
477 s->state, vsz_str_buf,
479 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
483 if ((int)(col + 1) < scr_width)
484 read_cmdline(line_buf + col, scr_width - col - 1, s->pid, s->comm);
485 fputs(line_buf, stdout);
486 /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu,
487 jif.busy - prev_jif.busy, jif.total - prev_jif.total); */
490 /* printf(" %d", hist_iterations); */
491 bb_putchar(OPT_BATCH_MODE ? '\n' : '\r');
499 static void clearmems(void)
501 clear_username_cache();
507 #if ENABLE_FEATURE_USE_TERMIOS
511 static void reset_term(void)
513 tcsetattr(0, TCSANOW, &initial_settings);
514 if (ENABLE_FEATURE_CLEAN_UP) {
516 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
522 static void sig_catcher(int sig ATTRIBUTE_UNUSED)
527 #endif /* FEATURE_USE_TERMIOS */
533 typedef unsigned long mem_t;
535 typedef struct topmem_status_t {
538 /* vsz doesn't count /dev/xxx mappings except /dev/zero */
548 enum { NUM_SORT_FIELD = 7 };
550 #define topmem ((topmem_status_t*)top)
552 #if ENABLE_FEATURE_TOPMEM
553 static int topmem_sort(char *a, char *b)
558 n = offsetof(topmem_status_t, vsz) + (sort_field * sizeof(mem_t));
559 l = *(mem_t*)(a + n);
560 r = *(mem_t*)(b + n);
565 /* We want to avoid unsigned->signed and truncation errors */
566 /* l>r: -1, l=r: 0, l<r: 1 */
567 n = (l > r) ? -1 : (l != r);
568 return inverted ? -n : n;
571 /* Cut "NNNN " out of " NNNN kb" */
572 static char *grab_number(char *str, const char *match, unsigned sz)
574 if (strncmp(str, match, sz) == 0) {
575 str = skip_whitespace(str + sz);
576 (skip_non_whitespace(str))[1] = '\0';
582 /* display header info (meminfo / loadavg) */
583 static void display_topmem_header(int scr_width)
594 /* 5 */ char *swaptotal;
595 /* 6 */ char *swapfree;
597 /* 8 */ char *mwrite;
604 #define total Z.u.total
605 #define mfree Z.u.mfree
607 #define cache Z.u.cache
608 #define swaptotal Z.u.swaptotal
609 #define swapfree Z.u.swapfree
610 #define dirty Z.u.dirty
611 #define mwrite Z.u.mwrite
612 #define anon Z.u.anon
614 #define slab Z.u.slab
617 memset(&Z, 0, sizeof(Z));
619 /* read memory info */
620 fp = xfopen("meminfo", "r");
621 while (fgets(linebuf, sizeof(linebuf), fp)) {
624 #define SCAN(match, name) \
625 p = grab_number(linebuf, match, sizeof(match)-1); \
626 if (p) { name = p; continue; }
628 SCAN("MemTotal:", total);
629 SCAN("MemFree:", mfree);
630 SCAN("Buffers:", buf);
631 SCAN("Cached:", cache);
632 SCAN("SwapTotal:", swaptotal);
633 SCAN("SwapFree:", swapfree);
634 SCAN("Dirty:", dirty);
635 SCAN("Writeback:", mwrite);
636 SCAN("AnonPages:", anon);
637 SCAN("Mapped:", map);
643 #define S(s) (s ? s : "0 ")
644 snprintf(linebuf, sizeof(linebuf),
645 "Mem %stotal %sanon %smap %sfree",
646 S(total), S(anon), S(map), S(mfree));
647 printf(OPT_BATCH_MODE ? "%.*s\n" : "\e[H\e[J%.*s\n", scr_width, linebuf);
649 snprintf(linebuf, sizeof(linebuf),
650 " %sslab %sbuf %scache %sdirty %swrite",
651 S(slab), S(buf), S(cache), S(dirty), S(mwrite));
652 printf("%.*s\n", scr_width, linebuf);
654 snprintf(linebuf, sizeof(linebuf),
655 "Swap %stotal %sfree", // TODO: % used?
656 S(swaptotal), S(swapfree));
657 printf("%.*s\n", scr_width, linebuf);
660 for (i = 0; i < ARRAY_SIZE(str); i++)
676 static void ulltoa6_and_space(unsigned long long ul, char buf[6])
678 /* see http://en.wikipedia.org/wiki/Tera */
679 smart_ulltoa5(ul, buf, " mgtpezy");
683 static NOINLINE void display_topmem_process_list(int count, int scr_width)
685 #define HDR_STR " PID VSZ VSZRW RSS (SHR) DIRTY (SHR) STACK"
686 #define MIN_WIDTH sizeof(HDR_STR)
687 const topmem_status_t *s = topmem;
689 display_topmem_header(scr_width);
690 strcpy(line_buf, HDR_STR " COMMAND");
691 line_buf[5 + sort_field * 6] = '*';
692 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width, line_buf);
694 while (--count >= 0) {
695 // PID VSZ VSZRW RSS (SHR) DIRTY (SHR) COMMAND
696 ulltoa6_and_space(s->pid , &line_buf[0*6]);
697 ulltoa6_and_space(s->vsz , &line_buf[1*6]);
698 ulltoa6_and_space(s->vszrw , &line_buf[2*6]);
699 ulltoa6_and_space(s->rss , &line_buf[3*6]);
700 ulltoa6_and_space(s->rss_sh , &line_buf[4*6]);
701 ulltoa6_and_space(s->dirty , &line_buf[5*6]);
702 ulltoa6_and_space(s->dirty_sh, &line_buf[6*6]);
703 ulltoa6_and_space(s->stack , &line_buf[7*6]);
704 line_buf[8*6] = '\0';
705 if (scr_width > (int)MIN_WIDTH) {
706 read_cmdline(&line_buf[8*6], scr_width - MIN_WIDTH, s->pid, s->comm);
708 printf("\n""%.*s", scr_width, line_buf);
711 bb_putchar(OPT_BATCH_MODE ? '\n' : '\r');
717 void display_topmem_process_list(int count, int scr_width);
718 int topmem_sort(char *a, char *b);
741 int top_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
742 int top_main(int argc ATTRIBUTE_UNUSED, char **argv)
749 SKIP_FEATURE_TOPMEM(const) unsigned scan_mask = TOP_MASK;
750 #if ENABLE_FEATURE_USE_TERMIOS
751 struct termios new_settings;
752 struct pollfd pfd[1];
756 pfd[0].events = POLLIN;
757 #endif /* FEATURE_USE_TERMIOS */
761 interval = 5; /* default update interval is 5 seconds */
762 iterations = 0; /* infinite */
764 /* all args are options; -n NUM */
765 opt_complementary = "-:n+";
766 getopt32(argv, "d:n:b", &sinterval, &iterations);
767 if (option_mask32 & OPT_d) {
768 /* Need to limit it to not overflow poll timeout */
769 interval = xatou16(sinterval); // -d
772 /* change to /proc */
774 #if ENABLE_FEATURE_USE_TERMIOS
775 tcgetattr(0, (void *) &initial_settings);
776 memcpy(&new_settings, &initial_settings, sizeof(new_settings));
777 /* unbuffered input, turn off echo */
778 new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL);
780 bb_signals(BB_FATAL_SIGS, sig_catcher);
781 tcsetattr(0, TCSANOW, (void *) &new_settings);
782 #endif /* FEATURE_USE_TERMIOS */
784 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
785 sort_function[0] = pcpu_sort;
786 sort_function[1] = mem_sort;
787 sort_function[2] = time_sort;
789 sort_function[0] = mem_sort;
790 #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
793 procps_status_t *p = NULL;
795 lines = 24; /* default */
797 #if ENABLE_FEATURE_USE_TERMIOS
798 /* We output to stdout, we need size of stdout (not stdin)! */
799 get_terminal_width_height(STDOUT_FILENO, &col, &lines);
800 if (lines < 5 || col < 10) {
804 #endif /* FEATURE_USE_TERMIOS */
805 if (col > LINE_BUF_SIZE-2) /* +2 bytes for '\n', NUL, */
806 col = LINE_BUF_SIZE-2;
807 if (!ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS && scan_mask == TOP_MASK)
812 /* read process IDs & status for all the processes */
813 while ((p = procps_scan(p, scan_mask)) != NULL) {
815 if (scan_mask == TOP_MASK) {
817 top = xrealloc(top, (++ntop) * sizeof(*top));
819 top[n].ppid = p->ppid;
821 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
822 top[n].ticks = p->stime + p->utime;
825 strcpy(top[n].state, p->state);
826 strcpy(top[n].comm, p->comm);
827 } else { /* TOPMEM */
828 #if ENABLE_FEATURE_TOPMEM
829 if (!(p->mapped_ro | p->mapped_rw))
830 continue; /* kernel threads are ignored */
832 top = xrealloc(topmem, (++ntop) * sizeof(*topmem));
833 strcpy(topmem[n].comm, p->comm);
834 topmem[n].pid = p->pid;
835 topmem[n].vsz = p->mapped_rw + p->mapped_ro;
836 topmem[n].vszrw = p->mapped_rw;
837 topmem[n].rss_sh = p->shared_clean + p->shared_dirty;
838 topmem[n].rss = p->private_clean + p->private_dirty + topmem[n].rss_sh;
839 topmem[n].dirty = p->private_dirty + p->shared_dirty;
840 topmem[n].dirty_sh = p->shared_dirty;
841 topmem[n].stack = p->stack;
844 } /* end of "while we read /proc" */
846 bb_error_msg("no process info in /proc");
850 if (scan_mask == TOP_MASK) {
851 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
852 if (!prev_hist_count) {
859 /* TODO: we don't need to sort all 10000 processes, we need to find top 24! */
860 qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp);
862 qsort(top, ntop, sizeof(top_status_t), (void*)(sort_function[0]));
863 #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
865 #if ENABLE_FEATURE_TOPMEM
867 qsort(topmem, ntop, sizeof(topmem_status_t), (void*)topmem_sort);
871 if (OPT_BATCH_MODE || count > ntop) {
874 if (scan_mask == TOP_MASK)
875 display_process_list(count, col);
876 #if ENABLE_FEATURE_TOPMEM
878 display_topmem_process_list(count, col);
881 if (iterations >= 0 && !--iterations)
883 #if !ENABLE_FEATURE_USE_TERMIOS
886 if (option_mask32 & (OPT_b|OPT_EOF))
887 /* batch mode, or EOF on stdin ("top </dev/null") */
889 else if (safe_poll(pfd, 1, interval * 1000) > 0) {
890 if (safe_read(STDIN_FILENO, &c, 1) != 1) { /* error/EOF? */
891 option_mask32 |= OPT_EOF;
894 if (c == initial_settings.c_cc[VINTR])
896 c |= 0x20; /* lowercase */
900 USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
901 sort_function[0] = pid_sort;
904 USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
905 sort_function[0] = mem_sort;
906 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
907 sort_function[1] = pcpu_sort;
908 sort_function[2] = time_sort;
911 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
913 USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
914 sort_function[0] = pcpu_sort;
915 sort_function[1] = mem_sort;
916 sort_function[2] = time_sort;
919 USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
920 sort_function[0] = time_sort;
921 sort_function[1] = mem_sort;
922 sort_function[2] = pcpu_sort;
924 #if ENABLE_FEATURE_TOPMEM
926 scan_mask = TOPMEM_MASK;
930 sort_field = (sort_field + 1) % NUM_SORT_FIELD;
937 #endif /* FEATURE_USE_TERMIOS */
938 } /* end of "while (1)" */
941 #if ENABLE_FEATURE_USE_TERMIOS