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);
123 u32 enqueue_full_events;
126 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
129 u64 vector_threshold;
131 /* dequeue hint to enqueue side */
132 CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
133 volatile u64 head_hint;
135 /* read-only, constant, shared */
136 CLIB_CACHE_LINE_ALIGN_MARK (cacheline3);
137 vlib_frame_queue_elt_t *elts;
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;
151 u32 frame_queue_nelts;
154 vlib_frame_queue_t **vlib_frame_queues;
155 vlib_frame_queue_per_thread_data_t *per_thread_data;
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;
167 } vlib_process_signal_event_mt_args_t;
169 /* Called early, in thread 0's context */
170 clib_error_t *vlib_thread_init (vlib_main_t * vm);
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);
176 void vlib_worker_thread_node_runtime_update (void);
178 void vlib_create_worker_threads (vlib_main_t * vm, int n,
179 void (*thread_function) (void *));
181 void vlib_worker_thread_init (vlib_worker_thread_t * w);
182 u32 vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts);
184 /* Check for a barrier sync request every 30ms */
185 #define BARRIER_SYNC_DELAY (0.030000)
188 /* long barrier timeout, for gdb... */
189 #define BARRIER_SYNC_TIMEOUT (600.1)
191 #define BARRIER_SYNC_TIMEOUT (1.0)
194 #define vlib_worker_thread_barrier_sync(X) {vlib_worker_thread_barrier_sync_int(X, __FUNCTION__);}
196 void vlib_worker_thread_barrier_sync_int (vlib_main_t * vm,
197 const char *func_name);
198 void vlib_worker_thread_barrier_release (vlib_main_t * vm);
199 u8 vlib_worker_thread_barrier_held (void);
200 void vlib_worker_thread_initial_barrier_sync_and_release (vlib_main_t * vm);
201 void vlib_worker_thread_node_refork (void);
203 * Wait until each of the workers has been once around the track
205 void vlib_worker_wait_one_loop (void);
207 static_always_inline uword
208 vlib_get_thread_index (void)
210 return __os_thread_index;
214 vlib_smp_unsafe_warning (void)
218 if (vlib_get_thread_index ())
219 fformat (stderr, "%s: SMP unsafe warning...\n", __FUNCTION__);
224 __foreach_vlib_main_helper (vlib_main_t *ii, vlib_main_t **p)
227 u32 index = ii - (vlib_main_t *) 0;
229 if (index >= vec_len (vlib_global_main.vlib_mains))
232 *p = vm = vlib_global_main.vlib_mains[index];
233 ASSERT (index == 0 || vm->parked_at_barrier == 1);
237 #define foreach_vlib_main() \
238 for (vlib_main_t *ii = 0, *this_vlib_main; \
239 __foreach_vlib_main_helper (ii, &this_vlib_main); ii++) \
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")
251 #define _(v,f,s) SCHED_POLICY_##f = v,
259 clib_error_t *(*vlib_launch_thread_cb) (void *fp, vlib_worker_thread_t * w,
261 clib_error_t *(*vlib_thread_set_lcore_cb) (u32 thread, u16 cpu);
262 } vlib_thread_callbacks_t;
266 /* Link list of registrations, built by constructors */
267 vlib_thread_registration_t *next;
269 /* Vector of registrations, w/ non-data-structure clones at the top */
270 vlib_thread_registration_t **registrations;
272 uword *thread_registrations_by_name;
274 vlib_worker_thread_t *worker_threads;
277 * Launch all threads as pthreads,
278 * not eal_rte_launch (strict affinity) threads
282 /* Number of vlib_main / vnet_main clones */
285 /* Number of thread stacks to create */
288 /* Number of pthreads */
291 /* Number of threads */
294 /* Number of cores to skip, must match the core mask */
297 /* Thread prefix name */
300 /* main thread lcore */
303 /* Bitmap of available CPU cores */
304 uword *cpu_core_bitmap;
306 /* Bitmap of available CPU sockets (NUMA nodes) */
307 uword *cpu_socket_bitmap;
309 /* Worker handoff queues */
310 vlib_frame_queue_main_t *frame_queue_mains;
312 /* worker thread initialization barrier */
313 volatile u32 worker_thread_release;
315 /* scheduling policy */
318 /* scheduling policy priority */
322 vlib_thread_callbacks_t cb;
323 int extern_thread_mgmt;
325 /* NUMA-bound heap size */
326 uword numa_heap_size;
328 } vlib_thread_main_t;
330 extern vlib_thread_main_t vlib_thread_main;
332 #include <vlib/global_funcs.h>
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) \
340 vlib_thread_main_t * tm = &vlib_thread_main; \
344 static void __vlib_rm_thread_registration_##x (void) \
345 __attribute__((__destructor__)) ; \
346 static void __vlib_rm_thread_registration_##x (void) \
348 vlib_thread_main_t * tm = &vlib_thread_main; \
349 VLIB_REMOVE_FROM_LINKED_LIST (tm->next, &x, next); \
351 __VA_ARGS__ vlib_thread_registration_t x
356 return vlib_thread_main.n_vlib_mains - 1;
360 vlib_get_worker_thread_index (u32 worker_index)
362 return worker_index + 1;
366 vlib_get_worker_index (u32 thread_index)
368 return thread_index - 1;
372 vlib_get_current_worker_index ()
374 return vlib_get_thread_index () - 1;
378 vlib_worker_thread_barrier_check (void)
380 if (PREDICT_FALSE (*vlib_worker_threads->wait_at_barrier))
382 vlib_global_main_t *vgm = vlib_get_global_main ();
383 vlib_main_t *vm = vlib_get_main ();
384 u32 thread_index = vm->thread_index;
385 f64 t = vlib_time_now (vm);
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 */ );
391 if (PREDICT_FALSE (vlib_worker_threads->barrier_elog_enabled))
393 vlib_worker_thread_t *w = vlib_worker_threads + thread_index;
395 ELOG_TYPE_DECLARE (e) = {
396 .format = "barrier-wait-thread-%d",
406 ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, w->elog_track);
407 ed->thread_index = thread_index;
412 vm = vlib_get_main ();
413 vm->parked_at_barrier = 1;
415 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, 1);
416 while (*vlib_worker_threads->wait_at_barrier)
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(...)
427 vm->time_offset = 0.0;
428 now = vlib_time_now (vm);
429 vm->time_offset = vgm->vlib_mains[0]->time_last_barrier_release - now;
430 vm->time_last_barrier_release = vlib_time_now (vm);
434 vm->parked_at_barrier = 0;
435 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, -1);
437 if (PREDICT_FALSE (*vlib_worker_threads->node_reforks_required))
439 if (PREDICT_FALSE (vlib_worker_threads->barrier_elog_enabled))
441 t = vlib_time_now (vm) - t;
442 vlib_worker_thread_t *w = vlib_worker_threads + thread_index;
444 ELOG_TYPE_DECLARE (e) = {
445 .format = "barrier-refork-thread-%d",
455 ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e,
457 ed->thread_index = thread_index;
460 vlib_worker_thread_node_refork ();
461 clib_atomic_fetch_add (vlib_worker_threads->node_reforks_required,
463 while (*vlib_worker_threads->node_reforks_required)
466 if (PREDICT_FALSE (vlib_worker_threads->barrier_elog_enabled))
468 t = vlib_time_now (vm) - t;
469 vlib_worker_thread_t *w = vlib_worker_threads + thread_index;
471 ELOG_TYPE_DECLARE (e) = {
472 .format = "barrier-released-thread-%d: %dus",
473 .format_args = "i4i4",
483 ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, w->elog_track);
484 ed->thread_index = thread_index;
485 ed->duration = (int) (1000000.0 * t);
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 */ );
494 always_inline vlib_main_t *
495 vlib_get_worker_vlib_main (u32 worker_index)
498 vlib_thread_main_t *tm = &vlib_thread_main;
499 ASSERT (worker_index < tm->n_vlib_mains - 1);
500 vm = vlib_get_main_by_index (worker_index + 1);
506 vlib_thread_is_main_w_barrier (void)
508 return (!vlib_num_workers ()
509 || ((vlib_get_thread_index () == 0
510 && vlib_worker_threads->wait_at_barrier[0])));
514 vlib_put_frame_queue_elt (vlib_frame_queue_elt_t * hf)
516 CLIB_MEMORY_BARRIER ();
520 static inline vlib_frame_queue_elt_t *
521 vlib_get_frame_queue_elt (u32 frame_queue_index, u32 index)
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);
530 fq = fqm->vlib_frame_queues[index];
533 new_tail = clib_atomic_add_fetch (&fq->tail, 1);
535 /* Wait until a ring slot is available */
536 while (new_tail >= fq->head_hint + fq->nelts)
537 vlib_worker_thread_barrier_check ();
539 elt = fq->elts + (new_tail & (fq->nelts - 1));
541 /* this would be very bad... */
545 elt->msg_type = VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME;
546 elt->last_n_vectors = elt->n_vectors = 0;
551 static inline vlib_frame_queue_t *
552 is_vlib_frame_queue_congested (u32 frame_queue_index,
555 vlib_frame_queue_t **
556 handoff_queue_by_worker_index)
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);
563 fq = handoff_queue_by_worker_index[index];
564 if (fq != (vlib_frame_queue_t *) (~0))
567 fq = fqm->vlib_frame_queues[index];
570 if (PREDICT_FALSE (fq->tail >= (fq->head_hint + queue_hi_thresh)))
572 /* a valid entry in the array will indicate the queue has reached
573 * the specified threshold and is congested
575 handoff_queue_by_worker_index[index] = fq;
576 fq->enqueue_full_events++;
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)
589 vlib_frame_queue_elt_t *elt;
591 if (handoff_queue_elt_by_worker_index[vlib_worker_index])
592 return handoff_queue_elt_by_worker_index[vlib_worker_index];
594 elt = vlib_get_frame_queue_elt (frame_queue_index, vlib_worker_index);
596 handoff_queue_elt_by_worker_index[vlib_worker_index] = elt;
601 u8 *vlib_thread_stack_init (uword thread_index);
602 int vlib_thread_cb_register (struct vlib_main_t *vm,
603 vlib_thread_callbacks_t * cb);
604 extern void *rpc_call_main_thread_cb_fn;
607 vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t *
609 void vlib_rpc_call_main_thread (void *function, u8 * args, u32 size);
610 void vlib_get_thread_core_numa (vlib_worker_thread_t * w, unsigned cpu_id);
611 vlib_thread_main_t *vlib_get_thread_main_not_inline (void);
613 #endif /* included_vlib_threads_h */
616 * fd.io coding-style-patch-verification: ON
619 * eval: (c-set-style "gnu")