1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <[email protected]>
4 *
5 * Parts came from builtin-{top,stat,record}.c, see those files for further
6 * copyright notes.
7 */
8 /*
9 * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select
10 * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu.
11 */
12 #define __SANE_USERSPACE_TYPES__
13
14 #include <byteswap.h>
15 #include <errno.h>
16 #include <inttypes.h>
17 #include <linux/bitops.h>
18 #include <api/fs/fs.h>
19 #include <api/fs/tracing_path.h>
20 #include <linux/hw_breakpoint.h>
21 #include <linux/perf_event.h>
22 #include <linux/compiler.h>
23 #include <linux/err.h>
24 #include <linux/zalloc.h>
25 #include <sys/ioctl.h>
26 #include <sys/resource.h>
27 #include <sys/syscall.h>
28 #include <sys/types.h>
29 #include <dirent.h>
30 #include <stdlib.h>
31 #include <perf/evsel.h>
32 #include "asm/bug.h"
33 #include "bpf_counter.h"
34 #include "callchain.h"
35 #include "cgroup.h"
36 #include "counts.h"
37 #include "event.h"
38 #include "evsel.h"
39 #include "time-utils.h"
40 #include "util/env.h"
41 #include "util/evsel_config.h"
42 #include "util/evsel_fprintf.h"
43 #include "evlist.h"
44 #include <perf/cpumap.h>
45 #include "thread_map.h"
46 #include "target.h"
47 #include "perf_regs.h"
48 #include "record.h"
49 #include "debug.h"
50 #include "trace-event.h"
51 #include "stat.h"
52 #include "string2.h"
53 #include "memswap.h"
54 #include "util.h"
55 #include "util/hashmap.h"
56 #include "off_cpu.h"
57 #include "pmu.h"
58 #include "pmus.h"
59 #include "hwmon_pmu.h"
60 #include "tool_pmu.h"
61 #include "rlimit.h"
62 #include "../perf-sys.h"
63 #include "util/parse-branch-options.h"
64 #include "util/bpf-filter.h"
65 #include "util/hist.h"
66 #include <internal/xyarray.h>
67 #include <internal/lib.h>
68 #include <internal/threadmap.h>
69 #include "util/intel-tpebs.h"
70
71 #include <linux/ctype.h>
72
73 #ifdef HAVE_LIBTRACEEVENT
74 #include <event-parse.h>
75 #endif
76
77 struct perf_missing_features perf_missing_features;
78
79 static clockid_t clockid;
80
evsel__no_extra_init(struct evsel * evsel __maybe_unused)81 static int evsel__no_extra_init(struct evsel *evsel __maybe_unused)
82 {
83 return 0;
84 }
85
test_attr__enabled(void)86 static bool test_attr__enabled(void)
87 {
88 static bool test_attr__enabled;
89 static bool test_attr__enabled_tested;
90
91 if (!test_attr__enabled_tested) {
92 char *dir = getenv("PERF_TEST_ATTR");
93
94 test_attr__enabled = (dir != NULL);
95 test_attr__enabled_tested = true;
96 }
97 return test_attr__enabled;
98 }
99
100 #define __WRITE_ASS(str, fmt, data) \
101 do { \
102 if (fprintf(file, #str "=%"fmt "\n", data) < 0) { \
103 perror("test attr - failed to write event file"); \
104 fclose(file); \
105 return -1; \
106 } \
107 } while (0)
108
109 #define WRITE_ASS(field, fmt) __WRITE_ASS(field, fmt, attr->field)
110
store_event(struct perf_event_attr * attr,pid_t pid,struct perf_cpu cpu,int fd,int group_fd,unsigned long flags)111 static int store_event(struct perf_event_attr *attr, pid_t pid, struct perf_cpu cpu,
112 int fd, int group_fd, unsigned long flags)
113 {
114 FILE *file;
115 char path[PATH_MAX];
116 char *dir = getenv("PERF_TEST_ATTR");
117
118 snprintf(path, PATH_MAX, "%s/event-%d-%llu-%d", dir,
119 attr->type, attr->config, fd);
120
121 file = fopen(path, "w+");
122 if (!file) {
123 perror("test attr - failed to open event file");
124 return -1;
125 }
126
127 if (fprintf(file, "[event-%d-%llu-%d]\n",
128 attr->type, attr->config, fd) < 0) {
129 perror("test attr - failed to write event file");
130 fclose(file);
131 return -1;
132 }
133
134 /* syscall arguments */
135 __WRITE_ASS(fd, "d", fd);
136 __WRITE_ASS(group_fd, "d", group_fd);
137 __WRITE_ASS(cpu, "d", cpu.cpu);
138 __WRITE_ASS(pid, "d", pid);
139 __WRITE_ASS(flags, "lu", flags);
140
141 /* struct perf_event_attr */
142 WRITE_ASS(type, PRIu32);
143 WRITE_ASS(size, PRIu32);
144 WRITE_ASS(config, "llu");
145 WRITE_ASS(sample_period, "llu");
146 WRITE_ASS(sample_type, "llu");
147 WRITE_ASS(read_format, "llu");
148 WRITE_ASS(disabled, "d");
149 WRITE_ASS(inherit, "d");
150 WRITE_ASS(pinned, "d");
151 WRITE_ASS(exclusive, "d");
152 WRITE_ASS(exclude_user, "d");
153 WRITE_ASS(exclude_kernel, "d");
154 WRITE_ASS(exclude_hv, "d");
155 WRITE_ASS(exclude_idle, "d");
156 WRITE_ASS(mmap, "d");
157 WRITE_ASS(comm, "d");
158 WRITE_ASS(freq, "d");
159 WRITE_ASS(inherit_stat, "d");
160 WRITE_ASS(enable_on_exec, "d");
161 WRITE_ASS(task, "d");
162 WRITE_ASS(watermark, "d");
163 WRITE_ASS(precise_ip, "d");
164 WRITE_ASS(mmap_data, "d");
165 WRITE_ASS(sample_id_all, "d");
166 WRITE_ASS(exclude_host, "d");
167 WRITE_ASS(exclude_guest, "d");
168 WRITE_ASS(exclude_callchain_kernel, "d");
169 WRITE_ASS(exclude_callchain_user, "d");
170 WRITE_ASS(mmap2, "d");
171 WRITE_ASS(comm_exec, "d");
172 WRITE_ASS(context_switch, "d");
173 WRITE_ASS(write_backward, "d");
174 WRITE_ASS(namespaces, "d");
175 WRITE_ASS(use_clockid, "d");
176 WRITE_ASS(wakeup_events, PRIu32);
177 WRITE_ASS(bp_type, PRIu32);
178 WRITE_ASS(config1, "llu");
179 WRITE_ASS(config2, "llu");
180 WRITE_ASS(branch_sample_type, "llu");
181 WRITE_ASS(sample_regs_user, "llu");
182 WRITE_ASS(sample_stack_user, PRIu32);
183
184 fclose(file);
185 return 0;
186 }
187
188 #undef __WRITE_ASS
189 #undef WRITE_ASS
190
test_attr__open(struct perf_event_attr * attr,pid_t pid,struct perf_cpu cpu,int fd,int group_fd,unsigned long flags)191 static void test_attr__open(struct perf_event_attr *attr, pid_t pid, struct perf_cpu cpu,
192 int fd, int group_fd, unsigned long flags)
193 {
194 int errno_saved = errno;
195
196 if ((fd != -1) && store_event(attr, pid, cpu, fd, group_fd, flags)) {
197 pr_err("test attr FAILED");
198 exit(128);
199 }
200
201 errno = errno_saved;
202 }
203
evsel__no_extra_fini(struct evsel * evsel __maybe_unused)204 static void evsel__no_extra_fini(struct evsel *evsel __maybe_unused)
205 {
206 }
207
208 static struct {
209 size_t size;
210 int (*init)(struct evsel *evsel);
211 void (*fini)(struct evsel *evsel);
212 } perf_evsel__object = {
213 .size = sizeof(struct evsel),
214 .init = evsel__no_extra_init,
215 .fini = evsel__no_extra_fini,
216 };
217
evsel__object_config(size_t object_size,int (* init)(struct evsel * evsel),void (* fini)(struct evsel * evsel))218 int evsel__object_config(size_t object_size, int (*init)(struct evsel *evsel),
219 void (*fini)(struct evsel *evsel))
220 {
221
222 if (object_size == 0)
223 goto set_methods;
224
225 if (perf_evsel__object.size > object_size)
226 return -EINVAL;
227
228 perf_evsel__object.size = object_size;
229
230 set_methods:
231 if (init != NULL)
232 perf_evsel__object.init = init;
233
234 if (fini != NULL)
235 perf_evsel__object.fini = fini;
236
237 return 0;
238 }
239
240 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
241
__evsel__sample_size(u64 sample_type)242 int __evsel__sample_size(u64 sample_type)
243 {
244 u64 mask = sample_type & PERF_SAMPLE_MASK;
245 int size = 0;
246 int i;
247
248 for (i = 0; i < 64; i++) {
249 if (mask & (1ULL << i))
250 size++;
251 }
252
253 size *= sizeof(u64);
254
255 return size;
256 }
257
258 /**
259 * __perf_evsel__calc_id_pos - calculate id_pos.
260 * @sample_type: sample type
261 *
262 * This function returns the position of the event id (PERF_SAMPLE_ID or
263 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
264 * perf_record_sample.
265 */
__perf_evsel__calc_id_pos(u64 sample_type)266 static int __perf_evsel__calc_id_pos(u64 sample_type)
267 {
268 int idx = 0;
269
270 if (sample_type & PERF_SAMPLE_IDENTIFIER)
271 return 0;
272
273 if (!(sample_type & PERF_SAMPLE_ID))
274 return -1;
275
276 if (sample_type & PERF_SAMPLE_IP)
277 idx += 1;
278
279 if (sample_type & PERF_SAMPLE_TID)
280 idx += 1;
281
282 if (sample_type & PERF_SAMPLE_TIME)
283 idx += 1;
284
285 if (sample_type & PERF_SAMPLE_ADDR)
286 idx += 1;
287
288 return idx;
289 }
290
291 /**
292 * __perf_evsel__calc_is_pos - calculate is_pos.
293 * @sample_type: sample type
294 *
295 * This function returns the position (counting backwards) of the event id
296 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
297 * sample_id_all is used there is an id sample appended to non-sample events.
298 */
__perf_evsel__calc_is_pos(u64 sample_type)299 static int __perf_evsel__calc_is_pos(u64 sample_type)
300 {
301 int idx = 1;
302
303 if (sample_type & PERF_SAMPLE_IDENTIFIER)
304 return 1;
305
306 if (!(sample_type & PERF_SAMPLE_ID))
307 return -1;
308
309 if (sample_type & PERF_SAMPLE_CPU)
310 idx += 1;
311
312 if (sample_type & PERF_SAMPLE_STREAM_ID)
313 idx += 1;
314
315 return idx;
316 }
317
evsel__calc_id_pos(struct evsel * evsel)318 void evsel__calc_id_pos(struct evsel *evsel)
319 {
320 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->core.attr.sample_type);
321 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->core.attr.sample_type);
322 }
323
__evsel__set_sample_bit(struct evsel * evsel,enum perf_event_sample_format bit)324 void __evsel__set_sample_bit(struct evsel *evsel,
325 enum perf_event_sample_format bit)
326 {
327 if (!(evsel->core.attr.sample_type & bit)) {
328 evsel->core.attr.sample_type |= bit;
329 evsel->sample_size += sizeof(u64);
330 evsel__calc_id_pos(evsel);
331 }
332 }
333
__evsel__reset_sample_bit(struct evsel * evsel,enum perf_event_sample_format bit)334 void __evsel__reset_sample_bit(struct evsel *evsel,
335 enum perf_event_sample_format bit)
336 {
337 if (evsel->core.attr.sample_type & bit) {
338 evsel->core.attr.sample_type &= ~bit;
339 evsel->sample_size -= sizeof(u64);
340 evsel__calc_id_pos(evsel);
341 }
342 }
343
evsel__set_sample_id(struct evsel * evsel,bool can_sample_identifier)344 void evsel__set_sample_id(struct evsel *evsel,
345 bool can_sample_identifier)
346 {
347 if (can_sample_identifier) {
348 evsel__reset_sample_bit(evsel, ID);
349 evsel__set_sample_bit(evsel, IDENTIFIER);
350 } else {
351 evsel__set_sample_bit(evsel, ID);
352 }
353 evsel->core.attr.read_format |= PERF_FORMAT_ID;
354 }
355
356 /**
357 * evsel__is_function_event - Return whether given evsel is a function
358 * trace event
359 *
360 * @evsel - evsel selector to be tested
361 *
362 * Return %true if event is function trace event
363 */
evsel__is_function_event(struct evsel * evsel)364 bool evsel__is_function_event(struct evsel *evsel)
365 {
366 #define FUNCTION_EVENT "ftrace:function"
367
368 return evsel->name &&
369 !strncmp(FUNCTION_EVENT, evsel->name, sizeof(FUNCTION_EVENT));
370
371 #undef FUNCTION_EVENT
372 }
373
evsel__init(struct evsel * evsel,struct perf_event_attr * attr,int idx)374 void evsel__init(struct evsel *evsel,
375 struct perf_event_attr *attr, int idx)
376 {
377 perf_evsel__init(&evsel->core, attr, idx);
378 evsel->tracking = !idx;
379 evsel->unit = strdup("");
380 evsel->scale = 1.0;
381 evsel->max_events = ULONG_MAX;
382 evsel->evlist = NULL;
383 evsel->bpf_obj = NULL;
384 evsel->bpf_fd = -1;
385 INIT_LIST_HEAD(&evsel->config_terms);
386 INIT_LIST_HEAD(&evsel->bpf_counter_list);
387 INIT_LIST_HEAD(&evsel->bpf_filters);
388 perf_evsel__object.init(evsel);
389 evsel->sample_size = __evsel__sample_size(attr->sample_type);
390 evsel__calc_id_pos(evsel);
391 evsel->cmdline_group_boundary = false;
392 evsel->metric_events = NULL;
393 evsel->per_pkg_mask = NULL;
394 evsel->collect_stat = false;
395 evsel->group_pmu_name = NULL;
396 evsel->skippable = false;
397 evsel->alternate_hw_config = PERF_COUNT_HW_MAX;
398 evsel->script_output_type = -1; // FIXME: OUTPUT_TYPE_UNSET, see builtin-script.c
399 }
400
evsel__new_idx(struct perf_event_attr * attr,int idx)401 struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx)
402 {
403 struct evsel *evsel = zalloc(perf_evsel__object.size);
404
405 if (!evsel)
406 return NULL;
407 evsel__init(evsel, attr, idx);
408
409 if (evsel__is_bpf_output(evsel) && !attr->sample_type) {
410 evsel->core.attr.sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
411 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
412 evsel->core.attr.sample_period = 1;
413 }
414
415 if (evsel__is_clock(evsel)) {
416 free((char *)evsel->unit);
417 evsel->unit = strdup("msec");
418 evsel->scale = 1e-6;
419 }
420
421 return evsel;
422 }
423
copy_config_terms(struct list_head * dst,struct list_head * src)424 int copy_config_terms(struct list_head *dst, struct list_head *src)
425 {
426 struct evsel_config_term *pos, *tmp;
427
428 list_for_each_entry(pos, src, list) {
429 tmp = malloc(sizeof(*tmp));
430 if (tmp == NULL)
431 return -ENOMEM;
432
433 *tmp = *pos;
434 if (tmp->free_str) {
435 tmp->val.str = strdup(pos->val.str);
436 if (tmp->val.str == NULL) {
437 free(tmp);
438 return -ENOMEM;
439 }
440 }
441 list_add_tail(&tmp->list, dst);
442 }
443 return 0;
444 }
445
evsel__copy_config_terms(struct evsel * dst,struct evsel * src)446 static int evsel__copy_config_terms(struct evsel *dst, struct evsel *src)
447 {
448 return copy_config_terms(&dst->config_terms, &src->config_terms);
449 }
450
451 /**
452 * evsel__clone - create a new evsel copied from @orig
453 * @orig: original evsel
454 *
455 * The assumption is that @orig is not configured nor opened yet.
456 * So we only care about the attributes that can be set while it's parsed.
457 */
evsel__clone(struct evsel * dest,struct evsel * orig)458 struct evsel *evsel__clone(struct evsel *dest, struct evsel *orig)
459 {
460 struct evsel *evsel;
461
462 BUG_ON(orig->core.fd);
463 BUG_ON(orig->counts);
464 BUG_ON(orig->priv);
465 BUG_ON(orig->per_pkg_mask);
466
467 /* cannot handle BPF objects for now */
468 if (orig->bpf_obj)
469 return NULL;
470
471 if (dest)
472 evsel = dest;
473 else
474 evsel = evsel__new(&orig->core.attr);
475
476 if (evsel == NULL)
477 return NULL;
478
479 evsel->core.cpus = perf_cpu_map__get(orig->core.cpus);
480 evsel->core.own_cpus = perf_cpu_map__get(orig->core.own_cpus);
481 evsel->core.threads = perf_thread_map__get(orig->core.threads);
482 evsel->core.nr_members = orig->core.nr_members;
483 evsel->core.system_wide = orig->core.system_wide;
484 evsel->core.requires_cpu = orig->core.requires_cpu;
485 evsel->core.is_pmu_core = orig->core.is_pmu_core;
486
487 if (orig->name) {
488 evsel->name = strdup(orig->name);
489 if (evsel->name == NULL)
490 goto out_err;
491 }
492 if (orig->group_name) {
493 evsel->group_name = strdup(orig->group_name);
494 if (evsel->group_name == NULL)
495 goto out_err;
496 }
497 if (orig->group_pmu_name) {
498 evsel->group_pmu_name = strdup(orig->group_pmu_name);
499 if (evsel->group_pmu_name == NULL)
500 goto out_err;
501 }
502 if (orig->filter) {
503 evsel->filter = strdup(orig->filter);
504 if (evsel->filter == NULL)
505 goto out_err;
506 }
507 if (orig->metric_id) {
508 evsel->metric_id = strdup(orig->metric_id);
509 if (evsel->metric_id == NULL)
510 goto out_err;
511 }
512 evsel->cgrp = cgroup__get(orig->cgrp);
513 #ifdef HAVE_LIBTRACEEVENT
514 if (orig->tp_sys) {
515 evsel->tp_sys = strdup(orig->tp_sys);
516 if (evsel->tp_sys == NULL)
517 goto out_err;
518 }
519 if (orig->tp_name) {
520 evsel->tp_name = strdup(orig->tp_name);
521 if (evsel->tp_name == NULL)
522 goto out_err;
523 }
524 evsel->tp_format = orig->tp_format;
525 #endif
526 evsel->handler = orig->handler;
527 evsel->core.leader = orig->core.leader;
528
529 evsel->max_events = orig->max_events;
530 zfree(&evsel->unit);
531 if (orig->unit) {
532 evsel->unit = strdup(orig->unit);
533 if (evsel->unit == NULL)
534 goto out_err;
535 }
536 evsel->scale = orig->scale;
537 evsel->snapshot = orig->snapshot;
538 evsel->per_pkg = orig->per_pkg;
539 evsel->percore = orig->percore;
540 evsel->precise_max = orig->precise_max;
541 evsel->is_libpfm_event = orig->is_libpfm_event;
542
543 evsel->exclude_GH = orig->exclude_GH;
544 evsel->sample_read = orig->sample_read;
545 evsel->auto_merge_stats = orig->auto_merge_stats;
546 evsel->collect_stat = orig->collect_stat;
547 evsel->weak_group = orig->weak_group;
548 evsel->use_config_name = orig->use_config_name;
549 evsel->pmu = orig->pmu;
550
551 if (evsel__copy_config_terms(evsel, orig) < 0)
552 goto out_err;
553
554 evsel->alternate_hw_config = orig->alternate_hw_config;
555
556 return evsel;
557
558 out_err:
559 evsel__delete(evsel);
560 return NULL;
561 }
562
trace_event__id(const char * sys,const char * name)563 static int trace_event__id(const char *sys, const char *name)
564 {
565 char *tp_dir = get_events_file(sys);
566 char path[PATH_MAX];
567 int id, err;
568
569 if (!tp_dir)
570 return -1;
571
572 scnprintf(path, PATH_MAX, "%s/%s/id", tp_dir, name);
573 put_events_file(tp_dir);
574 err = filename__read_int(path, &id);
575 if (err)
576 return err;
577
578 return id;
579 }
580
581 /*
582 * Returns pointer with encoded error via <linux/err.h> interface.
583 */
evsel__newtp_idx(const char * sys,const char * name,int idx,bool format)584 struct evsel *evsel__newtp_idx(const char *sys, const char *name, int idx, bool format)
585 {
586 struct perf_event_attr attr = {
587 .type = PERF_TYPE_TRACEPOINT,
588 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
589 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
590 };
591 struct evsel *evsel = zalloc(perf_evsel__object.size);
592 int err = -ENOMEM, id = -1;
593
594 if (evsel == NULL)
595 goto out_err;
596
597
598 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
599 goto out_free;
600
601 #ifdef HAVE_LIBTRACEEVENT
602 evsel->tp_sys = strdup(sys);
603 if (!evsel->tp_sys)
604 goto out_free;
605
606 evsel->tp_name = strdup(name);
607 if (!evsel->tp_name)
608 goto out_free;
609 #endif
610
611 event_attr_init(&attr);
612
613 if (format) {
614 id = trace_event__id(sys, name);
615 if (id < 0) {
616 err = id;
617 goto out_free;
618 }
619 }
620 attr.config = (__u64)id;
621 attr.sample_period = 1;
622 evsel__init(evsel, &attr, idx);
623 return evsel;
624
625 out_free:
626 zfree(&evsel->name);
627 #ifdef HAVE_LIBTRACEEVENT
628 zfree(&evsel->tp_sys);
629 zfree(&evsel->tp_name);
630 #endif
631 free(evsel);
632 out_err:
633 return ERR_PTR(err);
634 }
635
636 #ifdef HAVE_LIBTRACEEVENT
evsel__tp_format(struct evsel * evsel)637 struct tep_event *evsel__tp_format(struct evsel *evsel)
638 {
639 struct tep_event *tp_format = evsel->tp_format;
640
641 if (tp_format)
642 return tp_format;
643
644 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
645 return NULL;
646
647 if (!evsel->tp_sys)
648 tp_format = trace_event__tp_format_id(evsel->core.attr.config);
649 else
650 tp_format = trace_event__tp_format(evsel->tp_sys, evsel->tp_name);
651
652 if (IS_ERR(tp_format)) {
653 int err = -PTR_ERR(evsel->tp_format);
654
655 pr_err("Error getting tracepoint format '%s' '%s'(%d)\n",
656 evsel__name(evsel), strerror(err), err);
657 return NULL;
658 }
659 evsel->tp_format = tp_format;
660 return evsel->tp_format;
661 }
662 #endif
663
664 const char *const evsel__hw_names[PERF_COUNT_HW_MAX] = {
665 "cycles",
666 "instructions",
667 "cache-references",
668 "cache-misses",
669 "branches",
670 "branch-misses",
671 "bus-cycles",
672 "stalled-cycles-frontend",
673 "stalled-cycles-backend",
674 "ref-cycles",
675 };
676
677 char *evsel__bpf_counter_events;
678
evsel__match_bpf_counter_events(const char * name)679 bool evsel__match_bpf_counter_events(const char *name)
680 {
681 int name_len;
682 bool match;
683 char *ptr;
684
685 if (!evsel__bpf_counter_events)
686 return false;
687
688 ptr = strstr(evsel__bpf_counter_events, name);
689 name_len = strlen(name);
690
691 /* check name matches a full token in evsel__bpf_counter_events */
692 match = (ptr != NULL) &&
693 ((ptr == evsel__bpf_counter_events) || (*(ptr - 1) == ',')) &&
694 ((*(ptr + name_len) == ',') || (*(ptr + name_len) == '\0'));
695
696 return match;
697 }
698
__evsel__hw_name(u64 config)699 static const char *__evsel__hw_name(u64 config)
700 {
701 if (config < PERF_COUNT_HW_MAX && evsel__hw_names[config])
702 return evsel__hw_names[config];
703
704 return "unknown-hardware";
705 }
706
evsel__add_modifiers(struct evsel * evsel,char * bf,size_t size)707 static int evsel__add_modifiers(struct evsel *evsel, char *bf, size_t size)
708 {
709 int colon = 0, r = 0;
710 struct perf_event_attr *attr = &evsel->core.attr;
711
712 #define MOD_PRINT(context, mod) do { \
713 if (!attr->exclude_##context) { \
714 if (!colon) colon = ++r; \
715 r += scnprintf(bf + r, size - r, "%c", mod); \
716 } } while(0)
717
718 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
719 MOD_PRINT(kernel, 'k');
720 MOD_PRINT(user, 'u');
721 MOD_PRINT(hv, 'h');
722 }
723
724 if (attr->precise_ip) {
725 if (!colon)
726 colon = ++r;
727 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
728 }
729
730 if (attr->exclude_host || attr->exclude_guest) {
731 MOD_PRINT(host, 'H');
732 MOD_PRINT(guest, 'G');
733 }
734 #undef MOD_PRINT
735 if (colon)
736 bf[colon - 1] = ':';
737 return r;
738 }
739
arch_evsel__hw_name(struct evsel * evsel,char * bf,size_t size)740 int __weak arch_evsel__hw_name(struct evsel *evsel, char *bf, size_t size)
741 {
742 return scnprintf(bf, size, "%s", __evsel__hw_name(evsel->core.attr.config));
743 }
744
evsel__hw_name(struct evsel * evsel,char * bf,size_t size)745 static int evsel__hw_name(struct evsel *evsel, char *bf, size_t size)
746 {
747 int r = arch_evsel__hw_name(evsel, bf, size);
748 return r + evsel__add_modifiers(evsel, bf + r, size - r);
749 }
750
751 const char *const evsel__sw_names[PERF_COUNT_SW_MAX] = {
752 "cpu-clock",
753 "task-clock",
754 "page-faults",
755 "context-switches",
756 "cpu-migrations",
757 "minor-faults",
758 "major-faults",
759 "alignment-faults",
760 "emulation-faults",
761 "dummy",
762 };
763
__evsel__sw_name(u64 config)764 static const char *__evsel__sw_name(u64 config)
765 {
766 if (config < PERF_COUNT_SW_MAX && evsel__sw_names[config])
767 return evsel__sw_names[config];
768 return "unknown-software";
769 }
770
evsel__sw_name(struct evsel * evsel,char * bf,size_t size)771 static int evsel__sw_name(struct evsel *evsel, char *bf, size_t size)
772 {
773 int r = scnprintf(bf, size, "%s", __evsel__sw_name(evsel->core.attr.config));
774 return r + evsel__add_modifiers(evsel, bf + r, size - r);
775 }
776
__evsel__bp_name(char * bf,size_t size,u64 addr,u64 type)777 static int __evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
778 {
779 int r;
780
781 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
782
783 if (type & HW_BREAKPOINT_R)
784 r += scnprintf(bf + r, size - r, "r");
785
786 if (type & HW_BREAKPOINT_W)
787 r += scnprintf(bf + r, size - r, "w");
788
789 if (type & HW_BREAKPOINT_X)
790 r += scnprintf(bf + r, size - r, "x");
791
792 return r;
793 }
794
evsel__bp_name(struct evsel * evsel,char * bf,size_t size)795 static int evsel__bp_name(struct evsel *evsel, char *bf, size_t size)
796 {
797 struct perf_event_attr *attr = &evsel->core.attr;
798 int r = __evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
799 return r + evsel__add_modifiers(evsel, bf + r, size - r);
800 }
801
802 const char *const evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX][EVSEL__MAX_ALIASES] = {
803 { "L1-dcache", "l1-d", "l1d", "L1-data", },
804 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
805 { "LLC", "L2", },
806 { "dTLB", "d-tlb", "Data-TLB", },
807 { "iTLB", "i-tlb", "Instruction-TLB", },
808 { "branch", "branches", "bpu", "btb", "bpc", },
809 { "node", },
810 };
811
812 const char *const evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX][EVSEL__MAX_ALIASES] = {
813 { "load", "loads", "read", },
814 { "store", "stores", "write", },
815 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
816 };
817
818 const char *const evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX][EVSEL__MAX_ALIASES] = {
819 { "refs", "Reference", "ops", "access", },
820 { "misses", "miss", },
821 };
822
823 #define C(x) PERF_COUNT_HW_CACHE_##x
824 #define CACHE_READ (1 << C(OP_READ))
825 #define CACHE_WRITE (1 << C(OP_WRITE))
826 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
827 #define COP(x) (1 << x)
828
829 /*
830 * cache operation stat
831 * L1I : Read and prefetch only
832 * ITLB and BPU : Read-only
833 */
834 static const unsigned long evsel__hw_cache_stat[C(MAX)] = {
835 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
836 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
837 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
838 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
839 [C(ITLB)] = (CACHE_READ),
840 [C(BPU)] = (CACHE_READ),
841 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
842 };
843
evsel__is_cache_op_valid(u8 type,u8 op)844 bool evsel__is_cache_op_valid(u8 type, u8 op)
845 {
846 if (evsel__hw_cache_stat[type] & COP(op))
847 return true; /* valid */
848 else
849 return false; /* invalid */
850 }
851
__evsel__hw_cache_type_op_res_name(u8 type,u8 op,u8 result,char * bf,size_t size)852 int __evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result, char *bf, size_t size)
853 {
854 if (result) {
855 return scnprintf(bf, size, "%s-%s-%s", evsel__hw_cache[type][0],
856 evsel__hw_cache_op[op][0],
857 evsel__hw_cache_result[result][0]);
858 }
859
860 return scnprintf(bf, size, "%s-%s", evsel__hw_cache[type][0],
861 evsel__hw_cache_op[op][1]);
862 }
863
__evsel__hw_cache_name(u64 config,char * bf,size_t size)864 static int __evsel__hw_cache_name(u64 config, char *bf, size_t size)
865 {
866 u8 op, result, type = (config >> 0) & 0xff;
867 const char *err = "unknown-ext-hardware-cache-type";
868
869 if (type >= PERF_COUNT_HW_CACHE_MAX)
870 goto out_err;
871
872 op = (config >> 8) & 0xff;
873 err = "unknown-ext-hardware-cache-op";
874 if (op >= PERF_COUNT_HW_CACHE_OP_MAX)
875 goto out_err;
876
877 result = (config >> 16) & 0xff;
878 err = "unknown-ext-hardware-cache-result";
879 if (result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
880 goto out_err;
881
882 err = "invalid-cache";
883 if (!evsel__is_cache_op_valid(type, op))
884 goto out_err;
885
886 return __evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
887 out_err:
888 return scnprintf(bf, size, "%s", err);
889 }
890
evsel__hw_cache_name(struct evsel * evsel,char * bf,size_t size)891 static int evsel__hw_cache_name(struct evsel *evsel, char *bf, size_t size)
892 {
893 int ret = __evsel__hw_cache_name(evsel->core.attr.config, bf, size);
894 return ret + evsel__add_modifiers(evsel, bf + ret, size - ret);
895 }
896
evsel__raw_name(struct evsel * evsel,char * bf,size_t size)897 static int evsel__raw_name(struct evsel *evsel, char *bf, size_t size)
898 {
899 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->core.attr.config);
900 return ret + evsel__add_modifiers(evsel, bf + ret, size - ret);
901 }
902
evsel__name(struct evsel * evsel)903 const char *evsel__name(struct evsel *evsel)
904 {
905 char bf[128];
906
907 if (!evsel)
908 goto out_unknown;
909
910 if (evsel->name)
911 return evsel->name;
912
913 switch (evsel->core.attr.type) {
914 case PERF_TYPE_RAW:
915 evsel__raw_name(evsel, bf, sizeof(bf));
916 break;
917
918 case PERF_TYPE_HARDWARE:
919 evsel__hw_name(evsel, bf, sizeof(bf));
920 break;
921
922 case PERF_TYPE_HW_CACHE:
923 evsel__hw_cache_name(evsel, bf, sizeof(bf));
924 break;
925
926 case PERF_TYPE_SOFTWARE:
927 evsel__sw_name(evsel, bf, sizeof(bf));
928 break;
929
930 case PERF_TYPE_TRACEPOINT:
931 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
932 break;
933
934 case PERF_TYPE_BREAKPOINT:
935 evsel__bp_name(evsel, bf, sizeof(bf));
936 break;
937
938 case PERF_PMU_TYPE_TOOL:
939 scnprintf(bf, sizeof(bf), "%s", evsel__tool_pmu_event_name(evsel));
940 break;
941
942 default:
943 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
944 evsel->core.attr.type);
945 break;
946 }
947
948 evsel->name = strdup(bf);
949
950 if (evsel->name)
951 return evsel->name;
952 out_unknown:
953 return "unknown";
954 }
955
evsel__name_is(struct evsel * evsel,const char * name)956 bool evsel__name_is(struct evsel *evsel, const char *name)
957 {
958 return !strcmp(evsel__name(evsel), name);
959 }
960
evsel__metric_id(const struct evsel * evsel)961 const char *evsel__metric_id(const struct evsel *evsel)
962 {
963 if (evsel->metric_id)
964 return evsel->metric_id;
965
966 if (evsel__is_tool(evsel))
967 return evsel__tool_pmu_event_name(evsel);
968
969 return "unknown";
970 }
971
evsel__group_name(struct evsel * evsel)972 const char *evsel__group_name(struct evsel *evsel)
973 {
974 return evsel->group_name ?: "anon group";
975 }
976
977 /*
978 * Returns the group details for the specified leader,
979 * with following rules.
980 *
981 * For record -e '{cycles,instructions}'
982 * 'anon group { cycles:u, instructions:u }'
983 *
984 * For record -e 'cycles,instructions' and report --group
985 * 'cycles:u, instructions:u'
986 */
evsel__group_desc(struct evsel * evsel,char * buf,size_t size)987 int evsel__group_desc(struct evsel *evsel, char *buf, size_t size)
988 {
989 int ret = 0;
990 bool first = true;
991 struct evsel *pos;
992 const char *group_name = evsel__group_name(evsel);
993
994 if (!evsel->forced_leader)
995 ret = scnprintf(buf, size, "%s { ", group_name);
996
997 for_each_group_evsel(pos, evsel) {
998 if (symbol_conf.skip_empty &&
999 evsel__hists(pos)->stats.nr_samples == 0)
1000 continue;
1001
1002 ret += scnprintf(buf + ret, size - ret, "%s%s",
1003 first ? "" : ", ", evsel__name(pos));
1004 first = false;
1005 }
1006
1007 if (!evsel->forced_leader)
1008 ret += scnprintf(buf + ret, size - ret, " }");
1009
1010 return ret;
1011 }
1012
__evsel__config_callchain(struct evsel * evsel,struct record_opts * opts,struct callchain_param * param)1013 static void __evsel__config_callchain(struct evsel *evsel, struct record_opts *opts,
1014 struct callchain_param *param)
1015 {
1016 bool function = evsel__is_function_event(evsel);
1017 struct perf_event_attr *attr = &evsel->core.attr;
1018
1019 evsel__set_sample_bit(evsel, CALLCHAIN);
1020
1021 attr->sample_max_stack = param->max_stack;
1022
1023 if (opts->kernel_callchains)
1024 attr->exclude_callchain_user = 1;
1025 if (opts->user_callchains)
1026 attr->exclude_callchain_kernel = 1;
1027 if (param->record_mode == CALLCHAIN_LBR) {
1028 if (!opts->branch_stack) {
1029 if (attr->exclude_user) {
1030 pr_warning("LBR callstack option is only available "
1031 "to get user callchain information. "
1032 "Falling back to framepointers.\n");
1033 } else {
1034 evsel__set_sample_bit(evsel, BRANCH_STACK);
1035 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
1036 PERF_SAMPLE_BRANCH_CALL_STACK |
1037 PERF_SAMPLE_BRANCH_NO_CYCLES |
1038 PERF_SAMPLE_BRANCH_NO_FLAGS |
1039 PERF_SAMPLE_BRANCH_HW_INDEX;
1040 }
1041 } else
1042 pr_warning("Cannot use LBR callstack with branch stack. "
1043 "Falling back to framepointers.\n");
1044 }
1045
1046 if (param->record_mode == CALLCHAIN_DWARF) {
1047 if (!function) {
1048 const char *arch = perf_env__arch(evsel__env(evsel));
1049
1050 evsel__set_sample_bit(evsel, REGS_USER);
1051 evsel__set_sample_bit(evsel, STACK_USER);
1052 if (opts->sample_user_regs &&
1053 DWARF_MINIMAL_REGS(arch) != arch__user_reg_mask()) {
1054 attr->sample_regs_user |= DWARF_MINIMAL_REGS(arch);
1055 pr_warning("WARNING: The use of --call-graph=dwarf may require all the user registers, "
1056 "specifying a subset with --user-regs may render DWARF unwinding unreliable, "
1057 "so the minimal registers set (IP, SP) is explicitly forced.\n");
1058 } else {
1059 attr->sample_regs_user |= arch__user_reg_mask();
1060 }
1061 attr->sample_stack_user = param->dump_size;
1062 attr->exclude_callchain_user = 1;
1063 } else {
1064 pr_info("Cannot use DWARF unwind for function trace event,"
1065 " falling back to framepointers.\n");
1066 }
1067 }
1068
1069 if (function) {
1070 pr_info("Disabling user space callchains for function trace event.\n");
1071 attr->exclude_callchain_user = 1;
1072 }
1073 }
1074
evsel__config_callchain(struct evsel * evsel,struct record_opts * opts,struct callchain_param * param)1075 void evsel__config_callchain(struct evsel *evsel, struct record_opts *opts,
1076 struct callchain_param *param)
1077 {
1078 if (param->enabled)
1079 return __evsel__config_callchain(evsel, opts, param);
1080 }
1081
evsel__reset_callgraph(struct evsel * evsel,struct callchain_param * param)1082 static void evsel__reset_callgraph(struct evsel *evsel, struct callchain_param *param)
1083 {
1084 struct perf_event_attr *attr = &evsel->core.attr;
1085
1086 evsel__reset_sample_bit(evsel, CALLCHAIN);
1087 if (param->record_mode == CALLCHAIN_LBR) {
1088 evsel__reset_sample_bit(evsel, BRANCH_STACK);
1089 attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER |
1090 PERF_SAMPLE_BRANCH_CALL_STACK |
1091 PERF_SAMPLE_BRANCH_HW_INDEX);
1092 }
1093 if (param->record_mode == CALLCHAIN_DWARF) {
1094 evsel__reset_sample_bit(evsel, REGS_USER);
1095 evsel__reset_sample_bit(evsel, STACK_USER);
1096 }
1097 }
1098
evsel__apply_config_terms(struct evsel * evsel,struct record_opts * opts,bool track)1099 static void evsel__apply_config_terms(struct evsel *evsel,
1100 struct record_opts *opts, bool track)
1101 {
1102 struct evsel_config_term *term;
1103 struct list_head *config_terms = &evsel->config_terms;
1104 struct perf_event_attr *attr = &evsel->core.attr;
1105 /* callgraph default */
1106 struct callchain_param param = {
1107 .record_mode = callchain_param.record_mode,
1108 };
1109 u32 dump_size = 0;
1110 int max_stack = 0;
1111 const char *callgraph_buf = NULL;
1112
1113 list_for_each_entry(term, config_terms, list) {
1114 switch (term->type) {
1115 case EVSEL__CONFIG_TERM_PERIOD:
1116 if (!(term->weak && opts->user_interval != ULLONG_MAX)) {
1117 attr->sample_period = term->val.period;
1118 attr->freq = 0;
1119 evsel__reset_sample_bit(evsel, PERIOD);
1120 }
1121 break;
1122 case EVSEL__CONFIG_TERM_FREQ:
1123 if (!(term->weak && opts->user_freq != UINT_MAX)) {
1124 attr->sample_freq = term->val.freq;
1125 attr->freq = 1;
1126 evsel__set_sample_bit(evsel, PERIOD);
1127 }
1128 break;
1129 case EVSEL__CONFIG_TERM_TIME:
1130 if (term->val.time)
1131 evsel__set_sample_bit(evsel, TIME);
1132 else
1133 evsel__reset_sample_bit(evsel, TIME);
1134 break;
1135 case EVSEL__CONFIG_TERM_CALLGRAPH:
1136 callgraph_buf = term->val.str;
1137 break;
1138 case EVSEL__CONFIG_TERM_BRANCH:
1139 if (term->val.str && strcmp(term->val.str, "no")) {
1140 evsel__set_sample_bit(evsel, BRANCH_STACK);
1141 parse_branch_str(term->val.str,
1142 &attr->branch_sample_type);
1143 } else
1144 evsel__reset_sample_bit(evsel, BRANCH_STACK);
1145 break;
1146 case EVSEL__CONFIG_TERM_STACK_USER:
1147 dump_size = term->val.stack_user;
1148 break;
1149 case EVSEL__CONFIG_TERM_MAX_STACK:
1150 max_stack = term->val.max_stack;
1151 break;
1152 case EVSEL__CONFIG_TERM_MAX_EVENTS:
1153 evsel->max_events = term->val.max_events;
1154 break;
1155 case EVSEL__CONFIG_TERM_INHERIT:
1156 /*
1157 * attr->inherit should has already been set by
1158 * evsel__config. If user explicitly set
1159 * inherit using config terms, override global
1160 * opt->no_inherit setting.
1161 */
1162 attr->inherit = term->val.inherit ? 1 : 0;
1163 break;
1164 case EVSEL__CONFIG_TERM_OVERWRITE:
1165 attr->write_backward = term->val.overwrite ? 1 : 0;
1166 break;
1167 case EVSEL__CONFIG_TERM_DRV_CFG:
1168 break;
1169 case EVSEL__CONFIG_TERM_PERCORE:
1170 break;
1171 case EVSEL__CONFIG_TERM_AUX_OUTPUT:
1172 attr->aux_output = term->val.aux_output ? 1 : 0;
1173 break;
1174 case EVSEL__CONFIG_TERM_AUX_ACTION:
1175 /* Already applied by auxtrace */
1176 break;
1177 case EVSEL__CONFIG_TERM_AUX_SAMPLE_SIZE:
1178 /* Already applied by auxtrace */
1179 break;
1180 case EVSEL__CONFIG_TERM_CFG_CHG:
1181 break;
1182 default:
1183 break;
1184 }
1185 }
1186
1187 /* User explicitly set per-event callgraph, clear the old setting and reset. */
1188 if ((callgraph_buf != NULL) || (dump_size > 0) || max_stack) {
1189 bool sample_address = false;
1190
1191 if (max_stack) {
1192 param.max_stack = max_stack;
1193 if (callgraph_buf == NULL)
1194 callgraph_buf = "fp";
1195 }
1196
1197 /* parse callgraph parameters */
1198 if (callgraph_buf != NULL) {
1199 if (!strcmp(callgraph_buf, "no")) {
1200 param.enabled = false;
1201 param.record_mode = CALLCHAIN_NONE;
1202 } else {
1203 param.enabled = true;
1204 if (parse_callchain_record(callgraph_buf, ¶m)) {
1205 pr_err("per-event callgraph setting for %s failed. "
1206 "Apply callgraph global setting for it\n",
1207 evsel->name);
1208 return;
1209 }
1210 if (param.record_mode == CALLCHAIN_DWARF)
1211 sample_address = true;
1212 }
1213 }
1214 if (dump_size > 0) {
1215 dump_size = round_up(dump_size, sizeof(u64));
1216 param.dump_size = dump_size;
1217 }
1218
1219 /* If global callgraph set, clear it */
1220 if (callchain_param.enabled)
1221 evsel__reset_callgraph(evsel, &callchain_param);
1222
1223 /* set perf-event callgraph */
1224 if (param.enabled) {
1225 if (sample_address) {
1226 evsel__set_sample_bit(evsel, ADDR);
1227 evsel__set_sample_bit(evsel, DATA_SRC);
1228 evsel->core.attr.mmap_data = track;
1229 }
1230 evsel__config_callchain(evsel, opts, ¶m);
1231 }
1232 }
1233 }
1234
__evsel__get_config_term(struct evsel * evsel,enum evsel_term_type type)1235 struct evsel_config_term *__evsel__get_config_term(struct evsel *evsel, enum evsel_term_type type)
1236 {
1237 struct evsel_config_term *term, *found_term = NULL;
1238
1239 list_for_each_entry(term, &evsel->config_terms, list) {
1240 if (term->type == type)
1241 found_term = term;
1242 }
1243
1244 return found_term;
1245 }
1246
arch_evsel__set_sample_weight(struct evsel * evsel)1247 void __weak arch_evsel__set_sample_weight(struct evsel *evsel)
1248 {
1249 evsel__set_sample_bit(evsel, WEIGHT);
1250 }
1251
arch__post_evsel_config(struct evsel * evsel __maybe_unused,struct perf_event_attr * attr __maybe_unused)1252 void __weak arch__post_evsel_config(struct evsel *evsel __maybe_unused,
1253 struct perf_event_attr *attr __maybe_unused)
1254 {
1255 }
1256
evsel__set_default_freq_period(struct record_opts * opts,struct perf_event_attr * attr)1257 static void evsel__set_default_freq_period(struct record_opts *opts,
1258 struct perf_event_attr *attr)
1259 {
1260 if (opts->freq) {
1261 attr->freq = 1;
1262 attr->sample_freq = opts->freq;
1263 } else {
1264 attr->sample_period = opts->default_interval;
1265 }
1266 }
1267
evsel__is_offcpu_event(struct evsel * evsel)1268 static bool evsel__is_offcpu_event(struct evsel *evsel)
1269 {
1270 return evsel__is_bpf_output(evsel) && evsel__name_is(evsel, OFFCPU_EVENT);
1271 }
1272
1273 /*
1274 * The enable_on_exec/disabled value strategy:
1275 *
1276 * 1) For any type of traced program:
1277 * - all independent events and group leaders are disabled
1278 * - all group members are enabled
1279 *
1280 * Group members are ruled by group leaders. They need to
1281 * be enabled, because the group scheduling relies on that.
1282 *
1283 * 2) For traced programs executed by perf:
1284 * - all independent events and group leaders have
1285 * enable_on_exec set
1286 * - we don't specifically enable or disable any event during
1287 * the record command
1288 *
1289 * Independent events and group leaders are initially disabled
1290 * and get enabled by exec. Group members are ruled by group
1291 * leaders as stated in 1).
1292 *
1293 * 3) For traced programs attached by perf (pid/tid):
1294 * - we specifically enable or disable all events during
1295 * the record command
1296 *
1297 * When attaching events to already running traced we
1298 * enable/disable events specifically, as there's no
1299 * initial traced exec call.
1300 */
evsel__config(struct evsel * evsel,struct record_opts * opts,struct callchain_param * callchain)1301 void evsel__config(struct evsel *evsel, struct record_opts *opts,
1302 struct callchain_param *callchain)
1303 {
1304 struct evsel *leader = evsel__leader(evsel);
1305 struct perf_event_attr *attr = &evsel->core.attr;
1306 int track = evsel->tracking;
1307 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
1308
1309 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
1310 attr->inherit = target__has_cpu(&opts->target) ? 0 : !opts->no_inherit;
1311 attr->write_backward = opts->overwrite ? 1 : 0;
1312 attr->read_format = PERF_FORMAT_LOST;
1313
1314 evsel__set_sample_bit(evsel, IP);
1315 evsel__set_sample_bit(evsel, TID);
1316
1317 if (evsel->sample_read) {
1318 evsel__set_sample_bit(evsel, READ);
1319
1320 /*
1321 * We need ID even in case of single event, because
1322 * PERF_SAMPLE_READ process ID specific data.
1323 */
1324 evsel__set_sample_id(evsel, false);
1325
1326 /*
1327 * Apply group format only if we belong to group
1328 * with more than one members.
1329 */
1330 if (leader->core.nr_members > 1) {
1331 attr->read_format |= PERF_FORMAT_GROUP;
1332 }
1333
1334 /*
1335 * Inherit + SAMPLE_READ requires SAMPLE_TID in the read_format
1336 */
1337 if (attr->inherit) {
1338 evsel__set_sample_bit(evsel, TID);
1339 evsel->core.attr.read_format |=
1340 PERF_FORMAT_ID;
1341 }
1342 }
1343
1344 /*
1345 * We default some events to have a default interval. But keep
1346 * it a weak assumption overridable by the user.
1347 */
1348 if ((evsel->is_libpfm_event && !attr->sample_period) ||
1349 (!evsel->is_libpfm_event && (!attr->sample_period ||
1350 opts->user_freq != UINT_MAX ||
1351 opts->user_interval != ULLONG_MAX)))
1352 evsel__set_default_freq_period(opts, attr);
1353
1354 /*
1355 * If attr->freq was set (here or earlier), ask for period
1356 * to be sampled.
1357 */
1358 if (attr->freq)
1359 evsel__set_sample_bit(evsel, PERIOD);
1360
1361 if (opts->no_samples)
1362 attr->sample_freq = 0;
1363
1364 if (opts->inherit_stat) {
1365 evsel->core.attr.read_format |=
1366 PERF_FORMAT_TOTAL_TIME_ENABLED |
1367 PERF_FORMAT_TOTAL_TIME_RUNNING |
1368 PERF_FORMAT_ID;
1369 attr->inherit_stat = 1;
1370 }
1371
1372 if (opts->sample_address) {
1373 evsel__set_sample_bit(evsel, ADDR);
1374 attr->mmap_data = track;
1375 }
1376
1377 /*
1378 * We don't allow user space callchains for function trace
1379 * event, due to issues with page faults while tracing page
1380 * fault handler and its overall trickiness nature.
1381 */
1382 if (evsel__is_function_event(evsel))
1383 evsel->core.attr.exclude_callchain_user = 1;
1384
1385 if (callchain && callchain->enabled && !evsel->no_aux_samples)
1386 evsel__config_callchain(evsel, opts, callchain);
1387
1388 if (opts->sample_intr_regs && !evsel->no_aux_samples &&
1389 !evsel__is_dummy_event(evsel)) {
1390 attr->sample_regs_intr = opts->sample_intr_regs;
1391 evsel__set_sample_bit(evsel, REGS_INTR);
1392 }
1393
1394 if (opts->sample_user_regs && !evsel->no_aux_samples &&
1395 !evsel__is_dummy_event(evsel)) {
1396 attr->sample_regs_user |= opts->sample_user_regs;
1397 evsel__set_sample_bit(evsel, REGS_USER);
1398 }
1399
1400 if (target__has_cpu(&opts->target) || opts->sample_cpu)
1401 evsel__set_sample_bit(evsel, CPU);
1402
1403 /*
1404 * When the user explicitly disabled time don't force it here.
1405 */
1406 if (opts->sample_time &&
1407 (!perf_missing_features.sample_id_all &&
1408 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
1409 opts->sample_time_set)))
1410 evsel__set_sample_bit(evsel, TIME);
1411
1412 if (opts->raw_samples && !evsel->no_aux_samples) {
1413 evsel__set_sample_bit(evsel, TIME);
1414 evsel__set_sample_bit(evsel, RAW);
1415 evsel__set_sample_bit(evsel, CPU);
1416 }
1417
1418 if (opts->sample_address)
1419 evsel__set_sample_bit(evsel, DATA_SRC);
1420
1421 if (opts->sample_phys_addr)
1422 evsel__set_sample_bit(evsel, PHYS_ADDR);
1423
1424 if (opts->no_buffering) {
1425 attr->watermark = 0;
1426 attr->wakeup_events = 1;
1427 }
1428 if (opts->branch_stack && !evsel->no_aux_samples) {
1429 evsel__set_sample_bit(evsel, BRANCH_STACK);
1430 attr->branch_sample_type = opts->branch_stack;
1431 }
1432
1433 if (opts->sample_weight)
1434 arch_evsel__set_sample_weight(evsel);
1435
1436 attr->task = track;
1437 attr->mmap = track;
1438 attr->mmap2 = track && !perf_missing_features.mmap2;
1439 attr->comm = track;
1440 attr->build_id = track && opts->build_id;
1441
1442 /*
1443 * ksymbol is tracked separately with text poke because it needs to be
1444 * system wide and enabled immediately.
1445 */
1446 if (!opts->text_poke)
1447 attr->ksymbol = track && !perf_missing_features.ksymbol;
1448 attr->bpf_event = track && !opts->no_bpf_event && !perf_missing_features.bpf;
1449
1450 if (opts->record_namespaces)
1451 attr->namespaces = track;
1452
1453 if (opts->record_cgroup) {
1454 attr->cgroup = track && !perf_missing_features.cgroup;
1455 evsel__set_sample_bit(evsel, CGROUP);
1456 }
1457
1458 if (opts->sample_data_page_size)
1459 evsel__set_sample_bit(evsel, DATA_PAGE_SIZE);
1460
1461 if (opts->sample_code_page_size)
1462 evsel__set_sample_bit(evsel, CODE_PAGE_SIZE);
1463
1464 if (opts->record_switch_events)
1465 attr->context_switch = track;
1466
1467 if (opts->sample_transaction)
1468 evsel__set_sample_bit(evsel, TRANSACTION);
1469
1470 if (opts->running_time) {
1471 evsel->core.attr.read_format |=
1472 PERF_FORMAT_TOTAL_TIME_ENABLED |
1473 PERF_FORMAT_TOTAL_TIME_RUNNING;
1474 }
1475
1476 /*
1477 * XXX see the function comment above
1478 *
1479 * Disabling only independent events or group leaders,
1480 * keeping group members enabled.
1481 */
1482 if (evsel__is_group_leader(evsel))
1483 attr->disabled = 1;
1484
1485 /*
1486 * Setting enable_on_exec for independent events and
1487 * group leaders for traced executed by perf.
1488 */
1489 if (target__none(&opts->target) && evsel__is_group_leader(evsel) &&
1490 !opts->target.initial_delay)
1491 attr->enable_on_exec = 1;
1492
1493 if (evsel->immediate) {
1494 attr->disabled = 0;
1495 attr->enable_on_exec = 0;
1496 }
1497
1498 clockid = opts->clockid;
1499 if (opts->use_clockid) {
1500 attr->use_clockid = 1;
1501 attr->clockid = opts->clockid;
1502 }
1503
1504 if (evsel->precise_max)
1505 attr->precise_ip = 3;
1506
1507 if (opts->all_user) {
1508 attr->exclude_kernel = 1;
1509 attr->exclude_user = 0;
1510 }
1511
1512 if (opts->all_kernel) {
1513 attr->exclude_kernel = 0;
1514 attr->exclude_user = 1;
1515 }
1516
1517 if (evsel->core.own_cpus || evsel->unit)
1518 evsel->core.attr.read_format |= PERF_FORMAT_ID;
1519
1520 /*
1521 * Apply event specific term settings,
1522 * it overloads any global configuration.
1523 */
1524 evsel__apply_config_terms(evsel, opts, track);
1525
1526 evsel->ignore_missing_thread = opts->ignore_missing_thread;
1527
1528 /* The --period option takes the precedence. */
1529 if (opts->period_set) {
1530 if (opts->period)
1531 evsel__set_sample_bit(evsel, PERIOD);
1532 else
1533 evsel__reset_sample_bit(evsel, PERIOD);
1534 }
1535
1536 /*
1537 * A dummy event never triggers any actual counter and therefore
1538 * cannot be used with branch_stack.
1539 *
1540 * For initial_delay, a dummy event is added implicitly.
1541 * The software event will trigger -EOPNOTSUPP error out,
1542 * if BRANCH_STACK bit is set.
1543 */
1544 if (evsel__is_dummy_event(evsel))
1545 evsel__reset_sample_bit(evsel, BRANCH_STACK);
1546
1547 if (evsel__is_offcpu_event(evsel))
1548 evsel->core.attr.sample_type &= OFFCPU_SAMPLE_TYPES;
1549
1550 arch__post_evsel_config(evsel, attr);
1551 }
1552
evsel__set_filter(struct evsel * evsel,const char * filter)1553 int evsel__set_filter(struct evsel *evsel, const char *filter)
1554 {
1555 char *new_filter = strdup(filter);
1556
1557 if (new_filter != NULL) {
1558 free(evsel->filter);
1559 evsel->filter = new_filter;
1560 return 0;
1561 }
1562
1563 return -1;
1564 }
1565
evsel__append_filter(struct evsel * evsel,const char * fmt,const char * filter)1566 static int evsel__append_filter(struct evsel *evsel, const char *fmt, const char *filter)
1567 {
1568 char *new_filter;
1569
1570 if (evsel->filter == NULL)
1571 return evsel__set_filter(evsel, filter);
1572
1573 if (asprintf(&new_filter, fmt, evsel->filter, filter) > 0) {
1574 free(evsel->filter);
1575 evsel->filter = new_filter;
1576 return 0;
1577 }
1578
1579 return -1;
1580 }
1581
evsel__append_tp_filter(struct evsel * evsel,const char * filter)1582 int evsel__append_tp_filter(struct evsel *evsel, const char *filter)
1583 {
1584 return evsel__append_filter(evsel, "(%s) && (%s)", filter);
1585 }
1586
evsel__append_addr_filter(struct evsel * evsel,const char * filter)1587 int evsel__append_addr_filter(struct evsel *evsel, const char *filter)
1588 {
1589 return evsel__append_filter(evsel, "%s,%s", filter);
1590 }
1591
1592 /* Caller has to clear disabled after going through all CPUs. */
evsel__enable_cpu(struct evsel * evsel,int cpu_map_idx)1593 int evsel__enable_cpu(struct evsel *evsel, int cpu_map_idx)
1594 {
1595 return perf_evsel__enable_cpu(&evsel->core, cpu_map_idx);
1596 }
1597
evsel__enable(struct evsel * evsel)1598 int evsel__enable(struct evsel *evsel)
1599 {
1600 int err = perf_evsel__enable(&evsel->core);
1601
1602 if (!err)
1603 evsel->disabled = false;
1604 return err;
1605 }
1606
1607 /* Caller has to set disabled after going through all CPUs. */
evsel__disable_cpu(struct evsel * evsel,int cpu_map_idx)1608 int evsel__disable_cpu(struct evsel *evsel, int cpu_map_idx)
1609 {
1610 return perf_evsel__disable_cpu(&evsel->core, cpu_map_idx);
1611 }
1612
evsel__disable(struct evsel * evsel)1613 int evsel__disable(struct evsel *evsel)
1614 {
1615 int err = perf_evsel__disable(&evsel->core);
1616 /*
1617 * We mark it disabled here so that tools that disable a event can
1618 * ignore events after they disable it. I.e. the ring buffer may have
1619 * already a few more events queued up before the kernel got the stop
1620 * request.
1621 */
1622 if (!err)
1623 evsel->disabled = true;
1624
1625 return err;
1626 }
1627
free_config_terms(struct list_head * config_terms)1628 void free_config_terms(struct list_head *config_terms)
1629 {
1630 struct evsel_config_term *term, *h;
1631
1632 list_for_each_entry_safe(term, h, config_terms, list) {
1633 list_del_init(&term->list);
1634 if (term->free_str)
1635 zfree(&term->val.str);
1636 free(term);
1637 }
1638 }
1639
evsel__free_config_terms(struct evsel * evsel)1640 static void evsel__free_config_terms(struct evsel *evsel)
1641 {
1642 free_config_terms(&evsel->config_terms);
1643 }
1644
evsel__exit(struct evsel * evsel)1645 void evsel__exit(struct evsel *evsel)
1646 {
1647 assert(list_empty(&evsel->core.node));
1648 assert(evsel->evlist == NULL);
1649 bpf_counter__destroy(evsel);
1650 perf_bpf_filter__destroy(evsel);
1651 evsel__free_counts(evsel);
1652 perf_evsel__free_fd(&evsel->core);
1653 perf_evsel__free_id(&evsel->core);
1654 evsel__free_config_terms(evsel);
1655 cgroup__put(evsel->cgrp);
1656 perf_cpu_map__put(evsel->core.cpus);
1657 perf_cpu_map__put(evsel->core.own_cpus);
1658 perf_thread_map__put(evsel->core.threads);
1659 zfree(&evsel->group_name);
1660 zfree(&evsel->name);
1661 #ifdef HAVE_LIBTRACEEVENT
1662 zfree(&evsel->tp_sys);
1663 zfree(&evsel->tp_name);
1664 #endif
1665 zfree(&evsel->filter);
1666 zfree(&evsel->group_pmu_name);
1667 zfree(&evsel->unit);
1668 zfree(&evsel->metric_id);
1669 evsel__zero_per_pkg(evsel);
1670 hashmap__free(evsel->per_pkg_mask);
1671 evsel->per_pkg_mask = NULL;
1672 zfree(&evsel->metric_events);
1673 perf_evsel__object.fini(evsel);
1674 if (evsel__tool_event(evsel) == TOOL_PMU__EVENT_SYSTEM_TIME ||
1675 evsel__tool_event(evsel) == TOOL_PMU__EVENT_USER_TIME)
1676 xyarray__delete(evsel->start_times);
1677 }
1678
evsel__delete(struct evsel * evsel)1679 void evsel__delete(struct evsel *evsel)
1680 {
1681 if (!evsel)
1682 return;
1683
1684 evsel__exit(evsel);
1685 free(evsel);
1686 }
1687
evsel__compute_deltas(struct evsel * evsel,int cpu_map_idx,int thread,struct perf_counts_values * count)1688 void evsel__compute_deltas(struct evsel *evsel, int cpu_map_idx, int thread,
1689 struct perf_counts_values *count)
1690 {
1691 struct perf_counts_values tmp;
1692
1693 if (!evsel->prev_raw_counts)
1694 return;
1695
1696 tmp = *perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread);
1697 *perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread) = *count;
1698
1699 count->val = count->val - tmp.val;
1700 count->ena = count->ena - tmp.ena;
1701 count->run = count->run - tmp.run;
1702 }
1703
evsel__read_one(struct evsel * evsel,int cpu_map_idx,int thread)1704 static int evsel__read_one(struct evsel *evsel, int cpu_map_idx, int thread)
1705 {
1706 struct perf_counts_values *count = perf_counts(evsel->counts, cpu_map_idx, thread);
1707
1708 return perf_evsel__read(&evsel->core, cpu_map_idx, thread, count);
1709 }
1710
evsel__read_retire_lat(struct evsel * evsel,int cpu_map_idx,int thread)1711 static int evsel__read_retire_lat(struct evsel *evsel, int cpu_map_idx, int thread)
1712 {
1713 return tpebs_set_evsel(evsel, cpu_map_idx, thread);
1714 }
1715
evsel__set_count(struct evsel * counter,int cpu_map_idx,int thread,u64 val,u64 ena,u64 run,u64 lost)1716 static void evsel__set_count(struct evsel *counter, int cpu_map_idx, int thread,
1717 u64 val, u64 ena, u64 run, u64 lost)
1718 {
1719 struct perf_counts_values *count;
1720
1721 count = perf_counts(counter->counts, cpu_map_idx, thread);
1722
1723 if (counter->retire_lat) {
1724 evsel__read_retire_lat(counter, cpu_map_idx, thread);
1725 perf_counts__set_loaded(counter->counts, cpu_map_idx, thread, true);
1726 return;
1727 }
1728
1729 count->val = val;
1730 count->ena = ena;
1731 count->run = run;
1732 count->lost = lost;
1733
1734 perf_counts__set_loaded(counter->counts, cpu_map_idx, thread, true);
1735 }
1736
evsel__group_has_tpebs(struct evsel * leader)1737 static bool evsel__group_has_tpebs(struct evsel *leader)
1738 {
1739 struct evsel *evsel;
1740
1741 for_each_group_evsel(evsel, leader) {
1742 if (evsel__is_retire_lat(evsel))
1743 return true;
1744 }
1745 return false;
1746 }
1747
evsel__group_read_nr_members(struct evsel * leader)1748 static u64 evsel__group_read_nr_members(struct evsel *leader)
1749 {
1750 u64 nr = leader->core.nr_members;
1751 struct evsel *evsel;
1752
1753 for_each_group_evsel(evsel, leader) {
1754 if (evsel__is_retire_lat(evsel))
1755 nr--;
1756 }
1757 return nr;
1758 }
1759
evsel__group_read_size(struct evsel * leader)1760 static u64 evsel__group_read_size(struct evsel *leader)
1761 {
1762 u64 read_format = leader->core.attr.read_format;
1763 int entry = sizeof(u64); /* value */
1764 int size = 0;
1765 int nr = 1;
1766
1767 if (!evsel__group_has_tpebs(leader))
1768 return perf_evsel__read_size(&leader->core);
1769
1770 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1771 size += sizeof(u64);
1772
1773 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1774 size += sizeof(u64);
1775
1776 if (read_format & PERF_FORMAT_ID)
1777 entry += sizeof(u64);
1778
1779 if (read_format & PERF_FORMAT_LOST)
1780 entry += sizeof(u64);
1781
1782 if (read_format & PERF_FORMAT_GROUP) {
1783 nr = evsel__group_read_nr_members(leader);
1784 size += sizeof(u64);
1785 }
1786
1787 size += entry * nr;
1788 return size;
1789 }
1790
evsel__process_group_data(struct evsel * leader,int cpu_map_idx,int thread,u64 * data)1791 static int evsel__process_group_data(struct evsel *leader, int cpu_map_idx, int thread, u64 *data)
1792 {
1793 u64 read_format = leader->core.attr.read_format;
1794 struct sample_read_value *v;
1795 u64 nr, ena = 0, run = 0, lost = 0;
1796
1797 nr = *data++;
1798
1799 if (nr != evsel__group_read_nr_members(leader))
1800 return -EINVAL;
1801
1802 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1803 ena = *data++;
1804
1805 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1806 run = *data++;
1807
1808 v = (void *)data;
1809 sample_read_group__for_each(v, nr, read_format) {
1810 struct evsel *counter;
1811
1812 counter = evlist__id2evsel(leader->evlist, v->id);
1813 if (!counter)
1814 return -EINVAL;
1815
1816 if (read_format & PERF_FORMAT_LOST)
1817 lost = v->lost;
1818
1819 evsel__set_count(counter, cpu_map_idx, thread, v->value, ena, run, lost);
1820 }
1821
1822 return 0;
1823 }
1824
evsel__read_group(struct evsel * leader,int cpu_map_idx,int thread)1825 static int evsel__read_group(struct evsel *leader, int cpu_map_idx, int thread)
1826 {
1827 struct perf_stat_evsel *ps = leader->stats;
1828 u64 read_format = leader->core.attr.read_format;
1829 int size = evsel__group_read_size(leader);
1830 u64 *data = ps->group_data;
1831
1832 if (!(read_format & PERF_FORMAT_ID))
1833 return -EINVAL;
1834
1835 if (!evsel__is_group_leader(leader))
1836 return -EINVAL;
1837
1838 if (!data) {
1839 data = zalloc(size);
1840 if (!data)
1841 return -ENOMEM;
1842
1843 ps->group_data = data;
1844 }
1845
1846 if (FD(leader, cpu_map_idx, thread) < 0)
1847 return -EINVAL;
1848
1849 if (readn(FD(leader, cpu_map_idx, thread), data, size) <= 0)
1850 return -errno;
1851
1852 return evsel__process_group_data(leader, cpu_map_idx, thread, data);
1853 }
1854
__evsel__match(const struct evsel * evsel,u32 type,u64 config)1855 bool __evsel__match(const struct evsel *evsel, u32 type, u64 config)
1856 {
1857
1858 u32 e_type = evsel->core.attr.type;
1859 u64 e_config = evsel->core.attr.config;
1860
1861 if (e_type != type) {
1862 return type == PERF_TYPE_HARDWARE && evsel->pmu && evsel->pmu->is_core &&
1863 evsel->alternate_hw_config == config;
1864 }
1865
1866 if ((type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE) &&
1867 perf_pmus__supports_extended_type())
1868 e_config &= PERF_HW_EVENT_MASK;
1869
1870 return e_config == config;
1871 }
1872
evsel__read_counter(struct evsel * evsel,int cpu_map_idx,int thread)1873 int evsel__read_counter(struct evsel *evsel, int cpu_map_idx, int thread)
1874 {
1875 if (evsel__is_tool(evsel))
1876 return evsel__tool_pmu_read(evsel, cpu_map_idx, thread);
1877
1878 if (evsel__is_hwmon(evsel))
1879 return evsel__hwmon_pmu_read(evsel, cpu_map_idx, thread);
1880
1881 if (evsel__is_retire_lat(evsel))
1882 return evsel__read_retire_lat(evsel, cpu_map_idx, thread);
1883
1884 if (evsel->core.attr.read_format & PERF_FORMAT_GROUP)
1885 return evsel__read_group(evsel, cpu_map_idx, thread);
1886
1887 return evsel__read_one(evsel, cpu_map_idx, thread);
1888 }
1889
__evsel__read_on_cpu(struct evsel * evsel,int cpu_map_idx,int thread,bool scale)1890 int __evsel__read_on_cpu(struct evsel *evsel, int cpu_map_idx, int thread, bool scale)
1891 {
1892 struct perf_counts_values count;
1893 size_t nv = scale ? 3 : 1;
1894
1895 if (FD(evsel, cpu_map_idx, thread) < 0)
1896 return -EINVAL;
1897
1898 if (evsel->counts == NULL && evsel__alloc_counts(evsel) < 0)
1899 return -ENOMEM;
1900
1901 if (readn(FD(evsel, cpu_map_idx, thread), &count, nv * sizeof(u64)) <= 0)
1902 return -errno;
1903
1904 evsel__compute_deltas(evsel, cpu_map_idx, thread, &count);
1905 perf_counts_values__scale(&count, scale, NULL);
1906 *perf_counts(evsel->counts, cpu_map_idx, thread) = count;
1907 return 0;
1908 }
1909
evsel__match_other_cpu(struct evsel * evsel,struct evsel * other,int cpu_map_idx)1910 static int evsel__match_other_cpu(struct evsel *evsel, struct evsel *other,
1911 int cpu_map_idx)
1912 {
1913 struct perf_cpu cpu;
1914
1915 cpu = perf_cpu_map__cpu(evsel->core.cpus, cpu_map_idx);
1916 return perf_cpu_map__idx(other->core.cpus, cpu);
1917 }
1918
evsel__hybrid_group_cpu_map_idx(struct evsel * evsel,int cpu_map_idx)1919 static int evsel__hybrid_group_cpu_map_idx(struct evsel *evsel, int cpu_map_idx)
1920 {
1921 struct evsel *leader = evsel__leader(evsel);
1922
1923 if ((evsel__is_hybrid(evsel) && !evsel__is_hybrid(leader)) ||
1924 (!evsel__is_hybrid(evsel) && evsel__is_hybrid(leader))) {
1925 return evsel__match_other_cpu(evsel, leader, cpu_map_idx);
1926 }
1927
1928 return cpu_map_idx;
1929 }
1930
get_group_fd(struct evsel * evsel,int cpu_map_idx,int thread)1931 static int get_group_fd(struct evsel *evsel, int cpu_map_idx, int thread)
1932 {
1933 struct evsel *leader = evsel__leader(evsel);
1934 int fd;
1935
1936 if (evsel__is_group_leader(evsel))
1937 return -1;
1938
1939 /*
1940 * Leader must be already processed/open,
1941 * if not it's a bug.
1942 */
1943 BUG_ON(!leader->core.fd);
1944
1945 cpu_map_idx = evsel__hybrid_group_cpu_map_idx(evsel, cpu_map_idx);
1946 if (cpu_map_idx == -1)
1947 return -1;
1948
1949 fd = FD(leader, cpu_map_idx, thread);
1950 BUG_ON(fd == -1 && !leader->skippable);
1951
1952 /*
1953 * When the leader has been skipped, return -2 to distinguish from no
1954 * group leader case.
1955 */
1956 return fd == -1 ? -2 : fd;
1957 }
1958
evsel__remove_fd(struct evsel * pos,int nr_cpus,int nr_threads,int thread_idx)1959 static void evsel__remove_fd(struct evsel *pos, int nr_cpus, int nr_threads, int thread_idx)
1960 {
1961 for (int cpu = 0; cpu < nr_cpus; cpu++)
1962 for (int thread = thread_idx; thread < nr_threads - 1; thread++)
1963 FD(pos, cpu, thread) = FD(pos, cpu, thread + 1);
1964 }
1965
update_fds(struct evsel * evsel,int nr_cpus,int cpu_map_idx,int nr_threads,int thread_idx)1966 static int update_fds(struct evsel *evsel,
1967 int nr_cpus, int cpu_map_idx,
1968 int nr_threads, int thread_idx)
1969 {
1970 struct evsel *pos;
1971
1972 if (cpu_map_idx >= nr_cpus || thread_idx >= nr_threads)
1973 return -EINVAL;
1974
1975 evlist__for_each_entry(evsel->evlist, pos) {
1976 nr_cpus = pos != evsel ? nr_cpus : cpu_map_idx;
1977
1978 evsel__remove_fd(pos, nr_cpus, nr_threads, thread_idx);
1979
1980 /*
1981 * Since fds for next evsel has not been created,
1982 * there is no need to iterate whole event list.
1983 */
1984 if (pos == evsel)
1985 break;
1986 }
1987 return 0;
1988 }
1989
evsel__ignore_missing_thread(struct evsel * evsel,int nr_cpus,int cpu_map_idx,struct perf_thread_map * threads,int thread,int err)1990 static bool evsel__ignore_missing_thread(struct evsel *evsel,
1991 int nr_cpus, int cpu_map_idx,
1992 struct perf_thread_map *threads,
1993 int thread, int err)
1994 {
1995 pid_t ignore_pid = perf_thread_map__pid(threads, thread);
1996
1997 if (!evsel->ignore_missing_thread)
1998 return false;
1999
2000 /* The system wide setup does not work with threads. */
2001 if (evsel->core.system_wide)
2002 return false;
2003
2004 /* The -ESRCH is perf event syscall errno for pid's not found. */
2005 if (err != -ESRCH)
2006 return false;
2007
2008 /* If there's only one thread, let it fail. */
2009 if (threads->nr == 1)
2010 return false;
2011
2012 /*
2013 * We should remove fd for missing_thread first
2014 * because thread_map__remove() will decrease threads->nr.
2015 */
2016 if (update_fds(evsel, nr_cpus, cpu_map_idx, threads->nr, thread))
2017 return false;
2018
2019 if (thread_map__remove(threads, thread))
2020 return false;
2021
2022 pr_warning("WARNING: Ignored open failure for pid %d\n",
2023 ignore_pid);
2024 return true;
2025 }
2026
__open_attr__fprintf(FILE * fp,const char * name,const char * val,void * priv __maybe_unused)2027 static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
2028 void *priv __maybe_unused)
2029 {
2030 return fprintf(fp, " %-32s %s\n", name, val);
2031 }
2032
display_attr(struct perf_event_attr * attr)2033 static void display_attr(struct perf_event_attr *attr)
2034 {
2035 if (verbose >= 2 || debug_peo_args) {
2036 fprintf(stderr, "%.60s\n", graph_dotted_line);
2037 fprintf(stderr, "perf_event_attr:\n");
2038 perf_event_attr__fprintf(stderr, attr, __open_attr__fprintf, NULL);
2039 fprintf(stderr, "%.60s\n", graph_dotted_line);
2040 }
2041 }
2042
evsel__precise_ip_fallback(struct evsel * evsel)2043 bool evsel__precise_ip_fallback(struct evsel *evsel)
2044 {
2045 /* Do not try less precise if not requested. */
2046 if (!evsel->precise_max)
2047 return false;
2048
2049 /*
2050 * We tried all the precise_ip values, and it's
2051 * still failing, so leave it to standard fallback.
2052 */
2053 if (!evsel->core.attr.precise_ip) {
2054 evsel->core.attr.precise_ip = evsel->precise_ip_original;
2055 return false;
2056 }
2057
2058 if (!evsel->precise_ip_original)
2059 evsel->precise_ip_original = evsel->core.attr.precise_ip;
2060
2061 evsel->core.attr.precise_ip--;
2062 pr_debug2_peo("decreasing precise_ip by one (%d)\n", evsel->core.attr.precise_ip);
2063 display_attr(&evsel->core.attr);
2064 return true;
2065 }
2066
2067 static struct perf_cpu_map *empty_cpu_map;
2068 static struct perf_thread_map *empty_thread_map;
2069
__evsel__prepare_open(struct evsel * evsel,struct perf_cpu_map * cpus,struct perf_thread_map * threads)2070 static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
2071 struct perf_thread_map *threads)
2072 {
2073 int ret = 0;
2074 int nthreads = perf_thread_map__nr(threads);
2075
2076 if ((perf_missing_features.write_backward && evsel->core.attr.write_backward) ||
2077 (perf_missing_features.aux_output && evsel->core.attr.aux_output))
2078 return -EINVAL;
2079
2080 if (cpus == NULL) {
2081 if (empty_cpu_map == NULL) {
2082 empty_cpu_map = perf_cpu_map__new_any_cpu();
2083 if (empty_cpu_map == NULL)
2084 return -ENOMEM;
2085 }
2086
2087 cpus = empty_cpu_map;
2088 }
2089
2090 if (threads == NULL) {
2091 if (empty_thread_map == NULL) {
2092 empty_thread_map = thread_map__new_by_tid(-1);
2093 if (empty_thread_map == NULL)
2094 return -ENOMEM;
2095 }
2096
2097 threads = empty_thread_map;
2098 }
2099
2100 if (evsel->core.fd == NULL &&
2101 perf_evsel__alloc_fd(&evsel->core, perf_cpu_map__nr(cpus), nthreads) < 0)
2102 return -ENOMEM;
2103
2104 if (evsel__is_tool(evsel))
2105 ret = evsel__tool_pmu_prepare_open(evsel, cpus, nthreads);
2106
2107 evsel->open_flags = PERF_FLAG_FD_CLOEXEC;
2108 if (evsel->cgrp)
2109 evsel->open_flags |= PERF_FLAG_PID_CGROUP;
2110
2111 return ret;
2112 }
2113
evsel__disable_missing_features(struct evsel * evsel)2114 static void evsel__disable_missing_features(struct evsel *evsel)
2115 {
2116 if (perf_missing_features.inherit_sample_read && evsel->core.attr.inherit &&
2117 (evsel->core.attr.sample_type & PERF_SAMPLE_READ))
2118 evsel->core.attr.inherit = 0;
2119 if (perf_missing_features.branch_counters)
2120 evsel->core.attr.branch_sample_type &= ~PERF_SAMPLE_BRANCH_COUNTERS;
2121 if (perf_missing_features.read_lost)
2122 evsel->core.attr.read_format &= ~PERF_FORMAT_LOST;
2123 if (perf_missing_features.weight_struct) {
2124 evsel__set_sample_bit(evsel, WEIGHT);
2125 evsel__reset_sample_bit(evsel, WEIGHT_STRUCT);
2126 }
2127 if (perf_missing_features.clockid_wrong)
2128 evsel->core.attr.clockid = CLOCK_MONOTONIC; /* should always work */
2129 if (perf_missing_features.clockid) {
2130 evsel->core.attr.use_clockid = 0;
2131 evsel->core.attr.clockid = 0;
2132 }
2133 if (perf_missing_features.cloexec)
2134 evsel->open_flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
2135 if (perf_missing_features.mmap2)
2136 evsel->core.attr.mmap2 = 0;
2137 if (evsel->pmu && evsel->pmu->missing_features.exclude_guest)
2138 evsel->core.attr.exclude_guest = evsel->core.attr.exclude_host = 0;
2139 if (perf_missing_features.lbr_flags)
2140 evsel->core.attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
2141 PERF_SAMPLE_BRANCH_NO_CYCLES);
2142 if (perf_missing_features.group_read && evsel->core.attr.inherit)
2143 evsel->core.attr.read_format &= ~(PERF_FORMAT_GROUP|PERF_FORMAT_ID);
2144 if (perf_missing_features.ksymbol)
2145 evsel->core.attr.ksymbol = 0;
2146 if (perf_missing_features.bpf)
2147 evsel->core.attr.bpf_event = 0;
2148 if (perf_missing_features.branch_hw_idx)
2149 evsel->core.attr.branch_sample_type &= ~PERF_SAMPLE_BRANCH_HW_INDEX;
2150 if (perf_missing_features.sample_id_all)
2151 evsel->core.attr.sample_id_all = 0;
2152 }
2153
evsel__prepare_open(struct evsel * evsel,struct perf_cpu_map * cpus,struct perf_thread_map * threads)2154 int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
2155 struct perf_thread_map *threads)
2156 {
2157 int err;
2158
2159 err = __evsel__prepare_open(evsel, cpus, threads);
2160 if (err)
2161 return err;
2162
2163 evsel__disable_missing_features(evsel);
2164
2165 return err;
2166 }
2167
__has_attr_feature(struct perf_event_attr * attr,struct perf_cpu cpu,unsigned long flags)2168 static bool __has_attr_feature(struct perf_event_attr *attr,
2169 struct perf_cpu cpu, unsigned long flags)
2170 {
2171 int fd = syscall(SYS_perf_event_open, attr, /*pid=*/0, cpu.cpu,
2172 /*group_fd=*/-1, flags);
2173 close(fd);
2174
2175 if (fd < 0) {
2176 attr->exclude_kernel = 1;
2177
2178 fd = syscall(SYS_perf_event_open, attr, /*pid=*/0, cpu.cpu,
2179 /*group_fd=*/-1, flags);
2180 close(fd);
2181 }
2182
2183 if (fd < 0) {
2184 attr->exclude_hv = 1;
2185
2186 fd = syscall(SYS_perf_event_open, attr, /*pid=*/0, cpu.cpu,
2187 /*group_fd=*/-1, flags);
2188 close(fd);
2189 }
2190
2191 if (fd < 0) {
2192 attr->exclude_guest = 1;
2193
2194 fd = syscall(SYS_perf_event_open, attr, /*pid=*/0, cpu.cpu,
2195 /*group_fd=*/-1, flags);
2196 close(fd);
2197 }
2198
2199 attr->exclude_kernel = 0;
2200 attr->exclude_guest = 0;
2201 attr->exclude_hv = 0;
2202
2203 return fd >= 0;
2204 }
2205
has_attr_feature(struct perf_event_attr * attr,unsigned long flags)2206 static bool has_attr_feature(struct perf_event_attr *attr, unsigned long flags)
2207 {
2208 struct perf_cpu cpu = {.cpu = -1};
2209
2210 return __has_attr_feature(attr, cpu, flags);
2211 }
2212
evsel__detect_missing_pmu_features(struct evsel * evsel)2213 static void evsel__detect_missing_pmu_features(struct evsel *evsel)
2214 {
2215 struct perf_event_attr attr = {
2216 .type = evsel->core.attr.type,
2217 .config = evsel->core.attr.config,
2218 .disabled = 1,
2219 };
2220 struct perf_pmu *pmu = evsel->pmu;
2221 int old_errno;
2222
2223 old_errno = errno;
2224
2225 if (pmu == NULL)
2226 pmu = evsel->pmu = evsel__find_pmu(evsel);
2227
2228 if (pmu == NULL || pmu->missing_features.checked)
2229 goto out;
2230
2231 /*
2232 * Must probe features in the order they were added to the
2233 * perf_event_attr interface. These are kernel core limitation but
2234 * specific to PMUs with branch stack. So we can detect with the given
2235 * hardware event and stop on the first one succeeded.
2236 */
2237
2238 /* Please add new feature detection here. */
2239
2240 attr.exclude_guest = 1;
2241 if (has_attr_feature(&attr, /*flags=*/0))
2242 goto found;
2243 pmu->missing_features.exclude_guest = true;
2244 pr_debug2("switching off exclude_guest for PMU %s\n", pmu->name);
2245
2246 found:
2247 pmu->missing_features.checked = true;
2248 out:
2249 errno = old_errno;
2250 }
2251
evsel__detect_missing_brstack_features(struct evsel * evsel)2252 static void evsel__detect_missing_brstack_features(struct evsel *evsel)
2253 {
2254 static bool detection_done = false;
2255 struct perf_event_attr attr = {
2256 .type = evsel->core.attr.type,
2257 .config = evsel->core.attr.config,
2258 .disabled = 1,
2259 .sample_type = PERF_SAMPLE_BRANCH_STACK,
2260 .sample_period = 1000,
2261 };
2262 int old_errno;
2263
2264 if (detection_done)
2265 return;
2266
2267 old_errno = errno;
2268
2269 /*
2270 * Must probe features in the order they were added to the
2271 * perf_event_attr interface. These are PMU specific limitation
2272 * so we can detect with the given hardware event and stop on the
2273 * first one succeeded.
2274 */
2275
2276 /* Please add new feature detection here. */
2277
2278 attr.branch_sample_type = PERF_SAMPLE_BRANCH_COUNTERS;
2279 if (has_attr_feature(&attr, /*flags=*/0))
2280 goto found;
2281 perf_missing_features.branch_counters = true;
2282 pr_debug2("switching off branch counters support\n");
2283
2284 attr.branch_sample_type = PERF_SAMPLE_BRANCH_HW_INDEX;
2285 if (has_attr_feature(&attr, /*flags=*/0))
2286 goto found;
2287 perf_missing_features.branch_hw_idx = true;
2288 pr_debug2("switching off branch HW index support\n");
2289
2290 attr.branch_sample_type = PERF_SAMPLE_BRANCH_NO_CYCLES | PERF_SAMPLE_BRANCH_NO_FLAGS;
2291 if (has_attr_feature(&attr, /*flags=*/0))
2292 goto found;
2293 perf_missing_features.lbr_flags = true;
2294 pr_debug2_peo("switching off branch sample type no (cycles/flags)\n");
2295
2296 found:
2297 detection_done = true;
2298 errno = old_errno;
2299 }
2300
evsel__probe_aux_action(struct evsel * evsel,struct perf_cpu cpu)2301 static bool evsel__probe_aux_action(struct evsel *evsel, struct perf_cpu cpu)
2302 {
2303 struct perf_event_attr attr = evsel->core.attr;
2304 int old_errno = errno;
2305
2306 attr.disabled = 1;
2307 attr.aux_start_paused = 1;
2308
2309 if (__has_attr_feature(&attr, cpu, /*flags=*/0)) {
2310 errno = old_errno;
2311 return true;
2312 }
2313
2314 /*
2315 * EOPNOTSUPP means the kernel supports the feature but the PMU does
2316 * not, so keep that distinction if possible.
2317 */
2318 if (errno != EOPNOTSUPP)
2319 errno = old_errno;
2320
2321 return false;
2322 }
2323
evsel__detect_missing_aux_action_feature(struct evsel * evsel,struct perf_cpu cpu)2324 static void evsel__detect_missing_aux_action_feature(struct evsel *evsel, struct perf_cpu cpu)
2325 {
2326 static bool detection_done;
2327 struct evsel *leader;
2328
2329 /*
2330 * Don't bother probing aux_action if it is not being used or has been
2331 * probed before.
2332 */
2333 if (!evsel->core.attr.aux_action || detection_done)
2334 return;
2335
2336 detection_done = true;
2337
2338 /*
2339 * The leader is an AUX area event. If it has failed, assume the feature
2340 * is not supported.
2341 */
2342 leader = evsel__leader(evsel);
2343 if (evsel == leader) {
2344 perf_missing_features.aux_action = true;
2345 return;
2346 }
2347
2348 /*
2349 * AUX area event with aux_action must have been opened successfully
2350 * already, so feature is supported.
2351 */
2352 if (leader->core.attr.aux_action)
2353 return;
2354
2355 if (!evsel__probe_aux_action(leader, cpu))
2356 perf_missing_features.aux_action = true;
2357 }
2358
evsel__detect_missing_features(struct evsel * evsel,struct perf_cpu cpu)2359 static bool evsel__detect_missing_features(struct evsel *evsel, struct perf_cpu cpu)
2360 {
2361 static bool detection_done = false;
2362 struct perf_event_attr attr = {
2363 .type = PERF_TYPE_SOFTWARE,
2364 .config = PERF_COUNT_SW_TASK_CLOCK,
2365 .disabled = 1,
2366 };
2367 int old_errno;
2368
2369 evsel__detect_missing_aux_action_feature(evsel, cpu);
2370
2371 evsel__detect_missing_pmu_features(evsel);
2372
2373 if (evsel__has_br_stack(evsel))
2374 evsel__detect_missing_brstack_features(evsel);
2375
2376 if (detection_done)
2377 goto check;
2378
2379 old_errno = errno;
2380
2381 /*
2382 * Must probe features in the order they were added to the
2383 * perf_event_attr interface. These are kernel core limitation
2384 * not PMU-specific so we can detect with a software event and
2385 * stop on the first one succeeded.
2386 */
2387
2388 /* Please add new feature detection here. */
2389
2390 attr.inherit = true;
2391 attr.sample_type = PERF_SAMPLE_READ;
2392 if (has_attr_feature(&attr, /*flags=*/0))
2393 goto found;
2394 perf_missing_features.inherit_sample_read = true;
2395 pr_debug2("Using PERF_SAMPLE_READ / :S modifier is not compatible with inherit, falling back to no-inherit.\n");
2396 attr.inherit = false;
2397 attr.sample_type = 0;
2398
2399 attr.read_format = PERF_FORMAT_LOST;
2400 if (has_attr_feature(&attr, /*flags=*/0))
2401 goto found;
2402 perf_missing_features.read_lost = true;
2403 pr_debug2("switching off PERF_FORMAT_LOST support\n");
2404 attr.read_format = 0;
2405
2406 attr.sample_type = PERF_SAMPLE_WEIGHT_STRUCT;
2407 if (has_attr_feature(&attr, /*flags=*/0))
2408 goto found;
2409 perf_missing_features.weight_struct = true;
2410 pr_debug2("switching off weight struct support\n");
2411 attr.sample_type = 0;
2412
2413 attr.sample_type = PERF_SAMPLE_CODE_PAGE_SIZE;
2414 if (has_attr_feature(&attr, /*flags=*/0))
2415 goto found;
2416 perf_missing_features.code_page_size = true;
2417 pr_debug2_peo("Kernel has no PERF_SAMPLE_CODE_PAGE_SIZE support\n");
2418 attr.sample_type = 0;
2419
2420 attr.sample_type = PERF_SAMPLE_DATA_PAGE_SIZE;
2421 if (has_attr_feature(&attr, /*flags=*/0))
2422 goto found;
2423 perf_missing_features.data_page_size = true;
2424 pr_debug2_peo("Kernel has no PERF_SAMPLE_DATA_PAGE_SIZE support\n");
2425 attr.sample_type = 0;
2426
2427 attr.cgroup = 1;
2428 if (has_attr_feature(&attr, /*flags=*/0))
2429 goto found;
2430 perf_missing_features.cgroup = true;
2431 pr_debug2_peo("Kernel has no cgroup sampling support\n");
2432 attr.cgroup = 0;
2433
2434 attr.aux_output = 1;
2435 if (has_attr_feature(&attr, /*flags=*/0))
2436 goto found;
2437 perf_missing_features.aux_output = true;
2438 pr_debug2_peo("Kernel has no attr.aux_output support\n");
2439 attr.aux_output = 0;
2440
2441 attr.bpf_event = 1;
2442 if (has_attr_feature(&attr, /*flags=*/0))
2443 goto found;
2444 perf_missing_features.bpf = true;
2445 pr_debug2_peo("switching off bpf_event\n");
2446 attr.bpf_event = 0;
2447
2448 attr.ksymbol = 1;
2449 if (has_attr_feature(&attr, /*flags=*/0))
2450 goto found;
2451 perf_missing_features.ksymbol = true;
2452 pr_debug2_peo("switching off ksymbol\n");
2453 attr.ksymbol = 0;
2454
2455 attr.write_backward = 1;
2456 if (has_attr_feature(&attr, /*flags=*/0))
2457 goto found;
2458 perf_missing_features.write_backward = true;
2459 pr_debug2_peo("switching off write_backward\n");
2460 attr.write_backward = 0;
2461
2462 attr.use_clockid = 1;
2463 attr.clockid = CLOCK_MONOTONIC;
2464 if (has_attr_feature(&attr, /*flags=*/0))
2465 goto found;
2466 perf_missing_features.clockid = true;
2467 pr_debug2_peo("switching off clockid\n");
2468 attr.use_clockid = 0;
2469 attr.clockid = 0;
2470
2471 if (has_attr_feature(&attr, /*flags=*/PERF_FLAG_FD_CLOEXEC))
2472 goto found;
2473 perf_missing_features.cloexec = true;
2474 pr_debug2_peo("switching off cloexec flag\n");
2475
2476 attr.mmap2 = 1;
2477 if (has_attr_feature(&attr, /*flags=*/0))
2478 goto found;
2479 perf_missing_features.mmap2 = true;
2480 pr_debug2_peo("switching off mmap2\n");
2481 attr.mmap2 = 0;
2482
2483 /* set this unconditionally? */
2484 perf_missing_features.sample_id_all = true;
2485 pr_debug2_peo("switching off sample_id_all\n");
2486
2487 attr.inherit = 1;
2488 attr.read_format = PERF_FORMAT_GROUP;
2489 if (has_attr_feature(&attr, /*flags=*/0))
2490 goto found;
2491 perf_missing_features.group_read = true;
2492 pr_debug2_peo("switching off group read\n");
2493 attr.inherit = 0;
2494 attr.read_format = 0;
2495
2496 found:
2497 detection_done = true;
2498 errno = old_errno;
2499
2500 check:
2501 if (evsel->core.attr.inherit &&
2502 (evsel->core.attr.sample_type & PERF_SAMPLE_READ) &&
2503 perf_missing_features.inherit_sample_read)
2504 return true;
2505
2506 if ((evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS) &&
2507 perf_missing_features.branch_counters)
2508 return true;
2509
2510 if ((evsel->core.attr.read_format & PERF_FORMAT_LOST) &&
2511 perf_missing_features.read_lost)
2512 return true;
2513
2514 if ((evsel->core.attr.sample_type & PERF_SAMPLE_WEIGHT_STRUCT) &&
2515 perf_missing_features.weight_struct)
2516 return true;
2517
2518 if (evsel->core.attr.use_clockid && evsel->core.attr.clockid != CLOCK_MONOTONIC &&
2519 !perf_missing_features.clockid) {
2520 perf_missing_features.clockid_wrong = true;
2521 return true;
2522 }
2523
2524 if (evsel->core.attr.use_clockid && perf_missing_features.clockid)
2525 return true;
2526
2527 if ((evsel->open_flags & PERF_FLAG_FD_CLOEXEC) &&
2528 perf_missing_features.cloexec)
2529 return true;
2530
2531 if (evsel->core.attr.mmap2 && perf_missing_features.mmap2)
2532 return true;
2533
2534 if ((evsel->core.attr.branch_sample_type & (PERF_SAMPLE_BRANCH_NO_FLAGS |
2535 PERF_SAMPLE_BRANCH_NO_CYCLES)) &&
2536 perf_missing_features.lbr_flags)
2537 return true;
2538
2539 if (evsel->core.attr.inherit && (evsel->core.attr.read_format & PERF_FORMAT_GROUP) &&
2540 perf_missing_features.group_read)
2541 return true;
2542
2543 if (evsel->core.attr.ksymbol && perf_missing_features.ksymbol)
2544 return true;
2545
2546 if (evsel->core.attr.bpf_event && perf_missing_features.bpf)
2547 return true;
2548
2549 if ((evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX) &&
2550 perf_missing_features.branch_hw_idx)
2551 return true;
2552
2553 if (evsel->core.attr.sample_id_all && perf_missing_features.sample_id_all)
2554 return true;
2555
2556 return false;
2557 }
2558
evsel__open_cpu(struct evsel * evsel,struct perf_cpu_map * cpus,struct perf_thread_map * threads,int start_cpu_map_idx,int end_cpu_map_idx)2559 static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
2560 struct perf_thread_map *threads,
2561 int start_cpu_map_idx, int end_cpu_map_idx)
2562 {
2563 int idx, thread, nthreads;
2564 int pid = -1, err, old_errno;
2565 enum rlimit_action set_rlimit = NO_CHANGE;
2566 struct perf_cpu cpu;
2567
2568 if (evsel__is_retire_lat(evsel))
2569 return tpebs_start(evsel->evlist);
2570
2571 err = __evsel__prepare_open(evsel, cpus, threads);
2572 if (err)
2573 return err;
2574
2575 if (cpus == NULL)
2576 cpus = empty_cpu_map;
2577
2578 if (threads == NULL)
2579 threads = empty_thread_map;
2580
2581 nthreads = perf_thread_map__nr(threads);
2582
2583 if (evsel->cgrp)
2584 pid = evsel->cgrp->fd;
2585
2586 fallback_missing_features:
2587 evsel__disable_missing_features(evsel);
2588
2589 pr_debug3("Opening: %s\n", evsel__name(evsel));
2590 display_attr(&evsel->core.attr);
2591
2592 if (evsel__is_tool(evsel)) {
2593 return evsel__tool_pmu_open(evsel, threads,
2594 start_cpu_map_idx,
2595 end_cpu_map_idx);
2596 }
2597 if (evsel__is_hwmon(evsel)) {
2598 return evsel__hwmon_pmu_open(evsel, threads,
2599 start_cpu_map_idx,
2600 end_cpu_map_idx);
2601 }
2602
2603 for (idx = start_cpu_map_idx; idx < end_cpu_map_idx; idx++) {
2604 cpu = perf_cpu_map__cpu(cpus, idx);
2605
2606 for (thread = 0; thread < nthreads; thread++) {
2607 int fd, group_fd;
2608 retry_open:
2609 if (thread >= nthreads)
2610 break;
2611
2612 if (!evsel->cgrp && !evsel->core.system_wide)
2613 pid = perf_thread_map__pid(threads, thread);
2614
2615 group_fd = get_group_fd(evsel, idx, thread);
2616
2617 if (group_fd == -2) {
2618 pr_debug("broken group leader for %s\n", evsel->name);
2619 err = -EINVAL;
2620 goto out_close;
2621 }
2622
2623 /* Debug message used by test scripts */
2624 pr_debug2_peo("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx",
2625 pid, cpu.cpu, group_fd, evsel->open_flags);
2626
2627 fd = sys_perf_event_open(&evsel->core.attr, pid, cpu.cpu,
2628 group_fd, evsel->open_flags);
2629
2630 FD(evsel, idx, thread) = fd;
2631
2632 if (fd < 0) {
2633 err = -errno;
2634
2635 pr_debug2_peo("\nsys_perf_event_open failed, error %d\n",
2636 err);
2637 goto try_fallback;
2638 }
2639
2640 bpf_counter__install_pe(evsel, idx, fd);
2641
2642 if (unlikely(test_attr__enabled())) {
2643 test_attr__open(&evsel->core.attr, pid, cpu,
2644 fd, group_fd, evsel->open_flags);
2645 }
2646
2647 /* Debug message used by test scripts */
2648 pr_debug2_peo(" = %d\n", fd);
2649
2650 if (evsel->bpf_fd >= 0) {
2651 int evt_fd = fd;
2652 int bpf_fd = evsel->bpf_fd;
2653
2654 err = ioctl(evt_fd,
2655 PERF_EVENT_IOC_SET_BPF,
2656 bpf_fd);
2657 if (err && errno != EEXIST) {
2658 pr_err("failed to attach bpf fd %d: %s\n",
2659 bpf_fd, strerror(errno));
2660 err = -EINVAL;
2661 goto out_close;
2662 }
2663 }
2664
2665 set_rlimit = NO_CHANGE;
2666
2667 /*
2668 * If we succeeded but had to kill clockid, fail and
2669 * have evsel__open_strerror() print us a nice error.
2670 */
2671 if (perf_missing_features.clockid ||
2672 perf_missing_features.clockid_wrong) {
2673 err = -EINVAL;
2674 goto out_close;
2675 }
2676 }
2677 }
2678
2679 return 0;
2680
2681 try_fallback:
2682 if (evsel__ignore_missing_thread(evsel, perf_cpu_map__nr(cpus),
2683 idx, threads, thread, err)) {
2684 /* We just removed 1 thread, so lower the upper nthreads limit. */
2685 nthreads--;
2686
2687 /* ... and pretend like nothing have happened. */
2688 err = 0;
2689 goto retry_open;
2690 }
2691 /*
2692 * perf stat needs between 5 and 22 fds per CPU. When we run out
2693 * of them try to increase the limits.
2694 */
2695 if (err == -EMFILE && rlimit__increase_nofile(&set_rlimit))
2696 goto retry_open;
2697
2698 if (err == -EINVAL && evsel__detect_missing_features(evsel, cpu))
2699 goto fallback_missing_features;
2700
2701 if (evsel__precise_ip_fallback(evsel))
2702 goto retry_open;
2703
2704 out_close:
2705 if (err)
2706 threads->err_thread = thread;
2707
2708 old_errno = errno;
2709 do {
2710 while (--thread >= 0) {
2711 if (FD(evsel, idx, thread) >= 0)
2712 close(FD(evsel, idx, thread));
2713 FD(evsel, idx, thread) = -1;
2714 }
2715 thread = nthreads;
2716 } while (--idx >= 0);
2717 errno = old_errno;
2718 return err;
2719 }
2720
evsel__open(struct evsel * evsel,struct perf_cpu_map * cpus,struct perf_thread_map * threads)2721 int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
2722 struct perf_thread_map *threads)
2723 {
2724 return evsel__open_cpu(evsel, cpus, threads, 0, perf_cpu_map__nr(cpus));
2725 }
2726
evsel__close(struct evsel * evsel)2727 void evsel__close(struct evsel *evsel)
2728 {
2729 if (evsel__is_retire_lat(evsel))
2730 tpebs_delete();
2731 perf_evsel__close(&evsel->core);
2732 perf_evsel__free_id(&evsel->core);
2733 }
2734
evsel__open_per_cpu(struct evsel * evsel,struct perf_cpu_map * cpus,int cpu_map_idx)2735 int evsel__open_per_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, int cpu_map_idx)
2736 {
2737 if (cpu_map_idx == -1)
2738 return evsel__open_cpu(evsel, cpus, NULL, 0, perf_cpu_map__nr(cpus));
2739
2740 return evsel__open_cpu(evsel, cpus, NULL, cpu_map_idx, cpu_map_idx + 1);
2741 }
2742
evsel__open_per_thread(struct evsel * evsel,struct perf_thread_map * threads)2743 int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map *threads)
2744 {
2745 return evsel__open(evsel, NULL, threads);
2746 }
2747
perf_evsel__parse_id_sample(const struct evsel * evsel,const union perf_event * event,struct perf_sample * sample)2748 static int perf_evsel__parse_id_sample(const struct evsel *evsel,
2749 const union perf_event *event,
2750 struct perf_sample *sample)
2751 {
2752 u64 type = evsel->core.attr.sample_type;
2753 const __u64 *array = event->sample.array;
2754 bool swapped = evsel->needs_swap;
2755 union u64_swap u;
2756
2757 array += ((event->header.size -
2758 sizeof(event->header)) / sizeof(u64)) - 1;
2759
2760 if (type & PERF_SAMPLE_IDENTIFIER) {
2761 sample->id = *array;
2762 array--;
2763 }
2764
2765 if (type & PERF_SAMPLE_CPU) {
2766 u.val64 = *array;
2767 if (swapped) {
2768 /* undo swap of u64, then swap on individual u32s */
2769 u.val64 = bswap_64(u.val64);
2770 u.val32[0] = bswap_32(u.val32[0]);
2771 }
2772
2773 sample->cpu = u.val32[0];
2774 array--;
2775 }
2776
2777 if (type & PERF_SAMPLE_STREAM_ID) {
2778 sample->stream_id = *array;
2779 array--;
2780 }
2781
2782 if (type & PERF_SAMPLE_ID) {
2783 sample->id = *array;
2784 array--;
2785 }
2786
2787 if (type & PERF_SAMPLE_TIME) {
2788 sample->time = *array;
2789 array--;
2790 }
2791
2792 if (type & PERF_SAMPLE_TID) {
2793 u.val64 = *array;
2794 if (swapped) {
2795 /* undo swap of u64, then swap on individual u32s */
2796 u.val64 = bswap_64(u.val64);
2797 u.val32[0] = bswap_32(u.val32[0]);
2798 u.val32[1] = bswap_32(u.val32[1]);
2799 }
2800
2801 sample->pid = u.val32[0];
2802 sample->tid = u.val32[1];
2803 array--;
2804 }
2805
2806 return 0;
2807 }
2808
overflow(const void * endp,u16 max_size,const void * offset,u64 size)2809 static inline bool overflow(const void *endp, u16 max_size, const void *offset,
2810 u64 size)
2811 {
2812 return size > max_size || offset + size > endp;
2813 }
2814
2815 #define OVERFLOW_CHECK(offset, size, max_size) \
2816 do { \
2817 if (overflow(endp, (max_size), (offset), (size))) \
2818 return -EFAULT; \
2819 } while (0)
2820
2821 #define OVERFLOW_CHECK_u64(offset) \
2822 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
2823
2824 static int
perf_event__check_size(union perf_event * event,unsigned int sample_size)2825 perf_event__check_size(union perf_event *event, unsigned int sample_size)
2826 {
2827 /*
2828 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
2829 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
2830 * check the format does not go past the end of the event.
2831 */
2832 if (sample_size + sizeof(event->header) > event->header.size)
2833 return -EFAULT;
2834
2835 return 0;
2836 }
2837
arch_perf_parse_sample_weight(struct perf_sample * data,const __u64 * array,u64 type __maybe_unused)2838 void __weak arch_perf_parse_sample_weight(struct perf_sample *data,
2839 const __u64 *array,
2840 u64 type __maybe_unused)
2841 {
2842 data->weight = *array;
2843 }
2844
evsel__bitfield_swap_branch_flags(u64 value)2845 u64 evsel__bitfield_swap_branch_flags(u64 value)
2846 {
2847 u64 new_val = 0;
2848
2849 /*
2850 * branch_flags
2851 * union {
2852 * u64 values;
2853 * struct {
2854 * mispred:1 //target mispredicted
2855 * predicted:1 //target predicted
2856 * in_tx:1 //in transaction
2857 * abort:1 //transaction abort
2858 * cycles:16 //cycle count to last branch
2859 * type:4 //branch type
2860 * spec:2 //branch speculation info
2861 * new_type:4 //additional branch type
2862 * priv:3 //privilege level
2863 * reserved:31
2864 * }
2865 * }
2866 *
2867 * Avoid bswap64() the entire branch_flag.value,
2868 * as it has variable bit-field sizes. Instead the
2869 * macro takes the bit-field position/size,
2870 * swaps it based on the host endianness.
2871 */
2872 if (host_is_bigendian()) {
2873 new_val = bitfield_swap(value, 0, 1);
2874 new_val |= bitfield_swap(value, 1, 1);
2875 new_val |= bitfield_swap(value, 2, 1);
2876 new_val |= bitfield_swap(value, 3, 1);
2877 new_val |= bitfield_swap(value, 4, 16);
2878 new_val |= bitfield_swap(value, 20, 4);
2879 new_val |= bitfield_swap(value, 24, 2);
2880 new_val |= bitfield_swap(value, 26, 4);
2881 new_val |= bitfield_swap(value, 30, 3);
2882 new_val |= bitfield_swap(value, 33, 31);
2883 } else {
2884 new_val = bitfield_swap(value, 63, 1);
2885 new_val |= bitfield_swap(value, 62, 1);
2886 new_val |= bitfield_swap(value, 61, 1);
2887 new_val |= bitfield_swap(value, 60, 1);
2888 new_val |= bitfield_swap(value, 44, 16);
2889 new_val |= bitfield_swap(value, 40, 4);
2890 new_val |= bitfield_swap(value, 38, 2);
2891 new_val |= bitfield_swap(value, 34, 4);
2892 new_val |= bitfield_swap(value, 31, 3);
2893 new_val |= bitfield_swap(value, 0, 31);
2894 }
2895
2896 return new_val;
2897 }
2898
evsel__has_branch_counters(const struct evsel * evsel)2899 static inline bool evsel__has_branch_counters(const struct evsel *evsel)
2900 {
2901 struct evsel *leader = evsel__leader(evsel);
2902
2903 /* The branch counters feature only supports group */
2904 if (!leader || !evsel->evlist)
2905 return false;
2906
2907 if (evsel->evlist->nr_br_cntr < 0)
2908 evlist__update_br_cntr(evsel->evlist);
2909
2910 if (leader->br_cntr_nr > 0)
2911 return true;
2912
2913 return false;
2914 }
2915
evsel__parse_sample(struct evsel * evsel,union perf_event * event,struct perf_sample * data)2916 int evsel__parse_sample(struct evsel *evsel, union perf_event *event,
2917 struct perf_sample *data)
2918 {
2919 u64 type = evsel->core.attr.sample_type;
2920 bool swapped = evsel->needs_swap;
2921 const __u64 *array;
2922 u16 max_size = event->header.size;
2923 const void *endp = (void *)event + max_size;
2924 u64 sz;
2925
2926 /*
2927 * used for cross-endian analysis. See git commit 65014ab3
2928 * for why this goofiness is needed.
2929 */
2930 union u64_swap u;
2931
2932 memset(data, 0, sizeof(*data));
2933 data->cpu = data->pid = data->tid = -1;
2934 data->stream_id = data->id = data->time = -1ULL;
2935 data->period = evsel->core.attr.sample_period;
2936 data->cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
2937 data->misc = event->header.misc;
2938 data->data_src = PERF_MEM_DATA_SRC_NONE;
2939 data->vcpu = -1;
2940
2941 if (event->header.type != PERF_RECORD_SAMPLE) {
2942 if (!evsel->core.attr.sample_id_all)
2943 return 0;
2944 return perf_evsel__parse_id_sample(evsel, event, data);
2945 }
2946
2947 array = event->sample.array;
2948
2949 if (perf_event__check_size(event, evsel->sample_size))
2950 return -EFAULT;
2951
2952 if (type & PERF_SAMPLE_IDENTIFIER) {
2953 data->id = *array;
2954 array++;
2955 }
2956
2957 if (type & PERF_SAMPLE_IP) {
2958 data->ip = *array;
2959 array++;
2960 }
2961
2962 if (type & PERF_SAMPLE_TID) {
2963 u.val64 = *array;
2964 if (swapped) {
2965 /* undo swap of u64, then swap on individual u32s */
2966 u.val64 = bswap_64(u.val64);
2967 u.val32[0] = bswap_32(u.val32[0]);
2968 u.val32[1] = bswap_32(u.val32[1]);
2969 }
2970
2971 data->pid = u.val32[0];
2972 data->tid = u.val32[1];
2973 array++;
2974 }
2975
2976 if (type & PERF_SAMPLE_TIME) {
2977 data->time = *array;
2978 array++;
2979 }
2980
2981 if (type & PERF_SAMPLE_ADDR) {
2982 data->addr = *array;
2983 array++;
2984 }
2985
2986 if (type & PERF_SAMPLE_ID) {
2987 data->id = *array;
2988 array++;
2989 }
2990
2991 if (type & PERF_SAMPLE_STREAM_ID) {
2992 data->stream_id = *array;
2993 array++;
2994 }
2995
2996 if (type & PERF_SAMPLE_CPU) {
2997
2998 u.val64 = *array;
2999 if (swapped) {
3000 /* undo swap of u64, then swap on individual u32s */
3001 u.val64 = bswap_64(u.val64);
3002 u.val32[0] = bswap_32(u.val32[0]);
3003 }
3004
3005 data->cpu = u.val32[0];
3006 array++;
3007 }
3008
3009 if (type & PERF_SAMPLE_PERIOD) {
3010 data->period = *array;
3011 array++;
3012 }
3013
3014 if (type & PERF_SAMPLE_READ) {
3015 u64 read_format = evsel->core.attr.read_format;
3016
3017 OVERFLOW_CHECK_u64(array);
3018 if (read_format & PERF_FORMAT_GROUP)
3019 data->read.group.nr = *array;
3020 else
3021 data->read.one.value = *array;
3022
3023 array++;
3024
3025 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3026 OVERFLOW_CHECK_u64(array);
3027 data->read.time_enabled = *array;
3028 array++;
3029 }
3030
3031 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3032 OVERFLOW_CHECK_u64(array);
3033 data->read.time_running = *array;
3034 array++;
3035 }
3036
3037 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
3038 if (read_format & PERF_FORMAT_GROUP) {
3039 const u64 max_group_nr = UINT64_MAX /
3040 sizeof(struct sample_read_value);
3041
3042 if (data->read.group.nr > max_group_nr)
3043 return -EFAULT;
3044
3045 sz = data->read.group.nr * sample_read_value_size(read_format);
3046 OVERFLOW_CHECK(array, sz, max_size);
3047 data->read.group.values =
3048 (struct sample_read_value *)array;
3049 array = (void *)array + sz;
3050 } else {
3051 OVERFLOW_CHECK_u64(array);
3052 data->read.one.id = *array;
3053 array++;
3054
3055 if (read_format & PERF_FORMAT_LOST) {
3056 OVERFLOW_CHECK_u64(array);
3057 data->read.one.lost = *array;
3058 array++;
3059 }
3060 }
3061 }
3062
3063 if (type & PERF_SAMPLE_CALLCHAIN) {
3064 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
3065
3066 OVERFLOW_CHECK_u64(array);
3067 data->callchain = (struct ip_callchain *)array++;
3068 if (data->callchain->nr > max_callchain_nr)
3069 return -EFAULT;
3070 sz = data->callchain->nr * sizeof(u64);
3071 OVERFLOW_CHECK(array, sz, max_size);
3072 array = (void *)array + sz;
3073 }
3074
3075 if (type & PERF_SAMPLE_RAW) {
3076 OVERFLOW_CHECK_u64(array);
3077 u.val64 = *array;
3078
3079 /*
3080 * Undo swap of u64, then swap on individual u32s,
3081 * get the size of the raw area and undo all of the
3082 * swap. The pevent interface handles endianness by
3083 * itself.
3084 */
3085 if (swapped) {
3086 u.val64 = bswap_64(u.val64);
3087 u.val32[0] = bswap_32(u.val32[0]);
3088 u.val32[1] = bswap_32(u.val32[1]);
3089 }
3090 data->raw_size = u.val32[0];
3091
3092 /*
3093 * The raw data is aligned on 64bits including the
3094 * u32 size, so it's safe to use mem_bswap_64.
3095 */
3096 if (swapped)
3097 mem_bswap_64((void *) array, data->raw_size);
3098
3099 array = (void *)array + sizeof(u32);
3100
3101 OVERFLOW_CHECK(array, data->raw_size, max_size);
3102 data->raw_data = (void *)array;
3103 array = (void *)array + data->raw_size;
3104 }
3105
3106 if (type & PERF_SAMPLE_BRANCH_STACK) {
3107 const u64 max_branch_nr = UINT64_MAX /
3108 sizeof(struct branch_entry);
3109 struct branch_entry *e;
3110 unsigned int i;
3111
3112 OVERFLOW_CHECK_u64(array);
3113 data->branch_stack = (struct branch_stack *)array++;
3114
3115 if (data->branch_stack->nr > max_branch_nr)
3116 return -EFAULT;
3117
3118 sz = data->branch_stack->nr * sizeof(struct branch_entry);
3119 if (evsel__has_branch_hw_idx(evsel)) {
3120 sz += sizeof(u64);
3121 e = &data->branch_stack->entries[0];
3122 } else {
3123 data->no_hw_idx = true;
3124 /*
3125 * if the PERF_SAMPLE_BRANCH_HW_INDEX is not applied,
3126 * only nr and entries[] will be output by kernel.
3127 */
3128 e = (struct branch_entry *)&data->branch_stack->hw_idx;
3129 }
3130
3131 if (swapped) {
3132 /*
3133 * struct branch_flag does not have endian
3134 * specific bit field definition. And bswap
3135 * will not resolve the issue, since these
3136 * are bit fields.
3137 *
3138 * evsel__bitfield_swap_branch_flags() uses a
3139 * bitfield_swap macro to swap the bit position
3140 * based on the host endians.
3141 */
3142 for (i = 0; i < data->branch_stack->nr; i++, e++)
3143 e->flags.value = evsel__bitfield_swap_branch_flags(e->flags.value);
3144 }
3145
3146 OVERFLOW_CHECK(array, sz, max_size);
3147 array = (void *)array + sz;
3148
3149 if (evsel__has_branch_counters(evsel)) {
3150 data->branch_stack_cntr = (u64 *)array;
3151 sz = data->branch_stack->nr * sizeof(u64);
3152
3153 OVERFLOW_CHECK(array, sz, max_size);
3154 array = (void *)array + sz;
3155 }
3156 }
3157
3158 if (type & PERF_SAMPLE_REGS_USER) {
3159 OVERFLOW_CHECK_u64(array);
3160 data->user_regs.abi = *array;
3161 array++;
3162
3163 if (data->user_regs.abi) {
3164 u64 mask = evsel->core.attr.sample_regs_user;
3165
3166 sz = hweight64(mask) * sizeof(u64);
3167 OVERFLOW_CHECK(array, sz, max_size);
3168 data->user_regs.mask = mask;
3169 data->user_regs.regs = (u64 *)array;
3170 array = (void *)array + sz;
3171 }
3172 }
3173
3174 if (type & PERF_SAMPLE_STACK_USER) {
3175 OVERFLOW_CHECK_u64(array);
3176 sz = *array++;
3177
3178 data->user_stack.offset = ((char *)(array - 1)
3179 - (char *) event);
3180
3181 if (!sz) {
3182 data->user_stack.size = 0;
3183 } else {
3184 OVERFLOW_CHECK(array, sz, max_size);
3185 data->user_stack.data = (char *)array;
3186 array = (void *)array + sz;
3187 OVERFLOW_CHECK_u64(array);
3188 data->user_stack.size = *array++;
3189 if (WARN_ONCE(data->user_stack.size > sz,
3190 "user stack dump failure\n"))
3191 return -EFAULT;
3192 }
3193 }
3194
3195 if (type & PERF_SAMPLE_WEIGHT_TYPE) {
3196 OVERFLOW_CHECK_u64(array);
3197 arch_perf_parse_sample_weight(data, array, type);
3198 array++;
3199 }
3200
3201 if (type & PERF_SAMPLE_DATA_SRC) {
3202 OVERFLOW_CHECK_u64(array);
3203 data->data_src = *array;
3204 array++;
3205 }
3206
3207 if (type & PERF_SAMPLE_TRANSACTION) {
3208 OVERFLOW_CHECK_u64(array);
3209 data->transaction = *array;
3210 array++;
3211 }
3212
3213 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
3214 if (type & PERF_SAMPLE_REGS_INTR) {
3215 OVERFLOW_CHECK_u64(array);
3216 data->intr_regs.abi = *array;
3217 array++;
3218
3219 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
3220 u64 mask = evsel->core.attr.sample_regs_intr;
3221
3222 sz = hweight64(mask) * sizeof(u64);
3223 OVERFLOW_CHECK(array, sz, max_size);
3224 data->intr_regs.mask = mask;
3225 data->intr_regs.regs = (u64 *)array;
3226 array = (void *)array + sz;
3227 }
3228 }
3229
3230 data->phys_addr = 0;
3231 if (type & PERF_SAMPLE_PHYS_ADDR) {
3232 data->phys_addr = *array;
3233 array++;
3234 }
3235
3236 data->cgroup = 0;
3237 if (type & PERF_SAMPLE_CGROUP) {
3238 data->cgroup = *array;
3239 array++;
3240 }
3241
3242 data->data_page_size = 0;
3243 if (type & PERF_SAMPLE_DATA_PAGE_SIZE) {
3244 data->data_page_size = *array;
3245 array++;
3246 }
3247
3248 data->code_page_size = 0;
3249 if (type & PERF_SAMPLE_CODE_PAGE_SIZE) {
3250 data->code_page_size = *array;
3251 array++;
3252 }
3253
3254 if (type & PERF_SAMPLE_AUX) {
3255 OVERFLOW_CHECK_u64(array);
3256 sz = *array++;
3257
3258 OVERFLOW_CHECK(array, sz, max_size);
3259 /* Undo swap of data */
3260 if (swapped)
3261 mem_bswap_64((char *)array, sz);
3262 data->aux_sample.size = sz;
3263 data->aux_sample.data = (char *)array;
3264 array = (void *)array + sz;
3265 }
3266
3267 return 0;
3268 }
3269
evsel__parse_sample_timestamp(struct evsel * evsel,union perf_event * event,u64 * timestamp)3270 int evsel__parse_sample_timestamp(struct evsel *evsel, union perf_event *event,
3271 u64 *timestamp)
3272 {
3273 u64 type = evsel->core.attr.sample_type;
3274 const __u64 *array;
3275
3276 if (!(type & PERF_SAMPLE_TIME))
3277 return -1;
3278
3279 if (event->header.type != PERF_RECORD_SAMPLE) {
3280 struct perf_sample data = {
3281 .time = -1ULL,
3282 };
3283
3284 if (!evsel->core.attr.sample_id_all)
3285 return -1;
3286 if (perf_evsel__parse_id_sample(evsel, event, &data))
3287 return -1;
3288
3289 *timestamp = data.time;
3290 return 0;
3291 }
3292
3293 array = event->sample.array;
3294
3295 if (perf_event__check_size(event, evsel->sample_size))
3296 return -EFAULT;
3297
3298 if (type & PERF_SAMPLE_IDENTIFIER)
3299 array++;
3300
3301 if (type & PERF_SAMPLE_IP)
3302 array++;
3303
3304 if (type & PERF_SAMPLE_TID)
3305 array++;
3306
3307 if (type & PERF_SAMPLE_TIME)
3308 *timestamp = *array;
3309
3310 return 0;
3311 }
3312
evsel__id_hdr_size(const struct evsel * evsel)3313 u16 evsel__id_hdr_size(const struct evsel *evsel)
3314 {
3315 u64 sample_type = evsel->core.attr.sample_type;
3316 u16 size = 0;
3317
3318 if (sample_type & PERF_SAMPLE_TID)
3319 size += sizeof(u64);
3320
3321 if (sample_type & PERF_SAMPLE_TIME)
3322 size += sizeof(u64);
3323
3324 if (sample_type & PERF_SAMPLE_ID)
3325 size += sizeof(u64);
3326
3327 if (sample_type & PERF_SAMPLE_STREAM_ID)
3328 size += sizeof(u64);
3329
3330 if (sample_type & PERF_SAMPLE_CPU)
3331 size += sizeof(u64);
3332
3333 if (sample_type & PERF_SAMPLE_IDENTIFIER)
3334 size += sizeof(u64);
3335
3336 return size;
3337 }
3338
3339 #ifdef HAVE_LIBTRACEEVENT
evsel__field(struct evsel * evsel,const char * name)3340 struct tep_format_field *evsel__field(struct evsel *evsel, const char *name)
3341 {
3342 struct tep_event *tp_format = evsel__tp_format(evsel);
3343
3344 return tp_format ? tep_find_field(tp_format, name) : NULL;
3345 }
3346
evsel__common_field(struct evsel * evsel,const char * name)3347 struct tep_format_field *evsel__common_field(struct evsel *evsel, const char *name)
3348 {
3349 struct tep_event *tp_format = evsel__tp_format(evsel);
3350
3351 return tp_format ? tep_find_common_field(tp_format, name) : NULL;
3352 }
3353
evsel__rawptr(struct evsel * evsel,struct perf_sample * sample,const char * name)3354 void *evsel__rawptr(struct evsel *evsel, struct perf_sample *sample, const char *name)
3355 {
3356 struct tep_format_field *field = evsel__field(evsel, name);
3357 int offset;
3358
3359 if (!field)
3360 return NULL;
3361
3362 offset = field->offset;
3363
3364 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
3365 offset = *(int *)(sample->raw_data + field->offset);
3366 offset &= 0xffff;
3367 if (tep_field_is_relative(field->flags))
3368 offset += field->offset + field->size;
3369 }
3370
3371 return sample->raw_data + offset;
3372 }
3373
format_field__intval(struct tep_format_field * field,struct perf_sample * sample,bool needs_swap)3374 u64 format_field__intval(struct tep_format_field *field, struct perf_sample *sample,
3375 bool needs_swap)
3376 {
3377 u64 value;
3378 void *ptr = sample->raw_data + field->offset;
3379
3380 switch (field->size) {
3381 case 1:
3382 return *(u8 *)ptr;
3383 case 2:
3384 value = *(u16 *)ptr;
3385 break;
3386 case 4:
3387 value = *(u32 *)ptr;
3388 break;
3389 case 8:
3390 memcpy(&value, ptr, sizeof(u64));
3391 break;
3392 default:
3393 return 0;
3394 }
3395
3396 if (!needs_swap)
3397 return value;
3398
3399 switch (field->size) {
3400 case 2:
3401 return bswap_16(value);
3402 case 4:
3403 return bswap_32(value);
3404 case 8:
3405 return bswap_64(value);
3406 default:
3407 return 0;
3408 }
3409
3410 return 0;
3411 }
3412
evsel__intval(struct evsel * evsel,struct perf_sample * sample,const char * name)3413 u64 evsel__intval(struct evsel *evsel, struct perf_sample *sample, const char *name)
3414 {
3415 struct tep_format_field *field = evsel__field(evsel, name);
3416
3417 return field ? format_field__intval(field, sample, evsel->needs_swap) : 0;
3418 }
3419
evsel__intval_common(struct evsel * evsel,struct perf_sample * sample,const char * name)3420 u64 evsel__intval_common(struct evsel *evsel, struct perf_sample *sample, const char *name)
3421 {
3422 struct tep_format_field *field = evsel__common_field(evsel, name);
3423
3424 return field ? format_field__intval(field, sample, evsel->needs_swap) : 0;
3425 }
3426
evsel__taskstate(struct evsel * evsel,struct perf_sample * sample,const char * name)3427 char evsel__taskstate(struct evsel *evsel, struct perf_sample *sample, const char *name)
3428 {
3429 static struct tep_format_field *prev_state_field;
3430 static const char *states;
3431 struct tep_format_field *field;
3432 unsigned long long val;
3433 unsigned int bit;
3434 char state = '?'; /* '?' denotes unknown task state */
3435
3436 field = evsel__field(evsel, name);
3437
3438 if (!field)
3439 return state;
3440
3441 if (!states || field != prev_state_field) {
3442 states = parse_task_states(field);
3443 if (!states)
3444 return state;
3445 prev_state_field = field;
3446 }
3447
3448 /*
3449 * Note since the kernel exposes TASK_REPORT_MAX to userspace
3450 * to denote the 'preempted' state, we might as welll report
3451 * 'R' for this case, which make senses to users as well.
3452 *
3453 * We can change this if we have a good reason in the future.
3454 */
3455 val = evsel__intval(evsel, sample, name);
3456 bit = val ? ffs(val) : 0;
3457 state = (!bit || bit > strlen(states)) ? 'R' : states[bit-1];
3458 return state;
3459 }
3460 #endif
3461
evsel__fallback(struct evsel * evsel,struct target * target,int err,char * msg,size_t msgsize)3462 bool evsel__fallback(struct evsel *evsel, struct target *target, int err,
3463 char *msg, size_t msgsize)
3464 {
3465 int paranoid;
3466
3467 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
3468 evsel->core.attr.type == PERF_TYPE_HARDWARE &&
3469 evsel->core.attr.config == PERF_COUNT_HW_CPU_CYCLES) {
3470 /*
3471 * If it's cycles then fall back to hrtimer based cpu-clock sw
3472 * counter, which is always available even if no PMU support.
3473 *
3474 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
3475 * b0a873e).
3476 */
3477 evsel->core.attr.type = PERF_TYPE_SOFTWARE;
3478 evsel->core.attr.config = target__has_cpu(target)
3479 ? PERF_COUNT_SW_CPU_CLOCK
3480 : PERF_COUNT_SW_TASK_CLOCK;
3481 scnprintf(msg, msgsize,
3482 "The cycles event is not supported, trying to fall back to %s",
3483 target__has_cpu(target) ? "cpu-clock" : "task-clock");
3484
3485 zfree(&evsel->name);
3486 return true;
3487 } else if (err == EACCES && !evsel->core.attr.exclude_kernel &&
3488 (paranoid = perf_event_paranoid()) > 1) {
3489 const char *name = evsel__name(evsel);
3490 char *new_name;
3491 const char *sep = ":";
3492
3493 /* If event has exclude user then don't exclude kernel. */
3494 if (evsel->core.attr.exclude_user)
3495 return false;
3496
3497 /* Is there already the separator in the name. */
3498 if (strchr(name, '/') ||
3499 (strchr(name, ':') && !evsel->is_libpfm_event))
3500 sep = "";
3501
3502 if (asprintf(&new_name, "%s%su", name, sep) < 0)
3503 return false;
3504
3505 free(evsel->name);
3506 evsel->name = new_name;
3507 scnprintf(msg, msgsize, "kernel.perf_event_paranoid=%d, trying "
3508 "to fall back to excluding kernel and hypervisor "
3509 " samples", paranoid);
3510 evsel->core.attr.exclude_kernel = 1;
3511 evsel->core.attr.exclude_hv = 1;
3512
3513 return true;
3514 } else if (err == EOPNOTSUPP && !evsel->core.attr.exclude_guest &&
3515 !evsel->exclude_GH) {
3516 const char *name = evsel__name(evsel);
3517 char *new_name;
3518 const char *sep = ":";
3519
3520 /* Is there already the separator in the name. */
3521 if (strchr(name, '/') ||
3522 (strchr(name, ':') && !evsel->is_libpfm_event))
3523 sep = "";
3524
3525 if (asprintf(&new_name, "%s%sH", name, sep) < 0)
3526 return false;
3527
3528 free(evsel->name);
3529 evsel->name = new_name;
3530 /* Apple M1 requires exclude_guest */
3531 scnprintf(msg, msgsize, "trying to fall back to excluding guest samples");
3532 evsel->core.attr.exclude_guest = 1;
3533
3534 return true;
3535 }
3536
3537 return false;
3538 }
3539
find_process(const char * name)3540 static bool find_process(const char *name)
3541 {
3542 size_t len = strlen(name);
3543 DIR *dir;
3544 struct dirent *d;
3545 int ret = -1;
3546
3547 dir = opendir(procfs__mountpoint());
3548 if (!dir)
3549 return false;
3550
3551 /* Walk through the directory. */
3552 while (ret && (d = readdir(dir)) != NULL) {
3553 char path[PATH_MAX];
3554 char *data;
3555 size_t size;
3556
3557 if ((d->d_type != DT_DIR) ||
3558 !strcmp(".", d->d_name) ||
3559 !strcmp("..", d->d_name))
3560 continue;
3561
3562 scnprintf(path, sizeof(path), "%s/%s/comm",
3563 procfs__mountpoint(), d->d_name);
3564
3565 if (filename__read_str(path, &data, &size))
3566 continue;
3567
3568 ret = strncmp(name, data, len);
3569 free(data);
3570 }
3571
3572 closedir(dir);
3573 return ret ? false : true;
3574 }
3575
dump_perf_event_processes(char * msg,size_t size)3576 static int dump_perf_event_processes(char *msg, size_t size)
3577 {
3578 DIR *proc_dir;
3579 struct dirent *proc_entry;
3580 int printed = 0;
3581
3582 proc_dir = opendir(procfs__mountpoint());
3583 if (!proc_dir)
3584 return 0;
3585
3586 /* Walk through the /proc directory. */
3587 while ((proc_entry = readdir(proc_dir)) != NULL) {
3588 char buf[256];
3589 DIR *fd_dir;
3590 struct dirent *fd_entry;
3591 int fd_dir_fd;
3592
3593 if (proc_entry->d_type != DT_DIR ||
3594 !isdigit(proc_entry->d_name[0]) ||
3595 strlen(proc_entry->d_name) > sizeof(buf) - 4)
3596 continue;
3597
3598 scnprintf(buf, sizeof(buf), "%s/fd", proc_entry->d_name);
3599 fd_dir_fd = openat(dirfd(proc_dir), buf, O_DIRECTORY);
3600 if (fd_dir_fd == -1)
3601 continue;
3602 fd_dir = fdopendir(fd_dir_fd);
3603 if (!fd_dir) {
3604 close(fd_dir_fd);
3605 continue;
3606 }
3607 while ((fd_entry = readdir(fd_dir)) != NULL) {
3608 ssize_t link_size;
3609
3610 if (fd_entry->d_type != DT_LNK)
3611 continue;
3612 link_size = readlinkat(fd_dir_fd, fd_entry->d_name, buf, sizeof(buf));
3613 if (link_size < 0)
3614 continue;
3615 /* Take care as readlink doesn't null terminate the string. */
3616 if (!strncmp(buf, "anon_inode:[perf_event]", link_size)) {
3617 int cmdline_fd;
3618 ssize_t cmdline_size;
3619
3620 scnprintf(buf, sizeof(buf), "%s/cmdline", proc_entry->d_name);
3621 cmdline_fd = openat(dirfd(proc_dir), buf, O_RDONLY);
3622 if (cmdline_fd == -1)
3623 continue;
3624 cmdline_size = read(cmdline_fd, buf, sizeof(buf) - 1);
3625 close(cmdline_fd);
3626 if (cmdline_size < 0)
3627 continue;
3628 buf[cmdline_size] = '\0';
3629 for (ssize_t i = 0; i < cmdline_size; i++) {
3630 if (buf[i] == '\0')
3631 buf[i] = ' ';
3632 }
3633
3634 if (printed == 0)
3635 printed += scnprintf(msg, size, "Possible processes:\n");
3636
3637 printed += scnprintf(msg + printed, size - printed,
3638 "%s %s\n", proc_entry->d_name, buf);
3639 break;
3640 }
3641 }
3642 closedir(fd_dir);
3643 }
3644 closedir(proc_dir);
3645 return printed;
3646 }
3647
arch_evsel__open_strerror(struct evsel * evsel __maybe_unused,char * msg __maybe_unused,size_t size __maybe_unused)3648 int __weak arch_evsel__open_strerror(struct evsel *evsel __maybe_unused,
3649 char *msg __maybe_unused,
3650 size_t size __maybe_unused)
3651 {
3652 return 0;
3653 }
3654
evsel__open_strerror(struct evsel * evsel,struct target * target,int err,char * msg,size_t size)3655 int evsel__open_strerror(struct evsel *evsel, struct target *target,
3656 int err, char *msg, size_t size)
3657 {
3658 char sbuf[STRERR_BUFSIZE];
3659 int printed = 0, enforced = 0;
3660 int ret;
3661
3662 switch (err) {
3663 case EPERM:
3664 case EACCES:
3665 printed += scnprintf(msg + printed, size - printed,
3666 "Access to performance monitoring and observability operations is limited.\n");
3667
3668 if (!sysfs__read_int("fs/selinux/enforce", &enforced)) {
3669 if (enforced) {
3670 printed += scnprintf(msg + printed, size - printed,
3671 "Enforced MAC policy settings (SELinux) can limit access to performance\n"
3672 "monitoring and observability operations. Inspect system audit records for\n"
3673 "more perf_event access control information and adjusting the policy.\n");
3674 }
3675 }
3676
3677 if (err == EPERM)
3678 printed += scnprintf(msg, size,
3679 "No permission to enable %s event.\n\n", evsel__name(evsel));
3680
3681 return printed + scnprintf(msg + printed, size - printed,
3682 "Consider adjusting /proc/sys/kernel/perf_event_paranoid setting to open\n"
3683 "access to performance monitoring and observability operations for processes\n"
3684 "without CAP_PERFMON, CAP_SYS_PTRACE or CAP_SYS_ADMIN Linux capability.\n"
3685 "More information can be found at 'Perf events and tool security' document:\n"
3686 "https://www.kernel.org/doc/html/latest/admin-guide/perf-security.html\n"
3687 "perf_event_paranoid setting is %d:\n"
3688 " -1: Allow use of (almost) all events by all users\n"
3689 " Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK\n"
3690 ">= 0: Disallow raw and ftrace function tracepoint access\n"
3691 ">= 1: Disallow CPU event access\n"
3692 ">= 2: Disallow kernel profiling\n"
3693 "To make the adjusted perf_event_paranoid setting permanent preserve it\n"
3694 "in /etc/sysctl.conf (e.g. kernel.perf_event_paranoid = <setting>)",
3695 perf_event_paranoid());
3696 case ENOENT:
3697 return scnprintf(msg, size, "The %s event is not supported.", evsel__name(evsel));
3698 case EMFILE:
3699 return scnprintf(msg, size, "%s",
3700 "Too many events are opened.\n"
3701 "Probably the maximum number of open file descriptors has been reached.\n"
3702 "Hint: Try again after reducing the number of events.\n"
3703 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
3704 case ENOMEM:
3705 if (evsel__has_callchain(evsel) &&
3706 access("/proc/sys/kernel/perf_event_max_stack", F_OK) == 0)
3707 return scnprintf(msg, size,
3708 "Not enough memory to setup event with callchain.\n"
3709 "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n"
3710 "Hint: Current value: %d", sysctl__max_stack());
3711 break;
3712 case ENODEV:
3713 if (target->cpu_list)
3714 return scnprintf(msg, size, "%s",
3715 "No such device - did you specify an out-of-range profile CPU?");
3716 break;
3717 case EOPNOTSUPP:
3718 if (evsel->core.attr.sample_type & PERF_SAMPLE_BRANCH_STACK)
3719 return scnprintf(msg, size,
3720 "%s: PMU Hardware or event type doesn't support branch stack sampling.",
3721 evsel__name(evsel));
3722 if (evsel->core.attr.aux_output)
3723 return scnprintf(msg, size,
3724 "%s: PMU Hardware doesn't support 'aux_output' feature",
3725 evsel__name(evsel));
3726 if (evsel->core.attr.aux_action)
3727 return scnprintf(msg, size,
3728 "%s: PMU Hardware doesn't support 'aux_action' feature",
3729 evsel__name(evsel));
3730 if (evsel->core.attr.sample_period != 0)
3731 return scnprintf(msg, size,
3732 "%s: PMU Hardware doesn't support sampling/overflow-interrupts. Try 'perf stat'",
3733 evsel__name(evsel));
3734 if (evsel->core.attr.precise_ip)
3735 return scnprintf(msg, size, "%s",
3736 "\'precise\' request may not be supported. Try removing 'p' modifier.");
3737 #if defined(__i386__) || defined(__x86_64__)
3738 if (evsel->core.attr.type == PERF_TYPE_HARDWARE)
3739 return scnprintf(msg, size, "%s",
3740 "No hardware sampling interrupt available.\n");
3741 #endif
3742 break;
3743 case EBUSY:
3744 if (find_process("oprofiled"))
3745 return scnprintf(msg, size,
3746 "The PMU counters are busy/taken by another profiler.\n"
3747 "We found oprofile daemon running, please stop it and try again.");
3748 printed += scnprintf(
3749 msg, size,
3750 "The PMU %s counters are busy and in use by another process.\n",
3751 evsel->pmu ? evsel->pmu->name : "");
3752 return printed + dump_perf_event_processes(msg + printed, size - printed);
3753 break;
3754 case EINVAL:
3755 if (evsel->core.attr.sample_type & PERF_SAMPLE_CODE_PAGE_SIZE && perf_missing_features.code_page_size)
3756 return scnprintf(msg, size, "Asking for the code page size isn't supported by this kernel.");
3757 if (evsel->core.attr.sample_type & PERF_SAMPLE_DATA_PAGE_SIZE && perf_missing_features.data_page_size)
3758 return scnprintf(msg, size, "Asking for the data page size isn't supported by this kernel.");
3759 if (evsel->core.attr.write_backward && perf_missing_features.write_backward)
3760 return scnprintf(msg, size, "Reading from overwrite event is not supported by this kernel.");
3761 if (perf_missing_features.clockid)
3762 return scnprintf(msg, size, "clockid feature not supported.");
3763 if (perf_missing_features.clockid_wrong)
3764 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
3765 if (perf_missing_features.aux_action)
3766 return scnprintf(msg, size, "The 'aux_action' feature is not supported, update the kernel.");
3767 if (perf_missing_features.aux_output)
3768 return scnprintf(msg, size, "The 'aux_output' feature is not supported, update the kernel.");
3769 if (!target__has_cpu(target))
3770 return scnprintf(msg, size,
3771 "Invalid event (%s) in per-thread mode, enable system wide with '-a'.",
3772 evsel__name(evsel));
3773
3774 break;
3775 case ENODATA:
3776 return scnprintf(msg, size, "Cannot collect data source with the load latency event alone. "
3777 "Please add an auxiliary event in front of the load latency event.");
3778 default:
3779 break;
3780 }
3781
3782 ret = arch_evsel__open_strerror(evsel, msg, size);
3783 if (ret)
3784 return ret;
3785
3786 return scnprintf(msg, size,
3787 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
3788 "\"dmesg | grep -i perf\" may provide additional information.\n",
3789 err, str_error_r(err, sbuf, sizeof(sbuf)), evsel__name(evsel));
3790 }
3791
evsel__env(struct evsel * evsel)3792 struct perf_env *evsel__env(struct evsel *evsel)
3793 {
3794 if (evsel && evsel->evlist && evsel->evlist->env)
3795 return evsel->evlist->env;
3796 return &perf_env;
3797 }
3798
store_evsel_ids(struct evsel * evsel,struct evlist * evlist)3799 static int store_evsel_ids(struct evsel *evsel, struct evlist *evlist)
3800 {
3801 int cpu_map_idx, thread;
3802
3803 if (evsel__is_retire_lat(evsel))
3804 return 0;
3805
3806 for (cpu_map_idx = 0; cpu_map_idx < xyarray__max_x(evsel->core.fd); cpu_map_idx++) {
3807 for (thread = 0; thread < xyarray__max_y(evsel->core.fd);
3808 thread++) {
3809 int fd = FD(evsel, cpu_map_idx, thread);
3810
3811 if (perf_evlist__id_add_fd(&evlist->core, &evsel->core,
3812 cpu_map_idx, thread, fd) < 0)
3813 return -1;
3814 }
3815 }
3816
3817 return 0;
3818 }
3819
evsel__store_ids(struct evsel * evsel,struct evlist * evlist)3820 int evsel__store_ids(struct evsel *evsel, struct evlist *evlist)
3821 {
3822 struct perf_cpu_map *cpus = evsel->core.cpus;
3823 struct perf_thread_map *threads = evsel->core.threads;
3824
3825 if (perf_evsel__alloc_id(&evsel->core, perf_cpu_map__nr(cpus), threads->nr))
3826 return -ENOMEM;
3827
3828 return store_evsel_ids(evsel, evlist);
3829 }
3830
evsel__zero_per_pkg(struct evsel * evsel)3831 void evsel__zero_per_pkg(struct evsel *evsel)
3832 {
3833 struct hashmap_entry *cur;
3834 size_t bkt;
3835
3836 if (evsel->per_pkg_mask) {
3837 hashmap__for_each_entry(evsel->per_pkg_mask, cur, bkt)
3838 zfree(&cur->pkey);
3839
3840 hashmap__clear(evsel->per_pkg_mask);
3841 }
3842 }
3843
3844 /**
3845 * evsel__is_hybrid - does the evsel have a known PMU that is hybrid. Note, this
3846 * will be false on hybrid systems for hardware and legacy
3847 * cache events.
3848 */
evsel__is_hybrid(const struct evsel * evsel)3849 bool evsel__is_hybrid(const struct evsel *evsel)
3850 {
3851 if (perf_pmus__num_core_pmus() == 1)
3852 return false;
3853
3854 return evsel->core.is_pmu_core;
3855 }
3856
evsel__leader(const struct evsel * evsel)3857 struct evsel *evsel__leader(const struct evsel *evsel)
3858 {
3859 return container_of(evsel->core.leader, struct evsel, core);
3860 }
3861
evsel__has_leader(struct evsel * evsel,struct evsel * leader)3862 bool evsel__has_leader(struct evsel *evsel, struct evsel *leader)
3863 {
3864 return evsel->core.leader == &leader->core;
3865 }
3866
evsel__is_leader(struct evsel * evsel)3867 bool evsel__is_leader(struct evsel *evsel)
3868 {
3869 return evsel__has_leader(evsel, evsel);
3870 }
3871
evsel__set_leader(struct evsel * evsel,struct evsel * leader)3872 void evsel__set_leader(struct evsel *evsel, struct evsel *leader)
3873 {
3874 evsel->core.leader = &leader->core;
3875 }
3876
evsel__source_count(const struct evsel * evsel)3877 int evsel__source_count(const struct evsel *evsel)
3878 {
3879 struct evsel *pos;
3880 int count = 0;
3881
3882 evlist__for_each_entry(evsel->evlist, pos) {
3883 if (pos->metric_leader == evsel)
3884 count++;
3885 }
3886 return count;
3887 }
3888
arch_evsel__must_be_in_group(const struct evsel * evsel __maybe_unused)3889 bool __weak arch_evsel__must_be_in_group(const struct evsel *evsel __maybe_unused)
3890 {
3891 return false;
3892 }
3893
3894 /*
3895 * Remove an event from a given group (leader).
3896 * Some events, e.g., perf metrics Topdown events,
3897 * must always be grouped. Ignore the events.
3898 */
evsel__remove_from_group(struct evsel * evsel,struct evsel * leader)3899 void evsel__remove_from_group(struct evsel *evsel, struct evsel *leader)
3900 {
3901 if (!arch_evsel__must_be_in_group(evsel) && evsel != leader) {
3902 evsel__set_leader(evsel, evsel);
3903 evsel->core.nr_members = 0;
3904 leader->core.nr_members--;
3905 }
3906 }
3907