Mercurial > hg > nginx-quic
annotate src/os/unix/ngx_file_aio_read.c @ 4016:b044d6553d52
Move SO_ACCEPTFILTER and TCP_DEFER_ACCEPT checks into configure.
NetBSD 5.0+ has SO_ACCEPTFILTER support merged from FreeBSD, and having
accept filter check in FreeBSD-specific ngx_freebsd_config.h prevents it
from being used on NetBSD. Therefore move the check into configure (and
do the same for Linux-specific TCP_DEFER_ACCEPT, just to be in line).
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Sun, 21 Aug 2011 11:37:37 +0000 |
parents | 04cfc09b8b8d |
children | 37da005a5808 |
rev | line source |
---|---|
3052 | 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 /* | |
13 * FreeBSD file AIO features and quirks: | |
14 * | |
15 * if an asked data are already in VM cache, then aio_error() returns 0, | |
16 * and the data are already copied in buffer; | |
17 * | |
3065 | 18 * aio_read() preread in VM cache as minimum 16K (probably BKVASIZE); |
19 * the first AIO preload may be up to 128K; | |
3052 | 20 * |
21 * aio_read/aio_error() may return EINPROGRESS for just written data; | |
22 * | |
23 * kqueue EVFILT_AIO filter is level triggered only: an event repeats | |
24 * until aio_return() will be called; | |
25 * | |
26 * aio_cancel() can not cancel file AIO: it returns AIO_NOTCANCELED always. | |
27 */ | |
28 | |
29 | |
30 extern int ngx_kqueue; | |
31 | |
32 | |
33 static ssize_t ngx_file_aio_result(ngx_file_t *file, ngx_event_aio_t *aio, | |
34 ngx_event_t *ev); | |
35 static void ngx_file_aio_event_handler(ngx_event_t *ev); | |
36 | |
37 | |
38 ssize_t | |
39 ngx_file_aio_read(ngx_file_t *file, u_char *buf, size_t size, off_t offset, | |
40 ngx_pool_t *pool) | |
41 { | |
3294
04cfc09b8b8d
export aio presence knowledge to prevent using "aio sendfile",
Igor Sysoev <igor@sysoev.ru>
parents:
3065
diff
changeset
|
42 int n; |
04cfc09b8b8d
export aio presence knowledge to prevent using "aio sendfile",
Igor Sysoev <igor@sysoev.ru>
parents:
3065
diff
changeset
|
43 ngx_event_t *ev; |
04cfc09b8b8d
export aio presence knowledge to prevent using "aio sendfile",
Igor Sysoev <igor@sysoev.ru>
parents:
3065
diff
changeset
|
44 ngx_event_aio_t *aio; |
3052 | 45 |
3294
04cfc09b8b8d
export aio presence knowledge to prevent using "aio sendfile",
Igor Sysoev <igor@sysoev.ru>
parents:
3065
diff
changeset
|
46 if (!ngx_file_aio) { |
3052 | 47 return ngx_read_file(file, buf, size, offset); |
48 } | |
49 | |
50 aio = file->aio; | |
51 | |
52 if (aio == NULL) { | |
53 aio = ngx_pcalloc(pool, sizeof(ngx_event_aio_t)); | |
54 if (aio == NULL) { | |
55 return NGX_ERROR; | |
56 } | |
57 | |
58 aio->file = file; | |
59 aio->fd = file->fd; | |
60 aio->event.data = aio; | |
61 aio->event.ready = 1; | |
62 aio->event.log = file->log; | |
3065 | 63 #if (NGX_HAVE_AIO_SENDFILE) |
64 aio->last_offset = -1; | |
65 #endif | |
3052 | 66 file->aio = aio; |
67 } | |
68 | |
69 ev = &aio->event; | |
70 | |
71 if (!ev->ready) { | |
72 ngx_log_error(NGX_LOG_ALERT, file->log, 0, | |
73 "second aio post for \"%V\"", &file->name); | |
74 return NGX_AGAIN; | |
75 } | |
76 | |
77 ngx_log_debug4(NGX_LOG_DEBUG_CORE, file->log, 0, | |
78 "aio complete:%d @%O:%z %V", | |
79 ev->complete, offset, size, &file->name); | |
80 | |
81 if (ev->complete) { | |
82 ev->complete = 0; | |
83 ngx_set_errno(aio->err); | |
84 | |
85 if (aio->err == 0) { | |
86 return aio->nbytes; | |
87 } | |
88 | |
89 return NGX_ERROR; | |
90 } | |
91 | |
92 ngx_memzero(&aio->aiocb, sizeof(struct aiocb)); | |
93 | |
94 aio->aiocb.aio_fildes = file->fd; | |
95 aio->aiocb.aio_offset = offset; | |
96 aio->aiocb.aio_buf = buf; | |
97 aio->aiocb.aio_nbytes = size; | |
98 #if (NGX_HAVE_KQUEUE) | |
99 aio->aiocb.aio_sigevent.sigev_notify_kqueue = ngx_kqueue; | |
100 aio->aiocb.aio_sigevent.sigev_notify = SIGEV_KEVENT; | |
101 aio->aiocb.aio_sigevent.sigev_value.sigval_ptr = ev; | |
102 #endif | |
103 ev->handler = ngx_file_aio_event_handler; | |
104 | |
105 n = aio_read(&aio->aiocb); | |
106 | |
107 if (n == -1) { | |
108 n = ngx_errno; | |
109 | |
110 if (n == NGX_EAGAIN) { | |
111 return ngx_read_file(file, buf, size, offset); | |
112 } | |
113 | |
114 ngx_log_error(NGX_LOG_CRIT, file->log, n, | |
115 "aio_read(\"%V\") failed", &file->name); | |
116 | |
117 if (n == NGX_ENOSYS) { | |
3294
04cfc09b8b8d
export aio presence knowledge to prevent using "aio sendfile",
Igor Sysoev <igor@sysoev.ru>
parents:
3065
diff
changeset
|
118 ngx_file_aio = 0; |
3052 | 119 return ngx_read_file(file, buf, size, offset); |
120 } | |
121 | |
122 return NGX_ERROR; | |
123 } | |
124 | |
125 ngx_log_debug2(NGX_LOG_DEBUG_CORE, file->log, 0, | |
126 "aio_read: fd:%d %d", file->fd, n); | |
127 | |
128 ev->active = 1; | |
129 ev->ready = 0; | |
130 ev->complete = 0; | |
131 | |
132 return ngx_file_aio_result(aio->file, aio, ev); | |
133 } | |
134 | |
135 | |
136 static ssize_t | |
137 ngx_file_aio_result(ngx_file_t *file, ngx_event_aio_t *aio, ngx_event_t *ev) | |
138 { | |
139 int n; | |
140 ngx_err_t err; | |
141 | |
142 n = aio_error(&aio->aiocb); | |
143 | |
144 ngx_log_debug2(NGX_LOG_DEBUG_CORE, file->log, 0, | |
145 "aio_error: fd:%d %d", file->fd, n); | |
146 | |
147 if (n == -1) { | |
148 err = ngx_errno; | |
149 aio->err = err; | |
150 | |
151 ngx_log_error(NGX_LOG_ALERT, file->log, err, | |
152 "aio_error(\"%V\") failed", &file->name); | |
153 return NGX_ERROR; | |
154 } | |
155 | |
156 if (n != 0) { | |
157 if (n == NGX_EINPROGRESS) { | |
158 if (ev->ready) { | |
159 ev->ready = 0; | |
160 ngx_log_error(NGX_LOG_ALERT, file->log, n, | |
161 "aio_read(\"%V\") still in progress", | |
162 &file->name); | |
163 } | |
164 | |
165 return NGX_AGAIN; | |
166 } | |
167 | |
168 aio->err = n; | |
169 ev->ready = 0; | |
170 | |
171 ngx_log_error(NGX_LOG_CRIT, file->log, n, | |
172 "aio_read(\"%V\") failed", &file->name); | |
173 return NGX_ERROR; | |
174 } | |
175 | |
176 n = aio_return(&aio->aiocb); | |
177 | |
178 if (n == -1) { | |
179 err = ngx_errno; | |
180 aio->err = err; | |
181 ev->ready = 0; | |
182 | |
183 ngx_log_error(NGX_LOG_ALERT, file->log, err, | |
184 "aio_return(\"%V\") failed", &file->name); | |
185 return NGX_ERROR; | |
186 } | |
187 | |
188 aio->err = 0; | |
189 aio->nbytes = n; | |
190 ev->ready = 1; | |
191 ev->active = 0; | |
192 | |
193 ngx_log_debug2(NGX_LOG_DEBUG_CORE, file->log, 0, | |
194 "aio_return: fd:%d %d", file->fd, n); | |
195 | |
196 return n; | |
197 } | |
198 | |
199 | |
200 static void | |
201 ngx_file_aio_event_handler(ngx_event_t *ev) | |
202 { | |
203 ngx_event_aio_t *aio; | |
204 | |
205 aio = ev->data; | |
206 | |
207 ngx_log_debug2(NGX_LOG_DEBUG_CORE, ev->log, 0, | |
208 "aio event handler fd:%d %V", aio->fd, &aio->file->name); | |
209 | |
210 if (ngx_file_aio_result(aio->file, aio, ev) != NGX_AGAIN) { | |
211 aio->handler(ev); | |
212 } | |
213 } |