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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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 stat_segment_main_t stat_segment_main;
28 * Used only by VPP writers
31 vlib_stat_segment_lock (void)
33 stat_segment_main_t *sm = &stat_segment_main;
34 clib_spinlock_lock (sm->stat_segment_lockp);
35 sm->shared_header->in_progress = 1;
39 vlib_stat_segment_unlock (void)
41 stat_segment_main_t *sm = &stat_segment_main;
42 sm->shared_header->epoch++;
43 sm->shared_header->in_progress = 0;
44 clib_spinlock_unlock (sm->stat_segment_lockp);
48 * Change heap to the stats shared memory segment
51 vlib_stats_push_heap (void *old)
53 stat_segment_main_t *sm = &stat_segment_main;
56 ASSERT (sm && sm->shared_header);
57 return clib_mem_set_heap (sm->heap);
60 /* Name to vector index hash */
62 lookup_or_create_hash_index (void *oldheap, char *name, u32 next_vector_index)
64 stat_segment_main_t *sm = &stat_segment_main;
68 hp = hash_get_pair (sm->directory_vector_by_name, name);
71 hash_set (sm->directory_vector_by_name, name, next_vector_index);
72 index = next_vector_index;
83 vlib_stats_pop_heap (void *cm_arg, void *oldheap, u32 cindex,
84 stat_directory_type_t type)
86 vlib_simple_counter_main_t *cm = (vlib_simple_counter_main_t *) cm_arg;
87 stat_segment_main_t *sm = &stat_segment_main;
88 stat_segment_shared_header_t *shared_header = sm->shared_header;
89 char *stat_segment_name;
90 stat_segment_directory_entry_t e = { 0 };
92 /* Not all counters have names / hash-table entries */
93 if (!cm->name && !cm->stat_segment_name)
95 clib_mem_set_heap (oldheap);
99 ASSERT (shared_header);
101 vlib_stat_segment_lock ();
103 /* Lookup hash-table is on the main heap */
105 cm->stat_segment_name ? cm->stat_segment_name : cm->name;
106 u32 next_vector_index = vec_len (sm->directory_vector);
107 clib_mem_set_heap (oldheap); /* Exit stats segment */
108 u32 vector_index = lookup_or_create_hash_index (oldheap, stat_segment_name,
110 /* Back to stats segment */
111 clib_mem_set_heap (sm->heap); /* Re-enter stat segment */
114 /* Update the vector */
115 if (vector_index == next_vector_index)
117 strncpy (e.name, stat_segment_name, 128 - 1);
119 vec_add1 (sm->directory_vector, e);
122 stat_segment_directory_entry_t *ep = &sm->directory_vector[vector_index];
123 ep->offset = stat_segment_offset (shared_header, cm->counters); /* Vector of threads of vectors of counters */
125 ep->offset_vector ? stat_segment_pointer (shared_header,
126 ep->offset_vector) : 0;
128 /* Update the 2nd dimension offset vector */
130 vec_validate (offset_vector, vec_len (cm->counters) - 1);
132 if (sm->last != offset_vector)
134 for (i = 0; i < vec_len (cm->counters); i++)
136 stat_segment_offset (shared_header, cm->counters[i]);
139 offset_vector[cindex] =
140 stat_segment_offset (shared_header, cm->counters[cindex]);
142 ep->offset_vector = stat_segment_offset (shared_header, offset_vector);
143 sm->directory_vector[vector_index].offset =
144 stat_segment_offset (shared_header, cm->counters);
146 /* Reset the client hash table pointer, since it WILL change! */
147 shared_header->directory_offset =
148 stat_segment_offset (shared_header, sm->directory_vector);
150 vlib_stat_segment_unlock ();
151 clib_mem_set_heap (oldheap);
155 vlib_stats_register_error_index (u8 * name, u64 * em_vec, u64 index)
157 stat_segment_main_t *sm = &stat_segment_main;
158 stat_segment_shared_header_t *shared_header = sm->shared_header;
159 stat_segment_directory_entry_t e;
162 ASSERT (shared_header);
164 vlib_stat_segment_lock ();
166 memcpy (e.name, name, vec_len (name));
167 e.name[vec_len (name)] = '\0';
168 e.type = STAT_DIR_TYPE_ERROR_INDEX;
171 vec_add1 (sm->directory_vector, e);
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);
177 vlib_stat_segment_unlock ();
181 stat_validate_counter_vector (stat_segment_directory_entry_t * ep, u32 max)
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 ();
188 u64 *offset_vector = 0;
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++)
194 vec_validate_aligned (counters[i], max, CLIB_CACHE_LINE_BYTES);
195 vec_add1 (offset_vector,
196 stat_segment_offset (shared_header, counters[i]));
198 ep->offset = stat_segment_offset (shared_header, counters);
199 ep->offset_vector = stat_segment_offset (shared_header, offset_vector);
203 vlib_stats_pop_heap2 (u64 * error_vector, u32 thread_index, void *oldheap)
205 stat_segment_main_t *sm = &stat_segment_main;
206 stat_segment_shared_header_t *shared_header = sm->shared_header;
208 ASSERT (shared_header);
210 vlib_stat_segment_lock ();
212 /* Reset the client hash table pointer, since it WILL change! */
213 shared_header->error_offset =
214 stat_segment_offset (shared_header, error_vector);
215 shared_header->directory_offset =
216 stat_segment_offset (shared_header, sm->directory_vector);
218 vlib_stat_segment_unlock ();
219 clib_mem_set_heap (oldheap);
223 vlib_map_stat_segment_init (void)
225 stat_segment_main_t *sm = &stat_segment_main;
226 stat_segment_shared_header_t *shared_header;
227 stat_segment_directory_entry_t *ep;
231 char *mem_name = "stat_segment_test";
234 memory_size = sm->memory_size;
235 if (memory_size == 0)
236 memory_size = STAT_SEGMENT_DEFAULT_SIZE;
238 /* Create shared memory segment */
239 if ((mfd = memfd_create (mem_name, 0)) < 0)
240 return clib_error_return (0, "stat segment memfd_create failure");
243 if ((ftruncate (mfd, memory_size)) == -1)
244 return clib_error_return (0, "stat segment ftruncate failure");
247 mmap (NULL, memory_size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd,
249 return clib_error_return (0, "stat segment mmap failure");
252 #if USE_DLMALLOC == 0
253 heap = mheap_alloc_with_flags (((u8 *) memaddr) + getpagesize (),
254 memory_size - getpagesize (),
255 MHEAP_FLAG_DISABLE_VM |
256 MHEAP_FLAG_THREAD_SAFE);
259 create_mspace_with_base (((u8 *) memaddr) + getpagesize (),
260 memory_size - getpagesize (), 1 /* locked */ );
261 mspace_disable_expand (heap);
267 sm->directory_vector_by_name = hash_create_string (0, sizeof (uword));
268 sm->shared_header = shared_header = memaddr;
269 sm->stat_segment_lockp = clib_mem_alloc (sizeof (clib_spinlock_t));
270 clib_spinlock_init (sm->stat_segment_lockp);
272 oldheap = clib_mem_set_heap (sm->heap);
274 /* Set up the name to counter-vector hash table */
275 sm->directory_vector = 0;
277 shared_header->epoch = 1;
279 /* Scalar stats and node counters */
280 vec_validate (sm->directory_vector, STAT_COUNTERS);
282 strcpy(sm->directory_vector[STAT_COUNTER_##E].name, #p "/" #n); \
283 sm->directory_vector[STAT_COUNTER_##E].type = STAT_DIR_TYPE_##t;
284 foreach_stat_segment_counter_name
286 /* Save the vector offset in the shared segment, for clients */
287 shared_header->directory_offset =
288 stat_segment_offset (shared_header, sm->directory_vector);
290 clib_mem_set_heap (oldheap);
296 name_sort_cmp (void *a1, void *a2)
298 stat_segment_directory_entry_t *n1 = a1;
299 stat_segment_directory_entry_t *n2 = a2;
301 return strcmp ((char *) n1->name, (char *) n2->name);
305 format_stat_dir_entry (u8 * s, va_list * args)
307 stat_segment_directory_entry_t *ep =
308 va_arg (*args, stat_segment_directory_entry_t *);
312 format_string = "%-74s %-10s %10lld";
316 case STAT_DIR_TYPE_SCALAR_INDEX:
317 type_name = "ScalarPtr";
320 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
321 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
322 type_name = "CMainPtr";
325 case STAT_DIR_TYPE_ERROR_INDEX:
326 type_name = "ErrIndex";
330 type_name = "illegal!";
334 return format (s, format_string, ep->name, type_name, ep->offset);
337 static clib_error_t *
338 show_stat_segment_command_fn (vlib_main_t * vm,
339 unformat_input_t * input,
340 vlib_cli_command_t * cmd)
342 stat_segment_main_t *sm = &stat_segment_main;
345 stat_segment_directory_entry_t *show_data, *this;
351 if (unformat (input, "verbose"))
354 /* Lock even as reader, as this command doesn't handle epoch changes */
355 vlib_stat_segment_lock ();
356 show_data = vec_dup (sm->directory_vector);
357 vlib_stat_segment_unlock ();
359 vec_sort_with_function (show_data, name_sort_cmp);
361 vlib_cli_output (vm, "%-74s %10s %10s", "Name", "Type", "Value");
363 for (i = 0; i < vec_len (show_data); i++)
365 vlib_cli_output (vm, "%-100U", format_stat_dir_entry,
366 vec_elt_at_index (show_data, i));
372 vlib_cli_output (vm, "%U", format_mheap, sm->heap, 0 /* verbose */ );
379 VLIB_CLI_COMMAND (show_stat_segment_command, static) =
381 .path = "show statistics segment",
382 .short_help = "show statistics segment [verbose]",
383 .function = show_stat_segment_command_fn,
388 * Node performance counters:
389 * total_calls [threads][node-index]
396 update_node_counters (stat_segment_main_t * sm)
398 vlib_main_t *vm = vlib_mains[0];
399 vlib_main_t **stat_vms = 0;
400 vlib_node_t ***node_dups = 0;
402 stat_segment_shared_header_t *shared_header = sm->shared_header;
403 static u32 no_max_nodes = 0;
405 vlib_node_get_nodes (0 /* vm, for barrier sync */ ,
406 (u32) ~ 0 /* all threads */ ,
407 1 /* include stats */ ,
408 0 /* barrier sync */ ,
409 &node_dups, &stat_vms);
411 u32 l = vec_len (node_dups[0]);
414 * Extend performance nodes if necessary
416 if (l > no_max_nodes)
418 void *oldheap = clib_mem_set_heap (sm->heap);
419 vlib_stat_segment_lock ();
421 stat_validate_counter_vector (&sm->directory_vector
422 [STAT_COUNTER_NODE_CLOCKS], l);
423 stat_validate_counter_vector (&sm->directory_vector
424 [STAT_COUNTER_NODE_VECTORS], l);
425 stat_validate_counter_vector (&sm->directory_vector
426 [STAT_COUNTER_NODE_CALLS], l);
427 stat_validate_counter_vector (&sm->directory_vector
428 [STAT_COUNTER_NODE_SUSPENDS], l);
430 vec_validate (sm->nodes, l - 1);
431 stat_segment_directory_entry_t *ep;
432 ep = &sm->directory_vector[STAT_COUNTER_NODE_NAMES];
433 ep->offset = stat_segment_offset (shared_header, sm->nodes);
437 ep->offset_vector ? stat_segment_pointer (shared_header,
438 ep->offset_vector) : 0;
439 /* Update names dictionary */
440 vec_validate (offset_vector, l - 1);
441 vlib_node_t **nodes = node_dups[0];
443 for (i = 0; i < vec_len (nodes); i++)
445 vlib_node_t *n = nodes[i];
447 s = format (s, "%v%c", n->name, 0);
448 if (sm->nodes[n->index])
449 vec_free (sm->nodes[n->index]);
450 sm->nodes[n->index] = s;
452 sm->nodes[i] ? stat_segment_offset (shared_header,
456 ep->offset_vector = stat_segment_offset (shared_header, offset_vector);
458 vlib_stat_segment_unlock ();
459 clib_mem_set_heap (oldheap);
463 for (j = 0; j < vec_len (node_dups); j++)
465 vlib_node_t **nodes = node_dups[j];
466 u32 l = vec_len (nodes);
468 for (i = 0; i < vec_len (nodes); i++)
470 counter_t **counters;
472 vlib_node_t *n = nodes[i];
475 stat_segment_pointer (shared_header,
477 [STAT_COUNTER_NODE_CLOCKS].offset);
479 c[n->index] = n->stats_total.clocks - n->stats_last_clear.clocks;
482 stat_segment_pointer (shared_header,
484 [STAT_COUNTER_NODE_VECTORS].offset);
486 c[n->index] = n->stats_total.vectors - n->stats_last_clear.vectors;
489 stat_segment_pointer (shared_header,
491 [STAT_COUNTER_NODE_CALLS].offset);
493 c[n->index] = n->stats_total.calls - n->stats_last_clear.calls;
496 stat_segment_pointer (shared_header,
498 [STAT_COUNTER_NODE_SUSPENDS].offset);
501 n->stats_total.suspends - n->stats_last_clear.suspends;
507 do_stat_segment_updates (stat_segment_main_t * sm)
509 vlib_main_t *vm = vlib_mains[0];
511 u64 input_packets, last_input_packets;
513 vlib_main_t *this_vlib_main;
517 * Compute the average vector rate across all workers
521 start = vec_len (vlib_mains) > 1 ? 1 : 0;
523 for (i = start; i < vec_len (vlib_mains); i++)
525 this_vlib_main = vlib_mains[i];
526 vector_rate += vlib_last_vector_length_per_node (this_vlib_main);
528 vector_rate /= (f64) (i - start);
530 sm->directory_vector[STAT_COUNTER_VECTOR_RATE].value =
531 vector_rate / ((f64) (vec_len (vlib_mains) - start));
534 * Compute the aggregate input rate
536 now = vlib_time_now (vm);
537 dt = now - sm->directory_vector[STAT_COUNTER_LAST_UPDATE].value;
538 input_packets = vnet_get_aggregate_rx_packets ();
539 sm->directory_vector[STAT_COUNTER_INPUT_RATE].value =
540 (f64) (input_packets - sm->last_input_packets) / dt;
541 sm->directory_vector[STAT_COUNTER_LAST_UPDATE].value = now;
542 sm->last_input_packets = input_packets;
543 sm->directory_vector[STAT_COUNTER_LAST_STATS_CLEAR].value =
544 vm->node_main.time_last_runtime_stats_clear;
546 if (sm->node_counters_enabled)
547 update_node_counters (sm);
550 stat_segment_gauges_pool_t *g;
551 pool_foreach(g, sm->gauges,
553 g->fn(&sm->directory_vector[g->directory_index], g->caller_index);
557 /* Heartbeat, so clients detect we're still here */
558 sm->directory_vector[STAT_COUNTER_HEARTBEAT].value++;
562 * Accept connection on the socket and exchange the fd for the shared
565 static clib_error_t *
566 stats_socket_accept_ready (clib_file_t * uf)
568 stat_segment_main_t *sm = &stat_segment_main;
570 clib_socket_t client = { 0 };
572 err = clib_socket_accept (sm->socket, &client);
575 clib_error_report (err);
579 /* Send the fd across and close */
580 err = clib_socket_sendmsg (&client, 0, 0, &sm->memfd, 1);
582 clib_error_report (err);
583 clib_socket_close (&client);
589 stats_segment_socket_init (void)
591 stat_segment_main_t *sm = &stat_segment_main;
593 clib_socket_t *s = clib_mem_alloc (sizeof (clib_socket_t));
595 memset (s, 0, sizeof (clib_socket_t));
596 s->config = (char *) sm->socket_name;
597 s->flags = CLIB_SOCKET_F_IS_SERVER | CLIB_SOCKET_F_SEQPACKET |
598 CLIB_SOCKET_F_ALLOW_GROUP_WRITE | CLIB_SOCKET_F_PASSCRED;
600 if ((error = clib_socket_init (s)))
602 clib_error_report (error);
606 clib_file_t template = { 0 };
607 template.read_function = stats_socket_accept_ready;
608 template.file_descriptor = s->fd;
609 template.description = format (0, "stats segment listener %s", s->config);
610 clib_file_add (&file_main, &template);
615 static clib_error_t *
616 stats_segment_socket_exit (vlib_main_t * vm)
619 * cleanup the listener socket on exit.
621 stat_segment_main_t *sm = &stat_segment_main;
622 unlink ((char *) sm->socket_name);
626 VLIB_MAIN_LOOP_EXIT_FUNCTION (stats_segment_socket_exit);
629 stat_segment_collector_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
632 stat_segment_main_t *sm = &stat_segment_main;
634 /* Wait for Godot... */
635 f64 sleep_duration = 10;
639 do_stat_segment_updates (sm);
640 vlib_process_suspend (vm, sleep_duration);
642 return 0; /* or not */
645 static clib_error_t *
646 statseg_init (vlib_main_t * vm)
648 stat_segment_main_t *sm = &stat_segment_main;
651 /* dependent on unix_input_init */
652 if ((error = vlib_call_init_function (vm, unix_input_init)))
656 stats_segment_socket_init ();
662 stat_segment_register_gauge (u8 * name, stat_segment_update_fn update_fn,
665 stat_segment_main_t *sm = &stat_segment_main;
666 stat_segment_shared_header_t *shared_header = sm->shared_header;
668 stat_segment_directory_entry_t e;
670 stat_segment_gauges_pool_t *gauge;
672 ASSERT (shared_header);
674 oldheap = vlib_stats_push_heap (NULL);
675 vlib_stat_segment_lock ();
677 memset (&e, 0, sizeof (e));
678 e.type = STAT_DIR_TYPE_SCALAR_INDEX;
680 memcpy (e.name, name, vec_len (name));
681 index = vec_len (sm->directory_vector);
682 vec_add1 (sm->directory_vector, e);
684 shared_header->directory_offset =
685 stat_segment_offset (shared_header, sm->directory_vector);
687 vlib_stat_segment_unlock ();
688 clib_mem_set_heap (oldheap);
690 /* Back on our own heap */
691 pool_get (sm->gauges, gauge);
692 gauge->fn = update_fn;
693 gauge->caller_index = caller_index;
694 gauge->directory_index = index;
699 static clib_error_t *
700 statseg_config (vlib_main_t * vm, unformat_input_t * input)
702 stat_segment_main_t *sm = &stat_segment_main;
704 /* set default socket file name when statseg config stanza is empty. */
705 sm->socket_name = format (0, "%s", STAT_SEGMENT_SOCKET_FILE);
707 * NULL-terminate socket name string
708 * clib_socket_init()->socket_config() use C str*
710 vec_add1 (sm->socket_name, 0);
712 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
714 if (unformat (input, "socket-name %s", &sm->socket_name))
716 else if (unformat (input, "default"))
717 sm->socket_name = format (0, "%s", STAT_SEGMENT_SOCKET_FILE);
720 (input, "size %U", unformat_memory_size, &sm->memory_size))
722 else if (unformat (input, "per-node-counters on"))
723 sm->node_counters_enabled = 1;
724 else if (unformat (input, "per-node-counters off"))
725 sm->node_counters_enabled = 0;
727 return clib_error_return (0, "unknown input `%U'",
728 format_unformat_error, input);
733 static clib_error_t *
734 statseg_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
736 stat_segment_main_t *sm = &stat_segment_main;
737 stat_segment_shared_header_t *shared_header = sm->shared_header;
739 void *oldheap = vlib_stats_push_heap (sm->interfaces);
740 vlib_stat_segment_lock ();
742 vec_validate (sm->interfaces, sw_if_index);
745 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
746 vnet_sw_interface_t *si_sup =
747 vnet_get_sup_sw_interface (vnm, si->sw_if_index);
748 vnet_hw_interface_t *hi_sup;
750 ASSERT (si_sup->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
751 hi_sup = vnet_get_hw_interface (vnm, si_sup->hw_if_index);
754 s = format (s, "%v", hi_sup->name);
755 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
756 s = format (s, ".%d", si->sub.id);
757 s = format (s, "%c", 0);
758 sm->interfaces[sw_if_index] = s;
762 vec_free (sm->interfaces[sw_if_index]);
763 sm->interfaces[sw_if_index] = 0;
766 stat_segment_directory_entry_t *ep;
767 ep = &sm->directory_vector[STAT_COUNTER_INTERFACE_NAMES];
768 ep->offset = stat_segment_offset (shared_header, sm->interfaces);
772 ep->offset_vector ? stat_segment_pointer (shared_header,
773 ep->offset_vector) : 0;
775 vec_validate (offset_vector, vec_len (sm->interfaces) - 1);
777 if (sm->last != sm->interfaces)
779 /* the interface vector moved, so need to recalulate the offset array */
780 for (i = 0; i < vec_len (sm->interfaces); i++)
783 sm->interfaces[i] ? stat_segment_offset (shared_header,
784 sm->interfaces[i]) : 0;
789 offset_vector[sw_if_index] =
790 sm->interfaces[sw_if_index] ?
791 stat_segment_offset (shared_header, sm->interfaces[sw_if_index]) : 0;
793 ep->offset_vector = stat_segment_offset (shared_header, offset_vector);
795 vlib_stat_segment_unlock ();
796 clib_mem_set_heap (oldheap);
801 VLIB_INIT_FUNCTION (statseg_init);
802 VLIB_EARLY_CONFIG_FUNCTION (statseg_config, "statseg");
803 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (statseg_sw_interface_add_del);
806 VLIB_REGISTER_NODE (stat_segment_collector, static) =
808 .function = stat_segment_collector_process,
809 .name = "statseg-collector-process",
810 .type = VLIB_NODE_TYPE_PROCESS,
816 * fd.io coding-style-patch-verification: ON
819 * eval: (c-set-style "gnu")