1f6a7bbbfSNamhyung Kim // SPDX-License-Identifier: GPL-2.0
2f6a7bbbfSNamhyung Kim /*
3f6a7bbbfSNamhyung Kim * Benchmark scanning sysfs files for PMU information.
4f6a7bbbfSNamhyung Kim *
5f6a7bbbfSNamhyung Kim * Copyright 2023 Google LLC.
6f6a7bbbfSNamhyung Kim */
7f6a7bbbfSNamhyung Kim #include <stdio.h>
8f6a7bbbfSNamhyung Kim #include "bench.h"
9f6a7bbbfSNamhyung Kim #include "util/debug.h"
10f6a7bbbfSNamhyung Kim #include "util/pmu.h"
11f6a7bbbfSNamhyung Kim #include "util/pmus.h"
12f6a7bbbfSNamhyung Kim #include "util/stat.h"
13f6a7bbbfSNamhyung Kim #include <linux/atomic.h>
14f6a7bbbfSNamhyung Kim #include <linux/err.h>
15f6a7bbbfSNamhyung Kim #include <linux/time64.h>
16f6a7bbbfSNamhyung Kim #include <subcmd/parse-options.h>
17f6a7bbbfSNamhyung Kim
18f6a7bbbfSNamhyung Kim static unsigned int iterations = 100;
19f6a7bbbfSNamhyung Kim
20f6a7bbbfSNamhyung Kim struct pmu_scan_result {
21f6a7bbbfSNamhyung Kim char *name;
22f6a7bbbfSNamhyung Kim int nr_aliases;
23f6a7bbbfSNamhyung Kim int nr_formats;
24f6a7bbbfSNamhyung Kim int nr_caps;
259d6a1df9SIan Rogers bool is_core;
26f6a7bbbfSNamhyung Kim };
27f6a7bbbfSNamhyung Kim
28f6a7bbbfSNamhyung Kim static const struct option options[] = {
29f6a7bbbfSNamhyung Kim OPT_UINTEGER('i', "iterations", &iterations,
30f6a7bbbfSNamhyung Kim "Number of iterations used to compute average"),
31f6a7bbbfSNamhyung Kim OPT_END()
32f6a7bbbfSNamhyung Kim };
33f6a7bbbfSNamhyung Kim
34f6a7bbbfSNamhyung Kim static const char *const bench_usage[] = {
35f6a7bbbfSNamhyung Kim "perf bench internals pmu-scan <options>",
36f6a7bbbfSNamhyung Kim NULL
37f6a7bbbfSNamhyung Kim };
38f6a7bbbfSNamhyung Kim
39f6a7bbbfSNamhyung Kim static int nr_pmus;
40f6a7bbbfSNamhyung Kim static struct pmu_scan_result *results;
41f6a7bbbfSNamhyung Kim
save_result(void)42f6a7bbbfSNamhyung Kim static int save_result(void)
43f6a7bbbfSNamhyung Kim {
44f24ebe80SIan Rogers struct perf_pmu *pmu = NULL;
45f6a7bbbfSNamhyung Kim struct list_head *list;
46f6a7bbbfSNamhyung Kim struct pmu_scan_result *r;
47f6a7bbbfSNamhyung Kim
481eaf496eSIan Rogers while ((pmu = perf_pmus__scan(pmu)) != NULL) {
49f6a7bbbfSNamhyung Kim r = realloc(results, (nr_pmus + 1) * sizeof(*r));
50f6a7bbbfSNamhyung Kim if (r == NULL)
51f6a7bbbfSNamhyung Kim return -ENOMEM;
52f6a7bbbfSNamhyung Kim
53f6a7bbbfSNamhyung Kim results = r;
54f6a7bbbfSNamhyung Kim r = results + nr_pmus;
55f6a7bbbfSNamhyung Kim
56f6a7bbbfSNamhyung Kim r->name = strdup(pmu->name);
579d6a1df9SIan Rogers r->is_core = pmu->is_core;
58f6a7bbbfSNamhyung Kim r->nr_caps = pmu->nr_caps;
59f6a7bbbfSNamhyung Kim
60*c3245d20SIan Rogers r->nr_aliases = perf_pmu__num_events(pmu);
61f6a7bbbfSNamhyung Kim
62f6a7bbbfSNamhyung Kim r->nr_formats = 0;
63f6a7bbbfSNamhyung Kim list_for_each(list, &pmu->format)
64f6a7bbbfSNamhyung Kim r->nr_formats++;
65f6a7bbbfSNamhyung Kim
66f6a7bbbfSNamhyung Kim pr_debug("pmu[%d] name=%s, nr_caps=%d, nr_aliases=%d, nr_formats=%d\n",
67f6a7bbbfSNamhyung Kim nr_pmus, r->name, r->nr_caps, r->nr_aliases, r->nr_formats);
68f6a7bbbfSNamhyung Kim nr_pmus++;
69f6a7bbbfSNamhyung Kim }
70f6a7bbbfSNamhyung Kim
711eaf496eSIan Rogers perf_pmus__destroy();
72f6a7bbbfSNamhyung Kim return 0;
73f6a7bbbfSNamhyung Kim }
74f6a7bbbfSNamhyung Kim
check_result(bool core_only)759d6a1df9SIan Rogers static int check_result(bool core_only)
76f6a7bbbfSNamhyung Kim {
77f6a7bbbfSNamhyung Kim struct pmu_scan_result *r;
78f6a7bbbfSNamhyung Kim struct perf_pmu *pmu;
79f6a7bbbfSNamhyung Kim struct list_head *list;
80f6a7bbbfSNamhyung Kim int nr;
81f6a7bbbfSNamhyung Kim
82f6a7bbbfSNamhyung Kim for (int i = 0; i < nr_pmus; i++) {
83f6a7bbbfSNamhyung Kim r = &results[i];
849d6a1df9SIan Rogers if (core_only && !r->is_core)
859d6a1df9SIan Rogers continue;
869d6a1df9SIan Rogers
871eaf496eSIan Rogers pmu = perf_pmus__find(r->name);
88f6a7bbbfSNamhyung Kim if (pmu == NULL) {
89f6a7bbbfSNamhyung Kim pr_err("Cannot find PMU %s\n", r->name);
90f6a7bbbfSNamhyung Kim return -1;
91f6a7bbbfSNamhyung Kim }
92f6a7bbbfSNamhyung Kim
93f6a7bbbfSNamhyung Kim if (pmu->nr_caps != (u32)r->nr_caps) {
94f6a7bbbfSNamhyung Kim pr_err("Unmatched number of event caps in %s: expect %d vs got %d\n",
95f6a7bbbfSNamhyung Kim pmu->name, r->nr_caps, pmu->nr_caps);
96f6a7bbbfSNamhyung Kim return -1;
97f6a7bbbfSNamhyung Kim }
98f6a7bbbfSNamhyung Kim
99*c3245d20SIan Rogers nr = perf_pmu__num_events(pmu);
100f6a7bbbfSNamhyung Kim if (nr != r->nr_aliases) {
101f6a7bbbfSNamhyung Kim pr_err("Unmatched number of event aliases in %s: expect %d vs got %d\n",
102f6a7bbbfSNamhyung Kim pmu->name, r->nr_aliases, nr);
103f6a7bbbfSNamhyung Kim return -1;
104f6a7bbbfSNamhyung Kim }
105f6a7bbbfSNamhyung Kim
106f6a7bbbfSNamhyung Kim nr = 0;
107f6a7bbbfSNamhyung Kim list_for_each(list, &pmu->format)
108f6a7bbbfSNamhyung Kim nr++;
109f6a7bbbfSNamhyung Kim if (nr != r->nr_formats) {
110f6a7bbbfSNamhyung Kim pr_err("Unmatched number of event formats in %s: expect %d vs got %d\n",
111f6a7bbbfSNamhyung Kim pmu->name, r->nr_formats, nr);
112f6a7bbbfSNamhyung Kim return -1;
113f6a7bbbfSNamhyung Kim }
114f6a7bbbfSNamhyung Kim }
115f6a7bbbfSNamhyung Kim return 0;
116f6a7bbbfSNamhyung Kim }
117f6a7bbbfSNamhyung Kim
delete_result(void)118f6a7bbbfSNamhyung Kim static void delete_result(void)
119f6a7bbbfSNamhyung Kim {
120f6a7bbbfSNamhyung Kim for (int i = 0; i < nr_pmus; i++)
121f6a7bbbfSNamhyung Kim free(results[i].name);
122f6a7bbbfSNamhyung Kim free(results);
123f6a7bbbfSNamhyung Kim
124f6a7bbbfSNamhyung Kim results = NULL;
125f6a7bbbfSNamhyung Kim nr_pmus = 0;
126f6a7bbbfSNamhyung Kim }
127f6a7bbbfSNamhyung Kim
run_pmu_scan(void)128f6a7bbbfSNamhyung Kim static int run_pmu_scan(void)
129f6a7bbbfSNamhyung Kim {
130f6a7bbbfSNamhyung Kim struct stats stats;
131f6a7bbbfSNamhyung Kim struct timeval start, end, diff;
132f6a7bbbfSNamhyung Kim double time_average, time_stddev;
133f6a7bbbfSNamhyung Kim u64 runtime_us;
134f6a7bbbfSNamhyung Kim int ret;
135f6a7bbbfSNamhyung Kim
136f6a7bbbfSNamhyung Kim init_stats(&stats);
137f6a7bbbfSNamhyung Kim pr_info("Computing performance of sysfs PMU event scan for %u times\n",
138f6a7bbbfSNamhyung Kim iterations);
139f6a7bbbfSNamhyung Kim
140f6a7bbbfSNamhyung Kim if (save_result() < 0) {
141f6a7bbbfSNamhyung Kim pr_err("Failed to initialize PMU scan result\n");
142f6a7bbbfSNamhyung Kim return -1;
143f6a7bbbfSNamhyung Kim }
144f6a7bbbfSNamhyung Kim
1459d6a1df9SIan Rogers for (int j = 0; j < 2; j++) {
1469d6a1df9SIan Rogers bool core_only = (j == 0);
1479d6a1df9SIan Rogers
1489d6a1df9SIan Rogers for (unsigned int i = 0; i < iterations; i++) {
149f6a7bbbfSNamhyung Kim gettimeofday(&start, NULL);
1509d6a1df9SIan Rogers if (core_only)
1519d6a1df9SIan Rogers perf_pmus__scan_core(NULL);
1529d6a1df9SIan Rogers else
1531eaf496eSIan Rogers perf_pmus__scan(NULL);
154f6a7bbbfSNamhyung Kim gettimeofday(&end, NULL);
155f6a7bbbfSNamhyung Kim timersub(&end, &start, &diff);
156f6a7bbbfSNamhyung Kim runtime_us = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
157f6a7bbbfSNamhyung Kim update_stats(&stats, runtime_us);
158f6a7bbbfSNamhyung Kim
1599d6a1df9SIan Rogers ret = check_result(core_only);
1601eaf496eSIan Rogers perf_pmus__destroy();
161f6a7bbbfSNamhyung Kim if (ret < 0)
162f6a7bbbfSNamhyung Kim break;
163f6a7bbbfSNamhyung Kim }
164f6a7bbbfSNamhyung Kim time_average = avg_stats(&stats);
165f6a7bbbfSNamhyung Kim time_stddev = stddev_stats(&stats);
1669d6a1df9SIan Rogers pr_info(" Average%s PMU scanning took: %.3f usec (+- %.3f usec)\n",
1679d6a1df9SIan Rogers core_only ? " core" : "", time_average, time_stddev);
1689d6a1df9SIan Rogers }
169f6a7bbbfSNamhyung Kim delete_result();
170f6a7bbbfSNamhyung Kim return 0;
171f6a7bbbfSNamhyung Kim }
172f6a7bbbfSNamhyung Kim
bench_pmu_scan(int argc,const char ** argv)173f6a7bbbfSNamhyung Kim int bench_pmu_scan(int argc, const char **argv)
174f6a7bbbfSNamhyung Kim {
175f6a7bbbfSNamhyung Kim int err = 0;
176f6a7bbbfSNamhyung Kim
177f6a7bbbfSNamhyung Kim argc = parse_options(argc, argv, options, bench_usage, 0);
178f6a7bbbfSNamhyung Kim if (argc) {
179f6a7bbbfSNamhyung Kim usage_with_options(bench_usage, options);
180f6a7bbbfSNamhyung Kim exit(EXIT_FAILURE);
181f6a7bbbfSNamhyung Kim }
182f6a7bbbfSNamhyung Kim
183f6a7bbbfSNamhyung Kim err = run_pmu_scan();
184f6a7bbbfSNamhyung Kim
185f6a7bbbfSNamhyung Kim return err;
186f6a7bbbfSNamhyung Kim }
187