1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2017, Intel Corporation.
6 /* Manage metrics and groups of metrics from JSON files */
8 #include "metricgroup.h"
14 #include "pmu-hybrid.h"
21 #include <linux/ctype.h>
22 #include <linux/list_sort.h>
23 #include <linux/string.h>
24 #include <linux/zalloc.h>
25 #include <perf/cpumap.h>
26 #include <subcmd/parse-options.h>
27 #include <api/fs/fs.h>
32 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
37 struct metric_event me = {
44 nd = rblist__find(metric_events, &me);
46 return container_of(nd, struct metric_event, nd);
48 rblist__add_node(metric_events, &me);
49 nd = rblist__find(metric_events, &me);
51 return container_of(nd, struct metric_event, nd);
56 static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
58 struct metric_event *a = container_of(rb_node,
61 const struct metric_event *b = entry;
63 if (a->evsel == b->evsel)
65 if ((char *)a->evsel < (char *)b->evsel)
70 static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
73 struct metric_event *me = malloc(sizeof(struct metric_event));
77 memcpy(me, entry, sizeof(struct metric_event));
78 me->evsel = ((struct metric_event *)entry)->evsel;
79 INIT_LIST_HEAD(&me->head);
83 static void metric_event_delete(struct rblist *rblist __maybe_unused,
84 struct rb_node *rb_node)
86 struct metric_event *me = container_of(rb_node, struct metric_event, nd);
87 struct metric_expr *expr, *tmp;
89 list_for_each_entry_safe(expr, tmp, &me->head, nd) {
90 free((char *)expr->metric_name);
91 free(expr->metric_refs);
92 free(expr->metric_events);
99 static void metricgroup__rblist_init(struct rblist *metric_events)
101 rblist__init(metric_events);
102 metric_events->node_cmp = metric_event_cmp;
103 metric_events->node_new = metric_event_new;
104 metric_events->node_delete = metric_event_delete;
107 void metricgroup__rblist_exit(struct rblist *metric_events)
109 rblist__exit(metric_events);
113 * The metric under construction. The data held here will be placed in a
119 * The expression parse context importantly holding the IDs contained
120 * within the expression.
122 struct expr_parse_ctx *pctx;
123 /** The name of the metric such as "IPC". */
124 const char *metric_name;
125 /** Modifier on the metric such as "u" or NULL for none. */
126 const char *modifier;
127 /** The expression to parse, for example, "instructions/cycles". */
128 const char *metric_expr;
130 * The "ScaleUnit" that scales and adds a unit to the metric during
133 const char *metric_unit;
134 /** Optional null terminated array of referenced metrics. */
135 struct metric_ref *metric_refs;
137 * Is there a constraint on the group of events? In which case the
138 * events won't be grouped.
142 * Parsed events for the metric. Optional as events may be taken from a
143 * different metric whose group contains all the IDs necessary for this
146 struct evlist *evlist;
149 static void metricgroup___watchdog_constraint_hint(const char *name, bool foot)
151 static bool violate_nmi_constraint;
154 pr_warning("Splitting metric group %s into standalone metrics.\n", name);
155 violate_nmi_constraint = true;
159 if (!violate_nmi_constraint)
162 pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n"
163 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
165 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
168 static bool metricgroup__has_constraint(const struct pmu_event *pe)
170 if (!pe->metric_constraint)
173 if (!strcmp(pe->metric_constraint, "NO_NMI_WATCHDOG") &&
174 sysctl__nmi_watchdog_enabled()) {
175 metricgroup___watchdog_constraint_hint(pe->metric_name, false);
182 static void metric__free(struct metric *m)
187 free(m->metric_refs);
188 expr__ctx_free(m->pctx);
189 free((char *)m->modifier);
190 evlist__delete(m->evlist);
194 static struct metric *metric__new(const struct pmu_event *pe,
195 const char *modifier,
196 bool metric_no_group,
198 const char *user_requested_cpu_list,
203 m = zalloc(sizeof(*m));
207 m->pctx = expr__ctx_new();
211 m->metric_name = pe->metric_name;
214 m->modifier = strdup(modifier);
218 m->metric_expr = pe->metric_expr;
219 m->metric_unit = pe->unit;
220 m->pctx->sctx.user_requested_cpu_list = NULL;
221 if (user_requested_cpu_list) {
222 m->pctx->sctx.user_requested_cpu_list = strdup(user_requested_cpu_list);
223 if (!m->pctx->sctx.user_requested_cpu_list)
226 m->pctx->sctx.runtime = runtime;
227 m->pctx->sctx.system_wide = system_wide;
228 m->has_constraint = metric_no_group || metricgroup__has_constraint(pe);
229 m->metric_refs = NULL;
238 static bool contains_metric_id(struct evsel **metric_events, int num_events,
239 const char *metric_id)
243 for (i = 0; i < num_events; i++) {
244 if (!strcmp(evsel__metric_id(metric_events[i]), metric_id))
251 * setup_metric_events - Find a group of events in metric_evlist that correspond
252 * to the IDs from a parsed metric expression.
253 * @ids: the metric IDs to match.
254 * @metric_evlist: the list of perf events.
255 * @out_metric_events: holds the created metric events array.
257 static int setup_metric_events(struct hashmap *ids,
258 struct evlist *metric_evlist,
259 struct evsel ***out_metric_events)
261 struct evsel **metric_events;
262 const char *metric_id;
264 size_t ids_size, matched_events, i;
266 *out_metric_events = NULL;
267 ids_size = hashmap__size(ids);
269 metric_events = calloc(sizeof(void *), ids_size + 1);
274 evlist__for_each_entry(metric_evlist, ev) {
275 struct expr_id_data *val_ptr;
278 * Check for duplicate events with the same name. For
279 * example, uncore_imc/cas_count_read/ will turn into 6
280 * events per socket on skylakex. Only the first such
281 * event is placed in metric_events.
283 metric_id = evsel__metric_id(ev);
284 if (contains_metric_id(metric_events, matched_events, metric_id))
287 * Does this event belong to the parse context? For
288 * combined or shared groups, this metric may not care
291 if (hashmap__find(ids, metric_id, (void **)&val_ptr)) {
292 metric_events[matched_events++] = ev;
294 if (matched_events >= ids_size)
298 if (matched_events < ids_size) {
302 for (i = 0; i < ids_size; i++) {
303 ev = metric_events[i];
304 ev->collect_stat = true;
307 * The metric leader points to the identically named
308 * event in metric_events.
310 ev->metric_leader = ev;
312 * Mark two events with identical names in the same
313 * group (or globally) as being in use as uncore events
314 * may be duplicated for each pmu. Set the metric leader
315 * of such events to be the event that appears in
318 metric_id = evsel__metric_id(ev);
319 evlist__for_each_entry_continue(metric_evlist, ev) {
320 if (!strcmp(evsel__metric_id(ev), metric_id))
321 ev->metric_leader = metric_events[i];
324 *out_metric_events = metric_events;
328 static bool match_metric(const char *n, const char *list)
335 if (!strcmp(list, "all"))
338 return !strcasecmp(list, "No_group");
340 m = strcasestr(n, list);
343 if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
344 (m[len] == 0 || m[len] == ';'))
349 static bool match_pe_metric(const struct pmu_event *pe, const char *metric)
351 return match_metric(pe->metric_group, metric) ||
352 match_metric(pe->metric_name, metric);
358 struct strlist *metrics;
361 static int mep_cmp(struct rb_node *rb_node, const void *entry)
363 struct mep *a = container_of(rb_node, struct mep, nd);
364 struct mep *b = (struct mep *)entry;
366 return strcmp(a->name, b->name);
369 static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
372 struct mep *me = malloc(sizeof(struct mep));
376 memcpy(me, entry, sizeof(struct mep));
377 me->name = strdup(me->name);
380 me->metrics = strlist__new(NULL, NULL);
391 static struct mep *mep_lookup(struct rblist *groups, const char *name)
397 nd = rblist__find(groups, &me);
399 return container_of(nd, struct mep, nd);
400 rblist__add_node(groups, &me);
401 nd = rblist__find(groups, &me);
403 return container_of(nd, struct mep, nd);
407 static void mep_delete(struct rblist *rl __maybe_unused,
410 struct mep *me = container_of(nd, struct mep, nd);
412 strlist__delete(me->metrics);
417 static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
422 strlist__for_each_entry (sn, metrics) {
424 printf("%s%s", n > 0 ? " " : "", sn->s);
426 printf(" %s\n", sn->s);
433 static int metricgroup__print_pmu_event(const struct pmu_event *pe,
434 bool metricgroups, char *filter,
435 bool raw, bool details,
436 struct rblist *groups,
437 struct strlist *metriclist)
442 g = pe->metric_group;
443 if (!g && pe->metric_name) {
457 while ((g = strsep(&mg, ";")) != NULL) {
464 if (filter && !strstr(g, filter))
467 s = (char *)pe->metric_name;
469 if (asprintf(&s, "%s\n%*s%s]",
470 pe->metric_name, 8, "[", pe->desc) < 0)
473 if (asprintf(&s, "%s\n%*s%s]",
474 s, 8, "[", pe->metric_expr) < 0)
483 strlist__add(metriclist, s);
485 me = mep_lookup(groups, g);
488 strlist__add(me->metrics, s);
499 struct metricgroup_print_sys_idata {
500 struct strlist *metriclist;
502 struct rblist *groups;
508 struct metricgroup_iter_data {
509 pmu_event_iter_fn fn;
513 static int metricgroup__sys_event_iter(const struct pmu_event *pe,
514 const struct pmu_events_table *table,
517 struct metricgroup_iter_data *d = data;
518 struct perf_pmu *pmu = NULL;
520 if (!pe->metric_expr || !pe->compat)
523 while ((pmu = perf_pmu__scan(pmu))) {
525 if (!pmu->id || strcmp(pmu->id, pe->compat))
528 return d->fn(pe, table, d->data);
534 static int metricgroup__print_sys_event_iter(const struct pmu_event *pe,
535 const struct pmu_events_table *table __maybe_unused,
538 struct metricgroup_print_sys_idata *d = data;
540 return metricgroup__print_pmu_event(pe, d->metricgroups, d->filter, d->raw,
541 d->details, d->groups, d->metriclist);
544 struct metricgroup_print_data {
545 const char *pmu_name;
546 struct strlist *metriclist;
548 struct rblist *groups;
554 static int metricgroup__print_callback(const struct pmu_event *pe,
555 const struct pmu_events_table *table __maybe_unused,
558 struct metricgroup_print_data *data = vdata;
560 if (!pe->metric_expr)
563 if (data->pmu_name && perf_pmu__is_hybrid(pe->pmu) && strcmp(data->pmu_name, pe->pmu))
566 return metricgroup__print_pmu_event(pe, data->metricgroups, data->filter,
567 data->raw, data->details, data->groups,
571 void metricgroup__print(bool metrics, bool metricgroups, char *filter,
572 bool raw, bool details, const char *pmu_name)
574 struct rblist groups;
575 struct rb_node *node, *next;
576 struct strlist *metriclist = NULL;
577 const struct pmu_events_table *table;
580 metriclist = strlist__new(NULL, NULL);
585 rblist__init(&groups);
586 groups.node_new = mep_new;
587 groups.node_cmp = mep_cmp;
588 groups.node_delete = mep_delete;
589 table = pmu_events_table__find();
591 struct metricgroup_print_data data = {
592 .pmu_name = pmu_name,
593 .metriclist = metriclist,
594 .metricgroups = metricgroups,
601 pmu_events_table_for_each_event(table,
602 metricgroup__print_callback,
606 struct metricgroup_iter_data data = {
607 .fn = metricgroup__print_sys_event_iter,
608 .data = (void *) &(struct metricgroup_print_sys_idata){
609 .metriclist = metriclist,
610 .metricgroups = metricgroups,
618 pmu_for_each_sys_event(metricgroup__sys_event_iter, &data);
621 if (!filter || !rblist__empty(&groups)) {
622 if (metricgroups && !raw)
623 printf("\nMetric Groups:\n\n");
624 else if (metrics && !raw)
625 printf("\nMetrics:\n\n");
628 for (node = rb_first_cached(&groups.entries); node; node = next) {
629 struct mep *me = container_of(node, struct mep, nd);
632 printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
634 metricgroup__print_strlist(me->metrics, raw);
635 next = rb_next(node);
636 rblist__remove_node(&groups, node);
639 metricgroup__print_strlist(metriclist, raw);
640 strlist__delete(metriclist);
643 static const char *code_characters = ",-=@";
645 static int encode_metric_id(struct strbuf *sb, const char *x)
651 c = strchr(code_characters, *x);
653 ret = strbuf_addch(sb, '!');
657 ret = strbuf_addch(sb, '0' + (c - code_characters));
661 ret = strbuf_addch(sb, *x);
669 static int decode_metric_id(struct strbuf *sb, const char *x)
671 const char *orig = x;
681 if (i > strlen(code_characters)) {
682 pr_err("Bad metric-id encoding in: '%s'", orig);
685 c = code_characters[i];
687 ret = strbuf_addch(sb, c);
694 static int decode_all_metric_ids(struct evlist *perf_evlist, const char *modifier)
697 struct strbuf sb = STRBUF_INIT;
701 evlist__for_each_entry(perf_evlist, ev) {
705 ret = strbuf_setlen(&sb, 0);
709 ret = decode_metric_id(&sb, ev->metric_id);
713 free((char *)ev->metric_id);
714 ev->metric_id = strdup(sb.buf);
715 if (!ev->metric_id) {
720 * If the name is just the parsed event, use the metric-id to
721 * give a more friendly display version.
723 if (strstr(ev->name, "metric-id=")) {
724 bool has_slash = false;
727 for (cur = strchr(sb.buf, '@') ; cur; cur = strchr(++cur, '@')) {
733 if (!has_slash && !strchr(sb.buf, ':')) {
734 ret = strbuf_addch(&sb, ':');
738 ret = strbuf_addstr(&sb, modifier);
742 ev->name = strdup(sb.buf);
753 static int metricgroup__build_event_string(struct strbuf *events,
754 const struct expr_parse_ctx *ctx,
755 const char *modifier,
758 struct hashmap_entry *cur;
760 bool no_group = true, has_tool_events = false;
761 bool tool_events[PERF_TOOL_MAX] = {false};
764 #define RETURN_IF_NON_ZERO(x) do { if (x) return x; } while (0)
766 hashmap__for_each_entry(ctx->ids, cur, bkt) {
767 const char *sep, *rsep, *id = cur->key;
768 enum perf_tool_event ev;
770 pr_debug("found event %s\n", id);
772 /* Always move tool events outside of the group. */
773 ev = perf_tool_event__from_str(id);
774 if (ev != PERF_TOOL_NONE) {
775 has_tool_events = true;
776 tool_events[ev] = true;
779 /* Separate events with commas and open the group if necessary. */
781 if (!has_constraint) {
782 ret = strbuf_addch(events, '{');
783 RETURN_IF_NON_ZERO(ret);
788 ret = strbuf_addch(events, ',');
789 RETURN_IF_NON_ZERO(ret);
792 * Encode the ID as an event string. Add a qualifier for
793 * metric_id that is the original name except with characters
794 * that parse-events can't parse replaced. For example,
795 * 'msr@tsc@' gets added as msr/tsc,metric-id=msr!3tsc!3/
797 sep = strchr(id, '@');
799 ret = strbuf_add(events, id, sep - id);
800 RETURN_IF_NON_ZERO(ret);
801 ret = strbuf_addch(events, '/');
802 RETURN_IF_NON_ZERO(ret);
803 rsep = strrchr(sep, '@');
804 ret = strbuf_add(events, sep + 1, rsep - sep - 1);
805 RETURN_IF_NON_ZERO(ret);
806 ret = strbuf_addstr(events, ",metric-id=");
807 RETURN_IF_NON_ZERO(ret);
810 sep = strchr(id, ':');
812 ret = strbuf_add(events, id, sep - id);
813 RETURN_IF_NON_ZERO(ret);
815 ret = strbuf_addstr(events, id);
816 RETURN_IF_NON_ZERO(ret);
818 ret = strbuf_addstr(events, "/metric-id=");
819 RETURN_IF_NON_ZERO(ret);
821 ret = encode_metric_id(events, id);
822 RETURN_IF_NON_ZERO(ret);
823 ret = strbuf_addstr(events, "/");
824 RETURN_IF_NON_ZERO(ret);
827 ret = strbuf_addstr(events, sep + 1);
828 RETURN_IF_NON_ZERO(ret);
831 ret = strbuf_addstr(events, modifier);
832 RETURN_IF_NON_ZERO(ret);
835 if (!no_group && !has_constraint) {
836 ret = strbuf_addf(events, "}:W");
837 RETURN_IF_NON_ZERO(ret);
839 if (has_tool_events) {
842 perf_tool_event__for_each_event(i) {
843 if (tool_events[i]) {
845 ret = strbuf_addch(events, ',');
846 RETURN_IF_NON_ZERO(ret);
849 ret = strbuf_addstr(events, perf_tool_event__to_str(i));
850 RETURN_IF_NON_ZERO(ret);
856 #undef RETURN_IF_NON_ZERO
859 int __weak arch_get_runtimeparam(const struct pmu_event *pe __maybe_unused)
865 * A singly linked list on the stack of the names of metrics being
866 * processed. Used to identify recursion.
868 struct visited_metric {
870 const struct visited_metric *parent;
873 struct metricgroup_add_iter_data {
874 struct list_head *metric_list;
875 const char *metric_name;
876 const char *modifier;
879 bool metric_no_group;
880 const char *user_requested_cpu_list;
882 struct metric *root_metric;
883 const struct visited_metric *visited;
884 const struct pmu_events_table *table;
887 static bool metricgroup__find_metric(const char *metric,
888 const struct pmu_events_table *table,
889 struct pmu_event *pe);
891 static int add_metric(struct list_head *metric_list,
892 const struct pmu_event *pe,
893 const char *modifier,
894 bool metric_no_group,
895 const char *user_requested_cpu_list,
897 struct metric *root_metric,
898 const struct visited_metric *visited,
899 const struct pmu_events_table *table);
902 * resolve_metric - Locate metrics within the root metric and recursively add
903 * references to them.
904 * @metric_list: The list the metric is added to.
905 * @modifier: if non-null event modifiers like "u".
906 * @metric_no_group: Should events written to events be grouped "{}" or
907 * global. Grouping is the default but due to multiplexing the
909 * @user_requested_cpu_list: Command line specified CPUs to record on.
910 * @system_wide: Are events for all processes recorded.
911 * @root_metric: Metrics may reference other metrics to form a tree. In this
912 * case the root_metric holds all the IDs and a list of referenced
913 * metrics. When adding a root this argument is NULL.
914 * @visited: A singly linked list of metric names being added that is used to
916 * @table: The table that is searched for metrics, most commonly the table for the
917 * architecture perf is running upon.
919 static int resolve_metric(struct list_head *metric_list,
920 const char *modifier,
921 bool metric_no_group,
922 const char *user_requested_cpu_list,
924 struct metric *root_metric,
925 const struct visited_metric *visited,
926 const struct pmu_events_table *table)
928 struct hashmap_entry *cur;
931 /* The metric to resolve. */
934 * The key in the IDs map, this may differ from in case,
935 * etc. from pe->metric_name.
939 int i, ret = 0, pending_cnt = 0;
942 * Iterate all the parsed IDs and if there's a matching metric and it to
945 hashmap__for_each_entry(root_metric->pctx->ids, cur, bkt) {
948 if (metricgroup__find_metric(cur->key, table, &pe)) {
949 pending = realloc(pending,
950 (pending_cnt + 1) * sizeof(struct to_resolve));
954 memcpy(&pending[pending_cnt].pe, &pe, sizeof(pe));
955 pending[pending_cnt].key = cur->key;
960 /* Remove the metric IDs from the context. */
961 for (i = 0; i < pending_cnt; i++)
962 expr__del_id(root_metric->pctx, pending[i].key);
965 * Recursively add all the metrics, IDs are added to the root metric's
968 for (i = 0; i < pending_cnt; i++) {
969 ret = add_metric(metric_list, &pending[i].pe, modifier, metric_no_group,
970 user_requested_cpu_list, system_wide, root_metric, visited,
981 * __add_metric - Add a metric to metric_list.
982 * @metric_list: The list the metric is added to.
983 * @pe: The pmu_event containing the metric to be added.
984 * @modifier: if non-null event modifiers like "u".
985 * @metric_no_group: Should events written to events be grouped "{}" or
986 * global. Grouping is the default but due to multiplexing the
988 * @runtime: A special argument for the parser only known at runtime.
989 * @user_requested_cpu_list: Command line specified CPUs to record on.
990 * @system_wide: Are events for all processes recorded.
991 * @root_metric: Metrics may reference other metrics to form a tree. In this
992 * case the root_metric holds all the IDs and a list of referenced
993 * metrics. When adding a root this argument is NULL.
994 * @visited: A singly linked list of metric names being added that is used to
996 * @table: The table that is searched for metrics, most commonly the table for the
997 * architecture perf is running upon.
999 static int __add_metric(struct list_head *metric_list,
1000 const struct pmu_event *pe,
1001 const char *modifier,
1002 bool metric_no_group,
1004 const char *user_requested_cpu_list,
1006 struct metric *root_metric,
1007 const struct visited_metric *visited,
1008 const struct pmu_events_table *table)
1010 const struct visited_metric *vm;
1012 bool is_root = !root_metric;
1013 struct visited_metric visited_node = {
1014 .name = pe->metric_name,
1018 for (vm = visited; vm; vm = vm->parent) {
1019 if (!strcmp(pe->metric_name, vm->name)) {
1020 pr_err("failed: recursion detected for %s\n", pe->metric_name);
1027 * This metric is the root of a tree and may reference other
1028 * metrics that are added recursively.
1030 root_metric = metric__new(pe, modifier, metric_no_group, runtime,
1031 user_requested_cpu_list, system_wide);
1039 * This metric was referenced in a metric higher in the
1040 * tree. Check if the same metric is already resolved in the
1043 if (root_metric->metric_refs) {
1044 for (; root_metric->metric_refs[cnt].metric_name; cnt++) {
1045 if (!strcmp(pe->metric_name,
1046 root_metric->metric_refs[cnt].metric_name))
1051 /* Create reference. Need space for the entry and the terminator. */
1052 root_metric->metric_refs = realloc(root_metric->metric_refs,
1053 (cnt + 2) * sizeof(struct metric_ref));
1054 if (!root_metric->metric_refs)
1058 * Intentionally passing just const char pointers,
1059 * from 'pe' object, so they never go away. We don't
1060 * need to change them, so there's no need to create
1063 root_metric->metric_refs[cnt].metric_name = pe->metric_name;
1064 root_metric->metric_refs[cnt].metric_expr = pe->metric_expr;
1066 /* Null terminate array. */
1067 root_metric->metric_refs[cnt+1].metric_name = NULL;
1068 root_metric->metric_refs[cnt+1].metric_expr = NULL;
1072 * For both the parent and referenced metrics, we parse
1073 * all the metric's IDs and add it to the root context.
1075 if (expr__find_ids(pe->metric_expr, NULL, root_metric->pctx) < 0) {
1076 /* Broken metric. */
1079 /* Resolve referenced metrics. */
1080 ret = resolve_metric(metric_list, modifier, metric_no_group,
1081 user_requested_cpu_list, system_wide,
1082 root_metric, &visited_node, table);
1087 metric__free(root_metric);
1090 list_add(&root_metric->nd, metric_list);
1095 struct metricgroup__find_metric_data {
1097 struct pmu_event *pe;
1100 static int metricgroup__find_metric_callback(const struct pmu_event *pe,
1101 const struct pmu_events_table *table __maybe_unused,
1104 struct metricgroup__find_metric_data *data = vdata;
1106 if (!match_metric(pe->metric_name, data->metric))
1109 memcpy(data->pe, pe, sizeof(*pe));
1113 static bool metricgroup__find_metric(const char *metric,
1114 const struct pmu_events_table *table,
1115 struct pmu_event *pe)
1117 struct metricgroup__find_metric_data data = {
1122 return pmu_events_table_for_each_event(table, metricgroup__find_metric_callback, &data)
1126 static int add_metric(struct list_head *metric_list,
1127 const struct pmu_event *pe,
1128 const char *modifier,
1129 bool metric_no_group,
1130 const char *user_requested_cpu_list,
1132 struct metric *root_metric,
1133 const struct visited_metric *visited,
1134 const struct pmu_events_table *table)
1138 pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
1140 if (!strstr(pe->metric_expr, "?")) {
1141 ret = __add_metric(metric_list, pe, modifier, metric_no_group, 0,
1142 user_requested_cpu_list, system_wide, root_metric,
1147 count = arch_get_runtimeparam(pe);
1149 /* This loop is added to create multiple
1150 * events depend on count value and add
1151 * those events to metric_list.
1154 for (j = 0; j < count && !ret; j++)
1155 ret = __add_metric(metric_list, pe, modifier, metric_no_group, j,
1156 user_requested_cpu_list, system_wide,
1157 root_metric, visited, table);
1163 static int metricgroup__add_metric_sys_event_iter(const struct pmu_event *pe,
1164 const struct pmu_events_table *table __maybe_unused,
1167 struct metricgroup_add_iter_data *d = data;
1170 if (!match_pe_metric(pe, d->metric_name))
1173 ret = add_metric(d->metric_list, pe, d->modifier, d->metric_no_group,
1174 d->user_requested_cpu_list, d->system_wide,
1175 d->root_metric, d->visited, d->table);
1179 *(d->has_match) = true;
1187 * metric_list_cmp - list_sort comparator that sorts metrics with more events to
1188 * the front. tool events are excluded from the count.
1190 static int metric_list_cmp(void *priv __maybe_unused, const struct list_head *l,
1191 const struct list_head *r)
1193 const struct metric *left = container_of(l, struct metric, nd);
1194 const struct metric *right = container_of(r, struct metric, nd);
1195 struct expr_id_data *data;
1196 int i, left_count, right_count;
1198 left_count = hashmap__size(left->pctx->ids);
1199 perf_tool_event__for_each_event(i) {
1200 if (!expr__get_id(left->pctx, perf_tool_event__to_str(i), &data))
1204 right_count = hashmap__size(right->pctx->ids);
1205 perf_tool_event__for_each_event(i) {
1206 if (!expr__get_id(right->pctx, perf_tool_event__to_str(i), &data))
1210 return right_count - left_count;
1213 struct metricgroup__add_metric_data {
1214 struct list_head *list;
1215 const char *metric_name;
1216 const char *modifier;
1217 const char *user_requested_cpu_list;
1218 bool metric_no_group;
1223 static int metricgroup__add_metric_callback(const struct pmu_event *pe,
1224 const struct pmu_events_table *table,
1227 struct metricgroup__add_metric_data *data = vdata;
1230 if (pe->metric_expr &&
1231 (match_metric(pe->metric_group, data->metric_name) ||
1232 match_metric(pe->metric_name, data->metric_name))) {
1234 data->has_match = true;
1235 ret = add_metric(data->list, pe, data->modifier, data->metric_no_group,
1236 data->user_requested_cpu_list, data->system_wide,
1237 /*root_metric=*/NULL, /*visited_metrics=*/NULL, table);
1243 * metricgroup__add_metric - Find and add a metric, or a metric group.
1244 * @metric_name: The name of the metric or metric group. For example, "IPC"
1245 * could be the name of a metric and "TopDownL1" the name of a
1247 * @modifier: if non-null event modifiers like "u".
1248 * @metric_no_group: Should events written to events be grouped "{}" or
1249 * global. Grouping is the default but due to multiplexing the
1250 * user may override.
1251 * @user_requested_cpu_list: Command line specified CPUs to record on.
1252 * @system_wide: Are events for all processes recorded.
1253 * @metric_list: The list that the metric or metric group are added to.
1254 * @table: The table that is searched for metrics, most commonly the table for the
1255 * architecture perf is running upon.
1257 static int metricgroup__add_metric(const char *metric_name, const char *modifier,
1258 bool metric_no_group,
1259 const char *user_requested_cpu_list,
1261 struct list_head *metric_list,
1262 const struct pmu_events_table *table)
1266 bool has_match = false;
1269 struct metricgroup__add_metric_data data = {
1271 .metric_name = metric_name,
1272 .modifier = modifier,
1273 .metric_no_group = metric_no_group,
1274 .user_requested_cpu_list = user_requested_cpu_list,
1275 .system_wide = system_wide,
1279 * Iterate over all metrics seeing if metric matches either the
1280 * name or group. When it does add the metric to the list.
1282 ret = pmu_events_table_for_each_event(table, metricgroup__add_metric_callback,
1287 has_match = data.has_match;
1290 struct metricgroup_iter_data data = {
1291 .fn = metricgroup__add_metric_sys_event_iter,
1292 .data = (void *) &(struct metricgroup_add_iter_data) {
1293 .metric_list = &list,
1294 .metric_name = metric_name,
1295 .modifier = modifier,
1296 .metric_no_group = metric_no_group,
1297 .user_requested_cpu_list = user_requested_cpu_list,
1298 .system_wide = system_wide,
1299 .has_match = &has_match,
1305 pmu_for_each_sys_event(metricgroup__sys_event_iter, &data);
1307 /* End of pmu events. */
1313 * add to metric_list so that they can be released
1314 * even if it's failed
1316 list_splice(&list, metric_list);
1321 * metricgroup__add_metric_list - Find and add metrics, or metric groups,
1322 * specified in a list.
1323 * @list: the list of metrics or metric groups. For example, "IPC,CPI,TopDownL1"
1324 * would match the IPC and CPI metrics, and TopDownL1 would match all
1325 * the metrics in the TopDownL1 group.
1326 * @metric_no_group: Should events written to events be grouped "{}" or
1327 * global. Grouping is the default but due to multiplexing the
1328 * user may override.
1329 * @user_requested_cpu_list: Command line specified CPUs to record on.
1330 * @system_wide: Are events for all processes recorded.
1331 * @metric_list: The list that metrics are added to.
1332 * @table: The table that is searched for metrics, most commonly the table for the
1333 * architecture perf is running upon.
1335 static int metricgroup__add_metric_list(const char *list, bool metric_no_group,
1336 const char *user_requested_cpu_list,
1337 bool system_wide, struct list_head *metric_list,
1338 const struct pmu_events_table *table)
1340 char *list_itr, *list_copy, *metric_name, *modifier;
1343 list_copy = strdup(list);
1346 list_itr = list_copy;
1348 while ((metric_name = strsep(&list_itr, ",")) != NULL) {
1349 modifier = strchr(metric_name, ':');
1353 ret = metricgroup__add_metric(metric_name, modifier,
1354 metric_no_group, user_requested_cpu_list,
1355 system_wide, metric_list, table);
1357 pr_err("Cannot find metric or group `%s'\n", metric_name);
1368 * Warn about nmi_watchdog if any parsed metrics had the
1369 * NO_NMI_WATCHDOG constraint.
1371 metricgroup___watchdog_constraint_hint(NULL, true);
1379 static void metricgroup__free_metrics(struct list_head *metric_list)
1381 struct metric *m, *tmp;
1383 list_for_each_entry_safe (m, tmp, metric_list, nd) {
1384 list_del_init(&m->nd);
1390 * find_tool_events - Search for the pressence of tool events in metric_list.
1391 * @metric_list: List to take metrics from.
1392 * @tool_events: Array of false values, indices corresponding to tool events set
1393 * to true if tool event is found.
1395 static void find_tool_events(const struct list_head *metric_list,
1396 bool tool_events[PERF_TOOL_MAX])
1400 list_for_each_entry(m, metric_list, nd) {
1403 perf_tool_event__for_each_event(i) {
1404 struct expr_id_data *data;
1406 if (!tool_events[i] &&
1407 !expr__get_id(m->pctx, perf_tool_event__to_str(i), &data))
1408 tool_events[i] = true;
1414 * build_combined_expr_ctx - Make an expr_parse_ctx with all has_constraint
1415 * metric IDs, as the IDs are held in a set,
1416 * duplicates will be removed.
1417 * @metric_list: List to take metrics from.
1418 * @combined: Out argument for result.
1420 static int build_combined_expr_ctx(const struct list_head *metric_list,
1421 struct expr_parse_ctx **combined)
1423 struct hashmap_entry *cur;
1429 *combined = expr__ctx_new();
1433 list_for_each_entry(m, metric_list, nd) {
1434 if (m->has_constraint && !m->modifier) {
1435 hashmap__for_each_entry(m->pctx->ids, cur, bkt) {
1436 dup = strdup(cur->key);
1441 ret = expr__add_id(*combined, dup);
1449 expr__ctx_free(*combined);
1455 * parse_ids - Build the event string for the ids and parse them creating an
1456 * evlist. The encoded metric_ids are decoded.
1457 * @metric_no_merge: is metric sharing explicitly disabled.
1458 * @fake_pmu: used when testing metrics not supported by the current CPU.
1459 * @ids: the event identifiers parsed from a metric.
1460 * @modifier: any modifiers added to the events.
1461 * @has_constraint: false if events should be placed in a weak group.
1462 * @tool_events: entries set true if the tool event of index could be present in
1463 * the overall list of metrics.
1464 * @out_evlist: the created list of events.
1466 static int parse_ids(bool metric_no_merge, struct perf_pmu *fake_pmu,
1467 struct expr_parse_ctx *ids, const char *modifier,
1468 bool has_constraint, const bool tool_events[PERF_TOOL_MAX],
1469 struct evlist **out_evlist)
1471 struct parse_events_error parse_error;
1472 struct evlist *parsed_evlist;
1473 struct strbuf events = STRBUF_INIT;
1477 if (!metric_no_merge || hashmap__size(ids->ids) == 0) {
1478 bool added_event = false;
1481 * We may fail to share events between metrics because a tool
1482 * event isn't present in one metric. For example, a ratio of
1483 * cache misses doesn't need duration_time but the same events
1484 * may be used for a misses per second. Events without sharing
1485 * implies multiplexing, that is best avoided, so place
1486 * all tool events in every group.
1488 * Also, there may be no ids/events in the expression parsing
1489 * context because of constant evaluation, e.g.:
1490 * event1 if #smt_on else 0
1491 * Add a tool event to avoid a parse error on an empty string.
1493 perf_tool_event__for_each_event(i) {
1494 if (tool_events[i]) {
1495 char *tmp = strdup(perf_tool_event__to_str(i));
1499 ids__insert(ids->ids, tmp);
1503 if (!added_event && hashmap__size(ids->ids) == 0) {
1504 char *tmp = strdup("duration_time");
1508 ids__insert(ids->ids, tmp);
1511 ret = metricgroup__build_event_string(&events, ids, modifier,
1516 parsed_evlist = evlist__new();
1517 if (!parsed_evlist) {
1521 pr_debug("Parsing metric events '%s'\n", events.buf);
1522 parse_events_error__init(&parse_error);
1523 ret = __parse_events(parsed_evlist, events.buf, &parse_error, fake_pmu);
1525 parse_events_error__print(&parse_error, events.buf);
1528 ret = decode_all_metric_ids(parsed_evlist, modifier);
1532 *out_evlist = parsed_evlist;
1533 parsed_evlist = NULL;
1535 parse_events_error__exit(&parse_error);
1536 evlist__delete(parsed_evlist);
1537 strbuf_release(&events);
1541 static int parse_groups(struct evlist *perf_evlist, const char *str,
1542 bool metric_no_group,
1543 bool metric_no_merge,
1544 const char *user_requested_cpu_list,
1546 struct perf_pmu *fake_pmu,
1547 struct rblist *metric_events_list,
1548 const struct pmu_events_table *table)
1550 struct evlist *combined_evlist = NULL;
1551 LIST_HEAD(metric_list);
1553 bool tool_events[PERF_TOOL_MAX] = {false};
1556 if (metric_events_list->nr_entries == 0)
1557 metricgroup__rblist_init(metric_events_list);
1558 ret = metricgroup__add_metric_list(str, metric_no_group,
1559 user_requested_cpu_list,
1560 system_wide, &metric_list, table);
1564 /* Sort metrics from largest to smallest. */
1565 list_sort(NULL, &metric_list, metric_list_cmp);
1567 if (!metric_no_merge) {
1568 struct expr_parse_ctx *combined = NULL;
1570 find_tool_events(&metric_list, tool_events);
1572 ret = build_combined_expr_ctx(&metric_list, &combined);
1574 if (!ret && combined && hashmap__size(combined->ids)) {
1575 ret = parse_ids(metric_no_merge, fake_pmu, combined,
1577 /*has_constraint=*/true,
1582 expr__ctx_free(combined);
1588 list_for_each_entry(m, &metric_list, nd) {
1589 struct metric_event *me;
1590 struct evsel **metric_events;
1591 struct evlist *metric_evlist = NULL;
1593 struct metric_expr *expr;
1595 if (combined_evlist && m->has_constraint) {
1596 metric_evlist = combined_evlist;
1597 } else if (!metric_no_merge) {
1599 * See if the IDs for this metric are a subset of an
1602 list_for_each_entry(n, &metric_list, nd) {
1606 if (n->evlist == NULL)
1609 if ((!m->modifier && n->modifier) ||
1610 (m->modifier && !n->modifier) ||
1611 (m->modifier && n->modifier &&
1612 strcmp(m->modifier, n->modifier)))
1615 if (expr__subset_of_ids(n->pctx, m->pctx)) {
1616 pr_debug("Events in '%s' fully contained within '%s'\n",
1617 m->metric_name, n->metric_name);
1618 metric_evlist = n->evlist;
1624 if (!metric_evlist) {
1625 ret = parse_ids(metric_no_merge, fake_pmu, m->pctx, m->modifier,
1626 m->has_constraint, tool_events, &m->evlist);
1630 metric_evlist = m->evlist;
1632 ret = setup_metric_events(m->pctx->ids, metric_evlist, &metric_events);
1634 pr_debug("Cannot resolve IDs for %s: %s\n",
1635 m->metric_name, m->metric_expr);
1639 me = metricgroup__lookup(metric_events_list, metric_events[0], true);
1641 expr = malloc(sizeof(struct metric_expr));
1644 free(metric_events);
1648 expr->metric_refs = m->metric_refs;
1649 m->metric_refs = NULL;
1650 expr->metric_expr = m->metric_expr;
1654 if (asprintf(&tmp, "%s:%s", m->metric_name, m->modifier) < 0)
1655 expr->metric_name = NULL;
1657 expr->metric_name = tmp;
1659 expr->metric_name = strdup(m->metric_name);
1661 if (!expr->metric_name) {
1663 free(metric_events);
1666 expr->metric_unit = m->metric_unit;
1667 expr->metric_events = metric_events;
1668 expr->runtime = m->pctx->sctx.runtime;
1669 list_add(&expr->nd, &me->head);
1673 if (combined_evlist) {
1674 evlist__splice_list_tail(perf_evlist, &combined_evlist->core.entries);
1675 evlist__delete(combined_evlist);
1678 list_for_each_entry(m, &metric_list, nd) {
1680 evlist__splice_list_tail(perf_evlist, &m->evlist->core.entries);
1684 metricgroup__free_metrics(&metric_list);
1688 int metricgroup__parse_groups(struct evlist *perf_evlist,
1690 bool metric_no_group,
1691 bool metric_no_merge,
1692 const char *user_requested_cpu_list,
1694 struct rblist *metric_events)
1696 const struct pmu_events_table *table = pmu_events_table__find();
1701 return parse_groups(perf_evlist, str, metric_no_group, metric_no_merge,
1702 user_requested_cpu_list, system_wide,
1703 /*fake_pmu=*/NULL, metric_events, table);
1706 int metricgroup__parse_groups_test(struct evlist *evlist,
1707 const struct pmu_events_table *table,
1709 bool metric_no_group,
1710 bool metric_no_merge,
1711 struct rblist *metric_events)
1713 return parse_groups(evlist, str, metric_no_group, metric_no_merge,
1714 /*user_requested_cpu_list=*/NULL,
1715 /*system_wide=*/false,
1716 &perf_pmu__fake, metric_events, table);
1719 static int metricgroup__has_metric_callback(const struct pmu_event *pe,
1720 const struct pmu_events_table *table __maybe_unused,
1723 const char *metric = vdata;
1725 if (!pe->metric_expr)
1728 if (match_metric(pe->metric_name, metric))
1734 bool metricgroup__has_metric(const char *metric)
1736 const struct pmu_events_table *table = pmu_events_table__find();
1741 return pmu_events_table_for_each_event(table, metricgroup__has_metric_callback,
1742 (void *)metric) ? true : false;
1745 int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
1746 struct rblist *new_metric_events,
1747 struct rblist *old_metric_events)
1751 for (i = 0; i < rblist__nr_entries(old_metric_events); i++) {
1753 struct metric_event *old_me, *new_me;
1754 struct metric_expr *old_expr, *new_expr;
1755 struct evsel *evsel;
1759 nd = rblist__entry(old_metric_events, i);
1760 old_me = container_of(nd, struct metric_event, nd);
1762 evsel = evlist__find_evsel(evlist, old_me->evsel->core.idx);
1765 new_me = metricgroup__lookup(new_metric_events, evsel, true);
1769 pr_debug("copying metric event for cgroup '%s': %s (idx=%d)\n",
1770 cgrp ? cgrp->name : "root", evsel->name, evsel->core.idx);
1772 list_for_each_entry(old_expr, &old_me->head, nd) {
1773 new_expr = malloc(sizeof(*new_expr));
1777 new_expr->metric_expr = old_expr->metric_expr;
1778 new_expr->metric_name = strdup(old_expr->metric_name);
1779 if (!new_expr->metric_name)
1782 new_expr->metric_unit = old_expr->metric_unit;
1783 new_expr->runtime = old_expr->runtime;
1785 if (old_expr->metric_refs) {
1786 /* calculate number of metric_events */
1787 for (nr = 0; old_expr->metric_refs[nr].metric_name; nr++)
1789 alloc_size = sizeof(*new_expr->metric_refs);
1790 new_expr->metric_refs = calloc(nr + 1, alloc_size);
1791 if (!new_expr->metric_refs) {
1796 memcpy(new_expr->metric_refs, old_expr->metric_refs,
1799 new_expr->metric_refs = NULL;
1802 /* calculate number of metric_events */
1803 for (nr = 0; old_expr->metric_events[nr]; nr++)
1805 alloc_size = sizeof(*new_expr->metric_events);
1806 new_expr->metric_events = calloc(nr + 1, alloc_size);
1807 if (!new_expr->metric_events) {
1808 free(new_expr->metric_refs);
1813 /* copy evsel in the same position */
1814 for (idx = 0; idx < nr; idx++) {
1815 evsel = old_expr->metric_events[idx];
1816 evsel = evlist__find_evsel(evlist, evsel->core.idx);
1817 if (evsel == NULL) {
1818 free(new_expr->metric_events);
1819 free(new_expr->metric_refs);
1823 new_expr->metric_events[idx] = evsel;
1826 list_add(&new_expr->nd, &new_me->head);