blob: e91a26b1dc1c861adfcdd8b235071d42f43d4ad2 [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 Tarreau2dd0d472006-06-29 17:53:05 +020013#include <common/config.h>
Willy Tarreau9789f7b2008-06-24 08:17:16 +020014#include <common/eb32tree.h>
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020015#include <common/memory.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020016#include <common/mini-clist.h>
Willy Tarreau96bcfd72007-04-29 10:41:56 +020017#include <common/standard.h>
Willy Tarreaua6a6a932007-04-28 22:40:08 +020018#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020019
Willy Tarreaud825eef2007-05-12 22:35:00 +020020#include <proto/proxy.h>
Willy Tarreau531cf0c2009-03-08 16:35:27 +010021#include <proto/session.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022#include <proto/task.h>
23
Willy Tarreau9789f7b2008-06-24 08:17:16 +020024struct pool_head *pool2_task;
Willy Tarreau96bcfd72007-04-29 10:41:56 +020025
Willy Tarreau58b458d2008-06-29 22:40:23 +020026unsigned int run_queue = 0;
Willy Tarreau91e99932008-06-30 07:51:00 +020027unsigned int niced_tasks = 0; /* number of niced tasks in the run queue */
Willy Tarreauce44f122008-07-05 18:16:19 +020028struct task *last_timer = NULL; /* optimization: last queued timer */
Willy Tarreau964c9362007-01-07 00:38:00 +010029
Willy Tarreau28c41a42008-06-29 17:00:59 +020030static struct eb_root timers[TIMER_TREES]; /* trees with MSB 00, 01, 10 and 11 */
Willy Tarreau58b458d2008-06-29 22:40:23 +020031static struct eb_root rqueue[TIMER_TREES]; /* trees constituting the run queue */
32static unsigned int rqueue_ticks; /* insertion count */
Willy Tarreau9789f7b2008-06-24 08:17:16 +020033
Willy Tarreau4726f532009-03-07 17:25:21 +010034/* Puts the task <t> in run queue at a position depending on t->nice. <t> is
35 * returned. The nice value assigns boosts in 32th of the run queue size. A
36 * nice value of -1024 sets the task to -run_queue*32, while a nice value of
37 * 1024 sets the task to run_queue*32. The state flags are cleared, so the
38 * caller will have to set its flags after this call.
39 * The task must not already be in the run queue. If unsure, use the safer
40 * task_wakeup() function.
Willy Tarreau91e99932008-06-30 07:51:00 +020041 */
Willy Tarreau4df82062008-08-29 15:26:14 +020042struct task *__task_wakeup(struct task *t)
Willy Tarreaue33aece2007-04-30 13:15:14 +020043{
Willy Tarreau58b458d2008-06-29 22:40:23 +020044 run_queue++;
Willy Tarreau4726f532009-03-07 17:25:21 +010045 t->rq.key = ++rqueue_ticks;
Willy Tarreau91e99932008-06-30 07:51:00 +020046
47 if (likely(t->nice)) {
48 int offset;
49
50 niced_tasks++;
51 if (likely(t->nice > 0))
52 offset = (unsigned)((run_queue * (unsigned int)t->nice) / 32U);
53 else
54 offset = -(unsigned)((run_queue * (unsigned int)-t->nice) / 32U);
Willy Tarreau4726f532009-03-07 17:25:21 +010055 t->rq.key += offset;
Willy Tarreau91e99932008-06-30 07:51:00 +020056 }
57
Willy Tarreaufdccded2008-08-29 18:19:04 +020058 /* clear state flags at the same time */
Willy Tarreau4726f532009-03-07 17:25:21 +010059 t->state &= ~TASK_WOKEN_ANY;
Willy Tarreau58b458d2008-06-29 22:40:23 +020060
Willy Tarreaud0a201b2009-03-08 15:53:06 +010061 eb32_insert(&rqueue[timer_to_tree(t->rq.key)], &t->rq);
Willy Tarreau58b458d2008-06-29 22:40:23 +020062 return t;
Willy Tarreaue33aece2007-04-30 13:15:14 +020063}
Willy Tarreaud825eef2007-05-12 22:35:00 +020064
Willy Tarreau96bcfd72007-04-29 10:41:56 +020065/*
Willy Tarreau531cf0c2009-03-08 16:35:27 +010066 * __task_queue()
Willy Tarreau96bcfd72007-04-29 10:41:56 +020067 *
68 * Inserts a task into the wait queue at the position given by its expiration
Willy Tarreau4726f532009-03-07 17:25:21 +010069 * date. It does not matter if the task was already in the wait queue or not,
Willy Tarreau531cf0c2009-03-08 16:35:27 +010070 * as it will be unlinked. The task must not have an infinite expiration timer.
71 * Last, tasks must not be queued further than the end of the next tree, which
72 * is between <now_ms> and <now_ms> + TIMER_SIGN_BIT ms (now+12days..24days in
73 * 32bit).
74 *
75 * This function should not be used directly, it is meant to be called by the
76 * inline version of task_queue() which performs a few cheap preliminary tests
77 * before deciding to call __task_queue().
Willy Tarreau96bcfd72007-04-29 10:41:56 +020078 */
Willy Tarreau531cf0c2009-03-08 16:35:27 +010079void __task_queue(struct task *task)
Willy Tarreau964c9362007-01-07 00:38:00 +010080{
Willy Tarreau531cf0c2009-03-08 16:35:27 +010081 if (likely(task_in_wq(task)))
Willy Tarreau4726f532009-03-07 17:25:21 +010082 __task_unlink_wq(task);
Willy Tarreau4726f532009-03-07 17:25:21 +010083
84 /* the task is not in the queue now */
Willy Tarreaud0a201b2009-03-08 15:53:06 +010085 if (unlikely(!tick_isset(task->expire)))
Willy Tarreau4726f532009-03-07 17:25:21 +010086 return;
Willy Tarreau96bcfd72007-04-29 10:41:56 +020087
Willy Tarreaud0a201b2009-03-08 15:53:06 +010088 task->wq.key = tick_to_timer(task->expire);
Willy Tarreau28c41a42008-06-29 17:00:59 +020089#ifdef DEBUG_CHECK_INVALID_EXPIRATION_DATES
Willy Tarreaud0a201b2009-03-08 15:53:06 +010090 if ((task->wq.key - tick_to_timer(now_ms)) & TIMER_SIGN_BIT)
Willy Tarreau28c41a42008-06-29 17:00:59 +020091 /* we're queuing too far away or in the past (most likely) */
Willy Tarreau4726f532009-03-07 17:25:21 +010092 return;
Willy Tarreau28c41a42008-06-29 17:00:59 +020093#endif
Willy Tarreauce44f122008-07-05 18:16:19 +020094
95 if (likely(last_timer &&
Willy Tarreau4726f532009-03-07 17:25:21 +010096 last_timer->wq.key == task->wq.key &&
Willy Tarreau531cf0c2009-03-08 16:35:27 +010097 last_timer->wq.node.bit == -1 &&
98 last_timer->wq.node.node_p)) {
Willy Tarreauce44f122008-07-05 18:16:19 +020099 /* Most often, last queued timer has the same expiration date, so
100 * if it's not queued at the root, let's queue a dup directly there.
Willy Tarreau1b8ca662009-03-08 00:26:28 +0100101 * Note that we can only use dups at the dup tree's root (bit==-1).
Willy Tarreauce44f122008-07-05 18:16:19 +0200102 */
Willy Tarreau4726f532009-03-07 17:25:21 +0100103 eb_insert_dup(&last_timer->wq.node, &task->wq.node);
104 return;
Willy Tarreauce44f122008-07-05 18:16:19 +0200105 }
Willy Tarreaud0a201b2009-03-08 15:53:06 +0100106 eb32_insert(&timers[timer_to_tree(task->wq.key)], &task->wq);
Willy Tarreau4726f532009-03-07 17:25:21 +0100107 if (task->wq.node.bit == -1)
Willy Tarreau1b8ca662009-03-08 00:26:28 +0100108 last_timer = task; /* we only want dup a tree's root */
Willy Tarreau4726f532009-03-07 17:25:21 +0100109 return;
Willy Tarreau964c9362007-01-07 00:38:00 +0100110}
111
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200112/*
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200113 * Extract all expired timers from the timer queue, and wakes up all
Willy Tarreaud825eef2007-05-12 22:35:00 +0200114 * associated tasks. Returns the date of next event (or eternity).
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200115 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200116void wake_expired_tasks(int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117{
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200118 struct task *task;
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200119 struct eb32_node *eb;
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200120 unsigned int now_tree;
Willy Tarreau28c41a42008-06-29 17:00:59 +0200121 unsigned int tree;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122
Willy Tarreau28c41a42008-06-29 17:00:59 +0200123 /* In theory, we should :
124 * - wake all tasks from the <previous> tree
125 * - wake all expired tasks from the <current> tree
126 * - scan <next> trees for next expiration date if not found earlier.
127 * But we can do all this more easily : we scan all 3 trees before we
128 * wrap, and wake everything expired from there, then stop on the first
129 * non-expired entry.
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200130 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200131
Willy Tarreaud0a201b2009-03-08 15:53:06 +0100132 now_tree = timer_to_tree(tick_to_timer(now_ms));
Willy Tarreau28c41a42008-06-29 17:00:59 +0200133 tree = (now_tree - 1) & TIMER_TREE_MASK;
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200134 do {
Willy Tarreau28c41a42008-06-29 17:00:59 +0200135 eb = eb32_first(&timers[tree]);
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200136 while (eb) {
Willy Tarreau4726f532009-03-07 17:25:21 +0100137 task = eb32_entry(eb, struct task, wq);
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100138 if (likely((tick_to_timer(now_ms) - eb->key) & TIMER_SIGN_BIT)) {
Willy Tarreau28c41a42008-06-29 17:00:59 +0200139 /* note that we don't need this check for the <previous>
140 * tree, but it's cheaper than duplicating the code.
141 */
Willy Tarreaud0a201b2009-03-08 15:53:06 +0100142 *next = timer_to_tick(eb->key);
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200143 return;
144 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200145
Willy Tarreauaf754fc2008-06-29 19:25:52 +0200146 /* detach the task from the queue and add the task to the run queue */
147 eb = eb32_next(eb);
Willy Tarreau4726f532009-03-07 17:25:21 +0100148 __task_unlink_wq(task);
Willy Tarreau41365222009-03-08 07:46:27 +0100149
150 /* It is possible that this task was left at an earlier place in the
151 * tree because a recent call to task_queue() has not moved it. This
152 * happens when the new expiration date is later than the old one.
153 * Since it is very unlikely that we reach a timeout anyway, it's a
154 * lot cheaper to proceed like this because we almost never update
155 * the tree. We may also find disabled expiration dates there. Since
156 * we have detached the task from the tree, we simply call task_queue
157 * to take care of this.
158 */
159 if (!tick_is_expired(task->expire, now_ms)) {
160 task_queue(task);
161 continue;
162 }
Willy Tarreau4726f532009-03-07 17:25:21 +0100163 task_wakeup(task, TASK_WOKEN_TIMER);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200164 }
Willy Tarreau28c41a42008-06-29 17:00:59 +0200165 tree = (tree + 1) & TIMER_TREE_MASK;
166 } while (((tree - now_tree) & TIMER_TREE_MASK) < TIMER_TREES/2);
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200167
Willy Tarreau28c41a42008-06-29 17:00:59 +0200168 /* We have found no task to expire in any tree */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200169 *next = TICK_ETERNITY;
Willy Tarreau28c41a42008-06-29 17:00:59 +0200170 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200171}
172
Willy Tarreau58b458d2008-06-29 22:40:23 +0200173/* The run queue is chronologically sorted in a tree. An insertion counter is
174 * used to assign a position to each task. This counter may be combined with
175 * other variables (eg: nice value) to set the final position in the tree. The
176 * counter may wrap without a problem, of course. We then limit the number of
Willy Tarreau91e99932008-06-30 07:51:00 +0200177 * tasks processed at once to 1/4 of the number of tasks in the queue, and to
178 * 200 max in any case, so that general latency remains low and so that task
179 * positions have a chance to be considered. It also reduces the number of
180 * trees to be evaluated when no task remains.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200181 *
Willy Tarreau58b458d2008-06-29 22:40:23 +0200182 * Just like with timers, we start with tree[(current - 1)], which holds past
183 * values, and stop when we reach the middle of the list. In practise, we visit
184 * 3 out of 4 trees.
185 *
186 * The function adjusts <next> if a new event is closer.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200187 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200188void process_runnable_tasks(int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189{
Willy Tarreau964c9362007-01-07 00:38:00 +0100190 struct task *t;
Willy Tarreau58b458d2008-06-29 22:40:23 +0200191 struct eb32_node *eb;
192 unsigned int tree, stop;
193 unsigned int max_processed;
Willy Tarreau26c25062009-03-08 09:38:41 +0100194 int expire;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200195
Willy Tarreau58b458d2008-06-29 22:40:23 +0200196 if (!run_queue)
197 return;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200198
Willy Tarreau91e99932008-06-30 07:51:00 +0200199 max_processed = run_queue;
200 if (max_processed > 200)
201 max_processed = 200;
202
203 if (likely(niced_tasks))
204 max_processed /= 4;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200205
Willy Tarreaud0a201b2009-03-08 15:53:06 +0100206 tree = timer_to_tree(rqueue_ticks);
Willy Tarreau58b458d2008-06-29 22:40:23 +0200207 stop = (tree + TIMER_TREES / 2) & TIMER_TREE_MASK;
208 tree = (tree - 1) & TIMER_TREE_MASK;
Willy Tarreau964c9362007-01-07 00:38:00 +0100209
Willy Tarreau26c25062009-03-08 09:38:41 +0100210 expire = *next;
Willy Tarreau58b458d2008-06-29 22:40:23 +0200211 do {
212 eb = eb32_first(&rqueue[tree]);
213 while (eb) {
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100214 /* Note: this loop is one of the fastest code path in
215 * the whole program. It should not be re-arranged
216 * without a good reason.
217 */
Willy Tarreau4726f532009-03-07 17:25:21 +0100218 t = eb32_entry(eb, struct task, rq);
Willy Tarreau58b458d2008-06-29 22:40:23 +0200219
220 /* detach the task from the queue and add the task to the run queue */
221 eb = eb32_next(eb);
Willy Tarreau4726f532009-03-07 17:25:21 +0100222 __task_unlink_rq(t);
Willy Tarreau58b458d2008-06-29 22:40:23 +0200223
Willy Tarreau4726f532009-03-07 17:25:21 +0100224 t->state |= TASK_RUNNING;
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100225 /* This is an optimisation to help the processor's branch
226 * predictor take this most common call.
227 */
228 if (likely(t->process == process_session))
229 t = process_session(t);
230 else
231 t = t->process(t);
232
233 if (likely(t != NULL)) {
Willy Tarreau26c25062009-03-08 09:38:41 +0100234 t->state &= ~TASK_RUNNING;
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100235 if (t->expire) {
236 task_queue(t);
237 expire = tick_first_2nz(expire, t->expire);
238 }
Willy Tarreau26c25062009-03-08 09:38:41 +0100239 }
Willy Tarreau58b458d2008-06-29 22:40:23 +0200240
241 if (!--max_processed)
Willy Tarreau26c25062009-03-08 09:38:41 +0100242 goto out;
Willy Tarreau58b458d2008-06-29 22:40:23 +0200243 }
244 tree = (tree + 1) & TIMER_TREE_MASK;
245 } while (tree != stop);
Willy Tarreau26c25062009-03-08 09:38:41 +0100246 out:
247 *next = expire;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200248}
249
Willy Tarreau4726f532009-03-07 17:25:21 +0100250/* perform minimal intializations, report 0 in case of error, 1 if OK. */
251int init_task()
252{
253 memset(&timers, 0, sizeof(timers));
254 memset(&rqueue, 0, sizeof(rqueue));
255 pool2_task = create_pool("task", sizeof(struct task), MEM_F_SHARED);
256 return pool2_task != NULL;
257}
258
Willy Tarreaubaaee002006-06-26 02:48:02 +0200259/*
260 * Local variables:
261 * c-indent-level: 8
262 * c-basic-offset: 8
263 * End:
264 */