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 <vlib/vlib.h>
22 #include <vlib/threads.h>
23 #include <vlib/unix/cj.h>
25 DECLARE_CJ_GLOBAL_LOG;
27 #define FRAME_QUEUE_NELTS 32
35 vlib_worker_thread_t *vlib_worker_threads;
36 vlib_thread_main_t vlib_thread_main;
39 * Barrier tracing can be enabled on a normal build to collect information
40 * on barrier use, including timings and call stacks. Deliberately not
41 * keyed off CLIB_DEBUG, because that can add significant overhead which
42 * imapacts observed timings.
45 #ifdef BARRIER_TRACING
47 * Output of barrier tracing can be to syslog or elog as suits
49 #ifdef BARRIER_TRACING_ELOG
51 elog_id_for_msg_name (const char *msg_name)
58 h = hash_create_string (0, sizeof (uword));
60 p = hash_get_mem (h, msg_name);
63 r = elog_string (&vlib_global_main.elog_main, "%s", msg_name);
65 name_copy = format (0, "%s%c", msg_name, 0);
67 hash_set_mem (h, name_copy, r);
73 * elog Barrier trace functions, which are nulled out if BARRIER_TRACING isn't
78 barrier_trace_sync (f64 t_entry, f64 t_open, f64 t_closed)
81 ELOG_TYPE_DECLARE (e) =
83 .format = "barrier <%d#%s(O:%dus:%dus)(%dus)",
84 .format_args = "i4T4i4i4i4",
89 u32 count, caller, t_entry, t_open, t_closed;
92 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
93 ed->count = (int) vlib_worker_threads[0].barrier_sync_count;
94 ed->caller = elog_id_for_msg_name (vlib_worker_threads[0].barrier_caller);
95 ed->t_entry = (int) (1000000.0 * t_entry);
96 ed->t_open = (int) (1000000.0 * t_open);
97 ed->t_closed = (int) (1000000.0 * t_closed);
101 barrier_trace_sync_rec (f64 t_entry)
104 ELOG_TYPE_DECLARE (e) =
106 .format = "barrier <%d(%dus)%s",
107 .format_args = "i4i4T4",
112 u32 depth, t_entry, caller;
115 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
116 ed->depth = (int) vlib_worker_threads[0].recursion_level - 1;
117 ed->t_entry = (int) (1000000.0 * t_entry);
118 ed->caller = elog_id_for_msg_name (vlib_worker_threads[0].barrier_caller);
122 barrier_trace_release_rec (f64 t_entry)
125 ELOG_TYPE_DECLARE (e) =
127 .format = "barrier (%dus)%d>",
128 .format_args = "i4i4",
136 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
137 ed->t_entry = (int) (1000000.0 * t_entry);
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)
145 ELOG_TYPE_DECLARE (e) =
147 .format = "barrier (%dus){%d}(C:%dus)#%d>",
148 .format_args = "i4i4i4i4",
153 u32 t_entry, t_update_main, t_closed_total, count;
156 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
157 ed->t_entry = (int) (1000000.0 * t_entry);
158 ed->t_update_main = (int) (1000000.0 * t_update_main);
159 ed->t_closed_total = (int) (1000000.0 * t_closed_total);
160 ed->count = (int) vlib_worker_threads[0].barrier_sync_count;
162 /* Reset context for next trace */
163 vlib_worker_threads[0].barrier_context = NULL;
166 char barrier_trace[65536];
167 char *btp = barrier_trace;
170 * syslog Barrier trace functions, which are nulled out if BARRIER_TRACING
176 barrier_trace_sync (f64 t_entry, f64 t_open, f64 t_closed)
178 btp += sprintf (btp, "<%u#%s",
179 (unsigned int) vlib_worker_threads[0].barrier_sync_count,
180 vlib_worker_threads[0].barrier_caller);
182 if (vlib_worker_threads[0].barrier_context)
184 btp += sprintf (btp, "[%s]", vlib_worker_threads[0].barrier_context);
188 btp += sprintf (btp, "(O:%dus:%dus)(%dus):",
189 (int) (1000000.0 * t_entry),
190 (int) (1000000.0 * t_open), (int) (1000000.0 * t_closed));
195 barrier_trace_sync_rec (f64 t_entry)
197 btp += sprintf (btp, "<%u(%dus)%s:",
198 (int) vlib_worker_threads[0].recursion_level - 1,
199 (int) (1000000.0 * t_entry),
200 vlib_worker_threads[0].barrier_caller);
204 barrier_trace_release_rec (f64 t_entry)
206 btp += sprintf (btp, ":(%dus)%u>", (int) (1000000.0 * t_entry),
207 (int) vlib_worker_threads[0].recursion_level);
211 barrier_trace_release (f64 t_entry, f64 t_closed_total, f64 t_update_main)
214 btp += sprintf (btp, ":(%dus)", (int) (1000000.0 * t_entry));
215 if (t_update_main > 0)
217 btp += sprintf (btp, "{%dus}", (int) (1000000.0 * t_update_main));
220 btp += sprintf (btp, "(C:%dus)#%u>",
221 (int) (1000000.0 * t_closed_total),
222 (int) vlib_worker_threads[0].barrier_sync_count);
224 /* Dump buffer to syslog, and reset for next trace */
225 fformat (stderr, "BTRC %s\n", barrier_trace);
227 vlib_worker_threads[0].barrier_context = NULL;
232 /* Null functions for default case where barrier tracing isn't used */
234 barrier_trace_sync (f64 t_entry, f64 t_open, f64 t_closed)
239 barrier_trace_sync_rec (f64 t_entry)
244 barrier_trace_release_rec (f64 t_entry)
249 barrier_trace_release (f64 t_entry, f64 t_closed_total, f64 t_update_main)
255 os_get_nthreads (void)
259 len = vec_len (vlib_thread_stacks);
267 vlib_set_thread_name (char *name)
269 int pthread_setname_np (pthread_t __target_thread, const char *__name);
271 pthread_t thread = pthread_self ();
275 rv = pthread_setname_np (thread, name);
277 clib_warning ("pthread_setname_np returned %d", rv);
282 sort_registrations_by_no_clone (void *a0, void *a1)
284 vlib_thread_registration_t **tr0 = a0;
285 vlib_thread_registration_t **tr1 = a1;
287 return ((i32) ((*tr0)->no_data_structure_clone)
288 - ((i32) ((*tr1)->no_data_structure_clone)));
292 clib_sysfs_list_to_bitmap (char *filename)
297 fp = fopen (filename, "r");
302 vec_validate (buffer, 256 - 1);
303 if (fgets ((char *) buffer, 256, fp))
306 unformat_init_string (&in, (char *) buffer,
307 strlen ((char *) buffer));
308 if (unformat (&in, "%U", unformat_bitmap_list, &r) != 1)
309 clib_warning ("unformat_bitmap_list failed");
319 /* Called early in the init sequence */
322 vlib_thread_init (vlib_main_t * vm)
324 vlib_thread_main_t *tm = &vlib_thread_main;
325 vlib_worker_thread_t *w;
326 vlib_thread_registration_t *tr;
327 u32 n_vlib_mains = 1;
332 /* get bitmaps of active cpu cores and sockets */
333 tm->cpu_core_bitmap =
334 clib_sysfs_list_to_bitmap ("/sys/devices/system/cpu/online");
335 tm->cpu_socket_bitmap =
336 clib_sysfs_list_to_bitmap ("/sys/devices/system/node/online");
338 avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
340 /* by default we skip core 0, unless it is the only one available */
341 if (tm->skip_cores == ~0 && clib_bitmap_count_set_bits (avail_cpu) == 1)
347 for (i = 0; i < tm->skip_cores; i++)
349 uword c = clib_bitmap_first_set (avail_cpu);
351 return clib_error_return (0, "no available cpus to skip");
353 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
356 /* grab cpu for main thread */
359 tm->main_lcore = clib_bitmap_first_set (avail_cpu);
360 if (tm->main_lcore == (u8) ~ 0)
361 return clib_error_return (0, "no available cpus to be used for the"
366 if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
367 return clib_error_return (0, "cpu %u is not available to be used"
368 " for the main thread", tm->main_lcore);
370 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
372 /* assume that there is socket 0 only if there is no data from sysfs */
373 if (!tm->cpu_socket_bitmap)
374 tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
376 /* pin main thread to main_lcore */
377 if (tm->cb.vlib_thread_set_lcore_cb)
379 tm->cb.vlib_thread_set_lcore_cb (0, tm->main_lcore);
382 /* as many threads as stacks... */
383 vec_validate_aligned (vlib_worker_threads, vec_len (vlib_thread_stacks) - 1,
384 CLIB_CACHE_LINE_BYTES);
386 /* Preallocate thread 0 */
387 _vec_len (vlib_worker_threads) = 1;
388 w = vlib_worker_threads;
389 w->thread_mheap = clib_mem_get_heap ();
390 w->thread_stack = vlib_thread_stacks[0];
391 w->lcore_id = tm->main_lcore;
392 w->lwp = syscall (SYS_gettid);
393 w->thread_id = pthread_self ();
394 tm->n_vlib_mains = 1;
396 if (tm->sched_policy != ~0)
398 struct sched_param sched_param;
399 if (!sched_getparam (w->lwp, &sched_param))
401 if (tm->sched_priority != ~0)
402 sched_param.sched_priority = tm->sched_priority;
403 sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
407 /* assign threads to cores and set n_vlib_mains */
412 vec_add1 (tm->registrations, tr);
416 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
418 for (i = 0; i < vec_len (tm->registrations); i++)
421 tr = tm->registrations[i];
422 tr->first_index = first_index;
423 first_index += tr->count;
424 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
426 /* construct coremask */
427 if (tr->use_pthreads || !tr->count)
434 clib_bitmap_foreach (c, tr->coremask, ({
435 if (clib_bitmap_get(avail_cpu, c) == 0)
436 return clib_error_return (0, "cpu %u is not available to be used"
437 " for the '%s' thread",c, tr->name);
439 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
446 for (j = 0; j < tr->count; j++)
448 uword c = clib_bitmap_first_set (avail_cpu);
450 return clib_error_return (0,
451 "no available cpus to be used for"
452 " the '%s' thread", tr->name);
454 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
455 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
460 clib_bitmap_free (avail_cpu);
462 tm->n_vlib_mains = n_vlib_mains;
464 vec_validate_aligned (vlib_worker_threads, first_index - 1,
465 CLIB_CACHE_LINE_BYTES);
471 vlib_frame_queue_alloc (int nelts)
473 vlib_frame_queue_t *fq;
475 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
476 memset (fq, 0, sizeof (*fq));
478 fq->vector_threshold = 128; // packets
479 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
483 if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
484 fformat (stderr, "WARNING: fq->tail unaligned\n");
485 if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
486 fformat (stderr, "WARNING: fq->head unaligned\n");
487 if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
488 fformat (stderr, "WARNING: fq->elts unaligned\n");
490 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
491 fformat (stderr, "WARNING: fq->elts[0] size %d\n",
492 sizeof (fq->elts[0]));
493 if (nelts & (nelts - 1))
495 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
503 void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
505 vl_msg_api_handler_no_free (void *v)
509 /* Turned off, save as reference material... */
512 vlib_frame_queue_dequeue_internal (int thread_id,
513 vlib_main_t * vm, vlib_node_main_t * nm)
515 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
516 vlib_frame_queue_elt_t *elt;
518 vlib_pending_frame_t *p;
519 vlib_node_runtime_t *r;
520 u32 node_runtime_index;
525 ASSERT (vm == vlib_mains[thread_id]);
529 if (fq->head == fq->tail)
532 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
537 before = clib_cpu_time_now ();
540 node_runtime_index = elt->node_runtime_index;
541 msg_type = elt->msg_type;
545 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
546 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
547 /* note fallthrough... */
548 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
549 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
551 vlib_frame_free (vm, r, f);
553 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
554 vec_add2 (vm->node_main.pending_frames, p, 1);
555 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
556 p->node_runtime_index = elt->node_runtime_index;
557 p->frame_index = vlib_frame_index (vm, f);
558 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
559 fq->dequeue_vectors += (u64) f->n_vectors;
561 case VLIB_FRAME_QUEUE_ELT_API_MSG:
562 vl_msg_api_handler_no_free (f);
565 clib_warning ("bogus frame queue message, type %d", msg_type);
570 fq->dequeue_ticks += clib_cpu_time_now () - before;
571 CLIB_MEMORY_BARRIER ();
580 vlib_frame_queue_dequeue (int thread_id,
581 vlib_main_t * vm, vlib_node_main_t * nm)
583 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
587 vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
588 u32 frame_queue_index, vlib_frame_t * frame,
589 vlib_frame_queue_msg_type_t type)
591 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
592 vlib_frame_queue_elt_t *elt;
595 u64 before = clib_cpu_time_now ();
599 new_tail = __sync_add_and_fetch (&fq->tail, 1);
601 /* Wait until a ring slot is available */
602 while (new_tail >= fq->head + fq->nelts)
604 f64 b4 = vlib_time_now_ticks (vm, before);
605 vlib_worker_thread_barrier_check (vm, b4);
606 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
607 // vlib_frame_queue_dequeue (vm->thread_index, vm, nm);
610 elt = fq->elts + (new_tail & (fq->nelts - 1));
612 /* this would be very bad... */
617 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
618 save_count = frame->n_vectors;
621 elt->node_runtime_index = node_runtime_index;
622 elt->msg_type = type;
623 CLIB_MEMORY_BARRIER ();
630 /* To be called by vlib worker threads upon startup */
632 vlib_worker_thread_init (vlib_worker_thread_t * w)
634 vlib_thread_main_t *tm = vlib_get_thread_main ();
637 * Note: disabling signals in worker threads as follows
638 * prevents the api post-mortem dump scheme from working
642 * pthread_sigmask (SIG_SETMASK, &s, 0);
646 clib_mem_set_heap (w->thread_mheap);
648 if (vec_len (tm->thread_prefix) && w->registration->short_name)
650 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
651 w->registration->short_name, w->instance_id, '\0');
652 vlib_set_thread_name ((char *) w->name);
655 if (!w->registration->use_pthreads)
658 /* Initial barrier sync, for both worker and i/o threads */
659 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
661 while (*vlib_worker_threads->wait_at_barrier)
664 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
669 vlib_worker_thread_bootstrap_fn (void *arg)
672 vlib_worker_thread_t *w = arg;
674 w->lwp = syscall (SYS_gettid);
675 w->thread_id = pthread_self ();
677 __os_thread_index = w - vlib_worker_threads;
679 rv = (void *) clib_calljmp
680 ((uword (*)(uword)) w->thread_function,
681 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
682 /* NOTREACHED, we hope */
686 static clib_error_t *
687 vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned lcore_id)
689 vlib_thread_main_t *tm = &vlib_thread_main;
690 void *(*fp_arg) (void *) = fp;
692 w->lcore_id = lcore_id;
693 if (tm->cb.vlib_launch_thread_cb && !w->registration->use_pthreads)
694 return tm->cb.vlib_launch_thread_cb (fp, (void *) w, lcore_id);
700 CPU_SET (lcore_id, &cpuset);
702 if (pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w))
703 return clib_error_return_unix (0, "pthread_create");
705 if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
706 return clib_error_return_unix (0, "pthread_setaffinity_np");
712 static clib_error_t *
713 start_workers (vlib_main_t * vm)
716 vlib_worker_thread_t *w;
717 vlib_main_t *vm_clone;
719 vlib_thread_main_t *tm = &vlib_thread_main;
720 vlib_thread_registration_t *tr;
721 vlib_node_runtime_t *rt;
722 u32 n_vlib_mains = tm->n_vlib_mains;
723 u32 worker_thread_index;
724 u8 *main_heap = clib_mem_get_per_cpu_heap ();
725 mheap_t *main_heap_header = mheap_header (main_heap);
727 vec_reset_length (vlib_worker_threads);
729 /* Set up the main thread */
730 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
731 w->elog_track.name = "main thread";
732 elog_track_register (&vm->elog_main, &w->elog_track);
734 if (vec_len (tm->thread_prefix))
736 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
737 vlib_set_thread_name ((char *) w->name);
741 * Truth of the matter: we always use at least two
742 * threads. So, make the main heap thread-safe
743 * and make the event log thread-safe.
745 main_heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
747 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
748 vm->elog_main.lock[0] = 0;
750 if (n_vlib_mains > 1)
752 /* Replace hand-crafted length-1 vector with a real vector */
755 vec_validate_aligned (vlib_mains, tm->n_vlib_mains - 1,
756 CLIB_CACHE_LINE_BYTES);
757 _vec_len (vlib_mains) = 0;
758 vec_add1_aligned (vlib_mains, vm, CLIB_CACHE_LINE_BYTES);
760 vlib_worker_threads->wait_at_barrier =
761 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
762 vlib_worker_threads->workers_at_barrier =
763 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
765 vlib_worker_threads->node_reforks_required =
766 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
768 /* Ask for an initial barrier sync */
769 *vlib_worker_threads->workers_at_barrier = 0;
770 *vlib_worker_threads->wait_at_barrier = 1;
772 /* Without update or refork */
773 *vlib_worker_threads->node_reforks_required = 0;
774 vm->need_vlib_worker_thread_node_runtime_update = 0;
777 vm->barrier_epoch = 0;
778 vm->barrier_no_close_before = 0;
780 worker_thread_index = 1;
782 for (i = 0; i < vec_len (tm->registrations); i++)
784 vlib_node_main_t *nm, *nm_clone;
785 vlib_buffer_free_list_t *fl_clone, *fl_orig;
786 vlib_buffer_free_list_t *orig_freelist_pool;
789 tr = tm->registrations[i];
794 for (k = 0; k < tr->count; k++)
798 vec_add2 (vlib_worker_threads, w, 1);
801 mheap_alloc (0 /* use VM */ , tr->mheap_size);
803 w->thread_mheap = main_heap;
806 vlib_thread_stack_init (w - vlib_worker_threads);
807 w->thread_function = tr->function;
808 w->thread_function_arg = w;
810 w->registration = tr;
813 (char *) format (0, "%s %d", tr->name, k + 1);
814 vec_add1 (w->elog_track.name, 0);
815 elog_track_register (&vm->elog_main, &w->elog_track);
817 if (tr->no_data_structure_clone)
820 /* Fork vlib_global_main et al. Look for bugs here */
821 oldheap = clib_mem_set_heap (w->thread_mheap);
823 vm_clone = clib_mem_alloc_aligned (sizeof (*vm_clone),
824 CLIB_CACHE_LINE_BYTES);
825 clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
827 vm_clone->thread_index = worker_thread_index;
828 vm_clone->heap_base = w->thread_mheap;
829 vm_clone->init_functions_called =
830 hash_create (0, /* value bytes */ 0);
831 vm_clone->pending_rpc_requests = 0;
832 vec_validate (vm_clone->pending_rpc_requests, 0);
833 _vec_len (vm_clone->pending_rpc_requests) = 0;
834 memset (&vm_clone->random_buffer, 0,
835 sizeof (vm_clone->random_buffer));
837 nm = &vlib_mains[0]->node_main;
838 nm_clone = &vm_clone->node_main;
839 /* fork next frames array, preserving node runtime indices */
840 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
841 CLIB_CACHE_LINE_BYTES);
842 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
844 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
845 u32 save_node_runtime_index;
848 save_node_runtime_index = nf->node_runtime_index;
849 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
850 vlib_next_frame_init (nf);
851 nf->node_runtime_index = save_node_runtime_index;
852 nf->flags = save_flags;
855 /* fork the frame dispatch queue */
856 nm_clone->pending_frames = 0;
857 vec_validate (nm_clone->pending_frames, 10); /* $$$$$?????? */
858 _vec_len (nm_clone->pending_frames) = 0;
863 /* Allocate all nodes in single block for speed */
864 n = clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*n));
866 for (j = 0; j < vec_len (nm->nodes); j++)
868 clib_memcpy (n, nm->nodes[j], sizeof (*n));
869 /* none of the copied nodes have enqueue rights given out */
870 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
871 memset (&n->stats_total, 0, sizeof (n->stats_total));
872 memset (&n->stats_last_clear, 0,
873 sizeof (n->stats_last_clear));
874 vec_add1 (nm_clone->nodes, n);
877 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
878 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
879 CLIB_CACHE_LINE_BYTES);
881 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
883 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
884 rt->thread_index = vm_clone->thread_index;
885 /* copy initial runtime_data from node */
886 if (n->runtime_data && n->runtime_data_bytes > 0)
887 clib_memcpy (rt->runtime_data, n->runtime_data,
888 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
889 n->runtime_data_bytes));
892 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
893 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
894 CLIB_CACHE_LINE_BYTES);
895 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
897 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
898 rt->thread_index = vm_clone->thread_index;
899 /* copy initial runtime_data from node */
900 if (n->runtime_data && n->runtime_data_bytes > 0)
901 clib_memcpy (rt->runtime_data, n->runtime_data,
902 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
903 n->runtime_data_bytes));
906 nm_clone->processes = vec_dup_aligned (nm->processes,
907 CLIB_CACHE_LINE_BYTES);
909 /* zap the (per worker) frame freelists, etc */
910 nm_clone->frame_sizes = 0;
911 nm_clone->frame_size_hash = hash_create (0, sizeof (uword));
913 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
915 clib_mem_set_heap (oldheap);
916 vec_add1_aligned (vlib_mains, vm_clone, CLIB_CACHE_LINE_BYTES);
918 vm_clone->error_main.counters = vec_dup_aligned
919 (vlib_mains[0]->error_main.counters, CLIB_CACHE_LINE_BYTES);
920 vm_clone->error_main.counters_last_clear = vec_dup_aligned
921 (vlib_mains[0]->error_main.counters_last_clear,
922 CLIB_CACHE_LINE_BYTES);
924 /* Fork the vlib_buffer_main_t free lists, etc. */
925 orig_freelist_pool = vm_clone->buffer_free_list_pool;
926 vm_clone->buffer_free_list_pool = 0;
929 pool_foreach (fl_orig, orig_freelist_pool,
931 pool_get_aligned (vm_clone->buffer_free_list_pool,
932 fl_clone, CLIB_CACHE_LINE_BYTES);
933 ASSERT (fl_orig - orig_freelist_pool
934 == fl_clone - vm_clone->buffer_free_list_pool);
936 fl_clone[0] = fl_orig[0];
937 fl_clone->buffers = 0;
938 fl_clone->n_alloc = 0;
942 worker_thread_index++;
948 /* only have non-data-structure copy threads to create... */
949 for (i = 0; i < vec_len (tm->registrations); i++)
951 tr = tm->registrations[i];
953 for (j = 0; j < tr->count; j++)
955 vec_add2 (vlib_worker_threads, w, 1);
958 mheap_alloc (0 /* use VM */ , tr->mheap_size);
960 w->thread_mheap = main_heap;
962 vlib_thread_stack_init (w - vlib_worker_threads);
963 w->thread_function = tr->function;
964 w->thread_function_arg = w;
967 (char *) format (0, "%s %d", tr->name, j + 1);
968 w->registration = tr;
969 vec_add1 (w->elog_track.name, 0);
970 elog_track_register (&vm->elog_main, &w->elog_track);
975 worker_thread_index = 1;
977 for (i = 0; i < vec_len (tm->registrations); i++)
982 tr = tm->registrations[i];
984 if (tr->use_pthreads || tm->use_pthreads)
986 for (j = 0; j < tr->count; j++)
988 w = vlib_worker_threads + worker_thread_index++;
989 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
992 clib_error_report (err);
999 clib_bitmap_foreach (c, tr->coremask, ({
1000 w = vlib_worker_threads + worker_thread_index++;
1001 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
1004 clib_error_report (err);
1009 vlib_worker_thread_barrier_sync (vm);
1010 vlib_worker_thread_barrier_release (vm);
1014 VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
1018 worker_thread_node_runtime_update_internal (void)
1022 vlib_node_main_t *nm, *nm_clone;
1023 vlib_main_t *vm_clone;
1024 vlib_node_runtime_t *rt;
1026 vlib_node_runtime_sync_stats (vlib_main_t * vm,
1027 vlib_node_runtime_t * r,
1029 uword n_vectors, uword n_clocks);
1031 ASSERT (vlib_get_thread_index () == 0);
1034 nm = &vm->node_main;
1036 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
1039 * Scrape all runtime stats, so we don't lose node runtime(s) with
1040 * pending counts, or throw away worker / io thread counts.
1042 for (j = 0; j < vec_len (nm->nodes); j++)
1046 vlib_node_sync_stats (vm, n);
1049 for (i = 1; i < vec_len (vlib_mains); i++)
1053 vm_clone = vlib_mains[i];
1054 nm_clone = &vm_clone->node_main;
1056 for (j = 0; j < vec_len (nm_clone->nodes); j++)
1058 n = nm_clone->nodes[j];
1060 rt = vlib_node_get_runtime (vm_clone, n->index);
1061 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
1065 /* Per-worker clone rebuilds are now done on each thread */
1070 vlib_worker_thread_node_refork (void)
1072 vlib_main_t *vm, *vm_clone;
1073 vlib_node_main_t *nm, *nm_clone;
1074 vlib_node_t **old_nodes_clone;
1075 vlib_node_runtime_t *rt, *old_rt;
1077 vlib_node_t *new_n_clone;
1082 nm = &vm->node_main;
1083 vm_clone = vlib_get_main ();
1084 nm_clone = &vm_clone->node_main;
1086 /* Re-clone error heap */
1087 u64 *old_counters = vm_clone->error_main.counters;
1088 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
1090 clib_memcpy (&vm_clone->error_main, &vm->error_main,
1091 sizeof (vm->error_main));
1092 j = vec_len (vm->error_main.counters) - 1;
1093 vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES);
1094 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
1095 vm_clone->error_main.counters = old_counters;
1096 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
1098 nm_clone = &vm_clone->node_main;
1099 vec_free (nm_clone->next_frames);
1100 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
1101 CLIB_CACHE_LINE_BYTES);
1103 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
1105 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
1106 u32 save_node_runtime_index;
1109 save_node_runtime_index = nf->node_runtime_index;
1110 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
1111 vlib_next_frame_init (nf);
1112 nf->node_runtime_index = save_node_runtime_index;
1113 nf->flags = save_flags;
1116 old_nodes_clone = nm_clone->nodes;
1117 nm_clone->nodes = 0;
1121 /* Allocate all nodes in single block for speed */
1123 clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*new_n_clone));
1124 for (j = 0; j < vec_len (nm->nodes); j++)
1126 vlib_node_t *old_n_clone;
1129 new_n = nm->nodes[j];
1130 old_n_clone = old_nodes_clone[j];
1132 clib_memcpy (new_n_clone, new_n, sizeof (*new_n));
1133 /* none of the copied nodes have enqueue rights given out */
1134 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
1136 if (j >= vec_len (old_nodes_clone))
1138 /* new node, set to zero */
1139 memset (&new_n_clone->stats_total, 0,
1140 sizeof (new_n_clone->stats_total));
1141 memset (&new_n_clone->stats_last_clear, 0,
1142 sizeof (new_n_clone->stats_last_clear));
1146 /* Copy stats if the old data is valid */
1147 clib_memcpy (&new_n_clone->stats_total,
1148 &old_n_clone->stats_total,
1149 sizeof (new_n_clone->stats_total));
1150 clib_memcpy (&new_n_clone->stats_last_clear,
1151 &old_n_clone->stats_last_clear,
1152 sizeof (new_n_clone->stats_last_clear));
1154 /* keep previous node state */
1155 new_n_clone->state = old_n_clone->state;
1157 vec_add1 (nm_clone->nodes, new_n_clone);
1160 /* Free the old node clones */
1161 clib_mem_free (old_nodes_clone[0]);
1163 vec_free (old_nodes_clone);
1166 /* re-clone internal nodes */
1167 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL];
1168 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
1169 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
1170 CLIB_CACHE_LINE_BYTES);
1172 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
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 (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 (rt->runtime_data, old_rt[j].runtime_data,
1188 VLIB_NODE_RUNTIME_DATA_SIZE);
1193 /* re-clone input nodes */
1194 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
1195 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
1196 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
1197 CLIB_CACHE_LINE_BYTES);
1199 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_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 (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 (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);
1225 vlib_worker_thread_node_runtime_update (void)
1228 * Make a note that we need to do a node runtime update
1229 * prior to releasing the barrier.
1231 vlib_global_main.need_vlib_worker_thread_node_runtime_update = 1;
1235 unformat_sched_policy (unformat_input_t * input, va_list * args)
1237 u32 *r = va_arg (*args, u32 *);
1240 #define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
1241 foreach_sched_policy
1248 static clib_error_t *
1249 cpu_config (vlib_main_t * vm, unformat_input_t * input)
1251 vlib_thread_registration_t *tr;
1253 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->skip_cores = ~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, "coremask-%s %llx", &name, &coremask))
1286 p = hash_get_mem (tm->thread_registrations_by_name, name);
1288 return clib_error_return (0, "no such thread type '%s'", name);
1290 tr = (vlib_thread_registration_t *) p[0];
1292 if (tr->use_pthreads)
1293 return clib_error_return (0,
1294 "coremask cannot be set for '%s' threads",
1297 tr->coremask = clib_bitmap_set_multiple
1298 (tr->coremask, 0, coremask, BITS (coremask));
1299 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1301 else if (unformat (input, "corelist-%s %U", &name, unformat_bitmap_list,
1304 p = hash_get_mem (tm->thread_registrations_by_name, name);
1306 return clib_error_return (0, "no such thread type '%s'", name);
1308 tr = (vlib_thread_registration_t *) p[0];
1310 if (tr->use_pthreads)
1311 return clib_error_return (0,
1312 "corelist cannot be set for '%s' threads",
1315 tr->coremask = bitmap;
1316 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1320 (input, "scheduler-policy %U", unformat_sched_policy,
1323 else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
1325 else if (unformat (input, "%s %u", &name, &count))
1327 p = hash_get_mem (tm->thread_registrations_by_name, name);
1329 return clib_error_return (0, "no such thread type 3 '%s'", name);
1331 tr = (vlib_thread_registration_t *) p[0];
1332 if (tr->fixed_count)
1333 return clib_error_return
1334 (0, "number of %s threads not configurable", tr->name);
1341 if (tm->sched_priority != ~0)
1343 if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
1345 u32 prio_max = sched_get_priority_max (tm->sched_policy);
1346 u32 prio_min = sched_get_priority_min (tm->sched_policy);
1347 if (tm->sched_priority > prio_max)
1348 tm->sched_priority = prio_max;
1349 if (tm->sched_priority < prio_min)
1350 tm->sched_priority = prio_min;
1354 return clib_error_return
1356 "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1357 tm->sched_priority);
1362 if (!tm->thread_prefix)
1363 tm->thread_prefix = format (0, "vpp");
1367 tm->n_thread_stacks += tr->count;
1368 tm->n_pthreads += tr->count * tr->use_pthreads;
1369 tm->n_threads += tr->count * (tr->use_pthreads == 0);
1376 VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1378 #if !defined (__x86_64__) && !defined (__i386__) && !defined (__aarch64__) && !defined (__powerpc64__) && !defined(__arm__)
1380 __sync_fetch_and_add_8 (void)
1382 fformat (stderr, "%s called\n", __FUNCTION__);
1387 __sync_add_and_fetch_8 (void)
1389 fformat (stderr, "%s called\n", __FUNCTION__);
1394 void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
1396 vnet_main_fixup (vlib_fork_fixup_t which)
1401 vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1403 vlib_main_t *vm = vlib_get_main ();
1405 if (vlib_mains == 0)
1408 ASSERT (vlib_get_thread_index () == 0);
1409 vlib_worker_thread_barrier_sync (vm);
1413 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1414 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1420 vlib_worker_thread_barrier_release (vm);
1424 * Enforce minimum open time to minimize packet loss due to Rx overflow,
1425 * based on a test based heuristic that barrier should be open for at least
1426 * 3 time as long as it is closed (with an upper bound of 1ms because by that
1427 * point it is probably too late to make a difference)
1430 #ifndef BARRIER_MINIMUM_OPEN_LIMIT
1431 #define BARRIER_MINIMUM_OPEN_LIMIT 0.001
1434 #ifndef BARRIER_MINIMUM_OPEN_FACTOR
1435 #define BARRIER_MINIMUM_OPEN_FACTOR 3
1439 vlib_worker_thread_barrier_sync_int (vlib_main_t * vm)
1448 if (vec_len (vlib_mains) < 2)
1451 ASSERT (vlib_get_thread_index () == 0);
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);
1466 vlib_worker_threads[0].barrier_sync_count++;
1468 /* Enforce minimum barrier open time to minimize packet loss */
1469 ASSERT (vm->barrier_no_close_before <= (now + BARRIER_MINIMUM_OPEN_LIMIT));
1470 while ((now = vlib_time_now (vm)) < vm->barrier_no_close_before)
1473 /* Record time of closure */
1474 t_open = now - vm->barrier_epoch;
1475 vm->barrier_epoch = now;
1477 deadline = now + BARRIER_SYNC_TIMEOUT;
1479 *vlib_worker_threads->wait_at_barrier = 1;
1480 while (*vlib_worker_threads->workers_at_barrier != count)
1482 if ((now = vlib_time_now (vm)) > deadline)
1484 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1489 t_closed = now - vm->barrier_epoch;
1491 barrier_trace_sync (t_entry, t_open, t_closed);
1495 void vlib_stat_segment_lock (void) __attribute__ ((weak));
1497 vlib_stat_segment_lock (void)
1501 void vlib_stat_segment_unlock (void) __attribute__ ((weak));
1503 vlib_stat_segment_unlock (void)
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_smp_atomic_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;
1557 *vlib_worker_threads->wait_at_barrier = 0;
1559 while (*vlib_worker_threads->workers_at_barrier > 0)
1561 if ((now = vlib_time_now (vm)) > deadline)
1563 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1568 /* Wait for reforks before continuing */
1571 now = vlib_time_now (vm);
1573 deadline = now + BARRIER_SYNC_TIMEOUT;
1575 while (*vlib_worker_threads->node_reforks_required > 0)
1577 if ((now = vlib_time_now (vm)) > deadline)
1579 fformat (stderr, "%s: worker thread refork deadlock\n",
1584 vlib_stat_segment_unlock ();
1587 t_closed_total = now - vm->barrier_epoch;
1589 minimum_open = t_closed_total * BARRIER_MINIMUM_OPEN_FACTOR;
1591 if (minimum_open > BARRIER_MINIMUM_OPEN_LIMIT)
1593 minimum_open = BARRIER_MINIMUM_OPEN_LIMIT;
1596 vm->barrier_no_close_before = now + minimum_open;
1598 /* Record barrier epoch (used to enforce minimum open time) */
1599 vm->barrier_epoch = now;
1601 barrier_trace_release (t_entry, t_closed_total, t_update_main);
1606 * Check the frame queue to see if any frames are available.
1607 * If so, pull the packets off the frames and put them to
1611 vlib_frame_queue_dequeue (vlib_main_t * vm, vlib_frame_queue_main_t * fqm)
1613 u32 thread_id = vm->thread_index;
1614 vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id];
1615 vlib_frame_queue_elt_t *elt;
1624 ASSERT (vm == vlib_mains[thread_id]);
1626 if (PREDICT_FALSE (fqm->node_index == ~0))
1629 * Gather trace data for frame queues
1631 if (PREDICT_FALSE (fq->trace))
1633 frame_queue_trace_t *fqt;
1634 frame_queue_nelt_counter_t *fqh;
1637 fqt = &fqm->frame_queue_traces[thread_id];
1639 fqt->nelts = fq->nelts;
1640 fqt->head = fq->head;
1641 fqt->head_hint = fq->head_hint;
1642 fqt->tail = fq->tail;
1643 fqt->threshold = fq->vector_threshold;
1644 fqt->n_in_use = fqt->tail - fqt->head;
1645 if (fqt->n_in_use >= fqt->nelts)
1647 // if beyond max then use max
1648 fqt->n_in_use = fqt->nelts - 1;
1651 /* Record the number of elements in use in the histogram */
1652 fqh = &fqm->frame_queue_histogram[thread_id];
1653 fqh->count[fqt->n_in_use]++;
1655 /* Record a snapshot of the elements in use */
1656 for (elix = 0; elix < fqt->nelts; elix++)
1658 elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1659 if (1 || elt->valid)
1661 fqt->n_vectors[elix] = elt->n_vectors;
1669 if (fq->head == fq->tail)
1671 fq->head_hint = fq->head;
1675 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
1679 fq->head_hint = fq->head;
1683 from = elt->buffer_index;
1684 msg_type = elt->msg_type;
1686 ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
1687 ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1689 f = vlib_get_frame_to_node (vm, fqm->node_index);
1691 to = vlib_frame_vector_args (f);
1693 n_left_to_node = elt->n_vectors;
1695 while (n_left_to_node >= 4)
1703 n_left_to_node -= 4;
1706 while (n_left_to_node > 0)
1714 vectors += elt->n_vectors;
1715 f->n_vectors = elt->n_vectors;
1716 vlib_put_frame_to_node (vm, fqm->node_index, f);
1720 elt->msg_type = 0xfefefefe;
1721 CLIB_MEMORY_BARRIER ();
1726 * Limit the number of packets pushed into the graph
1728 if (vectors >= fq->vector_threshold)
1730 fq->head_hint = fq->head;
1739 vlib_worker_thread_fn (void *arg)
1741 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
1742 vlib_thread_main_t *tm = vlib_get_thread_main ();
1743 vlib_main_t *vm = vlib_get_main ();
1746 ASSERT (vm->thread_index == vlib_get_thread_index ());
1748 vlib_worker_thread_init (w);
1749 clib_time_init (&vm->clib_time);
1750 clib_mem_set_heap (w->thread_mheap);
1752 /* Wait until the dpdk init sequence is complete */
1753 while (tm->extern_thread_mgmt && tm->worker_thread_release == 0)
1754 vlib_worker_thread_barrier_check ();
1756 e = vlib_call_init_exit_functions
1757 (vm, vm->worker_init_function_registrations, 1 /* call_once */ );
1759 clib_error_report (e);
1761 vlib_worker_loop (vm);
1765 VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1768 .function = vlib_worker_thread_fn,
1773 vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1775 vlib_thread_main_t *tm = vlib_get_thread_main ();
1776 vlib_frame_queue_main_t *fqm;
1777 vlib_frame_queue_t *fq;
1780 if (frame_queue_nelts == 0)
1781 frame_queue_nelts = FRAME_QUEUE_NELTS;
1783 vec_add2 (tm->frame_queue_mains, fqm, 1);
1785 fqm->node_index = node_index;
1787 vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1);
1788 _vec_len (fqm->vlib_frame_queues) = 0;
1789 for (i = 0; i < tm->n_vlib_mains; i++)
1791 fq = vlib_frame_queue_alloc (frame_queue_nelts);
1792 vec_add1 (fqm->vlib_frame_queues, fq);
1795 return (fqm - tm->frame_queue_mains);
1799 vlib_thread_cb_register (struct vlib_main_t *vm, vlib_thread_callbacks_t * cb)
1801 vlib_thread_main_t *tm = vlib_get_thread_main ();
1803 if (tm->extern_thread_mgmt)
1806 tm->cb.vlib_launch_thread_cb = cb->vlib_launch_thread_cb;
1807 tm->extern_thread_mgmt = 1;
1812 vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t *
1815 ASSERT (vlib_get_thread_index () == 0);
1816 vlib_process_signal_event (vlib_get_main (), args->node_index,
1817 args->type_opaque, args->data);
1820 void *rpc_call_main_thread_cb_fn;
1823 vlib_rpc_call_main_thread (void *callback, u8 * args, u32 arg_size)
1825 if (rpc_call_main_thread_cb_fn)
1827 void (*fp) (void *, u8 *, u32) = rpc_call_main_thread_cb_fn;
1828 (*fp) (callback, args, arg_size);
1831 clib_warning ("BUG: rpc_call_main_thread_cb_fn NULL!");
1835 threads_init (vlib_main_t * vm)
1840 VLIB_INIT_FUNCTION (threads_init);
1843 * fd.io coding-style-patch-verification: ON
1846 * eval: (c-set-style "gnu")