blob: 2c9e0d7a82432e1ce53330f0a05bf3c3e9fdc76b [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 Tarreau9789f7b2008-06-24 08:17:16 +020026void *run_queue = NULL;
Willy Tarreau964c9362007-01-07 00:38:00 +010027
Willy Tarreau9789f7b2008-06-24 08:17:16 +020028/* Principle of the wait queue : we have two trees ordered by time. On of them
29 * contains all timers for current time-frame, and the other one for next
30 * time-frame. Each time-frame is TIMER_KEY_BITS bits wide in number of
31 * milliseconds, which is 49 days for 32 bits. Values are stored into and
32 * retrieved from the tree using a key of TIMER_KEY_BITS bits. A pointer
33 * always designates the current tree, which is the one we read from, until
34 * it is exhausted and <now> has its high bit designate the new tree.
35 * An improvement would consist in holding too large timers in a side tree
36 * consulted only once a switch. It could also be a simple list BTW.
37 */
38#define TIMER_KEY_BITS 32
39#define TIMER_SUBSEC_BITS 10
40#define TIMER_SECOND_BITS (TIMER_KEY_BITS - TIMER_SUBSEC_BITS)
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020041
Willy Tarreau9789f7b2008-06-24 08:17:16 +020042static struct {
43 struct eb_root *curr; /* current time frame (t[0],t[1]) */
44 struct eb_root t[2]; /* trees with MSB 0 and 1 */
45 struct timeval first; /* first value in the tree when known */
46} timers;
47
48/* returns an ordered key based on an expiration date. */
49static inline unsigned int timeval_to_key(const struct timeval *t)
50{
51 unsigned int key;
52
53 /* We choose sec << 10 + usec / 1000 below to keep the precision at the
54 * millisecond, but we might as well divide by 1024 and have a slightly
55 * lower precision of 1.024 ms.
56 */
57
58 key = ((unsigned int)t->tv_sec << TIMER_SUBSEC_BITS) +
59 ((unsigned int)t->tv_usec / 1000);
60
61#if TIMER_KEY_BITS != 32
62 key &= (1 << TIMER_KEY_BITS) - 1;
63#endif
64 return key;
65}
66
67/* returns a tree number based on an expiration date. */
68static inline unsigned int timeval_to_tree(const struct timeval *t)
69{
70 return (t->tv_sec >> TIMER_SECOND_BITS) & 1;
71}
Willy Tarreaubaaee002006-06-26 02:48:02 +020072
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020073/* perform minimal intializations, report 0 in case of error, 1 if OK. */
74int init_task()
75{
Willy Tarreau9789f7b2008-06-24 08:17:16 +020076 memset(&timers, 0, sizeof(timers));
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020077
Willy Tarreau9789f7b2008-06-24 08:17:16 +020078 /* note: we never queue in the past, so we start with <now> */
79 timers.curr = &timers.t[timeval_to_tree(&now)];
Willy Tarreau964c9362007-01-07 00:38:00 +010080
Willy Tarreau9789f7b2008-06-24 08:17:16 +020081 pool2_task = create_pool("task", sizeof(struct task), MEM_F_SHARED);
82 return pool2_task != NULL;
Willy Tarreau964c9362007-01-07 00:38:00 +010083}
84
Willy Tarreaue33aece2007-04-30 13:15:14 +020085struct task *_task_wakeup(struct task *t)
86{
87 return __task_wakeup(t);
88}
Willy Tarreaud825eef2007-05-12 22:35:00 +020089
Willy Tarreau96bcfd72007-04-29 10:41:56 +020090/*
91 * task_queue()
92 *
93 * Inserts a task into the wait queue at the position given by its expiration
Willy Tarreau9789f7b2008-06-24 08:17:16 +020094 * date. Note that the task must *not* already be in the wait queue nor in the
95 * run queue, otherwise unpredictable results may happen. Tasks queued with an
96 * eternity expiration date are simply returned. Last, tasks must not be queued
97 * further than the end of the next tree, which is between now and
98 * now+1<<TIMER_KEY_BITS ms (now+49days in 32bit).
Willy Tarreau96bcfd72007-04-29 10:41:56 +020099 */
100struct task *task_queue(struct task *task)
Willy Tarreau964c9362007-01-07 00:38:00 +0100101{
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200102 struct eb_root *tmp;
103 unsigned int key;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200104
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200105 if (unlikely(tv_iseternity(&task->expire)))
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200106 return task;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200107
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200108 if (tv_islt(&task->expire, &timers.first))
109 timers.first = task->expire;
110
111 key = timeval_to_key(&task->expire);
112 tmp = &timers.t[timeval_to_tree(&task->expire)];
113 eb32_insert(tmp, &task->eb);
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200114 return task;
Willy Tarreau964c9362007-01-07 00:38:00 +0100115}
116
117
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200118/*
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200119 * Extract all expired timers from the timer queue, and wakes up all
Willy Tarreaud825eef2007-05-12 22:35:00 +0200120 * associated tasks. Returns the date of next event (or eternity).
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200121 *
122 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200123void wake_expired_tasks(struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124{
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200125 struct task *task;
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200126 struct eb32_node *eb;
127 unsigned int now_key;
128 unsigned int now_tree;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200129
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200130
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200131 now_tree = timeval_to_tree(&now);
132
133 /* This is a speedup: we immediately check for an expirable task in the
134 * timer's index. Warning: if nothing is found, we still may have to
135 * switch the trees.
136 */
137 if (likely(tv_isgt(&timers.first, &now))) {
138 *next = timers.first;
139 if (timers.curr != &timers.t[now_tree])
140 timers.curr = &timers.t[now_tree];
141 return;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200142 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200143
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200144 now_key = timeval_to_key(&now);
145 do {
146 eb = eb32_first(timers.curr);
147 while (eb) {
148 struct eb32_node *next_eb;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200149
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200150 task = eb32_entry(eb, struct task, eb);
Willy Tarreaue62bdd42008-06-29 10:25:57 +0200151 if ((signed)(eb->key - now_key) > 0) {
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200152 *next = task->expire;
153 timers.first = task->expire;
154 return;
155 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200156
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200157 /* detach the task from the queue */
158 next_eb = eb32_next(eb);
159 eb32_delete(eb);
160 eb = next_eb;
161
162 /* and add the task to the run queue */
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200163 DLIST_ADD(run_queue, &task->qlist);
164 task->state = TASK_RUNNING;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200165 }
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200166
167 /* OK we have reached the end of the <curr> tree. It might mean
168 * that we must now switch, which is indicated by the fact that
169 * the current tree pointer does not match <now> anymore.
170 */
171 if (timers.curr == &timers.t[now_tree]) {
172 /* We cannot switch now, so we have to find the first
173 * timer of the next tree.
174 */
175 eb = eb32_first(&timers.t[now_tree ^ 1]);
176 if (eb) {
177 task = eb32_entry(eb, struct task, eb);
178 *next = task->expire;
179 timers.first = task->expire;
180 } else {
181 tv_eternity(next);
182 tv_eternity(&timers.first);
183 }
184 return;
185 }
186 timers.curr = &timers.t[now_tree];
187 } while (1);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200188}
189
190/*
191 * This does 4 things :
192 * - wake up all expired tasks
193 * - call all runnable tasks
194 * - call maintain_proxies() to enable/disable the listeners
Willy Tarreaud825eef2007-05-12 22:35:00 +0200195 * - return the date of next event in <next> or eternity.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200196 *
197 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200198void process_runnable_tasks(struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200199{
Willy Tarreaud825eef2007-05-12 22:35:00 +0200200 struct timeval temp;
Willy Tarreau964c9362007-01-07 00:38:00 +0100201 struct task *t;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200202 void *queue;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200203
Willy Tarreaud825eef2007-05-12 22:35:00 +0200204 wake_expired_tasks(next);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200205 /* process each task in the run queue now. Each task may be deleted
206 * since we only use the run queue's head. Note that any task can be
207 * woken up by any other task and it will be processed immediately
Willy Tarreau964c9362007-01-07 00:38:00 +0100208 * after as it will be queued on the run queue's head !
Willy Tarreaubaaee002006-06-26 02:48:02 +0200209 */
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200210
211 queue = run_queue;
212 foreach_dlist_item(t, queue, struct task *, qlist) {
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200213 DLIST_DEL(&t->qlist);
214 t->qlist.p = NULL;
215
216 t->state = TASK_IDLE;
Willy Tarreaud825eef2007-05-12 22:35:00 +0200217 t->process(t, &temp);
218 tv_bound(next, &temp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200219 }
Willy Tarreau964c9362007-01-07 00:38:00 +0100220
221 /* maintain all proxies in a consistent state. This should quickly
222 * become a task because it becomes expensive when there are huge
223 * numbers of proxies. */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200224 maintain_proxies(&temp);
225 tv_bound(next, &temp);
226 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200227}
228
Willy Tarreaubaaee002006-06-26 02:48:02 +0200229/*
230 * Local variables:
231 * c-indent-level: 8
232 * c-basic-offset: 8
233 * End:
234 */