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