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