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 enum |
| 68 | { |
| 69 | VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | } vlib_frame_queue_msg_type_t; |
| 71 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 72 | typedef struct |
| 73 | { |
Damjan Marion | b0d680b | 2016-11-04 14:41:44 +0100 | [diff] [blame] | 74 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | volatile u32 valid; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 76 | u32 msg_type; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | u32 n_vectors; |
| 78 | u32 last_n_vectors; |
| 79 | |
| 80 | /* 256 * 4 = 1024 bytes, even mult of cache line size */ |
| 81 | u32 buffer_index[VLIB_FRAME_SIZE]; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 82 | } |
| 83 | vlib_frame_queue_elt_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 85 | typedef struct |
| 86 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | /* First cache line */ |
Damjan Marion | b0d680b | 2016-11-04 14:41:44 +0100 | [diff] [blame] | 88 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | volatile u32 *wait_at_barrier; |
| 90 | volatile u32 *workers_at_barrier; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | |
| 92 | /* Second Cache Line */ |
Damjan Marion | b0d680b | 2016-11-04 14:41:44 +0100 | [diff] [blame] | 93 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | void *thread_mheap; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 95 | u8 *thread_stack; |
| 96 | void (*thread_function) (void *); |
| 97 | void *thread_function_arg; |
| 98 | i64 recursion_level; |
| 99 | elog_track_t elog_track; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | u32 instance_id; |
| 101 | vlib_thread_registration_t *registration; |
| 102 | u8 *name; |
Bud Grise | 42f2006 | 2016-03-16 13:09:46 -0400 | [diff] [blame] | 103 | u64 barrier_sync_count; |
Dave Barach | 88c6e00 | 2018-09-30 15:54:06 -0400 | [diff] [blame] | 104 | u8 barrier_elog_enabled; |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 105 | const char *barrier_caller; |
| 106 | const char *barrier_context; |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 107 | volatile u32 *node_reforks_required; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | |
| 109 | long lwp; |
Mohsin Kazmi | 5d64c78 | 2018-09-11 20:27:09 +0200 | [diff] [blame] | 110 | int cpu_id; |
| 111 | int core_id; |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 112 | int numa_id; |
Pavel Kotucek | 9876520 | 2016-10-07 08:37:28 +0200 | [diff] [blame] | 113 | pthread_t thread_id; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | } vlib_worker_thread_t; |
| 115 | |
Damjan Marion | 6a7acc2 | 2016-12-19 16:28:36 +0100 | [diff] [blame] | 116 | extern vlib_worker_thread_t *vlib_worker_threads; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 118 | typedef struct |
| 119 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | /* enqueue side */ |
Damjan Marion | b0d680b | 2016-11-04 14:41:44 +0100 | [diff] [blame] | 121 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | volatile u64 tail; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 123 | u32 enqueue_full_events; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | |
| 125 | /* dequeue side */ |
Damjan Marion | b0d680b | 2016-11-04 14:41:44 +0100 | [diff] [blame] | 126 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | volatile u64 head; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | u64 trace; |
| 129 | u64 vector_threshold; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | |
| 131 | /* dequeue hint to enqueue side */ |
Damjan Marion | b0d680b | 2016-11-04 14:41:44 +0100 | [diff] [blame] | 132 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline2); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | volatile u64 head_hint; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | |
| 135 | /* read-only, constant, shared */ |
Damjan Marion | b0d680b | 2016-11-04 14:41:44 +0100 | [diff] [blame] | 136 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline3); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 137 | vlib_frame_queue_elt_t *elts; |
| 138 | u32 nelts; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 139 | } |
| 140 | vlib_frame_queue_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 142 | typedef struct |
| 143 | { |
Damjan Marion | 78fd7e8 | 2018-07-20 18:47:05 +0200 | [diff] [blame] | 144 | vlib_frame_queue_elt_t **handoff_queue_elt_by_thread_index; |
| 145 | vlib_frame_queue_t **congested_handoff_queue_by_thread_index; |
| 146 | } vlib_frame_queue_per_thread_data_t; |
| 147 | |
| 148 | typedef struct |
| 149 | { |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 150 | u32 node_index; |
Damjan Marion | 78fd7e8 | 2018-07-20 18:47:05 +0200 | [diff] [blame] | 151 | u32 frame_queue_nelts; |
| 152 | u32 queue_hi_thresh; |
| 153 | |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 154 | vlib_frame_queue_t **vlib_frame_queues; |
Damjan Marion | 78fd7e8 | 2018-07-20 18:47:05 +0200 | [diff] [blame] | 155 | vlib_frame_queue_per_thread_data_t *per_thread_data; |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 156 | |
| 157 | /* for frame queue tracing */ |
| 158 | frame_queue_trace_t *frame_queue_traces; |
| 159 | frame_queue_nelt_counter_t *frame_queue_histogram; |
| 160 | } vlib_frame_queue_main_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | |
Dave Barach | 69128d0 | 2017-09-26 10:54:34 -0400 | [diff] [blame] | 162 | typedef struct |
| 163 | { |
| 164 | uword node_index; |
| 165 | uword type_opaque; |
| 166 | uword data; |
| 167 | } vlib_process_signal_event_mt_args_t; |
| 168 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 169 | /* Called early, in thread 0's context */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 170 | clib_error_t *vlib_thread_init (vlib_main_t * vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 172 | int vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index, |
| 173 | u32 frame_queue_index, vlib_frame_t * frame, |
| 174 | vlib_frame_queue_msg_type_t type); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 176 | void vlib_worker_thread_node_runtime_update (void); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 177 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 178 | void vlib_create_worker_threads (vlib_main_t * vm, int n, |
| 179 | void (*thread_function) (void *)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | |
| 181 | void vlib_worker_thread_init (vlib_worker_thread_t * w); |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 182 | 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] | 183 | |
| 184 | /* Check for a barrier sync request every 30ms */ |
| 185 | #define BARRIER_SYNC_DELAY (0.030000) |
| 186 | |
| 187 | #if CLIB_DEBUG > 0 |
| 188 | /* long barrier timeout, for gdb... */ |
| 189 | #define BARRIER_SYNC_TIMEOUT (600.1) |
| 190 | #else |
| 191 | #define BARRIER_SYNC_TIMEOUT (1.0) |
| 192 | #endif |
| 193 | |
Damjan Marion | 8343ee5 | 2019-02-26 17:15:48 +0100 | [diff] [blame] | 194 | #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] | 195 | |
Damjan Marion | 8343ee5 | 2019-02-26 17:15:48 +0100 | [diff] [blame] | 196 | void vlib_worker_thread_barrier_sync_int (vlib_main_t * vm, |
| 197 | const char *func_name); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 198 | void vlib_worker_thread_barrier_release (vlib_main_t * vm); |
Neale Ranns | 42845dd | 2020-05-26 13:12:17 +0000 | [diff] [blame] | 199 | u8 vlib_worker_thread_barrier_held (void); |
Dave Barach | c602b38 | 2019-06-03 19:48:22 -0400 | [diff] [blame] | 200 | 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] | 201 | void vlib_worker_thread_node_refork (void); |
Neale Ranns | 42845dd | 2020-05-26 13:12:17 +0000 | [diff] [blame] | 202 | /** |
| 203 | * Wait until each of the workers has been once around the track |
| 204 | */ |
| 205 | void vlib_worker_wait_one_loop (void); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 206 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 207 | static_always_inline uword |
| 208 | vlib_get_thread_index (void) |
| 209 | { |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 210 | return __os_thread_index; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 211 | } |
| 212 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 213 | always_inline void |
| 214 | vlib_smp_unsafe_warning (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 215 | { |
| 216 | if (CLIB_DEBUG > 0) |
| 217 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 218 | if (vlib_get_thread_index ()) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 219 | fformat (stderr, "%s: SMP unsafe warning...\n", __FUNCTION__); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
Damjan Marion | 1d429df | 2021-04-14 19:07:13 +0200 | [diff] [blame] | 223 | always_inline int |
| 224 | __foreach_vlib_main_helper (vlib_main_t *ii, vlib_main_t **p) |
| 225 | { |
| 226 | vlib_main_t *vm; |
| 227 | u32 index = ii - (vlib_main_t *) 0; |
| 228 | |
| 229 | if (index >= vec_len (vlib_global_main.vlib_mains)) |
| 230 | return 0; |
| 231 | |
| 232 | *p = vm = vlib_global_main.vlib_mains[index]; |
| 233 | ASSERT (index == 0 || vm->parked_at_barrier == 1); |
| 234 | return 1; |
| 235 | } |
| 236 | |
Damjan Marion | 92ccf9b | 2021-03-26 11:38:01 +0100 | [diff] [blame] | 237 | #define foreach_vlib_main() \ |
Damjan Marion | 1d429df | 2021-04-14 19:07:13 +0200 | [diff] [blame] | 238 | for (vlib_main_t *ii = 0, *this_vlib_main; \ |
| 239 | __foreach_vlib_main_helper (ii, &this_vlib_main); ii++) \ |
| 240 | if (this_vlib_main) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 242 | #define foreach_sched_policy \ |
| 243 | _(SCHED_OTHER, OTHER, "other") \ |
| 244 | _(SCHED_BATCH, BATCH, "batch") \ |
| 245 | _(SCHED_IDLE, IDLE, "idle") \ |
| 246 | _(SCHED_FIFO, FIFO, "fifo") \ |
| 247 | _(SCHED_RR, RR, "rr") |
| 248 | |
| 249 | typedef enum |
| 250 | { |
| 251 | #define _(v,f,s) SCHED_POLICY_##f = v, |
| 252 | foreach_sched_policy |
| 253 | #undef _ |
| 254 | SCHED_POLICY_N, |
| 255 | } sched_policy_t; |
| 256 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 257 | typedef struct |
| 258 | { |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 259 | clib_error_t *(*vlib_launch_thread_cb) (void *fp, vlib_worker_thread_t * w, |
Mohsin Kazmi | 5d64c78 | 2018-09-11 20:27:09 +0200 | [diff] [blame] | 260 | unsigned cpu_id); |
| 261 | clib_error_t *(*vlib_thread_set_lcore_cb) (u32 thread, u16 cpu); |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 262 | } vlib_thread_callbacks_t; |
| 263 | |
| 264 | typedef struct |
| 265 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | /* Link list of registrations, built by constructors */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 267 | vlib_thread_registration_t *next; |
| 268 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 269 | /* Vector of registrations, w/ non-data-structure clones at the top */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 270 | vlib_thread_registration_t **registrations; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 271 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 272 | uword *thread_registrations_by_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 273 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 274 | vlib_worker_thread_t *worker_threads; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 275 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 276 | /* |
| 277 | * Launch all threads as pthreads, |
| 278 | * not eal_rte_launch (strict affinity) threads |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | */ |
| 280 | int use_pthreads; |
| 281 | |
| 282 | /* Number of vlib_main / vnet_main clones */ |
| 283 | u32 n_vlib_mains; |
| 284 | |
| 285 | /* Number of thread stacks to create */ |
| 286 | u32 n_thread_stacks; |
| 287 | |
| 288 | /* Number of pthreads */ |
| 289 | u32 n_pthreads; |
| 290 | |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 291 | /* Number of threads */ |
| 292 | u32 n_threads; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 293 | |
| 294 | /* Number of cores to skip, must match the core mask */ |
| 295 | u32 skip_cores; |
| 296 | |
| 297 | /* Thread prefix name */ |
| 298 | u8 *thread_prefix; |
| 299 | |
| 300 | /* main thread lcore */ |
Damjan Marion | 858151f | 2018-07-11 10:51:00 +0200 | [diff] [blame] | 301 | u32 main_lcore; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 302 | |
| 303 | /* Bitmap of available CPU cores */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 304 | uword *cpu_core_bitmap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 305 | |
| 306 | /* Bitmap of available CPU sockets (NUMA nodes) */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 307 | uword *cpu_socket_bitmap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 308 | |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 309 | /* Worker handoff queues */ |
| 310 | vlib_frame_queue_main_t *frame_queue_mains; |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 311 | |
| 312 | /* worker thread initialization barrier */ |
| 313 | volatile u32 worker_thread_release; |
| 314 | |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 315 | /* scheduling policy */ |
| 316 | u32 sched_policy; |
| 317 | |
| 318 | /* scheduling policy priority */ |
| 319 | u32 sched_priority; |
| 320 | |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 321 | /* callbacks */ |
| 322 | vlib_thread_callbacks_t cb; |
| 323 | int extern_thread_mgmt; |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 324 | |
| 325 | /* NUMA-bound heap size */ |
| 326 | uword numa_heap_size; |
| 327 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 328 | } vlib_thread_main_t; |
| 329 | |
Damjan Marion | 6a7acc2 | 2016-12-19 16:28:36 +0100 | [diff] [blame] | 330 | extern vlib_thread_main_t vlib_thread_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 331 | |
Damjan Marion | ce359db | 2017-03-16 16:15:38 +0100 | [diff] [blame] | 332 | #include <vlib/global_funcs.h> |
| 333 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 334 | #define VLIB_REGISTER_THREAD(x,...) \ |
| 335 | __VA_ARGS__ vlib_thread_registration_t x; \ |
| 336 | static void __vlib_add_thread_registration_##x (void) \ |
| 337 | __attribute__((__constructor__)) ; \ |
| 338 | static void __vlib_add_thread_registration_##x (void) \ |
| 339 | { \ |
| 340 | vlib_thread_main_t * tm = &vlib_thread_main; \ |
| 341 | x.next = tm->next; \ |
| 342 | tm->next = &x; \ |
| 343 | } \ |
Damjan Marion | 72d2c4f | 2018-04-05 21:32:29 +0200 | [diff] [blame] | 344 | static void __vlib_rm_thread_registration_##x (void) \ |
| 345 | __attribute__((__destructor__)) ; \ |
| 346 | static void __vlib_rm_thread_registration_##x (void) \ |
| 347 | { \ |
| 348 | vlib_thread_main_t * tm = &vlib_thread_main; \ |
| 349 | VLIB_REMOVE_FROM_LINKED_LIST (tm->next, &x, next); \ |
| 350 | } \ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 351 | __VA_ARGS__ vlib_thread_registration_t x |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 352 | |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 353 | always_inline u32 |
| 354 | vlib_num_workers () |
| 355 | { |
| 356 | return vlib_thread_main.n_vlib_mains - 1; |
| 357 | } |
| 358 | |
| 359 | always_inline u32 |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 360 | vlib_get_worker_thread_index (u32 worker_index) |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 361 | { |
| 362 | return worker_index + 1; |
| 363 | } |
| 364 | |
Damjan Marion | 3a4ed39 | 2016-11-08 13:20:42 +0100 | [diff] [blame] | 365 | always_inline u32 |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 366 | vlib_get_worker_index (u32 thread_index) |
Damjan Marion | 3a4ed39 | 2016-11-08 13:20:42 +0100 | [diff] [blame] | 367 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 368 | return thread_index - 1; |
Damjan Marion | 3a4ed39 | 2016-11-08 13:20:42 +0100 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | always_inline u32 |
| 372 | vlib_get_current_worker_index () |
| 373 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 374 | return vlib_get_thread_index () - 1; |
Damjan Marion | 3a4ed39 | 2016-11-08 13:20:42 +0100 | [diff] [blame] | 375 | } |
| 376 | |
Damjan Marion | ce359db | 2017-03-16 16:15:38 +0100 | [diff] [blame] | 377 | static inline void |
| 378 | vlib_worker_thread_barrier_check (void) |
| 379 | { |
| 380 | if (PREDICT_FALSE (*vlib_worker_threads->wait_at_barrier)) |
| 381 | { |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 382 | vlib_global_main_t *vgm = vlib_get_global_main (); |
Dave Barach | 88c6e00 | 2018-09-30 15:54:06 -0400 | [diff] [blame] | 383 | vlib_main_t *vm = vlib_get_main (); |
| 384 | u32 thread_index = vm->thread_index; |
| 385 | f64 t = vlib_time_now (vm); |
| 386 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 387 | if (PREDICT_FALSE (vec_len (vm->barrier_perf_callbacks) != 0)) |
| 388 | clib_call_callbacks (vm->barrier_perf_callbacks, vm, |
| 389 | vm->clib_time.last_cpu_time, 0 /* enter */ ); |
| 390 | |
Dave Barach | 88c6e00 | 2018-09-30 15:54:06 -0400 | [diff] [blame] | 391 | if (PREDICT_FALSE (vlib_worker_threads->barrier_elog_enabled)) |
| 392 | { |
| 393 | vlib_worker_thread_t *w = vlib_worker_threads + thread_index; |
| 394 | /* *INDENT-OFF* */ |
| 395 | ELOG_TYPE_DECLARE (e) = { |
| 396 | .format = "barrier-wait-thread-%d", |
| 397 | .format_args = "i4", |
| 398 | }; |
| 399 | /* *INDENT-ON* */ |
| 400 | |
| 401 | struct |
| 402 | { |
| 403 | u32 thread_index; |
| 404 | } __clib_packed *ed; |
| 405 | |
Damjan Marion | f553a2c | 2021-03-26 13:45:37 +0100 | [diff] [blame] | 406 | 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] | 407 | ed->thread_index = thread_index; |
| 408 | } |
| 409 | |
Damjan Marion | ce359db | 2017-03-16 16:15:38 +0100 | [diff] [blame] | 410 | if (CLIB_DEBUG > 0) |
| 411 | { |
| 412 | vm = vlib_get_main (); |
| 413 | vm->parked_at_barrier = 1; |
| 414 | } |
Alexander Kabaev | feda545 | 2019-11-01 18:26:20 -0400 | [diff] [blame] | 415 | clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, 1); |
Damjan Marion | ce359db | 2017-03-16 16:15:38 +0100 | [diff] [blame] | 416 | while (*vlib_worker_threads->wait_at_barrier) |
| 417 | ; |
Dave Barach | a4324a9 | 2019-02-19 17:05:30 -0500 | [diff] [blame] | 418 | |
| 419 | /* |
| 420 | * Recompute the offset from thread-0 time. |
| 421 | * Note that vlib_time_now adds vm->time_offset, so |
| 422 | * clear it first. Save the resulting idea of "now", to |
| 423 | * see how well we're doing. See show_clock_command_fn(...) |
| 424 | */ |
| 425 | { |
| 426 | f64 now; |
| 427 | vm->time_offset = 0.0; |
| 428 | now = vlib_time_now (vm); |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 429 | vm->time_offset = vgm->vlib_mains[0]->time_last_barrier_release - now; |
Dave Barach | a4324a9 | 2019-02-19 17:05:30 -0500 | [diff] [blame] | 430 | vm->time_last_barrier_release = vlib_time_now (vm); |
| 431 | } |
| 432 | |
Damjan Marion | ce359db | 2017-03-16 16:15:38 +0100 | [diff] [blame] | 433 | if (CLIB_DEBUG > 0) |
| 434 | vm->parked_at_barrier = 0; |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 435 | clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, -1); |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 436 | |
| 437 | if (PREDICT_FALSE (*vlib_worker_threads->node_reforks_required)) |
| 438 | { |
Dave Barach | 88c6e00 | 2018-09-30 15:54:06 -0400 | [diff] [blame] | 439 | if (PREDICT_FALSE (vlib_worker_threads->barrier_elog_enabled)) |
| 440 | { |
| 441 | t = vlib_time_now (vm) - t; |
| 442 | vlib_worker_thread_t *w = vlib_worker_threads + thread_index; |
| 443 | /* *INDENT-OFF* */ |
| 444 | ELOG_TYPE_DECLARE (e) = { |
| 445 | .format = "barrier-refork-thread-%d", |
| 446 | .format_args = "i4", |
| 447 | }; |
| 448 | /* *INDENT-ON* */ |
| 449 | |
| 450 | struct |
| 451 | { |
| 452 | u32 thread_index; |
| 453 | } __clib_packed *ed; |
| 454 | |
| 455 | ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, |
| 456 | w->elog_track); |
| 457 | ed->thread_index = thread_index; |
| 458 | } |
| 459 | |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 460 | vlib_worker_thread_node_refork (); |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 461 | clib_atomic_fetch_add (vlib_worker_threads->node_reforks_required, |
| 462 | -1); |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 463 | while (*vlib_worker_threads->node_reforks_required) |
| 464 | ; |
| 465 | } |
Dave Barach | 88c6e00 | 2018-09-30 15:54:06 -0400 | [diff] [blame] | 466 | if (PREDICT_FALSE (vlib_worker_threads->barrier_elog_enabled)) |
| 467 | { |
| 468 | t = vlib_time_now (vm) - t; |
| 469 | vlib_worker_thread_t *w = vlib_worker_threads + thread_index; |
| 470 | /* *INDENT-OFF* */ |
| 471 | ELOG_TYPE_DECLARE (e) = { |
| 472 | .format = "barrier-released-thread-%d: %dus", |
| 473 | .format_args = "i4i4", |
| 474 | }; |
| 475 | /* *INDENT-ON* */ |
| 476 | |
| 477 | struct |
| 478 | { |
| 479 | u32 thread_index; |
| 480 | u32 duration; |
| 481 | } __clib_packed *ed; |
| 482 | |
Damjan Marion | f553a2c | 2021-03-26 13:45:37 +0100 | [diff] [blame] | 483 | 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] | 484 | ed->thread_index = thread_index; |
| 485 | ed->duration = (int) (1000000.0 * t); |
| 486 | } |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 487 | |
| 488 | if (PREDICT_FALSE (vec_len (vm->barrier_perf_callbacks) != 0)) |
| 489 | clib_call_callbacks (vm->barrier_perf_callbacks, vm, |
| 490 | vm->clib_time.last_cpu_time, 1 /* leave */ ); |
Damjan Marion | ce359db | 2017-03-16 16:15:38 +0100 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 494 | always_inline vlib_main_t * |
| 495 | vlib_get_worker_vlib_main (u32 worker_index) |
| 496 | { |
| 497 | vlib_main_t *vm; |
| 498 | vlib_thread_main_t *tm = &vlib_thread_main; |
| 499 | ASSERT (worker_index < tm->n_vlib_mains - 1); |
Damjan Marion | 6ffb7c6 | 2021-03-26 13:06:13 +0100 | [diff] [blame] | 500 | vm = vlib_get_main_by_index (worker_index + 1); |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 501 | ASSERT (vm); |
| 502 | return vm; |
| 503 | } |
| 504 | |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 505 | static inline u8 |
| 506 | vlib_thread_is_main_w_barrier (void) |
| 507 | { |
| 508 | return (!vlib_num_workers () |
| 509 | || ((vlib_get_thread_index () == 0 |
| 510 | && vlib_worker_threads->wait_at_barrier[0]))); |
| 511 | } |
| 512 | |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 513 | static inline void |
| 514 | vlib_put_frame_queue_elt (vlib_frame_queue_elt_t * hf) |
| 515 | { |
| 516 | CLIB_MEMORY_BARRIER (); |
| 517 | hf->valid = 1; |
| 518 | } |
| 519 | |
| 520 | static inline vlib_frame_queue_elt_t * |
| 521 | vlib_get_frame_queue_elt (u32 frame_queue_index, u32 index) |
| 522 | { |
| 523 | vlib_frame_queue_t *fq; |
| 524 | vlib_frame_queue_elt_t *elt; |
| 525 | vlib_thread_main_t *tm = &vlib_thread_main; |
| 526 | vlib_frame_queue_main_t *fqm = |
| 527 | vec_elt_at_index (tm->frame_queue_mains, frame_queue_index); |
| 528 | u64 new_tail; |
| 529 | |
| 530 | fq = fqm->vlib_frame_queues[index]; |
| 531 | ASSERT (fq); |
| 532 | |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 533 | new_tail = clib_atomic_add_fetch (&fq->tail, 1); |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 534 | |
| 535 | /* Wait until a ring slot is available */ |
| 536 | while (new_tail >= fq->head_hint + fq->nelts) |
| 537 | vlib_worker_thread_barrier_check (); |
| 538 | |
| 539 | elt = fq->elts + (new_tail & (fq->nelts - 1)); |
| 540 | |
| 541 | /* this would be very bad... */ |
| 542 | while (elt->valid) |
| 543 | ; |
| 544 | |
| 545 | elt->msg_type = VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME; |
| 546 | elt->last_n_vectors = elt->n_vectors = 0; |
| 547 | |
| 548 | return elt; |
| 549 | } |
| 550 | |
| 551 | static inline vlib_frame_queue_t * |
| 552 | is_vlib_frame_queue_congested (u32 frame_queue_index, |
| 553 | u32 index, |
| 554 | u32 queue_hi_thresh, |
| 555 | vlib_frame_queue_t ** |
| 556 | handoff_queue_by_worker_index) |
| 557 | { |
| 558 | vlib_frame_queue_t *fq; |
| 559 | vlib_thread_main_t *tm = &vlib_thread_main; |
| 560 | vlib_frame_queue_main_t *fqm = |
| 561 | vec_elt_at_index (tm->frame_queue_mains, frame_queue_index); |
| 562 | |
| 563 | fq = handoff_queue_by_worker_index[index]; |
| 564 | if (fq != (vlib_frame_queue_t *) (~0)) |
| 565 | return fq; |
| 566 | |
| 567 | fq = fqm->vlib_frame_queues[index]; |
| 568 | ASSERT (fq); |
| 569 | |
| 570 | if (PREDICT_FALSE (fq->tail >= (fq->head_hint + queue_hi_thresh))) |
| 571 | { |
| 572 | /* a valid entry in the array will indicate the queue has reached |
| 573 | * the specified threshold and is congested |
| 574 | */ |
| 575 | handoff_queue_by_worker_index[index] = fq; |
| 576 | fq->enqueue_full_events++; |
| 577 | return fq; |
| 578 | } |
| 579 | |
| 580 | return NULL; |
| 581 | } |
| 582 | |
| 583 | static inline vlib_frame_queue_elt_t * |
| 584 | vlib_get_worker_handoff_queue_elt (u32 frame_queue_index, |
| 585 | u32 vlib_worker_index, |
| 586 | vlib_frame_queue_elt_t ** |
| 587 | handoff_queue_elt_by_worker_index) |
| 588 | { |
| 589 | vlib_frame_queue_elt_t *elt; |
| 590 | |
| 591 | if (handoff_queue_elt_by_worker_index[vlib_worker_index]) |
| 592 | return handoff_queue_elt_by_worker_index[vlib_worker_index]; |
| 593 | |
| 594 | elt = vlib_get_frame_queue_elt (frame_queue_index, vlib_worker_index); |
| 595 | |
| 596 | handoff_queue_elt_by_worker_index[vlib_worker_index] = elt; |
| 597 | |
| 598 | return elt; |
| 599 | } |
| 600 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 601 | u8 *vlib_thread_stack_init (uword thread_index); |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 602 | int vlib_thread_cb_register (struct vlib_main_t *vm, |
| 603 | vlib_thread_callbacks_t * cb); |
Dave Barach | 69128d0 | 2017-09-26 10:54:34 -0400 | [diff] [blame] | 604 | extern void *rpc_call_main_thread_cb_fn; |
| 605 | |
| 606 | void |
| 607 | vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t * |
| 608 | args); |
| 609 | void vlib_rpc_call_main_thread (void *function, u8 * args, u32 size); |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 610 | 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] | 611 | vlib_thread_main_t *vlib_get_thread_main_not_inline (void); |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 612 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 613 | #endif /* included_vlib_threads_h */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 614 | |
| 615 | /* |
| 616 | * fd.io coding-style-patch-verification: ON |
| 617 | * |
| 618 | * Local Variables: |
| 619 | * eval: (c-set-style "gnu") |
| 620 | * End: |
| 621 | */ |