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>
28 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
33 struct metric_event me = {
40 nd = rblist__find(metric_events, &me);
42 return container_of(nd, struct metric_event, nd);
44 rblist__add_node(metric_events, &me);
45 nd = rblist__find(metric_events, &me);
47 return container_of(nd, struct metric_event, nd);
52 static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
54 struct metric_event *a = container_of(rb_node,
57 const struct metric_event *b = entry;
59 if (a->evsel == b->evsel)
61 if ((char *)a->evsel < (char *)b->evsel)
66 static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
69 struct metric_event *me = malloc(sizeof(struct metric_event));
73 memcpy(me, entry, sizeof(struct metric_event));
74 me->evsel = ((struct metric_event *)entry)->evsel;
75 INIT_LIST_HEAD(&me->head);
79 static void metric_event_delete(struct rblist *rblist __maybe_unused,
80 struct rb_node *rb_node)
82 struct metric_event *me = container_of(rb_node, struct metric_event, nd);
83 struct metric_expr *expr, *tmp;
85 list_for_each_entry_safe(expr, tmp, &me->head, nd) {
86 free(expr->metric_refs);
93 static void metricgroup__rblist_init(struct rblist *metric_events)
95 rblist__init(metric_events);
96 metric_events->node_cmp = metric_event_cmp;
97 metric_events->node_new = metric_event_new;
98 metric_events->node_delete = metric_event_delete;
101 void metricgroup__rblist_exit(struct rblist *metric_events)
103 rblist__exit(metric_events);
107 * A node in the list of referenced metrics. metric_expr
108 * is held as a convenience to avoid a search through the
111 struct metric_ref_node {
112 const char *metric_name;
113 const char *metric_expr;
114 struct list_head list;
119 struct expr_parse_ctx pctx;
120 const char *metric_name;
121 const char *metric_expr;
122 const char *metric_unit;
123 struct list_head metric_refs;
130 * Find a group of events in perf_evlist that correpond to those from a parsed
131 * metric expression. Note, as find_evsel_group is called in the same order as
132 * perf_evlist was constructed, metric_no_merge doesn't need to test for
133 * underfilling a group.
134 * @perf_evlist: a list of events something like: {metric1 leader, metric1
135 * sibling, metric1 sibling}:W,duration_time,{metric2 leader, metric2 sibling,
136 * metric2 sibling}:W,duration_time
137 * @pctx: the parse context for the metric expression.
138 * @metric_no_merge: don't attempt to share events for the metric with other
140 * @has_constraint: is there a contraint on the group of events? In which case
141 * the events won't be grouped.
142 * @metric_events: out argument, null terminated array of evsel's associated
144 * @evlist_used: in/out argument, bitmap tracking which evlist events are used.
145 * @return the first metric event or NULL on failure.
147 static struct evsel *find_evsel_group(struct evlist *perf_evlist,
148 struct expr_parse_ctx *pctx,
149 bool metric_no_merge,
151 struct evsel **metric_events,
152 unsigned long *evlist_used)
154 struct evsel *ev, *current_leader = NULL;
155 struct expr_id_data *val_ptr;
156 int i = 0, matched_events = 0, events_to_match;
157 const int idnum = (int)hashmap__size(&pctx->ids);
159 /* duration_time is grouped separately. */
160 if (!has_constraint &&
161 hashmap__find(&pctx->ids, "duration_time", (void **)&val_ptr))
162 events_to_match = idnum - 1;
164 events_to_match = idnum;
166 evlist__for_each_entry (perf_evlist, ev) {
168 * Events with a constraint aren't grouped and match the first
171 if (has_constraint && ev->weak_group)
173 /* Ignore event if already used and merging is disabled. */
174 if (metric_no_merge && test_bit(ev->idx, evlist_used))
176 if (!has_constraint && ev->leader != current_leader) {
178 * Start of a new group, discard the whole match and
182 memset(metric_events, 0,
183 sizeof(struct evsel *) * idnum);
184 current_leader = ev->leader;
186 if (hashmap__find(&pctx->ids, ev->name, (void **)&val_ptr)) {
187 if (has_constraint) {
189 * Events aren't grouped, ensure the same event
190 * isn't matched from two groups.
192 for (i = 0; i < matched_events; i++) {
193 if (!strcmp(ev->name,
194 metric_events[i]->name)) {
198 if (i != matched_events)
201 metric_events[matched_events++] = ev;
203 if (matched_events == events_to_match)
207 if (events_to_match != idnum) {
208 /* Add the first duration_time. */
209 evlist__for_each_entry(perf_evlist, ev) {
210 if (!strcmp(ev->name, "duration_time")) {
211 metric_events[matched_events++] = ev;
217 if (matched_events != idnum) {
218 /* Not whole match */
222 metric_events[idnum] = NULL;
224 for (i = 0; i < idnum; i++) {
225 ev = metric_events[i];
226 ev->metric_leader = ev;
227 set_bit(ev->idx, evlist_used);
230 return metric_events[0];
233 static int metricgroup__setup_events(struct list_head *groups,
234 bool metric_no_merge,
235 struct evlist *perf_evlist,
236 struct rblist *metric_events_list)
238 struct metric_event *me;
239 struct metric_expr *expr;
243 struct evsel *evsel, *tmp;
244 unsigned long *evlist_used;
246 evlist_used = bitmap_alloc(perf_evlist->core.nr_entries);
250 list_for_each_entry (eg, groups, nd) {
251 struct evsel **metric_events;
252 struct metric_ref *metric_refs = NULL;
254 metric_events = calloc(sizeof(void *),
255 hashmap__size(&eg->pctx.ids) + 1);
256 if (!metric_events) {
260 evsel = find_evsel_group(perf_evlist, &eg->pctx,
262 eg->has_constraint, metric_events,
265 pr_debug("Cannot resolve %s: %s\n",
266 eg->metric_name, eg->metric_expr);
270 for (i = 0; metric_events[i]; i++)
271 metric_events[i]->collect_stat = true;
272 me = metricgroup__lookup(metric_events_list, evsel, true);
278 expr = malloc(sizeof(struct metric_expr));
286 * Collect and store collected nested expressions
287 * for metric processing.
289 if (eg->metric_refs_cnt) {
290 struct metric_ref_node *ref;
292 metric_refs = zalloc(sizeof(struct metric_ref) * (eg->metric_refs_cnt + 1));
300 list_for_each_entry(ref, &eg->metric_refs, list) {
302 * Intentionally passing just const char pointers,
303 * originally from 'struct pmu_event' object.
304 * We don't need to change them, so there's no
305 * need to create our own copy.
307 metric_refs[i].metric_name = ref->metric_name;
308 metric_refs[i].metric_expr = ref->metric_expr;
313 expr->metric_refs = metric_refs;
314 expr->metric_expr = eg->metric_expr;
315 expr->metric_name = eg->metric_name;
316 expr->metric_unit = eg->metric_unit;
317 expr->metric_events = metric_events;
318 expr->runtime = eg->runtime;
319 list_add(&expr->nd, &me->head);
322 evlist__for_each_entry_safe(perf_evlist, tmp, evsel) {
323 if (!test_bit(evsel->idx, evlist_used)) {
324 evlist__remove(perf_evlist, evsel);
325 evsel__delete(evsel);
328 bitmap_free(evlist_used);
333 static bool match_metric(const char *n, const char *list)
340 if (!strcmp(list, "all"))
343 return !strcasecmp(list, "No_group");
345 m = strcasestr(n, list);
348 if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
349 (m[len] == 0 || m[len] == ';'))
357 struct strlist *metrics;
360 static int mep_cmp(struct rb_node *rb_node, const void *entry)
362 struct mep *a = container_of(rb_node, struct mep, nd);
363 struct mep *b = (struct mep *)entry;
365 return strcmp(a->name, b->name);
368 static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
371 struct mep *me = malloc(sizeof(struct mep));
375 memcpy(me, entry, sizeof(struct mep));
376 me->name = strdup(me->name);
379 me->metrics = strlist__new(NULL, NULL);
390 static struct mep *mep_lookup(struct rblist *groups, const char *name)
396 nd = rblist__find(groups, &me);
398 return container_of(nd, struct mep, nd);
399 rblist__add_node(groups, &me);
400 nd = rblist__find(groups, &me);
402 return container_of(nd, struct mep, nd);
406 static void mep_delete(struct rblist *rl __maybe_unused,
409 struct mep *me = container_of(nd, struct mep, nd);
411 strlist__delete(me->metrics);
416 static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
421 strlist__for_each_entry (sn, metrics) {
423 printf("%s%s", n > 0 ? " " : "", sn->s);
425 printf(" %s\n", sn->s);
432 void metricgroup__print(bool metrics, bool metricgroups, char *filter,
433 bool raw, bool details)
435 struct pmu_events_map *map = perf_pmu__find_map(NULL);
436 struct pmu_event *pe;
438 struct rblist groups;
439 struct rb_node *node, *next;
440 struct strlist *metriclist = NULL;
446 metriclist = strlist__new(NULL, NULL);
451 rblist__init(&groups);
452 groups.node_new = mep_new;
453 groups.node_cmp = mep_cmp;
454 groups.node_delete = mep_delete;
459 if (!pe->name && !pe->metric_group && !pe->metric_name)
461 if (!pe->metric_expr)
463 g = pe->metric_group;
464 if (!g && pe->metric_name) {
471 char *mg = strdup(g);
476 while ((g = strsep(&mg, ";")) != NULL) {
483 if (filter && !strstr(g, filter))
486 s = (char *)pe->metric_name;
488 if (asprintf(&s, "%s\n%*s%s]",
489 pe->metric_name, 8, "[", pe->desc) < 0)
493 if (asprintf(&s, "%s\n%*s%s]",
494 s, 8, "[", pe->metric_expr) < 0)
503 strlist__add(metriclist, s);
505 me = mep_lookup(&groups, g);
508 strlist__add(me->metrics, s);
515 if (metricgroups && !raw)
516 printf("\nMetric Groups:\n\n");
517 else if (metrics && !raw)
518 printf("\nMetrics:\n\n");
520 for (node = rb_first_cached(&groups.entries); node; node = next) {
521 struct mep *me = container_of(node, struct mep, nd);
524 printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
526 metricgroup__print_strlist(me->metrics, raw);
527 next = rb_next(node);
528 rblist__remove_node(&groups, node);
531 metricgroup__print_strlist(metriclist, raw);
532 strlist__delete(metriclist);
535 static void metricgroup__add_metric_weak_group(struct strbuf *events,
536 struct expr_parse_ctx *ctx)
538 struct hashmap_entry *cur;
540 bool no_group = true, has_duration = false;
542 hashmap__for_each_entry((&ctx->ids), cur, bkt) {
543 pr_debug("found event %s\n", (const char *)cur->key);
545 * Duration time maps to a software event and can make
546 * groups not count. Always use it outside a
549 if (!strcmp(cur->key, "duration_time")) {
553 strbuf_addf(events, "%s%s",
554 no_group ? "{" : ",",
555 (const char *)cur->key);
559 strbuf_addf(events, "}:W");
561 strbuf_addf(events, ",duration_time");
562 } else if (has_duration)
563 strbuf_addf(events, "duration_time");
566 static void metricgroup__add_metric_non_group(struct strbuf *events,
567 struct expr_parse_ctx *ctx)
569 struct hashmap_entry *cur;
573 hashmap__for_each_entry((&ctx->ids), cur, bkt) {
575 strbuf_addf(events, ",");
576 strbuf_addf(events, "%s", (const char *)cur->key);
581 static void metricgroup___watchdog_constraint_hint(const char *name, bool foot)
583 static bool violate_nmi_constraint;
586 pr_warning("Splitting metric group %s into standalone metrics.\n", name);
587 violate_nmi_constraint = true;
591 if (!violate_nmi_constraint)
594 pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n"
595 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
597 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
600 static bool metricgroup__has_constraint(struct pmu_event *pe)
602 if (!pe->metric_constraint)
605 if (!strcmp(pe->metric_constraint, "NO_NMI_WATCHDOG") &&
606 sysctl__nmi_watchdog_enabled()) {
607 metricgroup___watchdog_constraint_hint(pe->metric_name, false);
614 int __weak arch_get_runtimeparam(void)
619 static int __add_metric(struct list_head *group_list,
620 struct pmu_event *pe,
621 bool metric_no_group,
625 struct metric_ref_node *ref;
630 * We got in here for the parent group,
631 * allocate it and put it on the list.
633 eg = malloc(sizeof(*eg));
637 expr__ctx_init(&eg->pctx);
638 eg->metric_name = pe->metric_name;
639 eg->metric_expr = pe->metric_expr;
640 eg->metric_unit = pe->unit;
641 eg->runtime = runtime;
642 eg->has_constraint = metric_no_group || metricgroup__has_constraint(pe);
643 INIT_LIST_HEAD(&eg->metric_refs);
644 eg->metric_refs_cnt = 0;
648 * We got here for the referenced metric, via the
649 * recursive metricgroup__add_metric call, add
650 * it to the parent group.
654 ref = malloc(sizeof(*ref));
659 * Intentionally passing just const char pointers,
660 * from 'pe' object, so they never go away. We don't
661 * need to change them, so there's no need to create
664 ref->metric_name = pe->metric_name;
665 ref->metric_expr = pe->metric_expr;
667 list_add(&ref->list, &eg->metric_refs);
668 eg->metric_refs_cnt++;
672 * For both the parent and referenced metrics, we parse
673 * all the metric's IDs and add it to the parent context.
675 if (expr__find_other(pe->metric_expr, NULL, &eg->pctx, runtime) < 0) {
676 expr__ctx_clear(&eg->pctx);
682 * We add new group only in the 'parent' call,
683 * so bail out for referenced metric case.
685 if (eg->metric_refs_cnt)
688 if (list_empty(group_list))
689 list_add(&eg->nd, group_list);
691 struct list_head *pos;
693 /* Place the largest groups at the front. */
694 list_for_each_prev(pos, group_list) {
695 struct egroup *old = list_entry(pos, struct egroup, nd);
697 if (hashmap__size(&eg->pctx.ids) <=
698 hashmap__size(&old->pctx.ids))
701 list_add(&eg->nd, pos);
707 #define map_for_each_event(__pe, __idx, __map) \
708 for (__idx = 0, __pe = &__map->table[__idx]; \
709 __pe->name || __pe->metric_group || __pe->metric_name; \
710 __pe = &__map->table[++__idx])
712 #define map_for_each_metric(__pe, __idx, __map, __metric) \
713 map_for_each_event(__pe, __idx, __map) \
714 if (__pe->metric_expr && \
715 (match_metric(__pe->metric_group, __metric) || \
716 match_metric(__pe->metric_name, __metric)))
718 static struct pmu_event *find_metric(const char *metric, struct pmu_events_map *map)
720 struct pmu_event *pe;
723 map_for_each_event(pe, i, map) {
724 if (match_metric(pe->metric_name, metric))
731 static int add_metric(struct list_head *group_list,
732 struct pmu_event *pe,
733 bool metric_no_group,
734 struct egroup **egp);
736 static int __resolve_metric(struct egroup *eg,
737 bool metric_no_group,
738 struct list_head *group_list,
739 struct pmu_events_map *map)
741 struct hashmap_entry *cur;
747 * Iterate all the parsed IDs and if there's metric,
748 * add it to the context.
752 hashmap__for_each_entry((&eg->pctx.ids), cur, bkt) {
753 struct pmu_event *pe;
755 pe = find_metric(cur->key, map);
760 /* The metric key itself needs to go out.. */
761 expr__del_id(&eg->pctx, cur->key);
763 /* ... and it gets resolved to the parent context. */
764 ret = add_metric(group_list, pe, metric_no_group, &eg);
769 * We added new metric to hashmap, so we need
770 * to break the iteration and start over.
779 static int resolve_metric(bool metric_no_group,
780 struct list_head *metric_list,
781 struct pmu_events_map *map)
786 list_for_each_entry(eg, metric_list, nd) {
787 err = __resolve_metric(eg, metric_no_group, metric_list, map);
794 static int add_metric(struct list_head *group_list,
795 struct pmu_event *pe,
796 bool metric_no_group,
799 struct egroup *orig = *egp;
802 pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
804 if (!strstr(pe->metric_expr, "?")) {
805 ret = __add_metric(group_list, pe, metric_no_group, 1, egp);
809 count = arch_get_runtimeparam();
811 /* This loop is added to create multiple
812 * events depend on count value and add
813 * those events to group_list.
816 for (j = 0; j < count && !ret; j++, *egp = orig)
817 ret = __add_metric(group_list, pe, metric_no_group, j, egp);
823 static int metricgroup__add_metric(const char *metric, bool metric_no_group,
824 struct strbuf *events,
825 struct list_head *group_list,
826 struct pmu_events_map *map)
828 struct pmu_event *pe;
831 bool has_match = false;
833 map_for_each_metric(pe, i, map, metric) {
837 ret = add_metric(group_list, pe, metric_no_group, &eg);
842 * Process any possible referenced metrics
843 * included in the expression.
845 ret = resolve_metric(metric_no_group,
851 /* End of pmu events. */
855 list_for_each_entry(eg, group_list, nd) {
857 strbuf_addf(events, ",");
859 if (eg->has_constraint) {
860 metricgroup__add_metric_non_group(events,
863 metricgroup__add_metric_weak_group(events,
870 static int metricgroup__add_metric_list(const char *list, bool metric_no_group,
871 struct strbuf *events,
872 struct list_head *group_list,
873 struct pmu_events_map *map)
875 char *llist, *nlist, *p;
878 nlist = strdup(list);
883 strbuf_init(events, 100);
884 strbuf_addf(events, "%s", "");
886 while ((p = strsep(&llist, ",")) != NULL) {
887 ret = metricgroup__add_metric(p, metric_no_group, events,
889 if (ret == -EINVAL) {
890 fprintf(stderr, "Cannot find metric or group `%s'\n",
898 metricgroup___watchdog_constraint_hint(NULL, true);
903 static void egroup__free_refs(struct egroup *egroup)
905 struct metric_ref_node *ref, *tmp;
907 list_for_each_entry_safe(ref, tmp, &egroup->metric_refs, list) {
908 list_del(&ref->list);
913 static void metricgroup__free_egroups(struct list_head *group_list)
915 struct egroup *eg, *egtmp;
917 list_for_each_entry_safe (eg, egtmp, group_list, nd) {
918 egroup__free_refs(eg);
919 expr__ctx_clear(&eg->pctx);
920 list_del_init(&eg->nd);
925 static int parse_groups(struct evlist *perf_evlist, const char *str,
926 bool metric_no_group,
927 bool metric_no_merge,
928 struct perf_pmu *fake_pmu,
929 struct rblist *metric_events,
930 struct pmu_events_map *map)
932 struct parse_events_error parse_error;
933 struct strbuf extra_events;
934 LIST_HEAD(group_list);
937 if (metric_events->nr_entries == 0)
938 metricgroup__rblist_init(metric_events);
939 ret = metricgroup__add_metric_list(str, metric_no_group,
940 &extra_events, &group_list, map);
943 pr_debug("adding %s\n", extra_events.buf);
944 bzero(&parse_error, sizeof(parse_error));
945 ret = __parse_events(perf_evlist, extra_events.buf, &parse_error, fake_pmu);
947 parse_events_print_error(&parse_error, extra_events.buf);
950 strbuf_release(&extra_events);
951 ret = metricgroup__setup_events(&group_list, metric_no_merge,
952 perf_evlist, metric_events);
954 metricgroup__free_egroups(&group_list);
958 int metricgroup__parse_groups(const struct option *opt,
960 bool metric_no_group,
961 bool metric_no_merge,
962 struct rblist *metric_events)
964 struct evlist *perf_evlist = *(struct evlist **)opt->value;
965 struct pmu_events_map *map = perf_pmu__find_map(NULL);
970 return parse_groups(perf_evlist, str, metric_no_group,
971 metric_no_merge, NULL, metric_events, map);
974 int metricgroup__parse_groups_test(struct evlist *evlist,
975 struct pmu_events_map *map,
977 bool metric_no_group,
978 bool metric_no_merge,
979 struct rblist *metric_events)
981 return parse_groups(evlist, str, metric_no_group,
982 metric_no_merge, &perf_pmu__fake, metric_events, map);
985 bool metricgroup__has_metric(const char *metric)
987 struct pmu_events_map *map = perf_pmu__find_map(NULL);
988 struct pmu_event *pe;
997 if (!pe->name && !pe->metric_group && !pe->metric_name)
999 if (!pe->metric_expr)
1001 if (match_metric(pe->metric_name, metric))