directio

Igor Sysoev is at rambler-co.ru
Fri Jul 25 10:12:23 MSD 2008


Патч добавляет директиву directio, разрешающую использовать O_DIRECT
для файлов больше указанного размера:

directio 4m;
directio 0;  # запрещает O_DIRECT, default

Можно ставить на уровне http/server/location.
Возможно, имеет смысл выключить sendfile.

O_DIRECT, по идее, должно улучшать скорость отдачи больших файлов
и ухудшать - небольших.


-- 
Игорь Сысоев
http://sysoev.ru
-------------- next part --------------
Index: src/http/ngx_http_core_module.c
===================================================================
--- src/http/ngx_http_core_module.c	(revision 1439)
+++ src/http/ngx_http_core_module.c	(working copy)
@@ -353,6 +353,13 @@
       offsetof(ngx_http_core_loc_conf_t, sendfile_max_chunk),
       NULL },
 
+    { ngx_string("directio"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_off_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_core_loc_conf_t, directio),
+      NULL },
+
     { ngx_string("tcp_nopush"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
       ngx_conf_set_flag_slot,
@@ -2586,6 +2593,7 @@
     lcf->client_body_in_file_only = NGX_CONF_UNSET;
     lcf->sendfile = NGX_CONF_UNSET;
     lcf->sendfile_max_chunk = NGX_CONF_UNSET_SIZE;
+    lcf->directio = NGX_CONF_UNSET;
     lcf->tcp_nopush = NGX_CONF_UNSET;
     lcf->tcp_nodelay = NGX_CONF_UNSET;
     lcf->send_timeout = NGX_CONF_UNSET_MSEC;
@@ -2774,6 +2782,7 @@
     ngx_conf_merge_value(conf->sendfile, prev->sendfile, 0);
     ngx_conf_merge_size_value(conf->sendfile_max_chunk,
                               prev->sendfile_max_chunk, 0);
+    ngx_conf_merge_off_value(conf->directio, prev->directio, 0);
     ngx_conf_merge_value(conf->tcp_nopush, prev->tcp_nopush, 0);
     ngx_conf_merge_value(conf->tcp_nodelay, prev->tcp_nodelay, 1);
 
Index: src/http/ngx_http_core_module.h
===================================================================
--- src/http/ngx_http_core_module.h	(revision 1439)
+++ src/http/ngx_http_core_module.h	(working copy)
@@ -265,6 +265,7 @@
     ngx_str_t     default_type;
 
     off_t         client_max_body_size;    /* client_max_body_size */
+    off_t         directio;                /* directio */
 
     size_t        client_body_buffer_size; /* client_body_buffer_size */
     size_t        send_lowat;              /* send_lowat */
Index: src/http/ngx_http_script.c
===================================================================
--- src/http/ngx_http_script.c	(revision 1439)
+++ src/http/ngx_http_script.c	(working copy)
@@ -996,6 +996,7 @@
 
     ngx_memzero(&of, sizeof(ngx_open_file_info_t));
 
+    of.directio = clcf->directio;
     of.valid = clcf->open_file_cache_valid;
     of.min_uses = clcf->open_file_cache_min_uses;
     of.errors = clcf->open_file_cache_errors;
Index: src/http/modules/ngx_http_gzip_static_module.c
===================================================================
--- src/http/modules/ngx_http_gzip_static_module.c	(revision 1439)
+++ src/http/modules/ngx_http_gzip_static_module.c	(working copy)
@@ -121,6 +121,7 @@
 
     ngx_memzero(&of, sizeof(ngx_open_file_info_t));
 
+    of.directio = clcf->directio;
     of.valid = clcf->open_file_cache_valid;
     of.min_uses = clcf->open_file_cache_min_uses;
     of.errors = clcf->open_file_cache_errors;
Index: src/http/modules/ngx_http_index_module.c
===================================================================
--- src/http/modules/ngx_http_index_module.c	(revision 1439)
+++ src/http/modules/ngx_http_index_module.c	(working copy)
@@ -210,6 +210,7 @@
 
         ngx_memzero(&of, sizeof(ngx_open_file_info_t));
 
+        of.directio = clcf->directio;
         of.valid = clcf->open_file_cache_valid;
         of.min_uses = clcf->open_file_cache_min_uses;
         of.errors = clcf->open_file_cache_errors;
Index: src/http/modules/ngx_http_flv_module.c
===================================================================
--- src/http/modules/ngx_http_flv_module.c	(revision 1439)
+++ src/http/modules/ngx_http_flv_module.c	(working copy)
@@ -107,6 +107,7 @@
 
     ngx_memzero(&of, sizeof(ngx_open_file_info_t));
 
+    of.directio = clcf->directio;
     of.valid = clcf->open_file_cache_valid;
     of.min_uses = clcf->open_file_cache_min_uses;
     of.errors = clcf->open_file_cache_errors;
Index: src/http/modules/ngx_http_static_module.c
===================================================================
--- src/http/modules/ngx_http_static_module.c	(revision 1439)
+++ src/http/modules/ngx_http_static_module.c	(working copy)
@@ -98,6 +98,7 @@
 
     ngx_memzero(&of, sizeof(ngx_open_file_info_t));
 
+    of.directio = clcf->directio;
     of.valid = clcf->open_file_cache_valid;
     of.min_uses = clcf->open_file_cache_min_uses;
     of.errors = clcf->open_file_cache_errors;
Index: src/http/modules/perl/nginx.xs
===================================================================
--- src/http/modules/perl/nginx.xs	(revision 1439)
+++ src/http/modules/perl/nginx.xs	(working copy)
@@ -652,6 +652,7 @@
 
     ngx_memzero(&of, sizeof(ngx_open_file_info_t));
 
+    of.directio = clcf->directio;
     of.valid = clcf->open_file_cache_valid;
     of.min_uses = clcf->open_file_cache_min_uses;
     of.errors = clcf->open_file_cache_errors;
Index: src/os/unix/ngx_files.c
===================================================================
--- src/os/unix/ngx_files.c	(revision 1439)
+++ src/os/unix/ngx_files.c	(working copy)
@@ -351,3 +351,22 @@
 
     return 0;
 }
+
+
+#if (NGX_HAVE_O_DIRECT)
+
+ngx_int_t
+ngx_directio(ngx_fd_t fd)
+{
+    int  flags;
+
+    flags = fcntl(fd, F_GETFL);
+
+    if (flags == -1) {
+        return -1;
+    }
+
+    return fcntl(fd, F_SETFL, flags | O_DIRECT);
+}
+
+#endif
Index: src/os/unix/ngx_files.h
===================================================================
--- src/os/unix/ngx_files.h	(revision 1439)
+++ src/os/unix/ngx_files.h	(working copy)
@@ -177,4 +177,22 @@
 #define ngx_unlock_fd_n          "fcntl(F_SETLK, F_UNLCK)"
 
 
+#if (NGX_HAVE_O_DIRECT)
+
+ngx_int_t ngx_directio(ngx_fd_t fd);
+#define ngx_directio_n           "fcntl(O_DIRECT)"
+
+#elif (NGX_HAVE_F_NOCACHE)
+
+#define ngx_directio(fd)         fcntl(fd, F_NOCACHE, 1)
+#define ngx_directio_n           "fcntl(F_NOCACHE)"
+
+#else
+
+#define ngx_directio(fd)         0
+#define ngx_directio_n           "ngx_directio_n"
+
+#endif
+
+
 #endif /* _NGX_FILES_H_INCLUDED_ */
Index: src/core/ngx_open_file_cache.c
===================================================================
--- src/core/ngx_open_file_cache.c	(revision 1439)
+++ src/core/ngx_open_file_cache.c	(working copy)
@@ -499,6 +499,13 @@
         of->fd = fd;
     }
 
+    if (of->directio && of->directio <= ngx_file_size(&fi)) {
+        if (ngx_directio(fd) == -1) {
+            ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
+                          ngx_directio_n " \"%s\" failed", name);
+        }
+    }
+
 done:
 
     of->uniq = ngx_file_uniq(&fi);
Index: src/core/ngx_open_file_cache.h
===================================================================
--- src/core/ngx_open_file_cache.h	(revision 1439)
+++ src/core/ngx_open_file_cache.h	(working copy)
@@ -17,6 +17,7 @@
     ngx_file_uniq_t          uniq;
     time_t                   mtime;
     off_t                    size;
+    off_t                    directio;
     ngx_err_t                err;
 
     time_t                   valid;
Index: auto/os/features
===================================================================
--- auto/os/features	(revision 1439)
+++ auto/os/features	(working copy)
@@ -200,3 +200,23 @@
         CRYPT_LIB="-lcrypt"
     fi
 fi
+
+
+ngx_feature="O_DIRECT"
+ngx_feature_name="NGX_HAVE_O_DIRECT"
+ngx_feature_run=no
+ngx_feature_incs="#include <fcntl.h>"
+ngx_feature_path=
+ngx_feature_libs=
+ngx_feature_test="fcntl(0, F_SETFL, O_DIRECT);"
+. auto/feature
+
+
+ngx_feature="F_NOCACHE"
+ngx_feature_name="NGX_HAVE_F_NOCACHE"
+ngx_feature_run=no
+ngx_feature_incs="#include <fcntl.h>"
+ngx_feature_path=
+ngx_feature_libs=
+ngx_feature_test="fcntl(0, F_NOCACHE, 1);"
+. auto/feature


More information about the nginx-ru mailing list