[nginx] Events: processing of posted events changed from LIFO to...

Valentin Bartenev vbart at nginx.com
Mon Sep 1 14:22:17 UTC 2014


details:   http://hg.nginx.org/nginx/rev/3f5f0ab59b35
branches:  
changeset: 5821:3f5f0ab59b35
user:      Valentin Bartenev <vbart at nginx.com>
date:      Mon Sep 01 18:20:18 2014 +0400
description:
Events: processing of posted events changed from LIFO to FIFO.

In theory, this can provide a bit better distribution of latencies.

Also it simplifies the code, since ngx_queue_t is now used instead
of custom implementation.

diffstat:

 src/core/ngx_connection.c                   |   4 ++--
 src/event/modules/ngx_devpoll_module.c      |   3 ++-
 src/event/modules/ngx_epoll_module.c        |   3 ++-
 src/event/modules/ngx_eventport_module.c    |   3 ++-
 src/event/modules/ngx_kqueue_module.c       |   3 ++-
 src/event/modules/ngx_poll_module.c         |   3 ++-
 src/event/modules/ngx_rtsig_module.c        |   6 ++++--
 src/event/modules/ngx_select_module.c       |   3 ++-
 src/event/modules/ngx_win32_select_module.c |   3 ++-
 src/event/ngx_event.c                       |  14 +++++---------
 src/event/ngx_event.h                       |   9 +++++----
 src/event/ngx_event_posted.c                |  16 +++++++---------
 src/event/ngx_event_posted.h                |  27 +++++++++------------------
 src/http/ngx_http_spdy.c                    |   4 ++--
 14 files changed, 48 insertions(+), 53 deletions(-)

diffs (truncated from 312 to 300 lines):

diff -r 3377f9459e99 -r 3f5f0ab59b35 src/core/ngx_connection.c
--- a/src/core/ngx_connection.c	Mon Sep 01 18:20:03 2014 +0400
+++ b/src/core/ngx_connection.c	Mon Sep 01 18:20:18 2014 +0400
@@ -955,11 +955,11 @@ ngx_close_connection(ngx_connection_t *c
 
 #endif
 
-    if (c->read->prev) {
+    if (c->read->posted) {
         ngx_delete_posted_event(c->read);
     }
 
-    if (c->write->prev) {
+    if (c->write->posted) {
         ngx_delete_posted_event(c->write);
     }
 
diff -r 3377f9459e99 -r 3f5f0ab59b35 src/event/modules/ngx_devpoll_module.c
--- a/src/event/modules/ngx_devpoll_module.c	Mon Sep 01 18:20:03 2014 +0400
+++ b/src/event/modules/ngx_devpoll_module.c	Mon Sep 01 18:20:18 2014 +0400
@@ -344,7 +344,8 @@ ngx_devpoll_process_events(ngx_cycle_t *
     ngx_err_t           err;
     ngx_int_t           i;
     ngx_uint_t          level, instance;
-    ngx_event_t        *rev, *wev, **queue;
+    ngx_event_t        *rev, *wev;
+    ngx_queue_t        *queue;
     ngx_connection_t   *c;
     struct pollfd       pfd;
     struct dvpoll       dvp;
diff -r 3377f9459e99 -r 3f5f0ab59b35 src/event/modules/ngx_epoll_module.c
--- a/src/event/modules/ngx_epoll_module.c	Mon Sep 01 18:20:03 2014 +0400
+++ b/src/event/modules/ngx_epoll_module.c	Mon Sep 01 18:20:18 2014 +0400
@@ -568,7 +568,8 @@ ngx_epoll_process_events(ngx_cycle_t *cy
     ngx_int_t          instance, i;
     ngx_uint_t         level;
     ngx_err_t          err;
-    ngx_event_t       *rev, *wev, **queue;
+    ngx_event_t       *rev, *wev;
+    ngx_queue_t       *queue;
     ngx_connection_t  *c;
 
     /* NGX_TIMER_INFINITE == INFTIM */
diff -r 3377f9459e99 -r 3f5f0ab59b35 src/event/modules/ngx_eventport_module.c
--- a/src/event/modules/ngx_eventport_module.c	Mon Sep 01 18:20:03 2014 +0400
+++ b/src/event/modules/ngx_eventport_module.c	Mon Sep 01 18:20:18 2014 +0400
@@ -414,7 +414,8 @@ ngx_eventport_process_events(ngx_cycle_t
     ngx_err_t           err;
     ngx_int_t           instance;
     ngx_uint_t          i, level;
-    ngx_event_t        *ev, *rev, *wev, **queue;
+    ngx_event_t        *ev, *rev, *wev;
+    ngx_queue_t        *queue;
     ngx_connection_t   *c;
     struct timespec     ts, *tp;
 
diff -r 3377f9459e99 -r 3f5f0ab59b35 src/event/modules/ngx_kqueue_module.c
--- a/src/event/modules/ngx_kqueue_module.c	Mon Sep 01 18:20:03 2014 +0400
+++ b/src/event/modules/ngx_kqueue_module.c	Mon Sep 01 18:20:18 2014 +0400
@@ -495,7 +495,8 @@ ngx_kqueue_process_events(ngx_cycle_t *c
     ngx_int_t         i, instance;
     ngx_uint_t        level;
     ngx_err_t         err;
-    ngx_event_t      *ev, **queue;
+    ngx_event_t      *ev;
+    ngx_queue_t      *queue;
     struct timespec   ts, *tp;
 
     if (ngx_threaded) {
diff -r 3377f9459e99 -r 3f5f0ab59b35 src/event/modules/ngx_poll_module.c
--- a/src/event/modules/ngx_poll_module.c	Mon Sep 01 18:20:03 2014 +0400
+++ b/src/event/modules/ngx_poll_module.c	Mon Sep 01 18:20:18 2014 +0400
@@ -241,7 +241,8 @@ ngx_poll_process_events(ngx_cycle_t *cyc
     int                 ready, revents;
     ngx_err_t           err;
     ngx_uint_t          i, found, level;
-    ngx_event_t        *ev, **queue;
+    ngx_event_t        *ev;
+    ngx_queue_t        *queue;
     ngx_connection_t   *c;
 
     /* NGX_TIMER_INFINITE == INFTIM */
diff -r 3377f9459e99 -r 3f5f0ab59b35 src/event/modules/ngx_rtsig_module.c
--- a/src/event/modules/ngx_rtsig_module.c	Mon Sep 01 18:20:03 2014 +0400
+++ b/src/event/modules/ngx_rtsig_module.c	Mon Sep 01 18:20:18 2014 +0400
@@ -307,7 +307,8 @@ ngx_rtsig_process_events(ngx_cycle_t *cy
     ngx_int_t           instance;
     ngx_err_t           err;
     siginfo_t           si;
-    ngx_event_t        *rev, *wev, **queue;
+    ngx_event_t        *rev, *wev;
+    ngx_queue_t        *queue;
     struct timespec     ts, *tp;
     struct sigaction    sa;
     ngx_connection_t   *c;
@@ -480,7 +481,8 @@ ngx_rtsig_process_overflow(ngx_cycle_t *
     size_t             len;
     ngx_err_t          err;
     ngx_uint_t         tested, n, i;
-    ngx_event_t       *rev, *wev, **queue;
+    ngx_event_t       *rev, *wev;
+    ngx_queue_t       *queue;
     ngx_connection_t  *c;
     ngx_rtsig_conf_t  *rtscf;
 
diff -r 3377f9459e99 -r 3f5f0ab59b35 src/event/modules/ngx_select_module.c
--- a/src/event/modules/ngx_select_module.c	Mon Sep 01 18:20:03 2014 +0400
+++ b/src/event/modules/ngx_select_module.c	Mon Sep 01 18:20:18 2014 +0400
@@ -214,7 +214,8 @@ ngx_select_process_events(ngx_cycle_t *c
     int                ready, nready;
     ngx_err_t          err;
     ngx_uint_t         i, found;
-    ngx_event_t       *ev, **queue;
+    ngx_event_t       *ev;
+    ngx_queue_t       *queue;
     struct timeval     tv, *tp;
     ngx_connection_t  *c;
 
diff -r 3377f9459e99 -r 3f5f0ab59b35 src/event/modules/ngx_win32_select_module.c
--- a/src/event/modules/ngx_win32_select_module.c	Mon Sep 01 18:20:03 2014 +0400
+++ b/src/event/modules/ngx_win32_select_module.c	Mon Sep 01 18:20:18 2014 +0400
@@ -221,7 +221,8 @@ ngx_select_process_events(ngx_cycle_t *c
     int                ready, nready;
     ngx_err_t          err;
     ngx_uint_t         i, found;
-    ngx_event_t       *ev, **queue;
+    ngx_event_t       *ev;
+    ngx_queue_t       *queue;
     struct timeval     tv, *tp;
     ngx_connection_t  *c;
 
diff -r 3377f9459e99 -r 3f5f0ab59b35 src/event/ngx_event.c
--- a/src/event/ngx_event.c	Mon Sep 01 18:20:03 2014 +0400
+++ b/src/event/ngx_event.c	Mon Sep 01 18:20:18 2014 +0400
@@ -252,9 +252,7 @@ ngx_process_events_and_timers(ngx_cycle_
     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                    "timer delta: %M", delta);
 
-    if (ngx_posted_accept_events) {
-        ngx_event_process_posted(cycle, &ngx_posted_accept_events);
-    }
+    ngx_event_process_posted(cycle, &ngx_posted_accept_events);
 
     if (ngx_accept_mutex_held) {
         ngx_shmtx_unlock(&ngx_accept_mutex);
@@ -264,12 +262,7 @@ ngx_process_events_and_timers(ngx_cycle_
         ngx_event_expire_timers();
     }
 
-    ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
-                   "posted events %p", ngx_posted_events);
-
-    if (ngx_posted_events) {
-        ngx_event_process_posted(cycle, &ngx_posted_events);
-    }
+    ngx_event_process_posted(cycle, &ngx_posted_events);
 }
 
 
@@ -612,6 +605,9 @@ ngx_event_process_init(ngx_cycle_t *cycl
 
 #endif
 
+    ngx_queue_init(&ngx_posted_accept_events);
+    ngx_queue_init(&ngx_posted_events);
+
     if (ngx_event_timer_init(cycle->log) == NGX_ERROR) {
         return NGX_ERROR;
     }
diff -r 3377f9459e99 -r 3f5f0ab59b35 src/event/ngx_event.h
--- a/src/event/ngx_event.h	Mon Sep 01 18:20:03 2014 +0400
+++ b/src/event/ngx_event.h	Mon Sep 01 18:20:18 2014 +0400
@@ -74,6 +74,8 @@ struct ngx_event_s {
     /* the pending eof reported by kqueue, epoll or in aio chain operation */
     unsigned         pending_eof:1;
 
+    unsigned         posted:1;
+
 #if (NGX_WIN32)
     /* setsockopt(SO_UPDATE_ACCEPT_CONTEXT) was successful */
     unsigned         accept_context_updated:1;
@@ -125,16 +127,15 @@ struct ngx_event_s {
 
     ngx_rbtree_node_t   timer;
 
+    /* the posted queue */
+    ngx_queue_t      queue;
+
     unsigned         closed:1;
 
     /* to test on worker exit */
     unsigned         channel:1;
     unsigned         resolver:1;
 
-    /* the links of the posted queue */
-    ngx_event_t     *next;
-    ngx_event_t    **prev;
-
 
 #if 0
 
diff -r 3377f9459e99 -r 3f5f0ab59b35 src/event/ngx_event_posted.c
--- a/src/event/ngx_event_posted.c	Mon Sep 01 18:20:03 2014 +0400
+++ b/src/event/ngx_event_posted.c	Mon Sep 01 18:20:18 2014 +0400
@@ -10,26 +10,24 @@
 #include <ngx_event.h>
 
 
-ngx_event_t  *ngx_posted_accept_events;
-ngx_event_t  *ngx_posted_events;
+ngx_queue_t  ngx_posted_accept_events;
+ngx_queue_t  ngx_posted_events;
 
 
 void
-ngx_event_process_posted(ngx_cycle_t *cycle, ngx_event_t **posted)
+ngx_event_process_posted(ngx_cycle_t *cycle, ngx_queue_t *posted)
 {
+    ngx_queue_t  *q;
     ngx_event_t  *ev;
 
-    for ( ;; ) {
+    while (!ngx_queue_empty(posted)) {
 
-        ev = *posted;
+        q = ngx_queue_head(posted);
+        ev = ngx_queue_data(q, ngx_event_t, queue);
 
         ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                       "posted event %p", ev);
 
-        if (ev == NULL) {
-            return;
-        }
-
         ngx_delete_posted_event(ev);
 
         ev->handler(ev);
diff -r 3377f9459e99 -r 3f5f0ab59b35 src/event/ngx_event_posted.h
--- a/src/event/ngx_event_posted.h	Mon Sep 01 18:20:03 2014 +0400
+++ b/src/event/ngx_event_posted.h	Mon Sep 01 18:20:18 2014 +0400
@@ -14,16 +14,11 @@
 #include <ngx_event.h>
 
 
-#define ngx_post_event(ev, queue)                                             \
+#define ngx_post_event(ev, q)                                                 \
                                                                               \
-    if (ev->prev == NULL) {                                                   \
-        ev->next = *queue;                                                    \
-        ev->prev = queue;                                                     \
-        *queue = ev;                                                          \
-                                                                              \
-        if (ev->next) {                                                       \
-            ev->next->prev = &ev->next;                                       \
-        }                                                                     \
+    if (!ev->posted) {                                                        \
+        ev->posted = 1;                                                       \
+        ngx_queue_insert_tail(q, &ev->queue);                                 \
                                                                               \
         ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0, "post event %p", ev);  \
                                                                               \
@@ -35,23 +30,19 @@
 
 #define ngx_delete_posted_event(ev)                                           \
                                                                               \
-    *(ev->prev) = ev->next;                                                   \
+    ev->posted = 0;                                                           \
+    ngx_queue_remove(&ev->queue);                                             \
                                                                               \
-    if (ev->next) {                                                           \
-        ev->next->prev = ev->prev;                                            \
-    }                                                                         \
-                                                                              \
-    ev->prev = NULL;                                                          \
     ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0,                            \
                    "delete posted event %p", ev);
 
 
 
-void ngx_event_process_posted(ngx_cycle_t *cycle, ngx_event_t **posted);
+void ngx_event_process_posted(ngx_cycle_t *cycle, ngx_queue_t *posted);
 
 
-extern ngx_event_t  *ngx_posted_accept_events;
-extern ngx_event_t  *ngx_posted_events;
+extern ngx_queue_t  ngx_posted_accept_events;
+extern ngx_queue_t  ngx_posted_events;
 
 
 #endif /* _NGX_EVENT_POSTED_H_INCLUDED_ */
diff -r 3377f9459e99 -r 3f5f0ab59b35 src/http/ngx_http_spdy.c
--- a/src/http/ngx_http_spdy.c	Mon Sep 01 18:20:03 2014 +0400
+++ b/src/http/ngx_http_spdy.c	Mon Sep 01 18:20:18 2014 +0400
@@ -3372,7 +3372,7 @@ ngx_http_spdy_close_stream(ngx_http_spdy
         ngx_del_timer(ev);
     }
 
-    if (ev->prev) {
+    if (ev->posted) {



More information about the nginx-devel mailing list