Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 1 | /* |
| 2 | * perfmon_periodic.c - skeleton plug-in periodic function |
| 3 | * |
| 4 | * Copyright (c) <current-year> <your-organization> |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <vlib/vlib.h> |
| 19 | #include <vppinfra/error.h> |
| 20 | #include <perfmon/perfmon.h> |
| 21 | #include <asm/unistd.h> |
| 22 | #include <sys/ioctl.h> |
| 23 | |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 24 | /* "not in glibc" */ |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 25 | static long |
| 26 | perf_event_open (struct perf_event_attr *hw_event, pid_t pid, int cpu, |
| 27 | int group_fd, unsigned long flags) |
| 28 | { |
| 29 | int ret; |
| 30 | |
| 31 | ret = syscall (__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags); |
| 32 | return ret; |
| 33 | } |
| 34 | |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 35 | static void |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 36 | read_current_perf_counters (vlib_node_runtime_perf_callback_data_t * data, |
| 37 | vlib_node_runtime_perf_callback_args_t * args) |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 38 | { |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 39 | int i; |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 40 | perfmon_main_t *pm = &perfmon_main; |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 41 | perfmon_thread_t *pt = data->u[0].v; |
| 42 | u64 c[2] = { 0, 0 }; |
| 43 | u64 *cc; |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 44 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 45 | if (PREDICT_FALSE (args->call_type == VLIB_NODE_RUNTIME_PERF_RESET)) |
| 46 | return; |
| 47 | |
| 48 | if (args->call_type == VLIB_NODE_RUNTIME_PERF_BEFORE) |
| 49 | cc = pt->c; |
| 50 | else |
| 51 | cc = c; |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 52 | |
| 53 | for (i = 0; i < pm->n_active; i++) |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 54 | { |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 55 | if (pt->rdpmc_indices[i] != ~0) |
| 56 | cc[i] = clib_rdpmc ((int) pt->rdpmc_indices[i]); |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 57 | else |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 58 | { |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 59 | u64 sw_value; |
Dave Barach | 5f2cfb2 | 2019-05-20 10:28:57 -0400 | [diff] [blame] | 60 | int read_result; |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 61 | if ((read_result = read (pt->pm_fds[i], &sw_value, |
| 62 | sizeof (sw_value))) != sizeof (sw_value)) |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 63 | { |
| 64 | clib_unix_warning |
Dave Barach | 5f2cfb2 | 2019-05-20 10:28:57 -0400 | [diff] [blame] | 65 | ("counter read returned %d, expected %d", |
| 66 | read_result, sizeof (sw_value)); |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 67 | clib_callback_data_enable_disable |
| 68 | (&args->vm->vlib_node_runtime_perf_callbacks, |
Dave Barach | 5f2cfb2 | 2019-05-20 10:28:57 -0400 | [diff] [blame] | 69 | read_current_perf_counters, 0 /* enable */ ); |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 70 | return; |
| 71 | } |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 72 | cc[i] = sw_value; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 73 | } |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 74 | } |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 75 | |
| 76 | if (args->call_type == VLIB_NODE_RUNTIME_PERF_AFTER) |
| 77 | { |
| 78 | u32 node_index = args->node->node_index; |
| 79 | vec_validate (pt->counters, node_index); |
| 80 | pt->counters[node_index].ticks[0] += c[0] - pt->c[0]; |
| 81 | pt->counters[node_index].ticks[1] += c[1] - pt->c[1]; |
| 82 | pt->counters[node_index].vectors += args->packets; |
| 83 | } |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static void |
| 87 | clear_counters (perfmon_main_t * pm) |
| 88 | { |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 89 | int j; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 90 | vlib_main_t *vm = pm->vlib_main; |
| 91 | vlib_main_t *stat_vm; |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 92 | perfmon_thread_t *pt; |
| 93 | u32 len; |
| 94 | |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 95 | |
| 96 | vlib_worker_thread_barrier_sync (vm); |
| 97 | |
| 98 | for (j = 0; j < vec_len (vlib_mains); j++) |
| 99 | { |
| 100 | stat_vm = vlib_mains[j]; |
| 101 | if (stat_vm == 0) |
| 102 | continue; |
| 103 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 104 | pt = pm->threads[j]; |
| 105 | len = vec_len (pt->counters); |
| 106 | if (!len) |
| 107 | continue; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 108 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 109 | clib_memset (pt->counters, 0, len * sizeof (pt->counters[0])); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 110 | } |
| 111 | vlib_worker_thread_barrier_release (vm); |
| 112 | } |
| 113 | |
| 114 | static void |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 115 | enable_current_events (perfmon_main_t * pm) |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 116 | { |
| 117 | struct perf_event_attr pe; |
| 118 | int fd; |
| 119 | struct perf_event_mmap_page *p = 0; |
| 120 | perfmon_event_config_t *c; |
| 121 | vlib_main_t *vm = vlib_get_main (); |
| 122 | u32 my_thread_index = vm->thread_index; |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 123 | perfmon_thread_t *pt = pm->threads[my_thread_index]; |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 124 | u32 index; |
| 125 | int i, limit = 1; |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 126 | int cpu; |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 127 | vlib_node_runtime_perf_callback_data_t cbdata = { 0 }; |
| 128 | cbdata.fp = read_current_perf_counters; |
| 129 | cbdata.u[0].v = pt; |
| 130 | cbdata.u[1].v = vm; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 131 | |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 132 | if ((pm->current_event + 1) < vec_len (pm->single_events_to_collect)) |
| 133 | limit = 2; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 134 | |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 135 | for (i = 0; i < limit; i++) |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 136 | { |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 137 | c = vec_elt_at_index (pm->single_events_to_collect, |
| 138 | pm->current_event + i); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 139 | |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 140 | memset (&pe, 0, sizeof (struct perf_event_attr)); |
| 141 | pe.type = c->pe_type; |
| 142 | pe.size = sizeof (struct perf_event_attr); |
| 143 | pe.config = c->pe_config; |
| 144 | pe.disabled = 1; |
| 145 | pe.pinned = 1; |
| 146 | /* |
| 147 | * Note: excluding the kernel makes the |
| 148 | * (software) context-switch counter read 0... |
| 149 | */ |
| 150 | if (pe.type != PERF_TYPE_SOFTWARE) |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 151 | { |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 152 | /* Exclude kernel and hypervisor */ |
| 153 | pe.exclude_kernel = 1; |
| 154 | pe.exclude_hv = 1; |
| 155 | } |
| 156 | |
Damjan Marion | ee72141 | 2019-01-27 17:54:11 +0100 | [diff] [blame] | 157 | cpu = vm->cpu_id; |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 158 | |
| 159 | fd = perf_event_open (&pe, 0, cpu, -1, 0); |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 160 | if (fd == -1) |
| 161 | { |
| 162 | clib_unix_warning ("event open: type %d config %d", c->pe_type, |
| 163 | c->pe_config); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 164 | return; |
| 165 | } |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 166 | |
| 167 | if (pe.type != PERF_TYPE_SOFTWARE) |
| 168 | { |
| 169 | p = mmap (0, pm->page_size, PROT_READ, MAP_SHARED, fd, 0); |
| 170 | if (p == MAP_FAILED) |
| 171 | { |
| 172 | clib_unix_warning ("mmap"); |
| 173 | close (fd); |
| 174 | return; |
| 175 | } |
Benoît Ganne | 7176b80 | 2019-12-16 15:26:49 +0100 | [diff] [blame] | 176 | CLIB_MEM_UNPOISON (p, pm->page_size); |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 177 | } |
| 178 | else |
| 179 | p = 0; |
| 180 | |
Damjan Marion | 69a1d64 | 2019-01-24 20:24:33 +0100 | [diff] [blame] | 181 | if (ioctl (fd, PERF_EVENT_IOC_RESET, 0) < 0) |
| 182 | clib_unix_warning ("reset ioctl"); |
| 183 | |
| 184 | if (ioctl (fd, PERF_EVENT_IOC_ENABLE, 0) < 0) |
| 185 | clib_unix_warning ("enable ioctl"); |
| 186 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 187 | pt->perf_event_pages[i] = (void *) p; |
| 188 | pt->pm_fds[i] = fd; |
Su Wang | 7297f47 | 2019-03-21 00:14:14 -0400 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Hardware events must be all opened and enabled before aquiring |
| 193 | * pmc indices, otherwise the pmc indices might be out-dated. |
| 194 | */ |
| 195 | for (i = 0; i < limit; i++) |
| 196 | { |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 197 | p = (struct perf_event_mmap_page *) pt->perf_event_pages[i]; |
Su Wang | 7297f47 | 2019-03-21 00:14:14 -0400 | [diff] [blame] | 198 | |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 199 | /* |
| 200 | * Software event counters - and others not capable of being |
| 201 | * read via the "rdpmc" instruction - will be read |
| 202 | * by system calls. |
| 203 | */ |
Su Wang | 7297f47 | 2019-03-21 00:14:14 -0400 | [diff] [blame] | 204 | if (p == 0 || p->cap_user_rdpmc == 0) |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 205 | index = ~0; |
| 206 | else |
| 207 | index = p->index - 1; |
| 208 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 209 | pt->rdpmc_indices[i] = index; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 210 | } |
| 211 | |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 212 | pm->n_active = i; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 213 | /* Enable the main loop counter snapshot mechanism */ |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 214 | clib_callback_data_add (&vm->vlib_node_runtime_perf_callbacks, cbdata); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static void |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 218 | disable_events (perfmon_main_t * pm) |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 219 | { |
| 220 | vlib_main_t *vm = vlib_get_main (); |
| 221 | u32 my_thread_index = vm->thread_index; |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 222 | perfmon_thread_t *pt = pm->threads[my_thread_index]; |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 223 | int i; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 224 | |
| 225 | /* Stop main loop collection */ |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 226 | clib_callback_data_remove (&vm->vlib_node_runtime_perf_callbacks, |
| 227 | read_current_perf_counters); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 228 | |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 229 | for (i = 0; i < pm->n_active; i++) |
| 230 | { |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 231 | if (pt->pm_fds[i] == 0) |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 232 | continue; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 233 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 234 | if (ioctl (pt->pm_fds[i], PERF_EVENT_IOC_DISABLE, 0) < 0) |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 235 | clib_unix_warning ("disable ioctl"); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 236 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 237 | if (pt->perf_event_pages[i]) |
Benoît Ganne | 7176b80 | 2019-12-16 15:26:49 +0100 | [diff] [blame] | 238 | { |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 239 | if (munmap (pt->perf_event_pages[i], pm->page_size) < 0) |
Benoît Ganne | 7176b80 | 2019-12-16 15:26:49 +0100 | [diff] [blame] | 240 | clib_unix_warning ("munmap"); |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 241 | pt->perf_event_pages[i] = 0; |
Benoît Ganne | 7176b80 | 2019-12-16 15:26:49 +0100 | [diff] [blame] | 242 | } |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 243 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 244 | (void) close (pt->pm_fds[i]); |
| 245 | pt->pm_fds[i] = 0; |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 246 | } |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static void |
| 250 | worker_thread_start_event (vlib_main_t * vm) |
| 251 | { |
| 252 | perfmon_main_t *pm = &perfmon_main; |
| 253 | |
Dave Barach | 5f2cfb2 | 2019-05-20 10:28:57 -0400 | [diff] [blame] | 254 | clib_callback_enable_disable (vm->worker_thread_main_loop_callbacks, |
| 255 | vm->worker_thread_main_loop_callback_tmp, |
| 256 | vm->worker_thread_main_loop_callback_lock, |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 257 | worker_thread_start_event, 0 /* disable */ ); |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 258 | enable_current_events (pm); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | static void |
| 262 | worker_thread_stop_event (vlib_main_t * vm) |
| 263 | { |
| 264 | perfmon_main_t *pm = &perfmon_main; |
Dave Barach | 5f2cfb2 | 2019-05-20 10:28:57 -0400 | [diff] [blame] | 265 | clib_callback_enable_disable (vm->worker_thread_main_loop_callbacks, |
| 266 | vm->worker_thread_main_loop_callback_tmp, |
| 267 | vm->worker_thread_main_loop_callback_lock, |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 268 | worker_thread_stop_event, 0 /* disable */ ); |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 269 | disable_events (pm); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static void |
| 273 | start_event (perfmon_main_t * pm, f64 now, uword event_data) |
| 274 | { |
| 275 | int i; |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 276 | int last_set; |
| 277 | int all = 0; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 278 | pm->current_event = 0; |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 279 | |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 280 | if (vec_len (pm->single_events_to_collect) == 0) |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 281 | { |
| 282 | pm->state = PERFMON_STATE_OFF; |
| 283 | return; |
| 284 | } |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 285 | |
| 286 | last_set = clib_bitmap_last_set (pm->thread_bitmap); |
| 287 | all = (last_set == ~0); |
| 288 | |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 289 | pm->state = PERFMON_STATE_RUNNING; |
| 290 | clear_counters (pm); |
| 291 | |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 292 | /* Start collection on thread 0? */ |
| 293 | if (all || clib_bitmap_get (pm->thread_bitmap, 0)) |
| 294 | { |
| 295 | /* Start collection on this thread */ |
| 296 | enable_current_events (pm); |
| 297 | } |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 298 | |
| 299 | /* And also on worker threads */ |
| 300 | for (i = 1; i < vec_len (vlib_mains); i++) |
| 301 | { |
| 302 | if (vlib_mains[i] == 0) |
| 303 | continue; |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 304 | |
| 305 | if (all || clib_bitmap_get (pm->thread_bitmap, i)) |
Dave Barach | 5f2cfb2 | 2019-05-20 10:28:57 -0400 | [diff] [blame] | 306 | clib_callback_enable_disable |
| 307 | (vlib_mains[i]->worker_thread_main_loop_callbacks, |
| 308 | vlib_mains[i]->worker_thread_main_loop_callback_tmp, |
| 309 | vlib_mains[i]->worker_thread_main_loop_callback_lock, |
| 310 | (void *) worker_thread_start_event, 1 /* enable */ ); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | void |
| 315 | scrape_and_clear_counters (perfmon_main_t * pm) |
| 316 | { |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 317 | int i, j, k; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 318 | vlib_main_t *vm = pm->vlib_main; |
| 319 | vlib_main_t *stat_vm; |
| 320 | vlib_node_main_t *nm; |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 321 | perfmon_counters_t *ctr; |
| 322 | perfmon_counters_t *ctrs; |
| 323 | perfmon_counters_t **ctr_dups = 0; |
| 324 | perfmon_thread_t *pt; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 325 | perfmon_capture_t *c; |
| 326 | perfmon_event_config_t *current_event; |
| 327 | uword *p; |
| 328 | u8 *counter_name; |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 329 | u32 len; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 330 | |
| 331 | /* snapshoot the nodes, including pm counters */ |
| 332 | vlib_worker_thread_barrier_sync (vm); |
| 333 | |
| 334 | for (j = 0; j < vec_len (vlib_mains); j++) |
| 335 | { |
| 336 | stat_vm = vlib_mains[j]; |
| 337 | if (stat_vm == 0) |
| 338 | continue; |
| 339 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 340 | pt = pm->threads[j]; |
| 341 | len = vec_len (pt->counters); |
| 342 | ctrs = 0; |
| 343 | if (len) |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 344 | { |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 345 | vec_validate (ctrs, len - 1); |
| 346 | clib_memcpy (ctrs, pt->counters, len * sizeof (pt->counters[0])); |
| 347 | clib_memset (pt->counters, 0, len * sizeof (pt->counters[0])); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 348 | } |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 349 | vec_add1 (ctr_dups, ctrs); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | vlib_worker_thread_barrier_release (vm); |
| 353 | |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 354 | for (j = 0; j < vec_len (vlib_mains); j++) |
| 355 | { |
| 356 | stat_vm = vlib_mains[j]; |
| 357 | if (stat_vm == 0) |
| 358 | continue; |
| 359 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 360 | pt = pm->threads[j]; |
| 361 | ctrs = ctr_dups[j]; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 362 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 363 | for (i = 0; i < vec_len (ctrs); i++) |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 364 | { |
| 365 | u8 *capture_name; |
| 366 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 367 | ctr = &ctrs[i]; |
| 368 | nm = &stat_vm->node_main; |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 369 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 370 | if (ctr->ticks[0] == 0 && ctr->ticks[1] == 0) |
| 371 | continue; |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 372 | |
| 373 | for (k = 0; k < 2; k++) |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 374 | { |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 375 | /* |
| 376 | * We collect 2 counters at once, except for the |
| 377 | * last counter when the user asks for an odd number of |
| 378 | * counters |
| 379 | */ |
| 380 | if ((pm->current_event + k) |
| 381 | >= vec_len (pm->single_events_to_collect)) |
| 382 | break; |
| 383 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 384 | capture_name = format (0, "t%d-%v%c", j, nm->nodes[i]->name, 0); |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 385 | |
| 386 | p = hash_get_mem (pm->capture_by_thread_and_node_name, |
| 387 | capture_name); |
| 388 | |
| 389 | if (p == 0) |
| 390 | { |
| 391 | pool_get (pm->capture_pool, c); |
| 392 | memset (c, 0, sizeof (*c)); |
| 393 | c->thread_and_node_name = capture_name; |
| 394 | hash_set_mem (pm->capture_by_thread_and_node_name, |
| 395 | capture_name, c - pm->capture_pool); |
| 396 | } |
| 397 | else |
| 398 | { |
| 399 | c = pool_elt_at_index (pm->capture_pool, p[0]); |
| 400 | vec_free (capture_name); |
| 401 | } |
| 402 | |
| 403 | /* Snapshoot counters, etc. into the capture */ |
| 404 | current_event = pm->single_events_to_collect |
| 405 | + pm->current_event + k; |
| 406 | counter_name = (u8 *) current_event->name; |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 407 | |
| 408 | vec_add1 (c->counter_names, counter_name); |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 409 | vec_add1 (c->counter_values, ctr->ticks[k]); |
| 410 | vec_add1 (c->vectors_this_counter, ctr->vectors); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 411 | } |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 412 | } |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 413 | vec_free (ctrs); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 414 | } |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 415 | vec_free (ctr_dups); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static void |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 419 | handle_timeout (vlib_main_t * vm, perfmon_main_t * pm, f64 now) |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 420 | { |
| 421 | int i; |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 422 | int last_set, all; |
| 423 | |
| 424 | last_set = clib_bitmap_last_set (pm->thread_bitmap); |
| 425 | all = (last_set == ~0); |
| 426 | |
| 427 | if (all || clib_bitmap_get (pm->thread_bitmap, 0)) |
| 428 | disable_events (pm); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 429 | |
| 430 | /* And also on worker threads */ |
| 431 | for (i = 1; i < vec_len (vlib_mains); i++) |
| 432 | { |
| 433 | if (vlib_mains[i] == 0) |
| 434 | continue; |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 435 | if (all || clib_bitmap_get (pm->thread_bitmap, i)) |
Dave Barach | 5f2cfb2 | 2019-05-20 10:28:57 -0400 | [diff] [blame] | 436 | clib_callback_enable_disable |
| 437 | (vlib_mains[i]->worker_thread_main_loop_callbacks, |
| 438 | vlib_mains[i]->worker_thread_main_loop_callback_tmp, |
| 439 | vlib_mains[i]->worker_thread_main_loop_callback_lock, |
| 440 | (void *) worker_thread_stop_event, 1 /* enable */ ); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 441 | } |
| 442 | |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 443 | /* Make sure workers have stopped collection */ |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 444 | if (i > 1) |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 445 | { |
| 446 | f64 deadman = vlib_time_now (vm) + 1.0; |
| 447 | |
| 448 | for (i = 1; i < vec_len (vlib_mains); i++) |
| 449 | { |
| 450 | /* Has the worker actually stopped collecting data? */ |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 451 | while (clib_callback_data_is_set |
| 452 | (&vm->vlib_node_runtime_perf_callbacks, |
Dave Barach | 5f2cfb2 | 2019-05-20 10:28:57 -0400 | [diff] [blame] | 453 | read_current_perf_counters)) |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 454 | { |
| 455 | if (vlib_time_now (vm) > deadman) |
| 456 | { |
| 457 | clib_warning ("Thread %d deadman timeout!", i); |
| 458 | break; |
| 459 | } |
| 460 | vlib_process_suspend (pm->vlib_main, 1e-3); |
| 461 | } |
| 462 | } |
| 463 | } |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 464 | scrape_and_clear_counters (pm); |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 465 | pm->current_event += pm->n_active; |
| 466 | if (pm->current_event >= vec_len (pm->single_events_to_collect)) |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 467 | { |
| 468 | pm->current_event = 0; |
| 469 | pm->state = PERFMON_STATE_OFF; |
| 470 | return; |
| 471 | } |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 472 | |
| 473 | if (all || clib_bitmap_get (pm->thread_bitmap, 0)) |
| 474 | enable_current_events (pm); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 475 | |
| 476 | /* And also on worker threads */ |
| 477 | for (i = 1; i < vec_len (vlib_mains); i++) |
| 478 | { |
| 479 | if (vlib_mains[i] == 0) |
| 480 | continue; |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 481 | if (all || clib_bitmap_get (pm->thread_bitmap, i)) |
Dave Barach | 5f2cfb2 | 2019-05-20 10:28:57 -0400 | [diff] [blame] | 482 | clib_callback_enable_disable |
| 483 | (vlib_mains[i]->worker_thread_main_loop_callbacks, |
| 484 | vlib_mains[i]->worker_thread_main_loop_callback_tmp, |
| 485 | vlib_mains[i]->worker_thread_main_loop_callback_lock, |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 486 | worker_thread_start_event, 0 /* disable */ ); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
| 490 | static uword |
| 491 | perfmon_periodic_process (vlib_main_t * vm, |
| 492 | vlib_node_runtime_t * rt, vlib_frame_t * f) |
| 493 | { |
| 494 | perfmon_main_t *pm = &perfmon_main; |
| 495 | f64 now; |
| 496 | uword *event_data = 0; |
| 497 | uword event_type; |
| 498 | int i; |
| 499 | |
| 500 | while (1) |
| 501 | { |
| 502 | if (pm->state == PERFMON_STATE_RUNNING) |
| 503 | vlib_process_wait_for_event_or_clock (vm, pm->timeout_interval); |
| 504 | else |
| 505 | vlib_process_wait_for_event (vm); |
| 506 | |
| 507 | now = vlib_time_now (vm); |
| 508 | |
| 509 | event_type = vlib_process_get_events (vm, (uword **) & event_data); |
| 510 | |
| 511 | switch (event_type) |
| 512 | { |
| 513 | case PERFMON_START: |
| 514 | for (i = 0; i < vec_len (event_data); i++) |
| 515 | start_event (pm, now, event_data[i]); |
| 516 | break; |
| 517 | |
| 518 | /* Handle timeout */ |
| 519 | case ~0: |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 520 | handle_timeout (vm, pm, now); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 521 | break; |
| 522 | |
| 523 | default: |
| 524 | clib_warning ("Unexpected event %d", event_type); |
| 525 | break; |
| 526 | } |
| 527 | vec_reset_length (event_data); |
| 528 | } |
| 529 | return 0; /* or not */ |
| 530 | } |
| 531 | |
| 532 | /* *INDENT-OFF* */ |
| 533 | VLIB_REGISTER_NODE (perfmon_periodic_node) = |
| 534 | { |
| 535 | .function = perfmon_periodic_process, |
| 536 | .type = VLIB_NODE_TYPE_PROCESS, |
| 537 | .name = "perfmon-periodic-process", |
| 538 | }; |
| 539 | /* *INDENT-ON* */ |
| 540 | |
| 541 | /* |
| 542 | * fd.io coding-style-patch-verification: ON |
| 543 | * |
| 544 | * Local Variables: |
| 545 | * eval: (c-set-style "gnu") |
| 546 | * End: |
| 547 | */ |