]>
Commit | Line | Data |
---|---|---|
1 | // SPDX-License-Identifier: GPL-2.0 | |
2 | #include <errno.h> | |
3 | #include <signal.h> | |
4 | #include <inttypes.h> | |
5 | #include <linux/err.h> | |
6 | #include <linux/kernel.h> | |
7 | #include <linux/zalloc.h> | |
8 | #include <api/fs/fs.h> | |
9 | ||
10 | #include <byteswap.h> | |
11 | #include <unistd.h> | |
12 | #include <sys/types.h> | |
13 | #include <sys/mman.h> | |
14 | #include <perf/cpumap.h> | |
15 | ||
16 | #include "map_symbol.h" | |
17 | #include "branch.h" | |
18 | #include "debug.h" | |
19 | #include "env.h" | |
20 | #include "evlist.h" | |
21 | #include "evsel.h" | |
22 | #include "memswap.h" | |
23 | #include "map.h" | |
24 | #include "symbol.h" | |
25 | #include "session.h" | |
26 | #include "tool.h" | |
27 | #include "perf_regs.h" | |
28 | #include "asm/bug.h" | |
29 | #include "auxtrace.h" | |
30 | #include "thread.h" | |
31 | #include "thread-stack.h" | |
32 | #include "sample-raw.h" | |
33 | #include "stat.h" | |
34 | #include "tsc.h" | |
35 | #include "ui/progress.h" | |
36 | #include "util.h" | |
37 | #include "arch/common.h" | |
38 | #include "units.h" | |
39 | #include "annotate.h" | |
40 | #include "perf.h" | |
41 | #include <internal/lib.h> | |
42 | ||
43 | static int perf_session__deliver_event(struct perf_session *session, | |
44 | union perf_event *event, | |
45 | const struct perf_tool *tool, | |
46 | u64 file_offset, | |
47 | const char *file_path); | |
48 | ||
49 | static int perf_session__open(struct perf_session *session) | |
50 | { | |
51 | struct perf_data *data = session->data; | |
52 | ||
53 | if (perf_session__read_header(session) < 0) { | |
54 | pr_err("incompatible file format (rerun with -v to learn more)\n"); | |
55 | return -1; | |
56 | } | |
57 | ||
58 | if (perf_header__has_feat(&session->header, HEADER_AUXTRACE)) { | |
59 | /* Auxiliary events may reference exited threads, hold onto dead ones. */ | |
60 | symbol_conf.keep_exited_threads = true; | |
61 | } | |
62 | ||
63 | if (perf_data__is_pipe(data)) | |
64 | return 0; | |
65 | ||
66 | if (perf_header__has_feat(&session->header, HEADER_STAT)) | |
67 | return 0; | |
68 | ||
69 | if (!evlist__valid_sample_type(session->evlist)) { | |
70 | pr_err("non matching sample_type\n"); | |
71 | return -1; | |
72 | } | |
73 | ||
74 | if (!evlist__valid_sample_id_all(session->evlist)) { | |
75 | pr_err("non matching sample_id_all\n"); | |
76 | return -1; | |
77 | } | |
78 | ||
79 | if (!evlist__valid_read_format(session->evlist)) { | |
80 | pr_err("non matching read_format\n"); | |
81 | return -1; | |
82 | } | |
83 | ||
84 | return 0; | |
85 | } | |
86 | ||
87 | void perf_session__set_id_hdr_size(struct perf_session *session) | |
88 | { | |
89 | u16 id_hdr_size = evlist__id_hdr_size(session->evlist); | |
90 | ||
91 | machines__set_id_hdr_size(&session->machines, id_hdr_size); | |
92 | } | |
93 | ||
94 | int perf_session__create_kernel_maps(struct perf_session *session) | |
95 | { | |
96 | int ret = machine__create_kernel_maps(&session->machines.host); | |
97 | ||
98 | if (ret >= 0) | |
99 | ret = machines__create_guest_kernel_maps(&session->machines); | |
100 | return ret; | |
101 | } | |
102 | ||
103 | static void perf_session__destroy_kernel_maps(struct perf_session *session) | |
104 | { | |
105 | machines__destroy_kernel_maps(&session->machines); | |
106 | } | |
107 | ||
108 | static bool perf_session__has_comm_exec(struct perf_session *session) | |
109 | { | |
110 | struct evsel *evsel; | |
111 | ||
112 | evlist__for_each_entry(session->evlist, evsel) { | |
113 | if (evsel->core.attr.comm_exec) | |
114 | return true; | |
115 | } | |
116 | ||
117 | return false; | |
118 | } | |
119 | ||
120 | static void perf_session__set_comm_exec(struct perf_session *session) | |
121 | { | |
122 | bool comm_exec = perf_session__has_comm_exec(session); | |
123 | ||
124 | machines__set_comm_exec(&session->machines, comm_exec); | |
125 | } | |
126 | ||
127 | static int ordered_events__deliver_event(struct ordered_events *oe, | |
128 | struct ordered_event *event) | |
129 | { | |
130 | struct perf_session *session = container_of(oe, struct perf_session, | |
131 | ordered_events); | |
132 | ||
133 | return perf_session__deliver_event(session, event->event, | |
134 | session->tool, event->file_offset, | |
135 | event->file_path); | |
136 | } | |
137 | ||
138 | struct perf_session *__perf_session__new(struct perf_data *data, | |
139 | struct perf_tool *tool, | |
140 | bool trace_event_repipe) | |
141 | { | |
142 | int ret = -ENOMEM; | |
143 | struct perf_session *session = zalloc(sizeof(*session)); | |
144 | ||
145 | if (!session) | |
146 | goto out; | |
147 | ||
148 | session->trace_event_repipe = trace_event_repipe; | |
149 | session->tool = tool; | |
150 | session->decomp_data.zstd_decomp = &session->zstd_data; | |
151 | session->active_decomp = &session->decomp_data; | |
152 | INIT_LIST_HEAD(&session->auxtrace_index); | |
153 | machines__init(&session->machines); | |
154 | ordered_events__init(&session->ordered_events, | |
155 | ordered_events__deliver_event, NULL); | |
156 | ||
157 | perf_env__init(&session->header.env); | |
158 | if (data) { | |
159 | ret = perf_data__open(data); | |
160 | if (ret < 0) | |
161 | goto out_delete; | |
162 | ||
163 | session->data = data; | |
164 | ||
165 | if (perf_data__is_read(data)) { | |
166 | ret = perf_session__open(session); | |
167 | if (ret < 0) | |
168 | goto out_delete; | |
169 | ||
170 | /* | |
171 | * set session attributes that are present in perf.data | |
172 | * but not in pipe-mode. | |
173 | */ | |
174 | if (!data->is_pipe) { | |
175 | perf_session__set_id_hdr_size(session); | |
176 | perf_session__set_comm_exec(session); | |
177 | } | |
178 | ||
179 | evlist__init_trace_event_sample_raw(session->evlist); | |
180 | ||
181 | /* Open the directory data. */ | |
182 | if (data->is_dir) { | |
183 | ret = perf_data__open_dir(data); | |
184 | if (ret) | |
185 | goto out_delete; | |
186 | } | |
187 | ||
188 | if (!symbol_conf.kallsyms_name && | |
189 | !symbol_conf.vmlinux_name) | |
190 | symbol_conf.kallsyms_name = perf_data__kallsyms_name(data); | |
191 | } | |
192 | } else { | |
193 | session->machines.host.env = &perf_env; | |
194 | } | |
195 | ||
196 | session->machines.host.single_address_space = | |
197 | perf_env__single_address_space(session->machines.host.env); | |
198 | ||
199 | if (!data || perf_data__is_write(data)) { | |
200 | /* | |
201 | * In O_RDONLY mode this will be performed when reading the | |
202 | * kernel MMAP event, in perf_event__process_mmap(). | |
203 | */ | |
204 | if (perf_session__create_kernel_maps(session) < 0) | |
205 | pr_warning("Cannot read kernel map\n"); | |
206 | } | |
207 | ||
208 | /* | |
209 | * In pipe-mode, evlist is empty until PERF_RECORD_HEADER_ATTR is | |
210 | * processed, so evlist__sample_id_all is not meaningful here. | |
211 | */ | |
212 | if ((!data || !data->is_pipe) && tool && tool->ordering_requires_timestamps && | |
213 | tool->ordered_events && !evlist__sample_id_all(session->evlist)) { | |
214 | dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n"); | |
215 | tool->ordered_events = false; | |
216 | } | |
217 | ||
218 | return session; | |
219 | ||
220 | out_delete: | |
221 | perf_session__delete(session); | |
222 | out: | |
223 | return ERR_PTR(ret); | |
224 | } | |
225 | ||
226 | static void perf_decomp__release_events(struct decomp *next) | |
227 | { | |
228 | struct decomp *decomp; | |
229 | size_t mmap_len; | |
230 | ||
231 | do { | |
232 | decomp = next; | |
233 | if (decomp == NULL) | |
234 | break; | |
235 | next = decomp->next; | |
236 | mmap_len = decomp->mmap_len; | |
237 | munmap(decomp, mmap_len); | |
238 | } while (1); | |
239 | } | |
240 | ||
241 | void perf_session__delete(struct perf_session *session) | |
242 | { | |
243 | if (session == NULL) | |
244 | return; | |
245 | auxtrace__free(session); | |
246 | auxtrace_index__free(&session->auxtrace_index); | |
247 | debuginfo_cache__delete(); | |
248 | perf_session__destroy_kernel_maps(session); | |
249 | perf_decomp__release_events(session->decomp_data.decomp); | |
250 | perf_env__exit(&session->header.env); | |
251 | machines__exit(&session->machines); | |
252 | if (session->data) { | |
253 | if (perf_data__is_read(session->data)) | |
254 | evlist__delete(session->evlist); | |
255 | perf_data__close(session->data); | |
256 | } | |
257 | #ifdef HAVE_LIBTRACEEVENT | |
258 | trace_event__cleanup(&session->tevent); | |
259 | #endif | |
260 | free(session); | |
261 | } | |
262 | ||
263 | static void swap_sample_id_all(union perf_event *event, void *data) | |
264 | { | |
265 | void *end = (void *) event + event->header.size; | |
266 | int size = end - data; | |
267 | ||
268 | BUG_ON(size % sizeof(u64)); | |
269 | mem_bswap_64(data, size); | |
270 | } | |
271 | ||
272 | static void perf_event__all64_swap(union perf_event *event, | |
273 | bool sample_id_all __maybe_unused) | |
274 | { | |
275 | struct perf_event_header *hdr = &event->header; | |
276 | mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr)); | |
277 | } | |
278 | ||
279 | static void perf_event__comm_swap(union perf_event *event, bool sample_id_all) | |
280 | { | |
281 | event->comm.pid = bswap_32(event->comm.pid); | |
282 | event->comm.tid = bswap_32(event->comm.tid); | |
283 | ||
284 | if (sample_id_all) { | |
285 | void *data = &event->comm.comm; | |
286 | ||
287 | data += PERF_ALIGN(strlen(data) + 1, sizeof(u64)); | |
288 | swap_sample_id_all(event, data); | |
289 | } | |
290 | } | |
291 | ||
292 | static void perf_event__mmap_swap(union perf_event *event, | |
293 | bool sample_id_all) | |
294 | { | |
295 | event->mmap.pid = bswap_32(event->mmap.pid); | |
296 | event->mmap.tid = bswap_32(event->mmap.tid); | |
297 | event->mmap.start = bswap_64(event->mmap.start); | |
298 | event->mmap.len = bswap_64(event->mmap.len); | |
299 | event->mmap.pgoff = bswap_64(event->mmap.pgoff); | |
300 | ||
301 | if (sample_id_all) { | |
302 | void *data = &event->mmap.filename; | |
303 | ||
304 | data += PERF_ALIGN(strlen(data) + 1, sizeof(u64)); | |
305 | swap_sample_id_all(event, data); | |
306 | } | |
307 | } | |
308 | ||
309 | static void perf_event__mmap2_swap(union perf_event *event, | |
310 | bool sample_id_all) | |
311 | { | |
312 | event->mmap2.pid = bswap_32(event->mmap2.pid); | |
313 | event->mmap2.tid = bswap_32(event->mmap2.tid); | |
314 | event->mmap2.start = bswap_64(event->mmap2.start); | |
315 | event->mmap2.len = bswap_64(event->mmap2.len); | |
316 | event->mmap2.pgoff = bswap_64(event->mmap2.pgoff); | |
317 | ||
318 | if (!(event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID)) { | |
319 | event->mmap2.maj = bswap_32(event->mmap2.maj); | |
320 | event->mmap2.min = bswap_32(event->mmap2.min); | |
321 | event->mmap2.ino = bswap_64(event->mmap2.ino); | |
322 | event->mmap2.ino_generation = bswap_64(event->mmap2.ino_generation); | |
323 | } | |
324 | ||
325 | if (sample_id_all) { | |
326 | void *data = &event->mmap2.filename; | |
327 | ||
328 | data += PERF_ALIGN(strlen(data) + 1, sizeof(u64)); | |
329 | swap_sample_id_all(event, data); | |
330 | } | |
331 | } | |
332 | static void perf_event__task_swap(union perf_event *event, bool sample_id_all) | |
333 | { | |
334 | event->fork.pid = bswap_32(event->fork.pid); | |
335 | event->fork.tid = bswap_32(event->fork.tid); | |
336 | event->fork.ppid = bswap_32(event->fork.ppid); | |
337 | event->fork.ptid = bswap_32(event->fork.ptid); | |
338 | event->fork.time = bswap_64(event->fork.time); | |
339 | ||
340 | if (sample_id_all) | |
341 | swap_sample_id_all(event, &event->fork + 1); | |
342 | } | |
343 | ||
344 | static void perf_event__read_swap(union perf_event *event, bool sample_id_all) | |
345 | { | |
346 | event->read.pid = bswap_32(event->read.pid); | |
347 | event->read.tid = bswap_32(event->read.tid); | |
348 | event->read.value = bswap_64(event->read.value); | |
349 | event->read.time_enabled = bswap_64(event->read.time_enabled); | |
350 | event->read.time_running = bswap_64(event->read.time_running); | |
351 | event->read.id = bswap_64(event->read.id); | |
352 | ||
353 | if (sample_id_all) | |
354 | swap_sample_id_all(event, &event->read + 1); | |
355 | } | |
356 | ||
357 | static void perf_event__aux_swap(union perf_event *event, bool sample_id_all) | |
358 | { | |
359 | event->aux.aux_offset = bswap_64(event->aux.aux_offset); | |
360 | event->aux.aux_size = bswap_64(event->aux.aux_size); | |
361 | event->aux.flags = bswap_64(event->aux.flags); | |
362 | ||
363 | if (sample_id_all) | |
364 | swap_sample_id_all(event, &event->aux + 1); | |
365 | } | |
366 | ||
367 | static void perf_event__itrace_start_swap(union perf_event *event, | |
368 | bool sample_id_all) | |
369 | { | |
370 | event->itrace_start.pid = bswap_32(event->itrace_start.pid); | |
371 | event->itrace_start.tid = bswap_32(event->itrace_start.tid); | |
372 | ||
373 | if (sample_id_all) | |
374 | swap_sample_id_all(event, &event->itrace_start + 1); | |
375 | } | |
376 | ||
377 | static void perf_event__switch_swap(union perf_event *event, bool sample_id_all) | |
378 | { | |
379 | if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) { | |
380 | event->context_switch.next_prev_pid = | |
381 | bswap_32(event->context_switch.next_prev_pid); | |
382 | event->context_switch.next_prev_tid = | |
383 | bswap_32(event->context_switch.next_prev_tid); | |
384 | } | |
385 | ||
386 | if (sample_id_all) | |
387 | swap_sample_id_all(event, &event->context_switch + 1); | |
388 | } | |
389 | ||
390 | static void perf_event__text_poke_swap(union perf_event *event, bool sample_id_all) | |
391 | { | |
392 | event->text_poke.addr = bswap_64(event->text_poke.addr); | |
393 | event->text_poke.old_len = bswap_16(event->text_poke.old_len); | |
394 | event->text_poke.new_len = bswap_16(event->text_poke.new_len); | |
395 | ||
396 | if (sample_id_all) { | |
397 | size_t len = sizeof(event->text_poke.old_len) + | |
398 | sizeof(event->text_poke.new_len) + | |
399 | event->text_poke.old_len + | |
400 | event->text_poke.new_len; | |
401 | void *data = &event->text_poke.old_len; | |
402 | ||
403 | data += PERF_ALIGN(len, sizeof(u64)); | |
404 | swap_sample_id_all(event, data); | |
405 | } | |
406 | } | |
407 | ||
408 | static void perf_event__throttle_swap(union perf_event *event, | |
409 | bool sample_id_all) | |
410 | { | |
411 | event->throttle.time = bswap_64(event->throttle.time); | |
412 | event->throttle.id = bswap_64(event->throttle.id); | |
413 | event->throttle.stream_id = bswap_64(event->throttle.stream_id); | |
414 | ||
415 | if (sample_id_all) | |
416 | swap_sample_id_all(event, &event->throttle + 1); | |
417 | } | |
418 | ||
419 | static void perf_event__namespaces_swap(union perf_event *event, | |
420 | bool sample_id_all) | |
421 | { | |
422 | u64 i; | |
423 | ||
424 | event->namespaces.pid = bswap_32(event->namespaces.pid); | |
425 | event->namespaces.tid = bswap_32(event->namespaces.tid); | |
426 | event->namespaces.nr_namespaces = bswap_64(event->namespaces.nr_namespaces); | |
427 | ||
428 | for (i = 0; i < event->namespaces.nr_namespaces; i++) { | |
429 | struct perf_ns_link_info *ns = &event->namespaces.link_info[i]; | |
430 | ||
431 | ns->dev = bswap_64(ns->dev); | |
432 | ns->ino = bswap_64(ns->ino); | |
433 | } | |
434 | ||
435 | if (sample_id_all) | |
436 | swap_sample_id_all(event, &event->namespaces.link_info[i]); | |
437 | } | |
438 | ||
439 | static void perf_event__cgroup_swap(union perf_event *event, bool sample_id_all) | |
440 | { | |
441 | event->cgroup.id = bswap_64(event->cgroup.id); | |
442 | ||
443 | if (sample_id_all) { | |
444 | void *data = &event->cgroup.path; | |
445 | ||
446 | data += PERF_ALIGN(strlen(data) + 1, sizeof(u64)); | |
447 | swap_sample_id_all(event, data); | |
448 | } | |
449 | } | |
450 | ||
451 | static u8 revbyte(u8 b) | |
452 | { | |
453 | int rev = (b >> 4) | ((b & 0xf) << 4); | |
454 | rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2); | |
455 | rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1); | |
456 | return (u8) rev; | |
457 | } | |
458 | ||
459 | /* | |
460 | * XXX this is hack in attempt to carry flags bitfield | |
461 | * through endian village. ABI says: | |
462 | * | |
463 | * Bit-fields are allocated from right to left (least to most significant) | |
464 | * on little-endian implementations and from left to right (most to least | |
465 | * significant) on big-endian implementations. | |
466 | * | |
467 | * The above seems to be byte specific, so we need to reverse each | |
468 | * byte of the bitfield. 'Internet' also says this might be implementation | |
469 | * specific and we probably need proper fix and carry perf_event_attr | |
470 | * bitfield flags in separate data file FEAT_ section. Thought this seems | |
471 | * to work for now. | |
472 | */ | |
473 | static void swap_bitfield(u8 *p, unsigned len) | |
474 | { | |
475 | unsigned i; | |
476 | ||
477 | for (i = 0; i < len; i++) { | |
478 | *p = revbyte(*p); | |
479 | p++; | |
480 | } | |
481 | } | |
482 | ||
483 | /* exported for swapping attributes in file header */ | |
484 | void perf_event__attr_swap(struct perf_event_attr *attr) | |
485 | { | |
486 | attr->type = bswap_32(attr->type); | |
487 | attr->size = bswap_32(attr->size); | |
488 | ||
489 | #define bswap_safe(f, n) \ | |
490 | (attr->size > (offsetof(struct perf_event_attr, f) + \ | |
491 | sizeof(attr->f) * (n))) | |
492 | #define bswap_field(f, sz) \ | |
493 | do { \ | |
494 | if (bswap_safe(f, 0)) \ | |
495 | attr->f = bswap_##sz(attr->f); \ | |
496 | } while(0) | |
497 | #define bswap_field_16(f) bswap_field(f, 16) | |
498 | #define bswap_field_32(f) bswap_field(f, 32) | |
499 | #define bswap_field_64(f) bswap_field(f, 64) | |
500 | ||
501 | bswap_field_64(config); | |
502 | bswap_field_64(sample_period); | |
503 | bswap_field_64(sample_type); | |
504 | bswap_field_64(read_format); | |
505 | bswap_field_32(wakeup_events); | |
506 | bswap_field_32(bp_type); | |
507 | bswap_field_64(bp_addr); | |
508 | bswap_field_64(bp_len); | |
509 | bswap_field_64(branch_sample_type); | |
510 | bswap_field_64(sample_regs_user); | |
511 | bswap_field_32(sample_stack_user); | |
512 | bswap_field_32(aux_watermark); | |
513 | bswap_field_16(sample_max_stack); | |
514 | bswap_field_32(aux_sample_size); | |
515 | ||
516 | /* | |
517 | * After read_format are bitfields. Check read_format because | |
518 | * we are unable to use offsetof on bitfield. | |
519 | */ | |
520 | if (bswap_safe(read_format, 1)) | |
521 | swap_bitfield((u8 *) (&attr->read_format + 1), | |
522 | sizeof(u64)); | |
523 | #undef bswap_field_64 | |
524 | #undef bswap_field_32 | |
525 | #undef bswap_field | |
526 | #undef bswap_safe | |
527 | } | |
528 | ||
529 | static void perf_event__hdr_attr_swap(union perf_event *event, | |
530 | bool sample_id_all __maybe_unused) | |
531 | { | |
532 | size_t size; | |
533 | ||
534 | perf_event__attr_swap(&event->attr.attr); | |
535 | ||
536 | size = event->header.size; | |
537 | size -= perf_record_header_attr_id(event) - (void *)event; | |
538 | mem_bswap_64(perf_record_header_attr_id(event), size); | |
539 | } | |
540 | ||
541 | static void perf_event__event_update_swap(union perf_event *event, | |
542 | bool sample_id_all __maybe_unused) | |
543 | { | |
544 | event->event_update.type = bswap_64(event->event_update.type); | |
545 | event->event_update.id = bswap_64(event->event_update.id); | |
546 | } | |
547 | ||
548 | static void perf_event__event_type_swap(union perf_event *event, | |
549 | bool sample_id_all __maybe_unused) | |
550 | { | |
551 | event->event_type.event_type.event_id = | |
552 | bswap_64(event->event_type.event_type.event_id); | |
553 | } | |
554 | ||
555 | static void perf_event__tracing_data_swap(union perf_event *event, | |
556 | bool sample_id_all __maybe_unused) | |
557 | { | |
558 | event->tracing_data.size = bswap_32(event->tracing_data.size); | |
559 | } | |
560 | ||
561 | static void perf_event__auxtrace_info_swap(union perf_event *event, | |
562 | bool sample_id_all __maybe_unused) | |
563 | { | |
564 | size_t size; | |
565 | ||
566 | event->auxtrace_info.type = bswap_32(event->auxtrace_info.type); | |
567 | ||
568 | size = event->header.size; | |
569 | size -= (void *)&event->auxtrace_info.priv - (void *)event; | |
570 | mem_bswap_64(event->auxtrace_info.priv, size); | |
571 | } | |
572 | ||
573 | static void perf_event__auxtrace_swap(union perf_event *event, | |
574 | bool sample_id_all __maybe_unused) | |
575 | { | |
576 | event->auxtrace.size = bswap_64(event->auxtrace.size); | |
577 | event->auxtrace.offset = bswap_64(event->auxtrace.offset); | |
578 | event->auxtrace.reference = bswap_64(event->auxtrace.reference); | |
579 | event->auxtrace.idx = bswap_32(event->auxtrace.idx); | |
580 | event->auxtrace.tid = bswap_32(event->auxtrace.tid); | |
581 | event->auxtrace.cpu = bswap_32(event->auxtrace.cpu); | |
582 | } | |
583 | ||
584 | static void perf_event__auxtrace_error_swap(union perf_event *event, | |
585 | bool sample_id_all __maybe_unused) | |
586 | { | |
587 | event->auxtrace_error.type = bswap_32(event->auxtrace_error.type); | |
588 | event->auxtrace_error.code = bswap_32(event->auxtrace_error.code); | |
589 | event->auxtrace_error.cpu = bswap_32(event->auxtrace_error.cpu); | |
590 | event->auxtrace_error.pid = bswap_32(event->auxtrace_error.pid); | |
591 | event->auxtrace_error.tid = bswap_32(event->auxtrace_error.tid); | |
592 | event->auxtrace_error.fmt = bswap_32(event->auxtrace_error.fmt); | |
593 | event->auxtrace_error.ip = bswap_64(event->auxtrace_error.ip); | |
594 | if (event->auxtrace_error.fmt) | |
595 | event->auxtrace_error.time = bswap_64(event->auxtrace_error.time); | |
596 | if (event->auxtrace_error.fmt >= 2) { | |
597 | event->auxtrace_error.machine_pid = bswap_32(event->auxtrace_error.machine_pid); | |
598 | event->auxtrace_error.vcpu = bswap_32(event->auxtrace_error.vcpu); | |
599 | } | |
600 | } | |
601 | ||
602 | static void perf_event__thread_map_swap(union perf_event *event, | |
603 | bool sample_id_all __maybe_unused) | |
604 | { | |
605 | unsigned i; | |
606 | ||
607 | event->thread_map.nr = bswap_64(event->thread_map.nr); | |
608 | ||
609 | for (i = 0; i < event->thread_map.nr; i++) | |
610 | event->thread_map.entries[i].pid = bswap_64(event->thread_map.entries[i].pid); | |
611 | } | |
612 | ||
613 | static void perf_event__cpu_map_swap(union perf_event *event, | |
614 | bool sample_id_all __maybe_unused) | |
615 | { | |
616 | struct perf_record_cpu_map_data *data = &event->cpu_map.data; | |
617 | ||
618 | data->type = bswap_16(data->type); | |
619 | ||
620 | switch (data->type) { | |
621 | case PERF_CPU_MAP__CPUS: | |
622 | data->cpus_data.nr = bswap_16(data->cpus_data.nr); | |
623 | ||
624 | for (unsigned i = 0; i < data->cpus_data.nr; i++) | |
625 | data->cpus_data.cpu[i] = bswap_16(data->cpus_data.cpu[i]); | |
626 | break; | |
627 | case PERF_CPU_MAP__MASK: | |
628 | data->mask32_data.long_size = bswap_16(data->mask32_data.long_size); | |
629 | ||
630 | switch (data->mask32_data.long_size) { | |
631 | case 4: | |
632 | data->mask32_data.nr = bswap_16(data->mask32_data.nr); | |
633 | for (unsigned i = 0; i < data->mask32_data.nr; i++) | |
634 | data->mask32_data.mask[i] = bswap_32(data->mask32_data.mask[i]); | |
635 | break; | |
636 | case 8: | |
637 | data->mask64_data.nr = bswap_16(data->mask64_data.nr); | |
638 | for (unsigned i = 0; i < data->mask64_data.nr; i++) | |
639 | data->mask64_data.mask[i] = bswap_64(data->mask64_data.mask[i]); | |
640 | break; | |
641 | default: | |
642 | pr_err("cpu_map swap: unsupported long size\n"); | |
643 | } | |
644 | break; | |
645 | case PERF_CPU_MAP__RANGE_CPUS: | |
646 | data->range_cpu_data.start_cpu = bswap_16(data->range_cpu_data.start_cpu); | |
647 | data->range_cpu_data.end_cpu = bswap_16(data->range_cpu_data.end_cpu); | |
648 | break; | |
649 | default: | |
650 | break; | |
651 | } | |
652 | } | |
653 | ||
654 | static void perf_event__stat_config_swap(union perf_event *event, | |
655 | bool sample_id_all __maybe_unused) | |
656 | { | |
657 | u64 size; | |
658 | ||
659 | size = bswap_64(event->stat_config.nr) * sizeof(event->stat_config.data[0]); | |
660 | size += 1; /* nr item itself */ | |
661 | mem_bswap_64(&event->stat_config.nr, size); | |
662 | } | |
663 | ||
664 | static void perf_event__stat_swap(union perf_event *event, | |
665 | bool sample_id_all __maybe_unused) | |
666 | { | |
667 | event->stat.id = bswap_64(event->stat.id); | |
668 | event->stat.thread = bswap_32(event->stat.thread); | |
669 | event->stat.cpu = bswap_32(event->stat.cpu); | |
670 | event->stat.val = bswap_64(event->stat.val); | |
671 | event->stat.ena = bswap_64(event->stat.ena); | |
672 | event->stat.run = bswap_64(event->stat.run); | |
673 | } | |
674 | ||
675 | static void perf_event__stat_round_swap(union perf_event *event, | |
676 | bool sample_id_all __maybe_unused) | |
677 | { | |
678 | event->stat_round.type = bswap_64(event->stat_round.type); | |
679 | event->stat_round.time = bswap_64(event->stat_round.time); | |
680 | } | |
681 | ||
682 | static void perf_event__time_conv_swap(union perf_event *event, | |
683 | bool sample_id_all __maybe_unused) | |
684 | { | |
685 | event->time_conv.time_shift = bswap_64(event->time_conv.time_shift); | |
686 | event->time_conv.time_mult = bswap_64(event->time_conv.time_mult); | |
687 | event->time_conv.time_zero = bswap_64(event->time_conv.time_zero); | |
688 | ||
689 | if (event_contains(event->time_conv, time_cycles)) { | |
690 | event->time_conv.time_cycles = bswap_64(event->time_conv.time_cycles); | |
691 | event->time_conv.time_mask = bswap_64(event->time_conv.time_mask); | |
692 | } | |
693 | } | |
694 | ||
695 | typedef void (*perf_event__swap_op)(union perf_event *event, | |
696 | bool sample_id_all); | |
697 | ||
698 | static perf_event__swap_op perf_event__swap_ops[] = { | |
699 | [PERF_RECORD_MMAP] = perf_event__mmap_swap, | |
700 | [PERF_RECORD_MMAP2] = perf_event__mmap2_swap, | |
701 | [PERF_RECORD_COMM] = perf_event__comm_swap, | |
702 | [PERF_RECORD_FORK] = perf_event__task_swap, | |
703 | [PERF_RECORD_EXIT] = perf_event__task_swap, | |
704 | [PERF_RECORD_LOST] = perf_event__all64_swap, | |
705 | [PERF_RECORD_READ] = perf_event__read_swap, | |
706 | [PERF_RECORD_THROTTLE] = perf_event__throttle_swap, | |
707 | [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap, | |
708 | [PERF_RECORD_SAMPLE] = perf_event__all64_swap, | |
709 | [PERF_RECORD_AUX] = perf_event__aux_swap, | |
710 | [PERF_RECORD_ITRACE_START] = perf_event__itrace_start_swap, | |
711 | [PERF_RECORD_LOST_SAMPLES] = perf_event__all64_swap, | |
712 | [PERF_RECORD_SWITCH] = perf_event__switch_swap, | |
713 | [PERF_RECORD_SWITCH_CPU_WIDE] = perf_event__switch_swap, | |
714 | [PERF_RECORD_NAMESPACES] = perf_event__namespaces_swap, | |
715 | [PERF_RECORD_CGROUP] = perf_event__cgroup_swap, | |
716 | [PERF_RECORD_TEXT_POKE] = perf_event__text_poke_swap, | |
717 | [PERF_RECORD_AUX_OUTPUT_HW_ID] = perf_event__all64_swap, | |
718 | [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap, | |
719 | [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap, | |
720 | [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap, | |
721 | [PERF_RECORD_HEADER_BUILD_ID] = NULL, | |
722 | [PERF_RECORD_ID_INDEX] = perf_event__all64_swap, | |
723 | [PERF_RECORD_AUXTRACE_INFO] = perf_event__auxtrace_info_swap, | |
724 | [PERF_RECORD_AUXTRACE] = perf_event__auxtrace_swap, | |
725 | [PERF_RECORD_AUXTRACE_ERROR] = perf_event__auxtrace_error_swap, | |
726 | [PERF_RECORD_THREAD_MAP] = perf_event__thread_map_swap, | |
727 | [PERF_RECORD_CPU_MAP] = perf_event__cpu_map_swap, | |
728 | [PERF_RECORD_STAT_CONFIG] = perf_event__stat_config_swap, | |
729 | [PERF_RECORD_STAT] = perf_event__stat_swap, | |
730 | [PERF_RECORD_STAT_ROUND] = perf_event__stat_round_swap, | |
731 | [PERF_RECORD_EVENT_UPDATE] = perf_event__event_update_swap, | |
732 | [PERF_RECORD_TIME_CONV] = perf_event__time_conv_swap, | |
733 | [PERF_RECORD_HEADER_MAX] = NULL, | |
734 | }; | |
735 | ||
736 | /* | |
737 | * When perf record finishes a pass on every buffers, it records this pseudo | |
738 | * event. | |
739 | * We record the max timestamp t found in the pass n. | |
740 | * Assuming these timestamps are monotonic across cpus, we know that if | |
741 | * a buffer still has events with timestamps below t, they will be all | |
742 | * available and then read in the pass n + 1. | |
743 | * Hence when we start to read the pass n + 2, we can safely flush every | |
744 | * events with timestamps below t. | |
745 | * | |
746 | * ============ PASS n ================= | |
747 | * CPU 0 | CPU 1 | |
748 | * | | |
749 | * cnt1 timestamps | cnt2 timestamps | |
750 | * 1 | 2 | |
751 | * 2 | 3 | |
752 | * - | 4 <--- max recorded | |
753 | * | |
754 | * ============ PASS n + 1 ============== | |
755 | * CPU 0 | CPU 1 | |
756 | * | | |
757 | * cnt1 timestamps | cnt2 timestamps | |
758 | * 3 | 5 | |
759 | * 4 | 6 | |
760 | * 5 | 7 <---- max recorded | |
761 | * | |
762 | * Flush every events below timestamp 4 | |
763 | * | |
764 | * ============ PASS n + 2 ============== | |
765 | * CPU 0 | CPU 1 | |
766 | * | | |
767 | * cnt1 timestamps | cnt2 timestamps | |
768 | * 6 | 8 | |
769 | * 7 | 9 | |
770 | * - | 10 | |
771 | * | |
772 | * Flush every events below timestamp 7 | |
773 | * etc... | |
774 | */ | |
775 | int perf_event__process_finished_round(const struct perf_tool *tool __maybe_unused, | |
776 | union perf_event *event __maybe_unused, | |
777 | struct ordered_events *oe) | |
778 | { | |
779 | if (dump_trace) | |
780 | fprintf(stdout, "\n"); | |
781 | return ordered_events__flush(oe, OE_FLUSH__ROUND); | |
782 | } | |
783 | ||
784 | int perf_session__queue_event(struct perf_session *s, union perf_event *event, | |
785 | u64 timestamp, u64 file_offset, const char *file_path) | |
786 | { | |
787 | return ordered_events__queue(&s->ordered_events, event, timestamp, file_offset, file_path); | |
788 | } | |
789 | ||
790 | static void callchain__lbr_callstack_printf(struct perf_sample *sample) | |
791 | { | |
792 | struct ip_callchain *callchain = sample->callchain; | |
793 | struct branch_stack *lbr_stack = sample->branch_stack; | |
794 | struct branch_entry *entries = perf_sample__branch_entries(sample); | |
795 | u64 kernel_callchain_nr = callchain->nr; | |
796 | unsigned int i; | |
797 | ||
798 | for (i = 0; i < kernel_callchain_nr; i++) { | |
799 | if (callchain->ips[i] == PERF_CONTEXT_USER) | |
800 | break; | |
801 | } | |
802 | ||
803 | if ((i != kernel_callchain_nr) && lbr_stack->nr) { | |
804 | u64 total_nr; | |
805 | /* | |
806 | * LBR callstack can only get user call chain, | |
807 | * i is kernel call chain number, | |
808 | * 1 is PERF_CONTEXT_USER. | |
809 | * | |
810 | * The user call chain is stored in LBR registers. | |
811 | * LBR are pair registers. The caller is stored | |
812 | * in "from" register, while the callee is stored | |
813 | * in "to" register. | |
814 | * For example, there is a call stack | |
815 | * "A"->"B"->"C"->"D". | |
816 | * The LBR registers will be recorded like | |
817 | * "C"->"D", "B"->"C", "A"->"B". | |
818 | * So only the first "to" register and all "from" | |
819 | * registers are needed to construct the whole stack. | |
820 | */ | |
821 | total_nr = i + 1 + lbr_stack->nr + 1; | |
822 | kernel_callchain_nr = i + 1; | |
823 | ||
824 | printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr); | |
825 | ||
826 | for (i = 0; i < kernel_callchain_nr; i++) | |
827 | printf("..... %2d: %016" PRIx64 "\n", | |
828 | i, callchain->ips[i]); | |
829 | ||
830 | printf("..... %2d: %016" PRIx64 "\n", | |
831 | (int)(kernel_callchain_nr), entries[0].to); | |
832 | for (i = 0; i < lbr_stack->nr; i++) | |
833 | printf("..... %2d: %016" PRIx64 "\n", | |
834 | (int)(i + kernel_callchain_nr + 1), entries[i].from); | |
835 | } | |
836 | } | |
837 | ||
838 | static void callchain__printf(struct evsel *evsel, | |
839 | struct perf_sample *sample) | |
840 | { | |
841 | unsigned int i; | |
842 | struct ip_callchain *callchain = sample->callchain; | |
843 | ||
844 | if (evsel__has_branch_callstack(evsel)) | |
845 | callchain__lbr_callstack_printf(sample); | |
846 | ||
847 | printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr); | |
848 | ||
849 | for (i = 0; i < callchain->nr; i++) | |
850 | printf("..... %2d: %016" PRIx64 "\n", | |
851 | i, callchain->ips[i]); | |
852 | } | |
853 | ||
854 | static void branch_stack__printf(struct perf_sample *sample, | |
855 | struct evsel *evsel) | |
856 | { | |
857 | struct branch_entry *entries = perf_sample__branch_entries(sample); | |
858 | bool callstack = evsel__has_branch_callstack(evsel); | |
859 | u64 *branch_stack_cntr = sample->branch_stack_cntr; | |
860 | uint64_t i; | |
861 | ||
862 | if (!callstack) { | |
863 | printf("%s: nr:%" PRIu64 "\n", "... branch stack", sample->branch_stack->nr); | |
864 | } else { | |
865 | /* the reason of adding 1 to nr is because after expanding | |
866 | * branch stack it generates nr + 1 callstack records. e.g., | |
867 | * B()->C() | |
868 | * A()->B() | |
869 | * the final callstack should be: | |
870 | * C() | |
871 | * B() | |
872 | * A() | |
873 | */ | |
874 | printf("%s: nr:%" PRIu64 "\n", "... branch callstack", sample->branch_stack->nr+1); | |
875 | } | |
876 | ||
877 | for (i = 0; i < sample->branch_stack->nr; i++) { | |
878 | struct branch_entry *e = &entries[i]; | |
879 | ||
880 | if (!callstack) { | |
881 | printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 " %hu cycles %s%s%s%s %x %s %s\n", | |
882 | i, e->from, e->to, | |
883 | (unsigned short)e->flags.cycles, | |
884 | e->flags.mispred ? "M" : " ", | |
885 | e->flags.predicted ? "P" : " ", | |
886 | e->flags.abort ? "A" : " ", | |
887 | e->flags.in_tx ? "T" : " ", | |
888 | (unsigned)e->flags.reserved, | |
889 | get_branch_type(e), | |
890 | e->flags.spec ? branch_spec_desc(e->flags.spec) : ""); | |
891 | } else { | |
892 | if (i == 0) { | |
893 | printf("..... %2"PRIu64": %016" PRIx64 "\n" | |
894 | "..... %2"PRIu64": %016" PRIx64 "\n", | |
895 | i, e->to, i+1, e->from); | |
896 | } else { | |
897 | printf("..... %2"PRIu64": %016" PRIx64 "\n", i+1, e->from); | |
898 | } | |
899 | } | |
900 | } | |
901 | ||
902 | if (branch_stack_cntr) { | |
903 | unsigned int br_cntr_width, br_cntr_nr; | |
904 | ||
905 | perf_env__find_br_cntr_info(evsel__env(evsel), &br_cntr_nr, &br_cntr_width); | |
906 | printf("... branch stack counters: nr:%" PRIu64 " (counter width: %u max counter nr:%u)\n", | |
907 | sample->branch_stack->nr, br_cntr_width, br_cntr_nr); | |
908 | for (i = 0; i < sample->branch_stack->nr; i++) | |
909 | printf("..... %2"PRIu64": %016" PRIx64 "\n", i, branch_stack_cntr[i]); | |
910 | } | |
911 | } | |
912 | ||
913 | static void regs_dump__printf(u64 mask, u64 *regs, const char *arch) | |
914 | { | |
915 | unsigned rid, i = 0; | |
916 | ||
917 | for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) { | |
918 | u64 val = regs[i++]; | |
919 | ||
920 | printf(".... %-5s 0x%016" PRIx64 "\n", | |
921 | perf_reg_name(rid, arch), val); | |
922 | } | |
923 | } | |
924 | ||
925 | static const char *regs_abi[] = { | |
926 | [PERF_SAMPLE_REGS_ABI_NONE] = "none", | |
927 | [PERF_SAMPLE_REGS_ABI_32] = "32-bit", | |
928 | [PERF_SAMPLE_REGS_ABI_64] = "64-bit", | |
929 | }; | |
930 | ||
931 | static inline const char *regs_dump_abi(struct regs_dump *d) | |
932 | { | |
933 | if (d->abi > PERF_SAMPLE_REGS_ABI_64) | |
934 | return "unknown"; | |
935 | ||
936 | return regs_abi[d->abi]; | |
937 | } | |
938 | ||
939 | static void regs__printf(const char *type, struct regs_dump *regs, const char *arch) | |
940 | { | |
941 | u64 mask = regs->mask; | |
942 | ||
943 | printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n", | |
944 | type, | |
945 | mask, | |
946 | regs_dump_abi(regs)); | |
947 | ||
948 | regs_dump__printf(mask, regs->regs, arch); | |
949 | } | |
950 | ||
951 | static void regs_user__printf(struct perf_sample *sample, const char *arch) | |
952 | { | |
953 | struct regs_dump *user_regs; | |
954 | ||
955 | if (!sample->user_regs) | |
956 | return; | |
957 | ||
958 | user_regs = perf_sample__user_regs(sample); | |
959 | ||
960 | if (user_regs->regs) | |
961 | regs__printf("user", user_regs, arch); | |
962 | } | |
963 | ||
964 | static void regs_intr__printf(struct perf_sample *sample, const char *arch) | |
965 | { | |
966 | struct regs_dump *intr_regs; | |
967 | ||
968 | if (!sample->intr_regs) | |
969 | return; | |
970 | ||
971 | intr_regs = perf_sample__intr_regs(sample); | |
972 | ||
973 | if (intr_regs->regs) | |
974 | regs__printf("intr", intr_regs, arch); | |
975 | } | |
976 | ||
977 | static void stack_user__printf(struct stack_dump *dump) | |
978 | { | |
979 | printf("... ustack: size %" PRIu64 ", offset 0x%x\n", | |
980 | dump->size, dump->offset); | |
981 | } | |
982 | ||
983 | static void evlist__print_tstamp(struct evlist *evlist, union perf_event *event, struct perf_sample *sample) | |
984 | { | |
985 | u64 sample_type = __evlist__combined_sample_type(evlist); | |
986 | ||
987 | if (event->header.type != PERF_RECORD_SAMPLE && | |
988 | !evlist__sample_id_all(evlist)) { | |
989 | fputs("-1 -1 ", stdout); | |
990 | return; | |
991 | } | |
992 | ||
993 | if ((sample_type & PERF_SAMPLE_CPU)) | |
994 | printf("%u ", sample->cpu); | |
995 | ||
996 | if (sample_type & PERF_SAMPLE_TIME) | |
997 | printf("%" PRIu64 " ", sample->time); | |
998 | } | |
999 | ||
1000 | static void sample_read__printf(struct perf_sample *sample, u64 read_format) | |
1001 | { | |
1002 | printf("... sample_read:\n"); | |
1003 | ||
1004 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
1005 | printf("...... time enabled %016" PRIx64 "\n", | |
1006 | sample->read.time_enabled); | |
1007 | ||
1008 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
1009 | printf("...... time running %016" PRIx64 "\n", | |
1010 | sample->read.time_running); | |
1011 | ||
1012 | if (read_format & PERF_FORMAT_GROUP) { | |
1013 | struct sample_read_value *value = sample->read.group.values; | |
1014 | ||
1015 | printf(".... group nr %" PRIu64 "\n", sample->read.group.nr); | |
1016 | ||
1017 | sample_read_group__for_each(value, sample->read.group.nr, read_format) { | |
1018 | printf("..... id %016" PRIx64 | |
1019 | ", value %016" PRIx64, | |
1020 | value->id, value->value); | |
1021 | if (read_format & PERF_FORMAT_LOST) | |
1022 | printf(", lost %" PRIu64, value->lost); | |
1023 | printf("\n"); | |
1024 | } | |
1025 | } else { | |
1026 | printf("..... id %016" PRIx64 ", value %016" PRIx64, | |
1027 | sample->read.one.id, sample->read.one.value); | |
1028 | if (read_format & PERF_FORMAT_LOST) | |
1029 | printf(", lost %" PRIu64, sample->read.one.lost); | |
1030 | printf("\n"); | |
1031 | } | |
1032 | } | |
1033 | ||
1034 | static void dump_event(struct evlist *evlist, union perf_event *event, | |
1035 | u64 file_offset, struct perf_sample *sample, | |
1036 | const char *file_path) | |
1037 | { | |
1038 | if (!dump_trace) | |
1039 | return; | |
1040 | ||
1041 | printf("\n%#" PRIx64 "@%s [%#x]: event: %d\n", | |
1042 | file_offset, file_path, event->header.size, event->header.type); | |
1043 | ||
1044 | trace_event(event); | |
1045 | if (event->header.type == PERF_RECORD_SAMPLE && evlist->trace_event_sample_raw) | |
1046 | evlist->trace_event_sample_raw(evlist, event, sample); | |
1047 | ||
1048 | if (sample) | |
1049 | evlist__print_tstamp(evlist, event, sample); | |
1050 | ||
1051 | printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset, | |
1052 | event->header.size, perf_event__name(event->header.type)); | |
1053 | } | |
1054 | ||
1055 | char *get_page_size_name(u64 size, char *str) | |
1056 | { | |
1057 | if (!size || !unit_number__scnprintf(str, PAGE_SIZE_NAME_LEN, size)) | |
1058 | snprintf(str, PAGE_SIZE_NAME_LEN, "%s", "N/A"); | |
1059 | ||
1060 | return str; | |
1061 | } | |
1062 | ||
1063 | static void dump_sample(struct evsel *evsel, union perf_event *event, | |
1064 | struct perf_sample *sample, const char *arch) | |
1065 | { | |
1066 | u64 sample_type; | |
1067 | char str[PAGE_SIZE_NAME_LEN]; | |
1068 | ||
1069 | if (!dump_trace) | |
1070 | return; | |
1071 | ||
1072 | printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n", | |
1073 | event->header.misc, sample->pid, sample->tid, sample->ip, | |
1074 | sample->period, sample->addr); | |
1075 | ||
1076 | sample_type = evsel->core.attr.sample_type; | |
1077 | ||
1078 | if (evsel__has_callchain(evsel)) | |
1079 | callchain__printf(evsel, sample); | |
1080 | ||
1081 | if (evsel__has_br_stack(evsel)) | |
1082 | branch_stack__printf(sample, evsel); | |
1083 | ||
1084 | if (sample_type & PERF_SAMPLE_REGS_USER) | |
1085 | regs_user__printf(sample, arch); | |
1086 | ||
1087 | if (sample_type & PERF_SAMPLE_REGS_INTR) | |
1088 | regs_intr__printf(sample, arch); | |
1089 | ||
1090 | if (sample_type & PERF_SAMPLE_STACK_USER) | |
1091 | stack_user__printf(&sample->user_stack); | |
1092 | ||
1093 | if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) { | |
1094 | printf("... weight: %" PRIu64 "", sample->weight); | |
1095 | if (sample_type & PERF_SAMPLE_WEIGHT_STRUCT) { | |
1096 | printf(",0x%"PRIx16"", sample->ins_lat); | |
1097 | printf(",0x%"PRIx16"", sample->p_stage_cyc); | |
1098 | } | |
1099 | printf("\n"); | |
1100 | } | |
1101 | ||
1102 | if (sample_type & PERF_SAMPLE_DATA_SRC) | |
1103 | printf(" . data_src: 0x%"PRIx64"\n", sample->data_src); | |
1104 | ||
1105 | if (sample_type & PERF_SAMPLE_PHYS_ADDR) | |
1106 | printf(" .. phys_addr: 0x%"PRIx64"\n", sample->phys_addr); | |
1107 | ||
1108 | if (sample_type & PERF_SAMPLE_DATA_PAGE_SIZE) | |
1109 | printf(" .. data page size: %s\n", get_page_size_name(sample->data_page_size, str)); | |
1110 | ||
1111 | if (sample_type & PERF_SAMPLE_CODE_PAGE_SIZE) | |
1112 | printf(" .. code page size: %s\n", get_page_size_name(sample->code_page_size, str)); | |
1113 | ||
1114 | if (sample_type & PERF_SAMPLE_TRANSACTION) | |
1115 | printf("... transaction: %" PRIx64 "\n", sample->transaction); | |
1116 | ||
1117 | if (sample_type & PERF_SAMPLE_READ) | |
1118 | sample_read__printf(sample, evsel->core.attr.read_format); | |
1119 | } | |
1120 | ||
1121 | static void dump_read(struct evsel *evsel, union perf_event *event) | |
1122 | { | |
1123 | struct perf_record_read *read_event = &event->read; | |
1124 | u64 read_format; | |
1125 | ||
1126 | if (!dump_trace) | |
1127 | return; | |
1128 | ||
1129 | printf(": %d %d %s %" PRI_lu64 "\n", event->read.pid, event->read.tid, | |
1130 | evsel__name(evsel), event->read.value); | |
1131 | ||
1132 | if (!evsel) | |
1133 | return; | |
1134 | ||
1135 | read_format = evsel->core.attr.read_format; | |
1136 | ||
1137 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
1138 | printf("... time enabled : %" PRI_lu64 "\n", read_event->time_enabled); | |
1139 | ||
1140 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
1141 | printf("... time running : %" PRI_lu64 "\n", read_event->time_running); | |
1142 | ||
1143 | if (read_format & PERF_FORMAT_ID) | |
1144 | printf("... id : %" PRI_lu64 "\n", read_event->id); | |
1145 | ||
1146 | if (read_format & PERF_FORMAT_LOST) | |
1147 | printf("... lost : %" PRI_lu64 "\n", read_event->lost); | |
1148 | } | |
1149 | ||
1150 | static struct machine *machines__find_for_cpumode(struct machines *machines, | |
1151 | union perf_event *event, | |
1152 | struct perf_sample *sample) | |
1153 | { | |
1154 | if (perf_guest && | |
1155 | ((sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL) || | |
1156 | (sample->cpumode == PERF_RECORD_MISC_GUEST_USER))) { | |
1157 | u32 pid; | |
1158 | ||
1159 | if (sample->machine_pid) | |
1160 | pid = sample->machine_pid; | |
1161 | else if (event->header.type == PERF_RECORD_MMAP | |
1162 | || event->header.type == PERF_RECORD_MMAP2) | |
1163 | pid = event->mmap.pid; | |
1164 | else | |
1165 | pid = sample->pid; | |
1166 | ||
1167 | /* | |
1168 | * Guest code machine is created as needed and does not use | |
1169 | * DEFAULT_GUEST_KERNEL_ID. | |
1170 | */ | |
1171 | if (symbol_conf.guest_code) | |
1172 | return machines__findnew(machines, pid); | |
1173 | ||
1174 | return machines__find_guest(machines, pid); | |
1175 | } | |
1176 | ||
1177 | return &machines->host; | |
1178 | } | |
1179 | ||
1180 | static int deliver_sample_value(struct evlist *evlist, | |
1181 | const struct perf_tool *tool, | |
1182 | union perf_event *event, | |
1183 | struct perf_sample *sample, | |
1184 | struct sample_read_value *v, | |
1185 | struct machine *machine, | |
1186 | bool per_thread) | |
1187 | { | |
1188 | struct perf_sample_id *sid = evlist__id2sid(evlist, v->id); | |
1189 | struct evsel *evsel; | |
1190 | u64 *storage = NULL; | |
1191 | ||
1192 | if (sid) { | |
1193 | storage = perf_sample_id__get_period_storage(sid, sample->tid, per_thread); | |
1194 | } | |
1195 | ||
1196 | if (storage) { | |
1197 | sample->id = v->id; | |
1198 | sample->period = v->value - *storage; | |
1199 | *storage = v->value; | |
1200 | } | |
1201 | ||
1202 | if (!storage || sid->evsel == NULL) { | |
1203 | ++evlist->stats.nr_unknown_id; | |
1204 | return 0; | |
1205 | } | |
1206 | ||
1207 | /* | |
1208 | * There's no reason to deliver sample | |
1209 | * for zero period, bail out. | |
1210 | */ | |
1211 | if (!sample->period) | |
1212 | return 0; | |
1213 | ||
1214 | evsel = container_of(sid->evsel, struct evsel, core); | |
1215 | return tool->sample(tool, event, sample, evsel, machine); | |
1216 | } | |
1217 | ||
1218 | static int deliver_sample_group(struct evlist *evlist, | |
1219 | const struct perf_tool *tool, | |
1220 | union perf_event *event, | |
1221 | struct perf_sample *sample, | |
1222 | struct machine *machine, | |
1223 | u64 read_format, | |
1224 | bool per_thread) | |
1225 | { | |
1226 | int ret = -EINVAL; | |
1227 | struct sample_read_value *v = sample->read.group.values; | |
1228 | ||
1229 | if (tool->dont_split_sample_group) | |
1230 | return deliver_sample_value(evlist, tool, event, sample, v, machine, | |
1231 | per_thread); | |
1232 | ||
1233 | sample_read_group__for_each(v, sample->read.group.nr, read_format) { | |
1234 | ret = deliver_sample_value(evlist, tool, event, sample, v, | |
1235 | machine, per_thread); | |
1236 | if (ret) | |
1237 | break; | |
1238 | } | |
1239 | ||
1240 | return ret; | |
1241 | } | |
1242 | ||
1243 | static int evlist__deliver_sample(struct evlist *evlist, const struct perf_tool *tool, | |
1244 | union perf_event *event, struct perf_sample *sample, | |
1245 | struct evsel *evsel, struct machine *machine) | |
1246 | { | |
1247 | /* We know evsel != NULL. */ | |
1248 | u64 sample_type = evsel->core.attr.sample_type; | |
1249 | u64 read_format = evsel->core.attr.read_format; | |
1250 | bool per_thread = perf_evsel__attr_has_per_thread_sample_period(&evsel->core); | |
1251 | ||
1252 | /* Standard sample delivery. */ | |
1253 | if (!(sample_type & PERF_SAMPLE_READ)) | |
1254 | return tool->sample(tool, event, sample, evsel, machine); | |
1255 | ||
1256 | /* For PERF_SAMPLE_READ we have either single or group mode. */ | |
1257 | if (read_format & PERF_FORMAT_GROUP) | |
1258 | return deliver_sample_group(evlist, tool, event, sample, | |
1259 | machine, read_format, per_thread); | |
1260 | else | |
1261 | return deliver_sample_value(evlist, tool, event, sample, | |
1262 | &sample->read.one, machine, | |
1263 | per_thread); | |
1264 | } | |
1265 | ||
1266 | static int machines__deliver_event(struct machines *machines, | |
1267 | struct evlist *evlist, | |
1268 | union perf_event *event, | |
1269 | struct perf_sample *sample, | |
1270 | const struct perf_tool *tool, u64 file_offset, | |
1271 | const char *file_path) | |
1272 | { | |
1273 | struct evsel *evsel; | |
1274 | struct machine *machine; | |
1275 | ||
1276 | dump_event(evlist, event, file_offset, sample, file_path); | |
1277 | ||
1278 | evsel = evlist__id2evsel(evlist, sample->id); | |
1279 | ||
1280 | machine = machines__find_for_cpumode(machines, event, sample); | |
1281 | ||
1282 | switch (event->header.type) { | |
1283 | case PERF_RECORD_SAMPLE: | |
1284 | if (evsel == NULL) { | |
1285 | ++evlist->stats.nr_unknown_id; | |
1286 | return 0; | |
1287 | } | |
1288 | if (machine == NULL) { | |
1289 | ++evlist->stats.nr_unprocessable_samples; | |
1290 | dump_sample(evsel, event, sample, perf_env__arch(NULL)); | |
1291 | return 0; | |
1292 | } | |
1293 | dump_sample(evsel, event, sample, perf_env__arch(machine->env)); | |
1294 | return evlist__deliver_sample(evlist, tool, event, sample, evsel, machine); | |
1295 | case PERF_RECORD_MMAP: | |
1296 | return tool->mmap(tool, event, sample, machine); | |
1297 | case PERF_RECORD_MMAP2: | |
1298 | if (event->header.misc & PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT) | |
1299 | ++evlist->stats.nr_proc_map_timeout; | |
1300 | return tool->mmap2(tool, event, sample, machine); | |
1301 | case PERF_RECORD_COMM: | |
1302 | return tool->comm(tool, event, sample, machine); | |
1303 | case PERF_RECORD_NAMESPACES: | |
1304 | return tool->namespaces(tool, event, sample, machine); | |
1305 | case PERF_RECORD_CGROUP: | |
1306 | return tool->cgroup(tool, event, sample, machine); | |
1307 | case PERF_RECORD_FORK: | |
1308 | return tool->fork(tool, event, sample, machine); | |
1309 | case PERF_RECORD_EXIT: | |
1310 | return tool->exit(tool, event, sample, machine); | |
1311 | case PERF_RECORD_LOST: | |
1312 | if (tool->lost == perf_event__process_lost) | |
1313 | evlist->stats.total_lost += event->lost.lost; | |
1314 | return tool->lost(tool, event, sample, machine); | |
1315 | case PERF_RECORD_LOST_SAMPLES: | |
1316 | if (event->header.misc & PERF_RECORD_MISC_LOST_SAMPLES_BPF) | |
1317 | evlist->stats.total_dropped_samples += event->lost_samples.lost; | |
1318 | else if (tool->lost_samples == perf_event__process_lost_samples) | |
1319 | evlist->stats.total_lost_samples += event->lost_samples.lost; | |
1320 | return tool->lost_samples(tool, event, sample, machine); | |
1321 | case PERF_RECORD_READ: | |
1322 | dump_read(evsel, event); | |
1323 | return tool->read(tool, event, sample, evsel, machine); | |
1324 | case PERF_RECORD_THROTTLE: | |
1325 | return tool->throttle(tool, event, sample, machine); | |
1326 | case PERF_RECORD_UNTHROTTLE: | |
1327 | return tool->unthrottle(tool, event, sample, machine); | |
1328 | case PERF_RECORD_AUX: | |
1329 | if (tool->aux == perf_event__process_aux) { | |
1330 | if (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) | |
1331 | evlist->stats.total_aux_lost += 1; | |
1332 | if (event->aux.flags & PERF_AUX_FLAG_PARTIAL) | |
1333 | evlist->stats.total_aux_partial += 1; | |
1334 | if (event->aux.flags & PERF_AUX_FLAG_COLLISION) | |
1335 | evlist->stats.total_aux_collision += 1; | |
1336 | } | |
1337 | return tool->aux(tool, event, sample, machine); | |
1338 | case PERF_RECORD_ITRACE_START: | |
1339 | return tool->itrace_start(tool, event, sample, machine); | |
1340 | case PERF_RECORD_SWITCH: | |
1341 | case PERF_RECORD_SWITCH_CPU_WIDE: | |
1342 | return tool->context_switch(tool, event, sample, machine); | |
1343 | case PERF_RECORD_KSYMBOL: | |
1344 | return tool->ksymbol(tool, event, sample, machine); | |
1345 | case PERF_RECORD_BPF_EVENT: | |
1346 | return tool->bpf(tool, event, sample, machine); | |
1347 | case PERF_RECORD_TEXT_POKE: | |
1348 | return tool->text_poke(tool, event, sample, machine); | |
1349 | case PERF_RECORD_AUX_OUTPUT_HW_ID: | |
1350 | return tool->aux_output_hw_id(tool, event, sample, machine); | |
1351 | default: | |
1352 | ++evlist->stats.nr_unknown_events; | |
1353 | return -1; | |
1354 | } | |
1355 | } | |
1356 | ||
1357 | static int perf_session__deliver_event(struct perf_session *session, | |
1358 | union perf_event *event, | |
1359 | const struct perf_tool *tool, | |
1360 | u64 file_offset, | |
1361 | const char *file_path) | |
1362 | { | |
1363 | struct perf_sample sample; | |
1364 | int ret; | |
1365 | ||
1366 | perf_sample__init(&sample, /*all=*/false); | |
1367 | ret = evlist__parse_sample(session->evlist, event, &sample); | |
1368 | if (ret) { | |
1369 | pr_err("Can't parse sample, err = %d\n", ret); | |
1370 | goto out; | |
1371 | } | |
1372 | ||
1373 | ret = auxtrace__process_event(session, event, &sample, tool); | |
1374 | if (ret < 0) | |
1375 | goto out; | |
1376 | if (ret > 0) { | |
1377 | ret = 0; | |
1378 | goto out; | |
1379 | } | |
1380 | ||
1381 | ret = machines__deliver_event(&session->machines, session->evlist, | |
1382 | event, &sample, tool, file_offset, file_path); | |
1383 | ||
1384 | if (dump_trace && sample.aux_sample.size) | |
1385 | auxtrace__dump_auxtrace_sample(session, &sample); | |
1386 | out: | |
1387 | perf_sample__exit(&sample); | |
1388 | return ret; | |
1389 | } | |
1390 | ||
1391 | static s64 perf_session__process_user_event(struct perf_session *session, | |
1392 | union perf_event *event, | |
1393 | u64 file_offset, | |
1394 | const char *file_path) | |
1395 | { | |
1396 | struct ordered_events *oe = &session->ordered_events; | |
1397 | const struct perf_tool *tool = session->tool; | |
1398 | struct perf_sample sample; | |
1399 | int fd = perf_data__fd(session->data); | |
1400 | int err; | |
1401 | ||
1402 | perf_sample__init(&sample, /*all=*/true); | |
1403 | if ((event->header.type != PERF_RECORD_COMPRESSED && | |
1404 | event->header.type != PERF_RECORD_COMPRESSED2) || | |
1405 | perf_tool__compressed_is_stub(tool)) | |
1406 | dump_event(session->evlist, event, file_offset, &sample, file_path); | |
1407 | ||
1408 | /* These events are processed right away */ | |
1409 | switch (event->header.type) { | |
1410 | case PERF_RECORD_HEADER_ATTR: | |
1411 | err = tool->attr(tool, event, &session->evlist); | |
1412 | if (err == 0) { | |
1413 | perf_session__set_id_hdr_size(session); | |
1414 | perf_session__set_comm_exec(session); | |
1415 | } | |
1416 | break; | |
1417 | case PERF_RECORD_EVENT_UPDATE: | |
1418 | err = tool->event_update(tool, event, &session->evlist); | |
1419 | break; | |
1420 | case PERF_RECORD_HEADER_EVENT_TYPE: | |
1421 | /* | |
1422 | * Deprecated, but we need to handle it for sake | |
1423 | * of old data files create in pipe mode. | |
1424 | */ | |
1425 | err = 0; | |
1426 | break; | |
1427 | case PERF_RECORD_HEADER_TRACING_DATA: | |
1428 | /* | |
1429 | * Setup for reading amidst mmap, but only when we | |
1430 | * are in 'file' mode. The 'pipe' fd is in proper | |
1431 | * place already. | |
1432 | */ | |
1433 | if (!perf_data__is_pipe(session->data)) | |
1434 | lseek(fd, file_offset, SEEK_SET); | |
1435 | err = tool->tracing_data(session, event); | |
1436 | break; | |
1437 | case PERF_RECORD_HEADER_BUILD_ID: | |
1438 | err = tool->build_id(session, event); | |
1439 | break; | |
1440 | case PERF_RECORD_FINISHED_ROUND: | |
1441 | err = tool->finished_round(tool, event, oe); | |
1442 | break; | |
1443 | case PERF_RECORD_ID_INDEX: | |
1444 | err = tool->id_index(session, event); | |
1445 | break; | |
1446 | case PERF_RECORD_AUXTRACE_INFO: | |
1447 | err = tool->auxtrace_info(session, event); | |
1448 | break; | |
1449 | case PERF_RECORD_AUXTRACE: | |
1450 | /* | |
1451 | * Setup for reading amidst mmap, but only when we | |
1452 | * are in 'file' mode. The 'pipe' fd is in proper | |
1453 | * place already. | |
1454 | */ | |
1455 | if (!perf_data__is_pipe(session->data)) | |
1456 | lseek(fd, file_offset + event->header.size, SEEK_SET); | |
1457 | err = tool->auxtrace(session, event); | |
1458 | break; | |
1459 | case PERF_RECORD_AUXTRACE_ERROR: | |
1460 | perf_session__auxtrace_error_inc(session, event); | |
1461 | err = tool->auxtrace_error(session, event); | |
1462 | break; | |
1463 | case PERF_RECORD_THREAD_MAP: | |
1464 | err = tool->thread_map(session, event); | |
1465 | break; | |
1466 | case PERF_RECORD_CPU_MAP: | |
1467 | err = tool->cpu_map(session, event); | |
1468 | break; | |
1469 | case PERF_RECORD_STAT_CONFIG: | |
1470 | err = tool->stat_config(session, event); | |
1471 | break; | |
1472 | case PERF_RECORD_STAT: | |
1473 | err = tool->stat(session, event); | |
1474 | break; | |
1475 | case PERF_RECORD_STAT_ROUND: | |
1476 | err = tool->stat_round(session, event); | |
1477 | break; | |
1478 | case PERF_RECORD_TIME_CONV: | |
1479 | session->time_conv = event->time_conv; | |
1480 | err = tool->time_conv(session, event); | |
1481 | break; | |
1482 | case PERF_RECORD_HEADER_FEATURE: | |
1483 | err = tool->feature(session, event); | |
1484 | break; | |
1485 | case PERF_RECORD_COMPRESSED: | |
1486 | case PERF_RECORD_COMPRESSED2: | |
1487 | err = tool->compressed(session, event, file_offset, file_path); | |
1488 | if (err) | |
1489 | dump_event(session->evlist, event, file_offset, &sample, file_path); | |
1490 | break; | |
1491 | case PERF_RECORD_FINISHED_INIT: | |
1492 | err = tool->finished_init(session, event); | |
1493 | break; | |
1494 | default: | |
1495 | err = -EINVAL; | |
1496 | break; | |
1497 | } | |
1498 | perf_sample__exit(&sample); | |
1499 | return err; | |
1500 | } | |
1501 | ||
1502 | int perf_session__deliver_synth_event(struct perf_session *session, | |
1503 | union perf_event *event, | |
1504 | struct perf_sample *sample) | |
1505 | { | |
1506 | struct evlist *evlist = session->evlist; | |
1507 | const struct perf_tool *tool = session->tool; | |
1508 | ||
1509 | events_stats__inc(&evlist->stats, event->header.type); | |
1510 | ||
1511 | if (event->header.type >= PERF_RECORD_USER_TYPE_START) | |
1512 | return perf_session__process_user_event(session, event, 0, NULL); | |
1513 | ||
1514 | return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0, NULL); | |
1515 | } | |
1516 | ||
1517 | int perf_session__deliver_synth_attr_event(struct perf_session *session, | |
1518 | const struct perf_event_attr *attr, | |
1519 | u64 id) | |
1520 | { | |
1521 | union { | |
1522 | struct { | |
1523 | struct perf_record_header_attr attr; | |
1524 | u64 ids[1]; | |
1525 | } attr_id; | |
1526 | union perf_event ev; | |
1527 | } ev = { | |
1528 | .attr_id.attr.header.type = PERF_RECORD_HEADER_ATTR, | |
1529 | .attr_id.attr.header.size = sizeof(ev.attr_id), | |
1530 | .attr_id.ids[0] = id, | |
1531 | }; | |
1532 | ||
1533 | if (attr->size != sizeof(ev.attr_id.attr.attr)) { | |
1534 | pr_debug("Unexpected perf_event_attr size\n"); | |
1535 | return -EINVAL; | |
1536 | } | |
1537 | ev.attr_id.attr.attr = *attr; | |
1538 | return perf_session__deliver_synth_event(session, &ev.ev, NULL); | |
1539 | } | |
1540 | ||
1541 | static void event_swap(union perf_event *event, bool sample_id_all) | |
1542 | { | |
1543 | perf_event__swap_op swap; | |
1544 | ||
1545 | swap = perf_event__swap_ops[event->header.type]; | |
1546 | if (swap) | |
1547 | swap(event, sample_id_all); | |
1548 | } | |
1549 | ||
1550 | int perf_session__peek_event(struct perf_session *session, off_t file_offset, | |
1551 | void *buf, size_t buf_sz, | |
1552 | union perf_event **event_ptr, | |
1553 | struct perf_sample *sample) | |
1554 | { | |
1555 | union perf_event *event; | |
1556 | size_t hdr_sz, rest; | |
1557 | int fd; | |
1558 | ||
1559 | if (session->one_mmap && !session->header.needs_swap) { | |
1560 | event = file_offset - session->one_mmap_offset + | |
1561 | session->one_mmap_addr; | |
1562 | goto out_parse_sample; | |
1563 | } | |
1564 | ||
1565 | if (perf_data__is_pipe(session->data)) | |
1566 | return -1; | |
1567 | ||
1568 | fd = perf_data__fd(session->data); | |
1569 | hdr_sz = sizeof(struct perf_event_header); | |
1570 | ||
1571 | if (buf_sz < hdr_sz) | |
1572 | return -1; | |
1573 | ||
1574 | if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 || | |
1575 | readn(fd, buf, hdr_sz) != (ssize_t)hdr_sz) | |
1576 | return -1; | |
1577 | ||
1578 | event = (union perf_event *)buf; | |
1579 | ||
1580 | if (session->header.needs_swap) | |
1581 | perf_event_header__bswap(&event->header); | |
1582 | ||
1583 | if (event->header.size < hdr_sz || event->header.size > buf_sz) | |
1584 | return -1; | |
1585 | ||
1586 | buf += hdr_sz; | |
1587 | rest = event->header.size - hdr_sz; | |
1588 | ||
1589 | if (readn(fd, buf, rest) != (ssize_t)rest) | |
1590 | return -1; | |
1591 | ||
1592 | if (session->header.needs_swap) | |
1593 | event_swap(event, evlist__sample_id_all(session->evlist)); | |
1594 | ||
1595 | out_parse_sample: | |
1596 | ||
1597 | if (sample && event->header.type < PERF_RECORD_USER_TYPE_START && | |
1598 | evlist__parse_sample(session->evlist, event, sample)) | |
1599 | return -1; | |
1600 | ||
1601 | *event_ptr = event; | |
1602 | ||
1603 | return 0; | |
1604 | } | |
1605 | ||
1606 | int perf_session__peek_events(struct perf_session *session, u64 offset, | |
1607 | u64 size, peek_events_cb_t cb, void *data) | |
1608 | { | |
1609 | u64 max_offset = offset + size; | |
1610 | char buf[PERF_SAMPLE_MAX_SIZE]; | |
1611 | union perf_event *event; | |
1612 | int err; | |
1613 | ||
1614 | do { | |
1615 | err = perf_session__peek_event(session, offset, buf, | |
1616 | PERF_SAMPLE_MAX_SIZE, &event, | |
1617 | NULL); | |
1618 | if (err) | |
1619 | return err; | |
1620 | ||
1621 | err = cb(session, event, offset, data); | |
1622 | if (err) | |
1623 | return err; | |
1624 | ||
1625 | offset += event->header.size; | |
1626 | if (event->header.type == PERF_RECORD_AUXTRACE) | |
1627 | offset += event->auxtrace.size; | |
1628 | ||
1629 | } while (offset < max_offset); | |
1630 | ||
1631 | return err; | |
1632 | } | |
1633 | ||
1634 | static s64 perf_session__process_event(struct perf_session *session, | |
1635 | union perf_event *event, u64 file_offset, | |
1636 | const char *file_path) | |
1637 | { | |
1638 | struct evlist *evlist = session->evlist; | |
1639 | const struct perf_tool *tool = session->tool; | |
1640 | int ret; | |
1641 | ||
1642 | if (session->header.needs_swap) | |
1643 | event_swap(event, evlist__sample_id_all(evlist)); | |
1644 | ||
1645 | if (event->header.type >= PERF_RECORD_HEADER_MAX) { | |
1646 | /* perf should not support unaligned event, stop here. */ | |
1647 | if (event->header.size % sizeof(u64)) | |
1648 | return -EINVAL; | |
1649 | ||
1650 | /* This perf is outdated and does not support the latest event type. */ | |
1651 | ui__warning("Unsupported header type %u, please consider updating perf.\n", | |
1652 | event->header.type); | |
1653 | /* Skip unsupported event by returning its size. */ | |
1654 | return event->header.size; | |
1655 | } | |
1656 | ||
1657 | events_stats__inc(&evlist->stats, event->header.type); | |
1658 | ||
1659 | if (event->header.type >= PERF_RECORD_USER_TYPE_START) | |
1660 | return perf_session__process_user_event(session, event, file_offset, file_path); | |
1661 | ||
1662 | if (tool->ordered_events) { | |
1663 | u64 timestamp = -1ULL; | |
1664 | ||
1665 | ret = evlist__parse_sample_timestamp(evlist, event, ×tamp); | |
1666 | if (ret && ret != -1) | |
1667 | return ret; | |
1668 | ||
1669 | ret = perf_session__queue_event(session, event, timestamp, file_offset, file_path); | |
1670 | if (ret != -ETIME) | |
1671 | return ret; | |
1672 | } | |
1673 | ||
1674 | return perf_session__deliver_event(session, event, tool, file_offset, file_path); | |
1675 | } | |
1676 | ||
1677 | void perf_event_header__bswap(struct perf_event_header *hdr) | |
1678 | { | |
1679 | hdr->type = bswap_32(hdr->type); | |
1680 | hdr->misc = bswap_16(hdr->misc); | |
1681 | hdr->size = bswap_16(hdr->size); | |
1682 | } | |
1683 | ||
1684 | struct thread *perf_session__findnew(struct perf_session *session, pid_t pid) | |
1685 | { | |
1686 | return machine__findnew_thread(&session->machines.host, -1, pid); | |
1687 | } | |
1688 | ||
1689 | int perf_session__register_idle_thread(struct perf_session *session) | |
1690 | { | |
1691 | struct thread *thread = machine__idle_thread(&session->machines.host); | |
1692 | ||
1693 | /* machine__idle_thread() got the thread, so put it */ | |
1694 | thread__put(thread); | |
1695 | return thread ? 0 : -1; | |
1696 | } | |
1697 | ||
1698 | static void | |
1699 | perf_session__warn_order(const struct perf_session *session) | |
1700 | { | |
1701 | const struct ordered_events *oe = &session->ordered_events; | |
1702 | struct evsel *evsel; | |
1703 | bool should_warn = true; | |
1704 | ||
1705 | evlist__for_each_entry(session->evlist, evsel) { | |
1706 | if (evsel->core.attr.write_backward) | |
1707 | should_warn = false; | |
1708 | } | |
1709 | ||
1710 | if (!should_warn) | |
1711 | return; | |
1712 | if (oe->nr_unordered_events != 0) | |
1713 | ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events); | |
1714 | } | |
1715 | ||
1716 | static void perf_session__warn_about_errors(const struct perf_session *session) | |
1717 | { | |
1718 | const struct events_stats *stats = &session->evlist->stats; | |
1719 | ||
1720 | if (session->tool->lost == perf_event__process_lost && | |
1721 | stats->nr_events[PERF_RECORD_LOST] != 0) { | |
1722 | ui__warning("Processed %d events and lost %d chunks!\n\n" | |
1723 | "Check IO/CPU overload!\n\n", | |
1724 | stats->nr_events[0], | |
1725 | stats->nr_events[PERF_RECORD_LOST]); | |
1726 | } | |
1727 | ||
1728 | if (session->tool->lost_samples == perf_event__process_lost_samples) { | |
1729 | double drop_rate; | |
1730 | ||
1731 | drop_rate = (double)stats->total_lost_samples / | |
1732 | (double) (stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples); | |
1733 | if (drop_rate > 0.05) { | |
1734 | ui__warning("Processed %" PRIu64 " samples and lost %3.2f%%!\n\n", | |
1735 | stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples, | |
1736 | drop_rate * 100.0); | |
1737 | } | |
1738 | } | |
1739 | ||
1740 | if (session->tool->aux == perf_event__process_aux && | |
1741 | stats->total_aux_lost != 0) { | |
1742 | ui__warning("AUX data lost %" PRIu64 " times out of %u!\n\n", | |
1743 | stats->total_aux_lost, | |
1744 | stats->nr_events[PERF_RECORD_AUX]); | |
1745 | } | |
1746 | ||
1747 | if (session->tool->aux == perf_event__process_aux && | |
1748 | stats->total_aux_partial != 0) { | |
1749 | bool vmm_exclusive = false; | |
1750 | ||
1751 | (void)sysfs__read_bool("module/kvm_intel/parameters/vmm_exclusive", | |
1752 | &vmm_exclusive); | |
1753 | ||
1754 | ui__warning("AUX data had gaps in it %" PRIu64 " times out of %u!\n\n" | |
1755 | "Are you running a KVM guest in the background?%s\n\n", | |
1756 | stats->total_aux_partial, | |
1757 | stats->nr_events[PERF_RECORD_AUX], | |
1758 | vmm_exclusive ? | |
1759 | "\nReloading kvm_intel module with vmm_exclusive=0\n" | |
1760 | "will reduce the gaps to only guest's timeslices." : | |
1761 | ""); | |
1762 | } | |
1763 | ||
1764 | if (session->tool->aux == perf_event__process_aux && | |
1765 | stats->total_aux_collision != 0) { | |
1766 | ui__warning("AUX data detected collision %" PRIu64 " times out of %u!\n\n", | |
1767 | stats->total_aux_collision, | |
1768 | stats->nr_events[PERF_RECORD_AUX]); | |
1769 | } | |
1770 | ||
1771 | if (stats->nr_unknown_events != 0) { | |
1772 | ui__warning("Found %u unknown events!\n\n" | |
1773 | "Is this an older tool processing a perf.data " | |
1774 | "file generated by a more recent tool?\n\n" | |
1775 | "If that is not the case, consider " | |
1776 | "reporting to linux-kernel@vger.kernel.org.\n\n", | |
1777 | stats->nr_unknown_events); | |
1778 | } | |
1779 | ||
1780 | if (stats->nr_unknown_id != 0) { | |
1781 | ui__warning("%u samples with id not present in the header\n", | |
1782 | stats->nr_unknown_id); | |
1783 | } | |
1784 | ||
1785 | if (stats->nr_invalid_chains != 0) { | |
1786 | ui__warning("Found invalid callchains!\n\n" | |
1787 | "%u out of %u events were discarded for this reason.\n\n" | |
1788 | "Consider reporting to linux-kernel@vger.kernel.org.\n\n", | |
1789 | stats->nr_invalid_chains, | |
1790 | stats->nr_events[PERF_RECORD_SAMPLE]); | |
1791 | } | |
1792 | ||
1793 | if (stats->nr_unprocessable_samples != 0) { | |
1794 | ui__warning("%u unprocessable samples recorded.\n" | |
1795 | "Do you have a KVM guest running and not using 'perf kvm'?\n", | |
1796 | stats->nr_unprocessable_samples); | |
1797 | } | |
1798 | ||
1799 | perf_session__warn_order(session); | |
1800 | ||
1801 | events_stats__auxtrace_error_warn(stats); | |
1802 | ||
1803 | if (stats->nr_proc_map_timeout != 0) { | |
1804 | ui__warning("%d map information files for pre-existing threads were\n" | |
1805 | "not processed, if there are samples for addresses they\n" | |
1806 | "will not be resolved, you may find out which are these\n" | |
1807 | "threads by running with -v and redirecting the output\n" | |
1808 | "to a file.\n" | |
1809 | "The time limit to process proc map is too short?\n" | |
1810 | "Increase it by --proc-map-timeout\n", | |
1811 | stats->nr_proc_map_timeout); | |
1812 | } | |
1813 | } | |
1814 | ||
1815 | static int perf_session__flush_thread_stack(struct thread *thread, | |
1816 | void *p __maybe_unused) | |
1817 | { | |
1818 | return thread_stack__flush(thread); | |
1819 | } | |
1820 | ||
1821 | static int perf_session__flush_thread_stacks(struct perf_session *session) | |
1822 | { | |
1823 | return machines__for_each_thread(&session->machines, | |
1824 | perf_session__flush_thread_stack, | |
1825 | NULL); | |
1826 | } | |
1827 | ||
1828 | volatile sig_atomic_t session_done; | |
1829 | ||
1830 | static int __perf_session__process_decomp_events(struct perf_session *session); | |
1831 | ||
1832 | static int __perf_session__process_pipe_events(struct perf_session *session) | |
1833 | { | |
1834 | struct ordered_events *oe = &session->ordered_events; | |
1835 | const struct perf_tool *tool = session->tool; | |
1836 | struct ui_progress prog; | |
1837 | union perf_event *event; | |
1838 | uint32_t size, cur_size = 0; | |
1839 | void *buf = NULL; | |
1840 | s64 skip = 0; | |
1841 | u64 head; | |
1842 | ssize_t err; | |
1843 | void *p; | |
1844 | bool update_prog = false; | |
1845 | ||
1846 | /* | |
1847 | * If it's from a file saving pipe data (by redirection), it would have | |
1848 | * a file name other than "-". Then we can get the total size and show | |
1849 | * the progress. | |
1850 | */ | |
1851 | if (strcmp(session->data->path, "-") && session->data->file.size) { | |
1852 | ui_progress__init_size(&prog, session->data->file.size, | |
1853 | "Processing events..."); | |
1854 | update_prog = true; | |
1855 | } | |
1856 | ||
1857 | head = 0; | |
1858 | cur_size = sizeof(union perf_event); | |
1859 | ||
1860 | buf = malloc(cur_size); | |
1861 | if (!buf) | |
1862 | return -errno; | |
1863 | ordered_events__set_copy_on_queue(oe, true); | |
1864 | more: | |
1865 | event = buf; | |
1866 | err = perf_data__read(session->data, event, | |
1867 | sizeof(struct perf_event_header)); | |
1868 | if (err <= 0) { | |
1869 | if (err == 0) | |
1870 | goto done; | |
1871 | ||
1872 | pr_err("failed to read event header\n"); | |
1873 | goto out_err; | |
1874 | } | |
1875 | ||
1876 | if (session->header.needs_swap) | |
1877 | perf_event_header__bswap(&event->header); | |
1878 | ||
1879 | size = event->header.size; | |
1880 | if (size < sizeof(struct perf_event_header)) { | |
1881 | pr_err("bad event header size\n"); | |
1882 | goto out_err; | |
1883 | } | |
1884 | ||
1885 | if (size > cur_size) { | |
1886 | void *new = realloc(buf, size); | |
1887 | if (!new) { | |
1888 | pr_err("failed to allocate memory to read event\n"); | |
1889 | goto out_err; | |
1890 | } | |
1891 | buf = new; | |
1892 | cur_size = size; | |
1893 | event = buf; | |
1894 | } | |
1895 | p = event; | |
1896 | p += sizeof(struct perf_event_header); | |
1897 | ||
1898 | if (size - sizeof(struct perf_event_header)) { | |
1899 | err = perf_data__read(session->data, p, | |
1900 | size - sizeof(struct perf_event_header)); | |
1901 | if (err <= 0) { | |
1902 | if (err == 0) { | |
1903 | pr_err("unexpected end of event stream\n"); | |
1904 | goto done; | |
1905 | } | |
1906 | ||
1907 | pr_err("failed to read event data\n"); | |
1908 | goto out_err; | |
1909 | } | |
1910 | } | |
1911 | ||
1912 | if ((skip = perf_session__process_event(session, event, head, "pipe")) < 0) { | |
1913 | pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n", | |
1914 | head, event->header.size, event->header.type); | |
1915 | err = -EINVAL; | |
1916 | goto out_err; | |
1917 | } | |
1918 | ||
1919 | head += size; | |
1920 | ||
1921 | if (skip > 0) | |
1922 | head += skip; | |
1923 | ||
1924 | err = __perf_session__process_decomp_events(session); | |
1925 | if (err) | |
1926 | goto out_err; | |
1927 | ||
1928 | if (update_prog) | |
1929 | ui_progress__update(&prog, size); | |
1930 | ||
1931 | if (!session_done()) | |
1932 | goto more; | |
1933 | done: | |
1934 | /* do the final flush for ordered samples */ | |
1935 | err = ordered_events__flush(oe, OE_FLUSH__FINAL); | |
1936 | if (err) | |
1937 | goto out_err; | |
1938 | err = auxtrace__flush_events(session, tool); | |
1939 | if (err) | |
1940 | goto out_err; | |
1941 | err = perf_session__flush_thread_stacks(session); | |
1942 | out_err: | |
1943 | free(buf); | |
1944 | if (update_prog) | |
1945 | ui_progress__finish(); | |
1946 | if (!tool->no_warn) | |
1947 | perf_session__warn_about_errors(session); | |
1948 | ordered_events__free(&session->ordered_events); | |
1949 | auxtrace__free_events(session); | |
1950 | return err; | |
1951 | } | |
1952 | ||
1953 | static union perf_event * | |
1954 | prefetch_event(char *buf, u64 head, size_t mmap_size, | |
1955 | bool needs_swap, union perf_event *error) | |
1956 | { | |
1957 | union perf_event *event; | |
1958 | u16 event_size; | |
1959 | ||
1960 | /* | |
1961 | * Ensure we have enough space remaining to read | |
1962 | * the size of the event in the headers. | |
1963 | */ | |
1964 | if (head + sizeof(event->header) > mmap_size) | |
1965 | return NULL; | |
1966 | ||
1967 | event = (union perf_event *)(buf + head); | |
1968 | if (needs_swap) | |
1969 | perf_event_header__bswap(&event->header); | |
1970 | ||
1971 | event_size = event->header.size; | |
1972 | if (head + event_size <= mmap_size) | |
1973 | return event; | |
1974 | ||
1975 | /* We're not fetching the event so swap back again */ | |
1976 | if (needs_swap) | |
1977 | perf_event_header__bswap(&event->header); | |
1978 | ||
1979 | /* Check if the event fits into the next mmapped buf. */ | |
1980 | if (event_size <= mmap_size - head % page_size) { | |
1981 | /* Remap buf and fetch again. */ | |
1982 | return NULL; | |
1983 | } | |
1984 | ||
1985 | /* Invalid input. Event size should never exceed mmap_size. */ | |
1986 | pr_debug("%s: head=%#" PRIx64 " event->header.size=%#x, mmap_size=%#zx:" | |
1987 | " fuzzed or compressed perf.data?\n", __func__, head, event_size, mmap_size); | |
1988 | ||
1989 | return error; | |
1990 | } | |
1991 | ||
1992 | static union perf_event * | |
1993 | fetch_mmaped_event(u64 head, size_t mmap_size, char *buf, bool needs_swap) | |
1994 | { | |
1995 | return prefetch_event(buf, head, mmap_size, needs_swap, ERR_PTR(-EINVAL)); | |
1996 | } | |
1997 | ||
1998 | static union perf_event * | |
1999 | fetch_decomp_event(u64 head, size_t mmap_size, char *buf, bool needs_swap) | |
2000 | { | |
2001 | return prefetch_event(buf, head, mmap_size, needs_swap, NULL); | |
2002 | } | |
2003 | ||
2004 | static int __perf_session__process_decomp_events(struct perf_session *session) | |
2005 | { | |
2006 | s64 skip; | |
2007 | u64 size; | |
2008 | struct decomp *decomp = session->active_decomp->decomp_last; | |
2009 | ||
2010 | if (!decomp) | |
2011 | return 0; | |
2012 | ||
2013 | while (decomp->head < decomp->size && !session_done()) { | |
2014 | union perf_event *event = fetch_decomp_event(decomp->head, decomp->size, decomp->data, | |
2015 | session->header.needs_swap); | |
2016 | ||
2017 | if (!event) | |
2018 | break; | |
2019 | ||
2020 | size = event->header.size; | |
2021 | ||
2022 | if (size < sizeof(struct perf_event_header) || | |
2023 | (skip = perf_session__process_event(session, event, decomp->file_pos, | |
2024 | decomp->file_path)) < 0) { | |
2025 | pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n", | |
2026 | decomp->file_pos + decomp->head, event->header.size, event->header.type); | |
2027 | return -EINVAL; | |
2028 | } | |
2029 | ||
2030 | if (skip) | |
2031 | size += skip; | |
2032 | ||
2033 | decomp->head += size; | |
2034 | } | |
2035 | ||
2036 | return 0; | |
2037 | } | |
2038 | ||
2039 | /* | |
2040 | * On 64bit we can mmap the data file in one go. No need for tiny mmap | |
2041 | * slices. On 32bit we use 32MB. | |
2042 | */ | |
2043 | #if BITS_PER_LONG == 64 | |
2044 | #define MMAP_SIZE ULLONG_MAX | |
2045 | #define NUM_MMAPS 1 | |
2046 | #else | |
2047 | #define MMAP_SIZE (32 * 1024 * 1024ULL) | |
2048 | #define NUM_MMAPS 128 | |
2049 | #endif | |
2050 | ||
2051 | struct reader; | |
2052 | ||
2053 | typedef s64 (*reader_cb_t)(struct perf_session *session, | |
2054 | union perf_event *event, | |
2055 | u64 file_offset, | |
2056 | const char *file_path); | |
2057 | ||
2058 | struct reader { | |
2059 | int fd; | |
2060 | const char *path; | |
2061 | u64 data_size; | |
2062 | u64 data_offset; | |
2063 | reader_cb_t process; | |
2064 | bool in_place_update; | |
2065 | char *mmaps[NUM_MMAPS]; | |
2066 | size_t mmap_size; | |
2067 | int mmap_idx; | |
2068 | char *mmap_cur; | |
2069 | u64 file_pos; | |
2070 | u64 file_offset; | |
2071 | u64 head; | |
2072 | u64 size; | |
2073 | bool done; | |
2074 | struct zstd_data zstd_data; | |
2075 | struct decomp_data decomp_data; | |
2076 | }; | |
2077 | ||
2078 | static int | |
2079 | reader__init(struct reader *rd, bool *one_mmap) | |
2080 | { | |
2081 | u64 data_size = rd->data_size; | |
2082 | char **mmaps = rd->mmaps; | |
2083 | ||
2084 | rd->head = rd->data_offset; | |
2085 | data_size += rd->data_offset; | |
2086 | ||
2087 | rd->mmap_size = MMAP_SIZE; | |
2088 | if (rd->mmap_size > data_size) { | |
2089 | rd->mmap_size = data_size; | |
2090 | if (one_mmap) | |
2091 | *one_mmap = true; | |
2092 | } | |
2093 | ||
2094 | memset(mmaps, 0, sizeof(rd->mmaps)); | |
2095 | ||
2096 | if (zstd_init(&rd->zstd_data, 0)) | |
2097 | return -1; | |
2098 | rd->decomp_data.zstd_decomp = &rd->zstd_data; | |
2099 | ||
2100 | return 0; | |
2101 | } | |
2102 | ||
2103 | static void | |
2104 | reader__release_decomp(struct reader *rd) | |
2105 | { | |
2106 | perf_decomp__release_events(rd->decomp_data.decomp); | |
2107 | zstd_fini(&rd->zstd_data); | |
2108 | } | |
2109 | ||
2110 | static int | |
2111 | reader__mmap(struct reader *rd, struct perf_session *session) | |
2112 | { | |
2113 | int mmap_prot, mmap_flags; | |
2114 | char *buf, **mmaps = rd->mmaps; | |
2115 | u64 page_offset; | |
2116 | ||
2117 | mmap_prot = PROT_READ; | |
2118 | mmap_flags = MAP_SHARED; | |
2119 | ||
2120 | if (rd->in_place_update) { | |
2121 | mmap_prot |= PROT_WRITE; | |
2122 | } else if (session->header.needs_swap) { | |
2123 | mmap_prot |= PROT_WRITE; | |
2124 | mmap_flags = MAP_PRIVATE; | |
2125 | } | |
2126 | ||
2127 | if (mmaps[rd->mmap_idx]) { | |
2128 | munmap(mmaps[rd->mmap_idx], rd->mmap_size); | |
2129 | mmaps[rd->mmap_idx] = NULL; | |
2130 | } | |
2131 | ||
2132 | page_offset = page_size * (rd->head / page_size); | |
2133 | rd->file_offset += page_offset; | |
2134 | rd->head -= page_offset; | |
2135 | ||
2136 | buf = mmap(NULL, rd->mmap_size, mmap_prot, mmap_flags, rd->fd, | |
2137 | rd->file_offset); | |
2138 | if (buf == MAP_FAILED) { | |
2139 | pr_err("failed to mmap file\n"); | |
2140 | return -errno; | |
2141 | } | |
2142 | mmaps[rd->mmap_idx] = rd->mmap_cur = buf; | |
2143 | rd->mmap_idx = (rd->mmap_idx + 1) & (ARRAY_SIZE(rd->mmaps) - 1); | |
2144 | rd->file_pos = rd->file_offset + rd->head; | |
2145 | if (session->one_mmap) { | |
2146 | session->one_mmap_addr = buf; | |
2147 | session->one_mmap_offset = rd->file_offset; | |
2148 | } | |
2149 | ||
2150 | return 0; | |
2151 | } | |
2152 | ||
2153 | enum { | |
2154 | READER_OK, | |
2155 | READER_NODATA, | |
2156 | }; | |
2157 | ||
2158 | static int | |
2159 | reader__read_event(struct reader *rd, struct perf_session *session, | |
2160 | struct ui_progress *prog) | |
2161 | { | |
2162 | u64 size; | |
2163 | int err = READER_OK; | |
2164 | union perf_event *event; | |
2165 | s64 skip; | |
2166 | ||
2167 | event = fetch_mmaped_event(rd->head, rd->mmap_size, rd->mmap_cur, | |
2168 | session->header.needs_swap); | |
2169 | if (IS_ERR(event)) | |
2170 | return PTR_ERR(event); | |
2171 | ||
2172 | if (!event) | |
2173 | return READER_NODATA; | |
2174 | ||
2175 | size = event->header.size; | |
2176 | ||
2177 | skip = -EINVAL; | |
2178 | ||
2179 | if (size < sizeof(struct perf_event_header) || | |
2180 | (skip = rd->process(session, event, rd->file_pos, rd->path)) < 0) { | |
2181 | pr_err("%#" PRIx64 " [%#x]: failed to process type: %d [%s]\n", | |
2182 | rd->file_offset + rd->head, event->header.size, | |
2183 | event->header.type, strerror(-skip)); | |
2184 | err = skip; | |
2185 | goto out; | |
2186 | } | |
2187 | ||
2188 | if (skip) | |
2189 | size += skip; | |
2190 | ||
2191 | rd->size += size; | |
2192 | rd->head += size; | |
2193 | rd->file_pos += size; | |
2194 | ||
2195 | err = __perf_session__process_decomp_events(session); | |
2196 | if (err) | |
2197 | goto out; | |
2198 | ||
2199 | ui_progress__update(prog, size); | |
2200 | ||
2201 | out: | |
2202 | return err; | |
2203 | } | |
2204 | ||
2205 | static inline bool | |
2206 | reader__eof(struct reader *rd) | |
2207 | { | |
2208 | return (rd->file_pos >= rd->data_size + rd->data_offset); | |
2209 | } | |
2210 | ||
2211 | static int | |
2212 | reader__process_events(struct reader *rd, struct perf_session *session, | |
2213 | struct ui_progress *prog) | |
2214 | { | |
2215 | int err; | |
2216 | ||
2217 | err = reader__init(rd, &session->one_mmap); | |
2218 | if (err) | |
2219 | goto out; | |
2220 | ||
2221 | session->active_decomp = &rd->decomp_data; | |
2222 | ||
2223 | remap: | |
2224 | err = reader__mmap(rd, session); | |
2225 | if (err) | |
2226 | goto out; | |
2227 | ||
2228 | more: | |
2229 | err = reader__read_event(rd, session, prog); | |
2230 | if (err < 0) | |
2231 | goto out; | |
2232 | else if (err == READER_NODATA) | |
2233 | goto remap; | |
2234 | ||
2235 | if (session_done()) | |
2236 | goto out; | |
2237 | ||
2238 | if (!reader__eof(rd)) | |
2239 | goto more; | |
2240 | ||
2241 | out: | |
2242 | session->active_decomp = &session->decomp_data; | |
2243 | return err; | |
2244 | } | |
2245 | ||
2246 | static s64 process_simple(struct perf_session *session, | |
2247 | union perf_event *event, | |
2248 | u64 file_offset, | |
2249 | const char *file_path) | |
2250 | { | |
2251 | return perf_session__process_event(session, event, file_offset, file_path); | |
2252 | } | |
2253 | ||
2254 | static int __perf_session__process_events(struct perf_session *session) | |
2255 | { | |
2256 | struct reader rd = { | |
2257 | .fd = perf_data__fd(session->data), | |
2258 | .path = session->data->file.path, | |
2259 | .data_size = session->header.data_size, | |
2260 | .data_offset = session->header.data_offset, | |
2261 | .process = process_simple, | |
2262 | .in_place_update = session->data->in_place_update, | |
2263 | }; | |
2264 | struct ordered_events *oe = &session->ordered_events; | |
2265 | const struct perf_tool *tool = session->tool; | |
2266 | struct ui_progress prog; | |
2267 | int err; | |
2268 | ||
2269 | if (rd.data_size == 0) | |
2270 | return -1; | |
2271 | ||
2272 | ui_progress__init_size(&prog, rd.data_size, "Processing events..."); | |
2273 | ||
2274 | err = reader__process_events(&rd, session, &prog); | |
2275 | if (err) | |
2276 | goto out_err; | |
2277 | /* do the final flush for ordered samples */ | |
2278 | err = ordered_events__flush(oe, OE_FLUSH__FINAL); | |
2279 | if (err) | |
2280 | goto out_err; | |
2281 | err = auxtrace__flush_events(session, tool); | |
2282 | if (err) | |
2283 | goto out_err; | |
2284 | err = perf_session__flush_thread_stacks(session); | |
2285 | out_err: | |
2286 | ui_progress__finish(); | |
2287 | if (!tool->no_warn) | |
2288 | perf_session__warn_about_errors(session); | |
2289 | /* | |
2290 | * We may switching perf.data output, make ordered_events | |
2291 | * reusable. | |
2292 | */ | |
2293 | ordered_events__reinit(&session->ordered_events); | |
2294 | auxtrace__free_events(session); | |
2295 | reader__release_decomp(&rd); | |
2296 | session->one_mmap = false; | |
2297 | return err; | |
2298 | } | |
2299 | ||
2300 | /* | |
2301 | * Processing 2 MB of data from each reader in sequence, | |
2302 | * because that's the way the ordered events sorting works | |
2303 | * most efficiently. | |
2304 | */ | |
2305 | #define READER_MAX_SIZE (2 * 1024 * 1024) | |
2306 | ||
2307 | /* | |
2308 | * This function reads, merge and process directory data. | |
2309 | * It assumens the version 1 of directory data, where each | |
2310 | * data file holds per-cpu data, already sorted by kernel. | |
2311 | */ | |
2312 | static int __perf_session__process_dir_events(struct perf_session *session) | |
2313 | { | |
2314 | struct perf_data *data = session->data; | |
2315 | const struct perf_tool *tool = session->tool; | |
2316 | int i, ret, readers, nr_readers; | |
2317 | struct ui_progress prog; | |
2318 | u64 total_size = perf_data__size(session->data); | |
2319 | struct reader *rd; | |
2320 | ||
2321 | ui_progress__init_size(&prog, total_size, "Processing events..."); | |
2322 | ||
2323 | nr_readers = 1; | |
2324 | for (i = 0; i < data->dir.nr; i++) { | |
2325 | if (data->dir.files[i].size) | |
2326 | nr_readers++; | |
2327 | } | |
2328 | ||
2329 | rd = zalloc(nr_readers * sizeof(struct reader)); | |
2330 | if (!rd) | |
2331 | return -ENOMEM; | |
2332 | ||
2333 | rd[0] = (struct reader) { | |
2334 | .fd = perf_data__fd(session->data), | |
2335 | .path = session->data->file.path, | |
2336 | .data_size = session->header.data_size, | |
2337 | .data_offset = session->header.data_offset, | |
2338 | .process = process_simple, | |
2339 | .in_place_update = session->data->in_place_update, | |
2340 | }; | |
2341 | ret = reader__init(&rd[0], NULL); | |
2342 | if (ret) | |
2343 | goto out_err; | |
2344 | ret = reader__mmap(&rd[0], session); | |
2345 | if (ret) | |
2346 | goto out_err; | |
2347 | readers = 1; | |
2348 | ||
2349 | for (i = 0; i < data->dir.nr; i++) { | |
2350 | if (!data->dir.files[i].size) | |
2351 | continue; | |
2352 | rd[readers] = (struct reader) { | |
2353 | .fd = data->dir.files[i].fd, | |
2354 | .path = data->dir.files[i].path, | |
2355 | .data_size = data->dir.files[i].size, | |
2356 | .data_offset = 0, | |
2357 | .process = process_simple, | |
2358 | .in_place_update = session->data->in_place_update, | |
2359 | }; | |
2360 | ret = reader__init(&rd[readers], NULL); | |
2361 | if (ret) | |
2362 | goto out_err; | |
2363 | ret = reader__mmap(&rd[readers], session); | |
2364 | if (ret) | |
2365 | goto out_err; | |
2366 | readers++; | |
2367 | } | |
2368 | ||
2369 | i = 0; | |
2370 | while (readers) { | |
2371 | if (session_done()) | |
2372 | break; | |
2373 | ||
2374 | if (rd[i].done) { | |
2375 | i = (i + 1) % nr_readers; | |
2376 | continue; | |
2377 | } | |
2378 | if (reader__eof(&rd[i])) { | |
2379 | rd[i].done = true; | |
2380 | readers--; | |
2381 | continue; | |
2382 | } | |
2383 | ||
2384 | session->active_decomp = &rd[i].decomp_data; | |
2385 | ret = reader__read_event(&rd[i], session, &prog); | |
2386 | if (ret < 0) { | |
2387 | goto out_err; | |
2388 | } else if (ret == READER_NODATA) { | |
2389 | ret = reader__mmap(&rd[i], session); | |
2390 | if (ret) | |
2391 | goto out_err; | |
2392 | } | |
2393 | ||
2394 | if (rd[i].size >= READER_MAX_SIZE) { | |
2395 | rd[i].size = 0; | |
2396 | i = (i + 1) % nr_readers; | |
2397 | } | |
2398 | } | |
2399 | ||
2400 | ret = ordered_events__flush(&session->ordered_events, OE_FLUSH__FINAL); | |
2401 | if (ret) | |
2402 | goto out_err; | |
2403 | ||
2404 | ret = perf_session__flush_thread_stacks(session); | |
2405 | out_err: | |
2406 | ui_progress__finish(); | |
2407 | ||
2408 | if (!tool->no_warn) | |
2409 | perf_session__warn_about_errors(session); | |
2410 | ||
2411 | /* | |
2412 | * We may switching perf.data output, make ordered_events | |
2413 | * reusable. | |
2414 | */ | |
2415 | ordered_events__reinit(&session->ordered_events); | |
2416 | ||
2417 | session->one_mmap = false; | |
2418 | ||
2419 | session->active_decomp = &session->decomp_data; | |
2420 | for (i = 0; i < nr_readers; i++) | |
2421 | reader__release_decomp(&rd[i]); | |
2422 | zfree(&rd); | |
2423 | ||
2424 | return ret; | |
2425 | } | |
2426 | ||
2427 | int perf_session__process_events(struct perf_session *session) | |
2428 | { | |
2429 | if (perf_session__register_idle_thread(session) < 0) | |
2430 | return -ENOMEM; | |
2431 | ||
2432 | if (perf_data__is_pipe(session->data)) | |
2433 | return __perf_session__process_pipe_events(session); | |
2434 | ||
2435 | if (perf_data__is_dir(session->data) && session->data->dir.nr) | |
2436 | return __perf_session__process_dir_events(session); | |
2437 | ||
2438 | return __perf_session__process_events(session); | |
2439 | } | |
2440 | ||
2441 | bool perf_session__has_traces(struct perf_session *session, const char *msg) | |
2442 | { | |
2443 | struct evsel *evsel; | |
2444 | ||
2445 | evlist__for_each_entry(session->evlist, evsel) { | |
2446 | if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) | |
2447 | return true; | |
2448 | } | |
2449 | ||
2450 | pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg); | |
2451 | return false; | |
2452 | } | |
2453 | ||
2454 | bool perf_session__has_switch_events(struct perf_session *session) | |
2455 | { | |
2456 | struct evsel *evsel; | |
2457 | ||
2458 | evlist__for_each_entry(session->evlist, evsel) { | |
2459 | if (evsel->core.attr.context_switch) | |
2460 | return true; | |
2461 | } | |
2462 | ||
2463 | return false; | |
2464 | } | |
2465 | ||
2466 | int map__set_kallsyms_ref_reloc_sym(struct map *map, const char *symbol_name, u64 addr) | |
2467 | { | |
2468 | char *bracket; | |
2469 | struct ref_reloc_sym *ref; | |
2470 | struct kmap *kmap; | |
2471 | ||
2472 | ref = zalloc(sizeof(struct ref_reloc_sym)); | |
2473 | if (ref == NULL) | |
2474 | return -ENOMEM; | |
2475 | ||
2476 | ref->name = strdup(symbol_name); | |
2477 | if (ref->name == NULL) { | |
2478 | free(ref); | |
2479 | return -ENOMEM; | |
2480 | } | |
2481 | ||
2482 | bracket = strchr(ref->name, ']'); | |
2483 | if (bracket) | |
2484 | *bracket = '\0'; | |
2485 | ||
2486 | ref->addr = addr; | |
2487 | ||
2488 | kmap = map__kmap(map); | |
2489 | if (kmap) | |
2490 | kmap->ref_reloc_sym = ref; | |
2491 | ||
2492 | return 0; | |
2493 | } | |
2494 | ||
2495 | size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp) | |
2496 | { | |
2497 | return machines__fprintf_dsos(&session->machines, fp); | |
2498 | } | |
2499 | ||
2500 | size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp, | |
2501 | bool (skip)(struct dso *dso, int parm), int parm) | |
2502 | { | |
2503 | return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm); | |
2504 | } | |
2505 | ||
2506 | size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp) | |
2507 | { | |
2508 | size_t ret; | |
2509 | const char *msg = ""; | |
2510 | ||
2511 | if (perf_header__has_feat(&session->header, HEADER_AUXTRACE)) | |
2512 | msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)"; | |
2513 | ||
2514 | ret = fprintf(fp, "\nAggregated stats:%s\n", msg); | |
2515 | ||
2516 | ret += events_stats__fprintf(&session->evlist->stats, fp); | |
2517 | return ret; | |
2518 | } | |
2519 | ||
2520 | size_t perf_session__fprintf(struct perf_session *session, FILE *fp) | |
2521 | { | |
2522 | /* | |
2523 | * FIXME: Here we have to actually print all the machines in this | |
2524 | * session, not just the host... | |
2525 | */ | |
2526 | return machine__fprintf(&session->machines.host, fp); | |
2527 | } | |
2528 | ||
2529 | void perf_session__dump_kmaps(struct perf_session *session) | |
2530 | { | |
2531 | int save_verbose = verbose; | |
2532 | ||
2533 | fflush(stdout); | |
2534 | fprintf(stderr, "Kernel and module maps:\n"); | |
2535 | verbose = 0; /* Suppress verbose to print a summary only */ | |
2536 | maps__fprintf(machine__kernel_maps(&session->machines.host), stderr); | |
2537 | verbose = save_verbose; | |
2538 | } | |
2539 | ||
2540 | struct evsel *perf_session__find_first_evtype(struct perf_session *session, | |
2541 | unsigned int type) | |
2542 | { | |
2543 | struct evsel *pos; | |
2544 | ||
2545 | evlist__for_each_entry(session->evlist, pos) { | |
2546 | if (pos->core.attr.type == type) | |
2547 | return pos; | |
2548 | } | |
2549 | return NULL; | |
2550 | } | |
2551 | ||
2552 | int perf_session__cpu_bitmap(struct perf_session *session, | |
2553 | const char *cpu_list, unsigned long *cpu_bitmap) | |
2554 | { | |
2555 | int i, err = -1; | |
2556 | struct perf_cpu_map *map; | |
2557 | int nr_cpus = min(session->header.env.nr_cpus_avail, MAX_NR_CPUS); | |
2558 | struct perf_cpu cpu; | |
2559 | ||
2560 | for (i = 0; i < PERF_TYPE_MAX; ++i) { | |
2561 | struct evsel *evsel; | |
2562 | ||
2563 | evsel = perf_session__find_first_evtype(session, i); | |
2564 | if (!evsel) | |
2565 | continue; | |
2566 | ||
2567 | if (!(evsel->core.attr.sample_type & PERF_SAMPLE_CPU)) { | |
2568 | pr_err("File does not contain CPU events. " | |
2569 | "Remove -C option to proceed.\n"); | |
2570 | return -1; | |
2571 | } | |
2572 | } | |
2573 | ||
2574 | map = perf_cpu_map__new(cpu_list); | |
2575 | if (map == NULL) { | |
2576 | pr_err("Invalid cpu_list\n"); | |
2577 | return -1; | |
2578 | } | |
2579 | ||
2580 | perf_cpu_map__for_each_cpu(cpu, i, map) { | |
2581 | if (cpu.cpu >= nr_cpus) { | |
2582 | pr_err("Requested CPU %d too large. " | |
2583 | "Consider raising MAX_NR_CPUS\n", cpu.cpu); | |
2584 | goto out_delete_map; | |
2585 | } | |
2586 | ||
2587 | __set_bit(cpu.cpu, cpu_bitmap); | |
2588 | } | |
2589 | ||
2590 | err = 0; | |
2591 | ||
2592 | out_delete_map: | |
2593 | perf_cpu_map__put(map); | |
2594 | return err; | |
2595 | } | |
2596 | ||
2597 | void perf_session__fprintf_info(struct perf_session *session, FILE *fp, | |
2598 | bool full) | |
2599 | { | |
2600 | if (session == NULL || fp == NULL) | |
2601 | return; | |
2602 | ||
2603 | fprintf(fp, "# ========\n"); | |
2604 | perf_header__fprintf_info(session, fp, full); | |
2605 | fprintf(fp, "# ========\n#\n"); | |
2606 | } | |
2607 | ||
2608 | static int perf_session__register_guest(struct perf_session *session, pid_t machine_pid) | |
2609 | { | |
2610 | struct machine *machine = machines__findnew(&session->machines, machine_pid); | |
2611 | struct thread *thread; | |
2612 | ||
2613 | if (!machine) | |
2614 | return -ENOMEM; | |
2615 | ||
2616 | machine->single_address_space = session->machines.host.single_address_space; | |
2617 | ||
2618 | thread = machine__idle_thread(machine); | |
2619 | if (!thread) | |
2620 | return -ENOMEM; | |
2621 | thread__put(thread); | |
2622 | ||
2623 | machine->kallsyms_filename = perf_data__guest_kallsyms_name(session->data, machine_pid); | |
2624 | ||
2625 | return 0; | |
2626 | } | |
2627 | ||
2628 | static int perf_session__set_guest_cpu(struct perf_session *session, pid_t pid, | |
2629 | pid_t tid, int guest_cpu) | |
2630 | { | |
2631 | struct machine *machine = &session->machines.host; | |
2632 | struct thread *thread = machine__findnew_thread(machine, pid, tid); | |
2633 | ||
2634 | if (!thread) | |
2635 | return -ENOMEM; | |
2636 | thread__set_guest_cpu(thread, guest_cpu); | |
2637 | thread__put(thread); | |
2638 | ||
2639 | return 0; | |
2640 | } | |
2641 | ||
2642 | int perf_event__process_id_index(struct perf_session *session, | |
2643 | union perf_event *event) | |
2644 | { | |
2645 | struct evlist *evlist = session->evlist; | |
2646 | struct perf_record_id_index *ie = &event->id_index; | |
2647 | size_t sz = ie->header.size - sizeof(*ie); | |
2648 | size_t i, nr, max_nr; | |
2649 | size_t e1_sz = sizeof(struct id_index_entry); | |
2650 | size_t e2_sz = sizeof(struct id_index_entry_2); | |
2651 | size_t etot_sz = e1_sz + e2_sz; | |
2652 | struct id_index_entry_2 *e2; | |
2653 | pid_t last_pid = 0; | |
2654 | ||
2655 | max_nr = sz / e1_sz; | |
2656 | nr = ie->nr; | |
2657 | if (nr > max_nr) { | |
2658 | printf("Too big: nr %zu max_nr %zu\n", nr, max_nr); | |
2659 | return -EINVAL; | |
2660 | } | |
2661 | ||
2662 | if (sz >= nr * etot_sz) { | |
2663 | max_nr = sz / etot_sz; | |
2664 | if (nr > max_nr) { | |
2665 | printf("Too big2: nr %zu max_nr %zu\n", nr, max_nr); | |
2666 | return -EINVAL; | |
2667 | } | |
2668 | e2 = (void *)ie + sizeof(*ie) + nr * e1_sz; | |
2669 | } else { | |
2670 | e2 = NULL; | |
2671 | } | |
2672 | ||
2673 | if (dump_trace) | |
2674 | fprintf(stdout, " nr: %zu\n", nr); | |
2675 | ||
2676 | for (i = 0; i < nr; i++, (e2 ? e2++ : 0)) { | |
2677 | struct id_index_entry *e = &ie->entries[i]; | |
2678 | struct perf_sample_id *sid; | |
2679 | int ret; | |
2680 | ||
2681 | if (dump_trace) { | |
2682 | fprintf(stdout, " ... id: %"PRI_lu64, e->id); | |
2683 | fprintf(stdout, " idx: %"PRI_lu64, e->idx); | |
2684 | fprintf(stdout, " cpu: %"PRI_ld64, e->cpu); | |
2685 | fprintf(stdout, " tid: %"PRI_ld64, e->tid); | |
2686 | if (e2) { | |
2687 | fprintf(stdout, " machine_pid: %"PRI_ld64, e2->machine_pid); | |
2688 | fprintf(stdout, " vcpu: %"PRI_lu64"\n", e2->vcpu); | |
2689 | } else { | |
2690 | fprintf(stdout, "\n"); | |
2691 | } | |
2692 | } | |
2693 | ||
2694 | sid = evlist__id2sid(evlist, e->id); | |
2695 | if (!sid) | |
2696 | return -ENOENT; | |
2697 | ||
2698 | sid->idx = e->idx; | |
2699 | sid->cpu.cpu = e->cpu; | |
2700 | sid->tid = e->tid; | |
2701 | ||
2702 | if (!e2) | |
2703 | continue; | |
2704 | ||
2705 | sid->machine_pid = e2->machine_pid; | |
2706 | sid->vcpu.cpu = e2->vcpu; | |
2707 | ||
2708 | if (!sid->machine_pid) | |
2709 | continue; | |
2710 | ||
2711 | if (sid->machine_pid != last_pid) { | |
2712 | ret = perf_session__register_guest(session, sid->machine_pid); | |
2713 | if (ret) | |
2714 | return ret; | |
2715 | last_pid = sid->machine_pid; | |
2716 | perf_guest = true; | |
2717 | } | |
2718 | ||
2719 | ret = perf_session__set_guest_cpu(session, sid->machine_pid, e->tid, e2->vcpu); | |
2720 | if (ret) | |
2721 | return ret; | |
2722 | } | |
2723 | return 0; | |
2724 | } | |
2725 | ||
2726 | int perf_session__dsos_hit_all(struct perf_session *session) | |
2727 | { | |
2728 | struct rb_node *nd; | |
2729 | int err; | |
2730 | ||
2731 | err = machine__hit_all_dsos(&session->machines.host); | |
2732 | if (err) | |
2733 | return err; | |
2734 | ||
2735 | for (nd = rb_first_cached(&session->machines.guests); nd; | |
2736 | nd = rb_next(nd)) { | |
2737 | struct machine *pos = rb_entry(nd, struct machine, rb_node); | |
2738 | ||
2739 | err = machine__hit_all_dsos(pos); | |
2740 | if (err) | |
2741 | return err; | |
2742 | } | |
2743 | ||
2744 | return 0; | |
2745 | } |