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"
18 #include "pmu-events/pmu-events.h"
21 #include <linux/ctype.h>
22 #include <linux/string.h>
23 #include <linux/zalloc.h>
24 #include <subcmd/parse-options.h>
25 #include <api/fs/fs.h>
29 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
34 struct metric_event me = {
41 nd = rblist__find(metric_events, &me);
43 return container_of(nd, struct metric_event, nd);
45 rblist__add_node(metric_events, &me);
46 nd = rblist__find(metric_events, &me);
48 return container_of(nd, struct metric_event, nd);
53 static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
55 struct metric_event *a = container_of(rb_node,
58 const struct metric_event *b = entry;
60 if (a->evsel == b->evsel)
62 if ((char *)a->evsel < (char *)b->evsel)
67 static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
70 struct metric_event *me = malloc(sizeof(struct metric_event));
74 memcpy(me, entry, sizeof(struct metric_event));
75 me->evsel = ((struct metric_event *)entry)->evsel;
76 INIT_LIST_HEAD(&me->head);
80 static void metric_event_delete(struct rblist *rblist __maybe_unused,
81 struct rb_node *rb_node)
83 struct metric_event *me = container_of(rb_node, struct metric_event, nd);
84 struct metric_expr *expr, *tmp;
86 list_for_each_entry_safe(expr, tmp, &me->head, nd) {
87 free(expr->metric_refs);
88 free(expr->metric_events);
95 static void metricgroup__rblist_init(struct rblist *metric_events)
97 rblist__init(metric_events);
98 metric_events->node_cmp = metric_event_cmp;
99 metric_events->node_new = metric_event_new;
100 metric_events->node_delete = metric_event_delete;
103 void metricgroup__rblist_exit(struct rblist *metric_events)
105 rblist__exit(metric_events);
109 * A node in the list of referenced metrics. metric_expr
110 * is held as a convenience to avoid a search through the
113 struct metric_ref_node {
114 const char *metric_name;
115 const char *metric_expr;
116 struct list_head list;
121 struct expr_parse_ctx pctx;
122 const char *metric_name;
123 const char *metric_expr;
124 const char *metric_unit;
125 struct list_head metric_refs;
131 #define RECURSION_ID_MAX 1000
134 struct expr_id id[RECURSION_ID_MAX];
138 static struct expr_id *expr_ids__alloc(struct expr_ids *ids)
140 if (ids->cnt >= RECURSION_ID_MAX)
142 return &ids->id[ids->cnt++];
145 static void expr_ids__exit(struct expr_ids *ids)
149 for (i = 0; i < ids->cnt; i++)
154 * Find a group of events in perf_evlist that correpond to those from a parsed
155 * metric expression. Note, as find_evsel_group is called in the same order as
156 * perf_evlist was constructed, metric_no_merge doesn't need to test for
157 * underfilling a group.
158 * @perf_evlist: a list of events something like: {metric1 leader, metric1
159 * sibling, metric1 sibling}:W,duration_time,{metric2 leader, metric2 sibling,
160 * metric2 sibling}:W,duration_time
161 * @pctx: the parse context for the metric expression.
162 * @metric_no_merge: don't attempt to share events for the metric with other
164 * @has_constraint: is there a contraint on the group of events? In which case
165 * the events won't be grouped.
166 * @metric_events: out argument, null terminated array of evsel's associated
168 * @evlist_used: in/out argument, bitmap tracking which evlist events are used.
169 * @return the first metric event or NULL on failure.
171 static struct evsel *find_evsel_group(struct evlist *perf_evlist,
172 struct expr_parse_ctx *pctx,
173 bool metric_no_merge,
175 struct evsel **metric_events,
176 unsigned long *evlist_used)
178 struct evsel *ev, *current_leader = NULL;
179 struct expr_id_data *val_ptr;
180 int i = 0, matched_events = 0, events_to_match;
181 const int idnum = (int)hashmap__size(&pctx->ids);
183 /* duration_time is grouped separately. */
184 if (!has_constraint &&
185 hashmap__find(&pctx->ids, "duration_time", (void **)&val_ptr))
186 events_to_match = idnum - 1;
188 events_to_match = idnum;
190 evlist__for_each_entry (perf_evlist, ev) {
192 * Events with a constraint aren't grouped and match the first
195 if (has_constraint && ev->weak_group)
197 /* Ignore event if already used and merging is disabled. */
198 if (metric_no_merge && test_bit(ev->idx, evlist_used))
200 if (!has_constraint && ev->leader != current_leader) {
202 * Start of a new group, discard the whole match and
206 memset(metric_events, 0,
207 sizeof(struct evsel *) * idnum);
208 current_leader = ev->leader;
210 if (hashmap__find(&pctx->ids, ev->name, (void **)&val_ptr)) {
211 if (has_constraint) {
213 * Events aren't grouped, ensure the same event
214 * isn't matched from two groups.
216 for (i = 0; i < matched_events; i++) {
217 if (!strcmp(ev->name,
218 metric_events[i]->name)) {
222 if (i != matched_events)
225 metric_events[matched_events++] = ev;
227 if (matched_events == events_to_match)
231 if (events_to_match != idnum) {
232 /* Add the first duration_time. */
233 evlist__for_each_entry(perf_evlist, ev) {
234 if (!strcmp(ev->name, "duration_time")) {
235 metric_events[matched_events++] = ev;
241 if (matched_events != idnum) {
242 /* Not whole match */
246 metric_events[idnum] = NULL;
248 for (i = 0; i < idnum; i++) {
249 ev = metric_events[i];
250 ev->metric_leader = ev;
251 set_bit(ev->idx, evlist_used);
254 return metric_events[0];
257 static int metricgroup__setup_events(struct list_head *groups,
258 bool metric_no_merge,
259 struct evlist *perf_evlist,
260 struct rblist *metric_events_list)
262 struct metric_event *me;
263 struct metric_expr *expr;
267 struct evsel *evsel, *tmp;
268 unsigned long *evlist_used;
270 evlist_used = bitmap_alloc(perf_evlist->core.nr_entries);
274 list_for_each_entry (m, groups, nd) {
275 struct evsel **metric_events;
276 struct metric_ref *metric_refs = NULL;
278 metric_events = calloc(sizeof(void *),
279 hashmap__size(&m->pctx.ids) + 1);
280 if (!metric_events) {
284 evsel = find_evsel_group(perf_evlist, &m->pctx,
286 m->has_constraint, metric_events,
289 pr_debug("Cannot resolve %s: %s\n",
290 m->metric_name, m->metric_expr);
294 for (i = 0; metric_events[i]; i++)
295 metric_events[i]->collect_stat = true;
296 me = metricgroup__lookup(metric_events_list, evsel, true);
302 expr = malloc(sizeof(struct metric_expr));
310 * Collect and store collected nested expressions
311 * for metric processing.
313 if (m->metric_refs_cnt) {
314 struct metric_ref_node *ref;
316 metric_refs = zalloc(sizeof(struct metric_ref) * (m->metric_refs_cnt + 1));
325 list_for_each_entry(ref, &m->metric_refs, list) {
327 * Intentionally passing just const char pointers,
328 * originally from 'struct pmu_event' object.
329 * We don't need to change them, so there's no
330 * need to create our own copy.
332 metric_refs[i].metric_name = ref->metric_name;
333 metric_refs[i].metric_expr = ref->metric_expr;
338 expr->metric_refs = metric_refs;
339 expr->metric_expr = m->metric_expr;
340 expr->metric_name = m->metric_name;
341 expr->metric_unit = m->metric_unit;
342 expr->metric_events = metric_events;
343 expr->runtime = m->runtime;
344 list_add(&expr->nd, &me->head);
347 evlist__for_each_entry_safe(perf_evlist, tmp, evsel) {
348 if (!test_bit(evsel->idx, evlist_used)) {
349 evlist__remove(perf_evlist, evsel);
350 evsel__delete(evsel);
353 bitmap_free(evlist_used);
358 static bool match_metric(const char *n, const char *list)
365 if (!strcmp(list, "all"))
368 return !strcasecmp(list, "No_group");
370 m = strcasestr(n, list);
373 if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
374 (m[len] == 0 || m[len] == ';'))
382 struct strlist *metrics;
385 static int mep_cmp(struct rb_node *rb_node, const void *entry)
387 struct mep *a = container_of(rb_node, struct mep, nd);
388 struct mep *b = (struct mep *)entry;
390 return strcmp(a->name, b->name);
393 static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
396 struct mep *me = malloc(sizeof(struct mep));
400 memcpy(me, entry, sizeof(struct mep));
401 me->name = strdup(me->name);
404 me->metrics = strlist__new(NULL, NULL);
415 static struct mep *mep_lookup(struct rblist *groups, const char *name)
421 nd = rblist__find(groups, &me);
423 return container_of(nd, struct mep, nd);
424 rblist__add_node(groups, &me);
425 nd = rblist__find(groups, &me);
427 return container_of(nd, struct mep, nd);
431 static void mep_delete(struct rblist *rl __maybe_unused,
434 struct mep *me = container_of(nd, struct mep, nd);
436 strlist__delete(me->metrics);
441 static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
446 strlist__for_each_entry (sn, metrics) {
448 printf("%s%s", n > 0 ? " " : "", sn->s);
450 printf(" %s\n", sn->s);
457 void metricgroup__print(bool metrics, bool metricgroups, char *filter,
458 bool raw, bool details)
460 struct pmu_events_map *map = perf_pmu__find_map(NULL);
461 struct pmu_event *pe;
463 struct rblist groups;
464 struct rb_node *node, *next;
465 struct strlist *metriclist = NULL;
471 metriclist = strlist__new(NULL, NULL);
476 rblist__init(&groups);
477 groups.node_new = mep_new;
478 groups.node_cmp = mep_cmp;
479 groups.node_delete = mep_delete;
484 if (!pe->name && !pe->metric_group && !pe->metric_name)
486 if (!pe->metric_expr)
488 g = pe->metric_group;
489 if (!g && pe->metric_name) {
496 char *mg = strdup(g);
501 while ((g = strsep(&mg, ";")) != NULL) {
508 if (filter && !strstr(g, filter))
511 s = (char *)pe->metric_name;
513 if (asprintf(&s, "%s\n%*s%s]",
514 pe->metric_name, 8, "[", pe->desc) < 0)
518 if (asprintf(&s, "%s\n%*s%s]",
519 s, 8, "[", pe->metric_expr) < 0)
528 strlist__add(metriclist, s);
530 me = mep_lookup(&groups, g);
533 strlist__add(me->metrics, s);
543 if (metricgroups && !raw)
544 printf("\nMetric Groups:\n\n");
545 else if (metrics && !raw)
546 printf("\nMetrics:\n\n");
548 for (node = rb_first_cached(&groups.entries); node; node = next) {
549 struct mep *me = container_of(node, struct mep, nd);
552 printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
554 metricgroup__print_strlist(me->metrics, raw);
555 next = rb_next(node);
556 rblist__remove_node(&groups, node);
559 metricgroup__print_strlist(metriclist, raw);
560 strlist__delete(metriclist);
563 static void metricgroup__add_metric_weak_group(struct strbuf *events,
564 struct expr_parse_ctx *ctx)
566 struct hashmap_entry *cur;
568 bool no_group = true, has_duration = false;
570 hashmap__for_each_entry((&ctx->ids), cur, bkt) {
571 pr_debug("found event %s\n", (const char *)cur->key);
573 * Duration time maps to a software event and can make
574 * groups not count. Always use it outside a
577 if (!strcmp(cur->key, "duration_time")) {
581 strbuf_addf(events, "%s%s",
582 no_group ? "{" : ",",
583 (const char *)cur->key);
587 strbuf_addf(events, "}:W");
589 strbuf_addf(events, ",duration_time");
590 } else if (has_duration)
591 strbuf_addf(events, "duration_time");
594 static void metricgroup__add_metric_non_group(struct strbuf *events,
595 struct expr_parse_ctx *ctx)
597 struct hashmap_entry *cur;
601 hashmap__for_each_entry((&ctx->ids), cur, bkt) {
603 strbuf_addf(events, ",");
604 strbuf_addf(events, "%s", (const char *)cur->key);
609 static void metricgroup___watchdog_constraint_hint(const char *name, bool foot)
611 static bool violate_nmi_constraint;
614 pr_warning("Splitting metric group %s into standalone metrics.\n", name);
615 violate_nmi_constraint = true;
619 if (!violate_nmi_constraint)
622 pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n"
623 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
625 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
628 static bool metricgroup__has_constraint(struct pmu_event *pe)
630 if (!pe->metric_constraint)
633 if (!strcmp(pe->metric_constraint, "NO_NMI_WATCHDOG") &&
634 sysctl__nmi_watchdog_enabled()) {
635 metricgroup___watchdog_constraint_hint(pe->metric_name, false);
642 int __weak arch_get_runtimeparam(void)
647 static int __add_metric(struct list_head *metric_list,
648 struct pmu_event *pe,
649 bool metric_no_group,
652 struct expr_id *parent,
653 struct expr_ids *ids)
655 struct metric_ref_node *ref;
660 * We got in here for the parent group,
661 * allocate it and put it on the list.
663 m = zalloc(sizeof(*m));
667 expr__ctx_init(&m->pctx);
668 m->metric_name = pe->metric_name;
669 m->metric_expr = pe->metric_expr;
670 m->metric_unit = pe->unit;
671 m->runtime = runtime;
672 m->has_constraint = metric_no_group || metricgroup__has_constraint(pe);
673 INIT_LIST_HEAD(&m->metric_refs);
674 m->metric_refs_cnt = 0;
677 parent = expr_ids__alloc(ids);
683 parent->id = strdup(pe->metric_name);
690 * We got here for the referenced metric, via the
691 * recursive metricgroup__add_metric call, add
692 * it to the parent group.
696 ref = malloc(sizeof(*ref));
701 * Intentionally passing just const char pointers,
702 * from 'pe' object, so they never go away. We don't
703 * need to change them, so there's no need to create
706 ref->metric_name = pe->metric_name;
707 ref->metric_expr = pe->metric_expr;
709 list_add(&ref->list, &m->metric_refs);
710 m->metric_refs_cnt++;
713 /* Force all found IDs in metric to have us as parent ID. */
714 WARN_ON_ONCE(!parent);
715 m->pctx.parent = parent;
718 * For both the parent and referenced metrics, we parse
719 * all the metric's IDs and add it to the parent context.
721 if (expr__find_other(pe->metric_expr, NULL, &m->pctx, runtime) < 0) {
722 expr__ctx_clear(&m->pctx);
728 * We add new group only in the 'parent' call,
729 * so bail out for referenced metric case.
731 if (m->metric_refs_cnt)
734 if (list_empty(metric_list))
735 list_add(&m->nd, metric_list);
737 struct list_head *pos;
739 /* Place the largest groups at the front. */
740 list_for_each_prev(pos, metric_list) {
741 struct metric *old = list_entry(pos, struct metric, nd);
743 if (hashmap__size(&m->pctx.ids) <=
744 hashmap__size(&old->pctx.ids))
747 list_add(&m->nd, pos);
753 #define map_for_each_event(__pe, __idx, __map) \
754 for (__idx = 0, __pe = &__map->table[__idx]; \
755 __pe->name || __pe->metric_group || __pe->metric_name; \
756 __pe = &__map->table[++__idx])
758 #define map_for_each_metric(__pe, __idx, __map, __metric) \
759 map_for_each_event(__pe, __idx, __map) \
760 if (__pe->metric_expr && \
761 (match_metric(__pe->metric_group, __metric) || \
762 match_metric(__pe->metric_name, __metric)))
764 static struct pmu_event *find_metric(const char *metric, struct pmu_events_map *map)
766 struct pmu_event *pe;
769 map_for_each_event(pe, i, map) {
770 if (match_metric(pe->metric_name, metric))
777 static int recursion_check(struct metric *m, const char *id, struct expr_id **parent,
778 struct expr_ids *ids)
780 struct expr_id_data *data;
785 * We get the parent referenced by 'id' argument and
786 * traverse through all the parent object IDs to check
787 * if we already processed 'id', if we did, it's recursion
790 ret = expr__get_id(&m->pctx, id, &data);
797 if (!strcmp(p->id, id)) {
798 pr_err("failed: recursion detected for %s\n", id);
805 * If we are over the limit of static entris, the metric
806 * is too difficult/nested to process, fail as well.
808 p = expr_ids__alloc(ids);
810 pr_err("failed: too many nested metrics\n");
815 p->parent = data->parent;
818 return p->id ? 0 : -ENOMEM;
821 static int add_metric(struct list_head *metric_list,
822 struct pmu_event *pe,
823 bool metric_no_group,
825 struct expr_id *parent,
826 struct expr_ids *ids);
828 static int __resolve_metric(struct metric *m,
829 bool metric_no_group,
830 struct list_head *metric_list,
831 struct pmu_events_map *map,
832 struct expr_ids *ids)
834 struct hashmap_entry *cur;
840 * Iterate all the parsed IDs and if there's metric,
841 * add it to the context.
845 hashmap__for_each_entry((&m->pctx.ids), cur, bkt) {
846 struct expr_id *parent;
847 struct pmu_event *pe;
849 pe = find_metric(cur->key, map);
853 ret = recursion_check(m, cur->key, &parent, ids);
858 /* The metric key itself needs to go out.. */
859 expr__del_id(&m->pctx, cur->key);
861 /* ... and it gets resolved to the parent context. */
862 ret = add_metric(metric_list, pe, metric_no_group, &m, parent, ids);
867 * We added new metric to hashmap, so we need
868 * to break the iteration and start over.
877 static int resolve_metric(bool metric_no_group,
878 struct list_head *metric_list,
879 struct pmu_events_map *map,
880 struct expr_ids *ids)
885 list_for_each_entry(m, metric_list, nd) {
886 err = __resolve_metric(m, metric_no_group, metric_list, map, ids);
893 static int add_metric(struct list_head *metric_list,
894 struct pmu_event *pe,
895 bool metric_no_group,
897 struct expr_id *parent,
898 struct expr_ids *ids)
900 struct metric *orig = *m;
903 pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
905 if (!strstr(pe->metric_expr, "?")) {
906 ret = __add_metric(metric_list, pe, metric_no_group, 1, m, parent, ids);
910 count = arch_get_runtimeparam();
912 /* This loop is added to create multiple
913 * events depend on count value and add
914 * those events to metric_list.
917 for (j = 0; j < count && !ret; j++, *m = orig)
918 ret = __add_metric(metric_list, pe, metric_no_group, j, m, parent, ids);
924 static int metricgroup__add_metric(const char *metric, bool metric_no_group,
925 struct strbuf *events,
926 struct list_head *metric_list,
927 struct pmu_events_map *map)
929 struct expr_ids ids = { .cnt = 0, };
930 struct pmu_event *pe;
934 bool has_match = false;
936 map_for_each_metric(pe, i, map, metric) {
940 ret = add_metric(&list, pe, metric_no_group, &m, NULL, &ids);
945 * Process any possible referenced metrics
946 * included in the expression.
948 ret = resolve_metric(metric_no_group,
954 /* End of pmu events. */
960 list_for_each_entry(m, &list, nd) {
962 strbuf_addf(events, ",");
964 if (m->has_constraint) {
965 metricgroup__add_metric_non_group(events,
968 metricgroup__add_metric_weak_group(events,
975 * add to metric_list so that they can be released
976 * even if it's failed
978 list_splice(&list, metric_list);
979 expr_ids__exit(&ids);
983 static int metricgroup__add_metric_list(const char *list, bool metric_no_group,
984 struct strbuf *events,
985 struct list_head *metric_list,
986 struct pmu_events_map *map)
988 char *llist, *nlist, *p;
991 nlist = strdup(list);
996 strbuf_init(events, 100);
997 strbuf_addf(events, "%s", "");
999 while ((p = strsep(&llist, ",")) != NULL) {
1000 ret = metricgroup__add_metric(p, metric_no_group, events,
1002 if (ret == -EINVAL) {
1003 fprintf(stderr, "Cannot find metric or group `%s'\n",
1011 metricgroup___watchdog_constraint_hint(NULL, true);
1016 static void metric__free_refs(struct metric *metric)
1018 struct metric_ref_node *ref, *tmp;
1020 list_for_each_entry_safe(ref, tmp, &metric->metric_refs, list) {
1021 list_del(&ref->list);
1026 static void metricgroup__free_metrics(struct list_head *metric_list)
1028 struct metric *m, *tmp;
1030 list_for_each_entry_safe (m, tmp, metric_list, nd) {
1031 metric__free_refs(m);
1032 expr__ctx_clear(&m->pctx);
1033 list_del_init(&m->nd);
1038 static int parse_groups(struct evlist *perf_evlist, const char *str,
1039 bool metric_no_group,
1040 bool metric_no_merge,
1041 struct perf_pmu *fake_pmu,
1042 struct rblist *metric_events,
1043 struct pmu_events_map *map)
1045 struct parse_events_error parse_error;
1046 struct strbuf extra_events;
1047 LIST_HEAD(metric_list);
1050 if (metric_events->nr_entries == 0)
1051 metricgroup__rblist_init(metric_events);
1052 ret = metricgroup__add_metric_list(str, metric_no_group,
1053 &extra_events, &metric_list, map);
1056 pr_debug("adding %s\n", extra_events.buf);
1057 bzero(&parse_error, sizeof(parse_error));
1058 ret = __parse_events(perf_evlist, extra_events.buf, &parse_error, fake_pmu);
1060 parse_events_print_error(&parse_error, extra_events.buf);
1063 ret = metricgroup__setup_events(&metric_list, metric_no_merge,
1064 perf_evlist, metric_events);
1066 metricgroup__free_metrics(&metric_list);
1067 strbuf_release(&extra_events);
1071 int metricgroup__parse_groups(const struct option *opt,
1073 bool metric_no_group,
1074 bool metric_no_merge,
1075 struct rblist *metric_events)
1077 struct evlist *perf_evlist = *(struct evlist **)opt->value;
1078 struct pmu_events_map *map = perf_pmu__find_map(NULL);
1083 return parse_groups(perf_evlist, str, metric_no_group,
1084 metric_no_merge, NULL, metric_events, map);
1087 int metricgroup__parse_groups_test(struct evlist *evlist,
1088 struct pmu_events_map *map,
1090 bool metric_no_group,
1091 bool metric_no_merge,
1092 struct rblist *metric_events)
1094 return parse_groups(evlist, str, metric_no_group,
1095 metric_no_merge, &perf_pmu__fake, metric_events, map);
1098 bool metricgroup__has_metric(const char *metric)
1100 struct pmu_events_map *map = perf_pmu__find_map(NULL);
1101 struct pmu_event *pe;
1107 for (i = 0; ; i++) {
1108 pe = &map->table[i];
1110 if (!pe->name && !pe->metric_group && !pe->metric_name)
1112 if (!pe->metric_expr)
1114 if (match_metric(pe->metric_name, metric))