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.
19 #include <vppinfra/format.h>
20 #include <vppinfra/time_range.h>
21 #include <vppinfra/interrupt.h>
22 #include <vppinfra/bitmap.h>
23 #include <vppinfra/unix.h>
24 #include <vlib/vlib.h>
26 #include <vlib/threads.h>
28 #include <vlib/stats/stats.h>
36 vlib_worker_thread_t *vlib_worker_threads;
37 vlib_thread_main_t vlib_thread_main;
40 * Barrier tracing can be enabled on a normal build to collect information
41 * on barrier use, including timings and call stacks. Deliberately not
42 * keyed off CLIB_DEBUG, because that can add significant overhead which
43 * imapacts observed timings.
47 barrier_trace_sync (f64 t_entry, f64 t_open, f64 t_closed)
49 if (!vlib_worker_threads->barrier_elog_enabled)
52 ELOG_TYPE_DECLARE (e) = {
53 .format = "bar-trace-%s-#%d",
54 .format_args = "T4i4",
59 u32 caller, count, t_entry, t_open, t_closed;
62 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
63 ed->count = (int) vlib_worker_threads[0].barrier_sync_count;
64 ed->caller = elog_string (&vlib_global_main.elog_main,
65 (char *) vlib_worker_threads[0].barrier_caller);
66 ed->t_entry = (int) (1000000.0 * t_entry);
67 ed->t_open = (int) (1000000.0 * t_open);
68 ed->t_closed = (int) (1000000.0 * t_closed);
72 barrier_trace_sync_rec (f64 t_entry)
74 if (!vlib_worker_threads->barrier_elog_enabled)
77 ELOG_TYPE_DECLARE (e) = {
78 .format = "bar-syncrec-%s-#%d",
79 .format_args = "T4i4",
87 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
88 ed->depth = (int) vlib_worker_threads[0].recursion_level - 1;
89 ed->caller = elog_string (&vlib_global_main.elog_main,
90 (char *) vlib_worker_threads[0].barrier_caller);
94 barrier_trace_release_rec (f64 t_entry)
96 if (!vlib_worker_threads->barrier_elog_enabled)
99 ELOG_TYPE_DECLARE (e) = {
100 .format = "bar-relrrec-#%d",
109 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
110 ed->depth = (int) vlib_worker_threads[0].recursion_level;
114 barrier_trace_release (f64 t_entry, f64 t_closed_total, f64 t_update_main)
116 if (!vlib_worker_threads->barrier_elog_enabled)
119 ELOG_TYPE_DECLARE (e) = {
120 .format = "bar-rel-#%d-e%d-u%d-t%d",
121 .format_args = "i4i4i4i4",
126 u32 count, t_entry, t_update_main, t_closed_total;
129 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
130 ed->t_entry = (int) (1000000.0 * t_entry);
131 ed->t_update_main = (int) (1000000.0 * t_update_main);
132 ed->t_closed_total = (int) (1000000.0 * t_closed_total);
133 ed->count = (int) vlib_worker_threads[0].barrier_sync_count;
135 /* Reset context for next trace */
136 vlib_worker_threads[0].barrier_context = NULL;
140 os_get_nthreads (void)
142 return vec_len (vlib_thread_stacks);
146 vlib_set_thread_name (char *name)
148 int pthread_setname_np (pthread_t __target_thread, const char *__name);
150 pthread_t thread = pthread_self ();
154 rv = pthread_setname_np (thread, name);
156 clib_warning ("pthread_setname_np returned %d", rv);
161 sort_registrations_by_no_clone (void *a0, void *a1)
163 vlib_thread_registration_t **tr0 = a0;
164 vlib_thread_registration_t **tr1 = a1;
166 return ((i32) ((*tr0)->no_data_structure_clone)
167 - ((i32) ((*tr1)->no_data_structure_clone)));
171 /* Called early in the init sequence */
174 vlib_thread_init (vlib_main_t * vm)
176 vlib_thread_main_t *tm = &vlib_thread_main;
177 vlib_worker_thread_t *w;
178 vlib_thread_registration_t *tr;
179 u32 n_vlib_mains = 1;
183 u32 stats_num_worker_threads_dir_index;
185 stats_num_worker_threads_dir_index =
186 vlib_stats_add_gauge ("/sys/num_worker_threads");
187 ASSERT (stats_num_worker_threads_dir_index != ~0);
189 /* get bitmaps of active cpu cores and sockets */
190 tm->cpu_core_bitmap = os_get_online_cpu_core_bitmap ();
191 tm->cpu_socket_bitmap = os_get_online_cpu_node_bitmap ();
193 avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
196 for (i = 0; i < tm->skip_cores; i++)
198 uword c = clib_bitmap_first_set (avail_cpu);
200 return clib_error_return (0, "no available cpus to skip");
202 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
205 /* grab cpu for main thread */
206 if (tm->main_lcore != ~0)
208 if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
209 return clib_error_return (0, "cpu %u is not available to be used"
210 " for the main thread", tm->main_lcore);
211 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
214 /* assume that there is socket 0 only if there is no data from sysfs */
215 if (!tm->cpu_socket_bitmap)
216 tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
218 /* pin main thread to main_lcore */
219 if (tm->main_lcore != ~0)
223 CPU_SET (tm->main_lcore, &cpuset);
224 if (pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t),
227 return clib_error_return (0, "could not pin main thread to cpu %u",
232 /* Set up thread 0 */
233 vec_validate_aligned (vlib_worker_threads, 0, CLIB_CACHE_LINE_BYTES);
234 vec_set_len (vlib_worker_threads, 1);
235 w = vlib_worker_threads;
236 w->thread_mheap = clib_mem_get_heap ();
237 w->thread_stack = vlib_thread_stacks[0];
238 w->cpu_id = tm->main_lcore;
239 w->lwp = syscall (SYS_gettid);
240 w->thread_id = pthread_self ();
241 tm->n_vlib_mains = 1;
243 vlib_get_thread_core_numa (w, w->cpu_id);
245 if (tm->sched_policy != ~0)
247 struct sched_param sched_param;
248 if (!sched_getparam (w->lwp, &sched_param))
250 if (tm->sched_priority != ~0)
251 sched_param.sched_priority = tm->sched_priority;
252 sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
256 /* assign threads to cores and set n_vlib_mains */
261 vec_add1 (tm->registrations, tr);
265 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
267 for (i = 0; i < vec_len (tm->registrations); i++)
270 tr = tm->registrations[i];
271 tr->first_index = first_index;
272 first_index += tr->count;
273 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
275 /* construct coremask */
276 if (tr->use_pthreads || !tr->count)
282 clib_bitmap_foreach (c, tr->coremask) {
283 if (clib_bitmap_get(avail_cpu, c) == 0)
284 return clib_error_return (0, "cpu %u is not available to be used"
285 " for the '%s' thread",c, tr->name);
287 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
292 for (j = 0; j < tr->count; j++)
294 /* Do not use CPU 0 by default - leave it to the host and IRQs */
295 uword avail_c0 = clib_bitmap_get (avail_cpu, 0);
296 avail_cpu = clib_bitmap_set (avail_cpu, 0, 0);
298 uword c = clib_bitmap_first_set (avail_cpu);
299 /* Use CPU 0 as a last resort */
300 if (c == ~0 && avail_c0)
307 return clib_error_return (0,
308 "no available cpus to be used for"
309 " the '%s' thread #%u",
310 tr->name, tr->count);
312 avail_cpu = clib_bitmap_set (avail_cpu, 0, avail_c0);
313 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
314 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
319 clib_bitmap_free (avail_cpu);
321 tm->n_vlib_mains = n_vlib_mains;
322 vlib_stats_set_gauge (stats_num_worker_threads_dir_index, n_vlib_mains - 1);
325 * Allocate the remaining worker threads, and thread stack vector slots
326 * from now on, calls to os_get_nthreads() will return the correct
329 vec_validate_aligned (vlib_worker_threads, first_index - 1,
330 CLIB_CACHE_LINE_BYTES);
331 vec_validate (vlib_thread_stacks, vec_len (vlib_worker_threads) - 1);
336 vlib_frame_queue_alloc (int nelts)
338 vlib_frame_queue_t *fq;
340 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
341 clib_memset (fq, 0, sizeof (*fq));
343 fq->vector_threshold = 2 * VLIB_FRAME_SIZE;
344 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
346 if (nelts & (nelts - 1))
348 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
355 void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
357 vl_msg_api_handler_no_free (void *v)
361 /* To be called by vlib worker threads upon startup */
363 vlib_worker_thread_init (vlib_worker_thread_t * w)
365 vlib_thread_main_t *tm = vlib_get_thread_main ();
368 * Note: disabling signals in worker threads as follows
369 * prevents the api post-mortem dump scheme from working
373 * pthread_sigmask (SIG_SETMASK, &s, 0);
377 clib_mem_set_heap (w->thread_mheap);
379 if (vec_len (tm->thread_prefix) && w->registration->short_name)
381 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
382 w->registration->short_name, w->instance_id, '\0');
383 vlib_set_thread_name ((char *) w->name);
386 if (!w->registration->use_pthreads)
389 /* Initial barrier sync, for both worker and i/o threads */
390 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, 1);
392 while (*vlib_worker_threads->wait_at_barrier)
395 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, -1);
400 vlib_worker_thread_bootstrap_fn (void *arg)
402 vlib_worker_thread_t *w = arg;
404 w->lwp = syscall (SYS_gettid);
405 w->thread_id = pthread_self ();
407 __os_thread_index = w - vlib_worker_threads;
411 void *frame_addr = __builtin_frame_address (0);
412 if (frame_addr < (void *) w->thread_stack ||
413 frame_addr > (void *) w->thread_stack + VLIB_THREAD_STACK_SIZE)
415 /* heap is not set yet */
416 fprintf (stderr, "thread stack is not set properly\n");
421 w->thread_function (arg);
427 vlib_get_thread_core_numa (vlib_worker_thread_t * w, unsigned cpu_id)
429 clib_bitmap_t *nbmp = 0, *cbmp = 0;
430 int node, core_id = -1, numa_id = -1;
432 core_id = os_get_cpu_phys_core_id (cpu_id);
433 nbmp = os_get_online_cpu_node_bitmap ();
435 clib_bitmap_foreach (node, nbmp) {
436 cbmp = os_get_cpu_on_node_bitmap (node);
437 if (clib_bitmap_get (cbmp, cpu_id))
439 vec_reset_length (cbmp);
445 w->core_id = core_id;
446 w->numa_id = numa_id;
449 static clib_error_t *
450 vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned cpu_id)
452 clib_mem_main_t *mm = &clib_mem_main;
453 vlib_thread_main_t *tm = &vlib_thread_main;
457 void *(*fp_arg) (void *) = fp;
461 vlib_get_thread_core_numa (w, cpu_id);
463 /* Set up NUMA-bound heap if indicated */
464 if (mm->per_numa_mheaps[w->numa_id] == 0)
466 /* If the user requested a NUMA heap, create it... */
467 if (tm->numa_heap_size)
469 clib_mem_set_numa_affinity (w->numa_id, 1 /* force */ );
470 numa_heap = clib_mem_create_heap (0 /* DIY */ , tm->numa_heap_size,
472 "numa %u heap", w->numa_id);
473 clib_mem_set_default_numa_affinity ();
474 mm->per_numa_mheaps[w->numa_id] = numa_heap;
478 /* Or, use the main heap */
479 mm->per_numa_mheaps[w->numa_id] = w->thread_mheap;
484 CPU_SET (cpu_id, &cpuset);
486 if (pthread_attr_init (&attr))
487 return clib_error_return_unix (0, "pthread_attr_init");
489 if (pthread_attr_setstack (&attr, w->thread_stack,
490 VLIB_THREAD_STACK_SIZE))
491 return clib_error_return_unix (0, "pthread_attr_setstack");
493 if (pthread_create (&worker, &attr, fp_arg, (void *) w))
494 return clib_error_return_unix (0, "pthread_create");
496 if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
497 return clib_error_return_unix (0, "pthread_setaffinity_np");
499 if (pthread_attr_destroy (&attr))
500 return clib_error_return_unix (0, "pthread_attr_destroy");
505 static clib_error_t *
506 start_workers (vlib_main_t * vm)
508 vlib_global_main_t *vgm = vlib_get_global_main ();
509 vlib_main_t *fvm = vlib_get_first_main ();
511 vlib_worker_thread_t *w;
512 vlib_main_t *vm_clone;
514 vlib_thread_main_t *tm = &vlib_thread_main;
515 vlib_thread_registration_t *tr;
516 vlib_node_runtime_t *rt;
517 u32 n_vlib_mains = tm->n_vlib_mains;
518 u32 worker_thread_index;
519 u32 stats_err_entry_index = fvm->error_main.stats_err_entry_index;
520 clib_mem_heap_t *main_heap = clib_mem_get_per_cpu_heap ();
521 vlib_stats_register_mem_heap (main_heap);
523 vec_reset_length (vlib_worker_threads);
525 /* Set up the main thread */
526 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
527 w->elog_track.name = "main thread";
528 elog_track_register (vlib_get_elog_main (), &w->elog_track);
530 if (vec_len (tm->thread_prefix))
532 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
533 vlib_set_thread_name ((char *) w->name);
536 vgm->elog_main.lock =
537 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
538 vgm->elog_main.lock[0] = 0;
540 clib_callback_data_init (&vm->vlib_node_runtime_perf_callbacks,
541 &vm->worker_thread_main_loop_callback_lock);
543 vec_validate_aligned (vgm->vlib_mains, n_vlib_mains - 1,
544 CLIB_CACHE_LINE_BYTES);
545 vec_set_len (vgm->vlib_mains, 0);
546 vec_add1_aligned (vgm->vlib_mains, vm, CLIB_CACHE_LINE_BYTES);
548 if (n_vlib_mains > 1)
550 vlib_worker_threads->wait_at_barrier =
551 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
552 vlib_worker_threads->workers_at_barrier =
553 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
555 vlib_worker_threads->node_reforks_required =
556 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
558 /* We'll need the rpc vector lock... */
559 clib_spinlock_init (&vm->pending_rpc_lock);
561 /* Ask for an initial barrier sync */
562 *vlib_worker_threads->workers_at_barrier = 0;
563 *vlib_worker_threads->wait_at_barrier = 1;
565 /* Without update or refork */
566 *vlib_worker_threads->node_reforks_required = 0;
567 vgm->need_vlib_worker_thread_node_runtime_update = 0;
570 vm->barrier_epoch = 0;
571 vm->barrier_no_close_before = 0;
573 worker_thread_index = 1;
574 clib_spinlock_init (&vm->worker_thread_main_loop_callback_lock);
576 for (i = 0; i < vec_len (tm->registrations); i++)
578 vlib_node_main_t *nm, *nm_clone;
581 tr = tm->registrations[i];
586 for (k = 0; k < tr->count; k++)
591 vec_add2 (vlib_worker_threads, w, 1);
592 /* Currently unused, may not really work */
594 w->thread_mheap = clib_mem_create_heap (0, tr->mheap_size,
599 w->thread_mheap = main_heap;
602 vlib_thread_stack_init (w - vlib_worker_threads);
603 w->thread_function = tr->function;
604 w->thread_function_arg = w;
606 w->registration = tr;
609 (char *) format (0, "%s %d", tr->name, k + 1);
610 vec_add1 (w->elog_track.name, 0);
611 elog_track_register (vlib_get_elog_main (), &w->elog_track);
613 if (tr->no_data_structure_clone)
616 /* Fork vlib_global_main et al. Look for bugs here */
617 oldheap = clib_mem_set_heap (w->thread_mheap);
619 vm_clone = clib_mem_alloc_aligned (sizeof (*vm_clone),
620 CLIB_CACHE_LINE_BYTES);
621 clib_memcpy (vm_clone, vlib_get_first_main (),
624 vm_clone->thread_index = worker_thread_index;
625 vm_clone->pending_rpc_requests = 0;
626 vec_validate (vm_clone->pending_rpc_requests, 0);
627 vec_set_len (vm_clone->pending_rpc_requests, 0);
628 clib_memset (&vm_clone->random_buffer, 0,
629 sizeof (vm_clone->random_buffer));
631 (&vm_clone->worker_thread_main_loop_callback_lock);
632 clib_callback_data_init
633 (&vm_clone->vlib_node_runtime_perf_callbacks,
634 &vm_clone->worker_thread_main_loop_callback_lock);
636 nm = &vlib_get_first_main ()->node_main;
637 nm_clone = &vm_clone->node_main;
638 /* fork next frames array, preserving node runtime indices */
639 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
640 CLIB_CACHE_LINE_BYTES);
641 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
643 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
644 u32 save_node_runtime_index;
647 save_node_runtime_index = nf->node_runtime_index;
648 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
649 vlib_next_frame_init (nf);
650 nf->node_runtime_index = save_node_runtime_index;
651 nf->flags = save_flags;
654 /* fork the frame dispatch queue */
655 nm_clone->pending_frames = 0;
656 vec_validate (nm_clone->pending_frames, 10);
657 vec_set_len (nm_clone->pending_frames, 0);
662 /* Allocate all nodes in single block for speed */
663 n = clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*n));
665 for (j = 0; j < vec_len (nm->nodes); j++)
667 clib_memcpy (n, nm->nodes[j], sizeof (*n));
668 /* none of the copied nodes have enqueue rights given out */
669 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
670 clib_memset (&n->stats_total, 0, sizeof (n->stats_total));
671 clib_memset (&n->stats_last_clear, 0,
672 sizeof (n->stats_last_clear));
673 vec_add1 (nm_clone->nodes, n);
676 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
677 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
678 CLIB_CACHE_LINE_BYTES);
680 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
682 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
683 /* copy initial runtime_data from node */
684 if (n->runtime_data && n->runtime_data_bytes > 0)
685 clib_memcpy (rt->runtime_data, n->runtime_data,
686 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
687 n->runtime_data_bytes));
690 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
691 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
692 CLIB_CACHE_LINE_BYTES);
693 clib_interrupt_init (
694 &nm_clone->input_node_interrupts,
695 vec_len (nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT]));
696 clib_interrupt_init (
697 &nm_clone->pre_input_node_interrupts,
698 vec_len (nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT]));
699 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
701 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
702 /* copy initial runtime_data from node */
703 if (n->runtime_data && n->runtime_data_bytes > 0)
704 clib_memcpy (rt->runtime_data, n->runtime_data,
705 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
706 n->runtime_data_bytes));
709 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT] =
710 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT],
711 CLIB_CACHE_LINE_BYTES);
713 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT])
715 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
716 /* copy initial runtime_data from node */
717 if (n->runtime_data && n->runtime_data_bytes > 0)
718 clib_memcpy (rt->runtime_data, n->runtime_data,
719 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
720 n->runtime_data_bytes));
723 nm_clone->processes = vec_dup_aligned (nm->processes,
724 CLIB_CACHE_LINE_BYTES);
726 /* Create per-thread frame freelist */
727 nm_clone->frame_sizes = 0;
728 nm_clone->node_by_error = nm->node_by_error;
730 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
732 clib_mem_set_heap (oldheap);
733 vec_add1_aligned (vgm->vlib_mains, vm_clone,
734 CLIB_CACHE_LINE_BYTES);
736 /* Switch to the stats segment ... */
737 vlib_stats_validate (stats_err_entry_index, worker_thread_index,
738 vec_len (fvm->error_main.counters) - 1);
739 c = vlib_stats_get_entry_data_pointer (stats_err_entry_index);
740 vm_clone->error_main.counters = c[worker_thread_index];
742 vm_clone->error_main.counters_last_clear = vec_dup_aligned (
743 vlib_get_first_main ()->error_main.counters_last_clear,
744 CLIB_CACHE_LINE_BYTES);
746 worker_thread_index++;
752 /* only have non-data-structure copy threads to create... */
753 for (i = 0; i < vec_len (tm->registrations); i++)
755 tr = tm->registrations[i];
757 for (j = 0; j < tr->count; j++)
759 vec_add2 (vlib_worker_threads, w, 1);
762 w->thread_mheap = clib_mem_create_heap (0, tr->mheap_size,
768 w->thread_mheap = main_heap;
770 vlib_thread_stack_init (w - vlib_worker_threads);
771 w->thread_function = tr->function;
772 w->thread_function_arg = w;
775 (char *) format (0, "%s %d", tr->name, j + 1);
776 w->registration = tr;
777 vec_add1 (w->elog_track.name, 0);
778 elog_track_register (vlib_get_elog_main (), &w->elog_track);
783 worker_thread_index = 1;
785 for (i = 0; i < vec_len (tm->registrations); i++)
790 tr = tm->registrations[i];
792 if (tr->use_pthreads || tm->use_pthreads)
794 for (j = 0; j < tr->count; j++)
797 w = vlib_worker_threads + worker_thread_index++;
798 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
801 clib_unix_error ("%U, thread %s init on cpu %d failed",
802 format_clib_error, err, tr->name, 0);
808 clib_bitmap_foreach (c, tr->coremask) {
809 w = vlib_worker_threads + worker_thread_index++;
810 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
813 clib_unix_error ("%U, thread %s init on cpu %d failed",
814 format_clib_error, err, tr->name, c);
818 vlib_worker_thread_barrier_sync (vm);
821 err = vlib_call_init_exit_functions (
822 vm, &vgm->num_workers_change_function_registrations, 1 /* call_once */,
825 clib_error_report (err);
827 vlib_worker_thread_barrier_release (vm);
831 VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
835 worker_thread_node_runtime_update_internal (void)
839 vlib_node_main_t *nm, *nm_clone;
840 vlib_main_t *vm_clone;
841 vlib_node_runtime_t *rt;
843 ASSERT (vlib_get_thread_index () == 0);
845 vm = vlib_get_first_main ();
848 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
851 * Scrape all runtime stats, so we don't lose node runtime(s) with
852 * pending counts, or throw away worker / io thread counts.
854 for (j = 0; j < vec_len (nm->nodes); j++)
858 vlib_node_sync_stats (vm, n);
861 for (i = 1; i < vlib_get_n_threads (); i++)
865 vm_clone = vlib_get_main_by_index (i);
866 nm_clone = &vm_clone->node_main;
868 for (j = 0; j < vec_len (nm_clone->nodes); j++)
870 n = nm_clone->nodes[j];
872 rt = vlib_node_get_runtime (vm_clone, n->index);
873 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
877 /* Per-worker clone rebuilds are now done on each thread */
882 vlib_worker_thread_node_refork (void)
884 vlib_main_t *vm, *vm_clone;
885 vlib_node_main_t *nm, *nm_clone;
886 vlib_node_t **old_nodes_clone;
887 vlib_node_runtime_t *rt, *old_rt;
890 vlib_node_t *new_n_clone;
894 vm = vlib_get_first_main ();
896 vm_clone = vlib_get_main ();
897 nm_clone = &vm_clone->node_main;
899 /* Re-clone error heap */
900 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
902 clib_memcpy_fast (&vm_clone->error_main, &vm->error_main,
903 sizeof (vm->error_main));
904 j = vec_len (vm->error_main.counters) - 1;
906 c = vlib_stats_get_entry_data_pointer (vm->error_main.stats_err_entry_index);
907 vm_clone->error_main.counters = c[vm_clone->thread_index];
909 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
910 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
912 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
914 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
915 if ((nf->flags & VLIB_FRAME_IS_ALLOCATED) && nf->frame != NULL)
917 vlib_frame_t *f = nf->frame;
919 vlib_frame_free (vm_clone, f);
923 vec_free (nm_clone->next_frames);
924 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
925 CLIB_CACHE_LINE_BYTES);
927 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
929 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
930 u32 save_node_runtime_index;
933 save_node_runtime_index = nf->node_runtime_index;
934 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
935 vlib_next_frame_init (nf);
936 nf->node_runtime_index = save_node_runtime_index;
937 nf->flags = save_flags;
940 old_nodes_clone = nm_clone->nodes;
945 /* Allocate all nodes in single block for speed */
947 clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*new_n_clone));
948 for (j = 0; j < vec_len (nm->nodes); j++)
950 vlib_node_t *new_n = nm->nodes[j];
952 clib_memcpy_fast (new_n_clone, new_n, sizeof (*new_n));
953 /* none of the copied nodes have enqueue rights given out */
954 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
956 if (j >= vec_len (old_nodes_clone))
958 /* new node, set to zero */
959 clib_memset (&new_n_clone->stats_total, 0,
960 sizeof (new_n_clone->stats_total));
961 clib_memset (&new_n_clone->stats_last_clear, 0,
962 sizeof (new_n_clone->stats_last_clear));
966 vlib_node_t *old_n_clone = old_nodes_clone[j];
967 /* Copy stats if the old data is valid */
968 clib_memcpy_fast (&new_n_clone->stats_total,
969 &old_n_clone->stats_total,
970 sizeof (new_n_clone->stats_total));
971 clib_memcpy_fast (&new_n_clone->stats_last_clear,
972 &old_n_clone->stats_last_clear,
973 sizeof (new_n_clone->stats_last_clear));
975 /* keep previous node state */
976 new_n_clone->state = old_n_clone->state;
977 new_n_clone->flags = old_n_clone->flags;
979 vec_add1 (nm_clone->nodes, new_n_clone);
982 /* Free the old node clones */
983 clib_mem_free (old_nodes_clone[0]);
985 vec_free (old_nodes_clone);
988 /* re-clone internal nodes */
989 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL];
990 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
991 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
992 CLIB_CACHE_LINE_BYTES);
994 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
996 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
997 /* copy runtime_data, will be overwritten later for existing rt */
998 if (n->runtime_data && n->runtime_data_bytes > 0)
999 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1000 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1001 n->runtime_data_bytes));
1004 for (j = 0; j < vec_len (old_rt); j++)
1006 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1007 rt->state = old_rt[j].state;
1008 rt->flags = old_rt[j].flags;
1009 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1010 VLIB_NODE_RUNTIME_DATA_SIZE);
1015 /* re-clone input nodes */
1016 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
1017 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
1018 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
1019 CLIB_CACHE_LINE_BYTES);
1020 clib_interrupt_resize (
1021 &nm_clone->input_node_interrupts,
1022 vec_len (nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT]));
1023 clib_interrupt_resize (
1024 &nm_clone->pre_input_node_interrupts,
1025 vec_len (nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT]));
1027 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
1029 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1030 /* copy runtime_data, will be overwritten later for existing rt */
1031 if (n->runtime_data && n->runtime_data_bytes > 0)
1032 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1033 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1034 n->runtime_data_bytes));
1037 for (j = 0; j < vec_len (old_rt); j++)
1039 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1040 rt->state = old_rt[j].state;
1041 rt->flags = old_rt[j].flags;
1042 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1043 VLIB_NODE_RUNTIME_DATA_SIZE);
1048 /* re-clone pre-input nodes */
1049 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT];
1050 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT] =
1051 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT],
1052 CLIB_CACHE_LINE_BYTES);
1054 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT])
1056 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1057 /* copy runtime_data, will be overwritten later for existing rt */
1058 if (n->runtime_data && n->runtime_data_bytes > 0)
1059 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1060 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1061 n->runtime_data_bytes));
1064 for (j = 0; j < vec_len (old_rt); j++)
1066 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1067 rt->state = old_rt[j].state;
1068 rt->flags = old_rt[j].flags;
1069 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1070 VLIB_NODE_RUNTIME_DATA_SIZE);
1075 vec_free (nm_clone->processes);
1076 nm_clone->processes = vec_dup_aligned (nm->processes,
1077 CLIB_CACHE_LINE_BYTES);
1078 nm_clone->node_by_error = nm->node_by_error;
1082 vlib_worker_thread_node_runtime_update (void)
1085 * Make a note that we need to do a node runtime update
1086 * prior to releasing the barrier.
1088 vlib_global_main.need_vlib_worker_thread_node_runtime_update = 1;
1092 unformat_sched_policy (unformat_input_t * input, va_list * args)
1094 u32 *r = va_arg (*args, u32 *);
1097 #define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
1098 foreach_sched_policy
1105 static clib_error_t *
1106 cpu_config (vlib_main_t * vm, unformat_input_t * input)
1108 vlib_thread_registration_t *tr;
1110 vlib_thread_main_t *tm = &vlib_thread_main;
1115 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
1117 tm->n_thread_stacks = 1; /* account for main thread */
1118 tm->sched_policy = ~0;
1119 tm->sched_priority = ~0;
1120 tm->main_lcore = ~0;
1126 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
1130 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1132 if (unformat (input, "use-pthreads"))
1133 tm->use_pthreads = 1;
1134 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
1136 else if (unformat (input, "main-core %u", &tm->main_lcore))
1138 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
1140 else if (unformat (input, "numa-heap-size %U",
1141 unformat_memory_size, &tm->numa_heap_size))
1143 else if (unformat (input, "coremask-%s %U", &name,
1144 unformat_bitmap_mask, &bitmap) ||
1145 unformat (input, "corelist-%s %U", &name,
1146 unformat_bitmap_list, &bitmap))
1148 p = hash_get_mem (tm->thread_registrations_by_name, name);
1150 return clib_error_return (0, "no such thread type '%s'", name);
1152 tr = (vlib_thread_registration_t *) p[0];
1154 if (tr->use_pthreads)
1155 return clib_error_return (0,
1156 "corelist cannot be set for '%s' threads",
1159 return clib_error_return
1160 (0, "core placement of '%s' threads is already configured",
1163 tr->coremask = bitmap;
1164 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1168 (input, "scheduler-policy %U", unformat_sched_policy,
1171 else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
1173 else if (unformat (input, "%s %u", &name, &count))
1175 p = hash_get_mem (tm->thread_registrations_by_name, name);
1177 return clib_error_return (0, "no such thread type 3 '%s'", name);
1179 tr = (vlib_thread_registration_t *) p[0];
1181 if (tr->fixed_count)
1182 return clib_error_return
1183 (0, "number of '%s' threads not configurable", name);
1185 return clib_error_return
1186 (0, "number of '%s' threads is already configured", name);
1194 if (tm->sched_priority != ~0)
1196 if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
1198 u32 prio_max = sched_get_priority_max (tm->sched_policy);
1199 u32 prio_min = sched_get_priority_min (tm->sched_policy);
1200 if (tm->sched_priority > prio_max)
1201 tm->sched_priority = prio_max;
1202 if (tm->sched_priority < prio_min)
1203 tm->sched_priority = prio_min;
1207 return clib_error_return
1209 "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1210 tm->sched_priority);
1215 if (!tm->thread_prefix)
1216 tm->thread_prefix = format (0, "vpp");
1220 tm->n_thread_stacks += tr->count;
1221 tm->n_pthreads += tr->count * tr->use_pthreads;
1222 tm->n_threads += tr->count * (tr->use_pthreads == 0);
1229 VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1232 * Enforce minimum open time to minimize packet loss due to Rx overflow,
1233 * based on a test based heuristic that barrier should be open for at least
1234 * 3 time as long as it is closed (with an upper bound of 1ms because by that
1235 * point it is probably too late to make a difference)
1238 #ifndef BARRIER_MINIMUM_OPEN_LIMIT
1239 #define BARRIER_MINIMUM_OPEN_LIMIT 0.001
1242 #ifndef BARRIER_MINIMUM_OPEN_FACTOR
1243 #define BARRIER_MINIMUM_OPEN_FACTOR 3
1247 vlib_worker_thread_initial_barrier_sync_and_release (vlib_main_t * vm)
1250 f64 now = vlib_time_now (vm);
1251 u32 count = vlib_get_n_threads () - 1;
1253 /* No worker threads? */
1257 deadline = now + BARRIER_SYNC_TIMEOUT;
1258 *vlib_worker_threads->wait_at_barrier = 1;
1259 while (*vlib_worker_threads->workers_at_barrier != count)
1261 if ((now = vlib_time_now (vm)) > deadline)
1263 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1268 *vlib_worker_threads->wait_at_barrier = 0;
1272 * Return true if the wroker thread barrier is held
1275 vlib_worker_thread_barrier_held (void)
1277 if (vlib_get_n_threads () < 2)
1280 return (*vlib_worker_threads->wait_at_barrier == 1);
1284 vlib_worker_thread_barrier_sync_int (vlib_main_t * vm, const char *func_name)
1291 f64 max_vector_rate;
1295 if (vlib_get_n_threads () < 2)
1298 ASSERT (vlib_get_thread_index () == 0);
1300 vlib_worker_threads[0].barrier_caller = func_name;
1301 count = vlib_get_n_threads () - 1;
1303 /* Record entry relative to last close */
1304 now = vlib_time_now (vm);
1305 t_entry = now - vm->barrier_epoch;
1307 /* Tolerate recursive calls */
1308 if (++vlib_worker_threads[0].recursion_level > 1)
1310 barrier_trace_sync_rec (t_entry);
1314 if (PREDICT_FALSE (vec_len (vm->barrier_perf_callbacks) != 0))
1315 clib_call_callbacks (vm->barrier_perf_callbacks, vm,
1316 vm->clib_time.last_cpu_time, 0 /* enter */ );
1319 * Need data to decide if we're working hard enough to honor
1320 * the barrier hold-down timer.
1322 max_vector_rate = 0.0;
1323 for (i = 1; i < vlib_get_n_threads (); i++)
1325 vlib_main_t *ovm = vlib_get_main_by_index (i);
1326 max_vector_rate = clib_max (max_vector_rate,
1327 (f64) vlib_last_vectors_per_main_loop (ovm));
1330 vlib_worker_threads[0].barrier_sync_count++;
1332 /* Enforce minimum barrier open time to minimize packet loss */
1333 ASSERT (vm->barrier_no_close_before <= (now + BARRIER_MINIMUM_OPEN_LIMIT));
1336 * If any worker thread seems busy, which we define
1337 * as a vector rate above 10, we enforce the barrier hold-down timer
1339 if (max_vector_rate > 10.0)
1343 now = vlib_time_now (vm);
1344 /* Barrier hold-down timer expired? */
1345 if (now >= vm->barrier_no_close_before)
1347 if ((vm->barrier_no_close_before - now)
1348 > (2.0 * BARRIER_MINIMUM_OPEN_LIMIT))
1351 ("clock change: would have waited for %.4f seconds",
1352 (vm->barrier_no_close_before - now));
1357 /* Record time of closure */
1358 t_open = now - vm->barrier_epoch;
1359 vm->barrier_epoch = now;
1361 deadline = now + BARRIER_SYNC_TIMEOUT;
1363 *vlib_worker_threads->wait_at_barrier = 1;
1364 while (*vlib_worker_threads->workers_at_barrier != count)
1366 if ((now = vlib_time_now (vm)) > deadline)
1368 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1373 t_closed = now - vm->barrier_epoch;
1375 barrier_trace_sync (t_entry, t_open, t_closed);
1380 vlib_worker_thread_barrier_release (vlib_main_t * vm)
1382 vlib_global_main_t *vgm = vlib_get_global_main ();
1388 f64 t_update_main = 0.0;
1389 int refork_needed = 0;
1391 if (vlib_get_n_threads () < 2)
1394 ASSERT (vlib_get_thread_index () == 0);
1397 now = vlib_time_now (vm);
1398 t_entry = now - vm->barrier_epoch;
1400 if (--vlib_worker_threads[0].recursion_level > 0)
1402 barrier_trace_release_rec (t_entry);
1406 /* Update (all) node runtimes before releasing the barrier, if needed */
1407 if (vgm->need_vlib_worker_thread_node_runtime_update)
1410 * Lock stat segment here, so we's safe when
1411 * rebuilding the stat segment node clones from the
1414 vlib_stats_segment_lock ();
1416 /* Do stats elements on main thread */
1417 worker_thread_node_runtime_update_internal ();
1418 vgm->need_vlib_worker_thread_node_runtime_update = 0;
1420 /* Do per thread rebuilds in parallel */
1422 clib_atomic_fetch_add (vlib_worker_threads->node_reforks_required,
1423 (vlib_get_n_threads () - 1));
1424 now = vlib_time_now (vm);
1425 t_update_main = now - vm->barrier_epoch;
1428 deadline = now + BARRIER_SYNC_TIMEOUT;
1431 * Note when we let go of the barrier.
1432 * Workers can use this to derive a reasonably accurate
1433 * time offset. See vlib_time_now(...)
1435 vm->time_last_barrier_release = vlib_time_now (vm);
1436 CLIB_MEMORY_STORE_BARRIER ();
1438 *vlib_worker_threads->wait_at_barrier = 0;
1440 while (*vlib_worker_threads->workers_at_barrier > 0)
1442 if ((now = vlib_time_now (vm)) > deadline)
1444 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1449 /* Wait for reforks before continuing */
1452 now = vlib_time_now (vm);
1454 deadline = now + BARRIER_SYNC_TIMEOUT;
1456 while (*vlib_worker_threads->node_reforks_required > 0)
1458 if ((now = vlib_time_now (vm)) > deadline)
1460 fformat (stderr, "%s: worker thread refork deadlock\n",
1465 vlib_stats_segment_unlock ();
1468 t_closed_total = now - vm->barrier_epoch;
1470 minimum_open = t_closed_total * BARRIER_MINIMUM_OPEN_FACTOR;
1472 if (minimum_open > BARRIER_MINIMUM_OPEN_LIMIT)
1474 minimum_open = BARRIER_MINIMUM_OPEN_LIMIT;
1477 vm->barrier_no_close_before = now + minimum_open;
1479 /* Record barrier epoch (used to enforce minimum open time) */
1480 vm->barrier_epoch = now;
1482 barrier_trace_release (t_entry, t_closed_total, t_update_main);
1484 if (PREDICT_FALSE (vec_len (vm->barrier_perf_callbacks) != 0))
1485 clib_call_callbacks (vm->barrier_perf_callbacks, vm,
1486 vm->clib_time.last_cpu_time, 1 /* leave */ );
1490 vlib_worker_sync_rpc (void *args)
1492 ASSERT (vlib_thread_is_main_w_barrier ());
1493 vlib_worker_threads->wait_before_barrier = 0;
1497 vlib_workers_sync (void)
1499 if (PREDICT_FALSE (!vlib_num_workers ()))
1502 if (!(*vlib_worker_threads->wait_at_barrier) &&
1503 !clib_atomic_swap_rel_n (&vlib_worker_threads->wait_before_barrier, 1))
1505 u32 thread_index = vlib_get_thread_index ();
1506 vlib_rpc_call_main_thread (vlib_worker_sync_rpc, (u8 *) &thread_index,
1507 sizeof (thread_index));
1508 vlib_worker_flush_pending_rpc_requests (vlib_get_main ());
1511 /* Wait until main thread asks for barrier */
1512 while (!(*vlib_worker_threads->wait_at_barrier))
1515 /* Stop before barrier and make sure all threads are either
1516 * at worker barrier or the barrier before it */
1517 clib_atomic_fetch_add (&vlib_worker_threads->workers_before_barrier, 1);
1518 while (vlib_num_workers () > (*vlib_worker_threads->workers_at_barrier +
1519 vlib_worker_threads->workers_before_barrier))
1524 vlib_workers_continue (void)
1526 if (PREDICT_FALSE (!vlib_num_workers ()))
1529 clib_atomic_fetch_add (&vlib_worker_threads->done_work_before_barrier, 1);
1531 /* Wait until all workers are done with work before barrier */
1532 while (vlib_worker_threads->done_work_before_barrier <
1533 vlib_worker_threads->workers_before_barrier)
1536 clib_atomic_fetch_add (&vlib_worker_threads->done_work_before_barrier, -1);
1537 clib_atomic_fetch_add (&vlib_worker_threads->workers_before_barrier, -1);
1541 * Wait until each of the workers has been once around the track
1544 vlib_worker_wait_one_loop (void)
1546 vlib_global_main_t *vgm = vlib_get_global_main ();
1547 ASSERT (vlib_get_thread_index () == 0);
1549 if (vlib_get_n_threads () < 2)
1552 if (vlib_worker_thread_barrier_held ())
1558 vec_validate (counts, vlib_get_n_threads () - 1);
1560 /* record the current loop counts */
1561 vec_foreach_index (ii, vgm->vlib_mains)
1562 counts[ii] = vgm->vlib_mains[ii]->main_loop_count;
1564 /* spin until each changes, apart from the main thread, or we'd be
1566 for (ii = 1; ii < vec_len (counts); ii++)
1568 while (counts[ii] == vgm->vlib_mains[ii]->main_loop_count)
1577 vlib_worker_flush_pending_rpc_requests (vlib_main_t *vm)
1579 vlib_main_t *vm_global = vlib_get_first_main ();
1581 ASSERT (vm != vm_global);
1583 clib_spinlock_lock_if_init (&vm_global->pending_rpc_lock);
1584 vec_append (vm_global->pending_rpc_requests, vm->pending_rpc_requests);
1585 vec_reset_length (vm->pending_rpc_requests);
1586 clib_spinlock_unlock_if_init (&vm_global->pending_rpc_lock);
1590 vlib_worker_thread_fn (void *arg)
1592 vlib_global_main_t *vgm = vlib_get_global_main ();
1593 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
1594 vlib_main_t *vm = vlib_get_main ();
1597 ASSERT (vm->thread_index == vlib_get_thread_index ());
1599 vlib_worker_thread_init (w);
1600 clib_time_init (&vm->clib_time);
1601 clib_mem_set_heap (w->thread_mheap);
1603 vm->worker_init_functions_called = hash_create (0, 0);
1605 e = vlib_call_init_exit_functions_no_sort (
1606 vm, &vgm->worker_init_function_registrations, 1 /* call_once */,
1609 clib_error_report (e);
1611 vlib_worker_loop (vm);
1614 VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1617 .function = vlib_worker_thread_fn,
1620 extern clib_march_fn_registration
1621 *vlib_frame_queue_dequeue_with_aux_fn_march_fn_registrations;
1622 extern clib_march_fn_registration
1623 *vlib_frame_queue_dequeue_fn_march_fn_registrations;
1625 vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1627 vlib_thread_main_t *tm = vlib_get_thread_main ();
1628 vlib_main_t *vm = vlib_get_main ();
1629 vlib_frame_queue_main_t *fqm;
1630 vlib_frame_queue_t *fq;
1635 if (frame_queue_nelts == 0)
1636 frame_queue_nelts = FRAME_QUEUE_MAX_NELTS;
1638 num_threads = 1 /* main thread */ + tm->n_threads;
1639 ASSERT (frame_queue_nelts >= 8 + num_threads);
1641 vec_add2 (tm->frame_queue_mains, fqm, 1);
1643 node = vlib_get_node (vm, fqm->node_index);
1645 if (node->aux_offset)
1647 fqm->frame_queue_dequeue_fn =
1648 CLIB_MARCH_FN_VOID_POINTER (vlib_frame_queue_dequeue_with_aux_fn);
1652 fqm->frame_queue_dequeue_fn =
1653 CLIB_MARCH_FN_VOID_POINTER (vlib_frame_queue_dequeue_fn);
1656 fqm->node_index = node_index;
1657 fqm->frame_queue_nelts = frame_queue_nelts;
1659 vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1);
1660 vec_set_len (fqm->vlib_frame_queues, 0);
1661 for (i = 0; i < tm->n_vlib_mains; i++)
1663 fq = vlib_frame_queue_alloc (frame_queue_nelts);
1664 vec_add1 (fqm->vlib_frame_queues, fq);
1667 return (fqm - tm->frame_queue_mains);
1671 vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t *
1674 ASSERT (vlib_get_thread_index () == 0);
1675 vlib_process_signal_event (vlib_get_main (), args->node_index,
1676 args->type_opaque, args->data);
1679 void *rpc_call_main_thread_cb_fn;
1682 vlib_rpc_call_main_thread (void *callback, u8 * args, u32 arg_size)
1684 if (rpc_call_main_thread_cb_fn)
1686 void (*fp) (void *, u8 *, u32) = rpc_call_main_thread_cb_fn;
1687 (*fp) (callback, args, arg_size);
1690 clib_warning ("BUG: rpc_call_main_thread_cb_fn NULL!");
1694 threads_init (vlib_main_t * vm)
1696 const vlib_thread_main_t *tm = vlib_get_thread_main ();
1698 if (tm->main_lcore == ~0 && tm->n_vlib_mains > 1)
1699 return clib_error_return (0, "Configuration error, a main core must "
1700 "be specified when using worker threads");
1705 VLIB_INIT_FUNCTION (threads_init);
1707 static clib_error_t *
1708 show_clock_command_fn (vlib_main_t * vm,
1709 unformat_input_t * input, vlib_cli_command_t * cmd)
1712 clib_timebase_t _tb, *tb = &_tb;
1714 (void) unformat (input, "verbose %=", &verbose, 1);
1716 clib_timebase_init (tb, 0 /* GMT */ , CLIB_TIMEBASE_DAYLIGHT_NONE,
1719 vlib_cli_output (vm, "%U, %U GMT", format_clib_time, &vm->clib_time,
1720 verbose, format_clib_timebase_time,
1721 clib_timebase_now (tb));
1723 vlib_cli_output (vm, "Time last barrier release %.9f",
1724 vm->time_last_barrier_release);
1726 foreach_vlib_main ()
1728 vlib_cli_output (vm, "%d: %U", this_vlib_main->thread_index,
1729 format_clib_time, &this_vlib_main->clib_time, verbose);
1731 vlib_cli_output (vm, "Thread %d offset %.9f error %.9f",
1732 this_vlib_main->thread_index,
1733 this_vlib_main->time_offset,
1734 vm->time_last_barrier_release -
1735 this_vlib_main->time_last_barrier_release);
1740 VLIB_CLI_COMMAND (f_command, static) =
1742 .path = "show clock",
1743 .short_help = "show clock",
1744 .function = show_clock_command_fn,
1747 vlib_thread_main_t *
1748 vlib_get_thread_main_not_inline (void)
1750 return vlib_get_thread_main ();
1754 * fd.io coding-style-patch-verification: ON
1757 * eval: (c-set-style "gnu")