Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.15.12 ]​[ nginx-1.16.0 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 #include <ngx_event.h>
0011 
0012 
0013 extern int            ngx_eventfd;
0014 extern aio_context_t  ngx_aio_ctx;
0015 
0016 
0017 static void ngx_file_aio_event_handler(ngx_event_t *ev);
0018 
0019 
0020 static int
0021 io_submit(aio_context_t ctx, long n, struct iocb **paiocb)
0022 {
0023     return syscall(SYS_io_submit, ctx, n, paiocb);
0024 }
0025 
0026 
0027 ngx_int_t
0028 ngx_file_aio_init(ngx_file_t *file, ngx_pool_t *pool)
0029 {
0030     ngx_event_aio_t  *aio;
0031 
0032     aio = ngx_pcalloc(pool, sizeof(ngx_event_aio_t));
0033     if (aio == NULL) {
0034         return NGX_ERROR;
0035     }
0036 
0037     aio->file = file;
0038     aio->fd = file->fd;
0039     aio->event.data = aio;
0040     aio->event.ready = 1;
0041     aio->event.log = file->log;
0042 
0043     file->aio = aio;
0044 
0045     return NGX_OK;
0046 }
0047 
0048 
0049 ssize_t
0050 ngx_file_aio_read(ngx_file_t *file, u_char *buf, size_t size, off_t offset,
0051     ngx_pool_t *pool)
0052 {
0053     ngx_err_t         err;
0054     struct iocb      *piocb[1];
0055     ngx_event_t      *ev;
0056     ngx_event_aio_t  *aio;
0057 
0058     if (!ngx_file_aio) {
0059         return ngx_read_file(file, buf, size, offset);
0060     }
0061 
0062     if (file->aio == NULL && ngx_file_aio_init(file, pool) != NGX_OK) {
0063         return NGX_ERROR;
0064     }
0065 
0066     aio = file->aio;
0067     ev = &aio->event;
0068 
0069     if (!ev->ready) {
0070         ngx_log_error(NGX_LOG_ALERT, file->log, 0,
0071                       "second aio post for \"%V\"", &file->name);
0072         return NGX_AGAIN;
0073     }
0074 
0075     ngx_log_debug4(NGX_LOG_DEBUG_CORE, file->log, 0,
0076                    "aio complete:%d @%O:%uz %V",
0077                    ev->complete, offset, size, &file->name);
0078 
0079     if (ev->complete) {
0080         ev->active = 0;
0081         ev->complete = 0;
0082 
0083         if (aio->res >= 0) {
0084             ngx_set_errno(0);
0085             return aio->res;
0086         }
0087 
0088         ngx_set_errno(-aio->res);
0089 
0090         ngx_log_error(NGX_LOG_CRIT, file->log, ngx_errno,
0091                       "aio read \"%s\" failed", file->name.data);
0092 
0093         return NGX_ERROR;
0094     }
0095 
0096     ngx_memzero(&aio->aiocb, sizeof(struct iocb));
0097 
0098     aio->aiocb.aio_data = (uint64_t) (uintptr_t) ev;
0099     aio->aiocb.aio_lio_opcode = IOCB_CMD_PREAD;
0100     aio->aiocb.aio_fildes = file->fd;
0101     aio->aiocb.aio_buf = (uint64_t) (uintptr_t) buf;
0102     aio->aiocb.aio_nbytes = size;
0103     aio->aiocb.aio_offset = offset;
0104     aio->aiocb.aio_flags = IOCB_FLAG_RESFD;
0105     aio->aiocb.aio_resfd = ngx_eventfd;
0106 
0107     ev->handler = ngx_file_aio_event_handler;
0108 
0109     piocb[0] = &aio->aiocb;
0110 
0111     if (io_submit(ngx_aio_ctx, 1, piocb) == 1) {
0112         ev->active = 1;
0113         ev->ready = 0;
0114         ev->complete = 0;
0115 
0116         return NGX_AGAIN;
0117     }
0118 
0119     err = ngx_errno;
0120 
0121     if (err == NGX_EAGAIN) {
0122         return ngx_read_file(file, buf, size, offset);
0123     }
0124 
0125     ngx_log_error(NGX_LOG_CRIT, file->log, err,
0126                   "io_submit(\"%V\") failed", &file->name);
0127 
0128     if (err == NGX_ENOSYS) {
0129         ngx_file_aio = 0;
0130         return ngx_read_file(file, buf, size, offset);
0131     }
0132 
0133     return NGX_ERROR;
0134 }
0135 
0136 
0137 static void
0138 ngx_file_aio_event_handler(ngx_event_t *ev)
0139 {
0140     ngx_event_aio_t  *aio;
0141 
0142     aio = ev->data;
0143 
0144     ngx_log_debug2(NGX_LOG_DEBUG_CORE, ev->log, 0,
0145                    "aio event handler fd:%d %V", aio->fd, &aio->file->name);
0146 
0147     aio->handler(ev);
0148 }