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 | |
| 24 | // return 1 if the integer contains at least one zero byte |
| 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; |
| 41 | x = ~x & 0x80808080; /* save and invert bits 7, 15, 23, 31 */ |
| 42 | y &= 0x7F7F7F7F; /* clear them */ |
| 43 | y -= 0x01010101; /* generate a carry */ |
| 44 | y &= x; /* clear the bits that were already set */ |
| 45 | return !!y; |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | |
| 49 | #define FGETS2_BUFSIZE (256*1024) |
| 50 | const char *fgets2(FILE *stream) |
| 51 | { |
| 52 | static char buffer[FGETS2_BUFSIZE + 5]; |
| 53 | static char *end = buffer; |
| 54 | static char *line = buffer; |
| 55 | |
| 56 | char *next; |
| 57 | int ret; |
| 58 | |
| 59 | next = line; |
| 60 | |
| 61 | while (1) { |
| 62 | /* this is a speed-up, we read 32 bits at once and check for an |
| 63 | * LF character there. We stop if found then continue one at a |
| 64 | * time. |
| 65 | */ |
| 66 | while (next < end && (((unsigned long)next) & 3) && *next != '\n') |
| 67 | next++; |
| 68 | |
| 69 | /* now next is multiple of 4 or equal to end */ |
| 70 | while (next <= (end-32)) { |
| 71 | if (has_zero(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 72 | break; |
| 73 | next += 4; |
| 74 | if (has_zero(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 75 | break; |
| 76 | next += 4; |
| 77 | if (has_zero(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 78 | break; |
| 79 | next += 4; |
| 80 | if (has_zero(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 81 | break; |
| 82 | next += 4; |
| 83 | if (has_zero(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 84 | break; |
| 85 | next += 4; |
| 86 | if (has_zero(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 87 | break; |
| 88 | next += 4; |
| 89 | if (has_zero(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 90 | break; |
| 91 | next += 4; |
| 92 | if (has_zero(*(unsigned int *)next ^ 0x0A0A0A0A)) |
| 93 | break; |
| 94 | next += 4; |
| 95 | } |
| 96 | |
| 97 | /* we finish if needed. Note that next might be slightly higher |
| 98 | * than end here because we might have gone past it above. |
| 99 | */ |
| 100 | while (next < end) { |
| 101 | if (*next == '\n') { |
| 102 | const char *start = line; |
| 103 | |
| 104 | *next = '\0'; |
| 105 | line = next + 1; |
| 106 | return start; |
| 107 | } |
| 108 | next++; |
| 109 | } |
| 110 | |
| 111 | /* we found an incomplete line. First, let's move the |
| 112 | * remaining part of the buffer to the beginning, then |
| 113 | * try to complete the buffer with a new read. |
| 114 | */ |
| 115 | if (line > buffer) { |
| 116 | if (end != line) |
| 117 | memmove(buffer, line, end - line); |
| 118 | end = buffer + (end - line); |
| 119 | next = end; |
| 120 | line = buffer; |
| 121 | } else { |
| 122 | if (end == buffer + FGETS2_BUFSIZE) |
| 123 | return NULL; |
| 124 | } |
| 125 | |
| 126 | ret = read(fileno(stream), end, buffer + FGETS2_BUFSIZE - end); |
| 127 | |
| 128 | if (ret <= 0) { |
| 129 | if (end == line) |
| 130 | return NULL; |
| 131 | |
| 132 | *end = '\0'; |
| 133 | return line; |
| 134 | } |
| 135 | |
| 136 | end += ret; |
| 137 | /* search for '\n' again */ |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | #ifdef BENCHMARK |
| 142 | int main() { |
| 143 | const char *p; |
| 144 | unsigned int lines = 0; |
| 145 | |
| 146 | while ((p=fgets2(stdin))) |
| 147 | lines++; |
| 148 | printf("lines=%d\n", lines); |
| 149 | return 0; |
| 150 | } |
| 151 | #endif |