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
|
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <libaio.h>
#define FASTWRITER_SYNCIO_ALIGN 4096
#define SYNC_MODE
#define AIO_MODE 2
#define EXTRA_BUFFERS 2
#define WRITE_INTERVAL 1
size_t SKIP = 1;
size_t SEGMENT = 0;
size_t LINE = 0;
#define RAID_STRIP_SIZE 256
#define RAID_DISKS 8
#define STRIPS_AT_ONCE 2
#define MIN_BLOCK_SIZE (1024 * RAID_STRIP_SIZE)
#define BLOCK_SIZE (1024 * RAID_STRIP_SIZE * RAID_DISKS * STRIPS_AT_ONCE)
#define BUFSIZE (BLOCK_SIZE * (AIO_MODE + EXTRA_BUFFERS))
#ifdef AIO_MODE
# define SYNC_MODE
#endif /* AIO_MODE */
DIR *dir;
struct dirent *ent;
int flags = O_RDONLY|O_NOATIME|O_LARGEFILE|O_DIRECT;
size_t run = 0; // file reading order
size_t skip = 0; // file reading order
int fd = -1; // we are currently scheduling reads from this file
size_t offset = 0; // we are schedulling reads at this offset
size_t size; // size of block we are schedulling to read
size_t file_size; // total size of current file
size_t file_block_size; // access unit for current file
size_t curf = 0, schedf = 0;
size_t curio = 0, schedio = 0;
#define MAXLEN 128
int done[AIO_MODE + EXTRA_BUFFERS];
int done_finish[AIO_MODE + EXTRA_BUFFERS];
char done_file[AIO_MODE + EXTRA_BUFFERS][MAXLEN + 1];
size_t done_offset[AIO_MODE + EXTRA_BUFFERS];
size_t done_size[AIO_MODE + EXTRA_BUFFERS];
int done_fd[AIO_MODE + EXTRA_BUFFERS];
#define buf(io) (io%(AIO_MODE + EXTRA_BUFFERS))
#define max(a,b) (((a)>(b))?(a):(b))
#define min(a,b) (((a)<(b))?(a):(b))
#define page(size) (((size/4096)+(size%4096?1:0))*4096)
int next_file() {
size_t size;
struct stat st;
next:
while ((ent = readdir(dir))) {
skip += 1;
if ((((skip) - 1)%SKIP) != run) continue;
if (stat(ent->d_name, &st)) continue;
if (!S_ISREG(st.st_mode)) continue;
break;
}
if (ent) {
size = st.st_blksize;
int fd = open(ent->d_name, flags, 0);
if (fd < 0) goto next;
if (size < MIN_BLOCK_SIZE) size = BLOCK_SIZE;
if (size > BUFSIZE) {
printf("Buffer too small\n");
exit(1);
}
file_block_size = size;
file_size = st.st_size;
offset = 0;
return fd;
} else {
skip = 0;
run += 1;
if (run < SKIP) {
closedir(dir);
dir = opendir(".");
goto next;
}
}
return -1;
}
int next_block() {
int io;
size_t next_offset;
if (fd >= 0) {
if (LINE) offset += SEGMENT;
else offset += size;
if (offset >= file_size) {
fd = -1;
}
}
if (fd < 0) {
fd = next_file();
if (fd < 0) return -1;
// printf("open ===> %s (%i)\n", ent->d_name, fd);
}
if (LINE) size = LINE;
else size = file_block_size;
if ((offset + size) > file_size) size = file_size - offset;
io = buf(schedio);
strncpy(done_file[io], ent->d_name, MAXLEN);
done_offset[io] = offset;
done_size[io] = size;
done_fd[io] = fd;
done[io] = 0;
if (LINE) next_offset = offset + SEGMENT;
else next_offset = offset + size;
if (next_offset >= file_size) done_finish[io] = 1;
else done_finish[io] = 0;
return 0;
}
int main(int argc, char *argv[]) {
int err;
int i, n, io;
long double mcoef = 1000000. / (1024 * 1024);
io_context_t aio;
struct iocb ios[AIO_MODE], *ioptr[AIO_MODE];
int events;
struct io_event ev[AIO_MODE];
int ready;
void *buffer;
struct timeval start, fstart, tv;
if (argc < 2) {
printf("Usage: %s <directory> [skip] [segment] [line]\n", argv[0]);
exit(0);
}
chdir(argv[1]);
dir = opendir(".");
if (argc > 2) SKIP = atoi(argv[2]);
if (argc > 3) SEGMENT = atoi(argv[3]);
if (argc > 4) LINE = atoi(argv[4]);
if (!SKIP) SKIP = 1;
printf("%s: Skip %zu, Segment %zu, Line %zu\n", argv[1], SKIP, SEGMENT, LINE);
posix_memalign((void**)&buffer, FASTWRITER_SYNCIO_ALIGN, BUFSIZE);
memset(done, 0, sizeof(done));
memset(&aio, 0, sizeof(aio));
io_queue_init(AIO_MODE, &aio);
for (schedio = 0; schedio < AIO_MODE; schedio++) {
ioptr[schedio] = &ios[schedio];
memset(ioptr[schedio], 0, sizeof(struct iocb));
err = next_block();
if (err) break;
io_prep_pread(ioptr[schedio], fd, buffer + schedio * BLOCK_SIZE, page(size), offset);
io_set_callback(ioptr[schedio], (void*)(uintptr_t)schedio);
// printf("sched %zu: %zu (%zu %zu)\n", schedio, schedio, offset, size);
}
size_t us, fileus;
size_t last_write = 0;
// size_t last_file_write = 0;
// size_t last_file_size = 0;
size_t cur_file_size = 0;
size_t total_size = 0;
size_t files = 0;
gettimeofday(&start, NULL);
gettimeofday(&fstart, NULL);
n = io_submit(aio, schedio, ioptr);
if (n != schedio) {
printf("Failed to submit initial AIO job, io_submit returned %i\n", err);
exit(-1);
}
curio = 0;
events = 0;
ready = 0;
while ((err >= 0)||(curio != schedio)) {
io = buf(curio);
if (!done[io]) {
// printf("%i,%i - %i [%i %i %i %i]\n", curio, schedio, events, done[0], done[1], done[2], done[3]);
if (curio < schedio) {
n = io_getevents(aio, 1, AIO_MODE + EXTRA_BUFFERS - events, &ev[events], NULL);
if (n < 0) {
printf("Error waiting for AIO (%i)\n", -err);
exit(-1);
}
} else {
n = 0;
}
if ((!ready)&&(n > 1)) {
printf("*** Multiple read requests (%i of %i) are finished simultaneously. It is either:\n", err, AIO_MODE);
printf(" Small buffer size (%i KB)\n", BLOCK_SIZE/1024);
printf(" More parallel AIOs (%i) than supported by kernel, try %i\n", AIO_MODE, AIO_MODE - err);
}
for (i = 0; i < n; i++) {
struct io_event *ep = &ev[events + i];
int doneio = (uintptr_t)ep->data;
io = buf(doneio);
// printf("done %i: %lu %zu %zi\n", doneio, ep->res2, done_size[io], ep->res);
if (ep->res2 || (ep->res < done_size[io])) {
printf("Error in async IO (ret: %li, ret size: %zi, expected %zu)\n", ep->res2, ep->res, page(done_size[io]));
exit(-1);
}
done[io] = 1;
// printf("done (%i): %i\n", i, doneio);
}
events += n;
for (i = events - 1; (i >= 0)&&((schedio - curio) < (AIO_MODE + EXTRA_BUFFERS)); i--) {
err = next_block();
if (err) break;
// printf("sched %i: %zu (%i %zu %zu)\n", i, schedio, fd, offset, size);
struct iocb *newio = (struct iocb *)ev[i].obj;
memset(newio, 0, sizeof(struct iocb));
io_prep_pread(newio, fd, buffer + buf(schedio) * BLOCK_SIZE, page(size), offset);
io_set_callback(newio, (void*)(uintptr_t)schedio);
err = io_submit(aio, 1, &newio);
if (err != 1) {
printf("Failed to submit AIO jobs %i\n", err);
exit(-1);
}
schedio++;
}
events = i + 1;
if ((events)&&(!err)) {
printf("*** Unprocessed events (%i), probably not enough buffer space...\n", events);
// printf(" curio (%zu), schedio (%zu)\n", curio, schedio);
}
ready = 1;
continue;
}
io = buf(curio);
cur_file_size += done_size[io];
total_size += done_size[io];
gettimeofday(&tv, NULL);
us = (tv.tv_sec - start.tv_sec) * 1000000 + (tv.tv_usec - start.tv_usec);
fileus = (tv.tv_sec - fstart.tv_sec) * 1000000 + (tv.tv_usec - fstart.tv_usec);
if (done_finish[io]) {
// printf("closing %i ===> %s (%i)\n", io, done_file[io], done_fd[io]);
close(done_fd[io]);
gettimeofday(&fstart, NULL);
files++;
}
if ((us - last_write) > WRITE_INTERVAL * 1000000) {
last_write = us;
printf("Read: %lu files (%lu GB) at %zu MB/s", files, total_size / 1024 / 1024 / 1024, (size_t)(mcoef * total_size / us));
if (1) {//cur_file_size > BLOCK_SIZE) {
printf(", Last: %s (%lu MB) at %zu MB/s\n", done_file[io], cur_file_size / 1024 / 1024, (size_t)(mcoef * cur_file_size / fileus));
} else {
printf("\n");
}
}
if (done_finish[io]) {
cur_file_size = 0;
}
done[io] = 0;
curio++;
}
gettimeofday(&tv, NULL);
us = (tv.tv_sec - start.tv_sec) * 1000000 + (tv.tv_usec - start.tv_usec);
printf("Total: %lu files (%lu GB) at %zu MB/s\n", files, total_size / 1024 / 1024 / 1024, (size_t)(mcoef * total_size / us));
free(buffer);
closedir(dir);
}
|