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