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