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 <subcmd/parse-options.h>
26 #include <api/fs/fs.h>
31 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
36 struct metric_event me = {
43 nd = rblist__find(metric_events, &me);
45 return container_of(nd, struct metric_event, nd);
47 rblist__add_node(metric_events, &me);
48 nd = rblist__find(metric_events, &me);
50 return container_of(nd, struct metric_event, nd);
55 static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
57 struct metric_event *a = container_of(rb_node,
60 const struct metric_event *b = entry;
62 if (a->evsel == b->evsel)
64 if ((char *)a->evsel < (char *)b->evsel)
69 static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
72 struct metric_event *me = malloc(sizeof(struct metric_event));
76 memcpy(me, entry, sizeof(struct metric_event));
77 me->evsel = ((struct metric_event *)entry)->evsel;
78 INIT_LIST_HEAD(&me->head);
82 static void metric_event_delete(struct rblist *rblist __maybe_unused,
83 struct rb_node *rb_node)
85 struct metric_event *me = container_of(rb_node, struct metric_event, nd);
86 struct metric_expr *expr, *tmp;
88 list_for_each_entry_safe(expr, tmp, &me->head, nd) {
89 free((char *)expr->metric_name);
90 free(expr->metric_refs);
91 free(expr->metric_events);
98 static void metricgroup__rblist_init(struct rblist *metric_events)
100 rblist__init(metric_events);
101 metric_events->node_cmp = metric_event_cmp;
102 metric_events->node_new = metric_event_new;
103 metric_events->node_delete = metric_event_delete;
106 void metricgroup__rblist_exit(struct rblist *metric_events)
108 rblist__exit(metric_events);
112 * A node in the list of referenced metrics. metric_expr
113 * is held as a convenience to avoid a search through the
116 struct metric_ref_node {
117 const char *metric_name;
118 const char *metric_expr;
119 struct list_head list;
123 * The metric under construction. The data held here will be placed in a
129 * The expression parse context importantly holding the IDs contained
130 * within the expression.
132 struct expr_parse_ctx *pctx;
133 /** The name of the metric such as "IPC". */
134 const char *metric_name;
135 /** Modifier on the metric such as "u" or NULL for none. */
136 const char *modifier;
137 /** The expression to parse, for example, "instructions/cycles". */
138 const char *metric_expr;
140 * The "ScaleUnit" that scales and adds a unit to the metric during
143 const char *metric_unit;
144 /** Optional null terminated array of referenced metrics. */
145 struct metric_ref *metric_refs;
147 * Is there a constraint on the group of events? In which case the
148 * events won't be grouped.
152 * Parsed events for the metric. Optional as events may be taken from a
153 * different metric whose group contains all the IDs necessary for this
156 struct evlist *evlist;
159 static void metricgroup___watchdog_constraint_hint(const char *name, bool foot)
161 static bool violate_nmi_constraint;
164 pr_warning("Splitting metric group %s into standalone metrics.\n", name);
165 violate_nmi_constraint = true;
169 if (!violate_nmi_constraint)
172 pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n"
173 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
175 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
178 static bool metricgroup__has_constraint(const struct pmu_event *pe)
180 if (!pe->metric_constraint)
183 if (!strcmp(pe->metric_constraint, "NO_NMI_WATCHDOG") &&
184 sysctl__nmi_watchdog_enabled()) {
185 metricgroup___watchdog_constraint_hint(pe->metric_name, false);
192 static struct metric *metric__new(const struct pmu_event *pe,
193 const char *modifier,
194 bool metric_no_group,
199 m = zalloc(sizeof(*m));
203 m->pctx = expr__ctx_new();
209 m->metric_name = pe->metric_name;
210 m->modifier = modifier ? strdup(modifier) : NULL;
211 if (modifier && !m->modifier) {
212 expr__ctx_free(m->pctx);
216 m->metric_expr = pe->metric_expr;
217 m->metric_unit = pe->unit;
218 m->pctx->runtime = runtime;
219 m->has_constraint = metric_no_group || metricgroup__has_constraint(pe);
220 m->metric_refs = NULL;
226 static void metric__free(struct metric *m)
228 free(m->metric_refs);
229 expr__ctx_free(m->pctx);
230 free((char *)m->modifier);
231 evlist__delete(m->evlist);
235 static bool contains_metric_id(struct evsel **metric_events, int num_events,
236 const char *metric_id)
240 for (i = 0; i < num_events; i++) {
241 if (!strcmp(evsel__metric_id(metric_events[i]), metric_id))
248 * setup_metric_events - Find a group of events in metric_evlist that correspond
249 * to the IDs from a parsed metric expression.
250 * @ids: the metric IDs to match.
251 * @metric_evlist: the list of perf events.
252 * @out_metric_events: holds the created metric events array.
254 static int setup_metric_events(struct hashmap *ids,
255 struct evlist *metric_evlist,
256 struct evsel ***out_metric_events)
258 struct evsel **metric_events;
259 const char *metric_id;
261 size_t ids_size, matched_events, i;
263 *out_metric_events = NULL;
264 ids_size = hashmap__size(ids);
266 metric_events = calloc(sizeof(void *), ids_size + 1);
271 evlist__for_each_entry(metric_evlist, ev) {
272 struct expr_id_data *val_ptr;
275 * Check for duplicate events with the same name. For
276 * example, uncore_imc/cas_count_read/ will turn into 6
277 * events per socket on skylakex. Only the first such
278 * event is placed in metric_events.
280 metric_id = evsel__metric_id(ev);
281 if (contains_metric_id(metric_events, matched_events, metric_id))
284 * Does this event belong to the parse context? For
285 * combined or shared groups, this metric may not care
288 if (hashmap__find(ids, metric_id, (void **)&val_ptr)) {
289 metric_events[matched_events++] = ev;
291 if (matched_events >= ids_size)
295 if (matched_events < ids_size) {
299 for (i = 0; i < ids_size; i++) {
300 ev = metric_events[i];
301 ev->collect_stat = true;
304 * The metric leader points to the identically named
305 * event in metric_events.
307 ev->metric_leader = ev;
309 * Mark two events with identical names in the same
310 * group (or globally) as being in use as uncore events
311 * may be duplicated for each pmu. Set the metric leader
312 * of such events to be the event that appears in
315 metric_id = evsel__metric_id(ev);
316 evlist__for_each_entry_continue(metric_evlist, ev) {
317 if (!strcmp(evsel__metric_id(ev), metric_id))
318 ev->metric_leader = metric_events[i];
321 *out_metric_events = metric_events;
325 static bool match_metric(const char *n, const char *list)
332 if (!strcmp(list, "all"))
335 return !strcasecmp(list, "No_group");
337 m = strcasestr(n, list);
340 if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
341 (m[len] == 0 || m[len] == ';'))
346 static bool match_pe_metric(const struct pmu_event *pe, const char *metric)
348 return match_metric(pe->metric_group, metric) ||
349 match_metric(pe->metric_name, metric);
355 struct strlist *metrics;
358 static int mep_cmp(struct rb_node *rb_node, const void *entry)
360 struct mep *a = container_of(rb_node, struct mep, nd);
361 struct mep *b = (struct mep *)entry;
363 return strcmp(a->name, b->name);
366 static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
369 struct mep *me = malloc(sizeof(struct mep));
373 memcpy(me, entry, sizeof(struct mep));
374 me->name = strdup(me->name);
377 me->metrics = strlist__new(NULL, NULL);
388 static struct mep *mep_lookup(struct rblist *groups, const char *name)
394 nd = rblist__find(groups, &me);
396 return container_of(nd, struct mep, nd);
397 rblist__add_node(groups, &me);
398 nd = rblist__find(groups, &me);
400 return container_of(nd, struct mep, nd);
404 static void mep_delete(struct rblist *rl __maybe_unused,
407 struct mep *me = container_of(nd, struct mep, nd);
409 strlist__delete(me->metrics);
414 static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
419 strlist__for_each_entry (sn, metrics) {
421 printf("%s%s", n > 0 ? " " : "", sn->s);
423 printf(" %s\n", sn->s);
430 static int metricgroup__print_pmu_event(const struct pmu_event *pe,
431 bool metricgroups, char *filter,
432 bool raw, bool details,
433 struct rblist *groups,
434 struct strlist *metriclist)
439 g = pe->metric_group;
440 if (!g && pe->metric_name) {
454 while ((g = strsep(&mg, ";")) != NULL) {
461 if (filter && !strstr(g, filter))
464 s = (char *)pe->metric_name;
466 if (asprintf(&s, "%s\n%*s%s]",
467 pe->metric_name, 8, "[", pe->desc) < 0)
470 if (asprintf(&s, "%s\n%*s%s]",
471 s, 8, "[", pe->metric_expr) < 0)
480 strlist__add(metriclist, s);
482 me = mep_lookup(groups, g);
485 strlist__add(me->metrics, s);
496 struct metricgroup_print_sys_idata {
497 struct strlist *metriclist;
499 struct rblist *groups;
505 struct metricgroup_iter_data {
506 pmu_event_iter_fn fn;
510 static int metricgroup__sys_event_iter(const struct pmu_event *pe,
511 const struct pmu_events_table *table,
514 struct metricgroup_iter_data *d = data;
515 struct perf_pmu *pmu = NULL;
517 if (!pe->metric_expr || !pe->compat)
520 while ((pmu = perf_pmu__scan(pmu))) {
522 if (!pmu->id || strcmp(pmu->id, pe->compat))
525 return d->fn(pe, table, d->data);
531 static int metricgroup__print_sys_event_iter(const struct pmu_event *pe,
532 const struct pmu_events_table *table __maybe_unused,
535 struct metricgroup_print_sys_idata *d = data;
537 return metricgroup__print_pmu_event(pe, d->metricgroups, d->filter, d->raw,
538 d->details, d->groups, d->metriclist);
541 struct metricgroup_print_data {
542 const char *pmu_name;
543 struct strlist *metriclist;
545 struct rblist *groups;
551 static int metricgroup__print_callback(const struct pmu_event *pe,
552 const struct pmu_events_table *table __maybe_unused,
555 struct metricgroup_print_data *data = vdata;
557 if (!pe->metric_expr)
560 if (data->pmu_name && perf_pmu__is_hybrid(pe->pmu) && strcmp(data->pmu_name, pe->pmu))
563 return metricgroup__print_pmu_event(pe, data->metricgroups, data->filter,
564 data->raw, data->details, data->groups,
568 void metricgroup__print(bool metrics, bool metricgroups, char *filter,
569 bool raw, bool details, const char *pmu_name)
571 struct rblist groups;
572 struct rb_node *node, *next;
573 struct strlist *metriclist = NULL;
574 const struct pmu_events_table *table;
577 metriclist = strlist__new(NULL, NULL);
582 rblist__init(&groups);
583 groups.node_new = mep_new;
584 groups.node_cmp = mep_cmp;
585 groups.node_delete = mep_delete;
586 table = pmu_events_table__find();
588 struct metricgroup_print_data data = {
589 .pmu_name = pmu_name,
590 .metriclist = metriclist,
591 .metricgroups = metricgroups,
598 pmu_events_table_for_each_event(table,
599 metricgroup__print_callback,
603 struct metricgroup_iter_data data = {
604 .fn = metricgroup__print_sys_event_iter,
605 .data = (void *) &(struct metricgroup_print_sys_idata){
606 .metriclist = metriclist,
607 .metricgroups = metricgroups,
615 pmu_for_each_sys_event(metricgroup__sys_event_iter, &data);
618 if (!filter || !rblist__empty(&groups)) {
619 if (metricgroups && !raw)
620 printf("\nMetric Groups:\n\n");
621 else if (metrics && !raw)
622 printf("\nMetrics:\n\n");
625 for (node = rb_first_cached(&groups.entries); node; node = next) {
626 struct mep *me = container_of(node, struct mep, nd);
629 printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
631 metricgroup__print_strlist(me->metrics, raw);
632 next = rb_next(node);
633 rblist__remove_node(&groups, node);
636 metricgroup__print_strlist(metriclist, raw);
637 strlist__delete(metriclist);
640 static const char *code_characters = ",-=@";
642 static int encode_metric_id(struct strbuf *sb, const char *x)
648 c = strchr(code_characters, *x);
650 ret = strbuf_addch(sb, '!');
654 ret = strbuf_addch(sb, '0' + (c - code_characters));
658 ret = strbuf_addch(sb, *x);
666 static int decode_metric_id(struct strbuf *sb, const char *x)
668 const char *orig = x;
678 if (i > strlen(code_characters)) {
679 pr_err("Bad metric-id encoding in: '%s'", orig);
682 c = code_characters[i];
684 ret = strbuf_addch(sb, c);
691 static int decode_all_metric_ids(struct evlist *perf_evlist, const char *modifier)
694 struct strbuf sb = STRBUF_INIT;
698 evlist__for_each_entry(perf_evlist, ev) {
702 ret = strbuf_setlen(&sb, 0);
706 ret = decode_metric_id(&sb, ev->metric_id);
710 free((char *)ev->metric_id);
711 ev->metric_id = strdup(sb.buf);
712 if (!ev->metric_id) {
717 * If the name is just the parsed event, use the metric-id to
718 * give a more friendly display version.
720 if (strstr(ev->name, "metric-id=")) {
721 bool has_slash = false;
724 for (cur = strchr(sb.buf, '@') ; cur; cur = strchr(++cur, '@')) {
730 if (!has_slash && !strchr(sb.buf, ':')) {
731 ret = strbuf_addch(&sb, ':');
735 ret = strbuf_addstr(&sb, modifier);
739 ev->name = strdup(sb.buf);
750 static int metricgroup__build_event_string(struct strbuf *events,
751 const struct expr_parse_ctx *ctx,
752 const char *modifier,
755 struct hashmap_entry *cur;
757 bool no_group = true, has_tool_events = false;
758 bool tool_events[PERF_TOOL_MAX] = {false};
761 #define RETURN_IF_NON_ZERO(x) do { if (x) return x; } while (0)
763 hashmap__for_each_entry(ctx->ids, cur, bkt) {
764 const char *sep, *rsep, *id = cur->key;
765 enum perf_tool_event ev;
767 pr_debug("found event %s\n", id);
769 /* Always move tool events outside of the group. */
770 ev = perf_tool_event__from_str(id);
771 if (ev != PERF_TOOL_NONE) {
772 has_tool_events = true;
773 tool_events[ev] = true;
776 /* Separate events with commas and open the group if necessary. */
778 if (!has_constraint) {
779 ret = strbuf_addch(events, '{');
780 RETURN_IF_NON_ZERO(ret);
785 ret = strbuf_addch(events, ',');
786 RETURN_IF_NON_ZERO(ret);
789 * Encode the ID as an event string. Add a qualifier for
790 * metric_id that is the original name except with characters
791 * that parse-events can't parse replaced. For example,
792 * 'msr@tsc@' gets added as msr/tsc,metric-id=msr!3tsc!3/
794 sep = strchr(id, '@');
796 ret = strbuf_add(events, id, sep - id);
797 RETURN_IF_NON_ZERO(ret);
798 ret = strbuf_addch(events, '/');
799 RETURN_IF_NON_ZERO(ret);
800 rsep = strrchr(sep, '@');
801 ret = strbuf_add(events, sep + 1, rsep - sep - 1);
802 RETURN_IF_NON_ZERO(ret);
803 ret = strbuf_addstr(events, ",metric-id=");
804 RETURN_IF_NON_ZERO(ret);
807 sep = strchr(id, ':');
809 ret = strbuf_add(events, id, sep - id);
810 RETURN_IF_NON_ZERO(ret);
812 ret = strbuf_addstr(events, id);
813 RETURN_IF_NON_ZERO(ret);
815 ret = strbuf_addstr(events, "/metric-id=");
816 RETURN_IF_NON_ZERO(ret);
818 ret = encode_metric_id(events, id);
819 RETURN_IF_NON_ZERO(ret);
820 ret = strbuf_addstr(events, "/");
821 RETURN_IF_NON_ZERO(ret);
824 ret = strbuf_addstr(events, sep + 1);
825 RETURN_IF_NON_ZERO(ret);
828 ret = strbuf_addstr(events, modifier);
829 RETURN_IF_NON_ZERO(ret);
832 if (!no_group && !has_constraint) {
833 ret = strbuf_addf(events, "}:W");
834 RETURN_IF_NON_ZERO(ret);
836 if (has_tool_events) {
839 perf_tool_event__for_each_event(i) {
840 if (tool_events[i]) {
842 ret = strbuf_addch(events, ',');
843 RETURN_IF_NON_ZERO(ret);
846 ret = strbuf_addstr(events, perf_tool_event__to_str(i));
847 RETURN_IF_NON_ZERO(ret);
853 #undef RETURN_IF_NON_ZERO
856 int __weak arch_get_runtimeparam(const struct pmu_event *pe __maybe_unused)
862 * A singly linked list on the stack of the names of metrics being
863 * processed. Used to identify recursion.
865 struct visited_metric {
867 const struct visited_metric *parent;
870 struct metricgroup_add_iter_data {
871 struct list_head *metric_list;
872 const char *metric_name;
873 const char *modifier;
876 bool metric_no_group;
877 struct metric *root_metric;
878 const struct visited_metric *visited;
879 const struct pmu_events_table *table;
882 static int add_metric(struct list_head *metric_list,
883 const struct pmu_event *pe,
884 const char *modifier,
885 bool metric_no_group,
886 struct metric *root_metric,
887 const struct visited_metric *visited,
888 const struct pmu_events_table *table);
891 * resolve_metric - Locate metrics within the root metric and recursively add
892 * references to them.
893 * @metric_list: The list the metric is added to.
894 * @modifier: if non-null event modifiers like "u".
895 * @metric_no_group: Should events written to events be grouped "{}" or
896 * global. Grouping is the default but due to multiplexing the
898 * @root_metric: Metrics may reference other metrics to form a tree. In this
899 * case the root_metric holds all the IDs and a list of referenced
900 * metrics. When adding a root this argument is NULL.
901 * @visited: A singly linked list of metric names being added that is used to
903 * @table: The table that is searched for metrics, most commonly the table for the
904 * architecture perf is running upon.
906 static int resolve_metric(struct list_head *metric_list,
907 const char *modifier,
908 bool metric_no_group,
909 struct metric *root_metric,
910 const struct visited_metric *visited,
911 const struct pmu_events_table *table)
913 struct hashmap_entry *cur;
916 /* The metric to resolve. */
917 const struct pmu_event *pe;
919 * The key in the IDs map, this may differ from in case,
920 * etc. from pe->metric_name.
924 int i, ret = 0, pending_cnt = 0;
927 * Iterate all the parsed IDs and if there's a matching metric and it to
930 hashmap__for_each_entry(root_metric->pctx->ids, cur, bkt) {
931 const struct pmu_event *pe;
933 pe = metricgroup__find_metric(cur->key, table);
935 pending = realloc(pending,
936 (pending_cnt + 1) * sizeof(struct to_resolve));
940 pending[pending_cnt].pe = pe;
941 pending[pending_cnt].key = cur->key;
946 /* Remove the metric IDs from the context. */
947 for (i = 0; i < pending_cnt; i++)
948 expr__del_id(root_metric->pctx, pending[i].key);
951 * Recursively add all the metrics, IDs are added to the root metric's
954 for (i = 0; i < pending_cnt; i++) {
955 ret = add_metric(metric_list, pending[i].pe, modifier, metric_no_group,
956 root_metric, visited, table);
966 * __add_metric - Add a metric to metric_list.
967 * @metric_list: The list the metric is added to.
968 * @pe: The pmu_event containing the metric to be added.
969 * @modifier: if non-null event modifiers like "u".
970 * @metric_no_group: Should events written to events be grouped "{}" or
971 * global. Grouping is the default but due to multiplexing the
973 * @runtime: A special argument for the parser only known at runtime.
974 * @root_metric: Metrics may reference other metrics to form a tree. In this
975 * case the root_metric holds all the IDs and a list of referenced
976 * metrics. When adding a root this argument is NULL.
977 * @visited: A singly linked list of metric names being added that is used to
979 * @table: The table that is searched for metrics, most commonly the table for the
980 * architecture perf is running upon.
982 static int __add_metric(struct list_head *metric_list,
983 const struct pmu_event *pe,
984 const char *modifier,
985 bool metric_no_group,
987 struct metric *root_metric,
988 const struct visited_metric *visited,
989 const struct pmu_events_table *table)
991 const struct visited_metric *vm;
993 bool is_root = !root_metric;
994 struct visited_metric visited_node = {
995 .name = pe->metric_name,
999 for (vm = visited; vm; vm = vm->parent) {
1000 if (!strcmp(pe->metric_name, vm->name)) {
1001 pr_err("failed: recursion detected for %s\n", pe->metric_name);
1008 * This metric is the root of a tree and may reference other
1009 * metrics that are added recursively.
1011 root_metric = metric__new(pe, modifier, metric_no_group, runtime);
1019 * This metric was referenced in a metric higher in the
1020 * tree. Check if the same metric is already resolved in the
1023 if (root_metric->metric_refs) {
1024 for (; root_metric->metric_refs[cnt].metric_name; cnt++) {
1025 if (!strcmp(pe->metric_name,
1026 root_metric->metric_refs[cnt].metric_name))
1031 /* Create reference. Need space for the entry and the terminator. */
1032 root_metric->metric_refs = realloc(root_metric->metric_refs,
1033 (cnt + 2) * sizeof(struct metric_ref));
1034 if (!root_metric->metric_refs)
1038 * Intentionally passing just const char pointers,
1039 * from 'pe' object, so they never go away. We don't
1040 * need to change them, so there's no need to create
1043 root_metric->metric_refs[cnt].metric_name = pe->metric_name;
1044 root_metric->metric_refs[cnt].metric_expr = pe->metric_expr;
1046 /* Null terminate array. */
1047 root_metric->metric_refs[cnt+1].metric_name = NULL;
1048 root_metric->metric_refs[cnt+1].metric_expr = NULL;
1052 * For both the parent and referenced metrics, we parse
1053 * all the metric's IDs and add it to the root context.
1055 if (expr__find_ids(pe->metric_expr, NULL, root_metric->pctx) < 0) {
1056 /* Broken metric. */
1059 /* Resolve referenced metrics. */
1060 ret = resolve_metric(metric_list, modifier, metric_no_group, root_metric,
1061 &visited_node, table);
1066 metric__free(root_metric);
1069 list_add(&root_metric->nd, metric_list);
1074 struct metricgroup__find_metric_data {
1076 const struct pmu_event *pe;
1079 static int metricgroup__find_metric_callback(const struct pmu_event *pe,
1080 const struct pmu_events_table *table __maybe_unused,
1083 struct metricgroup__find_metric_data *data = vdata;
1085 if (!match_metric(pe->metric_name, data->metric))
1092 const struct pmu_event *metricgroup__find_metric(const char *metric,
1093 const struct pmu_events_table *table)
1095 struct metricgroup__find_metric_data data = {
1100 pmu_events_table_for_each_event(table, metricgroup__find_metric_callback, &data);
1105 static int add_metric(struct list_head *metric_list,
1106 const struct pmu_event *pe,
1107 const char *modifier,
1108 bool metric_no_group,
1109 struct metric *root_metric,
1110 const struct visited_metric *visited,
1111 const struct pmu_events_table *table)
1115 pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
1117 if (!strstr(pe->metric_expr, "?")) {
1118 ret = __add_metric(metric_list, pe, modifier, metric_no_group, 0,
1119 root_metric, visited, table);
1123 count = arch_get_runtimeparam(pe);
1125 /* This loop is added to create multiple
1126 * events depend on count value and add
1127 * those events to metric_list.
1130 for (j = 0; j < count && !ret; j++)
1131 ret = __add_metric(metric_list, pe, modifier, metric_no_group, j,
1132 root_metric, visited, table);
1138 static int metricgroup__add_metric_sys_event_iter(const struct pmu_event *pe,
1139 const struct pmu_events_table *table __maybe_unused,
1142 struct metricgroup_add_iter_data *d = data;
1145 if (!match_pe_metric(pe, d->metric_name))
1148 ret = add_metric(d->metric_list, pe, d->modifier, d->metric_no_group,
1149 d->root_metric, d->visited, d->table);
1153 *(d->has_match) = true;
1161 * metric_list_cmp - list_sort comparator that sorts metrics with more events to
1162 * the front. tool events are excluded from the count.
1164 static int metric_list_cmp(void *priv __maybe_unused, const struct list_head *l,
1165 const struct list_head *r)
1167 const struct metric *left = container_of(l, struct metric, nd);
1168 const struct metric *right = container_of(r, struct metric, nd);
1169 struct expr_id_data *data;
1170 int i, left_count, right_count;
1172 left_count = hashmap__size(left->pctx->ids);
1173 perf_tool_event__for_each_event(i) {
1174 if (!expr__get_id(left->pctx, perf_tool_event__to_str(i), &data))
1178 right_count = hashmap__size(right->pctx->ids);
1179 perf_tool_event__for_each_event(i) {
1180 if (!expr__get_id(right->pctx, perf_tool_event__to_str(i), &data))
1184 return right_count - left_count;
1187 struct metricgroup__add_metric_data {
1188 struct list_head *list;
1189 const char *metric_name;
1190 const char *modifier;
1191 bool metric_no_group;
1195 static int metricgroup__add_metric_callback(const struct pmu_event *pe,
1196 const struct pmu_events_table *table,
1199 struct metricgroup__add_metric_data *data = vdata;
1202 if (pe->metric_expr &&
1203 (match_metric(pe->metric_group, data->metric_name) ||
1204 match_metric(pe->metric_name, data->metric_name))) {
1206 data->has_match = true;
1207 ret = add_metric(data->list, pe, data->modifier, data->metric_no_group,
1208 /*root_metric=*/NULL,
1209 /*visited_metrics=*/NULL, table);
1215 * metricgroup__add_metric - Find and add a metric, or a metric group.
1216 * @metric_name: The name of the metric or metric group. For example, "IPC"
1217 * could be the name of a metric and "TopDownL1" the name of a
1219 * @modifier: if non-null event modifiers like "u".
1220 * @metric_no_group: Should events written to events be grouped "{}" or
1221 * global. Grouping is the default but due to multiplexing the
1222 * user may override.
1223 * @metric_list: The list that the metric or metric group are added to.
1224 * @table: The table that is searched for metrics, most commonly the table for the
1225 * architecture perf is running upon.
1227 static int metricgroup__add_metric(const char *metric_name, const char *modifier,
1228 bool metric_no_group,
1229 struct list_head *metric_list,
1230 const struct pmu_events_table *table)
1234 bool has_match = false;
1237 struct metricgroup__add_metric_data data = {
1239 .metric_name = metric_name,
1240 .modifier = modifier,
1241 .metric_no_group = metric_no_group,
1245 * Iterate over all metrics seeing if metric matches either the
1246 * name or group. When it does add the metric to the list.
1248 ret = pmu_events_table_for_each_event(table, metricgroup__add_metric_callback,
1253 has_match = data.has_match;
1256 struct metricgroup_iter_data data = {
1257 .fn = metricgroup__add_metric_sys_event_iter,
1258 .data = (void *) &(struct metricgroup_add_iter_data) {
1259 .metric_list = &list,
1260 .metric_name = metric_name,
1261 .modifier = modifier,
1262 .metric_no_group = metric_no_group,
1263 .has_match = &has_match,
1269 pmu_for_each_sys_event(metricgroup__sys_event_iter, &data);
1271 /* End of pmu events. */
1277 * add to metric_list so that they can be released
1278 * even if it's failed
1280 list_splice(&list, metric_list);
1285 * metricgroup__add_metric_list - Find and add metrics, or metric groups,
1286 * specified in a list.
1287 * @list: the list of metrics or metric groups. For example, "IPC,CPI,TopDownL1"
1288 * would match the IPC and CPI metrics, and TopDownL1 would match all
1289 * the metrics in the TopDownL1 group.
1290 * @metric_no_group: Should events written to events be grouped "{}" or
1291 * global. Grouping is the default but due to multiplexing the
1292 * user may override.
1293 * @metric_list: The list that metrics are added to.
1294 * @table: The table that is searched for metrics, most commonly the table for the
1295 * architecture perf is running upon.
1297 static int metricgroup__add_metric_list(const char *list, bool metric_no_group,
1298 struct list_head *metric_list,
1299 const struct pmu_events_table *table)
1301 char *list_itr, *list_copy, *metric_name, *modifier;
1304 list_copy = strdup(list);
1307 list_itr = list_copy;
1309 while ((metric_name = strsep(&list_itr, ",")) != NULL) {
1310 modifier = strchr(metric_name, ':');
1314 ret = metricgroup__add_metric(metric_name, modifier,
1315 metric_no_group, metric_list,
1318 pr_err("Cannot find metric or group `%s'\n", metric_name);
1329 * Warn about nmi_watchdog if any parsed metrics had the
1330 * NO_NMI_WATCHDOG constraint.
1332 metricgroup___watchdog_constraint_hint(NULL, true);
1340 static void metricgroup__free_metrics(struct list_head *metric_list)
1342 struct metric *m, *tmp;
1344 list_for_each_entry_safe (m, tmp, metric_list, nd) {
1345 list_del_init(&m->nd);
1351 * find_tool_events - Search for the pressence of tool events in metric_list.
1352 * @metric_list: List to take metrics from.
1353 * @tool_events: Array of false values, indices corresponding to tool events set
1354 * to true if tool event is found.
1356 static void find_tool_events(const struct list_head *metric_list,
1357 bool tool_events[PERF_TOOL_MAX])
1361 list_for_each_entry(m, metric_list, nd) {
1364 perf_tool_event__for_each_event(i) {
1365 struct expr_id_data *data;
1367 if (!tool_events[i] &&
1368 !expr__get_id(m->pctx, perf_tool_event__to_str(i), &data))
1369 tool_events[i] = true;
1375 * build_combined_expr_ctx - Make an expr_parse_ctx with all has_constraint
1376 * metric IDs, as the IDs are held in a set,
1377 * duplicates will be removed.
1378 * @metric_list: List to take metrics from.
1379 * @combined: Out argument for result.
1381 static int build_combined_expr_ctx(const struct list_head *metric_list,
1382 struct expr_parse_ctx **combined)
1384 struct hashmap_entry *cur;
1390 *combined = expr__ctx_new();
1394 list_for_each_entry(m, metric_list, nd) {
1395 if (m->has_constraint && !m->modifier) {
1396 hashmap__for_each_entry(m->pctx->ids, cur, bkt) {
1397 dup = strdup(cur->key);
1402 ret = expr__add_id(*combined, dup);
1410 expr__ctx_free(*combined);
1416 * parse_ids - Build the event string for the ids and parse them creating an
1417 * evlist. The encoded metric_ids are decoded.
1418 * @metric_no_merge: is metric sharing explicitly disabled.
1419 * @fake_pmu: used when testing metrics not supported by the current CPU.
1420 * @ids: the event identifiers parsed from a metric.
1421 * @modifier: any modifiers added to the events.
1422 * @has_constraint: false if events should be placed in a weak group.
1423 * @tool_events: entries set true if the tool event of index could be present in
1424 * the overall list of metrics.
1425 * @out_evlist: the created list of events.
1427 static int parse_ids(bool metric_no_merge, struct perf_pmu *fake_pmu,
1428 struct expr_parse_ctx *ids, const char *modifier,
1429 bool has_constraint, const bool tool_events[PERF_TOOL_MAX],
1430 struct evlist **out_evlist)
1432 struct parse_events_error parse_error;
1433 struct evlist *parsed_evlist;
1434 struct strbuf events = STRBUF_INIT;
1438 if (!metric_no_merge || hashmap__size(ids->ids) == 0) {
1439 bool added_event = false;
1442 * We may fail to share events between metrics because a tool
1443 * event isn't present in one metric. For example, a ratio of
1444 * cache misses doesn't need duration_time but the same events
1445 * may be used for a misses per second. Events without sharing
1446 * implies multiplexing, that is best avoided, so place
1447 * all tool events in every group.
1449 * Also, there may be no ids/events in the expression parsing
1450 * context because of constant evaluation, e.g.:
1451 * event1 if #smt_on else 0
1452 * Add a tool event to avoid a parse error on an empty string.
1454 perf_tool_event__for_each_event(i) {
1455 if (tool_events[i]) {
1456 char *tmp = strdup(perf_tool_event__to_str(i));
1460 ids__insert(ids->ids, tmp);
1464 if (!added_event && hashmap__size(ids->ids) == 0) {
1465 char *tmp = strdup("duration_time");
1469 ids__insert(ids->ids, tmp);
1472 ret = metricgroup__build_event_string(&events, ids, modifier,
1477 parsed_evlist = evlist__new();
1478 if (!parsed_evlist) {
1482 pr_debug("Parsing metric events '%s'\n", events.buf);
1483 parse_events_error__init(&parse_error);
1484 ret = __parse_events(parsed_evlist, events.buf, &parse_error, fake_pmu);
1486 parse_events_error__print(&parse_error, events.buf);
1489 ret = decode_all_metric_ids(parsed_evlist, modifier);
1493 *out_evlist = parsed_evlist;
1494 parsed_evlist = NULL;
1496 parse_events_error__exit(&parse_error);
1497 evlist__delete(parsed_evlist);
1498 strbuf_release(&events);
1502 static int parse_groups(struct evlist *perf_evlist, const char *str,
1503 bool metric_no_group,
1504 bool metric_no_merge,
1505 struct perf_pmu *fake_pmu,
1506 struct rblist *metric_events_list,
1507 const struct pmu_events_table *table)
1509 struct evlist *combined_evlist = NULL;
1510 LIST_HEAD(metric_list);
1512 bool tool_events[PERF_TOOL_MAX] = {false};
1515 if (metric_events_list->nr_entries == 0)
1516 metricgroup__rblist_init(metric_events_list);
1517 ret = metricgroup__add_metric_list(str, metric_no_group,
1518 &metric_list, table);
1522 /* Sort metrics from largest to smallest. */
1523 list_sort(NULL, &metric_list, metric_list_cmp);
1525 if (!metric_no_merge) {
1526 struct expr_parse_ctx *combined = NULL;
1528 find_tool_events(&metric_list, tool_events);
1530 ret = build_combined_expr_ctx(&metric_list, &combined);
1532 if (!ret && combined && hashmap__size(combined->ids)) {
1533 ret = parse_ids(metric_no_merge, fake_pmu, combined,
1535 /*has_constraint=*/true,
1540 expr__ctx_free(combined);
1546 list_for_each_entry(m, &metric_list, nd) {
1547 struct metric_event *me;
1548 struct evsel **metric_events;
1549 struct evlist *metric_evlist = NULL;
1551 struct metric_expr *expr;
1553 if (combined_evlist && m->has_constraint) {
1554 metric_evlist = combined_evlist;
1555 } else if (!metric_no_merge) {
1557 * See if the IDs for this metric are a subset of an
1560 list_for_each_entry(n, &metric_list, nd) {
1564 if (n->evlist == NULL)
1567 if ((!m->modifier && n->modifier) ||
1568 (m->modifier && !n->modifier) ||
1569 (m->modifier && n->modifier &&
1570 strcmp(m->modifier, n->modifier)))
1573 if (expr__subset_of_ids(n->pctx, m->pctx)) {
1574 pr_debug("Events in '%s' fully contained within '%s'\n",
1575 m->metric_name, n->metric_name);
1576 metric_evlist = n->evlist;
1582 if (!metric_evlist) {
1583 ret = parse_ids(metric_no_merge, fake_pmu, m->pctx, m->modifier,
1584 m->has_constraint, tool_events, &m->evlist);
1588 metric_evlist = m->evlist;
1590 ret = setup_metric_events(m->pctx->ids, metric_evlist, &metric_events);
1592 pr_debug("Cannot resolve IDs for %s: %s\n",
1593 m->metric_name, m->metric_expr);
1597 me = metricgroup__lookup(metric_events_list, metric_events[0], true);
1599 expr = malloc(sizeof(struct metric_expr));
1602 free(metric_events);
1606 expr->metric_refs = m->metric_refs;
1607 m->metric_refs = NULL;
1608 expr->metric_expr = m->metric_expr;
1612 if (asprintf(&tmp, "%s:%s", m->metric_name, m->modifier) < 0)
1613 expr->metric_name = NULL;
1615 expr->metric_name = tmp;
1617 expr->metric_name = strdup(m->metric_name);
1619 if (!expr->metric_name) {
1621 free(metric_events);
1624 expr->metric_unit = m->metric_unit;
1625 expr->metric_events = metric_events;
1626 expr->runtime = m->pctx->runtime;
1627 list_add(&expr->nd, &me->head);
1631 if (combined_evlist) {
1632 evlist__splice_list_tail(perf_evlist, &combined_evlist->core.entries);
1633 evlist__delete(combined_evlist);
1636 list_for_each_entry(m, &metric_list, nd) {
1638 evlist__splice_list_tail(perf_evlist, &m->evlist->core.entries);
1642 metricgroup__free_metrics(&metric_list);
1646 int metricgroup__parse_groups(const struct option *opt,
1648 bool metric_no_group,
1649 bool metric_no_merge,
1650 struct rblist *metric_events)
1652 struct evlist *perf_evlist = *(struct evlist **)opt->value;
1653 const struct pmu_events_table *table = pmu_events_table__find();
1655 return parse_groups(perf_evlist, str, metric_no_group,
1656 metric_no_merge, NULL, metric_events, table);
1659 int metricgroup__parse_groups_test(struct evlist *evlist,
1660 const struct pmu_events_table *table,
1662 bool metric_no_group,
1663 bool metric_no_merge,
1664 struct rblist *metric_events)
1666 return parse_groups(evlist, str, metric_no_group,
1667 metric_no_merge, &perf_pmu__fake, metric_events, table);
1670 static int metricgroup__has_metric_callback(const struct pmu_event *pe,
1671 const struct pmu_events_table *table __maybe_unused,
1674 const char *metric = vdata;
1676 if (!pe->metric_expr)
1679 if (match_metric(pe->metric_name, metric))
1685 bool metricgroup__has_metric(const char *metric)
1687 const struct pmu_events_table *table = pmu_events_table__find();
1692 return pmu_events_table_for_each_event(table, metricgroup__has_metric_callback,
1693 (void *)metric) ? true : false;
1696 int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
1697 struct rblist *new_metric_events,
1698 struct rblist *old_metric_events)
1702 for (i = 0; i < rblist__nr_entries(old_metric_events); i++) {
1704 struct metric_event *old_me, *new_me;
1705 struct metric_expr *old_expr, *new_expr;
1706 struct evsel *evsel;
1710 nd = rblist__entry(old_metric_events, i);
1711 old_me = container_of(nd, struct metric_event, nd);
1713 evsel = evlist__find_evsel(evlist, old_me->evsel->core.idx);
1716 new_me = metricgroup__lookup(new_metric_events, evsel, true);
1720 pr_debug("copying metric event for cgroup '%s': %s (idx=%d)\n",
1721 cgrp ? cgrp->name : "root", evsel->name, evsel->core.idx);
1723 list_for_each_entry(old_expr, &old_me->head, nd) {
1724 new_expr = malloc(sizeof(*new_expr));
1728 new_expr->metric_expr = old_expr->metric_expr;
1729 new_expr->metric_name = strdup(old_expr->metric_name);
1730 if (!new_expr->metric_name)
1733 new_expr->metric_unit = old_expr->metric_unit;
1734 new_expr->runtime = old_expr->runtime;
1736 if (old_expr->metric_refs) {
1737 /* calculate number of metric_events */
1738 for (nr = 0; old_expr->metric_refs[nr].metric_name; nr++)
1740 alloc_size = sizeof(*new_expr->metric_refs);
1741 new_expr->metric_refs = calloc(nr + 1, alloc_size);
1742 if (!new_expr->metric_refs) {
1747 memcpy(new_expr->metric_refs, old_expr->metric_refs,
1750 new_expr->metric_refs = NULL;
1753 /* calculate number of metric_events */
1754 for (nr = 0; old_expr->metric_events[nr]; nr++)
1756 alloc_size = sizeof(*new_expr->metric_events);
1757 new_expr->metric_events = calloc(nr + 1, alloc_size);
1758 if (!new_expr->metric_events) {
1759 free(new_expr->metric_refs);
1764 /* copy evsel in the same position */
1765 for (idx = 0; idx < nr; idx++) {
1766 evsel = old_expr->metric_events[idx];
1767 evsel = evlist__find_evsel(evlist, evsel->core.idx);
1768 if (evsel == NULL) {
1769 free(new_expr->metric_events);
1770 free(new_expr->metric_refs);
1774 new_expr->metric_events[idx] = evsel;
1777 list_add(&new_expr->nd, &new_me->head);