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