12025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2b18f3e36SAndi Kleen /* 3b18f3e36SAndi Kleen * Copyright (c) 2017, Intel Corporation. 4b18f3e36SAndi Kleen */ 5b18f3e36SAndi Kleen 6b18f3e36SAndi Kleen /* Manage metrics and groups of metrics from JSON files */ 7b18f3e36SAndi Kleen 8b18f3e36SAndi Kleen #include "metricgroup.h" 9b4209025SArnaldo Carvalho de Melo #include "debug.h" 10b18f3e36SAndi Kleen #include "evlist.h" 110b8026e8SArnaldo Carvalho de Melo #include "evsel.h" 12b18f3e36SAndi Kleen #include "strbuf.h" 13b18f3e36SAndi Kleen #include "pmu.h" 141eaf496eSIan Rogers #include "pmus.h" 15e5c6109fSIan Rogers #include "print-events.h" 1690053634SIan Rogers #include "smt.h" 17b18f3e36SAndi Kleen #include "expr.h" 18b18f3e36SAndi Kleen #include "rblist.h" 19b18f3e36SAndi Kleen #include <string.h> 20b18f3e36SAndi Kleen #include <errno.h> 21b18f3e36SAndi Kleen #include "strlist.h" 22b18f3e36SAndi Kleen #include <assert.h> 23bd9860bfSArnaldo Carvalho de Melo #include <linux/ctype.h> 2480be6434SIan Rogers #include <linux/list_sort.h> 25b4209025SArnaldo Carvalho de Melo #include <linux/string.h> 26d8f9da24SArnaldo Carvalho de Melo #include <linux/zalloc.h> 271725e9cdSIan Rogers #include <perf/cpumap.h> 280b8026e8SArnaldo Carvalho de Melo #include <subcmd/parse-options.h> 29ab483d8bSKan Liang #include <api/fs/fs.h> 30ab483d8bSKan Liang #include "util.h" 31f6fb0960SJiri Olsa #include <asm/bug.h> 32b214ba8cSNamhyung Kim #include "cgroup.h" 33bd560973SIan Rogers #include "util/hashmap.h" 34b18f3e36SAndi Kleen 35b18f3e36SAndi Kleen struct metric_event *metricgroup__lookup(struct rblist *metric_events, 3632dcd021SJiri Olsa struct evsel *evsel, 37b18f3e36SAndi Kleen bool create) 38b18f3e36SAndi Kleen { 39b18f3e36SAndi Kleen struct rb_node *nd; 40b18f3e36SAndi Kleen struct metric_event me = { 41b18f3e36SAndi Kleen .evsel = evsel 42b18f3e36SAndi Kleen }; 434bd1bef8SAndi Kleen 444bd1bef8SAndi Kleen if (!metric_events) 454bd1bef8SAndi Kleen return NULL; 464bd1bef8SAndi Kleen 4797b6b4acSIan Rogers if (evsel && evsel->metric_leader) 48a59fb796SIan Rogers me.evsel = evsel->metric_leader; 49b18f3e36SAndi Kleen nd = rblist__find(metric_events, &me); 50b18f3e36SAndi Kleen if (nd) 51b18f3e36SAndi Kleen return container_of(nd, struct metric_event, nd); 52b18f3e36SAndi Kleen if (create) { 53b18f3e36SAndi Kleen rblist__add_node(metric_events, &me); 54b18f3e36SAndi Kleen nd = rblist__find(metric_events, &me); 55b18f3e36SAndi Kleen if (nd) 56b18f3e36SAndi Kleen return container_of(nd, struct metric_event, nd); 57b18f3e36SAndi Kleen } 58b18f3e36SAndi Kleen return NULL; 59b18f3e36SAndi Kleen } 60b18f3e36SAndi Kleen 61b18f3e36SAndi Kleen static int metric_event_cmp(struct rb_node *rb_node, const void *entry) 62b18f3e36SAndi Kleen { 63b18f3e36SAndi Kleen struct metric_event *a = container_of(rb_node, 64b18f3e36SAndi Kleen struct metric_event, 65b18f3e36SAndi Kleen nd); 66b18f3e36SAndi Kleen const struct metric_event *b = entry; 67b18f3e36SAndi Kleen 68b18f3e36SAndi Kleen if (a->evsel == b->evsel) 69b18f3e36SAndi Kleen return 0; 70b18f3e36SAndi Kleen if ((char *)a->evsel < (char *)b->evsel) 71b18f3e36SAndi Kleen return -1; 72b18f3e36SAndi Kleen return +1; 73b18f3e36SAndi Kleen } 74b18f3e36SAndi Kleen 75b18f3e36SAndi Kleen static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused, 76b18f3e36SAndi Kleen const void *entry) 77b18f3e36SAndi Kleen { 78b18f3e36SAndi Kleen struct metric_event *me = malloc(sizeof(struct metric_event)); 79b18f3e36SAndi Kleen 80b18f3e36SAndi Kleen if (!me) 81b18f3e36SAndi Kleen return NULL; 82b18f3e36SAndi Kleen memcpy(me, entry, sizeof(struct metric_event)); 83b18f3e36SAndi Kleen me->evsel = ((struct metric_event *)entry)->evsel; 841c0e4795SKan Liang me->is_default = false; 85b18f3e36SAndi Kleen INIT_LIST_HEAD(&me->head); 86b18f3e36SAndi Kleen return &me->nd; 87b18f3e36SAndi Kleen } 88b18f3e36SAndi Kleen 899afe5658SJiri Olsa static void metric_event_delete(struct rblist *rblist __maybe_unused, 909afe5658SJiri Olsa struct rb_node *rb_node) 919afe5658SJiri Olsa { 929afe5658SJiri Olsa struct metric_event *me = container_of(rb_node, struct metric_event, nd); 939afe5658SJiri Olsa struct metric_expr *expr, *tmp; 949afe5658SJiri Olsa 959afe5658SJiri Olsa list_for_each_entry_safe(expr, tmp, &me->head, nd) { 9611ff9bcdSArnaldo Carvalho de Melo zfree(&expr->metric_name); 9711ff9bcdSArnaldo Carvalho de Melo zfree(&expr->metric_refs); 9811ff9bcdSArnaldo Carvalho de Melo zfree(&expr->metric_events); 999afe5658SJiri Olsa free(expr); 1009afe5658SJiri Olsa } 1019afe5658SJiri Olsa 1029afe5658SJiri Olsa free(me); 1039afe5658SJiri Olsa } 1049afe5658SJiri Olsa 105b18f3e36SAndi Kleen static void metricgroup__rblist_init(struct rblist *metric_events) 106b18f3e36SAndi Kleen { 107b18f3e36SAndi Kleen rblist__init(metric_events); 108b18f3e36SAndi Kleen metric_events->node_cmp = metric_event_cmp; 109b18f3e36SAndi Kleen metric_events->node_new = metric_event_new; 1109afe5658SJiri Olsa metric_events->node_delete = metric_event_delete; 1119afe5658SJiri Olsa } 1129afe5658SJiri Olsa 1139afe5658SJiri Olsa void metricgroup__rblist_exit(struct rblist *metric_events) 1149afe5658SJiri Olsa { 1159afe5658SJiri Olsa rblist__exit(metric_events); 116b18f3e36SAndi Kleen } 117b18f3e36SAndi Kleen 118485fcaedSIan Rogers /** 119485fcaedSIan Rogers * The metric under construction. The data held here will be placed in a 120485fcaedSIan Rogers * metric_expr. 121485fcaedSIan Rogers */ 122a0c05b36SJiri Olsa struct metric { 123b18f3e36SAndi Kleen struct list_head nd; 124485fcaedSIan Rogers /** 125485fcaedSIan Rogers * The expression parse context importantly holding the IDs contained 126485fcaedSIan Rogers * within the expression. 127485fcaedSIan Rogers */ 128cb94a02eSIan Rogers struct expr_parse_ctx *pctx; 129bd3846d0SIan Rogers const char *pmu; 130485fcaedSIan Rogers /** The name of the metric such as "IPC". */ 131b18f3e36SAndi Kleen const char *metric_name; 132b85a4d61SIan Rogers /** Modifier on the metric such as "u" or NULL for none. */ 133b85a4d61SIan Rogers const char *modifier; 134485fcaedSIan Rogers /** The expression to parse, for example, "instructions/cycles". */ 135b18f3e36SAndi Kleen const char *metric_expr; 136d0a3052fSIan Rogers /** Optional threshold expression where zero value is green, otherwise red. */ 137d0a3052fSIan Rogers const char *metric_threshold; 138485fcaedSIan Rogers /** 139485fcaedSIan Rogers * The "ScaleUnit" that scales and adds a unit to the metric during 140485fcaedSIan Rogers * output. 141485fcaedSIan Rogers */ 142287f2649SJin Yao const char *metric_unit; 143b0a9e8f8SKan Liang /** 144b0a9e8f8SKan Liang * Optional name of the metric group reported 145b0a9e8f8SKan Liang * if the Default metric group is being processed. 146b0a9e8f8SKan Liang */ 147b0a9e8f8SKan Liang const char *default_metricgroup_name; 14846bdc0bfSIan Rogers /** Optional null terminated array of referenced metrics. */ 14946bdc0bfSIan Rogers struct metric_ref *metric_refs; 150485fcaedSIan Rogers /** 151180a5013SIan Rogers * Should events of the metric be grouped? 152485fcaedSIan Rogers */ 153180a5013SIan Rogers bool group_events; 1545ecd5a0cSIan Rogers /** 1555ecd5a0cSIan Rogers * Parsed events for the metric. Optional as events may be taken from a 1565ecd5a0cSIan Rogers * different metric whose group contains all the IDs necessary for this 1575ecd5a0cSIan Rogers * one. 1585ecd5a0cSIan Rogers */ 1595ecd5a0cSIan Rogers struct evlist *evlist; 160b18f3e36SAndi Kleen }; 161b18f3e36SAndi Kleen 162180a5013SIan Rogers static void metric__watchdog_constraint_hint(const char *name, bool foot) 1633d81d761SIan Rogers { 1643d81d761SIan Rogers static bool violate_nmi_constraint; 1653d81d761SIan Rogers 1663d81d761SIan Rogers if (!foot) { 167180a5013SIan Rogers pr_warning("Not grouping metric %s's events.\n", name); 1683d81d761SIan Rogers violate_nmi_constraint = true; 1693d81d761SIan Rogers return; 1703d81d761SIan Rogers } 1713d81d761SIan Rogers 1723d81d761SIan Rogers if (!violate_nmi_constraint) 1733d81d761SIan Rogers return; 1743d81d761SIan Rogers 1753d81d761SIan Rogers pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n" 1763d81d761SIan Rogers " echo 0 > /proc/sys/kernel/nmi_watchdog\n" 1773d81d761SIan Rogers " perf stat ...\n" 1783d81d761SIan Rogers " echo 1 > /proc/sys/kernel/nmi_watchdog\n"); 1793d81d761SIan Rogers } 1803d81d761SIan Rogers 181180a5013SIan Rogers static bool metric__group_events(const struct pmu_metric *pm) 1823d81d761SIan Rogers { 18390053634SIan Rogers switch (pm->event_grouping) { 18490053634SIan Rogers case MetricNoGroupEvents: 18590053634SIan Rogers return false; 18690053634SIan Rogers case MetricNoGroupEventsNmi: 18790053634SIan Rogers if (!sysctl__nmi_watchdog_enabled()) 188180a5013SIan Rogers return true; 189180a5013SIan Rogers metric__watchdog_constraint_hint(pm->metric_name, /*foot=*/false); 190180a5013SIan Rogers return false; 19190053634SIan Rogers case MetricNoGroupEventsSmt: 19290053634SIan Rogers return !smt_on(); 19390053634SIan Rogers case MetricGroupEvents: 19490053634SIan Rogers default: 195180a5013SIan Rogers return true; 1963d81d761SIan Rogers } 19790053634SIan Rogers } 1983d81d761SIan Rogers 1991725e9cdSIan Rogers static void metric__free(struct metric *m) 2001725e9cdSIan Rogers { 2011725e9cdSIan Rogers if (!m) 2021725e9cdSIan Rogers return; 2031725e9cdSIan Rogers 20411ff9bcdSArnaldo Carvalho de Melo zfree(&m->metric_refs); 2051725e9cdSIan Rogers expr__ctx_free(m->pctx); 20611ff9bcdSArnaldo Carvalho de Melo zfree(&m->modifier); 2071725e9cdSIan Rogers evlist__delete(m->evlist); 2081725e9cdSIan Rogers free(m); 2091725e9cdSIan Rogers } 2101725e9cdSIan Rogers 211db95818eSIan Rogers static struct metric *metric__new(const struct pmu_metric *pm, 212b85a4d61SIan Rogers const char *modifier, 2133d81d761SIan Rogers bool metric_no_group, 2141725e9cdSIan Rogers int runtime, 2151725e9cdSIan Rogers const char *user_requested_cpu_list, 2161725e9cdSIan Rogers bool system_wide) 2173d81d761SIan Rogers { 2183d81d761SIan Rogers struct metric *m; 2193d81d761SIan Rogers 2203d81d761SIan Rogers m = zalloc(sizeof(*m)); 2213d81d761SIan Rogers if (!m) 2223d81d761SIan Rogers return NULL; 2233d81d761SIan Rogers 2243d81d761SIan Rogers m->pctx = expr__ctx_new(); 2251725e9cdSIan Rogers if (!m->pctx) 2261725e9cdSIan Rogers goto out_err; 2273d81d761SIan Rogers 228bd3846d0SIan Rogers m->pmu = pm->pmu ?: "cpu"; 229db95818eSIan Rogers m->metric_name = pm->metric_name; 230e2b005d6SIan Rogers m->default_metricgroup_name = pm->default_metricgroup_name ?: ""; 2311725e9cdSIan Rogers m->modifier = NULL; 2321725e9cdSIan Rogers if (modifier) { 2331725e9cdSIan Rogers m->modifier = strdup(modifier); 2341725e9cdSIan Rogers if (!m->modifier) 2351725e9cdSIan Rogers goto out_err; 236b85a4d61SIan Rogers } 237db95818eSIan Rogers m->metric_expr = pm->metric_expr; 238d0a3052fSIan Rogers m->metric_threshold = pm->metric_threshold; 239db95818eSIan Rogers m->metric_unit = pm->unit; 2401725e9cdSIan Rogers m->pctx->sctx.user_requested_cpu_list = NULL; 2411725e9cdSIan Rogers if (user_requested_cpu_list) { 2421725e9cdSIan Rogers m->pctx->sctx.user_requested_cpu_list = strdup(user_requested_cpu_list); 2431725e9cdSIan Rogers if (!m->pctx->sctx.user_requested_cpu_list) 2441725e9cdSIan Rogers goto out_err; 2451725e9cdSIan Rogers } 2461a6abddeSIan Rogers m->pctx->sctx.runtime = runtime; 2471725e9cdSIan Rogers m->pctx->sctx.system_wide = system_wide; 248180a5013SIan Rogers m->group_events = !metric_no_group && metric__group_events(pm); 24946bdc0bfSIan Rogers m->metric_refs = NULL; 2505ecd5a0cSIan Rogers m->evlist = NULL; 2513d81d761SIan Rogers 2523d81d761SIan Rogers return m; 2531725e9cdSIan Rogers out_err: 2541725e9cdSIan Rogers metric__free(m); 2551725e9cdSIan Rogers return NULL; 2563d81d761SIan Rogers } 2573d81d761SIan Rogers 258ec5c5b3dSIan Rogers static bool contains_metric_id(struct evsel **metric_events, int num_events, 259ec5c5b3dSIan Rogers const char *metric_id) 260dcc81be0SIan Rogers { 261dcc81be0SIan Rogers int i; 262dcc81be0SIan Rogers 263dcc81be0SIan Rogers for (i = 0; i < num_events; i++) { 264ec5c5b3dSIan Rogers if (!strcmp(evsel__metric_id(metric_events[i]), metric_id)) 265dcc81be0SIan Rogers return true; 266dcc81be0SIan Rogers } 267dcc81be0SIan Rogers return false; 268dcc81be0SIan Rogers } 269dcc81be0SIan Rogers 2702440689dSIan Rogers /** 2715ecd5a0cSIan Rogers * setup_metric_events - Find a group of events in metric_evlist that correspond 2725ecd5a0cSIan Rogers * to the IDs from a parsed metric expression. 273bd3846d0SIan Rogers * @pmu: The PMU for the IDs. 2745ecd5a0cSIan Rogers * @ids: the metric IDs to match. 2755ecd5a0cSIan Rogers * @metric_evlist: the list of perf events. 2765ecd5a0cSIan Rogers * @out_metric_events: holds the created metric events array. 2772440689dSIan Rogers */ 278bd3846d0SIan Rogers static int setup_metric_events(const char *pmu, struct hashmap *ids, 2795ecd5a0cSIan Rogers struct evlist *metric_evlist, 28017b3867dSIan Rogers struct evsel ***out_metric_events) 281b18f3e36SAndi Kleen { 2825ecd5a0cSIan Rogers struct evsel **metric_events; 283ec5c5b3dSIan Rogers const char *metric_id; 2845ecd5a0cSIan Rogers struct evsel *ev; 2855ecd5a0cSIan Rogers size_t ids_size, matched_events, i; 28694f9eb95SIan Rogers bool all_pmus = !strcmp(pmu, "all") || perf_pmus__num_core_pmus() == 1 || !is_pmu_core(pmu); 2872440689dSIan Rogers 2885ecd5a0cSIan Rogers *out_metric_events = NULL; 2895ecd5a0cSIan Rogers ids_size = hashmap__size(ids); 290b18f3e36SAndi Kleen 2917bbe8f00SSun Haiyong metric_events = calloc(ids_size + 1, sizeof(void *)); 2925ecd5a0cSIan Rogers if (!metric_events) 2935ecd5a0cSIan Rogers return -ENOMEM; 2945ecd5a0cSIan Rogers 2952440689dSIan Rogers matched_events = 0; 2965ecd5a0cSIan Rogers evlist__for_each_entry(metric_evlist, ev) { 2975ecd5a0cSIan Rogers struct expr_id_data *val_ptr; 2985ecd5a0cSIan Rogers 2998a4859c5SIan Rogers /* Don't match events for the wrong hybrid PMU. */ 300ec6a4a8bSIan Rogers if (!all_pmus && ev->pmu_name && evsel__is_hybrid(ev) && 3018a4859c5SIan Rogers strcmp(ev->pmu_name, pmu)) 302bd3846d0SIan Rogers continue; 30305530a79SIan Rogers /* 304762a05c5SIan Rogers * Check for duplicate events with the same name. For 305762a05c5SIan Rogers * example, uncore_imc/cas_count_read/ will turn into 6 306762a05c5SIan Rogers * events per socket on skylakex. Only the first such 3075ecd5a0cSIan Rogers * event is placed in metric_events. 30805530a79SIan Rogers */ 309ec5c5b3dSIan Rogers metric_id = evsel__metric_id(ev); 310ec5c5b3dSIan Rogers if (contains_metric_id(metric_events, matched_events, metric_id)) 31105530a79SIan Rogers continue; 3125ecd5a0cSIan Rogers /* 3135ecd5a0cSIan Rogers * Does this event belong to the parse context? For 3145ecd5a0cSIan Rogers * combined or shared groups, this metric may not care 3155ecd5a0cSIan Rogers * about this event. 3165ecd5a0cSIan Rogers */ 317c302378bSEduard Zingerman if (hashmap__find(ids, metric_id, &val_ptr)) { 3188a4859c5SIan Rogers pr_debug("Matched metric-id %s to %s\n", metric_id, evsel__name(ev)); 3192440689dSIan Rogers metric_events[matched_events++] = ev; 320dcc81be0SIan Rogers 3215ecd5a0cSIan Rogers if (matched_events >= ids_size) 3222440689dSIan Rogers break; 3232440689dSIan Rogers } 324762a05c5SIan Rogers } 3255ecd5a0cSIan Rogers if (matched_events < ids_size) { 3265ecd5a0cSIan Rogers free(metric_events); 3275ecd5a0cSIan Rogers return -EINVAL; 328b18f3e36SAndi Kleen } 3295ecd5a0cSIan Rogers for (i = 0; i < ids_size; i++) { 33058fc90fdSKajol Jain ev = metric_events[i]; 3315ecd5a0cSIan Rogers ev->collect_stat = true; 3325ecd5a0cSIan Rogers 333dcc81be0SIan Rogers /* 3345ecd5a0cSIan Rogers * The metric leader points to the identically named 3355ecd5a0cSIan Rogers * event in metric_events. 336dcc81be0SIan Rogers */ 337dcc81be0SIan Rogers ev->metric_leader = ev; 338dcc81be0SIan Rogers /* 3395ecd5a0cSIan Rogers * Mark two events with identical names in the same 3405ecd5a0cSIan Rogers * group (or globally) as being in use as uncore events 3415ecd5a0cSIan Rogers * may be duplicated for each pmu. Set the metric leader 3425ecd5a0cSIan Rogers * of such events to be the event that appears in 3435ecd5a0cSIan Rogers * metric_events. 344dcc81be0SIan Rogers */ 345ec5c5b3dSIan Rogers metric_id = evsel__metric_id(ev); 3465ecd5a0cSIan Rogers evlist__for_each_entry_continue(metric_evlist, ev) { 347d3e2bb43SIan Rogers if (!strcmp(evsel__metric_id(ev), metric_id)) 348dcc81be0SIan Rogers ev->metric_leader = metric_events[i]; 349dcc81be0SIan Rogers } 350dcc81be0SIan Rogers } 3515ecd5a0cSIan Rogers *out_metric_events = metric_events; 3525ecd5a0cSIan Rogers return 0; 353b18f3e36SAndi Kleen } 354b18f3e36SAndi Kleen 355d4be39caSIan Rogers static bool match_metric(const char *metric_or_groups, const char *sought) 356b18f3e36SAndi Kleen { 357b18f3e36SAndi Kleen int len; 358b18f3e36SAndi Kleen char *m; 359b18f3e36SAndi Kleen 360d4be39caSIan Rogers if (!sought) 361b18f3e36SAndi Kleen return false; 362d4be39caSIan Rogers if (!strcmp(sought, "all")) 363b18f3e36SAndi Kleen return true; 364d4be39caSIan Rogers if (!metric_or_groups) 365d4be39caSIan Rogers return !strcasecmp(sought, "No_group"); 366d4be39caSIan Rogers len = strlen(sought); 367d4be39caSIan Rogers if (!strncasecmp(metric_or_groups, sought, len) && 368d4be39caSIan Rogers (metric_or_groups[len] == 0 || metric_or_groups[len] == ';')) 369b18f3e36SAndi Kleen return true; 370d4be39caSIan Rogers m = strchr(metric_or_groups, ';'); 371d4be39caSIan Rogers return m && match_metric(m + 1, sought); 372b18f3e36SAndi Kleen } 373b18f3e36SAndi Kleen 374bd3846d0SIan Rogers static bool match_pm_metric(const struct pmu_metric *pm, const char *pmu, const char *metric) 375be335ec2SJohn Garry { 376bd3846d0SIan Rogers const char *pm_pmu = pm->pmu ?: "cpu"; 377bd3846d0SIan Rogers 378bd3846d0SIan Rogers if (strcmp(pmu, "all") && strcmp(pm_pmu, pmu)) 379bd3846d0SIan Rogers return false; 380bd3846d0SIan Rogers 381db95818eSIan Rogers return match_metric(pm->metric_group, metric) || 382db95818eSIan Rogers match_metric(pm->metric_name, metric); 383be335ec2SJohn Garry } 384be335ec2SJohn Garry 385e5c6109fSIan Rogers /** struct mep - RB-tree node for building printing information. */ 38671b0acceSAndi Kleen struct mep { 387e5c6109fSIan Rogers /** nd - RB-tree element. */ 38871b0acceSAndi Kleen struct rb_node nd; 389e5c6109fSIan Rogers /** @metric_group: Owned metric group name, separated others with ';'. */ 390e5c6109fSIan Rogers char *metric_group; 391e5c6109fSIan Rogers const char *metric_name; 392e5c6109fSIan Rogers const char *metric_desc; 393e5c6109fSIan Rogers const char *metric_long_desc; 394e5c6109fSIan Rogers const char *metric_expr; 395c7551a2eSIan Rogers const char *metric_threshold; 396e5c6109fSIan Rogers const char *metric_unit; 39771b0acceSAndi Kleen }; 39871b0acceSAndi Kleen 39971b0acceSAndi Kleen static int mep_cmp(struct rb_node *rb_node, const void *entry) 40071b0acceSAndi Kleen { 40171b0acceSAndi Kleen struct mep *a = container_of(rb_node, struct mep, nd); 40271b0acceSAndi Kleen struct mep *b = (struct mep *)entry; 403e5c6109fSIan Rogers int ret; 40471b0acceSAndi Kleen 405e5c6109fSIan Rogers ret = strcmp(a->metric_group, b->metric_group); 406e5c6109fSIan Rogers if (ret) 407e5c6109fSIan Rogers return ret; 408e5c6109fSIan Rogers 409e5c6109fSIan Rogers return strcmp(a->metric_name, b->metric_name); 41071b0acceSAndi Kleen } 41171b0acceSAndi Kleen 412e5c6109fSIan Rogers static struct rb_node *mep_new(struct rblist *rl __maybe_unused, const void *entry) 41371b0acceSAndi Kleen { 41471b0acceSAndi Kleen struct mep *me = malloc(sizeof(struct mep)); 41571b0acceSAndi Kleen 41671b0acceSAndi Kleen if (!me) 41771b0acceSAndi Kleen return NULL; 41871b0acceSAndi Kleen 419e5c6109fSIan Rogers memcpy(me, entry, sizeof(struct mep)); 420e5c6109fSIan Rogers return &me->nd; 42171b0acceSAndi Kleen } 42271b0acceSAndi Kleen 42371b0acceSAndi Kleen static void mep_delete(struct rblist *rl __maybe_unused, 42471b0acceSAndi Kleen struct rb_node *nd) 42571b0acceSAndi Kleen { 42671b0acceSAndi Kleen struct mep *me = container_of(nd, struct mep, nd); 42771b0acceSAndi Kleen 428e5c6109fSIan Rogers zfree(&me->metric_group); 42971b0acceSAndi Kleen free(me); 43071b0acceSAndi Kleen } 43171b0acceSAndi Kleen 432e5c6109fSIan Rogers static struct mep *mep_lookup(struct rblist *groups, const char *metric_group, 433e5c6109fSIan Rogers const char *metric_name) 43471b0acceSAndi Kleen { 435e5c6109fSIan Rogers struct rb_node *nd; 436e5c6109fSIan Rogers struct mep me = { 437e5c6109fSIan Rogers .metric_group = strdup(metric_group), 438e5c6109fSIan Rogers .metric_name = metric_name, 439e5c6109fSIan Rogers }; 440e5c6109fSIan Rogers nd = rblist__find(groups, &me); 441e5c6109fSIan Rogers if (nd) { 442e5c6109fSIan Rogers free(me.metric_group); 443e5c6109fSIan Rogers return container_of(nd, struct mep, nd); 44471b0acceSAndi Kleen } 445e5c6109fSIan Rogers rblist__add_node(groups, &me); 446e5c6109fSIan Rogers nd = rblist__find(groups, &me); 447e5c6109fSIan Rogers if (nd) 448e5c6109fSIan Rogers return container_of(nd, struct mep, nd); 449e5c6109fSIan Rogers return NULL; 45071b0acceSAndi Kleen } 45171b0acceSAndi Kleen 452db95818eSIan Rogers static int metricgroup__add_to_mep_groups(const struct pmu_metric *pm, 453e5c6109fSIan Rogers struct rblist *groups) 454f6fe1e48SJohn Garry { 455f6fe1e48SJohn Garry const char *g; 456f6fe1e48SJohn Garry char *omg, *mg; 457f6fe1e48SJohn Garry 4584b5ee6dbSIan Rogers mg = strdup(pm->metric_group ?: pm->metric_name); 459f6fe1e48SJohn Garry if (!mg) 460f6fe1e48SJohn Garry return -ENOMEM; 461f6fe1e48SJohn Garry omg = mg; 462f6fe1e48SJohn Garry while ((g = strsep(&mg, ";")) != NULL) { 463f6fe1e48SJohn Garry struct mep *me; 464f6fe1e48SJohn Garry 465f6fe1e48SJohn Garry g = skip_spaces(g); 466e5c6109fSIan Rogers if (strlen(g)) 467db95818eSIan Rogers me = mep_lookup(groups, g, pm->metric_name); 468e5c6109fSIan Rogers else 4694b5ee6dbSIan Rogers me = mep_lookup(groups, pm->metric_name, pm->metric_name); 470f6fe1e48SJohn Garry 471e5c6109fSIan Rogers if (me) { 472db95818eSIan Rogers me->metric_desc = pm->desc; 473db95818eSIan Rogers me->metric_long_desc = pm->long_desc; 474db95818eSIan Rogers me->metric_expr = pm->metric_expr; 475c7551a2eSIan Rogers me->metric_threshold = pm->metric_threshold; 476db95818eSIan Rogers me->metric_unit = pm->unit; 477f6fe1e48SJohn Garry } 478f6fe1e48SJohn Garry } 479f6fe1e48SJohn Garry free(omg); 480f6fe1e48SJohn Garry 481f6fe1e48SJohn Garry return 0; 482f6fe1e48SJohn Garry } 483f6fe1e48SJohn Garry 484a36fadb1SJohn Garry struct metricgroup_iter_data { 485db95818eSIan Rogers pmu_metric_iter_fn fn; 486a36fadb1SJohn Garry void *data; 487a36fadb1SJohn Garry }; 488a36fadb1SJohn Garry 489db95818eSIan Rogers static int metricgroup__sys_event_iter(const struct pmu_metric *pm, 490f8ea2c15SIan Rogers const struct pmu_metrics_table *table, 49129be2fe0SIan Rogers void *data) 492a36fadb1SJohn Garry { 493a36fadb1SJohn Garry struct metricgroup_iter_data *d = data; 494a36fadb1SJohn Garry struct perf_pmu *pmu = NULL; 495a36fadb1SJohn Garry 496db95818eSIan Rogers if (!pm->metric_expr || !pm->compat) 497a36fadb1SJohn Garry return 0; 498a36fadb1SJohn Garry 4991eaf496eSIan Rogers while ((pmu = perf_pmus__scan(pmu))) { 500a36fadb1SJohn Garry 50154409997SJing Zhang if (!pmu->id || !pmu_uncore_identifier_match(pm->compat, pmu->id)) 502a36fadb1SJohn Garry continue; 503a36fadb1SJohn Garry 504db95818eSIan Rogers return d->fn(pm, table, d->data); 505a36fadb1SJohn Garry } 506a36fadb1SJohn Garry return 0; 507a36fadb1SJohn Garry } 508a36fadb1SJohn Garry 509db95818eSIan Rogers static int metricgroup__add_to_mep_groups_callback(const struct pmu_metric *pm, 510f8ea2c15SIan Rogers const struct pmu_metrics_table *table __maybe_unused, 511660842e4SIan Rogers void *vdata) 512660842e4SIan Rogers { 513e5c6109fSIan Rogers struct rblist *groups = vdata; 514660842e4SIan Rogers 515db95818eSIan Rogers return metricgroup__add_to_mep_groups(pm, groups); 516660842e4SIan Rogers } 517660842e4SIan Rogers 518e5c6109fSIan Rogers void metricgroup__print(const struct print_callbacks *print_cb, void *print_state) 51971b0acceSAndi Kleen { 52071b0acceSAndi Kleen struct rblist groups; 521f8ea2c15SIan Rogers const struct pmu_metrics_table *table; 522e5c6109fSIan Rogers struct rb_node *node, *next; 52371b0acceSAndi Kleen 52471b0acceSAndi Kleen rblist__init(&groups); 52571b0acceSAndi Kleen groups.node_new = mep_new; 52671b0acceSAndi Kleen groups.node_cmp = mep_cmp; 52771b0acceSAndi Kleen groups.node_delete = mep_delete; 528f8ea2c15SIan Rogers table = pmu_metrics_table__find(); 529660842e4SIan Rogers if (table) { 5304000519eSIan Rogers pmu_metrics_table__for_each_metric(table, 531e5c6109fSIan Rogers metricgroup__add_to_mep_groups_callback, 532e5c6109fSIan Rogers &groups); 5330e0ae874SJin Yao } 534a36fadb1SJohn Garry { 535a36fadb1SJohn Garry struct metricgroup_iter_data data = { 536e5c6109fSIan Rogers .fn = metricgroup__add_to_mep_groups_callback, 537e5c6109fSIan Rogers .data = &groups, 538a36fadb1SJohn Garry }; 539db95818eSIan Rogers pmu_for_each_sys_metric(metricgroup__sys_event_iter, &data); 540a36fadb1SJohn Garry } 541a36fadb1SJohn Garry 542ca227029SDavidlohr Bueso for (node = rb_first_cached(&groups.entries); node; node = next) { 54371b0acceSAndi Kleen struct mep *me = container_of(node, struct mep, nd); 54471b0acceSAndi Kleen 545e5c6109fSIan Rogers print_cb->print_metric(print_state, 546e5c6109fSIan Rogers me->metric_group, 547e5c6109fSIan Rogers me->metric_name, 548e5c6109fSIan Rogers me->metric_desc, 549e5c6109fSIan Rogers me->metric_long_desc, 550e5c6109fSIan Rogers me->metric_expr, 551c7551a2eSIan Rogers me->metric_threshold, 552e5c6109fSIan Rogers me->metric_unit); 55371b0acceSAndi Kleen next = rb_next(node); 55471b0acceSAndi Kleen rblist__remove_node(&groups, node); 55571b0acceSAndi Kleen } 55671b0acceSAndi Kleen } 55771b0acceSAndi Kleen 558ec5c5b3dSIan Rogers static const char *code_characters = ",-=@"; 559ec5c5b3dSIan Rogers 560ec5c5b3dSIan Rogers static int encode_metric_id(struct strbuf *sb, const char *x) 561ec5c5b3dSIan Rogers { 562ec5c5b3dSIan Rogers char *c; 563ec5c5b3dSIan Rogers int ret = 0; 564ec5c5b3dSIan Rogers 565ec5c5b3dSIan Rogers for (; *x; x++) { 566ec5c5b3dSIan Rogers c = strchr(code_characters, *x); 567ec5c5b3dSIan Rogers if (c) { 568ec5c5b3dSIan Rogers ret = strbuf_addch(sb, '!'); 569ec5c5b3dSIan Rogers if (ret) 570ec5c5b3dSIan Rogers break; 571ec5c5b3dSIan Rogers 572ec5c5b3dSIan Rogers ret = strbuf_addch(sb, '0' + (c - code_characters)); 573ec5c5b3dSIan Rogers if (ret) 574ec5c5b3dSIan Rogers break; 575ec5c5b3dSIan Rogers } else { 576ec5c5b3dSIan Rogers ret = strbuf_addch(sb, *x); 577ec5c5b3dSIan Rogers if (ret) 578ec5c5b3dSIan Rogers break; 579ec5c5b3dSIan Rogers } 580ec5c5b3dSIan Rogers } 581ec5c5b3dSIan Rogers return ret; 582ec5c5b3dSIan Rogers } 583ec5c5b3dSIan Rogers 584ec5c5b3dSIan Rogers static int decode_metric_id(struct strbuf *sb, const char *x) 585ec5c5b3dSIan Rogers { 586ec5c5b3dSIan Rogers const char *orig = x; 587ec5c5b3dSIan Rogers size_t i; 588ec5c5b3dSIan Rogers char c; 589ec5c5b3dSIan Rogers int ret; 590ec5c5b3dSIan Rogers 591ec5c5b3dSIan Rogers for (; *x; x++) { 592ec5c5b3dSIan Rogers c = *x; 593ec5c5b3dSIan Rogers if (*x == '!') { 594ec5c5b3dSIan Rogers x++; 595ec5c5b3dSIan Rogers i = *x - '0'; 596ec5c5b3dSIan Rogers if (i > strlen(code_characters)) { 597ec5c5b3dSIan Rogers pr_err("Bad metric-id encoding in: '%s'", orig); 598ec5c5b3dSIan Rogers return -1; 599ec5c5b3dSIan Rogers } 600ec5c5b3dSIan Rogers c = code_characters[i]; 601ec5c5b3dSIan Rogers } 602ec5c5b3dSIan Rogers ret = strbuf_addch(sb, c); 603ec5c5b3dSIan Rogers if (ret) 604ec5c5b3dSIan Rogers return ret; 605ec5c5b3dSIan Rogers } 606ec5c5b3dSIan Rogers return 0; 607ec5c5b3dSIan Rogers } 608ec5c5b3dSIan Rogers 609b85a4d61SIan Rogers static int decode_all_metric_ids(struct evlist *perf_evlist, const char *modifier) 610ec5c5b3dSIan Rogers { 611ec5c5b3dSIan Rogers struct evsel *ev; 612ec5c5b3dSIan Rogers struct strbuf sb = STRBUF_INIT; 613ec5c5b3dSIan Rogers char *cur; 614ec5c5b3dSIan Rogers int ret = 0; 615ec5c5b3dSIan Rogers 616ec5c5b3dSIan Rogers evlist__for_each_entry(perf_evlist, ev) { 617ec5c5b3dSIan Rogers if (!ev->metric_id) 618ec5c5b3dSIan Rogers continue; 619ec5c5b3dSIan Rogers 620ec5c5b3dSIan Rogers ret = strbuf_setlen(&sb, 0); 621ec5c5b3dSIan Rogers if (ret) 622ec5c5b3dSIan Rogers break; 623ec5c5b3dSIan Rogers 624ec5c5b3dSIan Rogers ret = decode_metric_id(&sb, ev->metric_id); 625ec5c5b3dSIan Rogers if (ret) 626ec5c5b3dSIan Rogers break; 627ec5c5b3dSIan Rogers 628ec5c5b3dSIan Rogers free((char *)ev->metric_id); 629ec5c5b3dSIan Rogers ev->metric_id = strdup(sb.buf); 630ec5c5b3dSIan Rogers if (!ev->metric_id) { 631ec5c5b3dSIan Rogers ret = -ENOMEM; 632ec5c5b3dSIan Rogers break; 633ec5c5b3dSIan Rogers } 634ec5c5b3dSIan Rogers /* 635ec5c5b3dSIan Rogers * If the name is just the parsed event, use the metric-id to 636ec5c5b3dSIan Rogers * give a more friendly display version. 637ec5c5b3dSIan Rogers */ 638ec5c5b3dSIan Rogers if (strstr(ev->name, "metric-id=")) { 639b85a4d61SIan Rogers bool has_slash = false; 640ec5c5b3dSIan Rogers 64111ff9bcdSArnaldo Carvalho de Melo zfree(&ev->name); 642b85a4d61SIan Rogers for (cur = strchr(sb.buf, '@') ; cur; cur = strchr(++cur, '@')) { 643b85a4d61SIan Rogers *cur = '/'; 644b85a4d61SIan Rogers has_slash = true; 645b85a4d61SIan Rogers } 646b85a4d61SIan Rogers 647b85a4d61SIan Rogers if (modifier) { 648b85a4d61SIan Rogers if (!has_slash && !strchr(sb.buf, ':')) { 649b85a4d61SIan Rogers ret = strbuf_addch(&sb, ':'); 650b85a4d61SIan Rogers if (ret) 651b85a4d61SIan Rogers break; 652b85a4d61SIan Rogers } 653b85a4d61SIan Rogers ret = strbuf_addstr(&sb, modifier); 654b85a4d61SIan Rogers if (ret) 655b85a4d61SIan Rogers break; 656b85a4d61SIan Rogers } 657ec5c5b3dSIan Rogers ev->name = strdup(sb.buf); 658ec5c5b3dSIan Rogers if (!ev->name) { 659ec5c5b3dSIan Rogers ret = -ENOMEM; 660ec5c5b3dSIan Rogers break; 661ec5c5b3dSIan Rogers } 662ec5c5b3dSIan Rogers } 663ec5c5b3dSIan Rogers } 664ec5c5b3dSIan Rogers strbuf_release(&sb); 665ec5c5b3dSIan Rogers return ret; 666ec5c5b3dSIan Rogers } 667ec5c5b3dSIan Rogers 668ec5c5b3dSIan Rogers static int metricgroup__build_event_string(struct strbuf *events, 669ec5c5b3dSIan Rogers const struct expr_parse_ctx *ctx, 670b85a4d61SIan Rogers const char *modifier, 671180a5013SIan Rogers bool group_events) 672f742634aSKan Liang { 673ded80bdaSIan Rogers struct hashmap_entry *cur; 6744e21c13aSIan Rogers size_t bkt; 6759aa09230SIan Rogers bool no_group = true, has_tool_events = false; 6769aa09230SIan Rogers bool tool_events[PERF_TOOL_MAX] = {false}; 677ec5c5b3dSIan Rogers int ret = 0; 678ec5c5b3dSIan Rogers 679ec5c5b3dSIan Rogers #define RETURN_IF_NON_ZERO(x) do { if (x) return x; } while (0) 680f742634aSKan Liang 681cb94a02eSIan Rogers hashmap__for_each_entry(ctx->ids, cur, bkt) { 682c302378bSEduard Zingerman const char *sep, *rsep, *id = cur->pkey; 6839aa09230SIan Rogers enum perf_tool_event ev; 684ec5c5b3dSIan Rogers 685ec5c5b3dSIan Rogers pr_debug("found event %s\n", id); 6869aa09230SIan Rogers 6879aa09230SIan Rogers /* Always move tool events outside of the group. */ 6889aa09230SIan Rogers ev = perf_tool_event__from_str(id); 6899aa09230SIan Rogers if (ev != PERF_TOOL_NONE) { 6909aa09230SIan Rogers has_tool_events = true; 6919aa09230SIan Rogers tool_events[ev] = true; 692f742634aSKan Liang continue; 693f742634aSKan Liang } 694ec5c5b3dSIan Rogers /* Separate events with commas and open the group if necessary. */ 695ec5c5b3dSIan Rogers if (no_group) { 696180a5013SIan Rogers if (group_events) { 697ec5c5b3dSIan Rogers ret = strbuf_addch(events, '{'); 698ec5c5b3dSIan Rogers RETURN_IF_NON_ZERO(ret); 699ec5c5b3dSIan Rogers } 700ec5c5b3dSIan Rogers 701f742634aSKan Liang no_group = false; 702ec5c5b3dSIan Rogers } else { 703ec5c5b3dSIan Rogers ret = strbuf_addch(events, ','); 704ec5c5b3dSIan Rogers RETURN_IF_NON_ZERO(ret); 705f742634aSKan Liang } 706ec5c5b3dSIan Rogers /* 707ec5c5b3dSIan Rogers * Encode the ID as an event string. Add a qualifier for 708ec5c5b3dSIan Rogers * metric_id that is the original name except with characters 709ec5c5b3dSIan Rogers * that parse-events can't parse replaced. For example, 710ec5c5b3dSIan Rogers * 'msr@tsc@' gets added as msr/tsc,metric-id=msr!3tsc!3/ 711ec5c5b3dSIan Rogers */ 712ec5c5b3dSIan Rogers sep = strchr(id, '@'); 713ec5c5b3dSIan Rogers if (sep != NULL) { 714ec5c5b3dSIan Rogers ret = strbuf_add(events, id, sep - id); 715ec5c5b3dSIan Rogers RETURN_IF_NON_ZERO(ret); 716ec5c5b3dSIan Rogers ret = strbuf_addch(events, '/'); 717ec5c5b3dSIan Rogers RETURN_IF_NON_ZERO(ret); 718ec5c5b3dSIan Rogers rsep = strrchr(sep, '@'); 719ec5c5b3dSIan Rogers ret = strbuf_add(events, sep + 1, rsep - sep - 1); 720ec5c5b3dSIan Rogers RETURN_IF_NON_ZERO(ret); 721ec5c5b3dSIan Rogers ret = strbuf_addstr(events, ",metric-id="); 722ec5c5b3dSIan Rogers RETURN_IF_NON_ZERO(ret); 723ec5c5b3dSIan Rogers sep = rsep; 724ec5c5b3dSIan Rogers } else { 725ec5c5b3dSIan Rogers sep = strchr(id, ':'); 726ec5c5b3dSIan Rogers if (sep != NULL) { 727ec5c5b3dSIan Rogers ret = strbuf_add(events, id, sep - id); 728ec5c5b3dSIan Rogers RETURN_IF_NON_ZERO(ret); 729ec5c5b3dSIan Rogers } else { 730ec5c5b3dSIan Rogers ret = strbuf_addstr(events, id); 731ec5c5b3dSIan Rogers RETURN_IF_NON_ZERO(ret); 732f742634aSKan Liang } 733ec5c5b3dSIan Rogers ret = strbuf_addstr(events, "/metric-id="); 734ec5c5b3dSIan Rogers RETURN_IF_NON_ZERO(ret); 735ec5c5b3dSIan Rogers } 736ec5c5b3dSIan Rogers ret = encode_metric_id(events, id); 737ec5c5b3dSIan Rogers RETURN_IF_NON_ZERO(ret); 738ec5c5b3dSIan Rogers ret = strbuf_addstr(events, "/"); 739ec5c5b3dSIan Rogers RETURN_IF_NON_ZERO(ret); 740f742634aSKan Liang 741ec5c5b3dSIan Rogers if (sep != NULL) { 742ec5c5b3dSIan Rogers ret = strbuf_addstr(events, sep + 1); 743ec5c5b3dSIan Rogers RETURN_IF_NON_ZERO(ret); 744e2ce1059SIan Rogers } 745b85a4d61SIan Rogers if (modifier) { 746b85a4d61SIan Rogers ret = strbuf_addstr(events, modifier); 747b85a4d61SIan Rogers RETURN_IF_NON_ZERO(ret); 748b85a4d61SIan Rogers } 749ab483d8bSKan Liang } 750180a5013SIan Rogers if (!no_group && group_events) { 75160344f1aSZhengjun Xing ret = strbuf_addf(events, "}:W"); 7529aa09230SIan Rogers RETURN_IF_NON_ZERO(ret); 7539aa09230SIan Rogers } 7549aa09230SIan Rogers if (has_tool_events) { 7559aa09230SIan Rogers int i; 7569aa09230SIan Rogers 7579aa09230SIan Rogers perf_tool_event__for_each_event(i) { 7589aa09230SIan Rogers if (tool_events[i]) { 7599aa09230SIan Rogers if (!no_group) { 7609aa09230SIan Rogers ret = strbuf_addch(events, ','); 7619aa09230SIan Rogers RETURN_IF_NON_ZERO(ret); 7629aa09230SIan Rogers } 7639aa09230SIan Rogers no_group = false; 7649aa09230SIan Rogers ret = strbuf_addstr(events, perf_tool_event__to_str(i)); 7659aa09230SIan Rogers RETURN_IF_NON_ZERO(ret); 7669aa09230SIan Rogers } 7679aa09230SIan Rogers } 7689aa09230SIan Rogers } 769ec5c5b3dSIan Rogers 770ec5c5b3dSIan Rogers return ret; 771ec5c5b3dSIan Rogers #undef RETURN_IF_NON_ZERO 772ec5c5b3dSIan Rogers } 773ab483d8bSKan Liang 774db95818eSIan Rogers int __weak arch_get_runtimeparam(const struct pmu_metric *pm __maybe_unused) 7751e1a873dSKajol Jain { 7761e1a873dSKajol Jain return 1; 7771e1a873dSKajol Jain } 7781e1a873dSKajol Jain 77980be6434SIan Rogers /* 78080be6434SIan Rogers * A singly linked list on the stack of the names of metrics being 78180be6434SIan Rogers * processed. Used to identify recursion. 78280be6434SIan Rogers */ 78380be6434SIan Rogers struct visited_metric { 78480be6434SIan Rogers const char *name; 78580be6434SIan Rogers const struct visited_metric *parent; 78680be6434SIan Rogers }; 78780be6434SIan Rogers 788be335ec2SJohn Garry struct metricgroup_add_iter_data { 789be335ec2SJohn Garry struct list_head *metric_list; 790bd3846d0SIan Rogers const char *pmu; 79168074811SIan Rogers const char *metric_name; 792b85a4d61SIan Rogers const char *modifier; 793be335ec2SJohn Garry int *ret; 794be335ec2SJohn Garry bool *has_match; 795be335ec2SJohn Garry bool metric_no_group; 7961fd09e29SIan Rogers bool metric_no_threshold; 7971725e9cdSIan Rogers const char *user_requested_cpu_list; 7981725e9cdSIan Rogers bool system_wide; 79980be6434SIan Rogers struct metric *root_metric; 80080be6434SIan Rogers const struct visited_metric *visited; 801f8ea2c15SIan Rogers const struct pmu_metrics_table *table; 802be335ec2SJohn Garry }; 803be335ec2SJohn Garry 804bd3846d0SIan Rogers static bool metricgroup__find_metric(const char *pmu, 805bd3846d0SIan Rogers const char *metric, 806f8ea2c15SIan Rogers const struct pmu_metrics_table *table, 807db95818eSIan Rogers struct pmu_metric *pm); 808d3abd7b8SIan Rogers 80980be6434SIan Rogers static int add_metric(struct list_head *metric_list, 810db95818eSIan Rogers const struct pmu_metric *pm, 811b85a4d61SIan Rogers const char *modifier, 81280be6434SIan Rogers bool metric_no_group, 8131fd09e29SIan Rogers bool metric_no_threshold, 8141725e9cdSIan Rogers const char *user_requested_cpu_list, 8151725e9cdSIan Rogers bool system_wide, 81680be6434SIan Rogers struct metric *root_metric, 81780be6434SIan Rogers const struct visited_metric *visited, 818f8ea2c15SIan Rogers const struct pmu_metrics_table *table); 81980be6434SIan Rogers 82080be6434SIan Rogers /** 82180be6434SIan Rogers * resolve_metric - Locate metrics within the root metric and recursively add 82280be6434SIan Rogers * references to them. 82380be6434SIan Rogers * @metric_list: The list the metric is added to. 824bd3846d0SIan Rogers * @pmu: The PMU name to resolve metrics on, or "all" for all PMUs. 825b85a4d61SIan Rogers * @modifier: if non-null event modifiers like "u". 82680be6434SIan Rogers * @metric_no_group: Should events written to events be grouped "{}" or 82780be6434SIan Rogers * global. Grouping is the default but due to multiplexing the 82880be6434SIan Rogers * user may override. 8291725e9cdSIan Rogers * @user_requested_cpu_list: Command line specified CPUs to record on. 8301725e9cdSIan Rogers * @system_wide: Are events for all processes recorded. 83180be6434SIan Rogers * @root_metric: Metrics may reference other metrics to form a tree. In this 83280be6434SIan Rogers * case the root_metric holds all the IDs and a list of referenced 83380be6434SIan Rogers * metrics. When adding a root this argument is NULL. 83480be6434SIan Rogers * @visited: A singly linked list of metric names being added that is used to 83580be6434SIan Rogers * detect recursion. 836eeac7730SIan Rogers * @table: The table that is searched for metrics, most commonly the table for the 83780be6434SIan Rogers * architecture perf is running upon. 83880be6434SIan Rogers */ 83980be6434SIan Rogers static int resolve_metric(struct list_head *metric_list, 840bd3846d0SIan Rogers const char *pmu, 841b85a4d61SIan Rogers const char *modifier, 84280be6434SIan Rogers bool metric_no_group, 8431fd09e29SIan Rogers bool metric_no_threshold, 8441725e9cdSIan Rogers const char *user_requested_cpu_list, 8451725e9cdSIan Rogers bool system_wide, 84680be6434SIan Rogers struct metric *root_metric, 84780be6434SIan Rogers const struct visited_metric *visited, 848f8ea2c15SIan Rogers const struct pmu_metrics_table *table) 84980be6434SIan Rogers { 85080be6434SIan Rogers struct hashmap_entry *cur; 85180be6434SIan Rogers size_t bkt; 85280be6434SIan Rogers struct to_resolve { 85380be6434SIan Rogers /* The metric to resolve. */ 854db95818eSIan Rogers struct pmu_metric pm; 85580be6434SIan Rogers /* 85680be6434SIan Rogers * The key in the IDs map, this may differ from in case, 857db95818eSIan Rogers * etc. from pm->metric_name. 85880be6434SIan Rogers */ 85980be6434SIan Rogers const char *key; 86080be6434SIan Rogers } *pending = NULL; 86180be6434SIan Rogers int i, ret = 0, pending_cnt = 0; 86280be6434SIan Rogers 86380be6434SIan Rogers /* 86480be6434SIan Rogers * Iterate all the parsed IDs and if there's a matching metric and it to 86580be6434SIan Rogers * the pending array. 86680be6434SIan Rogers */ 86780be6434SIan Rogers hashmap__for_each_entry(root_metric->pctx->ids, cur, bkt) { 868db95818eSIan Rogers struct pmu_metric pm; 86980be6434SIan Rogers 870bd3846d0SIan Rogers if (metricgroup__find_metric(pmu, cur->pkey, table, &pm)) { 87180be6434SIan Rogers pending = realloc(pending, 87280be6434SIan Rogers (pending_cnt + 1) * sizeof(struct to_resolve)); 87380be6434SIan Rogers if (!pending) 87480be6434SIan Rogers return -ENOMEM; 87580be6434SIan Rogers 876db95818eSIan Rogers memcpy(&pending[pending_cnt].pm, &pm, sizeof(pm)); 877c302378bSEduard Zingerman pending[pending_cnt].key = cur->pkey; 87880be6434SIan Rogers pending_cnt++; 87980be6434SIan Rogers } 88080be6434SIan Rogers } 88180be6434SIan Rogers 88280be6434SIan Rogers /* Remove the metric IDs from the context. */ 88380be6434SIan Rogers for (i = 0; i < pending_cnt; i++) 88480be6434SIan Rogers expr__del_id(root_metric->pctx, pending[i].key); 88580be6434SIan Rogers 88680be6434SIan Rogers /* 88780be6434SIan Rogers * Recursively add all the metrics, IDs are added to the root metric's 88880be6434SIan Rogers * context. 88980be6434SIan Rogers */ 89080be6434SIan Rogers for (i = 0; i < pending_cnt; i++) { 891db95818eSIan Rogers ret = add_metric(metric_list, &pending[i].pm, modifier, metric_no_group, 8921fd09e29SIan Rogers metric_no_threshold, user_requested_cpu_list, system_wide, 8931fd09e29SIan Rogers root_metric, visited, table); 89480be6434SIan Rogers if (ret) 89580be6434SIan Rogers break; 89680be6434SIan Rogers } 89780be6434SIan Rogers 89880be6434SIan Rogers free(pending); 89980be6434SIan Rogers return ret; 90080be6434SIan Rogers } 90180be6434SIan Rogers 90268074811SIan Rogers /** 90368074811SIan Rogers * __add_metric - Add a metric to metric_list. 90468074811SIan Rogers * @metric_list: The list the metric is added to. 905db95818eSIan Rogers * @pm: The pmu_metric containing the metric to be added. 906b85a4d61SIan Rogers * @modifier: if non-null event modifiers like "u". 90768074811SIan Rogers * @metric_no_group: Should events written to events be grouped "{}" or 90868074811SIan Rogers * global. Grouping is the default but due to multiplexing the 90968074811SIan Rogers * user may override. 9101fd09e29SIan Rogers * @metric_no_threshold: Should threshold expressions be ignored? 91168074811SIan Rogers * @runtime: A special argument for the parser only known at runtime. 9121725e9cdSIan Rogers * @user_requested_cpu_list: Command line specified CPUs to record on. 9131725e9cdSIan Rogers * @system_wide: Are events for all processes recorded. 91480be6434SIan Rogers * @root_metric: Metrics may reference other metrics to form a tree. In this 91580be6434SIan Rogers * case the root_metric holds all the IDs and a list of referenced 91680be6434SIan Rogers * metrics. When adding a root this argument is NULL. 91780be6434SIan Rogers * @visited: A singly linked list of metric names being added that is used to 91880be6434SIan Rogers * detect recursion. 919eeac7730SIan Rogers * @table: The table that is searched for metrics, most commonly the table for the 92080be6434SIan Rogers * architecture perf is running upon. 92168074811SIan Rogers */ 922119e521aSJiri Olsa static int __add_metric(struct list_head *metric_list, 923db95818eSIan Rogers const struct pmu_metric *pm, 924b85a4d61SIan Rogers const char *modifier, 92505530a79SIan Rogers bool metric_no_group, 9261fd09e29SIan Rogers bool metric_no_threshold, 92783de0b7dSJiri Olsa int runtime, 9281725e9cdSIan Rogers const char *user_requested_cpu_list, 9291725e9cdSIan Rogers bool system_wide, 93080be6434SIan Rogers struct metric *root_metric, 93180be6434SIan Rogers const struct visited_metric *visited, 932f8ea2c15SIan Rogers const struct pmu_metrics_table *table) 93347352abaSKajol Jain { 93480be6434SIan Rogers const struct visited_metric *vm; 93580be6434SIan Rogers int ret; 93680be6434SIan Rogers bool is_root = !root_metric; 937d0a3052fSIan Rogers const char *expr; 93880be6434SIan Rogers struct visited_metric visited_node = { 939db95818eSIan Rogers .name = pm->metric_name, 94080be6434SIan Rogers .parent = visited, 94180be6434SIan Rogers }; 94247352abaSKajol Jain 94380be6434SIan Rogers for (vm = visited; vm; vm = vm->parent) { 944db95818eSIan Rogers if (!strcmp(pm->metric_name, vm->name)) { 945db95818eSIan Rogers pr_err("failed: recursion detected for %s\n", pm->metric_name); 94680be6434SIan Rogers return -1; 94780be6434SIan Rogers } 94880be6434SIan Rogers } 94980be6434SIan Rogers 95080be6434SIan Rogers if (is_root) { 95183de0b7dSJiri Olsa /* 95280be6434SIan Rogers * This metric is the root of a tree and may reference other 95380be6434SIan Rogers * metrics that are added recursively. 95483de0b7dSJiri Olsa */ 955db95818eSIan Rogers root_metric = metric__new(pm, modifier, metric_no_group, runtime, 9561725e9cdSIan Rogers user_requested_cpu_list, system_wide); 95780be6434SIan Rogers if (!root_metric) 95847352abaSKajol Jain return -ENOMEM; 95947352abaSKajol Jain 96083de0b7dSJiri Olsa } else { 96146bdc0bfSIan Rogers int cnt = 0; 96246bdc0bfSIan Rogers 96383de0b7dSJiri Olsa /* 964a3de7690SIan Rogers * This metric was referenced in a metric higher in the 965a3de7690SIan Rogers * tree. Check if the same metric is already resolved in the 966a3de7690SIan Rogers * metric_refs list. 96783de0b7dSJiri Olsa */ 96846bdc0bfSIan Rogers if (root_metric->metric_refs) { 96946bdc0bfSIan Rogers for (; root_metric->metric_refs[cnt].metric_name; cnt++) { 970db95818eSIan Rogers if (!strcmp(pm->metric_name, 97146bdc0bfSIan Rogers root_metric->metric_refs[cnt].metric_name)) 972a3de7690SIan Rogers return 0; 973a3de7690SIan Rogers } 97446bdc0bfSIan Rogers } 975a3de7690SIan Rogers 97646bdc0bfSIan Rogers /* Create reference. Need space for the entry and the terminator. */ 97746bdc0bfSIan Rogers root_metric->metric_refs = realloc(root_metric->metric_refs, 97846bdc0bfSIan Rogers (cnt + 2) * sizeof(struct metric_ref)); 97946bdc0bfSIan Rogers if (!root_metric->metric_refs) 98083de0b7dSJiri Olsa return -ENOMEM; 98183de0b7dSJiri Olsa 98283de0b7dSJiri Olsa /* 98383de0b7dSJiri Olsa * Intentionally passing just const char pointers, 98483de0b7dSJiri Olsa * from 'pe' object, so they never go away. We don't 98583de0b7dSJiri Olsa * need to change them, so there's no need to create 98683de0b7dSJiri Olsa * our own copy. 98783de0b7dSJiri Olsa */ 988db95818eSIan Rogers root_metric->metric_refs[cnt].metric_name = pm->metric_name; 989db95818eSIan Rogers root_metric->metric_refs[cnt].metric_expr = pm->metric_expr; 99083de0b7dSJiri Olsa 99146bdc0bfSIan Rogers /* Null terminate array. */ 99246bdc0bfSIan Rogers root_metric->metric_refs[cnt+1].metric_name = NULL; 99346bdc0bfSIan Rogers root_metric->metric_refs[cnt+1].metric_expr = NULL; 99483de0b7dSJiri Olsa } 99583de0b7dSJiri Olsa 99683de0b7dSJiri Olsa /* 99783de0b7dSJiri Olsa * For both the parent and referenced metrics, we parse 99880be6434SIan Rogers * all the metric's IDs and add it to the root context. 99983de0b7dSJiri Olsa */ 1000d0a3052fSIan Rogers ret = 0; 1001d0a3052fSIan Rogers expr = pm->metric_expr; 1002d0a3052fSIan Rogers if (is_root && pm->metric_threshold) { 1003d0a3052fSIan Rogers /* 1004d0a3052fSIan Rogers * Threshold expressions are built off the actual metric. Switch 1005d0a3052fSIan Rogers * to use that in case of additional necessary events. Change 1006d0a3052fSIan Rogers * the visited node name to avoid this being flagged as 10071fd09e29SIan Rogers * recursion. If the threshold events are disabled, just use the 10081fd09e29SIan Rogers * metric's name as a reference. This allows metric threshold 10091fd09e29SIan Rogers * computation if there are sufficient events. 1010d0a3052fSIan Rogers */ 1011d0a3052fSIan Rogers assert(strstr(pm->metric_threshold, pm->metric_name)); 10121fd09e29SIan Rogers expr = metric_no_threshold ? pm->metric_name : pm->metric_threshold; 1013d0a3052fSIan Rogers visited_node.name = "__threshold__"; 1014d0a3052fSIan Rogers } 1015d0a3052fSIan Rogers if (expr__find_ids(expr, NULL, root_metric->pctx) < 0) { 101680be6434SIan Rogers /* Broken metric. */ 101780be6434SIan Rogers ret = -EINVAL; 1018d0a3052fSIan Rogers } 1019d0a3052fSIan Rogers if (!ret) { 102080be6434SIan Rogers /* Resolve referenced metrics. */ 1021bd3846d0SIan Rogers const char *pmu = pm->pmu ?: "cpu"; 1022bd3846d0SIan Rogers 1023bd3846d0SIan Rogers ret = resolve_metric(metric_list, pmu, modifier, metric_no_group, 10241fd09e29SIan Rogers metric_no_threshold, user_requested_cpu_list, 1025bd3846d0SIan Rogers system_wide, root_metric, &visited_node, 1026bd3846d0SIan Rogers table); 1027ded80bdaSIan Rogers } 102880be6434SIan Rogers if (ret) { 102980be6434SIan Rogers if (is_root) 103080be6434SIan Rogers metric__free(root_metric); 103183de0b7dSJiri Olsa 103280be6434SIan Rogers } else if (is_root) 103380be6434SIan Rogers list_add(&root_metric->nd, metric_list); 10346bf2102bSIan Rogers 103580be6434SIan Rogers return ret; 103647352abaSKajol Jain } 103747352abaSKajol Jain 1038660842e4SIan Rogers struct metricgroup__find_metric_data { 1039bd3846d0SIan Rogers const char *pmu; 1040660842e4SIan Rogers const char *metric; 1041db95818eSIan Rogers struct pmu_metric *pm; 1042660842e4SIan Rogers }; 1043ce391940SJiri Olsa 1044db95818eSIan Rogers static int metricgroup__find_metric_callback(const struct pmu_metric *pm, 1045f8ea2c15SIan Rogers const struct pmu_metrics_table *table __maybe_unused, 1046660842e4SIan Rogers void *vdata) 1047660842e4SIan Rogers { 1048660842e4SIan Rogers struct metricgroup__find_metric_data *data = vdata; 1049bd3846d0SIan Rogers const char *pm_pmu = pm->pmu ?: "cpu"; 1050bd3846d0SIan Rogers 1051bd3846d0SIan Rogers if (strcmp(data->pmu, "all") && strcmp(pm_pmu, data->pmu)) 1052bd3846d0SIan Rogers return 0; 1053660842e4SIan Rogers 1054db95818eSIan Rogers if (!match_metric(pm->metric_name, data->metric)) 1055660842e4SIan Rogers return 0; 1056660842e4SIan Rogers 1057db95818eSIan Rogers memcpy(data->pm, pm, sizeof(*pm)); 1058d3abd7b8SIan Rogers return 1; 1059660842e4SIan Rogers } 1060ce391940SJiri Olsa 1061bd3846d0SIan Rogers static bool metricgroup__find_metric(const char *pmu, 1062bd3846d0SIan Rogers const char *metric, 1063f8ea2c15SIan Rogers const struct pmu_metrics_table *table, 1064db95818eSIan Rogers struct pmu_metric *pm) 106583de0b7dSJiri Olsa { 1066660842e4SIan Rogers struct metricgroup__find_metric_data data = { 1067bd3846d0SIan Rogers .pmu = pmu, 1068660842e4SIan Rogers .metric = metric, 1069db95818eSIan Rogers .pm = pm, 1070660842e4SIan Rogers }; 107183de0b7dSJiri Olsa 10724000519eSIan Rogers return pmu_metrics_table__for_each_metric(table, metricgroup__find_metric_callback, &data) 1073d3abd7b8SIan Rogers ? true : false; 107483de0b7dSJiri Olsa } 107583de0b7dSJiri Olsa 1076119e521aSJiri Olsa static int add_metric(struct list_head *metric_list, 1077db95818eSIan Rogers const struct pmu_metric *pm, 1078b85a4d61SIan Rogers const char *modifier, 107983de0b7dSJiri Olsa bool metric_no_group, 10801fd09e29SIan Rogers bool metric_no_threshold, 10811725e9cdSIan Rogers const char *user_requested_cpu_list, 10821725e9cdSIan Rogers bool system_wide, 108380be6434SIan Rogers struct metric *root_metric, 108480be6434SIan Rogers const struct visited_metric *visited, 1085f8ea2c15SIan Rogers const struct pmu_metrics_table *table) 1086a29c164aSJiri Olsa { 1087a29c164aSJiri Olsa int ret = 0; 1088a29c164aSJiri Olsa 1089db95818eSIan Rogers pr_debug("metric expr %s for %s\n", pm->metric_expr, pm->metric_name); 1090a29c164aSJiri Olsa 1091db95818eSIan Rogers if (!strstr(pm->metric_expr, "?")) { 10921fd09e29SIan Rogers ret = __add_metric(metric_list, pm, modifier, metric_no_group, 10931fd09e29SIan Rogers metric_no_threshold, 0, user_requested_cpu_list, 10941fd09e29SIan Rogers system_wide, root_metric, visited, table); 1095a29c164aSJiri Olsa } else { 1096a29c164aSJiri Olsa int j, count; 1097a29c164aSJiri Olsa 1098db95818eSIan Rogers count = arch_get_runtimeparam(pm); 1099a29c164aSJiri Olsa 1100a29c164aSJiri Olsa /* This loop is added to create multiple 1101a29c164aSJiri Olsa * events depend on count value and add 1102119e521aSJiri Olsa * those events to metric_list. 1103a29c164aSJiri Olsa */ 1104a29c164aSJiri Olsa 110580be6434SIan Rogers for (j = 0; j < count && !ret; j++) 11061fd09e29SIan Rogers ret = __add_metric(metric_list, pm, modifier, metric_no_group, 11071fd09e29SIan Rogers metric_no_threshold, j, user_requested_cpu_list, 11081fd09e29SIan Rogers system_wide, root_metric, visited, table); 1109a29c164aSJiri Olsa } 1110a29c164aSJiri Olsa 1111a29c164aSJiri Olsa return ret; 1112a29c164aSJiri Olsa } 1113a29c164aSJiri Olsa 1114db95818eSIan Rogers static int metricgroup__add_metric_sys_event_iter(const struct pmu_metric *pm, 1115f8ea2c15SIan Rogers const struct pmu_metrics_table *table __maybe_unused, 1116be335ec2SJohn Garry void *data) 1117be335ec2SJohn Garry { 1118be335ec2SJohn Garry struct metricgroup_add_iter_data *d = data; 1119be335ec2SJohn Garry int ret; 1120be335ec2SJohn Garry 1121bd3846d0SIan Rogers if (!match_pm_metric(pm, d->pmu, d->metric_name)) 1122be335ec2SJohn Garry return 0; 1123be335ec2SJohn Garry 1124db95818eSIan Rogers ret = add_metric(d->metric_list, pm, d->modifier, d->metric_no_group, 11251fd09e29SIan Rogers d->metric_no_threshold, d->user_requested_cpu_list, 11261fd09e29SIan Rogers d->system_wide, d->root_metric, d->visited, d->table); 1127be335ec2SJohn Garry if (ret) 1128fe7a98b9SJohn Garry goto out; 1129be335ec2SJohn Garry 1130be335ec2SJohn Garry *(d->has_match) = true; 1131be335ec2SJohn Garry 1132fe7a98b9SJohn Garry out: 1133fe7a98b9SJohn Garry *(d->ret) = ret; 1134fe7a98b9SJohn Garry return ret; 1135be335ec2SJohn Garry } 1136be335ec2SJohn Garry 11376b6b16b3SIan Rogers /** 11386b6b16b3SIan Rogers * metric_list_cmp - list_sort comparator that sorts metrics with more events to 11399aa09230SIan Rogers * the front. tool events are excluded from the count. 11406b6b16b3SIan Rogers */ 114180be6434SIan Rogers static int metric_list_cmp(void *priv __maybe_unused, const struct list_head *l, 114280be6434SIan Rogers const struct list_head *r) 114380be6434SIan Rogers { 114480be6434SIan Rogers const struct metric *left = container_of(l, struct metric, nd); 114580be6434SIan Rogers const struct metric *right = container_of(r, struct metric, nd); 11466b6b16b3SIan Rogers struct expr_id_data *data; 11479aa09230SIan Rogers int i, left_count, right_count; 114880be6434SIan Rogers 11496b6b16b3SIan Rogers left_count = hashmap__size(left->pctx->ids); 11509aa09230SIan Rogers perf_tool_event__for_each_event(i) { 11519aa09230SIan Rogers if (!expr__get_id(left->pctx, perf_tool_event__to_str(i), &data)) 11526b6b16b3SIan Rogers left_count--; 11539aa09230SIan Rogers } 11546b6b16b3SIan Rogers 11556b6b16b3SIan Rogers right_count = hashmap__size(right->pctx->ids); 11569aa09230SIan Rogers perf_tool_event__for_each_event(i) { 11579aa09230SIan Rogers if (!expr__get_id(right->pctx, perf_tool_event__to_str(i), &data)) 11586b6b16b3SIan Rogers right_count--; 11599aa09230SIan Rogers } 11606b6b16b3SIan Rogers 11616b6b16b3SIan Rogers return right_count - left_count; 116280be6434SIan Rogers } 116380be6434SIan Rogers 11641c0e4795SKan Liang /** 11651c0e4795SKan Liang * default_metricgroup_cmp - Implements complex key for the Default metricgroup 11661c0e4795SKan Liang * that first sorts by default_metricgroup_name, then 11671c0e4795SKan Liang * metric_name. 11681c0e4795SKan Liang */ 11691c0e4795SKan Liang static int default_metricgroup_cmp(void *priv __maybe_unused, 11701c0e4795SKan Liang const struct list_head *l, 11711c0e4795SKan Liang const struct list_head *r) 11721c0e4795SKan Liang { 11731c0e4795SKan Liang const struct metric *left = container_of(l, struct metric, nd); 11741c0e4795SKan Liang const struct metric *right = container_of(r, struct metric, nd); 11751c0e4795SKan Liang int diff = strcmp(right->default_metricgroup_name, left->default_metricgroup_name); 11761c0e4795SKan Liang 11771c0e4795SKan Liang if (diff) 11781c0e4795SKan Liang return diff; 11791c0e4795SKan Liang 11801c0e4795SKan Liang return strcmp(right->metric_name, left->metric_name); 11811c0e4795SKan Liang } 11821c0e4795SKan Liang 1183660842e4SIan Rogers struct metricgroup__add_metric_data { 1184660842e4SIan Rogers struct list_head *list; 1185bd3846d0SIan Rogers const char *pmu; 1186660842e4SIan Rogers const char *metric_name; 1187660842e4SIan Rogers const char *modifier; 11881725e9cdSIan Rogers const char *user_requested_cpu_list; 1189660842e4SIan Rogers bool metric_no_group; 11901fd09e29SIan Rogers bool metric_no_threshold; 11911725e9cdSIan Rogers bool system_wide; 1192660842e4SIan Rogers bool has_match; 1193660842e4SIan Rogers }; 1194660842e4SIan Rogers 1195db95818eSIan Rogers static int metricgroup__add_metric_callback(const struct pmu_metric *pm, 1196f8ea2c15SIan Rogers const struct pmu_metrics_table *table, 1197660842e4SIan Rogers void *vdata) 1198660842e4SIan Rogers { 1199660842e4SIan Rogers struct metricgroup__add_metric_data *data = vdata; 1200660842e4SIan Rogers int ret = 0; 1201660842e4SIan Rogers 1202bd3846d0SIan Rogers if (pm->metric_expr && match_pm_metric(pm, data->pmu, data->metric_name)) { 1203ccc66c60SIan Rogers bool metric_no_group = data->metric_no_group || 1204e4c4e8a5SKan Liang match_metric(pm->metricgroup_no_group, data->metric_name); 1205660842e4SIan Rogers 1206660842e4SIan Rogers data->has_match = true; 1207ccc66c60SIan Rogers ret = add_metric(data->list, pm, data->modifier, metric_no_group, 12081fd09e29SIan Rogers data->metric_no_threshold, data->user_requested_cpu_list, 12091fd09e29SIan Rogers data->system_wide, /*root_metric=*/NULL, 12101fd09e29SIan Rogers /*visited_metrics=*/NULL, table); 1211660842e4SIan Rogers } 1212660842e4SIan Rogers return ret; 1213660842e4SIan Rogers } 1214660842e4SIan Rogers 121568074811SIan Rogers /** 121668074811SIan Rogers * metricgroup__add_metric - Find and add a metric, or a metric group. 1217bd3846d0SIan Rogers * @pmu: The PMU name to search for metrics on, or "all" for all PMUs. 121868074811SIan Rogers * @metric_name: The name of the metric or metric group. For example, "IPC" 121968074811SIan Rogers * could be the name of a metric and "TopDownL1" the name of a 122068074811SIan Rogers * metric group. 1221b85a4d61SIan Rogers * @modifier: if non-null event modifiers like "u". 122268074811SIan Rogers * @metric_no_group: Should events written to events be grouped "{}" or 122368074811SIan Rogers * global. Grouping is the default but due to multiplexing the 122468074811SIan Rogers * user may override. 12251725e9cdSIan Rogers * @user_requested_cpu_list: Command line specified CPUs to record on. 12261725e9cdSIan Rogers * @system_wide: Are events for all processes recorded. 122768074811SIan Rogers * @metric_list: The list that the metric or metric group are added to. 1228eeac7730SIan Rogers * @table: The table that is searched for metrics, most commonly the table for the 122968074811SIan Rogers * architecture perf is running upon. 123068074811SIan Rogers */ 1231bd3846d0SIan Rogers static int metricgroup__add_metric(const char *pmu, const char *metric_name, const char *modifier, 12321fd09e29SIan Rogers bool metric_no_group, bool metric_no_threshold, 12331725e9cdSIan Rogers const char *user_requested_cpu_list, 12341725e9cdSIan Rogers bool system_wide, 1235119e521aSJiri Olsa struct list_head *metric_list, 1236f8ea2c15SIan Rogers const struct pmu_metrics_table *table) 1237b18f3e36SAndi Kleen { 123898461d9dSJiri Olsa LIST_HEAD(list); 1239660842e4SIan Rogers int ret; 124090810399SIan Rogers bool has_match = false; 1241b18f3e36SAndi Kleen 1242660842e4SIan Rogers { 1243660842e4SIan Rogers struct metricgroup__add_metric_data data = { 1244660842e4SIan Rogers .list = &list, 1245bd3846d0SIan Rogers .pmu = pmu, 1246660842e4SIan Rogers .metric_name = metric_name, 1247660842e4SIan Rogers .modifier = modifier, 1248660842e4SIan Rogers .metric_no_group = metric_no_group, 12491fd09e29SIan Rogers .metric_no_threshold = metric_no_threshold, 12501725e9cdSIan Rogers .user_requested_cpu_list = user_requested_cpu_list, 12511725e9cdSIan Rogers .system_wide = system_wide, 1252660842e4SIan Rogers .has_match = false, 1253660842e4SIan Rogers }; 125468074811SIan Rogers /* 1255660842e4SIan Rogers * Iterate over all metrics seeing if metric matches either the 1256660842e4SIan Rogers * name or group. When it does add the metric to the list. 125768074811SIan Rogers */ 12584000519eSIan Rogers ret = pmu_metrics_table__for_each_metric(table, metricgroup__add_metric_callback, 1259660842e4SIan Rogers &data); 126090810399SIan Rogers if (ret) 126127adafcdSNamhyung Kim goto out; 1262ce391940SJiri Olsa 1263660842e4SIan Rogers has_match = data.has_match; 1264660842e4SIan Rogers } 1265be335ec2SJohn Garry { 1266be335ec2SJohn Garry struct metricgroup_iter_data data = { 1267be335ec2SJohn Garry .fn = metricgroup__add_metric_sys_event_iter, 1268be335ec2SJohn Garry .data = (void *) &(struct metricgroup_add_iter_data) { 1269be335ec2SJohn Garry .metric_list = &list, 1270bd3846d0SIan Rogers .pmu = pmu, 127168074811SIan Rogers .metric_name = metric_name, 1272b85a4d61SIan Rogers .modifier = modifier, 1273be335ec2SJohn Garry .metric_no_group = metric_no_group, 12741725e9cdSIan Rogers .user_requested_cpu_list = user_requested_cpu_list, 12751725e9cdSIan Rogers .system_wide = system_wide, 1276be335ec2SJohn Garry .has_match = &has_match, 1277be335ec2SJohn Garry .ret = &ret, 1278eeac7730SIan Rogers .table = table, 1279be335ec2SJohn Garry }, 1280be335ec2SJohn Garry }; 1281be335ec2SJohn Garry 1282db95818eSIan Rogers pmu_for_each_sys_metric(metricgroup__sys_event_iter, &data); 1283be335ec2SJohn Garry } 1284ce391940SJiri Olsa /* End of pmu events. */ 12855ecd5a0cSIan Rogers if (!has_match) 128627adafcdSNamhyung Kim ret = -EINVAL; 128798461d9dSJiri Olsa 128827adafcdSNamhyung Kim out: 128927adafcdSNamhyung Kim /* 129027adafcdSNamhyung Kim * add to metric_list so that they can be released 129127adafcdSNamhyung Kim * even if it's failed 129227adafcdSNamhyung Kim */ 1293119e521aSJiri Olsa list_splice(&list, metric_list); 129427adafcdSNamhyung Kim return ret; 129590810399SIan Rogers } 1296b18f3e36SAndi Kleen 129768074811SIan Rogers /** 129868074811SIan Rogers * metricgroup__add_metric_list - Find and add metrics, or metric groups, 129968074811SIan Rogers * specified in a list. 1300bd3846d0SIan Rogers * @pmu: A pmu to restrict the metrics to, or "all" for all PMUS. 130168074811SIan Rogers * @list: the list of metrics or metric groups. For example, "IPC,CPI,TopDownL1" 130268074811SIan Rogers * would match the IPC and CPI metrics, and TopDownL1 would match all 130368074811SIan Rogers * the metrics in the TopDownL1 group. 130468074811SIan Rogers * @metric_no_group: Should events written to events be grouped "{}" or 130568074811SIan Rogers * global. Grouping is the default but due to multiplexing the 130668074811SIan Rogers * user may override. 13071725e9cdSIan Rogers * @user_requested_cpu_list: Command line specified CPUs to record on. 13081725e9cdSIan Rogers * @system_wide: Are events for all processes recorded. 130968074811SIan Rogers * @metric_list: The list that metrics are added to. 1310eeac7730SIan Rogers * @table: The table that is searched for metrics, most commonly the table for the 131168074811SIan Rogers * architecture perf is running upon. 131268074811SIan Rogers */ 1313bd3846d0SIan Rogers static int metricgroup__add_metric_list(const char *pmu, const char *list, 1314bd3846d0SIan Rogers bool metric_no_group, 13151fd09e29SIan Rogers bool metric_no_threshold, 13161725e9cdSIan Rogers const char *user_requested_cpu_list, 13171725e9cdSIan Rogers bool system_wide, struct list_head *metric_list, 1318f8ea2c15SIan Rogers const struct pmu_metrics_table *table) 1319b18f3e36SAndi Kleen { 1320b85a4d61SIan Rogers char *list_itr, *list_copy, *metric_name, *modifier; 1321ec5c5b3dSIan Rogers int ret, count = 0; 1322b18f3e36SAndi Kleen 1323b85a4d61SIan Rogers list_copy = strdup(list); 1324b85a4d61SIan Rogers if (!list_copy) 1325b18f3e36SAndi Kleen return -ENOMEM; 1326b85a4d61SIan Rogers list_itr = list_copy; 1327411bc316SAndi Kleen 1328b85a4d61SIan Rogers while ((metric_name = strsep(&list_itr, ",")) != NULL) { 1329b85a4d61SIan Rogers modifier = strchr(metric_name, ':'); 1330b85a4d61SIan Rogers if (modifier) 1331b85a4d61SIan Rogers *modifier++ = '\0'; 1332b85a4d61SIan Rogers 1333bd3846d0SIan Rogers ret = metricgroup__add_metric(pmu, metric_name, modifier, 13341fd09e29SIan Rogers metric_no_group, metric_no_threshold, 13351fd09e29SIan Rogers user_requested_cpu_list, 13361725e9cdSIan Rogers system_wide, metric_list, table); 1337ec5c5b3dSIan Rogers if (ret == -EINVAL) 1338b85a4d61SIan Rogers pr_err("Cannot find metric or group `%s'\n", metric_name); 1339ec5c5b3dSIan Rogers 1340ec5c5b3dSIan Rogers if (ret) 1341b18f3e36SAndi Kleen break; 1342ec5c5b3dSIan Rogers 1343ec5c5b3dSIan Rogers count++; 1344b18f3e36SAndi Kleen } 1345b85a4d61SIan Rogers free(list_copy); 1346ab483d8bSKan Liang 1347ec5c5b3dSIan Rogers if (!ret) { 1348ec5c5b3dSIan Rogers /* 1349ec5c5b3dSIan Rogers * Warn about nmi_watchdog if any parsed metrics had the 1350ec5c5b3dSIan Rogers * NO_NMI_WATCHDOG constraint. 1351ec5c5b3dSIan Rogers */ 1352180a5013SIan Rogers metric__watchdog_constraint_hint(NULL, /*foot=*/true); 1353ec5c5b3dSIan Rogers /* No metrics. */ 1354ec5c5b3dSIan Rogers if (count == 0) 1355ec5c5b3dSIan Rogers return -EINVAL; 1356ec5c5b3dSIan Rogers } 1357b18f3e36SAndi Kleen return ret; 1358b18f3e36SAndi Kleen } 1359b18f3e36SAndi Kleen 1360119e521aSJiri Olsa static void metricgroup__free_metrics(struct list_head *metric_list) 1361b18f3e36SAndi Kleen { 1362a0c05b36SJiri Olsa struct metric *m, *tmp; 1363b18f3e36SAndi Kleen 1364119e521aSJiri Olsa list_for_each_entry_safe (m, tmp, metric_list, nd) { 1365a0c05b36SJiri Olsa list_del_init(&m->nd); 13663d81d761SIan Rogers metric__free(m); 1367b18f3e36SAndi Kleen } 1368b18f3e36SAndi Kleen } 1369b18f3e36SAndi Kleen 13705ecd5a0cSIan Rogers /** 13718586d274SIan Rogers * find_tool_events - Search for the pressence of tool events in metric_list. 13728586d274SIan Rogers * @metric_list: List to take metrics from. 13738586d274SIan Rogers * @tool_events: Array of false values, indices corresponding to tool events set 13748586d274SIan Rogers * to true if tool event is found. 13758586d274SIan Rogers */ 13768586d274SIan Rogers static void find_tool_events(const struct list_head *metric_list, 13778586d274SIan Rogers bool tool_events[PERF_TOOL_MAX]) 13788586d274SIan Rogers { 13798586d274SIan Rogers struct metric *m; 13808586d274SIan Rogers 13818586d274SIan Rogers list_for_each_entry(m, metric_list, nd) { 13828586d274SIan Rogers int i; 13838586d274SIan Rogers 13848586d274SIan Rogers perf_tool_event__for_each_event(i) { 13858586d274SIan Rogers struct expr_id_data *data; 13868586d274SIan Rogers 13878586d274SIan Rogers if (!tool_events[i] && 13888586d274SIan Rogers !expr__get_id(m->pctx, perf_tool_event__to_str(i), &data)) 13898586d274SIan Rogers tool_events[i] = true; 13908586d274SIan Rogers } 13918586d274SIan Rogers } 13928586d274SIan Rogers } 13938586d274SIan Rogers 13948586d274SIan Rogers /** 1395180a5013SIan Rogers * build_combined_expr_ctx - Make an expr_parse_ctx with all !group_events 13965ecd5a0cSIan Rogers * metric IDs, as the IDs are held in a set, 13975ecd5a0cSIan Rogers * duplicates will be removed. 13985ecd5a0cSIan Rogers * @metric_list: List to take metrics from. 13995ecd5a0cSIan Rogers * @combined: Out argument for result. 14005ecd5a0cSIan Rogers */ 14015ecd5a0cSIan Rogers static int build_combined_expr_ctx(const struct list_head *metric_list, 14025ecd5a0cSIan Rogers struct expr_parse_ctx **combined) 14035ecd5a0cSIan Rogers { 14045ecd5a0cSIan Rogers struct hashmap_entry *cur; 14055ecd5a0cSIan Rogers size_t bkt; 14065ecd5a0cSIan Rogers struct metric *m; 14075ecd5a0cSIan Rogers char *dup; 14085ecd5a0cSIan Rogers int ret; 14095ecd5a0cSIan Rogers 14105ecd5a0cSIan Rogers *combined = expr__ctx_new(); 14115ecd5a0cSIan Rogers if (!*combined) 14125ecd5a0cSIan Rogers return -ENOMEM; 14135ecd5a0cSIan Rogers 14145ecd5a0cSIan Rogers list_for_each_entry(m, metric_list, nd) { 1415180a5013SIan Rogers if (!m->group_events && !m->modifier) { 14165ecd5a0cSIan Rogers hashmap__for_each_entry(m->pctx->ids, cur, bkt) { 1417c302378bSEduard Zingerman dup = strdup(cur->pkey); 14185ecd5a0cSIan Rogers if (!dup) { 14195ecd5a0cSIan Rogers ret = -ENOMEM; 14205ecd5a0cSIan Rogers goto err_out; 14215ecd5a0cSIan Rogers } 14225ecd5a0cSIan Rogers ret = expr__add_id(*combined, dup); 14235ecd5a0cSIan Rogers if (ret) 14245ecd5a0cSIan Rogers goto err_out; 14255ecd5a0cSIan Rogers } 14265ecd5a0cSIan Rogers } 14275ecd5a0cSIan Rogers } 14285ecd5a0cSIan Rogers return 0; 14295ecd5a0cSIan Rogers err_out: 14305ecd5a0cSIan Rogers expr__ctx_free(*combined); 14315ecd5a0cSIan Rogers *combined = NULL; 14325ecd5a0cSIan Rogers return ret; 14335ecd5a0cSIan Rogers } 14345ecd5a0cSIan Rogers 14355ecd5a0cSIan Rogers /** 14365ecd5a0cSIan Rogers * parse_ids - Build the event string for the ids and parse them creating an 14375ecd5a0cSIan Rogers * evlist. The encoded metric_ids are decoded. 14386b6b16b3SIan Rogers * @metric_no_merge: is metric sharing explicitly disabled. 14395ecd5a0cSIan Rogers * @fake_pmu: used when testing metrics not supported by the current CPU. 14405ecd5a0cSIan Rogers * @ids: the event identifiers parsed from a metric. 1441b85a4d61SIan Rogers * @modifier: any modifiers added to the events. 1442180a5013SIan Rogers * @group_events: should events be placed in a weak group. 14438586d274SIan Rogers * @tool_events: entries set true if the tool event of index could be present in 14448586d274SIan Rogers * the overall list of metrics. 14455ecd5a0cSIan Rogers * @out_evlist: the created list of events. 14465ecd5a0cSIan Rogers */ 14476b6b16b3SIan Rogers static int parse_ids(bool metric_no_merge, struct perf_pmu *fake_pmu, 14486b6b16b3SIan Rogers struct expr_parse_ctx *ids, const char *modifier, 1449180a5013SIan Rogers bool group_events, const bool tool_events[PERF_TOOL_MAX], 14508586d274SIan Rogers struct evlist **out_evlist) 14515ecd5a0cSIan Rogers { 14525ecd5a0cSIan Rogers struct parse_events_error parse_error; 14535ecd5a0cSIan Rogers struct evlist *parsed_evlist; 14545ecd5a0cSIan Rogers struct strbuf events = STRBUF_INIT; 14555ecd5a0cSIan Rogers int ret; 14565ecd5a0cSIan Rogers 14575ecd5a0cSIan Rogers *out_evlist = NULL; 14586b6b16b3SIan Rogers if (!metric_no_merge || hashmap__size(ids->ids) == 0) { 1459c788ef61SIan Rogers bool added_event = false; 14609aa09230SIan Rogers int i; 14615ecd5a0cSIan Rogers /* 14629aa09230SIan Rogers * We may fail to share events between metrics because a tool 14639aa09230SIan Rogers * event isn't present in one metric. For example, a ratio of 14649aa09230SIan Rogers * cache misses doesn't need duration_time but the same events 14659aa09230SIan Rogers * may be used for a misses per second. Events without sharing 14669aa09230SIan Rogers * implies multiplexing, that is best avoided, so place 14679aa09230SIan Rogers * all tool events in every group. 14686b6b16b3SIan Rogers * 14696b6b16b3SIan Rogers * Also, there may be no ids/events in the expression parsing 14706b6b16b3SIan Rogers * context because of constant evaluation, e.g.: 14715ecd5a0cSIan Rogers * event1 if #smt_on else 0 14729aa09230SIan Rogers * Add a tool event to avoid a parse error on an empty string. 14735ecd5a0cSIan Rogers */ 14749aa09230SIan Rogers perf_tool_event__for_each_event(i) { 14758586d274SIan Rogers if (tool_events[i]) { 14769aa09230SIan Rogers char *tmp = strdup(perf_tool_event__to_str(i)); 14779aa09230SIan Rogers 14785ecd5a0cSIan Rogers if (!tmp) 14795ecd5a0cSIan Rogers return -ENOMEM; 14805ecd5a0cSIan Rogers ids__insert(ids->ids, tmp); 1481c788ef61SIan Rogers added_event = true; 14825ecd5a0cSIan Rogers } 14839aa09230SIan Rogers } 1484c788ef61SIan Rogers if (!added_event && hashmap__size(ids->ids) == 0) { 1485c788ef61SIan Rogers char *tmp = strdup("duration_time"); 1486c788ef61SIan Rogers 1487c788ef61SIan Rogers if (!tmp) 1488c788ef61SIan Rogers return -ENOMEM; 1489c788ef61SIan Rogers ids__insert(ids->ids, tmp); 1490c788ef61SIan Rogers } 14918586d274SIan Rogers } 1492b85a4d61SIan Rogers ret = metricgroup__build_event_string(&events, ids, modifier, 1493180a5013SIan Rogers group_events); 14945ecd5a0cSIan Rogers if (ret) 14955ecd5a0cSIan Rogers return ret; 14965ecd5a0cSIan Rogers 14975ecd5a0cSIan Rogers parsed_evlist = evlist__new(); 14985ecd5a0cSIan Rogers if (!parsed_evlist) { 14995ecd5a0cSIan Rogers ret = -ENOMEM; 15005ecd5a0cSIan Rogers goto err_out; 15015ecd5a0cSIan Rogers } 15025ecd5a0cSIan Rogers pr_debug("Parsing metric events '%s'\n", events.buf); 150307eafd4eSIan Rogers parse_events_error__init(&parse_error); 1504411ad22eSIan Rogers ret = __parse_events(parsed_evlist, events.buf, /*pmu_filter=*/NULL, 1505*a2a6604eSDominique Martinet &parse_error, fake_pmu, /*warn_if_reordered=*/false, 1506*a2a6604eSDominique Martinet /*fake_tp=*/false); 15075ecd5a0cSIan Rogers if (ret) { 15086c191289SIan Rogers parse_events_error__print(&parse_error, events.buf); 15095ecd5a0cSIan Rogers goto err_out; 15105ecd5a0cSIan Rogers } 1511b85a4d61SIan Rogers ret = decode_all_metric_ids(parsed_evlist, modifier); 15125ecd5a0cSIan Rogers if (ret) 15135ecd5a0cSIan Rogers goto err_out; 15145ecd5a0cSIan Rogers 15155ecd5a0cSIan Rogers *out_evlist = parsed_evlist; 15165ecd5a0cSIan Rogers parsed_evlist = NULL; 15175ecd5a0cSIan Rogers err_out: 151807eafd4eSIan Rogers parse_events_error__exit(&parse_error); 15195ecd5a0cSIan Rogers evlist__delete(parsed_evlist); 15205ecd5a0cSIan Rogers strbuf_release(&events); 15215ecd5a0cSIan Rogers return ret; 15225ecd5a0cSIan Rogers } 15235ecd5a0cSIan Rogers 1524bd3846d0SIan Rogers static int parse_groups(struct evlist *perf_evlist, 1525bd3846d0SIan Rogers const char *pmu, const char *str, 152605530a79SIan Rogers bool metric_no_group, 152705530a79SIan Rogers bool metric_no_merge, 15281fd09e29SIan Rogers bool metric_no_threshold, 15291725e9cdSIan Rogers const char *user_requested_cpu_list, 15301725e9cdSIan Rogers bool system_wide, 153168173bdaSJiri Olsa struct perf_pmu *fake_pmu, 15325ecd5a0cSIan Rogers struct rblist *metric_events_list, 1533f8ea2c15SIan Rogers const struct pmu_metrics_table *table) 1534b18f3e36SAndi Kleen { 15355ecd5a0cSIan Rogers struct evlist *combined_evlist = NULL; 1536119e521aSJiri Olsa LIST_HEAD(metric_list); 15375ecd5a0cSIan Rogers struct metric *m; 15388586d274SIan Rogers bool tool_events[PERF_TOOL_MAX] = {false}; 15391c0e4795SKan Liang bool is_default = !strcmp(str, "Default"); 1540b18f3e36SAndi Kleen int ret; 1541b18f3e36SAndi Kleen 15425ecd5a0cSIan Rogers if (metric_events_list->nr_entries == 0) 15435ecd5a0cSIan Rogers metricgroup__rblist_init(metric_events_list); 1544bd3846d0SIan Rogers ret = metricgroup__add_metric_list(pmu, str, metric_no_group, metric_no_threshold, 15451725e9cdSIan Rogers user_requested_cpu_list, 15461725e9cdSIan Rogers system_wide, &metric_list, table); 1547ec5c5b3dSIan Rogers if (ret) 1548ec5c5b3dSIan Rogers goto out; 1549ec5c5b3dSIan Rogers 15505ecd5a0cSIan Rogers /* Sort metrics from largest to smallest. */ 15515ecd5a0cSIan Rogers list_sort(NULL, &metric_list, metric_list_cmp); 15525ecd5a0cSIan Rogers 15535ecd5a0cSIan Rogers if (!metric_no_merge) { 15545ecd5a0cSIan Rogers struct expr_parse_ctx *combined = NULL; 15555ecd5a0cSIan Rogers 15568586d274SIan Rogers find_tool_events(&metric_list, tool_events); 15578586d274SIan Rogers 15585ecd5a0cSIan Rogers ret = build_combined_expr_ctx(&metric_list, &combined); 15595ecd5a0cSIan Rogers 15605ecd5a0cSIan Rogers if (!ret && combined && hashmap__size(combined->ids)) { 15616b6b16b3SIan Rogers ret = parse_ids(metric_no_merge, fake_pmu, combined, 15626b6b16b3SIan Rogers /*modifier=*/NULL, 1563180a5013SIan Rogers /*group_events=*/false, 15648586d274SIan Rogers tool_events, 156517b3867dSIan Rogers &combined_evlist); 15665ecd5a0cSIan Rogers } 15675ecd5a0cSIan Rogers if (combined) 15685ecd5a0cSIan Rogers expr__ctx_free(combined); 15695ecd5a0cSIan Rogers 15705ecd5a0cSIan Rogers if (ret) 15715ecd5a0cSIan Rogers goto out; 15725ecd5a0cSIan Rogers } 15735ecd5a0cSIan Rogers 15741c0e4795SKan Liang if (is_default) 15751c0e4795SKan Liang list_sort(NULL, &metric_list, default_metricgroup_cmp); 15761c0e4795SKan Liang 15775ecd5a0cSIan Rogers list_for_each_entry(m, &metric_list, nd) { 15785ecd5a0cSIan Rogers struct metric_event *me; 15795ecd5a0cSIan Rogers struct evsel **metric_events; 15805ecd5a0cSIan Rogers struct evlist *metric_evlist = NULL; 15815ecd5a0cSIan Rogers struct metric *n; 15825ecd5a0cSIan Rogers struct metric_expr *expr; 15835ecd5a0cSIan Rogers 1584180a5013SIan Rogers if (combined_evlist && !m->group_events) { 15855ecd5a0cSIan Rogers metric_evlist = combined_evlist; 15865ecd5a0cSIan Rogers } else if (!metric_no_merge) { 15875ecd5a0cSIan Rogers /* 15885ecd5a0cSIan Rogers * See if the IDs for this metric are a subset of an 15895ecd5a0cSIan Rogers * earlier metric. 15905ecd5a0cSIan Rogers */ 15915ecd5a0cSIan Rogers list_for_each_entry(n, &metric_list, nd) { 15925ecd5a0cSIan Rogers if (m == n) 15935ecd5a0cSIan Rogers break; 15945ecd5a0cSIan Rogers 15955ecd5a0cSIan Rogers if (n->evlist == NULL) 15965ecd5a0cSIan Rogers continue; 15975ecd5a0cSIan Rogers 1598b85a4d61SIan Rogers if ((!m->modifier && n->modifier) || 1599b85a4d61SIan Rogers (m->modifier && !n->modifier) || 1600b85a4d61SIan Rogers (m->modifier && n->modifier && 1601b85a4d61SIan Rogers strcmp(m->modifier, n->modifier))) 1602b85a4d61SIan Rogers continue; 1603b85a4d61SIan Rogers 1604bd3846d0SIan Rogers if ((!m->pmu && n->pmu) || 1605bd3846d0SIan Rogers (m->pmu && !n->pmu) || 1606bd3846d0SIan Rogers (m->pmu && n->pmu && strcmp(m->pmu, n->pmu))) 1607bd3846d0SIan Rogers continue; 1608bd3846d0SIan Rogers 16095ecd5a0cSIan Rogers if (expr__subset_of_ids(n->pctx, m->pctx)) { 16105ecd5a0cSIan Rogers pr_debug("Events in '%s' fully contained within '%s'\n", 16115ecd5a0cSIan Rogers m->metric_name, n->metric_name); 16125ecd5a0cSIan Rogers metric_evlist = n->evlist; 16135ecd5a0cSIan Rogers break; 16145ecd5a0cSIan Rogers } 16155ecd5a0cSIan Rogers 16165ecd5a0cSIan Rogers } 16175ecd5a0cSIan Rogers } 16185ecd5a0cSIan Rogers if (!metric_evlist) { 161917b3867dSIan Rogers ret = parse_ids(metric_no_merge, fake_pmu, m->pctx, m->modifier, 1620180a5013SIan Rogers m->group_events, tool_events, &m->evlist); 16215ecd5a0cSIan Rogers if (ret) 16225ecd5a0cSIan Rogers goto out; 16235ecd5a0cSIan Rogers 16245ecd5a0cSIan Rogers metric_evlist = m->evlist; 16255ecd5a0cSIan Rogers } 1626bd3846d0SIan Rogers ret = setup_metric_events(fake_pmu ? "all" : m->pmu, m->pctx->ids, 1627bd3846d0SIan Rogers metric_evlist, &metric_events); 16285ecd5a0cSIan Rogers if (ret) { 16298a4859c5SIan Rogers pr_err("Cannot resolve IDs for %s: %s\n", 16305ecd5a0cSIan Rogers m->metric_name, m->metric_expr); 16315ecd5a0cSIan Rogers goto out; 16325ecd5a0cSIan Rogers } 16335ecd5a0cSIan Rogers 16345ecd5a0cSIan Rogers me = metricgroup__lookup(metric_events_list, metric_events[0], true); 16355ecd5a0cSIan Rogers 16365ecd5a0cSIan Rogers expr = malloc(sizeof(struct metric_expr)); 16375ecd5a0cSIan Rogers if (!expr) { 16385ecd5a0cSIan Rogers ret = -ENOMEM; 16395ecd5a0cSIan Rogers free(metric_events); 16405ecd5a0cSIan Rogers goto out; 16415ecd5a0cSIan Rogers } 16425ecd5a0cSIan Rogers 16435ecd5a0cSIan Rogers expr->metric_refs = m->metric_refs; 16445ecd5a0cSIan Rogers m->metric_refs = NULL; 16455ecd5a0cSIan Rogers expr->metric_expr = m->metric_expr; 1646b85a4d61SIan Rogers if (m->modifier) { 1647b85a4d61SIan Rogers char *tmp; 1648b85a4d61SIan Rogers 1649b85a4d61SIan Rogers if (asprintf(&tmp, "%s:%s", m->metric_name, m->modifier) < 0) 1650b85a4d61SIan Rogers expr->metric_name = NULL; 1651b85a4d61SIan Rogers else 1652b85a4d61SIan Rogers expr->metric_name = tmp; 1653b85a4d61SIan Rogers } else 1654b85a4d61SIan Rogers expr->metric_name = strdup(m->metric_name); 1655b85a4d61SIan Rogers 1656b85a4d61SIan Rogers if (!expr->metric_name) { 1657b85a4d61SIan Rogers ret = -ENOMEM; 1658b85a4d61SIan Rogers free(metric_events); 1659b85a4d61SIan Rogers goto out; 1660b85a4d61SIan Rogers } 1661d0a3052fSIan Rogers expr->metric_threshold = m->metric_threshold; 16625ecd5a0cSIan Rogers expr->metric_unit = m->metric_unit; 16635ecd5a0cSIan Rogers expr->metric_events = metric_events; 16641a6abddeSIan Rogers expr->runtime = m->pctx->sctx.runtime; 16651c0e4795SKan Liang expr->default_metricgroup_name = m->default_metricgroup_name; 16661c0e4795SKan Liang me->is_default = is_default; 16675ecd5a0cSIan Rogers list_add(&expr->nd, &me->head); 16685ecd5a0cSIan Rogers } 16695ecd5a0cSIan Rogers 16705ecd5a0cSIan Rogers 1671aba8c5e3SIan Rogers if (combined_evlist) { 16725ecd5a0cSIan Rogers evlist__splice_list_tail(perf_evlist, &combined_evlist->core.entries); 1673aba8c5e3SIan Rogers evlist__delete(combined_evlist); 1674aba8c5e3SIan Rogers } 16755ecd5a0cSIan Rogers 16765ecd5a0cSIan Rogers list_for_each_entry(m, &metric_list, nd) { 16775ecd5a0cSIan Rogers if (m->evlist) 16785ecd5a0cSIan Rogers evlist__splice_list_tail(perf_evlist, &m->evlist->core.entries); 16795ecd5a0cSIan Rogers } 16805ecd5a0cSIan Rogers 1681b18f3e36SAndi Kleen out: 1682119e521aSJiri Olsa metricgroup__free_metrics(&metric_list); 1683b18f3e36SAndi Kleen return ret; 1684b18f3e36SAndi Kleen } 1685742d92ffSThomas Richter 1686a4b8cfcaSIan Rogers int metricgroup__parse_groups(struct evlist *perf_evlist, 1687dae47d39SIan Rogers const char *pmu, 16888b4468a2SJiri Olsa const char *str, 16898b4468a2SJiri Olsa bool metric_no_group, 16908b4468a2SJiri Olsa bool metric_no_merge, 16911fd09e29SIan Rogers bool metric_no_threshold, 16921725e9cdSIan Rogers const char *user_requested_cpu_list, 16931725e9cdSIan Rogers bool system_wide, 169403f23570SWeilin Wang bool hardware_aware_grouping, 16958b4468a2SJiri Olsa struct rblist *metric_events) 16968b4468a2SJiri Olsa { 1697f8ea2c15SIan Rogers const struct pmu_metrics_table *table = pmu_metrics_table__find(); 16988b4468a2SJiri Olsa 16993f5df3acSIan Rogers if (!table) 17003f5df3acSIan Rogers return -EINVAL; 170103f23570SWeilin Wang if (hardware_aware_grouping) 170203f23570SWeilin Wang pr_debug("Use hardware aware grouping instead of traditional metric grouping method\n"); 17033f5df3acSIan Rogers 1704dae47d39SIan Rogers return parse_groups(perf_evlist, pmu, str, metric_no_group, metric_no_merge, 17051fd09e29SIan Rogers metric_no_threshold, user_requested_cpu_list, system_wide, 17061725e9cdSIan Rogers /*fake_pmu=*/NULL, metric_events, table); 17078b4468a2SJiri Olsa } 17088b4468a2SJiri Olsa 1709f78ac00aSJiri Olsa int metricgroup__parse_groups_test(struct evlist *evlist, 1710f8ea2c15SIan Rogers const struct pmu_metrics_table *table, 1711f78ac00aSJiri Olsa const char *str, 1712f78ac00aSJiri Olsa struct rblist *metric_events) 1713f78ac00aSJiri Olsa { 1714bd3846d0SIan Rogers return parse_groups(evlist, "all", str, 17151fd09e29SIan Rogers /*metric_no_group=*/false, 17161fd09e29SIan Rogers /*metric_no_merge=*/false, 17171fd09e29SIan Rogers /*metric_no_threshold=*/false, 17181725e9cdSIan Rogers /*user_requested_cpu_list=*/NULL, 17191725e9cdSIan Rogers /*system_wide=*/false, 17201725e9cdSIan Rogers &perf_pmu__fake, metric_events, table); 1721f78ac00aSJiri Olsa } 1722f78ac00aSJiri Olsa 1723bd3846d0SIan Rogers struct metricgroup__has_metric_data { 1724bd3846d0SIan Rogers const char *pmu; 1725bd3846d0SIan Rogers const char *metric; 1726bd3846d0SIan Rogers }; 1727db95818eSIan Rogers static int metricgroup__has_metric_callback(const struct pmu_metric *pm, 1728f8ea2c15SIan Rogers const struct pmu_metrics_table *table __maybe_unused, 1729660842e4SIan Rogers void *vdata) 1730660842e4SIan Rogers { 1731bd3846d0SIan Rogers struct metricgroup__has_metric_data *data = vdata; 1732660842e4SIan Rogers 1733bd3846d0SIan Rogers return match_pm_metric(pm, data->pmu, data->metric) ? 1 : 0; 1734660842e4SIan Rogers } 1735660842e4SIan Rogers 1736bd3846d0SIan Rogers bool metricgroup__has_metric(const char *pmu, const char *metric) 1737742d92ffSThomas Richter { 1738f8ea2c15SIan Rogers const struct pmu_metrics_table *table = pmu_metrics_table__find(); 1739bd3846d0SIan Rogers struct metricgroup__has_metric_data data = { 1740bd3846d0SIan Rogers .pmu = pmu, 1741bd3846d0SIan Rogers .metric = metric, 1742bd3846d0SIan Rogers }; 1743742d92ffSThomas Richter 1744eeac7730SIan Rogers if (!table) 1745742d92ffSThomas Richter return false; 1746742d92ffSThomas Richter 17474000519eSIan Rogers return pmu_metrics_table__for_each_metric(table, metricgroup__has_metric_callback, &data) 1748bd3846d0SIan Rogers ? true : false; 1749742d92ffSThomas Richter } 1750b214ba8cSNamhyung Kim 17511647cd5bSIan Rogers static int metricgroup__topdown_max_level_callback(const struct pmu_metric *pm, 17521647cd5bSIan Rogers const struct pmu_metrics_table *table __maybe_unused, 17531647cd5bSIan Rogers void *data) 17541647cd5bSIan Rogers { 17551647cd5bSIan Rogers unsigned int *max_level = data; 17561647cd5bSIan Rogers unsigned int level; 17579dde1276SIan Rogers const char *p = strstr(pm->metric_group ?: "", "TopdownL"); 17581647cd5bSIan Rogers 17591647cd5bSIan Rogers if (!p || p[8] == '\0') 17601647cd5bSIan Rogers return 0; 17611647cd5bSIan Rogers 17621647cd5bSIan Rogers level = p[8] - '0'; 17631647cd5bSIan Rogers if (level > *max_level) 17641647cd5bSIan Rogers *max_level = level; 17651647cd5bSIan Rogers 17661647cd5bSIan Rogers return 0; 17671647cd5bSIan Rogers } 17681647cd5bSIan Rogers 17691647cd5bSIan Rogers unsigned int metricgroups__topdown_max_level(void) 17701647cd5bSIan Rogers { 17711647cd5bSIan Rogers unsigned int max_level = 0; 17721647cd5bSIan Rogers const struct pmu_metrics_table *table = pmu_metrics_table__find(); 17731647cd5bSIan Rogers 17741647cd5bSIan Rogers if (!table) 17751647cd5bSIan Rogers return false; 17761647cd5bSIan Rogers 17774000519eSIan Rogers pmu_metrics_table__for_each_metric(table, metricgroup__topdown_max_level_callback, 17781647cd5bSIan Rogers &max_level); 17791647cd5bSIan Rogers return max_level; 17801647cd5bSIan Rogers } 17811647cd5bSIan Rogers 1782b214ba8cSNamhyung Kim int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp, 1783b214ba8cSNamhyung Kim struct rblist *new_metric_events, 1784b214ba8cSNamhyung Kim struct rblist *old_metric_events) 1785b214ba8cSNamhyung Kim { 178684f879c5SXin Gao unsigned int i; 1787b214ba8cSNamhyung Kim 1788b214ba8cSNamhyung Kim for (i = 0; i < rblist__nr_entries(old_metric_events); i++) { 1789b214ba8cSNamhyung Kim struct rb_node *nd; 1790b214ba8cSNamhyung Kim struct metric_event *old_me, *new_me; 1791b214ba8cSNamhyung Kim struct metric_expr *old_expr, *new_expr; 1792b214ba8cSNamhyung Kim struct evsel *evsel; 1793b214ba8cSNamhyung Kim size_t alloc_size; 1794b214ba8cSNamhyung Kim int idx, nr; 1795b214ba8cSNamhyung Kim 1796b214ba8cSNamhyung Kim nd = rblist__entry(old_metric_events, i); 1797b214ba8cSNamhyung Kim old_me = container_of(nd, struct metric_event, nd); 1798b214ba8cSNamhyung Kim 179938fe0e01SJiri Olsa evsel = evlist__find_evsel(evlist, old_me->evsel->core.idx); 1800b214ba8cSNamhyung Kim if (!evsel) 1801b214ba8cSNamhyung Kim return -EINVAL; 1802b214ba8cSNamhyung Kim new_me = metricgroup__lookup(new_metric_events, evsel, true); 1803b214ba8cSNamhyung Kim if (!new_me) 1804b214ba8cSNamhyung Kim return -ENOMEM; 1805b214ba8cSNamhyung Kim 1806b214ba8cSNamhyung Kim pr_debug("copying metric event for cgroup '%s': %s (idx=%d)\n", 180738fe0e01SJiri Olsa cgrp ? cgrp->name : "root", evsel->name, evsel->core.idx); 1808b214ba8cSNamhyung Kim 1809b214ba8cSNamhyung Kim list_for_each_entry(old_expr, &old_me->head, nd) { 1810b214ba8cSNamhyung Kim new_expr = malloc(sizeof(*new_expr)); 1811b214ba8cSNamhyung Kim if (!new_expr) 1812b214ba8cSNamhyung Kim return -ENOMEM; 1813b214ba8cSNamhyung Kim 1814b214ba8cSNamhyung Kim new_expr->metric_expr = old_expr->metric_expr; 18156c73f819SIan Rogers new_expr->metric_threshold = old_expr->metric_threshold; 1816b85a4d61SIan Rogers new_expr->metric_name = strdup(old_expr->metric_name); 1817b85a4d61SIan Rogers if (!new_expr->metric_name) 1818b85a4d61SIan Rogers return -ENOMEM; 1819b85a4d61SIan Rogers 1820b214ba8cSNamhyung Kim new_expr->metric_unit = old_expr->metric_unit; 1821b214ba8cSNamhyung Kim new_expr->runtime = old_expr->runtime; 1822b214ba8cSNamhyung Kim 1823b214ba8cSNamhyung Kim if (old_expr->metric_refs) { 1824b214ba8cSNamhyung Kim /* calculate number of metric_events */ 1825b214ba8cSNamhyung Kim for (nr = 0; old_expr->metric_refs[nr].metric_name; nr++) 1826b214ba8cSNamhyung Kim continue; 1827b214ba8cSNamhyung Kim alloc_size = sizeof(*new_expr->metric_refs); 1828b214ba8cSNamhyung Kim new_expr->metric_refs = calloc(nr + 1, alloc_size); 1829b214ba8cSNamhyung Kim if (!new_expr->metric_refs) { 1830b214ba8cSNamhyung Kim free(new_expr); 1831b214ba8cSNamhyung Kim return -ENOMEM; 1832b214ba8cSNamhyung Kim } 1833b214ba8cSNamhyung Kim 1834b214ba8cSNamhyung Kim memcpy(new_expr->metric_refs, old_expr->metric_refs, 1835b214ba8cSNamhyung Kim nr * alloc_size); 1836b214ba8cSNamhyung Kim } else { 1837b214ba8cSNamhyung Kim new_expr->metric_refs = NULL; 1838b214ba8cSNamhyung Kim } 1839b214ba8cSNamhyung Kim 1840b214ba8cSNamhyung Kim /* calculate number of metric_events */ 1841b214ba8cSNamhyung Kim for (nr = 0; old_expr->metric_events[nr]; nr++) 1842b214ba8cSNamhyung Kim continue; 1843b214ba8cSNamhyung Kim alloc_size = sizeof(*new_expr->metric_events); 1844b214ba8cSNamhyung Kim new_expr->metric_events = calloc(nr + 1, alloc_size); 1845b214ba8cSNamhyung Kim if (!new_expr->metric_events) { 184611ff9bcdSArnaldo Carvalho de Melo zfree(&new_expr->metric_refs); 1847b214ba8cSNamhyung Kim free(new_expr); 1848b214ba8cSNamhyung Kim return -ENOMEM; 1849b214ba8cSNamhyung Kim } 1850b214ba8cSNamhyung Kim 1851b214ba8cSNamhyung Kim /* copy evsel in the same position */ 1852b214ba8cSNamhyung Kim for (idx = 0; idx < nr; idx++) { 1853b214ba8cSNamhyung Kim evsel = old_expr->metric_events[idx]; 185438fe0e01SJiri Olsa evsel = evlist__find_evsel(evlist, evsel->core.idx); 1855b214ba8cSNamhyung Kim if (evsel == NULL) { 185611ff9bcdSArnaldo Carvalho de Melo zfree(&new_expr->metric_events); 185711ff9bcdSArnaldo Carvalho de Melo zfree(&new_expr->metric_refs); 1858b214ba8cSNamhyung Kim free(new_expr); 1859b214ba8cSNamhyung Kim return -EINVAL; 1860b214ba8cSNamhyung Kim } 1861b214ba8cSNamhyung Kim new_expr->metric_events[idx] = evsel; 1862b214ba8cSNamhyung Kim } 1863b214ba8cSNamhyung Kim 1864b214ba8cSNamhyung Kim list_add(&new_expr->nd, &new_me->head); 1865b214ba8cSNamhyung Kim } 1866b214ba8cSNamhyung Kim } 1867b214ba8cSNamhyung Kim return 0; 1868b214ba8cSNamhyung Kim } 1869