diff options
author | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2007-02-03 00:53:43 +0000 |
---|---|---|
committer | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2007-02-03 00:53:43 +0000 |
commit | faa511cbbed2a73e129a354b897c12995e36ab48 (patch) | |
tree | 8567a41729a6e4c79fb0ca4bc40cc5df42e76710 /runit | |
parent | 90b8e4482b34c7102957eda28e699b8a906fef1e (diff) | |
download | busybox-w32-faa511cbbed2a73e129a354b897c12995e36ab48.tar.gz busybox-w32-faa511cbbed2a73e129a354b897c12995e36ab48.tar.bz2 busybox-w32-faa511cbbed2a73e129a354b897c12995e36ab48.zip |
svlogd: fix performance problems: excessive write,
gettimeofday calls. rt_sigprocmask were dealt with
in previous commit
git-svn-id: svn://busybox.net/trunk/busybox@17731 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'runit')
-rw-r--r-- | runit/svlogd.c | 72 |
1 files changed, 46 insertions, 26 deletions
diff --git a/runit/svlogd.c b/runit/svlogd.c index a0e562b21..f0e9aaf6f 100644 --- a/runit/svlogd.c +++ b/runit/svlogd.c | |||
@@ -59,7 +59,7 @@ static iopause_fd input; | |||
59 | static int fl_flag_0; | 59 | static int fl_flag_0; |
60 | 60 | ||
61 | static struct logdir { | 61 | static struct logdir { |
62 | //// char *btmp; | 62 | ////char *btmp; |
63 | /* pattern list to match, in "aa\0bb\0\cc\0\0" form */ | 63 | /* pattern list to match, in "aa\0bb\0\cc\0\0" form */ |
64 | char *inst; | 64 | char *inst; |
65 | char *processor; | 65 | char *processor; |
@@ -73,6 +73,7 @@ static struct logdir { | |||
73 | int ppid; | 73 | int ppid; |
74 | int fddir; | 74 | int fddir; |
75 | int fdcur; | 75 | int fdcur; |
76 | FILE* filecur; //// | ||
76 | int fdlock; | 77 | int fdlock; |
77 | struct taia trotate; | 78 | struct taia trotate; |
78 | char fnsave[FMT_PTIME]; | 79 | char fnsave[FMT_PTIME]; |
@@ -300,11 +301,13 @@ static unsigned rotate(struct logdir *ld) | |||
300 | } | 301 | } |
301 | 302 | ||
302 | if (ld->size > 0) { | 303 | if (ld->size > 0) { |
303 | while (fsync(ld->fdcur) == -1) | 304 | while (fflush(ld->filecur) || fsync(ld->fdcur) == -1) |
304 | pause2cannot("fsync current logfile", ld->name); | 305 | pause2cannot("fsync current logfile", ld->name); |
305 | while (fchmod(ld->fdcur, 0744) == -1) | 306 | while (fchmod(ld->fdcur, 0744) == -1) |
306 | pause2cannot("set mode of current", ld->name); | 307 | pause2cannot("set mode of current", ld->name); |
307 | close(ld->fdcur); | 308 | ////close(ld->fdcur); |
309 | fclose(ld->filecur); | ||
310 | |||
308 | if (verbose) { | 311 | if (verbose) { |
309 | bb_error_msg(INFO"rename: %s/current %s %u", ld->name, | 312 | bb_error_msg(INFO"rename: %s/current %s %u", ld->name, |
310 | ld->fnsave, ld->size); | 313 | ld->fnsave, ld->size); |
@@ -313,6 +316,9 @@ static unsigned rotate(struct logdir *ld) | |||
313 | pause2cannot("rename current", ld->name); | 316 | pause2cannot("rename current", ld->name); |
314 | while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1) | 317 | while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1) |
315 | pause2cannot("create new current", ld->name); | 318 | pause2cannot("create new current", ld->name); |
319 | /* we presume this cannot fail */ | ||
320 | ld->filecur = fdopen(ld->fdcur, "a"); //// | ||
321 | setvbuf(ld->filecur, NULL, _IOFBF, linelen); //// | ||
316 | coe(ld->fdcur); | 322 | coe(ld->fdcur); |
317 | ld->size = 0; | 323 | ld->size = 0; |
318 | while (fchmod(ld->fdcur, 0644) == -1) | 324 | while (fchmod(ld->fdcur, 0644) == -1) |
@@ -337,7 +343,12 @@ static int buffer_pwrite(int n, char *s, unsigned len) | |||
337 | if (len > (ld->sizemax - ld->size)) | 343 | if (len > (ld->sizemax - ld->size)) |
338 | len = ld->sizemax - ld->size; | 344 | len = ld->sizemax - ld->size; |
339 | } | 345 | } |
340 | while ((i = full_write(ld->fdcur, s, len)) == -1) { | 346 | while (1) { |
347 | ////i = full_write(ld->fdcur, s, len); | ||
348 | ////if (i != -1) break; | ||
349 | i = fwrite(s, 1, len, ld->filecur); | ||
350 | if (i == len) break; | ||
351 | |||
341 | if ((errno == ENOSPC) && (ld->nmin < ld->nmax)) { | 352 | if ((errno == ENOSPC) && (ld->nmin < ld->nmax)) { |
342 | DIR *d; | 353 | DIR *d; |
343 | struct dirent *f; | 354 | struct dirent *f; |
@@ -399,11 +410,12 @@ static void logdir_close(struct logdir *ld) | |||
399 | ld->fddir = -1; | 410 | ld->fddir = -1; |
400 | if (ld->fdcur == -1) | 411 | if (ld->fdcur == -1) |
401 | return; /* impossible */ | 412 | return; /* impossible */ |
402 | while (fsync(ld->fdcur) == -1) | 413 | while (fflush(ld->filecur) || fsync(ld->fdcur) == -1) |
403 | pause2cannot("fsync current logfile", ld->name); | 414 | pause2cannot("fsync current logfile", ld->name); |
404 | while (fchmod(ld->fdcur, 0744) == -1) | 415 | while (fchmod(ld->fdcur, 0744) == -1) |
405 | pause2cannot("set mode of current", ld->name); | 416 | pause2cannot("set mode of current", ld->name); |
406 | close(ld->fdcur); | 417 | ////close(ld->fdcur); |
418 | fclose(ld->filecur); | ||
407 | ld->fdcur = -1; | 419 | ld->fdcur = -1; |
408 | if (ld->fdlock == -1) | 420 | if (ld->fdlock == -1) |
409 | return; /* impossible */ | 421 | return; /* impossible */ |
@@ -560,6 +572,10 @@ static unsigned logdir_open(struct logdir *ld, const char *fn) | |||
560 | } | 572 | } |
561 | while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1) | 573 | while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1) |
562 | pause2cannot("open current", ld->name); | 574 | pause2cannot("open current", ld->name); |
575 | /* we presume this cannot fail */ | ||
576 | ld->filecur = fdopen(ld->fdcur, "a"); //// | ||
577 | setvbuf(ld->filecur, NULL, _IOFBF, linelen); //// | ||
578 | |||
563 | coe(ld->fdcur); | 579 | coe(ld->fdcur); |
564 | while (fchmod(ld->fdcur, 0644) == -1) | 580 | while (fchmod(ld->fdcur, 0644) == -1) |
565 | pause2cannot("set mode of current", ld->name); | 581 | pause2cannot("set mode of current", ld->name); |
@@ -601,9 +617,8 @@ static ssize_t ndelay_read(int fd, void *buf, size_t count) | |||
601 | } | 617 | } |
602 | 618 | ||
603 | /* Used for reading stdin */ | 619 | /* Used for reading stdin */ |
604 | static int buffer_pread(int fd, char *s, unsigned len) | 620 | static int buffer_pread(int fd, char *s, unsigned len, struct taia *now) |
605 | { | 621 | { |
606 | struct taia now; | ||
607 | int i; | 622 | int i; |
608 | 623 | ||
609 | if (rotateasap) { | 624 | if (rotateasap) { |
@@ -620,12 +635,11 @@ static int buffer_pread(int fd, char *s, unsigned len) | |||
620 | logdirs_reopen(); | 635 | logdirs_reopen(); |
621 | reopenasap = 0; | 636 | reopenasap = 0; |
622 | } | 637 | } |
623 | taia_now(&now); | ||
624 | taia_uint(&trotate, 2744); | 638 | taia_uint(&trotate, 2744); |
625 | taia_add(&trotate, &now, &trotate); | 639 | taia_add(&trotate, now, &trotate); |
626 | for (i = 0; i < dirn; ++i) | 640 | for (i = 0; i < dirn; ++i) |
627 | if (dir[i].tmax) { | 641 | if (dir[i].tmax) { |
628 | if (taia_less(&dir[i].trotate, &now)) | 642 | if (taia_less(&dir[i].trotate, now)) |
629 | rotate(dir+i); | 643 | rotate(dir+i); |
630 | if (taia_less(&dir[i].trotate, &trotate)) | 644 | if (taia_less(&dir[i].trotate, &trotate)) |
631 | trotate = dir[i].trotate; | 645 | trotate = dir[i].trotate; |
@@ -633,7 +647,7 @@ static int buffer_pread(int fd, char *s, unsigned len) | |||
633 | 647 | ||
634 | while (1) { | 648 | while (1) { |
635 | sigprocmask(SIG_UNBLOCK, &blocked_sigset, NULL); | 649 | sigprocmask(SIG_UNBLOCK, &blocked_sigset, NULL); |
636 | iopause(&input, 1, &trotate, &now); | 650 | iopause(&input, 1, &trotate, now); |
637 | sigprocmask(SIG_BLOCK, &blocked_sigset, NULL); | 651 | sigprocmask(SIG_BLOCK, &blocked_sigset, NULL); |
638 | i = ndelay_read(fd, s, len); | 652 | i = ndelay_read(fd, s, len); |
639 | if (i >= 0) break; | 653 | if (i >= 0) break; |
@@ -734,7 +748,6 @@ static void logmatch(struct logdir *ld) | |||
734 | 748 | ||
735 | int svlogd_main(int argc, char **argv) | 749 | int svlogd_main(int argc, char **argv) |
736 | { | 750 | { |
737 | struct taia now; | ||
738 | char *r,*l,*b; | 751 | char *r,*l,*b; |
739 | ssize_t stdin_cnt = 0; | 752 | ssize_t stdin_cnt = 0; |
740 | int i; | 753 | int i; |
@@ -757,10 +770,10 @@ int svlogd_main(int argc, char **argv) | |||
757 | if (linemax == 0) linemax = BUFSIZ-26; | 770 | if (linemax == 0) linemax = BUFSIZ-26; |
758 | if (linemax < 256) linemax = 256; | 771 | if (linemax < 256) linemax = 256; |
759 | } | 772 | } |
760 | //// if (opt & 8) { // -b | 773 | ////if (opt & 8) { // -b |
761 | //// buflen = xatoi_u(b); | 774 | //// buflen = xatoi_u(b); |
762 | //// if (buflen == 0) buflen = 1024; | 775 | //// if (buflen == 0) buflen = 1024; |
763 | //// } | 776 | ////} |
764 | //if (opt & 0x10) timestamp++; // -t | 777 | //if (opt & 0x10) timestamp++; // -t |
765 | //if (opt & 0x20) verbose++; // -v | 778 | //if (opt & 0x20) verbose++; // -v |
766 | //if (timestamp > 2) timestamp = 2; | 779 | //if (timestamp > 2) timestamp = 2; |
@@ -807,8 +820,11 @@ int svlogd_main(int argc, char **argv) | |||
807 | if (!timestamp) | 820 | if (!timestamp) |
808 | memRchr = memrchr; | 821 | memRchr = memrchr; |
809 | 822 | ||
823 | setvbuf(stderr, NULL, _IOFBF, linelen); | ||
824 | |||
810 | /* Each iteration processes one or more lines */ | 825 | /* Each iteration processes one or more lines */ |
811 | while (1) { | 826 | while (1) { |
827 | struct taia now; | ||
812 | char stamp[FMT_PTIME]; | 828 | char stamp[FMT_PTIME]; |
813 | char *lineptr; | 829 | char *lineptr; |
814 | char *printptr; | 830 | char *printptr; |
@@ -817,9 +833,9 @@ int svlogd_main(int argc, char **argv) | |||
817 | char ch; | 833 | char ch; |
818 | 834 | ||
819 | lineptr = line; | 835 | lineptr = line; |
836 | taia_now(&now); | ||
820 | /* Prepare timestamp if needed */ | 837 | /* Prepare timestamp if needed */ |
821 | if (timestamp) { | 838 | if (timestamp) { |
822 | taia_now(&now); | ||
823 | switch (timestamp) { | 839 | switch (timestamp) { |
824 | case 1: | 840 | case 1: |
825 | fmt_taia25(stamp, &now); | 841 | fmt_taia25(stamp, &now); |
@@ -839,7 +855,7 @@ int svlogd_main(int argc, char **argv) | |||
839 | if (!np && !exitasap) { | 855 | if (!np && !exitasap) { |
840 | i = linemax - stdin_cnt; /* avail. bytes at tail */ | 856 | i = linemax - stdin_cnt; /* avail. bytes at tail */ |
841 | if (i >= 128) { | 857 | if (i >= 128) { |
842 | i = buffer_pread(0, lineptr + stdin_cnt, i); | 858 | i = buffer_pread(0, lineptr + stdin_cnt, i, &now); |
843 | if (i <= 0) /* EOF or error on stdin */ | 859 | if (i <= 0) /* EOF or error on stdin */ |
844 | exitasap = 1; | 860 | exitasap = 1; |
845 | else { | 861 | else { |
@@ -861,10 +877,10 @@ int svlogd_main(int argc, char **argv) | |||
861 | /* linelen == no of chars incl. '\n' (or == stdin_cnt) */ | 877 | /* linelen == no of chars incl. '\n' (or == stdin_cnt) */ |
862 | ch = lineptr[linelen-1]; | 878 | ch = lineptr[linelen-1]; |
863 | 879 | ||
864 | /* TODO: biggest performance hit is coming from the fact | 880 | /* Biggest performance hit was coming from the fact |
865 | * that we do not buffer writes. We may read many lines | 881 | * that we did not buffer writes. We were reading many lines |
866 | * in one read() above, but will do one write() | 882 | * in one read() above, but wrote one line per write(). |
867 | * per line below. Should we use stdio? */ | 883 | * We are using stdio to fix that */ |
868 | 884 | ||
869 | /* write out lineptr[0..linelen-1] to each log destination | 885 | /* write out lineptr[0..linelen-1] to each log destination |
870 | * (or lineptr[-26..linelen-1] if timestamping) */ | 886 | * (or lineptr[-26..linelen-1] if timestamping) */ |
@@ -882,7 +898,8 @@ int svlogd_main(int argc, char **argv) | |||
882 | if (ld->inst) | 898 | if (ld->inst) |
883 | logmatch(ld); | 899 | logmatch(ld); |
884 | if (ld->matcherr == 'e') | 900 | if (ld->matcherr == 'e') |
885 | full_write(2, printptr, printlen); | 901 | ////full_write(2, printptr, printlen); |
902 | fwrite(lineptr, 1, linelen, stderr); | ||
886 | if (ld->match != '+') continue; | 903 | if (ld->match != '+') continue; |
887 | buffer_pwrite(i, printptr, printlen); | 904 | buffer_pwrite(i, printptr, printlen); |
888 | } | 905 | } |
@@ -891,7 +908,8 @@ int svlogd_main(int argc, char **argv) | |||
891 | /* read/write repeatedly until we see it */ | 908 | /* read/write repeatedly until we see it */ |
892 | while (ch != '\n') { | 909 | while (ch != '\n') { |
893 | /* lineptr is emptied now, safe to use as buffer */ | 910 | /* lineptr is emptied now, safe to use as buffer */ |
894 | stdin_cnt = exitasap ? -1 : buffer_pread(0, lineptr, linemax); | 911 | taia_now(&now); |
912 | stdin_cnt = exitasap ? -1 : buffer_pread(0, lineptr, linemax, &now); | ||
895 | if (stdin_cnt <= 0) { /* EOF or error on stdin */ | 913 | if (stdin_cnt <= 0) { /* EOF or error on stdin */ |
896 | exitasap = 1; | 914 | exitasap = 1; |
897 | lineptr[0] = ch = '\n'; | 915 | lineptr[0] = ch = '\n'; |
@@ -908,7 +926,8 @@ int svlogd_main(int argc, char **argv) | |||
908 | for (i = 0; i < dirn; ++i) { | 926 | for (i = 0; i < dirn; ++i) { |
909 | if (dir[i].fddir == -1) continue; | 927 | if (dir[i].fddir == -1) continue; |
910 | if (dir[i].matcherr == 'e') | 928 | if (dir[i].matcherr == 'e') |
911 | full_write(2, lineptr, linelen); | 929 | ////full_write(2, lineptr, linelen); |
930 | fwrite(lineptr, 1, linelen, stderr); | ||
912 | if (dir[i].match != '+') continue; | 931 | if (dir[i].match != '+') continue; |
913 | buffer_pwrite(i, lineptr, linelen); | 932 | buffer_pwrite(i, lineptr, linelen); |
914 | } | 933 | } |
@@ -925,6 +944,7 @@ int svlogd_main(int argc, char **argv) | |||
925 | /* Move unprocessed data to the front of line */ | 944 | /* Move unprocessed data to the front of line */ |
926 | memmove((timestamp ? line+26 : line), lineptr, stdin_cnt); | 945 | memmove((timestamp ? line+26 : line), lineptr, stdin_cnt); |
927 | } | 946 | } |
947 | fflush(NULL);//// | ||
928 | } | 948 | } |
929 | 949 | ||
930 | for (i = 0; i < dirn; ++i) { | 950 | for (i = 0; i < dirn; ++i) { |