src/os/unix/ngx_file_aio_read.c - nginx source code

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_event.h>


  8. /*
  9. * FreeBSD file AIO features and quirks:
  10. *
  11. *    if an asked data are already in VM cache, then aio_error() returns 0,
  12. *    and the data are already copied in buffer;
  13. *
  14. *    aio_read() preread in VM cache as minimum 16K (probably BKVASIZE);
  15. *    the first AIO preload may be up to 128K;
  16. *
  17. *    aio_read/aio_error() may return EINPROGRESS for just written data;
  18. *
  19. *    kqueue EVFILT_AIO filter is level triggered only: an event repeats
  20. *    until aio_return() will be called;
  21. *
  22. *    aio_cancel() cannot cancel file AIO: it returns AIO_NOTCANCELED always.
  23. */


  24. extern int  ngx_kqueue;


  25. static ssize_t ngx_file_aio_result(ngx_file_t *file, ngx_event_aio_t *aio,
  26.     ngx_event_t *ev);
  27. static void ngx_file_aio_event_handler(ngx_event_t *ev);


  28. ngx_int_t
  29. ngx_file_aio_init(ngx_file_t *file, ngx_pool_t *pool)
  30. {
  31.     ngx_event_aio_t  *aio;

  32.     aio = ngx_pcalloc(pool, sizeof(ngx_event_aio_t));
  33.     if (aio == NULL) {
  34.         return NGX_ERROR;
  35.     }

  36.     aio->file = file;
  37.     aio->fd = file->fd;
  38.     aio->event.data = aio;
  39.     aio->event.ready = 1;
  40.     aio->event.log = file->log;

  41.     file->aio = aio;

  42.     return NGX_OK;
  43. }


  44. ssize_t
  45. ngx_file_aio_read(ngx_file_t *file, u_char *buf, size_t size, off_t offset,
  46.     ngx_pool_t *pool)
  47. {
  48.     int               n;
  49.     ngx_event_t      *ev;
  50.     ngx_event_aio_t  *aio;

  51.     if (!ngx_file_aio) {
  52.         return ngx_read_file(file, buf, size, offset);
  53.     }

  54.     if (file->aio == NULL && ngx_file_aio_init(file, pool) != NGX_OK) {
  55.         return NGX_ERROR;
  56.     }

  57.     aio = file->aio;
  58.     ev = &aio->event;

  59.     if (!ev->ready) {
  60.         ngx_log_error(NGX_LOG_ALERT, file->log, 0,
  61.                       "second aio post for \"%V\"", &file->name);
  62.         return NGX_AGAIN;
  63.     }

  64.     ngx_log_debug4(NGX_LOG_DEBUG_CORE, file->log, 0,
  65.                    "aio complete:%d @%O:%uz %V",
  66.                    ev->complete, offset, size, &file->name);

  67.     if (ev->complete) {
  68.         ev->complete = 0;
  69.         ngx_set_errno(aio->err);

  70.         if (aio->err == 0) {
  71.             return aio->nbytes;
  72.         }

  73.         ngx_log_error(NGX_LOG_CRIT, file->log, ngx_errno,
  74.                       "aio read \"%s\" failed", file->name.data);

  75.         return NGX_ERROR;
  76.     }

  77.     ngx_memzero(&aio->aiocb, sizeof(struct aiocb));

  78.     aio->aiocb.aio_fildes = file->fd;
  79.     aio->aiocb.aio_offset = offset;
  80.     aio->aiocb.aio_buf = buf;
  81.     aio->aiocb.aio_nbytes = size;
  82. #if (NGX_HAVE_KQUEUE)
  83.     aio->aiocb.aio_sigevent.sigev_notify_kqueue = ngx_kqueue;
  84.     aio->aiocb.aio_sigevent.sigev_notify = SIGEV_KEVENT;
  85.     aio->aiocb.aio_sigevent.sigev_value.sival_ptr = ev;
  86. #endif
  87.     ev->handler = ngx_file_aio_event_handler;

  88.     n = aio_read(&aio->aiocb);

  89.     if (n == -1) {
  90.         n = ngx_errno;

  91.         if (n == NGX_EAGAIN) {
  92.             return ngx_read_file(file, buf, size, offset);
  93.         }

  94.         ngx_log_error(NGX_LOG_CRIT, file->log, n,
  95.                       "aio_read(\"%V\") failed", &file->name);

  96.         if (n == NGX_ENOSYS) {
  97.             ngx_file_aio = 0;
  98.             return ngx_read_file(file, buf, size, offset);
  99.         }

  100.         return NGX_ERROR;
  101.     }

  102.     ngx_log_debug2(NGX_LOG_DEBUG_CORE, file->log, 0,
  103.                    "aio_read: fd:%d %d", file->fd, n);

  104.     ev->active = 1;
  105.     ev->ready = 0;
  106.     ev->complete = 0;

  107.     return ngx_file_aio_result(aio->file, aio, ev);
  108. }


  109. static ssize_t
  110. ngx_file_aio_result(ngx_file_t *file, ngx_event_aio_t *aio, ngx_event_t *ev)
  111. {
  112.     int        n;
  113.     ngx_err_t  err;

  114.     n = aio_error(&aio->aiocb);

  115.     ngx_log_debug2(NGX_LOG_DEBUG_CORE, file->log, 0,
  116.                    "aio_error: fd:%d %d", file->fd, n);

  117.     if (n == -1) {
  118.         err = ngx_errno;
  119.         aio->err = err;

  120.         ngx_log_error(NGX_LOG_ALERT, file->log, err,
  121.                       "aio_error(\"%V\") failed", &file->name);
  122.         return NGX_ERROR;
  123.     }

  124.     if (n == NGX_EINPROGRESS) {
  125.         if (ev->ready) {
  126.             ev->ready = 0;
  127.             ngx_log_error(NGX_LOG_ALERT, file->log, n,
  128.                           "aio_read(\"%V\") still in progress",
  129.                           &file->name);
  130.         }

  131.         return NGX_AGAIN;
  132.     }

  133.     n = aio_return(&aio->aiocb);

  134.     if (n == -1) {
  135.         err = ngx_errno;
  136.         aio->err = err;
  137.         ev->ready = 1;

  138.         ngx_log_error(NGX_LOG_CRIT, file->log, err,
  139.                       "aio_return(\"%V\") failed", &file->name);
  140.         return NGX_ERROR;
  141.     }

  142.     aio->err = 0;
  143.     aio->nbytes = n;
  144.     ev->ready = 1;
  145.     ev->active = 0;

  146.     ngx_log_debug2(NGX_LOG_DEBUG_CORE, file->log, 0,
  147.                    "aio_return: fd:%d %d", file->fd, n);

  148.     return n;
  149. }


  150. static void
  151. ngx_file_aio_event_handler(ngx_event_t *ev)
  152. {
  153.     ngx_event_aio_t  *aio;

  154.     aio = ev->data;

  155.     ngx_log_debug2(NGX_LOG_DEBUG_CORE, ev->log, 0,
  156.                    "aio event handler fd:%d %V", aio->fd, &aio->file->name);

  157.     if (ngx_file_aio_result(aio->file, aio, ev) != NGX_AGAIN) {
  158.         aio->handler(ev);
  159.     }
  160. }