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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef included_vlib_threads_h
16 #define included_vlib_threads_h
18 #include <vlib/main.h>
19 #include <vppinfra/callback.h>
20 #include <linux/sched.h>
22 void vlib_set_thread_name (char *name);
24 /* arg is actually a vlib__thread_t * */
25 typedef void (vlib_thread_function_t) (void *arg);
27 typedef struct vlib_thread_registration_
29 /* constructor generated list of thread registrations */
30 struct vlib_thread_registration_ *next;
32 /* config parameters */
35 vlib_thread_function_t *function;
39 int no_data_structure_clone;
40 u32 frame_queue_nelts;
42 /* All threads of this type run on pthreads */
46 } vlib_thread_registration_t;
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...
54 #define VLIB_MAX_CPUS 256
57 #if VLIB_MAX_CPUS > CLIB_MAX_MHEAPS
58 #error Please increase number of per-cpu mheaps
61 #define VLIB_CPU_MASK (VLIB_MAX_CPUS - 1) /* 0x3f, max */
62 #define VLIB_OFFSET_MASK (~VLIB_CPU_MASK)
64 #define VLIB_LOG2_THREAD_STACK_SIZE (21)
65 #define VLIB_THREAD_STACK_SIZE (1<<VLIB_LOG2_THREAD_STACK_SIZE)
69 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
74 STRUCT_MARK (end_of_reset);
76 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
77 u32 buffer_index[VLIB_FRAME_SIZE];
79 vlib_frame_queue_elt_t;
83 /* First cache line */
84 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
85 volatile u32 *wait_at_barrier;
86 volatile u32 *workers_at_barrier;
88 /* Second Cache Line */
89 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
92 void (*thread_function) (void *);
93 void *thread_function_arg;
95 elog_track_t elog_track;
97 vlib_thread_registration_t *registration;
99 u64 barrier_sync_count;
100 u8 barrier_elog_enabled;
101 const char *barrier_caller;
102 const char *barrier_context;
103 volatile u32 *node_reforks_required;
104 volatile u32 wait_before_barrier;
105 volatile u32 workers_before_barrier;
106 volatile u32 done_work_before_barrier;
113 } vlib_worker_thread_t;
115 extern vlib_worker_thread_t *vlib_worker_threads;
120 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
121 vlib_frame_queue_elt_t *elts;
122 u64 vector_threshold;
126 /* modified by enqueue side */
127 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
130 /* modified by dequeue side */
131 CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
139 u32 frame_queue_nelts;
141 vlib_frame_queue_t **vlib_frame_queues;
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;
153 } vlib_process_signal_event_mt_args_t;
155 /* Called early, in thread 0's context */
156 clib_error_t *vlib_thread_init (vlib_main_t * vm);
158 void vlib_worker_thread_node_runtime_update (void);
160 void vlib_create_worker_threads (vlib_main_t * vm, int n,
161 void (*thread_function) (void *));
163 void vlib_worker_thread_init (vlib_worker_thread_t * w);
164 u32 vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts);
166 /* Check for a barrier sync request every 30ms */
167 #define BARRIER_SYNC_DELAY (0.030000)
170 /* long barrier timeout, for gdb... */
171 #define BARRIER_SYNC_TIMEOUT (600.1)
173 #define BARRIER_SYNC_TIMEOUT (1.0)
176 #define vlib_worker_thread_barrier_sync(X) {vlib_worker_thread_barrier_sync_int(X, __FUNCTION__);}
178 void vlib_worker_thread_barrier_sync_int (vlib_main_t * vm,
179 const char *func_name);
180 void vlib_worker_thread_barrier_release (vlib_main_t * vm);
181 u8 vlib_worker_thread_barrier_held (void);
182 void vlib_worker_thread_initial_barrier_sync_and_release (vlib_main_t * vm);
183 void vlib_worker_thread_node_refork (void);
185 * Wait until each of the workers has been once around the track
187 void vlib_worker_wait_one_loop (void);
189 static_always_inline uword
190 vlib_get_thread_index (void)
192 return __os_thread_index;
196 vlib_smp_unsafe_warning (void)
200 if (vlib_get_thread_index ())
201 fformat (stderr, "%s: SMP unsafe warning...\n", __FUNCTION__);
206 __foreach_vlib_main_helper (vlib_main_t *ii, vlib_main_t **p)
209 u32 index = ii - (vlib_main_t *) 0;
211 if (index >= vec_len (vlib_global_main.vlib_mains))
214 *p = vm = vlib_global_main.vlib_mains[index];
215 ASSERT (index == 0 || vm->parked_at_barrier == 1);
219 #define foreach_vlib_main() \
220 for (vlib_main_t *ii = 0, *this_vlib_main; \
221 __foreach_vlib_main_helper (ii, &this_vlib_main); ii++) \
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")
233 #define _(v,f,s) SCHED_POLICY_##f = v,
241 /* Link list of registrations, built by constructors */
242 vlib_thread_registration_t *next;
244 /* Vector of registrations, w/ non-data-structure clones at the top */
245 vlib_thread_registration_t **registrations;
247 uword *thread_registrations_by_name;
249 vlib_worker_thread_t *worker_threads;
253 /* Number of vlib_main / vnet_main clones */
256 /* Number of thread stacks to create */
259 /* Number of pthreads */
262 /* Number of threads */
265 /* Number of cores to skip, must match the core mask */
268 /* Thread prefix name */
271 /* main thread lcore */
274 /* Bitmap of available CPU cores */
275 uword *cpu_core_bitmap;
277 /* Bitmap of available CPU sockets (NUMA nodes) */
278 uword *cpu_socket_bitmap;
280 /* Worker handoff queues */
281 vlib_frame_queue_main_t *frame_queue_mains;
283 /* worker thread initialization barrier */
284 volatile u32 worker_thread_release;
286 /* scheduling policy */
289 /* scheduling policy priority */
292 /* NUMA-bound heap size */
293 uword numa_heap_size;
295 } vlib_thread_main_t;
297 extern vlib_thread_main_t vlib_thread_main;
299 #include <vlib/global_funcs.h>
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) \
307 vlib_thread_main_t * tm = &vlib_thread_main; \
311 static void __vlib_rm_thread_registration_##x (void) \
312 __attribute__((__destructor__)) ; \
313 static void __vlib_rm_thread_registration_##x (void) \
315 vlib_thread_main_t * tm = &vlib_thread_main; \
316 VLIB_REMOVE_FROM_LINKED_LIST (tm->next, &x, next); \
318 __VA_ARGS__ vlib_thread_registration_t x
323 return vlib_thread_main.n_vlib_mains - 1;
327 vlib_get_worker_thread_index (u32 worker_index)
329 return worker_index + 1;
333 vlib_get_worker_index (u32 thread_index)
335 return thread_index - 1;
339 vlib_get_current_worker_index ()
341 return vlib_get_thread_index () - 1;
345 vlib_worker_thread_barrier_check (void)
347 if (PREDICT_FALSE (*vlib_worker_threads->wait_at_barrier))
349 vlib_global_main_t *vgm = vlib_get_global_main ();
350 vlib_main_t *vm = vlib_get_main ();
351 u32 thread_index = vm->thread_index;
352 f64 t = vlib_time_now (vm);
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 */ );
358 if (PREDICT_FALSE (vlib_worker_threads->barrier_elog_enabled))
360 vlib_worker_thread_t *w = vlib_worker_threads + thread_index;
362 ELOG_TYPE_DECLARE (e) = {
363 .format = "barrier-wait-thread-%d",
373 ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, w->elog_track);
374 ed->thread_index = thread_index;
379 vm = vlib_get_main ();
380 vm->parked_at_barrier = 1;
382 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, 1);
383 while (*vlib_worker_threads->wait_at_barrier)
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(...)
394 vm->time_offset = 0.0;
395 now = vlib_time_now (vm);
396 vm->time_offset = vgm->vlib_mains[0]->time_last_barrier_release - now;
397 vm->time_last_barrier_release = vlib_time_now (vm);
401 vm->parked_at_barrier = 0;
402 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, -1);
404 if (PREDICT_FALSE (*vlib_worker_threads->node_reforks_required))
406 if (PREDICT_FALSE (vlib_worker_threads->barrier_elog_enabled))
408 t = vlib_time_now (vm) - t;
409 vlib_worker_thread_t *w = vlib_worker_threads + thread_index;
411 ELOG_TYPE_DECLARE (e) = {
412 .format = "barrier-refork-thread-%d",
422 ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e,
424 ed->thread_index = thread_index;
427 vlib_worker_thread_node_refork ();
428 clib_atomic_fetch_add (vlib_worker_threads->node_reforks_required,
430 while (*vlib_worker_threads->node_reforks_required)
433 if (PREDICT_FALSE (vlib_worker_threads->barrier_elog_enabled))
435 t = vlib_time_now (vm) - t;
436 vlib_worker_thread_t *w = vlib_worker_threads + thread_index;
438 ELOG_TYPE_DECLARE (e) = {
439 .format = "barrier-released-thread-%d: %dus",
440 .format_args = "i4i4",
450 ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, w->elog_track);
451 ed->thread_index = thread_index;
452 ed->duration = (int) (1000000.0 * t);
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 */ );
461 always_inline vlib_main_t *
462 vlib_get_worker_vlib_main (u32 worker_index)
465 vlib_thread_main_t *tm = &vlib_thread_main;
466 ASSERT (worker_index < tm->n_vlib_mains - 1);
467 vm = vlib_get_main_by_index (worker_index + 1);
473 vlib_thread_is_main_w_barrier (void)
475 return (!vlib_num_workers ()
476 || ((vlib_get_thread_index () == 0
477 && vlib_worker_threads->wait_at_barrier[0])));
480 u8 *vlib_thread_stack_init (uword thread_index);
481 extern void *rpc_call_main_thread_cb_fn;
484 vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t *
486 void vlib_rpc_call_main_thread (void *function, u8 * args, u32 size);
487 void vlib_get_thread_core_numa (vlib_worker_thread_t * w, unsigned cpu_id);
488 vlib_thread_main_t *vlib_get_thread_main_not_inline (void);
491 * Force workers sync from within worker
493 * Must be paired with @ref vlib_workers_continue
495 void vlib_workers_sync (void);
497 * Release barrier after workers sync
499 void vlib_workers_continue (void);
501 #endif /* included_vlib_threads_h */
504 * fd.io coding-style-patch-verification: ON
507 * eval: (c-set-style "gnu")