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 | ||
25 | #define IOCB_FLAG_HIPRI (1 << 2) | |
26 | ||
478a96a8 JA |
27 | #define IOCTX_FLAG_IOPOLL (1 << 0) |
28 | #define IOCTX_FLAG_SCQRING (1 << 1) /* Use SQ/CQ rings */ | |
c9fb4c5b | 29 | #define IOCTX_FLAG_FIXEDBUFS (1 << 2) |
478a96a8 JA |
30 | #define IOCTX_FLAG_SQTHREAD (1 << 3) /* Use SQ thread */ |
31 | #define IOCTX_FLAG_SQWQ (1 << 4) /* Use SQ wq */ | |
c9fb4c5b JA |
32 | |
33 | #define barrier() __asm__ __volatile__("": : :"memory") | |
34 | ||
35 | #define min(a, b) ((a < b) ? (a) : (b)) | |
36 | ||
37 | typedef uint32_t u32; | |
478a96a8 | 38 | typedef uint16_t u16; |
c9fb4c5b JA |
39 | |
40 | struct aio_iocb_ring { | |
41 | union { | |
42 | struct { | |
43 | u32 head, tail; | |
44 | u32 nr_events; | |
478a96a8 | 45 | u16 sq_thread_cpu; |
c9fb4c5b JA |
46 | }; |
47 | struct iocb pad_iocb; | |
48 | }; | |
49 | struct iocb iocbs[0]; | |
50 | }; | |
51 | ||
52 | struct aio_io_event_ring { | |
53 | union { | |
54 | struct { | |
55 | u32 head, tail; | |
56 | u32 nr_events; | |
57 | }; | |
58 | struct io_event pad_event; | |
59 | }; | |
60 | struct io_event events[0]; | |
61 | }; | |
62 | ||
63 | #define IORING_FLAG_SUBMIT (1 << 0) | |
64 | #define IORING_FLAG_GETEVENTS (1 << 1) | |
65 | ||
c9fb4c5b JA |
66 | #define DEPTH 32 |
67 | #define RING_SIZE (DEPTH + 1) | |
68 | ||
69 | #define BATCH_SUBMIT 8 | |
70 | #define BATCH_COMPLETE 8 | |
71 | ||
72 | #define BS 4096 | |
73 | ||
74 | struct submitter { | |
75 | pthread_t thread; | |
76 | unsigned long max_blocks; | |
77 | io_context_t ioc; | |
78 | struct drand48_data rand; | |
79 | struct aio_iocb_ring *sq_ring; | |
80 | struct aio_io_event_ring *cq_ring; | |
81 | int inflight; | |
82 | unsigned long reaps; | |
83 | unsigned long done; | |
84 | unsigned long calls; | |
85 | volatile int finish; | |
86 | char filename[128]; | |
87 | }; | |
88 | ||
89 | static struct submitter submitters[1]; | |
90 | static volatile int finish; | |
91 | ||
92 | static int polled = 1; /* use IO polling */ | |
93 | static int fixedbufs = 1; /* use fixed user buffers */ | |
94 | static int buffered = 0; /* use buffered IO, not O_DIRECT */ | |
0527b23f JA |
95 | static int sq_thread = 0; /* use kernel submission thread */ |
96 | static int sq_thread_cpu = 0; /* pin above thread to this CPU */ | |
c9fb4c5b JA |
97 | |
98 | static int io_setup2(unsigned int nr_events, unsigned int flags, | |
99 | struct iocb *iocbs, struct aio_iocb_ring *sq_ring, | |
100 | struct aio_io_event_ring *cq_ring, io_context_t *ctx_idp) | |
101 | { | |
102 | return syscall(335, nr_events, flags, iocbs, sq_ring, cq_ring, ctx_idp); | |
103 | } | |
104 | ||
105 | static int io_ring_enter(io_context_t ctx, unsigned int to_submit, | |
106 | unsigned int min_complete, unsigned int flags) | |
107 | { | |
108 | return syscall(336, ctx, to_submit, min_complete, flags); | |
109 | } | |
110 | ||
111 | static int gettid(void) | |
112 | { | |
113 | return syscall(__NR_gettid); | |
114 | } | |
115 | ||
116 | static void init_io(struct submitter *s, int fd, struct iocb *iocb) | |
117 | { | |
118 | unsigned long offset; | |
119 | long r; | |
120 | ||
121 | lrand48_r(&s->rand, &r); | |
122 | offset = (r % (s->max_blocks - 1)) * BS; | |
123 | ||
124 | iocb->aio_fildes = fd; | |
125 | iocb->aio_lio_opcode = IO_CMD_PREAD; | |
126 | iocb->u.c.offset = offset; | |
127 | if (polled) | |
128 | iocb->u.c.flags = IOCB_FLAG_HIPRI; | |
129 | if (!fixedbufs) | |
130 | iocb->u.c.nbytes = BS; | |
131 | } | |
132 | ||
133 | static int prep_more_ios(struct submitter *s, int fd, int max_ios) | |
134 | { | |
135 | struct aio_iocb_ring *ring = s->sq_ring; | |
136 | struct iocb *iocb; | |
137 | u32 tail, next_tail, prepped = 0; | |
138 | ||
139 | next_tail = tail = ring->tail; | |
140 | do { | |
141 | next_tail++; | |
142 | if (next_tail == ring->nr_events) | |
143 | next_tail = 0; | |
144 | ||
145 | barrier(); | |
146 | if (next_tail == ring->head) | |
147 | break; | |
148 | ||
149 | iocb = &s->sq_ring->iocbs[tail]; | |
150 | init_io(s, fd, iocb); | |
151 | prepped++; | |
152 | tail = next_tail; | |
153 | } while (prepped < max_ios); | |
154 | ||
155 | if (ring->tail != tail) { | |
156 | /* order tail store with writes to iocbs above */ | |
157 | barrier(); | |
158 | ring->tail = tail; | |
159 | barrier(); | |
160 | } | |
161 | return prepped; | |
162 | } | |
163 | ||
164 | static int get_file_size(int fd, unsigned long *blocks) | |
165 | { | |
166 | struct stat st; | |
167 | ||
168 | if (fstat(fd, &st) < 0) | |
169 | return -1; | |
170 | if (S_ISBLK(st.st_mode)) { | |
171 | unsigned long long bytes; | |
172 | ||
173 | if (ioctl(fd, BLKGETSIZE64, &bytes) != 0) | |
174 | return -1; | |
175 | ||
176 | *blocks = bytes / BS; | |
177 | return 0; | |
178 | } else if (S_ISREG(st.st_mode)) { | |
179 | *blocks = st.st_size / BS; | |
180 | return 0; | |
181 | } | |
182 | ||
183 | return -1; | |
184 | } | |
185 | ||
186 | static int reap_events(struct submitter *s) | |
187 | { | |
188 | struct aio_io_event_ring *ring = s->cq_ring; | |
189 | struct io_event *ev; | |
190 | u32 head, reaped = 0; | |
191 | ||
192 | head = ring->head; | |
193 | do { | |
194 | barrier(); | |
195 | if (head == ring->tail) | |
196 | break; | |
197 | ev = &ring->events[head]; | |
198 | if (ev->res != BS) { | |
199 | int index = (int) (uintptr_t) ev->obj; | |
200 | struct iocb *iocb = &s->sq_ring->iocbs[index]; | |
201 | ||
202 | printf("io: unexpected ret=%ld\n", ev->res); | |
203 | printf("offset=%lu, size=%lu\n", (unsigned long) iocb->u.c.offset, (unsigned long) iocb->u.c.nbytes); | |
204 | return -1; | |
205 | } | |
206 | reaped++; | |
207 | head++; | |
208 | if (head == ring->nr_events) | |
209 | head = 0; | |
210 | } while (1); | |
211 | ||
212 | s->inflight -= reaped; | |
213 | ring->head = head; | |
214 | barrier(); | |
215 | return reaped; | |
216 | } | |
217 | ||
218 | static void *submitter_fn(void *data) | |
219 | { | |
220 | struct submitter *s = data; | |
221 | int fd, ret, prepped, flags; | |
222 | ||
223 | printf("submitter=%d\n", gettid()); | |
224 | ||
225 | flags = O_RDONLY; | |
226 | if (!buffered) | |
227 | flags |= O_DIRECT; | |
228 | fd = open(s->filename, flags); | |
229 | if (fd < 0) { | |
230 | perror("open"); | |
231 | goto done; | |
232 | } | |
233 | ||
234 | if (get_file_size(fd, &s->max_blocks)) { | |
235 | printf("failed getting size of device/file\n"); | |
236 | goto err; | |
237 | } | |
238 | if (!s->max_blocks) { | |
239 | printf("Zero file/device size?\n"); | |
240 | goto err; | |
241 | } | |
242 | ||
243 | s->max_blocks--; | |
244 | ||
245 | srand48_r(pthread_self(), &s->rand); | |
246 | ||
247 | prepped = 0; | |
248 | do { | |
249 | int to_wait, flags, to_submit, this_reap; | |
250 | ||
251 | if (!prepped && s->inflight < DEPTH) | |
252 | prepped = prep_more_ios(s, fd, min(DEPTH - s->inflight, BATCH_SUBMIT)); | |
253 | s->inflight += prepped; | |
254 | submit_more: | |
255 | to_submit = prepped; | |
256 | submit: | |
257 | if (s->inflight + BATCH_SUBMIT < DEPTH) | |
258 | to_wait = 0; | |
259 | else | |
260 | to_wait = min(s->inflight + to_submit, BATCH_COMPLETE); | |
261 | ||
262 | flags = IORING_FLAG_GETEVENTS; | |
263 | if (to_submit) | |
264 | flags |= IORING_FLAG_SUBMIT; | |
265 | ||
266 | ret = io_ring_enter(s->ioc, to_submit, to_wait, flags); | |
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]; | |
333 | unsigned long done, calls, reap; | |
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 | ||
354 | size = sizeof(struct aio_iocb_ring) + RING_SIZE * sizeof(struct iocb); | |
355 | if (posix_memalign(&p, 4096, size)) | |
356 | return 1; | |
357 | s->sq_ring = p; | |
478a96a8 | 358 | s->sq_ring->nr_events = RING_SIZE; |
c9fb4c5b JA |
359 | memset(p, 0, size); |
360 | ||
478a96a8 JA |
361 | /* CQ ring must be twice as big */ |
362 | size = sizeof(struct aio_io_event_ring) + | |
363 | 2 * RING_SIZE * sizeof(struct io_event); | |
c9fb4c5b JA |
364 | if (posix_memalign(&p, 4096, size)) |
365 | return 1; | |
366 | s->cq_ring = p; | |
478a96a8 | 367 | s->cq_ring->nr_events = 2 * RING_SIZE; |
c9fb4c5b JA |
368 | memset(p, 0, size); |
369 | ||
370 | for (j = 0; j < RING_SIZE; j++) { | |
371 | struct iocb *iocb = &s->sq_ring->iocbs[j]; | |
372 | ||
373 | if (posix_memalign(&iocb->u.c.buf, BS, BS)) { | |
374 | printf("failed alloc\n"); | |
375 | return 1; | |
376 | } | |
377 | iocb->u.c.nbytes = BS; | |
378 | } | |
379 | ||
380 | flags = IOCTX_FLAG_SCQRING; | |
381 | if (polled) | |
382 | flags |= IOCTX_FLAG_IOPOLL; | |
383 | if (fixedbufs) | |
384 | flags |= IOCTX_FLAG_FIXEDBUFS; | |
385 | if (buffered) | |
386 | flags |= IOCTX_FLAG_SQWQ; | |
387 | else if (sq_thread) { | |
388 | flags |= IOCTX_FLAG_SQTHREAD; | |
389 | s->sq_ring->sq_thread_cpu = sq_thread_cpu; | |
390 | } | |
391 | ||
392 | err = io_setup2(RING_SIZE, flags, s->sq_ring->iocbs, s->sq_ring, s->cq_ring, &s->ioc); | |
393 | if (err) { | |
394 | printf("ctx_init failed: %s, %d\n", strerror(errno), err); | |
395 | return 1; | |
396 | } | |
397 | printf("polled=%d, fixedbufs=%d, buffered=%d\n", polled, fixedbufs, buffered); | |
398 | printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, s->sq_ring->nr_events, s->cq_ring->nr_events); | |
399 | strcpy(s->filename, argv[1]); | |
400 | ||
401 | pthread_create(&s->thread, NULL, submitter_fn, s); | |
402 | ||
403 | reap = calls = done = 0; | |
404 | do { | |
405 | unsigned long this_done = 0; | |
406 | unsigned long this_reap = 0; | |
407 | unsigned long this_call = 0; | |
408 | unsigned long rpc = 0, ipc = 0; | |
409 | ||
410 | sleep(1); | |
411 | this_done += s->done; | |
412 | this_call += s->calls; | |
413 | this_reap += s->reaps; | |
414 | if (this_call - calls) { | |
415 | rpc = (this_done - done) / (this_call - calls); | |
416 | ipc = (this_reap - reap) / (this_call - calls); | |
417 | } | |
418 | printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%d tail=%d), %lu, %lu\n", | |
419 | this_done - done, rpc, ipc, s->inflight, | |
420 | s->cq_ring->head, s->cq_ring->tail, s->reaps, s->done); | |
421 | done = this_done; | |
422 | calls = this_call; | |
423 | reap = this_reap; | |
424 | } while (!finish); | |
425 | ||
426 | pthread_join(s->thread, &ret); | |
427 | return 0; | |
428 | } |