MEDIUM: task: remove the tasks_run_queue counter and have one per thread
This counter is solely used for reporting in the stats and is the hottest
thread contention point to date. Moving it to the scheduler and having a
separate one for the global run queue dramatically improves the performance,
showing a 12% boost on the request rate on 16 threads!
In addition, the thread debugging output which used to rely on rqueue_size
was not totally accurate as it would only report task counts. Now we can
return the exact thread's run queue length.
It is also interesting to note that there are still a few other task/tasklet
counters in the scheduler that are not efficiently updated because some cover
a single area and others cover multiple areas. It looks like having a distinct
counter for each of the following entries would help and would keep the code
a bit cleaner:
- global run queue (tree)
- per-thread run queue (tree)
- per-thread shared tasklets list
- per-thread local lists
Maybe even splitting the shared tasklets lists between pure tasklets and
tasks instead of having the whole and tasks would simplify the code because
there remain a number of places where several counters have to be updated.
(cherry picked from commit 9c7b8085f4cad284642e7f67d2274f2fb568f243)
[wt: backported as it does have a significant impact on many-core machines;
with 48 threads, the request rate is simply doubled and the response time
halved; There were significant context updates, all verified to be OK; it
does not seem reasonable to backport it to 2.2]
Signed-off-by: Willy Tarreau <w@1wt.eu>
diff --git a/src/task.c b/src/task.c
index 20b08ea..2959812 100644
--- a/src/task.c
+++ b/src/task.c
@@ -37,8 +37,6 @@
unsigned int nb_tasks = 0;
volatile unsigned long global_tasks_mask = 0; /* Mask of threads with tasks in the global runqueue */
-unsigned int tasks_run_queue = 0;
-unsigned int tasks_run_queue_cur = 0; /* copy of the run queue size */
unsigned int nb_tasks_cur = 0; /* copy of the tasks count */
unsigned int niced_tasks = 0; /* number of niced tasks in the run queue */
@@ -48,8 +46,9 @@
__decl_aligned_rwlock(wq_lock); /* RW lock related to the wait queue */
#ifdef USE_THREAD
-struct eb_root timers; /* sorted timers tree, global */
-struct eb_root rqueue; /* tree constituting the run queue */
+struct eb_root timers; /* sorted timers tree, global, accessed under wq_lock */
+struct eb_root rqueue; /* tree constituting the global run queue, accessed under rq_lock */
+unsigned int grq_total; /* total number of entries in the global run queue, use grq_lock */
#endif
static unsigned int rqueue_ticks; /* insertion count */
@@ -97,7 +96,7 @@
/* Beware: tasks that have never run don't have their ->list empty yet! */
MT_LIST_ADDQ(&task_per_thread[thr].shared_tasklet_list,
(struct mt_list *)&((struct tasklet *)t)->list);
- _HA_ATOMIC_ADD(&tasks_run_queue, 1);
+ _HA_ATOMIC_ADD(&task_per_thread[thr].rq_total, 1);
_HA_ATOMIC_ADD(&task_per_thread[thr].task_list_size, 1);
if (sleeping_thread_mask & (1UL << thr)) {
_HA_ATOMIC_AND(&sleeping_thread_mask, ~(1UL << thr));
@@ -122,17 +121,16 @@
if (root == &rqueue) {
HA_SPIN_LOCK(TASK_RQ_LOCK, &rq_lock);
}
-#endif
- /* Make sure if the task isn't in the runqueue, nobody inserts it
- * in the meanwhile.
- */
- _HA_ATOMIC_ADD(&tasks_run_queue, 1);
-#ifdef USE_THREAD
+
if (root == &rqueue) {
global_tasks_mask |= t->thread_mask;
+ grq_total++;
__ha_barrier_store();
- }
+ } else
#endif
+ {
+ _HA_ATOMIC_ADD(&sched->rq_total, 1);
+ }
t->rq.key = _HA_ATOMIC_ADD(&rqueue_ticks, 1);
if (likely(t->nice)) {
@@ -472,7 +470,7 @@
t->calls++;
sched->current = t;
- _HA_ATOMIC_SUB(&tasks_run_queue, 1);
+ _HA_ATOMIC_SUB(&sched->rq_total, 1);
if (TASK_IS_TASKLET(t)) {
LIST_DEL_INIT(&((struct tasklet *)t)->list);
@@ -587,7 +585,6 @@
return;
}
- tasks_run_queue_cur = tasks_run_queue; /* keep a copy for reporting */
nb_tasks_cur = nb_tasks;
max_processed = global.tune.runqueue_depth;