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>
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)
53 stat_segment_main_t *sm = &stat_segment_main;
55 ASSERT (sm && sm->shared_header);
56 return clib_mem_set_heap (sm->heap);
59 /* Name to vector index hash */
61 lookup_or_create_hash_index (void *oldheap, char *name, u32 next_vector_index)
63 stat_segment_main_t *sm = &stat_segment_main;
67 hp = hash_get_pair (sm->directory_vector_by_name, name);
70 hash_set (sm->directory_vector_by_name, name, next_vector_index);
71 index = next_vector_index;
82 vlib_stats_pop_heap (void *cm_arg, void *oldheap, stat_directory_type_t type)
84 vlib_simple_counter_main_t *cm = (vlib_simple_counter_main_t *) cm_arg;
85 stat_segment_main_t *sm = &stat_segment_main;
86 stat_segment_shared_header_t *shared_header = sm->shared_header;
87 char *stat_segment_name;
88 stat_segment_directory_entry_t e = { 0 };
90 /* Not all counters have names / hash-table entries */
91 if (!cm->name && !cm->stat_segment_name)
93 clib_mem_set_heap (oldheap);
97 ASSERT (shared_header);
99 vlib_stat_segment_lock ();
101 /* Lookup hash-table is on the main heap */
103 cm->stat_segment_name ? cm->stat_segment_name : cm->name;
104 u32 next_vector_index = vec_len (sm->directory_vector);
105 clib_mem_set_heap (oldheap); /* Exit stats segment */
106 u32 vector_index = lookup_or_create_hash_index (oldheap, stat_segment_name,
108 /* Back to stats segment */
109 clib_mem_set_heap (sm->heap); /* Re-enter stat segment */
112 /* Update the vector */
113 if (vector_index == next_vector_index)
115 strncpy (e.name, stat_segment_name, 128 - 1);
117 vec_add1 (sm->directory_vector, e);
121 stat_segment_directory_entry_t *ep = &sm->directory_vector[vector_index];
122 ep->offset = stat_segment_offset (shared_header, cm->counters); /* Vector of threads of vectors of counters */
124 ep->offset_vector ? stat_segment_pointer (shared_header,
125 ep->offset_vector) : 0;
127 /* Update the 2nd dimension offset vector */
129 vec_validate (offset_vector, vec_len (cm->counters) - 1);
130 for (i = 0; i < vec_len (cm->counters); i++)
131 offset_vector[i] = stat_segment_offset (shared_header, cm->counters[i]);
132 ep->offset_vector = stat_segment_offset (shared_header, offset_vector);
133 sm->directory_vector[vector_index].offset =
134 stat_segment_offset (shared_header, cm->counters);
136 /* Reset the client hash table pointer, since it WILL change! */
137 shared_header->directory_offset =
138 stat_segment_offset (shared_header, sm->directory_vector);
140 vlib_stat_segment_unlock ();
141 clib_mem_set_heap (oldheap);
145 vlib_stats_register_error_index (u8 * name, u64 * em_vec, u64 index)
147 stat_segment_main_t *sm = &stat_segment_main;
148 stat_segment_shared_header_t *shared_header = sm->shared_header;
149 stat_segment_directory_entry_t e;
152 ASSERT (shared_header);
154 vlib_stat_segment_lock ();
156 memcpy (e.name, name, vec_len (name));
157 e.name[vec_len (name)] = '\0';
158 e.type = STAT_DIR_TYPE_ERROR_INDEX;
161 vec_add1 (sm->directory_vector, e);
163 /* Warn clients to refresh any pointers they might be holding */
164 shared_header->directory_offset =
165 stat_segment_offset (shared_header, sm->directory_vector);
167 vlib_stat_segment_unlock ();
171 stat_validate_counter_vector (stat_segment_directory_entry_t * ep, u32 max)
173 stat_segment_main_t *sm = &stat_segment_main;
174 stat_segment_shared_header_t *shared_header = sm->shared_header;
175 counter_t **counters = 0;
176 vlib_thread_main_t *tm = vlib_get_thread_main ();
178 u64 *offset_vector = 0;
180 vec_validate_aligned (counters, tm->n_vlib_mains - 1,
181 CLIB_CACHE_LINE_BYTES);
182 for (i = 0; i < tm->n_vlib_mains; i++)
184 vec_validate_aligned (counters[i], max, CLIB_CACHE_LINE_BYTES);
185 vec_add1 (offset_vector,
186 stat_segment_offset (shared_header, counters[i]));
188 ep->offset = stat_segment_offset (shared_header, counters);
189 ep->offset_vector = stat_segment_offset (shared_header, offset_vector);
193 vlib_stats_pop_heap2 (u64 * error_vector, u32 thread_index, void *oldheap)
195 stat_segment_main_t *sm = &stat_segment_main;
196 stat_segment_shared_header_t *shared_header = sm->shared_header;
198 ASSERT (shared_header);
200 vlib_stat_segment_lock ();
202 /* Reset the client hash table pointer, since it WILL change! */
203 shared_header->error_offset =
204 stat_segment_offset (shared_header, error_vector);
205 shared_header->directory_offset =
206 stat_segment_offset (shared_header, sm->directory_vector);
208 vlib_stat_segment_unlock ();
209 clib_mem_set_heap (oldheap);
213 vlib_map_stat_segment_init (void)
215 stat_segment_main_t *sm = &stat_segment_main;
216 stat_segment_shared_header_t *shared_header;
217 stat_segment_directory_entry_t *ep;
228 char *mem_name = "stat_segment_test";
231 memory_size = sm->memory_size;
232 if (memory_size == 0)
233 memory_size = STAT_SEGMENT_DEFAULT_SIZE;
235 /* Create shared memory segment */
236 if ((mfd = memfd_create (mem_name, 0)) < 0)
237 return clib_error_return (0, "stat segment memfd_create failure");
240 if ((ftruncate (mfd, memory_size)) == -1)
241 return clib_error_return (0, "stat segment ftruncate failure");
244 mmap (NULL, memory_size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd,
246 return clib_error_return (0, "stat segment mmap failure");
249 #if USE_DLMALLOC == 0
250 heap = mheap_alloc_with_flags (((u8 *) memaddr) + getpagesize (),
251 memory_size - getpagesize (),
252 MHEAP_FLAG_DISABLE_VM |
253 MHEAP_FLAG_THREAD_SAFE);
256 create_mspace_with_base (((u8 *) memaddr) + getpagesize (),
257 memory_size - getpagesize (), 1 /* locked */ );
258 mspace_disable_expand (heap);
264 sm->directory_vector_by_name = hash_create_string (0, sizeof (uword));
265 sm->shared_header = shared_header = memaddr;
266 sm->stat_segment_lockp = clib_mem_alloc (sizeof (clib_spinlock_t));
267 clib_spinlock_init (sm->stat_segment_lockp);
269 oldheap = clib_mem_set_heap (sm->heap);
271 /* Set up the name to counter-vector hash table */
272 sm->directory_vector = 0;
274 shared_header->epoch = 1;
276 /* Scalar stats and node counters */
277 vec_validate (sm->directory_vector, STAT_COUNTERS - 1);
279 strcpy(sm->directory_vector[STAT_COUNTER_##E].name, "/sys" #p "/" #n); \
280 sm->directory_vector[STAT_COUNTER_##E].type = STAT_DIR_TYPE_##t;
281 foreach_stat_segment_counter_name
283 /* Save the vector offset in the shared segment, for clients */
284 shared_header->directory_offset =
285 stat_segment_offset (shared_header, sm->directory_vector);
287 clib_mem_set_heap (oldheap);
293 name_sort_cmp (void *a1, void *a2)
295 stat_segment_directory_entry_t *n1 = a1;
296 stat_segment_directory_entry_t *n2 = a2;
298 return strcmp ((char *) n1->name, (char *) n2->name);
302 format_stat_dir_entry (u8 * s, va_list * args)
304 stat_segment_directory_entry_t *ep =
305 va_arg (*args, stat_segment_directory_entry_t *);
309 format_string = "%-74s %-10s %10lld";
313 case STAT_DIR_TYPE_SCALAR_INDEX:
314 type_name = "ScalarPtr";
317 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
318 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
319 type_name = "CMainPtr";
322 case STAT_DIR_TYPE_ERROR_INDEX:
323 type_name = "ErrIndex";
327 type_name = "illegal!";
331 return format (s, format_string, ep->name, type_name, ep->offset);
334 static clib_error_t *
335 show_stat_segment_command_fn (vlib_main_t * vm,
336 unformat_input_t * input,
337 vlib_cli_command_t * cmd)
339 stat_segment_main_t *sm = &stat_segment_main;
342 stat_segment_directory_entry_t *show_data, *this;
348 if (unformat (input, "verbose"))
351 /* Lock even as reader, as this command doesn't handle epoch changes */
352 vlib_stat_segment_lock ();
353 show_data = vec_dup (sm->directory_vector);
354 vlib_stat_segment_unlock ();
356 vec_sort_with_function (show_data, name_sort_cmp);
358 vlib_cli_output (vm, "%-74s %10s %10s", "Name", "Type", "Value");
360 for (i = 0; i < vec_len (show_data); i++)
362 vlib_cli_output (vm, "%-100U", format_stat_dir_entry,
363 vec_elt_at_index (show_data, i));
369 vlib_cli_output (vm, "%U", format_mheap, sm->heap, 0 /* verbose */ );
376 VLIB_CLI_COMMAND (show_stat_segment_command, static) =
378 .path = "show statistics segment",
379 .short_help = "show statistics segment [verbose]",
380 .function = show_stat_segment_command_fn,
385 * Node performance counters:
386 * total_calls [threads][node-index]
393 update_node_counters (stat_segment_main_t * sm)
395 vlib_main_t *vm = vlib_mains[0];
396 vlib_main_t **stat_vms = 0;
397 vlib_node_t ***node_dups = 0;
399 stat_segment_shared_header_t *shared_header = sm->shared_header;
400 static u32 no_max_nodes = 0;
402 vlib_node_get_nodes (0 /* vm, for barrier sync */ ,
403 (u32) ~ 0 /* all threads */ ,
404 1 /* include stats */ ,
405 0 /* barrier sync */ ,
406 &node_dups, &stat_vms);
408 u32 l = vec_len (node_dups[0]);
411 * Extend performance nodes if necessary
413 if (l > no_max_nodes)
415 void *oldheap = clib_mem_set_heap (sm->heap);
416 vlib_stat_segment_lock ();
418 stat_validate_counter_vector (&sm->directory_vector
419 [STAT_COUNTER_NODE_CLOCKS], l);
420 stat_validate_counter_vector (&sm->directory_vector
421 [STAT_COUNTER_NODE_VECTORS], l);
422 stat_validate_counter_vector (&sm->directory_vector
423 [STAT_COUNTER_NODE_CALLS], l);
424 stat_validate_counter_vector (&sm->directory_vector
425 [STAT_COUNTER_NODE_SUSPENDS], l);
427 vlib_stat_segment_unlock ();
428 clib_mem_set_heap (oldheap);
432 for (j = 0; j < vec_len (node_dups); j++)
434 vlib_node_t **nodes = node_dups[j];
435 u32 l = vec_len (nodes);
437 for (i = 0; i < vec_len (nodes); i++)
439 counter_t **counters;
441 vlib_node_t *n = nodes[i];
444 stat_segment_pointer (shared_header,
446 [STAT_COUNTER_NODE_CLOCKS].offset);
448 c[n->index] = n->stats_total.clocks - n->stats_last_clear.clocks;
451 stat_segment_pointer (shared_header,
453 [STAT_COUNTER_NODE_VECTORS].offset);
455 c[n->index] = n->stats_total.vectors - n->stats_last_clear.vectors;
458 stat_segment_pointer (shared_header,
460 [STAT_COUNTER_NODE_CALLS].offset);
462 c[n->index] = n->stats_total.calls - n->stats_last_clear.calls;
465 stat_segment_pointer (shared_header,
467 [STAT_COUNTER_NODE_SUSPENDS].offset);
470 n->stats_total.suspends - n->stats_last_clear.suspends;
476 do_stat_segment_updates (stat_segment_main_t * sm)
478 vlib_main_t *vm = vlib_mains[0];
480 u64 input_packets, last_input_packets;
482 vlib_main_t *this_vlib_main;
486 * Compute the average vector rate across all workers
490 start = vec_len (vlib_mains) > 1 ? 1 : 0;
492 for (i = start; i < vec_len (vlib_mains); i++)
494 this_vlib_main = vlib_mains[i];
495 vector_rate += vlib_last_vector_length_per_node (this_vlib_main);
497 vector_rate /= (f64) (i - start);
499 sm->directory_vector[STAT_COUNTER_VECTOR_RATE].value =
500 vector_rate / ((f64) (vec_len (vlib_mains) - start));
503 * Compute the aggregate input rate
505 now = vlib_time_now (vm);
506 dt = now - sm->directory_vector[STAT_COUNTER_LAST_UPDATE].value;
507 input_packets = vnet_get_aggregate_rx_packets ();
508 sm->directory_vector[STAT_COUNTER_INPUT_RATE].value =
509 (f64) (input_packets - sm->last_input_packets) / dt;
510 sm->directory_vector[STAT_COUNTER_LAST_UPDATE].value = now;
511 sm->last_input_packets = input_packets;
512 sm->directory_vector[STAT_COUNTER_LAST_STATS_CLEAR].value =
513 vm->node_main.time_last_runtime_stats_clear;
515 if (sm->node_counters_enabled)
516 update_node_counters (sm);
518 /* Heartbeat, so clients detect we're still here */
519 sm->directory_vector[STAT_COUNTER_HEARTBEAT].value++;
523 * Accept connection on the socket and exchange the fd for the shared
526 static clib_error_t *
527 stats_socket_accept_ready (clib_file_t * uf)
529 stat_segment_main_t *sm = &stat_segment_main;
531 clib_socket_t client = { 0 };
533 err = clib_socket_accept (sm->socket, &client);
536 clib_error_report (err);
540 /* Send the fd across and close */
541 err = clib_socket_sendmsg (&client, 0, 0, &sm->memfd, 1);
543 clib_error_report (err);
544 clib_socket_close (&client);
550 stats_segment_socket_init (void)
552 stat_segment_main_t *sm = &stat_segment_main;
554 clib_socket_t *s = clib_mem_alloc (sizeof (clib_socket_t));
556 s->config = (char *) sm->socket_name;
557 s->flags = CLIB_SOCKET_F_IS_SERVER | CLIB_SOCKET_F_SEQPACKET |
558 CLIB_SOCKET_F_ALLOW_GROUP_WRITE | CLIB_SOCKET_F_PASSCRED;
559 if ((error = clib_socket_init (s)))
561 clib_error_report (error);
565 clib_file_t template = { 0 };
566 template.read_function = stats_socket_accept_ready;
567 template.file_descriptor = s->fd;
568 template.description =
569 format (0, "stats segment listener %s", STAT_SEGMENT_SOCKET_FILE);
570 clib_file_add (&file_main, &template);
576 stat_segment_collector_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
579 stat_segment_main_t *sm = &stat_segment_main;
581 /* Wait for Godot... */
582 f64 sleep_duration = 10;
586 do_stat_segment_updates (sm);
587 vlib_process_suspend (vm, sleep_duration);
589 return 0; /* or not */
592 static clib_error_t *
593 statseg_init (vlib_main_t * vm)
595 stat_segment_main_t *sm = &stat_segment_main;
598 if ((error = vlib_call_init_function (vm, unix_input_init)))
602 stats_segment_socket_init ();
607 static clib_error_t *
608 statseg_config (vlib_main_t * vm, unformat_input_t * input)
610 stat_segment_main_t *sm = &stat_segment_main;
612 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
614 if (unformat (input, "socket-name %s", &sm->socket_name))
616 else if (unformat (input, "default"))
617 sm->socket_name = format (0, "%s", STAT_SEGMENT_SOCKET_FILE);
620 (input, "size %U", unformat_memory_size, &sm->memory_size))
622 else if (unformat (input, "per-node-counters on"))
623 sm->node_counters_enabled = 1;
624 else if (unformat (input, "per-node-counters off"))
625 sm->node_counters_enabled = 0;
627 return clib_error_return (0, "unknown input `%U'",
628 format_unformat_error, input);
634 VLIB_INIT_FUNCTION (statseg_init);
635 VLIB_EARLY_CONFIG_FUNCTION (statseg_config, "statseg");
638 VLIB_REGISTER_NODE (stat_segment_collector, static) =
640 .function = stat_segment_collector_process,.name =
641 "statseg-collector-process",.type = VLIB_NODE_TYPE_PROCESS,};
646 * fd.io coding-style-patch-verification: ON
649 * eval: (c-set-style "gnu")