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;
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 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 */
97 static int io_uring_register_buffers(struct submitter *s)
99 struct io_uring_register_buffers reg = {
104 return syscall(__NR_sys_io_uring_register, s->ring_fd,
105 IORING_REGISTER_BUFFERS, ®);
108 static int io_uring_register_files(struct submitter *s)
110 struct io_uring_register_files reg;
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 reg.nr_fds = s->nr_files;
121 return syscall(__NR_sys_io_uring_register, s->ring_fd,
122 IORING_REGISTER_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);
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;
154 if (s->nr_files == 1) {
157 f = &s->files[s->cur_file];
158 if (f->pending_ios >= file_depth(s)) {
160 if (s->cur_file == s->nr_files)
166 lrand48_r(&s->rand, &r);
167 offset = (r % (f->max_blocks - 1)) * BS;
169 sqe->flags = IOSQE_FIXED_FILE;
171 sqe->opcode = IORING_OP_READ_FIXED;
172 sqe->addr = s->iovecs[index].iov_base;
174 sqe->buf_index = index;
176 sqe->opcode = IORING_OP_READV;
177 sqe->addr = &s->iovecs[index];
182 sqe->fd = f->fixed_fd;
184 sqe->user_data = (unsigned long) f;
187 static int prep_more_ios(struct submitter *s, int max_ios)
189 struct io_sq_ring *ring = &s->sq_ring;
190 unsigned index, tail, next_tail, prepped = 0;
192 next_tail = tail = *ring->tail;
196 if (next_tail == *ring->head)
199 index = tail & sq_ring_mask;
201 ring->array[index] = index;
204 } while (prepped < max_ios);
206 if (*ring->tail != tail) {
207 /* order tail store with writes to sqes above */
215 static int get_file_size(struct file *f)
219 if (fstat(f->real_fd, &st) < 0)
221 if (S_ISBLK(st.st_mode)) {
222 unsigned long long bytes;
224 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
227 f->max_blocks = bytes / BS;
229 } else if (S_ISREG(st.st_mode)) {
230 f->max_blocks = st.st_size / BS;
237 static int reap_events(struct submitter *s)
239 struct io_cq_ring *ring = &s->cq_ring;
240 struct io_uring_cqe *cqe;
241 unsigned head, reaped = 0;
248 if (head == *ring->tail)
250 cqe = &ring->cqes[head & cq_ring_mask];
251 f = (struct file *) cqe->user_data;
253 if (cqe->res != BS) {
254 printf("io: unexpected ret=%d\n", cqe->res);
257 if (cqe->flags & IOCQE_FLAG_CACHEHIT)
265 s->inflight -= reaped;
271 static void *submitter_fn(void *data)
273 struct submitter *s = data;
274 struct io_sq_ring *ring = &s->sq_ring;
277 printf("submitter=%d\n", gettid());
279 srand48_r(pthread_self(), &s->rand);
283 int to_wait, to_submit, this_reap, to_prep;
285 if (!prepped && s->inflight < DEPTH) {
286 to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
287 prepped = prep_more_ios(s, to_prep);
289 s->inflight += prepped;
293 if (s->inflight + BATCH_SUBMIT < DEPTH)
296 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
299 * Only need to call io_uring_enter if we're not using SQ thread
300 * poll, or if IORING_SQ_NEED_WAKEUP is set.
302 if (!sq_thread_poll || (*ring->flags & IORING_SQ_NEED_WAKEUP)) {
306 flags = IORING_ENTER_GETEVENTS;
307 ret = io_uring_enter(s, to_submit, to_wait, flags);
312 * For non SQ thread poll, we already got the events we needed
313 * through the io_uring_enter() above. For SQ thread poll, we
314 * need to loop here until we find enough events.
324 } while (sq_thread_poll && this_reap < to_wait);
325 s->reaps += this_reap;
333 } else if (ret < to_submit) {
334 int diff = to_submit - ret;
343 } else if (ret < 0) {
344 if (errno == EAGAIN) {
352 printf("io_submit: %s\n", strerror(errno));
355 } while (!s->finish);
361 static void sig_int(int sig)
363 printf("Exiting on signal %d\n", sig);
364 submitters[0].finish = 1;
368 static void arm_sig_int(void)
370 struct sigaction act;
372 memset(&act, 0, sizeof(act));
373 act.sa_handler = sig_int;
374 act.sa_flags = SA_RESTART;
375 sigaction(SIGINT, &act, NULL);
378 static int setup_ring(struct submitter *s)
380 struct io_sq_ring *sring = &s->sq_ring;
381 struct io_cq_ring *cring = &s->cq_ring;
382 struct io_uring_params p;
386 memset(&p, 0, sizeof(p));
389 p.flags |= IORING_SETUP_IOPOLL;
390 if (sq_thread_poll) {
391 p.flags |= IORING_SETUP_SQPOLL;
392 if (sq_thread_cpu != -1) {
393 p.flags |= IORING_SETUP_SQ_AFF;
394 p.sq_thread_cpu = sq_thread_cpu;
398 fd = io_uring_setup(DEPTH, &p);
400 perror("io_uring_setup");
406 ret = io_uring_register_buffers(s);
408 perror("io_uring_register_buffers");
413 ret = io_uring_register_files(s);
415 perror("io_uring_register_files");
419 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
420 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
422 printf("sq_ring ptr = 0x%p\n", ptr);
423 sring->head = ptr + p.sq_off.head;
424 sring->tail = ptr + p.sq_off.tail;
425 sring->ring_mask = ptr + p.sq_off.ring_mask;
426 sring->ring_entries = ptr + p.sq_off.ring_entries;
427 sring->flags = ptr + p.sq_off.flags;
428 sring->array = ptr + p.sq_off.array;
429 sq_ring_mask = *sring->ring_mask;
431 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
432 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
434 printf("sqes ptr = 0x%p\n", s->sqes);
436 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
437 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
439 printf("cq_ring ptr = 0x%p\n", ptr);
440 cring->head = ptr + p.cq_off.head;
441 cring->tail = ptr + p.cq_off.tail;
442 cring->ring_mask = ptr + p.cq_off.ring_mask;
443 cring->ring_entries = ptr + p.cq_off.ring_entries;
444 cring->cqes = ptr + p.cq_off.cqes;
445 cq_ring_mask = *cring->ring_mask;
449 int main(int argc, char *argv[])
451 struct submitter *s = &submitters[0];
452 unsigned long done, calls, reap, cache_hit, cache_miss;
453 int err, i, flags, fd;
458 printf("%s: filename\n", argv[0]);
462 flags = O_RDONLY | O_NOATIME;
468 struct file *f = &s->files[s->nr_files];
470 fd = open(argv[i], flags);
476 if (get_file_size(f)) {
477 printf("failed getting size of device/file\n");
480 if (f->max_blocks <= 1) {
481 printf("Zero file/device size?\n");
486 printf("Added file %s\n", argv[i]);
491 rlim.rlim_cur = RLIM_INFINITY;
492 rlim.rlim_max = RLIM_INFINITY;
493 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
500 for (i = 0; i < DEPTH; i++) {
503 if (posix_memalign(&buf, BS, BS)) {
504 printf("failed alloc\n");
507 s->iovecs[i].iov_base = buf;
508 s->iovecs[i].iov_len = BS;
513 printf("ring setup failed: %s, %d\n", strerror(errno), err);
516 printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
517 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
519 pthread_create(&s->thread, NULL, submitter_fn, s);
521 cache_hit = cache_miss = reap = calls = done = 0;
523 unsigned long this_done = 0;
524 unsigned long this_reap = 0;
525 unsigned long this_call = 0;
526 unsigned long this_cache_hit = 0;
527 unsigned long this_cache_miss = 0;
528 unsigned long rpc = 0, ipc = 0;
532 this_done += s->done;
533 this_call += s->calls;
534 this_reap += s->reaps;
535 this_cache_hit += s->cachehit;
536 this_cache_miss += s->cachemiss;
537 if (this_cache_hit && this_cache_miss) {
538 unsigned long hits, total;
540 hits = this_cache_hit - cache_hit;
541 total = hits + this_cache_miss - cache_miss;
542 hit = (double) hits / (double) total;
545 if (this_call - calls) {
546 rpc = (this_done - done) / (this_call - calls);
547 ipc = (this_reap - reap) / (this_call - calls);
550 printf("IOPS=%lu, IOS/call=%ld/%ld, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
551 this_done - done, rpc, ipc, s->inflight,
552 *s->cq_ring.head, *s->cq_ring.tail, hit);
556 cache_hit = s->cachehit;
557 cache_miss = s->cachemiss;
560 pthread_join(s->thread, &ret);