Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 2 | * 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #ifndef _TYPES_TASK_H |
| 23 | #define _TYPES_TASK_H |
| 24 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 25 | #include <sys/time.h> |
| 26 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 27 | #include <common/config.h> |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 28 | #include <common/mini-clist.h> |
Willy Tarreau | 8d38805 | 2017-11-05 13:34:20 +0100 | [diff] [blame] | 29 | #include <eb32sctree.h> |
Willy Tarreau | 45cb4fb | 2009-10-26 21:10:04 +0100 | [diff] [blame] | 30 | #include <eb32tree.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | |
| 32 | /* values for task->state */ |
Willy Tarreau | f0cea1e | 2018-07-26 16:13:00 +0200 | [diff] [blame] | 33 | #define TASK_SLEEPING 0x0000 /* task sleeping */ |
| 34 | #define TASK_RUNNING 0x0001 /* the task is currently running */ |
Olivier Houchard | 76e4518 | 2018-07-26 16:19:58 +0200 | [diff] [blame] | 35 | #define TASK_GLOBAL 0x0002 /* The task is currently in the global runqueue */ |
Willy Tarreau | 13afcb7 | 2019-01-27 17:41:27 +0100 | [diff] [blame] | 36 | #define TASK_QUEUED 0x0004 /* The task has been (re-)added to the run queue */ |
Willy Tarreau | f0cea1e | 2018-07-26 16:13:00 +0200 | [diff] [blame] | 37 | |
| 38 | #define TASK_WOKEN_INIT 0x0100 /* woken up for initialisation purposes */ |
| 39 | #define TASK_WOKEN_TIMER 0x0200 /* woken up because of expired timer */ |
| 40 | #define TASK_WOKEN_IO 0x0400 /* woken up because of completed I/O */ |
| 41 | #define TASK_WOKEN_SIGNAL 0x0800 /* woken up by a system signal */ |
| 42 | #define TASK_WOKEN_MSG 0x1000 /* woken up by another task's message */ |
| 43 | #define TASK_WOKEN_RES 0x2000 /* woken up because of available resource */ |
| 44 | #define TASK_WOKEN_OTHER 0x4000 /* woken up for an unspecified reason */ |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 45 | |
| 46 | /* use this to check a task state or to clean it up before queueing */ |
| 47 | #define TASK_WOKEN_ANY (TASK_WOKEN_OTHER|TASK_WOKEN_INIT|TASK_WOKEN_TIMER| \ |
| 48 | TASK_WOKEN_IO|TASK_WOKEN_SIGNAL|TASK_WOKEN_MSG| \ |
| 49 | TASK_WOKEN_RES) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 51 | struct notification { |
| 52 | struct list purge_me; /* Part of the list of signals to be purged in the |
| 53 | case of the LUA execution stack crash. */ |
| 54 | struct list wake_me; /* Part of list of signals to be targeted if an |
| 55 | event occurs. */ |
| 56 | struct task *task; /* The task to be wake if an event occurs. */ |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 57 | __decl_hathreads(HA_SPINLOCK_T lock); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 58 | }; |
| 59 | |
Willy Tarreau | 9634e86 | 2019-04-30 14:36:47 +0200 | [diff] [blame] | 60 | /* force to split per-thread stuff into separate cache lines */ |
| 61 | struct task_per_thread { |
| 62 | struct eb_root timers; /* tree constituting the per-thread wait queue */ |
| 63 | struct eb_root rqueue; /* tree constituting the per-thread run queue */ |
| 64 | struct list task_list; /* List of tasks to be run, mixing tasks and tasklets */ |
| 65 | int task_list_size; /* Number of tasks in the task_list */ |
| 66 | int rqueue_size; /* Number of elements in the per-thread run queue */ |
| 67 | __attribute__((aligned(64))) char end[0]; |
| 68 | }; |
| 69 | |
Olivier Houchard | b0bdae7 | 2018-05-18 18:45:28 +0200 | [diff] [blame] | 70 | /* This part is common between struct task and struct tasklet so that tasks |
| 71 | * can be used as-is as tasklets. |
| 72 | */ |
| 73 | #define TASK_COMMON \ |
| 74 | struct { \ |
| 75 | unsigned short state; /* task state : bitfield of TASK_ */ \ |
| 76 | short nice; /* task prio from -1024 to +1024, or -32768 for tasklets */ \ |
| 77 | unsigned int calls; /* number of times process was called */ \ |
Willy Tarreau | 9efd745 | 2018-05-31 14:48:54 +0200 | [diff] [blame] | 78 | uint64_t cpu_time; /* total CPU time consumed */ \ |
Olivier Houchard | b0bdae7 | 2018-05-18 18:45:28 +0200 | [diff] [blame] | 79 | struct task *(*process)(struct task *t, void *ctx, unsigned short state); /* the function which processes the task */ \ |
| 80 | void *context; /* the task's context */ \ |
| 81 | } |
| 82 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 83 | /* The base for all tasks */ |
| 84 | struct task { |
Olivier Houchard | b0bdae7 | 2018-05-18 18:45:28 +0200 | [diff] [blame] | 85 | TASK_COMMON; /* must be at the beginning! */ |
Willy Tarreau | 8d38805 | 2017-11-05 13:34:20 +0100 | [diff] [blame] | 86 | struct eb32sc_node rq; /* ebtree node used to hold the task in the run queue */ |
Willy Tarreau | 05efc0f | 2013-12-07 01:01:39 +0100 | [diff] [blame] | 87 | struct eb32_node wq; /* ebtree node used to hold the task in the wait queue */ |
| 88 | int expire; /* next expiration date for this task, in ticks */ |
Willy Tarreau | f65610a | 2017-10-31 16:06:06 +0100 | [diff] [blame] | 89 | unsigned long thread_mask; /* mask of thread IDs authorized to process the task */ |
Willy Tarreau | 9efd745 | 2018-05-31 14:48:54 +0200 | [diff] [blame] | 90 | uint64_t call_date; /* date of the last task wakeup or call */ |
| 91 | uint64_t lat_time; /* total latency time experienced */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 92 | }; |
| 93 | |
Olivier Houchard | b0bdae7 | 2018-05-18 18:45:28 +0200 | [diff] [blame] | 94 | /* lightweight tasks, without priority, mainly used for I/Os */ |
| 95 | struct tasklet { |
| 96 | TASK_COMMON; /* must be at the beginning! */ |
| 97 | struct list list; |
| 98 | }; |
| 99 | |
| 100 | #define TASK_IS_TASKLET(t) ((t)->nice == -32768) |
| 101 | |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 102 | /* |
| 103 | * The task callback (->process) is responsible for updating ->expire. It must |
| 104 | * return a pointer to the task itself, except if the task has been deleted, in |
| 105 | * which case it returns NULL so that the scheduler knows it must not check the |
| 106 | * expire timer. The scheduler will requeue the task at the proper location. |
| 107 | */ |
| 108 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 109 | #endif /* _TYPES_TASK_H */ |
| 110 | |
| 111 | /* |
| 112 | * Local variables: |
| 113 | * c-indent-level: 8 |
| 114 | * c-basic-offset: 8 |
| 115 | * End: |
| 116 | */ |