Commit | Line | Data |
---|---|---|
c9fb4c5b JA |
1 | #include <stdio.h> |
2 | #include <errno.h> | |
3 | #include <assert.h> | |
4 | #include <stdlib.h> | |
5 | #include <stddef.h> | |
6 | #include <signal.h> | |
7 | #include <inttypes.h> | |
932131c9 | 8 | #include <math.h> |
c9fb4c5b | 9 | |
256714ea JA |
10 | #ifdef CONFIG_LIBAIO |
11 | #include <libaio.h> | |
12 | #endif | |
13 | ||
c9fb4c5b JA |
14 | #include <sys/types.h> |
15 | #include <sys/stat.h> | |
16 | #include <sys/ioctl.h> | |
17 | #include <sys/syscall.h> | |
18 | #include <sys/resource.h> | |
c3e2fc25 | 19 | #include <sys/mman.h> |
e31b8288 | 20 | #include <sys/uio.h> |
c9fb4c5b JA |
21 | #include <linux/fs.h> |
22 | #include <fcntl.h> | |
23 | #include <unistd.h> | |
c9fb4c5b JA |
24 | #include <string.h> |
25 | #include <pthread.h> | |
26 | #include <sched.h> | |
27 | ||
74efb029 | 28 | #include "../arch/arch.h" |
57fa61f0 | 29 | #include "../lib/types.h" |
932131c9 JA |
30 | #include "../lib/roundup.h" |
31 | #include "../minmax.h" | |
f3e769a4 | 32 | #include "../os/linux/io_uring.h" |
ac122fea | 33 | |
e31b8288 | 34 | struct io_sq_ring { |
e2239016 JA |
35 | unsigned *head; |
36 | unsigned *tail; | |
37 | unsigned *ring_mask; | |
38 | unsigned *ring_entries; | |
ce1705de | 39 | unsigned *flags; |
e2239016 | 40 | unsigned *array; |
c9fb4c5b JA |
41 | }; |
42 | ||
e31b8288 | 43 | struct io_cq_ring { |
e2239016 JA |
44 | unsigned *head; |
45 | unsigned *tail; | |
46 | unsigned *ring_mask; | |
47 | unsigned *ring_entries; | |
f0403f94 | 48 | struct io_uring_cqe *cqes; |
c9fb4c5b JA |
49 | }; |
50 | ||
701d1277 | 51 | #define DEPTH 128 |
2e7888ef JA |
52 | #define BATCH_SUBMIT 32 |
53 | #define BATCH_COMPLETE 32 | |
c9fb4c5b JA |
54 | #define BS 4096 |
55 | ||
a7086591 JA |
56 | #define MAX_FDS 16 |
57 | ||
c3e2fc25 | 58 | static unsigned sq_ring_mask, cq_ring_mask; |
e39c34dc | 59 | |
a7086591 JA |
60 | struct file { |
61 | unsigned long max_blocks; | |
701d1277 | 62 | unsigned pending_ios; |
48e698fa JA |
63 | int real_fd; |
64 | int fixed_fd; | |
932131c9 | 65 | int fileno; |
a7086591 JA |
66 | }; |
67 | ||
932131c9 JA |
68 | #define PLAT_BITS 6 |
69 | #define PLAT_VAL (1 << PLAT_BITS) | |
70 | #define PLAT_GROUP_NR 29 | |
71 | #define PLAT_NR (PLAT_GROUP_NR * PLAT_VAL) | |
72 | ||
c9fb4c5b JA |
73 | struct submitter { |
74 | pthread_t thread; | |
f310970e | 75 | int ring_fd; |
54319661 | 76 | int index; |
e31b8288 | 77 | struct io_sq_ring sq_ring; |
f0403f94 | 78 | struct io_uring_sqe *sqes; |
e31b8288 | 79 | struct io_cq_ring cq_ring; |
c9fb4c5b | 80 | int inflight; |
932131c9 | 81 | int tid; |
c9fb4c5b JA |
82 | unsigned long reaps; |
83 | unsigned long done; | |
84 | unsigned long calls; | |
85 | volatile int finish; | |
701d1277 | 86 | |
48e698fa JA |
87 | __s32 *fds; |
88 | ||
932131c9 JA |
89 | unsigned long *clock_batch; |
90 | int clock_index; | |
91 | unsigned long *plat; | |
92 | ||
256714ea JA |
93 | #ifdef CONFIG_LIBAIO |
94 | io_context_t aio_ctx; | |
95 | #endif | |
96 | ||
a7086591 JA |
97 | struct file files[MAX_FDS]; |
98 | unsigned nr_files; | |
99 | unsigned cur_file; | |
e39863e3 | 100 | struct iovec iovecs[]; |
c9fb4c5b JA |
101 | }; |
102 | ||
e39863e3 | 103 | static struct submitter *submitter; |
c9fb4c5b | 104 | static volatile int finish; |
52479d8b | 105 | static int stats_running; |
c9fb4c5b | 106 | |
e39863e3 KB |
107 | static int depth = DEPTH; |
108 | static int batch_submit = BATCH_SUBMIT; | |
109 | static int batch_complete = BATCH_COMPLETE; | |
5bd526f2 | 110 | static int bs = BS; |
f0403f94 | 111 | static int polled = 1; /* use IO polling */ |
701d1277 | 112 | static int fixedbufs = 1; /* use fixed user buffers */ |
a71ad043 | 113 | static int dma_map; /* pre-map DMA buffers */ |
8c5fa755 | 114 | static int register_files = 1; /* use fixed files */ |
f0403f94 | 115 | static int buffered = 0; /* use buffered IO, not O_DIRECT */ |
3d7d00a3 JA |
116 | static int sq_thread_poll = 0; /* use kernel submission/poller thread */ |
117 | static int sq_thread_cpu = -1; /* pin above thread to this CPU */ | |
8025517d | 118 | static int do_nop = 0; /* no-op SQ ring commands */ |
54319661 | 119 | static int nthreads = 1; |
932131c9 | 120 | static int stats = 0; /* generate IO stats */ |
256714ea | 121 | static int aio = 0; /* use libaio */ |
65e1a5e8 | 122 | static int runtime = 0; /* runtime */ |
256714ea | 123 | |
932131c9 | 124 | static unsigned long tsc_rate; |
c9fb4c5b | 125 | |
203e4c26 JA |
126 | #define TSC_RATE_FILE "tsc-rate" |
127 | ||
84106576 | 128 | static int vectored = 1; |
b3915995 | 129 | |
932131c9 | 130 | static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, |
ad45a465 | 131 | 80.0, 90.0, 95.0, 99.0, 99.5, 99.9, 99.95, 99.99 }; |
932131c9 JA |
132 | static int plist_len = 17; |
133 | ||
a71ad043 JA |
134 | #ifndef IORING_REGISTER_MAP_BUFFERS |
135 | #define IORING_REGISTER_MAP_BUFFERS 20 | |
136 | struct io_uring_map_buffers { | |
137 | __s32 fd; | |
138 | __u32 buf_start; | |
139 | __u32 buf_end; | |
702b0be2 JA |
140 | __u32 flags; |
141 | __u64 rsvd[2]; | |
a71ad043 JA |
142 | }; |
143 | #endif | |
144 | ||
932131c9 JA |
145 | static unsigned long cycles_to_nsec(unsigned long cycles) |
146 | { | |
147 | uint64_t val; | |
148 | ||
149 | if (!tsc_rate) | |
150 | return cycles; | |
151 | ||
152 | val = cycles * 1000000000ULL; | |
153 | return val / tsc_rate; | |
154 | } | |
155 | ||
156 | static unsigned long plat_idx_to_val(unsigned int idx) | |
157 | { | |
158 | unsigned int error_bits; | |
159 | unsigned long k, base; | |
160 | ||
161 | assert(idx < PLAT_NR); | |
162 | ||
163 | /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use | |
164 | * all bits of the sample as index */ | |
165 | if (idx < (PLAT_VAL << 1)) | |
166 | return cycles_to_nsec(idx); | |
167 | ||
168 | /* Find the group and compute the minimum value of that group */ | |
169 | error_bits = (idx >> PLAT_BITS) - 1; | |
170 | base = ((unsigned long) 1) << (error_bits + PLAT_BITS); | |
171 | ||
172 | /* Find its bucket number of the group */ | |
173 | k = idx % PLAT_VAL; | |
174 | ||
175 | /* Return the mean of the range of the bucket */ | |
176 | return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits))); | |
177 | } | |
178 | ||
179 | unsigned int calc_clat_percentiles(unsigned long *io_u_plat, unsigned long nr, | |
180 | unsigned long **output, | |
181 | unsigned long *maxv, unsigned long *minv) | |
182 | { | |
183 | unsigned long sum = 0; | |
184 | unsigned int len = plist_len, i, j = 0; | |
185 | unsigned long *ovals = NULL; | |
186 | bool is_last; | |
187 | ||
188 | *minv = -1ULL; | |
189 | *maxv = 0; | |
190 | ||
191 | ovals = malloc(len * sizeof(*ovals)); | |
192 | if (!ovals) | |
193 | return 0; | |
194 | ||
195 | /* | |
196 | * Calculate bucket values, note down max and min values | |
197 | */ | |
198 | is_last = false; | |
199 | for (i = 0; i < PLAT_NR && !is_last; i++) { | |
200 | sum += io_u_plat[i]; | |
201 | while (sum >= ((long double) plist[j] / 100.0 * nr)) { | |
202 | assert(plist[j] <= 100.0); | |
203 | ||
204 | ovals[j] = plat_idx_to_val(i); | |
205 | if (ovals[j] < *minv) | |
206 | *minv = ovals[j]; | |
207 | if (ovals[j] > *maxv) | |
208 | *maxv = ovals[j]; | |
209 | ||
210 | is_last = (j == len - 1) != 0; | |
211 | if (is_last) | |
212 | break; | |
213 | ||
214 | j++; | |
215 | } | |
216 | } | |
217 | ||
218 | if (!is_last) | |
219 | fprintf(stderr, "error calculating latency percentiles\n"); | |
220 | ||
221 | *output = ovals; | |
222 | return len; | |
223 | } | |
224 | ||
225 | static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr, | |
226 | unsigned int precision) | |
227 | { | |
228 | unsigned int divisor, len, i, j = 0; | |
229 | unsigned long minv, maxv; | |
230 | unsigned long *ovals; | |
231 | int per_line, scale_down, time_width; | |
232 | bool is_last; | |
233 | char fmt[32]; | |
234 | ||
235 | len = calc_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv); | |
236 | if (!len || !ovals) | |
237 | goto out; | |
238 | ||
239 | if (!tsc_rate) { | |
240 | scale_down = 0; | |
241 | divisor = 1; | |
242 | printf(" percentiles (tsc ticks):\n |"); | |
243 | } else if (minv > 2000 && maxv > 99999) { | |
244 | scale_down = 1; | |
245 | divisor = 1000; | |
246 | printf(" percentiles (usec):\n |"); | |
247 | } else { | |
248 | scale_down = 0; | |
249 | divisor = 1; | |
250 | printf(" percentiles (nsec):\n |"); | |
251 | } | |
252 | ||
253 | time_width = max(5, (int) (log10(maxv / divisor) + 1)); | |
254 | snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3, | |
255 | precision, time_width); | |
256 | /* fmt will be something like " %5.2fth=[%4llu]%c" */ | |
257 | per_line = (80 - 7) / (precision + 10 + time_width); | |
258 | ||
259 | for (j = 0; j < len; j++) { | |
260 | /* for formatting */ | |
261 | if (j != 0 && (j % per_line) == 0) | |
262 | printf(" |"); | |
263 | ||
264 | /* end of the list */ | |
265 | is_last = (j == len - 1) != 0; | |
266 | ||
267 | for (i = 0; i < scale_down; i++) | |
268 | ovals[j] = (ovals[j] + 999) / 1000; | |
269 | ||
270 | printf(fmt, plist[j], ovals[j], is_last ? '\n' : ','); | |
271 | ||
272 | if (is_last) | |
273 | break; | |
274 | ||
275 | if ((j % per_line) == per_line - 1) /* for formatting */ | |
276 | printf("\n"); | |
277 | } | |
278 | ||
279 | out: | |
280 | free(ovals); | |
281 | } | |
282 | ||
283 | static unsigned int plat_val_to_idx(unsigned long val) | |
284 | { | |
285 | unsigned int msb, error_bits, base, offset, idx; | |
286 | ||
287 | /* Find MSB starting from bit 0 */ | |
288 | if (val == 0) | |
289 | msb = 0; | |
290 | else | |
291 | msb = (sizeof(val)*8) - __builtin_clzll(val) - 1; | |
292 | ||
293 | /* | |
294 | * MSB <= (PLAT_BITS-1), cannot be rounded off. Use | |
295 | * all bits of the sample as index | |
296 | */ | |
297 | if (msb <= PLAT_BITS) | |
298 | return val; | |
299 | ||
300 | /* Compute the number of error bits to discard*/ | |
301 | error_bits = msb - PLAT_BITS; | |
302 | ||
303 | /* Compute the number of buckets before the group */ | |
304 | base = (error_bits + 1) << PLAT_BITS; | |
305 | ||
306 | /* | |
307 | * Discard the error bits and apply the mask to find the | |
308 | * index for the buckets in the group | |
309 | */ | |
310 | offset = (PLAT_VAL - 1) & (val >> error_bits); | |
311 | ||
312 | /* Make sure the index does not exceed (array size - 1) */ | |
313 | idx = (base + offset) < (PLAT_NR - 1) ? | |
314 | (base + offset) : (PLAT_NR - 1); | |
315 | ||
316 | return idx; | |
317 | } | |
318 | ||
319 | static void add_stat(struct submitter *s, int clock_index, int nr) | |
320 | { | |
321 | #ifdef ARCH_HAVE_CPU_CLOCK | |
322 | unsigned long cycles; | |
323 | unsigned int pidx; | |
324 | ||
265697fc JA |
325 | if (!s->finish && clock_index) { |
326 | cycles = get_cpu_clock(); | |
327 | cycles -= s->clock_batch[clock_index]; | |
328 | pidx = plat_val_to_idx(cycles); | |
329 | s->plat[pidx] += nr; | |
330 | } | |
932131c9 JA |
331 | #endif |
332 | } | |
333 | ||
a71ad043 JA |
334 | static int io_uring_map_buffers(struct submitter *s) |
335 | { | |
336 | struct io_uring_map_buffers map = { | |
337 | .fd = s->files[0].real_fd, | |
a71ad043 | 338 | .buf_end = depth, |
a71ad043 JA |
339 | }; |
340 | ||
341 | if (do_nop) | |
342 | return 0; | |
343 | if (s->nr_files > 1) { | |
344 | fprintf(stderr, "Can't map buffers with multiple files\n"); | |
345 | return -1; | |
346 | } | |
347 | ||
348 | return syscall(__NR_io_uring_register, s->ring_fd, | |
349 | IORING_REGISTER_MAP_BUFFERS, &map, 1); | |
350 | } | |
351 | ||
2ea53ca3 | 352 | static int io_uring_register_buffers(struct submitter *s) |
c9fb4c5b | 353 | { |
8025517d JA |
354 | if (do_nop) |
355 | return 0; | |
356 | ||
bfed648c | 357 | return syscall(__NR_io_uring_register, s->ring_fd, |
e39863e3 | 358 | IORING_REGISTER_BUFFERS, s->iovecs, depth); |
2ea53ca3 JA |
359 | } |
360 | ||
a7abc9fb JA |
361 | static int io_uring_register_files(struct submitter *s) |
362 | { | |
48e698fa | 363 | int i; |
a7abc9fb | 364 | |
8025517d JA |
365 | if (do_nop) |
366 | return 0; | |
367 | ||
48e698fa JA |
368 | s->fds = calloc(s->nr_files, sizeof(__s32)); |
369 | for (i = 0; i < s->nr_files; i++) { | |
370 | s->fds[i] = s->files[i].real_fd; | |
371 | s->files[i].fixed_fd = i; | |
372 | } | |
a7abc9fb | 373 | |
bfed648c | 374 | return syscall(__NR_io_uring_register, s->ring_fd, |
919850d2 | 375 | IORING_REGISTER_FILES, s->fds, s->nr_files); |
a7abc9fb JA |
376 | } |
377 | ||
2ea53ca3 JA |
378 | static int io_uring_setup(unsigned entries, struct io_uring_params *p) |
379 | { | |
bfed648c | 380 | return syscall(__NR_io_uring_setup, entries, p); |
c9fb4c5b JA |
381 | } |
382 | ||
b3915995 JA |
383 | static void io_uring_probe(int fd) |
384 | { | |
385 | struct io_uring_probe *p; | |
386 | int ret; | |
387 | ||
388 | p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op)); | |
389 | if (!p) | |
390 | return; | |
391 | ||
392 | memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op)); | |
393 | ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256); | |
394 | if (ret < 0) | |
395 | goto out; | |
396 | ||
397 | if (IORING_OP_READ > p->ops_len) | |
398 | goto out; | |
399 | ||
400 | if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED)) | |
84106576 | 401 | vectored = 0; |
b3915995 JA |
402 | out: |
403 | free(p); | |
404 | } | |
405 | ||
c3e2fc25 JA |
406 | static int io_uring_enter(struct submitter *s, unsigned int to_submit, |
407 | unsigned int min_complete, unsigned int flags) | |
c9fb4c5b | 408 | { |
bfed648c JA |
409 | return syscall(__NR_io_uring_enter, s->ring_fd, to_submit, min_complete, |
410 | flags, NULL, 0); | |
c9fb4c5b JA |
411 | } |
412 | ||
77d814a1 | 413 | #ifndef CONFIG_HAVE_GETTID |
c9fb4c5b JA |
414 | static int gettid(void) |
415 | { | |
416 | return syscall(__NR_gettid); | |
417 | } | |
77d814a1 | 418 | #endif |
c9fb4c5b | 419 | |
701d1277 JA |
420 | static unsigned file_depth(struct submitter *s) |
421 | { | |
e39863e3 | 422 | return (depth + s->nr_files - 1) / s->nr_files; |
701d1277 JA |
423 | } |
424 | ||
a7086591 | 425 | static void init_io(struct submitter *s, unsigned index) |
c9fb4c5b | 426 | { |
f0403f94 | 427 | struct io_uring_sqe *sqe = &s->sqes[index]; |
c9fb4c5b | 428 | unsigned long offset; |
a7086591 | 429 | struct file *f; |
c9fb4c5b JA |
430 | long r; |
431 | ||
8025517d JA |
432 | if (do_nop) { |
433 | sqe->opcode = IORING_OP_NOP; | |
434 | return; | |
435 | } | |
436 | ||
701d1277 JA |
437 | if (s->nr_files == 1) { |
438 | f = &s->files[0]; | |
439 | } else { | |
440 | f = &s->files[s->cur_file]; | |
441 | if (f->pending_ios >= file_depth(s)) { | |
442 | s->cur_file++; | |
443 | if (s->cur_file == s->nr_files) | |
444 | s->cur_file = 0; | |
93d1811c | 445 | f = &s->files[s->cur_file]; |
701d1277 JA |
446 | } |
447 | } | |
448 | f->pending_ios++; | |
a7086591 | 449 | |
5e8865c0 | 450 | r = lrand48(); |
5bd526f2 | 451 | offset = (r % (f->max_blocks - 1)) * bs; |
c9fb4c5b | 452 | |
8c5fa755 JA |
453 | if (register_files) { |
454 | sqe->flags = IOSQE_FIXED_FILE; | |
455 | sqe->fd = f->fixed_fd; | |
456 | } else { | |
457 | sqe->flags = 0; | |
458 | sqe->fd = f->real_fd; | |
459 | } | |
f0403f94 | 460 | if (fixedbufs) { |
48e698fa | 461 | sqe->opcode = IORING_OP_READ_FIXED; |
919850d2 | 462 | sqe->addr = (unsigned long) s->iovecs[index].iov_base; |
5bd526f2 | 463 | sqe->len = bs; |
2ea53ca3 | 464 | sqe->buf_index = index; |
84106576 | 465 | } else if (!vectored) { |
b3915995 JA |
466 | sqe->opcode = IORING_OP_READ; |
467 | sqe->addr = (unsigned long) s->iovecs[index].iov_base; | |
468 | sqe->len = bs; | |
469 | sqe->buf_index = 0; | |
84106576 JA |
470 | } else { |
471 | sqe->opcode = IORING_OP_READV; | |
472 | sqe->addr = (unsigned long) &s->iovecs[index]; | |
473 | sqe->len = 1; | |
474 | sqe->buf_index = 0; | |
f0403f94 | 475 | } |
f0403f94 | 476 | sqe->ioprio = 0; |
f0403f94 | 477 | sqe->off = offset; |
932131c9 | 478 | sqe->user_data = (unsigned long) f->fileno; |
52479d8b | 479 | if (stats && stats_running) |
932131c9 | 480 | sqe->user_data |= ((unsigned long)s->clock_index << 32); |
c9fb4c5b JA |
481 | } |
482 | ||
256714ea | 483 | static int prep_more_ios_uring(struct submitter *s, int max_ios) |
c9fb4c5b | 484 | { |
e31b8288 | 485 | struct io_sq_ring *ring = &s->sq_ring; |
e2239016 | 486 | unsigned index, tail, next_tail, prepped = 0; |
c9fb4c5b | 487 | |
c3e2fc25 | 488 | next_tail = tail = *ring->tail; |
c9fb4c5b JA |
489 | do { |
490 | next_tail++; | |
fc2dc21b | 491 | if (next_tail == atomic_load_acquire(ring->head)) |
c9fb4c5b JA |
492 | break; |
493 | ||
e39c34dc | 494 | index = tail & sq_ring_mask; |
a7086591 | 495 | init_io(s, index); |
c3e2fc25 | 496 | ring->array[index] = index; |
c9fb4c5b JA |
497 | prepped++; |
498 | tail = next_tail; | |
499 | } while (prepped < max_ios); | |
500 | ||
fc2dc21b JA |
501 | if (prepped) |
502 | atomic_store_release(ring->tail, tail); | |
c9fb4c5b JA |
503 | return prepped; |
504 | } | |
505 | ||
a7086591 | 506 | static int get_file_size(struct file *f) |
c9fb4c5b JA |
507 | { |
508 | struct stat st; | |
509 | ||
48e698fa | 510 | if (fstat(f->real_fd, &st) < 0) |
c9fb4c5b JA |
511 | return -1; |
512 | if (S_ISBLK(st.st_mode)) { | |
513 | unsigned long long bytes; | |
514 | ||
48e698fa | 515 | if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0) |
c9fb4c5b JA |
516 | return -1; |
517 | ||
5bd526f2 | 518 | f->max_blocks = bytes / bs; |
c9fb4c5b JA |
519 | return 0; |
520 | } else if (S_ISREG(st.st_mode)) { | |
5bd526f2 | 521 | f->max_blocks = st.st_size / bs; |
c9fb4c5b JA |
522 | return 0; |
523 | } | |
524 | ||
525 | return -1; | |
526 | } | |
527 | ||
256714ea | 528 | static int reap_events_uring(struct submitter *s) |
c9fb4c5b | 529 | { |
e31b8288 | 530 | struct io_cq_ring *ring = &s->cq_ring; |
f0403f94 | 531 | struct io_uring_cqe *cqe; |
e2239016 | 532 | unsigned head, reaped = 0; |
ab85494f | 533 | int last_idx = -1, stat_nr = 0; |
c9fb4c5b | 534 | |
c3e2fc25 | 535 | head = *ring->head; |
c9fb4c5b | 536 | do { |
701d1277 JA |
537 | struct file *f; |
538 | ||
679d8352 | 539 | read_barrier(); |
fc2dc21b | 540 | if (head == atomic_load_acquire(ring->tail)) |
c9fb4c5b | 541 | break; |
f0403f94 | 542 | cqe = &ring->cqes[head & cq_ring_mask]; |
8025517d | 543 | if (!do_nop) { |
932131c9 JA |
544 | int fileno = cqe->user_data & 0xffffffff; |
545 | ||
546 | f = &s->files[fileno]; | |
8025517d | 547 | f->pending_ios--; |
5bd526f2 | 548 | if (cqe->res != bs) { |
8025517d | 549 | printf("io: unexpected ret=%d\n", cqe->res); |
154a9582 | 550 | if (polled && cqe->res == -EOPNOTSUPP) |
8066f6b6 | 551 | printf("Your filesystem/driver/kernel doesn't support polled IO\n"); |
8025517d JA |
552 | return -1; |
553 | } | |
c9fb4c5b | 554 | } |
932131c9 JA |
555 | if (stats) { |
556 | int clock_index = cqe->user_data >> 32; | |
557 | ||
ab85494f JA |
558 | if (last_idx != clock_index) { |
559 | if (last_idx != -1) { | |
560 | add_stat(s, last_idx, stat_nr); | |
561 | stat_nr = 0; | |
562 | } | |
563 | last_idx = clock_index; | |
d4af2ece JA |
564 | } |
565 | stat_nr++; | |
932131c9 | 566 | } |
c9fb4c5b JA |
567 | reaped++; |
568 | head++; | |
c9fb4c5b JA |
569 | } while (1); |
570 | ||
ab85494f JA |
571 | if (stat_nr) |
572 | add_stat(s, last_idx, stat_nr); | |
573 | ||
fc2dc21b JA |
574 | if (reaped) { |
575 | s->inflight -= reaped; | |
576 | atomic_store_release(ring->head, head); | |
577 | } | |
c9fb4c5b JA |
578 | return reaped; |
579 | } | |
580 | ||
256714ea | 581 | static int submitter_init(struct submitter *s) |
c9fb4c5b | 582 | { |
256714ea | 583 | int i, nr_batch; |
c9fb4c5b | 584 | |
932131c9 | 585 | s->tid = gettid(); |
7e847fd0 | 586 | printf("submitter=%d, tid=%d\n", s->index, s->tid); |
c9fb4c5b | 587 | |
5e8865c0 | 588 | srand48(pthread_self()); |
c9fb4c5b | 589 | |
932131c9 JA |
590 | for (i = 0; i < MAX_FDS; i++) |
591 | s->files[i].fileno = i; | |
592 | ||
593 | if (stats) { | |
594 | nr_batch = roundup_pow2(depth / batch_submit); | |
d4af2ece JA |
595 | if (nr_batch < 2) |
596 | nr_batch = 2; | |
932131c9 | 597 | s->clock_batch = calloc(nr_batch, sizeof(unsigned long)); |
52479d8b | 598 | s->clock_index = 1; |
932131c9 JA |
599 | |
600 | s->plat = calloc(PLAT_NR, sizeof(unsigned long)); | |
601 | } else { | |
602 | s->clock_batch = NULL; | |
603 | s->plat = NULL; | |
604 | nr_batch = 0; | |
605 | } | |
606 | ||
256714ea JA |
607 | return nr_batch; |
608 | } | |
609 | ||
610 | #ifdef CONFIG_LIBAIO | |
611 | static int prep_more_ios_aio(struct submitter *s, int max_ios, struct iocb *iocbs) | |
612 | { | |
613 | unsigned long offset, data; | |
614 | struct file *f; | |
615 | unsigned index; | |
616 | long r; | |
617 | ||
618 | index = 0; | |
619 | while (index < max_ios) { | |
620 | struct iocb *iocb = &iocbs[index]; | |
621 | ||
622 | if (s->nr_files == 1) { | |
623 | f = &s->files[0]; | |
624 | } else { | |
625 | f = &s->files[s->cur_file]; | |
626 | if (f->pending_ios >= file_depth(s)) { | |
627 | s->cur_file++; | |
628 | if (s->cur_file == s->nr_files) | |
629 | s->cur_file = 0; | |
630 | f = &s->files[s->cur_file]; | |
631 | } | |
632 | } | |
633 | f->pending_ios++; | |
634 | ||
635 | r = lrand48(); | |
636 | offset = (r % (f->max_blocks - 1)) * bs; | |
637 | io_prep_pread(iocb, f->real_fd, s->iovecs[index].iov_base, | |
638 | s->iovecs[index].iov_len, offset); | |
639 | ||
640 | data = f->fileno; | |
52479d8b | 641 | if (stats && stats_running) |
256714ea JA |
642 | data |= ((unsigned long) s->clock_index << 32); |
643 | iocb->data = (void *) (uintptr_t) data; | |
644 | index++; | |
645 | } | |
646 | return index; | |
647 | } | |
648 | ||
649 | static int reap_events_aio(struct submitter *s, struct io_event *events, int evs) | |
650 | { | |
651 | int last_idx = -1, stat_nr = 0; | |
652 | int reaped = 0; | |
653 | ||
654 | while (evs) { | |
655 | unsigned long data = (uintptr_t) events[reaped].data; | |
656 | struct file *f = &s->files[data & 0xffffffff]; | |
657 | ||
658 | f->pending_ios--; | |
659 | if (events[reaped].res != bs) { | |
660 | printf("io: unexpected ret=%ld\n", events[reaped].res); | |
661 | return -1; | |
662 | } | |
663 | if (stats) { | |
664 | int clock_index = data >> 32; | |
665 | ||
666 | if (last_idx != clock_index) { | |
667 | if (last_idx != -1) { | |
668 | add_stat(s, last_idx, stat_nr); | |
669 | stat_nr = 0; | |
670 | } | |
671 | last_idx = clock_index; | |
d4af2ece JA |
672 | } |
673 | stat_nr++; | |
256714ea JA |
674 | } |
675 | reaped++; | |
676 | evs--; | |
677 | } | |
678 | ||
679 | if (stat_nr) | |
680 | add_stat(s, last_idx, stat_nr); | |
681 | ||
682 | s->inflight -= reaped; | |
683 | s->done += reaped; | |
684 | return reaped; | |
685 | } | |
686 | ||
687 | static void *submitter_aio_fn(void *data) | |
688 | { | |
689 | struct submitter *s = data; | |
690 | int i, ret, prepped, nr_batch; | |
691 | struct iocb **iocbsptr; | |
692 | struct iocb *iocbs; | |
693 | struct io_event *events; | |
694 | ||
695 | nr_batch = submitter_init(s); | |
696 | ||
697 | iocbsptr = calloc(depth, sizeof(struct iocb *)); | |
698 | iocbs = calloc(depth, sizeof(struct iocb)); | |
699 | events = calloc(depth, sizeof(struct io_event)); | |
700 | ||
701 | for (i = 0; i < depth; i++) | |
702 | iocbsptr[i] = &iocbs[i]; | |
703 | ||
704 | prepped = 0; | |
705 | do { | |
706 | int to_wait, to_submit, to_prep; | |
707 | ||
708 | if (!prepped && s->inflight < depth) { | |
709 | to_prep = min(depth - s->inflight, batch_submit); | |
710 | prepped = prep_more_ios_aio(s, to_prep, iocbs); | |
711 | #ifdef ARCH_HAVE_CPU_CLOCK | |
712 | if (prepped && stats) { | |
713 | s->clock_batch[s->clock_index] = get_cpu_clock(); | |
714 | s->clock_index = (s->clock_index + 1) & (nr_batch - 1); | |
715 | } | |
716 | #endif | |
717 | } | |
718 | s->inflight += prepped; | |
719 | to_submit = prepped; | |
720 | ||
721 | if (to_submit && (s->inflight + to_submit <= depth)) | |
722 | to_wait = 0; | |
723 | else | |
724 | to_wait = min(s->inflight + to_submit, batch_complete); | |
725 | ||
726 | ret = io_submit(s->aio_ctx, to_submit, iocbsptr); | |
727 | s->calls++; | |
728 | if (ret < 0) { | |
729 | perror("io_submit"); | |
730 | break; | |
731 | } else if (ret != to_submit) { | |
732 | printf("submitted %d, wanted %d\n", ret, to_submit); | |
733 | break; | |
734 | } | |
735 | prepped = 0; | |
736 | ||
24a24c12 | 737 | while (to_wait) { |
256714ea JA |
738 | int r; |
739 | ||
24a24c12 JA |
740 | s->calls++; |
741 | r = io_getevents(s->aio_ctx, to_wait, to_wait, events, NULL); | |
742 | if (r < 0) { | |
743 | perror("io_getevents"); | |
744 | break; | |
745 | } else if (r != to_wait) { | |
746 | printf("r=%d, wait=%d\n", r, to_wait); | |
747 | break; | |
748 | } | |
749 | r = reap_events_aio(s, events, r); | |
750 | s->reaps += r; | |
751 | to_wait -= r; | |
256714ea JA |
752 | } |
753 | } while (!s->finish); | |
754 | ||
755 | free(iocbsptr); | |
756 | free(iocbs); | |
757 | free(events); | |
758 | finish = 1; | |
759 | return NULL; | |
760 | } | |
761 | #endif | |
762 | ||
763 | static void *submitter_uring_fn(void *data) | |
764 | { | |
765 | struct submitter *s = data; | |
766 | struct io_sq_ring *ring = &s->sq_ring; | |
767 | int ret, prepped, nr_batch; | |
768 | ||
769 | nr_batch = submitter_init(s); | |
770 | ||
c9fb4c5b JA |
771 | prepped = 0; |
772 | do { | |
f310970e | 773 | int to_wait, to_submit, this_reap, to_prep; |
fc2dc21b | 774 | unsigned ring_flags = 0; |
c9fb4c5b | 775 | |
e39863e3 KB |
776 | if (!prepped && s->inflight < depth) { |
777 | to_prep = min(depth - s->inflight, batch_submit); | |
256714ea | 778 | prepped = prep_more_ios_uring(s, to_prep); |
932131c9 JA |
779 | #ifdef ARCH_HAVE_CPU_CLOCK |
780 | if (prepped && stats) { | |
781 | s->clock_batch[s->clock_index] = get_cpu_clock(); | |
782 | s->clock_index = (s->clock_index + 1) & (nr_batch - 1); | |
783 | } | |
784 | #endif | |
f310970e | 785 | } |
c9fb4c5b JA |
786 | s->inflight += prepped; |
787 | submit_more: | |
788 | to_submit = prepped; | |
789 | submit: | |
e39863e3 | 790 | if (to_submit && (s->inflight + to_submit <= depth)) |
c9fb4c5b JA |
791 | to_wait = 0; |
792 | else | |
e39863e3 | 793 | to_wait = min(s->inflight + to_submit, batch_complete); |
c9fb4c5b | 794 | |
ce1705de JA |
795 | /* |
796 | * Only need to call io_uring_enter if we're not using SQ thread | |
797 | * poll, or if IORING_SQ_NEED_WAKEUP is set. | |
798 | */ | |
fc2dc21b JA |
799 | if (sq_thread_poll) |
800 | ring_flags = atomic_load_acquire(ring->flags); | |
801 | if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) { | |
e0abe388 JA |
802 | unsigned flags = 0; |
803 | ||
804 | if (to_wait) | |
805 | flags = IORING_ENTER_GETEVENTS; | |
fc2dc21b | 806 | if (ring_flags & IORING_SQ_NEED_WAKEUP) |
b532dd6d | 807 | flags |= IORING_ENTER_SQ_WAKEUP; |
e0abe388 | 808 | ret = io_uring_enter(s, to_submit, to_wait, flags); |
ce1705de | 809 | s->calls++; |
fc2dc21b JA |
810 | } else { |
811 | /* for SQPOLL, we submitted it all effectively */ | |
812 | ret = to_submit; | |
ce1705de | 813 | } |
c9fb4c5b | 814 | |
ce1705de JA |
815 | /* |
816 | * For non SQ thread poll, we already got the events we needed | |
817 | * through the io_uring_enter() above. For SQ thread poll, we | |
818 | * need to loop here until we find enough events. | |
819 | */ | |
820 | this_reap = 0; | |
821 | do { | |
822 | int r; | |
256714ea JA |
823 | |
824 | r = reap_events_uring(s); | |
7d1435e6 JA |
825 | if (r == -1) { |
826 | s->finish = 1; | |
ce1705de | 827 | break; |
7d1435e6 | 828 | } else if (r > 0) |
ce1705de JA |
829 | this_reap += r; |
830 | } while (sq_thread_poll && this_reap < to_wait); | |
c9fb4c5b JA |
831 | s->reaps += this_reap; |
832 | ||
833 | if (ret >= 0) { | |
834 | if (!ret) { | |
835 | to_submit = 0; | |
836 | if (s->inflight) | |
837 | goto submit; | |
838 | continue; | |
839 | } else if (ret < to_submit) { | |
840 | int diff = to_submit - ret; | |
841 | ||
842 | s->done += ret; | |
843 | prepped -= diff; | |
844 | goto submit_more; | |
845 | } | |
846 | s->done += ret; | |
847 | prepped = 0; | |
848 | continue; | |
849 | } else if (ret < 0) { | |
ac122fea | 850 | if (errno == EAGAIN) { |
c9fb4c5b JA |
851 | if (s->finish) |
852 | break; | |
853 | if (this_reap) | |
854 | goto submit; | |
c9fb4c5b JA |
855 | to_submit = 0; |
856 | goto submit; | |
857 | } | |
ac122fea | 858 | printf("io_submit: %s\n", strerror(errno)); |
c9fb4c5b JA |
859 | break; |
860 | } | |
861 | } while (!s->finish); | |
a7086591 | 862 | |
c9fb4c5b JA |
863 | finish = 1; |
864 | return NULL; | |
865 | } | |
866 | ||
54319661 JA |
867 | static struct submitter *get_submitter(int offset) |
868 | { | |
869 | void *ret; | |
870 | ||
871 | ret = submitter; | |
872 | if (offset) | |
873 | ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec)); | |
874 | return ret; | |
875 | } | |
876 | ||
65e1a5e8 | 877 | static void do_finish(const char *reason) |
c9fb4c5b | 878 | { |
54319661 | 879 | int j; |
65e1a5e8 | 880 | printf("Exiting on %s\n", reason); |
54319661 JA |
881 | for (j = 0; j < nthreads; j++) { |
882 | struct submitter *s = get_submitter(j); | |
883 | s->finish = 1; | |
884 | } | |
c9fb4c5b JA |
885 | finish = 1; |
886 | } | |
887 | ||
65e1a5e8 EV |
888 | static void sig_int(int sig) |
889 | { | |
890 | do_finish("signal"); | |
891 | } | |
892 | ||
c9fb4c5b JA |
893 | static void arm_sig_int(void) |
894 | { | |
895 | struct sigaction act; | |
896 | ||
897 | memset(&act, 0, sizeof(act)); | |
898 | act.sa_handler = sig_int; | |
899 | act.sa_flags = SA_RESTART; | |
900 | sigaction(SIGINT, &act, NULL); | |
2cf71009 BP |
901 | |
902 | /* Windows uses SIGBREAK as a quit signal from other applications */ | |
903 | #ifdef WIN32 | |
904 | sigaction(SIGBREAK, &act, NULL); | |
905 | #endif | |
c9fb4c5b JA |
906 | } |
907 | ||
256714ea JA |
908 | static int setup_aio(struct submitter *s) |
909 | { | |
910 | #ifdef CONFIG_LIBAIO | |
911 | if (polled) { | |
912 | fprintf(stderr, "aio does not support polled IO\n"); | |
913 | polled = 0; | |
914 | } | |
915 | if (sq_thread_poll) { | |
916 | fprintf(stderr, "aio does not support SQPOLL IO\n"); | |
917 | sq_thread_poll = 0; | |
918 | } | |
919 | if (do_nop) { | |
920 | fprintf(stderr, "aio does not support polled IO\n"); | |
921 | do_nop = 0; | |
922 | } | |
923 | if (fixedbufs || register_files) { | |
924 | fprintf(stderr, "aio does not support registered files or buffers\n"); | |
925 | fixedbufs = register_files = 0; | |
926 | } | |
927 | ||
928 | return io_queue_init(depth, &s->aio_ctx); | |
929 | #else | |
930 | fprintf(stderr, "Legacy AIO not available on this system/build\n"); | |
931 | errno = EINVAL; | |
932 | return -1; | |
933 | #endif | |
934 | } | |
935 | ||
c3e2fc25 JA |
936 | static int setup_ring(struct submitter *s) |
937 | { | |
e31b8288 JA |
938 | struct io_sq_ring *sring = &s->sq_ring; |
939 | struct io_cq_ring *cring = &s->cq_ring; | |
940 | struct io_uring_params p; | |
2ea53ca3 | 941 | int ret, fd; |
c3e2fc25 | 942 | void *ptr; |
c3e2fc25 JA |
943 | |
944 | memset(&p, 0, sizeof(p)); | |
945 | ||
7d1435e6 | 946 | if (polled && !do_nop) |
0e47f11b | 947 | p.flags |= IORING_SETUP_IOPOLL; |
3d7d00a3 | 948 | if (sq_thread_poll) { |
2ea53ca3 | 949 | p.flags |= IORING_SETUP_SQPOLL; |
3ade18a3 | 950 | if (sq_thread_cpu != -1) { |
3d7d00a3 | 951 | p.flags |= IORING_SETUP_SQ_AFF; |
3ade18a3 JA |
952 | p.sq_thread_cpu = sq_thread_cpu; |
953 | } | |
c3e2fc25 JA |
954 | } |
955 | ||
e39863e3 | 956 | fd = io_uring_setup(depth, &p); |
c3e2fc25 JA |
957 | if (fd < 0) { |
958 | perror("io_uring_setup"); | |
959 | return 1; | |
960 | } | |
f310970e | 961 | s->ring_fd = fd; |
2ea53ca3 | 962 | |
b3915995 JA |
963 | io_uring_probe(fd); |
964 | ||
2ea53ca3 | 965 | if (fixedbufs) { |
15130e3f JA |
966 | struct rlimit rlim; |
967 | ||
968 | rlim.rlim_cur = RLIM_INFINITY; | |
969 | rlim.rlim_max = RLIM_INFINITY; | |
970 | /* ignore potential error, not needed on newer kernels */ | |
971 | setrlimit(RLIMIT_MEMLOCK, &rlim); | |
972 | ||
2ea53ca3 JA |
973 | ret = io_uring_register_buffers(s); |
974 | if (ret < 0) { | |
a7abc9fb | 975 | perror("io_uring_register_buffers"); |
2ea53ca3 JA |
976 | return 1; |
977 | } | |
a71ad043 JA |
978 | |
979 | if (dma_map) { | |
980 | ret = io_uring_map_buffers(s); | |
981 | if (ret < 0) { | |
982 | perror("io_uring_map_buffers"); | |
983 | return 1; | |
984 | } | |
985 | } | |
2ea53ca3 JA |
986 | } |
987 | ||
8c5fa755 JA |
988 | if (register_files) { |
989 | ret = io_uring_register_files(s); | |
990 | if (ret < 0) { | |
991 | perror("io_uring_register_files"); | |
992 | return 1; | |
993 | } | |
a7abc9fb JA |
994 | } |
995 | ||
e2239016 | 996 | ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32), |
f310970e JA |
997 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
998 | IORING_OFF_SQ_RING); | |
c3e2fc25 JA |
999 | sring->head = ptr + p.sq_off.head; |
1000 | sring->tail = ptr + p.sq_off.tail; | |
1001 | sring->ring_mask = ptr + p.sq_off.ring_mask; | |
1002 | sring->ring_entries = ptr + p.sq_off.ring_entries; | |
ce1705de | 1003 | sring->flags = ptr + p.sq_off.flags; |
ac122fea | 1004 | sring->array = ptr + p.sq_off.array; |
c3e2fc25 JA |
1005 | sq_ring_mask = *sring->ring_mask; |
1006 | ||
f0403f94 | 1007 | s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe), |
f310970e | 1008 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
f0403f94 | 1009 | IORING_OFF_SQES); |
c3e2fc25 | 1010 | |
f0403f94 | 1011 | ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe), |
f310970e JA |
1012 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
1013 | IORING_OFF_CQ_RING); | |
c3e2fc25 JA |
1014 | cring->head = ptr + p.cq_off.head; |
1015 | cring->tail = ptr + p.cq_off.tail; | |
1016 | cring->ring_mask = ptr + p.cq_off.ring_mask; | |
1017 | cring->ring_entries = ptr + p.cq_off.ring_entries; | |
f0403f94 | 1018 | cring->cqes = ptr + p.cq_off.cqes; |
c3e2fc25 JA |
1019 | cq_ring_mask = *cring->ring_mask; |
1020 | return 0; | |
1021 | } | |
1022 | ||
2e7888ef JA |
1023 | static void file_depths(char *buf) |
1024 | { | |
26976a13 | 1025 | bool prev = false; |
2e7888ef | 1026 | char *p; |
54319661 | 1027 | int i, j; |
2e7888ef | 1028 | |
ac21bfab | 1029 | buf[0] = '\0'; |
2e7888ef | 1030 | p = buf; |
54319661 JA |
1031 | for (j = 0; j < nthreads; j++) { |
1032 | struct submitter *s = get_submitter(j); | |
2e7888ef | 1033 | |
54319661 JA |
1034 | for (i = 0; i < s->nr_files; i++) { |
1035 | struct file *f = &s->files[i]; | |
1036 | ||
26976a13 JA |
1037 | if (prev) |
1038 | p += sprintf(p, " %d", f->pending_ios); | |
54319661 | 1039 | else |
4f215227 | 1040 | p += sprintf(p, "%d", f->pending_ios); |
26976a13 | 1041 | prev = true; |
54319661 | 1042 | } |
2e7888ef JA |
1043 | } |
1044 | } | |
1045 | ||
d79ff8c9 | 1046 | static void usage(char *argv, int status) |
e39863e3 | 1047 | { |
65e1a5e8 EV |
1048 | char runtime_str[16]; |
1049 | snprintf(runtime_str, sizeof(runtime_str), "%d", runtime); | |
e39863e3 | 1050 | printf("%s [options] -- [filenames]\n" |
35268e11 AJ |
1051 | " -d <int> : IO Depth, default %d\n" |
1052 | " -s <int> : Batch submit, default %d\n" | |
1053 | " -c <int> : Batch complete, default %d\n" | |
1054 | " -b <int> : Block size, default %d\n" | |
1055 | " -p <bool> : Polled IO, default %d\n" | |
1056 | " -B <bool> : Fixed buffers, default %d\n" | |
a71ad043 | 1057 | " -R <bool> : DMA map fixed buffers, default %d\n" |
35268e11 | 1058 | " -F <bool> : Register files, default %d\n" |
0862f718 | 1059 | " -n <int> : Number of threads, default %d\n" |
2686fc22 | 1060 | " -O <bool> : Use O_DIRECT, default %d\n" |
4a39c524 EV |
1061 | " -N <bool> : Perform just no-op requests, default %d\n" |
1062 | " -t <bool> : Track IO latencies, default %d\n" | |
256714ea | 1063 | " -T <int> : TSC rate in HZ\n" |
65e1a5e8 EV |
1064 | " -a <bool> : Use legacy aio, default %d\n" |
1065 | " -r <int> : Runtime in seconds, default %s\n", | |
35268e11 | 1066 | argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled, |
a71ad043 JA |
1067 | fixedbufs, dma_map, register_files, nthreads, !buffered, do_nop, |
1068 | stats, aio, runtime == 0 ? "unlimited" : runtime_str); | |
d79ff8c9 | 1069 | exit(status); |
e39863e3 KB |
1070 | } |
1071 | ||
203e4c26 JA |
1072 | static void read_tsc_rate(void) |
1073 | { | |
1074 | char buffer[32]; | |
1075 | int fd, ret; | |
1076 | ||
1077 | if (tsc_rate) | |
1078 | return; | |
1079 | ||
1080 | fd = open(TSC_RATE_FILE, O_RDONLY); | |
1081 | if (fd < 0) | |
1082 | return; | |
1083 | ||
1084 | ret = read(fd, buffer, sizeof(buffer)); | |
1085 | if (ret < 0) { | |
1086 | close(fd); | |
1087 | return; | |
1088 | } | |
1089 | ||
1090 | tsc_rate = strtoul(buffer, NULL, 10); | |
1091 | printf("Using TSC rate %luHz\n", tsc_rate); | |
1092 | close(fd); | |
1093 | } | |
1094 | ||
1095 | static void write_tsc_rate(void) | |
1096 | { | |
1097 | char buffer[32]; | |
1098 | struct stat sb; | |
1099 | int fd, ret; | |
1100 | ||
1101 | if (!stat(TSC_RATE_FILE, &sb)) | |
1102 | return; | |
1103 | ||
1104 | fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644); | |
1105 | if (fd < 0) | |
1106 | return; | |
1107 | ||
1108 | memset(buffer, 0, sizeof(buffer)); | |
1109 | sprintf(buffer, "%lu", tsc_rate); | |
1110 | ret = write(fd, buffer, strlen(buffer)); | |
1111 | if (ret < 0) | |
1112 | perror("write"); | |
1113 | close(fd); | |
1114 | } | |
1115 | ||
c9fb4c5b JA |
1116 | int main(int argc, char *argv[]) |
1117 | { | |
e39863e3 | 1118 | struct submitter *s; |
05138221 | 1119 | unsigned long done, calls, reap; |
d79ff8c9 AJ |
1120 | int err, i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles; |
1121 | struct file f; | |
2e7888ef | 1122 | char *fdepths; |
c3e2fc25 | 1123 | void *ret; |
c9fb4c5b | 1124 | |
0862f718 JA |
1125 | if (!do_nop && argc < 2) |
1126 | usage(argv[0], 1); | |
c9fb4c5b | 1127 | |
a71ad043 | 1128 | while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:N:O:t:T:a:r:D:h?")) != -1) { |
e39863e3 | 1129 | switch (opt) { |
256714ea JA |
1130 | case 'a': |
1131 | aio = !!atoi(optarg); | |
1132 | break; | |
e39863e3 KB |
1133 | case 'd': |
1134 | depth = atoi(optarg); | |
1135 | break; | |
1136 | case 's': | |
1137 | batch_submit = atoi(optarg); | |
932131c9 JA |
1138 | if (!batch_submit) |
1139 | batch_submit = 1; | |
e39863e3 KB |
1140 | break; |
1141 | case 'c': | |
1142 | batch_complete = atoi(optarg); | |
932131c9 JA |
1143 | if (!batch_complete) |
1144 | batch_complete = 1; | |
e39863e3 | 1145 | break; |
5bd526f2 JA |
1146 | case 'b': |
1147 | bs = atoi(optarg); | |
1148 | break; | |
1149 | case 'p': | |
1150 | polled = !!atoi(optarg); | |
1151 | break; | |
6a87c3b0 JA |
1152 | case 'B': |
1153 | fixedbufs = !!atoi(optarg); | |
1154 | break; | |
1155 | case 'F': | |
1156 | register_files = !!atoi(optarg); | |
1157 | break; | |
54319661 JA |
1158 | case 'n': |
1159 | nthreads = atoi(optarg); | |
caf2b9ac JA |
1160 | if (!nthreads) { |
1161 | printf("Threads must be non-zero\n"); | |
1162 | usage(argv[0], 1); | |
1163 | } | |
54319661 | 1164 | break; |
0862f718 JA |
1165 | case 'N': |
1166 | do_nop = !!atoi(optarg); | |
1167 | break; | |
2686fc22 JA |
1168 | case 'O': |
1169 | buffered = !atoi(optarg); | |
1170 | break; | |
932131c9 JA |
1171 | case 't': |
1172 | #ifndef ARCH_HAVE_CPU_CLOCK | |
1173 | fprintf(stderr, "Stats not supported on this CPU\n"); | |
1174 | return 1; | |
1175 | #endif | |
1176 | stats = !!atoi(optarg); | |
1177 | break; | |
1178 | case 'T': | |
1179 | #ifndef ARCH_HAVE_CPU_CLOCK | |
1180 | fprintf(stderr, "Stats not supported on this CPU\n"); | |
1181 | return 1; | |
1182 | #endif | |
1183 | tsc_rate = strtoul(optarg, NULL, 10); | |
203e4c26 | 1184 | write_tsc_rate(); |
932131c9 | 1185 | break; |
65e1a5e8 EV |
1186 | case 'r': |
1187 | runtime = atoi(optarg); | |
1188 | break; | |
a71ad043 JA |
1189 | case 'D': |
1190 | dma_map = !!atoi(optarg); | |
1191 | break; | |
e39863e3 KB |
1192 | case 'h': |
1193 | case '?': | |
1194 | default: | |
d79ff8c9 | 1195 | usage(argv[0], 0); |
e39863e3 KB |
1196 | break; |
1197 | } | |
1198 | } | |
1199 | ||
203e4c26 JA |
1200 | if (stats) |
1201 | read_tsc_rate(); | |
1202 | ||
c5347611 JA |
1203 | if (batch_complete > depth) |
1204 | batch_complete = depth; | |
1205 | if (batch_submit > depth) | |
1206 | batch_submit = depth; | |
a71ad043 JA |
1207 | if (!fixedbufs && dma_map) |
1208 | dma_map = 0; | |
c5347611 | 1209 | |
54319661 JA |
1210 | submitter = calloc(nthreads, sizeof(*submitter) + |
1211 | depth * sizeof(struct iovec)); | |
1212 | for (j = 0; j < nthreads; j++) { | |
1213 | s = get_submitter(j); | |
1214 | s->index = j; | |
1215 | s->done = s->calls = s->reaps = 0; | |
1216 | } | |
e39863e3 | 1217 | |
701d1277 | 1218 | flags = O_RDONLY | O_NOATIME; |
a7086591 JA |
1219 | if (!buffered) |
1220 | flags |= O_DIRECT; | |
1221 | ||
54319661 | 1222 | j = 0; |
e39863e3 | 1223 | i = optind; |
d79ff8c9 | 1224 | nfiles = argc - i; |
2ac00585 JA |
1225 | if (!do_nop) { |
1226 | if (!nfiles) { | |
1227 | printf("No files specified\n"); | |
1228 | usage(argv[0], 1); | |
1229 | } | |
1230 | threads_per_f = nthreads / nfiles; | |
1231 | /* make sure each thread gets assigned files */ | |
1232 | if (threads_per_f == 0) { | |
1233 | threads_per_f = 1; | |
1234 | } else { | |
1235 | threads_rem = nthreads - threads_per_f * nfiles; | |
1236 | } | |
d79ff8c9 | 1237 | } |
8025517d | 1238 | while (!do_nop && i < argc) { |
d79ff8c9 AJ |
1239 | int k, limit; |
1240 | ||
1241 | memset(&f, 0, sizeof(f)); | |
a7086591 JA |
1242 | |
1243 | fd = open(argv[i], flags); | |
1244 | if (fd < 0) { | |
1245 | perror("open"); | |
1246 | return 1; | |
1247 | } | |
d79ff8c9 AJ |
1248 | f.real_fd = fd; |
1249 | if (get_file_size(&f)) { | |
a7086591 JA |
1250 | printf("failed getting size of device/file\n"); |
1251 | return 1; | |
1252 | } | |
d79ff8c9 | 1253 | if (f.max_blocks <= 1) { |
a7086591 JA |
1254 | printf("Zero file/device size?\n"); |
1255 | return 1; | |
1256 | } | |
d79ff8c9 AJ |
1257 | f.max_blocks--; |
1258 | ||
1259 | limit = threads_per_f; | |
1260 | limit += threads_rem > 0 ? 1 : 0; | |
1261 | for (k = 0; k < limit; k++) { | |
1262 | s = get_submitter((j + k) % nthreads); | |
a7086591 | 1263 | |
d79ff8c9 AJ |
1264 | if (s->nr_files == MAX_FDS) { |
1265 | printf("Max number of files (%d) reached\n", MAX_FDS); | |
1266 | break; | |
1267 | } | |
1268 | ||
1269 | memcpy(&s->files[s->nr_files], &f, sizeof(f)); | |
1270 | ||
1271 | printf("Added file %s (submitter %d)\n", argv[i], s->index); | |
1272 | s->nr_files++; | |
1273 | } | |
1274 | threads_rem--; | |
a7086591 | 1275 | i++; |
d79ff8c9 | 1276 | j += limit; |
a7086591 JA |
1277 | } |
1278 | ||
c9fb4c5b JA |
1279 | arm_sig_int(); |
1280 | ||
54319661 JA |
1281 | for (j = 0; j < nthreads; j++) { |
1282 | s = get_submitter(j); | |
1283 | for (i = 0; i < depth; i++) { | |
1284 | void *buf; | |
c9fb4c5b | 1285 | |
54319661 JA |
1286 | if (posix_memalign(&buf, bs, bs)) { |
1287 | printf("failed alloc\n"); | |
1288 | return 1; | |
1289 | } | |
1290 | s->iovecs[i].iov_base = buf; | |
1291 | s->iovecs[i].iov_len = bs; | |
c9fb4c5b | 1292 | } |
c9fb4c5b JA |
1293 | } |
1294 | ||
54319661 JA |
1295 | for (j = 0; j < nthreads; j++) { |
1296 | s = get_submitter(j); | |
1297 | ||
256714ea JA |
1298 | if (!aio) |
1299 | err = setup_ring(s); | |
1300 | else | |
1301 | err = setup_aio(s); | |
54319661 JA |
1302 | if (err) { |
1303 | printf("ring setup failed: %s, %d\n", strerror(errno), err); | |
1304 | return 1; | |
1305 | } | |
c9fb4c5b | 1306 | } |
54319661 | 1307 | s = get_submitter(0); |
a71ad043 | 1308 | printf("polled=%d, fixedbufs=%d/%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, dma_map, register_files, buffered, depth); |
256714ea JA |
1309 | if (!aio) |
1310 | printf("Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries); | |
256714ea | 1311 | else |
09ee86fa | 1312 | printf("Engine=aio\n"); |
c9fb4c5b | 1313 | |
54319661 JA |
1314 | for (j = 0; j < nthreads; j++) { |
1315 | s = get_submitter(j); | |
256714ea JA |
1316 | if (!aio) |
1317 | pthread_create(&s->thread, NULL, submitter_uring_fn, s); | |
1318 | #ifdef CONFIG_LIBAIO | |
1319 | else | |
1320 | pthread_create(&s->thread, NULL, submitter_aio_fn, s); | |
1321 | #endif | |
54319661 | 1322 | } |
c9fb4c5b | 1323 | |
54319661 | 1324 | fdepths = malloc(8 * s->nr_files * nthreads); |
05138221 | 1325 | reap = calls = done = 0; |
c9fb4c5b JA |
1326 | do { |
1327 | unsigned long this_done = 0; | |
1328 | unsigned long this_reap = 0; | |
1329 | unsigned long this_call = 0; | |
1330 | unsigned long rpc = 0, ipc = 0; | |
f3057d26 | 1331 | unsigned long iops, bw; |
c9fb4c5b JA |
1332 | |
1333 | sleep(1); | |
65e1a5e8 EV |
1334 | if (runtime && !--runtime) |
1335 | do_finish("timeout"); | |
a1f17100 JA |
1336 | |
1337 | /* don't print partial run, if interrupted by signal */ | |
1338 | if (finish) | |
1339 | break; | |
52479d8b JA |
1340 | |
1341 | /* one second in to the run, enable stats */ | |
1342 | if (stats) | |
1343 | stats_running = 1; | |
1344 | ||
54319661 JA |
1345 | for (j = 0; j < nthreads; j++) { |
1346 | this_done += s->done; | |
1347 | this_call += s->calls; | |
1348 | this_reap += s->reaps; | |
1349 | } | |
c9fb4c5b JA |
1350 | if (this_call - calls) { |
1351 | rpc = (this_done - done) / (this_call - calls); | |
1352 | ipc = (this_reap - reap) / (this_call - calls); | |
191561c1 JA |
1353 | } else |
1354 | rpc = ipc = -1; | |
2e7888ef | 1355 | file_depths(fdepths); |
22fd3501 | 1356 | iops = this_done - done; |
f3057d26 JA |
1357 | if (bs > 1048576) |
1358 | bw = iops * (bs / 1048576); | |
1359 | else | |
1360 | bw = iops / (1048576 / bs); | |
dc10c23a JA |
1361 | if (iops > 100000) |
1362 | printf("IOPS=%luK, ", iops / 1000); | |
1363 | else | |
53b5fa1e | 1364 | printf("IOPS=%lu, ", iops); |
6c5d3a1c JA |
1365 | if (!do_nop) |
1366 | printf("BW=%luMiB/s, ", bw); | |
1367 | printf("IOS/call=%ld/%ld, inflight=(%s)\n", rpc, ipc, fdepths); | |
c9fb4c5b JA |
1368 | done = this_done; |
1369 | calls = this_call; | |
1370 | reap = this_reap; | |
1371 | } while (!finish); | |
1372 | ||
54319661 JA |
1373 | for (j = 0; j < nthreads; j++) { |
1374 | s = get_submitter(j); | |
1375 | pthread_join(s->thread, &ret); | |
1376 | close(s->ring_fd); | |
932131c9 JA |
1377 | |
1378 | if (stats) { | |
1379 | unsigned long nr; | |
1380 | ||
1381 | printf("%d: Latency percentiles:\n", s->tid); | |
1382 | for (i = 0, nr = 0; i < PLAT_NR; i++) | |
1383 | nr += s->plat[i]; | |
1384 | show_clat_percentiles(s->plat, nr, 4); | |
1385 | free(s->clock_batch); | |
1386 | free(s->plat); | |
1387 | } | |
54319661 | 1388 | } |
932131c9 | 1389 | |
2e7888ef | 1390 | free(fdepths); |
54319661 | 1391 | free(submitter); |
c9fb4c5b JA |
1392 | return 0; |
1393 | } |