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