| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | #include <linux/list.h> |
| 3 | #include <linux/compiler.h> |
| 4 | #include <linux/string.h> |
| 5 | #include <linux/zalloc.h> |
| 6 | #include <linux/ctype.h> |
| 7 | #include <sys/types.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <unistd.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdbool.h> |
| 13 | #include <dirent.h> |
| 14 | #include <api/fs/fs.h> |
| 15 | #include <api/io.h> |
| 16 | #include <api/io_dir.h> |
| 17 | #include <locale.h> |
| 18 | #include <fnmatch.h> |
| 19 | #include <math.h> |
| 20 | #include "debug.h" |
| 21 | #include "evsel.h" |
| 22 | #include "pmu.h" |
| 23 | #include "drm_pmu.h" |
| 24 | #include "hwmon_pmu.h" |
| 25 | #include "pmus.h" |
| 26 | #include "tool_pmu.h" |
| 27 | #include "tp_pmu.h" |
| 28 | #include <util/pmu-bison.h> |
| 29 | #include <util/pmu-flex.h> |
| 30 | #include "parse-events.h" |
| 31 | #include "print-events.h" |
| 32 | #include "hashmap.h" |
| 33 | #include "header.h" |
| 34 | #include "string2.h" |
| 35 | #include "strbuf.h" |
| 36 | #include "fncache.h" |
| 37 | #include "util/evsel_config.h" |
| 38 | #include <regex.h> |
| 39 | |
| 40 | #define UNIT_MAX_LEN 31 /* max length for event unit name */ |
| 41 | |
| 42 | enum event_source { |
| 43 | /* An event loaded from /sys/bus/event_source/devices/<pmu>/events. */ |
| 44 | EVENT_SRC_SYSFS, |
| 45 | /* An event loaded from a CPUID matched json file. */ |
| 46 | EVENT_SRC_CPU_JSON, |
| 47 | /* |
| 48 | * An event loaded from a /sys/bus/event_source/devices/<pmu>/identifier matched json |
| 49 | * file. |
| 50 | */ |
| 51 | EVENT_SRC_SYS_JSON, |
| 52 | }; |
| 53 | |
| 54 | /** |
| 55 | * struct perf_pmu_alias - An event either read from sysfs or builtin in |
| 56 | * pmu-events.c, created by parsing the pmu-events json files. |
| 57 | */ |
| 58 | struct perf_pmu_alias { |
| 59 | /** @name: Name of the event like "mem-loads". */ |
| 60 | char *name; |
| 61 | /** @desc: Optional short description of the event. */ |
| 62 | char *desc; |
| 63 | /** @long_desc: Optional long description. */ |
| 64 | char *long_desc; |
| 65 | /** |
| 66 | * @topic: Optional topic such as cache or pipeline, particularly for |
| 67 | * json events. |
| 68 | */ |
| 69 | char *topic; |
| 70 | /** @terms: Owned copy of the event terms. */ |
| 71 | char *terms; |
| 72 | /** |
| 73 | * @legacy_terms: If the event aliases a legacy event, holds a copy |
| 74 | * ofthe legacy event string. |
| 75 | */ |
| 76 | char *legacy_terms; |
| 77 | /** |
| 78 | * @pmu_name: The name copied from the json struct pmu_event. This can |
| 79 | * differ from the PMU name as it won't have suffixes. |
| 80 | */ |
| 81 | char *pmu_name; |
| 82 | /** @unit: Units for the event, such as bytes or cache lines. */ |
| 83 | char unit[UNIT_MAX_LEN+1]; |
| 84 | /** @scale: Value to scale read counter values by. */ |
| 85 | double scale; |
| 86 | /** @retirement_latency_mean: Value to be given for unsampled retirement latency mean. */ |
| 87 | double retirement_latency_mean; |
| 88 | /** @retirement_latency_min: Value to be given for unsampled retirement latency min. */ |
| 89 | double retirement_latency_min; |
| 90 | /** @retirement_latency_max: Value to be given for unsampled retirement latency max. */ |
| 91 | double retirement_latency_max; |
| 92 | /** |
| 93 | * @per_pkg: Does the file |
| 94 | * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.per-pkg or |
| 95 | * equivalent json value exist and have the value 1. |
| 96 | */ |
| 97 | bool per_pkg; |
| 98 | /** |
| 99 | * @snapshot: Does the file |
| 100 | * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.snapshot |
| 101 | * exist and have the value 1. |
| 102 | */ |
| 103 | bool snapshot; |
| 104 | /** |
| 105 | * @deprecated: Is the event hidden and so not shown in perf list by |
| 106 | * default. |
| 107 | */ |
| 108 | bool deprecated; |
| 109 | /** |
| 110 | * @legacy_deprecated_checked: Legacy events may not be supported by the |
| 111 | * PMU need to be checked. If they aren't supported they are marked |
| 112 | * deprecated. |
| 113 | */ |
| 114 | bool legacy_deprecated_checked; |
| 115 | /** @from_sysfs: Was the alias from sysfs or a json event? */ |
| 116 | bool from_sysfs; |
| 117 | /** @info_loaded: Have the scale, unit and other values been read from disk? */ |
| 118 | bool info_loaded; |
| 119 | }; |
| 120 | |
| 121 | /** |
| 122 | * struct perf_pmu_format - Values from a format file read from |
| 123 | * <sysfs>/devices/cpu/format/ held in struct perf_pmu. |
| 124 | * |
| 125 | * For example, the contents of <sysfs>/devices/cpu/format/event may be |
| 126 | * "config:0-7" and will be represented here as name="event", |
| 127 | * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set. |
| 128 | */ |
| 129 | struct perf_pmu_format { |
| 130 | /** @list: Element on list within struct perf_pmu. */ |
| 131 | struct list_head list; |
| 132 | /** @bits: Which config bits are set by this format value. */ |
| 133 | DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS); |
| 134 | /** @name: The modifier/file name. */ |
| 135 | char *name; |
| 136 | /** |
| 137 | * @value : Which config value the format relates to. Supported values |
| 138 | * are from PERF_PMU_FORMAT_VALUE_CONFIG to |
| 139 | * PERF_PMU_FORMAT_VALUE_CONFIG_END. |
| 140 | */ |
| 141 | u16 value; |
| 142 | /** @loaded: Has the contents been loaded/parsed. */ |
| 143 | bool loaded; |
| 144 | }; |
| 145 | |
| 146 | static int pmu_aliases_parse(struct perf_pmu *pmu); |
| 147 | |
| 148 | static struct perf_pmu_format *perf_pmu__new_format(struct list_head *list, char *name) |
| 149 | { |
| 150 | struct perf_pmu_format *format; |
| 151 | |
| 152 | format = zalloc(sizeof(*format)); |
| 153 | if (!format) |
| 154 | return NULL; |
| 155 | |
| 156 | format->name = strdup(name); |
| 157 | if (!format->name) { |
| 158 | free(format); |
| 159 | return NULL; |
| 160 | } |
| 161 | list_add_tail(new: &format->list, head: list); |
| 162 | return format; |
| 163 | } |
| 164 | |
| 165 | /* Called at the end of parsing a format. */ |
| 166 | void perf_pmu_format__set_value(void *vformat, int config, unsigned long *bits) |
| 167 | { |
| 168 | struct perf_pmu_format *format = vformat; |
| 169 | |
| 170 | format->value = config; |
| 171 | memcpy(format->bits, bits, sizeof(format->bits)); |
| 172 | } |
| 173 | |
| 174 | static void __perf_pmu_format__load(struct perf_pmu_format *format, FILE *file) |
| 175 | { |
| 176 | void *scanner; |
| 177 | int ret; |
| 178 | |
| 179 | ret = perf_pmu_lex_init(&scanner); |
| 180 | if (ret) |
| 181 | return; |
| 182 | |
| 183 | perf_pmu_set_in(file, scanner); |
| 184 | ret = perf_pmu_parse(format, scanner); |
| 185 | perf_pmu_lex_destroy(scanner); |
| 186 | format->loaded = true; |
| 187 | } |
| 188 | |
| 189 | static void perf_pmu_format__load(const struct perf_pmu *pmu, struct perf_pmu_format *format) |
| 190 | { |
| 191 | char path[PATH_MAX]; |
| 192 | FILE *file = NULL; |
| 193 | |
| 194 | if (format->loaded) |
| 195 | return; |
| 196 | |
| 197 | if (!perf_pmu__pathname_scnprintf(buf: path, size: sizeof(path), pmu_name: pmu->name, filename: "format" )) |
| 198 | return; |
| 199 | |
| 200 | assert(strlen(path) + strlen(format->name) + 2 < sizeof(path)); |
| 201 | strcat(p: path, q: "/" ); |
| 202 | strcat(p: path, q: format->name); |
| 203 | |
| 204 | file = fopen(path, "r" ); |
| 205 | if (!file) |
| 206 | return; |
| 207 | __perf_pmu_format__load(format, file); |
| 208 | fclose(file); |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Parse & process all the sysfs attributes located under |
| 213 | * the directory specified in 'dir' parameter. |
| 214 | */ |
| 215 | static int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load) |
| 216 | { |
| 217 | struct io_dirent64 *evt_ent; |
| 218 | struct io_dir format_dir; |
| 219 | int ret = 0; |
| 220 | |
| 221 | io_dir__init(&format_dir, dirfd); |
| 222 | |
| 223 | while ((evt_ent = io_dir__readdir(&format_dir)) != NULL) { |
| 224 | struct perf_pmu_format *format; |
| 225 | char *name = evt_ent->d_name; |
| 226 | |
| 227 | if (io_dir__is_dir(&format_dir, evt_ent)) |
| 228 | continue; |
| 229 | |
| 230 | format = perf_pmu__new_format(list: &pmu->format, name); |
| 231 | if (!format) { |
| 232 | ret = -ENOMEM; |
| 233 | break; |
| 234 | } |
| 235 | |
| 236 | if (eager_load) { |
| 237 | FILE *file; |
| 238 | int fd = openat(dirfd, name, O_RDONLY); |
| 239 | |
| 240 | if (fd < 0) { |
| 241 | ret = -errno; |
| 242 | break; |
| 243 | } |
| 244 | file = fdopen(fd, "r" ); |
| 245 | if (!file) { |
| 246 | close(fd); |
| 247 | break; |
| 248 | } |
| 249 | __perf_pmu_format__load(format, file); |
| 250 | fclose(file); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | close(format_dir.dirfd); |
| 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Reading/parsing the default pmu format definition, which should be |
| 260 | * located at: |
| 261 | * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes. |
| 262 | */ |
| 263 | static int pmu_format(struct perf_pmu *pmu, int dirfd, const char *name, bool eager_load) |
| 264 | { |
| 265 | int fd; |
| 266 | |
| 267 | fd = perf_pmu__pathname_fd(dirfd, pmu_name: name, filename: "format" , O_DIRECTORY); |
| 268 | if (fd < 0) |
| 269 | return 0; |
| 270 | |
| 271 | /* it'll close the fd */ |
| 272 | if (perf_pmu__format_parse(pmu, dirfd: fd, eager_load)) |
| 273 | return -1; |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static int parse_double(const char *scale, char **end, double *sval) |
| 279 | { |
| 280 | char *lc; |
| 281 | int ret = 0; |
| 282 | |
| 283 | /* |
| 284 | * save current locale |
| 285 | */ |
| 286 | lc = setlocale(LC_NUMERIC, NULL); |
| 287 | |
| 288 | /* |
| 289 | * The lc string may be allocated in static storage, |
| 290 | * so get a dynamic copy to make it survive setlocale |
| 291 | * call below. |
| 292 | */ |
| 293 | lc = strdup(lc); |
| 294 | if (!lc) { |
| 295 | ret = -ENOMEM; |
| 296 | goto out; |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * force to C locale to ensure kernel |
| 301 | * scale string is converted correctly. |
| 302 | * kernel uses default C locale. |
| 303 | */ |
| 304 | setlocale(LC_NUMERIC, "C" ); |
| 305 | |
| 306 | *sval = strtod(scale, end); |
| 307 | |
| 308 | out: |
| 309 | /* restore locale */ |
| 310 | setlocale(LC_NUMERIC, lc); |
| 311 | free(lc); |
| 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | int perf_pmu__convert_scale(const char *scale, char **end, double *sval) |
| 316 | { |
| 317 | return parse_double(scale, end, sval); |
| 318 | } |
| 319 | |
| 320 | static int perf_pmu__parse_scale(struct perf_pmu *pmu, struct perf_pmu_alias *alias) |
| 321 | { |
| 322 | struct stat st; |
| 323 | ssize_t sret; |
| 324 | size_t len; |
| 325 | char scale[128]; |
| 326 | int fd, ret = -1; |
| 327 | char path[PATH_MAX]; |
| 328 | |
| 329 | len = perf_pmu__event_source_devices_scnprintf(pathname: path, size: sizeof(path)); |
| 330 | if (!len) |
| 331 | return 0; |
| 332 | scnprintf(buf: path + len, size: sizeof(path) - len, fmt: "%s/events/%s.scale" , pmu->name, alias->name); |
| 333 | |
| 334 | fd = open(path, O_RDONLY); |
| 335 | if (fd == -1) |
| 336 | return -1; |
| 337 | |
| 338 | if (fstat(fd, &st) < 0) |
| 339 | goto error; |
| 340 | |
| 341 | sret = read(fd, scale, sizeof(scale)-1); |
| 342 | if (sret < 0) |
| 343 | goto error; |
| 344 | |
| 345 | if (scale[sret - 1] == '\n') |
| 346 | scale[sret - 1] = '\0'; |
| 347 | else |
| 348 | scale[sret] = '\0'; |
| 349 | |
| 350 | ret = perf_pmu__convert_scale(scale, NULL, sval: &alias->scale); |
| 351 | error: |
| 352 | close(fd); |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | static int perf_pmu__parse_unit(struct perf_pmu *pmu, struct perf_pmu_alias *alias) |
| 357 | { |
| 358 | char path[PATH_MAX]; |
| 359 | size_t len; |
| 360 | ssize_t sret; |
| 361 | int fd; |
| 362 | |
| 363 | |
| 364 | len = perf_pmu__event_source_devices_scnprintf(pathname: path, size: sizeof(path)); |
| 365 | if (!len) |
| 366 | return 0; |
| 367 | scnprintf(buf: path + len, size: sizeof(path) - len, fmt: "%s/events/%s.unit" , pmu->name, alias->name); |
| 368 | |
| 369 | fd = open(path, O_RDONLY); |
| 370 | if (fd == -1) |
| 371 | return -1; |
| 372 | |
| 373 | sret = read(fd, alias->unit, UNIT_MAX_LEN); |
| 374 | if (sret < 0) |
| 375 | goto error; |
| 376 | |
| 377 | close(fd); |
| 378 | |
| 379 | if (alias->unit[sret - 1] == '\n') |
| 380 | alias->unit[sret - 1] = '\0'; |
| 381 | else |
| 382 | alias->unit[sret] = '\0'; |
| 383 | |
| 384 | return 0; |
| 385 | error: |
| 386 | close(fd); |
| 387 | alias->unit[0] = '\0'; |
| 388 | return -1; |
| 389 | } |
| 390 | |
| 391 | static bool perf_pmu__parse_event_source_bool(const char *pmu_name, const char *event_name, |
| 392 | const char *suffix) |
| 393 | { |
| 394 | char path[PATH_MAX]; |
| 395 | size_t len; |
| 396 | int fd; |
| 397 | |
| 398 | len = perf_pmu__event_source_devices_scnprintf(pathname: path, size: sizeof(path)); |
| 399 | if (!len) |
| 400 | return false; |
| 401 | |
| 402 | scnprintf(buf: path + len, size: sizeof(path) - len, fmt: "%s/events/%s.%s" , pmu_name, event_name, suffix); |
| 403 | |
| 404 | fd = open(path, O_RDONLY); |
| 405 | if (fd == -1) |
| 406 | return false; |
| 407 | |
| 408 | #ifndef NDEBUG |
| 409 | { |
| 410 | char buf[8]; |
| 411 | |
| 412 | len = read(fd, buf, sizeof(buf)); |
| 413 | assert(len == 1 || len == 2); |
| 414 | assert(buf[0] == '1'); |
| 415 | } |
| 416 | #endif |
| 417 | |
| 418 | close(fd); |
| 419 | return true; |
| 420 | } |
| 421 | |
| 422 | static void perf_pmu__parse_per_pkg(struct perf_pmu *pmu, struct perf_pmu_alias *alias) |
| 423 | { |
| 424 | alias->per_pkg = perf_pmu__parse_event_source_bool(pmu_name: pmu->name, event_name: alias->name, suffix: "per-pkg" ); |
| 425 | } |
| 426 | |
| 427 | static void perf_pmu__parse_snapshot(struct perf_pmu *pmu, struct perf_pmu_alias *alias) |
| 428 | { |
| 429 | alias->snapshot = perf_pmu__parse_event_source_bool(pmu_name: pmu->name, event_name: alias->name, suffix: "snapshot" ); |
| 430 | } |
| 431 | |
| 432 | /* Delete an alias entry. */ |
| 433 | static void perf_pmu_free_alias(struct perf_pmu_alias *alias) |
| 434 | { |
| 435 | if (!alias) |
| 436 | return; |
| 437 | |
| 438 | zfree(&alias->name); |
| 439 | zfree(&alias->desc); |
| 440 | zfree(&alias->long_desc); |
| 441 | zfree(&alias->topic); |
| 442 | zfree(&alias->pmu_name); |
| 443 | zfree(&alias->terms); |
| 444 | zfree(&alias->legacy_terms); |
| 445 | free(alias); |
| 446 | } |
| 447 | |
| 448 | static void perf_pmu__del_aliases(struct perf_pmu *pmu) |
| 449 | { |
| 450 | struct hashmap_entry *entry; |
| 451 | size_t bkt; |
| 452 | |
| 453 | if (!pmu->aliases) |
| 454 | return; |
| 455 | |
| 456 | hashmap__for_each_entry(pmu->aliases, entry, bkt) |
| 457 | perf_pmu_free_alias(alias: entry->pvalue); |
| 458 | |
| 459 | hashmap__free(map: pmu->aliases); |
| 460 | pmu->aliases = NULL; |
| 461 | } |
| 462 | |
| 463 | static struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu, |
| 464 | const char *name, |
| 465 | bool load) |
| 466 | { |
| 467 | struct perf_pmu_alias *alias; |
| 468 | bool has_sysfs_event; |
| 469 | char event_file_name[NAME_MAX + 8]; |
| 470 | |
| 471 | if (hashmap__find(pmu->aliases, name, &alias)) |
| 472 | return alias; |
| 473 | |
| 474 | if (!load || pmu->sysfs_aliases_loaded) |
| 475 | return NULL; |
| 476 | |
| 477 | /* |
| 478 | * Test if alias/event 'name' exists in the PMU's sysfs/events |
| 479 | * directory. If not skip parsing the sysfs aliases. Sysfs event |
| 480 | * name must be all lower or all upper case. |
| 481 | */ |
| 482 | scnprintf(buf: event_file_name, size: sizeof(event_file_name), fmt: "events/%s" , name); |
| 483 | for (size_t i = 7, n = 7 + strlen(name); i < n; i++) |
| 484 | event_file_name[i] = tolower(event_file_name[i]); |
| 485 | |
| 486 | has_sysfs_event = perf_pmu__file_exists(pmu, name: event_file_name); |
| 487 | if (!has_sysfs_event) { |
| 488 | for (size_t i = 7, n = 7 + strlen(name); i < n; i++) |
| 489 | event_file_name[i] = toupper(event_file_name[i]); |
| 490 | |
| 491 | has_sysfs_event = perf_pmu__file_exists(pmu, name: event_file_name); |
| 492 | } |
| 493 | if (has_sysfs_event) { |
| 494 | pmu_aliases_parse(pmu); |
| 495 | if (hashmap__find(pmu->aliases, name, &alias)) |
| 496 | return alias; |
| 497 | } |
| 498 | |
| 499 | return NULL; |
| 500 | } |
| 501 | |
| 502 | static bool assign_str(const char *name, const char *field, char **old_str, |
| 503 | const char *new_str) |
| 504 | { |
| 505 | if (!*old_str && new_str) { |
| 506 | *old_str = strdup(new_str); |
| 507 | return true; |
| 508 | } |
| 509 | |
| 510 | if (!new_str || !strcasecmp(s1: *old_str, s2: new_str)) |
| 511 | return false; /* Nothing to update. */ |
| 512 | |
| 513 | pr_debug("alias %s differs in field '%s' ('%s' != '%s')\n" , |
| 514 | name, field, *old_str, new_str); |
| 515 | zfree(old_str); |
| 516 | *old_str = strdup(new_str); |
| 517 | return true; |
| 518 | } |
| 519 | |
| 520 | static void read_alias_info(struct perf_pmu *pmu, struct perf_pmu_alias *alias) |
| 521 | { |
| 522 | if (!alias->from_sysfs || alias->info_loaded) |
| 523 | return; |
| 524 | |
| 525 | /* |
| 526 | * load unit name and scale if available |
| 527 | */ |
| 528 | perf_pmu__parse_unit(pmu, alias); |
| 529 | perf_pmu__parse_scale(pmu, alias); |
| 530 | perf_pmu__parse_per_pkg(pmu, alias); |
| 531 | perf_pmu__parse_snapshot(pmu, alias); |
| 532 | } |
| 533 | |
| 534 | struct update_alias_data { |
| 535 | struct perf_pmu *pmu; |
| 536 | struct perf_pmu_alias *alias; |
| 537 | bool legacy; |
| 538 | }; |
| 539 | |
| 540 | static int update_alias(const struct pmu_event *pe, |
| 541 | const struct pmu_events_table *table __maybe_unused, |
| 542 | void *vdata) |
| 543 | { |
| 544 | struct update_alias_data *data = vdata; |
| 545 | int ret = 0; |
| 546 | |
| 547 | read_alias_info(pmu: data->pmu, alias: data->alias); |
| 548 | assign_str(name: pe->name, field: "desc" , old_str: &data->alias->desc, new_str: pe->desc); |
| 549 | assign_str(name: pe->name, field: "long_desc" , old_str: &data->alias->long_desc, new_str: pe->long_desc); |
| 550 | assign_str(name: pe->name, field: "topic" , old_str: &data->alias->topic, new_str: pe->topic); |
| 551 | data->alias->per_pkg = pe->perpkg; |
| 552 | if (pe->event) { |
| 553 | if (data->legacy) { |
| 554 | zfree(&data->alias->legacy_terms); |
| 555 | data->alias->legacy_terms = strdup(pe->event); |
| 556 | } else { |
| 557 | zfree(&data->alias->terms); |
| 558 | data->alias->terms = strdup(pe->event); |
| 559 | } |
| 560 | } |
| 561 | if (!ret && pe->unit) { |
| 562 | char *unit; |
| 563 | |
| 564 | ret = perf_pmu__convert_scale(scale: pe->unit, end: &unit, sval: &data->alias->scale); |
| 565 | if (!ret) |
| 566 | snprintf(buf: data->alias->unit, size: sizeof(data->alias->unit), fmt: "%s" , unit); |
| 567 | } |
| 568 | if (!ret && pe->retirement_latency_mean) { |
| 569 | ret = parse_double(scale: pe->retirement_latency_mean, NULL, |
| 570 | sval: &data->alias->retirement_latency_mean); |
| 571 | } |
| 572 | if (!ret && pe->retirement_latency_min) { |
| 573 | ret = parse_double(scale: pe->retirement_latency_min, NULL, |
| 574 | sval: &data->alias->retirement_latency_min); |
| 575 | } |
| 576 | if (!ret && pe->retirement_latency_max) { |
| 577 | ret = parse_double(scale: pe->retirement_latency_max, NULL, |
| 578 | sval: &data->alias->retirement_latency_max); |
| 579 | } |
| 580 | return ret; |
| 581 | } |
| 582 | |
| 583 | static int perf_pmu__new_alias(struct perf_pmu *pmu, const char *name, |
| 584 | const char *desc, const char *val, int val_fd, |
| 585 | const struct pmu_event *pe, enum event_source src) |
| 586 | { |
| 587 | struct perf_pmu_alias *alias, *old_alias; |
| 588 | int ret = 0; |
| 589 | const char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL; |
| 590 | bool deprecated = false, perpkg = false; |
| 591 | |
| 592 | if (perf_pmu__find_alias(pmu, name, /*load=*/ false)) { |
| 593 | /* Alias was already created/loaded. */ |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | if (pe) { |
| 598 | long_desc = pe->long_desc; |
| 599 | topic = pe->topic; |
| 600 | unit = pe->unit; |
| 601 | perpkg = pe->perpkg; |
| 602 | deprecated = pe->deprecated; |
| 603 | if (pe->pmu && strcmp(pe->pmu, "default_core" )) |
| 604 | pmu_name = pe->pmu; |
| 605 | } |
| 606 | |
| 607 | alias = zalloc(sizeof(*alias)); |
| 608 | if (!alias) |
| 609 | return -ENOMEM; |
| 610 | |
| 611 | alias->scale = 1.0; |
| 612 | alias->unit[0] = '\0'; |
| 613 | alias->per_pkg = perpkg; |
| 614 | alias->snapshot = false; |
| 615 | alias->deprecated = deprecated; |
| 616 | alias->retirement_latency_mean = 0.0; |
| 617 | alias->retirement_latency_min = 0.0; |
| 618 | alias->retirement_latency_max = 0.0; |
| 619 | |
| 620 | if (!ret && pe && pe->retirement_latency_mean) { |
| 621 | ret = parse_double(scale: pe->retirement_latency_mean, NULL, |
| 622 | sval: &alias->retirement_latency_mean); |
| 623 | } |
| 624 | if (!ret && pe && pe->retirement_latency_min) { |
| 625 | ret = parse_double(scale: pe->retirement_latency_min, NULL, |
| 626 | sval: &alias->retirement_latency_min); |
| 627 | } |
| 628 | if (!ret && pe && pe->retirement_latency_max) { |
| 629 | ret = parse_double(scale: pe->retirement_latency_max, NULL, |
| 630 | sval: &alias->retirement_latency_max); |
| 631 | } |
| 632 | if (ret) |
| 633 | return ret; |
| 634 | |
| 635 | if (val_fd < 0) { |
| 636 | alias->terms = strdup(val); |
| 637 | } else { |
| 638 | char buf[256]; |
| 639 | struct io io; |
| 640 | size_t line_len; |
| 641 | |
| 642 | io__init(&io, val_fd, buf, sizeof(buf)); |
| 643 | ret = io__getline(&io, &alias->terms, &line_len) < 0 ? -errno : 0; |
| 644 | if (ret) { |
| 645 | pr_err("Failed to read alias %s\n" , name); |
| 646 | return ret; |
| 647 | } |
| 648 | if (line_len >= 1 && alias->terms[line_len - 1] == '\n') |
| 649 | alias->terms[line_len - 1] = '\0'; |
| 650 | } |
| 651 | alias->name = strdup(name); |
| 652 | alias->desc = desc ? strdup(desc) : NULL; |
| 653 | alias->long_desc = long_desc ? strdup(long_desc) : NULL; |
| 654 | alias->topic = topic ? strdup(topic) : NULL; |
| 655 | alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL; |
| 656 | if (unit) { |
| 657 | if (perf_pmu__convert_scale(scale: unit, end: (char **)&unit, sval: &alias->scale) < 0) { |
| 658 | perf_pmu_free_alias(alias); |
| 659 | return -1; |
| 660 | } |
| 661 | snprintf(buf: alias->unit, size: sizeof(alias->unit), fmt: "%s" , unit); |
| 662 | } |
| 663 | switch (src) { |
| 664 | default: |
| 665 | case EVENT_SRC_SYSFS: |
| 666 | alias->from_sysfs = true; |
| 667 | if (pmu->events_table || pmu->is_core) { |
| 668 | /* Update an event from sysfs with json data. */ |
| 669 | struct update_alias_data data = { |
| 670 | .pmu = pmu, |
| 671 | .alias = alias, |
| 672 | .legacy = false, |
| 673 | }; |
| 674 | if ((pmu_events_table__find_event(pmu->events_table, pmu, name, |
| 675 | update_alias, &data) == 0)) { |
| 676 | /* |
| 677 | * Override sysfs encodings with json encodings |
| 678 | * specific to the cpuid. |
| 679 | */ |
| 680 | pmu->cpu_common_json_aliases++; |
| 681 | } |
| 682 | if (pmu->is_core) { |
| 683 | /* Add in legacy encodings. */ |
| 684 | data.legacy = true; |
| 685 | if (pmu_events_table__find_event( |
| 686 | perf_pmu__default_core_events_table(), |
| 687 | pmu, name, update_alias, &data) == 0) |
| 688 | pmu->cpu_common_json_aliases++; |
| 689 | } |
| 690 | } |
| 691 | pmu->sysfs_aliases++; |
| 692 | break; |
| 693 | case EVENT_SRC_CPU_JSON: |
| 694 | pmu->cpu_json_aliases++; |
| 695 | break; |
| 696 | case EVENT_SRC_SYS_JSON: |
| 697 | pmu->sys_json_aliases++; |
| 698 | break; |
| 699 | |
| 700 | } |
| 701 | hashmap__set(pmu->aliases, alias->name, alias, /*old_key=*/ NULL, &old_alias); |
| 702 | perf_pmu_free_alias(alias: old_alias); |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | static inline bool pmu_alias_info_file(const char *name) |
| 707 | { |
| 708 | size_t len; |
| 709 | |
| 710 | len = strlen(name); |
| 711 | if (len > 5 && !strcmp(name + len - 5, ".unit" )) |
| 712 | return true; |
| 713 | if (len > 6 && !strcmp(name + len - 6, ".scale" )) |
| 714 | return true; |
| 715 | if (len > 8 && !strcmp(name + len - 8, ".per-pkg" )) |
| 716 | return true; |
| 717 | if (len > 9 && !strcmp(name + len - 9, ".snapshot" )) |
| 718 | return true; |
| 719 | |
| 720 | return false; |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | * Reading the pmu event aliases definition, which should be located at: |
| 725 | * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes. |
| 726 | */ |
| 727 | static int __pmu_aliases_parse(struct perf_pmu *pmu, int events_dir_fd) |
| 728 | { |
| 729 | struct io_dirent64 *evt_ent; |
| 730 | struct io_dir event_dir; |
| 731 | |
| 732 | io_dir__init(&event_dir, events_dir_fd); |
| 733 | |
| 734 | while ((evt_ent = io_dir__readdir(&event_dir))) { |
| 735 | char *name = evt_ent->d_name; |
| 736 | int fd; |
| 737 | |
| 738 | if (!strcmp(name, "." ) || !strcmp(name, ".." )) |
| 739 | continue; |
| 740 | |
| 741 | /* |
| 742 | * skip info files parsed in perf_pmu__new_alias() |
| 743 | */ |
| 744 | if (pmu_alias_info_file(name)) |
| 745 | continue; |
| 746 | |
| 747 | fd = openat(events_dir_fd, name, O_RDONLY); |
| 748 | if (fd == -1) { |
| 749 | pr_debug("Cannot open %s\n" , name); |
| 750 | continue; |
| 751 | } |
| 752 | |
| 753 | if (perf_pmu__new_alias(pmu, name, /*desc=*/ NULL, |
| 754 | /*val=*/ NULL, val_fd: fd, /*pe=*/ NULL, |
| 755 | src: EVENT_SRC_SYSFS) < 0) |
| 756 | pr_debug("Cannot set up %s\n" , name); |
| 757 | close(fd); |
| 758 | } |
| 759 | |
| 760 | pmu->sysfs_aliases_loaded = true; |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | static int pmu_aliases_parse(struct perf_pmu *pmu) |
| 765 | { |
| 766 | char path[PATH_MAX]; |
| 767 | size_t len; |
| 768 | int events_dir_fd, ret; |
| 769 | |
| 770 | if (pmu->sysfs_aliases_loaded) |
| 771 | return 0; |
| 772 | |
| 773 | len = perf_pmu__event_source_devices_scnprintf(pathname: path, size: sizeof(path)); |
| 774 | if (!len) |
| 775 | return 0; |
| 776 | scnprintf(buf: path + len, size: sizeof(path) - len, fmt: "%s/events" , pmu->name); |
| 777 | |
| 778 | events_dir_fd = open(path, O_DIRECTORY); |
| 779 | if (events_dir_fd == -1) { |
| 780 | pmu->sysfs_aliases_loaded = true; |
| 781 | return 0; |
| 782 | } |
| 783 | ret = __pmu_aliases_parse(pmu, events_dir_fd); |
| 784 | close(events_dir_fd); |
| 785 | return ret; |
| 786 | } |
| 787 | |
| 788 | static int pmu_aliases_parse_eager(struct perf_pmu *pmu, int sysfs_fd) |
| 789 | { |
| 790 | char path[NAME_MAX + 8]; |
| 791 | int ret, events_dir_fd; |
| 792 | |
| 793 | scnprintf(buf: path, size: sizeof(path), fmt: "%s/events" , pmu->name); |
| 794 | events_dir_fd = openat(sysfs_fd, path, O_DIRECTORY, 0); |
| 795 | if (events_dir_fd == -1) { |
| 796 | pmu->sysfs_aliases_loaded = true; |
| 797 | return 0; |
| 798 | } |
| 799 | ret = __pmu_aliases_parse(pmu, events_dir_fd); |
| 800 | close(events_dir_fd); |
| 801 | return ret; |
| 802 | } |
| 803 | |
| 804 | static int pmu_alias_terms(struct perf_pmu_alias *alias, struct list_head *terms) |
| 805 | { |
| 806 | struct parse_events_terms alias_terms; |
| 807 | struct parse_events_term *term; |
| 808 | int ret; |
| 809 | |
| 810 | parse_events_terms__init(terms: &alias_terms); |
| 811 | ret = parse_events_terms(terms: &alias_terms, str: alias->terms); |
| 812 | if (ret) { |
| 813 | pr_err("Cannot parse '%s' terms '%s': %d\n" , |
| 814 | alias->name, alias->terms, ret); |
| 815 | parse_events_terms__exit(terms: &alias_terms); |
| 816 | return ret; |
| 817 | } |
| 818 | list_for_each_entry(term, &alias_terms.terms, list) { |
| 819 | /* |
| 820 | * Weak terms don't override command line options, |
| 821 | * which we don't want for implicit terms in aliases. |
| 822 | */ |
| 823 | term->weak = true; |
| 824 | } |
| 825 | list_splice_init(list: &alias_terms.terms, head: terms); |
| 826 | parse_events_terms__exit(terms: &alias_terms); |
| 827 | return 0; |
| 828 | } |
| 829 | |
| 830 | /* |
| 831 | * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64) |
| 832 | * may have a "cpus" file. |
| 833 | */ |
| 834 | static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *pmu_name, bool is_core) |
| 835 | { |
| 836 | const char *templates[] = { |
| 837 | "cpumask" , |
| 838 | "cpus" , |
| 839 | NULL |
| 840 | }; |
| 841 | const char **template; |
| 842 | |
| 843 | for (template = templates; *template; template++) { |
| 844 | struct io io; |
| 845 | char buf[128]; |
| 846 | char *cpumask = NULL; |
| 847 | size_t cpumask_len; |
| 848 | ssize_t ret; |
| 849 | struct perf_cpu_map *cpus; |
| 850 | |
| 851 | io.fd = perf_pmu__pathname_fd(dirfd, pmu_name, filename: *template, O_RDONLY); |
| 852 | if (io.fd < 0) |
| 853 | continue; |
| 854 | |
| 855 | io__init(&io, io.fd, buf, sizeof(buf)); |
| 856 | ret = io__getline(&io, &cpumask, &cpumask_len); |
| 857 | close(io.fd); |
| 858 | if (ret < 0) |
| 859 | continue; |
| 860 | |
| 861 | cpus = perf_cpu_map__new(cpumask); |
| 862 | free(cpumask); |
| 863 | if (cpus) |
| 864 | return cpus; |
| 865 | } |
| 866 | |
| 867 | /* Nothing found, for core PMUs assume this means all CPUs. */ |
| 868 | return is_core ? cpu_map__online() : NULL; |
| 869 | } |
| 870 | |
| 871 | static bool pmu_is_uncore(int dirfd, const char *name) |
| 872 | { |
| 873 | int fd; |
| 874 | |
| 875 | fd = perf_pmu__pathname_fd(dirfd, pmu_name: name, filename: "cpumask" , O_PATH); |
| 876 | if (fd < 0) |
| 877 | return false; |
| 878 | |
| 879 | close(fd); |
| 880 | return true; |
| 881 | } |
| 882 | |
| 883 | static char *pmu_id(const char *name) |
| 884 | { |
| 885 | char path[PATH_MAX], *str; |
| 886 | size_t len; |
| 887 | |
| 888 | perf_pmu__pathname_scnprintf(buf: path, size: sizeof(path), pmu_name: name, filename: "identifier" ); |
| 889 | |
| 890 | if (filename__read_str(path, &str, &len) < 0) |
| 891 | return NULL; |
| 892 | |
| 893 | str[len - 1] = 0; /* remove line feed */ |
| 894 | |
| 895 | return str; |
| 896 | } |
| 897 | |
| 898 | /** |
| 899 | * is_sysfs_pmu_core() - PMU CORE devices have different name other than cpu in |
| 900 | * sysfs on some platforms like ARM or Intel hybrid. Looking for |
| 901 | * possible the cpus file in sysfs files to identify whether this is a |
| 902 | * core device. |
| 903 | * @name: The PMU name such as "cpu_atom". |
| 904 | */ |
| 905 | static int is_sysfs_pmu_core(const char *name) |
| 906 | { |
| 907 | char path[PATH_MAX]; |
| 908 | |
| 909 | if (!perf_pmu__pathname_scnprintf(buf: path, size: sizeof(path), pmu_name: name, filename: "cpus" )) |
| 910 | return 0; |
| 911 | return file_available(name: path); |
| 912 | } |
| 913 | |
| 914 | /** |
| 915 | * Return the length of the PMU name not including the suffix for uncore PMUs. |
| 916 | * |
| 917 | * We want to deduplicate many similar uncore PMUs by stripping their suffixes, |
| 918 | * but there are never going to be too many core PMUs and the suffixes might be |
| 919 | * interesting. "arm_cortex_a53" vs "arm_cortex_a57" or "cpum_cf" for example. |
| 920 | * |
| 921 | * @skip_duplicate_pmus: False in verbose mode so all uncore PMUs are visible |
| 922 | */ |
| 923 | static size_t pmu_deduped_name_len(const struct perf_pmu *pmu, const char *name, |
| 924 | bool skip_duplicate_pmus) |
| 925 | { |
| 926 | return skip_duplicate_pmus && !pmu->is_core |
| 927 | ? pmu_name_len_no_suffix(str: name) |
| 928 | : strlen(name); |
| 929 | } |
| 930 | |
| 931 | /** |
| 932 | * perf_pmu__match_wildcard - Does the pmu_name start with tok and is then only |
| 933 | * followed by nothing or a suffix? tok may contain |
| 934 | * part of a suffix. |
| 935 | * @pmu_name: The pmu_name with possible suffix. |
| 936 | * @tok: The wildcard argument to match. |
| 937 | */ |
| 938 | static bool perf_pmu__match_wildcard(const char *pmu_name, const char *tok) |
| 939 | { |
| 940 | const char *p, *suffix; |
| 941 | bool has_hex = false; |
| 942 | size_t tok_len = strlen(tok); |
| 943 | |
| 944 | /* Check start of pmu_name for equality. */ |
| 945 | if (strncmp(pmu_name, tok, tok_len)) |
| 946 | return false; |
| 947 | |
| 948 | suffix = p = pmu_name + tok_len; |
| 949 | if (*p == 0) |
| 950 | return true; |
| 951 | |
| 952 | if (*p == '_') { |
| 953 | ++p; |
| 954 | ++suffix; |
| 955 | } |
| 956 | |
| 957 | /* Ensure we end in a number */ |
| 958 | while (1) { |
| 959 | if (!isxdigit(*p)) |
| 960 | return false; |
| 961 | if (!has_hex) |
| 962 | has_hex = !isdigit(c: *p); |
| 963 | if (*(++p) == 0) |
| 964 | break; |
| 965 | } |
| 966 | |
| 967 | if (has_hex) |
| 968 | return (p - suffix) > 2; |
| 969 | |
| 970 | return true; |
| 971 | } |
| 972 | |
| 973 | /** |
| 974 | * perf_pmu__match_ignoring_suffix_uncore - Does the pmu_name match tok ignoring |
| 975 | * any trailing suffix on pmu_name and |
| 976 | * tok? The Suffix must be in form |
| 977 | * tok_{digits}, or tok{digits}. |
| 978 | * @pmu_name: The pmu_name with possible suffix. |
| 979 | * @tok: The possible match to pmu_name. |
| 980 | */ |
| 981 | static bool perf_pmu__match_ignoring_suffix_uncore(const char *pmu_name, const char *tok) |
| 982 | { |
| 983 | size_t pmu_name_len, tok_len; |
| 984 | |
| 985 | /* For robustness, check for NULL. */ |
| 986 | if (pmu_name == NULL) |
| 987 | return tok == NULL; |
| 988 | |
| 989 | /* uncore_ prefixes are ignored. */ |
| 990 | if (!strncmp(pmu_name, "uncore_" , 7)) |
| 991 | pmu_name += 7; |
| 992 | if (!strncmp(tok, "uncore_" , 7)) |
| 993 | tok += 7; |
| 994 | |
| 995 | pmu_name_len = pmu_name_len_no_suffix(str: pmu_name); |
| 996 | tok_len = pmu_name_len_no_suffix(str: tok); |
| 997 | if (pmu_name_len != tok_len) |
| 998 | return false; |
| 999 | |
| 1000 | return strncmp(pmu_name, tok, pmu_name_len) == 0; |
| 1001 | } |
| 1002 | |
| 1003 | |
| 1004 | /** |
| 1005 | * perf_pmu__match_wildcard_uncore - does to_match match the PMU's name? |
| 1006 | * @pmu_name: The pmu->name or pmu->alias to match against. |
| 1007 | * @to_match: the json struct pmu_event name. This may lack a suffix (which |
| 1008 | * matches) or be of the form "socket,pmuname" which will match |
| 1009 | * "socketX_pmunameY". |
| 1010 | */ |
| 1011 | static bool perf_pmu__match_wildcard_uncore(const char *pmu_name, const char *to_match) |
| 1012 | { |
| 1013 | char *mutable_to_match, *tok, *tmp; |
| 1014 | |
| 1015 | if (!pmu_name) |
| 1016 | return false; |
| 1017 | |
| 1018 | /* uncore_ prefixes are ignored. */ |
| 1019 | if (!strncmp(pmu_name, "uncore_" , 7)) |
| 1020 | pmu_name += 7; |
| 1021 | if (!strncmp(to_match, "uncore_" , 7)) |
| 1022 | to_match += 7; |
| 1023 | |
| 1024 | if (strchr(to_match, ',') == NULL) |
| 1025 | return perf_pmu__match_wildcard(pmu_name, tok: to_match); |
| 1026 | |
| 1027 | /* Process comma separated list of PMU name components. */ |
| 1028 | mutable_to_match = strdup(to_match); |
| 1029 | if (!mutable_to_match) |
| 1030 | return false; |
| 1031 | |
| 1032 | tok = strtok_r(mutable_to_match, "," , &tmp); |
| 1033 | while (tok) { |
| 1034 | size_t tok_len = strlen(tok); |
| 1035 | |
| 1036 | if (strncmp(pmu_name, tok, tok_len)) { |
| 1037 | /* Mismatch between part of pmu_name and tok. */ |
| 1038 | free(mutable_to_match); |
| 1039 | return false; |
| 1040 | } |
| 1041 | /* Move pmu_name forward over tok and suffix. */ |
| 1042 | pmu_name += tok_len; |
| 1043 | while (*pmu_name != '\0' && isdigit(c: *pmu_name)) |
| 1044 | pmu_name++; |
| 1045 | if (*pmu_name == '_') |
| 1046 | pmu_name++; |
| 1047 | |
| 1048 | tok = strtok_r(NULL, "," , &tmp); |
| 1049 | } |
| 1050 | free(mutable_to_match); |
| 1051 | return *pmu_name == '\0'; |
| 1052 | } |
| 1053 | |
| 1054 | bool pmu_uncore_identifier_match(const char *compat, const char *id) |
| 1055 | { |
| 1056 | regex_t re; |
| 1057 | regmatch_t pmatch[1]; |
| 1058 | int match; |
| 1059 | |
| 1060 | if (regcomp(&re, compat, REG_EXTENDED) != 0) { |
| 1061 | /* Warn unable to generate match particular string. */ |
| 1062 | pr_info("Invalid regular expression %s\n" , compat); |
| 1063 | return false; |
| 1064 | } |
| 1065 | |
| 1066 | match = !regexec(&re, id, 1, pmatch, 0); |
| 1067 | if (match) { |
| 1068 | /* Ensure a full match. */ |
| 1069 | match = pmatch[0].rm_so == 0 && (size_t)pmatch[0].rm_eo == strlen(id); |
| 1070 | } |
| 1071 | regfree(&re); |
| 1072 | |
| 1073 | return match; |
| 1074 | } |
| 1075 | |
| 1076 | static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe, |
| 1077 | const struct pmu_events_table *table __maybe_unused, |
| 1078 | void *vdata) |
| 1079 | { |
| 1080 | struct perf_pmu *pmu = vdata; |
| 1081 | |
| 1082 | perf_pmu__new_alias(pmu, name: pe->name, desc: pe->desc, val: pe->event, /*val_fd=*/ -1, |
| 1083 | pe, src: EVENT_SRC_CPU_JSON); |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
| 1087 | /* |
| 1088 | * From the pmu_events_table, find the events that correspond to the given |
| 1089 | * PMU and add them to the list 'head'. |
| 1090 | */ |
| 1091 | void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_events_table *table) |
| 1092 | { |
| 1093 | pmu_events_table__for_each_event(table, pmu, pmu_add_cpu_aliases_map_callback, pmu); |
| 1094 | } |
| 1095 | |
| 1096 | static void pmu_add_cpu_aliases(struct perf_pmu *pmu) |
| 1097 | { |
| 1098 | if (!pmu->events_table && !pmu->is_core) |
| 1099 | return; |
| 1100 | |
| 1101 | if (pmu->cpu_aliases_added) |
| 1102 | return; |
| 1103 | |
| 1104 | pmu_add_cpu_aliases_table(pmu, table: pmu->events_table); |
| 1105 | if (pmu->is_core) |
| 1106 | pmu_add_cpu_aliases_table(pmu, table: perf_pmu__default_core_events_table()); |
| 1107 | |
| 1108 | pmu->cpu_aliases_added = true; |
| 1109 | } |
| 1110 | |
| 1111 | static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe, |
| 1112 | const struct pmu_events_table *table __maybe_unused, |
| 1113 | void *vdata) |
| 1114 | { |
| 1115 | struct perf_pmu *pmu = vdata; |
| 1116 | |
| 1117 | if (!pe->compat || !pe->pmu) { |
| 1118 | /* No data to match. */ |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | if (!perf_pmu__match_wildcard_uncore(pmu_name: pmu->name, to_match: pe->pmu) && |
| 1123 | !perf_pmu__match_wildcard_uncore(pmu_name: pmu->alias_name, to_match: pe->pmu)) { |
| 1124 | /* PMU name/alias_name don't match. */ |
| 1125 | return 0; |
| 1126 | } |
| 1127 | |
| 1128 | if (pmu_uncore_identifier_match(compat: pe->compat, id: pmu->id)) { |
| 1129 | /* Id matched. */ |
| 1130 | perf_pmu__new_alias(pmu, |
| 1131 | name: pe->name, |
| 1132 | desc: pe->desc, |
| 1133 | val: pe->event, |
| 1134 | /*val_fd=*/ -1, |
| 1135 | pe, |
| 1136 | src: EVENT_SRC_SYS_JSON); |
| 1137 | } |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
| 1141 | void pmu_add_sys_aliases(struct perf_pmu *pmu) |
| 1142 | { |
| 1143 | if (!pmu->id) |
| 1144 | return; |
| 1145 | |
| 1146 | pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, pmu); |
| 1147 | } |
| 1148 | |
| 1149 | static char *pmu_find_alias_name(struct perf_pmu *pmu, int dirfd) |
| 1150 | { |
| 1151 | FILE *file = perf_pmu__open_file_at(pmu, dirfd, "alias" ); |
| 1152 | char *line = NULL; |
| 1153 | size_t line_len = 0; |
| 1154 | ssize_t ret; |
| 1155 | |
| 1156 | if (!file) |
| 1157 | return NULL; |
| 1158 | |
| 1159 | ret = getline(&line, &line_len, file); |
| 1160 | if (ret < 0) { |
| 1161 | fclose(file); |
| 1162 | return NULL; |
| 1163 | } |
| 1164 | /* Remove trailing newline. */ |
| 1165 | if (ret > 0 && line[ret - 1] == '\n') |
| 1166 | line[--ret] = '\0'; |
| 1167 | |
| 1168 | fclose(file); |
| 1169 | return line; |
| 1170 | } |
| 1171 | |
| 1172 | static int pmu_max_precise(int dirfd, struct perf_pmu *pmu) |
| 1173 | { |
| 1174 | int max_precise = -1; |
| 1175 | |
| 1176 | perf_pmu__scan_file_at(pmu, dirfd, name: "caps/max_precise" , fmt: "%d" , &max_precise); |
| 1177 | return max_precise; |
| 1178 | } |
| 1179 | |
| 1180 | void __weak |
| 1181 | perf_pmu__arch_init(struct perf_pmu *pmu) |
| 1182 | { |
| 1183 | if (pmu->is_core) |
| 1184 | pmu->mem_events = perf_mem_events; |
| 1185 | } |
| 1186 | |
| 1187 | /* Variant of str_hash that does tolower on each character. */ |
| 1188 | static size_t aliases__hash(long key, void *ctx __maybe_unused) |
| 1189 | { |
| 1190 | const char *s = (const char *)key; |
| 1191 | size_t h = 0; |
| 1192 | |
| 1193 | while (*s) { |
| 1194 | h = h * 31 + tolower(*s); |
| 1195 | s++; |
| 1196 | } |
| 1197 | return h; |
| 1198 | } |
| 1199 | |
| 1200 | static bool aliases__equal(long key1, long key2, void *ctx __maybe_unused) |
| 1201 | { |
| 1202 | return strcasecmp(s1: (const char *)key1, s2: (const char *)key2) == 0; |
| 1203 | } |
| 1204 | |
| 1205 | int perf_pmu__init(struct perf_pmu *pmu, __u32 type, const char *name) |
| 1206 | { |
| 1207 | pmu->type = type; |
| 1208 | INIT_LIST_HEAD(list: &pmu->format); |
| 1209 | INIT_LIST_HEAD(list: &pmu->caps); |
| 1210 | |
| 1211 | pmu->name = strdup(name); |
| 1212 | if (!pmu->name) |
| 1213 | return -ENOMEM; |
| 1214 | |
| 1215 | pmu->aliases = hashmap__new(hash_fn: aliases__hash, equal_fn: aliases__equal, /*ctx=*/ NULL); |
| 1216 | if (!pmu->aliases) |
| 1217 | return -ENOMEM; |
| 1218 | |
| 1219 | return 0; |
| 1220 | } |
| 1221 | |
| 1222 | static __u32 wellknown_pmu_type(const char *pmu_name) |
| 1223 | { |
| 1224 | struct { |
| 1225 | const char *pmu_name; |
| 1226 | __u32 type; |
| 1227 | } wellknown_pmus[] = { |
| 1228 | { |
| 1229 | "software" , |
| 1230 | PERF_TYPE_SOFTWARE |
| 1231 | }, |
| 1232 | { |
| 1233 | "tracepoint" , |
| 1234 | PERF_TYPE_TRACEPOINT |
| 1235 | }, |
| 1236 | { |
| 1237 | "breakpoint" , |
| 1238 | PERF_TYPE_BREAKPOINT |
| 1239 | }, |
| 1240 | }; |
| 1241 | for (size_t i = 0; i < ARRAY_SIZE(wellknown_pmus); i++) { |
| 1242 | if (!strcmp(wellknown_pmus[i].pmu_name, pmu_name)) |
| 1243 | return wellknown_pmus[i].type; |
| 1244 | } |
| 1245 | return PERF_TYPE_MAX; |
| 1246 | } |
| 1247 | |
| 1248 | struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *name, |
| 1249 | bool eager_load) |
| 1250 | { |
| 1251 | struct perf_pmu *pmu; |
| 1252 | |
| 1253 | pmu = zalloc(sizeof(*pmu)); |
| 1254 | if (!pmu) |
| 1255 | return NULL; |
| 1256 | |
| 1257 | if (perf_pmu__init(pmu, type: PERF_PMU_TYPE_FAKE, name) != 0) { |
| 1258 | perf_pmu__delete(pmu); |
| 1259 | return NULL; |
| 1260 | } |
| 1261 | |
| 1262 | /* |
| 1263 | * Read type early to fail fast if a lookup name isn't a PMU. Ensure |
| 1264 | * that type value is successfully assigned (return 1). |
| 1265 | */ |
| 1266 | if (perf_pmu__scan_file_at(pmu, dirfd, name: "type" , fmt: "%u" , &pmu->type) != 1) { |
| 1267 | /* Double check the PMU's name isn't wellknown. */ |
| 1268 | pmu->type = wellknown_pmu_type(pmu_name: name); |
| 1269 | if (pmu->type == PERF_TYPE_MAX) { |
| 1270 | perf_pmu__delete(pmu); |
| 1271 | return NULL; |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | /* |
| 1276 | * The pmu data we store & need consists of the pmu |
| 1277 | * type value and format definitions. Load both right |
| 1278 | * now. |
| 1279 | */ |
| 1280 | if (pmu_format(pmu, dirfd, name, eager_load)) { |
| 1281 | perf_pmu__delete(pmu); |
| 1282 | return NULL; |
| 1283 | } |
| 1284 | |
| 1285 | pmu->is_core = is_pmu_core(name); |
| 1286 | pmu->cpus = pmu_cpumask(dirfd, pmu_name: name, is_core: pmu->is_core); |
| 1287 | |
| 1288 | pmu->is_uncore = pmu_is_uncore(dirfd, name); |
| 1289 | if (pmu->is_uncore) |
| 1290 | pmu->id = pmu_id(name); |
| 1291 | pmu->max_precise = pmu_max_precise(dirfd, pmu); |
| 1292 | pmu->alias_name = pmu_find_alias_name(pmu, dirfd); |
| 1293 | pmu->events_table = perf_pmu__find_events_table(pmu); |
| 1294 | /* |
| 1295 | * Load the sys json events/aliases when loading the PMU as each event |
| 1296 | * may have a different compat regular expression. We therefore can't |
| 1297 | * know the number of sys json events/aliases without computing the |
| 1298 | * regular expressions for them all. |
| 1299 | */ |
| 1300 | pmu_add_sys_aliases(pmu); |
| 1301 | list_add_tail(new: &pmu->list, head: pmus); |
| 1302 | |
| 1303 | perf_pmu__arch_init(pmu); |
| 1304 | |
| 1305 | if (eager_load) |
| 1306 | pmu_aliases_parse_eager(pmu, sysfs_fd: dirfd); |
| 1307 | |
| 1308 | return pmu; |
| 1309 | } |
| 1310 | |
| 1311 | /* Creates the PMU when sysfs scanning fails. */ |
| 1312 | struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus) |
| 1313 | { |
| 1314 | struct perf_pmu *pmu = zalloc(sizeof(*pmu)); |
| 1315 | |
| 1316 | if (!pmu) |
| 1317 | return NULL; |
| 1318 | |
| 1319 | pmu->name = strdup("cpu" ); |
| 1320 | if (!pmu->name) { |
| 1321 | free(pmu); |
| 1322 | return NULL; |
| 1323 | } |
| 1324 | |
| 1325 | pmu->is_core = true; |
| 1326 | pmu->type = PERF_TYPE_RAW; |
| 1327 | pmu->cpus = cpu_map__online(); |
| 1328 | |
| 1329 | INIT_LIST_HEAD(list: &pmu->format); |
| 1330 | pmu->aliases = hashmap__new(hash_fn: aliases__hash, equal_fn: aliases__equal, /*ctx=*/ NULL); |
| 1331 | INIT_LIST_HEAD(list: &pmu->caps); |
| 1332 | list_add_tail(new: &pmu->list, head: core_pmus); |
| 1333 | return pmu; |
| 1334 | } |
| 1335 | |
| 1336 | bool perf_pmu__is_fake(const struct perf_pmu *pmu) |
| 1337 | { |
| 1338 | return pmu->type == PERF_PMU_TYPE_FAKE; |
| 1339 | } |
| 1340 | |
| 1341 | void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu) |
| 1342 | { |
| 1343 | struct perf_pmu_format *format; |
| 1344 | |
| 1345 | if (pmu->formats_checked) |
| 1346 | return; |
| 1347 | |
| 1348 | pmu->formats_checked = true; |
| 1349 | |
| 1350 | /* fake pmu doesn't have format list */ |
| 1351 | if (perf_pmu__is_fake(pmu)) |
| 1352 | return; |
| 1353 | |
| 1354 | list_for_each_entry(format, &pmu->format, list) { |
| 1355 | perf_pmu_format__load(pmu, format); |
| 1356 | if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) { |
| 1357 | pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'" |
| 1358 | "which is not supported by this version of perf!\n" , |
| 1359 | pmu->name, format->name, format->value); |
| 1360 | return; |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | bool evsel__is_aux_event(const struct evsel *evsel) |
| 1366 | { |
| 1367 | struct perf_pmu *pmu; |
| 1368 | |
| 1369 | if (evsel->needs_auxtrace_mmap) |
| 1370 | return true; |
| 1371 | |
| 1372 | pmu = evsel__find_pmu(evsel); |
| 1373 | return pmu && pmu->auxtrace; |
| 1374 | } |
| 1375 | |
| 1376 | /* |
| 1377 | * Set @config_name to @val as long as the user hasn't already set or cleared it |
| 1378 | * by passing a config term on the command line. |
| 1379 | * |
| 1380 | * @val is the value to put into the bits specified by @config_name rather than |
| 1381 | * the bit pattern. It is shifted into position by this function, so to set |
| 1382 | * something to true, pass 1 for val rather than a pre shifted value. |
| 1383 | */ |
| 1384 | #define field_prep(_mask, _val) (((_val) << (ffsll(_mask) - 1)) & (_mask)) |
| 1385 | void evsel__set_config_if_unset(struct perf_pmu *pmu, struct evsel *evsel, |
| 1386 | const char *config_name, u64 val) |
| 1387 | { |
| 1388 | u64 user_bits = 0, bits; |
| 1389 | struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG); |
| 1390 | |
| 1391 | if (term) |
| 1392 | user_bits = term->val.cfg_chg; |
| 1393 | |
| 1394 | bits = perf_pmu__format_bits(pmu, name: config_name); |
| 1395 | |
| 1396 | /* Do nothing if the user changed the value */ |
| 1397 | if (bits & user_bits) |
| 1398 | return; |
| 1399 | |
| 1400 | /* Otherwise replace it */ |
| 1401 | evsel->core.attr.config &= ~bits; |
| 1402 | evsel->core.attr.config |= field_prep(bits, val); |
| 1403 | } |
| 1404 | |
| 1405 | static struct perf_pmu_format * |
| 1406 | pmu_find_format(const struct list_head *formats, const char *name) |
| 1407 | { |
| 1408 | struct perf_pmu_format *format; |
| 1409 | |
| 1410 | list_for_each_entry(format, formats, list) |
| 1411 | if (!strcmp(format->name, name)) |
| 1412 | return format; |
| 1413 | |
| 1414 | return NULL; |
| 1415 | } |
| 1416 | |
| 1417 | __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name) |
| 1418 | { |
| 1419 | struct perf_pmu_format *format = pmu_find_format(formats: &pmu->format, name); |
| 1420 | __u64 bits = 0; |
| 1421 | int fbit; |
| 1422 | |
| 1423 | if (!format) |
| 1424 | return 0; |
| 1425 | |
| 1426 | for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS) |
| 1427 | bits |= 1ULL << fbit; |
| 1428 | |
| 1429 | return bits; |
| 1430 | } |
| 1431 | |
| 1432 | int perf_pmu__format_type(struct perf_pmu *pmu, const char *name) |
| 1433 | { |
| 1434 | struct perf_pmu_format *format = pmu_find_format(formats: &pmu->format, name); |
| 1435 | |
| 1436 | if (!format) |
| 1437 | return -1; |
| 1438 | |
| 1439 | perf_pmu_format__load(pmu, format); |
| 1440 | return format->value; |
| 1441 | } |
| 1442 | |
| 1443 | /* |
| 1444 | * Sets value based on the format definition (format parameter) |
| 1445 | * and unformatted value (value parameter). |
| 1446 | */ |
| 1447 | static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, |
| 1448 | bool zero) |
| 1449 | { |
| 1450 | unsigned long fbit, vbit; |
| 1451 | |
| 1452 | for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { |
| 1453 | |
| 1454 | if (!test_bit(fbit, format)) |
| 1455 | continue; |
| 1456 | |
| 1457 | if (value & (1llu << vbit++)) |
| 1458 | *v |= (1llu << fbit); |
| 1459 | else if (zero) |
| 1460 | *v &= ~(1llu << fbit); |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | static __u64 pmu_format_max_value(const unsigned long *format) |
| 1465 | { |
| 1466 | int w; |
| 1467 | |
| 1468 | w = bitmap_weight(src: format, PERF_PMU_FORMAT_BITS); |
| 1469 | if (!w) |
| 1470 | return 0; |
| 1471 | if (w < 64) |
| 1472 | return (1ULL << w) - 1; |
| 1473 | return -1; |
| 1474 | } |
| 1475 | |
| 1476 | /* |
| 1477 | * Term is a string term, and might be a param-term. Try to look up it's value |
| 1478 | * in the remaining terms. |
| 1479 | * - We have a term like "base-or-format-term=param-term", |
| 1480 | * - We need to find the value supplied for "param-term" (with param-term named |
| 1481 | * in a config string) later on in the term list. |
| 1482 | */ |
| 1483 | static int pmu_resolve_param_term(struct parse_events_term *term, |
| 1484 | struct parse_events_terms *head_terms, |
| 1485 | __u64 *value) |
| 1486 | { |
| 1487 | struct parse_events_term *t; |
| 1488 | |
| 1489 | list_for_each_entry(t, &head_terms->terms, list) { |
| 1490 | if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM && |
| 1491 | t->config && !strcmp(t->config, term->config)) { |
| 1492 | t->used = true; |
| 1493 | *value = t->val.num; |
| 1494 | return 0; |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | if (verbose > 0) |
| 1499 | printf("Required parameter '%s' not specified\n" , term->config); |
| 1500 | |
| 1501 | return -1; |
| 1502 | } |
| 1503 | |
| 1504 | static char *pmu_formats_string(const struct list_head *formats) |
| 1505 | { |
| 1506 | struct perf_pmu_format *format; |
| 1507 | char *str = NULL; |
| 1508 | struct strbuf buf = STRBUF_INIT; |
| 1509 | unsigned int i = 0; |
| 1510 | |
| 1511 | if (!formats) |
| 1512 | return NULL; |
| 1513 | |
| 1514 | /* sysfs exported terms */ |
| 1515 | list_for_each_entry(format, formats, list) |
| 1516 | if (strbuf_addf(sb: &buf, fmt: i++ ? ",%s" : "%s" , format->name) < 0) |
| 1517 | goto error; |
| 1518 | |
| 1519 | str = strbuf_detach(buf: &buf, NULL); |
| 1520 | error: |
| 1521 | strbuf_release(buf: &buf); |
| 1522 | |
| 1523 | return str; |
| 1524 | } |
| 1525 | |
| 1526 | /* |
| 1527 | * Setup one of config[12] attr members based on the |
| 1528 | * user input data - term parameter. |
| 1529 | */ |
| 1530 | static int pmu_config_term(const struct perf_pmu *pmu, |
| 1531 | struct perf_event_attr *attr, |
| 1532 | struct parse_events_term *term, |
| 1533 | struct parse_events_terms *head_terms, |
| 1534 | bool zero, bool apply_hardcoded, |
| 1535 | struct parse_events_error *err) |
| 1536 | { |
| 1537 | struct perf_pmu_format *format; |
| 1538 | __u64 *vp; |
| 1539 | __u64 val, max_val; |
| 1540 | |
| 1541 | /* |
| 1542 | * If this is a parameter we've already used for parameterized-eval, |
| 1543 | * skip it in normal eval. |
| 1544 | */ |
| 1545 | if (term->used) |
| 1546 | return 0; |
| 1547 | |
| 1548 | /* |
| 1549 | * Hardcoded terms are generally handled in event parsing, which |
| 1550 | * traditionally have had to handle not having a PMU. An alias may |
| 1551 | * have hard coded config values, optionally apply them below. |
| 1552 | */ |
| 1553 | if (parse_events__is_hardcoded_term(term)) { |
| 1554 | /* Config terms set all bits in the config. */ |
| 1555 | DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS); |
| 1556 | |
| 1557 | if (!apply_hardcoded) |
| 1558 | return 0; |
| 1559 | |
| 1560 | bitmap_fill(dst: bits, PERF_PMU_FORMAT_BITS); |
| 1561 | |
| 1562 | switch (term->type_term) { |
| 1563 | case PARSE_EVENTS__TERM_TYPE_CONFIG: |
| 1564 | assert(term->type_val == PARSE_EVENTS__TERM_TYPE_NUM); |
| 1565 | pmu_format_value(format: bits, value: term->val.num, v: &attr->config, zero); |
| 1566 | break; |
| 1567 | case PARSE_EVENTS__TERM_TYPE_CONFIG1: |
| 1568 | assert(term->type_val == PARSE_EVENTS__TERM_TYPE_NUM); |
| 1569 | pmu_format_value(format: bits, value: term->val.num, v: &attr->config1, zero); |
| 1570 | break; |
| 1571 | case PARSE_EVENTS__TERM_TYPE_CONFIG2: |
| 1572 | assert(term->type_val == PARSE_EVENTS__TERM_TYPE_NUM); |
| 1573 | pmu_format_value(format: bits, value: term->val.num, v: &attr->config2, zero); |
| 1574 | break; |
| 1575 | case PARSE_EVENTS__TERM_TYPE_CONFIG3: |
| 1576 | assert(term->type_val == PARSE_EVENTS__TERM_TYPE_NUM); |
| 1577 | pmu_format_value(format: bits, value: term->val.num, v: &attr->config3, zero); |
| 1578 | break; |
| 1579 | case PARSE_EVENTS__TERM_TYPE_CONFIG4: |
| 1580 | assert(term->type_val == PARSE_EVENTS__TERM_TYPE_NUM); |
| 1581 | pmu_format_value(format: bits, value: term->val.num, v: &attr->config4, zero); |
| 1582 | break; |
| 1583 | case PARSE_EVENTS__TERM_TYPE_LEGACY_HARDWARE_CONFIG: |
| 1584 | assert(term->type_val == PARSE_EVENTS__TERM_TYPE_NUM); |
| 1585 | assert(term->val.num < PERF_COUNT_HW_MAX); |
| 1586 | assert(pmu->is_core); |
| 1587 | attr->config = term->val.num; |
| 1588 | if (perf_pmus__supports_extended_type()) |
| 1589 | attr->config |= (__u64)pmu->type << PERF_PMU_TYPE_SHIFT; |
| 1590 | attr->type = PERF_TYPE_HARDWARE; |
| 1591 | break; |
| 1592 | case PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE_CONFIG: { |
| 1593 | #ifndef NDEBUG |
| 1594 | int cache_type = term->val.num & 0xFF; |
| 1595 | int cache_op = (term->val.num >> 8) & 0xFF; |
| 1596 | int cache_result = (term->val.num >> 16) & 0xFF; |
| 1597 | |
| 1598 | assert(cache_type < PERF_COUNT_HW_CACHE_MAX); |
| 1599 | assert(cache_op < PERF_COUNT_HW_CACHE_OP_MAX); |
| 1600 | assert(cache_result < PERF_COUNT_HW_CACHE_RESULT_MAX); |
| 1601 | #endif |
| 1602 | assert(term->type_val == PARSE_EVENTS__TERM_TYPE_NUM); |
| 1603 | assert((term->val.num & ~0xFFFFFF) == 0); |
| 1604 | assert(pmu->is_core); |
| 1605 | attr->config = term->val.num; |
| 1606 | if (perf_pmus__supports_extended_type()) |
| 1607 | attr->config |= (__u64)pmu->type << PERF_PMU_TYPE_SHIFT; |
| 1608 | attr->type = PERF_TYPE_HW_CACHE; |
| 1609 | break; |
| 1610 | } |
| 1611 | case PARSE_EVENTS__TERM_TYPE_USER: /* Not hardcoded. */ |
| 1612 | return -EINVAL; |
| 1613 | case PARSE_EVENTS__TERM_TYPE_NAME ... PARSE_EVENTS__TERM_TYPE_RATIO_TO_PREV: |
| 1614 | /* Skip non-config terms. */ |
| 1615 | break; |
| 1616 | default: |
| 1617 | break; |
| 1618 | } |
| 1619 | return 0; |
| 1620 | } |
| 1621 | |
| 1622 | format = pmu_find_format(formats: &pmu->format, name: term->config); |
| 1623 | if (!format) { |
| 1624 | char *pmu_term = pmu_formats_string(formats: &pmu->format); |
| 1625 | char *unknown_term; |
| 1626 | char *help_msg; |
| 1627 | |
| 1628 | if (asprintf(&unknown_term, |
| 1629 | "unknown term '%s' for pmu '%s'" , |
| 1630 | term->config, pmu->name) < 0) |
| 1631 | unknown_term = NULL; |
| 1632 | help_msg = parse_events_formats_error_string(additional_terms: pmu_term); |
| 1633 | if (err) { |
| 1634 | parse_events_error__handle(err, idx: term->err_term, |
| 1635 | str: unknown_term, |
| 1636 | help: help_msg); |
| 1637 | } else { |
| 1638 | pr_debug("%s (%s)\n" , unknown_term, help_msg); |
| 1639 | free(unknown_term); |
| 1640 | } |
| 1641 | free(pmu_term); |
| 1642 | return -EINVAL; |
| 1643 | } |
| 1644 | perf_pmu_format__load(pmu, format); |
| 1645 | switch (format->value) { |
| 1646 | case PERF_PMU_FORMAT_VALUE_CONFIG: |
| 1647 | vp = &attr->config; |
| 1648 | break; |
| 1649 | case PERF_PMU_FORMAT_VALUE_CONFIG1: |
| 1650 | vp = &attr->config1; |
| 1651 | break; |
| 1652 | case PERF_PMU_FORMAT_VALUE_CONFIG2: |
| 1653 | vp = &attr->config2; |
| 1654 | break; |
| 1655 | case PERF_PMU_FORMAT_VALUE_CONFIG3: |
| 1656 | vp = &attr->config3; |
| 1657 | break; |
| 1658 | case PERF_PMU_FORMAT_VALUE_CONFIG4: |
| 1659 | vp = &attr->config4; |
| 1660 | break; |
| 1661 | default: |
| 1662 | return -EINVAL; |
| 1663 | } |
| 1664 | |
| 1665 | /* |
| 1666 | * Either directly use a numeric term, or try to translate string terms |
| 1667 | * using event parameters. |
| 1668 | */ |
| 1669 | if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { |
| 1670 | if (term->no_value && |
| 1671 | bitmap_weight(src: format->bits, PERF_PMU_FORMAT_BITS) > 1) { |
| 1672 | if (err) { |
| 1673 | parse_events_error__handle(err, idx: term->err_val, |
| 1674 | str: strdup("no value assigned for term" ), |
| 1675 | NULL); |
| 1676 | } |
| 1677 | return -EINVAL; |
| 1678 | } |
| 1679 | |
| 1680 | val = term->val.num; |
| 1681 | } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { |
| 1682 | if (strcmp(term->val.str, "?" )) { |
| 1683 | if (verbose > 0) { |
| 1684 | pr_info("Invalid sysfs entry %s=%s\n" , |
| 1685 | term->config, term->val.str); |
| 1686 | } |
| 1687 | if (err) { |
| 1688 | parse_events_error__handle(err, idx: term->err_val, |
| 1689 | str: strdup("expected numeric value" ), |
| 1690 | NULL); |
| 1691 | } |
| 1692 | return -EINVAL; |
| 1693 | } |
| 1694 | |
| 1695 | if (pmu_resolve_param_term(term, head_terms, value: &val)) |
| 1696 | return -EINVAL; |
| 1697 | } else |
| 1698 | return -EINVAL; |
| 1699 | |
| 1700 | max_val = pmu_format_max_value(format: format->bits); |
| 1701 | if (val > max_val) { |
| 1702 | if (err) { |
| 1703 | char *err_str; |
| 1704 | |
| 1705 | if (asprintf(&err_str, |
| 1706 | "value too big for format (%s), maximum is %llu" , |
| 1707 | format->name, (unsigned long long)max_val) < 0) { |
| 1708 | err_str = strdup("value too big for format" ); |
| 1709 | } |
| 1710 | parse_events_error__handle(err, idx: term->err_val, str: err_str, /*help=*/NULL); |
| 1711 | return -EINVAL; |
| 1712 | } |
| 1713 | /* |
| 1714 | * Assume we don't care if !err, in which case the value will be |
| 1715 | * silently truncated. |
| 1716 | */ |
| 1717 | } |
| 1718 | |
| 1719 | pmu_format_value(format: format->bits, value: val, v: vp, zero); |
| 1720 | return 0; |
| 1721 | } |
| 1722 | |
| 1723 | int perf_pmu__config_terms(const struct perf_pmu *pmu, |
| 1724 | struct perf_event_attr *attr, |
| 1725 | struct parse_events_terms *terms, |
| 1726 | bool zero, bool apply_hardcoded, |
| 1727 | struct parse_events_error *err) |
| 1728 | { |
| 1729 | struct parse_events_term *term; |
| 1730 | |
| 1731 | if (perf_pmu__is_hwmon(pmu)) |
| 1732 | return hwmon_pmu__config_terms(pmu, attr, terms, err); |
| 1733 | if (perf_pmu__is_drm(pmu)) |
| 1734 | return drm_pmu__config_terms(pmu, attr, terms, err); |
| 1735 | |
| 1736 | list_for_each_entry(term, &terms->terms, list) { |
| 1737 | if (pmu_config_term(pmu, attr, term, head_terms: terms, zero, apply_hardcoded, err)) |
| 1738 | return -EINVAL; |
| 1739 | } |
| 1740 | |
| 1741 | return 0; |
| 1742 | } |
| 1743 | |
| 1744 | /* |
| 1745 | * Configures event's 'attr' parameter based on the: |
| 1746 | * 1) users input - specified in terms parameter |
| 1747 | * 2) pmu format definitions - specified by pmu parameter |
| 1748 | */ |
| 1749 | int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, |
| 1750 | struct parse_events_terms *head_terms, |
| 1751 | bool apply_hardcoded, |
| 1752 | struct parse_events_error *err) |
| 1753 | { |
| 1754 | bool zero = !!pmu->perf_event_attr_init_default; |
| 1755 | |
| 1756 | /* Fake PMU doesn't have proper terms so nothing to configure in attr. */ |
| 1757 | if (perf_pmu__is_fake(pmu)) |
| 1758 | return 0; |
| 1759 | |
| 1760 | return perf_pmu__config_terms(pmu, attr, terms: head_terms, zero, apply_hardcoded, err); |
| 1761 | } |
| 1762 | |
| 1763 | static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, |
| 1764 | struct parse_events_term *term) |
| 1765 | { |
| 1766 | struct perf_pmu_alias *alias; |
| 1767 | const char *name; |
| 1768 | |
| 1769 | if (parse_events__is_hardcoded_term(term)) |
| 1770 | return NULL; |
| 1771 | |
| 1772 | if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { |
| 1773 | if (!term->no_value) |
| 1774 | return NULL; |
| 1775 | if (pmu_find_format(formats: &pmu->format, name: term->config)) |
| 1776 | return NULL; |
| 1777 | name = term->config; |
| 1778 | |
| 1779 | } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { |
| 1780 | if (strcasecmp(s1: term->config, s2: "event" )) |
| 1781 | return NULL; |
| 1782 | name = term->val.str; |
| 1783 | } else { |
| 1784 | return NULL; |
| 1785 | } |
| 1786 | |
| 1787 | alias = perf_pmu__find_alias(pmu, name, /*load=*/ true); |
| 1788 | if (alias || pmu->cpu_aliases_added) |
| 1789 | return alias; |
| 1790 | |
| 1791 | /* Alias doesn't exist, try to get it from the json events. */ |
| 1792 | if ((pmu_events_table__find_event(pmu->events_table, pmu, name, |
| 1793 | pmu_add_cpu_aliases_map_callback, |
| 1794 | pmu) == 0) || |
| 1795 | (pmu->is_core && |
| 1796 | pmu_events_table__find_event(perf_pmu__default_core_events_table(), |
| 1797 | pmu, name, |
| 1798 | pmu_add_cpu_aliases_map_callback, |
| 1799 | pmu) == 0)) { |
| 1800 | alias = perf_pmu__find_alias(pmu, name, /*load=*/ false); |
| 1801 | } |
| 1802 | return alias; |
| 1803 | } |
| 1804 | |
| 1805 | |
| 1806 | static int check_info_data(struct perf_pmu *pmu, |
| 1807 | struct perf_pmu_alias *alias, |
| 1808 | struct perf_pmu_info *info, |
| 1809 | struct parse_events_error *err, |
| 1810 | int column) |
| 1811 | { |
| 1812 | read_alias_info(pmu, alias); |
| 1813 | /* |
| 1814 | * Only one term in event definition can |
| 1815 | * define unit, scale and snapshot, fail |
| 1816 | * if there's more than one. |
| 1817 | */ |
| 1818 | if (info->unit && alias->unit[0]) { |
| 1819 | parse_events_error__handle(err, idx: column, |
| 1820 | str: strdup("Attempt to set event's unit twice" ), |
| 1821 | NULL); |
| 1822 | return -EINVAL; |
| 1823 | } |
| 1824 | if (info->scale && alias->scale) { |
| 1825 | parse_events_error__handle(err, idx: column, |
| 1826 | str: strdup("Attempt to set event's scale twice" ), |
| 1827 | NULL); |
| 1828 | return -EINVAL; |
| 1829 | } |
| 1830 | if (info->snapshot && alias->snapshot) { |
| 1831 | parse_events_error__handle(err, idx: column, |
| 1832 | str: strdup("Attempt to set event snapshot twice" ), |
| 1833 | NULL); |
| 1834 | return -EINVAL; |
| 1835 | } |
| 1836 | |
| 1837 | if (alias->unit[0]) |
| 1838 | info->unit = alias->unit; |
| 1839 | |
| 1840 | if (alias->scale) |
| 1841 | info->scale = alias->scale; |
| 1842 | |
| 1843 | if (alias->snapshot) |
| 1844 | info->snapshot = alias->snapshot; |
| 1845 | |
| 1846 | return 0; |
| 1847 | } |
| 1848 | |
| 1849 | static int perf_pmu__parse_terms_to_attr(struct perf_pmu *pmu, const char *terms_str, |
| 1850 | struct perf_event_attr *attr) |
| 1851 | { |
| 1852 | struct parse_events_terms terms; |
| 1853 | int ret; |
| 1854 | |
| 1855 | parse_events_terms__init(terms: &terms); |
| 1856 | ret = parse_events_terms(terms: &terms, str: terms_str); |
| 1857 | if (ret) { |
| 1858 | pr_debug("Failed to parse terms '%s': %d\n" , terms_str, ret); |
| 1859 | parse_events_terms__exit(terms: &terms); |
| 1860 | return ret; |
| 1861 | } |
| 1862 | ret = perf_pmu__config(pmu, attr, head_terms: &terms, /*apply_hardcoded=*/true, /*err=*/NULL); |
| 1863 | parse_events_terms__exit(terms: &terms); |
| 1864 | return ret; |
| 1865 | } |
| 1866 | |
| 1867 | /* |
| 1868 | * Find alias in the terms list and replace it with the terms |
| 1869 | * defined for the alias |
| 1870 | */ |
| 1871 | int perf_pmu__check_alias(struct perf_pmu *pmu, struct parse_events_terms *head_terms, |
| 1872 | struct perf_pmu_info *info, bool *rewrote_terms, |
| 1873 | u64 *alternate_hw_config, struct parse_events_error *err) |
| 1874 | { |
| 1875 | struct parse_events_term *term, *h; |
| 1876 | struct perf_pmu_alias *alias; |
| 1877 | int ret; |
| 1878 | |
| 1879 | *rewrote_terms = false; |
| 1880 | info->per_pkg = false; |
| 1881 | |
| 1882 | /* |
| 1883 | * Mark unit and scale as not set |
| 1884 | * (different from default values, see below) |
| 1885 | */ |
| 1886 | info->unit = NULL; |
| 1887 | info->scale = 0.0; |
| 1888 | info->snapshot = false; |
| 1889 | info->retirement_latency_mean = 0.0; |
| 1890 | info->retirement_latency_min = 0.0; |
| 1891 | info->retirement_latency_max = 0.0; |
| 1892 | |
| 1893 | if (perf_pmu__is_hwmon(pmu)) { |
| 1894 | ret = hwmon_pmu__check_alias(terms: head_terms, info, err); |
| 1895 | goto out; |
| 1896 | } |
| 1897 | if (perf_pmu__is_drm(pmu)) { |
| 1898 | ret = drm_pmu__check_alias(pmu, terms: head_terms, info, err); |
| 1899 | goto out; |
| 1900 | } |
| 1901 | |
| 1902 | /* Fake PMU doesn't rewrite terms. */ |
| 1903 | if (perf_pmu__is_fake(pmu)) |
| 1904 | goto out; |
| 1905 | |
| 1906 | list_for_each_entry_safe(term, h, &head_terms->terms, list) { |
| 1907 | alias = pmu_find_alias(pmu, term); |
| 1908 | if (!alias) |
| 1909 | continue; |
| 1910 | ret = pmu_alias_terms(alias, terms: &term->list); |
| 1911 | if (ret) { |
| 1912 | parse_events_error__handle(err, idx: term->err_term, |
| 1913 | str: strdup("Failed to parse terms" ), |
| 1914 | NULL); |
| 1915 | return ret; |
| 1916 | } |
| 1917 | |
| 1918 | *rewrote_terms = true; |
| 1919 | ret = check_info_data(pmu, alias, info, err, column: term->err_term); |
| 1920 | if (ret) |
| 1921 | return ret; |
| 1922 | |
| 1923 | if (alias->legacy_terms) { |
| 1924 | struct perf_event_attr attr = {.config = 0,}; |
| 1925 | |
| 1926 | ret = perf_pmu__parse_terms_to_attr(pmu, terms_str: alias->legacy_terms, attr: &attr); |
| 1927 | if (ret) { |
| 1928 | parse_events_error__handle(err, idx: term->err_term, |
| 1929 | str: strdup("Error evaluating legacy terms" ), |
| 1930 | NULL); |
| 1931 | return ret; |
| 1932 | } |
| 1933 | if (attr.type == PERF_TYPE_HARDWARE) |
| 1934 | *alternate_hw_config = attr.config & PERF_HW_EVENT_MASK; |
| 1935 | } |
| 1936 | |
| 1937 | if (alias->per_pkg) |
| 1938 | info->per_pkg = true; |
| 1939 | |
| 1940 | info->retirement_latency_mean = alias->retirement_latency_mean; |
| 1941 | info->retirement_latency_min = alias->retirement_latency_min; |
| 1942 | info->retirement_latency_max = alias->retirement_latency_max; |
| 1943 | |
| 1944 | list_del_init(entry: &term->list); |
| 1945 | parse_events_term__delete(term); |
| 1946 | } |
| 1947 | out: |
| 1948 | /* |
| 1949 | * if no unit or scale found in aliases, then |
| 1950 | * set defaults as for evsel |
| 1951 | * unit cannot left to NULL |
| 1952 | */ |
| 1953 | if (info->unit == NULL) |
| 1954 | info->unit = "" ; |
| 1955 | |
| 1956 | if (info->scale == 0.0) |
| 1957 | info->scale = 1.0; |
| 1958 | |
| 1959 | return 0; |
| 1960 | } |
| 1961 | |
| 1962 | struct find_event_args { |
| 1963 | const char *event; |
| 1964 | void *state; |
| 1965 | pmu_event_callback cb; |
| 1966 | }; |
| 1967 | |
| 1968 | static int find_event_callback(void *state, struct pmu_event_info *info) |
| 1969 | { |
| 1970 | struct find_event_args *args = state; |
| 1971 | |
| 1972 | if (!strcmp(args->event, info->name)) |
| 1973 | return args->cb(args->state, info); |
| 1974 | |
| 1975 | return 0; |
| 1976 | } |
| 1977 | |
| 1978 | int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, pmu_event_callback cb) |
| 1979 | { |
| 1980 | struct find_event_args args = { |
| 1981 | .event = event, |
| 1982 | .state = state, |
| 1983 | .cb = cb, |
| 1984 | }; |
| 1985 | |
| 1986 | /* Sub-optimal, but function is only used by tests. */ |
| 1987 | return perf_pmu__for_each_event(pmu, /*skip_duplicate_pmus=*/ false, |
| 1988 | state: &args, cb: find_event_callback); |
| 1989 | } |
| 1990 | |
| 1991 | static void perf_pmu__del_formats(struct list_head *formats) |
| 1992 | { |
| 1993 | struct perf_pmu_format *fmt, *tmp; |
| 1994 | |
| 1995 | list_for_each_entry_safe(fmt, tmp, formats, list) { |
| 1996 | list_del(entry: &fmt->list); |
| 1997 | zfree(&fmt->name); |
| 1998 | free(fmt); |
| 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name) |
| 2003 | { |
| 2004 | struct perf_pmu_format *format; |
| 2005 | |
| 2006 | list_for_each_entry(format, &pmu->format, list) { |
| 2007 | if (!strcmp(format->name, name)) |
| 2008 | return true; |
| 2009 | } |
| 2010 | return false; |
| 2011 | } |
| 2012 | |
| 2013 | int perf_pmu__for_each_format(struct perf_pmu *pmu, void *state, pmu_format_callback cb) |
| 2014 | { |
| 2015 | static const char *const terms[] = { |
| 2016 | "config=0..0xffffffffffffffff" , |
| 2017 | "config1=0..0xffffffffffffffff" , |
| 2018 | "config2=0..0xffffffffffffffff" , |
| 2019 | "config3=0..0xffffffffffffffff" , |
| 2020 | "config4=0..0xffffffffffffffff" , |
| 2021 | "legacy-hardware-config=0..9," , |
| 2022 | "legacy-cache-config=0..0xffffff," , |
| 2023 | "name=string" , |
| 2024 | "period=number" , |
| 2025 | "freq=number" , |
| 2026 | "branch_type=(u|k|hv|any|...)" , |
| 2027 | "time" , |
| 2028 | "call-graph=(fp|dwarf|lbr)" , |
| 2029 | "stack-size=number" , |
| 2030 | "max-stack=number" , |
| 2031 | "nr=number" , |
| 2032 | "inherit" , |
| 2033 | "no-inherit" , |
| 2034 | "overwrite" , |
| 2035 | "no-overwrite" , |
| 2036 | "percore" , |
| 2037 | "aux-output" , |
| 2038 | "aux-action=(pause|resume|start-paused)" , |
| 2039 | "aux-sample-size=number" , |
| 2040 | "cpu=number" , |
| 2041 | "ratio-to-prev=string" , |
| 2042 | }; |
| 2043 | struct perf_pmu_format *format; |
| 2044 | int ret; |
| 2045 | |
| 2046 | /* |
| 2047 | * max-events and driver-config are missing above as are the internal |
| 2048 | * types user, metric-id, and raw. Assert against the enum |
| 2049 | * parse_events__term_type so they are kept in sync. |
| 2050 | */ |
| 2051 | _Static_assert(ARRAY_SIZE(terms) == __PARSE_EVENTS__TERM_TYPE_NR - 4, |
| 2052 | "perf_pmu__for_each_format()'s terms must be kept in sync with enum parse_events__term_type" ); |
| 2053 | list_for_each_entry(format, &pmu->format, list) { |
| 2054 | perf_pmu_format__load(pmu, format); |
| 2055 | ret = cb(state, format->name, (int)format->value, format->bits); |
| 2056 | if (ret) |
| 2057 | return ret; |
| 2058 | } |
| 2059 | if (!pmu->is_core) |
| 2060 | return 0; |
| 2061 | |
| 2062 | for (size_t i = 0; i < ARRAY_SIZE(terms); i++) { |
| 2063 | int config = PERF_PMU_FORMAT_VALUE_CONFIG; |
| 2064 | |
| 2065 | if (i < PERF_PMU_FORMAT_VALUE_CONFIG_END) |
| 2066 | config = i; |
| 2067 | |
| 2068 | ret = cb(state, terms[i], config, /*bits=*/NULL); |
| 2069 | if (ret) |
| 2070 | return ret; |
| 2071 | } |
| 2072 | return 0; |
| 2073 | } |
| 2074 | |
| 2075 | bool is_pmu_core(const char *name) |
| 2076 | { |
| 2077 | return !strcmp(name, "cpu" ) || !strcmp(name, "cpum_cf" ) || is_sysfs_pmu_core(name); |
| 2078 | } |
| 2079 | |
| 2080 | bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu) |
| 2081 | { |
| 2082 | return pmu->is_core; |
| 2083 | } |
| 2084 | |
| 2085 | bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu) |
| 2086 | { |
| 2087 | return !pmu->is_core || perf_pmus__num_core_pmus() == 1; |
| 2088 | } |
| 2089 | |
| 2090 | bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name) |
| 2091 | { |
| 2092 | if (!name) |
| 2093 | return false; |
| 2094 | if (perf_pmu__is_tool(pmu) && tool_pmu__skip_event(name)) |
| 2095 | return false; |
| 2096 | if (perf_pmu__is_tracepoint(pmu)) |
| 2097 | return tp_pmu__have_event(pmu, name); |
| 2098 | if (perf_pmu__is_hwmon(pmu)) |
| 2099 | return hwmon_pmu__have_event(pmu, name); |
| 2100 | if (perf_pmu__is_drm(pmu)) |
| 2101 | return drm_pmu__have_event(pmu, name); |
| 2102 | if (perf_pmu__find_alias(pmu, name, /*load=*/ true) != NULL) |
| 2103 | return true; |
| 2104 | if (pmu->cpu_aliases_added || (!pmu->events_table && !pmu->is_core)) |
| 2105 | return false; |
| 2106 | if (pmu_events_table__find_event(pmu->events_table, pmu, name, NULL, NULL) == 0) |
| 2107 | return true; |
| 2108 | return pmu->is_core && |
| 2109 | pmu_events_table__find_event(perf_pmu__default_core_events_table(), |
| 2110 | pmu, name, NULL, NULL) == 0; |
| 2111 | } |
| 2112 | |
| 2113 | size_t perf_pmu__num_events(struct perf_pmu *pmu) |
| 2114 | { |
| 2115 | size_t nr; |
| 2116 | |
| 2117 | if (perf_pmu__is_tracepoint(pmu)) |
| 2118 | return tp_pmu__num_events(pmu); |
| 2119 | if (perf_pmu__is_hwmon(pmu)) |
| 2120 | return hwmon_pmu__num_events(pmu); |
| 2121 | if (perf_pmu__is_drm(pmu)) |
| 2122 | return drm_pmu__num_events(pmu); |
| 2123 | |
| 2124 | pmu_aliases_parse(pmu); |
| 2125 | nr = pmu->sysfs_aliases + pmu->sys_json_aliases; |
| 2126 | |
| 2127 | if (pmu->cpu_aliases_added) { |
| 2128 | nr += pmu->cpu_json_aliases; |
| 2129 | } else if (pmu->events_table || pmu->is_core) { |
| 2130 | nr += pmu_events_table__num_events(pmu->events_table, pmu); |
| 2131 | if (pmu->is_core) { |
| 2132 | nr += pmu_events_table__num_events( |
| 2133 | perf_pmu__default_core_events_table(), pmu); |
| 2134 | } |
| 2135 | nr -= pmu->cpu_common_json_aliases; |
| 2136 | } else { |
| 2137 | assert(pmu->cpu_json_aliases == 0 && pmu->cpu_common_json_aliases == 0); |
| 2138 | } |
| 2139 | |
| 2140 | if (perf_pmu__is_tool(pmu)) |
| 2141 | nr -= tool_pmu__num_skip_events(); |
| 2142 | |
| 2143 | return pmu->selectable ? nr + 1 : nr; |
| 2144 | } |
| 2145 | |
| 2146 | static int sub_non_neg(int a, int b) |
| 2147 | { |
| 2148 | if (b > a) |
| 2149 | return 0; |
| 2150 | return a - b; |
| 2151 | } |
| 2152 | |
| 2153 | static char *format_alias(char *buf, int len, const struct perf_pmu *pmu, |
| 2154 | const struct perf_pmu_alias *alias, bool skip_duplicate_pmus) |
| 2155 | { |
| 2156 | struct parse_events_terms terms; |
| 2157 | struct parse_events_term *term; |
| 2158 | int ret, used; |
| 2159 | size_t pmu_name_len = pmu_deduped_name_len(pmu, name: pmu->name, |
| 2160 | skip_duplicate_pmus); |
| 2161 | |
| 2162 | /* Paramemterized events have the parameters shown. */ |
| 2163 | if (strstr(alias->terms, "=?" )) { |
| 2164 | /* No parameters. */ |
| 2165 | snprintf(buf, size: len, fmt: "%.*s/%s/" , (int)pmu_name_len, pmu->name, alias->name); |
| 2166 | return buf; |
| 2167 | } |
| 2168 | |
| 2169 | parse_events_terms__init(terms: &terms); |
| 2170 | ret = parse_events_terms(terms: &terms, str: alias->terms); |
| 2171 | if (ret) { |
| 2172 | pr_err("Failure to parse '%s' terms '%s': %d\n" , |
| 2173 | alias->name, alias->terms, ret); |
| 2174 | parse_events_terms__exit(terms: &terms); |
| 2175 | snprintf(buf, size: len, fmt: "%.*s/%s/" , (int)pmu_name_len, pmu->name, alias->name); |
| 2176 | return buf; |
| 2177 | } |
| 2178 | used = snprintf(buf, size: len, fmt: "%.*s/%s" , (int)pmu_name_len, pmu->name, alias->name); |
| 2179 | |
| 2180 | list_for_each_entry(term, &terms.terms, list) { |
| 2181 | if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) |
| 2182 | used += snprintf(buf: buf + used, size: sub_non_neg(a: len, b: used), |
| 2183 | fmt: ",%s=%s" , term->config, |
| 2184 | term->val.str); |
| 2185 | } |
| 2186 | parse_events_terms__exit(terms: &terms); |
| 2187 | if (sub_non_neg(a: len, b: used) > 0) { |
| 2188 | buf[used] = '/'; |
| 2189 | used++; |
| 2190 | } |
| 2191 | if (sub_non_neg(a: len, b: used) > 0) { |
| 2192 | buf[used] = '\0'; |
| 2193 | used++; |
| 2194 | } else |
| 2195 | buf[len - 1] = '\0'; |
| 2196 | |
| 2197 | return buf; |
| 2198 | } |
| 2199 | |
| 2200 | static bool perf_pmu_alias__check_deprecated(struct perf_pmu *pmu, struct perf_pmu_alias *alias) |
| 2201 | { |
| 2202 | struct perf_event_attr attr = {.config = 0,}; |
| 2203 | const char *check_terms; |
| 2204 | bool has_legacy_config; |
| 2205 | |
| 2206 | if (alias->legacy_deprecated_checked) |
| 2207 | return alias->deprecated; |
| 2208 | |
| 2209 | alias->legacy_deprecated_checked = true; |
| 2210 | if (alias->deprecated) |
| 2211 | return true; |
| 2212 | |
| 2213 | check_terms = alias->terms; |
| 2214 | has_legacy_config = |
| 2215 | strstr(check_terms, "legacy-hardware-config=" ) != NULL || |
| 2216 | strstr(check_terms, "legacy-cache-config=" ) != NULL; |
| 2217 | if (!has_legacy_config && alias->legacy_terms) { |
| 2218 | check_terms = alias->legacy_terms; |
| 2219 | has_legacy_config = |
| 2220 | strstr(check_terms, "legacy-hardware-config=" ) != NULL || |
| 2221 | strstr(check_terms, "legacy-cache-config=" ) != NULL; |
| 2222 | } |
| 2223 | if (!has_legacy_config) |
| 2224 | return false; |
| 2225 | |
| 2226 | if (perf_pmu__parse_terms_to_attr(pmu, terms_str: check_terms, attr: &attr) != 0) { |
| 2227 | /* Parsing failed, set as deprecated. */ |
| 2228 | alias->deprecated = true; |
| 2229 | } else if (attr.type < PERF_TYPE_MAX) { |
| 2230 | /* Flag unsupported legacy events as deprecated. */ |
| 2231 | alias->deprecated = !is_event_supported(type: attr.type, config: attr.config); |
| 2232 | } |
| 2233 | return alias->deprecated; |
| 2234 | } |
| 2235 | |
| 2236 | int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus, |
| 2237 | void *state, pmu_event_callback cb) |
| 2238 | { |
| 2239 | char buf[1024]; |
| 2240 | struct pmu_event_info info = { |
| 2241 | .pmu = pmu, |
| 2242 | .event_type_desc = "Kernel PMU event" , |
| 2243 | }; |
| 2244 | int ret = 0; |
| 2245 | struct hashmap_entry *entry; |
| 2246 | size_t bkt; |
| 2247 | |
| 2248 | if (perf_pmu__is_tracepoint(pmu)) |
| 2249 | return tp_pmu__for_each_event(pmu, state, cb); |
| 2250 | if (perf_pmu__is_hwmon(pmu)) |
| 2251 | return hwmon_pmu__for_each_event(pmu, state, cb); |
| 2252 | if (perf_pmu__is_drm(pmu)) |
| 2253 | return drm_pmu__for_each_event(pmu, state, cb); |
| 2254 | |
| 2255 | pmu_aliases_parse(pmu); |
| 2256 | pmu_add_cpu_aliases(pmu); |
| 2257 | hashmap__for_each_entry(pmu->aliases, entry, bkt) { |
| 2258 | struct perf_pmu_alias *event = entry->pvalue; |
| 2259 | size_t buf_used, pmu_name_len; |
| 2260 | |
| 2261 | if (perf_pmu__is_tool(pmu) && tool_pmu__skip_event(name: event->name)) |
| 2262 | continue; |
| 2263 | |
| 2264 | info.pmu_name = event->pmu_name ?: pmu->name; |
| 2265 | pmu_name_len = pmu_deduped_name_len(pmu, name: info.pmu_name, |
| 2266 | skip_duplicate_pmus); |
| 2267 | info.alias = NULL; |
| 2268 | if (event->desc) { |
| 2269 | info.name = event->name; |
| 2270 | buf_used = 0; |
| 2271 | } else { |
| 2272 | info.name = format_alias(buf, len: sizeof(buf), pmu, alias: event, |
| 2273 | skip_duplicate_pmus); |
| 2274 | if (pmu->is_core) { |
| 2275 | info.alias = info.name; |
| 2276 | info.name = event->name; |
| 2277 | } |
| 2278 | buf_used = strlen(buf) + 1; |
| 2279 | } |
| 2280 | info.scale_unit = NULL; |
| 2281 | if (strlen(event->unit) || event->scale != 1.0) { |
| 2282 | info.scale_unit = buf + buf_used; |
| 2283 | buf_used += snprintf(buf: buf + buf_used, size: sizeof(buf) - buf_used, |
| 2284 | fmt: "%G%s" , event->scale, event->unit) + 1; |
| 2285 | } |
| 2286 | info.desc = event->desc; |
| 2287 | info.long_desc = event->long_desc; |
| 2288 | info.encoding_desc = buf + buf_used; |
| 2289 | buf_used += snprintf(buf: buf + buf_used, size: sizeof(buf) - buf_used, |
| 2290 | fmt: "%.*s/%s/" , (int)pmu_name_len, info.pmu_name, event->terms) + 1; |
| 2291 | info.str = event->terms; |
| 2292 | info.topic = event->topic; |
| 2293 | info.deprecated = perf_pmu_alias__check_deprecated(pmu, alias: event); |
| 2294 | ret = cb(state, &info); |
| 2295 | if (ret) |
| 2296 | goto out; |
| 2297 | } |
| 2298 | if (pmu->selectable) { |
| 2299 | info.name = buf; |
| 2300 | snprintf(buf, size: sizeof(buf), fmt: "%s//" , pmu->name); |
| 2301 | info.alias = NULL; |
| 2302 | info.scale_unit = NULL; |
| 2303 | info.desc = NULL; |
| 2304 | info.long_desc = NULL; |
| 2305 | info.encoding_desc = NULL; |
| 2306 | info.topic = NULL; |
| 2307 | info.pmu_name = pmu->name; |
| 2308 | info.deprecated = false; |
| 2309 | ret = cb(state, &info); |
| 2310 | } |
| 2311 | out: |
| 2312 | return ret; |
| 2313 | } |
| 2314 | |
| 2315 | static bool perf_pmu___name_match(const struct perf_pmu *pmu, const char *to_match, bool wildcard) |
| 2316 | { |
| 2317 | const char *names[2] = { |
| 2318 | pmu->name, |
| 2319 | pmu->alias_name, |
| 2320 | }; |
| 2321 | if (pmu->is_core) { |
| 2322 | for (size_t i = 0; i < ARRAY_SIZE(names); i++) { |
| 2323 | const char *name = names[i]; |
| 2324 | |
| 2325 | if (!name) |
| 2326 | continue; |
| 2327 | |
| 2328 | if (!strcmp(name, to_match)) { |
| 2329 | /* Exact name match. */ |
| 2330 | return true; |
| 2331 | } |
| 2332 | } |
| 2333 | if (!strcmp(to_match, "default_core" )) { |
| 2334 | /* |
| 2335 | * jevents and tests use default_core as a marker for any core |
| 2336 | * PMU as the PMU name varies across architectures. |
| 2337 | */ |
| 2338 | return true; |
| 2339 | } |
| 2340 | return false; |
| 2341 | } |
| 2342 | if (!pmu->is_uncore) { |
| 2343 | /* |
| 2344 | * PMU isn't core or uncore, some kind of broken CPU mask |
| 2345 | * situation. Only match exact name. |
| 2346 | */ |
| 2347 | for (size_t i = 0; i < ARRAY_SIZE(names); i++) { |
| 2348 | const char *name = names[i]; |
| 2349 | |
| 2350 | if (!name) |
| 2351 | continue; |
| 2352 | |
| 2353 | if (!strcmp(name, to_match)) { |
| 2354 | /* Exact name match. */ |
| 2355 | return true; |
| 2356 | } |
| 2357 | } |
| 2358 | return false; |
| 2359 | } |
| 2360 | for (size_t i = 0; i < ARRAY_SIZE(names); i++) { |
| 2361 | const char *name = names[i]; |
| 2362 | |
| 2363 | if (!name) |
| 2364 | continue; |
| 2365 | |
| 2366 | if (wildcard && perf_pmu__match_wildcard_uncore(pmu_name: name, to_match)) |
| 2367 | return true; |
| 2368 | if (!wildcard && perf_pmu__match_ignoring_suffix_uncore(pmu_name: name, tok: to_match)) |
| 2369 | return true; |
| 2370 | } |
| 2371 | return false; |
| 2372 | } |
| 2373 | |
| 2374 | /** |
| 2375 | * perf_pmu__name_wildcard_match - Called by the jevents generated code to see |
| 2376 | * if pmu matches the json to_match string. |
| 2377 | * @pmu: The pmu whose name/alias to match. |
| 2378 | * @to_match: The possible match to pmu_name. |
| 2379 | */ |
| 2380 | bool perf_pmu__name_wildcard_match(const struct perf_pmu *pmu, const char *to_match) |
| 2381 | { |
| 2382 | return perf_pmu___name_match(pmu, to_match, /*wildcard=*/true); |
| 2383 | } |
| 2384 | |
| 2385 | /** |
| 2386 | * perf_pmu__name_no_suffix_match - Does pmu's name match to_match ignoring any |
| 2387 | * trailing suffix on the pmu_name and/or tok? |
| 2388 | * @pmu: The pmu whose name/alias to match. |
| 2389 | * @to_match: The possible match to pmu_name. |
| 2390 | */ |
| 2391 | bool perf_pmu__name_no_suffix_match(const struct perf_pmu *pmu, const char *to_match) |
| 2392 | { |
| 2393 | return perf_pmu___name_match(pmu, to_match, /*wildcard=*/false); |
| 2394 | } |
| 2395 | |
| 2396 | bool perf_pmu__is_software(const struct perf_pmu *pmu) |
| 2397 | { |
| 2398 | const char *known_sw_pmus[] = { |
| 2399 | "kprobe" , |
| 2400 | "msr" , |
| 2401 | "uprobe" , |
| 2402 | }; |
| 2403 | |
| 2404 | if (pmu->is_core || pmu->is_uncore || pmu->auxtrace) |
| 2405 | return false; |
| 2406 | switch (pmu->type) { |
| 2407 | case PERF_TYPE_HARDWARE: return false; |
| 2408 | case PERF_TYPE_SOFTWARE: return true; |
| 2409 | case PERF_TYPE_TRACEPOINT: return true; |
| 2410 | case PERF_TYPE_HW_CACHE: return false; |
| 2411 | case PERF_TYPE_RAW: return false; |
| 2412 | case PERF_TYPE_BREAKPOINT: return true; |
| 2413 | case PERF_PMU_TYPE_TOOL: return true; |
| 2414 | default: break; |
| 2415 | } |
| 2416 | for (size_t i = 0; i < ARRAY_SIZE(known_sw_pmus); i++) { |
| 2417 | if (!strcmp(pmu->name, known_sw_pmus[i])) |
| 2418 | return true; |
| 2419 | } |
| 2420 | return false; |
| 2421 | } |
| 2422 | |
| 2423 | FILE *perf_pmu__open_file(const struct perf_pmu *pmu, const char *name) |
| 2424 | { |
| 2425 | char path[PATH_MAX]; |
| 2426 | |
| 2427 | if (!perf_pmu__pathname_scnprintf(buf: path, size: sizeof(path), pmu_name: pmu->name, filename: name) || |
| 2428 | !file_available(name: path)) |
| 2429 | return NULL; |
| 2430 | |
| 2431 | return fopen(path, "r" ); |
| 2432 | } |
| 2433 | |
| 2434 | FILE *perf_pmu__open_file_at(const struct perf_pmu *pmu, int dirfd, const char *name) |
| 2435 | { |
| 2436 | int fd; |
| 2437 | |
| 2438 | fd = perf_pmu__pathname_fd(dirfd, pmu_name: pmu->name, filename: name, O_RDONLY); |
| 2439 | if (fd < 0) |
| 2440 | return NULL; |
| 2441 | |
| 2442 | return fdopen(fd, "r" ); |
| 2443 | } |
| 2444 | |
| 2445 | int perf_pmu__scan_file(const struct perf_pmu *pmu, const char *name, const char *fmt, |
| 2446 | ...) |
| 2447 | { |
| 2448 | va_list args; |
| 2449 | FILE *file; |
| 2450 | int ret = EOF; |
| 2451 | |
| 2452 | va_start(args, fmt); |
| 2453 | file = perf_pmu__open_file(pmu, name); |
| 2454 | if (file) { |
| 2455 | ret = vfscanf(file, fmt, args); |
| 2456 | fclose(file); |
| 2457 | } |
| 2458 | va_end(args); |
| 2459 | return ret; |
| 2460 | } |
| 2461 | |
| 2462 | int perf_pmu__scan_file_at(const struct perf_pmu *pmu, int dirfd, const char *name, |
| 2463 | const char *fmt, ...) |
| 2464 | { |
| 2465 | va_list args; |
| 2466 | FILE *file; |
| 2467 | int ret = EOF; |
| 2468 | |
| 2469 | va_start(args, fmt); |
| 2470 | file = perf_pmu__open_file_at(pmu, dirfd, name); |
| 2471 | if (file) { |
| 2472 | ret = vfscanf(file, fmt, args); |
| 2473 | fclose(file); |
| 2474 | } |
| 2475 | va_end(args); |
| 2476 | return ret; |
| 2477 | } |
| 2478 | |
| 2479 | bool perf_pmu__file_exists(const struct perf_pmu *pmu, const char *name) |
| 2480 | { |
| 2481 | char path[PATH_MAX]; |
| 2482 | |
| 2483 | if (!perf_pmu__pathname_scnprintf(buf: path, size: sizeof(path), pmu_name: pmu->name, filename: name)) |
| 2484 | return false; |
| 2485 | |
| 2486 | return file_available(name: path); |
| 2487 | } |
| 2488 | |
| 2489 | static int perf_pmu__new_caps(struct list_head *list, char *name, char *value) |
| 2490 | { |
| 2491 | struct perf_pmu_caps *caps = zalloc(sizeof(*caps)); |
| 2492 | |
| 2493 | if (!caps) |
| 2494 | return -ENOMEM; |
| 2495 | |
| 2496 | caps->name = strdup(name); |
| 2497 | if (!caps->name) |
| 2498 | goto free_caps; |
| 2499 | caps->value = strndup(value, strlen(value) - 1); |
| 2500 | if (!caps->value) |
| 2501 | goto free_name; |
| 2502 | list_add_tail(new: &caps->list, head: list); |
| 2503 | return 0; |
| 2504 | |
| 2505 | free_name: |
| 2506 | zfree(&caps->name); |
| 2507 | free_caps: |
| 2508 | free(caps); |
| 2509 | |
| 2510 | return -ENOMEM; |
| 2511 | } |
| 2512 | |
| 2513 | static void perf_pmu__del_caps(struct perf_pmu *pmu) |
| 2514 | { |
| 2515 | struct perf_pmu_caps *caps, *tmp; |
| 2516 | |
| 2517 | list_for_each_entry_safe(caps, tmp, &pmu->caps, list) { |
| 2518 | list_del(entry: &caps->list); |
| 2519 | zfree(&caps->name); |
| 2520 | zfree(&caps->value); |
| 2521 | free(caps); |
| 2522 | } |
| 2523 | } |
| 2524 | |
| 2525 | struct perf_pmu_caps *perf_pmu__get_cap(struct perf_pmu *pmu, const char *name) |
| 2526 | { |
| 2527 | struct perf_pmu_caps *caps; |
| 2528 | |
| 2529 | list_for_each_entry(caps, &pmu->caps, list) { |
| 2530 | if (!strcmp(caps->name, name)) |
| 2531 | return caps; |
| 2532 | } |
| 2533 | return NULL; |
| 2534 | } |
| 2535 | |
| 2536 | /* |
| 2537 | * Reading/parsing the given pmu capabilities, which should be located at: |
| 2538 | * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes. |
| 2539 | * Return the number of capabilities |
| 2540 | */ |
| 2541 | int perf_pmu__caps_parse(struct perf_pmu *pmu) |
| 2542 | { |
| 2543 | char caps_path[PATH_MAX]; |
| 2544 | struct io_dir caps_dir; |
| 2545 | struct io_dirent64 *evt_ent; |
| 2546 | int caps_fd; |
| 2547 | |
| 2548 | if (pmu->caps_initialized) |
| 2549 | return pmu->nr_caps; |
| 2550 | |
| 2551 | pmu->nr_caps = 0; |
| 2552 | |
| 2553 | if (!perf_pmu__pathname_scnprintf(buf: caps_path, size: sizeof(caps_path), pmu_name: pmu->name, filename: "caps" )) |
| 2554 | return -1; |
| 2555 | |
| 2556 | caps_fd = open(caps_path, O_CLOEXEC | O_DIRECTORY | O_RDONLY); |
| 2557 | if (caps_fd == -1) { |
| 2558 | pmu->caps_initialized = true; |
| 2559 | return 0; /* no error if caps does not exist */ |
| 2560 | } |
| 2561 | |
| 2562 | io_dir__init(&caps_dir, caps_fd); |
| 2563 | |
| 2564 | while ((evt_ent = io_dir__readdir(&caps_dir)) != NULL) { |
| 2565 | char *name = evt_ent->d_name; |
| 2566 | char value[128]; |
| 2567 | FILE *file; |
| 2568 | int fd; |
| 2569 | |
| 2570 | if (io_dir__is_dir(&caps_dir, evt_ent)) |
| 2571 | continue; |
| 2572 | |
| 2573 | fd = openat(caps_fd, name, O_RDONLY); |
| 2574 | if (fd == -1) |
| 2575 | continue; |
| 2576 | file = fdopen(fd, "r" ); |
| 2577 | if (!file) { |
| 2578 | close(fd); |
| 2579 | continue; |
| 2580 | } |
| 2581 | |
| 2582 | if (!fgets(value, sizeof(value), file) || |
| 2583 | (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) { |
| 2584 | fclose(file); |
| 2585 | continue; |
| 2586 | } |
| 2587 | |
| 2588 | pmu->nr_caps++; |
| 2589 | fclose(file); |
| 2590 | } |
| 2591 | |
| 2592 | close(caps_fd); |
| 2593 | |
| 2594 | pmu->caps_initialized = true; |
| 2595 | return pmu->nr_caps; |
| 2596 | } |
| 2597 | |
| 2598 | static void perf_pmu__compute_config_masks(struct perf_pmu *pmu) |
| 2599 | { |
| 2600 | struct perf_pmu_format *format; |
| 2601 | |
| 2602 | if (pmu->config_masks_computed) |
| 2603 | return; |
| 2604 | |
| 2605 | list_for_each_entry(format, &pmu->format, list) { |
| 2606 | unsigned int i; |
| 2607 | __u64 *mask; |
| 2608 | |
| 2609 | if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) |
| 2610 | continue; |
| 2611 | |
| 2612 | pmu->config_masks_present = true; |
| 2613 | mask = &pmu->config_masks[format->value]; |
| 2614 | |
| 2615 | for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS) |
| 2616 | *mask |= 1ULL << i; |
| 2617 | } |
| 2618 | pmu->config_masks_computed = true; |
| 2619 | } |
| 2620 | |
| 2621 | void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, |
| 2622 | const char *name, int config_num, |
| 2623 | const char *config_name) |
| 2624 | { |
| 2625 | __u64 bits; |
| 2626 | char buf[100]; |
| 2627 | |
| 2628 | perf_pmu__compute_config_masks(pmu); |
| 2629 | |
| 2630 | /* |
| 2631 | * Kernel doesn't export any valid format bits. |
| 2632 | */ |
| 2633 | if (!pmu->config_masks_present) |
| 2634 | return; |
| 2635 | |
| 2636 | bits = config & ~pmu->config_masks[config_num]; |
| 2637 | if (bits == 0) |
| 2638 | return; |
| 2639 | |
| 2640 | bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf)); |
| 2641 | |
| 2642 | pr_warning("WARNING: event '%s' not valid (bits %s of %s " |
| 2643 | "'%llx' not supported by kernel)!\n" , |
| 2644 | name ?: "N/A" , buf, config_name, config); |
| 2645 | } |
| 2646 | |
| 2647 | bool perf_pmu__wildcard_match(const struct perf_pmu *pmu, const char *wildcard_to_match) |
| 2648 | { |
| 2649 | const char *names[2] = { |
| 2650 | pmu->name, |
| 2651 | pmu->alias_name, |
| 2652 | }; |
| 2653 | bool need_fnmatch = strisglob(str: wildcard_to_match); |
| 2654 | |
| 2655 | if (!strncmp(wildcard_to_match, "uncore_" , 7)) |
| 2656 | wildcard_to_match += 7; |
| 2657 | |
| 2658 | for (size_t i = 0; i < ARRAY_SIZE(names); i++) { |
| 2659 | const char *pmu_name = names[i]; |
| 2660 | |
| 2661 | if (!pmu_name) |
| 2662 | continue; |
| 2663 | |
| 2664 | if (!strncmp(pmu_name, "uncore_" , 7)) |
| 2665 | pmu_name += 7; |
| 2666 | |
| 2667 | if (perf_pmu__match_wildcard(pmu_name, tok: wildcard_to_match) || |
| 2668 | (need_fnmatch && !fnmatch(wildcard_to_match, pmu_name, 0))) |
| 2669 | return true; |
| 2670 | } |
| 2671 | return false; |
| 2672 | } |
| 2673 | |
| 2674 | int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size) |
| 2675 | { |
| 2676 | const char *sysfs = sysfs__mountpoint(); |
| 2677 | |
| 2678 | if (!sysfs) |
| 2679 | return 0; |
| 2680 | return scnprintf(buf: pathname, size, fmt: "%s/bus/event_source/devices/" , sysfs); |
| 2681 | } |
| 2682 | |
| 2683 | int perf_pmu__event_source_devices_fd(void) |
| 2684 | { |
| 2685 | char path[PATH_MAX]; |
| 2686 | const char *sysfs = sysfs__mountpoint(); |
| 2687 | |
| 2688 | if (!sysfs) |
| 2689 | return -1; |
| 2690 | |
| 2691 | scnprintf(buf: path, size: sizeof(path), fmt: "%s/bus/event_source/devices/" , sysfs); |
| 2692 | return open(path, O_DIRECTORY); |
| 2693 | } |
| 2694 | |
| 2695 | /* |
| 2696 | * Fill 'buf' with the path to a file or folder in 'pmu_name' in |
| 2697 | * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format" |
| 2698 | * then pathname will be filled with |
| 2699 | * "/sys/bus/event_source/devices/cs_etm/format" |
| 2700 | * |
| 2701 | * Return 0 if the sysfs mountpoint couldn't be found, if no characters were |
| 2702 | * written or if the buffer size is exceeded. |
| 2703 | */ |
| 2704 | int perf_pmu__pathname_scnprintf(char *buf, size_t size, |
| 2705 | const char *pmu_name, const char *filename) |
| 2706 | { |
| 2707 | size_t len; |
| 2708 | |
| 2709 | len = perf_pmu__event_source_devices_scnprintf(pathname: buf, size); |
| 2710 | if (!len || (len + strlen(pmu_name) + strlen(filename) + 1) >= size) |
| 2711 | return 0; |
| 2712 | |
| 2713 | return scnprintf(buf: buf + len, size: size - len, fmt: "%s/%s" , pmu_name, filename); |
| 2714 | } |
| 2715 | |
| 2716 | int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags) |
| 2717 | { |
| 2718 | char path[PATH_MAX]; |
| 2719 | |
| 2720 | scnprintf(buf: path, size: sizeof(path), fmt: "%s/%s" , pmu_name, filename); |
| 2721 | return openat(dirfd, path, flags); |
| 2722 | } |
| 2723 | |
| 2724 | void perf_pmu__delete(struct perf_pmu *pmu) |
| 2725 | { |
| 2726 | if (!pmu) |
| 2727 | return; |
| 2728 | |
| 2729 | if (perf_pmu__is_hwmon(pmu)) |
| 2730 | hwmon_pmu__exit(pmu); |
| 2731 | else if (perf_pmu__is_drm(pmu)) |
| 2732 | drm_pmu__exit(pmu); |
| 2733 | |
| 2734 | perf_pmu__del_formats(formats: &pmu->format); |
| 2735 | perf_pmu__del_aliases(pmu); |
| 2736 | perf_pmu__del_caps(pmu); |
| 2737 | |
| 2738 | perf_cpu_map__put(pmu->cpus); |
| 2739 | |
| 2740 | zfree(&pmu->name); |
| 2741 | zfree(&pmu->alias_name); |
| 2742 | zfree(&pmu->id); |
| 2743 | free(pmu); |
| 2744 | } |
| 2745 | |
| 2746 | const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config) |
| 2747 | { |
| 2748 | struct hashmap_entry *entry; |
| 2749 | size_t bkt; |
| 2750 | |
| 2751 | if (!pmu) |
| 2752 | return NULL; |
| 2753 | |
| 2754 | pmu_aliases_parse(pmu); |
| 2755 | pmu_add_cpu_aliases(pmu); |
| 2756 | hashmap__for_each_entry(pmu->aliases, entry, bkt) { |
| 2757 | struct perf_pmu_alias *event = entry->pvalue; |
| 2758 | struct perf_event_attr attr = {.config = 0,}; |
| 2759 | int ret = perf_pmu__parse_terms_to_attr(pmu, terms_str: event->terms, attr: &attr); |
| 2760 | |
| 2761 | if (ret == 0 && config == attr.config) |
| 2762 | return event->name; |
| 2763 | } |
| 2764 | return NULL; |
| 2765 | } |
| 2766 | |