src/os/unix/ngx_file_aio_read.c - nginx-1.7.10

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. ssize_t
  29. ngx_file_aio_read(ngx_file_t *file, u_char *buf, size_t size, off_t offset,
  30.     ngx_pool_t *pool)
  31. {
  32.     int               n;
  33.     ngx_event_t      *ev;
  34.     ngx_event_aio_t  *aio;

  35.     if (!ngx_file_aio) {
  36.         return ngx_read_file(file, buf, size, offset);
  37.     }

  38.     aio = file->aio;

  39.     if (aio == NULL) {
  40.         aio = ngx_pcalloc(pool, sizeof(ngx_event_aio_t));
  41.         if (aio == NULL) {
  42.             return NGX_ERROR;
  43.         }

  44.         aio->file = file;
  45.         aio->fd = file->fd;
  46.         aio->event.data = aio;
  47.         aio->event.ready = 1;
  48.         aio->event.log = file->log;
  49. #if (NGX_HAVE_AIO_SENDFILE)
  50.         aio->last_offset = -1;
  51. #endif
  52.         file->aio = aio;
  53.     }

  54.     ev = &aio->event;

  55.     if (!ev->ready) {
  56.         ngx_log_error(NGX_LOG_ALERT, file->log, 0,
  57.                       "second aio post for \"%V\"", &file->name);
  58.         return NGX_AGAIN;
  59.     }

  60.     ngx_log_debug4(NGX_LOG_DEBUG_CORE, file->log, 0,
  61.                    "aio complete:%d @%O:%z %V",
  62.                    ev->complete, offset, size, &file->name);

  63.     if (ev->complete) {
  64.         ev->complete = 0;
  65.         ngx_set_errno(aio->err);

  66.         if (aio->err == 0) {
  67.             return aio->nbytes;
  68.         }

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

  71.         return NGX_ERROR;
  72.     }

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

  74.     aio->aiocb.aio_fildes = file->fd;
  75.     aio->aiocb.aio_offset = offset;
  76.     aio->aiocb.aio_buf = buf;
  77.     aio->aiocb.aio_nbytes = size;
  78. #if (NGX_HAVE_KQUEUE)
  79.     aio->aiocb.aio_sigevent.sigev_notify_kqueue = ngx_kqueue;
  80.     aio->aiocb.aio_sigevent.sigev_notify = SIGEV_KEVENT;
  81.     aio->aiocb.aio_sigevent.sigev_value.sigval_ptr = ev;
  82. #endif
  83.     ev->handler = ngx_file_aio_event_handler;

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

  85.     if (n == -1) {
  86.         n = ngx_errno;

  87.         if (n == NGX_EAGAIN) {
  88.             return ngx_read_file(file, buf, size, offset);
  89.         }

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

  92.         if (n == NGX_ENOSYS) {
  93.             ngx_file_aio = 0;
  94.             return ngx_read_file(file, buf, size, offset);
  95.         }

  96.         return NGX_ERROR;
  97.     }

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

  100.     ev->active = 1;
  101.     ev->ready = 0;
  102.     ev->complete = 0;

  103.     return ngx_file_aio_result(aio->file, aio, ev);
  104. }


  105. static ssize_t
  106. ngx_file_aio_result(ngx_file_t *file, ngx_event_aio_t *aio, ngx_event_t *ev)
  107. {
  108.     int        n;
  109.     ngx_err_t  err;

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

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

  113.     if (n == -1) {
  114.         err = ngx_errno;
  115.         aio->err = err;

  116.         ngx_log_error(NGX_LOG_ALERT, file->log, err,
  117.                       "aio_error(\"%V\") failed", &file->name);
  118.         return NGX_ERROR;
  119.     }

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

  127.         return NGX_AGAIN;
  128.     }

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

  130.     if (n == -1) {
  131.         err = ngx_errno;
  132.         aio->err = err;
  133.         ev->ready = 1;

  134.         ngx_log_error(NGX_LOG_CRIT, file->log, err,
  135.                       "aio_return(\"%V\") failed", &file->name);
  136.         return NGX_ERROR;
  137.     }

  138.     aio->err = 0;
  139.     aio->nbytes = n;
  140.     ev->ready = 1;
  141.     ev->active = 0;

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

  144.     return n;
  145. }


  146. static void
  147. ngx_file_aio_event_handler(ngx_event_t *ev)
  148. {
  149.     ngx_event_aio_t  *aio;

  150.     aio = ev->data;

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

  153.     if (ngx_file_aio_result(aio->file, aio, ev) != NGX_AGAIN) {
  154.         aio->handler(ev);
  155.     }
  156. }