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