Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 1 | /* |
| 2 | * perfmon.c - skeleton vpp engine plug-in |
| 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 <vnet/vnet.h> |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 19 | #include <perfmon/perfmon.h> |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 20 | #include <perfmon/perfmon_intel.h> |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 21 | |
| 22 | #include <vlibapi/api.h> |
| 23 | #include <vlibmemory/api.h> |
| 24 | #include <vpp/app/version.h> |
| 25 | #include <linux/limits.h> |
| 26 | |
| 27 | perfmon_main_t perfmon_main; |
| 28 | |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 29 | void |
| 30 | perfmon_register_intel_pmc (perfmon_intel_pmc_cpu_model_t * m, int n_models, |
| 31 | perfmon_intel_pmc_event_t * e, int n_events) |
Dave Barach | bef3619 | 2018-12-17 15:55:52 -0500 | [diff] [blame] | 32 | { |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 33 | perfmon_main_t *pm = &perfmon_main; |
| 34 | perfmon_intel_pmc_registration_t r; |
Dave Barach | bef3619 | 2018-12-17 15:55:52 -0500 | [diff] [blame] | 35 | |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 36 | r.events = e; |
| 37 | r.models = m; |
| 38 | r.n_events = n_events; |
| 39 | r.n_models = n_models; |
Dave Barach | bef3619 | 2018-12-17 15:55:52 -0500 | [diff] [blame] | 40 | |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 41 | vec_add1 (pm->perfmon_tables, r); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 42 | } |
| 43 | |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 44 | static inline u32 |
| 45 | get_cpuid (void) |
| 46 | { |
| 47 | #if defined(__x86_64__) |
| 48 | u32 cpuid; |
| 49 | asm volatile ("mov $1, %%eax; cpuid; mov %%eax, %0":"=r" (cpuid)::"%eax", |
| 50 | "%edx", "%ecx", "%rbx"); |
| 51 | return cpuid; |
| 52 | #else |
| 53 | return 0; |
| 54 | #endif |
| 55 | } |
| 56 | |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 57 | static int |
| 58 | perfmon_cpu_model_matches (perfmon_intel_pmc_cpu_model_t * mt, |
| 59 | u32 n_models, u8 model, u8 stepping) |
| 60 | { |
| 61 | u32 i; |
| 62 | for (i = 0; i < n_models; i++) |
| 63 | { |
| 64 | if (mt[i].model != model) |
| 65 | continue; |
| 66 | |
| 67 | if (mt[i].has_stepping) |
| 68 | { |
| 69 | if (mt[i].stepping != stepping) |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | return 1; |
| 74 | } |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static perfmon_intel_pmc_event_t * |
| 79 | perfmon_find_table_by_model_stepping (perfmon_main_t * pm, |
| 80 | u8 model, u8 stepping) |
| 81 | { |
| 82 | perfmon_intel_pmc_registration_t *rt; |
| 83 | |
| 84 | vec_foreach (rt, pm->perfmon_tables) |
| 85 | { |
| 86 | if (perfmon_cpu_model_matches (rt->models, rt->n_models, model, stepping)) |
| 87 | return rt->events; |
| 88 | } |
| 89 | return 0; |
| 90 | } |
| 91 | |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 92 | static clib_error_t * |
| 93 | perfmon_init (vlib_main_t * vm) |
| 94 | { |
| 95 | perfmon_main_t *pm = &perfmon_main; |
| 96 | clib_error_t *error = 0; |
| 97 | u32 cpuid; |
Dave Barach | bef3619 | 2018-12-17 15:55:52 -0500 | [diff] [blame] | 98 | u8 model, stepping; |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 99 | perfmon_intel_pmc_event_t *ev; |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 100 | int i; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 101 | |
| 102 | pm->vlib_main = vm; |
| 103 | pm->vnet_main = vnet_get_main (); |
| 104 | |
| 105 | pm->capture_by_thread_and_node_name = |
| 106 | hash_create_string (0, sizeof (uword)); |
| 107 | |
| 108 | pm->log_class = vlib_log_register_class ("perfmon", 0); |
| 109 | |
| 110 | /* Default data collection interval */ |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 111 | pm->timeout_interval = 2.0; /* seconds */ |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 112 | |
| 113 | vec_validate (pm->threads, vlib_get_thread_main ()->n_vlib_mains - 1); |
| 114 | for (i = 0; i < vec_len (pm->threads); i++) |
| 115 | { |
| 116 | perfmon_thread_t *pt = clib_mem_alloc_aligned |
| 117 | (sizeof (perfmon_thread_t), CLIB_CACHE_LINE_BYTES); |
| 118 | clib_memset (pt, 0, sizeof (*pt)); |
| 119 | pm->threads[i] = pt; |
| 120 | pt->pm_fds[0] = -1; |
| 121 | pt->pm_fds[1] = -1; |
| 122 | } |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 123 | pm->page_size = getpagesize (); |
| 124 | |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 125 | pm->perfmon_table = 0; |
| 126 | pm->pmc_event_by_name = 0; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 127 | |
| 128 | cpuid = get_cpuid (); |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 129 | model = ((cpuid >> 12) & 0xf0) | ((cpuid >> 4) & 0xf); |
| 130 | stepping = cpuid & 0xf; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 131 | |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 132 | pm->perfmon_table = perfmon_find_table_by_model_stepping (pm, |
| 133 | model, stepping); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 134 | |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 135 | if (pm->perfmon_table == 0) |
Dave Barach | bef3619 | 2018-12-17 15:55:52 -0500 | [diff] [blame] | 136 | { |
| 137 | vlib_log_err (pm->log_class, "No table for cpuid %x", cpuid); |
| 138 | vlib_log_err (pm->log_class, " model %x, stepping %x", |
| 139 | model, stepping); |
| 140 | } |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 141 | else |
| 142 | { |
| 143 | pm->pmc_event_by_name = hash_create_string (0, sizeof (u32)); |
| 144 | ev = pm->perfmon_table; |
| 145 | |
| 146 | for (; ev->event_name; ev++) |
| 147 | { |
| 148 | hash_set_mem (pm->pmc_event_by_name, ev->event_name, |
| 149 | ev - pm->perfmon_table); |
| 150 | } |
| 151 | } |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 152 | |
| 153 | return error; |
| 154 | } |
| 155 | |
| 156 | VLIB_INIT_FUNCTION (perfmon_init); |
| 157 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 158 | uword |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 159 | unformat_processor_event (unformat_input_t * input, va_list * args) |
| 160 | { |
| 161 | perfmon_main_t *pm = va_arg (*args, perfmon_main_t *); |
| 162 | perfmon_event_config_t *ep = va_arg (*args, perfmon_event_config_t *); |
| 163 | u8 *s = 0; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 164 | hash_pair_t *hp; |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 165 | u32 idx; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 166 | u32 pe_config = 0; |
| 167 | |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 168 | if (pm->perfmon_table == 0 || pm->pmc_event_by_name == 0) |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 169 | return 0; |
| 170 | |
| 171 | if (!unformat (input, "%s", &s)) |
| 172 | return 0; |
| 173 | |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 174 | hp = hash_get_pair_mem (pm->pmc_event_by_name, s); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 175 | |
| 176 | vec_free (s); |
| 177 | |
| 178 | if (hp == 0) |
| 179 | return 0; |
| 180 | |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 181 | idx = (u32) (hp->value[0]); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 182 | |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 183 | pe_config |= pm->perfmon_table[idx].event_code[0]; |
| 184 | pe_config |= pm->perfmon_table[idx].umask << 8; |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 185 | pe_config |= pm->perfmon_table[idx].edge << 18; |
| 186 | pe_config |= pm->perfmon_table[idx].anyt << 21; |
| 187 | pe_config |= pm->perfmon_table[idx].inv << 23; |
| 188 | pe_config |= pm->perfmon_table[idx].cmask << 24; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 189 | |
| 190 | ep->name = (char *) hp->key; |
| 191 | ep->pe_type = PERF_TYPE_RAW; |
| 192 | ep->pe_config = pe_config; |
| 193 | return 1; |
| 194 | } |
| 195 | |
| 196 | static clib_error_t * |
| 197 | set_pmc_command_fn (vlib_main_t * vm, |
| 198 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 199 | { |
| 200 | perfmon_main_t *pm = &perfmon_main; |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 201 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
| 202 | int num_threads = 1 + vtm->n_threads; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 203 | unformat_input_t _line_input, *line_input = &_line_input; |
| 204 | perfmon_event_config_t ec; |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 205 | f64 delay; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 206 | u32 timeout_seconds; |
| 207 | u32 deadman; |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 208 | int last_set; |
| 209 | clib_error_t *error; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 210 | |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 211 | vec_reset_length (pm->single_events_to_collect); |
| 212 | vec_reset_length (pm->paired_events_to_collect); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 213 | pm->ipc_event_index = ~0; |
| 214 | pm->mispredict_event_index = ~0; |
| 215 | |
| 216 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 217 | return clib_error_return (0, "counter names required..."); |
| 218 | |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 219 | clib_bitmap_zero (pm->thread_bitmap); |
| 220 | |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 221 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 222 | { |
| 223 | if (unformat (line_input, "timeout %u", &timeout_seconds)) |
| 224 | pm->timeout_interval = (f64) timeout_seconds; |
| 225 | else if (unformat (line_input, "instructions-per-clock")) |
| 226 | { |
| 227 | ec.name = "instructions"; |
| 228 | ec.pe_type = PERF_TYPE_HARDWARE; |
| 229 | ec.pe_config = PERF_COUNT_HW_INSTRUCTIONS; |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 230 | pm->ipc_event_index = vec_len (pm->paired_events_to_collect); |
| 231 | vec_add1 (pm->paired_events_to_collect, ec); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 232 | ec.name = "cpu-cycles"; |
| 233 | ec.pe_type = PERF_TYPE_HARDWARE; |
| 234 | ec.pe_config = PERF_COUNT_HW_CPU_CYCLES; |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 235 | vec_add1 (pm->paired_events_to_collect, ec); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 236 | } |
| 237 | else if (unformat (line_input, "branch-mispredict-rate")) |
| 238 | { |
| 239 | ec.name = "branch-misses"; |
| 240 | ec.pe_type = PERF_TYPE_HARDWARE; |
| 241 | ec.pe_config = PERF_COUNT_HW_BRANCH_MISSES; |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 242 | pm->mispredict_event_index = vec_len (pm->paired_events_to_collect); |
| 243 | vec_add1 (pm->paired_events_to_collect, ec); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 244 | ec.name = "branches"; |
| 245 | ec.pe_type = PERF_TYPE_HARDWARE; |
| 246 | ec.pe_config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS; |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 247 | vec_add1 (pm->paired_events_to_collect, ec); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 248 | } |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 249 | else if (unformat (line_input, "threads %U", |
| 250 | unformat_bitmap_list, &pm->thread_bitmap)) |
| 251 | ; |
| 252 | else if (unformat (line_input, "thread %U", |
| 253 | unformat_bitmap_list, &pm->thread_bitmap)) |
| 254 | ; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 255 | else if (unformat (line_input, "%U", unformat_processor_event, pm, &ec)) |
| 256 | { |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 257 | vec_add1 (pm->single_events_to_collect, ec); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 258 | } |
| 259 | #define _(type,event,str) \ |
| 260 | else if (unformat (line_input, str)) \ |
| 261 | { \ |
| 262 | ec.name = str; \ |
| 263 | ec.pe_type = type; \ |
| 264 | ec.pe_config = event; \ |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 265 | vec_add1 (pm->single_events_to_collect, ec); \ |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 266 | } |
| 267 | foreach_perfmon_event |
| 268 | #undef _ |
| 269 | else |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 270 | { |
| 271 | error = clib_error_return (0, "unknown input '%U'", |
| 272 | format_unformat_error, line_input); |
| 273 | unformat_free (line_input); |
| 274 | return error; |
| 275 | } |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 276 | } |
| 277 | |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 278 | unformat_free (line_input); |
| 279 | |
| 280 | last_set = clib_bitmap_last_set (pm->thread_bitmap); |
| 281 | if (last_set != ~0 && last_set >= num_threads) |
| 282 | return clib_error_return (0, "thread %d does not exist", last_set); |
| 283 | |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 284 | /* Stick paired events at the front of the (unified) list */ |
| 285 | if (vec_len (pm->paired_events_to_collect) > 0) |
| 286 | { |
| 287 | perfmon_event_config_t *tmp; |
| 288 | /* first 2n events are pairs... */ |
| 289 | vec_append (pm->paired_events_to_collect, pm->single_events_to_collect); |
| 290 | tmp = pm->single_events_to_collect; |
| 291 | pm->single_events_to_collect = pm->paired_events_to_collect; |
| 292 | pm->paired_events_to_collect = tmp; |
| 293 | } |
| 294 | |
| 295 | if (vec_len (pm->single_events_to_collect) == 0) |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 296 | return clib_error_return (0, "no events specified..."); |
| 297 | |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 298 | /* Figure out how long data collection will take */ |
| 299 | delay = |
| 300 | ((f64) vec_len (pm->single_events_to_collect)) * pm->timeout_interval; |
| 301 | delay /= 2.0; /* collect 2 stats at once */ |
| 302 | |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 303 | vlib_cli_output (vm, "Start collection for %d events, wait %.2f seconds", |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 304 | vec_len (pm->single_events_to_collect), delay); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 305 | |
| 306 | vlib_process_signal_event (pm->vlib_main, perfmon_periodic_node.index, |
| 307 | PERFMON_START, 0); |
| 308 | |
| 309 | /* Coarse-grained wait */ |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 310 | vlib_process_suspend (vm, delay); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 311 | |
| 312 | deadman = 0; |
| 313 | /* Reasonable to guess that collection may not be quite done... */ |
| 314 | while (pm->state == PERFMON_STATE_RUNNING) |
| 315 | { |
| 316 | vlib_process_suspend (vm, 10e-3); |
| 317 | if (deadman++ > 200) |
| 318 | { |
| 319 | vlib_cli_output (vm, "DEADMAN: collection still running..."); |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | vlib_cli_output (vm, "Data collection complete..."); |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | /* *INDENT-OFF* */ |
| 329 | VLIB_CLI_COMMAND (set_pmc_command, static) = |
| 330 | { |
| 331 | .path = "set pmc", |
Dave Barach | 53fe4a7 | 2019-01-26 09:50:26 -0500 | [diff] [blame] | 332 | .short_help = "set pmc [threads n,n1-n2] c1... [see \"show pmc events\"]", |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 333 | .function = set_pmc_command_fn, |
| 334 | .is_mp_safe = 1, |
| 335 | }; |
| 336 | /* *INDENT-ON* */ |
| 337 | |
| 338 | static int |
| 339 | capture_name_sort (void *a1, void *a2) |
| 340 | { |
| 341 | perfmon_capture_t *c1 = a1; |
| 342 | perfmon_capture_t *c2 = a2; |
| 343 | |
| 344 | return strcmp ((char *) c1->thread_and_node_name, |
| 345 | (char *) c2->thread_and_node_name); |
| 346 | } |
| 347 | |
| 348 | static u8 * |
| 349 | format_capture (u8 * s, va_list * args) |
| 350 | { |
| 351 | perfmon_main_t *pm = va_arg (*args, perfmon_main_t *); |
| 352 | perfmon_capture_t *c = va_arg (*args, perfmon_capture_t *); |
| 353 | int verbose __attribute__ ((unused)) = va_arg (*args, int); |
| 354 | f64 ticks_per_pkt; |
| 355 | int i; |
| 356 | |
| 357 | if (c == 0) |
| 358 | { |
| 359 | s = format (s, "%=40s%=20s%=16s%=16s%=16s", |
| 360 | "Name", "Counter", "Count", "Pkts", "Counts/Pkt"); |
| 361 | return s; |
| 362 | } |
| 363 | |
| 364 | for (i = 0; i < vec_len (c->counter_names); i++) |
| 365 | { |
| 366 | u8 *name; |
| 367 | |
| 368 | if (i == 0) |
| 369 | name = c->thread_and_node_name; |
| 370 | else |
| 371 | { |
| 372 | vec_add1 (s, '\n'); |
| 373 | name = (u8 *) ""; |
| 374 | } |
| 375 | |
| 376 | /* Deal with synthetic events right here */ |
| 377 | if (i == pm->ipc_event_index) |
| 378 | { |
| 379 | f64 ipc_rate; |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 380 | ASSERT ((i + 1) < vec_len (c->counter_names)); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 381 | |
| 382 | if (c->counter_values[i + 1] > 0) |
| 383 | ipc_rate = (f64) c->counter_values[i] |
| 384 | / (f64) c->counter_values[i + 1]; |
| 385 | else |
| 386 | ipc_rate = 0.0; |
| 387 | |
| 388 | s = format (s, "%-40s%+20s%+16llu%+16llu%+16.2e\n", |
| 389 | name, "instructions-per-clock", |
| 390 | c->counter_values[i], |
| 391 | c->counter_values[i + 1], ipc_rate); |
| 392 | name = (u8 *) ""; |
| 393 | } |
| 394 | |
| 395 | if (i == pm->mispredict_event_index) |
| 396 | { |
| 397 | f64 mispredict_rate; |
| 398 | ASSERT (i + 1 < vec_len (c->counter_names)); |
| 399 | |
| 400 | if (c->counter_values[i + 1] > 0) |
| 401 | mispredict_rate = (f64) c->counter_values[i] |
| 402 | / (f64) c->counter_values[i + 1]; |
| 403 | else |
| 404 | mispredict_rate = 0.0; |
| 405 | |
| 406 | s = format (s, "%-40s%+20s%+16llu%+16llu%+16.2e\n", |
| 407 | name, "branch-mispredict-rate", |
| 408 | c->counter_values[i], |
| 409 | c->counter_values[i + 1], mispredict_rate); |
| 410 | name = (u8 *) ""; |
| 411 | } |
| 412 | |
| 413 | if (c->vectors_this_counter[i]) |
| 414 | ticks_per_pkt = |
| 415 | ((f64) c->counter_values[i]) / ((f64) c->vectors_this_counter[i]); |
| 416 | else |
| 417 | ticks_per_pkt = 0.0; |
| 418 | |
| 419 | s = format (s, "%-40s%+20s%+16llu%+16llu%+16.2e", |
| 420 | name, c->counter_names[i], |
| 421 | c->counter_values[i], |
| 422 | c->vectors_this_counter[i], ticks_per_pkt); |
| 423 | } |
| 424 | return s; |
| 425 | } |
| 426 | |
| 427 | static u8 * |
| 428 | format_generic_events (u8 * s, va_list * args) |
| 429 | { |
| 430 | int verbose = va_arg (*args, int); |
| 431 | |
| 432 | #define _(type,config,name) \ |
| 433 | if (verbose == 0) \ |
| 434 | s = format (s, "\n %s", name); \ |
| 435 | else \ |
| 436 | s = format (s, "\n %s (%d, %d)", name, type, config); |
| 437 | foreach_perfmon_event; |
| 438 | #undef _ |
| 439 | return s; |
| 440 | } |
| 441 | |
| 442 | typedef struct |
| 443 | { |
| 444 | u8 *name; |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 445 | u32 index; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 446 | } sort_nvp_t; |
| 447 | |
| 448 | static int |
| 449 | sort_nvps_by_name (void *a1, void *a2) |
| 450 | { |
| 451 | sort_nvp_t *nvp1 = a1; |
| 452 | sort_nvp_t *nvp2 = a2; |
| 453 | |
| 454 | return strcmp ((char *) nvp1->name, (char *) nvp2->name); |
| 455 | } |
| 456 | |
| 457 | static u8 * |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 458 | format_pmc_event (u8 * s, va_list * args) |
| 459 | { |
| 460 | perfmon_intel_pmc_event_t *ev = va_arg (*args, perfmon_intel_pmc_event_t *); |
| 461 | |
| 462 | s = format (s, "%s\n", ev->event_name); |
| 463 | s = format (s, " umask: 0x%x\n", ev->umask); |
| 464 | s = format (s, " code: 0x%x", ev->event_code[0]); |
| 465 | |
| 466 | if (ev->event_code[1]) |
| 467 | s = format (s, " , 0x%x\n", ev->event_code[1]); |
| 468 | else |
| 469 | s = format (s, "\n"); |
| 470 | |
| 471 | return s; |
| 472 | } |
| 473 | |
| 474 | static u8 * |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 475 | format_processor_events (u8 * s, va_list * args) |
| 476 | { |
| 477 | perfmon_main_t *pm = va_arg (*args, perfmon_main_t *); |
| 478 | int verbose = va_arg (*args, int); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 479 | sort_nvp_t *sort_nvps = 0; |
| 480 | sort_nvp_t *sn; |
| 481 | u8 *key; |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 482 | u32 value; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 483 | |
| 484 | /* *INDENT-OFF* */ |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 485 | hash_foreach_mem (key, value, pm->pmc_event_by_name, |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 486 | ({ |
| 487 | vec_add2 (sort_nvps, sn, 1); |
| 488 | sn->name = key; |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 489 | sn->index = value; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 490 | })); |
| 491 | |
| 492 | vec_sort_with_function (sort_nvps, sort_nvps_by_name); |
| 493 | |
| 494 | if (verbose == 0) |
| 495 | { |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 496 | vec_foreach (sn, sort_nvps) |
| 497 | s = format (s, "\n %s ", sn->name); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 498 | } |
| 499 | else |
| 500 | { |
Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 501 | vec_foreach (sn, sort_nvps) |
| 502 | s = format(s, "%U", format_pmc_event, &pm->perfmon_table[sn->index]); |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 503 | } |
| 504 | vec_free (sort_nvps); |
| 505 | return s; |
| 506 | } |
| 507 | |
| 508 | |
| 509 | static clib_error_t * |
| 510 | show_pmc_command_fn (vlib_main_t * vm, |
| 511 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 512 | { |
| 513 | perfmon_main_t *pm = &perfmon_main; |
| 514 | int verbose = 0; |
| 515 | int events = 0; |
| 516 | int i; |
| 517 | perfmon_capture_t *c; |
| 518 | perfmon_capture_t *captures = 0; |
| 519 | |
| 520 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 521 | { |
| 522 | if (unformat (input, "events")) |
| 523 | events = 1; |
| 524 | else if (unformat (input, "verbose")) |
| 525 | verbose = 1; |
| 526 | else |
| 527 | break; |
| 528 | } |
| 529 | |
| 530 | if (events) |
| 531 | { |
| 532 | vlib_cli_output (vm, "Generic Events %U", |
| 533 | format_generic_events, verbose); |
| 534 | vlib_cli_output (vm, "Synthetic Events"); |
| 535 | vlib_cli_output (vm, " instructions-per-clock"); |
| 536 | vlib_cli_output (vm, " branch-mispredict-rate"); |
| 537 | if (pm->perfmon_table) |
| 538 | vlib_cli_output (vm, "Processor Events %U", |
| 539 | format_processor_events, pm, verbose); |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | if (pm->state == PERFMON_STATE_RUNNING) |
| 544 | { |
| 545 | vlib_cli_output (vm, "Data collection in progress..."); |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | if (pool_elts (pm->capture_pool) == 0) |
| 550 | { |
| 551 | vlib_cli_output (vm, "No data..."); |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 556 | pool_foreach (c, pm->capture_pool) |
| 557 | { |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 558 | vec_add1 (captures, *c); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 559 | } |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 560 | /* *INDENT-ON* */ |
| 561 | |
| 562 | vec_sort_with_function (captures, capture_name_sort); |
| 563 | |
| 564 | vlib_cli_output (vm, "%U", format_capture, pm, 0 /* header */ , |
| 565 | 0 /* verbose */ ); |
| 566 | |
| 567 | for (i = 0; i < vec_len (captures); i++) |
| 568 | { |
| 569 | c = captures + i; |
| 570 | |
| 571 | vlib_cli_output (vm, "%U", format_capture, pm, c, verbose); |
| 572 | } |
| 573 | |
| 574 | vec_free (captures); |
| 575 | |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | /* *INDENT-OFF* */ |
| 580 | VLIB_CLI_COMMAND (show_pmc_command, static) = |
| 581 | { |
| 582 | .path = "show pmc", |
| 583 | .short_help = "show pmc [verbose]", |
| 584 | .function = show_pmc_command_fn, |
| 585 | .is_mp_safe = 1, |
| 586 | }; |
| 587 | /* *INDENT-ON* */ |
| 588 | |
| 589 | static clib_error_t * |
| 590 | clear_pmc_command_fn (vlib_main_t * vm, |
| 591 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 592 | { |
| 593 | perfmon_main_t *pm = &perfmon_main; |
| 594 | u8 *key; |
| 595 | u32 *value; |
| 596 | |
| 597 | if (pm->state == PERFMON_STATE_RUNNING) |
| 598 | { |
| 599 | vlib_cli_output (vm, "Performance monitor is still running..."); |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | pool_free (pm->capture_pool); |
| 604 | |
| 605 | /* *INDENT-OFF* */ |
| 606 | hash_foreach_mem (key, value, pm->capture_by_thread_and_node_name, |
| 607 | ({ |
| 608 | vec_free (key); |
| 609 | })); |
| 610 | /* *INDENT-ON* */ |
| 611 | hash_free (pm->capture_by_thread_and_node_name); |
| 612 | pm->capture_by_thread_and_node_name = |
| 613 | hash_create_string (0, sizeof (uword)); |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | /* *INDENT-OFF* */ |
| 618 | VLIB_CLI_COMMAND (clear_pmc_command, static) = |
| 619 | { |
| 620 | .path = "clear pmc", |
| 621 | .short_help = "clear the performance monitor counters", |
| 622 | .function = clear_pmc_command_fn, |
| 623 | }; |
| 624 | /* *INDENT-ON* */ |
| 625 | |
| 626 | |
| 627 | /* |
| 628 | * fd.io coding-style-patch-verification: ON |
| 629 | * |
| 630 | * Local Variables: |
| 631 | * eval: (c-set-style "gnu") |
| 632 | * End: |
| 633 | */ |