blob: 6f7b26406626d5816f2bf75d1ad34251cdc238c6 [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 Tarreau2dd0d472006-06-29 17:53:05 +020015#include <common/config.h>
Willy Tarreau9789f7b2008-06-24 08:17:16 +020016#include <common/eb32tree.h>
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020017#include <common/memory.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020018#include <common/mini-clist.h>
Willy Tarreau96bcfd72007-04-29 10:41:56 +020019#include <common/standard.h>
Willy Tarreaua6a6a932007-04-28 22:40:08 +020020#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreaud825eef2007-05-12 22:35:00 +020022#include <proto/proxy.h>
Willy Tarreau531cf0c2009-03-08 16:35:27 +010023#include <proto/session.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024#include <proto/task.h>
25
Willy Tarreau9789f7b2008-06-24 08:17:16 +020026struct pool_head *pool2_task;
Willy Tarreau96bcfd72007-04-29 10:41:56 +020027
Willy Tarreau58b458d2008-06-29 22:40:23 +020028unsigned int run_queue = 0;
Willy Tarreau91e99932008-06-30 07:51:00 +020029unsigned int niced_tasks = 0; /* number of niced tasks in the run queue */
Willy Tarreauce44f122008-07-05 18:16:19 +020030struct task *last_timer = NULL; /* optimization: last queued timer */
Willy Tarreau964c9362007-01-07 00:38:00 +010031
Willy Tarreau28c41a42008-06-29 17:00:59 +020032static struct eb_root timers[TIMER_TREES]; /* trees with MSB 00, 01, 10 and 11 */
Willy Tarreau58b458d2008-06-29 22:40:23 +020033static struct eb_root rqueue[TIMER_TREES]; /* trees constituting the run queue */
34static unsigned int rqueue_ticks; /* insertion count */
Willy Tarreau9789f7b2008-06-24 08:17:16 +020035
Willy Tarreau4726f532009-03-07 17:25:21 +010036/* Puts the task <t> in run queue at a position depending on t->nice. <t> is
37 * returned. The nice value assigns boosts in 32th of the run queue size. A
38 * nice value of -1024 sets the task to -run_queue*32, while a nice value of
39 * 1024 sets the task to run_queue*32. The state flags are cleared, so the
40 * caller will have to set its flags after this call.
41 * The task must not already be in the run queue. If unsure, use the safer
42 * task_wakeup() function.
Willy Tarreau91e99932008-06-30 07:51:00 +020043 */
Willy Tarreau4df82062008-08-29 15:26:14 +020044struct task *__task_wakeup(struct task *t)
Willy Tarreaue33aece2007-04-30 13:15:14 +020045{
Willy Tarreau58b458d2008-06-29 22:40:23 +020046 run_queue++;
Willy Tarreau4726f532009-03-07 17:25:21 +010047 t->rq.key = ++rqueue_ticks;
Willy Tarreau91e99932008-06-30 07:51:00 +020048
49 if (likely(t->nice)) {
50 int offset;
51
52 niced_tasks++;
53 if (likely(t->nice > 0))
54 offset = (unsigned)((run_queue * (unsigned int)t->nice) / 32U);
55 else
56 offset = -(unsigned)((run_queue * (unsigned int)-t->nice) / 32U);
Willy Tarreau4726f532009-03-07 17:25:21 +010057 t->rq.key += offset;
Willy Tarreau91e99932008-06-30 07:51:00 +020058 }
59
Willy Tarreaufdccded2008-08-29 18:19:04 +020060 /* clear state flags at the same time */
Willy Tarreau4726f532009-03-07 17:25:21 +010061 t->state &= ~TASK_WOKEN_ANY;
Willy Tarreau58b458d2008-06-29 22:40:23 +020062
Willy Tarreaud0a201b2009-03-08 15:53:06 +010063 eb32_insert(&rqueue[timer_to_tree(t->rq.key)], &t->rq);
Willy Tarreau58b458d2008-06-29 22:40:23 +020064 return t;
Willy Tarreaue33aece2007-04-30 13:15:14 +020065}
Willy Tarreaud825eef2007-05-12 22:35:00 +020066
Willy Tarreau96bcfd72007-04-29 10:41:56 +020067/*
Willy Tarreau531cf0c2009-03-08 16:35:27 +010068 * __task_queue()
Willy Tarreau96bcfd72007-04-29 10:41:56 +020069 *
70 * Inserts a task into the wait queue at the position given by its expiration
Willy Tarreau4726f532009-03-07 17:25:21 +010071 * date. It does not matter if the task was already in the wait queue or not,
Willy Tarreau531cf0c2009-03-08 16:35:27 +010072 * as it will be unlinked. The task must not have an infinite expiration timer.
73 * Last, tasks must not be queued further than the end of the next tree, which
74 * is between <now_ms> and <now_ms> + TIMER_SIGN_BIT ms (now+12days..24days in
75 * 32bit).
76 *
77 * This function should not be used directly, it is meant to be called by the
78 * inline version of task_queue() which performs a few cheap preliminary tests
79 * before deciding to call __task_queue().
Willy Tarreau96bcfd72007-04-29 10:41:56 +020080 */
Willy Tarreau531cf0c2009-03-08 16:35:27 +010081void __task_queue(struct task *task)
Willy Tarreau964c9362007-01-07 00:38:00 +010082{
Willy Tarreau531cf0c2009-03-08 16:35:27 +010083 if (likely(task_in_wq(task)))
Willy Tarreau4726f532009-03-07 17:25:21 +010084 __task_unlink_wq(task);
Willy Tarreau4726f532009-03-07 17:25:21 +010085
86 /* the task is not in the queue now */
Willy Tarreaud0a201b2009-03-08 15:53:06 +010087 if (unlikely(!tick_isset(task->expire)))
Willy Tarreau4726f532009-03-07 17:25:21 +010088 return;
Willy Tarreau96bcfd72007-04-29 10:41:56 +020089
Willy Tarreaud0a201b2009-03-08 15:53:06 +010090 task->wq.key = tick_to_timer(task->expire);
Willy Tarreau28c41a42008-06-29 17:00:59 +020091#ifdef DEBUG_CHECK_INVALID_EXPIRATION_DATES
Willy Tarreaud0a201b2009-03-08 15:53:06 +010092 if ((task->wq.key - tick_to_timer(now_ms)) & TIMER_SIGN_BIT)
Willy Tarreau28c41a42008-06-29 17:00:59 +020093 /* we're queuing too far away or in the past (most likely) */
Willy Tarreau4726f532009-03-07 17:25:21 +010094 return;
Willy Tarreau28c41a42008-06-29 17:00:59 +020095#endif
Willy Tarreauce44f122008-07-05 18:16:19 +020096
97 if (likely(last_timer &&
Willy Tarreau4726f532009-03-07 17:25:21 +010098 last_timer->wq.key == task->wq.key &&
Willy Tarreau531cf0c2009-03-08 16:35:27 +010099 last_timer->wq.node.bit == -1 &&
100 last_timer->wq.node.node_p)) {
Willy Tarreauce44f122008-07-05 18:16:19 +0200101 /* Most often, last queued timer has the same expiration date, so
102 * if it's not queued at the root, let's queue a dup directly there.
Willy Tarreau1b8ca662009-03-08 00:26:28 +0100103 * Note that we can only use dups at the dup tree's root (bit==-1).
Willy Tarreauce44f122008-07-05 18:16:19 +0200104 */
Willy Tarreau4726f532009-03-07 17:25:21 +0100105 eb_insert_dup(&last_timer->wq.node, &task->wq.node);
106 return;
Willy Tarreauce44f122008-07-05 18:16:19 +0200107 }
Willy Tarreaud0a201b2009-03-08 15:53:06 +0100108 eb32_insert(&timers[timer_to_tree(task->wq.key)], &task->wq);
Willy Tarreau4726f532009-03-07 17:25:21 +0100109 if (task->wq.node.bit == -1)
Willy Tarreau1b8ca662009-03-08 00:26:28 +0100110 last_timer = task; /* we only want dup a tree's root */
Willy Tarreau4726f532009-03-07 17:25:21 +0100111 return;
Willy Tarreau964c9362007-01-07 00:38:00 +0100112}
113
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200114/*
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200115 * Extract all expired timers from the timer queue, and wakes up all
Willy Tarreaud825eef2007-05-12 22:35:00 +0200116 * associated tasks. Returns the date of next event (or eternity).
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200117 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200118void wake_expired_tasks(int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119{
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200120 struct task *task;
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200121 struct eb32_node *eb;
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200122 unsigned int now_tree;
Willy Tarreau28c41a42008-06-29 17:00:59 +0200123 unsigned int tree;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124
Willy Tarreau28c41a42008-06-29 17:00:59 +0200125 /* In theory, we should :
126 * - wake all tasks from the <previous> tree
127 * - wake all expired tasks from the <current> tree
128 * - scan <next> trees for next expiration date if not found earlier.
129 * But we can do all this more easily : we scan all 3 trees before we
130 * wrap, and wake everything expired from there, then stop on the first
131 * non-expired entry.
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200132 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200133
Willy Tarreaud0a201b2009-03-08 15:53:06 +0100134 now_tree = timer_to_tree(tick_to_timer(now_ms));
Willy Tarreau28c41a42008-06-29 17:00:59 +0200135 tree = (now_tree - 1) & TIMER_TREE_MASK;
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200136 do {
Willy Tarreau28c41a42008-06-29 17:00:59 +0200137 eb = eb32_first(&timers[tree]);
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200138 while (eb) {
Willy Tarreau4726f532009-03-07 17:25:21 +0100139 task = eb32_entry(eb, struct task, wq);
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100140 if (likely((tick_to_timer(now_ms) - eb->key) & TIMER_SIGN_BIT)) {
Willy Tarreau28c41a42008-06-29 17:00:59 +0200141 /* note that we don't need this check for the <previous>
142 * tree, but it's cheaper than duplicating the code.
143 */
Willy Tarreaud0a201b2009-03-08 15:53:06 +0100144 *next = timer_to_tick(eb->key);
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200145 return;
146 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200147
Willy Tarreauaf754fc2008-06-29 19:25:52 +0200148 /* detach the task from the queue and add the task to the run queue */
149 eb = eb32_next(eb);
Willy Tarreau4726f532009-03-07 17:25:21 +0100150 __task_unlink_wq(task);
Willy Tarreau41365222009-03-08 07:46:27 +0100151
152 /* It is possible that this task was left at an earlier place in the
153 * tree because a recent call to task_queue() has not moved it. This
154 * happens when the new expiration date is later than the old one.
155 * Since it is very unlikely that we reach a timeout anyway, it's a
156 * lot cheaper to proceed like this because we almost never update
157 * the tree. We may also find disabled expiration dates there. Since
158 * we have detached the task from the tree, we simply call task_queue
159 * to take care of this.
160 */
161 if (!tick_is_expired(task->expire, now_ms)) {
162 task_queue(task);
163 continue;
164 }
Willy Tarreau4726f532009-03-07 17:25:21 +0100165 task_wakeup(task, TASK_WOKEN_TIMER);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200166 }
Willy Tarreau28c41a42008-06-29 17:00:59 +0200167 tree = (tree + 1) & TIMER_TREE_MASK;
168 } while (((tree - now_tree) & TIMER_TREE_MASK) < TIMER_TREES/2);
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200169
Willy Tarreau28c41a42008-06-29 17:00:59 +0200170 /* We have found no task to expire in any tree */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200171 *next = TICK_ETERNITY;
Willy Tarreau28c41a42008-06-29 17:00:59 +0200172 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200173}
174
Willy Tarreau58b458d2008-06-29 22:40:23 +0200175/* The run queue is chronologically sorted in a tree. An insertion counter is
176 * used to assign a position to each task. This counter may be combined with
177 * other variables (eg: nice value) to set the final position in the tree. The
178 * counter may wrap without a problem, of course. We then limit the number of
Willy Tarreau91e99932008-06-30 07:51:00 +0200179 * tasks processed at once to 1/4 of the number of tasks in the queue, and to
180 * 200 max in any case, so that general latency remains low and so that task
181 * positions have a chance to be considered. It also reduces the number of
182 * trees to be evaluated when no task remains.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200183 *
Willy Tarreau58b458d2008-06-29 22:40:23 +0200184 * Just like with timers, we start with tree[(current - 1)], which holds past
185 * values, and stop when we reach the middle of the list. In practise, we visit
186 * 3 out of 4 trees.
187 *
188 * The function adjusts <next> if a new event is closer.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200190void process_runnable_tasks(int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200191{
Willy Tarreau964c9362007-01-07 00:38:00 +0100192 struct task *t;
Willy Tarreau58b458d2008-06-29 22:40:23 +0200193 struct eb32_node *eb;
194 unsigned int tree, stop;
195 unsigned int max_processed;
Willy Tarreau26c25062009-03-08 09:38:41 +0100196 int expire;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200197
Willy Tarreau58b458d2008-06-29 22:40:23 +0200198 if (!run_queue)
199 return;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200200
Willy Tarreau91e99932008-06-30 07:51:00 +0200201 max_processed = run_queue;
202 if (max_processed > 200)
203 max_processed = 200;
204
205 if (likely(niced_tasks))
206 max_processed /= 4;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200207
Willy Tarreaud0a201b2009-03-08 15:53:06 +0100208 tree = timer_to_tree(rqueue_ticks);
Willy Tarreau58b458d2008-06-29 22:40:23 +0200209 stop = (tree + TIMER_TREES / 2) & TIMER_TREE_MASK;
210 tree = (tree - 1) & TIMER_TREE_MASK;
Willy Tarreau964c9362007-01-07 00:38:00 +0100211
Willy Tarreau26c25062009-03-08 09:38:41 +0100212 expire = *next;
Willy Tarreau58b458d2008-06-29 22:40:23 +0200213 do {
214 eb = eb32_first(&rqueue[tree]);
215 while (eb) {
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100216 /* Note: this loop is one of the fastest code path in
217 * the whole program. It should not be re-arranged
218 * without a good reason.
219 */
Willy Tarreau4726f532009-03-07 17:25:21 +0100220 t = eb32_entry(eb, struct task, rq);
Willy Tarreau58b458d2008-06-29 22:40:23 +0200221
222 /* detach the task from the queue and add the task to the run queue */
223 eb = eb32_next(eb);
Willy Tarreau4726f532009-03-07 17:25:21 +0100224 __task_unlink_rq(t);
Willy Tarreau58b458d2008-06-29 22:40:23 +0200225
Willy Tarreau4726f532009-03-07 17:25:21 +0100226 t->state |= TASK_RUNNING;
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100227 /* This is an optimisation to help the processor's branch
228 * predictor take this most common call.
229 */
230 if (likely(t->process == process_session))
231 t = process_session(t);
232 else
233 t = t->process(t);
234
235 if (likely(t != NULL)) {
Willy Tarreau26c25062009-03-08 09:38:41 +0100236 t->state &= ~TASK_RUNNING;
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100237 if (t->expire) {
238 task_queue(t);
239 expire = tick_first_2nz(expire, t->expire);
240 }
Willy Tarreau26c25062009-03-08 09:38:41 +0100241 }
Willy Tarreau58b458d2008-06-29 22:40:23 +0200242
243 if (!--max_processed)
Willy Tarreau26c25062009-03-08 09:38:41 +0100244 goto out;
Willy Tarreau58b458d2008-06-29 22:40:23 +0200245 }
246 tree = (tree + 1) & TIMER_TREE_MASK;
247 } while (tree != stop);
Willy Tarreau26c25062009-03-08 09:38:41 +0100248 out:
249 *next = expire;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200250}
251
Willy Tarreau4726f532009-03-07 17:25:21 +0100252/* perform minimal intializations, report 0 in case of error, 1 if OK. */
253int init_task()
254{
255 memset(&timers, 0, sizeof(timers));
256 memset(&rqueue, 0, sizeof(rqueue));
257 pool2_task = create_pool("task", sizeof(struct task), MEM_F_SHARED);
258 return pool2_task != NULL;
259}
260
Willy Tarreaubaaee002006-06-26 02:48:02 +0200261/*
262 * Local variables:
263 * c-indent-level: 8
264 * c-basic-offset: 8
265 * End:
266 */