1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2017, Intel Corporation. 4 */ 5 6 /* Manage metrics and groups of metrics from JSON files */ 7 8 #include "metricgroup.h" 9 #include "debug.h" 10 #include "evlist.h" 11 #include "evsel.h" 12 #include "strbuf.h" 13 #include "pmu.h" 14 #include "pmus.h" 15 #include "print-events.h" 16 #include "smt.h" 17 #include "tool_pmu.h" 18 #include "expr.h" 19 #include "rblist.h" 20 #include <string.h> 21 #include <errno.h> 22 #include "strlist.h" 23 #include <assert.h> 24 #include <linux/ctype.h> 25 #include <linux/list_sort.h> 26 #include <linux/string.h> 27 #include <linux/zalloc.h> 28 #include <perf/cpumap.h> 29 #include <subcmd/parse-options.h> 30 #include <api/fs/fs.h> 31 #include "util.h" 32 #include <asm/bug.h> 33 #include "cgroup.h" 34 #include "util/hashmap.h" 35 36 struct metric_event *metricgroup__lookup(struct rblist *metric_events, 37 struct evsel *evsel, 38 bool create) 39 { 40 struct rb_node *nd; 41 struct metric_event me = { 42 .evsel = evsel 43 }; 44 45 if (!metric_events) 46 return NULL; 47 48 if (evsel && evsel->metric_leader) 49 me.evsel = evsel->metric_leader; 50 nd = rblist__find(metric_events, &me); 51 if (nd) 52 return container_of(nd, struct metric_event, nd); 53 if (create) { 54 rblist__add_node(metric_events, &me); 55 nd = rblist__find(metric_events, &me); 56 if (nd) 57 return container_of(nd, struct metric_event, nd); 58 } 59 return NULL; 60 } 61 62 static int metric_event_cmp(struct rb_node *rb_node, const void *entry) 63 { 64 struct metric_event *a = container_of(rb_node, 65 struct metric_event, 66 nd); 67 const struct metric_event *b = entry; 68 69 if (a->evsel == b->evsel) 70 return 0; 71 if ((char *)a->evsel < (char *)b->evsel) 72 return -1; 73 return +1; 74 } 75 76 static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused, 77 const void *entry) 78 { 79 struct metric_event *me = malloc(sizeof(struct metric_event)); 80 81 if (!me) 82 return NULL; 83 memcpy(me, entry, sizeof(struct metric_event)); 84 me->evsel = ((struct metric_event *)entry)->evsel; 85 me->is_default = false; 86 INIT_LIST_HEAD(&me->head); 87 return &me->nd; 88 } 89 90 static void metric_event_delete(struct rblist *rblist __maybe_unused, 91 struct rb_node *rb_node) 92 { 93 struct metric_event *me = container_of(rb_node, struct metric_event, nd); 94 struct metric_expr *expr, *tmp; 95 96 list_for_each_entry_safe(expr, tmp, &me->head, nd) { 97 zfree(&expr->metric_name); 98 zfree(&expr->metric_refs); 99 zfree(&expr->metric_events); 100 free(expr); 101 } 102 103 free(me); 104 } 105 106 void metricgroup__rblist_init(struct rblist *metric_events) 107 { 108 rblist__init(metric_events); 109 metric_events->node_cmp = metric_event_cmp; 110 metric_events->node_new = metric_event_new; 111 metric_events->node_delete = metric_event_delete; 112 } 113 114 void metricgroup__rblist_exit(struct rblist *metric_events) 115 { 116 rblist__exit(metric_events); 117 } 118 119 /** 120 * The metric under construction. The data held here will be placed in a 121 * metric_expr. 122 */ 123 struct metric { 124 struct list_head nd; 125 /** 126 * The expression parse context importantly holding the IDs contained 127 * within the expression. 128 */ 129 struct expr_parse_ctx *pctx; 130 const char *pmu; 131 /** The name of the metric such as "IPC". */ 132 const char *metric_name; 133 /** Modifier on the metric such as "u" or NULL for none. */ 134 const char *modifier; 135 /** The expression to parse, for example, "instructions/cycles". */ 136 const char *metric_expr; 137 /** Optional threshold expression where zero value is green, otherwise red. */ 138 const char *metric_threshold; 139 /** 140 * The "ScaleUnit" that scales and adds a unit to the metric during 141 * output. 142 */ 143 const char *metric_unit; 144 /** 145 * Optional name of the metric group reported 146 * if the Default metric group is being processed. 147 */ 148 const char *default_metricgroup_name; 149 /** Optional null terminated array of referenced metrics. */ 150 struct metric_ref *metric_refs; 151 /** 152 * Should events of the metric be grouped? 153 */ 154 bool group_events; 155 /** Show events even if in the Default metric group. */ 156 bool default_show_events; 157 /** 158 * Parsed events for the metric. Optional as events may be taken from a 159 * different metric whose group contains all the IDs necessary for this 160 * one. 161 */ 162 struct evlist *evlist; 163 }; 164 165 static void metric__watchdog_constraint_hint(const char *name, bool foot) 166 { 167 static bool violate_nmi_constraint; 168 169 if (!foot) { 170 pr_warning("Not grouping metric %s's events.\n", name); 171 violate_nmi_constraint = true; 172 return; 173 } 174 175 if (!violate_nmi_constraint) 176 return; 177 178 pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n" 179 " echo 0 > /proc/sys/kernel/nmi_watchdog\n" 180 " perf stat ...\n" 181 " echo 1 > /proc/sys/kernel/nmi_watchdog\n"); 182 } 183 184 static bool metric__group_events(const struct pmu_metric *pm, bool metric_no_threshold) 185 { 186 switch (pm->event_grouping) { 187 case MetricNoGroupEvents: 188 return false; 189 case MetricNoGroupEventsNmi: 190 if (!sysctl__nmi_watchdog_enabled()) 191 return true; 192 metric__watchdog_constraint_hint(pm->metric_name, /*foot=*/false); 193 return false; 194 case MetricNoGroupEventsSmt: 195 return !smt_on(); 196 case MetricNoGroupEventsThresholdAndNmi: 197 if (metric_no_threshold) 198 return true; 199 if (!sysctl__nmi_watchdog_enabled()) 200 return true; 201 metric__watchdog_constraint_hint(pm->metric_name, /*foot=*/false); 202 return false; 203 case MetricGroupEvents: 204 default: 205 return true; 206 } 207 } 208 209 static void metric__free(struct metric *m) 210 { 211 if (!m) 212 return; 213 214 zfree(&m->metric_refs); 215 expr__ctx_free(m->pctx); 216 zfree(&m->modifier); 217 evlist__delete(m->evlist); 218 free(m); 219 } 220 221 static struct metric *metric__new(const struct pmu_metric *pm, 222 const char *modifier, 223 bool metric_no_group, 224 bool metric_no_threshold, 225 int runtime, 226 const char *user_requested_cpu_list, 227 bool system_wide) 228 { 229 struct metric *m; 230 231 m = zalloc(sizeof(*m)); 232 if (!m) 233 return NULL; 234 235 m->pctx = expr__ctx_new(); 236 if (!m->pctx) 237 goto out_err; 238 239 m->pmu = pm->pmu ?: "cpu"; 240 m->metric_name = pm->metric_name; 241 m->default_metricgroup_name = pm->default_metricgroup_name ?: ""; 242 m->modifier = NULL; 243 if (modifier) { 244 m->modifier = strdup(modifier); 245 if (!m->modifier) 246 goto out_err; 247 } 248 m->metric_expr = pm->metric_expr; 249 m->metric_threshold = pm->metric_threshold; 250 m->metric_unit = pm->unit; 251 m->pctx->sctx.user_requested_cpu_list = NULL; 252 if (user_requested_cpu_list) { 253 m->pctx->sctx.user_requested_cpu_list = strdup(user_requested_cpu_list); 254 if (!m->pctx->sctx.user_requested_cpu_list) 255 goto out_err; 256 } 257 m->pctx->sctx.runtime = runtime; 258 m->pctx->sctx.system_wide = system_wide; 259 m->group_events = !metric_no_group && metric__group_events(pm, metric_no_threshold); 260 m->default_show_events = pm->default_show_events; 261 m->metric_refs = NULL; 262 m->evlist = NULL; 263 264 return m; 265 out_err: 266 metric__free(m); 267 return NULL; 268 } 269 270 static bool contains_metric_id(struct evsel **metric_events, int num_events, 271 const char *metric_id) 272 { 273 int i; 274 275 for (i = 0; i < num_events; i++) { 276 if (!strcmp(evsel__metric_id(metric_events[i]), metric_id)) 277 return true; 278 } 279 return false; 280 } 281 282 /** 283 * setup_metric_events - Find a group of events in metric_evlist that correspond 284 * to the IDs from a parsed metric expression. 285 * @pmu: The PMU for the IDs. 286 * @ids: the metric IDs to match. 287 * @metric_evlist: the list of perf events. 288 * @out_metric_events: holds the created metric events array. 289 */ 290 static int setup_metric_events(const char *pmu, struct hashmap *ids, 291 struct evlist *metric_evlist, 292 struct evsel ***out_metric_events) 293 { 294 struct evsel **metric_events; 295 const char *metric_id; 296 struct evsel *ev; 297 size_t ids_size, matched_events, i; 298 bool all_pmus = !strcmp(pmu, "all") || perf_pmus__num_core_pmus() == 1 || !is_pmu_core(pmu); 299 300 *out_metric_events = NULL; 301 ids_size = hashmap__size(ids); 302 303 metric_events = calloc(ids_size + 1, sizeof(void *)); 304 if (!metric_events) 305 return -ENOMEM; 306 307 matched_events = 0; 308 evlist__for_each_entry(metric_evlist, ev) { 309 struct expr_id_data *val_ptr; 310 311 /* Don't match events for the wrong hybrid PMU. */ 312 if (!all_pmus && ev->pmu && evsel__is_hybrid(ev) && 313 strcmp(ev->pmu->name, pmu)) 314 continue; 315 /* 316 * Check for duplicate events with the same name. For 317 * example, uncore_imc/cas_count_read/ will turn into 6 318 * events per socket on skylakex. Only the first such 319 * event is placed in metric_events. 320 */ 321 metric_id = evsel__metric_id(ev); 322 if (contains_metric_id(metric_events, matched_events, metric_id)) 323 continue; 324 /* 325 * Does this event belong to the parse context? For 326 * combined or shared groups, this metric may not care 327 * about this event. 328 */ 329 if (hashmap__find(ids, metric_id, &val_ptr)) { 330 pr_debug("Matched metric-id %s to %s\n", metric_id, evsel__name(ev)); 331 metric_events[matched_events++] = ev; 332 333 if (matched_events >= ids_size) 334 break; 335 } 336 } 337 if (matched_events < ids_size) { 338 free(metric_events); 339 return -EINVAL; 340 } 341 for (i = 0; i < ids_size; i++) { 342 ev = metric_events[i]; 343 ev->collect_stat = true; 344 345 /* 346 * The metric leader points to the identically named 347 * event in metric_events. 348 */ 349 ev->metric_leader = ev; 350 /* 351 * Mark two events with identical names in the same 352 * group (or globally) as being in use as uncore events 353 * may be duplicated for each pmu. Set the metric leader 354 * of such events to be the event that appears in 355 * metric_events. 356 */ 357 metric_id = evsel__metric_id(ev); 358 evlist__for_each_entry_continue(metric_evlist, ev) { 359 if (!strcmp(evsel__metric_id(ev), metric_id)) 360 ev->metric_leader = metric_events[i]; 361 } 362 } 363 *out_metric_events = metric_events; 364 return 0; 365 } 366 367 static bool match_metric_or_groups(const char *metric_or_groups, const char *sought) 368 { 369 int len; 370 char *m; 371 372 if (!sought) 373 return false; 374 if (!strcmp(sought, "all")) 375 return true; 376 if (!metric_or_groups) 377 return !strcasecmp(sought, "No_group"); 378 len = strlen(sought); 379 if (!strncasecmp(metric_or_groups, sought, len) && 380 (metric_or_groups[len] == 0 || metric_or_groups[len] == ';')) 381 return true; 382 m = strchr(metric_or_groups, ';'); 383 return m && match_metric_or_groups(m + 1, sought); 384 } 385 386 static bool match_pm_metric_or_groups(const struct pmu_metric *pm, const char *pmu, 387 const char *metric_or_groups) 388 { 389 const char *pm_pmu = pm->pmu ?: "cpu"; 390 391 if (strcmp(pmu, "all") && strcmp(pm_pmu, pmu)) 392 return false; 393 394 return match_metric_or_groups(pm->metric_group, metric_or_groups) || 395 match_metric_or_groups(pm->metric_name, metric_or_groups); 396 } 397 398 struct metricgroup_iter_data { 399 pmu_metric_iter_fn fn; 400 void *data; 401 }; 402 403 static int metricgroup__sys_event_iter(const struct pmu_metric *pm, 404 const struct pmu_metrics_table *table, 405 void *data) 406 { 407 struct metricgroup_iter_data *d = data; 408 struct perf_pmu *pmu = NULL; 409 410 if (!pm->metric_expr || !pm->compat) 411 return 0; 412 413 while ((pmu = perf_pmus__scan(pmu))) { 414 415 if (!pmu->id || !pmu_uncore_identifier_match(pm->compat, pmu->id)) 416 continue; 417 418 return d->fn(pm, table, d->data); 419 } 420 return 0; 421 } 422 423 int metricgroup__for_each_metric(const struct pmu_metrics_table *table, pmu_metric_iter_fn fn, 424 void *data) 425 { 426 struct metricgroup_iter_data sys_data = { 427 .fn = fn, 428 .data = data, 429 }; 430 const struct pmu_metrics_table *tables[2] = { 431 table, 432 pmu_metrics_table__default(), 433 }; 434 435 for (size_t i = 0; i < ARRAY_SIZE(tables); i++) { 436 int ret; 437 438 if (!tables[i]) 439 continue; 440 441 ret = pmu_metrics_table__for_each_metric(tables[i], fn, data); 442 if (ret) 443 return ret; 444 } 445 446 return pmu_for_each_sys_metric(metricgroup__sys_event_iter, &sys_data); 447 } 448 449 static const char *code_characters = ",-=@"; 450 451 static int encode_metric_id(struct strbuf *sb, const char *x) 452 { 453 char *c; 454 int ret = 0; 455 456 for (; *x; x++) { 457 c = strchr(code_characters, *x); 458 if (c) { 459 ret = strbuf_addch(sb, '!'); 460 if (ret) 461 break; 462 463 ret = strbuf_addch(sb, '0' + (c - code_characters)); 464 if (ret) 465 break; 466 } else { 467 ret = strbuf_addch(sb, *x); 468 if (ret) 469 break; 470 } 471 } 472 return ret; 473 } 474 475 static int decode_metric_id(struct strbuf *sb, const char *x) 476 { 477 const char *orig = x; 478 size_t i; 479 char c; 480 int ret; 481 482 for (; *x; x++) { 483 c = *x; 484 if (*x == '!') { 485 x++; 486 i = *x - '0'; 487 if (i > strlen(code_characters)) { 488 pr_err("Bad metric-id encoding in: '%s'", orig); 489 return -1; 490 } 491 c = code_characters[i]; 492 } 493 ret = strbuf_addch(sb, c); 494 if (ret) 495 return ret; 496 } 497 return 0; 498 } 499 500 static int decode_all_metric_ids(struct evlist *perf_evlist, const char *modifier) 501 { 502 struct evsel *ev; 503 struct strbuf sb = STRBUF_INIT; 504 char *cur; 505 int ret = 0; 506 507 evlist__for_each_entry(perf_evlist, ev) { 508 if (!ev->metric_id) 509 continue; 510 511 ret = strbuf_setlen(&sb, 0); 512 if (ret) 513 break; 514 515 ret = decode_metric_id(&sb, ev->metric_id); 516 if (ret) 517 break; 518 519 free((char *)ev->metric_id); 520 ev->metric_id = strdup(sb.buf); 521 if (!ev->metric_id) { 522 ret = -ENOMEM; 523 break; 524 } 525 /* 526 * If the name is just the parsed event, use the metric-id to 527 * give a more friendly display version. 528 */ 529 if (strstr(ev->name, "metric-id=")) { 530 bool has_slash = false; 531 532 zfree(&ev->name); 533 for (cur = strchr(sb.buf, '@') ; cur; cur = strchr(++cur, '@')) { 534 *cur = '/'; 535 has_slash = true; 536 } 537 538 if (modifier) { 539 if (!has_slash && !strchr(sb.buf, ':')) { 540 ret = strbuf_addch(&sb, ':'); 541 if (ret) 542 break; 543 } 544 ret = strbuf_addstr(&sb, modifier); 545 if (ret) 546 break; 547 } 548 ev->name = strdup(sb.buf); 549 if (!ev->name) { 550 ret = -ENOMEM; 551 break; 552 } 553 } 554 } 555 strbuf_release(&sb); 556 return ret; 557 } 558 559 static int metricgroup__build_event_string(struct strbuf *events, 560 const struct expr_parse_ctx *ctx, 561 const char *modifier, 562 bool group_events) 563 { 564 struct hashmap_entry *cur; 565 size_t bkt; 566 bool no_group = true, has_tool_events = false; 567 bool tool_events[TOOL_PMU__EVENT_MAX] = {false}; 568 int ret = 0; 569 570 #define RETURN_IF_NON_ZERO(x) do { if (x) return x; } while (0) 571 572 hashmap__for_each_entry(ctx->ids, cur, bkt) { 573 const char *sep, *rsep, *id = cur->pkey; 574 enum tool_pmu_event ev; 575 576 pr_debug("found event %s\n", id); 577 578 /* Always move tool events outside of the group. */ 579 ev = tool_pmu__str_to_event(id); 580 if (ev != TOOL_PMU__EVENT_NONE) { 581 has_tool_events = true; 582 tool_events[ev] = true; 583 continue; 584 } 585 /* Separate events with commas and open the group if necessary. */ 586 if (no_group) { 587 if (group_events) { 588 ret = strbuf_addch(events, '{'); 589 RETURN_IF_NON_ZERO(ret); 590 } 591 592 no_group = false; 593 } else { 594 ret = strbuf_addch(events, ','); 595 RETURN_IF_NON_ZERO(ret); 596 } 597 /* 598 * Encode the ID as an event string. Add a qualifier for 599 * metric_id that is the original name except with characters 600 * that parse-events can't parse replaced. For example, 601 * 'msr@tsc@' gets added as msr/tsc,metric-id=msr!3tsc!3/ 602 */ 603 sep = strchr(id, '@'); 604 if (sep != NULL) { 605 ret = strbuf_add(events, id, sep - id); 606 RETURN_IF_NON_ZERO(ret); 607 ret = strbuf_addch(events, '/'); 608 RETURN_IF_NON_ZERO(ret); 609 rsep = strrchr(sep, '@'); 610 ret = strbuf_add(events, sep + 1, rsep - sep - 1); 611 RETURN_IF_NON_ZERO(ret); 612 ret = strbuf_addstr(events, ",metric-id="); 613 RETURN_IF_NON_ZERO(ret); 614 sep = rsep; 615 } else { 616 sep = strchr(id, ':'); 617 if (sep != NULL) { 618 ret = strbuf_add(events, id, sep - id); 619 RETURN_IF_NON_ZERO(ret); 620 } else { 621 ret = strbuf_addstr(events, id); 622 RETURN_IF_NON_ZERO(ret); 623 } 624 ret = strbuf_addstr(events, "/metric-id="); 625 RETURN_IF_NON_ZERO(ret); 626 } 627 ret = encode_metric_id(events, id); 628 RETURN_IF_NON_ZERO(ret); 629 ret = strbuf_addstr(events, "/"); 630 RETURN_IF_NON_ZERO(ret); 631 632 if (sep != NULL) { 633 ret = strbuf_addstr(events, sep + 1); 634 RETURN_IF_NON_ZERO(ret); 635 } 636 if (modifier) { 637 ret = strbuf_addstr(events, modifier); 638 RETURN_IF_NON_ZERO(ret); 639 } 640 } 641 if (!no_group && group_events) { 642 ret = strbuf_addf(events, "}:W"); 643 RETURN_IF_NON_ZERO(ret); 644 } 645 if (has_tool_events) { 646 int i; 647 648 tool_pmu__for_each_event(i) { 649 if (tool_events[i]) { 650 if (!no_group) { 651 ret = strbuf_addch(events, ','); 652 RETURN_IF_NON_ZERO(ret); 653 } 654 no_group = false; 655 ret = strbuf_addstr(events, tool_pmu__event_to_str(i)); 656 RETURN_IF_NON_ZERO(ret); 657 } 658 } 659 } 660 661 return ret; 662 #undef RETURN_IF_NON_ZERO 663 } 664 665 int __weak arch_get_runtimeparam(const struct pmu_metric *pm __maybe_unused) 666 { 667 return 1; 668 } 669 670 /* 671 * A singly linked list on the stack of the names of metrics being 672 * processed. Used to identify recursion. 673 */ 674 struct visited_metric { 675 const char *name; 676 const struct visited_metric *parent; 677 }; 678 679 struct metricgroup_add_iter_data { 680 struct list_head *metric_list; 681 const char *pmu; 682 const char *metric_name; 683 const char *modifier; 684 int *ret; 685 bool *has_match; 686 bool metric_no_group; 687 bool metric_no_threshold; 688 const char *user_requested_cpu_list; 689 bool system_wide; 690 struct metric *root_metric; 691 const struct visited_metric *visited; 692 const struct pmu_metrics_table *table; 693 }; 694 695 static int add_metric(struct list_head *metric_list, 696 const struct pmu_metric *pm, 697 const char *modifier, 698 bool metric_no_group, 699 bool metric_no_threshold, 700 const char *user_requested_cpu_list, 701 bool system_wide, 702 struct metric *root_metric, 703 const struct visited_metric *visited, 704 const struct pmu_metrics_table *table); 705 706 static int metricgroup__find_metric_callback(const struct pmu_metric *pm, 707 const struct pmu_metrics_table *table __maybe_unused, 708 void *vdata) 709 { 710 struct pmu_metric *copied_pm = vdata; 711 712 memcpy(copied_pm, pm, sizeof(*pm)); 713 return 0; 714 } 715 716 /** 717 * resolve_metric - Locate metrics within the root metric and recursively add 718 * references to them. 719 * @metric_list: The list the metric is added to. 720 * @pmu: The PMU name to resolve metrics on, or "all" for all PMUs. 721 * @modifier: if non-null event modifiers like "u". 722 * @metric_no_group: Should events written to events be grouped "{}" or 723 * global. Grouping is the default but due to multiplexing the 724 * user may override. 725 * @user_requested_cpu_list: Command line specified CPUs to record on. 726 * @system_wide: Are events for all processes recorded. 727 * @root_metric: Metrics may reference other metrics to form a tree. In this 728 * case the root_metric holds all the IDs and a list of referenced 729 * metrics. When adding a root this argument is NULL. 730 * @visited: A singly linked list of metric names being added that is used to 731 * detect recursion. 732 * @table: The table that is searched for metrics, most commonly the table for the 733 * architecture perf is running upon. 734 */ 735 static int resolve_metric(struct list_head *metric_list, 736 struct perf_pmu *pmu, 737 const char *modifier, 738 bool metric_no_group, 739 bool metric_no_threshold, 740 const char *user_requested_cpu_list, 741 bool system_wide, 742 struct metric *root_metric, 743 const struct visited_metric *visited, 744 const struct pmu_metrics_table *table) 745 { 746 struct hashmap_entry *cur; 747 size_t bkt; 748 struct to_resolve { 749 /* The metric to resolve. */ 750 struct pmu_metric pm; 751 /* 752 * The key in the IDs map, this may differ from in case, 753 * etc. from pm->metric_name. 754 */ 755 const char *key; 756 } *pending = NULL; 757 int i, ret = 0, pending_cnt = 0; 758 759 /* 760 * Iterate all the parsed IDs and if there's a matching metric and it to 761 * the pending array. 762 */ 763 hashmap__for_each_entry(root_metric->pctx->ids, cur, bkt) { 764 struct pmu_metric pm; 765 766 if (pmu_metrics_table__find_metric(table, pmu, cur->pkey, 767 metricgroup__find_metric_callback, 768 &pm) != PMU_METRICS__NOT_FOUND) { 769 pending = realloc(pending, 770 (pending_cnt + 1) * sizeof(struct to_resolve)); 771 if (!pending) 772 return -ENOMEM; 773 774 memcpy(&pending[pending_cnt].pm, &pm, sizeof(pm)); 775 pending[pending_cnt].key = cur->pkey; 776 pending_cnt++; 777 } 778 } 779 780 /* Remove the metric IDs from the context. */ 781 for (i = 0; i < pending_cnt; i++) 782 expr__del_id(root_metric->pctx, pending[i].key); 783 784 /* 785 * Recursively add all the metrics, IDs are added to the root metric's 786 * context. 787 */ 788 for (i = 0; i < pending_cnt; i++) { 789 ret = add_metric(metric_list, &pending[i].pm, modifier, metric_no_group, 790 metric_no_threshold, user_requested_cpu_list, system_wide, 791 root_metric, visited, table); 792 if (ret) 793 break; 794 } 795 796 free(pending); 797 return ret; 798 } 799 800 /** 801 * __add_metric - Add a metric to metric_list. 802 * @metric_list: The list the metric is added to. 803 * @pm: The pmu_metric containing the metric to be added. 804 * @modifier: if non-null event modifiers like "u". 805 * @metric_no_group: Should events written to events be grouped "{}" or 806 * global. Grouping is the default but due to multiplexing the 807 * user may override. 808 * @metric_no_threshold: Should threshold expressions be ignored? 809 * @runtime: A special argument for the parser only known at runtime. 810 * @user_requested_cpu_list: Command line specified CPUs to record on. 811 * @system_wide: Are events for all processes recorded. 812 * @root_metric: Metrics may reference other metrics to form a tree. In this 813 * case the root_metric holds all the IDs and a list of referenced 814 * metrics. When adding a root this argument is NULL. 815 * @visited: A singly linked list of metric names being added that is used to 816 * detect recursion. 817 * @table: The table that is searched for metrics, most commonly the table for the 818 * architecture perf is running upon. 819 */ 820 static int __add_metric(struct list_head *metric_list, 821 const struct pmu_metric *pm, 822 const char *modifier, 823 bool metric_no_group, 824 bool metric_no_threshold, 825 int runtime, 826 const char *user_requested_cpu_list, 827 bool system_wide, 828 struct metric *root_metric, 829 const struct visited_metric *visited, 830 const struct pmu_metrics_table *table) 831 { 832 const struct visited_metric *vm; 833 int ret; 834 bool is_root = !root_metric; 835 const char *expr; 836 struct visited_metric visited_node = { 837 .name = pm->metric_name, 838 .parent = visited, 839 }; 840 841 for (vm = visited; vm; vm = vm->parent) { 842 if (!strcmp(pm->metric_name, vm->name)) { 843 pr_err("failed: recursion detected for %s\n", pm->metric_name); 844 return -1; 845 } 846 } 847 848 if (is_root) { 849 /* 850 * This metric is the root of a tree and may reference other 851 * metrics that are added recursively. 852 */ 853 root_metric = metric__new(pm, modifier, metric_no_group, metric_no_threshold, 854 runtime, user_requested_cpu_list, system_wide); 855 if (!root_metric) 856 return -ENOMEM; 857 858 } else { 859 int cnt = 0; 860 861 /* 862 * This metric was referenced in a metric higher in the 863 * tree. Check if the same metric is already resolved in the 864 * metric_refs list. 865 */ 866 if (root_metric->metric_refs) { 867 for (; root_metric->metric_refs[cnt].metric_name; cnt++) { 868 if (!strcmp(pm->metric_name, 869 root_metric->metric_refs[cnt].metric_name)) 870 return 0; 871 } 872 } 873 874 /* Create reference. Need space for the entry and the terminator. */ 875 root_metric->metric_refs = realloc(root_metric->metric_refs, 876 (cnt + 2) * sizeof(struct metric_ref)); 877 if (!root_metric->metric_refs) 878 return -ENOMEM; 879 880 /* 881 * Intentionally passing just const char pointers, 882 * from 'pe' object, so they never go away. We don't 883 * need to change them, so there's no need to create 884 * our own copy. 885 */ 886 root_metric->metric_refs[cnt].metric_name = pm->metric_name; 887 root_metric->metric_refs[cnt].metric_expr = pm->metric_expr; 888 889 /* Null terminate array. */ 890 root_metric->metric_refs[cnt+1].metric_name = NULL; 891 root_metric->metric_refs[cnt+1].metric_expr = NULL; 892 } 893 894 /* 895 * For both the parent and referenced metrics, we parse 896 * all the metric's IDs and add it to the root context. 897 */ 898 ret = 0; 899 expr = pm->metric_expr; 900 if (is_root && pm->metric_threshold) { 901 /* 902 * Threshold expressions are built off the actual metric. Switch 903 * to use that in case of additional necessary events. Change 904 * the visited node name to avoid this being flagged as 905 * recursion. If the threshold events are disabled, just use the 906 * metric's name as a reference. This allows metric threshold 907 * computation if there are sufficient events. 908 */ 909 assert(strstr(pm->metric_threshold, pm->metric_name)); 910 expr = metric_no_threshold ? pm->metric_name : pm->metric_threshold; 911 visited_node.name = "__threshold__"; 912 } 913 if (expr__find_ids(expr, NULL, root_metric->pctx) < 0) { 914 /* Broken metric. */ 915 ret = -EINVAL; 916 } 917 if (!ret) { 918 /* Resolve referenced metrics. */ 919 struct perf_pmu *pmu; 920 921 if (pm->pmu && pm->pmu[0] != '\0') 922 pmu = perf_pmus__find(pm->pmu); 923 else 924 pmu = perf_pmus__scan_core(/*pmu=*/ NULL); 925 926 ret = resolve_metric(metric_list, pmu, modifier, metric_no_group, 927 metric_no_threshold, user_requested_cpu_list, 928 system_wide, root_metric, &visited_node, 929 table); 930 } 931 if (ret) { 932 if (is_root) 933 metric__free(root_metric); 934 935 } else if (is_root) 936 list_add(&root_metric->nd, metric_list); 937 938 return ret; 939 } 940 941 static int add_metric(struct list_head *metric_list, 942 const struct pmu_metric *pm, 943 const char *modifier, 944 bool metric_no_group, 945 bool metric_no_threshold, 946 const char *user_requested_cpu_list, 947 bool system_wide, 948 struct metric *root_metric, 949 const struct visited_metric *visited, 950 const struct pmu_metrics_table *table) 951 { 952 int ret = 0; 953 954 pr_debug("metric expr %s for %s\n", pm->metric_expr, pm->metric_name); 955 956 if (!strstr(pm->metric_expr, "?")) { 957 ret = __add_metric(metric_list, pm, modifier, metric_no_group, 958 metric_no_threshold, 0, user_requested_cpu_list, 959 system_wide, root_metric, visited, table); 960 } else { 961 int j, count; 962 963 count = arch_get_runtimeparam(pm); 964 965 /* This loop is added to create multiple 966 * events depend on count value and add 967 * those events to metric_list. 968 */ 969 970 for (j = 0; j < count && !ret; j++) 971 ret = __add_metric(metric_list, pm, modifier, metric_no_group, 972 metric_no_threshold, j, user_requested_cpu_list, 973 system_wide, root_metric, visited, table); 974 } 975 976 return ret; 977 } 978 979 /** 980 * metric_list_cmp - list_sort comparator that sorts metrics with more events to 981 * the front. tool events are excluded from the count. 982 */ 983 static int metric_list_cmp(void *priv __maybe_unused, const struct list_head *l, 984 const struct list_head *r) 985 { 986 const struct metric *left = container_of(l, struct metric, nd); 987 const struct metric *right = container_of(r, struct metric, nd); 988 struct expr_id_data *data; 989 int i, left_count, right_count; 990 991 left_count = hashmap__size(left->pctx->ids); 992 tool_pmu__for_each_event(i) { 993 if (!expr__get_id(left->pctx, tool_pmu__event_to_str(i), &data)) 994 left_count--; 995 } 996 997 right_count = hashmap__size(right->pctx->ids); 998 tool_pmu__for_each_event(i) { 999 if (!expr__get_id(right->pctx, tool_pmu__event_to_str(i), &data)) 1000 right_count--; 1001 } 1002 1003 return right_count - left_count; 1004 } 1005 1006 /** 1007 * default_metricgroup_cmp - Implements complex key for the Default metricgroup 1008 * that first sorts by default_metricgroup_name, then 1009 * metric_name. 1010 */ 1011 static int default_metricgroup_cmp(void *priv __maybe_unused, 1012 const struct list_head *l, 1013 const struct list_head *r) 1014 { 1015 const struct metric *left = container_of(l, struct metric, nd); 1016 const struct metric *right = container_of(r, struct metric, nd); 1017 int diff = strcmp(right->default_metricgroup_name, left->default_metricgroup_name); 1018 1019 if (diff) 1020 return diff; 1021 1022 return strcmp(right->metric_name, left->metric_name); 1023 } 1024 1025 struct metricgroup__add_metric_data { 1026 struct list_head *list; 1027 const char *pmu; 1028 const char *metric_name; 1029 const char *modifier; 1030 const char *user_requested_cpu_list; 1031 bool metric_no_group; 1032 bool metric_no_threshold; 1033 bool system_wide; 1034 bool has_match; 1035 }; 1036 1037 static int metricgroup__add_metric_callback(const struct pmu_metric *pm, 1038 const struct pmu_metrics_table *table, 1039 void *vdata) 1040 { 1041 struct metricgroup__add_metric_data *data = vdata; 1042 int ret = 0; 1043 1044 if (pm->metric_expr && match_pm_metric_or_groups(pm, data->pmu, data->metric_name)) { 1045 bool metric_no_group = data->metric_no_group || 1046 match_metric_or_groups(pm->metricgroup_no_group, data->metric_name); 1047 1048 data->has_match = true; 1049 ret = add_metric(data->list, pm, data->modifier, metric_no_group, 1050 data->metric_no_threshold, data->user_requested_cpu_list, 1051 data->system_wide, /*root_metric=*/NULL, 1052 /*visited_metrics=*/NULL, table); 1053 } 1054 return ret; 1055 } 1056 1057 /** 1058 * metricgroup__add_metric - Find and add a metric, or a metric group. 1059 * @pmu: The PMU name to search for metrics on, or "all" for all PMUs. 1060 * @metric_name: The name of the metric or metric group. For example, "IPC" 1061 * could be the name of a metric and "TopDownL1" the name of a 1062 * metric group. 1063 * @modifier: if non-null event modifiers like "u". 1064 * @metric_no_group: Should events written to events be grouped "{}" or 1065 * global. Grouping is the default but due to multiplexing the 1066 * user may override. 1067 * @user_requested_cpu_list: Command line specified CPUs to record on. 1068 * @system_wide: Are events for all processes recorded. 1069 * @metric_list: The list that the metric or metric group are added to. 1070 * @table: The table that is searched for metrics, most commonly the table for the 1071 * architecture perf is running upon. 1072 */ 1073 static int metricgroup__add_metric(const char *pmu, const char *metric_name, const char *modifier, 1074 bool metric_no_group, bool metric_no_threshold, 1075 const char *user_requested_cpu_list, 1076 bool system_wide, 1077 struct list_head *metric_list, 1078 const struct pmu_metrics_table *table) 1079 { 1080 LIST_HEAD(list); 1081 int ret; 1082 struct metricgroup__add_metric_data data = { 1083 .list = &list, 1084 .pmu = pmu, 1085 .metric_name = metric_name, 1086 .modifier = modifier, 1087 .metric_no_group = metric_no_group, 1088 .metric_no_threshold = metric_no_threshold, 1089 .user_requested_cpu_list = user_requested_cpu_list, 1090 .system_wide = system_wide, 1091 .has_match = false, 1092 }; 1093 1094 /* 1095 * Iterate over all metrics seeing if metric matches either the 1096 * name or group. When it does add the metric to the list. 1097 */ 1098 ret = metricgroup__for_each_metric(table, metricgroup__add_metric_callback, &data); 1099 if (!ret && !data.has_match) 1100 ret = -EINVAL; 1101 1102 /* 1103 * add to metric_list so that they can be released 1104 * even if it's failed 1105 */ 1106 list_splice(&list, metric_list); 1107 return ret; 1108 } 1109 1110 /** 1111 * metricgroup__add_metric_list - Find and add metrics, or metric groups, 1112 * specified in a list. 1113 * @pmu: A pmu to restrict the metrics to, or "all" for all PMUS. 1114 * @list: the list of metrics or metric groups. For example, "IPC,CPI,TopDownL1" 1115 * would match the IPC and CPI metrics, and TopDownL1 would match all 1116 * the metrics in the TopDownL1 group. 1117 * @metric_no_group: Should events written to events be grouped "{}" or 1118 * global. Grouping is the default but due to multiplexing the 1119 * user may override. 1120 * @user_requested_cpu_list: Command line specified CPUs to record on. 1121 * @system_wide: Are events for all processes recorded. 1122 * @metric_list: The list that metrics are added to. 1123 * @table: The table that is searched for metrics, most commonly the table for the 1124 * architecture perf is running upon. 1125 */ 1126 static int metricgroup__add_metric_list(const char *pmu, const char *list, 1127 bool metric_no_group, 1128 bool metric_no_threshold, 1129 const char *user_requested_cpu_list, 1130 bool system_wide, struct list_head *metric_list, 1131 const struct pmu_metrics_table *table) 1132 { 1133 char *list_itr, *list_copy, *metric_name, *modifier; 1134 int ret, count = 0; 1135 1136 list_copy = strdup(list); 1137 if (!list_copy) 1138 return -ENOMEM; 1139 list_itr = list_copy; 1140 1141 while ((metric_name = strsep(&list_itr, ",")) != NULL) { 1142 modifier = strchr(metric_name, ':'); 1143 if (modifier) 1144 *modifier++ = '\0'; 1145 1146 ret = metricgroup__add_metric(pmu, metric_name, modifier, 1147 metric_no_group, metric_no_threshold, 1148 user_requested_cpu_list, 1149 system_wide, metric_list, table); 1150 if (ret == -EINVAL) 1151 pr_err("Cannot find metric or group `%s'\n", metric_name); 1152 1153 if (ret) 1154 break; 1155 1156 count++; 1157 } 1158 free(list_copy); 1159 1160 if (!ret) { 1161 /* 1162 * Warn about nmi_watchdog if any parsed metrics had the 1163 * NO_NMI_WATCHDOG constraint. 1164 */ 1165 metric__watchdog_constraint_hint(NULL, /*foot=*/true); 1166 /* No metrics. */ 1167 if (count == 0) 1168 return -EINVAL; 1169 } 1170 return ret; 1171 } 1172 1173 static void metricgroup__free_metrics(struct list_head *metric_list) 1174 { 1175 struct metric *m, *tmp; 1176 1177 list_for_each_entry_safe (m, tmp, metric_list, nd) { 1178 list_del_init(&m->nd); 1179 metric__free(m); 1180 } 1181 } 1182 1183 /** 1184 * find_tool_events - Search for the pressence of tool events in metric_list. 1185 * @metric_list: List to take metrics from. 1186 * @tool_events: Array of false values, indices corresponding to tool events set 1187 * to true if tool event is found. 1188 */ 1189 static void find_tool_events(const struct list_head *metric_list, 1190 bool tool_events[TOOL_PMU__EVENT_MAX]) 1191 { 1192 struct metric *m; 1193 1194 list_for_each_entry(m, metric_list, nd) { 1195 int i; 1196 1197 tool_pmu__for_each_event(i) { 1198 struct expr_id_data *data; 1199 1200 if (!tool_events[i] && 1201 !expr__get_id(m->pctx, tool_pmu__event_to_str(i), &data)) 1202 tool_events[i] = true; 1203 } 1204 } 1205 } 1206 1207 /** 1208 * build_combined_expr_ctx - Make an expr_parse_ctx with all !group_events 1209 * metric IDs, as the IDs are held in a set, 1210 * duplicates will be removed. 1211 * @metric_list: List to take metrics from. 1212 * @combined: Out argument for result. 1213 */ 1214 static int build_combined_expr_ctx(const struct list_head *metric_list, 1215 struct expr_parse_ctx **combined) 1216 { 1217 struct hashmap_entry *cur; 1218 size_t bkt; 1219 struct metric *m; 1220 char *dup; 1221 int ret; 1222 1223 *combined = expr__ctx_new(); 1224 if (!*combined) 1225 return -ENOMEM; 1226 1227 list_for_each_entry(m, metric_list, nd) { 1228 if (!m->group_events && !m->modifier) { 1229 hashmap__for_each_entry(m->pctx->ids, cur, bkt) { 1230 dup = strdup(cur->pkey); 1231 if (!dup) { 1232 ret = -ENOMEM; 1233 goto err_out; 1234 } 1235 ret = expr__add_id(*combined, dup); 1236 if (ret) 1237 goto err_out; 1238 } 1239 } 1240 } 1241 return 0; 1242 err_out: 1243 expr__ctx_free(*combined); 1244 *combined = NULL; 1245 return ret; 1246 } 1247 1248 /** 1249 * parse_ids - Build the event string for the ids and parse them creating an 1250 * evlist. The encoded metric_ids are decoded. 1251 * @metric_no_merge: is metric sharing explicitly disabled. 1252 * @fake_pmu: use a fake PMU when testing metrics not supported by the current CPU. 1253 * @ids: the event identifiers parsed from a metric. 1254 * @modifier: any modifiers added to the events. 1255 * @group_events: should events be placed in a weak group. 1256 * @tool_events: entries set true if the tool event of index could be present in 1257 * the overall list of metrics. 1258 * @out_evlist: the created list of events. 1259 */ 1260 static int parse_ids(bool metric_no_merge, bool fake_pmu, 1261 struct expr_parse_ctx *ids, const char *modifier, 1262 bool group_events, const bool tool_events[TOOL_PMU__EVENT_MAX], 1263 struct evlist **out_evlist) 1264 { 1265 struct parse_events_error parse_error; 1266 struct evlist *parsed_evlist; 1267 struct strbuf events = STRBUF_INIT; 1268 int ret; 1269 1270 *out_evlist = NULL; 1271 if (!metric_no_merge || hashmap__size(ids->ids) == 0) { 1272 bool added_event = false; 1273 int i; 1274 /* 1275 * We may fail to share events between metrics because a tool 1276 * event isn't present in one metric. For example, a ratio of 1277 * cache misses doesn't need duration_time but the same events 1278 * may be used for a misses per second. Events without sharing 1279 * implies multiplexing, that is best avoided, so place 1280 * all tool events in every group. 1281 * 1282 * Also, there may be no ids/events in the expression parsing 1283 * context because of constant evaluation, e.g.: 1284 * event1 if #smt_on else 0 1285 * Add a tool event to avoid a parse error on an empty string. 1286 */ 1287 tool_pmu__for_each_event(i) { 1288 if (tool_events[i]) { 1289 char *tmp = strdup(tool_pmu__event_to_str(i)); 1290 1291 if (!tmp) 1292 return -ENOMEM; 1293 ids__insert(ids->ids, tmp); 1294 added_event = true; 1295 } 1296 } 1297 if (!added_event && hashmap__size(ids->ids) == 0) { 1298 char *tmp = strdup("duration_time"); 1299 1300 if (!tmp) 1301 return -ENOMEM; 1302 ids__insert(ids->ids, tmp); 1303 } 1304 } 1305 ret = metricgroup__build_event_string(&events, ids, modifier, 1306 group_events); 1307 if (ret) 1308 return ret; 1309 1310 parsed_evlist = evlist__new(); 1311 if (!parsed_evlist) { 1312 ret = -ENOMEM; 1313 goto err_out; 1314 } 1315 pr_debug("Parsing metric events '%s'\n", events.buf); 1316 parse_events_error__init(&parse_error); 1317 ret = __parse_events(parsed_evlist, events.buf, /*pmu_filter=*/NULL, 1318 &parse_error, fake_pmu, /*warn_if_reordered=*/false, 1319 /*fake_tp=*/false); 1320 if (ret) { 1321 parse_events_error__print(&parse_error, events.buf); 1322 goto err_out; 1323 } 1324 ret = decode_all_metric_ids(parsed_evlist, modifier); 1325 if (ret) 1326 goto err_out; 1327 1328 *out_evlist = parsed_evlist; 1329 parsed_evlist = NULL; 1330 err_out: 1331 parse_events_error__exit(&parse_error); 1332 evlist__delete(parsed_evlist); 1333 strbuf_release(&events); 1334 return ret; 1335 } 1336 1337 /* How many times will a given evsel be used in a set of metrics? */ 1338 static int count_uses(struct list_head *metric_list, struct evsel *evsel) 1339 { 1340 const char *metric_id = evsel__metric_id(evsel); 1341 struct metric *m; 1342 int uses = 0; 1343 1344 list_for_each_entry(m, metric_list, nd) { 1345 if (hashmap__find(m->pctx->ids, metric_id, NULL)) 1346 uses++; 1347 } 1348 return uses; 1349 } 1350 1351 /* 1352 * Select the evsel that stat-display will use to trigger shadow/metric 1353 * printing. Pick the least shared non-tool evsel, encouraging metrics to be 1354 * with a hardware counter that is specific to them. 1355 */ 1356 static struct evsel *pick_display_evsel(struct list_head *metric_list, 1357 struct evsel **metric_events) 1358 { 1359 struct evsel *selected = metric_events[0]; 1360 size_t selected_uses; 1361 bool selected_is_tool; 1362 1363 if (!selected) 1364 return NULL; 1365 1366 selected_uses = count_uses(metric_list, selected); 1367 selected_is_tool = evsel__is_tool(selected); 1368 for (int i = 1; metric_events[i]; i++) { 1369 struct evsel *candidate = metric_events[i]; 1370 size_t candidate_uses = count_uses(metric_list, candidate); 1371 1372 if ((selected_is_tool && !evsel__is_tool(candidate)) || 1373 (candidate_uses < selected_uses)) { 1374 selected = candidate; 1375 selected_uses = candidate_uses; 1376 selected_is_tool = evsel__is_tool(selected); 1377 } 1378 } 1379 return selected; 1380 } 1381 1382 static int parse_groups(struct evlist *perf_evlist, 1383 const char *pmu, const char *str, 1384 bool metric_no_group, 1385 bool metric_no_merge, 1386 bool metric_no_threshold, 1387 const char *user_requested_cpu_list, 1388 bool system_wide, 1389 bool fake_pmu, 1390 const struct pmu_metrics_table *table) 1391 { 1392 struct evlist *combined_evlist = NULL; 1393 LIST_HEAD(metric_list); 1394 struct metric *m; 1395 bool tool_events[TOOL_PMU__EVENT_MAX] = {false}; 1396 bool is_default = !strcmp(str, "Default"); 1397 int ret; 1398 1399 ret = metricgroup__add_metric_list(pmu, str, metric_no_group, metric_no_threshold, 1400 user_requested_cpu_list, 1401 system_wide, &metric_list, table); 1402 if (ret) 1403 goto out; 1404 1405 /* Sort metrics from largest to smallest. */ 1406 list_sort(NULL, &metric_list, metric_list_cmp); 1407 1408 if (!metric_no_merge) { 1409 struct expr_parse_ctx *combined = NULL; 1410 1411 find_tool_events(&metric_list, tool_events); 1412 1413 ret = build_combined_expr_ctx(&metric_list, &combined); 1414 1415 if (!ret && combined && hashmap__size(combined->ids)) { 1416 ret = parse_ids(metric_no_merge, fake_pmu, combined, 1417 /*modifier=*/NULL, 1418 /*group_events=*/false, 1419 tool_events, 1420 &combined_evlist); 1421 } 1422 if (combined) 1423 expr__ctx_free(combined); 1424 1425 if (ret) 1426 goto out; 1427 } 1428 1429 if (is_default) 1430 list_sort(NULL, &metric_list, default_metricgroup_cmp); 1431 1432 list_for_each_entry(m, &metric_list, nd) { 1433 struct metric_event *me; 1434 struct evsel **metric_events; 1435 struct evlist *metric_evlist = NULL; 1436 struct metric *n; 1437 struct metric_expr *expr; 1438 1439 if (combined_evlist && !m->group_events) { 1440 metric_evlist = combined_evlist; 1441 } else if (!metric_no_merge) { 1442 /* 1443 * See if the IDs for this metric are a subset of an 1444 * earlier metric. 1445 */ 1446 list_for_each_entry(n, &metric_list, nd) { 1447 if (m == n) 1448 break; 1449 1450 if (n->evlist == NULL) 1451 continue; 1452 1453 if ((!m->modifier && n->modifier) || 1454 (m->modifier && !n->modifier) || 1455 (m->modifier && n->modifier && 1456 strcmp(m->modifier, n->modifier))) 1457 continue; 1458 1459 if ((!m->pmu && n->pmu) || 1460 (m->pmu && !n->pmu) || 1461 (m->pmu && n->pmu && strcmp(m->pmu, n->pmu))) 1462 continue; 1463 1464 if (expr__subset_of_ids(n->pctx, m->pctx)) { 1465 pr_debug("Events in '%s' fully contained within '%s'\n", 1466 m->metric_name, n->metric_name); 1467 metric_evlist = n->evlist; 1468 break; 1469 } 1470 1471 } 1472 } 1473 if (!metric_evlist) { 1474 ret = parse_ids(metric_no_merge, fake_pmu, m->pctx, m->modifier, 1475 m->group_events, tool_events, &m->evlist); 1476 if (ret) 1477 goto out; 1478 1479 metric_evlist = m->evlist; 1480 } 1481 ret = setup_metric_events(fake_pmu ? "all" : m->pmu, m->pctx->ids, 1482 metric_evlist, &metric_events); 1483 if (ret) { 1484 pr_err("Cannot resolve IDs for %s: %s\n", 1485 m->metric_name, m->metric_expr); 1486 goto out; 1487 } 1488 1489 me = metricgroup__lookup(&perf_evlist->metric_events, 1490 pick_display_evsel(&metric_list, metric_events), 1491 /*create=*/true); 1492 1493 expr = malloc(sizeof(struct metric_expr)); 1494 if (!expr) { 1495 ret = -ENOMEM; 1496 free(metric_events); 1497 goto out; 1498 } 1499 1500 expr->metric_refs = m->metric_refs; 1501 m->metric_refs = NULL; 1502 expr->metric_expr = m->metric_expr; 1503 if (m->modifier) { 1504 char *tmp; 1505 1506 if (asprintf(&tmp, "%s:%s", m->metric_name, m->modifier) < 0) 1507 expr->metric_name = NULL; 1508 else 1509 expr->metric_name = tmp; 1510 } else 1511 expr->metric_name = strdup(m->metric_name); 1512 1513 if (!expr->metric_name) { 1514 ret = -ENOMEM; 1515 free(expr); 1516 free(metric_events); 1517 goto out; 1518 } 1519 if (m->default_show_events) { 1520 struct evsel *pos; 1521 1522 for (int i = 0; metric_events[i]; i++) 1523 metric_events[i]->default_show_events = true; 1524 evlist__for_each_entry(metric_evlist, pos) { 1525 if (pos->metric_leader && pos->metric_leader->default_show_events) 1526 pos->default_show_events = true; 1527 } 1528 } 1529 expr->metric_threshold = m->metric_threshold; 1530 expr->metric_unit = m->metric_unit; 1531 expr->metric_events = metric_events; 1532 expr->runtime = m->pctx->sctx.runtime; 1533 expr->default_metricgroup_name = m->default_metricgroup_name; 1534 me->is_default = is_default; 1535 list_add(&expr->nd, &me->head); 1536 } 1537 1538 1539 if (combined_evlist) { 1540 evlist__splice_list_tail(perf_evlist, &combined_evlist->core.entries); 1541 evlist__delete(combined_evlist); 1542 } 1543 1544 list_for_each_entry(m, &metric_list, nd) { 1545 if (m->evlist) 1546 evlist__splice_list_tail(perf_evlist, &m->evlist->core.entries); 1547 } 1548 1549 out: 1550 metricgroup__free_metrics(&metric_list); 1551 return ret; 1552 } 1553 1554 int metricgroup__parse_groups(struct evlist *perf_evlist, 1555 const char *pmu, 1556 const char *str, 1557 bool metric_no_group, 1558 bool metric_no_merge, 1559 bool metric_no_threshold, 1560 const char *user_requested_cpu_list, 1561 bool system_wide, 1562 bool hardware_aware_grouping) 1563 { 1564 const struct pmu_metrics_table *table = pmu_metrics_table__find(); 1565 1566 if (!table) 1567 return -EINVAL; 1568 if (hardware_aware_grouping) 1569 pr_debug("Use hardware aware grouping instead of traditional metric grouping method\n"); 1570 1571 return parse_groups(perf_evlist, pmu, str, metric_no_group, metric_no_merge, 1572 metric_no_threshold, user_requested_cpu_list, system_wide, 1573 /*fake_pmu=*/false, table); 1574 } 1575 1576 int metricgroup__parse_groups_test(struct evlist *evlist, 1577 const struct pmu_metrics_table *table, 1578 const char *str) 1579 { 1580 return parse_groups(evlist, "all", str, 1581 /*metric_no_group=*/false, 1582 /*metric_no_merge=*/false, 1583 /*metric_no_threshold=*/false, 1584 /*user_requested_cpu_list=*/NULL, 1585 /*system_wide=*/false, 1586 /*fake_pmu=*/true, table); 1587 } 1588 1589 struct metricgroup__has_metric_data { 1590 const char *pmu; 1591 const char *metric_or_groups; 1592 }; 1593 static int metricgroup__has_metric_or_groups_callback(const struct pmu_metric *pm, 1594 const struct pmu_metrics_table *table 1595 __maybe_unused, 1596 void *vdata) 1597 { 1598 struct metricgroup__has_metric_data *data = vdata; 1599 1600 return match_pm_metric_or_groups(pm, data->pmu, data->metric_or_groups) ? 1 : 0; 1601 } 1602 1603 bool metricgroup__has_metric_or_groups(const char *pmu, const char *metric_or_groups) 1604 { 1605 const struct pmu_metrics_table *tables[2] = { 1606 pmu_metrics_table__find(), 1607 pmu_metrics_table__default(), 1608 }; 1609 struct metricgroup__has_metric_data data = { 1610 .pmu = pmu, 1611 .metric_or_groups = metric_or_groups, 1612 }; 1613 1614 for (size_t i = 0; i < ARRAY_SIZE(tables); i++) { 1615 if (pmu_metrics_table__for_each_metric(tables[i], 1616 metricgroup__has_metric_or_groups_callback, 1617 &data)) 1618 return true; 1619 } 1620 return false; 1621 } 1622 1623 static int metricgroup__topdown_max_level_callback(const struct pmu_metric *pm, 1624 const struct pmu_metrics_table *table __maybe_unused, 1625 void *data) 1626 { 1627 unsigned int *max_level = data; 1628 unsigned int level; 1629 const char *p = strstr(pm->metric_group ?: "", "TopdownL"); 1630 1631 if (!p || p[8] == '\0') 1632 return 0; 1633 1634 level = p[8] - '0'; 1635 if (level > *max_level) 1636 *max_level = level; 1637 1638 return 0; 1639 } 1640 1641 unsigned int metricgroups__topdown_max_level(void) 1642 { 1643 unsigned int max_level = 0; 1644 const struct pmu_metrics_table *table = pmu_metrics_table__find(); 1645 1646 if (!table) 1647 return false; 1648 1649 pmu_metrics_table__for_each_metric(table, metricgroup__topdown_max_level_callback, 1650 &max_level); 1651 return max_level; 1652 } 1653 1654 int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp, 1655 struct rblist *new_metric_events, 1656 struct rblist *old_metric_events) 1657 { 1658 unsigned int i; 1659 1660 for (i = 0; i < rblist__nr_entries(old_metric_events); i++) { 1661 struct rb_node *nd; 1662 struct metric_event *old_me, *new_me; 1663 struct metric_expr *old_expr, *new_expr; 1664 struct evsel *evsel; 1665 size_t alloc_size; 1666 int idx, nr; 1667 1668 nd = rblist__entry(old_metric_events, i); 1669 old_me = container_of(nd, struct metric_event, nd); 1670 1671 evsel = evlist__find_evsel(evlist, old_me->evsel->core.idx); 1672 if (!evsel) 1673 return -EINVAL; 1674 new_me = metricgroup__lookup(new_metric_events, evsel, /*create=*/true); 1675 if (!new_me) 1676 return -ENOMEM; 1677 1678 pr_debug("copying metric event for cgroup '%s': %s (idx=%d)\n", 1679 cgrp ? cgrp->name : "root", evsel->name, evsel->core.idx); 1680 1681 new_me->is_default = old_me->is_default; 1682 list_for_each_entry(old_expr, &old_me->head, nd) { 1683 new_expr = malloc(sizeof(*new_expr)); 1684 if (!new_expr) 1685 return -ENOMEM; 1686 1687 new_expr->metric_expr = old_expr->metric_expr; 1688 new_expr->metric_threshold = old_expr->metric_threshold; 1689 new_expr->metric_name = strdup(old_expr->metric_name); 1690 if (!new_expr->metric_name) 1691 return -ENOMEM; 1692 1693 new_expr->metric_unit = old_expr->metric_unit; 1694 new_expr->runtime = old_expr->runtime; 1695 new_expr->default_metricgroup_name = old_expr->default_metricgroup_name; 1696 1697 if (old_expr->metric_refs) { 1698 /* calculate number of metric_events */ 1699 for (nr = 0; old_expr->metric_refs[nr].metric_name; nr++) 1700 continue; 1701 alloc_size = sizeof(*new_expr->metric_refs); 1702 new_expr->metric_refs = calloc(nr + 1, alloc_size); 1703 if (!new_expr->metric_refs) { 1704 free(new_expr); 1705 return -ENOMEM; 1706 } 1707 1708 memcpy(new_expr->metric_refs, old_expr->metric_refs, 1709 nr * alloc_size); 1710 } else { 1711 new_expr->metric_refs = NULL; 1712 } 1713 1714 /* calculate number of metric_events */ 1715 for (nr = 0; old_expr->metric_events[nr]; nr++) 1716 continue; 1717 alloc_size = sizeof(*new_expr->metric_events); 1718 new_expr->metric_events = calloc(nr + 1, alloc_size); 1719 if (!new_expr->metric_events) { 1720 zfree(&new_expr->metric_refs); 1721 free(new_expr); 1722 return -ENOMEM; 1723 } 1724 1725 /* copy evsel in the same position */ 1726 for (idx = 0; idx < nr; idx++) { 1727 evsel = old_expr->metric_events[idx]; 1728 evsel = evlist__find_evsel(evlist, evsel->core.idx); 1729 if (evsel == NULL) { 1730 zfree(&new_expr->metric_events); 1731 zfree(&new_expr->metric_refs); 1732 free(new_expr); 1733 return -EINVAL; 1734 } 1735 new_expr->metric_events[idx] = evsel; 1736 } 1737 1738 list_add(&new_expr->nd, &new_me->head); 1739 } 1740 } 1741 return 0; 1742 } 1743