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 <linux/stddef.h> 13 #include <linux/perf_event.h> 14 #include <linux/zalloc.h> 15 #include <subcmd/pager.h> 16 17 #include "build-id.h" 18 #include "debug.h" 19 #include "evsel.h" 20 #include "metricgroup.h" 21 #include "parse-events.h" 22 #include "pmu.h" 23 #include "print-events.h" 24 #include "probe-file.h" 25 #include "string2.h" 26 #include "strlist.h" 27 #include "tracepoint.h" 28 #include "pfm.h" 29 #include "pmu-hybrid.h" 30 #include "thread_map.h" 31 32 #define MAX_NAME_LEN 100 33 34 /** Strings corresponding to enum perf_type_id. */ 35 static const char * const event_type_descriptors[] = { 36 "Hardware event", 37 "Software event", 38 "Tracepoint event", 39 "Hardware cache event", 40 "Raw hardware event descriptor", 41 "Hardware breakpoint", 42 }; 43 44 static const struct event_symbol event_symbols_tool[PERF_TOOL_MAX] = { 45 [PERF_TOOL_DURATION_TIME] = { 46 .symbol = "duration_time", 47 .alias = "", 48 }, 49 [PERF_TOOL_USER_TIME] = { 50 .symbol = "user_time", 51 .alias = "", 52 }, 53 [PERF_TOOL_SYSTEM_TIME] = { 54 .symbol = "system_time", 55 .alias = "", 56 }, 57 }; 58 59 /* 60 * Print the events from <debugfs_mount_point>/tracing/events 61 */ 62 void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unused, void *print_state __maybe_unused) 63 { 64 char *events_path = get_tracing_file("events"); 65 int events_fd = open(events_path, O_PATH); 66 67 put_tracing_file(events_path); 68 if (events_fd < 0) { 69 printf("Error: failed to open tracing events directory\n"); 70 return; 71 } 72 73 #ifdef HAVE_SCANDIRAT_SUPPORT 74 { 75 struct dirent **sys_namelist = NULL; 76 int sys_items = tracing_events__scandir_alphasort(&sys_namelist); 77 78 for (int i = 0; i < sys_items; i++) { 79 struct dirent *sys_dirent = sys_namelist[i]; 80 struct dirent **evt_namelist = NULL; 81 int dir_fd; 82 int evt_items; 83 84 if (sys_dirent->d_type != DT_DIR || 85 !strcmp(sys_dirent->d_name, ".") || 86 !strcmp(sys_dirent->d_name, "..")) 87 goto next_sys; 88 89 dir_fd = openat(events_fd, sys_dirent->d_name, O_PATH); 90 if (dir_fd < 0) 91 goto next_sys; 92 93 evt_items = scandirat(events_fd, sys_dirent->d_name, &evt_namelist, NULL, alphasort); 94 for (int j = 0; j < evt_items; j++) { 95 struct dirent *evt_dirent = evt_namelist[j]; 96 char evt_path[MAXPATHLEN]; 97 int evt_fd; 98 99 if (evt_dirent->d_type != DT_DIR || 100 !strcmp(evt_dirent->d_name, ".") || 101 !strcmp(evt_dirent->d_name, "..")) 102 goto next_evt; 103 104 snprintf(evt_path, sizeof(evt_path), "%s/id", evt_dirent->d_name); 105 evt_fd = openat(dir_fd, evt_path, O_RDONLY); 106 if (evt_fd < 0) 107 goto next_evt; 108 close(evt_fd); 109 110 snprintf(evt_path, MAXPATHLEN, "%s:%s", 111 sys_dirent->d_name, evt_dirent->d_name); 112 print_cb->print_event(print_state, 113 /*topic=*/NULL, 114 /*pmu_name=*/NULL, 115 evt_path, 116 /*event_alias=*/NULL, 117 /*scale_unit=*/NULL, 118 /*deprecated=*/false, 119 "Tracepoint event", 120 /*desc=*/NULL, 121 /*long_desc=*/NULL, 122 /*encoding_desc=*/NULL); 123 next_evt: 124 free(evt_namelist[j]); 125 } 126 close(dir_fd); 127 free(evt_namelist); 128 next_sys: 129 free(sys_namelist[i]); 130 } 131 132 free(sys_namelist); 133 } 134 #else 135 printf("\nWARNING: Your libc doesn't have the scandirat function, please ask its maintainers to implement it.\n" 136 " As a rough fallback, please do 'ls %s' to see the available tracepoint events.\n", events_path); 137 #endif 138 close(events_fd); 139 } 140 141 void print_sdt_events(const struct print_callbacks *print_cb, void *print_state) 142 { 143 struct strlist *bidlist, *sdtlist; 144 struct str_node *bid_nd, *sdt_name, *next_sdt_name; 145 const char *last_sdt_name = NULL; 146 147 /* 148 * The implicitly sorted sdtlist will hold the tracepoint name followed 149 * by @<buildid>. If the tracepoint name is unique (determined by 150 * looking at the adjacent nodes) the @<buildid> is dropped otherwise 151 * the executable path and buildid are added to the name. 152 */ 153 sdtlist = strlist__new(NULL, NULL); 154 if (!sdtlist) { 155 pr_debug("Failed to allocate new strlist for SDT\n"); 156 return; 157 } 158 bidlist = build_id_cache__list_all(true); 159 if (!bidlist) { 160 pr_debug("Failed to get buildids: %d\n", errno); 161 return; 162 } 163 strlist__for_each_entry(bid_nd, bidlist) { 164 struct probe_cache *pcache; 165 struct probe_cache_entry *ent; 166 167 pcache = probe_cache__new(bid_nd->s, NULL); 168 if (!pcache) 169 continue; 170 list_for_each_entry(ent, &pcache->entries, node) { 171 char buf[1024]; 172 173 snprintf(buf, sizeof(buf), "%s:%s@%s", 174 ent->pev.group, ent->pev.event, bid_nd->s); 175 strlist__add(sdtlist, buf); 176 } 177 probe_cache__delete(pcache); 178 } 179 strlist__delete(bidlist); 180 181 strlist__for_each_entry(sdt_name, sdtlist) { 182 bool show_detail = false; 183 char *bid = strchr(sdt_name->s, '@'); 184 char *evt_name = NULL; 185 186 if (bid) 187 *(bid++) = '\0'; 188 189 if (last_sdt_name && !strcmp(last_sdt_name, sdt_name->s)) { 190 show_detail = true; 191 } else { 192 next_sdt_name = strlist__next(sdt_name); 193 if (next_sdt_name) { 194 char *bid2 = strchr(next_sdt_name->s, '@'); 195 196 if (bid2) 197 *bid2 = '\0'; 198 if (strcmp(sdt_name->s, next_sdt_name->s) == 0) 199 show_detail = true; 200 if (bid2) 201 *bid2 = '@'; 202 } 203 } 204 last_sdt_name = sdt_name->s; 205 206 if (show_detail) { 207 char *path = build_id_cache__origname(bid); 208 209 if (path) { 210 if (asprintf(&evt_name, "%s@%s(%.12s)", sdt_name->s, path, bid) < 0) 211 evt_name = NULL; 212 free(path); 213 } 214 } 215 print_cb->print_event(print_state, 216 /*topic=*/NULL, 217 /*pmu_name=*/NULL, 218 evt_name ?: sdt_name->s, 219 /*event_alias=*/NULL, 220 /*deprecated=*/false, 221 /*scale_unit=*/NULL, 222 "SDT event", 223 /*desc=*/NULL, 224 /*long_desc=*/NULL, 225 /*encoding_desc=*/NULL); 226 227 free(evt_name); 228 } 229 strlist__delete(sdtlist); 230 } 231 232 static bool is_event_supported(u8 type, u64 config) 233 { 234 bool ret = true; 235 int open_return; 236 struct evsel *evsel; 237 struct perf_event_attr attr = { 238 .type = type, 239 .config = config, 240 .disabled = 1, 241 }; 242 struct perf_thread_map *tmap = thread_map__new_by_tid(0); 243 244 if (tmap == NULL) 245 return false; 246 247 evsel = evsel__new(&attr); 248 if (evsel) { 249 open_return = evsel__open(evsel, NULL, tmap); 250 ret = open_return >= 0; 251 252 if (open_return == -EACCES) { 253 /* 254 * This happens if the paranoid value 255 * /proc/sys/kernel/perf_event_paranoid is set to 2 256 * Re-run with exclude_kernel set; we don't do that 257 * by default as some ARM machines do not support it. 258 * 259 */ 260 evsel->core.attr.exclude_kernel = 1; 261 ret = evsel__open(evsel, NULL, tmap) >= 0; 262 } 263 evsel__delete(evsel); 264 } 265 266 perf_thread_map__put(tmap); 267 return ret; 268 } 269 270 int print_hwcache_events(const struct print_callbacks *print_cb, void *print_state) 271 { 272 struct perf_pmu *pmu = NULL; 273 const char *event_type_descriptor = event_type_descriptors[PERF_TYPE_HW_CACHE]; 274 275 while ((pmu = perf_pmu__scan(pmu)) != NULL) { 276 /* 277 * Skip uncore PMUs for performance. PERF_TYPE_HW_CACHE type 278 * attributes can accept software PMUs in the extended type, so 279 * also skip. 280 */ 281 if (pmu->is_uncore || pmu->type == PERF_TYPE_SOFTWARE) 282 continue; 283 284 for (int type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { 285 for (int op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { 286 /* skip invalid cache type */ 287 if (!evsel__is_cache_op_valid(type, op)) 288 continue; 289 290 for (int res = 0; res < PERF_COUNT_HW_CACHE_RESULT_MAX; res++) { 291 char name[64]; 292 char alias_name[128]; 293 __u64 config; 294 int ret; 295 296 __evsel__hw_cache_type_op_res_name(type, op, res, 297 name, sizeof(name)); 298 299 ret = parse_events__decode_legacy_cache(name, pmu->type, 300 &config); 301 if (ret || !is_event_supported(PERF_TYPE_HW_CACHE, config)) 302 continue; 303 snprintf(alias_name, sizeof(alias_name), "%s/%s/", 304 pmu->name, name); 305 print_cb->print_event(print_state, 306 "cache", 307 pmu->name, 308 name, 309 alias_name, 310 /*scale_unit=*/NULL, 311 /*deprecated=*/false, 312 event_type_descriptor, 313 /*desc=*/NULL, 314 /*long_desc=*/NULL, 315 /*encoding_desc=*/NULL); 316 } 317 } 318 } 319 } 320 return 0; 321 } 322 323 void print_tool_events(const struct print_callbacks *print_cb, void *print_state) 324 { 325 // Start at 1 because the first enum entry means no tool event. 326 for (int i = 1; i < PERF_TOOL_MAX; ++i) { 327 print_cb->print_event(print_state, 328 "tool", 329 /*pmu_name=*/NULL, 330 event_symbols_tool[i].symbol, 331 event_symbols_tool[i].alias, 332 /*scale_unit=*/NULL, 333 /*deprecated=*/false, 334 "Tool event", 335 /*desc=*/NULL, 336 /*long_desc=*/NULL, 337 /*encoding_desc=*/NULL); 338 } 339 } 340 341 void print_symbol_events(const struct print_callbacks *print_cb, void *print_state, 342 unsigned int type, const struct event_symbol *syms, 343 unsigned int max) 344 { 345 struct strlist *evt_name_list = strlist__new(NULL, NULL); 346 struct str_node *nd; 347 348 if (!evt_name_list) { 349 pr_debug("Failed to allocate new strlist for symbol events\n"); 350 return; 351 } 352 for (unsigned int i = 0; i < max; i++) { 353 /* 354 * New attr.config still not supported here, the latest 355 * example was PERF_COUNT_SW_CGROUP_SWITCHES 356 */ 357 if (syms[i].symbol == NULL) 358 continue; 359 360 if (!is_event_supported(type, i)) 361 continue; 362 363 if (strlen(syms[i].alias)) { 364 char name[MAX_NAME_LEN]; 365 366 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms[i].symbol, syms[i].alias); 367 strlist__add(evt_name_list, name); 368 } else 369 strlist__add(evt_name_list, syms[i].symbol); 370 } 371 372 strlist__for_each_entry(nd, evt_name_list) { 373 char *alias = strstr(nd->s, " OR "); 374 375 if (alias) { 376 *alias = '\0'; 377 alias += 4; 378 } 379 print_cb->print_event(print_state, 380 /*topic=*/NULL, 381 /*pmu_name=*/NULL, 382 nd->s, 383 alias, 384 /*scale_unit=*/NULL, 385 /*deprecated=*/false, 386 event_type_descriptors[type], 387 /*desc=*/NULL, 388 /*long_desc=*/NULL, 389 /*encoding_desc=*/NULL); 390 } 391 strlist__delete(evt_name_list); 392 } 393 394 /* 395 * Print the help text for the event symbols: 396 */ 397 void print_events(const struct print_callbacks *print_cb, void *print_state) 398 { 399 print_symbol_events(print_cb, print_state, PERF_TYPE_HARDWARE, 400 event_symbols_hw, PERF_COUNT_HW_MAX); 401 print_symbol_events(print_cb, print_state, PERF_TYPE_SOFTWARE, 402 event_symbols_sw, PERF_COUNT_SW_MAX); 403 404 print_tool_events(print_cb, print_state); 405 406 print_hwcache_events(print_cb, print_state); 407 408 print_pmu_events(print_cb, print_state); 409 410 print_cb->print_event(print_state, 411 /*topic=*/NULL, 412 /*pmu_name=*/NULL, 413 "rNNN", 414 /*event_alias=*/NULL, 415 /*scale_unit=*/NULL, 416 /*deprecated=*/false, 417 event_type_descriptors[PERF_TYPE_RAW], 418 /*desc=*/NULL, 419 /*long_desc=*/NULL, 420 /*encoding_desc=*/NULL); 421 422 print_cb->print_event(print_state, 423 /*topic=*/NULL, 424 /*pmu_name=*/NULL, 425 "cpu/t1=v1[,t2=v2,t3 ...]/modifier", 426 /*event_alias=*/NULL, 427 /*scale_unit=*/NULL, 428 /*deprecated=*/false, 429 event_type_descriptors[PERF_TYPE_RAW], 430 "(see 'man perf-list' on how to encode it)", 431 /*long_desc=*/NULL, 432 /*encoding_desc=*/NULL); 433 434 print_cb->print_event(print_state, 435 /*topic=*/NULL, 436 /*pmu_name=*/NULL, 437 "mem:<addr>[/len][:access]", 438 /*scale_unit=*/NULL, 439 /*event_alias=*/NULL, 440 /*deprecated=*/false, 441 event_type_descriptors[PERF_TYPE_BREAKPOINT], 442 /*desc=*/NULL, 443 /*long_desc=*/NULL, 444 /*encoding_desc=*/NULL); 445 446 print_tracepoint_events(print_cb, print_state); 447 448 print_sdt_events(print_cb, print_state); 449 450 metricgroup__print(print_cb, print_state); 451 452 print_libpfm_events(print_cb, print_state); 453 } 454