RSS

(root)/alps/fwbench : /seqreader.c (revision 19)

To get this branch, use:
bzr branch /webbzr/alps/fwbench
Line Revision Contents
1 8
#define _GNU_SOURCE
2
3 7
#include <stdio.h>
4
#include <stdlib.h>
5 13
#include <stdint.h>
6 7
#include <sys/types.h>
7
#include <sys/stat.h>
8
#include <sys/time.h>
9
#include <unistd.h>
10
#include <dirent.h>
11 8
#include <fcntl.h>
12
#include <string.h>
13
#include <errno.h>
14
15 13
#include <libaio.h>
16
17 11
#define FASTWRITER_SYNCIO_ALIGN 512
18
19 12
#define SYNC_MODE
20 13
#define AIO_MODE 2
21 15
//#define FS_SYNC_MODE
22 13
#define EXTRA_BUFFERS 2
23
#define WRITE_INTERVAL 1
24
25 17
26 13
#define RAID_STRIP_SIZE 	256
27
#define RAID_DISKS		8
28
#define STRIPS_AT_ONCE		2
29
30 17
#define FS_MIN_BLOCK_SIZE (1024 * RAID_STRIP_SIZE)
31
#define FS_BLOCK_SIZE (1024 * RAID_STRIP_SIZE * RAID_DISKS)
32
33 13
#ifdef AIO_MODE
34
# define SYNC_MODE
35
#endif /* AIO_MODE */
36
37 12
#ifdef SYNC_MODE
38 13
# define BLOCK_SIZE (1024 * RAID_STRIP_SIZE * RAID_DISKS * STRIPS_AT_ONCE)
39 12
#else /* SYNC_MODE */
40
# define BLOCK_SIZE 16384
41
#endif /* SYNC_MODE */
42 13
43
#ifdef AIO_MODE
44
# define BUFSIZE (BLOCK_SIZE * (AIO_MODE + EXTRA_BUFFERS))
45
#else /* AIO_MODE */
46 19
# define BUFSIZE (1024 * RAID_STRIP_SIZE * RAID_DISKS * STRIPS_AT_ONCE) 
47 13
#endif /* AIO_MODE */
48 8
49 7
50
int main(int argc, char *argv[]) {
51
    int err;
52
    size_t SKIP = 1;
53
    DIR *dir;
54
    struct dirent *ent;
55 15
    struct timeval start, fstart, tv;
56 7
    size_t us;
57
    size_t files = 0;
58
    size_t total_size = 0;
59 8
    size_t last_write = 0;
60 13
    size_t last_size = 0;
61 7
    size_t skip;
62
    size_t run;
63 13
    size_t ready;
64 9
    ssize_t res;
65 12
    size_t max_size = (size_t)-1;
66 15
    char *buffer;
67 9
    long double mcoef = 1000000. / (1024 * 1024);
68 12
    int flags = O_RDONLY|O_NOATIME|O_LARGEFILE;
69 17
    struct stat st;
70 12
71 13
#ifdef AIO_MODE
72
    int i;
73
    size_t curio, schedio;
74
    int done[AIO_MODE + EXTRA_BUFFERS];
75
    
76
    io_context_t aio;
77
    struct iocb io[AIO_MODE], *ioptr[AIO_MODE];
78
79
    int events;
80
    struct io_event ev[AIO_MODE];
81
#endif /* AIO_MODE */
82
83
84 11
    posix_memalign((void**)&buffer, FASTWRITER_SYNCIO_ALIGN, BUFSIZE);
85
    
86 7
    if (argc < 2) {
87 17
	printf("Usage: %s <directory|file|device> [skip|size]\n", argv[0]);
88 7
	exit(0);
89
    }
90
    
91 17
    if (stat(argv[1], &st)) {
92
	printf("stat on (%s) have failed", argv[1]);
93
	exit(-1);
94
    }
95
    
96
    if (strstr(argv[1], "/dev/")||(S_ISREG(st.st_mode))) {
97 12
	if (argc > 2) {
98
	    max_size = atol(argv[2]);
99
	    max_size *= 1024 * 1024 * 1024;
100
	}
101
102 15
#ifdef SYNC_MODE
103
	flags |= O_DIRECT;
104
#endif
105
	
106
	printf("Used buffer: %i MB, Block: %i KB\n", BUFSIZE / 1024 / 1024, BLOCK_SIZE/1024);
107
108
109 12
	int fd = open(argv[1], flags, 0);
110 8
	if (fd < 0) {
111
	    printf("Unable to open device %s\n", argv[1]);
112
	    exit(1);
113
	}
114 13
115 9
	size_t size = BLOCK_SIZE;
116 13
117
#ifdef AIO_MODE	
118
	memset(done, 0, sizeof(done));
119
	memset(&aio, 0, sizeof(aio));
120
	io_queue_init(AIO_MODE, &aio);
121
	for (i = 0; i < AIO_MODE; i++) {
122
	    ioptr[i] = &io[i];
123
	    memset(ioptr[i], 0, sizeof(struct iocb));
124
	    io_prep_pread(ioptr[i], fd, buffer + i * BLOCK_SIZE, BLOCK_SIZE, i * BLOCK_SIZE);
125
	    io_set_callback(ioptr[i], (void*)(uintptr_t)i);
126
	}
127
128
	curio = 0;
129
	schedio = AIO_MODE;
130
	events = 0;
131
#endif /* AIO_MODE */
132 8
	
133
	gettimeofday(&start, NULL);
134 13
135
#ifdef AIO_MODE	
136
        err = io_submit(aio, AIO_MODE, ioptr);
137
        if (err != AIO_MODE) {
138 17
    	    printf("Failed to submit initial AIO job, io_submit returned %i\n", err);
139
    	    exit(-1);
140 13
	}
141
#endif /* AIO_MODE */
142
	
143
#ifdef AIO_MODE	
144
	ready = 0;
145
	while (1) {
146
	    if (!done[curio%(AIO_MODE + EXTRA_BUFFERS)]) {
147 16
//		printf("%i,%i - %i [%i %i %i %i]\n", curio, schedio, events, done[0], done[1], done[2], done[3]);
148
		
149
		if (curio < schedio) {
150
		    err = io_getevents(aio, 1, AIO_MODE + EXTRA_BUFFERS - events, &ev[events], NULL);
151
		    if (err < 0) {
152
			printf("Error waiting for AIO (%i)\n", -err);
153
			exit(-1);
154
		    }
155
		} else {
156
		    err = 0;
157
		}
158 13
		
159
		if ((!ready)&&(err > 1)) {
160
		    printf("*** Multiple read requests (%i of %i) are finished simultaneously. It is either:\n", err, AIO_MODE);
161
		    printf("      Small buffer size (%i KB)\n", BLOCK_SIZE/1024);
162
		    printf("      More parallel AIOs (%i) than supported by kernel, try %i\n", AIO_MODE, AIO_MODE - err);
163
		}
164
		
165
		for (i = 0; i < err; i++) {
166
		    struct io_event *ep = &ev[events + i];
167
		    int doneio = (uintptr_t)ep->data;
168 17
	    	    if (ep->res2 || (ep->res != BLOCK_SIZE)) {
169
	    		printf("Error in async IO\n");
170
	    		exit(-1);
171
	    	    }
172 13
		    done[doneio%(AIO_MODE + EXTRA_BUFFERS)] = 1;
173
//		    printf("done (%i): %i\n", i, doneio);
174
		}
175
		
176
		events += err;
177
		
178
		for (i = events - 1; (i >= 0)&&((schedio - curio) < (AIO_MODE + EXTRA_BUFFERS)); i--) {
179 16
//		    printf("sched (%i): %i\n", i, schedio);
180 13
		    struct iocb *newio = (struct iocb *)ev[i].obj;
181
		    memset(newio, 0, sizeof(struct iocb));
182
		    io_prep_pread(newio, fd, buffer + (schedio % (AIO_MODE + EXTRA_BUFFERS)) * BLOCK_SIZE, BLOCK_SIZE, schedio * BLOCK_SIZE);
183
		    io_set_callback(newio, (void*)(uintptr_t)schedio);
184
		    err = io_submit(aio, 1, &newio);
185 17
		    if (err != 1) {
186
			printf("Failed to submit AIO jobs %i", err);
187
			exit(-1);
188
		    }
189 13
		    schedio++;
190
		}
191
		events = i + 1;
192
		
193
		if (events) {
194
		    printf("*** Unprocessed events (%i), probably not enough buffer space...\n", events);
195
//		    printf("      curio (%zu), schedio (%zu)\n", curio, schedio);
196
		}
197
198
		ready = 1;
199
		continue;
200
	    }
201
202
	    done[curio%(AIO_MODE + EXTRA_BUFFERS)] = 0;
203
	    curio++;
204
205
	    res = BLOCK_SIZE;
206
#else /* AIO_MODE */
207 9
        res = read(fd, buffer, size);
208
	while (res > 0) {
209 13
#endif /* AIO_MODE */
210
211 9
	    if (res != size) {
212
		printf("Incomplete read: %zu bytes read instead of %zu\n", res, size);
213
		exit(-1);
214
	    }
215
	    total_size += res;
216 8
217
	    gettimeofday(&tv, NULL);
218
	    us = (tv.tv_sec - start.tv_sec) * 1000000 + (tv.tv_usec - start.tv_usec);
219
	    if ((us - last_write) > WRITE_INTERVAL * 1000000) {
220 13
		printf("Reading: %s (%lu GB),  Measured speed: %zu MB/s, Current speed: %zu MB/s\n", argv[0], total_size / 1024 / 1024 / 1024, (size_t)(mcoef * total_size / us), (size_t)(mcoef * (total_size - last_size) / (us - last_write)));
221 8
		last_write = us;
222 13
		last_size = total_size;
223 8
	    }
224 12
	    
225
	    if (total_size > max_size) {
226
		printf("Reading: %s (%lu GB),  Measured speed: %zu MB/s\n", argv[0], total_size / 1024 / 1024 / 1024, (size_t)(mcoef * total_size / us));
227
		break;
228
	    }
229
	
230 13
#ifndef AIO_MODE	
231 9
	    res = read(fd, buffer, size);
232 13
#endif /* AIO_MODE */
233 8
	}
234
	
235 13
#ifdef AIO_MODE	
236
	io_queue_release(aio);
237
#endif /* AIO_MODE */
238
239 8
	close(fd);
240
241 9
	if (res < 0) {
242
	    printf("Read failed with errno %i\n", errno);
243
	    exit(-1);
244
	}
245 8
246 11
	free(buffer);
247
	
248 8
	return 0;
249
    }
250
251 15
#ifdef FS_SYNC_MODE
252
    flags |= O_DIRECT;
253
#endif /* FS_SYNC_MODE */
254
	
255 7
    chdir(argv[1]);
256
257
    if (argc > 2) {
258
	SKIP = atoi(argv[2]);
259
	
260 8
	printf("Skip %zu\n", SKIP);
261 7
    }
262
263
    gettimeofday(&start, NULL);
264
    for (run = 0; run < SKIP; run++) {
265
      skip = 0;
266
      dir = opendir(".");
267 8
      while ((ent = readdir(dir))) {
268 7
	if (((skip++)%SKIP) != run) continue;
269
270
	if (stat(ent->d_name, &st)) continue;
271
	if (!S_ISREG(st.st_mode)) continue;
272 12
273 15
	int size = st.st_blksize;
274
275 12
#ifdef F_MODE
276 7
	FILE *f = fopen(ent->d_name, "r");
277
	if (!f) continue;
278 12
#else	
279
	int fd = open(ent->d_name, flags, 0);
280
	if (fd < 0) continue;
281 15
282
# ifdef FS_SYNC_MODE
283
	if (size < BLOCK_SIZE) size = BLOCK_SIZE;
284
# endif /* FS_SYNC_MODE */
285 17
	if (size < FS_MIN_BLOCK_SIZE) size = FS_BLOCK_SIZE;
286 12
#endif	
287 15
288
	if (!files)
289
    	    printf("Reading %s, Block: %i KB\n", ent->d_name, size / 1024);
290 7
	
291
	if (size > BUFSIZE) {
292
	    printf("Buffer too small\n");
293
	    exit(1);
294
	}
295 12
296 15
	size_t last_file_write = 0;
297
	size_t last_file_size = 0;
298
	size_t file_size = 0;
299
        gettimeofday(&fstart, NULL);
300 12
301
#ifdef F_MODE
302 7
	while (!feof(f)) {
303 15
	    ssize_t ret = fread(buffer, 1, size, f);
304 12
#else
305 15
        while (1) {
306
	    ssize_t ret = read(fd, buffer, size);
307 12
#endif
308 15
	    if (ret <= 0) break;
309
	    
310
	    file_size += ret;
311
312
	    gettimeofday(&tv, NULL);
313
	    us = (tv.tv_sec - fstart.tv_sec) * 1000000 + (tv.tv_usec - fstart.tv_usec);
314
315
	    if ((us - last_file_write) > WRITE_INTERVAL * 1000000) {
316
		printf("Reading: %s (%lu GB),  Measured speed: %zu MB/s, Current speed: %zu MB/s\n", ent->d_name, file_size / 1024 / 1024 / 1024, (size_t)(mcoef * file_size / us), (size_t)(mcoef * (file_size - last_file_size) / (us - last_file_write)));
317
		last_file_write = us;
318
		last_file_size = file_size;
319
	    }
320
	}
321
322
	if (!file_size) {
323 12
	    printf("Read failed\n");
324
	    exit(1);
325
	}
326
327
#ifdef F_MODE
328 7
	fclose(f);
329 12
#else
330
	close(fd);
331
#endif
332 7
333
	total_size += st.st_size;
334
	files++;
335
	
336
	gettimeofday(&tv, NULL);
337
	us = (tv.tv_sec - start.tv_sec) * 1000000 + (tv.tv_usec - start.tv_usec);
338 15
	if ((us - last_write) > WRITE_INTERVAL * 1000000) {
339
	    last_write = us;
340
	    printf("Read: %lu files (%lu GB) at %zu MB/s", files, total_size / 1024 / 1024 / 1024, (size_t)(mcoef * total_size / us));
341
342
	    us = (tv.tv_sec - fstart.tv_sec) * 1000000 + (tv.tv_usec - fstart.tv_usec);
343
	    printf(", Last: %s (%lu MB) at %zu MB/s\n", ent->d_name, st.st_size/1024/1024, (size_t)(mcoef * file_size / us));
344
	}
345 7
      }
346
      closedir(dir);
347 15
348
      us = (tv.tv_sec - start.tv_sec) * 1000000 + (tv.tv_usec - start.tv_usec);
349
      printf("Total: %lu files (%lu GB) at %zu MB/s\n", files, total_size / 1024 / 1024 / 1024, (size_t)(mcoef * total_size / us));
350 7
    }
351 11
    
352
    free(buffer);
353 7
354
}

Loggerhead 1.18.1 is a web-based interface for Bazaar branches