1 // SPDX-License-Identifier: GPL-2.0 2 #include <dirent.h> 3 #include <errno.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include <fcntl.h> 8 #include <sys/param.h> 9 #include <unistd.h> 10 11 #include <api/fs/tracing_path.h> 12 #include <api/io.h> 13 #include <linux/stddef.h> 14 #include <linux/perf_event.h> 15 #include <linux/zalloc.h> 16 #include <subcmd/pager.h> 17 18 #include "build-id.h" 19 #include "debug.h" 20 #include "evsel.h" 21 #include "metricgroup.h" 22 #include "parse-events.h" 23 #include "pmu.h" 24 #include "pmus.h" 25 #include "print-events.h" 26 #include "probe-file.h" 27 #include "string2.h" 28 #include "strlist.h" 29 #include "tracepoint.h" 30 #include "pfm.h" 31 #include "thread_map.h" 32 #include "tool_pmu.h" 33 #include "util.h" 34 35 #define MAX_NAME_LEN 100 36 37 /** Strings corresponding to enum perf_type_id. */ 38 static const char * const event_type_descriptors[] = { 39 "Hardware event", 40 "Software event", 41 "Tracepoint event", 42 "Hardware cache event", 43 "Raw event descriptor", 44 "Hardware breakpoint", 45 }; 46 47 void print_sdt_events(const struct print_callbacks *print_cb, void *print_state) 48 { 49 struct strlist *bidlist, *sdtlist; 50 struct str_node *bid_nd, *sdt_name, *next_sdt_name; 51 const char *last_sdt_name = NULL; 52 53 /* 54 * The implicitly sorted sdtlist will hold the tracepoint name followed 55 * by @<buildid>. If the tracepoint name is unique (determined by 56 * looking at the adjacent nodes) the @<buildid> is dropped otherwise 57 * the executable path and buildid are added to the name. 58 */ 59 sdtlist = strlist__new(NULL, NULL); 60 if (!sdtlist) { 61 pr_debug("Failed to allocate new strlist for SDT\n"); 62 return; 63 } 64 bidlist = build_id_cache__list_all(true); 65 if (!bidlist) { 66 pr_debug("Failed to get buildids: %d\n", errno); 67 return; 68 } 69 strlist__for_each_entry(bid_nd, bidlist) { 70 struct probe_cache *pcache; 71 struct probe_cache_entry *ent; 72 73 pcache = probe_cache__new(bid_nd->s, NULL); 74 if (!pcache) 75 continue; 76 list_for_each_entry(ent, &pcache->entries, node) { 77 char buf[1024]; 78 79 snprintf(buf, sizeof(buf), "%s:%s@%s", 80 ent->pev.group, ent->pev.event, bid_nd->s); 81 strlist__add(sdtlist, buf); 82 } 83 probe_cache__delete(pcache); 84 } 85 strlist__delete(bidlist); 86 87 strlist__for_each_entry(sdt_name, sdtlist) { 88 bool show_detail = false; 89 char *bid = strchr(sdt_name->s, '@'); 90 char *evt_name = NULL; 91 92 if (bid) 93 *(bid++) = '\0'; 94 95 if (last_sdt_name && !strcmp(last_sdt_name, sdt_name->s)) { 96 show_detail = true; 97 } else { 98 next_sdt_name = strlist__next(sdt_name); 99 if (next_sdt_name) { 100 char *bid2 = strchr(next_sdt_name->s, '@'); 101 102 if (bid2) 103 *bid2 = '\0'; 104 if (strcmp(sdt_name->s, next_sdt_name->s) == 0) 105 show_detail = true; 106 if (bid2) 107 *bid2 = '@'; 108 } 109 } 110 last_sdt_name = sdt_name->s; 111 112 if (show_detail) { 113 char *path = build_id_cache__origname(bid); 114 115 if (path) { 116 if (asprintf(&evt_name, "%s@%s(%.12s)", sdt_name->s, path, bid) < 0) 117 evt_name = NULL; 118 free(path); 119 } 120 } 121 print_cb->print_event(print_state, 122 /*topic=*/NULL, 123 /*pmu_name=*/NULL, 124 PERF_TYPE_TRACEPOINT, 125 evt_name ?: sdt_name->s, 126 /*event_alias=*/NULL, 127 /*deprecated=*/false, 128 /*scale_unit=*/NULL, 129 "SDT event", 130 /*desc=*/NULL, 131 /*long_desc=*/NULL, 132 /*encoding_desc=*/NULL); 133 134 free(evt_name); 135 } 136 strlist__delete(sdtlist); 137 } 138 139 bool is_event_supported(u8 type, u64 config) 140 { 141 bool ret = true; 142 struct evsel *evsel; 143 struct perf_event_attr attr = { 144 .type = type, 145 .config = config, 146 .disabled = 1, 147 }; 148 struct perf_thread_map *tmap = thread_map__new_by_tid(0); 149 150 if (tmap == NULL) 151 return false; 152 153 evsel = evsel__new(&attr); 154 if (evsel) { 155 ret = evsel__open(evsel, NULL, tmap) >= 0; 156 157 if (!ret) { 158 /* 159 * The event may fail to open if the paranoid value 160 * /proc/sys/kernel/perf_event_paranoid is set to 2 161 * Re-run with exclude_kernel set; we don't do that by 162 * default as some ARM machines do not support it. 163 */ 164 evsel->core.attr.exclude_kernel = 1; 165 ret = evsel__open(evsel, NULL, tmap) >= 0; 166 } 167 168 if (!ret) { 169 /* 170 * The event may fail to open if the PMU requires 171 * exclude_guest to be set (e.g. as the Apple M1 PMU 172 * requires). 173 * Re-run with exclude_guest set; we don't do that by 174 * default as it's equally legitimate for another PMU 175 * driver to require that exclude_guest is clear. 176 */ 177 evsel->core.attr.exclude_guest = 1; 178 ret = evsel__open(evsel, NULL, tmap) >= 0; 179 } 180 181 evsel__close(evsel); 182 evsel__delete(evsel); 183 } 184 185 perf_thread_map__put(tmap); 186 return ret; 187 } 188 189 int print_hwcache_events(const struct print_callbacks *print_cb, void *print_state) 190 { 191 struct perf_pmu *pmu = NULL; 192 const char *event_type_descriptor = event_type_descriptors[PERF_TYPE_HW_CACHE]; 193 194 /* 195 * Only print core PMUs, skipping uncore for performance and 196 * PERF_TYPE_SOFTWARE that can succeed in opening legacy cache evenst. 197 */ 198 while ((pmu = perf_pmus__scan_core(pmu)) != NULL) { 199 if (pmu->is_uncore || pmu->type == PERF_TYPE_SOFTWARE) 200 continue; 201 202 for (int type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { 203 for (int op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { 204 /* skip invalid cache type */ 205 if (!evsel__is_cache_op_valid(type, op)) 206 continue; 207 208 for (int res = 0; res < PERF_COUNT_HW_CACHE_RESULT_MAX; res++) { 209 char name[64]; 210 char alias_name[128]; 211 __u64 config; 212 int ret; 213 214 __evsel__hw_cache_type_op_res_name(type, op, res, 215 name, sizeof(name)); 216 217 ret = parse_events__decode_legacy_cache(name, pmu->type, 218 &config); 219 if (ret || !is_event_supported(PERF_TYPE_HW_CACHE, config)) 220 continue; 221 snprintf(alias_name, sizeof(alias_name), "%s/%s/", 222 pmu->name, name); 223 print_cb->print_event(print_state, 224 "cache", 225 pmu->name, 226 pmu->type, 227 name, 228 alias_name, 229 /*scale_unit=*/NULL, 230 /*deprecated=*/false, 231 event_type_descriptor, 232 /*desc=*/NULL, 233 /*long_desc=*/NULL, 234 /*encoding_desc=*/NULL); 235 } 236 } 237 } 238 } 239 return 0; 240 } 241 242 void print_symbol_events(const struct print_callbacks *print_cb, void *print_state, 243 unsigned int type, const struct event_symbol *syms, 244 unsigned int max) 245 { 246 struct strlist *evt_name_list = strlist__new(NULL, NULL); 247 struct str_node *nd; 248 249 if (!evt_name_list) { 250 pr_debug("Failed to allocate new strlist for symbol events\n"); 251 return; 252 } 253 for (unsigned int i = 0; i < max; i++) { 254 /* 255 * New attr.config still not supported here, the latest 256 * example was PERF_COUNT_SW_CGROUP_SWITCHES 257 */ 258 if (syms[i].symbol == NULL) 259 continue; 260 261 if (!is_event_supported(type, i)) 262 continue; 263 264 if (strlen(syms[i].alias)) { 265 char name[MAX_NAME_LEN]; 266 267 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms[i].symbol, syms[i].alias); 268 strlist__add(evt_name_list, name); 269 } else 270 strlist__add(evt_name_list, syms[i].symbol); 271 } 272 273 strlist__for_each_entry(nd, evt_name_list) { 274 char *alias = strstr(nd->s, " OR "); 275 276 if (alias) { 277 *alias = '\0'; 278 alias += 4; 279 } 280 print_cb->print_event(print_state, 281 /*topic=*/NULL, 282 /*pmu_name=*/NULL, 283 type, 284 nd->s, 285 alias, 286 /*scale_unit=*/NULL, 287 /*deprecated=*/false, 288 event_type_descriptors[type], 289 /*desc=*/NULL, 290 /*long_desc=*/NULL, 291 /*encoding_desc=*/NULL); 292 } 293 strlist__delete(evt_name_list); 294 } 295 296 /** struct mep - RB-tree node for building printing information. */ 297 struct mep { 298 /** nd - RB-tree element. */ 299 struct rb_node nd; 300 /** @metric_group: Owned metric group name, separated others with ';'. */ 301 char *metric_group; 302 const char *metric_name; 303 const char *metric_desc; 304 const char *metric_long_desc; 305 const char *metric_expr; 306 const char *metric_threshold; 307 const char *metric_unit; 308 const char *pmu_name; 309 }; 310 311 static int mep_cmp(struct rb_node *rb_node, const void *entry) 312 { 313 struct mep *a = container_of(rb_node, struct mep, nd); 314 struct mep *b = (struct mep *)entry; 315 int ret; 316 317 ret = strcmp(a->metric_group, b->metric_group); 318 if (ret) 319 return ret; 320 321 return strcmp(a->metric_name, b->metric_name); 322 } 323 324 static struct rb_node *mep_new(struct rblist *rl __maybe_unused, const void *entry) 325 { 326 struct mep *me = malloc(sizeof(struct mep)); 327 328 if (!me) 329 return NULL; 330 331 memcpy(me, entry, sizeof(struct mep)); 332 return &me->nd; 333 } 334 335 static void mep_delete(struct rblist *rl __maybe_unused, 336 struct rb_node *nd) 337 { 338 struct mep *me = container_of(nd, struct mep, nd); 339 340 zfree(&me->metric_group); 341 free(me); 342 } 343 344 static struct mep *mep_lookup(struct rblist *groups, const char *metric_group, 345 const char *metric_name) 346 { 347 struct rb_node *nd; 348 struct mep me = { 349 .metric_group = strdup(metric_group), 350 .metric_name = metric_name, 351 }; 352 nd = rblist__find(groups, &me); 353 if (nd) { 354 free(me.metric_group); 355 return container_of(nd, struct mep, nd); 356 } 357 rblist__add_node(groups, &me); 358 nd = rblist__find(groups, &me); 359 if (nd) 360 return container_of(nd, struct mep, nd); 361 return NULL; 362 } 363 364 static int metricgroup__add_to_mep_groups_callback(const struct pmu_metric *pm, 365 const struct pmu_metrics_table *table __maybe_unused, 366 void *vdata) 367 { 368 struct rblist *groups = vdata; 369 const char *g; 370 char *omg, *mg; 371 372 mg = strdup(pm->metric_group ?: pm->metric_name); 373 if (!mg) 374 return -ENOMEM; 375 omg = mg; 376 while ((g = strsep(&mg, ";")) != NULL) { 377 struct mep *me; 378 379 g = skip_spaces(g); 380 if (strlen(g)) 381 me = mep_lookup(groups, g, pm->metric_name); 382 else 383 me = mep_lookup(groups, pm->metric_name, pm->metric_name); 384 385 if (me) { 386 me->metric_desc = pm->desc; 387 me->metric_long_desc = pm->long_desc; 388 me->metric_expr = pm->metric_expr; 389 me->metric_threshold = pm->metric_threshold; 390 me->metric_unit = pm->unit; 391 me->pmu_name = pm->pmu; 392 } 393 } 394 free(omg); 395 396 return 0; 397 } 398 399 void metricgroup__print(const struct print_callbacks *print_cb, void *print_state) 400 { 401 struct rblist groups; 402 struct rb_node *node, *next; 403 const struct pmu_metrics_table *table = pmu_metrics_table__find(); 404 405 rblist__init(&groups); 406 groups.node_new = mep_new; 407 groups.node_cmp = mep_cmp; 408 groups.node_delete = mep_delete; 409 410 metricgroup__for_each_metric(table, metricgroup__add_to_mep_groups_callback, &groups); 411 412 for (node = rb_first_cached(&groups.entries); node; node = next) { 413 struct mep *me = container_of(node, struct mep, nd); 414 415 print_cb->print_metric(print_state, 416 me->metric_group, 417 me->metric_name, 418 me->metric_desc, 419 me->metric_long_desc, 420 me->metric_expr, 421 me->metric_threshold, 422 me->metric_unit, 423 me->pmu_name); 424 next = rb_next(node); 425 rblist__remove_node(&groups, node); 426 } 427 } 428 429 /* 430 * Print the help text for the event symbols: 431 */ 432 void print_events(const struct print_callbacks *print_cb, void *print_state) 433 { 434 print_symbol_events(print_cb, print_state, PERF_TYPE_HARDWARE, 435 event_symbols_hw, PERF_COUNT_HW_MAX); 436 437 print_hwcache_events(print_cb, print_state); 438 439 perf_pmus__print_pmu_events(print_cb, print_state); 440 441 print_cb->print_event(print_state, 442 /*topic=*/NULL, 443 /*pmu_name=*/NULL, 444 PERF_TYPE_RAW, 445 "rNNN", 446 /*event_alias=*/NULL, 447 /*scale_unit=*/NULL, 448 /*deprecated=*/false, 449 event_type_descriptors[PERF_TYPE_RAW], 450 /*desc=*/NULL, 451 /*long_desc=*/NULL, 452 /*encoding_desc=*/NULL); 453 454 perf_pmus__print_raw_pmu_events(print_cb, print_state); 455 456 print_cb->print_event(print_state, 457 /*topic=*/NULL, 458 /*pmu_name=*/NULL, 459 PERF_TYPE_BREAKPOINT, 460 "mem:<addr>[/len][:access]", 461 /*scale_unit=*/NULL, 462 /*event_alias=*/NULL, 463 /*deprecated=*/false, 464 event_type_descriptors[PERF_TYPE_BREAKPOINT], 465 /*desc=*/NULL, 466 /*long_desc=*/NULL, 467 /*encoding_desc=*/NULL); 468 469 print_sdt_events(print_cb, print_state); 470 471 metricgroup__print(print_cb, print_state); 472 473 print_libpfm_events(print_cb, print_state); 474 } 475