… | |
… | |
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. */ |
… | |
… | |
286 | linuxaio_modify (EV_P_ int fd, int oev, int nev) |
286 | linuxaio_modify (EV_P_ int fd, int oev, int nev) |
287 | { |
287 | { |
288 | array_needsize (ANIOCBP, linuxaio_iocbps, linuxaio_iocbpmax, fd + 1, linuxaio_array_needsize_iocbp); |
288 | array_needsize (ANIOCBP, linuxaio_iocbps, linuxaio_iocbpmax, fd + 1, linuxaio_array_needsize_iocbp); |
289 | ANIOCBP iocb = linuxaio_iocbps [fd]; |
289 | ANIOCBP iocb = linuxaio_iocbps [fd]; |
290 | |
290 | |
291 | if (iocb->io.aio_reqprio < 0) |
291 | if (ecb_expect_false (iocb->io.aio_reqprio < 0)) |
292 | { |
292 | { |
293 | /* we handed this fd over to epoll, so undo this first */ |
293 | /* we handed this fd over to epoll, so undo this first */ |
294 | /* we do it manually because the optimisations on epoll_modify won't do us any good */ |
294 | /* we do it manually because the optimisations on epoll_modify won't do us any good */ |
295 | epoll_ctl (backend_fd, EPOLL_CTL_DEL, fd, 0); |
295 | epoll_ctl (backend_fd, EPOLL_CTL_DEL, fd, 0); |
296 | anfds [fd].emask = 0; |
296 | anfds [fd].emask = 0; |
297 | iocb->io.aio_reqprio = 0; |
297 | iocb->io.aio_reqprio = 0; |
298 | } |
298 | } |
299 | |
299 | |
300 | if (iocb->io.aio_buf) |
300 | if (ecb_expect_false (iocb->io.aio_buf)) |
301 | { |
301 | { |
|
|
302 | /* iocb active, so cancel it first before resubmit */ |
|
|
303 | for (;;) |
|
|
304 | { |
|
|
305 | /* on all relevant kernels, io_cancel fails with EINPROGRESS on "success" */ |
302 | evsys_io_cancel (linuxaio_ctx, &iocb->io, (struct io_event *)0); |
306 | if (ecb_expect_false (evsys_io_cancel (linuxaio_ctx, &iocb->io, (struct io_event *)0) == 0)) |
303 | /* on relevant kernels, io_cancel fails with EINPROGRES if everything is fine */ |
307 | break; |
|
|
308 | |
|
|
309 | if (ecb_expect_true (errno == EINPROGRESS)) |
|
|
310 | break; |
|
|
311 | |
|
|
312 | /* the EINPROGRESS test is for nicer error message. clumsy. */ |
304 | assert (("libev: linuxaio unexpected io_cancel failed", errno != EINPROGRESS)); |
313 | assert (("libev: linuxaio unexpected io_cancel failed", errno != EINPROGRESS && errno != EINTR)); |
305 | } |
314 | } |
|
|
315 | } |
|
|
316 | |
|
|
317 | iocb->io.aio_buf = |
|
|
318 | (nev & EV_READ ? POLLIN : 0) |
|
|
319 | | (nev & EV_WRITE ? POLLOUT : 0); |
306 | |
320 | |
307 | if (nev) |
321 | if (nev) |
308 | { |
322 | { |
309 | iocb->io.aio_buf = |
|
|
310 | (nev & EV_READ ? POLLIN : 0) |
|
|
311 | | (nev & EV_WRITE ? POLLOUT : 0); |
|
|
312 | |
|
|
313 | /* queue iocb up for io_submit */ |
323 | /* queue iocb up for io_submit */ |
314 | /* this assumes we only ever get one call per fd per loop iteration */ |
324 | /* this assumes we only ever get one call per fd per loop iteration */ |
315 | ++linuxaio_submitcnt; |
325 | ++linuxaio_submitcnt; |
316 | array_needsize (struct iocb *, linuxaio_submits, linuxaio_submitmax, linuxaio_submitcnt, array_needsize_noinit); |
326 | array_needsize (struct iocb *, linuxaio_submits, linuxaio_submitmax, linuxaio_submitcnt, array_needsize_noinit); |
317 | linuxaio_submits [linuxaio_submitcnt - 1] = &iocb->io; |
327 | linuxaio_submits [linuxaio_submitcnt - 1] = &iocb->io; |
… | |
… | |
372 | unsigned tail = *(volatile unsigned *)&ring->tail; |
382 | unsigned tail = *(volatile unsigned *)&ring->tail; |
373 | |
383 | |
374 | if (head == tail) |
384 | if (head == tail) |
375 | return 0; |
385 | return 0; |
376 | |
386 | |
377 | /* bail out if the ring buffer doesn't match the expected layout */ |
|
|
378 | if (expect_false (ring->magic != AIO_RING_MAGIC) |
|
|
379 | || ring->incompat_features != AIO_RING_INCOMPAT_FEATURES |
|
|
380 | || ring->header_length != sizeof (struct aio_ring)) /* TODO: or use it to find io_event[0]? */ |
|
|
381 | return 0; |
|
|
382 | |
|
|
383 | /* make sure the events up to tail are visible */ |
387 | /* make sure the events up to tail are visible */ |
384 | ECB_MEMORY_FENCE_ACQUIRE; |
388 | ECB_MEMORY_FENCE_ACQUIRE; |
385 | |
389 | |
386 | /* parse all available events, but only once, to avoid starvation */ |
390 | /* parse all available events, but only once, to avoid starvation */ |
387 | if (tail > head) /* normal case around */ |
391 | if (tail > head) /* normal case around */ |
… | |
… | |
397 | *(volatile unsigned *)&ring->head = tail; |
401 | *(volatile unsigned *)&ring->head = tail; |
398 | |
402 | |
399 | return 1; |
403 | return 1; |
400 | } |
404 | } |
401 | |
405 | |
|
|
406 | inline_size |
|
|
407 | int |
|
|
408 | linuxaio_ringbuf_valid (EV_P) |
|
|
409 | { |
|
|
410 | struct aio_ring *ring = (struct aio_ring *)linuxaio_ctx; |
|
|
411 | |
|
|
412 | return ecb_expect_true (ring->magic == AIO_RING_MAGIC) |
|
|
413 | && ring->incompat_features == EV_AIO_RING_INCOMPAT_FEATURES |
|
|
414 | && ring->header_length == sizeof (struct aio_ring); /* TODO: or use it to find io_event[0]? */ |
|
|
415 | } |
|
|
416 | |
402 | /* read at least one event from kernel, or timeout */ |
417 | /* read at least one event from kernel, or timeout */ |
403 | inline_size |
418 | inline_size |
404 | void |
419 | void |
405 | linuxaio_get_events (EV_P_ ev_tstamp timeout) |
420 | linuxaio_get_events (EV_P_ ev_tstamp timeout) |
406 | { |
421 | { |
407 | struct timespec ts; |
422 | struct timespec ts; |
408 | struct io_event ioev[1]; |
423 | struct io_event ioev[8]; /* 256 octet stack space */ |
409 | int res; |
424 | int want = 1; /* how many events to request */ |
|
|
425 | int ringbuf_valid = linuxaio_ringbuf_valid (EV_A); |
410 | |
426 | |
|
|
427 | if (ecb_expect_true (ringbuf_valid)) |
|
|
428 | { |
|
|
429 | /* if the ring buffer has any events, we don't wait or call the kernel at all */ |
411 | if (linuxaio_get_events_from_ring (EV_A)) |
430 | if (linuxaio_get_events_from_ring (EV_A)) |
412 | return; |
431 | return; |
413 | |
432 | |
414 | /* no events, so wait for at least one, then poll ring buffer again */ |
433 | /* if the ring buffer is empty, and we don't have a timeout, then don't call the kernel */ |
415 | /* this degrades to one event per loop iteration */ |
434 | if (!timeout) |
416 | /* if the ring buffer changes layout, but so be it */ |
435 | return; |
|
|
436 | } |
|
|
437 | else |
|
|
438 | /* no ringbuffer, request slightly larger batch */ |
|
|
439 | want = sizeof (ioev) / sizeof (ioev [0]); |
417 | |
440 | |
|
|
441 | /* no events, so wait for some |
|
|
442 | * for fairness reasons, we do this in a loop, to fetch all events |
|
|
443 | */ |
|
|
444 | for (;;) |
|
|
445 | { |
|
|
446 | int res; |
|
|
447 | |
418 | EV_RELEASE_CB; |
448 | EV_RELEASE_CB; |
419 | |
449 | |
420 | ts.tv_sec = (long)timeout; |
450 | ts.tv_sec = (long)timeout; |
421 | ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9); |
451 | ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9); |
422 | |
452 | |
423 | res = evsys_io_getevents (linuxaio_ctx, 1, sizeof (ioev) / sizeof (ioev [0]), ioev, &ts); |
453 | res = evsys_io_getevents (linuxaio_ctx, 1, want, ioev, &ts); |
424 | |
454 | |
425 | EV_ACQUIRE_CB; |
455 | EV_ACQUIRE_CB; |
426 | |
456 | |
427 | if (res < 0) |
457 | if (res < 0) |
428 | if (errno == EINTR) |
458 | if (errno == EINTR) |
429 | /* ignored */; |
459 | /* ignored, retry */; |
430 | else |
460 | else |
431 | ev_syserr ("(libev) linuxaio io_getevents"); |
461 | ev_syserr ("(libev) linuxaio io_getevents"); |
432 | else if (res) |
462 | else if (res) |
433 | { |
463 | { |
434 | /* at least one event available, handle it and any remaining ones in the ring buffer */ |
464 | /* at least one event available, handle them */ |
435 | linuxaio_parse_events (EV_A_ ioev, res); |
465 | linuxaio_parse_events (EV_A_ ioev, res); |
|
|
466 | |
|
|
467 | if (ecb_expect_true (ringbuf_valid)) |
|
|
468 | { |
|
|
469 | /* if we have a ring buffer, handle any remaining events in it */ |
436 | linuxaio_get_events_from_ring (EV_A); |
470 | linuxaio_get_events_from_ring (EV_A); |
|
|
471 | |
|
|
472 | /* at this point, we should have handled all outstanding events */ |
|
|
473 | break; |
|
|
474 | } |
|
|
475 | else if (res < want) |
|
|
476 | /* otherwise, if there were fewere events than we wanted, we assume there are no more */ |
|
|
477 | break; |
|
|
478 | } |
|
|
479 | else |
|
|
480 | break; /* no events from the kernel, we are done */ |
|
|
481 | |
|
|
482 | timeout = 0; /* only wait in the first iteration */ |
437 | } |
483 | } |
438 | } |
484 | } |
439 | |
485 | |
440 | inline_size |
486 | inline_size |
441 | int |
487 | int |
… | |
… | |
457 | /* which allows us to pinpoint the erroneous iocb */ |
503 | /* which allows us to pinpoint the erroneous iocb */ |
458 | for (submitted = 0; submitted < linuxaio_submitcnt; ) |
504 | for (submitted = 0; submitted < linuxaio_submitcnt; ) |
459 | { |
505 | { |
460 | int res = evsys_io_submit (linuxaio_ctx, linuxaio_submitcnt - submitted, linuxaio_submits + submitted); |
506 | int res = evsys_io_submit (linuxaio_ctx, linuxaio_submitcnt - submitted, linuxaio_submits + submitted); |
461 | |
507 | |
462 | if (expect_false (res < 0)) |
508 | if (ecb_expect_false (res < 0)) |
463 | if (errno == EINVAL) |
509 | if (errno == EINVAL) |
464 | { |
510 | { |
465 | /* This happens for unsupported fds, officially, but in my testing, |
511 | /* This happens for unsupported fds, officially, but in my testing, |
466 | * also randomly happens for supported fds. We fall back to good old |
512 | * also randomly happens for supported fds. We fall back to good old |
467 | * poll() here, under the assumption that this is a very rare case. |
513 | * poll() here, under the assumption that this is a very rare case. |
… | |
… | |
517 | assert (("libev: event loop rejected bad fd", errno != EBADF)); |
563 | assert (("libev: event loop rejected bad fd", errno != EBADF)); |
518 | fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes); |
564 | fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes); |
519 | |
565 | |
520 | res = 1; /* skip this iocb */ |
566 | res = 1; /* skip this iocb */ |
521 | } |
567 | } |
|
|
568 | else if (errno == EINTR) /* not seen in reality, not documented */ |
|
|
569 | res = 0; /* silently ignore and retry */ |
522 | else |
570 | else |
523 | ev_syserr ("(libev) linuxaio io_submit"); |
571 | ev_syserr ("(libev) linuxaio io_submit"); |
524 | |
572 | |
525 | submitted += res; |
573 | submitted += res; |
526 | } |
574 | } |