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/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 64
51 #define BATCH_COMPLETE 64
57 static unsigned sq_ring_mask, cq_ring_mask;
60 unsigned long max_blocks;
68 struct drand48_data rand;
69 struct io_sq_ring sq_ring;
70 struct io_uring_sqe *sqes;
71 struct iovec iovecs[DEPTH];
72 struct io_cq_ring cq_ring;
77 unsigned long cachehit, cachemiss;
80 struct file files[MAX_FDS];
85 static struct submitter submitters[1];
86 static volatile int finish;
88 static int polled = 1; /* use IO polling */
89 static int fixedbufs = 1; /* use fixed user buffers */
90 static int buffered = 0; /* use buffered IO, not O_DIRECT */
91 static int sq_thread_poll = 0; /* use kernel submission/poller thread */
92 static int sq_thread_cpu = -1; /* pin above thread to this CPU */
94 static int io_uring_register_buffers(struct submitter *s)
96 struct io_uring_register_buffers reg = {
101 return syscall(__NR_sys_io_uring_register, s->ring_fd,
102 IORING_REGISTER_BUFFERS, ®);
105 static int io_uring_register_files(struct submitter *s)
107 struct io_uring_register_files reg;
110 reg.fds = calloc(s->nr_files, sizeof(int));
111 for (i = 0; i < s->nr_files; i++)
112 reg.fds[i] = s->files[i].fd;
113 reg.nr_fds = s->nr_files;
115 ret = syscall(__NR_sys_io_uring_register, s->ring_fd,
116 IORING_REGISTER_FILES, ®);
121 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
123 return syscall(__NR_sys_io_uring_setup, entries, p);
126 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
127 unsigned int min_complete, unsigned int flags)
129 return syscall(__NR_sys_io_uring_enter, s->ring_fd, to_submit,
130 min_complete, flags);
133 static int gettid(void)
135 return syscall(__NR_gettid);
138 static unsigned file_depth(struct submitter *s)
140 return (DEPTH + s->nr_files - 1) / s->nr_files;
143 static void init_io(struct submitter *s, unsigned index)
145 struct io_uring_sqe *sqe = &s->sqes[index];
146 unsigned long offset;
150 if (s->nr_files == 1) {
153 f = &s->files[s->cur_file];
154 if (f->pending_ios >= file_depth(s)) {
156 if (s->cur_file == s->nr_files)
162 lrand48_r(&s->rand, &r);
163 offset = (r % (f->max_blocks - 1)) * BS;
165 sqe->flags = IOSQE_FIXED_FILE;
166 sqe->opcode = IORING_OP_READV;
168 sqe->addr = s->iovecs[index].iov_base;
170 sqe->buf_index = index;
171 sqe->flags |= IOSQE_FIXED_BUFFER;
173 sqe->addr = &s->iovecs[index];
180 sqe->data = (unsigned long) f;
183 static int prep_more_ios(struct submitter *s, int max_ios)
185 struct io_sq_ring *ring = &s->sq_ring;
186 unsigned index, tail, next_tail, prepped = 0;
188 next_tail = tail = *ring->tail;
192 if (next_tail == *ring->head)
195 index = tail & sq_ring_mask;
197 ring->array[index] = index;
200 } while (prepped < max_ios);
202 if (*ring->tail != tail) {
203 /* order tail store with writes to sqes above */
211 static int get_file_size(struct file *f)
215 if (fstat(f->fd, &st) < 0)
217 if (S_ISBLK(st.st_mode)) {
218 unsigned long long bytes;
220 if (ioctl(f->fd, BLKGETSIZE64, &bytes) != 0)
223 f->max_blocks = bytes / BS;
225 } else if (S_ISREG(st.st_mode)) {
226 f->max_blocks = st.st_size / BS;
233 static int reap_events(struct submitter *s)
235 struct io_cq_ring *ring = &s->cq_ring;
236 struct io_uring_cqe *cqe;
237 unsigned head, reaped = 0;
244 if (head == *ring->tail)
246 cqe = &ring->cqes[head & cq_ring_mask];
247 f = (struct file *) cqe->data;
249 if (cqe->res != BS) {
250 printf("io: unexpected ret=%d\n", cqe->res);
253 if (cqe->flags & IOCQE_FLAG_CACHEHIT)
261 s->inflight -= reaped;
267 static void *submitter_fn(void *data)
269 struct submitter *s = data;
270 struct io_sq_ring *ring = &s->sq_ring;
273 printf("submitter=%d\n", gettid());
275 srand48_r(pthread_self(), &s->rand);
279 int to_wait, to_submit, this_reap, to_prep;
281 if (!prepped && s->inflight < DEPTH) {
282 to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
283 prepped = prep_more_ios(s, to_prep);
285 s->inflight += prepped;
289 if (s->inflight + BATCH_SUBMIT < DEPTH)
292 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
295 * Only need to call io_uring_enter if we're not using SQ thread
296 * poll, or if IORING_SQ_NEED_WAKEUP is set.
298 if (!sq_thread_poll || (*ring->flags & IORING_SQ_NEED_WAKEUP)) {
299 ret = io_uring_enter(s, to_submit, to_wait,
300 IORING_ENTER_GETEVENTS);
305 * For non SQ thread poll, we already got the events we needed
306 * through the io_uring_enter() above. For SQ thread poll, we
307 * need to loop here until we find enough events.
317 } while (sq_thread_poll && this_reap < to_wait);
318 s->reaps += this_reap;
326 } else if (ret < to_submit) {
327 int diff = to_submit - ret;
336 } else if (ret < 0) {
337 if (errno == EAGAIN) {
345 printf("io_submit: %s\n", strerror(errno));
348 } while (!s->finish);
354 static void sig_int(int sig)
356 printf("Exiting on signal %d\n", sig);
357 submitters[0].finish = 1;
361 static void arm_sig_int(void)
363 struct sigaction act;
365 memset(&act, 0, sizeof(act));
366 act.sa_handler = sig_int;
367 act.sa_flags = SA_RESTART;
368 sigaction(SIGINT, &act, NULL);
371 static int setup_ring(struct submitter *s)
373 struct io_sq_ring *sring = &s->sq_ring;
374 struct io_cq_ring *cring = &s->cq_ring;
375 struct io_uring_params p;
379 memset(&p, 0, sizeof(p));
382 p.flags |= IORING_SETUP_IOPOLL;
383 if (sq_thread_poll) {
384 p.flags |= IORING_SETUP_SQPOLL;
385 if (sq_thread_cpu != -1)
386 p.flags |= IORING_SETUP_SQ_AFF;
389 fd = io_uring_setup(DEPTH, &p);
391 perror("io_uring_setup");
397 ret = io_uring_register_buffers(s);
399 perror("io_uring_register_buffers");
404 ret = io_uring_register_files(s);
406 perror("io_uring_register_files");
410 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
411 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
413 printf("sq_ring ptr = 0x%p\n", ptr);
414 sring->head = ptr + p.sq_off.head;
415 sring->tail = ptr + p.sq_off.tail;
416 sring->ring_mask = ptr + p.sq_off.ring_mask;
417 sring->ring_entries = ptr + p.sq_off.ring_entries;
418 sring->flags = ptr + p.sq_off.flags;
419 sring->array = ptr + p.sq_off.array;
420 sq_ring_mask = *sring->ring_mask;
422 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
423 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
425 printf("sqes ptr = 0x%p\n", s->sqes);
427 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
428 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
430 printf("cq_ring ptr = 0x%p\n", ptr);
431 cring->head = ptr + p.cq_off.head;
432 cring->tail = ptr + p.cq_off.tail;
433 cring->ring_mask = ptr + p.cq_off.ring_mask;
434 cring->ring_entries = ptr + p.cq_off.ring_entries;
435 cring->cqes = ptr + p.cq_off.cqes;
436 cq_ring_mask = *cring->ring_mask;
440 int main(int argc, char *argv[])
442 struct submitter *s = &submitters[0];
443 unsigned long done, calls, reap, cache_hit, cache_miss;
444 int err, i, flags, fd;
449 printf("%s: filename\n", argv[0]);
453 flags = O_RDONLY | O_NOATIME;
459 struct file *f = &s->files[s->nr_files];
461 fd = open(argv[i], flags);
467 if (get_file_size(f)) {
468 printf("failed getting size of device/file\n");
471 if (f->max_blocks <= 1) {
472 printf("Zero file/device size?\n");
477 printf("Added file %s\n", argv[i]);
482 rlim.rlim_cur = RLIM_INFINITY;
483 rlim.rlim_max = RLIM_INFINITY;
484 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
491 for (i = 0; i < DEPTH; i++) {
494 if (posix_memalign(&buf, BS, BS)) {
495 printf("failed alloc\n");
498 s->iovecs[i].iov_base = buf;
499 s->iovecs[i].iov_len = BS;
504 printf("ring setup failed: %s, %d\n", strerror(errno), err);
507 printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
508 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
510 pthread_create(&s->thread, NULL, submitter_fn, s);
512 cache_hit = cache_miss = reap = calls = done = 0;
514 unsigned long this_done = 0;
515 unsigned long this_reap = 0;
516 unsigned long this_call = 0;
517 unsigned long this_cache_hit = 0;
518 unsigned long this_cache_miss = 0;
519 unsigned long rpc = 0, ipc = 0;
523 this_done += s->done;
524 this_call += s->calls;
525 this_reap += s->reaps;
526 this_cache_hit += s->cachehit;
527 this_cache_miss += s->cachemiss;
528 if (this_cache_hit && this_cache_miss) {
529 unsigned long hits, total;
531 hits = this_cache_hit - cache_hit;
532 total = hits + this_cache_miss - cache_miss;
533 hit = (double) hits / (double) total;
536 if (this_call - calls) {
537 rpc = (this_done - done) / (this_call - calls);
538 ipc = (this_reap - reap) / (this_call - calls);
541 printf("IOPS=%lu, IOS/call=%ld/%ld, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
542 this_done - done, rpc, ipc, s->inflight,
543 *s->cq_ring.head, *s->cq_ring.tail, hit);
547 cache_hit = s->cachehit;
548 cache_miss = s->cachemiss;
551 pthread_join(s->thread, &ret);