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 | |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 9 | enum |
| 10 | { |
| 11 | NODE_CLOCKS, |
| 12 | NODE_VECTORS, |
| 13 | NODE_CALLS, |
| 14 | NODE_SUSPENDS, |
| 15 | N_NODE_COUNTERS |
| 16 | }; |
| 17 | |
| 18 | struct |
| 19 | { |
| 20 | u32 entry_index; |
| 21 | char *name; |
| 22 | } node_counters[] = { |
| 23 | [NODE_CLOCKS] = { .name = "clocks" }, |
| 24 | [NODE_VECTORS] = { .name = "vectors" }, |
| 25 | [NODE_CALLS] = { .name = "calls" }, |
| 26 | [NODE_SUSPENDS] = { .name = "suspends" }, |
| 27 | }; |
| 28 | |
| 29 | static struct |
| 30 | { |
| 31 | u8 *name; |
| 32 | u32 symlinks[N_NODE_COUNTERS]; |
| 33 | } *node_data = 0; |
| 34 | |
| 35 | static vlib_stats_string_vector_t node_names = 0; |
| 36 | |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 37 | static inline void |
| 38 | update_node_counters (vlib_stats_segment_t *sm) |
| 39 | { |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 40 | clib_bitmap_t *bmp = 0; |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 41 | vlib_main_t **stat_vms = 0; |
| 42 | vlib_node_t ***node_dups = 0; |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 43 | u32 n_nodes; |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 44 | int i, j; |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 45 | |
| 46 | vlib_node_get_nodes (0 /* vm, for barrier sync */, |
| 47 | (u32) ~0 /* all threads */, 1 /* include stats */, |
| 48 | 0 /* barrier sync */, &node_dups, &stat_vms); |
| 49 | |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 50 | n_nodes = vec_len (node_dups[0]); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 51 | |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 52 | vec_validate (node_data, n_nodes - 1); |
| 53 | |
| 54 | for (i = 0; i < n_nodes; i++) |
| 55 | if (vec_is_equal (node_data[i].name, node_dups[0][i]) == 0) |
| 56 | bmp = clib_bitmap_set (bmp, i, 1); |
| 57 | |
| 58 | if (bmp) |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 59 | { |
Damjan Marion | b3a5b39 | 2022-03-31 17:56:58 +0200 | [diff] [blame] | 60 | u32 last_thread = vlib_get_n_threads (); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 61 | vlib_stats_segment_lock (); |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 62 | clib_bitmap_foreach (i, bmp) |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 63 | { |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 64 | vlib_node_t *n = node_dups[0][i]; |
| 65 | if (node_data[i].name) |
| 66 | { |
| 67 | vec_free (node_data[i].name); |
| 68 | for (j = 0; j < ARRAY_LEN (node_data->symlinks); j++) |
| 69 | vlib_stats_remove_entry (node_data[i].symlinks[j]); |
| 70 | } |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 71 | |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 72 | node_data[i].name = vec_dup (node_dups[0][i]->name); |
| 73 | vlib_stats_set_string_vector (&node_names, n->index, "%v", n->name); |
| 74 | |
| 75 | for (int j = 0; j < ARRAY_LEN (node_counters); j++) |
| 76 | { |
| 77 | vlib_stats_validate (node_counters[j].entry_index, last_thread, |
| 78 | n_nodes - 1); |
| 79 | node_data[i].symlinks[j] = vlib_stats_add_symlink ( |
| 80 | node_counters[j].entry_index, n->index, "/nodes/%U/%s", |
| 81 | format_vlib_stats_symlink, n->name, node_counters[j].name); |
| 82 | ASSERT (node_data[i].symlinks[j] != CLIB_U32_MAX); |
| 83 | } |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 84 | } |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 85 | vlib_stats_segment_unlock (); |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 86 | vec_free (bmp); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | for (j = 0; j < vec_len (node_dups); j++) |
| 90 | { |
| 91 | vlib_node_t **nodes = node_dups[j]; |
| 92 | |
| 93 | for (i = 0; i < vec_len (nodes); i++) |
| 94 | { |
| 95 | counter_t **counters; |
| 96 | counter_t *c; |
| 97 | vlib_node_t *n = nodes[i]; |
| 98 | |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 99 | counters = vlib_stats_get_entry_data_pointer ( |
| 100 | node_counters[NODE_CLOCKS].entry_index); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 101 | c = counters[j]; |
| 102 | c[n->index] = n->stats_total.clocks - n->stats_last_clear.clocks; |
| 103 | |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 104 | counters = vlib_stats_get_entry_data_pointer ( |
| 105 | node_counters[NODE_VECTORS].entry_index); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 106 | c = counters[j]; |
| 107 | c[n->index] = n->stats_total.vectors - n->stats_last_clear.vectors; |
| 108 | |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 109 | counters = vlib_stats_get_entry_data_pointer ( |
| 110 | node_counters[NODE_CALLS].entry_index); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 111 | c = counters[j]; |
| 112 | c[n->index] = n->stats_total.calls - n->stats_last_clear.calls; |
| 113 | |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 114 | counters = vlib_stats_get_entry_data_pointer ( |
| 115 | node_counters[NODE_SUSPENDS].entry_index); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 116 | c = counters[j]; |
| 117 | c[n->index] = n->stats_total.suspends - n->stats_last_clear.suspends; |
| 118 | } |
| 119 | vec_free (node_dups[j]); |
| 120 | } |
| 121 | vec_free (node_dups); |
| 122 | vec_free (stat_vms); |
| 123 | } |
| 124 | |
| 125 | static void |
| 126 | do_stat_segment_updates (vlib_main_t *vm, vlib_stats_segment_t *sm) |
| 127 | { |
| 128 | if (sm->node_counters_enabled) |
| 129 | update_node_counters (sm); |
| 130 | |
| 131 | vlib_stats_collector_t *c; |
| 132 | pool_foreach (c, sm->collectors) |
| 133 | { |
| 134 | vlib_stats_collector_data_t data = { |
| 135 | .entry_index = c->entry_index, |
| 136 | .vector_index = c->vector_index, |
| 137 | .private_data = c->private_data, |
| 138 | .entry = sm->directory_vector + c->entry_index, |
| 139 | }; |
| 140 | c->fn (&data); |
| 141 | } |
| 142 | |
| 143 | /* Heartbeat, so clients detect we're still here */ |
| 144 | sm->directory_vector[STAT_COUNTER_HEARTBEAT].value++; |
| 145 | } |
| 146 | |
| 147 | static uword |
| 148 | stat_segment_collector_process (vlib_main_t *vm, vlib_node_runtime_t *rt, |
| 149 | vlib_frame_t *f) |
| 150 | { |
| 151 | vlib_stats_segment_t *sm = vlib_stats_get_segment (); |
| 152 | |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 153 | if (sm->node_counters_enabled) |
| 154 | { |
| 155 | node_names = vlib_stats_add_string_vector ("/sys/node/names"); |
| 156 | ASSERT (node_names); |
| 157 | |
| 158 | for (int x = 0; x < ARRAY_LEN (node_counters); x++) |
| 159 | { |
| 160 | node_counters[x].entry_index = vlib_stats_add_counter_vector ( |
| 161 | "/sys/node/%s", node_counters[x].name); |
| 162 | ASSERT (node_counters[x].entry_index != CLIB_U32_MAX); |
| 163 | } |
| 164 | } |
| 165 | |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 166 | while (1) |
| 167 | { |
| 168 | do_stat_segment_updates (vm, sm); |
| 169 | vlib_process_suspend (vm, sm->update_interval); |
| 170 | } |
| 171 | return 0; /* or not */ |
| 172 | } |
| 173 | |
| 174 | VLIB_REGISTER_NODE (stat_segment_collector, static) = { |
| 175 | .function = stat_segment_collector_process, |
| 176 | .name = "statseg-collector-process", |
| 177 | .type = VLIB_NODE_TYPE_PROCESS, |
| 178 | }; |