[nginx] Added support for offloading read() in thread pools.

Valentin Bartenev vbart at nginx.com
Wed Mar 18 15:58:05 UTC 2015


details:   http://hg.nginx.org/nginx/rev/1fdba317ee6d
branches:  
changeset: 6022:1fdba317ee6d
user:      Valentin Bartenev <vbart at nginx.com>
date:      Sat Mar 14 17:37:25 2015 +0300
description:
Added support for offloading read() in thread pools.

diffstat:

 src/core/ngx_buf.h                     |   12 +++-
 src/core/ngx_file.h                    |    6 +
 src/core/ngx_output_chain.c            |   32 ++++++--
 src/http/ngx_http_copy_filter_module.c |   76 ++++++++++++++++++++++-
 src/http/ngx_http_core_module.c        |   74 ++++++++++++++++++++++
 src/http/ngx_http_core_module.h        |   10 +++
 src/http/ngx_http_file_cache.c         |    2 +-
 src/os/unix/ngx_files.c                |  109 +++++++++++++++++++++++++++++++++
 src/os/unix/ngx_files.h                |    5 +
 9 files changed, 312 insertions(+), 14 deletions(-)

diffs (truncated from 522 to 300 lines):

diff -r 117c77b22db1 -r 1fdba317ee6d src/core/ngx_buf.h
--- a/src/core/ngx_buf.h	Sat Mar 14 17:37:21 2015 +0300
+++ b/src/core/ngx_buf.h	Sat Mar 14 17:37:25 2015 +0300
@@ -90,15 +90,23 @@ struct ngx_output_chain_ctx_s {
 #endif
     unsigned                     need_in_memory:1;
     unsigned                     need_in_temp:1;
+#if (NGX_HAVE_FILE_AIO || NGX_THREADS)
+    unsigned                     aio:1;
+#endif
+
 #if (NGX_HAVE_FILE_AIO)
-    unsigned                     aio:1;
-
     ngx_output_chain_aio_pt      aio_handler;
 #if (NGX_HAVE_AIO_SENDFILE)
     ssize_t                    (*aio_preload)(ngx_buf_t *file);
 #endif
 #endif
 
+#if (NGX_THREADS)
+    ngx_int_t                  (*thread_handler)(ngx_thread_task_t *task,
+                                                 ngx_file_t *file);
+    ngx_thread_task_t           *thread_task;
+#endif
+
     off_t                        alignment;
 
     ngx_pool_t                  *pool;
diff -r 117c77b22db1 -r 1fdba317ee6d src/core/ngx_file.h
--- a/src/core/ngx_file.h	Sat Mar 14 17:37:21 2015 +0300
+++ b/src/core/ngx_file.h	Sat Mar 14 17:37:25 2015 +0300
@@ -23,6 +23,12 @@ struct ngx_file_s {
 
     ngx_log_t                 *log;
 
+#if (NGX_THREADS)
+    ngx_int_t                (*thread_handler)(ngx_thread_task_t *task,
+                                               ngx_file_t *file);
+    void                      *thread_ctx;
+#endif
+
 #if (NGX_HAVE_FILE_AIO)
     ngx_event_aio_t           *aio;
 #endif
diff -r 117c77b22db1 -r 1fdba317ee6d src/core/ngx_output_chain.c
--- a/src/core/ngx_output_chain.c	Sat Mar 14 17:37:21 2015 +0300
+++ b/src/core/ngx_output_chain.c	Sat Mar 14 17:37:25 2015 +0300
@@ -50,7 +50,7 @@ ngx_output_chain(ngx_output_chain_ctx_t 
     ngx_chain_t  *cl, *out, **last_out;
 
     if (ctx->in == NULL && ctx->busy == NULL
-#if (NGX_HAVE_FILE_AIO)
+#if (NGX_HAVE_FILE_AIO || NGX_THREADS)
         && !ctx->aio
 #endif
        )
@@ -89,7 +89,7 @@ ngx_output_chain(ngx_output_chain_ctx_t 
 
     for ( ;; ) {
 
-#if (NGX_HAVE_FILE_AIO)
+#if (NGX_HAVE_FILE_AIO || NGX_THREADS)
         if (ctx->aio) {
             return NGX_AGAIN;
         }
@@ -233,6 +233,13 @@ ngx_output_chain_as_is(ngx_output_chain_
         return 1;
     }
 
+#if (NGX_THREADS)
+    if (buf->in_file) {
+        buf->file->thread_handler = ctx->thread_handler;
+        buf->file->thread_ctx = ctx->filter_ctx;
+    }
+#endif
+
     if (buf->in_file && buf->file->directio) {
         return 0;
     }
@@ -559,7 +566,6 @@ ngx_output_chain_copy_buf(ngx_output_cha
 #endif
 
 #if (NGX_HAVE_FILE_AIO)
-
         if (ctx->aio_handler) {
             n = ngx_file_aio_read(src->file, dst->pos, (size_t) size,
                                   src->file_pos, ctx->pool);
@@ -568,15 +574,23 @@ ngx_output_chain_copy_buf(ngx_output_cha
                 return NGX_AGAIN;
             }
 
-        } else {
+        } else
+#endif
+#if (NGX_THREADS)
+        if (src->file->thread_handler) {
+            n = ngx_thread_read(&ctx->thread_task, src->file, dst->pos,
+                                (size_t) size, src->file_pos, ctx->pool);
+            if (n == NGX_AGAIN) {
+                ctx->aio = 1;
+                return NGX_AGAIN;
+            }
+
+        } else
+#endif
+        {
             n = ngx_read_file(src->file, dst->pos, (size_t) size,
                               src->file_pos);
         }
-#else
-
-        n = ngx_read_file(src->file, dst->pos, (size_t) size, src->file_pos);
-
-#endif
 
 #if (NGX_HAVE_ALIGNED_DIRECTIO)
 
diff -r 117c77b22db1 -r 1fdba317ee6d src/http/ngx_http_copy_filter_module.c
--- a/src/http/ngx_http_copy_filter_module.c	Sat Mar 14 17:37:21 2015 +0300
+++ b/src/http/ngx_http_copy_filter_module.c	Sat Mar 14 17:37:25 2015 +0300
@@ -24,6 +24,11 @@ static ssize_t ngx_http_copy_aio_sendfil
 static void ngx_http_copy_aio_sendfile_event_handler(ngx_event_t *ev);
 #endif
 #endif
+#if (NGX_THREADS)
+static ngx_int_t ngx_http_copy_thread_handler(ngx_thread_task_t *task,
+    ngx_file_t *file);
+static void ngx_http_copy_thread_event_handler(ngx_event_t *ev);
+#endif
 
 static void *ngx_http_copy_filter_create_conf(ngx_conf_t *cf);
 static char *ngx_http_copy_filter_merge_conf(ngx_conf_t *cf,
@@ -121,7 +126,7 @@ ngx_http_copy_filter(ngx_http_request_t 
         ctx->filter_ctx = r;
 
 #if (NGX_HAVE_FILE_AIO)
-        if (ngx_file_aio && clcf->aio) {
+        if (ngx_file_aio && clcf->aio == NGX_HTTP_AIO_ON) {
             ctx->aio_handler = ngx_http_copy_aio_handler;
 #if (NGX_HAVE_AIO_SENDFILE)
             ctx->aio_preload = ngx_http_copy_aio_sendfile_preload;
@@ -129,12 +134,18 @@ ngx_http_copy_filter(ngx_http_request_t 
         }
 #endif
 
+#if (NGX_THREADS)
+        if (clcf->aio == NGX_HTTP_AIO_THREADS) {
+            ctx->thread_handler = ngx_http_copy_thread_handler;
+        }
+#endif
+
         if (in && in->buf && ngx_buf_size(in->buf)) {
             r->request_output = 1;
         }
     }
 
-#if (NGX_HAVE_FILE_AIO)
+#if (NGX_HAVE_FILE_AIO || NGX_THREADS)
     ctx->aio = r->aio;
 #endif
 
@@ -233,6 +244,67 @@ ngx_http_copy_aio_sendfile_event_handler
 #endif
 
 
+#if (NGX_THREADS)
+
+static ngx_int_t
+ngx_http_copy_thread_handler(ngx_thread_task_t *task, ngx_file_t *file)
+{
+    ngx_str_t                  name;
+    ngx_thread_pool_t         *tp;
+    ngx_http_request_t        *r;
+    ngx_http_core_loc_conf_t  *clcf;
+
+    r = file->thread_ctx;
+
+    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+    tp = clcf->thread_pool;
+
+    if (tp == NULL) {
+        if (ngx_http_complex_value(r, clcf->thread_pool_value, &name)
+            != NGX_OK)
+        {
+            return NGX_ERROR;
+        }
+
+        tp = ngx_thread_pool_get((ngx_cycle_t *) ngx_cycle, &name);
+
+        if (tp == NULL) {
+            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                          "thread pool \"%V\" not found", &name);
+            return NGX_ERROR;
+        }
+    }
+
+    task->event.data = r;
+    task->event.handler = ngx_http_copy_thread_event_handler;
+
+    if (ngx_thread_task_post(tp, task) != NGX_OK) {
+        return NGX_ERROR;
+    }
+
+    r->main->blocked++;
+    r->aio = 1;
+
+    return NGX_OK;
+}
+
+
+static void
+ngx_http_copy_thread_event_handler(ngx_event_t *ev)
+{
+    ngx_http_request_t  *r;
+
+    r = ev->data;
+
+    r->main->blocked--;
+    r->aio = 0;
+
+    r->connection->write->handler(r->connection->write);
+}
+
+#endif
+
+
 static void *
 ngx_http_copy_filter_create_conf(ngx_conf_t *cf)
 {
diff -r 117c77b22db1 -r 1fdba317ee6d src/http/ngx_http_core_module.c
--- a/src/http/ngx_http_core_module.c	Sat Mar 14 17:37:21 2015 +0300
+++ b/src/http/ngx_http_core_module.c	Sat Mar 14 17:37:25 2015 +0300
@@ -3624,6 +3624,10 @@ ngx_http_core_create_loc_conf(ngx_conf_t
     clcf->sendfile = NGX_CONF_UNSET;
     clcf->sendfile_max_chunk = NGX_CONF_UNSET_SIZE;
     clcf->aio = NGX_CONF_UNSET;
+#if (NGX_THREADS)
+    clcf->thread_pool = NGX_CONF_UNSET_PTR;
+    clcf->thread_pool_value = NGX_CONF_UNSET_PTR;
+#endif
     clcf->read_ahead = NGX_CONF_UNSET_SIZE;
     clcf->directio = NGX_CONF_UNSET;
     clcf->directio_alignment = NGX_CONF_UNSET;
@@ -3839,7 +3843,14 @@ ngx_http_core_merge_loc_conf(ngx_conf_t 
     ngx_conf_merge_value(conf->sendfile, prev->sendfile, 0);
     ngx_conf_merge_size_value(conf->sendfile_max_chunk,
                               prev->sendfile_max_chunk, 0);
+#if (NGX_HAVE_FILE_AIO || NGX_THREADS)
     ngx_conf_merge_value(conf->aio, prev->aio, NGX_HTTP_AIO_OFF);
+#endif
+#if (NGX_THREADS)
+    ngx_conf_merge_ptr_value(conf->thread_pool, prev->thread_pool, NULL);
+    ngx_conf_merge_ptr_value(conf->thread_pool_value, prev->thread_pool_value,
+                             NULL);
+#endif
     ngx_conf_merge_size_value(conf->read_ahead, prev->read_ahead, 0);
     ngx_conf_merge_off_value(conf->directio, prev->directio,
                               NGX_OPEN_FILE_DIRECTIO_OFF);
@@ -4644,6 +4655,11 @@ ngx_http_core_set_aio(ngx_conf_t *cf, ng
         return "is duplicate";
     }
 
+#if (NGX_THREADS)
+    clcf->thread_pool = NULL;
+    clcf->thread_pool_value = NULL;
+#endif
+
     value = cf->args->elts;
 
     if (ngx_strcmp(value[1].data, "off") == 0) {
@@ -4676,6 +4692,64 @@ ngx_http_core_set_aio(ngx_conf_t *cf, ng
 
 #endif
 
+    if (ngx_strncmp(value[1].data, "threads", 7) == 0
+        && (value[1].len == 7 || value[1].data[7] == '='))
+    {
+#if (NGX_THREADS)
+        ngx_str_t                          name;
+        ngx_thread_pool_t                 *tp;
+        ngx_http_complex_value_t           cv;
+        ngx_http_compile_complex_value_t   ccv;
+
+        clcf->aio = NGX_HTTP_AIO_THREADS;
+
+        if (value[1].len >= 8) {
+            name.len = value[1].len - 8;
+            name.data = value[1].data + 8;
+
+            ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
+
+            ccv.cf = cf;
+            ccv.value = &name;
+            ccv.complex_value = &cv;
+
+            if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
+                return NGX_CONF_ERROR;
+            }
+



More information about the nginx-devel mailing list