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/linux/sysfs.h>
23 #include <vlib/vlib.h>
25 #include <vlib/threads.h>
27 #include <vlib/stat_weak_inlines.h>
35 vlib_worker_thread_t *vlib_worker_threads;
36 vlib_thread_main_t vlib_thread_main;
39 * Barrier tracing can be enabled on a normal build to collect information
40 * on barrier use, including timings and call stacks. Deliberately not
41 * keyed off CLIB_DEBUG, because that can add significant overhead which
42 * imapacts observed timings.
46 barrier_trace_sync (f64 t_entry, f64 t_open, f64 t_closed)
48 if (!vlib_worker_threads->barrier_elog_enabled)
51 ELOG_TYPE_DECLARE (e) = {
52 .format = "bar-trace-%s-#%d",
53 .format_args = "T4i4",
58 u32 caller, count, t_entry, t_open, t_closed;
61 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
62 ed->count = (int) vlib_worker_threads[0].barrier_sync_count;
63 ed->caller = elog_string (&vlib_global_main.elog_main,
64 (char *) vlib_worker_threads[0].barrier_caller);
65 ed->t_entry = (int) (1000000.0 * t_entry);
66 ed->t_open = (int) (1000000.0 * t_open);
67 ed->t_closed = (int) (1000000.0 * t_closed);
71 barrier_trace_sync_rec (f64 t_entry)
73 if (!vlib_worker_threads->barrier_elog_enabled)
76 ELOG_TYPE_DECLARE (e) = {
77 .format = "bar-syncrec-%s-#%d",
78 .format_args = "T4i4",
86 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
87 ed->depth = (int) vlib_worker_threads[0].recursion_level - 1;
88 ed->caller = elog_string (&vlib_global_main.elog_main,
89 (char *) vlib_worker_threads[0].barrier_caller);
93 barrier_trace_release_rec (f64 t_entry)
95 if (!vlib_worker_threads->barrier_elog_enabled)
98 ELOG_TYPE_DECLARE (e) = {
99 .format = "bar-relrrec-#%d",
108 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
109 ed->depth = (int) vlib_worker_threads[0].recursion_level;
113 barrier_trace_release (f64 t_entry, f64 t_closed_total, f64 t_update_main)
115 if (!vlib_worker_threads->barrier_elog_enabled)
118 ELOG_TYPE_DECLARE (e) = {
119 .format = "bar-rel-#%d-e%d-u%d-t%d",
120 .format_args = "i4i4i4i4",
125 u32 count, t_entry, t_update_main, t_closed_total;
128 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
129 ed->t_entry = (int) (1000000.0 * t_entry);
130 ed->t_update_main = (int) (1000000.0 * t_update_main);
131 ed->t_closed_total = (int) (1000000.0 * t_closed_total);
132 ed->count = (int) vlib_worker_threads[0].barrier_sync_count;
134 /* Reset context for next trace */
135 vlib_worker_threads[0].barrier_context = NULL;
139 os_get_nthreads (void)
141 return vec_len (vlib_thread_stacks);
145 vlib_set_thread_name (char *name)
147 int pthread_setname_np (pthread_t __target_thread, const char *__name);
149 pthread_t thread = pthread_self ();
153 rv = pthread_setname_np (thread, name);
155 clib_warning ("pthread_setname_np returned %d", rv);
160 sort_registrations_by_no_clone (void *a0, void *a1)
162 vlib_thread_registration_t **tr0 = a0;
163 vlib_thread_registration_t **tr1 = a1;
165 return ((i32) ((*tr0)->no_data_structure_clone)
166 - ((i32) ((*tr1)->no_data_structure_clone)));
170 /* Called early in the init sequence */
173 vlib_thread_init (vlib_main_t * vm)
175 vlib_thread_main_t *tm = &vlib_thread_main;
176 vlib_worker_thread_t *w;
177 vlib_thread_registration_t *tr;
178 u32 n_vlib_mains = 1;
183 /* get bitmaps of active cpu cores and sockets */
184 tm->cpu_core_bitmap =
185 clib_sysfs_list_to_bitmap ("/sys/devices/system/cpu/online");
186 tm->cpu_socket_bitmap =
187 clib_sysfs_list_to_bitmap ("/sys/devices/system/node/online");
189 avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
192 for (i = 0; i < tm->skip_cores; i++)
194 uword c = clib_bitmap_first_set (avail_cpu);
196 return clib_error_return (0, "no available cpus to skip");
198 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
201 /* grab cpu for main thread */
202 if (tm->main_lcore == ~0)
204 /* if main-lcore is not set, we try to use lcore 1 */
205 if (clib_bitmap_get (avail_cpu, 1))
208 tm->main_lcore = clib_bitmap_first_set (avail_cpu);
209 if (tm->main_lcore == (u8) ~ 0)
210 return clib_error_return (0, "no available cpus to be used for the"
215 if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
216 return clib_error_return (0, "cpu %u is not available to be used"
217 " for the main thread", tm->main_lcore);
219 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
221 /* assume that there is socket 0 only if there is no data from sysfs */
222 if (!tm->cpu_socket_bitmap)
223 tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
225 /* pin main thread to main_lcore */
226 if (tm->cb.vlib_thread_set_lcore_cb)
228 tm->cb.vlib_thread_set_lcore_cb (0, tm->main_lcore);
234 CPU_SET (tm->main_lcore, &cpuset);
235 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
238 /* Set up thread 0 */
239 vec_validate_aligned (vlib_worker_threads, 0, CLIB_CACHE_LINE_BYTES);
240 _vec_len (vlib_worker_threads) = 1;
241 w = vlib_worker_threads;
242 w->thread_mheap = clib_mem_get_heap ();
243 w->thread_stack = vlib_thread_stacks[0];
244 w->cpu_id = tm->main_lcore;
245 w->lwp = syscall (SYS_gettid);
246 w->thread_id = pthread_self ();
247 tm->n_vlib_mains = 1;
249 vlib_get_thread_core_numa (w, w->cpu_id);
251 if (tm->sched_policy != ~0)
253 struct sched_param sched_param;
254 if (!sched_getparam (w->lwp, &sched_param))
256 if (tm->sched_priority != ~0)
257 sched_param.sched_priority = tm->sched_priority;
258 sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
262 /* assign threads to cores and set n_vlib_mains */
267 vec_add1 (tm->registrations, tr);
271 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
273 for (i = 0; i < vec_len (tm->registrations); i++)
276 tr = tm->registrations[i];
277 tr->first_index = first_index;
278 first_index += tr->count;
279 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
281 /* construct coremask */
282 if (tr->use_pthreads || !tr->count)
289 clib_bitmap_foreach (c, tr->coremask) {
290 if (clib_bitmap_get(avail_cpu, c) == 0)
291 return clib_error_return (0, "cpu %u is not available to be used"
292 " for the '%s' thread",c, tr->name);
294 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
300 for (j = 0; j < tr->count; j++)
302 /* Do not use CPU 0 by default - leave it to the host and IRQs */
303 uword avail_c0 = clib_bitmap_get (avail_cpu, 0);
304 avail_cpu = clib_bitmap_set (avail_cpu, 0, 0);
306 uword c = clib_bitmap_first_set (avail_cpu);
307 /* Use CPU 0 as a last resort */
308 if (c == ~0 && avail_c0)
315 return clib_error_return (0,
316 "no available cpus to be used for"
317 " the '%s' thread", tr->name);
319 avail_cpu = clib_bitmap_set (avail_cpu, 0, avail_c0);
320 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
321 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
326 clib_bitmap_free (avail_cpu);
328 tm->n_vlib_mains = n_vlib_mains;
331 * Allocate the remaining worker threads, and thread stack vector slots
332 * from now on, calls to os_get_nthreads() will return the correct
335 vec_validate_aligned (vlib_worker_threads, first_index - 1,
336 CLIB_CACHE_LINE_BYTES);
337 vec_validate (vlib_thread_stacks, vec_len (vlib_worker_threads) - 1);
342 vlib_frame_queue_alloc (int nelts)
344 vlib_frame_queue_t *fq;
346 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
347 clib_memset (fq, 0, sizeof (*fq));
349 fq->vector_threshold = 128; // packets
350 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
354 if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
355 fformat (stderr, "WARNING: fq->tail unaligned\n");
356 if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
357 fformat (stderr, "WARNING: fq->head unaligned\n");
358 if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
359 fformat (stderr, "WARNING: fq->elts unaligned\n");
361 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
362 fformat (stderr, "WARNING: fq->elts[0] size %d\n",
363 sizeof (fq->elts[0]));
364 if (nelts & (nelts - 1))
366 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
374 void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
376 vl_msg_api_handler_no_free (void *v)
380 /* To be called by vlib worker threads upon startup */
382 vlib_worker_thread_init (vlib_worker_thread_t * w)
384 vlib_thread_main_t *tm = vlib_get_thread_main ();
387 * Note: disabling signals in worker threads as follows
388 * prevents the api post-mortem dump scheme from working
392 * pthread_sigmask (SIG_SETMASK, &s, 0);
396 clib_mem_set_heap (w->thread_mheap);
398 if (vec_len (tm->thread_prefix) && w->registration->short_name)
400 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
401 w->registration->short_name, w->instance_id, '\0');
402 vlib_set_thread_name ((char *) w->name);
405 if (!w->registration->use_pthreads)
408 /* Initial barrier sync, for both worker and i/o threads */
409 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, 1);
411 while (*vlib_worker_threads->wait_at_barrier)
414 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, -1);
419 vlib_worker_thread_bootstrap_fn (void *arg)
422 vlib_worker_thread_t *w = arg;
424 w->lwp = syscall (SYS_gettid);
425 w->thread_id = pthread_self ();
427 __os_thread_index = w - vlib_worker_threads;
429 vlib_process_start_switch_stack (vlib_get_main_by_index (__os_thread_index),
431 rv = (void *) clib_calljmp
432 ((uword (*)(uword)) w->thread_function,
433 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
434 /* NOTREACHED, we hope */
439 vlib_get_thread_core_numa (vlib_worker_thread_t * w, unsigned cpu_id)
441 const char *sys_cpu_path = "/sys/devices/system/cpu/cpu";
442 const char *sys_node_path = "/sys/devices/system/node/node";
443 clib_bitmap_t *nbmp = 0, *cbmp = 0;
446 int core_id = -1, numa_id = -1;
448 p = format (p, "%s%u/topology/core_id%c", sys_cpu_path, cpu_id, 0);
449 clib_sysfs_read ((char *) p, "%d", &core_id);
450 vec_reset_length (p);
453 clib_sysfs_read ("/sys/devices/system/node/online", "%U",
454 unformat_bitmap_list, &nbmp);
455 clib_bitmap_foreach (node, nbmp) {
456 p = format (p, "%s%u/cpulist%c", sys_node_path, node, 0);
457 clib_sysfs_read ((char *) p, "%U", unformat_bitmap_list, &cbmp);
458 if (clib_bitmap_get (cbmp, cpu_id))
460 vec_reset_length (cbmp);
461 vec_reset_length (p);
468 w->core_id = core_id;
469 w->numa_id = numa_id;
472 static clib_error_t *
473 vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned cpu_id)
475 clib_mem_main_t *mm = &clib_mem_main;
476 vlib_thread_main_t *tm = &vlib_thread_main;
477 void *(*fp_arg) (void *) = fp;
481 vlib_get_thread_core_numa (w, cpu_id);
483 /* Set up NUMA-bound heap if indicated */
484 if (mm->per_numa_mheaps[w->numa_id] == 0)
486 /* If the user requested a NUMA heap, create it... */
487 if (tm->numa_heap_size)
489 clib_mem_set_numa_affinity (w->numa_id, 1 /* force */ );
490 numa_heap = clib_mem_create_heap (0 /* DIY */ , tm->numa_heap_size,
492 "numa %u heap", w->numa_id);
493 clib_mem_set_default_numa_affinity ();
494 mm->per_numa_mheaps[w->numa_id] = numa_heap;
498 /* Or, use the main heap */
499 mm->per_numa_mheaps[w->numa_id] = w->thread_mheap;
503 if (tm->cb.vlib_launch_thread_cb && !w->registration->use_pthreads)
504 return tm->cb.vlib_launch_thread_cb (fp, (void *) w, cpu_id);
510 CPU_SET (cpu_id, &cpuset);
512 if (pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w))
513 return clib_error_return_unix (0, "pthread_create");
515 if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
516 return clib_error_return_unix (0, "pthread_setaffinity_np");
522 static clib_error_t *
523 start_workers (vlib_main_t * vm)
525 vlib_global_main_t *vgm = vlib_get_global_main ();
527 vlib_worker_thread_t *w;
528 vlib_main_t *vm_clone;
530 vlib_thread_main_t *tm = &vlib_thread_main;
531 vlib_thread_registration_t *tr;
532 vlib_node_runtime_t *rt;
533 u32 n_vlib_mains = tm->n_vlib_mains;
534 u32 worker_thread_index;
535 clib_mem_heap_t *main_heap = clib_mem_get_per_cpu_heap ();
536 vlib_stats_register_mem_heap (main_heap);
538 vec_reset_length (vlib_worker_threads);
540 /* Set up the main thread */
541 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
542 w->elog_track.name = "main thread";
543 elog_track_register (vlib_get_elog_main (), &w->elog_track);
545 if (vec_len (tm->thread_prefix))
547 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
548 vlib_set_thread_name ((char *) w->name);
551 vgm->elog_main.lock =
552 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
553 vgm->elog_main.lock[0] = 0;
555 clib_callback_data_init (&vm->vlib_node_runtime_perf_callbacks,
556 &vm->worker_thread_main_loop_callback_lock);
558 vec_validate_aligned (vgm->vlib_mains, n_vlib_mains - 1,
559 CLIB_CACHE_LINE_BYTES);
560 _vec_len (vgm->vlib_mains) = 0;
561 vec_add1_aligned (vgm->vlib_mains, vm, CLIB_CACHE_LINE_BYTES);
563 if (n_vlib_mains > 1)
565 vlib_worker_threads->wait_at_barrier =
566 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
567 vlib_worker_threads->workers_at_barrier =
568 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
570 vlib_worker_threads->node_reforks_required =
571 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
573 /* We'll need the rpc vector lock... */
574 clib_spinlock_init (&vm->pending_rpc_lock);
576 /* Ask for an initial barrier sync */
577 *vlib_worker_threads->workers_at_barrier = 0;
578 *vlib_worker_threads->wait_at_barrier = 1;
580 /* Without update or refork */
581 *vlib_worker_threads->node_reforks_required = 0;
582 vgm->need_vlib_worker_thread_node_runtime_update = 0;
585 vm->barrier_epoch = 0;
586 vm->barrier_no_close_before = 0;
588 worker_thread_index = 1;
589 clib_spinlock_init (&vm->worker_thread_main_loop_callback_lock);
591 for (i = 0; i < vec_len (tm->registrations); i++)
593 vlib_node_main_t *nm, *nm_clone;
596 tr = tm->registrations[i];
601 for (k = 0; k < tr->count; k++)
605 vec_add2 (vlib_worker_threads, w, 1);
606 /* Currently unused, may not really work */
608 w->thread_mheap = clib_mem_create_heap (0, tr->mheap_size,
613 w->thread_mheap = main_heap;
616 vlib_thread_stack_init (w - vlib_worker_threads);
617 w->thread_function = tr->function;
618 w->thread_function_arg = w;
620 w->registration = tr;
623 (char *) format (0, "%s %d", tr->name, k + 1);
624 vec_add1 (w->elog_track.name, 0);
625 elog_track_register (vlib_get_elog_main (), &w->elog_track);
627 if (tr->no_data_structure_clone)
630 /* Fork vlib_global_main et al. Look for bugs here */
631 oldheap = clib_mem_set_heap (w->thread_mheap);
633 vm_clone = clib_mem_alloc_aligned (sizeof (*vm_clone),
634 CLIB_CACHE_LINE_BYTES);
635 clib_memcpy (vm_clone, vlib_get_first_main (),
638 vm_clone->thread_index = worker_thread_index;
639 vm_clone->heap_base = w->thread_mheap;
640 vm_clone->heap_aligned_base = (void *)
641 (((uword) w->thread_mheap) & ~(VLIB_FRAME_ALIGN - 1));
642 vm_clone->pending_rpc_requests = 0;
643 vec_validate (vm_clone->pending_rpc_requests, 0);
644 _vec_len (vm_clone->pending_rpc_requests) = 0;
645 clib_memset (&vm_clone->random_buffer, 0,
646 sizeof (vm_clone->random_buffer));
648 (&vm_clone->worker_thread_main_loop_callback_lock);
649 clib_callback_data_init
650 (&vm_clone->vlib_node_runtime_perf_callbacks,
651 &vm_clone->worker_thread_main_loop_callback_lock);
653 nm = &vlib_get_first_main ()->node_main;
654 nm_clone = &vm_clone->node_main;
655 /* fork next frames array, preserving node runtime indices */
656 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
657 CLIB_CACHE_LINE_BYTES);
658 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
660 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
661 u32 save_node_runtime_index;
664 save_node_runtime_index = nf->node_runtime_index;
665 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
666 vlib_next_frame_init (nf);
667 nf->node_runtime_index = save_node_runtime_index;
668 nf->flags = save_flags;
671 /* fork the frame dispatch queue */
672 nm_clone->pending_frames = 0;
673 vec_validate (nm_clone->pending_frames, 10);
674 _vec_len (nm_clone->pending_frames) = 0;
679 /* Allocate all nodes in single block for speed */
680 n = clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*n));
682 for (j = 0; j < vec_len (nm->nodes); j++)
684 clib_memcpy (n, nm->nodes[j], sizeof (*n));
685 /* none of the copied nodes have enqueue rights given out */
686 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
687 clib_memset (&n->stats_total, 0, sizeof (n->stats_total));
688 clib_memset (&n->stats_last_clear, 0,
689 sizeof (n->stats_last_clear));
690 vec_add1 (nm_clone->nodes, n);
693 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
694 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
695 CLIB_CACHE_LINE_BYTES);
697 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
699 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
700 rt->thread_index = vm_clone->thread_index;
701 /* copy initial runtime_data from node */
702 if (n->runtime_data && n->runtime_data_bytes > 0)
703 clib_memcpy (rt->runtime_data, n->runtime_data,
704 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
705 n->runtime_data_bytes));
708 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
709 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
710 CLIB_CACHE_LINE_BYTES);
711 clib_interrupt_init (
712 &nm_clone->interrupts,
713 vec_len (nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT]));
714 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
716 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
717 rt->thread_index = vm_clone->thread_index;
718 /* copy initial runtime_data from node */
719 if (n->runtime_data && n->runtime_data_bytes > 0)
720 clib_memcpy (rt->runtime_data, n->runtime_data,
721 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
722 n->runtime_data_bytes));
725 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT] =
726 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT],
727 CLIB_CACHE_LINE_BYTES);
729 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT])
731 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
732 rt->thread_index = vm_clone->thread_index;
733 /* copy initial runtime_data from node */
734 if (n->runtime_data && n->runtime_data_bytes > 0)
735 clib_memcpy (rt->runtime_data, n->runtime_data,
736 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
737 n->runtime_data_bytes));
740 nm_clone->processes = vec_dup_aligned (nm->processes,
741 CLIB_CACHE_LINE_BYTES);
743 /* Create per-thread frame freelist */
744 nm_clone->frame_sizes = vec_new (vlib_frame_size_t, 1);
745 #ifdef VLIB_SUPPORTS_ARBITRARY_SCALAR_SIZES
746 nm_clone->frame_size_hash = hash_create (0, sizeof (uword));
748 nm_clone->node_by_error = nm->node_by_error;
750 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
752 clib_mem_set_heap (oldheap);
753 vec_add1_aligned (vgm->vlib_mains, vm_clone,
754 CLIB_CACHE_LINE_BYTES);
756 /* Switch to the stats segment ... */
757 void *oldheap = vlib_stats_push_heap (0);
758 vm_clone->error_main.counters =
759 vec_dup_aligned (vlib_get_first_main ()->error_main.counters,
760 CLIB_CACHE_LINE_BYTES);
761 vlib_stats_pop_heap2 (vm_clone->error_main.counters,
762 worker_thread_index, oldheap, 1);
764 vm_clone->error_main.counters_last_clear = vec_dup_aligned (
765 vlib_get_first_main ()->error_main.counters_last_clear,
766 CLIB_CACHE_LINE_BYTES);
768 worker_thread_index++;
774 /* only have non-data-structure copy threads to create... */
775 for (i = 0; i < vec_len (tm->registrations); i++)
777 tr = tm->registrations[i];
779 for (j = 0; j < tr->count; j++)
781 vec_add2 (vlib_worker_threads, w, 1);
784 w->thread_mheap = clib_mem_create_heap (0, tr->mheap_size,
790 w->thread_mheap = main_heap;
792 vlib_thread_stack_init (w - vlib_worker_threads);
793 w->thread_function = tr->function;
794 w->thread_function_arg = w;
797 (char *) format (0, "%s %d", tr->name, j + 1);
798 w->registration = tr;
799 vec_add1 (w->elog_track.name, 0);
800 elog_track_register (vlib_get_elog_main (), &w->elog_track);
805 worker_thread_index = 1;
807 for (i = 0; i < vec_len (tm->registrations); i++)
812 tr = tm->registrations[i];
814 if (tr->use_pthreads || tm->use_pthreads)
816 for (j = 0; j < tr->count; j++)
818 w = vlib_worker_threads + worker_thread_index++;
819 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
822 clib_error_report (err);
829 clib_bitmap_foreach (c, tr->coremask) {
830 w = vlib_worker_threads + worker_thread_index++;
831 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
834 clib_error_report (err);
839 vlib_worker_thread_barrier_sync (vm);
840 vlib_worker_thread_barrier_release (vm);
844 VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
848 worker_thread_node_runtime_update_internal (void)
852 vlib_node_main_t *nm, *nm_clone;
853 vlib_main_t *vm_clone;
854 vlib_node_runtime_t *rt;
856 ASSERT (vlib_get_thread_index () == 0);
858 vm = vlib_get_first_main ();
861 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
864 * Scrape all runtime stats, so we don't lose node runtime(s) with
865 * pending counts, or throw away worker / io thread counts.
867 for (j = 0; j < vec_len (nm->nodes); j++)
871 vlib_node_sync_stats (vm, n);
874 for (i = 1; i < vlib_get_n_threads (); i++)
878 vm_clone = vlib_get_main_by_index (i);
879 nm_clone = &vm_clone->node_main;
881 for (j = 0; j < vec_len (nm_clone->nodes); j++)
883 n = nm_clone->nodes[j];
885 rt = vlib_node_get_runtime (vm_clone, n->index);
886 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
890 /* Per-worker clone rebuilds are now done on each thread */
895 vlib_worker_thread_node_refork (void)
897 vlib_main_t *vm, *vm_clone;
898 vlib_node_main_t *nm, *nm_clone;
899 vlib_node_t **old_nodes_clone;
900 vlib_node_runtime_t *rt, *old_rt;
902 vlib_node_t *new_n_clone;
906 vm = vlib_get_first_main ();
908 vm_clone = vlib_get_main ();
909 nm_clone = &vm_clone->node_main;
911 /* Re-clone error heap */
912 u64 *old_counters = vm_clone->error_main.counters;
913 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
915 clib_memcpy_fast (&vm_clone->error_main, &vm->error_main,
916 sizeof (vm->error_main));
917 j = vec_len (vm->error_main.counters) - 1;
919 /* Switch to the stats segment ... */
920 void *oldheap = vlib_stats_push_heap (0);
921 vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES);
922 vm_clone->error_main.counters = old_counters;
923 vlib_stats_pop_heap2 (vm_clone->error_main.counters, vm_clone->thread_index,
926 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
927 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
929 nm_clone = &vm_clone->node_main;
930 vec_free (nm_clone->next_frames);
931 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
932 CLIB_CACHE_LINE_BYTES);
934 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
936 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
937 u32 save_node_runtime_index;
940 save_node_runtime_index = nf->node_runtime_index;
941 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
942 vlib_next_frame_init (nf);
943 nf->node_runtime_index = save_node_runtime_index;
944 nf->flags = save_flags;
947 old_nodes_clone = nm_clone->nodes;
952 /* Allocate all nodes in single block for speed */
954 clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*new_n_clone));
955 for (j = 0; j < vec_len (nm->nodes); j++)
957 vlib_node_t *new_n = nm->nodes[j];
959 clib_memcpy_fast (new_n_clone, new_n, sizeof (*new_n));
960 /* none of the copied nodes have enqueue rights given out */
961 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
963 if (j >= vec_len (old_nodes_clone))
965 /* new node, set to zero */
966 clib_memset (&new_n_clone->stats_total, 0,
967 sizeof (new_n_clone->stats_total));
968 clib_memset (&new_n_clone->stats_last_clear, 0,
969 sizeof (new_n_clone->stats_last_clear));
973 vlib_node_t *old_n_clone = old_nodes_clone[j];
974 /* Copy stats if the old data is valid */
975 clib_memcpy_fast (&new_n_clone->stats_total,
976 &old_n_clone->stats_total,
977 sizeof (new_n_clone->stats_total));
978 clib_memcpy_fast (&new_n_clone->stats_last_clear,
979 &old_n_clone->stats_last_clear,
980 sizeof (new_n_clone->stats_last_clear));
982 /* keep previous node state */
983 new_n_clone->state = old_n_clone->state;
984 new_n_clone->flags = old_n_clone->flags;
986 vec_add1 (nm_clone->nodes, new_n_clone);
989 /* Free the old node clones */
990 clib_mem_free (old_nodes_clone[0]);
992 vec_free (old_nodes_clone);
995 /* re-clone internal nodes */
996 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL];
997 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
998 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
999 CLIB_CACHE_LINE_BYTES);
1001 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
1003 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1004 rt->thread_index = vm_clone->thread_index;
1005 /* copy runtime_data, will be overwritten later for existing rt */
1006 if (n->runtime_data && n->runtime_data_bytes > 0)
1007 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1008 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1009 n->runtime_data_bytes));
1012 for (j = 0; j < vec_len (old_rt); j++)
1014 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1015 rt->state = old_rt[j].state;
1016 rt->flags = old_rt[j].flags;
1017 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1018 VLIB_NODE_RUNTIME_DATA_SIZE);
1023 /* re-clone input nodes */
1024 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
1025 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
1026 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
1027 CLIB_CACHE_LINE_BYTES);
1028 clib_interrupt_resize (
1029 &nm_clone->interrupts,
1030 vec_len (nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT]));
1032 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
1034 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1035 rt->thread_index = vm_clone->thread_index;
1036 /* copy runtime_data, will be overwritten later for existing rt */
1037 if (n->runtime_data && n->runtime_data_bytes > 0)
1038 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1039 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1040 n->runtime_data_bytes));
1043 for (j = 0; j < vec_len (old_rt); j++)
1045 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1046 rt->state = old_rt[j].state;
1047 rt->flags = old_rt[j].flags;
1048 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1049 VLIB_NODE_RUNTIME_DATA_SIZE);
1054 /* re-clone pre-input nodes */
1055 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT];
1056 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT] =
1057 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT],
1058 CLIB_CACHE_LINE_BYTES);
1060 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT])
1062 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1063 rt->thread_index = vm_clone->thread_index;
1064 /* copy runtime_data, will be overwritten later for existing rt */
1065 if (n->runtime_data && n->runtime_data_bytes > 0)
1066 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1067 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1068 n->runtime_data_bytes));
1071 for (j = 0; j < vec_len (old_rt); j++)
1073 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1074 rt->state = old_rt[j].state;
1075 rt->flags = old_rt[j].flags;
1076 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1077 VLIB_NODE_RUNTIME_DATA_SIZE);
1082 nm_clone->processes = vec_dup_aligned (nm->processes,
1083 CLIB_CACHE_LINE_BYTES);
1084 nm_clone->node_by_error = nm->node_by_error;
1088 vlib_worker_thread_node_runtime_update (void)
1091 * Make a note that we need to do a node runtime update
1092 * prior to releasing the barrier.
1094 vlib_global_main.need_vlib_worker_thread_node_runtime_update = 1;
1098 unformat_sched_policy (unformat_input_t * input, va_list * args)
1100 u32 *r = va_arg (*args, u32 *);
1103 #define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
1104 foreach_sched_policy
1111 static clib_error_t *
1112 cpu_config (vlib_main_t * vm, unformat_input_t * input)
1114 vlib_thread_registration_t *tr;
1116 vlib_thread_main_t *tm = &vlib_thread_main;
1121 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
1123 tm->n_thread_stacks = 1; /* account for main thread */
1124 tm->sched_policy = ~0;
1125 tm->sched_priority = ~0;
1126 tm->main_lcore = ~0;
1132 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
1136 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1138 if (unformat (input, "use-pthreads"))
1139 tm->use_pthreads = 1;
1140 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
1142 else if (unformat (input, "main-core %u", &tm->main_lcore))
1144 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
1146 else if (unformat (input, "numa-heap-size %U",
1147 unformat_memory_size, &tm->numa_heap_size))
1149 else if (unformat (input, "coremask-%s %U", &name,
1150 unformat_bitmap_mask, &bitmap) ||
1151 unformat (input, "corelist-%s %U", &name,
1152 unformat_bitmap_list, &bitmap))
1154 p = hash_get_mem (tm->thread_registrations_by_name, name);
1156 return clib_error_return (0, "no such thread type '%s'", name);
1158 tr = (vlib_thread_registration_t *) p[0];
1160 if (tr->use_pthreads)
1161 return clib_error_return (0,
1162 "corelist cannot be set for '%s' threads",
1165 return clib_error_return
1166 (0, "core placement of '%s' threads is already configured",
1169 tr->coremask = bitmap;
1170 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1174 (input, "scheduler-policy %U", unformat_sched_policy,
1177 else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
1179 else if (unformat (input, "%s %u", &name, &count))
1181 p = hash_get_mem (tm->thread_registrations_by_name, name);
1183 return clib_error_return (0, "no such thread type 3 '%s'", name);
1185 tr = (vlib_thread_registration_t *) p[0];
1187 if (tr->fixed_count)
1188 return clib_error_return
1189 (0, "number of '%s' threads not configurable", name);
1191 return clib_error_return
1192 (0, "number of '%s' threads is already configured", name);
1200 if (tm->sched_priority != ~0)
1202 if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
1204 u32 prio_max = sched_get_priority_max (tm->sched_policy);
1205 u32 prio_min = sched_get_priority_min (tm->sched_policy);
1206 if (tm->sched_priority > prio_max)
1207 tm->sched_priority = prio_max;
1208 if (tm->sched_priority < prio_min)
1209 tm->sched_priority = prio_min;
1213 return clib_error_return
1215 "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1216 tm->sched_priority);
1221 if (!tm->thread_prefix)
1222 tm->thread_prefix = format (0, "vpp");
1226 tm->n_thread_stacks += tr->count;
1227 tm->n_pthreads += tr->count * tr->use_pthreads;
1228 tm->n_threads += tr->count * (tr->use_pthreads == 0);
1235 VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1238 * Enforce minimum open time to minimize packet loss due to Rx overflow,
1239 * based on a test based heuristic that barrier should be open for at least
1240 * 3 time as long as it is closed (with an upper bound of 1ms because by that
1241 * point it is probably too late to make a difference)
1244 #ifndef BARRIER_MINIMUM_OPEN_LIMIT
1245 #define BARRIER_MINIMUM_OPEN_LIMIT 0.001
1248 #ifndef BARRIER_MINIMUM_OPEN_FACTOR
1249 #define BARRIER_MINIMUM_OPEN_FACTOR 3
1253 vlib_worker_thread_initial_barrier_sync_and_release (vlib_main_t * vm)
1256 f64 now = vlib_time_now (vm);
1257 u32 count = vlib_get_n_threads () - 1;
1259 /* No worker threads? */
1263 deadline = now + BARRIER_SYNC_TIMEOUT;
1264 *vlib_worker_threads->wait_at_barrier = 1;
1265 while (*vlib_worker_threads->workers_at_barrier != count)
1267 if ((now = vlib_time_now (vm)) > deadline)
1269 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1274 *vlib_worker_threads->wait_at_barrier = 0;
1278 * Return true if the wroker thread barrier is held
1281 vlib_worker_thread_barrier_held (void)
1283 if (vlib_get_n_threads () < 2)
1286 return (*vlib_worker_threads->wait_at_barrier == 1);
1290 vlib_worker_thread_barrier_sync_int (vlib_main_t * vm, const char *func_name)
1297 f64 max_vector_rate;
1301 if (vlib_get_n_threads () < 2)
1304 ASSERT (vlib_get_thread_index () == 0);
1306 vlib_worker_threads[0].barrier_caller = func_name;
1307 count = vlib_get_n_threads () - 1;
1309 /* Record entry relative to last close */
1310 now = vlib_time_now (vm);
1311 t_entry = now - vm->barrier_epoch;
1313 /* Tolerate recursive calls */
1314 if (++vlib_worker_threads[0].recursion_level > 1)
1316 barrier_trace_sync_rec (t_entry);
1320 if (PREDICT_FALSE (vec_len (vm->barrier_perf_callbacks) != 0))
1321 clib_call_callbacks (vm->barrier_perf_callbacks, vm,
1322 vm->clib_time.last_cpu_time, 0 /* enter */ );
1325 * Need data to decide if we're working hard enough to honor
1326 * the barrier hold-down timer.
1328 max_vector_rate = 0.0;
1329 for (i = 1; i < vlib_get_n_threads (); i++)
1331 vlib_main_t *ovm = vlib_get_main_by_index (i);
1332 max_vector_rate = clib_max (max_vector_rate,
1333 (f64) vlib_last_vectors_per_main_loop (ovm));
1336 vlib_worker_threads[0].barrier_sync_count++;
1338 /* Enforce minimum barrier open time to minimize packet loss */
1339 ASSERT (vm->barrier_no_close_before <= (now + BARRIER_MINIMUM_OPEN_LIMIT));
1342 * If any worker thread seems busy, which we define
1343 * as a vector rate above 10, we enforce the barrier hold-down timer
1345 if (max_vector_rate > 10.0)
1349 now = vlib_time_now (vm);
1350 /* Barrier hold-down timer expired? */
1351 if (now >= vm->barrier_no_close_before)
1353 if ((vm->barrier_no_close_before - now)
1354 > (2.0 * BARRIER_MINIMUM_OPEN_LIMIT))
1357 ("clock change: would have waited for %.4f seconds",
1358 (vm->barrier_no_close_before - now));
1363 /* Record time of closure */
1364 t_open = now - vm->barrier_epoch;
1365 vm->barrier_epoch = now;
1367 deadline = now + BARRIER_SYNC_TIMEOUT;
1369 *vlib_worker_threads->wait_at_barrier = 1;
1370 while (*vlib_worker_threads->workers_at_barrier != count)
1372 if ((now = vlib_time_now (vm)) > deadline)
1374 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1379 t_closed = now - vm->barrier_epoch;
1381 barrier_trace_sync (t_entry, t_open, t_closed);
1386 vlib_worker_thread_barrier_release (vlib_main_t * vm)
1388 vlib_global_main_t *vgm = vlib_get_global_main ();
1394 f64 t_update_main = 0.0;
1395 int refork_needed = 0;
1397 if (vlib_get_n_threads () < 2)
1400 ASSERT (vlib_get_thread_index () == 0);
1403 now = vlib_time_now (vm);
1404 t_entry = now - vm->barrier_epoch;
1406 if (--vlib_worker_threads[0].recursion_level > 0)
1408 barrier_trace_release_rec (t_entry);
1412 /* Update (all) node runtimes before releasing the barrier, if needed */
1413 if (vgm->need_vlib_worker_thread_node_runtime_update)
1416 * Lock stat segment here, so we's safe when
1417 * rebuilding the stat segment node clones from the
1420 vlib_stat_segment_lock ();
1422 /* Do stats elements on main thread */
1423 worker_thread_node_runtime_update_internal ();
1424 vgm->need_vlib_worker_thread_node_runtime_update = 0;
1426 /* Do per thread rebuilds in parallel */
1428 clib_atomic_fetch_add (vlib_worker_threads->node_reforks_required,
1429 (vlib_get_n_threads () - 1));
1430 now = vlib_time_now (vm);
1431 t_update_main = now - vm->barrier_epoch;
1434 deadline = now + BARRIER_SYNC_TIMEOUT;
1437 * Note when we let go of the barrier.
1438 * Workers can use this to derive a reasonably accurate
1439 * time offset. See vlib_time_now(...)
1441 vm->time_last_barrier_release = vlib_time_now (vm);
1442 CLIB_MEMORY_STORE_BARRIER ();
1444 *vlib_worker_threads->wait_at_barrier = 0;
1446 while (*vlib_worker_threads->workers_at_barrier > 0)
1448 if ((now = vlib_time_now (vm)) > deadline)
1450 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1455 /* Wait for reforks before continuing */
1458 now = vlib_time_now (vm);
1460 deadline = now + BARRIER_SYNC_TIMEOUT;
1462 while (*vlib_worker_threads->node_reforks_required > 0)
1464 if ((now = vlib_time_now (vm)) > deadline)
1466 fformat (stderr, "%s: worker thread refork deadlock\n",
1471 vlib_stat_segment_unlock ();
1474 t_closed_total = now - vm->barrier_epoch;
1476 minimum_open = t_closed_total * BARRIER_MINIMUM_OPEN_FACTOR;
1478 if (minimum_open > BARRIER_MINIMUM_OPEN_LIMIT)
1480 minimum_open = BARRIER_MINIMUM_OPEN_LIMIT;
1483 vm->barrier_no_close_before = now + minimum_open;
1485 /* Record barrier epoch (used to enforce minimum open time) */
1486 vm->barrier_epoch = now;
1488 barrier_trace_release (t_entry, t_closed_total, t_update_main);
1490 if (PREDICT_FALSE (vec_len (vm->barrier_perf_callbacks) != 0))
1491 clib_call_callbacks (vm->barrier_perf_callbacks, vm,
1492 vm->clib_time.last_cpu_time, 1 /* leave */ );
1496 * Wait until each of the workers has been once around the track
1499 vlib_worker_wait_one_loop (void)
1501 vlib_global_main_t *vgm = vlib_get_global_main ();
1502 ASSERT (vlib_get_thread_index () == 0);
1504 if (vlib_get_n_threads () < 2)
1507 if (vlib_worker_thread_barrier_held ())
1513 vec_validate (counts, vlib_get_n_threads () - 1);
1515 /* record the current loop counts */
1516 vec_foreach_index (ii, vgm->vlib_mains)
1517 counts[ii] = vgm->vlib_mains[ii]->main_loop_count;
1519 /* spin until each changes, apart from the main thread, or we'd be
1521 for (ii = 1; ii < vec_len (counts); ii++)
1523 while (counts[ii] == vgm->vlib_mains[ii]->main_loop_count)
1532 vlib_worker_thread_fn (void *arg)
1534 vlib_global_main_t *vgm = vlib_get_global_main ();
1535 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
1536 vlib_thread_main_t *tm = vlib_get_thread_main ();
1537 vlib_main_t *vm = vlib_get_main ();
1540 vlib_process_finish_switch_stack (vm);
1542 ASSERT (vm->thread_index == vlib_get_thread_index ());
1544 vlib_worker_thread_init (w);
1545 clib_time_init (&vm->clib_time);
1546 clib_mem_set_heap (w->thread_mheap);
1548 vm->worker_init_functions_called = hash_create (0, 0);
1550 e = vlib_call_init_exit_functions_no_sort (
1551 vm, &vgm->worker_init_function_registrations, 1 /* call_once */,
1554 clib_error_report (e);
1556 /* Wait until the dpdk init sequence is complete */
1557 while (tm->extern_thread_mgmt && tm->worker_thread_release == 0)
1558 vlib_worker_thread_barrier_check ();
1560 vlib_worker_loop (vm);
1564 VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1567 .function = vlib_worker_thread_fn,
1572 vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1574 vlib_thread_main_t *tm = vlib_get_thread_main ();
1575 vlib_frame_queue_main_t *fqm;
1576 vlib_frame_queue_t *fq;
1580 if (frame_queue_nelts == 0)
1581 frame_queue_nelts = FRAME_QUEUE_MAX_NELTS;
1583 num_threads = 1 /* main thread */ + tm->n_threads;
1584 ASSERT (frame_queue_nelts >= 8 + num_threads);
1586 vec_add2 (tm->frame_queue_mains, fqm, 1);
1588 fqm->node_index = node_index;
1589 fqm->frame_queue_nelts = frame_queue_nelts;
1590 fqm->queue_hi_thresh = frame_queue_nelts - num_threads;
1592 vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1);
1593 vec_validate (fqm->per_thread_data, tm->n_vlib_mains - 1);
1594 _vec_len (fqm->vlib_frame_queues) = 0;
1595 for (i = 0; i < tm->n_vlib_mains; i++)
1597 vlib_frame_queue_per_thread_data_t *ptd;
1598 fq = vlib_frame_queue_alloc (frame_queue_nelts);
1599 vec_add1 (fqm->vlib_frame_queues, fq);
1601 ptd = vec_elt_at_index (fqm->per_thread_data, i);
1602 vec_validate (ptd->handoff_queue_elt_by_thread_index,
1603 tm->n_vlib_mains - 1);
1604 vec_validate_init_empty (ptd->congested_handoff_queue_by_thread_index,
1605 tm->n_vlib_mains - 1,
1606 (vlib_frame_queue_t *) (~0));
1609 return (fqm - tm->frame_queue_mains);
1613 vlib_thread_cb_register (struct vlib_main_t *vm, vlib_thread_callbacks_t * cb)
1615 vlib_thread_main_t *tm = vlib_get_thread_main ();
1617 if (tm->extern_thread_mgmt)
1620 tm->cb.vlib_launch_thread_cb = cb->vlib_launch_thread_cb;
1621 tm->extern_thread_mgmt = 1;
1626 vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t *
1629 ASSERT (vlib_get_thread_index () == 0);
1630 vlib_process_signal_event (vlib_get_main (), args->node_index,
1631 args->type_opaque, args->data);
1634 void *rpc_call_main_thread_cb_fn;
1637 vlib_rpc_call_main_thread (void *callback, u8 * args, u32 arg_size)
1639 if (rpc_call_main_thread_cb_fn)
1641 void (*fp) (void *, u8 *, u32) = rpc_call_main_thread_cb_fn;
1642 (*fp) (callback, args, arg_size);
1645 clib_warning ("BUG: rpc_call_main_thread_cb_fn NULL!");
1649 threads_init (vlib_main_t * vm)
1654 VLIB_INIT_FUNCTION (threads_init);
1657 static clib_error_t *
1658 show_clock_command_fn (vlib_main_t * vm,
1659 unformat_input_t * input, vlib_cli_command_t * cmd)
1663 clib_timebase_t _tb, *tb = &_tb;
1665 (void) unformat (input, "verbose %=", &verbose, 1);
1667 clib_timebase_init (tb, 0 /* GMT */ , CLIB_TIMEBASE_DAYLIGHT_NONE,
1670 vlib_cli_output (vm, "%U, %U GMT", format_clib_time, &vm->clib_time,
1671 verbose, format_clib_timebase_time,
1672 clib_timebase_now (tb));
1674 if (vlib_get_n_threads () == 1)
1677 vlib_cli_output (vm, "Time last barrier release %.9f",
1678 vm->time_last_barrier_release);
1680 for (i = 1; i < vlib_get_n_threads (); i++)
1682 vlib_main_t *ovm = vlib_get_main_by_index (i);
1686 vlib_cli_output (vm, "%d: %U", i, format_clib_time, &ovm->clib_time,
1690 vm, "Thread %d offset %.9f error %.9f", i, ovm->time_offset,
1691 vm->time_last_barrier_release - ovm->time_last_barrier_release);
1697 VLIB_CLI_COMMAND (f_command, static) =
1699 .path = "show clock",
1700 .short_help = "show clock",
1701 .function = show_clock_command_fn,
1705 vlib_thread_main_t *
1706 vlib_get_thread_main_not_inline (void)
1708 return vlib_get_thread_main ();
1712 * fd.io coding-style-patch-verification: ON
1715 * eval: (c-set-style "gnu")