1 /* SPDX-License-Identifier: Apache-2.0
2 * Copyright(c) 2022 Cisco Systems, Inc.
6 #include <vlib/unix/unix.h>
7 #include <vlib/stats/stats.h>
10 stat_validate_counter_vector2 (vlib_stats_entry_t *ep, u32 max1, u32 max2)
12 counter_t **counters = ep->data;
14 vec_validate_aligned (counters, max1, CLIB_CACHE_LINE_BYTES);
15 for (i = 0; i <= max1; i++)
16 vec_validate_aligned (counters[i], max2, CLIB_CACHE_LINE_BYTES);
22 stat_validate_counter_vector (vlib_stats_entry_t *ep, u32 max)
24 vlib_thread_main_t *tm = vlib_get_thread_main ();
25 ASSERT (tm->n_vlib_mains > 0);
26 stat_validate_counter_vector2 (ep, tm->n_vlib_mains, max);
30 update_node_counters (vlib_stats_segment_t *sm)
32 vlib_main_t **stat_vms = 0;
33 vlib_node_t ***node_dups = 0;
35 static u32 no_max_nodes = 0;
37 vlib_node_get_nodes (0 /* vm, for barrier sync */,
38 (u32) ~0 /* all threads */, 1 /* include stats */,
39 0 /* barrier sync */, &node_dups, &stat_vms);
41 u32 l = vec_len (node_dups[0]);
45 * Extend performance nodes if necessary
49 void *oldheap = clib_mem_set_heap (sm->heap);
50 vlib_stats_segment_lock ();
52 stat_validate_counter_vector (
53 &sm->directory_vector[STAT_COUNTER_NODE_CLOCKS], l - 1);
54 stat_validate_counter_vector (
55 &sm->directory_vector[STAT_COUNTER_NODE_VECTORS], l - 1);
56 stat_validate_counter_vector (
57 &sm->directory_vector[STAT_COUNTER_NODE_CALLS], l - 1);
58 stat_validate_counter_vector (
59 &sm->directory_vector[STAT_COUNTER_NODE_SUSPENDS], l - 1);
61 vec_validate (sm->nodes, l - 1);
62 vlib_stats_entry_t *ep;
63 ep = &sm->directory_vector[STAT_COUNTER_NODE_NAMES];
66 /* Update names dictionary */
67 vlib_node_t **nodes = node_dups[0];
69 for (i = 0; i < vec_len (nodes); i++)
71 vlib_node_t *n = nodes[i];
72 u8 *s = format (0, "%v%c", n->name, 0);
73 if (sm->nodes[n->index])
74 vec_free (sm->nodes[n->index]);
75 sm->nodes[n->index] = s;
77 oldheap = clib_mem_set_heap (oldheap);
78 #define _(E, t, name, p) \
79 vlib_stats_add_symlink (STAT_COUNTER_##E, n->index, "/nodes/%U/" #name, \
80 format_vlib_stats_symlink, s);
81 foreach_stat_segment_node_counter_name
83 oldheap = clib_mem_set_heap (oldheap);
86 vlib_stats_segment_unlock ();
87 clib_mem_set_heap (oldheap);
91 for (j = 0; j < vec_len (node_dups); j++)
93 vlib_node_t **nodes = node_dups[j];
95 for (i = 0; i < vec_len (nodes); i++)
99 vlib_node_t *n = nodes[i];
103 if (strncmp ((char *) sm->nodes[n->index], (char *) n->name,
104 strlen ((char *) sm->nodes[n->index])))
107 void *oldheap = clib_mem_set_heap (sm->heap);
108 vlib_stats_segment_lock ();
109 u8 *s = format (0, "%v%c", n->name, 0);
110 clib_mem_set_heap (oldheap);
111 #define _(E, t, name, p) \
112 vec_reset_length (symlink_name); \
113 symlink_name = format (symlink_name, "/nodes/%U/" #name, \
114 format_vlib_stats_symlink, sm->nodes[n->index]); \
115 vector_index = vlib_stats_find_entry_index ("%v", symlink_name); \
116 ASSERT (vector_index != -1); \
117 vlib_stats_rename_symlink (vector_index, "/nodes/%U/" #name, \
118 format_vlib_stats_symlink, s);
119 foreach_stat_segment_node_counter_name
121 vec_free (symlink_name);
122 clib_mem_set_heap (sm->heap);
123 vec_free (sm->nodes[n->index]);
124 sm->nodes[n->index] = s;
125 vlib_stats_segment_unlock ();
126 clib_mem_set_heap (oldheap);
130 counters = sm->directory_vector[STAT_COUNTER_NODE_CLOCKS].data;
132 c[n->index] = n->stats_total.clocks - n->stats_last_clear.clocks;
134 counters = sm->directory_vector[STAT_COUNTER_NODE_VECTORS].data;
136 c[n->index] = n->stats_total.vectors - n->stats_last_clear.vectors;
138 counters = sm->directory_vector[STAT_COUNTER_NODE_CALLS].data;
140 c[n->index] = n->stats_total.calls - n->stats_last_clear.calls;
142 counters = sm->directory_vector[STAT_COUNTER_NODE_SUSPENDS].data;
144 c[n->index] = n->stats_total.suspends - n->stats_last_clear.suspends;
146 vec_free (node_dups[j]);
148 vec_free (node_dups);
153 do_stat_segment_updates (vlib_main_t *vm, vlib_stats_segment_t *sm)
155 if (sm->node_counters_enabled)
156 update_node_counters (sm);
158 vlib_stats_collector_t *c;
159 pool_foreach (c, sm->collectors)
161 vlib_stats_collector_data_t data = {
162 .entry_index = c->entry_index,
163 .vector_index = c->vector_index,
164 .private_data = c->private_data,
165 .entry = sm->directory_vector + c->entry_index,
170 /* Heartbeat, so clients detect we're still here */
171 sm->directory_vector[STAT_COUNTER_HEARTBEAT].value++;
175 stat_segment_collector_process (vlib_main_t *vm, vlib_node_runtime_t *rt,
178 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
182 do_stat_segment_updates (vm, sm);
183 vlib_process_suspend (vm, sm->update_interval);
185 return 0; /* or not */
188 VLIB_REGISTER_NODE (stat_segment_collector, static) = {
189 .function = stat_segment_collector_process,
190 .name = "statseg-collector-process",
191 .type = VLIB_NODE_TYPE_PROCESS,