Skip to content

Commit e979fbf

Browse files
committed
perf stat: Move metric list from config to evlist
JIRA: https://issues.redhat.com/browse/RHEL-78198 upstream ======== commit faebee1 Author: Ian Rogers <irogers@google.com> Date: Thu Jul 10 16:51:19 2025 -0700 description =========== The rblist of metric_event that then have a list of associated metric_expr is moved out of the stat_config and into the evlist. This is done as part of refactoring things for python, having the state split in two places complicates that implementation. The evlist is doing the harder work of enabling and disabling events, the metrics are needed to compute a value and it doesn't seem unreasonable to hang them from the evlist. Signed-off-by: Ian Rogers <irogers@google.com> Link: https://lore.kernel.org/r/20250710235126.1086011-7-irogers@google.com Signed-off-by: Namhyung Kim <namhyung@kernel.org> Signed-off-by: Michael Petlan <mpetlan@redhat.com>
1 parent 0834d6d commit e979fbf

File tree

15 files changed

+75
-108
lines changed

15 files changed

+75
-108
lines changed

tools/perf/builtin-script.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,8 +2134,7 @@ static void perf_sample__fprint_metric(struct perf_script *script,
21342134
perf_stat__print_shadow_stats(&stat_config, ev2,
21352135
evsel_script(ev2)->val,
21362136
sample->cpu,
2137-
&ctx,
2138-
NULL);
2137+
&ctx);
21392138
}
21402139
evsel_script(leader)->gnum = 0;
21412140
}

tools/perf/builtin-stat.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,8 +1865,7 @@ static int add_default_events(void)
18651865
stat_config.metric_no_threshold,
18661866
stat_config.user_requested_cpu_list,
18671867
stat_config.system_wide,
1868-
stat_config.hardware_aware_grouping,
1869-
&stat_config.metric_events);
1868+
stat_config.hardware_aware_grouping);
18701869
goto out;
18711870
}
18721871

@@ -1903,8 +1902,7 @@ static int add_default_events(void)
19031902
stat_config.metric_no_threshold,
19041903
stat_config.user_requested_cpu_list,
19051904
stat_config.system_wide,
1906-
stat_config.hardware_aware_grouping,
1907-
&stat_config.metric_events);
1905+
stat_config.hardware_aware_grouping);
19081906
goto out;
19091907
}
19101908

@@ -1941,8 +1939,7 @@ static int add_default_events(void)
19411939
/*metric_no_threshold=*/true,
19421940
stat_config.user_requested_cpu_list,
19431941
stat_config.system_wide,
1944-
stat_config.hardware_aware_grouping,
1945-
&stat_config.metric_events) < 0) {
1942+
stat_config.hardware_aware_grouping) < 0) {
19461943
ret = -1;
19471944
goto out;
19481945
}
@@ -1991,8 +1988,7 @@ static int add_default_events(void)
19911988
/*metric_no_threshold=*/true,
19921989
stat_config.user_requested_cpu_list,
19931990
stat_config.system_wide,
1994-
stat_config.hardware_aware_grouping,
1995-
&stat_config.metric_events) < 0) {
1991+
stat_config.hardware_aware_grouping) < 0) {
19961992
ret = -1;
19971993
goto out;
19981994
}
@@ -2001,6 +1997,9 @@ static int add_default_events(void)
20011997
evsel->default_metricgroup = true;
20021998

20031999
evlist__splice_list_tail(evlist, &metric_evlist->core.entries);
2000+
metricgroup__copy_metric_events(evlist, /*cgrp=*/NULL,
2001+
&evlist->metric_events,
2002+
&metric_evlist->metric_events);
20042003
evlist__delete(metric_evlist);
20052004
}
20062005
}
@@ -2055,6 +2054,9 @@ static int add_default_events(void)
20552054
}
20562055
parse_events_error__exit(&err);
20572056
evlist__splice_list_tail(evsel_list, &evlist->core.entries);
2057+
metricgroup__copy_metric_events(evsel_list, /*cgrp=*/NULL,
2058+
&evsel_list->metric_events,
2059+
&evlist->metric_events);
20582060
evlist__delete(evlist);
20592061
return ret;
20602062
}
@@ -2741,8 +2743,7 @@ int cmd_stat(int argc, const char **argv)
27412743
stat_config.metric_no_threshold,
27422744
stat_config.user_requested_cpu_list,
27432745
stat_config.system_wide,
2744-
stat_config.hardware_aware_grouping,
2745-
&stat_config.metric_events);
2746+
stat_config.hardware_aware_grouping);
27462747

27472748
zfree(&metrics);
27482749
if (ret) {
@@ -2762,8 +2763,7 @@ int cmd_stat(int argc, const char **argv)
27622763
goto out;
27632764
}
27642765

2765-
if (evlist__expand_cgroup(evsel_list, stat_config.cgroup_list,
2766-
&stat_config.metric_events, true) < 0) {
2766+
if (evlist__expand_cgroup(evsel_list, stat_config.cgroup_list, true) < 0) {
27672767
parse_options_usage(stat_usage, stat_options,
27682768
"for-each-cgroup", 0);
27692769
goto out;
@@ -2938,7 +2938,6 @@ int cmd_stat(int argc, const char **argv)
29382938

29392939
evlist__delete(evsel_list);
29402940

2941-
metricgroup__rblist_exit(&stat_config.metric_events);
29422941
evlist__close_control(stat_config.ctl_fd, stat_config.ctl_fd_ack, &stat_config.ctl_fd_close);
29432942

29442943
return status;

tools/perf/tests/expand-cgroup.c

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
#include <stdlib.h>
1414
#include <string.h>
1515

16-
static int test_expand_events(struct evlist *evlist,
17-
struct rblist *metric_events)
16+
static int test_expand_events(struct evlist *evlist)
1817
{
1918
int i, ret = TEST_FAIL;
2019
int nr_events;
@@ -47,7 +46,7 @@ static int test_expand_events(struct evlist *evlist,
4746
was_group_event = evsel__is_group_event(evlist__first(evlist));
4847
nr_members = evlist__first(evlist)->core.nr_members;
4948

50-
ret = evlist__expand_cgroup(evlist, cgrp_str, metric_events, false);
49+
ret = evlist__expand_cgroup(evlist, cgrp_str, false);
5150
if (ret < 0) {
5251
pr_debug("failed to expand events for cgroups\n");
5352
goto out;
@@ -100,13 +99,11 @@ out: for (i = 0; i < nr_events; i++)
10099
static int expand_default_events(void)
101100
{
102101
int ret;
103-
struct rblist metric_events;
104102
struct evlist *evlist = evlist__new_default();
105103

106104
TEST_ASSERT_VAL("failed to get evlist", evlist);
107105

108-
rblist__init(&metric_events);
109-
ret = test_expand_events(evlist, &metric_events);
106+
ret = test_expand_events(evlist);
110107
evlist__delete(evlist);
111108
return ret;
112109
}
@@ -115,7 +112,6 @@ static int expand_group_events(void)
115112
{
116113
int ret;
117114
struct evlist *evlist;
118-
struct rblist metric_events;
119115
struct parse_events_error err;
120116
const char event_str[] = "{cycles,instructions}";
121117

@@ -132,8 +128,7 @@ static int expand_group_events(void)
132128
goto out;
133129
}
134130

135-
rblist__init(&metric_events);
136-
ret = test_expand_events(evlist, &metric_events);
131+
ret = test_expand_events(evlist);
137132
out:
138133
parse_events_error__exit(&err);
139134
evlist__delete(evlist);
@@ -144,7 +139,6 @@ static int expand_libpfm_events(void)
144139
{
145140
int ret;
146141
struct evlist *evlist;
147-
struct rblist metric_events;
148142
const char event_str[] = "CYCLES";
149143
struct option opt = {
150144
.value = &evlist,
@@ -166,8 +160,7 @@ static int expand_libpfm_events(void)
166160
goto out;
167161
}
168162

169-
rblist__init(&metric_events);
170-
ret = test_expand_events(evlist, &metric_events);
163+
ret = test_expand_events(evlist);
171164
out:
172165
evlist__delete(evlist);
173166
return ret;
@@ -177,25 +170,22 @@ static int expand_metric_events(void)
177170
{
178171
int ret;
179172
struct evlist *evlist;
180-
struct rblist metric_events;
181173
const char metric_str[] = "CPI";
182174
const struct pmu_metrics_table *pme_test;
183175

184176
evlist = evlist__new();
185177
TEST_ASSERT_VAL("failed to get evlist", evlist);
186178

187-
rblist__init(&metric_events);
188179
pme_test = find_core_metrics_table("testarch", "testcpu");
189-
ret = metricgroup__parse_groups_test(evlist, pme_test, metric_str, &metric_events);
180+
ret = metricgroup__parse_groups_test(evlist, pme_test, metric_str);
190181
if (ret < 0) {
191182
pr_debug("failed to parse '%s' metric\n", metric_str);
192183
goto out;
193184
}
194185

195-
ret = test_expand_events(evlist, &metric_events);
186+
ret = test_expand_events(evlist);
196187

197188
out:
198-
metricgroup__rblist_exit(&metric_events);
199189
evlist__delete(evlist);
200190
return ret;
201191
}

tools/perf/tests/parse-metric.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,14 @@ static void load_runtime_stat(struct evlist *evlist, struct value *vals)
4545
}
4646
}
4747

48-
static double compute_single(struct rblist *metric_events, struct evlist *evlist,
49-
const char *name)
48+
static double compute_single(struct evlist *evlist, const char *name)
5049
{
5150
struct metric_expr *mexp;
5251
struct metric_event *me;
5352
struct evsel *evsel;
5453

5554
evlist__for_each_entry(evlist, evsel) {
56-
me = metricgroup__lookup(metric_events, evsel, false);
55+
me = metricgroup__lookup(&evlist->metric_events, evsel, false);
5756
if (me != NULL) {
5857
list_for_each_entry (mexp, &me->head, nd) {
5958
if (strcmp(mexp->metric_name, name))
@@ -69,9 +68,6 @@ static int __compute_metric(const char *name, struct value *vals,
6968
const char *name1, double *ratio1,
7069
const char *name2, double *ratio2)
7170
{
72-
struct rblist metric_events = {
73-
.nr_entries = 0,
74-
};
7571
const struct pmu_metrics_table *pme_test;
7672
struct perf_cpu_map *cpus;
7773
struct evlist *evlist;
@@ -95,8 +91,7 @@ static int __compute_metric(const char *name, struct value *vals,
9591

9692
/* Parse the metric into metric_events list. */
9793
pme_test = find_core_metrics_table("testarch", "testcpu");
98-
err = metricgroup__parse_groups_test(evlist, pme_test, name,
99-
&metric_events);
94+
err = metricgroup__parse_groups_test(evlist, pme_test, name);
10095
if (err)
10196
goto out;
10297

@@ -109,13 +104,12 @@ static int __compute_metric(const char *name, struct value *vals,
109104

110105
/* And execute the metric */
111106
if (name1 && ratio1)
112-
*ratio1 = compute_single(&metric_events, evlist, name1);
107+
*ratio1 = compute_single(evlist, name1);
113108
if (name2 && ratio2)
114-
*ratio2 = compute_single(&metric_events, evlist, name2);
109+
*ratio2 = compute_single(evlist, name2);
115110

116111
out:
117112
/* ... cleanup. */
118-
metricgroup__rblist_exit(&metric_events);
119113
evlist__free_stats(evlist);
120114
perf_cpu_map__put(cpus);
121115
evlist__delete(evlist);

tools/perf/tests/pmu-events.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -868,9 +868,6 @@ static int test__parsing_callback(const struct pmu_metric *pm,
868868
struct evlist *evlist;
869869
struct perf_cpu_map *cpus;
870870
struct evsel *evsel;
871-
struct rblist metric_events = {
872-
.nr_entries = 0,
873-
};
874871
int err = 0;
875872

876873
if (!pm->metric_expr)
@@ -895,7 +892,7 @@ static int test__parsing_callback(const struct pmu_metric *pm,
895892

896893
perf_evlist__set_maps(&evlist->core, cpus, NULL);
897894

898-
err = metricgroup__parse_groups_test(evlist, table, pm->metric_name, &metric_events);
895+
err = metricgroup__parse_groups_test(evlist, table, pm->metric_name);
899896
if (err) {
900897
if (!strcmp(pm->metric_name, "M1") || !strcmp(pm->metric_name, "M2") ||
901898
!strcmp(pm->metric_name, "M3")) {
@@ -922,7 +919,7 @@ static int test__parsing_callback(const struct pmu_metric *pm,
922919
k++;
923920
}
924921
evlist__for_each_entry(evlist, evsel) {
925-
struct metric_event *me = metricgroup__lookup(&metric_events, evsel, false);
922+
struct metric_event *me = metricgroup__lookup(&evlist->metric_events, evsel, false);
926923

927924
if (me != NULL) {
928925
struct metric_expr *mexp;
@@ -944,7 +941,6 @@ static int test__parsing_callback(const struct pmu_metric *pm,
944941
pr_debug("Broken metric %s\n", pm->metric_name);
945942

946943
/* ... cleanup. */
947-
metricgroup__rblist_exit(&metric_events);
948944
evlist__free_stats(evlist);
949945
perf_cpu_map__put(cpus);
950946
evlist__delete(evlist);

tools/perf/util/cgroup.c

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,7 @@ static bool has_pattern_string(const char *str)
413413
return !!strpbrk(str, "{}[]()|*+?^$");
414414
}
415415

416-
int evlist__expand_cgroup(struct evlist *evlist, const char *str,
417-
struct rblist *metric_events, bool open_cgroup)
416+
int evlist__expand_cgroup(struct evlist *evlist, const char *str, bool open_cgroup)
418417
{
419418
struct evlist *orig_list, *tmp_list;
420419
struct evsel *pos, *evsel, *leader;
@@ -440,12 +439,8 @@ int evlist__expand_cgroup(struct evlist *evlist, const char *str,
440439
evlist__splice_list_tail(orig_list, &evlist->core.entries);
441440
evlist->core.nr_entries = 0;
442441

443-
if (metric_events) {
444-
orig_metric_events = *metric_events;
445-
rblist__init(metric_events);
446-
} else {
447-
rblist__init(&orig_metric_events);
448-
}
442+
orig_metric_events = evlist->metric_events;
443+
metricgroup__rblist_init(&evlist->metric_events);
449444

450445
if (has_pattern_string(str))
451446
prefix_len = match_cgroups(str);
@@ -490,12 +485,10 @@ int evlist__expand_cgroup(struct evlist *evlist, const char *str,
490485
cgroup__put(cgrp);
491486
nr_cgroups++;
492487

493-
if (metric_events) {
494-
if (metricgroup__copy_metric_events(tmp_list, cgrp,
495-
metric_events,
496-
&orig_metric_events) < 0)
497-
goto out_err;
498-
}
488+
if (metricgroup__copy_metric_events(tmp_list, cgrp,
489+
&evlist->metric_events,
490+
&orig_metric_events) < 0)
491+
goto out_err;
499492

500493
evlist__splice_list_tail(evlist, &tmp_list->core.entries);
501494
tmp_list->core.nr_entries = 0;
@@ -512,7 +505,7 @@ int evlist__expand_cgroup(struct evlist *evlist, const char *str,
512505
out_err:
513506
evlist__delete(orig_list);
514507
evlist__delete(tmp_list);
515-
rblist__exit(&orig_metric_events);
508+
metricgroup__rblist_exit(&orig_metric_events);
516509
release_cgroup_list();
517510

518511
return ret;

tools/perf/util/cgroup.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ struct rblist;
2828

2929
struct cgroup *cgroup__new(const char *name, bool do_open);
3030
struct cgroup *evlist__findnew_cgroup(struct evlist *evlist, const char *name);
31-
int evlist__expand_cgroup(struct evlist *evlist, const char *cgroups,
32-
struct rblist *metric_events, bool open_cgroup);
31+
int evlist__expand_cgroup(struct evlist *evlist, const char *cgroups, bool open_cgroup);
3332

3433
void evlist__set_default_cgroup(struct evlist *evlist, struct cgroup *cgroup);
3534

tools/perf/util/evlist.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "util/util.h"
3636
#include "util/env.h"
3737
#include "util/intel-tpebs.h"
38+
#include "util/metricgroup.h"
3839
#include "util/strbuf.h"
3940
#include <signal.h>
4041
#include <unistd.h>
@@ -83,6 +84,7 @@ void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
8384
evlist->ctl_fd.ack = -1;
8485
evlist->ctl_fd.pos = -1;
8586
evlist->nr_br_cntr = -1;
87+
metricgroup__rblist_init(&evlist->metric_events);
8688
}
8789

8890
struct evlist *evlist__new(void)
@@ -173,6 +175,7 @@ static void evlist__purge(struct evlist *evlist)
173175

174176
void evlist__exit(struct evlist *evlist)
175177
{
178+
metricgroup__rblist_exit(&evlist->metric_events);
176179
event_enable_timer__exit(&evlist->eet);
177180
zfree(&evlist->mmap);
178181
zfree(&evlist->overwrite_mmap);

tools/perf/util/evlist.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <perf/evlist.h>
1313
#include "events_stats.h"
1414
#include "evsel.h"
15+
#include "rblist.h"
1516
#include <pthread.h>
1617
#include <signal.h>
1718
#include <unistd.h>
@@ -86,6 +87,11 @@ struct evlist {
8687
int pos; /* index at evlist core object to check signals */
8788
} ctl_fd;
8889
struct event_enable_timer *eet;
90+
/**
91+
* @metric_events: A list of struct metric_event which each have a list
92+
* of struct metric_expr.
93+
*/
94+
struct rblist metric_events;
8995
};
9096

9197
struct evsel_str_handler {

0 commit comments

Comments
 (0)