comparison src/core/ngx_output_chain.c @ 7985:ec2e6893caaa

Simplified sendfile(SF_NODISKIO) usage. Starting with FreeBSD 11, there is no need to use AIO operations to preload data into cache for sendfile(SF_NODISKIO) to work. Instead, sendfile() handles non-blocking loading data from disk by itself. It still can, however, return EBUSY if a page is already being loaded (for example, by a different process). If this happens, we now post an event for the next event loop iteration, so sendfile() is retried "after a short period", as manpage recommends. The limit of the number of EBUSY tolerated without any progress is preserved, but now it does not result in an alert, since on an idle system event loop iteration might be very short and EBUSY can happen many times in a row. Instead, SF_NODISKIO is simply disabled for one call once the limit is reached. With this change, sendfile(SF_NODISKIO) is now used automatically as long as sendfile() is enabled, and no longer requires "aio on;".
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 27 Dec 2021 19:48:33 +0300
parents 862f6130d357
children b002ad258f1d
comparison
equal deleted inserted replaced
7984:ae992b5a27b2 7985:ec2e6893caaa
27 #define NGX_NONE 1 27 #define NGX_NONE 1
28 28
29 29
30 static ngx_inline ngx_int_t 30 static ngx_inline ngx_int_t
31 ngx_output_chain_as_is(ngx_output_chain_ctx_t *ctx, ngx_buf_t *buf); 31 ngx_output_chain_as_is(ngx_output_chain_ctx_t *ctx, ngx_buf_t *buf);
32 #if (NGX_HAVE_AIO_SENDFILE)
33 static ngx_int_t ngx_output_chain_aio_setup(ngx_output_chain_ctx_t *ctx,
34 ngx_file_t *file);
35 #endif
36 static ngx_int_t ngx_output_chain_add_copy(ngx_pool_t *pool, 32 static ngx_int_t ngx_output_chain_add_copy(ngx_pool_t *pool,
37 ngx_chain_t **chain, ngx_chain_t *in); 33 ngx_chain_t **chain, ngx_chain_t *in);
38 static ngx_int_t ngx_output_chain_align_file_buf(ngx_output_chain_ctx_t *ctx, 34 static ngx_int_t ngx_output_chain_align_file_buf(ngx_output_chain_ctx_t *ctx,
39 off_t bsize); 35 off_t bsize);
40 static ngx_int_t ngx_output_chain_get_buf(ngx_output_chain_ctx_t *ctx, 36 static ngx_int_t ngx_output_chain_get_buf(ngx_output_chain_ctx_t *ctx,
281 } 277 }
282 278
283 buf->in_file = 0; 279 buf->in_file = 0;
284 } 280 }
285 281
286 #if (NGX_HAVE_AIO_SENDFILE)
287 if (ctx->aio_preload && buf->in_file) {
288 (void) ngx_output_chain_aio_setup(ctx, buf->file);
289 }
290 #endif
291
292 if (ctx->need_in_memory && !ngx_buf_in_memory(buf)) { 282 if (ctx->need_in_memory && !ngx_buf_in_memory(buf)) {
293 return 0; 283 return 0;
294 } 284 }
295 285
296 if (ctx->need_in_temp && (buf->memory || buf->mmap)) { 286 if (ctx->need_in_temp && (buf->memory || buf->mmap)) {
297 return 0; 287 return 0;
298 } 288 }
299 289
300 return 1; 290 return 1;
301 } 291 }
302
303
304 #if (NGX_HAVE_AIO_SENDFILE)
305
306 static ngx_int_t
307 ngx_output_chain_aio_setup(ngx_output_chain_ctx_t *ctx, ngx_file_t *file)
308 {
309 ngx_event_aio_t *aio;
310
311 if (file->aio == NULL && ngx_file_aio_init(file, ctx->pool) != NGX_OK) {
312 return NGX_ERROR;
313 }
314
315 aio = file->aio;
316
317 aio->data = ctx->filter_ctx;
318 aio->preload_handler = ctx->aio_preload;
319
320 return NGX_OK;
321 }
322
323 #endif
324 292
325 293
326 static ngx_int_t 294 static ngx_int_t
327 ngx_output_chain_add_copy(ngx_pool_t *pool, ngx_chain_t **chain, 295 ngx_output_chain_add_copy(ngx_pool_t *pool, ngx_chain_t **chain,
328 ngx_chain_t *in) 296 ngx_chain_t *in)