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);
341 for (i = 0; i < tm->skip_cores; i++)
343 uword c = clib_bitmap_first_set (avail_cpu);
345 return clib_error_return (0, "no available cpus to skip");
347 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
350 /* grab cpu for main thread */
351 if (tm->main_lcore == ~0)
353 /* if main-lcore is not set, we try to use lcore 1 */
354 if (clib_bitmap_get (avail_cpu, 1))
357 tm->main_lcore = clib_bitmap_first_set (avail_cpu);
358 if (tm->main_lcore == (u8) ~ 0)
359 return clib_error_return (0, "no available cpus to be used for the"
364 if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
365 return clib_error_return (0, "cpu %u is not available to be used"
366 " for the main thread", tm->main_lcore);
368 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
370 /* assume that there is socket 0 only if there is no data from sysfs */
371 if (!tm->cpu_socket_bitmap)
372 tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
374 /* pin main thread to main_lcore */
375 if (tm->cb.vlib_thread_set_lcore_cb)
377 tm->cb.vlib_thread_set_lcore_cb (0, tm->main_lcore);
383 CPU_SET (tm->main_lcore, &cpuset);
384 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
387 /* as many threads as stacks... */
388 vec_validate_aligned (vlib_worker_threads, vec_len (vlib_thread_stacks) - 1,
389 CLIB_CACHE_LINE_BYTES);
391 /* Preallocate thread 0 */
392 _vec_len (vlib_worker_threads) = 1;
393 w = vlib_worker_threads;
394 w->thread_mheap = clib_mem_get_heap ();
395 w->thread_stack = vlib_thread_stacks[0];
396 w->lcore_id = tm->main_lcore;
397 w->lwp = syscall (SYS_gettid);
398 w->thread_id = pthread_self ();
399 tm->n_vlib_mains = 1;
401 if (tm->sched_policy != ~0)
403 struct sched_param sched_param;
404 if (!sched_getparam (w->lwp, &sched_param))
406 if (tm->sched_priority != ~0)
407 sched_param.sched_priority = tm->sched_priority;
408 sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
412 /* assign threads to cores and set n_vlib_mains */
417 vec_add1 (tm->registrations, tr);
421 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
423 for (i = 0; i < vec_len (tm->registrations); i++)
426 tr = tm->registrations[i];
427 tr->first_index = first_index;
428 first_index += tr->count;
429 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
431 /* construct coremask */
432 if (tr->use_pthreads || !tr->count)
439 clib_bitmap_foreach (c, tr->coremask, ({
440 if (clib_bitmap_get(avail_cpu, c) == 0)
441 return clib_error_return (0, "cpu %u is not available to be used"
442 " for the '%s' thread",c, tr->name);
444 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
451 for (j = 0; j < tr->count; j++)
453 uword c = clib_bitmap_first_set (avail_cpu);
455 return clib_error_return (0,
456 "no available cpus to be used for"
457 " the '%s' thread", tr->name);
459 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
460 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
465 clib_bitmap_free (avail_cpu);
467 tm->n_vlib_mains = n_vlib_mains;
469 vec_validate_aligned (vlib_worker_threads, first_index - 1,
470 CLIB_CACHE_LINE_BYTES);
476 vlib_frame_queue_alloc (int nelts)
478 vlib_frame_queue_t *fq;
480 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
481 memset (fq, 0, sizeof (*fq));
483 fq->vector_threshold = 128; // packets
484 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
488 if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
489 fformat (stderr, "WARNING: fq->tail unaligned\n");
490 if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
491 fformat (stderr, "WARNING: fq->head unaligned\n");
492 if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
493 fformat (stderr, "WARNING: fq->elts unaligned\n");
495 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
496 fformat (stderr, "WARNING: fq->elts[0] size %d\n",
497 sizeof (fq->elts[0]));
498 if (nelts & (nelts - 1))
500 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
508 void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
510 vl_msg_api_handler_no_free (void *v)
514 /* Turned off, save as reference material... */
517 vlib_frame_queue_dequeue_internal (int thread_id,
518 vlib_main_t * vm, vlib_node_main_t * nm)
520 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
521 vlib_frame_queue_elt_t *elt;
523 vlib_pending_frame_t *p;
524 vlib_node_runtime_t *r;
525 u32 node_runtime_index;
530 ASSERT (vm == vlib_mains[thread_id]);
534 if (fq->head == fq->tail)
537 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
542 before = clib_cpu_time_now ();
545 node_runtime_index = elt->node_runtime_index;
546 msg_type = elt->msg_type;
550 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
551 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
552 /* note fallthrough... */
553 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
554 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
556 vlib_frame_free (vm, r, f);
558 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
559 vec_add2 (vm->node_main.pending_frames, p, 1);
560 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
561 p->node_runtime_index = elt->node_runtime_index;
562 p->frame_index = vlib_frame_index (vm, f);
563 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
564 fq->dequeue_vectors += (u64) f->n_vectors;
566 case VLIB_FRAME_QUEUE_ELT_API_MSG:
567 vl_msg_api_handler_no_free (f);
570 clib_warning ("bogus frame queue message, type %d", msg_type);
575 fq->dequeue_ticks += clib_cpu_time_now () - before;
576 CLIB_MEMORY_BARRIER ();
585 vlib_frame_queue_dequeue (int thread_id,
586 vlib_main_t * vm, vlib_node_main_t * nm)
588 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
592 vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
593 u32 frame_queue_index, vlib_frame_t * frame,
594 vlib_frame_queue_msg_type_t type)
596 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
597 vlib_frame_queue_elt_t *elt;
600 u64 before = clib_cpu_time_now ();
604 new_tail = __sync_add_and_fetch (&fq->tail, 1);
606 /* Wait until a ring slot is available */
607 while (new_tail >= fq->head + fq->nelts)
609 f64 b4 = vlib_time_now_ticks (vm, before);
610 vlib_worker_thread_barrier_check (vm, b4);
611 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
612 // vlib_frame_queue_dequeue (vm->thread_index, vm, nm);
615 elt = fq->elts + (new_tail & (fq->nelts - 1));
617 /* this would be very bad... */
622 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
623 save_count = frame->n_vectors;
626 elt->node_runtime_index = node_runtime_index;
627 elt->msg_type = type;
628 CLIB_MEMORY_BARRIER ();
635 /* To be called by vlib worker threads upon startup */
637 vlib_worker_thread_init (vlib_worker_thread_t * w)
639 vlib_thread_main_t *tm = vlib_get_thread_main ();
642 * Note: disabling signals in worker threads as follows
643 * prevents the api post-mortem dump scheme from working
647 * pthread_sigmask (SIG_SETMASK, &s, 0);
651 clib_mem_set_heap (w->thread_mheap);
653 if (vec_len (tm->thread_prefix) && w->registration->short_name)
655 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
656 w->registration->short_name, w->instance_id, '\0');
657 vlib_set_thread_name ((char *) w->name);
660 if (!w->registration->use_pthreads)
663 /* Initial barrier sync, for both worker and i/o threads */
664 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
666 while (*vlib_worker_threads->wait_at_barrier)
669 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
674 vlib_worker_thread_bootstrap_fn (void *arg)
677 vlib_worker_thread_t *w = arg;
679 w->lwp = syscall (SYS_gettid);
680 w->thread_id = pthread_self ();
682 __os_thread_index = w - vlib_worker_threads;
684 rv = (void *) clib_calljmp
685 ((uword (*)(uword)) w->thread_function,
686 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
687 /* NOTREACHED, we hope */
691 static clib_error_t *
692 vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned lcore_id)
694 vlib_thread_main_t *tm = &vlib_thread_main;
695 void *(*fp_arg) (void *) = fp;
697 w->lcore_id = lcore_id;
698 if (tm->cb.vlib_launch_thread_cb && !w->registration->use_pthreads)
699 return tm->cb.vlib_launch_thread_cb (fp, (void *) w, lcore_id);
705 CPU_SET (lcore_id, &cpuset);
707 if (pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w))
708 return clib_error_return_unix (0, "pthread_create");
710 if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
711 return clib_error_return_unix (0, "pthread_setaffinity_np");
717 static clib_error_t *
718 start_workers (vlib_main_t * vm)
721 vlib_worker_thread_t *w;
722 vlib_main_t *vm_clone;
724 vlib_thread_main_t *tm = &vlib_thread_main;
725 vlib_thread_registration_t *tr;
726 vlib_node_runtime_t *rt;
727 u32 n_vlib_mains = tm->n_vlib_mains;
728 u32 worker_thread_index;
729 u8 *main_heap = clib_mem_get_per_cpu_heap ();
730 mheap_t *main_heap_header = mheap_header (main_heap);
732 vec_reset_length (vlib_worker_threads);
734 /* Set up the main thread */
735 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
736 w->elog_track.name = "main thread";
737 elog_track_register (&vm->elog_main, &w->elog_track);
739 if (vec_len (tm->thread_prefix))
741 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
742 vlib_set_thread_name ((char *) w->name);
746 * Truth of the matter: we always use at least two
747 * threads. So, make the main heap thread-safe
748 * and make the event log thread-safe.
750 main_heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
752 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
753 vm->elog_main.lock[0] = 0;
755 if (n_vlib_mains > 1)
757 /* Replace hand-crafted length-1 vector with a real vector */
760 vec_validate_aligned (vlib_mains, tm->n_vlib_mains - 1,
761 CLIB_CACHE_LINE_BYTES);
762 _vec_len (vlib_mains) = 0;
763 vec_add1_aligned (vlib_mains, vm, CLIB_CACHE_LINE_BYTES);
765 vlib_worker_threads->wait_at_barrier =
766 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
767 vlib_worker_threads->workers_at_barrier =
768 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
770 vlib_worker_threads->node_reforks_required =
771 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
773 /* Ask for an initial barrier sync */
774 *vlib_worker_threads->workers_at_barrier = 0;
775 *vlib_worker_threads->wait_at_barrier = 1;
777 /* Without update or refork */
778 *vlib_worker_threads->node_reforks_required = 0;
779 vm->need_vlib_worker_thread_node_runtime_update = 0;
782 vm->barrier_epoch = 0;
783 vm->barrier_no_close_before = 0;
785 worker_thread_index = 1;
787 for (i = 0; i < vec_len (tm->registrations); i++)
789 vlib_node_main_t *nm, *nm_clone;
790 vlib_buffer_free_list_t *fl_clone, *fl_orig;
791 vlib_buffer_free_list_t *orig_freelist_pool;
794 tr = tm->registrations[i];
799 for (k = 0; k < tr->count; k++)
803 vec_add2 (vlib_worker_threads, w, 1);
806 mheap_alloc (0 /* use VM */ , tr->mheap_size);
808 w->thread_mheap = main_heap;
811 vlib_thread_stack_init (w - vlib_worker_threads);
812 w->thread_function = tr->function;
813 w->thread_function_arg = w;
815 w->registration = tr;
818 (char *) format (0, "%s %d", tr->name, k + 1);
819 vec_add1 (w->elog_track.name, 0);
820 elog_track_register (&vm->elog_main, &w->elog_track);
822 if (tr->no_data_structure_clone)
825 /* Fork vlib_global_main et al. Look for bugs here */
826 oldheap = clib_mem_set_heap (w->thread_mheap);
828 vm_clone = clib_mem_alloc_aligned (sizeof (*vm_clone),
829 CLIB_CACHE_LINE_BYTES);
830 clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
832 vm_clone->thread_index = worker_thread_index;
833 vm_clone->heap_base = w->thread_mheap;
834 vm_clone->init_functions_called =
835 hash_create (0, /* value bytes */ 0);
836 vm_clone->pending_rpc_requests = 0;
837 vec_validate (vm_clone->pending_rpc_requests, 0);
838 _vec_len (vm_clone->pending_rpc_requests) = 0;
839 memset (&vm_clone->random_buffer, 0,
840 sizeof (vm_clone->random_buffer));
842 nm = &vlib_mains[0]->node_main;
843 nm_clone = &vm_clone->node_main;
844 /* fork next frames array, preserving node runtime indices */
845 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
846 CLIB_CACHE_LINE_BYTES);
847 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
849 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
850 u32 save_node_runtime_index;
853 save_node_runtime_index = nf->node_runtime_index;
854 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
855 vlib_next_frame_init (nf);
856 nf->node_runtime_index = save_node_runtime_index;
857 nf->flags = save_flags;
860 /* fork the frame dispatch queue */
861 nm_clone->pending_frames = 0;
862 vec_validate (nm_clone->pending_frames, 10); /* $$$$$?????? */
863 _vec_len (nm_clone->pending_frames) = 0;
868 /* Allocate all nodes in single block for speed */
869 n = clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*n));
871 for (j = 0; j < vec_len (nm->nodes); j++)
873 clib_memcpy (n, nm->nodes[j], sizeof (*n));
874 /* none of the copied nodes have enqueue rights given out */
875 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
876 memset (&n->stats_total, 0, sizeof (n->stats_total));
877 memset (&n->stats_last_clear, 0,
878 sizeof (n->stats_last_clear));
879 vec_add1 (nm_clone->nodes, n);
882 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
883 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
884 CLIB_CACHE_LINE_BYTES);
886 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
888 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
889 rt->thread_index = vm_clone->thread_index;
890 /* copy initial runtime_data from node */
891 if (n->runtime_data && n->runtime_data_bytes > 0)
892 clib_memcpy (rt->runtime_data, n->runtime_data,
893 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
894 n->runtime_data_bytes));
897 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
898 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
899 CLIB_CACHE_LINE_BYTES);
900 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
902 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
903 rt->thread_index = vm_clone->thread_index;
904 /* copy initial runtime_data from node */
905 if (n->runtime_data && n->runtime_data_bytes > 0)
906 clib_memcpy (rt->runtime_data, n->runtime_data,
907 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
908 n->runtime_data_bytes));
911 nm_clone->processes = vec_dup_aligned (nm->processes,
912 CLIB_CACHE_LINE_BYTES);
914 /* zap the (per worker) frame freelists, etc */
915 nm_clone->frame_sizes = 0;
916 nm_clone->frame_size_hash = hash_create (0, sizeof (uword));
918 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
920 clib_mem_set_heap (oldheap);
921 vec_add1_aligned (vlib_mains, vm_clone, CLIB_CACHE_LINE_BYTES);
923 vm_clone->error_main.counters = vec_dup_aligned
924 (vlib_mains[0]->error_main.counters, CLIB_CACHE_LINE_BYTES);
925 vm_clone->error_main.counters_last_clear = vec_dup_aligned
926 (vlib_mains[0]->error_main.counters_last_clear,
927 CLIB_CACHE_LINE_BYTES);
929 /* Fork the vlib_buffer_main_t free lists, etc. */
930 orig_freelist_pool = vm_clone->buffer_free_list_pool;
931 vm_clone->buffer_free_list_pool = 0;
934 pool_foreach (fl_orig, orig_freelist_pool,
936 pool_get_aligned (vm_clone->buffer_free_list_pool,
937 fl_clone, CLIB_CACHE_LINE_BYTES);
938 ASSERT (fl_orig - orig_freelist_pool
939 == fl_clone - vm_clone->buffer_free_list_pool);
941 fl_clone[0] = fl_orig[0];
942 fl_clone->buffers = 0;
943 fl_clone->n_alloc = 0;
947 worker_thread_index++;
953 /* only have non-data-structure copy threads to create... */
954 for (i = 0; i < vec_len (tm->registrations); i++)
956 tr = tm->registrations[i];
958 for (j = 0; j < tr->count; j++)
960 vec_add2 (vlib_worker_threads, w, 1);
963 mheap_alloc (0 /* use VM */ , tr->mheap_size);
965 w->thread_mheap = main_heap;
967 vlib_thread_stack_init (w - vlib_worker_threads);
968 w->thread_function = tr->function;
969 w->thread_function_arg = w;
972 (char *) format (0, "%s %d", tr->name, j + 1);
973 w->registration = tr;
974 vec_add1 (w->elog_track.name, 0);
975 elog_track_register (&vm->elog_main, &w->elog_track);
980 worker_thread_index = 1;
982 for (i = 0; i < vec_len (tm->registrations); i++)
987 tr = tm->registrations[i];
989 if (tr->use_pthreads || tm->use_pthreads)
991 for (j = 0; j < tr->count; j++)
993 w = vlib_worker_threads + worker_thread_index++;
994 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
997 clib_error_report (err);
1004 clib_bitmap_foreach (c, tr->coremask, ({
1005 w = vlib_worker_threads + worker_thread_index++;
1006 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
1009 clib_error_report (err);
1014 vlib_worker_thread_barrier_sync (vm);
1015 vlib_worker_thread_barrier_release (vm);
1019 VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
1023 worker_thread_node_runtime_update_internal (void)
1027 vlib_node_main_t *nm, *nm_clone;
1028 vlib_main_t *vm_clone;
1029 vlib_node_runtime_t *rt;
1031 vlib_node_runtime_sync_stats (vlib_main_t * vm,
1032 vlib_node_runtime_t * r,
1034 uword n_vectors, uword n_clocks);
1036 ASSERT (vlib_get_thread_index () == 0);
1039 nm = &vm->node_main;
1041 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
1044 * Scrape all runtime stats, so we don't lose node runtime(s) with
1045 * pending counts, or throw away worker / io thread counts.
1047 for (j = 0; j < vec_len (nm->nodes); j++)
1051 vlib_node_sync_stats (vm, n);
1054 for (i = 1; i < vec_len (vlib_mains); i++)
1058 vm_clone = vlib_mains[i];
1059 nm_clone = &vm_clone->node_main;
1061 for (j = 0; j < vec_len (nm_clone->nodes); j++)
1063 n = nm_clone->nodes[j];
1065 rt = vlib_node_get_runtime (vm_clone, n->index);
1066 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
1070 /* Per-worker clone rebuilds are now done on each thread */
1075 vlib_worker_thread_node_refork (void)
1077 vlib_main_t *vm, *vm_clone;
1078 vlib_node_main_t *nm, *nm_clone;
1079 vlib_node_t **old_nodes_clone;
1080 vlib_node_runtime_t *rt, *old_rt;
1082 vlib_node_t *new_n_clone;
1087 nm = &vm->node_main;
1088 vm_clone = vlib_get_main ();
1089 nm_clone = &vm_clone->node_main;
1091 /* Re-clone error heap */
1092 u64 *old_counters = vm_clone->error_main.counters;
1093 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
1095 clib_memcpy (&vm_clone->error_main, &vm->error_main,
1096 sizeof (vm->error_main));
1097 j = vec_len (vm->error_main.counters) - 1;
1098 vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES);
1099 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
1100 vm_clone->error_main.counters = old_counters;
1101 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
1103 nm_clone = &vm_clone->node_main;
1104 vec_free (nm_clone->next_frames);
1105 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
1106 CLIB_CACHE_LINE_BYTES);
1108 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
1110 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
1111 u32 save_node_runtime_index;
1114 save_node_runtime_index = nf->node_runtime_index;
1115 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
1116 vlib_next_frame_init (nf);
1117 nf->node_runtime_index = save_node_runtime_index;
1118 nf->flags = save_flags;
1121 old_nodes_clone = nm_clone->nodes;
1122 nm_clone->nodes = 0;
1126 /* Allocate all nodes in single block for speed */
1128 clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*new_n_clone));
1129 for (j = 0; j < vec_len (nm->nodes); j++)
1131 vlib_node_t *old_n_clone;
1134 new_n = nm->nodes[j];
1135 old_n_clone = old_nodes_clone[j];
1137 clib_memcpy (new_n_clone, new_n, sizeof (*new_n));
1138 /* none of the copied nodes have enqueue rights given out */
1139 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
1141 if (j >= vec_len (old_nodes_clone))
1143 /* new node, set to zero */
1144 memset (&new_n_clone->stats_total, 0,
1145 sizeof (new_n_clone->stats_total));
1146 memset (&new_n_clone->stats_last_clear, 0,
1147 sizeof (new_n_clone->stats_last_clear));
1151 /* Copy stats if the old data is valid */
1152 clib_memcpy (&new_n_clone->stats_total,
1153 &old_n_clone->stats_total,
1154 sizeof (new_n_clone->stats_total));
1155 clib_memcpy (&new_n_clone->stats_last_clear,
1156 &old_n_clone->stats_last_clear,
1157 sizeof (new_n_clone->stats_last_clear));
1159 /* keep previous node state */
1160 new_n_clone->state = old_n_clone->state;
1162 vec_add1 (nm_clone->nodes, new_n_clone);
1165 /* Free the old node clones */
1166 clib_mem_free (old_nodes_clone[0]);
1168 vec_free (old_nodes_clone);
1171 /* re-clone internal nodes */
1172 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL];
1173 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
1174 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
1175 CLIB_CACHE_LINE_BYTES);
1177 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
1179 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1180 rt->thread_index = vm_clone->thread_index;
1181 /* copy runtime_data, will be overwritten later for existing rt */
1182 if (n->runtime_data && n->runtime_data_bytes > 0)
1183 clib_memcpy (rt->runtime_data, n->runtime_data,
1184 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1185 n->runtime_data_bytes));
1188 for (j = 0; j < vec_len (old_rt); j++)
1190 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1191 rt->state = old_rt[j].state;
1192 clib_memcpy (rt->runtime_data, old_rt[j].runtime_data,
1193 VLIB_NODE_RUNTIME_DATA_SIZE);
1198 /* re-clone input nodes */
1199 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
1200 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
1201 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
1202 CLIB_CACHE_LINE_BYTES);
1204 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
1206 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1207 rt->thread_index = vm_clone->thread_index;
1208 /* copy runtime_data, will be overwritten later for existing rt */
1209 if (n->runtime_data && n->runtime_data_bytes > 0)
1210 clib_memcpy (rt->runtime_data, n->runtime_data,
1211 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1212 n->runtime_data_bytes));
1215 for (j = 0; j < vec_len (old_rt); j++)
1217 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1218 rt->state = old_rt[j].state;
1219 clib_memcpy (rt->runtime_data, old_rt[j].runtime_data,
1220 VLIB_NODE_RUNTIME_DATA_SIZE);
1225 nm_clone->processes = vec_dup_aligned (nm->processes,
1226 CLIB_CACHE_LINE_BYTES);
1230 vlib_worker_thread_node_runtime_update (void)
1233 * Make a note that we need to do a node runtime update
1234 * prior to releasing the barrier.
1236 vlib_global_main.need_vlib_worker_thread_node_runtime_update = 1;
1240 unformat_sched_policy (unformat_input_t * input, va_list * args)
1242 u32 *r = va_arg (*args, u32 *);
1245 #define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
1246 foreach_sched_policy
1253 static clib_error_t *
1254 cpu_config (vlib_main_t * vm, unformat_input_t * input)
1256 vlib_thread_registration_t *tr;
1258 vlib_thread_main_t *tm = &vlib_thread_main;
1264 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
1266 tm->n_thread_stacks = 1; /* account for main thread */
1267 tm->sched_policy = ~0;
1268 tm->sched_priority = ~0;
1269 tm->main_lcore = ~0;
1275 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
1279 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1281 if (unformat (input, "use-pthreads"))
1282 tm->use_pthreads = 1;
1283 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
1285 else if (unformat (input, "main-core %u", &tm->main_lcore))
1287 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
1289 else if (unformat (input, "coremask-%s %llx", &name, &coremask))
1291 p = hash_get_mem (tm->thread_registrations_by_name, name);
1293 return clib_error_return (0, "no such thread type '%s'", name);
1295 tr = (vlib_thread_registration_t *) p[0];
1297 if (tr->use_pthreads)
1298 return clib_error_return (0,
1299 "coremask cannot be set for '%s' threads",
1302 tr->coremask = clib_bitmap_set_multiple
1303 (tr->coremask, 0, coremask, BITS (coremask));
1304 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1306 else if (unformat (input, "corelist-%s %U", &name, unformat_bitmap_list,
1309 p = hash_get_mem (tm->thread_registrations_by_name, name);
1311 return clib_error_return (0, "no such thread type '%s'", name);
1313 tr = (vlib_thread_registration_t *) p[0];
1315 if (tr->use_pthreads)
1316 return clib_error_return (0,
1317 "corelist cannot be set for '%s' threads",
1320 tr->coremask = bitmap;
1321 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1325 (input, "scheduler-policy %U", unformat_sched_policy,
1328 else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
1330 else if (unformat (input, "%s %u", &name, &count))
1332 p = hash_get_mem (tm->thread_registrations_by_name, name);
1334 return clib_error_return (0, "no such thread type 3 '%s'", name);
1336 tr = (vlib_thread_registration_t *) p[0];
1337 if (tr->fixed_count)
1338 return clib_error_return
1339 (0, "number of %s threads not configurable", tr->name);
1346 if (tm->sched_priority != ~0)
1348 if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
1350 u32 prio_max = sched_get_priority_max (tm->sched_policy);
1351 u32 prio_min = sched_get_priority_min (tm->sched_policy);
1352 if (tm->sched_priority > prio_max)
1353 tm->sched_priority = prio_max;
1354 if (tm->sched_priority < prio_min)
1355 tm->sched_priority = prio_min;
1359 return clib_error_return
1361 "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1362 tm->sched_priority);
1367 if (!tm->thread_prefix)
1368 tm->thread_prefix = format (0, "vpp");
1372 tm->n_thread_stacks += tr->count;
1373 tm->n_pthreads += tr->count * tr->use_pthreads;
1374 tm->n_threads += tr->count * (tr->use_pthreads == 0);
1381 VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1383 #if !defined (__x86_64__) && !defined (__i386__) && !defined (__aarch64__) && !defined (__powerpc64__) && !defined(__arm__)
1385 __sync_fetch_and_add_8 (void)
1387 fformat (stderr, "%s called\n", __FUNCTION__);
1392 __sync_add_and_fetch_8 (void)
1394 fformat (stderr, "%s called\n", __FUNCTION__);
1399 void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
1401 vnet_main_fixup (vlib_fork_fixup_t which)
1406 vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1408 vlib_main_t *vm = vlib_get_main ();
1410 if (vlib_mains == 0)
1413 ASSERT (vlib_get_thread_index () == 0);
1414 vlib_worker_thread_barrier_sync (vm);
1418 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1419 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1425 vlib_worker_thread_barrier_release (vm);
1429 * Enforce minimum open time to minimize packet loss due to Rx overflow,
1430 * based on a test based heuristic that barrier should be open for at least
1431 * 3 time as long as it is closed (with an upper bound of 1ms because by that
1432 * point it is probably too late to make a difference)
1435 #ifndef BARRIER_MINIMUM_OPEN_LIMIT
1436 #define BARRIER_MINIMUM_OPEN_LIMIT 0.001
1439 #ifndef BARRIER_MINIMUM_OPEN_FACTOR
1440 #define BARRIER_MINIMUM_OPEN_FACTOR 3
1444 vlib_worker_thread_barrier_sync_int (vlib_main_t * vm)
1453 if (vec_len (vlib_mains) < 2)
1456 ASSERT (vlib_get_thread_index () == 0);
1458 count = vec_len (vlib_mains) - 1;
1460 /* Record entry relative to last close */
1461 now = vlib_time_now (vm);
1462 t_entry = now - vm->barrier_epoch;
1464 /* Tolerate recursive calls */
1465 if (++vlib_worker_threads[0].recursion_level > 1)
1467 barrier_trace_sync_rec (t_entry);
1471 vlib_worker_threads[0].barrier_sync_count++;
1473 /* Enforce minimum barrier open time to minimize packet loss */
1474 ASSERT (vm->barrier_no_close_before <= (now + BARRIER_MINIMUM_OPEN_LIMIT));
1475 while ((now = vlib_time_now (vm)) < vm->barrier_no_close_before)
1478 /* Record time of closure */
1479 t_open = now - vm->barrier_epoch;
1480 vm->barrier_epoch = now;
1482 deadline = now + BARRIER_SYNC_TIMEOUT;
1484 *vlib_worker_threads->wait_at_barrier = 1;
1485 while (*vlib_worker_threads->workers_at_barrier != count)
1487 if ((now = vlib_time_now (vm)) > deadline)
1489 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1494 t_closed = now - vm->barrier_epoch;
1496 barrier_trace_sync (t_entry, t_open, t_closed);
1500 void vlib_stat_segment_lock (void) __attribute__ ((weak));
1502 vlib_stat_segment_lock (void)
1506 void vlib_stat_segment_unlock (void) __attribute__ ((weak));
1508 vlib_stat_segment_unlock (void)
1513 vlib_worker_thread_barrier_release (vlib_main_t * vm)
1520 f64 t_update_main = 0.0;
1521 int refork_needed = 0;
1523 if (vec_len (vlib_mains) < 2)
1526 ASSERT (vlib_get_thread_index () == 0);
1529 now = vlib_time_now (vm);
1530 t_entry = now - vm->barrier_epoch;
1532 if (--vlib_worker_threads[0].recursion_level > 0)
1534 barrier_trace_release_rec (t_entry);
1538 /* Update (all) node runtimes before releasing the barrier, if needed */
1539 if (vm->need_vlib_worker_thread_node_runtime_update)
1542 * Lock stat segment here, so we's safe when
1543 * rebuilding the stat segment node clones from the
1546 vlib_stat_segment_lock ();
1548 /* Do stats elements on main thread */
1549 worker_thread_node_runtime_update_internal ();
1550 vm->need_vlib_worker_thread_node_runtime_update = 0;
1552 /* Do per thread rebuilds in parallel */
1554 clib_smp_atomic_add (vlib_worker_threads->node_reforks_required,
1555 (vec_len (vlib_mains) - 1));
1556 now = vlib_time_now (vm);
1557 t_update_main = now - vm->barrier_epoch;
1560 deadline = now + BARRIER_SYNC_TIMEOUT;
1562 *vlib_worker_threads->wait_at_barrier = 0;
1564 while (*vlib_worker_threads->workers_at_barrier > 0)
1566 if ((now = vlib_time_now (vm)) > deadline)
1568 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1573 /* Wait for reforks before continuing */
1576 now = vlib_time_now (vm);
1578 deadline = now + BARRIER_SYNC_TIMEOUT;
1580 while (*vlib_worker_threads->node_reforks_required > 0)
1582 if ((now = vlib_time_now (vm)) > deadline)
1584 fformat (stderr, "%s: worker thread refork deadlock\n",
1589 vlib_stat_segment_unlock ();
1592 t_closed_total = now - vm->barrier_epoch;
1594 minimum_open = t_closed_total * BARRIER_MINIMUM_OPEN_FACTOR;
1596 if (minimum_open > BARRIER_MINIMUM_OPEN_LIMIT)
1598 minimum_open = BARRIER_MINIMUM_OPEN_LIMIT;
1601 vm->barrier_no_close_before = now + minimum_open;
1603 /* Record barrier epoch (used to enforce minimum open time) */
1604 vm->barrier_epoch = now;
1606 barrier_trace_release (t_entry, t_closed_total, t_update_main);
1611 * Check the frame queue to see if any frames are available.
1612 * If so, pull the packets off the frames and put them to
1616 vlib_frame_queue_dequeue (vlib_main_t * vm, vlib_frame_queue_main_t * fqm)
1618 u32 thread_id = vm->thread_index;
1619 vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id];
1620 vlib_frame_queue_elt_t *elt;
1629 ASSERT (vm == vlib_mains[thread_id]);
1631 if (PREDICT_FALSE (fqm->node_index == ~0))
1634 * Gather trace data for frame queues
1636 if (PREDICT_FALSE (fq->trace))
1638 frame_queue_trace_t *fqt;
1639 frame_queue_nelt_counter_t *fqh;
1642 fqt = &fqm->frame_queue_traces[thread_id];
1644 fqt->nelts = fq->nelts;
1645 fqt->head = fq->head;
1646 fqt->head_hint = fq->head_hint;
1647 fqt->tail = fq->tail;
1648 fqt->threshold = fq->vector_threshold;
1649 fqt->n_in_use = fqt->tail - fqt->head;
1650 if (fqt->n_in_use >= fqt->nelts)
1652 // if beyond max then use max
1653 fqt->n_in_use = fqt->nelts - 1;
1656 /* Record the number of elements in use in the histogram */
1657 fqh = &fqm->frame_queue_histogram[thread_id];
1658 fqh->count[fqt->n_in_use]++;
1660 /* Record a snapshot of the elements in use */
1661 for (elix = 0; elix < fqt->nelts; elix++)
1663 elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1664 if (1 || elt->valid)
1666 fqt->n_vectors[elix] = elt->n_vectors;
1674 if (fq->head == fq->tail)
1676 fq->head_hint = fq->head;
1680 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
1684 fq->head_hint = fq->head;
1688 from = elt->buffer_index;
1689 msg_type = elt->msg_type;
1691 ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
1692 ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1694 f = vlib_get_frame_to_node (vm, fqm->node_index);
1696 to = vlib_frame_vector_args (f);
1698 n_left_to_node = elt->n_vectors;
1700 while (n_left_to_node >= 4)
1708 n_left_to_node -= 4;
1711 while (n_left_to_node > 0)
1719 vectors += elt->n_vectors;
1720 f->n_vectors = elt->n_vectors;
1721 vlib_put_frame_to_node (vm, fqm->node_index, f);
1725 elt->msg_type = 0xfefefefe;
1726 CLIB_MEMORY_BARRIER ();
1731 * Limit the number of packets pushed into the graph
1733 if (vectors >= fq->vector_threshold)
1735 fq->head_hint = fq->head;
1744 vlib_worker_thread_fn (void *arg)
1746 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
1747 vlib_thread_main_t *tm = vlib_get_thread_main ();
1748 vlib_main_t *vm = vlib_get_main ();
1751 ASSERT (vm->thread_index == vlib_get_thread_index ());
1753 vlib_worker_thread_init (w);
1754 clib_time_init (&vm->clib_time);
1755 clib_mem_set_heap (w->thread_mheap);
1757 /* Wait until the dpdk init sequence is complete */
1758 while (tm->extern_thread_mgmt && tm->worker_thread_release == 0)
1759 vlib_worker_thread_barrier_check ();
1761 e = vlib_call_init_exit_functions
1762 (vm, vm->worker_init_function_registrations, 1 /* call_once */ );
1764 clib_error_report (e);
1766 vlib_worker_loop (vm);
1770 VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1773 .function = vlib_worker_thread_fn,
1778 vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1780 vlib_thread_main_t *tm = vlib_get_thread_main ();
1781 vlib_frame_queue_main_t *fqm;
1782 vlib_frame_queue_t *fq;
1785 if (frame_queue_nelts == 0)
1786 frame_queue_nelts = FRAME_QUEUE_NELTS;
1788 vec_add2 (tm->frame_queue_mains, fqm, 1);
1790 fqm->node_index = node_index;
1792 vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1);
1793 _vec_len (fqm->vlib_frame_queues) = 0;
1794 for (i = 0; i < tm->n_vlib_mains; i++)
1796 fq = vlib_frame_queue_alloc (frame_queue_nelts);
1797 vec_add1 (fqm->vlib_frame_queues, fq);
1800 return (fqm - tm->frame_queue_mains);
1804 vlib_thread_cb_register (struct vlib_main_t *vm, vlib_thread_callbacks_t * cb)
1806 vlib_thread_main_t *tm = vlib_get_thread_main ();
1808 if (tm->extern_thread_mgmt)
1811 tm->cb.vlib_launch_thread_cb = cb->vlib_launch_thread_cb;
1812 tm->extern_thread_mgmt = 1;
1817 vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t *
1820 ASSERT (vlib_get_thread_index () == 0);
1821 vlib_process_signal_event (vlib_get_main (), args->node_index,
1822 args->type_opaque, args->data);
1825 void *rpc_call_main_thread_cb_fn;
1828 vlib_rpc_call_main_thread (void *callback, u8 * args, u32 arg_size)
1830 if (rpc_call_main_thread_cb_fn)
1832 void (*fp) (void *, u8 *, u32) = rpc_call_main_thread_cb_fn;
1833 (*fp) (callback, args, arg_size);
1836 clib_warning ("BUG: rpc_call_main_thread_cb_fn NULL!");
1840 threads_init (vlib_main_t * vm)
1845 VLIB_INIT_FUNCTION (threads_init);
1848 * fd.io coding-style-patch-verification: ON
1851 * eval: (c-set-style "gnu")