diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2016-03-15 16:06:29 +0100 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2016-03-15 16:06:29 +0100 |
| commit | a63e2a8cb2dd0d6819c1844c52344c3bd6f73659 (patch) | |
| tree | 0031e26695794fe58a05f44aab80edfc680750f8 | |
| parent | 8a26fda98c46b1ffd98a1a9874a0899b061226d8 (diff) | |
| download | busybox-w32-a63e2a8cb2dd0d6819c1844c52344c3bd6f73659.tar.gz busybox-w32-a63e2a8cb2dd0d6819c1844c52344c3bd6f73659.tar.bz2 busybox-w32-a63e2a8cb2dd0d6819c1844c52344c3bd6f73659.zip | |
nmeter: simple code shrink here and there
function old new delta
nmeter_main 709 707 -2
init_cr 15 12 -3
collect_time 141 131 -10
collect_cpu 610 593 -17
init_cpu 82 63 -19
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/5 up/down: 0/-51) Total: -51 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
| -rw-r--r-- | procps/nmeter.c | 39 |
1 files changed, 12 insertions, 27 deletions
diff --git a/procps/nmeter.c b/procps/nmeter.c index d6222af6b..1cc908504 100644 --- a/procps/nmeter.c +++ b/procps/nmeter.c | |||
| @@ -83,8 +83,8 @@ struct globals { | |||
| 83 | smallint is26; | 83 | smallint is26; |
| 84 | // 1 if sample delay is not an integer fraction of a second | 84 | // 1 if sample delay is not an integer fraction of a second |
| 85 | smallint need_seconds; | 85 | smallint need_seconds; |
| 86 | char final_char; | ||
| 86 | char *cur_outbuf; | 87 | char *cur_outbuf; |
| 87 | const char *final_str; | ||
| 88 | int delta; | 88 | int delta; |
| 89 | int deltanz; | 89 | int deltanz; |
| 90 | struct timeval tv; | 90 | struct timeval tv; |
| @@ -101,7 +101,6 @@ struct globals { | |||
| 101 | #define is26 (G.is26 ) | 101 | #define is26 (G.is26 ) |
| 102 | #define need_seconds (G.need_seconds ) | 102 | #define need_seconds (G.need_seconds ) |
| 103 | #define cur_outbuf (G.cur_outbuf ) | 103 | #define cur_outbuf (G.cur_outbuf ) |
| 104 | #define final_str (G.final_str ) | ||
| 105 | #define delta (G.delta ) | 104 | #define delta (G.delta ) |
| 106 | #define deltanz (G.deltanz ) | 105 | #define deltanz (G.deltanz ) |
| 107 | #define tv (G.tv ) | 106 | #define tv (G.tv ) |
| @@ -114,7 +113,7 @@ struct globals { | |||
| 114 | #define INIT_G() do { \ | 113 | #define INIT_G() do { \ |
| 115 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ | 114 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 116 | cur_outbuf = outbuf; \ | 115 | cur_outbuf = outbuf; \ |
| 117 | final_str = "\n"; \ | 116 | G.final_char = '\n'; \ |
| 118 | deltanz = delta = 1000000; \ | 117 | deltanz = delta = 1000000; \ |
| 119 | } while (0) | 118 | } while (0) |
| 120 | 119 | ||
| @@ -322,7 +321,6 @@ static void scale(ullong ul) | |||
| 322 | put(buf); | 321 | put(buf); |
| 323 | } | 322 | } |
| 324 | 323 | ||
| 325 | |||
| 326 | #define S_STAT(a) \ | 324 | #define S_STAT(a) \ |
| 327 | typedef struct a { \ | 325 | typedef struct a { \ |
| 328 | struct s_stat *next; \ | 326 | struct s_stat *next; \ |
| @@ -354,11 +352,10 @@ static s_stat* init_delay(const char *param) | |||
| 354 | 352 | ||
| 355 | static s_stat* init_cr(const char *param UNUSED_PARAM) | 353 | static s_stat* init_cr(const char *param UNUSED_PARAM) |
| 356 | { | 354 | { |
| 357 | final_str = "\r"; | 355 | G.final_char = '\r'; |
| 358 | return (s_stat*)0; | 356 | return NULL; |
| 359 | } | 357 | } |
| 360 | 358 | ||
| 361 | |||
| 362 | // user nice system idle iowait irq softirq (last 3 only in 2.6) | 359 | // user nice system idle iowait irq softirq (last 3 only in 2.6) |
| 363 | //cpu 649369 0 341297 4336769 11640 7122 1183 | 360 | //cpu 649369 0 341297 4336769 11640 7122 1183 |
| 364 | //cpuN 649369 0 341297 4336769 11640 7122 1183 | 361 | //cpuN 649369 0 341297 4336769 11640 7122 1183 |
| @@ -366,10 +363,9 @@ enum { CPU_FIELDCNT = 7 }; | |||
| 366 | S_STAT(cpu_stat) | 363 | S_STAT(cpu_stat) |
| 367 | ullong old[CPU_FIELDCNT]; | 364 | ullong old[CPU_FIELDCNT]; |
| 368 | int bar_sz; | 365 | int bar_sz; |
| 369 | char *bar; | 366 | char bar[1]; |
| 370 | S_STAT_END(cpu_stat) | 367 | S_STAT_END(cpu_stat) |
| 371 | 368 | ||
| 372 | |||
| 373 | static void FAST_FUNC collect_cpu(cpu_stat *s) | 369 | static void FAST_FUNC collect_cpu(cpu_stat *s) |
| 374 | { | 370 | { |
| 375 | ullong data[CPU_FIELDCNT] = { 0, 0, 0, 0, 0, 0, 0 }; | 371 | ullong data[CPU_FIELDCNT] = { 0, 0, 0, 0, 0, 0, 0 }; |
| @@ -431,22 +427,20 @@ static void FAST_FUNC collect_cpu(cpu_stat *s) | |||
| 431 | put(s->bar); | 427 | put(s->bar); |
| 432 | } | 428 | } |
| 433 | 429 | ||
| 434 | |||
| 435 | static s_stat* init_cpu(const char *param) | 430 | static s_stat* init_cpu(const char *param) |
| 436 | { | 431 | { |
| 437 | int sz; | 432 | int sz; |
| 438 | cpu_stat *s = xzalloc(sizeof(*s)); | 433 | cpu_stat *s; |
| 439 | s->collect = collect_cpu; | ||
| 440 | sz = strtoul(param, NULL, 0); /* param can be "" */ | 434 | sz = strtoul(param, NULL, 0); /* param can be "" */ |
| 441 | if (sz < 10) sz = 10; | 435 | if (sz < 10) sz = 10; |
| 442 | if (sz > 1000) sz = 1000; | 436 | if (sz > 1000) sz = 1000; |
| 443 | s->bar = xzalloc(sz+1); | 437 | s = xzalloc(sizeof(*s) + sz); |
| 444 | /*s->bar[sz] = '\0'; - xzalloc did it */ | 438 | /*s->bar[sz] = '\0'; - xzalloc did it */ |
| 445 | s->bar_sz = sz; | 439 | s->bar_sz = sz; |
| 440 | s->collect = collect_cpu; | ||
| 446 | return (s_stat*)s; | 441 | return (s_stat*)s; |
| 447 | } | 442 | } |
| 448 | 443 | ||
| 449 | |||
| 450 | S_STAT(int_stat) | 444 | S_STAT(int_stat) |
| 451 | ullong old; | 445 | ullong old; |
| 452 | int no; | 446 | int no; |
| @@ -481,7 +475,6 @@ static s_stat* init_int(const char *param) | |||
| 481 | return (s_stat*)s; | 475 | return (s_stat*)s; |
| 482 | } | 476 | } |
| 483 | 477 | ||
| 484 | |||
| 485 | S_STAT(ctx_stat) | 478 | S_STAT(ctx_stat) |
| 486 | ullong old; | 479 | ullong old; |
| 487 | S_STAT_END(ctx_stat) | 480 | S_STAT_END(ctx_stat) |
| @@ -509,7 +502,6 @@ static s_stat* init_ctx(const char *param UNUSED_PARAM) | |||
| 509 | return (s_stat*)s; | 502 | return (s_stat*)s; |
| 510 | } | 503 | } |
| 511 | 504 | ||
| 512 | |||
| 513 | S_STAT(blk_stat) | 505 | S_STAT(blk_stat) |
| 514 | const char* lookfor; | 506 | const char* lookfor; |
| 515 | ullong old[2]; | 507 | ullong old[2]; |
| @@ -555,7 +547,6 @@ static s_stat* init_blk(const char *param UNUSED_PARAM) | |||
| 555 | return (s_stat*)s; | 547 | return (s_stat*)s; |
| 556 | } | 548 | } |
| 557 | 549 | ||
| 558 | |||
| 559 | S_STAT(fork_stat) | 550 | S_STAT(fork_stat) |
| 560 | ullong old; | 551 | ullong old; |
| 561 | S_STAT_END(fork_stat) | 552 | S_STAT_END(fork_stat) |
| @@ -598,7 +589,6 @@ static s_stat* init_fork(const char *param) | |||
| 598 | return (s_stat*)s; | 589 | return (s_stat*)s; |
| 599 | } | 590 | } |
| 600 | 591 | ||
| 601 | |||
| 602 | S_STAT(if_stat) | 592 | S_STAT(if_stat) |
| 603 | ullong old[4]; | 593 | ullong old[4]; |
| 604 | const char *device; | 594 | const char *device; |
| @@ -645,7 +635,6 @@ static s_stat* init_if(const char *device) | |||
| 645 | return (s_stat*)s; | 635 | return (s_stat*)s; |
| 646 | } | 636 | } |
| 647 | 637 | ||
| 648 | |||
| 649 | S_STAT(mem_stat) | 638 | S_STAT(mem_stat) |
| 650 | char opt; | 639 | char opt; |
| 651 | S_STAT_END(mem_stat) | 640 | S_STAT_END(mem_stat) |
| @@ -728,7 +717,6 @@ static s_stat* init_mem(const char *param) | |||
| 728 | return (s_stat*)s; | 717 | return (s_stat*)s; |
| 729 | } | 718 | } |
| 730 | 719 | ||
| 731 | |||
| 732 | S_STAT(swp_stat) | 720 | S_STAT(swp_stat) |
| 733 | S_STAT_END(swp_stat) | 721 | S_STAT_END(swp_stat) |
| 734 | 722 | ||
| @@ -752,7 +740,6 @@ static s_stat* init_swp(const char *param UNUSED_PARAM) | |||
| 752 | return (s_stat*)s; | 740 | return (s_stat*)s; |
| 753 | } | 741 | } |
| 754 | 742 | ||
| 755 | |||
| 756 | S_STAT(fd_stat) | 743 | S_STAT(fd_stat) |
| 757 | S_STAT_END(fd_stat) | 744 | S_STAT_END(fd_stat) |
| 758 | 745 | ||
| @@ -778,17 +765,16 @@ static s_stat* init_fd(const char *param UNUSED_PARAM) | |||
| 778 | return (s_stat*)s; | 765 | return (s_stat*)s; |
| 779 | } | 766 | } |
| 780 | 767 | ||
| 781 | |||
| 782 | S_STAT(time_stat) | 768 | S_STAT(time_stat) |
| 783 | int prec; | 769 | unsigned prec; |
| 784 | int scale; | 770 | unsigned scale; |
| 785 | S_STAT_END(time_stat) | 771 | S_STAT_END(time_stat) |
| 786 | 772 | ||
| 787 | static void FAST_FUNC collect_time(time_stat *s) | 773 | static void FAST_FUNC collect_time(time_stat *s) |
| 788 | { | 774 | { |
| 789 | char buf[sizeof("12:34:56.123456")]; | 775 | char buf[sizeof("12:34:56.123456")]; |
| 790 | struct tm* tm; | 776 | struct tm* tm; |
| 791 | int us = tv.tv_usec + s->scale/2; | 777 | unsigned us = tv.tv_usec + s->scale/2; |
| 792 | time_t t = tv.tv_sec; | 778 | time_t t = tv.tv_sec; |
| 793 | 779 | ||
| 794 | if (us >= 1000000) { | 780 | if (us >= 1000000) { |
| @@ -829,7 +815,6 @@ static void FAST_FUNC collect_info(s_stat *s) | |||
| 829 | } | 815 | } |
| 830 | } | 816 | } |
| 831 | 817 | ||
| 832 | |||
| 833 | typedef s_stat* init_func(const char *param); | 818 | typedef s_stat* init_func(const char *param); |
| 834 | 819 | ||
| 835 | // Deprecated %NNNd is to be removed, -d MSEC supersedes it | 820 | // Deprecated %NNNd is to be removed, -d MSEC supersedes it |
| @@ -951,7 +936,7 @@ int nmeter_main(int argc UNUSED_PARAM, char **argv) | |||
| 951 | while (1) { | 936 | while (1) { |
| 952 | gettimeofday(&tv, NULL); | 937 | gettimeofday(&tv, NULL); |
| 953 | collect_info(first); | 938 | collect_info(first); |
| 954 | put(final_str); | 939 | put_c(G.final_char); |
| 955 | print_outbuf(); | 940 | print_outbuf(); |
| 956 | 941 | ||
| 957 | // Negative delta -> no usleep at all | 942 | // Negative delta -> no usleep at all |
