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> |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 14 | #include <common/memory.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 15 | #include <common/mini-clist.h> |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 16 | #include <common/standard.h> |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 17 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 18 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 19 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 20 | #include <proto/task.h> |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 21 | #include <types/task.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 22 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 23 | // FIXME: check 8bitops.c for faster FLS |
| 24 | #include <import/bitops.h> |
| 25 | #include <import/tree.h> |
| 26 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 27 | static struct ultree *stack[LLONGBITS]; |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 28 | |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 29 | struct pool_head *pool2_task, *pool2_tree64; |
| 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 | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 35 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 36 | int init_task() |
| 37 | { |
| 38 | pool2_task = create_pool("task", sizeof(struct task), MEM_F_SHARED); |
| 39 | pool2_tree64 = create_pool("tree64", sizeof(struct tree64), MEM_F_SHARED); |
| 40 | return pool2_task && pool2_tree64; |
| 41 | } |
| 42 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 43 | 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] | 44 | { |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 45 | return __ul2tree_insert(root, h, l); |
| 46 | } |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 47 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 48 | void *tree_delete(void *node) { |
| 49 | return __tree_delete(node); |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 50 | } |
| 51 | |
Willy Tarreau | e33aece | 2007-04-30 13:15:14 +0200 | [diff] [blame] | 52 | struct task *_task_wakeup(struct task *t) |
| 53 | { |
| 54 | return __task_wakeup(t); |
| 55 | } |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 56 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 57 | /* |
| 58 | * task_queue() |
| 59 | * |
| 60 | * Inserts a task into the wait queue at the position given by its expiration |
| 61 | * date. |
| 62 | * |
| 63 | */ |
| 64 | struct task *task_queue(struct task *task) |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 65 | { |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 66 | if (unlikely(task->qlist.p != NULL)) { |
| 67 | DLIST_DEL(&task->qlist); |
| 68 | task->qlist.p = NULL; |
| 69 | } |
| 70 | |
Willy Tarreau | 70bcfb7 | 2008-01-27 02:21:53 +0100 | [diff] [blame] | 71 | if (unlikely(task->wq != NULL)) { |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 72 | tree_delete(task->wq); |
| 73 | task->wq = NULL; |
| 74 | } |
| 75 | |
| 76 | if (unlikely(tv_iseternity(&task->expire))) { |
| 77 | task->wq = NULL; |
| 78 | DLIST_ADD(eternity_queue, &task->qlist); |
| 79 | return task; |
| 80 | } |
| 81 | |
| 82 | task->wq = ul2tree_insert(&timer_wq, task->expire.tv_sec, task->expire.tv_usec); |
| 83 | DLIST_ADD(task->wq->data, &task->qlist); |
| 84 | return task; |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 88 | /* |
| 89 | * Extract all expired timers from the wait queue, and wakes up all |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 90 | * associated tasks. Returns the date of next event (or eternity). |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 91 | * |
| 92 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 93 | void wake_expired_tasks(struct timeval *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 94 | { |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 95 | int slen; |
| 96 | struct task *task; |
| 97 | void *data; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 98 | |
Willy Tarreau | 1209033 | 2007-05-14 02:11:39 +0200 | [diff] [blame] | 99 | #ifdef WAKE_HINT_CHECK_FIRST |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 100 | /* |
| 101 | * Hint: tasks are *rarely* expired. So we can try to optimize |
Willy Tarreau | 1209033 | 2007-05-14 02:11:39 +0200 | [diff] [blame] | 102 | * by not scanning the tree at all in most cases. However, this |
| 103 | * code costs 160 more bytes which do not look much useful because |
| 104 | * the performance win is not obvious. |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 105 | */ |
| 106 | |
| 107 | if (likely(timer_wq.data != NULL)) { |
| 108 | task = LIST_ELEM(timer_wq.data, struct task *, qlist); |
Willy Tarreau | c64e539 | 2007-05-13 16:07:06 +0200 | [diff] [blame] | 109 | if (likely(tv_isgt(&task->expire, &now))) { |
Willy Tarreau | fbfc053 | 2007-05-14 02:03:47 +0200 | [diff] [blame] | 110 | *next = task->expire; |
Willy Tarreau | c64e539 | 2007-05-13 16:07:06 +0200 | [diff] [blame] | 111 | return; |
Willy Tarreau | 7317eb5 | 2007-05-09 00:54:10 +0200 | [diff] [blame] | 112 | } |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 113 | } |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 114 | /* OK we lose. Let's scan the tree then. */ |
Willy Tarreau | 1209033 | 2007-05-14 02:11:39 +0200 | [diff] [blame] | 115 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 116 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 117 | tree64_foreach(&timer_wq, data, stack, slen) { |
| 118 | task = LIST_ELEM(data, struct task *, qlist); |
| 119 | |
Willy Tarreau | c64e539 | 2007-05-13 16:07:06 +0200 | [diff] [blame] | 120 | if (tv_isgt(&task->expire, &now)) { |
Willy Tarreau | fbfc053 | 2007-05-14 02:03:47 +0200 | [diff] [blame] | 121 | *next = task->expire; |
Willy Tarreau | 1209033 | 2007-05-14 02:11:39 +0200 | [diff] [blame] | 122 | return; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 123 | } |
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 | /* |
| 126 | * OK, all tasks linked to this node will be unlinked, as well |
| 127 | * as the node itself, so we do not need to care about correct |
| 128 | * unlinking. |
| 129 | */ |
| 130 | foreach_dlist_item(task, data, struct task *, qlist) { |
| 131 | DLIST_DEL(&task->qlist); |
| 132 | task->wq = NULL; |
| 133 | DLIST_ADD(run_queue, &task->qlist); |
| 134 | task->state = TASK_RUNNING; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 135 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 136 | } |
Willy Tarreau | 1209033 | 2007-05-14 02:11:39 +0200 | [diff] [blame] | 137 | tv_eternity(next); |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 138 | return; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /* |
| 142 | * This does 4 things : |
| 143 | * - wake up all expired tasks |
| 144 | * - call all runnable tasks |
| 145 | * - call maintain_proxies() to enable/disable the listeners |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 146 | * - return the date of next event in <next> or eternity. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 147 | * |
| 148 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 149 | void process_runnable_tasks(struct timeval *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 150 | { |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 151 | struct timeval temp; |
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 | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 155 | wake_expired_tasks(next); |
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 | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 164 | DLIST_DEL(&t->qlist); |
| 165 | t->qlist.p = NULL; |
| 166 | |
| 167 | t->state = TASK_IDLE; |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 168 | t->process(t, &temp); |
| 169 | tv_bound(next, &temp); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 170 | } |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 171 | |
| 172 | /* maintain all proxies in a consistent state. This should quickly |
| 173 | * become a task because it becomes expensive when there are huge |
| 174 | * numbers of proxies. */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 175 | maintain_proxies(&temp); |
| 176 | tv_bound(next, &temp); |
| 177 | return; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 180 | /* |
| 181 | * Local variables: |
| 182 | * c-indent-level: 8 |
| 183 | * c-basic-offset: 8 |
| 184 | * End: |
| 185 | */ |