blob: 27a4ca745d26bb37788bd5cb59812f1a43a1913a [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/eb32sctree.h>
16#include <import/eb32tree.h>
17
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020018#include <haproxy/api.h>
Willy Tarreau5d9ddc52021-10-06 19:54:09 +020019#include <haproxy/activity.h>
Willy Tarreaue7723bd2020-06-24 11:11:02 +020020#include <haproxy/cfgparse.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 Tarreaud8b325c2021-10-06 19:25:38 +020025#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/tools.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027
Willy Tarreaue08f4bf2021-05-08 20:10:13 +020028extern struct task *process_stream(struct task *t, void *context, unsigned int state);
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
Olivier Houchardeba0c0b2018-07-26 18:53:28 +020038volatile unsigned long global_tasks_mask = 0; /* Mask of threads with tasks in the global runqueue */
Willy Tarreaue35c94a2009-03-21 10:01:42 +010039unsigned int niced_tasks = 0; /* number of niced tasks in the run queue */
Emeric Brun01948972017-03-30 15:37:25 +020040
Willy Tarreau078c2572021-10-06 15:58:46 +020041/* used for idle time calculation */
42THREAD_LOCAL unsigned int samp_time = 0; /* total elapsed time over current sample */
43THREAD_LOCAL unsigned int idle_time = 0; /* total idle time over current sample */
44
Willy Tarreaud022e9c2019-09-24 08:25:15 +020045THREAD_LOCAL struct task_per_thread *sched = &task_per_thread[0]; /* scheduler context for the current thread */
Willy Tarreau6d1222c2017-11-26 10:08:06 +010046
Willy Tarreau86abe442018-11-25 20:12:18 +010047__decl_aligned_spinlock(rq_lock); /* spin lock related to run queue */
Willy Tarreauef28dc12019-05-28 18:48:07 +020048__decl_aligned_rwlock(wq_lock); /* RW lock related to the wait queue */
Willy Tarreau964c9362007-01-07 00:38:00 +010049
Olivier Houchardb1ca58b2018-06-06 14:22:03 +020050#ifdef USE_THREAD
Willy Tarreauc6ba9a02021-02-20 12:49:54 +010051struct eb_root timers; /* sorted timers tree, global, accessed under wq_lock */
52struct eb_root rqueue; /* tree constituting the global run queue, accessed under rq_lock */
Willy Tarreau45499c52021-02-25 07:51:18 +010053unsigned int grq_total; /* total number of entries in the global run queue, atomic */
Willy Tarreauc6ba9a02021-02-20 12:49:54 +010054static unsigned int global_rqueue_ticks; /* insertion count in the grq, use rq_lock */
Olivier Houchardb1ca58b2018-06-06 14:22:03 +020055#endif
Willy Tarreaub20aa9e2018-10-15 14:52:21 +020056
Willy Tarreau8d8747a2018-10-15 16:12:48 +020057
58struct task_per_thread task_per_thread[MAX_THREADS];
Willy Tarreau9789f7b2008-06-24 08:17:16 +020059
Willy Tarreaueb8c2c62020-06-30 11:48:48 +020060
61/* Flags the task <t> for immediate destruction and puts it into its first
62 * thread's shared tasklet list if not yet queued/running. This will bypass
63 * the priority scheduling and make the task show up as fast as possible in
64 * the other thread's queue. Note that this operation isn't idempotent and is
65 * not supposed to be run on the same task from multiple threads at once. It's
66 * the caller's responsibility to make sure it is the only one able to kill the
67 * task.
68 */
69void task_kill(struct task *t)
70{
Willy Tarreau144f84a2021-03-02 16:09:26 +010071 unsigned int state = t->state;
Willy Tarreaueb8c2c62020-06-30 11:48:48 +020072 unsigned int thr;
73
74 BUG_ON(state & TASK_KILLED);
75
76 while (1) {
77 while (state & (TASK_RUNNING | TASK_QUEUED)) {
78 /* task already in the queue and about to be executed,
79 * or even currently running. Just add the flag and be
80 * done with it, the process loop will detect it and kill
81 * it. The CAS will fail if we arrive too late.
82 */
83 if (_HA_ATOMIC_CAS(&t->state, &state, state | TASK_KILLED))
84 return;
85 }
86
87 /* We'll have to wake it up, but we must also secure it so that
88 * it doesn't vanish under us. TASK_QUEUED guarantees nobody will
89 * add past us.
90 */
91 if (_HA_ATOMIC_CAS(&t->state, &state, state | TASK_QUEUED | TASK_KILLED)) {
92 /* Bypass the tree and go directly into the shared tasklet list.
93 * Note: that's a task so it must be accounted for as such. Pick
94 * the task's first thread for the job.
95 */
96 thr = my_ffsl(t->thread_mask) - 1;
Willy Tarreau54d31172020-07-02 14:14:00 +020097
98 /* Beware: tasks that have never run don't have their ->list empty yet! */
Willy Tarreau2b718102021-04-21 07:32:39 +020099 MT_LIST_APPEND(&task_per_thread[thr].shared_tasklet_list,
Willy Tarreau4f589262020-07-02 17:17:42 +0200100 (struct mt_list *)&((struct tasklet *)t)->list);
Willy Tarreau4781b152021-04-06 13:53:36 +0200101 _HA_ATOMIC_INC(&task_per_thread[thr].rq_total);
102 _HA_ATOMIC_INC(&task_per_thread[thr].tasks_in_list);
Willy Tarreau54d31172020-07-02 14:14:00 +0200103 if (sleeping_thread_mask & (1UL << thr)) {
104 _HA_ATOMIC_AND(&sleeping_thread_mask, ~(1UL << thr));
105 wake_thread(thr);
Willy Tarreaueb8c2c62020-06-30 11:48:48 +0200106 }
Willy Tarreau54d31172020-07-02 14:14:00 +0200107 return;
Willy Tarreaueb8c2c62020-06-30 11:48:48 +0200108 }
109 }
110}
111
Amaury Denoyelle7b368332021-07-28 16:12:57 +0200112/* Equivalent of task_kill for tasklets. Mark the tasklet <t> for destruction.
113 * It will be deleted on the next scheduler invocation. This function is
114 * thread-safe : a thread can kill a tasklet of another thread.
115 */
116void tasklet_kill(struct tasklet *t)
117{
118 unsigned int state = t->state;
119 unsigned int thr;
120
121 BUG_ON(state & TASK_KILLED);
122
123 while (1) {
124 while (state & (TASK_IN_LIST)) {
125 /* Tasklet already in the list ready to be executed. Add
126 * the killed flag and wait for the process loop to
127 * detect it.
128 */
129 if (_HA_ATOMIC_CAS(&t->state, &state, state | TASK_KILLED))
130 return;
131 }
132
133 /* Mark the tasklet as killed and wake the thread to process it
134 * as soon as possible.
135 */
136 if (_HA_ATOMIC_CAS(&t->state, &state, state | TASK_IN_LIST | TASK_KILLED)) {
137 thr = t->tid > 0 ? t->tid: tid;
138 MT_LIST_APPEND(&task_per_thread[thr].shared_tasklet_list,
139 (struct mt_list *)&t->list);
140 _HA_ATOMIC_INC(&task_per_thread[thr].rq_total);
141 if (sleeping_thread_mask & (1UL << thr)) {
142 _HA_ATOMIC_AND(&sleeping_thread_mask, ~(1UL << thr));
143 wake_thread(thr);
144 }
145 return;
146 }
147 }
148}
149
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100150/* Do not call this one, please use tasklet_wakeup_on() instead, as this one is
151 * the slow path of tasklet_wakeup_on() which performs some preliminary checks
152 * and sets TASK_IN_LIST before calling this one. A negative <thr> designates
153 * the current thread.
154 */
155void __tasklet_wakeup_on(struct tasklet *tl, int thr)
156{
157 if (likely(thr < 0)) {
158 /* this tasklet runs on the caller thread */
Willy Tarreau826fa872021-02-26 10:13:40 +0100159 if (tl->state & TASK_HEAVY) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200160 LIST_APPEND(&sched->tasklets[TL_HEAVY], &tl->list);
Willy Tarreau826fa872021-02-26 10:13:40 +0100161 sched->tl_class_mask |= 1 << TL_HEAVY;
162 }
163 else if (tl->state & TASK_SELF_WAKING) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200164 LIST_APPEND(&sched->tasklets[TL_BULK], &tl->list);
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100165 sched->tl_class_mask |= 1 << TL_BULK;
166 }
167 else if ((struct task *)tl == sched->current) {
168 _HA_ATOMIC_OR(&tl->state, TASK_SELF_WAKING);
Willy Tarreau2b718102021-04-21 07:32:39 +0200169 LIST_APPEND(&sched->tasklets[TL_BULK], &tl->list);
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100170 sched->tl_class_mask |= 1 << TL_BULK;
171 }
172 else if (sched->current_queue < 0) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200173 LIST_APPEND(&sched->tasklets[TL_URGENT], &tl->list);
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100174 sched->tl_class_mask |= 1 << TL_URGENT;
175 }
176 else {
Willy Tarreau2b718102021-04-21 07:32:39 +0200177 LIST_APPEND(&sched->tasklets[sched->current_queue], &tl->list);
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100178 sched->tl_class_mask |= 1 << sched->current_queue;
179 }
Willy Tarreau4781b152021-04-06 13:53:36 +0200180 _HA_ATOMIC_INC(&sched->rq_total);
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100181 } else {
182 /* this tasklet runs on a specific thread. */
Willy Tarreau2b718102021-04-21 07:32:39 +0200183 MT_LIST_APPEND(&task_per_thread[thr].shared_tasklet_list, (struct mt_list *)&tl->list);
Willy Tarreau4781b152021-04-06 13:53:36 +0200184 _HA_ATOMIC_INC(&task_per_thread[thr].rq_total);
Willy Tarreau9c6dbf02021-02-24 17:51:38 +0100185 if (sleeping_thread_mask & (1UL << thr)) {
186 _HA_ATOMIC_AND(&sleeping_thread_mask, ~(1UL << thr));
187 wake_thread(thr);
188 }
189 }
190}
191
Willy Tarreau4726f532009-03-07 17:25:21 +0100192/* Puts the task <t> in run queue at a position depending on t->nice. <t> is
193 * returned. The nice value assigns boosts in 32th of the run queue size. A
Christopher Faulet34c5cc92016-12-06 09:15:30 +0100194 * nice value of -1024 sets the task to -tasks_run_queue*32, while a nice value
195 * of 1024 sets the task to tasks_run_queue*32. The state flags are cleared, so
196 * the caller will have to set its flags after this call.
Willy Tarreau4726f532009-03-07 17:25:21 +0100197 * The task must not already be in the run queue. If unsure, use the safer
198 * task_wakeup() function.
Willy Tarreau91e99932008-06-30 07:51:00 +0200199 */
Willy Tarreau018564e2021-02-24 16:41:11 +0100200void __task_wakeup(struct task *t)
Willy Tarreaue33aece2007-04-30 13:15:14 +0200201{
Willy Tarreau018564e2021-02-24 16:41:11 +0100202 struct eb_root *root = &sched->rqueue;
203
Olivier Houchardb1ca58b2018-06-06 14:22:03 +0200204#ifdef USE_THREAD
Willy Tarreau018564e2021-02-24 16:41:11 +0100205 if (t->thread_mask != tid_bit && global.nbthread != 1) {
206 root = &rqueue;
207
Willy Tarreau4781b152021-04-06 13:53:36 +0200208 _HA_ATOMIC_INC(&grq_total);
Olivier Houchardf6e6dc12018-05-18 18:38:23 +0200209 HA_SPIN_LOCK(TASK_RQ_LOCK, &rq_lock);
Willy Tarreau9c7b8082021-02-24 15:10:07 +0100210
Olivier Houchardde82aea2019-04-17 19:10:22 +0200211 global_tasks_mask |= t->thread_mask;
Willy Tarreauc6ba9a02021-02-20 12:49:54 +0100212 t->rq.key = ++global_rqueue_ticks;
Olivier Houcharded1a6a02019-04-18 14:12:51 +0200213 __ha_barrier_store();
Willy Tarreauc6ba9a02021-02-20 12:49:54 +0100214 } else
Olivier Houchardc4aac9e2018-07-26 15:25:49 +0200215#endif
Willy Tarreau9c7b8082021-02-24 15:10:07 +0100216 {
Willy Tarreau4781b152021-04-06 13:53:36 +0200217 _HA_ATOMIC_INC(&sched->rq_total);
Willy Tarreauc6ba9a02021-02-20 12:49:54 +0100218 t->rq.key = ++sched->rqueue_ticks;
Willy Tarreau9c7b8082021-02-24 15:10:07 +0100219 }
Willy Tarreau91e99932008-06-30 07:51:00 +0200220
221 if (likely(t->nice)) {
222 int offset;
223
Willy Tarreau4781b152021-04-06 13:53:36 +0200224 _HA_ATOMIC_INC(&niced_tasks);
Willy Tarreau2d1fd0a2019-04-15 09:18:31 +0200225 offset = t->nice * (int)global.tune.runqueue_depth;
Willy Tarreau4726f532009-03-07 17:25:21 +0100226 t->rq.key += offset;
Willy Tarreau91e99932008-06-30 07:51:00 +0200227 }
228
Willy Tarreaud9add3a2019-04-25 08:57:41 +0200229 if (task_profiling_mask & tid_bit)
Willy Tarreau9efd7452018-05-31 14:48:54 +0200230 t->call_date = now_mono_time();
231
Olivier Houchardf6e6dc12018-05-18 18:38:23 +0200232 eb32sc_insert(root, &t->rq, t->thread_mask);
Willy Tarreau018564e2021-02-24 16:41:11 +0100233
Olivier Houchardb1ca58b2018-06-06 14:22:03 +0200234#ifdef USE_THREAD
Olivier Houchardf6e6dc12018-05-18 18:38:23 +0200235 if (root == &rqueue) {
Olivier Houchard4c2832852019-03-08 18:48:47 +0100236 _HA_ATOMIC_OR(&t->state, TASK_GLOBAL);
Olivier Houchardf6e6dc12018-05-18 18:38:23 +0200237 HA_SPIN_UNLOCK(TASK_RQ_LOCK, &rq_lock);
Willy Tarreau2c41d772021-02-24 16:13:03 +0100238
Willy Tarreaueeffb3d2021-02-24 16:44:51 +0100239 /* If all threads that are supposed to handle this task are sleeping,
240 * wake one.
241 */
242 if ((((t->thread_mask & all_threads_mask) & sleeping_thread_mask) ==
243 (t->thread_mask & all_threads_mask))) {
244 unsigned long m = (t->thread_mask & all_threads_mask) &~ tid_bit;
Olivier Houchard1b327902019-03-15 00:23:10 +0100245
Willy Tarreaueeffb3d2021-02-24 16:44:51 +0100246 m = (m & (m - 1)) ^ m; // keep lowest bit set
247 _HA_ATOMIC_AND(&sleeping_thread_mask, ~m);
248 wake_thread(my_ffsl(m) - 1);
249 }
Olivier Houchard1b327902019-03-15 00:23:10 +0100250 }
Willy Tarreau85d9b842018-07-27 17:14:41 +0200251#endif
Olivier Houchardf6e6dc12018-05-18 18:38:23 +0200252 return;
Willy Tarreaue33aece2007-04-30 13:15:14 +0200253}
Willy Tarreaud825eef2007-05-12 22:35:00 +0200254
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200255/*
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100256 * __task_queue()
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200257 *
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200258 * Inserts a task into wait queue <wq> at the position given by its expiration
Willy Tarreau4726f532009-03-07 17:25:21 +0100259 * date. It does not matter if the task was already in the wait queue or not,
Willy Tarreau7a969992021-09-30 16:38:09 +0200260 * as it will be unlinked. The task MUST NOT have an infinite expiration timer.
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100261 * Last, tasks must not be queued further than the end of the tree, which is
262 * between <now_ms> and <now_ms> + 2^31 ms (now+24days in 32bit).
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100263 *
264 * This function should not be used directly, it is meant to be called by the
265 * inline version of task_queue() which performs a few cheap preliminary tests
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200266 * before deciding to call __task_queue(). Moreover this function doesn't care
267 * at all about locking so the caller must be careful when deciding whether to
268 * lock or not around this call.
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200269 */
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200270void __task_queue(struct task *task, struct eb_root *wq)
Willy Tarreau964c9362007-01-07 00:38:00 +0100271{
Willy Tarreaue5d79bc2020-07-22 14:29:42 +0200272#ifdef USE_THREAD
273 BUG_ON((wq == &timers && !(task->state & TASK_SHARED_WQ)) ||
274 (wq == &sched->timers && (task->state & TASK_SHARED_WQ)) ||
275 (wq != &timers && wq != &sched->timers));
276#endif
Willy Tarreau7a969992021-09-30 16:38:09 +0200277 /* if this happens the process is doomed anyway, so better catch it now
278 * so that we have the caller in the stack.
279 */
280 BUG_ON(task->expire == TICK_ETERNITY);
Willy Tarreaue5d79bc2020-07-22 14:29:42 +0200281
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100282 if (likely(task_in_wq(task)))
Willy Tarreau4726f532009-03-07 17:25:21 +0100283 __task_unlink_wq(task);
Willy Tarreau4726f532009-03-07 17:25:21 +0100284
285 /* the task is not in the queue now */
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100286 task->wq.key = task->expire;
Willy Tarreau28c41a42008-06-29 17:00:59 +0200287#ifdef DEBUG_CHECK_INVALID_EXPIRATION_DATES
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100288 if (tick_is_lt(task->wq.key, now_ms))
Willy Tarreau28c41a42008-06-29 17:00:59 +0200289 /* we're queuing too far away or in the past (most likely) */
Willy Tarreau4726f532009-03-07 17:25:21 +0100290 return;
Willy Tarreau28c41a42008-06-29 17:00:59 +0200291#endif
Willy Tarreauce44f122008-07-05 18:16:19 +0200292
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200293 eb32_insert(wq, &task->wq);
Willy Tarreau964c9362007-01-07 00:38:00 +0100294}
295
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200296/*
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200297 * Extract all expired timers from the timer queue, and wakes up all
Willy Tarreauc49ba522019-12-11 08:12:23 +0100298 * associated tasks.
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200299 */
Willy Tarreauc49ba522019-12-11 08:12:23 +0100300void wake_expired_tasks()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200301{
Willy Tarreaud022e9c2019-09-24 08:25:15 +0200302 struct task_per_thread * const tt = sched; // thread's tasks
Willy Tarreau3cfaa8d2020-10-16 09:26:22 +0200303 int max_processed = global.tune.runqueue_depth;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200304 struct task *task;
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200305 struct eb32_node *eb;
Willy Tarreauaf613e82020-06-05 08:40:51 +0200306 __decl_thread(int key);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200307
Willy Tarreau3cfaa8d2020-10-16 09:26:22 +0200308 while (max_processed-- > 0) {
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200309 lookup_next_local:
Willy Tarreau4c1e1ad2019-09-24 07:19:08 +0200310 eb = eb32_lookup_ge(&tt->timers, now_ms - TIMER_LOOK_BACK);
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200311 if (!eb) {
312 /* we might have reached the end of the tree, typically because
313 * <now_ms> is in the first half and we're first scanning the last
314 * half. Let's loop back to the beginning of the tree now.
315 */
Willy Tarreau4c1e1ad2019-09-24 07:19:08 +0200316 eb = eb32_first(&tt->timers);
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200317 if (likely(!eb))
318 break;
319 }
320
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200321 /* It is possible that this task was left at an earlier place in the
322 * tree because a recent call to task_queue() has not moved it. This
323 * happens when the new expiration date is later than the old one.
324 * Since it is very unlikely that we reach a timeout anyway, it's a
325 * lot cheaper to proceed like this because we almost never update
326 * the tree. We may also find disabled expiration dates there. Since
327 * we have detached the task from the tree, we simply call task_queue
328 * to take care of this. Note that we might occasionally requeue it at
329 * the same place, before <eb>, so we have to check if this happens,
330 * and adjust <eb>, otherwise we may skip it which is not what we want.
331 * We may also not requeue the task (and not point eb at it) if its
Willy Tarreau77015ab2020-06-19 11:50:27 +0200332 * expiration time is not set. We also make sure we leave the real
333 * expiration date for the next task in the queue so that when calling
334 * next_timer_expiry() we're guaranteed to see the next real date and
335 * not the next apparent date. This is in order to avoid useless
336 * wakeups.
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200337 */
Willy Tarreau77015ab2020-06-19 11:50:27 +0200338
339 task = eb32_entry(eb, struct task, wq);
340 if (tick_is_expired(task->expire, now_ms)) {
341 /* expired task, wake it up */
342 __task_unlink_wq(task);
343 task_wakeup(task, TASK_WOKEN_TIMER);
344 }
345 else if (task->expire != eb->key) {
346 /* task is not expired but its key doesn't match so let's
347 * update it and skip to next apparently expired task.
348 */
349 __task_unlink_wq(task);
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200350 if (tick_isset(task->expire))
Willy Tarreau4c1e1ad2019-09-24 07:19:08 +0200351 __task_queue(task, &tt->timers);
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200352 }
Willy Tarreau77015ab2020-06-19 11:50:27 +0200353 else {
Willy Tarreau7a969992021-09-30 16:38:09 +0200354 /* task not expired and correctly placed. It may not be eternal. */
355 BUG_ON(task->expire == TICK_ETERNITY);
Willy Tarreau77015ab2020-06-19 11:50:27 +0200356 break;
357 }
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200358 }
359
360#ifdef USE_THREAD
Willy Tarreau1e928c02019-05-28 18:57:25 +0200361 if (eb_is_empty(&timers))
362 goto leave;
363
364 HA_RWLOCK_RDLOCK(TASK_WQ_LOCK, &wq_lock);
365 eb = eb32_lookup_ge(&timers, now_ms - TIMER_LOOK_BACK);
366 if (!eb) {
367 eb = eb32_first(&timers);
368 if (likely(!eb)) {
369 HA_RWLOCK_RDUNLOCK(TASK_WQ_LOCK, &wq_lock);
370 goto leave;
371 }
372 }
373 key = eb->key;
Willy Tarreau1e928c02019-05-28 18:57:25 +0200374
Willy Tarreaud48ed662020-10-16 09:31:41 +0200375 if (tick_is_lt(now_ms, key)) {
376 HA_RWLOCK_RDUNLOCK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreau1e928c02019-05-28 18:57:25 +0200377 goto leave;
Willy Tarreaud48ed662020-10-16 09:31:41 +0200378 }
Willy Tarreau1e928c02019-05-28 18:57:25 +0200379
380 /* There's really something of interest here, let's visit the queue */
381
Willy Tarreaud48ed662020-10-16 09:31:41 +0200382 if (HA_RWLOCK_TRYRDTOSK(TASK_WQ_LOCK, &wq_lock)) {
383 /* if we failed to grab the lock it means another thread is
384 * already doing the same here, so let it do the job.
385 */
386 HA_RWLOCK_RDUNLOCK(TASK_WQ_LOCK, &wq_lock);
387 goto leave;
388 }
389
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200390 while (1) {
Emeric Brunc60def82017-09-27 14:59:38 +0200391 lookup_next:
Willy Tarreau3cfaa8d2020-10-16 09:26:22 +0200392 if (max_processed-- <= 0)
393 break;
Emeric Brun01948972017-03-30 15:37:25 +0200394 eb = eb32_lookup_ge(&timers, now_ms - TIMER_LOOK_BACK);
Emeric Brunc60def82017-09-27 14:59:38 +0200395 if (!eb) {
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100396 /* we might have reached the end of the tree, typically because
397 * <now_ms> is in the first half and we're first scanning the last
398 * half. Let's loop back to the beginning of the tree now.
399 */
400 eb = eb32_first(&timers);
Willy Tarreaub992ba12017-11-05 19:09:27 +0100401 if (likely(!eb))
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100402 break;
403 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200404
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100405 task = eb32_entry(eb, struct task, wq);
Willy Tarreau77015ab2020-06-19 11:50:27 +0200406 if (tick_is_expired(task->expire, now_ms)) {
407 /* expired task, wake it up */
Willy Tarreaud48ed662020-10-16 09:31:41 +0200408 HA_RWLOCK_SKTOWR(TASK_WQ_LOCK, &wq_lock);
Willy Tarreau77015ab2020-06-19 11:50:27 +0200409 __task_unlink_wq(task);
Willy Tarreaud48ed662020-10-16 09:31:41 +0200410 HA_RWLOCK_WRTOSK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreau77015ab2020-06-19 11:50:27 +0200411 task_wakeup(task, TASK_WOKEN_TIMER);
412 }
413 else if (task->expire != eb->key) {
414 /* task is not expired but its key doesn't match so let's
415 * update it and skip to next apparently expired task.
416 */
Willy Tarreaud48ed662020-10-16 09:31:41 +0200417 HA_RWLOCK_SKTOWR(TASK_WQ_LOCK, &wq_lock);
Willy Tarreau77015ab2020-06-19 11:50:27 +0200418 __task_unlink_wq(task);
Willy Tarreaub992ba12017-11-05 19:09:27 +0100419 if (tick_isset(task->expire))
Willy Tarreau783afbe2020-07-22 14:12:45 +0200420 __task_queue(task, &timers);
Willy Tarreaud48ed662020-10-16 09:31:41 +0200421 HA_RWLOCK_WRTOSK(TASK_WQ_LOCK, &wq_lock);
Emeric Brunc60def82017-09-27 14:59:38 +0200422 goto lookup_next;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200423 }
Willy Tarreau77015ab2020-06-19 11:50:27 +0200424 else {
Willy Tarreau7a969992021-09-30 16:38:09 +0200425 /* task not expired and correctly placed. It may not be eternal. */
426 BUG_ON(task->expire == TICK_ETERNITY);
Willy Tarreau77015ab2020-06-19 11:50:27 +0200427 break;
428 }
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100429 }
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200430
Willy Tarreaud48ed662020-10-16 09:31:41 +0200431 HA_RWLOCK_SKUNLOCK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreaub20aa9e2018-10-15 14:52:21 +0200432#endif
Willy Tarreau1e928c02019-05-28 18:57:25 +0200433leave:
Willy Tarreauc49ba522019-12-11 08:12:23 +0100434 return;
435}
436
437/* Checks the next timer for the current thread by looking into its own timer
438 * list and the global one. It may return TICK_ETERNITY if no timer is present.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500439 * Note that the next timer might very well be slightly in the past.
Willy Tarreauc49ba522019-12-11 08:12:23 +0100440 */
441int next_timer_expiry()
442{
443 struct task_per_thread * const tt = sched; // thread's tasks
444 struct eb32_node *eb;
445 int ret = TICK_ETERNITY;
Willy Tarreau6ce02322020-08-21 05:48:34 +0200446 __decl_thread(int key = TICK_ETERNITY);
Willy Tarreauc49ba522019-12-11 08:12:23 +0100447
448 /* first check in the thread-local timers */
449 eb = eb32_lookup_ge(&tt->timers, now_ms - TIMER_LOOK_BACK);
450 if (!eb) {
451 /* we might have reached the end of the tree, typically because
452 * <now_ms> is in the first half and we're first scanning the last
453 * half. Let's loop back to the beginning of the tree now.
454 */
455 eb = eb32_first(&tt->timers);
456 }
457
458 if (eb)
459 ret = eb->key;
460
461#ifdef USE_THREAD
462 if (!eb_is_empty(&timers)) {
463 HA_RWLOCK_RDLOCK(TASK_WQ_LOCK, &wq_lock);
464 eb = eb32_lookup_ge(&timers, now_ms - TIMER_LOOK_BACK);
465 if (!eb)
466 eb = eb32_first(&timers);
467 if (eb)
468 key = eb->key;
469 HA_RWLOCK_RDUNLOCK(TASK_WQ_LOCK, &wq_lock);
470 if (eb)
471 ret = tick_first(ret, key);
472 }
473#endif
Willy Tarreaub992ba12017-11-05 19:09:27 +0100474 return ret;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200475}
476
Willy Tarreau59153fe2020-06-24 10:17:29 +0200477/* Walks over tasklet lists sched->tasklets[0..TL_CLASSES-1] and run at most
478 * budget[TL_*] of them. Returns the number of entries effectively processed
479 * (tasks and tasklets merged). The count of tasks in the list for the current
480 * thread is adjusted.
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100481 */
Willy Tarreau59153fe2020-06-24 10:17:29 +0200482unsigned int run_tasks_from_lists(unsigned int budgets[])
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100483{
Willy Tarreau144f84a2021-03-02 16:09:26 +0100484 struct task *(*process)(struct task *t, void *ctx, unsigned int state);
Willy Tarreauba48d5c2020-06-24 09:54:24 +0200485 struct list *tl_queues = sched->tasklets;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100486 struct task *t;
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200487 uint8_t budget_mask = (1 << TL_CLASSES) - 1;
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100488 struct sched_activity *profile_entry = NULL;
Willy Tarreau59153fe2020-06-24 10:17:29 +0200489 unsigned int done = 0;
490 unsigned int queue;
Willy Tarreau144f84a2021-03-02 16:09:26 +0100491 unsigned int state;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100492 void *ctx;
Willy Tarreau59153fe2020-06-24 10:17:29 +0200493
494 for (queue = 0; queue < TL_CLASSES;) {
495 sched->current_queue = queue;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100496
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200497 /* global.tune.sched.low-latency is set */
498 if (global.tune.options & GTUNE_SCHED_LOW_LATENCY) {
499 if (unlikely(sched->tl_class_mask & budget_mask & ((1 << queue) - 1))) {
500 /* a lower queue index has tasks again and still has a
501 * budget to run them. Let's switch to it now.
502 */
503 queue = (sched->tl_class_mask & 1) ? 0 :
504 (sched->tl_class_mask & 2) ? 1 : 2;
505 continue;
506 }
507
508 if (unlikely(queue > TL_URGENT &&
509 budget_mask & (1 << TL_URGENT) &&
510 !MT_LIST_ISEMPTY(&sched->shared_tasklet_list))) {
511 /* an urgent tasklet arrived from another thread */
512 break;
513 }
514
515 if (unlikely(queue > TL_NORMAL &&
516 budget_mask & (1 << TL_NORMAL) &&
Willy Tarreau2c41d772021-02-24 16:13:03 +0100517 (!eb_is_empty(&sched->rqueue) ||
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200518 (global_tasks_mask & tid_bit)))) {
519 /* a task was woken up by a bulk tasklet or another thread */
520 break;
521 }
522 }
523
Willy Tarreauba48d5c2020-06-24 09:54:24 +0200524 if (LIST_ISEMPTY(&tl_queues[queue])) {
525 sched->tl_class_mask &= ~(1 << queue);
Willy Tarreau59153fe2020-06-24 10:17:29 +0200526 queue++;
527 continue;
Willy Tarreauba48d5c2020-06-24 09:54:24 +0200528 }
529
Willy Tarreau59153fe2020-06-24 10:17:29 +0200530 if (!budgets[queue]) {
Willy Tarreaue7723bd2020-06-24 11:11:02 +0200531 budget_mask &= ~(1 << queue);
Willy Tarreau59153fe2020-06-24 10:17:29 +0200532 queue++;
533 continue;
534 }
Willy Tarreauba48d5c2020-06-24 09:54:24 +0200535
Willy Tarreau59153fe2020-06-24 10:17:29 +0200536 budgets[queue]--;
Willy Tarreauba48d5c2020-06-24 09:54:24 +0200537 t = (struct task *)LIST_ELEM(tl_queues[queue].n, struct tasklet *, list);
Amaury Denoyelle7b368332021-07-28 16:12:57 +0200538 state = t->state & (TASK_SHARED_WQ|TASK_SELF_WAKING|TASK_HEAVY|TASK_F_TASKLET|TASK_KILLED|TASK_F_USR1|TASK_KILLED);
Willy Tarreau74dea8c2021-02-26 00:25:51 +0100539
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100540 ti->flags &= ~TI_FL_STUCK; // this thread is still running
541 activity[tid].ctxsw++;
542 ctx = t->context;
543 process = t->process;
544 t->calls++;
Willy Tarreaud23d4132020-01-31 10:39:03 +0100545 sched->current = t;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100546
Willy Tarreau4781b152021-04-06 13:53:36 +0200547 _HA_ATOMIC_DEC(&sched->rq_total);
Willy Tarreau2da4c312020-11-30 14:52:11 +0100548
Willy Tarreaudb4e2382021-03-02 15:54:11 +0100549 if (state & TASK_F_TASKLET) {
Willy Tarreau2a54ffb2021-02-25 09:32:58 +0100550 uint64_t before = 0;
551
Willy Tarreau4d6c5942020-11-30 14:58:53 +0100552 LIST_DEL_INIT(&((struct tasklet *)t)->list);
553 __ha_barrier_store();
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100554
555 if (unlikely(task_profiling_mask & tid_bit)) {
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100556 profile_entry = sched_activity_entry(sched_activity, t->process);
557 before = now_mono_time();
Willy Tarreaub2285de2021-02-25 08:39:07 +0100558#ifdef DEBUG_TASK
Willy Tarreau2a54ffb2021-02-25 09:32:58 +0100559 if (((struct tasklet *)t)->call_date) {
560 HA_ATOMIC_ADD(&profile_entry->lat_time, before - ((struct tasklet *)t)->call_date);
561 ((struct tasklet *)t)->call_date = 0;
562 }
Willy Tarreaub2285de2021-02-25 08:39:07 +0100563#endif
Willy Tarreau2a54ffb2021-02-25 09:32:58 +0100564 }
565
566 state = _HA_ATOMIC_XCHG(&t->state, state);
567 __ha_barrier_atomic_store();
568
Amaury Denoyelle7b368332021-07-28 16:12:57 +0200569 if (likely(!(state & TASK_KILLED))) {
570 process(t, ctx, state);
571 }
572 else {
573 done++;
574 sched->current = NULL;
575 pool_free(pool_head_tasklet, t);
576 __ha_barrier_store();
577 continue;
578 }
Willy Tarreau2a54ffb2021-02-25 09:32:58 +0100579
580 if (unlikely(task_profiling_mask & tid_bit)) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200581 HA_ATOMIC_INC(&profile_entry->calls);
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100582 HA_ATOMIC_ADD(&profile_entry->cpu_time, now_mono_time() - before);
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100583 }
Willy Tarreau2a54ffb2021-02-25 09:32:58 +0100584
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100585 done++;
Willy Tarreaud23d4132020-01-31 10:39:03 +0100586 sched->current = NULL;
587 __ha_barrier_store();
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100588 continue;
589 }
590
Willy Tarreau4d6c5942020-11-30 14:58:53 +0100591 LIST_DEL_INIT(&((struct tasklet *)t)->list);
592 __ha_barrier_store();
Willy Tarreau6fa8bcd2021-03-02 16:26:05 +0100593 state = _HA_ATOMIC_XCHG(&t->state, state|TASK_RUNNING|TASK_F_USR1);
Willy Tarreau952c2642020-01-31 16:39:30 +0100594 __ha_barrier_atomic_store();
Willy Tarreau952c2642020-01-31 16:39:30 +0100595
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100596 /* OK then this is a regular task */
597
Willy Tarreau4781b152021-04-06 13:53:36 +0200598 _HA_ATOMIC_DEC(&task_per_thread[tid].tasks_in_list);
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100599 if (unlikely(t->call_date)) {
600 uint64_t now_ns = now_mono_time();
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100601 uint64_t lat = now_ns - t->call_date;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100602
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100603 t->lat_time += lat;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100604 t->call_date = now_ns;
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100605 profile_entry = sched_activity_entry(sched_activity, t->process);
606 HA_ATOMIC_ADD(&profile_entry->lat_time, lat);
Willy Tarreau4781b152021-04-06 13:53:36 +0200607 HA_ATOMIC_INC(&profile_entry->calls);
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100608 }
609
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100610 __ha_barrier_store();
Willy Tarreau8a6049c2020-06-30 11:48:48 +0200611
612 /* Note for below: if TASK_KILLED arrived before we've read the state, we
613 * directly free the task. Otherwise it will be seen after processing and
614 * it's freed on the exit path.
615 */
616 if (likely(!(state & TASK_KILLED) && process == process_stream))
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100617 t = process_stream(t, ctx, state);
Willy Tarreau8a6049c2020-06-30 11:48:48 +0200618 else if (!(state & TASK_KILLED) && process != NULL)
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100619 t = process(t, ctx, state);
620 else {
Willy Tarreau273aea42020-07-17 14:37:51 +0200621 task_unlink_wq(t);
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100622 __task_free(t);
623 sched->current = NULL;
624 __ha_barrier_store();
625 /* We don't want max_processed to be decremented if
626 * we're just freeing a destroyed task, we should only
627 * do so if we really ran a task.
628 */
629 continue;
630 }
631 sched->current = NULL;
632 __ha_barrier_store();
633 /* If there is a pending state we have to wake up the task
634 * immediately, else we defer it into wait queue
635 */
636 if (t != NULL) {
637 if (unlikely(t->call_date)) {
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100638 uint64_t cpu = now_mono_time() - t->call_date;
639
640 t->cpu_time += cpu;
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100641 t->call_date = 0;
Willy Tarreau4e2282f2021-01-29 00:07:40 +0100642 HA_ATOMIC_ADD(&profile_entry->cpu_time, cpu);
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100643 }
644
Willy Tarreau1db42732021-04-06 11:44:07 +0200645 state = _HA_ATOMIC_AND_FETCH(&t->state, ~TASK_RUNNING);
Willy Tarreau8a6049c2020-06-30 11:48:48 +0200646 if (unlikely(state & TASK_KILLED)) {
Willy Tarreau273aea42020-07-17 14:37:51 +0200647 task_unlink_wq(t);
Willy Tarreau8a6049c2020-06-30 11:48:48 +0200648 __task_free(t);
649 }
650 else if (state & TASK_WOKEN_ANY)
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100651 task_wakeup(t, 0);
652 else
653 task_queue(t);
654 }
655 done++;
656 }
Willy Tarreauba48d5c2020-06-24 09:54:24 +0200657 sched->current_queue = -1;
Willy Tarreau116ef222020-06-23 16:35:38 +0200658
Willy Tarreau4ffa0b52020-01-30 18:13:13 +0100659 return done;
660}
661
Willy Tarreau58b458d2008-06-29 22:40:23 +0200662/* The run queue is chronologically sorted in a tree. An insertion counter is
663 * used to assign a position to each task. This counter may be combined with
664 * other variables (eg: nice value) to set the final position in the tree. The
665 * counter may wrap without a problem, of course. We then limit the number of
Christopher Faulet8a48f672017-11-14 10:38:36 +0100666 * tasks processed to 200 in any case, so that general latency remains low and
Willy Tarreaucde79022019-04-12 18:03:41 +0200667 * so that task positions have a chance to be considered. The function scans
668 * both the global and local run queues and picks the most urgent task between
669 * the two. We need to grab the global runqueue lock to touch it so it's taken
670 * on the very first access to the global run queue and is released as soon as
671 * it reaches the end.
Willy Tarreau58b458d2008-06-29 22:40:23 +0200672 *
673 * The function adjusts <next> if a new event is closer.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200674 */
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +0100675void process_runnable_tasks()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676{
Willy Tarreaud022e9c2019-09-24 08:25:15 +0200677 struct task_per_thread * const tt = sched;
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200678 struct eb32sc_node *lrq; // next local run queue entry
679 struct eb32sc_node *grq; // next global run queue entry
Willy Tarreau964c9362007-01-07 00:38:00 +0100680 struct task *t;
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200681 const unsigned int default_weights[TL_CLASSES] = {
682 [TL_URGENT] = 64, // ~50% of CPU bandwidth for I/O
683 [TL_NORMAL] = 48, // ~37% of CPU bandwidth for tasks
684 [TL_BULK] = 16, // ~13% of CPU bandwidth for self-wakers
Willy Tarreau401135c2021-02-26 09:16:22 +0100685 [TL_HEAVY] = 1, // never more than 1 heavy task at once
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200686 };
687 unsigned int max[TL_CLASSES]; // max to be run per class
688 unsigned int max_total; // sum of max above
Olivier Houchard06910462019-10-11 16:35:01 +0200689 struct mt_list *tmp_list;
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200690 unsigned int queue;
691 int max_processed;
Willy Tarreaue7923c12021-02-25 07:09:08 +0100692 int lpicked, gpicked;
Willy Tarreau76390da2021-02-26 10:18:11 +0100693 int heavy_queued = 0;
Willy Tarreauc309dbd2020-11-30 15:39:00 +0100694 int budget;
Christopher Faulet3911ee82017-11-14 10:26:53 +0100695
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200696 ti->flags &= ~TI_FL_STUCK; // this thread is still running
697
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200698 if (!thread_has_tasks()) {
699 activity[tid].empty_rq++;
700 return;
701 }
702
Willy Tarreau5c8be272020-06-19 12:17:55 +0200703 max_processed = global.tune.runqueue_depth;
704
705 if (likely(niced_tasks))
706 max_processed = (max_processed + 3) / 4;
707
Willy Tarreau1691ba32021-03-10 09:26:24 +0100708 if (max_processed < sched->rq_total && sched->rq_total <= 2*max_processed) {
709 /* If the run queue exceeds the budget by up to 50%, let's cut it
710 * into two identical halves to improve latency.
711 */
712 max_processed = sched->rq_total / 2;
713 }
714
Willy Tarreau5c8be272020-06-19 12:17:55 +0200715 not_done_yet:
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200716 max[TL_URGENT] = max[TL_NORMAL] = max[TL_BULK] = 0;
Willy Tarreaucde79022019-04-12 18:03:41 +0200717
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200718 /* urgent tasklets list gets a default weight of ~50% */
Willy Tarreau49f90bf2020-06-24 09:39:48 +0200719 if ((tt->tl_class_mask & (1 << TL_URGENT)) ||
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200720 !MT_LIST_ISEMPTY(&tt->shared_tasklet_list))
721 max[TL_URGENT] = default_weights[TL_URGENT];
Willy Tarreaua62917b2020-01-30 18:37:28 +0100722
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200723 /* normal tasklets list gets a default weight of ~37% */
Willy Tarreau49f90bf2020-06-24 09:39:48 +0200724 if ((tt->tl_class_mask & (1 << TL_NORMAL)) ||
Willy Tarreau2c41d772021-02-24 16:13:03 +0100725 !eb_is_empty(&sched->rqueue) || (global_tasks_mask & tid_bit))
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200726 max[TL_NORMAL] = default_weights[TL_NORMAL];
Willy Tarreaua62917b2020-01-30 18:37:28 +0100727
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200728 /* bulk tasklets list gets a default weight of ~13% */
Willy Tarreau49f90bf2020-06-24 09:39:48 +0200729 if ((tt->tl_class_mask & (1 << TL_BULK)))
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200730 max[TL_BULK] = default_weights[TL_BULK];
731
Willy Tarreau401135c2021-02-26 09:16:22 +0100732 /* heavy tasks are processed only once and never refilled in a
Willy Tarreau76390da2021-02-26 10:18:11 +0100733 * call round. That budget is not lost either as we don't reset
734 * it unless consumed.
Willy Tarreau401135c2021-02-26 09:16:22 +0100735 */
Willy Tarreau76390da2021-02-26 10:18:11 +0100736 if (!heavy_queued) {
737 if ((tt->tl_class_mask & (1 << TL_HEAVY)))
738 max[TL_HEAVY] = default_weights[TL_HEAVY];
739 else
740 max[TL_HEAVY] = 0;
741 heavy_queued = 1;
742 }
Willy Tarreau401135c2021-02-26 09:16:22 +0100743
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200744 /* Now compute a fair share of the weights. Total may slightly exceed
Willy Tarreau1553b662020-06-30 13:46:21 +0200745 * 100% due to rounding, this is not a problem. Note that while in
746 * theory the sum cannot be NULL as we cannot get there without tasklets
747 * to process, in practice it seldom happens when multiple writers
Willy Tarreau2b718102021-04-21 07:32:39 +0200748 * conflict and rollback on MT_LIST_TRY_APPEND(shared_tasklet_list), causing
Willy Tarreau1553b662020-06-30 13:46:21 +0200749 * a first MT_LIST_ISEMPTY() to succeed for thread_has_task() and the
750 * one above to finally fail. This is extremely rare and not a problem.
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200751 */
Willy Tarreau401135c2021-02-26 09:16:22 +0100752 max_total = max[TL_URGENT] + max[TL_NORMAL] + max[TL_BULK] + max[TL_HEAVY];
Willy Tarreau1553b662020-06-30 13:46:21 +0200753 if (!max_total)
754 return;
755
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200756 for (queue = 0; queue < TL_CLASSES; queue++)
757 max[queue] = ((unsigned)max_processed * max[queue] + max_total - 1) / max_total;
758
Willy Tarreau76390da2021-02-26 10:18:11 +0100759 /* The heavy queue must never process more than one task at once
760 * anyway.
761 */
762 if (max[TL_HEAVY] > 1)
763 max[TL_HEAVY] = 1;
764
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200765 lrq = grq = NULL;
Christopher Faulet8a48f672017-11-14 10:38:36 +0100766
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200767 /* pick up to max[TL_NORMAL] regular tasks from prio-ordered run queues */
768 /* Note: the grq lock is always held when grq is not null */
Willy Tarreaue7923c12021-02-25 07:09:08 +0100769 lpicked = gpicked = 0;
Willy Tarreau1f3b1412021-02-24 14:13:40 +0100770 budget = max[TL_NORMAL] - tt->tasks_in_list;
Willy Tarreaue7923c12021-02-25 07:09:08 +0100771 while (lpicked + gpicked < budget) {
Willy Tarreaucde79022019-04-12 18:03:41 +0200772 if ((global_tasks_mask & tid_bit) && !grq) {
Willy Tarreau3466e3c2019-04-15 18:52:40 +0200773#ifdef USE_THREAD
Willy Tarreaucde79022019-04-12 18:03:41 +0200774 HA_SPIN_LOCK(TASK_RQ_LOCK, &rq_lock);
Willy Tarreauc6ba9a02021-02-20 12:49:54 +0100775 grq = eb32sc_lookup_ge(&rqueue, global_rqueue_ticks - TIMER_LOOK_BACK, tid_bit);
Willy Tarreaucde79022019-04-12 18:03:41 +0200776 if (unlikely(!grq)) {
777 grq = eb32sc_first(&rqueue, tid_bit);
778 if (!grq) {
Olivier Houchardde82aea2019-04-17 19:10:22 +0200779 global_tasks_mask &= ~tid_bit;
Willy Tarreaucde79022019-04-12 18:03:41 +0200780 HA_SPIN_UNLOCK(TASK_RQ_LOCK, &rq_lock);
Olivier Houchardc4aac9e2018-07-26 15:25:49 +0200781 }
Willy Tarreauf0c531a2017-11-05 16:35:59 +0100782 }
Willy Tarreau3466e3c2019-04-15 18:52:40 +0200783#endif
Willy Tarreaucde79022019-04-12 18:03:41 +0200784 }
Willy Tarreauf0c531a2017-11-05 16:35:59 +0100785
Willy Tarreaucde79022019-04-12 18:03:41 +0200786 /* If a global task is available for this thread, it's in grq
787 * now and the global RQ is locked.
788 */
Olivier Houchardf6e6dc12018-05-18 18:38:23 +0200789
Willy Tarreaucde79022019-04-12 18:03:41 +0200790 if (!lrq) {
Willy Tarreauc6ba9a02021-02-20 12:49:54 +0100791 lrq = eb32sc_lookup_ge(&tt->rqueue, tt->rqueue_ticks - TIMER_LOOK_BACK, tid_bit);
Willy Tarreaucde79022019-04-12 18:03:41 +0200792 if (unlikely(!lrq))
Willy Tarreau4c1e1ad2019-09-24 07:19:08 +0200793 lrq = eb32sc_first(&tt->rqueue, tid_bit);
Willy Tarreauf0c531a2017-11-05 16:35:59 +0100794 }
Willy Tarreauf0c531a2017-11-05 16:35:59 +0100795
Willy Tarreaucde79022019-04-12 18:03:41 +0200796 if (!lrq && !grq)
797 break;
798
799 if (likely(!grq || (lrq && (int)(lrq->key - grq->key) <= 0))) {
800 t = eb32sc_entry(lrq, struct task, rq);
801 lrq = eb32sc_next(lrq, tid_bit);
Willy Tarreau2b363ac2021-02-25 07:14:58 +0100802 eb32sc_delete(&t->rq);
Willy Tarreaue7923c12021-02-25 07:09:08 +0100803 lpicked++;
Olivier Houchardf6e6dc12018-05-18 18:38:23 +0200804 }
Willy Tarreau3466e3c2019-04-15 18:52:40 +0200805#ifdef USE_THREAD
Willy Tarreaucde79022019-04-12 18:03:41 +0200806 else {
807 t = eb32sc_entry(grq, struct task, rq);
808 grq = eb32sc_next(grq, tid_bit);
Willy Tarreau2b363ac2021-02-25 07:14:58 +0100809 _HA_ATOMIC_AND(&t->state, ~TASK_GLOBAL);
810 eb32sc_delete(&t->rq);
811
Willy Tarreaucde79022019-04-12 18:03:41 +0200812 if (unlikely(!grq)) {
813 grq = eb32sc_first(&rqueue, tid_bit);
814 if (!grq) {
Olivier Houchardde82aea2019-04-17 19:10:22 +0200815 global_tasks_mask &= ~tid_bit;
Willy Tarreaucde79022019-04-12 18:03:41 +0200816 HA_SPIN_UNLOCK(TASK_RQ_LOCK, &rq_lock);
Willy Tarreaucde79022019-04-12 18:03:41 +0200817 }
818 }
Willy Tarreaue7923c12021-02-25 07:09:08 +0100819 gpicked++;
Emeric Brun01948972017-03-30 15:37:25 +0200820 }
Willy Tarreau3466e3c2019-04-15 18:52:40 +0200821#endif
Willy Tarreau2b363ac2021-02-25 07:14:58 +0100822 if (t->nice)
Willy Tarreau4781b152021-04-06 13:53:36 +0200823 _HA_ATOMIC_DEC(&niced_tasks);
Willy Tarreaucde79022019-04-12 18:03:41 +0200824
Willy Tarreaua868c292020-11-30 15:30:22 +0100825 /* Add it to the local task list */
Willy Tarreau2b718102021-04-21 07:32:39 +0200826 LIST_APPEND(&tt->tasklets[TL_NORMAL], &((struct tasklet *)t)->list);
Olivier Houchardb0bdae72018-05-18 18:45:28 +0200827 }
Willy Tarreaucde79022019-04-12 18:03:41 +0200828
829 /* release the rqueue lock */
830 if (grq) {
831 HA_SPIN_UNLOCK(TASK_RQ_LOCK, &rq_lock);
832 grq = NULL;
833 }
834
Willy Tarreaue7923c12021-02-25 07:09:08 +0100835 if (lpicked + gpicked) {
Willy Tarreauc309dbd2020-11-30 15:39:00 +0100836 tt->tl_class_mask |= 1 << TL_NORMAL;
Willy Tarreaue7923c12021-02-25 07:09:08 +0100837 _HA_ATOMIC_ADD(&tt->tasks_in_list, lpicked + gpicked);
Willy Tarreaub7e0c632021-03-09 09:59:50 +0100838#ifdef USE_THREAD
Willy Tarreau45499c52021-02-25 07:51:18 +0100839 if (gpicked) {
840 _HA_ATOMIC_SUB(&grq_total, gpicked);
Willy Tarreauc9afbb12021-02-25 07:19:45 +0100841 _HA_ATOMIC_ADD(&tt->rq_total, gpicked);
Willy Tarreau45499c52021-02-25 07:51:18 +0100842 }
Willy Tarreaub7e0c632021-03-09 09:59:50 +0100843#endif
Willy Tarreaue7923c12021-02-25 07:09:08 +0100844 activity[tid].tasksw += lpicked + gpicked;
Willy Tarreauc309dbd2020-11-30 15:39:00 +0100845 }
846
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200847 /* Merge the list of tasklets waken up by other threads to the
848 * main list.
849 */
850 tmp_list = MT_LIST_BEHEAD(&tt->shared_tasklet_list);
Willy Tarreau49f90bf2020-06-24 09:39:48 +0200851 if (tmp_list) {
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200852 LIST_SPLICE_END_DETACHED(&tt->tasklets[TL_URGENT], (struct list *)tmp_list);
Willy Tarreau49f90bf2020-06-24 09:39:48 +0200853 if (!LIST_ISEMPTY(&tt->tasklets[TL_URGENT]))
854 tt->tl_class_mask |= 1 << TL_URGENT;
855 }
Willy Tarreaucde79022019-04-12 18:03:41 +0200856
Willy Tarreau3ef7a192020-06-24 07:21:08 +0200857 /* execute tasklets in each queue */
Willy Tarreau59153fe2020-06-24 10:17:29 +0200858 max_processed -= run_tasks_from_lists(max);
Willy Tarreaua62917b2020-01-30 18:37:28 +0100859
Willy Tarreau5c8be272020-06-19 12:17:55 +0200860 /* some tasks may have woken other ones up */
Willy Tarreau0c0c85e2020-06-23 11:32:35 +0200861 if (max_processed > 0 && thread_has_tasks())
Willy Tarreau5c8be272020-06-19 12:17:55 +0200862 goto not_done_yet;
863
Willy Tarreau49f90bf2020-06-24 09:39:48 +0200864 if (tt->tl_class_mask)
Willy Tarreaucde79022019-04-12 18:03:41 +0200865 activity[tid].long_rq++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200866}
867
Willy Tarreau61369892021-09-30 08:52:11 +0200868/* report the average CPU idle percentage over all running threads, between 0 and 100 */
869uint sched_report_idle()
870{
871 uint total = 0;
872 uint rthr = 0;
873 uint thr;
874
875 for (thr = 0; thr < MAX_THREADS; thr++) {
876 if (!(all_threads_mask & (1UL << thr)))
877 continue;
878 total += HA_ATOMIC_LOAD(&ha_thread_info[thr].idle_pct);
879 rthr++;
880 }
881 return rthr ? total / rthr : 0;
882}
883
Willy Tarreaud8b325c2021-10-06 19:25:38 +0200884/* Update the idle time value twice a second, to be called after
885 * tv_update_date() when called after poll(), and currently called only by
886 * sched_leaving_poll() below. It relies on <before_poll> to be updated to
887 * the system time before calling poll().
888 */
889static inline void sched_measure_idle()
890{
891 /* Let's compute the idle to work ratio. We worked between after_poll
892 * and before_poll, and slept between before_poll and date. The idle_pct
893 * is updated at most twice every second. Note that the current second
894 * rarely changes so we avoid a multiply when not needed.
895 */
896 int delta;
897
898 if ((delta = date.tv_sec - before_poll.tv_sec))
899 delta *= 1000000;
900 idle_time += delta + (date.tv_usec - before_poll.tv_usec);
901
902 if ((delta = date.tv_sec - after_poll.tv_sec))
903 delta *= 1000000;
904 samp_time += delta + (date.tv_usec - after_poll.tv_usec);
905
906 after_poll.tv_sec = date.tv_sec; after_poll.tv_usec = date.tv_usec;
907 if (samp_time < 500000)
908 return;
909
910 HA_ATOMIC_STORE(&ti->idle_pct, (100ULL * idle_time + samp_time / 2) / samp_time);
911 idle_time = samp_time = 0;
912}
913
914/* Collect date and time information after leaving poll(). <timeout> must be
915 * set to the maximum sleep time passed to poll (in milliseconds), and
916 * <interrupted> must be zero if the poller reached the timeout or non-zero
917 * otherwise, which generally is provided by the poller's return value.
918 */
919void sched_leaving_poll(int timeout, int interrupted)
920{
921 sched_measure_idle();
922 ti->prev_cpu_time = now_cpu_time();
923 ti->prev_mono_time = now_mono_time();
924}
925
926/* Collect date and time information before calling poll(). This will be used
927 * to count the run time of the past loop and the sleep time of the next poll.
928 * It also compares the elasped and cpu times during the activity period to
929 * estimate the amount of stolen time, which is reported if higher than half
930 * a millisecond.
931 */
932void sched_entering_poll()
933{
934 uint64_t new_mono_time;
935 uint64_t new_cpu_time;
936 int64_t stolen;
937
938 gettimeofday(&before_poll, NULL);
939
940 new_cpu_time = now_cpu_time();
941 new_mono_time = now_mono_time();
942
943 if (ti->prev_cpu_time && ti->prev_mono_time) {
944 new_cpu_time -= ti->prev_cpu_time;
945 new_mono_time -= ti->prev_mono_time;
946 stolen = new_mono_time - new_cpu_time;
947 if (unlikely(stolen >= 500000)) {
948 stolen /= 500000;
949 /* more than half a millisecond difference might
950 * indicate an undesired preemption.
951 */
952 report_stolen_time(stolen);
953 }
954 }
955}
956
William Lallemand27f3fa52018-12-06 14:05:20 +0100957/*
958 * Delete every tasks before running the master polling loop
959 */
960void mworker_cleantasks()
961{
962 struct task *t;
963 int i;
William Lallemandb5823392018-12-06 15:14:37 +0100964 struct eb32_node *tmp_wq = NULL;
965 struct eb32sc_node *tmp_rq = NULL;
William Lallemand27f3fa52018-12-06 14:05:20 +0100966
967#ifdef USE_THREAD
968 /* cleanup the global run queue */
William Lallemandb5823392018-12-06 15:14:37 +0100969 tmp_rq = eb32sc_first(&rqueue, MAX_THREADS_MASK);
970 while (tmp_rq) {
971 t = eb32sc_entry(tmp_rq, struct task, rq);
972 tmp_rq = eb32sc_next(tmp_rq, MAX_THREADS_MASK);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200973 task_destroy(t);
William Lallemand27f3fa52018-12-06 14:05:20 +0100974 }
975 /* cleanup the timers queue */
William Lallemandb5823392018-12-06 15:14:37 +0100976 tmp_wq = eb32_first(&timers);
977 while (tmp_wq) {
978 t = eb32_entry(tmp_wq, struct task, wq);
979 tmp_wq = eb32_next(tmp_wq);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200980 task_destroy(t);
William Lallemand27f3fa52018-12-06 14:05:20 +0100981 }
982#endif
983 /* clean the per thread run queue */
984 for (i = 0; i < global.nbthread; i++) {
William Lallemandb5823392018-12-06 15:14:37 +0100985 tmp_rq = eb32sc_first(&task_per_thread[i].rqueue, MAX_THREADS_MASK);
986 while (tmp_rq) {
987 t = eb32sc_entry(tmp_rq, struct task, rq);
988 tmp_rq = eb32sc_next(tmp_rq, MAX_THREADS_MASK);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200989 task_destroy(t);
William Lallemand27f3fa52018-12-06 14:05:20 +0100990 }
991 /* cleanup the per thread timers queue */
William Lallemandb5823392018-12-06 15:14:37 +0100992 tmp_wq = eb32_first(&task_per_thread[i].timers);
993 while (tmp_wq) {
994 t = eb32_entry(tmp_wq, struct task, wq);
995 tmp_wq = eb32_next(tmp_wq);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200996 task_destroy(t);
William Lallemand27f3fa52018-12-06 14:05:20 +0100997 }
998 }
999}
1000
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001001/* perform minimal intializations */
1002static void init_task()
Willy Tarreau4726f532009-03-07 17:25:21 +01001003{
Willy Tarreau401135c2021-02-26 09:16:22 +01001004 int i, q;
Olivier Houchardf6e6dc12018-05-18 18:38:23 +02001005
Olivier Houchardb1ca58b2018-06-06 14:22:03 +02001006#ifdef USE_THREAD
Willy Tarreaub20aa9e2018-10-15 14:52:21 +02001007 memset(&timers, 0, sizeof(timers));
Willy Tarreau4726f532009-03-07 17:25:21 +01001008 memset(&rqueue, 0, sizeof(rqueue));
Olivier Houchardb1ca58b2018-06-06 14:22:03 +02001009#endif
Willy Tarreau8d8747a2018-10-15 16:12:48 +02001010 memset(&task_per_thread, 0, sizeof(task_per_thread));
Olivier Houchardb0bdae72018-05-18 18:45:28 +02001011 for (i = 0; i < MAX_THREADS; i++) {
Willy Tarreau401135c2021-02-26 09:16:22 +01001012 for (q = 0; q < TL_CLASSES; q++)
1013 LIST_INIT(&task_per_thread[i].tasklets[q]);
Olivier Houchard06910462019-10-11 16:35:01 +02001014 MT_LIST_INIT(&task_per_thread[i].shared_tasklet_list);
Olivier Houchardb0bdae72018-05-18 18:45:28 +02001015 }
Willy Tarreau4726f532009-03-07 17:25:21 +01001016}
1017
Willy Tarreaue7723bd2020-06-24 11:11:02 +02001018/* config parser for global "tune.sched.low-latency", accepts "on" or "off" */
1019static int cfg_parse_tune_sched_low_latency(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001020 const struct proxy *defpx, const char *file, int line,
Willy Tarreaue7723bd2020-06-24 11:11:02 +02001021 char **err)
1022{
1023 if (too_many_args(1, args, err, NULL))
1024 return -1;
1025
1026 if (strcmp(args[1], "on") == 0)
1027 global.tune.options |= GTUNE_SCHED_LOW_LATENCY;
1028 else if (strcmp(args[1], "off") == 0)
1029 global.tune.options &= ~GTUNE_SCHED_LOW_LATENCY;
1030 else {
1031 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
1032 return -1;
1033 }
1034 return 0;
1035}
1036
1037/* config keyword parsers */
1038static struct cfg_kw_list cfg_kws = {ILH, {
1039 { CFG_GLOBAL, "tune.sched.low-latency", cfg_parse_tune_sched_low_latency },
1040 { 0, NULL, NULL }
1041}};
1042
1043INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001044INITCALL0(STG_PREPARE, init_task);
1045
Willy Tarreaubaaee002006-06-26 02:48:02 +02001046/*
1047 * Local variables:
1048 * c-indent-level: 8
1049 * c-basic-offset: 8
1050 * End:
1051 */