… | |
… | |
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_modfy 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 (ecb_expect_false (iocb->io.aio_buf)) |
|
|
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" */ |
|
|
306 | if (ecb_expect_false (evsys_io_cancel (linuxaio_ctx, &iocb->io, (struct io_event *)0) == 0)) |
|
|
307 | break; |
|
|
308 | |
|
|
309 | if (ecb_expect_true (errno == EINPROGRESS)) |
|
|
310 | break; |
|
|
311 | |
|
|
312 | /* the EINPROGRESS test is for nicer error message. clumsy. */ |
|
|
313 | assert (("libev: linuxaio unexpected io_cancel failed", errno != EINPROGRESS && errno != EINTR)); |
|
|
314 | } |
|
|
315 | } |
|
|
316 | |
300 | if (iocb->io.aio_buf) |
317 | iocb->io.aio_buf = |
301 | /* io_cancel always returns some error on relevant kernels, but works */ |
318 | (nev & EV_READ ? POLLIN : 0) |
302 | evsys_io_cancel (linuxaio_ctx, &iocb->io, (struct io_event *)0); |
319 | | (nev & EV_WRITE ? POLLOUT : 0); |
303 | |
320 | |
304 | if (nev) |
321 | if (nev) |
305 | { |
322 | { |
306 | iocb->io.aio_buf = |
|
|
307 | (nev & EV_READ ? POLLIN : 0) |
|
|
308 | | (nev & EV_WRITE ? POLLOUT : 0); |
|
|
309 | |
|
|
310 | /* queue iocb up for io_submit */ |
323 | /* queue iocb up for io_submit */ |
311 | /* 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 */ |
312 | ++linuxaio_submitcnt; |
325 | ++linuxaio_submitcnt; |
313 | 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); |
314 | linuxaio_submits [linuxaio_submitcnt - 1] = &iocb->io; |
327 | linuxaio_submits [linuxaio_submitcnt - 1] = &iocb->io; |
… | |
… | |
319 | linuxaio_epoll_cb (EV_P_ struct ev_io *w, int revents) |
332 | linuxaio_epoll_cb (EV_P_ struct ev_io *w, int revents) |
320 | { |
333 | { |
321 | epoll_poll (EV_A_ 0); |
334 | epoll_poll (EV_A_ 0); |
322 | } |
335 | } |
323 | |
336 | |
324 | static void |
337 | inline_speed |
|
|
338 | void |
325 | linuxaio_fd_rearm (EV_P_ int fd) |
339 | linuxaio_fd_rearm (EV_P_ int fd) |
326 | { |
340 | { |
327 | anfds [fd].events = 0; |
341 | anfds [fd].events = 0; |
328 | linuxaio_iocbps [fd]->io.aio_buf = 0; |
342 | linuxaio_iocbps [fd]->io.aio_buf = 0; |
329 | fd_change (EV_A_ fd, EV_ANFD_REIFY); |
343 | fd_change (EV_A_ fd, EV_ANFD_REIFY); |
… | |
… | |
345 | fd, |
359 | fd, |
346 | (res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
360 | (res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
347 | | (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
361 | | (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
348 | ); |
362 | ); |
349 | |
363 | |
350 | /* linux aio is oneshot: rearm fd. TODO: this does more work than needed */ |
364 | /* linux aio is oneshot: rearm fd. TODO: this does more work than strictly needed */ |
351 | linuxaio_fd_rearm (EV_A_ fd); |
365 | linuxaio_fd_rearm (EV_A_ fd); |
352 | |
366 | |
353 | --nr; |
367 | --nr; |
354 | ++ev; |
368 | ++ev; |
355 | } |
369 | } |
… | |
… | |
368 | unsigned tail = *(volatile unsigned *)&ring->tail; |
382 | unsigned tail = *(volatile unsigned *)&ring->tail; |
369 | |
383 | |
370 | if (head == tail) |
384 | if (head == tail) |
371 | return 0; |
385 | return 0; |
372 | |
386 | |
373 | /* bail out if the ring buffer doesn't match the expected layout */ |
|
|
374 | if (expect_false (ring->magic != AIO_RING_MAGIC) |
|
|
375 | || ring->incompat_features != AIO_RING_INCOMPAT_FEATURES |
|
|
376 | || ring->header_length != sizeof (struct aio_ring)) /* TODO: or use it to find io_event[0]? */ |
|
|
377 | return 0; |
|
|
378 | |
|
|
379 | /* make sure the events up to tail are visible */ |
387 | /* make sure the events up to tail are visible */ |
380 | ECB_MEMORY_FENCE_ACQUIRE; |
388 | ECB_MEMORY_FENCE_ACQUIRE; |
381 | |
389 | |
382 | /* parse all available events, but only once, to avoid starvation */ |
390 | /* parse all available events, but only once, to avoid starvation */ |
383 | if (tail > head) /* normal case around */ |
391 | if (tail > head) /* normal case around */ |
… | |
… | |
393 | *(volatile unsigned *)&ring->head = tail; |
401 | *(volatile unsigned *)&ring->head = tail; |
394 | |
402 | |
395 | return 1; |
403 | return 1; |
396 | } |
404 | } |
397 | |
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 | |
398 | /* read at least one event from kernel, or timeout */ |
417 | /* read at least one event from kernel, or timeout */ |
399 | inline_size |
418 | inline_size |
400 | void |
419 | void |
401 | linuxaio_get_events (EV_P_ ev_tstamp timeout) |
420 | linuxaio_get_events (EV_P_ ev_tstamp timeout) |
402 | { |
421 | { |
403 | struct timespec ts; |
422 | struct timespec ts; |
404 | struct io_event ioev[1]; |
423 | struct io_event ioev[8]; /* 256 octet stack space */ |
405 | int res; |
424 | int want = 1; /* how many events to request */ |
|
|
425 | int ringbuf_valid = linuxaio_ringbuf_valid (EV_A); |
406 | |
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 */ |
407 | if (linuxaio_get_events_from_ring (EV_A)) |
430 | if (linuxaio_get_events_from_ring (EV_A)) |
408 | return; |
431 | return; |
409 | |
432 | |
410 | /* 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 */ |
411 | /* this degrades to one event per loop iteration */ |
434 | if (!timeout) |
412 | /* 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]); |
413 | |
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 | |
414 | EV_RELEASE_CB; |
448 | EV_RELEASE_CB; |
415 | |
449 | |
416 | ts.tv_sec = (long)timeout; |
450 | ts.tv_sec = (long)timeout; |
417 | ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9); |
451 | ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9); |
418 | |
452 | |
419 | 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); |
420 | |
454 | |
421 | EV_ACQUIRE_CB; |
455 | EV_ACQUIRE_CB; |
422 | |
456 | |
423 | if (res < 0) |
457 | if (res < 0) |
424 | if (errno == EINTR) |
458 | if (errno == EINTR) |
425 | /* ignored */; |
459 | /* ignored, retry */; |
426 | else |
460 | else |
427 | ev_syserr ("(libev) linuxaio io_getevents"); |
461 | ev_syserr ("(libev) linuxaio io_getevents"); |
428 | else if (res) |
462 | else if (res) |
429 | { |
463 | { |
430 | /* at least one event available, handle it and any remaining ones in the ring buffer */ |
464 | /* at least one event available, handle them */ |
431 | 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 */ |
432 | 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 */ |
433 | } |
483 | } |
434 | } |
484 | } |
435 | |
485 | |
436 | inline_size |
486 | inline_size |
437 | int |
487 | int |
… | |
… | |
453 | /* which allows us to pinpoint the erroneous iocb */ |
503 | /* which allows us to pinpoint the erroneous iocb */ |
454 | for (submitted = 0; submitted < linuxaio_submitcnt; ) |
504 | for (submitted = 0; submitted < linuxaio_submitcnt; ) |
455 | { |
505 | { |
456 | 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); |
457 | |
507 | |
458 | if (expect_false (res < 0)) |
508 | if (ecb_expect_false (res < 0)) |
459 | if (errno == EINVAL) |
509 | if (errno == EINVAL) |
460 | { |
510 | { |
461 | /* This happens for unsupported fds, officially, but in my testing, |
511 | /* This happens for unsupported fds, officially, but in my testing, |
462 | * 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 |
463 | * 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. |
… | |
… | |
508 | /* it's easiest to handle this mess in another iteration */ |
558 | /* it's easiest to handle this mess in another iteration */ |
509 | return; |
559 | return; |
510 | } |
560 | } |
511 | else if (errno == EBADF) |
561 | else if (errno == EBADF) |
512 | { |
562 | { |
|
|
563 | assert (("libev: event loop rejected bad fd", errno != EBADF)); |
513 | fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes); |
564 | fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes); |
514 | |
565 | |
515 | res = 1; /* skip this iocb */ |
566 | res = 1; /* skip this iocb */ |
516 | } |
567 | } |
|
|
568 | else if (errno == EINTR) /* not seen in reality, not documented */ |
|
|
569 | res = 0; /* silently ignore and retry */ |
517 | else |
570 | else |
518 | ev_syserr ("(libev) linuxaio io_submit"); |
571 | ev_syserr ("(libev) linuxaio io_submit"); |
519 | |
572 | |
520 | submitted += res; |
573 | submitted += res; |
521 | } |
574 | } |