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.34 by root, Wed Jun 26 00:01:46 2019 UTC vs.
Revision 1.41 by root, Mon Jul 1 20:41:46 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. */
286linuxaio_modify (EV_P_ int fd, int oev, int nev) 286linuxaio_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 (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 (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 (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 (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;
322linuxaio_epoll_cb (EV_P_ struct ev_io *w, int revents) 332linuxaio_epoll_cb (EV_P_ struct ev_io *w, int revents)
323{ 333{
324 epoll_poll (EV_A_ 0); 334 epoll_poll (EV_A_ 0);
325} 335}
326 336
327static void 337inline_speed
338void
328linuxaio_fd_rearm (EV_P_ int fd) 339linuxaio_fd_rearm (EV_P_ int fd)
329{ 340{
330 anfds [fd].events = 0; 341 anfds [fd].events = 0;
331 linuxaio_iocbps [fd]->io.aio_buf = 0; 342 linuxaio_iocbps [fd]->io.aio_buf = 0;
332 fd_change (EV_A_ fd, EV_ANFD_REIFY); 343 fd_change (EV_A_ fd, EV_ANFD_REIFY);
348 fd, 359 fd,
349 (res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) 360 (res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
350 | (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) 361 | (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
351 ); 362 );
352 363
353 /* 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 */
354 linuxaio_fd_rearm (EV_A_ fd); 365 linuxaio_fd_rearm (EV_A_ fd);
355 366
356 --nr; 367 --nr;
357 ++ev; 368 ++ev;
358 } 369 }
371 unsigned tail = *(volatile unsigned *)&ring->tail; 382 unsigned tail = *(volatile unsigned *)&ring->tail;
372 383
373 if (head == tail) 384 if (head == tail)
374 return 0; 385 return 0;
375 386
376 /* bail out if the ring buffer doesn't match the expected layout */
377 if (expect_false (ring->magic != AIO_RING_MAGIC)
378 || ring->incompat_features != AIO_RING_INCOMPAT_FEATURES
379 || ring->header_length != sizeof (struct aio_ring)) /* TODO: or use it to find io_event[0]? */
380 return 0;
381
382 /* make sure the events up to tail are visible */ 387 /* make sure the events up to tail are visible */
383 ECB_MEMORY_FENCE_ACQUIRE; 388 ECB_MEMORY_FENCE_ACQUIRE;
384 389
385 /* parse all available events, but only once, to avoid starvation */ 390 /* parse all available events, but only once, to avoid starvation */
386 if (tail > head) /* normal case around */ 391 if (tail > head) /* normal case around */
396 *(volatile unsigned *)&ring->head = tail; 401 *(volatile unsigned *)&ring->head = tail;
397 402
398 return 1; 403 return 1;
399} 404}
400 405
406inline_size
407int
408linuxaio_ringbuf_valid (EV_P)
409{
410 struct aio_ring *ring = (struct aio_ring *)linuxaio_ctx;
411
412 return 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
401/* read at least one event from kernel, or timeout */ 417/* read at least one event from kernel, or timeout */
402inline_size 418inline_size
403void 419void
404linuxaio_get_events (EV_P_ ev_tstamp timeout) 420linuxaio_get_events (EV_P_ ev_tstamp timeout)
405{ 421{
406 struct timespec ts; 422 struct timespec ts;
407 struct io_event ioev[1]; 423 struct io_event ioev[8]; /* 256 octet stack space */
408 int res; 424 int want = 1; /* how many events to request */
425 int ringbuf_valid = linuxaio_ringbuf_valid (EV_A);
409 426
427 if (expect_true (ringbuf_valid))
428 {
429 /* if the ring buffer has any events, we don't wait or call the kernel at all */
410 if (linuxaio_get_events_from_ring (EV_A)) 430 if (linuxaio_get_events_from_ring (EV_A))
411 return; 431 return;
412 432
413 /* 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 */
414 /* this degrades to one event per loop iteration */ 434 if (!timeout)
415 /* 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]);
416 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
417 EV_RELEASE_CB; 448 EV_RELEASE_CB;
418 449
419 ts.tv_sec = (long)timeout; 450 ts.tv_sec = (long)timeout;
420 ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9); 451 ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9);
421 452
422 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);
423 454
424 EV_ACQUIRE_CB; 455 EV_ACQUIRE_CB;
425 456
426 if (res < 0) 457 if (res < 0)
427 if (errno == EINTR) 458 if (errno == EINTR)
428 /* ignored */; 459 /* ignored, retry */;
429 else 460 else
430 ev_syserr ("(libev) linuxaio io_getevents"); 461 ev_syserr ("(libev) linuxaio io_getevents");
431 else if (res) 462 else if (res)
432 { 463 {
433 /* at least one event available, handle it and any remaining ones in the ring buffer */ 464 /* at least one event available, handle them */
434 linuxaio_parse_events (EV_A_ ioev, res); 465 linuxaio_parse_events (EV_A_ ioev, res);
466
467 if (expect_true (ringbuf_valid))
468 {
469 /* if we have a ring buffer, handle any remaining events in it */
435 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 */
436 } 483 }
437} 484}
438 485
439inline_size 486inline_size
440int 487int
516 assert (("libev: event loop rejected bad fd", errno != EBADF)); 563 assert (("libev: event loop rejected bad fd", errno != EBADF));
517 fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes); 564 fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes);
518 565
519 res = 1; /* skip this iocb */ 566 res = 1; /* skip this iocb */
520 } 567 }
568 else if (errno == EINTR) /* not seen in reality, not documented */
569 res = 0; /* silently ignore and retry */
521 else 570 else
522 ev_syserr ("(libev) linuxaio io_submit"); 571 ev_syserr ("(libev) linuxaio io_submit");
523 572
524 submitted += res; 573 submitted += res;
525 } 574 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines