[nginx] Mp4: of.directio_off flag to avoid unneeded syscalls.
Maxim Dounin
mdounin at mdounin.ru
Sun May 25 20:59:02 UTC 2025
details: http://freenginx.org/hg/nginx/rev/268e0fb3a16c
branches:
changeset: 9360:268e0fb3a16c
user: Maxim Dounin <mdounin at mdounin.ru>
date: Sun May 25 23:57:39 2025 +0300
description:
Mp4: of.directio_off flag to avoid unneeded syscalls.
When set, ngx_open_cached_file() won't try to enable directio on the file,
allowing the caller to do it itself, but will set is_directio flag in
ngx_open_file_info_t and in open file cache. The fact that directio needs
to be enabled by the caller is signalled in the of.is_directio_off output
flag.
This approach makes it possible to avoid unneeded syscalls when directio
is enabled, yet the caller needs it disabled for some initial file
processing, such as in the mp4 module.
diffstat:
src/core/ngx_open_file_cache.c | 8 +++++++-
src/core/ngx_open_file_cache.h | 2 ++
src/http/modules/ngx_http_mp4_module.c | 10 +++++-----
3 files changed, 14 insertions(+), 6 deletions(-)
diffs (105 lines):
diff --git a/src/core/ngx_open_file_cache.c b/src/core/ngx_open_file_cache.c
--- a/src/core/ngx_open_file_cache.c
+++ b/src/core/ngx_open_file_cache.c
@@ -249,6 +249,7 @@ ngx_open_cached_file(ngx_open_file_cache
of->is_link = file->is_link;
of->is_exec = file->is_exec;
of->is_directio = file->is_directio;
+ of->is_directio_off = 0;
if (!file->is_dir) {
file->count++;
@@ -313,6 +314,7 @@ ngx_open_cached_file(ngx_open_file_cache
}
of->is_directio = file->is_directio;
+ of->is_directio_off = 0;
goto update;
}
@@ -920,7 +922,11 @@ ngx_open_and_stat_file(ngx_str_t *name,
}
if (of->directio <= ngx_file_size(&fi)) {
- if (ngx_directio_on(fd) == NGX_FILE_ERROR) {
+ if (of->directio_off) {
+ of->is_directio = 1;
+ of->is_directio_off = 1;
+
+ } else if (ngx_directio_on(fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
ngx_directio_on_n " \"%V\" failed", name);
diff --git a/src/core/ngx_open_file_cache.h b/src/core/ngx_open_file_cache.h
--- a/src/core/ngx_open_file_cache.h
+++ b/src/core/ngx_open_file_cache.h
@@ -42,12 +42,14 @@ typedef struct {
unsigned log:1;
unsigned errors:1;
unsigned events:1;
+ unsigned directio_off:1;
unsigned is_dir:1;
unsigned is_file:1;
unsigned is_link:1;
unsigned is_exec:1;
unsigned is_directio:1;
+ unsigned is_directio_off:1;
} ngx_open_file_info_t;
diff --git a/src/http/modules/ngx_http_mp4_module.c b/src/http/modules/ngx_http_mp4_module.c
--- a/src/http/modules/ngx_http_mp4_module.c
+++ b/src/http/modules/ngx_http_mp4_module.c
@@ -479,7 +479,7 @@ ngx_http_mp4_handler(ngx_http_request_t
u_char *last;
size_t root;
ngx_int_t rc, start, end;
- ngx_uint_t level, length, directio;
+ ngx_uint_t level, length;
ngx_str_t path, value;
ngx_log_t *log;
ngx_buf_t *b;
@@ -520,6 +520,7 @@ ngx_http_mp4_handler(ngx_http_request_t
of.read_ahead = clcf->read_ahead;
of.directio = clcf->directio;
+ of.directio_off = 1;
of.valid = clcf->open_file_cache_valid;
of.min_uses = clcf->open_file_cache_min_uses;
of.errors = clcf->open_file_cache_errors;
@@ -579,7 +580,6 @@ ngx_http_mp4_handler(ngx_http_request_t
start = -1;
length = 0;
- directio = 0;
r->headers_out.content_length_n = of.size;
mp4 = NULL;
b = NULL;
@@ -615,7 +615,7 @@ ngx_http_mp4_handler(ngx_http_request_t
if (start >= 0) {
r->single_range = 1;
- if (of.is_directio) {
+ if (of.is_directio && !of.is_directio_off) {
/*
* DIRECTIO is set on transfer only
@@ -627,7 +627,7 @@ ngx_http_mp4_handler(ngx_http_request_t
ngx_directio_off_n " \"%s\" failed", path.data);
}
- directio = 1;
+ of.is_directio_off = 1;
}
mp4 = ngx_pcalloc(r->pool, sizeof(ngx_http_mp4_file_t));
@@ -673,7 +673,7 @@ ngx_http_mp4_handler(ngx_http_request_t
log->action = "sending mp4 to client";
- if (directio) {
+ if (of.is_directio_off) {
/* DIRECTIO was switched off, restore it */
More information about the nginx-devel
mailing list