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