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