f690da21668d7ade9f718f217cfd03180003c211
[linux-2.6-microblaze.git] / tools / perf / util / metricgroup.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2017, Intel Corporation.
4  */
5
6 /* Manage metrics and groups of metrics from JSON files */
7
8 #include "metricgroup.h"
9 #include "debug.h"
10 #include "evlist.h"
11 #include "evsel.h"
12 #include "strbuf.h"
13 #include "pmu.h"
14 #include "expr.h"
15 #include "rblist.h"
16 #include <string.h>
17 #include <errno.h>
18 #include "pmu-events/pmu-events.h"
19 #include "strlist.h"
20 #include <assert.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>
26 #include "util.h"
27
28 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
29                                          struct evsel *evsel,
30                                          bool create)
31 {
32         struct rb_node *nd;
33         struct metric_event me = {
34                 .evsel = evsel
35         };
36
37         if (!metric_events)
38                 return NULL;
39
40         nd = rblist__find(metric_events, &me);
41         if (nd)
42                 return container_of(nd, struct metric_event, nd);
43         if (create) {
44                 rblist__add_node(metric_events, &me);
45                 nd = rblist__find(metric_events, &me);
46                 if (nd)
47                         return container_of(nd, struct metric_event, nd);
48         }
49         return NULL;
50 }
51
52 static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
53 {
54         struct metric_event *a = container_of(rb_node,
55                                               struct metric_event,
56                                               nd);
57         const struct metric_event *b = entry;
58
59         if (a->evsel == b->evsel)
60                 return 0;
61         if ((char *)a->evsel < (char *)b->evsel)
62                 return -1;
63         return +1;
64 }
65
66 static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
67                                         const void *entry)
68 {
69         struct metric_event *me = malloc(sizeof(struct metric_event));
70
71         if (!me)
72                 return NULL;
73         memcpy(me, entry, sizeof(struct metric_event));
74         me->evsel = ((struct metric_event *)entry)->evsel;
75         INIT_LIST_HEAD(&me->head);
76         return &me->nd;
77 }
78
79 static void metric_event_delete(struct rblist *rblist __maybe_unused,
80                                 struct rb_node *rb_node)
81 {
82         struct metric_event *me = container_of(rb_node, struct metric_event, nd);
83         struct metric_expr *expr, *tmp;
84
85         list_for_each_entry_safe(expr, tmp, &me->head, nd) {
86                 free(expr->metric_refs);
87                 free(expr);
88         }
89
90         free(me);
91 }
92
93 static void metricgroup__rblist_init(struct rblist *metric_events)
94 {
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;
99 }
100
101 void metricgroup__rblist_exit(struct rblist *metric_events)
102 {
103         rblist__exit(metric_events);
104 }
105
106 /*
107  * A node in the list of referenced metrics. metric_expr
108  * is held as a convenience to avoid a search through the
109  * metric list.
110  */
111 struct metric_ref_node {
112         const char *metric_name;
113         const char *metric_expr;
114         struct list_head list;
115 };
116
117 struct egroup {
118         struct list_head nd;
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;
124         int metric_refs_cnt;
125         int runtime;
126         bool has_constraint;
127 };
128
129 /**
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
139  * metrics.
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
143  * with the metric.
144  * @evlist_used: in/out argument, bitmap tracking which evlist events are used.
145  * @return the first metric event or NULL on failure.
146  */
147 static struct evsel *find_evsel_group(struct evlist *perf_evlist,
148                                       struct expr_parse_ctx *pctx,
149                                       bool metric_no_merge,
150                                       bool has_constraint,
151                                       struct evsel **metric_events,
152                                       unsigned long *evlist_used)
153 {
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);
158
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;
163         else
164                 events_to_match = idnum;
165
166         evlist__for_each_entry (perf_evlist, ev) {
167                 /*
168                  * Events with a constraint aren't grouped and match the first
169                  * events available.
170                  */
171                 if (has_constraint && ev->weak_group)
172                         continue;
173                 /* Ignore event if already used and merging is disabled. */
174                 if (metric_no_merge && test_bit(ev->idx, evlist_used))
175                         continue;
176                 if (!has_constraint && ev->leader != current_leader) {
177                         /*
178                          * Start of a new group, discard the whole match and
179                          * start again.
180                          */
181                         matched_events = 0;
182                         memset(metric_events, 0,
183                                 sizeof(struct evsel *) * idnum);
184                         current_leader = ev->leader;
185                 }
186                 if (hashmap__find(&pctx->ids, ev->name, (void **)&val_ptr)) {
187                         if (has_constraint) {
188                                 /*
189                                  * Events aren't grouped, ensure the same event
190                                  * isn't matched from two groups.
191                                  */
192                                 for (i = 0; i < matched_events; i++) {
193                                         if (!strcmp(ev->name,
194                                                     metric_events[i]->name)) {
195                                                 break;
196                                         }
197                                 }
198                                 if (i != matched_events)
199                                         continue;
200                         }
201                         metric_events[matched_events++] = ev;
202                 }
203                 if (matched_events == events_to_match)
204                         break;
205         }
206
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;
212                                 break;
213                         }
214                 }
215         }
216
217         if (matched_events != idnum) {
218                 /* Not whole match */
219                 return NULL;
220         }
221
222         metric_events[idnum] = NULL;
223
224         for (i = 0; i < idnum; i++) {
225                 ev = metric_events[i];
226                 ev->metric_leader = ev;
227                 set_bit(ev->idx, evlist_used);
228         }
229
230         return metric_events[0];
231 }
232
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)
237 {
238         struct metric_event *me;
239         struct metric_expr *expr;
240         int i = 0;
241         int ret = 0;
242         struct egroup *eg;
243         struct evsel *evsel, *tmp;
244         unsigned long *evlist_used;
245
246         evlist_used = bitmap_alloc(perf_evlist->core.nr_entries);
247         if (!evlist_used)
248                 return -ENOMEM;
249
250         list_for_each_entry (eg, groups, nd) {
251                 struct evsel **metric_events;
252                 struct metric_ref *metric_refs = NULL;
253
254                 metric_events = calloc(sizeof(void *),
255                                 hashmap__size(&eg->pctx.ids) + 1);
256                 if (!metric_events) {
257                         ret = -ENOMEM;
258                         break;
259                 }
260                 evsel = find_evsel_group(perf_evlist, &eg->pctx,
261                                          metric_no_merge,
262                                          eg->has_constraint, metric_events,
263                                          evlist_used);
264                 if (!evsel) {
265                         pr_debug("Cannot resolve %s: %s\n",
266                                         eg->metric_name, eg->metric_expr);
267                         free(metric_events);
268                         continue;
269                 }
270                 for (i = 0; metric_events[i]; i++)
271                         metric_events[i]->collect_stat = true;
272                 me = metricgroup__lookup(metric_events_list, evsel, true);
273                 if (!me) {
274                         ret = -ENOMEM;
275                         free(metric_events);
276                         break;
277                 }
278                 expr = malloc(sizeof(struct metric_expr));
279                 if (!expr) {
280                         ret = -ENOMEM;
281                         free(metric_events);
282                         break;
283                 }
284
285                 /*
286                  * Collect and store collected nested expressions
287                  * for metric processing.
288                  */
289                 if (eg->metric_refs_cnt) {
290                         struct metric_ref_node *ref;
291
292                         metric_refs = zalloc(sizeof(struct metric_ref) * (eg->metric_refs_cnt + 1));
293                         if (!metric_refs) {
294                                 ret = -ENOMEM;
295                                 free(metric_events);
296                                 break;
297                         }
298
299                         i = 0;
300                         list_for_each_entry(ref, &eg->metric_refs, list) {
301                                 /*
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.
306                                  */
307                                 metric_refs[i].metric_name = ref->metric_name;
308                                 metric_refs[i].metric_expr = ref->metric_expr;
309                                 i++;
310                         }
311                 };
312
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);
320         }
321
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);
326                 }
327         }
328         bitmap_free(evlist_used);
329
330         return ret;
331 }
332
333 static bool match_metric(const char *n, const char *list)
334 {
335         int len;
336         char *m;
337
338         if (!list)
339                 return false;
340         if (!strcmp(list, "all"))
341                 return true;
342         if (!n)
343                 return !strcasecmp(list, "No_group");
344         len = strlen(list);
345         m = strcasestr(n, list);
346         if (!m)
347                 return false;
348         if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
349             (m[len] == 0 || m[len] == ';'))
350                 return true;
351         return false;
352 }
353
354 struct mep {
355         struct rb_node nd;
356         const char *name;
357         struct strlist *metrics;
358 };
359
360 static int mep_cmp(struct rb_node *rb_node, const void *entry)
361 {
362         struct mep *a = container_of(rb_node, struct mep, nd);
363         struct mep *b = (struct mep *)entry;
364
365         return strcmp(a->name, b->name);
366 }
367
368 static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
369                                         const void *entry)
370 {
371         struct mep *me = malloc(sizeof(struct mep));
372
373         if (!me)
374                 return NULL;
375         memcpy(me, entry, sizeof(struct mep));
376         me->name = strdup(me->name);
377         if (!me->name)
378                 goto out_me;
379         me->metrics = strlist__new(NULL, NULL);
380         if (!me->metrics)
381                 goto out_name;
382         return &me->nd;
383 out_name:
384         zfree(&me->name);
385 out_me:
386         free(me);
387         return NULL;
388 }
389
390 static struct mep *mep_lookup(struct rblist *groups, const char *name)
391 {
392         struct rb_node *nd;
393         struct mep me = {
394                 .name = name
395         };
396         nd = rblist__find(groups, &me);
397         if (nd)
398                 return container_of(nd, struct mep, nd);
399         rblist__add_node(groups, &me);
400         nd = rblist__find(groups, &me);
401         if (nd)
402                 return container_of(nd, struct mep, nd);
403         return NULL;
404 }
405
406 static void mep_delete(struct rblist *rl __maybe_unused,
407                        struct rb_node *nd)
408 {
409         struct mep *me = container_of(nd, struct mep, nd);
410
411         strlist__delete(me->metrics);
412         zfree(&me->name);
413         free(me);
414 }
415
416 static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
417 {
418         struct str_node *sn;
419         int n = 0;
420
421         strlist__for_each_entry (sn, metrics) {
422                 if (raw)
423                         printf("%s%s", n > 0 ? " " : "", sn->s);
424                 else
425                         printf("  %s\n", sn->s);
426                 n++;
427         }
428         if (raw)
429                 putchar('\n');
430 }
431
432 void metricgroup__print(bool metrics, bool metricgroups, char *filter,
433                         bool raw, bool details)
434 {
435         struct pmu_events_map *map = perf_pmu__find_map(NULL);
436         struct pmu_event *pe;
437         int i;
438         struct rblist groups;
439         struct rb_node *node, *next;
440         struct strlist *metriclist = NULL;
441
442         if (!map)
443                 return;
444
445         if (!metricgroups) {
446                 metriclist = strlist__new(NULL, NULL);
447                 if (!metriclist)
448                         return;
449         }
450
451         rblist__init(&groups);
452         groups.node_new = mep_new;
453         groups.node_cmp = mep_cmp;
454         groups.node_delete = mep_delete;
455         for (i = 0; ; i++) {
456                 const char *g;
457                 pe = &map->table[i];
458
459                 if (!pe->name && !pe->metric_group && !pe->metric_name)
460                         break;
461                 if (!pe->metric_expr)
462                         continue;
463                 g = pe->metric_group;
464                 if (!g && pe->metric_name) {
465                         if (pe->name)
466                                 continue;
467                         g = "No_group";
468                 }
469                 if (g) {
470                         char *omg;
471                         char *mg = strdup(g);
472
473                         if (!mg)
474                                 return;
475                         omg = mg;
476                         while ((g = strsep(&mg, ";")) != NULL) {
477                                 struct mep *me;
478                                 char *s;
479
480                                 g = skip_spaces(g);
481                                 if (*g == 0)
482                                         g = "No_group";
483                                 if (filter && !strstr(g, filter))
484                                         continue;
485                                 if (raw)
486                                         s = (char *)pe->metric_name;
487                                 else {
488                                         if (asprintf(&s, "%s\n%*s%s]",
489                                                      pe->metric_name, 8, "[", pe->desc) < 0)
490                                                 return;
491
492                                         if (details) {
493                                                 if (asprintf(&s, "%s\n%*s%s]",
494                                                              s, 8, "[", pe->metric_expr) < 0)
495                                                         return;
496                                         }
497                                 }
498
499                                 if (!s)
500                                         continue;
501
502                                 if (!metricgroups) {
503                                         strlist__add(metriclist, s);
504                                 } else {
505                                         me = mep_lookup(&groups, g);
506                                         if (!me)
507                                                 continue;
508                                         strlist__add(me->metrics, s);
509                                 }
510                         }
511                         free(omg);
512                 }
513         }
514
515         if (metricgroups && !raw)
516                 printf("\nMetric Groups:\n\n");
517         else if (metrics && !raw)
518                 printf("\nMetrics:\n\n");
519
520         for (node = rb_first_cached(&groups.entries); node; node = next) {
521                 struct mep *me = container_of(node, struct mep, nd);
522
523                 if (metricgroups)
524                         printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
525                 if (metrics)
526                         metricgroup__print_strlist(me->metrics, raw);
527                 next = rb_next(node);
528                 rblist__remove_node(&groups, node);
529         }
530         if (!metricgroups)
531                 metricgroup__print_strlist(metriclist, raw);
532         strlist__delete(metriclist);
533 }
534
535 static void metricgroup__add_metric_weak_group(struct strbuf *events,
536                                                struct expr_parse_ctx *ctx)
537 {
538         struct hashmap_entry *cur;
539         size_t bkt;
540         bool no_group = true, has_duration = false;
541
542         hashmap__for_each_entry((&ctx->ids), cur, bkt) {
543                 pr_debug("found event %s\n", (const char *)cur->key);
544                 /*
545                  * Duration time maps to a software event and can make
546                  * groups not count. Always use it outside a
547                  * group.
548                  */
549                 if (!strcmp(cur->key, "duration_time")) {
550                         has_duration = true;
551                         continue;
552                 }
553                 strbuf_addf(events, "%s%s",
554                         no_group ? "{" : ",",
555                         (const char *)cur->key);
556                 no_group = false;
557         }
558         if (!no_group) {
559                 strbuf_addf(events, "}:W");
560                 if (has_duration)
561                         strbuf_addf(events, ",duration_time");
562         } else if (has_duration)
563                 strbuf_addf(events, "duration_time");
564 }
565
566 static void metricgroup__add_metric_non_group(struct strbuf *events,
567                                               struct expr_parse_ctx *ctx)
568 {
569         struct hashmap_entry *cur;
570         size_t bkt;
571         bool first = true;
572
573         hashmap__for_each_entry((&ctx->ids), cur, bkt) {
574                 if (!first)
575                         strbuf_addf(events, ",");
576                 strbuf_addf(events, "%s", (const char *)cur->key);
577                 first = false;
578         }
579 }
580
581 static void metricgroup___watchdog_constraint_hint(const char *name, bool foot)
582 {
583         static bool violate_nmi_constraint;
584
585         if (!foot) {
586                 pr_warning("Splitting metric group %s into standalone metrics.\n", name);
587                 violate_nmi_constraint = true;
588                 return;
589         }
590
591         if (!violate_nmi_constraint)
592                 return;
593
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"
596                    "    perf stat ...\n"
597                    "    echo 1 > /proc/sys/kernel/nmi_watchdog\n");
598 }
599
600 static bool metricgroup__has_constraint(struct pmu_event *pe)
601 {
602         if (!pe->metric_constraint)
603                 return false;
604
605         if (!strcmp(pe->metric_constraint, "NO_NMI_WATCHDOG") &&
606             sysctl__nmi_watchdog_enabled()) {
607                 metricgroup___watchdog_constraint_hint(pe->metric_name, false);
608                 return true;
609         }
610
611         return false;
612 }
613
614 int __weak arch_get_runtimeparam(void)
615 {
616         return 1;
617 }
618
619 static int __add_metric(struct list_head *group_list,
620                         struct pmu_event *pe,
621                         bool metric_no_group,
622                         int runtime,
623                         struct egroup **egp)
624 {
625         struct metric_ref_node *ref;
626         struct egroup *eg;
627
628         if (*egp == NULL) {
629                 /*
630                  * We got in here for the parent group,
631                  * allocate it and put it on the list.
632                  */
633                 eg = malloc(sizeof(*eg));
634                 if (!eg)
635                         return -ENOMEM;
636
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;
645                 *egp = eg;
646         } else {
647                 /*
648                  * We got here for the referenced metric, via the
649                  * recursive metricgroup__add_metric call, add
650                  * it to the parent group.
651                  */
652                 eg = *egp;
653
654                 ref = malloc(sizeof(*ref));
655                 if (!ref)
656                         return -ENOMEM;
657
658                 /*
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
662                  * our own copy.
663                  */
664                 ref->metric_name = pe->metric_name;
665                 ref->metric_expr = pe->metric_expr;
666
667                 list_add(&ref->list, &eg->metric_refs);
668                 eg->metric_refs_cnt++;
669         }
670
671         /*
672          * For both the parent and referenced metrics, we parse
673          * all the metric's IDs and add it to the parent context.
674          */
675         if (expr__find_other(pe->metric_expr, NULL, &eg->pctx, runtime) < 0) {
676                 expr__ctx_clear(&eg->pctx);
677                 free(eg);
678                 return -EINVAL;
679         }
680
681         /*
682          * We add new group only in the 'parent' call,
683          * so bail out for referenced metric case.
684          */
685         if (eg->metric_refs_cnt)
686                 return 0;
687
688         if (list_empty(group_list))
689                 list_add(&eg->nd, group_list);
690         else {
691                 struct list_head *pos;
692
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);
696
697                         if (hashmap__size(&eg->pctx.ids) <=
698                             hashmap__size(&old->pctx.ids))
699                                 break;
700                 }
701                 list_add(&eg->nd, pos);
702         }
703
704         return 0;
705 }
706
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])
711
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)))
717
718 static struct pmu_event *find_metric(const char *metric, struct pmu_events_map *map)
719 {
720         struct pmu_event *pe;
721         int i;
722
723         map_for_each_event(pe, i, map) {
724                 if (match_metric(pe->metric_name, metric))
725                         return pe;
726         }
727
728         return NULL;
729 }
730
731 static int add_metric(struct list_head *group_list,
732                       struct pmu_event *pe,
733                       bool metric_no_group,
734                       struct egroup **egp);
735
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)
740 {
741         struct hashmap_entry *cur;
742         size_t bkt;
743         bool all;
744         int ret;
745
746         /*
747          * Iterate all the parsed IDs and if there's metric,
748          * add it to the context.
749          */
750         do {
751                 all = true;
752                 hashmap__for_each_entry((&eg->pctx.ids), cur, bkt) {
753                         struct pmu_event *pe;
754
755                         pe = find_metric(cur->key, map);
756                         if (!pe)
757                                 continue;
758
759                         all = false;
760                         /* The metric key itself needs to go out.. */
761                         expr__del_id(&eg->pctx, cur->key);
762
763                         /* ... and it gets resolved to the parent context. */
764                         ret = add_metric(group_list, pe, metric_no_group, &eg);
765                         if (ret)
766                                 return ret;
767
768                         /*
769                          * We added new metric to hashmap, so we need
770                          * to break the iteration and start over.
771                          */
772                         break;
773                 }
774         } while (!all);
775
776         return 0;
777 }
778
779 static int resolve_metric(bool metric_no_group,
780                           struct list_head *metric_list,
781                           struct pmu_events_map *map)
782 {
783         struct egroup *eg;
784         int err;
785
786         list_for_each_entry(eg, metric_list, nd) {
787                 err = __resolve_metric(eg, metric_no_group, metric_list, map);
788                 if (err)
789                         return err;
790         }
791         return 0;
792 }
793
794 static int add_metric(struct list_head *group_list,
795                       struct pmu_event *pe,
796                       bool metric_no_group,
797                       struct egroup **egp)
798 {
799         struct egroup *orig = *egp;
800         int ret = 0;
801
802         pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
803
804         if (!strstr(pe->metric_expr, "?")) {
805                 ret = __add_metric(group_list, pe, metric_no_group, 1, egp);
806         } else {
807                 int j, count;
808
809                 count = arch_get_runtimeparam();
810
811                 /* This loop is added to create multiple
812                  * events depend on count value and add
813                  * those events to group_list.
814                  */
815
816                 for (j = 0; j < count && !ret; j++, *egp = orig)
817                         ret = __add_metric(group_list, pe, metric_no_group, j, egp);
818         }
819
820         return ret;
821 }
822
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)
827 {
828         struct pmu_event *pe;
829         struct egroup *eg;
830         int i, ret;
831         bool has_match = false;
832
833         map_for_each_metric(pe, i, map, metric) {
834                 has_match = true;
835                 eg = NULL;
836
837                 ret = add_metric(group_list, pe, metric_no_group, &eg);
838                 if (ret)
839                         return ret;
840
841                 /*
842                  * Process any possible referenced metrics
843                  * included in the expression.
844                  */
845                 ret = resolve_metric(metric_no_group,
846                                      group_list, map);
847                 if (ret)
848                         return ret;
849         }
850
851         /* End of pmu events. */
852         if (!has_match)
853                 return -EINVAL;
854
855         list_for_each_entry(eg, group_list, nd) {
856                 if (events->len > 0)
857                         strbuf_addf(events, ",");
858
859                 if (eg->has_constraint) {
860                         metricgroup__add_metric_non_group(events,
861                                                           &eg->pctx);
862                 } else {
863                         metricgroup__add_metric_weak_group(events,
864                                                            &eg->pctx);
865                 }
866         }
867         return 0;
868 }
869
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)
874 {
875         char *llist, *nlist, *p;
876         int ret = -EINVAL;
877
878         nlist = strdup(list);
879         if (!nlist)
880                 return -ENOMEM;
881         llist = nlist;
882
883         strbuf_init(events, 100);
884         strbuf_addf(events, "%s", "");
885
886         while ((p = strsep(&llist, ",")) != NULL) {
887                 ret = metricgroup__add_metric(p, metric_no_group, events,
888                                               group_list, map);
889                 if (ret == -EINVAL) {
890                         fprintf(stderr, "Cannot find metric or group `%s'\n",
891                                         p);
892                         break;
893                 }
894         }
895         free(nlist);
896
897         if (!ret)
898                 metricgroup___watchdog_constraint_hint(NULL, true);
899
900         return ret;
901 }
902
903 static void egroup__free_refs(struct egroup *egroup)
904 {
905         struct metric_ref_node *ref, *tmp;
906
907         list_for_each_entry_safe(ref, tmp, &egroup->metric_refs, list) {
908                 list_del(&ref->list);
909                 free(ref);
910         }
911 }
912
913 static void metricgroup__free_egroups(struct list_head *group_list)
914 {
915         struct egroup *eg, *egtmp;
916
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);
921                 free(eg);
922         }
923 }
924
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)
931 {
932         struct parse_events_error parse_error;
933         struct strbuf extra_events;
934         LIST_HEAD(group_list);
935         int ret;
936
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);
941         if (ret)
942                 return ret;
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);
946         if (ret) {
947                 parse_events_print_error(&parse_error, extra_events.buf);
948                 goto out;
949         }
950         strbuf_release(&extra_events);
951         ret = metricgroup__setup_events(&group_list, metric_no_merge,
952                                         perf_evlist, metric_events);
953 out:
954         metricgroup__free_egroups(&group_list);
955         return ret;
956 }
957
958 int metricgroup__parse_groups(const struct option *opt,
959                               const char *str,
960                               bool metric_no_group,
961                               bool metric_no_merge,
962                               struct rblist *metric_events)
963 {
964         struct evlist *perf_evlist = *(struct evlist **)opt->value;
965         struct pmu_events_map *map = perf_pmu__find_map(NULL);
966
967         if (!map)
968                 return 0;
969
970         return parse_groups(perf_evlist, str, metric_no_group,
971                             metric_no_merge, NULL, metric_events, map);
972 }
973
974 int metricgroup__parse_groups_test(struct evlist *evlist,
975                                    struct pmu_events_map *map,
976                                    const char *str,
977                                    bool metric_no_group,
978                                    bool metric_no_merge,
979                                    struct rblist *metric_events)
980 {
981         return parse_groups(evlist, str, metric_no_group,
982                             metric_no_merge, &perf_pmu__fake, metric_events, map);
983 }
984
985 bool metricgroup__has_metric(const char *metric)
986 {
987         struct pmu_events_map *map = perf_pmu__find_map(NULL);
988         struct pmu_event *pe;
989         int i;
990
991         if (!map)
992                 return false;
993
994         for (i = 0; ; i++) {
995                 pe = &map->table[i];
996
997                 if (!pe->name && !pe->metric_group && !pe->metric_name)
998                         break;
999                 if (!pe->metric_expr)
1000                         continue;
1001                 if (match_metric(pe->metric_name, metric))
1002                         return true;
1003         }
1004         return false;
1005 }