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> |
936be503 | 12 | #include "asm/bug.h" |
85c66be1 | 13 | #include <lk/debugfs.h> |
5555ded4 | 14 | #include "event-parse.h" |
69aad6f1 | 15 | #include "evsel.h" |
70082dd9 | 16 | #include "evlist.h" |
69aad6f1 | 17 | #include "util.h" |
86bd5e86 | 18 | #include "cpumap.h" |
fd78260b | 19 | #include "thread_map.h" |
12864b31 | 20 | #include "target.h" |
d2709c7c DH |
21 | #include <linux/hw_breakpoint.h> |
22 | #include <linux/perf_event.h> | |
26d33022 | 23 | #include "perf_regs.h" |
69aad6f1 | 24 | |
594ac61a ACM |
25 | static struct { |
26 | bool sample_id_all; | |
27 | bool exclude_guest; | |
28 | } perf_missing_features; | |
29 | ||
c52b12ed ACM |
30 | #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) |
31 | ||
bde09467 | 32 | static int __perf_evsel__sample_size(u64 sample_type) |
c2a70653 ACM |
33 | { |
34 | u64 mask = sample_type & PERF_SAMPLE_MASK; | |
35 | int size = 0; | |
36 | int i; | |
37 | ||
38 | for (i = 0; i < 64; i++) { | |
39 | if (mask & (1ULL << i)) | |
40 | size++; | |
41 | } | |
42 | ||
43 | size *= sizeof(u64); | |
44 | ||
45 | return size; | |
46 | } | |
47 | ||
4bf9ce1b | 48 | void hists__init(struct hists *hists) |
0e2a5f10 ACM |
49 | { |
50 | memset(hists, 0, sizeof(*hists)); | |
51 | hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT; | |
52 | hists->entries_in = &hists->entries_in_array[0]; | |
53 | hists->entries_collapsed = RB_ROOT; | |
54 | hists->entries = RB_ROOT; | |
55 | pthread_mutex_init(&hists->lock, NULL); | |
56 | } | |
57 | ||
7be5ebe8 ACM |
58 | void __perf_evsel__set_sample_bit(struct perf_evsel *evsel, |
59 | enum perf_event_sample_format bit) | |
60 | { | |
61 | if (!(evsel->attr.sample_type & bit)) { | |
62 | evsel->attr.sample_type |= bit; | |
63 | evsel->sample_size += sizeof(u64); | |
64 | } | |
65 | } | |
66 | ||
67 | void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel, | |
68 | enum perf_event_sample_format bit) | |
69 | { | |
70 | if (evsel->attr.sample_type & bit) { | |
71 | evsel->attr.sample_type &= ~bit; | |
72 | evsel->sample_size -= sizeof(u64); | |
73 | } | |
74 | } | |
75 | ||
7a5a5ca5 ACM |
76 | void perf_evsel__set_sample_id(struct perf_evsel *evsel) |
77 | { | |
78 | perf_evsel__set_sample_bit(evsel, ID); | |
79 | evsel->attr.read_format |= PERF_FORMAT_ID; | |
80 | } | |
81 | ||
ef1d1af2 ACM |
82 | void perf_evsel__init(struct perf_evsel *evsel, |
83 | struct perf_event_attr *attr, int idx) | |
84 | { | |
85 | evsel->idx = idx; | |
86 | evsel->attr = *attr; | |
2cfda562 | 87 | evsel->leader = evsel; |
ef1d1af2 | 88 | INIT_LIST_HEAD(&evsel->node); |
1980c2eb | 89 | hists__init(&evsel->hists); |
bde09467 | 90 | evsel->sample_size = __perf_evsel__sample_size(attr->sample_type); |
ef1d1af2 ACM |
91 | } |
92 | ||
23a2f3ab | 93 | struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx) |
69aad6f1 ACM |
94 | { |
95 | struct perf_evsel *evsel = zalloc(sizeof(*evsel)); | |
96 | ||
ef1d1af2 ACM |
97 | if (evsel != NULL) |
98 | perf_evsel__init(evsel, attr, idx); | |
69aad6f1 ACM |
99 | |
100 | return evsel; | |
101 | } | |
102 | ||
201b7334 | 103 | struct event_format *event_format__new(const char *sys, const char *name) |
efd2b924 ACM |
104 | { |
105 | int fd, n; | |
106 | char *filename; | |
107 | void *bf = NULL, *nbf; | |
108 | size_t size = 0, alloc_size = 0; | |
109 | struct event_format *format = NULL; | |
110 | ||
111 | if (asprintf(&filename, "%s/%s/%s/format", tracing_events_path, sys, name) < 0) | |
112 | goto out; | |
113 | ||
114 | fd = open(filename, O_RDONLY); | |
115 | if (fd < 0) | |
116 | goto out_free_filename; | |
117 | ||
118 | do { | |
119 | if (size == alloc_size) { | |
120 | alloc_size += BUFSIZ; | |
121 | nbf = realloc(bf, alloc_size); | |
122 | if (nbf == NULL) | |
123 | goto out_free_bf; | |
124 | bf = nbf; | |
125 | } | |
126 | ||
127 | n = read(fd, bf + size, BUFSIZ); | |
128 | if (n < 0) | |
129 | goto out_free_bf; | |
130 | size += n; | |
131 | } while (n > 0); | |
132 | ||
133 | pevent_parse_format(&format, bf, size, sys); | |
134 | ||
135 | out_free_bf: | |
136 | free(bf); | |
137 | close(fd); | |
138 | out_free_filename: | |
139 | free(filename); | |
140 | out: | |
141 | return format; | |
142 | } | |
143 | ||
144 | struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx) | |
145 | { | |
146 | struct perf_evsel *evsel = zalloc(sizeof(*evsel)); | |
147 | ||
148 | if (evsel != NULL) { | |
149 | struct perf_event_attr attr = { | |
0b80f8b3 ACM |
150 | .type = PERF_TYPE_TRACEPOINT, |
151 | .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | | |
152 | PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD), | |
efd2b924 ACM |
153 | }; |
154 | ||
e48ffe2b ACM |
155 | if (asprintf(&evsel->name, "%s:%s", sys, name) < 0) |
156 | goto out_free; | |
157 | ||
efd2b924 ACM |
158 | evsel->tp_format = event_format__new(sys, name); |
159 | if (evsel->tp_format == NULL) | |
160 | goto out_free; | |
161 | ||
0b80f8b3 | 162 | event_attr_init(&attr); |
efd2b924 | 163 | attr.config = evsel->tp_format->id; |
0b80f8b3 | 164 | attr.sample_period = 1; |
efd2b924 | 165 | perf_evsel__init(evsel, &attr, idx); |
efd2b924 ACM |
166 | } |
167 | ||
168 | return evsel; | |
169 | ||
170 | out_free: | |
e48ffe2b | 171 | free(evsel->name); |
efd2b924 ACM |
172 | free(evsel); |
173 | return NULL; | |
174 | } | |
175 | ||
8ad7013b | 176 | const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = { |
c410431c ACM |
177 | "cycles", |
178 | "instructions", | |
179 | "cache-references", | |
180 | "cache-misses", | |
181 | "branches", | |
182 | "branch-misses", | |
183 | "bus-cycles", | |
184 | "stalled-cycles-frontend", | |
185 | "stalled-cycles-backend", | |
186 | "ref-cycles", | |
187 | }; | |
188 | ||
dd4f5223 | 189 | static const char *__perf_evsel__hw_name(u64 config) |
c410431c ACM |
190 | { |
191 | if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config]) | |
192 | return perf_evsel__hw_names[config]; | |
193 | ||
194 | return "unknown-hardware"; | |
195 | } | |
196 | ||
27f18617 | 197 | static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size) |
c410431c | 198 | { |
27f18617 | 199 | int colon = 0, r = 0; |
c410431c | 200 | struct perf_event_attr *attr = &evsel->attr; |
c410431c ACM |
201 | bool exclude_guest_default = false; |
202 | ||
203 | #define MOD_PRINT(context, mod) do { \ | |
204 | if (!attr->exclude_##context) { \ | |
27f18617 | 205 | if (!colon) colon = ++r; \ |
c410431c ACM |
206 | r += scnprintf(bf + r, size - r, "%c", mod); \ |
207 | } } while(0) | |
208 | ||
209 | if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) { | |
210 | MOD_PRINT(kernel, 'k'); | |
211 | MOD_PRINT(user, 'u'); | |
212 | MOD_PRINT(hv, 'h'); | |
213 | exclude_guest_default = true; | |
214 | } | |
215 | ||
216 | if (attr->precise_ip) { | |
217 | if (!colon) | |
27f18617 | 218 | colon = ++r; |
c410431c ACM |
219 | r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp"); |
220 | exclude_guest_default = true; | |
221 | } | |
222 | ||
223 | if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) { | |
224 | MOD_PRINT(host, 'H'); | |
225 | MOD_PRINT(guest, 'G'); | |
226 | } | |
227 | #undef MOD_PRINT | |
228 | if (colon) | |
27f18617 | 229 | bf[colon - 1] = ':'; |
c410431c ACM |
230 | return r; |
231 | } | |
232 | ||
27f18617 ACM |
233 | static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size) |
234 | { | |
235 | int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config)); | |
236 | return r + perf_evsel__add_modifiers(evsel, bf + r, size - r); | |
237 | } | |
238 | ||
8ad7013b | 239 | const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = { |
335c2f5d ACM |
240 | "cpu-clock", |
241 | "task-clock", | |
242 | "page-faults", | |
243 | "context-switches", | |
8ad7013b | 244 | "cpu-migrations", |
335c2f5d ACM |
245 | "minor-faults", |
246 | "major-faults", | |
247 | "alignment-faults", | |
248 | "emulation-faults", | |
249 | }; | |
250 | ||
dd4f5223 | 251 | static const char *__perf_evsel__sw_name(u64 config) |
335c2f5d ACM |
252 | { |
253 | if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config]) | |
254 | return perf_evsel__sw_names[config]; | |
255 | return "unknown-software"; | |
256 | } | |
257 | ||
258 | static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size) | |
259 | { | |
260 | int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config)); | |
261 | return r + perf_evsel__add_modifiers(evsel, bf + r, size - r); | |
262 | } | |
263 | ||
287e74aa JO |
264 | static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type) |
265 | { | |
266 | int r; | |
267 | ||
268 | r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr); | |
269 | ||
270 | if (type & HW_BREAKPOINT_R) | |
271 | r += scnprintf(bf + r, size - r, "r"); | |
272 | ||
273 | if (type & HW_BREAKPOINT_W) | |
274 | r += scnprintf(bf + r, size - r, "w"); | |
275 | ||
276 | if (type & HW_BREAKPOINT_X) | |
277 | r += scnprintf(bf + r, size - r, "x"); | |
278 | ||
279 | return r; | |
280 | } | |
281 | ||
282 | static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size) | |
283 | { | |
284 | struct perf_event_attr *attr = &evsel->attr; | |
285 | int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type); | |
286 | return r + perf_evsel__add_modifiers(evsel, bf + r, size - r); | |
287 | } | |
288 | ||
0b668bc9 ACM |
289 | const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX] |
290 | [PERF_EVSEL__MAX_ALIASES] = { | |
291 | { "L1-dcache", "l1-d", "l1d", "L1-data", }, | |
292 | { "L1-icache", "l1-i", "l1i", "L1-instruction", }, | |
293 | { "LLC", "L2", }, | |
294 | { "dTLB", "d-tlb", "Data-TLB", }, | |
295 | { "iTLB", "i-tlb", "Instruction-TLB", }, | |
296 | { "branch", "branches", "bpu", "btb", "bpc", }, | |
297 | { "node", }, | |
298 | }; | |
299 | ||
300 | const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX] | |
301 | [PERF_EVSEL__MAX_ALIASES] = { | |
302 | { "load", "loads", "read", }, | |
303 | { "store", "stores", "write", }, | |
304 | { "prefetch", "prefetches", "speculative-read", "speculative-load", }, | |
305 | }; | |
306 | ||
307 | const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX] | |
308 | [PERF_EVSEL__MAX_ALIASES] = { | |
309 | { "refs", "Reference", "ops", "access", }, | |
310 | { "misses", "miss", }, | |
311 | }; | |
312 | ||
313 | #define C(x) PERF_COUNT_HW_CACHE_##x | |
314 | #define CACHE_READ (1 << C(OP_READ)) | |
315 | #define CACHE_WRITE (1 << C(OP_WRITE)) | |
316 | #define CACHE_PREFETCH (1 << C(OP_PREFETCH)) | |
317 | #define COP(x) (1 << x) | |
318 | ||
319 | /* | |
320 | * cache operartion stat | |
321 | * L1I : Read and prefetch only | |
322 | * ITLB and BPU : Read-only | |
323 | */ | |
324 | static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = { | |
325 | [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), | |
326 | [C(L1I)] = (CACHE_READ | CACHE_PREFETCH), | |
327 | [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), | |
328 | [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), | |
329 | [C(ITLB)] = (CACHE_READ), | |
330 | [C(BPU)] = (CACHE_READ), | |
331 | [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), | |
332 | }; | |
333 | ||
334 | bool perf_evsel__is_cache_op_valid(u8 type, u8 op) | |
335 | { | |
336 | if (perf_evsel__hw_cache_stat[type] & COP(op)) | |
337 | return true; /* valid */ | |
338 | else | |
339 | return false; /* invalid */ | |
340 | } | |
341 | ||
342 | int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result, | |
343 | char *bf, size_t size) | |
344 | { | |
345 | if (result) { | |
346 | return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0], | |
347 | perf_evsel__hw_cache_op[op][0], | |
348 | perf_evsel__hw_cache_result[result][0]); | |
349 | } | |
350 | ||
351 | return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0], | |
352 | perf_evsel__hw_cache_op[op][1]); | |
353 | } | |
354 | ||
dd4f5223 | 355 | static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size) |
0b668bc9 ACM |
356 | { |
357 | u8 op, result, type = (config >> 0) & 0xff; | |
358 | const char *err = "unknown-ext-hardware-cache-type"; | |
359 | ||
360 | if (type > PERF_COUNT_HW_CACHE_MAX) | |
361 | goto out_err; | |
362 | ||
363 | op = (config >> 8) & 0xff; | |
364 | err = "unknown-ext-hardware-cache-op"; | |
365 | if (op > PERF_COUNT_HW_CACHE_OP_MAX) | |
366 | goto out_err; | |
367 | ||
368 | result = (config >> 16) & 0xff; | |
369 | err = "unknown-ext-hardware-cache-result"; | |
370 | if (result > PERF_COUNT_HW_CACHE_RESULT_MAX) | |
371 | goto out_err; | |
372 | ||
373 | err = "invalid-cache"; | |
374 | if (!perf_evsel__is_cache_op_valid(type, op)) | |
375 | goto out_err; | |
376 | ||
377 | return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size); | |
378 | out_err: | |
379 | return scnprintf(bf, size, "%s", err); | |
380 | } | |
381 | ||
382 | static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size) | |
383 | { | |
384 | int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size); | |
385 | return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret); | |
386 | } | |
387 | ||
6eef3d9c ACM |
388 | static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size) |
389 | { | |
390 | int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config); | |
391 | return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret); | |
392 | } | |
393 | ||
7289f83c | 394 | const char *perf_evsel__name(struct perf_evsel *evsel) |
a4460836 | 395 | { |
7289f83c | 396 | char bf[128]; |
a4460836 | 397 | |
7289f83c ACM |
398 | if (evsel->name) |
399 | return evsel->name; | |
c410431c ACM |
400 | |
401 | switch (evsel->attr.type) { | |
402 | case PERF_TYPE_RAW: | |
6eef3d9c | 403 | perf_evsel__raw_name(evsel, bf, sizeof(bf)); |
c410431c ACM |
404 | break; |
405 | ||
406 | case PERF_TYPE_HARDWARE: | |
7289f83c | 407 | perf_evsel__hw_name(evsel, bf, sizeof(bf)); |
c410431c | 408 | break; |
0b668bc9 ACM |
409 | |
410 | case PERF_TYPE_HW_CACHE: | |
7289f83c | 411 | perf_evsel__hw_cache_name(evsel, bf, sizeof(bf)); |
0b668bc9 ACM |
412 | break; |
413 | ||
335c2f5d | 414 | case PERF_TYPE_SOFTWARE: |
7289f83c | 415 | perf_evsel__sw_name(evsel, bf, sizeof(bf)); |
335c2f5d ACM |
416 | break; |
417 | ||
a4460836 | 418 | case PERF_TYPE_TRACEPOINT: |
7289f83c | 419 | scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint"); |
a4460836 ACM |
420 | break; |
421 | ||
287e74aa JO |
422 | case PERF_TYPE_BREAKPOINT: |
423 | perf_evsel__bp_name(evsel, bf, sizeof(bf)); | |
424 | break; | |
425 | ||
c410431c | 426 | default: |
ca1b1457 RR |
427 | scnprintf(bf, sizeof(bf), "unknown attr type: %d", |
428 | evsel->attr.type); | |
a4460836 | 429 | break; |
c410431c ACM |
430 | } |
431 | ||
7289f83c ACM |
432 | evsel->name = strdup(bf); |
433 | ||
434 | return evsel->name ?: "unknown"; | |
c410431c ACM |
435 | } |
436 | ||
717e263f NK |
437 | const char *perf_evsel__group_name(struct perf_evsel *evsel) |
438 | { | |
439 | return evsel->group_name ?: "anon group"; | |
440 | } | |
441 | ||
442 | int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size) | |
443 | { | |
444 | int ret; | |
445 | struct perf_evsel *pos; | |
446 | const char *group_name = perf_evsel__group_name(evsel); | |
447 | ||
448 | ret = scnprintf(buf, size, "%s", group_name); | |
449 | ||
450 | ret += scnprintf(buf + ret, size - ret, " { %s", | |
451 | perf_evsel__name(evsel)); | |
452 | ||
453 | for_each_group_member(pos, evsel) | |
454 | ret += scnprintf(buf + ret, size - ret, ", %s", | |
455 | perf_evsel__name(pos)); | |
456 | ||
457 | ret += scnprintf(buf + ret, size - ret, " }"); | |
458 | ||
459 | return ret; | |
460 | } | |
461 | ||
774cb499 JO |
462 | /* |
463 | * The enable_on_exec/disabled value strategy: | |
464 | * | |
465 | * 1) For any type of traced program: | |
466 | * - all independent events and group leaders are disabled | |
467 | * - all group members are enabled | |
468 | * | |
469 | * Group members are ruled by group leaders. They need to | |
470 | * be enabled, because the group scheduling relies on that. | |
471 | * | |
472 | * 2) For traced programs executed by perf: | |
473 | * - all independent events and group leaders have | |
474 | * enable_on_exec set | |
475 | * - we don't specifically enable or disable any event during | |
476 | * the record command | |
477 | * | |
478 | * Independent events and group leaders are initially disabled | |
479 | * and get enabled by exec. Group members are ruled by group | |
480 | * leaders as stated in 1). | |
481 | * | |
482 | * 3) For traced programs attached by perf (pid/tid): | |
483 | * - we specifically enable or disable all events during | |
484 | * the record command | |
485 | * | |
486 | * When attaching events to already running traced we | |
487 | * enable/disable events specifically, as there's no | |
488 | * initial traced exec call. | |
489 | */ | |
cac21425 JO |
490 | void perf_evsel__config(struct perf_evsel *evsel, |
491 | struct perf_record_opts *opts) | |
0f82ebc4 ACM |
492 | { |
493 | struct perf_event_attr *attr = &evsel->attr; | |
494 | int track = !evsel->idx; /* only the first counter needs these */ | |
495 | ||
594ac61a | 496 | attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1; |
0f82ebc4 | 497 | attr->inherit = !opts->no_inherit; |
0f82ebc4 | 498 | |
7be5ebe8 ACM |
499 | perf_evsel__set_sample_bit(evsel, IP); |
500 | perf_evsel__set_sample_bit(evsel, TID); | |
0f82ebc4 ACM |
501 | |
502 | /* | |
503 | * We default some events to a 1 default interval. But keep | |
504 | * it a weak assumption overridable by the user. | |
505 | */ | |
506 | if (!attr->sample_period || (opts->user_freq != UINT_MAX && | |
507 | opts->user_interval != ULLONG_MAX)) { | |
508 | if (opts->freq) { | |
7be5ebe8 | 509 | perf_evsel__set_sample_bit(evsel, PERIOD); |
0f82ebc4 ACM |
510 | attr->freq = 1; |
511 | attr->sample_freq = opts->freq; | |
512 | } else { | |
513 | attr->sample_period = opts->default_interval; | |
514 | } | |
515 | } | |
516 | ||
517 | if (opts->no_samples) | |
518 | attr->sample_freq = 0; | |
519 | ||
520 | if (opts->inherit_stat) | |
521 | attr->inherit_stat = 1; | |
522 | ||
523 | if (opts->sample_address) { | |
7be5ebe8 | 524 | perf_evsel__set_sample_bit(evsel, ADDR); |
0f82ebc4 ACM |
525 | attr->mmap_data = track; |
526 | } | |
527 | ||
26d33022 | 528 | if (opts->call_graph) { |
7be5ebe8 | 529 | perf_evsel__set_sample_bit(evsel, CALLCHAIN); |
0f82ebc4 | 530 | |
26d33022 | 531 | if (opts->call_graph == CALLCHAIN_DWARF) { |
7be5ebe8 ACM |
532 | perf_evsel__set_sample_bit(evsel, REGS_USER); |
533 | perf_evsel__set_sample_bit(evsel, STACK_USER); | |
26d33022 JO |
534 | attr->sample_regs_user = PERF_REGS_MASK; |
535 | attr->sample_stack_user = opts->stack_dump_size; | |
536 | attr->exclude_callchain_user = 1; | |
537 | } | |
538 | } | |
539 | ||
e40ee742 | 540 | if (perf_target__has_cpu(&opts->target)) |
7be5ebe8 | 541 | perf_evsel__set_sample_bit(evsel, CPU); |
0f82ebc4 | 542 | |
3e76ac78 | 543 | if (opts->period) |
7be5ebe8 | 544 | perf_evsel__set_sample_bit(evsel, PERIOD); |
3e76ac78 | 545 | |
594ac61a | 546 | if (!perf_missing_features.sample_id_all && |
d67356e7 | 547 | (opts->sample_time || !opts->no_inherit || |
aa22dd49 | 548 | perf_target__has_cpu(&opts->target))) |
7be5ebe8 | 549 | perf_evsel__set_sample_bit(evsel, TIME); |
0f82ebc4 ACM |
550 | |
551 | if (opts->raw_samples) { | |
7be5ebe8 ACM |
552 | perf_evsel__set_sample_bit(evsel, TIME); |
553 | perf_evsel__set_sample_bit(evsel, RAW); | |
554 | perf_evsel__set_sample_bit(evsel, CPU); | |
0f82ebc4 ACM |
555 | } |
556 | ||
557 | if (opts->no_delay) { | |
558 | attr->watermark = 0; | |
559 | attr->wakeup_events = 1; | |
560 | } | |
bdfebd84 | 561 | if (opts->branch_stack) { |
7be5ebe8 | 562 | perf_evsel__set_sample_bit(evsel, BRANCH_STACK); |
bdfebd84 RAV |
563 | attr->branch_sample_type = opts->branch_stack; |
564 | } | |
0f82ebc4 ACM |
565 | |
566 | attr->mmap = track; | |
567 | attr->comm = track; | |
568 | ||
774cb499 JO |
569 | /* |
570 | * XXX see the function comment above | |
571 | * | |
572 | * Disabling only independent events or group leaders, | |
573 | * keeping group members enabled. | |
574 | */ | |
823254ed | 575 | if (perf_evsel__is_group_leader(evsel)) |
774cb499 JO |
576 | attr->disabled = 1; |
577 | ||
578 | /* | |
579 | * Setting enable_on_exec for independent events and | |
580 | * group leaders for traced executed by perf. | |
581 | */ | |
823254ed | 582 | if (perf_target__none(&opts->target) && perf_evsel__is_group_leader(evsel)) |
0f82ebc4 | 583 | attr->enable_on_exec = 1; |
0f82ebc4 ACM |
584 | } |
585 | ||
69aad6f1 ACM |
586 | int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads) |
587 | { | |
4af4c955 | 588 | int cpu, thread; |
69aad6f1 | 589 | evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int)); |
4af4c955 DA |
590 | |
591 | if (evsel->fd) { | |
592 | for (cpu = 0; cpu < ncpus; cpu++) { | |
593 | for (thread = 0; thread < nthreads; thread++) { | |
594 | FD(evsel, cpu, thread) = -1; | |
595 | } | |
596 | } | |
597 | } | |
598 | ||
69aad6f1 ACM |
599 | return evsel->fd != NULL ? 0 : -ENOMEM; |
600 | } | |
601 | ||
745cefc5 ACM |
602 | int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads, |
603 | const char *filter) | |
604 | { | |
605 | int cpu, thread; | |
606 | ||
607 | for (cpu = 0; cpu < ncpus; cpu++) { | |
608 | for (thread = 0; thread < nthreads; thread++) { | |
609 | int fd = FD(evsel, cpu, thread), | |
610 | err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter); | |
611 | ||
612 | if (err) | |
613 | return err; | |
614 | } | |
615 | } | |
616 | ||
617 | return 0; | |
618 | } | |
619 | ||
70db7533 ACM |
620 | int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads) |
621 | { | |
a91e5431 ACM |
622 | evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id)); |
623 | if (evsel->sample_id == NULL) | |
624 | return -ENOMEM; | |
625 | ||
626 | evsel->id = zalloc(ncpus * nthreads * sizeof(u64)); | |
627 | if (evsel->id == NULL) { | |
628 | xyarray__delete(evsel->sample_id); | |
629 | evsel->sample_id = NULL; | |
630 | return -ENOMEM; | |
631 | } | |
632 | ||
633 | return 0; | |
70db7533 ACM |
634 | } |
635 | ||
a7e191c3 FD |
636 | void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus) |
637 | { | |
638 | memset(evsel->counts, 0, (sizeof(*evsel->counts) + | |
639 | (ncpus * sizeof(struct perf_counts_values)))); | |
640 | } | |
641 | ||
c52b12ed ACM |
642 | int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus) |
643 | { | |
644 | evsel->counts = zalloc((sizeof(*evsel->counts) + | |
645 | (ncpus * sizeof(struct perf_counts_values)))); | |
646 | return evsel->counts != NULL ? 0 : -ENOMEM; | |
647 | } | |
648 | ||
69aad6f1 ACM |
649 | void perf_evsel__free_fd(struct perf_evsel *evsel) |
650 | { | |
651 | xyarray__delete(evsel->fd); | |
652 | evsel->fd = NULL; | |
653 | } | |
654 | ||
70db7533 ACM |
655 | void perf_evsel__free_id(struct perf_evsel *evsel) |
656 | { | |
a91e5431 ACM |
657 | xyarray__delete(evsel->sample_id); |
658 | evsel->sample_id = NULL; | |
659 | free(evsel->id); | |
70db7533 ACM |
660 | evsel->id = NULL; |
661 | } | |
662 | ||
c52b12ed ACM |
663 | void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads) |
664 | { | |
665 | int cpu, thread; | |
666 | ||
667 | for (cpu = 0; cpu < ncpus; cpu++) | |
668 | for (thread = 0; thread < nthreads; ++thread) { | |
669 | close(FD(evsel, cpu, thread)); | |
670 | FD(evsel, cpu, thread) = -1; | |
671 | } | |
672 | } | |
673 | ||
43f8e76e NK |
674 | void perf_evsel__free_counts(struct perf_evsel *evsel) |
675 | { | |
676 | free(evsel->counts); | |
677 | } | |
678 | ||
ef1d1af2 | 679 | void perf_evsel__exit(struct perf_evsel *evsel) |
69aad6f1 ACM |
680 | { |
681 | assert(list_empty(&evsel->node)); | |
736b05a0 NK |
682 | perf_evsel__free_fd(evsel); |
683 | perf_evsel__free_id(evsel); | |
ef1d1af2 ACM |
684 | } |
685 | ||
686 | void perf_evsel__delete(struct perf_evsel *evsel) | |
687 | { | |
688 | perf_evsel__exit(evsel); | |
023695d9 | 689 | close_cgroup(evsel->cgrp); |
6a4bb04c | 690 | free(evsel->group_name); |
e48ffe2b | 691 | if (evsel->tp_format) |
efd2b924 | 692 | pevent_free_format(evsel->tp_format); |
f0c55bcf | 693 | free(evsel->name); |
69aad6f1 ACM |
694 | free(evsel); |
695 | } | |
c52b12ed | 696 | |
c7a79c47 SE |
697 | static inline void compute_deltas(struct perf_evsel *evsel, |
698 | int cpu, | |
699 | struct perf_counts_values *count) | |
700 | { | |
701 | struct perf_counts_values tmp; | |
702 | ||
703 | if (!evsel->prev_raw_counts) | |
704 | return; | |
705 | ||
706 | if (cpu == -1) { | |
707 | tmp = evsel->prev_raw_counts->aggr; | |
708 | evsel->prev_raw_counts->aggr = *count; | |
709 | } else { | |
710 | tmp = evsel->prev_raw_counts->cpu[cpu]; | |
711 | evsel->prev_raw_counts->cpu[cpu] = *count; | |
712 | } | |
713 | ||
714 | count->val = count->val - tmp.val; | |
715 | count->ena = count->ena - tmp.ena; | |
716 | count->run = count->run - tmp.run; | |
717 | } | |
718 | ||
c52b12ed ACM |
719 | int __perf_evsel__read_on_cpu(struct perf_evsel *evsel, |
720 | int cpu, int thread, bool scale) | |
721 | { | |
722 | struct perf_counts_values count; | |
723 | size_t nv = scale ? 3 : 1; | |
724 | ||
725 | if (FD(evsel, cpu, thread) < 0) | |
726 | return -EINVAL; | |
727 | ||
4eed11d5 ACM |
728 | if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0) |
729 | return -ENOMEM; | |
730 | ||
c52b12ed ACM |
731 | if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0) |
732 | return -errno; | |
733 | ||
c7a79c47 SE |
734 | compute_deltas(evsel, cpu, &count); |
735 | ||
c52b12ed ACM |
736 | if (scale) { |
737 | if (count.run == 0) | |
738 | count.val = 0; | |
739 | else if (count.run < count.ena) | |
740 | count.val = (u64)((double)count.val * count.ena / count.run + 0.5); | |
741 | } else | |
742 | count.ena = count.run = 0; | |
743 | ||
744 | evsel->counts->cpu[cpu] = count; | |
745 | return 0; | |
746 | } | |
747 | ||
748 | int __perf_evsel__read(struct perf_evsel *evsel, | |
749 | int ncpus, int nthreads, bool scale) | |
750 | { | |
751 | size_t nv = scale ? 3 : 1; | |
752 | int cpu, thread; | |
753 | struct perf_counts_values *aggr = &evsel->counts->aggr, count; | |
754 | ||
52bcd994 | 755 | aggr->val = aggr->ena = aggr->run = 0; |
c52b12ed ACM |
756 | |
757 | for (cpu = 0; cpu < ncpus; cpu++) { | |
758 | for (thread = 0; thread < nthreads; thread++) { | |
759 | if (FD(evsel, cpu, thread) < 0) | |
760 | continue; | |
761 | ||
762 | if (readn(FD(evsel, cpu, thread), | |
763 | &count, nv * sizeof(u64)) < 0) | |
764 | return -errno; | |
765 | ||
766 | aggr->val += count.val; | |
767 | if (scale) { | |
768 | aggr->ena += count.ena; | |
769 | aggr->run += count.run; | |
770 | } | |
771 | } | |
772 | } | |
773 | ||
c7a79c47 SE |
774 | compute_deltas(evsel, -1, aggr); |
775 | ||
c52b12ed ACM |
776 | evsel->counts->scaled = 0; |
777 | if (scale) { | |
778 | if (aggr->run == 0) { | |
779 | evsel->counts->scaled = -1; | |
780 | aggr->val = 0; | |
781 | return 0; | |
782 | } | |
783 | ||
784 | if (aggr->run < aggr->ena) { | |
785 | evsel->counts->scaled = 1; | |
786 | aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5); | |
787 | } | |
788 | } else | |
789 | aggr->ena = aggr->run = 0; | |
790 | ||
791 | return 0; | |
792 | } | |
48290609 | 793 | |
6a4bb04c JO |
794 | static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread) |
795 | { | |
796 | struct perf_evsel *leader = evsel->leader; | |
797 | int fd; | |
798 | ||
823254ed | 799 | if (perf_evsel__is_group_leader(evsel)) |
6a4bb04c JO |
800 | return -1; |
801 | ||
802 | /* | |
803 | * Leader must be already processed/open, | |
804 | * if not it's a bug. | |
805 | */ | |
806 | BUG_ON(!leader->fd); | |
807 | ||
808 | fd = FD(leader, cpu, thread); | |
809 | BUG_ON(fd == -1); | |
810 | ||
811 | return fd; | |
812 | } | |
813 | ||
0252208e | 814 | static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus, |
6a4bb04c | 815 | struct thread_map *threads) |
48290609 | 816 | { |
0252208e | 817 | int cpu, thread; |
023695d9 | 818 | unsigned long flags = 0; |
727ab04e | 819 | int pid = -1, err; |
48290609 | 820 | |
0252208e ACM |
821 | if (evsel->fd == NULL && |
822 | perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0) | |
727ab04e | 823 | return -ENOMEM; |
4eed11d5 | 824 | |
023695d9 SE |
825 | if (evsel->cgrp) { |
826 | flags = PERF_FLAG_PID_CGROUP; | |
827 | pid = evsel->cgrp->fd; | |
828 | } | |
829 | ||
594ac61a ACM |
830 | fallback_missing_features: |
831 | if (perf_missing_features.exclude_guest) | |
832 | evsel->attr.exclude_guest = evsel->attr.exclude_host = 0; | |
833 | retry_sample_id: | |
834 | if (perf_missing_features.sample_id_all) | |
835 | evsel->attr.sample_id_all = 0; | |
836 | ||
86bd5e86 | 837 | for (cpu = 0; cpu < cpus->nr; cpu++) { |
9d04f178 | 838 | |
0252208e | 839 | for (thread = 0; thread < threads->nr; thread++) { |
6a4bb04c | 840 | int group_fd; |
023695d9 SE |
841 | |
842 | if (!evsel->cgrp) | |
843 | pid = threads->map[thread]; | |
844 | ||
6a4bb04c JO |
845 | group_fd = get_group_fd(evsel, cpu, thread); |
846 | ||
0252208e | 847 | FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr, |
023695d9 | 848 | pid, |
f08199d3 | 849 | cpus->map[cpu], |
023695d9 | 850 | group_fd, flags); |
727ab04e ACM |
851 | if (FD(evsel, cpu, thread) < 0) { |
852 | err = -errno; | |
594ac61a | 853 | goto try_fallback; |
727ab04e | 854 | } |
0252208e | 855 | } |
48290609 ACM |
856 | } |
857 | ||
858 | return 0; | |
859 | ||
594ac61a ACM |
860 | try_fallback: |
861 | if (err != -EINVAL || cpu > 0 || thread > 0) | |
862 | goto out_close; | |
863 | ||
864 | if (!perf_missing_features.exclude_guest && | |
865 | (evsel->attr.exclude_guest || evsel->attr.exclude_host)) { | |
866 | perf_missing_features.exclude_guest = true; | |
867 | goto fallback_missing_features; | |
868 | } else if (!perf_missing_features.sample_id_all) { | |
869 | perf_missing_features.sample_id_all = true; | |
870 | goto retry_sample_id; | |
871 | } | |
872 | ||
48290609 | 873 | out_close: |
0252208e ACM |
874 | do { |
875 | while (--thread >= 0) { | |
876 | close(FD(evsel, cpu, thread)); | |
877 | FD(evsel, cpu, thread) = -1; | |
878 | } | |
879 | thread = threads->nr; | |
880 | } while (--cpu >= 0); | |
727ab04e ACM |
881 | return err; |
882 | } | |
883 | ||
884 | void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads) | |
885 | { | |
886 | if (evsel->fd == NULL) | |
887 | return; | |
888 | ||
889 | perf_evsel__close_fd(evsel, ncpus, nthreads); | |
890 | perf_evsel__free_fd(evsel); | |
891 | evsel->fd = NULL; | |
48290609 ACM |
892 | } |
893 | ||
0252208e ACM |
894 | static struct { |
895 | struct cpu_map map; | |
896 | int cpus[1]; | |
897 | } empty_cpu_map = { | |
898 | .map.nr = 1, | |
899 | .cpus = { -1, }, | |
900 | }; | |
901 | ||
902 | static struct { | |
903 | struct thread_map map; | |
904 | int threads[1]; | |
905 | } empty_thread_map = { | |
906 | .map.nr = 1, | |
907 | .threads = { -1, }, | |
908 | }; | |
909 | ||
f08199d3 | 910 | int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus, |
6a4bb04c | 911 | struct thread_map *threads) |
48290609 | 912 | { |
0252208e ACM |
913 | if (cpus == NULL) { |
914 | /* Work around old compiler warnings about strict aliasing */ | |
915 | cpus = &empty_cpu_map.map; | |
48290609 ACM |
916 | } |
917 | ||
0252208e ACM |
918 | if (threads == NULL) |
919 | threads = &empty_thread_map.map; | |
48290609 | 920 | |
6a4bb04c | 921 | return __perf_evsel__open(evsel, cpus, threads); |
48290609 ACM |
922 | } |
923 | ||
f08199d3 | 924 | int perf_evsel__open_per_cpu(struct perf_evsel *evsel, |
6a4bb04c | 925 | struct cpu_map *cpus) |
48290609 | 926 | { |
6a4bb04c | 927 | return __perf_evsel__open(evsel, cpus, &empty_thread_map.map); |
0252208e | 928 | } |
48290609 | 929 | |
f08199d3 | 930 | int perf_evsel__open_per_thread(struct perf_evsel *evsel, |
6a4bb04c | 931 | struct thread_map *threads) |
0252208e | 932 | { |
6a4bb04c | 933 | return __perf_evsel__open(evsel, &empty_cpu_map.map, threads); |
48290609 | 934 | } |
70082dd9 | 935 | |
0807d2d8 ACM |
936 | static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel, |
937 | const union perf_event *event, | |
938 | struct perf_sample *sample) | |
d0dd74e8 | 939 | { |
0807d2d8 | 940 | u64 type = evsel->attr.sample_type; |
d0dd74e8 | 941 | const u64 *array = event->sample.array; |
0807d2d8 | 942 | bool swapped = evsel->needs_swap; |
37073f9e | 943 | union u64_swap u; |
d0dd74e8 ACM |
944 | |
945 | array += ((event->header.size - | |
946 | sizeof(event->header)) / sizeof(u64)) - 1; | |
947 | ||
948 | if (type & PERF_SAMPLE_CPU) { | |
37073f9e JO |
949 | u.val64 = *array; |
950 | if (swapped) { | |
951 | /* undo swap of u64, then swap on individual u32s */ | |
952 | u.val64 = bswap_64(u.val64); | |
953 | u.val32[0] = bswap_32(u.val32[0]); | |
954 | } | |
955 | ||
956 | sample->cpu = u.val32[0]; | |
d0dd74e8 ACM |
957 | array--; |
958 | } | |
959 | ||
960 | if (type & PERF_SAMPLE_STREAM_ID) { | |
961 | sample->stream_id = *array; | |
962 | array--; | |
963 | } | |
964 | ||
965 | if (type & PERF_SAMPLE_ID) { | |
966 | sample->id = *array; | |
967 | array--; | |
968 | } | |
969 | ||
970 | if (type & PERF_SAMPLE_TIME) { | |
971 | sample->time = *array; | |
972 | array--; | |
973 | } | |
974 | ||
975 | if (type & PERF_SAMPLE_TID) { | |
37073f9e JO |
976 | u.val64 = *array; |
977 | if (swapped) { | |
978 | /* undo swap of u64, then swap on individual u32s */ | |
979 | u.val64 = bswap_64(u.val64); | |
980 | u.val32[0] = bswap_32(u.val32[0]); | |
981 | u.val32[1] = bswap_32(u.val32[1]); | |
982 | } | |
983 | ||
984 | sample->pid = u.val32[0]; | |
985 | sample->tid = u.val32[1]; | |
d0dd74e8 ACM |
986 | } |
987 | ||
988 | return 0; | |
989 | } | |
990 | ||
98e1da90 FW |
991 | static bool sample_overlap(const union perf_event *event, |
992 | const void *offset, u64 size) | |
993 | { | |
994 | const void *base = event; | |
995 | ||
996 | if (offset + size > base + event->header.size) | |
997 | return true; | |
998 | ||
999 | return false; | |
1000 | } | |
1001 | ||
a3f698fe | 1002 | int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event, |
0807d2d8 | 1003 | struct perf_sample *data) |
d0dd74e8 | 1004 | { |
a3f698fe | 1005 | u64 type = evsel->attr.sample_type; |
0f6a3015 | 1006 | u64 regs_user = evsel->attr.sample_regs_user; |
0807d2d8 | 1007 | bool swapped = evsel->needs_swap; |
d0dd74e8 ACM |
1008 | const u64 *array; |
1009 | ||
936be503 DA |
1010 | /* |
1011 | * used for cross-endian analysis. See git commit 65014ab3 | |
1012 | * for why this goofiness is needed. | |
1013 | */ | |
6a11f92e | 1014 | union u64_swap u; |
936be503 | 1015 | |
f3bda2c9 | 1016 | memset(data, 0, sizeof(*data)); |
d0dd74e8 ACM |
1017 | data->cpu = data->pid = data->tid = -1; |
1018 | data->stream_id = data->id = data->time = -1ULL; | |
a4a03fc7 | 1019 | data->period = 1; |
d0dd74e8 ACM |
1020 | |
1021 | if (event->header.type != PERF_RECORD_SAMPLE) { | |
a3f698fe | 1022 | if (!evsel->attr.sample_id_all) |
d0dd74e8 | 1023 | return 0; |
0807d2d8 | 1024 | return perf_evsel__parse_id_sample(evsel, event, data); |
d0dd74e8 ACM |
1025 | } |
1026 | ||
1027 | array = event->sample.array; | |
1028 | ||
a3f698fe | 1029 | if (evsel->sample_size + sizeof(event->header) > event->header.size) |
a2854124 FW |
1030 | return -EFAULT; |
1031 | ||
d0dd74e8 ACM |
1032 | if (type & PERF_SAMPLE_IP) { |
1033 | data->ip = event->ip.ip; | |
1034 | array++; | |
1035 | } | |
1036 | ||
1037 | if (type & PERF_SAMPLE_TID) { | |
936be503 DA |
1038 | u.val64 = *array; |
1039 | if (swapped) { | |
1040 | /* undo swap of u64, then swap on individual u32s */ | |
1041 | u.val64 = bswap_64(u.val64); | |
1042 | u.val32[0] = bswap_32(u.val32[0]); | |
1043 | u.val32[1] = bswap_32(u.val32[1]); | |
1044 | } | |
1045 | ||
1046 | data->pid = u.val32[0]; | |
1047 | data->tid = u.val32[1]; | |
d0dd74e8 ACM |
1048 | array++; |
1049 | } | |
1050 | ||
1051 | if (type & PERF_SAMPLE_TIME) { | |
1052 | data->time = *array; | |
1053 | array++; | |
1054 | } | |
1055 | ||
7cec0922 | 1056 | data->addr = 0; |
d0dd74e8 ACM |
1057 | if (type & PERF_SAMPLE_ADDR) { |
1058 | data->addr = *array; | |
1059 | array++; | |
1060 | } | |
1061 | ||
1062 | data->id = -1ULL; | |
1063 | if (type & PERF_SAMPLE_ID) { | |
1064 | data->id = *array; | |
1065 | array++; | |
1066 | } | |
1067 | ||
1068 | if (type & PERF_SAMPLE_STREAM_ID) { | |
1069 | data->stream_id = *array; | |
1070 | array++; | |
1071 | } | |
1072 | ||
1073 | if (type & PERF_SAMPLE_CPU) { | |
936be503 DA |
1074 | |
1075 | u.val64 = *array; | |
1076 | if (swapped) { | |
1077 | /* undo swap of u64, then swap on individual u32s */ | |
1078 | u.val64 = bswap_64(u.val64); | |
1079 | u.val32[0] = bswap_32(u.val32[0]); | |
1080 | } | |
1081 | ||
1082 | data->cpu = u.val32[0]; | |
d0dd74e8 ACM |
1083 | array++; |
1084 | } | |
1085 | ||
1086 | if (type & PERF_SAMPLE_PERIOD) { | |
1087 | data->period = *array; | |
1088 | array++; | |
1089 | } | |
1090 | ||
1091 | if (type & PERF_SAMPLE_READ) { | |
f9d36996 | 1092 | fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n"); |
d0dd74e8 ACM |
1093 | return -1; |
1094 | } | |
1095 | ||
1096 | if (type & PERF_SAMPLE_CALLCHAIN) { | |
98e1da90 FW |
1097 | if (sample_overlap(event, array, sizeof(data->callchain->nr))) |
1098 | return -EFAULT; | |
1099 | ||
d0dd74e8 | 1100 | data->callchain = (struct ip_callchain *)array; |
98e1da90 FW |
1101 | |
1102 | if (sample_overlap(event, array, data->callchain->nr)) | |
1103 | return -EFAULT; | |
1104 | ||
d0dd74e8 ACM |
1105 | array += 1 + data->callchain->nr; |
1106 | } | |
1107 | ||
1108 | if (type & PERF_SAMPLE_RAW) { | |
8e303f20 JO |
1109 | const u64 *pdata; |
1110 | ||
936be503 DA |
1111 | u.val64 = *array; |
1112 | if (WARN_ONCE(swapped, | |
1113 | "Endianness of raw data not corrected!\n")) { | |
1114 | /* undo swap of u64, then swap on individual u32s */ | |
1115 | u.val64 = bswap_64(u.val64); | |
1116 | u.val32[0] = bswap_32(u.val32[0]); | |
1117 | u.val32[1] = bswap_32(u.val32[1]); | |
1118 | } | |
98e1da90 FW |
1119 | |
1120 | if (sample_overlap(event, array, sizeof(u32))) | |
1121 | return -EFAULT; | |
1122 | ||
936be503 | 1123 | data->raw_size = u.val32[0]; |
8e303f20 | 1124 | pdata = (void *) array + sizeof(u32); |
98e1da90 | 1125 | |
8e303f20 | 1126 | if (sample_overlap(event, pdata, data->raw_size)) |
98e1da90 FW |
1127 | return -EFAULT; |
1128 | ||
8e303f20 | 1129 | data->raw_data = (void *) pdata; |
fa30c964 SE |
1130 | |
1131 | array = (void *)array + data->raw_size + sizeof(u32); | |
d0dd74e8 ACM |
1132 | } |
1133 | ||
b5387528 RAV |
1134 | if (type & PERF_SAMPLE_BRANCH_STACK) { |
1135 | u64 sz; | |
1136 | ||
1137 | data->branch_stack = (struct branch_stack *)array; | |
1138 | array++; /* nr */ | |
1139 | ||
1140 | sz = data->branch_stack->nr * sizeof(struct branch_entry); | |
1141 | sz /= sizeof(u64); | |
1142 | array += sz; | |
1143 | } | |
0f6a3015 JO |
1144 | |
1145 | if (type & PERF_SAMPLE_REGS_USER) { | |
1146 | /* First u64 tells us if we have any regs in sample. */ | |
1147 | u64 avail = *array++; | |
1148 | ||
1149 | if (avail) { | |
1150 | data->user_regs.regs = (u64 *)array; | |
1151 | array += hweight_long(regs_user); | |
1152 | } | |
1153 | } | |
1154 | ||
1155 | if (type & PERF_SAMPLE_STACK_USER) { | |
1156 | u64 size = *array++; | |
1157 | ||
1158 | data->user_stack.offset = ((char *)(array - 1) | |
1159 | - (char *) event); | |
1160 | ||
1161 | if (!size) { | |
1162 | data->user_stack.size = 0; | |
1163 | } else { | |
1164 | data->user_stack.data = (char *)array; | |
1165 | array += size / sizeof(*array); | |
1166 | data->user_stack.size = *array; | |
1167 | } | |
1168 | } | |
1169 | ||
d0dd74e8 ACM |
1170 | return 0; |
1171 | } | |
74eec26f AV |
1172 | |
1173 | int perf_event__synthesize_sample(union perf_event *event, u64 type, | |
1174 | const struct perf_sample *sample, | |
1175 | bool swapped) | |
1176 | { | |
1177 | u64 *array; | |
1178 | ||
1179 | /* | |
1180 | * used for cross-endian analysis. See git commit 65014ab3 | |
1181 | * for why this goofiness is needed. | |
1182 | */ | |
6a11f92e | 1183 | union u64_swap u; |
74eec26f AV |
1184 | |
1185 | array = event->sample.array; | |
1186 | ||
1187 | if (type & PERF_SAMPLE_IP) { | |
1188 | event->ip.ip = sample->ip; | |
1189 | array++; | |
1190 | } | |
1191 | ||
1192 | if (type & PERF_SAMPLE_TID) { | |
1193 | u.val32[0] = sample->pid; | |
1194 | u.val32[1] = sample->tid; | |
1195 | if (swapped) { | |
1196 | /* | |
a3f698fe | 1197 | * Inverse of what is done in perf_evsel__parse_sample |
74eec26f AV |
1198 | */ |
1199 | u.val32[0] = bswap_32(u.val32[0]); | |
1200 | u.val32[1] = bswap_32(u.val32[1]); | |
1201 | u.val64 = bswap_64(u.val64); | |
1202 | } | |
1203 | ||
1204 | *array = u.val64; | |
1205 | array++; | |
1206 | } | |
1207 | ||
1208 | if (type & PERF_SAMPLE_TIME) { | |
1209 | *array = sample->time; | |
1210 | array++; | |
1211 | } | |
1212 | ||
1213 | if (type & PERF_SAMPLE_ADDR) { | |
1214 | *array = sample->addr; | |
1215 | array++; | |
1216 | } | |
1217 | ||
1218 | if (type & PERF_SAMPLE_ID) { | |
1219 | *array = sample->id; | |
1220 | array++; | |
1221 | } | |
1222 | ||
1223 | if (type & PERF_SAMPLE_STREAM_ID) { | |
1224 | *array = sample->stream_id; | |
1225 | array++; | |
1226 | } | |
1227 | ||
1228 | if (type & PERF_SAMPLE_CPU) { | |
1229 | u.val32[0] = sample->cpu; | |
1230 | if (swapped) { | |
1231 | /* | |
a3f698fe | 1232 | * Inverse of what is done in perf_evsel__parse_sample |
74eec26f AV |
1233 | */ |
1234 | u.val32[0] = bswap_32(u.val32[0]); | |
1235 | u.val64 = bswap_64(u.val64); | |
1236 | } | |
1237 | *array = u.val64; | |
1238 | array++; | |
1239 | } | |
1240 | ||
1241 | if (type & PERF_SAMPLE_PERIOD) { | |
1242 | *array = sample->period; | |
1243 | array++; | |
1244 | } | |
1245 | ||
1246 | return 0; | |
1247 | } | |
5555ded4 | 1248 | |
efd2b924 ACM |
1249 | struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name) |
1250 | { | |
1251 | return pevent_find_field(evsel->tp_format, name); | |
1252 | } | |
1253 | ||
5d2074ea | 1254 | void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample, |
5555ded4 ACM |
1255 | const char *name) |
1256 | { | |
efd2b924 | 1257 | struct format_field *field = perf_evsel__field(evsel, name); |
5555ded4 ACM |
1258 | int offset; |
1259 | ||
efd2b924 ACM |
1260 | if (!field) |
1261 | return NULL; | |
5555ded4 ACM |
1262 | |
1263 | offset = field->offset; | |
1264 | ||
1265 | if (field->flags & FIELD_IS_DYNAMIC) { | |
1266 | offset = *(int *)(sample->raw_data + field->offset); | |
1267 | offset &= 0xffff; | |
1268 | } | |
1269 | ||
1270 | return sample->raw_data + offset; | |
1271 | } | |
1272 | ||
1273 | u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample, | |
1274 | const char *name) | |
1275 | { | |
efd2b924 | 1276 | struct format_field *field = perf_evsel__field(evsel, name); |
e6b6f679 ACM |
1277 | void *ptr; |
1278 | u64 value; | |
5555ded4 | 1279 | |
efd2b924 ACM |
1280 | if (!field) |
1281 | return 0; | |
5555ded4 | 1282 | |
e6b6f679 | 1283 | ptr = sample->raw_data + field->offset; |
5555ded4 | 1284 | |
e6b6f679 ACM |
1285 | switch (field->size) { |
1286 | case 1: | |
1287 | return *(u8 *)ptr; | |
1288 | case 2: | |
1289 | value = *(u16 *)ptr; | |
1290 | break; | |
1291 | case 4: | |
1292 | value = *(u32 *)ptr; | |
1293 | break; | |
1294 | case 8: | |
1295 | value = *(u64 *)ptr; | |
1296 | break; | |
1297 | default: | |
1298 | return 0; | |
1299 | } | |
1300 | ||
1301 | if (!evsel->needs_swap) | |
1302 | return value; | |
1303 | ||
1304 | switch (field->size) { | |
1305 | case 2: | |
1306 | return bswap_16(value); | |
1307 | case 4: | |
1308 | return bswap_32(value); | |
1309 | case 8: | |
1310 | return bswap_64(value); | |
1311 | default: | |
1312 | return 0; | |
1313 | } | |
1314 | ||
1315 | return 0; | |
5555ded4 | 1316 | } |
0698aedd ACM |
1317 | |
1318 | static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...) | |
1319 | { | |
1320 | va_list args; | |
1321 | int ret = 0; | |
1322 | ||
1323 | if (!*first) { | |
1324 | ret += fprintf(fp, ","); | |
1325 | } else { | |
1326 | ret += fprintf(fp, ":"); | |
1327 | *first = false; | |
1328 | } | |
1329 | ||
1330 | va_start(args, fmt); | |
1331 | ret += vfprintf(fp, fmt, args); | |
1332 | va_end(args); | |
1333 | return ret; | |
1334 | } | |
1335 | ||
1336 | static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value) | |
1337 | { | |
1338 | if (value == 0) | |
1339 | return 0; | |
1340 | ||
1341 | return comma_fprintf(fp, first, " %s: %" PRIu64, field, value); | |
1342 | } | |
1343 | ||
1344 | #define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field) | |
1345 | ||
c79a4393 ACM |
1346 | struct bit_names { |
1347 | int bit; | |
1348 | const char *name; | |
1349 | }; | |
1350 | ||
1351 | static int bits__fprintf(FILE *fp, const char *field, u64 value, | |
1352 | struct bit_names *bits, bool *first) | |
1353 | { | |
1354 | int i = 0, printed = comma_fprintf(fp, first, " %s: ", field); | |
1355 | bool first_bit = true; | |
1356 | ||
1357 | do { | |
1358 | if (value & bits[i].bit) { | |
1359 | printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name); | |
1360 | first_bit = false; | |
1361 | } | |
1362 | } while (bits[++i].name != NULL); | |
1363 | ||
1364 | return printed; | |
1365 | } | |
1366 | ||
1367 | static int sample_type__fprintf(FILE *fp, bool *first, u64 value) | |
1368 | { | |
1369 | #define bit_name(n) { PERF_SAMPLE_##n, #n } | |
1370 | struct bit_names bits[] = { | |
1371 | bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR), | |
1372 | bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU), | |
1373 | bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW), | |
1374 | bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER), | |
1375 | { .name = NULL, } | |
1376 | }; | |
1377 | #undef bit_name | |
1378 | return bits__fprintf(fp, "sample_type", value, bits, first); | |
1379 | } | |
1380 | ||
1381 | static int read_format__fprintf(FILE *fp, bool *first, u64 value) | |
1382 | { | |
1383 | #define bit_name(n) { PERF_FORMAT_##n, #n } | |
1384 | struct bit_names bits[] = { | |
1385 | bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING), | |
1386 | bit_name(ID), bit_name(GROUP), | |
1387 | { .name = NULL, } | |
1388 | }; | |
1389 | #undef bit_name | |
1390 | return bits__fprintf(fp, "read_format", value, bits, first); | |
1391 | } | |
1392 | ||
0698aedd ACM |
1393 | int perf_evsel__fprintf(struct perf_evsel *evsel, |
1394 | struct perf_attr_details *details, FILE *fp) | |
1395 | { | |
1396 | bool first = true; | |
e6ab07d0 NK |
1397 | int printed = 0; |
1398 | ||
e35ef355 | 1399 | if (details->event_group) { |
e6ab07d0 NK |
1400 | struct perf_evsel *pos; |
1401 | ||
1402 | if (!perf_evsel__is_group_leader(evsel)) | |
1403 | return 0; | |
1404 | ||
1405 | if (evsel->nr_members > 1) | |
1406 | printed += fprintf(fp, "%s{", evsel->group_name ?: ""); | |
1407 | ||
1408 | printed += fprintf(fp, "%s", perf_evsel__name(evsel)); | |
1409 | for_each_group_member(pos, evsel) | |
1410 | printed += fprintf(fp, ",%s", perf_evsel__name(pos)); | |
1411 | ||
1412 | if (evsel->nr_members > 1) | |
1413 | printed += fprintf(fp, "}"); | |
1414 | goto out; | |
1415 | } | |
1416 | ||
1417 | printed += fprintf(fp, "%s", perf_evsel__name(evsel)); | |
0698aedd ACM |
1418 | |
1419 | if (details->verbose || details->freq) { | |
1420 | printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64, | |
1421 | (u64)evsel->attr.sample_freq); | |
1422 | } | |
1423 | ||
1424 | if (details->verbose) { | |
1425 | if_print(type); | |
1426 | if_print(config); | |
1427 | if_print(config1); | |
1428 | if_print(config2); | |
1429 | if_print(size); | |
c79a4393 ACM |
1430 | printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type); |
1431 | if (evsel->attr.read_format) | |
1432 | printed += read_format__fprintf(fp, &first, evsel->attr.read_format); | |
0698aedd ACM |
1433 | if_print(disabled); |
1434 | if_print(inherit); | |
1435 | if_print(pinned); | |
1436 | if_print(exclusive); | |
1437 | if_print(exclude_user); | |
1438 | if_print(exclude_kernel); | |
1439 | if_print(exclude_hv); | |
1440 | if_print(exclude_idle); | |
1441 | if_print(mmap); | |
1442 | if_print(comm); | |
1443 | if_print(freq); | |
1444 | if_print(inherit_stat); | |
1445 | if_print(enable_on_exec); | |
1446 | if_print(task); | |
1447 | if_print(watermark); | |
1448 | if_print(precise_ip); | |
1449 | if_print(mmap_data); | |
1450 | if_print(sample_id_all); | |
1451 | if_print(exclude_host); | |
1452 | if_print(exclude_guest); | |
1453 | if_print(__reserved_1); | |
1454 | if_print(wakeup_events); | |
1455 | if_print(bp_type); | |
1456 | if_print(branch_sample_type); | |
1457 | } | |
e6ab07d0 | 1458 | out: |
0698aedd ACM |
1459 | fputc('\n', fp); |
1460 | return ++printed; | |
1461 | } | |
c0a54341 ACM |
1462 | |
1463 | bool perf_evsel__fallback(struct perf_evsel *evsel, int err, | |
1464 | char *msg, size_t msgsize) | |
1465 | { | |
1466 | if ((err == ENOENT || err == ENXIO) && | |
1467 | evsel->attr.type == PERF_TYPE_HARDWARE && | |
1468 | evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) { | |
1469 | /* | |
1470 | * If it's cycles then fall back to hrtimer based | |
1471 | * cpu-clock-tick sw counter, which is always available even if | |
1472 | * no PMU support. | |
1473 | * | |
1474 | * PPC returns ENXIO until 2.6.37 (behavior changed with commit | |
1475 | * b0a873e). | |
1476 | */ | |
1477 | scnprintf(msg, msgsize, "%s", | |
1478 | "The cycles event is not supported, trying to fall back to cpu-clock-ticks"); | |
1479 | ||
1480 | evsel->attr.type = PERF_TYPE_SOFTWARE; | |
1481 | evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK; | |
1482 | ||
1483 | free(evsel->name); | |
1484 | evsel->name = NULL; | |
1485 | return true; | |
1486 | } | |
1487 | ||
1488 | return false; | |
1489 | } | |
56e52e85 ACM |
1490 | |
1491 | int perf_evsel__open_strerror(struct perf_evsel *evsel, | |
1492 | struct perf_target *target, | |
1493 | int err, char *msg, size_t size) | |
1494 | { | |
1495 | switch (err) { | |
1496 | case EPERM: | |
1497 | case EACCES: | |
1498 | return scnprintf(msg, size, "%s", | |
1499 | "You may not have permission to collect %sstats.\n" | |
1500 | "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n" | |
1501 | " -1 - Not paranoid at all\n" | |
1502 | " 0 - Disallow raw tracepoint access for unpriv\n" | |
1503 | " 1 - Disallow cpu events for unpriv\n" | |
1504 | " 2 - Disallow kernel profiling for unpriv", | |
1505 | target->system_wide ? "system-wide " : ""); | |
1506 | case ENOENT: | |
1507 | return scnprintf(msg, size, "The %s event is not supported.", | |
1508 | perf_evsel__name(evsel)); | |
1509 | case EMFILE: | |
1510 | return scnprintf(msg, size, "%s", | |
1511 | "Too many events are opened.\n" | |
1512 | "Try again after reducing the number of events."); | |
1513 | case ENODEV: | |
1514 | if (target->cpu_list) | |
1515 | return scnprintf(msg, size, "%s", | |
1516 | "No such device - did you specify an out-of-range profile CPU?\n"); | |
1517 | break; | |
1518 | case EOPNOTSUPP: | |
1519 | if (evsel->attr.precise_ip) | |
1520 | return scnprintf(msg, size, "%s", | |
1521 | "\'precise\' request may not be supported. Try removing 'p' modifier."); | |
1522 | #if defined(__i386__) || defined(__x86_64__) | |
1523 | if (evsel->attr.type == PERF_TYPE_HARDWARE) | |
1524 | return scnprintf(msg, size, "%s", | |
1525 | "No hardware sampling interrupt available.\n" | |
1526 | "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it."); | |
1527 | #endif | |
1528 | break; | |
1529 | default: | |
1530 | break; | |
1531 | } | |
1532 | ||
1533 | return scnprintf(msg, size, | |
1534 | "The sys_perf_event_open() syscall returned with %d (%s) for event (%s). \n" | |
1535 | "/bin/dmesg may provide additional information.\n" | |
1536 | "No CONFIG_PERF_EVENTS=y kernel support configured?\n", | |
1537 | err, strerror(err), perf_evsel__name(evsel)); | |
1538 | } |