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 | |
Willy Tarreau | c4710e1 | 2021-04-02 14:57:42 +0200 | [diff] [blame] | 37 | /* memchr() is faster in glibc with SSE since commit 093ecf92998de2 */ |
| 38 | #if defined(__x86_64__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 15)) |
| 39 | #define USE_MEMCHR |
| 40 | #endif |
| 41 | |
Willy Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame] | 42 | /* return non-zero if the integer contains at least one zero byte */ |
Willy Tarreau | f531dff | 2020-12-21 08:35:24 +0100 | [diff] [blame] | 43 | static inline __attribute__((unused)) unsigned int has_zero32(unsigned int x) |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 44 | { |
Willy Tarreau | 1769a18 | 2010-05-04 10:47:57 +0200 | [diff] [blame] | 45 | unsigned int y; |
| 46 | |
| 47 | /* Principle: we want to perform 4 tests on one 32-bit int at once. For |
| 48 | * this, we have to simulate an SIMD instruction which we don't have by |
| 49 | * default. The principle is that a zero byte is the only one which |
| 50 | * will cause a 1 to appear on the upper bit of a byte/word/etc... when |
| 51 | * we subtract 1. So we can detect a zero byte if a one appears at any |
| 52 | * of the bits 7, 15, 23 or 31 where it was not. It takes only one |
| 53 | * instruction to test for the presence of any of these bits, but it is |
| 54 | * still complex to check for their initial absence. Thus, we'll |
| 55 | * proceed differently : we first save and clear only those bits, then |
| 56 | * 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] | 57 | * The order of operations below is important to save registers and |
| 58 | * tests. The result is used as a boolean, so the last test must apply |
| 59 | * on the constant so that it can efficiently be inlined. |
Willy Tarreau | 1769a18 | 2010-05-04 10:47:57 +0200 | [diff] [blame] | 60 | */ |
Willy Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame] | 61 | #if defined(__i386__) |
| 62 | /* gcc on x86 loves copying registers over and over even on code that |
| 63 | * simple, so let's do it by hand to prevent it from doing so :-( |
| 64 | */ |
| 65 | asm("lea -0x01010101(%0),%1\n" |
| 66 | "not %0\n" |
| 67 | "and %1,%0\n" |
| 68 | : "=a" (x), "=r"(y) |
| 69 | : "0" (x) |
| 70 | ); |
| 71 | return x & 0x80808080; |
| 72 | #else |
| 73 | y = x - 0x01010101; /* generate a carry */ |
| 74 | x = ~x & y; /* clear the bits that were already set */ |
| 75 | return x & 0x80808080; |
| 76 | #endif |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Willy Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame] | 79 | /* return non-zero if the argument contains at least one zero byte. See principle above. */ |
Willy Tarreau | f531dff | 2020-12-21 08:35:24 +0100 | [diff] [blame] | 80 | static inline __attribute__((unused)) unsigned long long has_zero64(unsigned long long x) |
Willy Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame] | 81 | { |
| 82 | unsigned long long y; |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 83 | |
Willy Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame] | 84 | y = x - 0x0101010101010101ULL; /* generate a carry */ |
| 85 | y &= ~x; /* clear the bits that were already set */ |
| 86 | return y & 0x8080808080808080ULL; |
| 87 | } |
| 88 | |
Willy Tarreau | f531dff | 2020-12-21 08:35:24 +0100 | [diff] [blame] | 89 | static inline __attribute__((unused)) unsigned long has_zero(unsigned long x) |
Willy Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame] | 90 | { |
| 91 | return (sizeof(x) == 8) ? has_zero64(x) : has_zero32(x); |
| 92 | } |
| 93 | |
Willy Tarreau | 419a598 | 2012-06-12 08:52:22 +0200 | [diff] [blame] | 94 | /* find a '\n' between <next> and <end>. Warning: may read slightly past <end>. |
| 95 | * If no '\n' is found, <end> is returned. |
| 96 | */ |
| 97 | static char *find_lf(char *next, char *end) |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 98 | { |
Willy Tarreau | 419a598 | 2012-06-12 08:52:22 +0200 | [diff] [blame] | 99 | #if defined USE_MEMCHR |
| 100 | /* some recent libc use platform-specific optimizations to provide more |
| 101 | * efficient byte search than below (eg: glibc 2.11 on x86_64). |
| 102 | */ |
| 103 | next = memchr(next, '\n', end - next); |
| 104 | if (!next) |
| 105 | next = end; |
| 106 | #else |
| 107 | if (sizeof(long) == 4) { /* 32-bit system */ |
| 108 | /* this is a speed-up, we read 32 bits at once and check for an |
| 109 | * LF character there. We stop if found then continue one at a |
| 110 | * time. |
| 111 | */ |
| 112 | while (next < end && (((unsigned long)next) & 3) && *next != '\n') |
| 113 | next++; |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 114 | |
Willy Tarreau | 419a598 | 2012-06-12 08:52:22 +0200 | [diff] [blame] | 115 | /* Now next is multiple of 4 or equal to end. We know we can safely |
| 116 | * read up to 32 bytes past end if needed because they're allocated. |
| 117 | */ |
| 118 | while (next < end) { |
| 119 | if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 120 | break; |
| 121 | next += 4; |
| 122 | if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 123 | break; |
| 124 | next += 4; |
| 125 | if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 126 | break; |
| 127 | next += 4; |
| 128 | if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 129 | break; |
| 130 | next += 4; |
| 131 | if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 132 | break; |
| 133 | next += 4; |
| 134 | if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 135 | break; |
| 136 | next += 4; |
| 137 | if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 138 | break; |
| 139 | next += 4; |
| 140 | if (has_zero32(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 141 | break; |
| 142 | next += 4; |
| 143 | } |
| 144 | } |
| 145 | else { /* 64-bit system */ |
| 146 | /* this is a speed-up, we read 64 bits at once and check for an |
| 147 | * LF character there. We stop if found then continue one at a |
| 148 | * time. |
| 149 | */ |
| 150 | if (next <= end) { |
| 151 | /* max 3 bytes tested here */ |
| 152 | while ((((unsigned long)next) & 3) && *next != '\n') |
Willy Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame] | 153 | next++; |
| 154 | |
Willy Tarreau | 419a598 | 2012-06-12 08:52:22 +0200 | [diff] [blame] | 155 | /* maybe we have can skip 4 more bytes */ |
| 156 | if ((((unsigned long)next) & 4) && !has_zero32(*(unsigned int *)next ^ 0x0A0A0A0AU)) |
Willy Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame] | 157 | next += 4; |
Willy Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame] | 158 | } |
Willy Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame] | 159 | |
Willy Tarreau | 419a598 | 2012-06-12 08:52:22 +0200 | [diff] [blame] | 160 | /* now next is multiple of 8 or equal to end */ |
| 161 | while (next <= (end-68)) { |
| 162 | if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL)) |
| 163 | break; |
| 164 | next += 8; |
| 165 | if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL)) |
| 166 | break; |
| 167 | next += 8; |
| 168 | if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL)) |
| 169 | break; |
| 170 | next += 8; |
| 171 | if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL)) |
| 172 | break; |
| 173 | next += 8; |
| 174 | if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL)) |
| 175 | break; |
| 176 | next += 8; |
| 177 | if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL)) |
| 178 | break; |
| 179 | next += 8; |
| 180 | if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL)) |
| 181 | break; |
| 182 | next += 8; |
| 183 | if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL)) |
| 184 | break; |
| 185 | next += 8; |
| 186 | } |
Willy Tarreau | de5dc05 | 2012-06-09 09:44:03 +0200 | [diff] [blame] | 187 | |
Willy Tarreau | 419a598 | 2012-06-12 08:52:22 +0200 | [diff] [blame] | 188 | /* maybe we can skip 4 more bytes */ |
| 189 | if (!has_zero32(*(unsigned int *)next ^ 0x0A0A0A0AU)) |
| 190 | next += 4; |
| 191 | } |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 192 | |
Willy Tarreau | 419a598 | 2012-06-12 08:52:22 +0200 | [diff] [blame] | 193 | /* We finish if needed : if <next> is below <end>, it means we |
| 194 | * found an LF in one of the 4 following bytes. |
| 195 | */ |
| 196 | while (next < end) { |
| 197 | if (*next == '\n') |
| 198 | break; |
| 199 | next++; |
| 200 | } |
| 201 | #endif |
| 202 | return next; |
| 203 | } |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 204 | |
Willy Tarreau | 419a598 | 2012-06-12 08:52:22 +0200 | [diff] [blame] | 205 | const char *fgets2(FILE *stream) |
| 206 | { |
| 207 | static char buffer[FGETS2_BUFSIZE + 68]; /* Note: +32 is enough on 32-bit systems */ |
| 208 | static char *end = buffer; |
| 209 | static char *line = buffer; |
| 210 | char *next; |
| 211 | int ret; |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 212 | |
Willy Tarreau | 419a598 | 2012-06-12 08:52:22 +0200 | [diff] [blame] | 213 | next = line; |
| 214 | |
| 215 | while (1) { |
| 216 | next = find_lf(next, end); |
| 217 | if (next < end) { |
| 218 | const char *start = line; |
| 219 | *next = '\0'; |
| 220 | line = next + 1; |
| 221 | return start; |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* we found an incomplete line. First, let's move the |
| 225 | * remaining part of the buffer to the beginning, then |
Willy Tarreau | 31a02e9 | 2011-09-10 10:25:05 +0200 | [diff] [blame] | 226 | * try to complete the buffer with a new read. We can't |
| 227 | * rely on <next> anymore because it went past <end>. |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 228 | */ |
| 229 | if (line > buffer) { |
| 230 | if (end != line) |
| 231 | memmove(buffer, line, end - line); |
| 232 | end = buffer + (end - line); |
| 233 | next = end; |
| 234 | line = buffer; |
| 235 | } else { |
| 236 | if (end == buffer + FGETS2_BUFSIZE) |
| 237 | return NULL; |
| 238 | } |
| 239 | |
| 240 | ret = read(fileno(stream), end, buffer + FGETS2_BUFSIZE - end); |
| 241 | |
| 242 | if (ret <= 0) { |
| 243 | if (end == line) |
| 244 | return NULL; |
| 245 | |
| 246 | *end = '\0'; |
Willy Tarreau | 812e7a7 | 2011-07-09 14:28:01 +0200 | [diff] [blame] | 247 | end = line; /* ensure we stop next time */ |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 248 | return line; |
| 249 | } |
| 250 | |
| 251 | end += ret; |
Willy Tarreau | 31a02e9 | 2011-09-10 10:25:05 +0200 | [diff] [blame] | 252 | *end = '\n'; /* make parser stop ASAP */ |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 253 | /* search for '\n' again */ |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | #ifdef BENCHMARK |
| 258 | int main() { |
| 259 | const char *p; |
| 260 | unsigned int lines = 0; |
| 261 | |
| 262 | while ((p=fgets2(stdin))) |
| 263 | lines++; |
| 264 | printf("lines=%d\n", lines); |
| 265 | return 0; |
| 266 | } |
| 267 | #endif |