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;
38 __thread uword vlib_thread_index = 0;
41 os_get_cpu_number (void)
43 return vlib_thread_index;
51 len = vec_len (vlib_thread_stacks);
59 vlib_set_thread_name (char *name)
61 int pthread_setname_np (pthread_t __target_thread, const char *__name);
63 pthread_t thread = pthread_self ();
67 rv = pthread_setname_np (thread, name);
69 clib_warning ("pthread_setname_np returned %d", rv);
74 sort_registrations_by_no_clone (void *a0, void *a1)
76 vlib_thread_registration_t **tr0 = a0;
77 vlib_thread_registration_t **tr1 = a1;
79 return ((i32) ((*tr0)->no_data_structure_clone)
80 - ((i32) ((*tr1)->no_data_structure_clone)));
84 vlib_sysfs_list_to_bitmap (char *filename)
89 fp = fopen (filename, "r");
94 vec_validate (buffer, 256 - 1);
95 if (fgets ((char *) buffer, 256, fp))
98 unformat_init_string (&in, (char *) buffer,
99 strlen ((char *) buffer));
100 if (unformat (&in, "%U", unformat_bitmap_list, &r) != 1)
101 clib_warning ("unformat_bitmap_list failed");
111 /* Called early in the init sequence */
114 vlib_thread_init (vlib_main_t * vm)
116 vlib_thread_main_t *tm = &vlib_thread_main;
117 vlib_worker_thread_t *w;
118 vlib_thread_registration_t *tr;
119 u32 n_vlib_mains = 1;
124 /* get bitmaps of active cpu cores and sockets */
125 tm->cpu_core_bitmap =
126 vlib_sysfs_list_to_bitmap ("/sys/devices/system/cpu/online");
127 tm->cpu_socket_bitmap =
128 vlib_sysfs_list_to_bitmap ("/sys/devices/system/node/online");
130 avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
133 for (i = 0; i < tm->skip_cores; i++)
135 uword c = clib_bitmap_first_set (avail_cpu);
137 return clib_error_return (0, "no available cpus to skip");
139 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
142 /* grab cpu for main thread */
145 tm->main_lcore = clib_bitmap_first_set (avail_cpu);
146 if (tm->main_lcore == (u8) ~ 0)
147 return clib_error_return (0, "no available cpus to be used for the"
152 if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
153 return clib_error_return (0, "cpu %u is not available to be used"
154 " for the main thread", tm->main_lcore);
156 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
158 /* assume that there is socket 0 only if there is no data from sysfs */
159 if (!tm->cpu_socket_bitmap)
160 tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
162 /* pin main thread to main_lcore */
163 if (tm->cb.vlib_thread_set_lcore_cb)
165 tm->cb.vlib_thread_set_lcore_cb (0, tm->main_lcore);
171 CPU_SET (tm->main_lcore, &cpuset);
172 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
175 /* as many threads as stacks... */
176 vec_validate_aligned (vlib_worker_threads, vec_len (vlib_thread_stacks) - 1,
177 CLIB_CACHE_LINE_BYTES);
179 /* Preallocate thread 0 */
180 _vec_len (vlib_worker_threads) = 1;
181 w = vlib_worker_threads;
182 w->thread_mheap = clib_mem_get_heap ();
183 w->thread_stack = vlib_thread_stacks[0];
184 w->lcore_id = tm->main_lcore;
185 w->lwp = syscall (SYS_gettid);
186 w->thread_id = pthread_self ();
187 tm->n_vlib_mains = 1;
189 if (tm->sched_policy != ~0)
191 struct sched_param sched_param;
192 if (!sched_getparam (w->lwp, &sched_param))
194 if (tm->sched_priority != ~0)
195 sched_param.sched_priority = tm->sched_priority;
196 sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
200 /* assign threads to cores and set n_vlib_mains */
205 vec_add1 (tm->registrations, tr);
209 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
211 for (i = 0; i < vec_len (tm->registrations); i++)
214 tr = tm->registrations[i];
215 tr->first_index = first_index;
216 first_index += tr->count;
217 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
219 /* construct coremask */
220 if (tr->use_pthreads || !tr->count)
227 clib_bitmap_foreach (c, tr->coremask, ({
228 if (clib_bitmap_get(avail_cpu, c) == 0)
229 return clib_error_return (0, "cpu %u is not available to be used"
230 " for the '%s' thread",c, tr->name);
232 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
239 for (j = 0; j < tr->count; j++)
241 uword c = clib_bitmap_first_set (avail_cpu);
243 return clib_error_return (0,
244 "no available cpus to be used for"
245 " the '%s' thread", tr->name);
247 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
248 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
253 clib_bitmap_free (avail_cpu);
255 tm->n_vlib_mains = n_vlib_mains;
257 vec_validate_aligned (vlib_worker_threads, first_index - 1,
258 CLIB_CACHE_LINE_BYTES);
264 vlib_frame_queue_alloc (int nelts)
266 vlib_frame_queue_t *fq;
268 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
269 memset (fq, 0, sizeof (*fq));
271 fq->vector_threshold = 128; // packets
272 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
276 if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
277 fformat (stderr, "WARNING: fq->tail unaligned\n");
278 if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
279 fformat (stderr, "WARNING: fq->head unaligned\n");
280 if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
281 fformat (stderr, "WARNING: fq->elts unaligned\n");
283 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
284 fformat (stderr, "WARNING: fq->elts[0] size %d\n",
285 sizeof (fq->elts[0]));
286 if (nelts & (nelts - 1))
288 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
296 void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
298 vl_msg_api_handler_no_free (void *v)
302 /* Turned off, save as reference material... */
305 vlib_frame_queue_dequeue_internal (int thread_id,
306 vlib_main_t * vm, vlib_node_main_t * nm)
308 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
309 vlib_frame_queue_elt_t *elt;
311 vlib_pending_frame_t *p;
312 vlib_node_runtime_t *r;
313 u32 node_runtime_index;
318 ASSERT (vm == vlib_mains[thread_id]);
322 if (fq->head == fq->tail)
325 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
330 before = clib_cpu_time_now ();
333 node_runtime_index = elt->node_runtime_index;
334 msg_type = elt->msg_type;
338 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
339 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
340 /* note fallthrough... */
341 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
342 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
344 vlib_frame_free (vm, r, f);
346 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
347 vec_add2 (vm->node_main.pending_frames, p, 1);
348 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
349 p->node_runtime_index = elt->node_runtime_index;
350 p->frame_index = vlib_frame_index (vm, f);
351 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
352 fq->dequeue_vectors += (u64) f->n_vectors;
354 case VLIB_FRAME_QUEUE_ELT_API_MSG:
355 vl_msg_api_handler_no_free (f);
358 clib_warning ("bogus frame queue message, type %d", msg_type);
363 fq->dequeue_ticks += clib_cpu_time_now () - before;
364 CLIB_MEMORY_BARRIER ();
373 vlib_frame_queue_dequeue (int thread_id,
374 vlib_main_t * vm, vlib_node_main_t * nm)
376 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
380 vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
381 u32 frame_queue_index, vlib_frame_t * frame,
382 vlib_frame_queue_msg_type_t type)
384 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
385 vlib_frame_queue_elt_t *elt;
388 u64 before = clib_cpu_time_now ();
392 new_tail = __sync_add_and_fetch (&fq->tail, 1);
394 /* Wait until a ring slot is available */
395 while (new_tail >= fq->head + fq->nelts)
397 f64 b4 = vlib_time_now_ticks (vm, before);
398 vlib_worker_thread_barrier_check (vm, b4);
399 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
400 // vlib_frame_queue_dequeue (vm->thread_index, vm, nm);
403 elt = fq->elts + (new_tail & (fq->nelts - 1));
405 /* this would be very bad... */
410 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
411 save_count = frame->n_vectors;
414 elt->node_runtime_index = node_runtime_index;
415 elt->msg_type = type;
416 CLIB_MEMORY_BARRIER ();
423 /* To be called by vlib worker threads upon startup */
425 vlib_worker_thread_init (vlib_worker_thread_t * w)
427 vlib_thread_main_t *tm = vlib_get_thread_main ();
430 * Note: disabling signals in worker threads as follows
431 * prevents the api post-mortem dump scheme from working
435 * pthread_sigmask (SIG_SETMASK, &s, 0);
439 clib_mem_set_heap (w->thread_mheap);
441 if (vec_len (tm->thread_prefix) && w->registration->short_name)
443 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
444 w->registration->short_name, w->instance_id, '\0');
445 vlib_set_thread_name ((char *) w->name);
448 if (!w->registration->use_pthreads)
451 /* Initial barrier sync, for both worker and i/o threads */
452 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
454 while (*vlib_worker_threads->wait_at_barrier)
457 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
462 vlib_worker_thread_bootstrap_fn (void *arg)
465 vlib_worker_thread_t *w = arg;
467 w->lwp = syscall (SYS_gettid);
468 w->thread_id = pthread_self ();
470 vlib_thread_index = w - vlib_worker_threads;
472 rv = (void *) clib_calljmp
473 ((uword (*)(uword)) w->thread_function,
474 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
475 /* NOTREACHED, we hope */
479 static clib_error_t *
480 vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned lcore_id)
482 vlib_thread_main_t *tm = &vlib_thread_main;
483 void *(*fp_arg) (void *) = fp;
485 w->lcore_id = lcore_id;
486 if (tm->cb.vlib_launch_thread_cb && !w->registration->use_pthreads)
487 return tm->cb.vlib_launch_thread_cb (fp, (void *) w, lcore_id);
493 CPU_SET (lcore_id, &cpuset);
495 if (pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w))
496 return clib_error_return_unix (0, "pthread_create");
498 if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
499 return clib_error_return_unix (0, "pthread_setaffinity_np");
505 static clib_error_t *
506 start_workers (vlib_main_t * vm)
509 vlib_worker_thread_t *w;
510 vlib_main_t *vm_clone;
512 vlib_thread_main_t *tm = &vlib_thread_main;
513 vlib_thread_registration_t *tr;
514 vlib_node_runtime_t *rt;
515 u32 n_vlib_mains = tm->n_vlib_mains;
516 u32 worker_thread_index;
517 u8 *main_heap = clib_mem_get_per_cpu_heap ();
518 mheap_t *main_heap_header = mheap_header (main_heap);
520 vec_reset_length (vlib_worker_threads);
522 /* Set up the main thread */
523 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
524 w->elog_track.name = "main thread";
525 elog_track_register (&vm->elog_main, &w->elog_track);
527 if (vec_len (tm->thread_prefix))
529 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
530 vlib_set_thread_name ((char *) w->name);
534 * Truth of the matter: we always use at least two
535 * threads. So, make the main heap thread-safe
536 * and make the event log thread-safe.
538 main_heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
540 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
541 vm->elog_main.lock[0] = 0;
543 if (n_vlib_mains > 1)
545 /* Replace hand-crafted length-1 vector with a real vector */
548 vec_validate_aligned (vlib_mains, tm->n_vlib_mains - 1,
549 CLIB_CACHE_LINE_BYTES);
550 _vec_len (vlib_mains) = 0;
551 vec_add1_aligned (vlib_mains, vm, CLIB_CACHE_LINE_BYTES);
553 vlib_worker_threads->wait_at_barrier =
554 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
555 vlib_worker_threads->workers_at_barrier =
556 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
558 /* Ask for an initial barrier sync */
559 *vlib_worker_threads->workers_at_barrier = 0;
560 *vlib_worker_threads->wait_at_barrier = 1;
562 worker_thread_index = 1;
564 for (i = 0; i < vec_len (tm->registrations); i++)
566 vlib_node_main_t *nm, *nm_clone;
567 vlib_buffer_main_t *bm_clone;
568 vlib_buffer_free_list_t *fl_clone, *fl_orig;
569 vlib_buffer_free_list_t *orig_freelist_pool;
572 tr = tm->registrations[i];
577 for (k = 0; k < tr->count; k++)
579 vec_add2 (vlib_worker_threads, w, 1);
582 mheap_alloc (0 /* use VM */ , tr->mheap_size);
584 w->thread_mheap = main_heap;
587 vlib_thread_stack_init (w - vlib_worker_threads);
588 w->thread_function = tr->function;
589 w->thread_function_arg = w;
591 w->registration = tr;
594 (char *) format (0, "%s %d", tr->name, k + 1);
595 vec_add1 (w->elog_track.name, 0);
596 elog_track_register (&vm->elog_main, &w->elog_track);
598 if (tr->no_data_structure_clone)
601 /* Fork vlib_global_main et al. Look for bugs here */
602 oldheap = clib_mem_set_heap (w->thread_mheap);
604 vm_clone = clib_mem_alloc (sizeof (*vm_clone));
605 clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
607 vm_clone->thread_index = worker_thread_index;
608 vm_clone->heap_base = w->thread_mheap;
609 vm_clone->mbuf_alloc_list = 0;
610 vm_clone->init_functions_called =
611 hash_create (0, /* value bytes */ 0);
612 memset (&vm_clone->random_buffer, 0,
613 sizeof (vm_clone->random_buffer));
615 nm = &vlib_mains[0]->node_main;
616 nm_clone = &vm_clone->node_main;
617 /* fork next frames array, preserving node runtime indices */
618 nm_clone->next_frames = vec_dup (nm->next_frames);
619 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
621 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
622 u32 save_node_runtime_index;
625 save_node_runtime_index = nf->node_runtime_index;
626 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
627 vlib_next_frame_init (nf);
628 nf->node_runtime_index = save_node_runtime_index;
629 nf->flags = save_flags;
632 /* fork the frame dispatch queue */
633 nm_clone->pending_frames = 0;
634 vec_validate (nm_clone->pending_frames, 10); /* $$$$$?????? */
635 _vec_len (nm_clone->pending_frames) = 0;
639 for (j = 0; j < vec_len (nm->nodes); j++)
642 n = clib_mem_alloc_no_fail (sizeof (*n));
643 clib_memcpy (n, nm->nodes[j], sizeof (*n));
644 /* none of the copied nodes have enqueue rights given out */
645 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
646 memset (&n->stats_total, 0, sizeof (n->stats_total));
647 memset (&n->stats_last_clear, 0,
648 sizeof (n->stats_last_clear));
649 vec_add1 (nm_clone->nodes, n);
651 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
652 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
653 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
655 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
656 rt->thread_index = vm_clone->thread_index;
657 /* copy initial runtime_data from node */
658 if (n->runtime_data && n->runtime_data_bytes > 0)
659 clib_memcpy (rt->runtime_data, n->runtime_data,
660 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
661 n->runtime_data_bytes));
664 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
665 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
666 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
668 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
669 rt->thread_index = vm_clone->thread_index;
670 /* copy initial runtime_data from node */
671 if (n->runtime_data && n->runtime_data_bytes > 0)
672 clib_memcpy (rt->runtime_data, n->runtime_data,
673 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
674 n->runtime_data_bytes));
677 nm_clone->processes = vec_dup (nm->processes);
679 /* zap the (per worker) frame freelists, etc */
680 nm_clone->frame_sizes = 0;
681 nm_clone->frame_size_hash = 0;
683 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
685 clib_mem_set_heap (oldheap);
686 vec_add1_aligned (vlib_mains, vm_clone, CLIB_CACHE_LINE_BYTES);
688 vm_clone->error_main.counters =
689 vec_dup (vlib_mains[0]->error_main.counters);
690 vm_clone->error_main.counters_last_clear =
691 vec_dup (vlib_mains[0]->error_main.counters_last_clear);
693 /* Fork the vlib_buffer_main_t free lists, etc. */
694 bm_clone = vec_dup (vm_clone->buffer_main);
695 vm_clone->buffer_main = bm_clone;
697 orig_freelist_pool = bm_clone->buffer_free_list_pool;
698 bm_clone->buffer_free_list_pool = 0;
701 pool_foreach (fl_orig, orig_freelist_pool,
703 pool_get_aligned (bm_clone->buffer_free_list_pool,
704 fl_clone, CLIB_CACHE_LINE_BYTES);
705 ASSERT (fl_orig - orig_freelist_pool
706 == fl_clone - bm_clone->buffer_free_list_pool);
708 fl_clone[0] = fl_orig[0];
709 fl_clone->buffers = 0;
710 fl_clone->n_alloc = 0;
714 worker_thread_index++;
720 /* only have non-data-structure copy threads to create... */
721 for (i = 0; i < vec_len (tm->registrations); i++)
723 tr = tm->registrations[i];
725 for (j = 0; j < tr->count; j++)
727 vec_add2 (vlib_worker_threads, w, 1);
730 mheap_alloc (0 /* use VM */ , tr->mheap_size);
732 w->thread_mheap = main_heap;
734 vlib_thread_stack_init (w - vlib_worker_threads);
735 w->thread_function = tr->function;
736 w->thread_function_arg = w;
739 (char *) format (0, "%s %d", tr->name, j + 1);
740 w->registration = tr;
741 vec_add1 (w->elog_track.name, 0);
742 elog_track_register (&vm->elog_main, &w->elog_track);
747 worker_thread_index = 1;
749 for (i = 0; i < vec_len (tm->registrations); i++)
754 tr = tm->registrations[i];
756 if (tr->use_pthreads || tm->use_pthreads)
758 for (j = 0; j < tr->count; j++)
760 w = vlib_worker_threads + worker_thread_index++;
761 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
764 clib_error_report (err);
771 clib_bitmap_foreach (c, tr->coremask, ({
772 w = vlib_worker_threads + worker_thread_index++;
773 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
776 clib_error_report (err);
781 vlib_worker_thread_barrier_sync (vm);
782 vlib_worker_thread_barrier_release (vm);
786 VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
789 vlib_worker_thread_node_runtime_update (void)
792 vlib_worker_thread_t *w;
794 vlib_node_main_t *nm, *nm_clone;
795 vlib_node_t **old_nodes_clone;
796 vlib_main_t *vm_clone;
797 vlib_node_runtime_t *rt, *old_rt;
800 vlib_node_runtime_sync_stats (vlib_main_t * vm,
801 vlib_node_runtime_t * r,
803 uword n_vectors, uword n_clocks);
805 ASSERT (vlib_get_thread_index () == 0);
807 if (vec_len (vlib_mains) == 1)
813 ASSERT (vlib_get_thread_index () == 0);
814 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
817 * Scrape all runtime stats, so we don't lose node runtime(s) with
818 * pending counts, or throw away worker / io thread counts.
820 for (j = 0; j < vec_len (nm->nodes); j++)
824 vlib_node_sync_stats (vm, n);
827 for (i = 1; i < vec_len (vlib_mains); i++)
831 vm_clone = vlib_mains[i];
832 nm_clone = &vm_clone->node_main;
834 for (j = 0; j < vec_len (nm_clone->nodes); j++)
836 n = nm_clone->nodes[j];
838 rt = vlib_node_get_runtime (vm_clone, n->index);
839 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
843 for (i = 1; i < vec_len (vlib_mains); i++)
845 vlib_node_runtime_t *rt;
846 w = vlib_worker_threads + i;
847 oldheap = clib_mem_set_heap (w->thread_mheap);
849 vm_clone = vlib_mains[i];
851 /* Re-clone error heap */
852 u64 *old_counters = vm_clone->error_main.counters;
853 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
854 clib_memcpy (&vm_clone->error_main, &vm->error_main,
855 sizeof (vm->error_main));
856 j = vec_len (vm->error_main.counters) - 1;
857 vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES);
858 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
859 vm_clone->error_main.counters = old_counters;
860 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
862 nm_clone = &vm_clone->node_main;
863 vec_free (nm_clone->next_frames);
864 nm_clone->next_frames = vec_dup (nm->next_frames);
866 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
868 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
869 u32 save_node_runtime_index;
872 save_node_runtime_index = nf->node_runtime_index;
873 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
874 vlib_next_frame_init (nf);
875 nf->node_runtime_index = save_node_runtime_index;
876 nf->flags = save_flags;
879 old_nodes_clone = nm_clone->nodes;
883 for (j = 0; j < vec_len (nm->nodes); j++)
885 vlib_node_t *old_n_clone;
886 vlib_node_t *new_n, *new_n_clone;
888 new_n = nm->nodes[j];
889 old_n_clone = old_nodes_clone[j];
891 new_n_clone = clib_mem_alloc_no_fail (sizeof (*new_n_clone));
892 clib_memcpy (new_n_clone, new_n, sizeof (*new_n));
893 /* none of the copied nodes have enqueue rights given out */
894 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
896 if (j >= vec_len (old_nodes_clone))
898 /* new node, set to zero */
899 memset (&new_n_clone->stats_total, 0,
900 sizeof (new_n_clone->stats_total));
901 memset (&new_n_clone->stats_last_clear, 0,
902 sizeof (new_n_clone->stats_last_clear));
906 /* Copy stats if the old data is valid */
907 clib_memcpy (&new_n_clone->stats_total,
908 &old_n_clone->stats_total,
909 sizeof (new_n_clone->stats_total));
910 clib_memcpy (&new_n_clone->stats_last_clear,
911 &old_n_clone->stats_last_clear,
912 sizeof (new_n_clone->stats_last_clear));
914 /* keep previous node state */
915 new_n_clone->state = old_n_clone->state;
917 vec_add1 (nm_clone->nodes, new_n_clone);
919 /* Free the old node clone */
920 for (j = 0; j < vec_len (old_nodes_clone); j++)
921 clib_mem_free (old_nodes_clone[j]);
922 vec_free (old_nodes_clone);
925 /* re-clone internal nodes */
926 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL];
927 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
928 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
930 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
932 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
933 rt->thread_index = vm_clone->thread_index;
934 /* copy runtime_data, will be overwritten later for existing rt */
935 if (n->runtime_data && n->runtime_data_bytes > 0)
936 clib_memcpy (rt->runtime_data, n->runtime_data,
937 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
938 n->runtime_data_bytes));
941 for (j = 0; j < vec_len (old_rt); j++)
943 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
944 rt->state = old_rt[j].state;
945 clib_memcpy (rt->runtime_data, old_rt[j].runtime_data,
946 VLIB_NODE_RUNTIME_DATA_SIZE);
951 /* re-clone input nodes */
952 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
953 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
954 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
956 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
958 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
959 rt->thread_index = vm_clone->thread_index;
960 /* copy runtime_data, will be overwritten later for existing rt */
961 if (n->runtime_data && n->runtime_data_bytes > 0)
962 clib_memcpy (rt->runtime_data, n->runtime_data,
963 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
964 n->runtime_data_bytes));
967 for (j = 0; j < vec_len (old_rt); j++)
969 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
970 rt->state = old_rt[j].state;
971 clib_memcpy (rt->runtime_data, old_rt[j].runtime_data,
972 VLIB_NODE_RUNTIME_DATA_SIZE);
977 nm_clone->processes = vec_dup (nm->processes);
979 clib_mem_set_heap (oldheap);
981 // vnet_main_fork_fixup (i);
986 unformat_sched_policy (unformat_input_t * input, va_list * args)
988 u32 *r = va_arg (*args, u32 *);
991 #define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
999 static clib_error_t *
1000 cpu_config (vlib_main_t * vm, unformat_input_t * input)
1002 vlib_thread_registration_t *tr;
1004 vlib_thread_main_t *tm = &vlib_thread_main;
1010 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
1012 tm->n_thread_stacks = 1; /* account for main thread */
1013 tm->sched_policy = ~0;
1014 tm->sched_priority = ~0;
1020 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
1024 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1026 if (unformat (input, "use-pthreads"))
1027 tm->use_pthreads = 1;
1028 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
1030 else if (unformat (input, "main-core %u", &tm->main_lcore))
1032 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
1034 else if (unformat (input, "coremask-%s %llx", &name, &coremask))
1036 p = hash_get_mem (tm->thread_registrations_by_name, name);
1038 return clib_error_return (0, "no such thread type '%s'", name);
1040 tr = (vlib_thread_registration_t *) p[0];
1042 if (tr->use_pthreads)
1043 return clib_error_return (0,
1044 "coremask cannot be set for '%s' threads",
1047 tr->coremask = clib_bitmap_set_multiple
1048 (tr->coremask, 0, coremask, BITS (coremask));
1049 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1051 else if (unformat (input, "corelist-%s %U", &name, unformat_bitmap_list,
1054 p = hash_get_mem (tm->thread_registrations_by_name, name);
1056 return clib_error_return (0, "no such thread type '%s'", name);
1058 tr = (vlib_thread_registration_t *) p[0];
1060 if (tr->use_pthreads)
1061 return clib_error_return (0,
1062 "corelist cannot be set for '%s' threads",
1065 tr->coremask = bitmap;
1066 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1070 (input, "scheduler-policy %U", unformat_sched_policy,
1073 else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
1075 else if (unformat (input, "%s %u", &name, &count))
1077 p = hash_get_mem (tm->thread_registrations_by_name, name);
1079 return clib_error_return (0, "no such thread type 3 '%s'", name);
1081 tr = (vlib_thread_registration_t *) p[0];
1082 if (tr->fixed_count)
1083 return clib_error_return
1084 (0, "number of %s threads not configurable", tr->name);
1091 if (tm->sched_priority != ~0)
1093 if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
1095 u32 prio_max = sched_get_priority_max (tm->sched_policy);
1096 u32 prio_min = sched_get_priority_min (tm->sched_policy);
1097 if (tm->sched_priority > prio_max)
1098 tm->sched_priority = prio_max;
1099 if (tm->sched_priority < prio_min)
1100 tm->sched_priority = prio_min;
1104 return clib_error_return
1106 "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1107 tm->sched_priority);
1112 if (!tm->thread_prefix)
1113 tm->thread_prefix = format (0, "vpp");
1117 tm->n_thread_stacks += tr->count;
1118 tm->n_pthreads += tr->count * tr->use_pthreads;
1119 tm->n_threads += tr->count * (tr->use_pthreads == 0);
1126 VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1128 #if !defined (__x86_64__) && !defined (__aarch64__) && !defined (__powerpc64__) && !defined(__arm__)
1130 __sync_fetch_and_add_8 (void)
1132 fformat (stderr, "%s called\n", __FUNCTION__);
1137 __sync_add_and_fetch_8 (void)
1139 fformat (stderr, "%s called\n", __FUNCTION__);
1144 void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
1146 vnet_main_fixup (vlib_fork_fixup_t which)
1151 vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1153 vlib_main_t *vm = vlib_get_main ();
1155 if (vlib_mains == 0)
1158 ASSERT (vlib_get_thread_index () == 0);
1159 vlib_worker_thread_barrier_sync (vm);
1163 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1164 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1170 vlib_worker_thread_barrier_release (vm);
1174 vlib_worker_thread_barrier_sync (vlib_main_t * vm)
1179 if (vec_len (vlib_mains) < 2)
1182 count = vec_len (vlib_mains) - 1;
1184 /* Tolerate recursive calls */
1185 if (++vlib_worker_threads[0].recursion_level > 1)
1188 vlib_worker_threads[0].barrier_sync_count++;
1190 ASSERT (vlib_get_thread_index () == 0);
1192 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1194 *vlib_worker_threads->wait_at_barrier = 1;
1195 while (*vlib_worker_threads->workers_at_barrier != count)
1197 if (vlib_time_now (vm) > deadline)
1199 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1206 vlib_worker_thread_barrier_release (vlib_main_t * vm)
1210 if (vec_len (vlib_mains) < 2)
1213 if (--vlib_worker_threads[0].recursion_level > 0)
1216 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1218 *vlib_worker_threads->wait_at_barrier = 0;
1220 while (*vlib_worker_threads->workers_at_barrier > 0)
1222 if (vlib_time_now (vm) > deadline)
1224 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1231 * Check the frame queue to see if any frames are available.
1232 * If so, pull the packets off the frames and put them to
1236 vlib_frame_queue_dequeue (vlib_main_t * vm, vlib_frame_queue_main_t * fqm)
1238 u32 thread_id = vm->thread_index;
1239 vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id];
1240 vlib_frame_queue_elt_t *elt;
1249 ASSERT (vm == vlib_mains[thread_id]);
1251 if (PREDICT_FALSE (fqm->node_index == ~0))
1254 * Gather trace data for frame queues
1256 if (PREDICT_FALSE (fq->trace))
1258 frame_queue_trace_t *fqt;
1259 frame_queue_nelt_counter_t *fqh;
1262 fqt = &fqm->frame_queue_traces[thread_id];
1264 fqt->nelts = fq->nelts;
1265 fqt->head = fq->head;
1266 fqt->head_hint = fq->head_hint;
1267 fqt->tail = fq->tail;
1268 fqt->threshold = fq->vector_threshold;
1269 fqt->n_in_use = fqt->tail - fqt->head;
1270 if (fqt->n_in_use >= fqt->nelts)
1272 // if beyond max then use max
1273 fqt->n_in_use = fqt->nelts - 1;
1276 /* Record the number of elements in use in the histogram */
1277 fqh = &fqm->frame_queue_histogram[thread_id];
1278 fqh->count[fqt->n_in_use]++;
1280 /* Record a snapshot of the elements in use */
1281 for (elix = 0; elix < fqt->nelts; elix++)
1283 elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1284 if (1 || elt->valid)
1286 fqt->n_vectors[elix] = elt->n_vectors;
1294 if (fq->head == fq->tail)
1296 fq->head_hint = fq->head;
1300 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
1304 fq->head_hint = fq->head;
1308 from = elt->buffer_index;
1309 msg_type = elt->msg_type;
1311 ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
1312 ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1314 f = vlib_get_frame_to_node (vm, fqm->node_index);
1316 to = vlib_frame_vector_args (f);
1318 n_left_to_node = elt->n_vectors;
1320 while (n_left_to_node >= 4)
1328 n_left_to_node -= 4;
1331 while (n_left_to_node > 0)
1339 vectors += elt->n_vectors;
1340 f->n_vectors = elt->n_vectors;
1341 vlib_put_frame_to_node (vm, fqm->node_index, f);
1345 elt->msg_type = 0xfefefefe;
1346 CLIB_MEMORY_BARRIER ();
1351 * Limit the number of packets pushed into the graph
1353 if (vectors >= fq->vector_threshold)
1355 fq->head_hint = fq->head;
1364 vlib_worker_thread_fn (void *arg)
1366 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
1367 vlib_thread_main_t *tm = vlib_get_thread_main ();
1368 vlib_main_t *vm = vlib_get_main ();
1371 ASSERT (vm->thread_index == vlib_get_thread_index ());
1373 vlib_worker_thread_init (w);
1374 clib_time_init (&vm->clib_time);
1375 clib_mem_set_heap (w->thread_mheap);
1377 /* Wait until the dpdk init sequence is complete */
1378 while (tm->extern_thread_mgmt && tm->worker_thread_release == 0)
1379 vlib_worker_thread_barrier_check ();
1381 e = vlib_call_init_exit_functions
1382 (vm, vm->worker_init_function_registrations, 1 /* call_once */ );
1384 clib_error_report (e);
1386 vlib_worker_loop (vm);
1390 VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1393 .function = vlib_worker_thread_fn,
1398 vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1400 vlib_thread_main_t *tm = vlib_get_thread_main ();
1401 vlib_frame_queue_main_t *fqm;
1402 vlib_frame_queue_t *fq;
1405 if (frame_queue_nelts == 0)
1406 frame_queue_nelts = FRAME_QUEUE_NELTS;
1408 vec_add2 (tm->frame_queue_mains, fqm, 1);
1410 fqm->node_index = node_index;
1412 vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1);
1413 _vec_len (fqm->vlib_frame_queues) = 0;
1414 for (i = 0; i < tm->n_vlib_mains; i++)
1416 fq = vlib_frame_queue_alloc (frame_queue_nelts);
1417 vec_add1 (fqm->vlib_frame_queues, fq);
1420 return (fqm - tm->frame_queue_mains);
1425 vlib_thread_cb_register (struct vlib_main_t *vm, vlib_thread_callbacks_t * cb)
1427 vlib_thread_main_t *tm = vlib_get_thread_main ();
1429 if (tm->extern_thread_mgmt)
1432 tm->cb.vlib_launch_thread_cb = cb->vlib_launch_thread_cb;
1433 tm->extern_thread_mgmt = 1;
1438 threads_init (vlib_main_t * vm)
1443 VLIB_INIT_FUNCTION (threads_init);
1446 * fd.io coding-style-patch-verification: ON
1449 * eval: (c-set-style "gnu")