comparison src/event/modules/ngx_epoll_module.c @ 4130:04751572f900

Fixing Linux AIO initiatialization: AIO operations are disabled if kernel does not support them. Previously worker just exited.
author Igor Sysoev <igor@sysoev.ru>
date Fri, 16 Sep 2011 13:41:52 +0000
parents 6903dac6ad19
children a3203c4521c6
comparison
equal deleted inserted replaced
4129:6903dac6ad19 4130:04751572f900
203 struct timespec *tmo) 203 struct timespec *tmo)
204 { 204 {
205 return syscall(SYS_io_getevents, ctx, min_nr, nr, events, tmo); 205 return syscall(SYS_io_getevents, ctx, min_nr, nr, events, tmo);
206 } 206 }
207 207
208
209 static void
210 ngx_epoll_aio_init(ngx_cycle_t *cycle)
211 {
212 int n;
213 struct epoll_event ee;
214
215 ngx_eventfd = syscall(SYS_eventfd, 0);
216
217 if (ngx_eventfd == -1) {
218 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
219 "eventfd() failed");
220 ngx_file_aio = 0;
221 return;
222 }
223
224 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
225 "eventfd: %d", ngx_eventfd);
226
227 n = 1;
228
229 if (ioctl(ngx_eventfd, FIONBIO, &n) == -1) {
230 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
231 "ioctl(eventfd, FIONBIO) failed");
232 goto failed;
233 }
234
235 if (io_setup(1024, &ngx_aio_ctx) == -1) {
236 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
237 "io_setup() failed");
238 goto failed;
239 }
240
241 ngx_eventfd_event.data = &ngx_eventfd_conn;
242 ngx_eventfd_event.handler = ngx_epoll_eventfd_handler;
243 ngx_eventfd_event.log = cycle->log;
244 ngx_eventfd_event.active = 1;
245 ngx_eventfd_conn.fd = ngx_eventfd;
246 ngx_eventfd_conn.read = &ngx_eventfd_event;
247 ngx_eventfd_conn.log = cycle->log;
248
249 ee.events = EPOLLIN|EPOLLET;
250 ee.data.ptr = &ngx_eventfd_conn;
251
252 if (epoll_ctl(ep, EPOLL_CTL_ADD, ngx_eventfd, &ee) != -1) {
253 return;
254 }
255
256 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
257 "epoll_ctl(EPOLL_CTL_ADD, eventfd) failed");
258
259 if (io_destroy(ngx_aio_ctx) == -1) {
260 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
261 "io_destroy() failed");
262 }
263
264 failed:
265
266 if (close(ngx_eventfd) == -1) {
267 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
268 "eventfd close() failed");
269 }
270
271 ngx_eventfd = -1;
272 ngx_aio_ctx = 0;
273 ngx_file_aio = 0;
274 }
275
208 #endif 276 #endif
209 277
210 278
211 static ngx_int_t 279 static ngx_int_t
212 ngx_epoll_init(ngx_cycle_t *cycle, ngx_msec_t timer) 280 ngx_epoll_init(ngx_cycle_t *cycle, ngx_msec_t timer)
223 "epoll_create() failed"); 291 "epoll_create() failed");
224 return NGX_ERROR; 292 return NGX_ERROR;
225 } 293 }
226 294
227 #if (NGX_HAVE_FILE_AIO) 295 #if (NGX_HAVE_FILE_AIO)
228 { 296
229 int n; 297 ngx_epoll_aio_init(cycle);
230 struct epoll_event ee; 298
231
232 ngx_eventfd = syscall(SYS_eventfd, 0);
233
234 if (ngx_eventfd == -1) {
235 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
236 "eventfd() failed");
237 return NGX_ERROR;
238 }
239
240 n = 1;
241
242 if (ioctl(ngx_eventfd, FIONBIO, &n) == -1) {
243 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
244 "ioctl(eventfd, FIONBIO) failed");
245 }
246
247 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
248 "eventfd: %d", ngx_eventfd);
249
250 if (io_setup(1024, &ngx_aio_ctx) == -1) {
251
252 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
253 "io_setup() failed");
254 return NGX_ERROR;
255 }
256
257 ngx_eventfd_event.data = &ngx_eventfd_conn;
258 ngx_eventfd_event.handler = ngx_epoll_eventfd_handler;
259 ngx_eventfd_event.log = cycle->log;
260 ngx_eventfd_event.active = 1;
261 ngx_eventfd_conn.fd = ngx_eventfd;
262 ngx_eventfd_conn.read = &ngx_eventfd_event;
263 ngx_eventfd_conn.log = cycle->log;
264
265 ee.events = EPOLLIN|EPOLLET;
266 ee.data.ptr = &ngx_eventfd_conn;
267
268 if (epoll_ctl(ep, EPOLL_CTL_ADD, ngx_eventfd, &ee) == -1) {
269 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
270 "epoll_ctl(EPOLL_CTL_ADD, eventfd) failed");
271 return NGX_ERROR;
272 }
273 }
274 #endif 299 #endif
275 } 300 }
276 301
277 if (nevents < epcf->events) { 302 if (nevents < epcf->events) {
278 if (event_list) { 303 if (event_list) {
314 339
315 ep = -1; 340 ep = -1;
316 341
317 #if (NGX_HAVE_FILE_AIO) 342 #if (NGX_HAVE_FILE_AIO)
318 343
319 if (io_destroy(ngx_aio_ctx) == -1) { 344 if (ngx_eventfd != -1) {
320 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, 345
321 "io_destroy() failed"); 346 if (io_destroy(ngx_aio_ctx) == -1) {
347 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
348 "io_destroy() failed");
349 }
350
351 if (close(ngx_eventfd) == -1) {
352 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
353 "eventfd close() failed");
354 }
355
356 ngx_eventfd = -1;
322 } 357 }
323 358
324 ngx_aio_ctx = 0; 359 ngx_aio_ctx = 0;
325 360
326 #endif 361 #endif