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