FD.io VPP  v18.07.1-19-g511ce25
Vector Packet Processing
threads.c
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 #define _GNU_SOURCE
16 
17 #include <signal.h>
18 #include <math.h>
19 #include <vppinfra/format.h>
20 #include <vlib/vlib.h>
21 
22 #include <vlib/threads.h>
23 #include <vlib/unix/cj.h>
24 
26 
27 #define FRAME_QUEUE_NELTS 32
28 
29 u32
30 vl (void *p)
31 {
32  return vec_len (p);
33 }
34 
37 
38 /*
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.
43  */
44 
45 #ifdef BARRIER_TRACING
46  /*
47  * Output of barrier tracing can be to syslog or elog as suits
48  */
49 #ifdef BARRIER_TRACING_ELOG
50 static u32
51 elog_id_for_msg_name (const char *msg_name)
52 {
53  uword *p, r;
54  static uword *h;
55  u8 *name_copy;
56 
57  if (!h)
58  h = hash_create_string (0, sizeof (uword));
59 
60  p = hash_get_mem (h, msg_name);
61  if (p)
62  return p[0];
63  r = elog_string (&vlib_global_main.elog_main, "%s", msg_name);
64 
65  name_copy = format (0, "%s%c", msg_name, 0);
66 
67  hash_set_mem (h, name_copy, r);
68 
69  return r;
70 }
71 
72  /*
73  * elog Barrier trace functions, which are nulled out if BARRIER_TRACING isn't
74  * defined
75  */
76 
77 static inline void
78 barrier_trace_sync (f64 t_entry, f64 t_open, f64 t_closed)
79 {
80  /* *INDENT-OFF* */
81  ELOG_TYPE_DECLARE (e) =
82  {
83  .format = "barrier <%d#%s(O:%dus:%dus)(%dus)",
84  .format_args = "i4T4i4i4i4",
85  };
86  /* *INDENT-ON* */
87  struct
88  {
89  u32 count, caller, t_entry, t_open, t_closed;
90  } *ed = 0;
91 
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);
98 }
99 
100 static inline void
101 barrier_trace_sync_rec (f64 t_entry)
102 {
103  /* *INDENT-OFF* */
104  ELOG_TYPE_DECLARE (e) =
105  {
106  .format = "barrier <%d(%dus)%s",
107  .format_args = "i4i4T4",
108  };
109  /* *INDENT-ON* */
110  struct
111  {
112  u32 depth, t_entry, caller;
113  } *ed = 0;
114 
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);
119 }
120 
121 static inline void
123 {
124  /* *INDENT-OFF* */
125  ELOG_TYPE_DECLARE (e) =
126  {
127  .format = "barrier (%dus)%d>",
128  .format_args = "i4i4",
129  };
130  /* *INDENT-ON* */
131  struct
132  {
133  u32 t_entry, depth;
134  } *ed = 0;
135 
137  ed->t_entry = (int) (1000000.0 * t_entry);
138  ed->depth = (int) vlib_worker_threads[0].recursion_level;
139 }
140 
141 static inline void
142 barrier_trace_release (f64 t_entry, f64 t_closed_total, f64 t_update_main)
143 {
144  /* *INDENT-OFF* */
145  ELOG_TYPE_DECLARE (e) =
146  {
147  .format = "barrier (%dus){%d}(C:%dus)#%d>",
148  .format_args = "i4i4i4i4",
149  };
150  /* *INDENT-ON* */
151  struct
152  {
153  u32 t_entry, t_update_main, t_closed_total, count;
154  } *ed = 0;
155 
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;
161 
162  /* Reset context for next trace */
163  vlib_worker_threads[0].barrier_context = NULL;
164 }
165 #else
166 char barrier_trace[65536];
167 char *btp = barrier_trace;
168 
169  /*
170  * syslog Barrier trace functions, which are nulled out if BARRIER_TRACING
171  * isn't defined
172  */
173 
174 
175 static inline void
176 barrier_trace_sync (f64 t_entry, f64 t_open, f64 t_closed)
177 {
178  btp += sprintf (btp, "<%u#%s",
179  (unsigned int) vlib_worker_threads[0].barrier_sync_count,
180  vlib_worker_threads[0].barrier_caller);
181 
182  if (vlib_worker_threads[0].barrier_context)
183  {
184  btp += sprintf (btp, "[%s]", vlib_worker_threads[0].barrier_context);
185 
186  }
187 
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));
191 
192 }
193 
194 static inline void
195 barrier_trace_sync_rec (f64 t_entry)
196 {
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);
201 }
202 
203 static inline void
205 {
206  btp += sprintf (btp, ":(%dus)%u>", (int) (1000000.0 * t_entry),
207  (int) vlib_worker_threads[0].recursion_level);
208 }
209 
210 static inline void
211 barrier_trace_release (f64 t_entry, f64 t_closed_total, f64 t_update_main)
212 {
213 
214  btp += sprintf (btp, ":(%dus)", (int) (1000000.0 * t_entry));
215  if (t_update_main > 0)
216  {
217  btp += sprintf (btp, "{%dus}", (int) (1000000.0 * t_update_main));
218  }
219 
220  btp += sprintf (btp, "(C:%dus)#%u>",
221  (int) (1000000.0 * t_closed_total),
222  (int) vlib_worker_threads[0].barrier_sync_count);
223 
224  /* Dump buffer to syslog, and reset for next trace */
225  fformat (stderr, "BTRC %s\n", barrier_trace);
226  btp = barrier_trace;
227  vlib_worker_threads[0].barrier_context = NULL;
228 }
229 #endif
230 #else
231 
232  /* Null functions for default case where barrier tracing isn't used */
233 static inline void
234 barrier_trace_sync (f64 t_entry, f64 t_open, f64 t_closed)
235 {
236 }
237 
238 static inline void
240 {
241 }
242 
243 static inline void
245 {
246 }
247 
248 static inline void
249 barrier_trace_release (f64 t_entry, f64 t_closed_total, f64 t_update_main)
250 {
251 }
252 #endif
253 
254 uword
256 {
257  u32 len;
258 
259  len = vec_len (vlib_thread_stacks);
260  if (len == 0)
261  return 1;
262  else
263  return len;
264 }
265 
266 void
268 {
269  int pthread_setname_np (pthread_t __target_thread, const char *__name);
270  int rv;
271  pthread_t thread = pthread_self ();
272 
273  if (thread)
274  {
275  rv = pthread_setname_np (thread, name);
276  if (rv)
277  clib_warning ("pthread_setname_np returned %d", rv);
278  }
279 }
280 
281 static int
282 sort_registrations_by_no_clone (void *a0, void *a1)
283 {
284  vlib_thread_registration_t **tr0 = a0;
285  vlib_thread_registration_t **tr1 = a1;
286 
287  return ((i32) ((*tr0)->no_data_structure_clone)
288  - ((i32) ((*tr1)->no_data_structure_clone)));
289 }
290 
291 static uword *
293 {
294  FILE *fp;
295  uword *r = 0;
296 
297  fp = fopen (filename, "r");
298 
299  if (fp != NULL)
300  {
301  u8 *buffer = 0;
302  vec_validate (buffer, 256 - 1);
303  if (fgets ((char *) buffer, 256, fp))
304  {
305  unformat_input_t in;
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");
310  unformat_free (&in);
311  }
312  vec_free (buffer);
313  fclose (fp);
314  }
315  return r;
316 }
317 
318 
319 /* Called early in the init sequence */
320 
321 clib_error_t *
323 {
327  u32 n_vlib_mains = 1;
328  u32 first_index = 1;
329  u32 i;
330  uword *avail_cpu;
331 
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");
337 
338  avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
339 
340  /* skip cores */
341  for (i = 0; i < tm->skip_cores; i++)
342  {
343  uword c = clib_bitmap_first_set (avail_cpu);
344  if (c == ~0)
345  return clib_error_return (0, "no available cpus to skip");
346 
347  avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
348  }
349 
350  /* grab cpu for main thread */
351  if (tm->main_lcore == ~0)
352  {
353  /* if main-lcore is not set, we try to use lcore 1 */
354  if (clib_bitmap_get (avail_cpu, 1))
355  tm->main_lcore = 1;
356  else
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"
360  " main thread");
361  }
362  else
363  {
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);
367  }
368  avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
369 
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);
373 
374  /* pin main thread to main_lcore */
376  {
378  }
379  else
380  {
381  cpu_set_t cpuset;
382  CPU_ZERO (&cpuset);
383  CPU_SET (tm->main_lcore, &cpuset);
384  pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
385  }
386 
387  /* as many threads as stacks... */
388  vec_validate_aligned (vlib_worker_threads, vec_len (vlib_thread_stacks) - 1,
390 
391  /* Preallocate thread 0 */
392  _vec_len (vlib_worker_threads) = 1;
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;
400 
401  if (tm->sched_policy != ~0)
402  {
403  struct sched_param sched_param;
404  if (!sched_getparam (w->lwp, &sched_param))
405  {
406  if (tm->sched_priority != ~0)
407  sched_param.sched_priority = tm->sched_priority;
408  sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
409  }
410  }
411 
412  /* assign threads to cores and set n_vlib_mains */
413  tr = tm->next;
414 
415  while (tr)
416  {
417  vec_add1 (tm->registrations, tr);
418  tr = tr->next;
419  }
420 
422 
423  for (i = 0; i < vec_len (tm->registrations); i++)
424  {
425  int j;
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;
430 
431  /* construct coremask */
432  if (tr->use_pthreads || !tr->count)
433  continue;
434 
435  if (tr->coremask)
436  {
437  uword c;
438  /* *INDENT-OFF* */
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);
443 
444  avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
445  }));
446 /* *INDENT-ON* */
447 
448  }
449  else
450  {
451  for (j = 0; j < tr->count; j++)
452  {
453  uword c = clib_bitmap_first_set (avail_cpu);
454  if (c == ~0)
455  return clib_error_return (0,
456  "no available cpus to be used for"
457  " the '%s' thread", tr->name);
458 
459  avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
460  tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
461  }
462  }
463  }
464 
465  clib_bitmap_free (avail_cpu);
466 
467  tm->n_vlib_mains = n_vlib_mains;
468 
469  vec_validate_aligned (vlib_worker_threads, first_index - 1,
471 
472  return 0;
473 }
474 
477 {
478  vlib_frame_queue_t *fq;
479 
480  fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
481  memset (fq, 0, sizeof (*fq));
482  fq->nelts = nelts;
483  fq->vector_threshold = 128; // packets
485 
486  if (1)
487  {
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");
494 
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))
499  {
500  fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
501  abort ();
502  }
503  }
504 
505  return (fq);
506 }
507 
508 void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
509 void
511 {
512 }
513 
514 /* Turned off, save as reference material... */
515 #if 0
516 static inline int
517 vlib_frame_queue_dequeue_internal (int thread_id,
518  vlib_main_t * vm, vlib_node_main_t * nm)
519 {
520  vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
522  vlib_frame_t *f;
525  u32 node_runtime_index;
526  int msg_type;
527  u64 before;
528  int processed = 0;
529 
530  ASSERT (vm == vlib_mains[thread_id]);
531 
532  while (1)
533  {
534  if (fq->head == fq->tail)
535  return processed;
536 
537  elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
538 
539  if (!elt->valid)
540  return processed;
541 
542  before = clib_cpu_time_now ();
543 
544  f = elt->frame;
545  node_runtime_index = elt->node_runtime_index;
546  msg_type = elt->msg_type;
547 
548  switch (msg_type)
549  {
550  case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
552  /* note fallthrough... */
553  case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
555  node_runtime_index);
556  vlib_frame_free (vm, r, f);
557  break;
559  vec_add2 (vm->node_main.pending_frames, p, 1);
561  p->node_runtime_index = elt->node_runtime_index;
562  p->frame_index = vlib_frame_index (vm, f);
564  fq->dequeue_vectors += (u64) f->n_vectors;
565  break;
566  case VLIB_FRAME_QUEUE_ELT_API_MSG:
568  break;
569  default:
570  clib_warning ("bogus frame queue message, type %d", msg_type);
571  break;
572  }
573  elt->valid = 0;
574  fq->dequeues++;
575  fq->dequeue_ticks += clib_cpu_time_now () - before;
577  fq->head++;
578  processed++;
579  }
580  ASSERT (0);
581  return processed;
582 }
583 
584 int
585 vlib_frame_queue_dequeue (int thread_id,
586  vlib_main_t * vm, vlib_node_main_t * nm)
587 {
588  return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
589 }
590 
591 int
592 vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
593  u32 frame_queue_index, vlib_frame_t * frame,
595 {
596  vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
598  u32 save_count;
599  u64 new_tail;
600  u64 before = clib_cpu_time_now ();
601 
602  ASSERT (fq);
603 
604  new_tail = __sync_add_and_fetch (&fq->tail, 1);
605 
606  /* Wait until a ring slot is available */
607  while (new_tail >= fq->head + fq->nelts)
608  {
609  f64 b4 = vlib_time_now_ticks (vm, before);
611  /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
612  // vlib_frame_queue_dequeue (vm->thread_index, vm, nm);
613  }
614 
615  elt = fq->elts + (new_tail & (fq->nelts - 1));
616 
617  /* this would be very bad... */
618  while (elt->valid)
619  {
620  }
621 
622  /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
623  save_count = frame->n_vectors;
624 
625  elt->frame = frame;
626  elt->node_runtime_index = node_runtime_index;
627  elt->msg_type = type;
629  elt->valid = 1;
630 
631  return save_count;
632 }
633 #endif /* 0 */
634 
635 /* To be called by vlib worker threads upon startup */
636 void
638 {
640 
641  /*
642  * Note: disabling signals in worker threads as follows
643  * prevents the api post-mortem dump scheme from working
644  * {
645  * sigset_t s;
646  * sigfillset (&s);
647  * pthread_sigmask (SIG_SETMASK, &s, 0);
648  * }
649  */
650 
652 
653  if (vec_len (tm->thread_prefix) && w->registration->short_name)
654  {
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);
658  }
659 
660  if (!w->registration->use_pthreads)
661  {
662 
663  /* Initial barrier sync, for both worker and i/o threads */
664  clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
665 
666  while (*vlib_worker_threads->wait_at_barrier)
667  ;
668 
669  clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
670  }
671 }
672 
673 void *
675 {
676  void *rv;
677  vlib_worker_thread_t *w = arg;
678 
679  w->lwp = syscall (SYS_gettid);
680  w->thread_id = pthread_self ();
681 
682  __os_thread_index = w - vlib_worker_threads;
683 
684  rv = (void *) clib_calljmp
685  ((uword (*)(uword)) w->thread_function,
687  /* NOTREACHED, we hope */
688  return rv;
689 }
690 
691 static clib_error_t *
692 vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned lcore_id)
693 {
695  void *(*fp_arg) (void *) = fp;
696 
697  w->lcore_id = lcore_id;
699  return tm->cb.vlib_launch_thread_cb (fp, (void *) w, lcore_id);
700  else
701  {
702  pthread_t worker;
703  cpu_set_t cpuset;
704  CPU_ZERO (&cpuset);
705  CPU_SET (lcore_id, &cpuset);
706 
707  if (pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w))
708  return clib_error_return_unix (0, "pthread_create");
709 
710  if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
711  return clib_error_return_unix (0, "pthread_setaffinity_np");
712 
713  return 0;
714  }
715 }
716 
717 static clib_error_t *
719 {
720  int i, j;
722  vlib_main_t *vm_clone;
723  void *oldheap;
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);
731 
732  vec_reset_length (vlib_worker_threads);
733 
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";
738 
739  if (vec_len (tm->thread_prefix))
740  {
741  w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
742  vlib_set_thread_name ((char *) w->name);
743  }
744 
745  /*
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.
749  */
750  main_heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
751  vm->elog_main.lock =
753  vm->elog_main.lock[0] = 0;
754 
755  if (n_vlib_mains > 1)
756  {
757  /* Replace hand-crafted length-1 vector with a real vector */
758  vlib_mains = 0;
759 
762  _vec_len (vlib_mains) = 0;
764 
765  vlib_worker_threads->wait_at_barrier =
767  vlib_worker_threads->workers_at_barrier =
769 
770  vlib_worker_threads->node_reforks_required =
772 
773  /* Ask for an initial barrier sync */
774  *vlib_worker_threads->workers_at_barrier = 0;
775  *vlib_worker_threads->wait_at_barrier = 1;
776 
777  /* Without update or refork */
778  *vlib_worker_threads->node_reforks_required = 0;
780 
781  /* init timing */
782  vm->barrier_epoch = 0;
783  vm->barrier_no_close_before = 0;
784 
785  worker_thread_index = 1;
786 
787  for (i = 0; i < vec_len (tm->registrations); i++)
788  {
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;
792  int k;
793 
794  tr = tm->registrations[i];
795 
796  if (tr->count == 0)
797  continue;
798 
799  for (k = 0; k < tr->count; k++)
800  {
801  vlib_node_t *n;
802 
803  vec_add2 (vlib_worker_threads, w, 1);
804  if (tr->mheap_size)
805  w->thread_mheap =
806  mheap_alloc (0 /* use VM */ , tr->mheap_size);
807  else
808  w->thread_mheap = main_heap;
809 
810  w->thread_stack =
811  vlib_thread_stack_init (w - vlib_worker_threads);
812  w->thread_function = tr->function;
813  w->thread_function_arg = w;
814  w->instance_id = k;
815  w->registration = tr;
816 
817  w->elog_track.name =
818  (char *) format (0, "%s %d", tr->name, k + 1);
819  vec_add1 (w->elog_track.name, 0);
821 
822  if (tr->no_data_structure_clone)
823  continue;
824 
825  /* Fork vlib_global_main et al. Look for bugs here */
826  oldheap = clib_mem_set_heap (w->thread_mheap);
827 
828  vm_clone = clib_mem_alloc_aligned (sizeof (*vm_clone),
830  clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
831 
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));
841 
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,
847  for (j = 0; j < vec_len (nm_clone->next_frames); j++)
848  {
849  vlib_next_frame_t *nf = &nm_clone->next_frames[j];
850  u32 save_node_runtime_index;
851  u32 save_flags;
852 
853  save_node_runtime_index = nf->node_runtime_index;
854  save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
856  nf->node_runtime_index = save_node_runtime_index;
857  nf->flags = save_flags;
858  }
859 
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;
864 
865  /* fork nodes */
866  nm_clone->nodes = 0;
867 
868  /* Allocate all nodes in single block for speed */
869  n = clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*n));
870 
871  for (j = 0; j < vec_len (nm->nodes); j++)
872  {
873  clib_memcpy (n, nm->nodes[j], sizeof (*n));
874  /* none of the copied nodes have enqueue rights given out */
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);
880  n++;
881  }
885  vec_foreach (rt,
887  {
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)
894  n->runtime_data_bytes));
895  }
896 
901  {
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)
908  n->runtime_data_bytes));
909  }
910 
911  nm_clone->processes = vec_dup_aligned (nm->processes,
913 
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));
917 
918  /* Packet trace buffers are guaranteed to be empty, nothing to do here */
919 
920  clib_mem_set_heap (oldheap);
922 
924  (vlib_mains[0]->error_main.counters, CLIB_CACHE_LINE_BYTES);
926  (vlib_mains[0]->error_main.counters_last_clear,
928 
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;
932 
933  /* *INDENT-OFF* */
934  pool_foreach (fl_orig, orig_freelist_pool,
935  ({
937  fl_clone, CLIB_CACHE_LINE_BYTES);
938  ASSERT (fl_orig - orig_freelist_pool
939  == fl_clone - vm_clone->buffer_free_list_pool);
940 
941  fl_clone[0] = fl_orig[0];
942  fl_clone->buffers = 0;
943  fl_clone->n_alloc = 0;
944  }));
945 /* *INDENT-ON* */
946 
947  worker_thread_index++;
948  }
949  }
950  }
951  else
952  {
953  /* only have non-data-structure copy threads to create... */
954  for (i = 0; i < vec_len (tm->registrations); i++)
955  {
956  tr = tm->registrations[i];
957 
958  for (j = 0; j < tr->count; j++)
959  {
960  vec_add2 (vlib_worker_threads, w, 1);
961  if (tr->mheap_size)
962  w->thread_mheap =
963  mheap_alloc (0 /* use VM */ , tr->mheap_size);
964  else
965  w->thread_mheap = main_heap;
966  w->thread_stack =
967  vlib_thread_stack_init (w - vlib_worker_threads);
968  w->thread_function = tr->function;
969  w->thread_function_arg = w;
970  w->instance_id = j;
971  w->elog_track.name =
972  (char *) format (0, "%s %d", tr->name, j + 1);
973  w->registration = tr;
974  vec_add1 (w->elog_track.name, 0);
976  }
977  }
978  }
979 
980  worker_thread_index = 1;
981 
982  for (i = 0; i < vec_len (tm->registrations); i++)
983  {
984  clib_error_t *err;
985  int j;
986 
987  tr = tm->registrations[i];
988 
989  if (tr->use_pthreads || tm->use_pthreads)
990  {
991  for (j = 0; j < tr->count; j++)
992  {
993  w = vlib_worker_threads + worker_thread_index++;
995  w, 0);
996  if (err)
997  clib_error_report (err);
998  }
999  }
1000  else
1001  {
1002  uword c;
1003  /* *INDENT-OFF* */
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,
1007  w, c);
1008  if (err)
1009  clib_error_report (err);
1010  }));
1011  /* *INDENT-ON* */
1012  }
1013  }
1016  return 0;
1017 }
1018 
1020 
1021 
1022 static inline void
1024 {
1025  int i, j;
1026  vlib_main_t *vm;
1027  vlib_node_main_t *nm, *nm_clone;
1028  vlib_main_t *vm_clone;
1029  vlib_node_runtime_t *rt;
1030  never_inline void
1032  vlib_node_runtime_t * r,
1033  uword n_calls,
1034  uword n_vectors, uword n_clocks);
1035 
1036  ASSERT (vlib_get_thread_index () == 0);
1037 
1038  vm = vlib_mains[0];
1039  nm = &vm->node_main;
1040 
1041  ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
1042 
1043  /*
1044  * Scrape all runtime stats, so we don't lose node runtime(s) with
1045  * pending counts, or throw away worker / io thread counts.
1046  */
1047  for (j = 0; j < vec_len (nm->nodes); j++)
1048  {
1049  vlib_node_t *n;
1050  n = nm->nodes[j];
1051  vlib_node_sync_stats (vm, n);
1052  }
1053 
1054  for (i = 1; i < vec_len (vlib_mains); i++)
1055  {
1056  vlib_node_t *n;
1057 
1058  vm_clone = vlib_mains[i];
1059  nm_clone = &vm_clone->node_main;
1060 
1061  for (j = 0; j < vec_len (nm_clone->nodes); j++)
1062  {
1063  n = nm_clone->nodes[j];
1064 
1065  rt = vlib_node_get_runtime (vm_clone, n->index);
1066  vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
1067  }
1068  }
1069 
1070  /* Per-worker clone rebuilds are now done on each thread */
1071 }
1072 
1073 
1074 void
1076 {
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;
1081 
1082  vlib_node_t *new_n_clone;
1083 
1084  int j;
1085 
1086  vm = vlib_mains[0];
1087  nm = &vm->node_main;
1088  vm_clone = vlib_get_main ();
1089  nm_clone = &vm_clone->node_main;
1090 
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;
1094 
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;
1102 
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,
1107 
1108  for (j = 0; j < vec_len (nm_clone->next_frames); j++)
1109  {
1110  vlib_next_frame_t *nf = &nm_clone->next_frames[j];
1111  u32 save_node_runtime_index;
1112  u32 save_flags;
1113 
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;
1119  }
1120 
1121  old_nodes_clone = nm_clone->nodes;
1122  nm_clone->nodes = 0;
1123 
1124  /* re-fork nodes */
1125 
1126  /* Allocate all nodes in single block for speed */
1127  new_n_clone =
1128  clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*new_n_clone));
1129  for (j = 0; j < vec_len (nm->nodes); j++)
1130  {
1131  vlib_node_t *old_n_clone;
1132  vlib_node_t *new_n;
1133 
1134  new_n = nm->nodes[j];
1135  old_n_clone = old_nodes_clone[j];
1136 
1137  clib_memcpy (new_n_clone, new_n, sizeof (*new_n));
1138  /* none of the copied nodes have enqueue rights given out */
1140 
1141  if (j >= vec_len (old_nodes_clone))
1142  {
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));
1148  }
1149  else
1150  {
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));
1158 
1159  /* keep previous node state */
1160  new_n_clone->state = old_n_clone->state;
1161  }
1162  vec_add1 (nm_clone->nodes, new_n_clone);
1163  new_n_clone++;
1164  }
1165  /* Free the old node clones */
1166  clib_mem_free (old_nodes_clone[0]);
1167 
1168  vec_free (old_nodes_clone);
1169 
1170 
1171  /* re-clone internal nodes */
1172  old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL];
1176 
1178  {
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)
1185  n->runtime_data_bytes));
1186  }
1187 
1188  for (j = 0; j < vec_len (old_rt); j++)
1189  {
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,
1194  }
1195 
1196  vec_free (old_rt);
1197 
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] =
1203 
1205  {
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)
1212  n->runtime_data_bytes));
1213  }
1214 
1215  for (j = 0; j < vec_len (old_rt); j++)
1216  {
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,
1221  }
1222 
1223  vec_free (old_rt);
1224 
1225  nm_clone->processes = vec_dup_aligned (nm->processes,
1227 }
1228 
1229 void
1231 {
1232  /*
1233  * Make a note that we need to do a node runtime update
1234  * prior to releasing the barrier.
1235  */
1237 }
1238 
1239 u32
1240 unformat_sched_policy (unformat_input_t * input, va_list * args)
1241 {
1242  u32 *r = va_arg (*args, u32 *);
1243 
1244  if (0);
1245 #define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
1247 #undef _
1248  else
1249  return 0;
1250  return 1;
1251 }
1252 
1253 static clib_error_t *
1255 {
1257  uword *p;
1259  u8 *name;
1260  u64 coremask;
1261  uword *bitmap;
1262  u32 count;
1263 
1265 
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;
1270 
1271  tr = tm->next;
1272 
1273  while (tr)
1274  {
1276  tr = tr->next;
1277  }
1278 
1279  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1280  {
1281  if (unformat (input, "use-pthreads"))
1282  tm->use_pthreads = 1;
1283  else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
1284  ;
1285  else if (unformat (input, "main-core %u", &tm->main_lcore))
1286  ;
1287  else if (unformat (input, "skip-cores %u", &tm->skip_cores))
1288  ;
1289  else if (unformat (input, "coremask-%s %llx", &name, &coremask))
1290  {
1292  if (p == 0)
1293  return clib_error_return (0, "no such thread type '%s'", name);
1294 
1295  tr = (vlib_thread_registration_t *) p[0];
1296 
1297  if (tr->use_pthreads)
1298  return clib_error_return (0,
1299  "coremask cannot be set for '%s' threads",
1300  name);
1301 
1303  (tr->coremask, 0, coremask, BITS (coremask));
1305  }
1306  else if (unformat (input, "corelist-%s %U", &name, unformat_bitmap_list,
1307  &bitmap))
1308  {
1310  if (p == 0)
1311  return clib_error_return (0, "no such thread type '%s'", name);
1312 
1313  tr = (vlib_thread_registration_t *) p[0];
1314 
1315  if (tr->use_pthreads)
1316  return clib_error_return (0,
1317  "corelist cannot be set for '%s' threads",
1318  name);
1319 
1320  tr->coremask = bitmap;
1322  }
1323  else
1324  if (unformat
1325  (input, "scheduler-policy %U", unformat_sched_policy,
1326  &tm->sched_policy))
1327  ;
1328  else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
1329  ;
1330  else if (unformat (input, "%s %u", &name, &count))
1331  {
1333  if (p == 0)
1334  return clib_error_return (0, "no such thread type 3 '%s'", name);
1335 
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);
1340  tr->count = count;
1341  }
1342  else
1343  break;
1344  }
1345 
1346  if (tm->sched_priority != ~0)
1347  {
1348  if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
1349  {
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;
1356  }
1357  else
1358  {
1359  return clib_error_return
1360  (0,
1361  "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1362  tm->sched_priority);
1363  }
1364  }
1365  tr = tm->next;
1366 
1367  if (!tm->thread_prefix)
1368  tm->thread_prefix = format (0, "vpp");
1369 
1370  while (tr)
1371  {
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);
1375  tr = tr->next;
1376  }
1377 
1378  return 0;
1379 }
1380 
1382 
1383 #if !defined (__x86_64__) && !defined (__i386__) && !defined (__aarch64__) && !defined (__powerpc64__) && !defined(__arm__)
1384 void
1385 __sync_fetch_and_add_8 (void)
1386 {
1387  fformat (stderr, "%s called\n", __FUNCTION__);
1388  abort ();
1389 }
1390 
1391 void
1392 __sync_add_and_fetch_8 (void)
1393 {
1394  fformat (stderr, "%s called\n", __FUNCTION__);
1395  abort ();
1396 }
1397 #endif
1398 
1399 void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
1400 void
1402 {
1403 }
1404 
1405 void
1407 {
1408  vlib_main_t *vm = vlib_get_main ();
1409 
1410  if (vlib_mains == 0)
1411  return;
1412 
1413  ASSERT (vlib_get_thread_index () == 0);
1415 
1416  switch (which)
1417  {
1420  break;
1421 
1422  default:
1423  ASSERT (0);
1424  }
1426 }
1427 
1428  /*
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)
1433  */
1434 
1435 #ifndef BARRIER_MINIMUM_OPEN_LIMIT
1436 #define BARRIER_MINIMUM_OPEN_LIMIT 0.001
1437 #endif
1438 
1439 #ifndef BARRIER_MINIMUM_OPEN_FACTOR
1440 #define BARRIER_MINIMUM_OPEN_FACTOR 3
1441 #endif
1442 
1443 void
1445 {
1446  f64 deadline;
1447  f64 now;
1448  f64 t_entry;
1449  f64 t_open;
1450  f64 t_closed;
1451  f64 max_vector_rate;
1452  u32 count;
1453  int i;
1454 
1455  if (vec_len (vlib_mains) < 2)
1456  return;
1457 
1458  ASSERT (vlib_get_thread_index () == 0);
1459 
1460  count = vec_len (vlib_mains) - 1;
1461 
1462  /* Record entry relative to last close */
1463  now = vlib_time_now (vm);
1464  t_entry = now - vm->barrier_epoch;
1465 
1466  /* Tolerate recursive calls */
1467  if (++vlib_worker_threads[0].recursion_level > 1)
1468  {
1469  barrier_trace_sync_rec (t_entry);
1470  return;
1471  }
1472 
1473  /*
1474  * Need data to decide if we're working hard enough to honor
1475  * the barrier hold-down timer.
1476  */
1477  max_vector_rate = 0.0;
1478  for (i = 1; i < vec_len (vlib_mains); i++)
1479  max_vector_rate =
1480  clib_max (max_vector_rate,
1482 
1483  vlib_worker_threads[0].barrier_sync_count++;
1484 
1485  /* Enforce minimum barrier open time to minimize packet loss */
1487 
1488  /*
1489  * If any worker thread seems busy, which we define
1490  * as a vector rate above 10, we enforce the barrier hold-down timer
1491  */
1492  if (max_vector_rate > 10.0)
1493  {
1494  while (1)
1495  {
1496  now = vlib_time_now (vm);
1497  /* Barrier hold-down timer expired? */
1498  if (now >= vm->barrier_no_close_before)
1499  break;
1500  if ((vm->barrier_no_close_before - now)
1501  > (2.0 * BARRIER_MINIMUM_OPEN_LIMIT))
1502  {
1503  clib_warning
1504  ("clock change: would have waited for %.4f seconds",
1505  (vm->barrier_no_close_before - now));
1506  break;
1507  }
1508  }
1509  }
1510  /* Record time of closure */
1511  t_open = now - vm->barrier_epoch;
1512  vm->barrier_epoch = now;
1513 
1514  deadline = now + BARRIER_SYNC_TIMEOUT;
1515 
1516  *vlib_worker_threads->wait_at_barrier = 1;
1517  while (*vlib_worker_threads->workers_at_barrier != count)
1518  {
1519  if ((now = vlib_time_now (vm)) > deadline)
1520  {
1521  fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1522  os_panic ();
1523  }
1524  }
1525 
1526  t_closed = now - vm->barrier_epoch;
1527 
1528  barrier_trace_sync (t_entry, t_open, t_closed);
1529 
1530 }
1531 
1532 void vlib_stat_segment_lock (void) __attribute__ ((weak));
1533 void
1535 {
1536 }
1537 
1538 void vlib_stat_segment_unlock (void) __attribute__ ((weak));
1539 void
1541 {
1542 }
1543 
1544 void
1546 {
1547  f64 deadline;
1548  f64 now;
1549  f64 minimum_open;
1550  f64 t_entry;
1551  f64 t_closed_total;
1552  f64 t_update_main = 0.0;
1553  int refork_needed = 0;
1554 
1555  if (vec_len (vlib_mains) < 2)
1556  return;
1557 
1558  ASSERT (vlib_get_thread_index () == 0);
1559 
1560 
1561  now = vlib_time_now (vm);
1562  t_entry = now - vm->barrier_epoch;
1563 
1564  if (--vlib_worker_threads[0].recursion_level > 0)
1565  {
1566  barrier_trace_release_rec (t_entry);
1567  return;
1568  }
1569 
1570  /* Update (all) node runtimes before releasing the barrier, if needed */
1572  {
1573  /*
1574  * Lock stat segment here, so we's safe when
1575  * rebuilding the stat segment node clones from the
1576  * stat thread...
1577  */
1579 
1580  /* Do stats elements on main thread */
1583 
1584  /* Do per thread rebuilds in parallel */
1585  refork_needed = 1;
1586  clib_smp_atomic_add (vlib_worker_threads->node_reforks_required,
1587  (vec_len (vlib_mains) - 1));
1588  now = vlib_time_now (vm);
1589  t_update_main = now - vm->barrier_epoch;
1590  }
1591 
1592  deadline = now + BARRIER_SYNC_TIMEOUT;
1593 
1594  /*
1595  * Note when we let go of the barrier.
1596  * Workers can use this to derive a reasonably accurate
1597  * time offset. See vlib_time_now(...)
1598  */
1601 
1602  *vlib_worker_threads->wait_at_barrier = 0;
1603 
1604  while (*vlib_worker_threads->workers_at_barrier > 0)
1605  {
1606  if ((now = vlib_time_now (vm)) > deadline)
1607  {
1608  fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1609  os_panic ();
1610  }
1611  }
1612 
1613  /* Wait for reforks before continuing */
1614  if (refork_needed)
1615  {
1616  now = vlib_time_now (vm);
1617 
1618  deadline = now + BARRIER_SYNC_TIMEOUT;
1619 
1620  while (*vlib_worker_threads->node_reforks_required > 0)
1621  {
1622  if ((now = vlib_time_now (vm)) > deadline)
1623  {
1624  fformat (stderr, "%s: worker thread refork deadlock\n",
1625  __FUNCTION__);
1626  os_panic ();
1627  }
1628  }
1630  }
1631 
1632  t_closed_total = now - vm->barrier_epoch;
1633 
1634  minimum_open = t_closed_total * BARRIER_MINIMUM_OPEN_FACTOR;
1635 
1636  if (minimum_open > BARRIER_MINIMUM_OPEN_LIMIT)
1637  {
1638  minimum_open = BARRIER_MINIMUM_OPEN_LIMIT;
1639  }
1640 
1641  vm->barrier_no_close_before = now + minimum_open;
1642 
1643  /* Record barrier epoch (used to enforce minimum open time) */
1644  vm->barrier_epoch = now;
1645 
1646  barrier_trace_release (t_entry, t_closed_total, t_update_main);
1647 
1648 }
1649 
1650 /*
1651  * Check the frame queue to see if any frames are available.
1652  * If so, pull the packets off the frames and put them to
1653  * the handoff node.
1654  */
1655 int
1657 {
1658  u32 thread_id = vm->thread_index;
1659  vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id];
1661  u32 *from, *to;
1662  vlib_frame_t *f;
1663  int msg_type;
1664  int processed = 0;
1665  u32 n_left_to_node;
1666  u32 vectors = 0;
1667 
1668  ASSERT (fq);
1669  ASSERT (vm == vlib_mains[thread_id]);
1670 
1671  if (PREDICT_FALSE (fqm->node_index == ~0))
1672  return 0;
1673  /*
1674  * Gather trace data for frame queues
1675  */
1676  if (PREDICT_FALSE (fq->trace))
1677  {
1678  frame_queue_trace_t *fqt;
1680  u32 elix;
1681 
1682  fqt = &fqm->frame_queue_traces[thread_id];
1683 
1684  fqt->nelts = fq->nelts;
1685  fqt->head = fq->head;
1686  fqt->head_hint = fq->head_hint;
1687  fqt->tail = fq->tail;
1688  fqt->threshold = fq->vector_threshold;
1689  fqt->n_in_use = fqt->tail - fqt->head;
1690  if (fqt->n_in_use >= fqt->nelts)
1691  {
1692  // if beyond max then use max
1693  fqt->n_in_use = fqt->nelts - 1;
1694  }
1695 
1696  /* Record the number of elements in use in the histogram */
1697  fqh = &fqm->frame_queue_histogram[thread_id];
1698  fqh->count[fqt->n_in_use]++;
1699 
1700  /* Record a snapshot of the elements in use */
1701  for (elix = 0; elix < fqt->nelts; elix++)
1702  {
1703  elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1704  if (1 || elt->valid)
1705  {
1706  fqt->n_vectors[elix] = elt->n_vectors;
1707  }
1708  }
1709  fqt->written = 1;
1710  }
1711 
1712  while (1)
1713  {
1714  if (fq->head == fq->tail)
1715  {
1716  fq->head_hint = fq->head;
1717  return processed;
1718  }
1719 
1720  elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
1721 
1722  if (!elt->valid)
1723  {
1724  fq->head_hint = fq->head;
1725  return processed;
1726  }
1727 
1728  from = elt->buffer_index;
1729  msg_type = elt->msg_type;
1730 
1732  ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1733 
1734  f = vlib_get_frame_to_node (vm, fqm->node_index);
1735 
1736  to = vlib_frame_vector_args (f);
1737 
1738  n_left_to_node = elt->n_vectors;
1739 
1740  while (n_left_to_node >= 4)
1741  {
1742  to[0] = from[0];
1743  to[1] = from[1];
1744  to[2] = from[2];
1745  to[3] = from[3];
1746  to += 4;
1747  from += 4;
1748  n_left_to_node -= 4;
1749  }
1750 
1751  while (n_left_to_node > 0)
1752  {
1753  to[0] = from[0];
1754  to++;
1755  from++;
1756  n_left_to_node--;
1757  }
1758 
1759  vectors += elt->n_vectors;
1760  f->n_vectors = elt->n_vectors;
1761  vlib_put_frame_to_node (vm, fqm->node_index, f);
1762 
1763  elt->valid = 0;
1764  elt->n_vectors = 0;
1765  elt->msg_type = 0xfefefefe;
1767  fq->head++;
1768  processed++;
1769 
1770  /*
1771  * Limit the number of packets pushed into the graph
1772  */
1773  if (vectors >= fq->vector_threshold)
1774  {
1775  fq->head_hint = fq->head;
1776  return processed;
1777  }
1778  }
1779  ASSERT (0);
1780  return processed;
1781 }
1782 
1783 void
1785 {
1788  vlib_main_t *vm = vlib_get_main ();
1789  clib_error_t *e;
1790 
1792 
1794  clib_time_init (&vm->clib_time);
1796 
1797  /* Wait until the dpdk init sequence is complete */
1798  while (tm->extern_thread_mgmt && tm->worker_thread_release == 0)
1800 
1802  (vm, vm->worker_init_function_registrations, 1 /* call_once */ );
1803  if (e)
1804  clib_error_report (e);
1805 
1806  vlib_worker_loop (vm);
1807 }
1808 
1809 /* *INDENT-OFF* */
1810 VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1811  .name = "workers",
1812  .short_name = "wk",
1813  .function = vlib_worker_thread_fn,
1814 };
1815 /* *INDENT-ON* */
1816 
1817 u32
1818 vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1819 {
1822  vlib_frame_queue_t *fq;
1823  int i;
1824 
1825  if (frame_queue_nelts == 0)
1826  frame_queue_nelts = FRAME_QUEUE_NELTS;
1827 
1828  vec_add2 (tm->frame_queue_mains, fqm, 1);
1829 
1830  fqm->node_index = node_index;
1831 
1833  _vec_len (fqm->vlib_frame_queues) = 0;
1834  for (i = 0; i < tm->n_vlib_mains; i++)
1835  {
1836  fq = vlib_frame_queue_alloc (frame_queue_nelts);
1837  vec_add1 (fqm->vlib_frame_queues, fq);
1838  }
1839 
1840  return (fqm - tm->frame_queue_mains);
1841 }
1842 
1843 int
1845 {
1847 
1848  if (tm->extern_thread_mgmt)
1849  return -1;
1850 
1852  tm->extern_thread_mgmt = 1;
1853  return 0;
1854 }
1855 
1856 void
1858  args)
1859 {
1860  ASSERT (vlib_get_thread_index () == 0);
1862  args->type_opaque, args->data);
1863 }
1864 
1866 
1867 void
1868 vlib_rpc_call_main_thread (void *callback, u8 * args, u32 arg_size)
1869 {
1871  {
1872  void (*fp) (void *, u8 *, u32) = rpc_call_main_thread_cb_fn;
1873  (*fp) (callback, args, arg_size);
1874  }
1875  else
1876  clib_warning ("BUG: rpc_call_main_thread_cb_fn NULL!");
1877 }
1878 
1879 clib_error_t *
1881 {
1882  return 0;
1883 }
1884 
1886 
1887 
1888 static clib_error_t *
1890  unformat_input_t * input, vlib_cli_command_t * cmd)
1891 {
1892  int i;
1893  f64 now;
1894 
1895  now = vlib_time_now (vm);
1896 
1897  vlib_cli_output (vm, "Time now %.9f", now);
1898 
1899  if (vec_len (vlib_mains) == 1)
1900  return 0;
1901 
1902  vlib_cli_output (vm, "Time last barrier release %.9f",
1904 
1905  for (i = 1; i < vec_len (vlib_mains); i++)
1906  {
1907  if (vlib_mains[i] == 0)
1908  continue;
1909  vlib_cli_output (vm, "Thread %d offset %.9f error %.9f", i,
1910  vlib_mains[i]->time_offset,
1913  }
1914  return 0;
1915 }
1916 
1917 /* *INDENT-OFF* */
1918 VLIB_CLI_COMMAND (f_command, static) =
1919 {
1920  .path = "show clock",
1921  .short_help = "show clock",
1922  .function = show_clock_command_fn,
1923 };
1924 /* *INDENT-ON* */
1925 
1926 /*
1927  * fd.io coding-style-patch-verification: ON
1928  *
1929  * Local Variables:
1930  * eval: (c-set-style "gnu")
1931  * End:
1932  */
_vlib_init_function_list_elt_t * worker_init_function_registrations
Definition: main.h:183
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:437
static void barrier_trace_release_rec(f64 t_entry)
Definition: threads.c:244
static void barrier_trace_sync(f64 t_entry, f64 t_open, f64 t_closed)
Definition: threads.c:234
u32 vl(void *p)
Definition: threads.c:30
static f64 vlib_last_vectors_per_main_loop_as_f64(vlib_main_t *vm)
Definition: main.h:310
uword * pending_rpc_requests
Definition: main.h:218
vlib_main_t vlib_global_main
Definition: main.c:1644
never_inline void vlib_node_runtime_sync_stats(vlib_main_t *vm, vlib_node_runtime_t *r, uword n_calls, uword n_vectors, uword n_clocks)
Definition: main.c:540
clib_error_t *(* vlib_thread_set_lcore_cb)(u32 thread, u16 lcore)
Definition: threads.h:286
#define clib_min(x, y)
Definition: clib.h:289
vlib_process_t ** processes
Definition: node.h:717
#define VLIB_PENDING_FRAME_NO_NEXT_FRAME
Definition: node.h:442
static void vlib_buffer_free(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers Frees the entire buffer chain for each buffer.
Definition: buffer_funcs.h:534
u8 runtime_data[0]
Function dependent node-runtime data.
Definition: node.h:500
int vlib_frame_queue_enqueue(vlib_main_t *vm, u32 node_runtime_index, u32 frame_queue_index, vlib_frame_t *frame, vlib_frame_queue_msg_type_t type)
#define VLIB_MAIN_LOOP_ENTER_FUNCTION(x)
Definition: init.h:159
word elog_track_register(elog_main_t *em, elog_track_t *t)
register an event track
Definition: elog.c:198
unsigned long u64
Definition: types.h:89
void * mheap_alloc(void *memory, uword size)
Definition: mheap.c:963
#define CLIB_MEMORY_STORE_BARRIER()
Definition: clib.h:112
#define NULL
Definition: clib.h:55
u32 index
Definition: node.h:273
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:228
#define vec_add2_aligned(V, P, N, A)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:574
clib_error_t * threads_init(vlib_main_t *vm)
Definition: threads.c:1880
void os_panic(void)
Definition: unix-misc.c:174
u32 vlib_frame_queue_main_init(u32 node_index, u32 frame_queue_nelts)
Definition: threads.c:1818
u32 thread_index
Definition: main.h:179
void * thread_function_arg
Definition: threads.h:114
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:523
static int sort_registrations_by_no_clone(void *a0, void *a1)
Definition: threads.c:282
static u64 clib_cpu_time_now(void)
Definition: time.h:73
frame_queue_trace_t * frame_queue_traces
Definition: threads.h:170
void vlib_process_signal_event_mt_helper(vlib_process_signal_event_mt_args_t *args)
Definition: threads.c:1857
elog_track_t elog_track
Definition: threads.h:116
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:562
int i
static mheap_t * mheap_header(u8 *v)
void vnet_main_fixup(vlib_fork_fixup_t which)
Definition: threads.c:1401
static uword * clib_bitmap_set(uword *ai, uword i, uword value)
Sets the ith bit of a bitmap to new_value Removes trailing zeros from the bitmap. ...
Definition: bitmap.h:167
#define hash_set_mem(h, key, value)
Definition: hash.h:275
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
clib_time_t clib_time
Definition: main.h:63
void vlib_worker_thread_fn(void *arg)
Definition: threads.c:1784
u32 unformat_sched_policy(unformat_input_t *input, va_list *args)
Definition: threads.c:1240
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:448
struct vlib_thread_registration_ * next
Definition: threads.h:47
#define MHEAP_FLAG_THREAD_SAFE
u32 buffer_index[VLIB_FRAME_SIZE]
Definition: threads.h:98
void * runtime_data
Definition: node.h:279
volatile u32 valid
Definition: threads.h:92
vlib_main_t ** vlib_mains
Definition: buffer.c:303
unsigned char u8
Definition: types.h:56
#define clib_bitmap_dup(v)
Duplicate a bitmap.
Definition: bitmap.h:87
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
double f64
Definition: types.h:142
pthread_t thread[MAX_CONNS]
Definition: main.c:125
u8 state
Definition: node.h:301
u16 thread_index
thread this node runs on
Definition: node.h:498
#define vlib_worker_thread_barrier_sync(X)
Definition: threads.h:212
u64 * counters_last_clear
Definition: error.h:81
static void vlib_worker_thread_barrier_check(void)
Definition: threads.h:399
vlib_thread_registration_t * next
Definition: threads.h:292
#define vec_add1_aligned(V, E, A)
Add 1 element to end of vector (alignment specified).
Definition: vec.h:533
#define VLIB_NODE_RUNTIME_DATA_SIZE
Definition: node.h:511
vlib_node_stats_t stats_last_clear
Definition: node.h:267
#define clib_smp_atomic_add(addr, increment)
Definition: smp.h:46
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:443
void vlib_worker_thread_node_runtime_update(void)
Definition: threads.c:1230
u64 count[FRAME_QUEUE_MAX_NELTS]
Definition: node.h:761
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:156
#define VLIB_INVALID_NODE_INDEX
Definition: node.h:361
vlib_frame_queue_msg_type_t
Definition: threads.h:84
vlib_node_t ** nodes
Definition: node.h:676
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:182
#define clib_error_return(e, args...)
Definition: error.h:99
uword * lock
SMP lock, non-zero means locking required.
Definition: elog.h:172
uword * cpu_core_bitmap
Definition: threads.h:329
#define BARRIER_MINIMUM_OPEN_FACTOR
Definition: threads.c:1440
clib_error_t * vlib_call_init_exit_functions(vlib_main_t *vm, _vlib_init_function_list_elt_t *head, int call_once)
Definition: init.c:43
vlib_frame_queue_elt_t * elts
Definition: threads.h:159
f64 time_last_barrier_release
Definition: main.h:66
unsigned int u32
Definition: types.h:88
vlib_node_runtime_t * nodes_by_type[VLIB_N_NODE_TYPE]
Definition: node.h:686
#define VLIB_FRAME_SIZE
Definition: node.h:364
void vlib_set_thread_name(char *name)
Definition: threads.c:267
void vl_msg_api_handler_no_free(void *)
Definition: threads.c:510
#define hash_create_string(elts, value_bytes)
Definition: hash.h:690
void unformat_init_string(unformat_input_t *input, char *string, int string_len)
Definition: unformat.c:1023
vlib_fork_fixup_t
Definition: threads.h:236
#define BARRIER_SYNC_TIMEOUT
Definition: threads.h:204
void * rpc_call_main_thread_cb_fn
Definition: threads.c:1865
VLIB_REGISTER_THREAD(worker_thread_reg, static)
int extern_thread_mgmt
Definition: threads.h:348
vlib_worker_thread_t * vlib_worker_threads
Definition: threads.c:35
#define clib_bitmap_foreach(i, ai, body)
Macro to iterate across set bits in a bitmap.
Definition: bitmap.h:361
u32 next_frame_index
Definition: node.h:439
vlib_node_stats_t stats_total
Definition: node.h:263
volatile u64 head
Definition: threads.h:146
u16 state
Input node state.
Definition: node.h:488
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:952
static uword clib_bitmap_first_set(uword *ai)
Return the lowest numbered set bit in a bitmap.
Definition: bitmap.h:385
u8 * vlib_thread_stack_init(uword thread_index)
Definition: main.c:614
static void vlib_next_frame_init(vlib_next_frame_t *nf)
Definition: node.h:422
vlib_error_main_t error_main
Definition: main.h:144
static u32 vlib_frame_index(vlib_main_t *vm, vlib_frame_t *f)
Definition: node_funcs.h:245
vlib_thread_callbacks_t cb
Definition: threads.h:347
#define VLIB_FRAME_NO_FREE_AFTER_DISPATCH
Definition: node.h:398
int vlib_thread_cb_register(struct vlib_main_t *vm, vlib_thread_callbacks_t *cb)
Definition: threads.c:1844
#define v
Definition: acl.c:491
struct _unformat_input_t unformat_input_t
char * name
Track name vector.
Definition: elog.h:115
#define clib_error_return_unix(e, args...)
Definition: error.h:102
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:191
static void * clib_mem_get_per_cpu_heap(void)
Definition: mem.h:58
void vlib_frame_free(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_frame_t *f)
Definition: main.c:211
#define ELOG_DATA(em, f)
Definition: elog.h:481
static clib_error_t * vlib_launch_thread_int(void *fp, vlib_worker_thread_t *w, unsigned lcore_id)
Definition: threads.c:692
#define PREDICT_FALSE(x)
Definition: clib.h:105
void vlib_worker_thread_node_refork(void)
Definition: threads.c:1075
vlib_buffer_free_list_t * buffer_free_list_pool
Definition: main.h:111
u32 node_index
Node index.
Definition: node.h:473
uword * init_functions_called
Definition: main.h:176
void clib_time_init(clib_time_t *c)
Definition: time.c:178
uword * frame_size_hash
Definition: node.h:732
vlib_thread_main_t vlib_thread_main
Definition: threads.c:36
word fformat(FILE *f, char *fmt,...)
Definition: format.c:453
void(* thread_function)(void *)
Definition: threads.h:113
static clib_error_t * cpu_config(vlib_main_t *vm, unformat_input_t *input)
Definition: threads.c:1254
#define pool_get_aligned(P, E, A)
Allocate an object E from a pool P (general version).
Definition: pool.h:188
i32 n_vectors[FRAME_QUEUE_MAX_NELTS]
Definition: node.h:756
u64 * counters
Definition: error.h:78
u32 owner_node_index
Definition: node.h:344
vlib_frame_queue_t * vlib_frame_queue_alloc(int nelts)
Definition: threads.c:476
volatile u64 tail
Definition: threads.h:138
#define clib_mem_alloc_no_fail(size)
Definition: mem.h:156
#define VLIB_EARLY_CONFIG_FUNCTION(x, n,...)
Definition: init.h:195
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
svmdb_client_t * c
u16 n_vectors
Definition: node.h:380
vlib_frame_queue_t ** vlib_frame_queues
Definition: threads.h:167
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:221
vlib_main_t * vm
Definition: buffer.c:294
u32 node_runtime_index
Definition: node.h:433
vlib_pending_frame_t * pending_frames
Definition: node.h:702
vlib_thread_function_t * function
Definition: threads.h:52
int vlib_frame_queue_dequeue(vlib_main_t *vm, vlib_frame_queue_main_t *fqm)
Definition: threads.c:1656
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:339
void * heap_base
Definition: main.h:107
static void * clib_mem_set_heap(void *heap)
Definition: mem.h:226
#define clib_warning(format, args...)
Definition: error.h:59
static vlib_node_runtime_t * vlib_node_get_runtime(vlib_main_t *vm, u32 node_index)
Get node runtime by node index.
Definition: node_funcs.h:89
#define clib_memcpy(a, b, c)
Definition: string.h:75
elog_main_t elog_main
Definition: main.h:161
frame_queue_nelt_counter_t * frame_queue_histogram
Definition: threads.h:171
static uword * clib_bitmap_set_multiple(uword *bitmap, uword i, uword value, uword n_bits)
sets the ith through ith + n_bits bits in a bitmap
Definition: bitmap.h:275
#define VLIB_FRAME_PENDING
Definition: node.h:409
static uword clib_bitmap_get(uword *ai, uword i)
Gets the ith bit value from a bitmap.
Definition: bitmap.h:197
#define ELOG_TYPE_DECLARE(f)
Definition: elog.h:439
void vlib_worker_thread_init(vlib_worker_thread_t *w)
Definition: threads.c:637
uword os_get_nthreads(void)
Definition: threads.c:255
static void * clib_mem_get_heap(void)
Definition: mem.h:220
volatile u32 * wait_at_barrier
Definition: threads.h:106
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
#define FRAME_QUEUE_NELTS
Definition: threads.c:27
void vlib_stat_segment_unlock(void)
Definition: threads.c:1540
#define never_inline
Definition: clib.h:89
signed int i32
Definition: types.h:81
#define hash_create(elts, value_bytes)
Definition: hash.h:696
#define ASSERT(truth)
static clib_error_t * show_clock_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: threads.c:1889
static void barrier_trace_sync_rec(f64 t_entry)
Definition: threads.c:239
vlib_frame_queue_main_t * frame_queue_mains
Definition: threads.h:335
static u32 elog_id_for_msg_name(mc_main_t *m, char *msg_name)
Definition: mc.c:46
u16 flags
Definition: node.h:371
static void clib_mem_free(void *p)
Definition: mem.h:179
#define clib_error_report(e)
Definition: error.h:113
#define clib_bitmap_free(v)
Free a bitmap.
Definition: bitmap.h:92
void vlib_worker_thread_barrier_sync_int(vlib_main_t *vm)
Definition: threads.c:1444
size_t count
Definition: vapi.c:46
int need_vlib_worker_thread_node_runtime_update
Definition: main.h:206
uword * thread_registrations_by_name
Definition: threads.h:297
#define BARRIER_MINIMUM_OPEN_LIMIT
Definition: threads.c:1436
volatile u32 * node_reforks_required
Definition: threads.h:125
#define clib_max(x, y)
Definition: clib.h:282
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
void vlib_node_sync_stats(vlib_main_t *vm, vlib_node_t *n)
Definition: main.c:570
static uword clib_bitmap_count_set_bits(uword *ai)
Return the number of set bits in a bitmap.
Definition: bitmap.h:462
static void barrier_trace_release(f64 t_entry, f64 t_closed_total, f64 t_update_main)
Definition: threads.c:249
void vlib_worker_loop(vlib_main_t *vm)
Definition: main.c:1639
#define vec_dup_aligned(V, A)
Return copy of vector (no header, alignment specified).
Definition: vec.h:382
u32 elog_string(elog_main_t *em, char *fmt,...)
add a string to the event-log string table
Definition: elog.c:534
f64 barrier_no_close_before
Definition: main.h:215
static clib_error_t * start_workers(vlib_main_t *vm)
Definition: threads.c:718
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
void vlib_rpc_call_main_thread(void *callback, u8 *args, u32 arg_size)
Definition: threads.c:1868
DECLARE_CJ_GLOBAL_LOG
Definition: threads.c:25
vlib_node_main_t node_main
Definition: main.h:135
u64 uword
Definition: types.h:112
vlib_next_frame_t * next_frames
Definition: node.h:699
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:982
static void unformat_free(unformat_input_t *i)
Definition: format.h:162
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:267
volatile u64 head_hint
Definition: threads.h:155
#define VLIB_THREAD_STACK_SIZE
Definition: threads.h:82
f64 barrier_epoch
Definition: main.h:212
vlib_frame_size_t * frame_sizes
Definition: node.h:735
clib_error_t *(* vlib_launch_thread_cb)(void *fp, vlib_worker_thread_t *w, unsigned lcore_id)
Definition: threads.h:284
#define hash_get_mem(h, key)
Definition: hash.h:269
static void worker_thread_node_runtime_update_internal(void)
Definition: threads.c:1023
static void * clib_mem_alloc_aligned(uword size, uword align)
Definition: mem.h:120
volatile u32 * workers_at_barrier
Definition: threads.h:107
uword clib_calljmp(uword(*func)(uword func_arg), uword func_arg, void *stack)
static uword * clib_sysfs_list_to_bitmap(char *filename)
Definition: threads.c:292
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1545
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
static f64 vlib_time_now_ticks(vlib_main_t *vm, u64 n)
Definition: main.h:234
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:59
void vlib_stat_segment_lock(void)
Definition: threads.c:1534
#define vec_foreach(var, vec)
Vector iterator.
void * vlib_worker_thread_bootstrap_fn(void *arg)
Definition: threads.c:674
#define CLIB_MEMORY_BARRIER()
Definition: clib.h:109
u32 node_runtime_index
Definition: node.h:392
uword * cpu_socket_bitmap
Definition: threads.h:332
#define foreach_sched_policy
Definition: threads.h:267
vlib_thread_registration_t ** registrations
Definition: threads.h:295
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:62
u8 ** vlib_thread_stacks
Definition: main.c:597
pthread_t thread_id
Definition: threads.h:129
vlib_thread_registration_t * registration
Definition: threads.h:118
#define BITS(x)
Definition: clib.h:58
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:681
volatile u32 worker_thread_release
Definition: threads.h:338
void vlib_worker_thread_fork_fixup(vlib_fork_fixup_t which)
Definition: threads.c:1406
clib_random_buffer_t random_buffer
Definition: main.h:173
u8 runtime_data_bytes
Definition: node.h:304
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170
#define VLIB_FRAME_FREE_AFTER_DISPATCH
Definition: node.h:412
clib_error_t * vlib_thread_init(vlib_main_t *vm)
Definition: threads.c:322