summaryrefslogtreecommitdiff
path: root/tools/perf/bench/pmu-scan.c
blob: 9e4d36486f6255ce739e0fe53446e48f27d365f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// SPDX-License-Identifier: GPL-2.0
/*
 * Benchmark scanning sysfs files for PMU information.
 *
 * Copyright 2023 Google LLC.
 */
#include <stdio.h>
#include "bench.h"
#include "util/debug.h"
#include "util/pmu.h"
#include "util/pmus.h"
#include "util/stat.h"
#include <linux/atomic.h>
#include <linux/err.h>
#include <linux/time64.h>
#include <subcmd/parse-options.h>

static unsigned int iterations = 100;

struct pmu_scan_result {
	char *name;
	int nr_aliases;
	int nr_formats;
	int nr_caps;
	bool is_core;
};

static const struct option options[] = {
	OPT_UINTEGER('i', "iterations", &iterations,
		"Number of iterations used to compute average"),
	OPT_END()
};

static const char *const bench_usage[] = {
	"perf bench internals pmu-scan <options>",
	NULL
};

static int nr_pmus;
static struct pmu_scan_result *results;

static int save_result(void)
{
	struct perf_pmu *pmu = NULL;
	struct list_head *list;
	struct pmu_scan_result *r;

	while ((pmu = perf_pmus__scan(pmu)) != NULL) {
		r = realloc(results, (nr_pmus + 1) * sizeof(*r));
		if (r == NULL)
			return -ENOMEM;

		results = r;
		r = results + nr_pmus;

		r->name = strdup(pmu->name);
		r->is_core = pmu->is_core;
		r->nr_caps = pmu->nr_caps;

		r->nr_aliases = perf_pmu__num_events(pmu);

		r->nr_formats = 0;
		list_for_each(list, &pmu->format)
			r->nr_formats++;

		pr_debug("pmu[%d] name=%s, nr_caps=%d, nr_aliases=%d, nr_formats=%d\n",
			nr_pmus, r->name, r->nr_caps, r->nr_aliases, r->nr_formats);
		nr_pmus++;
	}

	perf_pmus__destroy();
	return 0;
}

static int check_result(bool core_only)
{
	struct pmu_scan_result *r;
	struct perf_pmu *pmu;
	struct list_head *list;
	int nr;

	for (int i = 0; i < nr_pmus; i++) {
		r = &results[i];
		if (core_only && !r->is_core)
			continue;

		pmu = perf_pmus__find(r->name);
		if (pmu == NULL) {
			pr_err("Cannot find PMU %s\n", r->name);
			return -1;
		}

		if (pmu->nr_caps != (u32)r->nr_caps) {
			pr_err("Unmatched number of event caps in %s: expect %d vs got %d\n",
				pmu->name, r->nr_caps, pmu->nr_caps);
			return -1;
		}

		nr = perf_pmu__num_events(pmu);
		if (nr != r->nr_aliases) {
			pr_err("Unmatched number of event aliases in %s: expect %d vs got %d\n",
				pmu->name, r->nr_aliases, nr);
			return -1;
		}

		nr = 0;
		list_for_each(list, &pmu->format)
			nr++;
		if (nr != r->nr_formats) {
			pr_err("Unmatched number of event formats in %s: expect %d vs got %d\n",
				pmu->name, r->nr_formats, nr);
			return -1;
		}
	}
	return 0;
}

static void delete_result(void)
{
	for (int i = 0; i < nr_pmus; i++)
		free(results[i].name);
	free(results);

	results = NULL;
	nr_pmus = 0;
}

static int run_pmu_scan(void)
{
	struct stats stats;
	struct timeval start, end, diff;
	double time_average, time_stddev;
	u64 runtime_us;
	int ret;

	init_stats(&stats);
	pr_info("Computing performance of sysfs PMU event scan for %u times\n",
		iterations);

	if (save_result() < 0) {
		pr_err("Failed to initialize PMU scan result\n");
		return -1;
	}

	for (int j = 0; j < 2; j++) {
		bool core_only = (j == 0);

		for (unsigned int i = 0; i < iterations; i++) {
			gettimeofday(&start, NULL);
			if (core_only)
				perf_pmus__scan_core(NULL);
			else
				perf_pmus__scan(NULL);
			gettimeofday(&end, NULL);
			timersub(&end, &start, &diff);
			runtime_us = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
			update_stats(&stats, runtime_us);

			ret = check_result(core_only);
			perf_pmus__destroy();
			if (ret < 0)
				break;
		}
		time_average = avg_stats(&stats);
		time_stddev = stddev_stats(&stats);
		pr_info("  Average%s PMU scanning took: %.3f usec (+- %.3f usec)\n",
			core_only ? " core" : "", time_average, time_stddev);
	}
	delete_result();
	return 0;
}

int bench_pmu_scan(int argc, const char **argv)
{
	int err = 0;

	argc = parse_options(argc, argv, options, bench_usage, 0);
	if (argc) {
		usage_with_options(bench_usage, options);
		exit(EXIT_FAILURE);
	}

	err = run_pmu_scan();

	return err;
}