Linux 6.12-rc1
[linux-block.git] / tools / perf / util / metricgroup.c
CommitLineData
2025cf9e 1// SPDX-License-Identifier: GPL-2.0-only
b18f3e36
AK
2/*
3 * Copyright (c) 2017, Intel Corporation.
b18f3e36
AK
4 */
5
6/* Manage metrics and groups of metrics from JSON files */
7
8#include "metricgroup.h"
b4209025 9#include "debug.h"
b18f3e36 10#include "evlist.h"
0b8026e8 11#include "evsel.h"
b18f3e36
AK
12#include "strbuf.h"
13#include "pmu.h"
1eaf496e 14#include "pmus.h"
e5c6109f 15#include "print-events.h"
90053634 16#include "smt.h"
b18f3e36
AK
17#include "expr.h"
18#include "rblist.h"
b18f3e36 19#include <string.h>
b18f3e36 20#include <errno.h>
b18f3e36
AK
21#include "strlist.h"
22#include <assert.h>
bd9860bf 23#include <linux/ctype.h>
80be6434 24#include <linux/list_sort.h>
b4209025 25#include <linux/string.h>
d8f9da24 26#include <linux/zalloc.h>
1725e9cd 27#include <perf/cpumap.h>
0b8026e8 28#include <subcmd/parse-options.h>
ab483d8b
KL
29#include <api/fs/fs.h>
30#include "util.h"
f6fb0960 31#include <asm/bug.h>
b214ba8c 32#include "cgroup.h"
bd560973 33#include "util/hashmap.h"
b18f3e36
AK
34
35struct metric_event *metricgroup__lookup(struct rblist *metric_events,
32dcd021 36 struct evsel *evsel,
b18f3e36
AK
37 bool create)
38{
39 struct rb_node *nd;
40 struct metric_event me = {
41 .evsel = evsel
42 };
4bd1bef8
AK
43
44 if (!metric_events)
45 return NULL;
46
97b6b4ac 47 if (evsel && evsel->metric_leader)
a59fb796 48 me.evsel = evsel->metric_leader;
b18f3e36
AK
49 nd = rblist__find(metric_events, &me);
50 if (nd)
51 return container_of(nd, struct metric_event, nd);
52 if (create) {
53 rblist__add_node(metric_events, &me);
54 nd = rblist__find(metric_events, &me);
55 if (nd)
56 return container_of(nd, struct metric_event, nd);
57 }
58 return NULL;
59}
60
61static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
62{
63 struct metric_event *a = container_of(rb_node,
64 struct metric_event,
65 nd);
66 const struct metric_event *b = entry;
67
68 if (a->evsel == b->evsel)
69 return 0;
70 if ((char *)a->evsel < (char *)b->evsel)
71 return -1;
72 return +1;
73}
74
75static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
76 const void *entry)
77{
78 struct metric_event *me = malloc(sizeof(struct metric_event));
79
80 if (!me)
81 return NULL;
82 memcpy(me, entry, sizeof(struct metric_event));
83 me->evsel = ((struct metric_event *)entry)->evsel;
1c0e4795 84 me->is_default = false;
b18f3e36
AK
85 INIT_LIST_HEAD(&me->head);
86 return &me->nd;
87}
88
9afe5658
JO
89static void metric_event_delete(struct rblist *rblist __maybe_unused,
90 struct rb_node *rb_node)
91{
92 struct metric_event *me = container_of(rb_node, struct metric_event, nd);
93 struct metric_expr *expr, *tmp;
94
95 list_for_each_entry_safe(expr, tmp, &me->head, nd) {
11ff9bcd
ACM
96 zfree(&expr->metric_name);
97 zfree(&expr->metric_refs);
98 zfree(&expr->metric_events);
9afe5658
JO
99 free(expr);
100 }
101
102 free(me);
103}
104
b18f3e36
AK
105static void metricgroup__rblist_init(struct rblist *metric_events)
106{
107 rblist__init(metric_events);
108 metric_events->node_cmp = metric_event_cmp;
109 metric_events->node_new = metric_event_new;
9afe5658
JO
110 metric_events->node_delete = metric_event_delete;
111}
112
113void metricgroup__rblist_exit(struct rblist *metric_events)
114{
115 rblist__exit(metric_events);
b18f3e36
AK
116}
117
485fcaed
IR
118/**
119 * The metric under construction. The data held here will be placed in a
120 * metric_expr.
121 */
a0c05b36 122struct metric {
b18f3e36 123 struct list_head nd;
485fcaed
IR
124 /**
125 * The expression parse context importantly holding the IDs contained
126 * within the expression.
127 */
cb94a02e 128 struct expr_parse_ctx *pctx;
bd3846d0 129 const char *pmu;
485fcaed 130 /** The name of the metric such as "IPC". */
b18f3e36 131 const char *metric_name;
b85a4d61
IR
132 /** Modifier on the metric such as "u" or NULL for none. */
133 const char *modifier;
485fcaed 134 /** The expression to parse, for example, "instructions/cycles". */
b18f3e36 135 const char *metric_expr;
d0a3052f
IR
136 /** Optional threshold expression where zero value is green, otherwise red. */
137 const char *metric_threshold;
485fcaed
IR
138 /**
139 * The "ScaleUnit" that scales and adds a unit to the metric during
140 * output.
141 */
287f2649 142 const char *metric_unit;
b0a9e8f8
KL
143 /**
144 * Optional name of the metric group reported
145 * if the Default metric group is being processed.
146 */
147 const char *default_metricgroup_name;
46bdc0bf
IR
148 /** Optional null terminated array of referenced metrics. */
149 struct metric_ref *metric_refs;
485fcaed 150 /**
180a5013 151 * Should events of the metric be grouped?
485fcaed 152 */
180a5013 153 bool group_events;
5ecd5a0c
IR
154 /**
155 * Parsed events for the metric. Optional as events may be taken from a
156 * different metric whose group contains all the IDs necessary for this
157 * one.
158 */
159 struct evlist *evlist;
b18f3e36
AK
160};
161
180a5013 162static void metric__watchdog_constraint_hint(const char *name, bool foot)
3d81d761
IR
163{
164 static bool violate_nmi_constraint;
165
166 if (!foot) {
180a5013 167 pr_warning("Not grouping metric %s's events.\n", name);
3d81d761
IR
168 violate_nmi_constraint = true;
169 return;
170 }
171
172 if (!violate_nmi_constraint)
173 return;
174
175 pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n"
176 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
177 " perf stat ...\n"
178 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
179}
180
180a5013 181static bool metric__group_events(const struct pmu_metric *pm)
3d81d761 182{
90053634
IR
183 switch (pm->event_grouping) {
184 case MetricNoGroupEvents:
185 return false;
186 case MetricNoGroupEventsNmi:
187 if (!sysctl__nmi_watchdog_enabled())
188 return true;
180a5013
IR
189 metric__watchdog_constraint_hint(pm->metric_name, /*foot=*/false);
190 return false;
90053634
IR
191 case MetricNoGroupEventsSmt:
192 return !smt_on();
193 case MetricGroupEvents:
194 default:
195 return true;
3d81d761 196 }
3d81d761
IR
197}
198
1725e9cd
IR
199static void metric__free(struct metric *m)
200{
201 if (!m)
202 return;
203
11ff9bcd 204 zfree(&m->metric_refs);
1725e9cd 205 expr__ctx_free(m->pctx);
11ff9bcd 206 zfree(&m->modifier);
1725e9cd
IR
207 evlist__delete(m->evlist);
208 free(m);
209}
210
db95818e 211static struct metric *metric__new(const struct pmu_metric *pm,
b85a4d61 212 const char *modifier,
3d81d761 213 bool metric_no_group,
1725e9cd
IR
214 int runtime,
215 const char *user_requested_cpu_list,
216 bool system_wide)
3d81d761
IR
217{
218 struct metric *m;
219
220 m = zalloc(sizeof(*m));
221 if (!m)
222 return NULL;
223
224 m->pctx = expr__ctx_new();
1725e9cd
IR
225 if (!m->pctx)
226 goto out_err;
3d81d761 227
bd3846d0 228 m->pmu = pm->pmu ?: "cpu";
db95818e 229 m->metric_name = pm->metric_name;
e2b005d6 230 m->default_metricgroup_name = pm->default_metricgroup_name ?: "";
1725e9cd
IR
231 m->modifier = NULL;
232 if (modifier) {
233 m->modifier = strdup(modifier);
234 if (!m->modifier)
235 goto out_err;
b85a4d61 236 }
db95818e 237 m->metric_expr = pm->metric_expr;
d0a3052f 238 m->metric_threshold = pm->metric_threshold;
db95818e 239 m->metric_unit = pm->unit;
1725e9cd
IR
240 m->pctx->sctx.user_requested_cpu_list = NULL;
241 if (user_requested_cpu_list) {
242 m->pctx->sctx.user_requested_cpu_list = strdup(user_requested_cpu_list);
243 if (!m->pctx->sctx.user_requested_cpu_list)
244 goto out_err;
245 }
1a6abdde 246 m->pctx->sctx.runtime = runtime;
1725e9cd 247 m->pctx->sctx.system_wide = system_wide;
180a5013 248 m->group_events = !metric_no_group && metric__group_events(pm);
46bdc0bf 249 m->metric_refs = NULL;
5ecd5a0c 250 m->evlist = NULL;
3d81d761
IR
251
252 return m;
1725e9cd
IR
253out_err:
254 metric__free(m);
255 return NULL;
3d81d761
IR
256}
257
ec5c5b3d
IR
258static bool contains_metric_id(struct evsel **metric_events, int num_events,
259 const char *metric_id)
dcc81be0
IR
260{
261 int i;
262
263 for (i = 0; i < num_events; i++) {
ec5c5b3d 264 if (!strcmp(evsel__metric_id(metric_events[i]), metric_id))
dcc81be0
IR
265 return true;
266 }
267 return false;
268}
269
2440689d 270/**
5ecd5a0c
IR
271 * setup_metric_events - Find a group of events in metric_evlist that correspond
272 * to the IDs from a parsed metric expression.
bd3846d0 273 * @pmu: The PMU for the IDs.
5ecd5a0c
IR
274 * @ids: the metric IDs to match.
275 * @metric_evlist: the list of perf events.
276 * @out_metric_events: holds the created metric events array.
2440689d 277 */
bd3846d0 278static int setup_metric_events(const char *pmu, struct hashmap *ids,
5ecd5a0c 279 struct evlist *metric_evlist,
17b3867d 280 struct evsel ***out_metric_events)
b18f3e36 281{
5ecd5a0c 282 struct evsel **metric_events;
ec5c5b3d 283 const char *metric_id;
5ecd5a0c
IR
284 struct evsel *ev;
285 size_t ids_size, matched_events, i;
94f9eb95 286 bool all_pmus = !strcmp(pmu, "all") || perf_pmus__num_core_pmus() == 1 || !is_pmu_core(pmu);
2440689d 287
5ecd5a0c
IR
288 *out_metric_events = NULL;
289 ids_size = hashmap__size(ids);
762a05c5 290
7bbe8f00 291 metric_events = calloc(ids_size + 1, sizeof(void *));
5ecd5a0c
IR
292 if (!metric_events)
293 return -ENOMEM;
294
295 matched_events = 0;
296 evlist__for_each_entry(metric_evlist, ev) {
297 struct expr_id_data *val_ptr;
762a05c5 298
8a4859c5 299 /* Don't match events for the wrong hybrid PMU. */
ec6a4a8b 300 if (!all_pmus && ev->pmu_name && evsel__is_hybrid(ev) &&
8a4859c5 301 strcmp(ev->pmu_name, pmu))
bd3846d0 302 continue;
dcc81be0 303 /*
5ecd5a0c
IR
304 * Check for duplicate events with the same name. For
305 * example, uncore_imc/cas_count_read/ will turn into 6
306 * events per socket on skylakex. Only the first such
307 * event is placed in metric_events.
dcc81be0 308 */
5ecd5a0c
IR
309 metric_id = evsel__metric_id(ev);
310 if (contains_metric_id(metric_events, matched_events, metric_id))
311 continue;
312 /*
313 * Does this event belong to the parse context? For
314 * combined or shared groups, this metric may not care
315 * about this event.
316 */
c302378b 317 if (hashmap__find(ids, metric_id, &val_ptr)) {
8a4859c5 318 pr_debug("Matched metric-id %s to %s\n", metric_id, evsel__name(ev));
762a05c5 319 metric_events[matched_events++] = ev;
f01642e4 320
5ecd5a0c
IR
321 if (matched_events >= ids_size)
322 break;
323 }
f01642e4 324 }
5ecd5a0c
IR
325 if (matched_events < ids_size) {
326 free(metric_events);
327 return -EINVAL;
328 }
329 for (i = 0; i < ids_size; i++) {
58fc90fd 330 ev = metric_events[i];
5ecd5a0c
IR
331 ev->collect_stat = true;
332
dcc81be0 333 /*
5ecd5a0c
IR
334 * The metric leader points to the identically named
335 * event in metric_events.
dcc81be0
IR
336 */
337 ev->metric_leader = ev;
338 /*
5ecd5a0c
IR
339 * Mark two events with identical names in the same
340 * group (or globally) as being in use as uncore events
341 * may be duplicated for each pmu. Set the metric leader
342 * of such events to be the event that appears in
343 * metric_events.
dcc81be0 344 */
ec5c5b3d 345 metric_id = evsel__metric_id(ev);
5ecd5a0c 346 evlist__for_each_entry_continue(metric_evlist, ev) {
d3e2bb43 347 if (!strcmp(evsel__metric_id(ev), metric_id))
dcc81be0 348 ev->metric_leader = metric_events[i];
dcc81be0 349 }
f01642e4 350 }
5ecd5a0c
IR
351 *out_metric_events = metric_events;
352 return 0;
b18f3e36
AK
353}
354
d4be39ca 355static bool match_metric(const char *metric_or_groups, const char *sought)
b18f3e36
AK
356{
357 int len;
358 char *m;
359
d4be39ca 360 if (!sought)
b18f3e36 361 return false;
d4be39ca 362 if (!strcmp(sought, "all"))
b18f3e36 363 return true;
d4be39ca
IR
364 if (!metric_or_groups)
365 return !strcasecmp(sought, "No_group");
366 len = strlen(sought);
367 if (!strncasecmp(metric_or_groups, sought, len) &&
368 (metric_or_groups[len] == 0 || metric_or_groups[len] == ';'))
b18f3e36 369 return true;
d4be39ca
IR
370 m = strchr(metric_or_groups, ';');
371 return m && match_metric(m + 1, sought);
b18f3e36
AK
372}
373
bd3846d0 374static bool match_pm_metric(const struct pmu_metric *pm, const char *pmu, const char *metric)
be335ec2 375{
bd3846d0
IR
376 const char *pm_pmu = pm->pmu ?: "cpu";
377
378 if (strcmp(pmu, "all") && strcmp(pm_pmu, pmu))
379 return false;
380
db95818e
IR
381 return match_metric(pm->metric_group, metric) ||
382 match_metric(pm->metric_name, metric);
be335ec2
JG
383}
384
e5c6109f 385/** struct mep - RB-tree node for building printing information. */
71b0acce 386struct mep {
e5c6109f 387 /** nd - RB-tree element. */
71b0acce 388 struct rb_node nd;
e5c6109f
IR
389 /** @metric_group: Owned metric group name, separated others with ';'. */
390 char *metric_group;
391 const char *metric_name;
392 const char *metric_desc;
393 const char *metric_long_desc;
394 const char *metric_expr;
c7551a2e 395 const char *metric_threshold;
e5c6109f 396 const char *metric_unit;
71b0acce
AK
397};
398
399static int mep_cmp(struct rb_node *rb_node, const void *entry)
400{
401 struct mep *a = container_of(rb_node, struct mep, nd);
402 struct mep *b = (struct mep *)entry;
e5c6109f
IR
403 int ret;
404
405 ret = strcmp(a->metric_group, b->metric_group);
406 if (ret)
407 return ret;
71b0acce 408
e5c6109f 409 return strcmp(a->metric_name, b->metric_name);
71b0acce
AK
410}
411
e5c6109f 412static struct rb_node *mep_new(struct rblist *rl __maybe_unused, const void *entry)
71b0acce
AK
413{
414 struct mep *me = malloc(sizeof(struct mep));
415
416 if (!me)
417 return NULL;
e5c6109f 418
71b0acce 419 memcpy(me, entry, sizeof(struct mep));
71b0acce 420 return &me->nd;
e5c6109f
IR
421}
422
423static void mep_delete(struct rblist *rl __maybe_unused,
424 struct rb_node *nd)
425{
426 struct mep *me = container_of(nd, struct mep, nd);
427
428 zfree(&me->metric_group);
71b0acce 429 free(me);
71b0acce
AK
430}
431
e5c6109f
IR
432static struct mep *mep_lookup(struct rblist *groups, const char *metric_group,
433 const char *metric_name)
71b0acce
AK
434{
435 struct rb_node *nd;
436 struct mep me = {
e5c6109f
IR
437 .metric_group = strdup(metric_group),
438 .metric_name = metric_name,
71b0acce
AK
439 };
440 nd = rblist__find(groups, &me);
e5c6109f
IR
441 if (nd) {
442 free(me.metric_group);
71b0acce 443 return container_of(nd, struct mep, nd);
e5c6109f 444 }
71b0acce
AK
445 rblist__add_node(groups, &me);
446 nd = rblist__find(groups, &me);
447 if (nd)
448 return container_of(nd, struct mep, nd);
449 return NULL;
450}
451
db95818e 452static int metricgroup__add_to_mep_groups(const struct pmu_metric *pm,
e5c6109f 453 struct rblist *groups)
f6fe1e48
JG
454{
455 const char *g;
456 char *omg, *mg;
457
4b5ee6db 458 mg = strdup(pm->metric_group ?: pm->metric_name);
f6fe1e48
JG
459 if (!mg)
460 return -ENOMEM;
461 omg = mg;
462 while ((g = strsep(&mg, ";")) != NULL) {
463 struct mep *me;
f6fe1e48
JG
464
465 g = skip_spaces(g);
e5c6109f 466 if (strlen(g))
db95818e 467 me = mep_lookup(groups, g, pm->metric_name);
e5c6109f 468 else
4b5ee6db 469 me = mep_lookup(groups, pm->metric_name, pm->metric_name);
f6fe1e48 470
e5c6109f 471 if (me) {
db95818e
IR
472 me->metric_desc = pm->desc;
473 me->metric_long_desc = pm->long_desc;
474 me->metric_expr = pm->metric_expr;
c7551a2e 475 me->metric_threshold = pm->metric_threshold;
db95818e 476 me->metric_unit = pm->unit;
f6fe1e48 477 }
f6fe1e48
JG
478 }
479 free(omg);
480
481 return 0;
482}
483
a36fadb1 484struct metricgroup_iter_data {
db95818e 485 pmu_metric_iter_fn fn;
a36fadb1
JG
486 void *data;
487};
488
db95818e 489static int metricgroup__sys_event_iter(const struct pmu_metric *pm,
f8ea2c15 490 const struct pmu_metrics_table *table,
29be2fe0 491 void *data)
a36fadb1
JG
492{
493 struct metricgroup_iter_data *d = data;
494 struct perf_pmu *pmu = NULL;
495
db95818e 496 if (!pm->metric_expr || !pm->compat)
a36fadb1
JG
497 return 0;
498
1eaf496e 499 while ((pmu = perf_pmus__scan(pmu))) {
a36fadb1 500
54409997 501 if (!pmu->id || !pmu_uncore_identifier_match(pm->compat, pmu->id))
a36fadb1
JG
502 continue;
503
db95818e 504 return d->fn(pm, table, d->data);
a36fadb1 505 }
a36fadb1
JG
506 return 0;
507}
508
db95818e 509static int metricgroup__add_to_mep_groups_callback(const struct pmu_metric *pm,
f8ea2c15
IR
510 const struct pmu_metrics_table *table __maybe_unused,
511 void *vdata)
660842e4 512{
e5c6109f 513 struct rblist *groups = vdata;
660842e4 514
db95818e 515 return metricgroup__add_to_mep_groups(pm, groups);
660842e4
IR
516}
517
e5c6109f 518void metricgroup__print(const struct print_callbacks *print_cb, void *print_state)
71b0acce 519{
71b0acce 520 struct rblist groups;
f8ea2c15 521 const struct pmu_metrics_table *table;
e5c6109f 522 struct rb_node *node, *next;
71b0acce
AK
523
524 rblist__init(&groups);
525 groups.node_new = mep_new;
526 groups.node_cmp = mep_cmp;
527 groups.node_delete = mep_delete;
f8ea2c15 528 table = pmu_metrics_table__find();
660842e4 529 if (table) {
4000519e 530 pmu_metrics_table__for_each_metric(table,
db95818e
IR
531 metricgroup__add_to_mep_groups_callback,
532 &groups);
71b0acce 533 }
a36fadb1
JG
534 {
535 struct metricgroup_iter_data data = {
e5c6109f
IR
536 .fn = metricgroup__add_to_mep_groups_callback,
537 .data = &groups,
a36fadb1 538 };
db95818e 539 pmu_for_each_sys_metric(metricgroup__sys_event_iter, &data);
a36fadb1
JG
540 }
541
ca227029 542 for (node = rb_first_cached(&groups.entries); node; node = next) {
71b0acce
AK
543 struct mep *me = container_of(node, struct mep, nd);
544
e5c6109f
IR
545 print_cb->print_metric(print_state,
546 me->metric_group,
547 me->metric_name,
548 me->metric_desc,
549 me->metric_long_desc,
550 me->metric_expr,
c7551a2e 551 me->metric_threshold,
e5c6109f 552 me->metric_unit);
71b0acce
AK
553 next = rb_next(node);
554 rblist__remove_node(&groups, node);
555 }
71b0acce
AK
556}
557
ec5c5b3d
IR
558static const char *code_characters = ",-=@";
559
560static int encode_metric_id(struct strbuf *sb, const char *x)
561{
562 char *c;
563 int ret = 0;
564
565 for (; *x; x++) {
566 c = strchr(code_characters, *x);
567 if (c) {
568 ret = strbuf_addch(sb, '!');
569 if (ret)
570 break;
571
572 ret = strbuf_addch(sb, '0' + (c - code_characters));
573 if (ret)
574 break;
575 } else {
576 ret = strbuf_addch(sb, *x);
577 if (ret)
578 break;
579 }
580 }
581 return ret;
582}
583
584static int decode_metric_id(struct strbuf *sb, const char *x)
585{
586 const char *orig = x;
587 size_t i;
588 char c;
589 int ret;
590
591 for (; *x; x++) {
592 c = *x;
593 if (*x == '!') {
594 x++;
595 i = *x - '0';
596 if (i > strlen(code_characters)) {
597 pr_err("Bad metric-id encoding in: '%s'", orig);
598 return -1;
599 }
600 c = code_characters[i];
601 }
602 ret = strbuf_addch(sb, c);
603 if (ret)
604 return ret;
605 }
606 return 0;
607}
608
b85a4d61 609static int decode_all_metric_ids(struct evlist *perf_evlist, const char *modifier)
ec5c5b3d
IR
610{
611 struct evsel *ev;
612 struct strbuf sb = STRBUF_INIT;
613 char *cur;
614 int ret = 0;
615
616 evlist__for_each_entry(perf_evlist, ev) {
617 if (!ev->metric_id)
618 continue;
619
620 ret = strbuf_setlen(&sb, 0);
621 if (ret)
622 break;
623
624 ret = decode_metric_id(&sb, ev->metric_id);
625 if (ret)
626 break;
627
628 free((char *)ev->metric_id);
629 ev->metric_id = strdup(sb.buf);
630 if (!ev->metric_id) {
631 ret = -ENOMEM;
632 break;
633 }
634 /*
635 * If the name is just the parsed event, use the metric-id to
636 * give a more friendly display version.
637 */
638 if (strstr(ev->name, "metric-id=")) {
b85a4d61
IR
639 bool has_slash = false;
640
11ff9bcd 641 zfree(&ev->name);
b85a4d61 642 for (cur = strchr(sb.buf, '@') ; cur; cur = strchr(++cur, '@')) {
ec5c5b3d 643 *cur = '/';
b85a4d61
IR
644 has_slash = true;
645 }
ec5c5b3d 646
b85a4d61
IR
647 if (modifier) {
648 if (!has_slash && !strchr(sb.buf, ':')) {
649 ret = strbuf_addch(&sb, ':');
650 if (ret)
651 break;
652 }
653 ret = strbuf_addstr(&sb, modifier);
654 if (ret)
655 break;
656 }
ec5c5b3d
IR
657 ev->name = strdup(sb.buf);
658 if (!ev->name) {
659 ret = -ENOMEM;
660 break;
661 }
662 }
663 }
664 strbuf_release(&sb);
665 return ret;
666}
667
668static int metricgroup__build_event_string(struct strbuf *events,
669 const struct expr_parse_ctx *ctx,
b85a4d61 670 const char *modifier,
180a5013 671 bool group_events)
f742634a 672{
ded80bda 673 struct hashmap_entry *cur;
4e21c13a 674 size_t bkt;
9aa09230
IR
675 bool no_group = true, has_tool_events = false;
676 bool tool_events[PERF_TOOL_MAX] = {false};
ec5c5b3d
IR
677 int ret = 0;
678
679#define RETURN_IF_NON_ZERO(x) do { if (x) return x; } while (0)
f742634a 680
cb94a02e 681 hashmap__for_each_entry(ctx->ids, cur, bkt) {
c302378b 682 const char *sep, *rsep, *id = cur->pkey;
9aa09230 683 enum perf_tool_event ev;
ec5c5b3d
IR
684
685 pr_debug("found event %s\n", id);
9aa09230
IR
686
687 /* Always move tool events outside of the group. */
688 ev = perf_tool_event__from_str(id);
689 if (ev != PERF_TOOL_NONE) {
690 has_tool_events = true;
691 tool_events[ev] = true;
f742634a
KL
692 continue;
693 }
ec5c5b3d
IR
694 /* Separate events with commas and open the group if necessary. */
695 if (no_group) {
180a5013 696 if (group_events) {
ec5c5b3d
IR
697 ret = strbuf_addch(events, '{');
698 RETURN_IF_NON_ZERO(ret);
699 }
ab483d8b 700
ec5c5b3d
IR
701 no_group = false;
702 } else {
703 ret = strbuf_addch(events, ',');
704 RETURN_IF_NON_ZERO(ret);
705 }
706 /*
707 * Encode the ID as an event string. Add a qualifier for
708 * metric_id that is the original name except with characters
709 * that parse-events can't parse replaced. For example,
710 * 'msr@tsc@' gets added as msr/tsc,metric-id=msr!3tsc!3/
711 */
712 sep = strchr(id, '@');
713 if (sep != NULL) {
714 ret = strbuf_add(events, id, sep - id);
715 RETURN_IF_NON_ZERO(ret);
716 ret = strbuf_addch(events, '/');
717 RETURN_IF_NON_ZERO(ret);
718 rsep = strrchr(sep, '@');
719 ret = strbuf_add(events, sep + 1, rsep - sep - 1);
720 RETURN_IF_NON_ZERO(ret);
721 ret = strbuf_addstr(events, ",metric-id=");
722 RETURN_IF_NON_ZERO(ret);
723 sep = rsep;
724 } else {
725 sep = strchr(id, ':');
726 if (sep != NULL) {
727 ret = strbuf_add(events, id, sep - id);
728 RETURN_IF_NON_ZERO(ret);
729 } else {
730 ret = strbuf_addstr(events, id);
731 RETURN_IF_NON_ZERO(ret);
732 }
733 ret = strbuf_addstr(events, "/metric-id=");
734 RETURN_IF_NON_ZERO(ret);
735 }
736 ret = encode_metric_id(events, id);
737 RETURN_IF_NON_ZERO(ret);
738 ret = strbuf_addstr(events, "/");
739 RETURN_IF_NON_ZERO(ret);
740
741 if (sep != NULL) {
742 ret = strbuf_addstr(events, sep + 1);
743 RETURN_IF_NON_ZERO(ret);
744 }
b85a4d61
IR
745 if (modifier) {
746 ret = strbuf_addstr(events, modifier);
747 RETURN_IF_NON_ZERO(ret);
748 }
e2ce1059 749 }
180a5013 750 if (!no_group && group_events) {
ec5c5b3d 751 ret = strbuf_addf(events, "}:W");
9aa09230
IR
752 RETURN_IF_NON_ZERO(ret);
753 }
754 if (has_tool_events) {
755 int i;
756
757 perf_tool_event__for_each_event(i) {
758 if (tool_events[i]) {
759 if (!no_group) {
760 ret = strbuf_addch(events, ',');
761 RETURN_IF_NON_ZERO(ret);
762 }
763 no_group = false;
764 ret = strbuf_addstr(events, perf_tool_event__to_str(i));
765 RETURN_IF_NON_ZERO(ret);
766 }
767 }
768 }
ec5c5b3d
IR
769
770 return ret;
771#undef RETURN_IF_NON_ZERO
ab483d8b
KL
772}
773
db95818e 774int __weak arch_get_runtimeparam(const struct pmu_metric *pm __maybe_unused)
1e1a873d
KJ
775{
776 return 1;
777}
778
80be6434
IR
779/*
780 * A singly linked list on the stack of the names of metrics being
781 * processed. Used to identify recursion.
782 */
783struct visited_metric {
784 const char *name;
785 const struct visited_metric *parent;
786};
787
be335ec2
JG
788struct metricgroup_add_iter_data {
789 struct list_head *metric_list;
bd3846d0 790 const char *pmu;
68074811 791 const char *metric_name;
b85a4d61 792 const char *modifier;
be335ec2
JG
793 int *ret;
794 bool *has_match;
795 bool metric_no_group;
1fd09e29 796 bool metric_no_threshold;
1725e9cd
IR
797 const char *user_requested_cpu_list;
798 bool system_wide;
80be6434
IR
799 struct metric *root_metric;
800 const struct visited_metric *visited;
f8ea2c15 801 const struct pmu_metrics_table *table;
be335ec2
JG
802};
803
bd3846d0
IR
804static bool metricgroup__find_metric(const char *pmu,
805 const char *metric,
f8ea2c15 806 const struct pmu_metrics_table *table,
db95818e 807 struct pmu_metric *pm);
d3abd7b8 808
80be6434 809static int add_metric(struct list_head *metric_list,
db95818e 810 const struct pmu_metric *pm,
b85a4d61 811 const char *modifier,
80be6434 812 bool metric_no_group,
1fd09e29 813 bool metric_no_threshold,
1725e9cd
IR
814 const char *user_requested_cpu_list,
815 bool system_wide,
80be6434
IR
816 struct metric *root_metric,
817 const struct visited_metric *visited,
f8ea2c15 818 const struct pmu_metrics_table *table);
80be6434
IR
819
820/**
821 * resolve_metric - Locate metrics within the root metric and recursively add
822 * references to them.
823 * @metric_list: The list the metric is added to.
bd3846d0 824 * @pmu: The PMU name to resolve metrics on, or "all" for all PMUs.
b85a4d61 825 * @modifier: if non-null event modifiers like "u".
80be6434
IR
826 * @metric_no_group: Should events written to events be grouped "{}" or
827 * global. Grouping is the default but due to multiplexing the
828 * user may override.
1725e9cd
IR
829 * @user_requested_cpu_list: Command line specified CPUs to record on.
830 * @system_wide: Are events for all processes recorded.
80be6434
IR
831 * @root_metric: Metrics may reference other metrics to form a tree. In this
832 * case the root_metric holds all the IDs and a list of referenced
833 * metrics. When adding a root this argument is NULL.
834 * @visited: A singly linked list of metric names being added that is used to
835 * detect recursion.
eeac7730 836 * @table: The table that is searched for metrics, most commonly the table for the
80be6434
IR
837 * architecture perf is running upon.
838 */
839static int resolve_metric(struct list_head *metric_list,
bd3846d0 840 const char *pmu,
b85a4d61 841 const char *modifier,
80be6434 842 bool metric_no_group,
1fd09e29 843 bool metric_no_threshold,
1725e9cd
IR
844 const char *user_requested_cpu_list,
845 bool system_wide,
80be6434
IR
846 struct metric *root_metric,
847 const struct visited_metric *visited,
f8ea2c15 848 const struct pmu_metrics_table *table)
80be6434
IR
849{
850 struct hashmap_entry *cur;
851 size_t bkt;
852 struct to_resolve {
853 /* The metric to resolve. */
db95818e 854 struct pmu_metric pm;
80be6434
IR
855 /*
856 * The key in the IDs map, this may differ from in case,
db95818e 857 * etc. from pm->metric_name.
80be6434
IR
858 */
859 const char *key;
860 } *pending = NULL;
861 int i, ret = 0, pending_cnt = 0;
862
863 /*
864 * Iterate all the parsed IDs and if there's a matching metric and it to
865 * the pending array.
866 */
867 hashmap__for_each_entry(root_metric->pctx->ids, cur, bkt) {
db95818e 868 struct pmu_metric pm;
80be6434 869
bd3846d0 870 if (metricgroup__find_metric(pmu, cur->pkey, table, &pm)) {
80be6434
IR
871 pending = realloc(pending,
872 (pending_cnt + 1) * sizeof(struct to_resolve));
873 if (!pending)
874 return -ENOMEM;
875
db95818e 876 memcpy(&pending[pending_cnt].pm, &pm, sizeof(pm));
c302378b 877 pending[pending_cnt].key = cur->pkey;
80be6434
IR
878 pending_cnt++;
879 }
880 }
881
882 /* Remove the metric IDs from the context. */
883 for (i = 0; i < pending_cnt; i++)
884 expr__del_id(root_metric->pctx, pending[i].key);
885
886 /*
887 * Recursively add all the metrics, IDs are added to the root metric's
888 * context.
889 */
890 for (i = 0; i < pending_cnt; i++) {
db95818e 891 ret = add_metric(metric_list, &pending[i].pm, modifier, metric_no_group,
1fd09e29
IR
892 metric_no_threshold, user_requested_cpu_list, system_wide,
893 root_metric, visited, table);
80be6434
IR
894 if (ret)
895 break;
896 }
897
898 free(pending);
899 return ret;
900}
901
68074811
IR
902/**
903 * __add_metric - Add a metric to metric_list.
904 * @metric_list: The list the metric is added to.
db95818e 905 * @pm: The pmu_metric containing the metric to be added.
b85a4d61 906 * @modifier: if non-null event modifiers like "u".
68074811
IR
907 * @metric_no_group: Should events written to events be grouped "{}" or
908 * global. Grouping is the default but due to multiplexing the
909 * user may override.
1fd09e29 910 * @metric_no_threshold: Should threshold expressions be ignored?
68074811 911 * @runtime: A special argument for the parser only known at runtime.
1725e9cd
IR
912 * @user_requested_cpu_list: Command line specified CPUs to record on.
913 * @system_wide: Are events for all processes recorded.
80be6434
IR
914 * @root_metric: Metrics may reference other metrics to form a tree. In this
915 * case the root_metric holds all the IDs and a list of referenced
916 * metrics. When adding a root this argument is NULL.
917 * @visited: A singly linked list of metric names being added that is used to
918 * detect recursion.
eeac7730 919 * @table: The table that is searched for metrics, most commonly the table for the
80be6434 920 * architecture perf is running upon.
68074811 921 */
119e521a 922static int __add_metric(struct list_head *metric_list,
db95818e 923 const struct pmu_metric *pm,
b85a4d61 924 const char *modifier,
e7e1badd 925 bool metric_no_group,
1fd09e29 926 bool metric_no_threshold,
83de0b7d 927 int runtime,
1725e9cd
IR
928 const char *user_requested_cpu_list,
929 bool system_wide,
80be6434
IR
930 struct metric *root_metric,
931 const struct visited_metric *visited,
f8ea2c15 932 const struct pmu_metrics_table *table)
47352aba 933{
80be6434
IR
934 const struct visited_metric *vm;
935 int ret;
936 bool is_root = !root_metric;
d0a3052f 937 const char *expr;
80be6434 938 struct visited_metric visited_node = {
db95818e 939 .name = pm->metric_name,
80be6434
IR
940 .parent = visited,
941 };
942
943 for (vm = visited; vm; vm = vm->parent) {
db95818e
IR
944 if (!strcmp(pm->metric_name, vm->name)) {
945 pr_err("failed: recursion detected for %s\n", pm->metric_name);
80be6434
IR
946 return -1;
947 }
948 }
47352aba 949
80be6434 950 if (is_root) {
83de0b7d 951 /*
80be6434
IR
952 * This metric is the root of a tree and may reference other
953 * metrics that are added recursively.
83de0b7d 954 */
db95818e 955 root_metric = metric__new(pm, modifier, metric_no_group, runtime,
1725e9cd 956 user_requested_cpu_list, system_wide);
80be6434 957 if (!root_metric)
83de0b7d
JO
958 return -ENOMEM;
959
83de0b7d 960 } else {
46bdc0bf
IR
961 int cnt = 0;
962
83de0b7d 963 /*
a3de7690
IR
964 * This metric was referenced in a metric higher in the
965 * tree. Check if the same metric is already resolved in the
966 * metric_refs list.
83de0b7d 967 */
46bdc0bf
IR
968 if (root_metric->metric_refs) {
969 for (; root_metric->metric_refs[cnt].metric_name; cnt++) {
db95818e 970 if (!strcmp(pm->metric_name,
46bdc0bf
IR
971 root_metric->metric_refs[cnt].metric_name))
972 return 0;
973 }
a3de7690
IR
974 }
975
46bdc0bf
IR
976 /* Create reference. Need space for the entry and the terminator. */
977 root_metric->metric_refs = realloc(root_metric->metric_refs,
978 (cnt + 2) * sizeof(struct metric_ref));
979 if (!root_metric->metric_refs)
83de0b7d
JO
980 return -ENOMEM;
981
982 /*
983 * Intentionally passing just const char pointers,
984 * from 'pe' object, so they never go away. We don't
985 * need to change them, so there's no need to create
986 * our own copy.
987 */
db95818e
IR
988 root_metric->metric_refs[cnt].metric_name = pm->metric_name;
989 root_metric->metric_refs[cnt].metric_expr = pm->metric_expr;
47352aba 990
46bdc0bf
IR
991 /* Null terminate array. */
992 root_metric->metric_refs[cnt+1].metric_name = NULL;
993 root_metric->metric_refs[cnt+1].metric_expr = NULL;
83de0b7d 994 }
ded80bda 995
83de0b7d
JO
996 /*
997 * For both the parent and referenced metrics, we parse
80be6434 998 * all the metric's IDs and add it to the root context.
83de0b7d 999 */
d0a3052f
IR
1000 ret = 0;
1001 expr = pm->metric_expr;
1002 if (is_root && pm->metric_threshold) {
1003 /*
1004 * Threshold expressions are built off the actual metric. Switch
1005 * to use that in case of additional necessary events. Change
1006 * the visited node name to avoid this being flagged as
1fd09e29
IR
1007 * recursion. If the threshold events are disabled, just use the
1008 * metric's name as a reference. This allows metric threshold
1009 * computation if there are sufficient events.
d0a3052f
IR
1010 */
1011 assert(strstr(pm->metric_threshold, pm->metric_name));
1fd09e29 1012 expr = metric_no_threshold ? pm->metric_name : pm->metric_threshold;
d0a3052f
IR
1013 visited_node.name = "__threshold__";
1014 }
1015 if (expr__find_ids(expr, NULL, root_metric->pctx) < 0) {
80be6434
IR
1016 /* Broken metric. */
1017 ret = -EINVAL;
d0a3052f
IR
1018 }
1019 if (!ret) {
80be6434 1020 /* Resolve referenced metrics. */
bd3846d0
IR
1021 const char *pmu = pm->pmu ?: "cpu";
1022
1023 ret = resolve_metric(metric_list, pmu, modifier, metric_no_group,
1fd09e29 1024 metric_no_threshold, user_requested_cpu_list,
bd3846d0
IR
1025 system_wide, root_metric, &visited_node,
1026 table);
ded80bda 1027 }
80be6434
IR
1028 if (ret) {
1029 if (is_root)
1030 metric__free(root_metric);
6bf2102b 1031
80be6434
IR
1032 } else if (is_root)
1033 list_add(&root_metric->nd, metric_list);
47352aba 1034
80be6434 1035 return ret;
47352aba
KJ
1036}
1037
660842e4 1038struct metricgroup__find_metric_data {
bd3846d0 1039 const char *pmu;
660842e4 1040 const char *metric;
db95818e 1041 struct pmu_metric *pm;
660842e4
IR
1042};
1043
db95818e 1044static int metricgroup__find_metric_callback(const struct pmu_metric *pm,
f8ea2c15 1045 const struct pmu_metrics_table *table __maybe_unused,
660842e4
IR
1046 void *vdata)
1047{
1048 struct metricgroup__find_metric_data *data = vdata;
bd3846d0
IR
1049 const char *pm_pmu = pm->pmu ?: "cpu";
1050
1051 if (strcmp(data->pmu, "all") && strcmp(pm_pmu, data->pmu))
1052 return 0;
660842e4 1053
db95818e 1054 if (!match_metric(pm->metric_name, data->metric))
660842e4 1055 return 0;
ce391940 1056
db95818e 1057 memcpy(data->pm, pm, sizeof(*pm));
d3abd7b8 1058 return 1;
660842e4 1059}
ce391940 1060
bd3846d0
IR
1061static bool metricgroup__find_metric(const char *pmu,
1062 const char *metric,
f8ea2c15 1063 const struct pmu_metrics_table *table,
db95818e 1064 struct pmu_metric *pm)
83de0b7d 1065{
660842e4 1066 struct metricgroup__find_metric_data data = {
bd3846d0 1067 .pmu = pmu,
660842e4 1068 .metric = metric,
db95818e 1069 .pm = pm,
660842e4 1070 };
83de0b7d 1071
4000519e 1072 return pmu_metrics_table__for_each_metric(table, metricgroup__find_metric_callback, &data)
d3abd7b8 1073 ? true : false;
83de0b7d
JO
1074}
1075
119e521a 1076static int add_metric(struct list_head *metric_list,
db95818e 1077 const struct pmu_metric *pm,
b85a4d61 1078 const char *modifier,
83de0b7d 1079 bool metric_no_group,
1fd09e29 1080 bool metric_no_threshold,
1725e9cd
IR
1081 const char *user_requested_cpu_list,
1082 bool system_wide,
80be6434
IR
1083 struct metric *root_metric,
1084 const struct visited_metric *visited,
f8ea2c15 1085 const struct pmu_metrics_table *table)
83de0b7d 1086{
a29c164a
JO
1087 int ret = 0;
1088
db95818e 1089 pr_debug("metric expr %s for %s\n", pm->metric_expr, pm->metric_name);
a29c164a 1090
db95818e 1091 if (!strstr(pm->metric_expr, "?")) {
1fd09e29
IR
1092 ret = __add_metric(metric_list, pm, modifier, metric_no_group,
1093 metric_no_threshold, 0, user_requested_cpu_list,
1094 system_wide, root_metric, visited, table);
a29c164a
JO
1095 } else {
1096 int j, count;
1097
db95818e 1098 count = arch_get_runtimeparam(pm);
a29c164a
JO
1099
1100 /* This loop is added to create multiple
1101 * events depend on count value and add
119e521a 1102 * those events to metric_list.
a29c164a
JO
1103 */
1104
80be6434 1105 for (j = 0; j < count && !ret; j++)
1fd09e29
IR
1106 ret = __add_metric(metric_list, pm, modifier, metric_no_group,
1107 metric_no_threshold, j, user_requested_cpu_list,
1108 system_wide, root_metric, visited, table);
a29c164a
JO
1109 }
1110
1111 return ret;
1112}
1113
db95818e 1114static int metricgroup__add_metric_sys_event_iter(const struct pmu_metric *pm,
f8ea2c15
IR
1115 const struct pmu_metrics_table *table __maybe_unused,
1116 void *data)
be335ec2
JG
1117{
1118 struct metricgroup_add_iter_data *d = data;
1119 int ret;
1120
bd3846d0 1121 if (!match_pm_metric(pm, d->pmu, d->metric_name))
be335ec2
JG
1122 return 0;
1123
db95818e 1124 ret = add_metric(d->metric_list, pm, d->modifier, d->metric_no_group,
1fd09e29
IR
1125 d->metric_no_threshold, d->user_requested_cpu_list,
1126 d->system_wide, d->root_metric, d->visited, d->table);
be335ec2 1127 if (ret)
fe7a98b9 1128 goto out;
be335ec2
JG
1129
1130 *(d->has_match) = true;
1131
fe7a98b9
JG
1132out:
1133 *(d->ret) = ret;
1134 return ret;
be335ec2
JG
1135}
1136
6b6b16b3
IR
1137/**
1138 * metric_list_cmp - list_sort comparator that sorts metrics with more events to
9aa09230 1139 * the front. tool events are excluded from the count.
6b6b16b3 1140 */
80be6434
IR
1141static int metric_list_cmp(void *priv __maybe_unused, const struct list_head *l,
1142 const struct list_head *r)
1143{
1144 const struct metric *left = container_of(l, struct metric, nd);
1145 const struct metric *right = container_of(r, struct metric, nd);
6b6b16b3 1146 struct expr_id_data *data;
9aa09230 1147 int i, left_count, right_count;
6b6b16b3
IR
1148
1149 left_count = hashmap__size(left->pctx->ids);
9aa09230
IR
1150 perf_tool_event__for_each_event(i) {
1151 if (!expr__get_id(left->pctx, perf_tool_event__to_str(i), &data))
1152 left_count--;
1153 }
6b6b16b3
IR
1154
1155 right_count = hashmap__size(right->pctx->ids);
9aa09230
IR
1156 perf_tool_event__for_each_event(i) {
1157 if (!expr__get_id(right->pctx, perf_tool_event__to_str(i), &data))
1158 right_count--;
1159 }
80be6434 1160
6b6b16b3 1161 return right_count - left_count;
80be6434
IR
1162}
1163
1c0e4795
KL
1164/**
1165 * default_metricgroup_cmp - Implements complex key for the Default metricgroup
1166 * that first sorts by default_metricgroup_name, then
1167 * metric_name.
1168 */
1169static int default_metricgroup_cmp(void *priv __maybe_unused,
1170 const struct list_head *l,
1171 const struct list_head *r)
1172{
1173 const struct metric *left = container_of(l, struct metric, nd);
1174 const struct metric *right = container_of(r, struct metric, nd);
1175 int diff = strcmp(right->default_metricgroup_name, left->default_metricgroup_name);
1176
1177 if (diff)
1178 return diff;
1179
1180 return strcmp(right->metric_name, left->metric_name);
1181}
1182
660842e4
IR
1183struct metricgroup__add_metric_data {
1184 struct list_head *list;
bd3846d0 1185 const char *pmu;
660842e4
IR
1186 const char *metric_name;
1187 const char *modifier;
1725e9cd 1188 const char *user_requested_cpu_list;
660842e4 1189 bool metric_no_group;
1fd09e29 1190 bool metric_no_threshold;
1725e9cd 1191 bool system_wide;
660842e4
IR
1192 bool has_match;
1193};
1194
db95818e 1195static int metricgroup__add_metric_callback(const struct pmu_metric *pm,
f8ea2c15 1196 const struct pmu_metrics_table *table,
660842e4
IR
1197 void *vdata)
1198{
1199 struct metricgroup__add_metric_data *data = vdata;
1200 int ret = 0;
1201
bd3846d0 1202 if (pm->metric_expr && match_pm_metric(pm, data->pmu, data->metric_name)) {
ccc66c60 1203 bool metric_no_group = data->metric_no_group ||
e4c4e8a5 1204 match_metric(pm->metricgroup_no_group, data->metric_name);
660842e4
IR
1205
1206 data->has_match = true;
ccc66c60 1207 ret = add_metric(data->list, pm, data->modifier, metric_no_group,
1fd09e29
IR
1208 data->metric_no_threshold, data->user_requested_cpu_list,
1209 data->system_wide, /*root_metric=*/NULL,
1210 /*visited_metrics=*/NULL, table);
660842e4
IR
1211 }
1212 return ret;
1213}
1214
68074811
IR
1215/**
1216 * metricgroup__add_metric - Find and add a metric, or a metric group.
bd3846d0 1217 * @pmu: The PMU name to search for metrics on, or "all" for all PMUs.
68074811
IR
1218 * @metric_name: The name of the metric or metric group. For example, "IPC"
1219 * could be the name of a metric and "TopDownL1" the name of a
1220 * metric group.
b85a4d61 1221 * @modifier: if non-null event modifiers like "u".
68074811
IR
1222 * @metric_no_group: Should events written to events be grouped "{}" or
1223 * global. Grouping is the default but due to multiplexing the
1224 * user may override.
1725e9cd
IR
1225 * @user_requested_cpu_list: Command line specified CPUs to record on.
1226 * @system_wide: Are events for all processes recorded.
68074811 1227 * @metric_list: The list that the metric or metric group are added to.
eeac7730 1228 * @table: The table that is searched for metrics, most commonly the table for the
68074811
IR
1229 * architecture perf is running upon.
1230 */
bd3846d0 1231static int metricgroup__add_metric(const char *pmu, const char *metric_name, const char *modifier,
1fd09e29 1232 bool metric_no_group, bool metric_no_threshold,
1725e9cd
IR
1233 const char *user_requested_cpu_list,
1234 bool system_wide,
119e521a 1235 struct list_head *metric_list,
f8ea2c15 1236 const struct pmu_metrics_table *table)
b18f3e36 1237{
98461d9d 1238 LIST_HEAD(list);
660842e4 1239 int ret;
90810399 1240 bool has_match = false;
b18f3e36 1241
660842e4
IR
1242 {
1243 struct metricgroup__add_metric_data data = {
1244 .list = &list,
bd3846d0 1245 .pmu = pmu,
660842e4
IR
1246 .metric_name = metric_name,
1247 .modifier = modifier,
1248 .metric_no_group = metric_no_group,
1fd09e29 1249 .metric_no_threshold = metric_no_threshold,
1725e9cd
IR
1250 .user_requested_cpu_list = user_requested_cpu_list,
1251 .system_wide = system_wide,
660842e4
IR
1252 .has_match = false,
1253 };
1254 /*
1255 * Iterate over all metrics seeing if metric matches either the
1256 * name or group. When it does add the metric to the list.
1257 */
4000519e 1258 ret = pmu_metrics_table__for_each_metric(table, metricgroup__add_metric_callback,
db95818e 1259 &data);
a29c164a 1260 if (ret)
27adafcd 1261 goto out;
ce391940 1262
660842e4
IR
1263 has_match = data.has_match;
1264 }
be335ec2
JG
1265 {
1266 struct metricgroup_iter_data data = {
1267 .fn = metricgroup__add_metric_sys_event_iter,
1268 .data = (void *) &(struct metricgroup_add_iter_data) {
1269 .metric_list = &list,
bd3846d0 1270 .pmu = pmu,
68074811 1271 .metric_name = metric_name,
b85a4d61 1272 .modifier = modifier,
be335ec2 1273 .metric_no_group = metric_no_group,
1725e9cd
IR
1274 .user_requested_cpu_list = user_requested_cpu_list,
1275 .system_wide = system_wide,
be335ec2
JG
1276 .has_match = &has_match,
1277 .ret = &ret,
eeac7730 1278 .table = table,
be335ec2
JG
1279 },
1280 };
1281
db95818e 1282 pmu_for_each_sys_metric(metricgroup__sys_event_iter, &data);
be335ec2 1283 }
ce391940 1284 /* End of pmu events. */
5ecd5a0c 1285 if (!has_match)
27adafcd 1286 ret = -EINVAL;
98461d9d 1287
27adafcd
NK
1288out:
1289 /*
1290 * add to metric_list so that they can be released
1291 * even if it's failed
1292 */
119e521a 1293 list_splice(&list, metric_list);
27adafcd 1294 return ret;
b18f3e36
AK
1295}
1296
68074811
IR
1297/**
1298 * metricgroup__add_metric_list - Find and add metrics, or metric groups,
1299 * specified in a list.
bd3846d0 1300 * @pmu: A pmu to restrict the metrics to, or "all" for all PMUS.
68074811
IR
1301 * @list: the list of metrics or metric groups. For example, "IPC,CPI,TopDownL1"
1302 * would match the IPC and CPI metrics, and TopDownL1 would match all
1303 * the metrics in the TopDownL1 group.
1304 * @metric_no_group: Should events written to events be grouped "{}" or
1305 * global. Grouping is the default but due to multiplexing the
1306 * user may override.
1725e9cd
IR
1307 * @user_requested_cpu_list: Command line specified CPUs to record on.
1308 * @system_wide: Are events for all processes recorded.
68074811 1309 * @metric_list: The list that metrics are added to.
eeac7730 1310 * @table: The table that is searched for metrics, most commonly the table for the
68074811
IR
1311 * architecture perf is running upon.
1312 */
bd3846d0
IR
1313static int metricgroup__add_metric_list(const char *pmu, const char *list,
1314 bool metric_no_group,
1fd09e29 1315 bool metric_no_threshold,
1725e9cd
IR
1316 const char *user_requested_cpu_list,
1317 bool system_wide, struct list_head *metric_list,
f8ea2c15 1318 const struct pmu_metrics_table *table)
b18f3e36 1319{
b85a4d61 1320 char *list_itr, *list_copy, *metric_name, *modifier;
ec5c5b3d 1321 int ret, count = 0;
b18f3e36 1322
b85a4d61
IR
1323 list_copy = strdup(list);
1324 if (!list_copy)
b18f3e36 1325 return -ENOMEM;
b85a4d61 1326 list_itr = list_copy;
411bc316 1327
b85a4d61
IR
1328 while ((metric_name = strsep(&list_itr, ",")) != NULL) {
1329 modifier = strchr(metric_name, ':');
1330 if (modifier)
1331 *modifier++ = '\0';
1332
bd3846d0 1333 ret = metricgroup__add_metric(pmu, metric_name, modifier,
1fd09e29
IR
1334 metric_no_group, metric_no_threshold,
1335 user_requested_cpu_list,
1725e9cd 1336 system_wide, metric_list, table);
ec5c5b3d 1337 if (ret == -EINVAL)
b85a4d61 1338 pr_err("Cannot find metric or group `%s'\n", metric_name);
ec5c5b3d
IR
1339
1340 if (ret)
b18f3e36 1341 break;
ec5c5b3d
IR
1342
1343 count++;
b18f3e36 1344 }
b85a4d61 1345 free(list_copy);
ab483d8b 1346
ec5c5b3d
IR
1347 if (!ret) {
1348 /*
1349 * Warn about nmi_watchdog if any parsed metrics had the
1350 * NO_NMI_WATCHDOG constraint.
1351 */
180a5013 1352 metric__watchdog_constraint_hint(NULL, /*foot=*/true);
ec5c5b3d
IR
1353 /* No metrics. */
1354 if (count == 0)
1355 return -EINVAL;
1356 }
b18f3e36
AK
1357 return ret;
1358}
1359
119e521a 1360static void metricgroup__free_metrics(struct list_head *metric_list)
b18f3e36 1361{
a0c05b36 1362 struct metric *m, *tmp;
b18f3e36 1363
119e521a 1364 list_for_each_entry_safe (m, tmp, metric_list, nd) {
a0c05b36 1365 list_del_init(&m->nd);
3d81d761 1366 metric__free(m);
b18f3e36
AK
1367 }
1368}
1369
8586d274
IR
1370/**
1371 * find_tool_events - Search for the pressence of tool events in metric_list.
1372 * @metric_list: List to take metrics from.
1373 * @tool_events: Array of false values, indices corresponding to tool events set
1374 * to true if tool event is found.
1375 */
1376static void find_tool_events(const struct list_head *metric_list,
1377 bool tool_events[PERF_TOOL_MAX])
1378{
1379 struct metric *m;
1380
1381 list_for_each_entry(m, metric_list, nd) {
1382 int i;
1383
1384 perf_tool_event__for_each_event(i) {
1385 struct expr_id_data *data;
1386
1387 if (!tool_events[i] &&
1388 !expr__get_id(m->pctx, perf_tool_event__to_str(i), &data))
1389 tool_events[i] = true;
1390 }
1391 }
1392}
1393
5ecd5a0c 1394/**
180a5013 1395 * build_combined_expr_ctx - Make an expr_parse_ctx with all !group_events
5ecd5a0c
IR
1396 * metric IDs, as the IDs are held in a set,
1397 * duplicates will be removed.
1398 * @metric_list: List to take metrics from.
1399 * @combined: Out argument for result.
1400 */
1401static int build_combined_expr_ctx(const struct list_head *metric_list,
1402 struct expr_parse_ctx **combined)
1403{
1404 struct hashmap_entry *cur;
1405 size_t bkt;
1406 struct metric *m;
1407 char *dup;
1408 int ret;
1409
1410 *combined = expr__ctx_new();
1411 if (!*combined)
1412 return -ENOMEM;
1413
1414 list_for_each_entry(m, metric_list, nd) {
180a5013 1415 if (!m->group_events && !m->modifier) {
5ecd5a0c 1416 hashmap__for_each_entry(m->pctx->ids, cur, bkt) {
c302378b 1417 dup = strdup(cur->pkey);
5ecd5a0c
IR
1418 if (!dup) {
1419 ret = -ENOMEM;
1420 goto err_out;
1421 }
1422 ret = expr__add_id(*combined, dup);
1423 if (ret)
1424 goto err_out;
1425 }
1426 }
1427 }
1428 return 0;
1429err_out:
1430 expr__ctx_free(*combined);
1431 *combined = NULL;
1432 return ret;
1433}
1434
1435/**
1436 * parse_ids - Build the event string for the ids and parse them creating an
1437 * evlist. The encoded metric_ids are decoded.
6b6b16b3 1438 * @metric_no_merge: is metric sharing explicitly disabled.
92532073 1439 * @fake_pmu: use a fake PMU when testing metrics not supported by the current CPU.
5ecd5a0c 1440 * @ids: the event identifiers parsed from a metric.
b85a4d61 1441 * @modifier: any modifiers added to the events.
180a5013 1442 * @group_events: should events be placed in a weak group.
8586d274
IR
1443 * @tool_events: entries set true if the tool event of index could be present in
1444 * the overall list of metrics.
5ecd5a0c
IR
1445 * @out_evlist: the created list of events.
1446 */
92532073 1447static int parse_ids(bool metric_no_merge, bool fake_pmu,
6b6b16b3 1448 struct expr_parse_ctx *ids, const char *modifier,
180a5013 1449 bool group_events, const bool tool_events[PERF_TOOL_MAX],
8586d274 1450 struct evlist **out_evlist)
5ecd5a0c
IR
1451{
1452 struct parse_events_error parse_error;
1453 struct evlist *parsed_evlist;
1454 struct strbuf events = STRBUF_INIT;
1455 int ret;
1456
1457 *out_evlist = NULL;
6b6b16b3 1458 if (!metric_no_merge || hashmap__size(ids->ids) == 0) {
c788ef61 1459 bool added_event = false;
9aa09230 1460 int i;
5ecd5a0c 1461 /*
9aa09230
IR
1462 * We may fail to share events between metrics because a tool
1463 * event isn't present in one metric. For example, a ratio of
1464 * cache misses doesn't need duration_time but the same events
1465 * may be used for a misses per second. Events without sharing
1466 * implies multiplexing, that is best avoided, so place
1467 * all tool events in every group.
6b6b16b3
IR
1468 *
1469 * Also, there may be no ids/events in the expression parsing
1470 * context because of constant evaluation, e.g.:
1471 * event1 if #smt_on else 0
9aa09230 1472 * Add a tool event to avoid a parse error on an empty string.
5ecd5a0c 1473 */
9aa09230 1474 perf_tool_event__for_each_event(i) {
8586d274
IR
1475 if (tool_events[i]) {
1476 char *tmp = strdup(perf_tool_event__to_str(i));
5ecd5a0c 1477
8586d274
IR
1478 if (!tmp)
1479 return -ENOMEM;
1480 ids__insert(ids->ids, tmp);
c788ef61 1481 added_event = true;
8586d274 1482 }
9aa09230 1483 }
c788ef61
IR
1484 if (!added_event && hashmap__size(ids->ids) == 0) {
1485 char *tmp = strdup("duration_time");
1486
1487 if (!tmp)
1488 return -ENOMEM;
1489 ids__insert(ids->ids, tmp);
1490 }
5ecd5a0c 1491 }
b85a4d61 1492 ret = metricgroup__build_event_string(&events, ids, modifier,
180a5013 1493 group_events);
5ecd5a0c
IR
1494 if (ret)
1495 return ret;
1496
1497 parsed_evlist = evlist__new();
1498 if (!parsed_evlist) {
1499 ret = -ENOMEM;
1500 goto err_out;
1501 }
1502 pr_debug("Parsing metric events '%s'\n", events.buf);
07eafd4e 1503 parse_events_error__init(&parse_error);
411ad22e 1504 ret = __parse_events(parsed_evlist, events.buf, /*pmu_filter=*/NULL,
a2a6604e
DM
1505 &parse_error, fake_pmu, /*warn_if_reordered=*/false,
1506 /*fake_tp=*/false);
5ecd5a0c 1507 if (ret) {
6c191289 1508 parse_events_error__print(&parse_error, events.buf);
5ecd5a0c
IR
1509 goto err_out;
1510 }
b85a4d61 1511 ret = decode_all_metric_ids(parsed_evlist, modifier);
5ecd5a0c
IR
1512 if (ret)
1513 goto err_out;
1514
1515 *out_evlist = parsed_evlist;
1516 parsed_evlist = NULL;
1517err_out:
07eafd4e 1518 parse_events_error__exit(&parse_error);
5ecd5a0c
IR
1519 evlist__delete(parsed_evlist);
1520 strbuf_release(&events);
1521 return ret;
1522}
1523
bd3846d0
IR
1524static int parse_groups(struct evlist *perf_evlist,
1525 const char *pmu, const char *str,
8b4468a2
JO
1526 bool metric_no_group,
1527 bool metric_no_merge,
1fd09e29 1528 bool metric_no_threshold,
1725e9cd
IR
1529 const char *user_requested_cpu_list,
1530 bool system_wide,
92532073 1531 bool fake_pmu,
5ecd5a0c 1532 struct rblist *metric_events_list,
f8ea2c15 1533 const struct pmu_metrics_table *table)
b18f3e36 1534{
5ecd5a0c 1535 struct evlist *combined_evlist = NULL;
119e521a 1536 LIST_HEAD(metric_list);
5ecd5a0c 1537 struct metric *m;
8586d274 1538 bool tool_events[PERF_TOOL_MAX] = {false};
1c0e4795 1539 bool is_default = !strcmp(str, "Default");
b18f3e36
AK
1540 int ret;
1541
5ecd5a0c
IR
1542 if (metric_events_list->nr_entries == 0)
1543 metricgroup__rblist_init(metric_events_list);
bd3846d0 1544 ret = metricgroup__add_metric_list(pmu, str, metric_no_group, metric_no_threshold,
1725e9cd
IR
1545 user_requested_cpu_list,
1546 system_wide, &metric_list, table);
b18f3e36 1547 if (ret)
4f57a1ed 1548 goto out;
5ecd5a0c
IR
1549
1550 /* Sort metrics from largest to smallest. */
1551 list_sort(NULL, &metric_list, metric_list_cmp);
1552
1553 if (!metric_no_merge) {
1554 struct expr_parse_ctx *combined = NULL;
1555
8586d274
IR
1556 find_tool_events(&metric_list, tool_events);
1557
5ecd5a0c
IR
1558 ret = build_combined_expr_ctx(&metric_list, &combined);
1559
1560 if (!ret && combined && hashmap__size(combined->ids)) {
6b6b16b3
IR
1561 ret = parse_ids(metric_no_merge, fake_pmu, combined,
1562 /*modifier=*/NULL,
180a5013 1563 /*group_events=*/false,
8586d274 1564 tool_events,
17b3867d 1565 &combined_evlist);
5ecd5a0c
IR
1566 }
1567 if (combined)
1568 expr__ctx_free(combined);
1569
1570 if (ret)
1571 goto out;
1572 }
1573
1c0e4795
KL
1574 if (is_default)
1575 list_sort(NULL, &metric_list, default_metricgroup_cmp);
1576
5ecd5a0c
IR
1577 list_for_each_entry(m, &metric_list, nd) {
1578 struct metric_event *me;
1579 struct evsel **metric_events;
1580 struct evlist *metric_evlist = NULL;
1581 struct metric *n;
1582 struct metric_expr *expr;
1583
180a5013 1584 if (combined_evlist && !m->group_events) {
5ecd5a0c
IR
1585 metric_evlist = combined_evlist;
1586 } else if (!metric_no_merge) {
1587 /*
1588 * See if the IDs for this metric are a subset of an
1589 * earlier metric.
1590 */
1591 list_for_each_entry(n, &metric_list, nd) {
1592 if (m == n)
1593 break;
1594
1595 if (n->evlist == NULL)
1596 continue;
1597
b85a4d61
IR
1598 if ((!m->modifier && n->modifier) ||
1599 (m->modifier && !n->modifier) ||
1600 (m->modifier && n->modifier &&
1601 strcmp(m->modifier, n->modifier)))
1602 continue;
1603
bd3846d0
IR
1604 if ((!m->pmu && n->pmu) ||
1605 (m->pmu && !n->pmu) ||
1606 (m->pmu && n->pmu && strcmp(m->pmu, n->pmu)))
1607 continue;
1608
5ecd5a0c
IR
1609 if (expr__subset_of_ids(n->pctx, m->pctx)) {
1610 pr_debug("Events in '%s' fully contained within '%s'\n",
1611 m->metric_name, n->metric_name);
1612 metric_evlist = n->evlist;
1613 break;
1614 }
1615
1616 }
1617 }
1618 if (!metric_evlist) {
17b3867d 1619 ret = parse_ids(metric_no_merge, fake_pmu, m->pctx, m->modifier,
180a5013 1620 m->group_events, tool_events, &m->evlist);
5ecd5a0c
IR
1621 if (ret)
1622 goto out;
1623
1624 metric_evlist = m->evlist;
1625 }
bd3846d0
IR
1626 ret = setup_metric_events(fake_pmu ? "all" : m->pmu, m->pctx->ids,
1627 metric_evlist, &metric_events);
5ecd5a0c 1628 if (ret) {
8a4859c5 1629 pr_err("Cannot resolve IDs for %s: %s\n",
5ecd5a0c
IR
1630 m->metric_name, m->metric_expr);
1631 goto out;
1632 }
1633
1634 me = metricgroup__lookup(metric_events_list, metric_events[0], true);
1635
1636 expr = malloc(sizeof(struct metric_expr));
1637 if (!expr) {
1638 ret = -ENOMEM;
1639 free(metric_events);
1640 goto out;
1641 }
1642
1643 expr->metric_refs = m->metric_refs;
1644 m->metric_refs = NULL;
1645 expr->metric_expr = m->metric_expr;
b85a4d61
IR
1646 if (m->modifier) {
1647 char *tmp;
1648
1649 if (asprintf(&tmp, "%s:%s", m->metric_name, m->modifier) < 0)
1650 expr->metric_name = NULL;
1651 else
1652 expr->metric_name = tmp;
1653 } else
1654 expr->metric_name = strdup(m->metric_name);
1655
1656 if (!expr->metric_name) {
1657 ret = -ENOMEM;
1658 free(metric_events);
1659 goto out;
1660 }
d0a3052f 1661 expr->metric_threshold = m->metric_threshold;
5ecd5a0c
IR
1662 expr->metric_unit = m->metric_unit;
1663 expr->metric_events = metric_events;
1a6abdde 1664 expr->runtime = m->pctx->sctx.runtime;
1c0e4795
KL
1665 expr->default_metricgroup_name = m->default_metricgroup_name;
1666 me->is_default = is_default;
5ecd5a0c
IR
1667 list_add(&expr->nd, &me->head);
1668 }
1669
1670
aba8c5e3 1671 if (combined_evlist) {
5ecd5a0c 1672 evlist__splice_list_tail(perf_evlist, &combined_evlist->core.entries);
aba8c5e3
IR
1673 evlist__delete(combined_evlist);
1674 }
5ecd5a0c
IR
1675
1676 list_for_each_entry(m, &metric_list, nd) {
1677 if (m->evlist)
1678 evlist__splice_list_tail(perf_evlist, &m->evlist->core.entries);
b18f3e36 1679 }
ec5c5b3d 1680
b18f3e36 1681out:
119e521a 1682 metricgroup__free_metrics(&metric_list);
b18f3e36
AK
1683 return ret;
1684}
742d92ff 1685
a4b8cfca 1686int metricgroup__parse_groups(struct evlist *perf_evlist,
dae47d39 1687 const char *pmu,
8b4468a2
JO
1688 const char *str,
1689 bool metric_no_group,
1690 bool metric_no_merge,
1fd09e29 1691 bool metric_no_threshold,
1725e9cd
IR
1692 const char *user_requested_cpu_list,
1693 bool system_wide,
03f23570 1694 bool hardware_aware_grouping,
8b4468a2
JO
1695 struct rblist *metric_events)
1696{
f8ea2c15 1697 const struct pmu_metrics_table *table = pmu_metrics_table__find();
8b4468a2 1698
3f5df3ac
IR
1699 if (!table)
1700 return -EINVAL;
03f23570
WW
1701 if (hardware_aware_grouping)
1702 pr_debug("Use hardware aware grouping instead of traditional metric grouping method\n");
3f5df3ac 1703
dae47d39 1704 return parse_groups(perf_evlist, pmu, str, metric_no_group, metric_no_merge,
1fd09e29 1705 metric_no_threshold, user_requested_cpu_list, system_wide,
92532073 1706 /*fake_pmu=*/false, metric_events, table);
8b4468a2
JO
1707}
1708
f78ac00a 1709int metricgroup__parse_groups_test(struct evlist *evlist,
f8ea2c15 1710 const struct pmu_metrics_table *table,
f78ac00a 1711 const char *str,
f78ac00a
JO
1712 struct rblist *metric_events)
1713{
bd3846d0 1714 return parse_groups(evlist, "all", str,
1fd09e29
IR
1715 /*metric_no_group=*/false,
1716 /*metric_no_merge=*/false,
1717 /*metric_no_threshold=*/false,
1725e9cd
IR
1718 /*user_requested_cpu_list=*/NULL,
1719 /*system_wide=*/false,
92532073 1720 /*fake_pmu=*/true, metric_events, table);
f78ac00a
JO
1721}
1722
bd3846d0
IR
1723struct metricgroup__has_metric_data {
1724 const char *pmu;
1725 const char *metric;
1726};
db95818e 1727static int metricgroup__has_metric_callback(const struct pmu_metric *pm,
f8ea2c15 1728 const struct pmu_metrics_table *table __maybe_unused,
660842e4
IR
1729 void *vdata)
1730{
bd3846d0 1731 struct metricgroup__has_metric_data *data = vdata;
660842e4 1732
bd3846d0 1733 return match_pm_metric(pm, data->pmu, data->metric) ? 1 : 0;
660842e4
IR
1734}
1735
bd3846d0 1736bool metricgroup__has_metric(const char *pmu, const char *metric)
742d92ff 1737{
f8ea2c15 1738 const struct pmu_metrics_table *table = pmu_metrics_table__find();
bd3846d0
IR
1739 struct metricgroup__has_metric_data data = {
1740 .pmu = pmu,
1741 .metric = metric,
1742 };
742d92ff 1743
eeac7730 1744 if (!table)
742d92ff
TR
1745 return false;
1746
4000519e 1747 return pmu_metrics_table__for_each_metric(table, metricgroup__has_metric_callback, &data)
bd3846d0 1748 ? true : false;
742d92ff 1749}
b214ba8c 1750
1647cd5b
IR
1751static int metricgroup__topdown_max_level_callback(const struct pmu_metric *pm,
1752 const struct pmu_metrics_table *table __maybe_unused,
1753 void *data)
1754{
1755 unsigned int *max_level = data;
1756 unsigned int level;
9dde1276 1757 const char *p = strstr(pm->metric_group ?: "", "TopdownL");
1647cd5b
IR
1758
1759 if (!p || p[8] == '\0')
1760 return 0;
1761
1762 level = p[8] - '0';
1763 if (level > *max_level)
1764 *max_level = level;
1765
1766 return 0;
1767}
1768
1769unsigned int metricgroups__topdown_max_level(void)
1770{
1771 unsigned int max_level = 0;
1772 const struct pmu_metrics_table *table = pmu_metrics_table__find();
1773
1774 if (!table)
1775 return false;
1776
4000519e 1777 pmu_metrics_table__for_each_metric(table, metricgroup__topdown_max_level_callback,
1647cd5b
IR
1778 &max_level);
1779 return max_level;
1780}
1781
b214ba8c
NK
1782int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
1783 struct rblist *new_metric_events,
1784 struct rblist *old_metric_events)
1785{
84f879c5 1786 unsigned int i;
b214ba8c
NK
1787
1788 for (i = 0; i < rblist__nr_entries(old_metric_events); i++) {
1789 struct rb_node *nd;
1790 struct metric_event *old_me, *new_me;
1791 struct metric_expr *old_expr, *new_expr;
1792 struct evsel *evsel;
1793 size_t alloc_size;
1794 int idx, nr;
1795
1796 nd = rblist__entry(old_metric_events, i);
1797 old_me = container_of(nd, struct metric_event, nd);
1798
38fe0e01 1799 evsel = evlist__find_evsel(evlist, old_me->evsel->core.idx);
b214ba8c
NK
1800 if (!evsel)
1801 return -EINVAL;
1802 new_me = metricgroup__lookup(new_metric_events, evsel, true);
1803 if (!new_me)
1804 return -ENOMEM;
1805
1806 pr_debug("copying metric event for cgroup '%s': %s (idx=%d)\n",
38fe0e01 1807 cgrp ? cgrp->name : "root", evsel->name, evsel->core.idx);
b214ba8c
NK
1808
1809 list_for_each_entry(old_expr, &old_me->head, nd) {
1810 new_expr = malloc(sizeof(*new_expr));
1811 if (!new_expr)
1812 return -ENOMEM;
1813
1814 new_expr->metric_expr = old_expr->metric_expr;
6c73f819 1815 new_expr->metric_threshold = old_expr->metric_threshold;
b85a4d61
IR
1816 new_expr->metric_name = strdup(old_expr->metric_name);
1817 if (!new_expr->metric_name)
1818 return -ENOMEM;
1819
b214ba8c
NK
1820 new_expr->metric_unit = old_expr->metric_unit;
1821 new_expr->runtime = old_expr->runtime;
1822
1823 if (old_expr->metric_refs) {
1824 /* calculate number of metric_events */
1825 for (nr = 0; old_expr->metric_refs[nr].metric_name; nr++)
1826 continue;
1827 alloc_size = sizeof(*new_expr->metric_refs);
1828 new_expr->metric_refs = calloc(nr + 1, alloc_size);
1829 if (!new_expr->metric_refs) {
1830 free(new_expr);
1831 return -ENOMEM;
1832 }
1833
1834 memcpy(new_expr->metric_refs, old_expr->metric_refs,
1835 nr * alloc_size);
1836 } else {
1837 new_expr->metric_refs = NULL;
1838 }
1839
1840 /* calculate number of metric_events */
1841 for (nr = 0; old_expr->metric_events[nr]; nr++)
1842 continue;
1843 alloc_size = sizeof(*new_expr->metric_events);
1844 new_expr->metric_events = calloc(nr + 1, alloc_size);
1845 if (!new_expr->metric_events) {
11ff9bcd 1846 zfree(&new_expr->metric_refs);
b214ba8c
NK
1847 free(new_expr);
1848 return -ENOMEM;
1849 }
1850
1851 /* copy evsel in the same position */
1852 for (idx = 0; idx < nr; idx++) {
1853 evsel = old_expr->metric_events[idx];
38fe0e01 1854 evsel = evlist__find_evsel(evlist, evsel->core.idx);
b214ba8c 1855 if (evsel == NULL) {
11ff9bcd
ACM
1856 zfree(&new_expr->metric_events);
1857 zfree(&new_expr->metric_refs);
b214ba8c
NK
1858 free(new_expr);
1859 return -EINVAL;
1860 }
1861 new_expr->metric_events[idx] = evsel;
1862 }
1863
1864 list_add(&new_expr->nd, &new_me->head);
1865 }
1866 }
1867 return 0;
1868}