blob: 2a7b6f53c538ff6a1620455b7a8b513b19070af8 [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
9#define STAT_SEGMENT_SOCKET_FILENAME "stats.sock"
10
11static void
12vector_rate_collector_fn (vlib_stats_collector_data_t *d)
13{
14 vlib_main_t *this_vlib_main;
Damjan Marionfa7f21b2022-03-23 16:35:18 +010015 counter_t **counters;
16 counter_t *cb;
Damjan Marion8973b072022-03-01 15:51:18 +010017 f64 vector_rate = 0.0;
18 u32 i, n_threads = vlib_get_n_threads ();
19
Damjan Marion58fd4812022-03-14 13:04:38 +010020 vlib_stats_validate (d->entry_index, 0, n_threads - 1);
Damjan Marionfa7f21b2022-03-23 16:35:18 +010021 counters = d->entry->data;
22 cb = counters[0];
Damjan Marion8973b072022-03-01 15:51:18 +010023
24 for (i = 0; i < n_threads; i++)
25 {
26 f64 this_vector_rate;
27 this_vlib_main = vlib_get_main_by_index (i);
28
29 this_vector_rate = vlib_internal_node_vector_rate (this_vlib_main);
30 vlib_clear_internal_node_vector_rate (this_vlib_main);
31
32 cb[i] = this_vector_rate;
33 vector_rate += this_vector_rate;
34 }
35
36 /* And set the system average rate */
37 vector_rate /= (f64) (i > 1 ? i - 1 : 1);
38 vlib_stats_set_gauge (d->private_data, vector_rate);
39}
40
41clib_error_t *
42vlib_stats_init (vlib_main_t *vm)
43{
44 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
45 vlib_stats_shared_header_t *shared_header;
46 vlib_stats_collector_reg_t reg = {};
47
48 void *oldheap;
49 uword memory_size, sys_page_sz;
50 int mfd;
51 char *mem_name = "stat segment";
52 void *heap, *memaddr;
53
54 memory_size = sm->memory_size;
55 if (memory_size == 0)
56 memory_size = STAT_SEGMENT_DEFAULT_SIZE;
57
58 if (sm->log2_page_sz == CLIB_MEM_PAGE_SZ_UNKNOWN)
59 sm->log2_page_sz = CLIB_MEM_PAGE_SZ_DEFAULT;
60
61 mfd = clib_mem_vm_create_fd (sm->log2_page_sz, mem_name);
62
63 if (mfd == -1)
64 return clib_error_return (0, "stat segment memory fd failure: %U",
65 format_clib_error, clib_mem_get_last_error ());
66 /* Set size */
67 if ((ftruncate (mfd, memory_size)) == -1)
68 {
69 close (mfd);
70 return clib_error_return (0, "stat segment ftruncate failure");
71 }
72
73 memaddr = clib_mem_vm_map_shared (0, memory_size, mfd, 0, mem_name);
74
75 if (memaddr == CLIB_MEM_VM_MAP_FAILED)
76 return clib_error_return (0, "stat segment mmap failure");
77
78 sys_page_sz = clib_mem_get_page_size ();
79
80 heap =
81 clib_mem_create_heap (((u8 *) memaddr) + sys_page_sz,
82 memory_size - sys_page_sz, 1 /* locked */, mem_name);
83 sm->heap = heap;
84 sm->memfd = mfd;
85
86 sm->directory_vector_by_name = hash_create_string (0, sizeof (uword));
87 sm->shared_header = shared_header = memaddr;
88
89 shared_header->version = STAT_SEGMENT_VERSION;
90 shared_header->base = memaddr;
91
92 sm->stat_segment_lockp = clib_mem_alloc (sizeof (clib_spinlock_t));
Damjan Mariond1bd9af2022-03-14 13:04:38 +010093 sm->locking_thread_index = ~0;
94 sm->n_locks = 0;
Damjan Marion8973b072022-03-01 15:51:18 +010095 clib_spinlock_init (sm->stat_segment_lockp);
96
97 oldheap = clib_mem_set_heap (sm->heap);
98
99 /* Set up the name to counter-vector hash table */
100 sm->directory_vector = 0;
Damjan Marioneecec8c2022-04-06 12:06:41 +0200101 sm->dir_vector_first_free_elt = CLIB_U32_MAX;
Damjan Marion8973b072022-03-01 15:51:18 +0100102
103 shared_header->epoch = 1;
104
105 /* Scalar stats and node counters */
106 vec_validate (sm->directory_vector, STAT_COUNTERS - 1);
107#define _(E, t, n, p) \
108 strcpy (sm->directory_vector[STAT_COUNTER_##E].name, p "/" #n); \
109 sm->directory_vector[STAT_COUNTER_##E].type = STAT_DIR_TYPE_##t;
110 foreach_stat_segment_counter_name
111#undef _
112 /* Save the vector in the shared segment, for clients */
113 shared_header->directory_vector = sm->directory_vector;
114
115 clib_mem_set_heap (oldheap);
116
117 vlib_stats_register_mem_heap (heap);
118
119 reg.collect_fn = vector_rate_collector_fn;
120 reg.private_data = vlib_stats_add_gauge ("/sys/vector_rate");
121 reg.entry_index =
122 vlib_stats_add_counter_vector ("/sys/vector_rate_per_worker");
123 vlib_stats_register_collector_fn (&reg);
Damjan Marion58fd4812022-03-14 13:04:38 +0100124 vlib_stats_validate (reg.entry_index, 0, vlib_get_n_threads ());
Damjan Marion8973b072022-03-01 15:51:18 +0100125
126 return 0;
127}
128
129static clib_error_t *
130statseg_config (vlib_main_t *vm, unformat_input_t *input)
131{
132 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
133 sm->update_interval = 10.0;
134
135 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
136 {
137 if (unformat (input, "socket-name %s", &sm->socket_name))
138 ;
139 /* DEPRECATE: default (does nothing) */
140 else if (unformat (input, "default"))
141 ;
142 else if (unformat (input, "size %U", unformat_memory_size,
143 &sm->memory_size))
144 ;
145 else if (unformat (input, "page-size %U", unformat_log2_page_size,
146 &sm->log2_page_sz))
147 ;
148 else if (unformat (input, "per-node-counters on"))
149 sm->node_counters_enabled = 1;
150 else if (unformat (input, "per-node-counters off"))
151 sm->node_counters_enabled = 0;
152 else if (unformat (input, "update-interval %f", &sm->update_interval))
153 ;
154 else
155 return clib_error_return (0, "unknown input `%U'",
156 format_unformat_error, input);
157 }
158
159 /*
160 * NULL-terminate socket name string
161 * clib_socket_init()->socket_config() use C str*
162 */
163 if (vec_len (sm->socket_name))
164 vec_terminate_c_string (sm->socket_name);
165
166 return 0;
167}
168
169VLIB_EARLY_CONFIG_FUNCTION (statseg_config, "statseg");
170
171/*
172 * Accept connection on the socket and exchange the fd for the shared
173 * memory segment.
174 */
175static clib_error_t *
176stats_socket_accept_ready (clib_file_t *uf)
177{
178 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
179 clib_error_t *err;
180 clib_socket_t client = { 0 };
181
182 err = clib_socket_accept (sm->socket, &client);
183 if (err)
184 {
185 clib_error_report (err);
186 return err;
187 }
188
189 /* Send the fd across and close */
190 err = clib_socket_sendmsg (&client, 0, 0, &sm->memfd, 1);
191 if (err)
192 clib_error_report (err);
193 clib_socket_close (&client);
194
195 return 0;
196}
197
198static clib_error_t *
199stats_segment_socket_init (void)
200{
201 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
202 clib_error_t *error;
203 clib_socket_t *s = clib_mem_alloc (sizeof (clib_socket_t));
204
205 memset (s, 0, sizeof (clib_socket_t));
206 s->config = (char *) sm->socket_name;
207 s->flags = CLIB_SOCKET_F_IS_SERVER | CLIB_SOCKET_F_SEQPACKET |
208 CLIB_SOCKET_F_ALLOW_GROUP_WRITE | CLIB_SOCKET_F_PASSCRED;
209
210 if ((error = clib_socket_init (s)))
211 return error;
212
213 clib_file_t template = { 0 };
214 template.read_function = stats_socket_accept_ready;
215 template.file_descriptor = s->fd;
216 template.description = format (0, "stats segment listener %s", s->config);
217 clib_file_add (&file_main, &template);
218
219 sm->socket = s;
220
221 return 0;
222}
223
224static clib_error_t *
225stats_segment_socket_exit (vlib_main_t *vm)
226{
227 /*
228 * cleanup the listener socket on exit.
229 */
230 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
231 unlink ((char *) sm->socket_name);
232 return 0;
233}
234
235VLIB_MAIN_LOOP_EXIT_FUNCTION (stats_segment_socket_exit);
236
237static clib_error_t *
238statseg_init (vlib_main_t *vm)
239{
240 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
241
242 /* set default socket file name when statseg config stanza is empty. */
243 if (!vec_len (sm->socket_name))
244 sm->socket_name = format (0, "%s/%s%c", vlib_unix_get_runtime_dir (),
245 STAT_SEGMENT_SOCKET_FILENAME, 0);
246 return stats_segment_socket_init ();
247}
248
249VLIB_INIT_FUNCTION (statseg_init) = {
250 .runs_after = VLIB_INITS ("unix_input_init", "linux_epoll_input_init"),
251};