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