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.
This commit is contained in:
Valentin Bartenev 2014-09-01 18:20:18 +04:00
parent 2a81e05566
commit 37d24e7e3b
14 changed files with 48 additions and 53 deletions

View File

@ -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);
}

View File

@ -344,7 +344,8 @@ ngx_devpoll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
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;

View File

@ -568,7 +568,8 @@ ngx_epoll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags)
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 */

View File

@ -414,7 +414,8 @@ ngx_eventport_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
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;

View File

@ -495,7 +495,8 @@ ngx_kqueue_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
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) {

View File

@ -241,7 +241,8 @@ ngx_poll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags)
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 */

View File

@ -307,7 +307,8 @@ ngx_rtsig_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags)
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 *cycle, ngx_msec_t timer,
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;

View File

@ -214,7 +214,8 @@ ngx_select_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
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;

View File

@ -221,7 +221,8 @@ ngx_select_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
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;

View File

@ -252,9 +252,7 @@ ngx_process_events_and_timers(ngx_cycle_t *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);
}
if (ngx_accept_mutex_held) {
ngx_shmtx_unlock(&ngx_accept_mutex);
@ -264,13 +262,8 @@ ngx_process_events_and_timers(ngx_cycle_t *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_int_t
@ -612,6 +605,9 @@ ngx_event_process_init(ngx_cycle_t *cycle)
#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;
}

View File

@ -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

View File

@ -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);

View File

@ -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_ */

View File

@ -3372,7 +3372,7 @@ ngx_http_spdy_close_stream(ngx_http_spdy_stream_t *stream, ngx_int_t rc)
ngx_del_timer(ev);
}
if (ev->prev) {
if (ev->posted) {
ngx_delete_posted_event(ev);
}
@ -3387,7 +3387,7 @@ ngx_http_spdy_close_stream(ngx_http_spdy_stream_t *stream, ngx_int_t rc)
ngx_del_timer(ev);
}
if (ev->prev) {
if (ev->posted) {
ngx_delete_posted_event(ev);
}