1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/list.h> 3 #include <linux/list_sort.h> 4 #include <linux/string.h> 5 #include <linux/zalloc.h> 6 #include <subcmd/pager.h> 7 #include <sys/types.h> 8 #include <ctype.h> 9 #include <dirent.h> 10 #include <pthread.h> 11 #include <string.h> 12 #include <unistd.h> 13 #include "debug.h" 14 #include "evsel.h" 15 #include "pmus.h" 16 #include "pmu.h" 17 #include "print-events.h" 18 19 /* 20 * core_pmus: A PMU belongs to core_pmus if it's name is "cpu" or it's sysfs 21 * directory contains "cpus" file. All PMUs belonging to core_pmus 22 * must have pmu->is_core=1. If there are more than one PMU in 23 * this list, perf interprets it as a heterogeneous platform. 24 * (FWIW, certain ARM platforms having heterogeneous cores uses 25 * homogeneous PMU, and thus they are treated as homogeneous 26 * platform by perf because core_pmus will have only one entry) 27 * other_pmus: All other PMUs which are not part of core_pmus list. It doesn't 28 * matter whether PMU is present per SMT-thread or outside of the 29 * core in the hw. For e.g., an instance of AMD ibs_fetch// and 30 * ibs_op// PMUs is present in each hw SMT thread, however they 31 * are captured under other_pmus. PMUs belonging to other_pmus 32 * must have pmu->is_core=0 but pmu->is_uncore could be 0 or 1. 33 */ 34 static LIST_HEAD(core_pmus); 35 static LIST_HEAD(other_pmus); 36 static bool read_sysfs_core_pmus; 37 static bool read_sysfs_all_pmus; 38 39 static int pmu_name_len_no_suffix(const char *str, unsigned long *num) 40 { 41 int orig_len, len; 42 43 orig_len = len = strlen(str); 44 45 /* Non-uncore PMUs have their full length, for example, i915. */ 46 if (!strstarts(str, "uncore_")) 47 return len; 48 49 /* 50 * Count trailing digits and '_', if '_{num}' suffix isn't present use 51 * the full length. 52 */ 53 while (len > 0 && isdigit(str[len - 1])) 54 len--; 55 56 if (len > 0 && len != orig_len && str[len - 1] == '_') { 57 if (num) 58 *num = strtoul(&str[len], NULL, 10); 59 return len - 1; 60 } 61 return orig_len; 62 } 63 64 void perf_pmus__destroy(void) 65 { 66 struct perf_pmu *pmu, *tmp; 67 68 list_for_each_entry_safe(pmu, tmp, &core_pmus, list) { 69 list_del(&pmu->list); 70 71 perf_pmu__delete(pmu); 72 } 73 list_for_each_entry_safe(pmu, tmp, &other_pmus, list) { 74 list_del(&pmu->list); 75 76 perf_pmu__delete(pmu); 77 } 78 read_sysfs_core_pmus = false; 79 read_sysfs_all_pmus = false; 80 } 81 82 static struct perf_pmu *pmu_find(const char *name) 83 { 84 struct perf_pmu *pmu; 85 86 list_for_each_entry(pmu, &core_pmus, list) { 87 if (!strcmp(pmu->name, name) || 88 (pmu->alias_name && !strcmp(pmu->alias_name, name))) 89 return pmu; 90 } 91 list_for_each_entry(pmu, &other_pmus, list) { 92 if (!strcmp(pmu->name, name) || 93 (pmu->alias_name && !strcmp(pmu->alias_name, name))) 94 return pmu; 95 } 96 97 return NULL; 98 } 99 100 struct perf_pmu *perf_pmus__find(const char *name) 101 { 102 struct perf_pmu *pmu; 103 int dirfd; 104 bool core_pmu; 105 106 /* 107 * Once PMU is loaded it stays in the list, 108 * so we keep us from multiple reading/parsing 109 * the pmu format definitions. 110 */ 111 pmu = pmu_find(name); 112 if (pmu) 113 return pmu; 114 115 if (read_sysfs_all_pmus) 116 return NULL; 117 118 core_pmu = is_pmu_core(name); 119 if (core_pmu && read_sysfs_core_pmus) 120 return NULL; 121 122 dirfd = perf_pmu__event_source_devices_fd(); 123 pmu = perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name); 124 close(dirfd); 125 126 return pmu; 127 } 128 129 static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name) 130 { 131 struct perf_pmu *pmu; 132 bool core_pmu; 133 134 /* 135 * Once PMU is loaded it stays in the list, 136 * so we keep us from multiple reading/parsing 137 * the pmu format definitions. 138 */ 139 pmu = pmu_find(name); 140 if (pmu) 141 return pmu; 142 143 if (read_sysfs_all_pmus) 144 return NULL; 145 146 core_pmu = is_pmu_core(name); 147 if (core_pmu && read_sysfs_core_pmus) 148 return NULL; 149 150 return perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name); 151 } 152 153 static int pmus_cmp(void *priv __maybe_unused, 154 const struct list_head *lhs, const struct list_head *rhs) 155 { 156 unsigned long lhs_num = 0, rhs_num = 0; 157 struct perf_pmu *lhs_pmu = container_of(lhs, struct perf_pmu, list); 158 struct perf_pmu *rhs_pmu = container_of(rhs, struct perf_pmu, list); 159 const char *lhs_pmu_name = lhs_pmu->name ?: ""; 160 const char *rhs_pmu_name = rhs_pmu->name ?: ""; 161 int lhs_pmu_name_len = pmu_name_len_no_suffix(lhs_pmu_name, &lhs_num); 162 int rhs_pmu_name_len = pmu_name_len_no_suffix(rhs_pmu_name, &rhs_num); 163 int ret = strncmp(lhs_pmu_name, rhs_pmu_name, 164 lhs_pmu_name_len < rhs_pmu_name_len ? lhs_pmu_name_len : rhs_pmu_name_len); 165 166 if (lhs_pmu_name_len != rhs_pmu_name_len || ret != 0 || lhs_pmu_name_len == 0) 167 return ret; 168 169 return lhs_num < rhs_num ? -1 : (lhs_num > rhs_num ? 1 : 0); 170 } 171 172 /* Add all pmus in sysfs to pmu list: */ 173 static void pmu_read_sysfs(bool core_only) 174 { 175 int fd; 176 DIR *dir; 177 struct dirent *dent; 178 179 if (read_sysfs_all_pmus || (core_only && read_sysfs_core_pmus)) 180 return; 181 182 fd = perf_pmu__event_source_devices_fd(); 183 if (fd < 0) 184 return; 185 186 dir = fdopendir(fd); 187 if (!dir) { 188 close(fd); 189 return; 190 } 191 192 while ((dent = readdir(dir))) { 193 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) 194 continue; 195 if (core_only && !is_pmu_core(dent->d_name)) 196 continue; 197 /* add to static LIST_HEAD(core_pmus) or LIST_HEAD(other_pmus): */ 198 perf_pmu__find2(fd, dent->d_name); 199 } 200 201 closedir(dir); 202 if (list_empty(&core_pmus)) { 203 if (!perf_pmu__create_placeholder_core_pmu(&core_pmus)) 204 pr_err("Failure to set up any core PMUs\n"); 205 } 206 list_sort(NULL, &core_pmus, pmus_cmp); 207 list_sort(NULL, &other_pmus, pmus_cmp); 208 if (!list_empty(&core_pmus)) { 209 read_sysfs_core_pmus = true; 210 if (!core_only) 211 read_sysfs_all_pmus = true; 212 } 213 } 214 215 static struct perf_pmu *__perf_pmus__find_by_type(unsigned int type) 216 { 217 struct perf_pmu *pmu; 218 219 list_for_each_entry(pmu, &core_pmus, list) { 220 if (pmu->type == type) 221 return pmu; 222 } 223 224 list_for_each_entry(pmu, &other_pmus, list) { 225 if (pmu->type == type) 226 return pmu; 227 } 228 return NULL; 229 } 230 231 struct perf_pmu *perf_pmus__find_by_type(unsigned int type) 232 { 233 struct perf_pmu *pmu = __perf_pmus__find_by_type(type); 234 235 if (pmu || read_sysfs_all_pmus) 236 return pmu; 237 238 pmu_read_sysfs(/*core_only=*/false); 239 pmu = __perf_pmus__find_by_type(type); 240 return pmu; 241 } 242 243 /* 244 * pmu iterator: If pmu is NULL, we start at the begin, otherwise return the 245 * next pmu. Returns NULL on end. 246 */ 247 struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu) 248 { 249 bool use_core_pmus = !pmu || pmu->is_core; 250 251 if (!pmu) { 252 pmu_read_sysfs(/*core_only=*/false); 253 pmu = list_prepare_entry(pmu, &core_pmus, list); 254 } 255 if (use_core_pmus) { 256 list_for_each_entry_continue(pmu, &core_pmus, list) 257 return pmu; 258 259 pmu = NULL; 260 pmu = list_prepare_entry(pmu, &other_pmus, list); 261 } 262 list_for_each_entry_continue(pmu, &other_pmus, list) 263 return pmu; 264 return NULL; 265 } 266 267 struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu) 268 { 269 if (!pmu) { 270 pmu_read_sysfs(/*core_only=*/true); 271 pmu = list_prepare_entry(pmu, &core_pmus, list); 272 } 273 list_for_each_entry_continue(pmu, &core_pmus, list) 274 return pmu; 275 276 return NULL; 277 } 278 279 const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str) 280 { 281 struct perf_pmu *pmu = NULL; 282 283 while ((pmu = perf_pmus__scan(pmu)) != NULL) { 284 if (!strcmp(pmu->name, str)) 285 return pmu; 286 /* Ignore "uncore_" prefix. */ 287 if (!strncmp(pmu->name, "uncore_", 7)) { 288 if (!strcmp(pmu->name + 7, str)) 289 return pmu; 290 } 291 /* Ignore "cpu_" prefix on Intel hybrid PMUs. */ 292 if (!strncmp(pmu->name, "cpu_", 4)) { 293 if (!strcmp(pmu->name + 4, str)) 294 return pmu; 295 } 296 } 297 return NULL; 298 } 299 300 int __weak perf_pmus__num_mem_pmus(void) 301 { 302 /* All core PMUs are for mem events. */ 303 return perf_pmus__num_core_pmus(); 304 } 305 306 /** Struct for ordering events as output in perf list. */ 307 struct sevent { 308 /** PMU for event. */ 309 const struct perf_pmu *pmu; 310 const char *name; 311 const char* alias; 312 const char *scale_unit; 313 const char *desc; 314 const char *long_desc; 315 const char *encoding_desc; 316 const char *topic; 317 const char *pmu_name; 318 bool deprecated; 319 }; 320 321 static int cmp_sevent(const void *a, const void *b) 322 { 323 const struct sevent *as = a; 324 const struct sevent *bs = b; 325 bool a_iscpu, b_iscpu; 326 int ret; 327 328 /* Put extra events last. */ 329 if (!!as->desc != !!bs->desc) 330 return !!as->desc - !!bs->desc; 331 332 /* Order by topics. */ 333 ret = strcmp(as->topic ?: "", bs->topic ?: ""); 334 if (ret) 335 return ret; 336 337 /* Order CPU core events to be first */ 338 a_iscpu = as->pmu ? as->pmu->is_core : true; 339 b_iscpu = bs->pmu ? bs->pmu->is_core : true; 340 if (a_iscpu != b_iscpu) 341 return a_iscpu ? -1 : 1; 342 343 /* Order by PMU name. */ 344 if (as->pmu != bs->pmu) { 345 ret = strcmp(as->pmu_name ?: "", bs->pmu_name ?: ""); 346 if (ret) 347 return ret; 348 } 349 350 /* Order by event name. */ 351 return strcmp(as->name, bs->name); 352 } 353 354 static bool pmu_alias_is_duplicate(struct sevent *a, struct sevent *b) 355 { 356 /* Different names -> never duplicates */ 357 if (strcmp(a->name ?: "//", b->name ?: "//")) 358 return false; 359 360 /* Don't remove duplicates for different PMUs */ 361 return strcmp(a->pmu_name, b->pmu_name) == 0; 362 } 363 364 struct events_callback_state { 365 struct sevent *aliases; 366 size_t aliases_len; 367 size_t index; 368 }; 369 370 static int perf_pmus__print_pmu_events__callback(void *vstate, 371 struct pmu_event_info *info) 372 { 373 struct events_callback_state *state = vstate; 374 struct sevent *s; 375 376 if (state->index >= state->aliases_len) { 377 pr_err("Unexpected event %s/%s/\n", info->pmu->name, info->name); 378 return 1; 379 } 380 s = &state->aliases[state->index]; 381 s->pmu = info->pmu; 382 #define COPY_STR(str) s->str = info->str ? strdup(info->str) : NULL 383 COPY_STR(name); 384 COPY_STR(alias); 385 COPY_STR(scale_unit); 386 COPY_STR(desc); 387 COPY_STR(long_desc); 388 COPY_STR(encoding_desc); 389 COPY_STR(topic); 390 COPY_STR(pmu_name); 391 #undef COPY_STR 392 s->deprecated = info->deprecated; 393 state->index++; 394 return 0; 395 } 396 397 void perf_pmus__print_pmu_events(const struct print_callbacks *print_cb, void *print_state) 398 { 399 struct perf_pmu *pmu; 400 int printed = 0; 401 int len; 402 struct sevent *aliases; 403 struct events_callback_state state; 404 405 pmu = NULL; 406 len = 0; 407 while ((pmu = perf_pmus__scan(pmu)) != NULL) 408 len += perf_pmu__num_events(pmu); 409 410 aliases = zalloc(sizeof(struct sevent) * len); 411 if (!aliases) { 412 pr_err("FATAL: not enough memory to print PMU events\n"); 413 return; 414 } 415 pmu = NULL; 416 state = (struct events_callback_state) { 417 .aliases = aliases, 418 .aliases_len = len, 419 .index = 0, 420 }; 421 while ((pmu = perf_pmus__scan(pmu)) != NULL) { 422 perf_pmu__for_each_event(pmu, &state, perf_pmus__print_pmu_events__callback); 423 } 424 qsort(aliases, len, sizeof(struct sevent), cmp_sevent); 425 for (int j = 0; j < len; j++) { 426 /* Skip duplicates */ 427 if (j > 0 && pmu_alias_is_duplicate(&aliases[j], &aliases[j - 1])) 428 continue; 429 430 print_cb->print_event(print_state, 431 aliases[j].pmu_name, 432 aliases[j].topic, 433 aliases[j].name, 434 aliases[j].alias, 435 aliases[j].scale_unit, 436 aliases[j].deprecated, 437 "Kernel PMU event", 438 aliases[j].desc, 439 aliases[j].long_desc, 440 aliases[j].encoding_desc); 441 zfree(&aliases[j].name); 442 zfree(&aliases[j].alias); 443 zfree(&aliases[j].scale_unit); 444 zfree(&aliases[j].desc); 445 zfree(&aliases[j].long_desc); 446 zfree(&aliases[j].encoding_desc); 447 zfree(&aliases[j].topic); 448 zfree(&aliases[j].pmu_name); 449 } 450 if (printed && pager_in_use()) 451 printf("\n"); 452 453 zfree(&aliases); 454 } 455 456 bool perf_pmus__have_event(const char *pname, const char *name) 457 { 458 struct perf_pmu *pmu = perf_pmus__find(pname); 459 460 return pmu && perf_pmu__have_event(pmu, name); 461 } 462 463 int perf_pmus__num_core_pmus(void) 464 { 465 static int count; 466 467 if (!count) { 468 struct perf_pmu *pmu = NULL; 469 470 while ((pmu = perf_pmus__scan_core(pmu)) != NULL) 471 count++; 472 } 473 return count; 474 } 475 476 static bool __perf_pmus__supports_extended_type(void) 477 { 478 struct perf_pmu *pmu = NULL; 479 480 if (perf_pmus__num_core_pmus() <= 1) 481 return false; 482 483 while ((pmu = perf_pmus__scan_core(pmu)) != NULL) { 484 if (!is_event_supported(PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES | ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT))) 485 return false; 486 } 487 488 return true; 489 } 490 491 static bool perf_pmus__do_support_extended_type; 492 493 static void perf_pmus__init_supports_extended_type(void) 494 { 495 perf_pmus__do_support_extended_type = __perf_pmus__supports_extended_type(); 496 } 497 498 bool perf_pmus__supports_extended_type(void) 499 { 500 static pthread_once_t extended_type_once = PTHREAD_ONCE_INIT; 501 502 pthread_once(&extended_type_once, perf_pmus__init_supports_extended_type); 503 504 return perf_pmus__do_support_extended_type; 505 } 506 507 char *perf_pmus__default_pmu_name(void) 508 { 509 int fd; 510 DIR *dir; 511 struct dirent *dent; 512 char *result = NULL; 513 514 if (!list_empty(&core_pmus)) 515 return strdup(list_first_entry(&core_pmus, struct perf_pmu, list)->name); 516 517 fd = perf_pmu__event_source_devices_fd(); 518 if (fd < 0) 519 return strdup("cpu"); 520 521 dir = fdopendir(fd); 522 if (!dir) { 523 close(fd); 524 return strdup("cpu"); 525 } 526 527 while ((dent = readdir(dir))) { 528 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) 529 continue; 530 if (is_pmu_core(dent->d_name)) { 531 result = strdup(dent->d_name); 532 break; 533 } 534 } 535 536 closedir(dir); 537 return result ?: strdup("cpu"); 538 } 539 540 struct perf_pmu *evsel__find_pmu(const struct evsel *evsel) 541 { 542 struct perf_pmu *pmu = evsel->pmu; 543 544 if (!pmu) { 545 pmu = perf_pmus__find_by_type(evsel->core.attr.type); 546 ((struct evsel *)evsel)->pmu = pmu; 547 } 548 return pmu; 549 } 550