blob: 3cfbd2dbd66bae7c541743b8b571c009c45d7cfd [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;
101
102 shared_header->epoch = 1;
103
104 /* Scalar stats and node counters */
105 vec_validate (sm->directory_vector, STAT_COUNTERS - 1);
106#define _(E, t, n, p) \
107 strcpy (sm->directory_vector[STAT_COUNTER_##E].name, p "/" #n); \
108 sm->directory_vector[STAT_COUNTER_##E].type = STAT_DIR_TYPE_##t;
109 foreach_stat_segment_counter_name
110#undef _
111 /* Save the vector in the shared segment, for clients */
112 shared_header->directory_vector = sm->directory_vector;
113
114 clib_mem_set_heap (oldheap);
115
116 vlib_stats_register_mem_heap (heap);
117
118 reg.collect_fn = vector_rate_collector_fn;
119 reg.private_data = vlib_stats_add_gauge ("/sys/vector_rate");
120 reg.entry_index =
121 vlib_stats_add_counter_vector ("/sys/vector_rate_per_worker");
122 vlib_stats_register_collector_fn (&reg);
Damjan Marion58fd4812022-03-14 13:04:38 +0100123 vlib_stats_validate (reg.entry_index, 0, vlib_get_n_threads ());
Damjan Marion8973b072022-03-01 15:51:18 +0100124
125 return 0;
126}
127
128static clib_error_t *
129statseg_config (vlib_main_t *vm, unformat_input_t *input)
130{
131 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
132 sm->update_interval = 10.0;
133
134 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
135 {
136 if (unformat (input, "socket-name %s", &sm->socket_name))
137 ;
138 /* DEPRECATE: default (does nothing) */
139 else if (unformat (input, "default"))
140 ;
141 else if (unformat (input, "size %U", unformat_memory_size,
142 &sm->memory_size))
143 ;
144 else if (unformat (input, "page-size %U", unformat_log2_page_size,
145 &sm->log2_page_sz))
146 ;
147 else if (unformat (input, "per-node-counters on"))
148 sm->node_counters_enabled = 1;
149 else if (unformat (input, "per-node-counters off"))
150 sm->node_counters_enabled = 0;
151 else if (unformat (input, "update-interval %f", &sm->update_interval))
152 ;
153 else
154 return clib_error_return (0, "unknown input `%U'",
155 format_unformat_error, input);
156 }
157
158 /*
159 * NULL-terminate socket name string
160 * clib_socket_init()->socket_config() use C str*
161 */
162 if (vec_len (sm->socket_name))
163 vec_terminate_c_string (sm->socket_name);
164
165 return 0;
166}
167
168VLIB_EARLY_CONFIG_FUNCTION (statseg_config, "statseg");
169
170/*
171 * Accept connection on the socket and exchange the fd for the shared
172 * memory segment.
173 */
174static clib_error_t *
175stats_socket_accept_ready (clib_file_t *uf)
176{
177 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
178 clib_error_t *err;
179 clib_socket_t client = { 0 };
180
181 err = clib_socket_accept (sm->socket, &client);
182 if (err)
183 {
184 clib_error_report (err);
185 return err;
186 }
187
188 /* Send the fd across and close */
189 err = clib_socket_sendmsg (&client, 0, 0, &sm->memfd, 1);
190 if (err)
191 clib_error_report (err);
192 clib_socket_close (&client);
193
194 return 0;
195}
196
197static clib_error_t *
198stats_segment_socket_init (void)
199{
200 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
201 clib_error_t *error;
202 clib_socket_t *s = clib_mem_alloc (sizeof (clib_socket_t));
203
204 memset (s, 0, sizeof (clib_socket_t));
205 s->config = (char *) sm->socket_name;
206 s->flags = CLIB_SOCKET_F_IS_SERVER | CLIB_SOCKET_F_SEQPACKET |
207 CLIB_SOCKET_F_ALLOW_GROUP_WRITE | CLIB_SOCKET_F_PASSCRED;
208
209 if ((error = clib_socket_init (s)))
210 return error;
211
212 clib_file_t template = { 0 };
213 template.read_function = stats_socket_accept_ready;
214 template.file_descriptor = s->fd;
215 template.description = format (0, "stats segment listener %s", s->config);
216 clib_file_add (&file_main, &template);
217
218 sm->socket = s;
219
220 return 0;
221}
222
223static clib_error_t *
224stats_segment_socket_exit (vlib_main_t *vm)
225{
226 /*
227 * cleanup the listener socket on exit.
228 */
229 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
230 unlink ((char *) sm->socket_name);
231 return 0;
232}
233
234VLIB_MAIN_LOOP_EXIT_FUNCTION (stats_segment_socket_exit);
235
236static clib_error_t *
237statseg_init (vlib_main_t *vm)
238{
239 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
240
241 /* set default socket file name when statseg config stanza is empty. */
242 if (!vec_len (sm->socket_name))
243 sm->socket_name = format (0, "%s/%s%c", vlib_unix_get_runtime_dir (),
244 STAT_SEGMENT_SOCKET_FILENAME, 0);
245 return stats_segment_socket_init ();
246}
247
248VLIB_INIT_FUNCTION (statseg_init) = {
249 .runs_after = VLIB_INITS ("unix_input_init", "linux_epoll_input_init"),
250};