-
Notifications
You must be signed in to change notification settings - Fork 17
/
scan.c
383 lines (322 loc) · 10.2 KB
/
scan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
Copyright 2010-2011 True Blue Logic Ltd
This program is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _XOPEN_SOURCE 600
#define _FILE_OFFSET_BITS 64
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <inttypes.h>
#ifdef HAVE_AIO
#include <aio.h>
#endif
#include <string.h>
#include <setjmp.h>
#include "rabin.h"
#include "scan.h"
#define likely(x) __builtin_expect(x, 1)
#define unlikely(x) __builtin_expect(x, 0)
#if defined(_POSIX_ADVISORY_INFO) && (_POSIX_ADVISORY_INFO > 0)
# define HAVE_POSIX_FADVISE
#endif
struct scan_ctx {
/* The main read buffer itself */
unsigned char *buffer[3];
unsigned char *buffer_end;
int buffer_size;
/* Reading the source file */
int fd;
unsigned long long source_offset;
unsigned char *p;
int bytes_left;
int eof; /* if EOF has been read */
struct rabin_ctx *rabin_ctx;
int window_size;
int target_chunk_size;
int minimum_chunk_size;
int maximum_chunk_size;
#ifdef HAVE_AIO
struct aiocb aiocb;
#endif
unsigned char *io_destination;
void (*start_io)(struct scan_ctx *, unsigned char *);
void (*finish_io)(struct scan_ctx *);
jmp_buf jmp_env;
};
static int retry_read(struct scan_ctx *scan, unsigned char *p,
int bytes_to_read) {
ssize_t result;
unsigned char *start, *end;
start = p;
end = p + bytes_to_read;
while (p < end) {
do {
result = read(scan->fd, p, end - p);
} while (result < 0 && errno == EINTR);
if (result < 0) {
longjmp(scan->jmp_env, 1);
} else if (!result) {
scan->eof = 1;
break;
}
p += result;
}
return p - start;
}
static void start_sync_io(struct scan_ctx *scan, unsigned char *buffer) {
scan->io_destination = buffer;
}
static void finish_sync_io(struct scan_ctx *scan) {
int bytes_read;
bytes_read = retry_read(scan, scan->io_destination, scan->buffer_size / 3);
#ifdef HAVE_POSIX_FADVISE
posix_fadvise(scan->fd, scan->source_offset, bytes_read, POSIX_FADV_DONTNEED);
#endif
scan->source_offset += bytes_read;
scan->bytes_left += bytes_read;
}
#ifdef HAVE_AIO
static void start_aio(struct scan_ctx *scan, unsigned char *buffer) {
memset(&scan->aiocb, 0, sizeof(scan->aiocb));
scan->aiocb.aio_buf = scan->io_destination = buffer;
scan->aiocb.aio_nbytes = scan->buffer_size / 3;
scan->aiocb.aio_fildes = scan->fd;
scan->aiocb.aio_offset = scan->source_offset;
if (aio_read(&scan->aiocb)) {
longjmp(scan->jmp_env, 1);
}
}
static void finish_aio(struct scan_ctx *scan) {
int bytes_read;
off_t required_offset, lseek_result;
int result;
const struct aiocb *cblist[1] = { &scan->aiocb };
do {
result = aio_suspend(cblist, 1, 0);
} while (result < 0 && errno == EINTR);
if (result) {
longjmp(scan->jmp_env, 1);
}
if (aio_error(&scan->aiocb)) {
longjmp(scan->jmp_env, 1);
}
bytes_read = aio_return(&scan->aiocb);
if (!bytes_read) {
scan->eof = 1;
return;
} else if (bytes_read != scan->buffer_size / 3) {
required_offset = scan->source_offset + bytes_read;
/* If we have been reading from a regular file, then the current
offset will not match the data we have read in using aio_read,
since that doesn't alter the offset, so we need to seek to the
correct offset. However, we could have been reading from a pipe, in
which case this lseek will fail because of ESPIPE, which is fine
since there is no "offset" to be misaligned and we should
still fill the buffer. Any other failure is an error */
lseek_result = lseek(scan->fd, required_offset, SEEK_SET);
if ((lseek_result == required_offset) ||
(lseek_result < 0 && errno == ESPIPE))
bytes_read += retry_read(scan,
scan->io_destination + bytes_read,
scan->buffer_size / 3 - bytes_read);
else
longjmp(scan->jmp_env, 1);
}
#ifdef HAVE_POSIX_FADVISE
posix_fadvise(scan->fd, scan->source_offset, bytes_read, POSIX_FADV_DONTNEED);
#endif
scan->source_offset += bytes_read;
scan->bytes_left += bytes_read;
}
#endif /* #ifdef HAVE_AIO */
static inline void read_more_data(struct scan_ctx *scan) {
unsigned char *head = scan->p + scan->bytes_left;
unsigned char *readahead_buffer;
if (scan->eof)
return;
assert(head == scan->buffer[0] || head == scan->buffer[1] ||
head == scan->buffer[2] ||
head == scan->buffer_end);
if (head == scan->buffer_end)
head = scan->buffer[0];
if (head == scan->buffer[0])
readahead_buffer = scan->buffer[1];
else if (head == scan->buffer[1])
readahead_buffer = scan->buffer[2];
else if (head == scan->buffer[2])
readahead_buffer = scan->buffer[0];
scan->finish_io(scan);
if (!scan->eof)
scan->start_io(scan, readahead_buffer);
}
static inline void boundary_hit(struct scan_ctx *scan, unsigned char *boundary,
int chunk_size, struct scan_chunk_data *chunk_data) {
if (scan->buffer[0] + chunk_size > boundary) {
/* chunk is wrapped */
chunk_data[0].buf = boundary + scan->buffer_size - chunk_size;
chunk_data[0].size = chunk_size - (boundary - scan->buffer[0]);
chunk_data[1].buf = scan->buffer[0];
chunk_data[1].size = boundary - scan->buffer[0];
} else {
/* chunk is not wrapped */
chunk_data[0].buf = boundary - chunk_size;
chunk_data[0].size = chunk_size;
chunk_data[1].buf = 0;
chunk_data[1].size = 0;
}
}
int scan_read_chunk(struct scan_ctx *scan,
struct scan_chunk_data *chunk_data) {
uint32_t hash;
int current_chunk_size;
int temp;
unsigned char *old;
if (setjmp(scan->jmp_env))
return 0;
if (unlikely(scan->bytes_left <= scan->minimum_chunk_size)) {
if (unlikely(scan->eof)) {
boundary_hit(scan, scan->p + scan->bytes_left, scan->bytes_left,
chunk_data);
return SCAN_CHUNK_FOUND | SCAN_CHUNK_LAST;
} else {
read_more_data(scan);
/* Check again in case of EOF */
if (unlikely(scan->bytes_left <= scan->minimum_chunk_size)) {
assert(scan->eof);
boundary_hit(scan, scan->p + scan->bytes_left,
scan->bytes_left, chunk_data);
return SCAN_CHUNK_FOUND | SCAN_CHUNK_LAST;
}
}
}
assert(scan->bytes_left >= scan->minimum_chunk_size);
/* Move forward by the minimum_chunk_size */
scan->p += scan->minimum_chunk_size;
scan->bytes_left -= scan->minimum_chunk_size;
if (unlikely(scan->p >= scan->buffer_end))
scan->p -= scan->buffer_size;
current_chunk_size = scan->minimum_chunk_size;
/* Calculate the first hash (aligned to the end of the minimum chunk size)
* */
if(unlikely(scan->p < scan->buffer[0] + scan->window_size)) {
/* scan->p has wrapped; hash calculation must take place in two
* sections */
temp = scan->window_size - (scan->p - scan->buffer[0]);
/* temp is the amount before the wrap */
old = scan->buffer_end - temp;
hash = rabin_hash_split(scan->rabin_ctx, old, temp, scan->buffer[0]);
} else {
old = scan->p - scan->window_size;
hash = rabin_hash(scan->rabin_ctx, old);
}
while (1) {
if (unlikely((((scan->target_chunk_size-1) & hash) ==
scan->target_chunk_size-1)
|| (current_chunk_size >= scan->maximum_chunk_size))) {
/* Hash has matched to a boundary, or we have got to the maximum
* chunk size */
boundary_hit(scan, scan->p, current_chunk_size,
chunk_data);
return SCAN_CHUNK_FOUND;
} else {
/* Move up and calculate the next hash */
if (unlikely(!scan->bytes_left)) {
if (unlikely(scan->eof)) {
boundary_hit(scan, scan->p, current_chunk_size,
chunk_data);
return SCAN_CHUNK_FOUND | SCAN_CHUNK_LAST;
}
read_more_data(scan);
if (unlikely(!scan->bytes_left)) {
assert(scan->eof);
boundary_hit(scan, scan->p, current_chunk_size,
chunk_data);
return SCAN_CHUNK_FOUND | SCAN_CHUNK_LAST;
}
}
hash = rabin_hash_next(scan->rabin_ctx, hash, *old, *scan->p);
scan->p += 1;
if (unlikely(scan->p >= scan->buffer_end))
scan->p -= scan->buffer_size;
old += 1;
if (unlikely(old >= scan->buffer_end))
old -= scan->buffer_size;
scan->bytes_left -= 1;
current_chunk_size += 1;
}
}
}
struct scan_ctx *scan_init(void) {
struct scan_ctx *scan;
scan = malloc(sizeof(struct scan_ctx));
if (!scan)
goto unwind0;
scan->buffer_size = 3 * (1<<24);
scan->buffer[0] = malloc(scan->buffer_size);
if (!scan->buffer[0])
goto unwind1;
scan->buffer[1] = scan->buffer[0] + scan->buffer_size/3;
scan->buffer[2] = scan->buffer[1] + scan->buffer_size/3;
scan->buffer_end = scan->buffer[0] + scan->buffer_size;
scan->p = scan->buffer[0];
scan->eof = 0;
scan->bytes_left = 0;
scan->source_offset = 0;
scan->window_size = 48;
scan->target_chunk_size = 1 << 18;
scan->minimum_chunk_size = 1 << 16;
scan->maximum_chunk_size = 1 << 24;
scan->rabin_ctx = rabin_init(1103515245, scan->window_size);
if (!scan->rabin_ctx)
goto unwind2;
scan->start_io = start_sync_io;
scan->finish_io = finish_sync_io;
return scan;
unwind2:
rabin_free(scan->rabin_ctx);
unwind1:
free(scan->buffer[0]);
unwind0:
return 0;
}
void scan_free(struct scan_ctx *scan) {
rabin_free(scan->rabin_ctx);
free(scan->buffer[0]);
free(scan);
}
void scan_set_fd(struct scan_ctx *scan, int fd) {
scan->fd = fd;
}
#ifdef HAVE_AIO
void scan_set_aio(struct scan_ctx *scan) {
scan->start_io = start_aio;
scan->finish_io = finish_aio;
}
#else
void scan_set_aio(struct scan_ctx *scan) {}
#endif
int scan_begin(struct scan_ctx *scan) {
if (setjmp(scan->jmp_env))
return 0;
scan->start_io(scan, scan->buffer[0]);
scan->finish_io(scan);
if (!scan->eof)
scan->start_io(scan, scan->buffer[1]);
return 1;
}
/* vim: set ts=8 sw=4 sts=4 cindent : */