518
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.h>
|
|
10
|
|
11
|
|
12 extern int ngx_eventfd;
|
|
13 extern aio_context_t ngx_aio_ctx;
|
|
14
|
|
15
|
|
16 static void ngx_file_aio_event_handler(ngx_event_t *ev);
|
|
17
|
|
18
|
640
|
19 static int
|
518
|
20 io_submit(aio_context_t ctx, long n, struct iocb **paiocb)
|
|
21 {
|
|
22 return syscall(SYS_io_submit, ctx, n, paiocb);
|
|
23 }
|
|
24
|
|
25
|
|
26 ssize_t
|
|
27 ngx_file_aio_read(ngx_file_t *file, u_char *buf, size_t size, off_t offset,
|
|
28 ngx_pool_t *pool)
|
|
29 {
|
640
|
30 ngx_err_t err;
|
542
|
31 struct iocb *piocb[1];
|
|
32 ngx_event_t *ev;
|
|
33 ngx_event_aio_t *aio;
|
518
|
34
|
542
|
35 if (!ngx_file_aio) {
|
518
|
36 return ngx_read_file(file, buf, size, offset);
|
|
37 }
|
|
38
|
|
39 aio = file->aio;
|
|
40
|
|
41 if (aio == NULL) {
|
|
42 aio = ngx_pcalloc(pool, sizeof(ngx_event_aio_t));
|
|
43 if (aio == NULL) {
|
|
44 return NGX_ERROR;
|
|
45 }
|
|
46
|
|
47 aio->file = file;
|
|
48 aio->fd = file->fd;
|
|
49 aio->event.data = aio;
|
|
50 aio->event.ready = 1;
|
|
51 aio->event.log = file->log;
|
|
52 file->aio = aio;
|
|
53 }
|
|
54
|
|
55 ev = &aio->event;
|
|
56
|
|
57 if (!ev->ready) {
|
|
58 ngx_log_error(NGX_LOG_ALERT, file->log, 0,
|
|
59 "second aio post for \"%V\"", &file->name);
|
|
60 return NGX_AGAIN;
|
|
61 }
|
|
62
|
|
63 ngx_log_debug4(NGX_LOG_DEBUG_CORE, file->log, 0,
|
|
64 "aio complete:%d @%O:%z %V",
|
|
65 ev->complete, offset, size, &file->name);
|
|
66
|
|
67 if (ev->complete) {
|
|
68 ev->active = 0;
|
|
69 ev->complete = 0;
|
|
70
|
|
71 if (aio->res >= 0) {
|
|
72 ngx_set_errno(0);
|
|
73 return aio->res;
|
|
74 }
|
|
75
|
|
76 ngx_set_errno(-aio->res);
|
638
|
77
|
|
78 ngx_log_error(NGX_LOG_CRIT, file->log, ngx_errno,
|
|
79 "aio read \"%s\" failed", file->name.data);
|
|
80
|
518
|
81 return NGX_ERROR;
|
|
82 }
|
|
83
|
|
84 ngx_memzero(&aio->aiocb, sizeof(struct iocb));
|
|
85
|
|
86 aio->aiocb.aio_data = (uint64_t) (uintptr_t) ev;
|
|
87 aio->aiocb.aio_lio_opcode = IOCB_CMD_PREAD;
|
|
88 aio->aiocb.aio_fildes = file->fd;
|
|
89 aio->aiocb.aio_buf = (uint64_t) (uintptr_t) buf;
|
|
90 aio->aiocb.aio_nbytes = size;
|
|
91 aio->aiocb.aio_offset = offset;
|
|
92 aio->aiocb.aio_flags = IOCB_FLAG_RESFD;
|
|
93 aio->aiocb.aio_resfd = ngx_eventfd;
|
|
94
|
|
95 ev->handler = ngx_file_aio_event_handler;
|
|
96
|
|
97 piocb[0] = &aio->aiocb;
|
|
98
|
640
|
99 if (io_submit(ngx_aio_ctx, 1, piocb) == 1) {
|
602
|
100 ev->active = 1;
|
|
101 ev->ready = 0;
|
|
102 ev->complete = 0;
|
|
103
|
518
|
104 return NGX_AGAIN;
|
|
105 }
|
|
106
|
640
|
107 err = ngx_errno;
|
518
|
108
|
640
|
109 if (err == NGX_EAGAIN) {
|
518
|
110 return ngx_read_file(file, buf, size, offset);
|
|
111 }
|
|
112
|
640
|
113 ngx_log_error(NGX_LOG_CRIT, file->log, err,
|
518
|
114 "io_submit(\"%V\") failed", &file->name);
|
|
115
|
640
|
116 if (err == NGX_ENOSYS) {
|
542
|
117 ngx_file_aio = 0;
|
518
|
118 return ngx_read_file(file, buf, size, offset);
|
|
119 }
|
|
120
|
|
121 return NGX_ERROR;
|
|
122 }
|
|
123
|
|
124
|
|
125 static void
|
|
126 ngx_file_aio_event_handler(ngx_event_t *ev)
|
|
127 {
|
|
128 ngx_event_aio_t *aio;
|
|
129
|
|
130 aio = ev->data;
|
|
131
|
|
132 ngx_log_debug2(NGX_LOG_DEBUG_CORE, ev->log, 0,
|
|
133 "aio event handler fd:%d %V", aio->fd, &aio->file->name);
|
|
134
|
|
135 aio->handler(ev);
|
|
136 }
|