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