Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | #ifndef included_vlib_threads_h |
| 16 | #define included_vlib_threads_h |
| 17 | |
| 18 | #include <vlib/main.h> |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 19 | #include <vppinfra/callback.h> |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 20 | #include <linux/sched.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 21 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 22 | void vlib_set_thread_name (char *name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 23 | |
| 24 | /* arg is actually a vlib__thread_t * */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 25 | typedef void (vlib_thread_function_t) (void *arg); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 26 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 27 | typedef struct vlib_thread_registration_ |
| 28 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 29 | /* constructor generated list of thread registrations */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 30 | struct vlib_thread_registration_ *next; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 31 | |
| 32 | /* config parameters */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 33 | char *name; |
| 34 | char *short_name; |
| 35 | vlib_thread_function_t *function; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 36 | uword mheap_size; |
| 37 | int fixed_count; |
| 38 | u32 count; |
| 39 | int no_data_structure_clone; |
Bud Grise | 68adab9 | 2016-02-12 10:36:11 -0500 | [diff] [blame] | 40 | u32 frame_queue_nelts; |
| 41 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 42 | /* All threads of this type run on pthreads */ |
| 43 | int use_pthreads; |
| 44 | u32 first_index; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 45 | uword *coremask; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | } vlib_thread_registration_t; |
| 47 | |
Damjan Marion | 3f46baf | 2016-02-06 19:16:21 +0100 | [diff] [blame] | 48 | /* |
| 49 | * Frames have their cpu / vlib_main_t index in the low-order N bits |
| 50 | * Make VLIB_MAX_CPUS a power-of-two, please... |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | */ |
Damjan Marion | 3f46baf | 2016-02-06 19:16:21 +0100 | [diff] [blame] | 52 | |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 53 | #ifndef VLIB_MAX_CPUS |
Damjan Marion | 3f46baf | 2016-02-06 19:16:21 +0100 | [diff] [blame] | 54 | #define VLIB_MAX_CPUS 256 |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 55 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | |
Damjan Marion | ce8debf | 2016-02-06 19:16:21 +0100 | [diff] [blame] | 57 | #if VLIB_MAX_CPUS > CLIB_MAX_MHEAPS |
| 58 | #error Please increase number of per-cpu mheaps |
| 59 | #endif |
| 60 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 61 | #define VLIB_CPU_MASK (VLIB_MAX_CPUS - 1) /* 0x3f, max */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | #define VLIB_OFFSET_MASK (~VLIB_CPU_MASK) |
| 63 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 64 | #define VLIB_LOG2_THREAD_STACK_SIZE (21) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | #define VLIB_THREAD_STACK_SIZE (1<<VLIB_LOG2_THREAD_STACK_SIZE) |
| 66 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 67 | typedef struct |
| 68 | { |
Damjan Marion | b0d680b | 2016-11-04 14:41:44 +0100 | [diff] [blame] | 69 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | volatile u32 valid; |
Damjan Marion | c0d9ca7 | 2021-05-11 09:39:24 +0200 | [diff] [blame] | 71 | u32 maybe_trace : 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | u32 n_vectors; |
Damjan Marion | c0d9ca7 | 2021-05-11 09:39:24 +0200 | [diff] [blame] | 73 | u32 offset; |
| 74 | STRUCT_MARK (end_of_reset); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | |
Damjan Marion | c0d9ca7 | 2021-05-11 09:39:24 +0200 | [diff] [blame] | 76 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | u32 buffer_index[VLIB_FRAME_SIZE]; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 78 | } |
| 79 | vlib_frame_queue_elt_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 81 | typedef struct |
| 82 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | /* First cache line */ |
Damjan Marion | b0d680b | 2016-11-04 14:41:44 +0100 | [diff] [blame] | 84 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | volatile u32 *wait_at_barrier; |
| 86 | volatile u32 *workers_at_barrier; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | |
| 88 | /* Second Cache Line */ |
Damjan Marion | b0d680b | 2016-11-04 14:41:44 +0100 | [diff] [blame] | 89 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | void *thread_mheap; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 91 | u8 *thread_stack; |
| 92 | void (*thread_function) (void *); |
| 93 | void *thread_function_arg; |
| 94 | i64 recursion_level; |
| 95 | elog_track_t elog_track; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | u32 instance_id; |
| 97 | vlib_thread_registration_t *registration; |
| 98 | u8 *name; |
Bud Grise | 42f2006 | 2016-03-16 13:09:46 -0400 | [diff] [blame] | 99 | u64 barrier_sync_count; |
Dave Barach | 88c6e00 | 2018-09-30 15:54:06 -0400 | [diff] [blame] | 100 | u8 barrier_elog_enabled; |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 101 | const char *barrier_caller; |
| 102 | const char *barrier_context; |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 103 | volatile u32 *node_reforks_required; |
Florin Coras | 4b20830 | 2022-03-30 13:50:19 -0700 | [diff] [blame] | 104 | volatile u32 wait_before_barrier; |
| 105 | volatile u32 workers_before_barrier; |
| 106 | volatile u32 done_work_before_barrier; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | |
| 108 | long lwp; |
Mohsin Kazmi | 5d64c78 | 2018-09-11 20:27:09 +0200 | [diff] [blame] | 109 | int cpu_id; |
| 110 | int core_id; |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 111 | int numa_id; |
Pavel Kotucek | 9876520 | 2016-10-07 08:37:28 +0200 | [diff] [blame] | 112 | pthread_t thread_id; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | } vlib_worker_thread_t; |
| 114 | |
Damjan Marion | 6a7acc2 | 2016-12-19 16:28:36 +0100 | [diff] [blame] | 115 | extern vlib_worker_thread_t *vlib_worker_threads; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 117 | typedef struct |
| 118 | { |
Damjan Marion | c0d9ca7 | 2021-05-11 09:39:24 +0200 | [diff] [blame] | 119 | /* static data */ |
Damjan Marion | b0d680b | 2016-11-04 14:41:44 +0100 | [diff] [blame] | 120 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | vlib_frame_queue_elt_t *elts; |
Damjan Marion | c0d9ca7 | 2021-05-11 09:39:24 +0200 | [diff] [blame] | 122 | u64 vector_threshold; |
| 123 | u64 trace; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | u32 nelts; |
Damjan Marion | c0d9ca7 | 2021-05-11 09:39:24 +0200 | [diff] [blame] | 125 | |
| 126 | /* modified by enqueue side */ |
| 127 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline1); |
| 128 | volatile u64 tail; |
| 129 | |
| 130 | /* modified by dequeue side */ |
| 131 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline2); |
| 132 | volatile u64 head; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 133 | } |
| 134 | vlib_frame_queue_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 136 | typedef struct |
| 137 | { |
| 138 | u32 node_index; |
Damjan Marion | 78fd7e8 | 2018-07-20 18:47:05 +0200 | [diff] [blame] | 139 | u32 frame_queue_nelts; |
Damjan Marion | 78fd7e8 | 2018-07-20 18:47:05 +0200 | [diff] [blame] | 140 | |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 141 | vlib_frame_queue_t **vlib_frame_queues; |
| 142 | |
| 143 | /* for frame queue tracing */ |
| 144 | frame_queue_trace_t *frame_queue_traces; |
| 145 | frame_queue_nelt_counter_t *frame_queue_histogram; |
| 146 | } vlib_frame_queue_main_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 147 | |
Dave Barach | 69128d0 | 2017-09-26 10:54:34 -0400 | [diff] [blame] | 148 | typedef struct |
| 149 | { |
| 150 | uword node_index; |
| 151 | uword type_opaque; |
| 152 | uword data; |
| 153 | } vlib_process_signal_event_mt_args_t; |
| 154 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 155 | /* Called early, in thread 0's context */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 156 | clib_error_t *vlib_thread_init (vlib_main_t * vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 158 | void vlib_worker_thread_node_runtime_update (void); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 160 | void vlib_create_worker_threads (vlib_main_t * vm, int n, |
| 161 | void (*thread_function) (void *)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 162 | |
| 163 | void vlib_worker_thread_init (vlib_worker_thread_t * w); |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 164 | u32 vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | |
| 166 | /* Check for a barrier sync request every 30ms */ |
| 167 | #define BARRIER_SYNC_DELAY (0.030000) |
| 168 | |
| 169 | #if CLIB_DEBUG > 0 |
| 170 | /* long barrier timeout, for gdb... */ |
| 171 | #define BARRIER_SYNC_TIMEOUT (600.1) |
| 172 | #else |
| 173 | #define BARRIER_SYNC_TIMEOUT (1.0) |
| 174 | #endif |
| 175 | |
Damjan Marion | 8343ee5 | 2019-02-26 17:15:48 +0100 | [diff] [blame] | 176 | #define vlib_worker_thread_barrier_sync(X) {vlib_worker_thread_barrier_sync_int(X, __FUNCTION__);} |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 177 | |
Damjan Marion | 8343ee5 | 2019-02-26 17:15:48 +0100 | [diff] [blame] | 178 | void vlib_worker_thread_barrier_sync_int (vlib_main_t * vm, |
| 179 | const char *func_name); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 180 | void vlib_worker_thread_barrier_release (vlib_main_t * vm); |
Neale Ranns | 42845dd | 2020-05-26 13:12:17 +0000 | [diff] [blame] | 181 | u8 vlib_worker_thread_barrier_held (void); |
Dave Barach | c602b38 | 2019-06-03 19:48:22 -0400 | [diff] [blame] | 182 | void vlib_worker_thread_initial_barrier_sync_and_release (vlib_main_t * vm); |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 183 | void vlib_worker_thread_node_refork (void); |
Neale Ranns | 42845dd | 2020-05-26 13:12:17 +0000 | [diff] [blame] | 184 | /** |
| 185 | * Wait until each of the workers has been once around the track |
| 186 | */ |
| 187 | void vlib_worker_wait_one_loop (void); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 189 | static_always_inline uword |
| 190 | vlib_get_thread_index (void) |
| 191 | { |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 192 | return __os_thread_index; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 195 | always_inline void |
| 196 | vlib_smp_unsafe_warning (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | { |
| 198 | if (CLIB_DEBUG > 0) |
| 199 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 200 | if (vlib_get_thread_index ()) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 201 | fformat (stderr, "%s: SMP unsafe warning...\n", __FUNCTION__); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Damjan Marion | 1d429df | 2021-04-14 19:07:13 +0200 | [diff] [blame] | 205 | always_inline int |
| 206 | __foreach_vlib_main_helper (vlib_main_t *ii, vlib_main_t **p) |
| 207 | { |
| 208 | vlib_main_t *vm; |
| 209 | u32 index = ii - (vlib_main_t *) 0; |
| 210 | |
| 211 | if (index >= vec_len (vlib_global_main.vlib_mains)) |
| 212 | return 0; |
| 213 | |
| 214 | *p = vm = vlib_global_main.vlib_mains[index]; |
| 215 | ASSERT (index == 0 || vm->parked_at_barrier == 1); |
| 216 | return 1; |
| 217 | } |
| 218 | |
Damjan Marion | 92ccf9b | 2021-03-26 11:38:01 +0100 | [diff] [blame] | 219 | #define foreach_vlib_main() \ |
Damjan Marion | 1d429df | 2021-04-14 19:07:13 +0200 | [diff] [blame] | 220 | for (vlib_main_t *ii = 0, *this_vlib_main; \ |
| 221 | __foreach_vlib_main_helper (ii, &this_vlib_main); ii++) \ |
| 222 | if (this_vlib_main) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 223 | |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 224 | #define foreach_sched_policy \ |
| 225 | _(SCHED_OTHER, OTHER, "other") \ |
| 226 | _(SCHED_BATCH, BATCH, "batch") \ |
| 227 | _(SCHED_IDLE, IDLE, "idle") \ |
| 228 | _(SCHED_FIFO, FIFO, "fifo") \ |
| 229 | _(SCHED_RR, RR, "rr") |
| 230 | |
| 231 | typedef enum |
| 232 | { |
| 233 | #define _(v,f,s) SCHED_POLICY_##f = v, |
| 234 | foreach_sched_policy |
| 235 | #undef _ |
| 236 | SCHED_POLICY_N, |
| 237 | } sched_policy_t; |
| 238 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 239 | typedef struct |
| 240 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | /* Link list of registrations, built by constructors */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 242 | vlib_thread_registration_t *next; |
| 243 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 244 | /* Vector of registrations, w/ non-data-structure clones at the top */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 245 | vlib_thread_registration_t **registrations; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 246 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 247 | uword *thread_registrations_by_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 248 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 249 | vlib_worker_thread_t *worker_threads; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 250 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | int use_pthreads; |
| 252 | |
| 253 | /* Number of vlib_main / vnet_main clones */ |
| 254 | u32 n_vlib_mains; |
| 255 | |
| 256 | /* Number of thread stacks to create */ |
| 257 | u32 n_thread_stacks; |
| 258 | |
| 259 | /* Number of pthreads */ |
| 260 | u32 n_pthreads; |
| 261 | |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 262 | /* Number of threads */ |
| 263 | u32 n_threads; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 264 | |
| 265 | /* Number of cores to skip, must match the core mask */ |
| 266 | u32 skip_cores; |
| 267 | |
| 268 | /* Thread prefix name */ |
| 269 | u8 *thread_prefix; |
| 270 | |
| 271 | /* main thread lcore */ |
Damjan Marion | 858151f | 2018-07-11 10:51:00 +0200 | [diff] [blame] | 272 | u32 main_lcore; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 273 | |
| 274 | /* Bitmap of available CPU cores */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 275 | uword *cpu_core_bitmap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 276 | |
| 277 | /* Bitmap of available CPU sockets (NUMA nodes) */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 278 | uword *cpu_socket_bitmap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 280 | /* Worker handoff queues */ |
| 281 | vlib_frame_queue_main_t *frame_queue_mains; |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 282 | |
| 283 | /* worker thread initialization barrier */ |
| 284 | volatile u32 worker_thread_release; |
| 285 | |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 286 | /* scheduling policy */ |
| 287 | u32 sched_policy; |
| 288 | |
| 289 | /* scheduling policy priority */ |
| 290 | u32 sched_priority; |
| 291 | |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 292 | /* NUMA-bound heap size */ |
| 293 | uword numa_heap_size; |
| 294 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 295 | } vlib_thread_main_t; |
| 296 | |
Damjan Marion | 6a7acc2 | 2016-12-19 16:28:36 +0100 | [diff] [blame] | 297 | extern vlib_thread_main_t vlib_thread_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 298 | |
Damjan Marion | ce359db | 2017-03-16 16:15:38 +0100 | [diff] [blame] | 299 | #include <vlib/global_funcs.h> |
| 300 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 301 | #define VLIB_REGISTER_THREAD(x,...) \ |
| 302 | __VA_ARGS__ vlib_thread_registration_t x; \ |
| 303 | static void __vlib_add_thread_registration_##x (void) \ |
| 304 | __attribute__((__constructor__)) ; \ |
| 305 | static void __vlib_add_thread_registration_##x (void) \ |
| 306 | { \ |
| 307 | vlib_thread_main_t * tm = &vlib_thread_main; \ |
| 308 | x.next = tm->next; \ |
| 309 | tm->next = &x; \ |
| 310 | } \ |
Damjan Marion | 72d2c4f | 2018-04-05 21:32:29 +0200 | [diff] [blame] | 311 | static void __vlib_rm_thread_registration_##x (void) \ |
| 312 | __attribute__((__destructor__)) ; \ |
| 313 | static void __vlib_rm_thread_registration_##x (void) \ |
| 314 | { \ |
| 315 | vlib_thread_main_t * tm = &vlib_thread_main; \ |
| 316 | VLIB_REMOVE_FROM_LINKED_LIST (tm->next, &x, next); \ |
| 317 | } \ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 318 | __VA_ARGS__ vlib_thread_registration_t x |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 319 | |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 320 | always_inline u32 |
| 321 | vlib_num_workers () |
| 322 | { |
| 323 | return vlib_thread_main.n_vlib_mains - 1; |
| 324 | } |
| 325 | |
| 326 | always_inline u32 |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 327 | vlib_get_worker_thread_index (u32 worker_index) |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 328 | { |
| 329 | return worker_index + 1; |
| 330 | } |
| 331 | |
Damjan Marion | 3a4ed39 | 2016-11-08 13:20:42 +0100 | [diff] [blame] | 332 | always_inline u32 |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 333 | vlib_get_worker_index (u32 thread_index) |
Damjan Marion | 3a4ed39 | 2016-11-08 13:20:42 +0100 | [diff] [blame] | 334 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 335 | return thread_index - 1; |
Damjan Marion | 3a4ed39 | 2016-11-08 13:20:42 +0100 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | always_inline u32 |
| 339 | vlib_get_current_worker_index () |
| 340 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 341 | return vlib_get_thread_index () - 1; |
Damjan Marion | 3a4ed39 | 2016-11-08 13:20:42 +0100 | [diff] [blame] | 342 | } |
| 343 | |
Damjan Marion | ce359db | 2017-03-16 16:15:38 +0100 | [diff] [blame] | 344 | static inline void |
| 345 | vlib_worker_thread_barrier_check (void) |
| 346 | { |
| 347 | if (PREDICT_FALSE (*vlib_worker_threads->wait_at_barrier)) |
| 348 | { |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 349 | vlib_global_main_t *vgm = vlib_get_global_main (); |
Dave Barach | 88c6e00 | 2018-09-30 15:54:06 -0400 | [diff] [blame] | 350 | vlib_main_t *vm = vlib_get_main (); |
| 351 | u32 thread_index = vm->thread_index; |
| 352 | f64 t = vlib_time_now (vm); |
| 353 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 354 | if (PREDICT_FALSE (vec_len (vm->barrier_perf_callbacks) != 0)) |
| 355 | clib_call_callbacks (vm->barrier_perf_callbacks, vm, |
| 356 | vm->clib_time.last_cpu_time, 0 /* enter */ ); |
| 357 | |
Dave Barach | 88c6e00 | 2018-09-30 15:54:06 -0400 | [diff] [blame] | 358 | if (PREDICT_FALSE (vlib_worker_threads->barrier_elog_enabled)) |
| 359 | { |
| 360 | vlib_worker_thread_t *w = vlib_worker_threads + thread_index; |
| 361 | /* *INDENT-OFF* */ |
| 362 | ELOG_TYPE_DECLARE (e) = { |
| 363 | .format = "barrier-wait-thread-%d", |
| 364 | .format_args = "i4", |
| 365 | }; |
| 366 | /* *INDENT-ON* */ |
| 367 | |
| 368 | struct |
| 369 | { |
| 370 | u32 thread_index; |
| 371 | } __clib_packed *ed; |
| 372 | |
Damjan Marion | f553a2c | 2021-03-26 13:45:37 +0100 | [diff] [blame] | 373 | ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, w->elog_track); |
Dave Barach | 88c6e00 | 2018-09-30 15:54:06 -0400 | [diff] [blame] | 374 | ed->thread_index = thread_index; |
| 375 | } |
| 376 | |
Damjan Marion | ce359db | 2017-03-16 16:15:38 +0100 | [diff] [blame] | 377 | if (CLIB_DEBUG > 0) |
| 378 | { |
| 379 | vm = vlib_get_main (); |
| 380 | vm->parked_at_barrier = 1; |
| 381 | } |
Alexander Kabaev | feda545 | 2019-11-01 18:26:20 -0400 | [diff] [blame] | 382 | clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, 1); |
Damjan Marion | ce359db | 2017-03-16 16:15:38 +0100 | [diff] [blame] | 383 | while (*vlib_worker_threads->wait_at_barrier) |
| 384 | ; |
Dave Barach | a4324a9 | 2019-02-19 17:05:30 -0500 | [diff] [blame] | 385 | |
| 386 | /* |
| 387 | * Recompute the offset from thread-0 time. |
| 388 | * Note that vlib_time_now adds vm->time_offset, so |
| 389 | * clear it first. Save the resulting idea of "now", to |
| 390 | * see how well we're doing. See show_clock_command_fn(...) |
| 391 | */ |
| 392 | { |
| 393 | f64 now; |
| 394 | vm->time_offset = 0.0; |
| 395 | now = vlib_time_now (vm); |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 396 | vm->time_offset = vgm->vlib_mains[0]->time_last_barrier_release - now; |
Dave Barach | a4324a9 | 2019-02-19 17:05:30 -0500 | [diff] [blame] | 397 | vm->time_last_barrier_release = vlib_time_now (vm); |
| 398 | } |
| 399 | |
Damjan Marion | ce359db | 2017-03-16 16:15:38 +0100 | [diff] [blame] | 400 | if (CLIB_DEBUG > 0) |
| 401 | vm->parked_at_barrier = 0; |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 402 | clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, -1); |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 403 | |
| 404 | if (PREDICT_FALSE (*vlib_worker_threads->node_reforks_required)) |
| 405 | { |
Dave Barach | 88c6e00 | 2018-09-30 15:54:06 -0400 | [diff] [blame] | 406 | if (PREDICT_FALSE (vlib_worker_threads->barrier_elog_enabled)) |
| 407 | { |
| 408 | t = vlib_time_now (vm) - t; |
| 409 | vlib_worker_thread_t *w = vlib_worker_threads + thread_index; |
| 410 | /* *INDENT-OFF* */ |
| 411 | ELOG_TYPE_DECLARE (e) = { |
| 412 | .format = "barrier-refork-thread-%d", |
| 413 | .format_args = "i4", |
| 414 | }; |
| 415 | /* *INDENT-ON* */ |
| 416 | |
| 417 | struct |
| 418 | { |
| 419 | u32 thread_index; |
| 420 | } __clib_packed *ed; |
| 421 | |
| 422 | ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, |
| 423 | w->elog_track); |
| 424 | ed->thread_index = thread_index; |
| 425 | } |
| 426 | |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 427 | vlib_worker_thread_node_refork (); |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 428 | clib_atomic_fetch_add (vlib_worker_threads->node_reforks_required, |
| 429 | -1); |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 430 | while (*vlib_worker_threads->node_reforks_required) |
| 431 | ; |
| 432 | } |
Dave Barach | 88c6e00 | 2018-09-30 15:54:06 -0400 | [diff] [blame] | 433 | if (PREDICT_FALSE (vlib_worker_threads->barrier_elog_enabled)) |
| 434 | { |
| 435 | t = vlib_time_now (vm) - t; |
| 436 | vlib_worker_thread_t *w = vlib_worker_threads + thread_index; |
| 437 | /* *INDENT-OFF* */ |
| 438 | ELOG_TYPE_DECLARE (e) = { |
| 439 | .format = "barrier-released-thread-%d: %dus", |
| 440 | .format_args = "i4i4", |
| 441 | }; |
| 442 | /* *INDENT-ON* */ |
| 443 | |
| 444 | struct |
| 445 | { |
| 446 | u32 thread_index; |
| 447 | u32 duration; |
| 448 | } __clib_packed *ed; |
| 449 | |
Damjan Marion | f553a2c | 2021-03-26 13:45:37 +0100 | [diff] [blame] | 450 | ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, w->elog_track); |
Dave Barach | 88c6e00 | 2018-09-30 15:54:06 -0400 | [diff] [blame] | 451 | ed->thread_index = thread_index; |
| 452 | ed->duration = (int) (1000000.0 * t); |
| 453 | } |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 454 | |
| 455 | if (PREDICT_FALSE (vec_len (vm->barrier_perf_callbacks) != 0)) |
| 456 | clib_call_callbacks (vm->barrier_perf_callbacks, vm, |
| 457 | vm->clib_time.last_cpu_time, 1 /* leave */ ); |
Damjan Marion | ce359db | 2017-03-16 16:15:38 +0100 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 461 | always_inline vlib_main_t * |
| 462 | vlib_get_worker_vlib_main (u32 worker_index) |
| 463 | { |
| 464 | vlib_main_t *vm; |
| 465 | vlib_thread_main_t *tm = &vlib_thread_main; |
| 466 | ASSERT (worker_index < tm->n_vlib_mains - 1); |
Damjan Marion | 6ffb7c6 | 2021-03-26 13:06:13 +0100 | [diff] [blame] | 467 | vm = vlib_get_main_by_index (worker_index + 1); |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 468 | ASSERT (vm); |
| 469 | return vm; |
| 470 | } |
| 471 | |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 472 | static inline u8 |
| 473 | vlib_thread_is_main_w_barrier (void) |
| 474 | { |
| 475 | return (!vlib_num_workers () |
| 476 | || ((vlib_get_thread_index () == 0 |
| 477 | && vlib_worker_threads->wait_at_barrier[0]))); |
| 478 | } |
| 479 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 480 | u8 *vlib_thread_stack_init (uword thread_index); |
Dave Barach | 69128d0 | 2017-09-26 10:54:34 -0400 | [diff] [blame] | 481 | extern void *rpc_call_main_thread_cb_fn; |
| 482 | |
| 483 | void |
| 484 | vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t * |
| 485 | args); |
| 486 | void vlib_rpc_call_main_thread (void *function, u8 * args, u32 size); |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 487 | void vlib_get_thread_core_numa (vlib_worker_thread_t * w, unsigned cpu_id); |
Dave Barach | ab1a50c | 2020-10-06 14:08:16 -0400 | [diff] [blame] | 488 | vlib_thread_main_t *vlib_get_thread_main_not_inline (void); |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 489 | |
Florin Coras | 4b20830 | 2022-03-30 13:50:19 -0700 | [diff] [blame] | 490 | /** |
| 491 | * Force workers sync from within worker |
| 492 | * |
| 493 | * Must be paired with @ref vlib_workers_continue |
| 494 | */ |
| 495 | void vlib_workers_sync (void); |
| 496 | /** |
| 497 | * Release barrier after workers sync |
| 498 | */ |
| 499 | void vlib_workers_continue (void); |
| 500 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 501 | #endif /* included_vlib_threads_h */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 502 | |
| 503 | /* |
| 504 | * fd.io coding-style-patch-verification: ON |
| 505 | * |
| 506 | * Local Variables: |
| 507 | * eval: (c-set-style "gnu") |
| 508 | * End: |
| 509 | */ |