blob: 8d4ab39e571b97dea3b032082e1620cd7c977fe9 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Task management functions.
3 *
Willy Tarreau4726f532009-03-07 17:25:21 +01004 * Copyright 2000-2009 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 Tarreau87bed622009-03-08 22:25:28 +010013#include <string.h>
14
Willy Tarreau2dd0d472006-06-29 17:53:05 +020015#include <common/config.h>
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020016#include <common/memory.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020017#include <common/mini-clist.h>
Willy Tarreau96bcfd72007-04-29 10:41:56 +020018#include <common/standard.h>
Willy Tarreaua6a6a932007-04-28 22:40:08 +020019#include <common/time.h>
Willy Tarreau45cb4fb2009-10-26 21:10:04 +010020#include <eb32tree.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreaud825eef2007-05-12 22:35:00 +020022#include <proto/proxy.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020023#include <proto/stream.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024#include <proto/task.h>
25
Willy Tarreau9789f7b2008-06-24 08:17:16 +020026struct pool_head *pool2_task;
Willy Tarreau96bcfd72007-04-29 10:41:56 +020027
Thierry FOURNIERd6975962017-07-12 14:31:10 +020028/* This is the memory pool containing all the signal structs. These
29 * struct are used to store each requiered signal between two tasks.
30 */
31struct pool_head *pool2_notification;
32
Willy Tarreaua4613182009-03-21 18:13:21 +010033unsigned int nb_tasks = 0;
Christopher Faulet34c5cc92016-12-06 09:15:30 +010034unsigned int tasks_run_queue = 0;
35unsigned int tasks_run_queue_cur = 0; /* copy of the run queue size */
Willy Tarreauc7bdf092009-03-21 18:33:52 +010036unsigned int nb_tasks_cur = 0; /* copy of the tasks count */
Willy Tarreaue35c94a2009-03-21 10:01:42 +010037unsigned int niced_tasks = 0; /* number of niced tasks in the run queue */
Emeric Brun01948972017-03-30 15:37:25 +020038
Willy Tarreau964c9362007-01-07 00:38:00 +010039
Willy Tarreaue35c94a2009-03-21 10:01:42 +010040static struct eb_root timers; /* sorted timers tree */
41static struct eb_root rqueue; /* tree constituting the run queue */
42static unsigned int rqueue_ticks; /* insertion count */
Willy Tarreau9789f7b2008-06-24 08:17:16 +020043
Willy Tarreau4726f532009-03-07 17:25:21 +010044/* Puts the task <t> in run queue at a position depending on t->nice. <t> is
45 * returned. The nice value assigns boosts in 32th of the run queue size. A
Christopher Faulet34c5cc92016-12-06 09:15:30 +010046 * nice value of -1024 sets the task to -tasks_run_queue*32, while a nice value
47 * of 1024 sets the task to tasks_run_queue*32. The state flags are cleared, so
48 * the caller will have to set its flags after this call.
Willy Tarreau4726f532009-03-07 17:25:21 +010049 * The task must not already be in the run queue. If unsure, use the safer
50 * task_wakeup() function.
Willy Tarreau91e99932008-06-30 07:51:00 +020051 */
Willy Tarreau4df82062008-08-29 15:26:14 +020052struct task *__task_wakeup(struct task *t)
Willy Tarreaue33aece2007-04-30 13:15:14 +020053{
Christopher Faulet34c5cc92016-12-06 09:15:30 +010054 tasks_run_queue++;
Willy Tarreau4726f532009-03-07 17:25:21 +010055 t->rq.key = ++rqueue_ticks;
Willy Tarreau91e99932008-06-30 07:51:00 +020056
57 if (likely(t->nice)) {
58 int offset;
59
60 niced_tasks++;
61 if (likely(t->nice > 0))
Christopher Faulet34c5cc92016-12-06 09:15:30 +010062 offset = (unsigned)((tasks_run_queue * (unsigned int)t->nice) / 32U);
Willy Tarreau91e99932008-06-30 07:51:00 +020063 else
Christopher Faulet34c5cc92016-12-06 09:15:30 +010064 offset = -(unsigned)((tasks_run_queue * (unsigned int)-t->nice) / 32U);
Willy Tarreau4726f532009-03-07 17:25:21 +010065 t->rq.key += offset;
Willy Tarreau91e99932008-06-30 07:51:00 +020066 }
67
Emeric Brun01948972017-03-30 15:37:25 +020068 /* reset flag to pending ones
69 * Note: __task_wakeup must not be called
70 * if task is running
71 */
72 t->state = t->pending_state;
Willy Tarreaue35c94a2009-03-21 10:01:42 +010073 eb32_insert(&rqueue, &t->rq);
Willy Tarreau58b458d2008-06-29 22:40:23 +020074 return t;
Willy Tarreaue33aece2007-04-30 13:15:14 +020075}
Willy Tarreaud825eef2007-05-12 22:35:00 +020076
Willy Tarreau96bcfd72007-04-29 10:41:56 +020077/*
Willy Tarreau531cf0c2009-03-08 16:35:27 +010078 * __task_queue()
Willy Tarreau96bcfd72007-04-29 10:41:56 +020079 *
80 * Inserts a task into the wait queue at the position given by its expiration
Willy Tarreau4726f532009-03-07 17:25:21 +010081 * date. It does not matter if the task was already in the wait queue or not,
Willy Tarreau531cf0c2009-03-08 16:35:27 +010082 * as it will be unlinked. The task must not have an infinite expiration timer.
Willy Tarreaue35c94a2009-03-21 10:01:42 +010083 * Last, tasks must not be queued further than the end of the tree, which is
84 * between <now_ms> and <now_ms> + 2^31 ms (now+24days in 32bit).
Willy Tarreau531cf0c2009-03-08 16:35:27 +010085 *
86 * This function should not be used directly, it is meant to be called by the
87 * inline version of task_queue() which performs a few cheap preliminary tests
88 * before deciding to call __task_queue().
Willy Tarreau96bcfd72007-04-29 10:41:56 +020089 */
Willy Tarreau531cf0c2009-03-08 16:35:27 +010090void __task_queue(struct task *task)
Willy Tarreau964c9362007-01-07 00:38:00 +010091{
Willy Tarreau531cf0c2009-03-08 16:35:27 +010092 if (likely(task_in_wq(task)))
Willy Tarreau4726f532009-03-07 17:25:21 +010093 __task_unlink_wq(task);
Willy Tarreau4726f532009-03-07 17:25:21 +010094
95 /* the task is not in the queue now */
Willy Tarreaue35c94a2009-03-21 10:01:42 +010096 task->wq.key = task->expire;
Willy Tarreau28c41a42008-06-29 17:00:59 +020097#ifdef DEBUG_CHECK_INVALID_EXPIRATION_DATES
Willy Tarreaue35c94a2009-03-21 10:01:42 +010098 if (tick_is_lt(task->wq.key, now_ms))
Willy Tarreau28c41a42008-06-29 17:00:59 +020099 /* we're queuing too far away or in the past (most likely) */
Willy Tarreau4726f532009-03-07 17:25:21 +0100100 return;
Willy Tarreau28c41a42008-06-29 17:00:59 +0200101#endif
Willy Tarreauce44f122008-07-05 18:16:19 +0200102
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100103 eb32_insert(&timers, &task->wq);
SaVaGe1d7a4202009-10-06 18:53:37 +0300104
Willy Tarreau4726f532009-03-07 17:25:21 +0100105 return;
Willy Tarreau964c9362007-01-07 00:38:00 +0100106}
107
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200108/*
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200109 * Extract all expired timers from the timer queue, and wakes up all
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +0100110 * associated tasks. Returns the date of next event (or eternity).
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200111 */
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +0100112int wake_expired_tasks()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113{
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200114 struct task *task;
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200115 struct eb32_node *eb;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100117 while (1) {
Emeric Brun01948972017-03-30 15:37:25 +0200118 eb = eb32_lookup_ge(&timers, now_ms - TIMER_LOOK_BACK);
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100119 if (unlikely(!eb)) {
120 /* we might have reached the end of the tree, typically because
121 * <now_ms> is in the first half and we're first scanning the last
122 * half. Let's loop back to the beginning of the tree now.
123 */
124 eb = eb32_first(&timers);
125 if (likely(!eb))
126 break;
127 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200128
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100129 if (likely(tick_is_lt(now_ms, eb->key))) {
130 /* timer not expired yet, revisit it later */
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +0100131 return eb->key;
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100132 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200133
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100134 /* timer looks expired, detach it from the queue */
135 task = eb32_entry(eb, struct task, wq);
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100136 __task_unlink_wq(task);
Willy Tarreau41365222009-03-08 07:46:27 +0100137
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100138 /* It is possible that this task was left at an earlier place in the
139 * tree because a recent call to task_queue() has not moved it. This
140 * happens when the new expiration date is later than the old one.
141 * Since it is very unlikely that we reach a timeout anyway, it's a
142 * lot cheaper to proceed like this because we almost never update
143 * the tree. We may also find disabled expiration dates there. Since
144 * we have detached the task from the tree, we simply call task_queue
Willy Tarreau814c9782009-07-14 23:48:55 +0200145 * to take care of this. Note that we might occasionally requeue it at
146 * the same place, before <eb>, so we have to check if this happens,
147 * and adjust <eb>, otherwise we may skip it which is not what we want.
Willy Tarreau34e98ea2009-08-09 09:09:54 +0200148 * We may also not requeue the task (and not point eb at it) if its
149 * expiration time is not set.
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100150 */
151 if (!tick_is_expired(task->expire, now_ms)) {
Willy Tarreau34e98ea2009-08-09 09:09:54 +0200152 if (!tick_isset(task->expire))
153 continue;
154 __task_queue(task);
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100155 continue;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200156 }
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100157 task_wakeup(task, TASK_WOKEN_TIMER);
158 }
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200159
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +0100160 /* No task is expired */
161 return TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200162}
163
Willy Tarreau58b458d2008-06-29 22:40:23 +0200164/* The run queue is chronologically sorted in a tree. An insertion counter is
165 * used to assign a position to each task. This counter may be combined with
166 * other variables (eg: nice value) to set the final position in the tree. The
167 * counter may wrap without a problem, of course. We then limit the number of
Willy Tarreau91e99932008-06-30 07:51:00 +0200168 * tasks processed at once to 1/4 of the number of tasks in the queue, and to
169 * 200 max in any case, so that general latency remains low and so that task
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100170 * positions have a chance to be considered.
Willy Tarreau58b458d2008-06-29 22:40:23 +0200171 *
172 * The function adjusts <next> if a new event is closer.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200173 */
Thierry FOURNIER9cf7c4b2014-12-15 13:26:01 +0100174void process_runnable_tasks()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200175{
Willy Tarreau964c9362007-01-07 00:38:00 +0100176 struct task *t;
Emeric Brun01948972017-03-30 15:37:25 +0200177 int i;
178 int max_processed;
179 struct eb32_node *rq_next;
180 int rewind;
181 struct task *local_tasks[16];
182 int local_tasks_count;
Christopher Faulet34c5cc92016-12-06 09:15:30 +0100183 tasks_run_queue_cur = tasks_run_queue; /* keep a copy for reporting */
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100184 nb_tasks_cur = nb_tasks;
Christopher Faulet34c5cc92016-12-06 09:15:30 +0100185 max_processed = tasks_run_queue;
Christopher Faulet34c5cc92016-12-06 09:15:30 +0100186 if (!tasks_run_queue)
Willy Tarreau98c61212011-09-10 20:08:49 +0200187 return;
188
Willy Tarreau91e99932008-06-30 07:51:00 +0200189 if (max_processed > 200)
190 max_processed = 200;
191
192 if (likely(niced_tasks))
Willy Tarreau218859a2009-03-21 11:53:09 +0100193 max_processed = (max_processed + 3) / 4;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200194
Emeric Brun01948972017-03-30 15:37:25 +0200195 while (max_processed > 0) {
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100196 /* Note: this loop is one of the fastest code path in
197 * the whole program. It should not be re-arranged
198 * without a good reason.
199 */
Emeric Brun01948972017-03-30 15:37:25 +0200200
201 rewind = 0;
202 rq_next = eb32_lookup_ge(&rqueue, rqueue_ticks - TIMER_LOOK_BACK);
203 if (!rq_next) {
204 /* we might have reached the end of the tree, typically because
205 * <rqueue_ticks> is in the first half and we're first scanning
206 * the last half. Let's loop back to the beginning of the tree now.
207 */
208 rq_next = eb32_first(&rqueue);
Willy Tarreau501260b2015-02-23 16:07:01 +0100209 if (!rq_next) {
Emeric Brun01948972017-03-30 15:37:25 +0200210 break;
211 }
212 rewind = 1;
213 }
214
215 local_tasks_count = 0;
216 while (local_tasks_count < 16) {
217 t = eb32_entry(rq_next, struct task, rq);
218 rq_next = eb32_next(rq_next);
219 /* detach the task from the queue */
220 __task_unlink_rq(t);
221 t->state |= TASK_RUNNING;
222 t->pending_state = 0;
223 t->calls++;
224 local_tasks[local_tasks_count++] = t;
225 if (!rq_next) {
226 if (rewind || !(rq_next = eb32_first(&rqueue))) {
Willy Tarreau501260b2015-02-23 16:07:01 +0100227 break;
Emeric Brun01948972017-03-30 15:37:25 +0200228 }
229 rewind = 1;
Willy Tarreau501260b2015-02-23 16:07:01 +0100230 }
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100231 }
Willy Tarreau58b458d2008-06-29 22:40:23 +0200232
Emeric Brun01948972017-03-30 15:37:25 +0200233 if (!local_tasks_count)
234 break;
Willy Tarreau58b458d2008-06-29 22:40:23 +0200235
Emeric Brun01948972017-03-30 15:37:25 +0200236
237 for (i = 0; i < local_tasks_count ; i++) {
238 t = local_tasks[i];
239 /* This is an optimisation to help the processor's branch
240 * predictor take this most common call.
241 */
242 if (likely(t->process == process_stream))
243 t = process_stream(t);
244 else
245 t = t->process(t);
246 local_tasks[i] = t;
247 }
Willy Tarreau531cf0c2009-03-08 16:35:27 +0100248
Emeric Brun01948972017-03-30 15:37:25 +0200249 max_processed -= local_tasks_count;
250 for (i = 0; i < local_tasks_count ; i++) {
251 t = local_tasks[i];
252 if (likely(t != NULL)) {
253 t->state &= ~TASK_RUNNING;
254 /* If there is a pending state
255 * we have to wake up the task
256 * immediatly, else we defer
257 * it into wait queue
258 */
259 if (t->pending_state)
260 __task_wakeup(t);
261 else
262 task_queue(t);
263 }
Willy Tarreau58b458d2008-06-29 22:40:23 +0200264 }
Willy Tarreaue35c94a2009-03-21 10:01:42 +0100265 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200266}
267
Willy Tarreau4726f532009-03-07 17:25:21 +0100268/* perform minimal intializations, report 0 in case of error, 1 if OK. */
269int init_task()
270{
271 memset(&timers, 0, sizeof(timers));
272 memset(&rqueue, 0, sizeof(rqueue));
273 pool2_task = create_pool("task", sizeof(struct task), MEM_F_SHARED);
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200274 if (!pool2_task)
275 return 0;
276 pool2_notification = create_pool("notification", sizeof(struct notification), MEM_F_SHARED);
277 if (!pool2_notification)
278 return 0;
279 return 1;
Willy Tarreau4726f532009-03-07 17:25:21 +0100280}
281
Willy Tarreaubaaee002006-06-26 02:48:02 +0200282/*
283 * Local variables:
284 * c-indent-level: 8
285 * c-basic-offset: 8
286 * End:
287 */