blob: c671aa78c3981575b10c17f299645ce7fcf21648 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Seidenberg6c81f5a2020-07-10 15:49:03 +000019#include <vppinfra/callback.h>
Tom Jones72cb9f72024-01-25 10:24:08 +000020#ifdef __linux__
Pavel Kotucek1e765832016-09-23 08:54:14 +020021#include <linux/sched.h>
Tom Jones72cb9f72024-01-25 10:24:08 +000022#elif __FreeBSD__
23#include <sys/sched.h>
24#endif /* __linux__ */
Ed Warnickecb9cada2015-12-08 15:45:58 -070025
Dave Barach9b8ffd92016-07-08 08:13:45 -040026void vlib_set_thread_name (char *name);
Ed Warnickecb9cada2015-12-08 15:45:58 -070027
28/* arg is actually a vlib__thread_t * */
Dave Barach9b8ffd92016-07-08 08:13:45 -040029typedef void (vlib_thread_function_t) (void *arg);
Ed Warnickecb9cada2015-12-08 15:45:58 -070030
Dave Barach9b8ffd92016-07-08 08:13:45 -040031typedef struct vlib_thread_registration_
32{
Ed Warnickecb9cada2015-12-08 15:45:58 -070033 /* constructor generated list of thread registrations */
Dave Barach9b8ffd92016-07-08 08:13:45 -040034 struct vlib_thread_registration_ *next;
Ed Warnickecb9cada2015-12-08 15:45:58 -070035
36 /* config parameters */
Dave Barach9b8ffd92016-07-08 08:13:45 -040037 char *name;
38 char *short_name;
39 vlib_thread_function_t *function;
Ed Warnickecb9cada2015-12-08 15:45:58 -070040 uword mheap_size;
41 int fixed_count;
42 u32 count;
43 int no_data_structure_clone;
Bud Grise68adab92016-02-12 10:36:11 -050044 u32 frame_queue_nelts;
45
Ed Warnickecb9cada2015-12-08 15:45:58 -070046 /* All threads of this type run on pthreads */
47 int use_pthreads;
48 u32 first_index;
Dave Barach9b8ffd92016-07-08 08:13:45 -040049 uword *coremask;
Ed Warnickecb9cada2015-12-08 15:45:58 -070050} vlib_thread_registration_t;
51
Florin Coras93992a92017-05-24 18:03:56 -070052#define VLIB_LOG2_THREAD_STACK_SIZE (21)
Ed Warnickecb9cada2015-12-08 15:45:58 -070053#define VLIB_THREAD_STACK_SIZE (1<<VLIB_LOG2_THREAD_STACK_SIZE)
54
Dave Barach9b8ffd92016-07-08 08:13:45 -040055typedef struct
56{
Damjan Marionb0d680b2016-11-04 14:41:44 +010057 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070058 volatile u32 valid;
Damjan Marionc0d9ca72021-05-11 09:39:24 +020059 u32 maybe_trace : 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -070060 u32 n_vectors;
Damjan Marionc0d9ca72021-05-11 09:39:24 +020061 u32 offset;
62 STRUCT_MARK (end_of_reset);
Ed Warnickecb9cada2015-12-08 15:45:58 -070063
Damjan Marionc0d9ca72021-05-11 09:39:24 +020064 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
Ed Warnickecb9cada2015-12-08 15:45:58 -070065 u32 buffer_index[VLIB_FRAME_SIZE];
Mohammed Hawarie7149262022-05-18 10:08:47 +020066 u32 aux_data[VLIB_FRAME_SIZE];
Dave Barach9b8ffd92016-07-08 08:13:45 -040067}
68vlib_frame_queue_elt_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070069
Dave Barach9b8ffd92016-07-08 08:13:45 -040070typedef struct
71{
Ed Warnickecb9cada2015-12-08 15:45:58 -070072 /* First cache line */
Damjan Marionb0d680b2016-11-04 14:41:44 +010073 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070074 volatile u32 *wait_at_barrier;
75 volatile u32 *workers_at_barrier;
Ed Warnickecb9cada2015-12-08 15:45:58 -070076
77 /* Second Cache Line */
Damjan Marionb0d680b2016-11-04 14:41:44 +010078 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
Ed Warnickecb9cada2015-12-08 15:45:58 -070079 void *thread_mheap;
Dave Barach9b8ffd92016-07-08 08:13:45 -040080 u8 *thread_stack;
81 void (*thread_function) (void *);
82 void *thread_function_arg;
83 i64 recursion_level;
84 elog_track_t elog_track;
Ed Warnickecb9cada2015-12-08 15:45:58 -070085 u32 instance_id;
86 vlib_thread_registration_t *registration;
87 u8 *name;
Bud Grise42f20062016-03-16 13:09:46 -040088 u64 barrier_sync_count;
Dave Barach88c6e002018-09-30 15:54:06 -040089 u8 barrier_elog_enabled;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010090 const char *barrier_caller;
91 const char *barrier_context;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +010092 volatile u32 *node_reforks_required;
Florin Coras4b208302022-03-30 13:50:19 -070093 volatile u32 wait_before_barrier;
94 volatile u32 workers_before_barrier;
95 volatile u32 done_work_before_barrier;
Ed Warnickecb9cada2015-12-08 15:45:58 -070096
97 long lwp;
Mohsin Kazmi5d64c782018-09-11 20:27:09 +020098 int cpu_id;
99 int core_id;
Dave Baracha690fdb2020-01-21 12:34:55 -0500100 int numa_id;
Pavel Kotucek98765202016-10-07 08:37:28 +0200101 pthread_t thread_id;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102} vlib_worker_thread_t;
103
Damjan Marion6a7acc22016-12-19 16:28:36 +0100104extern vlib_worker_thread_t *vlib_worker_threads;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105
Dave Barach9b8ffd92016-07-08 08:13:45 -0400106typedef struct
107{
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200108 /* static data */
Damjan Marionb0d680b2016-11-04 14:41:44 +0100109 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 vlib_frame_queue_elt_t *elts;
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200111 u64 vector_threshold;
112 u64 trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113 u32 nelts;
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200114
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 Barach9b8ffd92016-07-08 08:13:45 -0400122}
123vlib_frame_queue_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124
Mohammed Hawarie7149262022-05-18 10:08:47 +0200125struct vlib_frame_queue_main_t_;
126typedef u32 (vlib_frame_queue_dequeue_fn_t) (
127 vlib_main_t *vm, struct vlib_frame_queue_main_t_ *fqm);
128typedef struct vlib_frame_queue_main_t_
Damjan Marionaaef1eb2016-11-08 17:37:01 +0100129{
130 u32 node_index;
Damjan Marion78fd7e82018-07-20 18:47:05 +0200131 u32 frame_queue_nelts;
Damjan Marion78fd7e82018-07-20 18:47:05 +0200132
Damjan Marionaaef1eb2016-11-08 17:37:01 +0100133 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 Hawarie7149262022-05-18 10:08:47 +0200138 vlib_frame_queue_dequeue_fn_t *frame_queue_dequeue_fn;
Damjan Marionaaef1eb2016-11-08 17:37:01 +0100139} vlib_frame_queue_main_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140
Dave Barach69128d02017-09-26 10:54:34 -0400141typedef struct
142{
143 uword node_index;
144 uword type_opaque;
145 uword data;
146} vlib_process_signal_event_mt_args_t;
147
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148/* Called early, in thread 0's context */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400149clib_error_t *vlib_thread_init (vlib_main_t * vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150
Dave Barach9b8ffd92016-07-08 08:13:45 -0400151void vlib_worker_thread_node_runtime_update (void);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152
Dave Barach9b8ffd92016-07-08 08:13:45 -0400153void vlib_create_worker_threads (vlib_main_t * vm, int n,
154 void (*thread_function) (void *));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155
156void vlib_worker_thread_init (vlib_worker_thread_t * w);
Damjan Marionaaef1eb2016-11-08 17:37:01 +0100157u32 vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158
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 Marion8343ee52019-02-26 17:15:48 +0100169#define vlib_worker_thread_barrier_sync(X) {vlib_worker_thread_barrier_sync_int(X, __FUNCTION__);}
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100170
Damjan Marion8343ee52019-02-26 17:15:48 +0100171void vlib_worker_thread_barrier_sync_int (vlib_main_t * vm,
172 const char *func_name);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400173void vlib_worker_thread_barrier_release (vlib_main_t * vm);
Neale Ranns42845dd2020-05-26 13:12:17 +0000174u8 vlib_worker_thread_barrier_held (void);
Dave Barachc602b382019-06-03 19:48:22 -0400175void vlib_worker_thread_initial_barrier_sync_and_release (vlib_main_t * vm);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100176void vlib_worker_thread_node_refork (void);
Neale Ranns42845dd2020-05-26 13:12:17 +0000177/**
178 * Wait until each of the workers has been once around the track
179 */
180void vlib_worker_wait_one_loop (void);
Florin Coras4cadd3b2024-02-01 20:46:15 -0800181/**
182 * Flush worker's pending rpc requests to main thread's rpc queue
183 */
184void vlib_worker_flush_pending_rpc_requests (vlib_main_t *vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185
Damjan Marion586afd72017-04-05 19:18:20 +0200186static_always_inline uword
187vlib_get_thread_index (void)
188{
Damjan Marionf55f9b82017-05-10 21:06:28 +0200189 return __os_thread_index;
Damjan Marion586afd72017-04-05 19:18:20 +0200190}
191
Dave Barach9b8ffd92016-07-08 08:13:45 -0400192always_inline void
193vlib_smp_unsafe_warning (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194{
195 if (CLIB_DEBUG > 0)
196 {
Damjan Marion586afd72017-04-05 19:18:20 +0200197 if (vlib_get_thread_index ())
Dave Barach9b8ffd92016-07-08 08:13:45 -0400198 fformat (stderr, "%s: SMP unsafe warning...\n", __FUNCTION__);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199 }
200}
201
Damjan Marion1d429df2021-04-14 19:07:13 +0200202always_inline int
Benoît Gannefa500e92024-06-06 10:46:50 +0200203__foreach_vlib_main_helper (vlib_main_t *ii, vlib_main_t **p, int checks)
Damjan Marion1d429df2021-04-14 19:07:13 +0200204{
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];
Benoît Gannefa500e92024-06-06 10:46:50 +0200212 ASSERT (!checks || index == 0 || vm->parked_at_barrier == 1);
Damjan Marion1d429df2021-04-14 19:07:13 +0200213 return 1;
214}
215
Benoît Gannefa500e92024-06-06 10:46:50 +0200216#define foreach_vlib_main__(checks) \
Damjan Marion1d429df2021-04-14 19:07:13 +0200217 for (vlib_main_t *ii = 0, *this_vlib_main; \
Benoît Gannefa500e92024-06-06 10:46:50 +0200218 __foreach_vlib_main_helper (ii, &this_vlib_main, checks); ii++) \
Damjan Marion1d429df2021-04-14 19:07:13 +0200219 if (this_vlib_main)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220
Benoît Gannefa500e92024-06-06 10:46:50 +0200221#define foreach_vlib_main() foreach_vlib_main__ (1)
222
Tom Jones72cb9f72024-01-25 10:24:08 +0000223#define foreach_sched_policy_posix \
224 _ (SCHED_OTHER, OTHER, "other") \
225 _ (SCHED_FIFO, FIFO, "fifo") \
226 _ (SCHED_RR, RR, "rr")
227#define foreach_sched_policy_linux \
228 _ (SCHED_BATCH, BATCH, "batch") \
229 _ (SCHED_IDLE, IDLE, "idle")
230
231#ifdef __linux__
232#define foreach_sched_policy \
233 foreach_sched_policy_posix foreach_sched_policy_linux
234#else
235#define foreach_sched_policy foreach_sched_policy_posix
236#endif /* __linux__ */
Pavel Kotucek1e765832016-09-23 08:54:14 +0200237
238typedef enum
239{
240#define _(v,f,s) SCHED_POLICY_##f = v,
241 foreach_sched_policy
242#undef _
243 SCHED_POLICY_N,
244} sched_policy_t;
245
Dave Barach9b8ffd92016-07-08 08:13:45 -0400246typedef struct
247{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248 /* Link list of registrations, built by constructors */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400249 vlib_thread_registration_t *next;
250
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251 /* Vector of registrations, w/ non-data-structure clones at the top */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400252 vlib_thread_registration_t **registrations;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253
Dave Barach9b8ffd92016-07-08 08:13:45 -0400254 uword *thread_registrations_by_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255
Dave Barach9b8ffd92016-07-08 08:13:45 -0400256 vlib_worker_thread_t *worker_threads;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258 int use_pthreads;
259
260 /* Number of vlib_main / vnet_main clones */
261 u32 n_vlib_mains;
262
263 /* Number of thread stacks to create */
264 u32 n_thread_stacks;
265
266 /* Number of pthreads */
267 u32 n_pthreads;
268
Damjan Marion878c6092017-01-04 13:19:27 +0100269 /* Number of threads */
270 u32 n_threads;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271
272 /* Number of cores to skip, must match the core mask */
273 u32 skip_cores;
274
275 /* Thread prefix name */
276 u8 *thread_prefix;
277
278 /* main thread lcore */
Damjan Marion858151f2018-07-11 10:51:00 +0200279 u32 main_lcore;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280
281 /* Bitmap of available CPU cores */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400282 uword *cpu_core_bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283
284 /* Bitmap of available CPU sockets (NUMA nodes) */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400285 uword *cpu_socket_bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286
Damjan Marionaaef1eb2016-11-08 17:37:01 +0100287 /* Worker handoff queues */
288 vlib_frame_queue_main_t *frame_queue_mains;
Damjan Marion0f8ecf02016-06-27 08:30:30 +0200289
290 /* worker thread initialization barrier */
291 volatile u32 worker_thread_release;
292
Pavel Kotucek1e765832016-09-23 08:54:14 +0200293 /* scheduling policy */
294 u32 sched_policy;
295
296 /* scheduling policy priority */
297 u32 sched_priority;
298
Dave Baracha690fdb2020-01-21 12:34:55 -0500299 /* NUMA-bound heap size */
300 uword numa_heap_size;
301
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302} vlib_thread_main_t;
303
Damjan Marion6a7acc22016-12-19 16:28:36 +0100304extern vlib_thread_main_t vlib_thread_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305
Damjan Marionce359db2017-03-16 16:15:38 +0100306#include <vlib/global_funcs.h>
307
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308#define VLIB_REGISTER_THREAD(x,...) \
309 __VA_ARGS__ vlib_thread_registration_t x; \
310static void __vlib_add_thread_registration_##x (void) \
311 __attribute__((__constructor__)) ; \
312static void __vlib_add_thread_registration_##x (void) \
313{ \
314 vlib_thread_main_t * tm = &vlib_thread_main; \
315 x.next = tm->next; \
316 tm->next = &x; \
317} \
Damjan Marion72d2c4f2018-04-05 21:32:29 +0200318static void __vlib_rm_thread_registration_##x (void) \
319 __attribute__((__destructor__)) ; \
320static void __vlib_rm_thread_registration_##x (void) \
321{ \
322 vlib_thread_main_t * tm = &vlib_thread_main; \
323 VLIB_REMOVE_FROM_LINKED_LIST (tm->next, &x, next); \
324} \
Dave Barach9b8ffd92016-07-08 08:13:45 -0400325__VA_ARGS__ vlib_thread_registration_t x
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326
Damjan Marion64034362016-11-07 22:19:55 +0100327always_inline u32
328vlib_num_workers ()
329{
330 return vlib_thread_main.n_vlib_mains - 1;
331}
332
333always_inline u32
Damjan Marion586afd72017-04-05 19:18:20 +0200334vlib_get_worker_thread_index (u32 worker_index)
Damjan Marion64034362016-11-07 22:19:55 +0100335{
336 return worker_index + 1;
337}
338
Damjan Marion3a4ed392016-11-08 13:20:42 +0100339always_inline u32
Damjan Marion586afd72017-04-05 19:18:20 +0200340vlib_get_worker_index (u32 thread_index)
Damjan Marion3a4ed392016-11-08 13:20:42 +0100341{
Damjan Marion586afd72017-04-05 19:18:20 +0200342 return thread_index - 1;
Damjan Marion3a4ed392016-11-08 13:20:42 +0100343}
344
345always_inline u32
346vlib_get_current_worker_index ()
347{
Damjan Marion586afd72017-04-05 19:18:20 +0200348 return vlib_get_thread_index () - 1;
Damjan Marion3a4ed392016-11-08 13:20:42 +0100349}
350
Damjan Marionce359db2017-03-16 16:15:38 +0100351static inline void
352vlib_worker_thread_barrier_check (void)
353{
354 if (PREDICT_FALSE (*vlib_worker_threads->wait_at_barrier))
355 {
Damjan Marionfd8deb42021-03-06 12:26:28 +0100356 vlib_global_main_t *vgm = vlib_get_global_main ();
Dave Barach88c6e002018-09-30 15:54:06 -0400357 vlib_main_t *vm = vlib_get_main ();
358 u32 thread_index = vm->thread_index;
359 f64 t = vlib_time_now (vm);
360
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000361 if (PREDICT_FALSE (vec_len (vm->barrier_perf_callbacks) != 0))
362 clib_call_callbacks (vm->barrier_perf_callbacks, vm,
363 vm->clib_time.last_cpu_time, 0 /* enter */ );
364
Dave Barach88c6e002018-09-30 15:54:06 -0400365 if (PREDICT_FALSE (vlib_worker_threads->barrier_elog_enabled))
366 {
367 vlib_worker_thread_t *w = vlib_worker_threads + thread_index;
Dave Barach88c6e002018-09-30 15:54:06 -0400368 ELOG_TYPE_DECLARE (e) = {
369 .format = "barrier-wait-thread-%d",
370 .format_args = "i4",
371 };
Dave Barach88c6e002018-09-30 15:54:06 -0400372
373 struct
374 {
375 u32 thread_index;
376 } __clib_packed *ed;
377
Damjan Marionf553a2c2021-03-26 13:45:37 +0100378 ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, w->elog_track);
Dave Barach88c6e002018-09-30 15:54:06 -0400379 ed->thread_index = thread_index;
380 }
381
Damjan Marionce359db2017-03-16 16:15:38 +0100382 if (CLIB_DEBUG > 0)
383 {
384 vm = vlib_get_main ();
385 vm->parked_at_barrier = 1;
386 }
Alexander Kabaevfeda5452019-11-01 18:26:20 -0400387 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, 1);
Damjan Marionce359db2017-03-16 16:15:38 +0100388 while (*vlib_worker_threads->wait_at_barrier)
389 ;
Dave Baracha4324a92019-02-19 17:05:30 -0500390
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 Marionfd8deb42021-03-06 12:26:28 +0100401 vm->time_offset = vgm->vlib_mains[0]->time_last_barrier_release - now;
Dave Baracha4324a92019-02-19 17:05:30 -0500402 vm->time_last_barrier_release = vlib_time_now (vm);
403 }
404
Damjan Marionce359db2017-03-16 16:15:38 +0100405 if (CLIB_DEBUG > 0)
406 vm->parked_at_barrier = 0;
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000407 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, -1);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100408
409 if (PREDICT_FALSE (*vlib_worker_threads->node_reforks_required))
410 {
Dave Barach88c6e002018-09-30 15:54:06 -0400411 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;
Dave Barach88c6e002018-09-30 15:54:06 -0400415 ELOG_TYPE_DECLARE (e) = {
416 .format = "barrier-refork-thread-%d",
417 .format_args = "i4",
418 };
Dave Barach88c6e002018-09-30 15:54:06 -0400419
420 struct
421 {
422 u32 thread_index;
423 } __clib_packed *ed;
424
425 ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e,
426 w->elog_track);
427 ed->thread_index = thread_index;
428 }
429
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100430 vlib_worker_thread_node_refork ();
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000431 clib_atomic_fetch_add (vlib_worker_threads->node_reforks_required,
432 -1);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100433 while (*vlib_worker_threads->node_reforks_required)
434 ;
435 }
Dave Barach88c6e002018-09-30 15:54:06 -0400436 if (PREDICT_FALSE (vlib_worker_threads->barrier_elog_enabled))
437 {
438 t = vlib_time_now (vm) - t;
439 vlib_worker_thread_t *w = vlib_worker_threads + thread_index;
Dave Barach88c6e002018-09-30 15:54:06 -0400440 ELOG_TYPE_DECLARE (e) = {
441 .format = "barrier-released-thread-%d: %dus",
442 .format_args = "i4i4",
443 };
Dave Barach88c6e002018-09-30 15:54:06 -0400444
445 struct
446 {
447 u32 thread_index;
448 u32 duration;
449 } __clib_packed *ed;
450
Damjan Marionf553a2c2021-03-26 13:45:37 +0100451 ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, w->elog_track);
Dave Barach88c6e002018-09-30 15:54:06 -0400452 ed->thread_index = thread_index;
453 ed->duration = (int) (1000000.0 * t);
454 }
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000455
456 if (PREDICT_FALSE (vec_len (vm->barrier_perf_callbacks) != 0))
457 clib_call_callbacks (vm->barrier_perf_callbacks, vm,
458 vm->clib_time.last_cpu_time, 1 /* leave */ );
Damjan Marionce359db2017-03-16 16:15:38 +0100459 }
460}
461
Damjan Marion64034362016-11-07 22:19:55 +0100462always_inline vlib_main_t *
463vlib_get_worker_vlib_main (u32 worker_index)
464{
465 vlib_main_t *vm;
466 vlib_thread_main_t *tm = &vlib_thread_main;
467 ASSERT (worker_index < tm->n_vlib_mains - 1);
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100468 vm = vlib_get_main_by_index (worker_index + 1);
Damjan Marion64034362016-11-07 22:19:55 +0100469 ASSERT (vm);
470 return vm;
471}
472
Florin Coras568ebc72018-09-18 16:12:50 -0700473static inline u8
474vlib_thread_is_main_w_barrier (void)
475{
476 return (!vlib_num_workers ()
477 || ((vlib_get_thread_index () == 0
478 && vlib_worker_threads->wait_at_barrier[0])));
479}
480
Damjan Marion586afd72017-04-05 19:18:20 +0200481u8 *vlib_thread_stack_init (uword thread_index);
Dave Barach69128d02017-09-26 10:54:34 -0400482extern void *rpc_call_main_thread_cb_fn;
483
484void
485vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t *
486 args);
487void vlib_rpc_call_main_thread (void *function, u8 * args, u32 size);
Dave Baracha690fdb2020-01-21 12:34:55 -0500488void vlib_get_thread_core_numa (vlib_worker_thread_t * w, unsigned cpu_id);
Dave Barachab1a50c2020-10-06 14:08:16 -0400489vlib_thread_main_t *vlib_get_thread_main_not_inline (void);
Dave Baracha690fdb2020-01-21 12:34:55 -0500490
Florin Coras4b208302022-03-30 13:50:19 -0700491/**
492 * Force workers sync from within worker
493 *
494 * Must be paired with @ref vlib_workers_continue
495 */
496void vlib_workers_sync (void);
497/**
498 * Release barrier after workers sync
499 */
500void vlib_workers_continue (void);
501
Ed Warnickecb9cada2015-12-08 15:45:58 -0700502#endif /* included_vlib_threads_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400503
504/*
505 * fd.io coding-style-patch-verification: ON
506 *
507 * Local Variables:
508 * eval: (c-set-style "gnu")
509 * End:
510 */