xref: /openbmc/linux/tools/perf/util/pfm.c (revision 5d7800d9)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Support for libpfm4 event encoding.
4  *
5  * Copyright 2020 Google LLC.
6  */
7 #include "util/cpumap.h"
8 #include "util/debug.h"
9 #include "util/event.h"
10 #include "util/evlist.h"
11 #include "util/evsel.h"
12 #include "util/parse-events.h"
13 #include "util/pmus.h"
14 #include "util/pfm.h"
15 #include "util/strbuf.h"
16 #include "util/thread_map.h"
17 
18 #include <string.h>
19 #include <linux/kernel.h>
20 #include <perfmon/pfmlib_perf_event.h>
21 
22 static void libpfm_initialize(void)
23 {
24 	int ret;
25 
26 	ret = pfm_initialize();
27 	if (ret != PFM_SUCCESS) {
28 		ui__warning("libpfm failed to initialize: %s\n",
29 			pfm_strerror(ret));
30 	}
31 }
32 
33 int parse_libpfm_events_option(const struct option *opt, const char *str,
34 			int unset __maybe_unused)
35 {
36 	struct evlist *evlist = *(struct evlist **)opt->value;
37 	struct perf_event_attr attr;
38 	struct perf_pmu *pmu;
39 	struct evsel *evsel, *grp_leader = NULL;
40 	char *p, *q, *p_orig;
41 	const char *sep;
42 	int grp_evt = -1;
43 	int ret;
44 
45 	libpfm_initialize();
46 
47 	p_orig = p = strdup(str);
48 	if (!p)
49 		return -1;
50 	/*
51 	 * force loading of the PMU list
52 	 */
53 	perf_pmus__scan(NULL);
54 
55 	for (q = p; strsep(&p, ",{}"); q = p) {
56 		sep = p ? str + (p - p_orig - 1) : "";
57 		if (*sep == '{') {
58 			if (grp_evt > -1) {
59 				ui__error(
60 					"nested event groups not supported\n");
61 				goto error;
62 			}
63 			grp_evt++;
64 		}
65 
66 		/* no event */
67 		if (*q == '\0') {
68 			if (*sep == '}') {
69 				if (grp_evt < 0) {
70 					ui__error("cannot close a non-existing event group\n");
71 					goto error;
72 				}
73 				grp_evt--;
74 			}
75 			continue;
76 		}
77 
78 		memset(&attr, 0, sizeof(attr));
79 		event_attr_init(&attr);
80 
81 		ret = pfm_get_perf_event_encoding(q, PFM_PLM0|PFM_PLM3,
82 						&attr, NULL, NULL);
83 
84 		if (ret != PFM_SUCCESS) {
85 			ui__error("failed to parse event %s : %s\n", str,
86 				  pfm_strerror(ret));
87 			goto error;
88 		}
89 
90 		pmu = perf_pmus__find_by_type((unsigned int)attr.type);
91 		evsel = parse_events__add_event(evlist->core.nr_entries,
92 						&attr, q, /*metric_id=*/NULL,
93 						pmu);
94 		if (evsel == NULL)
95 			goto error;
96 
97 		evsel->is_libpfm_event = true;
98 
99 		evlist__add(evlist, evsel);
100 
101 		if (grp_evt == 0)
102 			grp_leader = evsel;
103 
104 		if (grp_evt > -1) {
105 			evsel__set_leader(evsel, grp_leader);
106 			grp_leader->core.nr_members++;
107 			grp_evt++;
108 		}
109 
110 		if (*sep == '}') {
111 			if (grp_evt < 0) {
112 				ui__error(
113 				   "cannot close a non-existing event group\n");
114 				goto error;
115 			}
116 			grp_leader = NULL;
117 			grp_evt = -1;
118 		}
119 	}
120 	free(p_orig);
121 	return 0;
122 error:
123 	free(p_orig);
124 	return -1;
125 }
126 
127 static bool is_libpfm_event_supported(const char *name, struct perf_cpu_map *cpus,
128 				      struct perf_thread_map *threads)
129 {
130 	struct perf_pmu *pmu;
131 	struct evsel *evsel;
132 	struct perf_event_attr attr = {};
133 	bool result = true;
134 	int ret;
135 
136 	ret = pfm_get_perf_event_encoding(name, PFM_PLM0|PFM_PLM3,
137 					  &attr, NULL, NULL);
138 	if (ret != PFM_SUCCESS)
139 		return false;
140 
141 	pmu = perf_pmus__find_by_type((unsigned int)attr.type);
142 	evsel = parse_events__add_event(0, &attr, name, /*metric_id=*/NULL, pmu);
143 	if (evsel == NULL)
144 		return false;
145 
146 	evsel->is_libpfm_event = true;
147 
148 	if (evsel__open(evsel, cpus, threads) < 0)
149 		result = false;
150 
151 	evsel__close(evsel);
152 	evsel__delete(evsel);
153 
154 	return result;
155 }
156 
157 static const char *srcs[PFM_ATTR_CTRL_MAX] = {
158 	[PFM_ATTR_CTRL_UNKNOWN] = "???",
159 	[PFM_ATTR_CTRL_PMU] = "PMU",
160 	[PFM_ATTR_CTRL_PERF_EVENT] = "perf_event",
161 };
162 
163 static void
164 print_attr_flags(struct strbuf *buf, const pfm_event_attr_info_t *info)
165 {
166 	if (info->is_dfl)
167 		strbuf_addf(buf, "[default] ");
168 
169 	if (info->is_precise)
170 		strbuf_addf(buf, "[precise] ");
171 }
172 
173 static void
174 print_libpfm_event(const struct print_callbacks *print_cb, void *print_state,
175 		const pfm_pmu_info_t *pinfo, const pfm_event_info_t *info,
176 		struct strbuf *buf)
177 {
178 	int j, ret;
179 	char topic[80], name[80];
180 	struct perf_cpu_map *cpus = perf_cpu_map__empty_new(1);
181 	struct perf_thread_map *threads = thread_map__new_by_tid(0);
182 
183 	strbuf_setlen(buf, 0);
184 	snprintf(topic, sizeof(topic), "pfm %s", pinfo->name);
185 
186 	snprintf(name, sizeof(name), "%s::%s", pinfo->name, info->name);
187 	strbuf_addf(buf, "Code: 0x%"PRIx64"\n", info->code);
188 
189 	pfm_for_each_event_attr(j, info) {
190 		pfm_event_attr_info_t ainfo;
191 		const char *src;
192 
193 		ainfo.size = sizeof(ainfo);
194 		ret = pfm_get_event_attr_info(info->idx, j, PFM_OS_PERF_EVENT_EXT, &ainfo);
195 		if (ret != PFM_SUCCESS)
196 			continue;
197 
198 		if (ainfo.ctrl >= PFM_ATTR_CTRL_MAX)
199 			ainfo.ctrl = PFM_ATTR_CTRL_UNKNOWN;
200 
201 		src = srcs[ainfo.ctrl];
202 		switch (ainfo.type) {
203 		case PFM_ATTR_UMASK: /* Ignore for now */
204 			break;
205 		case PFM_ATTR_MOD_BOOL:
206 			strbuf_addf(buf, " Modif: %s: [%s] : %s (boolean)\n", src,
207 				    ainfo.name, ainfo.desc);
208 			break;
209 		case PFM_ATTR_MOD_INTEGER:
210 			strbuf_addf(buf, " Modif: %s: [%s] : %s (integer)\n", src,
211 				    ainfo.name, ainfo.desc);
212 			break;
213 		case PFM_ATTR_NONE:
214 		case PFM_ATTR_RAW_UMASK:
215 		case PFM_ATTR_MAX:
216 		default:
217 			strbuf_addf(buf, " Attr: %s: [%s] : %s\n", src,
218 				    ainfo.name, ainfo.desc);
219 		}
220 	}
221 
222 	if (is_libpfm_event_supported(name, cpus, threads)) {
223 		print_cb->print_event(print_state, pinfo->name, topic,
224 				      name, info->equiv,
225 				      /*scale_unit=*/NULL,
226 				      /*deprecated=*/NULL, "PFM event",
227 				      info->desc, /*long_desc=*/NULL,
228 				      /*encoding_desc=*/buf->buf);
229 	}
230 
231 	pfm_for_each_event_attr(j, info) {
232 		pfm_event_attr_info_t ainfo;
233 		const char *src;
234 
235 		strbuf_setlen(buf, 0);
236 
237 		ainfo.size = sizeof(ainfo);
238 		ret = pfm_get_event_attr_info(info->idx, j, PFM_OS_PERF_EVENT_EXT, &ainfo);
239 		if (ret != PFM_SUCCESS)
240 			continue;
241 
242 		if (ainfo.ctrl >= PFM_ATTR_CTRL_MAX)
243 			ainfo.ctrl = PFM_ATTR_CTRL_UNKNOWN;
244 
245 		src = srcs[ainfo.ctrl];
246 		if (ainfo.type == PFM_ATTR_UMASK) {
247 			strbuf_addf(buf, "Umask: 0x%02"PRIx64" : %s: ",
248 				ainfo.code, src);
249 			print_attr_flags(buf, &ainfo);
250 			snprintf(name, sizeof(name), "%s::%s:%s",
251 				 pinfo->name, info->name, ainfo.name);
252 
253 			if (!is_libpfm_event_supported(name, cpus, threads))
254 				continue;
255 
256 			print_cb->print_event(print_state,
257 					pinfo->name,
258 					topic,
259 					name, /*alias=*/NULL,
260 					/*scale_unit=*/NULL,
261 					/*deprecated=*/NULL, "PFM event",
262 					ainfo.desc, /*long_desc=*/NULL,
263 					/*encoding_desc=*/buf->buf);
264 		}
265 	}
266 
267 	perf_cpu_map__put(cpus);
268 	perf_thread_map__put(threads);
269 }
270 
271 void print_libpfm_events(const struct print_callbacks *print_cb, void *print_state)
272 {
273 	pfm_event_info_t info;
274 	pfm_pmu_info_t pinfo;
275 	int p, ret;
276 	struct strbuf storage;
277 
278 	libpfm_initialize();
279 
280 	/* initialize to zero to indicate ABI version */
281 	info.size  = sizeof(info);
282 	pinfo.size = sizeof(pinfo);
283 
284 	strbuf_init(&storage, 2048);
285 
286 	pfm_for_all_pmus(p) {
287 		ret = pfm_get_pmu_info(p, &pinfo);
288 		if (ret != PFM_SUCCESS)
289 			continue;
290 
291 		/* only print events that are supported by host HW */
292 		if (!pinfo.is_present)
293 			continue;
294 
295 		/* handled by perf directly */
296 		if (pinfo.pmu == PFM_PMU_PERF_EVENT)
297 			continue;
298 
299 		for (int i = pinfo.first_event; i != -1; i = pfm_get_event_next(i)) {
300 			ret = pfm_get_event_info(i, PFM_OS_PERF_EVENT_EXT,
301 						&info);
302 			if (ret != PFM_SUCCESS)
303 				continue;
304 
305 			print_libpfm_event(print_cb, print_state, &pinfo, &info, &storage);
306 		}
307 	}
308 	strbuf_release(&storage);
309 }
310