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.32 by root, Tue Jun 25 18:07:44 2019 UTC vs.
Revision 1.37 by root, Wed Jun 26 07:20:09 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. */
289 ANIOCBP iocb = linuxaio_iocbps [fd]; 289 ANIOCBP iocb = linuxaio_iocbps [fd];
290 290
291 if (iocb->io.aio_reqprio < 0) 291 if (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 (iocb->io.aio_buf) 300 if (iocb->io.aio_buf)
301 /* io_cancel always returns some error on relevant kernels, but works */ 301 {
302 evsys_io_cancel (linuxaio_ctx, &iocb->io, (struct io_event *)0); 302 evsys_io_cancel (linuxaio_ctx, &iocb->io, (struct io_event *)0);
303 /* on relevant kernels, io_cancel fails with EINPROGRES if everything is fine */
304 assert (("libev: linuxaio unexpected io_cancel failed", errno == EINPROGRESS));
305 }
303 306
304 if (nev) 307 if (nev)
305 { 308 {
306 iocb->io.aio_buf = 309 iocb->io.aio_buf =
307 (nev & EV_READ ? POLLIN : 0) 310 (nev & EV_READ ? POLLIN : 0)
319linuxaio_epoll_cb (EV_P_ struct ev_io *w, int revents) 322linuxaio_epoll_cb (EV_P_ struct ev_io *w, int revents)
320{ 323{
321 epoll_poll (EV_A_ 0); 324 epoll_poll (EV_A_ 0);
322} 325}
323 326
324static void 327inline_speed
328void
325linuxaio_fd_rearm (EV_P_ int fd) 329linuxaio_fd_rearm (EV_P_ int fd)
326{ 330{
327 anfds [fd].events = 0; 331 anfds [fd].events = 0;
328 linuxaio_iocbps [fd]->io.aio_buf = 0; 332 linuxaio_iocbps [fd]->io.aio_buf = 0;
329 fd_change (EV_A_ fd, EV_ANFD_REIFY); 333 fd_change (EV_A_ fd, EV_ANFD_REIFY);
345 fd, 349 fd,
346 (res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) 350 (res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
347 | (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) 351 | (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
348 ); 352 );
349 353
350 /* linux aio is oneshot: rearm fd. TODO: this does more work than needed */ 354 /* linux aio is oneshot: rearm fd. TODO: this does more work than strictly needed */
351 linuxaio_fd_rearm (EV_A_ fd); 355 linuxaio_fd_rearm (EV_A_ fd);
352 356
353 --nr; 357 --nr;
354 ++ev; 358 ++ev;
355 } 359 }
368 unsigned tail = *(volatile unsigned *)&ring->tail; 372 unsigned tail = *(volatile unsigned *)&ring->tail;
369 373
370 if (head == tail) 374 if (head == tail)
371 return 0; 375 return 0;
372 376
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 */ 377 /* make sure the events up to tail are visible */
380 ECB_MEMORY_FENCE_ACQUIRE; 378 ECB_MEMORY_FENCE_ACQUIRE;
381 379
382 /* parse all available events, but only once, to avoid starvation */ 380 /* parse all available events, but only once, to avoid starvation */
383 if (tail > head) /* normal case around */ 381 if (tail > head) /* normal case around */
393 *(volatile unsigned *)&ring->head = tail; 391 *(volatile unsigned *)&ring->head = tail;
394 392
395 return 1; 393 return 1;
396} 394}
397 395
396inline_size
397int
398linuxaio_ringbuf_valid (EV_P)
399{
400 struct aio_ring *ring = (struct aio_ring *)linuxaio_ctx;
401
402 return expect_true (ring->magic == AIO_RING_MAGIC)
403 && ring->incompat_features == EV_AIO_RING_INCOMPAT_FEATURES
404 && ring->header_length == sizeof (struct aio_ring); /* TODO: or use it to find io_event[0]? */
405}
406
398/* read at least one event from kernel, or timeout */ 407/* read at least one event from kernel, or timeout */
399inline_size 408inline_size
400void 409void
401linuxaio_get_events (EV_P_ ev_tstamp timeout) 410linuxaio_get_events (EV_P_ ev_tstamp timeout)
402{ 411{
403 struct timespec ts; 412 struct timespec ts;
404 struct io_event ioev[1]; 413 struct io_event ioev[8]; /* 256 octet stack space */
405 int res; 414 int want = 1; /* how many events to request */
415 int ringbuf_valid = linuxaio_ringbuf_valid (EV_A);
406 416
417 if (expect_true (ringbuf_valid))
418 {
419 /* 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)) 420 if (linuxaio_get_events_from_ring (EV_A))
408 return; 421 return;
409 422
410 /* no events, so wait for at least one, then poll ring buffer again */ 423 /* 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 */ 424 if (!timeout)
412 /* if the ring buffer changes layout, but so be it */ 425 return;
426 }
427 else
428 /* no ringbuffer, request slightly larger batch */
429 want = sizeof (ioev) / sizeof (ioev [0]);
413 430
431 /* no events, so wait for some
432 * for fairness reasons, we do this in a loop, to fetch all events
433 */
434 for (;;)
435 {
436 int res;
437
414 EV_RELEASE_CB; 438 EV_RELEASE_CB;
415 439
416 ts.tv_sec = (long)timeout; 440 ts.tv_sec = (long)timeout;
417 ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9); 441 ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9);
418 442
419 res = evsys_io_getevents (linuxaio_ctx, 1, sizeof (ioev) / sizeof (ioev [0]), ioev, &ts); 443 res = evsys_io_getevents (linuxaio_ctx, 1, want, ioev, &ts);
420 444
421 EV_ACQUIRE_CB; 445 EV_ACQUIRE_CB;
422 446
423 if (res < 0) 447 if (res < 0)
424 if (errno == EINTR) 448 if (errno == EINTR)
425 /* ignored */; 449 /* ignored, retry */;
426 else 450 else
427 ev_syserr ("(libev) linuxaio io_getevents"); 451 ev_syserr ("(libev) linuxaio io_getevents");
428 else if (res) 452 else if (res)
429 { 453 {
430 /* at least one event available, handle it and any remaining ones in the ring buffer */ 454 /* at least one event available, handle them */
431 linuxaio_parse_events (EV_A_ ioev, res); 455 linuxaio_parse_events (EV_A_ ioev, res);
456
457 if (expect_true (ringbuf_valid))
458 {
459 /* if we have a ring buffer, handle any remaining events in it */
432 linuxaio_get_events_from_ring (EV_A); 460 linuxaio_get_events_from_ring (EV_A);
461
462 /* at this point, we should have handled all outstanding events */
463 break;
464 }
465 else if (res < want)
466 /* otherwise, if there were fewere events than we wanted, we assume there are no more */
467 break;
468 }
469 else
470 break; /* no events from the kernel, we are done */
471
472 timeout = 0; /* only wait in the first iteration */
433 } 473 }
434} 474}
435 475
436inline_size 476inline_size
437int 477int
508 /* it's easiest to handle this mess in another iteration */ 548 /* it's easiest to handle this mess in another iteration */
509 return; 549 return;
510 } 550 }
511 else if (errno == EBADF) 551 else if (errno == EBADF)
512 { 552 {
553 assert (("libev: event loop rejected bad fd", errno != EBADF));
513 fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes); 554 fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes);
514 555
515 res = 1; /* skip this iocb */ 556 res = 1; /* skip this iocb */
516 } 557 }
517 else 558 else
570void 611void
571linuxaio_destroy (EV_P) 612linuxaio_destroy (EV_P)
572{ 613{
573 epoll_destroy (EV_A); 614 epoll_destroy (EV_A);
574 linuxaio_free_iocbp (EV_A); 615 linuxaio_free_iocbp (EV_A);
575 evsys_io_destroy (linuxaio_ctx); 616 evsys_io_destroy (linuxaio_ctx); /* fails in child, aio context is destroyed */
576} 617}
577 618
578inline_size 619inline_size
579void 620void
580linuxaio_fork (EV_P) 621linuxaio_fork (EV_P)
586 linuxaio_iteration = 0; /* we start over in the child */ 627 linuxaio_iteration = 0; /* we start over in the child */
587 628
588 while (linuxaio_io_setup (EV_A) < 0) 629 while (linuxaio_io_setup (EV_A) < 0)
589 ev_syserr ("(libev) linuxaio io_setup"); 630 ev_syserr ("(libev) linuxaio io_setup");
590 631
632 /* forking epoll should also effectively unregister all fds from the backend */
591 epoll_fork (EV_A); 633 epoll_fork (EV_A);
592 634
593 ev_io_stop (EV_A_ &linuxaio_epoll_w); 635 ev_io_stop (EV_A_ &linuxaio_epoll_w);
594 ev_io_set (EV_A_ &linuxaio_epoll_w, backend_fd, EV_READ); 636 ev_io_set (EV_A_ &linuxaio_epoll_w, backend_fd, EV_READ);
595 ev_io_start (EV_A_ &linuxaio_epoll_w); 637 ev_io_start (EV_A_ &linuxaio_epoll_w);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines