t/io_uring: don't use *rand48_r() variants
[fio.git] / t / io_uring.c
CommitLineData
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>
8
9#include <sys/types.h>
10#include <sys/stat.h>
11#include <sys/ioctl.h>
12#include <sys/syscall.h>
13#include <sys/resource.h>
c3e2fc25 14#include <sys/mman.h>
e31b8288 15#include <sys/uio.h>
c9fb4c5b
JA
16#include <linux/fs.h>
17#include <fcntl.h>
18#include <unistd.h>
c9fb4c5b
JA
19#include <string.h>
20#include <pthread.h>
21#include <sched.h>
22
74efb029 23#include "../arch/arch.h"
57fa61f0 24#include "../lib/types.h"
f3e769a4 25#include "../os/linux/io_uring.h"
ac122fea 26
e31b8288 27#define min(a, b) ((a < b) ? (a) : (b))
c3e2fc25 28
e31b8288 29struct io_sq_ring {
e2239016
JA
30 unsigned *head;
31 unsigned *tail;
32 unsigned *ring_mask;
33 unsigned *ring_entries;
ce1705de 34 unsigned *flags;
e2239016 35 unsigned *array;
c9fb4c5b
JA
36};
37
e31b8288 38struct io_cq_ring {
e2239016
JA
39 unsigned *head;
40 unsigned *tail;
41 unsigned *ring_mask;
42 unsigned *ring_entries;
f0403f94 43 struct io_uring_cqe *cqes;
c9fb4c5b
JA
44};
45
701d1277 46#define DEPTH 128
2e7888ef
JA
47#define BATCH_SUBMIT 32
48#define BATCH_COMPLETE 32
c9fb4c5b
JA
49
50#define BS 4096
51
a7086591
JA
52#define MAX_FDS 16
53
c3e2fc25 54static unsigned sq_ring_mask, cq_ring_mask;
e39c34dc 55
a7086591
JA
56struct file {
57 unsigned long max_blocks;
701d1277 58 unsigned pending_ios;
48e698fa
JA
59 int real_fd;
60 int fixed_fd;
a7086591
JA
61};
62
c9fb4c5b
JA
63struct submitter {
64 pthread_t thread;
f310970e 65 int ring_fd;
e31b8288 66 struct io_sq_ring sq_ring;
f0403f94 67 struct io_uring_sqe *sqes;
e31b8288 68 struct io_cq_ring cq_ring;
c9fb4c5b
JA
69 int inflight;
70 unsigned long reaps;
71 unsigned long done;
72 unsigned long calls;
73 volatile int finish;
701d1277 74
48e698fa
JA
75 __s32 *fds;
76
a7086591
JA
77 struct file files[MAX_FDS];
78 unsigned nr_files;
79 unsigned cur_file;
e39863e3 80 struct iovec iovecs[];
c9fb4c5b
JA
81};
82
e39863e3 83static struct submitter *submitter;
c9fb4c5b
JA
84static volatile int finish;
85
e39863e3
KB
86static int depth = DEPTH;
87static int batch_submit = BATCH_SUBMIT;
88static int batch_complete = BATCH_COMPLETE;
f0403f94 89static int polled = 1; /* use IO polling */
701d1277 90static int fixedbufs = 1; /* use fixed user buffers */
8c5fa755 91static int register_files = 1; /* use fixed files */
f0403f94 92static int buffered = 0; /* use buffered IO, not O_DIRECT */
3d7d00a3
JA
93static int sq_thread_poll = 0; /* use kernel submission/poller thread */
94static int sq_thread_cpu = -1; /* pin above thread to this CPU */
8025517d 95static int do_nop = 0; /* no-op SQ ring commands */
c9fb4c5b 96
2ea53ca3 97static int io_uring_register_buffers(struct submitter *s)
c9fb4c5b 98{
8025517d
JA
99 if (do_nop)
100 return 0;
101
bfed648c 102 return syscall(__NR_io_uring_register, s->ring_fd,
e39863e3 103 IORING_REGISTER_BUFFERS, s->iovecs, depth);
2ea53ca3
JA
104}
105
a7abc9fb
JA
106static int io_uring_register_files(struct submitter *s)
107{
48e698fa 108 int i;
a7abc9fb 109
8025517d
JA
110 if (do_nop)
111 return 0;
112
48e698fa
JA
113 s->fds = calloc(s->nr_files, sizeof(__s32));
114 for (i = 0; i < s->nr_files; i++) {
115 s->fds[i] = s->files[i].real_fd;
116 s->files[i].fixed_fd = i;
117 }
a7abc9fb 118
bfed648c 119 return syscall(__NR_io_uring_register, s->ring_fd,
919850d2 120 IORING_REGISTER_FILES, s->fds, s->nr_files);
a7abc9fb
JA
121}
122
2ea53ca3
JA
123static int io_uring_setup(unsigned entries, struct io_uring_params *p)
124{
bfed648c 125 return syscall(__NR_io_uring_setup, entries, p);
c9fb4c5b
JA
126}
127
c3e2fc25
JA
128static int io_uring_enter(struct submitter *s, unsigned int to_submit,
129 unsigned int min_complete, unsigned int flags)
c9fb4c5b 130{
bfed648c
JA
131 return syscall(__NR_io_uring_enter, s->ring_fd, to_submit, min_complete,
132 flags, NULL, 0);
c9fb4c5b
JA
133}
134
77d814a1 135#ifndef CONFIG_HAVE_GETTID
c9fb4c5b
JA
136static int gettid(void)
137{
138 return syscall(__NR_gettid);
139}
77d814a1 140#endif
c9fb4c5b 141
701d1277
JA
142static unsigned file_depth(struct submitter *s)
143{
e39863e3 144 return (depth + s->nr_files - 1) / s->nr_files;
701d1277
JA
145}
146
a7086591 147static void init_io(struct submitter *s, unsigned index)
c9fb4c5b 148{
f0403f94 149 struct io_uring_sqe *sqe = &s->sqes[index];
c9fb4c5b 150 unsigned long offset;
a7086591 151 struct file *f;
c9fb4c5b
JA
152 long r;
153
8025517d
JA
154 if (do_nop) {
155 sqe->opcode = IORING_OP_NOP;
156 return;
157 }
158
701d1277
JA
159 if (s->nr_files == 1) {
160 f = &s->files[0];
161 } else {
162 f = &s->files[s->cur_file];
163 if (f->pending_ios >= file_depth(s)) {
164 s->cur_file++;
165 if (s->cur_file == s->nr_files)
166 s->cur_file = 0;
93d1811c 167 f = &s->files[s->cur_file];
701d1277
JA
168 }
169 }
170 f->pending_ios++;
a7086591 171
5e8865c0 172 r = lrand48();
a7086591 173 offset = (r % (f->max_blocks - 1)) * BS;
c9fb4c5b 174
8c5fa755
JA
175 if (register_files) {
176 sqe->flags = IOSQE_FIXED_FILE;
177 sqe->fd = f->fixed_fd;
178 } else {
179 sqe->flags = 0;
180 sqe->fd = f->real_fd;
181 }
f0403f94 182 if (fixedbufs) {
48e698fa 183 sqe->opcode = IORING_OP_READ_FIXED;
919850d2 184 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
f0403f94 185 sqe->len = BS;
2ea53ca3 186 sqe->buf_index = index;
f0403f94 187 } else {
48e698fa 188 sqe->opcode = IORING_OP_READV;
919850d2 189 sqe->addr = (unsigned long) &s->iovecs[index];
f0403f94 190 sqe->len = 1;
2ea53ca3 191 sqe->buf_index = 0;
f0403f94 192 }
f0403f94 193 sqe->ioprio = 0;
f0403f94 194 sqe->off = offset;
48e698fa 195 sqe->user_data = (unsigned long) f;
c9fb4c5b
JA
196}
197
a7086591 198static int prep_more_ios(struct submitter *s, int max_ios)
c9fb4c5b 199{
e31b8288 200 struct io_sq_ring *ring = &s->sq_ring;
e2239016 201 unsigned index, tail, next_tail, prepped = 0;
c9fb4c5b 202
c3e2fc25 203 next_tail = tail = *ring->tail;
c9fb4c5b
JA
204 do {
205 next_tail++;
679d8352 206 read_barrier();
c3e2fc25 207 if (next_tail == *ring->head)
c9fb4c5b
JA
208 break;
209
e39c34dc 210 index = tail & sq_ring_mask;
a7086591 211 init_io(s, index);
c3e2fc25 212 ring->array[index] = index;
c9fb4c5b
JA
213 prepped++;
214 tail = next_tail;
215 } while (prepped < max_ios);
216
c3e2fc25 217 if (*ring->tail != tail) {
c3e2fc25 218 *ring->tail = tail;
679d8352 219 write_barrier();
c9fb4c5b
JA
220 }
221 return prepped;
222}
223
a7086591 224static int get_file_size(struct file *f)
c9fb4c5b
JA
225{
226 struct stat st;
227
48e698fa 228 if (fstat(f->real_fd, &st) < 0)
c9fb4c5b
JA
229 return -1;
230 if (S_ISBLK(st.st_mode)) {
231 unsigned long long bytes;
232
48e698fa 233 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
c9fb4c5b
JA
234 return -1;
235
a7086591 236 f->max_blocks = bytes / BS;
c9fb4c5b
JA
237 return 0;
238 } else if (S_ISREG(st.st_mode)) {
a7086591 239 f->max_blocks = st.st_size / BS;
c9fb4c5b
JA
240 return 0;
241 }
242
243 return -1;
244}
245
246static int reap_events(struct submitter *s)
247{
e31b8288 248 struct io_cq_ring *ring = &s->cq_ring;
f0403f94 249 struct io_uring_cqe *cqe;
e2239016 250 unsigned head, reaped = 0;
c9fb4c5b 251
c3e2fc25 252 head = *ring->head;
c9fb4c5b 253 do {
701d1277
JA
254 struct file *f;
255
679d8352 256 read_barrier();
c3e2fc25 257 if (head == *ring->tail)
c9fb4c5b 258 break;
f0403f94 259 cqe = &ring->cqes[head & cq_ring_mask];
8025517d 260 if (!do_nop) {
e3466352 261 f = (struct file *) (uintptr_t) cqe->user_data;
8025517d
JA
262 f->pending_ios--;
263 if (cqe->res != BS) {
264 printf("io: unexpected ret=%d\n", cqe->res);
154a9582 265 if (polled && cqe->res == -EOPNOTSUPP)
8066f6b6 266 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
8025517d
JA
267 return -1;
268 }
c9fb4c5b
JA
269 }
270 reaped++;
271 head++;
c9fb4c5b
JA
272 } while (1);
273
274 s->inflight -= reaped;
c3e2fc25 275 *ring->head = head;
679d8352 276 write_barrier();
c9fb4c5b
JA
277 return reaped;
278}
279
280static void *submitter_fn(void *data)
281{
282 struct submitter *s = data;
ce1705de 283 struct io_sq_ring *ring = &s->sq_ring;
a7086591 284 int ret, prepped;
c9fb4c5b
JA
285
286 printf("submitter=%d\n", gettid());
287
5e8865c0 288 srand48(pthread_self());
c9fb4c5b
JA
289
290 prepped = 0;
291 do {
f310970e 292 int to_wait, to_submit, this_reap, to_prep;
c9fb4c5b 293
e39863e3
KB
294 if (!prepped && s->inflight < depth) {
295 to_prep = min(depth - s->inflight, batch_submit);
a7086591 296 prepped = prep_more_ios(s, to_prep);
f310970e 297 }
c9fb4c5b
JA
298 s->inflight += prepped;
299submit_more:
300 to_submit = prepped;
301submit:
e39863e3 302 if (to_submit && (s->inflight + to_submit <= depth))
c9fb4c5b
JA
303 to_wait = 0;
304 else
e39863e3 305 to_wait = min(s->inflight + to_submit, batch_complete);
c9fb4c5b 306
ce1705de
JA
307 /*
308 * Only need to call io_uring_enter if we're not using SQ thread
309 * poll, or if IORING_SQ_NEED_WAKEUP is set.
310 */
311 if (!sq_thread_poll || (*ring->flags & IORING_SQ_NEED_WAKEUP)) {
e0abe388
JA
312 unsigned flags = 0;
313
314 if (to_wait)
315 flags = IORING_ENTER_GETEVENTS;
e2f309e6 316 if ((*ring->flags & IORING_SQ_NEED_WAKEUP))
b532dd6d 317 flags |= IORING_ENTER_SQ_WAKEUP;
e0abe388 318 ret = io_uring_enter(s, to_submit, to_wait, flags);
ce1705de
JA
319 s->calls++;
320 }
c9fb4c5b 321
ce1705de
JA
322 /*
323 * For non SQ thread poll, we already got the events we needed
324 * through the io_uring_enter() above. For SQ thread poll, we
325 * need to loop here until we find enough events.
326 */
327 this_reap = 0;
328 do {
329 int r;
330 r = reap_events(s);
7d1435e6
JA
331 if (r == -1) {
332 s->finish = 1;
ce1705de 333 break;
7d1435e6 334 } else if (r > 0)
ce1705de
JA
335 this_reap += r;
336 } while (sq_thread_poll && this_reap < to_wait);
c9fb4c5b
JA
337 s->reaps += this_reap;
338
339 if (ret >= 0) {
340 if (!ret) {
341 to_submit = 0;
342 if (s->inflight)
343 goto submit;
344 continue;
345 } else if (ret < to_submit) {
346 int diff = to_submit - ret;
347
348 s->done += ret;
349 prepped -= diff;
350 goto submit_more;
351 }
352 s->done += ret;
353 prepped = 0;
354 continue;
355 } else if (ret < 0) {
ac122fea 356 if (errno == EAGAIN) {
c9fb4c5b
JA
357 if (s->finish)
358 break;
359 if (this_reap)
360 goto submit;
c9fb4c5b
JA
361 to_submit = 0;
362 goto submit;
363 }
ac122fea 364 printf("io_submit: %s\n", strerror(errno));
c9fb4c5b
JA
365 break;
366 }
367 } while (!s->finish);
a7086591 368
c9fb4c5b
JA
369 finish = 1;
370 return NULL;
371}
372
373static void sig_int(int sig)
374{
375 printf("Exiting on signal %d\n", sig);
e39863e3 376 submitter->finish = 1;
c9fb4c5b
JA
377 finish = 1;
378}
379
380static void arm_sig_int(void)
381{
382 struct sigaction act;
383
384 memset(&act, 0, sizeof(act));
385 act.sa_handler = sig_int;
386 act.sa_flags = SA_RESTART;
387 sigaction(SIGINT, &act, NULL);
388}
389
c3e2fc25
JA
390static int setup_ring(struct submitter *s)
391{
e31b8288
JA
392 struct io_sq_ring *sring = &s->sq_ring;
393 struct io_cq_ring *cring = &s->cq_ring;
394 struct io_uring_params p;
2ea53ca3 395 int ret, fd;
c3e2fc25 396 void *ptr;
c3e2fc25
JA
397
398 memset(&p, 0, sizeof(p));
399
7d1435e6 400 if (polled && !do_nop)
0e47f11b 401 p.flags |= IORING_SETUP_IOPOLL;
3d7d00a3 402 if (sq_thread_poll) {
2ea53ca3 403 p.flags |= IORING_SETUP_SQPOLL;
3ade18a3 404 if (sq_thread_cpu != -1) {
3d7d00a3 405 p.flags |= IORING_SETUP_SQ_AFF;
3ade18a3
JA
406 p.sq_thread_cpu = sq_thread_cpu;
407 }
c3e2fc25
JA
408 }
409
e39863e3 410 fd = io_uring_setup(depth, &p);
c3e2fc25
JA
411 if (fd < 0) {
412 perror("io_uring_setup");
413 return 1;
414 }
f310970e 415 s->ring_fd = fd;
2ea53ca3
JA
416
417 if (fixedbufs) {
418 ret = io_uring_register_buffers(s);
419 if (ret < 0) {
a7abc9fb 420 perror("io_uring_register_buffers");
2ea53ca3
JA
421 return 1;
422 }
423 }
424
8c5fa755
JA
425 if (register_files) {
426 ret = io_uring_register_files(s);
427 if (ret < 0) {
428 perror("io_uring_register_files");
429 return 1;
430 }
a7abc9fb
JA
431 }
432
e2239016 433 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
f310970e
JA
434 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
435 IORING_OFF_SQ_RING);
c3e2fc25
JA
436 printf("sq_ring ptr = 0x%p\n", ptr);
437 sring->head = ptr + p.sq_off.head;
438 sring->tail = ptr + p.sq_off.tail;
439 sring->ring_mask = ptr + p.sq_off.ring_mask;
440 sring->ring_entries = ptr + p.sq_off.ring_entries;
ce1705de 441 sring->flags = ptr + p.sq_off.flags;
ac122fea 442 sring->array = ptr + p.sq_off.array;
c3e2fc25
JA
443 sq_ring_mask = *sring->ring_mask;
444
f0403f94 445 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
f310970e 446 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
f0403f94
JA
447 IORING_OFF_SQES);
448 printf("sqes ptr = 0x%p\n", s->sqes);
c3e2fc25 449
f0403f94 450 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
f310970e
JA
451 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
452 IORING_OFF_CQ_RING);
c3e2fc25
JA
453 printf("cq_ring ptr = 0x%p\n", ptr);
454 cring->head = ptr + p.cq_off.head;
455 cring->tail = ptr + p.cq_off.tail;
456 cring->ring_mask = ptr + p.cq_off.ring_mask;
457 cring->ring_entries = ptr + p.cq_off.ring_entries;
f0403f94 458 cring->cqes = ptr + p.cq_off.cqes;
c3e2fc25
JA
459 cq_ring_mask = *cring->ring_mask;
460 return 0;
461}
462
2e7888ef
JA
463static void file_depths(char *buf)
464{
e39863e3 465 struct submitter *s = submitter;
2e7888ef
JA
466 char *p;
467 int i;
468
ac21bfab 469 buf[0] = '\0';
2e7888ef
JA
470 p = buf;
471 for (i = 0; i < s->nr_files; i++) {
472 struct file *f = &s->files[i];
473
474 if (i + 1 == s->nr_files)
475 p += sprintf(p, "%d", f->pending_ios);
476 else
477 p += sprintf(p, "%d, ", f->pending_ios);
478 }
479}
480
e39863e3
KB
481static void usage(char *argv)
482{
483 printf("%s [options] -- [filenames]\n"
484 " -d <int> : IO Depth, default %d\n"
485 " -s <int> : Batch submit, default %d\n"
486 " -c <int> : Batch complete, default %d\n",
487 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE);
488 exit(0);
489}
490
c9fb4c5b
JA
491int main(int argc, char *argv[])
492{
e39863e3 493 struct submitter *s;
05138221 494 unsigned long done, calls, reap;
e39863e3 495 int err, i, flags, fd, opt;
2e7888ef 496 char *fdepths;
c3e2fc25 497 void *ret;
c9fb4c5b 498
8025517d 499 if (!do_nop && argc < 2) {
e39863e3 500 printf("%s: filename [options]\n", argv[0]);
c9fb4c5b
JA
501 return 1;
502 }
503
e39863e3
KB
504 while ((opt = getopt(argc, argv, "d:s:c:h?")) != -1) {
505 switch (opt) {
506 case 'd':
507 depth = atoi(optarg);
508 break;
509 case 's':
510 batch_submit = atoi(optarg);
511 break;
512 case 'c':
513 batch_complete = atoi(optarg);
514 break;
515 case 'h':
516 case '?':
517 default:
518 usage(argv[0]);
519 break;
520 }
521 }
522
d9c50de7
KB
523 submitter = malloc(sizeof(*submitter) + depth * sizeof(struct iovec));
524 memset(submitter, 0, sizeof(*submitter) + depth * sizeof(struct iovec));
e39863e3
KB
525 s = submitter;
526
701d1277 527 flags = O_RDONLY | O_NOATIME;
a7086591
JA
528 if (!buffered)
529 flags |= O_DIRECT;
530
e39863e3 531 i = optind;
8025517d 532 while (!do_nop && i < argc) {
be26a982 533 struct file *f;
a7086591 534
be26a982
JA
535 if (s->nr_files == MAX_FDS) {
536 printf("Max number of files (%d) reached\n", MAX_FDS);
537 break;
538 }
a7086591
JA
539 fd = open(argv[i], flags);
540 if (fd < 0) {
541 perror("open");
542 return 1;
543 }
be26a982
JA
544
545 f = &s->files[s->nr_files];
48e698fa 546 f->real_fd = fd;
a7086591
JA
547 if (get_file_size(f)) {
548 printf("failed getting size of device/file\n");
549 return 1;
550 }
551 if (f->max_blocks <= 1) {
552 printf("Zero file/device size?\n");
553 return 1;
554 }
555 f->max_blocks--;
556
557 printf("Added file %s\n", argv[i]);
558 s->nr_files++;
559 i++;
560 }
561
b3ce355d
JA
562 if (fixedbufs) {
563 struct rlimit rlim;
564
565 rlim.rlim_cur = RLIM_INFINITY;
566 rlim.rlim_max = RLIM_INFINITY;
567 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
568 perror("setrlimit");
569 return 1;
570 }
c9fb4c5b
JA
571 }
572
573 arm_sig_int();
574
e39863e3 575 for (i = 0; i < depth; i++) {
c3e2fc25 576 void *buf;
c9fb4c5b 577
c3e2fc25 578 if (posix_memalign(&buf, BS, BS)) {
c9fb4c5b
JA
579 printf("failed alloc\n");
580 return 1;
581 }
c3e2fc25
JA
582 s->iovecs[i].iov_base = buf;
583 s->iovecs[i].iov_len = BS;
c9fb4c5b
JA
584 }
585
c3e2fc25 586 err = setup_ring(s);
c9fb4c5b 587 if (err) {
c3e2fc25 588 printf("ring setup failed: %s, %d\n", strerror(errno), err);
c9fb4c5b
JA
589 return 1;
590 }
c3e2fc25 591 printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
e39863e3 592 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", depth, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
c9fb4c5b
JA
593
594 pthread_create(&s->thread, NULL, submitter_fn, s);
595
2e7888ef 596 fdepths = malloc(8 * s->nr_files);
05138221 597 reap = calls = done = 0;
c9fb4c5b
JA
598 do {
599 unsigned long this_done = 0;
600 unsigned long this_reap = 0;
601 unsigned long this_call = 0;
602 unsigned long rpc = 0, ipc = 0;
603
604 sleep(1);
605 this_done += s->done;
606 this_call += s->calls;
607 this_reap += s->reaps;
608 if (this_call - calls) {
609 rpc = (this_done - done) / (this_call - calls);
610 ipc = (this_reap - reap) / (this_call - calls);
191561c1
JA
611 } else
612 rpc = ipc = -1;
2e7888ef 613 file_depths(fdepths);
05138221 614 printf("IOPS=%lu, IOS/call=%ld/%ld, inflight=%u (%s)\n",
c9fb4c5b 615 this_done - done, rpc, ipc, s->inflight,
05138221 616 fdepths);
c9fb4c5b
JA
617 done = this_done;
618 calls = this_call;
619 reap = this_reap;
620 } while (!finish);
621
622 pthread_join(s->thread, &ret);
e31b8288 623 close(s->ring_fd);
2e7888ef 624 free(fdepths);
c9fb4c5b
JA
625 return 0;
626}