1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/list.h>
3 #include <linux/list_sort.h>
4 #include <linux/string.h>
5 #include <linux/zalloc.h>
6 #include <subcmd/pager.h>
7 #include <sys/types.h>
8 #include <ctype.h>
9 #include <dirent.h>
10 #include <pthread.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include "cpumap.h"
14 #include "debug.h"
15 #include "evsel.h"
16 #include "pmus.h"
17 #include "pmu.h"
18 #include "hwmon_pmu.h"
19 #include "tool_pmu.h"
20 #include "print-events.h"
21 #include "strbuf.h"
22
23 /*
24 * core_pmus: A PMU belongs to core_pmus if it's name is "cpu" or it's sysfs
25 * directory contains "cpus" file. All PMUs belonging to core_pmus
26 * must have pmu->is_core=1. If there are more than one PMU in
27 * this list, perf interprets it as a heterogeneous platform.
28 * (FWIW, certain ARM platforms having heterogeneous cores uses
29 * homogeneous PMU, and thus they are treated as homogeneous
30 * platform by perf because core_pmus will have only one entry)
31 * other_pmus: All other PMUs which are not part of core_pmus list. It doesn't
32 * matter whether PMU is present per SMT-thread or outside of the
33 * core in the hw. For e.g., an instance of AMD ibs_fetch// and
34 * ibs_op// PMUs is present in each hw SMT thread, however they
35 * are captured under other_pmus. PMUs belonging to other_pmus
36 * must have pmu->is_core=0 but pmu->is_uncore could be 0 or 1.
37 */
38 static LIST_HEAD(core_pmus);
39 static LIST_HEAD(other_pmus);
40 enum perf_tool_pmu_type {
41 PERF_TOOL_PMU_TYPE_PE_CORE,
42 PERF_TOOL_PMU_TYPE_PE_OTHER,
43 PERF_TOOL_PMU_TYPE_TOOL,
44 PERF_TOOL_PMU_TYPE_HWMON,
45
46 #define PERF_TOOL_PMU_TYPE_PE_CORE_MASK (1 << PERF_TOOL_PMU_TYPE_PE_CORE)
47 #define PERF_TOOL_PMU_TYPE_PE_OTHER_MASK (1 << PERF_TOOL_PMU_TYPE_PE_OTHER)
48 #define PERF_TOOL_PMU_TYPE_TOOL_MASK (1 << PERF_TOOL_PMU_TYPE_TOOL)
49 #define PERF_TOOL_PMU_TYPE_HWMON_MASK (1 << PERF_TOOL_PMU_TYPE_HWMON)
50
51 #define PERF_TOOL_PMU_TYPE_ALL_MASK (PERF_TOOL_PMU_TYPE_PE_CORE_MASK | \
52 PERF_TOOL_PMU_TYPE_PE_OTHER_MASK | \
53 PERF_TOOL_PMU_TYPE_TOOL_MASK | \
54 PERF_TOOL_PMU_TYPE_HWMON_MASK)
55 };
56 static unsigned int read_pmu_types;
57
58 static void pmu_read_sysfs(unsigned int to_read_pmus);
59
pmu_name_len_no_suffix(const char * str)60 size_t pmu_name_len_no_suffix(const char *str)
61 {
62 int orig_len, len;
63 bool has_hex_digits = false;
64
65 orig_len = len = strlen(str);
66
67 /* Count trailing digits. */
68 while (len > 0 && isxdigit(str[len - 1])) {
69 if (!isdigit(str[len - 1]))
70 has_hex_digits = true;
71 len--;
72 }
73
74 if (len > 0 && len != orig_len && str[len - 1] == '_') {
75 /*
76 * There is a '_{num}' suffix. For decimal suffixes any length
77 * will do, for hexadecimal ensure more than 2 hex digits so
78 * that S390's cpum_cf PMU doesn't match.
79 */
80 if (!has_hex_digits || (orig_len - len) > 2)
81 return len - 1;
82 }
83 /* Use the full length. */
84 return orig_len;
85 }
86
pmu_name_cmp(const char * lhs_pmu_name,const char * rhs_pmu_name)87 int pmu_name_cmp(const char *lhs_pmu_name, const char *rhs_pmu_name)
88 {
89 unsigned long long lhs_num = 0, rhs_num = 0;
90 size_t lhs_pmu_name_len = pmu_name_len_no_suffix(lhs_pmu_name);
91 size_t rhs_pmu_name_len = pmu_name_len_no_suffix(rhs_pmu_name);
92 int ret = strncmp(lhs_pmu_name, rhs_pmu_name,
93 lhs_pmu_name_len < rhs_pmu_name_len ? lhs_pmu_name_len : rhs_pmu_name_len);
94
95 if (lhs_pmu_name_len != rhs_pmu_name_len || ret != 0 || lhs_pmu_name_len == 0)
96 return ret;
97
98 if (lhs_pmu_name_len + 1 < strlen(lhs_pmu_name))
99 lhs_num = strtoull(&lhs_pmu_name[lhs_pmu_name_len + 1], NULL, 16);
100 if (rhs_pmu_name_len + 1 < strlen(rhs_pmu_name))
101 rhs_num = strtoull(&rhs_pmu_name[rhs_pmu_name_len + 1], NULL, 16);
102
103 return lhs_num < rhs_num ? -1 : (lhs_num > rhs_num ? 1 : 0);
104 }
105
perf_pmus__destroy(void)106 void perf_pmus__destroy(void)
107 {
108 struct perf_pmu *pmu, *tmp;
109
110 list_for_each_entry_safe(pmu, tmp, &core_pmus, list) {
111 list_del(&pmu->list);
112
113 perf_pmu__delete(pmu);
114 }
115 list_for_each_entry_safe(pmu, tmp, &other_pmus, list) {
116 list_del(&pmu->list);
117
118 perf_pmu__delete(pmu);
119 }
120 read_pmu_types = 0;
121 }
122
pmu_find(const char * name)123 static struct perf_pmu *pmu_find(const char *name)
124 {
125 struct perf_pmu *pmu;
126
127 list_for_each_entry(pmu, &core_pmus, list) {
128 if (!strcmp(pmu->name, name) ||
129 (pmu->alias_name && !strcmp(pmu->alias_name, name)))
130 return pmu;
131 }
132 list_for_each_entry(pmu, &other_pmus, list) {
133 if (!strcmp(pmu->name, name) ||
134 (pmu->alias_name && !strcmp(pmu->alias_name, name)))
135 return pmu;
136 }
137
138 return NULL;
139 }
140
perf_pmus__find(const char * name)141 struct perf_pmu *perf_pmus__find(const char *name)
142 {
143 struct perf_pmu *pmu;
144 int dirfd;
145 bool core_pmu;
146 unsigned int to_read_pmus = 0;
147
148 /*
149 * Once PMU is loaded it stays in the list,
150 * so we keep us from multiple reading/parsing
151 * the pmu format definitions.
152 */
153 pmu = pmu_find(name);
154 if (pmu)
155 return pmu;
156
157 if (read_pmu_types == PERF_TOOL_PMU_TYPE_ALL_MASK)
158 return NULL;
159
160 core_pmu = is_pmu_core(name);
161 if (core_pmu && (read_pmu_types & PERF_TOOL_PMU_TYPE_PE_CORE_MASK))
162 return NULL;
163
164 dirfd = perf_pmu__event_source_devices_fd();
165 pmu = perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name,
166 /*eager_load=*/false);
167 close(dirfd);
168
169 if (pmu)
170 return pmu;
171
172 /* Looking up an individual perf event PMU failed, check if a tool PMU should be read. */
173 if (!strncmp(name, "hwmon_", 6))
174 to_read_pmus |= PERF_TOOL_PMU_TYPE_HWMON_MASK;
175 else if (!strcmp(name, "tool"))
176 to_read_pmus |= PERF_TOOL_PMU_TYPE_TOOL_MASK;
177
178 if (to_read_pmus) {
179 pmu_read_sysfs(to_read_pmus);
180 pmu = pmu_find(name);
181 if (pmu)
182 return pmu;
183 }
184 /* Read all necessary PMUs from sysfs and see if the PMU is found. */
185 to_read_pmus = PERF_TOOL_PMU_TYPE_PE_CORE_MASK;
186 if (!core_pmu)
187 to_read_pmus |= PERF_TOOL_PMU_TYPE_PE_OTHER_MASK;
188 pmu_read_sysfs(to_read_pmus);
189 return pmu_find(name);
190 }
191
perf_pmu__find2(int dirfd,const char * name)192 static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name)
193 {
194 struct perf_pmu *pmu;
195 bool core_pmu;
196
197 /*
198 * Once PMU is loaded it stays in the list,
199 * so we keep us from multiple reading/parsing
200 * the pmu format definitions.
201 */
202 pmu = pmu_find(name);
203 if (pmu)
204 return pmu;
205
206 if (read_pmu_types == PERF_TOOL_PMU_TYPE_ALL_MASK)
207 return NULL;
208
209 core_pmu = is_pmu_core(name);
210 if (core_pmu && (read_pmu_types & PERF_TOOL_PMU_TYPE_PE_CORE_MASK))
211 return NULL;
212
213 return perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name,
214 /*eager_load=*/false);
215 }
216
pmus_cmp(void * priv __maybe_unused,const struct list_head * lhs,const struct list_head * rhs)217 static int pmus_cmp(void *priv __maybe_unused,
218 const struct list_head *lhs, const struct list_head *rhs)
219 {
220 struct perf_pmu *lhs_pmu = container_of(lhs, struct perf_pmu, list);
221 struct perf_pmu *rhs_pmu = container_of(rhs, struct perf_pmu, list);
222
223 return pmu_name_cmp(lhs_pmu->name ?: "", rhs_pmu->name ?: "");
224 }
225
226 /* Add all pmus in sysfs to pmu list: */
pmu_read_sysfs(unsigned int to_read_types)227 static void pmu_read_sysfs(unsigned int to_read_types)
228 {
229 struct perf_pmu *tool_pmu;
230
231 if ((read_pmu_types & to_read_types) == to_read_types) {
232 /* All requested PMU types have been read. */
233 return;
234 }
235
236 if (to_read_types & (PERF_TOOL_PMU_TYPE_PE_CORE_MASK | PERF_TOOL_PMU_TYPE_PE_OTHER_MASK)) {
237 int fd = perf_pmu__event_source_devices_fd();
238 DIR *dir;
239 struct dirent *dent;
240 bool core_only = (to_read_types & PERF_TOOL_PMU_TYPE_PE_OTHER_MASK) == 0;
241
242 if (fd < 0)
243 goto skip_pe_pmus;
244
245 dir = fdopendir(fd);
246 if (!dir) {
247 close(fd);
248 goto skip_pe_pmus;
249 }
250
251 while ((dent = readdir(dir))) {
252 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
253 continue;
254 if (core_only && !is_pmu_core(dent->d_name))
255 continue;
256 /* add to static LIST_HEAD(core_pmus) or LIST_HEAD(other_pmus): */
257 perf_pmu__find2(fd, dent->d_name);
258 }
259
260 closedir(dir);
261 }
262 skip_pe_pmus:
263 if ((to_read_types & PERF_TOOL_PMU_TYPE_PE_CORE_MASK) && list_empty(&core_pmus)) {
264 if (!perf_pmu__create_placeholder_core_pmu(&core_pmus))
265 pr_err("Failure to set up any core PMUs\n");
266 }
267 list_sort(NULL, &core_pmus, pmus_cmp);
268
269 if ((to_read_types & PERF_TOOL_PMU_TYPE_TOOL_MASK) != 0 &&
270 (read_pmu_types & PERF_TOOL_PMU_TYPE_TOOL_MASK) == 0) {
271 tool_pmu = tool_pmu__new();
272 if (tool_pmu)
273 list_add_tail(&tool_pmu->list, &other_pmus);
274 }
275 if ((to_read_types & PERF_TOOL_PMU_TYPE_HWMON_MASK) != 0 &&
276 (read_pmu_types & PERF_TOOL_PMU_TYPE_HWMON_MASK) == 0)
277 perf_pmus__read_hwmon_pmus(&other_pmus);
278
279 list_sort(NULL, &other_pmus, pmus_cmp);
280
281 read_pmu_types |= to_read_types;
282 }
283
__perf_pmus__find_by_type(unsigned int type)284 static struct perf_pmu *__perf_pmus__find_by_type(unsigned int type)
285 {
286 struct perf_pmu *pmu;
287
288 list_for_each_entry(pmu, &core_pmus, list) {
289 if (pmu->type == type)
290 return pmu;
291 }
292
293 list_for_each_entry(pmu, &other_pmus, list) {
294 if (pmu->type == type)
295 return pmu;
296 }
297 return NULL;
298 }
299
perf_pmus__find_by_type(unsigned int type)300 struct perf_pmu *perf_pmus__find_by_type(unsigned int type)
301 {
302 unsigned int to_read_pmus;
303 struct perf_pmu *pmu = __perf_pmus__find_by_type(type);
304
305 if (pmu || (read_pmu_types == PERF_TOOL_PMU_TYPE_ALL_MASK))
306 return pmu;
307
308 if (type >= PERF_PMU_TYPE_PE_START && type <= PERF_PMU_TYPE_PE_END) {
309 to_read_pmus = PERF_TOOL_PMU_TYPE_PE_CORE_MASK |
310 PERF_TOOL_PMU_TYPE_PE_OTHER_MASK;
311 } else if (type >= PERF_PMU_TYPE_HWMON_START && type <= PERF_PMU_TYPE_HWMON_END) {
312 to_read_pmus = PERF_TOOL_PMU_TYPE_HWMON_MASK;
313 } else {
314 to_read_pmus = PERF_TOOL_PMU_TYPE_TOOL_MASK;
315 }
316 pmu_read_sysfs(to_read_pmus);
317 pmu = __perf_pmus__find_by_type(type);
318 return pmu;
319 }
320
321 /*
322 * pmu iterator: If pmu is NULL, we start at the begin, otherwise return the
323 * next pmu. Returns NULL on end.
324 */
perf_pmus__scan(struct perf_pmu * pmu)325 struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu)
326 {
327 bool use_core_pmus = !pmu || pmu->is_core;
328
329 if (!pmu) {
330 pmu_read_sysfs(PERF_TOOL_PMU_TYPE_ALL_MASK);
331 pmu = list_prepare_entry(pmu, &core_pmus, list);
332 }
333 if (use_core_pmus) {
334 list_for_each_entry_continue(pmu, &core_pmus, list)
335 return pmu;
336
337 pmu = NULL;
338 pmu = list_prepare_entry(pmu, &other_pmus, list);
339 }
340 list_for_each_entry_continue(pmu, &other_pmus, list)
341 return pmu;
342 return NULL;
343 }
344
perf_pmus__scan_core(struct perf_pmu * pmu)345 struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu)
346 {
347 if (!pmu) {
348 pmu_read_sysfs(PERF_TOOL_PMU_TYPE_PE_CORE_MASK);
349 return list_first_entry_or_null(&core_pmus, typeof(*pmu), list);
350 }
351 list_for_each_entry_continue(pmu, &core_pmus, list)
352 return pmu;
353
354 return NULL;
355 }
356
perf_pmus__scan_skip_duplicates(struct perf_pmu * pmu)357 static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)
358 {
359 bool use_core_pmus = !pmu || pmu->is_core;
360 int last_pmu_name_len = 0;
361 const char *last_pmu_name = (pmu && pmu->name) ? pmu->name : "";
362
363 if (!pmu) {
364 pmu_read_sysfs(PERF_TOOL_PMU_TYPE_ALL_MASK);
365 pmu = list_prepare_entry(pmu, &core_pmus, list);
366 } else
367 last_pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "");
368
369 if (use_core_pmus) {
370 list_for_each_entry_continue(pmu, &core_pmus, list) {
371 int pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "");
372
373 if (last_pmu_name_len == pmu_name_len &&
374 !strncmp(last_pmu_name, pmu->name ?: "", pmu_name_len))
375 continue;
376
377 return pmu;
378 }
379 pmu = NULL;
380 pmu = list_prepare_entry(pmu, &other_pmus, list);
381 }
382 list_for_each_entry_continue(pmu, &other_pmus, list) {
383 int pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "");
384
385 if (last_pmu_name_len == pmu_name_len &&
386 !strncmp(last_pmu_name, pmu->name ?: "", pmu_name_len))
387 continue;
388
389 return pmu;
390 }
391 return NULL;
392 }
393
perf_pmus__pmu_for_pmu_filter(const char * str)394 const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str)
395 {
396 struct perf_pmu *pmu = NULL;
397
398 while ((pmu = perf_pmus__scan(pmu)) != NULL) {
399 if (!strcmp(pmu->name, str))
400 return pmu;
401 /* Ignore "uncore_" prefix. */
402 if (!strncmp(pmu->name, "uncore_", 7)) {
403 if (!strcmp(pmu->name + 7, str))
404 return pmu;
405 }
406 /* Ignore "cpu_" prefix on Intel hybrid PMUs. */
407 if (!strncmp(pmu->name, "cpu_", 4)) {
408 if (!strcmp(pmu->name + 4, str))
409 return pmu;
410 }
411 }
412 return NULL;
413 }
414
415 /** Struct for ordering events as output in perf list. */
416 struct sevent {
417 /** PMU for event. */
418 const struct perf_pmu *pmu;
419 const char *name;
420 const char* alias;
421 const char *scale_unit;
422 const char *desc;
423 const char *long_desc;
424 const char *encoding_desc;
425 const char *topic;
426 const char *pmu_name;
427 const char *event_type_desc;
428 bool deprecated;
429 };
430
cmp_sevent(const void * a,const void * b)431 static int cmp_sevent(const void *a, const void *b)
432 {
433 const struct sevent *as = a;
434 const struct sevent *bs = b;
435 bool a_iscpu, b_iscpu;
436 int ret;
437
438 /* Put extra events last. */
439 if (!!as->desc != !!bs->desc)
440 return !!as->desc - !!bs->desc;
441
442 /* Order by topics. */
443 ret = strcmp(as->topic ?: "", bs->topic ?: "");
444 if (ret)
445 return ret;
446
447 /* Order CPU core events to be first */
448 a_iscpu = as->pmu ? as->pmu->is_core : true;
449 b_iscpu = bs->pmu ? bs->pmu->is_core : true;
450 if (a_iscpu != b_iscpu)
451 return a_iscpu ? -1 : 1;
452
453 /* Order by PMU name. */
454 if (as->pmu != bs->pmu) {
455 ret = strcmp(as->pmu_name ?: "", bs->pmu_name ?: "");
456 if (ret)
457 return ret;
458 }
459
460 /* Order by event name. */
461 return strcmp(as->name, bs->name);
462 }
463
pmu_alias_is_duplicate(struct sevent * a,struct sevent * b)464 static bool pmu_alias_is_duplicate(struct sevent *a, struct sevent *b)
465 {
466 /* Different names -> never duplicates */
467 if (strcmp(a->name ?: "//", b->name ?: "//"))
468 return false;
469
470 /* Don't remove duplicates for different PMUs */
471 return strcmp(a->pmu_name, b->pmu_name) == 0;
472 }
473
474 struct events_callback_state {
475 struct sevent *aliases;
476 size_t aliases_len;
477 size_t index;
478 };
479
perf_pmus__print_pmu_events__callback(void * vstate,struct pmu_event_info * info)480 static int perf_pmus__print_pmu_events__callback(void *vstate,
481 struct pmu_event_info *info)
482 {
483 struct events_callback_state *state = vstate;
484 struct sevent *s;
485
486 if (state->index >= state->aliases_len) {
487 pr_err("Unexpected event %s/%s/\n", info->pmu->name, info->name);
488 return 1;
489 }
490 assert(info->pmu != NULL || info->name != NULL);
491 s = &state->aliases[state->index];
492 s->pmu = info->pmu;
493 #define COPY_STR(str) s->str = info->str ? strdup(info->str) : NULL
494 COPY_STR(name);
495 COPY_STR(alias);
496 COPY_STR(scale_unit);
497 COPY_STR(desc);
498 COPY_STR(long_desc);
499 COPY_STR(encoding_desc);
500 COPY_STR(topic);
501 COPY_STR(pmu_name);
502 COPY_STR(event_type_desc);
503 #undef COPY_STR
504 s->deprecated = info->deprecated;
505 state->index++;
506 return 0;
507 }
508
perf_pmus__print_pmu_events(const struct print_callbacks * print_cb,void * print_state)509 void perf_pmus__print_pmu_events(const struct print_callbacks *print_cb, void *print_state)
510 {
511 struct perf_pmu *pmu;
512 int printed = 0;
513 int len;
514 struct sevent *aliases;
515 struct events_callback_state state;
516 bool skip_duplicate_pmus = print_cb->skip_duplicate_pmus(print_state);
517 struct perf_pmu *(*scan_fn)(struct perf_pmu *);
518
519 if (skip_duplicate_pmus)
520 scan_fn = perf_pmus__scan_skip_duplicates;
521 else
522 scan_fn = perf_pmus__scan;
523
524 pmu = NULL;
525 len = 0;
526 while ((pmu = scan_fn(pmu)) != NULL)
527 len += perf_pmu__num_events(pmu);
528
529 aliases = zalloc(sizeof(struct sevent) * len);
530 if (!aliases) {
531 pr_err("FATAL: not enough memory to print PMU events\n");
532 return;
533 }
534 pmu = NULL;
535 state = (struct events_callback_state) {
536 .aliases = aliases,
537 .aliases_len = len,
538 .index = 0,
539 };
540 while ((pmu = scan_fn(pmu)) != NULL) {
541 perf_pmu__for_each_event(pmu, skip_duplicate_pmus, &state,
542 perf_pmus__print_pmu_events__callback);
543 }
544 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
545 for (int j = 0; j < len; j++) {
546 /* Skip duplicates */
547 if (j < len - 1 && pmu_alias_is_duplicate(&aliases[j], &aliases[j + 1]))
548 goto free;
549
550 print_cb->print_event(print_state,
551 aliases[j].topic,
552 aliases[j].pmu_name,
553 aliases[j].name,
554 aliases[j].alias,
555 aliases[j].scale_unit,
556 aliases[j].deprecated,
557 aliases[j].event_type_desc,
558 aliases[j].desc,
559 aliases[j].long_desc,
560 aliases[j].encoding_desc);
561 free:
562 zfree(&aliases[j].name);
563 zfree(&aliases[j].alias);
564 zfree(&aliases[j].scale_unit);
565 zfree(&aliases[j].desc);
566 zfree(&aliases[j].long_desc);
567 zfree(&aliases[j].encoding_desc);
568 zfree(&aliases[j].topic);
569 zfree(&aliases[j].pmu_name);
570 zfree(&aliases[j].event_type_desc);
571 }
572 if (printed && pager_in_use())
573 printf("\n");
574
575 zfree(&aliases);
576 }
577
578 struct build_format_string_args {
579 struct strbuf short_string;
580 struct strbuf long_string;
581 int num_formats;
582 };
583
build_format_string(void * state,const char * name,int config,const unsigned long * bits)584 static int build_format_string(void *state, const char *name, int config,
585 const unsigned long *bits)
586 {
587 struct build_format_string_args *args = state;
588 unsigned int num_bits;
589 int ret1, ret2 = 0;
590
591 (void)config;
592 args->num_formats++;
593 if (args->num_formats > 1) {
594 strbuf_addch(&args->long_string, ',');
595 if (args->num_formats < 4)
596 strbuf_addch(&args->short_string, ',');
597 }
598 num_bits = bits ? bitmap_weight(bits, PERF_PMU_FORMAT_BITS) : 0;
599 if (num_bits <= 1) {
600 ret1 = strbuf_addf(&args->long_string, "%s", name);
601 if (args->num_formats < 4)
602 ret2 = strbuf_addf(&args->short_string, "%s", name);
603 } else if (num_bits > 8) {
604 ret1 = strbuf_addf(&args->long_string, "%s=0..0x%llx", name,
605 ULLONG_MAX >> (64 - num_bits));
606 if (args->num_formats < 4) {
607 ret2 = strbuf_addf(&args->short_string, "%s=0..0x%llx", name,
608 ULLONG_MAX >> (64 - num_bits));
609 }
610 } else {
611 ret1 = strbuf_addf(&args->long_string, "%s=0..%llu", name,
612 ULLONG_MAX >> (64 - num_bits));
613 if (args->num_formats < 4) {
614 ret2 = strbuf_addf(&args->short_string, "%s=0..%llu", name,
615 ULLONG_MAX >> (64 - num_bits));
616 }
617 }
618 return ret1 < 0 ? ret1 : (ret2 < 0 ? ret2 : 0);
619 }
620
perf_pmus__print_raw_pmu_events(const struct print_callbacks * print_cb,void * print_state)621 void perf_pmus__print_raw_pmu_events(const struct print_callbacks *print_cb, void *print_state)
622 {
623 bool skip_duplicate_pmus = print_cb->skip_duplicate_pmus(print_state);
624 struct perf_pmu *(*scan_fn)(struct perf_pmu *);
625 struct perf_pmu *pmu = NULL;
626
627 if (skip_duplicate_pmus)
628 scan_fn = perf_pmus__scan_skip_duplicates;
629 else
630 scan_fn = perf_pmus__scan;
631
632 while ((pmu = scan_fn(pmu)) != NULL) {
633 struct build_format_string_args format_args = {
634 .short_string = STRBUF_INIT,
635 .long_string = STRBUF_INIT,
636 .num_formats = 0,
637 };
638 int len = pmu_name_len_no_suffix(pmu->name);
639 const char *desc = "(see 'man perf-list' or 'man perf-record' on how to encode it)";
640
641 if (!pmu->is_core)
642 desc = NULL;
643
644 strbuf_addf(&format_args.short_string, "%.*s/", len, pmu->name);
645 strbuf_addf(&format_args.long_string, "%.*s/", len, pmu->name);
646 perf_pmu__for_each_format(pmu, &format_args, build_format_string);
647
648 if (format_args.num_formats > 3)
649 strbuf_addf(&format_args.short_string, ",.../modifier");
650 else
651 strbuf_addf(&format_args.short_string, "/modifier");
652
653 strbuf_addf(&format_args.long_string, "/modifier");
654 print_cb->print_event(print_state,
655 /*topic=*/NULL,
656 /*pmu_name=*/NULL,
657 format_args.short_string.buf,
658 /*event_alias=*/NULL,
659 /*scale_unit=*/NULL,
660 /*deprecated=*/false,
661 "Raw event descriptor",
662 desc,
663 /*long_desc=*/NULL,
664 format_args.long_string.buf);
665
666 strbuf_release(&format_args.short_string);
667 strbuf_release(&format_args.long_string);
668 }
669 }
670
perf_pmus__have_event(const char * pname,const char * name)671 bool perf_pmus__have_event(const char *pname, const char *name)
672 {
673 struct perf_pmu *pmu = perf_pmus__find(pname);
674
675 return pmu && perf_pmu__have_event(pmu, name);
676 }
677
perf_pmus__num_core_pmus(void)678 int perf_pmus__num_core_pmus(void)
679 {
680 static int count;
681
682 if (!count) {
683 struct perf_pmu *pmu = NULL;
684
685 while ((pmu = perf_pmus__scan_core(pmu)) != NULL)
686 count++;
687 }
688 return count;
689 }
690
__perf_pmus__supports_extended_type(void)691 static bool __perf_pmus__supports_extended_type(void)
692 {
693 struct perf_pmu *pmu = NULL;
694
695 if (perf_pmus__num_core_pmus() <= 1)
696 return false;
697
698 while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
699 if (!is_event_supported(PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES | ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT)))
700 return false;
701 }
702
703 return true;
704 }
705
706 static bool perf_pmus__do_support_extended_type;
707
perf_pmus__init_supports_extended_type(void)708 static void perf_pmus__init_supports_extended_type(void)
709 {
710 perf_pmus__do_support_extended_type = __perf_pmus__supports_extended_type();
711 }
712
perf_pmus__supports_extended_type(void)713 bool perf_pmus__supports_extended_type(void)
714 {
715 static pthread_once_t extended_type_once = PTHREAD_ONCE_INIT;
716
717 pthread_once(&extended_type_once, perf_pmus__init_supports_extended_type);
718
719 return perf_pmus__do_support_extended_type;
720 }
721
perf_pmus__default_pmu_name(void)722 char *perf_pmus__default_pmu_name(void)
723 {
724 int fd;
725 DIR *dir;
726 struct dirent *dent;
727 char *result = NULL;
728
729 if (!list_empty(&core_pmus))
730 return strdup(list_first_entry(&core_pmus, struct perf_pmu, list)->name);
731
732 fd = perf_pmu__event_source_devices_fd();
733 if (fd < 0)
734 return strdup("cpu");
735
736 dir = fdopendir(fd);
737 if (!dir) {
738 close(fd);
739 return strdup("cpu");
740 }
741
742 while ((dent = readdir(dir))) {
743 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
744 continue;
745 if (is_pmu_core(dent->d_name)) {
746 result = strdup(dent->d_name);
747 break;
748 }
749 }
750
751 closedir(dir);
752 return result ?: strdup("cpu");
753 }
754
evsel__find_pmu(const struct evsel * evsel)755 struct perf_pmu *evsel__find_pmu(const struct evsel *evsel)
756 {
757 struct perf_pmu *pmu = evsel->pmu;
758 bool legacy_core_type;
759
760 if (pmu)
761 return pmu;
762
763 pmu = perf_pmus__find_by_type(evsel->core.attr.type);
764 legacy_core_type =
765 evsel->core.attr.type == PERF_TYPE_HARDWARE ||
766 evsel->core.attr.type == PERF_TYPE_HW_CACHE;
767 if (!pmu && legacy_core_type) {
768 if (perf_pmus__supports_extended_type()) {
769 u32 type = evsel->core.attr.config >> PERF_PMU_TYPE_SHIFT;
770
771 pmu = perf_pmus__find_by_type(type);
772 } else {
773 pmu = perf_pmus__find_core_pmu();
774 }
775 }
776 ((struct evsel *)evsel)->pmu = pmu;
777 return pmu;
778 }
779
perf_pmus__find_core_pmu(void)780 struct perf_pmu *perf_pmus__find_core_pmu(void)
781 {
782 return perf_pmus__scan_core(NULL);
783 }
784
perf_pmus__add_test_pmu(int test_sysfs_dirfd,const char * name)785 struct perf_pmu *perf_pmus__add_test_pmu(int test_sysfs_dirfd, const char *name)
786 {
787 /*
788 * Some PMU functions read from the sysfs mount point, so care is
789 * needed, hence passing the eager_load flag to load things like the
790 * format files.
791 */
792 return perf_pmu__lookup(&other_pmus, test_sysfs_dirfd, name, /*eager_load=*/true);
793 }
794
perf_pmus__add_test_hwmon_pmu(int hwmon_dir,const char * sysfs_name,const char * name)795 struct perf_pmu *perf_pmus__add_test_hwmon_pmu(int hwmon_dir,
796 const char *sysfs_name,
797 const char *name)
798 {
799 return hwmon_pmu__new(&other_pmus, hwmon_dir, sysfs_name, name);
800 }
801
perf_pmus__fake_pmu(void)802 struct perf_pmu *perf_pmus__fake_pmu(void)
803 {
804 static struct perf_pmu fake = {
805 .name = "fake",
806 .type = PERF_PMU_TYPE_FAKE,
807 .format = LIST_HEAD_INIT(fake.format),
808 };
809
810 return &fake;
811 }
812