Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Task management functions. |
| 3 | * |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 4 | * Copyright 2000-2007 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> |
| 14 | #include <common/mini-clist.h> |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 15 | #include <common/standard.h> |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 16 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 17 | |
| 18 | #include <proto/task.h> |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 19 | #include <types/task.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 20 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 21 | // FIXME: check 8bitops.c for faster FLS |
| 22 | #include <import/bitops.h> |
| 23 | #include <import/tree.h> |
| 24 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 25 | |
| 26 | /* FIXME : this should be removed very quickly ! */ |
| 27 | extern int maintain_proxies(void); |
| 28 | |
| 29 | void **pool_task= NULL; |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 30 | void **pool_tree64 = NULL; |
| 31 | static struct ultree *stack[LLONGBITS]; |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 32 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 33 | UL2TREE_HEAD(timer_wq); |
| 34 | void *eternity_queue = NULL; |
| 35 | void *run_queue = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 37 | struct ultree *ul2tree_insert(struct ultree *root, unsigned long h, unsigned long l) |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 38 | { |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 39 | return __ul2tree_insert(root, h, l); |
| 40 | } |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 41 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 42 | void *tree_delete(void *node) { |
| 43 | return __tree_delete(node); |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 44 | } |
| 45 | |
Willy Tarreau | e33aece | 2007-04-30 13:15:14 +0200 | [diff] [blame] | 46 | struct task *_task_wakeup(struct task *t) |
| 47 | { |
| 48 | return __task_wakeup(t); |
| 49 | } |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 50 | /* |
| 51 | * task_queue() |
| 52 | * |
| 53 | * Inserts a task into the wait queue at the position given by its expiration |
| 54 | * date. |
| 55 | * |
| 56 | */ |
| 57 | struct task *task_queue(struct task *task) |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 58 | { |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 59 | if (unlikely(task->qlist.p != NULL)) { |
| 60 | DLIST_DEL(&task->qlist); |
| 61 | task->qlist.p = NULL; |
| 62 | } |
| 63 | |
| 64 | if (unlikely(task->wq)) { |
| 65 | tree_delete(task->wq); |
| 66 | task->wq = NULL; |
| 67 | } |
| 68 | |
| 69 | if (unlikely(tv_iseternity(&task->expire))) { |
| 70 | task->wq = NULL; |
| 71 | DLIST_ADD(eternity_queue, &task->qlist); |
| 72 | return task; |
| 73 | } |
| 74 | |
| 75 | task->wq = ul2tree_insert(&timer_wq, task->expire.tv_sec, task->expire.tv_usec); |
| 76 | DLIST_ADD(task->wq->data, &task->qlist); |
| 77 | return task; |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 81 | /* |
| 82 | * Extract all expired timers from the wait queue, and wakes up all |
| 83 | * associated tasks. |
| 84 | * Returns the time to wait for next task (next_time). |
| 85 | * |
| 86 | * FIXME: Use an alternative queue for ETERNITY tasks. |
| 87 | * |
| 88 | */ |
| 89 | int wake_expired_tasks() |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 90 | { |
Willy Tarreau | 7317eb5 | 2007-05-09 00:54:10 +0200 | [diff] [blame^] | 91 | __label__ out; |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 92 | int slen; |
| 93 | struct task *task; |
| 94 | void *data; |
| 95 | int next_time; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 96 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 97 | /* |
| 98 | * Hint: tasks are *rarely* expired. So we can try to optimize |
| 99 | * by not scanning the tree at all in most cases. |
| 100 | */ |
| 101 | |
| 102 | if (likely(timer_wq.data != NULL)) { |
| 103 | task = LIST_ELEM(timer_wq.data, struct task *, qlist); |
Willy Tarreau | 7317eb5 | 2007-05-09 00:54:10 +0200 | [diff] [blame^] | 104 | if (likely(__tv_isge(&task->expire, &now) > 0)) { |
| 105 | next_time = tv_ms_remain(&now, &task->expire); |
| 106 | goto out; |
| 107 | } |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* OK we lose. Let's scan the tree then. */ |
| 111 | next_time = TIME_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 112 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 113 | tree64_foreach(&timer_wq, data, stack, slen) { |
| 114 | task = LIST_ELEM(data, struct task *, qlist); |
| 115 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 116 | if (__tv_isgt(&task->expire, &now)) { |
| 117 | next_time = tv_ms_remain(&now, &task->expire); |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 118 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 119 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 120 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 121 | /* |
| 122 | * OK, all tasks linked to this node will be unlinked, as well |
| 123 | * as the node itself, so we do not need to care about correct |
| 124 | * unlinking. |
| 125 | */ |
| 126 | foreach_dlist_item(task, data, struct task *, qlist) { |
| 127 | DLIST_DEL(&task->qlist); |
| 128 | task->wq = NULL; |
| 129 | DLIST_ADD(run_queue, &task->qlist); |
| 130 | task->state = TASK_RUNNING; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 131 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 132 | } |
Willy Tarreau | 7317eb5 | 2007-05-09 00:54:10 +0200 | [diff] [blame^] | 133 | out: |
| 134 | /* Ensure that we don't report sub-millisecond timeouts */ |
| 135 | if (next_time != TIME_ETERNITY) |
| 136 | next_time++; |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 137 | return next_time; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* |
| 141 | * This does 4 things : |
| 142 | * - wake up all expired tasks |
| 143 | * - call all runnable tasks |
| 144 | * - call maintain_proxies() to enable/disable the listeners |
| 145 | * - return the delay till next event in ms, -1 = wait indefinitely |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 146 | * |
| 147 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 148 | int process_runnable_tasks() |
| 149 | { |
| 150 | int next_time; |
| 151 | int time2; |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 152 | struct task *t; |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 153 | void *queue; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 154 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 155 | next_time = wake_expired_tasks(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 156 | /* process each task in the run queue now. Each task may be deleted |
| 157 | * since we only use the run queue's head. Note that any task can be |
| 158 | * woken up by any other task and it will be processed immediately |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 159 | * after as it will be queued on the run queue's head ! |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 160 | */ |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 161 | |
| 162 | queue = run_queue; |
| 163 | foreach_dlist_item(t, queue, struct task *, qlist) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 164 | int temp_time; |
| 165 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 166 | DLIST_DEL(&t->qlist); |
| 167 | t->qlist.p = NULL; |
| 168 | |
| 169 | t->state = TASK_IDLE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 170 | temp_time = t->process(t); |
| 171 | next_time = MINTIME(temp_time, next_time); |
| 172 | } |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 173 | |
| 174 | /* maintain all proxies in a consistent state. This should quickly |
| 175 | * become a task because it becomes expensive when there are huge |
| 176 | * numbers of proxies. */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 177 | time2 = maintain_proxies(); |
| 178 | return MINTIME(time2, next_time); |
| 179 | } |
| 180 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 181 | /* |
| 182 | * Local variables: |
| 183 | * c-indent-level: 8 |
| 184 | * c-basic-offset: 8 |
| 185 | * End: |
| 186 | */ |