io_uring: ensure that the io_uring_register() structs are 32-bit safe
[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 barrier() __asm__ __volatile__("": : :"memory")
c3e2fc25 28
e31b8288 29#define min(a, b) ((a < b) ? (a) : (b))
c3e2fc25 30
e31b8288 31struct io_sq_ring {
e2239016
JA
32 unsigned *head;
33 unsigned *tail;
34 unsigned *ring_mask;
35 unsigned *ring_entries;
ce1705de 36 unsigned *flags;
e2239016 37 unsigned *array;
c9fb4c5b
JA
38};
39
e31b8288 40struct io_cq_ring {
e2239016
JA
41 unsigned *head;
42 unsigned *tail;
43 unsigned *ring_mask;
44 unsigned *ring_entries;
f0403f94 45 struct io_uring_cqe *cqes;
c9fb4c5b
JA
46};
47
701d1277 48#define DEPTH 128
c9fb4c5b 49
701d1277
JA
50#define BATCH_SUBMIT 64
51#define BATCH_COMPLETE 64
c9fb4c5b
JA
52
53#define BS 4096
54
a7086591
JA
55#define MAX_FDS 16
56
c3e2fc25 57static unsigned sq_ring_mask, cq_ring_mask;
e39c34dc 58
a7086591
JA
59struct file {
60 unsigned long max_blocks;
701d1277 61 unsigned pending_ios;
48e698fa
JA
62 int real_fd;
63 int fixed_fd;
a7086591
JA
64};
65
c9fb4c5b
JA
66struct submitter {
67 pthread_t thread;
f310970e 68 int ring_fd;
c9fb4c5b 69 struct drand48_data rand;
e31b8288 70 struct io_sq_ring sq_ring;
f0403f94 71 struct io_uring_sqe *sqes;
c3e2fc25 72 struct iovec iovecs[DEPTH];
e31b8288 73 struct io_cq_ring cq_ring;
c9fb4c5b
JA
74 int inflight;
75 unsigned long reaps;
76 unsigned long done;
77 unsigned long calls;
305c06ce 78 unsigned long cachehit, cachemiss;
c9fb4c5b 79 volatile int finish;
701d1277 80
48e698fa
JA
81 __s32 *fds;
82
a7086591
JA
83 struct file files[MAX_FDS];
84 unsigned nr_files;
85 unsigned cur_file;
c9fb4c5b
JA
86};
87
88static struct submitter submitters[1];
89static volatile int finish;
90
f0403f94 91static int polled = 1; /* use IO polling */
701d1277 92static int fixedbufs = 1; /* use fixed user buffers */
f0403f94 93static int buffered = 0; /* use buffered IO, not O_DIRECT */
3d7d00a3
JA
94static int sq_thread_poll = 0; /* use kernel submission/poller thread */
95static int sq_thread_cpu = -1; /* pin above thread to this CPU */
8025517d 96static int do_nop = 0; /* no-op SQ ring commands */
c9fb4c5b 97
2ea53ca3 98static int io_uring_register_buffers(struct submitter *s)
c9fb4c5b 99{
2ea53ca3
JA
100 struct io_uring_register_buffers reg = {
101 .iovecs = s->iovecs,
102 .nr_iovecs = DEPTH
103 };
104
8025517d
JA
105 if (do_nop)
106 return 0;
107
2ea53ca3
JA
108 return syscall(__NR_sys_io_uring_register, s->ring_fd,
109 IORING_REGISTER_BUFFERS, &reg);
110}
111
a7abc9fb
JA
112static int io_uring_register_files(struct submitter *s)
113{
114 struct io_uring_register_files reg;
48e698fa 115 int i;
a7abc9fb 116
8025517d
JA
117 if (do_nop)
118 return 0;
119
48e698fa
JA
120 s->fds = calloc(s->nr_files, sizeof(__s32));
121 for (i = 0; i < s->nr_files; i++) {
122 s->fds[i] = s->files[i].real_fd;
123 s->files[i].fixed_fd = i;
124 }
125 reg.fds = s->fds;
a7abc9fb
JA
126 reg.nr_fds = s->nr_files;
127
48e698fa 128 return syscall(__NR_sys_io_uring_register, s->ring_fd,
a7abc9fb 129 IORING_REGISTER_FILES, &reg);
a7abc9fb
JA
130}
131
2ea53ca3
JA
132static int io_uring_setup(unsigned entries, struct io_uring_params *p)
133{
134 return syscall(__NR_sys_io_uring_setup, entries, p);
c9fb4c5b
JA
135}
136
c3e2fc25
JA
137static int io_uring_enter(struct submitter *s, unsigned int to_submit,
138 unsigned int min_complete, unsigned int flags)
c9fb4c5b 139{
f310970e
JA
140 return syscall(__NR_sys_io_uring_enter, s->ring_fd, to_submit,
141 min_complete, flags);
c9fb4c5b
JA
142}
143
144static int gettid(void)
145{
146 return syscall(__NR_gettid);
147}
148
701d1277
JA
149static unsigned file_depth(struct submitter *s)
150{
151 return (DEPTH + s->nr_files - 1) / s->nr_files;
152}
153
a7086591 154static void init_io(struct submitter *s, unsigned index)
c9fb4c5b 155{
f0403f94 156 struct io_uring_sqe *sqe = &s->sqes[index];
c9fb4c5b 157 unsigned long offset;
a7086591 158 struct file *f;
c9fb4c5b
JA
159 long r;
160
8025517d
JA
161 if (do_nop) {
162 sqe->opcode = IORING_OP_NOP;
163 return;
164 }
165
701d1277
JA
166 if (s->nr_files == 1) {
167 f = &s->files[0];
168 } else {
169 f = &s->files[s->cur_file];
170 if (f->pending_ios >= file_depth(s)) {
171 s->cur_file++;
172 if (s->cur_file == s->nr_files)
173 s->cur_file = 0;
174 }
175 }
176 f->pending_ios++;
a7086591 177
c9fb4c5b 178 lrand48_r(&s->rand, &r);
a7086591 179 offset = (r % (f->max_blocks - 1)) * BS;
c9fb4c5b 180
a7abc9fb 181 sqe->flags = IOSQE_FIXED_FILE;
f0403f94 182 if (fixedbufs) {
48e698fa 183 sqe->opcode = IORING_OP_READ_FIXED;
f0403f94
JA
184 sqe->addr = s->iovecs[index].iov_base;
185 sqe->len = BS;
2ea53ca3 186 sqe->buf_index = index;
f0403f94 187 } else {
48e698fa 188 sqe->opcode = IORING_OP_READV;
f0403f94
JA
189 sqe->addr = &s->iovecs[index];
190 sqe->len = 1;
2ea53ca3 191 sqe->buf_index = 0;
f0403f94 192 }
f0403f94 193 sqe->ioprio = 0;
48e698fa 194 sqe->fd = f->fixed_fd;
f0403f94 195 sqe->off = offset;
48e698fa 196 sqe->user_data = (unsigned long) f;
c9fb4c5b
JA
197}
198
a7086591 199static int prep_more_ios(struct submitter *s, int max_ios)
c9fb4c5b 200{
e31b8288 201 struct io_sq_ring *ring = &s->sq_ring;
e2239016 202 unsigned index, tail, next_tail, prepped = 0;
c9fb4c5b 203
c3e2fc25 204 next_tail = tail = *ring->tail;
c9fb4c5b
JA
205 do {
206 next_tail++;
c9fb4c5b 207 barrier();
c3e2fc25 208 if (next_tail == *ring->head)
c9fb4c5b
JA
209 break;
210
e39c34dc 211 index = tail & sq_ring_mask;
a7086591 212 init_io(s, index);
c3e2fc25 213 ring->array[index] = index;
c9fb4c5b
JA
214 prepped++;
215 tail = next_tail;
216 } while (prepped < max_ios);
217
c3e2fc25 218 if (*ring->tail != tail) {
f0403f94 219 /* order tail store with writes to sqes above */
c9fb4c5b 220 barrier();
c3e2fc25 221 *ring->tail = tail;
c9fb4c5b
JA
222 barrier();
223 }
224 return prepped;
225}
226
a7086591 227static int get_file_size(struct file *f)
c9fb4c5b
JA
228{
229 struct stat st;
230
48e698fa 231 if (fstat(f->real_fd, &st) < 0)
c9fb4c5b
JA
232 return -1;
233 if (S_ISBLK(st.st_mode)) {
234 unsigned long long bytes;
235
48e698fa 236 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
c9fb4c5b
JA
237 return -1;
238
a7086591 239 f->max_blocks = bytes / BS;
c9fb4c5b
JA
240 return 0;
241 } else if (S_ISREG(st.st_mode)) {
a7086591 242 f->max_blocks = st.st_size / BS;
c9fb4c5b
JA
243 return 0;
244 }
245
246 return -1;
247}
248
249static int reap_events(struct submitter *s)
250{
e31b8288 251 struct io_cq_ring *ring = &s->cq_ring;
f0403f94 252 struct io_uring_cqe *cqe;
e2239016 253 unsigned head, reaped = 0;
c9fb4c5b 254
c3e2fc25 255 head = *ring->head;
c9fb4c5b 256 do {
701d1277
JA
257 struct file *f;
258
c9fb4c5b 259 barrier();
c3e2fc25 260 if (head == *ring->tail)
c9fb4c5b 261 break;
f0403f94 262 cqe = &ring->cqes[head & cq_ring_mask];
8025517d
JA
263 if (!do_nop) {
264 f = (struct file *) cqe->user_data;
265 f->pending_ios--;
266 if (cqe->res != BS) {
267 printf("io: unexpected ret=%d\n", cqe->res);
268 return -1;
269 }
c9fb4c5b 270 }
f0403f94 271 if (cqe->flags & IOCQE_FLAG_CACHEHIT)
305c06ce
JA
272 s->cachehit++;
273 else
274 s->cachemiss++;
c9fb4c5b
JA
275 reaped++;
276 head++;
c9fb4c5b
JA
277 } while (1);
278
279 s->inflight -= reaped;
c3e2fc25 280 *ring->head = head;
c9fb4c5b
JA
281 barrier();
282 return reaped;
283}
284
285static void *submitter_fn(void *data)
286{
287 struct submitter *s = data;
ce1705de 288 struct io_sq_ring *ring = &s->sq_ring;
a7086591 289 int ret, prepped;
c9fb4c5b
JA
290
291 printf("submitter=%d\n", gettid());
292
c9fb4c5b
JA
293 srand48_r(pthread_self(), &s->rand);
294
295 prepped = 0;
296 do {
f310970e 297 int to_wait, to_submit, this_reap, to_prep;
c9fb4c5b 298
f310970e
JA
299 if (!prepped && s->inflight < DEPTH) {
300 to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
a7086591 301 prepped = prep_more_ios(s, to_prep);
f310970e 302 }
c9fb4c5b
JA
303 s->inflight += prepped;
304submit_more:
305 to_submit = prepped;
306submit:
307 if (s->inflight + BATCH_SUBMIT < DEPTH)
308 to_wait = 0;
309 else
310 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
311
ce1705de
JA
312 /*
313 * Only need to call io_uring_enter if we're not using SQ thread
314 * poll, or if IORING_SQ_NEED_WAKEUP is set.
315 */
316 if (!sq_thread_poll || (*ring->flags & IORING_SQ_NEED_WAKEUP)) {
e0abe388
JA
317 unsigned flags = 0;
318
319 if (to_wait)
320 flags = IORING_ENTER_GETEVENTS;
321 ret = io_uring_enter(s, to_submit, to_wait, flags);
ce1705de
JA
322 s->calls++;
323 }
c9fb4c5b 324
ce1705de
JA
325 /*
326 * For non SQ thread poll, we already got the events we needed
327 * through the io_uring_enter() above. For SQ thread poll, we
328 * need to loop here until we find enough events.
329 */
330 this_reap = 0;
331 do {
332 int r;
333 r = reap_events(s);
334 if (r == -1)
335 break;
336 else if (r > 0)
337 this_reap += r;
338 } while (sq_thread_poll && this_reap < to_wait);
c9fb4c5b
JA
339 s->reaps += this_reap;
340
341 if (ret >= 0) {
342 if (!ret) {
343 to_submit = 0;
344 if (s->inflight)
345 goto submit;
346 continue;
347 } else if (ret < to_submit) {
348 int diff = to_submit - ret;
349
350 s->done += ret;
351 prepped -= diff;
352 goto submit_more;
353 }
354 s->done += ret;
355 prepped = 0;
356 continue;
357 } else if (ret < 0) {
ac122fea 358 if (errno == EAGAIN) {
c9fb4c5b
JA
359 if (s->finish)
360 break;
361 if (this_reap)
362 goto submit;
c9fb4c5b
JA
363 to_submit = 0;
364 goto submit;
365 }
ac122fea 366 printf("io_submit: %s\n", strerror(errno));
c9fb4c5b
JA
367 break;
368 }
369 } while (!s->finish);
a7086591 370
c9fb4c5b
JA
371 finish = 1;
372 return NULL;
373}
374
375static void sig_int(int sig)
376{
377 printf("Exiting on signal %d\n", sig);
378 submitters[0].finish = 1;
379 finish = 1;
380}
381
382static void arm_sig_int(void)
383{
384 struct sigaction act;
385
386 memset(&act, 0, sizeof(act));
387 act.sa_handler = sig_int;
388 act.sa_flags = SA_RESTART;
389 sigaction(SIGINT, &act, NULL);
390}
391
c3e2fc25
JA
392static int setup_ring(struct submitter *s)
393{
e31b8288
JA
394 struct io_sq_ring *sring = &s->sq_ring;
395 struct io_cq_ring *cring = &s->cq_ring;
396 struct io_uring_params p;
2ea53ca3 397 int ret, fd;
c3e2fc25 398 void *ptr;
c3e2fc25
JA
399
400 memset(&p, 0, sizeof(p));
401
0e47f11b
JA
402 if (polled)
403 p.flags |= IORING_SETUP_IOPOLL;
3d7d00a3 404 if (sq_thread_poll) {
2ea53ca3 405 p.flags |= IORING_SETUP_SQPOLL;
3ade18a3 406 if (sq_thread_cpu != -1) {
3d7d00a3 407 p.flags |= IORING_SETUP_SQ_AFF;
3ade18a3
JA
408 p.sq_thread_cpu = sq_thread_cpu;
409 }
c3e2fc25
JA
410 }
411
2ea53ca3 412 fd = io_uring_setup(DEPTH, &p);
c3e2fc25
JA
413 if (fd < 0) {
414 perror("io_uring_setup");
415 return 1;
416 }
f310970e 417 s->ring_fd = fd;
2ea53ca3
JA
418
419 if (fixedbufs) {
420 ret = io_uring_register_buffers(s);
421 if (ret < 0) {
a7abc9fb 422 perror("io_uring_register_buffers");
2ea53ca3
JA
423 return 1;
424 }
425 }
426
a7abc9fb
JA
427 ret = io_uring_register_files(s);
428 if (ret < 0) {
429 perror("io_uring_register_files");
430 return 1;
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
c9fb4c5b
JA
463int main(int argc, char *argv[])
464{
465 struct submitter *s = &submitters[0];
305c06ce 466 unsigned long done, calls, reap, cache_hit, cache_miss;
a7086591 467 int err, i, flags, fd;
c9fb4c5b 468 struct rlimit rlim;
c3e2fc25 469 void *ret;
c9fb4c5b 470
8025517d 471 if (!do_nop && argc < 2) {
c9fb4c5b
JA
472 printf("%s: filename\n", argv[0]);
473 return 1;
474 }
475
701d1277 476 flags = O_RDONLY | O_NOATIME;
a7086591
JA
477 if (!buffered)
478 flags |= O_DIRECT;
479
480 i = 1;
8025517d 481 while (!do_nop && i < argc) {
a7086591
JA
482 struct file *f = &s->files[s->nr_files];
483
484 fd = open(argv[i], flags);
485 if (fd < 0) {
486 perror("open");
487 return 1;
488 }
48e698fa 489 f->real_fd = fd;
a7086591
JA
490 if (get_file_size(f)) {
491 printf("failed getting size of device/file\n");
492 return 1;
493 }
494 if (f->max_blocks <= 1) {
495 printf("Zero file/device size?\n");
496 return 1;
497 }
498 f->max_blocks--;
499
500 printf("Added file %s\n", argv[i]);
501 s->nr_files++;
502 i++;
503 }
504
c9fb4c5b
JA
505 rlim.rlim_cur = RLIM_INFINITY;
506 rlim.rlim_max = RLIM_INFINITY;
507 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
508 perror("setrlimit");
509 return 1;
510 }
511
512 arm_sig_int();
513
c3e2fc25
JA
514 for (i = 0; i < DEPTH; i++) {
515 void *buf;
c9fb4c5b 516
c3e2fc25 517 if (posix_memalign(&buf, BS, BS)) {
c9fb4c5b
JA
518 printf("failed alloc\n");
519 return 1;
520 }
c3e2fc25
JA
521 s->iovecs[i].iov_base = buf;
522 s->iovecs[i].iov_len = BS;
c9fb4c5b
JA
523 }
524
c3e2fc25 525 err = setup_ring(s);
c9fb4c5b 526 if (err) {
c3e2fc25 527 printf("ring setup failed: %s, %d\n", strerror(errno), err);
c9fb4c5b
JA
528 return 1;
529 }
c3e2fc25
JA
530 printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
531 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
c9fb4c5b
JA
532
533 pthread_create(&s->thread, NULL, submitter_fn, s);
534
305c06ce 535 cache_hit = cache_miss = reap = calls = done = 0;
c9fb4c5b
JA
536 do {
537 unsigned long this_done = 0;
538 unsigned long this_reap = 0;
539 unsigned long this_call = 0;
305c06ce
JA
540 unsigned long this_cache_hit = 0;
541 unsigned long this_cache_miss = 0;
c9fb4c5b 542 unsigned long rpc = 0, ipc = 0;
305c06ce 543 double hit = 0.0;
c9fb4c5b
JA
544
545 sleep(1);
546 this_done += s->done;
547 this_call += s->calls;
548 this_reap += s->reaps;
305c06ce
JA
549 this_cache_hit += s->cachehit;
550 this_cache_miss += s->cachemiss;
551 if (this_cache_hit && this_cache_miss) {
552 unsigned long hits, total;
553
554 hits = this_cache_hit - cache_hit;
555 total = hits + this_cache_miss - cache_miss;
556 hit = (double) hits / (double) total;
557 hit *= 100.0;
558 }
c9fb4c5b
JA
559 if (this_call - calls) {
560 rpc = (this_done - done) / (this_call - calls);
561 ipc = (this_reap - reap) / (this_call - calls);
191561c1
JA
562 } else
563 rpc = ipc = -1;
564 printf("IOPS=%lu, IOS/call=%ld/%ld, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
c9fb4c5b 565 this_done - done, rpc, ipc, s->inflight,
c3e2fc25 566 *s->cq_ring.head, *s->cq_ring.tail, hit);
c9fb4c5b
JA
567 done = this_done;
568 calls = this_call;
569 reap = this_reap;
305c06ce
JA
570 cache_hit = s->cachehit;
571 cache_miss = s->cachemiss;
c9fb4c5b
JA
572 } while (!finish);
573
574 pthread_join(s->thread, &ret);
e31b8288 575 close(s->ring_fd);
c9fb4c5b
JA
576 return 0;
577}