]>
Commit | Line | Data |
---|---|---|
f8a95309 ACM |
1 | /* |
2 | * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> | |
3 | * | |
4 | * Parts came from builtin-{top,stat,record}.c, see those files for further | |
5 | * copyright notes. | |
6 | * | |
7 | * Released under the GPL v2. (and only v2, not any later version) | |
8 | */ | |
9 | ||
936be503 | 10 | #include <byteswap.h> |
a43783ae | 11 | #include <errno.h> |
fd20e811 | 12 | #include <inttypes.h> |
0f6a3015 | 13 | #include <linux/bitops.h> |
2157f6ee | 14 | #include <api/fs/fs.h> |
4605eab3 | 15 | #include <api/fs/tracing_path.h> |
4e319027 RR |
16 | #include <traceevent/event-parse.h> |
17 | #include <linux/hw_breakpoint.h> | |
18 | #include <linux/perf_event.h> | |
0353631a | 19 | #include <linux/compiler.h> |
8dd2a131 | 20 | #include <linux/err.h> |
86a5e0c2 | 21 | #include <sys/ioctl.h> |
bec19672 | 22 | #include <sys/resource.h> |
2157f6ee ACM |
23 | #include <sys/types.h> |
24 | #include <dirent.h> | |
4e319027 | 25 | #include "asm/bug.h" |
8f651eae | 26 | #include "callchain.h" |
f14d5707 | 27 | #include "cgroup.h" |
5ab8c689 | 28 | #include "event.h" |
69aad6f1 | 29 | #include "evsel.h" |
70082dd9 | 30 | #include "evlist.h" |
69aad6f1 | 31 | #include "util.h" |
86bd5e86 | 32 | #include "cpumap.h" |
fd78260b | 33 | #include "thread_map.h" |
12864b31 | 34 | #include "target.h" |
26d33022 | 35 | #include "perf_regs.h" |
e3e1a54f | 36 | #include "debug.h" |
97978b3e | 37 | #include "trace-event.h" |
a9a3a4d9 | 38 | #include "stat.h" |
f69d9d78 | 39 | #include "memswap.h" |
ac12f676 | 40 | #include "util/parse-branch-options.h" |
69aad6f1 | 41 | |
3d689ed6 ACM |
42 | #include "sane_ctype.h" |
43 | ||
594ac61a ACM |
44 | static struct { |
45 | bool sample_id_all; | |
46 | bool exclude_guest; | |
5c5e854b | 47 | bool mmap2; |
57480d2c | 48 | bool cloexec; |
814c8c38 PZ |
49 | bool clockid; |
50 | bool clockid_wrong; | |
bd0f8895 | 51 | bool lbr_flags; |
b90dc17a | 52 | bool write_backward; |
82bf311e | 53 | bool group_read; |
594ac61a ACM |
54 | } perf_missing_features; |
55 | ||
814c8c38 PZ |
56 | static clockid_t clockid; |
57 | ||
ce8ccff5 ACM |
58 | static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused) |
59 | { | |
60 | return 0; | |
61 | } | |
62 | ||
10213e2f JO |
63 | void __weak test_attr__ready(void) { } |
64 | ||
ce8ccff5 ACM |
65 | static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused) |
66 | { | |
67 | } | |
68 | ||
69 | static struct { | |
70 | size_t size; | |
71 | int (*init)(struct perf_evsel *evsel); | |
72 | void (*fini)(struct perf_evsel *evsel); | |
73 | } perf_evsel__object = { | |
74 | .size = sizeof(struct perf_evsel), | |
75 | .init = perf_evsel__no_extra_init, | |
76 | .fini = perf_evsel__no_extra_fini, | |
77 | }; | |
78 | ||
79 | int perf_evsel__object_config(size_t object_size, | |
80 | int (*init)(struct perf_evsel *evsel), | |
81 | void (*fini)(struct perf_evsel *evsel)) | |
82 | { | |
83 | ||
84 | if (object_size == 0) | |
85 | goto set_methods; | |
86 | ||
87 | if (perf_evsel__object.size > object_size) | |
88 | return -EINVAL; | |
89 | ||
90 | perf_evsel__object.size = object_size; | |
91 | ||
92 | set_methods: | |
93 | if (init != NULL) | |
94 | perf_evsel__object.init = init; | |
95 | ||
96 | if (fini != NULL) | |
97 | perf_evsel__object.fini = fini; | |
98 | ||
99 | return 0; | |
100 | } | |
101 | ||
c52b12ed ACM |
102 | #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) |
103 | ||
75562573 | 104 | int __perf_evsel__sample_size(u64 sample_type) |
c2a70653 ACM |
105 | { |
106 | u64 mask = sample_type & PERF_SAMPLE_MASK; | |
107 | int size = 0; | |
108 | int i; | |
109 | ||
110 | for (i = 0; i < 64; i++) { | |
111 | if (mask & (1ULL << i)) | |
112 | size++; | |
113 | } | |
114 | ||
115 | size *= sizeof(u64); | |
116 | ||
117 | return size; | |
118 | } | |
119 | ||
75562573 AH |
120 | /** |
121 | * __perf_evsel__calc_id_pos - calculate id_pos. | |
122 | * @sample_type: sample type | |
123 | * | |
124 | * This function returns the position of the event id (PERF_SAMPLE_ID or | |
125 | * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct | |
126 | * sample_event. | |
127 | */ | |
128 | static int __perf_evsel__calc_id_pos(u64 sample_type) | |
129 | { | |
130 | int idx = 0; | |
131 | ||
132 | if (sample_type & PERF_SAMPLE_IDENTIFIER) | |
133 | return 0; | |
134 | ||
135 | if (!(sample_type & PERF_SAMPLE_ID)) | |
136 | return -1; | |
137 | ||
138 | if (sample_type & PERF_SAMPLE_IP) | |
139 | idx += 1; | |
140 | ||
141 | if (sample_type & PERF_SAMPLE_TID) | |
142 | idx += 1; | |
143 | ||
144 | if (sample_type & PERF_SAMPLE_TIME) | |
145 | idx += 1; | |
146 | ||
147 | if (sample_type & PERF_SAMPLE_ADDR) | |
148 | idx += 1; | |
149 | ||
150 | return idx; | |
151 | } | |
152 | ||
153 | /** | |
154 | * __perf_evsel__calc_is_pos - calculate is_pos. | |
155 | * @sample_type: sample type | |
156 | * | |
157 | * This function returns the position (counting backwards) of the event id | |
158 | * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if | |
159 | * sample_id_all is used there is an id sample appended to non-sample events. | |
160 | */ | |
161 | static int __perf_evsel__calc_is_pos(u64 sample_type) | |
162 | { | |
163 | int idx = 1; | |
164 | ||
165 | if (sample_type & PERF_SAMPLE_IDENTIFIER) | |
166 | return 1; | |
167 | ||
168 | if (!(sample_type & PERF_SAMPLE_ID)) | |
169 | return -1; | |
170 | ||
171 | if (sample_type & PERF_SAMPLE_CPU) | |
172 | idx += 1; | |
173 | ||
174 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
175 | idx += 1; | |
176 | ||
177 | return idx; | |
178 | } | |
179 | ||
180 | void perf_evsel__calc_id_pos(struct perf_evsel *evsel) | |
181 | { | |
182 | evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type); | |
183 | evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type); | |
184 | } | |
185 | ||
7be5ebe8 ACM |
186 | void __perf_evsel__set_sample_bit(struct perf_evsel *evsel, |
187 | enum perf_event_sample_format bit) | |
188 | { | |
189 | if (!(evsel->attr.sample_type & bit)) { | |
190 | evsel->attr.sample_type |= bit; | |
191 | evsel->sample_size += sizeof(u64); | |
75562573 | 192 | perf_evsel__calc_id_pos(evsel); |
7be5ebe8 ACM |
193 | } |
194 | } | |
195 | ||
196 | void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel, | |
197 | enum perf_event_sample_format bit) | |
198 | { | |
199 | if (evsel->attr.sample_type & bit) { | |
200 | evsel->attr.sample_type &= ~bit; | |
201 | evsel->sample_size -= sizeof(u64); | |
75562573 | 202 | perf_evsel__calc_id_pos(evsel); |
7be5ebe8 ACM |
203 | } |
204 | } | |
205 | ||
75562573 AH |
206 | void perf_evsel__set_sample_id(struct perf_evsel *evsel, |
207 | bool can_sample_identifier) | |
7a5a5ca5 | 208 | { |
75562573 AH |
209 | if (can_sample_identifier) { |
210 | perf_evsel__reset_sample_bit(evsel, ID); | |
211 | perf_evsel__set_sample_bit(evsel, IDENTIFIER); | |
212 | } else { | |
213 | perf_evsel__set_sample_bit(evsel, ID); | |
214 | } | |
7a5a5ca5 ACM |
215 | evsel->attr.read_format |= PERF_FORMAT_ID; |
216 | } | |
217 | ||
5496bc0c ACM |
218 | /** |
219 | * perf_evsel__is_function_event - Return whether given evsel is a function | |
220 | * trace event | |
221 | * | |
222 | * @evsel - evsel selector to be tested | |
223 | * | |
224 | * Return %true if event is function trace event | |
225 | */ | |
226 | bool perf_evsel__is_function_event(struct perf_evsel *evsel) | |
227 | { | |
228 | #define FUNCTION_EVENT "ftrace:function" | |
229 | ||
230 | return evsel->name && | |
231 | !strncmp(FUNCTION_EVENT, evsel->name, sizeof(FUNCTION_EVENT)); | |
232 | ||
233 | #undef FUNCTION_EVENT | |
234 | } | |
235 | ||
ef1d1af2 ACM |
236 | void perf_evsel__init(struct perf_evsel *evsel, |
237 | struct perf_event_attr *attr, int idx) | |
238 | { | |
239 | evsel->idx = idx; | |
60b0896c | 240 | evsel->tracking = !idx; |
ef1d1af2 | 241 | evsel->attr = *attr; |
2cfda562 | 242 | evsel->leader = evsel; |
410136f5 SE |
243 | evsel->unit = ""; |
244 | evsel->scale = 1.0; | |
d49e4695 | 245 | evsel->evlist = NULL; |
1f45b1d4 | 246 | evsel->bpf_fd = -1; |
ef1d1af2 | 247 | INIT_LIST_HEAD(&evsel->node); |
930a2e29 | 248 | INIT_LIST_HEAD(&evsel->config_terms); |
ce8ccff5 | 249 | perf_evsel__object.init(evsel); |
bde09467 | 250 | evsel->sample_size = __perf_evsel__sample_size(attr->sample_type); |
75562573 | 251 | perf_evsel__calc_id_pos(evsel); |
15bfd2cc | 252 | evsel->cmdline_group_boundary = false; |
37932c18 | 253 | evsel->metric_expr = NULL; |
96284814 | 254 | evsel->metric_name = NULL; |
37932c18 AK |
255 | evsel->metric_events = NULL; |
256 | evsel->collect_stat = false; | |
ef1d1af2 ACM |
257 | } |
258 | ||
ef503831 | 259 | struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx) |
69aad6f1 | 260 | { |
ce8ccff5 | 261 | struct perf_evsel *evsel = zalloc(perf_evsel__object.size); |
69aad6f1 | 262 | |
77123503 HT |
263 | if (!evsel) |
264 | return NULL; | |
265 | perf_evsel__init(evsel, attr, idx); | |
69aad6f1 | 266 | |
03e0a7df | 267 | if (perf_evsel__is_bpf_output(evsel)) { |
d37ba880 WN |
268 | evsel->attr.sample_type |= (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | |
269 | PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD), | |
03e0a7df WN |
270 | evsel->attr.sample_period = 1; |
271 | } | |
272 | ||
69aad6f1 ACM |
273 | return evsel; |
274 | } | |
275 | ||
f1e52f14 ACM |
276 | static bool perf_event_can_profile_kernel(void) |
277 | { | |
278 | return geteuid() == 0 || perf_event_paranoid() == -1; | |
279 | } | |
280 | ||
30269dc1 | 281 | struct perf_evsel *perf_evsel__new_cycles(bool precise) |
7c48dcfd ACM |
282 | { |
283 | struct perf_event_attr attr = { | |
284 | .type = PERF_TYPE_HARDWARE, | |
285 | .config = PERF_COUNT_HW_CPU_CYCLES, | |
f1e52f14 | 286 | .exclude_kernel = !perf_event_can_profile_kernel(), |
7c48dcfd ACM |
287 | }; |
288 | struct perf_evsel *evsel; | |
289 | ||
290 | event_attr_init(&attr); | |
30269dc1 ACM |
291 | |
292 | if (!precise) | |
293 | goto new_event; | |
7a1ac110 ACM |
294 | /* |
295 | * Unnamed union member, not supported as struct member named | |
296 | * initializer in older compilers such as gcc 4.4.7 | |
297 | * | |
298 | * Just for probing the precise_ip: | |
299 | */ | |
300 | attr.sample_period = 1; | |
7c48dcfd ACM |
301 | |
302 | perf_event_attr__set_max_precise_ip(&attr); | |
7a1ac110 ACM |
303 | /* |
304 | * Now let the usual logic to set up the perf_event_attr defaults | |
305 | * to kick in when we return and before perf_evsel__open() is called. | |
306 | */ | |
307 | attr.sample_period = 0; | |
30269dc1 | 308 | new_event: |
7c48dcfd ACM |
309 | evsel = perf_evsel__new(&attr); |
310 | if (evsel == NULL) | |
311 | goto out; | |
312 | ||
313 | /* use asprintf() because free(evsel) assumes name is allocated */ | |
ede5626d ACM |
314 | if (asprintf(&evsel->name, "cycles%s%s%.*s", |
315 | (attr.precise_ip || attr.exclude_kernel) ? ":" : "", | |
316 | attr.exclude_kernel ? "u" : "", | |
317 | attr.precise_ip ? attr.precise_ip + 1 : 0, "ppp") < 0) | |
7c48dcfd ACM |
318 | goto error_free; |
319 | out: | |
320 | return evsel; | |
321 | error_free: | |
322 | perf_evsel__delete(evsel); | |
323 | evsel = NULL; | |
324 | goto out; | |
325 | } | |
326 | ||
8dd2a131 JO |
327 | /* |
328 | * Returns pointer with encoded error via <linux/err.h> interface. | |
329 | */ | |
ef503831 | 330 | struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx) |
efd2b924 | 331 | { |
ce8ccff5 | 332 | struct perf_evsel *evsel = zalloc(perf_evsel__object.size); |
8dd2a131 | 333 | int err = -ENOMEM; |
efd2b924 | 334 | |
8dd2a131 JO |
335 | if (evsel == NULL) { |
336 | goto out_err; | |
337 | } else { | |
efd2b924 | 338 | struct perf_event_attr attr = { |
0b80f8b3 ACM |
339 | .type = PERF_TYPE_TRACEPOINT, |
340 | .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | | |
341 | PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD), | |
efd2b924 ACM |
342 | }; |
343 | ||
e48ffe2b ACM |
344 | if (asprintf(&evsel->name, "%s:%s", sys, name) < 0) |
345 | goto out_free; | |
346 | ||
97978b3e | 347 | evsel->tp_format = trace_event__tp_format(sys, name); |
8dd2a131 JO |
348 | if (IS_ERR(evsel->tp_format)) { |
349 | err = PTR_ERR(evsel->tp_format); | |
efd2b924 | 350 | goto out_free; |
8dd2a131 | 351 | } |
efd2b924 | 352 | |
0b80f8b3 | 353 | event_attr_init(&attr); |
efd2b924 | 354 | attr.config = evsel->tp_format->id; |
0b80f8b3 | 355 | attr.sample_period = 1; |
efd2b924 | 356 | perf_evsel__init(evsel, &attr, idx); |
efd2b924 ACM |
357 | } |
358 | ||
359 | return evsel; | |
360 | ||
361 | out_free: | |
74cf249d | 362 | zfree(&evsel->name); |
efd2b924 | 363 | free(evsel); |
8dd2a131 JO |
364 | out_err: |
365 | return ERR_PTR(err); | |
efd2b924 ACM |
366 | } |
367 | ||
8ad7013b | 368 | const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = { |
c410431c ACM |
369 | "cycles", |
370 | "instructions", | |
371 | "cache-references", | |
372 | "cache-misses", | |
373 | "branches", | |
374 | "branch-misses", | |
375 | "bus-cycles", | |
376 | "stalled-cycles-frontend", | |
377 | "stalled-cycles-backend", | |
378 | "ref-cycles", | |
379 | }; | |
380 | ||
dd4f5223 | 381 | static const char *__perf_evsel__hw_name(u64 config) |
c410431c ACM |
382 | { |
383 | if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config]) | |
384 | return perf_evsel__hw_names[config]; | |
385 | ||
386 | return "unknown-hardware"; | |
387 | } | |
388 | ||
27f18617 | 389 | static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size) |
c410431c | 390 | { |
27f18617 | 391 | int colon = 0, r = 0; |
c410431c | 392 | struct perf_event_attr *attr = &evsel->attr; |
c410431c ACM |
393 | bool exclude_guest_default = false; |
394 | ||
395 | #define MOD_PRINT(context, mod) do { \ | |
396 | if (!attr->exclude_##context) { \ | |
27f18617 | 397 | if (!colon) colon = ++r; \ |
c410431c ACM |
398 | r += scnprintf(bf + r, size - r, "%c", mod); \ |
399 | } } while(0) | |
400 | ||
401 | if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) { | |
402 | MOD_PRINT(kernel, 'k'); | |
403 | MOD_PRINT(user, 'u'); | |
404 | MOD_PRINT(hv, 'h'); | |
405 | exclude_guest_default = true; | |
406 | } | |
407 | ||
408 | if (attr->precise_ip) { | |
409 | if (!colon) | |
27f18617 | 410 | colon = ++r; |
c410431c ACM |
411 | r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp"); |
412 | exclude_guest_default = true; | |
413 | } | |
414 | ||
415 | if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) { | |
416 | MOD_PRINT(host, 'H'); | |
417 | MOD_PRINT(guest, 'G'); | |
418 | } | |
419 | #undef MOD_PRINT | |
420 | if (colon) | |
27f18617 | 421 | bf[colon - 1] = ':'; |
c410431c ACM |
422 | return r; |
423 | } | |
424 | ||
27f18617 ACM |
425 | static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size) |
426 | { | |
427 | int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config)); | |
428 | return r + perf_evsel__add_modifiers(evsel, bf + r, size - r); | |
429 | } | |
430 | ||
8ad7013b | 431 | const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = { |
335c2f5d ACM |
432 | "cpu-clock", |
433 | "task-clock", | |
434 | "page-faults", | |
435 | "context-switches", | |
8ad7013b | 436 | "cpu-migrations", |
335c2f5d ACM |
437 | "minor-faults", |
438 | "major-faults", | |
439 | "alignment-faults", | |
440 | "emulation-faults", | |
d22d1a2a | 441 | "dummy", |
335c2f5d ACM |
442 | }; |
443 | ||
dd4f5223 | 444 | static const char *__perf_evsel__sw_name(u64 config) |
335c2f5d ACM |
445 | { |
446 | if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config]) | |
447 | return perf_evsel__sw_names[config]; | |
448 | return "unknown-software"; | |
449 | } | |
450 | ||
451 | static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size) | |
452 | { | |
453 | int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config)); | |
454 | return r + perf_evsel__add_modifiers(evsel, bf + r, size - r); | |
455 | } | |
456 | ||
287e74aa JO |
457 | static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type) |
458 | { | |
459 | int r; | |
460 | ||
461 | r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr); | |
462 | ||
463 | if (type & HW_BREAKPOINT_R) | |
464 | r += scnprintf(bf + r, size - r, "r"); | |
465 | ||
466 | if (type & HW_BREAKPOINT_W) | |
467 | r += scnprintf(bf + r, size - r, "w"); | |
468 | ||
469 | if (type & HW_BREAKPOINT_X) | |
470 | r += scnprintf(bf + r, size - r, "x"); | |
471 | ||
472 | return r; | |
473 | } | |
474 | ||
475 | static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size) | |
476 | { | |
477 | struct perf_event_attr *attr = &evsel->attr; | |
478 | int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type); | |
479 | return r + perf_evsel__add_modifiers(evsel, bf + r, size - r); | |
480 | } | |
481 | ||
0b668bc9 ACM |
482 | const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX] |
483 | [PERF_EVSEL__MAX_ALIASES] = { | |
484 | { "L1-dcache", "l1-d", "l1d", "L1-data", }, | |
485 | { "L1-icache", "l1-i", "l1i", "L1-instruction", }, | |
486 | { "LLC", "L2", }, | |
487 | { "dTLB", "d-tlb", "Data-TLB", }, | |
488 | { "iTLB", "i-tlb", "Instruction-TLB", }, | |
489 | { "branch", "branches", "bpu", "btb", "bpc", }, | |
490 | { "node", }, | |
491 | }; | |
492 | ||
493 | const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX] | |
494 | [PERF_EVSEL__MAX_ALIASES] = { | |
495 | { "load", "loads", "read", }, | |
496 | { "store", "stores", "write", }, | |
497 | { "prefetch", "prefetches", "speculative-read", "speculative-load", }, | |
498 | }; | |
499 | ||
500 | const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX] | |
501 | [PERF_EVSEL__MAX_ALIASES] = { | |
502 | { "refs", "Reference", "ops", "access", }, | |
503 | { "misses", "miss", }, | |
504 | }; | |
505 | ||
506 | #define C(x) PERF_COUNT_HW_CACHE_##x | |
507 | #define CACHE_READ (1 << C(OP_READ)) | |
508 | #define CACHE_WRITE (1 << C(OP_WRITE)) | |
509 | #define CACHE_PREFETCH (1 << C(OP_PREFETCH)) | |
510 | #define COP(x) (1 << x) | |
511 | ||
512 | /* | |
513 | * cache operartion stat | |
514 | * L1I : Read and prefetch only | |
515 | * ITLB and BPU : Read-only | |
516 | */ | |
517 | static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = { | |
518 | [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), | |
519 | [C(L1I)] = (CACHE_READ | CACHE_PREFETCH), | |
520 | [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), | |
521 | [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), | |
522 | [C(ITLB)] = (CACHE_READ), | |
523 | [C(BPU)] = (CACHE_READ), | |
524 | [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), | |
525 | }; | |
526 | ||
527 | bool perf_evsel__is_cache_op_valid(u8 type, u8 op) | |
528 | { | |
529 | if (perf_evsel__hw_cache_stat[type] & COP(op)) | |
530 | return true; /* valid */ | |
531 | else | |
532 | return false; /* invalid */ | |
533 | } | |
534 | ||
535 | int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result, | |
536 | char *bf, size_t size) | |
537 | { | |
538 | if (result) { | |
539 | return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0], | |
540 | perf_evsel__hw_cache_op[op][0], | |
541 | perf_evsel__hw_cache_result[result][0]); | |
542 | } | |
543 | ||
544 | return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0], | |
545 | perf_evsel__hw_cache_op[op][1]); | |
546 | } | |
547 | ||
dd4f5223 | 548 | static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size) |
0b668bc9 ACM |
549 | { |
550 | u8 op, result, type = (config >> 0) & 0xff; | |
551 | const char *err = "unknown-ext-hardware-cache-type"; | |
552 | ||
c53412ee | 553 | if (type >= PERF_COUNT_HW_CACHE_MAX) |
0b668bc9 ACM |
554 | goto out_err; |
555 | ||
556 | op = (config >> 8) & 0xff; | |
557 | err = "unknown-ext-hardware-cache-op"; | |
c53412ee | 558 | if (op >= PERF_COUNT_HW_CACHE_OP_MAX) |
0b668bc9 ACM |
559 | goto out_err; |
560 | ||
561 | result = (config >> 16) & 0xff; | |
562 | err = "unknown-ext-hardware-cache-result"; | |
c53412ee | 563 | if (result >= PERF_COUNT_HW_CACHE_RESULT_MAX) |
0b668bc9 ACM |
564 | goto out_err; |
565 | ||
566 | err = "invalid-cache"; | |
567 | if (!perf_evsel__is_cache_op_valid(type, op)) | |
568 | goto out_err; | |
569 | ||
570 | return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size); | |
571 | out_err: | |
572 | return scnprintf(bf, size, "%s", err); | |
573 | } | |
574 | ||
575 | static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size) | |
576 | { | |
577 | int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size); | |
578 | return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret); | |
579 | } | |
580 | ||
6eef3d9c ACM |
581 | static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size) |
582 | { | |
583 | int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config); | |
584 | return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret); | |
585 | } | |
586 | ||
7289f83c | 587 | const char *perf_evsel__name(struct perf_evsel *evsel) |
a4460836 | 588 | { |
7289f83c | 589 | char bf[128]; |
a4460836 | 590 | |
feb493e8 ACM |
591 | if (!evsel) |
592 | goto out_unknown; | |
593 | ||
7289f83c ACM |
594 | if (evsel->name) |
595 | return evsel->name; | |
c410431c ACM |
596 | |
597 | switch (evsel->attr.type) { | |
598 | case PERF_TYPE_RAW: | |
6eef3d9c | 599 | perf_evsel__raw_name(evsel, bf, sizeof(bf)); |
c410431c ACM |
600 | break; |
601 | ||
602 | case PERF_TYPE_HARDWARE: | |
7289f83c | 603 | perf_evsel__hw_name(evsel, bf, sizeof(bf)); |
c410431c | 604 | break; |
0b668bc9 ACM |
605 | |
606 | case PERF_TYPE_HW_CACHE: | |
7289f83c | 607 | perf_evsel__hw_cache_name(evsel, bf, sizeof(bf)); |
0b668bc9 ACM |
608 | break; |
609 | ||
335c2f5d | 610 | case PERF_TYPE_SOFTWARE: |
7289f83c | 611 | perf_evsel__sw_name(evsel, bf, sizeof(bf)); |
335c2f5d ACM |
612 | break; |
613 | ||
a4460836 | 614 | case PERF_TYPE_TRACEPOINT: |
7289f83c | 615 | scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint"); |
a4460836 ACM |
616 | break; |
617 | ||
287e74aa JO |
618 | case PERF_TYPE_BREAKPOINT: |
619 | perf_evsel__bp_name(evsel, bf, sizeof(bf)); | |
620 | break; | |
621 | ||
c410431c | 622 | default: |
ca1b1457 RR |
623 | scnprintf(bf, sizeof(bf), "unknown attr type: %d", |
624 | evsel->attr.type); | |
a4460836 | 625 | break; |
c410431c ACM |
626 | } |
627 | ||
7289f83c ACM |
628 | evsel->name = strdup(bf); |
629 | ||
feb493e8 ACM |
630 | if (evsel->name) |
631 | return evsel->name; | |
632 | out_unknown: | |
633 | return "unknown"; | |
c410431c ACM |
634 | } |
635 | ||
717e263f NK |
636 | const char *perf_evsel__group_name(struct perf_evsel *evsel) |
637 | { | |
638 | return evsel->group_name ?: "anon group"; | |
639 | } | |
640 | ||
641 | int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size) | |
642 | { | |
643 | int ret; | |
644 | struct perf_evsel *pos; | |
645 | const char *group_name = perf_evsel__group_name(evsel); | |
646 | ||
647 | ret = scnprintf(buf, size, "%s", group_name); | |
648 | ||
649 | ret += scnprintf(buf + ret, size - ret, " { %s", | |
650 | perf_evsel__name(evsel)); | |
651 | ||
652 | for_each_group_member(pos, evsel) | |
653 | ret += scnprintf(buf + ret, size - ret, ", %s", | |
654 | perf_evsel__name(pos)); | |
655 | ||
656 | ret += scnprintf(buf + ret, size - ret, " }"); | |
657 | ||
658 | return ret; | |
659 | } | |
660 | ||
01e0d50c ACM |
661 | void perf_evsel__config_callchain(struct perf_evsel *evsel, |
662 | struct record_opts *opts, | |
663 | struct callchain_param *param) | |
6bedfab6 JO |
664 | { |
665 | bool function = perf_evsel__is_function_event(evsel); | |
666 | struct perf_event_attr *attr = &evsel->attr; | |
667 | ||
668 | perf_evsel__set_sample_bit(evsel, CALLCHAIN); | |
669 | ||
792d48b4 ACM |
670 | attr->sample_max_stack = param->max_stack; |
671 | ||
c3a6a8c4 | 672 | if (param->record_mode == CALLCHAIN_LBR) { |
aad2b21c KL |
673 | if (!opts->branch_stack) { |
674 | if (attr->exclude_user) { | |
675 | pr_warning("LBR callstack option is only available " | |
676 | "to get user callchain information. " | |
677 | "Falling back to framepointers.\n"); | |
678 | } else { | |
679 | perf_evsel__set_sample_bit(evsel, BRANCH_STACK); | |
680 | attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER | | |
bd0f8895 AK |
681 | PERF_SAMPLE_BRANCH_CALL_STACK | |
682 | PERF_SAMPLE_BRANCH_NO_CYCLES | | |
683 | PERF_SAMPLE_BRANCH_NO_FLAGS; | |
aad2b21c KL |
684 | } |
685 | } else | |
686 | pr_warning("Cannot use LBR callstack with branch stack. " | |
687 | "Falling back to framepointers.\n"); | |
688 | } | |
689 | ||
c3a6a8c4 | 690 | if (param->record_mode == CALLCHAIN_DWARF) { |
6bedfab6 JO |
691 | if (!function) { |
692 | perf_evsel__set_sample_bit(evsel, REGS_USER); | |
693 | perf_evsel__set_sample_bit(evsel, STACK_USER); | |
84c41742 | 694 | attr->sample_regs_user |= PERF_REGS_MASK; |
c3a6a8c4 | 695 | attr->sample_stack_user = param->dump_size; |
6bedfab6 JO |
696 | attr->exclude_callchain_user = 1; |
697 | } else { | |
698 | pr_info("Cannot use DWARF unwind for function trace event," | |
699 | " falling back to framepointers.\n"); | |
700 | } | |
701 | } | |
702 | ||
703 | if (function) { | |
704 | pr_info("Disabling user space callchains for function trace event.\n"); | |
705 | attr->exclude_callchain_user = 1; | |
706 | } | |
707 | } | |
708 | ||
d457c963 KL |
709 | static void |
710 | perf_evsel__reset_callgraph(struct perf_evsel *evsel, | |
711 | struct callchain_param *param) | |
712 | { | |
713 | struct perf_event_attr *attr = &evsel->attr; | |
714 | ||
715 | perf_evsel__reset_sample_bit(evsel, CALLCHAIN); | |
716 | if (param->record_mode == CALLCHAIN_LBR) { | |
717 | perf_evsel__reset_sample_bit(evsel, BRANCH_STACK); | |
718 | attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER | | |
719 | PERF_SAMPLE_BRANCH_CALL_STACK); | |
720 | } | |
721 | if (param->record_mode == CALLCHAIN_DWARF) { | |
722 | perf_evsel__reset_sample_bit(evsel, REGS_USER); | |
723 | perf_evsel__reset_sample_bit(evsel, STACK_USER); | |
724 | } | |
725 | } | |
726 | ||
727 | static void apply_config_terms(struct perf_evsel *evsel, | |
728 | struct record_opts *opts) | |
930a2e29 JO |
729 | { |
730 | struct perf_evsel_config_term *term; | |
32067712 KL |
731 | struct list_head *config_terms = &evsel->config_terms; |
732 | struct perf_event_attr *attr = &evsel->attr; | |
3f648b1a ACM |
733 | /* callgraph default */ |
734 | struct callchain_param param = { | |
735 | .record_mode = callchain_param.record_mode, | |
736 | }; | |
d457c963 | 737 | u32 dump_size = 0; |
792d48b4 ACM |
738 | int max_stack = 0; |
739 | const char *callgraph_buf = NULL; | |
d457c963 | 740 | |
930a2e29 JO |
741 | list_for_each_entry(term, config_terms, list) { |
742 | switch (term->type) { | |
ee4c7588 | 743 | case PERF_EVSEL__CONFIG_TERM_PERIOD: |
59622fd4 AK |
744 | if (!(term->weak && opts->user_interval != ULLONG_MAX)) { |
745 | attr->sample_period = term->val.period; | |
746 | attr->freq = 0; | |
d90b13c2 | 747 | perf_evsel__reset_sample_bit(evsel, PERIOD); |
59622fd4 | 748 | } |
32067712 | 749 | break; |
09af2a55 | 750 | case PERF_EVSEL__CONFIG_TERM_FREQ: |
59622fd4 AK |
751 | if (!(term->weak && opts->user_freq != UINT_MAX)) { |
752 | attr->sample_freq = term->val.freq; | |
753 | attr->freq = 1; | |
d90b13c2 | 754 | perf_evsel__set_sample_bit(evsel, PERIOD); |
59622fd4 | 755 | } |
09af2a55 | 756 | break; |
32067712 KL |
757 | case PERF_EVSEL__CONFIG_TERM_TIME: |
758 | if (term->val.time) | |
759 | perf_evsel__set_sample_bit(evsel, TIME); | |
760 | else | |
761 | perf_evsel__reset_sample_bit(evsel, TIME); | |
762 | break; | |
d457c963 KL |
763 | case PERF_EVSEL__CONFIG_TERM_CALLGRAPH: |
764 | callgraph_buf = term->val.callgraph; | |
765 | break; | |
ac12f676 AK |
766 | case PERF_EVSEL__CONFIG_TERM_BRANCH: |
767 | if (term->val.branch && strcmp(term->val.branch, "no")) { | |
768 | perf_evsel__set_sample_bit(evsel, BRANCH_STACK); | |
769 | parse_branch_str(term->val.branch, | |
770 | &attr->branch_sample_type); | |
771 | } else | |
772 | perf_evsel__reset_sample_bit(evsel, BRANCH_STACK); | |
773 | break; | |
d457c963 KL |
774 | case PERF_EVSEL__CONFIG_TERM_STACK_USER: |
775 | dump_size = term->val.stack_user; | |
776 | break; | |
792d48b4 ACM |
777 | case PERF_EVSEL__CONFIG_TERM_MAX_STACK: |
778 | max_stack = term->val.max_stack; | |
779 | break; | |
374ce938 WN |
780 | case PERF_EVSEL__CONFIG_TERM_INHERIT: |
781 | /* | |
782 | * attr->inherit should has already been set by | |
783 | * perf_evsel__config. If user explicitly set | |
784 | * inherit using config terms, override global | |
785 | * opt->no_inherit setting. | |
786 | */ | |
787 | attr->inherit = term->val.inherit ? 1 : 0; | |
788 | break; | |
626a6b78 WN |
789 | case PERF_EVSEL__CONFIG_TERM_OVERWRITE: |
790 | attr->write_backward = term->val.overwrite ? 1 : 0; | |
791 | break; | |
930a2e29 JO |
792 | default: |
793 | break; | |
794 | } | |
795 | } | |
d457c963 KL |
796 | |
797 | /* User explicitly set per-event callgraph, clear the old setting and reset. */ | |
792d48b4 ACM |
798 | if ((callgraph_buf != NULL) || (dump_size > 0) || max_stack) { |
799 | if (max_stack) { | |
800 | param.max_stack = max_stack; | |
801 | if (callgraph_buf == NULL) | |
802 | callgraph_buf = "fp"; | |
803 | } | |
d457c963 KL |
804 | |
805 | /* parse callgraph parameters */ | |
806 | if (callgraph_buf != NULL) { | |
f9db0d0f KL |
807 | if (!strcmp(callgraph_buf, "no")) { |
808 | param.enabled = false; | |
809 | param.record_mode = CALLCHAIN_NONE; | |
810 | } else { | |
811 | param.enabled = true; | |
812 | if (parse_callchain_record(callgraph_buf, ¶m)) { | |
813 | pr_err("per-event callgraph setting for %s failed. " | |
814 | "Apply callgraph global setting for it\n", | |
815 | evsel->name); | |
816 | return; | |
817 | } | |
d457c963 KL |
818 | } |
819 | } | |
820 | if (dump_size > 0) { | |
821 | dump_size = round_up(dump_size, sizeof(u64)); | |
822 | param.dump_size = dump_size; | |
823 | } | |
824 | ||
825 | /* If global callgraph set, clear it */ | |
826 | if (callchain_param.enabled) | |
827 | perf_evsel__reset_callgraph(evsel, &callchain_param); | |
828 | ||
829 | /* set perf-event callgraph */ | |
830 | if (param.enabled) | |
01e0d50c | 831 | perf_evsel__config_callchain(evsel, opts, ¶m); |
d457c963 | 832 | } |
930a2e29 JO |
833 | } |
834 | ||
d226df7d KL |
835 | static bool is_dummy_event(struct perf_evsel *evsel) |
836 | { | |
837 | return (evsel->attr.type == PERF_TYPE_SOFTWARE) && | |
838 | (evsel->attr.config == PERF_COUNT_SW_DUMMY); | |
839 | } | |
840 | ||
774cb499 JO |
841 | /* |
842 | * The enable_on_exec/disabled value strategy: | |
843 | * | |
844 | * 1) For any type of traced program: | |
845 | * - all independent events and group leaders are disabled | |
846 | * - all group members are enabled | |
847 | * | |
848 | * Group members are ruled by group leaders. They need to | |
849 | * be enabled, because the group scheduling relies on that. | |
850 | * | |
851 | * 2) For traced programs executed by perf: | |
852 | * - all independent events and group leaders have | |
853 | * enable_on_exec set | |
854 | * - we don't specifically enable or disable any event during | |
855 | * the record command | |
856 | * | |
857 | * Independent events and group leaders are initially disabled | |
858 | * and get enabled by exec. Group members are ruled by group | |
859 | * leaders as stated in 1). | |
860 | * | |
861 | * 3) For traced programs attached by perf (pid/tid): | |
862 | * - we specifically enable or disable all events during | |
863 | * the record command | |
864 | * | |
865 | * When attaching events to already running traced we | |
866 | * enable/disable events specifically, as there's no | |
867 | * initial traced exec call. | |
868 | */ | |
e68ae9cf ACM |
869 | void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts, |
870 | struct callchain_param *callchain) | |
0f82ebc4 | 871 | { |
3c176311 | 872 | struct perf_evsel *leader = evsel->leader; |
0f82ebc4 | 873 | struct perf_event_attr *attr = &evsel->attr; |
60b0896c | 874 | int track = evsel->tracking; |
3aa5939d | 875 | bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread; |
0f82ebc4 | 876 | |
594ac61a | 877 | attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1; |
0f82ebc4 | 878 | attr->inherit = !opts->no_inherit; |
626a6b78 | 879 | attr->write_backward = opts->overwrite ? 1 : 0; |
0f82ebc4 | 880 | |
7be5ebe8 ACM |
881 | perf_evsel__set_sample_bit(evsel, IP); |
882 | perf_evsel__set_sample_bit(evsel, TID); | |
0f82ebc4 | 883 | |
3c176311 JO |
884 | if (evsel->sample_read) { |
885 | perf_evsel__set_sample_bit(evsel, READ); | |
886 | ||
887 | /* | |
888 | * We need ID even in case of single event, because | |
889 | * PERF_SAMPLE_READ process ID specific data. | |
890 | */ | |
75562573 | 891 | perf_evsel__set_sample_id(evsel, false); |
3c176311 JO |
892 | |
893 | /* | |
894 | * Apply group format only if we belong to group | |
895 | * with more than one members. | |
896 | */ | |
897 | if (leader->nr_members > 1) { | |
898 | attr->read_format |= PERF_FORMAT_GROUP; | |
899 | attr->inherit = 0; | |
900 | } | |
901 | } | |
902 | ||
0f82ebc4 | 903 | /* |
17314e23 | 904 | * We default some events to have a default interval. But keep |
0f82ebc4 ACM |
905 | * it a weak assumption overridable by the user. |
906 | */ | |
17314e23 | 907 | if (!attr->sample_period || (opts->user_freq != UINT_MAX || |
0f82ebc4 ACM |
908 | opts->user_interval != ULLONG_MAX)) { |
909 | if (opts->freq) { | |
7be5ebe8 | 910 | perf_evsel__set_sample_bit(evsel, PERIOD); |
0f82ebc4 ACM |
911 | attr->freq = 1; |
912 | attr->sample_freq = opts->freq; | |
913 | } else { | |
914 | attr->sample_period = opts->default_interval; | |
915 | } | |
916 | } | |
917 | ||
3c176311 JO |
918 | /* |
919 | * Disable sampling for all group members other | |
920 | * than leader in case leader 'leads' the sampling. | |
921 | */ | |
922 | if ((leader != evsel) && leader->sample_read) { | |
923 | attr->sample_freq = 0; | |
924 | attr->sample_period = 0; | |
925 | } | |
926 | ||
0f82ebc4 ACM |
927 | if (opts->no_samples) |
928 | attr->sample_freq = 0; | |
929 | ||
a17f0697 JO |
930 | if (opts->inherit_stat) { |
931 | evsel->attr.read_format |= | |
932 | PERF_FORMAT_TOTAL_TIME_ENABLED | | |
933 | PERF_FORMAT_TOTAL_TIME_RUNNING | | |
934 | PERF_FORMAT_ID; | |
0f82ebc4 | 935 | attr->inherit_stat = 1; |
a17f0697 | 936 | } |
0f82ebc4 ACM |
937 | |
938 | if (opts->sample_address) { | |
7be5ebe8 | 939 | perf_evsel__set_sample_bit(evsel, ADDR); |
0f82ebc4 ACM |
940 | attr->mmap_data = track; |
941 | } | |
942 | ||
f140373b JO |
943 | /* |
944 | * We don't allow user space callchains for function trace | |
945 | * event, due to issues with page faults while tracing page | |
946 | * fault handler and its overall trickiness nature. | |
947 | */ | |
948 | if (perf_evsel__is_function_event(evsel)) | |
949 | evsel->attr.exclude_callchain_user = 1; | |
950 | ||
e68ae9cf | 951 | if (callchain && callchain->enabled && !evsel->no_aux_samples) |
01e0d50c | 952 | perf_evsel__config_callchain(evsel, opts, callchain); |
26d33022 | 953 | |
6a21c0b5 | 954 | if (opts->sample_intr_regs) { |
bcc84ec6 | 955 | attr->sample_regs_intr = opts->sample_intr_regs; |
6a21c0b5 SE |
956 | perf_evsel__set_sample_bit(evsel, REGS_INTR); |
957 | } | |
958 | ||
84c41742 AK |
959 | if (opts->sample_user_regs) { |
960 | attr->sample_regs_user |= opts->sample_user_regs; | |
961 | perf_evsel__set_sample_bit(evsel, REGS_USER); | |
962 | } | |
963 | ||
b6f35ed7 | 964 | if (target__has_cpu(&opts->target) || opts->sample_cpu) |
7be5ebe8 | 965 | perf_evsel__set_sample_bit(evsel, CPU); |
0f82ebc4 | 966 | |
8affc2b8 | 967 | /* |
bd1a0be5 | 968 | * When the user explicitly disabled time don't force it here. |
8affc2b8 AK |
969 | */ |
970 | if (opts->sample_time && | |
971 | (!perf_missing_features.sample_id_all && | |
3abebc55 AH |
972 | (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu || |
973 | opts->sample_time_set))) | |
7be5ebe8 | 974 | perf_evsel__set_sample_bit(evsel, TIME); |
0f82ebc4 | 975 | |
6ff1ce76 | 976 | if (opts->raw_samples && !evsel->no_aux_samples) { |
7be5ebe8 ACM |
977 | perf_evsel__set_sample_bit(evsel, TIME); |
978 | perf_evsel__set_sample_bit(evsel, RAW); | |
979 | perf_evsel__set_sample_bit(evsel, CPU); | |
0f82ebc4 ACM |
980 | } |
981 | ||
ccf49bfc | 982 | if (opts->sample_address) |
1e7ed5ec | 983 | perf_evsel__set_sample_bit(evsel, DATA_SRC); |
ccf49bfc | 984 | |
3b0a5daa KL |
985 | if (opts->sample_phys_addr) |
986 | perf_evsel__set_sample_bit(evsel, PHYS_ADDR); | |
987 | ||
509051ea | 988 | if (opts->no_buffering) { |
0f82ebc4 ACM |
989 | attr->watermark = 0; |
990 | attr->wakeup_events = 1; | |
991 | } | |
6ff1ce76 | 992 | if (opts->branch_stack && !evsel->no_aux_samples) { |
7be5ebe8 | 993 | perf_evsel__set_sample_bit(evsel, BRANCH_STACK); |
bdfebd84 RAV |
994 | attr->branch_sample_type = opts->branch_stack; |
995 | } | |
0f82ebc4 | 996 | |
05484298 | 997 | if (opts->sample_weight) |
1e7ed5ec | 998 | perf_evsel__set_sample_bit(evsel, WEIGHT); |
05484298 | 999 | |
62e503b7 | 1000 | attr->task = track; |
5c5e854b | 1001 | attr->mmap = track; |
a5a5ba72 | 1002 | attr->mmap2 = track && !perf_missing_features.mmap2; |
5c5e854b | 1003 | attr->comm = track; |
0f82ebc4 | 1004 | |
f3b3614a HB |
1005 | if (opts->record_namespaces) |
1006 | attr->namespaces = track; | |
1007 | ||
b757bb09 AH |
1008 | if (opts->record_switch_events) |
1009 | attr->context_switch = track; | |
1010 | ||
475eeab9 | 1011 | if (opts->sample_transaction) |
1e7ed5ec | 1012 | perf_evsel__set_sample_bit(evsel, TRANSACTION); |
475eeab9 | 1013 | |
85c273d2 AK |
1014 | if (opts->running_time) { |
1015 | evsel->attr.read_format |= | |
1016 | PERF_FORMAT_TOTAL_TIME_ENABLED | | |
1017 | PERF_FORMAT_TOTAL_TIME_RUNNING; | |
1018 | } | |
1019 | ||
774cb499 JO |
1020 | /* |
1021 | * XXX see the function comment above | |
1022 | * | |
1023 | * Disabling only independent events or group leaders, | |
1024 | * keeping group members enabled. | |
1025 | */ | |
823254ed | 1026 | if (perf_evsel__is_group_leader(evsel)) |
774cb499 JO |
1027 | attr->disabled = 1; |
1028 | ||
1029 | /* | |
1030 | * Setting enable_on_exec for independent events and | |
1031 | * group leaders for traced executed by perf. | |
1032 | */ | |
6619a53e AK |
1033 | if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) && |
1034 | !opts->initial_delay) | |
0f82ebc4 | 1035 | attr->enable_on_exec = 1; |
2afd2bcf AH |
1036 | |
1037 | if (evsel->immediate) { | |
1038 | attr->disabled = 0; | |
1039 | attr->enable_on_exec = 0; | |
1040 | } | |
814c8c38 PZ |
1041 | |
1042 | clockid = opts->clockid; | |
1043 | if (opts->use_clockid) { | |
1044 | attr->use_clockid = 1; | |
1045 | attr->clockid = opts->clockid; | |
1046 | } | |
930a2e29 | 1047 | |
7f94af7a JO |
1048 | if (evsel->precise_max) |
1049 | perf_event_attr__set_max_precise_ip(attr); | |
1050 | ||
85723885 JO |
1051 | if (opts->all_user) { |
1052 | attr->exclude_kernel = 1; | |
1053 | attr->exclude_user = 0; | |
1054 | } | |
1055 | ||
1056 | if (opts->all_kernel) { | |
1057 | attr->exclude_kernel = 0; | |
1058 | attr->exclude_user = 1; | |
1059 | } | |
1060 | ||
930a2e29 JO |
1061 | /* |
1062 | * Apply event specific term settings, | |
1063 | * it overloads any global configuration. | |
1064 | */ | |
d457c963 | 1065 | apply_config_terms(evsel, opts); |
a359c17a JO |
1066 | |
1067 | evsel->ignore_missing_thread = opts->ignore_missing_thread; | |
26314661 JO |
1068 | |
1069 | /* The --period option takes the precedence. */ | |
1070 | if (opts->period_set) { | |
1071 | if (opts->period) | |
1072 | perf_evsel__set_sample_bit(evsel, PERIOD); | |
1073 | else | |
1074 | perf_evsel__reset_sample_bit(evsel, PERIOD); | |
1075 | } | |
d226df7d KL |
1076 | |
1077 | /* | |
1078 | * For initial_delay, a dummy event is added implicitly. | |
1079 | * The software event will trigger -EOPNOTSUPP error out, | |
1080 | * if BRANCH_STACK bit is set. | |
1081 | */ | |
1082 | if (opts->initial_delay && is_dummy_event(evsel)) | |
1083 | perf_evsel__reset_sample_bit(evsel, BRANCH_STACK); | |
0f82ebc4 ACM |
1084 | } |
1085 | ||
8885846f | 1086 | static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads) |
69aad6f1 | 1087 | { |
bf8e8f4b AH |
1088 | if (evsel->system_wide) |
1089 | nthreads = 1; | |
1090 | ||
69aad6f1 | 1091 | evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int)); |
4af4c955 DA |
1092 | |
1093 | if (evsel->fd) { | |
18ef15c6 | 1094 | int cpu, thread; |
4af4c955 DA |
1095 | for (cpu = 0; cpu < ncpus; cpu++) { |
1096 | for (thread = 0; thread < nthreads; thread++) { | |
1097 | FD(evsel, cpu, thread) = -1; | |
1098 | } | |
1099 | } | |
1100 | } | |
1101 | ||
69aad6f1 ACM |
1102 | return evsel->fd != NULL ? 0 : -ENOMEM; |
1103 | } | |
1104 | ||
475fb533 | 1105 | static int perf_evsel__run_ioctl(struct perf_evsel *evsel, |
e2407bef | 1106 | int ioc, void *arg) |
745cefc5 ACM |
1107 | { |
1108 | int cpu, thread; | |
1109 | ||
475fb533 AK |
1110 | for (cpu = 0; cpu < xyarray__max_x(evsel->fd); cpu++) { |
1111 | for (thread = 0; thread < xyarray__max_y(evsel->fd); thread++) { | |
745cefc5 | 1112 | int fd = FD(evsel, cpu, thread), |
e2407bef | 1113 | err = ioctl(fd, ioc, arg); |
745cefc5 ACM |
1114 | |
1115 | if (err) | |
1116 | return err; | |
1117 | } | |
1118 | } | |
1119 | ||
1120 | return 0; | |
1121 | } | |
1122 | ||
475fb533 | 1123 | int perf_evsel__apply_filter(struct perf_evsel *evsel, const char *filter) |
e2407bef | 1124 | { |
475fb533 | 1125 | return perf_evsel__run_ioctl(evsel, |
e2407bef AK |
1126 | PERF_EVENT_IOC_SET_FILTER, |
1127 | (void *)filter); | |
1128 | } | |
1129 | ||
12467ae4 ACM |
1130 | int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter) |
1131 | { | |
1132 | char *new_filter = strdup(filter); | |
1133 | ||
1134 | if (new_filter != NULL) { | |
1135 | free(evsel->filter); | |
1136 | evsel->filter = new_filter; | |
1137 | return 0; | |
1138 | } | |
1139 | ||
1140 | return -1; | |
1141 | } | |
1142 | ||
3541c034 MP |
1143 | static int perf_evsel__append_filter(struct perf_evsel *evsel, |
1144 | const char *fmt, const char *filter) | |
64ec84f5 ACM |
1145 | { |
1146 | char *new_filter; | |
1147 | ||
1148 | if (evsel->filter == NULL) | |
1149 | return perf_evsel__set_filter(evsel, filter); | |
1150 | ||
b15d0a4c | 1151 | if (asprintf(&new_filter, fmt, evsel->filter, filter) > 0) { |
64ec84f5 ACM |
1152 | free(evsel->filter); |
1153 | evsel->filter = new_filter; | |
1154 | return 0; | |
1155 | } | |
1156 | ||
1157 | return -1; | |
1158 | } | |
1159 | ||
3541c034 MP |
1160 | int perf_evsel__append_tp_filter(struct perf_evsel *evsel, const char *filter) |
1161 | { | |
1162 | return perf_evsel__append_filter(evsel, "(%s) && (%s)", filter); | |
1163 | } | |
1164 | ||
1e857484 MP |
1165 | int perf_evsel__append_addr_filter(struct perf_evsel *evsel, const char *filter) |
1166 | { | |
1167 | return perf_evsel__append_filter(evsel, "%s,%s", filter); | |
1168 | } | |
1169 | ||
5cd95fc3 | 1170 | int perf_evsel__enable(struct perf_evsel *evsel) |
e2407bef | 1171 | { |
475fb533 | 1172 | return perf_evsel__run_ioctl(evsel, |
e2407bef AK |
1173 | PERF_EVENT_IOC_ENABLE, |
1174 | 0); | |
1175 | } | |
1176 | ||
e98a4cbb JO |
1177 | int perf_evsel__disable(struct perf_evsel *evsel) |
1178 | { | |
475fb533 | 1179 | return perf_evsel__run_ioctl(evsel, |
e98a4cbb JO |
1180 | PERF_EVENT_IOC_DISABLE, |
1181 | 0); | |
1182 | } | |
1183 | ||
70db7533 ACM |
1184 | int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads) |
1185 | { | |
8d9cbd8f VG |
1186 | if (ncpus == 0 || nthreads == 0) |
1187 | return 0; | |
1188 | ||
bf8e8f4b AH |
1189 | if (evsel->system_wide) |
1190 | nthreads = 1; | |
1191 | ||
a91e5431 ACM |
1192 | evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id)); |
1193 | if (evsel->sample_id == NULL) | |
1194 | return -ENOMEM; | |
1195 | ||
1196 | evsel->id = zalloc(ncpus * nthreads * sizeof(u64)); | |
1197 | if (evsel->id == NULL) { | |
1198 | xyarray__delete(evsel->sample_id); | |
1199 | evsel->sample_id = NULL; | |
1200 | return -ENOMEM; | |
1201 | } | |
1202 | ||
1203 | return 0; | |
70db7533 ACM |
1204 | } |
1205 | ||
8885846f | 1206 | static void perf_evsel__free_fd(struct perf_evsel *evsel) |
69aad6f1 ACM |
1207 | { |
1208 | xyarray__delete(evsel->fd); | |
1209 | evsel->fd = NULL; | |
1210 | } | |
1211 | ||
8885846f | 1212 | static void perf_evsel__free_id(struct perf_evsel *evsel) |
70db7533 | 1213 | { |
a91e5431 ACM |
1214 | xyarray__delete(evsel->sample_id); |
1215 | evsel->sample_id = NULL; | |
04662523 | 1216 | zfree(&evsel->id); |
70db7533 ACM |
1217 | } |
1218 | ||
930a2e29 JO |
1219 | static void perf_evsel__free_config_terms(struct perf_evsel *evsel) |
1220 | { | |
1221 | struct perf_evsel_config_term *term, *h; | |
1222 | ||
1223 | list_for_each_entry_safe(term, h, &evsel->config_terms, list) { | |
1224 | list_del(&term->list); | |
1225 | free(term); | |
1226 | } | |
1227 | } | |
1228 | ||
475fb533 | 1229 | void perf_evsel__close_fd(struct perf_evsel *evsel) |
c52b12ed ACM |
1230 | { |
1231 | int cpu, thread; | |
1232 | ||
475fb533 AK |
1233 | for (cpu = 0; cpu < xyarray__max_x(evsel->fd); cpu++) |
1234 | for (thread = 0; thread < xyarray__max_y(evsel->fd); ++thread) { | |
c52b12ed ACM |
1235 | close(FD(evsel, cpu, thread)); |
1236 | FD(evsel, cpu, thread) = -1; | |
1237 | } | |
1238 | } | |
1239 | ||
ef1d1af2 | 1240 | void perf_evsel__exit(struct perf_evsel *evsel) |
69aad6f1 ACM |
1241 | { |
1242 | assert(list_empty(&evsel->node)); | |
d49e4695 | 1243 | assert(evsel->evlist == NULL); |
e153f9ab | 1244 | perf_evsel__free_counts(evsel); |
736b05a0 NK |
1245 | perf_evsel__free_fd(evsel); |
1246 | perf_evsel__free_id(evsel); | |
930a2e29 | 1247 | perf_evsel__free_config_terms(evsel); |
597e48c1 | 1248 | close_cgroup(evsel->cgrp); |
f30a79b0 | 1249 | cpu_map__put(evsel->cpus); |
fce4d296 | 1250 | cpu_map__put(evsel->own_cpus); |
578e91ec | 1251 | thread_map__put(evsel->threads); |
597e48c1 | 1252 | zfree(&evsel->group_name); |
597e48c1 | 1253 | zfree(&evsel->name); |
ce8ccff5 | 1254 | perf_evsel__object.fini(evsel); |
ef1d1af2 ACM |
1255 | } |
1256 | ||
1257 | void perf_evsel__delete(struct perf_evsel *evsel) | |
1258 | { | |
1259 | perf_evsel__exit(evsel); | |
69aad6f1 ACM |
1260 | free(evsel); |
1261 | } | |
c52b12ed | 1262 | |
a6fa0038 | 1263 | void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread, |
857a94a2 | 1264 | struct perf_counts_values *count) |
c7a79c47 SE |
1265 | { |
1266 | struct perf_counts_values tmp; | |
1267 | ||
1268 | if (!evsel->prev_raw_counts) | |
1269 | return; | |
1270 | ||
1271 | if (cpu == -1) { | |
1272 | tmp = evsel->prev_raw_counts->aggr; | |
1273 | evsel->prev_raw_counts->aggr = *count; | |
1274 | } else { | |
a6fa0038 JO |
1275 | tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread); |
1276 | *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count; | |
c7a79c47 SE |
1277 | } |
1278 | ||
1279 | count->val = count->val - tmp.val; | |
1280 | count->ena = count->ena - tmp.ena; | |
1281 | count->run = count->run - tmp.run; | |
1282 | } | |
1283 | ||
13112bbf JO |
1284 | void perf_counts_values__scale(struct perf_counts_values *count, |
1285 | bool scale, s8 *pscaled) | |
1286 | { | |
1287 | s8 scaled = 0; | |
1288 | ||
1289 | if (scale) { | |
1290 | if (count->run == 0) { | |
1291 | scaled = -1; | |
1292 | count->val = 0; | |
1293 | } else if (count->run < count->ena) { | |
1294 | scaled = 1; | |
1295 | count->val = (u64)((double) count->val * count->ena / count->run + 0.5); | |
1296 | } | |
1297 | } else | |
1298 | count->ena = count->run = 0; | |
1299 | ||
1300 | if (pscaled) | |
1301 | *pscaled = scaled; | |
1302 | } | |
1303 | ||
de63403b JO |
1304 | static int perf_evsel__read_size(struct perf_evsel *evsel) |
1305 | { | |
1306 | u64 read_format = evsel->attr.read_format; | |
1307 | int entry = sizeof(u64); /* value */ | |
1308 | int size = 0; | |
1309 | int nr = 1; | |
1310 | ||
1311 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
1312 | size += sizeof(u64); | |
1313 | ||
1314 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
1315 | size += sizeof(u64); | |
1316 | ||
1317 | if (read_format & PERF_FORMAT_ID) | |
1318 | entry += sizeof(u64); | |
1319 | ||
1320 | if (read_format & PERF_FORMAT_GROUP) { | |
1321 | nr = evsel->nr_members; | |
1322 | size += sizeof(u64); | |
1323 | } | |
1324 | ||
1325 | size += entry * nr; | |
1326 | return size; | |
1327 | } | |
1328 | ||
f99f4719 JO |
1329 | int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread, |
1330 | struct perf_counts_values *count) | |
1331 | { | |
de63403b JO |
1332 | size_t size = perf_evsel__read_size(evsel); |
1333 | ||
f99f4719 JO |
1334 | memset(count, 0, sizeof(*count)); |
1335 | ||
1336 | if (FD(evsel, cpu, thread) < 0) | |
1337 | return -EINVAL; | |
1338 | ||
de63403b | 1339 | if (readn(FD(evsel, cpu, thread), count->values, size) <= 0) |
f99f4719 JO |
1340 | return -errno; |
1341 | ||
1342 | return 0; | |
1343 | } | |
1344 | ||
f7794d52 JO |
1345 | static int |
1346 | perf_evsel__read_one(struct perf_evsel *evsel, int cpu, int thread) | |
1347 | { | |
1348 | struct perf_counts_values *count = perf_counts(evsel->counts, cpu, thread); | |
1349 | ||
1350 | return perf_evsel__read(evsel, cpu, thread, count); | |
1351 | } | |
1352 | ||
1353 | static void | |
1354 | perf_evsel__set_count(struct perf_evsel *counter, int cpu, int thread, | |
1355 | u64 val, u64 ena, u64 run) | |
1356 | { | |
1357 | struct perf_counts_values *count; | |
1358 | ||
1359 | count = perf_counts(counter->counts, cpu, thread); | |
1360 | ||
1361 | count->val = val; | |
1362 | count->ena = ena; | |
1363 | count->run = run; | |
82bf311e | 1364 | count->loaded = true; |
f7794d52 JO |
1365 | } |
1366 | ||
1367 | static int | |
1368 | perf_evsel__process_group_data(struct perf_evsel *leader, | |
1369 | int cpu, int thread, u64 *data) | |
1370 | { | |
1371 | u64 read_format = leader->attr.read_format; | |
1372 | struct sample_read_value *v; | |
1373 | u64 nr, ena = 0, run = 0, i; | |
1374 | ||
1375 | nr = *data++; | |
1376 | ||
1377 | if (nr != (u64) leader->nr_members) | |
1378 | return -EINVAL; | |
1379 | ||
1380 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
1381 | ena = *data++; | |
1382 | ||
1383 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
1384 | run = *data++; | |
1385 | ||
1386 | v = (struct sample_read_value *) data; | |
1387 | ||
1388 | perf_evsel__set_count(leader, cpu, thread, | |
1389 | v[0].value, ena, run); | |
1390 | ||
1391 | for (i = 1; i < nr; i++) { | |
1392 | struct perf_evsel *counter; | |
1393 | ||
1394 | counter = perf_evlist__id2evsel(leader->evlist, v[i].id); | |
1395 | if (!counter) | |
1396 | return -EINVAL; | |
1397 | ||
1398 | perf_evsel__set_count(counter, cpu, thread, | |
1399 | v[i].value, ena, run); | |
1400 | } | |
1401 | ||
1402 | return 0; | |
1403 | } | |
1404 | ||
1405 | static int | |
1406 | perf_evsel__read_group(struct perf_evsel *leader, int cpu, int thread) | |
1407 | { | |
8e2d8e20 | 1408 | struct perf_stat_evsel *ps = leader->stats; |
f7794d52 JO |
1409 | u64 read_format = leader->attr.read_format; |
1410 | int size = perf_evsel__read_size(leader); | |
1411 | u64 *data = ps->group_data; | |
1412 | ||
1413 | if (!(read_format & PERF_FORMAT_ID)) | |
1414 | return -EINVAL; | |
1415 | ||
1416 | if (!perf_evsel__is_group_leader(leader)) | |
1417 | return -EINVAL; | |
1418 | ||
1419 | if (!data) { | |
1420 | data = zalloc(size); | |
1421 | if (!data) | |
1422 | return -ENOMEM; | |
1423 | ||
1424 | ps->group_data = data; | |
1425 | } | |
1426 | ||
1427 | if (FD(leader, cpu, thread) < 0) | |
1428 | return -EINVAL; | |
1429 | ||
1430 | if (readn(FD(leader, cpu, thread), data, size) <= 0) | |
1431 | return -errno; | |
1432 | ||
1433 | return perf_evsel__process_group_data(leader, cpu, thread, data); | |
1434 | } | |
1435 | ||
1436 | int perf_evsel__read_counter(struct perf_evsel *evsel, int cpu, int thread) | |
1437 | { | |
1438 | u64 read_format = evsel->attr.read_format; | |
1439 | ||
1440 | if (read_format & PERF_FORMAT_GROUP) | |
1441 | return perf_evsel__read_group(evsel, cpu, thread); | |
1442 | else | |
1443 | return perf_evsel__read_one(evsel, cpu, thread); | |
1444 | } | |
1445 | ||
c52b12ed ACM |
1446 | int __perf_evsel__read_on_cpu(struct perf_evsel *evsel, |
1447 | int cpu, int thread, bool scale) | |
1448 | { | |
1449 | struct perf_counts_values count; | |
1450 | size_t nv = scale ? 3 : 1; | |
1451 | ||
1452 | if (FD(evsel, cpu, thread) < 0) | |
1453 | return -EINVAL; | |
1454 | ||
a6fa0038 | 1455 | if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0) |
4eed11d5 ACM |
1456 | return -ENOMEM; |
1457 | ||
db49a717 | 1458 | if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) <= 0) |
c52b12ed ACM |
1459 | return -errno; |
1460 | ||
a6fa0038 | 1461 | perf_evsel__compute_deltas(evsel, cpu, thread, &count); |
13112bbf | 1462 | perf_counts_values__scale(&count, scale, NULL); |
a6fa0038 | 1463 | *perf_counts(evsel->counts, cpu, thread) = count; |
c52b12ed ACM |
1464 | return 0; |
1465 | } | |
1466 | ||
6a4bb04c JO |
1467 | static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread) |
1468 | { | |
1469 | struct perf_evsel *leader = evsel->leader; | |
1470 | int fd; | |
1471 | ||
823254ed | 1472 | if (perf_evsel__is_group_leader(evsel)) |
6a4bb04c JO |
1473 | return -1; |
1474 | ||
1475 | /* | |
1476 | * Leader must be already processed/open, | |
1477 | * if not it's a bug. | |
1478 | */ | |
1479 | BUG_ON(!leader->fd); | |
1480 | ||
1481 | fd = FD(leader, cpu, thread); | |
1482 | BUG_ON(fd == -1); | |
1483 | ||
1484 | return fd; | |
1485 | } | |
1486 | ||
2c5e8c52 PZ |
1487 | struct bit_names { |
1488 | int bit; | |
1489 | const char *name; | |
1490 | }; | |
1491 | ||
1492 | static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits) | |
1493 | { | |
1494 | bool first_bit = true; | |
1495 | int i = 0; | |
1496 | ||
1497 | do { | |
1498 | if (value & bits[i].bit) { | |
1499 | buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name); | |
1500 | first_bit = false; | |
1501 | } | |
1502 | } while (bits[++i].name != NULL); | |
1503 | } | |
1504 | ||
1505 | static void __p_sample_type(char *buf, size_t size, u64 value) | |
1506 | { | |
1507 | #define bit_name(n) { PERF_SAMPLE_##n, #n } | |
1508 | struct bit_names bits[] = { | |
1509 | bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR), | |
1510 | bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU), | |
1511 | bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW), | |
1512 | bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER), | |
84422592 | 1513 | bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC), |
3b0a5daa | 1514 | bit_name(WEIGHT), bit_name(PHYS_ADDR), |
2c5e8c52 PZ |
1515 | { .name = NULL, } |
1516 | }; | |
1517 | #undef bit_name | |
1518 | __p_bits(buf, size, value, bits); | |
1519 | } | |
1520 | ||
a213b92e ACM |
1521 | static void __p_branch_sample_type(char *buf, size_t size, u64 value) |
1522 | { | |
1523 | #define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n } | |
1524 | struct bit_names bits[] = { | |
1525 | bit_name(USER), bit_name(KERNEL), bit_name(HV), bit_name(ANY), | |
1526 | bit_name(ANY_CALL), bit_name(ANY_RETURN), bit_name(IND_CALL), | |
1527 | bit_name(ABORT_TX), bit_name(IN_TX), bit_name(NO_TX), | |
1528 | bit_name(COND), bit_name(CALL_STACK), bit_name(IND_JUMP), | |
1529 | bit_name(CALL), bit_name(NO_FLAGS), bit_name(NO_CYCLES), | |
1530 | { .name = NULL, } | |
1531 | }; | |
1532 | #undef bit_name | |
1533 | __p_bits(buf, size, value, bits); | |
1534 | } | |
1535 | ||
2c5e8c52 PZ |
1536 | static void __p_read_format(char *buf, size_t size, u64 value) |
1537 | { | |
1538 | #define bit_name(n) { PERF_FORMAT_##n, #n } | |
1539 | struct bit_names bits[] = { | |
1540 | bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING), | |
1541 | bit_name(ID), bit_name(GROUP), | |
1542 | { .name = NULL, } | |
1543 | }; | |
1544 | #undef bit_name | |
1545 | __p_bits(buf, size, value, bits); | |
1546 | } | |
1547 | ||
1548 | #define BUF_SIZE 1024 | |
1549 | ||
7310aed7 | 1550 | #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val)) |
2c5e8c52 PZ |
1551 | #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val)) |
1552 | #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val)) | |
1553 | #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val) | |
a213b92e | 1554 | #define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val) |
2c5e8c52 PZ |
1555 | #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val) |
1556 | ||
1557 | #define PRINT_ATTRn(_n, _f, _p) \ | |
1558 | do { \ | |
1559 | if (attr->_f) { \ | |
1560 | _p(attr->_f); \ | |
1561 | ret += attr__fprintf(fp, _n, buf, priv);\ | |
1562 | } \ | |
1563 | } while (0) | |
1564 | ||
1565 | #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p) | |
1566 | ||
1567 | int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr, | |
1568 | attr__fprintf_f attr__fprintf, void *priv) | |
1569 | { | |
1570 | char buf[BUF_SIZE]; | |
1571 | int ret = 0; | |
1572 | ||
1573 | PRINT_ATTRf(type, p_unsigned); | |
1574 | PRINT_ATTRf(size, p_unsigned); | |
1575 | PRINT_ATTRf(config, p_hex); | |
1576 | PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned); | |
1577 | PRINT_ATTRf(sample_type, p_sample_type); | |
1578 | PRINT_ATTRf(read_format, p_read_format); | |
1579 | ||
1580 | PRINT_ATTRf(disabled, p_unsigned); | |
1581 | PRINT_ATTRf(inherit, p_unsigned); | |
1582 | PRINT_ATTRf(pinned, p_unsigned); | |
1583 | PRINT_ATTRf(exclusive, p_unsigned); | |
1584 | PRINT_ATTRf(exclude_user, p_unsigned); | |
1585 | PRINT_ATTRf(exclude_kernel, p_unsigned); | |
1586 | PRINT_ATTRf(exclude_hv, p_unsigned); | |
1587 | PRINT_ATTRf(exclude_idle, p_unsigned); | |
1588 | PRINT_ATTRf(mmap, p_unsigned); | |
1589 | PRINT_ATTRf(comm, p_unsigned); | |
1590 | PRINT_ATTRf(freq, p_unsigned); | |
1591 | PRINT_ATTRf(inherit_stat, p_unsigned); | |
1592 | PRINT_ATTRf(enable_on_exec, p_unsigned); | |
1593 | PRINT_ATTRf(task, p_unsigned); | |
1594 | PRINT_ATTRf(watermark, p_unsigned); | |
1595 | PRINT_ATTRf(precise_ip, p_unsigned); | |
1596 | PRINT_ATTRf(mmap_data, p_unsigned); | |
1597 | PRINT_ATTRf(sample_id_all, p_unsigned); | |
1598 | PRINT_ATTRf(exclude_host, p_unsigned); | |
1599 | PRINT_ATTRf(exclude_guest, p_unsigned); | |
1600 | PRINT_ATTRf(exclude_callchain_kernel, p_unsigned); | |
1601 | PRINT_ATTRf(exclude_callchain_user, p_unsigned); | |
1602 | PRINT_ATTRf(mmap2, p_unsigned); | |
1603 | PRINT_ATTRf(comm_exec, p_unsigned); | |
1604 | PRINT_ATTRf(use_clockid, p_unsigned); | |
0286039f | 1605 | PRINT_ATTRf(context_switch, p_unsigned); |
0a241ef4 | 1606 | PRINT_ATTRf(write_backward, p_unsigned); |
2c5e8c52 PZ |
1607 | |
1608 | PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned); | |
1609 | PRINT_ATTRf(bp_type, p_unsigned); | |
1610 | PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex); | |
1611 | PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex); | |
a213b92e | 1612 | PRINT_ATTRf(branch_sample_type, p_branch_sample_type); |
2c5e8c52 PZ |
1613 | PRINT_ATTRf(sample_regs_user, p_hex); |
1614 | PRINT_ATTRf(sample_stack_user, p_unsigned); | |
1615 | PRINT_ATTRf(clockid, p_signed); | |
1616 | PRINT_ATTRf(sample_regs_intr, p_hex); | |
70d73de4 | 1617 | PRINT_ATTRf(aux_watermark, p_unsigned); |
792d48b4 | 1618 | PRINT_ATTRf(sample_max_stack, p_unsigned); |
e3e1a54f AH |
1619 | |
1620 | return ret; | |
1621 | } | |
1622 | ||
2c5e8c52 | 1623 | static int __open_attr__fprintf(FILE *fp, const char *name, const char *val, |
0353631a | 1624 | void *priv __maybe_unused) |
2c5e8c52 PZ |
1625 | { |
1626 | return fprintf(fp, " %-32s %s\n", name, val); | |
1627 | } | |
1628 | ||
12f6fd2a MZ |
1629 | static void perf_evsel__remove_fd(struct perf_evsel *pos, |
1630 | int nr_cpus, int nr_threads, | |
1631 | int thread_idx) | |
1632 | { | |
1633 | for (int cpu = 0; cpu < nr_cpus; cpu++) | |
1634 | for (int thread = thread_idx; thread < nr_threads - 1; thread++) | |
1635 | FD(pos, cpu, thread) = FD(pos, cpu, thread + 1); | |
1636 | } | |
1637 | ||
1638 | static int update_fds(struct perf_evsel *evsel, | |
1639 | int nr_cpus, int cpu_idx, | |
1640 | int nr_threads, int thread_idx) | |
1641 | { | |
1642 | struct perf_evsel *pos; | |
1643 | ||
1644 | if (cpu_idx >= nr_cpus || thread_idx >= nr_threads) | |
1645 | return -EINVAL; | |
1646 | ||
1647 | evlist__for_each_entry(evsel->evlist, pos) { | |
1648 | nr_cpus = pos != evsel ? nr_cpus : cpu_idx; | |
1649 | ||
1650 | perf_evsel__remove_fd(pos, nr_cpus, nr_threads, thread_idx); | |
1651 | ||
1652 | /* | |
1653 | * Since fds for next evsel has not been created, | |
1654 | * there is no need to iterate whole event list. | |
1655 | */ | |
1656 | if (pos == evsel) | |
1657 | break; | |
1658 | } | |
1659 | return 0; | |
1660 | } | |
1661 | ||
a359c17a | 1662 | static bool ignore_missing_thread(struct perf_evsel *evsel, |
12f6fd2a | 1663 | int nr_cpus, int cpu, |
a359c17a JO |
1664 | struct thread_map *threads, |
1665 | int thread, int err) | |
1666 | { | |
12f6fd2a MZ |
1667 | pid_t ignore_pid = thread_map__pid(threads, thread); |
1668 | ||
a359c17a JO |
1669 | if (!evsel->ignore_missing_thread) |
1670 | return false; | |
1671 | ||
1672 | /* The system wide setup does not work with threads. */ | |
1673 | if (evsel->system_wide) | |
1674 | return false; | |
1675 | ||
1676 | /* The -ESRCH is perf event syscall errno for pid's not found. */ | |
1677 | if (err != -ESRCH) | |
1678 | return false; | |
1679 | ||
1680 | /* If there's only one thread, let it fail. */ | |
1681 | if (threads->nr == 1) | |
1682 | return false; | |
1683 | ||
12f6fd2a MZ |
1684 | /* |
1685 | * We should remove fd for missing_thread first | |
1686 | * because thread_map__remove() will decrease threads->nr. | |
1687 | */ | |
1688 | if (update_fds(evsel, nr_cpus, cpu, threads->nr, thread)) | |
1689 | return false; | |
1690 | ||
a359c17a JO |
1691 | if (thread_map__remove(threads, thread)) |
1692 | return false; | |
1693 | ||
1694 | pr_warning("WARNING: Ignored open failure for pid %d\n", | |
12f6fd2a | 1695 | ignore_pid); |
a359c17a JO |
1696 | return true; |
1697 | } | |
1698 | ||
c24ae6d9 ACM |
1699 | int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus, |
1700 | struct thread_map *threads) | |
48290609 | 1701 | { |
bf8e8f4b | 1702 | int cpu, thread, nthreads; |
57480d2c | 1703 | unsigned long flags = PERF_FLAG_FD_CLOEXEC; |
727ab04e | 1704 | int pid = -1, err; |
bec19672 | 1705 | enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE; |
48290609 | 1706 | |
32a951b4 ACM |
1707 | if (perf_missing_features.write_backward && evsel->attr.write_backward) |
1708 | return -EINVAL; | |
1709 | ||
c24ae6d9 ACM |
1710 | if (cpus == NULL) { |
1711 | static struct cpu_map *empty_cpu_map; | |
1712 | ||
1713 | if (empty_cpu_map == NULL) { | |
1714 | empty_cpu_map = cpu_map__dummy_new(); | |
1715 | if (empty_cpu_map == NULL) | |
1716 | return -ENOMEM; | |
1717 | } | |
1718 | ||
1719 | cpus = empty_cpu_map; | |
1720 | } | |
1721 | ||
1722 | if (threads == NULL) { | |
1723 | static struct thread_map *empty_thread_map; | |
1724 | ||
1725 | if (empty_thread_map == NULL) { | |
1726 | empty_thread_map = thread_map__new_by_tid(-1); | |
1727 | if (empty_thread_map == NULL) | |
1728 | return -ENOMEM; | |
1729 | } | |
1730 | ||
1731 | threads = empty_thread_map; | |
1732 | } | |
1733 | ||
bf8e8f4b AH |
1734 | if (evsel->system_wide) |
1735 | nthreads = 1; | |
1736 | else | |
1737 | nthreads = threads->nr; | |
1738 | ||
0252208e | 1739 | if (evsel->fd == NULL && |
bf8e8f4b | 1740 | perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0) |
727ab04e | 1741 | return -ENOMEM; |
4eed11d5 | 1742 | |
023695d9 | 1743 | if (evsel->cgrp) { |
57480d2c | 1744 | flags |= PERF_FLAG_PID_CGROUP; |
023695d9 SE |
1745 | pid = evsel->cgrp->fd; |
1746 | } | |
1747 | ||
594ac61a | 1748 | fallback_missing_features: |
814c8c38 PZ |
1749 | if (perf_missing_features.clockid_wrong) |
1750 | evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */ | |
1751 | if (perf_missing_features.clockid) { | |
1752 | evsel->attr.use_clockid = 0; | |
1753 | evsel->attr.clockid = 0; | |
1754 | } | |
57480d2c YD |
1755 | if (perf_missing_features.cloexec) |
1756 | flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC; | |
5c5e854b SE |
1757 | if (perf_missing_features.mmap2) |
1758 | evsel->attr.mmap2 = 0; | |
594ac61a ACM |
1759 | if (perf_missing_features.exclude_guest) |
1760 | evsel->attr.exclude_guest = evsel->attr.exclude_host = 0; | |
bd0f8895 AK |
1761 | if (perf_missing_features.lbr_flags) |
1762 | evsel->attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS | | |
1763 | PERF_SAMPLE_BRANCH_NO_CYCLES); | |
82bf311e JO |
1764 | if (perf_missing_features.group_read && evsel->attr.inherit) |
1765 | evsel->attr.read_format &= ~(PERF_FORMAT_GROUP|PERF_FORMAT_ID); | |
594ac61a ACM |
1766 | retry_sample_id: |
1767 | if (perf_missing_features.sample_id_all) | |
1768 | evsel->attr.sample_id_all = 0; | |
1769 | ||
2c5e8c52 PZ |
1770 | if (verbose >= 2) { |
1771 | fprintf(stderr, "%.60s\n", graph_dotted_line); | |
1772 | fprintf(stderr, "perf_event_attr:\n"); | |
1773 | perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL); | |
1774 | fprintf(stderr, "%.60s\n", graph_dotted_line); | |
1775 | } | |
e3e1a54f | 1776 | |
86bd5e86 | 1777 | for (cpu = 0; cpu < cpus->nr; cpu++) { |
9d04f178 | 1778 | |
bf8e8f4b | 1779 | for (thread = 0; thread < nthreads; thread++) { |
83c2e4f3 | 1780 | int fd, group_fd; |
023695d9 | 1781 | |
bf8e8f4b | 1782 | if (!evsel->cgrp && !evsel->system_wide) |
e13798c7 | 1783 | pid = thread_map__pid(threads, thread); |
023695d9 | 1784 | |
6a4bb04c | 1785 | group_fd = get_group_fd(evsel, cpu, thread); |
bec19672 | 1786 | retry_open: |
7b4b82bc | 1787 | pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx", |
e3e1a54f AH |
1788 | pid, cpus->map[cpu], group_fd, flags); |
1789 | ||
10213e2f JO |
1790 | test_attr__ready(); |
1791 | ||
83c2e4f3 JO |
1792 | fd = sys_perf_event_open(&evsel->attr, pid, cpus->map[cpu], |
1793 | group_fd, flags); | |
1794 | ||
1795 | FD(evsel, cpu, thread) = fd; | |
1796 | ||
1797 | if (fd < 0) { | |
727ab04e | 1798 | err = -errno; |
a359c17a | 1799 | |
12f6fd2a | 1800 | if (ignore_missing_thread(evsel, cpus->nr, cpu, threads, thread, err)) { |
a359c17a JO |
1801 | /* |
1802 | * We just removed 1 thread, so take a step | |
1803 | * back on thread index and lower the upper | |
1804 | * nthreads limit. | |
1805 | */ | |
1806 | nthreads--; | |
1807 | thread--; | |
1808 | ||
1809 | /* ... and pretend like nothing have happened. */ | |
1810 | err = 0; | |
1811 | continue; | |
1812 | } | |
1813 | ||
7b4b82bc | 1814 | pr_debug2("\nsys_perf_event_open failed, error %d\n", |
f852fd62 | 1815 | err); |
594ac61a | 1816 | goto try_fallback; |
727ab04e | 1817 | } |
1f45b1d4 | 1818 | |
83c2e4f3 | 1819 | pr_debug2(" = %d\n", fd); |
7b4b82bc | 1820 | |
1f45b1d4 | 1821 | if (evsel->bpf_fd >= 0) { |
83c2e4f3 | 1822 | int evt_fd = fd; |
1f45b1d4 WN |
1823 | int bpf_fd = evsel->bpf_fd; |
1824 | ||
1825 | err = ioctl(evt_fd, | |
1826 | PERF_EVENT_IOC_SET_BPF, | |
1827 | bpf_fd); | |
1828 | if (err && errno != EEXIST) { | |
1829 | pr_err("failed to attach bpf fd %d: %s\n", | |
1830 | bpf_fd, strerror(errno)); | |
1831 | err = -EINVAL; | |
1832 | goto out_close; | |
1833 | } | |
1834 | } | |
1835 | ||
bec19672 | 1836 | set_rlimit = NO_CHANGE; |
814c8c38 PZ |
1837 | |
1838 | /* | |
1839 | * If we succeeded but had to kill clockid, fail and | |
1840 | * have perf_evsel__open_strerror() print us a nice | |
1841 | * error. | |
1842 | */ | |
1843 | if (perf_missing_features.clockid || | |
1844 | perf_missing_features.clockid_wrong) { | |
1845 | err = -EINVAL; | |
1846 | goto out_close; | |
1847 | } | |
0252208e | 1848 | } |
48290609 ACM |
1849 | } |
1850 | ||
1851 | return 0; | |
1852 | ||
594ac61a | 1853 | try_fallback: |
bec19672 AK |
1854 | /* |
1855 | * perf stat needs between 5 and 22 fds per CPU. When we run out | |
1856 | * of them try to increase the limits. | |
1857 | */ | |
1858 | if (err == -EMFILE && set_rlimit < INCREASED_MAX) { | |
1859 | struct rlimit l; | |
1860 | int old_errno = errno; | |
1861 | ||
1862 | if (getrlimit(RLIMIT_NOFILE, &l) == 0) { | |
1863 | if (set_rlimit == NO_CHANGE) | |
1864 | l.rlim_cur = l.rlim_max; | |
1865 | else { | |
1866 | l.rlim_cur = l.rlim_max + 1000; | |
1867 | l.rlim_max = l.rlim_cur; | |
1868 | } | |
1869 | if (setrlimit(RLIMIT_NOFILE, &l) == 0) { | |
1870 | set_rlimit++; | |
1871 | errno = old_errno; | |
1872 | goto retry_open; | |
1873 | } | |
1874 | } | |
1875 | errno = old_errno; | |
1876 | } | |
1877 | ||
594ac61a ACM |
1878 | if (err != -EINVAL || cpu > 0 || thread > 0) |
1879 | goto out_close; | |
1880 | ||
814c8c38 PZ |
1881 | /* |
1882 | * Must probe features in the order they were added to the | |
1883 | * perf_event_attr interface. | |
1884 | */ | |
7da36e94 ACM |
1885 | if (!perf_missing_features.write_backward && evsel->attr.write_backward) { |
1886 | perf_missing_features.write_backward = true; | |
2b04e0f8 | 1887 | pr_debug2("switching off write_backward\n"); |
32a951b4 | 1888 | goto out_close; |
7da36e94 | 1889 | } else if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) { |
814c8c38 | 1890 | perf_missing_features.clockid_wrong = true; |
2b04e0f8 | 1891 | pr_debug2("switching off clockid\n"); |
814c8c38 PZ |
1892 | goto fallback_missing_features; |
1893 | } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) { | |
1894 | perf_missing_features.clockid = true; | |
2b04e0f8 | 1895 | pr_debug2("switching off use_clockid\n"); |
814c8c38 PZ |
1896 | goto fallback_missing_features; |
1897 | } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) { | |
57480d2c | 1898 | perf_missing_features.cloexec = true; |
2b04e0f8 | 1899 | pr_debug2("switching off cloexec flag\n"); |
57480d2c YD |
1900 | goto fallback_missing_features; |
1901 | } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) { | |
5c5e854b | 1902 | perf_missing_features.mmap2 = true; |
2b04e0f8 | 1903 | pr_debug2("switching off mmap2\n"); |
5c5e854b SE |
1904 | goto fallback_missing_features; |
1905 | } else if (!perf_missing_features.exclude_guest && | |
1906 | (evsel->attr.exclude_guest || evsel->attr.exclude_host)) { | |
594ac61a | 1907 | perf_missing_features.exclude_guest = true; |
2b04e0f8 | 1908 | pr_debug2("switching off exclude_guest, exclude_host\n"); |
594ac61a ACM |
1909 | goto fallback_missing_features; |
1910 | } else if (!perf_missing_features.sample_id_all) { | |
1911 | perf_missing_features.sample_id_all = true; | |
2b04e0f8 | 1912 | pr_debug2("switching off sample_id_all\n"); |
594ac61a | 1913 | goto retry_sample_id; |
bd0f8895 AK |
1914 | } else if (!perf_missing_features.lbr_flags && |
1915 | (evsel->attr.branch_sample_type & | |
1916 | (PERF_SAMPLE_BRANCH_NO_CYCLES | | |
1917 | PERF_SAMPLE_BRANCH_NO_FLAGS))) { | |
1918 | perf_missing_features.lbr_flags = true; | |
2b04e0f8 | 1919 | pr_debug2("switching off branch sample type no (cycles/flags)\n"); |
bd0f8895 | 1920 | goto fallback_missing_features; |
82bf311e JO |
1921 | } else if (!perf_missing_features.group_read && |
1922 | evsel->attr.inherit && | |
1923 | (evsel->attr.read_format & PERF_FORMAT_GROUP)) { | |
1924 | perf_missing_features.group_read = true; | |
1925 | pr_debug2("switching off group read\n"); | |
1926 | goto fallback_missing_features; | |
594ac61a | 1927 | } |
48290609 | 1928 | out_close: |
0252208e ACM |
1929 | do { |
1930 | while (--thread >= 0) { | |
1931 | close(FD(evsel, cpu, thread)); | |
1932 | FD(evsel, cpu, thread) = -1; | |
1933 | } | |
bf8e8f4b | 1934 | thread = nthreads; |
0252208e | 1935 | } while (--cpu >= 0); |
727ab04e ACM |
1936 | return err; |
1937 | } | |
1938 | ||
475fb533 | 1939 | void perf_evsel__close(struct perf_evsel *evsel) |
727ab04e ACM |
1940 | { |
1941 | if (evsel->fd == NULL) | |
1942 | return; | |
1943 | ||
475fb533 | 1944 | perf_evsel__close_fd(evsel); |
727ab04e | 1945 | perf_evsel__free_fd(evsel); |
48290609 ACM |
1946 | } |
1947 | ||
f08199d3 | 1948 | int perf_evsel__open_per_cpu(struct perf_evsel *evsel, |
6a4bb04c | 1949 | struct cpu_map *cpus) |
48290609 | 1950 | { |
c24ae6d9 | 1951 | return perf_evsel__open(evsel, cpus, NULL); |
0252208e | 1952 | } |
48290609 | 1953 | |
f08199d3 | 1954 | int perf_evsel__open_per_thread(struct perf_evsel *evsel, |
6a4bb04c | 1955 | struct thread_map *threads) |
0252208e | 1956 | { |
c24ae6d9 | 1957 | return perf_evsel__open(evsel, NULL, threads); |
48290609 | 1958 | } |
70082dd9 | 1959 | |
0807d2d8 ACM |
1960 | static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel, |
1961 | const union perf_event *event, | |
1962 | struct perf_sample *sample) | |
d0dd74e8 | 1963 | { |
0807d2d8 | 1964 | u64 type = evsel->attr.sample_type; |
d0dd74e8 | 1965 | const u64 *array = event->sample.array; |
0807d2d8 | 1966 | bool swapped = evsel->needs_swap; |
37073f9e | 1967 | union u64_swap u; |
d0dd74e8 ACM |
1968 | |
1969 | array += ((event->header.size - | |
1970 | sizeof(event->header)) / sizeof(u64)) - 1; | |
1971 | ||
75562573 AH |
1972 | if (type & PERF_SAMPLE_IDENTIFIER) { |
1973 | sample->id = *array; | |
1974 | array--; | |
1975 | } | |
1976 | ||
d0dd74e8 | 1977 | if (type & PERF_SAMPLE_CPU) { |
37073f9e JO |
1978 | u.val64 = *array; |
1979 | if (swapped) { | |
1980 | /* undo swap of u64, then swap on individual u32s */ | |
1981 | u.val64 = bswap_64(u.val64); | |
1982 | u.val32[0] = bswap_32(u.val32[0]); | |
1983 | } | |
1984 | ||
1985 | sample->cpu = u.val32[0]; | |
d0dd74e8 ACM |
1986 | array--; |
1987 | } | |
1988 | ||
1989 | if (type & PERF_SAMPLE_STREAM_ID) { | |
1990 | sample->stream_id = *array; | |
1991 | array--; | |
1992 | } | |
1993 | ||
1994 | if (type & PERF_SAMPLE_ID) { | |
1995 | sample->id = *array; | |
1996 | array--; | |
1997 | } | |
1998 | ||
1999 | if (type & PERF_SAMPLE_TIME) { | |
2000 | sample->time = *array; | |
2001 | array--; | |
2002 | } | |
2003 | ||
2004 | if (type & PERF_SAMPLE_TID) { | |
37073f9e JO |
2005 | u.val64 = *array; |
2006 | if (swapped) { | |
2007 | /* undo swap of u64, then swap on individual u32s */ | |
2008 | u.val64 = bswap_64(u.val64); | |
2009 | u.val32[0] = bswap_32(u.val32[0]); | |
2010 | u.val32[1] = bswap_32(u.val32[1]); | |
2011 | } | |
2012 | ||
2013 | sample->pid = u.val32[0]; | |
2014 | sample->tid = u.val32[1]; | |
dd44bc6b | 2015 | array--; |
d0dd74e8 ACM |
2016 | } |
2017 | ||
2018 | return 0; | |
2019 | } | |
2020 | ||
03b6ea9b AH |
2021 | static inline bool overflow(const void *endp, u16 max_size, const void *offset, |
2022 | u64 size) | |
98e1da90 | 2023 | { |
03b6ea9b AH |
2024 | return size > max_size || offset + size > endp; |
2025 | } | |
98e1da90 | 2026 | |
03b6ea9b AH |
2027 | #define OVERFLOW_CHECK(offset, size, max_size) \ |
2028 | do { \ | |
2029 | if (overflow(endp, (max_size), (offset), (size))) \ | |
2030 | return -EFAULT; \ | |
2031 | } while (0) | |
98e1da90 | 2032 | |
03b6ea9b AH |
2033 | #define OVERFLOW_CHECK_u64(offset) \ |
2034 | OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64)) | |
98e1da90 | 2035 | |
a3f698fe | 2036 | int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event, |
0807d2d8 | 2037 | struct perf_sample *data) |
d0dd74e8 | 2038 | { |
a3f698fe | 2039 | u64 type = evsel->attr.sample_type; |
0807d2d8 | 2040 | bool swapped = evsel->needs_swap; |
d0dd74e8 | 2041 | const u64 *array; |
03b6ea9b AH |
2042 | u16 max_size = event->header.size; |
2043 | const void *endp = (void *)event + max_size; | |
2044 | u64 sz; | |
d0dd74e8 | 2045 | |
936be503 DA |
2046 | /* |
2047 | * used for cross-endian analysis. See git commit 65014ab3 | |
2048 | * for why this goofiness is needed. | |
2049 | */ | |
6a11f92e | 2050 | union u64_swap u; |
936be503 | 2051 | |
f3bda2c9 | 2052 | memset(data, 0, sizeof(*data)); |
d0dd74e8 ACM |
2053 | data->cpu = data->pid = data->tid = -1; |
2054 | data->stream_id = data->id = data->time = -1ULL; | |
bc529086 | 2055 | data->period = evsel->attr.sample_period; |
473398a2 | 2056 | data->cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
d0dd74e8 ACM |
2057 | |
2058 | if (event->header.type != PERF_RECORD_SAMPLE) { | |
a3f698fe | 2059 | if (!evsel->attr.sample_id_all) |
d0dd74e8 | 2060 | return 0; |
0807d2d8 | 2061 | return perf_evsel__parse_id_sample(evsel, event, data); |
d0dd74e8 ACM |
2062 | } |
2063 | ||
2064 | array = event->sample.array; | |
2065 | ||
03b6ea9b AH |
2066 | /* |
2067 | * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes | |
2068 | * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to | |
2069 | * check the format does not go past the end of the event. | |
2070 | */ | |
a3f698fe | 2071 | if (evsel->sample_size + sizeof(event->header) > event->header.size) |
a2854124 FW |
2072 | return -EFAULT; |
2073 | ||
75562573 AH |
2074 | data->id = -1ULL; |
2075 | if (type & PERF_SAMPLE_IDENTIFIER) { | |
2076 | data->id = *array; | |
2077 | array++; | |
2078 | } | |
2079 | ||
d0dd74e8 | 2080 | if (type & PERF_SAMPLE_IP) { |
ef89325f | 2081 | data->ip = *array; |
d0dd74e8 ACM |
2082 | array++; |
2083 | } | |
2084 | ||
2085 | if (type & PERF_SAMPLE_TID) { | |
936be503 DA |
2086 | u.val64 = *array; |
2087 | if (swapped) { | |
2088 | /* undo swap of u64, then swap on individual u32s */ | |
2089 | u.val64 = bswap_64(u.val64); | |
2090 | u.val32[0] = bswap_32(u.val32[0]); | |
2091 | u.val32[1] = bswap_32(u.val32[1]); | |
2092 | } | |
2093 | ||
2094 | data->pid = u.val32[0]; | |
2095 | data->tid = u.val32[1]; | |
d0dd74e8 ACM |
2096 | array++; |
2097 | } | |
2098 | ||
2099 | if (type & PERF_SAMPLE_TIME) { | |
2100 | data->time = *array; | |
2101 | array++; | |
2102 | } | |
2103 | ||
7cec0922 | 2104 | data->addr = 0; |
d0dd74e8 ACM |
2105 | if (type & PERF_SAMPLE_ADDR) { |
2106 | data->addr = *array; | |
2107 | array++; | |
2108 | } | |
2109 | ||
d0dd74e8 ACM |
2110 | if (type & PERF_SAMPLE_ID) { |
2111 | data->id = *array; | |
2112 | array++; | |
2113 | } | |
2114 | ||
2115 | if (type & PERF_SAMPLE_STREAM_ID) { | |
2116 | data->stream_id = *array; | |
2117 | array++; | |
2118 | } | |
2119 | ||
2120 | if (type & PERF_SAMPLE_CPU) { | |
936be503 DA |
2121 | |
2122 | u.val64 = *array; | |
2123 | if (swapped) { | |
2124 | /* undo swap of u64, then swap on individual u32s */ | |
2125 | u.val64 = bswap_64(u.val64); | |
2126 | u.val32[0] = bswap_32(u.val32[0]); | |
2127 | } | |
2128 | ||
2129 | data->cpu = u.val32[0]; | |
d0dd74e8 ACM |
2130 | array++; |
2131 | } | |
2132 | ||
2133 | if (type & PERF_SAMPLE_PERIOD) { | |
2134 | data->period = *array; | |
2135 | array++; | |
2136 | } | |
2137 | ||
2138 | if (type & PERF_SAMPLE_READ) { | |
9ede473c JO |
2139 | u64 read_format = evsel->attr.read_format; |
2140 | ||
03b6ea9b | 2141 | OVERFLOW_CHECK_u64(array); |
9ede473c JO |
2142 | if (read_format & PERF_FORMAT_GROUP) |
2143 | data->read.group.nr = *array; | |
2144 | else | |
2145 | data->read.one.value = *array; | |
2146 | ||
2147 | array++; | |
2148 | ||
2149 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { | |
03b6ea9b | 2150 | OVERFLOW_CHECK_u64(array); |
9ede473c JO |
2151 | data->read.time_enabled = *array; |
2152 | array++; | |
2153 | } | |
2154 | ||
2155 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { | |
03b6ea9b | 2156 | OVERFLOW_CHECK_u64(array); |
9ede473c JO |
2157 | data->read.time_running = *array; |
2158 | array++; | |
2159 | } | |
2160 | ||
2161 | /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ | |
2162 | if (read_format & PERF_FORMAT_GROUP) { | |
03b6ea9b AH |
2163 | const u64 max_group_nr = UINT64_MAX / |
2164 | sizeof(struct sample_read_value); | |
2165 | ||
2166 | if (data->read.group.nr > max_group_nr) | |
2167 | return -EFAULT; | |
2168 | sz = data->read.group.nr * | |
2169 | sizeof(struct sample_read_value); | |
2170 | OVERFLOW_CHECK(array, sz, max_size); | |
2171 | data->read.group.values = | |
2172 | (struct sample_read_value *)array; | |
2173 | array = (void *)array + sz; | |
9ede473c | 2174 | } else { |
03b6ea9b | 2175 | OVERFLOW_CHECK_u64(array); |
9ede473c JO |
2176 | data->read.one.id = *array; |
2177 | array++; | |
2178 | } | |
d0dd74e8 ACM |
2179 | } |
2180 | ||
2181 | if (type & PERF_SAMPLE_CALLCHAIN) { | |
03b6ea9b | 2182 | const u64 max_callchain_nr = UINT64_MAX / sizeof(u64); |
98e1da90 | 2183 | |
03b6ea9b AH |
2184 | OVERFLOW_CHECK_u64(array); |
2185 | data->callchain = (struct ip_callchain *)array++; | |
2186 | if (data->callchain->nr > max_callchain_nr) | |
98e1da90 | 2187 | return -EFAULT; |
03b6ea9b AH |
2188 | sz = data->callchain->nr * sizeof(u64); |
2189 | OVERFLOW_CHECK(array, sz, max_size); | |
2190 | array = (void *)array + sz; | |
d0dd74e8 ACM |
2191 | } |
2192 | ||
2193 | if (type & PERF_SAMPLE_RAW) { | |
03b6ea9b | 2194 | OVERFLOW_CHECK_u64(array); |
936be503 | 2195 | u.val64 = *array; |
f69d9d78 JO |
2196 | |
2197 | /* | |
2198 | * Undo swap of u64, then swap on individual u32s, | |
2199 | * get the size of the raw area and undo all of the | |
2200 | * swap. The pevent interface handles endianity by | |
2201 | * itself. | |
2202 | */ | |
2203 | if (swapped) { | |
936be503 DA |
2204 | u.val64 = bswap_64(u.val64); |
2205 | u.val32[0] = bswap_32(u.val32[0]); | |
2206 | u.val32[1] = bswap_32(u.val32[1]); | |
2207 | } | |
936be503 | 2208 | data->raw_size = u.val32[0]; |
f69d9d78 JO |
2209 | |
2210 | /* | |
2211 | * The raw data is aligned on 64bits including the | |
2212 | * u32 size, so it's safe to use mem_bswap_64. | |
2213 | */ | |
2214 | if (swapped) | |
2215 | mem_bswap_64((void *) array, data->raw_size); | |
2216 | ||
03b6ea9b | 2217 | array = (void *)array + sizeof(u32); |
98e1da90 | 2218 | |
03b6ea9b AH |
2219 | OVERFLOW_CHECK(array, data->raw_size, max_size); |
2220 | data->raw_data = (void *)array; | |
2221 | array = (void *)array + data->raw_size; | |
d0dd74e8 ACM |
2222 | } |
2223 | ||
b5387528 | 2224 | if (type & PERF_SAMPLE_BRANCH_STACK) { |
03b6ea9b AH |
2225 | const u64 max_branch_nr = UINT64_MAX / |
2226 | sizeof(struct branch_entry); | |
b5387528 | 2227 | |
03b6ea9b AH |
2228 | OVERFLOW_CHECK_u64(array); |
2229 | data->branch_stack = (struct branch_stack *)array++; | |
b5387528 | 2230 | |
03b6ea9b AH |
2231 | if (data->branch_stack->nr > max_branch_nr) |
2232 | return -EFAULT; | |
b5387528 | 2233 | sz = data->branch_stack->nr * sizeof(struct branch_entry); |
03b6ea9b AH |
2234 | OVERFLOW_CHECK(array, sz, max_size); |
2235 | array = (void *)array + sz; | |
b5387528 | 2236 | } |
0f6a3015 JO |
2237 | |
2238 | if (type & PERF_SAMPLE_REGS_USER) { | |
03b6ea9b | 2239 | OVERFLOW_CHECK_u64(array); |
5b95a4a3 AH |
2240 | data->user_regs.abi = *array; |
2241 | array++; | |
0f6a3015 | 2242 | |
5b95a4a3 | 2243 | if (data->user_regs.abi) { |
352ea45a | 2244 | u64 mask = evsel->attr.sample_regs_user; |
03b6ea9b | 2245 | |
352ea45a | 2246 | sz = hweight_long(mask) * sizeof(u64); |
03b6ea9b | 2247 | OVERFLOW_CHECK(array, sz, max_size); |
352ea45a | 2248 | data->user_regs.mask = mask; |
0f6a3015 | 2249 | data->user_regs.regs = (u64 *)array; |
03b6ea9b | 2250 | array = (void *)array + sz; |
0f6a3015 JO |
2251 | } |
2252 | } | |
2253 | ||
2254 | if (type & PERF_SAMPLE_STACK_USER) { | |
03b6ea9b AH |
2255 | OVERFLOW_CHECK_u64(array); |
2256 | sz = *array++; | |
0f6a3015 JO |
2257 | |
2258 | data->user_stack.offset = ((char *)(array - 1) | |
2259 | - (char *) event); | |
2260 | ||
03b6ea9b | 2261 | if (!sz) { |
0f6a3015 JO |
2262 | data->user_stack.size = 0; |
2263 | } else { | |
03b6ea9b | 2264 | OVERFLOW_CHECK(array, sz, max_size); |
0f6a3015 | 2265 | data->user_stack.data = (char *)array; |
03b6ea9b AH |
2266 | array = (void *)array + sz; |
2267 | OVERFLOW_CHECK_u64(array); | |
54bd2692 | 2268 | data->user_stack.size = *array++; |
a65cb4b9 JO |
2269 | if (WARN_ONCE(data->user_stack.size > sz, |
2270 | "user stack dump failure\n")) | |
2271 | return -EFAULT; | |
0f6a3015 JO |
2272 | } |
2273 | } | |
2274 | ||
05484298 | 2275 | if (type & PERF_SAMPLE_WEIGHT) { |
03b6ea9b | 2276 | OVERFLOW_CHECK_u64(array); |
05484298 AK |
2277 | data->weight = *array; |
2278 | array++; | |
2279 | } | |
2280 | ||
98a3b32c SE |
2281 | data->data_src = PERF_MEM_DATA_SRC_NONE; |
2282 | if (type & PERF_SAMPLE_DATA_SRC) { | |
03b6ea9b | 2283 | OVERFLOW_CHECK_u64(array); |
98a3b32c SE |
2284 | data->data_src = *array; |
2285 | array++; | |
2286 | } | |
2287 | ||
475eeab9 AK |
2288 | data->transaction = 0; |
2289 | if (type & PERF_SAMPLE_TRANSACTION) { | |
87b95524 | 2290 | OVERFLOW_CHECK_u64(array); |
475eeab9 AK |
2291 | data->transaction = *array; |
2292 | array++; | |
2293 | } | |
2294 | ||
6a21c0b5 SE |
2295 | data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE; |
2296 | if (type & PERF_SAMPLE_REGS_INTR) { | |
2297 | OVERFLOW_CHECK_u64(array); | |
2298 | data->intr_regs.abi = *array; | |
2299 | array++; | |
2300 | ||
2301 | if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) { | |
2302 | u64 mask = evsel->attr.sample_regs_intr; | |
2303 | ||
2304 | sz = hweight_long(mask) * sizeof(u64); | |
2305 | OVERFLOW_CHECK(array, sz, max_size); | |
2306 | data->intr_regs.mask = mask; | |
2307 | data->intr_regs.regs = (u64 *)array; | |
2308 | array = (void *)array + sz; | |
2309 | } | |
2310 | } | |
2311 | ||
3b0a5daa KL |
2312 | data->phys_addr = 0; |
2313 | if (type & PERF_SAMPLE_PHYS_ADDR) { | |
2314 | data->phys_addr = *array; | |
2315 | array++; | |
2316 | } | |
2317 | ||
d0dd74e8 ACM |
2318 | return 0; |
2319 | } | |
74eec26f | 2320 | |
b1cf6f65 | 2321 | size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type, |
352ea45a | 2322 | u64 read_format) |
b1cf6f65 AH |
2323 | { |
2324 | size_t sz, result = sizeof(struct sample_event); | |
2325 | ||
2326 | if (type & PERF_SAMPLE_IDENTIFIER) | |
2327 | result += sizeof(u64); | |
2328 | ||
2329 | if (type & PERF_SAMPLE_IP) | |
2330 | result += sizeof(u64); | |
2331 | ||
2332 | if (type & PERF_SAMPLE_TID) | |
2333 | result += sizeof(u64); | |
2334 | ||
2335 | if (type & PERF_SAMPLE_TIME) | |
2336 | result += sizeof(u64); | |
2337 | ||
2338 | if (type & PERF_SAMPLE_ADDR) | |
2339 | result += sizeof(u64); | |
2340 | ||
2341 | if (type & PERF_SAMPLE_ID) | |
2342 | result += sizeof(u64); | |
2343 | ||
2344 | if (type & PERF_SAMPLE_STREAM_ID) | |
2345 | result += sizeof(u64); | |
2346 | ||
2347 | if (type & PERF_SAMPLE_CPU) | |
2348 | result += sizeof(u64); | |
2349 | ||
2350 | if (type & PERF_SAMPLE_PERIOD) | |
2351 | result += sizeof(u64); | |
2352 | ||
2353 | if (type & PERF_SAMPLE_READ) { | |
2354 | result += sizeof(u64); | |
2355 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
2356 | result += sizeof(u64); | |
2357 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
2358 | result += sizeof(u64); | |
2359 | /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ | |
2360 | if (read_format & PERF_FORMAT_GROUP) { | |
2361 | sz = sample->read.group.nr * | |
2362 | sizeof(struct sample_read_value); | |
2363 | result += sz; | |
2364 | } else { | |
2365 | result += sizeof(u64); | |
2366 | } | |
2367 | } | |
2368 | ||
2369 | if (type & PERF_SAMPLE_CALLCHAIN) { | |
2370 | sz = (sample->callchain->nr + 1) * sizeof(u64); | |
2371 | result += sz; | |
2372 | } | |
2373 | ||
2374 | if (type & PERF_SAMPLE_RAW) { | |
2375 | result += sizeof(u32); | |
2376 | result += sample->raw_size; | |
2377 | } | |
2378 | ||
2379 | if (type & PERF_SAMPLE_BRANCH_STACK) { | |
2380 | sz = sample->branch_stack->nr * sizeof(struct branch_entry); | |
2381 | sz += sizeof(u64); | |
2382 | result += sz; | |
2383 | } | |
2384 | ||
2385 | if (type & PERF_SAMPLE_REGS_USER) { | |
2386 | if (sample->user_regs.abi) { | |
2387 | result += sizeof(u64); | |
352ea45a | 2388 | sz = hweight_long(sample->user_regs.mask) * sizeof(u64); |
b1cf6f65 AH |
2389 | result += sz; |
2390 | } else { | |
2391 | result += sizeof(u64); | |
2392 | } | |
2393 | } | |
2394 | ||
2395 | if (type & PERF_SAMPLE_STACK_USER) { | |
2396 | sz = sample->user_stack.size; | |
2397 | result += sizeof(u64); | |
2398 | if (sz) { | |
2399 | result += sz; | |
2400 | result += sizeof(u64); | |
2401 | } | |
2402 | } | |
2403 | ||
2404 | if (type & PERF_SAMPLE_WEIGHT) | |
2405 | result += sizeof(u64); | |
2406 | ||
2407 | if (type & PERF_SAMPLE_DATA_SRC) | |
2408 | result += sizeof(u64); | |
2409 | ||
42d88910 AH |
2410 | if (type & PERF_SAMPLE_TRANSACTION) |
2411 | result += sizeof(u64); | |
2412 | ||
6a21c0b5 SE |
2413 | if (type & PERF_SAMPLE_REGS_INTR) { |
2414 | if (sample->intr_regs.abi) { | |
2415 | result += sizeof(u64); | |
2416 | sz = hweight_long(sample->intr_regs.mask) * sizeof(u64); | |
2417 | result += sz; | |
2418 | } else { | |
2419 | result += sizeof(u64); | |
2420 | } | |
2421 | } | |
2422 | ||
3b0a5daa KL |
2423 | if (type & PERF_SAMPLE_PHYS_ADDR) |
2424 | result += sizeof(u64); | |
2425 | ||
b1cf6f65 AH |
2426 | return result; |
2427 | } | |
2428 | ||
74eec26f | 2429 | int perf_event__synthesize_sample(union perf_event *event, u64 type, |
352ea45a | 2430 | u64 read_format, |
74eec26f AV |
2431 | const struct perf_sample *sample, |
2432 | bool swapped) | |
2433 | { | |
2434 | u64 *array; | |
d03f2170 | 2435 | size_t sz; |
74eec26f AV |
2436 | /* |
2437 | * used for cross-endian analysis. See git commit 65014ab3 | |
2438 | * for why this goofiness is needed. | |
2439 | */ | |
6a11f92e | 2440 | union u64_swap u; |
74eec26f AV |
2441 | |
2442 | array = event->sample.array; | |
2443 | ||
75562573 AH |
2444 | if (type & PERF_SAMPLE_IDENTIFIER) { |
2445 | *array = sample->id; | |
2446 | array++; | |
2447 | } | |
2448 | ||
74eec26f | 2449 | if (type & PERF_SAMPLE_IP) { |
ef89325f | 2450 | *array = sample->ip; |
74eec26f AV |
2451 | array++; |
2452 | } | |
2453 | ||
2454 | if (type & PERF_SAMPLE_TID) { | |
2455 | u.val32[0] = sample->pid; | |
2456 | u.val32[1] = sample->tid; | |
2457 | if (swapped) { | |
2458 | /* | |
a3f698fe | 2459 | * Inverse of what is done in perf_evsel__parse_sample |
74eec26f AV |
2460 | */ |
2461 | u.val32[0] = bswap_32(u.val32[0]); | |
2462 | u.val32[1] = bswap_32(u.val32[1]); | |
2463 | u.val64 = bswap_64(u.val64); | |
2464 | } | |
2465 | ||
2466 | *array = u.val64; | |
2467 | array++; | |
2468 | } | |
2469 | ||
2470 | if (type & PERF_SAMPLE_TIME) { | |
2471 | *array = sample->time; | |
2472 | array++; | |
2473 | } | |
2474 | ||
2475 | if (type & PERF_SAMPLE_ADDR) { | |
2476 | *array = sample->addr; | |
2477 | array++; | |
2478 | } | |
2479 | ||
2480 | if (type & PERF_SAMPLE_ID) { | |
2481 | *array = sample->id; | |
2482 | array++; | |
2483 | } | |
2484 | ||
2485 | if (type & PERF_SAMPLE_STREAM_ID) { | |
2486 | *array = sample->stream_id; | |
2487 | array++; | |
2488 | } | |
2489 | ||
2490 | if (type & PERF_SAMPLE_CPU) { | |
2491 | u.val32[0] = sample->cpu; | |
2492 | if (swapped) { | |
2493 | /* | |
a3f698fe | 2494 | * Inverse of what is done in perf_evsel__parse_sample |
74eec26f AV |
2495 | */ |
2496 | u.val32[0] = bswap_32(u.val32[0]); | |
2497 | u.val64 = bswap_64(u.val64); | |
2498 | } | |
2499 | *array = u.val64; | |
2500 | array++; | |
2501 | } | |
2502 | ||
2503 | if (type & PERF_SAMPLE_PERIOD) { | |
2504 | *array = sample->period; | |
2505 | array++; | |
2506 | } | |
2507 | ||
d03f2170 AH |
2508 | if (type & PERF_SAMPLE_READ) { |
2509 | if (read_format & PERF_FORMAT_GROUP) | |
2510 | *array = sample->read.group.nr; | |
2511 | else | |
2512 | *array = sample->read.one.value; | |
2513 | array++; | |
2514 | ||
2515 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { | |
2516 | *array = sample->read.time_enabled; | |
2517 | array++; | |
2518 | } | |
2519 | ||
2520 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { | |
2521 | *array = sample->read.time_running; | |
2522 | array++; | |
2523 | } | |
2524 | ||
2525 | /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ | |
2526 | if (read_format & PERF_FORMAT_GROUP) { | |
2527 | sz = sample->read.group.nr * | |
2528 | sizeof(struct sample_read_value); | |
2529 | memcpy(array, sample->read.group.values, sz); | |
2530 | array = (void *)array + sz; | |
2531 | } else { | |
2532 | *array = sample->read.one.id; | |
2533 | array++; | |
2534 | } | |
2535 | } | |
2536 | ||
2537 | if (type & PERF_SAMPLE_CALLCHAIN) { | |
2538 | sz = (sample->callchain->nr + 1) * sizeof(u64); | |
2539 | memcpy(array, sample->callchain, sz); | |
2540 | array = (void *)array + sz; | |
2541 | } | |
2542 | ||
2543 | if (type & PERF_SAMPLE_RAW) { | |
2544 | u.val32[0] = sample->raw_size; | |
2545 | if (WARN_ONCE(swapped, | |
2546 | "Endianness of raw data not corrected!\n")) { | |
2547 | /* | |
2548 | * Inverse of what is done in perf_evsel__parse_sample | |
2549 | */ | |
2550 | u.val32[0] = bswap_32(u.val32[0]); | |
2551 | u.val32[1] = bswap_32(u.val32[1]); | |
2552 | u.val64 = bswap_64(u.val64); | |
2553 | } | |
2554 | *array = u.val64; | |
2555 | array = (void *)array + sizeof(u32); | |
2556 | ||
2557 | memcpy(array, sample->raw_data, sample->raw_size); | |
2558 | array = (void *)array + sample->raw_size; | |
2559 | } | |
2560 | ||
2561 | if (type & PERF_SAMPLE_BRANCH_STACK) { | |
2562 | sz = sample->branch_stack->nr * sizeof(struct branch_entry); | |
2563 | sz += sizeof(u64); | |
2564 | memcpy(array, sample->branch_stack, sz); | |
2565 | array = (void *)array + sz; | |
2566 | } | |
2567 | ||
2568 | if (type & PERF_SAMPLE_REGS_USER) { | |
2569 | if (sample->user_regs.abi) { | |
2570 | *array++ = sample->user_regs.abi; | |
352ea45a | 2571 | sz = hweight_long(sample->user_regs.mask) * sizeof(u64); |
d03f2170 AH |
2572 | memcpy(array, sample->user_regs.regs, sz); |
2573 | array = (void *)array + sz; | |
2574 | } else { | |
2575 | *array++ = 0; | |
2576 | } | |
2577 | } | |
2578 | ||
2579 | if (type & PERF_SAMPLE_STACK_USER) { | |
2580 | sz = sample->user_stack.size; | |
2581 | *array++ = sz; | |
2582 | if (sz) { | |
2583 | memcpy(array, sample->user_stack.data, sz); | |
2584 | array = (void *)array + sz; | |
2585 | *array++ = sz; | |
2586 | } | |
2587 | } | |
2588 | ||
2589 | if (type & PERF_SAMPLE_WEIGHT) { | |
2590 | *array = sample->weight; | |
2591 | array++; | |
2592 | } | |
2593 | ||
2594 | if (type & PERF_SAMPLE_DATA_SRC) { | |
2595 | *array = sample->data_src; | |
2596 | array++; | |
2597 | } | |
2598 | ||
42d88910 AH |
2599 | if (type & PERF_SAMPLE_TRANSACTION) { |
2600 | *array = sample->transaction; | |
2601 | array++; | |
2602 | } | |
2603 | ||
6a21c0b5 SE |
2604 | if (type & PERF_SAMPLE_REGS_INTR) { |
2605 | if (sample->intr_regs.abi) { | |
2606 | *array++ = sample->intr_regs.abi; | |
2607 | sz = hweight_long(sample->intr_regs.mask) * sizeof(u64); | |
2608 | memcpy(array, sample->intr_regs.regs, sz); | |
2609 | array = (void *)array + sz; | |
2610 | } else { | |
2611 | *array++ = 0; | |
2612 | } | |
2613 | } | |
2614 | ||
3b0a5daa KL |
2615 | if (type & PERF_SAMPLE_PHYS_ADDR) { |
2616 | *array = sample->phys_addr; | |
2617 | array++; | |
2618 | } | |
2619 | ||
74eec26f AV |
2620 | return 0; |
2621 | } | |
5555ded4 | 2622 | |
efd2b924 ACM |
2623 | struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name) |
2624 | { | |
2625 | return pevent_find_field(evsel->tp_format, name); | |
2626 | } | |
2627 | ||
5d2074ea | 2628 | void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample, |
5555ded4 ACM |
2629 | const char *name) |
2630 | { | |
efd2b924 | 2631 | struct format_field *field = perf_evsel__field(evsel, name); |
5555ded4 ACM |
2632 | int offset; |
2633 | ||
efd2b924 ACM |
2634 | if (!field) |
2635 | return NULL; | |
5555ded4 ACM |
2636 | |
2637 | offset = field->offset; | |
2638 | ||
2639 | if (field->flags & FIELD_IS_DYNAMIC) { | |
2640 | offset = *(int *)(sample->raw_data + field->offset); | |
2641 | offset &= 0xffff; | |
2642 | } | |
2643 | ||
2644 | return sample->raw_data + offset; | |
2645 | } | |
2646 | ||
90525176 ACM |
2647 | u64 format_field__intval(struct format_field *field, struct perf_sample *sample, |
2648 | bool needs_swap) | |
5555ded4 | 2649 | { |
e6b6f679 | 2650 | u64 value; |
90525176 | 2651 | void *ptr = sample->raw_data + field->offset; |
5555ded4 | 2652 | |
e6b6f679 ACM |
2653 | switch (field->size) { |
2654 | case 1: | |
2655 | return *(u8 *)ptr; | |
2656 | case 2: | |
2657 | value = *(u16 *)ptr; | |
2658 | break; | |
2659 | case 4: | |
2660 | value = *(u32 *)ptr; | |
2661 | break; | |
2662 | case 8: | |
e94eedab | 2663 | memcpy(&value, ptr, sizeof(u64)); |
e6b6f679 ACM |
2664 | break; |
2665 | default: | |
2666 | return 0; | |
2667 | } | |
2668 | ||
90525176 | 2669 | if (!needs_swap) |
e6b6f679 ACM |
2670 | return value; |
2671 | ||
2672 | switch (field->size) { | |
2673 | case 2: | |
2674 | return bswap_16(value); | |
2675 | case 4: | |
2676 | return bswap_32(value); | |
2677 | case 8: | |
2678 | return bswap_64(value); | |
2679 | default: | |
2680 | return 0; | |
2681 | } | |
2682 | ||
2683 | return 0; | |
5555ded4 | 2684 | } |
0698aedd | 2685 | |
90525176 ACM |
2686 | u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample, |
2687 | const char *name) | |
2688 | { | |
2689 | struct format_field *field = perf_evsel__field(evsel, name); | |
2690 | ||
2691 | if (!field) | |
2692 | return 0; | |
2693 | ||
2694 | return field ? format_field__intval(field, sample, evsel->needs_swap) : 0; | |
2695 | } | |
2696 | ||
c0a54341 ACM |
2697 | bool perf_evsel__fallback(struct perf_evsel *evsel, int err, |
2698 | char *msg, size_t msgsize) | |
2699 | { | |
08094828 ACM |
2700 | int paranoid; |
2701 | ||
2b821cce | 2702 | if ((err == ENOENT || err == ENXIO || err == ENODEV) && |
c0a54341 ACM |
2703 | evsel->attr.type == PERF_TYPE_HARDWARE && |
2704 | evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) { | |
2705 | /* | |
2706 | * If it's cycles then fall back to hrtimer based | |
2707 | * cpu-clock-tick sw counter, which is always available even if | |
2708 | * no PMU support. | |
2709 | * | |
2710 | * PPC returns ENXIO until 2.6.37 (behavior changed with commit | |
2711 | * b0a873e). | |
2712 | */ | |
2713 | scnprintf(msg, msgsize, "%s", | |
2714 | "The cycles event is not supported, trying to fall back to cpu-clock-ticks"); | |
2715 | ||
2716 | evsel->attr.type = PERF_TYPE_SOFTWARE; | |
2717 | evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK; | |
2718 | ||
04662523 | 2719 | zfree(&evsel->name); |
08094828 ACM |
2720 | return true; |
2721 | } else if (err == EACCES && !evsel->attr.exclude_kernel && | |
2722 | (paranoid = perf_event_paranoid()) > 1) { | |
2723 | const char *name = perf_evsel__name(evsel); | |
2724 | char *new_name; | |
2725 | ||
2726 | if (asprintf(&new_name, "%s%su", name, strchr(name, ':') ? "" : ":") < 0) | |
2727 | return false; | |
2728 | ||
2729 | if (evsel->name) | |
2730 | free(evsel->name); | |
2731 | evsel->name = new_name; | |
2732 | scnprintf(msg, msgsize, | |
2733 | "kernel.perf_event_paranoid=%d, trying to fall back to excluding kernel samples", paranoid); | |
2734 | evsel->attr.exclude_kernel = 1; | |
2735 | ||
c0a54341 ACM |
2736 | return true; |
2737 | } | |
2738 | ||
2739 | return false; | |
2740 | } | |
56e52e85 | 2741 | |
2157f6ee ACM |
2742 | static bool find_process(const char *name) |
2743 | { | |
2744 | size_t len = strlen(name); | |
2745 | DIR *dir; | |
2746 | struct dirent *d; | |
2747 | int ret = -1; | |
2748 | ||
2749 | dir = opendir(procfs__mountpoint()); | |
2750 | if (!dir) | |
2751 | return false; | |
2752 | ||
2753 | /* Walk through the directory. */ | |
2754 | while (ret && (d = readdir(dir)) != NULL) { | |
2755 | char path[PATH_MAX]; | |
2756 | char *data; | |
2757 | size_t size; | |
2758 | ||
2759 | if ((d->d_type != DT_DIR) || | |
2760 | !strcmp(".", d->d_name) || | |
2761 | !strcmp("..", d->d_name)) | |
2762 | continue; | |
2763 | ||
2764 | scnprintf(path, sizeof(path), "%s/%s/comm", | |
2765 | procfs__mountpoint(), d->d_name); | |
2766 | ||
2767 | if (filename__read_str(path, &data, &size)) | |
2768 | continue; | |
2769 | ||
2770 | ret = strncmp(name, data, len); | |
2771 | free(data); | |
2772 | } | |
2773 | ||
2774 | closedir(dir); | |
2775 | return ret ? false : true; | |
2776 | } | |
2777 | ||
602ad878 | 2778 | int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target, |
56e52e85 ACM |
2779 | int err, char *msg, size_t size) |
2780 | { | |
6e81c74c | 2781 | char sbuf[STRERR_BUFSIZE]; |
32ccb130 | 2782 | int printed = 0; |
6e81c74c | 2783 | |
56e52e85 ACM |
2784 | switch (err) { |
2785 | case EPERM: | |
2786 | case EACCES: | |
32ccb130 JY |
2787 | if (err == EPERM) |
2788 | printed = scnprintf(msg, size, | |
2789 | "No permission to enable %s event.\n\n", | |
2790 | perf_evsel__name(evsel)); | |
2791 | ||
2792 | return scnprintf(msg + printed, size - printed, | |
3379e0c3 BH |
2793 | "You may not have permission to collect %sstats.\n\n" |
2794 | "Consider tweaking /proc/sys/kernel/perf_event_paranoid,\n" | |
2795 | "which controls use of the performance events system by\n" | |
2796 | "unprivileged users (without CAP_SYS_ADMIN).\n\n" | |
7d173913 | 2797 | "The current value is %d:\n\n" |
3379e0c3 | 2798 | " -1: Allow use of (almost) all events by all users\n" |
ac0bb6b7 KK |
2799 | " Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK\n" |
2800 | ">= 0: Disallow ftrace function tracepoint by users without CAP_SYS_ADMIN\n" | |
2801 | " Disallow raw tracepoint access by users without CAP_SYS_ADMIN\n" | |
3379e0c3 | 2802 | ">= 1: Disallow CPU event access by users without CAP_SYS_ADMIN\n" |
d6195a6a ACM |
2803 | ">= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN\n\n" |
2804 | "To make this setting permanent, edit /etc/sysctl.conf too, e.g.:\n\n" | |
2805 | " kernel.perf_event_paranoid = -1\n" , | |
7d173913 ACM |
2806 | target->system_wide ? "system-wide " : "", |
2807 | perf_event_paranoid()); | |
56e52e85 ACM |
2808 | case ENOENT: |
2809 | return scnprintf(msg, size, "The %s event is not supported.", | |
2810 | perf_evsel__name(evsel)); | |
2811 | case EMFILE: | |
2812 | return scnprintf(msg, size, "%s", | |
2813 | "Too many events are opened.\n" | |
18ffdfe8 JO |
2814 | "Probably the maximum number of open file descriptors has been reached.\n" |
2815 | "Hint: Try again after reducing the number of events.\n" | |
2816 | "Hint: Try increasing the limit with 'ulimit -n <limit>'"); | |
de46d526 ACM |
2817 | case ENOMEM: |
2818 | if ((evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) != 0 && | |
2819 | access("/proc/sys/kernel/perf_event_max_stack", F_OK) == 0) | |
2820 | return scnprintf(msg, size, | |
2821 | "Not enough memory to setup event with callchain.\n" | |
2822 | "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n" | |
2823 | "Hint: Current value: %d", sysctl_perf_event_max_stack); | |
2824 | break; | |
56e52e85 ACM |
2825 | case ENODEV: |
2826 | if (target->cpu_list) | |
2827 | return scnprintf(msg, size, "%s", | |
81d64f46 | 2828 | "No such device - did you specify an out-of-range profile CPU?"); |
56e52e85 ACM |
2829 | break; |
2830 | case EOPNOTSUPP: | |
dc89e75a VG |
2831 | if (evsel->attr.sample_period != 0) |
2832 | return scnprintf(msg, size, "%s", | |
2833 | "PMU Hardware doesn't support sampling/overflow-interrupts."); | |
56e52e85 ACM |
2834 | if (evsel->attr.precise_ip) |
2835 | return scnprintf(msg, size, "%s", | |
2836 | "\'precise\' request may not be supported. Try removing 'p' modifier."); | |
2837 | #if defined(__i386__) || defined(__x86_64__) | |
2838 | if (evsel->attr.type == PERF_TYPE_HARDWARE) | |
2839 | return scnprintf(msg, size, "%s", | |
2840 | "No hardware sampling interrupt available.\n" | |
2841 | "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it."); | |
2842 | #endif | |
2843 | break; | |
63914aca JO |
2844 | case EBUSY: |
2845 | if (find_process("oprofiled")) | |
2846 | return scnprintf(msg, size, | |
2847 | "The PMU counters are busy/taken by another profiler.\n" | |
2848 | "We found oprofile daemon running, please stop it and try again."); | |
2849 | break; | |
814c8c38 | 2850 | case EINVAL: |
32a951b4 | 2851 | if (evsel->attr.write_backward && perf_missing_features.write_backward) |
7da36e94 | 2852 | return scnprintf(msg, size, "Reading from overwrite event is not supported by this kernel."); |
814c8c38 PZ |
2853 | if (perf_missing_features.clockid) |
2854 | return scnprintf(msg, size, "clockid feature not supported."); | |
2855 | if (perf_missing_features.clockid_wrong) | |
2856 | return scnprintf(msg, size, "wrong clockid (%d).", clockid); | |
2857 | break; | |
56e52e85 ACM |
2858 | default: |
2859 | break; | |
2860 | } | |
2861 | ||
2862 | return scnprintf(msg, size, | |
6e81c74c | 2863 | "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n" |
56e52e85 | 2864 | "/bin/dmesg may provide additional information.\n" |
81d64f46 | 2865 | "No CONFIG_PERF_EVENTS=y kernel support configured?", |
c8b5f2c9 | 2866 | err, str_error_r(err, sbuf, sizeof(sbuf)), |
6e81c74c | 2867 | perf_evsel__name(evsel)); |
56e52e85 | 2868 | } |
f4e47f9f RB |
2869 | |
2870 | char *perf_evsel__env_arch(struct perf_evsel *evsel) | |
2871 | { | |
2872 | if (evsel && evsel->evlist && evsel->evlist->env) | |
2873 | return evsel->evlist->env->arch; | |
2874 | return NULL; | |
2875 | } | |
69fb09f6 JY |
2876 | |
2877 | char *perf_evsel__env_cpuid(struct perf_evsel *evsel) | |
2878 | { | |
2879 | if (evsel && evsel->evlist && evsel->evlist->env) | |
2880 | return evsel->evlist->env->cpuid; | |
2881 | return NULL; | |
2882 | } |