blob: 6ca97671f01bc31de34b984b50eb037acf7e676d [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002 * include/types/task.h
3 * Macros, variables and structures for task management.
4 *
5 * Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#ifndef _TYPES_TASK_H
23#define _TYPES_TASK_H
24
Willy Tarreaubaaee002006-06-26 02:48:02 +020025#include <sys/time.h>
26
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020027#include <common/config.h>
Willy Tarreau96bcfd72007-04-29 10:41:56 +020028#include <common/mini-clist.h>
Willy Tarreau8d388052017-11-05 13:34:20 +010029#include <eb32sctree.h>
Willy Tarreau45cb4fb2009-10-26 21:10:04 +010030#include <eb32tree.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
32/* values for task->state */
Willy Tarreauf0cea1e2018-07-26 16:13:00 +020033#define TASK_SLEEPING 0x0000 /* task sleeping */
34#define TASK_RUNNING 0x0001 /* the task is currently running */
Olivier Houchard76e45182018-07-26 16:19:58 +020035#define TASK_GLOBAL 0x0002 /* The task is currently in the global runqueue */
Willy Tarreau13afcb72019-01-27 17:41:27 +010036#define TASK_QUEUED 0x0004 /* The task has been (re-)added to the run queue */
Willy Tarreaudd0e89a2019-12-19 07:39:06 +010037#define TASK_SHARED_WQ 0x0008 /* The task's expiration may be updated by other
38 * threads, must be set before first queue/wakeup */
Willy Tarreaubb238832020-01-31 10:48:10 +010039#define TASK_SELF_WAKING 0x0010 /* task/tasklet found waking itself */
Willy Tarreauf0cea1e2018-07-26 16:13:00 +020040
41#define TASK_WOKEN_INIT 0x0100 /* woken up for initialisation purposes */
42#define TASK_WOKEN_TIMER 0x0200 /* woken up because of expired timer */
43#define TASK_WOKEN_IO 0x0400 /* woken up because of completed I/O */
44#define TASK_WOKEN_SIGNAL 0x0800 /* woken up by a system signal */
45#define TASK_WOKEN_MSG 0x1000 /* woken up by another task's message */
46#define TASK_WOKEN_RES 0x2000 /* woken up because of available resource */
47#define TASK_WOKEN_OTHER 0x4000 /* woken up for an unspecified reason */
Willy Tarreaufdccded2008-08-29 18:19:04 +020048
49/* use this to check a task state or to clean it up before queueing */
50#define TASK_WOKEN_ANY (TASK_WOKEN_OTHER|TASK_WOKEN_INIT|TASK_WOKEN_TIMER| \
51 TASK_WOKEN_IO|TASK_WOKEN_SIGNAL|TASK_WOKEN_MSG| \
52 TASK_WOKEN_RES)
Willy Tarreaubaaee002006-06-26 02:48:02 +020053
Willy Tarreaua62917b2020-01-30 18:37:28 +010054enum {
55 TL_URGENT = 0, /* urgent tasklets (I/O callbacks) */
56 TL_NORMAL = 1, /* normal tasks */
57 TL_BULK = 2, /* bulk task/tasklets, streaming I/Os */
58 TL_CLASSES /* must be last */
59};
60
Thierry FOURNIERd6975962017-07-12 14:31:10 +020061struct notification {
62 struct list purge_me; /* Part of the list of signals to be purged in the
63 case of the LUA execution stack crash. */
64 struct list wake_me; /* Part of list of signals to be targeted if an
65 event occurs. */
66 struct task *task; /* The task to be wake if an event occurs. */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010067 __decl_hathreads(HA_SPINLOCK_T lock);
Thierry FOURNIERd6975962017-07-12 14:31:10 +020068};
69
Willy Tarreau9634e862019-04-30 14:36:47 +020070/* force to split per-thread stuff into separate cache lines */
71struct task_per_thread {
72 struct eb_root timers; /* tree constituting the per-thread wait queue */
73 struct eb_root rqueue; /* tree constituting the per-thread run queue */
Olivier Houchard06910462019-10-11 16:35:01 +020074 struct mt_list shared_tasklet_list; /* Tasklet to be run, woken up by other threads */
Willy Tarreaua62917b2020-01-30 18:37:28 +010075 struct list tasklets[TL_CLASSES]; /* tasklets (and/or tasks) to run, by class */
76 int task_list_size; /* Number of tasks among the tasklets */
Willy Tarreau9634e862019-04-30 14:36:47 +020077 int rqueue_size; /* Number of elements in the per-thread run queue */
Willy Tarreaud022e9c2019-09-24 08:25:15 +020078 struct task *current; /* current task (not tasklet) */
Willy Tarreau9634e862019-04-30 14:36:47 +020079 __attribute__((aligned(64))) char end[0];
80};
81
Olivier Houchardb0bdae72018-05-18 18:45:28 +020082/* This part is common between struct task and struct tasklet so that tasks
83 * can be used as-is as tasklets.
84 */
85#define TASK_COMMON \
86 struct { \
87 unsigned short state; /* task state : bitfield of TASK_ */ \
88 short nice; /* task prio from -1024 to +1024, or -32768 for tasklets */ \
89 unsigned int calls; /* number of times process was called */ \
90 struct task *(*process)(struct task *t, void *ctx, unsigned short state); /* the function which processes the task */ \
91 void *context; /* the task's context */ \
92 }
93
Willy Tarreaubaaee002006-06-26 02:48:02 +020094/* The base for all tasks */
95struct task {
Olivier Houchardb0bdae72018-05-18 18:45:28 +020096 TASK_COMMON; /* must be at the beginning! */
Willy Tarreau8d388052017-11-05 13:34:20 +010097 struct eb32sc_node rq; /* ebtree node used to hold the task in the run queue */
Willy Tarreau05efc0f2013-12-07 01:01:39 +010098 struct eb32_node wq; /* ebtree node used to hold the task in the wait queue */
99 int expire; /* next expiration date for this task, in ticks */
Willy Tarreauf65610a2017-10-31 16:06:06 +0100100 unsigned long thread_mask; /* mask of thread IDs authorized to process the task */
Willy Tarreau9efd7452018-05-31 14:48:54 +0200101 uint64_t call_date; /* date of the last task wakeup or call */
102 uint64_t lat_time; /* total latency time experienced */
Willy Tarreau247a8b12019-08-08 10:09:08 +0200103 uint64_t cpu_time; /* total CPU time consumed */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104};
105
Olivier Houchardb0bdae72018-05-18 18:45:28 +0200106/* lightweight tasks, without priority, mainly used for I/Os */
107struct tasklet {
108 TASK_COMMON; /* must be at the beginning! */
Olivier Houchard06910462019-10-11 16:35:01 +0200109 struct list list;
Willy Tarreau8cdc1672019-10-18 06:43:53 +0200110 int tid; /* TID of the tasklet owner, <0 if local */
Olivier Houchardb0bdae72018-05-18 18:45:28 +0200111};
112
113#define TASK_IS_TASKLET(t) ((t)->nice == -32768)
114
Willy Tarreau26c25062009-03-08 09:38:41 +0100115/*
116 * The task callback (->process) is responsible for updating ->expire. It must
117 * return a pointer to the task itself, except if the task has been deleted, in
118 * which case it returns NULL so that the scheduler knows it must not check the
119 * expire timer. The scheduler will requeue the task at the proper location.
120 */
121
Willy Tarreau64e60122019-07-12 08:31:17 +0200122
123/* A work_list is a thread-safe way to enqueue some work to be run on another
124 * thread. It consists of a list, a task and a general-purpose argument.
125 * A work is appended to the list by atomically adding a list element to the
126 * list and waking up the associated task, which is done using work_add(). The
127 * caller must be careful about how operations are run as it will definitely
128 * happen that the element being enqueued is processed by the other thread
129 * before the call returns. Some locking conventions between the caller and the
130 * callee might sometimes be necessary. The task is always woken up with reason
131 * TASK_WOKEN_OTHER and a context pointing to the work_list entry.
132 */
133struct work_list {
Olivier Houchard859dc802019-08-08 15:47:21 +0200134 struct mt_list head;
Willy Tarreau64e60122019-07-12 08:31:17 +0200135 struct task *task;
136 void *arg;
137};
138
Willy Tarreaubaaee002006-06-26 02:48:02 +0200139#endif /* _TYPES_TASK_H */
140
141/*
142 * Local variables:
143 * c-indent-level: 8
144 * c-basic-offset: 8
145 * End:
146 */