blob: cc348bbf3ad5554378ddf039654e1a773510b595 [file] [log] [blame]
Damjan Marion8973b072022-03-01 15:51:18 +01001/* SPDX-License-Identifier: Apache-2.0
2 * Copyright(c) 2022 Cisco Systems, Inc.
3 */
4
5#include <vlib/vlib.h>
6#include <vlib/unix/unix.h>
7#include <vlib/stats/stats.h>
8
9static void
10stat_validate_counter_vector2 (vlib_stats_entry_t *ep, u32 max1, u32 max2)
11{
12 counter_t **counters = ep->data;
13 int i;
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);
17
18 ep->data = counters;
19}
20
21static void
22stat_validate_counter_vector (vlib_stats_entry_t *ep, u32 max)
23{
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);
27}
28
29static inline void
30update_node_counters (vlib_stats_segment_t *sm)
31{
32 vlib_main_t **stat_vms = 0;
33 vlib_node_t ***node_dups = 0;
34 int i, j;
35 static u32 no_max_nodes = 0;
36
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);
40
41 u32 l = vec_len (node_dups[0]);
42 u8 *symlink_name = 0;
43
44 /*
45 * Extend performance nodes if necessary
46 */
47 if (l > no_max_nodes)
48 {
49 void *oldheap = clib_mem_set_heap (sm->heap);
50 vlib_stats_segment_lock ();
51
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);
60
61 vec_validate (sm->nodes, l - 1);
62 vlib_stats_entry_t *ep;
63 ep = &sm->directory_vector[STAT_COUNTER_NODE_NAMES];
64 ep->data = sm->nodes;
65
66 /* Update names dictionary */
67 vlib_node_t **nodes = node_dups[0];
68 int i;
69 for (i = 0; i < vec_len (nodes); i++)
70 {
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;
76
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
82#undef _
83 oldheap = clib_mem_set_heap (oldheap);
84 }
85
86 vlib_stats_segment_unlock ();
87 clib_mem_set_heap (oldheap);
88 no_max_nodes = l;
89 }
90
91 for (j = 0; j < vec_len (node_dups); j++)
92 {
93 vlib_node_t **nodes = node_dups[j];
94
95 for (i = 0; i < vec_len (nodes); i++)
96 {
97 counter_t **counters;
98 counter_t *c;
99 vlib_node_t *n = nodes[i];
100
101 if (j == 0)
102 {
103 if (strncmp ((char *) sm->nodes[n->index], (char *) n->name,
104 strlen ((char *) sm->nodes[n->index])))
105 {
106 u32 vector_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
120#undef _
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);
127 }
128 }
129
130 counters = sm->directory_vector[STAT_COUNTER_NODE_CLOCKS].data;
131 c = counters[j];
132 c[n->index] = n->stats_total.clocks - n->stats_last_clear.clocks;
133
134 counters = sm->directory_vector[STAT_COUNTER_NODE_VECTORS].data;
135 c = counters[j];
136 c[n->index] = n->stats_total.vectors - n->stats_last_clear.vectors;
137
138 counters = sm->directory_vector[STAT_COUNTER_NODE_CALLS].data;
139 c = counters[j];
140 c[n->index] = n->stats_total.calls - n->stats_last_clear.calls;
141
142 counters = sm->directory_vector[STAT_COUNTER_NODE_SUSPENDS].data;
143 c = counters[j];
144 c[n->index] = n->stats_total.suspends - n->stats_last_clear.suspends;
145 }
146 vec_free (node_dups[j]);
147 }
148 vec_free (node_dups);
149 vec_free (stat_vms);
150}
151
152static void
153do_stat_segment_updates (vlib_main_t *vm, vlib_stats_segment_t *sm)
154{
155 if (sm->node_counters_enabled)
156 update_node_counters (sm);
157
158 vlib_stats_collector_t *c;
159 pool_foreach (c, sm->collectors)
160 {
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,
166 };
167 c->fn (&data);
168 }
169
170 /* Heartbeat, so clients detect we're still here */
171 sm->directory_vector[STAT_COUNTER_HEARTBEAT].value++;
172}
173
174static uword
175stat_segment_collector_process (vlib_main_t *vm, vlib_node_runtime_t *rt,
176 vlib_frame_t *f)
177{
178 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
179
180 while (1)
181 {
182 do_stat_segment_updates (vm, sm);
183 vlib_process_suspend (vm, sm->update_interval);
184 }
185 return 0; /* or not */
186}
187
188VLIB_REGISTER_NODE (stat_segment_collector, static) = {
189 .function = stat_segment_collector_process,
190 .name = "statseg-collector-process",
191 .type = VLIB_NODE_TYPE_PROCESS,
192};