Commit | Line | Data |
---|---|---|
b8f46c5a XG |
1 | #define _FILE_OFFSET_BITS 64 |
2 | ||
94c744b6 ACM |
3 | #include <linux/kernel.h> |
4 | ||
ba21594c | 5 | #include <byteswap.h> |
94c744b6 ACM |
6 | #include <unistd.h> |
7 | #include <sys/types.h> | |
a41794cd | 8 | #include <sys/mman.h> |
94c744b6 | 9 | |
e248de33 ACM |
10 | #include "evlist.h" |
11 | #include "evsel.h" | |
94c744b6 | 12 | #include "session.h" |
45694aa7 | 13 | #include "tool.h" |
a328626b | 14 | #include "sort.h" |
94c744b6 | 15 | #include "util.h" |
5d67be97 | 16 | #include "cpumap.h" |
da378962 | 17 | #include "event-parse.h" |
0f6a3015 | 18 | #include "perf_regs.h" |
71ad0f5e | 19 | #include "unwind.h" |
94c744b6 ACM |
20 | |
21 | static int perf_session__open(struct perf_session *self, bool force) | |
22 | { | |
23 | struct stat input_stat; | |
24 | ||
8dc58101 TZ |
25 | if (!strcmp(self->filename, "-")) { |
26 | self->fd_pipe = true; | |
27 | self->fd = STDIN_FILENO; | |
28 | ||
a91e5431 | 29 | if (perf_session__read_header(self, self->fd) < 0) |
69996df4 | 30 | pr_err("incompatible file format (rerun with -v to learn more)"); |
8dc58101 TZ |
31 | |
32 | return 0; | |
33 | } | |
34 | ||
f887f301 | 35 | self->fd = open(self->filename, O_RDONLY); |
94c744b6 | 36 | if (self->fd < 0) { |
0f2c3de2 AI |
37 | int err = errno; |
38 | ||
39 | pr_err("failed to open %s: %s", self->filename, strerror(err)); | |
40 | if (err == ENOENT && !strcmp(self->filename, "perf.data")) | |
94c744b6 ACM |
41 | pr_err(" (try 'perf record' first)"); |
42 | pr_err("\n"); | |
43 | return -errno; | |
44 | } | |
45 | ||
46 | if (fstat(self->fd, &input_stat) < 0) | |
47 | goto out_close; | |
48 | ||
49 | if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) { | |
50 | pr_err("file %s not owned by current user or root\n", | |
51 | self->filename); | |
52 | goto out_close; | |
53 | } | |
54 | ||
55 | if (!input_stat.st_size) { | |
56 | pr_info("zero-sized file (%s), nothing to do!\n", | |
57 | self->filename); | |
58 | goto out_close; | |
59 | } | |
60 | ||
a91e5431 | 61 | if (perf_session__read_header(self, self->fd) < 0) { |
69996df4 | 62 | pr_err("incompatible file format (rerun with -v to learn more)"); |
94c744b6 ACM |
63 | goto out_close; |
64 | } | |
65 | ||
c2a70653 ACM |
66 | if (!perf_evlist__valid_sample_type(self->evlist)) { |
67 | pr_err("non matching sample_type"); | |
68 | goto out_close; | |
69 | } | |
70 | ||
71 | if (!perf_evlist__valid_sample_id_all(self->evlist)) { | |
72 | pr_err("non matching sample_id_all"); | |
73 | goto out_close; | |
74 | } | |
75 | ||
94c744b6 ACM |
76 | self->size = input_stat.st_size; |
77 | return 0; | |
78 | ||
79 | out_close: | |
80 | close(self->fd); | |
81 | self->fd = -1; | |
82 | return -1; | |
83 | } | |
84 | ||
7b56cce2 | 85 | void perf_session__set_id_hdr_size(struct perf_session *session) |
9c90a61c | 86 | { |
7b56cce2 ACM |
87 | u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist); |
88 | ||
89 | session->host_machine.id_hdr_size = id_hdr_size; | |
90 | machines__set_id_hdr_size(&session->machines, id_hdr_size); | |
9c90a61c ACM |
91 | } |
92 | ||
a1645ce1 ZY |
93 | int perf_session__create_kernel_maps(struct perf_session *self) |
94 | { | |
d118f8ba | 95 | int ret = machine__create_kernel_maps(&self->host_machine); |
a1645ce1 | 96 | |
a1645ce1 | 97 | if (ret >= 0) |
d118f8ba | 98 | ret = machines__create_guest_kernel_maps(&self->machines); |
a1645ce1 ZY |
99 | return ret; |
100 | } | |
101 | ||
076c6e45 ACM |
102 | static void perf_session__destroy_kernel_maps(struct perf_session *self) |
103 | { | |
104 | machine__destroy_kernel_maps(&self->host_machine); | |
105 | machines__destroy_guest_kernel_maps(&self->machines); | |
106 | } | |
107 | ||
21ef97f0 IM |
108 | struct perf_session *perf_session__new(const char *filename, int mode, |
109 | bool force, bool repipe, | |
45694aa7 | 110 | struct perf_tool *tool) |
94c744b6 | 111 | { |
efad1415 RR |
112 | struct perf_session *self; |
113 | struct stat st; | |
114 | size_t len; | |
115 | ||
116 | if (!filename || !strlen(filename)) { | |
117 | if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode)) | |
118 | filename = "-"; | |
119 | else | |
120 | filename = "perf.data"; | |
121 | } | |
122 | ||
123 | len = strlen(filename); | |
124 | self = zalloc(sizeof(*self) + len); | |
94c744b6 ACM |
125 | |
126 | if (self == NULL) | |
127 | goto out; | |
128 | ||
94c744b6 | 129 | memcpy(self->filename, filename, len); |
55b44629 TG |
130 | /* |
131 | * On 64bit we can mmap the data file in one go. No need for tiny mmap | |
132 | * slices. On 32bit we use 32MB. | |
133 | */ | |
134 | #if BITS_PER_LONG == 64 | |
135 | self->mmap_window = ULLONG_MAX; | |
136 | #else | |
137 | self->mmap_window = 32 * 1024 * 1024ULL; | |
138 | #endif | |
23346f21 | 139 | self->machines = RB_ROOT; |
454c407e | 140 | self->repipe = repipe; |
a1225dec | 141 | INIT_LIST_HEAD(&self->ordered_samples.samples); |
020bb75a | 142 | INIT_LIST_HEAD(&self->ordered_samples.sample_cache); |
5c891f38 | 143 | INIT_LIST_HEAD(&self->ordered_samples.to_free); |
1f626bc3 | 144 | machine__init(&self->host_machine, "", HOST_KERNEL_ID); |
4bf9ce1b | 145 | hists__init(&self->hists); |
94c744b6 | 146 | |
64abebf7 ACM |
147 | if (mode == O_RDONLY) { |
148 | if (perf_session__open(self, force) < 0) | |
149 | goto out_delete; | |
7b56cce2 | 150 | perf_session__set_id_hdr_size(self); |
64abebf7 ACM |
151 | } else if (mode == O_WRONLY) { |
152 | /* | |
153 | * In O_RDONLY mode this will be performed when reading the | |
8115d60c | 154 | * kernel MMAP event, in perf_event__process_mmap(). |
64abebf7 ACM |
155 | */ |
156 | if (perf_session__create_kernel_maps(self) < 0) | |
157 | goto out_delete; | |
158 | } | |
d549c769 | 159 | |
45694aa7 | 160 | if (tool && tool->ordering_requires_timestamps && |
5e562474 | 161 | tool->ordered_samples && !perf_evlist__sample_id_all(self->evlist)) { |
21ef97f0 | 162 | dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n"); |
45694aa7 | 163 | tool->ordered_samples = false; |
21ef97f0 IM |
164 | } |
165 | ||
94c744b6 ACM |
166 | out: |
167 | return self; | |
4aa65636 ACM |
168 | out_delete: |
169 | perf_session__delete(self); | |
170 | return NULL; | |
94c744b6 ACM |
171 | } |
172 | ||
b424eba2 | 173 | static void machine__delete_dead_threads(struct machine *machine) |
d65a458b ACM |
174 | { |
175 | struct thread *n, *t; | |
176 | ||
b424eba2 | 177 | list_for_each_entry_safe(t, n, &machine->dead_threads, node) { |
d65a458b ACM |
178 | list_del(&t->node); |
179 | thread__delete(t); | |
180 | } | |
181 | } | |
182 | ||
b424eba2 ACM |
183 | static void perf_session__delete_dead_threads(struct perf_session *session) |
184 | { | |
185 | machine__delete_dead_threads(&session->host_machine); | |
186 | } | |
187 | ||
188 | static void machine__delete_threads(struct machine *self) | |
d65a458b ACM |
189 | { |
190 | struct rb_node *nd = rb_first(&self->threads); | |
191 | ||
192 | while (nd) { | |
193 | struct thread *t = rb_entry(nd, struct thread, rb_node); | |
194 | ||
195 | rb_erase(&t->rb_node, &self->threads); | |
196 | nd = rb_next(nd); | |
197 | thread__delete(t); | |
198 | } | |
199 | } | |
200 | ||
b424eba2 ACM |
201 | static void perf_session__delete_threads(struct perf_session *session) |
202 | { | |
203 | machine__delete_threads(&session->host_machine); | |
204 | } | |
205 | ||
94c744b6 ACM |
206 | void perf_session__delete(struct perf_session *self) |
207 | { | |
076c6e45 | 208 | perf_session__destroy_kernel_maps(self); |
d65a458b ACM |
209 | perf_session__delete_dead_threads(self); |
210 | perf_session__delete_threads(self); | |
211 | machine__exit(&self->host_machine); | |
94c744b6 ACM |
212 | close(self->fd); |
213 | free(self); | |
214 | } | |
a328626b | 215 | |
b424eba2 | 216 | void machine__remove_thread(struct machine *self, struct thread *th) |
720a3aeb | 217 | { |
70597f21 | 218 | self->last_match = NULL; |
720a3aeb ACM |
219 | rb_erase(&th->rb_node, &self->threads); |
220 | /* | |
221 | * We may have references to this thread, for instance in some hist_entry | |
222 | * instances, so just move them to a separate list. | |
223 | */ | |
224 | list_add_tail(&th->node, &self->dead_threads); | |
225 | } | |
226 | ||
a328626b ACM |
227 | static bool symbol__match_parent_regex(struct symbol *sym) |
228 | { | |
229 | if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0)) | |
230 | return 1; | |
231 | ||
232 | return 0; | |
233 | } | |
234 | ||
b5387528 RAV |
235 | static const u8 cpumodes[] = { |
236 | PERF_RECORD_MISC_USER, | |
237 | PERF_RECORD_MISC_KERNEL, | |
238 | PERF_RECORD_MISC_GUEST_USER, | |
239 | PERF_RECORD_MISC_GUEST_KERNEL | |
240 | }; | |
241 | #define NCPUMODES (sizeof(cpumodes)/sizeof(u8)) | |
242 | ||
243 | static void ip__resolve_ams(struct machine *self, struct thread *thread, | |
244 | struct addr_map_symbol *ams, | |
245 | u64 ip) | |
246 | { | |
247 | struct addr_location al; | |
248 | size_t i; | |
249 | u8 m; | |
250 | ||
251 | memset(&al, 0, sizeof(al)); | |
252 | ||
253 | for (i = 0; i < NCPUMODES; i++) { | |
254 | m = cpumodes[i]; | |
255 | /* | |
256 | * We cannot use the header.misc hint to determine whether a | |
257 | * branch stack address is user, kernel, guest, hypervisor. | |
258 | * Branches may straddle the kernel/user/hypervisor boundaries. | |
259 | * Thus, we have to try consecutively until we find a match | |
260 | * or else, the symbol is unknown | |
261 | */ | |
262 | thread__find_addr_location(thread, self, m, MAP__FUNCTION, | |
263 | ip, &al, NULL); | |
264 | if (al.sym) | |
265 | goto found; | |
266 | } | |
267 | found: | |
268 | ams->addr = ip; | |
a68c2c58 | 269 | ams->al_addr = al.addr; |
b5387528 RAV |
270 | ams->sym = al.sym; |
271 | ams->map = al.map; | |
272 | } | |
273 | ||
274 | struct branch_info *machine__resolve_bstack(struct machine *self, | |
275 | struct thread *thr, | |
276 | struct branch_stack *bs) | |
277 | { | |
278 | struct branch_info *bi; | |
279 | unsigned int i; | |
280 | ||
281 | bi = calloc(bs->nr, sizeof(struct branch_info)); | |
282 | if (!bi) | |
283 | return NULL; | |
284 | ||
285 | for (i = 0; i < bs->nr; i++) { | |
286 | ip__resolve_ams(self, thr, &bi[i].to, bs->entries[i].to); | |
287 | ip__resolve_ams(self, thr, &bi[i].from, bs->entries[i].from); | |
288 | bi[i].flags = bs->entries[i].flags; | |
289 | } | |
290 | return bi; | |
291 | } | |
292 | ||
71ad0f5e JO |
293 | static int machine__resolve_callchain_sample(struct machine *machine, |
294 | struct thread *thread, | |
295 | struct ip_callchain *chain, | |
296 | struct symbol **parent) | |
297 | ||
a328626b ACM |
298 | { |
299 | u8 cpumode = PERF_RECORD_MISC_USER; | |
a328626b | 300 | unsigned int i; |
1b3a0e95 | 301 | int err; |
a328626b | 302 | |
47260645 | 303 | callchain_cursor_reset(&callchain_cursor); |
a328626b | 304 | |
114067b6 NK |
305 | if (chain->nr > PERF_MAX_STACK_DEPTH) { |
306 | pr_warning("corrupted callchain. skipping...\n"); | |
307 | return 0; | |
308 | } | |
309 | ||
a328626b | 310 | for (i = 0; i < chain->nr; i++) { |
d797fdc5 | 311 | u64 ip; |
a328626b ACM |
312 | struct addr_location al; |
313 | ||
d797fdc5 SL |
314 | if (callchain_param.order == ORDER_CALLEE) |
315 | ip = chain->ips[i]; | |
316 | else | |
317 | ip = chain->ips[chain->nr - i - 1]; | |
318 | ||
a328626b ACM |
319 | if (ip >= PERF_CONTEXT_MAX) { |
320 | switch (ip) { | |
321 | case PERF_CONTEXT_HV: | |
71ad0f5e JO |
322 | cpumode = PERF_RECORD_MISC_HYPERVISOR; |
323 | break; | |
a328626b | 324 | case PERF_CONTEXT_KERNEL: |
71ad0f5e JO |
325 | cpumode = PERF_RECORD_MISC_KERNEL; |
326 | break; | |
a328626b | 327 | case PERF_CONTEXT_USER: |
71ad0f5e JO |
328 | cpumode = PERF_RECORD_MISC_USER; |
329 | break; | |
a328626b | 330 | default: |
114067b6 NK |
331 | pr_debug("invalid callchain context: " |
332 | "%"PRId64"\n", (s64) ip); | |
333 | /* | |
334 | * It seems the callchain is corrupted. | |
335 | * Discard all. | |
336 | */ | |
337 | callchain_cursor_reset(&callchain_cursor); | |
338 | return 0; | |
a328626b ACM |
339 | } |
340 | continue; | |
341 | } | |
342 | ||
a1645ce1 | 343 | al.filtered = false; |
71ad0f5e | 344 | thread__find_addr_location(thread, machine, cpumode, |
743eb868 | 345 | MAP__FUNCTION, ip, &al, NULL); |
a328626b ACM |
346 | if (al.sym != NULL) { |
347 | if (sort__has_parent && !*parent && | |
348 | symbol__match_parent_regex(al.sym)) | |
349 | *parent = al.sym; | |
d599db3f | 350 | if (!symbol_conf.use_callchain) |
a328626b | 351 | break; |
a328626b | 352 | } |
1b3a0e95 | 353 | |
47260645 | 354 | err = callchain_cursor_append(&callchain_cursor, |
1b3a0e95 FW |
355 | ip, al.map, al.sym); |
356 | if (err) | |
357 | return err; | |
a328626b ACM |
358 | } |
359 | ||
1b3a0e95 | 360 | return 0; |
a328626b | 361 | } |
06aae590 | 362 | |
71ad0f5e JO |
363 | static int unwind_entry(struct unwind_entry *entry, void *arg) |
364 | { | |
365 | struct callchain_cursor *cursor = arg; | |
366 | return callchain_cursor_append(cursor, entry->ip, | |
367 | entry->map, entry->sym); | |
368 | } | |
369 | ||
370 | int machine__resolve_callchain(struct machine *machine, | |
371 | struct perf_evsel *evsel, | |
372 | struct thread *thread, | |
373 | struct perf_sample *sample, | |
374 | struct symbol **parent) | |
375 | ||
376 | { | |
377 | int ret; | |
378 | ||
379 | callchain_cursor_reset(&callchain_cursor); | |
380 | ||
381 | ret = machine__resolve_callchain_sample(machine, thread, | |
382 | sample->callchain, parent); | |
383 | if (ret) | |
384 | return ret; | |
385 | ||
386 | /* Can we do dwarf post unwind? */ | |
387 | if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) && | |
388 | (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER))) | |
389 | return 0; | |
390 | ||
391 | return unwind__get_entries(unwind_entry, &callchain_cursor, machine, | |
392 | thread, evsel->attr.sample_regs_user, | |
393 | sample); | |
394 | ||
395 | } | |
396 | ||
d20deb64 ACM |
397 | static int process_event_synth_tracing_data_stub(union perf_event *event __used, |
398 | struct perf_session *session __used) | |
399 | { | |
400 | dump_printf(": unhandled!\n"); | |
401 | return 0; | |
402 | } | |
403 | ||
10d0f086 ACM |
404 | static int process_event_synth_attr_stub(union perf_event *event __used, |
405 | struct perf_evlist **pevlist __used) | |
406 | { | |
407 | dump_printf(": unhandled!\n"); | |
408 | return 0; | |
409 | } | |
410 | ||
45694aa7 | 411 | static int process_event_sample_stub(struct perf_tool *tool __used, |
d20deb64 | 412 | union perf_event *event __used, |
9e69c210 ACM |
413 | struct perf_sample *sample __used, |
414 | struct perf_evsel *evsel __used, | |
743eb868 | 415 | struct machine *machine __used) |
9e69c210 ACM |
416 | { |
417 | dump_printf(": unhandled!\n"); | |
418 | return 0; | |
419 | } | |
420 | ||
45694aa7 | 421 | static int process_event_stub(struct perf_tool *tool __used, |
d20deb64 | 422 | union perf_event *event __used, |
8d50e5b4 | 423 | struct perf_sample *sample __used, |
743eb868 | 424 | struct machine *machine __used) |
06aae590 ACM |
425 | { |
426 | dump_printf(": unhandled!\n"); | |
427 | return 0; | |
428 | } | |
429 | ||
45694aa7 | 430 | static int process_finished_round_stub(struct perf_tool *tool __used, |
d20deb64 | 431 | union perf_event *event __used, |
743eb868 ACM |
432 | struct perf_session *perf_session __used) |
433 | { | |
434 | dump_printf(": unhandled!\n"); | |
435 | return 0; | |
436 | } | |
437 | ||
45694aa7 | 438 | static int process_event_type_stub(struct perf_tool *tool __used, |
743eb868 | 439 | union perf_event *event __used) |
d6b17beb FW |
440 | { |
441 | dump_printf(": unhandled!\n"); | |
442 | return 0; | |
443 | } | |
444 | ||
45694aa7 | 445 | static int process_finished_round(struct perf_tool *tool, |
d20deb64 ACM |
446 | union perf_event *event, |
447 | struct perf_session *session); | |
d6b17beb | 448 | |
45694aa7 | 449 | static void perf_tool__fill_defaults(struct perf_tool *tool) |
06aae590 | 450 | { |
45694aa7 ACM |
451 | if (tool->sample == NULL) |
452 | tool->sample = process_event_sample_stub; | |
453 | if (tool->mmap == NULL) | |
454 | tool->mmap = process_event_stub; | |
455 | if (tool->comm == NULL) | |
456 | tool->comm = process_event_stub; | |
457 | if (tool->fork == NULL) | |
458 | tool->fork = process_event_stub; | |
459 | if (tool->exit == NULL) | |
460 | tool->exit = process_event_stub; | |
461 | if (tool->lost == NULL) | |
462 | tool->lost = perf_event__process_lost; | |
463 | if (tool->read == NULL) | |
464 | tool->read = process_event_sample_stub; | |
465 | if (tool->throttle == NULL) | |
466 | tool->throttle = process_event_stub; | |
467 | if (tool->unthrottle == NULL) | |
468 | tool->unthrottle = process_event_stub; | |
469 | if (tool->attr == NULL) | |
470 | tool->attr = process_event_synth_attr_stub; | |
471 | if (tool->event_type == NULL) | |
472 | tool->event_type = process_event_type_stub; | |
473 | if (tool->tracing_data == NULL) | |
474 | tool->tracing_data = process_event_synth_tracing_data_stub; | |
475 | if (tool->build_id == NULL) | |
476 | tool->build_id = process_finished_round_stub; | |
477 | if (tool->finished_round == NULL) { | |
478 | if (tool->ordered_samples) | |
479 | tool->finished_round = process_finished_round; | |
d6b17beb | 480 | else |
45694aa7 | 481 | tool->finished_round = process_finished_round_stub; |
d6b17beb | 482 | } |
06aae590 | 483 | } |
80c0120a DA |
484 | |
485 | void mem_bswap_32(void *src, int byte_size) | |
486 | { | |
487 | u32 *m = src; | |
488 | while (byte_size > 0) { | |
489 | *m = bswap_32(*m); | |
490 | byte_size -= sizeof(u32); | |
491 | ++m; | |
492 | } | |
493 | } | |
06aae590 | 494 | |
ba21594c ACM |
495 | void mem_bswap_64(void *src, int byte_size) |
496 | { | |
497 | u64 *m = src; | |
498 | ||
499 | while (byte_size > 0) { | |
500 | *m = bswap_64(*m); | |
501 | byte_size -= sizeof(u64); | |
502 | ++m; | |
503 | } | |
504 | } | |
505 | ||
268fb20f JO |
506 | static void swap_sample_id_all(union perf_event *event, void *data) |
507 | { | |
508 | void *end = (void *) event + event->header.size; | |
509 | int size = end - data; | |
510 | ||
511 | BUG_ON(size % sizeof(u64)); | |
512 | mem_bswap_64(data, size); | |
513 | } | |
514 | ||
515 | static void perf_event__all64_swap(union perf_event *event, | |
516 | bool sample_id_all __used) | |
ba21594c | 517 | { |
8115d60c ACM |
518 | struct perf_event_header *hdr = &event->header; |
519 | mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr)); | |
ba21594c ACM |
520 | } |
521 | ||
268fb20f | 522 | static void perf_event__comm_swap(union perf_event *event, bool sample_id_all) |
ba21594c | 523 | { |
8115d60c ACM |
524 | event->comm.pid = bswap_32(event->comm.pid); |
525 | event->comm.tid = bswap_32(event->comm.tid); | |
268fb20f JO |
526 | |
527 | if (sample_id_all) { | |
528 | void *data = &event->comm.comm; | |
529 | ||
530 | data += ALIGN(strlen(data) + 1, sizeof(u64)); | |
531 | swap_sample_id_all(event, data); | |
532 | } | |
ba21594c ACM |
533 | } |
534 | ||
268fb20f JO |
535 | static void perf_event__mmap_swap(union perf_event *event, |
536 | bool sample_id_all) | |
ba21594c | 537 | { |
8115d60c ACM |
538 | event->mmap.pid = bswap_32(event->mmap.pid); |
539 | event->mmap.tid = bswap_32(event->mmap.tid); | |
540 | event->mmap.start = bswap_64(event->mmap.start); | |
541 | event->mmap.len = bswap_64(event->mmap.len); | |
542 | event->mmap.pgoff = bswap_64(event->mmap.pgoff); | |
268fb20f JO |
543 | |
544 | if (sample_id_all) { | |
545 | void *data = &event->mmap.filename; | |
546 | ||
547 | data += ALIGN(strlen(data) + 1, sizeof(u64)); | |
548 | swap_sample_id_all(event, data); | |
549 | } | |
ba21594c ACM |
550 | } |
551 | ||
268fb20f | 552 | static void perf_event__task_swap(union perf_event *event, bool sample_id_all) |
ba21594c | 553 | { |
8115d60c ACM |
554 | event->fork.pid = bswap_32(event->fork.pid); |
555 | event->fork.tid = bswap_32(event->fork.tid); | |
556 | event->fork.ppid = bswap_32(event->fork.ppid); | |
557 | event->fork.ptid = bswap_32(event->fork.ptid); | |
558 | event->fork.time = bswap_64(event->fork.time); | |
268fb20f JO |
559 | |
560 | if (sample_id_all) | |
561 | swap_sample_id_all(event, &event->fork + 1); | |
ba21594c ACM |
562 | } |
563 | ||
268fb20f | 564 | static void perf_event__read_swap(union perf_event *event, bool sample_id_all) |
ba21594c | 565 | { |
8115d60c ACM |
566 | event->read.pid = bswap_32(event->read.pid); |
567 | event->read.tid = bswap_32(event->read.tid); | |
568 | event->read.value = bswap_64(event->read.value); | |
569 | event->read.time_enabled = bswap_64(event->read.time_enabled); | |
570 | event->read.time_running = bswap_64(event->read.time_running); | |
571 | event->read.id = bswap_64(event->read.id); | |
268fb20f JO |
572 | |
573 | if (sample_id_all) | |
574 | swap_sample_id_all(event, &event->read + 1); | |
ba21594c ACM |
575 | } |
576 | ||
e108c66e JO |
577 | static u8 revbyte(u8 b) |
578 | { | |
579 | int rev = (b >> 4) | ((b & 0xf) << 4); | |
580 | rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2); | |
581 | rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1); | |
582 | return (u8) rev; | |
583 | } | |
584 | ||
585 | /* | |
586 | * XXX this is hack in attempt to carry flags bitfield | |
587 | * throught endian village. ABI says: | |
588 | * | |
589 | * Bit-fields are allocated from right to left (least to most significant) | |
590 | * on little-endian implementations and from left to right (most to least | |
591 | * significant) on big-endian implementations. | |
592 | * | |
593 | * The above seems to be byte specific, so we need to reverse each | |
594 | * byte of the bitfield. 'Internet' also says this might be implementation | |
595 | * specific and we probably need proper fix and carry perf_event_attr | |
596 | * bitfield flags in separate data file FEAT_ section. Thought this seems | |
597 | * to work for now. | |
598 | */ | |
599 | static void swap_bitfield(u8 *p, unsigned len) | |
600 | { | |
601 | unsigned i; | |
602 | ||
603 | for (i = 0; i < len; i++) { | |
604 | *p = revbyte(*p); | |
605 | p++; | |
606 | } | |
607 | } | |
608 | ||
eda3913b DA |
609 | /* exported for swapping attributes in file header */ |
610 | void perf_event__attr_swap(struct perf_event_attr *attr) | |
611 | { | |
612 | attr->type = bswap_32(attr->type); | |
613 | attr->size = bswap_32(attr->size); | |
614 | attr->config = bswap_64(attr->config); | |
615 | attr->sample_period = bswap_64(attr->sample_period); | |
616 | attr->sample_type = bswap_64(attr->sample_type); | |
617 | attr->read_format = bswap_64(attr->read_format); | |
618 | attr->wakeup_events = bswap_32(attr->wakeup_events); | |
619 | attr->bp_type = bswap_32(attr->bp_type); | |
620 | attr->bp_addr = bswap_64(attr->bp_addr); | |
621 | attr->bp_len = bswap_64(attr->bp_len); | |
e108c66e JO |
622 | |
623 | swap_bitfield((u8 *) (&attr->read_format + 1), sizeof(u64)); | |
eda3913b DA |
624 | } |
625 | ||
268fb20f JO |
626 | static void perf_event__hdr_attr_swap(union perf_event *event, |
627 | bool sample_id_all __used) | |
2c46dbb5 TZ |
628 | { |
629 | size_t size; | |
630 | ||
eda3913b | 631 | perf_event__attr_swap(&event->attr.attr); |
2c46dbb5 | 632 | |
8115d60c ACM |
633 | size = event->header.size; |
634 | size -= (void *)&event->attr.id - (void *)event; | |
635 | mem_bswap_64(event->attr.id, size); | |
2c46dbb5 TZ |
636 | } |
637 | ||
268fb20f JO |
638 | static void perf_event__event_type_swap(union perf_event *event, |
639 | bool sample_id_all __used) | |
cd19a035 | 640 | { |
8115d60c ACM |
641 | event->event_type.event_type.event_id = |
642 | bswap_64(event->event_type.event_type.event_id); | |
cd19a035 TZ |
643 | } |
644 | ||
268fb20f JO |
645 | static void perf_event__tracing_data_swap(union perf_event *event, |
646 | bool sample_id_all __used) | |
9215545e | 647 | { |
8115d60c | 648 | event->tracing_data.size = bswap_32(event->tracing_data.size); |
9215545e TZ |
649 | } |
650 | ||
268fb20f JO |
651 | typedef void (*perf_event__swap_op)(union perf_event *event, |
652 | bool sample_id_all); | |
ba21594c | 653 | |
8115d60c ACM |
654 | static perf_event__swap_op perf_event__swap_ops[] = { |
655 | [PERF_RECORD_MMAP] = perf_event__mmap_swap, | |
656 | [PERF_RECORD_COMM] = perf_event__comm_swap, | |
657 | [PERF_RECORD_FORK] = perf_event__task_swap, | |
658 | [PERF_RECORD_EXIT] = perf_event__task_swap, | |
659 | [PERF_RECORD_LOST] = perf_event__all64_swap, | |
660 | [PERF_RECORD_READ] = perf_event__read_swap, | |
661 | [PERF_RECORD_SAMPLE] = perf_event__all64_swap, | |
eda3913b | 662 | [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap, |
8115d60c ACM |
663 | [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap, |
664 | [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap, | |
665 | [PERF_RECORD_HEADER_BUILD_ID] = NULL, | |
666 | [PERF_RECORD_HEADER_MAX] = NULL, | |
ba21594c ACM |
667 | }; |
668 | ||
c61e52ee FW |
669 | struct sample_queue { |
670 | u64 timestamp; | |
e4c2df13 | 671 | u64 file_offset; |
8115d60c | 672 | union perf_event *event; |
c61e52ee FW |
673 | struct list_head list; |
674 | }; | |
675 | ||
020bb75a TG |
676 | static void perf_session_free_sample_buffers(struct perf_session *session) |
677 | { | |
678 | struct ordered_samples *os = &session->ordered_samples; | |
679 | ||
5c891f38 | 680 | while (!list_empty(&os->to_free)) { |
020bb75a TG |
681 | struct sample_queue *sq; |
682 | ||
5c891f38 | 683 | sq = list_entry(os->to_free.next, struct sample_queue, list); |
020bb75a TG |
684 | list_del(&sq->list); |
685 | free(sq); | |
686 | } | |
687 | } | |
688 | ||
cbf41645 | 689 | static int perf_session_deliver_event(struct perf_session *session, |
8115d60c | 690 | union perf_event *event, |
8d50e5b4 | 691 | struct perf_sample *sample, |
45694aa7 | 692 | struct perf_tool *tool, |
f74725dc | 693 | u64 file_offset); |
cbf41645 | 694 | |
c61e52ee | 695 | static void flush_sample_queue(struct perf_session *s, |
45694aa7 | 696 | struct perf_tool *tool) |
c61e52ee | 697 | { |
a1225dec TG |
698 | struct ordered_samples *os = &s->ordered_samples; |
699 | struct list_head *head = &os->samples; | |
c61e52ee | 700 | struct sample_queue *tmp, *iter; |
8d50e5b4 | 701 | struct perf_sample sample; |
a1225dec TG |
702 | u64 limit = os->next_flush; |
703 | u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL; | |
88660563 | 704 | unsigned idx = 0, progress_next = os->nr_samples / 16; |
5538beca | 705 | int ret; |
c61e52ee | 706 | |
45694aa7 | 707 | if (!tool->ordered_samples || !limit) |
c61e52ee FW |
708 | return; |
709 | ||
710 | list_for_each_entry_safe(iter, tmp, head, list) { | |
711 | if (iter->timestamp > limit) | |
a1225dec | 712 | break; |
c61e52ee | 713 | |
cb0b29e0 ACM |
714 | ret = perf_evlist__parse_sample(s->evlist, iter->event, &sample, |
715 | s->header.needs_swap); | |
5538beca FW |
716 | if (ret) |
717 | pr_err("Can't parse sample, err = %d\n", ret); | |
718 | else | |
45694aa7 | 719 | perf_session_deliver_event(s, iter->event, &sample, tool, |
5538beca | 720 | iter->file_offset); |
c61e52ee | 721 | |
a1225dec | 722 | os->last_flush = iter->timestamp; |
c61e52ee | 723 | list_del(&iter->list); |
020bb75a | 724 | list_add(&iter->list, &os->sample_cache); |
88660563 ACM |
725 | if (++idx >= progress_next) { |
726 | progress_next += os->nr_samples / 16; | |
727 | ui_progress__update(idx, os->nr_samples, | |
728 | "Processing time ordered events..."); | |
729 | } | |
c61e52ee | 730 | } |
a1225dec TG |
731 | |
732 | if (list_empty(head)) { | |
733 | os->last_sample = NULL; | |
734 | } else if (last_ts <= limit) { | |
735 | os->last_sample = | |
736 | list_entry(head->prev, struct sample_queue, list); | |
737 | } | |
88660563 ACM |
738 | |
739 | os->nr_samples = 0; | |
c61e52ee FW |
740 | } |
741 | ||
d6b17beb FW |
742 | /* |
743 | * When perf record finishes a pass on every buffers, it records this pseudo | |
744 | * event. | |
745 | * We record the max timestamp t found in the pass n. | |
746 | * Assuming these timestamps are monotonic across cpus, we know that if | |
747 | * a buffer still has events with timestamps below t, they will be all | |
748 | * available and then read in the pass n + 1. | |
749 | * Hence when we start to read the pass n + 2, we can safely flush every | |
750 | * events with timestamps below t. | |
751 | * | |
752 | * ============ PASS n ================= | |
753 | * CPU 0 | CPU 1 | |
754 | * | | |
755 | * cnt1 timestamps | cnt2 timestamps | |
756 | * 1 | 2 | |
757 | * 2 | 3 | |
758 | * - | 4 <--- max recorded | |
759 | * | |
760 | * ============ PASS n + 1 ============== | |
761 | * CPU 0 | CPU 1 | |
762 | * | | |
763 | * cnt1 timestamps | cnt2 timestamps | |
764 | * 3 | 5 | |
765 | * 4 | 6 | |
766 | * 5 | 7 <---- max recorded | |
767 | * | |
768 | * Flush every events below timestamp 4 | |
769 | * | |
770 | * ============ PASS n + 2 ============== | |
771 | * CPU 0 | CPU 1 | |
772 | * | | |
773 | * cnt1 timestamps | cnt2 timestamps | |
774 | * 6 | 8 | |
775 | * 7 | 9 | |
776 | * - | 10 | |
777 | * | |
778 | * Flush every events below timestamp 7 | |
779 | * etc... | |
780 | */ | |
45694aa7 | 781 | static int process_finished_round(struct perf_tool *tool, |
d20deb64 ACM |
782 | union perf_event *event __used, |
783 | struct perf_session *session) | |
d6b17beb | 784 | { |
45694aa7 | 785 | flush_sample_queue(session, tool); |
d6b17beb FW |
786 | session->ordered_samples.next_flush = session->ordered_samples.max_timestamp; |
787 | ||
788 | return 0; | |
789 | } | |
790 | ||
c61e52ee | 791 | /* The queue is ordered by time */ |
cbf41645 | 792 | static void __queue_event(struct sample_queue *new, struct perf_session *s) |
c61e52ee | 793 | { |
a1225dec TG |
794 | struct ordered_samples *os = &s->ordered_samples; |
795 | struct sample_queue *sample = os->last_sample; | |
796 | u64 timestamp = new->timestamp; | |
797 | struct list_head *p; | |
c61e52ee | 798 | |
88660563 | 799 | ++os->nr_samples; |
a1225dec | 800 | os->last_sample = new; |
c61e52ee | 801 | |
a1225dec TG |
802 | if (!sample) { |
803 | list_add(&new->list, &os->samples); | |
804 | os->max_timestamp = timestamp; | |
c61e52ee FW |
805 | return; |
806 | } | |
807 | ||
808 | /* | |
a1225dec TG |
809 | * last_sample might point to some random place in the list as it's |
810 | * the last queued event. We expect that the new event is close to | |
811 | * this. | |
c61e52ee | 812 | */ |
a1225dec TG |
813 | if (sample->timestamp <= timestamp) { |
814 | while (sample->timestamp <= timestamp) { | |
815 | p = sample->list.next; | |
816 | if (p == &os->samples) { | |
817 | list_add_tail(&new->list, &os->samples); | |
818 | os->max_timestamp = timestamp; | |
819 | return; | |
820 | } | |
821 | sample = list_entry(p, struct sample_queue, list); | |
822 | } | |
823 | list_add_tail(&new->list, &sample->list); | |
824 | } else { | |
825 | while (sample->timestamp > timestamp) { | |
826 | p = sample->list.prev; | |
827 | if (p == &os->samples) { | |
828 | list_add(&new->list, &os->samples); | |
829 | return; | |
830 | } | |
831 | sample = list_entry(p, struct sample_queue, list); | |
832 | } | |
833 | list_add(&new->list, &sample->list); | |
834 | } | |
c61e52ee FW |
835 | } |
836 | ||
5c891f38 TG |
837 | #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue)) |
838 | ||
8115d60c | 839 | static int perf_session_queue_event(struct perf_session *s, union perf_event *event, |
8d50e5b4 | 840 | struct perf_sample *sample, u64 file_offset) |
c61e52ee | 841 | { |
5c891f38 TG |
842 | struct ordered_samples *os = &s->ordered_samples; |
843 | struct list_head *sc = &os->sample_cache; | |
8d50e5b4 | 844 | u64 timestamp = sample->time; |
c61e52ee | 845 | struct sample_queue *new; |
c61e52ee | 846 | |
79a14c1f | 847 | if (!timestamp || timestamp == ~0ULL) |
cbf41645 TG |
848 | return -ETIME; |
849 | ||
c61e52ee FW |
850 | if (timestamp < s->ordered_samples.last_flush) { |
851 | printf("Warning: Timestamp below last timeslice flush\n"); | |
852 | return -EINVAL; | |
853 | } | |
854 | ||
020bb75a TG |
855 | if (!list_empty(sc)) { |
856 | new = list_entry(sc->next, struct sample_queue, list); | |
857 | list_del(&new->list); | |
5c891f38 TG |
858 | } else if (os->sample_buffer) { |
859 | new = os->sample_buffer + os->sample_buffer_idx; | |
860 | if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER) | |
861 | os->sample_buffer = NULL; | |
020bb75a | 862 | } else { |
5c891f38 TG |
863 | os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new)); |
864 | if (!os->sample_buffer) | |
020bb75a | 865 | return -ENOMEM; |
5c891f38 TG |
866 | list_add(&os->sample_buffer->list, &os->to_free); |
867 | os->sample_buffer_idx = 2; | |
868 | new = os->sample_buffer + 1; | |
020bb75a | 869 | } |
c61e52ee FW |
870 | |
871 | new->timestamp = timestamp; | |
e4c2df13 | 872 | new->file_offset = file_offset; |
fe174207 | 873 | new->event = event; |
c61e52ee | 874 | |
cbf41645 | 875 | __queue_event(new, s); |
640c03ce | 876 | |
640c03ce ACM |
877 | return 0; |
878 | } | |
c61e52ee | 879 | |
8d50e5b4 | 880 | static void callchain__printf(struct perf_sample *sample) |
640c03ce ACM |
881 | { |
882 | unsigned int i; | |
c61e52ee | 883 | |
9486aa38 | 884 | printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr); |
640c03ce ACM |
885 | |
886 | for (i = 0; i < sample->callchain->nr; i++) | |
9486aa38 ACM |
887 | printf("..... %2d: %016" PRIx64 "\n", |
888 | i, sample->callchain->ips[i]); | |
c61e52ee FW |
889 | } |
890 | ||
b5387528 RAV |
891 | static void branch_stack__printf(struct perf_sample *sample) |
892 | { | |
893 | uint64_t i; | |
894 | ||
895 | printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr); | |
896 | ||
897 | for (i = 0; i < sample->branch_stack->nr; i++) | |
898 | printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n", | |
899 | i, sample->branch_stack->entries[i].from, | |
900 | sample->branch_stack->entries[i].to); | |
901 | } | |
902 | ||
0f6a3015 JO |
903 | static void regs_dump__printf(u64 mask, u64 *regs) |
904 | { | |
905 | unsigned rid, i = 0; | |
906 | ||
907 | for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) { | |
908 | u64 val = regs[i++]; | |
909 | ||
910 | printf(".... %-5s 0x%" PRIx64 "\n", | |
911 | perf_reg_name(rid), val); | |
912 | } | |
913 | } | |
914 | ||
915 | static void regs_user__printf(struct perf_sample *sample, u64 mask) | |
916 | { | |
917 | struct regs_dump *user_regs = &sample->user_regs; | |
918 | ||
919 | if (user_regs->regs) { | |
920 | printf("... user regs: mask 0x%" PRIx64 "\n", mask); | |
921 | regs_dump__printf(mask, user_regs->regs); | |
922 | } | |
923 | } | |
924 | ||
925 | static void stack_user__printf(struct stack_dump *dump) | |
926 | { | |
927 | printf("... ustack: size %" PRIu64 ", offset 0x%x\n", | |
928 | dump->size, dump->offset); | |
929 | } | |
930 | ||
9c90a61c | 931 | static void perf_session__print_tstamp(struct perf_session *session, |
8115d60c | 932 | union perf_event *event, |
8d50e5b4 | 933 | struct perf_sample *sample) |
9c90a61c | 934 | { |
7f3be652 ACM |
935 | u64 sample_type = perf_evlist__sample_type(session->evlist); |
936 | ||
9c90a61c | 937 | if (event->header.type != PERF_RECORD_SAMPLE && |
5e562474 | 938 | !perf_evlist__sample_id_all(session->evlist)) { |
9c90a61c ACM |
939 | fputs("-1 -1 ", stdout); |
940 | return; | |
941 | } | |
942 | ||
7f3be652 | 943 | if ((sample_type & PERF_SAMPLE_CPU)) |
9c90a61c ACM |
944 | printf("%u ", sample->cpu); |
945 | ||
7f3be652 | 946 | if (sample_type & PERF_SAMPLE_TIME) |
9486aa38 | 947 | printf("%" PRIu64 " ", sample->time); |
9c90a61c ACM |
948 | } |
949 | ||
8115d60c | 950 | static void dump_event(struct perf_session *session, union perf_event *event, |
8d50e5b4 | 951 | u64 file_offset, struct perf_sample *sample) |
9aefcab0 TG |
952 | { |
953 | if (!dump_trace) | |
954 | return; | |
955 | ||
9486aa38 ACM |
956 | printf("\n%#" PRIx64 " [%#x]: event: %d\n", |
957 | file_offset, event->header.size, event->header.type); | |
9aefcab0 TG |
958 | |
959 | trace_event(event); | |
960 | ||
961 | if (sample) | |
962 | perf_session__print_tstamp(session, event, sample); | |
963 | ||
9486aa38 | 964 | printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset, |
8115d60c | 965 | event->header.size, perf_event__name(event->header.type)); |
9aefcab0 TG |
966 | } |
967 | ||
0f6a3015 | 968 | static void dump_sample(struct perf_evsel *evsel, union perf_event *event, |
8d50e5b4 | 969 | struct perf_sample *sample) |
9aefcab0 | 970 | { |
7f3be652 ACM |
971 | u64 sample_type; |
972 | ||
ddbc24b7 ACM |
973 | if (!dump_trace) |
974 | return; | |
975 | ||
7cec0922 | 976 | printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n", |
9486aa38 | 977 | event->header.misc, sample->pid, sample->tid, sample->ip, |
7cec0922 | 978 | sample->period, sample->addr); |
9aefcab0 | 979 | |
0f6a3015 | 980 | sample_type = evsel->attr.sample_type; |
7f3be652 ACM |
981 | |
982 | if (sample_type & PERF_SAMPLE_CALLCHAIN) | |
ddbc24b7 | 983 | callchain__printf(sample); |
b5387528 | 984 | |
7f3be652 | 985 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) |
b5387528 | 986 | branch_stack__printf(sample); |
0f6a3015 JO |
987 | |
988 | if (sample_type & PERF_SAMPLE_REGS_USER) | |
989 | regs_user__printf(sample, evsel->attr.sample_regs_user); | |
990 | ||
991 | if (sample_type & PERF_SAMPLE_STACK_USER) | |
992 | stack_user__printf(&sample->user_stack); | |
9aefcab0 TG |
993 | } |
994 | ||
743eb868 ACM |
995 | static struct machine * |
996 | perf_session__find_machine_for_cpumode(struct perf_session *session, | |
997 | union perf_event *event) | |
998 | { | |
999 | const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; | |
1000 | ||
7c0f4a41 DA |
1001 | if (perf_guest && |
1002 | ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) || | |
1003 | (cpumode == PERF_RECORD_MISC_GUEST_USER))) { | |
7fb0a5ee ND |
1004 | u32 pid; |
1005 | ||
1006 | if (event->header.type == PERF_RECORD_MMAP) | |
1007 | pid = event->mmap.pid; | |
1008 | else | |
1009 | pid = event->ip.pid; | |
1010 | ||
207b5792 | 1011 | return perf_session__findnew_machine(session, pid); |
7fb0a5ee | 1012 | } |
743eb868 ACM |
1013 | |
1014 | return perf_session__find_host_machine(session); | |
1015 | } | |
1016 | ||
cbf41645 | 1017 | static int perf_session_deliver_event(struct perf_session *session, |
8115d60c | 1018 | union perf_event *event, |
8d50e5b4 | 1019 | struct perf_sample *sample, |
45694aa7 | 1020 | struct perf_tool *tool, |
532e7269 | 1021 | u64 file_offset) |
cbf41645 | 1022 | { |
9e69c210 | 1023 | struct perf_evsel *evsel; |
743eb868 | 1024 | struct machine *machine; |
9e69c210 | 1025 | |
532e7269 TG |
1026 | dump_event(session, event, file_offset, sample); |
1027 | ||
7b27509f ACM |
1028 | evsel = perf_evlist__id2evsel(session->evlist, sample->id); |
1029 | if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) { | |
1030 | /* | |
1031 | * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here | |
1032 | * because the tools right now may apply filters, discarding | |
1033 | * some of the samples. For consistency, in the future we | |
1034 | * should have something like nr_filtered_samples and remove | |
1035 | * the sample->period from total_sample_period, etc, KISS for | |
1036 | * now tho. | |
1037 | * | |
1038 | * Also testing against NULL allows us to handle files without | |
1039 | * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the | |
1040 | * future probably it'll be a good idea to restrict event | |
1041 | * processing via perf_session to files with both set. | |
1042 | */ | |
1043 | hists__inc_nr_events(&evsel->hists, event->header.type); | |
1044 | } | |
1045 | ||
743eb868 ACM |
1046 | machine = perf_session__find_machine_for_cpumode(session, event); |
1047 | ||
cbf41645 TG |
1048 | switch (event->header.type) { |
1049 | case PERF_RECORD_SAMPLE: | |
0f6a3015 | 1050 | dump_sample(evsel, event, sample); |
9e69c210 ACM |
1051 | if (evsel == NULL) { |
1052 | ++session->hists.stats.nr_unknown_id; | |
6782206b | 1053 | return 0; |
9e69c210 | 1054 | } |
0c095715 JR |
1055 | if (machine == NULL) { |
1056 | ++session->hists.stats.nr_unprocessable_samples; | |
6782206b | 1057 | return 0; |
0c095715 | 1058 | } |
45694aa7 | 1059 | return tool->sample(tool, event, sample, evsel, machine); |
cbf41645 | 1060 | case PERF_RECORD_MMAP: |
45694aa7 | 1061 | return tool->mmap(tool, event, sample, machine); |
cbf41645 | 1062 | case PERF_RECORD_COMM: |
45694aa7 | 1063 | return tool->comm(tool, event, sample, machine); |
cbf41645 | 1064 | case PERF_RECORD_FORK: |
45694aa7 | 1065 | return tool->fork(tool, event, sample, machine); |
cbf41645 | 1066 | case PERF_RECORD_EXIT: |
45694aa7 | 1067 | return tool->exit(tool, event, sample, machine); |
cbf41645 | 1068 | case PERF_RECORD_LOST: |
45694aa7 | 1069 | if (tool->lost == perf_event__process_lost) |
743eb868 | 1070 | session->hists.stats.total_lost += event->lost.lost; |
45694aa7 | 1071 | return tool->lost(tool, event, sample, machine); |
cbf41645 | 1072 | case PERF_RECORD_READ: |
45694aa7 | 1073 | return tool->read(tool, event, sample, evsel, machine); |
cbf41645 | 1074 | case PERF_RECORD_THROTTLE: |
45694aa7 | 1075 | return tool->throttle(tool, event, sample, machine); |
cbf41645 | 1076 | case PERF_RECORD_UNTHROTTLE: |
45694aa7 | 1077 | return tool->unthrottle(tool, event, sample, machine); |
cbf41645 TG |
1078 | default: |
1079 | ++session->hists.stats.nr_unknown_events; | |
1080 | return -1; | |
1081 | } | |
1082 | } | |
1083 | ||
3dfc2c0a | 1084 | static int perf_session__preprocess_sample(struct perf_session *session, |
8115d60c | 1085 | union perf_event *event, struct perf_sample *sample) |
3dfc2c0a TG |
1086 | { |
1087 | if (event->header.type != PERF_RECORD_SAMPLE || | |
7f3be652 | 1088 | !(perf_evlist__sample_type(session->evlist) & PERF_SAMPLE_CALLCHAIN)) |
3dfc2c0a TG |
1089 | return 0; |
1090 | ||
1091 | if (!ip_callchain__valid(sample->callchain, event)) { | |
1092 | pr_debug("call-chain problem with event, skipping it.\n"); | |
1093 | ++session->hists.stats.nr_invalid_chains; | |
1094 | session->hists.stats.total_invalid_chains += sample->period; | |
1095 | return -EINVAL; | |
1096 | } | |
1097 | return 0; | |
1098 | } | |
1099 | ||
8115d60c | 1100 | static int perf_session__process_user_event(struct perf_session *session, union perf_event *event, |
45694aa7 | 1101 | struct perf_tool *tool, u64 file_offset) |
06aae590 | 1102 | { |
10d0f086 ACM |
1103 | int err; |
1104 | ||
ba74f064 | 1105 | dump_event(session, event, file_offset, NULL); |
06aae590 | 1106 | |
cbf41645 | 1107 | /* These events are processed right away */ |
06aae590 | 1108 | switch (event->header.type) { |
2c46dbb5 | 1109 | case PERF_RECORD_HEADER_ATTR: |
45694aa7 | 1110 | err = tool->attr(event, &session->evlist); |
10d0f086 | 1111 | if (err == 0) |
7b56cce2 | 1112 | perf_session__set_id_hdr_size(session); |
10d0f086 | 1113 | return err; |
cd19a035 | 1114 | case PERF_RECORD_HEADER_EVENT_TYPE: |
45694aa7 | 1115 | return tool->event_type(tool, event); |
9215545e TZ |
1116 | case PERF_RECORD_HEADER_TRACING_DATA: |
1117 | /* setup for reading amidst mmap */ | |
cbf41645 | 1118 | lseek(session->fd, file_offset, SEEK_SET); |
45694aa7 | 1119 | return tool->tracing_data(event, session); |
c7929e47 | 1120 | case PERF_RECORD_HEADER_BUILD_ID: |
45694aa7 | 1121 | return tool->build_id(tool, event, session); |
d6b17beb | 1122 | case PERF_RECORD_FINISHED_ROUND: |
45694aa7 | 1123 | return tool->finished_round(tool, event, session); |
06aae590 | 1124 | default: |
ba74f064 | 1125 | return -EINVAL; |
06aae590 | 1126 | } |
ba74f064 TG |
1127 | } |
1128 | ||
268fb20f JO |
1129 | static void event_swap(union perf_event *event, bool sample_id_all) |
1130 | { | |
1131 | perf_event__swap_op swap; | |
1132 | ||
1133 | swap = perf_event__swap_ops[event->header.type]; | |
1134 | if (swap) | |
1135 | swap(event, sample_id_all); | |
1136 | } | |
1137 | ||
ba74f064 | 1138 | static int perf_session__process_event(struct perf_session *session, |
8115d60c | 1139 | union perf_event *event, |
45694aa7 | 1140 | struct perf_tool *tool, |
ba74f064 TG |
1141 | u64 file_offset) |
1142 | { | |
8d50e5b4 | 1143 | struct perf_sample sample; |
ba74f064 TG |
1144 | int ret; |
1145 | ||
268fb20f | 1146 | if (session->header.needs_swap) |
5e562474 | 1147 | event_swap(event, perf_evlist__sample_id_all(session->evlist)); |
ba74f064 TG |
1148 | |
1149 | if (event->header.type >= PERF_RECORD_HEADER_MAX) | |
1150 | return -EINVAL; | |
1151 | ||
1152 | hists__inc_nr_events(&session->hists, event->header.type); | |
1153 | ||
1154 | if (event->header.type >= PERF_RECORD_USER_TYPE_START) | |
45694aa7 | 1155 | return perf_session__process_user_event(session, event, tool, file_offset); |
cbf41645 | 1156 | |
3dfc2c0a TG |
1157 | /* |
1158 | * For all kernel events we get the sample data | |
1159 | */ | |
cb0b29e0 ACM |
1160 | ret = perf_evlist__parse_sample(session->evlist, event, &sample, |
1161 | session->header.needs_swap); | |
5538beca FW |
1162 | if (ret) |
1163 | return ret; | |
3dfc2c0a TG |
1164 | |
1165 | /* Preprocess sample records - precheck callchains */ | |
1166 | if (perf_session__preprocess_sample(session, event, &sample)) | |
1167 | return 0; | |
1168 | ||
45694aa7 | 1169 | if (tool->ordered_samples) { |
e4c2df13 TG |
1170 | ret = perf_session_queue_event(session, event, &sample, |
1171 | file_offset); | |
cbf41645 TG |
1172 | if (ret != -ETIME) |
1173 | return ret; | |
1174 | } | |
1175 | ||
45694aa7 | 1176 | return perf_session_deliver_event(session, event, &sample, tool, |
f74725dc | 1177 | file_offset); |
06aae590 ACM |
1178 | } |
1179 | ||
ba21594c ACM |
1180 | void perf_event_header__bswap(struct perf_event_header *self) |
1181 | { | |
1182 | self->type = bswap_32(self->type); | |
1183 | self->misc = bswap_16(self->misc); | |
1184 | self->size = bswap_16(self->size); | |
1185 | } | |
1186 | ||
b424eba2 ACM |
1187 | struct thread *perf_session__findnew(struct perf_session *session, pid_t pid) |
1188 | { | |
1189 | return machine__findnew_thread(&session->host_machine, pid); | |
1190 | } | |
1191 | ||
06aae590 ACM |
1192 | static struct thread *perf_session__register_idle_thread(struct perf_session *self) |
1193 | { | |
1194 | struct thread *thread = perf_session__findnew(self, 0); | |
1195 | ||
1196 | if (thread == NULL || thread__set_comm(thread, "swapper")) { | |
1197 | pr_err("problem inserting idle task.\n"); | |
1198 | thread = NULL; | |
1199 | } | |
1200 | ||
1201 | return thread; | |
1202 | } | |
1203 | ||
11095994 | 1204 | static void perf_session__warn_about_errors(const struct perf_session *session, |
45694aa7 | 1205 | const struct perf_tool *tool) |
11095994 | 1206 | { |
45694aa7 | 1207 | if (tool->lost == perf_event__process_lost && |
7b27509f ACM |
1208 | session->hists.stats.nr_events[PERF_RECORD_LOST] != 0) { |
1209 | ui__warning("Processed %d events and lost %d chunks!\n\n" | |
1210 | "Check IO/CPU overload!\n\n", | |
1211 | session->hists.stats.nr_events[0], | |
1212 | session->hists.stats.nr_events[PERF_RECORD_LOST]); | |
11095994 ACM |
1213 | } |
1214 | ||
1215 | if (session->hists.stats.nr_unknown_events != 0) { | |
1216 | ui__warning("Found %u unknown events!\n\n" | |
1217 | "Is this an older tool processing a perf.data " | |
1218 | "file generated by a more recent tool?\n\n" | |
1219 | "If that is not the case, consider " | |
1220 | "reporting to linux-kernel@vger.kernel.org.\n\n", | |
1221 | session->hists.stats.nr_unknown_events); | |
1222 | } | |
1223 | ||
9e69c210 ACM |
1224 | if (session->hists.stats.nr_unknown_id != 0) { |
1225 | ui__warning("%u samples with id not present in the header\n", | |
1226 | session->hists.stats.nr_unknown_id); | |
1227 | } | |
1228 | ||
11095994 ACM |
1229 | if (session->hists.stats.nr_invalid_chains != 0) { |
1230 | ui__warning("Found invalid callchains!\n\n" | |
1231 | "%u out of %u events were discarded for this reason.\n\n" | |
1232 | "Consider reporting to linux-kernel@vger.kernel.org.\n\n", | |
1233 | session->hists.stats.nr_invalid_chains, | |
1234 | session->hists.stats.nr_events[PERF_RECORD_SAMPLE]); | |
1235 | } | |
0c095715 JR |
1236 | |
1237 | if (session->hists.stats.nr_unprocessable_samples != 0) { | |
1238 | ui__warning("%u unprocessable samples recorded.\n" | |
1239 | "Do you have a KVM guest running and not using 'perf kvm'?\n", | |
1240 | session->hists.stats.nr_unprocessable_samples); | |
1241 | } | |
11095994 ACM |
1242 | } |
1243 | ||
8dc58101 TZ |
1244 | #define session_done() (*(volatile int *)(&session_done)) |
1245 | volatile int session_done; | |
1246 | ||
1247 | static int __perf_session__process_pipe_events(struct perf_session *self, | |
45694aa7 | 1248 | struct perf_tool *tool) |
8dc58101 | 1249 | { |
444d2866 SE |
1250 | union perf_event *event; |
1251 | uint32_t size, cur_size = 0; | |
1252 | void *buf = NULL; | |
8dc58101 TZ |
1253 | int skip = 0; |
1254 | u64 head; | |
1255 | int err; | |
1256 | void *p; | |
1257 | ||
45694aa7 | 1258 | perf_tool__fill_defaults(tool); |
8dc58101 TZ |
1259 | |
1260 | head = 0; | |
444d2866 SE |
1261 | cur_size = sizeof(union perf_event); |
1262 | ||
1263 | buf = malloc(cur_size); | |
1264 | if (!buf) | |
1265 | return -errno; | |
8dc58101 | 1266 | more: |
444d2866 SE |
1267 | event = buf; |
1268 | err = readn(self->fd, event, sizeof(struct perf_event_header)); | |
8dc58101 TZ |
1269 | if (err <= 0) { |
1270 | if (err == 0) | |
1271 | goto done; | |
1272 | ||
1273 | pr_err("failed to read event header\n"); | |
1274 | goto out_err; | |
1275 | } | |
1276 | ||
1277 | if (self->header.needs_swap) | |
444d2866 | 1278 | perf_event_header__bswap(&event->header); |
8dc58101 | 1279 | |
444d2866 | 1280 | size = event->header.size; |
8dc58101 TZ |
1281 | if (size == 0) |
1282 | size = 8; | |
1283 | ||
444d2866 SE |
1284 | if (size > cur_size) { |
1285 | void *new = realloc(buf, size); | |
1286 | if (!new) { | |
1287 | pr_err("failed to allocate memory to read event\n"); | |
1288 | goto out_err; | |
1289 | } | |
1290 | buf = new; | |
1291 | cur_size = size; | |
1292 | event = buf; | |
1293 | } | |
1294 | p = event; | |
8dc58101 TZ |
1295 | p += sizeof(struct perf_event_header); |
1296 | ||
794e43b5 | 1297 | if (size - sizeof(struct perf_event_header)) { |
1e7972cc | 1298 | err = readn(self->fd, p, size - sizeof(struct perf_event_header)); |
794e43b5 TZ |
1299 | if (err <= 0) { |
1300 | if (err == 0) { | |
1301 | pr_err("unexpected end of event stream\n"); | |
1302 | goto done; | |
1303 | } | |
8dc58101 | 1304 | |
794e43b5 TZ |
1305 | pr_err("failed to read event data\n"); |
1306 | goto out_err; | |
1307 | } | |
8dc58101 TZ |
1308 | } |
1309 | ||
444d2866 | 1310 | if ((skip = perf_session__process_event(self, event, tool, head)) < 0) { |
9389a460 | 1311 | pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n", |
444d2866 | 1312 | head, event->header.size, event->header.type); |
9389a460 JO |
1313 | err = -EINVAL; |
1314 | goto out_err; | |
8dc58101 TZ |
1315 | } |
1316 | ||
1317 | head += size; | |
1318 | ||
8dc58101 TZ |
1319 | if (skip > 0) |
1320 | head += skip; | |
1321 | ||
1322 | if (!session_done()) | |
1323 | goto more; | |
1324 | done: | |
1325 | err = 0; | |
1326 | out_err: | |
444d2866 | 1327 | free(buf); |
45694aa7 | 1328 | perf_session__warn_about_errors(self, tool); |
020bb75a | 1329 | perf_session_free_sample_buffers(self); |
8dc58101 TZ |
1330 | return err; |
1331 | } | |
1332 | ||
998bedc8 FW |
1333 | static union perf_event * |
1334 | fetch_mmaped_event(struct perf_session *session, | |
1335 | u64 head, size_t mmap_size, char *buf) | |
1336 | { | |
1337 | union perf_event *event; | |
1338 | ||
1339 | /* | |
1340 | * Ensure we have enough space remaining to read | |
1341 | * the size of the event in the headers. | |
1342 | */ | |
1343 | if (head + sizeof(event->header) > mmap_size) | |
1344 | return NULL; | |
1345 | ||
1346 | event = (union perf_event *)(buf + head); | |
1347 | ||
1348 | if (session->header.needs_swap) | |
1349 | perf_event_header__bswap(&event->header); | |
1350 | ||
1351 | if (head + event->header.size > mmap_size) | |
1352 | return NULL; | |
1353 | ||
1354 | return event; | |
1355 | } | |
1356 | ||
0331ee0c | 1357 | int __perf_session__process_events(struct perf_session *session, |
6122e4e4 | 1358 | u64 data_offset, u64 data_size, |
45694aa7 | 1359 | u64 file_size, struct perf_tool *tool) |
06aae590 | 1360 | { |
55b44629 | 1361 | u64 head, page_offset, file_offset, file_pos, progress_next; |
fe174207 | 1362 | int err, mmap_prot, mmap_flags, map_idx = 0; |
55b44629 | 1363 | size_t page_size, mmap_size; |
fe174207 | 1364 | char *buf, *mmaps[8]; |
8115d60c | 1365 | union perf_event *event; |
06aae590 | 1366 | uint32_t size; |
0331ee0c | 1367 | |
45694aa7 | 1368 | perf_tool__fill_defaults(tool); |
06aae590 | 1369 | |
1b75962e | 1370 | page_size = sysconf(_SC_PAGESIZE); |
06aae590 | 1371 | |
0331ee0c TG |
1372 | page_offset = page_size * (data_offset / page_size); |
1373 | file_offset = page_offset; | |
1374 | head = data_offset - page_offset; | |
06aae590 | 1375 | |
d6513281 TG |
1376 | if (data_offset + data_size < file_size) |
1377 | file_size = data_offset + data_size; | |
1378 | ||
55b44629 | 1379 | progress_next = file_size / 16; |
55b44629 TG |
1380 | |
1381 | mmap_size = session->mmap_window; | |
1382 | if (mmap_size > file_size) | |
1383 | mmap_size = file_size; | |
1384 | ||
fe174207 TG |
1385 | memset(mmaps, 0, sizeof(mmaps)); |
1386 | ||
ba21594c ACM |
1387 | mmap_prot = PROT_READ; |
1388 | mmap_flags = MAP_SHARED; | |
1389 | ||
0331ee0c | 1390 | if (session->header.needs_swap) { |
ba21594c ACM |
1391 | mmap_prot |= PROT_WRITE; |
1392 | mmap_flags = MAP_PRIVATE; | |
1393 | } | |
06aae590 | 1394 | remap: |
55b44629 TG |
1395 | buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd, |
1396 | file_offset); | |
06aae590 ACM |
1397 | if (buf == MAP_FAILED) { |
1398 | pr_err("failed to mmap file\n"); | |
1399 | err = -errno; | |
1400 | goto out_err; | |
1401 | } | |
fe174207 TG |
1402 | mmaps[map_idx] = buf; |
1403 | map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1); | |
d6513281 | 1404 | file_pos = file_offset + head; |
06aae590 ACM |
1405 | |
1406 | more: | |
998bedc8 FW |
1407 | event = fetch_mmaped_event(session, head, mmap_size, buf); |
1408 | if (!event) { | |
fe174207 TG |
1409 | if (mmaps[map_idx]) { |
1410 | munmap(mmaps[map_idx], mmap_size); | |
1411 | mmaps[map_idx] = NULL; | |
1412 | } | |
06aae590 | 1413 | |
0331ee0c TG |
1414 | page_offset = page_size * (head / page_size); |
1415 | file_offset += page_offset; | |
1416 | head -= page_offset; | |
06aae590 ACM |
1417 | goto remap; |
1418 | } | |
1419 | ||
1420 | size = event->header.size; | |
1421 | ||
d6513281 | 1422 | if (size == 0 || |
45694aa7 | 1423 | perf_session__process_event(session, event, tool, file_pos) < 0) { |
9389a460 JO |
1424 | pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n", |
1425 | file_offset + head, event->header.size, | |
1426 | event->header.type); | |
1427 | err = -EINVAL; | |
1428 | goto out_err; | |
06aae590 ACM |
1429 | } |
1430 | ||
1431 | head += size; | |
d6513281 | 1432 | file_pos += size; |
06aae590 | 1433 | |
55b44629 TG |
1434 | if (file_pos >= progress_next) { |
1435 | progress_next += file_size / 16; | |
ca59bcbc ACM |
1436 | ui_progress__update(file_pos, file_size, |
1437 | "Processing events..."); | |
55b44629 TG |
1438 | } |
1439 | ||
d6513281 | 1440 | if (file_pos < file_size) |
06aae590 | 1441 | goto more; |
d6513281 | 1442 | |
06aae590 | 1443 | err = 0; |
c61e52ee | 1444 | /* do the final flush for ordered samples */ |
0331ee0c | 1445 | session->ordered_samples.next_flush = ULLONG_MAX; |
45694aa7 | 1446 | flush_sample_queue(session, tool); |
06aae590 | 1447 | out_err: |
45694aa7 | 1448 | perf_session__warn_about_errors(session, tool); |
020bb75a | 1449 | perf_session_free_sample_buffers(session); |
06aae590 ACM |
1450 | return err; |
1451 | } | |
27295592 | 1452 | |
6122e4e4 | 1453 | int perf_session__process_events(struct perf_session *self, |
45694aa7 | 1454 | struct perf_tool *tool) |
6122e4e4 ACM |
1455 | { |
1456 | int err; | |
1457 | ||
1458 | if (perf_session__register_idle_thread(self) == NULL) | |
1459 | return -ENOMEM; | |
1460 | ||
8dc58101 TZ |
1461 | if (!self->fd_pipe) |
1462 | err = __perf_session__process_events(self, | |
1463 | self->header.data_offset, | |
1464 | self->header.data_size, | |
45694aa7 | 1465 | self->size, tool); |
8dc58101 | 1466 | else |
45694aa7 | 1467 | err = __perf_session__process_pipe_events(self, tool); |
88ca895d | 1468 | |
6122e4e4 ACM |
1469 | return err; |
1470 | } | |
1471 | ||
7f3be652 | 1472 | bool perf_session__has_traces(struct perf_session *session, const char *msg) |
27295592 | 1473 | { |
7f3be652 | 1474 | if (!(perf_evlist__sample_type(session->evlist) & PERF_SAMPLE_RAW)) { |
d549c769 ACM |
1475 | pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg); |
1476 | return false; | |
27295592 ACM |
1477 | } |
1478 | ||
d549c769 | 1479 | return true; |
27295592 | 1480 | } |
56b03f3c | 1481 | |
743eb868 ACM |
1482 | int maps__set_kallsyms_ref_reloc_sym(struct map **maps, |
1483 | const char *symbol_name, u64 addr) | |
56b03f3c ACM |
1484 | { |
1485 | char *bracket; | |
9de89fe7 | 1486 | enum map_type i; |
a1645ce1 ZY |
1487 | struct ref_reloc_sym *ref; |
1488 | ||
1489 | ref = zalloc(sizeof(struct ref_reloc_sym)); | |
1490 | if (ref == NULL) | |
1491 | return -ENOMEM; | |
56b03f3c | 1492 | |
a1645ce1 ZY |
1493 | ref->name = strdup(symbol_name); |
1494 | if (ref->name == NULL) { | |
1495 | free(ref); | |
56b03f3c | 1496 | return -ENOMEM; |
a1645ce1 | 1497 | } |
56b03f3c | 1498 | |
a1645ce1 | 1499 | bracket = strchr(ref->name, ']'); |
56b03f3c ACM |
1500 | if (bracket) |
1501 | *bracket = '\0'; | |
1502 | ||
a1645ce1 | 1503 | ref->addr = addr; |
9de89fe7 ACM |
1504 | |
1505 | for (i = 0; i < MAP__NR_TYPES; ++i) { | |
a1645ce1 ZY |
1506 | struct kmap *kmap = map__kmap(maps[i]); |
1507 | kmap->ref_reloc_sym = ref; | |
9de89fe7 ACM |
1508 | } |
1509 | ||
56b03f3c ACM |
1510 | return 0; |
1511 | } | |
1f626bc3 ACM |
1512 | |
1513 | size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp) | |
1514 | { | |
1515 | return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) + | |
1516 | __dsos__fprintf(&self->host_machine.user_dsos, fp) + | |
1517 | machines__fprintf_dsos(&self->machines, fp); | |
1518 | } | |
f869097e ACM |
1519 | |
1520 | size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp, | |
1521 | bool with_hits) | |
1522 | { | |
1523 | size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits); | |
1524 | return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits); | |
1525 | } | |
e248de33 ACM |
1526 | |
1527 | size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp) | |
1528 | { | |
1529 | struct perf_evsel *pos; | |
1530 | size_t ret = fprintf(fp, "Aggregated stats:\n"); | |
1531 | ||
1532 | ret += hists__fprintf_nr_events(&session->hists, fp); | |
1533 | ||
1534 | list_for_each_entry(pos, &session->evlist->entries, node) { | |
7289f83c | 1535 | ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos)); |
e248de33 ACM |
1536 | ret += hists__fprintf_nr_events(&pos->hists, fp); |
1537 | } | |
1538 | ||
1539 | return ret; | |
1540 | } | |
c0230b2b | 1541 | |
b424eba2 ACM |
1542 | size_t perf_session__fprintf(struct perf_session *session, FILE *fp) |
1543 | { | |
1544 | /* | |
1545 | * FIXME: Here we have to actually print all the machines in this | |
1546 | * session, not just the host... | |
1547 | */ | |
1548 | return machine__fprintf(&session->host_machine, fp); | |
1549 | } | |
1550 | ||
1551 | void perf_session__remove_thread(struct perf_session *session, | |
1552 | struct thread *th) | |
1553 | { | |
1554 | /* | |
1555 | * FIXME: This one makes no sense, we need to remove the thread from | |
1556 | * the machine it belongs to, perf_session can have many machines, so | |
1557 | * doing it always on ->host_machine is wrong. Fix when auditing all | |
1558 | * the 'perf kvm' code. | |
1559 | */ | |
1560 | machine__remove_thread(&session->host_machine, th); | |
1561 | } | |
1562 | ||
9cbdb702 DA |
1563 | struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session, |
1564 | unsigned int type) | |
1565 | { | |
1566 | struct perf_evsel *pos; | |
1567 | ||
1568 | list_for_each_entry(pos, &session->evlist->entries, node) { | |
1569 | if (pos->attr.type == type) | |
1570 | return pos; | |
1571 | } | |
1572 | return NULL; | |
1573 | } | |
1574 | ||
71ad0f5e JO |
1575 | void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event, |
1576 | struct perf_sample *sample, struct machine *machine, | |
1577 | int print_sym, int print_dso, int print_symoffset) | |
c0230b2b DA |
1578 | { |
1579 | struct addr_location al; | |
c0230b2b DA |
1580 | struct callchain_cursor_node *node; |
1581 | ||
743eb868 | 1582 | if (perf_event__preprocess_sample(event, machine, &al, sample, |
c0230b2b DA |
1583 | NULL) < 0) { |
1584 | error("problem processing %d event, skipping it.\n", | |
1585 | event->header.type); | |
1586 | return; | |
1587 | } | |
1588 | ||
1589 | if (symbol_conf.use_callchain && sample->callchain) { | |
1590 | ||
71ad0f5e JO |
1591 | |
1592 | if (machine__resolve_callchain(machine, evsel, al.thread, | |
1593 | sample, NULL) != 0) { | |
c0230b2b DA |
1594 | if (verbose) |
1595 | error("Failed to resolve callchain. Skipping\n"); | |
1596 | return; | |
1597 | } | |
47260645 | 1598 | callchain_cursor_commit(&callchain_cursor); |
c0230b2b DA |
1599 | |
1600 | while (1) { | |
47260645 | 1601 | node = callchain_cursor_current(&callchain_cursor); |
c0230b2b DA |
1602 | if (!node) |
1603 | break; | |
1604 | ||
787bef17 DA |
1605 | printf("\t%16" PRIx64, node->ip); |
1606 | if (print_sym) { | |
547a92e0 AN |
1607 | printf(" "); |
1608 | symbol__fprintf_symname(node->sym, stdout); | |
610723f2 DA |
1609 | } |
1610 | if (print_dso) { | |
547a92e0 | 1611 | printf(" ("); |
52deff71 | 1612 | map__fprintf_dsoname(node->map, stdout); |
547a92e0 | 1613 | printf(")"); |
787bef17 DA |
1614 | } |
1615 | printf("\n"); | |
c0230b2b | 1616 | |
47260645 | 1617 | callchain_cursor_advance(&callchain_cursor); |
c0230b2b DA |
1618 | } |
1619 | ||
1620 | } else { | |
adc4bf99 | 1621 | printf("%16" PRIx64, sample->ip); |
787bef17 | 1622 | if (print_sym) { |
547a92e0 | 1623 | printf(" "); |
a978f2ab AN |
1624 | if (print_symoffset) |
1625 | symbol__fprintf_symname_offs(al.sym, &al, | |
1626 | stdout); | |
1627 | else | |
1628 | symbol__fprintf_symname(al.sym, stdout); | |
610723f2 DA |
1629 | } |
1630 | ||
1631 | if (print_dso) { | |
547a92e0 AN |
1632 | printf(" ("); |
1633 | map__fprintf_dsoname(al.map, stdout); | |
1634 | printf(")"); | |
787bef17 | 1635 | } |
c0230b2b DA |
1636 | } |
1637 | } | |
5d67be97 AB |
1638 | |
1639 | int perf_session__cpu_bitmap(struct perf_session *session, | |
1640 | const char *cpu_list, unsigned long *cpu_bitmap) | |
1641 | { | |
1642 | int i; | |
1643 | struct cpu_map *map; | |
1644 | ||
1645 | for (i = 0; i < PERF_TYPE_MAX; ++i) { | |
1646 | struct perf_evsel *evsel; | |
1647 | ||
1648 | evsel = perf_session__find_first_evtype(session, i); | |
1649 | if (!evsel) | |
1650 | continue; | |
1651 | ||
1652 | if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) { | |
1653 | pr_err("File does not contain CPU events. " | |
1654 | "Remove -c option to proceed.\n"); | |
1655 | return -1; | |
1656 | } | |
1657 | } | |
1658 | ||
1659 | map = cpu_map__new(cpu_list); | |
47fbe53b DA |
1660 | if (map == NULL) { |
1661 | pr_err("Invalid cpu_list\n"); | |
1662 | return -1; | |
1663 | } | |
5d67be97 AB |
1664 | |
1665 | for (i = 0; i < map->nr; i++) { | |
1666 | int cpu = map->map[i]; | |
1667 | ||
1668 | if (cpu >= MAX_NR_CPUS) { | |
1669 | pr_err("Requested CPU %d too large. " | |
1670 | "Consider raising MAX_NR_CPUS\n", cpu); | |
1671 | return -1; | |
1672 | } | |
1673 | ||
1674 | set_bit(cpu, cpu_bitmap); | |
1675 | } | |
1676 | ||
1677 | return 0; | |
1678 | } | |
fbe96f29 SE |
1679 | |
1680 | void perf_session__fprintf_info(struct perf_session *session, FILE *fp, | |
1681 | bool full) | |
1682 | { | |
1683 | struct stat st; | |
1684 | int ret; | |
1685 | ||
1686 | if (session == NULL || fp == NULL) | |
1687 | return; | |
1688 | ||
1689 | ret = fstat(session->fd, &st); | |
1690 | if (ret == -1) | |
1691 | return; | |
1692 | ||
1693 | fprintf(fp, "# ========\n"); | |
1694 | fprintf(fp, "# captured on: %s", ctime(&st.st_ctime)); | |
1695 | perf_header__fprintf_info(session, fp, full); | |
1696 | fprintf(fp, "# ========\n#\n"); | |
1697 | } | |
da378962 ACM |
1698 | |
1699 | ||
1700 | int __perf_session__set_tracepoints_handlers(struct perf_session *session, | |
1701 | const struct perf_evsel_str_handler *assocs, | |
1702 | size_t nr_assocs) | |
1703 | { | |
1704 | struct perf_evlist *evlist = session->evlist; | |
1705 | struct event_format *format; | |
1706 | struct perf_evsel *evsel; | |
1707 | char *tracepoint, *name; | |
1708 | size_t i; | |
1709 | int err; | |
1710 | ||
1711 | for (i = 0; i < nr_assocs; i++) { | |
1712 | err = -ENOMEM; | |
1713 | tracepoint = strdup(assocs[i].name); | |
1714 | if (tracepoint == NULL) | |
1715 | goto out; | |
1716 | ||
1717 | err = -ENOENT; | |
1718 | name = strchr(tracepoint, ':'); | |
1719 | if (name == NULL) | |
1720 | goto out_free; | |
1721 | ||
1722 | *name++ = '\0'; | |
1723 | format = pevent_find_event_by_name(session->pevent, | |
1724 | tracepoint, name); | |
1725 | if (format == NULL) { | |
1726 | /* | |
1727 | * Adding a handler for an event not in the session, | |
1728 | * just ignore it. | |
1729 | */ | |
1730 | goto next; | |
1731 | } | |
1732 | ||
1733 | evsel = perf_evlist__find_tracepoint_by_id(evlist, format->id); | |
1734 | if (evsel == NULL) | |
1735 | goto next; | |
1736 | ||
1737 | err = -EEXIST; | |
1738 | if (evsel->handler.func != NULL) | |
1739 | goto out_free; | |
1740 | evsel->handler.func = assocs[i].handler; | |
1741 | next: | |
1742 | free(tracepoint); | |
1743 | } | |
1744 | ||
1745 | err = 0; | |
1746 | out: | |
1747 | return err; | |
1748 | ||
1749 | out_free: | |
1750 | free(tracepoint); | |
1751 | goto out; | |
1752 | } |