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_cqe *cqes;
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_sqe *sqes;
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 = 1; /* use IO polling */
78 static int fixedbufs = 0; /* use fixed user buffers */
79 static int buffered = 0; /* 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_sqe *sqe = &s->sqes[index];
104 unsigned long offset;
107 lrand48_r(&s->rand, &r);
108 offset = (r % (s->max_blocks - 1)) * BS;
111 sqe->opcode = IORING_OP_READ_FIXED;
112 sqe->addr = s->iovecs[index].iov_base;
115 sqe->opcode = IORING_OP_READV;
116 sqe->addr = &s->iovecs[index];
125 static int prep_more_ios(struct submitter *s, int fd, int max_ios)
127 struct io_sq_ring *ring = &s->sq_ring;
128 unsigned index, tail, next_tail, prepped = 0;
130 next_tail = tail = *ring->tail;
134 if (next_tail == *ring->head)
137 index = tail & sq_ring_mask;
138 init_io(s, fd, index);
139 ring->array[index] = index;
142 } while (prepped < max_ios);
144 if (*ring->tail != tail) {
145 /* order tail store with writes to sqes above */
153 static int get_file_size(int fd, unsigned long *blocks)
157 if (fstat(fd, &st) < 0)
159 if (S_ISBLK(st.st_mode)) {
160 unsigned long long bytes;
162 if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
165 *blocks = bytes / BS;
167 } else if (S_ISREG(st.st_mode)) {
168 *blocks = st.st_size / BS;
175 static int reap_events(struct submitter *s)
177 struct io_cq_ring *ring = &s->cq_ring;
178 struct io_uring_cqe *cqe;
179 unsigned head, reaped = 0;
184 if (head == *ring->tail)
186 cqe = &ring->cqes[head & cq_ring_mask];
187 if (cqe->res != BS) {
188 struct io_uring_sqe *sqe = &s->sqes[cqe->index];
190 printf("io: unexpected ret=%d\n", cqe->res);
191 printf("offset=%lu, size=%lu\n",
192 (unsigned long) sqe->off,
193 (unsigned long) sqe->len);
196 if (cqe->flags & IOCQE_FLAG_CACHEHIT)
204 s->inflight -= reaped;
210 static void *submitter_fn(void *data)
212 struct submitter *s = data;
213 int fd, ret, prepped, flags;
215 printf("submitter=%d\n", gettid());
220 fd = open(s->filename, flags);
226 if (get_file_size(fd, &s->max_blocks)) {
227 printf("failed getting size of device/file\n");
230 if (s->max_blocks <= 1) {
231 printf("Zero file/device size?\n");
236 srand48_r(pthread_self(), &s->rand);
240 int to_wait, to_submit, this_reap, to_prep;
242 if (!prepped && s->inflight < DEPTH) {
243 to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
244 prepped = prep_more_ios(s, fd, to_prep);
246 s->inflight += prepped;
250 if (s->inflight + BATCH_SUBMIT < DEPTH)
253 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
255 ret = io_uring_enter(s, to_submit, to_wait,
256 IORING_ENTER_GETEVENTS);
259 this_reap = reap_events(s);
262 s->reaps += this_reap;
270 } else if (ret < to_submit) {
271 int diff = to_submit - ret;
280 } else if (ret < 0) {
281 if (errno == EAGAIN) {
289 printf("io_submit: %s\n", strerror(errno));
292 } while (!s->finish);
300 static void sig_int(int sig)
302 printf("Exiting on signal %d\n", sig);
303 submitters[0].finish = 1;
307 static void arm_sig_int(void)
309 struct sigaction act;
311 memset(&act, 0, sizeof(act));
312 act.sa_handler = sig_int;
313 act.sa_flags = SA_RESTART;
314 sigaction(SIGINT, &act, NULL);
317 static int setup_ring(struct submitter *s)
319 struct io_sq_ring *sring = &s->sq_ring;
320 struct io_cq_ring *cring = &s->cq_ring;
321 struct io_uring_params p;
325 memset(&p, 0, sizeof(p));
328 p.flags |= IORING_SETUP_IOPOLL;
330 p.flags |= IORING_SETUP_SQWQ;
331 else if (sq_thread) {
332 p.flags |= IORING_SETUP_SQTHREAD;
333 p.sq_thread_cpu = sq_thread_cpu;
337 fd = io_uring_setup(DEPTH, s->iovecs, &p);
339 fd = io_uring_setup(DEPTH, NULL, &p);
341 perror("io_uring_setup");
346 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
347 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
349 printf("sq_ring ptr = 0x%p\n", ptr);
350 sring->head = ptr + p.sq_off.head;
351 sring->tail = ptr + p.sq_off.tail;
352 sring->ring_mask = ptr + p.sq_off.ring_mask;
353 sring->ring_entries = ptr + p.sq_off.ring_entries;
354 sring->array = ptr + p.sq_off.array;
355 sq_ring_mask = *sring->ring_mask;
357 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
358 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
360 printf("sqes ptr = 0x%p\n", s->sqes);
362 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
363 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
365 printf("cq_ring ptr = 0x%p\n", ptr);
366 cring->head = ptr + p.cq_off.head;
367 cring->tail = ptr + p.cq_off.tail;
368 cring->ring_mask = ptr + p.cq_off.ring_mask;
369 cring->ring_entries = ptr + p.cq_off.ring_entries;
370 cring->cqes = ptr + p.cq_off.cqes;
371 cq_ring_mask = *cring->ring_mask;
375 int main(int argc, char *argv[])
377 struct submitter *s = &submitters[0];
378 unsigned long done, calls, reap, cache_hit, cache_miss;
384 printf("%s: filename\n", argv[0]);
388 rlim.rlim_cur = RLIM_INFINITY;
389 rlim.rlim_max = RLIM_INFINITY;
390 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
397 for (i = 0; i < DEPTH; i++) {
400 if (posix_memalign(&buf, BS, BS)) {
401 printf("failed alloc\n");
404 s->iovecs[i].iov_base = buf;
405 s->iovecs[i].iov_len = BS;
410 printf("ring setup failed: %s, %d\n", strerror(errno), err);
413 printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
414 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
415 strcpy(s->filename, argv[1]);
417 pthread_create(&s->thread, NULL, submitter_fn, s);
419 cache_hit = cache_miss = reap = calls = done = 0;
421 unsigned long this_done = 0;
422 unsigned long this_reap = 0;
423 unsigned long this_call = 0;
424 unsigned long this_cache_hit = 0;
425 unsigned long this_cache_miss = 0;
426 unsigned long rpc = 0, ipc = 0;
430 this_done += s->done;
431 this_call += s->calls;
432 this_reap += s->reaps;
433 this_cache_hit += s->cachehit;
434 this_cache_miss += s->cachemiss;
435 if (this_cache_hit && this_cache_miss) {
436 unsigned long hits, total;
438 hits = this_cache_hit - cache_hit;
439 total = hits + this_cache_miss - cache_miss;
440 hit = (double) hits / (double) total;
443 if (this_call - calls) {
444 rpc = (this_done - done) / (this_call - calls);
445 ipc = (this_reap - reap) / (this_call - calls);
447 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
448 this_done - done, rpc, ipc, s->inflight,
449 *s->cq_ring.head, *s->cq_ring.tail, hit);
453 cache_hit = s->cachehit;
454 cache_miss = s->cachemiss;
457 pthread_join(s->thread, &ret);