t/io_uring: only set IORING_ENTER_GETEVENTS when actively reaping
[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"
e31b8288 25#include "../os/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 */
c9fb4c5b 96
2ea53ca3 97static int io_uring_register_buffers(struct submitter *s)
c9fb4c5b 98{
2ea53ca3
JA
99 struct io_uring_register_buffers reg = {
100 .iovecs = s->iovecs,
101 .nr_iovecs = DEPTH
102 };
103
104 return syscall(__NR_sys_io_uring_register, s->ring_fd,
105 IORING_REGISTER_BUFFERS, &reg);
106}
107
a7abc9fb
JA
108static int io_uring_register_files(struct submitter *s)
109{
110 struct io_uring_register_files reg;
48e698fa 111 int i;
a7abc9fb 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 }
118 reg.fds = s->fds;
a7abc9fb
JA
119 reg.nr_fds = s->nr_files;
120
48e698fa 121 return syscall(__NR_sys_io_uring_register, s->ring_fd,
a7abc9fb 122 IORING_REGISTER_FILES, &reg);
a7abc9fb
JA
123}
124
2ea53ca3
JA
125static int io_uring_setup(unsigned entries, struct io_uring_params *p)
126{
127 return syscall(__NR_sys_io_uring_setup, entries, p);
c9fb4c5b
JA
128}
129
c3e2fc25
JA
130static int io_uring_enter(struct submitter *s, unsigned int to_submit,
131 unsigned int min_complete, unsigned int flags)
c9fb4c5b 132{
f310970e
JA
133 return syscall(__NR_sys_io_uring_enter, s->ring_fd, to_submit,
134 min_complete, flags);
c9fb4c5b
JA
135}
136
137static int gettid(void)
138{
139 return syscall(__NR_gettid);
140}
141
701d1277
JA
142static unsigned file_depth(struct submitter *s)
143{
144 return (DEPTH + s->nr_files - 1) / s->nr_files;
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
701d1277
JA
154 if (s->nr_files == 1) {
155 f = &s->files[0];
156 } else {
157 f = &s->files[s->cur_file];
158 if (f->pending_ios >= file_depth(s)) {
159 s->cur_file++;
160 if (s->cur_file == s->nr_files)
161 s->cur_file = 0;
162 }
163 }
164 f->pending_ios++;
a7086591 165
c9fb4c5b 166 lrand48_r(&s->rand, &r);
a7086591 167 offset = (r % (f->max_blocks - 1)) * BS;
c9fb4c5b 168
a7abc9fb 169 sqe->flags = IOSQE_FIXED_FILE;
f0403f94 170 if (fixedbufs) {
48e698fa 171 sqe->opcode = IORING_OP_READ_FIXED;
f0403f94
JA
172 sqe->addr = s->iovecs[index].iov_base;
173 sqe->len = BS;
2ea53ca3 174 sqe->buf_index = index;
f0403f94 175 } else {
48e698fa 176 sqe->opcode = IORING_OP_READV;
f0403f94
JA
177 sqe->addr = &s->iovecs[index];
178 sqe->len = 1;
2ea53ca3 179 sqe->buf_index = 0;
f0403f94 180 }
f0403f94 181 sqe->ioprio = 0;
48e698fa 182 sqe->fd = f->fixed_fd;
f0403f94 183 sqe->off = offset;
48e698fa 184 sqe->user_data = (unsigned long) f;
c9fb4c5b
JA
185}
186
a7086591 187static int prep_more_ios(struct submitter *s, int max_ios)
c9fb4c5b 188{
e31b8288 189 struct io_sq_ring *ring = &s->sq_ring;
e2239016 190 unsigned index, tail, next_tail, prepped = 0;
c9fb4c5b 191
c3e2fc25 192 next_tail = tail = *ring->tail;
c9fb4c5b
JA
193 do {
194 next_tail++;
c9fb4c5b 195 barrier();
c3e2fc25 196 if (next_tail == *ring->head)
c9fb4c5b
JA
197 break;
198
e39c34dc 199 index = tail & sq_ring_mask;
a7086591 200 init_io(s, index);
c3e2fc25 201 ring->array[index] = index;
c9fb4c5b
JA
202 prepped++;
203 tail = next_tail;
204 } while (prepped < max_ios);
205
c3e2fc25 206 if (*ring->tail != tail) {
f0403f94 207 /* order tail store with writes to sqes above */
c9fb4c5b 208 barrier();
c3e2fc25 209 *ring->tail = tail;
c9fb4c5b
JA
210 barrier();
211 }
212 return prepped;
213}
214
a7086591 215static int get_file_size(struct file *f)
c9fb4c5b
JA
216{
217 struct stat st;
218
48e698fa 219 if (fstat(f->real_fd, &st) < 0)
c9fb4c5b
JA
220 return -1;
221 if (S_ISBLK(st.st_mode)) {
222 unsigned long long bytes;
223
48e698fa 224 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
c9fb4c5b
JA
225 return -1;
226
a7086591 227 f->max_blocks = bytes / BS;
c9fb4c5b
JA
228 return 0;
229 } else if (S_ISREG(st.st_mode)) {
a7086591 230 f->max_blocks = st.st_size / BS;
c9fb4c5b
JA
231 return 0;
232 }
233
234 return -1;
235}
236
237static int reap_events(struct submitter *s)
238{
e31b8288 239 struct io_cq_ring *ring = &s->cq_ring;
f0403f94 240 struct io_uring_cqe *cqe;
e2239016 241 unsigned head, reaped = 0;
c9fb4c5b 242
c3e2fc25 243 head = *ring->head;
c9fb4c5b 244 do {
701d1277
JA
245 struct file *f;
246
c9fb4c5b 247 barrier();
c3e2fc25 248 if (head == *ring->tail)
c9fb4c5b 249 break;
f0403f94 250 cqe = &ring->cqes[head & cq_ring_mask];
48e698fa 251 f = (struct file *) cqe->user_data;
701d1277 252 f->pending_ios--;
f0403f94 253 if (cqe->res != BS) {
f0403f94 254 printf("io: unexpected ret=%d\n", cqe->res);
c9fb4c5b
JA
255 return -1;
256 }
f0403f94 257 if (cqe->flags & IOCQE_FLAG_CACHEHIT)
305c06ce
JA
258 s->cachehit++;
259 else
260 s->cachemiss++;
c9fb4c5b
JA
261 reaped++;
262 head++;
c9fb4c5b
JA
263 } while (1);
264
265 s->inflight -= reaped;
c3e2fc25 266 *ring->head = head;
c9fb4c5b
JA
267 barrier();
268 return reaped;
269}
270
271static void *submitter_fn(void *data)
272{
273 struct submitter *s = data;
ce1705de 274 struct io_sq_ring *ring = &s->sq_ring;
a7086591 275 int ret, prepped;
c9fb4c5b
JA
276
277 printf("submitter=%d\n", gettid());
278
c9fb4c5b
JA
279 srand48_r(pthread_self(), &s->rand);
280
281 prepped = 0;
282 do {
f310970e 283 int to_wait, to_submit, this_reap, to_prep;
c9fb4c5b 284
f310970e
JA
285 if (!prepped && s->inflight < DEPTH) {
286 to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
a7086591 287 prepped = prep_more_ios(s, to_prep);
f310970e 288 }
c9fb4c5b
JA
289 s->inflight += prepped;
290submit_more:
291 to_submit = prepped;
292submit:
293 if (s->inflight + BATCH_SUBMIT < DEPTH)
294 to_wait = 0;
295 else
296 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
297
ce1705de
JA
298 /*
299 * Only need to call io_uring_enter if we're not using SQ thread
300 * poll, or if IORING_SQ_NEED_WAKEUP is set.
301 */
302 if (!sq_thread_poll || (*ring->flags & IORING_SQ_NEED_WAKEUP)) {
e0abe388
JA
303 unsigned flags = 0;
304
305 if (to_wait)
306 flags = IORING_ENTER_GETEVENTS;
307 ret = io_uring_enter(s, to_submit, to_wait, flags);
ce1705de
JA
308 s->calls++;
309 }
c9fb4c5b 310
ce1705de
JA
311 /*
312 * For non SQ thread poll, we already got the events we needed
313 * through the io_uring_enter() above. For SQ thread poll, we
314 * need to loop here until we find enough events.
315 */
316 this_reap = 0;
317 do {
318 int r;
319 r = reap_events(s);
320 if (r == -1)
321 break;
322 else if (r > 0)
323 this_reap += r;
324 } while (sq_thread_poll && this_reap < to_wait);
c9fb4c5b
JA
325 s->reaps += this_reap;
326
327 if (ret >= 0) {
328 if (!ret) {
329 to_submit = 0;
330 if (s->inflight)
331 goto submit;
332 continue;
333 } else if (ret < to_submit) {
334 int diff = to_submit - ret;
335
336 s->done += ret;
337 prepped -= diff;
338 goto submit_more;
339 }
340 s->done += ret;
341 prepped = 0;
342 continue;
343 } else if (ret < 0) {
ac122fea 344 if (errno == EAGAIN) {
c9fb4c5b
JA
345 if (s->finish)
346 break;
347 if (this_reap)
348 goto submit;
c9fb4c5b
JA
349 to_submit = 0;
350 goto submit;
351 }
ac122fea 352 printf("io_submit: %s\n", strerror(errno));
c9fb4c5b
JA
353 break;
354 }
355 } while (!s->finish);
a7086591 356
c9fb4c5b
JA
357 finish = 1;
358 return NULL;
359}
360
361static void sig_int(int sig)
362{
363 printf("Exiting on signal %d\n", sig);
364 submitters[0].finish = 1;
365 finish = 1;
366}
367
368static void arm_sig_int(void)
369{
370 struct sigaction act;
371
372 memset(&act, 0, sizeof(act));
373 act.sa_handler = sig_int;
374 act.sa_flags = SA_RESTART;
375 sigaction(SIGINT, &act, NULL);
376}
377
c3e2fc25
JA
378static int setup_ring(struct submitter *s)
379{
e31b8288
JA
380 struct io_sq_ring *sring = &s->sq_ring;
381 struct io_cq_ring *cring = &s->cq_ring;
382 struct io_uring_params p;
2ea53ca3 383 int ret, fd;
c3e2fc25 384 void *ptr;
c3e2fc25
JA
385
386 memset(&p, 0, sizeof(p));
387
0e47f11b
JA
388 if (polled)
389 p.flags |= IORING_SETUP_IOPOLL;
3d7d00a3 390 if (sq_thread_poll) {
2ea53ca3 391 p.flags |= IORING_SETUP_SQPOLL;
3ade18a3 392 if (sq_thread_cpu != -1) {
3d7d00a3 393 p.flags |= IORING_SETUP_SQ_AFF;
3ade18a3
JA
394 p.sq_thread_cpu = sq_thread_cpu;
395 }
c3e2fc25
JA
396 }
397
2ea53ca3 398 fd = io_uring_setup(DEPTH, &p);
c3e2fc25
JA
399 if (fd < 0) {
400 perror("io_uring_setup");
401 return 1;
402 }
f310970e 403 s->ring_fd = fd;
2ea53ca3
JA
404
405 if (fixedbufs) {
406 ret = io_uring_register_buffers(s);
407 if (ret < 0) {
a7abc9fb 408 perror("io_uring_register_buffers");
2ea53ca3
JA
409 return 1;
410 }
411 }
412
a7abc9fb
JA
413 ret = io_uring_register_files(s);
414 if (ret < 0) {
415 perror("io_uring_register_files");
416 return 1;
417 }
418
e2239016 419 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
f310970e
JA
420 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
421 IORING_OFF_SQ_RING);
c3e2fc25
JA
422 printf("sq_ring ptr = 0x%p\n", ptr);
423 sring->head = ptr + p.sq_off.head;
424 sring->tail = ptr + p.sq_off.tail;
425 sring->ring_mask = ptr + p.sq_off.ring_mask;
426 sring->ring_entries = ptr + p.sq_off.ring_entries;
ce1705de 427 sring->flags = ptr + p.sq_off.flags;
ac122fea 428 sring->array = ptr + p.sq_off.array;
c3e2fc25
JA
429 sq_ring_mask = *sring->ring_mask;
430
f0403f94 431 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
f310970e 432 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
f0403f94
JA
433 IORING_OFF_SQES);
434 printf("sqes ptr = 0x%p\n", s->sqes);
c3e2fc25 435
f0403f94 436 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
f310970e
JA
437 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
438 IORING_OFF_CQ_RING);
c3e2fc25
JA
439 printf("cq_ring ptr = 0x%p\n", ptr);
440 cring->head = ptr + p.cq_off.head;
441 cring->tail = ptr + p.cq_off.tail;
442 cring->ring_mask = ptr + p.cq_off.ring_mask;
443 cring->ring_entries = ptr + p.cq_off.ring_entries;
f0403f94 444 cring->cqes = ptr + p.cq_off.cqes;
c3e2fc25
JA
445 cq_ring_mask = *cring->ring_mask;
446 return 0;
447}
448
c9fb4c5b
JA
449int main(int argc, char *argv[])
450{
451 struct submitter *s = &submitters[0];
305c06ce 452 unsigned long done, calls, reap, cache_hit, cache_miss;
a7086591 453 int err, i, flags, fd;
c9fb4c5b 454 struct rlimit rlim;
c3e2fc25 455 void *ret;
c9fb4c5b
JA
456
457 if (argc < 2) {
458 printf("%s: filename\n", argv[0]);
459 return 1;
460 }
461
701d1277 462 flags = O_RDONLY | O_NOATIME;
a7086591
JA
463 if (!buffered)
464 flags |= O_DIRECT;
465
466 i = 1;
467 while (i < argc) {
468 struct file *f = &s->files[s->nr_files];
469
470 fd = open(argv[i], flags);
471 if (fd < 0) {
472 perror("open");
473 return 1;
474 }
48e698fa 475 f->real_fd = fd;
a7086591
JA
476 if (get_file_size(f)) {
477 printf("failed getting size of device/file\n");
478 return 1;
479 }
480 if (f->max_blocks <= 1) {
481 printf("Zero file/device size?\n");
482 return 1;
483 }
484 f->max_blocks--;
485
486 printf("Added file %s\n", argv[i]);
487 s->nr_files++;
488 i++;
489 }
490
c9fb4c5b
JA
491 rlim.rlim_cur = RLIM_INFINITY;
492 rlim.rlim_max = RLIM_INFINITY;
493 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
494 perror("setrlimit");
495 return 1;
496 }
497
498 arm_sig_int();
499
c3e2fc25
JA
500 for (i = 0; i < DEPTH; i++) {
501 void *buf;
c9fb4c5b 502
c3e2fc25 503 if (posix_memalign(&buf, BS, BS)) {
c9fb4c5b
JA
504 printf("failed alloc\n");
505 return 1;
506 }
c3e2fc25
JA
507 s->iovecs[i].iov_base = buf;
508 s->iovecs[i].iov_len = BS;
c9fb4c5b
JA
509 }
510
c3e2fc25 511 err = setup_ring(s);
c9fb4c5b 512 if (err) {
c3e2fc25 513 printf("ring setup failed: %s, %d\n", strerror(errno), err);
c9fb4c5b
JA
514 return 1;
515 }
c3e2fc25
JA
516 printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
517 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
c9fb4c5b
JA
518
519 pthread_create(&s->thread, NULL, submitter_fn, s);
520
305c06ce 521 cache_hit = cache_miss = reap = calls = done = 0;
c9fb4c5b
JA
522 do {
523 unsigned long this_done = 0;
524 unsigned long this_reap = 0;
525 unsigned long this_call = 0;
305c06ce
JA
526 unsigned long this_cache_hit = 0;
527 unsigned long this_cache_miss = 0;
c9fb4c5b 528 unsigned long rpc = 0, ipc = 0;
305c06ce 529 double hit = 0.0;
c9fb4c5b
JA
530
531 sleep(1);
532 this_done += s->done;
533 this_call += s->calls;
534 this_reap += s->reaps;
305c06ce
JA
535 this_cache_hit += s->cachehit;
536 this_cache_miss += s->cachemiss;
537 if (this_cache_hit && this_cache_miss) {
538 unsigned long hits, total;
539
540 hits = this_cache_hit - cache_hit;
541 total = hits + this_cache_miss - cache_miss;
542 hit = (double) hits / (double) total;
543 hit *= 100.0;
544 }
c9fb4c5b
JA
545 if (this_call - calls) {
546 rpc = (this_done - done) / (this_call - calls);
547 ipc = (this_reap - reap) / (this_call - calls);
191561c1
JA
548 } else
549 rpc = ipc = -1;
550 printf("IOPS=%lu, IOS/call=%ld/%ld, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
c9fb4c5b 551 this_done - done, rpc, ipc, s->inflight,
c3e2fc25 552 *s->cq_ring.head, *s->cq_ring.tail, hit);
c9fb4c5b
JA
553 done = this_done;
554 calls = this_call;
555 reap = this_reap;
305c06ce
JA
556 cache_hit = s->cachehit;
557 cache_miss = s->cachemiss;
c9fb4c5b
JA
558 } while (!finish);
559
560 pthread_join(s->thread, &ret);
e31b8288 561 close(s->ring_fd);
c9fb4c5b
JA
562 return 0;
563}