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.39 by root, Mon Jul 1 20:34:31 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 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) == 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 }
303 315
304 if (nev) 316 if (nev)
305 { 317 {
306 iocb->io.aio_buf = 318 iocb->io.aio_buf =
307 (nev & EV_READ ? POLLIN : 0) 319 (nev & EV_READ ? POLLIN : 0)
319linuxaio_epoll_cb (EV_P_ struct ev_io *w, int revents) 331linuxaio_epoll_cb (EV_P_ struct ev_io *w, int revents)
320{ 332{
321 epoll_poll (EV_A_ 0); 333 epoll_poll (EV_A_ 0);
322} 334}
323 335
324static void 336inline_speed
337void
325linuxaio_fd_rearm (EV_P_ int fd) 338linuxaio_fd_rearm (EV_P_ int fd)
326{ 339{
327 anfds [fd].events = 0; 340 anfds [fd].events = 0;
328 linuxaio_iocbps [fd]->io.aio_buf = 0; 341 linuxaio_iocbps [fd]->io.aio_buf = 0;
329 fd_change (EV_A_ fd, EV_ANFD_REIFY); 342 fd_change (EV_A_ fd, EV_ANFD_REIFY);
345 fd, 358 fd,
346 (res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) 359 (res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
347 | (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) 360 | (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
348 ); 361 );
349 362
350 /* 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 */
351 linuxaio_fd_rearm (EV_A_ fd); 364 linuxaio_fd_rearm (EV_A_ fd);
352 365
353 --nr; 366 --nr;
354 ++ev; 367 ++ev;
355 } 368 }
368 unsigned tail = *(volatile unsigned *)&ring->tail; 381 unsigned tail = *(volatile unsigned *)&ring->tail;
369 382
370 if (head == tail) 383 if (head == tail)
371 return 0; 384 return 0;
372 385
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 */ 386 /* make sure the events up to tail are visible */
380 ECB_MEMORY_FENCE_ACQUIRE; 387 ECB_MEMORY_FENCE_ACQUIRE;
381 388
382 /* parse all available events, but only once, to avoid starvation */ 389 /* parse all available events, but only once, to avoid starvation */
383 if (tail > head) /* normal case around */ 390 if (tail > head) /* normal case around */
393 *(volatile unsigned *)&ring->head = tail; 400 *(volatile unsigned *)&ring->head = tail;
394 401
395 return 1; 402 return 1;
396} 403}
397 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
398/* read at least one event from kernel, or timeout */ 416/* read at least one event from kernel, or timeout */
399inline_size 417inline_size
400void 418void
401linuxaio_get_events (EV_P_ ev_tstamp timeout) 419linuxaio_get_events (EV_P_ ev_tstamp timeout)
402{ 420{
403 struct timespec ts; 421 struct timespec ts;
404 struct io_event ioev[1]; 422 struct io_event ioev[8]; /* 256 octet stack space */
405 int res; 423 int want = 1; /* how many events to request */
424 int ringbuf_valid = linuxaio_ringbuf_valid (EV_A);
406 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 */
407 if (linuxaio_get_events_from_ring (EV_A)) 429 if (linuxaio_get_events_from_ring (EV_A))
408 return; 430 return;
409 431
410 /* 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 */
411 /* this degrades to one event per loop iteration */ 433 if (!timeout)
412 /* 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]);
413 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
414 EV_RELEASE_CB; 447 EV_RELEASE_CB;
415 448
416 ts.tv_sec = (long)timeout; 449 ts.tv_sec = (long)timeout;
417 ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9); 450 ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9);
418 451
419 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);
420 453
421 EV_ACQUIRE_CB; 454 EV_ACQUIRE_CB;
422 455
423 if (res < 0) 456 if (res < 0)
424 if (errno == EINTR) 457 if (errno == EINTR)
425 /* ignored */; 458 /* ignored, retry */;
426 else 459 else
427 ev_syserr ("(libev) linuxaio io_getevents"); 460 ev_syserr ("(libev) linuxaio io_getevents");
428 else if (res) 461 else if (res)
429 { 462 {
430 /* at least one event available, handle it and any remaining ones in the ring buffer */ 463 /* at least one event available, handle them */
431 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 */
432 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 */
433 } 482 }
434} 483}
435 484
436inline_size 485inline_size
437int 486int
508 /* it's easiest to handle this mess in another iteration */ 557 /* it's easiest to handle this mess in another iteration */
509 return; 558 return;
510 } 559 }
511 else if (errno == EBADF) 560 else if (errno == EBADF)
512 { 561 {
562 assert (("libev: event loop rejected bad fd", errno != EBADF));
513 fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes); 563 fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes);
514 564
515 res = 1; /* skip this iocb */ 565 res = 1; /* skip this iocb */
516 } 566 }
567 else if (errno == EINTR) /* not seen in reality, not documented */
568 res = 0; /* silently ignore and retry */
517 else 569 else
518 ev_syserr ("(libev) linuxaio io_submit"); 570 ev_syserr ("(libev) linuxaio io_submit");
519 571
520 submitted += res; 572 submitted += res;
521 } 573 }
570void 622void
571linuxaio_destroy (EV_P) 623linuxaio_destroy (EV_P)
572{ 624{
573 epoll_destroy (EV_A); 625 epoll_destroy (EV_A);
574 linuxaio_free_iocbp (EV_A); 626 linuxaio_free_iocbp (EV_A);
575 evsys_io_destroy (linuxaio_ctx); 627 evsys_io_destroy (linuxaio_ctx); /* fails in child, aio context is destroyed */
576} 628}
577 629
578inline_size 630inline_size
579void 631void
580linuxaio_fork (EV_P) 632linuxaio_fork (EV_P)
586 linuxaio_iteration = 0; /* we start over in the child */ 638 linuxaio_iteration = 0; /* we start over in the child */
587 639
588 while (linuxaio_io_setup (EV_A) < 0) 640 while (linuxaio_io_setup (EV_A) < 0)
589 ev_syserr ("(libev) linuxaio io_setup"); 641 ev_syserr ("(libev) linuxaio io_setup");
590 642
643 /* forking epoll should also effectively unregister all fds from the backend */
591 epoll_fork (EV_A); 644 epoll_fork (EV_A);
592 645
593 ev_io_stop (EV_A_ &linuxaio_epoll_w); 646 ev_io_stop (EV_A_ &linuxaio_epoll_w);
594 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);
595 ev_io_start (EV_A_ &linuxaio_epoll_w); 648 ev_io_start (EV_A_ &linuxaio_epoll_w);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines