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.
17 #include <vppinfra/format.h>
18 #include <vlib/vlib.h>
20 #include <vlib/threads.h>
21 #include <vlib/unix/physmem.h>
23 #include <vlib/unix/cj.h>
26 #include <rte_config.h>
27 #include <rte_common.h>
29 #include <rte_launch.h>
30 #include <rte_lcore.h>
32 DECLARE_CJ_GLOBAL_LOG;
34 #define FRAME_QUEUE_NELTS 32
39 * Weak definitions of DPDK symbols used in this file.
40 * Needed for linking test programs without DPDK libs.
42 unsigned __thread __attribute__((weak)) RTE_PER_LCORE(_lcore_id);
43 struct lcore_config __attribute__((weak)) lcore_config[];
44 unsigned __attribute__((weak)) rte_socket_id();
45 int __attribute__((weak)) rte_eal_remote_launch();
52 void debug_hex_bytes (u8 *s, u32 n)
54 fformat (stderr, "%U\n", format_hex_bytes, s, n);
57 vlib_thread_main_t vlib_thread_main;
60 os_get_cpu_number (void)
66 len = vec_len (vlib_thread_stacks);
70 /* Get any old stack address. */
73 n = ((uword)sp - (uword)vlib_thread_stacks[0])
74 >> VLIB_LOG2_THREAD_STACK_SIZE;
76 /* "processes" have their own stacks, and they always run in thread 0 */
83 vlib_set_thread_name (char *name)
85 int pthread_setname_np (pthread_t __target_thread, const char *__name);
86 pthread_t thread = pthread_self();
89 pthread_setname_np(thread, name);
92 static int sort_registrations_by_no_clone (void *a0, void * a1)
94 vlib_thread_registration_t ** tr0 = a0;
95 vlib_thread_registration_t ** tr1 = a1;
97 return ((i32)((*tr0)->no_data_structure_clone)
98 - ((i32)((*tr1)->no_data_structure_clone)));
102 vlib_sysfs_list_to_bitmap(char * filename)
107 fp = fopen (filename, "r");
112 vec_validate (buffer, 256-1);
113 if (fgets ((char *)buffer, 256, fp))
116 unformat_init_string (&in, (char *) buffer, strlen ((char *) buffer));
117 unformat(&in, "%U", unformat_bitmap_list, &r);
127 /* Called early in the init sequence */
130 vlib_thread_init (vlib_main_t * vm)
132 vlib_thread_main_t * tm = &vlib_thread_main;
133 vlib_worker_thread_t * w;
134 vlib_thread_registration_t * tr;
135 u32 n_vlib_mains = 1;
140 /* get bitmaps of active cpu cores and sockets */
141 tm->cpu_core_bitmap =
142 vlib_sysfs_list_to_bitmap("/sys/devices/system/cpu/online");
143 tm->cpu_socket_bitmap =
144 vlib_sysfs_list_to_bitmap("/sys/devices/system/node/online");
146 avail_cpu = clib_bitmap_dup(tm->cpu_core_bitmap);
149 for (i=0; i < tm->skip_cores; i++)
151 uword c = clib_bitmap_first_set(avail_cpu);
153 return clib_error_return (0, "no available cpus to skip");
155 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
158 /* grab cpu for main thread */
161 tm->main_lcore = clib_bitmap_first_set(avail_cpu);
162 if (tm->main_lcore == (u8) ~0)
163 return clib_error_return (0, "no available cpus to be used for the"
168 if (clib_bitmap_get(avail_cpu, tm->main_lcore) == 0)
169 return clib_error_return (0, "cpu %u is not available to be used"
170 " for the main thread", tm->main_lcore);
172 avail_cpu = clib_bitmap_set(avail_cpu, tm->main_lcore, 0);
174 /* assume that there is socket 0 only if there is no data from sysfs */
175 if (!tm->cpu_socket_bitmap)
176 tm->cpu_socket_bitmap = clib_bitmap_set(0, 0, 1);
178 /* as many threads as stacks... */
179 vec_validate_aligned (vlib_worker_threads, vec_len(vlib_thread_stacks)-1,
180 CLIB_CACHE_LINE_BYTES);
182 /* Preallocate thread 0 */
183 _vec_len(vlib_worker_threads) = 1;
184 w = vlib_worker_threads;
185 w->thread_mheap = clib_mem_get_heap();
186 w->thread_stack = vlib_thread_stacks[0];
187 w->dpdk_lcore_id = -1;
188 w->lwp = syscall(SYS_gettid);
189 tm->n_vlib_mains = 1;
191 /* assign threads to cores and set n_vlib_mains */
196 vec_add1 (tm->registrations, tr);
200 vec_sort_with_function
201 (tm->registrations, sort_registrations_by_no_clone);
203 for (i = 0; i < vec_len (tm->registrations); i++)
206 tr = tm->registrations[i];
207 tr->first_index = first_index;
208 first_index += tr->count;
209 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
211 /* construct coremask */
212 if (tr->use_pthreads || !tr->count)
218 clib_bitmap_foreach (c, tr->coremask, ({
219 if (clib_bitmap_get(avail_cpu, c) == 0)
220 return clib_error_return (0, "cpu %u is not available to be used"
221 " for the '%s' thread",c, tr->name);
223 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
229 for (j=0; j < tr->count; j++)
231 uword c = clib_bitmap_first_set(avail_cpu);
233 return clib_error_return (0, "no available cpus to be used for"
234 " the '%s' thread", tr->name);
236 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
237 tr->coremask = clib_bitmap_set(tr->coremask, c, 1);
242 clib_bitmap_free(avail_cpu);
244 tm->n_vlib_mains = n_vlib_mains;
246 vec_validate_aligned (vlib_worker_threads, first_index-1,
247 CLIB_CACHE_LINE_BYTES);
250 tm->efd.enabled = VLIB_EFD_DISABLED;
251 tm->efd.queue_hi_thresh = ((VLIB_EFD_DEF_WORKER_HI_THRESH_PCT *
252 FRAME_QUEUE_NELTS)/100);
256 vlib_worker_thread_t *
257 vlib_alloc_thread (vlib_main_t * vm)
259 vlib_worker_thread_t * w;
261 if (vec_len(vlib_worker_threads) >= vec_len (vlib_thread_stacks))
263 clib_warning ("out of worker threads... Quitting...");
266 vec_add2 (vlib_worker_threads, w, 1);
267 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
271 vlib_frame_queue_t * vlib_frame_queue_alloc (int nelts)
273 vlib_frame_queue_t * fq;
275 fq = clib_mem_alloc_aligned(sizeof (*fq), CLIB_CACHE_LINE_BYTES);
276 memset (fq, 0, sizeof (*fq));
278 fq->vector_threshold = 128; // packets
279 vec_validate_aligned (fq->elts, nelts-1, CLIB_CACHE_LINE_BYTES);
283 if (((uword)&fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
284 fformat(stderr, "WARNING: fq->tail unaligned\n");
285 if (((uword)&fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
286 fformat(stderr, "WARNING: fq->head unaligned\n");
287 if (((uword)fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
288 fformat(stderr, "WARNING: fq->elts unaligned\n");
290 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
291 fformat(stderr, "WARNING: fq->elts[0] size %d\n",
292 sizeof (fq->elts[0]));
293 if (nelts & (nelts -1))
295 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
303 void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
304 void vl_msg_api_handler_no_free (void *v) { }
306 /* Turned off, save as reference material... */
308 static inline int vlib_frame_queue_dequeue_internal (int thread_id,
310 vlib_node_main_t *nm)
312 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
313 vlib_frame_queue_elt_t *elt;
315 vlib_pending_frame_t *p;
316 vlib_node_runtime_t *r;
317 u32 node_runtime_index;
322 ASSERT(vm == vlib_mains[thread_id]);
326 if (fq->head == fq->tail)
329 elt = fq->elts + ((fq->head+1) & (fq->nelts-1));
334 before = clib_cpu_time_now();
337 node_runtime_index = elt->node_runtime_index;
338 msg_type = elt->msg_type;
342 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
343 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
344 /* note fallthrough... */
345 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
346 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
348 vlib_frame_free (vm, r, f);
350 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
351 vec_add2 (vm->node_main.pending_frames, p, 1);
352 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
353 p->node_runtime_index = elt->node_runtime_index;
354 p->frame_index = vlib_frame_index (vm, f);
355 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
356 fq->dequeue_vectors += (u64) f->n_vectors;
358 case VLIB_FRAME_QUEUE_ELT_API_MSG:
359 vl_msg_api_handler_no_free (f);
362 clib_warning ("bogus frame queue message, type %d", msg_type);
367 fq->dequeue_ticks += clib_cpu_time_now() - before;
368 CLIB_MEMORY_BARRIER();
376 int vlib_frame_queue_dequeue (int thread_id,
378 vlib_node_main_t *nm)
380 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
383 int vlib_frame_queue_enqueue (vlib_main_t *vm, u32 node_runtime_index,
384 u32 frame_queue_index, vlib_frame_t *frame,
385 vlib_frame_queue_msg_type_t type)
387 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
388 vlib_frame_queue_elt_t *elt;
391 u64 before = clib_cpu_time_now();
395 new_tail = __sync_add_and_fetch (&fq->tail, 1);
397 /* Wait until a ring slot is available */
398 while (new_tail >= fq->head + fq->nelts)
400 f64 b4 = vlib_time_now_ticks (vm, before);
401 vlib_worker_thread_barrier_check (vm, b4);
402 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
403 // vlib_frame_queue_dequeue (vm->cpu_index, vm, nm);
406 elt = fq->elts + (new_tail & (fq->nelts-1));
408 /* this would be very bad... */
413 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
414 save_count = frame->n_vectors;
417 elt->node_runtime_index = node_runtime_index;
418 elt->msg_type = type;
419 CLIB_MEMORY_BARRIER();
426 /* To be called by vlib worker threads upon startup */
427 void vlib_worker_thread_init (vlib_worker_thread_t * w)
429 vlib_thread_main_t *tm = vlib_get_thread_main();
431 /* worker threads wants no signals. */
435 pthread_sigmask (SIG_SETMASK, &s, 0);
438 clib_mem_set_heap (w->thread_mheap);
440 if (vec_len(tm->thread_prefix) && w->registration->short_name)
442 w->name = format(0, "%v_%s_%d%c", tm->thread_prefix,
443 w->registration->short_name,
446 vlib_set_thread_name((char *)w->name);
449 if (!w->registration->use_pthreads)
452 /* Initial barrier sync, for both worker and i/o threads */
453 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
455 while (*vlib_worker_threads->wait_at_barrier)
458 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
462 void *vlib_worker_thread_bootstrap_fn (void *arg)
465 vlib_worker_thread_t *w = arg;
467 w->lwp = syscall(SYS_gettid);
468 w->dpdk_lcore_id = -1;
470 if (w->registration && !w->registration->use_pthreads &&
471 rte_socket_id) /* do we really have dpdk linked */
473 unsigned lcore = rte_lcore_id();
474 lcore = lcore < RTE_MAX_LCORE ? lcore : -1;
475 w->dpdk_lcore_id = lcore;
479 rv = (void *) clib_calljmp
480 ((uword (*)(uword)) w->thread_function,
481 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
482 /* NOTREACHED, we hope */
487 vlib_launch_thread (void *fp, vlib_worker_thread_t *w, unsigned lcore_id)
490 void *(*fp_arg)(void *) = fp;
493 if (!w->registration->use_pthreads)
494 if (rte_eal_remote_launch) /* do we have dpdk linked */
495 return rte_eal_remote_launch (fp, (void *)w, lcore_id);
500 return pthread_create (&dummy, NULL /* attr */, fp_arg, (void *)w);
503 static clib_error_t * start_workers (vlib_main_t * vm)
506 vlib_worker_thread_t *w;
507 vlib_main_t *vm_clone;
509 vlib_frame_queue_t *fq;
510 vlib_thread_main_t * tm = &vlib_thread_main;
511 vlib_thread_registration_t * tr;
512 vlib_node_runtime_t * rt;
513 u32 n_vlib_mains = tm->n_vlib_mains;
514 u32 worker_thread_index;
516 vec_reset_length (vlib_worker_threads);
518 /* Set up the main thread */
519 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
520 w->elog_track.name = "main thread";
521 elog_track_register (&vm->elog_main, &w->elog_track);
523 if (vec_len(tm->thread_prefix))
525 w->name = format(0, "%v_main%c", tm->thread_prefix, '\0');
526 vlib_set_thread_name((char *)w->name);
530 w->dpdk_lcore_id = -1;
531 if (rte_socket_id) /* do we really have dpdk linked */
533 unsigned lcore = rte_lcore_id();
534 w->dpdk_lcore_id = lcore < RTE_MAX_LCORE ? lcore : -1;;
538 if (n_vlib_mains > 1)
540 u8 * heap = clib_mem_get_per_cpu_heap();
541 mheap_t * h = mheap_header (heap);
543 /* make the main heap thread-safe */
544 h->flags |= MHEAP_FLAG_THREAD_SAFE;
546 /* Make the event-log MP-safe */
548 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
549 CLIB_CACHE_LINE_BYTES);
551 vm->elog_main.lock[0] = 0;
553 vec_validate (vlib_mains, tm->n_vlib_mains - 1);
554 _vec_len (vlib_mains) = 0;
555 vec_add1 (vlib_mains, vm);
557 vec_validate (vlib_frame_queues, tm->n_vlib_mains - 1);
558 _vec_len (vlib_frame_queues) = 0;
559 fq = vlib_frame_queue_alloc (FRAME_QUEUE_NELTS);
560 vec_add1 (vlib_frame_queues, fq);
562 vlib_worker_threads->wait_at_barrier =
563 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
564 vlib_worker_threads->workers_at_barrier =
565 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
567 /* Ask for an initial barrier sync */
568 *vlib_worker_threads->workers_at_barrier = 0;
569 *vlib_worker_threads->wait_at_barrier = 1;
571 worker_thread_index = 1;
573 for (i = 0; i < vec_len(tm->registrations); i++)
575 vlib_node_main_t *nm, *nm_clone;
576 vlib_buffer_main_t *bm_clone;
577 vlib_buffer_free_list_t *fl_clone, *fl_orig;
578 vlib_buffer_free_list_t *orig_freelist_pool;
581 tr = tm->registrations[i];
586 for (k = 0; k < tr->count; k++)
588 vec_add2 (vlib_worker_threads, w, 1);
590 * Share the main heap which is now thread-safe.
592 * To allocate separate heaps, code:
593 * mheap_alloc (0 / * use VM * /, tr->mheap_size);
595 w->thread_mheap = heap;
596 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
597 w->thread_function = tr->function;
598 w->thread_function_arg = w;
600 w->registration = tr;
603 (char *) format (0, "%s %d", tr->name, k+1);
604 vec_add1 (w->elog_track.name, 0);
605 elog_track_register (&vm->elog_main, &w->elog_track);
607 if (tr->no_data_structure_clone)
610 /* Allocate "to-worker-N" frame queue */
611 fq = vlib_frame_queue_alloc (FRAME_QUEUE_NELTS);
612 vec_validate (vlib_frame_queues, worker_thread_index);
613 vlib_frame_queues[worker_thread_index] = fq;
615 /* Fork vlib_global_main et al. Look for bugs here */
616 oldheap = clib_mem_set_heap (w->thread_mheap);
618 vm_clone = clib_mem_alloc (sizeof (*vm_clone));
619 memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
621 vm_clone->cpu_index = worker_thread_index;
622 vm_clone->heap_base = w->thread_mheap;
623 vm_clone->mbuf_alloc_list = 0;
624 memset (&vm_clone->random_buffer, 0, sizeof (vm_clone->random_buffer));
626 nm = &vlib_mains[0]->node_main;
627 nm_clone = &vm_clone->node_main;
628 /* fork next frames array, preserving node runtime indices */
629 nm_clone->next_frames = vec_dup (nm->next_frames);
630 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
632 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
633 u32 save_node_runtime_index;
635 save_node_runtime_index = nf->node_runtime_index;
636 vlib_next_frame_init (nf);
637 nf->node_runtime_index = save_node_runtime_index;
640 /* fork the frame dispatch queue */
641 nm_clone->pending_frames = 0;
642 vec_validate (nm_clone->pending_frames, 10); /* $$$$$?????? */
643 _vec_len (nm_clone->pending_frames) = 0;
647 for (j = 0; j < vec_len (nm->nodes); j++)
650 n = clib_mem_alloc_no_fail (sizeof(*n));
651 memcpy (n, nm->nodes[j], sizeof (*n));
652 /* none of the copied nodes have enqueue rights given out */
653 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
654 memset (&n->stats_total, 0, sizeof (n->stats_total));
655 memset (&n->stats_last_clear, 0, sizeof (n->stats_last_clear));
656 vec_add1 (nm_clone->nodes, n);
658 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
659 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
661 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
662 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
663 vec_foreach(rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
664 rt->cpu_index = vm_clone->cpu_index;
666 nm_clone->processes = vec_dup (nm->processes);
668 /* zap the (per worker) frame freelists, etc */
669 nm_clone->frame_sizes = 0;
670 nm_clone->frame_size_hash = 0;
672 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
674 clib_mem_set_heap (oldheap);
675 vec_add1 (vlib_mains, vm_clone);
677 unix_physmem_init (vm_clone, 0 /* physmem not required */);
679 vm_clone->error_main.counters =
680 vec_dup(vlib_mains[0]->error_main.counters);
681 vm_clone->error_main.counters_last_clear =
682 vec_dup(vlib_mains[0]->error_main.counters_last_clear);
684 /* Fork the vlib_buffer_main_t free lists, etc. */
685 bm_clone = vec_dup (vm_clone->buffer_main);
686 vm_clone->buffer_main = bm_clone;
688 orig_freelist_pool = bm_clone->buffer_free_list_pool;
689 bm_clone->buffer_free_list_pool = 0;
691 pool_foreach (fl_orig, orig_freelist_pool,
693 pool_get_aligned (bm_clone->buffer_free_list_pool,
694 fl_clone, CLIB_CACHE_LINE_BYTES);
695 ASSERT (fl_orig - orig_freelist_pool
696 == fl_clone - bm_clone->buffer_free_list_pool);
698 fl_clone[0] = fl_orig[0];
699 fl_clone->aligned_buffers = 0;
700 fl_clone->unaligned_buffers = 0;
701 fl_clone->n_alloc = 0;
704 worker_thread_index++;
710 /* only have non-data-structure copy threads to create... */
711 for (i = 0; i < vec_len(tm->registrations); i++)
713 tr = tm->registrations[i];
715 for (j = 0; j < tr->count; j++)
717 vec_add2 (vlib_worker_threads, w, 1);
718 w->thread_mheap = mheap_alloc (0 /* use VM */, tr->mheap_size);
719 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
720 w->thread_function = tr->function;
721 w->thread_function_arg = w;
724 (char *) format (0, "%s %d", tr->name, j+1);
725 w->registration = tr;
726 vec_add1 (w->elog_track.name, 0);
727 elog_track_register (&vm->elog_main, &w->elog_track);
732 worker_thread_index = 1;
734 for (i = 0; i < vec_len (tm->registrations); i++)
738 tr = tm->registrations[i];
740 if (tr->use_pthreads || tm->use_pthreads)
742 for (j = 0; j < tr->count; j++)
744 w = vlib_worker_threads + worker_thread_index++;
745 if (vlib_launch_thread (vlib_worker_thread_bootstrap_fn, w, 0) < 0)
746 clib_warning ("Couldn't start '%s' pthread ", tr->name);
752 clib_bitmap_foreach (c, tr->coremask, ({
753 w = vlib_worker_threads + worker_thread_index++;
754 if (vlib_launch_thread (vlib_worker_thread_bootstrap_fn, w, c) < 0)
755 clib_warning ("Couldn't start DPDK lcore %d", c);
760 vlib_worker_thread_barrier_sync(vm);
761 vlib_worker_thread_barrier_release(vm);
765 VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
767 void vlib_worker_thread_node_runtime_update(void)
770 vlib_worker_thread_t *w;
772 vlib_node_main_t *nm, *nm_clone;
773 vlib_node_t ** old_nodes_clone;
774 vlib_main_t *vm_clone;
775 vlib_node_runtime_t * rt, * old_rt;
778 vlib_node_runtime_sync_stats (vlib_main_t * vm,
779 vlib_node_runtime_t * r,
784 ASSERT (os_get_cpu_number() == 0);
786 if (vec_len (vlib_mains) == 0)
792 ASSERT (os_get_cpu_number() == 0);
793 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
796 * Scrape all runtime stats, so we don't lose node runtime(s) with
797 * pending counts, or throw away worker / io thread counts.
799 for (j = 0; j < vec_len (nm->nodes); j++)
803 vlib_node_sync_stats (vm, n);
806 for (i = 1; i < vec_len (vlib_mains); i++)
810 vm_clone = vlib_mains[i];
811 nm_clone = &vm_clone->node_main;
813 for (j = 0; j < vec_len (nm_clone->nodes); j++)
815 n = nm_clone->nodes[j];
817 rt = vlib_node_get_runtime (vm_clone, n->index);
818 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
822 for (i = 1; i < vec_len (vlib_mains); i++)
824 vlib_node_runtime_t * rt;
825 w = vlib_worker_threads + i;
826 oldheap = clib_mem_set_heap (w->thread_mheap);
828 vm_clone = vlib_mains[i];
830 /* Re-clone error heap */
831 u64 * old_counters = vm_clone->error_main.counters;
832 u64 * old_counters_all_clear = vm_clone->error_main.counters_last_clear;
833 memcpy (&vm_clone->error_main, &vm->error_main, sizeof (vm->error_main));
834 j = vec_len(vm->error_main.counters) - 1;
835 vec_validate_aligned(old_counters, j, CLIB_CACHE_LINE_BYTES);
836 vec_validate_aligned(old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
837 vm_clone->error_main.counters = old_counters;
838 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
840 nm_clone = &vm_clone->node_main;
841 vec_free (nm_clone->next_frames);
842 nm_clone->next_frames = vec_dup (nm->next_frames);
844 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
846 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
847 u32 save_node_runtime_index;
849 save_node_runtime_index = nf->node_runtime_index;
850 vlib_next_frame_init (nf);
851 nf->node_runtime_index = save_node_runtime_index;
854 old_nodes_clone = nm_clone->nodes;
858 for (j = 0; j < vec_len (nm->nodes); j++) {
859 vlib_node_t *old_n_clone;
860 vlib_node_t *new_n, *new_n_clone;
862 new_n = nm->nodes[j];
863 old_n_clone = old_nodes_clone[j];
865 new_n_clone = clib_mem_alloc_no_fail (sizeof(*new_n_clone));
866 memcpy (new_n_clone, new_n, sizeof (*new_n));
867 /* none of the copied nodes have enqueue rights given out */
868 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
870 if (j >= vec_len (old_nodes_clone))
872 /* new node, set to zero */
873 memset (&new_n_clone->stats_total, 0,
874 sizeof (new_n_clone->stats_total));
875 memset (&new_n_clone->stats_last_clear, 0,
876 sizeof (new_n_clone->stats_last_clear));
880 /* Copy stats if the old data is valid */
881 memcpy (&new_n_clone->stats_total,
882 &old_n_clone->stats_total,
883 sizeof (new_n_clone->stats_total));
884 memcpy (&new_n_clone->stats_last_clear,
885 &old_n_clone->stats_last_clear,
886 sizeof (new_n_clone->stats_last_clear));
888 /* keep previous node state */
889 new_n_clone->state = old_n_clone->state;
891 vec_add1 (nm_clone->nodes, new_n_clone);
893 /* Free the old node clone */
894 for (j = 0; j < vec_len(old_nodes_clone); j++)
895 clib_mem_free (old_nodes_clone[j]);
896 vec_free (old_nodes_clone);
898 vec_free (nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
900 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
901 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
903 /* clone input node runtime */
904 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
906 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
907 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
909 vec_foreach(rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
911 rt->cpu_index = vm_clone->cpu_index;
914 for (j=0; j < vec_len(old_rt); j++)
916 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
917 rt->state = old_rt[j].state;
922 nm_clone->processes = vec_dup (nm->processes);
924 clib_mem_set_heap (oldheap);
926 // vnet_main_fork_fixup (i);
930 static clib_error_t *
931 cpu_config (vlib_main_t * vm, unformat_input_t * input)
933 vlib_thread_registration_t *tr;
935 vlib_thread_main_t * tm = &vlib_thread_main;
941 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
942 tm->n_thread_stacks = 1; /* account for main thread */
948 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword)tr);
952 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
954 if (unformat (input, "main-thread-io"))
955 tm->main_thread_is_io_node = 1;
956 else if (unformat (input, "use-pthreads"))
957 tm->use_pthreads = 1;
958 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
960 else if (unformat (input, "main-core %u", &tm->main_lcore))
962 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
964 else if (unformat (input, "coremask-%s %llx", &name, &coremask))
966 p = hash_get_mem (tm->thread_registrations_by_name, name);
968 return clib_error_return (0, "no such thread type '%s'", name);
970 tr = (vlib_thread_registration_t *)p[0];
972 if (tr->use_pthreads)
973 return clib_error_return (0, "coremask cannot be set for '%s' threads",
976 tr->coremask = clib_bitmap_set_multiple
977 (tr->coremask, 0, coremask, BITS(coremask));
978 tr->count = clib_bitmap_count_set_bits (tr->coremask);
980 else if (unformat (input, "corelist-%s %U", &name, unformat_bitmap_list,
983 p = hash_get_mem (tm->thread_registrations_by_name, name);
985 return clib_error_return (0, "no such thread type '%s'", name);
987 tr = (vlib_thread_registration_t *)p[0];
989 if (tr->use_pthreads)
990 return clib_error_return (0, "corelist cannot be set for '%s' threads",
993 tr->coremask = bitmap;
994 tr->count = clib_bitmap_count_set_bits (tr->coremask);
996 else if (unformat (input, "%s %u", &name, &count))
998 p = hash_get_mem (tm->thread_registrations_by_name, name);
1000 return clib_error_return (0, "no such thread type '%s'", name);
1002 tr = (vlib_thread_registration_t *)p[0];
1003 if (tr->fixed_count)
1004 return clib_error_return
1005 (0, "number of %s threads not configurable", tr->name);
1014 if (!tm->thread_prefix)
1015 tm->thread_prefix = format(0, "vpp");
1019 tm->n_thread_stacks += tr->count;
1020 tm->n_pthreads += tr->count * tr->use_pthreads;
1021 tm->n_eal_threads += tr->count * (tr->use_pthreads == 0);
1028 VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1030 #if !defined (__x86_64__) && !defined (__aarch64__) && !defined (__powerpc64__)
1031 void __sync_fetch_and_add_8 (void)
1033 fformat(stderr, "%s called\n", __FUNCTION__);
1036 void __sync_add_and_fetch_8 (void)
1038 fformat(stderr, "%s called\n", __FUNCTION__);
1043 void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
1044 void vnet_main_fixup (vlib_fork_fixup_t which) { }
1046 void vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1048 vlib_main_t * vm = vlib_get_main();
1050 if (vlib_mains == 0)
1053 ASSERT(os_get_cpu_number() == 0);
1054 vlib_worker_thread_barrier_sync(vm);
1058 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1059 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1065 vlib_worker_thread_barrier_release(vm);
1068 void vlib_worker_thread_barrier_sync(vlib_main_t *vm)
1076 count = vec_len (vlib_mains) - 1;
1078 /* Tolerate recursive calls */
1079 if (++vlib_worker_threads[0].recursion_level > 1)
1082 ASSERT (os_get_cpu_number() == 0);
1084 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1086 *vlib_worker_threads->wait_at_barrier = 1;
1087 while (*vlib_worker_threads->workers_at_barrier != count)
1089 if (vlib_time_now(vm) > deadline)
1091 fformat(stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1097 void vlib_worker_thread_barrier_release(vlib_main_t * vm)
1104 if (--vlib_worker_threads[0].recursion_level > 0)
1107 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1109 *vlib_worker_threads->wait_at_barrier = 0;
1111 while (*vlib_worker_threads->workers_at_barrier > 0)
1113 if (vlib_time_now(vm) > deadline)
1115 fformat(stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1121 static clib_error_t *
1122 show_threads_fn (vlib_main_t * vm,
1123 unformat_input_t * input,
1124 vlib_cli_command_t * cmd)
1126 vlib_worker_thread_t * w;
1129 vlib_cli_output (vm, "%-7s%-20s%-12s%-8s%-7s%-7s%-7s%-10s",
1130 "ID", "Name", "Type", "LWP",
1131 "lcore", "Core", "Socket", "State");
1133 #if !defined(__powerpc64__)
1134 for (i = 0; i < vec_len(vlib_worker_threads); i++)
1136 w = vlib_worker_threads + i;
1139 line = format(line, "%-7d%-20s%-12s%-8d",
1141 w->name ? w->name : (u8 *) "",
1142 w->registration ? w->registration->name : "",
1146 int lcore = w->dpdk_lcore_id;
1149 line = format(line, "%-7u%-7u%-7u",
1151 lcore_config[lcore].core_id,
1152 lcore_config[lcore].socket_id);
1154 switch(lcore_config[lcore].state)
1157 line = format(line, "wait");
1160 line = format(line, "running");
1163 line = format(line, "finished");
1166 line = format(line, "unknown");
1170 vlib_cli_output(vm, "%v", line);
1179 VLIB_CLI_COMMAND (show_threads_command, static) = {
1180 .path = "show threads",
1181 .short_help = "Show threads",
1182 .function = show_threads_fn,