2006-06-25 20:48:02 -04:00
|
|
|
/*
|
|
|
|
|
* Task management functions.
|
|
|
|
|
*
|
2009-03-07 11:25:21 -05:00
|
|
|
* Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
|
2006-06-25 20:48:02 -04:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2009-03-08 17:25:28 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
|
2006-06-29 11:53:05 -04:00
|
|
|
#include <common/config.h>
|
2007-05-13 13:43:47 -04:00
|
|
|
#include <common/memory.h>
|
2006-06-29 11:53:05 -04:00
|
|
|
#include <common/mini-clist.h>
|
2007-04-29 04:41:56 -04:00
|
|
|
#include <common/standard.h>
|
2007-04-28 16:40:08 -04:00
|
|
|
#include <common/time.h>
|
MAJOR: task: make use of the scope-aware ebtree functions
Currently the task scheduler suffers from an O(n) lookup when
skipping tasks that are not for the current thread. The reason
is that eb32_lookup_ge() has no information about the current
thread so it always revisits many tasks for other threads before
finding its own tasks.
This is particularly visible with HTTP/2 since the number of
concurrent streams created at once causes long series of tasks
for the same stream in the scheduler. With only 10 connections
and 100 streams each, by running on two threads, the performance
drops from 640kreq/s to 11.2kreq/s! Lookup metrics show that for
only 200000 task lookups, 430 million skips had to be performed,
which means that on average, each lookup leads to 2150 nodes to
be visited.
This commit backports the principle of scope lookups for ebtrees
from the ebtree_v7 development tree. The idea is that each node
contains a mask indicating the union of the scopes for the nodes
below it, which is fed during insertion, and used during lookups.
Then during lookups, branches that do not contain any leaf matching
the requested scope are simply ignored. This perfectly matches a
thread mask, allowing a thread to only extract the tasks it cares
about from the run queue, and to always find them in O(log(n))
instead of O(n). Thus the scheduler uses tid_bit and
task->thread_mask as the ebtree scope here.
Doing this has recovered most of the performance, as can be seen on
the test below with two threads, 10 connections, 100 streams each,
and 1 million requests total :
Before After Gain
test duration : 89.6s 4.73s x19
HTTP requests/s (DEBUG) : 11200 211300 x19
HTTP requests/s (PROD) : 15900 447000 x28
spin_lock time : 85.2s 0.46s /185
time per lookup : 13us 40ns /325
Even when going to 6 threads (on 3 hyperthreaded CPU cores), the
performance stays around 284000 req/s, showing that the contention
is much lower.
A test showed that there's no benefit in using this for the wait queue
though.
2017-11-05 07:34:20 -05:00
|
|
|
#include <eb32sctree.h>
|
2009-10-26 16:10:04 -04:00
|
|
|
#include <eb32tree.h>
|
2006-06-25 20:48:02 -04:00
|
|
|
|
2007-05-12 16:35:00 -04:00
|
|
|
#include <proto/proxy.h>
|
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
|
|
|
#include <proto/stream.h>
|
2006-06-25 20:48:02 -04:00
|
|
|
#include <proto/task.h>
|
2007-04-29 04:41:56 -04:00
|
|
|
|
2017-11-24 11:34:44 -05:00
|
|
|
struct pool_head *pool_head_task;
|
2006-06-25 20:48:02 -04:00
|
|
|
|
2017-07-12 08:31:10 -04:00
|
|
|
/* This is the memory pool containing all the signal structs. These
|
|
|
|
|
* struct are used to store each requiered signal between two tasks.
|
|
|
|
|
*/
|
2017-11-24 11:34:44 -05:00
|
|
|
struct pool_head *pool_head_notification;
|
2017-07-12 08:31:10 -04:00
|
|
|
|
2009-03-21 13:13:21 -04:00
|
|
|
unsigned int nb_tasks = 0;
|
2017-11-14 04:26:53 -05:00
|
|
|
unsigned long active_tasks_mask = 0; /* Mask of threads with active tasks */
|
2016-12-06 03:15:30 -05:00
|
|
|
unsigned int tasks_run_queue = 0;
|
|
|
|
|
unsigned int tasks_run_queue_cur = 0; /* copy of the run queue size */
|
2009-03-21 13:33:52 -04:00
|
|
|
unsigned int nb_tasks_cur = 0; /* copy of the tasks count */
|
2009-03-21 05:01:42 -04:00
|
|
|
unsigned int niced_tasks = 0; /* number of niced tasks in the run queue */
|
2017-03-30 09:37:25 -04:00
|
|
|
|
2017-11-26 04:08:06 -05:00
|
|
|
THREAD_LOCAL struct task *curr_task = NULL; /* task currently running or NULL */
|
|
|
|
|
|
2017-11-26 04:19:16 -05:00
|
|
|
__decl_hathreads(HA_SPINLOCK_T __attribute__((aligned(64))) rq_lock); /* spin lock related to run queue */
|
|
|
|
|
__decl_hathreads(HA_SPINLOCK_T __attribute__((aligned(64))) wq_lock); /* spin lock related to wait queue */
|
2006-06-25 20:48:02 -04:00
|
|
|
|
2009-03-21 05:01:42 -04:00
|
|
|
static struct eb_root timers; /* sorted timers tree */
|
|
|
|
|
static struct eb_root rqueue; /* tree constituting the run queue */
|
|
|
|
|
static unsigned int rqueue_ticks; /* insertion count */
|
2008-06-24 02:17:16 -04:00
|
|
|
|
2009-03-07 11:25:21 -05:00
|
|
|
/* Puts the task <t> in run queue at a position depending on t->nice. <t> is
|
|
|
|
|
* returned. The nice value assigns boosts in 32th of the run queue size. A
|
2016-12-06 03:15:30 -05:00
|
|
|
* nice value of -1024 sets the task to -tasks_run_queue*32, while a nice value
|
|
|
|
|
* of 1024 sets the task to tasks_run_queue*32. The state flags are cleared, so
|
|
|
|
|
* the caller will have to set its flags after this call.
|
2009-03-07 11:25:21 -05:00
|
|
|
* The task must not already be in the run queue. If unsure, use the safer
|
|
|
|
|
* task_wakeup() function.
|
2008-06-30 01:51:00 -04:00
|
|
|
*/
|
2008-08-29 09:26:14 -04:00
|
|
|
struct task *__task_wakeup(struct task *t)
|
2007-04-30 07:15:14 -04:00
|
|
|
{
|
2016-12-06 03:15:30 -05:00
|
|
|
tasks_run_queue++;
|
2017-11-14 04:26:53 -05:00
|
|
|
active_tasks_mask |= t->thread_mask;
|
2009-03-07 11:25:21 -05:00
|
|
|
t->rq.key = ++rqueue_ticks;
|
2008-06-30 01:51:00 -04:00
|
|
|
|
|
|
|
|
if (likely(t->nice)) {
|
|
|
|
|
int offset;
|
|
|
|
|
|
|
|
|
|
niced_tasks++;
|
|
|
|
|
if (likely(t->nice > 0))
|
2016-12-06 03:15:30 -05:00
|
|
|
offset = (unsigned)((tasks_run_queue * (unsigned int)t->nice) / 32U);
|
2008-06-30 01:51:00 -04:00
|
|
|
else
|
2016-12-06 03:15:30 -05:00
|
|
|
offset = -(unsigned)((tasks_run_queue * (unsigned int)-t->nice) / 32U);
|
2009-03-07 11:25:21 -05:00
|
|
|
t->rq.key += offset;
|
2008-06-30 01:51:00 -04:00
|
|
|
}
|
|
|
|
|
|
2017-03-30 09:37:25 -04:00
|
|
|
/* reset flag to pending ones
|
|
|
|
|
* Note: __task_wakeup must not be called
|
|
|
|
|
* if task is running
|
|
|
|
|
*/
|
|
|
|
|
t->state = t->pending_state;
|
MAJOR: task: make use of the scope-aware ebtree functions
Currently the task scheduler suffers from an O(n) lookup when
skipping tasks that are not for the current thread. The reason
is that eb32_lookup_ge() has no information about the current
thread so it always revisits many tasks for other threads before
finding its own tasks.
This is particularly visible with HTTP/2 since the number of
concurrent streams created at once causes long series of tasks
for the same stream in the scheduler. With only 10 connections
and 100 streams each, by running on two threads, the performance
drops from 640kreq/s to 11.2kreq/s! Lookup metrics show that for
only 200000 task lookups, 430 million skips had to be performed,
which means that on average, each lookup leads to 2150 nodes to
be visited.
This commit backports the principle of scope lookups for ebtrees
from the ebtree_v7 development tree. The idea is that each node
contains a mask indicating the union of the scopes for the nodes
below it, which is fed during insertion, and used during lookups.
Then during lookups, branches that do not contain any leaf matching
the requested scope are simply ignored. This perfectly matches a
thread mask, allowing a thread to only extract the tasks it cares
about from the run queue, and to always find them in O(log(n))
instead of O(n). Thus the scheduler uses tid_bit and
task->thread_mask as the ebtree scope here.
Doing this has recovered most of the performance, as can be seen on
the test below with two threads, 10 connections, 100 streams each,
and 1 million requests total :
Before After Gain
test duration : 89.6s 4.73s x19
HTTP requests/s (DEBUG) : 11200 211300 x19
HTTP requests/s (PROD) : 15900 447000 x28
spin_lock time : 85.2s 0.46s /185
time per lookup : 13us 40ns /325
Even when going to 6 threads (on 3 hyperthreaded CPU cores), the
performance stays around 284000 req/s, showing that the contention
is much lower.
A test showed that there's no benefit in using this for the wait queue
though.
2017-11-05 07:34:20 -05:00
|
|
|
eb32sc_insert(&rqueue, &t->rq, t->thread_mask);
|
2008-06-29 16:40:23 -04:00
|
|
|
return t;
|
2007-04-30 07:15:14 -04:00
|
|
|
}
|
2007-05-12 16:35:00 -04:00
|
|
|
|
2007-04-29 04:41:56 -04:00
|
|
|
/*
|
2009-03-08 11:35:27 -04:00
|
|
|
* __task_queue()
|
2007-04-29 04:41:56 -04:00
|
|
|
*
|
|
|
|
|
* Inserts a task into the wait queue at the position given by its expiration
|
2009-03-07 11:25:21 -05:00
|
|
|
* date. It does not matter if the task was already in the wait queue or not,
|
2009-03-08 11:35:27 -04:00
|
|
|
* as it will be unlinked. The task must not have an infinite expiration timer.
|
2009-03-21 05:01:42 -04:00
|
|
|
* Last, tasks must not be queued further than the end of the tree, which is
|
|
|
|
|
* between <now_ms> and <now_ms> + 2^31 ms (now+24days in 32bit).
|
2009-03-08 11:35:27 -04:00
|
|
|
*
|
|
|
|
|
* This function should not be used directly, it is meant to be called by the
|
|
|
|
|
* inline version of task_queue() which performs a few cheap preliminary tests
|
|
|
|
|
* before deciding to call __task_queue().
|
2007-04-29 04:41:56 -04:00
|
|
|
*/
|
2009-03-08 11:35:27 -04:00
|
|
|
void __task_queue(struct task *task)
|
2006-06-25 20:48:02 -04:00
|
|
|
{
|
2009-03-08 11:35:27 -04:00
|
|
|
if (likely(task_in_wq(task)))
|
2009-03-07 11:25:21 -05:00
|
|
|
__task_unlink_wq(task);
|
|
|
|
|
|
|
|
|
|
/* the task is not in the queue now */
|
2009-03-21 05:01:42 -04:00
|
|
|
task->wq.key = task->expire;
|
2008-06-29 11:00:59 -04:00
|
|
|
#ifdef DEBUG_CHECK_INVALID_EXPIRATION_DATES
|
2009-03-21 05:01:42 -04:00
|
|
|
if (tick_is_lt(task->wq.key, now_ms))
|
2008-06-29 11:00:59 -04:00
|
|
|
/* we're queuing too far away or in the past (most likely) */
|
2009-03-07 11:25:21 -05:00
|
|
|
return;
|
2008-06-29 11:00:59 -04:00
|
|
|
#endif
|
2008-07-05 12:16:19 -04:00
|
|
|
|
2009-03-21 05:01:42 -04:00
|
|
|
eb32_insert(&timers, &task->wq);
|
[BUG] task.c: don't assing last_timer to node-less entries
I noticed that in __eb32_insert , if the tree is empty
(root->b[EB_LEFT] == NULL) , the node.bit is not defined.
However in __task_queue there are checks:
- if (last_timer->node.bit < 0)
- if (task->wq.node.bit < last_timer->node.bit)
which might rely upon an undefined value.
This is how I see it:
1. We insert eb32_node in an empty wait queue tree for a task (called by
process_runnable_tasks() ):
Inserting into empty wait queue &task->wq = 0x72a87c8, last_timer
pointer: (nil)
2. Then, we set the last timer to the same address:
Setting last_timer: (nil) to: 0x72a87c8
3. We get a new task to be inserted in the queue (again called by
process_runnable_tasks()) , before the __task_unlink_wq() is called for
the previous task.
4. At this point, we still have last_timer set to 0x72a87c8 , but since
it was inserted in an empty tree, it doesn't have node.bit and the
values above get dereferenced with undefined value.
The bug has no effect right now because the check for equality is still
made, so the next timer will still be queued at the right place anyway,
without any possible side-effect. But it's a pending bug waiting for a
small change somewhere to strike.
Iliya Polihronov
2009-10-06 11:53:37 -04:00
|
|
|
|
2009-03-07 11:25:21 -05:00
|
|
|
return;
|
2007-04-29 04:41:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2008-06-24 02:17:16 -04:00
|
|
|
* Extract all expired timers from the timer queue, and wakes up all
|
2014-12-15 07:26:01 -05:00
|
|
|
* associated tasks. Returns the date of next event (or eternity).
|
2007-04-29 04:41:56 -04:00
|
|
|
*/
|
2014-12-15 07:26:01 -05:00
|
|
|
int wake_expired_tasks()
|
2007-04-29 04:41:56 -04:00
|
|
|
{
|
|
|
|
|
struct task *task;
|
2008-06-24 02:17:16 -04:00
|
|
|
struct eb32_node *eb;
|
2017-11-05 13:09:27 -05:00
|
|
|
int ret = TICK_ETERNITY;
|
2008-06-29 11:00:59 -04:00
|
|
|
|
2009-03-21 05:01:42 -04:00
|
|
|
while (1) {
|
2017-11-07 04:42:54 -05:00
|
|
|
HA_SPIN_LOCK(TASK_WQ_LOCK, &wq_lock);
|
2017-09-27 08:59:38 -04:00
|
|
|
lookup_next:
|
2017-03-30 09:37:25 -04:00
|
|
|
eb = eb32_lookup_ge(&timers, now_ms - TIMER_LOOK_BACK);
|
2017-09-27 08:59:38 -04:00
|
|
|
if (!eb) {
|
2009-03-21 05:01:42 -04:00
|
|
|
/* we might have reached the end of the tree, typically because
|
|
|
|
|
* <now_ms> is in the first half and we're first scanning the last
|
|
|
|
|
* half. Let's loop back to the beginning of the tree now.
|
|
|
|
|
*/
|
|
|
|
|
eb = eb32_first(&timers);
|
2017-11-05 13:09:27 -05:00
|
|
|
if (likely(!eb))
|
2009-03-21 05:01:42 -04:00
|
|
|
break;
|
|
|
|
|
}
|
2008-06-29 11:00:59 -04:00
|
|
|
|
2017-11-05 13:09:27 -05:00
|
|
|
if (tick_is_lt(now_ms, eb->key)) {
|
2009-03-21 05:01:42 -04:00
|
|
|
/* timer not expired yet, revisit it later */
|
2017-11-05 13:09:27 -05:00
|
|
|
ret = eb->key;
|
|
|
|
|
break;
|
2009-03-21 05:01:42 -04:00
|
|
|
}
|
2008-06-24 02:17:16 -04:00
|
|
|
|
2009-03-21 05:01:42 -04:00
|
|
|
/* timer looks expired, detach it from the queue */
|
|
|
|
|
task = eb32_entry(eb, struct task, wq);
|
|
|
|
|
__task_unlink_wq(task);
|
2009-03-08 01:46:27 -05:00
|
|
|
|
2009-03-21 05:01:42 -04:00
|
|
|
/* It is possible that this task was left at an earlier place in the
|
|
|
|
|
* tree because a recent call to task_queue() has not moved it. This
|
|
|
|
|
* happens when the new expiration date is later than the old one.
|
|
|
|
|
* Since it is very unlikely that we reach a timeout anyway, it's a
|
|
|
|
|
* lot cheaper to proceed like this because we almost never update
|
|
|
|
|
* the tree. We may also find disabled expiration dates there. Since
|
|
|
|
|
* we have detached the task from the tree, we simply call task_queue
|
2009-07-14 17:48:55 -04:00
|
|
|
* to take care of this. Note that we might occasionally requeue it at
|
|
|
|
|
* the same place, before <eb>, so we have to check if this happens,
|
|
|
|
|
* and adjust <eb>, otherwise we may skip it which is not what we want.
|
2009-08-09 03:09:54 -04:00
|
|
|
* We may also not requeue the task (and not point eb at it) if its
|
|
|
|
|
* expiration time is not set.
|
2009-03-21 05:01:42 -04:00
|
|
|
*/
|
|
|
|
|
if (!tick_is_expired(task->expire, now_ms)) {
|
2017-11-05 13:09:27 -05:00
|
|
|
if (tick_isset(task->expire))
|
|
|
|
|
__task_queue(task);
|
2017-09-27 08:59:38 -04:00
|
|
|
goto lookup_next;
|
2006-06-25 20:48:02 -04:00
|
|
|
}
|
2009-03-21 05:01:42 -04:00
|
|
|
task_wakeup(task, TASK_WOKEN_TIMER);
|
2017-11-23 12:36:50 -05:00
|
|
|
HA_SPIN_UNLOCK(TASK_WQ_LOCK, &wq_lock);
|
2009-03-21 05:01:42 -04:00
|
|
|
}
|
2008-06-24 02:17:16 -04:00
|
|
|
|
2017-11-07 04:42:54 -05:00
|
|
|
HA_SPIN_UNLOCK(TASK_WQ_LOCK, &wq_lock);
|
2017-11-05 13:09:27 -05:00
|
|
|
return ret;
|
2006-06-25 20:48:02 -04:00
|
|
|
}
|
|
|
|
|
|
2008-06-29 16:40:23 -04:00
|
|
|
/* The run queue is chronologically sorted in a tree. An insertion counter is
|
|
|
|
|
* used to assign a position to each task. This counter may be combined with
|
|
|
|
|
* other variables (eg: nice value) to set the final position in the tree. The
|
|
|
|
|
* counter may wrap without a problem, of course. We then limit the number of
|
2017-11-14 04:38:36 -05:00
|
|
|
* tasks processed to 200 in any case, so that general latency remains low and
|
|
|
|
|
* so that task positions have a chance to be considered.
|
2006-06-25 20:48:02 -04:00
|
|
|
*
|
2008-06-29 16:40:23 -04:00
|
|
|
* The function adjusts <next> if a new event is closer.
|
2006-06-25 20:48:02 -04:00
|
|
|
*/
|
2014-12-15 07:26:01 -05:00
|
|
|
void process_runnable_tasks()
|
2006-06-25 20:48:02 -04:00
|
|
|
{
|
2007-01-06 18:38:00 -05:00
|
|
|
struct task *t;
|
2017-03-30 09:37:25 -04:00
|
|
|
int i;
|
|
|
|
|
int max_processed;
|
MAJOR: task: make use of the scope-aware ebtree functions
Currently the task scheduler suffers from an O(n) lookup when
skipping tasks that are not for the current thread. The reason
is that eb32_lookup_ge() has no information about the current
thread so it always revisits many tasks for other threads before
finding its own tasks.
This is particularly visible with HTTP/2 since the number of
concurrent streams created at once causes long series of tasks
for the same stream in the scheduler. With only 10 connections
and 100 streams each, by running on two threads, the performance
drops from 640kreq/s to 11.2kreq/s! Lookup metrics show that for
only 200000 task lookups, 430 million skips had to be performed,
which means that on average, each lookup leads to 2150 nodes to
be visited.
This commit backports the principle of scope lookups for ebtrees
from the ebtree_v7 development tree. The idea is that each node
contains a mask indicating the union of the scopes for the nodes
below it, which is fed during insertion, and used during lookups.
Then during lookups, branches that do not contain any leaf matching
the requested scope are simply ignored. This perfectly matches a
thread mask, allowing a thread to only extract the tasks it cares
about from the run queue, and to always find them in O(log(n))
instead of O(n). Thus the scheduler uses tid_bit and
task->thread_mask as the ebtree scope here.
Doing this has recovered most of the performance, as can be seen on
the test below with two threads, 10 connections, 100 streams each,
and 1 million requests total :
Before After Gain
test duration : 89.6s 4.73s x19
HTTP requests/s (DEBUG) : 11200 211300 x19
HTTP requests/s (PROD) : 15900 447000 x28
spin_lock time : 85.2s 0.46s /185
time per lookup : 13us 40ns /325
Even when going to 6 threads (on 3 hyperthreaded CPU cores), the
performance stays around 284000 req/s, showing that the contention
is much lower.
A test showed that there's no benefit in using this for the wait queue
though.
2017-11-05 07:34:20 -05:00
|
|
|
struct eb32sc_node *rq_next;
|
2017-03-30 09:37:25 -04:00
|
|
|
struct task *local_tasks[16];
|
|
|
|
|
int local_tasks_count;
|
2017-11-06 02:36:53 -05:00
|
|
|
int final_tasks_count;
|
2017-11-14 04:26:53 -05:00
|
|
|
|
2016-12-06 03:15:30 -05:00
|
|
|
tasks_run_queue_cur = tasks_run_queue; /* keep a copy for reporting */
|
2009-03-21 13:33:52 -04:00
|
|
|
nb_tasks_cur = nb_tasks;
|
2017-11-14 04:38:36 -05:00
|
|
|
max_processed = 200;
|
2017-11-05 10:35:59 -05:00
|
|
|
if (unlikely(global.nbthread <= 1)) {
|
|
|
|
|
/* when no lock is needed, this loop is much faster */
|
2018-01-20 13:30:13 -05:00
|
|
|
if (!(active_tasks_mask & tid_bit)) {
|
|
|
|
|
activity[tid].empty_rq++;
|
2017-11-14 04:38:36 -05:00
|
|
|
return;
|
2018-01-20 13:30:13 -05:00
|
|
|
}
|
2017-11-14 04:38:36 -05:00
|
|
|
|
2017-11-14 04:26:53 -05:00
|
|
|
active_tasks_mask &= ~tid_bit;
|
2017-11-05 10:35:59 -05:00
|
|
|
rq_next = eb32sc_lookup_ge(&rqueue, rqueue_ticks - TIMER_LOOK_BACK, tid_bit);
|
|
|
|
|
while (1) {
|
|
|
|
|
if (!rq_next) {
|
|
|
|
|
/* we might have reached the end of the tree, typically because
|
|
|
|
|
* <rqueue_ticks> is in the first half and we're first scanning
|
|
|
|
|
* the last half. Let's loop back to the beginning of the tree now.
|
|
|
|
|
*/
|
|
|
|
|
rq_next = eb32sc_first(&rqueue, tid_bit);
|
|
|
|
|
if (!rq_next)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t = eb32sc_entry(rq_next, struct task, rq);
|
|
|
|
|
rq_next = eb32sc_next(rq_next, tid_bit);
|
|
|
|
|
__task_unlink_rq(t);
|
|
|
|
|
t->state |= TASK_RUNNING;
|
|
|
|
|
t->pending_state = 0;
|
|
|
|
|
|
|
|
|
|
t->calls++;
|
2017-11-26 04:08:06 -05:00
|
|
|
curr_task = t;
|
2017-11-05 10:35:59 -05:00
|
|
|
/* This is an optimisation to help the processor's branch
|
|
|
|
|
* predictor take this most common call.
|
|
|
|
|
*/
|
|
|
|
|
if (likely(t->process == process_stream))
|
|
|
|
|
t = process_stream(t);
|
|
|
|
|
else
|
|
|
|
|
t = t->process(t);
|
2017-11-26 04:08:06 -05:00
|
|
|
curr_task = NULL;
|
2017-11-05 10:35:59 -05:00
|
|
|
|
|
|
|
|
if (likely(t != NULL)) {
|
|
|
|
|
t->state &= ~TASK_RUNNING;
|
|
|
|
|
/* If there is a pending state
|
|
|
|
|
* we have to wake up the task
|
|
|
|
|
* immediatly, else we defer
|
|
|
|
|
* it into wait queue
|
|
|
|
|
*/
|
|
|
|
|
if (t->pending_state)
|
|
|
|
|
__task_wakeup(t);
|
|
|
|
|
else
|
|
|
|
|
task_queue(t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
max_processed--;
|
2017-11-14 04:26:53 -05:00
|
|
|
if (max_processed <= 0) {
|
|
|
|
|
active_tasks_mask |= tid_bit;
|
2018-01-20 13:30:13 -05:00
|
|
|
activity[tid].long_rq++;
|
2017-11-05 10:35:59 -05:00
|
|
|
break;
|
2017-11-14 04:26:53 -05:00
|
|
|
}
|
2017-11-05 10:35:59 -05:00
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 04:42:54 -05:00
|
|
|
HA_SPIN_LOCK(TASK_RQ_LOCK, &rq_lock);
|
2017-11-14 04:38:36 -05:00
|
|
|
if (!(active_tasks_mask & tid_bit)) {
|
|
|
|
|
HA_SPIN_UNLOCK(TASK_RQ_LOCK, &rq_lock);
|
2018-01-20 13:30:13 -05:00
|
|
|
activity[tid].empty_rq++;
|
2017-11-14 04:38:36 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-14 04:26:53 -05:00
|
|
|
active_tasks_mask &= ~tid_bit;
|
|
|
|
|
while (1) {
|
2009-03-21 05:01:42 -04:00
|
|
|
/* Note: this loop is one of the fastest code path in
|
|
|
|
|
* the whole program. It should not be re-arranged
|
|
|
|
|
* without a good reason.
|
|
|
|
|
*/
|
2017-11-08 08:05:19 -05:00
|
|
|
|
|
|
|
|
/* we have to restart looking up after every batch */
|
|
|
|
|
rq_next = eb32sc_lookup_ge(&rqueue, rqueue_ticks - TIMER_LOOK_BACK, tid_bit);
|
2017-11-05 17:57:00 -05:00
|
|
|
for (local_tasks_count = 0; local_tasks_count < 16; local_tasks_count++) {
|
|
|
|
|
if (unlikely(!rq_next)) {
|
|
|
|
|
/* either we just started or we reached the end
|
|
|
|
|
* of the tree, typically because <rqueue_ticks>
|
|
|
|
|
* is in the first half and we're first scanning
|
|
|
|
|
* the last half. Let's loop back to the beginning
|
|
|
|
|
* of the tree now.
|
|
|
|
|
*/
|
2017-11-14 04:17:48 -05:00
|
|
|
rq_next = eb32sc_first(&rqueue, tid_bit);
|
|
|
|
|
if (!rq_next)
|
|
|
|
|
break;
|
2017-03-30 09:37:25 -04:00
|
|
|
}
|
|
|
|
|
|
MAJOR: task: make use of the scope-aware ebtree functions
Currently the task scheduler suffers from an O(n) lookup when
skipping tasks that are not for the current thread. The reason
is that eb32_lookup_ge() has no information about the current
thread so it always revisits many tasks for other threads before
finding its own tasks.
This is particularly visible with HTTP/2 since the number of
concurrent streams created at once causes long series of tasks
for the same stream in the scheduler. With only 10 connections
and 100 streams each, by running on two threads, the performance
drops from 640kreq/s to 11.2kreq/s! Lookup metrics show that for
only 200000 task lookups, 430 million skips had to be performed,
which means that on average, each lookup leads to 2150 nodes to
be visited.
This commit backports the principle of scope lookups for ebtrees
from the ebtree_v7 development tree. The idea is that each node
contains a mask indicating the union of the scopes for the nodes
below it, which is fed during insertion, and used during lookups.
Then during lookups, branches that do not contain any leaf matching
the requested scope are simply ignored. This perfectly matches a
thread mask, allowing a thread to only extract the tasks it cares
about from the run queue, and to always find them in O(log(n))
instead of O(n). Thus the scheduler uses tid_bit and
task->thread_mask as the ebtree scope here.
Doing this has recovered most of the performance, as can be seen on
the test below with two threads, 10 connections, 100 streams each,
and 1 million requests total :
Before After Gain
test duration : 89.6s 4.73s x19
HTTP requests/s (DEBUG) : 11200 211300 x19
HTTP requests/s (PROD) : 15900 447000 x28
spin_lock time : 85.2s 0.46s /185
time per lookup : 13us 40ns /325
Even when going to 6 threads (on 3 hyperthreaded CPU cores), the
performance stays around 284000 req/s, showing that the contention
is much lower.
A test showed that there's no benefit in using this for the wait queue
though.
2017-11-05 07:34:20 -05:00
|
|
|
t = eb32sc_entry(rq_next, struct task, rq);
|
|
|
|
|
rq_next = eb32sc_next(rq_next, tid_bit);
|
2017-11-05 17:57:00 -05:00
|
|
|
|
|
|
|
|
/* detach the task from the queue */
|
|
|
|
|
__task_unlink_rq(t);
|
|
|
|
|
local_tasks[local_tasks_count] = t;
|
|
|
|
|
t->state |= TASK_RUNNING;
|
|
|
|
|
t->pending_state = 0;
|
|
|
|
|
t->calls++;
|
|
|
|
|
max_processed--;
|
2009-03-21 05:01:42 -04:00
|
|
|
}
|
2008-06-29 16:40:23 -04:00
|
|
|
|
2017-03-30 09:37:25 -04:00
|
|
|
if (!local_tasks_count)
|
|
|
|
|
break;
|
2008-06-29 16:40:23 -04:00
|
|
|
|
2017-11-07 04:42:54 -05:00
|
|
|
HA_SPIN_UNLOCK(TASK_RQ_LOCK, &rq_lock);
|
2009-03-08 11:35:27 -04:00
|
|
|
|
2017-11-06 02:36:53 -05:00
|
|
|
final_tasks_count = 0;
|
2017-03-30 09:37:25 -04:00
|
|
|
for (i = 0; i < local_tasks_count ; i++) {
|
|
|
|
|
t = local_tasks[i];
|
|
|
|
|
/* This is an optimisation to help the processor's branch
|
|
|
|
|
* predictor take this most common call.
|
|
|
|
|
*/
|
2017-11-26 04:08:06 -05:00
|
|
|
curr_task = t;
|
2017-03-30 09:37:25 -04:00
|
|
|
if (likely(t->process == process_stream))
|
|
|
|
|
t = process_stream(t);
|
|
|
|
|
else
|
|
|
|
|
t = t->process(t);
|
2017-11-26 04:08:06 -05:00
|
|
|
curr_task = NULL;
|
2017-11-06 02:36:53 -05:00
|
|
|
if (t)
|
|
|
|
|
local_tasks[final_tasks_count++] = t;
|
2017-03-30 09:37:25 -04:00
|
|
|
}
|
|
|
|
|
|
2017-11-06 02:36:53 -05:00
|
|
|
for (i = 0; i < final_tasks_count ; i++) {
|
2017-03-30 09:37:25 -04:00
|
|
|
t = local_tasks[i];
|
2017-11-06 02:36:53 -05:00
|
|
|
/* If there is a pending state
|
|
|
|
|
* we have to wake up the task
|
|
|
|
|
* immediatly, else we defer
|
|
|
|
|
* it into wait queue
|
|
|
|
|
*/
|
2017-11-23 12:36:50 -05:00
|
|
|
HA_SPIN_LOCK(TASK_RQ_LOCK, &rq_lock);
|
|
|
|
|
t->state &= ~TASK_RUNNING;
|
|
|
|
|
if (t->pending_state) {
|
2017-11-06 02:36:53 -05:00
|
|
|
__task_wakeup(t);
|
2017-11-23 12:36:50 -05:00
|
|
|
HA_SPIN_UNLOCK(TASK_RQ_LOCK, &rq_lock);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* we must never hold the RQ lock before the WQ lock */
|
|
|
|
|
HA_SPIN_UNLOCK(TASK_RQ_LOCK, &rq_lock);
|
2017-11-06 02:36:53 -05:00
|
|
|
task_queue(t);
|
2017-11-23 12:36:50 -05:00
|
|
|
}
|
2008-06-29 16:40:23 -04:00
|
|
|
}
|
2017-11-05 17:57:00 -05:00
|
|
|
|
2017-11-23 12:36:50 -05:00
|
|
|
HA_SPIN_LOCK(TASK_RQ_LOCK, &rq_lock);
|
2017-11-14 04:26:53 -05:00
|
|
|
if (max_processed <= 0) {
|
|
|
|
|
active_tasks_mask |= tid_bit;
|
2018-01-20 13:30:13 -05:00
|
|
|
activity[tid].long_rq++;
|
2017-11-14 04:26:53 -05:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-07 04:42:54 -05:00
|
|
|
HA_SPIN_UNLOCK(TASK_RQ_LOCK, &rq_lock);
|
2006-06-25 20:48:02 -04:00
|
|
|
}
|
|
|
|
|
|
2009-03-07 11:25:21 -05:00
|
|
|
/* perform minimal intializations, report 0 in case of error, 1 if OK. */
|
|
|
|
|
int init_task()
|
|
|
|
|
{
|
|
|
|
|
memset(&timers, 0, sizeof(timers));
|
|
|
|
|
memset(&rqueue, 0, sizeof(rqueue));
|
2017-11-07 04:42:54 -05:00
|
|
|
HA_SPIN_INIT(&wq_lock);
|
|
|
|
|
HA_SPIN_INIT(&rq_lock);
|
2017-11-24 11:34:44 -05:00
|
|
|
pool_head_task = create_pool("task", sizeof(struct task), MEM_F_SHARED);
|
|
|
|
|
if (!pool_head_task)
|
2017-07-12 08:31:10 -04:00
|
|
|
return 0;
|
2017-11-24 11:34:44 -05:00
|
|
|
pool_head_notification = create_pool("notification", sizeof(struct notification), MEM_F_SHARED);
|
|
|
|
|
if (!pool_head_notification)
|
2017-07-12 08:31:10 -04:00
|
|
|
return 0;
|
|
|
|
|
return 1;
|
2009-03-07 11:25:21 -05:00
|
|
|
}
|
|
|
|
|
|
2006-06-25 20:48:02 -04:00
|
|
|
/*
|
|
|
|
|
* Local variables:
|
|
|
|
|
* c-indent-level: 8
|
|
|
|
|
* c-basic-offset: 8
|
|
|
|
|
* End:
|
|
|
|
|
*/
|