blob: 1bf76864c4f26cc61036b5c0f57eda2fd42b5ede [file] [log] [blame]
Thomas Gleixner91007042019-05-29 07:12:25 -07001// SPDX-License-Identifier: GPL-2.0-only
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02002/*
3 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4 *
5 * Parts came from builtin-{top,stat,record}.c, see those files for further
6 * copyright notes.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02007 */
8
David Ahern936be502011-09-06 09:12:26 -06009#include <byteswap.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030010#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030011#include <inttypes.h>
Jiri Olsa0f6a3012012-08-07 15:20:45 +020012#include <linux/bitops.h>
Arnaldo Carvalho de Melo2157f6e2017-06-20 12:05:38 -030013#include <api/fs/fs.h>
Jiri Olsa4605eab2015-09-02 09:56:43 +020014#include <api/fs/tracing_path.h>
Robert Richter4e319022013-06-11 17:29:18 +020015#include <traceevent/event-parse.h>
16#include <linux/hw_breakpoint.h>
17#include <linux/perf_event.h>
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -030018#include <linux/compiler.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +020019#include <linux/err.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030020#include <linux/zalloc.h>
Arnaldo Carvalho de Melo86a5e0c2017-04-19 19:03:14 -030021#include <sys/ioctl.h>
Andi Kleenbec19672013-08-04 19:41:26 -070022#include <sys/resource.h>
Arnaldo Carvalho de Melo2157f6e2017-06-20 12:05:38 -030023#include <sys/types.h>
24#include <dirent.h>
Arnaldo Carvalho de Melof2a39fe2019-08-30 14:45:20 -030025#include <stdlib.h>
Jiri Olsab04c5972019-07-21 13:24:24 +020026#include <perf/evsel.h>
Robert Richter4e319022013-06-11 17:29:18 +020027#include "asm/bug.h"
Song Liufa853c42020-12-29 13:42:14 -080028#include "bpf_counter.h"
Arnaldo Carvalho de Melo8f651ea2014-10-09 16:12:24 -030029#include "callchain.h"
Arnaldo Carvalho de Melof14d5702014-10-17 12:17:40 -030030#include "cgroup.h"
Arnaldo Carvalho de Meloddee6882019-08-21 14:20:54 -030031#include "counts.h"
Arnaldo Carvalho de Melo5ab8c682017-04-25 15:30:47 -030032#include "event.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020033#include "evsel.h"
Arnaldo Carvalho de Melo9db0e362019-09-30 11:48:32 -030034#include "util/env.h"
Arnaldo Carvalho de Melo95be9d12019-09-24 15:56:14 -030035#include "util/evsel_config.h"
Arnaldo Carvalho de Meloca125272019-09-24 15:41:51 -030036#include "util/evsel_fprintf.h"
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -020037#include "evlist.h"
Arnaldo Carvalho de Melo87ffb6c2019-09-10 16:29:02 +010038#include <perf/cpumap.h>
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020039#include "thread_map.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090040#include "target.h"
Jiri Olsa26d33022012-08-07 15:20:47 +020041#include "perf_regs.h"
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -030042#include "record.h"
Adrian Huntere3e1a542013-08-14 15:48:24 +030043#include "debug.h"
Jiri Olsa97978b32013-12-03 14:09:24 +010044#include "trace-event.h"
Jiri Olsaa9a3a4d2015-06-14 10:19:26 +020045#include "stat.h"
Arnaldo Carvalho de Melo6a9fa4e2019-06-25 17:31:26 -030046#include "string2.h"
Jiri Olsaf9d8adb2017-11-29 19:43:46 +010047#include "memswap.h"
Arnaldo Carvalho de Melo2da39f12019-08-27 11:51:18 -030048#include "util.h"
Arnaldo Carvalho de Melo91854f92019-08-29 14:59:50 -030049#include "../perf-sys.h"
Andi Kleenac12f672016-10-12 14:02:06 -070050#include "util/parse-branch-options.h"
Arnaldo Carvalho de Melo76466022019-08-21 11:30:29 -030051#include <internal/xyarray.h>
Arnaldo Carvalho de Melofb71c86c2019-09-03 10:56:06 -030052#include <internal/lib.h>
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020053
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030054#include <linux/ctype.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030055
Arnaldo Carvalho de Melo9a831b32018-02-02 11:27:25 -030056struct perf_missing_features perf_missing_features;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030057
Peter Zijlstra814c8c32015-03-31 00:19:31 +020058static clockid_t clockid;
59
Arnaldo Carvalho de Melo4c703822020-05-06 12:55:06 -030060static int evsel__no_extra_init(struct evsel *evsel __maybe_unused)
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -030061{
62 return 0;
63}
64
Jiri Olsa10213e22017-07-03 16:50:18 +020065void __weak test_attr__ready(void) { }
66
Arnaldo Carvalho de Melo4c703822020-05-06 12:55:06 -030067static void evsel__no_extra_fini(struct evsel *evsel __maybe_unused)
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -030068{
69}
70
71static struct {
72 size_t size;
Jiri Olsa32dcd022019-07-21 13:23:51 +020073 int (*init)(struct evsel *evsel);
74 void (*fini)(struct evsel *evsel);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -030075} perf_evsel__object = {
Jiri Olsa32dcd022019-07-21 13:23:51 +020076 .size = sizeof(struct evsel),
Arnaldo Carvalho de Melo4c703822020-05-06 12:55:06 -030077 .init = evsel__no_extra_init,
78 .fini = evsel__no_extra_fini,
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -030079};
80
Arnaldo Carvalho de Melo4c703822020-05-06 12:55:06 -030081int evsel__object_config(size_t object_size, int (*init)(struct evsel *evsel),
82 void (*fini)(struct evsel *evsel))
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -030083{
84
85 if (object_size == 0)
86 goto set_methods;
87
88 if (perf_evsel__object.size > object_size)
89 return -EINVAL;
90
91 perf_evsel__object.size = object_size;
92
93set_methods:
94 if (init != NULL)
95 perf_evsel__object.init = init;
96
97 if (fini != NULL)
98 perf_evsel__object.fini = fini;
99
100 return 0;
101}
102
Jiri Olsa9dfcb752019-07-21 13:24:45 +0200103#define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200104
Arnaldo Carvalho de Melo2aaefde2020-04-29 16:00:27 -0300105int __evsel__sample_size(u64 sample_type)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300106{
107 u64 mask = sample_type & PERF_SAMPLE_MASK;
108 int size = 0;
109 int i;
110
111 for (i = 0; i < 64; i++) {
112 if (mask & (1ULL << i))
113 size++;
114 }
115
116 size *= sizeof(u64);
117
118 return size;
119}
120
Adrian Hunter75562572013-08-27 11:23:09 +0300121/**
122 * __perf_evsel__calc_id_pos - calculate id_pos.
123 * @sample_type: sample type
124 *
125 * This function returns the position of the event id (PERF_SAMPLE_ID or
126 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300127 * perf_record_sample.
Adrian Hunter75562572013-08-27 11:23:09 +0300128 */
129static int __perf_evsel__calc_id_pos(u64 sample_type)
130{
131 int idx = 0;
132
133 if (sample_type & PERF_SAMPLE_IDENTIFIER)
134 return 0;
135
136 if (!(sample_type & PERF_SAMPLE_ID))
137 return -1;
138
139 if (sample_type & PERF_SAMPLE_IP)
140 idx += 1;
141
142 if (sample_type & PERF_SAMPLE_TID)
143 idx += 1;
144
145 if (sample_type & PERF_SAMPLE_TIME)
146 idx += 1;
147
148 if (sample_type & PERF_SAMPLE_ADDR)
149 idx += 1;
150
151 return idx;
152}
153
154/**
155 * __perf_evsel__calc_is_pos - calculate is_pos.
156 * @sample_type: sample type
157 *
158 * This function returns the position (counting backwards) of the event id
159 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
160 * sample_id_all is used there is an id sample appended to non-sample events.
161 */
162static int __perf_evsel__calc_is_pos(u64 sample_type)
163{
164 int idx = 1;
165
166 if (sample_type & PERF_SAMPLE_IDENTIFIER)
167 return 1;
168
169 if (!(sample_type & PERF_SAMPLE_ID))
170 return -1;
171
172 if (sample_type & PERF_SAMPLE_CPU)
173 idx += 1;
174
175 if (sample_type & PERF_SAMPLE_STREAM_ID)
176 idx += 1;
177
178 return idx;
179}
180
Arnaldo Carvalho de Melo4b5e87b2020-04-29 15:58:40 -0300181void evsel__calc_id_pos(struct evsel *evsel)
Adrian Hunter75562572013-08-27 11:23:09 +0300182{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200183 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->core.attr.sample_type);
184 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->core.attr.sample_type);
Adrian Hunter75562572013-08-27 11:23:09 +0300185}
186
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -0300187void __evsel__set_sample_bit(struct evsel *evsel,
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300188 enum perf_event_sample_format bit)
189{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200190 if (!(evsel->core.attr.sample_type & bit)) {
191 evsel->core.attr.sample_type |= bit;
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300192 evsel->sample_size += sizeof(u64);
Arnaldo Carvalho de Melo4b5e87b2020-04-29 15:58:40 -0300193 evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300194 }
195}
196
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -0300197void __evsel__reset_sample_bit(struct evsel *evsel,
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300198 enum perf_event_sample_format bit)
199{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200200 if (evsel->core.attr.sample_type & bit) {
201 evsel->core.attr.sample_type &= ~bit;
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300202 evsel->sample_size -= sizeof(u64);
Arnaldo Carvalho de Melo4b5e87b2020-04-29 15:58:40 -0300203 evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300204 }
205}
206
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -0300207void evsel__set_sample_id(struct evsel *evsel,
Adrian Hunter75562572013-08-27 11:23:09 +0300208 bool can_sample_identifier)
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300209{
Adrian Hunter75562572013-08-27 11:23:09 +0300210 if (can_sample_identifier) {
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -0300211 evsel__reset_sample_bit(evsel, ID);
212 evsel__set_sample_bit(evsel, IDENTIFIER);
Adrian Hunter75562572013-08-27 11:23:09 +0300213 } else {
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -0300214 evsel__set_sample_bit(evsel, ID);
Adrian Hunter75562572013-08-27 11:23:09 +0300215 }
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200216 evsel->core.attr.read_format |= PERF_FORMAT_ID;
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300217}
218
Arnaldo Carvalho de Melo5496bc02016-07-07 11:51:47 -0300219/**
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -0300220 * evsel__is_function_event - Return whether given evsel is a function
Arnaldo Carvalho de Melo5496bc02016-07-07 11:51:47 -0300221 * trace event
222 *
223 * @evsel - evsel selector to be tested
224 *
225 * Return %true if event is function trace event
226 */
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -0300227bool evsel__is_function_event(struct evsel *evsel)
Arnaldo Carvalho de Melo5496bc02016-07-07 11:51:47 -0300228{
229#define FUNCTION_EVENT "ftrace:function"
230
231 return evsel->name &&
232 !strncmp(FUNCTION_EVENT, evsel->name, sizeof(FUNCTION_EVENT));
233
234#undef FUNCTION_EVENT
235}
236
Jiri Olsab4b62ee2019-07-21 13:23:53 +0200237void evsel__init(struct evsel *evsel,
238 struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200239{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200240 perf_evsel__init(&evsel->core, attr);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200241 evsel->idx = idx;
Adrian Hunter60b08962014-07-31 09:00:52 +0300242 evsel->tracking = !idx;
Namhyung Kim2cfda562012-11-29 15:38:29 +0900243 evsel->leader = evsel;
Stephane Eranian410136f2013-11-12 17:58:49 +0100244 evsel->unit = "";
245 evsel->scale = 1.0;
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -0300246 evsel->max_events = ULONG_MAX;
Arnaldo Carvalho de Melod49e4692015-08-27 08:07:40 -0400247 evsel->evlist = NULL;
Arnaldo Carvalho de Meloaf4a0992019-07-15 16:22:57 -0300248 evsel->bpf_obj = NULL;
Wang Nan1f45b1d2015-10-14 12:41:18 +0000249 evsel->bpf_fd = -1;
Jiri Olsa930a2e22015-07-29 05:42:10 -0400250 INIT_LIST_HEAD(&evsel->config_terms);
Song Liufa853c42020-12-29 13:42:14 -0800251 INIT_LIST_HEAD(&evsel->bpf_counter_list);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300252 perf_evsel__object.init(evsel);
Arnaldo Carvalho de Melo2aaefde2020-04-29 16:00:27 -0300253 evsel->sample_size = __evsel__sample_size(attr->sample_type);
Arnaldo Carvalho de Melo4b5e87b2020-04-29 15:58:40 -0300254 evsel__calc_id_pos(evsel);
Wang Nan15bfd2c2015-07-10 07:36:09 +0000255 evsel->cmdline_group_boundary = false;
Andi Kleen37932c12017-03-20 13:17:08 -0700256 evsel->metric_expr = NULL;
Andi Kleen96284812017-03-20 13:17:10 -0700257 evsel->metric_name = NULL;
Andi Kleen37932c12017-03-20 13:17:08 -0700258 evsel->metric_events = NULL;
Arnaldo Carvalho de Melof0aef472020-05-13 11:00:04 -0300259 evsel->per_pkg_mask = NULL;
Andi Kleen37932c12017-03-20 13:17:08 -0700260 evsel->collect_stat = false;
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -0500261 evsel->pmu_name = NULL;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200262}
263
Arnaldo Carvalho de Melo8f6725a2020-05-06 13:27:04 -0300264struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200265{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200266 struct evsel *evsel = zalloc(perf_evsel__object.size);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200267
Hisao Tanabefd8d2702018-08-25 00:45:56 +0900268 if (!evsel)
269 return NULL;
Jiri Olsab4b62ee2019-07-21 13:23:53 +0200270 evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200271
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -0300272 if (evsel__is_bpf_output(evsel)) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200273 evsel->core.attr.sample_type |= (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
Wang Nand37ba882016-04-01 13:26:42 +0000274 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200275 evsel->core.attr.sample_period = 1;
Wang Nan03e0a7d2016-02-22 09:10:37 +0000276 }
277
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -0300278 if (evsel__is_clock(evsel)) {
Jiri Olsa0aa802a2018-07-20 13:00:34 +0200279 /*
280 * The evsel->unit points to static alias->unit
281 * so it's ok to use static string in here.
282 */
283 static const char *unit = "msec";
284
285 evsel->unit = unit;
286 evsel->scale = 1e-6;
287 }
288
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200289 return evsel;
290}
291
Arnaldo Carvalho de Melof1e52f12017-09-22 15:41:44 -0300292static bool perf_event_can_profile_kernel(void)
293{
Igor Lubashevaa972932019-08-26 21:39:14 -0400294 return perf_event_paranoid_check(1);
Arnaldo Carvalho de Melof1e52f12017-09-22 15:41:44 -0300295}
296
Arnaldo Carvalho de Melo8f6725a2020-05-06 13:27:04 -0300297struct evsel *evsel__new_cycles(bool precise)
Arnaldo Carvalho de Melo7c48dcf2016-07-28 18:33:20 -0300298{
299 struct perf_event_attr attr = {
300 .type = PERF_TYPE_HARDWARE,
301 .config = PERF_COUNT_HW_CPU_CYCLES,
Arnaldo Carvalho de Melof1e52f12017-09-22 15:41:44 -0300302 .exclude_kernel = !perf_event_can_profile_kernel(),
Arnaldo Carvalho de Melo7c48dcf2016-07-28 18:33:20 -0300303 };
Jiri Olsa32dcd022019-07-21 13:23:51 +0200304 struct evsel *evsel;
Arnaldo Carvalho de Melo7c48dcf2016-07-28 18:33:20 -0300305
306 event_attr_init(&attr);
Arnaldo Carvalho de Melo30269dc2017-07-03 13:05:43 -0300307
308 if (!precise)
309 goto new_event;
Arnaldo Carvalho de Melo7c48dcf2016-07-28 18:33:20 -0300310
Arnaldo Carvalho de Melo7a1ac112017-06-09 16:54:28 -0300311 /*
312 * Now let the usual logic to set up the perf_event_attr defaults
313 * to kick in when we return and before perf_evsel__open() is called.
314 */
Arnaldo Carvalho de Melo30269dc2017-07-03 13:05:43 -0300315new_event:
Jiri Olsa365c3ae2019-07-21 13:23:58 +0200316 evsel = evsel__new(&attr);
Arnaldo Carvalho de Melo7c48dcf2016-07-28 18:33:20 -0300317 if (evsel == NULL)
318 goto out;
319
Jiri Olsa4e8a5c12019-03-14 15:00:10 +0100320 evsel->precise_max = true;
321
Arnaldo Carvalho de Melo7c48dcf2016-07-28 18:33:20 -0300322 /* use asprintf() because free(evsel) assumes name is allocated */
Arnaldo Carvalho de Meloede56262017-07-10 16:19:25 -0300323 if (asprintf(&evsel->name, "cycles%s%s%.*s",
324 (attr.precise_ip || attr.exclude_kernel) ? ":" : "",
325 attr.exclude_kernel ? "u" : "",
326 attr.precise_ip ? attr.precise_ip + 1 : 0, "ppp") < 0)
Arnaldo Carvalho de Melo7c48dcf2016-07-28 18:33:20 -0300327 goto error_free;
328out:
329 return evsel;
330error_free:
Jiri Olsa5eb2dd22019-07-21 13:23:57 +0200331 evsel__delete(evsel);
Arnaldo Carvalho de Melo7c48dcf2016-07-28 18:33:20 -0300332 evsel = NULL;
333 goto out;
334}
335
Namhyung Kim7fedd9b2020-09-24 21:44:51 +0900336static int evsel__copy_config_terms(struct evsel *dst, struct evsel *src)
337{
338 struct evsel_config_term *pos, *tmp;
339
340 list_for_each_entry(pos, &src->config_terms, list) {
341 tmp = malloc(sizeof(*tmp));
342 if (tmp == NULL)
343 return -ENOMEM;
344
345 *tmp = *pos;
346 if (tmp->free_str) {
347 tmp->val.str = strdup(pos->val.str);
348 if (tmp->val.str == NULL) {
349 free(tmp);
350 return -ENOMEM;
351 }
352 }
353 list_add_tail(&tmp->list, &dst->config_terms);
354 }
355 return 0;
356}
357
358/**
359 * evsel__clone - create a new evsel copied from @orig
360 * @orig: original evsel
361 *
362 * The assumption is that @orig is not configured nor opened yet.
363 * So we only care about the attributes that can be set while it's parsed.
364 */
365struct evsel *evsel__clone(struct evsel *orig)
366{
367 struct evsel *evsel;
368
369 BUG_ON(orig->core.fd);
370 BUG_ON(orig->counts);
371 BUG_ON(orig->priv);
372 BUG_ON(orig->per_pkg_mask);
373
374 /* cannot handle BPF objects for now */
375 if (orig->bpf_obj)
376 return NULL;
377
378 evsel = evsel__new(&orig->core.attr);
379 if (evsel == NULL)
380 return NULL;
381
382 evsel->core.cpus = perf_cpu_map__get(orig->core.cpus);
383 evsel->core.own_cpus = perf_cpu_map__get(orig->core.own_cpus);
384 evsel->core.threads = perf_thread_map__get(orig->core.threads);
385 evsel->core.nr_members = orig->core.nr_members;
386 evsel->core.system_wide = orig->core.system_wide;
387
388 if (orig->name) {
389 evsel->name = strdup(orig->name);
390 if (evsel->name == NULL)
391 goto out_err;
392 }
393 if (orig->group_name) {
394 evsel->group_name = strdup(orig->group_name);
395 if (evsel->group_name == NULL)
396 goto out_err;
397 }
398 if (orig->pmu_name) {
399 evsel->pmu_name = strdup(orig->pmu_name);
400 if (evsel->pmu_name == NULL)
401 goto out_err;
402 }
403 if (orig->filter) {
404 evsel->filter = strdup(orig->filter);
405 if (evsel->filter == NULL)
406 goto out_err;
407 }
408 evsel->cgrp = cgroup__get(orig->cgrp);
409 evsel->tp_format = orig->tp_format;
410 evsel->handler = orig->handler;
411 evsel->leader = orig->leader;
412
413 evsel->max_events = orig->max_events;
414 evsel->tool_event = orig->tool_event;
415 evsel->unit = orig->unit;
416 evsel->scale = orig->scale;
417 evsel->snapshot = orig->snapshot;
418 evsel->per_pkg = orig->per_pkg;
419 evsel->percore = orig->percore;
420 evsel->precise_max = orig->precise_max;
421 evsel->use_uncore_alias = orig->use_uncore_alias;
422 evsel->is_libpfm_event = orig->is_libpfm_event;
423
424 evsel->exclude_GH = orig->exclude_GH;
425 evsel->sample_read = orig->sample_read;
426 evsel->auto_merge_stats = orig->auto_merge_stats;
427 evsel->collect_stat = orig->collect_stat;
428 evsel->weak_group = orig->weak_group;
429
430 if (evsel__copy_config_terms(evsel, orig) < 0)
431 goto out_err;
432
433 return evsel;
434
435out_err:
436 evsel__delete(evsel);
437 return NULL;
438}
439
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200440/*
441 * Returns pointer with encoded error via <linux/err.h> interface.
442 */
Arnaldo Carvalho de Melo8f6725a2020-05-06 13:27:04 -0300443struct evsel *evsel__newtp_idx(const char *sys, const char *name, int idx)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300444{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200445 struct evsel *evsel = zalloc(perf_evsel__object.size);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200446 int err = -ENOMEM;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300447
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200448 if (evsel == NULL) {
449 goto out_err;
450 } else {
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300451 struct perf_event_attr attr = {
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300452 .type = PERF_TYPE_TRACEPOINT,
453 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
454 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300455 };
456
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300457 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
458 goto out_free;
459
Jiri Olsa97978b32013-12-03 14:09:24 +0100460 evsel->tp_format = trace_event__tp_format(sys, name);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200461 if (IS_ERR(evsel->tp_format)) {
462 err = PTR_ERR(evsel->tp_format);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300463 goto out_free;
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200464 }
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300465
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300466 event_attr_init(&attr);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300467 attr.config = evsel->tp_format->id;
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300468 attr.sample_period = 1;
Jiri Olsab4b62ee2019-07-21 13:23:53 +0200469 evsel__init(evsel, &attr, idx);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300470 }
471
472 return evsel;
473
474out_free:
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300475 zfree(&evsel->name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300476 free(evsel);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200477out_err:
478 return ERR_PTR(err);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300479}
480
Arnaldo Carvalho de Meloc64e85e2020-05-06 13:32:23 -0300481const char *evsel__hw_names[PERF_COUNT_HW_MAX] = {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300482 "cycles",
483 "instructions",
484 "cache-references",
485 "cache-misses",
486 "branches",
487 "branch-misses",
488 "bus-cycles",
489 "stalled-cycles-frontend",
490 "stalled-cycles-backend",
491 "ref-cycles",
492};
493
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300494static const char *__evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300495{
Arnaldo Carvalho de Meloc64e85e2020-05-06 13:32:23 -0300496 if (config < PERF_COUNT_HW_MAX && evsel__hw_names[config])
497 return evsel__hw_names[config];
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300498
499 return "unknown-hardware";
500}
501
Arnaldo Carvalho de Melo56933022020-11-30 09:08:24 -0300502static int evsel__add_modifiers(struct evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300503{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300504 int colon = 0, r = 0;
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200505 struct perf_event_attr *attr = &evsel->core.attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300506 bool exclude_guest_default = false;
507
508#define MOD_PRINT(context, mod) do { \
509 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300510 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300511 r += scnprintf(bf + r, size - r, "%c", mod); \
512 } } while(0)
513
514 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
515 MOD_PRINT(kernel, 'k');
516 MOD_PRINT(user, 'u');
517 MOD_PRINT(hv, 'h');
518 exclude_guest_default = true;
519 }
520
521 if (attr->precise_ip) {
522 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300523 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300524 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
525 exclude_guest_default = true;
526 }
527
528 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
529 MOD_PRINT(host, 'H');
530 MOD_PRINT(guest, 'G');
531 }
532#undef MOD_PRINT
533 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300534 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300535 return r;
536}
537
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300538static int evsel__hw_name(struct evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300539{
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300540 int r = scnprintf(bf, size, "%s", __evsel__hw_name(evsel->core.attr.config));
Arnaldo Carvalho de Melo56933022020-11-30 09:08:24 -0300541 return r + evsel__add_modifiers(evsel, bf + r, size - r);
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300542}
543
Arnaldo Carvalho de Meloc64e85e2020-05-06 13:32:23 -0300544const char *evsel__sw_names[PERF_COUNT_SW_MAX] = {
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300545 "cpu-clock",
546 "task-clock",
547 "page-faults",
548 "context-switches",
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300549 "cpu-migrations",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300550 "minor-faults",
551 "major-faults",
552 "alignment-faults",
553 "emulation-faults",
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300554 "dummy",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300555};
556
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300557static const char *__evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300558{
Arnaldo Carvalho de Meloc64e85e2020-05-06 13:32:23 -0300559 if (config < PERF_COUNT_SW_MAX && evsel__sw_names[config])
560 return evsel__sw_names[config];
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300561 return "unknown-software";
562}
563
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300564static int evsel__sw_name(struct evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300565{
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300566 int r = scnprintf(bf, size, "%s", __evsel__sw_name(evsel->core.attr.config));
Arnaldo Carvalho de Melo56933022020-11-30 09:08:24 -0300567 return r + evsel__add_modifiers(evsel, bf + r, size - r);
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300568}
569
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300570static int __evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
Jiri Olsa287e74a2012-06-28 23:18:49 +0200571{
572 int r;
573
574 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
575
576 if (type & HW_BREAKPOINT_R)
577 r += scnprintf(bf + r, size - r, "r");
578
579 if (type & HW_BREAKPOINT_W)
580 r += scnprintf(bf + r, size - r, "w");
581
582 if (type & HW_BREAKPOINT_X)
583 r += scnprintf(bf + r, size - r, "x");
584
585 return r;
586}
587
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300588static int evsel__bp_name(struct evsel *evsel, char *bf, size_t size)
Jiri Olsa287e74a2012-06-28 23:18:49 +0200589{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200590 struct perf_event_attr *attr = &evsel->core.attr;
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300591 int r = __evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
Arnaldo Carvalho de Melo56933022020-11-30 09:08:24 -0300592 return r + evsel__add_modifiers(evsel, bf + r, size - r);
Jiri Olsa287e74a2012-06-28 23:18:49 +0200593}
594
Arnaldo Carvalho de Meloc64e85e2020-05-06 13:32:23 -0300595const char *evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX][EVSEL__MAX_ALIASES] = {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300596 { "L1-dcache", "l1-d", "l1d", "L1-data", },
597 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
598 { "LLC", "L2", },
599 { "dTLB", "d-tlb", "Data-TLB", },
600 { "iTLB", "i-tlb", "Instruction-TLB", },
601 { "branch", "branches", "bpu", "btb", "bpc", },
602 { "node", },
603};
604
Arnaldo Carvalho de Meloc64e85e2020-05-06 13:32:23 -0300605const char *evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX][EVSEL__MAX_ALIASES] = {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300606 { "load", "loads", "read", },
607 { "store", "stores", "write", },
608 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
609};
610
Arnaldo Carvalho de Meloc64e85e2020-05-06 13:32:23 -0300611const char *evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX][EVSEL__MAX_ALIASES] = {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300612 { "refs", "Reference", "ops", "access", },
613 { "misses", "miss", },
614};
615
616#define C(x) PERF_COUNT_HW_CACHE_##x
617#define CACHE_READ (1 << C(OP_READ))
618#define CACHE_WRITE (1 << C(OP_WRITE))
619#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
620#define COP(x) (1 << x)
621
622/*
623 * cache operartion stat
624 * L1I : Read and prefetch only
625 * ITLB and BPU : Read-only
626 */
Arnaldo Carvalho de Meloc64e85e2020-05-06 13:32:23 -0300627static unsigned long evsel__hw_cache_stat[C(MAX)] = {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300628 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
629 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
630 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
631 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
632 [C(ITLB)] = (CACHE_READ),
633 [C(BPU)] = (CACHE_READ),
634 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
635};
636
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -0300637bool evsel__is_cache_op_valid(u8 type, u8 op)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300638{
Arnaldo Carvalho de Meloc64e85e2020-05-06 13:32:23 -0300639 if (evsel__hw_cache_stat[type] & COP(op))
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300640 return true; /* valid */
641 else
642 return false; /* invalid */
643}
644
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300645int __evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300646{
647 if (result) {
Arnaldo Carvalho de Meloc64e85e2020-05-06 13:32:23 -0300648 return scnprintf(bf, size, "%s-%s-%s", evsel__hw_cache[type][0],
649 evsel__hw_cache_op[op][0],
650 evsel__hw_cache_result[result][0]);
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300651 }
652
Arnaldo Carvalho de Meloc64e85e2020-05-06 13:32:23 -0300653 return scnprintf(bf, size, "%s-%s", evsel__hw_cache[type][0],
654 evsel__hw_cache_op[op][1]);
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300655}
656
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300657static int __evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300658{
659 u8 op, result, type = (config >> 0) & 0xff;
660 const char *err = "unknown-ext-hardware-cache-type";
661
Arnaldo Carvalho de Meloc53412e2016-08-18 16:30:28 -0300662 if (type >= PERF_COUNT_HW_CACHE_MAX)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300663 goto out_err;
664
665 op = (config >> 8) & 0xff;
666 err = "unknown-ext-hardware-cache-op";
Arnaldo Carvalho de Meloc53412e2016-08-18 16:30:28 -0300667 if (op >= PERF_COUNT_HW_CACHE_OP_MAX)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300668 goto out_err;
669
670 result = (config >> 16) & 0xff;
671 err = "unknown-ext-hardware-cache-result";
Arnaldo Carvalho de Meloc53412e2016-08-18 16:30:28 -0300672 if (result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300673 goto out_err;
674
675 err = "invalid-cache";
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -0300676 if (!evsel__is_cache_op_valid(type, op))
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300677 goto out_err;
678
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300679 return __evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300680out_err:
681 return scnprintf(bf, size, "%s", err);
682}
683
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300684static int evsel__hw_cache_name(struct evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300685{
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300686 int ret = __evsel__hw_cache_name(evsel->core.attr.config, bf, size);
Arnaldo Carvalho de Melo56933022020-11-30 09:08:24 -0300687 return ret + evsel__add_modifiers(evsel, bf + ret, size - ret);
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300688}
689
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300690static int evsel__raw_name(struct evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300691{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200692 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->core.attr.config);
Arnaldo Carvalho de Melo56933022020-11-30 09:08:24 -0300693 return ret + evsel__add_modifiers(evsel, bf + ret, size - ret);
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300694}
695
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300696static int evsel__tool_name(char *bf, size_t size)
Andi Kleen3371f382019-03-26 15:18:22 -0700697{
698 int ret = scnprintf(bf, size, "duration_time");
699 return ret;
700}
701
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300702const char *evsel__name(struct evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300703{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300704 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300705
Arnaldo Carvalho de Melofdbdd7e2019-06-17 14:32:53 -0300706 if (!evsel)
707 goto out_unknown;
708
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300709 if (evsel->name)
710 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300711
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200712 switch (evsel->core.attr.type) {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300713 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300714 evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300715 break;
716
717 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300718 evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300719 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300720
721 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300722 evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300723 break;
724
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300725 case PERF_TYPE_SOFTWARE:
Andi Kleen3371f382019-03-26 15:18:22 -0700726 if (evsel->tool_event)
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300727 evsel__tool_name(bf, sizeof(bf));
Andi Kleen3371f382019-03-26 15:18:22 -0700728 else
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300729 evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300730 break;
731
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300732 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300733 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300734 break;
735
Jiri Olsa287e74a2012-06-28 23:18:49 +0200736 case PERF_TYPE_BREAKPOINT:
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300737 evsel__bp_name(evsel, bf, sizeof(bf));
Jiri Olsa287e74a2012-06-28 23:18:49 +0200738 break;
739
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300740 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200741 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200742 evsel->core.attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300743 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300744 }
745
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300746 evsel->name = strdup(bf);
747
Arnaldo Carvalho de Melofdbdd7e2019-06-17 14:32:53 -0300748 if (evsel->name)
749 return evsel->name;
750out_unknown:
751 return "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300752}
753
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300754const char *evsel__group_name(struct evsel *evsel)
Namhyung Kim717e2632013-01-22 18:09:44 +0900755{
756 return evsel->group_name ?: "anon group";
757}
758
Jiri Olsa8ef278b2018-03-07 16:50:02 +0100759/*
760 * Returns the group details for the specified leader,
761 * with following rules.
762 *
763 * For record -e '{cycles,instructions}'
764 * 'anon group { cycles:u, instructions:u }'
765 *
766 * For record -e 'cycles,instructions' and report --group
767 * 'cycles:u, instructions:u'
768 */
Arnaldo Carvalho de Melo347c7512020-04-29 16:09:12 -0300769int evsel__group_desc(struct evsel *evsel, char *buf, size_t size)
Namhyung Kim717e2632013-01-22 18:09:44 +0900770{
Jiri Olsa8ef278b2018-03-07 16:50:02 +0100771 int ret = 0;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200772 struct evsel *pos;
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300773 const char *group_name = evsel__group_name(evsel);
Namhyung Kim717e2632013-01-22 18:09:44 +0900774
Jiri Olsa8ef278b2018-03-07 16:50:02 +0100775 if (!evsel->forced_leader)
776 ret = scnprintf(buf, size, "%s { ", group_name);
Namhyung Kim717e2632013-01-22 18:09:44 +0900777
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300778 ret += scnprintf(buf + ret, size - ret, "%s", evsel__name(evsel));
Namhyung Kim717e2632013-01-22 18:09:44 +0900779
780 for_each_group_member(pos, evsel)
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300781 ret += scnprintf(buf + ret, size - ret, ", %s", evsel__name(pos));
Namhyung Kim717e2632013-01-22 18:09:44 +0900782
Jiri Olsa8ef278b2018-03-07 16:50:02 +0100783 if (!evsel->forced_leader)
784 ret += scnprintf(buf + ret, size - ret, " }");
Namhyung Kim717e2632013-01-22 18:09:44 +0900785
786 return ret;
787}
788
Arnaldo Carvalho de Melo6ec17b42020-04-29 15:57:01 -0300789static void __evsel__config_callchain(struct evsel *evsel, struct record_opts *opts,
790 struct callchain_param *param)
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100791{
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -0300792 bool function = evsel__is_function_event(evsel);
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200793 struct perf_event_attr *attr = &evsel->core.attr;
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100794
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -0300795 evsel__set_sample_bit(evsel, CALLCHAIN);
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100796
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300797 attr->sample_max_stack = param->max_stack;
798
yuzhoujian53651b22019-05-30 14:29:22 +0100799 if (opts->kernel_callchains)
800 attr->exclude_callchain_user = 1;
801 if (opts->user_callchains)
802 attr->exclude_callchain_kernel = 1;
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400803 if (param->record_mode == CALLCHAIN_LBR) {
Kan Liangaad2b212015-01-05 13:23:04 -0500804 if (!opts->branch_stack) {
805 if (attr->exclude_user) {
806 pr_warning("LBR callstack option is only available "
807 "to get user callchain information. "
808 "Falling back to framepointers.\n");
809 } else {
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -0300810 evsel__set_sample_bit(evsel, BRANCH_STACK);
Kan Liangaad2b212015-01-05 13:23:04 -0500811 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
Andi Kleenbd0f8892015-12-11 16:12:24 -0800812 PERF_SAMPLE_BRANCH_CALL_STACK |
813 PERF_SAMPLE_BRANCH_NO_CYCLES |
Kan Liangd3f85432020-02-28 08:30:01 -0800814 PERF_SAMPLE_BRANCH_NO_FLAGS |
815 PERF_SAMPLE_BRANCH_HW_INDEX;
Kan Liangaad2b212015-01-05 13:23:04 -0500816 }
817 } else
818 pr_warning("Cannot use LBR callstack with branch stack. "
819 "Falling back to framepointers.\n");
820 }
821
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400822 if (param->record_mode == CALLCHAIN_DWARF) {
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100823 if (!function) {
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -0300824 evsel__set_sample_bit(evsel, REGS_USER);
825 evsel__set_sample_bit(evsel, STACK_USER);
Alexey Budankovd194d8f2019-05-30 22:03:36 +0300826 if (opts->sample_user_regs && DWARF_MINIMAL_REGS != PERF_REGS_MASK) {
827 attr->sample_regs_user |= DWARF_MINIMAL_REGS;
828 pr_warning("WARNING: The use of --call-graph=dwarf may require all the user registers, "
829 "specifying a subset with --user-regs may render DWARF unwinding unreliable, "
830 "so the minimal registers set (IP, SP) is explicitly forced.\n");
831 } else {
832 attr->sample_regs_user |= PERF_REGS_MASK;
833 }
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400834 attr->sample_stack_user = param->dump_size;
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100835 attr->exclude_callchain_user = 1;
836 } else {
837 pr_info("Cannot use DWARF unwind for function trace event,"
838 " falling back to framepointers.\n");
839 }
840 }
841
842 if (function) {
843 pr_info("Disabling user space callchains for function trace event.\n");
844 attr->exclude_callchain_user = 1;
845 }
846}
847
Arnaldo Carvalho de Melo6ec17b42020-04-29 15:57:01 -0300848void evsel__config_callchain(struct evsel *evsel, struct record_opts *opts,
849 struct callchain_param *param)
Arnaldo Carvalho de Melo1688c2f2018-01-12 16:21:04 -0300850{
851 if (param->enabled)
Arnaldo Carvalho de Melo6ec17b42020-04-29 15:57:01 -0300852 return __evsel__config_callchain(evsel, opts, param);
Arnaldo Carvalho de Melo1688c2f2018-01-12 16:21:04 -0300853}
854
Arnaldo Carvalho de Melo56933022020-11-30 09:08:24 -0300855static void evsel__reset_callgraph(struct evsel *evsel, struct callchain_param *param)
Kan Liangd457c962015-08-11 06:30:47 -0400856{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200857 struct perf_event_attr *attr = &evsel->core.attr;
Kan Liangd457c962015-08-11 06:30:47 -0400858
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -0300859 evsel__reset_sample_bit(evsel, CALLCHAIN);
Kan Liangd457c962015-08-11 06:30:47 -0400860 if (param->record_mode == CALLCHAIN_LBR) {
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -0300861 evsel__reset_sample_bit(evsel, BRANCH_STACK);
Kan Liangd457c962015-08-11 06:30:47 -0400862 attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER |
Kan Liangd3f85432020-02-28 08:30:01 -0800863 PERF_SAMPLE_BRANCH_CALL_STACK |
864 PERF_SAMPLE_BRANCH_HW_INDEX);
Kan Liangd457c962015-08-11 06:30:47 -0400865 }
866 if (param->record_mode == CALLCHAIN_DWARF) {
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -0300867 evsel__reset_sample_bit(evsel, REGS_USER);
868 evsel__reset_sample_bit(evsel, STACK_USER);
Kan Liangd457c962015-08-11 06:30:47 -0400869 }
870}
871
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300872static void evsel__apply_config_terms(struct evsel *evsel,
873 struct record_opts *opts, bool track)
Jiri Olsa930a2e22015-07-29 05:42:10 -0400874{
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300875 struct evsel_config_term *term;
Kan Liang32067712015-08-04 04:30:19 -0400876 struct list_head *config_terms = &evsel->config_terms;
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200877 struct perf_event_attr *attr = &evsel->core.attr;
Arnaldo Carvalho de Melo249d98e2018-01-15 11:07:58 -0300878 /* callgraph default */
879 struct callchain_param param = {
880 .record_mode = callchain_param.record_mode,
881 };
Kan Liangd457c962015-08-11 06:30:47 -0400882 u32 dump_size = 0;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300883 int max_stack = 0;
884 const char *callgraph_buf = NULL;
Kan Liangd457c962015-08-11 06:30:47 -0400885
Jiri Olsa930a2e22015-07-29 05:42:10 -0400886 list_for_each_entry(term, config_terms, list) {
887 switch (term->type) {
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300888 case EVSEL__CONFIG_TERM_PERIOD:
Andi Kleenc2f1cea2017-10-20 13:27:55 -0700889 if (!(term->weak && opts->user_interval != ULLONG_MAX)) {
890 attr->sample_period = term->val.period;
891 attr->freq = 0;
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -0300892 evsel__reset_sample_bit(evsel, PERIOD);
Andi Kleenc2f1cea2017-10-20 13:27:55 -0700893 }
Kan Liang32067712015-08-04 04:30:19 -0400894 break;
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300895 case EVSEL__CONFIG_TERM_FREQ:
Andi Kleenc2f1cea2017-10-20 13:27:55 -0700896 if (!(term->weak && opts->user_freq != UINT_MAX)) {
897 attr->sample_freq = term->val.freq;
898 attr->freq = 1;
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -0300899 evsel__set_sample_bit(evsel, PERIOD);
Andi Kleenc2f1cea2017-10-20 13:27:55 -0700900 }
Namhyung Kim09af2a52015-08-09 15:45:23 +0900901 break;
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300902 case EVSEL__CONFIG_TERM_TIME:
Kan Liang32067712015-08-04 04:30:19 -0400903 if (term->val.time)
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -0300904 evsel__set_sample_bit(evsel, TIME);
Kan Liang32067712015-08-04 04:30:19 -0400905 else
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -0300906 evsel__reset_sample_bit(evsel, TIME);
Kan Liang32067712015-08-04 04:30:19 -0400907 break;
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300908 case EVSEL__CONFIG_TERM_CALLGRAPH:
Leo Yane8846022020-01-17 13:52:50 +0800909 callgraph_buf = term->val.str;
Kan Liangd457c962015-08-11 06:30:47 -0400910 break;
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300911 case EVSEL__CONFIG_TERM_BRANCH:
Leo Yane8846022020-01-17 13:52:50 +0800912 if (term->val.str && strcmp(term->val.str, "no")) {
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -0300913 evsel__set_sample_bit(evsel, BRANCH_STACK);
Leo Yane8846022020-01-17 13:52:50 +0800914 parse_branch_str(term->val.str,
Andi Kleenac12f672016-10-12 14:02:06 -0700915 &attr->branch_sample_type);
916 } else
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -0300917 evsel__reset_sample_bit(evsel, BRANCH_STACK);
Andi Kleenac12f672016-10-12 14:02:06 -0700918 break;
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300919 case EVSEL__CONFIG_TERM_STACK_USER:
Kan Liangd457c962015-08-11 06:30:47 -0400920 dump_size = term->val.stack_user;
921 break;
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300922 case EVSEL__CONFIG_TERM_MAX_STACK:
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300923 max_stack = term->val.max_stack;
924 break;
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300925 case EVSEL__CONFIG_TERM_MAX_EVENTS:
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -0300926 evsel->max_events = term->val.max_events;
927 break;
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300928 case EVSEL__CONFIG_TERM_INHERIT:
Wang Nan374ce932015-10-28 10:55:02 +0000929 /*
930 * attr->inherit should has already been set by
Arnaldo Carvalho de Melo6ec17b42020-04-29 15:57:01 -0300931 * evsel__config. If user explicitly set
Wang Nan374ce932015-10-28 10:55:02 +0000932 * inherit using config terms, override global
933 * opt->no_inherit setting.
934 */
935 attr->inherit = term->val.inherit ? 1 : 0;
936 break;
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300937 case EVSEL__CONFIG_TERM_OVERWRITE:
Wang Nan626a6b72016-07-14 08:34:45 +0000938 attr->write_backward = term->val.overwrite ? 1 : 0;
939 break;
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300940 case EVSEL__CONFIG_TERM_DRV_CFG:
Mathieu Poirier21787902018-01-10 13:46:51 -0700941 break;
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300942 case EVSEL__CONFIG_TERM_PERCORE:
Jin Yao064b4e82019-04-12 21:59:47 +0800943 break;
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300944 case EVSEL__CONFIG_TERM_AUX_OUTPUT:
Adrian Hunter1b992152019-08-06 11:46:05 +0300945 attr->aux_output = term->val.aux_output ? 1 : 0;
946 break;
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300947 case EVSEL__CONFIG_TERM_AUX_SAMPLE_SIZE:
Adrian Huntereb7a52d2019-11-15 14:42:17 +0200948 /* Already applied by auxtrace */
949 break;
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300950 case EVSEL__CONFIG_TERM_CFG_CHG:
Adrian Huntera1ac7de2019-11-15 14:42:22 +0200951 break;
Jiri Olsa930a2e22015-07-29 05:42:10 -0400952 default:
953 break;
954 }
955 }
Kan Liangd457c962015-08-11 06:30:47 -0400956
957 /* User explicitly set per-event callgraph, clear the old setting and reset. */
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300958 if ((callgraph_buf != NULL) || (dump_size > 0) || max_stack) {
Arnaldo Carvalho de Melo0d3dcc02018-01-16 11:16:25 -0300959 bool sample_address = false;
960
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300961 if (max_stack) {
962 param.max_stack = max_stack;
963 if (callgraph_buf == NULL)
964 callgraph_buf = "fp";
965 }
Kan Liangd457c962015-08-11 06:30:47 -0400966
967 /* parse callgraph parameters */
968 if (callgraph_buf != NULL) {
Kan Liangf9db0d02015-08-11 06:30:48 -0400969 if (!strcmp(callgraph_buf, "no")) {
970 param.enabled = false;
971 param.record_mode = CALLCHAIN_NONE;
972 } else {
973 param.enabled = true;
974 if (parse_callchain_record(callgraph_buf, &param)) {
975 pr_err("per-event callgraph setting for %s failed. "
976 "Apply callgraph global setting for it\n",
977 evsel->name);
978 return;
979 }
Arnaldo Carvalho de Melo0d3dcc02018-01-16 11:16:25 -0300980 if (param.record_mode == CALLCHAIN_DWARF)
981 sample_address = true;
Kan Liangd457c962015-08-11 06:30:47 -0400982 }
983 }
984 if (dump_size > 0) {
985 dump_size = round_up(dump_size, sizeof(u64));
986 param.dump_size = dump_size;
987 }
988
989 /* If global callgraph set, clear it */
990 if (callchain_param.enabled)
Arnaldo Carvalho de Melo56933022020-11-30 09:08:24 -0300991 evsel__reset_callgraph(evsel, &callchain_param);
Kan Liangd457c962015-08-11 06:30:47 -0400992
993 /* set perf-event callgraph */
Arnaldo Carvalho de Melo0d3dcc02018-01-16 11:16:25 -0300994 if (param.enabled) {
995 if (sample_address) {
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -0300996 evsel__set_sample_bit(evsel, ADDR);
997 evsel__set_sample_bit(evsel, DATA_SRC);
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200998 evsel->core.attr.mmap_data = track;
Arnaldo Carvalho de Melo0d3dcc02018-01-16 11:16:25 -0300999 }
Arnaldo Carvalho de Melo6ec17b42020-04-29 15:57:01 -03001000 evsel__config_callchain(evsel, opts, &param);
Arnaldo Carvalho de Melo0d3dcc02018-01-16 11:16:25 -03001001 }
Kan Liangd457c962015-08-11 06:30:47 -04001002 }
Jiri Olsa930a2e22015-07-29 05:42:10 -04001003}
1004
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -03001005struct evsel_config_term *__evsel__get_config_term(struct evsel *evsel, enum evsel_term_type type)
Adrian Huntereb7a52d2019-11-15 14:42:17 +02001006{
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -03001007 struct evsel_config_term *term, *found_term = NULL;
Adrian Huntereb7a52d2019-11-15 14:42:17 +02001008
1009 list_for_each_entry(term, &evsel->config_terms, list) {
1010 if (term->type == type)
1011 found_term = term;
1012 }
1013
1014 return found_term;
1015}
1016
Kan Liangea8d0ed2021-02-02 12:09:09 -08001017void __weak arch_evsel__set_sample_weight(struct evsel *evsel)
1018{
1019 evsel__set_sample_bit(evsel, WEIGHT);
1020}
1021
Jiri Olsa774cb492012-11-12 18:34:01 +01001022/*
1023 * The enable_on_exec/disabled value strategy:
1024 *
1025 * 1) For any type of traced program:
1026 * - all independent events and group leaders are disabled
1027 * - all group members are enabled
1028 *
1029 * Group members are ruled by group leaders. They need to
1030 * be enabled, because the group scheduling relies on that.
1031 *
1032 * 2) For traced programs executed by perf:
1033 * - all independent events and group leaders have
1034 * enable_on_exec set
1035 * - we don't specifically enable or disable any event during
1036 * the record command
1037 *
1038 * Independent events and group leaders are initially disabled
1039 * and get enabled by exec. Group members are ruled by group
1040 * leaders as stated in 1).
1041 *
1042 * 3) For traced programs attached by perf (pid/tid):
1043 * - we specifically enable or disable all events during
1044 * the record command
1045 *
1046 * When attaching events to already running traced we
1047 * enable/disable events specifically, as there's no
1048 * initial traced exec call.
1049 */
Arnaldo Carvalho de Melo6ec17b42020-04-29 15:57:01 -03001050void evsel__config(struct evsel *evsel, struct record_opts *opts,
1051 struct callchain_param *callchain)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001052{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001053 struct evsel *leader = evsel->leader;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001054 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter60b08962014-07-31 09:00:52 +03001055 int track = evsel->tracking;
Adrian Hunter3aa59392013-11-15 15:52:29 +02001056 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001057
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001058 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001059 attr->inherit = !opts->no_inherit;
Wang Nan626a6b72016-07-14 08:34:45 +00001060 attr->write_backward = opts->overwrite ? 1 : 0;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001061
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -03001062 evsel__set_sample_bit(evsel, IP);
1063 evsel__set_sample_bit(evsel, TID);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001064
Jiri Olsa3c176312012-10-10 17:39:03 +02001065 if (evsel->sample_read) {
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -03001066 evsel__set_sample_bit(evsel, READ);
Jiri Olsa3c176312012-10-10 17:39:03 +02001067
1068 /*
1069 * We need ID even in case of single event, because
1070 * PERF_SAMPLE_READ process ID specific data.
1071 */
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -03001072 evsel__set_sample_id(evsel, false);
Jiri Olsa3c176312012-10-10 17:39:03 +02001073
1074 /*
1075 * Apply group format only if we belong to group
1076 * with more than one members.
1077 */
Jiri Olsa5643b1a2019-07-21 13:24:46 +02001078 if (leader->core.nr_members > 1) {
Jiri Olsa3c176312012-10-10 17:39:03 +02001079 attr->read_format |= PERF_FORMAT_GROUP;
1080 attr->inherit = 0;
1081 }
1082 }
1083
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001084 /*
Namhyung Kim17314e22014-06-09 14:43:37 +09001085 * We default some events to have a default interval. But keep
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001086 * it a weak assumption overridable by the user.
1087 */
Stephane Eranianae5dcc82020-09-11 19:56:53 -07001088 if (!attr->sample_period) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001089 if (opts->freq) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001090 attr->freq = 1;
1091 attr->sample_freq = opts->freq;
1092 } else {
1093 attr->sample_period = opts->default_interval;
1094 }
1095 }
David Sharpce4326d2020-09-11 19:56:52 -07001096 /*
1097 * If attr->freq was set (here or earlier), ask for period
1098 * to be sampled.
1099 */
1100 if (attr->freq)
1101 evsel__set_sample_bit(evsel, PERIOD);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001102
1103 if (opts->no_samples)
1104 attr->sample_freq = 0;
1105
Jiri Olsaa17f06972017-08-24 18:27:31 +02001106 if (opts->inherit_stat) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001107 evsel->core.attr.read_format |=
Jiri Olsaa17f06972017-08-24 18:27:31 +02001108 PERF_FORMAT_TOTAL_TIME_ENABLED |
1109 PERF_FORMAT_TOTAL_TIME_RUNNING |
1110 PERF_FORMAT_ID;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001111 attr->inherit_stat = 1;
Jiri Olsaa17f06972017-08-24 18:27:31 +02001112 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001113
1114 if (opts->sample_address) {
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -03001115 evsel__set_sample_bit(evsel, ADDR);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001116 attr->mmap_data = track;
1117 }
1118
Jiri Olsaf1403732014-11-13 18:21:03 +01001119 /*
1120 * We don't allow user space callchains for function trace
1121 * event, due to issues with page faults while tracing page
1122 * fault handler and its overall trickiness nature.
1123 */
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -03001124 if (evsel__is_function_event(evsel))
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001125 evsel->core.attr.exclude_callchain_user = 1;
Jiri Olsaf1403732014-11-13 18:21:03 +01001126
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -03001127 if (callchain && callchain->enabled && !evsel->no_aux_samples)
Arnaldo Carvalho de Melo6ec17b42020-04-29 15:57:01 -03001128 evsel__config_callchain(evsel, opts, callchain);
Jiri Olsa26d33022012-08-07 15:20:47 +02001129
Jin Yaoc4735d92020-07-20 09:00:13 +08001130 if (opts->sample_intr_regs && !evsel->no_aux_samples &&
1131 !evsel__is_dummy_event(evsel)) {
Stephane Eranianbcc84ec2015-08-31 18:41:12 +02001132 attr->sample_regs_intr = opts->sample_intr_regs;
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -03001133 evsel__set_sample_bit(evsel, REGS_INTR);
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001134 }
1135
Jin Yaoc4735d92020-07-20 09:00:13 +08001136 if (opts->sample_user_regs && !evsel->no_aux_samples &&
1137 !evsel__is_dummy_event(evsel)) {
Andi Kleen84c41742017-09-05 10:00:28 -07001138 attr->sample_regs_user |= opts->sample_user_regs;
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -03001139 evsel__set_sample_bit(evsel, REGS_USER);
Andi Kleen84c41742017-09-05 10:00:28 -07001140 }
1141
Jiri Olsab6f35ed2016-08-01 20:02:35 +02001142 if (target__has_cpu(&opts->target) || opts->sample_cpu)
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -03001143 evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001144
Andi Kleen8affc2b2014-07-31 14:45:04 +08001145 /*
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -08001146 * When the user explicitly disabled time don't force it here.
Andi Kleen8affc2b2014-07-31 14:45:04 +08001147 */
1148 if (opts->sample_time &&
1149 (!perf_missing_features.sample_id_all &&
Adrian Hunter3abebc52015-07-06 14:51:01 +03001150 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
1151 opts->sample_time_set)))
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -03001152 evsel__set_sample_bit(evsel, TIME);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001153
Adrian Hunter6ff1ce72014-07-14 13:02:56 +03001154 if (opts->raw_samples && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -03001155 evsel__set_sample_bit(evsel, TIME);
1156 evsel__set_sample_bit(evsel, RAW);
1157 evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001158 }
1159
Stephane Eranianccf49bf2013-01-24 16:10:37 +01001160 if (opts->sample_address)
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -03001161 evsel__set_sample_bit(evsel, DATA_SRC);
Stephane Eranianccf49bf2013-01-24 16:10:37 +01001162
Kan Liang3b0a5da2017-08-29 13:11:08 -04001163 if (opts->sample_phys_addr)
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -03001164 evsel__set_sample_bit(evsel, PHYS_ADDR);
Kan Liang3b0a5da2017-08-29 13:11:08 -04001165
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -03001166 if (opts->no_buffering) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001167 attr->watermark = 0;
1168 attr->wakeup_events = 1;
1169 }
Adrian Hunter6ff1ce72014-07-14 13:02:56 +03001170 if (opts->branch_stack && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -03001171 evsel__set_sample_bit(evsel, BRANCH_STACK);
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +01001172 attr->branch_sample_type = opts->branch_stack;
1173 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001174
Andi Kleen05484292013-01-24 16:10:29 +01001175 if (opts->sample_weight)
Kan Liangea8d0ed2021-02-02 12:09:09 -08001176 arch_evsel__set_sample_weight(evsel);
Andi Kleen05484292013-01-24 16:10:29 +01001177
Jiri Olsae29386c2020-12-14 11:54:57 +01001178 attr->task = track;
1179 attr->mmap = track;
1180 attr->mmap2 = track && !perf_missing_features.mmap2;
1181 attr->comm = track;
1182 attr->build_id = track && opts->build_id;
1183
Adrian Hunter246eba82020-05-12 15:19:18 +03001184 /*
1185 * ksymbol is tracked separately with text poke because it needs to be
1186 * system wide and enabled immediately.
1187 */
1188 if (!opts->text_poke)
1189 attr->ksymbol = track && !perf_missing_features.ksymbol;
Arnaldo Carvalho de Melo74a1e862019-08-26 19:31:06 -03001190 attr->bpf_event = track && !opts->no_bpf_event && !perf_missing_features.bpf;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001191
Hari Bathinif3b36142017-03-08 02:11:43 +05301192 if (opts->record_namespaces)
1193 attr->namespaces = track;
1194
Namhyung Kim8fb4b672020-03-25 21:45:34 +09001195 if (opts->record_cgroup) {
1196 attr->cgroup = track && !perf_missing_features.cgroup;
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -03001197 evsel__set_sample_bit(evsel, CGROUP);
Namhyung Kim8fb4b672020-03-25 21:45:34 +09001198 }
1199
Kan Liang542b88f2020-11-30 09:27:53 -08001200 if (opts->sample_data_page_size)
1201 evsel__set_sample_bit(evsel, DATA_PAGE_SIZE);
1202
Kan Liangc1de7f32021-01-05 11:57:49 -08001203 if (opts->sample_code_page_size)
1204 evsel__set_sample_bit(evsel, CODE_PAGE_SIZE);
1205
Adrian Hunterb757bb02015-07-21 12:44:04 +03001206 if (opts->record_switch_events)
1207 attr->context_switch = track;
1208
Andi Kleen475eeab2013-09-20 07:40:43 -07001209 if (opts->sample_transaction)
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -03001210 evsel__set_sample_bit(evsel, TRANSACTION);
Andi Kleen475eeab2013-09-20 07:40:43 -07001211
Andi Kleen85c273d2015-02-24 15:13:40 -08001212 if (opts->running_time) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001213 evsel->core.attr.read_format |=
Andi Kleen85c273d2015-02-24 15:13:40 -08001214 PERF_FORMAT_TOTAL_TIME_ENABLED |
1215 PERF_FORMAT_TOTAL_TIME_RUNNING;
1216 }
1217
Jiri Olsa774cb492012-11-12 18:34:01 +01001218 /*
1219 * XXX see the function comment above
1220 *
1221 * Disabling only independent events or group leaders,
1222 * keeping group members enabled.
1223 */
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -03001224 if (evsel__is_group_leader(evsel))
Jiri Olsa774cb492012-11-12 18:34:01 +01001225 attr->disabled = 1;
1226
1227 /*
1228 * Setting enable_on_exec for independent events and
1229 * group leaders for traced executed by perf.
1230 */
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -03001231 if (target__none(&opts->target) && evsel__is_group_leader(evsel) &&
1232 !opts->initial_delay)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001233 attr->enable_on_exec = 1;
Adrian Hunter2afd2bc2014-07-14 13:02:57 +03001234
1235 if (evsel->immediate) {
1236 attr->disabled = 0;
1237 attr->enable_on_exec = 0;
1238 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001239
1240 clockid = opts->clockid;
1241 if (opts->use_clockid) {
1242 attr->use_clockid = 1;
1243 attr->clockid = opts->clockid;
1244 }
Jiri Olsa930a2e22015-07-29 05:42:10 -04001245
Jiri Olsa7f94af72015-10-05 20:06:05 +02001246 if (evsel->precise_max)
Jiri Olsa4e8a5c12019-03-14 15:00:10 +01001247 attr->precise_ip = 3;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001248
Jiri Olsa85723882016-02-15 09:34:31 +01001249 if (opts->all_user) {
1250 attr->exclude_kernel = 1;
1251 attr->exclude_user = 0;
1252 }
1253
1254 if (opts->all_kernel) {
1255 attr->exclude_kernel = 0;
1256 attr->exclude_user = 1;
1257 }
1258
Jiri Olsafe1f61b2019-07-21 13:24:38 +02001259 if (evsel->core.own_cpus || evsel->unit)
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001260 evsel->core.attr.read_format |= PERF_FORMAT_ID;
Jiri Olsa4ab84552018-10-03 23:20:52 +02001261
Jiri Olsa930a2e22015-07-29 05:42:10 -04001262 /*
1263 * Apply event specific term settings,
1264 * it overloads any global configuration.
1265 */
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -03001266 evsel__apply_config_terms(evsel, opts, track);
Jiri Olsaa359c172016-12-13 08:46:22 +01001267
1268 evsel->ignore_missing_thread = opts->ignore_missing_thread;
Jiri Olsaf290aa12018-02-01 09:38:11 +01001269
1270 /* The --period option takes the precedence. */
1271 if (opts->period_set) {
1272 if (opts->period)
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -03001273 evsel__set_sample_bit(evsel, PERIOD);
Jiri Olsaf290aa12018-02-01 09:38:11 +01001274 else
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -03001275 evsel__reset_sample_bit(evsel, PERIOD);
Jiri Olsaf290aa12018-02-01 09:38:11 +01001276 }
Kan Liang95035c52018-07-09 07:15:22 -07001277
1278 /*
Ian Rogers5885a202020-05-08 14:08:03 -03001279 * A dummy event never triggers any actual counter and therefore
1280 * cannot be used with branch_stack.
1281 *
Kan Liang95035c52018-07-09 07:15:22 -07001282 * For initial_delay, a dummy event is added implicitly.
1283 * The software event will trigger -EOPNOTSUPP error out,
1284 * if BRANCH_STACK bit is set.
1285 */
Adrian Hunter442ad2252020-06-29 12:19:51 +03001286 if (evsel__is_dummy_event(evsel))
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -03001287 evsel__reset_sample_bit(evsel, BRANCH_STACK);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001288}
1289
Arnaldo Carvalho de Meload681ad2020-04-29 16:19:05 -03001290int evsel__set_filter(struct evsel *evsel, const char *filter)
Arnaldo Carvalho de Melo12467ae2015-07-03 17:05:50 -03001291{
1292 char *new_filter = strdup(filter);
1293
1294 if (new_filter != NULL) {
1295 free(evsel->filter);
1296 evsel->filter = new_filter;
1297 return 0;
1298 }
1299
1300 return -1;
1301}
1302
Arnaldo Carvalho de Meload681ad2020-04-29 16:19:05 -03001303static int evsel__append_filter(struct evsel *evsel, const char *fmt, const char *filter)
Arnaldo Carvalho de Melo64ec84f2015-07-04 12:19:13 -03001304{
1305 char *new_filter;
1306
1307 if (evsel->filter == NULL)
Arnaldo Carvalho de Meload681ad2020-04-29 16:19:05 -03001308 return evsel__set_filter(evsel, filter);
Arnaldo Carvalho de Melo64ec84f2015-07-04 12:19:13 -03001309
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06001310 if (asprintf(&new_filter, fmt, evsel->filter, filter) > 0) {
Arnaldo Carvalho de Melo64ec84f2015-07-04 12:19:13 -03001311 free(evsel->filter);
1312 evsel->filter = new_filter;
1313 return 0;
1314 }
1315
1316 return -1;
1317}
1318
Arnaldo Carvalho de Meload681ad2020-04-29 16:19:05 -03001319int evsel__append_tp_filter(struct evsel *evsel, const char *filter)
Mathieu Poirier3541c032016-09-16 08:44:04 -06001320{
Arnaldo Carvalho de Meload681ad2020-04-29 16:19:05 -03001321 return evsel__append_filter(evsel, "(%s) && (%s)", filter);
Mathieu Poirier3541c032016-09-16 08:44:04 -06001322}
1323
Arnaldo Carvalho de Meload681ad2020-04-29 16:19:05 -03001324int evsel__append_addr_filter(struct evsel *evsel, const char *filter)
Mathieu Poirier1e857482016-09-16 08:44:05 -06001325{
Arnaldo Carvalho de Meload681ad2020-04-29 16:19:05 -03001326 return evsel__append_filter(evsel, "%s,%s", filter);
Mathieu Poirier1e857482016-09-16 08:44:05 -06001327}
1328
Andi Kleen363fb122019-11-20 16:15:21 -08001329/* Caller has to clear disabled after going through all CPUs. */
1330int evsel__enable_cpu(struct evsel *evsel, int cpu)
1331{
1332 return perf_evsel__enable_cpu(&evsel->core, cpu);
1333}
1334
Jiri Olsaec7f24e2019-07-21 13:24:02 +02001335int evsel__enable(struct evsel *evsel)
Andi Kleene2407be2013-08-02 17:41:10 -07001336{
Jiri Olsaa00571f2019-07-21 13:24:52 +02001337 int err = perf_evsel__enable(&evsel->core);
Arnaldo Carvalho de Melob7e84522018-10-20 09:04:41 -03001338
1339 if (!err)
1340 evsel->disabled = false;
Arnaldo Carvalho de Melob7e84522018-10-20 09:04:41 -03001341 return err;
Andi Kleene2407be2013-08-02 17:41:10 -07001342}
1343
Andi Kleen363fb122019-11-20 16:15:21 -08001344/* Caller has to set disabled after going through all CPUs. */
1345int evsel__disable_cpu(struct evsel *evsel, int cpu)
1346{
1347 return perf_evsel__disable_cpu(&evsel->core, cpu);
1348}
1349
Jiri Olsa9a10bb22019-07-21 13:24:03 +02001350int evsel__disable(struct evsel *evsel)
Jiri Olsae98a4cb2015-12-03 10:06:41 +01001351{
Jiri Olsaa00571f2019-07-21 13:24:52 +02001352 int err = perf_evsel__disable(&evsel->core);
Arnaldo Carvalho de Melob7e84522018-10-20 09:04:41 -03001353 /*
1354 * We mark it disabled here so that tools that disable a event can
1355 * ignore events after they disable it. I.e. the ring buffer may have
1356 * already a few more events queued up before the kernel got the stop
1357 * request.
1358 */
1359 if (!err)
1360 evsel->disabled = true;
1361
1362 return err;
Jiri Olsae98a4cb2015-12-03 10:06:41 +01001363}
1364
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -03001365static void evsel__free_config_terms(struct evsel *evsel)
Jiri Olsa930a2e22015-07-29 05:42:10 -04001366{
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -03001367 struct evsel_config_term *term, *h;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001368
1369 list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -03001370 list_del_init(&term->list);
Leo Yan3220fb82020-01-17 13:52:51 +08001371 if (term->free_str)
1372 zfree(&term->val.str);
Jiri Olsa930a2e22015-07-29 05:42:10 -04001373 free(term);
1374 }
1375}
1376
Arnaldo Carvalho de Melo30f7c592020-04-29 15:53:17 -03001377void evsel__exit(struct evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001378{
Jiri Olsab27c4ec2019-07-21 13:24:22 +02001379 assert(list_empty(&evsel->core.node));
Arnaldo Carvalho de Melod49e4692015-08-27 08:07:40 -04001380 assert(evsel->evlist == NULL);
Song Liufa853c42020-12-29 13:42:14 -08001381 bpf_counter__destroy(evsel);
Arnaldo Carvalho de Melo7d1e2392020-05-06 13:38:26 -03001382 evsel__free_counts(evsel);
Jiri Olsa88761fa2019-07-21 13:24:50 +02001383 perf_evsel__free_fd(&evsel->core);
Jiri Olsa70c20362019-09-03 10:34:29 +02001384 perf_evsel__free_id(&evsel->core);
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -03001385 evsel__free_config_terms(evsel);
Arnaldo Carvalho de Meloa53b6462018-03-06 10:10:45 -03001386 cgroup__put(evsel->cgrp);
Jiri Olsad400bd32019-07-21 13:24:37 +02001387 perf_cpu_map__put(evsel->core.cpus);
Jiri Olsafe1f61b2019-07-21 13:24:38 +02001388 perf_cpu_map__put(evsel->core.own_cpus);
Jiri Olsaaf663bd2019-07-21 13:24:39 +02001389 perf_thread_map__put(evsel->core.threads);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -03001390 zfree(&evsel->group_name);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -03001391 zfree(&evsel->name);
Ian Rogersd4953f72020-03-14 10:03:56 -07001392 zfree(&evsel->pmu_name);
Ian Rogers3efc8992020-05-12 16:59:18 -07001393 zfree(&evsel->per_pkg_mask);
1394 zfree(&evsel->metric_events);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -03001395 perf_evsel__object.fini(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -02001396}
1397
Jiri Olsa5eb2dd22019-07-21 13:23:57 +02001398void evsel__delete(struct evsel *evsel)
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -02001399{
Arnaldo Carvalho de Melo30f7c592020-04-29 15:53:17 -03001400 evsel__exit(evsel);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001401 free(evsel);
1402}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001403
Arnaldo Carvalho de Melo12f52612020-04-29 15:47:38 -03001404void evsel__compute_deltas(struct evsel *evsel, int cpu, int thread,
1405 struct perf_counts_values *count)
Stephane Eranianc7a79c42013-01-29 12:47:43 +01001406{
1407 struct perf_counts_values tmp;
1408
1409 if (!evsel->prev_raw_counts)
1410 return;
1411
1412 if (cpu == -1) {
1413 tmp = evsel->prev_raw_counts->aggr;
1414 evsel->prev_raw_counts->aggr = *count;
1415 } else {
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001416 tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
1417 *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
Stephane Eranianc7a79c42013-01-29 12:47:43 +01001418 }
1419
1420 count->val = count->val - tmp.val;
1421 count->ena = count->ena - tmp.ena;
1422 count->run = count->run - tmp.run;
1423}
1424
Jiri Olsa13112bb2014-11-21 10:31:06 +01001425void perf_counts_values__scale(struct perf_counts_values *count,
1426 bool scale, s8 *pscaled)
1427{
1428 s8 scaled = 0;
1429
1430 if (scale) {
1431 if (count->run == 0) {
1432 scaled = -1;
1433 count->val = 0;
1434 } else if (count->run < count->ena) {
1435 scaled = 1;
Andi Kleen42a58642019-03-14 15:50:02 -07001436 count->val = (u64)((double) count->val * count->ena / count->run);
Jiri Olsa13112bb2014-11-21 10:31:06 +01001437 }
Andi Kleen75998bb2019-03-14 15:50:01 -07001438 }
Jiri Olsa13112bb2014-11-21 10:31:06 +01001439
1440 if (pscaled)
1441 *pscaled = scaled;
1442}
1443
Arnaldo Carvalho de Meloea089692020-04-30 11:00:53 -03001444static int evsel__read_one(struct evsel *evsel, int cpu, int thread)
Jiri Olsaf7794d52017-07-26 14:02:05 +02001445{
1446 struct perf_counts_values *count = perf_counts(evsel->counts, cpu, thread);
1447
Jiri Olsa5c30af92019-07-21 13:24:51 +02001448 return perf_evsel__read(&evsel->core, cpu, thread, count);
Jiri Olsaf7794d52017-07-26 14:02:05 +02001449}
1450
Arnaldo Carvalho de Melo56933022020-11-30 09:08:24 -03001451static void evsel__set_count(struct evsel *counter, int cpu, int thread, u64 val, u64 ena, u64 run)
Jiri Olsaf7794d52017-07-26 14:02:05 +02001452{
1453 struct perf_counts_values *count;
1454
1455 count = perf_counts(counter->counts, cpu, thread);
1456
1457 count->val = val;
1458 count->ena = ena;
1459 count->run = run;
Jiri Olsadf1d6852019-07-21 13:23:48 +02001460
1461 perf_counts__set_loaded(counter->counts, cpu, thread, true);
Jiri Olsaf7794d52017-07-26 14:02:05 +02001462}
1463
Arnaldo Carvalho de Melo56933022020-11-30 09:08:24 -03001464static int evsel__process_group_data(struct evsel *leader, int cpu, int thread, u64 *data)
Jiri Olsaf7794d52017-07-26 14:02:05 +02001465{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001466 u64 read_format = leader->core.attr.read_format;
Jiri Olsaf7794d52017-07-26 14:02:05 +02001467 struct sample_read_value *v;
1468 u64 nr, ena = 0, run = 0, i;
1469
1470 nr = *data++;
1471
Jiri Olsa5643b1a2019-07-21 13:24:46 +02001472 if (nr != (u64) leader->core.nr_members)
Jiri Olsaf7794d52017-07-26 14:02:05 +02001473 return -EINVAL;
1474
1475 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1476 ena = *data++;
1477
1478 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1479 run = *data++;
1480
1481 v = (struct sample_read_value *) data;
1482
Arnaldo Carvalho de Melo56933022020-11-30 09:08:24 -03001483 evsel__set_count(leader, cpu, thread, v[0].value, ena, run);
Jiri Olsaf7794d52017-07-26 14:02:05 +02001484
1485 for (i = 1; i < nr; i++) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02001486 struct evsel *counter;
Jiri Olsaf7794d52017-07-26 14:02:05 +02001487
Arnaldo Carvalho de Melo3ccf8a72020-11-30 14:17:57 -03001488 counter = evlist__id2evsel(leader->evlist, v[i].id);
Jiri Olsaf7794d52017-07-26 14:02:05 +02001489 if (!counter)
1490 return -EINVAL;
1491
Arnaldo Carvalho de Melo56933022020-11-30 09:08:24 -03001492 evsel__set_count(counter, cpu, thread, v[i].value, ena, run);
Jiri Olsaf7794d52017-07-26 14:02:05 +02001493 }
1494
1495 return 0;
1496}
1497
Arnaldo Carvalho de Meloea089692020-04-30 11:00:53 -03001498static int evsel__read_group(struct evsel *leader, int cpu, int thread)
Jiri Olsaf7794d52017-07-26 14:02:05 +02001499{
Arnaldo Carvalho de Melo82806c32017-11-09 12:03:40 -03001500 struct perf_stat_evsel *ps = leader->stats;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001501 u64 read_format = leader->core.attr.read_format;
Jiri Olsa5c30af92019-07-21 13:24:51 +02001502 int size = perf_evsel__read_size(&leader->core);
Jiri Olsaf7794d52017-07-26 14:02:05 +02001503 u64 *data = ps->group_data;
1504
1505 if (!(read_format & PERF_FORMAT_ID))
1506 return -EINVAL;
1507
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -03001508 if (!evsel__is_group_leader(leader))
Jiri Olsaf7794d52017-07-26 14:02:05 +02001509 return -EINVAL;
1510
1511 if (!data) {
1512 data = zalloc(size);
1513 if (!data)
1514 return -ENOMEM;
1515
1516 ps->group_data = data;
1517 }
1518
1519 if (FD(leader, cpu, thread) < 0)
1520 return -EINVAL;
1521
1522 if (readn(FD(leader, cpu, thread), data, size) <= 0)
1523 return -errno;
1524
Arnaldo Carvalho de Melo56933022020-11-30 09:08:24 -03001525 return evsel__process_group_data(leader, cpu, thread, data);
Jiri Olsaf7794d52017-07-26 14:02:05 +02001526}
1527
Arnaldo Carvalho de Meloea089692020-04-30 11:00:53 -03001528int evsel__read_counter(struct evsel *evsel, int cpu, int thread)
Jiri Olsaf7794d52017-07-26 14:02:05 +02001529{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001530 u64 read_format = evsel->core.attr.read_format;
Jiri Olsaf7794d52017-07-26 14:02:05 +02001531
1532 if (read_format & PERF_FORMAT_GROUP)
Arnaldo Carvalho de Meloea089692020-04-30 11:00:53 -03001533 return evsel__read_group(evsel, cpu, thread);
1534
1535 return evsel__read_one(evsel, cpu, thread);
Jiri Olsaf7794d52017-07-26 14:02:05 +02001536}
1537
Arnaldo Carvalho de Meloea089692020-04-30 11:00:53 -03001538int __evsel__read_on_cpu(struct evsel *evsel, int cpu, int thread, bool scale)
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001539{
1540 struct perf_counts_values count;
1541 size_t nv = scale ? 3 : 1;
1542
1543 if (FD(evsel, cpu, thread) < 0)
1544 return -EINVAL;
1545
Arnaldo Carvalho de Melo7d1e2392020-05-06 13:38:26 -03001546 if (evsel->counts == NULL && evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001547 return -ENOMEM;
1548
Stephane Eraniandb49a712017-04-12 11:23:01 -07001549 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) <= 0)
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001550 return -errno;
1551
Arnaldo Carvalho de Melo12f52612020-04-29 15:47:38 -03001552 evsel__compute_deltas(evsel, cpu, thread, &count);
Jiri Olsa13112bb2014-11-21 10:31:06 +01001553 perf_counts_values__scale(&count, scale, NULL);
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001554 *perf_counts(evsel->counts, cpu, thread) = count;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001555 return 0;
1556}
1557
Jiri Olsa32dcd022019-07-21 13:23:51 +02001558static int get_group_fd(struct evsel *evsel, int cpu, int thread)
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001559{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001560 struct evsel *leader = evsel->leader;
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001561 int fd;
1562
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -03001563 if (evsel__is_group_leader(evsel))
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001564 return -1;
1565
1566 /*
1567 * Leader must be already processed/open,
1568 * if not it's a bug.
1569 */
Jiri Olsa9dfcb752019-07-21 13:24:45 +02001570 BUG_ON(!leader->core.fd);
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001571
1572 fd = FD(leader, cpu, thread);
1573 BUG_ON(fd == -1);
1574
1575 return fd;
1576}
1577
Arnaldo Carvalho de Melo56933022020-11-30 09:08:24 -03001578static void evsel__remove_fd(struct evsel *pos, int nr_cpus, int nr_threads, int thread_idx)
Mengting Zhangca800062017-12-13 15:01:53 +08001579{
1580 for (int cpu = 0; cpu < nr_cpus; cpu++)
1581 for (int thread = thread_idx; thread < nr_threads - 1; thread++)
1582 FD(pos, cpu, thread) = FD(pos, cpu, thread + 1);
1583}
1584
Jiri Olsa32dcd022019-07-21 13:23:51 +02001585static int update_fds(struct evsel *evsel,
Mengting Zhangca800062017-12-13 15:01:53 +08001586 int nr_cpus, int cpu_idx,
1587 int nr_threads, int thread_idx)
1588{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001589 struct evsel *pos;
Mengting Zhangca800062017-12-13 15:01:53 +08001590
1591 if (cpu_idx >= nr_cpus || thread_idx >= nr_threads)
1592 return -EINVAL;
1593
1594 evlist__for_each_entry(evsel->evlist, pos) {
1595 nr_cpus = pos != evsel ? nr_cpus : cpu_idx;
1596
Arnaldo Carvalho de Melo56933022020-11-30 09:08:24 -03001597 evsel__remove_fd(pos, nr_cpus, nr_threads, thread_idx);
Mengting Zhangca800062017-12-13 15:01:53 +08001598
1599 /*
1600 * Since fds for next evsel has not been created,
1601 * there is no need to iterate whole event list.
1602 */
1603 if (pos == evsel)
1604 break;
1605 }
1606 return 0;
1607}
1608
Jiri Olsa32dcd022019-07-21 13:23:51 +02001609static bool ignore_missing_thread(struct evsel *evsel,
Mengting Zhangca800062017-12-13 15:01:53 +08001610 int nr_cpus, int cpu,
Jiri Olsa9749b902019-07-21 13:23:50 +02001611 struct perf_thread_map *threads,
Jiri Olsaa359c172016-12-13 08:46:22 +01001612 int thread, int err)
1613{
Jiri Olsaa2f354e2019-08-22 13:11:41 +02001614 pid_t ignore_pid = perf_thread_map__pid(threads, thread);
Mengting Zhangca800062017-12-13 15:01:53 +08001615
Jiri Olsaa359c172016-12-13 08:46:22 +01001616 if (!evsel->ignore_missing_thread)
1617 return false;
1618
1619 /* The system wide setup does not work with threads. */
Jiri Olsa648b5af2019-08-06 11:35:19 +02001620 if (evsel->core.system_wide)
Jiri Olsaa359c172016-12-13 08:46:22 +01001621 return false;
1622
1623 /* The -ESRCH is perf event syscall errno for pid's not found. */
1624 if (err != -ESRCH)
1625 return false;
1626
1627 /* If there's only one thread, let it fail. */
1628 if (threads->nr == 1)
1629 return false;
1630
Mengting Zhangca800062017-12-13 15:01:53 +08001631 /*
1632 * We should remove fd for missing_thread first
1633 * because thread_map__remove() will decrease threads->nr.
1634 */
1635 if (update_fds(evsel, nr_cpus, cpu, threads->nr, thread))
1636 return false;
1637
Jiri Olsaa359c172016-12-13 08:46:22 +01001638 if (thread_map__remove(threads, thread))
1639 return false;
1640
1641 pr_warning("WARNING: Ignored open failure for pid %d\n",
Mengting Zhangca800062017-12-13 15:01:53 +08001642 ignore_pid);
Jiri Olsaa359c172016-12-13 08:46:22 +01001643 return true;
1644}
1645
Arnaldo Carvalho de Meloca125272019-09-24 15:41:51 -03001646static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1647 void *priv __maybe_unused)
1648{
1649 return fprintf(fp, " %-32s %s\n", name, val);
1650}
1651
Jiri Olsa4e8a5c12019-03-14 15:00:10 +01001652static void display_attr(struct perf_event_attr *attr)
1653{
Ravi Bangoriaccd26742019-11-08 15:11:28 +05301654 if (verbose >= 2 || debug_peo_args) {
Jiri Olsa4e8a5c12019-03-14 15:00:10 +01001655 fprintf(stderr, "%.60s\n", graph_dotted_line);
1656 fprintf(stderr, "perf_event_attr:\n");
1657 perf_event_attr__fprintf(stderr, attr, __open_attr__fprintf, NULL);
1658 fprintf(stderr, "%.60s\n", graph_dotted_line);
1659 }
1660}
1661
Jiri Olsa32dcd022019-07-21 13:23:51 +02001662static int perf_event_open(struct evsel *evsel,
Jiri Olsa4e8a5c12019-03-14 15:00:10 +01001663 pid_t pid, int cpu, int group_fd,
1664 unsigned long flags)
1665{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001666 int precise_ip = evsel->core.attr.precise_ip;
Jiri Olsa4e8a5c12019-03-14 15:00:10 +01001667 int fd;
1668
1669 while (1) {
Ravi Bangoriaccd26742019-11-08 15:11:28 +05301670 pr_debug2_peo("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx",
Jiri Olsa4e8a5c12019-03-14 15:00:10 +01001671 pid, cpu, group_fd, flags);
1672
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001673 fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, group_fd, flags);
Jiri Olsa4e8a5c12019-03-14 15:00:10 +01001674 if (fd >= 0)
1675 break;
1676
Jiri Olsacd136182019-07-03 10:09:49 +02001677 /* Do not try less precise if not requested. */
1678 if (!evsel->precise_max)
Jiri Olsa4e8a5c12019-03-14 15:00:10 +01001679 break;
1680
1681 /*
1682 * We tried all the precise_ip values, and it's
1683 * still failing, so leave it to standard fallback.
1684 */
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001685 if (!evsel->core.attr.precise_ip) {
1686 evsel->core.attr.precise_ip = precise_ip;
Jiri Olsa4e8a5c12019-03-14 15:00:10 +01001687 break;
1688 }
1689
Ravi Bangoriaccd26742019-11-08 15:11:28 +05301690 pr_debug2_peo("\nsys_perf_event_open failed, error %d\n", -ENOTSUP);
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001691 evsel->core.attr.precise_ip--;
Ravi Bangoriaccd26742019-11-08 15:11:28 +05301692 pr_debug2_peo("decreasing precise_ip by one (%d)\n", evsel->core.attr.precise_ip);
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001693 display_attr(&evsel->core.attr);
Jiri Olsa4e8a5c12019-03-14 15:00:10 +01001694 }
1695
1696 return fd;
1697}
1698
Andi Kleen4804e012019-11-20 16:15:19 -08001699static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
1700 struct perf_thread_map *threads,
1701 int start_cpu, int end_cpu)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001702{
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001703 int cpu, thread, nthreads;
Yann Droneaud57480d22014-06-30 22:28:47 +02001704 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
Andi Kleen796c01a2019-10-20 10:51:54 -07001705 int pid = -1, err, old_errno;
Andi Kleenbec19672013-08-04 19:41:26 -07001706 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001707
Arnaldo Carvalho de Meloacb9f2d2019-08-13 11:06:38 -03001708 if ((perf_missing_features.write_backward && evsel->core.attr.write_backward) ||
1709 (perf_missing_features.aux_output && evsel->core.attr.aux_output))
Arnaldo Carvalho de Melo32a951b2016-07-14 08:34:33 +00001710 return -EINVAL;
1711
Arnaldo Carvalho de Meloc24ae6d2017-02-14 10:59:04 -03001712 if (cpus == NULL) {
Jiri Olsaf8548392019-07-21 13:23:49 +02001713 static struct perf_cpu_map *empty_cpu_map;
Arnaldo Carvalho de Meloc24ae6d2017-02-14 10:59:04 -03001714
1715 if (empty_cpu_map == NULL) {
Jiri Olsa397721e2019-07-21 13:24:16 +02001716 empty_cpu_map = perf_cpu_map__dummy_new();
Arnaldo Carvalho de Meloc24ae6d2017-02-14 10:59:04 -03001717 if (empty_cpu_map == NULL)
1718 return -ENOMEM;
1719 }
1720
1721 cpus = empty_cpu_map;
1722 }
1723
1724 if (threads == NULL) {
Jiri Olsa9749b902019-07-21 13:23:50 +02001725 static struct perf_thread_map *empty_thread_map;
Arnaldo Carvalho de Meloc24ae6d2017-02-14 10:59:04 -03001726
1727 if (empty_thread_map == NULL) {
1728 empty_thread_map = thread_map__new_by_tid(-1);
1729 if (empty_thread_map == NULL)
1730 return -ENOMEM;
1731 }
1732
1733 threads = empty_thread_map;
1734 }
1735
Jiri Olsa648b5af2019-08-06 11:35:19 +02001736 if (evsel->core.system_wide)
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001737 nthreads = 1;
1738 else
1739 nthreads = threads->nr;
1740
Jiri Olsa9dfcb752019-07-21 13:24:45 +02001741 if (evsel->core.fd == NULL &&
Jiri Olsab8eca4d2019-07-21 13:24:48 +02001742 perf_evsel__alloc_fd(&evsel->core, cpus->nr, nthreads) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001743 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001744
Stephane Eranian023695d2011-02-14 11:20:01 +02001745 if (evsel->cgrp) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001746 flags |= PERF_FLAG_PID_CGROUP;
Stephane Eranian023695d2011-02-14 11:20:01 +02001747 pid = evsel->cgrp->fd;
1748 }
1749
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001750fallback_missing_features:
Kan Liangea8d0ed2021-02-02 12:09:09 -08001751 if (perf_missing_features.weight_struct) {
1752 evsel__set_sample_bit(evsel, WEIGHT);
1753 evsel__reset_sample_bit(evsel, WEIGHT_STRUCT);
1754 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001755 if (perf_missing_features.clockid_wrong)
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001756 evsel->core.attr.clockid = CLOCK_MONOTONIC; /* should always work */
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001757 if (perf_missing_features.clockid) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001758 evsel->core.attr.use_clockid = 0;
1759 evsel->core.attr.clockid = 0;
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001760 }
Yann Droneaud57480d22014-06-30 22:28:47 +02001761 if (perf_missing_features.cloexec)
1762 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001763 if (perf_missing_features.mmap2)
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001764 evsel->core.attr.mmap2 = 0;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001765 if (perf_missing_features.exclude_guest)
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001766 evsel->core.attr.exclude_guest = evsel->core.attr.exclude_host = 0;
Andi Kleenbd0f8892015-12-11 16:12:24 -08001767 if (perf_missing_features.lbr_flags)
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001768 evsel->core.attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
Andi Kleenbd0f8892015-12-11 16:12:24 -08001769 PERF_SAMPLE_BRANCH_NO_CYCLES);
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001770 if (perf_missing_features.group_read && evsel->core.attr.inherit)
1771 evsel->core.attr.read_format &= ~(PERF_FORMAT_GROUP|PERF_FORMAT_ID);
Song Liu9aa0bfa2019-01-17 08:15:17 -08001772 if (perf_missing_features.ksymbol)
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001773 evsel->core.attr.ksymbol = 0;
Arnaldo Carvalho de Melo74a1e862019-08-26 19:31:06 -03001774 if (perf_missing_features.bpf)
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001775 evsel->core.attr.bpf_event = 0;
Kan Liangd3f85432020-02-28 08:30:01 -08001776 if (perf_missing_features.branch_hw_idx)
1777 evsel->core.attr.branch_sample_type &= ~PERF_SAMPLE_BRANCH_HW_INDEX;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001778retry_sample_id:
1779 if (perf_missing_features.sample_id_all)
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001780 evsel->core.attr.sample_id_all = 0;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001781
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001782 display_attr(&evsel->core.attr);
Adrian Huntere3e1a542013-08-14 15:48:24 +03001783
Andi Kleen4804e012019-11-20 16:15:19 -08001784 for (cpu = start_cpu; cpu < end_cpu; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -02001785
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001786 for (thread = 0; thread < nthreads; thread++) {
Jiri Olsa83c2e4f32016-12-12 11:35:40 +01001787 int fd, group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +02001788
Jiri Olsa648b5af2019-08-06 11:35:19 +02001789 if (!evsel->cgrp && !evsel->core.system_wide)
Jiri Olsaa2f354e2019-08-22 13:11:41 +02001790 pid = perf_thread_map__pid(threads, thread);
Stephane Eranian023695d2011-02-14 11:20:01 +02001791
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001792 group_fd = get_group_fd(evsel, cpu, thread);
Andi Kleenbec19672013-08-04 19:41:26 -07001793retry_open:
Jiri Olsa10213e22017-07-03 16:50:18 +02001794 test_attr__ready();
1795
Jiri Olsa4e8a5c12019-03-14 15:00:10 +01001796 fd = perf_event_open(evsel, pid, cpus->map[cpu],
1797 group_fd, flags);
Jiri Olsa83c2e4f32016-12-12 11:35:40 +01001798
1799 FD(evsel, cpu, thread) = fd;
1800
Song Liufa853c42020-12-29 13:42:14 -08001801 bpf_counter__install_pe(evsel, cpu, fd);
1802
Jiri Olsa8366f0d2020-08-27 21:32:01 +02001803 if (unlikely(test_attr__enabled)) {
1804 test_attr__open(&evsel->core.attr, pid, cpus->map[cpu],
1805 fd, group_fd, flags);
1806 }
1807
Jiri Olsa83c2e4f32016-12-12 11:35:40 +01001808 if (fd < 0) {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001809 err = -errno;
Jiri Olsaa359c172016-12-13 08:46:22 +01001810
Mengting Zhangca800062017-12-13 15:01:53 +08001811 if (ignore_missing_thread(evsel, cpus->nr, cpu, threads, thread, err)) {
Jiri Olsaa359c172016-12-13 08:46:22 +01001812 /*
1813 * We just removed 1 thread, so take a step
1814 * back on thread index and lower the upper
1815 * nthreads limit.
1816 */
1817 nthreads--;
1818 thread--;
1819
1820 /* ... and pretend like nothing have happened. */
1821 err = 0;
1822 continue;
1823 }
1824
Ravi Bangoriaccd26742019-11-08 15:11:28 +05301825 pr_debug2_peo("\nsys_perf_event_open failed, error %d\n",
Adrian Hunterf852fd62013-11-01 15:51:29 +02001826 err);
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001827 goto try_fallback;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001828 }
Wang Nan1f45b1d2015-10-14 12:41:18 +00001829
Ravi Bangoriaccd26742019-11-08 15:11:28 +05301830 pr_debug2_peo(" = %d\n", fd);
Jiri Olsa7b4b82bc2016-11-21 22:33:26 +01001831
Wang Nan1f45b1d2015-10-14 12:41:18 +00001832 if (evsel->bpf_fd >= 0) {
Jiri Olsa83c2e4f32016-12-12 11:35:40 +01001833 int evt_fd = fd;
Wang Nan1f45b1d2015-10-14 12:41:18 +00001834 int bpf_fd = evsel->bpf_fd;
1835
1836 err = ioctl(evt_fd,
1837 PERF_EVENT_IOC_SET_BPF,
1838 bpf_fd);
1839 if (err && errno != EEXIST) {
1840 pr_err("failed to attach bpf fd %d: %s\n",
1841 bpf_fd, strerror(errno));
1842 err = -EINVAL;
1843 goto out_close;
1844 }
1845 }
1846
Andi Kleenbec19672013-08-04 19:41:26 -07001847 set_rlimit = NO_CHANGE;
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001848
1849 /*
1850 * If we succeeded but had to kill clockid, fail and
Arnaldo Carvalho de Melo2bb72db2020-05-04 13:43:03 -03001851 * have evsel__open_strerror() print us a nice error.
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001852 */
1853 if (perf_missing_features.clockid ||
1854 perf_missing_features.clockid_wrong) {
1855 err = -EINVAL;
1856 goto out_close;
1857 }
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001858 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001859 }
1860
1861 return 0;
1862
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001863try_fallback:
Andi Kleenbec19672013-08-04 19:41:26 -07001864 /*
1865 * perf stat needs between 5 and 22 fds per CPU. When we run out
1866 * of them try to increase the limits.
1867 */
1868 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1869 struct rlimit l;
Andi Kleenbec19672013-08-04 19:41:26 -07001870
Andi Kleen796c01a2019-10-20 10:51:54 -07001871 old_errno = errno;
Andi Kleenbec19672013-08-04 19:41:26 -07001872 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1873 if (set_rlimit == NO_CHANGE)
1874 l.rlim_cur = l.rlim_max;
1875 else {
1876 l.rlim_cur = l.rlim_max + 1000;
1877 l.rlim_max = l.rlim_cur;
1878 }
1879 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1880 set_rlimit++;
1881 errno = old_errno;
1882 goto retry_open;
1883 }
1884 }
1885 errno = old_errno;
1886 }
1887
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001888 if (err != -EINVAL || cpu > 0 || thread > 0)
1889 goto out_close;
1890
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001891 /*
1892 * Must probe features in the order they were added to the
1893 * perf_event_attr interface.
1894 */
Kan Liangea8d0ed2021-02-02 12:09:09 -08001895 if (!perf_missing_features.weight_struct &&
1896 (evsel->core.attr.sample_type & PERF_SAMPLE_WEIGHT_STRUCT)) {
1897 perf_missing_features.weight_struct = true;
1898 pr_debug2("switching off weight struct support\n");
1899 goto fallback_missing_features;
1900 } else if (!perf_missing_features.code_page_size &&
Kan Liangc1de7f32021-01-05 11:57:49 -08001901 (evsel->core.attr.sample_type & PERF_SAMPLE_CODE_PAGE_SIZE)) {
1902 perf_missing_features.code_page_size = true;
1903 pr_debug2_peo("Kernel has no PERF_SAMPLE_CODE_PAGE_SIZE support, bailing out\n");
1904 goto out_close;
1905 } else if (!perf_missing_features.data_page_size &&
Arnaldo Carvalho de Melo456ef4c12020-12-07 14:04:05 -03001906 (evsel->core.attr.sample_type & PERF_SAMPLE_DATA_PAGE_SIZE)) {
1907 perf_missing_features.data_page_size = true;
1908 pr_debug2_peo("Kernel has no PERF_SAMPLE_DATA_PAGE_SIZE support, bailing out\n");
1909 goto out_close;
1910 } else if (!perf_missing_features.cgroup && evsel->core.attr.cgroup) {
Namhyung Kim8fb4b672020-03-25 21:45:34 +09001911 perf_missing_features.cgroup = true;
1912 pr_debug2_peo("Kernel has no cgroup sampling support, bailing out\n");
1913 goto out_close;
1914 } else if (!perf_missing_features.branch_hw_idx &&
Kan Liangd3f85432020-02-28 08:30:01 -08001915 (evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX)) {
1916 perf_missing_features.branch_hw_idx = true;
1917 pr_debug2("switching off branch HW index support\n");
1918 goto fallback_missing_features;
1919 } else if (!perf_missing_features.aux_output && evsel->core.attr.aux_output) {
Arnaldo Carvalho de Meloacb9f2d2019-08-13 11:06:38 -03001920 perf_missing_features.aux_output = true;
Ravi Bangoriaccd26742019-11-08 15:11:28 +05301921 pr_debug2_peo("Kernel has no attr.aux_output support, bailing out\n");
Arnaldo Carvalho de Meloacb9f2d2019-08-13 11:06:38 -03001922 goto out_close;
Arnaldo Carvalho de Melo74a1e862019-08-26 19:31:06 -03001923 } else if (!perf_missing_features.bpf && evsel->core.attr.bpf_event) {
1924 perf_missing_features.bpf = true;
Ravi Bangoriaccd26742019-11-08 15:11:28 +05301925 pr_debug2_peo("switching off bpf_event\n");
Song Liu45178a92019-01-17 08:15:18 -08001926 goto fallback_missing_features;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001927 } else if (!perf_missing_features.ksymbol && evsel->core.attr.ksymbol) {
Song Liu9aa0bfa2019-01-17 08:15:17 -08001928 perf_missing_features.ksymbol = true;
Ravi Bangoriaccd26742019-11-08 15:11:28 +05301929 pr_debug2_peo("switching off ksymbol\n");
Song Liu9aa0bfa2019-01-17 08:15:17 -08001930 goto fallback_missing_features;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001931 } else if (!perf_missing_features.write_backward && evsel->core.attr.write_backward) {
Arnaldo Carvalho de Melo7da36e92016-06-20 10:47:18 +00001932 perf_missing_features.write_backward = true;
Ravi Bangoriaccd26742019-11-08 15:11:28 +05301933 pr_debug2_peo("switching off write_backward\n");
Arnaldo Carvalho de Melo32a951b2016-07-14 08:34:33 +00001934 goto out_close;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001935 } else if (!perf_missing_features.clockid_wrong && evsel->core.attr.use_clockid) {
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001936 perf_missing_features.clockid_wrong = true;
Ravi Bangoriaccd26742019-11-08 15:11:28 +05301937 pr_debug2_peo("switching off clockid\n");
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001938 goto fallback_missing_features;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001939 } else if (!perf_missing_features.clockid && evsel->core.attr.use_clockid) {
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001940 perf_missing_features.clockid = true;
Ravi Bangoriaccd26742019-11-08 15:11:28 +05301941 pr_debug2_peo("switching off use_clockid\n");
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001942 goto fallback_missing_features;
1943 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001944 perf_missing_features.cloexec = true;
Ravi Bangoriaccd26742019-11-08 15:11:28 +05301945 pr_debug2_peo("switching off cloexec flag\n");
Yann Droneaud57480d22014-06-30 22:28:47 +02001946 goto fallback_missing_features;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001947 } else if (!perf_missing_features.mmap2 && evsel->core.attr.mmap2) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001948 perf_missing_features.mmap2 = true;
Ravi Bangoriaccd26742019-11-08 15:11:28 +05301949 pr_debug2_peo("switching off mmap2\n");
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001950 goto fallback_missing_features;
1951 } else if (!perf_missing_features.exclude_guest &&
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001952 (evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host)) {
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001953 perf_missing_features.exclude_guest = true;
Ravi Bangoriaccd26742019-11-08 15:11:28 +05301954 pr_debug2_peo("switching off exclude_guest, exclude_host\n");
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001955 goto fallback_missing_features;
1956 } else if (!perf_missing_features.sample_id_all) {
1957 perf_missing_features.sample_id_all = true;
Ravi Bangoriaccd26742019-11-08 15:11:28 +05301958 pr_debug2_peo("switching off sample_id_all\n");
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001959 goto retry_sample_id;
Andi Kleenbd0f8892015-12-11 16:12:24 -08001960 } else if (!perf_missing_features.lbr_flags &&
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001961 (evsel->core.attr.branch_sample_type &
Andi Kleenbd0f8892015-12-11 16:12:24 -08001962 (PERF_SAMPLE_BRANCH_NO_CYCLES |
1963 PERF_SAMPLE_BRANCH_NO_FLAGS))) {
1964 perf_missing_features.lbr_flags = true;
Ravi Bangoriaccd26742019-11-08 15:11:28 +05301965 pr_debug2_peo("switching off branch sample type no (cycles/flags)\n");
Andi Kleenbd0f8892015-12-11 16:12:24 -08001966 goto fallback_missing_features;
Jiri Olsa82bf3112017-07-26 14:02:06 +02001967 } else if (!perf_missing_features.group_read &&
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001968 evsel->core.attr.inherit &&
1969 (evsel->core.attr.read_format & PERF_FORMAT_GROUP) &&
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -03001970 evsel__is_group_leader(evsel)) {
Jiri Olsa82bf3112017-07-26 14:02:06 +02001971 perf_missing_features.group_read = true;
Ravi Bangoriaccd26742019-11-08 15:11:28 +05301972 pr_debug2_peo("switching off group read\n");
Jiri Olsa82bf3112017-07-26 14:02:06 +02001973 goto fallback_missing_features;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001974 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001975out_close:
Jin Yaoab6c79b2018-01-16 23:43:08 +08001976 if (err)
1977 threads->err_thread = thread;
1978
Andi Kleen796c01a2019-10-20 10:51:54 -07001979 old_errno = errno;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001980 do {
1981 while (--thread >= 0) {
Andi Kleen2ccfb8b2019-10-20 10:51:55 -07001982 if (FD(evsel, cpu, thread) >= 0)
1983 close(FD(evsel, cpu, thread));
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001984 FD(evsel, cpu, thread) = -1;
1985 }
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001986 thread = nthreads;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001987 } while (--cpu >= 0);
Andi Kleen796c01a2019-10-20 10:51:54 -07001988 errno = old_errno;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001989 return err;
1990}
1991
Andi Kleen4804e012019-11-20 16:15:19 -08001992int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
1993 struct perf_thread_map *threads)
1994{
1995 return evsel__open_cpu(evsel, cpus, threads, 0, cpus ? cpus->nr : 1);
1996}
1997
Jiri Olsa88761fa2019-07-21 13:24:50 +02001998void evsel__close(struct evsel *evsel)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001999{
Jiri Olsa88761fa2019-07-21 13:24:50 +02002000 perf_evsel__close(&evsel->core);
Jiri Olsa70c20362019-09-03 10:34:29 +02002001 perf_evsel__free_id(&evsel->core);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02002002}
2003
Arnaldo Carvalho de Meloaa8c4062020-04-29 16:21:03 -03002004int evsel__open_per_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, int cpu)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02002005{
Andi Kleen4804e012019-11-20 16:15:19 -08002006 if (cpu == -1)
2007 return evsel__open_cpu(evsel, cpus, NULL, 0,
2008 cpus ? cpus->nr : 1);
2009
2010 return evsel__open_cpu(evsel, cpus, NULL, cpu, cpu + 1);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02002011}
2012
Arnaldo Carvalho de Meloaa8c4062020-04-29 16:21:03 -03002013int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02002014{
Jiri Olsa5972d1e2019-07-21 13:24:01 +02002015 return evsel__open(evsel, NULL, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02002016}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -02002017
Jiri Olsa32dcd022019-07-21 13:23:51 +02002018static int perf_evsel__parse_id_sample(const struct evsel *evsel,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002019 const union perf_event *event,
2020 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002021{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002022 u64 type = evsel->core.attr.sample_type;
Jiri Olsab1fcd192019-08-25 20:17:52 +02002023 const __u64 *array = event->sample.array;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002024 bool swapped = evsel->needs_swap;
Jiri Olsa37073f92012-05-30 14:23:44 +02002025 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002026
2027 array += ((event->header.size -
2028 sizeof(event->header)) / sizeof(u64)) - 1;
2029
Adrian Hunter75562572013-08-27 11:23:09 +03002030 if (type & PERF_SAMPLE_IDENTIFIER) {
2031 sample->id = *array;
2032 array--;
2033 }
2034
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002035 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +02002036 u.val64 = *array;
2037 if (swapped) {
2038 /* undo swap of u64, then swap on individual u32s */
2039 u.val64 = bswap_64(u.val64);
2040 u.val32[0] = bswap_32(u.val32[0]);
2041 }
2042
2043 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002044 array--;
2045 }
2046
2047 if (type & PERF_SAMPLE_STREAM_ID) {
2048 sample->stream_id = *array;
2049 array--;
2050 }
2051
2052 if (type & PERF_SAMPLE_ID) {
2053 sample->id = *array;
2054 array--;
2055 }
2056
2057 if (type & PERF_SAMPLE_TIME) {
2058 sample->time = *array;
2059 array--;
2060 }
2061
2062 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +02002063 u.val64 = *array;
2064 if (swapped) {
2065 /* undo swap of u64, then swap on individual u32s */
2066 u.val64 = bswap_64(u.val64);
2067 u.val32[0] = bswap_32(u.val32[0]);
2068 u.val32[1] = bswap_32(u.val32[1]);
2069 }
2070
2071 sample->pid = u.val32[0];
2072 sample->tid = u.val32[1];
Adrian Hunterdd44bc62013-10-18 15:29:01 +03002073 array--;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002074 }
2075
2076 return 0;
2077}
2078
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002079static inline bool overflow(const void *endp, u16 max_size, const void *offset,
2080 u64 size)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02002081{
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002082 return size > max_size || offset + size > endp;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02002083}
2084
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002085#define OVERFLOW_CHECK(offset, size, max_size) \
2086 do { \
2087 if (overflow(endp, (max_size), (offset), (size))) \
2088 return -EFAULT; \
2089 } while (0)
2090
2091#define OVERFLOW_CHECK_u64(offset) \
2092 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
2093
Jiri Olsa01468122017-08-03 13:10:28 +02002094static int
2095perf_event__check_size(union perf_event *event, unsigned int sample_size)
2096{
2097 /*
2098 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
2099 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
2100 * check the format does not go past the end of the event.
2101 */
2102 if (sample_size + sizeof(event->header) > event->header.size)
2103 return -EFAULT;
2104
2105 return 0;
2106}
2107
Kan Liangfbefe9c2021-02-05 08:01:52 -08002108void __weak arch_perf_parse_sample_weight(struct perf_sample *data,
2109 const __u64 *array,
2110 u64 type __maybe_unused)
2111{
2112 data->weight = *array;
2113}
2114
Arnaldo Carvalho de Melo6b6017a2020-04-30 11:03:49 -03002115int evsel__parse_sample(struct evsel *evsel, union perf_event *event,
2116 struct perf_sample *data)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002117{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002118 u64 type = evsel->core.attr.sample_type;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002119 bool swapped = evsel->needs_swap;
Jiri Olsab1fcd192019-08-25 20:17:52 +02002120 const __u64 *array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002121 u16 max_size = event->header.size;
2122 const void *endp = (void *)event + max_size;
2123 u64 sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002124
David Ahern936be502011-09-06 09:12:26 -06002125 /*
2126 * used for cross-endian analysis. See git commit 65014ab3
2127 * for why this goofiness is needed.
2128 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02002129 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -06002130
Robert Richterf3bda2c2011-12-15 17:32:39 +01002131 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002132 data->cpu = data->pid = data->tid = -1;
2133 data->stream_id = data->id = data->time = -1ULL;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002134 data->period = evsel->core.attr.sample_period;
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03002135 data->cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Jiri Olsa28a0b392018-01-07 17:03:52 +01002136 data->misc = event->header.misc;
Jiri Olsa3ad31d82017-08-03 16:07:05 +02002137 data->id = -1ULL;
2138 data->data_src = PERF_MEM_DATA_SRC_NONE;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002139
2140 if (event->header.type != PERF_RECORD_SAMPLE) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002141 if (!evsel->core.attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002142 return 0;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002143 return perf_evsel__parse_id_sample(evsel, event, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002144 }
2145
2146 array = event->sample.array;
2147
Jiri Olsa01468122017-08-03 13:10:28 +02002148 if (perf_event__check_size(event, evsel->sample_size))
Frederic Weisbeckera2854122011-05-21 19:33:04 +02002149 return -EFAULT;
2150
Adrian Hunter75562572013-08-27 11:23:09 +03002151 if (type & PERF_SAMPLE_IDENTIFIER) {
2152 data->id = *array;
2153 array++;
2154 }
2155
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002156 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03002157 data->ip = *array;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002158 array++;
2159 }
2160
2161 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -06002162 u.val64 = *array;
2163 if (swapped) {
2164 /* undo swap of u64, then swap on individual u32s */
2165 u.val64 = bswap_64(u.val64);
2166 u.val32[0] = bswap_32(u.val32[0]);
2167 u.val32[1] = bswap_32(u.val32[1]);
2168 }
2169
2170 data->pid = u.val32[0];
2171 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002172 array++;
2173 }
2174
2175 if (type & PERF_SAMPLE_TIME) {
2176 data->time = *array;
2177 array++;
2178 }
2179
2180 if (type & PERF_SAMPLE_ADDR) {
2181 data->addr = *array;
2182 array++;
2183 }
2184
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002185 if (type & PERF_SAMPLE_ID) {
2186 data->id = *array;
2187 array++;
2188 }
2189
2190 if (type & PERF_SAMPLE_STREAM_ID) {
2191 data->stream_id = *array;
2192 array++;
2193 }
2194
2195 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -06002196
2197 u.val64 = *array;
2198 if (swapped) {
2199 /* undo swap of u64, then swap on individual u32s */
2200 u.val64 = bswap_64(u.val64);
2201 u.val32[0] = bswap_32(u.val32[0]);
2202 }
2203
2204 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002205 array++;
2206 }
2207
2208 if (type & PERF_SAMPLE_PERIOD) {
2209 data->period = *array;
2210 array++;
2211 }
2212
2213 if (type & PERF_SAMPLE_READ) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002214 u64 read_format = evsel->core.attr.read_format;
Jiri Olsa9ede4732012-10-10 17:38:13 +02002215
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002216 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02002217 if (read_format & PERF_FORMAT_GROUP)
2218 data->read.group.nr = *array;
2219 else
2220 data->read.one.value = *array;
2221
2222 array++;
2223
2224 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002225 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02002226 data->read.time_enabled = *array;
2227 array++;
2228 }
2229
2230 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002231 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02002232 data->read.time_running = *array;
2233 array++;
2234 }
2235
2236 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2237 if (read_format & PERF_FORMAT_GROUP) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002238 const u64 max_group_nr = UINT64_MAX /
2239 sizeof(struct sample_read_value);
2240
2241 if (data->read.group.nr > max_group_nr)
2242 return -EFAULT;
2243 sz = data->read.group.nr *
2244 sizeof(struct sample_read_value);
2245 OVERFLOW_CHECK(array, sz, max_size);
2246 data->read.group.values =
2247 (struct sample_read_value *)array;
2248 array = (void *)array + sz;
Jiri Olsa9ede4732012-10-10 17:38:13 +02002249 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002250 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02002251 data->read.one.id = *array;
2252 array++;
2253 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002254 }
2255
Adrian Hunter8e94b322020-04-01 13:16:07 +03002256 if (type & PERF_SAMPLE_CALLCHAIN) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002257 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
2258
2259 OVERFLOW_CHECK_u64(array);
2260 data->callchain = (struct ip_callchain *)array++;
2261 if (data->callchain->nr > max_callchain_nr)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02002262 return -EFAULT;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002263 sz = data->callchain->nr * sizeof(u64);
2264 OVERFLOW_CHECK(array, sz, max_size);
2265 array = (void *)array + sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002266 }
2267
2268 if (type & PERF_SAMPLE_RAW) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002269 OVERFLOW_CHECK_u64(array);
David Ahern936be502011-09-06 09:12:26 -06002270 u.val64 = *array;
Jiri Olsaf9d8adb2017-11-29 19:43:46 +01002271
2272 /*
2273 * Undo swap of u64, then swap on individual u32s,
2274 * get the size of the raw area and undo all of the
2275 * swap. The pevent interface handles endianity by
2276 * itself.
2277 */
2278 if (swapped) {
David Ahern936be502011-09-06 09:12:26 -06002279 u.val64 = bswap_64(u.val64);
2280 u.val32[0] = bswap_32(u.val32[0]);
2281 u.val32[1] = bswap_32(u.val32[1]);
2282 }
David Ahern936be502011-09-06 09:12:26 -06002283 data->raw_size = u.val32[0];
Jiri Olsaf9d8adb2017-11-29 19:43:46 +01002284
2285 /*
2286 * The raw data is aligned on 64bits including the
2287 * u32 size, so it's safe to use mem_bswap_64.
2288 */
2289 if (swapped)
2290 mem_bswap_64((void *) array, data->raw_size);
2291
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002292 array = (void *)array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02002293
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002294 OVERFLOW_CHECK(array, data->raw_size, max_size);
2295 data->raw_data = (void *)array;
2296 array = (void *)array + data->raw_size;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002297 }
2298
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01002299 if (type & PERF_SAMPLE_BRANCH_STACK) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002300 const u64 max_branch_nr = UINT64_MAX /
2301 sizeof(struct branch_entry);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01002302
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002303 OVERFLOW_CHECK_u64(array);
2304 data->branch_stack = (struct branch_stack *)array++;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01002305
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002306 if (data->branch_stack->nr > max_branch_nr)
2307 return -EFAULT;
Kan Liang42bbabe2020-02-28 08:30:00 -08002308
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01002309 sz = data->branch_stack->nr * sizeof(struct branch_entry);
Arnaldo Carvalho de Melo4f138a92020-04-30 11:19:45 -03002310 if (evsel__has_branch_hw_idx(evsel))
Kan Liang42bbabe2020-02-28 08:30:00 -08002311 sz += sizeof(u64);
2312 else
2313 data->no_hw_idx = true;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002314 OVERFLOW_CHECK(array, sz, max_size);
2315 array = (void *)array + sz;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01002316 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002317
2318 if (type & PERF_SAMPLE_REGS_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002319 OVERFLOW_CHECK_u64(array);
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03002320 data->user_regs.abi = *array;
2321 array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002322
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03002323 if (data->user_regs.abi) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002324 u64 mask = evsel->core.attr.sample_regs_user;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002325
Mao Han3a5b64f2019-04-10 16:16:43 +08002326 sz = hweight64(mask) * sizeof(u64);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002327 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa352ea452014-01-07 13:47:25 +01002328 data->user_regs.mask = mask;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002329 data->user_regs.regs = (u64 *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002330 array = (void *)array + sz;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002331 }
2332 }
2333
2334 if (type & PERF_SAMPLE_STACK_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002335 OVERFLOW_CHECK_u64(array);
2336 sz = *array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002337
2338 data->user_stack.offset = ((char *)(array - 1)
2339 - (char *) event);
2340
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002341 if (!sz) {
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002342 data->user_stack.size = 0;
2343 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002344 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002345 data->user_stack.data = (char *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002346 array = (void *)array + sz;
2347 OVERFLOW_CHECK_u64(array);
Adrian Hunter54bd2692013-07-04 16:20:34 +03002348 data->user_stack.size = *array++;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +02002349 if (WARN_ONCE(data->user_stack.size > sz,
2350 "user stack dump failure\n"))
2351 return -EFAULT;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002352 }
2353 }
2354
Kan Liangea8d0ed2021-02-02 12:09:09 -08002355 if (type & PERF_SAMPLE_WEIGHT_TYPE) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002356 OVERFLOW_CHECK_u64(array);
Kan Liangfbefe9c2021-02-05 08:01:52 -08002357 arch_perf_parse_sample_weight(data, array, type);
Andi Kleen05484292013-01-24 16:10:29 +01002358 array++;
2359 }
2360
Stephane Eranian98a3b322013-01-24 16:10:35 +01002361 if (type & PERF_SAMPLE_DATA_SRC) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002362 OVERFLOW_CHECK_u64(array);
Stephane Eranian98a3b322013-01-24 16:10:35 +01002363 data->data_src = *array;
2364 array++;
2365 }
2366
Andi Kleen475eeab2013-09-20 07:40:43 -07002367 if (type & PERF_SAMPLE_TRANSACTION) {
Adrian Hunter87b95522013-11-01 15:51:36 +02002368 OVERFLOW_CHECK_u64(array);
Andi Kleen475eeab2013-09-20 07:40:43 -07002369 data->transaction = *array;
2370 array++;
2371 }
2372
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002373 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
2374 if (type & PERF_SAMPLE_REGS_INTR) {
2375 OVERFLOW_CHECK_u64(array);
2376 data->intr_regs.abi = *array;
2377 array++;
2378
2379 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002380 u64 mask = evsel->core.attr.sample_regs_intr;
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002381
Mao Han3a5b64f2019-04-10 16:16:43 +08002382 sz = hweight64(mask) * sizeof(u64);
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002383 OVERFLOW_CHECK(array, sz, max_size);
2384 data->intr_regs.mask = mask;
2385 data->intr_regs.regs = (u64 *)array;
2386 array = (void *)array + sz;
2387 }
2388 }
2389
Kan Liang3b0a5da2017-08-29 13:11:08 -04002390 data->phys_addr = 0;
2391 if (type & PERF_SAMPLE_PHYS_ADDR) {
2392 data->phys_addr = *array;
2393 array++;
2394 }
2395
Namhyung Kimba78c1c2020-03-25 21:45:30 +09002396 data->cgroup = 0;
2397 if (type & PERF_SAMPLE_CGROUP) {
2398 data->cgroup = *array;
2399 array++;
2400 }
2401
Kan Liang542b88f2020-11-30 09:27:53 -08002402 data->data_page_size = 0;
2403 if (type & PERF_SAMPLE_DATA_PAGE_SIZE) {
2404 data->data_page_size = *array;
2405 array++;
2406 }
2407
Kan Liangc1de7f32021-01-05 11:57:49 -08002408 data->code_page_size = 0;
2409 if (type & PERF_SAMPLE_CODE_PAGE_SIZE) {
2410 data->code_page_size = *array;
2411 array++;
2412 }
2413
Adrian Hunter98dcf142019-11-15 14:42:11 +02002414 if (type & PERF_SAMPLE_AUX) {
2415 OVERFLOW_CHECK_u64(array);
2416 sz = *array++;
2417
2418 OVERFLOW_CHECK(array, sz, max_size);
2419 /* Undo swap of data */
2420 if (swapped)
2421 mem_bswap_64((char *)array, sz);
2422 data->aux_sample.size = sz;
2423 data->aux_sample.data = (char *)array;
2424 array = (void *)array + sz;
2425 }
2426
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002427 return 0;
2428}
Andrew Vagin74eec262011-11-28 12:03:31 +03002429
Arnaldo Carvalho de Melo6b6017a2020-04-30 11:03:49 -03002430int evsel__parse_sample_timestamp(struct evsel *evsel, union perf_event *event,
2431 u64 *timestamp)
Jiri Olsa01468122017-08-03 13:10:28 +02002432{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002433 u64 type = evsel->core.attr.sample_type;
Jiri Olsab1fcd192019-08-25 20:17:52 +02002434 const __u64 *array;
Jiri Olsa01468122017-08-03 13:10:28 +02002435
2436 if (!(type & PERF_SAMPLE_TIME))
2437 return -1;
2438
2439 if (event->header.type != PERF_RECORD_SAMPLE) {
2440 struct perf_sample data = {
2441 .time = -1ULL,
2442 };
2443
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002444 if (!evsel->core.attr.sample_id_all)
Jiri Olsa01468122017-08-03 13:10:28 +02002445 return -1;
2446 if (perf_evsel__parse_id_sample(evsel, event, &data))
2447 return -1;
2448
2449 *timestamp = data.time;
2450 return 0;
2451 }
2452
2453 array = event->sample.array;
2454
2455 if (perf_event__check_size(event, evsel->sample_size))
2456 return -EFAULT;
2457
2458 if (type & PERF_SAMPLE_IDENTIFIER)
2459 array++;
2460
2461 if (type & PERF_SAMPLE_IP)
2462 array++;
2463
2464 if (type & PERF_SAMPLE_TID)
2465 array++;
2466
2467 if (type & PERF_SAMPLE_TIME)
2468 *timestamp = *array;
2469
2470 return 0;
2471}
2472
Arnaldo Carvalho de Meloefc0cdc2020-04-29 16:26:57 -03002473struct tep_format_field *evsel__field(struct evsel *evsel, const char *name)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002474{
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04002475 return tep_find_field(evsel->tp_format, name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002476}
2477
Arnaldo Carvalho de Meloefc0cdc2020-04-29 16:26:57 -03002478void *evsel__rawptr(struct evsel *evsel, struct perf_sample *sample, const char *name)
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002479{
Arnaldo Carvalho de Meloefc0cdc2020-04-29 16:26:57 -03002480 struct tep_format_field *field = evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002481 int offset;
2482
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002483 if (!field)
2484 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002485
2486 offset = field->offset;
2487
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04002488 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002489 offset = *(int *)(sample->raw_data + field->offset);
2490 offset &= 0xffff;
2491 }
2492
2493 return sample->raw_data + offset;
2494}
2495
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04002496u64 format_field__intval(struct tep_format_field *field, struct perf_sample *sample,
Arnaldo Carvalho de Melo90525172016-05-31 12:47:46 -03002497 bool needs_swap)
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002498{
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002499 u64 value;
Arnaldo Carvalho de Melo90525172016-05-31 12:47:46 -03002500 void *ptr = sample->raw_data + field->offset;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002501
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002502 switch (field->size) {
2503 case 1:
2504 return *(u8 *)ptr;
2505 case 2:
2506 value = *(u16 *)ptr;
2507 break;
2508 case 4:
2509 value = *(u32 *)ptr;
2510 break;
2511 case 8:
David Aherne94eeda2015-03-24 16:14:09 -04002512 memcpy(&value, ptr, sizeof(u64));
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002513 break;
2514 default:
2515 return 0;
2516 }
2517
Arnaldo Carvalho de Melo90525172016-05-31 12:47:46 -03002518 if (!needs_swap)
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002519 return value;
2520
2521 switch (field->size) {
2522 case 2:
2523 return bswap_16(value);
2524 case 4:
2525 return bswap_32(value);
2526 case 8:
2527 return bswap_64(value);
2528 default:
2529 return 0;
2530 }
2531
2532 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002533}
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002534
Arnaldo Carvalho de Meloefc0cdc2020-04-29 16:26:57 -03002535u64 evsel__intval(struct evsel *evsel, struct perf_sample *sample, const char *name)
Arnaldo Carvalho de Melo90525172016-05-31 12:47:46 -03002536{
Arnaldo Carvalho de Meloefc0cdc2020-04-29 16:26:57 -03002537 struct tep_format_field *field = evsel__field(evsel, name);
Arnaldo Carvalho de Melo90525172016-05-31 12:47:46 -03002538
2539 if (!field)
2540 return 0;
2541
2542 return field ? format_field__intval(field, sample, evsel->needs_swap) : 0;
2543}
2544
Arnaldo Carvalho de Meloae430892020-04-30 11:46:15 -03002545bool evsel__fallback(struct evsel *evsel, int err, char *msg, size_t msgsize)
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002546{
Arnaldo Carvalho de Melo08094822016-05-12 16:07:47 -03002547 int paranoid;
2548
David Ahern2b821cc2013-07-18 17:27:59 -06002549 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002550 evsel->core.attr.type == PERF_TYPE_HARDWARE &&
2551 evsel->core.attr.config == PERF_COUNT_HW_CPU_CYCLES) {
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002552 /*
2553 * If it's cycles then fall back to hrtimer based
2554 * cpu-clock-tick sw counter, which is always available even if
2555 * no PMU support.
2556 *
2557 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2558 * b0a873e).
2559 */
2560 scnprintf(msg, msgsize, "%s",
2561"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2562
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002563 evsel->core.attr.type = PERF_TYPE_SOFTWARE;
2564 evsel->core.attr.config = PERF_COUNT_SW_CPU_CLOCK;
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002565
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03002566 zfree(&evsel->name);
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002567 return true;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002568 } else if (err == EACCES && !evsel->core.attr.exclude_kernel &&
Arnaldo Carvalho de Melo08094822016-05-12 16:07:47 -03002569 (paranoid = perf_event_paranoid()) > 1) {
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -03002570 const char *name = evsel__name(evsel);
Arnaldo Carvalho de Melo08094822016-05-12 16:07:47 -03002571 char *new_name;
Jiri Olsa129193b2018-04-23 11:08:17 +02002572 const char *sep = ":";
Arnaldo Carvalho de Melo08094822016-05-12 16:07:47 -03002573
Stephane Eranianbec49a92020-04-14 09:15:50 -07002574 /* If event has exclude user then don't exclude kernel. */
2575 if (evsel->core.attr.exclude_user)
2576 return false;
2577
Jiri Olsa129193b2018-04-23 11:08:17 +02002578 /* Is there already the separator in the name. */
2579 if (strchr(name, '/') ||
Stephane Eranian70943492020-05-05 11:29:43 -07002580 (strchr(name, ':') && !evsel->is_libpfm_event))
Jiri Olsa129193b2018-04-23 11:08:17 +02002581 sep = "";
2582
2583 if (asprintf(&new_name, "%s%su", name, sep) < 0)
Arnaldo Carvalho de Melo08094822016-05-12 16:07:47 -03002584 return false;
2585
2586 if (evsel->name)
2587 free(evsel->name);
2588 evsel->name = new_name;
Stephane Eranian4ec8d982019-09-20 16:03:56 -07002589 scnprintf(msg, msgsize, "kernel.perf_event_paranoid=%d, trying "
2590 "to fall back to excluding kernel and hypervisor "
2591 " samples", paranoid);
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002592 evsel->core.attr.exclude_kernel = 1;
Stephane Eranian4ec8d982019-09-20 16:03:56 -07002593 evsel->core.attr.exclude_hv = 1;
Arnaldo Carvalho de Melo08094822016-05-12 16:07:47 -03002594
2595 return true;
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002596 }
2597
2598 return false;
2599}
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002600
Arnaldo Carvalho de Melo2157f6e2017-06-20 12:05:38 -03002601static bool find_process(const char *name)
2602{
2603 size_t len = strlen(name);
2604 DIR *dir;
2605 struct dirent *d;
2606 int ret = -1;
2607
2608 dir = opendir(procfs__mountpoint());
2609 if (!dir)
2610 return false;
2611
2612 /* Walk through the directory. */
2613 while (ret && (d = readdir(dir)) != NULL) {
2614 char path[PATH_MAX];
2615 char *data;
2616 size_t size;
2617
2618 if ((d->d_type != DT_DIR) ||
2619 !strcmp(".", d->d_name) ||
2620 !strcmp("..", d->d_name))
2621 continue;
2622
2623 scnprintf(path, sizeof(path), "%s/%s/comm",
2624 procfs__mountpoint(), d->d_name);
2625
2626 if (filename__read_str(path, &data, &size))
2627 continue;
2628
2629 ret = strncmp(name, data, len);
2630 free(data);
2631 }
2632
2633 closedir(dir);
2634 return ret ? false : true;
2635}
2636
Arnaldo Carvalho de Melo2bb72db2020-05-04 13:43:03 -03002637int evsel__open_strerror(struct evsel *evsel, struct target *target,
2638 int err, char *msg, size_t size)
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002639{
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002640 char sbuf[STRERR_BUFSIZE];
Alexey Budankovc1034eb2020-04-30 10:15:57 +03002641 int printed = 0, enforced = 0;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002642
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002643 switch (err) {
2644 case EPERM:
2645 case EACCES:
Alexey Budankovc1034eb2020-04-30 10:15:57 +03002646 printed += scnprintf(msg + printed, size - printed,
2647 "Access to performance monitoring and observability operations is limited.\n");
2648
2649 if (!sysfs__read_int("fs/selinux/enforce", &enforced)) {
2650 if (enforced) {
2651 printed += scnprintf(msg + printed, size - printed,
2652 "Enforced MAC policy settings (SELinux) can limit access to performance\n"
2653 "monitoring and observability operations. Inspect system audit records for\n"
2654 "more perf_event access control information and adjusting the policy.\n");
2655 }
2656 }
2657
Jin Yao32ccb132017-04-07 20:08:52 +08002658 if (err == EPERM)
Alexey Budankovc1034eb2020-04-30 10:15:57 +03002659 printed += scnprintf(msg, size,
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -03002660 "No permission to enable %s event.\n\n", evsel__name(evsel));
Jin Yao32ccb132017-04-07 20:08:52 +08002661
2662 return scnprintf(msg + printed, size - printed,
Alexey Budankovc1034eb2020-04-30 10:15:57 +03002663 "Consider adjusting /proc/sys/kernel/perf_event_paranoid setting to open\n"
Alexey Budankov4b0297e2020-08-05 10:31:20 +03002664 "access to performance monitoring and observability operations for processes\n"
2665 "without CAP_PERFMON, CAP_SYS_PTRACE or CAP_SYS_ADMIN Linux capability.\n"
2666 "More information can be found at 'Perf events and tool security' document:\n"
2667 "https://www.kernel.org/doc/html/latest/admin-guide/perf-security.html\n"
Alexey Budankovc1034eb2020-04-30 10:15:57 +03002668 "perf_event_paranoid setting is %d:\n"
Ben Hutchings3379e0c2016-01-19 21:35:15 +00002669 " -1: Allow use of (almost) all events by all users\n"
Konstantin Khlebnikovac0bb6b2017-08-20 14:39:20 +03002670 " Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK\n"
Alexey Budankovc1034eb2020-04-30 10:15:57 +03002671 ">= 0: Disallow raw and ftrace function tracepoint access\n"
2672 ">= 1: Disallow CPU event access\n"
2673 ">= 2: Disallow kernel profiling\n"
2674 "To make the adjusted perf_event_paranoid setting permanent preserve it\n"
2675 "in /etc/sysctl.conf (e.g. kernel.perf_event_paranoid = <setting>)",
2676 perf_event_paranoid());
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002677 case ENOENT:
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -03002678 return scnprintf(msg, size, "The %s event is not supported.", evsel__name(evsel));
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002679 case EMFILE:
2680 return scnprintf(msg, size, "%s",
2681 "Too many events are opened.\n"
Jiri Olsa18ffdfe2015-05-25 22:51:54 +02002682 "Probably the maximum number of open file descriptors has been reached.\n"
2683 "Hint: Try again after reducing the number of events.\n"
2684 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
Arnaldo Carvalho de Melode46d522016-04-27 17:51:45 -03002685 case ENOMEM:
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -03002686 if (evsel__has_callchain(evsel) &&
Arnaldo Carvalho de Melode46d522016-04-27 17:51:45 -03002687 access("/proc/sys/kernel/perf_event_max_stack", F_OK) == 0)
2688 return scnprintf(msg, size,
2689 "Not enough memory to setup event with callchain.\n"
2690 "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n"
Arnaldo Carvalho de Melo029c75e2018-05-17 16:31:32 -03002691 "Hint: Current value: %d", sysctl__max_stack());
Arnaldo Carvalho de Melode46d522016-04-27 17:51:45 -03002692 break;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002693 case ENODEV:
2694 if (target->cpu_list)
2695 return scnprintf(msg, size, "%s",
Arnaldo Carvalho de Melo81d64f42016-04-27 17:56:53 -03002696 "No such device - did you specify an out-of-range profile CPU?");
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002697 break;
2698 case EOPNOTSUPP:
Adrian Hunter2c9a11a2020-07-10 18:10:55 +03002699 if (evsel->core.attr.aux_output)
2700 return scnprintf(msg, size,
2701 "%s: PMU Hardware doesn't support 'aux_output' feature",
2702 evsel__name(evsel));
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002703 if (evsel->core.attr.sample_period != 0)
Kim Phillips114bc192017-11-14 15:04:52 -06002704 return scnprintf(msg, size,
2705 "%s: PMU Hardware doesn't support sampling/overflow-interrupts. Try 'perf stat'",
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -03002706 evsel__name(evsel));
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002707 if (evsel->core.attr.precise_ip)
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002708 return scnprintf(msg, size, "%s",
2709 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2710#if defined(__i386__) || defined(__x86_64__)
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002711 if (evsel->core.attr.type == PERF_TYPE_HARDWARE)
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002712 return scnprintf(msg, size, "%s",
Andi Kleenccbb6af2018-04-06 13:38:12 -07002713 "No hardware sampling interrupt available.\n");
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002714#endif
2715 break;
Jiri Olsa63914ac2014-08-01 17:46:54 +02002716 case EBUSY:
2717 if (find_process("oprofiled"))
2718 return scnprintf(msg, size,
2719 "The PMU counters are busy/taken by another profiler.\n"
2720 "We found oprofile daemon running, please stop it and try again.");
2721 break;
Peter Zijlstra814c8c32015-03-31 00:19:31 +02002722 case EINVAL:
Kan Liangc1de7f32021-01-05 11:57:49 -08002723 if (evsel->core.attr.sample_type & PERF_SAMPLE_CODE_PAGE_SIZE && perf_missing_features.code_page_size)
2724 return scnprintf(msg, size, "Asking for the code page size isn't supported by this kernel.");
Arnaldo Carvalho de Melo456ef4c12020-12-07 14:04:05 -03002725 if (evsel->core.attr.sample_type & PERF_SAMPLE_DATA_PAGE_SIZE && perf_missing_features.data_page_size)
2726 return scnprintf(msg, size, "Asking for the data page size isn't supported by this kernel.");
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002727 if (evsel->core.attr.write_backward && perf_missing_features.write_backward)
Arnaldo Carvalho de Melo7da36e92016-06-20 10:47:18 +00002728 return scnprintf(msg, size, "Reading from overwrite event is not supported by this kernel.");
Peter Zijlstra814c8c32015-03-31 00:19:31 +02002729 if (perf_missing_features.clockid)
2730 return scnprintf(msg, size, "clockid feature not supported.");
2731 if (perf_missing_features.clockid_wrong)
2732 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
Arnaldo Carvalho de Meloacb9f2d2019-08-13 11:06:38 -03002733 if (perf_missing_features.aux_output)
2734 return scnprintf(msg, size, "The 'aux_output' feature is not supported, update the kernel.");
Peter Zijlstra814c8c32015-03-31 00:19:31 +02002735 break;
Kan Liang2a57d402021-02-02 12:09:06 -08002736 case ENODATA:
2737 return scnprintf(msg, size, "Cannot collect data source with the load latency event alone. "
2738 "Please add an auxiliary event in front of the load latency event.");
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002739 default:
2740 break;
2741 }
2742
2743 return scnprintf(msg, size,
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002744 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
Andi Kleenec394842018-04-06 13:38:11 -07002745 "/bin/dmesg | grep -i perf may provide additional information.\n",
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -03002746 err, str_error_r(err, sbuf, sizeof(sbuf)), evsel__name(evsel));
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002747}
Ravi Bangoriaf4e47f92016-06-30 11:44:19 +05302748
Arnaldo Carvalho de Melo6e6d1d62020-05-04 13:44:03 -03002749struct perf_env *evsel__env(struct evsel *evsel)
Jin Yao69fb09f2017-07-07 13:06:34 +08002750{
Arnaldo Carvalho de Melo5449f132017-12-11 12:46:11 -03002751 if (evsel && evsel->evlist)
2752 return evsel->evlist->env;
Arnaldo Carvalho de Melo9db0e362019-09-30 11:48:32 -03002753 return &perf_env;
Jin Yao69fb09f2017-07-07 13:06:34 +08002754}
Jiri Olsa650d6222018-08-30 08:32:16 +02002755
Jiri Olsa63503db2019-07-21 13:23:52 +02002756static int store_evsel_ids(struct evsel *evsel, struct evlist *evlist)
Jiri Olsa650d6222018-08-30 08:32:16 +02002757{
2758 int cpu, thread;
2759
Jiri Olsa9dfcb752019-07-21 13:24:45 +02002760 for (cpu = 0; cpu < xyarray__max_x(evsel->core.fd); cpu++) {
2761 for (thread = 0; thread < xyarray__max_y(evsel->core.fd);
Jiri Olsa650d6222018-08-30 08:32:16 +02002762 thread++) {
2763 int fd = FD(evsel, cpu, thread);
2764
Jiri Olsad5a99482019-09-03 11:19:56 +02002765 if (perf_evlist__id_add_fd(&evlist->core, &evsel->core,
Jiri Olsa650d6222018-08-30 08:32:16 +02002766 cpu, thread, fd) < 0)
2767 return -1;
2768 }
2769 }
2770
2771 return 0;
2772}
2773
Arnaldo Carvalho de Melo34397752020-05-04 13:45:19 -03002774int evsel__store_ids(struct evsel *evsel, struct evlist *evlist)
Jiri Olsa650d6222018-08-30 08:32:16 +02002775{
Jiri Olsad400bd32019-07-21 13:24:37 +02002776 struct perf_cpu_map *cpus = evsel->core.cpus;
Jiri Olsaaf663bd2019-07-21 13:24:39 +02002777 struct perf_thread_map *threads = evsel->core.threads;
Jiri Olsa650d6222018-08-30 08:32:16 +02002778
Jiri Olsa70c20362019-09-03 10:34:29 +02002779 if (perf_evsel__alloc_id(&evsel->core, cpus->nr, threads->nr))
Jiri Olsa650d6222018-08-30 08:32:16 +02002780 return -ENOMEM;
2781
2782 return store_evsel_ids(evsel, evlist);
2783}