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 <sys/param.h> 8 9 #include <api/fs/tracing_path.h> 10 #include <linux/stddef.h> 11 #include <linux/perf_event.h> 12 #include <linux/zalloc.h> 13 #include <subcmd/pager.h> 14 15 #include "build-id.h" 16 #include "debug.h" 17 #include "evsel.h" 18 #include "metricgroup.h" 19 #include "parse-events.h" 20 #include "pmu.h" 21 #include "print-events.h" 22 #include "probe-file.h" 23 #include "string2.h" 24 #include "strlist.h" 25 #include "thread_map.h" 26 #include "tracepoint.h" 27 #include "pfm.h" 28 #include "pmu-hybrid.h" 29 30 #define MAX_NAME_LEN 100 31 32 static const char * const event_type_descriptors[] = { 33 "Hardware event", 34 "Software event", 35 "Tracepoint event", 36 "Hardware cache event", 37 "Raw hardware event descriptor", 38 "Hardware breakpoint", 39 }; 40 41 static const struct event_symbol event_symbols_tool[PERF_TOOL_MAX] = { 42 [PERF_TOOL_DURATION_TIME] = { 43 .symbol = "duration_time", 44 .alias = "", 45 }, 46 [PERF_TOOL_USER_TIME] = { 47 .symbol = "user_time", 48 .alias = "", 49 }, 50 [PERF_TOOL_SYSTEM_TIME] = { 51 .symbol = "system_time", 52 .alias = "", 53 }, 54 }; 55 56 static int cmp_string(const void *a, const void *b) 57 { 58 const char * const *as = a; 59 const char * const *bs = b; 60 61 return strcmp(*as, *bs); 62 } 63 64 /* 65 * Print the events from <debugfs_mount_point>/tracing/events 66 */ 67 void print_tracepoint_events(const char *subsys_glob, 68 const char *event_glob, bool name_only) 69 { 70 DIR *sys_dir, *evt_dir; 71 struct dirent *sys_dirent, *evt_dirent; 72 char evt_path[MAXPATHLEN]; 73 char *dir_path; 74 char **evt_list = NULL; 75 unsigned int evt_i = 0, evt_num = 0; 76 bool evt_num_known = false; 77 78 restart: 79 sys_dir = tracing_events__opendir(); 80 if (!sys_dir) 81 return; 82 83 if (evt_num_known) { 84 evt_list = zalloc(sizeof(char *) * evt_num); 85 if (!evt_list) 86 goto out_close_sys_dir; 87 } 88 89 for_each_subsystem(sys_dir, sys_dirent) { 90 if (subsys_glob != NULL && 91 !strglobmatch(sys_dirent->d_name, subsys_glob)) 92 continue; 93 94 dir_path = get_events_file(sys_dirent->d_name); 95 if (!dir_path) 96 continue; 97 evt_dir = opendir(dir_path); 98 if (!evt_dir) 99 goto next; 100 101 for_each_event(dir_path, evt_dir, evt_dirent) { 102 if (event_glob != NULL && 103 !strglobmatch(evt_dirent->d_name, event_glob)) 104 continue; 105 106 if (!evt_num_known) { 107 evt_num++; 108 continue; 109 } 110 111 snprintf(evt_path, MAXPATHLEN, "%s:%s", 112 sys_dirent->d_name, evt_dirent->d_name); 113 114 evt_list[evt_i] = strdup(evt_path); 115 if (evt_list[evt_i] == NULL) { 116 put_events_file(dir_path); 117 goto out_close_evt_dir; 118 } 119 evt_i++; 120 } 121 closedir(evt_dir); 122 next: 123 put_events_file(dir_path); 124 } 125 closedir(sys_dir); 126 127 if (!evt_num_known) { 128 evt_num_known = true; 129 goto restart; 130 } 131 qsort(evt_list, evt_num, sizeof(char *), cmp_string); 132 evt_i = 0; 133 while (evt_i < evt_num) { 134 if (name_only) { 135 printf("%s ", evt_list[evt_i++]); 136 continue; 137 } 138 printf(" %-50s [%s]\n", evt_list[evt_i++], 139 event_type_descriptors[PERF_TYPE_TRACEPOINT]); 140 } 141 if (evt_num && pager_in_use()) 142 printf("\n"); 143 144 out_free: 145 evt_num = evt_i; 146 for (evt_i = 0; evt_i < evt_num; evt_i++) 147 zfree(&evt_list[evt_i]); 148 zfree(&evt_list); 149 return; 150 151 out_close_evt_dir: 152 closedir(evt_dir); 153 out_close_sys_dir: 154 closedir(sys_dir); 155 156 printf("FATAL: not enough memory to print %s\n", 157 event_type_descriptors[PERF_TYPE_TRACEPOINT]); 158 if (evt_list) 159 goto out_free; 160 } 161 162 void print_sdt_events(const char *subsys_glob, const char *event_glob, 163 bool name_only) 164 { 165 struct probe_cache *pcache; 166 struct probe_cache_entry *ent; 167 struct strlist *bidlist, *sdtlist; 168 struct strlist_config cfg = {.dont_dupstr = true}; 169 struct str_node *nd, *nd2; 170 char *buf, *path, *ptr = NULL; 171 bool show_detail = false; 172 int ret; 173 174 sdtlist = strlist__new(NULL, &cfg); 175 if (!sdtlist) { 176 pr_debug("Failed to allocate new strlist for SDT\n"); 177 return; 178 } 179 bidlist = build_id_cache__list_all(true); 180 if (!bidlist) { 181 pr_debug("Failed to get buildids: %d\n", errno); 182 return; 183 } 184 strlist__for_each_entry(nd, bidlist) { 185 pcache = probe_cache__new(nd->s, NULL); 186 if (!pcache) 187 continue; 188 list_for_each_entry(ent, &pcache->entries, node) { 189 if (!ent->sdt) 190 continue; 191 if (subsys_glob && 192 !strglobmatch(ent->pev.group, subsys_glob)) 193 continue; 194 if (event_glob && 195 !strglobmatch(ent->pev.event, event_glob)) 196 continue; 197 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group, 198 ent->pev.event, nd->s); 199 if (ret > 0) 200 strlist__add(sdtlist, buf); 201 } 202 probe_cache__delete(pcache); 203 } 204 strlist__delete(bidlist); 205 206 strlist__for_each_entry(nd, sdtlist) { 207 buf = strchr(nd->s, '@'); 208 if (buf) 209 *(buf++) = '\0'; 210 if (name_only) { 211 printf("%s ", nd->s); 212 continue; 213 } 214 nd2 = strlist__next(nd); 215 if (nd2) { 216 ptr = strchr(nd2->s, '@'); 217 if (ptr) 218 *ptr = '\0'; 219 if (strcmp(nd->s, nd2->s) == 0) 220 show_detail = true; 221 } 222 if (show_detail) { 223 path = build_id_cache__origname(buf); 224 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf); 225 if (ret > 0) { 226 printf(" %-50s [%s]\n", buf, "SDT event"); 227 free(buf); 228 } 229 free(path); 230 } else 231 printf(" %-50s [%s]\n", nd->s, "SDT event"); 232 if (nd2) { 233 if (strcmp(nd->s, nd2->s) != 0) 234 show_detail = false; 235 if (ptr) 236 *ptr = '@'; 237 } 238 } 239 strlist__delete(sdtlist); 240 } 241 242 static bool is_event_supported(u8 type, unsigned int config) 243 { 244 bool ret = true; 245 int open_return; 246 struct evsel *evsel; 247 struct perf_event_attr attr = { 248 .type = type, 249 .config = config, 250 .disabled = 1, 251 }; 252 struct perf_thread_map *tmap = thread_map__new_by_tid(0); 253 254 if (tmap == NULL) 255 return false; 256 257 evsel = evsel__new(&attr); 258 if (evsel) { 259 open_return = evsel__open(evsel, NULL, tmap); 260 ret = open_return >= 0; 261 262 if (open_return == -EACCES) { 263 /* 264 * This happens if the paranoid value 265 * /proc/sys/kernel/perf_event_paranoid is set to 2 266 * Re-run with exclude_kernel set; we don't do that 267 * by default as some ARM machines do not support it. 268 * 269 */ 270 evsel->core.attr.exclude_kernel = 1; 271 ret = evsel__open(evsel, NULL, tmap) >= 0; 272 } 273 evsel__delete(evsel); 274 } 275 276 perf_thread_map__put(tmap); 277 return ret; 278 } 279 280 int print_hwcache_events(const char *event_glob, bool name_only) 281 { 282 unsigned int type, op, i, evt_i = 0, evt_num = 0, npmus = 0; 283 char name[64], new_name[128]; 284 char **evt_list = NULL, **evt_pmus = NULL; 285 bool evt_num_known = false; 286 struct perf_pmu *pmu = NULL; 287 288 if (perf_pmu__has_hybrid()) { 289 npmus = perf_pmu__hybrid_pmu_num(); 290 evt_pmus = zalloc(sizeof(char *) * npmus); 291 if (!evt_pmus) 292 goto out_enomem; 293 } 294 295 restart: 296 if (evt_num_known) { 297 evt_list = zalloc(sizeof(char *) * evt_num); 298 if (!evt_list) 299 goto out_enomem; 300 } 301 302 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { 303 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { 304 /* skip invalid cache type */ 305 if (!evsel__is_cache_op_valid(type, op)) 306 continue; 307 308 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { 309 unsigned int hybrid_supported = 0, j; 310 bool supported; 311 312 __evsel__hw_cache_type_op_res_name(type, op, i, name, sizeof(name)); 313 if (event_glob != NULL && !strglobmatch(name, event_glob)) 314 continue; 315 316 if (!perf_pmu__has_hybrid()) { 317 if (!is_event_supported(PERF_TYPE_HW_CACHE, 318 type | (op << 8) | (i << 16))) { 319 continue; 320 } 321 } else { 322 perf_pmu__for_each_hybrid_pmu(pmu) { 323 if (!evt_num_known) { 324 evt_num++; 325 continue; 326 } 327 328 supported = is_event_supported( 329 PERF_TYPE_HW_CACHE, 330 type | (op << 8) | (i << 16) | 331 ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT)); 332 if (supported) { 333 snprintf(new_name, sizeof(new_name), 334 "%s/%s/", pmu->name, name); 335 evt_pmus[hybrid_supported] = 336 strdup(new_name); 337 hybrid_supported++; 338 } 339 } 340 341 if (hybrid_supported == 0) 342 continue; 343 } 344 345 if (!evt_num_known) { 346 evt_num++; 347 continue; 348 } 349 350 if ((hybrid_supported == 0) || 351 (hybrid_supported == npmus)) { 352 evt_list[evt_i] = strdup(name); 353 if (npmus > 0) { 354 for (j = 0; j < npmus; j++) 355 zfree(&evt_pmus[j]); 356 } 357 } else { 358 for (j = 0; j < hybrid_supported; j++) { 359 evt_list[evt_i++] = evt_pmus[j]; 360 evt_pmus[j] = NULL; 361 } 362 continue; 363 } 364 365 if (evt_list[evt_i] == NULL) 366 goto out_enomem; 367 evt_i++; 368 } 369 } 370 } 371 372 if (!evt_num_known) { 373 evt_num_known = true; 374 goto restart; 375 } 376 377 for (evt_i = 0; evt_i < evt_num; evt_i++) { 378 if (!evt_list[evt_i]) 379 break; 380 } 381 382 evt_num = evt_i; 383 qsort(evt_list, evt_num, sizeof(char *), cmp_string); 384 evt_i = 0; 385 while (evt_i < evt_num) { 386 if (name_only) { 387 printf("%s ", evt_list[evt_i++]); 388 continue; 389 } 390 printf(" %-50s [%s]\n", evt_list[evt_i++], 391 event_type_descriptors[PERF_TYPE_HW_CACHE]); 392 } 393 if (evt_num && pager_in_use()) 394 printf("\n"); 395 396 out_free: 397 evt_num = evt_i; 398 for (evt_i = 0; evt_i < evt_num; evt_i++) 399 zfree(&evt_list[evt_i]); 400 zfree(&evt_list); 401 402 for (evt_i = 0; evt_i < npmus; evt_i++) 403 zfree(&evt_pmus[evt_i]); 404 zfree(&evt_pmus); 405 return evt_num; 406 407 out_enomem: 408 printf("FATAL: not enough memory to print %s\n", 409 event_type_descriptors[PERF_TYPE_HW_CACHE]); 410 if (evt_list) 411 goto out_free; 412 return evt_num; 413 } 414 415 static void print_tool_event(const struct event_symbol *syms, const char *event_glob, 416 bool name_only) 417 { 418 if (syms->symbol == NULL) 419 return; 420 421 if (event_glob && !(strglobmatch(syms->symbol, event_glob) || 422 (syms->alias && strglobmatch(syms->alias, event_glob)))) 423 return; 424 425 if (name_only) 426 printf("%s ", syms->symbol); 427 else { 428 char name[MAX_NAME_LEN]; 429 430 if (syms->alias && strlen(syms->alias)) 431 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); 432 else 433 strlcpy(name, syms->symbol, MAX_NAME_LEN); 434 printf(" %-50s [%s]\n", name, "Tool event"); 435 } 436 } 437 438 void print_tool_events(const char *event_glob, bool name_only) 439 { 440 // Start at 1 because the first enum entry means no tool event. 441 for (int i = 1; i < PERF_TOOL_MAX; ++i) 442 print_tool_event(event_symbols_tool + i, event_glob, name_only); 443 444 if (pager_in_use()) 445 printf("\n"); 446 } 447 448 void print_symbol_events(const char *event_glob, unsigned int type, 449 struct event_symbol *syms, unsigned int max, 450 bool name_only) 451 { 452 unsigned int i, evt_i = 0, evt_num = 0; 453 char name[MAX_NAME_LEN]; 454 char **evt_list = NULL; 455 bool evt_num_known = false; 456 457 restart: 458 if (evt_num_known) { 459 evt_list = zalloc(sizeof(char *) * evt_num); 460 if (!evt_list) 461 goto out_enomem; 462 syms -= max; 463 } 464 465 for (i = 0; i < max; i++, syms++) { 466 /* 467 * New attr.config still not supported here, the latest 468 * example was PERF_COUNT_SW_CGROUP_SWITCHES 469 */ 470 if (syms->symbol == NULL) 471 continue; 472 473 if (event_glob != NULL && !(strglobmatch(syms->symbol, event_glob) || 474 (syms->alias && strglobmatch(syms->alias, event_glob)))) 475 continue; 476 477 if (!is_event_supported(type, i)) 478 continue; 479 480 if (!evt_num_known) { 481 evt_num++; 482 continue; 483 } 484 485 if (!name_only && strlen(syms->alias)) 486 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); 487 else 488 strlcpy(name, syms->symbol, MAX_NAME_LEN); 489 490 evt_list[evt_i] = strdup(name); 491 if (evt_list[evt_i] == NULL) 492 goto out_enomem; 493 evt_i++; 494 } 495 496 if (!evt_num_known) { 497 evt_num_known = true; 498 goto restart; 499 } 500 qsort(evt_list, evt_num, sizeof(char *), cmp_string); 501 evt_i = 0; 502 while (evt_i < evt_num) { 503 if (name_only) { 504 printf("%s ", evt_list[evt_i++]); 505 continue; 506 } 507 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]); 508 } 509 if (evt_num && pager_in_use()) 510 printf("\n"); 511 512 out_free: 513 evt_num = evt_i; 514 for (evt_i = 0; evt_i < evt_num; evt_i++) 515 zfree(&evt_list[evt_i]); 516 zfree(&evt_list); 517 return; 518 519 out_enomem: 520 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]); 521 if (evt_list) 522 goto out_free; 523 } 524 525 /* 526 * Print the help text for the event symbols: 527 */ 528 void print_events(const char *event_glob, bool name_only, bool quiet_flag, 529 bool long_desc, bool details_flag, bool deprecated, 530 const char *pmu_name) 531 { 532 print_symbol_events(event_glob, PERF_TYPE_HARDWARE, 533 event_symbols_hw, PERF_COUNT_HW_MAX, name_only); 534 535 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE, 536 event_symbols_sw, PERF_COUNT_SW_MAX, name_only); 537 print_tool_events(event_glob, name_only); 538 539 print_hwcache_events(event_glob, name_only); 540 541 print_pmu_events(event_glob, name_only, quiet_flag, long_desc, 542 details_flag, deprecated, pmu_name); 543 544 if (event_glob != NULL) 545 return; 546 547 if (!name_only) { 548 printf(" %-50s [%s]\n", 549 "rNNN", 550 event_type_descriptors[PERF_TYPE_RAW]); 551 printf(" %-50s [%s]\n", 552 "cpu/t1=v1[,t2=v2,t3 ...]/modifier", 553 event_type_descriptors[PERF_TYPE_RAW]); 554 if (pager_in_use()) 555 printf(" (see 'man perf-list' on how to encode it)\n\n"); 556 557 printf(" %-50s [%s]\n", 558 "mem:<addr>[/len][:access]", 559 event_type_descriptors[PERF_TYPE_BREAKPOINT]); 560 if (pager_in_use()) 561 printf("\n"); 562 } 563 564 print_tracepoint_events(NULL, NULL, name_only); 565 566 print_sdt_events(NULL, NULL, name_only); 567 568 metricgroup__print(true, true, NULL, name_only, details_flag, 569 pmu_name); 570 571 print_libpfm_events(name_only, long_desc); 572 } 573