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;
43 unsigned *ring_entries;
44 struct io_uring_event *events;
49 #define BATCH_SUBMIT 8
50 #define BATCH_COMPLETE 8
54 static unsigned sq_ring_mask, cq_ring_mask;
58 unsigned long max_blocks;
60 struct drand48_data rand;
61 struct io_sq_ring sq_ring;
62 struct io_uring_iocb *iocbs;
63 struct iovec iovecs[DEPTH];
64 struct io_cq_ring cq_ring;
69 unsigned long cachehit, cachemiss;
74 static struct submitter submitters[1];
75 static volatile int finish;
77 static int polled = 0; /* use IO polling */
78 static int fixedbufs = 0; /* use fixed user buffers */
79 static int buffered = 1; /* use buffered IO, not O_DIRECT */
80 static int sq_thread = 0; /* use kernel submission thread */
81 static int sq_thread_cpu = 0; /* pin above thread to this CPU */
83 static int io_uring_setup(unsigned entries, struct iovec *iovecs,
84 struct io_uring_params *p)
86 return syscall(__NR_sys_io_uring_setup, entries, iovecs, p);
89 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
90 unsigned int min_complete, unsigned int flags)
92 return syscall(__NR_sys_io_uring_enter, s->ring_fd, to_submit,
96 static int gettid(void)
98 return syscall(__NR_gettid);
101 static void init_io(struct submitter *s, int fd, unsigned index)
103 struct io_uring_iocb *iocb = &s->iocbs[index];
104 unsigned long offset;
107 lrand48_r(&s->rand, &r);
108 offset = (r % (s->max_blocks - 1)) * BS;
110 iocb->opcode = IORING_OP_READ;
115 iocb->addr = s->iovecs[index].iov_base;
119 static int prep_more_ios(struct submitter *s, int fd, int max_ios)
121 struct io_sq_ring *ring = &s->sq_ring;
122 unsigned index, tail, next_tail, prepped = 0;
124 next_tail = tail = *ring->tail;
128 if (next_tail == *ring->head)
131 index = tail & sq_ring_mask;
132 init_io(s, fd, index);
133 ring->array[index] = index;
136 } while (prepped < max_ios);
138 if (*ring->tail != tail) {
139 /* order tail store with writes to iocbs above */
147 static int get_file_size(int fd, unsigned long *blocks)
151 if (fstat(fd, &st) < 0)
153 if (S_ISBLK(st.st_mode)) {
154 unsigned long long bytes;
156 if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
159 *blocks = bytes / BS;
161 } else if (S_ISREG(st.st_mode)) {
162 *blocks = st.st_size / BS;
169 static int reap_events(struct submitter *s)
171 struct io_cq_ring *ring = &s->cq_ring;
172 struct io_uring_event *ev;
173 unsigned head, reaped = 0;
178 if (head == *ring->tail)
180 ev = &ring->events[head & cq_ring_mask];
182 struct io_uring_iocb *iocb = &s->iocbs[ev->index];
184 printf("io: unexpected ret=%d\n", ev->res);
185 printf("offset=%lu, size=%lu\n",
186 (unsigned long) iocb->off,
187 (unsigned long) iocb->len);
190 if (ev->flags & IOEV_FLAG_CACHEHIT)
198 s->inflight -= reaped;
204 static void *submitter_fn(void *data)
206 struct submitter *s = data;
207 int fd, ret, prepped, flags;
209 printf("submitter=%d\n", gettid());
214 fd = open(s->filename, flags);
220 if (get_file_size(fd, &s->max_blocks)) {
221 printf("failed getting size of device/file\n");
224 if (s->max_blocks <= 1) {
225 printf("Zero file/device size?\n");
230 srand48_r(pthread_self(), &s->rand);
234 int to_wait, to_submit, this_reap, to_prep;
236 if (!prepped && s->inflight < DEPTH) {
237 to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
238 prepped = prep_more_ios(s, fd, to_prep);
240 s->inflight += prepped;
244 if (s->inflight + BATCH_SUBMIT < DEPTH)
247 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
249 ret = io_uring_enter(s, to_submit, to_wait,
250 IORING_ENTER_GETEVENTS);
253 this_reap = reap_events(s);
256 s->reaps += this_reap;
264 } else if (ret < to_submit) {
265 int diff = to_submit - ret;
274 } else if (ret < 0) {
275 if (errno == EAGAIN) {
283 printf("io_submit: %s\n", strerror(errno));
286 } while (!s->finish);
294 static void sig_int(int sig)
296 printf("Exiting on signal %d\n", sig);
297 submitters[0].finish = 1;
301 static void arm_sig_int(void)
303 struct sigaction act;
305 memset(&act, 0, sizeof(act));
306 act.sa_handler = sig_int;
307 act.sa_flags = SA_RESTART;
308 sigaction(SIGINT, &act, NULL);
311 static int setup_ring(struct submitter *s)
313 struct io_sq_ring *sring = &s->sq_ring;
314 struct io_cq_ring *cring = &s->cq_ring;
315 struct io_uring_params p;
319 memset(&p, 0, sizeof(p));
322 p.flags |= IORING_SETUP_IOPOLL;
324 p.flags |= IORING_SETUP_FIXEDBUFS;
326 p.flags |= IORING_SETUP_SQWQ;
327 else if (sq_thread) {
328 p.flags |= IORING_SETUP_SQTHREAD;
329 p.sq_thread_cpu = sq_thread_cpu;
333 fd = io_uring_setup(DEPTH, s->iovecs, &p);
335 fd = io_uring_setup(DEPTH, NULL, &p);
337 perror("io_uring_setup");
342 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
343 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
345 printf("sq_ring ptr = 0x%p\n", ptr);
346 sring->head = ptr + p.sq_off.head;
347 sring->tail = ptr + p.sq_off.tail;
348 sring->ring_mask = ptr + p.sq_off.ring_mask;
349 sring->ring_entries = ptr + p.sq_off.ring_entries;
350 sring->array = ptr + p.sq_off.array;
351 sq_ring_mask = *sring->ring_mask;
353 s->iocbs = mmap(0, p.sq_entries * sizeof(struct io_uring_iocb),
354 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
356 printf("iocbs ptr = 0x%p\n", s->iocbs);
358 ptr = mmap(0, p.cq_off.events + p.cq_entries * sizeof(struct io_uring_event),
359 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
361 printf("cq_ring ptr = 0x%p\n", ptr);
362 cring->head = ptr + p.cq_off.head;
363 cring->tail = ptr + p.cq_off.tail;
364 cring->ring_mask = ptr + p.cq_off.ring_mask;
365 cring->ring_entries = ptr + p.cq_off.ring_entries;
366 cring->events = ptr + p.cq_off.events;
367 cq_ring_mask = *cring->ring_mask;
371 int main(int argc, char *argv[])
373 struct submitter *s = &submitters[0];
374 unsigned long done, calls, reap, cache_hit, cache_miss;
380 printf("%s: filename\n", argv[0]);
384 rlim.rlim_cur = RLIM_INFINITY;
385 rlim.rlim_max = RLIM_INFINITY;
386 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
393 for (i = 0; i < DEPTH; i++) {
396 if (posix_memalign(&buf, BS, BS)) {
397 printf("failed alloc\n");
400 s->iovecs[i].iov_base = buf;
401 s->iovecs[i].iov_len = BS;
406 printf("ring setup failed: %s, %d\n", strerror(errno), err);
409 printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
410 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
411 strcpy(s->filename, argv[1]);
413 pthread_create(&s->thread, NULL, submitter_fn, s);
415 cache_hit = cache_miss = reap = calls = done = 0;
417 unsigned long this_done = 0;
418 unsigned long this_reap = 0;
419 unsigned long this_call = 0;
420 unsigned long this_cache_hit = 0;
421 unsigned long this_cache_miss = 0;
422 unsigned long rpc = 0, ipc = 0;
426 this_done += s->done;
427 this_call += s->calls;
428 this_reap += s->reaps;
429 this_cache_hit += s->cachehit;
430 this_cache_miss += s->cachemiss;
431 if (this_cache_hit && this_cache_miss) {
432 unsigned long hits, total;
434 hits = this_cache_hit - cache_hit;
435 total = hits + this_cache_miss - cache_miss;
436 hit = (double) hits / (double) total;
439 if (this_call - calls) {
440 rpc = (this_done - done) / (this_call - calls);
441 ipc = (this_reap - reap) / (this_call - calls);
443 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
444 this_done - done, rpc, ipc, s->inflight,
445 *s->cq_ring.head, *s->cq_ring.tail, hit);
449 cache_hit = s->cachehit;
450 cache_miss = s->cachemiss;
453 pthread_join(s->thread, &ret);