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 VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME,
70 } vlib_frame_queue_msg_type_t;
74 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
80 /* 256 * 4 = 1024 bytes, even mult of cache line size */
81 u32 buffer_index[VLIB_FRAME_SIZE];
83 vlib_frame_queue_elt_t;
87 /* First cache line */
88 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
89 volatile u32 *wait_at_barrier;
90 volatile u32 *workers_at_barrier;
92 /* Second Cache Line */
93 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
96 void (*thread_function) (void *);
97 void *thread_function_arg;
99 elog_track_t elog_track;
101 vlib_thread_registration_t *registration;
103 u64 barrier_sync_count;
104 u8 barrier_elog_enabled;
105 const char *barrier_caller;
106 const char *barrier_context;
107 volatile u32 *node_reforks_required;
114 } vlib_worker_thread_t;
116 extern vlib_worker_thread_t *vlib_worker_threads;
121 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
126 u32 enqueue_full_events;
129 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
135 u64 vector_threshold;
137 /* dequeue hint to enqueue side */
138 CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
139 volatile u64 head_hint;
141 /* read-only, constant, shared */
142 CLIB_CACHE_LINE_ALIGN_MARK (cacheline3);
143 vlib_frame_queue_elt_t *elts;
150 vlib_frame_queue_elt_t **handoff_queue_elt_by_thread_index;
151 vlib_frame_queue_t **congested_handoff_queue_by_thread_index;
152 } vlib_frame_queue_per_thread_data_t;
157 u32 frame_queue_nelts;
160 vlib_frame_queue_t **vlib_frame_queues;
161 vlib_frame_queue_per_thread_data_t *per_thread_data;
163 /* for frame queue tracing */
164 frame_queue_trace_t *frame_queue_traces;
165 frame_queue_nelt_counter_t *frame_queue_histogram;
166 } vlib_frame_queue_main_t;
173 } vlib_process_signal_event_mt_args_t;
175 /* Called early, in thread 0's context */
176 clib_error_t *vlib_thread_init (vlib_main_t * vm);
178 int vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
179 u32 frame_queue_index, vlib_frame_t * frame,
180 vlib_frame_queue_msg_type_t type);
183 vlib_frame_queue_dequeue (vlib_main_t * vm, vlib_frame_queue_main_t * fqm);
185 void vlib_worker_thread_node_runtime_update (void);
187 void vlib_create_worker_threads (vlib_main_t * vm, int n,
188 void (*thread_function) (void *));
190 void vlib_worker_thread_init (vlib_worker_thread_t * w);
191 u32 vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts);
193 /* Check for a barrier sync request every 30ms */
194 #define BARRIER_SYNC_DELAY (0.030000)
197 /* long barrier timeout, for gdb... */
198 #define BARRIER_SYNC_TIMEOUT (600.1)
200 #define BARRIER_SYNC_TIMEOUT (1.0)
203 #define vlib_worker_thread_barrier_sync(X) {vlib_worker_thread_barrier_sync_int(X, __FUNCTION__);}
205 void vlib_worker_thread_barrier_sync_int (vlib_main_t * vm,
206 const char *func_name);
207 void vlib_worker_thread_barrier_release (vlib_main_t * vm);
208 u8 vlib_worker_thread_barrier_held (void);
209 void vlib_worker_thread_initial_barrier_sync_and_release (vlib_main_t * vm);
210 void vlib_worker_thread_node_refork (void);
212 * Wait until each of the workers has been once around the track
214 void vlib_worker_wait_one_loop (void);
216 static_always_inline uword
217 vlib_get_thread_index (void)
219 return __os_thread_index;
223 vlib_smp_unsafe_warning (void)
227 if (vlib_get_thread_index ())
228 fformat (stderr, "%s: SMP unsafe warning...\n", __FUNCTION__);
233 __foreach_vlib_main_helper (vlib_main_t *ii, vlib_main_t **p)
236 u32 index = ii - (vlib_main_t *) 0;
238 if (index >= vec_len (vlib_global_main.vlib_mains))
241 *p = vm = vlib_global_main.vlib_mains[index];
242 ASSERT (index == 0 || vm->parked_at_barrier == 1);
246 #define foreach_vlib_main() \
247 for (vlib_main_t *ii = 0, *this_vlib_main; \
248 __foreach_vlib_main_helper (ii, &this_vlib_main); ii++) \
251 #define foreach_sched_policy \
252 _(SCHED_OTHER, OTHER, "other") \
253 _(SCHED_BATCH, BATCH, "batch") \
254 _(SCHED_IDLE, IDLE, "idle") \
255 _(SCHED_FIFO, FIFO, "fifo") \
256 _(SCHED_RR, RR, "rr")
260 #define _(v,f,s) SCHED_POLICY_##f = v,
268 clib_error_t *(*vlib_launch_thread_cb) (void *fp, vlib_worker_thread_t * w,
270 clib_error_t *(*vlib_thread_set_lcore_cb) (u32 thread, u16 cpu);
271 } vlib_thread_callbacks_t;
275 /* Link list of registrations, built by constructors */
276 vlib_thread_registration_t *next;
278 /* Vector of registrations, w/ non-data-structure clones at the top */
279 vlib_thread_registration_t **registrations;
281 uword *thread_registrations_by_name;
283 vlib_worker_thread_t *worker_threads;
286 * Launch all threads as pthreads,
287 * not eal_rte_launch (strict affinity) threads
291 /* Number of vlib_main / vnet_main clones */
294 /* Number of thread stacks to create */
297 /* Number of pthreads */
300 /* Number of threads */
303 /* Number of cores to skip, must match the core mask */
306 /* Thread prefix name */
309 /* main thread lcore */
312 /* Bitmap of available CPU cores */
313 uword *cpu_core_bitmap;
315 /* Bitmap of available CPU sockets (NUMA nodes) */
316 uword *cpu_socket_bitmap;
318 /* Worker handoff queues */
319 vlib_frame_queue_main_t *frame_queue_mains;
321 /* worker thread initialization barrier */
322 volatile u32 worker_thread_release;
324 /* scheduling policy */
327 /* scheduling policy priority */
331 vlib_thread_callbacks_t cb;
332 int extern_thread_mgmt;
334 /* NUMA-bound heap size */
335 uword numa_heap_size;
337 } vlib_thread_main_t;
339 extern vlib_thread_main_t vlib_thread_main;
341 #include <vlib/global_funcs.h>
343 #define VLIB_REGISTER_THREAD(x,...) \
344 __VA_ARGS__ vlib_thread_registration_t x; \
345 static void __vlib_add_thread_registration_##x (void) \
346 __attribute__((__constructor__)) ; \
347 static void __vlib_add_thread_registration_##x (void) \
349 vlib_thread_main_t * tm = &vlib_thread_main; \
353 static void __vlib_rm_thread_registration_##x (void) \
354 __attribute__((__destructor__)) ; \
355 static void __vlib_rm_thread_registration_##x (void) \
357 vlib_thread_main_t * tm = &vlib_thread_main; \
358 VLIB_REMOVE_FROM_LINKED_LIST (tm->next, &x, next); \
360 __VA_ARGS__ vlib_thread_registration_t x
365 return vlib_thread_main.n_vlib_mains - 1;
369 vlib_get_worker_thread_index (u32 worker_index)
371 return worker_index + 1;
375 vlib_get_worker_index (u32 thread_index)
377 return thread_index - 1;
381 vlib_get_current_worker_index ()
383 return vlib_get_thread_index () - 1;
387 vlib_worker_thread_barrier_check (void)
389 if (PREDICT_FALSE (*vlib_worker_threads->wait_at_barrier))
391 vlib_global_main_t *vgm = vlib_get_global_main ();
392 vlib_main_t *vm = vlib_get_main ();
393 u32 thread_index = vm->thread_index;
394 f64 t = vlib_time_now (vm);
396 if (PREDICT_FALSE (vec_len (vm->barrier_perf_callbacks) != 0))
397 clib_call_callbacks (vm->barrier_perf_callbacks, vm,
398 vm->clib_time.last_cpu_time, 0 /* enter */ );
400 if (PREDICT_FALSE (vlib_worker_threads->barrier_elog_enabled))
402 vlib_worker_thread_t *w = vlib_worker_threads + thread_index;
404 ELOG_TYPE_DECLARE (e) = {
405 .format = "barrier-wait-thread-%d",
415 ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, w->elog_track);
416 ed->thread_index = thread_index;
421 vm = vlib_get_main ();
422 vm->parked_at_barrier = 1;
424 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, 1);
425 while (*vlib_worker_threads->wait_at_barrier)
429 * Recompute the offset from thread-0 time.
430 * Note that vlib_time_now adds vm->time_offset, so
431 * clear it first. Save the resulting idea of "now", to
432 * see how well we're doing. See show_clock_command_fn(...)
436 vm->time_offset = 0.0;
437 now = vlib_time_now (vm);
438 vm->time_offset = vgm->vlib_mains[0]->time_last_barrier_release - now;
439 vm->time_last_barrier_release = vlib_time_now (vm);
443 vm->parked_at_barrier = 0;
444 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, -1);
446 if (PREDICT_FALSE (*vlib_worker_threads->node_reforks_required))
448 if (PREDICT_FALSE (vlib_worker_threads->barrier_elog_enabled))
450 t = vlib_time_now (vm) - t;
451 vlib_worker_thread_t *w = vlib_worker_threads + thread_index;
453 ELOG_TYPE_DECLARE (e) = {
454 .format = "barrier-refork-thread-%d",
464 ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e,
466 ed->thread_index = thread_index;
469 vlib_worker_thread_node_refork ();
470 clib_atomic_fetch_add (vlib_worker_threads->node_reforks_required,
472 while (*vlib_worker_threads->node_reforks_required)
475 if (PREDICT_FALSE (vlib_worker_threads->barrier_elog_enabled))
477 t = vlib_time_now (vm) - t;
478 vlib_worker_thread_t *w = vlib_worker_threads + thread_index;
480 ELOG_TYPE_DECLARE (e) = {
481 .format = "barrier-released-thread-%d: %dus",
482 .format_args = "i4i4",
492 ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, w->elog_track);
493 ed->thread_index = thread_index;
494 ed->duration = (int) (1000000.0 * t);
497 if (PREDICT_FALSE (vec_len (vm->barrier_perf_callbacks) != 0))
498 clib_call_callbacks (vm->barrier_perf_callbacks, vm,
499 vm->clib_time.last_cpu_time, 1 /* leave */ );
503 always_inline vlib_main_t *
504 vlib_get_worker_vlib_main (u32 worker_index)
507 vlib_thread_main_t *tm = &vlib_thread_main;
508 ASSERT (worker_index < tm->n_vlib_mains - 1);
509 vm = vlib_get_main_by_index (worker_index + 1);
515 vlib_thread_is_main_w_barrier (void)
517 return (!vlib_num_workers ()
518 || ((vlib_get_thread_index () == 0
519 && vlib_worker_threads->wait_at_barrier[0])));
523 vlib_put_frame_queue_elt (vlib_frame_queue_elt_t * hf)
525 CLIB_MEMORY_BARRIER ();
529 static inline vlib_frame_queue_elt_t *
530 vlib_get_frame_queue_elt (u32 frame_queue_index, u32 index)
532 vlib_frame_queue_t *fq;
533 vlib_frame_queue_elt_t *elt;
534 vlib_thread_main_t *tm = &vlib_thread_main;
535 vlib_frame_queue_main_t *fqm =
536 vec_elt_at_index (tm->frame_queue_mains, frame_queue_index);
539 fq = fqm->vlib_frame_queues[index];
542 new_tail = clib_atomic_add_fetch (&fq->tail, 1);
544 /* Wait until a ring slot is available */
545 while (new_tail >= fq->head_hint + fq->nelts)
546 vlib_worker_thread_barrier_check ();
548 elt = fq->elts + (new_tail & (fq->nelts - 1));
550 /* this would be very bad... */
554 elt->msg_type = VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME;
555 elt->last_n_vectors = elt->n_vectors = 0;
560 static inline vlib_frame_queue_t *
561 is_vlib_frame_queue_congested (u32 frame_queue_index,
564 vlib_frame_queue_t **
565 handoff_queue_by_worker_index)
567 vlib_frame_queue_t *fq;
568 vlib_thread_main_t *tm = &vlib_thread_main;
569 vlib_frame_queue_main_t *fqm =
570 vec_elt_at_index (tm->frame_queue_mains, frame_queue_index);
572 fq = handoff_queue_by_worker_index[index];
573 if (fq != (vlib_frame_queue_t *) (~0))
576 fq = fqm->vlib_frame_queues[index];
579 if (PREDICT_FALSE (fq->tail >= (fq->head_hint + queue_hi_thresh)))
581 /* a valid entry in the array will indicate the queue has reached
582 * the specified threshold and is congested
584 handoff_queue_by_worker_index[index] = fq;
585 fq->enqueue_full_events++;
592 static inline vlib_frame_queue_elt_t *
593 vlib_get_worker_handoff_queue_elt (u32 frame_queue_index,
594 u32 vlib_worker_index,
595 vlib_frame_queue_elt_t **
596 handoff_queue_elt_by_worker_index)
598 vlib_frame_queue_elt_t *elt;
600 if (handoff_queue_elt_by_worker_index[vlib_worker_index])
601 return handoff_queue_elt_by_worker_index[vlib_worker_index];
603 elt = vlib_get_frame_queue_elt (frame_queue_index, vlib_worker_index);
605 handoff_queue_elt_by_worker_index[vlib_worker_index] = elt;
610 u8 *vlib_thread_stack_init (uword thread_index);
611 int vlib_thread_cb_register (struct vlib_main_t *vm,
612 vlib_thread_callbacks_t * cb);
613 extern void *rpc_call_main_thread_cb_fn;
616 vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t *
618 void vlib_rpc_call_main_thread (void *function, u8 * args, u32 size);
619 void vlib_get_thread_core_numa (vlib_worker_thread_t * w, unsigned cpu_id);
620 vlib_thread_main_t *vlib_get_thread_main_not_inline (void);
622 #endif /* included_vlib_threads_h */
625 * fd.io coding-style-patch-verification: ON
628 * eval: (c-set-style "gnu")