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