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