2006-06-25 20:48:02 -04:00
|
|
|
/*
|
2010-08-27 11:56:48 -04:00
|
|
|
* include/types/task.h
|
|
|
|
|
* Macros, variables and structures for task management.
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation, version 2.1
|
|
|
|
|
* exclusively.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
*/
|
2006-06-25 20:48:02 -04:00
|
|
|
|
|
|
|
|
#ifndef _TYPES_TASK_H
|
|
|
|
|
#define _TYPES_TASK_H
|
|
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
2006-06-29 12:54:54 -04:00
|
|
|
#include <common/config.h>
|
2007-04-29 04:41:56 -04:00
|
|
|
#include <common/mini-clist.h>
|
MAJOR: task: make use of the scope-aware ebtree functions
Currently the task scheduler suffers from an O(n) lookup when
skipping tasks that are not for the current thread. The reason
is that eb32_lookup_ge() has no information about the current
thread so it always revisits many tasks for other threads before
finding its own tasks.
This is particularly visible with HTTP/2 since the number of
concurrent streams created at once causes long series of tasks
for the same stream in the scheduler. With only 10 connections
and 100 streams each, by running on two threads, the performance
drops from 640kreq/s to 11.2kreq/s! Lookup metrics show that for
only 200000 task lookups, 430 million skips had to be performed,
which means that on average, each lookup leads to 2150 nodes to
be visited.
This commit backports the principle of scope lookups for ebtrees
from the ebtree_v7 development tree. The idea is that each node
contains a mask indicating the union of the scopes for the nodes
below it, which is fed during insertion, and used during lookups.
Then during lookups, branches that do not contain any leaf matching
the requested scope are simply ignored. This perfectly matches a
thread mask, allowing a thread to only extract the tasks it cares
about from the run queue, and to always find them in O(log(n))
instead of O(n). Thus the scheduler uses tid_bit and
task->thread_mask as the ebtree scope here.
Doing this has recovered most of the performance, as can be seen on
the test below with two threads, 10 connections, 100 streams each,
and 1 million requests total :
Before After Gain
test duration : 89.6s 4.73s x19
HTTP requests/s (DEBUG) : 11200 211300 x19
HTTP requests/s (PROD) : 15900 447000 x28
spin_lock time : 85.2s 0.46s /185
time per lookup : 13us 40ns /325
Even when going to 6 threads (on 3 hyperthreaded CPU cores), the
performance stays around 284000 req/s, showing that the contention
is much lower.
A test showed that there's no benefit in using this for the wait queue
though.
2017-11-05 07:34:20 -05:00
|
|
|
#include <eb32sctree.h>
|
2009-10-26 16:10:04 -04:00
|
|
|
#include <eb32tree.h>
|
2006-06-25 20:48:02 -04:00
|
|
|
|
|
|
|
|
/* values for task->state */
|
2018-07-26 10:13:00 -04:00
|
|
|
#define TASK_SLEEPING 0x0000 /* task sleeping */
|
|
|
|
|
#define TASK_RUNNING 0x0001 /* the task is currently running */
|
2018-07-26 10:19:58 -04:00
|
|
|
#define TASK_GLOBAL 0x0002 /* The task is currently in the global runqueue */
|
2018-07-26 10:13:00 -04:00
|
|
|
|
|
|
|
|
#define TASK_WOKEN_INIT 0x0100 /* woken up for initialisation purposes */
|
|
|
|
|
#define TASK_WOKEN_TIMER 0x0200 /* woken up because of expired timer */
|
|
|
|
|
#define TASK_WOKEN_IO 0x0400 /* woken up because of completed I/O */
|
|
|
|
|
#define TASK_WOKEN_SIGNAL 0x0800 /* woken up by a system signal */
|
|
|
|
|
#define TASK_WOKEN_MSG 0x1000 /* woken up by another task's message */
|
|
|
|
|
#define TASK_WOKEN_RES 0x2000 /* woken up because of available resource */
|
|
|
|
|
#define TASK_WOKEN_OTHER 0x4000 /* woken up for an unspecified reason */
|
2008-08-29 12:19:04 -04:00
|
|
|
|
|
|
|
|
/* use this to check a task state or to clean it up before queueing */
|
|
|
|
|
#define TASK_WOKEN_ANY (TASK_WOKEN_OTHER|TASK_WOKEN_INIT|TASK_WOKEN_TIMER| \
|
|
|
|
|
TASK_WOKEN_IO|TASK_WOKEN_SIGNAL|TASK_WOKEN_MSG| \
|
|
|
|
|
TASK_WOKEN_RES)
|
2006-06-25 20:48:02 -04:00
|
|
|
|
2017-07-12 08:31:10 -04:00
|
|
|
struct notification {
|
|
|
|
|
struct list purge_me; /* Part of the list of signals to be purged in the
|
|
|
|
|
case of the LUA execution stack crash. */
|
|
|
|
|
struct list wake_me; /* Part of list of signals to be targeted if an
|
|
|
|
|
event occurs. */
|
|
|
|
|
struct task *task; /* The task to be wake if an event occurs. */
|
2017-11-13 04:34:01 -05:00
|
|
|
__decl_hathreads(HA_SPINLOCK_T lock);
|
2017-07-12 08:31:10 -04:00
|
|
|
};
|
|
|
|
|
|
2018-05-18 12:45:28 -04:00
|
|
|
/* This part is common between struct task and struct tasklet so that tasks
|
|
|
|
|
* can be used as-is as tasklets.
|
|
|
|
|
*/
|
|
|
|
|
#define TASK_COMMON \
|
|
|
|
|
struct { \
|
|
|
|
|
unsigned short state; /* task state : bitfield of TASK_ */ \
|
|
|
|
|
short nice; /* task prio from -1024 to +1024, or -32768 for tasklets */ \
|
|
|
|
|
unsigned int calls; /* number of times process was called */ \
|
2018-05-31 08:48:54 -04:00
|
|
|
uint64_t cpu_time; /* total CPU time consumed */ \
|
2018-05-18 12:45:28 -04:00
|
|
|
struct task *(*process)(struct task *t, void *ctx, unsigned short state); /* the function which processes the task */ \
|
|
|
|
|
void *context; /* the task's context */ \
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-25 20:48:02 -04:00
|
|
|
/* The base for all tasks */
|
|
|
|
|
struct task {
|
2018-05-18 12:45:28 -04:00
|
|
|
TASK_COMMON; /* must be at the beginning! */
|
MAJOR: task: make use of the scope-aware ebtree functions
Currently the task scheduler suffers from an O(n) lookup when
skipping tasks that are not for the current thread. The reason
is that eb32_lookup_ge() has no information about the current
thread so it always revisits many tasks for other threads before
finding its own tasks.
This is particularly visible with HTTP/2 since the number of
concurrent streams created at once causes long series of tasks
for the same stream in the scheduler. With only 10 connections
and 100 streams each, by running on two threads, the performance
drops from 640kreq/s to 11.2kreq/s! Lookup metrics show that for
only 200000 task lookups, 430 million skips had to be performed,
which means that on average, each lookup leads to 2150 nodes to
be visited.
This commit backports the principle of scope lookups for ebtrees
from the ebtree_v7 development tree. The idea is that each node
contains a mask indicating the union of the scopes for the nodes
below it, which is fed during insertion, and used during lookups.
Then during lookups, branches that do not contain any leaf matching
the requested scope are simply ignored. This perfectly matches a
thread mask, allowing a thread to only extract the tasks it cares
about from the run queue, and to always find them in O(log(n))
instead of O(n). Thus the scheduler uses tid_bit and
task->thread_mask as the ebtree scope here.
Doing this has recovered most of the performance, as can be seen on
the test below with two threads, 10 connections, 100 streams each,
and 1 million requests total :
Before After Gain
test duration : 89.6s 4.73s x19
HTTP requests/s (DEBUG) : 11200 211300 x19
HTTP requests/s (PROD) : 15900 447000 x28
spin_lock time : 85.2s 0.46s /185
time per lookup : 13us 40ns /325
Even when going to 6 threads (on 3 hyperthreaded CPU cores), the
performance stays around 284000 req/s, showing that the contention
is much lower.
A test showed that there's no benefit in using this for the wait queue
though.
2017-11-05 07:34:20 -05:00
|
|
|
struct eb32sc_node rq; /* ebtree node used to hold the task in the run queue */
|
2013-12-06 19:01:39 -05:00
|
|
|
struct eb32_node wq; /* ebtree node used to hold the task in the wait queue */
|
|
|
|
|
int expire; /* next expiration date for this task, in ticks */
|
2017-10-31 11:06:06 -04:00
|
|
|
unsigned long thread_mask; /* mask of thread IDs authorized to process the task */
|
2018-05-31 08:48:54 -04:00
|
|
|
uint64_t call_date; /* date of the last task wakeup or call */
|
|
|
|
|
uint64_t lat_time; /* total latency time experienced */
|
2006-06-25 20:48:02 -04:00
|
|
|
};
|
|
|
|
|
|
2018-05-18 12:45:28 -04:00
|
|
|
/* lightweight tasks, without priority, mainly used for I/Os */
|
|
|
|
|
struct tasklet {
|
|
|
|
|
TASK_COMMON; /* must be at the beginning! */
|
|
|
|
|
struct list list;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define TASK_IS_TASKLET(t) ((t)->nice == -32768)
|
|
|
|
|
|
2009-03-08 04:38:41 -04:00
|
|
|
/*
|
|
|
|
|
* The task callback (->process) is responsible for updating ->expire. It must
|
|
|
|
|
* return a pointer to the task itself, except if the task has been deleted, in
|
|
|
|
|
* which case it returns NULL so that the scheduler knows it must not check the
|
|
|
|
|
* expire timer. The scheduler will requeue the task at the proper location.
|
|
|
|
|
*/
|
|
|
|
|
|
2006-06-25 20:48:02 -04:00
|
|
|
#endif /* _TYPES_TASK_H */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Local variables:
|
|
|
|
|
* c-indent-level: 8
|
|
|
|
|
* c-basic-offset: 8
|
|
|
|
|
* End:
|
|
|
|
|
*/
|