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; */
87 #define G (*(struct globals*)&bb_common_bufsiz1)
89 #define ntop (G.ntop )
90 #define sort_field (G.sort_field )
91 #define inverted (G.inverted )
92 #define initial_settings (G.initial_settings )
93 #define sort_function (G.sort_function )
94 #define prev_hist (G.prev_hist )
95 #define prev_hist_count (G.prev_hist_count )
97 #define prev_jif (G.prev_jif )
98 #define total_pcpu (G.total_pcpu )
101 #define OPT_BATCH_MODE (option_mask32 & 0x4)
104 #if ENABLE_FEATURE_USE_TERMIOS
105 static int pid_sort(top_status_t *P, top_status_t *Q)
107 /* Buggy wrt pids with high bit set */
108 /* (linux pids are in [1..2^15-1]) */
109 return (Q->pid - P->pid);
113 static int mem_sort(top_status_t *P, top_status_t *Q)
115 /* We want to avoid unsigned->signed and truncation errors */
116 if (Q->vsz < P->vsz) return -1;
117 return Q->vsz != P->vsz; /* 0 if ==, 1 if > */
121 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
123 static int pcpu_sort(top_status_t *P, top_status_t *Q)
125 /* Buggy wrt ticks with high bit set */
126 /* Affects only processes for which ticks overflow */
127 return (int)Q->pcpu - (int)P->pcpu;
130 static int time_sort(top_status_t *P, top_status_t *Q)
132 /* We want to avoid unsigned->signed and truncation errors */
133 if (Q->ticks < P->ticks) return -1;
134 return Q->ticks != P->ticks; /* 0 if ==, 1 if > */
137 static int mult_lvl_cmp(void* a, void* b)
141 for (i = 0; i < SORT_DEPTH; i++) {
142 cmp_val = (*sort_function[i])(a, b);
150 static void get_jiffy_counts(void)
152 FILE* fp = xfopen("stat", "r");
154 if (fscanf(fp, "cpu %lld %lld %lld %lld %lld %lld %lld %lld",
155 &jif.usr,&jif.nic,&jif.sys,&jif.idle,
156 &jif.iowait,&jif.irq,&jif.softirq,&jif.steal) < 4) {
157 bb_error_msg_and_die("failed to read /proc/stat");
160 jif.total = jif.usr + jif.nic + jif.sys + jif.idle
161 + jif.iowait + jif.irq + jif.softirq + jif.steal;
162 /* procps 2.x does not count iowait as busy time */
163 jif.busy = jif.total - jif.idle - jif.iowait;
167 static void do_stats(void)
172 struct save_hist *new_hist;
177 new_hist = xmalloc(sizeof(struct save_hist)*ntop);
179 * Make a pass through the data to get stats.
181 /* hist_iterations = 0; */
183 for (n = 0; n < ntop; n++) {
187 * Calculate time in cur process. Time is sum of user time
191 new_hist[n].ticks = cur->ticks;
192 new_hist[n].pid = pid;
194 /* find matching entry from previous pass */
196 /* do not start at index 0, continue at last used one
197 * (brought hist_iterations from ~14000 down to 172) */
199 if (prev_hist_count) do {
200 if (prev_hist[i].pid == pid) {
201 cur->pcpu = cur->ticks - prev_hist[i].ticks;
202 total_pcpu += cur->pcpu;
205 i = (i+1) % prev_hist_count;
206 /* hist_iterations++; */
207 } while (i != last_i);
208 /* total_vsz += cur->vsz; */
212 * Save cur frame's information.
215 prev_hist = new_hist;
216 prev_hist_count = ntop;
218 #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
220 #if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS && ENABLE_FEATURE_TOP_DECIMALS
221 /* formats 7 char string (8 with terminating NUL) */
222 static char *fmt_100percent_8(char pbuf[8], unsigned value, unsigned total)
225 if (value >= total) { /* 100% ? */
226 strcpy(pbuf, " 100% ");
229 /* else generate " [N/space]N.N% " string */
230 value = 1000 * value / total;
234 pbuf[1] = t ? t + '0' : ' ';
235 pbuf[2] = '0' + (value / 10);
237 pbuf[4] = '0' + (value % 10);
245 static unsigned long display_header(int scr_width)
250 unsigned long total, used, mfree, shared, buffers, cached;
251 #if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS
255 /* read memory info */
256 fp = xfopen("meminfo", "r");
259 * Old kernels (such as 2.4.x) had a nice summary of memory info that
260 * we could parse, however this is gone entirely in 2.6. Try parsing
261 * the old way first, and if that fails, parse each field manually.
263 * First, we read in the first line. Old kernels will have bogus
264 * strings we don't care about, whereas new kernels will start right
268 if (fscanf(fp, "MemTotal: %lu %s\n", &total, buf) != 2) {
269 fgets(buf, sizeof(buf), fp); /* skip first line */
271 fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu",
272 &total, &used, &mfree, &shared, &buffers, &cached);
273 /* convert to kilobytes */
282 * Revert to manual parsing, which incidentally already has the
283 * sizes in kilobytes. This should be safe for both 2.4 and
287 fscanf(fp, "MemFree: %lu %s\n", &mfree, buf);
290 * MemShared: is no longer present in 2.6. Report this as 0,
291 * to maintain consistent behavior with normal procps.
293 if (fscanf(fp, "MemShared: %lu %s\n", &shared, buf) != 2)
296 fscanf(fp, "Buffers: %lu %s\n", &buffers, buf);
297 fscanf(fp, "Cached: %lu %s\n", &cached, buf);
299 used = total - mfree;
303 /* output memory info */
304 if (scr_width > sizeof(scrbuf))
305 scr_width = sizeof(scrbuf);
306 snprintf(scrbuf, scr_width,
307 "Mem: %luK used, %luK free, %luK shrd, %luK buff, %luK cached",
308 used, mfree, shared, buffers, cached);
309 /* clear screen & go to top */
310 printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf);
312 #if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS
314 * xxx% = (jif.xxx - prev_jif.xxx) / (jif.total - prev_jif.total) * 100%
316 /* using (unsigned) casts to make operations cheaper */
317 total_diff = ((unsigned)(jif.total - prev_jif.total) ? : 1);
318 #if ENABLE_FEATURE_TOP_DECIMALS
319 /* Generated code is approx +0.3k */
320 #define CALC_STAT(xxx) char xxx[8]
321 #define SHOW_STAT(xxx) fmt_100percent_8(xxx, (unsigned)(jif.xxx - prev_jif.xxx), total_diff)
324 #define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff
325 #define SHOW_STAT(xxx) xxx
328 { /* need block: CALC_STAT are declarations */
338 snprintf(scrbuf, scr_width,
339 /* Barely fits in 79 chars when in "decimals" mode. */
340 "CPU:"FMT"usr"FMT"sys"FMT"nice"FMT"idle"FMT"io"FMT"irq"FMT"softirq",
341 SHOW_STAT(usr), SHOW_STAT(sys), SHOW_STAT(nic), SHOW_STAT(idle),
342 SHOW_STAT(iowait), SHOW_STAT(irq), SHOW_STAT(softirq)
343 //, SHOW_STAT(steal) - what is this 'steal' thing?
344 // I doubt anyone wants to know it
353 /* read load average as a string */
355 open_read_close("loadavg", buf, sizeof("N.NN N.NN N.NN")-1);
356 buf[sizeof("N.NN N.NN N.NN")-1] = '\0';
357 snprintf(scrbuf, scr_width, "Load average: %s", buf);
363 static void display_process_list(int count, int scr_width)
366 BITS_PER_INT = sizeof(int)*8
369 top_status_t *s = top;
371 unsigned long total_memory = display_header(scr_width); /* or use total_vsz? */
372 /* xxx_shift and xxx_scale variables allow us to replace
373 * expensive divides with multiply and shift */
374 unsigned pmem_shift, pmem_scale, pmem_half;
375 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
376 unsigned pcpu_shift, pcpu_scale, pcpu_half;
379 /* what info of the processes is shown */
380 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
381 " PID PPID USER STAT VSZ %MEM %CPU COMMAND");
384 /* !CPU_USAGE_PERCENTAGE */
385 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
386 " PID PPID USER STAT VSZ %MEM COMMAND");
389 #if ENABLE_FEATURE_TOP_DECIMALS
391 #define CALC_STAT(name, val) div_t name = div((val), 10)
392 #define SHOW_STAT(name) name.quot, '0'+name.rem
396 #define CALC_STAT(name, val) unsigned name = (val)
397 #define SHOW_STAT(name) name
401 * MEM% = s->vsz/MemTotal
403 pmem_shift = BITS_PER_INT-11;
404 pmem_scale = UPSCALE*(1U<<(BITS_PER_INT-11)) / total_memory;
405 /* s->vsz is in kb. we want (s->vsz * pmem_scale) to never overflow */
406 while (pmem_scale >= 512) {
410 pmem_half = (1U << pmem_shift) / (ENABLE_FEATURE_TOP_DECIMALS? 20 : 2);
411 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
412 busy_jifs = jif.busy - prev_jif.busy;
413 /* This happens if there were lots of short-lived processes
414 * between two top updates (e.g. compilation) */
415 if (total_pcpu < busy_jifs) total_pcpu = busy_jifs;
418 * CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks
419 * (pcpu is delta of sys+user time between samples)
421 /* (jif.xxx - prev_jif.xxx) and s->pcpu are
422 * in 0..~64000 range (HZ*update_interval).
423 * we assume that unsigned is at least 32-bit.
426 pcpu_scale = (UPSCALE*64*(uint16_t)busy_jifs ? : 1);
427 while (pcpu_scale < (1U<<(BITS_PER_INT-2))) {
431 pcpu_scale /= ( (uint16_t)(jif.total-prev_jif.total)*total_pcpu ? : 1);
432 /* we want (s->pcpu * pcpu_scale) to never overflow */
433 while (pcpu_scale >= 1024) {
437 pcpu_half = (1U << pcpu_shift) / (ENABLE_FEATURE_TOP_DECIMALS? 20 : 2);
438 /* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */
441 scr_width += 2; /* account for leading '\n' and trailing NUL */
442 /* Ok, all preliminary data is ready, go thru the list */
443 while (count-- > 0) {
446 CALC_STAT(pmem, (s->vsz*pmem_scale + pmem_half) >> pmem_shift);
447 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
448 CALC_STAT(pcpu, (s->pcpu*pcpu_scale + pcpu_half) >> pcpu_shift);
451 if (s->vsz >= 100000)
452 sprintf(vsz_str_buf, "%6ldm", s->vsz/1024);
454 sprintf(vsz_str_buf, "%7ld", s->vsz);
455 // PID PPID USER STAT VSZ %MEM [%CPU] COMMAND
456 col = snprintf(buf, scr_width,
457 "\n" "%5u%6u %-8.8s %s%s" FMT
458 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
462 s->pid, s->ppid, get_cached_username(s->uid),
463 s->state, vsz_str_buf,
465 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
470 read_cmdline(buf + col, scr_width - col, s->pid, s->comm);
472 /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu,
473 jif.busy - prev_jif.busy, jif.total - prev_jif.total); */
476 /* printf(" %d", hist_iterations); */
477 putchar(OPT_BATCH_MODE ? '\n' : '\r');
485 static void clearmems(void)
487 clear_username_cache();
493 #if ENABLE_FEATURE_USE_TERMIOS
497 static void reset_term(void)
499 tcsetattr(0, TCSANOW, (void *) &initial_settings);
500 if (ENABLE_FEATURE_CLEAN_UP) {
502 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
508 static void sig_catcher(int sig ATTRIBUTE_UNUSED)
513 #endif /* FEATURE_USE_TERMIOS */
530 typedef unsigned long mem_t;
532 typedef struct topmem_status_t {
535 /* vsz doesn't count /dev/xxx mappings except /dev/zero */
545 enum { NUM_SORT_FIELD = 7 };
547 #define topmem ((topmem_status_t*)top)
549 #if ENABLE_FEATURE_TOPMEM
550 static int topmem_sort(char *a, char *b)
555 n = offsetof(topmem_status_t, vsz) + (sort_field * sizeof(mem_t));
556 l = *(mem_t*)(a + n);
557 r = *(mem_t*)(b + n);
562 /* We want to avoid unsigned->signed and truncation errors */
563 /* l>r: -1, l=r: 0, l<r: 1 */
564 n = (l > r) ? -1 : (l != r);
565 return inverted ? -n : n;
568 /* Cut "NNNN " out of " NNNN kb" */
569 static char *grab_number(char *str, const char *match, unsigned sz)
571 if (strncmp(str, match, sz) == 0) {
572 str = skip_whitespace(str + sz);
573 (skip_non_whitespace(str))[1] = '\0';
579 /* display header info (meminfo / loadavg) */
580 static void display_topmem_header(int scr_width)
591 /* 5 */ char *swaptotal;
592 /* 6 */ char *swapfree;
594 /* 8 */ char *mwrite;
601 #define total Z.total
602 #define mfree Z.mfree
604 #define cache Z.cache
605 #define swaptotal Z.swaptotal
606 #define swapfree Z.swapfree
607 #define dirty Z.dirty
608 #define mwrite Z.mwrite
614 memset(&Z, 0, sizeof(Z));
616 /* read memory info */
617 fp = xfopen("meminfo", "r");
618 while (fgets(linebuf, sizeof(linebuf), fp)) {
621 #define SCAN(match, name) \
622 p = grab_number(linebuf, match, sizeof(match)-1); \
623 if (p) { name = p; continue; }
625 SCAN("MemTotal:", total);
626 SCAN("MemFree:", mfree);
627 SCAN("Buffers:", buf);
628 SCAN("Cached:", cache);
629 SCAN("SwapTotal:", swaptotal);
630 SCAN("SwapFree:", swapfree);
631 SCAN("Dirty:", dirty);
632 SCAN("Writeback:", mwrite);
633 SCAN("AnonPages:", anon);
634 SCAN("Mapped:", map);
640 #define S(s) (s ? s : "0")
641 snprintf(linebuf, sizeof(linebuf),
642 "Mem %stotal %sanon %smap %sfree",
643 S(total), S(anon), S(map), S(mfree));
644 printf(OPT_BATCH_MODE ? "%.*s\n" : "\e[H\e[J%.*s\n", scr_width, linebuf);
646 snprintf(linebuf, sizeof(linebuf),
647 " %sslab %sbuf %scache %sdirty %swrite",
648 S(slab), S(buf), S(cache), S(dirty), S(mwrite));
649 printf("%.*s\n", scr_width, linebuf);
651 snprintf(linebuf, sizeof(linebuf),
652 "Swap %stotal %sfree", // TODO: % used?
653 S(swaptotal), S(swapfree));
654 printf("%.*s\n", scr_width, linebuf);
657 for (i = 0; i < ARRAY_SIZE(str); i++)
673 // Converts unsigned long long value into compact 5-char
674 // representation. Sixth char is always ' '
675 static void smart_ulltoa6(unsigned long long ul, char buf[6])
679 unsigned v, u, idx = 0;
681 if (ul > 99999) { // do not scale if 99999 or less
686 } while (ul >= 100000);
688 v = ul; // ullong divisions are expensive, avoid them
694 // 99999 or less: use "12345" format
695 // u is value/10, v is last digit
696 c = buf[0] = " 123456789"[u/1000];
697 if (c != ' ') fmt = "0123456789";
698 c = buf[1] = fmt[u/100%10];
699 if (c != ' ') fmt = "0123456789";
700 c = buf[2] = fmt[u/10%10];
701 if (c != ' ') fmt = "0123456789";
703 buf[4] = "0123456789"[v];
705 // value has been scaled into 0..9999.9 range
706 // u is value, v is 1/10ths (allows for 92.1M format)
708 // value is >= 100: use "1234M', " 123M" formats
709 c = buf[0] = " 123456789"[u/1000];
710 if (c != ' ') fmt = "0123456789";
711 c = buf[1] = fmt[u/100%10];
712 if (c != ' ') fmt = "0123456789";
717 // value is < 100: use "92.1M" format
718 c = buf[0] = " 123456789"[u/10];
719 if (c != ' ') fmt = "0123456789";
723 buf[3] = "0123456789"[v];
724 // see http://en.wikipedia.org/wiki/Tera
725 buf[4] = " mgtpezy"[idx];
730 static void display_topmem_process_list(int count, int scr_width)
732 #define HDR_STR " PID VSZ VSZRW RSS (SHR) DIRTY (SHR) STACK"
733 #define MIN_WIDTH sizeof(HDR_STR)
734 const topmem_status_t *s = topmem;
735 char buf[scr_width | MIN_WIDTH]; /* a|b is a cheap max(a,b) */
737 display_topmem_header(scr_width);
738 strcpy(buf, HDR_STR " COMMAND");
739 buf[5 + sort_field * 6] = '*';
740 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width, buf);
742 while (--count >= 0) {
743 // PID VSZ VSZRW RSS (SHR) DIRTY (SHR) COMMAND
744 smart_ulltoa6(s->pid , &buf[0*6]);
745 smart_ulltoa6(s->vsz , &buf[1*6]);
746 smart_ulltoa6(s->vszrw , &buf[2*6]);
747 smart_ulltoa6(s->rss , &buf[3*6]);
748 smart_ulltoa6(s->rss_sh , &buf[4*6]);
749 smart_ulltoa6(s->dirty , &buf[5*6]);
750 smart_ulltoa6(s->dirty_sh, &buf[6*6]);
751 smart_ulltoa6(s->stack , &buf[7*6]);
753 if (scr_width > MIN_WIDTH) {
754 read_cmdline(&buf[8*6], scr_width - MIN_WIDTH, s->pid, s->comm);
756 printf("\n""%.*s", scr_width, buf);
759 putchar(OPT_BATCH_MODE ? '\n' : '\r');
765 void display_topmem_process_list(int count, int scr_width);
766 int topmem_sort(char *a, char *b);
800 int top_main(int argc, char **argv);
801 int top_main(int argc, char **argv)
803 int count, lines, col;
805 int iterations = 0; /* infinite */
806 char *sinterval, *siterations;
807 SKIP_FEATURE_TOPMEM(const) unsigned scan_mask = TOP_MASK;
808 #if ENABLE_FEATURE_USE_TERMIOS
809 struct termios new_settings;
810 struct pollfd pfd[1];
814 pfd[0].events = POLLIN;
815 #endif /* FEATURE_USE_TERMIOS */
817 interval = 5; /* default update rate is 5 seconds */
819 /* do normal option parsing */
820 opt_complementary = "-";
821 getopt32(argv, "d:n:b", &sinterval, &siterations);
822 if (option_mask32 & 0x1) {
823 /* Need to limit it to not overflow poll timeout */
824 interval = xatou16(sinterval); // -d
826 if (option_mask32 & 0x2)
827 iterations = xatoi_u(siterations); // -n
828 //if (option_mask32 & 0x4) // -b
830 /* change to /proc */
832 #if ENABLE_FEATURE_USE_TERMIOS
833 tcgetattr(0, (void *) &initial_settings);
834 memcpy(&new_settings, &initial_settings, sizeof(new_settings));
835 /* unbuffered input, turn off echo */
836 new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL);
838 signal(SIGTERM, sig_catcher);
839 signal(SIGINT, sig_catcher);
840 tcsetattr(0, TCSANOW, (void *) &new_settings);
842 #endif /* FEATURE_USE_TERMIOS */
844 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
845 sort_function[0] = pcpu_sort;
846 sort_function[1] = mem_sort;
847 sort_function[2] = time_sort;
849 sort_function[0] = mem_sort;
850 #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
853 procps_status_t *p = NULL;
858 #if ENABLE_FEATURE_USE_TERMIOS
859 get_terminal_width_height(0, &col, &lines);
860 if (lines < 5 || col < 10) {
864 #endif /* FEATURE_USE_TERMIOS */
865 if (!ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS && scan_mask == TOP_MASK)
870 /* read process IDs & status for all the processes */
871 while ((p = procps_scan(p, scan_mask)) != NULL) {
873 if (scan_mask == TOP_MASK) {
875 top = xrealloc(top, (++ntop) * sizeof(*top));
877 top[n].ppid = p->ppid;
879 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
880 top[n].ticks = p->stime + p->utime;
883 strcpy(top[n].state, p->state);
884 strcpy(top[n].comm, p->comm);
885 } else { /* TOPMEM */
886 #if ENABLE_FEATURE_TOPMEM
887 if (!(p->mapped_ro | p->mapped_rw))
888 continue; /* kernel threads are ignored */
890 top = xrealloc(topmem, (++ntop) * sizeof(*topmem));
891 strcpy(topmem[n].comm, p->comm);
892 topmem[n].pid = p->pid;
893 topmem[n].vsz = p->mapped_rw + p->mapped_ro;
894 topmem[n].vszrw = p->mapped_rw;
895 topmem[n].rss_sh = p->shared_clean + p->shared_dirty;
896 topmem[n].rss = p->private_clean + p->private_dirty + topmem[n].rss_sh;
897 topmem[n].dirty = p->private_dirty + p->shared_dirty;
898 topmem[n].dirty_sh = p->shared_dirty;
899 topmem[n].stack = p->stack;
904 bb_error_msg_and_die("no process info in /proc");
907 if (scan_mask == TOP_MASK) {
908 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
909 if (!prev_hist_count) {
916 /* TODO: we don't need to sort all 10000 processes, we need to find top 24! */
917 qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp);
919 qsort(top, ntop, sizeof(top_status_t), (void*)(sort_function[0]));
920 #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
921 } else { /* TOPMEM */
922 qsort(topmem, ntop, sizeof(topmem_status_t), (void*)topmem_sort);
925 if (OPT_BATCH_MODE || count > ntop) {
928 if (scan_mask == TOP_MASK)
929 display_process_list(count, col);
931 display_topmem_process_list(count, col);
933 if (iterations >= 0 && !--iterations)
935 #if !ENABLE_FEATURE_USE_TERMIOS
938 if (poll(pfd, 1, interval * 1000) != 0) {
939 if (read(0, &c, 1) != 1) /* signal */
941 if (c == initial_settings.c_cc[VINTR])
943 c |= 0x20; /* lowercase */
947 USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
948 sort_function[0] = pid_sort;
951 USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
952 sort_function[0] = mem_sort;
953 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
954 sort_function[1] = pcpu_sort;
955 sort_function[2] = time_sort;
958 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
960 USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
961 sort_function[0] = pcpu_sort;
962 sort_function[1] = mem_sort;
963 sort_function[2] = time_sort;
966 USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
967 sort_function[0] = time_sort;
968 sort_function[1] = mem_sort;
969 sort_function[2] = pcpu_sort;
971 #if ENABLE_FEATURE_TOPMEM
973 scan_mask = TOPMEM_MASK;
977 sort_field = (sort_field + 1) % NUM_SORT_FIELD;
984 #endif /* FEATURE_USE_TERMIOS */