blob: 7556a31c2d26bbda50d341cde24c42eed57c0fc1 [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 Tarreau96c148b2011-09-09 08:21:55 +020041 y -= 0x01010101; /* generate a carry */
42 y &= ~x; /* clear the bits that were already set */
43 return y & 0x80808080;
Willy Tarreau214c2032009-02-20 11:02:32 +010044}
45
Willy Tarreau1769a182010-05-04 10:47:57 +020046
Willy Tarreau96c148b2011-09-09 08:21:55 +020047// return non-zero if the argument contains at least one zero byte. See principle above.
48static inline unsigned long long has_zero64(unsigned long long x)
Willy Tarreau214c2032009-02-20 11:02:32 +010049{
Willy Tarreau1769a182010-05-04 10:47:57 +020050 unsigned long long y;
Willy Tarreau214c2032009-02-20 11:02:32 +010051
Willy Tarreau1769a182010-05-04 10:47:57 +020052 y = x;
Willy Tarreau1769a182010-05-04 10:47:57 +020053 y -= 0x0101010101010101ULL; /* generate a carry */
Willy Tarreau96c148b2011-09-09 08:21:55 +020054 y &= ~x; /* clear the bits that were already set */
55 return y & 0x8080808080808080ULL;
Willy Tarreau214c2032009-02-20 11:02:32 +010056}
57
58#define FGETS2_BUFSIZE (256*1024)
59const char *fgets2(FILE *stream)
60{
Willy Tarreaud2c142c2010-05-05 12:22:08 +020061 static char buffer[FGETS2_BUFSIZE + 9]; // +9 to have zeroes past the end
Willy Tarreau214c2032009-02-20 11:02:32 +010062 static char *end = buffer;
63 static char *line = buffer;
64
65 char *next;
66 int ret;
67
68 next = line;
69
70 while (1) {
Willy Tarreaud2c142c2010-05-05 12:22:08 +020071 /* this is a speed-up, we read 64 bits at once and check for an
Willy Tarreau214c2032009-02-20 11:02:32 +010072 * LF character there. We stop if found then continue one at a
73 * time.
74 */
Willy Tarreau214c2032009-02-20 11:02:32 +010075
Willy Tarreaud2c142c2010-05-05 12:22:08 +020076 if (next <= (end-12)) {
77 /* max 3 bytes tested here */
78 while ((((unsigned long)next) & 3) && *next != '\n')
79 next++;
80
81 /* maybe we have can skip 4 more bytes */
82 if ((((unsigned long)next) & 4) && !has_zero(*(unsigned int *)next ^ 0x0A0A0A0AU))
83 next += 4;
84 }
85
86 /* now next is multiple of 8 or equal to end */
87 while (next <= (end-68)) {
88 if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL))
89 break;
90 next += 8;
91 if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL))
92 break;
93 next += 8;
94 if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL))
95 break;
96 next += 8;
Willy Tarreau214c2032009-02-20 11:02:32 +010097 if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL))
98 break;
99 next += 8;
100 if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL))
101 break;
102 next += 8;
103 if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL))
104 break;
105 next += 8;
106 if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL))
107 break;
108 next += 8;
Willy Tarreaud2c142c2010-05-05 12:22:08 +0200109 if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL))
110 break;
111 next += 8;
Willy Tarreau214c2032009-02-20 11:02:32 +0100112 }
113
Willy Tarreaud2c142c2010-05-05 12:22:08 +0200114 /* maybe we can skip 4 more bytes */
115 if (!has_zero(*(unsigned int *)next ^ 0x0A0A0A0AU))
116 next += 4;
117
Willy Tarreau214c2032009-02-20 11:02:32 +0100118 /* we finish if needed. Note that next might be slightly higher
119 * than end here because we might have gone past it above.
120 */
121 while (next < end) {
122 if (*next == '\n') {
123 const char *start = line;
124
125 *next = '\0';
126 line = next + 1;
127 return start;
128 }
129 next++;
130 }
131
132 /* we found an incomplete line. First, let's move the
133 * remaining part of the buffer to the beginning, then
134 * try to complete the buffer with a new read.
135 */
136 if (line > buffer) {
137 if (end != line)
138 memmove(buffer, line, end - line);
139 end = buffer + (end - line);
140 next = end;
141 line = buffer;
142 } else {
143 if (end == buffer + FGETS2_BUFSIZE)
144 return NULL;
145 }
146
147 ret = read(fileno(stream), end, buffer + FGETS2_BUFSIZE - end);
148
149 if (ret <= 0) {
150 if (end == line)
151 return NULL;
152
153 *end = '\0';
Willy Tarreau812e7a72011-07-09 14:28:01 +0200154 end = line; /* ensure we stop next time */
Willy Tarreau214c2032009-02-20 11:02:32 +0100155 return line;
156 }
157
158 end += ret;
159 /* search for '\n' again */
160 }
161}
162
163#ifdef BENCHMARK
164int main() {
165 const char *p;
166 unsigned int lines = 0;
167
168 while ((p=fgets2(stdin)))
169 lines++;
170 printf("lines=%d\n", lines);
171 return 0;
172}
173#endif