Nginx : Get rid of "ff_host" thread, so single thread is used for both fstack and kernel.

This commit is contained in:
chenwei 2018-01-17 17:51:08 +08:00
parent e340d433ea
commit f4c5cf3972
11 changed files with 85 additions and 508 deletions

View File

@ -156,6 +156,13 @@ static ngx_command_t ngx_core_commands[] = {
0, 0,
offsetof(ngx_core_conf_t, fstack_conf), offsetof(ngx_core_conf_t, fstack_conf),
NULL }, NULL },
{ ngx_string("schedule_timeout"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
0,
offsetof(ngx_core_conf_t, schedule_timeout),
NULL },
#endif #endif
ngx_null_command ngx_null_command
@ -1037,6 +1044,10 @@ ngx_core_module_create_conf(ngx_cycle_t *cycle)
ccf->user = (ngx_uid_t) NGX_CONF_UNSET_UINT; ccf->user = (ngx_uid_t) NGX_CONF_UNSET_UINT;
ccf->group = (ngx_gid_t) NGX_CONF_UNSET_UINT; ccf->group = (ngx_gid_t) NGX_CONF_UNSET_UINT;
#if (NGX_HAVE_FSTACK)
ccf->schedule_timeout = NGX_CONF_UNSET_MSEC;
#endif
if (ngx_array_init(&ccf->env, cycle->pool, 1, sizeof(ngx_str_t)) if (ngx_array_init(&ccf->env, cycle->pool, 1, sizeof(ngx_str_t))
!= NGX_OK) != NGX_OK)
{ {
@ -1059,6 +1070,10 @@ ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf)
ngx_conf_init_value(ccf->worker_processes, 1); ngx_conf_init_value(ccf->worker_processes, 1);
ngx_conf_init_value(ccf->debug_points, 0); ngx_conf_init_value(ccf->debug_points, 0);
#if (NGX_HAVE_FSTACK)
ngx_conf_init_msec_value(ccf->schedule_timeout, 30);
#endif
#if (NGX_HAVE_CPU_AFFINITY) #if (NGX_HAVE_CPU_AFFINITY)
if (!ccf->cpu_affinity_auto if (!ccf->cpu_affinity_auto

View File

@ -116,6 +116,7 @@ typedef struct {
#if (NGX_HAVE_FSTACK) #if (NGX_HAVE_FSTACK)
ngx_str_t fstack_conf; ngx_str_t fstack_conf;
ngx_msec_t schedule_timeout;
#endif #endif
} ngx_core_conf_t; } ngx_core_conf_t;

View File

@ -36,8 +36,6 @@
static void * ngx_ff_host_event_create_conf(ngx_cycle_t *cycle); static void * ngx_ff_host_event_create_conf(ngx_cycle_t *cycle);
static char * ngx_ff_host_event_init_conf(ngx_cycle_t *cycle, static char * ngx_ff_host_event_init_conf(ngx_cycle_t *cycle,
void *conf); void *conf);
static ngx_int_t ngx_ff_host_event_init_process(ngx_cycle_t *cycle);
static void ngx_ff_host_event_exit_process(ngx_cycle_t *cycle);
static ngx_int_t ngx_ff_epoll_init(ngx_cycle_t *cycle, ngx_msec_t timer); static ngx_int_t ngx_ff_epoll_init(ngx_cycle_t *cycle, ngx_msec_t timer);
static ngx_int_t ngx_ff_epoll_add_event(ngx_event_t *ev, static ngx_int_t ngx_ff_epoll_add_event(ngx_event_t *ev,
ngx_int_t event, ngx_uint_t flags); ngx_int_t event, ngx_uint_t flags);
@ -45,39 +43,10 @@ static ngx_int_t ngx_ff_epoll_del_event(ngx_event_t *ev,
ngx_int_t event, ngx_uint_t flags); ngx_int_t event, ngx_uint_t flags);
static ngx_int_t ngx_ff_epoll_process_events(ngx_cycle_t *cycle, static ngx_int_t ngx_ff_epoll_process_events(ngx_cycle_t *cycle,
ngx_msec_t timer, ngx_uint_t flags); ngx_msec_t timer, ngx_uint_t flags);
static ngx_int_t ngx_ff_create_connection(ngx_cycle_t *cycle);
static void ngx_ff_delete_connection();
static void ngx_ff_worker_channel_handler(ngx_event_t *ev);
static void *ngx_ff_host_event_thread_main(void *args);
static ngx_int_t ngx_ff_add_channel_event(ngx_cycle_t *cycle,
ngx_fd_t fd, ngx_int_t event, ngx_event_handler_pt handler);
static void ngx_ff_process_events_and_timers(ngx_cycle_t *cycle);
ngx_int_t ngx_ff_start_worker_channel(ngx_cycle_t *cycle,
ngx_fd_t fd, ngx_int_t event);
void ngx_event_cancel_timers_of_host(void);
void ngx_event_expire_timers_of_host(void);
ngx_msec_t ngx_event_find_timer_of_host(void);
void ngx_event_cancel_timers_of_host(void);
struct channel_thread_args {
ngx_cycle_t *cycle;
ngx_fd_t fd;
ngx_int_t event;
ngx_event_handler_pt handler;
};
static pthread_t channel_thread;
static int thread_quit;
static int ep = -1; static int ep = -1;
static struct epoll_event *event_list; static struct epoll_event *event_list;
static ngx_uint_t nevents; static ngx_uint_t nevents;
static ngx_connection_t *channel_connection;
#include <semaphore.h>
static sem_t sem;
typedef struct { typedef struct {
ngx_uint_t events; ngx_uint_t events;
@ -101,10 +70,10 @@ ngx_module_t ngx_ff_host_event_module = {
NGX_CORE_MODULE, /* module type */ NGX_CORE_MODULE, /* module type */
NULL, /* init master */ NULL, /* init master */
NULL, /* init module */ NULL, /* init module */
ngx_ff_host_event_init_process, /* init process */ NULL, /* init process */
NULL, /* init thread */ NULL, /* init thread */
NULL, /* exit thread */ NULL, /* exit thread */
ngx_ff_host_event_exit_process, /* exit process */ NULL, /* exit process */
NULL, /* exit master */ NULL, /* exit master */
NGX_MODULE_V1_PADDING NGX_MODULE_V1_PADDING
}; };
@ -125,40 +94,17 @@ static char *
ngx_ff_host_event_init_conf(ngx_cycle_t *cycle, void *conf) ngx_ff_host_event_init_conf(ngx_cycle_t *cycle, void *conf)
{ {
ngx_ff_host_event_conf_t *cf = conf; ngx_ff_host_event_conf_t *cf = conf;
cf->events = 1; cf->events = 8;
return NGX_CONF_OK; return NGX_CONF_OK;
} }
static ngx_int_t ngx_ff_host_event_init_process(ngx_cycle_t *cycle)
{
if (sem_init(&sem, 0, 0) != 0)
{
return NGX_ERROR;
}
return NGX_OK;
}
static void ngx_ff_host_event_exit_process(ngx_cycle_t *cycle)
{
struct timespec ts;
if (clock_gettime( CLOCK_REALTIME,&ts ) < 0)
return;
//5s
ts.tv_sec += 4;
(void) sem_timedwait(&sem, &ts);
}
static ngx_int_t static ngx_int_t
ngx_ff_epoll_init(ngx_cycle_t *cycle, ngx_msec_t timer) ngx_ff_epoll_init(ngx_cycle_t *cycle, ngx_msec_t timer)
{ {
if (ep == -1) { if (ep == -1) {
ep = epoll_create(1); /* The size is just a hint */
ep = epoll_create(100);
if (ep == -1) { if (ep == -1) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
@ -176,12 +122,26 @@ ngx_ff_epoll_init(ngx_cycle_t *cycle, ngx_msec_t timer)
return NGX_ERROR; return NGX_ERROR;
} }
nevents = 1; nevents = 8;
channel_connection = NULL;
return NGX_OK; return NGX_OK;
} }
static void
ngx_ff_epoll_done(ngx_cycle_t *cycle)
{
if (close(ep) == -1) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"epoll close() failed");
}
ep = -1;
ngx_free(event_list);
event_list = NULL;
nevents = 0;
}
static ngx_int_t static ngx_int_t
ngx_ff_epoll_add_event(ngx_event_t *ev, ngx_int_t event, ngx_ff_epoll_add_event(ngx_event_t *ev, ngx_int_t event,
ngx_uint_t flags) ngx_uint_t flags)
@ -298,7 +258,6 @@ ngx_ff_epoll_del_event(ngx_event_t *ev, ngx_int_t event,
return NGX_OK; return NGX_OK;
} }
#if 0
static ngx_int_t static ngx_int_t
ngx_ff_epoll_add_connection(ngx_connection_t *c) ngx_ff_epoll_add_connection(ngx_connection_t *c)
{ {
@ -359,7 +318,6 @@ ngx_ff_epoll_del_connection(ngx_connection_t *c, ngx_uint_t flags)
return NGX_OK; return NGX_OK;
} }
#endif
static ngx_int_t static ngx_int_t
ngx_ff_epoll_process_events(ngx_cycle_t *cycle, ngx_ff_epoll_process_events(ngx_cycle_t *cycle,
@ -479,284 +437,17 @@ ngx_ff_epoll_process_events(ngx_cycle_t *cycle,
return NGX_OK; return NGX_OK;
} }
static ngx_int_t
ngx_ff_create_connection(ngx_cycle_t *cycle)
{
ngx_event_t *rev, *wev;
ngx_connection_t *c;
c = ngx_calloc(sizeof(ngx_connection_t), cycle->log);
if (c == NULL) {
return NGX_ERROR;
}
rev = ngx_calloc(sizeof(ngx_event_t), cycle->log);
if (rev == NULL) {
ngx_free(c);
return NGX_ERROR;
}
rev->index = NGX_INVALID_INDEX;
rev->data = c;
rev->log = cycle->log;
wev = ngx_calloc(sizeof(ngx_event_t), cycle->log);
if (wev == NULL) {
ngx_free(c);
ngx_free(rev);
return NGX_ERROR;
}
wev->index = NGX_INVALID_INDEX;
wev->write = 1;
wev->data = c;
wev->log = cycle->log;
c->pool = cycle->pool;
c->data = NULL;
c->read = rev;
c->write = wev;
c->fd = (ngx_socket_t) -1;
c->log = cycle->log;
channel_connection = c;
return NGX_OK;
}
static void
ngx_ff_delete_connection()
{
ngx_connection_t *c = channel_connection;
if (c == NULL) {
return;
}
if (c->read) {
ngx_free(c->read);
}
if (c->write) {
ngx_free(c->write);
}
ngx_free(c);
channel_connection = NULL;
}
static ngx_int_t
ngx_ff_add_channel_event(ngx_cycle_t *cycle, ngx_fd_t fd,
ngx_int_t event, ngx_event_handler_pt handler)
{
ngx_connection_t *c;
ngx_event_t *ev, *rev, *wev;
if (channel_connection != NULL) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"add channel event failed: already added");
return NGX_ERROR;
}
if (ngx_ff_create_connection(cycle) != NGX_OK) {
return NGX_ERROR;
}
c = channel_connection;
c->fd = fd;
rev = c->read;
wev = c->write;
rev->channel = 1;
wev->channel = 1;
rev->belong_to_host = wev->belong_to_host = 1;
ev = (event == NGX_READ_EVENT) ? rev : wev;
ev->handler = handler;
if (ngx_ff_epoll_add_event(ev, event, 0) == NGX_ERROR) {
return NGX_ERROR;
}
return NGX_OK;
}
static void
ngx_ff_worker_channel_handler(ngx_event_t *ev)
{
ngx_int_t n;
ngx_channel_t ch;
ngx_connection_t *c;
if (ev->timedout) {
ev->timedout = 0;
return;
}
c = ev->data;
ngx_log_debug0(NGX_LOG_DEBUG_CORE, ev->log, 0, "worker channel handler");
for ( ;; ) {
n = ngx_read_channel(c->fd, &ch, sizeof(ngx_channel_t), ev->log);
ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0, "channel: %i", n);
if (n == NGX_ERROR) {
ngx_ff_epoll_del_event(c->read, NGX_READ_EVENT, NGX_CLOSE_EVENT);
close(c->fd);
ngx_ff_delete_connection();
thread_quit = 1;
return;
}
if (n == NGX_AGAIN) {
return;
}
ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0,
"channel command: %ui", ch.command);
switch (ch.command) {
case NGX_CMD_QUIT:
ngx_quit = 1;
thread_quit = 1;
break;
case NGX_CMD_TERMINATE:
ngx_terminate = 1;
thread_quit = 1;
break;
case NGX_CMD_REOPEN:
ngx_reopen = 1;
break;
case NGX_CMD_OPEN_CHANNEL:
ngx_log_debug3(NGX_LOG_DEBUG_CORE, ev->log, 0,
"get channel s:%i pid:%P fd:%d",
ch.slot, ch.pid, ch.fd);
ngx_processes[ch.slot].pid = ch.pid;
ngx_processes[ch.slot].channel[0] = ch.fd;
break;
case NGX_CMD_CLOSE_CHANNEL:
ngx_log_debug4(NGX_LOG_DEBUG_CORE, ev->log, 0,
"close channel s:%i pid:%P our:%P fd:%d",
ch.slot, ch.pid, ngx_processes[ch.slot].pid,
ngx_processes[ch.slot].channel[0]);
if (close(ngx_processes[ch.slot].channel[0]) == -1) {
ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno,
"close() channel failed");
}
ngx_processes[ch.slot].channel[0] = -1;
break;
}
}
}
static void *
ngx_ff_host_event_thread_main(void *args)
{
struct channel_thread_args *cta = args;
ngx_cycle_t *cycle = cta->cycle;
if (ngx_ff_add_channel_event(cycle, cta->fd, cta->event,
cta->handler) != NGX_OK) {
return NULL;
}
for (;;) {
ngx_ff_process_events_and_timers(cycle);
if (thread_quit) {
break;
}
}
ngx_event_cancel_timers_of_host();
ngx_free(cta);
return NULL;
}
static void
ngx_ff_process_events_and_timers(ngx_cycle_t *cycle)
{
ngx_uint_t flags;
ngx_msec_t timer, delta;
timer = ngx_event_find_timer_of_host();
flags = NGX_UPDATE_TIME;
/* handle signals from master in case of network inactivity */
if (timer == NGX_TIMER_INFINITE || timer > 500) {
timer = 500;
}
delta = ngx_current_msec;
(void) ngx_ff_epoll_process_events(cycle, timer, flags);
delta = ngx_current_msec - delta;
ngx_event_process_posted(cycle, &ngx_posted_accept_events_of_host);
if (delta) {
ngx_event_expire_timers_of_host();
}
ngx_event_process_posted(cycle, &ngx_posted_events_of_host);
}
ngx_int_t
ngx_ff_start_worker_channel(ngx_cycle_t *cycle, ngx_fd_t fd,
ngx_int_t event)
{
int ret;
struct channel_thread_args *cta;
cta = ngx_alloc(sizeof(struct channel_thread_args), cycle->log);
if (cta == NULL) {
return NGX_ERROR;
}
cta->cycle = cycle;
cta->fd = fd;
cta->event = event;
cta->handler = ngx_ff_worker_channel_handler;
ret = pthread_create(&channel_thread, NULL,
ngx_ff_host_event_thread_main, (void *)cta);
if (ret != 0) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"pthread_create() failed");
return NGX_ERROR;
}
pthread_detach(channel_thread);
return NGX_OK;
}
ngx_event_actions_t ngx_ff_host_event_actions = { ngx_event_actions_t ngx_ff_host_event_actions = {
ngx_ff_epoll_add_event, /* add an event */ ngx_ff_epoll_add_event, /* add an event */
ngx_ff_epoll_del_event, /* delete an event */ ngx_ff_epoll_del_event, /* delete an event */
ngx_ff_epoll_add_event, /* enable an event */ ngx_ff_epoll_add_event, /* enable an event */
ngx_ff_epoll_add_event, /* disable an event */ ngx_ff_epoll_add_event, /* disable an event */
NULL, /* add an connection */ ngx_ff_epoll_add_connection, /* add an connection */
NULL, /* delete an connection */ ngx_ff_epoll_del_connection, /* delete an connection */
NULL, /* trigger a notify */ NULL, /* trigger a notify */
ngx_ff_epoll_process_events, /* process the events */ ngx_ff_epoll_process_events, /* process the events */
ngx_ff_epoll_init, /* init the events */ ngx_ff_epoll_init, /* init the events */
NULL, /* done the events */ ngx_ff_epoll_done, /* done the events */
}; };
#endif #endif

View File

@ -34,6 +34,10 @@ static char *ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd,
static void *ngx_event_core_create_conf(ngx_cycle_t *cycle); static void *ngx_event_core_create_conf(ngx_cycle_t *cycle);
static char *ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf); static char *ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf);
#if (NGX_HAVE_FSTACK)
extern ngx_int_t ngx_ff_epoll_process_events(ngx_cycle_t *cycle,
ngx_msec_t timer, ngx_uint_t flags);
#endif
static ngx_uint_t ngx_timer_resolution; static ngx_uint_t ngx_timer_resolution;
sig_atomic_t ngx_event_timer_alarm; sig_atomic_t ngx_event_timer_alarm;
@ -56,6 +60,10 @@ ngx_uint_t ngx_accept_mutex_held;
ngx_msec_t ngx_accept_mutex_delay; ngx_msec_t ngx_accept_mutex_delay;
ngx_int_t ngx_accept_disabled; ngx_int_t ngx_accept_disabled;
#if (NGX_HAVE_FSTACK)
static ngx_msec_t ngx_schedule_timeout;
#endif
#if (NGX_STAT_STUB) #if (NGX_STAT_STUB)
@ -195,6 +203,10 @@ ngx_process_events_and_timers(ngx_cycle_t *cycle)
{ {
ngx_uint_t flags; ngx_uint_t flags;
ngx_msec_t timer, delta; ngx_msec_t timer, delta;
#if (NGX_HAVE_FSTACK)
static ngx_uint_t tick;
static ngx_msec_t initial; //msec
#endif
if (ngx_timer_resolution) { if (ngx_timer_resolution) {
timer = NGX_TIMER_INFINITE; timer = NGX_TIMER_INFINITE;
@ -239,7 +251,19 @@ ngx_process_events_and_timers(ngx_cycle_t *cycle)
delta = ngx_current_msec; delta = ngx_current_msec;
(void) ngx_process_events(cycle, timer, flags); //(void) ngx_process_events(cycle, timer, flags);
#if (NGX_HAVE_FSTACK)
/* handle message from kernel (PS: signals from master) in case of network inactivity */
if (ngx_current_msec - initial >= ngx_schedule_timeout || tick >= ngx_schedule_timeout) {
(void) ngx_ff_process_host_events(cycle, 0, flags);
/* Update timer*/
initial = ngx_current_msec;
tick = 0;
}
tick++;
#endif
delta = ngx_current_msec - delta; delta = ngx_current_msec - delta;
@ -546,6 +570,10 @@ ngx_event_module_init(ngx_cycle_t *cycle)
#endif #endif
#if (NGX_HAVE_FSTACK)
ngx_schedule_timeout = ccf->schedule_timeout;
#endif
return NGX_OK; return NGX_OK;
} }
@ -607,11 +635,6 @@ ngx_event_process_init(ngx_cycle_t *cycle)
ngx_queue_init(&ngx_posted_accept_events); ngx_queue_init(&ngx_posted_accept_events);
ngx_queue_init(&ngx_posted_events); ngx_queue_init(&ngx_posted_events);
#if (NGX_HAVE_FSTACK)
ngx_queue_init(&ngx_posted_accept_events_of_host);
ngx_queue_init(&ngx_posted_events_of_host);
#endif
if (ngx_event_timer_init(cycle->log) == NGX_ERROR) { if (ngx_event_timer_init(cycle->log) == NGX_ERROR) {
return NGX_ERROR; return NGX_ERROR;
} }

View File

@ -453,6 +453,8 @@ static inline ngx_int_t ngx_process_events(
return ngx_event_actions.process_events(cycle, timer, flags); return ngx_event_actions.process_events(cycle, timer, flags);
} }
#define ngx_ff_process_host_events ngx_ff_host_event_actions.process_events
#else #else
#define ngx_process_events ngx_event_actions.process_events #define ngx_process_events ngx_event_actions.process_events

View File

@ -13,11 +13,6 @@
ngx_queue_t ngx_posted_accept_events; ngx_queue_t ngx_posted_accept_events;
ngx_queue_t ngx_posted_events; ngx_queue_t ngx_posted_events;
#if (NGX_HAVE_FSTACK)
ngx_queue_t ngx_posted_accept_events_of_host;
ngx_queue_t ngx_posted_events_of_host;
#endif
void void
ngx_event_process_posted(ngx_cycle_t *cycle, ngx_queue_t *posted) ngx_event_process_posted(ngx_cycle_t *cycle, ngx_queue_t *posted)

View File

@ -14,34 +14,6 @@
#include <ngx_event.h> #include <ngx_event.h>
#if (NGX_HAVE_FSTACK)
#define ngx_post_event(ev, q) \
\
if (!(ev)->posted) { \
(ev)->posted = 1; \
if (1 == (ev)->belong_to_host) { \
if (q == &ngx_posted_events) { \
ngx_queue_insert_tail( \
&ngx_posted_events_of_host, &(ev)->queue); \
} else if (q == &ngx_posted_accept_events) { \
ngx_queue_insert_tail( \
&ngx_posted_accept_events_of_host, &(ev)->queue); \
} else { \
ngx_log_error(NGX_LOG_EMERG, (ev)->log, 0, \
"ngx_post_event: unkowned posted queue"); \
exit(1); \
} \
} else { \
ngx_queue_insert_tail(q, &(ev)->queue); \
} \
\
ngx_log_debug1(NGX_LOG_DEBUG_CORE, (ev)->log, 0, "post event %p", ev);\
\
} else { \
ngx_log_debug1(NGX_LOG_DEBUG_CORE, (ev)->log, 0, \
"update posted event %p", ev); \
}
#else
#define ngx_post_event(ev, q) \ #define ngx_post_event(ev, q) \
\ \
if (!(ev)->posted) { \ if (!(ev)->posted) { \
@ -54,7 +26,6 @@
ngx_log_debug1(NGX_LOG_DEBUG_CORE, (ev)->log, 0, \ ngx_log_debug1(NGX_LOG_DEBUG_CORE, (ev)->log, 0, \
"update posted event %p", ev); \ "update posted event %p", ev); \
} }
#endif
#define ngx_delete_posted_event(ev) \ #define ngx_delete_posted_event(ev) \
@ -73,9 +44,5 @@ void ngx_event_process_posted(ngx_cycle_t *cycle, ngx_queue_t *posted);
extern ngx_queue_t ngx_posted_accept_events; extern ngx_queue_t ngx_posted_accept_events;
extern ngx_queue_t ngx_posted_events; extern ngx_queue_t ngx_posted_events;
#if (NGX_HAVE_FSTACK)
extern ngx_queue_t ngx_posted_accept_events_of_host;
extern ngx_queue_t ngx_posted_events_of_host;
#endif
#endif /* _NGX_EVENT_POSTED_H_INCLUDED_ */ #endif /* _NGX_EVENT_POSTED_H_INCLUDED_ */

View File

@ -13,11 +13,6 @@
ngx_rbtree_t ngx_event_timer_rbtree; ngx_rbtree_t ngx_event_timer_rbtree;
static ngx_rbtree_node_t ngx_event_timer_sentinel; static ngx_rbtree_node_t ngx_event_timer_sentinel;
#if (NGX_HAVE_FSTACK)
ngx_rbtree_t ngx_event_timer_rbtree_of_host;
static ngx_rbtree_node_t ngx_event_timer_sentinel_of_host;
#endif
/* /*
* the event timer rbtree may contain the duplicate keys, however, * the event timer rbtree may contain the duplicate keys, however,
* it should not be a problem, because we use the rbtree to find * it should not be a problem, because we use the rbtree to find
@ -30,54 +25,22 @@ ngx_event_timer_init(ngx_log_t *log)
ngx_rbtree_init(&ngx_event_timer_rbtree, &ngx_event_timer_sentinel, ngx_rbtree_init(&ngx_event_timer_rbtree, &ngx_event_timer_sentinel,
ngx_rbtree_insert_timer_value); ngx_rbtree_insert_timer_value);
#if (NGX_HAVE_FSTACK)
ngx_rbtree_init(&ngx_event_timer_rbtree_of_host, &ngx_event_timer_sentinel_of_host,
ngx_rbtree_insert_timer_value);
#endif
return NGX_OK; return NGX_OK;
} }
#if (NGX_HAVE_FSTACK)
ngx_msec_t
ngx_event_find_timer_internal(
ngx_rbtree_t *rbtree, ngx_rbtree_node_t *sentinel);
ngx_msec_t ngx_msec_t
ngx_event_find_timer(void) ngx_event_find_timer(void)
{ {
return ngx_event_find_timer_internal(&ngx_event_timer_rbtree, &ngx_event_timer_sentinel);
}
ngx_msec_t
ngx_event_find_timer_of_host(void)
{
return ngx_event_find_timer_internal(&ngx_event_timer_rbtree_of_host, &ngx_event_timer_sentinel_of_host);
}
ngx_msec_t
ngx_event_find_timer_internal(
ngx_rbtree_t *rbtree, ngx_rbtree_node_t *rbtree_sentinel)
{
#else
ngx_msec_t
ngx_event_find_timer(void)
{
ngx_rbtree_t * rbtree = &ngx_event_timer_rbtree;
ngx_rbtree_node_t *rbtree_sentinel = &ngx_event_timer_sentinel;
#endif
ngx_msec_int_t timer; ngx_msec_int_t timer;
ngx_rbtree_node_t *node, *root, *sentinel; ngx_rbtree_node_t *node, *root, *sentinel;
if (rbtree->root == rbtree_sentinel) { if (ngx_event_timer_rbtree.root == &ngx_event_timer_sentinel) {
return NGX_TIMER_INFINITE; return NGX_TIMER_INFINITE;
} }
root = rbtree->root; root = ngx_event_timer_rbtree.root;
sentinel = rbtree->sentinel; sentinel = ngx_event_timer_rbtree.sentinel;
node = ngx_rbtree_min(root, sentinel); node = ngx_rbtree_min(root, sentinel);
@ -87,39 +50,16 @@ ngx_event_find_timer(void)
} }
#if (NGX_HAVE_FSTACK)
void
ngx_event_expire_timers_internal(ngx_rbtree_t *rbtree);
void void
ngx_event_expire_timers(void) ngx_event_expire_timers(void)
{ {
ngx_event_expire_timers_internal(&ngx_event_timer_rbtree);
}
void
ngx_event_expire_timers_of_host(void)
{
ngx_event_expire_timers_internal(&ngx_event_timer_rbtree_of_host);
}
void
ngx_event_expire_timers_internal(ngx_rbtree_t *rbtree)
{
#else
void
ngx_event_expire_timers(void)
{
ngx_rbtree_t * rbtree = &ngx_event_timer_rbtree;
#endif
ngx_event_t *ev; ngx_event_t *ev;
ngx_rbtree_node_t *node, *root, *sentinel; ngx_rbtree_node_t *node, *root, *sentinel;
sentinel = rbtree->sentinel; sentinel = ngx_event_timer_rbtree.sentinel;
for ( ;; ) { for ( ;; ) {
root = rbtree->root; root = ngx_event_timer_rbtree.root;
if (root == sentinel) { if (root == sentinel) {
return; return;
@ -139,7 +79,7 @@ ngx_event_expire_timers(void)
"event timer del: %d: %M", "event timer del: %d: %M",
ngx_event_ident(ev->data), ev->timer.key); ngx_event_ident(ev->data), ev->timer.key);
ngx_rbtree_delete(rbtree, &ev->timer); ngx_rbtree_delete(&ngx_event_timer_rbtree, &ev->timer);
#if (NGX_DEBUG) #if (NGX_DEBUG)
ev->timer.left = NULL; ev->timer.left = NULL;
@ -156,39 +96,16 @@ ngx_event_expire_timers(void)
} }
#if (NGX_HAVE_FSTACK)
void
ngx_event_cancel_timers_internal(ngx_rbtree_t *rbtree);
void void
ngx_event_cancel_timers(void) ngx_event_cancel_timers(void)
{ {
ngx_event_cancel_timers_internal(&ngx_event_timer_rbtree);
}
void
ngx_event_cancel_timers_of_host(void)
{
ngx_event_cancel_timers_internal(&ngx_event_timer_rbtree_of_host);
}
void
ngx_event_cancel_timers_internal(ngx_rbtree_t *rbtree)
{
#else
void
ngx_event_cancel_timers(void)
{
ngx_rbtree_t * rbtree = &ngx_event_timer_rbtree;
#endif
ngx_event_t *ev; ngx_event_t *ev;
ngx_rbtree_node_t *node, *root, *sentinel; ngx_rbtree_node_t *node, *root, *sentinel;
sentinel = rbtree->sentinel; sentinel = ngx_event_timer_rbtree.sentinel;
for ( ;; ) { for ( ;; ) {
root = rbtree->root; root = ngx_event_timer_rbtree.root;
if (root == sentinel) { if (root == sentinel) {
return; return;
@ -206,7 +123,7 @@ ngx_event_cancel_timers(void)
"event timer cancel: %d: %M", "event timer cancel: %d: %M",
ngx_event_ident(ev->data), ev->timer.key); ngx_event_ident(ev->data), ev->timer.key);
ngx_rbtree_delete(rbtree, &ev->timer); ngx_rbtree_delete(&ngx_event_timer_rbtree, &ev->timer);
#if (NGX_DEBUG) #if (NGX_DEBUG)
ev->timer.left = NULL; ev->timer.left = NULL;

View File

@ -27,9 +27,6 @@ void ngx_event_cancel_timers(void);
extern ngx_rbtree_t ngx_event_timer_rbtree; extern ngx_rbtree_t ngx_event_timer_rbtree;
#if (NGX_HAVE_FSTACK)
extern ngx_rbtree_t ngx_event_timer_rbtree_of_host;
#endif
static ngx_inline void static ngx_inline void
ngx_event_del_timer(ngx_event_t *ev) ngx_event_del_timer(ngx_event_t *ev)
@ -38,20 +35,8 @@ ngx_event_del_timer(ngx_event_t *ev)
"event timer del: %d: %M", "event timer del: %d: %M",
ngx_event_ident(ev->data), ev->timer.key); ngx_event_ident(ev->data), ev->timer.key);
#if (NGX_HAVE_FSTACK)
if(ev->belong_to_host){
ngx_rbtree_delete(&ngx_event_timer_rbtree_of_host, &ev->timer);
} else {
ngx_rbtree_delete(&ngx_event_timer_rbtree, &ev->timer);
}
#else
ngx_rbtree_delete(&ngx_event_timer_rbtree, &ev->timer); ngx_rbtree_delete(&ngx_event_timer_rbtree, &ev->timer);
#endif
#if (NGX_DEBUG) #if (NGX_DEBUG)
ev->timer.left = NULL; ev->timer.left = NULL;
ev->timer.right = NULL; ev->timer.right = NULL;
@ -96,20 +81,8 @@ ngx_event_add_timer(ngx_event_t *ev, ngx_msec_t timer)
"event timer add: %d: %M:%M", "event timer add: %d: %M:%M",
ngx_event_ident(ev->data), timer, ev->timer.key); ngx_event_ident(ev->data), timer, ev->timer.key);
#if (NGX_HAVE_FSTACK)
if(ev->belong_to_host){
ngx_rbtree_insert(&ngx_event_timer_rbtree_of_host, &ev->timer);
} else {
ngx_rbtree_insert(&ngx_event_timer_rbtree, &ev->timer);
}
#else
ngx_rbtree_insert(&ngx_event_timer_rbtree, &ev->timer); ngx_rbtree_insert(&ngx_event_timer_rbtree, &ev->timer);
#endif
ev->timer_set = 1; ev->timer_set = 1;
} }

View File

@ -213,6 +213,10 @@ ngx_add_channel_event(ngx_cycle_t *cycle, ngx_fd_t fd, ngx_int_t event,
rev = c->read; rev = c->read;
wev = c->write; wev = c->write;
#if (NGX_HAVE_FSTACK)
rev->belong_to_host = wev->belong_to_host = 1;
#endif
rev->log = cycle->log; rev->log = cycle->log;
wev->log = cycle->log; wev->log = cycle->log;

View File

@ -22,12 +22,7 @@ static void ngx_master_process_exit(ngx_cycle_t *cycle);
static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data); static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data);
static void ngx_worker_process_init(ngx_cycle_t *cycle, ngx_int_t worker); static void ngx_worker_process_init(ngx_cycle_t *cycle, ngx_int_t worker);
static void ngx_worker_process_exit(ngx_cycle_t *cycle); static void ngx_worker_process_exit(ngx_cycle_t *cycle);
#if (NGX_HAVE_FSTACK)
extern ngx_int_t ngx_ff_start_worker_channel(ngx_cycle_t *cycle,
ngx_fd_t fd, ngx_int_t event);
#else
static void ngx_channel_handler(ngx_event_t *ev); static void ngx_channel_handler(ngx_event_t *ev);
#endif
static void ngx_cache_manager_process_cycle(ngx_cycle_t *cycle, void *data); static void ngx_cache_manager_process_cycle(ngx_cycle_t *cycle, void *data);
static void ngx_cache_manager_process_handler(ngx_event_t *ev); static void ngx_cache_manager_process_handler(ngx_event_t *ev);
static void ngx_cache_loader_process_handler(ngx_event_t *ev); static void ngx_cache_loader_process_handler(ngx_event_t *ev);
@ -1057,12 +1052,8 @@ ngx_worker_process_init(ngx_cycle_t *cycle, ngx_int_t worker)
ngx_last_process = 0; ngx_last_process = 0;
#endif #endif
#if (NGX_HAVE_FSTACK)
if (ngx_ff_start_worker_channel(cycle, ngx_channel, NGX_READ_EVENT)
#else
if (ngx_add_channel_event(cycle, ngx_channel, NGX_READ_EVENT, if (ngx_add_channel_event(cycle, ngx_channel, NGX_READ_EVENT,
ngx_channel_handler) ngx_channel_handler)
#endif
== NGX_ERROR) == NGX_ERROR)
{ {
/* fatal */ /* fatal */
@ -1137,7 +1128,6 @@ ngx_worker_process_exit(ngx_cycle_t *cycle)
exit(0); exit(0);
} }
#if (!NGX_HAVE_FSTACK)
static void static void
ngx_channel_handler(ngx_event_t *ev) ngx_channel_handler(ngx_event_t *ev)
{ {
@ -1224,7 +1214,6 @@ ngx_channel_handler(ngx_event_t *ev)
} }
} }
} }
#endif
static void static void
ngx_cache_manager_process_cycle(ngx_cycle_t *cycle, void *data) ngx_cache_manager_process_cycle(ngx_cycle_t *cycle, void *data)