Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | * fast fgets() replacement for log parsing |
| 3 | * |
Willy Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame^] | 4 | * Copyright 2000-2012 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 5 | * |
Willy Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame^] | 6 | * 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 Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 19 | * |
| 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 Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame^] | 22 | * 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 Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 25 | * |
| 26 | */ |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <stdio.h> |
| 31 | #include <unistd.h> |
| 32 | |
Willy Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame^] | 33 | #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 */ |
| 38 | static inline unsigned int has_zero32(unsigned int x) |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 39 | { |
Willy Tarreau | 1769a18 | 2010-05-04 10:47:57 +0200 | [diff] [blame] | 40 | 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 Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame^] | 52 | * 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 Tarreau | 1769a18 | 2010-05-04 10:47:57 +0200 | [diff] [blame] | 55 | */ |
Willy Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame^] | 56 | #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 Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Willy Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame^] | 74 | /* return non-zero if the argument contains at least one zero byte. See principle above. */ |
| 75 | static inline unsigned long long has_zero64(unsigned long long x) |
| 76 | { |
| 77 | unsigned long long y; |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 78 | |
Willy Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame^] | 79 | y = x - 0x0101010101010101ULL; /* generate a carry */ |
| 80 | y &= ~x; /* clear the bits that were already set */ |
| 81 | return y & 0x8080808080808080ULL; |
| 82 | } |
| 83 | |
| 84 | static inline unsigned long has_zero(unsigned long x) |
| 85 | { |
| 86 | return (sizeof(x) == 8) ? has_zero64(x) : has_zero32(x); |
| 87 | } |
| 88 | |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 89 | const char *fgets2(FILE *stream) |
| 90 | { |
Willy Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame^] | 91 | static char buffer[FGETS2_BUFSIZE + 68]; /* Note: +32 is enough on 32-bit systems */ |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 92 | static char *end = buffer; |
| 93 | static char *line = buffer; |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 94 | char *next; |
| 95 | int ret; |
| 96 | |
| 97 | next = line; |
| 98 | |
| 99 | while (1) { |
Willy Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame^] | 100 | if (sizeof(long) == 4) { /* 32-bit system */ |
| 101 | /* this is a speed-up, we read 32 bits at once and check for an |
| 102 | * LF character there. We stop if found then continue one at a |
| 103 | * time. |
| 104 | */ |
| 105 | while (next < end && (((unsigned long)next) & 3) && *next != '\n') |
| 106 | next++; |
| 107 | |
| 108 | /* Now next is multiple of 4 or equal to end. We know we can safely |
| 109 | * read up to 32 bytes past end if needed because they're allocated. |
| 110 | */ |
| 111 | while (next < end) { |
| 112 | if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 113 | break; |
| 114 | next += 4; |
| 115 | if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 116 | break; |
| 117 | next += 4; |
| 118 | if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 119 | break; |
| 120 | next += 4; |
| 121 | if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 122 | break; |
| 123 | next += 4; |
| 124 | if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 125 | break; |
| 126 | next += 4; |
| 127 | if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 128 | break; |
| 129 | next += 4; |
| 130 | if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 131 | break; |
| 132 | next += 4; |
| 133 | if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 134 | break; |
| 135 | next += 4; |
| 136 | } |
| 137 | } |
| 138 | else { /* 64-bit system */ |
| 139 | /* this is a speed-up, we read 64 bits at once and check for an |
| 140 | * LF character there. We stop if found then continue one at a |
| 141 | * time. |
| 142 | */ |
| 143 | if (next <= end) { |
| 144 | /* max 3 bytes tested here */ |
| 145 | while ((((unsigned long)next) & 3) && *next != '\n') |
| 146 | next++; |
| 147 | |
| 148 | /* maybe we have can skip 4 more bytes */ |
| 149 | if ((((unsigned long)next) & 4) && !has_zero32(*(unsigned int *)next ^ 0x0A0A0A0AU)) |
| 150 | next += 4; |
| 151 | } |
| 152 | |
| 153 | /* now next is multiple of 8 or equal to end */ |
| 154 | while (next <= (end-68)) { |
| 155 | if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL)) |
| 156 | break; |
| 157 | next += 8; |
| 158 | if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL)) |
| 159 | break; |
| 160 | next += 8; |
| 161 | if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL)) |
| 162 | break; |
| 163 | next += 8; |
| 164 | if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL)) |
| 165 | break; |
| 166 | next += 8; |
| 167 | if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL)) |
| 168 | break; |
| 169 | next += 8; |
| 170 | if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL)) |
| 171 | break; |
| 172 | next += 8; |
| 173 | if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL)) |
| 174 | break; |
| 175 | next += 8; |
| 176 | if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL)) |
| 177 | break; |
| 178 | next += 8; |
| 179 | } |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 180 | |
Willy Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame^] | 181 | /* maybe we can skip 4 more bytes */ |
| 182 | if (!has_zero32(*(unsigned int *)next ^ 0x0A0A0A0AU)) |
| 183 | next += 4; |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Willy Tarreau | 31a02e9 | 2011-09-10 10:25:05 +0200 | [diff] [blame] | 186 | /* We finish if needed : if <next> is below <end>, it means we |
| 187 | * found an LF in one of the 4 following bytes. |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 188 | */ |
| 189 | while (next < end) { |
| 190 | if (*next == '\n') { |
| 191 | const char *start = line; |
| 192 | |
| 193 | *next = '\0'; |
| 194 | line = next + 1; |
| 195 | return start; |
| 196 | } |
| 197 | next++; |
| 198 | } |
| 199 | |
| 200 | /* we found an incomplete line. First, let's move the |
| 201 | * remaining part of the buffer to the beginning, then |
Willy Tarreau | 31a02e9 | 2011-09-10 10:25:05 +0200 | [diff] [blame] | 202 | * try to complete the buffer with a new read. We can't |
| 203 | * rely on <next> anymore because it went past <end>. |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 204 | */ |
| 205 | if (line > buffer) { |
| 206 | if (end != line) |
| 207 | memmove(buffer, line, end - line); |
| 208 | end = buffer + (end - line); |
| 209 | next = end; |
| 210 | line = buffer; |
| 211 | } else { |
| 212 | if (end == buffer + FGETS2_BUFSIZE) |
| 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | ret = read(fileno(stream), end, buffer + FGETS2_BUFSIZE - end); |
| 217 | |
| 218 | if (ret <= 0) { |
| 219 | if (end == line) |
| 220 | return NULL; |
| 221 | |
| 222 | *end = '\0'; |
Willy Tarreau | 812e7a7 | 2011-07-09 14:28:01 +0200 | [diff] [blame] | 223 | end = line; /* ensure we stop next time */ |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 224 | return line; |
| 225 | } |
| 226 | |
| 227 | end += ret; |
Willy Tarreau | 31a02e9 | 2011-09-10 10:25:05 +0200 | [diff] [blame] | 228 | *end = '\n'; /* make parser stop ASAP */ |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 229 | /* search for '\n' again */ |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | #ifdef BENCHMARK |
| 234 | int main() { |
| 235 | const char *p; |
| 236 | unsigned int lines = 0; |
| 237 | |
| 238 | while ((p=fgets2(stdin))) |
| 239 | lines++; |
| 240 | printf("lines=%d\n", lines); |
| 241 | return 0; |
| 242 | } |
| 243 | #endif |