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