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