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/linux/sysfs.h>
21 #include <vlib/vlib.h>
23 #include <vlib/threads.h>
24 #include <vlib/unix/cj.h>
26 #include <vlib/stat_weak_inlines.h>
28 DECLARE_CJ_GLOBAL_LOG;
30 #define FRAME_QUEUE_NELTS 64
38 vlib_worker_thread_t *vlib_worker_threads;
39 vlib_thread_main_t vlib_thread_main;
42 * Barrier tracing can be enabled on a normal build to collect information
43 * on barrier use, including timings and call stacks. Deliberately not
44 * keyed off CLIB_DEBUG, because that can add significant overhead which
45 * imapacts observed timings.
49 elog_global_id_for_msg_name (const char *msg_name)
56 h = hash_create_string (0, sizeof (uword));
58 p = hash_get_mem (h, msg_name);
61 r = elog_string (&vlib_global_main.elog_main, "%s", msg_name);
63 name_copy = format (0, "%s%c", msg_name, 0);
65 hash_set_mem (h, name_copy, r);
71 barrier_trace_sync (f64 t_entry, f64 t_open, f64 t_closed)
73 if (!vlib_worker_threads->barrier_elog_enabled)
77 ELOG_TYPE_DECLARE (e) =
79 .format = "bar-trace-%s-#%d",
80 .format_args = "T4i4",
85 u32 caller, count, t_entry, t_open, t_closed;
88 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
89 ed->count = (int) vlib_worker_threads[0].barrier_sync_count;
90 ed->caller = elog_global_id_for_msg_name
91 (vlib_worker_threads[0].barrier_caller);
92 ed->t_entry = (int) (1000000.0 * t_entry);
93 ed->t_open = (int) (1000000.0 * t_open);
94 ed->t_closed = (int) (1000000.0 * t_closed);
98 barrier_trace_sync_rec (f64 t_entry)
100 if (!vlib_worker_threads->barrier_elog_enabled)
104 ELOG_TYPE_DECLARE (e) =
106 .format = "bar-syncrec-%s-#%d",
107 .format_args = "T4i4",
115 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
116 ed->depth = (int) vlib_worker_threads[0].recursion_level - 1;
117 ed->caller = elog_global_id_for_msg_name
118 (vlib_worker_threads[0].barrier_caller);
122 barrier_trace_release_rec (f64 t_entry)
124 if (!vlib_worker_threads->barrier_elog_enabled)
128 ELOG_TYPE_DECLARE (e) =
130 .format = "bar-relrrec-#%d",
139 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
140 ed->depth = (int) vlib_worker_threads[0].recursion_level;
144 barrier_trace_release (f64 t_entry, f64 t_closed_total, f64 t_update_main)
146 if (!vlib_worker_threads->barrier_elog_enabled)
150 ELOG_TYPE_DECLARE (e) =
152 .format = "bar-rel-#%d-e%d-u%d-t%d",
153 .format_args = "i4i4i4i4",
158 u32 count, t_entry, t_update_main, t_closed_total;
161 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
162 ed->t_entry = (int) (1000000.0 * t_entry);
163 ed->t_update_main = (int) (1000000.0 * t_update_main);
164 ed->t_closed_total = (int) (1000000.0 * t_closed_total);
165 ed->count = (int) vlib_worker_threads[0].barrier_sync_count;
167 /* Reset context for next trace */
168 vlib_worker_threads[0].barrier_context = NULL;
172 os_get_nthreads (void)
174 return vec_len (vlib_thread_stacks);
178 vlib_set_thread_name (char *name)
180 int pthread_setname_np (pthread_t __target_thread, const char *__name);
182 pthread_t thread = pthread_self ();
186 rv = pthread_setname_np (thread, name);
188 clib_warning ("pthread_setname_np returned %d", rv);
193 sort_registrations_by_no_clone (void *a0, void *a1)
195 vlib_thread_registration_t **tr0 = a0;
196 vlib_thread_registration_t **tr1 = a1;
198 return ((i32) ((*tr0)->no_data_structure_clone)
199 - ((i32) ((*tr1)->no_data_structure_clone)));
203 clib_sysfs_list_to_bitmap (char *filename)
208 fp = fopen (filename, "r");
213 vec_validate (buffer, 256 - 1);
214 if (fgets ((char *) buffer, 256, fp))
217 unformat_init_string (&in, (char *) buffer,
218 strlen ((char *) buffer));
219 if (unformat (&in, "%U", unformat_bitmap_list, &r) != 1)
220 clib_warning ("unformat_bitmap_list failed");
230 /* Called early in the init sequence */
233 vlib_thread_init (vlib_main_t * vm)
235 vlib_thread_main_t *tm = &vlib_thread_main;
236 vlib_worker_thread_t *w;
237 vlib_thread_registration_t *tr;
238 u32 n_vlib_mains = 1;
243 /* get bitmaps of active cpu cores and sockets */
244 tm->cpu_core_bitmap =
245 clib_sysfs_list_to_bitmap ("/sys/devices/system/cpu/online");
246 tm->cpu_socket_bitmap =
247 clib_sysfs_list_to_bitmap ("/sys/devices/system/node/online");
249 avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
252 for (i = 0; i < tm->skip_cores; i++)
254 uword c = clib_bitmap_first_set (avail_cpu);
256 return clib_error_return (0, "no available cpus to skip");
258 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
261 /* grab cpu for main thread */
262 if (tm->main_lcore == ~0)
264 /* if main-lcore is not set, we try to use lcore 1 */
265 if (clib_bitmap_get (avail_cpu, 1))
268 tm->main_lcore = clib_bitmap_first_set (avail_cpu);
269 if (tm->main_lcore == (u8) ~ 0)
270 return clib_error_return (0, "no available cpus to be used for the"
275 if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
276 return clib_error_return (0, "cpu %u is not available to be used"
277 " for the main thread", tm->main_lcore);
279 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
281 /* assume that there is socket 0 only if there is no data from sysfs */
282 if (!tm->cpu_socket_bitmap)
283 tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
285 /* pin main thread to main_lcore */
286 if (tm->cb.vlib_thread_set_lcore_cb)
288 tm->cb.vlib_thread_set_lcore_cb (0, tm->main_lcore);
294 CPU_SET (tm->main_lcore, &cpuset);
295 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
298 /* Set up thread 0 */
299 vec_validate_aligned (vlib_worker_threads, 0, CLIB_CACHE_LINE_BYTES);
300 _vec_len (vlib_worker_threads) = 1;
301 w = vlib_worker_threads;
302 w->thread_mheap = clib_mem_get_heap ();
303 w->thread_stack = vlib_thread_stacks[0];
304 w->cpu_id = tm->main_lcore;
305 w->lwp = syscall (SYS_gettid);
306 w->thread_id = pthread_self ();
307 tm->n_vlib_mains = 1;
309 if (tm->sched_policy != ~0)
311 struct sched_param sched_param;
312 if (!sched_getparam (w->lwp, &sched_param))
314 if (tm->sched_priority != ~0)
315 sched_param.sched_priority = tm->sched_priority;
316 sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
320 /* assign threads to cores and set n_vlib_mains */
325 vec_add1 (tm->registrations, tr);
329 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
331 for (i = 0; i < vec_len (tm->registrations); i++)
334 tr = tm->registrations[i];
335 tr->first_index = first_index;
336 first_index += tr->count;
337 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
339 /* construct coremask */
340 if (tr->use_pthreads || !tr->count)
347 clib_bitmap_foreach (c, tr->coremask, ({
348 if (clib_bitmap_get(avail_cpu, c) == 0)
349 return clib_error_return (0, "cpu %u is not available to be used"
350 " for the '%s' thread",c, tr->name);
352 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
358 for (j = 0; j < tr->count; j++)
360 uword c = clib_bitmap_first_set (avail_cpu);
362 return clib_error_return (0,
363 "no available cpus to be used for"
364 " the '%s' thread", tr->name);
366 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
367 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
372 clib_bitmap_free (avail_cpu);
374 tm->n_vlib_mains = n_vlib_mains;
377 * Allocate the remaining worker threads, and thread stack vector slots
378 * from now on, calls to os_get_nthreads() will return the correct
381 vec_validate_aligned (vlib_worker_threads, first_index - 1,
382 CLIB_CACHE_LINE_BYTES);
383 vec_validate (vlib_thread_stacks, vec_len (vlib_worker_threads) - 1);
388 vlib_frame_queue_alloc (int nelts)
390 vlib_frame_queue_t *fq;
392 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
393 clib_memset (fq, 0, sizeof (*fq));
395 fq->vector_threshold = 128; // packets
396 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
400 if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
401 fformat (stderr, "WARNING: fq->tail unaligned\n");
402 if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
403 fformat (stderr, "WARNING: fq->head unaligned\n");
404 if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
405 fformat (stderr, "WARNING: fq->elts unaligned\n");
407 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
408 fformat (stderr, "WARNING: fq->elts[0] size %d\n",
409 sizeof (fq->elts[0]));
410 if (nelts & (nelts - 1))
412 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
420 void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
422 vl_msg_api_handler_no_free (void *v)
426 /* Turned off, save as reference material... */
429 vlib_frame_queue_dequeue_internal (int thread_id,
430 vlib_main_t * vm, vlib_node_main_t * nm)
432 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
433 vlib_frame_queue_elt_t *elt;
435 vlib_pending_frame_t *p;
436 vlib_node_runtime_t *r;
437 u32 node_runtime_index;
442 ASSERT (vm == vlib_mains[thread_id]);
446 if (fq->head == fq->tail)
449 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
454 before = clib_cpu_time_now ();
457 node_runtime_index = elt->node_runtime_index;
458 msg_type = elt->msg_type;
462 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
463 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
464 /* note fallthrough... */
465 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
466 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
468 vlib_frame_free (vm, r, f);
470 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
471 vec_add2 (vm->node_main.pending_frames, p, 1);
472 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
473 p->node_runtime_index = elt->node_runtime_index;
474 p->frame_index = vlib_frame_index (vm, f);
475 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
476 fq->dequeue_vectors += (u64) f->n_vectors;
478 case VLIB_FRAME_QUEUE_ELT_API_MSG:
479 vl_msg_api_handler_no_free (f);
482 clib_warning ("bogus frame queue message, type %d", msg_type);
487 fq->dequeue_ticks += clib_cpu_time_now () - before;
488 CLIB_MEMORY_BARRIER ();
497 vlib_frame_queue_dequeue (int thread_id,
498 vlib_main_t * vm, vlib_node_main_t * nm)
500 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
504 vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
505 u32 frame_queue_index, vlib_frame_t * frame,
506 vlib_frame_queue_msg_type_t type)
508 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
509 vlib_frame_queue_elt_t *elt;
512 u64 before = clib_cpu_time_now ();
516 new_tail = clib_atomic_add_fetch (&fq->tail, 1);
518 /* Wait until a ring slot is available */
519 while (new_tail >= fq->head + fq->nelts)
521 f64 b4 = vlib_time_now_ticks (vm, before);
522 vlib_worker_thread_barrier_check (vm, b4);
523 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
524 // vlib_frame_queue_dequeue (vm->thread_index, vm, nm);
527 elt = fq->elts + (new_tail & (fq->nelts - 1));
529 /* this would be very bad... */
534 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
535 save_count = frame->n_vectors;
538 elt->node_runtime_index = node_runtime_index;
539 elt->msg_type = type;
540 CLIB_MEMORY_BARRIER ();
547 /* To be called by vlib worker threads upon startup */
549 vlib_worker_thread_init (vlib_worker_thread_t * w)
551 vlib_thread_main_t *tm = vlib_get_thread_main ();
554 * Note: disabling signals in worker threads as follows
555 * prevents the api post-mortem dump scheme from working
559 * pthread_sigmask (SIG_SETMASK, &s, 0);
563 clib_mem_set_heap (w->thread_mheap);
565 if (vec_len (tm->thread_prefix) && w->registration->short_name)
567 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
568 w->registration->short_name, w->instance_id, '\0');
569 vlib_set_thread_name ((char *) w->name);
572 if (!w->registration->use_pthreads)
575 /* Initial barrier sync, for both worker and i/o threads */
576 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, 1);
578 while (*vlib_worker_threads->wait_at_barrier)
581 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, -1);
586 vlib_worker_thread_bootstrap_fn (void *arg)
589 vlib_worker_thread_t *w = arg;
591 w->lwp = syscall (SYS_gettid);
592 w->thread_id = pthread_self ();
594 __os_thread_index = w - vlib_worker_threads;
596 rv = (void *) clib_calljmp
597 ((uword (*)(uword)) w->thread_function,
598 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
599 /* NOTREACHED, we hope */
604 vlib_get_thread_core_socket (vlib_worker_thread_t * w, unsigned cpu_id)
606 const char *sys_cpu_path = "/sys/devices/system/cpu/cpu";
608 int core_id = -1, socket_id = -1;
610 p = format (p, "%s%u/topology/core_id%c", sys_cpu_path, cpu_id, 0);
611 clib_sysfs_read ((char *) p, "%d", &core_id);
612 vec_reset_length (p);
614 format (p, "%s%u/topology/physical_package_id%c", sys_cpu_path, cpu_id,
616 clib_sysfs_read ((char *) p, "%d", &socket_id);
619 w->core_id = core_id;
620 w->socket_id = socket_id;
623 static clib_error_t *
624 vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned cpu_id)
626 vlib_thread_main_t *tm = &vlib_thread_main;
627 void *(*fp_arg) (void *) = fp;
630 vlib_get_thread_core_socket (w, cpu_id);
631 if (tm->cb.vlib_launch_thread_cb && !w->registration->use_pthreads)
632 return tm->cb.vlib_launch_thread_cb (fp, (void *) w, cpu_id);
638 CPU_SET (cpu_id, &cpuset);
640 if (pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w))
641 return clib_error_return_unix (0, "pthread_create");
643 if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
644 return clib_error_return_unix (0, "pthread_setaffinity_np");
650 static clib_error_t *
651 start_workers (vlib_main_t * vm)
654 vlib_worker_thread_t *w;
655 vlib_main_t *vm_clone;
657 vlib_thread_main_t *tm = &vlib_thread_main;
658 vlib_thread_registration_t *tr;
659 vlib_node_runtime_t *rt;
660 u32 n_vlib_mains = tm->n_vlib_mains;
661 u32 worker_thread_index;
662 u8 *main_heap = clib_mem_get_per_cpu_heap ();
664 vec_reset_length (vlib_worker_threads);
666 /* Set up the main thread */
667 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
668 w->elog_track.name = "main thread";
669 elog_track_register (&vm->elog_main, &w->elog_track);
671 if (vec_len (tm->thread_prefix))
673 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
674 vlib_set_thread_name ((char *) w->name);
678 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
679 vm->elog_main.lock[0] = 0;
681 if (n_vlib_mains > 1)
683 /* Replace hand-crafted length-1 vector with a real vector */
686 vec_validate_aligned (vlib_mains, tm->n_vlib_mains - 1,
687 CLIB_CACHE_LINE_BYTES);
688 _vec_len (vlib_mains) = 0;
689 vec_add1_aligned (vlib_mains, vm, CLIB_CACHE_LINE_BYTES);
691 vlib_worker_threads->wait_at_barrier =
692 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
693 vlib_worker_threads->workers_at_barrier =
694 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
696 vlib_worker_threads->node_reforks_required =
697 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
699 /* We'll need the rpc vector lock... */
700 clib_spinlock_init (&vm->pending_rpc_lock);
702 /* Ask for an initial barrier sync */
703 *vlib_worker_threads->workers_at_barrier = 0;
704 *vlib_worker_threads->wait_at_barrier = 1;
706 /* Without update or refork */
707 *vlib_worker_threads->node_reforks_required = 0;
708 vm->need_vlib_worker_thread_node_runtime_update = 0;
711 vm->barrier_epoch = 0;
712 vm->barrier_no_close_before = 0;
714 worker_thread_index = 1;
716 for (i = 0; i < vec_len (tm->registrations); i++)
718 vlib_node_main_t *nm, *nm_clone;
721 tr = tm->registrations[i];
726 for (k = 0; k < tr->count; k++)
730 vec_add2 (vlib_worker_threads, w, 1);
731 /* Currently unused, may not really work */
734 #if USE_DLMALLOC == 0
736 mheap_alloc (0 /* use VM */ , tr->mheap_size);
738 w->thread_mheap = create_mspace (tr->mheap_size,
743 w->thread_mheap = main_heap;
746 vlib_thread_stack_init (w - vlib_worker_threads);
747 w->thread_function = tr->function;
748 w->thread_function_arg = w;
750 w->registration = tr;
753 (char *) format (0, "%s %d", tr->name, k + 1);
754 vec_add1 (w->elog_track.name, 0);
755 elog_track_register (&vm->elog_main, &w->elog_track);
757 if (tr->no_data_structure_clone)
760 /* Fork vlib_global_main et al. Look for bugs here */
761 oldheap = clib_mem_set_heap (w->thread_mheap);
763 vm_clone = clib_mem_alloc_aligned (sizeof (*vm_clone),
764 CLIB_CACHE_LINE_BYTES);
765 clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
767 vm_clone->thread_index = worker_thread_index;
768 vm_clone->heap_base = w->thread_mheap;
769 vm_clone->heap_aligned_base = (void *)
770 (((uword) w->thread_mheap) & ~(VLIB_FRAME_ALIGN - 1));
771 vm_clone->init_functions_called =
772 hash_create (0, /* value bytes */ 0);
773 vm_clone->pending_rpc_requests = 0;
774 vec_validate (vm_clone->pending_rpc_requests, 0);
775 _vec_len (vm_clone->pending_rpc_requests) = 0;
776 clib_memset (&vm_clone->random_buffer, 0,
777 sizeof (vm_clone->random_buffer));
779 nm = &vlib_mains[0]->node_main;
780 nm_clone = &vm_clone->node_main;
781 /* fork next frames array, preserving node runtime indices */
782 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
783 CLIB_CACHE_LINE_BYTES);
784 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
786 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
787 u32 save_node_runtime_index;
790 save_node_runtime_index = nf->node_runtime_index;
791 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
792 vlib_next_frame_init (nf);
793 nf->node_runtime_index = save_node_runtime_index;
794 nf->flags = save_flags;
797 /* fork the frame dispatch queue */
798 nm_clone->pending_frames = 0;
799 vec_validate (nm_clone->pending_frames, 10);
800 _vec_len (nm_clone->pending_frames) = 0;
805 /* Allocate all nodes in single block for speed */
806 n = clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*n));
808 for (j = 0; j < vec_len (nm->nodes); j++)
810 clib_memcpy (n, nm->nodes[j], sizeof (*n));
811 /* none of the copied nodes have enqueue rights given out */
812 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
813 clib_memset (&n->stats_total, 0, sizeof (n->stats_total));
814 clib_memset (&n->stats_last_clear, 0,
815 sizeof (n->stats_last_clear));
816 vec_add1 (nm_clone->nodes, n);
819 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
820 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
821 CLIB_CACHE_LINE_BYTES);
823 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
825 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
826 rt->thread_index = vm_clone->thread_index;
827 /* copy initial runtime_data from node */
828 if (n->runtime_data && n->runtime_data_bytes > 0)
829 clib_memcpy (rt->runtime_data, n->runtime_data,
830 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
831 n->runtime_data_bytes));
834 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
835 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
836 CLIB_CACHE_LINE_BYTES);
837 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
839 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
840 rt->thread_index = vm_clone->thread_index;
841 /* copy initial runtime_data from node */
842 if (n->runtime_data && n->runtime_data_bytes > 0)
843 clib_memcpy (rt->runtime_data, n->runtime_data,
844 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
845 n->runtime_data_bytes));
848 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT] =
849 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT],
850 CLIB_CACHE_LINE_BYTES);
852 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT])
854 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
855 rt->thread_index = vm_clone->thread_index;
856 /* copy initial runtime_data from node */
857 if (n->runtime_data && n->runtime_data_bytes > 0)
858 clib_memcpy (rt->runtime_data, n->runtime_data,
859 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
860 n->runtime_data_bytes));
863 nm_clone->processes = vec_dup_aligned (nm->processes,
864 CLIB_CACHE_LINE_BYTES);
866 /* Create per-thread frame freelist */
867 nm_clone->frame_sizes = vec_new (vlib_frame_size_t, 1);
868 #ifdef VLIB_SUPPORTS_ARBITRARY_SCALAR_SIZES
869 nm_clone->frame_size_hash = hash_create (0, sizeof (uword));
872 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
874 clib_mem_set_heap (oldheap);
875 vec_add1_aligned (vlib_mains, vm_clone, CLIB_CACHE_LINE_BYTES);
877 /* Switch to the stats segment ... */
878 void *oldheap = vlib_stats_push_heap (0);
879 vm_clone->error_main.counters = vec_dup_aligned
880 (vlib_mains[0]->error_main.counters, CLIB_CACHE_LINE_BYTES);
881 vlib_stats_pop_heap2 (vm_clone->error_main.counters,
882 worker_thread_index, oldheap, 1);
884 vm_clone->error_main.counters_last_clear = vec_dup_aligned
885 (vlib_mains[0]->error_main.counters_last_clear,
886 CLIB_CACHE_LINE_BYTES);
888 worker_thread_index++;
894 /* only have non-data-structure copy threads to create... */
895 for (i = 0; i < vec_len (tm->registrations); i++)
897 tr = tm->registrations[i];
899 for (j = 0; j < tr->count; j++)
901 vec_add2 (vlib_worker_threads, w, 1);
904 #if USE_DLMALLOC == 0
906 mheap_alloc (0 /* use VM */ , tr->mheap_size);
909 create_mspace (tr->mheap_size, 0 /* locked */ );
913 w->thread_mheap = main_heap;
915 vlib_thread_stack_init (w - vlib_worker_threads);
916 w->thread_function = tr->function;
917 w->thread_function_arg = w;
920 (char *) format (0, "%s %d", tr->name, j + 1);
921 w->registration = tr;
922 vec_add1 (w->elog_track.name, 0);
923 elog_track_register (&vm->elog_main, &w->elog_track);
928 worker_thread_index = 1;
930 for (i = 0; i < vec_len (tm->registrations); i++)
935 tr = tm->registrations[i];
937 if (tr->use_pthreads || tm->use_pthreads)
939 for (j = 0; j < tr->count; j++)
941 w = vlib_worker_threads + worker_thread_index++;
942 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
945 clib_error_report (err);
952 clib_bitmap_foreach (c, tr->coremask, ({
953 w = vlib_worker_threads + worker_thread_index++;
954 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
957 clib_error_report (err);
962 vlib_worker_thread_barrier_sync (vm);
963 vlib_worker_thread_barrier_release (vm);
967 VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
971 worker_thread_node_runtime_update_internal (void)
975 vlib_node_main_t *nm, *nm_clone;
976 vlib_main_t *vm_clone;
977 vlib_node_runtime_t *rt;
979 vlib_node_runtime_sync_stats (vlib_main_t * vm,
980 vlib_node_runtime_t * r,
982 uword n_vectors, uword n_clocks);
984 ASSERT (vlib_get_thread_index () == 0);
989 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
992 * Scrape all runtime stats, so we don't lose node runtime(s) with
993 * pending counts, or throw away worker / io thread counts.
995 for (j = 0; j < vec_len (nm->nodes); j++)
999 vlib_node_sync_stats (vm, n);
1002 for (i = 1; i < vec_len (vlib_mains); i++)
1006 vm_clone = vlib_mains[i];
1007 nm_clone = &vm_clone->node_main;
1009 for (j = 0; j < vec_len (nm_clone->nodes); j++)
1011 n = nm_clone->nodes[j];
1013 rt = vlib_node_get_runtime (vm_clone, n->index);
1014 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
1018 /* Per-worker clone rebuilds are now done on each thread */
1023 vlib_worker_thread_node_refork (void)
1025 vlib_main_t *vm, *vm_clone;
1026 vlib_node_main_t *nm, *nm_clone;
1027 vlib_node_t **old_nodes_clone;
1028 vlib_node_runtime_t *rt, *old_rt;
1030 vlib_node_t *new_n_clone;
1035 nm = &vm->node_main;
1036 vm_clone = vlib_get_main ();
1037 nm_clone = &vm_clone->node_main;
1039 /* Re-clone error heap */
1040 u64 *old_counters = vm_clone->error_main.counters;
1041 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
1043 clib_memcpy_fast (&vm_clone->error_main, &vm->error_main,
1044 sizeof (vm->error_main));
1045 j = vec_len (vm->error_main.counters) - 1;
1047 /* Switch to the stats segment ... */
1048 void *oldheap = vlib_stats_push_heap (0);
1049 vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES);
1050 vm_clone->error_main.counters = old_counters;
1051 vlib_stats_pop_heap2 (vm_clone->error_main.counters, vm_clone->thread_index,
1054 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
1055 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
1057 nm_clone = &vm_clone->node_main;
1058 vec_free (nm_clone->next_frames);
1059 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
1060 CLIB_CACHE_LINE_BYTES);
1062 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
1064 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
1065 u32 save_node_runtime_index;
1068 save_node_runtime_index = nf->node_runtime_index;
1069 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
1070 vlib_next_frame_init (nf);
1071 nf->node_runtime_index = save_node_runtime_index;
1072 nf->flags = save_flags;
1075 old_nodes_clone = nm_clone->nodes;
1076 nm_clone->nodes = 0;
1080 /* Allocate all nodes in single block for speed */
1082 clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*new_n_clone));
1083 for (j = 0; j < vec_len (nm->nodes); j++)
1085 vlib_node_t *old_n_clone;
1088 new_n = nm->nodes[j];
1089 old_n_clone = old_nodes_clone[j];
1091 clib_memcpy_fast (new_n_clone, new_n, sizeof (*new_n));
1092 /* none of the copied nodes have enqueue rights given out */
1093 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
1095 if (j >= vec_len (old_nodes_clone))
1097 /* new node, set to zero */
1098 clib_memset (&new_n_clone->stats_total, 0,
1099 sizeof (new_n_clone->stats_total));
1100 clib_memset (&new_n_clone->stats_last_clear, 0,
1101 sizeof (new_n_clone->stats_last_clear));
1105 /* Copy stats if the old data is valid */
1106 clib_memcpy_fast (&new_n_clone->stats_total,
1107 &old_n_clone->stats_total,
1108 sizeof (new_n_clone->stats_total));
1109 clib_memcpy_fast (&new_n_clone->stats_last_clear,
1110 &old_n_clone->stats_last_clear,
1111 sizeof (new_n_clone->stats_last_clear));
1113 /* keep previous node state */
1114 new_n_clone->state = old_n_clone->state;
1116 vec_add1 (nm_clone->nodes, new_n_clone);
1119 /* Free the old node clones */
1120 clib_mem_free (old_nodes_clone[0]);
1122 vec_free (old_nodes_clone);
1125 /* re-clone internal nodes */
1126 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL];
1127 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
1128 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
1129 CLIB_CACHE_LINE_BYTES);
1131 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
1133 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1134 rt->thread_index = vm_clone->thread_index;
1135 /* copy runtime_data, will be overwritten later for existing rt */
1136 if (n->runtime_data && n->runtime_data_bytes > 0)
1137 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1138 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1139 n->runtime_data_bytes));
1142 for (j = 0; j < vec_len (old_rt); j++)
1144 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1145 rt->state = old_rt[j].state;
1146 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1147 VLIB_NODE_RUNTIME_DATA_SIZE);
1152 /* re-clone input nodes */
1153 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
1154 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
1155 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
1156 CLIB_CACHE_LINE_BYTES);
1158 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
1160 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1161 rt->thread_index = vm_clone->thread_index;
1162 /* copy runtime_data, will be overwritten later for existing rt */
1163 if (n->runtime_data && n->runtime_data_bytes > 0)
1164 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1165 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1166 n->runtime_data_bytes));
1169 for (j = 0; j < vec_len (old_rt); j++)
1171 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1172 rt->state = old_rt[j].state;
1173 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1174 VLIB_NODE_RUNTIME_DATA_SIZE);
1179 /* re-clone pre-input nodes */
1180 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT];
1181 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT] =
1182 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT],
1183 CLIB_CACHE_LINE_BYTES);
1185 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT])
1187 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1188 rt->thread_index = vm_clone->thread_index;
1189 /* copy runtime_data, will be overwritten later for existing rt */
1190 if (n->runtime_data && n->runtime_data_bytes > 0)
1191 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1192 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1193 n->runtime_data_bytes));
1196 for (j = 0; j < vec_len (old_rt); j++)
1198 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1199 rt->state = old_rt[j].state;
1200 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1201 VLIB_NODE_RUNTIME_DATA_SIZE);
1206 nm_clone->processes = vec_dup_aligned (nm->processes,
1207 CLIB_CACHE_LINE_BYTES);
1211 vlib_worker_thread_node_runtime_update (void)
1214 * Make a note that we need to do a node runtime update
1215 * prior to releasing the barrier.
1217 vlib_global_main.need_vlib_worker_thread_node_runtime_update = 1;
1221 unformat_sched_policy (unformat_input_t * input, va_list * args)
1223 u32 *r = va_arg (*args, u32 *);
1226 #define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
1227 foreach_sched_policy
1234 static clib_error_t *
1235 cpu_config (vlib_main_t * vm, unformat_input_t * input)
1237 vlib_thread_registration_t *tr;
1239 vlib_thread_main_t *tm = &vlib_thread_main;
1244 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
1246 tm->n_thread_stacks = 1; /* account for main thread */
1247 tm->sched_policy = ~0;
1248 tm->sched_priority = ~0;
1249 tm->main_lcore = ~0;
1255 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
1259 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1261 if (unformat (input, "use-pthreads"))
1262 tm->use_pthreads = 1;
1263 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
1265 else if (unformat (input, "main-core %u", &tm->main_lcore))
1267 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
1269 else if (unformat (input, "coremask-%s %U", &name,
1270 unformat_bitmap_mask, &bitmap) ||
1271 unformat (input, "corelist-%s %U", &name,
1272 unformat_bitmap_list, &bitmap))
1274 p = hash_get_mem (tm->thread_registrations_by_name, name);
1276 return clib_error_return (0, "no such thread type '%s'", name);
1278 tr = (vlib_thread_registration_t *) p[0];
1280 if (tr->use_pthreads)
1281 return clib_error_return (0,
1282 "corelist cannot be set for '%s' threads",
1285 tr->coremask = bitmap;
1286 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1290 (input, "scheduler-policy %U", unformat_sched_policy,
1293 else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
1295 else if (unformat (input, "%s %u", &name, &count))
1297 p = hash_get_mem (tm->thread_registrations_by_name, name);
1299 return clib_error_return (0, "no such thread type 3 '%s'", name);
1301 tr = (vlib_thread_registration_t *) p[0];
1302 if (tr->fixed_count)
1303 return clib_error_return
1304 (0, "number of %s threads not configurable", tr->name);
1311 if (tm->sched_priority != ~0)
1313 if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
1315 u32 prio_max = sched_get_priority_max (tm->sched_policy);
1316 u32 prio_min = sched_get_priority_min (tm->sched_policy);
1317 if (tm->sched_priority > prio_max)
1318 tm->sched_priority = prio_max;
1319 if (tm->sched_priority < prio_min)
1320 tm->sched_priority = prio_min;
1324 return clib_error_return
1326 "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1327 tm->sched_priority);
1332 if (!tm->thread_prefix)
1333 tm->thread_prefix = format (0, "vpp");
1337 tm->n_thread_stacks += tr->count;
1338 tm->n_pthreads += tr->count * tr->use_pthreads;
1339 tm->n_threads += tr->count * (tr->use_pthreads == 0);
1346 VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1348 void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
1350 vnet_main_fixup (vlib_fork_fixup_t which)
1355 vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1357 vlib_main_t *vm = vlib_get_main ();
1359 if (vlib_mains == 0)
1362 ASSERT (vlib_get_thread_index () == 0);
1363 vlib_worker_thread_barrier_sync (vm);
1367 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1368 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1374 vlib_worker_thread_barrier_release (vm);
1378 * Enforce minimum open time to minimize packet loss due to Rx overflow,
1379 * based on a test based heuristic that barrier should be open for at least
1380 * 3 time as long as it is closed (with an upper bound of 1ms because by that
1381 * point it is probably too late to make a difference)
1384 #ifndef BARRIER_MINIMUM_OPEN_LIMIT
1385 #define BARRIER_MINIMUM_OPEN_LIMIT 0.001
1388 #ifndef BARRIER_MINIMUM_OPEN_FACTOR
1389 #define BARRIER_MINIMUM_OPEN_FACTOR 3
1393 vlib_worker_thread_initial_barrier_sync_and_release (vlib_main_t * vm)
1396 f64 now = vlib_time_now (vm);
1397 u32 count = vec_len (vlib_mains) - 1;
1399 /* No worker threads? */
1403 deadline = now + BARRIER_SYNC_TIMEOUT;
1404 *vlib_worker_threads->wait_at_barrier = 1;
1405 while (*vlib_worker_threads->workers_at_barrier != count)
1407 if ((now = vlib_time_now (vm)) > deadline)
1409 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1414 *vlib_worker_threads->wait_at_barrier = 0;
1418 vlib_worker_thread_barrier_sync_int (vlib_main_t * vm, const char *func_name)
1425 f64 max_vector_rate;
1429 if (vec_len (vlib_mains) < 2)
1432 ASSERT (vlib_get_thread_index () == 0);
1434 vlib_worker_threads[0].barrier_caller = func_name;
1435 count = vec_len (vlib_mains) - 1;
1437 /* Record entry relative to last close */
1438 now = vlib_time_now (vm);
1439 t_entry = now - vm->barrier_epoch;
1441 /* Tolerate recursive calls */
1442 if (++vlib_worker_threads[0].recursion_level > 1)
1444 barrier_trace_sync_rec (t_entry);
1449 * Need data to decide if we're working hard enough to honor
1450 * the barrier hold-down timer.
1452 max_vector_rate = 0.0;
1453 for (i = 1; i < vec_len (vlib_mains); i++)
1455 clib_max (max_vector_rate,
1456 vlib_last_vectors_per_main_loop_as_f64 (vlib_mains[i]));
1458 vlib_worker_threads[0].barrier_sync_count++;
1460 /* Enforce minimum barrier open time to minimize packet loss */
1461 ASSERT (vm->barrier_no_close_before <= (now + BARRIER_MINIMUM_OPEN_LIMIT));
1464 * If any worker thread seems busy, which we define
1465 * as a vector rate above 10, we enforce the barrier hold-down timer
1467 if (max_vector_rate > 10.0)
1471 now = vlib_time_now (vm);
1472 /* Barrier hold-down timer expired? */
1473 if (now >= vm->barrier_no_close_before)
1475 if ((vm->barrier_no_close_before - now)
1476 > (2.0 * BARRIER_MINIMUM_OPEN_LIMIT))
1479 ("clock change: would have waited for %.4f seconds",
1480 (vm->barrier_no_close_before - now));
1485 /* Record time of closure */
1486 t_open = now - vm->barrier_epoch;
1487 vm->barrier_epoch = now;
1489 deadline = now + BARRIER_SYNC_TIMEOUT;
1491 *vlib_worker_threads->wait_at_barrier = 1;
1492 while (*vlib_worker_threads->workers_at_barrier != count)
1494 if ((now = vlib_time_now (vm)) > deadline)
1496 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1501 t_closed = now - vm->barrier_epoch;
1503 barrier_trace_sync (t_entry, t_open, t_closed);
1508 vlib_worker_thread_barrier_release (vlib_main_t * vm)
1515 f64 t_update_main = 0.0;
1516 int refork_needed = 0;
1518 if (vec_len (vlib_mains) < 2)
1521 ASSERT (vlib_get_thread_index () == 0);
1524 now = vlib_time_now (vm);
1525 t_entry = now - vm->barrier_epoch;
1527 if (--vlib_worker_threads[0].recursion_level > 0)
1529 barrier_trace_release_rec (t_entry);
1533 /* Update (all) node runtimes before releasing the barrier, if needed */
1534 if (vm->need_vlib_worker_thread_node_runtime_update)
1537 * Lock stat segment here, so we's safe when
1538 * rebuilding the stat segment node clones from the
1541 vlib_stat_segment_lock ();
1543 /* Do stats elements on main thread */
1544 worker_thread_node_runtime_update_internal ();
1545 vm->need_vlib_worker_thread_node_runtime_update = 0;
1547 /* Do per thread rebuilds in parallel */
1549 clib_atomic_fetch_add (vlib_worker_threads->node_reforks_required,
1550 (vec_len (vlib_mains) - 1));
1551 now = vlib_time_now (vm);
1552 t_update_main = now - vm->barrier_epoch;
1555 deadline = now + BARRIER_SYNC_TIMEOUT;
1558 * Note when we let go of the barrier.
1559 * Workers can use this to derive a reasonably accurate
1560 * time offset. See vlib_time_now(...)
1562 vm->time_last_barrier_release = vlib_time_now (vm);
1563 CLIB_MEMORY_STORE_BARRIER ();
1565 *vlib_worker_threads->wait_at_barrier = 0;
1567 while (*vlib_worker_threads->workers_at_barrier > 0)
1569 if ((now = vlib_time_now (vm)) > deadline)
1571 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1576 /* Wait for reforks before continuing */
1579 now = vlib_time_now (vm);
1581 deadline = now + BARRIER_SYNC_TIMEOUT;
1583 while (*vlib_worker_threads->node_reforks_required > 0)
1585 if ((now = vlib_time_now (vm)) > deadline)
1587 fformat (stderr, "%s: worker thread refork deadlock\n",
1592 vlib_stat_segment_unlock ();
1595 t_closed_total = now - vm->barrier_epoch;
1597 minimum_open = t_closed_total * BARRIER_MINIMUM_OPEN_FACTOR;
1599 if (minimum_open > BARRIER_MINIMUM_OPEN_LIMIT)
1601 minimum_open = BARRIER_MINIMUM_OPEN_LIMIT;
1604 vm->barrier_no_close_before = now + minimum_open;
1606 /* Record barrier epoch (used to enforce minimum open time) */
1607 vm->barrier_epoch = now;
1609 barrier_trace_release (t_entry, t_closed_total, t_update_main);
1614 * Check the frame queue to see if any frames are available.
1615 * If so, pull the packets off the frames and put them to
1619 vlib_frame_queue_dequeue (vlib_main_t * vm, vlib_frame_queue_main_t * fqm)
1621 u32 thread_id = vm->thread_index;
1622 vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id];
1623 vlib_frame_queue_elt_t *elt;
1632 ASSERT (vm == vlib_mains[thread_id]);
1634 if (PREDICT_FALSE (fqm->node_index == ~0))
1637 * Gather trace data for frame queues
1639 if (PREDICT_FALSE (fq->trace))
1641 frame_queue_trace_t *fqt;
1642 frame_queue_nelt_counter_t *fqh;
1645 fqt = &fqm->frame_queue_traces[thread_id];
1647 fqt->nelts = fq->nelts;
1648 fqt->head = fq->head;
1649 fqt->head_hint = fq->head_hint;
1650 fqt->tail = fq->tail;
1651 fqt->threshold = fq->vector_threshold;
1652 fqt->n_in_use = fqt->tail - fqt->head;
1653 if (fqt->n_in_use >= fqt->nelts)
1655 // if beyond max then use max
1656 fqt->n_in_use = fqt->nelts - 1;
1659 /* Record the number of elements in use in the histogram */
1660 fqh = &fqm->frame_queue_histogram[thread_id];
1661 fqh->count[fqt->n_in_use]++;
1663 /* Record a snapshot of the elements in use */
1664 for (elix = 0; elix < fqt->nelts; elix++)
1666 elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1667 if (1 || elt->valid)
1669 fqt->n_vectors[elix] = elt->n_vectors;
1677 if (fq->head == fq->tail)
1679 fq->head_hint = fq->head;
1683 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
1687 fq->head_hint = fq->head;
1691 from = elt->buffer_index;
1692 msg_type = elt->msg_type;
1694 ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
1695 ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1697 f = vlib_get_frame_to_node (vm, fqm->node_index);
1699 to = vlib_frame_vector_args (f);
1701 n_left_to_node = elt->n_vectors;
1703 while (n_left_to_node >= 4)
1711 n_left_to_node -= 4;
1714 while (n_left_to_node > 0)
1722 vectors += elt->n_vectors;
1723 f->n_vectors = elt->n_vectors;
1724 vlib_put_frame_to_node (vm, fqm->node_index, f);
1728 elt->msg_type = 0xfefefefe;
1729 CLIB_MEMORY_BARRIER ();
1734 * Limit the number of packets pushed into the graph
1736 if (vectors >= fq->vector_threshold)
1738 fq->head_hint = fq->head;
1747 vlib_worker_thread_fn (void *arg)
1749 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
1750 vlib_thread_main_t *tm = vlib_get_thread_main ();
1751 vlib_main_t *vm = vlib_get_main ();
1754 ASSERT (vm->thread_index == vlib_get_thread_index ());
1756 vlib_worker_thread_init (w);
1757 clib_time_init (&vm->clib_time);
1758 clib_mem_set_heap (w->thread_mheap);
1760 e = vlib_call_init_exit_functions_no_sort
1761 (vm, &vm->worker_init_function_registrations, 1 /* call_once */ );
1763 clib_error_report (e);
1765 /* Wait until the dpdk init sequence is complete */
1766 while (tm->extern_thread_mgmt && tm->worker_thread_release == 0)
1767 vlib_worker_thread_barrier_check ();
1769 vlib_worker_loop (vm);
1773 VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1776 .function = vlib_worker_thread_fn,
1781 vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1783 vlib_thread_main_t *tm = vlib_get_thread_main ();
1784 vlib_frame_queue_main_t *fqm;
1785 vlib_frame_queue_t *fq;
1788 if (frame_queue_nelts == 0)
1789 frame_queue_nelts = FRAME_QUEUE_NELTS;
1791 ASSERT (frame_queue_nelts >= 8);
1793 vec_add2 (tm->frame_queue_mains, fqm, 1);
1795 fqm->node_index = node_index;
1796 fqm->frame_queue_nelts = frame_queue_nelts;
1797 fqm->queue_hi_thresh = frame_queue_nelts - 2;
1799 vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1);
1800 vec_validate (fqm->per_thread_data, tm->n_vlib_mains - 1);
1801 _vec_len (fqm->vlib_frame_queues) = 0;
1802 for (i = 0; i < tm->n_vlib_mains; i++)
1804 vlib_frame_queue_per_thread_data_t *ptd;
1805 fq = vlib_frame_queue_alloc (frame_queue_nelts);
1806 vec_add1 (fqm->vlib_frame_queues, fq);
1808 ptd = vec_elt_at_index (fqm->per_thread_data, i);
1809 vec_validate (ptd->handoff_queue_elt_by_thread_index,
1810 tm->n_vlib_mains - 1);
1811 vec_validate_init_empty (ptd->congested_handoff_queue_by_thread_index,
1812 tm->n_vlib_mains - 1,
1813 (vlib_frame_queue_t *) (~0));
1816 return (fqm - tm->frame_queue_mains);
1820 vlib_thread_cb_register (struct vlib_main_t *vm, vlib_thread_callbacks_t * cb)
1822 vlib_thread_main_t *tm = vlib_get_thread_main ();
1824 if (tm->extern_thread_mgmt)
1827 tm->cb.vlib_launch_thread_cb = cb->vlib_launch_thread_cb;
1828 tm->extern_thread_mgmt = 1;
1833 vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t *
1836 ASSERT (vlib_get_thread_index () == 0);
1837 vlib_process_signal_event (vlib_get_main (), args->node_index,
1838 args->type_opaque, args->data);
1841 void *rpc_call_main_thread_cb_fn;
1844 vlib_rpc_call_main_thread (void *callback, u8 * args, u32 arg_size)
1846 if (rpc_call_main_thread_cb_fn)
1848 void (*fp) (void *, u8 *, u32) = rpc_call_main_thread_cb_fn;
1849 (*fp) (callback, args, arg_size);
1852 clib_warning ("BUG: rpc_call_main_thread_cb_fn NULL!");
1856 threads_init (vlib_main_t * vm)
1861 VLIB_INIT_FUNCTION (threads_init);
1864 static clib_error_t *
1865 show_clock_command_fn (vlib_main_t * vm,
1866 unformat_input_t * input, vlib_cli_command_t * cmd)
1871 now = vlib_time_now (vm);
1873 vlib_cli_output (vm, "Time now %.9f", now);
1875 if (vec_len (vlib_mains) == 1)
1878 vlib_cli_output (vm, "Time last barrier release %.9f",
1879 vm->time_last_barrier_release);
1881 for (i = 1; i < vec_len (vlib_mains); i++)
1883 if (vlib_mains[i] == 0)
1885 vlib_cli_output (vm, "Thread %d offset %.9f error %.9f", i,
1886 vlib_mains[i]->time_offset,
1887 vm->time_last_barrier_release -
1888 vlib_mains[i]->time_last_barrier_release);
1894 VLIB_CLI_COMMAND (f_command, static) =
1896 .path = "show clock",
1897 .short_help = "show clock",
1898 .function = show_clock_command_fn,
1903 * fd.io coding-style-patch-verification: ON
1906 * eval: (c-set-style "gnu")