blob: f054e3170749469c60e9a292feecca166ba01ed4 [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 Tarreaubaaee002006-06-26 02:48:02 +020021#include <proto/task.h>
22
Willy Tarreau9789f7b2008-06-24 08:17:16 +020023struct pool_head *pool2_task;
Willy Tarreau96bcfd72007-04-29 10:41:56 +020024
Willy Tarreau58b458d2008-06-29 22:40:23 +020025unsigned int run_queue = 0;
Willy Tarreau91e99932008-06-30 07:51:00 +020026unsigned int niced_tasks = 0; /* number of niced tasks in the run queue */
Willy Tarreauce44f122008-07-05 18:16:19 +020027struct task *last_timer = NULL; /* optimization: last queued timer */
Willy Tarreau964c9362007-01-07 00:38:00 +010028
Willy Tarreau28c41a42008-06-29 17:00:59 +020029static struct eb_root timers[TIMER_TREES]; /* trees with MSB 00, 01, 10 and 11 */
Willy Tarreau58b458d2008-06-29 22:40:23 +020030static struct eb_root rqueue[TIMER_TREES]; /* trees constituting the run queue */
31static unsigned int rqueue_ticks; /* insertion count */
Willy Tarreau9789f7b2008-06-24 08:17:16 +020032
Willy Tarreau4726f532009-03-07 17:25:21 +010033/* Puts the task <t> in run queue at a position depending on t->nice. <t> is
34 * returned. The nice value assigns boosts in 32th of the run queue size. A
35 * nice value of -1024 sets the task to -run_queue*32, while a nice value of
36 * 1024 sets the task to run_queue*32. The state flags are cleared, so the
37 * caller will have to set its flags after this call.
38 * The task must not already be in the run queue. If unsure, use the safer
39 * task_wakeup() function.
Willy Tarreau91e99932008-06-30 07:51:00 +020040 */
Willy Tarreau4df82062008-08-29 15:26:14 +020041struct task *__task_wakeup(struct task *t)
Willy Tarreaue33aece2007-04-30 13:15:14 +020042{
Willy Tarreau58b458d2008-06-29 22:40:23 +020043 run_queue++;
Willy Tarreau4726f532009-03-07 17:25:21 +010044 t->rq.key = ++rqueue_ticks;
Willy Tarreau91e99932008-06-30 07:51:00 +020045
46 if (likely(t->nice)) {
47 int offset;
48
49 niced_tasks++;
50 if (likely(t->nice > 0))
51 offset = (unsigned)((run_queue * (unsigned int)t->nice) / 32U);
52 else
53 offset = -(unsigned)((run_queue * (unsigned int)-t->nice) / 32U);
Willy Tarreau4726f532009-03-07 17:25:21 +010054 t->rq.key += offset;
Willy Tarreau91e99932008-06-30 07:51:00 +020055 }
56
Willy Tarreaufdccded2008-08-29 18:19:04 +020057 /* clear state flags at the same time */
Willy Tarreau4726f532009-03-07 17:25:21 +010058 t->state &= ~TASK_WOKEN_ANY;
Willy Tarreau58b458d2008-06-29 22:40:23 +020059
Willy Tarreaud0a201b2009-03-08 15:53:06 +010060 eb32_insert(&rqueue[timer_to_tree(t->rq.key)], &t->rq);
Willy Tarreau58b458d2008-06-29 22:40:23 +020061 return t;
Willy Tarreaue33aece2007-04-30 13:15:14 +020062}
Willy Tarreaud825eef2007-05-12 22:35:00 +020063
Willy Tarreau96bcfd72007-04-29 10:41:56 +020064/*
65 * task_queue()
66 *
67 * Inserts a task into the wait queue at the position given by its expiration
Willy Tarreau4726f532009-03-07 17:25:21 +010068 * date. It does not matter if the task was already in the wait queue or not,
69 * and it may even help if its position has not changed because we'll be able
70 * to return without doing anything. Tasks queued with an eternity expiration
71 * are just unlinked from the WQ. Last, tasks must not be queued further than
72 * the end of the next tree, which is between <now_ms> and <now_ms> +
73 * TIMER_SIGN_BIT ms (now+12days..24days in 32bit).
Willy Tarreau96bcfd72007-04-29 10:41:56 +020074 */
Willy Tarreau4726f532009-03-07 17:25:21 +010075void task_queue(struct task *task)
Willy Tarreau964c9362007-01-07 00:38:00 +010076{
Willy Tarreau4726f532009-03-07 17:25:21 +010077 /* if the task is already in the wait queue, we may reuse its position
78 * or we will at least have to unlink it first.
79 */
80 if (task_in_wq(task)) {
Willy Tarreau41365222009-03-08 07:46:27 +010081 /* If we already have a place in the wait queue no later than the
82 * timeout we're trying to set, we'll stay there, because it is very
83 * unlikely that we will reach the timeout anyway. If the timeout
84 * has been disabled, it's useless to leave the queue as well. We'll
85 * rely on wake_expired_tasks() to catch the node and move it to the
86 * proper place should it ever happen.
87 */
Willy Tarreaud0a201b2009-03-08 15:53:06 +010088 if (!tick_isset(task->expire) ||
89 ((task->wq.key - tick_to_timer(task->expire)) & TIMER_SIGN_BIT))
Willy Tarreau4726f532009-03-07 17:25:21 +010090 return;
91 __task_unlink_wq(task);
92 }
93
94 /* the task is not in the queue now */
Willy Tarreaud0a201b2009-03-08 15:53:06 +010095 if (unlikely(!tick_isset(task->expire)))
Willy Tarreau4726f532009-03-07 17:25:21 +010096 return;
Willy Tarreau96bcfd72007-04-29 10:41:56 +020097
Willy Tarreaud0a201b2009-03-08 15:53:06 +010098 task->wq.key = tick_to_timer(task->expire);
Willy Tarreau28c41a42008-06-29 17:00:59 +020099#ifdef DEBUG_CHECK_INVALID_EXPIRATION_DATES
Willy Tarreaud0a201b2009-03-08 15:53:06 +0100100 if ((task->wq.key - tick_to_timer(now_ms)) & TIMER_SIGN_BIT)
Willy Tarreau28c41a42008-06-29 17:00:59 +0200101 /* we're queuing too far away or in the past (most likely) */
Willy Tarreau4726f532009-03-07 17:25:21 +0100102 return;
Willy Tarreau28c41a42008-06-29 17:00:59 +0200103#endif
Willy Tarreauce44f122008-07-05 18:16:19 +0200104
105 if (likely(last_timer &&
Willy Tarreau4726f532009-03-07 17:25:21 +0100106 last_timer->wq.key == task->wq.key &&
107 last_timer->wq.node.node_p &&
108 last_timer->wq.node.bit == -1)) {
Willy Tarreauce44f122008-07-05 18:16:19 +0200109 /* Most often, last queued timer has the same expiration date, so
110 * if it's not queued at the root, let's queue a dup directly there.
Willy Tarreau1b8ca662009-03-08 00:26:28 +0100111 * Note that we can only use dups at the dup tree's root (bit==-1).
Willy Tarreauce44f122008-07-05 18:16:19 +0200112 */
Willy Tarreau4726f532009-03-07 17:25:21 +0100113 eb_insert_dup(&last_timer->wq.node, &task->wq.node);
114 return;
Willy Tarreauce44f122008-07-05 18:16:19 +0200115 }
Willy Tarreaud0a201b2009-03-08 15:53:06 +0100116 eb32_insert(&timers[timer_to_tree(task->wq.key)], &task->wq);
Willy Tarreau4726f532009-03-07 17:25:21 +0100117 if (task->wq.node.bit == -1)
Willy Tarreau1b8ca662009-03-08 00:26:28 +0100118 last_timer = task; /* we only want dup a tree's root */
Willy Tarreau4726f532009-03-07 17:25:21 +0100119 return;
Willy Tarreau964c9362007-01-07 00:38:00 +0100120}
121
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200122/*
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200123 * Extract all expired timers from the timer queue, and wakes up all
Willy Tarreaud825eef2007-05-12 22:35:00 +0200124 * associated tasks. Returns the date of next event (or eternity).
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200125 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200126void wake_expired_tasks(int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200127{
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200128 struct task *task;
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200129 struct eb32_node *eb;
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200130 unsigned int now_tree;
Willy Tarreau28c41a42008-06-29 17:00:59 +0200131 unsigned int tree;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200132
Willy Tarreau28c41a42008-06-29 17:00:59 +0200133 /* In theory, we should :
134 * - wake all tasks from the <previous> tree
135 * - wake all expired tasks from the <current> tree
136 * - scan <next> trees for next expiration date if not found earlier.
137 * But we can do all this more easily : we scan all 3 trees before we
138 * wrap, and wake everything expired from there, then stop on the first
139 * non-expired entry.
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200140 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200141
Willy Tarreaud0a201b2009-03-08 15:53:06 +0100142 now_tree = timer_to_tree(tick_to_timer(now_ms));
Willy Tarreau28c41a42008-06-29 17:00:59 +0200143 tree = (now_tree - 1) & TIMER_TREE_MASK;
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200144 do {
Willy Tarreau28c41a42008-06-29 17:00:59 +0200145 eb = eb32_first(&timers[tree]);
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200146 while (eb) {
Willy Tarreau4726f532009-03-07 17:25:21 +0100147 task = eb32_entry(eb, struct task, wq);
Willy Tarreaud0a201b2009-03-08 15:53:06 +0100148 if ((tick_to_timer(now_ms) - eb->key) & TIMER_SIGN_BIT) {
Willy Tarreau28c41a42008-06-29 17:00:59 +0200149 /* note that we don't need this check for the <previous>
150 * tree, but it's cheaper than duplicating the code.
151 */
Willy Tarreaud0a201b2009-03-08 15:53:06 +0100152 *next = timer_to_tick(eb->key);
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200153 return;
154 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200155
Willy Tarreauaf754fc2008-06-29 19:25:52 +0200156 /* detach the task from the queue and add the task to the run queue */
157 eb = eb32_next(eb);
Willy Tarreau4726f532009-03-07 17:25:21 +0100158 __task_unlink_wq(task);
Willy Tarreau41365222009-03-08 07:46:27 +0100159
160 /* It is possible that this task was left at an earlier place in the
161 * tree because a recent call to task_queue() has not moved it. This
162 * happens when the new expiration date is later than the old one.
163 * Since it is very unlikely that we reach a timeout anyway, it's a
164 * lot cheaper to proceed like this because we almost never update
165 * the tree. We may also find disabled expiration dates there. Since
166 * we have detached the task from the tree, we simply call task_queue
167 * to take care of this.
168 */
169 if (!tick_is_expired(task->expire, now_ms)) {
170 task_queue(task);
171 continue;
172 }
Willy Tarreau4726f532009-03-07 17:25:21 +0100173 task_wakeup(task, TASK_WOKEN_TIMER);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200174 }
Willy Tarreau28c41a42008-06-29 17:00:59 +0200175 tree = (tree + 1) & TIMER_TREE_MASK;
176 } while (((tree - now_tree) & TIMER_TREE_MASK) < TIMER_TREES/2);
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200177
Willy Tarreau28c41a42008-06-29 17:00:59 +0200178 /* We have found no task to expire in any tree */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200179 *next = TICK_ETERNITY;
Willy Tarreau28c41a42008-06-29 17:00:59 +0200180 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200181}
182
Willy Tarreau58b458d2008-06-29 22:40:23 +0200183/* The run queue is chronologically sorted in a tree. An insertion counter is
184 * used to assign a position to each task. This counter may be combined with
185 * other variables (eg: nice value) to set the final position in the tree. The
186 * counter may wrap without a problem, of course. We then limit the number of
Willy Tarreau91e99932008-06-30 07:51:00 +0200187 * tasks processed at once to 1/4 of the number of tasks in the queue, and to
188 * 200 max in any case, so that general latency remains low and so that task
189 * positions have a chance to be considered. It also reduces the number of
190 * trees to be evaluated when no task remains.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200191 *
Willy Tarreau58b458d2008-06-29 22:40:23 +0200192 * Just like with timers, we start with tree[(current - 1)], which holds past
193 * values, and stop when we reach the middle of the list. In practise, we visit
194 * 3 out of 4 trees.
195 *
196 * The function adjusts <next> if a new event is closer.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200197 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200198void process_runnable_tasks(int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200199{
Willy Tarreau964c9362007-01-07 00:38:00 +0100200 struct task *t;
Willy Tarreau58b458d2008-06-29 22:40:23 +0200201 struct eb32_node *eb;
202 unsigned int tree, stop;
203 unsigned int max_processed;
Willy Tarreau26c25062009-03-08 09:38:41 +0100204 int expire;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200205
Willy Tarreau58b458d2008-06-29 22:40:23 +0200206 if (!run_queue)
207 return;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200208
Willy Tarreau91e99932008-06-30 07:51:00 +0200209 max_processed = run_queue;
210 if (max_processed > 200)
211 max_processed = 200;
212
213 if (likely(niced_tasks))
214 max_processed /= 4;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200215
Willy Tarreaud0a201b2009-03-08 15:53:06 +0100216 tree = timer_to_tree(rqueue_ticks);
Willy Tarreau58b458d2008-06-29 22:40:23 +0200217 stop = (tree + TIMER_TREES / 2) & TIMER_TREE_MASK;
218 tree = (tree - 1) & TIMER_TREE_MASK;
Willy Tarreau964c9362007-01-07 00:38:00 +0100219
Willy Tarreau26c25062009-03-08 09:38:41 +0100220 expire = *next;
Willy Tarreau58b458d2008-06-29 22:40:23 +0200221 do {
222 eb = eb32_first(&rqueue[tree]);
223 while (eb) {
Willy Tarreau4726f532009-03-07 17:25:21 +0100224 t = eb32_entry(eb, struct task, rq);
Willy Tarreau58b458d2008-06-29 22:40:23 +0200225
226 /* detach the task from the queue and add the task to the run queue */
227 eb = eb32_next(eb);
Willy Tarreau4726f532009-03-07 17:25:21 +0100228 __task_unlink_rq(t);
Willy Tarreau58b458d2008-06-29 22:40:23 +0200229
Willy Tarreau4726f532009-03-07 17:25:21 +0100230 t->state |= TASK_RUNNING;
Willy Tarreau26c25062009-03-08 09:38:41 +0100231 if (likely(t->process(t) != NULL)) {
232 t->state &= ~TASK_RUNNING;
233 expire = tick_first(expire, t->expire);
234 task_queue(t);
235 }
Willy Tarreau58b458d2008-06-29 22:40:23 +0200236
237 if (!--max_processed)
Willy Tarreau26c25062009-03-08 09:38:41 +0100238 goto out;
Willy Tarreau58b458d2008-06-29 22:40:23 +0200239 }
240 tree = (tree + 1) & TIMER_TREE_MASK;
241 } while (tree != stop);
Willy Tarreau26c25062009-03-08 09:38:41 +0100242 out:
243 *next = expire;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200244}
245
Willy Tarreau4726f532009-03-07 17:25:21 +0100246/* perform minimal intializations, report 0 in case of error, 1 if OK. */
247int init_task()
248{
249 memset(&timers, 0, sizeof(timers));
250 memset(&rqueue, 0, sizeof(rqueue));
251 pool2_task = create_pool("task", sizeof(struct task), MEM_F_SHARED);
252 return pool2_task != NULL;
253}
254
Willy Tarreaubaaee002006-06-26 02:48:02 +0200255/*
256 * Local variables:
257 * c-indent-level: 8
258 * c-basic-offset: 8
259 * End:
260 */