blob: 9c982a9a14ebe7f244931ba2ed35560ded478703 [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
Damjan Marion8973b072022-03-01 15:51:18 +01009static inline void
10update_node_counters (vlib_stats_segment_t *sm)
11{
12 vlib_main_t **stat_vms = 0;
13 vlib_node_t ***node_dups = 0;
14 int i, j;
15 static u32 no_max_nodes = 0;
16
17 vlib_node_get_nodes (0 /* vm, for barrier sync */,
18 (u32) ~0 /* all threads */, 1 /* include stats */,
19 0 /* barrier sync */, &node_dups, &stat_vms);
20
21 u32 l = vec_len (node_dups[0]);
22 u8 *symlink_name = 0;
23
24 /*
25 * Extend performance nodes if necessary
26 */
27 if (l > no_max_nodes)
28 {
Damjan Marionb3a5b392022-03-31 17:56:58 +020029 u32 last_thread = vlib_get_n_threads ();
Damjan Marion8973b072022-03-01 15:51:18 +010030 void *oldheap = clib_mem_set_heap (sm->heap);
31 vlib_stats_segment_lock ();
32
Damjan Marionb3a5b392022-03-31 17:56:58 +020033 vlib_stats_validate (STAT_COUNTER_NODE_CLOCKS, last_thread, l - 1);
34 vlib_stats_validate (STAT_COUNTER_NODE_VECTORS, last_thread, l - 1);
35 vlib_stats_validate (STAT_COUNTER_NODE_CALLS, last_thread, l - 1);
36 vlib_stats_validate (STAT_COUNTER_NODE_SUSPENDS, last_thread, l - 1);
Damjan Marion8973b072022-03-01 15:51:18 +010037
38 vec_validate (sm->nodes, l - 1);
39 vlib_stats_entry_t *ep;
40 ep = &sm->directory_vector[STAT_COUNTER_NODE_NAMES];
41 ep->data = sm->nodes;
42
43 /* Update names dictionary */
44 vlib_node_t **nodes = node_dups[0];
45 int i;
46 for (i = 0; i < vec_len (nodes); i++)
47 {
48 vlib_node_t *n = nodes[i];
49 u8 *s = format (0, "%v%c", n->name, 0);
50 if (sm->nodes[n->index])
51 vec_free (sm->nodes[n->index]);
52 sm->nodes[n->index] = s;
53
54 oldheap = clib_mem_set_heap (oldheap);
55#define _(E, t, name, p) \
56 vlib_stats_add_symlink (STAT_COUNTER_##E, n->index, "/nodes/%U/" #name, \
57 format_vlib_stats_symlink, s);
58 foreach_stat_segment_node_counter_name
59#undef _
60 oldheap = clib_mem_set_heap (oldheap);
61 }
62
63 vlib_stats_segment_unlock ();
64 clib_mem_set_heap (oldheap);
65 no_max_nodes = l;
66 }
67
68 for (j = 0; j < vec_len (node_dups); j++)
69 {
70 vlib_node_t **nodes = node_dups[j];
71
72 for (i = 0; i < vec_len (nodes); i++)
73 {
74 counter_t **counters;
75 counter_t *c;
76 vlib_node_t *n = nodes[i];
77
78 if (j == 0)
79 {
80 if (strncmp ((char *) sm->nodes[n->index], (char *) n->name,
81 strlen ((char *) sm->nodes[n->index])))
82 {
83 u32 vector_index;
84 void *oldheap = clib_mem_set_heap (sm->heap);
85 vlib_stats_segment_lock ();
86 u8 *s = format (0, "%v%c", n->name, 0);
87 clib_mem_set_heap (oldheap);
88#define _(E, t, name, p) \
89 vec_reset_length (symlink_name); \
90 symlink_name = format (symlink_name, "/nodes/%U/" #name, \
91 format_vlib_stats_symlink, sm->nodes[n->index]); \
92 vector_index = vlib_stats_find_entry_index ("%v", symlink_name); \
93 ASSERT (vector_index != -1); \
94 vlib_stats_rename_symlink (vector_index, "/nodes/%U/" #name, \
95 format_vlib_stats_symlink, s);
96 foreach_stat_segment_node_counter_name
97#undef _
98 vec_free (symlink_name);
99 clib_mem_set_heap (sm->heap);
100 vec_free (sm->nodes[n->index]);
101 sm->nodes[n->index] = s;
102 vlib_stats_segment_unlock ();
103 clib_mem_set_heap (oldheap);
104 }
105 }
106
107 counters = sm->directory_vector[STAT_COUNTER_NODE_CLOCKS].data;
108 c = counters[j];
109 c[n->index] = n->stats_total.clocks - n->stats_last_clear.clocks;
110
111 counters = sm->directory_vector[STAT_COUNTER_NODE_VECTORS].data;
112 c = counters[j];
113 c[n->index] = n->stats_total.vectors - n->stats_last_clear.vectors;
114
115 counters = sm->directory_vector[STAT_COUNTER_NODE_CALLS].data;
116 c = counters[j];
117 c[n->index] = n->stats_total.calls - n->stats_last_clear.calls;
118
119 counters = sm->directory_vector[STAT_COUNTER_NODE_SUSPENDS].data;
120 c = counters[j];
121 c[n->index] = n->stats_total.suspends - n->stats_last_clear.suspends;
122 }
123 vec_free (node_dups[j]);
124 }
125 vec_free (node_dups);
126 vec_free (stat_vms);
127}
128
129static void
130do_stat_segment_updates (vlib_main_t *vm, vlib_stats_segment_t *sm)
131{
132 if (sm->node_counters_enabled)
133 update_node_counters (sm);
134
135 vlib_stats_collector_t *c;
136 pool_foreach (c, sm->collectors)
137 {
138 vlib_stats_collector_data_t data = {
139 .entry_index = c->entry_index,
140 .vector_index = c->vector_index,
141 .private_data = c->private_data,
142 .entry = sm->directory_vector + c->entry_index,
143 };
144 c->fn (&data);
145 }
146
147 /* Heartbeat, so clients detect we're still here */
148 sm->directory_vector[STAT_COUNTER_HEARTBEAT].value++;
149}
150
151static uword
152stat_segment_collector_process (vlib_main_t *vm, vlib_node_runtime_t *rt,
153 vlib_frame_t *f)
154{
155 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
156
157 while (1)
158 {
159 do_stat_segment_updates (vm, sm);
160 vlib_process_suspend (vm, sm->update_interval);
161 }
162 return 0; /* or not */
163}
164
165VLIB_REGISTER_NODE (stat_segment_collector, static) = {
166 .function = stat_segment_collector_process,
167 .name = "statseg-collector-process",
168 .type = VLIB_NODE_TYPE_PROCESS,
169};