Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 96c148b | 2011-09-09 08:21:55 +0200 | [diff] [blame] | 24 | // return non-zero if the integer contains at least one zero byte |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 25 | static inline unsigned int has_zero(unsigned int x) |
| 26 | { |
Willy Tarreau | 1769a18 | 2010-05-04 10:47:57 +0200 | [diff] [blame] | 27 | 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 Tarreau | 96c148b | 2011-09-09 08:21:55 +0200 | [diff] [blame] | 41 | y -= 0x01010101; /* generate a carry */ |
| 42 | y &= ~x; /* clear the bits that were already set */ |
| 43 | return y & 0x80808080; |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 44 | } |
| 45 | |
Willy Tarreau | 1769a18 | 2010-05-04 10:47:57 +0200 | [diff] [blame] | 46 | |
Willy Tarreau | 96c148b | 2011-09-09 08:21:55 +0200 | [diff] [blame] | 47 | // return non-zero if the argument contains at least one zero byte. See principle above. |
| 48 | static inline unsigned long long has_zero64(unsigned long long x) |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 49 | { |
Willy Tarreau | 1769a18 | 2010-05-04 10:47:57 +0200 | [diff] [blame] | 50 | unsigned long long y; |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 51 | |
Willy Tarreau | 1769a18 | 2010-05-04 10:47:57 +0200 | [diff] [blame] | 52 | y = x; |
Willy Tarreau | 1769a18 | 2010-05-04 10:47:57 +0200 | [diff] [blame] | 53 | y -= 0x0101010101010101ULL; /* generate a carry */ |
Willy Tarreau | 96c148b | 2011-09-09 08:21:55 +0200 | [diff] [blame] | 54 | y &= ~x; /* clear the bits that were already set */ |
| 55 | return y & 0x8080808080808080ULL; |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | #define FGETS2_BUFSIZE (256*1024) |
| 59 | const char *fgets2(FILE *stream) |
| 60 | { |
Willy Tarreau | 31a02e9 | 2011-09-10 10:25:05 +0200 | [diff] [blame] | 61 | static char buffer[FGETS2_BUFSIZE + 68]; |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 62 | 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 Tarreau | d2c142c | 2010-05-05 12:22:08 +0200 | [diff] [blame] | 71 | /* this is a speed-up, we read 64 bits at once and check for an |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 72 | * LF character there. We stop if found then continue one at a |
| 73 | * time. |
| 74 | */ |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 75 | |
Willy Tarreau | 31a02e9 | 2011-09-10 10:25:05 +0200 | [diff] [blame] | 76 | if (next <= end) { |
Willy Tarreau | d2c142c | 2010-05-05 12:22:08 +0200 | [diff] [blame] | 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 Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 97 | 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 Tarreau | d2c142c | 2010-05-05 12:22:08 +0200 | [diff] [blame] | 109 | if (has_zero64(*(unsigned long long *)next ^ 0x0A0A0A0A0A0A0A0AULL)) |
| 110 | break; |
| 111 | next += 8; |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Willy Tarreau | d2c142c | 2010-05-05 12:22:08 +0200 | [diff] [blame] | 114 | /* maybe we can skip 4 more bytes */ |
| 115 | if (!has_zero(*(unsigned int *)next ^ 0x0A0A0A0AU)) |
| 116 | next += 4; |
| 117 | |
Willy Tarreau | 31a02e9 | 2011-09-10 10:25:05 +0200 | [diff] [blame] | 118 | /* We finish if needed : if <next> is below <end>, it means we |
| 119 | * found an LF in one of the 4 following bytes. |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 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 |
Willy Tarreau | 31a02e9 | 2011-09-10 10:25:05 +0200 | [diff] [blame] | 134 | * try to complete the buffer with a new read. We can't |
| 135 | * rely on <next> anymore because it went past <end>. |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 136 | */ |
| 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 Tarreau | 812e7a7 | 2011-07-09 14:28:01 +0200 | [diff] [blame] | 155 | end = line; /* ensure we stop next time */ |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 156 | return line; |
| 157 | } |
| 158 | |
| 159 | end += ret; |
Willy Tarreau | 31a02e9 | 2011-09-10 10:25:05 +0200 | [diff] [blame] | 160 | *end = '\n'; /* make parser stop ASAP */ |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 161 | /* search for '\n' again */ |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | #ifdef BENCHMARK |
| 166 | int 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 |