FD.io VPP  v19.08.1-401-g8e4ed521a
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
63 lookup_or_create_hash_index (u8 * name, u32 next_vector_index)
64 {
66  u32 index;
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  /* we allocate our private copy of 'name' */
76  hash_set (sm->directory_vector_by_name, format (0, "%s%c", name, 0),
77  next_vector_index);
78  index = next_vector_index;
79  }
80  else
81  {
82  index = hp->value[0];
83  }
84 
85  return index;
86 }
87 
88 void
89 vlib_stats_pop_heap (void *cm_arg, void *oldheap, u32 cindex,
91 {
94  stat_segment_shared_header_t *shared_header = sm->shared_header;
95  char *stat_segment_name;
97 
98  /* Not all counters have names / hash-table entries */
99  if (!cm->name && !cm->stat_segment_name)
100  {
101  clib_mem_set_heap (oldheap);
102  return;
103  }
104 
105  ASSERT (shared_header);
106 
108 
109  /* Lookup hash-table is on the main heap */
110  stat_segment_name =
111  cm->stat_segment_name ? cm->stat_segment_name : cm->name;
112  u32 next_vector_index = vec_len (sm->directory_vector);
113  clib_mem_set_heap (oldheap); /* Exit stats segment */
114  u32 vector_index = lookup_or_create_hash_index ((u8 *) stat_segment_name,
115  next_vector_index);
116  /* Back to stats segment */
117  clib_mem_set_heap (sm->heap); /* Re-enter stat segment */
118 
119 
120  /* Update the vector */
121  if (vector_index == next_vector_index)
122  { /* New */
123  strncpy (e.name, stat_segment_name, 128 - 1);
124  e.type = type;
125  vec_add1 (sm->directory_vector, e);
126  }
127 
128  stat_segment_directory_entry_t *ep = &sm->directory_vector[vector_index];
129  ep->offset = stat_segment_offset (shared_header, cm->counters); /* Vector of threads of vectors of counters */
130  u64 *offset_vector =
131  ep->offset_vector ? stat_segment_pointer (shared_header,
132  ep->offset_vector) : 0;
133 
134  /* Update the 2nd dimension offset vector */
135  int i;
136  vec_validate (offset_vector, vec_len (cm->counters) - 1);
137 
138  if (sm->last != offset_vector)
139  {
140  for (i = 0; i < vec_len (cm->counters); i++)
141  offset_vector[i] =
142  stat_segment_offset (shared_header, cm->counters[i]);
143  }
144  else
145  offset_vector[cindex] =
146  stat_segment_offset (shared_header, cm->counters[cindex]);
147 
148  ep->offset_vector = stat_segment_offset (shared_header, offset_vector);
149  sm->directory_vector[vector_index].offset =
150  stat_segment_offset (shared_header, cm->counters);
151 
152  /* Reset the client hash table pointer, since it WILL change! */
153  shared_header->directory_offset =
154  stat_segment_offset (shared_header, sm->directory_vector);
155 
157  clib_mem_set_heap (oldheap);
158 }
159 
160 void
161 vlib_stats_register_error_index (void *oldheap, u8 * name, u64 * em_vec,
162  u64 index)
163 {
165  stat_segment_shared_header_t *shared_header = sm->shared_header;
167 
168  ASSERT (shared_header);
169 
171  u32 next_vector_index = vec_len (sm->directory_vector);
172  clib_mem_set_heap (oldheap); /* Exit stats segment */
173 
174  u32 vector_index = lookup_or_create_hash_index (name,
175  next_vector_index);
176 
177  /* Back to stats segment */
178  clib_mem_set_heap (sm->heap); /* Re-enter stat segment */
179 
180  if (next_vector_index == vector_index)
181  {
182  memcpy (e.name, name, vec_len (name));
183  e.name[vec_len (name)] = '\0';
185  e.offset = index;
186  e.offset_vector = 0;
187  vec_add1 (sm->directory_vector, e);
188 
189  /* Warn clients to refresh any pointers they might be holding */
190  shared_header->directory_offset =
191  stat_segment_offset (shared_header, sm->directory_vector);
192  }
193 
195 }
196 
197 static void
199 {
201  stat_segment_shared_header_t *shared_header = sm->shared_header;
202  counter_t **counters = 0;
204  int i;
205  u64 *offset_vector = 0;
206 
207  vec_validate_aligned (counters, tm->n_vlib_mains - 1,
209  for (i = 0; i < tm->n_vlib_mains; i++)
210  {
211  vec_validate_aligned (counters[i], max, CLIB_CACHE_LINE_BYTES);
212  vec_add1 (offset_vector,
213  stat_segment_offset (shared_header, counters[i]));
214  }
215  ep->offset = stat_segment_offset (shared_header, counters);
216  ep->offset_vector = stat_segment_offset (shared_header, offset_vector);
217 }
218 
219 always_inline void
221  u32 thread_index, u32 index, u64 value)
222 {
224  stat_segment_shared_header_t *shared_header = sm->shared_header;
225 
226  ASSERT (shared_header);
227  counter_t *offset_vector =
229  counter_t *cb =
230  stat_segment_pointer (sm->shared_header, offset_vector[thread_index]);
231  cb[index] = value;
232 }
233 
234 void
235 vlib_stats_pop_heap2 (u64 * error_vector, u32 thread_index, void *oldheap,
236  int lock)
237 {
239  stat_segment_shared_header_t *shared_header = sm->shared_header;
240 
241  ASSERT (shared_header);
242 
243  if (lock)
245 
246  /* Reset the client hash table pointer, since it WILL change! */
247  vec_validate (sm->error_vector, thread_index);
248  sm->error_vector[thread_index] =
249  stat_segment_offset (shared_header, error_vector);
250 
251  shared_header->error_offset =
252  stat_segment_offset (shared_header, sm->error_vector);
253  shared_header->directory_offset =
254  stat_segment_offset (shared_header, sm->directory_vector);
255 
256  if (lock)
258  clib_mem_set_heap (oldheap);
259 }
260 
261 clib_error_t *
263 {
265  stat_segment_shared_header_t *shared_header;
266  void *oldheap;
267  ssize_t memory_size;
268  int mfd;
269  char *mem_name = "stat_segment_test";
270  void *memaddr;
271 
272  memory_size = sm->memory_size;
273  if (memory_size == 0)
274  memory_size = STAT_SEGMENT_DEFAULT_SIZE;
275 
276  /* Create shared memory segment */
277  if ((mfd = memfd_create (mem_name, 0)) < 0)
278  return clib_error_return (0, "stat segment memfd_create failure");
279 
280  /* Set size */
281  if ((ftruncate (mfd, memory_size)) == -1)
282  return clib_error_return (0, "stat segment ftruncate failure");
283 
284  if ((memaddr =
285  mmap (NULL, memory_size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd,
286  0)) == MAP_FAILED)
287  return clib_error_return (0, "stat segment mmap failure");
288 
289  void *heap;
290 #if USE_DLMALLOC == 0
291  heap = mheap_alloc_with_flags (((u8 *) memaddr) + getpagesize (),
292  memory_size - getpagesize (),
295 #else
296  heap =
297  create_mspace_with_base (((u8 *) memaddr) + getpagesize (),
298  memory_size - getpagesize (), 1 /* locked */ );
299  mspace_disable_expand (heap);
300 #endif
301 
302  sm->heap = heap;
303  sm->memfd = mfd;
304 
306  sm->shared_header = shared_header = memaddr;
307 
308  shared_header->version = STAT_SEGMENT_VERSION;
309 
312 
313  oldheap = clib_mem_set_heap (sm->heap);
314 
315  /* Set up the name to counter-vector hash table */
316  sm->directory_vector = 0;
317 
318  shared_header->epoch = 1;
319 
320  /* Scalar stats and node counters */
322 #define _(E,t,n,p) \
323  strcpy(sm->directory_vector[STAT_COUNTER_##E].name, #p "/" #n); \
324  sm->directory_vector[STAT_COUNTER_##E].type = STAT_DIR_TYPE_##t;
326 #undef _
327  /* Save the vector offset in the shared segment, for clients */
328  shared_header->directory_offset =
329  stat_segment_offset (shared_header, sm->directory_vector);
330 
331  clib_mem_set_heap (oldheap);
332 
333  /* Total shared memory size */
335  mheap_usage (sm->heap, &usage);
337  usage.bytes_total;
338 
339  return 0;
340 }
341 
342 static int
343 name_sort_cmp (void *a1, void *a2)
344 {
347 
348  return strcmp ((char *) n1->name, (char *) n2->name);
349 }
350 
351 static u8 *
352 format_stat_dir_entry (u8 * s, va_list * args)
353 {
355  va_arg (*args, stat_segment_directory_entry_t *);
356  char *type_name;
357  char *format_string;
358 
359  format_string = "%-74s %-10s %10lld";
360 
361  switch (ep->type)
362  {
364  type_name = "ScalarPtr";
365  break;
366 
369  type_name = "CMainPtr";
370  break;
371 
373  type_name = "ErrIndex";
374  break;
375 
377  type_name = "NameVector";
378  break;
379 
380  default:
381  type_name = "illegal!";
382  break;
383  }
384 
385  return format (s, format_string, ep->name, type_name, ep->offset);
386 }
387 
388 static clib_error_t *
390  unformat_input_t * input,
391  vlib_cli_command_t * cmd)
392 {
395  int i;
396 
397  int verbose = 0;
398 
399  if (unformat (input, "verbose"))
400  verbose = 1;
401 
402  /* Lock even as reader, as this command doesn't handle epoch changes */
404  show_data = vec_dup (sm->directory_vector);
406 
408 
409  vlib_cli_output (vm, "%-74s %10s %10s", "Name", "Type", "Value");
410 
411  for (i = 0; i < vec_len (show_data); i++)
412  {
413  vlib_cli_output (vm, "%-100U", format_stat_dir_entry,
414  vec_elt_at_index (show_data, i));
415  }
416 
417  if (verbose)
418  {
419  ASSERT (sm->heap);
420  vlib_cli_output (vm, "%U", format_mheap, sm->heap, 0 /* verbose */ );
421  }
422 
423  return 0;
424 }
425 
426 /* *INDENT-OFF* */
427 VLIB_CLI_COMMAND (show_stat_segment_command, static) =
428 {
429  .path = "show statistics segment",
430  .short_help = "show statistics segment [verbose]",
431  .function = show_stat_segment_command_fn,
432 };
433 /* *INDENT-ON* */
434 
435 /*
436  * Node performance counters:
437  * total_calls [threads][node-index]
438  * total_vectors
439  * total_calls
440  * total suspends
441  */
442 
443 static inline void
445 {
446  vlib_main_t **stat_vms = 0;
447  vlib_node_t ***node_dups = 0;
448  int i, j;
449  stat_segment_shared_header_t *shared_header = sm->shared_header;
450  static u32 no_max_nodes = 0;
451 
452  vlib_node_get_nodes (0 /* vm, for barrier sync */ ,
453  (u32) ~ 0 /* all threads */ ,
454  1 /* include stats */ ,
455  0 /* barrier sync */ ,
456  &node_dups, &stat_vms);
457 
458  u32 l = vec_len (node_dups[0]);
459 
460  /*
461  * Extend performance nodes if necessary
462  */
463  if (l > no_max_nodes)
464  {
465  void *oldheap = clib_mem_set_heap (sm->heap);
467 
469  [STAT_COUNTER_NODE_CLOCKS], l - 1);
471  [STAT_COUNTER_NODE_VECTORS], l - 1);
473  [STAT_COUNTER_NODE_CALLS], l - 1);
475  [STAT_COUNTER_NODE_SUSPENDS], l - 1);
476 
477  vec_validate (sm->nodes, l - 1);
480  ep->offset = stat_segment_offset (shared_header, sm->nodes);
481 
482  int i;
483  u64 *offset_vector =
484  ep->offset_vector ? stat_segment_pointer (shared_header,
485  ep->offset_vector) : 0;
486  /* Update names dictionary */
487  vec_validate (offset_vector, l - 1);
488  vlib_node_t **nodes = node_dups[0];
489 
490  for (i = 0; i < vec_len (nodes); i++)
491  {
492  vlib_node_t *n = nodes[i];
493  u8 *s = 0;
494  s = format (s, "%v%c", n->name, 0);
495  if (sm->nodes[n->index])
496  vec_free (sm->nodes[n->index]);
497  sm->nodes[n->index] = s;
498  offset_vector[i] =
499  sm->nodes[i] ? stat_segment_offset (shared_header,
500  sm->nodes[i]) : 0;
501 
502  }
503  ep->offset_vector = stat_segment_offset (shared_header, offset_vector);
504 
506  clib_mem_set_heap (oldheap);
507  no_max_nodes = l;
508  }
509 
510  for (j = 0; j < vec_len (node_dups); j++)
511  {
512  vlib_node_t **nodes = node_dups[j];
513 
514  for (i = 0; i < vec_len (nodes); i++)
515  {
516  counter_t **counters;
517  counter_t *c;
518  vlib_node_t *n = nodes[i];
519 
520  counters =
521  stat_segment_pointer (shared_header,
522  sm->directory_vector
524  c = counters[j];
526 
527  counters =
528  stat_segment_pointer (shared_header,
529  sm->directory_vector
531  c = counters[j];
533 
534  counters =
535  stat_segment_pointer (shared_header,
536  sm->directory_vector
538  c = counters[j];
539  c[n->index] = n->stats_total.calls - n->stats_last_clear.calls;
540 
541  counters =
542  stat_segment_pointer (shared_header,
543  sm->directory_vector
545  c = counters[j];
546  c[n->index] =
548  }
549  vec_free (node_dups[j]);
550  }
551  vec_free (node_dups);
552  vec_free (stat_vms);
553 }
554 
555 static void
557 {
558  vlib_main_t *vm = vlib_mains[0];
559  f64 vector_rate;
560  u64 input_packets;
561  f64 dt, now;
562  vlib_main_t *this_vlib_main;
563  int i;
564  static int num_worker_threads_set;
565 
566  /*
567  * Set once at the beginning of time.
568  * Can't do this from the init routine, which happens before
569  * start_workers sets up vlib_mains...
570  */
571  if (PREDICT_FALSE (num_worker_threads_set == 0))
572  {
573  void *oldheap = clib_mem_set_heap (sm->heap);
575 
578  num_worker_threads_set = 1;
580  clib_mem_set_heap (oldheap);
581  }
582 
583  /*
584  * Compute per-worker vector rates, and the average vector rate
585  * across all workers
586  */
587  vector_rate = 0.0;
588 
589  for (i = 0; i < vec_len (vlib_mains); i++)
590  {
591 
592  f64 this_vector_rate;
593 
594  this_vlib_main = vlib_mains[i];
595 
596  this_vector_rate = vlib_internal_node_vector_rate (this_vlib_main);
597  vlib_clear_internal_node_vector_rate (this_vlib_main);
598 
599  vector_rate += this_vector_rate;
600 
601  /* Set the per-worker rate */
604  this_vector_rate);
605  }
606 
607  /* And set the system average rate */
608  vector_rate /= (f64) (i > 1 ? i - 1 : 1);
609 
611 
612  /*
613  * Compute the aggregate input rate
614  */
615  now = vlib_time_now (vm);
617  input_packets = vnet_get_aggregate_rx_packets ();
619  (f64) (input_packets - sm->last_input_packets) / dt;
621  sm->last_input_packets = input_packets;
624 
625  /* Stats segment memory heap counter */
627  mheap_usage (sm->heap, &usage);
629  usage.bytes_used;
630 
631  if (sm->node_counters_enabled)
633 
634  /* *INDENT-OFF* */
636  pool_foreach(g, sm->gauges,
637  ({
638  g->fn(&sm->directory_vector[g->directory_index], g->caller_index);
639  }));
640  /* *INDENT-ON* */
641 
642  /* Heartbeat, so clients detect we're still here */
644 }
645 
646 /*
647  * Accept connection on the socket and exchange the fd for the shared
648  * memory segment.
649  */
650 static clib_error_t *
652 {
654  clib_error_t *err;
655  clib_socket_t client = { 0 };
656 
657  err = clib_socket_accept (sm->socket, &client);
658  if (err)
659  {
660  clib_error_report (err);
661  return err;
662  }
663 
664  /* Send the fd across and close */
665  err = clib_socket_sendmsg (&client, 0, 0, &sm->memfd, 1);
666  if (err)
667  clib_error_report (err);
668  clib_socket_close (&client);
669 
670  return 0;
671 }
672 
673 static void
675 {
677  clib_error_t *error;
679 
680  memset (s, 0, sizeof (clib_socket_t));
681  s->config = (char *) sm->socket_name;
684 
685  if ((error = clib_socket_init (s)))
686  {
687  clib_error_report (error);
688  return;
689  }
690 
691  clib_file_t template = { 0 };
693  template.file_descriptor = s->fd;
694  template.description = format (0, "stats segment listener %s", s->config);
695  clib_file_add (&file_main, &template);
696 
697  sm->socket = s;
698 }
699 
700 static clib_error_t *
702 {
703  /*
704  * cleanup the listener socket on exit.
705  */
707  unlink ((char *) sm->socket_name);
708  return 0;
709 }
710 
712 
713 /* Overrides weak reference in vlib:node_cli.c */
714 f64
716 {
717  return stat_segment_main.update_interval;
718 }
719 
720 static uword
722  vlib_frame_t * f)
723 {
725 
726  while (1)
727  {
730  }
731  return 0; /* or not */
732 }
733 
734 static clib_error_t *
736 {
738 
739  if (sm->socket_name)
741 
742  return 0;
743 }
744 
745 /* *INDENT-OFF* */
747 {
748  .runs_after = VLIB_INITS("unix_input_init"),
749 };
750 /* *INDENT-ON* */
751 
752 clib_error_t *
754  u32 caller_index)
755 {
757  stat_segment_shared_header_t *shared_header = sm->shared_header;
758  void *oldheap;
761 
762  ASSERT (shared_header);
763 
764  u32 next_vector_index = vec_len (sm->directory_vector);
765  u32 vector_index = lookup_or_create_hash_index (name,
766  next_vector_index);
767 
768  if (vector_index < next_vector_index) /* Already registered */
769  return clib_error_return (0, "%v is alreadty registered", name);
770 
771  oldheap = vlib_stats_push_heap (NULL);
773 
774  memset (&e, 0, sizeof (e));
776 
777  memcpy (e.name, name, vec_len (name));
778  vec_add1 (sm->directory_vector, e);
779 
780  shared_header->directory_offset =
781  stat_segment_offset (shared_header, sm->directory_vector);
782 
784  clib_mem_set_heap (oldheap);
785 
786  /* Back on our own heap */
787  pool_get (sm->gauges, gauge);
788  gauge->fn = update_fn;
789  gauge->caller_index = caller_index;
790  gauge->directory_index = next_vector_index;
791 
792  return NULL;
793 }
794 
795 clib_error_t *
797 {
799  stat_segment_shared_header_t *shared_header = sm->shared_header;
800  void *oldheap;
802 
803  ASSERT (shared_header);
804  ASSERT (vlib_get_thread_index () == 0);
805 
806  u32 next_vector_index = vec_len (sm->directory_vector);
807  u32 vector_index = lookup_or_create_hash_index (name,
808  next_vector_index);
809 
810  if (vector_index < next_vector_index) /* Already registered */
811  return clib_error_return (0, "%v is already registered", name);
812 
813  oldheap = vlib_stats_push_heap (NULL);
815 
816  memset (&e, 0, sizeof (e));
818 
819  memcpy (e.name, name, vec_len (name));
820  vec_add1 (sm->directory_vector, e);
821 
822  shared_header->directory_offset =
823  stat_segment_offset (shared_header, sm->directory_vector);
824 
826  clib_mem_set_heap (oldheap);
827 
828  *index = next_vector_index;
829  return 0;
830 }
831 
832 clib_error_t *
834 {
836  stat_segment_shared_header_t *shared_header = sm->shared_header;
838 
839  ASSERT (shared_header);
840 
841  if (index > vec_len (sm->directory_vector))
842  return clib_error_return (0, "%u index does not exist", index);
843 
844  e = &sm->directory_vector[index];
846  return clib_error_return (0, "%u index cannot be deleted", index);
847 
849  vec_del1 (sm->directory_vector, index);
850  return 0;
851 }
852 
853 void
855 {
857 
858  ASSERT (index < vec_len (sm->directory_vector));
859  sm->directory_vector[index].index = value;
860 }
861 
862 static clib_error_t *
864 {
866  sm->update_interval = 10.0;
867 
869  {
870  if (unformat (input, "socket-name %s", &sm->socket_name))
871  ;
872  else if (unformat (input, "default"))
873  {
875  sm->socket_name = format (sm->socket_name, "%s",
877  }
878  else if (unformat (input, "size %U",
880  ;
881  else if (unformat (input, "per-node-counters on"))
882  sm->node_counters_enabled = 1;
883  else if (unformat (input, "per-node-counters off"))
884  sm->node_counters_enabled = 0;
885  else if (unformat (input, "update-interval %f", &sm->update_interval))
886  ;
887  else
888  return clib_error_return (0, "unknown input `%U'",
889  format_unformat_error, input);
890  }
891 
892  /* set default socket file name when statseg config stanza is empty. */
893  if (!vec_len (sm->socket_name))
894  sm->socket_name = format (sm->socket_name, "%s",
896  /*
897  * NULL-terminate socket name string
898  * clib_socket_init()->socket_config() use C str*
899  */
901 
902  return 0;
903 }
904 
906 
907 static clib_error_t *
909 {
911  stat_segment_shared_header_t *shared_header = sm->shared_header;
912 
913  void *oldheap = vlib_stats_push_heap (sm->interfaces);
915 
916  vec_validate (sm->interfaces, sw_if_index);
917  if (is_add)
918  {
919  vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
920  vnet_sw_interface_t *si_sup =
922  vnet_hw_interface_t *hi_sup;
923 
925  hi_sup = vnet_get_hw_interface (vnm, si_sup->hw_if_index);
926 
927  u8 *s = 0;
928  s = format (s, "%v", hi_sup->name);
930  s = format (s, ".%d", si->sub.id);
931  s = format (s, "%c", 0);
932  sm->interfaces[sw_if_index] = s;
933  }
934  else
935  {
936  vec_free (sm->interfaces[sw_if_index]);
937  sm->interfaces[sw_if_index] = 0;
938  }
939 
942  ep->offset = stat_segment_offset (shared_header, sm->interfaces);
943 
944  int i;
945  u64 *offset_vector =
946  ep->offset_vector ? stat_segment_pointer (shared_header,
947  ep->offset_vector) : 0;
948 
949  vec_validate (offset_vector, vec_len (sm->interfaces) - 1);
950 
951  if (sm->last != sm->interfaces)
952  {
953  /* the interface vector moved, so need to recalulate the offset array */
954  for (i = 0; i < vec_len (sm->interfaces); i++)
955  {
956  offset_vector[i] =
957  sm->interfaces[i] ? stat_segment_offset (shared_header,
958  sm->interfaces[i]) : 0;
959  }
960  }
961  else
962  {
963  offset_vector[sw_if_index] =
964  sm->interfaces[sw_if_index] ?
965  stat_segment_offset (shared_header, sm->interfaces[sw_if_index]) : 0;
966  }
967  ep->offset_vector = stat_segment_offset (shared_header, offset_vector);
968 
970  clib_mem_set_heap (oldheap);
971 
972  return 0;
973 }
974 
976 
977 /* *INDENT-OFF* */
979 {
981 .name = "statseg-collector-process",
982 .type = VLIB_NODE_TYPE_PROCESS,
983 };
984 
985 /* *INDENT-ON* */
986 
987 /*
988  * fd.io coding-style-patch-verification: ON
989  *
990  * Local Variables:
991  * eval: (c-set-style "gnu")
992  * End:
993  */
static clib_error_t * statseg_config(vlib_main_t *vm, unformat_input_t *input)
Definition: stat_segment.c:863
#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:220
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:235
stat_segment_gauges_pool_t * gauges
Definition: stat_segment.h:84
#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:103
uint64_t index
static clib_error_t * stats_socket_accept_ready(clib_file_t *uf)
Definition: stat_segment.c:651
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:88
unsigned long u64
Definition: types.h:89
#define NULL
Definition: clib.h:58
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:68
static void stat_validate_counter_vector(stat_segment_directory_entry_t *ep, u32 max)
Definition: stat_segment.c:198
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:1014
#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 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:73
#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
#define CLIB_SOCKET_F_IS_SERVER
Definition: socket.h:58
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:556
vl_api_interface_index_t sw_if_index
Definition: gre.api:50
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
#define always_inline
Definition: clib.h:98
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:577
#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:89
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:77
unsigned int u32
Definition: types.h:88
static clib_error_t * statseg_init(vlib_main_t *vm)
Definition: stat_segment.c:735
A collection of simple counters.
Definition: counter.h:57
#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
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:674
clib_error_t * clib_socket_accept(clib_socket_t *server, clib_socket_t *client)
Definition: socket.c:524
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:352
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:701
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:375
#define PREDICT_FALSE(x)
Definition: clib.h:111
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:804
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:753
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:213
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)
Definition: stat_segment.c:978
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:833
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:715
#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:721
static u32 lookup_or_create_hash_index(u8 *name, u32 next_vector_index)
Definition: stat_segment.c:63
void mheap_usage(void *heap, clib_mem_usage_t *usage)
Definition: mem_dlmalloc.c:387
uword * directory_vector_by_name
Definition: stat_segment.h:87
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:444
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:980
static int name_sort_cmp(void *a1, void *a2)
Definition: stat_segment.c:343
static clib_error_t * statseg_sw_interface_add_del(vnet_main_t *vnm, u32 sw_if_index, u32 is_add)
Definition: stat_segment.c:908
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:389
unformat_function_t unformat_memory_size
Definition: format.h:296
clib_socket_t * socket
Definition: stat_segment.h:97
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_register_error_index(void *oldheap, u8 *name, u64 *em_vec, u64 index)
Definition: stat_segment.c:161
#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:262
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
clib_error_t * stat_segment_register_state_counter(u8 *name, u32 *index)
Definition: stat_segment.c:796
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:854
clib_spinlock_t * stat_segment_lockp
Definition: stat_segment.h:96
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171
stat_directory_type_t type