blob: 776a91538eb71486d78bf3e4f585dd064d3859f1 [file] [log] [blame]
Willy Tarreau214c2032009-02-20 11:02:32 +01001/*
2 * fast fgets() replacement for log parsing
3 *
Willy Tarreaude5dc052012-06-09 09:44:03 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Willy Tarreau214c2032009-02-20 11:02:32 +01005 *
Willy Tarreaude5dc052012-06-09 09:44:03 +02006 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation, version 2.1
9 * exclusively.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Willy Tarreau214c2032009-02-20 11:02:32 +010019 *
20 * This function manages its own buffer and returns a pointer to that buffer
21 * in order to avoid expensive memory copies. It also checks for line breaks
Willy Tarreaude5dc052012-06-09 09:44:03 +020022 * 32 or 64 bits at a time. It could be improved a lot using mmap() but we
23 * would not be allowed to replace trailing \n with zeroes and we would be
24 * limited to small log files on 32-bit machines.
Willy Tarreau214c2032009-02-20 11:02:32 +010025 *
26 */
27
28#include <stdlib.h>
29#include <string.h>
30#include <stdio.h>
31#include <unistd.h>
32
Willy Tarreaude5dc052012-06-09 09:44:03 +020033#ifndef FGETS2_BUFSIZE
34#define FGETS2_BUFSIZE (256*1024)
35#endif
36
37/* return non-zero if the integer contains at least one zero byte */
Willy Tarreau9080ac02020-12-21 08:35:24 +010038static inline __attribute__((unused)) unsigned int has_zero32(unsigned int x)
Willy Tarreau214c2032009-02-20 11:02:32 +010039{
Willy Tarreau1769a182010-05-04 10:47:57 +020040 unsigned int y;
41
42 /* Principle: we want to perform 4 tests on one 32-bit int at once. For
43 * this, we have to simulate an SIMD instruction which we don't have by
44 * default. The principle is that a zero byte is the only one which
45 * will cause a 1 to appear on the upper bit of a byte/word/etc... when
46 * we subtract 1. So we can detect a zero byte if a one appears at any
47 * of the bits 7, 15, 23 or 31 where it was not. It takes only one
48 * instruction to test for the presence of any of these bits, but it is
49 * still complex to check for their initial absence. Thus, we'll
50 * proceed differently : we first save and clear only those bits, then
51 * we check in the final result if one of them is present and was not.
Willy Tarreaude5dc052012-06-09 09:44:03 +020052 * The order of operations below is important to save registers and
53 * tests. The result is used as a boolean, so the last test must apply
54 * on the constant so that it can efficiently be inlined.
Willy Tarreau1769a182010-05-04 10:47:57 +020055 */
Willy Tarreaude5dc052012-06-09 09:44:03 +020056#if defined(__i386__)
57 /* gcc on x86 loves copying registers over and over even on code that
58 * simple, so let's do it by hand to prevent it from doing so :-(
59 */
60 asm("lea -0x01010101(%0),%1\n"
61 "not %0\n"
62 "and %1,%0\n"
63 : "=a" (x), "=r"(y)
64 : "0" (x)
65 );
66 return x & 0x80808080;
67#else
68 y = x - 0x01010101; /* generate a carry */
69 x = ~x & y; /* clear the bits that were already set */
70 return x & 0x80808080;
71#endif
Willy Tarreau214c2032009-02-20 11:02:32 +010072}
73
Willy Tarreaude5dc052012-06-09 09:44:03 +020074/* return non-zero if the argument contains at least one zero byte. See principle above. */
Willy Tarreau9080ac02020-12-21 08:35:24 +010075static inline __attribute__((unused)) unsigned long long has_zero64(unsigned long long x)
Willy Tarreaude5dc052012-06-09 09:44:03 +020076{
77 unsigned long long y;
Willy Tarreau214c2032009-02-20 11:02:32 +010078
Willy Tarreaude5dc052012-06-09 09:44:03 +020079 y = x - 0x0101010101010101ULL; /* generate a carry */
80 y &= ~x; /* clear the bits that were already set */
81 return y & 0x8080808080808080ULL;
82}
83
Willy Tarreau9080ac02020-12-21 08:35:24 +010084static inline __attribute__((unused)) unsigned long has_zero(unsigned long x)
Willy Tarreaude5dc052012-06-09 09:44:03 +020085{
86 return (sizeof(x) == 8) ? has_zero64(x) : has_zero32(x);
87}
88
Willy Tarreau419a5982012-06-12 08:52:22 +020089/* find a '\n' between <next> and <end>. Warning: may read slightly past <end>.
90 * If no '\n' is found, <end> is returned.
91 */
92static char *find_lf(char *next, char *end)
Willy Tarreau214c2032009-02-20 11:02:32 +010093{
Willy Tarreau419a5982012-06-12 08:52:22 +020094#if defined USE_MEMCHR
95 /* some recent libc use platform-specific optimizations to provide more
96 * efficient byte search than below (eg: glibc 2.11 on x86_64).
97 */
98 next = memchr(next, '\n', end - next);
99 if (!next)
100 next = end;
101#else
102 if (sizeof(long) == 4) { /* 32-bit system */
103 /* this is a speed-up, we read 32 bits at once and check for an
104 * LF character there. We stop if found then continue one at a
105 * time.
106 */
107 while (next < end && (((unsigned long)next) & 3) && *next != '\n')
108 next++;
Willy Tarreau214c2032009-02-20 11:02:32 +0100109
Willy Tarreau419a5982012-06-12 08:52:22 +0200110 /* Now next is multiple of 4 or equal to end. We know we can safely
111 * read up to 32 bytes past end if needed because they're allocated.
112 */
113 while (next < end) {
114 if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A))
115 break;
116 next += 4;
117 if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A))
118 break;
119 next += 4;
120 if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A))
121 break;
122 next += 4;
123 if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A))
124 break;
125 next += 4;
126 if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A))
127 break;
128 next += 4;
129 if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A))
130 break;
131 next += 4;
132 if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A))
133 break;
134 next += 4;
135 if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A))
136 break;
137 next += 4;
138 }
139 }
140 else { /* 64-bit system */
141 /* this is a speed-up, we read 64 bits at once and check for an
142 * LF character there. We stop if found then continue one at a
143 * time.
144 */
145 if (next <= end) {
146 /* max 3 bytes tested here */
147 while ((((unsigned long)next) & 3) && *next != '\n')
Willy Tarreaude5dc052012-06-09 09:44:03 +0200148 next++;
149
Willy Tarreau419a5982012-06-12 08:52:22 +0200150 /* maybe we have can skip 4 more bytes */
151 if ((((unsigned long)next) & 4) && !has_zero32(*(unsigned int *)next ^ 0x0A0A0A0AU))
Willy Tarreaude5dc052012-06-09 09:44:03 +0200152 next += 4;
Willy Tarreaude5dc052012-06-09 09:44:03 +0200153 }
Willy Tarreaude5dc052012-06-09 09:44:03 +0200154
Willy Tarreau419a5982012-06-12 08:52:22 +0200155 /* now next is multiple of 8 or equal to end */
156 while (next <= (end-68)) {
157 if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL))
158 break;
159 next += 8;
160 if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL))
161 break;
162 next += 8;
163 if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL))
164 break;
165 next += 8;
166 if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL))
167 break;
168 next += 8;
169 if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL))
170 break;
171 next += 8;
172 if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL))
173 break;
174 next += 8;
175 if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL))
176 break;
177 next += 8;
178 if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL))
179 break;
180 next += 8;
181 }
Willy Tarreaude5dc052012-06-09 09:44:03 +0200182
Willy Tarreau419a5982012-06-12 08:52:22 +0200183 /* maybe we can skip 4 more bytes */
184 if (!has_zero32(*(unsigned int *)next ^ 0x0A0A0A0AU))
185 next += 4;
186 }
Willy Tarreau214c2032009-02-20 11:02:32 +0100187
Willy Tarreau419a5982012-06-12 08:52:22 +0200188 /* We finish if needed : if <next> is below <end>, it means we
189 * found an LF in one of the 4 following bytes.
190 */
191 while (next < end) {
192 if (*next == '\n')
193 break;
194 next++;
195 }
196#endif
197 return next;
198}
Willy Tarreau214c2032009-02-20 11:02:32 +0100199
Willy Tarreau419a5982012-06-12 08:52:22 +0200200const char *fgets2(FILE *stream)
201{
202 static char buffer[FGETS2_BUFSIZE + 68]; /* Note: +32 is enough on 32-bit systems */
203 static char *end = buffer;
204 static char *line = buffer;
205 char *next;
206 int ret;
Willy Tarreau214c2032009-02-20 11:02:32 +0100207
Willy Tarreau419a5982012-06-12 08:52:22 +0200208 next = line;
209
210 while (1) {
211 next = find_lf(next, end);
212 if (next < end) {
213 const char *start = line;
214 *next = '\0';
215 line = next + 1;
216 return start;
Willy Tarreau214c2032009-02-20 11:02:32 +0100217 }
218
219 /* we found an incomplete line. First, let's move the
220 * remaining part of the buffer to the beginning, then
Willy Tarreau31a02e92011-09-10 10:25:05 +0200221 * try to complete the buffer with a new read. We can't
222 * rely on <next> anymore because it went past <end>.
Willy Tarreau214c2032009-02-20 11:02:32 +0100223 */
224 if (line > buffer) {
225 if (end != line)
226 memmove(buffer, line, end - line);
227 end = buffer + (end - line);
228 next = end;
229 line = buffer;
230 } else {
231 if (end == buffer + FGETS2_BUFSIZE)
232 return NULL;
233 }
234
235 ret = read(fileno(stream), end, buffer + FGETS2_BUFSIZE - end);
236
237 if (ret <= 0) {
238 if (end == line)
239 return NULL;
240
241 *end = '\0';
Willy Tarreau812e7a72011-07-09 14:28:01 +0200242 end = line; /* ensure we stop next time */
Willy Tarreau214c2032009-02-20 11:02:32 +0100243 return line;
244 }
245
246 end += ret;
Willy Tarreau31a02e92011-09-10 10:25:05 +0200247 *end = '\n'; /* make parser stop ASAP */
Willy Tarreau214c2032009-02-20 11:02:32 +0100248 /* search for '\n' again */
249 }
250}
251
252#ifdef BENCHMARK
253int main() {
254 const char *p;
255 unsigned int lines = 0;
256
257 while ((p=fgets2(stdin)))
258 lines++;
259 printf("lines=%d\n", lines);
260 return 0;
261}
262#endif