Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/procps/nmeter.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/procps/nmeter.c
r1765 r2725 1 1 /* 2 ** Licensed under the GPL v2, see the file LICENSE in this tarball 3 ** 4 ** Based on nanotop.c from floppyfw project 5 ** 6 ** Contact me: vda.linux@googlemail.com */ 2 * Licensed under GPLv2, see file LICENSE in this source tree. 3 * 4 * Based on nanotop.c from floppyfw project 5 * 6 * Contact me: vda.linux@googlemail.com 7 */ 7 8 8 9 //TODO: … … 18 19 // totalswap=134209536, freeswap=134209536, procs=157}) 19 20 20 #include <time.h>21 21 #include "libbb.h" 22 22 23 23 typedef unsigned long long ullong; 24 24 25 enum { PROC_FILE_SIZE = 4096 }; 25 enum { /* Preferably use powers of 2 */ 26 PROC_MIN_FILE_SIZE = 256, 27 PROC_MAX_FILE_SIZE = 16 * 1024, 28 }; 26 29 27 30 typedef struct proc_file { 28 31 char *file; 29 //const char *name;32 int file_sz; 30 33 smallint last_gen; 31 34 } proc_file; … … 76 79 #define proc_sys_fs_filenr (G.proc_sys_fs_filenr) 77 80 #define INIT_G() do { \ 78 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \79 80 81 82 81 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ 82 cur_outbuf = outbuf; \ 83 final_str = "\n"; \ 84 deltanz = delta = 1000000; \ 85 } while (0) 83 86 84 87 // We depend on this being a char[], not char* - we take sizeof() of it … … 99 102 int sz = cur_outbuf - outbuf; 100 103 if (sz > 0) { 101 xwrite( 1, outbuf, sz);104 xwrite(STDOUT_FILENO, outbuf, sz); 102 105 cur_outbuf = outbuf; 103 106 } … … 125 128 } 126 129 127 static void readfile_z( char *buf, int sz, const char* fname)130 static void readfile_z(proc_file *pf, const char* fname) 128 131 { 129 132 // open_read_close() will do two reads in order to be sure we are at EOF, 130 133 // and we don't need/want that. 131 // sz = open_read_close(fname, buf, sz-1); 132 133 int fd = xopen(fname, O_RDONLY); 134 int fd; 135 int sz, rdsz; 136 char *buf; 137 138 sz = pf->file_sz; 139 buf = pf->file; 140 if (!buf) { 141 buf = xmalloc(PROC_MIN_FILE_SIZE); 142 sz = PROC_MIN_FILE_SIZE; 143 } 144 again: 145 fd = xopen(fname, O_RDONLY); 134 146 buf[0] = '\0'; 135 if (fd >= 0) { 136 sz = read(fd, buf, sz-1); 137 if (sz > 0) buf[sz] = '\0'; 138 close(fd); 139 } 147 rdsz = read(fd, buf, sz-1); 148 close(fd); 149 if (rdsz > 0) { 150 if (rdsz == sz-1 && sz < PROC_MAX_FILE_SIZE) { 151 sz *= 2; 152 buf = xrealloc(buf, sz); 153 goto again; 154 } 155 buf[rdsz] = '\0'; 156 } 157 pf->file_sz = sz; 158 pf->file = buf; 140 159 } 141 160 … … 144 163 if (pf->last_gen != gen) { 145 164 pf->last_gen = gen; 146 // We allocate PROC_FILE_SIZE bytes. This wastes memory, 147 // but allows us to allocate only once (at first sample) 148 // per proc file, and reuse buffer for each sample 149 if (!pf->file) 150 pf->file = xmalloc(PROC_FILE_SIZE); 151 readfile_z(pf->file, PROC_FILE_SIZE, proc_name[pf - &first_proc_file]); 165 readfile_z(pf, proc_name[pf - &first_proc_file]); 152 166 } 153 167 return pf->file; 154 168 } 155 169 156 static inlineullong read_after_slash(const char *p)170 static ullong read_after_slash(const char *p) 157 171 { 158 172 p = strchr(p, '/'); … … 229 243 static int rdval_diskstats(const char* p, ullong *vec) 230 244 { 231 ullong rd = 0; // to avoid "warning: 'rd' might be used uninitialized"245 ullong rd = rd; // for compiler 232 246 int indexline = 0; 233 247 vec[0] = 0; … … 258 272 { 259 273 char buf[5]; 260 smart_ulltoa5(ul, buf); 274 275 /* see http://en.wikipedia.org/wiki/Tera */ 276 smart_ulltoa4(ul, buf, " kmgtpezy"); 277 buf[4] = '\0'; 261 278 put(buf); 262 279 } … … 266 283 typedef struct a { \ 267 284 struct s_stat *next; \ 268 void (*collect)(struct a *s) ; \285 void (*collect)(struct a *s) FAST_FUNC; \ 269 286 const char *label; 270 287 #define S_STAT_END(a) } a; … … 273 290 S_STAT_END(s_stat) 274 291 275 static void collect_literal(s_stat *s)292 static void FAST_FUNC collect_literal(s_stat *s UNUSED_PARAM) 276 293 { 277 294 } … … 279 296 static s_stat* init_literal(void) 280 297 { 281 s_stat *s = x malloc(sizeof(s_stat));298 s_stat *s = xzalloc(sizeof(*s)); 282 299 s->collect = collect_literal; 283 300 return (s_stat*)s; … … 286 303 static s_stat* init_delay(const char *param) 287 304 { 288 delta = bb_strtoi(param, NULL, 0) * 1000;305 delta = strtoul(param, NULL, 0) * 1000; /* param can be "" */ 289 306 deltanz = delta > 0 ? delta : 1; 290 307 need_seconds = (1000000%deltanz) != 0; … … 292 309 } 293 310 294 static s_stat* init_cr(const char *param )311 static s_stat* init_cr(const char *param UNUSED_PARAM) 295 312 { 296 313 final_str = "\r"; … … 310 327 311 328 312 static void collect_cpu(cpu_stat *s)329 static void FAST_FUNC collect_cpu(cpu_stat *s) 313 330 { 314 331 ullong data[CPU_FIELDCNT] = { 0, 0, 0, 0, 0, 0, 0 }; … … 367 384 { 368 385 int sz; 369 cpu_stat *s = x malloc(sizeof(cpu_stat));386 cpu_stat *s = xzalloc(sizeof(*s)); 370 387 s->collect = collect_cpu; 371 sz = strto l(param, NULL, 0);388 sz = strtoul(param, NULL, 0); /* param can be "" */ 372 389 if (sz < 10) sz = 10; 373 390 if (sz > 1000) sz = 1000; 374 s->bar = x malloc(sz+1);375 s->bar[sz] = '\0';391 s->bar = xzalloc(sz+1); 392 /*s->bar[sz] = '\0'; - xzalloc did it */ 376 393 s->bar_sz = sz; 377 394 return (s_stat*)s; … … 384 401 S_STAT_END(int_stat) 385 402 386 static void collect_int(int_stat *s)403 static void FAST_FUNC collect_int(int_stat *s) 387 404 { 388 405 ullong data[1]; … … 402 419 static s_stat* init_int(const char *param) 403 420 { 404 int_stat *s = x malloc(sizeof(int_stat));421 int_stat *s = xzalloc(sizeof(*s)); 405 422 s->collect = collect_int; 406 if (param[0] =='\0') {423 if (param[0] == '\0') { 407 424 s->no = 1; 408 425 } else { 409 int n = strtoul(param, NULL, 0);410 s->no = n +2;426 int n = xatoi_positive(param); 427 s->no = n + 2; 411 428 } 412 429 return (s_stat*)s; … … 418 435 S_STAT_END(ctx_stat) 419 436 420 static void collect_ctx(ctx_stat *s)437 static void FAST_FUNC collect_ctx(ctx_stat *s) 421 438 { 422 439 ullong data[1]; … … 434 451 } 435 452 436 static s_stat* init_ctx(const char *param )437 { 438 ctx_stat *s = x malloc(sizeof(ctx_stat));453 static s_stat* init_ctx(const char *param UNUSED_PARAM) 454 { 455 ctx_stat *s = xzalloc(sizeof(*s)); 439 456 s->collect = collect_ctx; 440 457 return (s_stat*)s; … … 447 464 S_STAT_END(blk_stat) 448 465 449 static void collect_blk(blk_stat *s)466 static void FAST_FUNC collect_blk(blk_stat *s) 450 467 { 451 468 ullong data[2]; … … 476 493 } 477 494 478 static s_stat* init_blk(const char *param )479 { 480 blk_stat *s = x malloc(sizeof(blk_stat));495 static s_stat* init_blk(const char *param UNUSED_PARAM) 496 { 497 blk_stat *s = xzalloc(sizeof(*s)); 481 498 s->collect = collect_blk; 482 499 s->lookfor = "page"; … … 489 506 S_STAT_END(fork_stat) 490 507 491 static void collect_thread_nr(fork_stat *s)508 static void FAST_FUNC collect_thread_nr(fork_stat *s UNUSED_PARAM) 492 509 { 493 510 ullong data[1]; … … 500 517 } 501 518 502 static void collect_fork(fork_stat *s)519 static void FAST_FUNC collect_fork(fork_stat *s) 503 520 { 504 521 ullong data[1]; … … 518 535 static s_stat* init_fork(const char *param) 519 536 { 520 fork_stat *s = x malloc(sizeof(fork_stat));537 fork_stat *s = xzalloc(sizeof(*s)); 521 538 if (*param == 'n') { 522 539 s->collect = collect_thread_nr; … … 534 551 S_STAT_END(if_stat) 535 552 536 static void collect_if(if_stat *s)553 static void FAST_FUNC collect_if(if_stat *s) 537 554 { 538 555 ullong data[4]; … … 558 575 static s_stat* init_if(const char *device) 559 576 { 560 if_stat *s = x malloc(sizeof(if_stat));577 if_stat *s = xzalloc(sizeof(*s)); 561 578 562 579 if (!device || !device[0]) … … 565 582 566 583 s->device = device; 567 s->device_colon = xmalloc(strlen(device)+2); 568 strcpy(s->device_colon, device); 569 strcat(s->device_colon, ":"); 584 s->device_colon = xasprintf("%s:", device); 570 585 return (s_stat*)s; 571 586 } … … 611 626 //HugePages_Free: 0 612 627 //Hugepagesize: 4096 kB 613 static void collect_mem(mem_stat *s)628 static void FAST_FUNC collect_mem(mem_stat *s) 614 629 { 615 630 ullong m_total = 0; … … 623 638 return; 624 639 } 625 if (s->opt == ' f') {640 if (s->opt == 't') { 626 641 scale(m_total << 10); 627 642 return; … … 648 663 static s_stat* init_mem(const char *param) 649 664 { 650 mem_stat *s = x malloc(sizeof(mem_stat));665 mem_stat *s = xzalloc(sizeof(*s)); 651 666 s->collect = collect_mem; 652 667 s->opt = param[0]; … … 658 673 S_STAT_END(swp_stat) 659 674 660 static void collect_swp(swp_stat *s)675 static void FAST_FUNC collect_swp(swp_stat *s UNUSED_PARAM) 661 676 { 662 677 ullong s_total[1]; … … 671 686 } 672 687 673 static s_stat* init_swp(const char *param )674 { 675 swp_stat *s = x malloc(sizeof(swp_stat));688 static s_stat* init_swp(const char *param UNUSED_PARAM) 689 { 690 swp_stat *s = xzalloc(sizeof(*s)); 676 691 s->collect = collect_swp; 677 692 return (s_stat*)s; … … 682 697 S_STAT_END(fd_stat) 683 698 684 static void collect_fd(fd_stat *s)699 static void FAST_FUNC collect_fd(fd_stat *s UNUSED_PARAM) 685 700 { 686 701 ullong data[2]; … … 694 709 } 695 710 696 static s_stat* init_fd(const char *param )697 { 698 fd_stat *s = x malloc(sizeof(fd_stat));711 static s_stat* init_fd(const char *param UNUSED_PARAM) 712 { 713 fd_stat *s = xzalloc(sizeof(*s)); 699 714 s->collect = collect_fd; 700 715 return (s_stat*)s; … … 707 722 S_STAT_END(time_stat) 708 723 709 static void collect_time(time_stat *s)724 static void FAST_FUNC collect_time(time_stat *s) 710 725 { 711 726 char buf[sizeof("12:34:56.123456")]; … … 729 744 { 730 745 int prec; 731 time_stat *s = x malloc(sizeof(time_stat));746 time_stat *s = xzalloc(sizeof(*s)); 732 747 733 748 s->collect = collect_time; 734 prec = param[0] -'0';749 prec = param[0] - '0'; 735 750 if (prec < 0) prec = 0; 736 751 else if (prec > 6) prec = 6; … … 742 757 } 743 758 744 static void collect_info(s_stat *s)759 static void FAST_FUNC collect_info(s_stat *s) 745 760 { 746 761 gen ^= 1; … … 771 786 }; 772 787 773 int nmeter_main(int argc, char **argv) ;774 int nmeter_main(int argc , char **argv)788 int nmeter_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 789 int nmeter_main(int argc UNUSED_PARAM, char **argv) 775 790 { 776 791 char buf[32]; … … 784 799 xchdir("/proc"); 785 800 786 if ( argc != 2)801 if (!argv[1]) 787 802 bb_show_usage(); 788 803 789 if (open_read_close("version", buf, sizeof(buf)) > 0) 790 is26 = (strstr(buf, " 2.4.")==NULL); 804 if (open_read_close("version", buf, sizeof(buf)-1) > 0) { 805 buf[sizeof(buf)-1] = '\0'; 806 is26 = (strstr(buf, " 2.4.") == NULL); 807 } 791 808 792 809 // Can use argv[1] directly, but this will mess up … … 801 818 break; 802 819 if (cur[1] == '%') { // %% 803 strcpy(cur, cur+1);820 overlapping_strcpy(cur, cur + 1); 804 821 cur++; 805 822 goto again; … … 832 849 if (s) { 833 850 s->label = prev; 834 s->next = 0;851 /*s->next = NULL; - all initXXX funcs use xzalloc */ 835 852 if (!first) 836 853 first = s; … … 847 864 s = init_literal(); 848 865 s->label = prev; 849 s->next = 0;866 /*s->next = NULL; - all initXXX funcs use xzalloc */ 850 867 if (!first) 851 868 first = s;
Note:
See TracChangeset
for help on using the changeset viewer.