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/linux/sysfs.h>
22 #include <vlib/vlib.h>
24 #include <vlib/threads.h>
25 #include <vlib/unix/cj.h>
27 #include <vlib/stat_weak_inlines.h>
29 DECLARE_CJ_GLOBAL_LOG;
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 barrier_trace_sync (f64 t_entry, f64 t_open, f64 t_closed)
51 if (!vlib_worker_threads->barrier_elog_enabled)
55 ELOG_TYPE_DECLARE (e) =
57 .format = "bar-trace-%s-#%d",
58 .format_args = "T4i4",
63 u32 caller, count, t_entry, t_open, t_closed;
66 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
67 ed->count = (int) vlib_worker_threads[0].barrier_sync_count;
68 ed->caller = elog_string (&vlib_global_main.elog_main,
69 (char *) vlib_worker_threads[0].barrier_caller);
70 ed->t_entry = (int) (1000000.0 * t_entry);
71 ed->t_open = (int) (1000000.0 * t_open);
72 ed->t_closed = (int) (1000000.0 * t_closed);
76 barrier_trace_sync_rec (f64 t_entry)
78 if (!vlib_worker_threads->barrier_elog_enabled)
82 ELOG_TYPE_DECLARE (e) =
84 .format = "bar-syncrec-%s-#%d",
85 .format_args = "T4i4",
93 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
94 ed->depth = (int) vlib_worker_threads[0].recursion_level - 1;
95 ed->caller = elog_string (&vlib_global_main.elog_main,
96 (char *) vlib_worker_threads[0].barrier_caller);
100 barrier_trace_release_rec (f64 t_entry)
102 if (!vlib_worker_threads->barrier_elog_enabled)
106 ELOG_TYPE_DECLARE (e) =
108 .format = "bar-relrrec-#%d",
117 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
118 ed->depth = (int) vlib_worker_threads[0].recursion_level;
122 barrier_trace_release (f64 t_entry, f64 t_closed_total, f64 t_update_main)
124 if (!vlib_worker_threads->barrier_elog_enabled)
128 ELOG_TYPE_DECLARE (e) =
130 .format = "bar-rel-#%d-e%d-u%d-t%d",
131 .format_args = "i4i4i4i4",
136 u32 count, t_entry, t_update_main, t_closed_total;
139 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
140 ed->t_entry = (int) (1000000.0 * t_entry);
141 ed->t_update_main = (int) (1000000.0 * t_update_main);
142 ed->t_closed_total = (int) (1000000.0 * t_closed_total);
143 ed->count = (int) vlib_worker_threads[0].barrier_sync_count;
145 /* Reset context for next trace */
146 vlib_worker_threads[0].barrier_context = NULL;
150 os_get_nthreads (void)
152 return vec_len (vlib_thread_stacks);
156 vlib_set_thread_name (char *name)
158 int pthread_setname_np (pthread_t __target_thread, const char *__name);
160 pthread_t thread = pthread_self ();
164 rv = pthread_setname_np (thread, name);
166 clib_warning ("pthread_setname_np returned %d", rv);
171 sort_registrations_by_no_clone (void *a0, void *a1)
173 vlib_thread_registration_t **tr0 = a0;
174 vlib_thread_registration_t **tr1 = a1;
176 return ((i32) ((*tr0)->no_data_structure_clone)
177 - ((i32) ((*tr1)->no_data_structure_clone)));
181 clib_sysfs_list_to_bitmap (char *filename)
186 fp = fopen (filename, "r");
191 vec_validate (buffer, 256 - 1);
192 if (fgets ((char *) buffer, 256, fp))
195 unformat_init_string (&in, (char *) buffer,
196 strlen ((char *) buffer));
197 if (unformat (&in, "%U", unformat_bitmap_list, &r) != 1)
198 clib_warning ("unformat_bitmap_list failed");
208 /* Called early in the init sequence */
211 vlib_thread_init (vlib_main_t * vm)
213 vlib_thread_main_t *tm = &vlib_thread_main;
214 vlib_worker_thread_t *w;
215 vlib_thread_registration_t *tr;
216 u32 n_vlib_mains = 1;
221 /* get bitmaps of active cpu cores and sockets */
222 tm->cpu_core_bitmap =
223 clib_sysfs_list_to_bitmap ("/sys/devices/system/cpu/online");
224 tm->cpu_socket_bitmap =
225 clib_sysfs_list_to_bitmap ("/sys/devices/system/node/online");
227 avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
230 for (i = 0; i < tm->skip_cores; i++)
232 uword c = clib_bitmap_first_set (avail_cpu);
234 return clib_error_return (0, "no available cpus to skip");
236 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
239 /* grab cpu for main thread */
240 if (tm->main_lcore == ~0)
242 /* if main-lcore is not set, we try to use lcore 1 */
243 if (clib_bitmap_get (avail_cpu, 1))
246 tm->main_lcore = clib_bitmap_first_set (avail_cpu);
247 if (tm->main_lcore == (u8) ~ 0)
248 return clib_error_return (0, "no available cpus to be used for the"
253 if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
254 return clib_error_return (0, "cpu %u is not available to be used"
255 " for the main thread", tm->main_lcore);
257 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
260 * Determine if the number of workers is greater than 0.
261 * If so, mark CPU 0 unavailable so workers will be numbered after main.
264 uword *p = hash_get_mem (tm->thread_registrations_by_name, "workers");
267 vlib_thread_registration_t *tr = (vlib_thread_registration_t *) p[0];
268 int worker_thread_count = tr->count;
269 n_workers = worker_thread_count;
271 if (tm->skip_cores == 0 && n_workers)
272 avail_cpu = clib_bitmap_set (avail_cpu, 0, 0);
274 /* assume that there is socket 0 only if there is no data from sysfs */
275 if (!tm->cpu_socket_bitmap)
276 tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
278 /* pin main thread to main_lcore */
279 if (tm->cb.vlib_thread_set_lcore_cb)
281 tm->cb.vlib_thread_set_lcore_cb (0, tm->main_lcore);
287 CPU_SET (tm->main_lcore, &cpuset);
288 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
291 /* Set up thread 0 */
292 vec_validate_aligned (vlib_worker_threads, 0, CLIB_CACHE_LINE_BYTES);
293 _vec_len (vlib_worker_threads) = 1;
294 w = vlib_worker_threads;
295 w->thread_mheap = clib_mem_get_heap ();
296 w->thread_stack = vlib_thread_stacks[0];
297 w->cpu_id = tm->main_lcore;
298 w->lwp = syscall (SYS_gettid);
299 w->thread_id = pthread_self ();
300 tm->n_vlib_mains = 1;
302 vlib_get_thread_core_numa (w, w->cpu_id);
304 if (tm->sched_policy != ~0)
306 struct sched_param sched_param;
307 if (!sched_getparam (w->lwp, &sched_param))
309 if (tm->sched_priority != ~0)
310 sched_param.sched_priority = tm->sched_priority;
311 sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
315 /* assign threads to cores and set n_vlib_mains */
320 vec_add1 (tm->registrations, tr);
324 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
326 for (i = 0; i < vec_len (tm->registrations); i++)
329 tr = tm->registrations[i];
330 tr->first_index = first_index;
331 first_index += tr->count;
332 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
334 /* construct coremask */
335 if (tr->use_pthreads || !tr->count)
342 clib_bitmap_foreach (c, tr->coremask, ({
343 if (clib_bitmap_get(avail_cpu, c) == 0)
344 return clib_error_return (0, "cpu %u is not available to be used"
345 " for the '%s' thread",c, tr->name);
347 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
353 for (j = 0; j < tr->count; j++)
355 uword c = clib_bitmap_first_set (avail_cpu);
357 return clib_error_return (0,
358 "no available cpus to be used for"
359 " the '%s' thread", tr->name);
361 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
362 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
367 clib_bitmap_free (avail_cpu);
369 tm->n_vlib_mains = n_vlib_mains;
372 * Allocate the remaining worker threads, and thread stack vector slots
373 * from now on, calls to os_get_nthreads() will return the correct
376 vec_validate_aligned (vlib_worker_threads, first_index - 1,
377 CLIB_CACHE_LINE_BYTES);
378 vec_validate (vlib_thread_stacks, vec_len (vlib_worker_threads) - 1);
383 vlib_frame_queue_alloc (int nelts)
385 vlib_frame_queue_t *fq;
387 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
388 clib_memset (fq, 0, sizeof (*fq));
390 fq->vector_threshold = 128; // packets
391 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
395 if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
396 fformat (stderr, "WARNING: fq->tail unaligned\n");
397 if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
398 fformat (stderr, "WARNING: fq->head unaligned\n");
399 if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
400 fformat (stderr, "WARNING: fq->elts unaligned\n");
402 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
403 fformat (stderr, "WARNING: fq->elts[0] size %d\n",
404 sizeof (fq->elts[0]));
405 if (nelts & (nelts - 1))
407 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
415 void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
417 vl_msg_api_handler_no_free (void *v)
421 /* Turned off, save as reference material... */
424 vlib_frame_queue_dequeue_internal (int thread_id,
425 vlib_main_t * vm, vlib_node_main_t * nm)
427 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
428 vlib_frame_queue_elt_t *elt;
430 vlib_pending_frame_t *p;
431 vlib_node_runtime_t *r;
432 u32 node_runtime_index;
437 ASSERT (vm == vlib_mains[thread_id]);
441 if (fq->head == fq->tail)
444 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
449 before = clib_cpu_time_now ();
452 node_runtime_index = elt->node_runtime_index;
453 msg_type = elt->msg_type;
457 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
458 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
459 /* note fallthrough... */
460 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
461 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
463 vlib_frame_free (vm, r, f);
465 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
466 vec_add2 (vm->node_main.pending_frames, p, 1);
467 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
468 p->node_runtime_index = elt->node_runtime_index;
469 p->frame_index = vlib_frame_index (vm, f);
470 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
471 fq->dequeue_vectors += (u64) f->n_vectors;
473 case VLIB_FRAME_QUEUE_ELT_API_MSG:
474 vl_msg_api_handler_no_free (f);
477 clib_warning ("bogus frame queue message, type %d", msg_type);
482 fq->dequeue_ticks += clib_cpu_time_now () - before;
483 CLIB_MEMORY_BARRIER ();
492 vlib_frame_queue_dequeue (int thread_id,
493 vlib_main_t * vm, vlib_node_main_t * nm)
495 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
499 vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
500 u32 frame_queue_index, vlib_frame_t * frame,
501 vlib_frame_queue_msg_type_t type)
503 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
504 vlib_frame_queue_elt_t *elt;
507 u64 before = clib_cpu_time_now ();
511 new_tail = clib_atomic_add_fetch (&fq->tail, 1);
513 /* Wait until a ring slot is available */
514 while (new_tail >= fq->head + fq->nelts)
516 f64 b4 = vlib_time_now_ticks (vm, before);
517 vlib_worker_thread_barrier_check (vm, b4);
518 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
519 // vlib_frame_queue_dequeue (vm->thread_index, vm, nm);
522 elt = fq->elts + (new_tail & (fq->nelts - 1));
524 /* this would be very bad... */
529 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
530 save_count = frame->n_vectors;
533 elt->node_runtime_index = node_runtime_index;
534 elt->msg_type = type;
535 CLIB_MEMORY_BARRIER ();
542 /* To be called by vlib worker threads upon startup */
544 vlib_worker_thread_init (vlib_worker_thread_t * w)
546 vlib_thread_main_t *tm = vlib_get_thread_main ();
549 * Note: disabling signals in worker threads as follows
550 * prevents the api post-mortem dump scheme from working
554 * pthread_sigmask (SIG_SETMASK, &s, 0);
558 clib_mem_set_heap (w->thread_mheap);
560 if (vec_len (tm->thread_prefix) && w->registration->short_name)
562 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
563 w->registration->short_name, w->instance_id, '\0');
564 vlib_set_thread_name ((char *) w->name);
567 if (!w->registration->use_pthreads)
570 /* Initial barrier sync, for both worker and i/o threads */
571 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, 1);
573 while (*vlib_worker_threads->wait_at_barrier)
576 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, -1);
581 vlib_worker_thread_bootstrap_fn (void *arg)
584 vlib_worker_thread_t *w = arg;
586 w->lwp = syscall (SYS_gettid);
587 w->thread_id = pthread_self ();
589 __os_thread_index = w - vlib_worker_threads;
591 rv = (void *) clib_calljmp
592 ((uword (*)(uword)) w->thread_function,
593 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
594 /* NOTREACHED, we hope */
599 vlib_get_thread_core_numa (vlib_worker_thread_t * w, unsigned cpu_id)
601 const char *sys_cpu_path = "/sys/devices/system/cpu/cpu";
602 const char *sys_node_path = "/sys/devices/system/node/node";
603 clib_bitmap_t *nbmp = 0, *cbmp = 0;
606 int core_id = -1, numa_id = -1;
608 p = format (p, "%s%u/topology/core_id%c", sys_cpu_path, cpu_id, 0);
609 clib_sysfs_read ((char *) p, "%d", &core_id);
610 vec_reset_length (p);
613 clib_sysfs_read ("/sys/devices/system/node/online", "%U",
614 unformat_bitmap_list, &nbmp);
615 clib_bitmap_foreach (node, nbmp, ({
616 p = format (p, "%s%u/cpulist%c", sys_node_path, node, 0);
617 clib_sysfs_read ((char *) p, "%U", unformat_bitmap_list, &cbmp);
618 if (clib_bitmap_get (cbmp, cpu_id))
620 vec_reset_length (cbmp);
621 vec_reset_length (p);
628 w->core_id = core_id;
629 w->numa_id = numa_id;
632 static clib_error_t *
633 vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned cpu_id)
635 vlib_thread_main_t *tm = &vlib_thread_main;
636 void *(*fp_arg) (void *) = fp;
640 vlib_get_thread_core_numa (w, cpu_id);
642 /* Set up NUMA-bound heap if indicated */
643 if (clib_per_numa_mheaps[w->numa_id] == 0)
645 /* If the user requested a NUMA heap, create it... */
646 if (tm->numa_heap_size)
648 numa_heap = clib_mem_init_thread_safe_numa
649 (0 /* DIY */ , tm->numa_heap_size, w->numa_id);
650 clib_per_numa_mheaps[w->numa_id] = numa_heap;
654 /* Or, use the main heap */
655 clib_per_numa_mheaps[w->numa_id] = w->thread_mheap;
659 if (tm->cb.vlib_launch_thread_cb && !w->registration->use_pthreads)
660 return tm->cb.vlib_launch_thread_cb (fp, (void *) w, cpu_id);
666 CPU_SET (cpu_id, &cpuset);
668 if (pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w))
669 return clib_error_return_unix (0, "pthread_create");
671 if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
672 return clib_error_return_unix (0, "pthread_setaffinity_np");
678 static clib_error_t *
679 start_workers (vlib_main_t * vm)
682 vlib_worker_thread_t *w;
683 vlib_main_t *vm_clone;
685 vlib_thread_main_t *tm = &vlib_thread_main;
686 vlib_thread_registration_t *tr;
687 vlib_node_runtime_t *rt;
688 u32 n_vlib_mains = tm->n_vlib_mains;
689 u32 worker_thread_index;
690 u8 *main_heap = clib_mem_get_per_cpu_heap ();
692 vec_reset_length (vlib_worker_threads);
694 /* Set up the main thread */
695 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
696 w->elog_track.name = "main thread";
697 elog_track_register (&vm->elog_main, &w->elog_track);
699 if (vec_len (tm->thread_prefix))
701 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
702 vlib_set_thread_name ((char *) w->name);
706 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
707 vm->elog_main.lock[0] = 0;
709 if (n_vlib_mains > 1)
711 /* Replace hand-crafted length-1 vector with a real vector */
714 vec_validate_aligned (vlib_mains, tm->n_vlib_mains - 1,
715 CLIB_CACHE_LINE_BYTES);
716 _vec_len (vlib_mains) = 0;
717 vec_add1_aligned (vlib_mains, vm, CLIB_CACHE_LINE_BYTES);
719 vlib_worker_threads->wait_at_barrier =
720 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
721 vlib_worker_threads->workers_at_barrier =
722 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
724 vlib_worker_threads->node_reforks_required =
725 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
727 /* We'll need the rpc vector lock... */
728 clib_spinlock_init (&vm->pending_rpc_lock);
730 /* Ask for an initial barrier sync */
731 *vlib_worker_threads->workers_at_barrier = 0;
732 *vlib_worker_threads->wait_at_barrier = 1;
734 /* Without update or refork */
735 *vlib_worker_threads->node_reforks_required = 0;
736 vm->need_vlib_worker_thread_node_runtime_update = 0;
739 vm->barrier_epoch = 0;
740 vm->barrier_no_close_before = 0;
742 worker_thread_index = 1;
744 for (i = 0; i < vec_len (tm->registrations); i++)
746 vlib_node_main_t *nm, *nm_clone;
749 tr = tm->registrations[i];
754 for (k = 0; k < tr->count; k++)
758 vec_add2 (vlib_worker_threads, w, 1);
759 /* Currently unused, may not really work */
761 w->thread_mheap = create_mspace (tr->mheap_size,
764 w->thread_mheap = main_heap;
767 vlib_thread_stack_init (w - vlib_worker_threads);
768 w->thread_function = tr->function;
769 w->thread_function_arg = w;
771 w->registration = tr;
774 (char *) format (0, "%s %d", tr->name, k + 1);
775 vec_add1 (w->elog_track.name, 0);
776 elog_track_register (&vm->elog_main, &w->elog_track);
778 if (tr->no_data_structure_clone)
781 /* Fork vlib_global_main et al. Look for bugs here */
782 oldheap = clib_mem_set_heap (w->thread_mheap);
784 vm_clone = clib_mem_alloc_aligned (sizeof (*vm_clone),
785 CLIB_CACHE_LINE_BYTES);
786 clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
788 vm_clone->thread_index = worker_thread_index;
789 vm_clone->heap_base = w->thread_mheap;
790 vm_clone->heap_aligned_base = (void *)
791 (((uword) w->thread_mheap) & ~(VLIB_FRAME_ALIGN - 1));
792 vm_clone->init_functions_called =
793 hash_create (0, /* value bytes */ 0);
794 vm_clone->pending_rpc_requests = 0;
795 vec_validate (vm_clone->pending_rpc_requests, 0);
796 _vec_len (vm_clone->pending_rpc_requests) = 0;
797 clib_memset (&vm_clone->random_buffer, 0,
798 sizeof (vm_clone->random_buffer));
800 nm = &vlib_mains[0]->node_main;
801 nm_clone = &vm_clone->node_main;
802 /* fork next frames array, preserving node runtime indices */
803 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
804 CLIB_CACHE_LINE_BYTES);
805 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
807 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
808 u32 save_node_runtime_index;
811 save_node_runtime_index = nf->node_runtime_index;
812 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
813 vlib_next_frame_init (nf);
814 nf->node_runtime_index = save_node_runtime_index;
815 nf->flags = save_flags;
818 /* fork the frame dispatch queue */
819 nm_clone->pending_frames = 0;
820 vec_validate (nm_clone->pending_frames, 10);
821 _vec_len (nm_clone->pending_frames) = 0;
826 /* Allocate all nodes in single block for speed */
827 n = clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*n));
829 for (j = 0; j < vec_len (nm->nodes); j++)
831 clib_memcpy (n, nm->nodes[j], sizeof (*n));
832 /* none of the copied nodes have enqueue rights given out */
833 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
834 clib_memset (&n->stats_total, 0, sizeof (n->stats_total));
835 clib_memset (&n->stats_last_clear, 0,
836 sizeof (n->stats_last_clear));
837 vec_add1 (nm_clone->nodes, n);
840 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
841 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
842 CLIB_CACHE_LINE_BYTES);
844 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
846 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
847 rt->thread_index = vm_clone->thread_index;
848 /* copy initial runtime_data from node */
849 if (n->runtime_data && n->runtime_data_bytes > 0)
850 clib_memcpy (rt->runtime_data, n->runtime_data,
851 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
852 n->runtime_data_bytes));
855 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
856 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
857 CLIB_CACHE_LINE_BYTES);
858 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
860 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
861 rt->thread_index = vm_clone->thread_index;
862 /* copy initial runtime_data from node */
863 if (n->runtime_data && n->runtime_data_bytes > 0)
864 clib_memcpy (rt->runtime_data, n->runtime_data,
865 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
866 n->runtime_data_bytes));
869 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT] =
870 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT],
871 CLIB_CACHE_LINE_BYTES);
873 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT])
875 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
876 rt->thread_index = vm_clone->thread_index;
877 /* copy initial runtime_data from node */
878 if (n->runtime_data && n->runtime_data_bytes > 0)
879 clib_memcpy (rt->runtime_data, n->runtime_data,
880 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
881 n->runtime_data_bytes));
884 nm_clone->processes = vec_dup_aligned (nm->processes,
885 CLIB_CACHE_LINE_BYTES);
887 /* Create per-thread frame freelist */
888 nm_clone->frame_sizes = vec_new (vlib_frame_size_t, 1);
889 #ifdef VLIB_SUPPORTS_ARBITRARY_SCALAR_SIZES
890 nm_clone->frame_size_hash = hash_create (0, sizeof (uword));
892 nm_clone->node_by_error = nm->node_by_error;
894 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
896 clib_mem_set_heap (oldheap);
897 vec_add1_aligned (vlib_mains, vm_clone, CLIB_CACHE_LINE_BYTES);
899 /* Switch to the stats segment ... */
900 void *oldheap = vlib_stats_push_heap (0);
901 vm_clone->error_main.counters = vec_dup_aligned
902 (vlib_mains[0]->error_main.counters, CLIB_CACHE_LINE_BYTES);
903 vlib_stats_pop_heap2 (vm_clone->error_main.counters,
904 worker_thread_index, oldheap, 1);
906 vm_clone->error_main.counters_last_clear = vec_dup_aligned
907 (vlib_mains[0]->error_main.counters_last_clear,
908 CLIB_CACHE_LINE_BYTES);
910 worker_thread_index++;
916 /* only have non-data-structure copy threads to create... */
917 for (i = 0; i < vec_len (tm->registrations); i++)
919 tr = tm->registrations[i];
921 for (j = 0; j < tr->count; j++)
923 vec_add2 (vlib_worker_threads, w, 1);
927 create_mspace (tr->mheap_size, 0 /* locked */ );
930 w->thread_mheap = main_heap;
932 vlib_thread_stack_init (w - vlib_worker_threads);
933 w->thread_function = tr->function;
934 w->thread_function_arg = w;
937 (char *) format (0, "%s %d", tr->name, j + 1);
938 w->registration = tr;
939 vec_add1 (w->elog_track.name, 0);
940 elog_track_register (&vm->elog_main, &w->elog_track);
945 worker_thread_index = 1;
947 for (i = 0; i < vec_len (tm->registrations); i++)
952 tr = tm->registrations[i];
954 if (tr->use_pthreads || tm->use_pthreads)
956 for (j = 0; j < tr->count; j++)
958 w = vlib_worker_threads + worker_thread_index++;
959 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
962 clib_error_report (err);
969 clib_bitmap_foreach (c, tr->coremask, ({
970 w = vlib_worker_threads + worker_thread_index++;
971 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
974 clib_error_report (err);
979 vlib_worker_thread_barrier_sync (vm);
980 vlib_worker_thread_barrier_release (vm);
984 VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
988 worker_thread_node_runtime_update_internal (void)
992 vlib_node_main_t *nm, *nm_clone;
993 vlib_main_t *vm_clone;
994 vlib_node_runtime_t *rt;
996 vlib_node_runtime_sync_stats (vlib_main_t * vm,
997 vlib_node_runtime_t * r,
999 uword n_vectors, uword n_clocks);
1001 ASSERT (vlib_get_thread_index () == 0);
1004 nm = &vm->node_main;
1006 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
1009 * Scrape all runtime stats, so we don't lose node runtime(s) with
1010 * pending counts, or throw away worker / io thread counts.
1012 for (j = 0; j < vec_len (nm->nodes); j++)
1016 vlib_node_sync_stats (vm, n);
1019 for (i = 1; i < vec_len (vlib_mains); i++)
1023 vm_clone = vlib_mains[i];
1024 nm_clone = &vm_clone->node_main;
1026 for (j = 0; j < vec_len (nm_clone->nodes); j++)
1028 n = nm_clone->nodes[j];
1030 rt = vlib_node_get_runtime (vm_clone, n->index);
1031 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
1035 /* Per-worker clone rebuilds are now done on each thread */
1040 vlib_worker_thread_node_refork (void)
1042 vlib_main_t *vm, *vm_clone;
1043 vlib_node_main_t *nm, *nm_clone;
1044 vlib_node_t **old_nodes_clone;
1045 vlib_node_runtime_t *rt, *old_rt;
1047 vlib_node_t *new_n_clone;
1052 nm = &vm->node_main;
1053 vm_clone = vlib_get_main ();
1054 nm_clone = &vm_clone->node_main;
1056 /* Re-clone error heap */
1057 u64 *old_counters = vm_clone->error_main.counters;
1058 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
1060 clib_memcpy_fast (&vm_clone->error_main, &vm->error_main,
1061 sizeof (vm->error_main));
1062 j = vec_len (vm->error_main.counters) - 1;
1064 /* Switch to the stats segment ... */
1065 void *oldheap = vlib_stats_push_heap (0);
1066 vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES);
1067 vm_clone->error_main.counters = old_counters;
1068 vlib_stats_pop_heap2 (vm_clone->error_main.counters, vm_clone->thread_index,
1071 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
1072 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
1074 nm_clone = &vm_clone->node_main;
1075 vec_free (nm_clone->next_frames);
1076 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
1077 CLIB_CACHE_LINE_BYTES);
1079 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
1081 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
1082 u32 save_node_runtime_index;
1085 save_node_runtime_index = nf->node_runtime_index;
1086 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
1087 vlib_next_frame_init (nf);
1088 nf->node_runtime_index = save_node_runtime_index;
1089 nf->flags = save_flags;
1092 old_nodes_clone = nm_clone->nodes;
1093 nm_clone->nodes = 0;
1097 /* Allocate all nodes in single block for speed */
1099 clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*new_n_clone));
1100 for (j = 0; j < vec_len (nm->nodes); j++)
1102 vlib_node_t *new_n = nm->nodes[j];
1104 clib_memcpy_fast (new_n_clone, new_n, sizeof (*new_n));
1105 /* none of the copied nodes have enqueue rights given out */
1106 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
1108 if (j >= vec_len (old_nodes_clone))
1110 /* new node, set to zero */
1111 clib_memset (&new_n_clone->stats_total, 0,
1112 sizeof (new_n_clone->stats_total));
1113 clib_memset (&new_n_clone->stats_last_clear, 0,
1114 sizeof (new_n_clone->stats_last_clear));
1118 vlib_node_t *old_n_clone = old_nodes_clone[j];
1119 /* Copy stats if the old data is valid */
1120 clib_memcpy_fast (&new_n_clone->stats_total,
1121 &old_n_clone->stats_total,
1122 sizeof (new_n_clone->stats_total));
1123 clib_memcpy_fast (&new_n_clone->stats_last_clear,
1124 &old_n_clone->stats_last_clear,
1125 sizeof (new_n_clone->stats_last_clear));
1127 /* keep previous node state */
1128 new_n_clone->state = old_n_clone->state;
1130 vec_add1 (nm_clone->nodes, new_n_clone);
1133 /* Free the old node clones */
1134 clib_mem_free (old_nodes_clone[0]);
1136 vec_free (old_nodes_clone);
1139 /* re-clone internal nodes */
1140 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL];
1141 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
1142 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
1143 CLIB_CACHE_LINE_BYTES);
1145 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
1147 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1148 rt->thread_index = vm_clone->thread_index;
1149 /* copy runtime_data, will be overwritten later for existing rt */
1150 if (n->runtime_data && n->runtime_data_bytes > 0)
1151 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1152 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1153 n->runtime_data_bytes));
1156 for (j = 0; j < vec_len (old_rt); j++)
1158 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1159 rt->state = old_rt[j].state;
1160 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1161 VLIB_NODE_RUNTIME_DATA_SIZE);
1166 /* re-clone input nodes */
1167 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
1168 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
1169 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
1170 CLIB_CACHE_LINE_BYTES);
1172 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
1174 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1175 rt->thread_index = vm_clone->thread_index;
1176 /* copy runtime_data, will be overwritten later for existing rt */
1177 if (n->runtime_data && n->runtime_data_bytes > 0)
1178 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1179 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1180 n->runtime_data_bytes));
1183 for (j = 0; j < vec_len (old_rt); j++)
1185 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1186 rt->state = old_rt[j].state;
1187 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1188 VLIB_NODE_RUNTIME_DATA_SIZE);
1193 /* re-clone pre-input nodes */
1194 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT];
1195 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT] =
1196 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT],
1197 CLIB_CACHE_LINE_BYTES);
1199 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT])
1201 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1202 rt->thread_index = vm_clone->thread_index;
1203 /* copy runtime_data, will be overwritten later for existing rt */
1204 if (n->runtime_data && n->runtime_data_bytes > 0)
1205 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1206 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1207 n->runtime_data_bytes));
1210 for (j = 0; j < vec_len (old_rt); j++)
1212 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1213 rt->state = old_rt[j].state;
1214 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1215 VLIB_NODE_RUNTIME_DATA_SIZE);
1220 nm_clone->processes = vec_dup_aligned (nm->processes,
1221 CLIB_CACHE_LINE_BYTES);
1222 nm_clone->node_by_error = nm->node_by_error;
1226 vlib_worker_thread_node_runtime_update (void)
1229 * Make a note that we need to do a node runtime update
1230 * prior to releasing the barrier.
1232 vlib_global_main.need_vlib_worker_thread_node_runtime_update = 1;
1236 unformat_sched_policy (unformat_input_t * input, va_list * args)
1238 u32 *r = va_arg (*args, u32 *);
1241 #define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
1242 foreach_sched_policy
1249 static clib_error_t *
1250 cpu_config (vlib_main_t * vm, unformat_input_t * input)
1252 vlib_thread_registration_t *tr;
1254 vlib_thread_main_t *tm = &vlib_thread_main;
1259 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
1261 tm->n_thread_stacks = 1; /* account for main thread */
1262 tm->sched_policy = ~0;
1263 tm->sched_priority = ~0;
1264 tm->main_lcore = ~0;
1270 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
1274 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1276 if (unformat (input, "use-pthreads"))
1277 tm->use_pthreads = 1;
1278 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
1280 else if (unformat (input, "main-core %u", &tm->main_lcore))
1282 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
1284 else if (unformat (input, "numa-heap-size %U",
1285 unformat_memory_size, &tm->numa_heap_size))
1287 else if (unformat (input, "coremask-%s %U", &name,
1288 unformat_bitmap_mask, &bitmap) ||
1289 unformat (input, "corelist-%s %U", &name,
1290 unformat_bitmap_list, &bitmap))
1292 p = hash_get_mem (tm->thread_registrations_by_name, name);
1294 return clib_error_return (0, "no such thread type '%s'", name);
1296 tr = (vlib_thread_registration_t *) p[0];
1298 if (tr->use_pthreads)
1299 return clib_error_return (0,
1300 "corelist cannot be set for '%s' threads",
1303 tr->coremask = bitmap;
1304 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1308 (input, "scheduler-policy %U", unformat_sched_policy,
1311 else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
1313 else if (unformat (input, "%s %u", &name, &count))
1315 p = hash_get_mem (tm->thread_registrations_by_name, name);
1317 return clib_error_return (0, "no such thread type 3 '%s'", name);
1319 tr = (vlib_thread_registration_t *) p[0];
1320 if (tr->fixed_count)
1321 return clib_error_return
1322 (0, "number of %s threads not configurable", tr->name);
1329 if (tm->sched_priority != ~0)
1331 if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
1333 u32 prio_max = sched_get_priority_max (tm->sched_policy);
1334 u32 prio_min = sched_get_priority_min (tm->sched_policy);
1335 if (tm->sched_priority > prio_max)
1336 tm->sched_priority = prio_max;
1337 if (tm->sched_priority < prio_min)
1338 tm->sched_priority = prio_min;
1342 return clib_error_return
1344 "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1345 tm->sched_priority);
1350 if (!tm->thread_prefix)
1351 tm->thread_prefix = format (0, "vpp");
1355 tm->n_thread_stacks += tr->count;
1356 tm->n_pthreads += tr->count * tr->use_pthreads;
1357 tm->n_threads += tr->count * (tr->use_pthreads == 0);
1364 VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1366 void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
1368 vnet_main_fixup (vlib_fork_fixup_t which)
1373 vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1375 vlib_main_t *vm = vlib_get_main ();
1377 if (vlib_mains == 0)
1380 ASSERT (vlib_get_thread_index () == 0);
1381 vlib_worker_thread_barrier_sync (vm);
1385 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1386 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1392 vlib_worker_thread_barrier_release (vm);
1396 * Enforce minimum open time to minimize packet loss due to Rx overflow,
1397 * based on a test based heuristic that barrier should be open for at least
1398 * 3 time as long as it is closed (with an upper bound of 1ms because by that
1399 * point it is probably too late to make a difference)
1402 #ifndef BARRIER_MINIMUM_OPEN_LIMIT
1403 #define BARRIER_MINIMUM_OPEN_LIMIT 0.001
1406 #ifndef BARRIER_MINIMUM_OPEN_FACTOR
1407 #define BARRIER_MINIMUM_OPEN_FACTOR 3
1411 vlib_worker_thread_initial_barrier_sync_and_release (vlib_main_t * vm)
1414 f64 now = vlib_time_now (vm);
1415 u32 count = vec_len (vlib_mains) - 1;
1417 /* No worker threads? */
1421 deadline = now + BARRIER_SYNC_TIMEOUT;
1422 *vlib_worker_threads->wait_at_barrier = 1;
1423 while (*vlib_worker_threads->workers_at_barrier != count)
1425 if ((now = vlib_time_now (vm)) > deadline)
1427 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1432 *vlib_worker_threads->wait_at_barrier = 0;
1436 vlib_worker_thread_barrier_sync_int (vlib_main_t * vm, const char *func_name)
1443 f64 max_vector_rate;
1447 if (vec_len (vlib_mains) < 2)
1450 ASSERT (vlib_get_thread_index () == 0);
1452 vlib_worker_threads[0].barrier_caller = func_name;
1453 count = vec_len (vlib_mains) - 1;
1455 /* Record entry relative to last close */
1456 now = vlib_time_now (vm);
1457 t_entry = now - vm->barrier_epoch;
1459 /* Tolerate recursive calls */
1460 if (++vlib_worker_threads[0].recursion_level > 1)
1462 barrier_trace_sync_rec (t_entry);
1467 * Need data to decide if we're working hard enough to honor
1468 * the barrier hold-down timer.
1470 max_vector_rate = 0.0;
1471 for (i = 1; i < vec_len (vlib_mains); i++)
1473 clib_max (max_vector_rate,
1474 (f64) vlib_last_vectors_per_main_loop (vlib_mains[i]));
1476 vlib_worker_threads[0].barrier_sync_count++;
1478 /* Enforce minimum barrier open time to minimize packet loss */
1479 ASSERT (vm->barrier_no_close_before <= (now + BARRIER_MINIMUM_OPEN_LIMIT));
1482 * If any worker thread seems busy, which we define
1483 * as a vector rate above 10, we enforce the barrier hold-down timer
1485 if (max_vector_rate > 10.0)
1489 now = vlib_time_now (vm);
1490 /* Barrier hold-down timer expired? */
1491 if (now >= vm->barrier_no_close_before)
1493 if ((vm->barrier_no_close_before - now)
1494 > (2.0 * BARRIER_MINIMUM_OPEN_LIMIT))
1497 ("clock change: would have waited for %.4f seconds",
1498 (vm->barrier_no_close_before - now));
1503 /* Record time of closure */
1504 t_open = now - vm->barrier_epoch;
1505 vm->barrier_epoch = now;
1507 deadline = now + BARRIER_SYNC_TIMEOUT;
1509 *vlib_worker_threads->wait_at_barrier = 1;
1510 while (*vlib_worker_threads->workers_at_barrier != count)
1512 if ((now = vlib_time_now (vm)) > deadline)
1514 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1519 t_closed = now - vm->barrier_epoch;
1521 barrier_trace_sync (t_entry, t_open, t_closed);
1526 vlib_worker_thread_barrier_release (vlib_main_t * vm)
1533 f64 t_update_main = 0.0;
1534 int refork_needed = 0;
1536 if (vec_len (vlib_mains) < 2)
1539 ASSERT (vlib_get_thread_index () == 0);
1542 now = vlib_time_now (vm);
1543 t_entry = now - vm->barrier_epoch;
1545 if (--vlib_worker_threads[0].recursion_level > 0)
1547 barrier_trace_release_rec (t_entry);
1551 /* Update (all) node runtimes before releasing the barrier, if needed */
1552 if (vm->need_vlib_worker_thread_node_runtime_update)
1555 * Lock stat segment here, so we's safe when
1556 * rebuilding the stat segment node clones from the
1559 vlib_stat_segment_lock ();
1561 /* Do stats elements on main thread */
1562 worker_thread_node_runtime_update_internal ();
1563 vm->need_vlib_worker_thread_node_runtime_update = 0;
1565 /* Do per thread rebuilds in parallel */
1567 clib_atomic_fetch_add (vlib_worker_threads->node_reforks_required,
1568 (vec_len (vlib_mains) - 1));
1569 now = vlib_time_now (vm);
1570 t_update_main = now - vm->barrier_epoch;
1573 deadline = now + BARRIER_SYNC_TIMEOUT;
1576 * Note when we let go of the barrier.
1577 * Workers can use this to derive a reasonably accurate
1578 * time offset. See vlib_time_now(...)
1580 vm->time_last_barrier_release = vlib_time_now (vm);
1581 CLIB_MEMORY_STORE_BARRIER ();
1583 *vlib_worker_threads->wait_at_barrier = 0;
1585 while (*vlib_worker_threads->workers_at_barrier > 0)
1587 if ((now = vlib_time_now (vm)) > deadline)
1589 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1594 /* Wait for reforks before continuing */
1597 now = vlib_time_now (vm);
1599 deadline = now + BARRIER_SYNC_TIMEOUT;
1601 while (*vlib_worker_threads->node_reforks_required > 0)
1603 if ((now = vlib_time_now (vm)) > deadline)
1605 fformat (stderr, "%s: worker thread refork deadlock\n",
1610 vlib_stat_segment_unlock ();
1613 t_closed_total = now - vm->barrier_epoch;
1615 minimum_open = t_closed_total * BARRIER_MINIMUM_OPEN_FACTOR;
1617 if (minimum_open > BARRIER_MINIMUM_OPEN_LIMIT)
1619 minimum_open = BARRIER_MINIMUM_OPEN_LIMIT;
1622 vm->barrier_no_close_before = now + minimum_open;
1624 /* Record barrier epoch (used to enforce minimum open time) */
1625 vm->barrier_epoch = now;
1627 barrier_trace_release (t_entry, t_closed_total, t_update_main);
1632 * Check the frame queue to see if any frames are available.
1633 * If so, pull the packets off the frames and put them to
1637 vlib_frame_queue_dequeue (vlib_main_t * vm, vlib_frame_queue_main_t * fqm)
1639 u32 thread_id = vm->thread_index;
1640 vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id];
1641 vlib_frame_queue_elt_t *elt;
1650 ASSERT (vm == vlib_mains[thread_id]);
1652 if (PREDICT_FALSE (fqm->node_index == ~0))
1655 * Gather trace data for frame queues
1657 if (PREDICT_FALSE (fq->trace))
1659 frame_queue_trace_t *fqt;
1660 frame_queue_nelt_counter_t *fqh;
1663 fqt = &fqm->frame_queue_traces[thread_id];
1665 fqt->nelts = fq->nelts;
1666 fqt->head = fq->head;
1667 fqt->head_hint = fq->head_hint;
1668 fqt->tail = fq->tail;
1669 fqt->threshold = fq->vector_threshold;
1670 fqt->n_in_use = fqt->tail - fqt->head;
1671 if (fqt->n_in_use >= fqt->nelts)
1673 // if beyond max then use max
1674 fqt->n_in_use = fqt->nelts - 1;
1677 /* Record the number of elements in use in the histogram */
1678 fqh = &fqm->frame_queue_histogram[thread_id];
1679 fqh->count[fqt->n_in_use]++;
1681 /* Record a snapshot of the elements in use */
1682 for (elix = 0; elix < fqt->nelts; elix++)
1684 elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1685 if (1 || elt->valid)
1687 fqt->n_vectors[elix] = elt->n_vectors;
1696 if (fq->head == fq->tail)
1698 fq->head_hint = fq->head;
1702 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
1706 fq->head_hint = fq->head;
1710 from = elt->buffer_index;
1711 msg_type = elt->msg_type;
1713 ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
1714 ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1716 f = vlib_get_frame_to_node (vm, fqm->node_index);
1718 /* If the first vector is traced, set the frame trace flag */
1719 b = vlib_get_buffer (vm, from[0]);
1720 if (b->flags & VLIB_BUFFER_IS_TRACED)
1721 f->frame_flags |= VLIB_NODE_FLAG_TRACE;
1723 to = vlib_frame_vector_args (f);
1725 n_left_to_node = elt->n_vectors;
1727 while (n_left_to_node >= 4)
1735 n_left_to_node -= 4;
1738 while (n_left_to_node > 0)
1746 vectors += elt->n_vectors;
1747 f->n_vectors = elt->n_vectors;
1748 vlib_put_frame_to_node (vm, fqm->node_index, f);
1752 elt->msg_type = 0xfefefefe;
1753 CLIB_MEMORY_BARRIER ();
1758 * Limit the number of packets pushed into the graph
1760 if (vectors >= fq->vector_threshold)
1762 fq->head_hint = fq->head;
1771 vlib_worker_thread_fn (void *arg)
1773 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
1774 vlib_thread_main_t *tm = vlib_get_thread_main ();
1775 vlib_main_t *vm = vlib_get_main ();
1778 ASSERT (vm->thread_index == vlib_get_thread_index ());
1780 vlib_worker_thread_init (w);
1781 clib_time_init (&vm->clib_time);
1782 clib_mem_set_heap (w->thread_mheap);
1784 e = vlib_call_init_exit_functions_no_sort
1785 (vm, &vm->worker_init_function_registrations, 1 /* call_once */ );
1787 clib_error_report (e);
1789 /* Wait until the dpdk init sequence is complete */
1790 while (tm->extern_thread_mgmt && tm->worker_thread_release == 0)
1791 vlib_worker_thread_barrier_check ();
1793 vlib_worker_loop (vm);
1797 VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1800 .function = vlib_worker_thread_fn,
1805 vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1807 vlib_thread_main_t *tm = vlib_get_thread_main ();
1808 vlib_frame_queue_main_t *fqm;
1809 vlib_frame_queue_t *fq;
1812 if (frame_queue_nelts == 0)
1813 frame_queue_nelts = FRAME_QUEUE_MAX_NELTS;
1815 ASSERT (frame_queue_nelts >= 8);
1817 vec_add2 (tm->frame_queue_mains, fqm, 1);
1819 fqm->node_index = node_index;
1820 fqm->frame_queue_nelts = frame_queue_nelts;
1821 fqm->queue_hi_thresh = frame_queue_nelts - 2;
1823 vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1);
1824 vec_validate (fqm->per_thread_data, tm->n_vlib_mains - 1);
1825 _vec_len (fqm->vlib_frame_queues) = 0;
1826 for (i = 0; i < tm->n_vlib_mains; i++)
1828 vlib_frame_queue_per_thread_data_t *ptd;
1829 fq = vlib_frame_queue_alloc (frame_queue_nelts);
1830 vec_add1 (fqm->vlib_frame_queues, fq);
1832 ptd = vec_elt_at_index (fqm->per_thread_data, i);
1833 vec_validate (ptd->handoff_queue_elt_by_thread_index,
1834 tm->n_vlib_mains - 1);
1835 vec_validate_init_empty (ptd->congested_handoff_queue_by_thread_index,
1836 tm->n_vlib_mains - 1,
1837 (vlib_frame_queue_t *) (~0));
1840 return (fqm - tm->frame_queue_mains);
1844 vlib_thread_cb_register (struct vlib_main_t *vm, vlib_thread_callbacks_t * cb)
1846 vlib_thread_main_t *tm = vlib_get_thread_main ();
1848 if (tm->extern_thread_mgmt)
1851 tm->cb.vlib_launch_thread_cb = cb->vlib_launch_thread_cb;
1852 tm->extern_thread_mgmt = 1;
1857 vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t *
1860 ASSERT (vlib_get_thread_index () == 0);
1861 vlib_process_signal_event (vlib_get_main (), args->node_index,
1862 args->type_opaque, args->data);
1865 void *rpc_call_main_thread_cb_fn;
1868 vlib_rpc_call_main_thread (void *callback, u8 * args, u32 arg_size)
1870 if (rpc_call_main_thread_cb_fn)
1872 void (*fp) (void *, u8 *, u32) = rpc_call_main_thread_cb_fn;
1873 (*fp) (callback, args, arg_size);
1876 clib_warning ("BUG: rpc_call_main_thread_cb_fn NULL!");
1880 threads_init (vlib_main_t * vm)
1885 VLIB_INIT_FUNCTION (threads_init);
1888 static clib_error_t *
1889 show_clock_command_fn (vlib_main_t * vm,
1890 unformat_input_t * input, vlib_cli_command_t * cmd)
1894 clib_timebase_t _tb, *tb = &_tb;
1896 (void) unformat (input, "verbose %=", &verbose, 1);
1898 clib_timebase_init (tb, 0 /* GMT */ , CLIB_TIMEBASE_DAYLIGHT_NONE,
1901 vlib_cli_output (vm, "%U, %U GMT", format_clib_time, &vm->clib_time,
1902 verbose, format_clib_timebase_time,
1903 clib_timebase_now (tb));
1905 if (vec_len (vlib_mains) == 1)
1908 vlib_cli_output (vm, "Time last barrier release %.9f",
1909 vm->time_last_barrier_release);
1911 for (i = 1; i < vec_len (vlib_mains); i++)
1913 if (vlib_mains[i] == 0)
1916 vlib_cli_output (vm, "%d: %U", i, format_clib_time,
1917 &vlib_mains[i]->clib_time, verbose);
1919 vlib_cli_output (vm, "Thread %d offset %.9f error %.9f", i,
1920 vlib_mains[i]->time_offset,
1921 vm->time_last_barrier_release -
1922 vlib_mains[i]->time_last_barrier_release);
1928 VLIB_CLI_COMMAND (f_command, static) =
1930 .path = "show clock",
1931 .short_help = "show clock",
1932 .function = show_clock_command_fn,
1937 * fd.io coding-style-patch-verification: ON
1940 * eval: (c-set-style "gnu")