blob: faf21f3f1d3c88bbe63ddd306984b119ec68bd6d [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 Tarreau6a28a302022-09-07 09:17:45 +020028extern void stream_update_timings(struct task *t, uint64_t lat, uint64_t cpu);
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreau8ceae722018-11-26 11:58:30 +010030DECLARE_POOL(pool_head_task, "task", sizeof(struct task));
31DECLARE_POOL(pool_head_tasklet, "tasklet", sizeof(struct tasklet));
Willy Tarreau96bcfd72007-04-29 10:41:56 +020032
Thierry FOURNIERd6975962017-07-12 14:31:10 +020033/* This is the memory pool containing all the signal structs. These
Joseph Herlantcf92b6d2018-11-15 14:19:23 -080034 * struct are used to store each required signal between two tasks.
Thierry FOURNIERd6975962017-07-12 14:31:10 +020035 */
Willy Tarreau8ceae722018-11-26 11:58:30 +010036DECLARE_POOL(pool_head_notification, "notification", sizeof(struct notification));
Thierry FOURNIERd6975962017-07-12 14:31:10 +020037
Willy Tarreaufc50b9d2022-11-22 07:05:44 +010038/* The lock protecting all wait queues at once. For now we have no better
39 * alternative since a task may have to be removed from a queue and placed
40 * into another one. Storing the WQ index into the task doesn't seem to be
41 * sufficient either.
42 */
Willy Tarreau5ec79f12022-11-22 10:24:07 +010043__decl_aligned_rwlock(wq_lock);
Willy Tarreaueb8c2c62020-06-30 11:48:48 +020044
45/* Flags the task <t> for immediate destruction and puts it into its first
46 * thread's shared tasklet list if not yet queued/running. This will bypass
47 * the priority scheduling and make the task show up as fast as possible in
48 * the other thread's queue. Note that this operation isn't idempotent and is
49 * not supposed to be run on the same task from multiple threads at once. It's
50 * the caller's responsibility to make sure it is the only one able to kill the
51 * task.
52 */
53void task_kill(struct task *t)
54{
Willy Tarreau144f84a2021-03-02 16:09:26 +010055 unsigned int state = t->state;
Willy Tarreaueb8c2c62020-06-30 11:48:48 +020056 unsigned int thr;
57
58 BUG_ON(state & TASK_KILLED);
59
60 while (1) {
61 while (state & (TASK_RUNNING | TASK_QUEUED)) {
62 /* task already in the queue and about to be executed,
63 * or even currently running. Just add the flag and be
64 * done with it, the process loop will detect it and kill
65 * it. The CAS will fail if we arrive too late.
66 */
67 if (_HA_ATOMIC_CAS(&t->state, &state, state | TASK_KILLED))
68 return;
69 }
70
71 /* We'll have to wake it up, but we must also secure it so that
72 * it doesn't vanish under us. TASK_QUEUED guarantees nobody will
73 * add past us.
74 */
75 if (_HA_ATOMIC_CAS(&t->state, &state, state | TASK_QUEUED | TASK_KILLED)) {
76 /* Bypass the tree and go directly into the shared tasklet list.
77 * Note: that's a task so it must be accounted for as such. Pick
78 * the task's first thread for the job.
79 */
Willy Tarreau29ffe262022-06-15 14:31:38 +020080 thr = t->tid >= 0 ? t->tid : tid;
Willy Tarreau54d31172020-07-02 14:14:00 +020081
82 /* Beware: tasks that have never run don't have their ->list empty yet! */
Willy Tarreau1a9c9222021-10-01 11:30:33 +020083 MT_LIST_APPEND(&ha_thread_ctx[thr].shared_tasklet_list,
Willy Tarreaucc5cd5b2022-01-28 09:48:12 +010084 list_to_mt_list(&((struct tasklet *)t)->list));
Willy Tarreau1a9c9222021-10-01 11:30:33 +020085 _HA_ATOMIC_INC(&ha_thread_ctx[thr].rq_total);
86 _HA_ATOMIC_INC(&ha_thread_ctx[thr].tasks_in_list);
Willy Tarreauf3efef42022-06-20 09:14:40 +020087 wake_thread(thr);
Willy Tarreau54d31172020-07-02 14:14:00 +020088 return;
Willy Tarreaueb8c2c62020-06-30 11:48:48 +020089 }
90 }
91}
92
Amaury Denoyelle7b368332021-07-28 16:12:57 +020093/* Equivalent of task_kill for tasklets. Mark the tasklet <t> for destruction.
94 * It will be deleted on the next scheduler invocation. This function is
95 * thread-safe : a thread can kill a tasklet of another thread.
96 */
97void tasklet_kill(struct tasklet *t)
98{
99 unsigned int state = t->state;
100 unsigned int thr;
101
102 BUG_ON(state & TASK_KILLED);
103
104 while (1) {
105 while (state & (TASK_IN_LIST)) {
106 /* Tasklet already in the list ready to be executed. Add
107 * the killed flag and wait for the process loop to
108 * detect it.
109 */
110 if (_HA_ATOMIC_CAS(&t->state, &state, state | TASK_KILLED))
111 return;
112 }
113
114 /* Mark the tasklet as killed and wake the thread to process it
115 * as soon as possible.
116 */
117 if (_HA_ATOMIC_CAS(&t->state, &state, state | TASK_IN_LIST | TASK_KILLED)) {
Willy Tarreau9b3aa632022-06-15 15:54:56 +0200118 thr = t->tid >= 0 ? t->tid : tid;
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200119 MT_LIST_APPEND(&ha_thread_ctx[thr].shared_tasklet_list,
Willy Tarreaucc5cd5b2022-01-28 09:48:12 +0100120 list_to_mt_list(&t->list));
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200121 _HA_ATOMIC_INC(&ha_thread_ctx[thr].rq_total);
Willy Tarreauf3efef42022-06-20 09:14:40 +0200122 wake_thread(thr);
Amaury Denoyelle7b368332021-07-28 16:12:57 +0200123 return;
124 }
125 }
126}
127
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100128/* Do not call this one, please use tasklet_wakeup_on() instead, as this one is
129 * the slow path of tasklet_wakeup_on() which performs some preliminary checks
130 * and sets TASK_IN_LIST before calling this one. A negative <thr> designates
131 * the current thread.
132 */
133void __tasklet_wakeup_on(struct tasklet *tl, int thr)
134{
135 if (likely(thr < 0)) {
136 /* this tasklet runs on the caller thread */
Willy Tarreau826fa872021-02-26 10:13:40 +0100137 if (tl->state & TASK_HEAVY) {
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200138 LIST_APPEND(&th_ctx->tasklets[TL_HEAVY], &tl->list);
139 th_ctx->tl_class_mask |= 1 << TL_HEAVY;
Willy Tarreau826fa872021-02-26 10:13:40 +0100140 }
141 else if (tl->state & TASK_SELF_WAKING) {
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200142 LIST_APPEND(&th_ctx->tasklets[TL_BULK], &tl->list);
143 th_ctx->tl_class_mask |= 1 << TL_BULK;
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100144 }
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200145 else if ((struct task *)tl == th_ctx->current) {
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100146 _HA_ATOMIC_OR(&tl->state, TASK_SELF_WAKING);
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200147 LIST_APPEND(&th_ctx->tasklets[TL_BULK], &tl->list);
148 th_ctx->tl_class_mask |= 1 << TL_BULK;
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100149 }
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200150 else if (th_ctx->current_queue < 0) {
151 LIST_APPEND(&th_ctx->tasklets[TL_URGENT], &tl->list);
152 th_ctx->tl_class_mask |= 1 << TL_URGENT;
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100153 }
154 else {
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200155 LIST_APPEND(&th_ctx->tasklets[th_ctx->current_queue], &tl->list);
156 th_ctx->tl_class_mask |= 1 << th_ctx->current_queue;
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100157 }
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200158 _HA_ATOMIC_INC(&th_ctx->rq_total);
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100159 } else {
160 /* this tasklet runs on a specific thread. */
Willy Tarreaucc5cd5b2022-01-28 09:48:12 +0100161 MT_LIST_APPEND(&ha_thread_ctx[thr].shared_tasklet_list, list_to_mt_list(&tl->list));
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200162 _HA_ATOMIC_INC(&ha_thread_ctx[thr].rq_total);
Willy Tarreauf3efef42022-06-20 09:14:40 +0200163 wake_thread(thr);
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100164 }
165}
166
Frédéric Lécaillead548b52022-06-29 10:53:03 +0200167/* Do not call this one, please use tasklet_wakeup_after_on() instead, as this one is
168 * the slow path of tasklet_wakeup_after() which performs some preliminary checks
169 * and sets TASK_IN_LIST before calling this one.
170 */
171struct list *__tasklet_wakeup_after(struct list *head, struct tasklet *tl)
172{
173 BUG_ON(tid != tl->tid);
174 /* this tasklet runs on the caller thread */
175 if (!head) {
176 if (tl->state & TASK_HEAVY) {
177 LIST_INSERT(&th_ctx->tasklets[TL_HEAVY], &tl->list);
178 th_ctx->tl_class_mask |= 1 << TL_HEAVY;
179 }
180 else if (tl->state & TASK_SELF_WAKING) {
181 LIST_INSERT(&th_ctx->tasklets[TL_BULK], &tl->list);
182 th_ctx->tl_class_mask |= 1 << TL_BULK;
183 }
184 else if ((struct task *)tl == th_ctx->current) {
185 _HA_ATOMIC_OR(&tl->state, TASK_SELF_WAKING);
186 LIST_INSERT(&th_ctx->tasklets[TL_BULK], &tl->list);
187 th_ctx->tl_class_mask |= 1 << TL_BULK;
188 }
189 else if (th_ctx->current_queue < 0) {
190 LIST_INSERT(&th_ctx->tasklets[TL_URGENT], &tl->list);
191 th_ctx->tl_class_mask |= 1 << TL_URGENT;
192 }
193 else {
194 LIST_INSERT(&th_ctx->tasklets[th_ctx->current_queue], &tl->list);
195 th_ctx->tl_class_mask |= 1 << th_ctx->current_queue;
196 }
197 }
198 else {
199 LIST_APPEND(head, &tl->list);
200 }
201 _HA_ATOMIC_INC(&th_ctx->rq_total);
202 return &tl->list;
203}
204
Willy Tarreau4726f532009-03-07 17:25:21 +0100205/* Puts the task <t> in run queue at a position depending on t->nice. <t> is
206 * returned. The nice value assigns boosts in 32th of the run queue size. A
Christopher Faulet34c5cc92016-12-06 09:15:30 +0100207 * nice value of -1024 sets the task to -tasks_run_queue*32, while a nice value
208 * of 1024 sets the task to tasks_run_queue*32. The state flags are cleared, so
209 * the caller will have to set its flags after this call.
Willy Tarreau4726f532009-03-07 17:25:21 +0100210 * The task must not already be in the run queue. If unsure, use the safer
211 * task_wakeup() function.
Willy Tarreau91e99932008-06-30 07:51:00 +0200212 */
Willy Tarreau018564e2021-02-24 16:41:11 +0100213void __task_wakeup(struct task *t)
Willy Tarreaue33aece2007-04-30 13:15:14 +0200214{
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200215 struct eb_root *root = &th_ctx->rqueue;
Willy Tarreau29ffe262022-06-15 14:31:38 +0200216 int thr __maybe_unused = t->tid >= 0 ? t->tid : tid;
Willy Tarreau018564e2021-02-24 16:41:11 +0100217
Olivier Houchardb1ca58b2018-06-06 14:22:03 +0200218#ifdef USE_THREAD
Willy Tarreau29ffe262022-06-15 14:31:38 +0200219 if (thr != tid) {
Willy Tarreau6f780382022-06-16 15:30:50 +0200220 root = &ha_thread_ctx[thr].rqueue_shared;
Willy Tarreau018564e2021-02-24 16:41:11 +0100221
Willy Tarreauda195e82022-06-16 15:52:49 +0200222 _HA_ATOMIC_INC(&ha_thread_ctx[thr].rq_total);
Willy Tarreaub17dd6c2022-06-16 16:58:17 +0200223 HA_SPIN_LOCK(TASK_RQ_LOCK, &ha_thread_ctx[thr].rqsh_lock);
Willy Tarreau9c7b8082021-02-24 15:10:07 +0100224
Willy Tarreau6f780382022-06-16 15:30:50 +0200225 t->rq.key = _HA_ATOMIC_ADD_FETCH(&ha_thread_ctx[thr].rqueue_ticks, 1);
Olivier Houcharded1a6a02019-04-18 14:12:51 +0200226 __ha_barrier_store();
Willy Tarreauc6ba9a02021-02-20 12:49:54 +0100227 } else
Olivier Houchardc4aac9e2018-07-26 15:25:49 +0200228#endif
Willy Tarreau9c7b8082021-02-24 15:10:07 +0100229 {
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200230 _HA_ATOMIC_INC(&th_ctx->rq_total);
Willy Tarreaua4fb79b2022-06-16 15:44:35 +0200231 t->rq.key = _HA_ATOMIC_ADD_FETCH(&th_ctx->rqueue_ticks, 1);
Willy Tarreau9c7b8082021-02-24 15:10:07 +0100232 }
Willy Tarreau91e99932008-06-30 07:51:00 +0200233
234 if (likely(t->nice)) {
235 int offset;
236
Willy Tarreau91a7c162022-07-07 15:25:40 +0200237 _HA_ATOMIC_INC(&tg_ctx->niced_tasks);
Willy Tarreau2d1fd0a2019-04-15 09:18:31 +0200238 offset = t->nice * (int)global.tune.runqueue_depth;
Willy Tarreau4726f532009-03-07 17:25:21 +0100239 t->rq.key += offset;
Willy Tarreau91e99932008-06-30 07:51:00 +0200240 }
241
Willy Tarreaubdcd3252022-06-22 09:19:46 +0200242 if (_HA_ATOMIC_LOAD(&th_ctx->flags) & TH_FL_TASK_PROFILING)
Willy Tarreau04e50b32022-09-07 14:49:50 +0200243 t->wake_date = now_mono_time();
Willy Tarreau9efd7452018-05-31 14:48:54 +0200244
Willy Tarreau319d1362022-06-16 16:28:01 +0200245 eb32_insert(root, &t->rq);
Willy Tarreau018564e2021-02-24 16:41:11 +0100246
Olivier Houchardb1ca58b2018-06-06 14:22:03 +0200247#ifdef USE_THREAD
Willy Tarreau29ffe262022-06-15 14:31:38 +0200248 if (thr != tid) {
Willy Tarreaub17dd6c2022-06-16 16:58:17 +0200249 HA_SPIN_UNLOCK(TASK_RQ_LOCK, &ha_thread_ctx[thr].rqsh_lock);
Willy Tarreau2c41d772021-02-24 16:13:03 +0100250
Willy Tarreaueeffb3d2021-02-24 16:44:51 +0100251 /* If all threads that are supposed to handle this task are sleeping,
252 * wake one.
253 */
Willy Tarreauf3efef42022-06-20 09:14:40 +0200254 wake_thread(thr);
Olivier Houchard1b327902019-03-15 00:23:10 +0100255 }
Willy Tarreau85d9b842018-07-27 17:14:41 +0200256#endif
Olivier Houchardf6e6dc12018-05-18 18:38:23 +0200257 return;
Willy Tarreaue33aece2007-04-30 13:15:14 +0200258}
Willy Tarreaud825eef2007-05-12 22:35:00 +0200259
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200260/*
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100261 * __task_queue()
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200262 *
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200263 * Inserts a task into wait queue <wq> at the position given by its expiration
Willy Tarreau4726f532009-03-07 17:25:21 +0100264 * date. It does not matter if the task was already in the wait queue or not,
Willy Tarreau7a969992021-09-30 16:38:09 +0200265 * as it will be unlinked. The task MUST NOT have an infinite expiration timer.
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100266 * Last, tasks must not be queued further than the end of the tree, which is
267 * between <now_ms> and <now_ms> + 2^31 ms (now+24days in 32bit).
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100268 *
269 * This function should not be used directly, it is meant to be called by the
270 * inline version of task_queue() which performs a few cheap preliminary tests
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200271 * before deciding to call __task_queue(). Moreover this function doesn't care
272 * at all about locking so the caller must be careful when deciding whether to
273 * lock or not around this call.
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200274 */
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200275void __task_queue(struct task *task, struct eb_root *wq)
Willy Tarreau964c9362007-01-07 00:38:00 +0100276{
Willy Tarreaue5d79bc2020-07-22 14:29:42 +0200277#ifdef USE_THREAD
Willy Tarreaub0e77122022-07-07 15:22:55 +0200278 BUG_ON((wq == &tg_ctx->timers && task->tid >= 0) ||
Willy Tarreau159e3ac2022-06-15 16:48:45 +0200279 (wq == &th_ctx->timers && task->tid < 0) ||
Willy Tarreaub0e77122022-07-07 15:22:55 +0200280 (wq != &tg_ctx->timers && wq != &th_ctx->timers));
Willy Tarreaue5d79bc2020-07-22 14:29:42 +0200281#endif
Willy Tarreau7a969992021-09-30 16:38:09 +0200282 /* if this happens the process is doomed anyway, so better catch it now
283 * so that we have the caller in the stack.
284 */
285 BUG_ON(task->expire == TICK_ETERNITY);
Willy Tarreaue5d79bc2020-07-22 14:29:42 +0200286
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100287 if (likely(task_in_wq(task)))
Willy Tarreau4726f532009-03-07 17:25:21 +0100288 __task_unlink_wq(task);
Willy Tarreau4726f532009-03-07 17:25:21 +0100289
290 /* the task is not in the queue now */
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100291 task->wq.key = task->expire;
Willy Tarreau28c41a42008-06-29 17:00:59 +0200292#ifdef DEBUG_CHECK_INVALID_EXPIRATION_DATES
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100293 if (tick_is_lt(task->wq.key, now_ms))
Willy Tarreau28c41a42008-06-29 17:00:59 +0200294 /* we're queuing too far away or in the past (most likely) */
Willy Tarreau4726f532009-03-07 17:25:21 +0100295 return;
Willy Tarreau28c41a42008-06-29 17:00:59 +0200296#endif
Willy Tarreauce44f122008-07-05 18:16:19 +0200297
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200298 eb32_insert(wq, &task->wq);
Willy Tarreau964c9362007-01-07 00:38:00 +0100299}
300
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200301/*
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200302 * Extract all expired timers from the timer queue, and wakes up all
Willy Tarreauc49ba522019-12-11 08:12:23 +0100303 * associated tasks.
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200304 */
Willy Tarreauc49ba522019-12-11 08:12:23 +0100305void wake_expired_tasks()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200306{
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200307 struct thread_ctx * const tt = th_ctx; // thread's tasks
Willy Tarreau3cfaa8d2020-10-16 09:26:22 +0200308 int max_processed = global.tune.runqueue_depth;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200309 struct task *task;
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200310 struct eb32_node *eb;
Willy Tarreauaf613e82020-06-05 08:40:51 +0200311 __decl_thread(int key);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200312
Willy Tarreauf5aef022022-06-14 15:04:34 +0200313 while (1) {
314 if (max_processed-- <= 0)
315 goto leave;
316
Willy Tarreau4c1e1ad2019-09-24 07:19:08 +0200317 eb = eb32_lookup_ge(&tt->timers, now_ms - TIMER_LOOK_BACK);
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200318 if (!eb) {
319 /* we might have reached the end of the tree, typically because
320 * <now_ms> is in the first half and we're first scanning the last
321 * half. Let's loop back to the beginning of the tree now.
322 */
Willy Tarreau4c1e1ad2019-09-24 07:19:08 +0200323 eb = eb32_first(&tt->timers);
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200324 if (likely(!eb))
325 break;
326 }
327
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200328 /* It is possible that this task was left at an earlier place in the
329 * tree because a recent call to task_queue() has not moved it. This
330 * happens when the new expiration date is later than the old one.
331 * Since it is very unlikely that we reach a timeout anyway, it's a
332 * lot cheaper to proceed like this because we almost never update
333 * the tree. We may also find disabled expiration dates there. Since
334 * we have detached the task from the tree, we simply call task_queue
335 * to take care of this. Note that we might occasionally requeue it at
336 * the same place, before <eb>, so we have to check if this happens,
337 * and adjust <eb>, otherwise we may skip it which is not what we want.
338 * We may also not requeue the task (and not point eb at it) if its
Willy Tarreau77015ab2020-06-19 11:50:27 +0200339 * expiration time is not set. We also make sure we leave the real
340 * expiration date for the next task in the queue so that when calling
341 * next_timer_expiry() we're guaranteed to see the next real date and
342 * not the next apparent date. This is in order to avoid useless
343 * wakeups.
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200344 */
Willy Tarreau77015ab2020-06-19 11:50:27 +0200345
346 task = eb32_entry(eb, struct task, wq);
347 if (tick_is_expired(task->expire, now_ms)) {
348 /* expired task, wake it up */
349 __task_unlink_wq(task);
350 task_wakeup(task, TASK_WOKEN_TIMER);
351 }
352 else if (task->expire != eb->key) {
353 /* task is not expired but its key doesn't match so let's
354 * update it and skip to next apparently expired task.
355 */
356 __task_unlink_wq(task);
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200357 if (tick_isset(task->expire))
Willy Tarreau4c1e1ad2019-09-24 07:19:08 +0200358 __task_queue(task, &tt->timers);
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200359 }
Willy Tarreau77015ab2020-06-19 11:50:27 +0200360 else {
Willy Tarreau7a969992021-09-30 16:38:09 +0200361 /* task not expired and correctly placed. It may not be eternal. */
362 BUG_ON(task->expire == TICK_ETERNITY);
Willy Tarreau77015ab2020-06-19 11:50:27 +0200363 break;
364 }
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200365 }
366
367#ifdef USE_THREAD
Willy Tarreaub0e77122022-07-07 15:22:55 +0200368 if (eb_is_empty(&tg_ctx->timers))
Willy Tarreau1e928c02019-05-28 18:57:25 +0200369 goto leave;
370
Willy Tarreaufc50b9d2022-11-22 07:05:44 +0100371 HA_RWLOCK_RDLOCK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreaub0e77122022-07-07 15:22:55 +0200372 eb = eb32_lookup_ge(&tg_ctx->timers, now_ms - TIMER_LOOK_BACK);
Willy Tarreau1e928c02019-05-28 18:57:25 +0200373 if (!eb) {
Willy Tarreaub0e77122022-07-07 15:22:55 +0200374 eb = eb32_first(&tg_ctx->timers);
Willy Tarreau1e928c02019-05-28 18:57:25 +0200375 if (likely(!eb)) {
Willy Tarreaufc50b9d2022-11-22 07:05:44 +0100376 HA_RWLOCK_RDUNLOCK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreau1e928c02019-05-28 18:57:25 +0200377 goto leave;
378 }
379 }
380 key = eb->key;
Willy Tarreau1e928c02019-05-28 18:57:25 +0200381
Willy Tarreaud48ed662020-10-16 09:31:41 +0200382 if (tick_is_lt(now_ms, key)) {
Willy Tarreaufc50b9d2022-11-22 07:05:44 +0100383 HA_RWLOCK_RDUNLOCK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreau1e928c02019-05-28 18:57:25 +0200384 goto leave;
Willy Tarreaud48ed662020-10-16 09:31:41 +0200385 }
Willy Tarreau1e928c02019-05-28 18:57:25 +0200386
387 /* There's really something of interest here, let's visit the queue */
388
Willy Tarreaufc50b9d2022-11-22 07:05:44 +0100389 if (HA_RWLOCK_TRYRDTOSK(TASK_WQ_LOCK, &wq_lock)) {
Willy Tarreaud48ed662020-10-16 09:31:41 +0200390 /* if we failed to grab the lock it means another thread is
391 * already doing the same here, so let it do the job.
392 */
Willy Tarreaufc50b9d2022-11-22 07:05:44 +0100393 HA_RWLOCK_RDUNLOCK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreaud48ed662020-10-16 09:31:41 +0200394 goto leave;
395 }
396
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200397 while (1) {
Emeric Brunc60def82017-09-27 14:59:38 +0200398 lookup_next:
Willy Tarreau3cfaa8d2020-10-16 09:26:22 +0200399 if (max_processed-- <= 0)
400 break;
Willy Tarreaub0e77122022-07-07 15:22:55 +0200401 eb = eb32_lookup_ge(&tg_ctx->timers, now_ms - TIMER_LOOK_BACK);
Emeric Brunc60def82017-09-27 14:59:38 +0200402 if (!eb) {
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100403 /* we might have reached the end of the tree, typically because
404 * <now_ms> is in the first half and we're first scanning the last
405 * half. Let's loop back to the beginning of the tree now.
406 */
Willy Tarreaub0e77122022-07-07 15:22:55 +0200407 eb = eb32_first(&tg_ctx->timers);
Willy Tarreaub992ba12017-11-05 19:09:27 +0100408 if (likely(!eb))
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100409 break;
410 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200411
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100412 task = eb32_entry(eb, struct task, wq);
Willy Tarreau6c8babf2022-02-14 10:18:51 +0100413
414 /* Check for any competing run of the task (quite rare but may
415 * involve a dangerous concurrent access on task->expire). In
416 * order to protect against this, we'll take an exclusive access
417 * on TASK_RUNNING before checking/touching task->expire. If the
418 * task is already RUNNING on another thread, it will deal by
419 * itself with the requeuing so we must not do anything and
420 * simply quit the loop for now, because we cannot wait with the
421 * WQ lock held as this would prevent the running thread from
422 * requeuing the task. One annoying effect of holding RUNNING
423 * here is that a concurrent task_wakeup() will refrain from
424 * waking it up. This forces us to check for a wakeup after
425 * releasing the flag.
426 */
427 if (HA_ATOMIC_FETCH_OR(&task->state, TASK_RUNNING) & TASK_RUNNING)
428 break;
429
Willy Tarreau77015ab2020-06-19 11:50:27 +0200430 if (tick_is_expired(task->expire, now_ms)) {
431 /* expired task, wake it up */
Willy Tarreaufc50b9d2022-11-22 07:05:44 +0100432 HA_RWLOCK_SKTOWR(TASK_WQ_LOCK, &wq_lock);
Willy Tarreau77015ab2020-06-19 11:50:27 +0200433 __task_unlink_wq(task);
Willy Tarreaufc50b9d2022-11-22 07:05:44 +0100434 HA_RWLOCK_WRTOSK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreau6c8babf2022-02-14 10:18:51 +0100435 task_drop_running(task, TASK_WOKEN_TIMER);
Willy Tarreau77015ab2020-06-19 11:50:27 +0200436 }
437 else if (task->expire != eb->key) {
438 /* task is not expired but its key doesn't match so let's
439 * update it and skip to next apparently expired task.
440 */
Willy Tarreaufc50b9d2022-11-22 07:05:44 +0100441 HA_RWLOCK_SKTOWR(TASK_WQ_LOCK, &wq_lock);
Willy Tarreau77015ab2020-06-19 11:50:27 +0200442 __task_unlink_wq(task);
Willy Tarreaub992ba12017-11-05 19:09:27 +0100443 if (tick_isset(task->expire))
Willy Tarreaub0e77122022-07-07 15:22:55 +0200444 __task_queue(task, &tg_ctx->timers);
Willy Tarreaufc50b9d2022-11-22 07:05:44 +0100445 HA_RWLOCK_WRTOSK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreau6c8babf2022-02-14 10:18:51 +0100446 task_drop_running(task, 0);
Emeric Brunc60def82017-09-27 14:59:38 +0200447 goto lookup_next;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200448 }
Willy Tarreau77015ab2020-06-19 11:50:27 +0200449 else {
Willy Tarreau7a969992021-09-30 16:38:09 +0200450 /* task not expired and correctly placed. It may not be eternal. */
451 BUG_ON(task->expire == TICK_ETERNITY);
Willy Tarreau6c8babf2022-02-14 10:18:51 +0100452 task_drop_running(task, 0);
Willy Tarreau77015ab2020-06-19 11:50:27 +0200453 break;
454 }
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100455 }
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200456
Willy Tarreaufc50b9d2022-11-22 07:05:44 +0100457 HA_RWLOCK_SKUNLOCK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200458#endif
Willy Tarreau1e928c02019-05-28 18:57:25 +0200459leave:
Willy Tarreauc49ba522019-12-11 08:12:23 +0100460 return;
461}
462
463/* Checks the next timer for the current thread by looking into its own timer
464 * list and the global one. It may return TICK_ETERNITY if no timer is present.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500465 * Note that the next timer might very well be slightly in the past.
Willy Tarreauc49ba522019-12-11 08:12:23 +0100466 */
467int next_timer_expiry()
468{
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200469 struct thread_ctx * const tt = th_ctx; // thread's tasks
Willy Tarreauc49ba522019-12-11 08:12:23 +0100470 struct eb32_node *eb;
471 int ret = TICK_ETERNITY;
Willy Tarreau6ce02322020-08-21 05:48:34 +0200472 __decl_thread(int key = TICK_ETERNITY);
Willy Tarreauc49ba522019-12-11 08:12:23 +0100473
474 /* first check in the thread-local timers */
475 eb = eb32_lookup_ge(&tt->timers, now_ms - TIMER_LOOK_BACK);
476 if (!eb) {
477 /* we might have reached the end of the tree, typically because
478 * <now_ms> is in the first half and we're first scanning the last
479 * half. Let's loop back to the beginning of the tree now.
480 */
481 eb = eb32_first(&tt->timers);
482 }
483
484 if (eb)
485 ret = eb->key;
486
487#ifdef USE_THREAD
Willy Tarreaub0e77122022-07-07 15:22:55 +0200488 if (!eb_is_empty(&tg_ctx->timers)) {
Willy Tarreaufc50b9d2022-11-22 07:05:44 +0100489 HA_RWLOCK_RDLOCK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreaub0e77122022-07-07 15:22:55 +0200490 eb = eb32_lookup_ge(&tg_ctx->timers, now_ms - TIMER_LOOK_BACK);
Willy Tarreauc49ba522019-12-11 08:12:23 +0100491 if (!eb)
Willy Tarreaub0e77122022-07-07 15:22:55 +0200492 eb = eb32_first(&tg_ctx->timers);
Willy Tarreauc49ba522019-12-11 08:12:23 +0100493 if (eb)
494 key = eb->key;
Willy Tarreaufc50b9d2022-11-22 07:05:44 +0100495 HA_RWLOCK_RDUNLOCK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreauc49ba522019-12-11 08:12:23 +0100496 if (eb)
497 ret = tick_first(ret, key);
498 }
499#endif
Willy Tarreaub992ba12017-11-05 19:09:27 +0100500 return ret;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200501}
502
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200503/* Walks over tasklet lists th_ctx->tasklets[0..TL_CLASSES-1] and run at most
Willy Tarreau59153fe2020-06-24 10:17:29 +0200504 * budget[TL_*] of them. Returns the number of entries effectively processed
505 * (tasks and tasklets merged). The count of tasks in the list for the current
506 * thread is adjusted.
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100507 */
Willy Tarreau59153fe2020-06-24 10:17:29 +0200508unsigned int run_tasks_from_lists(unsigned int budgets[])
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100509{
Willy Tarreau144f84a2021-03-02 16:09:26 +0100510 struct task *(*process)(struct task *t, void *ctx, unsigned int state);
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200511 struct list *tl_queues = th_ctx->tasklets;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100512 struct task *t;
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200513 uint8_t budget_mask = (1 << TL_CLASSES) - 1;
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100514 struct sched_activity *profile_entry = NULL;
Willy Tarreau59153fe2020-06-24 10:17:29 +0200515 unsigned int done = 0;
516 unsigned int queue;
Willy Tarreau144f84a2021-03-02 16:09:26 +0100517 unsigned int state;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100518 void *ctx;
Willy Tarreau59153fe2020-06-24 10:17:29 +0200519
520 for (queue = 0; queue < TL_CLASSES;) {
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200521 th_ctx->current_queue = queue;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100522
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200523 /* global.tune.sched.low-latency is set */
524 if (global.tune.options & GTUNE_SCHED_LOW_LATENCY) {
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200525 if (unlikely(th_ctx->tl_class_mask & budget_mask & ((1 << queue) - 1))) {
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200526 /* a lower queue index has tasks again and still has a
527 * budget to run them. Let's switch to it now.
528 */
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200529 queue = (th_ctx->tl_class_mask & 1) ? 0 :
530 (th_ctx->tl_class_mask & 2) ? 1 : 2;
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200531 continue;
532 }
533
534 if (unlikely(queue > TL_URGENT &&
535 budget_mask & (1 << TL_URGENT) &&
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200536 !MT_LIST_ISEMPTY(&th_ctx->shared_tasklet_list))) {
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200537 /* an urgent tasklet arrived from another thread */
538 break;
539 }
540
541 if (unlikely(queue > TL_NORMAL &&
542 budget_mask & (1 << TL_NORMAL) &&
Willy Tarreauc958c702022-06-16 15:59:36 +0200543 (!eb_is_empty(&th_ctx->rqueue) || !eb_is_empty(&th_ctx->rqueue_shared)))) {
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200544 /* a task was woken up by a bulk tasklet or another thread */
545 break;
546 }
547 }
548
Willy Tarreauba48d5c2020-06-24 09:54:24 +0200549 if (LIST_ISEMPTY(&tl_queues[queue])) {
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200550 th_ctx->tl_class_mask &= ~(1 << queue);
Willy Tarreau59153fe2020-06-24 10:17:29 +0200551 queue++;
552 continue;
Willy Tarreauba48d5c2020-06-24 09:54:24 +0200553 }
554
Willy Tarreau59153fe2020-06-24 10:17:29 +0200555 if (!budgets[queue]) {
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200556 budget_mask &= ~(1 << queue);
Willy Tarreau59153fe2020-06-24 10:17:29 +0200557 queue++;
558 continue;
559 }
Willy Tarreauba48d5c2020-06-24 09:54:24 +0200560
Willy Tarreau59153fe2020-06-24 10:17:29 +0200561 budgets[queue]--;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100562 activity[tid].ctxsw++;
Willy Tarreau3193eb92021-10-21 16:17:29 +0200563
564 t = (struct task *)LIST_ELEM(tl_queues[queue].n, struct tasklet *, list);
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100565 ctx = t->context;
566 process = t->process;
567 t->calls++;
Willy Tarreaua9a23842022-09-07 17:06:16 +0200568
569 th_ctx->sched_wake_date = t->wake_date;
570 if (th_ctx->sched_wake_date) {
571 uint32_t now_ns = now_mono_time();
572 uint32_t lat = now_ns - th_ctx->sched_wake_date;
573
574 t->wake_date = 0;
575 th_ctx->sched_call_date = now_ns;
Willy Tarreau3d4cdb12022-09-07 18:37:47 +0200576 profile_entry = sched_activity_entry(sched_activity, t->process, t->caller);
Willy Tarreaua9a23842022-09-07 17:06:16 +0200577 th_ctx->sched_profile_entry = profile_entry;
578 HA_ATOMIC_ADD(&profile_entry->lat_time, lat);
579 HA_ATOMIC_INC(&profile_entry->calls);
580 }
581 __ha_barrier_store();
582
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200583 th_ctx->current = t;
Willy Tarreaubdcd3252022-06-22 09:19:46 +0200584 _HA_ATOMIC_AND(&th_ctx->flags, ~TH_FL_STUCK); // this thread is still running
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100585
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200586 _HA_ATOMIC_DEC(&th_ctx->rq_total);
Willy Tarreaua9a23842022-09-07 17:06:16 +0200587 LIST_DEL_INIT(&((struct tasklet *)t)->list);
588 __ha_barrier_store();
Willy Tarreau2da4c312020-11-30 14:52:11 +0100589
Willy Tarreau3193eb92021-10-21 16:17:29 +0200590 if (t->state & TASK_F_TASKLET) {
Willy Tarreaua9a23842022-09-07 17:06:16 +0200591 /* this is a tasklet */
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 Tarreaua9a23842022-09-07 17:06:16 +0200605 } else {
606 /* This is a regular task */
Willy Tarreau2a54ffb2021-02-25 09:32:58 +0100607
Willy Tarreaua9a23842022-09-07 17:06:16 +0200608 /* We must be the exclusive owner of the TASK_RUNNING bit, and
609 * have to be careful that the task is not being manipulated on
610 * another thread finding it expired in wake_expired_tasks().
611 * The TASK_RUNNING bit will be set during these operations,
612 * they are extremely rare and do not last long so the best to
613 * do here is to wait.
614 */
615 state = _HA_ATOMIC_LOAD(&t->state);
616 do {
617 while (unlikely(state & TASK_RUNNING)) {
618 __ha_cpu_relax();
619 state = _HA_ATOMIC_LOAD(&t->state);
620 }
621 } while (!_HA_ATOMIC_CAS(&t->state, &state, (state & TASK_PERSISTENT) | TASK_RUNNING));
Willy Tarreau62b5b962022-09-07 15:11:25 +0200622
Willy Tarreaua9a23842022-09-07 17:06:16 +0200623 __ha_barrier_atomic_store();
Willy Tarreau62b5b962022-09-07 15:11:25 +0200624
Willy Tarreaua9a23842022-09-07 17:06:16 +0200625 _HA_ATOMIC_DEC(&ha_thread_ctx[tid].tasks_in_list);
Willy Tarreau62b5b962022-09-07 15:11:25 +0200626
Willy Tarreaua9a23842022-09-07 17:06:16 +0200627 /* Note for below: if TASK_KILLED arrived before we've read the state, we
628 * directly free the task. Otherwise it will be seen after processing and
629 * it's freed on the exit path.
630 */
631 if (likely(!(state & TASK_KILLED) && process == process_stream))
632 t = process_stream(t, ctx, state);
633 else if (!(state & TASK_KILLED) && process != NULL)
634 t = process(t, ctx, state);
635 else {
636 task_unlink_wq(t);
637 __task_free(t);
638 th_ctx->current = NULL;
639 __ha_barrier_store();
640 /* We don't want max_processed to be decremented if
641 * we're just freeing a destroyed task, we should only
642 * do so if we really ran a task.
643 */
644 continue;
Willy Tarreau6c8babf2022-02-14 10:18:51 +0100645 }
Willy Tarreau3193eb92021-10-21 16:17:29 +0200646
Willy Tarreaua9a23842022-09-07 17:06:16 +0200647 /* If there is a pending state we have to wake up the task
648 * immediately, else we defer it into wait queue
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100649 */
Willy Tarreaua9a23842022-09-07 17:06:16 +0200650 if (t != NULL) {
651 state = _HA_ATOMIC_LOAD(&t->state);
652 if (unlikely(state & TASK_KILLED)) {
653 task_unlink_wq(t);
654 __task_free(t);
655 }
656 else {
657 task_queue(t);
658 task_drop_running(t, 0);
659 }
660 }
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100661 }
Willy Tarreaua9a23842022-09-07 17:06:16 +0200662
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200663 th_ctx->current = NULL;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100664 __ha_barrier_store();
Willy Tarreau62b5b962022-09-07 15:11:25 +0200665
666 /* stats are only registered for non-zero wake dates */
Willy Tarreaua9a23842022-09-07 17:06:16 +0200667 if (unlikely(th_ctx->sched_wake_date))
668 HA_ATOMIC_ADD(&profile_entry->cpu_time, (uint32_t)(now_mono_time() - th_ctx->sched_call_date));
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100669 done++;
670 }
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200671 th_ctx->current_queue = -1;
Willy Tarreau116ef222020-06-23 16:35:38 +0200672
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100673 return done;
674}
675
Willy Tarreau58b458d2008-06-29 22:40:23 +0200676/* The run queue is chronologically sorted in a tree. An insertion counter is
677 * used to assign a position to each task. This counter may be combined with
678 * other variables (eg: nice value) to set the final position in the tree. The
679 * counter may wrap without a problem, of course. We then limit the number of
Christopher Faulet8a48f672017-11-14 10:38:36 +0100680 * tasks processed to 200 in any case, so that general latency remains low and
Willy Tarreaucde79022019-04-12 18:03:41 +0200681 * so that task positions have a chance to be considered. The function scans
682 * both the global and local run queues and picks the most urgent task between
683 * the two. We need to grab the global runqueue lock to touch it so it's taken
684 * on the very first access to the global run queue and is released as soon as
685 * it reaches the end.
Willy Tarreau58b458d2008-06-29 22:40:23 +0200686 *
687 * The function adjusts <next> if a new event is closer.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200688 */
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +0100689void process_runnable_tasks()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200690{
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200691 struct thread_ctx * const tt = th_ctx;
Willy Tarreau319d1362022-06-16 16:28:01 +0200692 struct eb32_node *lrq; // next local run queue entry
693 struct eb32_node *grq; // next global run queue entry
Willy Tarreau964c9362007-01-07 00:38:00 +0100694 struct task *t;
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200695 const unsigned int default_weights[TL_CLASSES] = {
696 [TL_URGENT] = 64, // ~50% of CPU bandwidth for I/O
697 [TL_NORMAL] = 48, // ~37% of CPU bandwidth for tasks
698 [TL_BULK] = 16, // ~13% of CPU bandwidth for self-wakers
Willy Tarreau401135c2021-02-26 09:16:22 +0100699 [TL_HEAVY] = 1, // never more than 1 heavy task at once
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200700 };
701 unsigned int max[TL_CLASSES]; // max to be run per class
702 unsigned int max_total; // sum of max above
Olivier Houchard06910462019-10-11 16:35:01 +0200703 struct mt_list *tmp_list;
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200704 unsigned int queue;
705 int max_processed;
Willy Tarreaue7923c12021-02-25 07:09:08 +0100706 int lpicked, gpicked;
Willy Tarreau76390da2021-02-26 10:18:11 +0100707 int heavy_queued = 0;
Willy Tarreauc309dbd2020-11-30 15:39:00 +0100708 int budget;
Christopher Faulet3911ee82017-11-14 10:26:53 +0100709
Willy Tarreaubdcd3252022-06-22 09:19:46 +0200710 _HA_ATOMIC_AND(&th_ctx->flags, ~TH_FL_STUCK); // this thread is still running
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200711
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200712 if (!thread_has_tasks()) {
713 activity[tid].empty_rq++;
714 return;
715 }
716
Willy Tarreau5c8be272020-06-19 12:17:55 +0200717 max_processed = global.tune.runqueue_depth;
718
Willy Tarreau91a7c162022-07-07 15:25:40 +0200719 if (likely(tg_ctx->niced_tasks))
Willy Tarreau5c8be272020-06-19 12:17:55 +0200720 max_processed = (max_processed + 3) / 4;
721
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200722 if (max_processed < th_ctx->rq_total && th_ctx->rq_total <= 2*max_processed) {
Willy Tarreau1691ba32021-03-10 09:26:24 +0100723 /* If the run queue exceeds the budget by up to 50%, let's cut it
724 * into two identical halves to improve latency.
725 */
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200726 max_processed = th_ctx->rq_total / 2;
Willy Tarreau1691ba32021-03-10 09:26:24 +0100727 }
728
Willy Tarreau5c8be272020-06-19 12:17:55 +0200729 not_done_yet:
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200730 max[TL_URGENT] = max[TL_NORMAL] = max[TL_BULK] = 0;
Willy Tarreaucde79022019-04-12 18:03:41 +0200731
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200732 /* urgent tasklets list gets a default weight of ~50% */
Willy Tarreau49f90bf2020-06-24 09:39:48 +0200733 if ((tt->tl_class_mask & (1 << TL_URGENT)) ||
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200734 !MT_LIST_ISEMPTY(&tt->shared_tasklet_list))
735 max[TL_URGENT] = default_weights[TL_URGENT];
Willy Tarreaua62917b2020-01-30 18:37:28 +0100736
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200737 /* normal tasklets list gets a default weight of ~37% */
Willy Tarreau49f90bf2020-06-24 09:39:48 +0200738 if ((tt->tl_class_mask & (1 << TL_NORMAL)) ||
Willy Tarreauc958c702022-06-16 15:59:36 +0200739 !eb_is_empty(&th_ctx->rqueue) || !eb_is_empty(&th_ctx->rqueue_shared))
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200740 max[TL_NORMAL] = default_weights[TL_NORMAL];
Willy Tarreaua62917b2020-01-30 18:37:28 +0100741
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200742 /* bulk tasklets list gets a default weight of ~13% */
Willy Tarreau49f90bf2020-06-24 09:39:48 +0200743 if ((tt->tl_class_mask & (1 << TL_BULK)))
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200744 max[TL_BULK] = default_weights[TL_BULK];
745
Willy Tarreau401135c2021-02-26 09:16:22 +0100746 /* heavy tasks are processed only once and never refilled in a
Willy Tarreau76390da2021-02-26 10:18:11 +0100747 * call round. That budget is not lost either as we don't reset
748 * it unless consumed.
Willy Tarreau401135c2021-02-26 09:16:22 +0100749 */
Willy Tarreau76390da2021-02-26 10:18:11 +0100750 if (!heavy_queued) {
751 if ((tt->tl_class_mask & (1 << TL_HEAVY)))
752 max[TL_HEAVY] = default_weights[TL_HEAVY];
753 else
754 max[TL_HEAVY] = 0;
755 heavy_queued = 1;
756 }
Willy Tarreau401135c2021-02-26 09:16:22 +0100757
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200758 /* Now compute a fair share of the weights. Total may slightly exceed
Willy Tarreau1553b662020-06-30 13:46:21 +0200759 * 100% due to rounding, this is not a problem. Note that while in
760 * theory the sum cannot be NULL as we cannot get there without tasklets
761 * to process, in practice it seldom happens when multiple writers
Willy Tarreau2b718102021-04-21 07:32:39 +0200762 * conflict and rollback on MT_LIST_TRY_APPEND(shared_tasklet_list), causing
Willy Tarreau1553b662020-06-30 13:46:21 +0200763 * a first MT_LIST_ISEMPTY() to succeed for thread_has_task() and the
764 * one above to finally fail. This is extremely rare and not a problem.
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200765 */
Willy Tarreau401135c2021-02-26 09:16:22 +0100766 max_total = max[TL_URGENT] + max[TL_NORMAL] + max[TL_BULK] + max[TL_HEAVY];
Willy Tarreau1553b662020-06-30 13:46:21 +0200767 if (!max_total)
768 return;
769
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200770 for (queue = 0; queue < TL_CLASSES; queue++)
771 max[queue] = ((unsigned)max_processed * max[queue] + max_total - 1) / max_total;
772
Willy Tarreau76390da2021-02-26 10:18:11 +0100773 /* The heavy queue must never process more than one task at once
774 * anyway.
775 */
776 if (max[TL_HEAVY] > 1)
777 max[TL_HEAVY] = 1;
778
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200779 lrq = grq = NULL;
Christopher Faulet8a48f672017-11-14 10:38:36 +0100780
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200781 /* pick up to max[TL_NORMAL] regular tasks from prio-ordered run queues */
782 /* Note: the grq lock is always held when grq is not null */
Willy Tarreaue7923c12021-02-25 07:09:08 +0100783 lpicked = gpicked = 0;
Willy Tarreau1f3b1412021-02-24 14:13:40 +0100784 budget = max[TL_NORMAL] - tt->tasks_in_list;
Willy Tarreaue7923c12021-02-25 07:09:08 +0100785 while (lpicked + gpicked < budget) {
Willy Tarreauc958c702022-06-16 15:59:36 +0200786 if (!eb_is_empty(&th_ctx->rqueue_shared) && !grq) {
Willy Tarreau3466e3c2019-04-15 18:52:40 +0200787#ifdef USE_THREAD
Willy Tarreaub17dd6c2022-06-16 16:58:17 +0200788 HA_SPIN_LOCK(TASK_RQ_LOCK, &th_ctx->rqsh_lock);
Willy Tarreau319d1362022-06-16 16:28:01 +0200789 grq = eb32_lookup_ge(&th_ctx->rqueue_shared, _HA_ATOMIC_LOAD(&tt->rqueue_ticks) - TIMER_LOOK_BACK);
Willy Tarreaucde79022019-04-12 18:03:41 +0200790 if (unlikely(!grq)) {
Willy Tarreau319d1362022-06-16 16:28:01 +0200791 grq = eb32_first(&th_ctx->rqueue_shared);
Willy Tarreauc958c702022-06-16 15:59:36 +0200792 if (!grq)
Willy Tarreaub17dd6c2022-06-16 16:58:17 +0200793 HA_SPIN_UNLOCK(TASK_RQ_LOCK, &th_ctx->rqsh_lock);
Willy Tarreauf0c531a2017-11-05 16:35:59 +0100794 }
Willy Tarreau3466e3c2019-04-15 18:52:40 +0200795#endif
Willy Tarreaucde79022019-04-12 18:03:41 +0200796 }
Willy Tarreauf0c531a2017-11-05 16:35:59 +0100797
Willy Tarreaucde79022019-04-12 18:03:41 +0200798 /* If a global task is available for this thread, it's in grq
799 * now and the global RQ is locked.
800 */
Olivier Houchardf6e6dc12018-05-18 18:38:23 +0200801
Willy Tarreaucde79022019-04-12 18:03:41 +0200802 if (!lrq) {
Willy Tarreau319d1362022-06-16 16:28:01 +0200803 lrq = eb32_lookup_ge(&tt->rqueue, _HA_ATOMIC_LOAD(&tt->rqueue_ticks) - TIMER_LOOK_BACK);
Willy Tarreaucde79022019-04-12 18:03:41 +0200804 if (unlikely(!lrq))
Willy Tarreau319d1362022-06-16 16:28:01 +0200805 lrq = eb32_first(&tt->rqueue);
Willy Tarreauf0c531a2017-11-05 16:35:59 +0100806 }
Willy Tarreauf0c531a2017-11-05 16:35:59 +0100807
Willy Tarreaucde79022019-04-12 18:03:41 +0200808 if (!lrq && !grq)
809 break;
810
811 if (likely(!grq || (lrq && (int)(lrq->key - grq->key) <= 0))) {
Willy Tarreau319d1362022-06-16 16:28:01 +0200812 t = eb32_entry(lrq, struct task, rq);
813 lrq = eb32_next(lrq);
814 eb32_delete(&t->rq);
Willy Tarreaue7923c12021-02-25 07:09:08 +0100815 lpicked++;
Olivier Houchardf6e6dc12018-05-18 18:38:23 +0200816 }
Willy Tarreau3466e3c2019-04-15 18:52:40 +0200817#ifdef USE_THREAD
Willy Tarreaucde79022019-04-12 18:03:41 +0200818 else {
Willy Tarreau319d1362022-06-16 16:28:01 +0200819 t = eb32_entry(grq, struct task, rq);
820 grq = eb32_next(grq);
821 eb32_delete(&t->rq);
Willy Tarreau2b363ac2021-02-25 07:14:58 +0100822
Willy Tarreaucde79022019-04-12 18:03:41 +0200823 if (unlikely(!grq)) {
Willy Tarreau319d1362022-06-16 16:28:01 +0200824 grq = eb32_first(&th_ctx->rqueue_shared);
Willy Tarreauc958c702022-06-16 15:59:36 +0200825 if (!grq)
Willy Tarreaub17dd6c2022-06-16 16:58:17 +0200826 HA_SPIN_UNLOCK(TASK_RQ_LOCK, &th_ctx->rqsh_lock);
Willy Tarreaucde79022019-04-12 18:03:41 +0200827 }
Willy Tarreaue7923c12021-02-25 07:09:08 +0100828 gpicked++;
Emeric Brun01948972017-03-30 15:37:25 +0200829 }
Willy Tarreau3466e3c2019-04-15 18:52:40 +0200830#endif
Willy Tarreau2b363ac2021-02-25 07:14:58 +0100831 if (t->nice)
Willy Tarreau91a7c162022-07-07 15:25:40 +0200832 _HA_ATOMIC_DEC(&tg_ctx->niced_tasks);
Willy Tarreaucde79022019-04-12 18:03:41 +0200833
Willy Tarreaua868c292020-11-30 15:30:22 +0100834 /* Add it to the local task list */
Willy Tarreau2b718102021-04-21 07:32:39 +0200835 LIST_APPEND(&tt->tasklets[TL_NORMAL], &((struct tasklet *)t)->list);
Olivier Houchardb0bdae72018-05-18 18:45:28 +0200836 }
Willy Tarreaucde79022019-04-12 18:03:41 +0200837
838 /* release the rqueue lock */
839 if (grq) {
Willy Tarreaub17dd6c2022-06-16 16:58:17 +0200840 HA_SPIN_UNLOCK(TASK_RQ_LOCK, &th_ctx->rqsh_lock);
Willy Tarreaucde79022019-04-12 18:03:41 +0200841 grq = NULL;
842 }
843
Willy Tarreaue7923c12021-02-25 07:09:08 +0100844 if (lpicked + gpicked) {
Willy Tarreauc309dbd2020-11-30 15:39:00 +0100845 tt->tl_class_mask |= 1 << TL_NORMAL;
Willy Tarreaue7923c12021-02-25 07:09:08 +0100846 _HA_ATOMIC_ADD(&tt->tasks_in_list, lpicked + gpicked);
Willy Tarreaue7923c12021-02-25 07:09:08 +0100847 activity[tid].tasksw += lpicked + gpicked;
Willy Tarreauc309dbd2020-11-30 15:39:00 +0100848 }
849
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200850 /* Merge the list of tasklets waken up by other threads to the
851 * main list.
852 */
853 tmp_list = MT_LIST_BEHEAD(&tt->shared_tasklet_list);
Willy Tarreau49f90bf2020-06-24 09:39:48 +0200854 if (tmp_list) {
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200855 LIST_SPLICE_END_DETACHED(&tt->tasklets[TL_URGENT], (struct list *)tmp_list);
Willy Tarreau49f90bf2020-06-24 09:39:48 +0200856 if (!LIST_ISEMPTY(&tt->tasklets[TL_URGENT]))
857 tt->tl_class_mask |= 1 << TL_URGENT;
858 }
Willy Tarreaucde79022019-04-12 18:03:41 +0200859
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200860 /* execute tasklets in each queue */
Willy Tarreau59153fe2020-06-24 10:17:29 +0200861 max_processed -= run_tasks_from_lists(max);
Willy Tarreaua62917b2020-01-30 18:37:28 +0100862
Willy Tarreau5c8be272020-06-19 12:17:55 +0200863 /* some tasks may have woken other ones up */
Willy Tarreau0c0c85e2020-06-23 11:32:35 +0200864 if (max_processed > 0 && thread_has_tasks())
Willy Tarreau5c8be272020-06-19 12:17:55 +0200865 goto not_done_yet;
866
Willy Tarreau49f90bf2020-06-24 09:39:48 +0200867 if (tt->tl_class_mask)
Willy Tarreaucde79022019-04-12 18:03:41 +0200868 activity[tid].long_rq++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200869}
870
William Lallemand27f3fa52018-12-06 14:05:20 +0100871/*
872 * Delete every tasks before running the master polling loop
873 */
874void mworker_cleantasks()
875{
876 struct task *t;
877 int i;
William Lallemandb5823392018-12-06 15:14:37 +0100878 struct eb32_node *tmp_wq = NULL;
Willy Tarreau319d1362022-06-16 16:28:01 +0200879 struct eb32_node *tmp_rq = NULL;
William Lallemand27f3fa52018-12-06 14:05:20 +0100880
881#ifdef USE_THREAD
882 /* cleanup the global run queue */
Willy Tarreau319d1362022-06-16 16:28:01 +0200883 tmp_rq = eb32_first(&th_ctx->rqueue_shared);
William Lallemandb5823392018-12-06 15:14:37 +0100884 while (tmp_rq) {
Willy Tarreau319d1362022-06-16 16:28:01 +0200885 t = eb32_entry(tmp_rq, struct task, rq);
886 tmp_rq = eb32_next(tmp_rq);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200887 task_destroy(t);
William Lallemand27f3fa52018-12-06 14:05:20 +0100888 }
889 /* cleanup the timers queue */
Willy Tarreaub0e77122022-07-07 15:22:55 +0200890 tmp_wq = eb32_first(&tg_ctx->timers);
William Lallemandb5823392018-12-06 15:14:37 +0100891 while (tmp_wq) {
892 t = eb32_entry(tmp_wq, struct task, wq);
893 tmp_wq = eb32_next(tmp_wq);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200894 task_destroy(t);
William Lallemand27f3fa52018-12-06 14:05:20 +0100895 }
896#endif
897 /* clean the per thread run queue */
898 for (i = 0; i < global.nbthread; i++) {
Willy Tarreau319d1362022-06-16 16:28:01 +0200899 tmp_rq = eb32_first(&ha_thread_ctx[i].rqueue);
William Lallemandb5823392018-12-06 15:14:37 +0100900 while (tmp_rq) {
Willy Tarreau319d1362022-06-16 16:28:01 +0200901 t = eb32_entry(tmp_rq, struct task, rq);
902 tmp_rq = eb32_next(tmp_rq);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200903 task_destroy(t);
William Lallemand27f3fa52018-12-06 14:05:20 +0100904 }
905 /* cleanup the per thread timers queue */
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200906 tmp_wq = eb32_first(&ha_thread_ctx[i].timers);
William Lallemandb5823392018-12-06 15:14:37 +0100907 while (tmp_wq) {
908 t = eb32_entry(tmp_wq, struct task, wq);
909 tmp_wq = eb32_next(tmp_wq);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200910 task_destroy(t);
William Lallemand27f3fa52018-12-06 14:05:20 +0100911 }
912 }
913}
914
Willy Tarreaub6b3df32018-11-26 16:31:20 +0100915/* perform minimal intializations */
916static void init_task()
Willy Tarreau4726f532009-03-07 17:25:21 +0100917{
Willy Tarreau401135c2021-02-26 09:16:22 +0100918 int i, q;
Olivier Houchardf6e6dc12018-05-18 18:38:23 +0200919
Willy Tarreaub0e77122022-07-07 15:22:55 +0200920 for (i = 0; i < MAX_TGROUPS; i++)
921 memset(&ha_tgroup_ctx[i].timers, 0, sizeof(ha_tgroup_ctx[i].timers));
922
Olivier Houchardb0bdae72018-05-18 18:45:28 +0200923 for (i = 0; i < MAX_THREADS; i++) {
Willy Tarreau401135c2021-02-26 09:16:22 +0100924 for (q = 0; q < TL_CLASSES; q++)
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200925 LIST_INIT(&ha_thread_ctx[i].tasklets[q]);
926 MT_LIST_INIT(&ha_thread_ctx[i].shared_tasklet_list);
Olivier Houchardb0bdae72018-05-18 18:45:28 +0200927 }
Willy Tarreau4726f532009-03-07 17:25:21 +0100928}
929
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200930/* config parser for global "tune.sched.low-latency", accepts "on" or "off" */
931static int cfg_parse_tune_sched_low_latency(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100932 const struct proxy *defpx, const char *file, int line,
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200933 char **err)
934{
935 if (too_many_args(1, args, err, NULL))
936 return -1;
937
938 if (strcmp(args[1], "on") == 0)
939 global.tune.options |= GTUNE_SCHED_LOW_LATENCY;
940 else if (strcmp(args[1], "off") == 0)
941 global.tune.options &= ~GTUNE_SCHED_LOW_LATENCY;
942 else {
943 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
944 return -1;
945 }
946 return 0;
947}
948
949/* config keyword parsers */
950static struct cfg_kw_list cfg_kws = {ILH, {
951 { CFG_GLOBAL, "tune.sched.low-latency", cfg_parse_tune_sched_low_latency },
952 { 0, NULL, NULL }
953}};
954
955INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Willy Tarreaub6b3df32018-11-26 16:31:20 +0100956INITCALL0(STG_PREPARE, init_task);
957
Willy Tarreaubaaee002006-06-26 02:48:02 +0200958/*
959 * Local variables:
960 * c-indent-level: 8
961 * c-basic-offset: 8
962 * End:
963 */