diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-09-08 17:34:05 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-09-08 17:34:05 +0000 |
commit | 4c1d88daff14331b64ef1019faf3f6f1ecaff817 (patch) | |
tree | 9676ad98bca2d41581406e7ccebbbd069f4d74cc | |
parent | e7c1ad1540ad18b17bd9ec93823dbf38583b5b87 (diff) | |
download | busybox-w32-4c1d88daff14331b64ef1019faf3f6f1ecaff817.tar.gz busybox-w32-4c1d88daff14331b64ef1019faf3f6f1ecaff817.tar.bz2 busybox-w32-4c1d88daff14331b64ef1019faf3f6f1ecaff817.zip |
top: get rid of on-stack variable buffers, use permanent one.
code shrank with and without TOPMEM:
top_main 828 844 +16
display_process_list 1525 1473 -52
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 16/-52) Total: -36 bytes
top_main 1150 1171 +21
display_topmem_process_list 1150 1167 +17
display_process_list 1525 1473 -52
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/1 up/down: 38/-52) Total: -14 bytes
-rw-r--r-- | include/platform.h | 1 | ||||
-rw-r--r-- | procps/top.c | 45 |
2 files changed, 25 insertions, 21 deletions
diff --git a/include/platform.h b/include/platform.h index ff23ca18c..39809450e 100644 --- a/include/platform.h +++ b/include/platform.h | |||
@@ -54,6 +54,7 @@ | |||
54 | # define ATTRIBUTE_ALIGNED(m) __attribute__ ((__aligned__(m))) | 54 | # define ATTRIBUTE_ALIGNED(m) __attribute__ ((__aligned__(m))) |
55 | # if __GNUC_PREREQ (3,0) | 55 | # if __GNUC_PREREQ (3,0) |
56 | # define ALWAYS_INLINE __attribute__ ((always_inline)) inline | 56 | # define ALWAYS_INLINE __attribute__ ((always_inline)) inline |
57 | # define NOINLINE __attribute__((noinline)) | ||
57 | # if !ENABLE_WERROR | 58 | # if !ENABLE_WERROR |
58 | # define ATTRIBUTE_DEPRECATED __attribute__ ((__deprecated__)) | 59 | # define ATTRIBUTE_DEPRECATED __attribute__ ((__deprecated__)) |
59 | # define ATTRIBUTE_UNUSED_RESULT __attribute__ ((warn_unused_result)) | 60 | # define ATTRIBUTE_UNUSED_RESULT __attribute__ ((warn_unused_result)) |
diff --git a/procps/top.c b/procps/top.c index a9580962a..529db5f7b 100644 --- a/procps/top.c +++ b/procps/top.c | |||
@@ -82,8 +82,10 @@ struct globals { | |||
82 | /* int hist_iterations; */ | 82 | /* int hist_iterations; */ |
83 | unsigned total_pcpu; | 83 | unsigned total_pcpu; |
84 | /* unsigned long total_vsz; */ | 84 | /* unsigned long total_vsz; */ |
85 | char line_buf[80]; | ||
85 | #endif | 86 | #endif |
86 | }; | 87 | }; |
88 | enum { LINE_BUF_SIZE = COMMON_BUFSIZE - offsetof(struct globals, line_buf) }; | ||
87 | #define G (*(struct globals*)&bb_common_bufsiz1) | 89 | #define G (*(struct globals*)&bb_common_bufsiz1) |
88 | #define INIT_G() \ | 90 | #define INIT_G() \ |
89 | do { \ | 91 | do { \ |
@@ -102,6 +104,7 @@ struct globals { | |||
102 | #define jif (G.jif ) | 104 | #define jif (G.jif ) |
103 | #define prev_jif (G.prev_jif ) | 105 | #define prev_jif (G.prev_jif ) |
104 | #define total_pcpu (G.total_pcpu ) | 106 | #define total_pcpu (G.total_pcpu ) |
107 | #define line_buf (G.line_buf ) | ||
105 | 108 | ||
106 | 109 | ||
107 | #define OPT_BATCH_MODE (option_mask32 & 0x4) | 110 | #define OPT_BATCH_MODE (option_mask32 & 0x4) |
@@ -366,7 +369,7 @@ static unsigned long display_header(int scr_width) | |||
366 | return total; | 369 | return total; |
367 | } | 370 | } |
368 | 371 | ||
369 | static void display_process_list(int count, int scr_width) | 372 | static NOINLINE void display_process_list(int count, int scr_width) |
370 | { | 373 | { |
371 | enum { | 374 | enum { |
372 | BITS_PER_INT = sizeof(int)*8 | 375 | BITS_PER_INT = sizeof(int)*8 |
@@ -447,7 +450,6 @@ static void display_process_list(int count, int scr_width) | |||
447 | scr_width += 2; /* account for leading '\n' and trailing NUL */ | 450 | scr_width += 2; /* account for leading '\n' and trailing NUL */ |
448 | /* Ok, all preliminary data is ready, go thru the list */ | 451 | /* Ok, all preliminary data is ready, go thru the list */ |
449 | while (count-- > 0) { | 452 | while (count-- > 0) { |
450 | char buf[scr_width]; | ||
451 | unsigned col; | 453 | unsigned col; |
452 | CALC_STAT(pmem, (s->vsz*pmem_scale + pmem_half) >> pmem_shift); | 454 | CALC_STAT(pmem, (s->vsz*pmem_scale + pmem_half) >> pmem_shift); |
453 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE | 455 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
@@ -459,7 +461,7 @@ static void display_process_list(int count, int scr_width) | |||
459 | else | 461 | else |
460 | sprintf(vsz_str_buf, "%7ld", s->vsz); | 462 | sprintf(vsz_str_buf, "%7ld", s->vsz); |
461 | // PID PPID USER STAT VSZ %MEM [%CPU] COMMAND | 463 | // PID PPID USER STAT VSZ %MEM [%CPU] COMMAND |
462 | col = snprintf(buf, scr_width, | 464 | col = snprintf(line_buf, scr_width, |
463 | "\n" "%5u%6u %-8.8s %s%s" FMT | 465 | "\n" "%5u%6u %-8.8s %s%s" FMT |
464 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE | 466 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
465 | FMT | 467 | FMT |
@@ -473,8 +475,8 @@ static void display_process_list(int count, int scr_width) | |||
473 | #endif | 475 | #endif |
474 | ); | 476 | ); |
475 | if (col < scr_width) | 477 | if (col < scr_width) |
476 | read_cmdline(buf + col, scr_width - col, s->pid, s->comm); | 478 | read_cmdline(line_buf + col, scr_width - col, s->pid, s->comm); |
477 | fputs(buf, stdout); | 479 | fputs(line_buf, stdout); |
478 | /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu, | 480 | /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu, |
479 | jif.busy - prev_jif.busy, jif.total - prev_jif.total); */ | 481 | jif.busy - prev_jif.busy, jif.total - prev_jif.total); */ |
480 | s++; | 482 | s++; |
@@ -722,33 +724,32 @@ static void smart_ulltoa6(unsigned long long ul, char buf[6]) | |||
722 | buf[5] = ' '; | 724 | buf[5] = ' '; |
723 | } | 725 | } |
724 | 726 | ||
725 | static void display_topmem_process_list(int count, int scr_width) | 727 | static NOINLINE void display_topmem_process_list(int count, int scr_width) |
726 | { | 728 | { |
727 | #define HDR_STR " PID VSZ VSZRW RSS (SHR) DIRTY (SHR) STACK" | 729 | #define HDR_STR " PID VSZ VSZRW RSS (SHR) DIRTY (SHR) STACK" |
728 | #define MIN_WIDTH sizeof(HDR_STR) | 730 | #define MIN_WIDTH sizeof(HDR_STR) |
729 | const topmem_status_t *s = topmem; | 731 | const topmem_status_t *s = topmem; |
730 | char buf[scr_width | MIN_WIDTH]; /* a|b is a cheap max(a,b) */ | ||
731 | 732 | ||
732 | display_topmem_header(scr_width); | 733 | display_topmem_header(scr_width); |
733 | strcpy(buf, HDR_STR " COMMAND"); | 734 | strcpy(line_buf, HDR_STR " COMMAND"); |
734 | buf[5 + sort_field * 6] = '*'; | 735 | line_buf[5 + sort_field * 6] = '*'; |
735 | printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width, buf); | 736 | printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width, line_buf); |
736 | 737 | ||
737 | while (--count >= 0) { | 738 | while (--count >= 0) { |
738 | // PID VSZ VSZRW RSS (SHR) DIRTY (SHR) COMMAND | 739 | // PID VSZ VSZRW RSS (SHR) DIRTY (SHR) COMMAND |
739 | smart_ulltoa6(s->pid , &buf[0*6]); | 740 | smart_ulltoa6(s->pid , &line_buf[0*6]); |
740 | smart_ulltoa6(s->vsz , &buf[1*6]); | 741 | smart_ulltoa6(s->vsz , &line_buf[1*6]); |
741 | smart_ulltoa6(s->vszrw , &buf[2*6]); | 742 | smart_ulltoa6(s->vszrw , &line_buf[2*6]); |
742 | smart_ulltoa6(s->rss , &buf[3*6]); | 743 | smart_ulltoa6(s->rss , &line_buf[3*6]); |
743 | smart_ulltoa6(s->rss_sh , &buf[4*6]); | 744 | smart_ulltoa6(s->rss_sh , &line_buf[4*6]); |
744 | smart_ulltoa6(s->dirty , &buf[5*6]); | 745 | smart_ulltoa6(s->dirty , &line_buf[5*6]); |
745 | smart_ulltoa6(s->dirty_sh, &buf[6*6]); | 746 | smart_ulltoa6(s->dirty_sh, &line_buf[6*6]); |
746 | smart_ulltoa6(s->stack , &buf[7*6]); | 747 | smart_ulltoa6(s->stack , &line_buf[7*6]); |
747 | buf[8*6] = '\0'; | 748 | line_buf[8*6] = '\0'; |
748 | if (scr_width > MIN_WIDTH) { | 749 | if (scr_width > MIN_WIDTH) { |
749 | read_cmdline(&buf[8*6], scr_width - MIN_WIDTH, s->pid, s->comm); | 750 | read_cmdline(&line_buf[8*6], scr_width - MIN_WIDTH, s->pid, s->comm); |
750 | } | 751 | } |
751 | printf("\n""%.*s", scr_width, buf); | 752 | printf("\n""%.*s", scr_width, line_buf); |
752 | s++; | 753 | s++; |
753 | } | 754 | } |
754 | putchar(OPT_BATCH_MODE ? '\n' : '\r'); | 755 | putchar(OPT_BATCH_MODE ? '\n' : '\r'); |
@@ -848,6 +849,8 @@ int top_main(int argc, char **argv) | |||
848 | continue; | 849 | continue; |
849 | } | 850 | } |
850 | #endif /* FEATURE_USE_TERMIOS */ | 851 | #endif /* FEATURE_USE_TERMIOS */ |
852 | if (col > LINE_BUF_SIZE-2) /* +2 bytes for '\n', NUL, */ | ||
853 | col = LINE_BUF_SIZE-2; | ||
851 | if (!ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS && scan_mask == TOP_MASK) | 854 | if (!ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS && scan_mask == TOP_MASK) |
852 | lines -= 3; | 855 | lines -= 3; |
853 | else | 856 | else |