blob: 6a89cd96448988ba7bbb7f3a2700c0fcec036679 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002 * include/proto/task.h
3 * Functions for task management.
4 *
5 * Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#ifndef _PROTO_TASK_H
23#define _PROTO_TASK_H
24
25
26#include <sys/time.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020027
28#include <common/config.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020029#include <common/memory.h>
Willy Tarreau96bcfd72007-04-29 10:41:56 +020030#include <common/mini-clist.h>
31#include <common/standard.h>
Willy Tarreaud0a201b2009-03-08 15:53:06 +010032#include <common/ticks.h>
Emeric Brunc60def82017-09-27 14:59:38 +020033#include <common/hathreads.h>
34
Willy Tarreau45cb4fb2009-10-26 21:10:04 +010035#include <eb32tree.h>
Willy Tarreau96bcfd72007-04-29 10:41:56 +020036
Willy Tarreaueb118892014-11-13 16:57:19 +010037#include <types/global.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020038#include <types/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039
Willy Tarreaud0a201b2009-03-08 15:53:06 +010040/* Principle of the wait queue.
41 *
42 * We want to be able to tell whether an expiration date is before of after the
43 * current time <now>. We KNOW that expiration dates are never too far apart,
44 * because they are measured in ticks (milliseconds). We also know that almost
45 * all dates will be in the future, and that a very small part of them will be
46 * in the past, they are the ones which have expired since last time we checked
47 * them. Using ticks, we know if a date is in the future or in the past, but we
48 * cannot use that to store sorted information because that reference changes
49 * all the time.
50 *
Willy Tarreaue35c94a2009-03-21 10:01:42 +010051 * We'll use the fact that the time wraps to sort timers. Timers above <now>
52 * are in the future, timers below <now> are in the past. Here, "above" and
53 * "below" are to be considered modulo 2^31.
Willy Tarreaud0a201b2009-03-08 15:53:06 +010054 *
Willy Tarreaue35c94a2009-03-21 10:01:42 +010055 * Timers are stored sorted in an ebtree. We use the new ability for ebtrees to
56 * lookup values starting from X to only expire tasks between <now> - 2^31 and
57 * <now>. If the end of the tree is reached while walking over it, we simply
58 * loop back to the beginning. That way, we have no problem keeping sorted
59 * wrapping timers in a tree, between (now - 24 days) and (now + 24 days). The
60 * keys in the tree always reflect their real position, none can be infinite.
61 * This reduces the number of checks to be performed.
Willy Tarreaud0a201b2009-03-08 15:53:06 +010062 *
63 * Another nice optimisation is to allow a timer to stay at an old place in the
64 * queue as long as it's not further than the real expiration date. That way,
65 * we use the tree as a place holder for a minorant of the real expiration
66 * date. Since we have a very low chance of hitting a timeout anyway, we can
67 * bounce the nodes to their right place when we scan the tree if we encounter
68 * a misplaced node once in a while. This even allows us not to remove the
69 * infinite timers from the wait queue.
70 *
71 * So, to summarize, we have :
72 * - node->key always defines current position in the wait queue
73 * - timer is the real expiration date (possibly infinite)
Willy Tarreaue35c94a2009-03-21 10:01:42 +010074 * - node->key is always before or equal to timer
Willy Tarreaud0a201b2009-03-08 15:53:06 +010075 *
76 * The run queue works similarly to the wait queue except that the current date
77 * is replaced by an insertion counter which can also wrap without any problem.
78 */
79
Willy Tarreaue35c94a2009-03-21 10:01:42 +010080/* The farthest we can look back in a timer tree */
81#define TIMER_LOOK_BACK (1U << 31)
Willy Tarreaud0a201b2009-03-08 15:53:06 +010082
83/* a few exported variables */
Willy Tarreaua4613182009-03-21 18:13:21 +010084extern unsigned int nb_tasks; /* total number of tasks */
Christopher Faulet34c5cc92016-12-06 09:15:30 +010085extern unsigned int tasks_run_queue; /* run queue size */
86extern unsigned int tasks_run_queue_cur;
Willy Tarreauc7bdf092009-03-21 18:33:52 +010087extern unsigned int nb_tasks_cur;
Willy Tarreau91e99932008-06-30 07:51:00 +020088extern unsigned int niced_tasks; /* number of niced tasks in the run queue */
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020089extern struct pool_head *pool2_task;
Thierry FOURNIERd6975962017-07-12 14:31:10 +020090extern struct pool_head *pool2_notification;
Emeric Brunc60def82017-09-27 14:59:38 +020091#ifdef USE_THREAD
92extern HA_SPINLOCK_T rq_lock; /* spin lock related to run queue */
93extern HA_SPINLOCK_T wq_lock; /* spin lock related to wait queue */
94#endif
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020095
Willy Tarreau4726f532009-03-07 17:25:21 +010096/* return 0 if task is in run queue, otherwise non-zero */
97static inline int task_in_rq(struct task *t)
98{
99 return t->rq.node.leaf_p != NULL;
100}
101
102/* return 0 if task is in wait queue, otherwise non-zero */
103static inline int task_in_wq(struct task *t)
104{
105 return t->wq.node.leaf_p != NULL;
106}
107
Willy Tarreaufdccded2008-08-29 18:19:04 +0200108/* puts the task <t> in run queue with reason flags <f>, and returns <t> */
Willy Tarreau4df82062008-08-29 15:26:14 +0200109struct task *__task_wakeup(struct task *t);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200110static inline struct task *task_wakeup(struct task *t, unsigned int f)
Willy Tarreau4df82062008-08-29 15:26:14 +0200111{
Emeric Brunc60def82017-09-27 14:59:38 +0200112 SPIN_LOCK(TASK_RQ_LOCK, &rq_lock);
113
Emeric Brun01948972017-03-30 15:37:25 +0200114 /* If task is running, we postpone the call
115 * and backup the state.
116 */
117 if (unlikely(t->state & TASK_RUNNING)) {
118 t->pending_state |= f;
Emeric Brunc60def82017-09-27 14:59:38 +0200119 SPIN_UNLOCK(TASK_RQ_LOCK, &rq_lock);
Emeric Brun01948972017-03-30 15:37:25 +0200120 return t;
121 }
Willy Tarreau4726f532009-03-07 17:25:21 +0100122 if (likely(!task_in_rq(t)))
Willy Tarreaufdccded2008-08-29 18:19:04 +0200123 __task_wakeup(t);
124 t->state |= f;
Emeric Brunc60def82017-09-27 14:59:38 +0200125 SPIN_UNLOCK(TASK_RQ_LOCK, &rq_lock);
126
Willy Tarreaufdccded2008-08-29 18:19:04 +0200127 return t;
Willy Tarreau4df82062008-08-29 15:26:14 +0200128}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200129
Willy Tarreauf65610a2017-10-31 16:06:06 +0100130/* change the thread affinity of a task to <thread_mask> */
Emeric Brunc60def82017-09-27 14:59:38 +0200131static inline void task_set_affinity(struct task *t, unsigned long thread_mask)
132{
Willy Tarreauf65610a2017-10-31 16:06:06 +0100133 t->thread_mask = thread_mask;
Emeric Brunc60def82017-09-27 14:59:38 +0200134}
Willy Tarreauf65610a2017-10-31 16:06:06 +0100135
Willy Tarreau4726f532009-03-07 17:25:21 +0100136/*
137 * Unlink the task from the wait queue, and possibly update the last_timer
138 * pointer. A pointer to the task itself is returned. The task *must* already
139 * be in the wait queue before calling this function. If unsure, use the safer
140 * task_unlink_wq() function.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200141 */
Willy Tarreau4726f532009-03-07 17:25:21 +0100142static inline struct task *__task_unlink_wq(struct task *t)
143{
144 eb32_delete(&t->wq);
Willy Tarreau4726f532009-03-07 17:25:21 +0100145 return t;
146}
147
148static inline struct task *task_unlink_wq(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200149{
Emeric Brunc60def82017-09-27 14:59:38 +0200150 SPIN_LOCK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreau4726f532009-03-07 17:25:21 +0100151 if (likely(task_in_wq(t)))
152 __task_unlink_wq(t);
Emeric Brunc60def82017-09-27 14:59:38 +0200153 SPIN_UNLOCK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200154 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200155}
156
157/*
Christopher Faulet34c5cc92016-12-06 09:15:30 +0100158 * Unlink the task from the run queue. The tasks_run_queue size and number of
159 * niced tasks are updated too. A pointer to the task itself is returned. The
160 * task *must* already be in the run queue before calling this function. If
161 * unsure, use the safer task_unlink_rq() function. Note that the pointer to the
162 * next run queue entry is neither checked nor updated.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200163 */
Willy Tarreau4726f532009-03-07 17:25:21 +0100164static inline struct task *__task_unlink_rq(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200165{
Willy Tarreau4726f532009-03-07 17:25:21 +0100166 eb32_delete(&t->rq);
Christopher Faulet34c5cc92016-12-06 09:15:30 +0100167 tasks_run_queue--;
Willy Tarreau4726f532009-03-07 17:25:21 +0100168 if (likely(t->nice))
169 niced_tasks--;
Willy Tarreauce44f122008-07-05 18:16:19 +0200170 return t;
171}
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200172
Willy Tarreau501260b2015-02-23 16:07:01 +0100173/* This function unlinks task <t> from the run queue if it is in it. It also
174 * takes care of updating the next run queue task if it was this task.
175 */
Willy Tarreau4726f532009-03-07 17:25:21 +0100176static inline struct task *task_unlink_rq(struct task *t)
177{
Emeric Brunc60def82017-09-27 14:59:38 +0200178 SPIN_LOCK(TASK_RQ_LOCK, &rq_lock);
179 if (likely(task_in_rq(t)))
Willy Tarreau4726f532009-03-07 17:25:21 +0100180 __task_unlink_rq(t);
Emeric Brunc60def82017-09-27 14:59:38 +0200181 SPIN_UNLOCK(TASK_RQ_LOCK, &rq_lock);
Willy Tarreau4726f532009-03-07 17:25:21 +0100182 return t;
183}
184
Willy Tarreauce44f122008-07-05 18:16:19 +0200185/*
186 * Unlinks the task and adjusts run queue stats.
187 * A pointer to the task itself is returned.
188 */
189static inline struct task *task_delete(struct task *t)
190{
Willy Tarreau4726f532009-03-07 17:25:21 +0100191 task_unlink_wq(t);
192 task_unlink_rq(t);
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200193 return t;
194}
195
196/*
Willy Tarreaua4613182009-03-21 18:13:21 +0100197 * Initialize a new task. The bare minimum is performed (queue pointers and
198 * state). The task is returned. This function should not be used outside of
199 * task_new().
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200200 */
Emeric Brunc60def82017-09-27 14:59:38 +0200201static inline struct task *task_init(struct task *t, unsigned long thread_mask)
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200202{
Willy Tarreau4726f532009-03-07 17:25:21 +0100203 t->wq.node.leaf_p = NULL;
204 t->rq.node.leaf_p = NULL;
Emeric Brun01948972017-03-30 15:37:25 +0200205 t->pending_state = t->state = TASK_SLEEPING;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100206 t->thread_mask = thread_mask;
Willy Tarreau91e99932008-06-30 07:51:00 +0200207 t->nice = 0;
Willy Tarreau3884cba2009-03-28 17:54:35 +0100208 t->calls = 0;
Willy Tarreauf4219992017-07-24 17:52:58 +0200209 t->expire = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200210 return t;
211}
212
213/*
Willy Tarreaua4613182009-03-21 18:13:21 +0100214 * Allocate and initialise a new task. The new task is returned, or NULL in
215 * case of lack of memory. The task count is incremented. Tasks should only
216 * be allocated this way, and must be freed using task_free().
217 */
Emeric Brunc60def82017-09-27 14:59:38 +0200218static inline struct task *task_new(unsigned long thread_mask)
Willy Tarreaua4613182009-03-21 18:13:21 +0100219{
220 struct task *t = pool_alloc2(pool2_task);
221 if (t) {
Emeric Brunc60def82017-09-27 14:59:38 +0200222 HA_ATOMIC_ADD(&nb_tasks, 1);
223 task_init(t, thread_mask);
Willy Tarreaua4613182009-03-21 18:13:21 +0100224 }
225 return t;
226}
227
228/*
229 * Free a task. Its context must have been freed since it will be lost.
230 * The task count is decremented.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200231 */
232static inline void task_free(struct task *t)
233{
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200234 pool_free2(pool2_task, t);
Willy Tarreaueb118892014-11-13 16:57:19 +0100235 if (unlikely(stopping))
236 pool_flush2(pool2_task);
Emeric Brunc60def82017-09-27 14:59:38 +0200237 HA_ATOMIC_SUB(&nb_tasks, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200238}
239
Willy Tarreau4726f532009-03-07 17:25:21 +0100240/* Place <task> into the wait queue, where it may already be. If the expiration
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100241 * timer is infinite, do nothing and rely on wake_expired_task to clean up.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200242 */
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100243void __task_queue(struct task *task);
244static inline void task_queue(struct task *task)
245{
246 /* If we already have a place in the wait queue no later than the
247 * timeout we're trying to set, we'll stay there, because it is very
248 * unlikely that we will reach the timeout anyway. If the timeout
249 * has been disabled, it's useless to leave the queue as well. We'll
250 * rely on wake_expired_tasks() to catch the node and move it to the
251 * proper place should it ever happen. Finally we only add the task
252 * to the queue if it was not there or if it was further than what
253 * we want.
254 */
255 if (!tick_isset(task->expire))
256 return;
257
Emeric Brunc60def82017-09-27 14:59:38 +0200258 SPIN_LOCK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100259 if (!task_in_wq(task) || tick_is_lt(task->expire, task->wq.key))
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100260 __task_queue(task);
Emeric Brunc60def82017-09-27 14:59:38 +0200261 SPIN_UNLOCK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100262}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200263
Willy Tarreau26e48812011-07-25 14:30:42 +0200264/* Ensure <task> will be woken up at most at <when>. If the task is already in
265 * the run queue (but not running), nothing is done. It may be used that way
266 * with a delay : task_schedule(task, tick_add(now_ms, delay));
267 */
268static inline void task_schedule(struct task *task, int when)
269{
Emeric Brunc60def82017-09-27 14:59:38 +0200270 /* TODO: mthread, check if there is no tisk with this test */
Willy Tarreau26e48812011-07-25 14:30:42 +0200271 if (task_in_rq(task))
272 return;
273
Emeric Brunc60def82017-09-27 14:59:38 +0200274 SPIN_LOCK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreau26e48812011-07-25 14:30:42 +0200275 if (task_in_wq(task))
276 when = tick_first(when, task->expire);
277
278 task->expire = when;
279 if (!task_in_wq(task) || tick_is_lt(task->expire, task->wq.key))
280 __task_queue(task);
Emeric Brunc60def82017-09-27 14:59:38 +0200281 SPIN_UNLOCK(TASK_WQ_LOCK, &wq_lock);
Willy Tarreau26e48812011-07-25 14:30:42 +0200282}
283
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200284/* This function register a new signal. "lua" is the current lua
285 * execution context. It contains a pointer to the associated task.
286 * "link" is a list head attached to an other task that must be wake
287 * the lua task if an event occurs. This is useful with external
288 * events like TCP I/O or sleep functions. This funcion allocate
289 * memory for the signal.
290 */
291static inline struct notification *notification_new(struct list *purge, struct list *event, struct task *wakeup)
292{
293 struct notification *com = pool_alloc2(pool2_notification);
294 if (!com)
295 return NULL;
296 LIST_ADDQ(purge, &com->purge_me);
297 LIST_ADDQ(event, &com->wake_me);
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200298 SPIN_INIT(&com->lock);
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200299 com->task = wakeup;
300 return com;
301}
302
303/* This function purge all the pending signals when the LUA execution
304 * is finished. This prevent than a coprocess try to wake a deleted
305 * task. This function remove the memory associated to the signal.
306 */
307static inline void notification_purge(struct list *purge)
308{
309 struct notification *com, *back;
310
311 /* Delete all pending communication signals. */
312 list_for_each_entry_safe(com, back, purge, purge_me) {
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200313 SPIN_LOCK(NOTIF_LOCK, &com->lock);
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200314 LIST_DEL(&com->purge_me);
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200315 if (!com->task) {
316 SPIN_UNLOCK(NOTIF_LOCK, &com->lock);
317 pool_free2(pool2_notification, com);
318 continue;
319 }
320 com->task = NULL;
321 SPIN_UNLOCK(NOTIF_LOCK, &com->lock);
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200322 }
323}
324
325/* This function sends signals. It wakes all the tasks attached
326 * to a list head, and remove the signal, and free the used
327 * memory.
328 */
329static inline void notification_wake(struct list *wake)
330{
331 struct notification *com, *back;
332
333 /* Wake task and delete all pending communication signals. */
334 list_for_each_entry_safe(com, back, wake, wake_me) {
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200335 SPIN_LOCK(NOTIF_LOCK, &com->lock);
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200336 LIST_DEL(&com->wake_me);
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200337 if (!com->task) {
338 SPIN_UNLOCK(NOTIF_LOCK, &com->lock);
339 pool_free2(pool2_notification, com);
340 continue;
341 }
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200342 task_wakeup(com->task, TASK_WOKEN_MSG);
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200343 com->task = NULL;
344 SPIN_UNLOCK(NOTIF_LOCK, &com->lock);
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200345 }
346}
347
Willy Tarreaubaaee002006-06-26 02:48:02 +0200348/*
Willy Tarreau918ff602011-07-25 16:33:49 +0200349 * This does 3 things :
Willy Tarreaubaaee002006-06-26 02:48:02 +0200350 * - wake up all expired tasks
351 * - call all runnable tasks
Willy Tarreaud825eef2007-05-12 22:35:00 +0200352 * - return the date of next event in <next> or eternity.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200353 */
354
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +0100355void process_runnable_tasks();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200356
Willy Tarreau58b458d2008-06-29 22:40:23 +0200357/*
358 * Extract all expired timers from the timer queue, and wakes up all
359 * associated tasks. Returns the date of next event (or eternity).
360 */
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +0100361int wake_expired_tasks();
Willy Tarreau58b458d2008-06-29 22:40:23 +0200362
Willy Tarreaud0a201b2009-03-08 15:53:06 +0100363/* Perform minimal initializations, report 0 in case of error, 1 if OK. */
364int init_task();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365
366#endif /* _PROTO_TASK_H */
367
368/*
369 * Local variables:
370 * c-indent-level: 8
371 * c-basic-offset: 8
372 * End:
373 */