ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev_linuxaio.c
(Generate patch)

Comparing libev/ev_linuxaio.c (file contents):
Revision 1.35 by root, Wed Jun 26 00:11:24 2019 UTC vs.
Revision 1.40 by root, Mon Jul 1 20:35:17 2019 UTC

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
106struct aio_ring 106struct 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. */
297 iocb->io.aio_reqprio = 0; 297 iocb->io.aio_reqprio = 0;
298 } 298 }
299 299
300 if (iocb->io.aio_buf) 300 if (iocb->io.aio_buf)
301 { 301 {
302 for (;;)
303 {
304 /* on all relevant kernels, io_cancel fails with EINPROGRESS on "success" */
302 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))
303 /* on relevant kernels, io_cancel fails with EINPROGRES if everything is fine */ 306 break;
307
308 if (expect_true (errno == EINPROGRESS))
309 break;
310
311 /* the EINPROGRESS test is for nicer error message. clumsy. */
304 assert (("libev: linuxaio unexpected io_cancel failed", errno != EINPROGRESS)); 312 assert (("libev: linuxaio unexpected io_cancel failed", errno != EINPROGRESS && errno != EINTR));
313 }
305 } 314 }
306 315
307 if (nev) 316 if (nev)
308 { 317 {
309 iocb->io.aio_buf = 318 iocb->io.aio_buf =
372 unsigned tail = *(volatile unsigned *)&ring->tail; 381 unsigned tail = *(volatile unsigned *)&ring->tail;
373 382
374 if (head == tail) 383 if (head == tail)
375 return 0; 384 return 0;
376 385
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 */ 386 /* make sure the events up to tail are visible */
384 ECB_MEMORY_FENCE_ACQUIRE; 387 ECB_MEMORY_FENCE_ACQUIRE;
385 388
386 /* parse all available events, but only once, to avoid starvation */ 389 /* parse all available events, but only once, to avoid starvation */
387 if (tail > head) /* normal case around */ 390 if (tail > head) /* normal case around */
397 *(volatile unsigned *)&ring->head = tail; 400 *(volatile unsigned *)&ring->head = tail;
398 401
399 return 1; 402 return 1;
400} 403}
401 404
405inline_size
406int
407linuxaio_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
402/* read at least one event from kernel, or timeout */ 416/* read at least one event from kernel, or timeout */
403inline_size 417inline_size
404void 418void
405linuxaio_get_events (EV_P_ ev_tstamp timeout) 419linuxaio_get_events (EV_P_ ev_tstamp timeout)
406{ 420{
407 struct timespec ts; 421 struct timespec ts;
408 struct io_event ioev[1]; 422 struct io_event ioev[8]; /* 256 octet stack space */
409 int res; 423 int want = 1; /* how many events to request */
424 int ringbuf_valid = linuxaio_ringbuf_valid (EV_A);
410 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 */
411 if (linuxaio_get_events_from_ring (EV_A)) 429 if (linuxaio_get_events_from_ring (EV_A))
412 return; 430 return;
413 431
414 /* 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 */
415 /* this degrades to one event per loop iteration */ 433 if (!timeout)
416 /* 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]);
417 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
418 EV_RELEASE_CB; 447 EV_RELEASE_CB;
419 448
420 ts.tv_sec = (long)timeout; 449 ts.tv_sec = (long)timeout;
421 ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9); 450 ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9);
422 451
423 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);
424 453
425 EV_ACQUIRE_CB; 454 EV_ACQUIRE_CB;
426 455
427 if (res < 0) 456 if (res < 0)
428 if (errno == EINTR) 457 if (errno == EINTR)
429 /* ignored */; 458 /* ignored, retry */;
430 else 459 else
431 ev_syserr ("(libev) linuxaio io_getevents"); 460 ev_syserr ("(libev) linuxaio io_getevents");
432 else if (res) 461 else if (res)
433 { 462 {
434 /* at least one event available, handle it and any remaining ones in the ring buffer */ 463 /* at least one event available, handle them */
435 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 */
436 linuxaio_get_events_from_ring (EV_A); 469 linuxaio_get_events_from_ring (EV_A);
470
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 */
437 } 482 }
438} 483}
439 484
440inline_size 485inline_size
441int 486int
517 assert (("libev: event loop rejected bad fd", errno != EBADF)); 562 assert (("libev: event loop rejected bad fd", errno != EBADF));
518 fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes); 563 fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes);
519 564
520 res = 1; /* skip this iocb */ 565 res = 1; /* skip this iocb */
521 } 566 }
567 else if (errno == EINTR) /* not seen in reality, not documented */
568 res = 0; /* silently ignore and retry */
522 else 569 else
523 ev_syserr ("(libev) linuxaio io_submit"); 570 ev_syserr ("(libev) linuxaio io_submit");
524 571
525 submitted += res; 572 submitted += res;
526 } 573 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines