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.
20 #include <vppinfra/format.h>
21 #include <vlib/vlib.h>
23 #include <vlib/threads.h>
24 #include <vlib/unix/cj.h>
28 #include <rte_config.h>
29 #include <rte_common.h>
31 #include <rte_launch.h>
32 #include <rte_lcore.h>
34 DECLARE_CJ_GLOBAL_LOG;
36 #define FRAME_QUEUE_NELTS 32
41 * Weak definitions of DPDK symbols used in this file.
42 * Needed for linking test programs without DPDK libs.
44 unsigned __thread __attribute__((weak)) RTE_PER_LCORE(_lcore_id);
45 struct lcore_config __attribute__((weak)) lcore_config[];
46 unsigned __attribute__((weak)) rte_socket_id();
47 int __attribute__((weak)) rte_eal_remote_launch();
54 void debug_hex_bytes (u8 *s, u32 n)
56 fformat (stderr, "%U\n", format_hex_bytes, s, n);
59 vlib_thread_main_t vlib_thread_main;
62 os_get_cpu_number (void)
68 len = vec_len (vlib_thread_stacks);
72 /* Get any old stack address. */
75 n = ((uword)sp - (uword)vlib_thread_stacks[0])
76 >> VLIB_LOG2_THREAD_STACK_SIZE;
78 /* "processes" have their own stacks, and they always run in thread 0 */
85 vlib_set_thread_name (char *name)
87 int pthread_setname_np (pthread_t __target_thread, const char *__name);
88 pthread_t thread = pthread_self();
91 pthread_setname_np(thread, name);
94 static int sort_registrations_by_no_clone (void *a0, void * a1)
96 vlib_thread_registration_t ** tr0 = a0;
97 vlib_thread_registration_t ** tr1 = a1;
99 return ((i32)((*tr0)->no_data_structure_clone)
100 - ((i32)((*tr1)->no_data_structure_clone)));
104 vlib_sysfs_list_to_bitmap(char * filename)
109 fp = fopen (filename, "r");
114 vec_validate (buffer, 256-1);
115 if (fgets ((char *)buffer, 256, fp))
118 unformat_init_string (&in, (char *) buffer, strlen ((char *) buffer));
119 unformat(&in, "%U", unformat_bitmap_list, &r);
129 /* Called early in the init sequence */
132 vlib_thread_init (vlib_main_t * vm)
134 vlib_thread_main_t * tm = &vlib_thread_main;
135 vlib_worker_thread_t * w;
136 vlib_thread_registration_t * tr;
137 u32 n_vlib_mains = 1;
142 /* get bitmaps of active cpu cores and sockets */
143 tm->cpu_core_bitmap =
144 vlib_sysfs_list_to_bitmap("/sys/devices/system/cpu/online");
145 tm->cpu_socket_bitmap =
146 vlib_sysfs_list_to_bitmap("/sys/devices/system/node/online");
148 avail_cpu = clib_bitmap_dup(tm->cpu_core_bitmap);
151 for (i=0; i < tm->skip_cores; i++)
153 uword c = clib_bitmap_first_set(avail_cpu);
155 return clib_error_return (0, "no available cpus to skip");
157 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
160 /* grab cpu for main thread */
163 tm->main_lcore = clib_bitmap_first_set(avail_cpu);
164 if (tm->main_lcore == (u8) ~0)
165 return clib_error_return (0, "no available cpus to be used for the"
170 if (clib_bitmap_get(avail_cpu, tm->main_lcore) == 0)
171 return clib_error_return (0, "cpu %u is not available to be used"
172 " for the main thread", tm->main_lcore);
174 avail_cpu = clib_bitmap_set(avail_cpu, tm->main_lcore, 0);
176 /* assume that there is socket 0 only if there is no data from sysfs */
177 if (!tm->cpu_socket_bitmap)
178 tm->cpu_socket_bitmap = clib_bitmap_set(0, 0, 1);
180 /* pin main thread to main_lcore */
185 CPU_SET(tm->main_lcore, &cpuset);
186 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
190 /* as many threads as stacks... */
191 vec_validate_aligned (vlib_worker_threads, vec_len(vlib_thread_stacks)-1,
192 CLIB_CACHE_LINE_BYTES);
194 /* Preallocate thread 0 */
195 _vec_len(vlib_worker_threads) = 1;
196 w = vlib_worker_threads;
197 w->thread_mheap = clib_mem_get_heap();
198 w->thread_stack = vlib_thread_stacks[0];
199 w->dpdk_lcore_id = -1;
200 w->lwp = syscall(SYS_gettid);
201 tm->n_vlib_mains = 1;
203 /* assign threads to cores and set n_vlib_mains */
208 vec_add1 (tm->registrations, tr);
212 vec_sort_with_function
213 (tm->registrations, sort_registrations_by_no_clone);
215 for (i = 0; i < vec_len (tm->registrations); i++)
218 tr = tm->registrations[i];
219 tr->first_index = first_index;
220 first_index += tr->count;
221 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
223 /* construct coremask */
224 if (tr->use_pthreads || !tr->count)
230 clib_bitmap_foreach (c, tr->coremask, ({
231 if (clib_bitmap_get(avail_cpu, c) == 0)
232 return clib_error_return (0, "cpu %u is not available to be used"
233 " for the '%s' thread",c, tr->name);
235 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
241 for (j=0; j < tr->count; j++)
243 uword c = clib_bitmap_first_set(avail_cpu);
245 return clib_error_return (0, "no available cpus to be used for"
246 " the '%s' thread", tr->name);
248 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
249 tr->coremask = clib_bitmap_set(tr->coremask, c, 1);
254 clib_bitmap_free(avail_cpu);
256 tm->n_vlib_mains = n_vlib_mains;
258 vec_validate_aligned (vlib_worker_threads, first_index-1,
259 CLIB_CACHE_LINE_BYTES);
262 tm->efd.enabled = VLIB_EFD_DISABLED;
263 tm->efd.queue_hi_thresh = ((VLIB_EFD_DEF_WORKER_HI_THRESH_PCT *
264 FRAME_QUEUE_NELTS)/100);
268 vlib_worker_thread_t *
269 vlib_alloc_thread (vlib_main_t * vm)
271 vlib_worker_thread_t * w;
273 if (vec_len(vlib_worker_threads) >= vec_len (vlib_thread_stacks))
275 clib_warning ("out of worker threads... Quitting...");
278 vec_add2 (vlib_worker_threads, w, 1);
279 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
283 vlib_frame_queue_t * vlib_frame_queue_alloc (int nelts)
285 vlib_frame_queue_t * fq;
287 fq = clib_mem_alloc_aligned(sizeof (*fq), CLIB_CACHE_LINE_BYTES);
288 memset (fq, 0, sizeof (*fq));
290 fq->vector_threshold = 128; // packets
291 vec_validate_aligned (fq->elts, nelts-1, CLIB_CACHE_LINE_BYTES);
295 if (((uword)&fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
296 fformat(stderr, "WARNING: fq->tail unaligned\n");
297 if (((uword)&fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
298 fformat(stderr, "WARNING: fq->head unaligned\n");
299 if (((uword)fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
300 fformat(stderr, "WARNING: fq->elts unaligned\n");
302 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
303 fformat(stderr, "WARNING: fq->elts[0] size %d\n",
304 sizeof (fq->elts[0]));
305 if (nelts & (nelts -1))
307 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
315 void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
316 void vl_msg_api_handler_no_free (void *v) { }
318 /* Turned off, save as reference material... */
320 static inline int vlib_frame_queue_dequeue_internal (int thread_id,
322 vlib_node_main_t *nm)
324 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
325 vlib_frame_queue_elt_t *elt;
327 vlib_pending_frame_t *p;
328 vlib_node_runtime_t *r;
329 u32 node_runtime_index;
334 ASSERT(vm == vlib_mains[thread_id]);
338 if (fq->head == fq->tail)
341 elt = fq->elts + ((fq->head+1) & (fq->nelts-1));
346 before = clib_cpu_time_now();
349 node_runtime_index = elt->node_runtime_index;
350 msg_type = elt->msg_type;
354 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
355 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
356 /* note fallthrough... */
357 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
358 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
360 vlib_frame_free (vm, r, f);
362 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
363 vec_add2 (vm->node_main.pending_frames, p, 1);
364 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
365 p->node_runtime_index = elt->node_runtime_index;
366 p->frame_index = vlib_frame_index (vm, f);
367 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
368 fq->dequeue_vectors += (u64) f->n_vectors;
370 case VLIB_FRAME_QUEUE_ELT_API_MSG:
371 vl_msg_api_handler_no_free (f);
374 clib_warning ("bogus frame queue message, type %d", msg_type);
379 fq->dequeue_ticks += clib_cpu_time_now() - before;
380 CLIB_MEMORY_BARRIER();
388 int vlib_frame_queue_dequeue (int thread_id,
390 vlib_node_main_t *nm)
392 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
395 int vlib_frame_queue_enqueue (vlib_main_t *vm, u32 node_runtime_index,
396 u32 frame_queue_index, vlib_frame_t *frame,
397 vlib_frame_queue_msg_type_t type)
399 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
400 vlib_frame_queue_elt_t *elt;
403 u64 before = clib_cpu_time_now();
407 new_tail = __sync_add_and_fetch (&fq->tail, 1);
409 /* Wait until a ring slot is available */
410 while (new_tail >= fq->head + fq->nelts)
412 f64 b4 = vlib_time_now_ticks (vm, before);
413 vlib_worker_thread_barrier_check (vm, b4);
414 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
415 // vlib_frame_queue_dequeue (vm->cpu_index, vm, nm);
418 elt = fq->elts + (new_tail & (fq->nelts-1));
420 /* this would be very bad... */
425 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
426 save_count = frame->n_vectors;
429 elt->node_runtime_index = node_runtime_index;
430 elt->msg_type = type;
431 CLIB_MEMORY_BARRIER();
438 /* To be called by vlib worker threads upon startup */
439 void vlib_worker_thread_init (vlib_worker_thread_t * w)
441 vlib_thread_main_t *tm = vlib_get_thread_main();
443 /* worker threads wants no signals. */
447 pthread_sigmask (SIG_SETMASK, &s, 0);
450 clib_mem_set_heap (w->thread_mheap);
452 if (vec_len(tm->thread_prefix) && w->registration->short_name)
454 w->name = format(0, "%v_%s_%d%c", tm->thread_prefix,
455 w->registration->short_name,
458 vlib_set_thread_name((char *)w->name);
461 if (!w->registration->use_pthreads)
464 /* Initial barrier sync, for both worker and i/o threads */
465 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
467 while (*vlib_worker_threads->wait_at_barrier)
470 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
474 void *vlib_worker_thread_bootstrap_fn (void *arg)
477 vlib_worker_thread_t *w = arg;
479 w->lwp = syscall(SYS_gettid);
480 w->dpdk_lcore_id = -1;
482 if (w->registration && !w->registration->use_pthreads &&
483 rte_socket_id) /* do we really have dpdk linked */
485 unsigned lcore = rte_lcore_id();
486 lcore = lcore < RTE_MAX_LCORE ? lcore : -1;
487 w->dpdk_lcore_id = lcore;
491 rv = (void *) clib_calljmp
492 ((uword (*)(uword)) w->thread_function,
493 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
494 /* NOTREACHED, we hope */
499 vlib_launch_thread (void *fp, vlib_worker_thread_t *w, unsigned lcore_id)
501 void *(*fp_arg)(void *) = fp;
504 if (!w->registration->use_pthreads)
505 if (rte_eal_remote_launch) /* do we have dpdk linked */
506 return rte_eal_remote_launch (fp, (void *)w, lcore_id);
516 CPU_SET(lcore_id, &cpuset);
518 ret = pthread_create (&worker, NULL /* attr */, fp_arg, (void *)w);
520 return pthread_setaffinity_np(worker, sizeof(cpu_set_t), &cpuset);
526 static clib_error_t * start_workers (vlib_main_t * vm)
529 vlib_worker_thread_t *w;
530 vlib_main_t *vm_clone;
532 vlib_frame_queue_t *fq;
533 vlib_thread_main_t * tm = &vlib_thread_main;
534 vlib_thread_registration_t * tr;
535 vlib_node_runtime_t * rt;
536 u32 n_vlib_mains = tm->n_vlib_mains;
537 u32 worker_thread_index;
538 u8 * main_heap = clib_mem_get_per_cpu_heap();
539 mheap_t * main_heap_header = mheap_header (main_heap);
541 vec_reset_length (vlib_worker_threads);
543 /* Set up the main thread */
544 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
545 w->elog_track.name = "main thread";
546 elog_track_register (&vm->elog_main, &w->elog_track);
548 if (vec_len(tm->thread_prefix))
550 w->name = format(0, "%v_main%c", tm->thread_prefix, '\0');
551 vlib_set_thread_name((char *)w->name);
555 w->dpdk_lcore_id = -1;
556 if (rte_socket_id) /* do we really have dpdk linked */
558 unsigned lcore = rte_lcore_id();
559 w->dpdk_lcore_id = lcore < RTE_MAX_LCORE ? lcore : -1;;
564 * Truth of the matter: we always use at least two
565 * threads. So, make the main heap thread-safe
566 * and make the event log thread-safe.
568 main_heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
570 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
571 CLIB_CACHE_LINE_BYTES);
572 vm->elog_main.lock[0] = 0;
574 if (n_vlib_mains > 1)
576 vec_validate (vlib_mains, tm->n_vlib_mains - 1);
577 _vec_len (vlib_mains) = 0;
578 vec_add1 (vlib_mains, vm);
580 vec_validate (vlib_frame_queues, tm->n_vlib_mains - 1);
581 _vec_len (vlib_frame_queues) = 0;
582 fq = vlib_frame_queue_alloc (FRAME_QUEUE_NELTS);
583 vec_add1 (vlib_frame_queues, fq);
585 vlib_worker_threads->wait_at_barrier =
586 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
587 vlib_worker_threads->workers_at_barrier =
588 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
590 /* Ask for an initial barrier sync */
591 *vlib_worker_threads->workers_at_barrier = 0;
592 *vlib_worker_threads->wait_at_barrier = 1;
594 worker_thread_index = 1;
596 for (i = 0; i < vec_len(tm->registrations); i++)
598 vlib_node_main_t *nm, *nm_clone;
599 vlib_buffer_main_t *bm_clone;
600 vlib_buffer_free_list_t *fl_clone, *fl_orig;
601 vlib_buffer_free_list_t *orig_freelist_pool;
604 tr = tm->registrations[i];
609 for (k = 0; k < tr->count; k++)
611 vec_add2 (vlib_worker_threads, w, 1);
613 w->thread_mheap = mheap_alloc (0 /* use VM */, tr->mheap_size);
615 w->thread_mheap = main_heap;
616 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
617 w->thread_function = tr->function;
618 w->thread_function_arg = w;
620 w->registration = tr;
623 (char *) format (0, "%s %d", tr->name, k+1);
624 vec_add1 (w->elog_track.name, 0);
625 elog_track_register (&vm->elog_main, &w->elog_track);
627 if (tr->no_data_structure_clone)
630 /* Allocate "to-worker-N" frame queue */
631 if (tr->frame_queue_nelts) {
632 fq = vlib_frame_queue_alloc (tr->frame_queue_nelts);
634 fq = vlib_frame_queue_alloc (FRAME_QUEUE_NELTS);
637 vec_validate (vlib_frame_queues, worker_thread_index);
638 vlib_frame_queues[worker_thread_index] = fq;
640 /* Fork vlib_global_main et al. Look for bugs here */
641 oldheap = clib_mem_set_heap (w->thread_mheap);
643 vm_clone = clib_mem_alloc (sizeof (*vm_clone));
644 clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
646 vm_clone->cpu_index = worker_thread_index;
647 vm_clone->heap_base = w->thread_mheap;
648 vm_clone->mbuf_alloc_list = 0;
649 memset (&vm_clone->random_buffer, 0, sizeof (vm_clone->random_buffer));
651 nm = &vlib_mains[0]->node_main;
652 nm_clone = &vm_clone->node_main;
653 /* fork next frames array, preserving node runtime indices */
654 nm_clone->next_frames = vec_dup (nm->next_frames);
655 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
657 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
658 u32 save_node_runtime_index;
661 save_node_runtime_index = nf->node_runtime_index;
662 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
663 vlib_next_frame_init (nf);
664 nf->node_runtime_index = save_node_runtime_index;
665 nf->flags = save_flags;
668 /* fork the frame dispatch queue */
669 nm_clone->pending_frames = 0;
670 vec_validate (nm_clone->pending_frames, 10); /* $$$$$?????? */
671 _vec_len (nm_clone->pending_frames) = 0;
675 for (j = 0; j < vec_len (nm->nodes); j++)
678 n = clib_mem_alloc_no_fail (sizeof(*n));
679 clib_memcpy (n, nm->nodes[j], sizeof (*n));
680 /* none of the copied nodes have enqueue rights given out */
681 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
682 memset (&n->stats_total, 0, sizeof (n->stats_total));
683 memset (&n->stats_last_clear, 0, sizeof (n->stats_last_clear));
684 vec_add1 (nm_clone->nodes, n);
686 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
687 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
689 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
690 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
691 vec_foreach(rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
692 rt->cpu_index = vm_clone->cpu_index;
694 nm_clone->processes = vec_dup (nm->processes);
696 /* zap the (per worker) frame freelists, etc */
697 nm_clone->frame_sizes = 0;
698 nm_clone->frame_size_hash = 0;
700 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
702 clib_mem_set_heap (oldheap);
703 vec_add1 (vlib_mains, vm_clone);
705 vm_clone->error_main.counters =
706 vec_dup(vlib_mains[0]->error_main.counters);
707 vm_clone->error_main.counters_last_clear =
708 vec_dup(vlib_mains[0]->error_main.counters_last_clear);
710 /* Fork the vlib_buffer_main_t free lists, etc. */
711 bm_clone = vec_dup (vm_clone->buffer_main);
712 vm_clone->buffer_main = bm_clone;
714 orig_freelist_pool = bm_clone->buffer_free_list_pool;
715 bm_clone->buffer_free_list_pool = 0;
717 pool_foreach (fl_orig, orig_freelist_pool,
719 pool_get_aligned (bm_clone->buffer_free_list_pool,
720 fl_clone, CLIB_CACHE_LINE_BYTES);
721 ASSERT (fl_orig - orig_freelist_pool
722 == fl_clone - bm_clone->buffer_free_list_pool);
724 fl_clone[0] = fl_orig[0];
725 fl_clone->aligned_buffers = 0;
726 fl_clone->unaligned_buffers = 0;
727 fl_clone->n_alloc = 0;
730 worker_thread_index++;
736 /* only have non-data-structure copy threads to create... */
737 for (i = 0; i < vec_len(tm->registrations); i++)
739 tr = tm->registrations[i];
741 for (j = 0; j < tr->count; j++)
743 vec_add2 (vlib_worker_threads, w, 1);
745 w->thread_mheap = mheap_alloc (0 /* use VM */, tr->mheap_size);
747 w->thread_mheap = main_heap;
748 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
749 w->thread_function = tr->function;
750 w->thread_function_arg = w;
753 (char *) format (0, "%s %d", tr->name, j+1);
754 w->registration = tr;
755 vec_add1 (w->elog_track.name, 0);
756 elog_track_register (&vm->elog_main, &w->elog_track);
761 worker_thread_index = 1;
763 for (i = 0; i < vec_len (tm->registrations); i++)
767 tr = tm->registrations[i];
769 if (tr->use_pthreads || tm->use_pthreads)
771 for (j = 0; j < tr->count; j++)
773 w = vlib_worker_threads + worker_thread_index++;
774 if (vlib_launch_thread (vlib_worker_thread_bootstrap_fn, w, 0) < 0)
775 clib_warning ("Couldn't start '%s' pthread ", tr->name);
781 clib_bitmap_foreach (c, tr->coremask, ({
782 w = vlib_worker_threads + worker_thread_index++;
783 if (vlib_launch_thread (vlib_worker_thread_bootstrap_fn, w, c) < 0)
784 clib_warning ("Couldn't start DPDK lcore %d", c);
789 vlib_worker_thread_barrier_sync(vm);
790 vlib_worker_thread_barrier_release(vm);
794 VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
796 void vlib_worker_thread_node_runtime_update(void)
799 vlib_worker_thread_t *w;
801 vlib_node_main_t *nm, *nm_clone;
802 vlib_node_t ** old_nodes_clone;
803 vlib_main_t *vm_clone;
804 vlib_node_runtime_t * rt, * old_rt;
807 vlib_node_runtime_sync_stats (vlib_main_t * vm,
808 vlib_node_runtime_t * r,
813 ASSERT (os_get_cpu_number() == 0);
815 if (vec_len (vlib_mains) == 0)
821 ASSERT (os_get_cpu_number() == 0);
822 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
825 * Scrape all runtime stats, so we don't lose node runtime(s) with
826 * pending counts, or throw away worker / io thread counts.
828 for (j = 0; j < vec_len (nm->nodes); j++)
832 vlib_node_sync_stats (vm, n);
835 for (i = 1; i < vec_len (vlib_mains); i++)
839 vm_clone = vlib_mains[i];
840 nm_clone = &vm_clone->node_main;
842 for (j = 0; j < vec_len (nm_clone->nodes); j++)
844 n = nm_clone->nodes[j];
846 rt = vlib_node_get_runtime (vm_clone, n->index);
847 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
851 for (i = 1; i < vec_len (vlib_mains); i++)
853 vlib_node_runtime_t * rt;
854 w = vlib_worker_threads + i;
855 oldheap = clib_mem_set_heap (w->thread_mheap);
857 vm_clone = vlib_mains[i];
859 /* Re-clone error heap */
860 u64 * old_counters = vm_clone->error_main.counters;
861 u64 * old_counters_all_clear = vm_clone->error_main.counters_last_clear;
862 clib_memcpy (&vm_clone->error_main, &vm->error_main, sizeof (vm->error_main));
863 j = vec_len(vm->error_main.counters) - 1;
864 vec_validate_aligned(old_counters, j, CLIB_CACHE_LINE_BYTES);
865 vec_validate_aligned(old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
866 vm_clone->error_main.counters = old_counters;
867 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
869 nm_clone = &vm_clone->node_main;
870 vec_free (nm_clone->next_frames);
871 nm_clone->next_frames = vec_dup (nm->next_frames);
873 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
875 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
876 u32 save_node_runtime_index;
879 save_node_runtime_index = nf->node_runtime_index;
880 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
881 vlib_next_frame_init (nf);
882 nf->node_runtime_index = save_node_runtime_index;
883 nf->flags = save_flags;
886 old_nodes_clone = nm_clone->nodes;
890 for (j = 0; j < vec_len (nm->nodes); j++) {
891 vlib_node_t *old_n_clone;
892 vlib_node_t *new_n, *new_n_clone;
894 new_n = nm->nodes[j];
895 old_n_clone = old_nodes_clone[j];
897 new_n_clone = clib_mem_alloc_no_fail (sizeof(*new_n_clone));
898 clib_memcpy (new_n_clone, new_n, sizeof (*new_n));
899 /* none of the copied nodes have enqueue rights given out */
900 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
902 if (j >= vec_len (old_nodes_clone))
904 /* new node, set to zero */
905 memset (&new_n_clone->stats_total, 0,
906 sizeof (new_n_clone->stats_total));
907 memset (&new_n_clone->stats_last_clear, 0,
908 sizeof (new_n_clone->stats_last_clear));
912 /* Copy stats if the old data is valid */
913 clib_memcpy (&new_n_clone->stats_total,
914 &old_n_clone->stats_total,
915 sizeof (new_n_clone->stats_total));
916 clib_memcpy (&new_n_clone->stats_last_clear,
917 &old_n_clone->stats_last_clear,
918 sizeof (new_n_clone->stats_last_clear));
920 /* keep previous node state */
921 new_n_clone->state = old_n_clone->state;
923 vec_add1 (nm_clone->nodes, new_n_clone);
925 /* Free the old node clone */
926 for (j = 0; j < vec_len(old_nodes_clone); j++)
927 clib_mem_free (old_nodes_clone[j]);
928 vec_free (old_nodes_clone);
930 vec_free (nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
932 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
933 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
935 /* clone input node runtime */
936 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
938 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
939 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
941 vec_foreach(rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
943 rt->cpu_index = vm_clone->cpu_index;
946 for (j=0; j < vec_len(old_rt); j++)
948 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
949 rt->state = old_rt[j].state;
954 nm_clone->processes = vec_dup (nm->processes);
956 clib_mem_set_heap (oldheap);
958 // vnet_main_fork_fixup (i);
962 static clib_error_t *
963 cpu_config (vlib_main_t * vm, unformat_input_t * input)
965 vlib_thread_registration_t *tr;
967 vlib_thread_main_t * tm = &vlib_thread_main;
973 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
974 tm->n_thread_stacks = 1; /* account for main thread */
980 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword)tr);
984 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
986 if (unformat (input, "main-thread-io"))
987 tm->main_thread_is_io_node = 1;
988 else if (unformat (input, "use-pthreads"))
989 tm->use_pthreads = 1;
990 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
992 else if (unformat (input, "main-core %u", &tm->main_lcore))
994 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
996 else if (unformat (input, "coremask-%s %llx", &name, &coremask))
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];
1004 if (tr->use_pthreads)
1005 return clib_error_return (0, "coremask cannot be set for '%s' threads",
1008 tr->coremask = clib_bitmap_set_multiple
1009 (tr->coremask, 0, coremask, BITS(coremask));
1010 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1012 else if (unformat (input, "corelist-%s %U", &name, unformat_bitmap_list,
1015 p = hash_get_mem (tm->thread_registrations_by_name, name);
1017 return clib_error_return (0, "no such thread type '%s'", name);
1019 tr = (vlib_thread_registration_t *)p[0];
1021 if (tr->use_pthreads)
1022 return clib_error_return (0, "corelist cannot be set for '%s' threads",
1025 tr->coremask = bitmap;
1026 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1028 else if (unformat (input, "%s %u", &name, &count))
1030 p = hash_get_mem (tm->thread_registrations_by_name, name);
1032 return clib_error_return (0, "no such thread type '%s'", name);
1034 tr = (vlib_thread_registration_t *)p[0];
1035 if (tr->fixed_count)
1036 return clib_error_return
1037 (0, "number of %s threads not configurable", tr->name);
1046 if (!tm->thread_prefix)
1047 tm->thread_prefix = format(0, "vpp");
1051 tm->n_thread_stacks += tr->count;
1052 tm->n_pthreads += tr->count * tr->use_pthreads;
1053 tm->n_eal_threads += tr->count * (tr->use_pthreads == 0);
1060 VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1062 #if !defined (__x86_64__) && !defined (__aarch64__) && !defined (__powerpc64__) && !defined(__arm__)
1063 void __sync_fetch_and_add_8 (void)
1065 fformat(stderr, "%s called\n", __FUNCTION__);
1068 void __sync_add_and_fetch_8 (void)
1070 fformat(stderr, "%s called\n", __FUNCTION__);
1075 void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
1076 void vnet_main_fixup (vlib_fork_fixup_t which) { }
1078 void vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1080 vlib_main_t * vm = vlib_get_main();
1082 if (vlib_mains == 0)
1085 ASSERT(os_get_cpu_number() == 0);
1086 vlib_worker_thread_barrier_sync(vm);
1090 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1091 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1097 vlib_worker_thread_barrier_release(vm);
1100 void vlib_worker_thread_barrier_sync(vlib_main_t *vm)
1108 count = vec_len (vlib_mains) - 1;
1110 /* Tolerate recursive calls */
1111 if (++vlib_worker_threads[0].recursion_level > 1)
1114 vlib_worker_threads[0].barrier_sync_count++;
1116 ASSERT (os_get_cpu_number() == 0);
1118 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1120 *vlib_worker_threads->wait_at_barrier = 1;
1121 while (*vlib_worker_threads->workers_at_barrier != count)
1123 if (vlib_time_now(vm) > deadline)
1125 fformat(stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1131 void vlib_worker_thread_barrier_release(vlib_main_t * vm)
1138 if (--vlib_worker_threads[0].recursion_level > 0)
1141 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1143 *vlib_worker_threads->wait_at_barrier = 0;
1145 while (*vlib_worker_threads->workers_at_barrier > 0)
1147 if (vlib_time_now(vm) > deadline)
1149 fformat(stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1155 static clib_error_t *
1156 show_threads_fn (vlib_main_t * vm,
1157 unformat_input_t * input,
1158 vlib_cli_command_t * cmd)
1160 vlib_worker_thread_t * w;
1163 vlib_cli_output (vm, "%-7s%-20s%-12s%-8s%-7s%-7s%-7s%-10s",
1164 "ID", "Name", "Type", "LWP",
1165 "lcore", "Core", "Socket", "State");
1167 #if !defined(__powerpc64__)
1168 for (i = 0; i < vec_len(vlib_worker_threads); i++)
1170 w = vlib_worker_threads + i;
1173 line = format(line, "%-7d%-20s%-12s%-8d",
1175 w->name ? w->name : (u8 *) "",
1176 w->registration ? w->registration->name : "",
1180 int lcore = w->dpdk_lcore_id;
1183 line = format(line, "%-7u%-7u%-7u",
1185 lcore_config[lcore].core_id,
1186 lcore_config[lcore].socket_id);
1188 switch(lcore_config[lcore].state)
1191 line = format(line, "wait");
1194 line = format(line, "running");
1197 line = format(line, "finished");
1200 line = format(line, "unknown");
1204 vlib_cli_output(vm, "%v", line);
1213 VLIB_CLI_COMMAND (show_threads_command, static) = {
1214 .path = "show threads",
1215 .short_help = "Show threads",
1216 .function = show_threads_fn,