FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
stat_segment.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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 
16 #include <vppinfra/mem.h>
17 #include <vlib/vlib.h>
18 #include <vlib/unix/unix.h>
19 #include "stat_segment.h"
20 #include <vnet/vnet.h>
21 #include <vnet/devices/devices.h> /* vnet_get_aggregate_rx_packets */
22 #undef HAVE_MEMFD_CREATE
23 #include <vppinfra/linux/syscall.h>
25 #include <vppinfra/mheap.h>
26 
28 
29 /*
30  * Used only by VPP writers
31  */
32 void
34 {
37  sm->shared_header->in_progress = 1;
38 }
39 
40 void
42 {
44  sm->shared_header->epoch++;
45  sm->shared_header->in_progress = 0;
47 }
48 
49 /*
50  * Change heap to the stats shared memory segment
51  */
52 void *
54 {
56 
57  sm->last = old;
58  ASSERT (sm && sm->shared_header);
59  return clib_mem_set_heap (sm->heap);
60 }
61 
62 static u32
64 {
67  hash_pair_t *hp;
68 
69  /* Must be called in the context of the main heap */
70  ASSERT (clib_mem_get_heap () != sm->heap);
71 
72  hp = hash_get_pair (sm->directory_vector_by_name, name);
73  if (hp)
74  {
75  index = hp->value[0];
76  }
77 
78  return index;
79 }
80 
81 static void
83 {
85 
86  /* Must be called in the context of the main heap */
87  ASSERT (clib_mem_get_heap () != sm->heap);
88 
89  hash_set (sm->directory_vector_by_name, format (0, "%s%c", name, 0), index);
90 }
91 
92 static u32
94 {
96  u32 next_vector_index = vec_len (sm->directory_vector);
97 
98  ssize_t i;
100  {
102  {
103  next_vector_index = i;
104  break;
105  }
106  }
107 
108  return next_vector_index;
109 }
110 
111 static u32
113 {
115 
116  ASSERT (clib_mem_get_heap () == sm->heap);
117 
119 
120  clib_mem_set_heap (oldheap);
121  create_hash_index ((u8 *) e->name, index);
122  clib_mem_set_heap (sm->heap);
123 
124  vec_validate (sm->directory_vector, index);
125  sm->directory_vector[index] = *e;
126 
127  return index;
128 }
129 
130 static void
131 vlib_stats_delete_counter (u32 index, void *oldheap)
132 {
135 
136  ASSERT (clib_mem_get_heap () == sm->heap);
137 
138  if (index > vec_len (sm->directory_vector))
139  return;
140 
141  e = &sm->directory_vector[index];
142 
143  clib_mem_set_heap (oldheap);
145  clib_mem_set_heap (sm->heap);
146 
147  memset (e, 0, sizeof (*e));
149 }
150 
151 /*
152  * Called from main heap
153  */
154 void
155 vlib_stats_delete_cm (void *cm_arg)
156 {
160  stat_segment_shared_header_t *shared_header = sm->shared_header;
161 
162  /* Not all counters have names / hash-table entries */
163  if (!cm->name && !cm->stat_segment_name)
164  {
165  return;
166  }
168 
169  /* Lookup hash-table is on the main heap */
170  char *stat_segment_name =
171  cm->stat_segment_name ? cm->stat_segment_name : cm->name;
172  u32 index = lookup_hash_index ((u8 *) stat_segment_name);
173 
174  e = &sm->directory_vector[index];
176 
177  u64 *offset_vector = stat_segment_pointer (shared_header, e->offset_vector);
178 
179  void *oldheap = clib_mem_set_heap (sm->heap); /* Enter stats segment */
180  vec_free (offset_vector);
181  clib_mem_set_heap (oldheap); /* Exit stats segment */
182 
183  memset (e, 0, sizeof (*e));
185 
187 }
188 
189 void
190 vlib_stats_pop_heap (void *cm_arg, void *oldheap, u32 cindex,
192 {
195  stat_segment_shared_header_t *shared_header = sm->shared_header;
196  char *stat_segment_name;
198 
199  /* Not all counters have names / hash-table entries */
200  if (!cm->name && !cm->stat_segment_name)
201  {
202  clib_mem_set_heap (oldheap);
203  return;
204  }
205 
206  ASSERT (shared_header);
207 
209 
210  /* Lookup hash-table is on the main heap */
211  stat_segment_name =
212  cm->stat_segment_name ? cm->stat_segment_name : cm->name;
213 
214  clib_mem_set_heap (oldheap); /* Exit stats segment */
215  u32 vector_index = lookup_hash_index ((u8 *) stat_segment_name);
216  /* Back to stats segment */
217  clib_mem_set_heap (sm->heap); /* Re-enter stat segment */
218 
219 
220  /* Update the vector */
221  if (vector_index == STAT_SEGMENT_INDEX_INVALID)
222  { /* New */
223  strncpy (e.name, stat_segment_name, 128 - 1);
224  e.type = type;
225  vector_index = vlib_stats_create_counter (&e, oldheap);
226  }
227 
228  stat_segment_directory_entry_t *ep = &sm->directory_vector[vector_index];
229  ep->offset = stat_segment_offset (shared_header, cm->counters); /* Vector of threads of vectors of counters */
230  u64 *offset_vector =
231  ep->offset_vector ? stat_segment_pointer (shared_header,
232  ep->offset_vector) : 0;
233 
234  /* Update the 2nd dimension offset vector */
235  int i;
236  vec_validate (offset_vector, vec_len (cm->counters) - 1);
237 
238  if (sm->last != offset_vector)
239  {
240  for (i = 0; i < vec_len (cm->counters); i++)
241  offset_vector[i] =
242  stat_segment_offset (shared_header, cm->counters[i]);
243  }
244  else
245  offset_vector[cindex] =
246  stat_segment_offset (shared_header, cm->counters[cindex]);
247 
248  ep->offset_vector = stat_segment_offset (shared_header, offset_vector);
249  sm->directory_vector[vector_index].offset =
250  stat_segment_offset (shared_header, cm->counters);
251 
252  /* Reset the client hash table pointer, since it WILL change! */
253  shared_header->directory_offset =
254  stat_segment_offset (shared_header, sm->directory_vector);
255 
257  clib_mem_set_heap (oldheap);
258 }
259 
260 void
261 vlib_stats_register_error_index (void *oldheap, u8 * name, u64 * em_vec,
262  u64 index)
263 {
265  stat_segment_shared_header_t *shared_header = sm->shared_header;
267 
268  ASSERT (shared_header);
269 
271  clib_mem_set_heap (oldheap); /* Exit stats segment */
272  u32 vector_index = lookup_hash_index (name);
273  /* Back to stats segment */
274  clib_mem_set_heap (sm->heap); /* Re-enter stat segment */
275 
276  if (vector_index == STAT_SEGMENT_INDEX_INVALID)
277  {
278  memcpy (e.name, name, vec_len (name));
279  e.name[vec_len (name)] = '\0';
281  e.offset = index;
282  e.offset_vector = 0;
283  vector_index = vlib_stats_create_counter (&e, oldheap);
284 
285  /* Warn clients to refresh any pointers they might be holding */
286  shared_header->directory_offset =
287  stat_segment_offset (shared_header, sm->directory_vector);
288  }
289 
291 }
292 
293 static void
295 {
297  stat_segment_shared_header_t *shared_header = sm->shared_header;
298  counter_t **counters =
299  ep->offset ? stat_segment_pointer (shared_header, ep->offset) : 0;
301  int i;
302  u64 *offset_vector = 0;
303 
304  vec_validate_aligned (counters, tm->n_vlib_mains - 1,
306  ep->offset = stat_segment_offset (shared_header, counters);
307 
308  for (i = 0; i < tm->n_vlib_mains; i++)
309  {
310  vec_validate_aligned (counters[i], max, CLIB_CACHE_LINE_BYTES);
311  vec_add1 (offset_vector,
312  stat_segment_offset (shared_header, counters[i]));
313  }
314 
315  if (ep->offset_vector)
316  {
318  vec_free (p);
319  }
320  ep->offset_vector = stat_segment_offset (shared_header, offset_vector);
321 }
322 
323 always_inline void
325  u32 thread_index, u32 index, u64 value)
326 {
328  stat_segment_shared_header_t *shared_header = sm->shared_header;
329 
330  ASSERT (shared_header);
331  counter_t *offset_vector =
333  counter_t *cb =
334  stat_segment_pointer (sm->shared_header, offset_vector[thread_index]);
335  cb[index] = value;
336 }
337 
338 void
339 vlib_stats_pop_heap2 (u64 * error_vector, u32 thread_index, void *oldheap,
340  int lock)
341 {
343  stat_segment_shared_header_t *shared_header = sm->shared_header;
344 
345  ASSERT (shared_header);
346 
347  if (lock)
349 
350  /* Reset the client hash table pointer, since it WILL change! */
351  vec_validate (sm->error_vector, thread_index);
352  sm->error_vector[thread_index] =
353  stat_segment_offset (shared_header, error_vector);
354 
355  shared_header->error_offset =
356  stat_segment_offset (shared_header, sm->error_vector);
357  shared_header->directory_offset =
358  stat_segment_offset (shared_header, sm->directory_vector);
359 
360  if (lock)
362  clib_mem_set_heap (oldheap);
363 }
364 
365 clib_error_t *
367 {
369  stat_segment_shared_header_t *shared_header;
370  void *oldheap;
371  ssize_t memory_size;
372  int mfd;
373  char *mem_name = "stat_segment_test";
374  void *memaddr;
375 
376  memory_size = sm->memory_size;
377  if (memory_size == 0)
378  memory_size = STAT_SEGMENT_DEFAULT_SIZE;
379 
380  /* Create shared memory segment */
381  if ((mfd = memfd_create (mem_name, 0)) < 0)
382  return clib_error_return (0, "stat segment memfd_create failure");
383 
384  /* Set size */
385  if ((ftruncate (mfd, memory_size)) == -1)
386  return clib_error_return (0, "stat segment ftruncate failure");
387 
388  if ((memaddr =
389  mmap (NULL, memory_size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd,
390  0)) == MAP_FAILED)
391  return clib_error_return (0, "stat segment mmap failure");
392 
393  void *heap;
394 #if USE_DLMALLOC == 0
395  heap = mheap_alloc_with_flags (((u8 *) memaddr) + getpagesize (),
396  memory_size - getpagesize (),
399 #else
400  heap =
401  create_mspace_with_base (((u8 *) memaddr) + getpagesize (),
402  memory_size - getpagesize (), 1 /* locked */ );
403  mspace_disable_expand (heap);
404 #endif
405 
406  sm->heap = heap;
407  sm->memfd = mfd;
408 
410  sm->shared_header = shared_header = memaddr;
411 
412  shared_header->version = STAT_SEGMENT_VERSION;
413 
416 
417  oldheap = clib_mem_set_heap (sm->heap);
418 
419  /* Set up the name to counter-vector hash table */
420  sm->directory_vector = 0;
421 
422  shared_header->epoch = 1;
423 
424  /* Scalar stats and node counters */
426 #define _(E,t,n,p) \
427  strcpy(sm->directory_vector[STAT_COUNTER_##E].name, #p "/" #n); \
428  sm->directory_vector[STAT_COUNTER_##E].type = STAT_DIR_TYPE_##t;
430 #undef _
431  /* Save the vector offset in the shared segment, for clients */
432  shared_header->directory_offset =
433  stat_segment_offset (shared_header, sm->directory_vector);
434 
435  clib_mem_set_heap (oldheap);
436 
437  /* Total shared memory size */
439  mheap_usage (sm->heap, &usage);
441  usage.bytes_total;
442 
443  return 0;
444 }
445 
446 static int
447 name_sort_cmp (void *a1, void *a2)
448 {
451 
452  return strcmp ((char *) n1->name, (char *) n2->name);
453 }
454 
455 static u8 *
456 format_stat_dir_entry (u8 * s, va_list * args)
457 {
459  va_arg (*args, stat_segment_directory_entry_t *);
460  char *type_name;
461  char *format_string;
462 
463  format_string = "%-74s %-10s %10lld";
464 
465  switch (ep->type)
466  {
468  type_name = "ScalarPtr";
469  break;
470 
473  type_name = "CMainPtr";
474  break;
475 
477  type_name = "ErrIndex";
478  break;
479 
481  type_name = "NameVector";
482  break;
483 
484  case STAT_DIR_TYPE_EMPTY:
485  type_name = "empty";
486  break;
487 
488  default:
489  type_name = "illegal!";
490  break;
491  }
492 
493  return format (s, format_string, ep->name, type_name, ep->offset);
494 }
495 
496 static clib_error_t *
498  unformat_input_t * input,
499  vlib_cli_command_t * cmd)
500 {
503  int i;
504 
505  int verbose = 0;
506 
507  if (unformat (input, "verbose"))
508  verbose = 1;
509 
510  /* Lock even as reader, as this command doesn't handle epoch changes */
512  show_data = vec_dup (sm->directory_vector);
514 
516 
517  vlib_cli_output (vm, "%-74s %10s %10s", "Name", "Type", "Value");
518 
519  for (i = 0; i < vec_len (show_data); i++)
520  {
522 
523  if (ep->type == STAT_DIR_TYPE_EMPTY)
524  continue;
525 
526  vlib_cli_output (vm, "%-100U", format_stat_dir_entry,
527  vec_elt_at_index (show_data, i));
528  }
529 
530  if (verbose)
531  {
532  ASSERT (sm->heap);
533  vlib_cli_output (vm, "%U", format_mheap, sm->heap, 0 /* verbose */ );
534  }
535 
536  return 0;
537 }
538 
539 /* *INDENT-OFF* */
540 VLIB_CLI_COMMAND (show_stat_segment_command, static) =
541 {
542  .path = "show statistics segment",
543  .short_help = "show statistics segment [verbose]",
544  .function = show_stat_segment_command_fn,
545 };
546 /* *INDENT-ON* */
547 
548 /*
549  * Node performance counters:
550  * total_calls [threads][node-index]
551  * total_vectors
552  * total_calls
553  * total suspends
554  */
555 
556 static inline void
558 {
559  vlib_main_t **stat_vms = 0;
560  vlib_node_t ***node_dups = 0;
561  int i, j;
562  stat_segment_shared_header_t *shared_header = sm->shared_header;
563  static u32 no_max_nodes = 0;
564 
565  vlib_node_get_nodes (0 /* vm, for barrier sync */ ,
566  (u32) ~ 0 /* all threads */ ,
567  1 /* include stats */ ,
568  0 /* barrier sync */ ,
569  &node_dups, &stat_vms);
570 
571  u32 l = vec_len (node_dups[0]);
572 
573  /*
574  * Extend performance nodes if necessary
575  */
576  if (l > no_max_nodes)
577  {
578  void *oldheap = clib_mem_set_heap (sm->heap);
580 
582  [STAT_COUNTER_NODE_CLOCKS], l - 1);
584  [STAT_COUNTER_NODE_VECTORS], l - 1);
586  [STAT_COUNTER_NODE_CALLS], l - 1);
588  [STAT_COUNTER_NODE_SUSPENDS], l - 1);
589 
590  vec_validate (sm->nodes, l - 1);
593  ep->offset = stat_segment_offset (shared_header, sm->nodes);
594 
595  int i;
596  u64 *offset_vector =
597  ep->offset_vector ? stat_segment_pointer (shared_header,
598  ep->offset_vector) : 0;
599  /* Update names dictionary */
600  vec_validate (offset_vector, l - 1);
601  vlib_node_t **nodes = node_dups[0];
602 
603  for (i = 0; i < vec_len (nodes); i++)
604  {
605  vlib_node_t *n = nodes[i];
606  u8 *s = 0;
607  s = format (s, "%v%c", n->name, 0);
608  if (sm->nodes[n->index])
609  vec_free (sm->nodes[n->index]);
610  sm->nodes[n->index] = s;
611  offset_vector[i] =
612  sm->nodes[i] ? stat_segment_offset (shared_header,
613  sm->nodes[i]) : 0;
614 
615  }
616  ep->offset_vector = stat_segment_offset (shared_header, offset_vector);
617 
619  clib_mem_set_heap (oldheap);
620  no_max_nodes = l;
621  }
622 
623  for (j = 0; j < vec_len (node_dups); j++)
624  {
625  vlib_node_t **nodes = node_dups[j];
626 
627  for (i = 0; i < vec_len (nodes); i++)
628  {
629  counter_t **counters;
630  counter_t *c;
631  vlib_node_t *n = nodes[i];
632 
633  counters =
634  stat_segment_pointer (shared_header,
635  sm->directory_vector
637  c = counters[j];
639 
640  counters =
641  stat_segment_pointer (shared_header,
642  sm->directory_vector
644  c = counters[j];
646 
647  counters =
648  stat_segment_pointer (shared_header,
649  sm->directory_vector
651  c = counters[j];
652  c[n->index] = n->stats_total.calls - n->stats_last_clear.calls;
653 
654  counters =
655  stat_segment_pointer (shared_header,
656  sm->directory_vector
658  c = counters[j];
659  c[n->index] =
661  }
662  vec_free (node_dups[j]);
663  }
664  vec_free (node_dups);
665  vec_free (stat_vms);
666 }
667 
668 static void
670 {
671  vlib_main_t *vm = vlib_mains[0];
672  f64 vector_rate;
673  u64 input_packets;
674  f64 dt, now;
675  vlib_main_t *this_vlib_main;
676  int i;
677  static int num_worker_threads_set;
678 
679  /*
680  * Set once at the beginning of time.
681  * Can't do this from the init routine, which happens before
682  * start_workers sets up vlib_mains...
683  */
684  if (PREDICT_FALSE (num_worker_threads_set == 0))
685  {
686  void *oldheap = clib_mem_set_heap (sm->heap);
688 
691  num_worker_threads_set = 1;
693  clib_mem_set_heap (oldheap);
694  }
695 
696  /*
697  * Compute per-worker vector rates, and the average vector rate
698  * across all workers
699  */
700  vector_rate = 0.0;
701 
702  for (i = 0; i < vec_len (vlib_mains); i++)
703  {
704 
705  f64 this_vector_rate;
706 
707  this_vlib_main = vlib_mains[i];
708 
709  this_vector_rate = vlib_internal_node_vector_rate (this_vlib_main);
710  vlib_clear_internal_node_vector_rate (this_vlib_main);
711 
712  vector_rate += this_vector_rate;
713 
714  /* Set the per-worker rate */
717  this_vector_rate);
718  }
719 
720  /* And set the system average rate */
721  vector_rate /= (f64) (i > 1 ? i - 1 : 1);
722 
724 
725  /*
726  * Compute the aggregate input rate
727  */
728  now = vlib_time_now (vm);
730  input_packets = vnet_get_aggregate_rx_packets ();
732  (f64) (input_packets - sm->last_input_packets) / dt;
734  sm->last_input_packets = input_packets;
737 
738  /* Stats segment memory heap counter */
740  mheap_usage (sm->heap, &usage);
742  usage.bytes_used;
743 
744  if (sm->node_counters_enabled)
746 
747  /* *INDENT-OFF* */
749  pool_foreach(g, sm->gauges,
750  ({
751  g->fn(&sm->directory_vector[g->directory_index], g->caller_index);
752  }));
753  /* *INDENT-ON* */
754 
755  /* Heartbeat, so clients detect we're still here */
757 }
758 
759 /*
760  * Accept connection on the socket and exchange the fd for the shared
761  * memory segment.
762  */
763 static clib_error_t *
765 {
767  clib_error_t *err;
768  clib_socket_t client = { 0 };
769 
770  err = clib_socket_accept (sm->socket, &client);
771  if (err)
772  {
773  clib_error_report (err);
774  return err;
775  }
776 
777  /* Send the fd across and close */
778  err = clib_socket_sendmsg (&client, 0, 0, &sm->memfd, 1);
779  if (err)
780  clib_error_report (err);
781  clib_socket_close (&client);
782 
783  return 0;
784 }
785 
786 static void
788 {
790  clib_error_t *error;
792 
793  memset (s, 0, sizeof (clib_socket_t));
794  s->config = (char *) sm->socket_name;
797 
798  if ((error = clib_socket_init (s)))
799  {
800  clib_error_report (error);
801  return;
802  }
803 
804  clib_file_t template = { 0 };
806  template.file_descriptor = s->fd;
807  template.description = format (0, "stats segment listener %s", s->config);
808  clib_file_add (&file_main, &template);
809 
810  sm->socket = s;
811 }
812 
813 static clib_error_t *
815 {
816  /*
817  * cleanup the listener socket on exit.
818  */
820  unlink ((char *) sm->socket_name);
821  return 0;
822 }
823 
825 
826 /* Overrides weak reference in vlib:node_cli.c */
827 f64
829 {
830  return stat_segment_main.update_interval;
831 }
832 
833 static uword
835  vlib_frame_t * f)
836 {
838 
839  while (1)
840  {
843  }
844  return 0; /* or not */
845 }
846 
847 static clib_error_t *
849 {
851 
852  if (sm->socket_name)
854 
855  return 0;
856 }
857 
858 /* *INDENT-OFF* */
860 {
861  .runs_after = VLIB_INITS("unix_input_init"),
862 };
863 /* *INDENT-ON* */
864 
865 clib_error_t *
867  u32 caller_index)
868 {
870  stat_segment_shared_header_t *shared_header = sm->shared_header;
871  void *oldheap;
874 
875  ASSERT (shared_header);
876 
877  u32 vector_index = lookup_hash_index (name);
878 
879  if (vector_index != STAT_SEGMENT_INDEX_INVALID) /* Already registered */
880  return clib_error_return (0, "%v is already registered", name);
881 
882  memset (&e, 0, sizeof (e));
884  memcpy (e.name, name, vec_len (name));
885 
886  oldheap = vlib_stats_push_heap (NULL);
888  vector_index = vlib_stats_create_counter (&e, oldheap);
889 
890  shared_header->directory_offset =
891  stat_segment_offset (shared_header, sm->directory_vector);
892 
894  clib_mem_set_heap (oldheap);
895 
896  /* Back on our own heap */
897  pool_get (sm->gauges, gauge);
898  gauge->fn = update_fn;
899  gauge->caller_index = caller_index;
900  gauge->directory_index = vector_index;
901 
902  return NULL;
903 }
904 
905 clib_error_t *
907 {
909  stat_segment_shared_header_t *shared_header = sm->shared_header;
910  void *oldheap;
912 
913  ASSERT (shared_header);
914  ASSERT (vlib_get_thread_index () == 0);
915 
916  u32 vector_index = lookup_hash_index (name);
917 
918  if (vector_index != STAT_SEGMENT_INDEX_INVALID) /* Already registered */
919  return clib_error_return (0, "%v is already registered", name);
920 
921  memset (&e, 0, sizeof (e));
923  memcpy (e.name, name, vec_len (name));
924 
925  oldheap = vlib_stats_push_heap (NULL);
927 
928  vector_index = vlib_stats_create_counter (&e, oldheap);
929 
930  shared_header->directory_offset =
931  stat_segment_offset (shared_header, sm->directory_vector);
932 
934  clib_mem_set_heap (oldheap);
935 
936  *index = vector_index;
937  return 0;
938 }
939 
940 clib_error_t *
942 {
944  stat_segment_shared_header_t *shared_header = sm->shared_header;
946  void *oldheap;
947 
948  ASSERT (shared_header);
949 
950  if (index > vec_len (sm->directory_vector))
951  return clib_error_return (0, "%u index does not exist", index);
952 
953  e = &sm->directory_vector[index];
955  return clib_error_return (0, "%u index cannot be deleted", index);
956 
957  oldheap = vlib_stats_push_heap (NULL);
959 
960  vlib_stats_delete_counter (index, oldheap);
961 
963  clib_mem_set_heap (oldheap);
964 
965  return 0;
966 }
967 
968 void
970 {
972 
973  ASSERT (index < vec_len (sm->directory_vector));
974  sm->directory_vector[index].index = value;
975 }
976 
977 static clib_error_t *
979 {
981  sm->update_interval = 10.0;
982 
984  {
985  if (unformat (input, "socket-name %s", &sm->socket_name))
986  ;
987  else if (unformat (input, "default"))
988  {
990  sm->socket_name = format (sm->socket_name, "%s",
992  }
993  else if (unformat (input, "size %U",
995  ;
996  else if (unformat (input, "per-node-counters on"))
997  sm->node_counters_enabled = 1;
998  else if (unformat (input, "per-node-counters off"))
999  sm->node_counters_enabled = 0;
1000  else if (unformat (input, "update-interval %f", &sm->update_interval))
1001  ;
1002  else
1003  return clib_error_return (0, "unknown input `%U'",
1004  format_unformat_error, input);
1005  }
1006 
1007  /* set default socket file name when statseg config stanza is empty. */
1008  if (!vec_len (sm->socket_name))
1009  sm->socket_name = format (sm->socket_name, "%s",
1011  /*
1012  * NULL-terminate socket name string
1013  * clib_socket_init()->socket_config() use C str*
1014  */
1016 
1017  return 0;
1018 }
1019 
1021 
1022 static clib_error_t *
1024 {
1026  stat_segment_shared_header_t *shared_header = sm->shared_header;
1027 
1028  void *oldheap = vlib_stats_push_heap (sm->interfaces);
1030 
1031  vec_validate (sm->interfaces, sw_if_index);
1032  if (is_add)
1033  {
1034  vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
1035  vnet_sw_interface_t *si_sup =
1037  vnet_hw_interface_t *hi_sup;
1038 
1040  hi_sup = vnet_get_hw_interface (vnm, si_sup->hw_if_index);
1041 
1042  u8 *s = 0;
1043  s = format (s, "%v", hi_sup->name);
1045  s = format (s, ".%d", si->sub.id);
1046  s = format (s, "%c", 0);
1047  sm->interfaces[sw_if_index] = s;
1048  }
1049  else
1050  {
1051  vec_free (sm->interfaces[sw_if_index]);
1052  sm->interfaces[sw_if_index] = 0;
1053  }
1054 
1057  ep->offset = stat_segment_offset (shared_header, sm->interfaces);
1058 
1059  int i;
1060  u64 *offset_vector =
1061  ep->offset_vector ? stat_segment_pointer (shared_header,
1062  ep->offset_vector) : 0;
1063 
1064  vec_validate (offset_vector, vec_len (sm->interfaces) - 1);
1065 
1066  if (sm->last != sm->interfaces)
1067  {
1068  /* the interface vector moved, so need to recalulate the offset array */
1069  for (i = 0; i < vec_len (sm->interfaces); i++)
1070  {
1071  offset_vector[i] =
1072  sm->interfaces[i] ? stat_segment_offset (shared_header,
1073  sm->interfaces[i]) : 0;
1074  }
1075  }
1076  else
1077  {
1078  offset_vector[sw_if_index] =
1079  sm->interfaces[sw_if_index] ?
1080  stat_segment_offset (shared_header, sm->interfaces[sw_if_index]) : 0;
1081  }
1082  ep->offset_vector = stat_segment_offset (shared_header, offset_vector);
1083 
1085  clib_mem_set_heap (oldheap);
1086 
1087  return 0;
1088 }
1089 
1091 
1092 /* *INDENT-OFF* */
1094 {
1095 .function = stat_segment_collector_process,
1096 .name = "statseg-collector-process",
1097 .type = VLIB_NODE_TYPE_PROCESS,
1098 };
1099 
1100 /* *INDENT-ON* */
1101 
1102 /*
1103  * fd.io coding-style-patch-verification: ON
1104  *
1105  * Local Variables:
1106  * eval: (c-set-style "gnu")
1107  * End:
1108  */
static clib_error_t * statseg_config(vlib_main_t *vm, unformat_input_t *input)
Definition: stat_segment.c:978
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:439
static void stat_set_simple_counter(stat_segment_directory_entry_t *ep, u32 thread_index, u32 index, u64 value)
Definition: stat_segment.c:324
uword bytes_total
Definition: mem.h:315
#define hash_set(h, key, value)
Definition: hash.h:255
static_always_inline void clib_spinlock_unlock(clib_spinlock_t *p)
Definition: lock.h:102
static_always_inline void clib_spinlock_lock(clib_spinlock_t *p)
Definition: lock.h:80
void vlib_stats_pop_heap2(u64 *error_vector, u32 thread_index, void *oldheap, int lock)
Definition: stat_segment.c:339
stat_segment_gauges_pool_t * gauges
Definition: stat_segment.h:86
#define hash_unset(h, key)
Definition: hash.h:261
static u64 vnet_get_aggregate_rx_packets(void)
Definition: devices.h:98
stat_segment_shared_header_t * shared_header
Definition: stat_segment.h:105
uint64_t index
static clib_error_t * stats_socket_accept_ready(clib_file_t *uf)
Definition: stat_segment.c:764
stat_segment_main_t stat_segment_main
Definition: stat_segment.c:27
void * vlib_stats_push_heap(void *old)
Definition: stat_segment.c:53
stat_segment_directory_entry_t * directory_vector
Definition: stat_segment.h:90
unsigned long u64
Definition: types.h:89
u32 index
Definition: node.h:280
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:279
static uint64_t stat_segment_offset(void *start, void *data)
Definition: stat_segment.h:70
static void stat_validate_counter_vector(stat_segment_directory_entry_t *ep, u32 max)
Definition: stat_segment.c:294
static int memfd_create(const char *name, unsigned int flags)
Definition: syscall.h:52
#define STAT_SEGMENT_DEFAULT_SIZE
Definition: stat_segment.h:62
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
#define vec_terminate_c_string(V)
(If necessary) NULL terminate a vector containing a c-string.
Definition: vec.h:1018
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
int i
clib_error_t * clib_socket_init(clib_socket_t *s)
Definition: socket.c:384
static void usage(void)
Definition: health_check.c:14
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
#define foreach_stat_segment_counter_name
Definition: stat_segment.h:43
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:450
#define MHEAP_FLAG_THREAD_SAFE
#define vec_foreach_index_backwards(var, v)
Iterate over vector indices (reverse).
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:236
vlib_main_t ** vlib_mains
Definition: buffer.c:332
uword bytes_used
Definition: mem.h:315
unsigned char u8
Definition: types.h:56
void(* stat_segment_update_fn)(stat_segment_directory_entry_t *e, u32 i)
Definition: stat_segment.h:75
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
clib_file_function_t * read_function
Definition: file.h:67
double f64
Definition: types.h:142
uword value[0]
Definition: hash.h:165
static void create_hash_index(u8 *name, u32 index)
Definition: stat_segment.c:82
#define CLIB_SOCKET_F_IS_SERVER
Definition: socket.h:58
static u32 vlib_stats_get_next_vector_index()
Definition: stat_segment.c:93
static uword vlib_process_suspend(vlib_main_t *vm, f64 dt)
Suspend a vlib cooperative multi-tasking thread for a period of time.
Definition: node_funcs.h:422
DLMALLOC_EXPORT mspace create_mspace_with_base(void *base, size_t capacity, int locked)
vlib_node_stats_t stats_last_clear
Definition: node.h:274
uint64_t value
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
static void do_stat_segment_updates(stat_segment_main_t *sm)
Definition: stat_segment.c:669
vl_api_interface_index_t sw_if_index
Definition: gre.api:50
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
static u32 vlib_stats_create_counter(stat_segment_directory_entry_t *e, void *oldheap)
Definition: stat_segment.c:112
#define always_inline
Definition: clib.h:99
static void * stat_segment_pointer(void *start, uint64_t offset)
void vlib_node_get_nodes(vlib_main_t *vm, u32 max_threads, int include_stats, int barrier_sync, vlib_node_t ****node_dupsp, vlib_main_t ***stat_vmsp)
Get list of nodes.
Definition: node.c:551
#define MHEAP_FLAG_DISABLE_VM
void vlib_stats_pop_heap(void *cm_arg, void *oldheap, u32 cindex, stat_directory_type_t type)
Definition: stat_segment.c:190
static clib_error_t * clib_socket_sendmsg(clib_socket_t *s, void *msg, int msglen, int fds[], int num_fds)
Definition: socket.h:151
uint64_t counter_t
64bit counters
Definition: counter_types.h:22
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
static vnet_sw_interface_t * vnet_get_sup_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
#define clib_error_return(e, args...)
Definition: error.h:99
clib_file_main_t file_main
Definition: main.c:63
#define hash_get_pair(h, key)
Definition: hash.h:252
stat_segment_update_fn fn
Definition: stat_segment.h:79
unsigned int u32
Definition: types.h:88
static clib_error_t * statseg_init(vlib_main_t *vm)
Definition: stat_segment.c:848
A collection of simple counters.
Definition: counter.h:57
static u32 lookup_hash_index(u8 *name)
Definition: stat_segment.c:63
#define hash_create_string(elts, value_bytes)
Definition: hash.h:690
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:63
char * name
The counter collection&#39;s name.
Definition: counter.h:64
vl_api_fib_path_type_t type
Definition: fib_types.api:123
u8 * format_mheap(u8 *s, va_list *va)
Definition: mem_dlmalloc.c:354
static void vlib_stats_delete_counter(u32 index, void *oldheap)
Definition: stat_segment.c:131
vnet_crypto_main_t * cm
Definition: quic_crypto.c:41
vlib_node_stats_t stats_total
Definition: node.h:270
vnet_sub_interface_t sub
Definition: interface.h:723
static void stats_segment_socket_init(void)
Definition: stat_segment.c:787
clib_error_t * clib_socket_accept(clib_socket_t *server, clib_socket_t *client)
Definition: socket.c:526
stat_directory_type_t
f64 time_last_runtime_stats_clear
Definition: node.h:761
static clib_error_t * clib_socket_close(clib_socket_t *sock)
Definition: socket.h:175
static u8 * format_stat_dir_entry(u8 *s, va_list *args)
Definition: stat_segment.c:456
void vlib_stat_segment_unlock(void)
Definition: stat_segment.c:41
uint64_t offset
DLMALLOC_EXPORT void mspace_disable_expand(mspace msp)
struct _unformat_input_t unformat_input_t
u64 memory_size
Definition: vhost_user.h:141
static clib_error_t * stats_segment_socket_exit(vlib_main_t *vm)
Definition: stat_segment.c:814
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:375
#define PREDICT_FALSE(x)
Definition: clib.h:112
u8 name[64]
Definition: memclnt.api:152
u8 * name
Definition: node.h:264
#define VLIB_EARLY_CONFIG_FUNCTION(x, n,...)
Definition: init.h:226
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
svmdb_client_t * c
clib_error_t * stat_segment_register_gauge(u8 *name, stat_segment_update_fn update_fn, u32 caller_index)
Definition: stat_segment.c:866
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:218
vlib_main_t * vm
Definition: buffer.c:323
#define STAT_SEGMENT_SOCKET_FILE
Definition: stat_client.h:30
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
static void vlib_clear_internal_node_vector_rate(vlib_main_t *vm)
Definition: main.h:364
#define VLIB_MAIN_LOOP_EXIT_FUNCTION(x)
Definition: init.h:178
static void * clib_mem_set_heap(void *heap)
Definition: mem.h:290
static vlib_node_registration_t stat_segment_collector
(constructor) VLIB_REGISTER_NODE (stat_segment_collector)
void vlib_stat_segment_lock(void)
Definition: stat_segment.c:33
VNET_SW_INTERFACE_ADD_DEL_FUNCTION(statseg_sw_interface_add_del)
uint64_t offset_vector
static void * clib_mem_get_heap(void)
Definition: mem.h:284
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:161
void * mheap_alloc_with_flags(void *memory, uword memory_size, uword flags)
Definition: mheap.c:885
char name[128]
clib_error_t * stat_segment_deregister_state_counter(u32 index)
Definition: stat_segment.c:941
u8 value
Definition: qos.api:53
#define ASSERT(truth)
static uword clib_file_add(clib_file_main_t *um, clib_file_t *template)
Definition: file.h:96
f64 vlib_get_stat_segment_update_rate(void)
Definition: stat_segment.c:828
#define clib_error_report(e)
Definition: error.h:113
struct _socket_t clib_socket_t
static void * clib_mem_alloc(uword size)
Definition: mem.h:153
#define CLIB_SOCKET_F_SEQPACKET
Definition: socket.h:63
#define STAT_SEGMENT_VERSION
Definition: stat_segment.h:65
static uword stat_segment_collector_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: stat_segment.c:834
void mheap_usage(void *heap, clib_mem_usage_t *usage)
Definition: mem_dlmalloc.c:387
uword * directory_vector_by_name
Definition: stat_segment.h:89
char * stat_segment_name
Name in stat segment directory.
Definition: counter.h:65
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
vlib_node_main_t node_main
Definition: main.h:158
#define CLIB_SOCKET_F_PASSCRED
Definition: socket.h:64
u64 uword
Definition: types.h:112
static void update_node_counters(stat_segment_main_t *sm)
Definition: stat_segment.c:557
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:983
static int name_sort_cmp(void *a1, void *a2)
Definition: stat_segment.c:447
static clib_error_t * statseg_sw_interface_add_del(vnet_main_t *vnm, u32 sw_if_index, u32 is_add)
static clib_error_t * show_stat_segment_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: stat_segment.c:497
unformat_function_t unformat_memory_size
Definition: format.h:296
clib_socket_t * socket
Definition: stat_segment.h:99
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
counter_t ** counters
Per-thread u64 non-atomic counters.
Definition: counter.h:59
vnet_sw_interface_type_t type
Definition: interface.h:701
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
Definition: file.h:51
void vlib_stats_delete_cm(void *cm_arg)
Definition: stat_segment.c:155
void vlib_stats_register_error_index(void *oldheap, u8 *name, u64 *em_vec, u64 index)
Definition: stat_segment.c:261
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
#define CLIB_SOCKET_F_ALLOW_GROUP_WRITE
Definition: socket.h:62
clib_error_t * vlib_map_stat_segment_init(void)
Definition: stat_segment.c:366
static f64 vlib_internal_node_vector_rate(vlib_main_t *vm)
Definition: main.h:348
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:772
#define VLIB_INITS(...)
Definition: init.h:344
#define STAT_SEGMENT_INDEX_INVALID
Definition: stat_segment.h:67
clib_error_t * stat_segment_register_state_counter(u8 *name, u32 *index)
Definition: stat_segment.c:906
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
void stat_segment_set_state_counter(u32 index, u64 value)
Definition: stat_segment.c:969
clib_spinlock_t * stat_segment_lockp
Definition: stat_segment.h:98
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171
stat_directory_type_t type