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)
94 struct G_sizecheck { \
95 char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
99 #define ntop (G.ntop )
100 #define sort_field (G.sort_field )
101 #define inverted (G.inverted )
102 #define initial_settings (G.initial_settings )
103 #define sort_function (G.sort_function )
104 #define prev_hist (G.prev_hist )
105 #define prev_hist_count (G.prev_hist_count )
107 #define prev_jif (G.prev_jif )
108 #define total_pcpu (G.total_pcpu )
109 #define line_buf (G.line_buf )
112 #define OPT_BATCH_MODE (option_mask32 & 0x4)
115 #if ENABLE_FEATURE_USE_TERMIOS
116 static int pid_sort(top_status_t
*P
, top_status_t
*Q
)
118 /* Buggy wrt pids with high bit set */
119 /* (linux pids are in [1..2^15-1]) */
120 return (Q
->pid
- P
->pid
);
124 static int mem_sort(top_status_t
*P
, top_status_t
*Q
)
126 /* We want to avoid unsigned->signed and truncation errors */
127 if (Q
->vsz
< P
->vsz
) return -1;
128 return Q
->vsz
!= P
->vsz
; /* 0 if ==, 1 if > */
132 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
134 static int pcpu_sort(top_status_t
*P
, top_status_t
*Q
)
136 /* Buggy wrt ticks with high bit set */
137 /* Affects only processes for which ticks overflow */
138 return (int)Q
->pcpu
- (int)P
->pcpu
;
141 static int time_sort(top_status_t
*P
, top_status_t
*Q
)
143 /* We want to avoid unsigned->signed and truncation errors */
144 if (Q
->ticks
< P
->ticks
) return -1;
145 return Q
->ticks
!= P
->ticks
; /* 0 if ==, 1 if > */
148 static int mult_lvl_cmp(void* a
, void* b
)
152 for (i
= 0; i
< SORT_DEPTH
; i
++) {
153 cmp_val
= (*sort_function
[i
])(a
, b
);
161 static void get_jiffy_counts(void)
163 FILE* fp
= xfopen("stat", "r");
165 if (fscanf(fp
, "cpu %lld %lld %lld %lld %lld %lld %lld %lld",
166 &jif
.usr
,&jif
.nic
,&jif
.sys
,&jif
.idle
,
167 &jif
.iowait
,&jif
.irq
,&jif
.softirq
,&jif
.steal
) < 4) {
168 bb_error_msg_and_die("failed to read /proc/stat");
171 jif
.total
= jif
.usr
+ jif
.nic
+ jif
.sys
+ jif
.idle
172 + jif
.iowait
+ jif
.irq
+ jif
.softirq
+ jif
.steal
;
173 /* procps 2.x does not count iowait as busy time */
174 jif
.busy
= jif
.total
- jif
.idle
- jif
.iowait
;
178 static void do_stats(void)
183 struct save_hist
*new_hist
;
188 new_hist
= xmalloc(sizeof(struct save_hist
)*ntop
);
190 * Make a pass through the data to get stats.
192 /* hist_iterations = 0; */
194 for (n
= 0; n
< ntop
; n
++) {
198 * Calculate time in cur process. Time is sum of user time
202 new_hist
[n
].ticks
= cur
->ticks
;
203 new_hist
[n
].pid
= pid
;
205 /* find matching entry from previous pass */
207 /* do not start at index 0, continue at last used one
208 * (brought hist_iterations from ~14000 down to 172) */
210 if (prev_hist_count
) do {
211 if (prev_hist
[i
].pid
== pid
) {
212 cur
->pcpu
= cur
->ticks
- prev_hist
[i
].ticks
;
213 total_pcpu
+= cur
->pcpu
;
216 i
= (i
+1) % prev_hist_count
;
217 /* hist_iterations++; */
218 } while (i
!= last_i
);
219 /* total_vsz += cur->vsz; */
223 * Save cur frame's information.
226 prev_hist
= new_hist
;
227 prev_hist_count
= ntop
;
229 #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
231 #if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS && ENABLE_FEATURE_TOP_DECIMALS
232 /* formats 7 char string (8 with terminating NUL) */
233 static char *fmt_100percent_8(char pbuf
[8], unsigned value
, unsigned total
)
236 if (value
>= total
) { /* 100% ? */
237 strcpy(pbuf
, " 100% ");
240 /* else generate " [N/space]N.N% " string */
241 value
= 1000 * value
/ total
;
245 pbuf
[1] = t
? t
+ '0' : ' ';
246 pbuf
[2] = '0' + (value
/ 10);
248 pbuf
[4] = '0' + (value
% 10);
256 static unsigned long display_header(int scr_width
)
261 unsigned long total
, used
, mfree
, shared
, buffers
, cached
;
262 #if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS
266 /* read memory info */
267 fp
= xfopen("meminfo", "r");
270 * Old kernels (such as 2.4.x) had a nice summary of memory info that
271 * we could parse, however this is gone entirely in 2.6. Try parsing
272 * the old way first, and if that fails, parse each field manually.
274 * First, we read in the first line. Old kernels will have bogus
275 * strings we don't care about, whereas new kernels will start right
279 if (fscanf(fp
, "MemTotal: %lu %s\n", &total
, buf
) != 2) {
280 fgets(buf
, sizeof(buf
), fp
); /* skip first line */
282 fscanf(fp
, "Mem: %lu %lu %lu %lu %lu %lu",
283 &total
, &used
, &mfree
, &shared
, &buffers
, &cached
);
284 /* convert to kilobytes */
293 * Revert to manual parsing, which incidentally already has the
294 * sizes in kilobytes. This should be safe for both 2.4 and
298 fscanf(fp
, "MemFree: %lu %s\n", &mfree
, buf
);
301 * MemShared: is no longer present in 2.6. Report this as 0,
302 * to maintain consistent behavior with normal procps.
304 if (fscanf(fp
, "MemShared: %lu %s\n", &shared
, buf
) != 2)
307 fscanf(fp
, "Buffers: %lu %s\n", &buffers
, buf
);
308 fscanf(fp
, "Cached: %lu %s\n", &cached
, buf
);
310 used
= total
- mfree
;
314 /* output memory info */
315 if (scr_width
> sizeof(scrbuf
))
316 scr_width
= sizeof(scrbuf
);
317 snprintf(scrbuf
, scr_width
,
318 "Mem: %luK used, %luK free, %luK shrd, %luK buff, %luK cached",
319 used
, mfree
, shared
, buffers
, cached
);
320 /* clear screen & go to top */
321 printf(OPT_BATCH_MODE
? "%s\n" : "\e[H\e[J%s\n", scrbuf
);
323 #if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS
325 * xxx% = (jif.xxx - prev_jif.xxx) / (jif.total - prev_jif.total) * 100%
327 /* using (unsigned) casts to make operations cheaper */
328 total_diff
= ((unsigned)(jif
.total
- prev_jif
.total
) ? : 1);
329 #if ENABLE_FEATURE_TOP_DECIMALS
330 /* Generated code is approx +0.3k */
331 #define CALC_STAT(xxx) char xxx[8]
332 #define SHOW_STAT(xxx) fmt_100percent_8(xxx, (unsigned)(jif.xxx - prev_jif.xxx), total_diff)
335 #define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff
336 #define SHOW_STAT(xxx) xxx
339 { /* need block: CALC_STAT are declarations */
349 snprintf(scrbuf
, scr_width
,
350 /* Barely fits in 79 chars when in "decimals" mode. */
351 "CPU:"FMT
"usr"FMT
"sys"FMT
"nice"FMT
"idle"FMT
"io"FMT
"irq"FMT
"softirq",
352 SHOW_STAT(usr
), SHOW_STAT(sys
), SHOW_STAT(nic
), SHOW_STAT(idle
),
353 SHOW_STAT(iowait
), SHOW_STAT(irq
), SHOW_STAT(softirq
)
354 //, SHOW_STAT(steal) - what is this 'steal' thing?
355 // I doubt anyone wants to know it
364 /* read load average as a string */
366 open_read_close("loadavg", buf
, sizeof("N.NN N.NN N.NN")-1);
367 buf
[sizeof("N.NN N.NN N.NN")-1] = '\0';
368 snprintf(scrbuf
, scr_width
, "Load average: %s", buf
);
374 static NOINLINE
void display_process_list(int count
, int scr_width
)
377 BITS_PER_INT
= sizeof(int)*8
380 top_status_t
*s
= top
;
382 unsigned long total_memory
= display_header(scr_width
); /* or use total_vsz? */
383 /* xxx_shift and xxx_scale variables allow us to replace
384 * expensive divides with multiply and shift */
385 unsigned pmem_shift
, pmem_scale
, pmem_half
;
386 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
387 unsigned pcpu_shift
, pcpu_scale
, pcpu_half
;
390 /* what info of the processes is shown */
391 printf(OPT_BATCH_MODE
? "%.*s" : "\e[7m%.*s\e[0m", scr_width
,
392 " PID PPID USER STAT VSZ %MEM %CPU COMMAND");
395 /* !CPU_USAGE_PERCENTAGE */
396 printf(OPT_BATCH_MODE
? "%.*s" : "\e[7m%.*s\e[0m", scr_width
,
397 " PID PPID USER STAT VSZ %MEM COMMAND");
400 #if ENABLE_FEATURE_TOP_DECIMALS
402 #define CALC_STAT(name, val) div_t name = div((val), 10)
403 #define SHOW_STAT(name) name.quot, '0'+name.rem
407 #define CALC_STAT(name, val) unsigned name = (val)
408 #define SHOW_STAT(name) name
412 * MEM% = s->vsz/MemTotal
414 pmem_shift
= BITS_PER_INT
-11;
415 pmem_scale
= UPSCALE
*(1U<<(BITS_PER_INT
-11)) / total_memory
;
416 /* s->vsz is in kb. we want (s->vsz * pmem_scale) to never overflow */
417 while (pmem_scale
>= 512) {
421 pmem_half
= (1U << pmem_shift
) / (ENABLE_FEATURE_TOP_DECIMALS
? 20 : 2);
422 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
423 busy_jifs
= jif
.busy
- prev_jif
.busy
;
424 /* This happens if there were lots of short-lived processes
425 * between two top updates (e.g. compilation) */
426 if (total_pcpu
< busy_jifs
) total_pcpu
= busy_jifs
;
429 * CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks
430 * (pcpu is delta of sys+user time between samples)
432 /* (jif.xxx - prev_jif.xxx) and s->pcpu are
433 * in 0..~64000 range (HZ*update_interval).
434 * we assume that unsigned is at least 32-bit.
437 pcpu_scale
= (UPSCALE
*64*(uint16_t)busy_jifs
? : 1);
438 while (pcpu_scale
< (1U<<(BITS_PER_INT
-2))) {
442 pcpu_scale
/= ( (uint16_t)(jif
.total
-prev_jif
.total
)*total_pcpu
? : 1);
443 /* we want (s->pcpu * pcpu_scale) to never overflow */
444 while (pcpu_scale
>= 1024) {
448 pcpu_half
= (1U << pcpu_shift
) / (ENABLE_FEATURE_TOP_DECIMALS
? 20 : 2);
449 /* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */
452 scr_width
+= 2; /* account for leading '\n' and trailing NUL */
453 /* Ok, all preliminary data is ready, go thru the list */
454 while (count
-- > 0) {
456 CALC_STAT(pmem
, (s
->vsz
*pmem_scale
+ pmem_half
) >> pmem_shift
);
457 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
458 CALC_STAT(pcpu
, (s
->pcpu
*pcpu_scale
+ pcpu_half
) >> pcpu_shift
);
461 if (s
->vsz
>= 100000)
462 sprintf(vsz_str_buf
, "%6ldm", s
->vsz
/1024);
464 sprintf(vsz_str_buf
, "%7ld", s
->vsz
);
465 // PID PPID USER STAT VSZ %MEM [%CPU] COMMAND
466 col
= snprintf(line_buf
, scr_width
,
467 "\n" "%5u%6u %-8.8s %s%s" FMT
468 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
472 s
->pid
, s
->ppid
, get_cached_username(s
->uid
),
473 s
->state
, vsz_str_buf
,
475 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
479 if (col
+ 1 < scr_width
)
480 read_cmdline(line_buf
+ col
, scr_width
- col
- 1, s
->pid
, s
->comm
);
481 fputs(line_buf
, stdout
);
482 /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu,
483 jif.busy - prev_jif.busy, jif.total - prev_jif.total); */
486 /* printf(" %d", hist_iterations); */
487 bb_putchar(OPT_BATCH_MODE
? '\n' : '\r');
495 static void clearmems(void)
497 clear_username_cache();
503 #if ENABLE_FEATURE_USE_TERMIOS
507 static void reset_term(void)
509 tcsetattr(0, TCSANOW
, (void *) &initial_settings
);
510 if (ENABLE_FEATURE_CLEAN_UP
) {
512 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
518 static void sig_catcher(int sig ATTRIBUTE_UNUSED
)
523 #endif /* FEATURE_USE_TERMIOS */
529 typedef unsigned long mem_t
;
531 typedef struct topmem_status_t
{
534 /* vsz doesn't count /dev/xxx mappings except /dev/zero */
544 enum { NUM_SORT_FIELD
= 7 };
546 #define topmem ((topmem_status_t*)top)
548 #if ENABLE_FEATURE_TOPMEM
549 static int topmem_sort(char *a
, char *b
)
554 n
= offsetof(topmem_status_t
, vsz
) + (sort_field
* sizeof(mem_t
));
555 l
= *(mem_t
*)(a
+ n
);
556 r
= *(mem_t
*)(b
+ n
);
561 /* We want to avoid unsigned->signed and truncation errors */
562 /* l>r: -1, l=r: 0, l<r: 1 */
563 n
= (l
> r
) ? -1 : (l
!= r
);
564 return inverted
? -n
: n
;
567 /* Cut "NNNN " out of " NNNN kb" */
568 static char *grab_number(char *str
, const char *match
, unsigned sz
)
570 if (strncmp(str
, match
, sz
) == 0) {
571 str
= skip_whitespace(str
+ sz
);
572 (skip_non_whitespace(str
))[1] = '\0';
578 /* display header info (meminfo / loadavg) */
579 static void display_topmem_header(int scr_width
)
590 /* 5 */ char *swaptotal
;
591 /* 6 */ char *swapfree
;
593 /* 8 */ char *mwrite
;
600 #define total Z.total
601 #define mfree Z.mfree
603 #define cache Z.cache
604 #define swaptotal Z.swaptotal
605 #define swapfree Z.swapfree
606 #define dirty Z.dirty
607 #define mwrite Z.mwrite
613 memset(&Z
, 0, sizeof(Z
));
615 /* read memory info */
616 fp
= xfopen("meminfo", "r");
617 while (fgets(linebuf
, sizeof(linebuf
), fp
)) {
620 #define SCAN(match, name) \
621 p = grab_number(linebuf, match, sizeof(match)-1); \
622 if (p) { name = p; continue; }
624 SCAN("MemTotal:", total
);
625 SCAN("MemFree:", mfree
);
626 SCAN("Buffers:", buf
);
627 SCAN("Cached:", cache
);
628 SCAN("SwapTotal:", swaptotal
);
629 SCAN("SwapFree:", swapfree
);
630 SCAN("Dirty:", dirty
);
631 SCAN("Writeback:", mwrite
);
632 SCAN("AnonPages:", anon
);
633 SCAN("Mapped:", map
);
639 #define S(s) (s ? s : "0")
640 snprintf(linebuf
, sizeof(linebuf
),
641 "Mem %stotal %sanon %smap %sfree",
642 S(total
), S(anon
), S(map
), S(mfree
));
643 printf(OPT_BATCH_MODE
? "%.*s\n" : "\e[H\e[J%.*s\n", scr_width
, linebuf
);
645 snprintf(linebuf
, sizeof(linebuf
),
646 " %sslab %sbuf %scache %sdirty %swrite",
647 S(slab
), S(buf
), S(cache
), S(dirty
), S(mwrite
));
648 printf("%.*s\n", scr_width
, linebuf
);
650 snprintf(linebuf
, sizeof(linebuf
),
651 "Swap %stotal %sfree", // TODO: % used?
652 S(swaptotal
), S(swapfree
));
653 printf("%.*s\n", scr_width
, linebuf
);
656 for (i
= 0; i
< ARRAY_SIZE(str
); i
++)
672 // Converts unsigned long long value into compact 5-char
673 // representation. Sixth char is always ' '
674 static void smart_ulltoa6(unsigned long long ul
, char buf
[6])
678 unsigned v
, u
, idx
= 0;
680 if (ul
> 99999) { // do not scale if 99999 or less
685 } while (ul
>= 100000);
687 v
= ul
; // ullong divisions are expensive, avoid them
693 // 99999 or less: use "12345" format
694 // u is value/10, v is last digit
695 c
= buf
[0] = " 123456789"[u
/1000];
696 if (c
!= ' ') fmt
= "0123456789";
697 c
= buf
[1] = fmt
[u
/100%10];
698 if (c
!= ' ') fmt
= "0123456789";
699 c
= buf
[2] = fmt
[u
/10%10];
700 if (c
!= ' ') fmt
= "0123456789";
702 buf
[4] = "0123456789"[v
];
704 // value has been scaled into 0..9999.9 range
705 // u is value, v is 1/10ths (allows for 92.1M format)
707 // value is >= 100: use "1234M', " 123M" formats
708 c
= buf
[0] = " 123456789"[u
/1000];
709 if (c
!= ' ') fmt
= "0123456789";
710 c
= buf
[1] = fmt
[u
/100%10];
711 if (c
!= ' ') fmt
= "0123456789";
716 // value is < 100: use "92.1M" format
717 c
= buf
[0] = " 123456789"[u
/10];
718 if (c
!= ' ') fmt
= "0123456789";
722 buf
[3] = "0123456789"[v
];
723 // see http://en.wikipedia.org/wiki/Tera
724 buf
[4] = " mgtpezy"[idx
];
729 static NOINLINE
void display_topmem_process_list(int count
, int scr_width
)
731 #define HDR_STR " PID VSZ VSZRW RSS (SHR) DIRTY (SHR) STACK"
732 #define MIN_WIDTH sizeof(HDR_STR)
733 const topmem_status_t
*s
= topmem
;
735 display_topmem_header(scr_width
);
736 strcpy(line_buf
, HDR_STR
" COMMAND");
737 line_buf
[5 + sort_field
* 6] = '*';
738 printf(OPT_BATCH_MODE
? "%.*s" : "\e[7m%.*s\e[0m", scr_width
, line_buf
);
740 while (--count
>= 0) {
741 // PID VSZ VSZRW RSS (SHR) DIRTY (SHR) COMMAND
742 smart_ulltoa6(s
->pid
, &line_buf
[0*6]);
743 smart_ulltoa6(s
->vsz
, &line_buf
[1*6]);
744 smart_ulltoa6(s
->vszrw
, &line_buf
[2*6]);
745 smart_ulltoa6(s
->rss
, &line_buf
[3*6]);
746 smart_ulltoa6(s
->rss_sh
, &line_buf
[4*6]);
747 smart_ulltoa6(s
->dirty
, &line_buf
[5*6]);
748 smart_ulltoa6(s
->dirty_sh
, &line_buf
[6*6]);
749 smart_ulltoa6(s
->stack
, &line_buf
[7*6]);
750 line_buf
[8*6] = '\0';
751 if (scr_width
> MIN_WIDTH
) {
752 read_cmdline(&line_buf
[8*6], scr_width
- MIN_WIDTH
, s
->pid
, s
->comm
);
754 printf("\n""%.*s", scr_width
, line_buf
);
757 bb_putchar(OPT_BATCH_MODE
? '\n' : '\r');
763 void display_topmem_process_list(int count
, int scr_width
);
764 int topmem_sort(char *a
, char *b
);
787 int top_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
788 int top_main(int argc
, char **argv
)
790 int count
, lines
, col
;
793 char *sinterval
, *siterations
;
794 SKIP_FEATURE_TOPMEM(const) unsigned scan_mask
= TOP_MASK
;
795 #if ENABLE_FEATURE_USE_TERMIOS
796 struct termios new_settings
;
797 struct pollfd pfd
[1];
801 pfd
[0].events
= POLLIN
;
802 #endif /* FEATURE_USE_TERMIOS */
806 interval
= 5; /* default update rate is 5 seconds */
807 iterations
= 0; /* infinite */
809 /* do normal option parsing */
810 opt_complementary
= "-";
811 getopt32(argv
, "d:n:b", &sinterval
, &siterations
);
812 if (option_mask32
& 0x1) {
813 /* Need to limit it to not overflow poll timeout */
814 interval
= xatou16(sinterval
); // -d
816 if (option_mask32
& 0x2)
817 iterations
= xatoi_u(siterations
); // -n
818 //if (option_mask32 & 0x4) // -b
820 /* change to /proc */
822 #if ENABLE_FEATURE_USE_TERMIOS
823 tcgetattr(0, (void *) &initial_settings
);
824 memcpy(&new_settings
, &initial_settings
, sizeof(new_settings
));
825 /* unbuffered input, turn off echo */
826 new_settings
.c_lflag
&= ~(ISIG
| ICANON
| ECHO
| ECHONL
);
828 signal(SIGTERM
, sig_catcher
);
829 signal(SIGINT
, sig_catcher
);
830 tcsetattr(0, TCSANOW
, (void *) &new_settings
);
832 #endif /* FEATURE_USE_TERMIOS */
834 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
835 sort_function
[0] = pcpu_sort
;
836 sort_function
[1] = mem_sort
;
837 sort_function
[2] = time_sort
;
839 sort_function
[0] = mem_sort
;
840 #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
843 procps_status_t
*p
= NULL
;
845 lines
= 24; /* default */
847 #if ENABLE_FEATURE_USE_TERMIOS
848 get_terminal_width_height(0, &col
, &lines
);
849 if (lines
< 5 || col
< 10) {
853 #endif /* FEATURE_USE_TERMIOS */
854 if (col
> LINE_BUF_SIZE
-2) /* +2 bytes for '\n', NUL, */
855 col
= LINE_BUF_SIZE
-2;
856 if (!ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS
&& scan_mask
== TOP_MASK
)
861 /* read process IDs & status for all the processes */
862 while ((p
= procps_scan(p
, scan_mask
)) != NULL
) {
864 if (scan_mask
== TOP_MASK
) {
866 top
= xrealloc(top
, (++ntop
) * sizeof(*top
));
868 top
[n
].ppid
= p
->ppid
;
870 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
871 top
[n
].ticks
= p
->stime
+ p
->utime
;
874 strcpy(top
[n
].state
, p
->state
);
875 strcpy(top
[n
].comm
, p
->comm
);
876 } else { /* TOPMEM */
877 #if ENABLE_FEATURE_TOPMEM
878 if (!(p
->mapped_ro
| p
->mapped_rw
))
879 continue; /* kernel threads are ignored */
881 top
= xrealloc(topmem
, (++ntop
) * sizeof(*topmem
));
882 strcpy(topmem
[n
].comm
, p
->comm
);
883 topmem
[n
].pid
= p
->pid
;
884 topmem
[n
].vsz
= p
->mapped_rw
+ p
->mapped_ro
;
885 topmem
[n
].vszrw
= p
->mapped_rw
;
886 topmem
[n
].rss_sh
= p
->shared_clean
+ p
->shared_dirty
;
887 topmem
[n
].rss
= p
->private_clean
+ p
->private_dirty
+ topmem
[n
].rss_sh
;
888 topmem
[n
].dirty
= p
->private_dirty
+ p
->shared_dirty
;
889 topmem
[n
].dirty_sh
= p
->shared_dirty
;
890 topmem
[n
].stack
= p
->stack
;
895 bb_error_msg_and_die("no process info in /proc");
898 if (scan_mask
== TOP_MASK
) {
899 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
900 if (!prev_hist_count
) {
907 /* TODO: we don't need to sort all 10000 processes, we need to find top 24! */
908 qsort(top
, ntop
, sizeof(top_status_t
), (void*)mult_lvl_cmp
);
910 qsort(top
, ntop
, sizeof(top_status_t
), (void*)(sort_function
[0]));
911 #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
912 } else { /* TOPMEM */
913 qsort(topmem
, ntop
, sizeof(topmem_status_t
), (void*)topmem_sort
);
916 if (OPT_BATCH_MODE
|| count
> ntop
) {
919 if (scan_mask
== TOP_MASK
)
920 display_process_list(count
, col
);
922 display_topmem_process_list(count
, col
);
924 if (iterations
>= 0 && !--iterations
)
926 #if !ENABLE_FEATURE_USE_TERMIOS
929 if (safe_poll(pfd
, 1, interval
* 1000) > 0) {
930 if (read(0, &c
, 1) != 1) /* signal */
932 if (c
== initial_settings
.c_cc
[VINTR
])
934 c
|= 0x20; /* lowercase */
938 USE_FEATURE_TOPMEM(scan_mask
= TOP_MASK
;)
939 sort_function
[0] = pid_sort
;
942 USE_FEATURE_TOPMEM(scan_mask
= TOP_MASK
;)
943 sort_function
[0] = mem_sort
;
944 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
945 sort_function
[1] = pcpu_sort
;
946 sort_function
[2] = time_sort
;
949 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
951 USE_FEATURE_TOPMEM(scan_mask
= TOP_MASK
;)
952 sort_function
[0] = pcpu_sort
;
953 sort_function
[1] = mem_sort
;
954 sort_function
[2] = time_sort
;
957 USE_FEATURE_TOPMEM(scan_mask
= TOP_MASK
;)
958 sort_function
[0] = time_sort
;
959 sort_function
[1] = mem_sort
;
960 sort_function
[2] = pcpu_sort
;
962 #if ENABLE_FEATURE_TOPMEM
964 scan_mask
= TOPMEM_MASK
;
968 sort_field
= (sort_field
+ 1) % NUM_SORT_FIELD
;
975 #endif /* FEATURE_USE_TERMIOS */