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"
20 #include <linux/ctype.h>
21 #include <linux/string.h>
22 #include <linux/zalloc.h>
23 #include <subcmd/parse-options.h>
24 #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++)
153 static bool contains_event(struct evsel **metric_events, int num_events,
154 const char *event_name)
158 for (i = 0; i < num_events; i++) {
159 if (!strcmp(metric_events[i]->name, event_name))
166 * Find a group of events in perf_evlist that correspond to those from a parsed
167 * metric expression. Note, as find_evsel_group is called in the same order as
168 * perf_evlist was constructed, metric_no_merge doesn't need to test for
169 * underfilling a group.
170 * @perf_evlist: a list of events something like: {metric1 leader, metric1
171 * sibling, metric1 sibling}:W,duration_time,{metric2 leader, metric2 sibling,
172 * metric2 sibling}:W,duration_time
173 * @pctx: the parse context for the metric expression.
174 * @metric_no_merge: don't attempt to share events for the metric with other
176 * @has_constraint: is there a contraint on the group of events? In which case
177 * the events won't be grouped.
178 * @metric_events: out argument, null terminated array of evsel's associated
180 * @evlist_used: in/out argument, bitmap tracking which evlist events are used.
181 * @return the first metric event or NULL on failure.
183 static struct evsel *find_evsel_group(struct evlist *perf_evlist,
184 struct expr_parse_ctx *pctx,
185 bool metric_no_merge,
187 struct evsel **metric_events,
188 unsigned long *evlist_used)
190 struct evsel *ev, *current_leader = NULL;
191 struct expr_id_data *val_ptr;
192 int i = 0, matched_events = 0, events_to_match;
193 const int idnum = (int)hashmap__size(&pctx->ids);
196 * duration_time is always grouped separately, when events are grouped
197 * (ie has_constraint is false) then ignore it in the matching loop and
198 * add it to metric_events at the end.
200 if (!has_constraint &&
201 hashmap__find(&pctx->ids, "duration_time", (void **)&val_ptr))
202 events_to_match = idnum - 1;
204 events_to_match = idnum;
206 evlist__for_each_entry (perf_evlist, ev) {
208 * Events with a constraint aren't grouped and match the first
211 if (has_constraint && ev->weak_group)
213 /* Ignore event if already used and merging is disabled. */
214 if (metric_no_merge && test_bit(ev->idx, evlist_used))
216 if (!has_constraint && ev->leader != current_leader) {
218 * Start of a new group, discard the whole match and
222 memset(metric_events, 0,
223 sizeof(struct evsel *) * idnum);
224 current_leader = ev->leader;
227 * Check for duplicate events with the same name. For example,
228 * uncore_imc/cas_count_read/ will turn into 6 events per socket
229 * on skylakex. Only the first such event is placed in
230 * metric_events. If events aren't grouped then this also
231 * ensures that the same event in different sibling groups
232 * aren't both added to metric_events.
234 if (contains_event(metric_events, matched_events, ev->name))
236 /* Does this event belong to the parse context? */
237 if (hashmap__find(&pctx->ids, ev->name, (void **)&val_ptr))
238 metric_events[matched_events++] = ev;
240 if (matched_events == events_to_match)
244 if (events_to_match != idnum) {
245 /* Add the first duration_time. */
246 evlist__for_each_entry(perf_evlist, ev) {
247 if (!strcmp(ev->name, "duration_time")) {
248 metric_events[matched_events++] = ev;
254 if (matched_events != idnum) {
255 /* Not a whole match */
259 metric_events[idnum] = NULL;
261 for (i = 0; i < idnum; i++) {
262 ev = metric_events[i];
263 /* Don't free the used events. */
264 set_bit(ev->idx, evlist_used);
266 * The metric leader points to the identically named event in
269 ev->metric_leader = ev;
271 * Mark two events with identical names in the same group (or
272 * globally) as being in use as uncore events may be duplicated
273 * for each pmu. Set the metric leader of such events to be the
274 * event that appears in metric_events.
276 evlist__for_each_entry_continue(perf_evlist, ev) {
278 * If events are grouped then the search can terminate
279 * when then group is left.
281 if (!has_constraint &&
282 ev->leader != metric_events[i]->leader)
284 if (!strcmp(metric_events[i]->name, ev->name)) {
285 set_bit(ev->idx, evlist_used);
286 ev->metric_leader = metric_events[i];
291 return metric_events[0];
294 static int metricgroup__setup_events(struct list_head *groups,
295 bool metric_no_merge,
296 struct evlist *perf_evlist,
297 struct rblist *metric_events_list)
299 struct metric_event *me;
300 struct metric_expr *expr;
304 struct evsel *evsel, *tmp;
305 unsigned long *evlist_used;
307 evlist_used = bitmap_alloc(perf_evlist->core.nr_entries);
311 list_for_each_entry (m, groups, nd) {
312 struct evsel **metric_events;
313 struct metric_ref *metric_refs = NULL;
315 metric_events = calloc(sizeof(void *),
316 hashmap__size(&m->pctx.ids) + 1);
317 if (!metric_events) {
321 evsel = find_evsel_group(perf_evlist, &m->pctx,
323 m->has_constraint, metric_events,
326 pr_debug("Cannot resolve %s: %s\n",
327 m->metric_name, m->metric_expr);
331 for (i = 0; metric_events[i]; i++)
332 metric_events[i]->collect_stat = true;
333 me = metricgroup__lookup(metric_events_list, evsel, true);
339 expr = malloc(sizeof(struct metric_expr));
347 * Collect and store collected nested expressions
348 * for metric processing.
350 if (m->metric_refs_cnt) {
351 struct metric_ref_node *ref;
353 metric_refs = zalloc(sizeof(struct metric_ref) * (m->metric_refs_cnt + 1));
362 list_for_each_entry(ref, &m->metric_refs, list) {
364 * Intentionally passing just const char pointers,
365 * originally from 'struct pmu_event' object.
366 * We don't need to change them, so there's no
367 * need to create our own copy.
369 metric_refs[i].metric_name = ref->metric_name;
370 metric_refs[i].metric_expr = ref->metric_expr;
375 expr->metric_refs = metric_refs;
376 expr->metric_expr = m->metric_expr;
377 expr->metric_name = m->metric_name;
378 expr->metric_unit = m->metric_unit;
379 expr->metric_events = metric_events;
380 expr->runtime = m->runtime;
381 list_add(&expr->nd, &me->head);
384 evlist__for_each_entry_safe(perf_evlist, tmp, evsel) {
385 if (!test_bit(evsel->idx, evlist_used)) {
386 evlist__remove(perf_evlist, evsel);
387 evsel__delete(evsel);
390 bitmap_free(evlist_used);
395 static bool match_metric(const char *n, const char *list)
402 if (!strcmp(list, "all"))
405 return !strcasecmp(list, "No_group");
407 m = strcasestr(n, list);
410 if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
411 (m[len] == 0 || m[len] == ';'))
419 struct strlist *metrics;
422 static int mep_cmp(struct rb_node *rb_node, const void *entry)
424 struct mep *a = container_of(rb_node, struct mep, nd);
425 struct mep *b = (struct mep *)entry;
427 return strcmp(a->name, b->name);
430 static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
433 struct mep *me = malloc(sizeof(struct mep));
437 memcpy(me, entry, sizeof(struct mep));
438 me->name = strdup(me->name);
441 me->metrics = strlist__new(NULL, NULL);
452 static struct mep *mep_lookup(struct rblist *groups, const char *name)
458 nd = rblist__find(groups, &me);
460 return container_of(nd, struct mep, nd);
461 rblist__add_node(groups, &me);
462 nd = rblist__find(groups, &me);
464 return container_of(nd, struct mep, nd);
468 static void mep_delete(struct rblist *rl __maybe_unused,
471 struct mep *me = container_of(nd, struct mep, nd);
473 strlist__delete(me->metrics);
478 static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
483 strlist__for_each_entry (sn, metrics) {
485 printf("%s%s", n > 0 ? " " : "", sn->s);
487 printf(" %s\n", sn->s);
494 void metricgroup__print(bool metrics, bool metricgroups, char *filter,
495 bool raw, bool details)
497 struct pmu_events_map *map = perf_pmu__find_map(NULL);
498 struct pmu_event *pe;
500 struct rblist groups;
501 struct rb_node *node, *next;
502 struct strlist *metriclist = NULL;
508 metriclist = strlist__new(NULL, NULL);
513 rblist__init(&groups);
514 groups.node_new = mep_new;
515 groups.node_cmp = mep_cmp;
516 groups.node_delete = mep_delete;
521 if (!pe->name && !pe->metric_group && !pe->metric_name)
523 if (!pe->metric_expr)
525 g = pe->metric_group;
526 if (!g && pe->metric_name) {
533 char *mg = strdup(g);
538 while ((g = strsep(&mg, ";")) != NULL) {
545 if (filter && !strstr(g, filter))
548 s = (char *)pe->metric_name;
550 if (asprintf(&s, "%s\n%*s%s]",
551 pe->metric_name, 8, "[", pe->desc) < 0)
555 if (asprintf(&s, "%s\n%*s%s]",
556 s, 8, "[", pe->metric_expr) < 0)
565 strlist__add(metriclist, s);
567 me = mep_lookup(&groups, g);
570 strlist__add(me->metrics, s);
580 if (!filter || !rblist__empty(&groups)) {
581 if (metricgroups && !raw)
582 printf("\nMetric Groups:\n\n");
583 else if (metrics && !raw)
584 printf("\nMetrics:\n\n");
587 for (node = rb_first_cached(&groups.entries); node; node = next) {
588 struct mep *me = container_of(node, struct mep, nd);
591 printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
593 metricgroup__print_strlist(me->metrics, raw);
594 next = rb_next(node);
595 rblist__remove_node(&groups, node);
598 metricgroup__print_strlist(metriclist, raw);
599 strlist__delete(metriclist);
602 static void metricgroup__add_metric_weak_group(struct strbuf *events,
603 struct expr_parse_ctx *ctx)
605 struct hashmap_entry *cur;
607 bool no_group = true, has_duration = false;
609 hashmap__for_each_entry((&ctx->ids), cur, bkt) {
610 pr_debug("found event %s\n", (const char *)cur->key);
612 * Duration time maps to a software event and can make
613 * groups not count. Always use it outside a
616 if (!strcmp(cur->key, "duration_time")) {
620 strbuf_addf(events, "%s%s",
621 no_group ? "{" : ",",
622 (const char *)cur->key);
626 strbuf_addf(events, "}:W");
628 strbuf_addf(events, ",duration_time");
629 } else if (has_duration)
630 strbuf_addf(events, "duration_time");
633 static void metricgroup__add_metric_non_group(struct strbuf *events,
634 struct expr_parse_ctx *ctx)
636 struct hashmap_entry *cur;
640 hashmap__for_each_entry((&ctx->ids), cur, bkt) {
642 strbuf_addf(events, ",");
643 strbuf_addf(events, "%s", (const char *)cur->key);
648 static void metricgroup___watchdog_constraint_hint(const char *name, bool foot)
650 static bool violate_nmi_constraint;
653 pr_warning("Splitting metric group %s into standalone metrics.\n", name);
654 violate_nmi_constraint = true;
658 if (!violate_nmi_constraint)
661 pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n"
662 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
664 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
667 static bool metricgroup__has_constraint(struct pmu_event *pe)
669 if (!pe->metric_constraint)
672 if (!strcmp(pe->metric_constraint, "NO_NMI_WATCHDOG") &&
673 sysctl__nmi_watchdog_enabled()) {
674 metricgroup___watchdog_constraint_hint(pe->metric_name, false);
681 int __weak arch_get_runtimeparam(struct pmu_event *pe __maybe_unused)
686 static int __add_metric(struct list_head *metric_list,
687 struct pmu_event *pe,
688 bool metric_no_group,
691 struct expr_id *parent,
692 struct expr_ids *ids)
694 struct metric_ref_node *ref;
699 * We got in here for the parent group,
700 * allocate it and put it on the list.
702 m = zalloc(sizeof(*m));
706 expr__ctx_init(&m->pctx);
707 m->metric_name = pe->metric_name;
708 m->metric_expr = pe->metric_expr;
709 m->metric_unit = pe->unit;
710 m->runtime = runtime;
711 m->has_constraint = metric_no_group || metricgroup__has_constraint(pe);
712 INIT_LIST_HEAD(&m->metric_refs);
713 m->metric_refs_cnt = 0;
715 parent = expr_ids__alloc(ids);
721 parent->id = strdup(pe->metric_name);
729 * We got here for the referenced metric, via the
730 * recursive metricgroup__add_metric call, add
731 * it to the parent group.
735 ref = malloc(sizeof(*ref));
740 * Intentionally passing just const char pointers,
741 * from 'pe' object, so they never go away. We don't
742 * need to change them, so there's no need to create
745 ref->metric_name = pe->metric_name;
746 ref->metric_expr = pe->metric_expr;
748 list_add(&ref->list, &m->metric_refs);
749 m->metric_refs_cnt++;
752 /* Force all found IDs in metric to have us as parent ID. */
753 WARN_ON_ONCE(!parent);
754 m->pctx.parent = parent;
757 * For both the parent and referenced metrics, we parse
758 * all the metric's IDs and add it to the parent context.
760 if (expr__find_other(pe->metric_expr, NULL, &m->pctx, runtime) < 0) {
761 if (m->metric_refs_cnt == 0) {
762 expr__ctx_clear(&m->pctx);
770 * We add new group only in the 'parent' call,
771 * so bail out for referenced metric case.
773 if (m->metric_refs_cnt)
776 if (list_empty(metric_list))
777 list_add(&m->nd, metric_list);
779 struct list_head *pos;
781 /* Place the largest groups at the front. */
782 list_for_each_prev(pos, metric_list) {
783 struct metric *old = list_entry(pos, struct metric, nd);
785 if (hashmap__size(&m->pctx.ids) <=
786 hashmap__size(&old->pctx.ids))
789 list_add(&m->nd, pos);
795 #define map_for_each_event(__pe, __idx, __map) \
796 for (__idx = 0, __pe = &__map->table[__idx]; \
797 __pe->name || __pe->metric_group || __pe->metric_name; \
798 __pe = &__map->table[++__idx])
800 #define map_for_each_metric(__pe, __idx, __map, __metric) \
801 map_for_each_event(__pe, __idx, __map) \
802 if (__pe->metric_expr && \
803 (match_metric(__pe->metric_group, __metric) || \
804 match_metric(__pe->metric_name, __metric)))
806 static struct pmu_event *find_metric(const char *metric, struct pmu_events_map *map)
808 struct pmu_event *pe;
811 map_for_each_event(pe, i, map) {
812 if (match_metric(pe->metric_name, metric))
819 static int recursion_check(struct metric *m, const char *id, struct expr_id **parent,
820 struct expr_ids *ids)
822 struct expr_id_data *data;
827 * We get the parent referenced by 'id' argument and
828 * traverse through all the parent object IDs to check
829 * if we already processed 'id', if we did, it's recursion
832 ret = expr__get_id(&m->pctx, id, &data);
839 if (!strcmp(p->id, id)) {
840 pr_err("failed: recursion detected for %s\n", id);
847 * If we are over the limit of static entris, the metric
848 * is too difficult/nested to process, fail as well.
850 p = expr_ids__alloc(ids);
852 pr_err("failed: too many nested metrics\n");
857 p->parent = data->parent;
860 return p->id ? 0 : -ENOMEM;
863 static int add_metric(struct list_head *metric_list,
864 struct pmu_event *pe,
865 bool metric_no_group,
867 struct expr_id *parent,
868 struct expr_ids *ids);
870 static int __resolve_metric(struct metric *m,
871 bool metric_no_group,
872 struct list_head *metric_list,
873 struct pmu_events_map *map,
874 struct expr_ids *ids)
876 struct hashmap_entry *cur;
882 * Iterate all the parsed IDs and if there's metric,
883 * add it to the context.
887 hashmap__for_each_entry((&m->pctx.ids), cur, bkt) {
888 struct expr_id *parent;
889 struct pmu_event *pe;
891 pe = find_metric(cur->key, map);
895 ret = recursion_check(m, cur->key, &parent, ids);
900 /* The metric key itself needs to go out.. */
901 expr__del_id(&m->pctx, cur->key);
903 /* ... and it gets resolved to the parent context. */
904 ret = add_metric(metric_list, pe, metric_no_group, &m, parent, ids);
909 * We added new metric to hashmap, so we need
910 * to break the iteration and start over.
919 static int resolve_metric(bool metric_no_group,
920 struct list_head *metric_list,
921 struct pmu_events_map *map,
922 struct expr_ids *ids)
927 list_for_each_entry(m, metric_list, nd) {
928 err = __resolve_metric(m, metric_no_group, metric_list, map, ids);
935 static int add_metric(struct list_head *metric_list,
936 struct pmu_event *pe,
937 bool metric_no_group,
939 struct expr_id *parent,
940 struct expr_ids *ids)
942 struct metric *orig = *m;
945 pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
947 if (!strstr(pe->metric_expr, "?")) {
948 ret = __add_metric(metric_list, pe, metric_no_group, 1, m, parent, ids);
952 count = arch_get_runtimeparam(pe);
954 /* This loop is added to create multiple
955 * events depend on count value and add
956 * those events to metric_list.
959 for (j = 0; j < count && !ret; j++, *m = orig)
960 ret = __add_metric(metric_list, pe, metric_no_group, j, m, parent, ids);
966 static int metricgroup__add_metric(const char *metric, bool metric_no_group,
967 struct strbuf *events,
968 struct list_head *metric_list,
969 struct pmu_events_map *map)
971 struct expr_ids ids = { .cnt = 0, };
972 struct pmu_event *pe;
976 bool has_match = false;
978 map_for_each_metric(pe, i, map, metric) {
982 ret = add_metric(&list, pe, metric_no_group, &m, NULL, &ids);
987 * Process any possible referenced metrics
988 * included in the expression.
990 ret = resolve_metric(metric_no_group,
996 /* End of pmu events. */
1002 list_for_each_entry(m, &list, nd) {
1003 if (events->len > 0)
1004 strbuf_addf(events, ",");
1006 if (m->has_constraint) {
1007 metricgroup__add_metric_non_group(events,
1010 metricgroup__add_metric_weak_group(events,
1017 * add to metric_list so that they can be released
1018 * even if it's failed
1020 list_splice(&list, metric_list);
1021 expr_ids__exit(&ids);
1025 static int metricgroup__add_metric_list(const char *list, bool metric_no_group,
1026 struct strbuf *events,
1027 struct list_head *metric_list,
1028 struct pmu_events_map *map)
1030 char *llist, *nlist, *p;
1033 nlist = strdup(list);
1038 strbuf_init(events, 100);
1039 strbuf_addf(events, "%s", "");
1041 while ((p = strsep(&llist, ",")) != NULL) {
1042 ret = metricgroup__add_metric(p, metric_no_group, events,
1044 if (ret == -EINVAL) {
1045 fprintf(stderr, "Cannot find metric or group `%s'\n",
1053 metricgroup___watchdog_constraint_hint(NULL, true);
1058 static void metric__free_refs(struct metric *metric)
1060 struct metric_ref_node *ref, *tmp;
1062 list_for_each_entry_safe(ref, tmp, &metric->metric_refs, list) {
1063 list_del(&ref->list);
1068 static void metricgroup__free_metrics(struct list_head *metric_list)
1070 struct metric *m, *tmp;
1072 list_for_each_entry_safe (m, tmp, metric_list, nd) {
1073 metric__free_refs(m);
1074 expr__ctx_clear(&m->pctx);
1075 list_del_init(&m->nd);
1080 static int parse_groups(struct evlist *perf_evlist, const char *str,
1081 bool metric_no_group,
1082 bool metric_no_merge,
1083 struct perf_pmu *fake_pmu,
1084 struct rblist *metric_events,
1085 struct pmu_events_map *map)
1087 struct parse_events_error parse_error;
1088 struct strbuf extra_events;
1089 LIST_HEAD(metric_list);
1092 if (metric_events->nr_entries == 0)
1093 metricgroup__rblist_init(metric_events);
1094 ret = metricgroup__add_metric_list(str, metric_no_group,
1095 &extra_events, &metric_list, map);
1098 pr_debug("adding %s\n", extra_events.buf);
1099 bzero(&parse_error, sizeof(parse_error));
1100 ret = __parse_events(perf_evlist, extra_events.buf, &parse_error, fake_pmu);
1102 parse_events_print_error(&parse_error, extra_events.buf);
1105 ret = metricgroup__setup_events(&metric_list, metric_no_merge,
1106 perf_evlist, metric_events);
1108 metricgroup__free_metrics(&metric_list);
1109 strbuf_release(&extra_events);
1113 int metricgroup__parse_groups(const struct option *opt,
1115 bool metric_no_group,
1116 bool metric_no_merge,
1117 struct rblist *metric_events)
1119 struct evlist *perf_evlist = *(struct evlist **)opt->value;
1120 struct pmu_events_map *map = perf_pmu__find_map(NULL);
1125 return parse_groups(perf_evlist, str, metric_no_group,
1126 metric_no_merge, NULL, metric_events, map);
1129 int metricgroup__parse_groups_test(struct evlist *evlist,
1130 struct pmu_events_map *map,
1132 bool metric_no_group,
1133 bool metric_no_merge,
1134 struct rblist *metric_events)
1136 return parse_groups(evlist, str, metric_no_group,
1137 metric_no_merge, &perf_pmu__fake, metric_events, map);
1140 bool metricgroup__has_metric(const char *metric)
1142 struct pmu_events_map *map = perf_pmu__find_map(NULL);
1143 struct pmu_event *pe;
1149 for (i = 0; ; i++) {
1150 pe = &map->table[i];
1152 if (!pe->name && !pe->metric_group && !pe->metric_name)
1154 if (!pe->metric_expr)
1156 if (match_metric(pe->metric_name, metric))
1162 int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
1163 struct rblist *new_metric_events,
1164 struct rblist *old_metric_events)
1168 for (i = 0; i < rblist__nr_entries(old_metric_events); i++) {
1170 struct metric_event *old_me, *new_me;
1171 struct metric_expr *old_expr, *new_expr;
1172 struct evsel *evsel;
1176 nd = rblist__entry(old_metric_events, i);
1177 old_me = container_of(nd, struct metric_event, nd);
1179 evsel = evlist__find_evsel(evlist, old_me->evsel->idx);
1182 new_me = metricgroup__lookup(new_metric_events, evsel, true);
1186 pr_debug("copying metric event for cgroup '%s': %s (idx=%d)\n",
1187 cgrp ? cgrp->name : "root", evsel->name, evsel->idx);
1189 list_for_each_entry(old_expr, &old_me->head, nd) {
1190 new_expr = malloc(sizeof(*new_expr));
1194 new_expr->metric_expr = old_expr->metric_expr;
1195 new_expr->metric_name = old_expr->metric_name;
1196 new_expr->metric_unit = old_expr->metric_unit;
1197 new_expr->runtime = old_expr->runtime;
1199 if (old_expr->metric_refs) {
1200 /* calculate number of metric_events */
1201 for (nr = 0; old_expr->metric_refs[nr].metric_name; nr++)
1203 alloc_size = sizeof(*new_expr->metric_refs);
1204 new_expr->metric_refs = calloc(nr + 1, alloc_size);
1205 if (!new_expr->metric_refs) {
1210 memcpy(new_expr->metric_refs, old_expr->metric_refs,
1213 new_expr->metric_refs = NULL;
1216 /* calculate number of metric_events */
1217 for (nr = 0; old_expr->metric_events[nr]; nr++)
1219 alloc_size = sizeof(*new_expr->metric_events);
1220 new_expr->metric_events = calloc(nr + 1, alloc_size);
1221 if (!new_expr->metric_events) {
1222 free(new_expr->metric_refs);
1227 /* copy evsel in the same position */
1228 for (idx = 0; idx < nr; idx++) {
1229 evsel = old_expr->metric_events[idx];
1230 evsel = evlist__find_evsel(evlist, evsel->idx);
1231 if (evsel == NULL) {
1232 free(new_expr->metric_events);
1233 free(new_expr->metric_refs);
1237 new_expr->metric_events[idx] = evsel;
1240 list_add(&new_expr->nd, &new_me->head);