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