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 DECLARE_CJ_GLOBAL_LOG;
28 #define FRAME_QUEUE_NELTS 64
36 vlib_worker_thread_t *vlib_worker_threads;
37 vlib_thread_main_t vlib_thread_main;
40 * Barrier tracing can be enabled on a normal build to collect information
41 * on barrier use, including timings and call stacks. Deliberately not
42 * keyed off CLIB_DEBUG, because that can add significant overhead which
43 * imapacts observed timings.
47 elog_global_id_for_msg_name (const char *msg_name)
54 h = hash_create_string (0, sizeof (uword));
56 p = hash_get_mem (h, msg_name);
59 r = elog_string (&vlib_global_main.elog_main, "%s", msg_name);
61 name_copy = format (0, "%s%c", msg_name, 0);
63 hash_set_mem (h, name_copy, r);
69 barrier_trace_sync (f64 t_entry, f64 t_open, f64 t_closed)
71 if (!vlib_worker_threads->barrier_elog_enabled)
75 ELOG_TYPE_DECLARE (e) =
77 .format = "bar-trace-%s-#%d",
78 .format_args = "T4i4",
83 u32 caller, count, t_entry, t_open, t_closed;
86 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
87 ed->count = (int) vlib_worker_threads[0].barrier_sync_count;
88 ed->caller = elog_global_id_for_msg_name
89 (vlib_worker_threads[0].barrier_caller);
90 ed->t_entry = (int) (1000000.0 * t_entry);
91 ed->t_open = (int) (1000000.0 * t_open);
92 ed->t_closed = (int) (1000000.0 * t_closed);
96 barrier_trace_sync_rec (f64 t_entry)
98 if (!vlib_worker_threads->barrier_elog_enabled)
102 ELOG_TYPE_DECLARE (e) =
104 .format = "bar-syncrec-%s-#%d",
105 .format_args = "T4i4",
113 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
114 ed->depth = (int) vlib_worker_threads[0].recursion_level - 1;
115 ed->caller = elog_global_id_for_msg_name
116 (vlib_worker_threads[0].barrier_caller);
120 barrier_trace_release_rec (f64 t_entry)
122 if (!vlib_worker_threads->barrier_elog_enabled)
126 ELOG_TYPE_DECLARE (e) =
128 .format = "bar-relrrec-#%d",
137 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
138 ed->depth = (int) vlib_worker_threads[0].recursion_level;
142 barrier_trace_release (f64 t_entry, f64 t_closed_total, f64 t_update_main)
144 if (!vlib_worker_threads->barrier_elog_enabled)
148 ELOG_TYPE_DECLARE (e) =
150 .format = "bar-rel-#%d-e%d-u%d-t%d",
151 .format_args = "i4i4i4i4",
156 u32 count, t_entry, t_update_main, t_closed_total;
159 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
160 ed->t_entry = (int) (1000000.0 * t_entry);
161 ed->t_update_main = (int) (1000000.0 * t_update_main);
162 ed->t_closed_total = (int) (1000000.0 * t_closed_total);
163 ed->count = (int) vlib_worker_threads[0].barrier_sync_count;
165 /* Reset context for next trace */
166 vlib_worker_threads[0].barrier_context = NULL;
170 os_get_nthreads (void)
172 return vec_len (vlib_thread_stacks);
176 vlib_set_thread_name (char *name)
178 int pthread_setname_np (pthread_t __target_thread, const char *__name);
180 pthread_t thread = pthread_self ();
184 rv = pthread_setname_np (thread, name);
186 clib_warning ("pthread_setname_np returned %d", rv);
191 sort_registrations_by_no_clone (void *a0, void *a1)
193 vlib_thread_registration_t **tr0 = a0;
194 vlib_thread_registration_t **tr1 = a1;
196 return ((i32) ((*tr0)->no_data_structure_clone)
197 - ((i32) ((*tr1)->no_data_structure_clone)));
201 clib_sysfs_list_to_bitmap (char *filename)
206 fp = fopen (filename, "r");
211 vec_validate (buffer, 256 - 1);
212 if (fgets ((char *) buffer, 256, fp))
215 unformat_init_string (&in, (char *) buffer,
216 strlen ((char *) buffer));
217 if (unformat (&in, "%U", unformat_bitmap_list, &r) != 1)
218 clib_warning ("unformat_bitmap_list failed");
228 /* Called early in the init sequence */
231 vlib_thread_init (vlib_main_t * vm)
233 vlib_thread_main_t *tm = &vlib_thread_main;
234 vlib_worker_thread_t *w;
235 vlib_thread_registration_t *tr;
236 u32 n_vlib_mains = 1;
241 /* get bitmaps of active cpu cores and sockets */
242 tm->cpu_core_bitmap =
243 clib_sysfs_list_to_bitmap ("/sys/devices/system/cpu/online");
244 tm->cpu_socket_bitmap =
245 clib_sysfs_list_to_bitmap ("/sys/devices/system/node/online");
247 avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
250 for (i = 0; i < tm->skip_cores; i++)
252 uword c = clib_bitmap_first_set (avail_cpu);
254 return clib_error_return (0, "no available cpus to skip");
256 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
259 /* grab cpu for main thread */
260 if (tm->main_lcore == ~0)
262 /* if main-lcore is not set, we try to use lcore 1 */
263 if (clib_bitmap_get (avail_cpu, 1))
266 tm->main_lcore = clib_bitmap_first_set (avail_cpu);
267 if (tm->main_lcore == (u8) ~ 0)
268 return clib_error_return (0, "no available cpus to be used for the"
273 if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
274 return clib_error_return (0, "cpu %u is not available to be used"
275 " for the main thread", tm->main_lcore);
277 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
279 /* assume that there is socket 0 only if there is no data from sysfs */
280 if (!tm->cpu_socket_bitmap)
281 tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
283 /* pin main thread to main_lcore */
284 if (tm->cb.vlib_thread_set_lcore_cb)
286 tm->cb.vlib_thread_set_lcore_cb (0, tm->main_lcore);
292 CPU_SET (tm->main_lcore, &cpuset);
293 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
296 /* Set up thread 0 */
297 vec_validate_aligned (vlib_worker_threads, 0, CLIB_CACHE_LINE_BYTES);
298 _vec_len (vlib_worker_threads) = 1;
299 w = vlib_worker_threads;
300 w->thread_mheap = clib_mem_get_heap ();
301 w->thread_stack = vlib_thread_stacks[0];
302 w->cpu_id = tm->main_lcore;
303 w->lwp = syscall (SYS_gettid);
304 w->thread_id = pthread_self ();
305 tm->n_vlib_mains = 1;
307 if (tm->sched_policy != ~0)
309 struct sched_param sched_param;
310 if (!sched_getparam (w->lwp, &sched_param))
312 if (tm->sched_priority != ~0)
313 sched_param.sched_priority = tm->sched_priority;
314 sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
318 /* assign threads to cores and set n_vlib_mains */
323 vec_add1 (tm->registrations, tr);
327 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
329 for (i = 0; i < vec_len (tm->registrations); i++)
332 tr = tm->registrations[i];
333 tr->first_index = first_index;
334 first_index += tr->count;
335 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
337 /* construct coremask */
338 if (tr->use_pthreads || !tr->count)
345 clib_bitmap_foreach (c, tr->coremask, ({
346 if (clib_bitmap_get(avail_cpu, c) == 0)
347 return clib_error_return (0, "cpu %u is not available to be used"
348 " for the '%s' thread",c, tr->name);
350 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
356 for (j = 0; j < tr->count; j++)
358 uword c = clib_bitmap_first_set (avail_cpu);
360 return clib_error_return (0,
361 "no available cpus to be used for"
362 " the '%s' thread", tr->name);
364 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
365 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
370 clib_bitmap_free (avail_cpu);
372 tm->n_vlib_mains = n_vlib_mains;
375 * Allocate the remaining worker threads, and thread stack vector slots
376 * from now on, calls to os_get_nthreads() will return the correct
379 vec_validate_aligned (vlib_worker_threads, first_index - 1,
380 CLIB_CACHE_LINE_BYTES);
381 vec_validate (vlib_thread_stacks, vec_len (vlib_worker_threads) - 1);
386 vlib_frame_queue_alloc (int nelts)
388 vlib_frame_queue_t *fq;
390 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
391 clib_memset (fq, 0, sizeof (*fq));
393 fq->vector_threshold = 128; // packets
394 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
398 if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
399 fformat (stderr, "WARNING: fq->tail unaligned\n");
400 if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
401 fformat (stderr, "WARNING: fq->head unaligned\n");
402 if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
403 fformat (stderr, "WARNING: fq->elts unaligned\n");
405 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
406 fformat (stderr, "WARNING: fq->elts[0] size %d\n",
407 sizeof (fq->elts[0]));
408 if (nelts & (nelts - 1))
410 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
418 void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
420 vl_msg_api_handler_no_free (void *v)
424 /* Turned off, save as reference material... */
427 vlib_frame_queue_dequeue_internal (int thread_id,
428 vlib_main_t * vm, vlib_node_main_t * nm)
430 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
431 vlib_frame_queue_elt_t *elt;
433 vlib_pending_frame_t *p;
434 vlib_node_runtime_t *r;
435 u32 node_runtime_index;
440 ASSERT (vm == vlib_mains[thread_id]);
444 if (fq->head == fq->tail)
447 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
452 before = clib_cpu_time_now ();
455 node_runtime_index = elt->node_runtime_index;
456 msg_type = elt->msg_type;
460 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
461 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
462 /* note fallthrough... */
463 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
464 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
466 vlib_frame_free (vm, r, f);
468 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
469 vec_add2 (vm->node_main.pending_frames, p, 1);
470 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
471 p->node_runtime_index = elt->node_runtime_index;
472 p->frame_index = vlib_frame_index (vm, f);
473 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
474 fq->dequeue_vectors += (u64) f->n_vectors;
476 case VLIB_FRAME_QUEUE_ELT_API_MSG:
477 vl_msg_api_handler_no_free (f);
480 clib_warning ("bogus frame queue message, type %d", msg_type);
485 fq->dequeue_ticks += clib_cpu_time_now () - before;
486 CLIB_MEMORY_BARRIER ();
495 vlib_frame_queue_dequeue (int thread_id,
496 vlib_main_t * vm, vlib_node_main_t * nm)
498 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
502 vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
503 u32 frame_queue_index, vlib_frame_t * frame,
504 vlib_frame_queue_msg_type_t type)
506 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
507 vlib_frame_queue_elt_t *elt;
510 u64 before = clib_cpu_time_now ();
514 new_tail = clib_atomic_add_fetch (&fq->tail, 1);
516 /* Wait until a ring slot is available */
517 while (new_tail >= fq->head + fq->nelts)
519 f64 b4 = vlib_time_now_ticks (vm, before);
520 vlib_worker_thread_barrier_check (vm, b4);
521 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
522 // vlib_frame_queue_dequeue (vm->thread_index, vm, nm);
525 elt = fq->elts + (new_tail & (fq->nelts - 1));
527 /* this would be very bad... */
532 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
533 save_count = frame->n_vectors;
536 elt->node_runtime_index = node_runtime_index;
537 elt->msg_type = type;
538 CLIB_MEMORY_BARRIER ();
545 /* To be called by vlib worker threads upon startup */
547 vlib_worker_thread_init (vlib_worker_thread_t * w)
549 vlib_thread_main_t *tm = vlib_get_thread_main ();
552 * Note: disabling signals in worker threads as follows
553 * prevents the api post-mortem dump scheme from working
557 * pthread_sigmask (SIG_SETMASK, &s, 0);
561 clib_mem_set_heap (w->thread_mheap);
563 if (vec_len (tm->thread_prefix) && w->registration->short_name)
565 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
566 w->registration->short_name, w->instance_id, '\0');
567 vlib_set_thread_name ((char *) w->name);
570 if (!w->registration->use_pthreads)
573 /* Initial barrier sync, for both worker and i/o threads */
574 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, 1);
576 while (*vlib_worker_threads->wait_at_barrier)
579 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, -1);
584 vlib_worker_thread_bootstrap_fn (void *arg)
587 vlib_worker_thread_t *w = arg;
589 w->lwp = syscall (SYS_gettid);
590 w->thread_id = pthread_self ();
592 __os_thread_index = w - vlib_worker_threads;
594 rv = (void *) clib_calljmp
595 ((uword (*)(uword)) w->thread_function,
596 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
597 /* NOTREACHED, we hope */
602 vlib_get_thread_core_socket (vlib_worker_thread_t * w, unsigned cpu_id)
604 const char *sys_cpu_path = "/sys/devices/system/cpu/cpu";
606 int core_id = -1, socket_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);
612 format (p, "%s%u/topology/physical_package_id%c", sys_cpu_path, cpu_id,
614 clib_sysfs_read ((char *) p, "%d", &socket_id);
617 w->core_id = core_id;
618 w->socket_id = socket_id;
621 static clib_error_t *
622 vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned cpu_id)
624 vlib_thread_main_t *tm = &vlib_thread_main;
625 void *(*fp_arg) (void *) = fp;
628 vlib_get_thread_core_socket (w, cpu_id);
629 if (tm->cb.vlib_launch_thread_cb && !w->registration->use_pthreads)
630 return tm->cb.vlib_launch_thread_cb (fp, (void *) w, cpu_id);
636 CPU_SET (cpu_id, &cpuset);
638 if (pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w))
639 return clib_error_return_unix (0, "pthread_create");
641 if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
642 return clib_error_return_unix (0, "pthread_setaffinity_np");
648 static clib_error_t *
649 start_workers (vlib_main_t * vm)
652 vlib_worker_thread_t *w;
653 vlib_main_t *vm_clone;
655 vlib_thread_main_t *tm = &vlib_thread_main;
656 vlib_thread_registration_t *tr;
657 vlib_node_runtime_t *rt;
658 u32 n_vlib_mains = tm->n_vlib_mains;
659 u32 worker_thread_index;
660 u8 *main_heap = clib_mem_get_per_cpu_heap ();
662 vec_reset_length (vlib_worker_threads);
664 /* Set up the main thread */
665 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
666 w->elog_track.name = "main thread";
667 elog_track_register (&vm->elog_main, &w->elog_track);
669 if (vec_len (tm->thread_prefix))
671 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
672 vlib_set_thread_name ((char *) w->name);
676 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
677 vm->elog_main.lock[0] = 0;
679 if (n_vlib_mains > 1)
681 /* Replace hand-crafted length-1 vector with a real vector */
684 vec_validate_aligned (vlib_mains, tm->n_vlib_mains - 1,
685 CLIB_CACHE_LINE_BYTES);
686 _vec_len (vlib_mains) = 0;
687 vec_add1_aligned (vlib_mains, vm, CLIB_CACHE_LINE_BYTES);
689 vlib_worker_threads->wait_at_barrier =
690 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
691 vlib_worker_threads->workers_at_barrier =
692 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
694 vlib_worker_threads->node_reforks_required =
695 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
697 /* We'll need the rpc vector lock... */
698 clib_spinlock_init (&vm->pending_rpc_lock);
700 /* Ask for an initial barrier sync */
701 *vlib_worker_threads->workers_at_barrier = 0;
702 *vlib_worker_threads->wait_at_barrier = 1;
704 /* Without update or refork */
705 *vlib_worker_threads->node_reforks_required = 0;
706 vm->need_vlib_worker_thread_node_runtime_update = 0;
709 vm->barrier_epoch = 0;
710 vm->barrier_no_close_before = 0;
712 worker_thread_index = 1;
714 for (i = 0; i < vec_len (tm->registrations); i++)
716 vlib_node_main_t *nm, *nm_clone;
719 tr = tm->registrations[i];
724 for (k = 0; k < tr->count; k++)
728 vec_add2 (vlib_worker_threads, w, 1);
729 /* Currently unused, may not really work */
732 #if USE_DLMALLOC == 0
734 mheap_alloc (0 /* use VM */ , tr->mheap_size);
736 w->thread_mheap = create_mspace (tr->mheap_size,
741 w->thread_mheap = main_heap;
744 vlib_thread_stack_init (w - vlib_worker_threads);
745 w->thread_function = tr->function;
746 w->thread_function_arg = w;
748 w->registration = tr;
751 (char *) format (0, "%s %d", tr->name, k + 1);
752 vec_add1 (w->elog_track.name, 0);
753 elog_track_register (&vm->elog_main, &w->elog_track);
755 if (tr->no_data_structure_clone)
758 /* Fork vlib_global_main et al. Look for bugs here */
759 oldheap = clib_mem_set_heap (w->thread_mheap);
761 vm_clone = clib_mem_alloc_aligned (sizeof (*vm_clone),
762 CLIB_CACHE_LINE_BYTES);
763 clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
765 vm_clone->thread_index = worker_thread_index;
766 vm_clone->heap_base = w->thread_mheap;
767 vm_clone->heap_aligned_base = (void *)
768 (((uword) w->thread_mheap) & ~(VLIB_FRAME_ALIGN - 1));
769 vm_clone->init_functions_called =
770 hash_create (0, /* value bytes */ 0);
771 vm_clone->pending_rpc_requests = 0;
772 vec_validate (vm_clone->pending_rpc_requests, 0);
773 _vec_len (vm_clone->pending_rpc_requests) = 0;
774 clib_memset (&vm_clone->random_buffer, 0,
775 sizeof (vm_clone->random_buffer));
777 nm = &vlib_mains[0]->node_main;
778 nm_clone = &vm_clone->node_main;
779 /* fork next frames array, preserving node runtime indices */
780 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
781 CLIB_CACHE_LINE_BYTES);
782 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
784 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
785 u32 save_node_runtime_index;
788 save_node_runtime_index = nf->node_runtime_index;
789 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
790 vlib_next_frame_init (nf);
791 nf->node_runtime_index = save_node_runtime_index;
792 nf->flags = save_flags;
795 /* fork the frame dispatch queue */
796 nm_clone->pending_frames = 0;
797 vec_validate (nm_clone->pending_frames, 10);
798 _vec_len (nm_clone->pending_frames) = 0;
803 /* Allocate all nodes in single block for speed */
804 n = clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*n));
806 for (j = 0; j < vec_len (nm->nodes); j++)
808 clib_memcpy (n, nm->nodes[j], sizeof (*n));
809 /* none of the copied nodes have enqueue rights given out */
810 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
811 clib_memset (&n->stats_total, 0, sizeof (n->stats_total));
812 clib_memset (&n->stats_last_clear, 0,
813 sizeof (n->stats_last_clear));
814 vec_add1 (nm_clone->nodes, n);
817 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
818 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
819 CLIB_CACHE_LINE_BYTES);
821 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
823 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
824 rt->thread_index = vm_clone->thread_index;
825 /* copy initial runtime_data from node */
826 if (n->runtime_data && n->runtime_data_bytes > 0)
827 clib_memcpy (rt->runtime_data, n->runtime_data,
828 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
829 n->runtime_data_bytes));
832 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
833 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
834 CLIB_CACHE_LINE_BYTES);
835 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
837 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
838 rt->thread_index = vm_clone->thread_index;
839 /* copy initial runtime_data from node */
840 if (n->runtime_data && n->runtime_data_bytes > 0)
841 clib_memcpy (rt->runtime_data, n->runtime_data,
842 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
843 n->runtime_data_bytes));
846 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT] =
847 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT],
848 CLIB_CACHE_LINE_BYTES);
850 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT])
852 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
853 rt->thread_index = vm_clone->thread_index;
854 /* copy initial runtime_data from node */
855 if (n->runtime_data && n->runtime_data_bytes > 0)
856 clib_memcpy (rt->runtime_data, n->runtime_data,
857 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
858 n->runtime_data_bytes));
861 nm_clone->processes = vec_dup_aligned (nm->processes,
862 CLIB_CACHE_LINE_BYTES);
864 /* Create per-thread frame freelist */
865 nm_clone->frame_sizes = vec_new (vlib_frame_size_t, 1);
866 #ifdef VLIB_SUPPORTS_ARBITRARY_SCALAR_SIZES
867 nm_clone->frame_size_hash = hash_create (0, sizeof (uword));
870 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
872 clib_mem_set_heap (oldheap);
873 vec_add1_aligned (vlib_mains, vm_clone, CLIB_CACHE_LINE_BYTES);
875 vm_clone->error_main.counters = vec_dup_aligned
876 (vlib_mains[0]->error_main.counters, CLIB_CACHE_LINE_BYTES);
877 vm_clone->error_main.counters_last_clear = vec_dup_aligned
878 (vlib_mains[0]->error_main.counters_last_clear,
879 CLIB_CACHE_LINE_BYTES);
881 worker_thread_index++;
887 /* only have non-data-structure copy threads to create... */
888 for (i = 0; i < vec_len (tm->registrations); i++)
890 tr = tm->registrations[i];
892 for (j = 0; j < tr->count; j++)
894 vec_add2 (vlib_worker_threads, w, 1);
897 #if USE_DLMALLOC == 0
899 mheap_alloc (0 /* use VM */ , tr->mheap_size);
902 create_mspace (tr->mheap_size, 0 /* locked */ );
906 w->thread_mheap = main_heap;
908 vlib_thread_stack_init (w - vlib_worker_threads);
909 w->thread_function = tr->function;
910 w->thread_function_arg = w;
913 (char *) format (0, "%s %d", tr->name, j + 1);
914 w->registration = tr;
915 vec_add1 (w->elog_track.name, 0);
916 elog_track_register (&vm->elog_main, &w->elog_track);
921 worker_thread_index = 1;
923 for (i = 0; i < vec_len (tm->registrations); i++)
928 tr = tm->registrations[i];
930 if (tr->use_pthreads || tm->use_pthreads)
932 for (j = 0; j < tr->count; j++)
934 w = vlib_worker_threads + worker_thread_index++;
935 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
938 clib_error_report (err);
945 clib_bitmap_foreach (c, tr->coremask, ({
946 w = vlib_worker_threads + worker_thread_index++;
947 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
950 clib_error_report (err);
955 vlib_worker_thread_barrier_sync (vm);
956 vlib_worker_thread_barrier_release (vm);
960 VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
964 worker_thread_node_runtime_update_internal (void)
968 vlib_node_main_t *nm, *nm_clone;
969 vlib_main_t *vm_clone;
970 vlib_node_runtime_t *rt;
972 vlib_node_runtime_sync_stats (vlib_main_t * vm,
973 vlib_node_runtime_t * r,
975 uword n_vectors, uword n_clocks);
977 ASSERT (vlib_get_thread_index () == 0);
982 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
985 * Scrape all runtime stats, so we don't lose node runtime(s) with
986 * pending counts, or throw away worker / io thread counts.
988 for (j = 0; j < vec_len (nm->nodes); j++)
992 vlib_node_sync_stats (vm, n);
995 for (i = 1; i < vec_len (vlib_mains); i++)
999 vm_clone = vlib_mains[i];
1000 nm_clone = &vm_clone->node_main;
1002 for (j = 0; j < vec_len (nm_clone->nodes); j++)
1004 n = nm_clone->nodes[j];
1006 rt = vlib_node_get_runtime (vm_clone, n->index);
1007 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
1011 /* Per-worker clone rebuilds are now done on each thread */
1016 vlib_worker_thread_node_refork (void)
1018 vlib_main_t *vm, *vm_clone;
1019 vlib_node_main_t *nm, *nm_clone;
1020 vlib_node_t **old_nodes_clone;
1021 vlib_node_runtime_t *rt, *old_rt;
1023 vlib_node_t *new_n_clone;
1028 nm = &vm->node_main;
1029 vm_clone = vlib_get_main ();
1030 nm_clone = &vm_clone->node_main;
1032 /* Re-clone error heap */
1033 u64 *old_counters = vm_clone->error_main.counters;
1034 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
1036 clib_memcpy_fast (&vm_clone->error_main, &vm->error_main,
1037 sizeof (vm->error_main));
1038 j = vec_len (vm->error_main.counters) - 1;
1039 vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES);
1040 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
1041 vm_clone->error_main.counters = old_counters;
1042 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
1044 nm_clone = &vm_clone->node_main;
1045 vec_free (nm_clone->next_frames);
1046 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
1047 CLIB_CACHE_LINE_BYTES);
1049 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
1051 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
1052 u32 save_node_runtime_index;
1055 save_node_runtime_index = nf->node_runtime_index;
1056 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
1057 vlib_next_frame_init (nf);
1058 nf->node_runtime_index = save_node_runtime_index;
1059 nf->flags = save_flags;
1062 old_nodes_clone = nm_clone->nodes;
1063 nm_clone->nodes = 0;
1067 /* Allocate all nodes in single block for speed */
1069 clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*new_n_clone));
1070 for (j = 0; j < vec_len (nm->nodes); j++)
1072 vlib_node_t *old_n_clone;
1075 new_n = nm->nodes[j];
1076 old_n_clone = old_nodes_clone[j];
1078 clib_memcpy_fast (new_n_clone, new_n, sizeof (*new_n));
1079 /* none of the copied nodes have enqueue rights given out */
1080 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
1082 if (j >= vec_len (old_nodes_clone))
1084 /* new node, set to zero */
1085 clib_memset (&new_n_clone->stats_total, 0,
1086 sizeof (new_n_clone->stats_total));
1087 clib_memset (&new_n_clone->stats_last_clear, 0,
1088 sizeof (new_n_clone->stats_last_clear));
1092 /* Copy stats if the old data is valid */
1093 clib_memcpy_fast (&new_n_clone->stats_total,
1094 &old_n_clone->stats_total,
1095 sizeof (new_n_clone->stats_total));
1096 clib_memcpy_fast (&new_n_clone->stats_last_clear,
1097 &old_n_clone->stats_last_clear,
1098 sizeof (new_n_clone->stats_last_clear));
1100 /* keep previous node state */
1101 new_n_clone->state = old_n_clone->state;
1103 vec_add1 (nm_clone->nodes, new_n_clone);
1106 /* Free the old node clones */
1107 clib_mem_free (old_nodes_clone[0]);
1109 vec_free (old_nodes_clone);
1112 /* re-clone internal nodes */
1113 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL];
1114 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
1115 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
1116 CLIB_CACHE_LINE_BYTES);
1118 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
1120 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1121 rt->thread_index = vm_clone->thread_index;
1122 /* copy runtime_data, will be overwritten later for existing rt */
1123 if (n->runtime_data && n->runtime_data_bytes > 0)
1124 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1125 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1126 n->runtime_data_bytes));
1129 for (j = 0; j < vec_len (old_rt); j++)
1131 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1132 rt->state = old_rt[j].state;
1133 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1134 VLIB_NODE_RUNTIME_DATA_SIZE);
1139 /* re-clone input nodes */
1140 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
1141 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
1142 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
1143 CLIB_CACHE_LINE_BYTES);
1145 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
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 pre-input nodes */
1167 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT];
1168 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT] =
1169 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT],
1170 CLIB_CACHE_LINE_BYTES);
1172 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_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 nm_clone->processes = vec_dup_aligned (nm->processes,
1194 CLIB_CACHE_LINE_BYTES);
1198 vlib_worker_thread_node_runtime_update (void)
1201 * Make a note that we need to do a node runtime update
1202 * prior to releasing the barrier.
1204 vlib_global_main.need_vlib_worker_thread_node_runtime_update = 1;
1208 unformat_sched_policy (unformat_input_t * input, va_list * args)
1210 u32 *r = va_arg (*args, u32 *);
1213 #define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
1214 foreach_sched_policy
1221 static clib_error_t *
1222 cpu_config (vlib_main_t * vm, unformat_input_t * input)
1224 vlib_thread_registration_t *tr;
1226 vlib_thread_main_t *tm = &vlib_thread_main;
1231 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
1233 tm->n_thread_stacks = 1; /* account for main thread */
1234 tm->sched_policy = ~0;
1235 tm->sched_priority = ~0;
1236 tm->main_lcore = ~0;
1242 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
1246 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1248 if (unformat (input, "use-pthreads"))
1249 tm->use_pthreads = 1;
1250 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
1252 else if (unformat (input, "main-core %u", &tm->main_lcore))
1254 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
1256 else if (unformat (input, "coremask-%s %U", &name,
1257 unformat_bitmap_mask, &bitmap) ||
1258 unformat (input, "corelist-%s %U", &name,
1259 unformat_bitmap_list, &bitmap))
1261 p = hash_get_mem (tm->thread_registrations_by_name, name);
1263 return clib_error_return (0, "no such thread type '%s'", name);
1265 tr = (vlib_thread_registration_t *) p[0];
1267 if (tr->use_pthreads)
1268 return clib_error_return (0,
1269 "corelist cannot be set for '%s' threads",
1272 tr->coremask = bitmap;
1273 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1277 (input, "scheduler-policy %U", unformat_sched_policy,
1280 else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
1282 else if (unformat (input, "%s %u", &name, &count))
1284 p = hash_get_mem (tm->thread_registrations_by_name, name);
1286 return clib_error_return (0, "no such thread type 3 '%s'", name);
1288 tr = (vlib_thread_registration_t *) p[0];
1289 if (tr->fixed_count)
1290 return clib_error_return
1291 (0, "number of %s threads not configurable", tr->name);
1298 if (tm->sched_priority != ~0)
1300 if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
1302 u32 prio_max = sched_get_priority_max (tm->sched_policy);
1303 u32 prio_min = sched_get_priority_min (tm->sched_policy);
1304 if (tm->sched_priority > prio_max)
1305 tm->sched_priority = prio_max;
1306 if (tm->sched_priority < prio_min)
1307 tm->sched_priority = prio_min;
1311 return clib_error_return
1313 "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1314 tm->sched_priority);
1319 if (!tm->thread_prefix)
1320 tm->thread_prefix = format (0, "vpp");
1324 tm->n_thread_stacks += tr->count;
1325 tm->n_pthreads += tr->count * tr->use_pthreads;
1326 tm->n_threads += tr->count * (tr->use_pthreads == 0);
1333 VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1335 void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
1337 vnet_main_fixup (vlib_fork_fixup_t which)
1342 vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1344 vlib_main_t *vm = vlib_get_main ();
1346 if (vlib_mains == 0)
1349 ASSERT (vlib_get_thread_index () == 0);
1350 vlib_worker_thread_barrier_sync (vm);
1354 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1355 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1361 vlib_worker_thread_barrier_release (vm);
1365 * Enforce minimum open time to minimize packet loss due to Rx overflow,
1366 * based on a test based heuristic that barrier should be open for at least
1367 * 3 time as long as it is closed (with an upper bound of 1ms because by that
1368 * point it is probably too late to make a difference)
1371 #ifndef BARRIER_MINIMUM_OPEN_LIMIT
1372 #define BARRIER_MINIMUM_OPEN_LIMIT 0.001
1375 #ifndef BARRIER_MINIMUM_OPEN_FACTOR
1376 #define BARRIER_MINIMUM_OPEN_FACTOR 3
1380 vlib_worker_thread_barrier_sync_int (vlib_main_t * vm, const char *func_name)
1387 f64 max_vector_rate;
1391 if (vec_len (vlib_mains) < 2)
1394 ASSERT (vlib_get_thread_index () == 0);
1396 vlib_worker_threads[0].barrier_caller = func_name;
1397 count = vec_len (vlib_mains) - 1;
1399 /* Record entry relative to last close */
1400 now = vlib_time_now (vm);
1401 t_entry = now - vm->barrier_epoch;
1403 /* Tolerate recursive calls */
1404 if (++vlib_worker_threads[0].recursion_level > 1)
1406 barrier_trace_sync_rec (t_entry);
1411 * Need data to decide if we're working hard enough to honor
1412 * the barrier hold-down timer.
1414 max_vector_rate = 0.0;
1415 for (i = 1; i < vec_len (vlib_mains); i++)
1417 clib_max (max_vector_rate,
1418 vlib_last_vectors_per_main_loop_as_f64 (vlib_mains[i]));
1420 vlib_worker_threads[0].barrier_sync_count++;
1422 /* Enforce minimum barrier open time to minimize packet loss */
1423 ASSERT (vm->barrier_no_close_before <= (now + BARRIER_MINIMUM_OPEN_LIMIT));
1426 * If any worker thread seems busy, which we define
1427 * as a vector rate above 10, we enforce the barrier hold-down timer
1429 if (max_vector_rate > 10.0)
1433 now = vlib_time_now (vm);
1434 /* Barrier hold-down timer expired? */
1435 if (now >= vm->barrier_no_close_before)
1437 if ((vm->barrier_no_close_before - now)
1438 > (2.0 * BARRIER_MINIMUM_OPEN_LIMIT))
1441 ("clock change: would have waited for %.4f seconds",
1442 (vm->barrier_no_close_before - now));
1447 /* Record time of closure */
1448 t_open = now - vm->barrier_epoch;
1449 vm->barrier_epoch = now;
1451 deadline = now + BARRIER_SYNC_TIMEOUT;
1453 *vlib_worker_threads->wait_at_barrier = 1;
1454 while (*vlib_worker_threads->workers_at_barrier != count)
1456 if ((now = vlib_time_now (vm)) > deadline)
1458 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1463 t_closed = now - vm->barrier_epoch;
1465 barrier_trace_sync (t_entry, t_open, t_closed);
1469 void vlib_stat_segment_lock (void) __attribute__ ((weak));
1471 vlib_stat_segment_lock (void)
1475 void vlib_stat_segment_unlock (void) __attribute__ ((weak));
1477 vlib_stat_segment_unlock (void)
1482 vlib_worker_thread_barrier_release (vlib_main_t * vm)
1489 f64 t_update_main = 0.0;
1490 int refork_needed = 0;
1492 if (vec_len (vlib_mains) < 2)
1495 ASSERT (vlib_get_thread_index () == 0);
1498 now = vlib_time_now (vm);
1499 t_entry = now - vm->barrier_epoch;
1501 if (--vlib_worker_threads[0].recursion_level > 0)
1503 barrier_trace_release_rec (t_entry);
1507 /* Update (all) node runtimes before releasing the barrier, if needed */
1508 if (vm->need_vlib_worker_thread_node_runtime_update)
1511 * Lock stat segment here, so we's safe when
1512 * rebuilding the stat segment node clones from the
1515 vlib_stat_segment_lock ();
1517 /* Do stats elements on main thread */
1518 worker_thread_node_runtime_update_internal ();
1519 vm->need_vlib_worker_thread_node_runtime_update = 0;
1521 /* Do per thread rebuilds in parallel */
1523 clib_atomic_fetch_add (vlib_worker_threads->node_reforks_required,
1524 (vec_len (vlib_mains) - 1));
1525 now = vlib_time_now (vm);
1526 t_update_main = now - vm->barrier_epoch;
1529 deadline = now + BARRIER_SYNC_TIMEOUT;
1532 * Note when we let go of the barrier.
1533 * Workers can use this to derive a reasonably accurate
1534 * time offset. See vlib_time_now(...)
1536 vm->time_last_barrier_release = vlib_time_now (vm);
1537 CLIB_MEMORY_STORE_BARRIER ();
1539 *vlib_worker_threads->wait_at_barrier = 0;
1541 while (*vlib_worker_threads->workers_at_barrier > 0)
1543 if ((now = vlib_time_now (vm)) > deadline)
1545 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1550 /* Wait for reforks before continuing */
1553 now = vlib_time_now (vm);
1555 deadline = now + BARRIER_SYNC_TIMEOUT;
1557 while (*vlib_worker_threads->node_reforks_required > 0)
1559 if ((now = vlib_time_now (vm)) > deadline)
1561 fformat (stderr, "%s: worker thread refork deadlock\n",
1566 vlib_stat_segment_unlock ();
1569 t_closed_total = now - vm->barrier_epoch;
1571 minimum_open = t_closed_total * BARRIER_MINIMUM_OPEN_FACTOR;
1573 if (minimum_open > BARRIER_MINIMUM_OPEN_LIMIT)
1575 minimum_open = BARRIER_MINIMUM_OPEN_LIMIT;
1578 vm->barrier_no_close_before = now + minimum_open;
1580 /* Record barrier epoch (used to enforce minimum open time) */
1581 vm->barrier_epoch = now;
1583 barrier_trace_release (t_entry, t_closed_total, t_update_main);
1588 * Check the frame queue to see if any frames are available.
1589 * If so, pull the packets off the frames and put them to
1593 vlib_frame_queue_dequeue (vlib_main_t * vm, vlib_frame_queue_main_t * fqm)
1595 u32 thread_id = vm->thread_index;
1596 vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id];
1597 vlib_frame_queue_elt_t *elt;
1606 ASSERT (vm == vlib_mains[thread_id]);
1608 if (PREDICT_FALSE (fqm->node_index == ~0))
1611 * Gather trace data for frame queues
1613 if (PREDICT_FALSE (fq->trace))
1615 frame_queue_trace_t *fqt;
1616 frame_queue_nelt_counter_t *fqh;
1619 fqt = &fqm->frame_queue_traces[thread_id];
1621 fqt->nelts = fq->nelts;
1622 fqt->head = fq->head;
1623 fqt->head_hint = fq->head_hint;
1624 fqt->tail = fq->tail;
1625 fqt->threshold = fq->vector_threshold;
1626 fqt->n_in_use = fqt->tail - fqt->head;
1627 if (fqt->n_in_use >= fqt->nelts)
1629 // if beyond max then use max
1630 fqt->n_in_use = fqt->nelts - 1;
1633 /* Record the number of elements in use in the histogram */
1634 fqh = &fqm->frame_queue_histogram[thread_id];
1635 fqh->count[fqt->n_in_use]++;
1637 /* Record a snapshot of the elements in use */
1638 for (elix = 0; elix < fqt->nelts; elix++)
1640 elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1641 if (1 || elt->valid)
1643 fqt->n_vectors[elix] = elt->n_vectors;
1651 if (fq->head == fq->tail)
1653 fq->head_hint = fq->head;
1657 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
1661 fq->head_hint = fq->head;
1665 from = elt->buffer_index;
1666 msg_type = elt->msg_type;
1668 ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
1669 ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1671 f = vlib_get_frame_to_node (vm, fqm->node_index);
1673 to = vlib_frame_vector_args (f);
1675 n_left_to_node = elt->n_vectors;
1677 while (n_left_to_node >= 4)
1685 n_left_to_node -= 4;
1688 while (n_left_to_node > 0)
1696 vectors += elt->n_vectors;
1697 f->n_vectors = elt->n_vectors;
1698 vlib_put_frame_to_node (vm, fqm->node_index, f);
1702 elt->msg_type = 0xfefefefe;
1703 CLIB_MEMORY_BARRIER ();
1708 * Limit the number of packets pushed into the graph
1710 if (vectors >= fq->vector_threshold)
1712 fq->head_hint = fq->head;
1721 vlib_worker_thread_fn (void *arg)
1723 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
1724 vlib_thread_main_t *tm = vlib_get_thread_main ();
1725 vlib_main_t *vm = vlib_get_main ();
1728 ASSERT (vm->thread_index == vlib_get_thread_index ());
1730 vlib_worker_thread_init (w);
1731 clib_time_init (&vm->clib_time);
1732 clib_mem_set_heap (w->thread_mheap);
1734 /* Wait until the dpdk init sequence is complete */
1735 while (tm->extern_thread_mgmt && tm->worker_thread_release == 0)
1736 vlib_worker_thread_barrier_check ();
1738 e = vlib_call_init_exit_functions
1739 (vm, &vm->worker_init_function_registrations, 1 /* call_once */ );
1741 clib_error_report (e);
1743 vlib_worker_loop (vm);
1747 VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1750 .function = vlib_worker_thread_fn,
1755 vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1757 vlib_thread_main_t *tm = vlib_get_thread_main ();
1758 vlib_frame_queue_main_t *fqm;
1759 vlib_frame_queue_t *fq;
1762 if (frame_queue_nelts == 0)
1763 frame_queue_nelts = FRAME_QUEUE_NELTS;
1765 ASSERT (frame_queue_nelts >= 8);
1767 vec_add2 (tm->frame_queue_mains, fqm, 1);
1769 fqm->node_index = node_index;
1770 fqm->frame_queue_nelts = frame_queue_nelts;
1771 fqm->queue_hi_thresh = frame_queue_nelts - 2;
1773 vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1);
1774 vec_validate (fqm->per_thread_data, tm->n_vlib_mains - 1);
1775 _vec_len (fqm->vlib_frame_queues) = 0;
1776 for (i = 0; i < tm->n_vlib_mains; i++)
1778 vlib_frame_queue_per_thread_data_t *ptd;
1779 fq = vlib_frame_queue_alloc (frame_queue_nelts);
1780 vec_add1 (fqm->vlib_frame_queues, fq);
1782 ptd = vec_elt_at_index (fqm->per_thread_data, i);
1783 vec_validate (ptd->handoff_queue_elt_by_thread_index,
1784 tm->n_vlib_mains - 1);
1785 vec_validate_init_empty (ptd->congested_handoff_queue_by_thread_index,
1786 tm->n_vlib_mains - 1,
1787 (vlib_frame_queue_t *) (~0));
1790 return (fqm - tm->frame_queue_mains);
1794 vlib_thread_cb_register (struct vlib_main_t *vm, vlib_thread_callbacks_t * cb)
1796 vlib_thread_main_t *tm = vlib_get_thread_main ();
1798 if (tm->extern_thread_mgmt)
1801 tm->cb.vlib_launch_thread_cb = cb->vlib_launch_thread_cb;
1802 tm->extern_thread_mgmt = 1;
1807 vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t *
1810 ASSERT (vlib_get_thread_index () == 0);
1811 vlib_process_signal_event (vlib_get_main (), args->node_index,
1812 args->type_opaque, args->data);
1815 void *rpc_call_main_thread_cb_fn;
1818 vlib_rpc_call_main_thread (void *callback, u8 * args, u32 arg_size)
1820 if (rpc_call_main_thread_cb_fn)
1822 void (*fp) (void *, u8 *, u32) = rpc_call_main_thread_cb_fn;
1823 (*fp) (callback, args, arg_size);
1826 clib_warning ("BUG: rpc_call_main_thread_cb_fn NULL!");
1830 threads_init (vlib_main_t * vm)
1835 VLIB_INIT_FUNCTION (threads_init);
1838 static clib_error_t *
1839 show_clock_command_fn (vlib_main_t * vm,
1840 unformat_input_t * input, vlib_cli_command_t * cmd)
1845 now = vlib_time_now (vm);
1847 vlib_cli_output (vm, "Time now %.9f", now);
1849 if (vec_len (vlib_mains) == 1)
1852 vlib_cli_output (vm, "Time last barrier release %.9f",
1853 vm->time_last_barrier_release);
1855 for (i = 1; i < vec_len (vlib_mains); i++)
1857 if (vlib_mains[i] == 0)
1859 vlib_cli_output (vm, "Thread %d offset %.9f error %.9f", i,
1860 vlib_mains[i]->time_offset,
1861 vm->time_last_barrier_release -
1862 vlib_mains[i]->time_last_barrier_release);
1868 VLIB_CLI_COMMAND (f_command, static) =
1870 .path = "show clock",
1871 .short_help = "show clock",
1872 .function = show_clock_command_fn,
1877 * fd.io coding-style-patch-verification: ON
1880 * eval: (c-set-style "gnu")