Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

perf pmu: Merge JSON events with sysfs at load time

Rather than load all sysfs events then parsing all JSON events and
merging with ones that already exist. When a sysfs event is loaded, look
for a corresponding JSON event and merge immediately.

To simplify the logic, early exit the perf_pmu__new_alias function if an
alias is attempted to be added twice - as merging has already been
explicitly handled.

Fix the copying of terms to a merged alias and some ENOMEM paths.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Gaosheng Cui <cuigaosheng1@huawei.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jing Zhang <renyu.zj@linux.alibaba.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Rob Herring <robh@kernel.org>
Link: https://lore.kernel.org/r/20230824041330.266337-12-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
f63a536f f26d22f1

+89 -90
+89 -90
tools/perf/util/pmu.c
··· 377 377 return 0; 378 378 } 379 379 380 - static void perf_pmu_assign_str(char *name, const char *field, char **old_str, 381 - char **new_str) 382 - { 383 - if (!*old_str) 384 - goto set_new; 385 - 386 - if (*new_str) { /* Have new string, check with old */ 387 - if (strcasecmp(*old_str, *new_str)) 388 - pr_debug("alias %s differs in field '%s'\n", 389 - name, field); 390 - zfree(old_str); 391 - } else /* Nothing new --> keep old string */ 392 - return; 393 - set_new: 394 - *old_str = *new_str; 395 - *new_str = NULL; 396 - } 397 - 398 - static void perf_pmu_update_alias(struct perf_pmu_alias *old, 399 - struct perf_pmu_alias *newalias) 400 - { 401 - perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc); 402 - perf_pmu_assign_str(old->name, "long_desc", &old->long_desc, 403 - &newalias->long_desc); 404 - perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic); 405 - perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str); 406 - old->scale = newalias->scale; 407 - old->per_pkg = newalias->per_pkg; 408 - old->snapshot = newalias->snapshot; 409 - memcpy(old->unit, newalias->unit, sizeof(old->unit)); 410 - } 411 - 412 380 /* Delete an alias entry. */ 413 381 static void perf_pmu_free_alias(struct perf_pmu_alias *newalias) 414 382 { ··· 400 432 } 401 433 } 402 434 403 - /* Merge an alias, search in alias list. If this name is already 404 - * present merge both of them to combine all information. 405 - */ 406 - static bool perf_pmu_merge_alias(struct perf_pmu *pmu, 407 - struct perf_pmu_alias *newalias) 435 + static struct perf_pmu_alias *perf_pmu__find_alias(const struct perf_pmu *pmu, const char *name) 408 436 { 409 - struct perf_pmu_alias *a; 437 + struct perf_pmu_alias *alias; 410 438 411 - list_for_each_entry(a, &pmu->aliases, list) { 412 - if (!strcasecmp(newalias->name, a->name)) { 413 - if (newalias->pmu_name && a->pmu_name && 414 - !strcasecmp(newalias->pmu_name, a->pmu_name)) { 415 - continue; 416 - } 417 - perf_pmu_update_alias(a, newalias); 418 - perf_pmu_free_alias(newalias); 419 - return true; 420 - } 439 + list_for_each_entry(alias, &pmu->aliases, list) { 440 + if (!strcasecmp(alias->name, name)) 441 + return alias; 421 442 } 422 - return false; 443 + return NULL; 444 + } 445 + 446 + static bool assign_str(const char *name, const char *field, char **old_str, 447 + const char *new_str) 448 + { 449 + if (!*old_str && new_str) { 450 + *old_str = strdup(new_str); 451 + return true; 452 + } 453 + 454 + if (!new_str || !strcasecmp(*old_str, new_str)) 455 + return false; /* Nothing to update. */ 456 + 457 + pr_debug("alias %s differs in field '%s' ('%s' != '%s')\n", 458 + name, field, *old_str, new_str); 459 + zfree(old_str); 460 + *old_str = strdup(new_str); 461 + return true; 462 + } 463 + 464 + static int update_alias(const struct pmu_event *pe, 465 + const struct pmu_events_table *table __maybe_unused, 466 + void *vdata) 467 + { 468 + struct perf_pmu_alias *alias = vdata; 469 + int ret = 0; 470 + 471 + assign_str(pe->name, "desc", &alias->desc, pe->desc); 472 + assign_str(pe->name, "long_desc", &alias->long_desc, pe->long_desc); 473 + assign_str(pe->name, "topic", &alias->topic, pe->topic); 474 + alias->per_pkg = pe->perpkg; 475 + if (assign_str(pe->name, "value", &alias->str, pe->event)) { 476 + parse_events_terms__purge(&alias->terms); 477 + ret = parse_events_terms(&alias->terms, pe->event, /*input=*/NULL); 478 + } 479 + if (!ret && pe->unit) { 480 + char *unit; 481 + 482 + ret = perf_pmu__convert_scale(pe->unit, &unit, &alias->scale); 483 + if (!ret) 484 + snprintf(alias->unit, sizeof(alias->unit), "%s", unit); 485 + } 486 + return ret; 423 487 } 424 488 425 489 static int perf_pmu__new_alias(struct perf_pmu *pmu, int dirfd, const char *name, ··· 464 464 char newval[256]; 465 465 const char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL; 466 466 bool deprecated = false, perpkg = false; 467 + 468 + if (perf_pmu__find_alias(pmu, name)) { 469 + /* Alias was already created/loaded. */ 470 + return 0; 471 + } 467 472 468 473 if (pe) { 469 474 long_desc = pe->long_desc; ··· 497 492 return ret; 498 493 } 499 494 500 - /* Scan event and remove leading zeroes, spaces, newlines, some 501 - * platforms have terms specified as 502 - * event=0x0091 (read from files ../<PMU>/events/<FILE> 503 - * and terms specified as event=0x91 (read from JSON files). 504 - * 505 - * Rebuild string to make alias->str member comparable. 506 - */ 507 - memset(newval, 0, sizeof(newval)); 508 - ret = 0; 509 - list_for_each_entry(term, &alias->terms, list) { 510 - if (ret) 511 - ret += scnprintf(newval + ret, sizeof(newval) - ret, 512 - ","); 513 - if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) 514 - ret += scnprintf(newval + ret, sizeof(newval) - ret, 515 - "%s=%#x", term->config, term->val.num); 516 - else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 517 - ret += scnprintf(newval + ret, sizeof(newval) - ret, 518 - "%s=%s", term->config, term->val.str); 519 - } 520 - 521 495 alias->name = strdup(name); 522 496 if (dirfd >= 0) { 523 497 /* ··· 512 528 alias->long_desc = long_desc ? strdup(long_desc) : 513 529 desc ? strdup(desc) : NULL; 514 530 alias->topic = topic ? strdup(topic) : NULL; 531 + alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL; 515 532 if (unit) { 516 - if (perf_pmu__convert_scale(unit, (char **)&unit, &alias->scale) < 0) 533 + if (perf_pmu__convert_scale(unit, (char **)&unit, &alias->scale) < 0) { 534 + perf_pmu_free_alias(alias); 517 535 return -1; 536 + } 518 537 snprintf(alias->unit, sizeof(alias->unit), "%s", unit); 519 538 } 539 + if (!pe) { 540 + /* Update an event from sysfs with json data. */ 541 + const struct pmu_events_table *table = perf_pmu__find_events_table(pmu); 542 + 543 + if (table) 544 + pmu_events_table__find_event(table, pmu, name, update_alias, alias); 545 + } 546 + 547 + /* Scan event and remove leading zeroes, spaces, newlines, some 548 + * platforms have terms specified as 549 + * event=0x0091 (read from files ../<PMU>/events/<FILE> 550 + * and terms specified as event=0x91 (read from JSON files). 551 + * 552 + * Rebuild string to make alias->str member comparable. 553 + */ 554 + ret = 0; 555 + list_for_each_entry(term, &alias->terms, list) { 556 + if (ret) 557 + ret += scnprintf(newval + ret, sizeof(newval) - ret, 558 + ","); 559 + if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) 560 + ret += scnprintf(newval + ret, sizeof(newval) - ret, 561 + "%s=%#x", term->config, term->val.num); 562 + else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 563 + ret += scnprintf(newval + ret, sizeof(newval) - ret, 564 + "%s=%s", term->config, term->val.str); 565 + } 520 566 alias->str = strdup(newval); 521 - alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL; 522 - 523 - if (!perf_pmu_merge_alias(pmu, alias)) 524 - list_add_tail(&alias->list, &pmu->aliases); 525 - 567 + list_add_tail(&alias->list, &pmu->aliases); 526 568 return 0; 527 569 } 528 570 ··· 954 944 INIT_LIST_HEAD(&pmu->format); 955 945 INIT_LIST_HEAD(&pmu->aliases); 956 946 INIT_LIST_HEAD(&pmu->caps); 947 + pmu->name = strdup(name); 948 + if (!pmu->name) 949 + goto err; 957 950 /* 958 951 * The pmu data we store & need consists of the pmu 959 952 * type value and format definitions. Load both right ··· 975 962 } 976 963 pmu->is_core = is_pmu_core(name); 977 964 pmu->cpus = pmu_cpumask(dirfd, name, pmu->is_core); 978 - pmu->name = strdup(name); 979 - if (!pmu->name) 980 - goto err; 981 965 982 966 /* Read type, and ensure that type value is successfully assigned (return 1) */ 983 967 if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) != 1) ··· 1365 1355 bool zero = !!pmu->default_config; 1366 1356 1367 1357 return perf_pmu__config_terms(pmu, attr, head_terms, zero, err); 1368 - } 1369 - 1370 - static struct perf_pmu_alias *perf_pmu__find_alias(const struct perf_pmu *pmu, const char *str) 1371 - { 1372 - struct perf_pmu_alias *alias; 1373 - 1374 - list_for_each_entry(alias, &pmu->aliases, list) { 1375 - if (!strcasecmp(alias->name, str)) 1376 - return alias; 1377 - } 1378 - return NULL; 1379 1358 } 1380 1359 1381 1360 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,