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