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 min(a, b) ((a < b) ? (a) : (b))
33 unsigned *ring_entries;
42 unsigned *ring_entries;
43 struct io_uring_cqe *cqes;
48 #define BATCH_SUBMIT 32
49 #define BATCH_COMPLETE 32
55 static unsigned sq_ring_mask, cq_ring_mask;
58 unsigned long max_blocks;
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;
76 unsigned long cachehit, cachemiss;
81 struct file files[MAX_FDS];
86 static struct submitter submitters[1];
87 static volatile int finish;
89 static int polled = 1; /* use IO polling */
90 static int fixedbufs = 1; /* use fixed user buffers */
91 static int register_files = 1; /* use fixed files */
92 static int buffered = 0; /* use buffered IO, not O_DIRECT */
93 static int sq_thread_poll = 0; /* use kernel submission/poller thread */
94 static int sq_thread_cpu = -1; /* pin above thread to this CPU */
95 static int do_nop = 0; /* no-op SQ ring commands */
97 static int io_uring_register_buffers(struct submitter *s)
102 return syscall(__NR_sys_io_uring_register, s->ring_fd,
103 IORING_REGISTER_BUFFERS, s->iovecs, DEPTH);
106 static int io_uring_register_files(struct submitter *s)
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;
119 return syscall(__NR_sys_io_uring_register, s->ring_fd,
120 IORING_REGISTER_FILES, s->fds, s->nr_files);
123 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
125 return syscall(__NR_sys_io_uring_setup, entries, p);
128 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
129 unsigned int min_complete, unsigned int flags)
131 return syscall(__NR_sys_io_uring_enter, s->ring_fd, to_submit,
132 min_complete, flags);
135 static int gettid(void)
137 return syscall(__NR_gettid);
140 static unsigned file_depth(struct submitter *s)
142 return (DEPTH + s->nr_files - 1) / s->nr_files;
145 static void init_io(struct submitter *s, unsigned index)
147 struct io_uring_sqe *sqe = &s->sqes[index];
148 unsigned long offset;
153 sqe->opcode = IORING_OP_NOP;
157 if (s->nr_files == 1) {
160 f = &s->files[s->cur_file];
161 if (f->pending_ios >= file_depth(s)) {
163 if (s->cur_file == s->nr_files)
165 f = &s->files[s->cur_file];
170 lrand48_r(&s->rand, &r);
171 offset = (r % (f->max_blocks - 1)) * BS;
173 if (register_files) {
174 sqe->flags = IOSQE_FIXED_FILE;
175 sqe->fd = f->fixed_fd;
178 sqe->fd = f->real_fd;
181 sqe->opcode = IORING_OP_READ_FIXED;
182 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
184 sqe->buf_index = index;
186 sqe->opcode = IORING_OP_READV;
187 sqe->addr = (unsigned long) &s->iovecs[index];
193 sqe->user_data = (unsigned long) f;
196 static int prep_more_ios(struct submitter *s, int max_ios)
198 struct io_sq_ring *ring = &s->sq_ring;
199 unsigned index, tail, next_tail, prepped = 0;
201 next_tail = tail = *ring->tail;
205 if (next_tail == *ring->head)
208 index = tail & sq_ring_mask;
210 ring->array[index] = index;
213 } while (prepped < max_ios);
215 if (*ring->tail != tail) {
216 /* order tail store with writes to sqes above */
224 static int get_file_size(struct file *f)
228 if (fstat(f->real_fd, &st) < 0)
230 if (S_ISBLK(st.st_mode)) {
231 unsigned long long bytes;
233 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
236 f->max_blocks = bytes / BS;
238 } else if (S_ISREG(st.st_mode)) {
239 f->max_blocks = st.st_size / BS;
246 static int reap_events(struct submitter *s)
248 struct io_cq_ring *ring = &s->cq_ring;
249 struct io_uring_cqe *cqe;
250 unsigned head, reaped = 0;
257 if (head == *ring->tail)
259 cqe = &ring->cqes[head & cq_ring_mask];
261 f = (struct file *) (uintptr_t) cqe->user_data;
263 if (cqe->res != BS) {
264 printf("io: unexpected ret=%d\n", cqe->res);
268 if (cqe->flags & IOCQE_FLAG_CACHEHIT)
276 s->inflight -= reaped;
282 static void *submitter_fn(void *data)
284 struct submitter *s = data;
285 struct io_sq_ring *ring = &s->sq_ring;
288 printf("submitter=%d\n", gettid());
290 srand48_r(pthread_self(), &s->rand);
294 int to_wait, to_submit, this_reap, to_prep;
296 if (!prepped && s->inflight < DEPTH) {
297 to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
298 prepped = prep_more_ios(s, to_prep);
300 s->inflight += prepped;
304 if (to_submit && (s->inflight + to_submit <= DEPTH))
307 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
310 * Only need to call io_uring_enter if we're not using SQ thread
311 * poll, or if IORING_SQ_NEED_WAKEUP is set.
313 if (!sq_thread_poll || (*ring->flags & IORING_SQ_NEED_WAKEUP)) {
317 flags = IORING_ENTER_GETEVENTS;
318 if ((*ring->flags & IORING_SQ_NEED_WAKEUP))
319 flags |= IORING_ENTER_SQ_WAKEUP;
320 ret = io_uring_enter(s, to_submit, to_wait, flags);
325 * For non SQ thread poll, we already got the events we needed
326 * through the io_uring_enter() above. For SQ thread poll, we
327 * need to loop here until we find enough events.
338 } while (sq_thread_poll && this_reap < to_wait);
339 s->reaps += this_reap;
347 } else if (ret < to_submit) {
348 int diff = to_submit - ret;
357 } else if (ret < 0) {
358 if (errno == EAGAIN) {
366 printf("io_submit: %s\n", strerror(errno));
369 } while (!s->finish);
375 static void sig_int(int sig)
377 printf("Exiting on signal %d\n", sig);
378 submitters[0].finish = 1;
382 static void arm_sig_int(void)
384 struct sigaction act;
386 memset(&act, 0, sizeof(act));
387 act.sa_handler = sig_int;
388 act.sa_flags = SA_RESTART;
389 sigaction(SIGINT, &act, NULL);
392 static int setup_ring(struct submitter *s)
394 struct io_sq_ring *sring = &s->sq_ring;
395 struct io_cq_ring *cring = &s->cq_ring;
396 struct io_uring_params p;
400 memset(&p, 0, sizeof(p));
402 if (polled && !do_nop)
403 p.flags |= IORING_SETUP_IOPOLL;
404 if (sq_thread_poll) {
405 p.flags |= IORING_SETUP_SQPOLL;
406 if (sq_thread_cpu != -1) {
407 p.flags |= IORING_SETUP_SQ_AFF;
408 p.sq_thread_cpu = sq_thread_cpu;
412 fd = io_uring_setup(DEPTH, &p);
414 perror("io_uring_setup");
420 ret = io_uring_register_buffers(s);
422 perror("io_uring_register_buffers");
427 if (register_files) {
428 ret = io_uring_register_files(s);
430 perror("io_uring_register_files");
435 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
436 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
438 printf("sq_ring ptr = 0x%p\n", ptr);
439 sring->head = ptr + p.sq_off.head;
440 sring->tail = ptr + p.sq_off.tail;
441 sring->ring_mask = ptr + p.sq_off.ring_mask;
442 sring->ring_entries = ptr + p.sq_off.ring_entries;
443 sring->flags = ptr + p.sq_off.flags;
444 sring->array = ptr + p.sq_off.array;
445 sq_ring_mask = *sring->ring_mask;
447 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
448 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
450 printf("sqes ptr = 0x%p\n", s->sqes);
452 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
453 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
455 printf("cq_ring ptr = 0x%p\n", ptr);
456 cring->head = ptr + p.cq_off.head;
457 cring->tail = ptr + p.cq_off.tail;
458 cring->ring_mask = ptr + p.cq_off.ring_mask;
459 cring->ring_entries = ptr + p.cq_off.ring_entries;
460 cring->cqes = ptr + p.cq_off.cqes;
461 cq_ring_mask = *cring->ring_mask;
465 static void file_depths(char *buf)
467 struct submitter *s = &submitters[0];
473 for (i = 0; i < s->nr_files; i++) {
474 struct file *f = &s->files[i];
476 if (i + 1 == s->nr_files)
477 p += sprintf(p, "%d", f->pending_ios);
479 p += sprintf(p, "%d, ", f->pending_ios);
483 int main(int argc, char *argv[])
485 struct submitter *s = &submitters[0];
486 unsigned long done, calls, reap, cache_hit, cache_miss;
487 int err, i, flags, fd;
491 if (!do_nop && argc < 2) {
492 printf("%s: filename\n", argv[0]);
496 flags = O_RDONLY | O_NOATIME;
501 while (!do_nop && i < argc) {
502 struct file *f = &s->files[s->nr_files];
504 fd = open(argv[i], flags);
510 if (get_file_size(f)) {
511 printf("failed getting size of device/file\n");
514 if (f->max_blocks <= 1) {
515 printf("Zero file/device size?\n");
520 printf("Added file %s\n", argv[i]);
528 rlim.rlim_cur = RLIM_INFINITY;
529 rlim.rlim_max = RLIM_INFINITY;
530 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
538 for (i = 0; i < DEPTH; i++) {
541 if (posix_memalign(&buf, BS, BS)) {
542 printf("failed alloc\n");
545 s->iovecs[i].iov_base = buf;
546 s->iovecs[i].iov_len = BS;
551 printf("ring setup failed: %s, %d\n", strerror(errno), err);
554 printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
555 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
557 pthread_create(&s->thread, NULL, submitter_fn, s);
559 fdepths = malloc(8 * s->nr_files);
560 cache_hit = cache_miss = reap = calls = done = 0;
562 unsigned long this_done = 0;
563 unsigned long this_reap = 0;
564 unsigned long this_call = 0;
565 unsigned long this_cache_hit = 0;
566 unsigned long this_cache_miss = 0;
567 unsigned long rpc = 0, ipc = 0;
571 this_done += s->done;
572 this_call += s->calls;
573 this_reap += s->reaps;
574 this_cache_hit += s->cachehit;
575 this_cache_miss += s->cachemiss;
576 if (this_cache_hit && this_cache_miss) {
577 unsigned long hits, total;
579 hits = this_cache_hit - cache_hit;
580 total = hits + this_cache_miss - cache_miss;
581 hit = (double) hits / (double) total;
584 if (this_call - calls) {
585 rpc = (this_done - done) / (this_call - calls);
586 ipc = (this_reap - reap) / (this_call - calls);
589 file_depths(fdepths);
590 printf("IOPS=%lu, IOS/call=%ld/%ld, inflight=%u (%s), Cachehit=%0.2f%%\n",
591 this_done - done, rpc, ipc, s->inflight,
596 cache_hit = s->cachehit;
597 cache_miss = s->cachemiss;
600 pthread_join(s->thread, &ret);