11 #include <sys/ioctl.h>
12 #include <sys/syscall.h>
13 #include <sys/resource.h>
23 #include "../arch/arch.h"
24 #include "../lib/types.h"
25 #include "../os/linux/io_uring.h"
27 #define barrier() __asm__ __volatile__("": : :"memory")
29 #define min(a, b) ((a < b) ? (a) : (b))
35 unsigned *ring_entries;
44 unsigned *ring_entries;
45 struct io_uring_cqe *cqes;
50 #define BATCH_SUBMIT 32
51 #define BATCH_COMPLETE 32
57 static unsigned sq_ring_mask, cq_ring_mask;
60 unsigned long max_blocks;
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;
78 unsigned long cachehit, cachemiss;
83 struct file files[MAX_FDS];
88 static struct submitter submitters[1];
89 static volatile int finish;
91 static int polled = 1; /* use IO polling */
92 static int fixedbufs = 1; /* use fixed user buffers */
93 static int register_files = 1; /* use fixed files */
94 static int buffered = 0; /* use buffered IO, not O_DIRECT */
95 static int sq_thread_poll = 0; /* use kernel submission/poller thread */
96 static int sq_thread_cpu = -1; /* pin above thread to this CPU */
97 static int do_nop = 0; /* no-op SQ ring commands */
99 static int io_uring_register_buffers(struct submitter *s)
101 struct io_uring_register_buffers reg = {
109 return syscall(__NR_sys_io_uring_register, s->ring_fd,
110 IORING_REGISTER_BUFFERS, ®);
113 static int io_uring_register_files(struct submitter *s)
115 struct io_uring_register_files reg;
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;
127 reg.nr_fds = s->nr_files;
129 return syscall(__NR_sys_io_uring_register, s->ring_fd,
130 IORING_REGISTER_FILES, ®);
133 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
135 return syscall(__NR_sys_io_uring_setup, entries, p);
138 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
139 unsigned int min_complete, unsigned int flags)
141 return syscall(__NR_sys_io_uring_enter, s->ring_fd, to_submit,
142 min_complete, flags);
145 static int gettid(void)
147 return syscall(__NR_gettid);
150 static unsigned file_depth(struct submitter *s)
152 return (DEPTH + s->nr_files - 1) / s->nr_files;
155 static void init_io(struct submitter *s, unsigned index)
157 struct io_uring_sqe *sqe = &s->sqes[index];
158 unsigned long offset;
163 sqe->opcode = IORING_OP_NOP;
167 if (s->nr_files == 1) {
170 f = &s->files[s->cur_file];
171 if (f->pending_ios >= file_depth(s)) {
173 if (s->cur_file == s->nr_files)
175 f = &s->files[s->cur_file];
180 lrand48_r(&s->rand, &r);
181 offset = (r % (f->max_blocks - 1)) * BS;
183 if (register_files) {
184 sqe->flags = IOSQE_FIXED_FILE;
185 sqe->fd = f->fixed_fd;
188 sqe->fd = f->real_fd;
191 sqe->opcode = IORING_OP_READ_FIXED;
192 sqe->addr = s->iovecs[index].iov_base;
194 sqe->buf_index = index;
196 sqe->opcode = IORING_OP_READV;
197 sqe->addr = &s->iovecs[index];
203 sqe->user_data = (unsigned long) f;
206 static int prep_more_ios(struct submitter *s, int max_ios)
208 struct io_sq_ring *ring = &s->sq_ring;
209 unsigned index, tail, next_tail, prepped = 0;
211 next_tail = tail = *ring->tail;
215 if (next_tail == *ring->head)
218 index = tail & sq_ring_mask;
220 ring->array[index] = index;
223 } while (prepped < max_ios);
225 if (*ring->tail != tail) {
226 /* order tail store with writes to sqes above */
234 static int get_file_size(struct file *f)
238 if (fstat(f->real_fd, &st) < 0)
240 if (S_ISBLK(st.st_mode)) {
241 unsigned long long bytes;
243 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
246 f->max_blocks = bytes / BS;
248 } else if (S_ISREG(st.st_mode)) {
249 f->max_blocks = st.st_size / BS;
256 static int reap_events(struct submitter *s)
258 struct io_cq_ring *ring = &s->cq_ring;
259 struct io_uring_cqe *cqe;
260 unsigned head, reaped = 0;
267 if (head == *ring->tail)
269 cqe = &ring->cqes[head & cq_ring_mask];
271 f = (struct file *) (uintptr_t) cqe->user_data;
273 if (cqe->res != BS) {
274 printf("io: unexpected ret=%d\n", cqe->res);
278 if (cqe->flags & IOCQE_FLAG_CACHEHIT)
286 s->inflight -= reaped;
292 static void *submitter_fn(void *data)
294 struct submitter *s = data;
295 struct io_sq_ring *ring = &s->sq_ring;
298 printf("submitter=%d\n", gettid());
300 srand48_r(pthread_self(), &s->rand);
304 int to_wait, to_submit, this_reap, to_prep;
306 if (!prepped && s->inflight < DEPTH) {
307 to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
308 prepped = prep_more_ios(s, to_prep);
310 s->inflight += prepped;
314 if (to_submit && (s->inflight + to_submit <= DEPTH))
317 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
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.
323 if (!sq_thread_poll || (*ring->flags & IORING_SQ_NEED_WAKEUP)) {
327 flags = IORING_ENTER_GETEVENTS;
328 ret = io_uring_enter(s, to_submit, to_wait, flags);
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.
345 } while (sq_thread_poll && this_reap < to_wait);
346 s->reaps += this_reap;
354 } else if (ret < to_submit) {
355 int diff = to_submit - ret;
364 } else if (ret < 0) {
365 if (errno == EAGAIN) {
373 printf("io_submit: %s\n", strerror(errno));
376 } while (!s->finish);
382 static void sig_int(int sig)
384 printf("Exiting on signal %d\n", sig);
385 submitters[0].finish = 1;
389 static void arm_sig_int(void)
391 struct sigaction act;
393 memset(&act, 0, sizeof(act));
394 act.sa_handler = sig_int;
395 act.sa_flags = SA_RESTART;
396 sigaction(SIGINT, &act, NULL);
399 static int setup_ring(struct submitter *s)
401 struct io_sq_ring *sring = &s->sq_ring;
402 struct io_cq_ring *cring = &s->cq_ring;
403 struct io_uring_params p;
407 memset(&p, 0, sizeof(p));
410 p.flags |= IORING_SETUP_IOPOLL;
411 if (sq_thread_poll) {
412 p.flags |= IORING_SETUP_SQPOLL;
413 if (sq_thread_cpu != -1) {
414 p.flags |= IORING_SETUP_SQ_AFF;
415 p.sq_thread_cpu = sq_thread_cpu;
419 fd = io_uring_setup(DEPTH, &p);
421 perror("io_uring_setup");
427 ret = io_uring_register_buffers(s);
429 perror("io_uring_register_buffers");
434 if (register_files) {
435 ret = io_uring_register_files(s);
437 perror("io_uring_register_files");
442 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
443 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
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;
450 sring->flags = ptr + p.sq_off.flags;
451 sring->array = ptr + p.sq_off.array;
452 sq_ring_mask = *sring->ring_mask;
454 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
455 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
457 printf("sqes ptr = 0x%p\n", s->sqes);
459 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
460 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
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;
467 cring->cqes = ptr + p.cq_off.cqes;
468 cq_ring_mask = *cring->ring_mask;
472 static void file_depths(char *buf)
474 struct submitter *s = &submitters[0];
479 for (i = 0; i < s->nr_files; i++) {
480 struct file *f = &s->files[i];
482 if (i + 1 == s->nr_files)
483 p += sprintf(p, "%d", f->pending_ios);
485 p += sprintf(p, "%d, ", f->pending_ios);
489 int main(int argc, char *argv[])
491 struct submitter *s = &submitters[0];
492 unsigned long done, calls, reap, cache_hit, cache_miss;
493 int err, i, flags, fd;
497 if (!do_nop && argc < 2) {
498 printf("%s: filename\n", argv[0]);
502 flags = O_RDONLY | O_NOATIME;
507 while (!do_nop && i < argc) {
508 struct file *f = &s->files[s->nr_files];
510 fd = open(argv[i], flags);
516 if (get_file_size(f)) {
517 printf("failed getting size of device/file\n");
520 if (f->max_blocks <= 1) {
521 printf("Zero file/device size?\n");
526 printf("Added file %s\n", argv[i]);
534 rlim.rlim_cur = RLIM_INFINITY;
535 rlim.rlim_max = RLIM_INFINITY;
536 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
544 for (i = 0; i < DEPTH; i++) {
547 if (posix_memalign(&buf, BS, BS)) {
548 printf("failed alloc\n");
551 s->iovecs[i].iov_base = buf;
552 s->iovecs[i].iov_len = BS;
557 printf("ring setup failed: %s, %d\n", strerror(errno), err);
560 printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
561 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
563 pthread_create(&s->thread, NULL, submitter_fn, s);
565 fdepths = malloc(8 * s->nr_files);
566 cache_hit = cache_miss = reap = calls = done = 0;
568 unsigned long this_done = 0;
569 unsigned long this_reap = 0;
570 unsigned long this_call = 0;
571 unsigned long this_cache_hit = 0;
572 unsigned long this_cache_miss = 0;
573 unsigned long rpc = 0, ipc = 0;
577 this_done += s->done;
578 this_call += s->calls;
579 this_reap += s->reaps;
580 this_cache_hit += s->cachehit;
581 this_cache_miss += s->cachemiss;
582 if (this_cache_hit && this_cache_miss) {
583 unsigned long hits, total;
585 hits = this_cache_hit - cache_hit;
586 total = hits + this_cache_miss - cache_miss;
587 hit = (double) hits / (double) total;
590 if (this_call - calls) {
591 rpc = (this_done - done) / (this_call - calls);
592 ipc = (this_reap - reap) / (this_call - calls);
595 file_depths(fdepths);
596 printf("IOPS=%lu, IOS/call=%ld/%ld, inflight=%u (%s), Cachehit=%0.2f%%\n",
597 this_done - done, rpc, ipc, s->inflight,
602 cache_hit = s->cachehit;
603 cache_miss = s->cachemiss;
606 pthread_join(s->thread, &ret);