summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorValentin Bartenev <vbart@nginx.com>2018-10-22 16:04:16 +0300
committerValentin Bartenev <vbart@nginx.com>2018-10-22 16:04:16 +0300
commitda0ef366dc1affa740618eb4f4baa8bbb62f8d70 (patch)
tree8eea7dedbfb8864d7281acb523a5f5dab68aec08 /src
parentb20e995e80d236945ec8388dfee37257ce9e5445 (diff)
downloadunit-da0ef366dc1affa740618eb4f4baa8bbb62f8d70.tar.gz
unit-da0ef366dc1affa740618eb4f4baa8bbb62f8d70.tar.bz2
Handling of timers with bias.
Timers that don't require maximum precision (most of them, actually) can be triggered earlier or later within the bias interval. To reduce wakeups by timers, the expire function now triggers not only all timers that fall within the elapsed time, but also those whose bias falls within this interval.
Diffstat (limited to 'src')
-rw-r--r--src/nxt_conn.h2
-rw-r--r--src/nxt_http_request.c2
-rw-r--r--src/nxt_router.c4
-rw-r--r--src/nxt_timer.c37
-rw-r--r--src/nxt_timer.h8
5 files changed, 30 insertions, 23 deletions
diff --git a/src/nxt_conn.h b/src/nxt_conn.h
index 6f6cae9c..d8b48694 100644
--- a/src/nxt_conn.h
+++ b/src/nxt_conn.h
@@ -175,7 +175,7 @@ struct nxt_conn_s {
do { \
(ev)->work_queue = (wq); \
(ev)->log = &(c)->log; \
- (ev)->precision = NXT_TIMER_DEFAULT_PRECISION; \
+ (ev)->bias = NXT_TIMER_DEFAULT_BIAS; \
} while (0)
diff --git a/src/nxt_http_request.c b/src/nxt_http_request.c
index 06d45247..b80998cb 100644
--- a/src/nxt_http_request.c
+++ b/src/nxt_http_request.c
@@ -192,7 +192,7 @@ nxt_http_app_request(nxt_task_t *task, void *obj, void *data)
ar->timer.task = &engine->task;
ar->timer.work_queue = &engine->fast_work_queue;
ar->timer.log = engine->task.log;
- ar->timer.precision = NXT_TIMER_DEFAULT_PRECISION;
+ ar->timer.bias = NXT_TIMER_DEFAULT_BIAS;
ar->r.remote.start = nxt_sockaddr_address(r->remote);
ar->r.remote.length = r->remote->address_length;
diff --git a/src/nxt_router.c b/src/nxt_router.c
index 3a5382e4..7ecbca81 100644
--- a/src/nxt_router.c
+++ b/src/nxt_router.c
@@ -1578,7 +1578,7 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
app_joint->use_count = 1;
app_joint->app = app;
- app_joint->idle_timer.precision = NXT_TIMER_DEFAULT_PRECISION;
+ app_joint->idle_timer.bias = NXT_TIMER_DEFAULT_BIAS;
app_joint->idle_timer.work_queue = &engine->fast_work_queue;
app_joint->idle_timer.handler = nxt_router_app_idle_timeout;
app_joint->idle_timer.task = &engine->task;
@@ -4119,7 +4119,7 @@ nxt_router_adjust_idle_timer(nxt_task_t *task, void *obj, void *data)
nxt_assert(app->engine == engine);
- threshold = engine->timers.now + app->joint->idle_timer.precision;
+ threshold = engine->timers.now + app->joint->idle_timer.bias;
timeout = 0;
nxt_thread_mutex_lock(&app->mutex);
diff --git a/src/nxt_timer.c b/src/nxt_timer.c
index 0b89dafc..cba4755b 100644
--- a/src/nxt_timer.c
+++ b/src/nxt_timer.c
@@ -75,7 +75,8 @@ nxt_timer_add(nxt_event_engine_t *engine, nxt_timer_t *timer,
time = engine->timers.now + timeout;
- nxt_debug(timer->task, "timer add: %M %M:%M", timer->time, timeout, time);
+ nxt_debug(timer->task, "timer add: %M±%d %M:%M",
+ timer->time, timer->bias, timeout, time);
timer->enabled = 1;
@@ -84,11 +85,12 @@ nxt_timer_add(nxt_event_engine_t *engine, nxt_timer_t *timer,
diff = nxt_msec_diff(time, timer->time);
/*
* Use the previous timer if difference between it and the
- * new timer is less than required precision milliseconds: this
- * decreases number of rbtree operations for fast connections.
+ * new timer is within bias: this decreases number of rbtree
+ * operations for fast connections.
*/
- if (nxt_abs(diff) < timer->precision) {
- nxt_debug(timer->task, "timer previous: %M", time);
+ if (nxt_abs(diff) <= timer->bias) {
+ nxt_debug(timer->task, "timer previous: %M±%d",
+ time, timer->bias);
nxt_timer_change(engine, timer, NXT_TIMER_NOPE, 0);
return;
@@ -102,7 +104,8 @@ nxt_timer_add(nxt_event_engine_t *engine, nxt_timer_t *timer,
nxt_bool_t
nxt_timer_delete(nxt_event_engine_t *engine, nxt_timer_t *timer)
{
- nxt_debug(timer->task, "timer delete: %M", timer->time);
+ nxt_debug(timer->task, "timer delete: %M±%d",
+ timer->time, timer->bias);
timer->enabled = 0;
@@ -142,7 +145,8 @@ nxt_timer_change(nxt_event_engine_t *engine, nxt_timer_t *timer,
timer->change = timers->nchanges;
}
- nxt_debug(timer->task, "timer change: %M:%d", time, change);
+ nxt_debug(timer->task, "timer change: %M±%d:%d",
+ time, timer->bias, change);
ch = &timers->changes[timer->change - 1];
@@ -190,7 +194,8 @@ nxt_timer_changes_commit(nxt_event_engine_t *engine)
/* Fall through. */
case NXT_TIMER_DELETE:
- nxt_debug(timer->task, "timer rbtree delete: %M", timer->time);
+ nxt_debug(timer->task, "timer rbtree delete: %M±%d",
+ timer->time, timer->bias);
nxt_rbtree_delete(&timers->tree, &timer->node);
nxt_timer_in_tree_clear(timer);
@@ -206,7 +211,8 @@ nxt_timer_changes_commit(nxt_event_engine_t *engine)
while (add < add_end) {
timer = *add;
- nxt_debug(timer->task, "timer rbtree insert: %M", timer->time);
+ nxt_debug(timer->task, "timer rbtree insert: %M±%d",
+ timer->time, timer->bias);
nxt_rbtree_insert(&timers->tree, &timer->node);
nxt_timer_in_tree_set(timer);
@@ -252,10 +258,10 @@ nxt_timer_find(nxt_event_engine_t *engine)
if (timer->enabled) {
time = timer->time;
- timers->minimum = time;
+ timers->minimum = time - timer->bias;
- nxt_debug(timer->task, "timer found minimum: %M:%M",
- time, timers->now);
+ nxt_debug(timer->task, "timer found minimum: %M±%d:%M",
+ time, timer->bias, timers->now);
delta = nxt_msec_diff(time, timers->now);
@@ -297,14 +303,15 @@ nxt_timer_expire(nxt_event_engine_t *engine, nxt_msec_t now)
{
timer = (nxt_timer_t *) node;
- /* timer->time > now */
- if (nxt_msec_diff(timer->time , now) > 0) {
+ /* timer->time > now + timer->bias */
+ if (nxt_msec_diff(timer->time , now) > (int32_t) timer->bias) {
return;
}
next = nxt_rbtree_node_successor(tree, node);
- nxt_debug(timer->task, "timer expire delete: %M", timer->time);
+ nxt_debug(timer->task, "timer expire delete: %M±%d",
+ timer->time, timer->bias);
nxt_rbtree_delete(tree, &timer->node);
nxt_timer_in_tree_clear(timer);
diff --git a/src/nxt_timer.h b/src/nxt_timer.h
index 211564d6..4199f0dd 100644
--- a/src/nxt_timer.h
+++ b/src/nxt_timer.h
@@ -8,9 +8,9 @@
#define _NXT_TIMER_H_INCLUDED_
-/* Valid values are between 1ms to 255ms. */
-#define NXT_TIMER_DEFAULT_PRECISION 100
-//#define NXT_TIMER_DEFAULT_PRECISION 1
+/* Valid values are between 0ms to 255ms. */
+#define NXT_TIMER_DEFAULT_BIAS 50
+//#define NXT_TIMER_DEFAULT_BIAS 0
/*
@@ -25,7 +25,7 @@ typedef struct {
/* The rbtree node must be the first field. */
NXT_RBTREE_NODE (node);
- uint8_t precision;
+ uint8_t bias;
uint16_t change:14;
uint16_t enabled:1;