… | |
… | |
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. */ |
… | |
… | |
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 = |
… | |
… | |
322 | linuxaio_epoll_cb (EV_P_ struct ev_io *w, int revents) |
331 | linuxaio_epoll_cb (EV_P_ struct ev_io *w, int revents) |
323 | { |
332 | { |
324 | epoll_poll (EV_A_ 0); |
333 | epoll_poll (EV_A_ 0); |
325 | } |
334 | } |
326 | |
335 | |
327 | static void |
336 | inline_speed |
|
|
337 | void |
328 | linuxaio_fd_rearm (EV_P_ int fd) |
338 | linuxaio_fd_rearm (EV_P_ int fd) |
329 | { |
339 | { |
330 | anfds [fd].events = 0; |
340 | anfds [fd].events = 0; |
331 | linuxaio_iocbps [fd]->io.aio_buf = 0; |
341 | linuxaio_iocbps [fd]->io.aio_buf = 0; |
332 | fd_change (EV_A_ fd, EV_ANFD_REIFY); |
342 | fd_change (EV_A_ fd, EV_ANFD_REIFY); |
… | |
… | |
348 | fd, |
358 | fd, |
349 | (res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
359 | (res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
350 | | (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
360 | | (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
351 | ); |
361 | ); |
352 | |
362 | |
353 | /* 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 */ |
354 | linuxaio_fd_rearm (EV_A_ fd); |
364 | linuxaio_fd_rearm (EV_A_ fd); |
355 | |
365 | |
356 | --nr; |
366 | --nr; |
357 | ++ev; |
367 | ++ev; |
358 | } |
368 | } |
… | |
… | |
371 | unsigned tail = *(volatile unsigned *)&ring->tail; |
381 | unsigned tail = *(volatile unsigned *)&ring->tail; |
372 | |
382 | |
373 | if (head == tail) |
383 | if (head == tail) |
374 | return 0; |
384 | return 0; |
375 | |
385 | |
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 */ |
386 | /* make sure the events up to tail are visible */ |
383 | ECB_MEMORY_FENCE_ACQUIRE; |
387 | ECB_MEMORY_FENCE_ACQUIRE; |
384 | |
388 | |
385 | /* parse all available events, but only once, to avoid starvation */ |
389 | /* parse all available events, but only once, to avoid starvation */ |
386 | if (tail > head) /* normal case around */ |
390 | if (tail > head) /* normal case around */ |
… | |
… | |
396 | *(volatile unsigned *)&ring->head = tail; |
400 | *(volatile unsigned *)&ring->head = tail; |
397 | |
401 | |
398 | return 1; |
402 | return 1; |
399 | } |
403 | } |
400 | |
404 | |
|
|
405 | inline_size |
|
|
406 | int |
|
|
407 | linuxaio_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 | |
401 | /* read at least one event from kernel, or timeout */ |
416 | /* read at least one event from kernel, or timeout */ |
402 | inline_size |
417 | inline_size |
403 | void |
418 | void |
404 | linuxaio_get_events (EV_P_ ev_tstamp timeout) |
419 | linuxaio_get_events (EV_P_ ev_tstamp timeout) |
405 | { |
420 | { |
406 | struct timespec ts; |
421 | struct timespec ts; |
407 | struct io_event ioev[1]; |
422 | struct io_event ioev[8]; /* 256 octet stack space */ |
408 | int res; |
423 | int want = 1; /* how many events to request */ |
|
|
424 | int ringbuf_valid = linuxaio_ringbuf_valid (EV_A); |
409 | |
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 */ |
410 | if (linuxaio_get_events_from_ring (EV_A)) |
429 | if (linuxaio_get_events_from_ring (EV_A)) |
411 | return; |
430 | return; |
412 | |
431 | |
413 | /* 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 */ |
414 | /* this degrades to one event per loop iteration */ |
433 | if (!timeout) |
415 | /* 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]); |
416 | |
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 | |
417 | EV_RELEASE_CB; |
447 | EV_RELEASE_CB; |
418 | |
448 | |
419 | ts.tv_sec = (long)timeout; |
449 | ts.tv_sec = (long)timeout; |
420 | ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9); |
450 | ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9); |
421 | |
451 | |
422 | 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); |
423 | |
453 | |
424 | EV_ACQUIRE_CB; |
454 | EV_ACQUIRE_CB; |
425 | |
455 | |
426 | if (res < 0) |
456 | if (res < 0) |
427 | if (errno == EINTR) |
457 | if (errno == EINTR) |
428 | /* ignored */; |
458 | /* ignored, retry */; |
429 | else |
459 | else |
430 | ev_syserr ("(libev) linuxaio io_getevents"); |
460 | ev_syserr ("(libev) linuxaio io_getevents"); |
431 | else if (res) |
461 | else if (res) |
432 | { |
462 | { |
433 | /* at least one event available, handle it and any remaining ones in the ring buffer */ |
463 | /* at least one event available, handle them */ |
434 | 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 */ |
435 | 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 */ |
436 | } |
482 | } |
437 | } |
483 | } |
438 | |
484 | |
439 | inline_size |
485 | inline_size |
440 | int |
486 | int |
… | |
… | |
516 | assert (("libev: event loop rejected bad fd", errno != EBADF)); |
562 | assert (("libev: event loop rejected bad fd", errno != EBADF)); |
517 | fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes); |
563 | fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes); |
518 | |
564 | |
519 | res = 1; /* skip this iocb */ |
565 | res = 1; /* skip this iocb */ |
520 | } |
566 | } |
|
|
567 | else if (errno == EINTR) /* not seen in reality, not documented */ |
|
|
568 | res = 0; /* silently ignore and retry */ |
521 | else |
569 | else |
522 | ev_syserr ("(libev) linuxaio io_submit"); |
570 | ev_syserr ("(libev) linuxaio io_submit"); |
523 | |
571 | |
524 | submitted += res; |
572 | submitted += res; |
525 | } |
573 | } |