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