diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-01-27 22:24:59 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-01-27 22:24:59 +0000 |
commit | ca549c5e69a276918c3b66794d2e860caa9d28cf (patch) | |
tree | 037962e903fc473938d62f678b7ef6c4a00b2ad0 /runit | |
parent | bde74b617f0c17921a846a28aac63330951a9cc7 (diff) | |
download | busybox-w32-ca549c5e69a276918c3b66794d2e860caa9d28cf.tar.gz busybox-w32-ca549c5e69a276918c3b66794d2e860caa9d28cf.tar.bz2 busybox-w32-ca549c5e69a276918c3b66794d2e860caa9d28cf.zip |
svlogd: remove unused buffering, stop doing tons of memcpy
Diffstat (limited to 'runit')
-rw-r--r-- | runit/runit_lib.c | 4 | ||||
-rw-r--r-- | runit/svlogd.c | 88 |
2 files changed, 58 insertions, 34 deletions
diff --git a/runit/runit_lib.c b/runit/runit_lib.c index aa1d96002..6401ba0c8 100644 --- a/runit/runit_lib.c +++ b/runit/runit_lib.c | |||
@@ -286,12 +286,12 @@ uint64_t taia2millisec(const struct taia *t) | |||
286 | } | 286 | } |
287 | 287 | ||
288 | 288 | ||
289 | void iopause(iopause_fd *x,unsigned len,struct taia *deadline,struct taia *stamp) | 289 | void iopause(iopause_fd *x, unsigned len, struct taia *deadline, struct taia *stamp) |
290 | { | 290 | { |
291 | int millisecs; | 291 | int millisecs; |
292 | int i; | 292 | int i; |
293 | 293 | ||
294 | if (taia_less(deadline,stamp)) | 294 | if (taia_less(deadline, stamp)) |
295 | millisecs = 0; | 295 | millisecs = 0; |
296 | else { | 296 | else { |
297 | uint64_t m; | 297 | uint64_t m; |
diff --git a/runit/svlogd.c b/runit/svlogd.c index 67ffc443c..a588ffeb0 100644 --- a/runit/svlogd.c +++ b/runit/svlogd.c | |||
@@ -35,7 +35,7 @@ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |||
35 | 35 | ||
36 | static unsigned verbose; | 36 | static unsigned verbose; |
37 | static int linemax = 1000; | 37 | static int linemax = 1000; |
38 | static int buflen = 1024; | 38 | ////static int buflen = 1024; |
39 | static int linelen; | 39 | static int linelen; |
40 | 40 | ||
41 | static char **fndir; | 41 | static char **fndir; |
@@ -49,13 +49,13 @@ static unsigned rotateasap; | |||
49 | static unsigned reopenasap; | 49 | static unsigned reopenasap; |
50 | static unsigned linecomplete = 1; | 50 | static unsigned linecomplete = 1; |
51 | static unsigned tmaxflag; | 51 | static unsigned tmaxflag; |
52 | static iopause_fd in; | 52 | static iopause_fd input; |
53 | 53 | ||
54 | static const char *replace = ""; | 54 | static const char *replace = ""; |
55 | static char repl; | 55 | static char repl; |
56 | 56 | ||
57 | static struct logdir { | 57 | static struct logdir { |
58 | char *btmp; | 58 | ////char *btmp; |
59 | /* pattern list to match, in "aa\0bb\0\cc\0\0" form */ | 59 | /* pattern list to match, in "aa\0bb\0\cc\0\0" form */ |
60 | char *inst; | 60 | char *inst; |
61 | char *processor; | 61 | char *processor; |
@@ -373,7 +373,8 @@ static int buffer_pwrite(int n, char *s, unsigned len) | |||
373 | } | 373 | } |
374 | } | 374 | } |
375 | } | 375 | } |
376 | if (errno) pause2cannot("write to current", ld->name); | 376 | if (errno) |
377 | pause2cannot("write to current", ld->name); | ||
377 | } | 378 | } |
378 | 379 | ||
379 | ld->size += i; | 380 | ld->size += i; |
@@ -621,7 +622,7 @@ static int buffer_pread(int fd, char *s, unsigned len) | |||
621 | sig_unblock(SIGCHLD); | 622 | sig_unblock(SIGCHLD); |
622 | sig_unblock(SIGALRM); | 623 | sig_unblock(SIGALRM); |
623 | sig_unblock(SIGHUP); | 624 | sig_unblock(SIGHUP); |
624 | iopause(&in, 1, &trotate, &now); | 625 | iopause(&input, 1, &trotate, &now); |
625 | sig_block(SIGTERM); | 626 | sig_block(SIGTERM); |
626 | sig_block(SIGCHLD); | 627 | sig_block(SIGCHLD); |
627 | sig_block(SIGALRM); | 628 | sig_block(SIGALRM); |
@@ -732,6 +733,8 @@ int svlogd_main(int argc, char **argv) | |||
732 | unsigned opt; | 733 | unsigned opt; |
733 | unsigned timestamp = 0; | 734 | unsigned timestamp = 0; |
734 | 735 | ||
736 | #define line bb_common_bufsiz1 | ||
737 | |||
735 | opt_complementary = "tt:vv"; | 738 | opt_complementary = "tt:vv"; |
736 | opt = getopt32(argc, argv, "r:R:l:b:tv", | 739 | opt = getopt32(argc, argv, "r:R:l:b:tv", |
737 | &r, &replace, &l, &b, ×tamp, &verbose); | 740 | &r, &replace, &l, &b, ×tamp, &verbose); |
@@ -741,38 +744,40 @@ int svlogd_main(int argc, char **argv) | |||
741 | } | 744 | } |
742 | if (opt & 2) if (!repl) repl = '_'; // -R | 745 | if (opt & 2) if (!repl) repl = '_'; // -R |
743 | if (opt & 4) { // -l | 746 | if (opt & 4) { // -l |
744 | linemax = xatou_range(l, 0, 1000); | 747 | linemax = xatou_range(l, 0, BUFSIZ-26); |
745 | if (linemax == 0) linemax = 1000; | 748 | if (linemax == 0) linemax = BUFSIZ-26; |
746 | if (linemax < 256) linemax = 256; | 749 | if (linemax < 256) linemax = 256; |
747 | } | 750 | } |
748 | if (opt & 8) { // -b | 751 | if (opt & 8) { // -b |
749 | buflen = xatoi_u(b); | 752 | ////buflen = xatoi_u(b); |
750 | if (buflen == 0) buflen = 1024; | 753 | ////if (buflen == 0) buflen = 1024; |
751 | } | 754 | } |
752 | //if (opt & 0x10) timestamp++; // -t | 755 | //if (opt & 0x10) timestamp++; // -t |
753 | //if (opt & 0x20) verbose++; // -v | 756 | //if (opt & 0x20) verbose++; // -v |
754 | if (timestamp > 2) timestamp = 2; | 757 | //if (timestamp > 2) timestamp = 2; |
755 | argv += optind; | 758 | argv += optind; |
756 | argc -= optind; | 759 | argc -= optind; |
757 | 760 | ||
758 | dirn = argc; | 761 | dirn = argc; |
759 | if (dirn <= 0) usage(); | 762 | if (dirn <= 0) usage(); |
760 | if (buflen <= linemax) usage(); | 763 | ////if (buflen <= linemax) usage(); |
761 | fdwdir = xopen(".", O_RDONLY|O_NDELAY); | 764 | fdwdir = xopen(".", O_RDONLY|O_NDELAY); |
762 | coe(fdwdir); | 765 | coe(fdwdir); |
763 | dir = xmalloc(dirn * sizeof(struct logdir)); | 766 | dir = xmalloc(dirn * sizeof(struct logdir)); |
764 | for (i = 0; i < dirn; ++i) { | 767 | for (i = 0; i < dirn; ++i) { |
765 | dir[i].fddir = -1; | 768 | dir[i].fddir = -1; |
766 | dir[i].fdcur = -1; | 769 | dir[i].fdcur = -1; |
767 | dir[i].btmp = xmalloc(buflen); | 770 | ////dir[i].btmp = xmalloc(buflen); |
768 | dir[i].ppid = 0; | 771 | dir[i].ppid = 0; |
769 | } | 772 | } |
770 | line = xmalloc(linemax + (timestamp ? 26 : 0)); | 773 | /* line = xmalloc(linemax + (timestamp ? 26 : 0)); */ |
771 | fndir = argv; | 774 | fndir = argv; |
772 | in.fd = 0; | 775 | input.fd = 0; |
773 | in.events = IOPAUSE_READ; | 776 | input.events = IOPAUSE_READ; |
774 | ndelay_on(in.fd); | 777 | /* I be damned. Linux 2.6.18: this somehow affects |
775 | 778 | * OTHER processes! Konsole starts to redraw itself much slower! | |
779 | * This persists even after svlogd exits */ | ||
780 | ndelay_on(input.fd); | ||
776 | sig_block(SIGTERM); | 781 | sig_block(SIGTERM); |
777 | sig_block(SIGCHLD); | 782 | sig_block(SIGCHLD); |
778 | sig_block(SIGALRM); | 783 | sig_block(SIGALRM); |
@@ -786,14 +791,16 @@ int svlogd_main(int argc, char **argv) | |||
786 | 791 | ||
787 | /* Each iteration processes one line */ | 792 | /* Each iteration processes one line */ |
788 | while (1) { | 793 | while (1) { |
789 | int printlen; | 794 | char stamp[FMT_PTIME]; |
790 | char *lineptr = line; | 795 | char *lineptr; |
796 | char *printptr; | ||
791 | char *np; | 797 | char *np; |
798 | int printlen; | ||
792 | char ch; | 799 | char ch; |
793 | 800 | ||
801 | lineptr = line; | ||
794 | /* Prepare timestamp if needed */ | 802 | /* Prepare timestamp if needed */ |
795 | if (timestamp) { | 803 | if (timestamp) { |
796 | char stamp[FMT_PTIME]; | ||
797 | taia_now(&now); | 804 | taia_now(&now); |
798 | switch (timestamp) { | 805 | switch (timestamp) { |
799 | case 1: | 806 | case 1: |
@@ -803,8 +810,6 @@ int svlogd_main(int argc, char **argv) | |||
803 | fmt_ptime30nul(stamp, &now); | 810 | fmt_ptime30nul(stamp, &now); |
804 | break; | 811 | break; |
805 | } | 812 | } |
806 | memcpy(line, stamp, 25); | ||
807 | line[25] = ' '; | ||
808 | lineptr += 26; | 813 | lineptr += 26; |
809 | } | 814 | } |
810 | 815 | ||
@@ -819,8 +824,8 @@ int svlogd_main(int argc, char **argv) | |||
819 | if (sz <= 0) /* EOF or error on stdin */ | 824 | if (sz <= 0) /* EOF or error on stdin */ |
820 | exitasap = 1; | 825 | exitasap = 1; |
821 | else { | 826 | else { |
827 | np = memchr(lineptr + stdin_cnt, '\n', sz); | ||
822 | stdin_cnt += sz; | 828 | stdin_cnt += sz; |
823 | np = memchr(lineptr, '\n', stdin_cnt); | ||
824 | } | 829 | } |
825 | } | 830 | } |
826 | if (stdin_cnt <= 0 && exitasap) | 831 | if (stdin_cnt <= 0 && exitasap) |
@@ -828,21 +833,33 @@ int svlogd_main(int argc, char **argv) | |||
828 | 833 | ||
829 | /* Search for '\n' (in fact, np already holds the result) */ | 834 | /* Search for '\n' (in fact, np already holds the result) */ |
830 | linelen = stdin_cnt; | 835 | linelen = stdin_cnt; |
831 | if (np) linelen = np - lineptr + 1; | 836 | if (np) { |
837 | print_to_nl: /* NB: starting from here lineptr may point | ||
838 | * farther out into line[] */ | ||
839 | linelen = np - lineptr + 1; | ||
840 | } | ||
832 | /* linelen == no of chars incl. '\n' (or == stdin_cnt) */ | 841 | /* linelen == no of chars incl. '\n' (or == stdin_cnt) */ |
833 | ch = lineptr[linelen-1]; | 842 | ch = lineptr[linelen-1]; |
834 | 843 | ||
835 | printlen = linelen + (timestamp ? 26 : 0); | 844 | /* write out lineptr[0..linelen-1] to each log destination */ |
836 | /* write out line[0..printlen-1] to each log destination */ | 845 | /* (or lineptr[-26..linelen-1] if timestamping) */ |
846 | printlen = linelen; | ||
847 | printptr = lineptr; | ||
848 | if (timestamp) { | ||
849 | printlen += 26; | ||
850 | printptr -= 26; | ||
851 | memcpy(printptr, stamp, 25); | ||
852 | printptr[25] = ' '; | ||
853 | } | ||
837 | for (i = 0; i < dirn; ++i) { | 854 | for (i = 0; i < dirn; ++i) { |
838 | struct logdir *ld = &dir[i]; | 855 | struct logdir *ld = &dir[i]; |
839 | if (ld->fddir == -1) continue; | 856 | if (ld->fddir == -1) continue; |
840 | if (ld->inst) | 857 | if (ld->inst) |
841 | logmatch(ld); | 858 | logmatch(ld); |
842 | if (ld->matcherr == 'e') | 859 | if (ld->matcherr == 'e') |
843 | full_write(2, line, printlen); | 860 | full_write(2, printptr, printlen); |
844 | if (ld->match != '+') continue; | 861 | if (ld->match != '+') continue; |
845 | buffer_pwrite(i, line, printlen); | 862 | buffer_pwrite(i, printptr, printlen); |
846 | } | 863 | } |
847 | 864 | ||
848 | /* If we didn't see '\n' (long input line), */ | 865 | /* If we didn't see '\n' (long input line), */ |
@@ -851,9 +868,9 @@ int svlogd_main(int argc, char **argv) | |||
851 | /* lineptr is emptied now, safe to use as buffer */ | 868 | /* lineptr is emptied now, safe to use as buffer */ |
852 | stdin_cnt = exitasap ? -1 : buffer_pread(0, lineptr, linemax); | 869 | stdin_cnt = exitasap ? -1 : buffer_pread(0, lineptr, linemax); |
853 | if (stdin_cnt <= 0) { /* EOF or error on stdin */ | 870 | if (stdin_cnt <= 0) { /* EOF or error on stdin */ |
871 | exitasap = 1; | ||
854 | lineptr[0] = ch = '\n'; | 872 | lineptr[0] = ch = '\n'; |
855 | linelen = 1; | 873 | linelen = 1; |
856 | exitasap = 1; | ||
857 | stdin_cnt = 1; | 874 | stdin_cnt = 1; |
858 | } else { | 875 | } else { |
859 | linelen = stdin_cnt; | 876 | linelen = stdin_cnt; |
@@ -871,10 +888,17 @@ int svlogd_main(int argc, char **argv) | |||
871 | } | 888 | } |
872 | } | 889 | } |
873 | 890 | ||
874 | /* Move unprocessed data to the front of line */ | ||
875 | stdin_cnt -= linelen; | 891 | stdin_cnt -= linelen; |
876 | if (stdin_cnt > 0) /* TODO: slow if buffer is big */ | 892 | if (stdin_cnt > 0) { |
877 | memmove(lineptr, &lineptr[linelen], stdin_cnt); | 893 | lineptr += linelen; |
894 | /* If we see another '\n', we don't need to read | ||
895 | * next piece of input: can print what we have */ | ||
896 | np = memchr(lineptr, '\n', stdin_cnt); | ||
897 | if (np) | ||
898 | goto print_to_nl; | ||
899 | /* Move unprocessed data to the front of line */ | ||
900 | memmove((timestamp ? line+26 : line), lineptr, stdin_cnt); | ||
901 | } | ||
878 | } | 902 | } |
879 | 903 | ||
880 | for (i = 0; i < dirn; ++i) { | 904 | for (i = 0; i < dirn; ++i) { |