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)
90 struct G_sizecheck { \
91 char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
95 #define ntop (G.ntop )
96 #define sort_field (G.sort_field )
97 #define inverted (G.inverted )
98 #define initial_settings (G.initial_settings )
99 #define sort_function (G.sort_function )
100 #define prev_hist (G.prev_hist )
101 #define prev_hist_count (G.prev_hist_count )
103 #define prev_jif (G.prev_jif )
104 #define total_pcpu (G.total_pcpu )
107 #define OPT_BATCH_MODE (option_mask32 & 0x4)
110 #if ENABLE_FEATURE_USE_TERMIOS
111 static int pid_sort(top_status_t *P, top_status_t *Q)
113 /* Buggy wrt pids with high bit set */
114 /* (linux pids are in [1..2^15-1]) */
115 return (Q->pid - P->pid);
119 static int mem_sort(top_status_t *P, top_status_t *Q)
121 /* We want to avoid unsigned->signed and truncation errors */
122 if (Q->vsz < P->vsz) return -1;
123 return Q->vsz != P->vsz; /* 0 if ==, 1 if > */
127 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
129 static int pcpu_sort(top_status_t *P, top_status_t *Q)
131 /* Buggy wrt ticks with high bit set */
132 /* Affects only processes for which ticks overflow */
133 return (int)Q->pcpu - (int)P->pcpu;
136 static int time_sort(top_status_t *P, top_status_t *Q)
138 /* We want to avoid unsigned->signed and truncation errors */
139 if (Q->ticks < P->ticks) return -1;
140 return Q->ticks != P->ticks; /* 0 if ==, 1 if > */
143 static int mult_lvl_cmp(void* a, void* b)
147 for (i = 0; i < SORT_DEPTH; i++) {
148 cmp_val = (*sort_function[i])(a, b);
156 static void get_jiffy_counts(void)
158 FILE* fp = xfopen("stat", "r");
160 if (fscanf(fp, "cpu %lld %lld %lld %lld %lld %lld %lld %lld",
161 &jif.usr,&jif.nic,&jif.sys,&jif.idle,
162 &jif.iowait,&jif.irq,&jif.softirq,&jif.steal) < 4) {
163 bb_error_msg_and_die("failed to read /proc/stat");
166 jif.total = jif.usr + jif.nic + jif.sys + jif.idle
167 + jif.iowait + jif.irq + jif.softirq + jif.steal;
168 /* procps 2.x does not count iowait as busy time */
169 jif.busy = jif.total - jif.idle - jif.iowait;
173 static void do_stats(void)
178 struct save_hist *new_hist;
183 new_hist = xmalloc(sizeof(struct save_hist)*ntop);
185 * Make a pass through the data to get stats.
187 /* hist_iterations = 0; */
189 for (n = 0; n < ntop; n++) {
193 * Calculate time in cur process. Time is sum of user time
197 new_hist[n].ticks = cur->ticks;
198 new_hist[n].pid = pid;
200 /* find matching entry from previous pass */
202 /* do not start at index 0, continue at last used one
203 * (brought hist_iterations from ~14000 down to 172) */
205 if (prev_hist_count) do {
206 if (prev_hist[i].pid == pid) {
207 cur->pcpu = cur->ticks - prev_hist[i].ticks;
208 total_pcpu += cur->pcpu;
211 i = (i+1) % prev_hist_count;
212 /* hist_iterations++; */
213 } while (i != last_i);
214 /* total_vsz += cur->vsz; */
218 * Save cur frame's information.
221 prev_hist = new_hist;
222 prev_hist_count = ntop;
224 #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
226 #if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS && ENABLE_FEATURE_TOP_DECIMALS
227 /* formats 7 char string (8 with terminating NUL) */
228 static char *fmt_100percent_8(char pbuf[8], unsigned value, unsigned total)
231 if (value >= total) { /* 100% ? */
232 strcpy(pbuf, " 100% ");
235 /* else generate " [N/space]N.N% " string */
236 value = 1000 * value / total;
240 pbuf[1] = t ? t + '0' : ' ';
241 pbuf[2] = '0' + (value / 10);
243 pbuf[4] = '0' + (value % 10);
251 static unsigned long display_header(int scr_width)
256 unsigned long total, used, mfree, shared, buffers, cached;
257 #if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS
261 /* read memory info */
262 fp = xfopen("meminfo", "r");
265 * Old kernels (such as 2.4.x) had a nice summary of memory info that
266 * we could parse, however this is gone entirely in 2.6. Try parsing
267 * the old way first, and if that fails, parse each field manually.
269 * First, we read in the first line. Old kernels will have bogus
270 * strings we don't care about, whereas new kernels will start right
274 if (fscanf(fp, "MemTotal: %lu %s\n", &total, buf) != 2) {
275 fgets(buf, sizeof(buf), fp); /* skip first line */
277 fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu",
278 &total, &used, &mfree, &shared, &buffers, &cached);
279 /* convert to kilobytes */
288 * Revert to manual parsing, which incidentally already has the
289 * sizes in kilobytes. This should be safe for both 2.4 and
293 fscanf(fp, "MemFree: %lu %s\n", &mfree, buf);
296 * MemShared: is no longer present in 2.6. Report this as 0,
297 * to maintain consistent behavior with normal procps.
299 if (fscanf(fp, "MemShared: %lu %s\n", &shared, buf) != 2)
302 fscanf(fp, "Buffers: %lu %s\n", &buffers, buf);
303 fscanf(fp, "Cached: %lu %s\n", &cached, buf);
305 used = total - mfree;
309 /* output memory info */
310 if (scr_width > sizeof(scrbuf))
311 scr_width = sizeof(scrbuf);
312 snprintf(scrbuf, scr_width,
313 "Mem: %luK used, %luK free, %luK shrd, %luK buff, %luK cached",
314 used, mfree, shared, buffers, cached);
315 /* clear screen & go to top */
316 printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf);
318 #if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS
320 * xxx% = (jif.xxx - prev_jif.xxx) / (jif.total - prev_jif.total) * 100%
322 /* using (unsigned) casts to make operations cheaper */
323 total_diff = ((unsigned)(jif.total - prev_jif.total) ? : 1);
324 #if ENABLE_FEATURE_TOP_DECIMALS
325 /* Generated code is approx +0.3k */
326 #define CALC_STAT(xxx) char xxx[8]
327 #define SHOW_STAT(xxx) fmt_100percent_8(xxx, (unsigned)(jif.xxx - prev_jif.xxx), total_diff)
330 #define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff
331 #define SHOW_STAT(xxx) xxx
334 { /* need block: CALC_STAT are declarations */
344 snprintf(scrbuf, scr_width,
345 /* Barely fits in 79 chars when in "decimals" mode. */
346 "CPU:"FMT"usr"FMT"sys"FMT"nice"FMT"idle"FMT"io"FMT"irq"FMT"softirq",
347 SHOW_STAT(usr), SHOW_STAT(sys), SHOW_STAT(nic), SHOW_STAT(idle),
348 SHOW_STAT(iowait), SHOW_STAT(irq), SHOW_STAT(softirq)
349 //, SHOW_STAT(steal) - what is this 'steal' thing?
350 // I doubt anyone wants to know it
359 /* read load average as a string */
361 open_read_close("loadavg", buf, sizeof("N.NN N.NN N.NN")-1);
362 buf[sizeof("N.NN N.NN N.NN")-1] = '\0';
363 snprintf(scrbuf, scr_width, "Load average: %s", buf);
369 static void display_process_list(int count, int scr_width)
372 BITS_PER_INT = sizeof(int)*8
375 top_status_t *s = top;
377 unsigned long total_memory = display_header(scr_width); /* or use total_vsz? */
378 /* xxx_shift and xxx_scale variables allow us to replace
379 * expensive divides with multiply and shift */
380 unsigned pmem_shift, pmem_scale, pmem_half;
381 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
382 unsigned pcpu_shift, pcpu_scale, pcpu_half;
385 /* what info of the processes is shown */
386 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
387 " PID PPID USER STAT VSZ %MEM %CPU COMMAND");
390 /* !CPU_USAGE_PERCENTAGE */
391 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
392 " PID PPID USER STAT VSZ %MEM COMMAND");
395 #if ENABLE_FEATURE_TOP_DECIMALS
397 #define CALC_STAT(name, val) div_t name = div((val), 10)
398 #define SHOW_STAT(name) name.quot, '0'+name.rem
402 #define CALC_STAT(name, val) unsigned name = (val)
403 #define SHOW_STAT(name) name
407 * MEM% = s->vsz/MemTotal
409 pmem_shift = BITS_PER_INT-11;
410 pmem_scale = UPSCALE*(1U<<(BITS_PER_INT-11)) / total_memory;
411 /* s->vsz is in kb. we want (s->vsz * pmem_scale) to never overflow */
412 while (pmem_scale >= 512) {
416 pmem_half = (1U << pmem_shift) / (ENABLE_FEATURE_TOP_DECIMALS? 20 : 2);
417 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
418 busy_jifs = jif.busy - prev_jif.busy;
419 /* This happens if there were lots of short-lived processes
420 * between two top updates (e.g. compilation) */
421 if (total_pcpu < busy_jifs) total_pcpu = busy_jifs;
424 * CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks
425 * (pcpu is delta of sys+user time between samples)
427 /* (jif.xxx - prev_jif.xxx) and s->pcpu are
428 * in 0..~64000 range (HZ*update_interval).
429 * we assume that unsigned is at least 32-bit.
432 pcpu_scale = (UPSCALE*64*(uint16_t)busy_jifs ? : 1);
433 while (pcpu_scale < (1U<<(BITS_PER_INT-2))) {
437 pcpu_scale /= ( (uint16_t)(jif.total-prev_jif.total)*total_pcpu ? : 1);
438 /* we want (s->pcpu * pcpu_scale) to never overflow */
439 while (pcpu_scale >= 1024) {
443 pcpu_half = (1U << pcpu_shift) / (ENABLE_FEATURE_TOP_DECIMALS? 20 : 2);
444 /* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */
447 scr_width += 2; /* account for leading '\n' and trailing NUL */
448 /* Ok, all preliminary data is ready, go thru the list */
449 while (count-- > 0) {
452 CALC_STAT(pmem, (s->vsz*pmem_scale + pmem_half) >> pmem_shift);
453 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
454 CALC_STAT(pcpu, (s->pcpu*pcpu_scale + pcpu_half) >> pcpu_shift);
457 if (s->vsz >= 100000)
458 sprintf(vsz_str_buf, "%6ldm", s->vsz/1024);
460 sprintf(vsz_str_buf, "%7ld", s->vsz);
461 // PID PPID USER STAT VSZ %MEM [%CPU] COMMAND
462 col = snprintf(buf, scr_width,
463 "\n" "%5u%6u %-8.8s %s%s" FMT
464 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
468 s->pid, s->ppid, get_cached_username(s->uid),
469 s->state, vsz_str_buf,
471 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
476 read_cmdline(buf + col, scr_width - col, s->pid, s->comm);
478 /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu,
479 jif.busy - prev_jif.busy, jif.total - prev_jif.total); */
482 /* printf(" %d", hist_iterations); */
483 putchar(OPT_BATCH_MODE ? '\n' : '\r');
491 static void clearmems(void)
493 clear_username_cache();
499 #if ENABLE_FEATURE_USE_TERMIOS
503 static void reset_term(void)
505 tcsetattr(0, TCSANOW, (void *) &initial_settings);
506 if (ENABLE_FEATURE_CLEAN_UP) {
508 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
514 static void sig_catcher(int sig ATTRIBUTE_UNUSED)
519 #endif /* FEATURE_USE_TERMIOS */
525 typedef unsigned long mem_t;
527 typedef struct topmem_status_t {
530 /* vsz doesn't count /dev/xxx mappings except /dev/zero */
540 enum { NUM_SORT_FIELD = 7 };
542 #define topmem ((topmem_status_t*)top)
544 #if ENABLE_FEATURE_TOPMEM
545 static int topmem_sort(char *a, char *b)
550 n = offsetof(topmem_status_t, vsz) + (sort_field * sizeof(mem_t));
551 l = *(mem_t*)(a + n);
552 r = *(mem_t*)(b + n);
557 /* We want to avoid unsigned->signed and truncation errors */
558 /* l>r: -1, l=r: 0, l<r: 1 */
559 n = (l > r) ? -1 : (l != r);
560 return inverted ? -n : n;
563 /* Cut "NNNN " out of " NNNN kb" */
564 static char *grab_number(char *str, const char *match, unsigned sz)
566 if (strncmp(str, match, sz) == 0) {
567 str = skip_whitespace(str + sz);
568 (skip_non_whitespace(str))[1] = '\0';
574 /* display header info (meminfo / loadavg) */
575 static void display_topmem_header(int scr_width)
586 /* 5 */ char *swaptotal;
587 /* 6 */ char *swapfree;
589 /* 8 */ char *mwrite;
596 #define total Z.total
597 #define mfree Z.mfree
599 #define cache Z.cache
600 #define swaptotal Z.swaptotal
601 #define swapfree Z.swapfree
602 #define dirty Z.dirty
603 #define mwrite Z.mwrite
609 memset(&Z, 0, sizeof(Z));
611 /* read memory info */
612 fp = xfopen("meminfo", "r");
613 while (fgets(linebuf, sizeof(linebuf), fp)) {
616 #define SCAN(match, name) \
617 p = grab_number(linebuf, match, sizeof(match)-1); \
618 if (p) { name = p; continue; }
620 SCAN("MemTotal:", total);
621 SCAN("MemFree:", mfree);
622 SCAN("Buffers:", buf);
623 SCAN("Cached:", cache);
624 SCAN("SwapTotal:", swaptotal);
625 SCAN("SwapFree:", swapfree);
626 SCAN("Dirty:", dirty);
627 SCAN("Writeback:", mwrite);
628 SCAN("AnonPages:", anon);
629 SCAN("Mapped:", map);
635 #define S(s) (s ? s : "0")
636 snprintf(linebuf, sizeof(linebuf),
637 "Mem %stotal %sanon %smap %sfree",
638 S(total), S(anon), S(map), S(mfree));
639 printf(OPT_BATCH_MODE ? "%.*s\n" : "\e[H\e[J%.*s\n", scr_width, linebuf);
641 snprintf(linebuf, sizeof(linebuf),
642 " %sslab %sbuf %scache %sdirty %swrite",
643 S(slab), S(buf), S(cache), S(dirty), S(mwrite));
644 printf("%.*s\n", scr_width, linebuf);
646 snprintf(linebuf, sizeof(linebuf),
647 "Swap %stotal %sfree", // TODO: % used?
648 S(swaptotal), S(swapfree));
649 printf("%.*s\n", scr_width, linebuf);
652 for (i = 0; i < ARRAY_SIZE(str); i++)
668 // Converts unsigned long long value into compact 5-char
669 // representation. Sixth char is always ' '
670 static void smart_ulltoa6(unsigned long long ul, char buf[6])
674 unsigned v, u, idx = 0;
676 if (ul > 99999) { // do not scale if 99999 or less
681 } while (ul >= 100000);
683 v = ul; // ullong divisions are expensive, avoid them
689 // 99999 or less: use "12345" format
690 // u is value/10, v is last digit
691 c = buf[0] = " 123456789"[u/1000];
692 if (c != ' ') fmt = "0123456789";
693 c = buf[1] = fmt[u/100%10];
694 if (c != ' ') fmt = "0123456789";
695 c = buf[2] = fmt[u/10%10];
696 if (c != ' ') fmt = "0123456789";
698 buf[4] = "0123456789"[v];
700 // value has been scaled into 0..9999.9 range
701 // u is value, v is 1/10ths (allows for 92.1M format)
703 // value is >= 100: use "1234M', " 123M" formats
704 c = buf[0] = " 123456789"[u/1000];
705 if (c != ' ') fmt = "0123456789";
706 c = buf[1] = fmt[u/100%10];
707 if (c != ' ') fmt = "0123456789";
712 // value is < 100: use "92.1M" format
713 c = buf[0] = " 123456789"[u/10];
714 if (c != ' ') fmt = "0123456789";
718 buf[3] = "0123456789"[v];
719 // see http://en.wikipedia.org/wiki/Tera
720 buf[4] = " mgtpezy"[idx];
725 static void display_topmem_process_list(int count, int scr_width)
727 #define HDR_STR " PID VSZ VSZRW RSS (SHR) DIRTY (SHR) STACK"
728 #define MIN_WIDTH sizeof(HDR_STR)
729 const topmem_status_t *s = topmem;
730 char buf[scr_width | MIN_WIDTH]; /* a|b is a cheap max(a,b) */
732 display_topmem_header(scr_width);
733 strcpy(buf, HDR_STR " COMMAND");
734 buf[5 + sort_field * 6] = '*';
735 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width, buf);
737 while (--count >= 0) {
738 // PID VSZ VSZRW RSS (SHR) DIRTY (SHR) COMMAND
739 smart_ulltoa6(s->pid , &buf[0*6]);
740 smart_ulltoa6(s->vsz , &buf[1*6]);
741 smart_ulltoa6(s->vszrw , &buf[2*6]);
742 smart_ulltoa6(s->rss , &buf[3*6]);
743 smart_ulltoa6(s->rss_sh , &buf[4*6]);
744 smart_ulltoa6(s->dirty , &buf[5*6]);
745 smart_ulltoa6(s->dirty_sh, &buf[6*6]);
746 smart_ulltoa6(s->stack , &buf[7*6]);
748 if (scr_width > MIN_WIDTH) {
749 read_cmdline(&buf[8*6], scr_width - MIN_WIDTH, s->pid, s->comm);
751 printf("\n""%.*s", scr_width, buf);
754 putchar(OPT_BATCH_MODE ? '\n' : '\r');
760 void display_topmem_process_list(int count, int scr_width);
761 int topmem_sort(char *a, char *b);
784 int top_main(int argc, char **argv);
785 int top_main(int argc, char **argv)
787 int count, lines, col;
790 char *sinterval, *siterations;
791 SKIP_FEATURE_TOPMEM(const) unsigned scan_mask = TOP_MASK;
792 #if ENABLE_FEATURE_USE_TERMIOS
793 struct termios new_settings;
794 struct pollfd pfd[1];
798 pfd[0].events = POLLIN;
799 #endif /* FEATURE_USE_TERMIOS */
803 interval = 5; /* default update rate is 5 seconds */
804 iterations = 0; /* infinite */
806 /* do normal option parsing */
807 opt_complementary = "-";
808 getopt32(argv, "d:n:b", &sinterval, &siterations);
809 if (option_mask32 & 0x1) {
810 /* Need to limit it to not overflow poll timeout */
811 interval = xatou16(sinterval); // -d
813 if (option_mask32 & 0x2)
814 iterations = xatoi_u(siterations); // -n
815 //if (option_mask32 & 0x4) // -b
817 /* change to /proc */
819 #if ENABLE_FEATURE_USE_TERMIOS
820 tcgetattr(0, (void *) &initial_settings);
821 memcpy(&new_settings, &initial_settings, sizeof(new_settings));
822 /* unbuffered input, turn off echo */
823 new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL);
825 signal(SIGTERM, sig_catcher);
826 signal(SIGINT, sig_catcher);
827 tcsetattr(0, TCSANOW, (void *) &new_settings);
829 #endif /* FEATURE_USE_TERMIOS */
831 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
832 sort_function[0] = pcpu_sort;
833 sort_function[1] = mem_sort;
834 sort_function[2] = time_sort;
836 sort_function[0] = mem_sort;
837 #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
840 procps_status_t *p = NULL;
842 lines = 24; /* default */
844 #if ENABLE_FEATURE_USE_TERMIOS
845 get_terminal_width_height(0, &col, &lines);
846 if (lines < 5 || col < 10) {
850 #endif /* FEATURE_USE_TERMIOS */
851 if (!ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS && scan_mask == TOP_MASK)
856 /* read process IDs & status for all the processes */
857 while ((p = procps_scan(p, scan_mask)) != NULL) {
859 if (scan_mask == TOP_MASK) {
861 top = xrealloc(top, (++ntop) * sizeof(*top));
863 top[n].ppid = p->ppid;
865 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
866 top[n].ticks = p->stime + p->utime;
869 strcpy(top[n].state, p->state);
870 strcpy(top[n].comm, p->comm);
871 } else { /* TOPMEM */
872 #if ENABLE_FEATURE_TOPMEM
873 if (!(p->mapped_ro | p->mapped_rw))
874 continue; /* kernel threads are ignored */
876 top = xrealloc(topmem, (++ntop) * sizeof(*topmem));
877 strcpy(topmem[n].comm, p->comm);
878 topmem[n].pid = p->pid;
879 topmem[n].vsz = p->mapped_rw + p->mapped_ro;
880 topmem[n].vszrw = p->mapped_rw;
881 topmem[n].rss_sh = p->shared_clean + p->shared_dirty;
882 topmem[n].rss = p->private_clean + p->private_dirty + topmem[n].rss_sh;
883 topmem[n].dirty = p->private_dirty + p->shared_dirty;
884 topmem[n].dirty_sh = p->shared_dirty;
885 topmem[n].stack = p->stack;
890 bb_error_msg_and_die("no process info in /proc");
893 if (scan_mask == TOP_MASK) {
894 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
895 if (!prev_hist_count) {
902 /* TODO: we don't need to sort all 10000 processes, we need to find top 24! */
903 qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp);
905 qsort(top, ntop, sizeof(top_status_t), (void*)(sort_function[0]));
906 #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
907 } else { /* TOPMEM */
908 qsort(topmem, ntop, sizeof(topmem_status_t), (void*)topmem_sort);
911 if (OPT_BATCH_MODE || count > ntop) {
914 if (scan_mask == TOP_MASK)
915 display_process_list(count, col);
917 display_topmem_process_list(count, col);
919 if (iterations >= 0 && !--iterations)
921 #if !ENABLE_FEATURE_USE_TERMIOS
924 if (poll(pfd, 1, interval * 1000) != 0) {
925 if (read(0, &c, 1) != 1) /* signal */
927 if (c == initial_settings.c_cc[VINTR])
929 c |= 0x20; /* lowercase */
933 USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
934 sort_function[0] = pid_sort;
937 USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
938 sort_function[0] = mem_sort;
939 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
940 sort_function[1] = pcpu_sort;
941 sort_function[2] = time_sort;
944 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
946 USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
947 sort_function[0] = pcpu_sort;
948 sort_function[1] = mem_sort;
949 sort_function[2] = time_sort;
952 USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
953 sort_function[0] = time_sort;
954 sort_function[1] = mem_sort;
955 sort_function[2] = pcpu_sort;
957 #if ENABLE_FEATURE_TOPMEM
959 scan_mask = TOPMEM_MASK;
963 sort_field = (sort_field + 1) % NUM_SORT_FIELD;
970 #endif /* FEATURE_USE_TERMIOS */