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"
17 #include <linux/kernel.h>
18 #include <perfmon/pfmlib_perf_event.h>
20 static void libpfm_initialize(void)
24 ret = pfm_initialize();
25 if (ret != PFM_SUCCESS) {
26 ui__warning("libpfm failed to initialize: %s\n",
31 int parse_libpfm_events_option(const struct option *opt, const char *str,
32 int unset __maybe_unused)
34 struct evlist *evlist = *(struct evlist **)opt->value;
35 struct perf_event_attr attr;
37 struct evsel *evsel, *grp_leader = NULL;
45 p_orig = p = strdup(str);
49 * force loading of the PMU list
53 for (q = p; strsep(&p, ",{}"); q = p) {
54 sep = p ? str + (p - p_orig - 1) : "";
58 "nested event groups not supported\n");
68 ui__error("cannot close a non-existing event group\n");
76 memset(&attr, 0, sizeof(attr));
77 event_attr_init(&attr);
79 ret = pfm_get_perf_event_encoding(q, PFM_PLM0|PFM_PLM3,
82 if (ret != PFM_SUCCESS) {
83 ui__error("failed to parse event %s : %s\n", str,
88 pmu = perf_pmu__find_by_type((unsigned int)attr.type);
89 evsel = parse_events__add_event(evlist->core.nr_entries,
94 evsel->is_libpfm_event = true;
96 evlist__add(evlist, evsel);
102 evsel__set_leader(evsel, grp_leader);
103 grp_leader->core.nr_members++;
110 "cannot close a non-existing event group\n");
113 evlist->core.nr_groups++;
125 static const char *srcs[PFM_ATTR_CTRL_MAX] = {
126 [PFM_ATTR_CTRL_UNKNOWN] = "???",
127 [PFM_ATTR_CTRL_PMU] = "PMU",
128 [PFM_ATTR_CTRL_PERF_EVENT] = "perf_event",
132 print_attr_flags(pfm_event_attr_info_t *info)
137 printf("[default] ");
141 if (info->is_precise) {
142 printf("[precise] ");
151 print_libpfm_events_detailed(pfm_event_info_t *info, bool long_desc)
153 pfm_event_attr_info_t ainfo;
157 ainfo.size = sizeof(ainfo);
159 printf(" %s\n", info->name);
160 printf(" [%s]\n", info->desc);
163 printf(" Equiv: %s\n", info->equiv);
165 printf(" Code : 0x%"PRIx64"\n", info->code);
167 pfm_for_each_event_attr(j, info) {
168 ret = pfm_get_event_attr_info(info->idx, j,
169 PFM_OS_PERF_EVENT_EXT, &ainfo);
170 if (ret != PFM_SUCCESS)
173 if (ainfo.type == PFM_ATTR_UMASK) {
174 printf(" %s:%s\n", info->name, ainfo.name);
175 printf(" [%s]\n", ainfo.desc);
181 if (ainfo.ctrl >= PFM_ATTR_CTRL_MAX)
182 ainfo.ctrl = PFM_ATTR_CTRL_UNKNOWN;
184 src = srcs[ainfo.ctrl];
185 switch (ainfo.type) {
187 printf(" Umask : 0x%02"PRIx64" : %s: ",
189 print_attr_flags(&ainfo);
192 case PFM_ATTR_MOD_BOOL:
193 printf(" Modif : %s: [%s] : %s (boolean)\n", src,
194 ainfo.name, ainfo.desc);
196 case PFM_ATTR_MOD_INTEGER:
197 printf(" Modif : %s: [%s] : %s (integer)\n", src,
198 ainfo.name, ainfo.desc);
201 case PFM_ATTR_RAW_UMASK:
204 printf(" Attr : %s: [%s] : %s\n", src,
205 ainfo.name, ainfo.desc);
211 * list all pmu::event:umask, pmu::event
212 * printed events may not be all valid combinations of umask for an event
215 print_libpfm_events_raw(pfm_pmu_info_t *pinfo, pfm_event_info_t *info)
217 pfm_event_attr_info_t ainfo;
219 bool has_umask = false;
221 ainfo.size = sizeof(ainfo);
223 pfm_for_each_event_attr(j, info) {
224 ret = pfm_get_event_attr_info(info->idx, j,
225 PFM_OS_PERF_EVENT_EXT, &ainfo);
226 if (ret != PFM_SUCCESS)
229 if (ainfo.type != PFM_ATTR_UMASK)
232 printf("%s::%s:%s\n", pinfo->name, info->name, ainfo.name);
236 printf("%s::%s\n", pinfo->name, info->name);
239 void print_libpfm_events(bool name_only, bool long_desc)
241 pfm_event_info_t info;
242 pfm_pmu_info_t pinfo;
247 /* initialize to zero to indicate ABI version */
248 info.size = sizeof(info);
249 pinfo.size = sizeof(pinfo);
252 puts("\nList of pre-defined events (to be used in --pfm-events):\n");
254 pfm_for_all_pmus(p) {
255 bool printed_pmu = false;
257 ret = pfm_get_pmu_info(p, &pinfo);
258 if (ret != PFM_SUCCESS)
261 /* only print events that are supported by host HW */
262 if (!pinfo.is_present)
265 /* handled by perf directly */
266 if (pinfo.pmu == PFM_PMU_PERF_EVENT)
269 for (i = pinfo.first_event; i != -1;
270 i = pfm_get_event_next(i)) {
272 ret = pfm_get_event_info(i, PFM_OS_PERF_EVENT_EXT,
274 if (ret != PFM_SUCCESS)
277 if (!name_only && !printed_pmu) {
278 printf("%s:\n", pinfo.name);
283 print_libpfm_events_detailed(&info, long_desc);
285 print_libpfm_events_raw(&pinfo, &info);
287 if (!name_only && printed_pmu)