src/os/unix/ngx_linux_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. extern int            ngx_eventfd;
  9. extern aio_context_t  ngx_aio_ctx;


  10. static void ngx_file_aio_event_handler(ngx_event_t *ev);


  11. static int
  12. io_submit(aio_context_t ctx, long n, struct iocb **paiocb)
  13. {
  14.     return syscall(SYS_io_submit, ctx, n, paiocb);
  15. }


  16. ssize_t
  17. ngx_file_aio_read(ngx_file_t *file, u_char *buf, size_t size, off_t offset,
  18.     ngx_pool_t *pool)
  19. {
  20.     ngx_err_t         err;
  21.     struct iocb      *piocb[1];
  22.     ngx_event_t      *ev;
  23.     ngx_event_aio_t  *aio;

  24.     if (!ngx_file_aio) {
  25.         return ngx_read_file(file, buf, size, offset);
  26.     }

  27.     aio = file->aio;

  28.     if (aio == NULL) {
  29.         aio = ngx_pcalloc(pool, sizeof(ngx_event_aio_t));
  30.         if (aio == NULL) {
  31.             return NGX_ERROR;
  32.         }

  33.         aio->file = file;
  34.         aio->fd = file->fd;
  35.         aio->event.data = aio;
  36.         aio->event.ready = 1;
  37.         aio->event.log = file->log;
  38.         file->aio = aio;
  39.     }

  40.     ev = &aio->event;

  41.     if (!ev->ready) {
  42.         ngx_log_error(NGX_LOG_ALERT, file->log, 0,
  43.                       "second aio post for \"%V\"", &file->name);
  44.         return NGX_AGAIN;
  45.     }

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

  49.     if (ev->complete) {
  50.         ev->active = 0;
  51.         ev->complete = 0;

  52.         if (aio->res >= 0) {
  53.             ngx_set_errno(0);
  54.             return aio->res;
  55.         }

  56.         ngx_set_errno(-aio->res);

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

  59.         return NGX_ERROR;
  60.     }

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

  62.     aio->aiocb.aio_data = (uint64_t) (uintptr_t) ev;
  63.     aio->aiocb.aio_lio_opcode = IOCB_CMD_PREAD;
  64.     aio->aiocb.aio_fildes = file->fd;
  65.     aio->aiocb.aio_buf = (uint64_t) (uintptr_t) buf;
  66.     aio->aiocb.aio_nbytes = size;
  67.     aio->aiocb.aio_offset = offset;
  68.     aio->aiocb.aio_flags = IOCB_FLAG_RESFD;
  69.     aio->aiocb.aio_resfd = ngx_eventfd;

  70.     ev->handler = ngx_file_aio_event_handler;

  71.     piocb[0] = &aio->aiocb;

  72.     if (io_submit(ngx_aio_ctx, 1, piocb) == 1) {
  73.         ev->active = 1;
  74.         ev->ready = 0;
  75.         ev->complete = 0;

  76.         return NGX_AGAIN;
  77.     }

  78.     err = ngx_errno;

  79.     if (err == NGX_EAGAIN) {
  80.         return ngx_read_file(file, buf, size, offset);
  81.     }

  82.     ngx_log_error(NGX_LOG_CRIT, file->log, err,
  83.                   "io_submit(\"%V\") failed", &file->name);

  84.     if (err == NGX_ENOSYS) {
  85.         ngx_file_aio = 0;
  86.         return ngx_read_file(file, buf, size, offset);
  87.     }

  88.     return NGX_ERROR;
  89. }


  90. static void
  91. ngx_file_aio_event_handler(ngx_event_t *ev)
  92. {
  93.     ngx_event_aio_t  *aio;

  94.     aio = ev->data;

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

  97.     aio->handler(ev);
  98. }