blob: 1fd19d77290966acf614ffaffff364bb502675b3 [file] [log] [blame]
Willy Tarreau214c2032009-02-20 11:02:32 +01001/*
2 * fast fgets() replacement for log parsing
3 *
4 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 * This function manages its own buffer and returns a pointer to that buffer
12 * in order to avoid expensive memory copies. It also checks for line breaks
13 * 32 bits at a time. It could be improved a lot using mmap() but we would
14 * not be allowed to replace trailing \n with zeroes and we would be limited
15 * to small log files on 32-bit machines.
16 *
17 */
18
19#include <stdlib.h>
20#include <string.h>
21#include <stdio.h>
22#include <unistd.h>
23
Willy Tarreau96c148b2011-09-09 08:21:55 +020024// return non-zero if the integer contains at least one zero byte
Willy Tarreau214c2032009-02-20 11:02:32 +010025static inline unsigned int has_zero(unsigned int x)
26{
Willy Tarreau1769a182010-05-04 10:47:57 +020027 unsigned int y;
28
29 /* Principle: we want to perform 4 tests on one 32-bit int at once. For
30 * this, we have to simulate an SIMD instruction which we don't have by
31 * default. The principle is that a zero byte is the only one which
32 * will cause a 1 to appear on the upper bit of a byte/word/etc... when
33 * we subtract 1. So we can detect a zero byte if a one appears at any
34 * of the bits 7, 15, 23 or 31 where it was not. It takes only one
35 * instruction to test for the presence of any of these bits, but it is
36 * still complex to check for their initial absence. Thus, we'll
37 * proceed differently : we first save and clear only those bits, then
38 * we check in the final result if one of them is present and was not.
39 */
40 y = x;
Willy Tarreau1769a182010-05-04 10:47:57 +020041 y -= 0x01010101; /* generate a carry */
Willy Tarreau96c148b2011-09-09 08:21:55 +020042 y &= ~x; /* clear the bits that were already set */
43 return y & 0x80808080;
Willy Tarreau214c2032009-02-20 11:02:32 +010044}
45
46
47#define FGETS2_BUFSIZE (256*1024)
48const char *fgets2(FILE *stream)
49{
Willy Tarreau31a02e92011-09-10 10:25:05 +020050 static char buffer[FGETS2_BUFSIZE + 32];
Willy Tarreau214c2032009-02-20 11:02:32 +010051 static char *end = buffer;
52 static char *line = buffer;
53
54 char *next;
55 int ret;
56
57 next = line;
58
59 while (1) {
60 /* this is a speed-up, we read 32 bits at once and check for an
61 * LF character there. We stop if found then continue one at a
62 * time.
63 */
64 while (next < end && (((unsigned long)next) & 3) && *next != '\n')
65 next++;
66
Willy Tarreau31a02e92011-09-10 10:25:05 +020067 /* Now next is multiple of 4 or equal to end. We know we can safely
68 * read up to 32 bytes past end if needed because they're allocated.
69 */
70 while (next < end) {
Willy Tarreau214c2032009-02-20 11:02:32 +010071 if (has_zero(*(unsigned int *)next ^ 0x0A0A0A0A))
72 break;
73 next += 4;
74 if (has_zero(*(unsigned int *)next ^ 0x0A0A0A0A))
75 break;
76 next += 4;
77 if (has_zero(*(unsigned int *)next ^ 0x0A0A0A0A))
78 break;
79 next += 4;
80 if (has_zero(*(unsigned int *)next ^ 0x0A0A0A0A))
81 break;
82 next += 4;
83 if (has_zero(*(unsigned int *)next ^ 0x0A0A0A0A))
84 break;
85 next += 4;
86 if (has_zero(*(unsigned int *)next ^ 0x0A0A0A0A))
87 break;
88 next += 4;
89 if (has_zero(*(unsigned int *)next ^ 0x0A0A0A0A))
90 break;
91 next += 4;
92 if (has_zero(*(unsigned int *)next ^ 0x0A0A0A0A))
93 break;
94 next += 4;
95 }
96
Willy Tarreau31a02e92011-09-10 10:25:05 +020097 /* We finish if needed : if <next> is below <end>, it means we
98 * found an LF in one of the 4 following bytes.
Willy Tarreau214c2032009-02-20 11:02:32 +010099 */
100 while (next < end) {
101 if (*next == '\n') {
102 const char *start = line;
103
104 *next = '\0';
105 line = next + 1;
106 return start;
107 }
108 next++;
109 }
110
111 /* we found an incomplete line. First, let's move the
112 * remaining part of the buffer to the beginning, then
Willy Tarreau31a02e92011-09-10 10:25:05 +0200113 * try to complete the buffer with a new read. We can't
114 * rely on <next> anymore because it went past <end>.
Willy Tarreau214c2032009-02-20 11:02:32 +0100115 */
116 if (line > buffer) {
117 if (end != line)
118 memmove(buffer, line, end - line);
119 end = buffer + (end - line);
120 next = end;
121 line = buffer;
122 } else {
123 if (end == buffer + FGETS2_BUFSIZE)
124 return NULL;
125 }
126
127 ret = read(fileno(stream), end, buffer + FGETS2_BUFSIZE - end);
128
129 if (ret <= 0) {
130 if (end == line)
131 return NULL;
132
133 *end = '\0';
Willy Tarreau812e7a72011-07-09 14:28:01 +0200134 end = line; /* ensure we stop next time */
Willy Tarreau214c2032009-02-20 11:02:32 +0100135 return line;
136 }
137
138 end += ret;
Willy Tarreau31a02e92011-09-10 10:25:05 +0200139 *end = '\n'; /* make parser stop ASAP */
Willy Tarreau214c2032009-02-20 11:02:32 +0100140 /* search for '\n' again */
141 }
142}
143
144#ifdef BENCHMARK
145int main() {
146 const char *p;
147 unsigned int lines = 0;
148
149 while ((p=fgets2(stdin)))
150 lines++;
151 printf("lines=%d\n", lines);
152 return 0;
153}
154#endif