blob: 1be6f227d95c98094a5de8bbbc390480a4d707dc [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 Tarreau31a02e92011-09-10 10:25:05 +020061 static char buffer[FGETS2_BUFSIZE + 68];
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 Tarreau31a02e92011-09-10 10:25:05 +020076 if (next <= end) {
Willy Tarreaud2c142c2010-05-05 12:22:08 +020077 /* 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 Tarreau31a02e92011-09-10 10:25:05 +0200118 /* We finish if needed : if <next> is below <end>, it means we
119 * found an LF in one of the 4 following bytes.
Willy Tarreau214c2032009-02-20 11:02:32 +0100120 */
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
Willy Tarreau31a02e92011-09-10 10:25:05 +0200134 * try to complete the buffer with a new read. We can't
135 * rely on <next> anymore because it went past <end>.
Willy Tarreau214c2032009-02-20 11:02:32 +0100136 */
137 if (line > buffer) {
138 if (end != line)
139 memmove(buffer, line, end - line);
140 end = buffer + (end - line);
141 next = end;
142 line = buffer;
143 } else {
144 if (end == buffer + FGETS2_BUFSIZE)
145 return NULL;
146 }
147
148 ret = read(fileno(stream), end, buffer + FGETS2_BUFSIZE - end);
149
150 if (ret <= 0) {
151 if (end == line)
152 return NULL;
153
154 *end = '\0';
Willy Tarreau812e7a72011-07-09 14:28:01 +0200155 end = line; /* ensure we stop next time */
Willy Tarreau214c2032009-02-20 11:02:32 +0100156 return line;
157 }
158
159 end += ret;
Willy Tarreau31a02e92011-09-10 10:25:05 +0200160 *end = '\n'; /* make parser stop ASAP */
Willy Tarreau214c2032009-02-20 11:02:32 +0100161 /* search for '\n' again */
162 }
163}
164
165#ifdef BENCHMARK
166int main() {
167 const char *p;
168 unsigned int lines = 0;
169
170 while ((p=fgets2(stdin)))
171 lines++;
172 printf("lines=%d\n", lines);
173 return 0;
174}
175#endif