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