Commit | Line | Data |
---|---|---|
c9fb4c5b JA |
1 | #include <stdio.h> |
2 | #include <errno.h> | |
3 | #include <assert.h> | |
4 | #include <stdlib.h> | |
5 | #include <stddef.h> | |
6 | #include <signal.h> | |
7 | #include <inttypes.h> | |
8 | ||
9 | #include <sys/types.h> | |
10 | #include <sys/stat.h> | |
11 | #include <sys/ioctl.h> | |
12 | #include <sys/syscall.h> | |
13 | #include <sys/resource.h> | |
c3e2fc25 | 14 | #include <sys/mman.h> |
e31b8288 | 15 | #include <sys/uio.h> |
c9fb4c5b JA |
16 | #include <linux/fs.h> |
17 | #include <fcntl.h> | |
18 | #include <unistd.h> | |
c9fb4c5b JA |
19 | #include <string.h> |
20 | #include <pthread.h> | |
21 | #include <sched.h> | |
22 | ||
74efb029 | 23 | #include "../arch/arch.h" |
57fa61f0 | 24 | #include "../lib/types.h" |
e31b8288 | 25 | #include "../os/io_uring.h" |
ac122fea | 26 | |
e31b8288 | 27 | #define barrier() __asm__ __volatile__("": : :"memory") |
c3e2fc25 | 28 | |
e31b8288 | 29 | #define min(a, b) ((a < b) ? (a) : (b)) |
c3e2fc25 | 30 | |
e31b8288 | 31 | struct io_sq_ring { |
e2239016 JA |
32 | unsigned *head; |
33 | unsigned *tail; | |
34 | unsigned *ring_mask; | |
35 | unsigned *ring_entries; | |
36 | unsigned *array; | |
c9fb4c5b JA |
37 | }; |
38 | ||
e31b8288 | 39 | struct io_cq_ring { |
e2239016 JA |
40 | unsigned *head; |
41 | unsigned *tail; | |
42 | unsigned *ring_mask; | |
43 | unsigned *ring_entries; | |
f0403f94 | 44 | struct io_uring_cqe *cqes; |
c9fb4c5b JA |
45 | }; |
46 | ||
c9fb4c5b | 47 | #define DEPTH 32 |
c9fb4c5b JA |
48 | |
49 | #define BATCH_SUBMIT 8 | |
50 | #define BATCH_COMPLETE 8 | |
51 | ||
52 | #define BS 4096 | |
53 | ||
c3e2fc25 | 54 | static unsigned sq_ring_mask, cq_ring_mask; |
e39c34dc | 55 | |
c9fb4c5b JA |
56 | struct submitter { |
57 | pthread_t thread; | |
58 | unsigned long max_blocks; | |
f310970e | 59 | int ring_fd; |
c9fb4c5b | 60 | struct drand48_data rand; |
e31b8288 | 61 | struct io_sq_ring sq_ring; |
f0403f94 | 62 | struct io_uring_sqe *sqes; |
c3e2fc25 | 63 | struct iovec iovecs[DEPTH]; |
e31b8288 | 64 | struct io_cq_ring cq_ring; |
c9fb4c5b JA |
65 | int inflight; |
66 | unsigned long reaps; | |
67 | unsigned long done; | |
68 | unsigned long calls; | |
305c06ce | 69 | unsigned long cachehit, cachemiss; |
c9fb4c5b JA |
70 | volatile int finish; |
71 | char filename[128]; | |
72 | }; | |
73 | ||
74 | static struct submitter submitters[1]; | |
75 | static volatile int finish; | |
76 | ||
f0403f94 | 77 | static int polled = 1; /* use IO polling */ |
c3e2fc25 | 78 | static int fixedbufs = 0; /* use fixed user buffers */ |
f0403f94 | 79 | static int buffered = 0; /* use buffered IO, not O_DIRECT */ |
0527b23f JA |
80 | static int sq_thread = 0; /* use kernel submission thread */ |
81 | static int sq_thread_cpu = 0; /* pin above thread to this CPU */ | |
c9fb4c5b | 82 | |
c3e2fc25 | 83 | static int io_uring_setup(unsigned entries, struct iovec *iovecs, |
e31b8288 | 84 | struct io_uring_params *p) |
c9fb4c5b | 85 | { |
74efb029 | 86 | return syscall(__NR_sys_io_uring_setup, entries, iovecs, p); |
c9fb4c5b JA |
87 | } |
88 | ||
c3e2fc25 JA |
89 | static int io_uring_enter(struct submitter *s, unsigned int to_submit, |
90 | unsigned int min_complete, unsigned int flags) | |
c9fb4c5b | 91 | { |
f310970e JA |
92 | return syscall(__NR_sys_io_uring_enter, s->ring_fd, to_submit, |
93 | min_complete, flags); | |
c9fb4c5b JA |
94 | } |
95 | ||
96 | static int gettid(void) | |
97 | { | |
98 | return syscall(__NR_gettid); | |
99 | } | |
100 | ||
c3e2fc25 | 101 | static void init_io(struct submitter *s, int fd, unsigned index) |
c9fb4c5b | 102 | { |
f0403f94 | 103 | struct io_uring_sqe *sqe = &s->sqes[index]; |
c9fb4c5b JA |
104 | unsigned long offset; |
105 | long r; | |
106 | ||
107 | lrand48_r(&s->rand, &r); | |
108 | offset = (r % (s->max_blocks - 1)) * BS; | |
109 | ||
f0403f94 JA |
110 | if (fixedbufs) { |
111 | sqe->opcode = IORING_OP_READ_FIXED; | |
112 | sqe->addr = s->iovecs[index].iov_base; | |
113 | sqe->len = BS; | |
114 | } else { | |
115 | sqe->opcode = IORING_OP_READV; | |
116 | sqe->addr = &s->iovecs[index]; | |
117 | sqe->len = 1; | |
118 | } | |
119 | sqe->flags = 0; | |
120 | sqe->ioprio = 0; | |
121 | sqe->fd = fd; | |
122 | sqe->off = offset; | |
c9fb4c5b JA |
123 | } |
124 | ||
125 | static int prep_more_ios(struct submitter *s, int fd, int max_ios) | |
126 | { | |
e31b8288 | 127 | struct io_sq_ring *ring = &s->sq_ring; |
e2239016 | 128 | unsigned index, tail, next_tail, prepped = 0; |
c9fb4c5b | 129 | |
c3e2fc25 | 130 | next_tail = tail = *ring->tail; |
c9fb4c5b JA |
131 | do { |
132 | next_tail++; | |
c9fb4c5b | 133 | barrier(); |
c3e2fc25 | 134 | if (next_tail == *ring->head) |
c9fb4c5b JA |
135 | break; |
136 | ||
e39c34dc | 137 | index = tail & sq_ring_mask; |
c3e2fc25 JA |
138 | init_io(s, fd, index); |
139 | ring->array[index] = index; | |
c9fb4c5b JA |
140 | prepped++; |
141 | tail = next_tail; | |
142 | } while (prepped < max_ios); | |
143 | ||
c3e2fc25 | 144 | if (*ring->tail != tail) { |
f0403f94 | 145 | /* order tail store with writes to sqes above */ |
c9fb4c5b | 146 | barrier(); |
c3e2fc25 | 147 | *ring->tail = tail; |
c9fb4c5b JA |
148 | barrier(); |
149 | } | |
150 | return prepped; | |
151 | } | |
152 | ||
153 | static int get_file_size(int fd, unsigned long *blocks) | |
154 | { | |
155 | struct stat st; | |
156 | ||
157 | if (fstat(fd, &st) < 0) | |
158 | return -1; | |
159 | if (S_ISBLK(st.st_mode)) { | |
160 | unsigned long long bytes; | |
161 | ||
162 | if (ioctl(fd, BLKGETSIZE64, &bytes) != 0) | |
163 | return -1; | |
164 | ||
165 | *blocks = bytes / BS; | |
166 | return 0; | |
167 | } else if (S_ISREG(st.st_mode)) { | |
168 | *blocks = st.st_size / BS; | |
169 | return 0; | |
170 | } | |
171 | ||
172 | return -1; | |
173 | } | |
174 | ||
175 | static int reap_events(struct submitter *s) | |
176 | { | |
e31b8288 | 177 | struct io_cq_ring *ring = &s->cq_ring; |
f0403f94 | 178 | struct io_uring_cqe *cqe; |
e2239016 | 179 | unsigned head, reaped = 0; |
c9fb4c5b | 180 | |
c3e2fc25 | 181 | head = *ring->head; |
c9fb4c5b JA |
182 | do { |
183 | barrier(); | |
c3e2fc25 | 184 | if (head == *ring->tail) |
c9fb4c5b | 185 | break; |
f0403f94 JA |
186 | cqe = &ring->cqes[head & cq_ring_mask]; |
187 | if (cqe->res != BS) { | |
188 | struct io_uring_sqe *sqe = &s->sqes[cqe->index]; | |
c9fb4c5b | 189 | |
f0403f94 | 190 | printf("io: unexpected ret=%d\n", cqe->res); |
f310970e | 191 | printf("offset=%lu, size=%lu\n", |
f0403f94 JA |
192 | (unsigned long) sqe->off, |
193 | (unsigned long) sqe->len); | |
c9fb4c5b JA |
194 | return -1; |
195 | } | |
f0403f94 | 196 | if (cqe->flags & IOCQE_FLAG_CACHEHIT) |
305c06ce JA |
197 | s->cachehit++; |
198 | else | |
199 | s->cachemiss++; | |
c9fb4c5b JA |
200 | reaped++; |
201 | head++; | |
c9fb4c5b JA |
202 | } while (1); |
203 | ||
204 | s->inflight -= reaped; | |
c3e2fc25 | 205 | *ring->head = head; |
c9fb4c5b JA |
206 | barrier(); |
207 | return reaped; | |
208 | } | |
209 | ||
210 | static void *submitter_fn(void *data) | |
211 | { | |
212 | struct submitter *s = data; | |
213 | int fd, ret, prepped, flags; | |
214 | ||
215 | printf("submitter=%d\n", gettid()); | |
216 | ||
217 | flags = O_RDONLY; | |
218 | if (!buffered) | |
219 | flags |= O_DIRECT; | |
220 | fd = open(s->filename, flags); | |
221 | if (fd < 0) { | |
222 | perror("open"); | |
223 | goto done; | |
224 | } | |
225 | ||
226 | if (get_file_size(fd, &s->max_blocks)) { | |
227 | printf("failed getting size of device/file\n"); | |
228 | goto err; | |
229 | } | |
f310970e | 230 | if (s->max_blocks <= 1) { |
c9fb4c5b JA |
231 | printf("Zero file/device size?\n"); |
232 | goto err; | |
233 | } | |
c9fb4c5b JA |
234 | s->max_blocks--; |
235 | ||
236 | srand48_r(pthread_self(), &s->rand); | |
237 | ||
238 | prepped = 0; | |
239 | do { | |
f310970e | 240 | int to_wait, to_submit, this_reap, to_prep; |
c9fb4c5b | 241 | |
f310970e JA |
242 | if (!prepped && s->inflight < DEPTH) { |
243 | to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT); | |
244 | prepped = prep_more_ios(s, fd, to_prep); | |
245 | } | |
c9fb4c5b JA |
246 | s->inflight += prepped; |
247 | submit_more: | |
248 | to_submit = prepped; | |
249 | submit: | |
250 | if (s->inflight + BATCH_SUBMIT < DEPTH) | |
251 | to_wait = 0; | |
252 | else | |
253 | to_wait = min(s->inflight + to_submit, BATCH_COMPLETE); | |
254 | ||
f310970e JA |
255 | ret = io_uring_enter(s, to_submit, to_wait, |
256 | IORING_ENTER_GETEVENTS); | |
c9fb4c5b JA |
257 | s->calls++; |
258 | ||
259 | this_reap = reap_events(s); | |
260 | if (this_reap == -1) | |
261 | break; | |
262 | s->reaps += this_reap; | |
263 | ||
264 | if (ret >= 0) { | |
265 | if (!ret) { | |
266 | to_submit = 0; | |
267 | if (s->inflight) | |
268 | goto submit; | |
269 | continue; | |
270 | } else if (ret < to_submit) { | |
271 | int diff = to_submit - ret; | |
272 | ||
273 | s->done += ret; | |
274 | prepped -= diff; | |
275 | goto submit_more; | |
276 | } | |
277 | s->done += ret; | |
278 | prepped = 0; | |
279 | continue; | |
280 | } else if (ret < 0) { | |
ac122fea | 281 | if (errno == EAGAIN) { |
c9fb4c5b JA |
282 | if (s->finish) |
283 | break; | |
284 | if (this_reap) | |
285 | goto submit; | |
c9fb4c5b JA |
286 | to_submit = 0; |
287 | goto submit; | |
288 | } | |
ac122fea | 289 | printf("io_submit: %s\n", strerror(errno)); |
c9fb4c5b JA |
290 | break; |
291 | } | |
292 | } while (!s->finish); | |
293 | err: | |
294 | close(fd); | |
295 | done: | |
296 | finish = 1; | |
297 | return NULL; | |
298 | } | |
299 | ||
300 | static void sig_int(int sig) | |
301 | { | |
302 | printf("Exiting on signal %d\n", sig); | |
303 | submitters[0].finish = 1; | |
304 | finish = 1; | |
305 | } | |
306 | ||
307 | static void arm_sig_int(void) | |
308 | { | |
309 | struct sigaction act; | |
310 | ||
311 | memset(&act, 0, sizeof(act)); | |
312 | act.sa_handler = sig_int; | |
313 | act.sa_flags = SA_RESTART; | |
314 | sigaction(SIGINT, &act, NULL); | |
315 | } | |
316 | ||
c3e2fc25 JA |
317 | static int setup_ring(struct submitter *s) |
318 | { | |
e31b8288 JA |
319 | struct io_sq_ring *sring = &s->sq_ring; |
320 | struct io_cq_ring *cring = &s->cq_ring; | |
321 | struct io_uring_params p; | |
c3e2fc25 JA |
322 | void *ptr; |
323 | int fd; | |
324 | ||
325 | memset(&p, 0, sizeof(p)); | |
326 | ||
c3e2fc25 | 327 | if (polled) |
e31b8288 | 328 | p.flags |= IORING_SETUP_IOPOLL; |
c3e2fc25 | 329 | if (buffered) |
e31b8288 | 330 | p.flags |= IORING_SETUP_SQWQ; |
c3e2fc25 | 331 | else if (sq_thread) { |
e31b8288 | 332 | p.flags |= IORING_SETUP_SQTHREAD; |
c3e2fc25 JA |
333 | p.sq_thread_cpu = sq_thread_cpu; |
334 | } | |
335 | ||
336 | if (fixedbufs) | |
337 | fd = io_uring_setup(DEPTH, s->iovecs, &p); | |
338 | else | |
339 | fd = io_uring_setup(DEPTH, NULL, &p); | |
340 | if (fd < 0) { | |
341 | perror("io_uring_setup"); | |
342 | return 1; | |
343 | } | |
344 | ||
f310970e | 345 | s->ring_fd = fd; |
e2239016 | 346 | ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32), |
f310970e JA |
347 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
348 | IORING_OFF_SQ_RING); | |
c3e2fc25 JA |
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; | |
ac122fea | 354 | sring->array = ptr + p.sq_off.array; |
c3e2fc25 JA |
355 | sq_ring_mask = *sring->ring_mask; |
356 | ||
f0403f94 | 357 | s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe), |
f310970e | 358 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
f0403f94 JA |
359 | IORING_OFF_SQES); |
360 | printf("sqes ptr = 0x%p\n", s->sqes); | |
c3e2fc25 | 361 | |
f0403f94 | 362 | ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe), |
f310970e JA |
363 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
364 | IORING_OFF_CQ_RING); | |
c3e2fc25 JA |
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; | |
f0403f94 | 370 | cring->cqes = ptr + p.cq_off.cqes; |
c3e2fc25 JA |
371 | cq_ring_mask = *cring->ring_mask; |
372 | return 0; | |
373 | } | |
374 | ||
c9fb4c5b JA |
375 | int main(int argc, char *argv[]) |
376 | { | |
377 | struct submitter *s = &submitters[0]; | |
305c06ce | 378 | unsigned long done, calls, reap, cache_hit, cache_miss; |
c3e2fc25 | 379 | int err, i; |
c9fb4c5b | 380 | struct rlimit rlim; |
c3e2fc25 | 381 | void *ret; |
c9fb4c5b JA |
382 | |
383 | if (argc < 2) { | |
384 | printf("%s: filename\n", argv[0]); | |
385 | return 1; | |
386 | } | |
387 | ||
388 | rlim.rlim_cur = RLIM_INFINITY; | |
389 | rlim.rlim_max = RLIM_INFINITY; | |
390 | if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) { | |
391 | perror("setrlimit"); | |
392 | return 1; | |
393 | } | |
394 | ||
395 | arm_sig_int(); | |
396 | ||
c3e2fc25 JA |
397 | for (i = 0; i < DEPTH; i++) { |
398 | void *buf; | |
c9fb4c5b | 399 | |
c3e2fc25 | 400 | if (posix_memalign(&buf, BS, BS)) { |
c9fb4c5b JA |
401 | printf("failed alloc\n"); |
402 | return 1; | |
403 | } | |
c3e2fc25 JA |
404 | s->iovecs[i].iov_base = buf; |
405 | s->iovecs[i].iov_len = BS; | |
c9fb4c5b JA |
406 | } |
407 | ||
c3e2fc25 | 408 | err = setup_ring(s); |
c9fb4c5b | 409 | if (err) { |
c3e2fc25 | 410 | printf("ring setup failed: %s, %d\n", strerror(errno), err); |
c9fb4c5b JA |
411 | return 1; |
412 | } | |
c3e2fc25 JA |
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); | |
c9fb4c5b JA |
415 | strcpy(s->filename, argv[1]); |
416 | ||
417 | pthread_create(&s->thread, NULL, submitter_fn, s); | |
418 | ||
305c06ce | 419 | cache_hit = cache_miss = reap = calls = done = 0; |
c9fb4c5b JA |
420 | do { |
421 | unsigned long this_done = 0; | |
422 | unsigned long this_reap = 0; | |
423 | unsigned long this_call = 0; | |
305c06ce JA |
424 | unsigned long this_cache_hit = 0; |
425 | unsigned long this_cache_miss = 0; | |
c9fb4c5b | 426 | unsigned long rpc = 0, ipc = 0; |
305c06ce | 427 | double hit = 0.0; |
c9fb4c5b JA |
428 | |
429 | sleep(1); | |
430 | this_done += s->done; | |
431 | this_call += s->calls; | |
432 | this_reap += s->reaps; | |
305c06ce JA |
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; | |
437 | ||
438 | hits = this_cache_hit - cache_hit; | |
439 | total = hits + this_cache_miss - cache_miss; | |
440 | hit = (double) hits / (double) total; | |
441 | hit *= 100.0; | |
442 | } | |
c9fb4c5b JA |
443 | if (this_call - calls) { |
444 | rpc = (this_done - done) / (this_call - calls); | |
445 | ipc = (this_reap - reap) / (this_call - calls); | |
446 | } | |
10c4d131 | 447 | printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n", |
c9fb4c5b | 448 | this_done - done, rpc, ipc, s->inflight, |
c3e2fc25 | 449 | *s->cq_ring.head, *s->cq_ring.tail, hit); |
c9fb4c5b JA |
450 | done = this_done; |
451 | calls = this_call; | |
452 | reap = this_reap; | |
305c06ce JA |
453 | cache_hit = s->cachehit; |
454 | cache_miss = s->cachemiss; | |
c9fb4c5b JA |
455 | } while (!finish); |
456 | ||
457 | pthread_join(s->thread, &ret); | |
e31b8288 | 458 | close(s->ring_fd); |
c9fb4c5b JA |
459 | return 0; |
460 | } |