xref: /openbmc/linux/tools/perf/bench/pmu-scan.c (revision 1eaf496ed386934f1c2439a120fe84a05194f91a)
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;
25f6a7bbbfSNamhyung Kim };
26f6a7bbbfSNamhyung Kim 
27f6a7bbbfSNamhyung Kim static const struct option options[] = {
28f6a7bbbfSNamhyung Kim 	OPT_UINTEGER('i', "iterations", &iterations,
29f6a7bbbfSNamhyung Kim 		"Number of iterations used to compute average"),
30f6a7bbbfSNamhyung Kim 	OPT_END()
31f6a7bbbfSNamhyung Kim };
32f6a7bbbfSNamhyung Kim 
33f6a7bbbfSNamhyung Kim static const char *const bench_usage[] = {
34f6a7bbbfSNamhyung Kim 	"perf bench internals pmu-scan <options>",
35f6a7bbbfSNamhyung Kim 	NULL
36f6a7bbbfSNamhyung Kim };
37f6a7bbbfSNamhyung Kim 
38f6a7bbbfSNamhyung Kim static int nr_pmus;
39f6a7bbbfSNamhyung Kim static struct pmu_scan_result *results;
40f6a7bbbfSNamhyung Kim 
41f6a7bbbfSNamhyung Kim static int save_result(void)
42f6a7bbbfSNamhyung Kim {
43f24ebe80SIan Rogers 	struct perf_pmu *pmu = NULL;
44f6a7bbbfSNamhyung Kim 	struct list_head *list;
45f6a7bbbfSNamhyung Kim 	struct pmu_scan_result *r;
46f6a7bbbfSNamhyung Kim 
47*1eaf496eSIan Rogers 	while ((pmu = perf_pmus__scan(pmu)) != NULL) {
48f6a7bbbfSNamhyung Kim 		r = realloc(results, (nr_pmus + 1) * sizeof(*r));
49f6a7bbbfSNamhyung Kim 		if (r == NULL)
50f6a7bbbfSNamhyung Kim 			return -ENOMEM;
51f6a7bbbfSNamhyung Kim 
52f6a7bbbfSNamhyung Kim 		results = r;
53f6a7bbbfSNamhyung Kim 		r = results + nr_pmus;
54f6a7bbbfSNamhyung Kim 
55f6a7bbbfSNamhyung Kim 		r->name = strdup(pmu->name);
56f6a7bbbfSNamhyung Kim 		r->nr_caps = pmu->nr_caps;
57f6a7bbbfSNamhyung Kim 
58f6a7bbbfSNamhyung Kim 		r->nr_aliases = 0;
59f6a7bbbfSNamhyung Kim 		list_for_each(list, &pmu->aliases)
60f6a7bbbfSNamhyung Kim 			r->nr_aliases++;
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 
71*1eaf496eSIan Rogers 	perf_pmus__destroy();
72f6a7bbbfSNamhyung Kim 	return 0;
73f6a7bbbfSNamhyung Kim }
74f6a7bbbfSNamhyung Kim 
75f6a7bbbfSNamhyung Kim static int check_result(void)
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];
84*1eaf496eSIan Rogers 		pmu = perf_pmus__find(r->name);
85f6a7bbbfSNamhyung Kim 		if (pmu == NULL) {
86f6a7bbbfSNamhyung Kim 			pr_err("Cannot find PMU %s\n", r->name);
87f6a7bbbfSNamhyung Kim 			return -1;
88f6a7bbbfSNamhyung Kim 		}
89f6a7bbbfSNamhyung Kim 
90f6a7bbbfSNamhyung Kim 		if (pmu->nr_caps != (u32)r->nr_caps) {
91f6a7bbbfSNamhyung Kim 			pr_err("Unmatched number of event caps in %s: expect %d vs got %d\n",
92f6a7bbbfSNamhyung Kim 				pmu->name, r->nr_caps, pmu->nr_caps);
93f6a7bbbfSNamhyung Kim 			return -1;
94f6a7bbbfSNamhyung Kim 		}
95f6a7bbbfSNamhyung Kim 
96f6a7bbbfSNamhyung Kim 		nr = 0;
97f6a7bbbfSNamhyung Kim 		list_for_each(list, &pmu->aliases)
98f6a7bbbfSNamhyung Kim 			nr++;
99f6a7bbbfSNamhyung Kim 		if (nr != r->nr_aliases) {
100f6a7bbbfSNamhyung Kim 			pr_err("Unmatched number of event aliases in %s: expect %d vs got %d\n",
101f6a7bbbfSNamhyung Kim 				pmu->name, r->nr_aliases, nr);
102f6a7bbbfSNamhyung Kim 			return -1;
103f6a7bbbfSNamhyung Kim 		}
104f6a7bbbfSNamhyung Kim 
105f6a7bbbfSNamhyung Kim 		nr = 0;
106f6a7bbbfSNamhyung Kim 		list_for_each(list, &pmu->format)
107f6a7bbbfSNamhyung Kim 			nr++;
108f6a7bbbfSNamhyung Kim 		if (nr != r->nr_formats) {
109f6a7bbbfSNamhyung Kim 			pr_err("Unmatched number of event formats in %s: expect %d vs got %d\n",
110f6a7bbbfSNamhyung Kim 				pmu->name, r->nr_formats, nr);
111f6a7bbbfSNamhyung Kim 			return -1;
112f6a7bbbfSNamhyung Kim 		}
113f6a7bbbfSNamhyung Kim 	}
114f6a7bbbfSNamhyung Kim 	return 0;
115f6a7bbbfSNamhyung Kim }
116f6a7bbbfSNamhyung Kim 
117f6a7bbbfSNamhyung Kim static void delete_result(void)
118f6a7bbbfSNamhyung Kim {
119f6a7bbbfSNamhyung Kim 	for (int i = 0; i < nr_pmus; i++)
120f6a7bbbfSNamhyung Kim 		free(results[i].name);
121f6a7bbbfSNamhyung Kim 	free(results);
122f6a7bbbfSNamhyung Kim 
123f6a7bbbfSNamhyung Kim 	results = NULL;
124f6a7bbbfSNamhyung Kim 	nr_pmus = 0;
125f6a7bbbfSNamhyung Kim }
126f6a7bbbfSNamhyung Kim 
127f6a7bbbfSNamhyung Kim static int run_pmu_scan(void)
128f6a7bbbfSNamhyung Kim {
129f6a7bbbfSNamhyung Kim 	struct stats stats;
130f6a7bbbfSNamhyung Kim 	struct timeval start, end, diff;
131f6a7bbbfSNamhyung Kim 	double time_average, time_stddev;
132f6a7bbbfSNamhyung Kim 	u64 runtime_us;
133f6a7bbbfSNamhyung Kim 	unsigned int i;
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 
145f6a7bbbfSNamhyung Kim 	for (i = 0; i < iterations; i++) {
146f6a7bbbfSNamhyung Kim 		gettimeofday(&start, NULL);
147*1eaf496eSIan Rogers 		perf_pmus__scan(NULL);
148f6a7bbbfSNamhyung Kim 		gettimeofday(&end, NULL);
149f6a7bbbfSNamhyung Kim 
150f6a7bbbfSNamhyung Kim 		timersub(&end, &start, &diff);
151f6a7bbbfSNamhyung Kim 		runtime_us = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
152f6a7bbbfSNamhyung Kim 		update_stats(&stats, runtime_us);
153f6a7bbbfSNamhyung Kim 
154f6a7bbbfSNamhyung Kim 		ret = check_result();
155*1eaf496eSIan Rogers 		perf_pmus__destroy();
156f6a7bbbfSNamhyung Kim 		if (ret < 0)
157f6a7bbbfSNamhyung Kim 			break;
158f6a7bbbfSNamhyung Kim 	}
159f6a7bbbfSNamhyung Kim 
160f6a7bbbfSNamhyung Kim 	time_average = avg_stats(&stats);
161f6a7bbbfSNamhyung Kim 	time_stddev = stddev_stats(&stats);
162f6a7bbbfSNamhyung Kim 	pr_info("  Average PMU scanning took: %.3f usec (+- %.3f usec)\n",
163f6a7bbbfSNamhyung Kim 		time_average, time_stddev);
164f6a7bbbfSNamhyung Kim 
165f6a7bbbfSNamhyung Kim 	delete_result();
166f6a7bbbfSNamhyung Kim 	return 0;
167f6a7bbbfSNamhyung Kim }
168f6a7bbbfSNamhyung Kim 
169f6a7bbbfSNamhyung Kim int bench_pmu_scan(int argc, const char **argv)
170f6a7bbbfSNamhyung Kim {
171f6a7bbbfSNamhyung Kim 	int err = 0;
172f6a7bbbfSNamhyung Kim 
173f6a7bbbfSNamhyung Kim 	argc = parse_options(argc, argv, options, bench_usage, 0);
174f6a7bbbfSNamhyung Kim 	if (argc) {
175f6a7bbbfSNamhyung Kim 		usage_with_options(bench_usage, options);
176f6a7bbbfSNamhyung Kim 		exit(EXIT_FAILURE);
177f6a7bbbfSNamhyung Kim 	}
178f6a7bbbfSNamhyung Kim 
179f6a7bbbfSNamhyung Kim 	err = run_pmu_scan();
180f6a7bbbfSNamhyung Kim 
181f6a7bbbfSNamhyung Kim 	return err;
182f6a7bbbfSNamhyung Kim }
183