blob: 76a7432737fcd0754477c1089a109de92de4639d [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Task management functions.
3 *
Willy Tarreau4726f532009-03-07 17:25:21 +01004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau87bed622009-03-08 22:25:28 +010013#include <string.h>
14
Willy Tarreaub2551052020-06-09 09:07:15 +020015#include <import/eb32tree.h>
16
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Willy Tarreau5d9ddc52021-10-06 19:54:09 +020018#include <haproxy/activity.h>
Willy Tarreaue7723bd2020-06-24 11:11:02 +020019#include <haproxy/cfgparse.h>
Willy Tarreau55542642021-10-08 09:33:24 +020020#include <haproxy/clock.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/fd.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020022#include <haproxy/list.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/pool.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020024#include <haproxy/task.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020025#include <haproxy/tools.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
Willy Tarreaue08f4bf2021-05-08 20:10:13 +020027extern struct task *process_stream(struct task *t, void *context, unsigned int state);
Willy Tarreaubaaee002006-06-26 02:48:02 +020028
Willy Tarreau8ceae722018-11-26 11:58:30 +010029DECLARE_POOL(pool_head_task, "task", sizeof(struct task));
30DECLARE_POOL(pool_head_tasklet, "tasklet", sizeof(struct tasklet));
Willy Tarreau96bcfd72007-04-29 10:41:56 +020031
Thierry FOURNIERd6975962017-07-12 14:31:10 +020032/* This is the memory pool containing all the signal structs. These
Joseph Herlantcf92b6d2018-11-15 14:19:23 -080033 * struct are used to store each required signal between two tasks.
Thierry FOURNIERd6975962017-07-12 14:31:10 +020034 */
Willy Tarreau8ceae722018-11-26 11:58:30 +010035DECLARE_POOL(pool_head_notification, "notification", sizeof(struct notification));
Thierry FOURNIERd6975962017-07-12 14:31:10 +020036
Willy Tarreaue35c94a2009-03-21 10:01:42 +010037unsigned int niced_tasks = 0; /* number of niced tasks in the run queue */
Emeric Brun01948972017-03-30 15:37:25 +020038
Willy Tarreauef28dc12019-05-28 18:48:07 +020039__decl_aligned_rwlock(wq_lock); /* RW lock related to the wait queue */
Willy Tarreau964c9362007-01-07 00:38:00 +010040
Olivier Houchardb1ca58b2018-06-06 14:22:03 +020041#ifdef USE_THREAD
Willy Tarreauc6ba9a02021-02-20 12:49:54 +010042struct eb_root timers; /* sorted timers tree, global, accessed under wq_lock */
Olivier Houchardb1ca58b2018-06-06 14:22:03 +020043#endif
Willy Tarreaub20aa9e2018-10-15 14:52:21 +020044
Willy Tarreau8d8747a2018-10-15 16:12:48 +020045
Willy Tarreaueb8c2c62020-06-30 11:48:48 +020046
47/* Flags the task <t> for immediate destruction and puts it into its first
48 * thread's shared tasklet list if not yet queued/running. This will bypass
49 * the priority scheduling and make the task show up as fast as possible in
50 * the other thread's queue. Note that this operation isn't idempotent and is
51 * not supposed to be run on the same task from multiple threads at once. It's
52 * the caller's responsibility to make sure it is the only one able to kill the
53 * task.
54 */
55void task_kill(struct task *t)
56{
Willy Tarreau144f84a2021-03-02 16:09:26 +010057 unsigned int state = t->state;
Willy Tarreaueb8c2c62020-06-30 11:48:48 +020058 unsigned int thr;
59
60 BUG_ON(state & TASK_KILLED);
61
62 while (1) {
63 while (state & (TASK_RUNNING | TASK_QUEUED)) {
64 /* task already in the queue and about to be executed,
65 * or even currently running. Just add the flag and be
66 * done with it, the process loop will detect it and kill
67 * it. The CAS will fail if we arrive too late.
68 */
69 if (_HA_ATOMIC_CAS(&t->state, &state, state | TASK_KILLED))
70 return;
71 }
72
73 /* We'll have to wake it up, but we must also secure it so that
74 * it doesn't vanish under us. TASK_QUEUED guarantees nobody will
75 * add past us.
76 */
77 if (_HA_ATOMIC_CAS(&t->state, &state, state | TASK_QUEUED | TASK_KILLED)) {
78 /* Bypass the tree and go directly into the shared tasklet list.
79 * Note: that's a task so it must be accounted for as such. Pick
80 * the task's first thread for the job.
81 */
Willy Tarreau29ffe262022-06-15 14:31:38 +020082 thr = t->tid >= 0 ? t->tid : tid;
Willy Tarreau54d31172020-07-02 14:14:00 +020083
84 /* Beware: tasks that have never run don't have their ->list empty yet! */
Willy Tarreau1a9c9222021-10-01 11:30:33 +020085 MT_LIST_APPEND(&ha_thread_ctx[thr].shared_tasklet_list,
Willy Tarreaucc5cd5b2022-01-28 09:48:12 +010086 list_to_mt_list(&((struct tasklet *)t)->list));
Willy Tarreau1a9c9222021-10-01 11:30:33 +020087 _HA_ATOMIC_INC(&ha_thread_ctx[thr].rq_total);
88 _HA_ATOMIC_INC(&ha_thread_ctx[thr].tasks_in_list);
Willy Tarreauf3efef42022-06-20 09:14:40 +020089 wake_thread(thr);
Willy Tarreau54d31172020-07-02 14:14:00 +020090 return;
Willy Tarreaueb8c2c62020-06-30 11:48:48 +020091 }
92 }
93}
94
Amaury Denoyelle7b368332021-07-28 16:12:57 +020095/* Equivalent of task_kill for tasklets. Mark the tasklet <t> for destruction.
96 * It will be deleted on the next scheduler invocation. This function is
97 * thread-safe : a thread can kill a tasklet of another thread.
98 */
99void tasklet_kill(struct tasklet *t)
100{
101 unsigned int state = t->state;
102 unsigned int thr;
103
104 BUG_ON(state & TASK_KILLED);
105
106 while (1) {
107 while (state & (TASK_IN_LIST)) {
108 /* Tasklet already in the list ready to be executed. Add
109 * the killed flag and wait for the process loop to
110 * detect it.
111 */
112 if (_HA_ATOMIC_CAS(&t->state, &state, state | TASK_KILLED))
113 return;
114 }
115
116 /* Mark the tasklet as killed and wake the thread to process it
117 * as soon as possible.
118 */
119 if (_HA_ATOMIC_CAS(&t->state, &state, state | TASK_IN_LIST | TASK_KILLED)) {
Willy Tarreau9b3aa632022-06-15 15:54:56 +0200120 thr = t->tid >= 0 ? t->tid : tid;
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200121 MT_LIST_APPEND(&ha_thread_ctx[thr].shared_tasklet_list,
Willy Tarreaucc5cd5b2022-01-28 09:48:12 +0100122 list_to_mt_list(&t->list));
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200123 _HA_ATOMIC_INC(&ha_thread_ctx[thr].rq_total);
Willy Tarreauf3efef42022-06-20 09:14:40 +0200124 wake_thread(thr);
Amaury Denoyelle7b368332021-07-28 16:12:57 +0200125 return;
126 }
127 }
128}
129
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100130/* Do not call this one, please use tasklet_wakeup_on() instead, as this one is
131 * the slow path of tasklet_wakeup_on() which performs some preliminary checks
132 * and sets TASK_IN_LIST before calling this one. A negative <thr> designates
133 * the current thread.
134 */
135void __tasklet_wakeup_on(struct tasklet *tl, int thr)
136{
137 if (likely(thr < 0)) {
138 /* this tasklet runs on the caller thread */
Willy Tarreau826fa872021-02-26 10:13:40 +0100139 if (tl->state & TASK_HEAVY) {
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200140 LIST_APPEND(&th_ctx->tasklets[TL_HEAVY], &tl->list);
141 th_ctx->tl_class_mask |= 1 << TL_HEAVY;
Willy Tarreau826fa872021-02-26 10:13:40 +0100142 }
143 else if (tl->state & TASK_SELF_WAKING) {
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200144 LIST_APPEND(&th_ctx->tasklets[TL_BULK], &tl->list);
145 th_ctx->tl_class_mask |= 1 << TL_BULK;
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100146 }
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200147 else if ((struct task *)tl == th_ctx->current) {
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100148 _HA_ATOMIC_OR(&tl->state, TASK_SELF_WAKING);
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200149 LIST_APPEND(&th_ctx->tasklets[TL_BULK], &tl->list);
150 th_ctx->tl_class_mask |= 1 << TL_BULK;
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100151 }
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200152 else if (th_ctx->current_queue < 0) {
153 LIST_APPEND(&th_ctx->tasklets[TL_URGENT], &tl->list);
154 th_ctx->tl_class_mask |= 1 << TL_URGENT;
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100155 }
156 else {
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200157 LIST_APPEND(&th_ctx->tasklets[th_ctx->current_queue], &tl->list);
158 th_ctx->tl_class_mask |= 1 << th_ctx->current_queue;
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100159 }
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200160 _HA_ATOMIC_INC(&th_ctx->rq_total);
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100161 } else {
162 /* this tasklet runs on a specific thread. */
Willy Tarreaucc5cd5b2022-01-28 09:48:12 +0100163 MT_LIST_APPEND(&ha_thread_ctx[thr].shared_tasklet_list, list_to_mt_list(&tl->list));
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200164 _HA_ATOMIC_INC(&ha_thread_ctx[thr].rq_total);
Willy Tarreauf3efef42022-06-20 09:14:40 +0200165 wake_thread(thr);
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100166 }
167}
168
Frédéric Lécaillead548b52022-06-29 10:53:03 +0200169/* Do not call this one, please use tasklet_wakeup_after_on() instead, as this one is
170 * the slow path of tasklet_wakeup_after() which performs some preliminary checks
171 * and sets TASK_IN_LIST before calling this one.
172 */
173struct list *__tasklet_wakeup_after(struct list *head, struct tasklet *tl)
174{
175 BUG_ON(tid != tl->tid);
176 /* this tasklet runs on the caller thread */
177 if (!head) {
178 if (tl->state & TASK_HEAVY) {
179 LIST_INSERT(&th_ctx->tasklets[TL_HEAVY], &tl->list);
180 th_ctx->tl_class_mask |= 1 << TL_HEAVY;
181 }
182 else if (tl->state & TASK_SELF_WAKING) {
183 LIST_INSERT(&th_ctx->tasklets[TL_BULK], &tl->list);
184 th_ctx->tl_class_mask |= 1 << TL_BULK;
185 }
186 else if ((struct task *)tl == th_ctx->current) {
187 _HA_ATOMIC_OR(&tl->state, TASK_SELF_WAKING);
188 LIST_INSERT(&th_ctx->tasklets[TL_BULK], &tl->list);
189 th_ctx->tl_class_mask |= 1 << TL_BULK;
190 }
191 else if (th_ctx->current_queue < 0) {
192 LIST_INSERT(&th_ctx->tasklets[TL_URGENT], &tl->list);
193 th_ctx->tl_class_mask |= 1 << TL_URGENT;
194 }
195 else {
196 LIST_INSERT(&th_ctx->tasklets[th_ctx->current_queue], &tl->list);
197 th_ctx->tl_class_mask |= 1 << th_ctx->current_queue;
198 }
199 }
200 else {
201 LIST_APPEND(head, &tl->list);
202 }
203 _HA_ATOMIC_INC(&th_ctx->rq_total);
204 return &tl->list;
205}
206
Willy Tarreau4726f532009-03-07 17:25:21 +0100207/* Puts the task <t> in run queue at a position depending on t->nice. <t> is
208 * returned. The nice value assigns boosts in 32th of the run queue size. A
Christopher Faulet34c5cc92016-12-06 09:15:30 +0100209 * nice value of -1024 sets the task to -tasks_run_queue*32, while a nice value
210 * of 1024 sets the task to tasks_run_queue*32. The state flags are cleared, so
211 * the caller will have to set its flags after this call.
Willy Tarreau4726f532009-03-07 17:25:21 +0100212 * The task must not already be in the run queue. If unsure, use the safer
213 * task_wakeup() function.
Willy Tarreau91e99932008-06-30 07:51:00 +0200214 */
Willy Tarreau018564e2021-02-24 16:41:11 +0100215void __task_wakeup(struct task *t)
Willy Tarreaue33aece2007-04-30 13:15:14 +0200216{
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200217 struct eb_root *root = &th_ctx->rqueue;
Willy Tarreau29ffe262022-06-15 14:31:38 +0200218 int thr __maybe_unused = t->tid >= 0 ? t->tid : tid;
Willy Tarreau018564e2021-02-24 16:41:11 +0100219
Olivier Houchardb1ca58b2018-06-06 14:22:03 +0200220#ifdef USE_THREAD
Willy Tarreau29ffe262022-06-15 14:31:38 +0200221 if (thr != tid) {
Willy Tarreau6f780382022-06-16 15:30:50 +0200222 root = &ha_thread_ctx[thr].rqueue_shared;
Willy Tarreau018564e2021-02-24 16:41:11 +0100223
Willy Tarreauda195e82022-06-16 15:52:49 +0200224 _HA_ATOMIC_INC(&ha_thread_ctx[thr].rq_total);
Willy Tarreaub17dd6c2022-06-16 16:58:17 +0200225 HA_SPIN_LOCK(TASK_RQ_LOCK, &ha_thread_ctx[thr].rqsh_lock);
Willy Tarreau9c7b8082021-02-24 15:10:07 +0100226
Willy Tarreau6f780382022-06-16 15:30:50 +0200227 t->rq.key = _HA_ATOMIC_ADD_FETCH(&ha_thread_ctx[thr].rqueue_ticks, 1);
Olivier Houcharded1a6a02019-04-18 14:12:51 +0200228 __ha_barrier_store();
Willy Tarreauc6ba9a02021-02-20 12:49:54 +0100229 } else
Olivier Houchardc4aac9e2018-07-26 15:25:49 +0200230#endif
Willy Tarreau9c7b8082021-02-24 15:10:07 +0100231 {
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200232 _HA_ATOMIC_INC(&th_ctx->rq_total);
Willy Tarreaua4fb79b2022-06-16 15:44:35 +0200233 t->rq.key = _HA_ATOMIC_ADD_FETCH(&th_ctx->rqueue_ticks, 1);
Willy Tarreau9c7b8082021-02-24 15:10:07 +0100234 }
Willy Tarreau91e99932008-06-30 07:51:00 +0200235
236 if (likely(t->nice)) {
237 int offset;
238
Willy Tarreau4781b152021-04-06 13:53:36 +0200239 _HA_ATOMIC_INC(&niced_tasks);
Willy Tarreau2d1fd0a2019-04-15 09:18:31 +0200240 offset = t->nice * (int)global.tune.runqueue_depth;
Willy Tarreau4726f532009-03-07 17:25:21 +0100241 t->rq.key += offset;
Willy Tarreau91e99932008-06-30 07:51:00 +0200242 }
243
Willy Tarreau680ed5f2022-06-13 15:59:39 +0200244 if (th_ctx->flags & TH_FL_TASK_PROFILING)
Willy Tarreau9efd7452018-05-31 14:48:54 +0200245 t->call_date = now_mono_time();
246
Willy Tarreau319d1362022-06-16 16:28:01 +0200247 eb32_insert(root, &t->rq);
Willy Tarreau018564e2021-02-24 16:41:11 +0100248
Olivier Houchardb1ca58b2018-06-06 14:22:03 +0200249#ifdef USE_THREAD
Willy Tarreau29ffe262022-06-15 14:31:38 +0200250 if (thr != tid) {
Willy Tarreaub17dd6c2022-06-16 16:58:17 +0200251 HA_SPIN_UNLOCK(TASK_RQ_LOCK, &ha_thread_ctx[thr].rqsh_lock);
Willy Tarreau2c41d772021-02-24 16:13:03 +0100252
Willy Tarreaueeffb3d2021-02-24 16:44:51 +0100253 /* If all threads that are supposed to handle this task are sleeping,
254 * wake one.
255 */
Willy Tarreauf3efef42022-06-20 09:14:40 +0200256 wake_thread(thr);
Olivier Houchard1b327902019-03-15 00:23:10 +0100257 }
Willy Tarreau85d9b842018-07-27 17:14:41 +0200258#endif
Olivier Houchardf6e6dc12018-05-18 18:38:23 +0200259 return;
Willy Tarreaue33aece2007-04-30 13:15:14 +0200260}
Willy Tarreaud825eef2007-05-12 22:35:00 +0200261
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200262/*
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100263 * __task_queue()
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200264 *
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200265 * Inserts a task into wait queue <wq> at the position given by its expiration
Willy Tarreau4726f532009-03-07 17:25:21 +0100266 * date. It does not matter if the task was already in the wait queue or not,
Willy Tarreau7a969992021-09-30 16:38:09 +0200267 * as it will be unlinked. The task MUST NOT have an infinite expiration timer.
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100268 * Last, tasks must not be queued further than the end of the tree, which is
269 * between <now_ms> and <now_ms> + 2^31 ms (now+24days in 32bit).
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100270 *
271 * This function should not be used directly, it is meant to be called by the
272 * inline version of task_queue() which performs a few cheap preliminary tests
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200273 * before deciding to call __task_queue(). Moreover this function doesn't care
274 * at all about locking so the caller must be careful when deciding whether to
275 * lock or not around this call.
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200276 */
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200277void __task_queue(struct task *task, struct eb_root *wq)
Willy Tarreau964c9362007-01-07 00:38:00 +0100278{
Willy Tarreaue5d79bc2020-07-22 14:29:42 +0200279#ifdef USE_THREAD
Willy Tarreau159e3ac2022-06-15 16:48:45 +0200280 BUG_ON((wq == &timers && task->tid >= 0) ||
281 (wq == &th_ctx->timers && task->tid < 0) ||
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200282 (wq != &timers && wq != &th_ctx->timers));
Willy Tarreaue5d79bc2020-07-22 14:29:42 +0200283#endif
Willy Tarreau7a969992021-09-30 16:38:09 +0200284 /* if this happens the process is doomed anyway, so better catch it now
285 * so that we have the caller in the stack.
286 */
287 BUG_ON(task->expire == TICK_ETERNITY);
Willy Tarreaue5d79bc2020-07-22 14:29:42 +0200288
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100289 if (likely(task_in_wq(task)))
Willy Tarreau4726f532009-03-07 17:25:21 +0100290 __task_unlink_wq(task);
Willy Tarreau4726f532009-03-07 17:25:21 +0100291
292 /* the task is not in the queue now */
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100293 task->wq.key = task->expire;
Willy Tarreau28c41a42008-06-29 17:00:59 +0200294#ifdef DEBUG_CHECK_INVALID_EXPIRATION_DATES
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100295 if (tick_is_lt(task->wq.key, now_ms))
Willy Tarreau28c41a42008-06-29 17:00:59 +0200296 /* we're queuing too far away or in the past (most likely) */
Willy Tarreau4726f532009-03-07 17:25:21 +0100297 return;
Willy Tarreau28c41a42008-06-29 17:00:59 +0200298#endif
Willy Tarreauce44f122008-07-05 18:16:19 +0200299
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200300 eb32_insert(wq, &task->wq);
Willy Tarreau964c9362007-01-07 00:38:00 +0100301}
302
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200303/*
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200304 * Extract all expired timers from the timer queue, and wakes up all
Willy Tarreauc49ba522019-12-11 08:12:23 +0100305 * associated tasks.
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200306 */
Willy Tarreauc49ba522019-12-11 08:12:23 +0100307void wake_expired_tasks()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200308{
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200309 struct thread_ctx * const tt = th_ctx; // thread's tasks
Willy Tarreau3cfaa8d2020-10-16 09:26:22 +0200310 int max_processed = global.tune.runqueue_depth;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200311 struct task *task;
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200312 struct eb32_node *eb;
Willy Tarreauaf613e82020-06-05 08:40:51 +0200313 __decl_thread(int key);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200314
Willy Tarreauf5aef022022-06-14 15:04:34 +0200315 while (1) {
316 if (max_processed-- <= 0)
317 goto leave;
318
Willy Tarreau4c1e1ad2019-09-24 07:19:08 +0200319 eb = eb32_lookup_ge(&tt->timers, now_ms - TIMER_LOOK_BACK);
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200320 if (!eb) {
321 /* we might have reached the end of the tree, typically because
322 * <now_ms> is in the first half and we're first scanning the last
323 * half. Let's loop back to the beginning of the tree now.
324 */
Willy Tarreau4c1e1ad2019-09-24 07:19:08 +0200325 eb = eb32_first(&tt->timers);
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200326 if (likely(!eb))
327 break;
328 }
329
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200330 /* It is possible that this task was left at an earlier place in the
331 * tree because a recent call to task_queue() has not moved it. This
332 * happens when the new expiration date is later than the old one.
333 * Since it is very unlikely that we reach a timeout anyway, it's a
334 * lot cheaper to proceed like this because we almost never update
335 * the tree. We may also find disabled expiration dates there. Since
336 * we have detached the task from the tree, we simply call task_queue
337 * to take care of this. Note that we might occasionally requeue it at
338 * the same place, before <eb>, so we have to check if this happens,
339 * and adjust <eb>, otherwise we may skip it which is not what we want.
340 * We may also not requeue the task (and not point eb at it) if its
Willy Tarreau77015ab2020-06-19 11:50:27 +0200341 * expiration time is not set. We also make sure we leave the real
342 * expiration date for the next task in the queue so that when calling
343 * next_timer_expiry() we're guaranteed to see the next real date and
344 * not the next apparent date. This is in order to avoid useless
345 * wakeups.
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200346 */
Willy Tarreau77015ab2020-06-19 11:50:27 +0200347
348 task = eb32_entry(eb, struct task, wq);
349 if (tick_is_expired(task->expire, now_ms)) {
350 /* expired task, wake it up */
351 __task_unlink_wq(task);
352 task_wakeup(task, TASK_WOKEN_TIMER);
353 }
354 else if (task->expire != eb->key) {
355 /* task is not expired but its key doesn't match so let's
356 * update it and skip to next apparently expired task.
357 */
358 __task_unlink_wq(task);
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200359 if (tick_isset(task->expire))
Willy Tarreau4c1e1ad2019-09-24 07:19:08 +0200360 __task_queue(task, &tt->timers);
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200361 }
Willy Tarreau77015ab2020-06-19 11:50:27 +0200362 else {
Willy Tarreau7a969992021-09-30 16:38:09 +0200363 /* task not expired and correctly placed. It may not be eternal. */
364 BUG_ON(task->expire == TICK_ETERNITY);
Willy Tarreau77015ab2020-06-19 11:50:27 +0200365 break;
366 }
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200367 }
368
369#ifdef USE_THREAD
Willy Tarreau1e928c02019-05-28 18:57:25 +0200370 if (eb_is_empty(&timers))
371 goto leave;
372
373 HA_RWLOCK_RDLOCK(TASK_WQ_LOCK, &wq_lock);
374 eb = eb32_lookup_ge(&timers, now_ms - TIMER_LOOK_BACK);
375 if (!eb) {
376 eb = eb32_first(&timers);
377 if (likely(!eb)) {
378 HA_RWLOCK_RDUNLOCK(TASK_WQ_LOCK, &wq_lock);
379 goto leave;
380 }
381 }
382 key = eb->key;
Willy Tarreau1e928c02019-05-28 18:57:25 +0200383
Willy Tarreaud48ed662020-10-16 09:31:41 +0200384 if (tick_is_lt(now_ms, key)) {
385 HA_RWLOCK_RDUNLOCK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreau1e928c02019-05-28 18:57:25 +0200386 goto leave;
Willy Tarreaud48ed662020-10-16 09:31:41 +0200387 }
Willy Tarreau1e928c02019-05-28 18:57:25 +0200388
389 /* There's really something of interest here, let's visit the queue */
390
Willy Tarreaud48ed662020-10-16 09:31:41 +0200391 if (HA_RWLOCK_TRYRDTOSK(TASK_WQ_LOCK, &wq_lock)) {
392 /* if we failed to grab the lock it means another thread is
393 * already doing the same here, so let it do the job.
394 */
395 HA_RWLOCK_RDUNLOCK(TASK_WQ_LOCK, &wq_lock);
396 goto leave;
397 }
398
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200399 while (1) {
Emeric Brunc60def82017-09-27 14:59:38 +0200400 lookup_next:
Willy Tarreau3cfaa8d2020-10-16 09:26:22 +0200401 if (max_processed-- <= 0)
402 break;
Emeric Brun01948972017-03-30 15:37:25 +0200403 eb = eb32_lookup_ge(&timers, now_ms - TIMER_LOOK_BACK);
Emeric Brunc60def82017-09-27 14:59:38 +0200404 if (!eb) {
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100405 /* we might have reached the end of the tree, typically because
406 * <now_ms> is in the first half and we're first scanning the last
407 * half. Let's loop back to the beginning of the tree now.
408 */
409 eb = eb32_first(&timers);
Willy Tarreaub992ba12017-11-05 19:09:27 +0100410 if (likely(!eb))
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100411 break;
412 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100414 task = eb32_entry(eb, struct task, wq);
Willy Tarreau6c8babf2022-02-14 10:18:51 +0100415
416 /* Check for any competing run of the task (quite rare but may
417 * involve a dangerous concurrent access on task->expire). In
418 * order to protect against this, we'll take an exclusive access
419 * on TASK_RUNNING before checking/touching task->expire. If the
420 * task is already RUNNING on another thread, it will deal by
421 * itself with the requeuing so we must not do anything and
422 * simply quit the loop for now, because we cannot wait with the
423 * WQ lock held as this would prevent the running thread from
424 * requeuing the task. One annoying effect of holding RUNNING
425 * here is that a concurrent task_wakeup() will refrain from
426 * waking it up. This forces us to check for a wakeup after
427 * releasing the flag.
428 */
429 if (HA_ATOMIC_FETCH_OR(&task->state, TASK_RUNNING) & TASK_RUNNING)
430 break;
431
Willy Tarreau77015ab2020-06-19 11:50:27 +0200432 if (tick_is_expired(task->expire, now_ms)) {
433 /* expired task, wake it up */
Willy Tarreaud48ed662020-10-16 09:31:41 +0200434 HA_RWLOCK_SKTOWR(TASK_WQ_LOCK, &wq_lock);
Willy Tarreau77015ab2020-06-19 11:50:27 +0200435 __task_unlink_wq(task);
Willy Tarreaud48ed662020-10-16 09:31:41 +0200436 HA_RWLOCK_WRTOSK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreau6c8babf2022-02-14 10:18:51 +0100437 task_drop_running(task, TASK_WOKEN_TIMER);
Willy Tarreau77015ab2020-06-19 11:50:27 +0200438 }
439 else if (task->expire != eb->key) {
440 /* task is not expired but its key doesn't match so let's
441 * update it and skip to next apparently expired task.
442 */
Willy Tarreaud48ed662020-10-16 09:31:41 +0200443 HA_RWLOCK_SKTOWR(TASK_WQ_LOCK, &wq_lock);
Willy Tarreau77015ab2020-06-19 11:50:27 +0200444 __task_unlink_wq(task);
Willy Tarreaub992ba12017-11-05 19:09:27 +0100445 if (tick_isset(task->expire))
Willy Tarreau783afbe2020-07-22 14:12:45 +0200446 __task_queue(task, &timers);
Willy Tarreaud48ed662020-10-16 09:31:41 +0200447 HA_RWLOCK_WRTOSK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreau6c8babf2022-02-14 10:18:51 +0100448 task_drop_running(task, 0);
Emeric Brunc60def82017-09-27 14:59:38 +0200449 goto lookup_next;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200450 }
Willy Tarreau77015ab2020-06-19 11:50:27 +0200451 else {
Willy Tarreau7a969992021-09-30 16:38:09 +0200452 /* task not expired and correctly placed. It may not be eternal. */
453 BUG_ON(task->expire == TICK_ETERNITY);
Willy Tarreau6c8babf2022-02-14 10:18:51 +0100454 task_drop_running(task, 0);
Willy Tarreau77015ab2020-06-19 11:50:27 +0200455 break;
456 }
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100457 }
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200458
Willy Tarreaud48ed662020-10-16 09:31:41 +0200459 HA_RWLOCK_SKUNLOCK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200460#endif
Willy Tarreau1e928c02019-05-28 18:57:25 +0200461leave:
Willy Tarreauc49ba522019-12-11 08:12:23 +0100462 return;
463}
464
465/* Checks the next timer for the current thread by looking into its own timer
466 * list and the global one. It may return TICK_ETERNITY if no timer is present.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500467 * Note that the next timer might very well be slightly in the past.
Willy Tarreauc49ba522019-12-11 08:12:23 +0100468 */
469int next_timer_expiry()
470{
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200471 struct thread_ctx * const tt = th_ctx; // thread's tasks
Willy Tarreauc49ba522019-12-11 08:12:23 +0100472 struct eb32_node *eb;
473 int ret = TICK_ETERNITY;
Willy Tarreau6ce02322020-08-21 05:48:34 +0200474 __decl_thread(int key = TICK_ETERNITY);
Willy Tarreauc49ba522019-12-11 08:12:23 +0100475
476 /* first check in the thread-local timers */
477 eb = eb32_lookup_ge(&tt->timers, now_ms - TIMER_LOOK_BACK);
478 if (!eb) {
479 /* we might have reached the end of the tree, typically because
480 * <now_ms> is in the first half and we're first scanning the last
481 * half. Let's loop back to the beginning of the tree now.
482 */
483 eb = eb32_first(&tt->timers);
484 }
485
486 if (eb)
487 ret = eb->key;
488
489#ifdef USE_THREAD
490 if (!eb_is_empty(&timers)) {
491 HA_RWLOCK_RDLOCK(TASK_WQ_LOCK, &wq_lock);
492 eb = eb32_lookup_ge(&timers, now_ms - TIMER_LOOK_BACK);
493 if (!eb)
494 eb = eb32_first(&timers);
495 if (eb)
496 key = eb->key;
497 HA_RWLOCK_RDUNLOCK(TASK_WQ_LOCK, &wq_lock);
498 if (eb)
499 ret = tick_first(ret, key);
500 }
501#endif
Willy Tarreaub992ba12017-11-05 19:09:27 +0100502 return ret;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200503}
504
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200505/* Walks over tasklet lists th_ctx->tasklets[0..TL_CLASSES-1] and run at most
Willy Tarreau59153fe2020-06-24 10:17:29 +0200506 * budget[TL_*] of them. Returns the number of entries effectively processed
507 * (tasks and tasklets merged). The count of tasks in the list for the current
508 * thread is adjusted.
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100509 */
Willy Tarreau59153fe2020-06-24 10:17:29 +0200510unsigned int run_tasks_from_lists(unsigned int budgets[])
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100511{
Willy Tarreau144f84a2021-03-02 16:09:26 +0100512 struct task *(*process)(struct task *t, void *ctx, unsigned int state);
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200513 struct list *tl_queues = th_ctx->tasklets;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100514 struct task *t;
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200515 uint8_t budget_mask = (1 << TL_CLASSES) - 1;
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100516 struct sched_activity *profile_entry = NULL;
Willy Tarreau59153fe2020-06-24 10:17:29 +0200517 unsigned int done = 0;
518 unsigned int queue;
Willy Tarreau144f84a2021-03-02 16:09:26 +0100519 unsigned int state;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100520 void *ctx;
Willy Tarreau59153fe2020-06-24 10:17:29 +0200521
522 for (queue = 0; queue < TL_CLASSES;) {
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200523 th_ctx->current_queue = queue;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100524
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200525 /* global.tune.sched.low-latency is set */
526 if (global.tune.options & GTUNE_SCHED_LOW_LATENCY) {
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200527 if (unlikely(th_ctx->tl_class_mask & budget_mask & ((1 << queue) - 1))) {
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200528 /* a lower queue index has tasks again and still has a
529 * budget to run them. Let's switch to it now.
530 */
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200531 queue = (th_ctx->tl_class_mask & 1) ? 0 :
532 (th_ctx->tl_class_mask & 2) ? 1 : 2;
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200533 continue;
534 }
535
536 if (unlikely(queue > TL_URGENT &&
537 budget_mask & (1 << TL_URGENT) &&
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200538 !MT_LIST_ISEMPTY(&th_ctx->shared_tasklet_list))) {
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200539 /* an urgent tasklet arrived from another thread */
540 break;
541 }
542
543 if (unlikely(queue > TL_NORMAL &&
544 budget_mask & (1 << TL_NORMAL) &&
Willy Tarreauc958c702022-06-16 15:59:36 +0200545 (!eb_is_empty(&th_ctx->rqueue) || !eb_is_empty(&th_ctx->rqueue_shared)))) {
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200546 /* a task was woken up by a bulk tasklet or another thread */
547 break;
548 }
549 }
550
Willy Tarreauba48d5c2020-06-24 09:54:24 +0200551 if (LIST_ISEMPTY(&tl_queues[queue])) {
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200552 th_ctx->tl_class_mask &= ~(1 << queue);
Willy Tarreau59153fe2020-06-24 10:17:29 +0200553 queue++;
554 continue;
Willy Tarreauba48d5c2020-06-24 09:54:24 +0200555 }
556
Willy Tarreau59153fe2020-06-24 10:17:29 +0200557 if (!budgets[queue]) {
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200558 budget_mask &= ~(1 << queue);
Willy Tarreau59153fe2020-06-24 10:17:29 +0200559 queue++;
560 continue;
561 }
Willy Tarreauba48d5c2020-06-24 09:54:24 +0200562
Willy Tarreau59153fe2020-06-24 10:17:29 +0200563 budgets[queue]--;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100564 activity[tid].ctxsw++;
Willy Tarreau3193eb92021-10-21 16:17:29 +0200565
566 t = (struct task *)LIST_ELEM(tl_queues[queue].n, struct tasklet *, list);
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100567 ctx = t->context;
568 process = t->process;
569 t->calls++;
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200570 th_ctx->current = t;
Willy Tarreau3193eb92021-10-21 16:17:29 +0200571 th_ctx->flags &= ~TH_FL_STUCK; // this thread is still running
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100572
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200573 _HA_ATOMIC_DEC(&th_ctx->rq_total);
Willy Tarreau2da4c312020-11-30 14:52:11 +0100574
Willy Tarreau3193eb92021-10-21 16:17:29 +0200575 if (t->state & TASK_F_TASKLET) {
Willy Tarreau2a54ffb2021-02-25 09:32:58 +0100576 uint64_t before = 0;
577
Willy Tarreau4d6c5942020-11-30 14:58:53 +0100578 LIST_DEL_INIT(&((struct tasklet *)t)->list);
579 __ha_barrier_store();
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100580
Willy Tarreau680ed5f2022-06-13 15:59:39 +0200581 if (unlikely(th_ctx->flags & TH_FL_TASK_PROFILING)) {
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100582 profile_entry = sched_activity_entry(sched_activity, t->process);
583 before = now_mono_time();
Willy Tarreaub2285de2021-02-25 08:39:07 +0100584#ifdef DEBUG_TASK
Willy Tarreau2a54ffb2021-02-25 09:32:58 +0100585 if (((struct tasklet *)t)->call_date) {
586 HA_ATOMIC_ADD(&profile_entry->lat_time, before - ((struct tasklet *)t)->call_date);
587 ((struct tasklet *)t)->call_date = 0;
588 }
Willy Tarreaub2285de2021-02-25 08:39:07 +0100589#endif
Willy Tarreau2a54ffb2021-02-25 09:32:58 +0100590 }
591
Willy Tarreau3193eb92021-10-21 16:17:29 +0200592 state = _HA_ATOMIC_FETCH_AND(&t->state, TASK_PERSISTENT);
Willy Tarreau2a54ffb2021-02-25 09:32:58 +0100593 __ha_barrier_atomic_store();
594
Amaury Denoyelle7b368332021-07-28 16:12:57 +0200595 if (likely(!(state & TASK_KILLED))) {
596 process(t, ctx, state);
597 }
598 else {
599 done++;
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200600 th_ctx->current = NULL;
Amaury Denoyelle7b368332021-07-28 16:12:57 +0200601 pool_free(pool_head_tasklet, t);
602 __ha_barrier_store();
603 continue;
604 }
Willy Tarreau2a54ffb2021-02-25 09:32:58 +0100605
Willy Tarreau680ed5f2022-06-13 15:59:39 +0200606 if (unlikely(th_ctx->flags & TH_FL_TASK_PROFILING)) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200607 HA_ATOMIC_INC(&profile_entry->calls);
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100608 HA_ATOMIC_ADD(&profile_entry->cpu_time, now_mono_time() - before);
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100609 }
Willy Tarreau2a54ffb2021-02-25 09:32:58 +0100610
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100611 done++;
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200612 th_ctx->current = NULL;
Willy Tarreaud23d4132020-01-31 10:39:03 +0100613 __ha_barrier_store();
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100614 continue;
615 }
616
Willy Tarreau4d6c5942020-11-30 14:58:53 +0100617 LIST_DEL_INIT(&((struct tasklet *)t)->list);
618 __ha_barrier_store();
Willy Tarreau3193eb92021-10-21 16:17:29 +0200619
Willy Tarreau6c8babf2022-02-14 10:18:51 +0100620 /* We must be the exclusive owner of the TASK_RUNNING bit, and
621 * have to be careful that the task is not being manipulated on
622 * another thread finding it expired in wake_expired_tasks().
623 * The TASK_RUNNING bit will be set during these operations,
624 * they are extremely rare and do not last long so the best to
625 * do here is to wait.
626 */
627 state = _HA_ATOMIC_LOAD(&t->state);
628 do {
629 while (unlikely(state & TASK_RUNNING)) {
630 __ha_cpu_relax();
631 state = _HA_ATOMIC_LOAD(&t->state);
632 }
633 } while (!_HA_ATOMIC_CAS(&t->state, &state, (state & TASK_PERSISTENT) | TASK_RUNNING));
Willy Tarreau3193eb92021-10-21 16:17:29 +0200634
Willy Tarreau952c2642020-01-31 16:39:30 +0100635 __ha_barrier_atomic_store();
Willy Tarreau952c2642020-01-31 16:39:30 +0100636
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100637 /* OK then this is a regular task */
638
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200639 _HA_ATOMIC_DEC(&ha_thread_ctx[tid].tasks_in_list);
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100640 if (unlikely(t->call_date)) {
641 uint64_t now_ns = now_mono_time();
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100642 uint64_t lat = now_ns - t->call_date;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100643
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100644 t->lat_time += lat;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100645 t->call_date = now_ns;
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100646 profile_entry = sched_activity_entry(sched_activity, t->process);
647 HA_ATOMIC_ADD(&profile_entry->lat_time, lat);
Willy Tarreau4781b152021-04-06 13:53:36 +0200648 HA_ATOMIC_INC(&profile_entry->calls);
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100649 }
650
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100651 __ha_barrier_store();
Willy Tarreau8a6049c2020-06-30 11:48:48 +0200652
653 /* Note for below: if TASK_KILLED arrived before we've read the state, we
654 * directly free the task. Otherwise it will be seen after processing and
655 * it's freed on the exit path.
656 */
657 if (likely(!(state & TASK_KILLED) && process == process_stream))
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100658 t = process_stream(t, ctx, state);
Willy Tarreau8a6049c2020-06-30 11:48:48 +0200659 else if (!(state & TASK_KILLED) && process != NULL)
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100660 t = process(t, ctx, state);
661 else {
Willy Tarreau273aea42020-07-17 14:37:51 +0200662 task_unlink_wq(t);
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100663 __task_free(t);
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200664 th_ctx->current = NULL;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100665 __ha_barrier_store();
666 /* We don't want max_processed to be decremented if
667 * we're just freeing a destroyed task, we should only
668 * do so if we really ran a task.
669 */
670 continue;
671 }
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200672 th_ctx->current = NULL;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100673 __ha_barrier_store();
674 /* If there is a pending state we have to wake up the task
675 * immediately, else we defer it into wait queue
676 */
677 if (t != NULL) {
678 if (unlikely(t->call_date)) {
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100679 uint64_t cpu = now_mono_time() - t->call_date;
680
681 t->cpu_time += cpu;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100682 t->call_date = 0;
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100683 HA_ATOMIC_ADD(&profile_entry->cpu_time, cpu);
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100684 }
685
Willy Tarreau6c8babf2022-02-14 10:18:51 +0100686 state = _HA_ATOMIC_LOAD(&t->state);
Willy Tarreau8a6049c2020-06-30 11:48:48 +0200687 if (unlikely(state & TASK_KILLED)) {
Willy Tarreau273aea42020-07-17 14:37:51 +0200688 task_unlink_wq(t);
Willy Tarreau8a6049c2020-06-30 11:48:48 +0200689 __task_free(t);
690 }
Willy Tarreau6c8babf2022-02-14 10:18:51 +0100691 else {
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100692 task_queue(t);
Willy Tarreau6c8babf2022-02-14 10:18:51 +0100693 task_drop_running(t, 0);
694 }
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100695 }
696 done++;
697 }
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200698 th_ctx->current_queue = -1;
Willy Tarreau116ef222020-06-23 16:35:38 +0200699
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100700 return done;
701}
702
Willy Tarreau58b458d2008-06-29 22:40:23 +0200703/* The run queue is chronologically sorted in a tree. An insertion counter is
704 * used to assign a position to each task. This counter may be combined with
705 * other variables (eg: nice value) to set the final position in the tree. The
706 * counter may wrap without a problem, of course. We then limit the number of
Christopher Faulet8a48f672017-11-14 10:38:36 +0100707 * tasks processed to 200 in any case, so that general latency remains low and
Willy Tarreaucde79022019-04-12 18:03:41 +0200708 * so that task positions have a chance to be considered. The function scans
709 * both the global and local run queues and picks the most urgent task between
710 * the two. We need to grab the global runqueue lock to touch it so it's taken
711 * on the very first access to the global run queue and is released as soon as
712 * it reaches the end.
Willy Tarreau58b458d2008-06-29 22:40:23 +0200713 *
714 * The function adjusts <next> if a new event is closer.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200715 */
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +0100716void process_runnable_tasks()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717{
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200718 struct thread_ctx * const tt = th_ctx;
Willy Tarreau319d1362022-06-16 16:28:01 +0200719 struct eb32_node *lrq; // next local run queue entry
720 struct eb32_node *grq; // next global run queue entry
Willy Tarreau964c9362007-01-07 00:38:00 +0100721 struct task *t;
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200722 const unsigned int default_weights[TL_CLASSES] = {
723 [TL_URGENT] = 64, // ~50% of CPU bandwidth for I/O
724 [TL_NORMAL] = 48, // ~37% of CPU bandwidth for tasks
725 [TL_BULK] = 16, // ~13% of CPU bandwidth for self-wakers
Willy Tarreau401135c2021-02-26 09:16:22 +0100726 [TL_HEAVY] = 1, // never more than 1 heavy task at once
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200727 };
728 unsigned int max[TL_CLASSES]; // max to be run per class
729 unsigned int max_total; // sum of max above
Olivier Houchard06910462019-10-11 16:35:01 +0200730 struct mt_list *tmp_list;
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200731 unsigned int queue;
732 int max_processed;
Willy Tarreaue7923c12021-02-25 07:09:08 +0100733 int lpicked, gpicked;
Willy Tarreau76390da2021-02-26 10:18:11 +0100734 int heavy_queued = 0;
Willy Tarreauc309dbd2020-11-30 15:39:00 +0100735 int budget;
Christopher Faulet3911ee82017-11-14 10:26:53 +0100736
Willy Tarreaua0b99532021-09-30 18:48:37 +0200737 th_ctx->flags &= ~TH_FL_STUCK; // this thread is still running
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200738
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200739 if (!thread_has_tasks()) {
740 activity[tid].empty_rq++;
741 return;
742 }
743
Willy Tarreau5c8be272020-06-19 12:17:55 +0200744 max_processed = global.tune.runqueue_depth;
745
746 if (likely(niced_tasks))
747 max_processed = (max_processed + 3) / 4;
748
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200749 if (max_processed < th_ctx->rq_total && th_ctx->rq_total <= 2*max_processed) {
Willy Tarreau1691ba32021-03-10 09:26:24 +0100750 /* If the run queue exceeds the budget by up to 50%, let's cut it
751 * into two identical halves to improve latency.
752 */
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200753 max_processed = th_ctx->rq_total / 2;
Willy Tarreau1691ba32021-03-10 09:26:24 +0100754 }
755
Willy Tarreau5c8be272020-06-19 12:17:55 +0200756 not_done_yet:
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200757 max[TL_URGENT] = max[TL_NORMAL] = max[TL_BULK] = 0;
Willy Tarreaucde79022019-04-12 18:03:41 +0200758
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200759 /* urgent tasklets list gets a default weight of ~50% */
Willy Tarreau49f90bf2020-06-24 09:39:48 +0200760 if ((tt->tl_class_mask & (1 << TL_URGENT)) ||
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200761 !MT_LIST_ISEMPTY(&tt->shared_tasklet_list))
762 max[TL_URGENT] = default_weights[TL_URGENT];
Willy Tarreaua62917b2020-01-30 18:37:28 +0100763
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200764 /* normal tasklets list gets a default weight of ~37% */
Willy Tarreau49f90bf2020-06-24 09:39:48 +0200765 if ((tt->tl_class_mask & (1 << TL_NORMAL)) ||
Willy Tarreauc958c702022-06-16 15:59:36 +0200766 !eb_is_empty(&th_ctx->rqueue) || !eb_is_empty(&th_ctx->rqueue_shared))
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200767 max[TL_NORMAL] = default_weights[TL_NORMAL];
Willy Tarreaua62917b2020-01-30 18:37:28 +0100768
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200769 /* bulk tasklets list gets a default weight of ~13% */
Willy Tarreau49f90bf2020-06-24 09:39:48 +0200770 if ((tt->tl_class_mask & (1 << TL_BULK)))
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200771 max[TL_BULK] = default_weights[TL_BULK];
772
Willy Tarreau401135c2021-02-26 09:16:22 +0100773 /* heavy tasks are processed only once and never refilled in a
Willy Tarreau76390da2021-02-26 10:18:11 +0100774 * call round. That budget is not lost either as we don't reset
775 * it unless consumed.
Willy Tarreau401135c2021-02-26 09:16:22 +0100776 */
Willy Tarreau76390da2021-02-26 10:18:11 +0100777 if (!heavy_queued) {
778 if ((tt->tl_class_mask & (1 << TL_HEAVY)))
779 max[TL_HEAVY] = default_weights[TL_HEAVY];
780 else
781 max[TL_HEAVY] = 0;
782 heavy_queued = 1;
783 }
Willy Tarreau401135c2021-02-26 09:16:22 +0100784
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200785 /* Now compute a fair share of the weights. Total may slightly exceed
Willy Tarreau1553b662020-06-30 13:46:21 +0200786 * 100% due to rounding, this is not a problem. Note that while in
787 * theory the sum cannot be NULL as we cannot get there without tasklets
788 * to process, in practice it seldom happens when multiple writers
Willy Tarreau2b718102021-04-21 07:32:39 +0200789 * conflict and rollback on MT_LIST_TRY_APPEND(shared_tasklet_list), causing
Willy Tarreau1553b662020-06-30 13:46:21 +0200790 * a first MT_LIST_ISEMPTY() to succeed for thread_has_task() and the
791 * one above to finally fail. This is extremely rare and not a problem.
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200792 */
Willy Tarreau401135c2021-02-26 09:16:22 +0100793 max_total = max[TL_URGENT] + max[TL_NORMAL] + max[TL_BULK] + max[TL_HEAVY];
Willy Tarreau1553b662020-06-30 13:46:21 +0200794 if (!max_total)
795 return;
796
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200797 for (queue = 0; queue < TL_CLASSES; queue++)
798 max[queue] = ((unsigned)max_processed * max[queue] + max_total - 1) / max_total;
799
Willy Tarreau76390da2021-02-26 10:18:11 +0100800 /* The heavy queue must never process more than one task at once
801 * anyway.
802 */
803 if (max[TL_HEAVY] > 1)
804 max[TL_HEAVY] = 1;
805
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200806 lrq = grq = NULL;
Christopher Faulet8a48f672017-11-14 10:38:36 +0100807
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200808 /* pick up to max[TL_NORMAL] regular tasks from prio-ordered run queues */
809 /* Note: the grq lock is always held when grq is not null */
Willy Tarreaue7923c12021-02-25 07:09:08 +0100810 lpicked = gpicked = 0;
Willy Tarreau1f3b1412021-02-24 14:13:40 +0100811 budget = max[TL_NORMAL] - tt->tasks_in_list;
Willy Tarreaue7923c12021-02-25 07:09:08 +0100812 while (lpicked + gpicked < budget) {
Willy Tarreauc958c702022-06-16 15:59:36 +0200813 if (!eb_is_empty(&th_ctx->rqueue_shared) && !grq) {
Willy Tarreau3466e3c2019-04-15 18:52:40 +0200814#ifdef USE_THREAD
Willy Tarreaub17dd6c2022-06-16 16:58:17 +0200815 HA_SPIN_LOCK(TASK_RQ_LOCK, &th_ctx->rqsh_lock);
Willy Tarreau319d1362022-06-16 16:28:01 +0200816 grq = eb32_lookup_ge(&th_ctx->rqueue_shared, _HA_ATOMIC_LOAD(&tt->rqueue_ticks) - TIMER_LOOK_BACK);
Willy Tarreaucde79022019-04-12 18:03:41 +0200817 if (unlikely(!grq)) {
Willy Tarreau319d1362022-06-16 16:28:01 +0200818 grq = eb32_first(&th_ctx->rqueue_shared);
Willy Tarreauc958c702022-06-16 15:59:36 +0200819 if (!grq)
Willy Tarreaub17dd6c2022-06-16 16:58:17 +0200820 HA_SPIN_UNLOCK(TASK_RQ_LOCK, &th_ctx->rqsh_lock);
Willy Tarreauf0c531a2017-11-05 16:35:59 +0100821 }
Willy Tarreau3466e3c2019-04-15 18:52:40 +0200822#endif
Willy Tarreaucde79022019-04-12 18:03:41 +0200823 }
Willy Tarreauf0c531a2017-11-05 16:35:59 +0100824
Willy Tarreaucde79022019-04-12 18:03:41 +0200825 /* If a global task is available for this thread, it's in grq
826 * now and the global RQ is locked.
827 */
Olivier Houchardf6e6dc12018-05-18 18:38:23 +0200828
Willy Tarreaucde79022019-04-12 18:03:41 +0200829 if (!lrq) {
Willy Tarreau319d1362022-06-16 16:28:01 +0200830 lrq = eb32_lookup_ge(&tt->rqueue, _HA_ATOMIC_LOAD(&tt->rqueue_ticks) - TIMER_LOOK_BACK);
Willy Tarreaucde79022019-04-12 18:03:41 +0200831 if (unlikely(!lrq))
Willy Tarreau319d1362022-06-16 16:28:01 +0200832 lrq = eb32_first(&tt->rqueue);
Willy Tarreauf0c531a2017-11-05 16:35:59 +0100833 }
Willy Tarreauf0c531a2017-11-05 16:35:59 +0100834
Willy Tarreaucde79022019-04-12 18:03:41 +0200835 if (!lrq && !grq)
836 break;
837
838 if (likely(!grq || (lrq && (int)(lrq->key - grq->key) <= 0))) {
Willy Tarreau319d1362022-06-16 16:28:01 +0200839 t = eb32_entry(lrq, struct task, rq);
840 lrq = eb32_next(lrq);
841 eb32_delete(&t->rq);
Willy Tarreaue7923c12021-02-25 07:09:08 +0100842 lpicked++;
Olivier Houchardf6e6dc12018-05-18 18:38:23 +0200843 }
Willy Tarreau3466e3c2019-04-15 18:52:40 +0200844#ifdef USE_THREAD
Willy Tarreaucde79022019-04-12 18:03:41 +0200845 else {
Willy Tarreau319d1362022-06-16 16:28:01 +0200846 t = eb32_entry(grq, struct task, rq);
847 grq = eb32_next(grq);
848 eb32_delete(&t->rq);
Willy Tarreau2b363ac2021-02-25 07:14:58 +0100849
Willy Tarreaucde79022019-04-12 18:03:41 +0200850 if (unlikely(!grq)) {
Willy Tarreau319d1362022-06-16 16:28:01 +0200851 grq = eb32_first(&th_ctx->rqueue_shared);
Willy Tarreauc958c702022-06-16 15:59:36 +0200852 if (!grq)
Willy Tarreaub17dd6c2022-06-16 16:58:17 +0200853 HA_SPIN_UNLOCK(TASK_RQ_LOCK, &th_ctx->rqsh_lock);
Willy Tarreaucde79022019-04-12 18:03:41 +0200854 }
Willy Tarreaue7923c12021-02-25 07:09:08 +0100855 gpicked++;
Emeric Brun01948972017-03-30 15:37:25 +0200856 }
Willy Tarreau3466e3c2019-04-15 18:52:40 +0200857#endif
Willy Tarreau2b363ac2021-02-25 07:14:58 +0100858 if (t->nice)
Willy Tarreau4781b152021-04-06 13:53:36 +0200859 _HA_ATOMIC_DEC(&niced_tasks);
Willy Tarreaucde79022019-04-12 18:03:41 +0200860
Willy Tarreaua868c292020-11-30 15:30:22 +0100861 /* Add it to the local task list */
Willy Tarreau2b718102021-04-21 07:32:39 +0200862 LIST_APPEND(&tt->tasklets[TL_NORMAL], &((struct tasklet *)t)->list);
Olivier Houchardb0bdae72018-05-18 18:45:28 +0200863 }
Willy Tarreaucde79022019-04-12 18:03:41 +0200864
865 /* release the rqueue lock */
866 if (grq) {
Willy Tarreaub17dd6c2022-06-16 16:58:17 +0200867 HA_SPIN_UNLOCK(TASK_RQ_LOCK, &th_ctx->rqsh_lock);
Willy Tarreaucde79022019-04-12 18:03:41 +0200868 grq = NULL;
869 }
870
Willy Tarreaue7923c12021-02-25 07:09:08 +0100871 if (lpicked + gpicked) {
Willy Tarreauc309dbd2020-11-30 15:39:00 +0100872 tt->tl_class_mask |= 1 << TL_NORMAL;
Willy Tarreaue7923c12021-02-25 07:09:08 +0100873 _HA_ATOMIC_ADD(&tt->tasks_in_list, lpicked + gpicked);
Willy Tarreaue7923c12021-02-25 07:09:08 +0100874 activity[tid].tasksw += lpicked + gpicked;
Willy Tarreauc309dbd2020-11-30 15:39:00 +0100875 }
876
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200877 /* Merge the list of tasklets waken up by other threads to the
878 * main list.
879 */
880 tmp_list = MT_LIST_BEHEAD(&tt->shared_tasklet_list);
Willy Tarreau49f90bf2020-06-24 09:39:48 +0200881 if (tmp_list) {
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200882 LIST_SPLICE_END_DETACHED(&tt->tasklets[TL_URGENT], (struct list *)tmp_list);
Willy Tarreau49f90bf2020-06-24 09:39:48 +0200883 if (!LIST_ISEMPTY(&tt->tasklets[TL_URGENT]))
884 tt->tl_class_mask |= 1 << TL_URGENT;
885 }
Willy Tarreaucde79022019-04-12 18:03:41 +0200886
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200887 /* execute tasklets in each queue */
Willy Tarreau59153fe2020-06-24 10:17:29 +0200888 max_processed -= run_tasks_from_lists(max);
Willy Tarreaua62917b2020-01-30 18:37:28 +0100889
Willy Tarreau5c8be272020-06-19 12:17:55 +0200890 /* some tasks may have woken other ones up */
Willy Tarreau0c0c85e2020-06-23 11:32:35 +0200891 if (max_processed > 0 && thread_has_tasks())
Willy Tarreau5c8be272020-06-19 12:17:55 +0200892 goto not_done_yet;
893
Willy Tarreau49f90bf2020-06-24 09:39:48 +0200894 if (tt->tl_class_mask)
Willy Tarreaucde79022019-04-12 18:03:41 +0200895 activity[tid].long_rq++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200896}
897
William Lallemand27f3fa52018-12-06 14:05:20 +0100898/*
899 * Delete every tasks before running the master polling loop
900 */
901void mworker_cleantasks()
902{
903 struct task *t;
904 int i;
William Lallemandb5823392018-12-06 15:14:37 +0100905 struct eb32_node *tmp_wq = NULL;
Willy Tarreau319d1362022-06-16 16:28:01 +0200906 struct eb32_node *tmp_rq = NULL;
William Lallemand27f3fa52018-12-06 14:05:20 +0100907
908#ifdef USE_THREAD
909 /* cleanup the global run queue */
Willy Tarreau319d1362022-06-16 16:28:01 +0200910 tmp_rq = eb32_first(&th_ctx->rqueue_shared);
William Lallemandb5823392018-12-06 15:14:37 +0100911 while (tmp_rq) {
Willy Tarreau319d1362022-06-16 16:28:01 +0200912 t = eb32_entry(tmp_rq, struct task, rq);
913 tmp_rq = eb32_next(tmp_rq);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200914 task_destroy(t);
William Lallemand27f3fa52018-12-06 14:05:20 +0100915 }
916 /* cleanup the timers queue */
William Lallemandb5823392018-12-06 15:14:37 +0100917 tmp_wq = eb32_first(&timers);
918 while (tmp_wq) {
919 t = eb32_entry(tmp_wq, struct task, wq);
920 tmp_wq = eb32_next(tmp_wq);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200921 task_destroy(t);
William Lallemand27f3fa52018-12-06 14:05:20 +0100922 }
923#endif
924 /* clean the per thread run queue */
925 for (i = 0; i < global.nbthread; i++) {
Willy Tarreau319d1362022-06-16 16:28:01 +0200926 tmp_rq = eb32_first(&ha_thread_ctx[i].rqueue);
William Lallemandb5823392018-12-06 15:14:37 +0100927 while (tmp_rq) {
Willy Tarreau319d1362022-06-16 16:28:01 +0200928 t = eb32_entry(tmp_rq, struct task, rq);
929 tmp_rq = eb32_next(tmp_rq);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200930 task_destroy(t);
William Lallemand27f3fa52018-12-06 14:05:20 +0100931 }
932 /* cleanup the per thread timers queue */
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200933 tmp_wq = eb32_first(&ha_thread_ctx[i].timers);
William Lallemandb5823392018-12-06 15:14:37 +0100934 while (tmp_wq) {
935 t = eb32_entry(tmp_wq, struct task, wq);
936 tmp_wq = eb32_next(tmp_wq);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200937 task_destroy(t);
William Lallemand27f3fa52018-12-06 14:05:20 +0100938 }
939 }
940}
941
Willy Tarreaub6b3df32018-11-26 16:31:20 +0100942/* perform minimal intializations */
943static void init_task()
Willy Tarreau4726f532009-03-07 17:25:21 +0100944{
Willy Tarreau401135c2021-02-26 09:16:22 +0100945 int i, q;
Olivier Houchardf6e6dc12018-05-18 18:38:23 +0200946
Olivier Houchardb1ca58b2018-06-06 14:22:03 +0200947#ifdef USE_THREAD
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200948 memset(&timers, 0, sizeof(timers));
Olivier Houchardb1ca58b2018-06-06 14:22:03 +0200949#endif
Olivier Houchardb0bdae72018-05-18 18:45:28 +0200950 for (i = 0; i < MAX_THREADS; i++) {
Willy Tarreau401135c2021-02-26 09:16:22 +0100951 for (q = 0; q < TL_CLASSES; q++)
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200952 LIST_INIT(&ha_thread_ctx[i].tasklets[q]);
953 MT_LIST_INIT(&ha_thread_ctx[i].shared_tasklet_list);
Olivier Houchardb0bdae72018-05-18 18:45:28 +0200954 }
Willy Tarreau4726f532009-03-07 17:25:21 +0100955}
956
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200957/* config parser for global "tune.sched.low-latency", accepts "on" or "off" */
958static int cfg_parse_tune_sched_low_latency(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100959 const struct proxy *defpx, const char *file, int line,
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200960 char **err)
961{
962 if (too_many_args(1, args, err, NULL))
963 return -1;
964
965 if (strcmp(args[1], "on") == 0)
966 global.tune.options |= GTUNE_SCHED_LOW_LATENCY;
967 else if (strcmp(args[1], "off") == 0)
968 global.tune.options &= ~GTUNE_SCHED_LOW_LATENCY;
969 else {
970 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
971 return -1;
972 }
973 return 0;
974}
975
976/* config keyword parsers */
977static struct cfg_kw_list cfg_kws = {ILH, {
978 { CFG_GLOBAL, "tune.sched.low-latency", cfg_parse_tune_sched_low_latency },
979 { 0, NULL, NULL }
980}};
981
982INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Willy Tarreaub6b3df32018-11-26 16:31:20 +0100983INITCALL0(STG_PREPARE, init_task);
984
Willy Tarreaubaaee002006-06-26 02:48:02 +0200985/*
986 * Local variables:
987 * c-indent-level: 8
988 * c-basic-offset: 8
989 * End:
990 */