Skip to content

Commit faebee1

Browse files
captain5050namhyung
authored andcommitted
perf stat: Move metric list from config to evlist
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>
1 parent cb336b6 commit faebee1

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
@@ -2136,8 +2136,7 @@ static void perf_sample__fprint_metric(struct perf_script *script,
21362136
perf_stat__print_shadow_stats(&stat_config, ev2,
21372137
evsel_script(ev2)->val,
21382138
sample->cpu,
2139-
&ctx,
2140-
NULL);
2139+
&ctx);
21412140
}
21422141
evsel_script(leader)->gnum = 0;
21432142
}

tools/perf/builtin-stat.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,8 +1863,7 @@ static int add_default_events(void)
18631863
stat_config.metric_no_threshold,
18641864
stat_config.user_requested_cpu_list,
18651865
stat_config.system_wide,
1866-
stat_config.hardware_aware_grouping,
1867-
&stat_config.metric_events);
1866+
stat_config.hardware_aware_grouping);
18681867
goto out;
18691868
}
18701869

@@ -1901,8 +1900,7 @@ static int add_default_events(void)
19011900
stat_config.metric_no_threshold,
19021901
stat_config.user_requested_cpu_list,
19031902
stat_config.system_wide,
1904-
stat_config.hardware_aware_grouping,
1905-
&stat_config.metric_events);
1903+
stat_config.hardware_aware_grouping);
19061904
goto out;
19071905
}
19081906

@@ -1939,8 +1937,7 @@ static int add_default_events(void)
19391937
/*metric_no_threshold=*/true,
19401938
stat_config.user_requested_cpu_list,
19411939
stat_config.system_wide,
1942-
stat_config.hardware_aware_grouping,
1943-
&stat_config.metric_events) < 0) {
1940+
stat_config.hardware_aware_grouping) < 0) {
19441941
ret = -1;
19451942
goto out;
19461943
}
@@ -1989,8 +1986,7 @@ static int add_default_events(void)
19891986
/*metric_no_threshold=*/true,
19901987
stat_config.user_requested_cpu_list,
19911988
stat_config.system_wide,
1992-
stat_config.hardware_aware_grouping,
1993-
&stat_config.metric_events) < 0) {
1989+
stat_config.hardware_aware_grouping) < 0) {
19941990
ret = -1;
19951991
goto out;
19961992
}
@@ -1999,6 +1995,9 @@ static int add_default_events(void)
19991995
evsel->default_metricgroup = true;
20001996

20011997
evlist__splice_list_tail(evlist, &metric_evlist->core.entries);
1998+
metricgroup__copy_metric_events(evlist, /*cgrp=*/NULL,
1999+
&evlist->metric_events,
2000+
&metric_evlist->metric_events);
20022001
evlist__delete(metric_evlist);
20032002
}
20042003
}
@@ -2053,6 +2052,9 @@ static int add_default_events(void)
20532052
}
20542053
parse_events_error__exit(&err);
20552054
evlist__splice_list_tail(evsel_list, &evlist->core.entries);
2055+
metricgroup__copy_metric_events(evsel_list, /*cgrp=*/NULL,
2056+
&evsel_list->metric_events,
2057+
&evlist->metric_events);
20562058
evlist__delete(evlist);
20572059
return ret;
20582060
}
@@ -2739,8 +2741,7 @@ int cmd_stat(int argc, const char **argv)
27392741
stat_config.metric_no_threshold,
27402742
stat_config.user_requested_cpu_list,
27412743
stat_config.system_wide,
2742-
stat_config.hardware_aware_grouping,
2743-
&stat_config.metric_events);
2744+
stat_config.hardware_aware_grouping);
27442745

27452746
zfree(&metrics);
27462747
if (ret) {
@@ -2760,8 +2761,7 @@ int cmd_stat(int argc, const char **argv)
27602761
goto out;
27612762
}
27622763

2763-
if (evlist__expand_cgroup(evsel_list, stat_config.cgroup_list,
2764-
&stat_config.metric_events, true) < 0) {
2764+
if (evlist__expand_cgroup(evsel_list, stat_config.cgroup_list, true) < 0) {
27652765
parse_options_usage(stat_usage, stat_options,
27662766
"for-each-cgroup", 0);
27672767
goto out;
@@ -2936,7 +2936,6 @@ int cmd_stat(int argc, const char **argv)
29362936

29372937
evlist__delete(evsel_list);
29382938

2939-
metricgroup__rblist_exit(&stat_config.metric_events);
29402939
evlist__close_control(stat_config.ctl_fd, stat_config.ctl_fd_ack, &stat_config.ctl_fd_close);
29412940

29422941
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)