Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Task management functions. |
| 3 | * |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 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 Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 13 | #include <common/config.h> |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 14 | #include <common/eb32tree.h> |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 15 | #include <common/memory.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 16 | #include <common/mini-clist.h> |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 17 | #include <common/standard.h> |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 18 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 19 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 20 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | #include <proto/task.h> |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 22 | #include <types/task.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 23 | |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 24 | struct pool_head *pool2_task; |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 25 | |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 26 | void *run_queue = NULL; |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 27 | |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 28 | /* 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 Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 41 | |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 42 | static 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. */ |
| 49 | static 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. */ |
| 68 | static inline unsigned int timeval_to_tree(const struct timeval *t) |
| 69 | { |
| 70 | return (t->tv_sec >> TIMER_SECOND_BITS) & 1; |
| 71 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 72 | |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 73 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 74 | int init_task() |
| 75 | { |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 76 | memset(&timers, 0, sizeof(timers)); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 77 | |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 78 | /* note: we never queue in the past, so we start with <now> */ |
| 79 | timers.curr = &timers.t[timeval_to_tree(&now)]; |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 80 | |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 81 | pool2_task = create_pool("task", sizeof(struct task), MEM_F_SHARED); |
| 82 | return pool2_task != NULL; |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Willy Tarreau | e33aece | 2007-04-30 13:15:14 +0200 | [diff] [blame] | 85 | struct task *_task_wakeup(struct task *t) |
| 86 | { |
| 87 | return __task_wakeup(t); |
| 88 | } |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 89 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 90 | /* |
| 91 | * task_queue() |
| 92 | * |
| 93 | * Inserts a task into the wait queue at the position given by its expiration |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 94 | * 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 Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 99 | */ |
| 100 | struct task *task_queue(struct task *task) |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 101 | { |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 102 | struct eb_root *tmp; |
| 103 | unsigned int key; |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 104 | |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 105 | if (unlikely(tv_iseternity(&task->expire))) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 106 | return task; |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 107 | |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 108 | 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 Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 114 | return task; |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 118 | /* |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 119 | * Extract all expired timers from the timer queue, and wakes up all |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 120 | * associated tasks. Returns the date of next event (or eternity). |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 121 | * |
| 122 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 123 | void wake_expired_tasks(struct timeval *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 124 | { |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 125 | struct task *task; |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 126 | struct eb32_node *eb; |
| 127 | unsigned int now_key; |
| 128 | unsigned int now_tree; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 129 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 130 | |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 131 | 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 Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 142 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 143 | |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 144 | now_key = timeval_to_key(&now); |
| 145 | do { |
| 146 | eb = eb32_first(timers.curr); |
| 147 | while (eb) { |
| 148 | struct eb32_node *next_eb; |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 149 | |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 150 | task = eb32_entry(eb, struct task, eb); |
Willy Tarreau | e62bdd4 | 2008-06-29 10:25:57 +0200 | [diff] [blame^] | 151 | if ((signed)(eb->key - now_key) > 0) { |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 152 | *next = task->expire; |
| 153 | timers.first = task->expire; |
| 154 | return; |
| 155 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 156 | |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 157 | /* 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 Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 163 | DLIST_ADD(run_queue, &task->qlist); |
| 164 | task->state = TASK_RUNNING; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 165 | } |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 166 | |
| 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 188 | } |
| 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 Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 195 | * - return the date of next event in <next> or eternity. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 196 | * |
| 197 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 198 | void process_runnable_tasks(struct timeval *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 199 | { |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 200 | struct timeval temp; |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 201 | struct task *t; |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 202 | void *queue; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 203 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 204 | wake_expired_tasks(next); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 205 | /* 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 Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 208 | * after as it will be queued on the run queue's head ! |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 209 | */ |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 210 | |
| 211 | queue = run_queue; |
| 212 | foreach_dlist_item(t, queue, struct task *, qlist) { |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 213 | DLIST_DEL(&t->qlist); |
| 214 | t->qlist.p = NULL; |
| 215 | |
| 216 | t->state = TASK_IDLE; |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 217 | t->process(t, &temp); |
| 218 | tv_bound(next, &temp); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 219 | } |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 220 | |
| 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 Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 224 | maintain_proxies(&temp); |
| 225 | tv_bound(next, &temp); |
| 226 | return; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 229 | /* |
| 230 | * Local variables: |
| 231 | * c-indent-level: 8 |
| 232 | * c-basic-offset: 8 |
| 233 | * End: |
| 234 | */ |