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