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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
|
// SPDX-License-Identifier: GPL-2.0
#include <linux/list.h>
#include <linux/list_sort.h>
#include <linux/string.h>
#include <linux/zalloc.h>
#include <subcmd/pager.h>
#include <sys/types.h>
#include <ctype.h>
#include <dirent.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include "cpumap.h"
#include "debug.h"
#include "evsel.h"
#include "pmus.h"
#include "pmu.h"
#include "print-events.h"
/*
* core_pmus: A PMU belongs to core_pmus if it's name is "cpu" or it's sysfs
* directory contains "cpus" file. All PMUs belonging to core_pmus
* must have pmu->is_core=1. If there are more than one PMU in
* this list, perf interprets it as a heterogeneous platform.
* (FWIW, certain ARM platforms having heterogeneous cores uses
* homogeneous PMU, and thus they are treated as homogeneous
* platform by perf because core_pmus will have only one entry)
* other_pmus: All other PMUs which are not part of core_pmus list. It doesn't
* matter whether PMU is present per SMT-thread or outside of the
* core in the hw. For e.g., an instance of AMD ibs_fetch// and
* ibs_op// PMUs is present in each hw SMT thread, however they
* are captured under other_pmus. PMUs belonging to other_pmus
* must have pmu->is_core=0 but pmu->is_uncore could be 0 or 1.
*/
static LIST_HEAD(core_pmus);
static LIST_HEAD(other_pmus);
static bool read_sysfs_core_pmus;
static bool read_sysfs_all_pmus;
static void pmu_read_sysfs(bool core_only);
int pmu_name_len_no_suffix(const char *str, unsigned long *num)
{
int orig_len, len;
orig_len = len = strlen(str);
/* Non-uncore PMUs have their full length, for example, i915. */
if (!strstarts(str, "uncore_"))
return len;
/*
* Count trailing digits and '_', if '_{num}' suffix isn't present use
* the full length.
*/
while (len > 0 && isdigit(str[len - 1]))
len--;
if (len > 0 && len != orig_len && str[len - 1] == '_') {
if (num)
*num = strtoul(&str[len], NULL, 10);
return len - 1;
}
return orig_len;
}
void perf_pmus__destroy(void)
{
struct perf_pmu *pmu, *tmp;
list_for_each_entry_safe(pmu, tmp, &core_pmus, list) {
list_del(&pmu->list);
perf_pmu__delete(pmu);
}
list_for_each_entry_safe(pmu, tmp, &other_pmus, list) {
list_del(&pmu->list);
perf_pmu__delete(pmu);
}
read_sysfs_core_pmus = false;
read_sysfs_all_pmus = false;
}
static struct perf_pmu *pmu_find(const char *name)
{
struct perf_pmu *pmu;
list_for_each_entry(pmu, &core_pmus, list) {
if (!strcmp(pmu->name, name) ||
(pmu->alias_name && !strcmp(pmu->alias_name, name)))
return pmu;
}
list_for_each_entry(pmu, &other_pmus, list) {
if (!strcmp(pmu->name, name) ||
(pmu->alias_name && !strcmp(pmu->alias_name, name)))
return pmu;
}
return NULL;
}
struct perf_pmu *perf_pmus__find(const char *name)
{
struct perf_pmu *pmu;
int dirfd;
bool core_pmu;
/*
* Once PMU is loaded it stays in the list,
* so we keep us from multiple reading/parsing
* the pmu format definitions.
*/
pmu = pmu_find(name);
if (pmu)
return pmu;
if (read_sysfs_all_pmus)
return NULL;
core_pmu = is_pmu_core(name);
if (core_pmu && read_sysfs_core_pmus)
return NULL;
dirfd = perf_pmu__event_source_devices_fd();
pmu = perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name);
close(dirfd);
if (!pmu) {
/*
* Looking up an inidividual PMU failed. This may mean name is
* an alias, so read the PMUs from sysfs and try to find again.
*/
pmu_read_sysfs(core_pmu);
pmu = pmu_find(name);
}
return pmu;
}
static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name)
{
struct perf_pmu *pmu;
bool core_pmu;
/*
* Once PMU is loaded it stays in the list,
* so we keep us from multiple reading/parsing
* the pmu format definitions.
*/
pmu = pmu_find(name);
if (pmu)
return pmu;
if (read_sysfs_all_pmus)
return NULL;
core_pmu = is_pmu_core(name);
if (core_pmu && read_sysfs_core_pmus)
return NULL;
return perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name);
}
static int pmus_cmp(void *priv __maybe_unused,
const struct list_head *lhs, const struct list_head *rhs)
{
unsigned long lhs_num = 0, rhs_num = 0;
struct perf_pmu *lhs_pmu = container_of(lhs, struct perf_pmu, list);
struct perf_pmu *rhs_pmu = container_of(rhs, struct perf_pmu, list);
const char *lhs_pmu_name = lhs_pmu->name ?: "";
const char *rhs_pmu_name = rhs_pmu->name ?: "";
int lhs_pmu_name_len = pmu_name_len_no_suffix(lhs_pmu_name, &lhs_num);
int rhs_pmu_name_len = pmu_name_len_no_suffix(rhs_pmu_name, &rhs_num);
int ret = strncmp(lhs_pmu_name, rhs_pmu_name,
lhs_pmu_name_len < rhs_pmu_name_len ? lhs_pmu_name_len : rhs_pmu_name_len);
if (lhs_pmu_name_len != rhs_pmu_name_len || ret != 0 || lhs_pmu_name_len == 0)
return ret;
return lhs_num < rhs_num ? -1 : (lhs_num > rhs_num ? 1 : 0);
}
/* Add all pmus in sysfs to pmu list: */
static void pmu_read_sysfs(bool core_only)
{
int fd;
DIR *dir;
struct dirent *dent;
if (read_sysfs_all_pmus || (core_only && read_sysfs_core_pmus))
return;
fd = perf_pmu__event_source_devices_fd();
if (fd < 0)
return;
dir = fdopendir(fd);
if (!dir) {
close(fd);
return;
}
while ((dent = readdir(dir))) {
if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
continue;
if (core_only && !is_pmu_core(dent->d_name))
continue;
/* add to static LIST_HEAD(core_pmus) or LIST_HEAD(other_pmus): */
perf_pmu__find2(fd, dent->d_name);
}
closedir(dir);
if (list_empty(&core_pmus)) {
if (!perf_pmu__create_placeholder_core_pmu(&core_pmus))
pr_err("Failure to set up any core PMUs\n");
}
list_sort(NULL, &core_pmus, pmus_cmp);
list_sort(NULL, &other_pmus, pmus_cmp);
if (!list_empty(&core_pmus)) {
read_sysfs_core_pmus = true;
if (!core_only)
read_sysfs_all_pmus = true;
}
}
static struct perf_pmu *__perf_pmus__find_by_type(unsigned int type)
{
struct perf_pmu *pmu;
list_for_each_entry(pmu, &core_pmus, list) {
if (pmu->type == type)
return pmu;
}
list_for_each_entry(pmu, &other_pmus, list) {
if (pmu->type == type)
return pmu;
}
return NULL;
}
struct perf_pmu *perf_pmus__find_by_type(unsigned int type)
{
struct perf_pmu *pmu = __perf_pmus__find_by_type(type);
if (pmu || read_sysfs_all_pmus)
return pmu;
pmu_read_sysfs(/*core_only=*/false);
pmu = __perf_pmus__find_by_type(type);
return pmu;
}
/*
* pmu iterator: If pmu is NULL, we start at the begin, otherwise return the
* next pmu. Returns NULL on end.
*/
struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu)
{
bool use_core_pmus = !pmu || pmu->is_core;
if (!pmu) {
pmu_read_sysfs(/*core_only=*/false);
pmu = list_prepare_entry(pmu, &core_pmus, list);
}
if (use_core_pmus) {
list_for_each_entry_continue(pmu, &core_pmus, list)
return pmu;
pmu = NULL;
pmu = list_prepare_entry(pmu, &other_pmus, list);
}
list_for_each_entry_continue(pmu, &other_pmus, list)
return pmu;
return NULL;
}
struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu)
{
if (!pmu) {
pmu_read_sysfs(/*core_only=*/true);
return list_first_entry_or_null(&core_pmus, typeof(*pmu), list);
}
list_for_each_entry_continue(pmu, &core_pmus, list)
return pmu;
return NULL;
}
static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)
{
bool use_core_pmus = !pmu || pmu->is_core;
int last_pmu_name_len = 0;
const char *last_pmu_name = (pmu && pmu->name) ? pmu->name : "";
if (!pmu) {
pmu_read_sysfs(/*core_only=*/false);
pmu = list_prepare_entry(pmu, &core_pmus, list);
} else
last_pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "", NULL);
if (use_core_pmus) {
list_for_each_entry_continue(pmu, &core_pmus, list) {
int pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "", /*num=*/NULL);
if (last_pmu_name_len == pmu_name_len &&
!strncmp(last_pmu_name, pmu->name ?: "", pmu_name_len))
continue;
return pmu;
}
pmu = NULL;
pmu = list_prepare_entry(pmu, &other_pmus, list);
}
list_for_each_entry_continue(pmu, &other_pmus, list) {
int pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "", /*num=*/NULL);
if (last_pmu_name_len == pmu_name_len &&
!strncmp(last_pmu_name, pmu->name ?: "", pmu_name_len))
continue;
return pmu;
}
return NULL;
}
const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str)
{
struct perf_pmu *pmu = NULL;
while ((pmu = perf_pmus__scan(pmu)) != NULL) {
if (!strcmp(pmu->name, str))
return pmu;
/* Ignore "uncore_" prefix. */
if (!strncmp(pmu->name, "uncore_", 7)) {
if (!strcmp(pmu->name + 7, str))
return pmu;
}
/* Ignore "cpu_" prefix on Intel hybrid PMUs. */
if (!strncmp(pmu->name, "cpu_", 4)) {
if (!strcmp(pmu->name + 4, str))
return pmu;
}
}
return NULL;
}
int __weak perf_pmus__num_mem_pmus(void)
{
/* All core PMUs are for mem events. */
return perf_pmus__num_core_pmus();
}
/** Struct for ordering events as output in perf list. */
struct sevent {
/** PMU for event. */
const struct perf_pmu *pmu;
const char *name;
const char* alias;
const char *scale_unit;
const char *desc;
const char *long_desc;
const char *encoding_desc;
const char *topic;
const char *pmu_name;
bool deprecated;
};
static int cmp_sevent(const void *a, const void *b)
{
const struct sevent *as = a;
const struct sevent *bs = b;
bool a_iscpu, b_iscpu;
int ret;
/* Put extra events last. */
if (!!as->desc != !!bs->desc)
return !!as->desc - !!bs->desc;
/* Order by topics. */
ret = strcmp(as->topic ?: "", bs->topic ?: "");
if (ret)
return ret;
/* Order CPU core events to be first */
a_iscpu = as->pmu ? as->pmu->is_core : true;
b_iscpu = bs->pmu ? bs->pmu->is_core : true;
if (a_iscpu != b_iscpu)
return a_iscpu ? -1 : 1;
/* Order by PMU name. */
if (as->pmu != bs->pmu) {
ret = strcmp(as->pmu_name ?: "", bs->pmu_name ?: "");
if (ret)
return ret;
}
/* Order by event name. */
return strcmp(as->name, bs->name);
}
static bool pmu_alias_is_duplicate(struct sevent *a, struct sevent *b)
{
/* Different names -> never duplicates */
if (strcmp(a->name ?: "//", b->name ?: "//"))
return false;
/* Don't remove duplicates for different PMUs */
return strcmp(a->pmu_name, b->pmu_name) == 0;
}
struct events_callback_state {
struct sevent *aliases;
size_t aliases_len;
size_t index;
};
static int perf_pmus__print_pmu_events__callback(void *vstate,
struct pmu_event_info *info)
{
struct events_callback_state *state = vstate;
struct sevent *s;
if (state->index >= state->aliases_len) {
pr_err("Unexpected event %s/%s/\n", info->pmu->name, info->name);
return 1;
}
s = &state->aliases[state->index];
s->pmu = info->pmu;
#define COPY_STR(str) s->str = info->str ? strdup(info->str) : NULL
COPY_STR(name);
COPY_STR(alias);
COPY_STR(scale_unit);
COPY_STR(desc);
COPY_STR(long_desc);
COPY_STR(encoding_desc);
COPY_STR(topic);
COPY_STR(pmu_name);
#undef COPY_STR
s->deprecated = info->deprecated;
state->index++;
return 0;
}
void perf_pmus__print_pmu_events(const struct print_callbacks *print_cb, void *print_state)
{
struct perf_pmu *pmu;
int printed = 0;
int len;
struct sevent *aliases;
struct events_callback_state state;
bool skip_duplicate_pmus = print_cb->skip_duplicate_pmus(print_state);
struct perf_pmu *(*scan_fn)(struct perf_pmu *);
if (skip_duplicate_pmus)
scan_fn = perf_pmus__scan_skip_duplicates;
else
scan_fn = perf_pmus__scan;
pmu = NULL;
len = 0;
while ((pmu = scan_fn(pmu)) != NULL)
len += perf_pmu__num_events(pmu);
aliases = zalloc(sizeof(struct sevent) * len);
if (!aliases) {
pr_err("FATAL: not enough memory to print PMU events\n");
return;
}
pmu = NULL;
state = (struct events_callback_state) {
.aliases = aliases,
.aliases_len = len,
.index = 0,
};
while ((pmu = scan_fn(pmu)) != NULL) {
perf_pmu__for_each_event(pmu, skip_duplicate_pmus, &state,
perf_pmus__print_pmu_events__callback);
}
qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
for (int j = 0; j < len; j++) {
/* Skip duplicates */
if (j > 0 && pmu_alias_is_duplicate(&aliases[j], &aliases[j - 1]))
continue;
print_cb->print_event(print_state,
aliases[j].pmu_name,
aliases[j].topic,
aliases[j].name,
aliases[j].alias,
aliases[j].scale_unit,
aliases[j].deprecated,
"Kernel PMU event",
aliases[j].desc,
aliases[j].long_desc,
aliases[j].encoding_desc);
zfree(&aliases[j].name);
zfree(&aliases[j].alias);
zfree(&aliases[j].scale_unit);
zfree(&aliases[j].desc);
zfree(&aliases[j].long_desc);
zfree(&aliases[j].encoding_desc);
zfree(&aliases[j].topic);
zfree(&aliases[j].pmu_name);
}
if (printed && pager_in_use())
printf("\n");
zfree(&aliases);
}
bool perf_pmus__have_event(const char *pname, const char *name)
{
struct perf_pmu *pmu = perf_pmus__find(pname);
return pmu && perf_pmu__have_event(pmu, name);
}
int perf_pmus__num_core_pmus(void)
{
static int count;
if (!count) {
struct perf_pmu *pmu = NULL;
while ((pmu = perf_pmus__scan_core(pmu)) != NULL)
count++;
}
return count;
}
static bool __perf_pmus__supports_extended_type(void)
{
struct perf_pmu *pmu = NULL;
if (perf_pmus__num_core_pmus() <= 1)
return false;
while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
if (!is_event_supported(PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES | ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT)))
return false;
}
return true;
}
static bool perf_pmus__do_support_extended_type;
static void perf_pmus__init_supports_extended_type(void)
{
perf_pmus__do_support_extended_type = __perf_pmus__supports_extended_type();
}
bool perf_pmus__supports_extended_type(void)
{
static pthread_once_t extended_type_once = PTHREAD_ONCE_INIT;
pthread_once(&extended_type_once, perf_pmus__init_supports_extended_type);
return perf_pmus__do_support_extended_type;
}
char *perf_pmus__default_pmu_name(void)
{
int fd;
DIR *dir;
struct dirent *dent;
char *result = NULL;
if (!list_empty(&core_pmus))
return strdup(list_first_entry(&core_pmus, struct perf_pmu, list)->name);
fd = perf_pmu__event_source_devices_fd();
if (fd < 0)
return strdup("cpu");
dir = fdopendir(fd);
if (!dir) {
close(fd);
return strdup("cpu");
}
while ((dent = readdir(dir))) {
if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
continue;
if (is_pmu_core(dent->d_name)) {
result = strdup(dent->d_name);
break;
}
}
closedir(dir);
return result ?: strdup("cpu");
}
struct perf_pmu *evsel__find_pmu(const struct evsel *evsel)
{
struct perf_pmu *pmu = evsel->pmu;
if (!pmu) {
pmu = perf_pmus__find_by_type(evsel->core.attr.type);
((struct evsel *)evsel)->pmu = pmu;
}
return pmu;
}
struct perf_pmu *perf_pmus__find_core_pmu(void)
{
return perf_pmus__scan_core(NULL);
}
|