/alps/fwbench

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