stat_segment.c revision b6fde4a8
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>
24#include <vpp-api/client/stat_client.h>
25#include <vppinfra/mheap.h>
26
27stat_segment_main_t stat_segment_main;
28
29/*
30 *  Used only by VPP writers
31 */
32void
33vlib_stat_segment_lock (void)
34{
35  stat_segment_main_t *sm = &stat_segment_main;
36  clib_spinlock_lock (sm->stat_segment_lockp);
37  sm->shared_header->in_progress = 1;
38}
39
40void
41vlib_stat_segment_unlock (void)
42{
43  stat_segment_main_t *sm = &stat_segment_main;
44  sm->shared_header->epoch++;
45  sm->shared_header->in_progress = 0;
46  clib_spinlock_unlock (sm->stat_segment_lockp);
47}
48
49/*
50 * Change heap to the stats shared memory segment
51 */
52void *
53vlib_stats_push_heap (void *old)
54{
55  stat_segment_main_t *sm = &stat_segment_main;
56
57  sm->last = old;
58  ASSERT (sm && sm->shared_header);
59  return clib_mem_set_heap (sm->heap);
60}
61
62static u32
63lookup_or_create_hash_index (u8 * name, u32 next_vector_index)
64{
65  stat_segment_main_t *sm = &stat_segment_main;
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
88void
89vlib_stats_pop_heap (void *cm_arg, void *oldheap, u32 cindex,
90		     stat_directory_type_t type)
91{
92  vlib_simple_counter_main_t *cm = (vlib_simple_counter_main_t *) cm_arg;
93  stat_segment_main_t *sm = &stat_segment_main;
94  stat_segment_shared_header_t *shared_header = sm->shared_header;
95  char *stat_segment_name;
96  stat_segment_directory_entry_t e = { 0 };
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
107  vlib_stat_segment_lock ();
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
156  vlib_stat_segment_unlock ();
157  clib_mem_set_heap (oldheap);
158}
159
160void
161vlib_stats_register_error_index (void *oldheap, u8 * name, u64 * em_vec,
162				 u64 index)
163{
164  stat_segment_main_t *sm = &stat_segment_main;
165  stat_segment_shared_header_t *shared_header = sm->shared_header;
166  stat_segment_directory_entry_t e;
167
168  ASSERT (shared_header);
169
170  vlib_stat_segment_lock ();
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';
184      e.type = STAT_DIR_TYPE_ERROR_INDEX;
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
194  vlib_stat_segment_unlock ();
195}
196
197static void
198stat_validate_counter_vector (stat_segment_directory_entry_t * ep, u32 max)
199{
200  stat_segment_main_t *sm = &stat_segment_main;
201  stat_segment_shared_header_t *shared_header = sm->shared_header;
202  counter_t **counters = 0;
203  vlib_thread_main_t *tm = vlib_get_thread_main ();
204  int i;
205  u64 *offset_vector = 0;
206
207  vec_validate_aligned (counters, tm->n_vlib_mains - 1,
208			CLIB_CACHE_LINE_BYTES);
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
219void
220vlib_stats_pop_heap2 (u64 * error_vector, u32 thread_index, void *oldheap,
221		      int lock)
222{
223  stat_segment_main_t *sm = &stat_segment_main;
224  stat_segment_shared_header_t *shared_header = sm->shared_header;
225
226  ASSERT (shared_header);
227
228  if (lock)
229    vlib_stat_segment_lock ();
230
231  /* Reset the client hash table pointer, since it WILL change! */
232  vec_validate (sm->error_vector, thread_index);
233  sm->error_vector[thread_index] =
234    stat_segment_offset (shared_header, error_vector);
235
236  shared_header->error_offset =
237    stat_segment_offset (shared_header, sm->error_vector);
238  shared_header->directory_offset =
239    stat_segment_offset (shared_header, sm->directory_vector);
240
241  if (lock)
242    vlib_stat_segment_unlock ();
243  clib_mem_set_heap (oldheap);
244}
245
246clib_error_t *
247vlib_map_stat_segment_init (void)
248{
249  stat_segment_main_t *sm = &stat_segment_main;
250  stat_segment_shared_header_t *shared_header;
251  void *oldheap;
252  ssize_t memory_size;
253  int mfd;
254  char *mem_name = "stat_segment_test";
255  void *memaddr;
256
257  memory_size = sm->memory_size;
258  if (memory_size == 0)
259    memory_size = STAT_SEGMENT_DEFAULT_SIZE;
260
261  /* Create shared memory segment */
262  if ((mfd = memfd_create (mem_name, 0)) < 0)
263    return clib_error_return (0, "stat segment memfd_create failure");
264
265  /* Set size */
266  if ((ftruncate (mfd, memory_size)) == -1)
267    return clib_error_return (0, "stat segment ftruncate failure");
268
269  if ((memaddr =
270       mmap (NULL, memory_size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd,
271	     0)) == MAP_FAILED)
272    return clib_error_return (0, "stat segment mmap failure");
273
274  void *heap;
275#if USE_DLMALLOC == 0
276  heap = mheap_alloc_with_flags (((u8 *) memaddr) + getpagesize (),
277				 memory_size - getpagesize (),
278				 MHEAP_FLAG_DISABLE_VM |
279				 MHEAP_FLAG_THREAD_SAFE);
280#else
281  heap =
282    create_mspace_with_base (((u8 *) memaddr) + getpagesize (),
283			     memory_size - getpagesize (), 1 /* locked */ );
284  mspace_disable_expand (heap);
285#endif
286
287  sm->heap = heap;
288  sm->memfd = mfd;
289
290  sm->directory_vector_by_name = hash_create_string (0, sizeof (uword));
291  sm->shared_header = shared_header = memaddr;
292
293  shared_header->version = STAT_SEGMENT_VERSION;
294
295  sm->stat_segment_lockp = clib_mem_alloc (sizeof (clib_spinlock_t));
296  clib_spinlock_init (sm->stat_segment_lockp);
297
298  oldheap = clib_mem_set_heap (sm->heap);
299
300  /* Set up the name to counter-vector hash table */
301  sm->directory_vector = 0;
302
303  shared_header->epoch = 1;
304
305  /* Scalar stats and node counters */
306  vec_validate (sm->directory_vector, STAT_COUNTERS - 1);
307#define _(E,t,n,p)							\
308  strcpy(sm->directory_vector[STAT_COUNTER_##E].name,  #p "/" #n); \
309  sm->directory_vector[STAT_COUNTER_##E].type = STAT_DIR_TYPE_##t;
310  foreach_stat_segment_counter_name
311#undef _
312    /* Save the vector offset in the shared segment, for clients */
313    shared_header->directory_offset =
314    stat_segment_offset (shared_header, sm->directory_vector);
315
316  clib_mem_set_heap (oldheap);
317
318  /* Total shared memory size */
319  clib_mem_usage_t usage;
320  mheap_usage (sm->heap, &usage);
321  sm->directory_vector[STAT_COUNTER_MEM_STATSEG_TOTAL].value =
322    usage.bytes_total;
323
324  return 0;
325}
326
327static int
328name_sort_cmp (void *a1, void *a2)
329{
330  stat_segment_directory_entry_t *n1 = a1;
331  stat_segment_directory_entry_t *n2 = a2;
332
333  return strcmp ((char *) n1->name, (char *) n2->name);
334}
335
336static u8 *
337format_stat_dir_entry (u8 * s, va_list * args)
338{
339  stat_segment_directory_entry_t *ep =
340    va_arg (*args, stat_segment_directory_entry_t *);
341  char *type_name;
342  char *format_string;
343
344  format_string = "%-74s %-10s %10lld";
345
346  switch (ep->type)
347    {
348    case STAT_DIR_TYPE_SCALAR_INDEX:
349      type_name = "ScalarPtr";
350      break;
351
352    case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
353    case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
354      type_name = "CMainPtr";
355      break;
356
357    case STAT_DIR_TYPE_ERROR_INDEX:
358      type_name = "ErrIndex";
359      break;
360
361    default:
362      type_name = "illegal!";
363      break;
364    }
365
366  return format (s, format_string, ep->name, type_name, ep->offset);
367}
368
369static clib_error_t *
370show_stat_segment_command_fn (vlib_main_t * vm,
371			      unformat_input_t * input,
372			      vlib_cli_command_t * cmd)
373{
374  stat_segment_main_t *sm = &stat_segment_main;
375  stat_segment_directory_entry_t *show_data;
376  int i;
377
378  int verbose = 0;
379
380  if (unformat (input, "verbose"))
381    verbose = 1;
382
383  /* Lock even as reader, as this command doesn't handle epoch changes */
384  vlib_stat_segment_lock ();
385  show_data = vec_dup (sm->directory_vector);
386  vlib_stat_segment_unlock ();
387
388  vec_sort_with_function (show_data, name_sort_cmp);
389
390  vlib_cli_output (vm, "%-74s %10s %10s", "Name", "Type", "Value");
391
392  for (i = 0; i < vec_len (show_data); i++)
393    {
394      vlib_cli_output (vm, "%-100U", format_stat_dir_entry,
395		       vec_elt_at_index (show_data, i));
396    }
397
398  if (verbose)
399    {
400      ASSERT (sm->heap);
401      vlib_cli_output (vm, "%U", format_mheap, sm->heap, 0 /* verbose */ );
402    }
403
404  return 0;
405}
406
407/* *INDENT-OFF* */
408VLIB_CLI_COMMAND (show_stat_segment_command, static) =
409{
410  .path = "show statistics segment",
411  .short_help = "show statistics segment [verbose]",
412  .function = show_stat_segment_command_fn,
413};
414/* *INDENT-ON* */
415
416/*
417 * Node performance counters:
418 * total_calls [threads][node-index]
419 * total_vectors
420 * total_calls
421 * total suspends
422 */
423
424static inline void
425update_node_counters (stat_segment_main_t * sm)
426{
427  vlib_main_t **stat_vms = 0;
428  vlib_node_t ***node_dups = 0;
429  int i, j;
430  stat_segment_shared_header_t *shared_header = sm->shared_header;
431  static u32 no_max_nodes = 0;
432
433  vlib_node_get_nodes (0 /* vm, for barrier sync */ ,
434		       (u32) ~ 0 /* all threads */ ,
435		       1 /* include stats */ ,
436		       0 /* barrier sync */ ,
437		       &node_dups, &stat_vms);
438
439  u32 l = vec_len (node_dups[0]);
440
441  /*
442   * Extend performance nodes if necessary
443   */
444  if (l > no_max_nodes)
445    {
446      void *oldheap = clib_mem_set_heap (sm->heap);
447      vlib_stat_segment_lock ();
448
449      stat_validate_counter_vector (&sm->directory_vector
450				    [STAT_COUNTER_NODE_CLOCKS], l - 1);
451      stat_validate_counter_vector (&sm->directory_vector
452				    [STAT_COUNTER_NODE_VECTORS], l - 1);
453      stat_validate_counter_vector (&sm->directory_vector
454				    [STAT_COUNTER_NODE_CALLS], l - 1);
455      stat_validate_counter_vector (&sm->directory_vector
456				    [STAT_COUNTER_NODE_SUSPENDS], l - 1);
457
458      vec_validate (sm->nodes, l - 1);
459      stat_segment_directory_entry_t *ep;
460      ep = &sm->directory_vector[STAT_COUNTER_NODE_NAMES];
461      ep->offset = stat_segment_offset (shared_header, sm->nodes);
462
463      int i;
464      u64 *offset_vector =
465	ep->offset_vector ? stat_segment_pointer (shared_header,
466						  ep->offset_vector) : 0;
467      /* Update names dictionary */
468      vec_validate (offset_vector, l - 1);
469      vlib_node_t **nodes = node_dups[0];
470
471      for (i = 0; i < vec_len (nodes); i++)
472	{
473	  vlib_node_t *n = nodes[i];
474	  u8 *s = 0;
475	  s = format (s, "%v%c", n->name, 0);
476	  if (sm->nodes[n->index])
477	    vec_free (sm->nodes[n->index]);
478	  sm->nodes[n->index] = s;
479	  offset_vector[i] =
480	    sm->nodes[i] ? stat_segment_offset (shared_header,
481						sm->nodes[i]) : 0;
482
483	}
484      ep->offset_vector = stat_segment_offset (shared_header, offset_vector);
485
486      vlib_stat_segment_unlock ();
487      clib_mem_set_heap (oldheap);
488      no_max_nodes = l;
489    }
490
491  for (j = 0; j < vec_len (node_dups); j++)
492    {
493      vlib_node_t **nodes = node_dups[j];
494
495      for (i = 0; i < vec_len (nodes); i++)
496	{
497	  counter_t **counters;
498	  counter_t *c;
499	  vlib_node_t *n = nodes[i];
500
501	  counters =
502	    stat_segment_pointer (shared_header,
503				  sm->directory_vector
504				  [STAT_COUNTER_NODE_CLOCKS].offset);
505	  c = counters[j];
506	  c[n->index] = n->stats_total.clocks - n->stats_last_clear.clocks;
507
508	  counters =
509	    stat_segment_pointer (shared_header,
510				  sm->directory_vector
511				  [STAT_COUNTER_NODE_VECTORS].offset);
512	  c = counters[j];
513	  c[n->index] = n->stats_total.vectors - n->stats_last_clear.vectors;
514
515	  counters =
516	    stat_segment_pointer (shared_header,
517				  sm->directory_vector
518				  [STAT_COUNTER_NODE_CALLS].offset);
519	  c = counters[j];
520	  c[n->index] = n->stats_total.calls - n->stats_last_clear.calls;
521
522	  counters =
523	    stat_segment_pointer (shared_header,
524				  sm->directory_vector
525				  [STAT_COUNTER_NODE_SUSPENDS].offset);
526	  c = counters[j];
527	  c[n->index] =
528	    n->stats_total.suspends - n->stats_last_clear.suspends;
529	}
530      vec_free (node_dups[j]);
531    }
532  vec_free (node_dups);
533  vec_free (stat_vms);
534}
535
536static void
537do_stat_segment_updates (stat_segment_main_t * sm)
538{
539  stat_segment_shared_header_t *shared_header = sm->shared_header;
540  vlib_main_t *vm = vlib_mains[0];
541  f64 vector_rate;
542  u64 input_packets;
543  f64 dt, now;
544  vlib_main_t *this_vlib_main;
545  int i, start;
546  counter_t **counters;
547  static int num_worker_threads_set;
548
549  /*
550   * Set once at the beginning of time.
551   * Can't do this from the init routine, which happens before
552   * start_workers sets up vlib_mains...
553   */
554  if (PREDICT_FALSE (num_worker_threads_set == 0))
555    {
556      sm->directory_vector[STAT_COUNTER_NUM_WORKER_THREADS].value =
557	vec_len (vlib_mains) > 1 ? vec_len (vlib_mains) - 1 : 1;
558
559      stat_validate_counter_vector (&sm->directory_vector
560				    [STAT_COUNTER_VECTOR_RATE_PER_WORKER],
561				    vec_len (vlib_mains));
562      num_worker_threads_set = 1;
563    }
564
565  /*
566   * Compute per-worker vector rates, and the average vector rate
567   * across all workers
568   */
569  vector_rate = 0.0;
570
571  counters =
572    stat_segment_pointer (shared_header,
573			  sm->directory_vector
574			  [STAT_COUNTER_VECTOR_RATE_PER_WORKER].offset);
575
576  start = vec_len (vlib_mains) > 1 ? 1 : 0;
577
578  for (i = start; i < vec_len (vlib_mains); i++)
579    {
580
581      f64 this_vector_rate;
582
583      this_vlib_main = vlib_mains[i];
584
585      this_vector_rate = vlib_last_vector_length_per_node (this_vlib_main);
586      vector_rate += this_vector_rate;
587
588      /* Set the per-worker rate */
589      counters[i - start][0] = this_vector_rate;
590    }
591
592  /* And set the system average rate */
593  vector_rate /= (f64) (i - start);
594
595  sm->directory_vector[STAT_COUNTER_VECTOR_RATE].value = vector_rate;
596
597  /*
598   * Compute the aggregate input rate
599   */
600  now = vlib_time_now (vm);
601  dt = now - sm->directory_vector[STAT_COUNTER_LAST_UPDATE].value;
602  input_packets = vnet_get_aggregate_rx_packets ();
603  sm->directory_vector[STAT_COUNTER_INPUT_RATE].value =
604    (f64) (input_packets - sm->last_input_packets) / dt;
605  sm->directory_vector[STAT_COUNTER_LAST_UPDATE].value = now;
606  sm->last_input_packets = input_packets;
607  sm->directory_vector[STAT_COUNTER_LAST_STATS_CLEAR].value =
608    vm->node_main.time_last_runtime_stats_clear;
609
610  /* Stats segment memory heap counter */
611  clib_mem_usage_t usage;
612  mheap_usage (sm->heap, &usage);
613  sm->directory_vector[STAT_COUNTER_MEM_STATSEG_USED].value =
614    usage.bytes_used;
615
616  if (sm->node_counters_enabled)
617    update_node_counters (sm);
618
619  /* *INDENT-OFF* */
620  stat_segment_gauges_pool_t *g;
621  pool_foreach(g, sm->gauges,
622  ({
623    g->fn(&sm->directory_vector[g->directory_index], g->caller_index);
624  }));
625  /* *INDENT-ON* */
626
627  /* Heartbeat, so clients detect we're still here */
628  sm->directory_vector[STAT_COUNTER_HEARTBEAT].value++;
629}
630
631/*
632 * Accept connection on the socket and exchange the fd for the shared
633 * memory segment.
634 */
635static clib_error_t *
636stats_socket_accept_ready (clib_file_t * uf)
637{
638  stat_segment_main_t *sm = &stat_segment_main;
639  clib_error_t *err;
640  clib_socket_t client = { 0 };
641
642  err = clib_socket_accept (sm->socket, &client);
643  if (err)
644    {
645      clib_error_report (err);
646      return err;
647    }
648
649  /* Send the fd across and close */
650  err = clib_socket_sendmsg (&client, 0, 0, &sm->memfd, 1);
651  if (err)
652    clib_error_report (err);
653  clib_socket_close (&client);
654
655  return 0;
656}
657
658static clib_error_t *
659stats_segment_socket_init (void)
660{
661  stat_segment_main_t *sm = &stat_segment_main;
662  clib_error_t *error;
663  clib_socket_t *s = clib_mem_alloc (sizeof (clib_socket_t));
664
665  memset (s, 0, sizeof (clib_socket_t));
666  s->config = (char *) sm->socket_name;
667  s->flags = CLIB_SOCKET_F_IS_SERVER | CLIB_SOCKET_F_SEQPACKET |
668    CLIB_SOCKET_F_ALLOW_GROUP_WRITE | CLIB_SOCKET_F_PASSCRED;
669
670  if ((error = clib_socket_init (s)))
671    return error;
672
673  clib_file_t template = { 0 };
674  template.read_function = stats_socket_accept_ready;
675  template.file_descriptor = s->fd;
676  template.description = format (0, "stats segment listener %s", s->config);
677  clib_file_add (&file_main, &template);
678
679  sm->socket = s;
680
681  return 0;
682}
683
684static clib_error_t *
685stats_segment_socket_exit (vlib_main_t * vm)
686{
687  /*
688   * cleanup the listener socket on exit.
689   */
690  stat_segment_main_t *sm = &stat_segment_main;
691  unlink ((char *) sm->socket_name);
692  return 0;
693}
694
695VLIB_MAIN_LOOP_EXIT_FUNCTION (stats_segment_socket_exit);
696
697static uword
698stat_segment_collector_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
699				vlib_frame_t * f)
700{
701  stat_segment_main_t *sm = &stat_segment_main;
702
703  /* Wait for Godot... */
704  f64 sleep_duration = 10;
705
706  while (1)
707    {
708      do_stat_segment_updates (sm);
709      vlib_process_suspend (vm, sleep_duration);
710    }
711  return 0;			/* or not */
712}
713
714static clib_error_t *
715statseg_init (vlib_main_t * vm)
716{
717  stat_segment_main_t *sm = &stat_segment_main;
718
719  /* set default socket file name when statseg config stanza is empty. */
720  if (!vec_len (sm->socket_name))
721    sm->socket_name = format (0, "%s/%s%c", vlib_unix_get_runtime_dir (),
722			      STAT_SEGMENT_SOCKET_FILENAME, 0);
723  return stats_segment_socket_init ();
724}
725
726/* *INDENT-OFF* */
727VLIB_INIT_FUNCTION (statseg_init) =
728{
729  .runs_after = VLIB_INITS("unix_input_init"),
730};
731/* *INDENT-ON* */
732
733
734clib_error_t *
735stat_segment_register_gauge (u8 * name, stat_segment_update_fn update_fn,
736			     u32 caller_index)
737{
738  stat_segment_main_t *sm = &stat_segment_main;
739  stat_segment_shared_header_t *shared_header = sm->shared_header;
740  void *oldheap;
741  stat_segment_directory_entry_t e;
742  stat_segment_gauges_pool_t *gauge;
743
744  ASSERT (shared_header);
745
746  u32 next_vector_index = vec_len (sm->directory_vector);
747  u32 vector_index = lookup_or_create_hash_index (name,
748						  next_vector_index);
749
750  if (vector_index < next_vector_index)	/* Already registered */
751    return clib_error_return (0, "%v is alreadty registered", name);
752
753  oldheap = vlib_stats_push_heap (NULL);
754  vlib_stat_segment_lock ();
755
756  memset (&e, 0, sizeof (e));
757  e.type = STAT_DIR_TYPE_SCALAR_INDEX;
758
759  memcpy (e.name, name, vec_len (name));
760  vec_add1 (sm->directory_vector, e);
761
762  shared_header->directory_offset =
763    stat_segment_offset (shared_header, sm->directory_vector);
764
765  vlib_stat_segment_unlock ();
766  clib_mem_set_heap (oldheap);
767
768  /* Back on our own heap */
769  pool_get (sm->gauges, gauge);
770  gauge->fn = update_fn;
771  gauge->caller_index = caller_index;
772  gauge->directory_index = next_vector_index;
773
774  return NULL;
775}
776
777static clib_error_t *
778statseg_config (vlib_main_t * vm, unformat_input_t * input)
779{
780  stat_segment_main_t *sm = &stat_segment_main;
781
782  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
783    {
784      if (unformat (input, "socket-name %s", &sm->socket_name))
785	;
786      /* DEPRECATE: default (does nothing) */
787      else if (unformat (input, "default"))
788	;
789      else if (unformat (input, "size %U",
790			 unformat_memory_size, &sm->memory_size))
791	;
792      else if (unformat (input, "per-node-counters on"))
793	sm->node_counters_enabled = 1;
794      else if (unformat (input, "per-node-counters off"))
795	sm->node_counters_enabled = 0;
796      else
797	return clib_error_return (0, "unknown input `%U'",
798				  format_unformat_error, input);
799    }
800
801  /*
802   * NULL-terminate socket name string
803   * clib_socket_init()->socket_config() use C str*
804   */
805  if (vec_len (sm->socket_name))
806    vec_terminate_c_string (sm->socket_name);
807
808  return 0;
809}
810
811static clib_error_t *
812statseg_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
813{
814  stat_segment_main_t *sm = &stat_segment_main;
815  stat_segment_shared_header_t *shared_header = sm->shared_header;
816
817  void *oldheap = vlib_stats_push_heap (sm->interfaces);
818  vlib_stat_segment_lock ();
819
820  vec_validate (sm->interfaces, sw_if_index);
821  if (is_add)
822    {
823      vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
824      vnet_sw_interface_t *si_sup =
825	vnet_get_sup_sw_interface (vnm, si->sw_if_index);
826      vnet_hw_interface_t *hi_sup;
827
828      ASSERT (si_sup->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
829      hi_sup = vnet_get_hw_interface (vnm, si_sup->hw_if_index);
830
831      u8 *s = 0;
832      s = format (s, "%v", hi_sup->name);
833      if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
834	s = format (s, ".%d", si->sub.id);
835      s = format (s, "%c", 0);
836      sm->interfaces[sw_if_index] = s;
837    }
838  else
839    {
840      vec_free (sm->interfaces[sw_if_index]);
841      sm->interfaces[sw_if_index] = 0;
842    }
843
844  stat_segment_directory_entry_t *ep;
845  ep = &sm->directory_vector[STAT_COUNTER_INTERFACE_NAMES];
846  ep->offset = stat_segment_offset (shared_header, sm->interfaces);
847
848  int i;
849  u64 *offset_vector =
850    ep->offset_vector ? stat_segment_pointer (shared_header,
851					      ep->offset_vector) : 0;
852
853  vec_validate (offset_vector, vec_len (sm->interfaces) - 1);
854
855  if (sm->last != sm->interfaces)
856    {
857      /* the interface vector moved, so need to recalulate the offset array */
858      for (i = 0; i < vec_len (sm->interfaces); i++)
859	{
860	  offset_vector[i] =
861	    sm->interfaces[i] ? stat_segment_offset (shared_header,
862						     sm->interfaces[i]) : 0;
863	}
864    }
865  else
866    {
867      offset_vector[sw_if_index] =
868	sm->interfaces[sw_if_index] ?
869	stat_segment_offset (shared_header, sm->interfaces[sw_if_index]) : 0;
870    }
871  ep->offset_vector = stat_segment_offset (shared_header, offset_vector);
872
873  vlib_stat_segment_unlock ();
874  clib_mem_set_heap (oldheap);
875
876  return 0;
877}
878
879VLIB_EARLY_CONFIG_FUNCTION (statseg_config, "statseg");
880VNET_SW_INTERFACE_ADD_DEL_FUNCTION (statseg_sw_interface_add_del);
881
882/* *INDENT-OFF* */
883VLIB_REGISTER_NODE (stat_segment_collector, static) =
884{
885.function = stat_segment_collector_process,
886.name = "statseg-collector-process",
887.type = VLIB_NODE_TYPE_PROCESS,
888};
889
890/* *INDENT-ON* */
891
892/*
893 * fd.io coding-style-patch-verification: ON
894 *
895 * Local Variables:
896 * eval: (c-set-style "gnu")
897 * End:
898 */
899