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;
47 #define BATCH_SUBMIT 32
48 #define BATCH_COMPLETE 32
54 static unsigned sq_ring_mask, cq_ring_mask;
57 unsigned long max_blocks;
66 struct drand48_data rand;
67 struct io_sq_ring sq_ring;
68 struct io_uring_sqe *sqes;
69 struct io_cq_ring cq_ring;
74 unsigned long cachehit, cachemiss;
79 struct file files[MAX_FDS];
82 struct iovec iovecs[];
85 static struct submitter *submitter;
86 static volatile int finish;
88 static int depth = DEPTH;
89 static int batch_submit = BATCH_SUBMIT;
90 static int batch_complete = BATCH_COMPLETE;
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)
104 return syscall(__NR_sys_io_uring_register, s->ring_fd,
105 IORING_REGISTER_BUFFERS, s->iovecs, depth);
108 static int io_uring_register_files(struct submitter *s)
115 s->fds = calloc(s->nr_files, sizeof(__s32));
116 for (i = 0; i < s->nr_files; i++) {
117 s->fds[i] = s->files[i].real_fd;
118 s->files[i].fixed_fd = i;
121 return syscall(__NR_sys_io_uring_register, s->ring_fd,
122 IORING_REGISTER_FILES, s->fds, s->nr_files);
125 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
127 return syscall(__NR_sys_io_uring_setup, entries, p);
130 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
131 unsigned int min_complete, unsigned int flags)
133 return syscall(__NR_sys_io_uring_enter, s->ring_fd, to_submit,
134 min_complete, flags, NULL, 0);
137 static int gettid(void)
139 return syscall(__NR_gettid);
142 static unsigned file_depth(struct submitter *s)
144 return (depth + s->nr_files - 1) / s->nr_files;
147 static void init_io(struct submitter *s, unsigned index)
149 struct io_uring_sqe *sqe = &s->sqes[index];
150 unsigned long offset;
155 sqe->opcode = IORING_OP_NOP;
159 if (s->nr_files == 1) {
162 f = &s->files[s->cur_file];
163 if (f->pending_ios >= file_depth(s)) {
165 if (s->cur_file == s->nr_files)
167 f = &s->files[s->cur_file];
172 lrand48_r(&s->rand, &r);
173 offset = (r % (f->max_blocks - 1)) * BS;
175 if (register_files) {
176 sqe->flags = IOSQE_FIXED_FILE;
177 sqe->fd = f->fixed_fd;
180 sqe->fd = f->real_fd;
183 sqe->opcode = IORING_OP_READ_FIXED;
184 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
186 sqe->buf_index = index;
188 sqe->opcode = IORING_OP_READV;
189 sqe->addr = (unsigned long) &s->iovecs[index];
195 sqe->user_data = (unsigned long) f;
198 static int prep_more_ios(struct submitter *s, int max_ios)
200 struct io_sq_ring *ring = &s->sq_ring;
201 unsigned index, tail, next_tail, prepped = 0;
203 next_tail = tail = *ring->tail;
207 if (next_tail == *ring->head)
210 index = tail & sq_ring_mask;
212 ring->array[index] = index;
215 } while (prepped < max_ios);
217 if (*ring->tail != tail) {
218 /* order tail store with writes to sqes above */
226 static int get_file_size(struct file *f)
230 if (fstat(f->real_fd, &st) < 0)
232 if (S_ISBLK(st.st_mode)) {
233 unsigned long long bytes;
235 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
238 f->max_blocks = bytes / BS;
240 } else if (S_ISREG(st.st_mode)) {
241 f->max_blocks = st.st_size / BS;
248 static int reap_events(struct submitter *s)
250 struct io_cq_ring *ring = &s->cq_ring;
251 struct io_uring_cqe *cqe;
252 unsigned head, reaped = 0;
259 if (head == *ring->tail)
261 cqe = &ring->cqes[head & cq_ring_mask];
263 f = (struct file *) (uintptr_t) cqe->user_data;
265 if (cqe->res != BS) {
266 printf("io: unexpected ret=%d\n", cqe->res);
267 if (polled && cqe->res == -EOPNOTSUPP)
268 printf("Your filesystem doesn't support poll\n");
272 if (cqe->flags & IOCQE_FLAG_CACHEHIT)
280 s->inflight -= reaped;
286 static void *submitter_fn(void *data)
288 struct submitter *s = data;
289 struct io_sq_ring *ring = &s->sq_ring;
292 printf("submitter=%d\n", gettid());
294 srand48_r(pthread_self(), &s->rand);
298 int to_wait, to_submit, this_reap, to_prep;
300 if (!prepped && s->inflight < depth) {
301 to_prep = min(depth - s->inflight, batch_submit);
302 prepped = prep_more_ios(s, to_prep);
304 s->inflight += prepped;
308 if (to_submit && (s->inflight + to_submit <= depth))
311 to_wait = min(s->inflight + to_submit, batch_complete);
314 * Only need to call io_uring_enter if we're not using SQ thread
315 * poll, or if IORING_SQ_NEED_WAKEUP is set.
317 if (!sq_thread_poll || (*ring->flags & IORING_SQ_NEED_WAKEUP)) {
321 flags = IORING_ENTER_GETEVENTS;
322 if ((*ring->flags & IORING_SQ_NEED_WAKEUP))
323 flags |= IORING_ENTER_SQ_WAKEUP;
324 ret = io_uring_enter(s, to_submit, to_wait, flags);
329 * For non SQ thread poll, we already got the events we needed
330 * through the io_uring_enter() above. For SQ thread poll, we
331 * need to loop here until we find enough events.
342 } while (sq_thread_poll && this_reap < to_wait);
343 s->reaps += this_reap;
351 } else if (ret < to_submit) {
352 int diff = to_submit - ret;
361 } else if (ret < 0) {
362 if (errno == EAGAIN) {
370 printf("io_submit: %s\n", strerror(errno));
373 } while (!s->finish);
379 static void sig_int(int sig)
381 printf("Exiting on signal %d\n", sig);
382 submitter->finish = 1;
386 static void arm_sig_int(void)
388 struct sigaction act;
390 memset(&act, 0, sizeof(act));
391 act.sa_handler = sig_int;
392 act.sa_flags = SA_RESTART;
393 sigaction(SIGINT, &act, NULL);
396 static int setup_ring(struct submitter *s)
398 struct io_sq_ring *sring = &s->sq_ring;
399 struct io_cq_ring *cring = &s->cq_ring;
400 struct io_uring_params p;
404 memset(&p, 0, sizeof(p));
406 if (polled && !do_nop)
407 p.flags |= IORING_SETUP_IOPOLL;
408 if (sq_thread_poll) {
409 p.flags |= IORING_SETUP_SQPOLL;
410 if (sq_thread_cpu != -1) {
411 p.flags |= IORING_SETUP_SQ_AFF;
412 p.sq_thread_cpu = sq_thread_cpu;
416 fd = io_uring_setup(depth, &p);
418 perror("io_uring_setup");
424 ret = io_uring_register_buffers(s);
426 perror("io_uring_register_buffers");
431 if (register_files) {
432 ret = io_uring_register_files(s);
434 perror("io_uring_register_files");
439 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
440 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
442 printf("sq_ring ptr = 0x%p\n", ptr);
443 sring->head = ptr + p.sq_off.head;
444 sring->tail = ptr + p.sq_off.tail;
445 sring->ring_mask = ptr + p.sq_off.ring_mask;
446 sring->ring_entries = ptr + p.sq_off.ring_entries;
447 sring->flags = ptr + p.sq_off.flags;
448 sring->array = ptr + p.sq_off.array;
449 sq_ring_mask = *sring->ring_mask;
451 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
452 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
454 printf("sqes ptr = 0x%p\n", s->sqes);
456 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
457 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
459 printf("cq_ring ptr = 0x%p\n", ptr);
460 cring->head = ptr + p.cq_off.head;
461 cring->tail = ptr + p.cq_off.tail;
462 cring->ring_mask = ptr + p.cq_off.ring_mask;
463 cring->ring_entries = ptr + p.cq_off.ring_entries;
464 cring->cqes = ptr + p.cq_off.cqes;
465 cq_ring_mask = *cring->ring_mask;
469 static void file_depths(char *buf)
471 struct submitter *s = submitter;
477 for (i = 0; i < s->nr_files; i++) {
478 struct file *f = &s->files[i];
480 if (i + 1 == s->nr_files)
481 p += sprintf(p, "%d", f->pending_ios);
483 p += sprintf(p, "%d, ", f->pending_ios);
487 static void usage(char *argv)
489 printf("%s [options] -- [filenames]\n"
490 " -d <int> : IO Depth, default %d\n"
491 " -s <int> : Batch submit, default %d\n"
492 " -c <int> : Batch complete, default %d\n",
493 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE);
497 int main(int argc, char *argv[])
500 unsigned long done, calls, reap, cache_hit, cache_miss;
501 int err, i, flags, fd, opt;
505 if (!do_nop && argc < 2) {
506 printf("%s: filename [options]\n", argv[0]);
510 while ((opt = getopt(argc, argv, "d:s:c:h?")) != -1) {
513 depth = atoi(optarg);
516 batch_submit = atoi(optarg);
519 batch_complete = atoi(optarg);
529 submitter = malloc(sizeof(*submitter) * depth * sizeof(struct iovec));
532 flags = O_RDONLY | O_NOATIME;
537 while (!do_nop && i < argc) {
540 if (s->nr_files == MAX_FDS) {
541 printf("Max number of files (%d) reached\n", MAX_FDS);
544 fd = open(argv[i], flags);
550 f = &s->files[s->nr_files];
552 if (get_file_size(f)) {
553 printf("failed getting size of device/file\n");
556 if (f->max_blocks <= 1) {
557 printf("Zero file/device size?\n");
562 printf("Added file %s\n", argv[i]);
570 rlim.rlim_cur = RLIM_INFINITY;
571 rlim.rlim_max = RLIM_INFINITY;
572 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
580 for (i = 0; i < depth; i++) {
583 if (posix_memalign(&buf, BS, BS)) {
584 printf("failed alloc\n");
587 s->iovecs[i].iov_base = buf;
588 s->iovecs[i].iov_len = BS;
593 printf("ring setup failed: %s, %d\n", strerror(errno), err);
596 printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
597 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", depth, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
599 pthread_create(&s->thread, NULL, submitter_fn, s);
601 fdepths = malloc(8 * s->nr_files);
602 cache_hit = cache_miss = reap = calls = done = 0;
604 unsigned long this_done = 0;
605 unsigned long this_reap = 0;
606 unsigned long this_call = 0;
607 unsigned long this_cache_hit = 0;
608 unsigned long this_cache_miss = 0;
609 unsigned long rpc = 0, ipc = 0;
613 this_done += s->done;
614 this_call += s->calls;
615 this_reap += s->reaps;
616 this_cache_hit += s->cachehit;
617 this_cache_miss += s->cachemiss;
618 if (this_cache_hit && this_cache_miss) {
619 unsigned long hits, total;
621 hits = this_cache_hit - cache_hit;
622 total = hits + this_cache_miss - cache_miss;
623 hit = (double) hits / (double) total;
626 if (this_call - calls) {
627 rpc = (this_done - done) / (this_call - calls);
628 ipc = (this_reap - reap) / (this_call - calls);
631 file_depths(fdepths);
632 printf("IOPS=%lu, IOS/call=%ld/%ld, inflight=%u (%s), Cachehit=%0.2f%%\n",
633 this_done - done, rpc, ipc, s->inflight,
638 cache_hit = s->cachehit;
639 cache_miss = s->cachemiss;
642 pthread_join(s->thread, &ret);