… | |
… | |
100 | * not only is this totally undocumented, not even the source code |
100 | * not only is this totally undocumented, not even the source code |
101 | * can tell you what the future semantics of compat_features and |
101 | * can tell you what the future semantics of compat_features and |
102 | * incompat_features are, or what header_length actually is for. |
102 | * incompat_features are, or what header_length actually is for. |
103 | */ |
103 | */ |
104 | #define AIO_RING_MAGIC 0xa10a10a1 |
104 | #define AIO_RING_MAGIC 0xa10a10a1 |
105 | #define AIO_RING_INCOMPAT_FEATURES 0 |
105 | #define EV_AIO_RING_INCOMPAT_FEATURES 0 |
106 | struct aio_ring |
106 | struct aio_ring |
107 | { |
107 | { |
108 | unsigned id; /* kernel internal index number */ |
108 | unsigned id; /* kernel internal index number */ |
109 | unsigned nr; /* number of io_events */ |
109 | unsigned nr; /* number of io_events */ |
110 | unsigned head; /* Written to by userland or by kernel. */ |
110 | unsigned head; /* Written to by userland or by kernel. */ |
… | |
… | |
116 | unsigned header_length; /* size of aio_ring */ |
116 | unsigned header_length; /* size of aio_ring */ |
117 | |
117 | |
118 | struct io_event io_events[0]; |
118 | struct io_event io_events[0]; |
119 | }; |
119 | }; |
120 | |
120 | |
|
|
121 | /* |
|
|
122 | * define some syscall wrappers for common architectures |
|
|
123 | * this is mostly for nice looks during debugging, not performance. |
|
|
124 | * our syscalls return < 0, not == -1, on error. which is good |
|
|
125 | * enough for linux aio. |
|
|
126 | * TODO: arm is also common nowadays, maybe even mips and x86 |
|
|
127 | * TODO: after implementing this, it suddenly looks like overkill, but its hard to remove... |
|
|
128 | */ |
|
|
129 | #if __GNUC__ && __linux && ECB_AMD64 && !defined __OPTIMIZE_SIZE__ |
|
|
130 | /* the costly errno access probably kills this for size optimisation */ |
|
|
131 | |
|
|
132 | #define ev_syscall(nr,narg,arg1,arg2,arg3,arg4,arg5) \ |
|
|
133 | ({ \ |
|
|
134 | long res; \ |
|
|
135 | register unsigned long r5 __asm__ ("r8" ); \ |
|
|
136 | register unsigned long r4 __asm__ ("r10"); \ |
|
|
137 | register unsigned long r3 __asm__ ("rdx"); \ |
|
|
138 | register unsigned long r2 __asm__ ("rsi"); \ |
|
|
139 | register unsigned long r1 __asm__ ("rdi"); \ |
|
|
140 | if (narg >= 5) r5 = (unsigned long)(arg5); \ |
|
|
141 | if (narg >= 4) r4 = (unsigned long)(arg4); \ |
|
|
142 | if (narg >= 3) r3 = (unsigned long)(arg3); \ |
|
|
143 | if (narg >= 2) r2 = (unsigned long)(arg2); \ |
|
|
144 | if (narg >= 1) r1 = (unsigned long)(arg1); \ |
|
|
145 | __asm__ __volatile__ ( \ |
|
|
146 | "syscall\n\t" \ |
|
|
147 | : "=a" (res) \ |
|
|
148 | : "0" (nr), "r" (r1), "r" (r2), "r" (r3), "r" (r4), "r" (r5) \ |
|
|
149 | : "cc", "r11", "cx", "memory"); \ |
|
|
150 | errno = -res; \ |
|
|
151 | res; \ |
|
|
152 | }) |
|
|
153 | |
|
|
154 | #endif |
|
|
155 | |
|
|
156 | #ifdef ev_syscall |
|
|
157 | #define ev_syscall0(nr) ev_syscall (nr, 0, 0, 0, 0, 0, 0 |
|
|
158 | #define ev_syscall1(nr,arg1) ev_syscall (nr, 1, arg1, 0, 0, 0, 0) |
|
|
159 | #define ev_syscall2(nr,arg1,arg2) ev_syscall (nr, 2, arg1, arg2, 0, 0, 0) |
|
|
160 | #define ev_syscall3(nr,arg1,arg2,arg3) ev_syscall (nr, 3, arg1, arg2, arg3, 0, 0) |
|
|
161 | #define ev_syscall4(nr,arg1,arg2,arg3,arg4) ev_syscall (nr, 3, arg1, arg2, arg3, arg4, 0) |
|
|
162 | #define ev_syscall5(nr,arg1,arg2,arg3,arg4,arg5) ev_syscall (nr, 5, arg1, arg2, arg3, arg4, arg5) |
|
|
163 | #else |
|
|
164 | #define ev_syscall0(nr) syscall (nr) |
|
|
165 | #define ev_syscall1(nr,arg1) syscall (nr, arg1) |
|
|
166 | #define ev_syscall2(nr,arg1,arg2) syscall (nr, arg1, arg2) |
|
|
167 | #define ev_syscall3(nr,arg1,arg2,arg3) syscall (nr, arg1, arg2, arg3) |
|
|
168 | #define ev_syscall4(nr,arg1,arg2,arg3,arg4) syscall (nr, arg1, arg2, arg3, arg4) |
|
|
169 | #define ev_syscall5(nr,arg1,arg2,arg3,arg4,arg5) syscall (nr, arg1, arg2, arg3, arg4, arg5) |
|
|
170 | #endif |
|
|
171 | |
121 | inline_size |
172 | inline_size |
122 | int |
173 | int |
123 | evsys_io_setup (unsigned nr_events, aio_context_t *ctx_idp) |
174 | evsys_io_setup (unsigned nr_events, aio_context_t *ctx_idp) |
124 | { |
175 | { |
125 | return syscall (SYS_io_setup, nr_events, ctx_idp); |
176 | return ev_syscall2 (SYS_io_setup, nr_events, ctx_idp); |
126 | } |
177 | } |
127 | |
178 | |
128 | inline_size |
179 | inline_size |
129 | int |
180 | int |
130 | evsys_io_destroy (aio_context_t ctx_id) |
181 | evsys_io_destroy (aio_context_t ctx_id) |
131 | { |
182 | { |
132 | return syscall (SYS_io_destroy, ctx_id); |
183 | return ev_syscall1 (SYS_io_destroy, ctx_id); |
133 | } |
184 | } |
134 | |
185 | |
135 | inline_size |
186 | inline_size |
136 | int |
187 | int |
137 | evsys_io_submit (aio_context_t ctx_id, long nr, struct iocb *cbp[]) |
188 | evsys_io_submit (aio_context_t ctx_id, long nr, struct iocb *cbp[]) |
138 | { |
189 | { |
139 | return syscall (SYS_io_submit, ctx_id, nr, cbp); |
190 | return ev_syscall3 (SYS_io_submit, ctx_id, nr, cbp); |
140 | } |
191 | } |
141 | |
192 | |
142 | inline_size |
193 | inline_size |
143 | int |
194 | int |
144 | evsys_io_cancel (aio_context_t ctx_id, struct iocb *cbp, struct io_event *result) |
195 | evsys_io_cancel (aio_context_t ctx_id, struct iocb *cbp, struct io_event *result) |
145 | { |
196 | { |
146 | return syscall (SYS_io_cancel, ctx_id, cbp, result); |
197 | return ev_syscall3 (SYS_io_cancel, ctx_id, cbp, result); |
147 | } |
198 | } |
148 | |
199 | |
149 | inline_size |
200 | inline_size |
150 | int |
201 | int |
151 | evsys_io_getevents (aio_context_t ctx_id, long min_nr, long nr, struct io_event *events, struct timespec *timeout) |
202 | evsys_io_getevents (aio_context_t ctx_id, long min_nr, long nr, struct io_event *events, struct timespec *timeout) |
152 | { |
203 | { |
153 | return syscall (SYS_io_getevents, ctx_id, min_nr, nr, events, timeout); |
204 | return ev_syscall5 (SYS_io_getevents, ctx_id, min_nr, nr, events, timeout); |
154 | } |
205 | } |
155 | |
206 | |
156 | /*****************************************************************************/ |
207 | /*****************************************************************************/ |
157 | /* actual backed implementation */ |
208 | /* actual backed implementation */ |
158 | |
209 | |
… | |
… | |
238 | ANIOCBP iocb = linuxaio_iocbps [fd]; |
289 | ANIOCBP iocb = linuxaio_iocbps [fd]; |
239 | |
290 | |
240 | if (iocb->io.aio_reqprio < 0) |
291 | if (iocb->io.aio_reqprio < 0) |
241 | { |
292 | { |
242 | /* we handed this fd over to epoll, so undo this first */ |
293 | /* we handed this fd over to epoll, so undo this first */ |
243 | /* we do it manually because the optimisations on epoll_modfy won't do us any good */ |
294 | /* we do it manually because the optimisations on epoll_modify won't do us any good */ |
244 | epoll_ctl (backend_fd, EPOLL_CTL_DEL, fd, 0); |
295 | epoll_ctl (backend_fd, EPOLL_CTL_DEL, fd, 0); |
|
|
296 | anfds [fd].emask = 0; |
245 | iocb->io.aio_reqprio = 0; |
297 | iocb->io.aio_reqprio = 0; |
246 | } |
298 | } |
247 | |
299 | |
248 | if (iocb->io.aio_buf) |
300 | if (iocb->io.aio_buf) |
249 | /* io_cancel always returns some error on relevant kernels, but works */ |
301 | { |
|
|
302 | for (;;) |
|
|
303 | { |
|
|
304 | /* on all relevant kernels, io_cancel fails with EINPROGRESS on "success" */ |
250 | evsys_io_cancel (linuxaio_ctx, &iocb->io, (struct io_event *)0); |
305 | if (expect_false (evsys_io_cancel (linuxaio_ctx, &iocb->io, (struct io_event *)0) == 0)) |
|
|
306 | break; |
|
|
307 | |
|
|
308 | if (expect_true (errno == EINPROGRESS)) |
|
|
309 | break; |
|
|
310 | |
|
|
311 | /* the EINPROGRESS test is for nicer error message. clumsy. */ |
|
|
312 | assert (("libev: linuxaio unexpected io_cancel failed", errno != EINPROGRESS && errno != EINTR)); |
|
|
313 | } |
|
|
314 | } |
251 | |
315 | |
252 | if (nev) |
316 | if (nev) |
253 | { |
317 | { |
254 | iocb->io.aio_buf = |
318 | iocb->io.aio_buf = |
255 | (nev & EV_READ ? POLLIN : 0) |
319 | (nev & EV_READ ? POLLIN : 0) |
… | |
… | |
267 | linuxaio_epoll_cb (EV_P_ struct ev_io *w, int revents) |
331 | linuxaio_epoll_cb (EV_P_ struct ev_io *w, int revents) |
268 | { |
332 | { |
269 | epoll_poll (EV_A_ 0); |
333 | epoll_poll (EV_A_ 0); |
270 | } |
334 | } |
271 | |
335 | |
272 | static void |
336 | inline_speed |
|
|
337 | void |
273 | linuxaio_fd_rearm (EV_P_ int fd) |
338 | linuxaio_fd_rearm (EV_P_ int fd) |
274 | { |
339 | { |
275 | anfds [fd].events = 0; |
340 | anfds [fd].events = 0; |
276 | linuxaio_iocbps [fd]->io.aio_buf = 0; |
341 | linuxaio_iocbps [fd]->io.aio_buf = 0; |
277 | fd_change (EV_A_ fd, EV_ANFD_REIFY); |
342 | fd_change (EV_A_ fd, EV_ANFD_REIFY); |
… | |
… | |
293 | fd, |
358 | fd, |
294 | (res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
359 | (res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
295 | | (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
360 | | (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
296 | ); |
361 | ); |
297 | |
362 | |
298 | /* linux aio is oneshot: rearm fd. TODO: this does more work than needed */ |
363 | /* linux aio is oneshot: rearm fd. TODO: this does more work than strictly needed */ |
299 | linuxaio_fd_rearm (EV_A_ fd); |
364 | linuxaio_fd_rearm (EV_A_ fd); |
300 | |
365 | |
301 | --nr; |
366 | --nr; |
302 | ++ev; |
367 | ++ev; |
303 | } |
368 | } |
… | |
… | |
316 | unsigned tail = *(volatile unsigned *)&ring->tail; |
381 | unsigned tail = *(volatile unsigned *)&ring->tail; |
317 | |
382 | |
318 | if (head == tail) |
383 | if (head == tail) |
319 | return 0; |
384 | return 0; |
320 | |
385 | |
321 | /* bail out if the ring buffer doesn't match the expected layout */ |
|
|
322 | if (expect_false (ring->magic != AIO_RING_MAGIC) |
|
|
323 | || ring->incompat_features != AIO_RING_INCOMPAT_FEATURES |
|
|
324 | || ring->header_length != sizeof (struct aio_ring)) /* TODO: or use it to find io_event[0]? */ |
|
|
325 | return 0; |
|
|
326 | |
|
|
327 | /* make sure the events up to tail are visible */ |
386 | /* make sure the events up to tail are visible */ |
328 | ECB_MEMORY_FENCE_ACQUIRE; |
387 | ECB_MEMORY_FENCE_ACQUIRE; |
329 | |
388 | |
330 | /* parse all available events, but only once, to avoid starvation */ |
389 | /* parse all available events, but only once, to avoid starvation */ |
331 | if (tail > head) /* normal case around */ |
390 | if (tail > head) /* normal case around */ |
… | |
… | |
341 | *(volatile unsigned *)&ring->head = tail; |
400 | *(volatile unsigned *)&ring->head = tail; |
342 | |
401 | |
343 | return 1; |
402 | return 1; |
344 | } |
403 | } |
345 | |
404 | |
|
|
405 | inline_size |
|
|
406 | int |
|
|
407 | linuxaio_ringbuf_valid (EV_P) |
|
|
408 | { |
|
|
409 | struct aio_ring *ring = (struct aio_ring *)linuxaio_ctx; |
|
|
410 | |
|
|
411 | return expect_true (ring->magic == AIO_RING_MAGIC) |
|
|
412 | && ring->incompat_features == EV_AIO_RING_INCOMPAT_FEATURES |
|
|
413 | && ring->header_length == sizeof (struct aio_ring); /* TODO: or use it to find io_event[0]? */ |
|
|
414 | } |
|
|
415 | |
346 | /* read at least one event from kernel, or timeout */ |
416 | /* read at least one event from kernel, or timeout */ |
347 | inline_size |
417 | inline_size |
348 | void |
418 | void |
349 | linuxaio_get_events (EV_P_ ev_tstamp timeout) |
419 | linuxaio_get_events (EV_P_ ev_tstamp timeout) |
350 | { |
420 | { |
351 | struct timespec ts; |
421 | struct timespec ts; |
352 | struct io_event ioev[1]; |
422 | struct io_event ioev[8]; /* 256 octet stack space */ |
353 | int res; |
423 | int want = 1; /* how many events to request */ |
|
|
424 | int ringbuf_valid = linuxaio_ringbuf_valid (EV_A); |
354 | |
425 | |
|
|
426 | if (expect_true (ringbuf_valid)) |
|
|
427 | { |
|
|
428 | /* if the ring buffer has any events, we don't wait or call the kernel at all */ |
355 | if (linuxaio_get_events_from_ring (EV_A)) |
429 | if (linuxaio_get_events_from_ring (EV_A)) |
356 | return; |
430 | return; |
357 | |
431 | |
358 | /* no events, so wait for at least one, then poll ring buffer again */ |
432 | /* if the ring buffer is empty, and we don't have a timeout, then don't call the kernel */ |
359 | /* this degrades to one event per loop iteration */ |
433 | if (!timeout) |
360 | /* if the ring buffer changes layout, but so be it */ |
434 | return; |
|
|
435 | } |
|
|
436 | else |
|
|
437 | /* no ringbuffer, request slightly larger batch */ |
|
|
438 | want = sizeof (ioev) / sizeof (ioev [0]); |
361 | |
439 | |
|
|
440 | /* no events, so wait for some |
|
|
441 | * for fairness reasons, we do this in a loop, to fetch all events |
|
|
442 | */ |
|
|
443 | for (;;) |
|
|
444 | { |
|
|
445 | int res; |
|
|
446 | |
362 | EV_RELEASE_CB; |
447 | EV_RELEASE_CB; |
363 | |
448 | |
364 | ts.tv_sec = (long)timeout; |
449 | ts.tv_sec = (long)timeout; |
365 | ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9); |
450 | ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9); |
366 | |
451 | |
367 | res = evsys_io_getevents (linuxaio_ctx, 1, sizeof (ioev) / sizeof (ioev [0]), ioev, &ts); |
452 | res = evsys_io_getevents (linuxaio_ctx, 1, want, ioev, &ts); |
368 | |
453 | |
369 | EV_ACQUIRE_CB; |
454 | EV_ACQUIRE_CB; |
370 | |
455 | |
371 | if (res < 0) |
456 | if (res < 0) |
372 | if (errno == EINTR) |
457 | if (errno == EINTR) |
373 | /* ignored */; |
458 | /* ignored, retry */; |
374 | else |
459 | else |
375 | ev_syserr ("(libev) linuxaio io_getevents"); |
460 | ev_syserr ("(libev) linuxaio io_getevents"); |
376 | else if (res) |
461 | else if (res) |
377 | { |
462 | { |
378 | /* at least one event received, handle it and any remaining ones in the ring buffer */ |
463 | /* at least one event available, handle them */ |
379 | linuxaio_parse_events (EV_A_ ioev, res); |
464 | linuxaio_parse_events (EV_A_ ioev, res); |
|
|
465 | |
|
|
466 | if (expect_true (ringbuf_valid)) |
|
|
467 | { |
|
|
468 | /* if we have a ring buffer, handle any remaining events in it */ |
380 | linuxaio_get_events_from_ring (EV_A); |
469 | linuxaio_get_events_from_ring (EV_A); |
381 | } |
|
|
382 | } |
|
|
383 | |
470 | |
384 | static int |
471 | /* at this point, we should have handled all outstanding events */ |
|
|
472 | break; |
|
|
473 | } |
|
|
474 | else if (res < want) |
|
|
475 | /* otherwise, if there were fewere events than we wanted, we assume there are no more */ |
|
|
476 | break; |
|
|
477 | } |
|
|
478 | else |
|
|
479 | break; /* no events from the kernel, we are done */ |
|
|
480 | |
|
|
481 | timeout = 0; /* only wait in the first iteration */ |
|
|
482 | } |
|
|
483 | } |
|
|
484 | |
|
|
485 | inline_size |
|
|
486 | int |
385 | linuxaio_io_setup (EV_P) |
487 | linuxaio_io_setup (EV_P) |
386 | { |
488 | { |
387 | linuxaio_ctx = 0; |
489 | linuxaio_ctx = 0; |
388 | return evsys_io_setup (linuxaio_nr_events (EV_A), &linuxaio_ctx); |
490 | return evsys_io_setup (linuxaio_nr_events (EV_A), &linuxaio_ctx); |
389 | } |
491 | } |
… | |
… | |
455 | /* it's easiest to handle this mess in another iteration */ |
557 | /* it's easiest to handle this mess in another iteration */ |
456 | return; |
558 | return; |
457 | } |
559 | } |
458 | else if (errno == EBADF) |
560 | else if (errno == EBADF) |
459 | { |
561 | { |
|
|
562 | assert (("libev: event loop rejected bad fd", errno != EBADF)); |
460 | fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes); |
563 | fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes); |
461 | |
564 | |
462 | res = 1; /* skip this iocb */ |
565 | res = 1; /* skip this iocb */ |
463 | } |
566 | } |
|
|
567 | else if (errno == EINTR) /* not seen in reality, not documented */ |
|
|
568 | res = 0; /* silently ignore and retry */ |
464 | else |
569 | else |
465 | ev_syserr ("(libev) linuxaio io_submit"); |
570 | ev_syserr ("(libev) linuxaio io_submit"); |
466 | |
571 | |
467 | submitted += res; |
572 | submitted += res; |
468 | } |
573 | } |
… | |
… | |
517 | void |
622 | void |
518 | linuxaio_destroy (EV_P) |
623 | linuxaio_destroy (EV_P) |
519 | { |
624 | { |
520 | epoll_destroy (EV_A); |
625 | epoll_destroy (EV_A); |
521 | linuxaio_free_iocbp (EV_A); |
626 | linuxaio_free_iocbp (EV_A); |
522 | evsys_io_destroy (linuxaio_ctx); |
627 | evsys_io_destroy (linuxaio_ctx); /* fails in child, aio context is destroyed */ |
523 | } |
628 | } |
524 | |
629 | |
525 | inline_size |
630 | inline_size |
526 | void |
631 | void |
527 | linuxaio_fork (EV_P) |
632 | linuxaio_fork (EV_P) |
… | |
… | |
533 | linuxaio_iteration = 0; /* we start over in the child */ |
638 | linuxaio_iteration = 0; /* we start over in the child */ |
534 | |
639 | |
535 | while (linuxaio_io_setup (EV_A) < 0) |
640 | while (linuxaio_io_setup (EV_A) < 0) |
536 | ev_syserr ("(libev) linuxaio io_setup"); |
641 | ev_syserr ("(libev) linuxaio io_setup"); |
537 | |
642 | |
|
|
643 | /* forking epoll should also effectively unregister all fds from the backend */ |
538 | epoll_fork (EV_A); |
644 | epoll_fork (EV_A); |
539 | |
645 | |
540 | ev_io_stop (EV_A_ &linuxaio_epoll_w); |
646 | ev_io_stop (EV_A_ &linuxaio_epoll_w); |
541 | ev_io_set (EV_A_ &linuxaio_epoll_w, backend_fd, EV_READ); |
647 | ev_io_set (EV_A_ &linuxaio_epoll_w, backend_fd, EV_READ); |
542 | ev_io_start (EV_A_ &linuxaio_epoll_w); |
648 | ev_io_start (EV_A_ &linuxaio_epoll_w); |