… | |
… | |
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. */ |
… | |
… | |
124 | * our syscalls return < 0, not == -1, on error. which is good |
124 | * our syscalls return < 0, not == -1, on error. which is good |
125 | * enough for linux aio. |
125 | * enough for linux aio. |
126 | * TODO: arm is also common nowadays, maybe even mips and x86 |
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... |
127 | * TODO: after implementing this, it suddenly looks like overkill, but its hard to remove... |
128 | */ |
128 | */ |
129 | #if __GNUC__ && __linux && ECB_AMD64 |
129 | #if __GNUC__ && __linux && ECB_AMD64 && !defined __OPTIMIZE_SIZE__ |
|
|
130 | /* the costly errno access probably kills this for size optimisation */ |
130 | |
131 | |
131 | #define ev_syscall(nr,narg,arg1,arg2,arg3,arg4,arg5) \ |
132 | #define ev_syscall(nr,narg,arg1,arg2,arg3,arg4,arg5) \ |
132 | ({ \ |
133 | ({ \ |
133 | long res; \ |
134 | long res; \ |
134 | register unsigned long r5 __asm__ ("r8" ); \ |
135 | register unsigned long r5 __asm__ ("r8" ); \ |
… | |
… | |
288 | ANIOCBP iocb = linuxaio_iocbps [fd]; |
289 | ANIOCBP iocb = linuxaio_iocbps [fd]; |
289 | |
290 | |
290 | if (iocb->io.aio_reqprio < 0) |
291 | if (iocb->io.aio_reqprio < 0) |
291 | { |
292 | { |
292 | /* we handed this fd over to epoll, so undo this first */ |
293 | /* we handed this fd over to epoll, so undo this first */ |
293 | /* 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 */ |
294 | epoll_ctl (backend_fd, EPOLL_CTL_DEL, fd, 0); |
295 | epoll_ctl (backend_fd, EPOLL_CTL_DEL, fd, 0); |
295 | anfds [fd].emask = 0; |
296 | anfds [fd].emask = 0; |
296 | iocb->io.aio_reqprio = 0; |
297 | iocb->io.aio_reqprio = 0; |
297 | } |
298 | } |
298 | |
299 | |
299 | if (iocb->io.aio_buf) |
300 | if (iocb->io.aio_buf) |
300 | /* 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" */ |
301 | 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) == 09) |
|
|
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 | } |
302 | |
315 | |
303 | if (nev) |
316 | if (nev) |
304 | { |
317 | { |
305 | iocb->io.aio_buf = |
318 | iocb->io.aio_buf = |
306 | (nev & EV_READ ? POLLIN : 0) |
319 | (nev & EV_READ ? POLLIN : 0) |
… | |
… | |
318 | linuxaio_epoll_cb (EV_P_ struct ev_io *w, int revents) |
331 | linuxaio_epoll_cb (EV_P_ struct ev_io *w, int revents) |
319 | { |
332 | { |
320 | epoll_poll (EV_A_ 0); |
333 | epoll_poll (EV_A_ 0); |
321 | } |
334 | } |
322 | |
335 | |
323 | static void |
336 | inline_speed |
|
|
337 | void |
324 | linuxaio_fd_rearm (EV_P_ int fd) |
338 | linuxaio_fd_rearm (EV_P_ int fd) |
325 | { |
339 | { |
326 | anfds [fd].events = 0; |
340 | anfds [fd].events = 0; |
327 | linuxaio_iocbps [fd]->io.aio_buf = 0; |
341 | linuxaio_iocbps [fd]->io.aio_buf = 0; |
328 | fd_change (EV_A_ fd, EV_ANFD_REIFY); |
342 | fd_change (EV_A_ fd, EV_ANFD_REIFY); |
… | |
… | |
344 | fd, |
358 | fd, |
345 | (res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
359 | (res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
346 | | (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
360 | | (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
347 | ); |
361 | ); |
348 | |
362 | |
349 | /* 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 */ |
350 | linuxaio_fd_rearm (EV_A_ fd); |
364 | linuxaio_fd_rearm (EV_A_ fd); |
351 | |
365 | |
352 | --nr; |
366 | --nr; |
353 | ++ev; |
367 | ++ev; |
354 | } |
368 | } |
… | |
… | |
367 | unsigned tail = *(volatile unsigned *)&ring->tail; |
381 | unsigned tail = *(volatile unsigned *)&ring->tail; |
368 | |
382 | |
369 | if (head == tail) |
383 | if (head == tail) |
370 | return 0; |
384 | return 0; |
371 | |
385 | |
372 | /* bail out if the ring buffer doesn't match the expected layout */ |
|
|
373 | if (expect_false (ring->magic != AIO_RING_MAGIC) |
|
|
374 | || ring->incompat_features != AIO_RING_INCOMPAT_FEATURES |
|
|
375 | || ring->header_length != sizeof (struct aio_ring)) /* TODO: or use it to find io_event[0]? */ |
|
|
376 | return 0; |
|
|
377 | |
|
|
378 | /* make sure the events up to tail are visible */ |
386 | /* make sure the events up to tail are visible */ |
379 | ECB_MEMORY_FENCE_ACQUIRE; |
387 | ECB_MEMORY_FENCE_ACQUIRE; |
380 | |
388 | |
381 | /* parse all available events, but only once, to avoid starvation */ |
389 | /* parse all available events, but only once, to avoid starvation */ |
382 | if (tail > head) /* normal case around */ |
390 | if (tail > head) /* normal case around */ |
… | |
… | |
392 | *(volatile unsigned *)&ring->head = tail; |
400 | *(volatile unsigned *)&ring->head = tail; |
393 | |
401 | |
394 | return 1; |
402 | return 1; |
395 | } |
403 | } |
396 | |
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 | |
397 | /* read at least one event from kernel, or timeout */ |
416 | /* read at least one event from kernel, or timeout */ |
398 | inline_size |
417 | inline_size |
399 | void |
418 | void |
400 | linuxaio_get_events (EV_P_ ev_tstamp timeout) |
419 | linuxaio_get_events (EV_P_ ev_tstamp timeout) |
401 | { |
420 | { |
402 | struct timespec ts; |
421 | struct timespec ts; |
403 | struct io_event ioev[1]; |
422 | struct io_event ioev[8]; /* 256 octet stack space */ |
404 | int res; |
423 | int want = 1; /* how many events to request */ |
|
|
424 | int ringbuf_valid = linuxaio_ringbuf_valid (EV_A); |
405 | |
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 */ |
406 | if (linuxaio_get_events_from_ring (EV_A)) |
429 | if (linuxaio_get_events_from_ring (EV_A)) |
407 | return; |
430 | return; |
408 | |
431 | |
409 | /* 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 */ |
410 | /* this degrades to one event per loop iteration */ |
433 | if (!timeout) |
411 | /* 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]); |
412 | |
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 | |
413 | EV_RELEASE_CB; |
447 | EV_RELEASE_CB; |
414 | |
448 | |
415 | ts.tv_sec = (long)timeout; |
449 | ts.tv_sec = (long)timeout; |
416 | ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9); |
450 | ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9); |
417 | |
451 | |
418 | 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); |
419 | |
453 | |
420 | EV_ACQUIRE_CB; |
454 | EV_ACQUIRE_CB; |
421 | |
455 | |
422 | if (res < 0) |
456 | if (res < 0) |
423 | if (errno == EINTR) |
457 | if (errno == EINTR) |
424 | /* ignored */; |
458 | /* ignored, retry */; |
425 | else |
459 | else |
426 | ev_syserr ("(libev) linuxaio io_getevents"); |
460 | ev_syserr ("(libev) linuxaio io_getevents"); |
427 | else if (res) |
461 | else if (res) |
428 | { |
462 | { |
429 | /* at least one event received, handle it and any remaining ones in the ring buffer */ |
463 | /* at least one event available, handle them */ |
430 | 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 */ |
431 | linuxaio_get_events_from_ring (EV_A); |
469 | linuxaio_get_events_from_ring (EV_A); |
432 | } |
|
|
433 | } |
|
|
434 | |
470 | |
435 | 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 |
436 | linuxaio_io_setup (EV_P) |
487 | linuxaio_io_setup (EV_P) |
437 | { |
488 | { |
438 | linuxaio_ctx = 0; |
489 | linuxaio_ctx = 0; |
439 | return evsys_io_setup (linuxaio_nr_events (EV_A), &linuxaio_ctx); |
490 | return evsys_io_setup (linuxaio_nr_events (EV_A), &linuxaio_ctx); |
440 | } |
491 | } |
… | |
… | |
506 | /* it's easiest to handle this mess in another iteration */ |
557 | /* it's easiest to handle this mess in another iteration */ |
507 | return; |
558 | return; |
508 | } |
559 | } |
509 | else if (errno == EBADF) |
560 | else if (errno == EBADF) |
510 | { |
561 | { |
|
|
562 | assert (("libev: event loop rejected bad fd", errno != EBADF)); |
511 | fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes); |
563 | fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes); |
512 | |
564 | |
513 | res = 1; /* skip this iocb */ |
565 | res = 1; /* skip this iocb */ |
514 | } |
566 | } |
|
|
567 | else if (errno == EINTR) /* not seen in reality, not documented */ |
|
|
568 | res = 0; /* silently ignore and retry */ |
515 | else |
569 | else |
516 | ev_syserr ("(libev) linuxaio io_submit"); |
570 | ev_syserr ("(libev) linuxaio io_submit"); |
517 | |
571 | |
518 | submitted += res; |
572 | submitted += res; |
519 | } |
573 | } |
… | |
… | |
568 | void |
622 | void |
569 | linuxaio_destroy (EV_P) |
623 | linuxaio_destroy (EV_P) |
570 | { |
624 | { |
571 | epoll_destroy (EV_A); |
625 | epoll_destroy (EV_A); |
572 | linuxaio_free_iocbp (EV_A); |
626 | linuxaio_free_iocbp (EV_A); |
573 | evsys_io_destroy (linuxaio_ctx); |
627 | evsys_io_destroy (linuxaio_ctx); /* fails in child, aio context is destroyed */ |
574 | } |
628 | } |
575 | |
629 | |
576 | inline_size |
630 | inline_size |
577 | void |
631 | void |
578 | linuxaio_fork (EV_P) |
632 | linuxaio_fork (EV_P) |
… | |
… | |
584 | linuxaio_iteration = 0; /* we start over in the child */ |
638 | linuxaio_iteration = 0; /* we start over in the child */ |
585 | |
639 | |
586 | while (linuxaio_io_setup (EV_A) < 0) |
640 | while (linuxaio_io_setup (EV_A) < 0) |
587 | ev_syserr ("(libev) linuxaio io_setup"); |
641 | ev_syserr ("(libev) linuxaio io_setup"); |
588 | |
642 | |
|
|
643 | /* forking epoll should also effectively unregister all fds from the backend */ |
589 | epoll_fork (EV_A); |
644 | epoll_fork (EV_A); |
590 | |
645 | |
591 | ev_io_stop (EV_A_ &linuxaio_epoll_w); |
646 | ev_io_stop (EV_A_ &linuxaio_epoll_w); |
592 | 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); |
593 | ev_io_start (EV_A_ &linuxaio_epoll_w); |
648 | ev_io_start (EV_A_ &linuxaio_epoll_w); |