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 && | |
710 | (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu))) | |
7be5ebe8 | 711 | perf_evsel__set_sample_bit(evsel, TIME); |
0f82ebc4 | 712 | |
6ff1ce76 | 713 | if (opts->raw_samples && !evsel->no_aux_samples) { |
7be5ebe8 ACM |
714 | perf_evsel__set_sample_bit(evsel, TIME); |
715 | perf_evsel__set_sample_bit(evsel, RAW); | |
716 | perf_evsel__set_sample_bit(evsel, CPU); | |
0f82ebc4 ACM |
717 | } |
718 | ||
ccf49bfc | 719 | if (opts->sample_address) |
1e7ed5ec | 720 | perf_evsel__set_sample_bit(evsel, DATA_SRC); |
ccf49bfc | 721 | |
509051ea | 722 | if (opts->no_buffering) { |
0f82ebc4 ACM |
723 | attr->watermark = 0; |
724 | attr->wakeup_events = 1; | |
725 | } | |
6ff1ce76 | 726 | if (opts->branch_stack && !evsel->no_aux_samples) { |
7be5ebe8 | 727 | perf_evsel__set_sample_bit(evsel, BRANCH_STACK); |
bdfebd84 RAV |
728 | attr->branch_sample_type = opts->branch_stack; |
729 | } | |
0f82ebc4 | 730 | |
05484298 | 731 | if (opts->sample_weight) |
1e7ed5ec | 732 | perf_evsel__set_sample_bit(evsel, WEIGHT); |
05484298 | 733 | |
62e503b7 | 734 | attr->task = track; |
5c5e854b | 735 | attr->mmap = track; |
a5a5ba72 | 736 | attr->mmap2 = track && !perf_missing_features.mmap2; |
5c5e854b | 737 | attr->comm = track; |
0f82ebc4 | 738 | |
475eeab9 | 739 | if (opts->sample_transaction) |
1e7ed5ec | 740 | perf_evsel__set_sample_bit(evsel, TRANSACTION); |
475eeab9 | 741 | |
85c273d2 AK |
742 | if (opts->running_time) { |
743 | evsel->attr.read_format |= | |
744 | PERF_FORMAT_TOTAL_TIME_ENABLED | | |
745 | PERF_FORMAT_TOTAL_TIME_RUNNING; | |
746 | } | |
747 | ||
774cb499 JO |
748 | /* |
749 | * XXX see the function comment above | |
750 | * | |
751 | * Disabling only independent events or group leaders, | |
752 | * keeping group members enabled. | |
753 | */ | |
823254ed | 754 | if (perf_evsel__is_group_leader(evsel)) |
774cb499 JO |
755 | attr->disabled = 1; |
756 | ||
757 | /* | |
758 | * Setting enable_on_exec for independent events and | |
759 | * group leaders for traced executed by perf. | |
760 | */ | |
6619a53e AK |
761 | if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) && |
762 | !opts->initial_delay) | |
0f82ebc4 | 763 | attr->enable_on_exec = 1; |
2afd2bcf AH |
764 | |
765 | if (evsel->immediate) { | |
766 | attr->disabled = 0; | |
767 | attr->enable_on_exec = 0; | |
768 | } | |
814c8c38 PZ |
769 | |
770 | clockid = opts->clockid; | |
771 | if (opts->use_clockid) { | |
772 | attr->use_clockid = 1; | |
773 | attr->clockid = opts->clockid; | |
774 | } | |
0f82ebc4 ACM |
775 | } |
776 | ||
8885846f | 777 | static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads) |
69aad6f1 | 778 | { |
4af4c955 | 779 | int cpu, thread; |
bf8e8f4b AH |
780 | |
781 | if (evsel->system_wide) | |
782 | nthreads = 1; | |
783 | ||
69aad6f1 | 784 | evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int)); |
4af4c955 DA |
785 | |
786 | if (evsel->fd) { | |
787 | for (cpu = 0; cpu < ncpus; cpu++) { | |
788 | for (thread = 0; thread < nthreads; thread++) { | |
789 | FD(evsel, cpu, thread) = -1; | |
790 | } | |
791 | } | |
792 | } | |
793 | ||
69aad6f1 ACM |
794 | return evsel->fd != NULL ? 0 : -ENOMEM; |
795 | } | |
796 | ||
e2407bef AK |
797 | static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads, |
798 | int ioc, void *arg) | |
745cefc5 ACM |
799 | { |
800 | int cpu, thread; | |
801 | ||
bf8e8f4b AH |
802 | if (evsel->system_wide) |
803 | nthreads = 1; | |
804 | ||
745cefc5 ACM |
805 | for (cpu = 0; cpu < ncpus; cpu++) { |
806 | for (thread = 0; thread < nthreads; thread++) { | |
807 | int fd = FD(evsel, cpu, thread), | |
e2407bef | 808 | err = ioctl(fd, ioc, arg); |
745cefc5 ACM |
809 | |
810 | if (err) | |
811 | return err; | |
812 | } | |
813 | } | |
814 | ||
815 | return 0; | |
816 | } | |
817 | ||
f47805a2 ACM |
818 | int perf_evsel__apply_filter(struct perf_evsel *evsel, int ncpus, int nthreads, |
819 | const char *filter) | |
e2407bef AK |
820 | { |
821 | return perf_evsel__run_ioctl(evsel, ncpus, nthreads, | |
822 | PERF_EVENT_IOC_SET_FILTER, | |
823 | (void *)filter); | |
824 | } | |
825 | ||
12467ae4 ACM |
826 | int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter) |
827 | { | |
828 | char *new_filter = strdup(filter); | |
829 | ||
830 | if (new_filter != NULL) { | |
831 | free(evsel->filter); | |
832 | evsel->filter = new_filter; | |
833 | return 0; | |
834 | } | |
835 | ||
836 | return -1; | |
837 | } | |
838 | ||
e2407bef AK |
839 | int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads) |
840 | { | |
841 | return perf_evsel__run_ioctl(evsel, ncpus, nthreads, | |
842 | PERF_EVENT_IOC_ENABLE, | |
843 | 0); | |
844 | } | |
845 | ||
70db7533 ACM |
846 | int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads) |
847 | { | |
8d9cbd8f VG |
848 | if (ncpus == 0 || nthreads == 0) |
849 | return 0; | |
850 | ||
bf8e8f4b AH |
851 | if (evsel->system_wide) |
852 | nthreads = 1; | |
853 | ||
a91e5431 ACM |
854 | evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id)); |
855 | if (evsel->sample_id == NULL) | |
856 | return -ENOMEM; | |
857 | ||
858 | evsel->id = zalloc(ncpus * nthreads * sizeof(u64)); | |
859 | if (evsel->id == NULL) { | |
860 | xyarray__delete(evsel->sample_id); | |
861 | evsel->sample_id = NULL; | |
862 | return -ENOMEM; | |
863 | } | |
864 | ||
865 | return 0; | |
70db7533 ACM |
866 | } |
867 | ||
8885846f | 868 | static void perf_evsel__free_fd(struct perf_evsel *evsel) |
69aad6f1 ACM |
869 | { |
870 | xyarray__delete(evsel->fd); | |
871 | evsel->fd = NULL; | |
872 | } | |
873 | ||
8885846f | 874 | static void perf_evsel__free_id(struct perf_evsel *evsel) |
70db7533 | 875 | { |
a91e5431 ACM |
876 | xyarray__delete(evsel->sample_id); |
877 | evsel->sample_id = NULL; | |
04662523 | 878 | zfree(&evsel->id); |
70db7533 ACM |
879 | } |
880 | ||
c52b12ed ACM |
881 | void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads) |
882 | { | |
883 | int cpu, thread; | |
884 | ||
bf8e8f4b AH |
885 | if (evsel->system_wide) |
886 | nthreads = 1; | |
887 | ||
c52b12ed ACM |
888 | for (cpu = 0; cpu < ncpus; cpu++) |
889 | for (thread = 0; thread < nthreads; ++thread) { | |
890 | close(FD(evsel, cpu, thread)); | |
891 | FD(evsel, cpu, thread) = -1; | |
892 | } | |
893 | } | |
894 | ||
ef1d1af2 | 895 | void perf_evsel__exit(struct perf_evsel *evsel) |
69aad6f1 ACM |
896 | { |
897 | assert(list_empty(&evsel->node)); | |
736b05a0 NK |
898 | perf_evsel__free_fd(evsel); |
899 | perf_evsel__free_id(evsel); | |
597e48c1 | 900 | close_cgroup(evsel->cgrp); |
f30a79b0 | 901 | cpu_map__put(evsel->cpus); |
578e91ec | 902 | thread_map__put(evsel->threads); |
597e48c1 | 903 | zfree(&evsel->group_name); |
597e48c1 | 904 | zfree(&evsel->name); |
ce8ccff5 | 905 | perf_evsel__object.fini(evsel); |
ef1d1af2 ACM |
906 | } |
907 | ||
908 | void perf_evsel__delete(struct perf_evsel *evsel) | |
909 | { | |
910 | perf_evsel__exit(evsel); | |
69aad6f1 ACM |
911 | free(evsel); |
912 | } | |
c52b12ed | 913 | |
a6fa0038 | 914 | void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread, |
857a94a2 | 915 | struct perf_counts_values *count) |
c7a79c47 SE |
916 | { |
917 | struct perf_counts_values tmp; | |
918 | ||
919 | if (!evsel->prev_raw_counts) | |
920 | return; | |
921 | ||
922 | if (cpu == -1) { | |
923 | tmp = evsel->prev_raw_counts->aggr; | |
924 | evsel->prev_raw_counts->aggr = *count; | |
925 | } else { | |
a6fa0038 JO |
926 | tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread); |
927 | *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count; | |
c7a79c47 SE |
928 | } |
929 | ||
930 | count->val = count->val - tmp.val; | |
931 | count->ena = count->ena - tmp.ena; | |
932 | count->run = count->run - tmp.run; | |
933 | } | |
934 | ||
13112bbf JO |
935 | void perf_counts_values__scale(struct perf_counts_values *count, |
936 | bool scale, s8 *pscaled) | |
937 | { | |
938 | s8 scaled = 0; | |
939 | ||
940 | if (scale) { | |
941 | if (count->run == 0) { | |
942 | scaled = -1; | |
943 | count->val = 0; | |
944 | } else if (count->run < count->ena) { | |
945 | scaled = 1; | |
946 | count->val = (u64)((double) count->val * count->ena / count->run + 0.5); | |
947 | } | |
948 | } else | |
949 | count->ena = count->run = 0; | |
950 | ||
951 | if (pscaled) | |
952 | *pscaled = scaled; | |
953 | } | |
954 | ||
f99f4719 JO |
955 | int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread, |
956 | struct perf_counts_values *count) | |
957 | { | |
958 | memset(count, 0, sizeof(*count)); | |
959 | ||
960 | if (FD(evsel, cpu, thread) < 0) | |
961 | return -EINVAL; | |
962 | ||
963 | if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) < 0) | |
964 | return -errno; | |
965 | ||
966 | return 0; | |
967 | } | |
968 | ||
c52b12ed ACM |
969 | int __perf_evsel__read_on_cpu(struct perf_evsel *evsel, |
970 | int cpu, int thread, bool scale) | |
971 | { | |
972 | struct perf_counts_values count; | |
973 | size_t nv = scale ? 3 : 1; | |
974 | ||
975 | if (FD(evsel, cpu, thread) < 0) | |
976 | return -EINVAL; | |
977 | ||
a6fa0038 | 978 | if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0) |
4eed11d5 ACM |
979 | return -ENOMEM; |
980 | ||
c52b12ed ACM |
981 | if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0) |
982 | return -errno; | |
983 | ||
a6fa0038 | 984 | perf_evsel__compute_deltas(evsel, cpu, thread, &count); |
13112bbf | 985 | perf_counts_values__scale(&count, scale, NULL); |
a6fa0038 | 986 | *perf_counts(evsel->counts, cpu, thread) = count; |
c52b12ed ACM |
987 | return 0; |
988 | } | |
989 | ||
6a4bb04c JO |
990 | static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread) |
991 | { | |
992 | struct perf_evsel *leader = evsel->leader; | |
993 | int fd; | |
994 | ||
823254ed | 995 | if (perf_evsel__is_group_leader(evsel)) |
6a4bb04c JO |
996 | return -1; |
997 | ||
998 | /* | |
999 | * Leader must be already processed/open, | |
1000 | * if not it's a bug. | |
1001 | */ | |
1002 | BUG_ON(!leader->fd); | |
1003 | ||
1004 | fd = FD(leader, cpu, thread); | |
1005 | BUG_ON(fd == -1); | |
1006 | ||
1007 | return fd; | |
1008 | } | |
1009 | ||
2c5e8c52 PZ |
1010 | struct bit_names { |
1011 | int bit; | |
1012 | const char *name; | |
1013 | }; | |
1014 | ||
1015 | static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits) | |
1016 | { | |
1017 | bool first_bit = true; | |
1018 | int i = 0; | |
1019 | ||
1020 | do { | |
1021 | if (value & bits[i].bit) { | |
1022 | buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name); | |
1023 | first_bit = false; | |
1024 | } | |
1025 | } while (bits[++i].name != NULL); | |
1026 | } | |
1027 | ||
1028 | static void __p_sample_type(char *buf, size_t size, u64 value) | |
1029 | { | |
1030 | #define bit_name(n) { PERF_SAMPLE_##n, #n } | |
1031 | struct bit_names bits[] = { | |
1032 | bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR), | |
1033 | bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU), | |
1034 | bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW), | |
1035 | bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER), | |
1036 | bit_name(IDENTIFIER), bit_name(REGS_INTR), | |
1037 | { .name = NULL, } | |
1038 | }; | |
1039 | #undef bit_name | |
1040 | __p_bits(buf, size, value, bits); | |
1041 | } | |
1042 | ||
1043 | static void __p_read_format(char *buf, size_t size, u64 value) | |
1044 | { | |
1045 | #define bit_name(n) { PERF_FORMAT_##n, #n } | |
1046 | struct bit_names bits[] = { | |
1047 | bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING), | |
1048 | bit_name(ID), bit_name(GROUP), | |
1049 | { .name = NULL, } | |
1050 | }; | |
1051 | #undef bit_name | |
1052 | __p_bits(buf, size, value, bits); | |
1053 | } | |
1054 | ||
1055 | #define BUF_SIZE 1024 | |
1056 | ||
7310aed7 | 1057 | #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val)) |
2c5e8c52 PZ |
1058 | #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val)) |
1059 | #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val)) | |
1060 | #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val) | |
1061 | #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val) | |
1062 | ||
1063 | #define PRINT_ATTRn(_n, _f, _p) \ | |
1064 | do { \ | |
1065 | if (attr->_f) { \ | |
1066 | _p(attr->_f); \ | |
1067 | ret += attr__fprintf(fp, _n, buf, priv);\ | |
1068 | } \ | |
1069 | } while (0) | |
1070 | ||
1071 | #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p) | |
1072 | ||
1073 | int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr, | |
1074 | attr__fprintf_f attr__fprintf, void *priv) | |
1075 | { | |
1076 | char buf[BUF_SIZE]; | |
1077 | int ret = 0; | |
1078 | ||
1079 | PRINT_ATTRf(type, p_unsigned); | |
1080 | PRINT_ATTRf(size, p_unsigned); | |
1081 | PRINT_ATTRf(config, p_hex); | |
1082 | PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned); | |
1083 | PRINT_ATTRf(sample_type, p_sample_type); | |
1084 | PRINT_ATTRf(read_format, p_read_format); | |
1085 | ||
1086 | PRINT_ATTRf(disabled, p_unsigned); | |
1087 | PRINT_ATTRf(inherit, p_unsigned); | |
1088 | PRINT_ATTRf(pinned, p_unsigned); | |
1089 | PRINT_ATTRf(exclusive, p_unsigned); | |
1090 | PRINT_ATTRf(exclude_user, p_unsigned); | |
1091 | PRINT_ATTRf(exclude_kernel, p_unsigned); | |
1092 | PRINT_ATTRf(exclude_hv, p_unsigned); | |
1093 | PRINT_ATTRf(exclude_idle, p_unsigned); | |
1094 | PRINT_ATTRf(mmap, p_unsigned); | |
1095 | PRINT_ATTRf(comm, p_unsigned); | |
1096 | PRINT_ATTRf(freq, p_unsigned); | |
1097 | PRINT_ATTRf(inherit_stat, p_unsigned); | |
1098 | PRINT_ATTRf(enable_on_exec, p_unsigned); | |
1099 | PRINT_ATTRf(task, p_unsigned); | |
1100 | PRINT_ATTRf(watermark, p_unsigned); | |
1101 | PRINT_ATTRf(precise_ip, p_unsigned); | |
1102 | PRINT_ATTRf(mmap_data, p_unsigned); | |
1103 | PRINT_ATTRf(sample_id_all, p_unsigned); | |
1104 | PRINT_ATTRf(exclude_host, p_unsigned); | |
1105 | PRINT_ATTRf(exclude_guest, p_unsigned); | |
1106 | PRINT_ATTRf(exclude_callchain_kernel, p_unsigned); | |
1107 | PRINT_ATTRf(exclude_callchain_user, p_unsigned); | |
1108 | PRINT_ATTRf(mmap2, p_unsigned); | |
1109 | PRINT_ATTRf(comm_exec, p_unsigned); | |
1110 | PRINT_ATTRf(use_clockid, p_unsigned); | |
1111 | ||
1112 | PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned); | |
1113 | PRINT_ATTRf(bp_type, p_unsigned); | |
1114 | PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex); | |
1115 | PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex); | |
1116 | PRINT_ATTRf(sample_regs_user, p_hex); | |
1117 | PRINT_ATTRf(sample_stack_user, p_unsigned); | |
1118 | PRINT_ATTRf(clockid, p_signed); | |
1119 | PRINT_ATTRf(sample_regs_intr, p_hex); | |
70d73de4 | 1120 | PRINT_ATTRf(aux_watermark, p_unsigned); |
e3e1a54f AH |
1121 | |
1122 | return ret; | |
1123 | } | |
1124 | ||
2c5e8c52 PZ |
1125 | static int __open_attr__fprintf(FILE *fp, const char *name, const char *val, |
1126 | void *priv __attribute__((unused))) | |
1127 | { | |
1128 | return fprintf(fp, " %-32s %s\n", name, val); | |
1129 | } | |
1130 | ||
0252208e | 1131 | static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus, |
6a4bb04c | 1132 | struct thread_map *threads) |
48290609 | 1133 | { |
bf8e8f4b | 1134 | int cpu, thread, nthreads; |
57480d2c | 1135 | unsigned long flags = PERF_FLAG_FD_CLOEXEC; |
727ab04e | 1136 | int pid = -1, err; |
bec19672 | 1137 | enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE; |
48290609 | 1138 | |
bf8e8f4b AH |
1139 | if (evsel->system_wide) |
1140 | nthreads = 1; | |
1141 | else | |
1142 | nthreads = threads->nr; | |
1143 | ||
0252208e | 1144 | if (evsel->fd == NULL && |
bf8e8f4b | 1145 | perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0) |
727ab04e | 1146 | return -ENOMEM; |
4eed11d5 | 1147 | |
023695d9 | 1148 | if (evsel->cgrp) { |
57480d2c | 1149 | flags |= PERF_FLAG_PID_CGROUP; |
023695d9 SE |
1150 | pid = evsel->cgrp->fd; |
1151 | } | |
1152 | ||
594ac61a | 1153 | fallback_missing_features: |
814c8c38 PZ |
1154 | if (perf_missing_features.clockid_wrong) |
1155 | evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */ | |
1156 | if (perf_missing_features.clockid) { | |
1157 | evsel->attr.use_clockid = 0; | |
1158 | evsel->attr.clockid = 0; | |
1159 | } | |
57480d2c YD |
1160 | if (perf_missing_features.cloexec) |
1161 | flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC; | |
5c5e854b SE |
1162 | if (perf_missing_features.mmap2) |
1163 | evsel->attr.mmap2 = 0; | |
594ac61a ACM |
1164 | if (perf_missing_features.exclude_guest) |
1165 | evsel->attr.exclude_guest = evsel->attr.exclude_host = 0; | |
1166 | retry_sample_id: | |
1167 | if (perf_missing_features.sample_id_all) | |
1168 | evsel->attr.sample_id_all = 0; | |
1169 | ||
2c5e8c52 PZ |
1170 | if (verbose >= 2) { |
1171 | fprintf(stderr, "%.60s\n", graph_dotted_line); | |
1172 | fprintf(stderr, "perf_event_attr:\n"); | |
1173 | perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL); | |
1174 | fprintf(stderr, "%.60s\n", graph_dotted_line); | |
1175 | } | |
e3e1a54f | 1176 | |
86bd5e86 | 1177 | for (cpu = 0; cpu < cpus->nr; cpu++) { |
9d04f178 | 1178 | |
bf8e8f4b | 1179 | for (thread = 0; thread < nthreads; thread++) { |
6a4bb04c | 1180 | int group_fd; |
023695d9 | 1181 | |
bf8e8f4b | 1182 | if (!evsel->cgrp && !evsel->system_wide) |
e13798c7 | 1183 | pid = thread_map__pid(threads, thread); |
023695d9 | 1184 | |
6a4bb04c | 1185 | group_fd = get_group_fd(evsel, cpu, thread); |
bec19672 | 1186 | retry_open: |
a33f6efc | 1187 | pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n", |
e3e1a54f AH |
1188 | pid, cpus->map[cpu], group_fd, flags); |
1189 | ||
0252208e | 1190 | FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr, |
023695d9 | 1191 | pid, |
f08199d3 | 1192 | cpus->map[cpu], |
023695d9 | 1193 | group_fd, flags); |
727ab04e ACM |
1194 | if (FD(evsel, cpu, thread) < 0) { |
1195 | err = -errno; | |
a33f6efc | 1196 | pr_debug2("sys_perf_event_open failed, error %d\n", |
f852fd62 | 1197 | err); |
594ac61a | 1198 | goto try_fallback; |
727ab04e | 1199 | } |
bec19672 | 1200 | set_rlimit = NO_CHANGE; |
814c8c38 PZ |
1201 | |
1202 | /* | |
1203 | * If we succeeded but had to kill clockid, fail and | |
1204 | * have perf_evsel__open_strerror() print us a nice | |
1205 | * error. | |
1206 | */ | |
1207 | if (perf_missing_features.clockid || | |
1208 | perf_missing_features.clockid_wrong) { | |
1209 | err = -EINVAL; | |
1210 | goto out_close; | |
1211 | } | |
0252208e | 1212 | } |
48290609 ACM |
1213 | } |
1214 | ||
1215 | return 0; | |
1216 | ||
594ac61a | 1217 | try_fallback: |
bec19672 AK |
1218 | /* |
1219 | * perf stat needs between 5 and 22 fds per CPU. When we run out | |
1220 | * of them try to increase the limits. | |
1221 | */ | |
1222 | if (err == -EMFILE && set_rlimit < INCREASED_MAX) { | |
1223 | struct rlimit l; | |
1224 | int old_errno = errno; | |
1225 | ||
1226 | if (getrlimit(RLIMIT_NOFILE, &l) == 0) { | |
1227 | if (set_rlimit == NO_CHANGE) | |
1228 | l.rlim_cur = l.rlim_max; | |
1229 | else { | |
1230 | l.rlim_cur = l.rlim_max + 1000; | |
1231 | l.rlim_max = l.rlim_cur; | |
1232 | } | |
1233 | if (setrlimit(RLIMIT_NOFILE, &l) == 0) { | |
1234 | set_rlimit++; | |
1235 | errno = old_errno; | |
1236 | goto retry_open; | |
1237 | } | |
1238 | } | |
1239 | errno = old_errno; | |
1240 | } | |
1241 | ||
594ac61a ACM |
1242 | if (err != -EINVAL || cpu > 0 || thread > 0) |
1243 | goto out_close; | |
1244 | ||
814c8c38 PZ |
1245 | /* |
1246 | * Must probe features in the order they were added to the | |
1247 | * perf_event_attr interface. | |
1248 | */ | |
1249 | if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) { | |
1250 | perf_missing_features.clockid_wrong = true; | |
1251 | goto fallback_missing_features; | |
1252 | } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) { | |
1253 | perf_missing_features.clockid = true; | |
1254 | goto fallback_missing_features; | |
1255 | } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) { | |
57480d2c YD |
1256 | perf_missing_features.cloexec = true; |
1257 | goto fallback_missing_features; | |
1258 | } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) { | |
5c5e854b SE |
1259 | perf_missing_features.mmap2 = true; |
1260 | goto fallback_missing_features; | |
1261 | } else if (!perf_missing_features.exclude_guest && | |
1262 | (evsel->attr.exclude_guest || evsel->attr.exclude_host)) { | |
594ac61a ACM |
1263 | perf_missing_features.exclude_guest = true; |
1264 | goto fallback_missing_features; | |
1265 | } else if (!perf_missing_features.sample_id_all) { | |
1266 | perf_missing_features.sample_id_all = true; | |
1267 | goto retry_sample_id; | |
1268 | } | |
1269 | ||
48290609 | 1270 | out_close: |
0252208e ACM |
1271 | do { |
1272 | while (--thread >= 0) { | |
1273 | close(FD(evsel, cpu, thread)); | |
1274 | FD(evsel, cpu, thread) = -1; | |
1275 | } | |
bf8e8f4b | 1276 | thread = nthreads; |
0252208e | 1277 | } while (--cpu >= 0); |
727ab04e ACM |
1278 | return err; |
1279 | } | |
1280 | ||
1281 | void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads) | |
1282 | { | |
1283 | if (evsel->fd == NULL) | |
1284 | return; | |
1285 | ||
1286 | perf_evsel__close_fd(evsel, ncpus, nthreads); | |
1287 | perf_evsel__free_fd(evsel); | |
48290609 ACM |
1288 | } |
1289 | ||
0252208e ACM |
1290 | static struct { |
1291 | struct cpu_map map; | |
1292 | int cpus[1]; | |
1293 | } empty_cpu_map = { | |
1294 | .map.nr = 1, | |
1295 | .cpus = { -1, }, | |
1296 | }; | |
1297 | ||
1298 | static struct { | |
1299 | struct thread_map map; | |
1300 | int threads[1]; | |
1301 | } empty_thread_map = { | |
1302 | .map.nr = 1, | |
1303 | .threads = { -1, }, | |
1304 | }; | |
1305 | ||
f08199d3 | 1306 | int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus, |
6a4bb04c | 1307 | struct thread_map *threads) |
48290609 | 1308 | { |
0252208e ACM |
1309 | if (cpus == NULL) { |
1310 | /* Work around old compiler warnings about strict aliasing */ | |
1311 | cpus = &empty_cpu_map.map; | |
48290609 ACM |
1312 | } |
1313 | ||
0252208e ACM |
1314 | if (threads == NULL) |
1315 | threads = &empty_thread_map.map; | |
48290609 | 1316 | |
6a4bb04c | 1317 | return __perf_evsel__open(evsel, cpus, threads); |
48290609 ACM |
1318 | } |
1319 | ||
f08199d3 | 1320 | int perf_evsel__open_per_cpu(struct perf_evsel *evsel, |
6a4bb04c | 1321 | struct cpu_map *cpus) |
48290609 | 1322 | { |
6a4bb04c | 1323 | return __perf_evsel__open(evsel, cpus, &empty_thread_map.map); |
0252208e | 1324 | } |
48290609 | 1325 | |
f08199d3 | 1326 | int perf_evsel__open_per_thread(struct perf_evsel *evsel, |
6a4bb04c | 1327 | struct thread_map *threads) |
0252208e | 1328 | { |
6a4bb04c | 1329 | return __perf_evsel__open(evsel, &empty_cpu_map.map, threads); |
48290609 | 1330 | } |
70082dd9 | 1331 | |
0807d2d8 ACM |
1332 | static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel, |
1333 | const union perf_event *event, | |
1334 | struct perf_sample *sample) | |
d0dd74e8 | 1335 | { |
0807d2d8 | 1336 | u64 type = evsel->attr.sample_type; |
d0dd74e8 | 1337 | const u64 *array = event->sample.array; |
0807d2d8 | 1338 | bool swapped = evsel->needs_swap; |
37073f9e | 1339 | union u64_swap u; |
d0dd74e8 ACM |
1340 | |
1341 | array += ((event->header.size - | |
1342 | sizeof(event->header)) / sizeof(u64)) - 1; | |
1343 | ||
75562573 AH |
1344 | if (type & PERF_SAMPLE_IDENTIFIER) { |
1345 | sample->id = *array; | |
1346 | array--; | |
1347 | } | |
1348 | ||
d0dd74e8 | 1349 | if (type & PERF_SAMPLE_CPU) { |
37073f9e JO |
1350 | u.val64 = *array; |
1351 | if (swapped) { | |
1352 | /* undo swap of u64, then swap on individual u32s */ | |
1353 | u.val64 = bswap_64(u.val64); | |
1354 | u.val32[0] = bswap_32(u.val32[0]); | |
1355 | } | |
1356 | ||
1357 | sample->cpu = u.val32[0]; | |
d0dd74e8 ACM |
1358 | array--; |
1359 | } | |
1360 | ||
1361 | if (type & PERF_SAMPLE_STREAM_ID) { | |
1362 | sample->stream_id = *array; | |
1363 | array--; | |
1364 | } | |
1365 | ||
1366 | if (type & PERF_SAMPLE_ID) { | |
1367 | sample->id = *array; | |
1368 | array--; | |
1369 | } | |
1370 | ||
1371 | if (type & PERF_SAMPLE_TIME) { | |
1372 | sample->time = *array; | |
1373 | array--; | |
1374 | } | |
1375 | ||
1376 | if (type & PERF_SAMPLE_TID) { | |
37073f9e JO |
1377 | u.val64 = *array; |
1378 | if (swapped) { | |
1379 | /* undo swap of u64, then swap on individual u32s */ | |
1380 | u.val64 = bswap_64(u.val64); | |
1381 | u.val32[0] = bswap_32(u.val32[0]); | |
1382 | u.val32[1] = bswap_32(u.val32[1]); | |
1383 | } | |
1384 | ||
1385 | sample->pid = u.val32[0]; | |
1386 | sample->tid = u.val32[1]; | |
dd44bc6b | 1387 | array--; |
d0dd74e8 ACM |
1388 | } |
1389 | ||
1390 | return 0; | |
1391 | } | |
1392 | ||
03b6ea9b AH |
1393 | static inline bool overflow(const void *endp, u16 max_size, const void *offset, |
1394 | u64 size) | |
98e1da90 | 1395 | { |
03b6ea9b AH |
1396 | return size > max_size || offset + size > endp; |
1397 | } | |
98e1da90 | 1398 | |
03b6ea9b AH |
1399 | #define OVERFLOW_CHECK(offset, size, max_size) \ |
1400 | do { \ | |
1401 | if (overflow(endp, (max_size), (offset), (size))) \ | |
1402 | return -EFAULT; \ | |
1403 | } while (0) | |
98e1da90 | 1404 | |
03b6ea9b AH |
1405 | #define OVERFLOW_CHECK_u64(offset) \ |
1406 | OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64)) | |
98e1da90 | 1407 | |
a3f698fe | 1408 | int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event, |
0807d2d8 | 1409 | struct perf_sample *data) |
d0dd74e8 | 1410 | { |
a3f698fe | 1411 | u64 type = evsel->attr.sample_type; |
0807d2d8 | 1412 | bool swapped = evsel->needs_swap; |
d0dd74e8 | 1413 | const u64 *array; |
03b6ea9b AH |
1414 | u16 max_size = event->header.size; |
1415 | const void *endp = (void *)event + max_size; | |
1416 | u64 sz; | |
d0dd74e8 | 1417 | |
936be503 DA |
1418 | /* |
1419 | * used for cross-endian analysis. See git commit 65014ab3 | |
1420 | * for why this goofiness is needed. | |
1421 | */ | |
6a11f92e | 1422 | union u64_swap u; |
936be503 | 1423 | |
f3bda2c9 | 1424 | memset(data, 0, sizeof(*data)); |
d0dd74e8 ACM |
1425 | data->cpu = data->pid = data->tid = -1; |
1426 | data->stream_id = data->id = data->time = -1ULL; | |
bc529086 | 1427 | data->period = evsel->attr.sample_period; |
05484298 | 1428 | data->weight = 0; |
d0dd74e8 ACM |
1429 | |
1430 | if (event->header.type != PERF_RECORD_SAMPLE) { | |
a3f698fe | 1431 | if (!evsel->attr.sample_id_all) |
d0dd74e8 | 1432 | return 0; |
0807d2d8 | 1433 | return perf_evsel__parse_id_sample(evsel, event, data); |
d0dd74e8 ACM |
1434 | } |
1435 | ||
1436 | array = event->sample.array; | |
1437 | ||
03b6ea9b AH |
1438 | /* |
1439 | * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes | |
1440 | * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to | |
1441 | * check the format does not go past the end of the event. | |
1442 | */ | |
a3f698fe | 1443 | if (evsel->sample_size + sizeof(event->header) > event->header.size) |
a2854124 FW |
1444 | return -EFAULT; |
1445 | ||
75562573 AH |
1446 | data->id = -1ULL; |
1447 | if (type & PERF_SAMPLE_IDENTIFIER) { | |
1448 | data->id = *array; | |
1449 | array++; | |
1450 | } | |
1451 | ||
d0dd74e8 | 1452 | if (type & PERF_SAMPLE_IP) { |
ef89325f | 1453 | data->ip = *array; |
d0dd74e8 ACM |
1454 | array++; |
1455 | } | |
1456 | ||
1457 | if (type & PERF_SAMPLE_TID) { | |
936be503 DA |
1458 | u.val64 = *array; |
1459 | if (swapped) { | |
1460 | /* undo swap of u64, then swap on individual u32s */ | |
1461 | u.val64 = bswap_64(u.val64); | |
1462 | u.val32[0] = bswap_32(u.val32[0]); | |
1463 | u.val32[1] = bswap_32(u.val32[1]); | |
1464 | } | |
1465 | ||
1466 | data->pid = u.val32[0]; | |
1467 | data->tid = u.val32[1]; | |
d0dd74e8 ACM |
1468 | array++; |
1469 | } | |
1470 | ||
1471 | if (type & PERF_SAMPLE_TIME) { | |
1472 | data->time = *array; | |
1473 | array++; | |
1474 | } | |
1475 | ||
7cec0922 | 1476 | data->addr = 0; |
d0dd74e8 ACM |
1477 | if (type & PERF_SAMPLE_ADDR) { |
1478 | data->addr = *array; | |
1479 | array++; | |
1480 | } | |
1481 | ||
d0dd74e8 ACM |
1482 | if (type & PERF_SAMPLE_ID) { |
1483 | data->id = *array; | |
1484 | array++; | |
1485 | } | |
1486 | ||
1487 | if (type & PERF_SAMPLE_STREAM_ID) { | |
1488 | data->stream_id = *array; | |
1489 | array++; | |
1490 | } | |
1491 | ||
1492 | if (type & PERF_SAMPLE_CPU) { | |
936be503 DA |
1493 | |
1494 | u.val64 = *array; | |
1495 | if (swapped) { | |
1496 | /* undo swap of u64, then swap on individual u32s */ | |
1497 | u.val64 = bswap_64(u.val64); | |
1498 | u.val32[0] = bswap_32(u.val32[0]); | |
1499 | } | |
1500 | ||
1501 | data->cpu = u.val32[0]; | |
d0dd74e8 ACM |
1502 | array++; |
1503 | } | |
1504 | ||
1505 | if (type & PERF_SAMPLE_PERIOD) { | |
1506 | data->period = *array; | |
1507 | array++; | |
1508 | } | |
1509 | ||
1510 | if (type & PERF_SAMPLE_READ) { | |
9ede473c JO |
1511 | u64 read_format = evsel->attr.read_format; |
1512 | ||
03b6ea9b | 1513 | OVERFLOW_CHECK_u64(array); |
9ede473c JO |
1514 | if (read_format & PERF_FORMAT_GROUP) |
1515 | data->read.group.nr = *array; | |
1516 | else | |
1517 | data->read.one.value = *array; | |
1518 | ||
1519 | array++; | |
1520 | ||
1521 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { | |
03b6ea9b | 1522 | OVERFLOW_CHECK_u64(array); |
9ede473c JO |
1523 | data->read.time_enabled = *array; |
1524 | array++; | |
1525 | } | |
1526 | ||
1527 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { | |
03b6ea9b | 1528 | OVERFLOW_CHECK_u64(array); |
9ede473c JO |
1529 | data->read.time_running = *array; |
1530 | array++; | |
1531 | } | |
1532 | ||
1533 | /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ | |
1534 | if (read_format & PERF_FORMAT_GROUP) { | |
03b6ea9b AH |
1535 | const u64 max_group_nr = UINT64_MAX / |
1536 | sizeof(struct sample_read_value); | |
1537 | ||
1538 | if (data->read.group.nr > max_group_nr) | |
1539 | return -EFAULT; | |
1540 | sz = data->read.group.nr * | |
1541 | sizeof(struct sample_read_value); | |
1542 | OVERFLOW_CHECK(array, sz, max_size); | |
1543 | data->read.group.values = | |
1544 | (struct sample_read_value *)array; | |
1545 | array = (void *)array + sz; | |
9ede473c | 1546 | } else { |
03b6ea9b | 1547 | OVERFLOW_CHECK_u64(array); |
9ede473c JO |
1548 | data->read.one.id = *array; |
1549 | array++; | |
1550 | } | |
d0dd74e8 ACM |
1551 | } |
1552 | ||
1553 | if (type & PERF_SAMPLE_CALLCHAIN) { | |
03b6ea9b | 1554 | const u64 max_callchain_nr = UINT64_MAX / sizeof(u64); |
98e1da90 | 1555 | |
03b6ea9b AH |
1556 | OVERFLOW_CHECK_u64(array); |
1557 | data->callchain = (struct ip_callchain *)array++; | |
1558 | if (data->callchain->nr > max_callchain_nr) | |
98e1da90 | 1559 | return -EFAULT; |
03b6ea9b AH |
1560 | sz = data->callchain->nr * sizeof(u64); |
1561 | OVERFLOW_CHECK(array, sz, max_size); | |
1562 | array = (void *)array + sz; | |
d0dd74e8 ACM |
1563 | } |
1564 | ||
1565 | if (type & PERF_SAMPLE_RAW) { | |
03b6ea9b | 1566 | OVERFLOW_CHECK_u64(array); |
936be503 DA |
1567 | u.val64 = *array; |
1568 | if (WARN_ONCE(swapped, | |
1569 | "Endianness of raw data not corrected!\n")) { | |
1570 | /* undo swap of u64, then swap on individual u32s */ | |
1571 | u.val64 = bswap_64(u.val64); | |
1572 | u.val32[0] = bswap_32(u.val32[0]); | |
1573 | u.val32[1] = bswap_32(u.val32[1]); | |
1574 | } | |
936be503 | 1575 | data->raw_size = u.val32[0]; |
03b6ea9b | 1576 | array = (void *)array + sizeof(u32); |
98e1da90 | 1577 | |
03b6ea9b AH |
1578 | OVERFLOW_CHECK(array, data->raw_size, max_size); |
1579 | data->raw_data = (void *)array; | |
1580 | array = (void *)array + data->raw_size; | |
d0dd74e8 ACM |
1581 | } |
1582 | ||
b5387528 | 1583 | if (type & PERF_SAMPLE_BRANCH_STACK) { |
03b6ea9b AH |
1584 | const u64 max_branch_nr = UINT64_MAX / |
1585 | sizeof(struct branch_entry); | |
b5387528 | 1586 | |
03b6ea9b AH |
1587 | OVERFLOW_CHECK_u64(array); |
1588 | data->branch_stack = (struct branch_stack *)array++; | |
b5387528 | 1589 | |
03b6ea9b AH |
1590 | if (data->branch_stack->nr > max_branch_nr) |
1591 | return -EFAULT; | |
b5387528 | 1592 | sz = data->branch_stack->nr * sizeof(struct branch_entry); |
03b6ea9b AH |
1593 | OVERFLOW_CHECK(array, sz, max_size); |
1594 | array = (void *)array + sz; | |
b5387528 | 1595 | } |
0f6a3015 JO |
1596 | |
1597 | if (type & PERF_SAMPLE_REGS_USER) { | |
03b6ea9b | 1598 | OVERFLOW_CHECK_u64(array); |
5b95a4a3 AH |
1599 | data->user_regs.abi = *array; |
1600 | array++; | |
0f6a3015 | 1601 | |
5b95a4a3 | 1602 | if (data->user_regs.abi) { |
352ea45a | 1603 | u64 mask = evsel->attr.sample_regs_user; |
03b6ea9b | 1604 | |
352ea45a | 1605 | sz = hweight_long(mask) * sizeof(u64); |
03b6ea9b | 1606 | OVERFLOW_CHECK(array, sz, max_size); |
352ea45a | 1607 | data->user_regs.mask = mask; |
0f6a3015 | 1608 | data->user_regs.regs = (u64 *)array; |
03b6ea9b | 1609 | array = (void *)array + sz; |
0f6a3015 JO |
1610 | } |
1611 | } | |
1612 | ||
1613 | if (type & PERF_SAMPLE_STACK_USER) { | |
03b6ea9b AH |
1614 | OVERFLOW_CHECK_u64(array); |
1615 | sz = *array++; | |
0f6a3015 JO |
1616 | |
1617 | data->user_stack.offset = ((char *)(array - 1) | |
1618 | - (char *) event); | |
1619 | ||
03b6ea9b | 1620 | if (!sz) { |
0f6a3015 JO |
1621 | data->user_stack.size = 0; |
1622 | } else { | |
03b6ea9b | 1623 | OVERFLOW_CHECK(array, sz, max_size); |
0f6a3015 | 1624 | data->user_stack.data = (char *)array; |
03b6ea9b AH |
1625 | array = (void *)array + sz; |
1626 | OVERFLOW_CHECK_u64(array); | |
54bd2692 | 1627 | data->user_stack.size = *array++; |
a65cb4b9 JO |
1628 | if (WARN_ONCE(data->user_stack.size > sz, |
1629 | "user stack dump failure\n")) | |
1630 | return -EFAULT; | |
0f6a3015 JO |
1631 | } |
1632 | } | |
1633 | ||
05484298 AK |
1634 | data->weight = 0; |
1635 | if (type & PERF_SAMPLE_WEIGHT) { | |
03b6ea9b | 1636 | OVERFLOW_CHECK_u64(array); |
05484298 AK |
1637 | data->weight = *array; |
1638 | array++; | |
1639 | } | |
1640 | ||
98a3b32c SE |
1641 | data->data_src = PERF_MEM_DATA_SRC_NONE; |
1642 | if (type & PERF_SAMPLE_DATA_SRC) { | |
03b6ea9b | 1643 | OVERFLOW_CHECK_u64(array); |
98a3b32c SE |
1644 | data->data_src = *array; |
1645 | array++; | |
1646 | } | |
1647 | ||
475eeab9 AK |
1648 | data->transaction = 0; |
1649 | if (type & PERF_SAMPLE_TRANSACTION) { | |
87b95524 | 1650 | OVERFLOW_CHECK_u64(array); |
475eeab9 AK |
1651 | data->transaction = *array; |
1652 | array++; | |
1653 | } | |
1654 | ||
6a21c0b5 SE |
1655 | data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE; |
1656 | if (type & PERF_SAMPLE_REGS_INTR) { | |
1657 | OVERFLOW_CHECK_u64(array); | |
1658 | data->intr_regs.abi = *array; | |
1659 | array++; | |
1660 | ||
1661 | if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) { | |
1662 | u64 mask = evsel->attr.sample_regs_intr; | |
1663 | ||
1664 | sz = hweight_long(mask) * sizeof(u64); | |
1665 | OVERFLOW_CHECK(array, sz, max_size); | |
1666 | data->intr_regs.mask = mask; | |
1667 | data->intr_regs.regs = (u64 *)array; | |
1668 | array = (void *)array + sz; | |
1669 | } | |
1670 | } | |
1671 | ||
d0dd74e8 ACM |
1672 | return 0; |
1673 | } | |
74eec26f | 1674 | |
b1cf6f65 | 1675 | size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type, |
352ea45a | 1676 | u64 read_format) |
b1cf6f65 AH |
1677 | { |
1678 | size_t sz, result = sizeof(struct sample_event); | |
1679 | ||
1680 | if (type & PERF_SAMPLE_IDENTIFIER) | |
1681 | result += sizeof(u64); | |
1682 | ||
1683 | if (type & PERF_SAMPLE_IP) | |
1684 | result += sizeof(u64); | |
1685 | ||
1686 | if (type & PERF_SAMPLE_TID) | |
1687 | result += sizeof(u64); | |
1688 | ||
1689 | if (type & PERF_SAMPLE_TIME) | |
1690 | result += sizeof(u64); | |
1691 | ||
1692 | if (type & PERF_SAMPLE_ADDR) | |
1693 | result += sizeof(u64); | |
1694 | ||
1695 | if (type & PERF_SAMPLE_ID) | |
1696 | result += sizeof(u64); | |
1697 | ||
1698 | if (type & PERF_SAMPLE_STREAM_ID) | |
1699 | result += sizeof(u64); | |
1700 | ||
1701 | if (type & PERF_SAMPLE_CPU) | |
1702 | result += sizeof(u64); | |
1703 | ||
1704 | if (type & PERF_SAMPLE_PERIOD) | |
1705 | result += sizeof(u64); | |
1706 | ||
1707 | if (type & PERF_SAMPLE_READ) { | |
1708 | result += sizeof(u64); | |
1709 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
1710 | result += sizeof(u64); | |
1711 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
1712 | result += sizeof(u64); | |
1713 | /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ | |
1714 | if (read_format & PERF_FORMAT_GROUP) { | |
1715 | sz = sample->read.group.nr * | |
1716 | sizeof(struct sample_read_value); | |
1717 | result += sz; | |
1718 | } else { | |
1719 | result += sizeof(u64); | |
1720 | } | |
1721 | } | |
1722 | ||
1723 | if (type & PERF_SAMPLE_CALLCHAIN) { | |
1724 | sz = (sample->callchain->nr + 1) * sizeof(u64); | |
1725 | result += sz; | |
1726 | } | |
1727 | ||
1728 | if (type & PERF_SAMPLE_RAW) { | |
1729 | result += sizeof(u32); | |
1730 | result += sample->raw_size; | |
1731 | } | |
1732 | ||
1733 | if (type & PERF_SAMPLE_BRANCH_STACK) { | |
1734 | sz = sample->branch_stack->nr * sizeof(struct branch_entry); | |
1735 | sz += sizeof(u64); | |
1736 | result += sz; | |
1737 | } | |
1738 | ||
1739 | if (type & PERF_SAMPLE_REGS_USER) { | |
1740 | if (sample->user_regs.abi) { | |
1741 | result += sizeof(u64); | |
352ea45a | 1742 | sz = hweight_long(sample->user_regs.mask) * sizeof(u64); |
b1cf6f65 AH |
1743 | result += sz; |
1744 | } else { | |
1745 | result += sizeof(u64); | |
1746 | } | |
1747 | } | |
1748 | ||
1749 | if (type & PERF_SAMPLE_STACK_USER) { | |
1750 | sz = sample->user_stack.size; | |
1751 | result += sizeof(u64); | |
1752 | if (sz) { | |
1753 | result += sz; | |
1754 | result += sizeof(u64); | |
1755 | } | |
1756 | } | |
1757 | ||
1758 | if (type & PERF_SAMPLE_WEIGHT) | |
1759 | result += sizeof(u64); | |
1760 | ||
1761 | if (type & PERF_SAMPLE_DATA_SRC) | |
1762 | result += sizeof(u64); | |
1763 | ||
42d88910 AH |
1764 | if (type & PERF_SAMPLE_TRANSACTION) |
1765 | result += sizeof(u64); | |
1766 | ||
6a21c0b5 SE |
1767 | if (type & PERF_SAMPLE_REGS_INTR) { |
1768 | if (sample->intr_regs.abi) { | |
1769 | result += sizeof(u64); | |
1770 | sz = hweight_long(sample->intr_regs.mask) * sizeof(u64); | |
1771 | result += sz; | |
1772 | } else { | |
1773 | result += sizeof(u64); | |
1774 | } | |
1775 | } | |
1776 | ||
b1cf6f65 AH |
1777 | return result; |
1778 | } | |
1779 | ||
74eec26f | 1780 | int perf_event__synthesize_sample(union perf_event *event, u64 type, |
352ea45a | 1781 | u64 read_format, |
74eec26f AV |
1782 | const struct perf_sample *sample, |
1783 | bool swapped) | |
1784 | { | |
1785 | u64 *array; | |
d03f2170 | 1786 | size_t sz; |
74eec26f AV |
1787 | /* |
1788 | * used for cross-endian analysis. See git commit 65014ab3 | |
1789 | * for why this goofiness is needed. | |
1790 | */ | |
6a11f92e | 1791 | union u64_swap u; |
74eec26f AV |
1792 | |
1793 | array = event->sample.array; | |
1794 | ||
75562573 AH |
1795 | if (type & PERF_SAMPLE_IDENTIFIER) { |
1796 | *array = sample->id; | |
1797 | array++; | |
1798 | } | |
1799 | ||
74eec26f | 1800 | if (type & PERF_SAMPLE_IP) { |
ef89325f | 1801 | *array = sample->ip; |
74eec26f AV |
1802 | array++; |
1803 | } | |
1804 | ||
1805 | if (type & PERF_SAMPLE_TID) { | |
1806 | u.val32[0] = sample->pid; | |
1807 | u.val32[1] = sample->tid; | |
1808 | if (swapped) { | |
1809 | /* | |
a3f698fe | 1810 | * Inverse of what is done in perf_evsel__parse_sample |
74eec26f AV |
1811 | */ |
1812 | u.val32[0] = bswap_32(u.val32[0]); | |
1813 | u.val32[1] = bswap_32(u.val32[1]); | |
1814 | u.val64 = bswap_64(u.val64); | |
1815 | } | |
1816 | ||
1817 | *array = u.val64; | |
1818 | array++; | |
1819 | } | |
1820 | ||
1821 | if (type & PERF_SAMPLE_TIME) { | |
1822 | *array = sample->time; | |
1823 | array++; | |
1824 | } | |
1825 | ||
1826 | if (type & PERF_SAMPLE_ADDR) { | |
1827 | *array = sample->addr; | |
1828 | array++; | |
1829 | } | |
1830 | ||
1831 | if (type & PERF_SAMPLE_ID) { | |
1832 | *array = sample->id; | |
1833 | array++; | |
1834 | } | |
1835 | ||
1836 | if (type & PERF_SAMPLE_STREAM_ID) { | |
1837 | *array = sample->stream_id; | |
1838 | array++; | |
1839 | } | |
1840 | ||
1841 | if (type & PERF_SAMPLE_CPU) { | |
1842 | u.val32[0] = sample->cpu; | |
1843 | if (swapped) { | |
1844 | /* | |
a3f698fe | 1845 | * Inverse of what is done in perf_evsel__parse_sample |
74eec26f AV |
1846 | */ |
1847 | u.val32[0] = bswap_32(u.val32[0]); | |
1848 | u.val64 = bswap_64(u.val64); | |
1849 | } | |
1850 | *array = u.val64; | |
1851 | array++; | |
1852 | } | |
1853 | ||
1854 | if (type & PERF_SAMPLE_PERIOD) { | |
1855 | *array = sample->period; | |
1856 | array++; | |
1857 | } | |
1858 | ||
d03f2170 AH |
1859 | if (type & PERF_SAMPLE_READ) { |
1860 | if (read_format & PERF_FORMAT_GROUP) | |
1861 | *array = sample->read.group.nr; | |
1862 | else | |
1863 | *array = sample->read.one.value; | |
1864 | array++; | |
1865 | ||
1866 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { | |
1867 | *array = sample->read.time_enabled; | |
1868 | array++; | |
1869 | } | |
1870 | ||
1871 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { | |
1872 | *array = sample->read.time_running; | |
1873 | array++; | |
1874 | } | |
1875 | ||
1876 | /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ | |
1877 | if (read_format & PERF_FORMAT_GROUP) { | |
1878 | sz = sample->read.group.nr * | |
1879 | sizeof(struct sample_read_value); | |
1880 | memcpy(array, sample->read.group.values, sz); | |
1881 | array = (void *)array + sz; | |
1882 | } else { | |
1883 | *array = sample->read.one.id; | |
1884 | array++; | |
1885 | } | |
1886 | } | |
1887 | ||
1888 | if (type & PERF_SAMPLE_CALLCHAIN) { | |
1889 | sz = (sample->callchain->nr + 1) * sizeof(u64); | |
1890 | memcpy(array, sample->callchain, sz); | |
1891 | array = (void *)array + sz; | |
1892 | } | |
1893 | ||
1894 | if (type & PERF_SAMPLE_RAW) { | |
1895 | u.val32[0] = sample->raw_size; | |
1896 | if (WARN_ONCE(swapped, | |
1897 | "Endianness of raw data not corrected!\n")) { | |
1898 | /* | |
1899 | * Inverse of what is done in perf_evsel__parse_sample | |
1900 | */ | |
1901 | u.val32[0] = bswap_32(u.val32[0]); | |
1902 | u.val32[1] = bswap_32(u.val32[1]); | |
1903 | u.val64 = bswap_64(u.val64); | |
1904 | } | |
1905 | *array = u.val64; | |
1906 | array = (void *)array + sizeof(u32); | |
1907 | ||
1908 | memcpy(array, sample->raw_data, sample->raw_size); | |
1909 | array = (void *)array + sample->raw_size; | |
1910 | } | |
1911 | ||
1912 | if (type & PERF_SAMPLE_BRANCH_STACK) { | |
1913 | sz = sample->branch_stack->nr * sizeof(struct branch_entry); | |
1914 | sz += sizeof(u64); | |
1915 | memcpy(array, sample->branch_stack, sz); | |
1916 | array = (void *)array + sz; | |
1917 | } | |
1918 | ||
1919 | if (type & PERF_SAMPLE_REGS_USER) { | |
1920 | if (sample->user_regs.abi) { | |
1921 | *array++ = sample->user_regs.abi; | |
352ea45a | 1922 | sz = hweight_long(sample->user_regs.mask) * sizeof(u64); |
d03f2170 AH |
1923 | memcpy(array, sample->user_regs.regs, sz); |
1924 | array = (void *)array + sz; | |
1925 | } else { | |
1926 | *array++ = 0; | |
1927 | } | |
1928 | } | |
1929 | ||
1930 | if (type & PERF_SAMPLE_STACK_USER) { | |
1931 | sz = sample->user_stack.size; | |
1932 | *array++ = sz; | |
1933 | if (sz) { | |
1934 | memcpy(array, sample->user_stack.data, sz); | |
1935 | array = (void *)array + sz; | |
1936 | *array++ = sz; | |
1937 | } | |
1938 | } | |
1939 | ||
1940 | if (type & PERF_SAMPLE_WEIGHT) { | |
1941 | *array = sample->weight; | |
1942 | array++; | |
1943 | } | |
1944 | ||
1945 | if (type & PERF_SAMPLE_DATA_SRC) { | |
1946 | *array = sample->data_src; | |
1947 | array++; | |
1948 | } | |
1949 | ||
42d88910 AH |
1950 | if (type & PERF_SAMPLE_TRANSACTION) { |
1951 | *array = sample->transaction; | |
1952 | array++; | |
1953 | } | |
1954 | ||
6a21c0b5 SE |
1955 | if (type & PERF_SAMPLE_REGS_INTR) { |
1956 | if (sample->intr_regs.abi) { | |
1957 | *array++ = sample->intr_regs.abi; | |
1958 | sz = hweight_long(sample->intr_regs.mask) * sizeof(u64); | |
1959 | memcpy(array, sample->intr_regs.regs, sz); | |
1960 | array = (void *)array + sz; | |
1961 | } else { | |
1962 | *array++ = 0; | |
1963 | } | |
1964 | } | |
1965 | ||
74eec26f AV |
1966 | return 0; |
1967 | } | |
5555ded4 | 1968 | |
efd2b924 ACM |
1969 | struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name) |
1970 | { | |
1971 | return pevent_find_field(evsel->tp_format, name); | |
1972 | } | |
1973 | ||
5d2074ea | 1974 | void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample, |
5555ded4 ACM |
1975 | const char *name) |
1976 | { | |
efd2b924 | 1977 | struct format_field *field = perf_evsel__field(evsel, name); |
5555ded4 ACM |
1978 | int offset; |
1979 | ||
efd2b924 ACM |
1980 | if (!field) |
1981 | return NULL; | |
5555ded4 ACM |
1982 | |
1983 | offset = field->offset; | |
1984 | ||
1985 | if (field->flags & FIELD_IS_DYNAMIC) { | |
1986 | offset = *(int *)(sample->raw_data + field->offset); | |
1987 | offset &= 0xffff; | |
1988 | } | |
1989 | ||
1990 | return sample->raw_data + offset; | |
1991 | } | |
1992 | ||
1993 | u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample, | |
1994 | const char *name) | |
1995 | { | |
efd2b924 | 1996 | struct format_field *field = perf_evsel__field(evsel, name); |
e6b6f679 ACM |
1997 | void *ptr; |
1998 | u64 value; | |
5555ded4 | 1999 | |
efd2b924 ACM |
2000 | if (!field) |
2001 | return 0; | |
5555ded4 | 2002 | |
e6b6f679 | 2003 | ptr = sample->raw_data + field->offset; |
5555ded4 | 2004 | |
e6b6f679 ACM |
2005 | switch (field->size) { |
2006 | case 1: | |
2007 | return *(u8 *)ptr; | |
2008 | case 2: | |
2009 | value = *(u16 *)ptr; | |
2010 | break; | |
2011 | case 4: | |
2012 | value = *(u32 *)ptr; | |
2013 | break; | |
2014 | case 8: | |
e94eedab | 2015 | memcpy(&value, ptr, sizeof(u64)); |
e6b6f679 ACM |
2016 | break; |
2017 | default: | |
2018 | return 0; | |
2019 | } | |
2020 | ||
2021 | if (!evsel->needs_swap) | |
2022 | return value; | |
2023 | ||
2024 | switch (field->size) { | |
2025 | case 2: | |
2026 | return bswap_16(value); | |
2027 | case 4: | |
2028 | return bswap_32(value); | |
2029 | case 8: | |
2030 | return bswap_64(value); | |
2031 | default: | |
2032 | return 0; | |
2033 | } | |
2034 | ||
2035 | return 0; | |
5555ded4 | 2036 | } |
0698aedd ACM |
2037 | |
2038 | static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...) | |
2039 | { | |
2040 | va_list args; | |
2041 | int ret = 0; | |
2042 | ||
2043 | if (!*first) { | |
2044 | ret += fprintf(fp, ","); | |
2045 | } else { | |
2046 | ret += fprintf(fp, ":"); | |
2047 | *first = false; | |
2048 | } | |
2049 | ||
2050 | va_start(args, fmt); | |
2051 | ret += vfprintf(fp, fmt, args); | |
2052 | va_end(args); | |
2053 | return ret; | |
2054 | } | |
2055 | ||
2c5e8c52 | 2056 | static int __print_attr__fprintf(FILE *fp, const char *name, const char *val, void *priv) |
c79a4393 | 2057 | { |
2c5e8c52 | 2058 | return comma_fprintf(fp, (bool *)priv, " %s: %s", name, val); |
c79a4393 ACM |
2059 | } |
2060 | ||
0698aedd ACM |
2061 | int perf_evsel__fprintf(struct perf_evsel *evsel, |
2062 | struct perf_attr_details *details, FILE *fp) | |
2063 | { | |
2064 | bool first = true; | |
e6ab07d0 NK |
2065 | int printed = 0; |
2066 | ||
e35ef355 | 2067 | if (details->event_group) { |
e6ab07d0 NK |
2068 | struct perf_evsel *pos; |
2069 | ||
2070 | if (!perf_evsel__is_group_leader(evsel)) | |
2071 | return 0; | |
2072 | ||
2073 | if (evsel->nr_members > 1) | |
2074 | printed += fprintf(fp, "%s{", evsel->group_name ?: ""); | |
2075 | ||
2076 | printed += fprintf(fp, "%s", perf_evsel__name(evsel)); | |
2077 | for_each_group_member(pos, evsel) | |
2078 | printed += fprintf(fp, ",%s", perf_evsel__name(pos)); | |
2079 | ||
2080 | if (evsel->nr_members > 1) | |
2081 | printed += fprintf(fp, "}"); | |
2082 | goto out; | |
2083 | } | |
2084 | ||
2085 | printed += fprintf(fp, "%s", perf_evsel__name(evsel)); | |
0698aedd | 2086 | |
2c5e8c52 PZ |
2087 | if (details->verbose) { |
2088 | printed += perf_event_attr__fprintf(fp, &evsel->attr, | |
2089 | __print_attr__fprintf, &first); | |
2090 | } else if (details->freq) { | |
0698aedd ACM |
2091 | printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64, |
2092 | (u64)evsel->attr.sample_freq); | |
2093 | } | |
e6ab07d0 | 2094 | out: |
0698aedd ACM |
2095 | fputc('\n', fp); |
2096 | return ++printed; | |
2097 | } | |
c0a54341 ACM |
2098 | |
2099 | bool perf_evsel__fallback(struct perf_evsel *evsel, int err, | |
2100 | char *msg, size_t msgsize) | |
2101 | { | |
2b821cce | 2102 | if ((err == ENOENT || err == ENXIO || err == ENODEV) && |
c0a54341 ACM |
2103 | evsel->attr.type == PERF_TYPE_HARDWARE && |
2104 | evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) { | |
2105 | /* | |
2106 | * If it's cycles then fall back to hrtimer based | |
2107 | * cpu-clock-tick sw counter, which is always available even if | |
2108 | * no PMU support. | |
2109 | * | |
2110 | * PPC returns ENXIO until 2.6.37 (behavior changed with commit | |
2111 | * b0a873e). | |
2112 | */ | |
2113 | scnprintf(msg, msgsize, "%s", | |
2114 | "The cycles event is not supported, trying to fall back to cpu-clock-ticks"); | |
2115 | ||
2116 | evsel->attr.type = PERF_TYPE_SOFTWARE; | |
2117 | evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK; | |
2118 | ||
04662523 | 2119 | zfree(&evsel->name); |
c0a54341 ACM |
2120 | return true; |
2121 | } | |
2122 | ||
2123 | return false; | |
2124 | } | |
56e52e85 | 2125 | |
602ad878 | 2126 | int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target, |
56e52e85 ACM |
2127 | int err, char *msg, size_t size) |
2128 | { | |
6e81c74c MH |
2129 | char sbuf[STRERR_BUFSIZE]; |
2130 | ||
56e52e85 ACM |
2131 | switch (err) { |
2132 | case EPERM: | |
2133 | case EACCES: | |
b69e63a4 | 2134 | return scnprintf(msg, size, |
56e52e85 ACM |
2135 | "You may not have permission to collect %sstats.\n" |
2136 | "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n" | |
2137 | " -1 - Not paranoid at all\n" | |
2138 | " 0 - Disallow raw tracepoint access for unpriv\n" | |
2139 | " 1 - Disallow cpu events for unpriv\n" | |
2140 | " 2 - Disallow kernel profiling for unpriv", | |
2141 | target->system_wide ? "system-wide " : ""); | |
2142 | case ENOENT: | |
2143 | return scnprintf(msg, size, "The %s event is not supported.", | |
2144 | perf_evsel__name(evsel)); | |
2145 | case EMFILE: | |
2146 | return scnprintf(msg, size, "%s", | |
2147 | "Too many events are opened.\n" | |
18ffdfe8 JO |
2148 | "Probably the maximum number of open file descriptors has been reached.\n" |
2149 | "Hint: Try again after reducing the number of events.\n" | |
2150 | "Hint: Try increasing the limit with 'ulimit -n <limit>'"); | |
56e52e85 ACM |
2151 | case ENODEV: |
2152 | if (target->cpu_list) | |
2153 | return scnprintf(msg, size, "%s", | |
2154 | "No such device - did you specify an out-of-range profile CPU?\n"); | |
2155 | break; | |
2156 | case EOPNOTSUPP: | |
2157 | if (evsel->attr.precise_ip) | |
2158 | return scnprintf(msg, size, "%s", | |
2159 | "\'precise\' request may not be supported. Try removing 'p' modifier."); | |
2160 | #if defined(__i386__) || defined(__x86_64__) | |
2161 | if (evsel->attr.type == PERF_TYPE_HARDWARE) | |
2162 | return scnprintf(msg, size, "%s", | |
2163 | "No hardware sampling interrupt available.\n" | |
2164 | "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it."); | |
2165 | #endif | |
2166 | break; | |
63914aca JO |
2167 | case EBUSY: |
2168 | if (find_process("oprofiled")) | |
2169 | return scnprintf(msg, size, | |
2170 | "The PMU counters are busy/taken by another profiler.\n" | |
2171 | "We found oprofile daemon running, please stop it and try again."); | |
2172 | break; | |
814c8c38 PZ |
2173 | case EINVAL: |
2174 | if (perf_missing_features.clockid) | |
2175 | return scnprintf(msg, size, "clockid feature not supported."); | |
2176 | if (perf_missing_features.clockid_wrong) | |
2177 | return scnprintf(msg, size, "wrong clockid (%d).", clockid); | |
2178 | break; | |
56e52e85 ACM |
2179 | default: |
2180 | break; | |
2181 | } | |
2182 | ||
2183 | return scnprintf(msg, size, | |
6e81c74c | 2184 | "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n" |
56e52e85 ACM |
2185 | "/bin/dmesg may provide additional information.\n" |
2186 | "No CONFIG_PERF_EVENTS=y kernel support configured?\n", | |
6e81c74c MH |
2187 | err, strerror_r(err, sbuf, sizeof(sbuf)), |
2188 | perf_evsel__name(evsel)); | |
56e52e85 | 2189 | } |