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;
78 struct file files[MAX_FDS];
81 struct iovec iovecs[];
84 static struct submitter *submitter;
85 static volatile int finish;
87 static int depth = DEPTH;
88 static int batch_submit = BATCH_SUBMIT;
89 static int batch_complete = BATCH_COMPLETE;
90 static int polled = 1; /* use IO polling */
91 static int fixedbufs = 1; /* use fixed user buffers */
92 static int register_files = 1; /* use fixed files */
93 static int buffered = 0; /* use buffered IO, not O_DIRECT */
94 static int sq_thread_poll = 0; /* use kernel submission/poller thread */
95 static int sq_thread_cpu = -1; /* pin above thread to this CPU */
96 static int do_nop = 0; /* no-op SQ ring commands */
98 static int io_uring_register_buffers(struct submitter *s)
103 return syscall(__NR_sys_io_uring_register, s->ring_fd,
104 IORING_REGISTER_BUFFERS, s->iovecs, depth);
107 static int io_uring_register_files(struct submitter *s)
114 s->fds = calloc(s->nr_files, sizeof(__s32));
115 for (i = 0; i < s->nr_files; i++) {
116 s->fds[i] = s->files[i].real_fd;
117 s->files[i].fixed_fd = i;
120 return syscall(__NR_sys_io_uring_register, s->ring_fd,
121 IORING_REGISTER_FILES, s->fds, s->nr_files);
124 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
126 return syscall(__NR_sys_io_uring_setup, entries, p);
129 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
130 unsigned int min_complete, unsigned int flags)
132 return syscall(__NR_sys_io_uring_enter, s->ring_fd, to_submit,
133 min_complete, flags, NULL, 0);
136 #ifndef CONFIG_HAVE_GETTID
137 static int gettid(void)
139 return syscall(__NR_gettid);
143 static unsigned file_depth(struct submitter *s)
145 return (depth + s->nr_files - 1) / s->nr_files;
148 static void init_io(struct submitter *s, unsigned index)
150 struct io_uring_sqe *sqe = &s->sqes[index];
151 unsigned long offset;
156 sqe->opcode = IORING_OP_NOP;
160 if (s->nr_files == 1) {
163 f = &s->files[s->cur_file];
164 if (f->pending_ios >= file_depth(s)) {
166 if (s->cur_file == s->nr_files)
168 f = &s->files[s->cur_file];
173 lrand48_r(&s->rand, &r);
174 offset = (r % (f->max_blocks - 1)) * BS;
176 if (register_files) {
177 sqe->flags = IOSQE_FIXED_FILE;
178 sqe->fd = f->fixed_fd;
181 sqe->fd = f->real_fd;
184 sqe->opcode = IORING_OP_READ_FIXED;
185 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
187 sqe->buf_index = index;
189 sqe->opcode = IORING_OP_READV;
190 sqe->addr = (unsigned long) &s->iovecs[index];
196 sqe->user_data = (unsigned long) f;
199 static int prep_more_ios(struct submitter *s, int max_ios)
201 struct io_sq_ring *ring = &s->sq_ring;
202 unsigned index, tail, next_tail, prepped = 0;
204 next_tail = tail = *ring->tail;
208 if (next_tail == *ring->head)
211 index = tail & sq_ring_mask;
213 ring->array[index] = index;
216 } while (prepped < max_ios);
218 if (*ring->tail != tail) {
219 /* order tail store with writes to sqes above */
227 static int get_file_size(struct file *f)
231 if (fstat(f->real_fd, &st) < 0)
233 if (S_ISBLK(st.st_mode)) {
234 unsigned long long bytes;
236 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
239 f->max_blocks = bytes / BS;
241 } else if (S_ISREG(st.st_mode)) {
242 f->max_blocks = st.st_size / BS;
249 static int reap_events(struct submitter *s)
251 struct io_cq_ring *ring = &s->cq_ring;
252 struct io_uring_cqe *cqe;
253 unsigned head, reaped = 0;
260 if (head == *ring->tail)
262 cqe = &ring->cqes[head & cq_ring_mask];
264 f = (struct file *) (uintptr_t) cqe->user_data;
266 if (cqe->res != BS) {
267 printf("io: unexpected ret=%d\n", cqe->res);
268 if (polled && cqe->res == -EOPNOTSUPP)
269 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
277 s->inflight -= reaped;
283 static void *submitter_fn(void *data)
285 struct submitter *s = data;
286 struct io_sq_ring *ring = &s->sq_ring;
289 printf("submitter=%d\n", gettid());
291 srand48_r(pthread_self(), &s->rand);
295 int to_wait, to_submit, this_reap, to_prep;
297 if (!prepped && s->inflight < depth) {
298 to_prep = min(depth - s->inflight, batch_submit);
299 prepped = prep_more_ios(s, to_prep);
301 s->inflight += prepped;
305 if (to_submit && (s->inflight + to_submit <= depth))
308 to_wait = min(s->inflight + to_submit, batch_complete);
311 * Only need to call io_uring_enter if we're not using SQ thread
312 * poll, or if IORING_SQ_NEED_WAKEUP is set.
314 if (!sq_thread_poll || (*ring->flags & IORING_SQ_NEED_WAKEUP)) {
318 flags = IORING_ENTER_GETEVENTS;
319 if ((*ring->flags & IORING_SQ_NEED_WAKEUP))
320 flags |= IORING_ENTER_SQ_WAKEUP;
321 ret = io_uring_enter(s, to_submit, to_wait, flags);
326 * For non SQ thread poll, we already got the events we needed
327 * through the io_uring_enter() above. For SQ thread poll, we
328 * need to loop here until we find enough events.
339 } while (sq_thread_poll && this_reap < to_wait);
340 s->reaps += this_reap;
348 } else if (ret < to_submit) {
349 int diff = to_submit - ret;
358 } else if (ret < 0) {
359 if (errno == EAGAIN) {
367 printf("io_submit: %s\n", strerror(errno));
370 } while (!s->finish);
376 static void sig_int(int sig)
378 printf("Exiting on signal %d\n", sig);
379 submitter->finish = 1;
383 static void arm_sig_int(void)
385 struct sigaction act;
387 memset(&act, 0, sizeof(act));
388 act.sa_handler = sig_int;
389 act.sa_flags = SA_RESTART;
390 sigaction(SIGINT, &act, NULL);
393 static int setup_ring(struct submitter *s)
395 struct io_sq_ring *sring = &s->sq_ring;
396 struct io_cq_ring *cring = &s->cq_ring;
397 struct io_uring_params p;
401 memset(&p, 0, sizeof(p));
403 if (polled && !do_nop)
404 p.flags |= IORING_SETUP_IOPOLL;
405 if (sq_thread_poll) {
406 p.flags |= IORING_SETUP_SQPOLL;
407 if (sq_thread_cpu != -1) {
408 p.flags |= IORING_SETUP_SQ_AFF;
409 p.sq_thread_cpu = sq_thread_cpu;
413 fd = io_uring_setup(depth, &p);
415 perror("io_uring_setup");
421 ret = io_uring_register_buffers(s);
423 perror("io_uring_register_buffers");
428 if (register_files) {
429 ret = io_uring_register_files(s);
431 perror("io_uring_register_files");
436 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
437 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
439 printf("sq_ring ptr = 0x%p\n", ptr);
440 sring->head = ptr + p.sq_off.head;
441 sring->tail = ptr + p.sq_off.tail;
442 sring->ring_mask = ptr + p.sq_off.ring_mask;
443 sring->ring_entries = ptr + p.sq_off.ring_entries;
444 sring->flags = ptr + p.sq_off.flags;
445 sring->array = ptr + p.sq_off.array;
446 sq_ring_mask = *sring->ring_mask;
448 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
449 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
451 printf("sqes ptr = 0x%p\n", s->sqes);
453 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
454 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
456 printf("cq_ring ptr = 0x%p\n", ptr);
457 cring->head = ptr + p.cq_off.head;
458 cring->tail = ptr + p.cq_off.tail;
459 cring->ring_mask = ptr + p.cq_off.ring_mask;
460 cring->ring_entries = ptr + p.cq_off.ring_entries;
461 cring->cqes = ptr + p.cq_off.cqes;
462 cq_ring_mask = *cring->ring_mask;
466 static void file_depths(char *buf)
468 struct submitter *s = submitter;
474 for (i = 0; i < s->nr_files; i++) {
475 struct file *f = &s->files[i];
477 if (i + 1 == s->nr_files)
478 p += sprintf(p, "%d", f->pending_ios);
480 p += sprintf(p, "%d, ", f->pending_ios);
484 static void usage(char *argv)
486 printf("%s [options] -- [filenames]\n"
487 " -d <int> : IO Depth, default %d\n"
488 " -s <int> : Batch submit, default %d\n"
489 " -c <int> : Batch complete, default %d\n",
490 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE);
494 int main(int argc, char *argv[])
497 unsigned long done, calls, reap;
498 int err, i, flags, fd, opt;
502 if (!do_nop && argc < 2) {
503 printf("%s: filename [options]\n", argv[0]);
507 while ((opt = getopt(argc, argv, "d:s:c:h?")) != -1) {
510 depth = atoi(optarg);
513 batch_submit = atoi(optarg);
516 batch_complete = atoi(optarg);
526 submitter = malloc(sizeof(*submitter) + depth * sizeof(struct iovec));
527 memset(submitter, 0, sizeof(*submitter) + depth * sizeof(struct iovec));
530 flags = O_RDONLY | O_NOATIME;
535 while (!do_nop && i < argc) {
538 if (s->nr_files == MAX_FDS) {
539 printf("Max number of files (%d) reached\n", MAX_FDS);
542 fd = open(argv[i], flags);
548 f = &s->files[s->nr_files];
550 if (get_file_size(f)) {
551 printf("failed getting size of device/file\n");
554 if (f->max_blocks <= 1) {
555 printf("Zero file/device size?\n");
560 printf("Added file %s\n", argv[i]);
568 rlim.rlim_cur = RLIM_INFINITY;
569 rlim.rlim_max = RLIM_INFINITY;
570 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
578 for (i = 0; i < depth; i++) {
581 if (posix_memalign(&buf, BS, BS)) {
582 printf("failed alloc\n");
585 s->iovecs[i].iov_base = buf;
586 s->iovecs[i].iov_len = BS;
591 printf("ring setup failed: %s, %d\n", strerror(errno), err);
594 printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
595 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", depth, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
597 pthread_create(&s->thread, NULL, submitter_fn, s);
599 fdepths = malloc(8 * s->nr_files);
600 reap = calls = done = 0;
602 unsigned long this_done = 0;
603 unsigned long this_reap = 0;
604 unsigned long this_call = 0;
605 unsigned long rpc = 0, ipc = 0;
608 this_done += s->done;
609 this_call += s->calls;
610 this_reap += s->reaps;
611 if (this_call - calls) {
612 rpc = (this_done - done) / (this_call - calls);
613 ipc = (this_reap - reap) / (this_call - calls);
616 file_depths(fdepths);
617 printf("IOPS=%lu, IOS/call=%ld/%ld, inflight=%u (%s)\n",
618 this_done - done, rpc, ipc, s->inflight,
625 pthread_join(s->thread, &ret);