blob: 13d6817efc668d8bfcc32dbaf2ae1df5683b2306 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Task management functions.
3 *
Willy Tarreau9789f7b2008-06-24 08:17:16 +02004 * Copyright 2000-2008 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>
Willy Tarreau96bcfd72007-04-29 10:41:56 +020022#include <types/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
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 +020030/* Principle of the wait queue.
31 *
32 * We want to be able to tell whether an expiration date is before of after the
33 * current time <now>. We KNOW that expiration dates are never too far apart,
34 * because they are already computed by adding integer numbers of milliseconds
35 * to the current date.
36 * We also know that almost all dates will be in the future, and that a very
37 * small part of them will be in the past, they are the ones which have expired
38 * since last time we checked them.
39 *
40 * The current implementation uses a wrapping time cut into 3 ranges :
41 * - previous : those ones are expired by definition
42 * - current : some are expired, some are not
43 * - next : none are expired
44 *
45 * We use the higher two bits of the timers expressed in ticks (milliseconds)
46 * to determine which range a timer is in, compared to <now> :
47 *
48 * now previous current next0 next1
49 * [31:30] [31:30] [31:30] [31:30] [31:30]
50 * 00 11 00 01 10
51 * 01 00 01 10 11
52 * 10 01 10 11 00
53 * 11 10 11 00 01
54 *
55 * By definition, <current> is the range containing <now> as well as all timers
56 * which have the same 2 high bits as <now>, <previous> is the range just
57 * before, which contains all timers whose high bits equal those of <now> minus
58 * 1. Last, <next> is composed of the two remaining ranges.
59 *
60 * For ease of implementation, the timers will then be stored into 4 queues 0-3
61 * determined by the 2 higher bits of the timer. The expiration algorithm is
62 * very simple :
63 * - expire everything in <previous>=queue[((now>>30)-1)&3]
64 * - expire from <current>=queue[(now>>30)&3] everything where timer >= now
65 *
66 * With this algorithm, it's possible to queue tasks meant to expire 24.8 days
67 * in the future, and still be able to detect events remaining unprocessed for
68 * the last 12.4 days! Note that the principle might be extended to any number
69 * of higher bits as long as there is only one range for expired tasks. For
70 * instance, using the 8 higher bits to index the range, we would have one past
71 * range of 4.6 hours (24 bits in ms), and 254 ranges in the future totalizing
72 * 49.3 days. This would eat more memory for a very little added benefit.
73 *
74 * Also, in order to maintain the ability to perform time comparisons, it is
75 * recommended to avoid using the <next1> range above, as values in this range
76 * may not easily be compared to <now> outside of these functions as it is the
77 * opposite of the <current> range, and <timer>-<now> may randomly be positive
78 * or negative. That means we're left with +/- 12 days timers.
79 *
80 * To keep timers ordered, we use 4 ebtrees [0..3]. To keep computation low, we
81 * may use (seconds*1024)+milliseconds, which preserves ordering eventhough we
82 * can't do real computations on it. Future evolutions could make use of 1024th
83 * of seconds instead of milliseconds, with the special value 0 avoided (and
84 * replaced with 1), so that zero indicates the timer is not set.
Willy Tarreau9789f7b2008-06-24 08:17:16 +020085 */
Willy Tarreau28c41a42008-06-29 17:00:59 +020086
87#define TIMER_TICK_BITS 32
88#define TIMER_TREE_BITS 2
89#define TIMER_TREES (1 << TIMER_TREE_BITS)
90#define TIMER_TREE_SHIFT (TIMER_TICK_BITS - TIMER_TREE_BITS)
91#define TIMER_TREE_MASK (TIMER_TREES - 1)
92#define TIMER_TICK_MASK ((1U << (TIMER_TICK_BITS-1)) * 2 - 1)
93#define TIMER_SIGN_BIT (1 << (TIMER_TICK_BITS - 1))
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020094
Willy Tarreau28c41a42008-06-29 17:00:59 +020095static struct eb_root timers[TIMER_TREES]; /* trees with MSB 00, 01, 10 and 11 */
Willy Tarreau58b458d2008-06-29 22:40:23 +020096static struct eb_root rqueue[TIMER_TREES]; /* trees constituting the run queue */
97static unsigned int rqueue_ticks; /* insertion count */
Willy Tarreau9789f7b2008-06-24 08:17:16 +020098
99/* returns an ordered key based on an expiration date. */
Willy Tarreau28c41a42008-06-29 17:00:59 +0200100static inline unsigned int timeval_to_ticks(const struct timeval *t)
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200101{
102 unsigned int key;
103
Willy Tarreau28c41a42008-06-29 17:00:59 +0200104 key = ((unsigned int)t->tv_sec * 1000) + ((unsigned int)t->tv_usec / 1000);
105 key &= TIMER_TICK_MASK;
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200106 return key;
107}
108
Willy Tarreau28c41a42008-06-29 17:00:59 +0200109/* returns a tree number based on a ticks value */
110static inline unsigned int ticks_to_tree(unsigned int ticks)
111{
112 return (ticks >> TIMER_TREE_SHIFT) & TIMER_TREE_MASK;
113}
114
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200115/* returns a tree number based on an expiration date. */
116static inline unsigned int timeval_to_tree(const struct timeval *t)
117{
Willy Tarreau28c41a42008-06-29 17:00:59 +0200118 return ticks_to_tree(timeval_to_ticks(t));
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200119}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200120
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200121/* perform minimal intializations, report 0 in case of error, 1 if OK. */
122int init_task()
123{
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200124 memset(&timers, 0, sizeof(timers));
Willy Tarreau58b458d2008-06-29 22:40:23 +0200125 memset(&rqueue, 0, sizeof(rqueue));
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200126 pool2_task = create_pool("task", sizeof(struct task), MEM_F_SHARED);
127 return pool2_task != NULL;
Willy Tarreau964c9362007-01-07 00:38:00 +0100128}
129
Willy Tarreau91e99932008-06-30 07:51:00 +0200130/* Puts the task <t> in run queue at a position depending on t->nice.
131 * <t> is returned. The nice value assigns boosts in 32th of the run queue
132 * size. A nice value of -1024 sets the task to -run_queue*32, while a nice
133 * value of 1024 sets the task to run_queue*32.
134 */
Willy Tarreau58b458d2008-06-29 22:40:23 +0200135struct task *task_wakeup(struct task *t)
Willy Tarreaue33aece2007-04-30 13:15:14 +0200136{
Willy Tarreau58b458d2008-06-29 22:40:23 +0200137 if (t->state == TASK_RUNNING)
138 return t;
139
Willy Tarreauce44f122008-07-05 18:16:19 +0200140 task_dequeue(t);
Willy Tarreau58b458d2008-06-29 22:40:23 +0200141
142 run_queue++;
143 t->eb.key = ++rqueue_ticks;
Willy Tarreau91e99932008-06-30 07:51:00 +0200144
145 if (likely(t->nice)) {
146 int offset;
147
148 niced_tasks++;
149 if (likely(t->nice > 0))
150 offset = (unsigned)((run_queue * (unsigned int)t->nice) / 32U);
151 else
152 offset = -(unsigned)((run_queue * (unsigned int)-t->nice) / 32U);
153 t->eb.key += offset;
154 }
155
Willy Tarreau58b458d2008-06-29 22:40:23 +0200156 t->state = TASK_RUNNING;
157
158 eb32_insert(&rqueue[ticks_to_tree(t->eb.key)], &t->eb);
159 return t;
Willy Tarreaue33aece2007-04-30 13:15:14 +0200160}
Willy Tarreaud825eef2007-05-12 22:35:00 +0200161
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200162/*
163 * task_queue()
164 *
165 * Inserts a task into the wait queue at the position given by its expiration
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200166 * date. Note that the task must *not* already be in the wait queue nor in the
167 * run queue, otherwise unpredictable results may happen. Tasks queued with an
168 * eternity expiration date are simply returned. Last, tasks must not be queued
Willy Tarreau28c41a42008-06-29 17:00:59 +0200169 * further than the end of the next tree, which is between <now_ms> and
170 * <now_ms> + TIMER_SIGN_BIT ms (now+12days..24days in 32bit).
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200171 */
172struct task *task_queue(struct task *task)
Willy Tarreau964c9362007-01-07 00:38:00 +0100173{
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200174 if (unlikely(tv_iseternity(&task->expire)))
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200175 return task;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200176
Willy Tarreau28c41a42008-06-29 17:00:59 +0200177 task->eb.key = timeval_to_ticks(&task->expire);
178#ifdef DEBUG_CHECK_INVALID_EXPIRATION_DATES
179 if ((task->eb.key - now_ms) & TIMER_SIGN_BIT)
180 /* we're queuing too far away or in the past (most likely) */
181 return task;
182#endif
Willy Tarreauce44f122008-07-05 18:16:19 +0200183
184 if (likely(last_timer &&
185 last_timer->eb.key == task->eb.key &&
186 last_timer->eb.node.node_p)) {
187 /* Most often, last queued timer has the same expiration date, so
188 * if it's not queued at the root, let's queue a dup directly there.
189 */
190 eb_insert_dup(&last_timer->eb.node, &task->eb.node);
191 return task;
192 }
Willy Tarreau28c41a42008-06-29 17:00:59 +0200193 eb32_insert(&timers[ticks_to_tree(task->eb.key)], &task->eb);
Willy Tarreauce44f122008-07-05 18:16:19 +0200194 last_timer = task;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200195 return task;
Willy Tarreau964c9362007-01-07 00:38:00 +0100196}
197
198
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200199/*
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200200 * Extract all expired timers from the timer queue, and wakes up all
Willy Tarreaud825eef2007-05-12 22:35:00 +0200201 * associated tasks. Returns the date of next event (or eternity).
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200202 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200203void wake_expired_tasks(struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200204{
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200205 struct task *task;
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200206 struct eb32_node *eb;
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200207 unsigned int now_tree;
Willy Tarreau28c41a42008-06-29 17:00:59 +0200208 unsigned int tree;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200209
Willy Tarreau28c41a42008-06-29 17:00:59 +0200210 /* In theory, we should :
211 * - wake all tasks from the <previous> tree
212 * - wake all expired tasks from the <current> tree
213 * - scan <next> trees for next expiration date if not found earlier.
214 * But we can do all this more easily : we scan all 3 trees before we
215 * wrap, and wake everything expired from there, then stop on the first
216 * non-expired entry.
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200217 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200218
Willy Tarreau28c41a42008-06-29 17:00:59 +0200219 now_tree = ticks_to_tree(now_ms);
220 tree = (now_tree - 1) & TIMER_TREE_MASK;
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200221 do {
Willy Tarreau28c41a42008-06-29 17:00:59 +0200222 eb = eb32_first(&timers[tree]);
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200223 while (eb) {
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200224 task = eb32_entry(eb, struct task, eb);
Willy Tarreau28c41a42008-06-29 17:00:59 +0200225 if ((now_ms - eb->key) & TIMER_SIGN_BIT) {
226 /* note that we don't need this check for the <previous>
227 * tree, but it's cheaper than duplicating the code.
228 */
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200229 *next = task->expire;
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200230 return;
231 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200232
Willy Tarreauaf754fc2008-06-29 19:25:52 +0200233 /* detach the task from the queue and add the task to the run queue */
234 eb = eb32_next(eb);
Willy Tarreau58b458d2008-06-29 22:40:23 +0200235 task_wakeup(task);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200236 }
Willy Tarreau28c41a42008-06-29 17:00:59 +0200237 tree = (tree + 1) & TIMER_TREE_MASK;
238 } while (((tree - now_tree) & TIMER_TREE_MASK) < TIMER_TREES/2);
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200239
Willy Tarreau28c41a42008-06-29 17:00:59 +0200240 /* We have found no task to expire in any tree */
241 tv_eternity(next);
242 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200243}
244
Willy Tarreau58b458d2008-06-29 22:40:23 +0200245/* The run queue is chronologically sorted in a tree. An insertion counter is
246 * used to assign a position to each task. This counter may be combined with
247 * other variables (eg: nice value) to set the final position in the tree. The
248 * counter may wrap without a problem, of course. We then limit the number of
Willy Tarreau91e99932008-06-30 07:51:00 +0200249 * tasks processed at once to 1/4 of the number of tasks in the queue, and to
250 * 200 max in any case, so that general latency remains low and so that task
251 * positions have a chance to be considered. It also reduces the number of
252 * trees to be evaluated when no task remains.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200253 *
Willy Tarreau58b458d2008-06-29 22:40:23 +0200254 * Just like with timers, we start with tree[(current - 1)], which holds past
255 * values, and stop when we reach the middle of the list. In practise, we visit
256 * 3 out of 4 trees.
257 *
258 * The function adjusts <next> if a new event is closer.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200259 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200260void process_runnable_tasks(struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200261{
Willy Tarreaud825eef2007-05-12 22:35:00 +0200262 struct timeval temp;
Willy Tarreau964c9362007-01-07 00:38:00 +0100263 struct task *t;
Willy Tarreau58b458d2008-06-29 22:40:23 +0200264 struct eb32_node *eb;
265 unsigned int tree, stop;
266 unsigned int max_processed;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200267
Willy Tarreau58b458d2008-06-29 22:40:23 +0200268 if (!run_queue)
269 return;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200270
Willy Tarreau91e99932008-06-30 07:51:00 +0200271 max_processed = run_queue;
272 if (max_processed > 200)
273 max_processed = 200;
274
275 if (likely(niced_tasks))
276 max_processed /= 4;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200277
Willy Tarreau58b458d2008-06-29 22:40:23 +0200278 tree = ticks_to_tree(rqueue_ticks);
279 stop = (tree + TIMER_TREES / 2) & TIMER_TREE_MASK;
280 tree = (tree - 1) & TIMER_TREE_MASK;
Willy Tarreau964c9362007-01-07 00:38:00 +0100281
Willy Tarreau58b458d2008-06-29 22:40:23 +0200282 do {
283 eb = eb32_first(&rqueue[tree]);
284 while (eb) {
285 t = eb32_entry(eb, struct task, eb);
286
287 /* detach the task from the queue and add the task to the run queue */
288 eb = eb32_next(eb);
289
290 run_queue--;
Willy Tarreau91e99932008-06-30 07:51:00 +0200291 if (likely(t->nice))
292 niced_tasks--;
Willy Tarreau58b458d2008-06-29 22:40:23 +0200293 t->state = TASK_IDLE;
Willy Tarreauce44f122008-07-05 18:16:19 +0200294 task_dequeue(t);
Willy Tarreau58b458d2008-06-29 22:40:23 +0200295
296 t->process(t, &temp);
297 tv_bound(next, &temp);
298
299 if (!--max_processed)
300 return;
301 }
302 tree = (tree + 1) & TIMER_TREE_MASK;
303 } while (tree != stop);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200304}
305
Willy Tarreaubaaee002006-06-26 02:48:02 +0200306/*
307 * Local variables:
308 * c-indent-level: 8
309 * c-basic-offset: 8
310 * End:
311 */