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