1 // SPDX-License-Identifier: GPL-2.0
3 * Support for libpfm4 event encoding.
5 * Copyright 2020 Google LLC.
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"
15 #include "util/strbuf.h"
16 #include "util/thread_map.h"
19 #include <linux/kernel.h>
20 #include <perfmon/pfmlib_perf_event.h>
22 static void libpfm_initialize(void)
26 ret = pfm_initialize();
27 if (ret != PFM_SUCCESS) {
28 ui__warning("libpfm failed to initialize: %s\n",
33 int parse_libpfm_events_option(const struct option *opt, const char *str,
34 int unset __maybe_unused)
36 struct evlist *evlist = *(struct evlist **)opt->value;
37 struct perf_event_attr attr;
39 struct evsel *evsel, *grp_leader = NULL;
47 p_orig = p = strdup(str);
51 * force loading of the PMU list
53 perf_pmus__scan(NULL);
55 for (q = p; strsep(&p, ",{}"); q = p) {
56 sep = p ? str + (p - p_orig - 1) : "";
60 "nested event groups not supported\n");
70 ui__error("cannot close a non-existing event group\n");
78 memset(&attr, 0, sizeof(attr));
79 event_attr_init(&attr);
81 ret = pfm_get_perf_event_encoding(q, PFM_PLM0|PFM_PLM3,
84 if (ret != PFM_SUCCESS) {
85 ui__error("failed to parse event %s : %s\n", str,
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,
97 evsel->is_libpfm_event = true;
99 evlist__add(evlist, evsel);
105 evsel__set_leader(evsel, grp_leader);
106 grp_leader->core.nr_members++;
113 "cannot close a non-existing event group\n");
127 static bool is_libpfm_event_supported(const char *name, struct perf_cpu_map *cpus,
128 struct perf_thread_map *threads)
130 struct perf_pmu *pmu;
132 struct perf_event_attr attr = {};
136 ret = pfm_get_perf_event_encoding(name, PFM_PLM0|PFM_PLM3,
138 if (ret != PFM_SUCCESS)
141 pmu = perf_pmus__find_by_type((unsigned int)attr.type);
142 evsel = parse_events__add_event(0, &attr, name, /*metric_id=*/NULL, pmu);
146 evsel->is_libpfm_event = true;
148 if (evsel__open(evsel, cpus, threads) < 0)
152 evsel__delete(evsel);
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",
164 print_attr_flags(struct strbuf *buf, const pfm_event_attr_info_t *info)
167 strbuf_addf(buf, "[default] ");
169 if (info->is_precise)
170 strbuf_addf(buf, "[precise] ");
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,
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);
183 strbuf_setlen(buf, 0);
184 snprintf(topic, sizeof(topic), "pfm %s", pinfo->name);
186 snprintf(name, sizeof(name), "%s::%s", pinfo->name, info->name);
187 strbuf_addf(buf, "Code: 0x%"PRIx64"\n", info->code);
189 pfm_for_each_event_attr(j, info) {
190 pfm_event_attr_info_t ainfo;
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)
198 if (ainfo.ctrl >= PFM_ATTR_CTRL_MAX)
199 ainfo.ctrl = PFM_ATTR_CTRL_UNKNOWN;
201 src = srcs[ainfo.ctrl];
202 switch (ainfo.type) {
203 case PFM_ATTR_UMASK: /* Ignore for now */
205 case PFM_ATTR_MOD_BOOL:
206 strbuf_addf(buf, " Modif: %s: [%s] : %s (boolean)\n", src,
207 ainfo.name, ainfo.desc);
209 case PFM_ATTR_MOD_INTEGER:
210 strbuf_addf(buf, " Modif: %s: [%s] : %s (integer)\n", src,
211 ainfo.name, ainfo.desc);
214 case PFM_ATTR_RAW_UMASK:
217 strbuf_addf(buf, " Attr: %s: [%s] : %s\n", src,
218 ainfo.name, ainfo.desc);
222 if (is_libpfm_event_supported(name, cpus, threads)) {
223 print_cb->print_event(print_state, pinfo->name, topic,
226 /*deprecated=*/NULL, "PFM event",
227 info->desc, /*long_desc=*/NULL,
228 /*encoding_desc=*/buf->buf);
231 pfm_for_each_event_attr(j, info) {
232 pfm_event_attr_info_t ainfo;
235 strbuf_setlen(buf, 0);
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)
242 if (ainfo.ctrl >= PFM_ATTR_CTRL_MAX)
243 ainfo.ctrl = PFM_ATTR_CTRL_UNKNOWN;
245 src = srcs[ainfo.ctrl];
246 if (ainfo.type == PFM_ATTR_UMASK) {
247 strbuf_addf(buf, "Umask: 0x%02"PRIx64" : %s: ",
249 print_attr_flags(buf, &ainfo);
250 snprintf(name, sizeof(name), "%s::%s:%s",
251 pinfo->name, info->name, ainfo.name);
253 if (!is_libpfm_event_supported(name, cpus, threads))
256 print_cb->print_event(print_state,
259 name, /*alias=*/NULL,
261 /*deprecated=*/NULL, "PFM event",
262 ainfo.desc, /*long_desc=*/NULL,
263 /*encoding_desc=*/buf->buf);
267 perf_cpu_map__put(cpus);
268 perf_thread_map__put(threads);
271 void print_libpfm_events(const struct print_callbacks *print_cb, void *print_state)
273 pfm_event_info_t info;
274 pfm_pmu_info_t pinfo;
276 struct strbuf storage;
280 /* initialize to zero to indicate ABI version */
281 info.size = sizeof(info);
282 pinfo.size = sizeof(pinfo);
284 strbuf_init(&storage, 2048);
286 pfm_for_all_pmus(p) {
287 ret = pfm_get_pmu_info(p, &pinfo);
288 if (ret != PFM_SUCCESS)
291 /* only print events that are supported by host HW */
292 if (!pinfo.is_present)
295 /* handled by perf directly */
296 if (pinfo.pmu == PFM_PMU_PERF_EVENT)
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,
302 if (ret != PFM_SUCCESS)
305 print_libpfm_event(print_cb, print_state, &pinfo, &info, &storage);
308 strbuf_release(&storage);