blob: 04d1e33f4592b9b47a75609b6d2e109de91c01fe [file] [log] [blame]
Xiao Guangrongb8f46c52010-02-03 11:53:14 +08001#define _FILE_OFFSET_BITS 64
2
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02003#include <linux/kernel.h>
4
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02005#include <byteswap.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02006#include <unistd.h>
7#include <sys/types.h>
Arnaldo Carvalho de Meloa41794c2010-05-18 18:29:23 -03008#include <sys/mman.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02009
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -030010#include "evlist.h"
11#include "evsel.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020012#include "session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020013#include "tool.h"
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -020014#include "sort.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020015#include "util.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100016#include "cpumap.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020017
18static int perf_session__open(struct perf_session *self, bool force)
19{
20 struct stat input_stat;
21
Tom Zanussi8dc58102010-04-01 23:59:15 -050022 if (!strcmp(self->filename, "-")) {
23 self->fd_pipe = true;
24 self->fd = STDIN_FILENO;
25
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030026 if (perf_session__read_header(self, self->fd) < 0)
Stephane Eranian69996df2012-02-09 23:21:06 +010027 pr_err("incompatible file format (rerun with -v to learn more)");
Tom Zanussi8dc58102010-04-01 23:59:15 -050028
29 return 0;
30 }
31
Xiao Guangrongf887f302010-02-04 16:46:42 +080032 self->fd = open(self->filename, O_RDONLY);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020033 if (self->fd < 0) {
Andy Isaacson0f2c3de2010-06-11 20:36:15 -070034 int err = errno;
35
36 pr_err("failed to open %s: %s", self->filename, strerror(err));
37 if (err == ENOENT && !strcmp(self->filename, "perf.data"))
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020038 pr_err(" (try 'perf record' first)");
39 pr_err("\n");
40 return -errno;
41 }
42
43 if (fstat(self->fd, &input_stat) < 0)
44 goto out_close;
45
46 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
47 pr_err("file %s not owned by current user or root\n",
48 self->filename);
49 goto out_close;
50 }
51
52 if (!input_stat.st_size) {
53 pr_info("zero-sized file (%s), nothing to do!\n",
54 self->filename);
55 goto out_close;
56 }
57
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030058 if (perf_session__read_header(self, self->fd) < 0) {
Stephane Eranian69996df2012-02-09 23:21:06 +010059 pr_err("incompatible file format (rerun with -v to learn more)");
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020060 goto out_close;
61 }
62
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030063 if (!perf_evlist__valid_sample_type(self->evlist)) {
64 pr_err("non matching sample_type");
65 goto out_close;
66 }
67
68 if (!perf_evlist__valid_sample_id_all(self->evlist)) {
69 pr_err("non matching sample_id_all");
70 goto out_close;
71 }
72
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020073 self->size = input_stat.st_size;
74 return 0;
75
76out_close:
77 close(self->fd);
78 self->fd = -1;
79 return -1;
80}
81
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020082void perf_session__update_sample_type(struct perf_session *self)
83{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030084 self->sample_type = perf_evlist__sample_type(self->evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030085 self->sample_size = __perf_evsel__sample_size(self->sample_type);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030086 self->sample_id_all = perf_evlist__sample_id_all(self->evlist);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -020087 self->id_hdr_size = perf_evlist__id_hdr_size(self->evlist);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020088 self->host_machine.id_hdr_size = self->id_hdr_size;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020089}
90
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080091int perf_session__create_kernel_maps(struct perf_session *self)
92{
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -030093 int ret = machine__create_kernel_maps(&self->host_machine);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080094
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080095 if (ret >= 0)
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -030096 ret = machines__create_guest_kernel_maps(&self->machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080097 return ret;
98}
99
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300100static void perf_session__destroy_kernel_maps(struct perf_session *self)
101{
102 machine__destroy_kernel_maps(&self->host_machine);
103 machines__destroy_guest_kernel_maps(&self->machines);
104}
105
Ian Munsie21ef97f2010-12-10 14:09:16 +1100106struct perf_session *perf_session__new(const char *filename, int mode,
107 bool force, bool repipe,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200108 struct perf_tool *tool)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200109{
Robert Richterefad1412011-12-07 10:02:54 +0100110 struct perf_session *self;
111 struct stat st;
112 size_t len;
113
114 if (!filename || !strlen(filename)) {
115 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
116 filename = "-";
117 else
118 filename = "perf.data";
119 }
120
121 len = strlen(filename);
122 self = zalloc(sizeof(*self) + len);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200123
124 if (self == NULL)
125 goto out;
126
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200127 memcpy(self->filename, filename, len);
Thomas Gleixner55b44622010-11-30 17:49:46 +0000128 /*
129 * On 64bit we can mmap the data file in one go. No need for tiny mmap
130 * slices. On 32bit we use 32MB.
131 */
132#if BITS_PER_LONG == 64
133 self->mmap_window = ULLONG_MAX;
134#else
135 self->mmap_window = 32 * 1024 * 1024ULL;
136#endif
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300137 self->machines = RB_ROOT;
Tom Zanussi454c4072010-05-01 01:41:20 -0500138 self->repipe = repipe;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000139 INIT_LIST_HEAD(&self->ordered_samples.samples);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000140 INIT_LIST_HEAD(&self->ordered_samples.sample_cache);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000141 INIT_LIST_HEAD(&self->ordered_samples.to_free);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300142 machine__init(&self->host_machine, "", HOST_KERNEL_ID);
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100143 hists__init(&self->hists);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200144
Arnaldo Carvalho de Melo64abebf2010-01-27 21:05:52 -0200145 if (mode == O_RDONLY) {
146 if (perf_session__open(self, force) < 0)
147 goto out_delete;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300148 perf_session__update_sample_type(self);
Arnaldo Carvalho de Melo64abebf2010-01-27 21:05:52 -0200149 } else if (mode == O_WRONLY) {
150 /*
151 * In O_RDONLY mode this will be performed when reading the
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200152 * kernel MMAP event, in perf_event__process_mmap().
Arnaldo Carvalho de Melo64abebf2010-01-27 21:05:52 -0200153 */
154 if (perf_session__create_kernel_maps(self) < 0)
155 goto out_delete;
156 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200157
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200158 if (tool && tool->ordering_requires_timestamps &&
159 tool->ordered_samples && !self->sample_id_all) {
Ian Munsie21ef97f2010-12-10 14:09:16 +1100160 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200161 tool->ordered_samples = false;
Ian Munsie21ef97f2010-12-10 14:09:16 +1100162 }
163
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200164out:
165 return self;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200166out_delete:
167 perf_session__delete(self);
168 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200169}
170
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200171static void machine__delete_dead_threads(struct machine *machine)
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300172{
173 struct thread *n, *t;
174
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200175 list_for_each_entry_safe(t, n, &machine->dead_threads, node) {
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300176 list_del(&t->node);
177 thread__delete(t);
178 }
179}
180
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200181static void perf_session__delete_dead_threads(struct perf_session *session)
182{
183 machine__delete_dead_threads(&session->host_machine);
184}
185
186static void machine__delete_threads(struct machine *self)
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300187{
188 struct rb_node *nd = rb_first(&self->threads);
189
190 while (nd) {
191 struct thread *t = rb_entry(nd, struct thread, rb_node);
192
193 rb_erase(&t->rb_node, &self->threads);
194 nd = rb_next(nd);
195 thread__delete(t);
196 }
197}
198
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200199static void perf_session__delete_threads(struct perf_session *session)
200{
201 machine__delete_threads(&session->host_machine);
202}
203
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200204void perf_session__delete(struct perf_session *self)
205{
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300206 perf_session__destroy_kernel_maps(self);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300207 perf_session__delete_dead_threads(self);
208 perf_session__delete_threads(self);
209 machine__exit(&self->host_machine);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200210 close(self->fd);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200211 free(self);
212}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200213
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200214void machine__remove_thread(struct machine *self, struct thread *th)
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300215{
Arnaldo Carvalho de Melo70597f22010-08-02 18:59:28 -0300216 self->last_match = NULL;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300217 rb_erase(&th->rb_node, &self->threads);
218 /*
219 * We may have references to this thread, for instance in some hist_entry
220 * instances, so just move them to a separate list.
221 */
222 list_add_tail(&th->node, &self->dead_threads);
223}
224
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200225static bool symbol__match_parent_regex(struct symbol *sym)
226{
227 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
228 return 1;
229
230 return 0;
231}
232
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100233static const u8 cpumodes[] = {
234 PERF_RECORD_MISC_USER,
235 PERF_RECORD_MISC_KERNEL,
236 PERF_RECORD_MISC_GUEST_USER,
237 PERF_RECORD_MISC_GUEST_KERNEL
238};
239#define NCPUMODES (sizeof(cpumodes)/sizeof(u8))
240
241static void ip__resolve_ams(struct machine *self, struct thread *thread,
242 struct addr_map_symbol *ams,
243 u64 ip)
244{
245 struct addr_location al;
246 size_t i;
247 u8 m;
248
249 memset(&al, 0, sizeof(al));
250
251 for (i = 0; i < NCPUMODES; i++) {
252 m = cpumodes[i];
253 /*
254 * We cannot use the header.misc hint to determine whether a
255 * branch stack address is user, kernel, guest, hypervisor.
256 * Branches may straddle the kernel/user/hypervisor boundaries.
257 * Thus, we have to try consecutively until we find a match
258 * or else, the symbol is unknown
259 */
260 thread__find_addr_location(thread, self, m, MAP__FUNCTION,
261 ip, &al, NULL);
262 if (al.sym)
263 goto found;
264 }
265found:
266 ams->addr = ip;
Stephane Eraniana68c2c52012-03-08 23:47:48 +0100267 ams->al_addr = al.addr;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100268 ams->sym = al.sym;
269 ams->map = al.map;
270}
271
272struct branch_info *machine__resolve_bstack(struct machine *self,
273 struct thread *thr,
274 struct branch_stack *bs)
275{
276 struct branch_info *bi;
277 unsigned int i;
278
279 bi = calloc(bs->nr, sizeof(struct branch_info));
280 if (!bi)
281 return NULL;
282
283 for (i = 0; i < bs->nr; i++) {
284 ip__resolve_ams(self, thr, &bi[i].to, bs->entries[i].to);
285 ip__resolve_ams(self, thr, &bi[i].from, bs->entries[i].from);
286 bi[i].flags = bs->entries[i].flags;
287 }
288 return bi;
289}
290
Namhyung Kim47260642012-05-31 14:43:26 +0900291int machine__resolve_callchain(struct machine *self,
292 struct perf_evsel *evsel __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200293 struct thread *thread,
294 struct ip_callchain *chain,
295 struct symbol **parent)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200296{
297 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200298 unsigned int i;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100299 int err;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200300
Namhyung Kim47260642012-05-31 14:43:26 +0900301 callchain_cursor_reset(&callchain_cursor);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200302
Namhyung Kim114067b2012-05-31 14:43:27 +0900303 if (chain->nr > PERF_MAX_STACK_DEPTH) {
304 pr_warning("corrupted callchain. skipping...\n");
305 return 0;
306 }
307
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200308 for (i = 0; i < chain->nr; i++) {
Sam Liaod797fdc2011-06-07 23:49:46 +0800309 u64 ip;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200310 struct addr_location al;
311
Sam Liaod797fdc2011-06-07 23:49:46 +0800312 if (callchain_param.order == ORDER_CALLEE)
313 ip = chain->ips[i];
314 else
315 ip = chain->ips[chain->nr - i - 1];
316
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200317 if (ip >= PERF_CONTEXT_MAX) {
318 switch (ip) {
319 case PERF_CONTEXT_HV:
320 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
321 case PERF_CONTEXT_KERNEL:
322 cpumode = PERF_RECORD_MISC_KERNEL; break;
323 case PERF_CONTEXT_USER:
324 cpumode = PERF_RECORD_MISC_USER; break;
325 default:
Namhyung Kim114067b2012-05-31 14:43:27 +0900326 pr_debug("invalid callchain context: "
327 "%"PRId64"\n", (s64) ip);
328 /*
329 * It seems the callchain is corrupted.
330 * Discard all.
331 */
332 callchain_cursor_reset(&callchain_cursor);
333 return 0;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200334 }
335 continue;
336 }
337
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800338 al.filtered = false;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200339 thread__find_addr_location(thread, self, cpumode,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200340 MAP__FUNCTION, ip, &al, NULL);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200341 if (al.sym != NULL) {
342 if (sort__has_parent && !*parent &&
343 symbol__match_parent_regex(al.sym))
344 *parent = al.sym;
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200345 if (!symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200346 break;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200347 }
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100348
Namhyung Kim47260642012-05-31 14:43:26 +0900349 err = callchain_cursor_append(&callchain_cursor,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100350 ip, al.map, al.sym);
351 if (err)
352 return err;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200353 }
354
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100355 return 0;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200356}
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200357
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200358static int process_event_synth_tracing_data_stub(union perf_event *event __used,
359 struct perf_session *session __used)
360{
361 dump_printf(": unhandled!\n");
362 return 0;
363}
364
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200365static int process_event_synth_attr_stub(union perf_event *event __used,
366 struct perf_evlist **pevlist __used)
367{
368 dump_printf(": unhandled!\n");
369 return 0;
370}
371
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200372static int process_event_sample_stub(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200373 union perf_event *event __used,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300374 struct perf_sample *sample __used,
375 struct perf_evsel *evsel __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200376 struct machine *machine __used)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300377{
378 dump_printf(": unhandled!\n");
379 return 0;
380}
381
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200382static int process_event_stub(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200383 union perf_event *event __used,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200384 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200385 struct machine *machine __used)
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200386{
387 dump_printf(": unhandled!\n");
388 return 0;
389}
390
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200391static int process_finished_round_stub(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200392 union perf_event *event __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200393 struct perf_session *perf_session __used)
394{
395 dump_printf(": unhandled!\n");
396 return 0;
397}
398
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200399static int process_event_type_stub(struct perf_tool *tool __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200400 union perf_event *event __used)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200401{
402 dump_printf(": unhandled!\n");
403 return 0;
404}
405
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200406static int process_finished_round(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200407 union perf_event *event,
408 struct perf_session *session);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200409
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200410static void perf_tool__fill_defaults(struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200411{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200412 if (tool->sample == NULL)
413 tool->sample = process_event_sample_stub;
414 if (tool->mmap == NULL)
415 tool->mmap = process_event_stub;
416 if (tool->comm == NULL)
417 tool->comm = process_event_stub;
418 if (tool->fork == NULL)
419 tool->fork = process_event_stub;
420 if (tool->exit == NULL)
421 tool->exit = process_event_stub;
422 if (tool->lost == NULL)
423 tool->lost = perf_event__process_lost;
424 if (tool->read == NULL)
425 tool->read = process_event_sample_stub;
426 if (tool->throttle == NULL)
427 tool->throttle = process_event_stub;
428 if (tool->unthrottle == NULL)
429 tool->unthrottle = process_event_stub;
430 if (tool->attr == NULL)
431 tool->attr = process_event_synth_attr_stub;
432 if (tool->event_type == NULL)
433 tool->event_type = process_event_type_stub;
434 if (tool->tracing_data == NULL)
435 tool->tracing_data = process_event_synth_tracing_data_stub;
436 if (tool->build_id == NULL)
437 tool->build_id = process_finished_round_stub;
438 if (tool->finished_round == NULL) {
439 if (tool->ordered_samples)
440 tool->finished_round = process_finished_round;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200441 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200442 tool->finished_round = process_finished_round_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200443 }
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200444}
445
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200446void mem_bswap_64(void *src, int byte_size)
447{
448 u64 *m = src;
449
450 while (byte_size > 0) {
451 *m = bswap_64(*m);
452 byte_size -= sizeof(u64);
453 ++m;
454 }
455}
456
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200457static void perf_event__all64_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200458{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200459 struct perf_event_header *hdr = &event->header;
460 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200461}
462
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200463static void perf_event__comm_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200464{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200465 event->comm.pid = bswap_32(event->comm.pid);
466 event->comm.tid = bswap_32(event->comm.tid);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200467}
468
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200469static void perf_event__mmap_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200470{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200471 event->mmap.pid = bswap_32(event->mmap.pid);
472 event->mmap.tid = bswap_32(event->mmap.tid);
473 event->mmap.start = bswap_64(event->mmap.start);
474 event->mmap.len = bswap_64(event->mmap.len);
475 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200476}
477
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200478static void perf_event__task_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200479{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200480 event->fork.pid = bswap_32(event->fork.pid);
481 event->fork.tid = bswap_32(event->fork.tid);
482 event->fork.ppid = bswap_32(event->fork.ppid);
483 event->fork.ptid = bswap_32(event->fork.ptid);
484 event->fork.time = bswap_64(event->fork.time);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200485}
486
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200487static void perf_event__read_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200488{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200489 event->read.pid = bswap_32(event->read.pid);
490 event->read.tid = bswap_32(event->read.tid);
491 event->read.value = bswap_64(event->read.value);
492 event->read.time_enabled = bswap_64(event->read.time_enabled);
493 event->read.time_running = bswap_64(event->read.time_running);
494 event->read.id = bswap_64(event->read.id);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200495}
496
Jiri Olsae108c662012-05-16 08:59:03 +0200497static u8 revbyte(u8 b)
498{
499 int rev = (b >> 4) | ((b & 0xf) << 4);
500 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
501 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
502 return (u8) rev;
503}
504
505/*
506 * XXX this is hack in attempt to carry flags bitfield
507 * throught endian village. ABI says:
508 *
509 * Bit-fields are allocated from right to left (least to most significant)
510 * on little-endian implementations and from left to right (most to least
511 * significant) on big-endian implementations.
512 *
513 * The above seems to be byte specific, so we need to reverse each
514 * byte of the bitfield. 'Internet' also says this might be implementation
515 * specific and we probably need proper fix and carry perf_event_attr
516 * bitfield flags in separate data file FEAT_ section. Thought this seems
517 * to work for now.
518 */
519static void swap_bitfield(u8 *p, unsigned len)
520{
521 unsigned i;
522
523 for (i = 0; i < len; i++) {
524 *p = revbyte(*p);
525 p++;
526 }
527}
528
David Aherneda39132011-07-15 12:34:09 -0600529/* exported for swapping attributes in file header */
530void perf_event__attr_swap(struct perf_event_attr *attr)
531{
532 attr->type = bswap_32(attr->type);
533 attr->size = bswap_32(attr->size);
534 attr->config = bswap_64(attr->config);
535 attr->sample_period = bswap_64(attr->sample_period);
536 attr->sample_type = bswap_64(attr->sample_type);
537 attr->read_format = bswap_64(attr->read_format);
538 attr->wakeup_events = bswap_32(attr->wakeup_events);
539 attr->bp_type = bswap_32(attr->bp_type);
540 attr->bp_addr = bswap_64(attr->bp_addr);
541 attr->bp_len = bswap_64(attr->bp_len);
Jiri Olsae108c662012-05-16 08:59:03 +0200542
543 swap_bitfield((u8 *) (&attr->read_format + 1), sizeof(u64));
David Aherneda39132011-07-15 12:34:09 -0600544}
545
546static void perf_event__hdr_attr_swap(union perf_event *event)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500547{
548 size_t size;
549
David Aherneda39132011-07-15 12:34:09 -0600550 perf_event__attr_swap(&event->attr.attr);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500551
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200552 size = event->header.size;
553 size -= (void *)&event->attr.id - (void *)event;
554 mem_bswap_64(event->attr.id, size);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500555}
556
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200557static void perf_event__event_type_swap(union perf_event *event)
Tom Zanussicd19a032010-04-01 23:59:20 -0500558{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200559 event->event_type.event_type.event_id =
560 bswap_64(event->event_type.event_type.event_id);
Tom Zanussicd19a032010-04-01 23:59:20 -0500561}
562
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200563static void perf_event__tracing_data_swap(union perf_event *event)
Tom Zanussi92155452010-04-01 23:59:21 -0500564{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200565 event->tracing_data.size = bswap_32(event->tracing_data.size);
Tom Zanussi92155452010-04-01 23:59:21 -0500566}
567
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200568typedef void (*perf_event__swap_op)(union perf_event *event);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200569
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200570static perf_event__swap_op perf_event__swap_ops[] = {
571 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
572 [PERF_RECORD_COMM] = perf_event__comm_swap,
573 [PERF_RECORD_FORK] = perf_event__task_swap,
574 [PERF_RECORD_EXIT] = perf_event__task_swap,
575 [PERF_RECORD_LOST] = perf_event__all64_swap,
576 [PERF_RECORD_READ] = perf_event__read_swap,
577 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
David Aherneda39132011-07-15 12:34:09 -0600578 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200579 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
580 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
581 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
582 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200583};
584
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200585struct sample_queue {
586 u64 timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000587 u64 file_offset;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200588 union perf_event *event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200589 struct list_head list;
590};
591
Thomas Gleixner020bb752010-11-30 17:49:53 +0000592static void perf_session_free_sample_buffers(struct perf_session *session)
593{
594 struct ordered_samples *os = &session->ordered_samples;
595
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000596 while (!list_empty(&os->to_free)) {
Thomas Gleixner020bb752010-11-30 17:49:53 +0000597 struct sample_queue *sq;
598
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000599 sq = list_entry(os->to_free.next, struct sample_queue, list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000600 list_del(&sq->list);
601 free(sq);
602 }
603}
604
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100605static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200606 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200607 struct perf_sample *sample,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200608 struct perf_tool *tool,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000609 u64 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100610
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200611static void flush_sample_queue(struct perf_session *s,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200612 struct perf_tool *tool)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200613{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000614 struct ordered_samples *os = &s->ordered_samples;
615 struct list_head *head = &os->samples;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200616 struct sample_queue *tmp, *iter;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200617 struct perf_sample sample;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000618 u64 limit = os->next_flush;
619 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200620 unsigned idx = 0, progress_next = os->nr_samples / 16;
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200621 int ret;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200622
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200623 if (!tool->ordered_samples || !limit)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200624 return;
625
626 list_for_each_entry_safe(iter, tmp, head, list) {
627 if (iter->timestamp > limit)
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000628 break;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200629
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200630 ret = perf_session__parse_sample(s, iter->event, &sample);
631 if (ret)
632 pr_err("Can't parse sample, err = %d\n", ret);
633 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200634 perf_session_deliver_event(s, iter->event, &sample, tool,
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200635 iter->file_offset);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200636
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000637 os->last_flush = iter->timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200638 list_del(&iter->list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000639 list_add(&iter->list, &os->sample_cache);
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200640 if (++idx >= progress_next) {
641 progress_next += os->nr_samples / 16;
642 ui_progress__update(idx, os->nr_samples,
643 "Processing time ordered events...");
644 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200645 }
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000646
647 if (list_empty(head)) {
648 os->last_sample = NULL;
649 } else if (last_ts <= limit) {
650 os->last_sample =
651 list_entry(head->prev, struct sample_queue, list);
652 }
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200653
654 os->nr_samples = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200655}
656
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200657/*
658 * When perf record finishes a pass on every buffers, it records this pseudo
659 * event.
660 * We record the max timestamp t found in the pass n.
661 * Assuming these timestamps are monotonic across cpus, we know that if
662 * a buffer still has events with timestamps below t, they will be all
663 * available and then read in the pass n + 1.
664 * Hence when we start to read the pass n + 2, we can safely flush every
665 * events with timestamps below t.
666 *
667 * ============ PASS n =================
668 * CPU 0 | CPU 1
669 * |
670 * cnt1 timestamps | cnt2 timestamps
671 * 1 | 2
672 * 2 | 3
673 * - | 4 <--- max recorded
674 *
675 * ============ PASS n + 1 ==============
676 * CPU 0 | CPU 1
677 * |
678 * cnt1 timestamps | cnt2 timestamps
679 * 3 | 5
680 * 4 | 6
681 * 5 | 7 <---- max recorded
682 *
683 * Flush every events below timestamp 4
684 *
685 * ============ PASS n + 2 ==============
686 * CPU 0 | CPU 1
687 * |
688 * cnt1 timestamps | cnt2 timestamps
689 * 6 | 8
690 * 7 | 9
691 * - | 10
692 *
693 * Flush every events below timestamp 7
694 * etc...
695 */
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200696static int process_finished_round(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200697 union perf_event *event __used,
698 struct perf_session *session)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200699{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200700 flush_sample_queue(session, tool);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200701 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
702
703 return 0;
704}
705
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200706/* The queue is ordered by time */
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100707static void __queue_event(struct sample_queue *new, struct perf_session *s)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200708{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000709 struct ordered_samples *os = &s->ordered_samples;
710 struct sample_queue *sample = os->last_sample;
711 u64 timestamp = new->timestamp;
712 struct list_head *p;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200713
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200714 ++os->nr_samples;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000715 os->last_sample = new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200716
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000717 if (!sample) {
718 list_add(&new->list, &os->samples);
719 os->max_timestamp = timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200720 return;
721 }
722
723 /*
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000724 * last_sample might point to some random place in the list as it's
725 * the last queued event. We expect that the new event is close to
726 * this.
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200727 */
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000728 if (sample->timestamp <= timestamp) {
729 while (sample->timestamp <= timestamp) {
730 p = sample->list.next;
731 if (p == &os->samples) {
732 list_add_tail(&new->list, &os->samples);
733 os->max_timestamp = timestamp;
734 return;
735 }
736 sample = list_entry(p, struct sample_queue, list);
737 }
738 list_add_tail(&new->list, &sample->list);
739 } else {
740 while (sample->timestamp > timestamp) {
741 p = sample->list.prev;
742 if (p == &os->samples) {
743 list_add(&new->list, &os->samples);
744 return;
745 }
746 sample = list_entry(p, struct sample_queue, list);
747 }
748 list_add(&new->list, &sample->list);
749 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200750}
751
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000752#define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
753
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200754static int perf_session_queue_event(struct perf_session *s, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200755 struct perf_sample *sample, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200756{
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000757 struct ordered_samples *os = &s->ordered_samples;
758 struct list_head *sc = &os->sample_cache;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200759 u64 timestamp = sample->time;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200760 struct sample_queue *new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200761
Thomas Gleixner79a14c12010-12-07 12:48:44 +0000762 if (!timestamp || timestamp == ~0ULL)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100763 return -ETIME;
764
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200765 if (timestamp < s->ordered_samples.last_flush) {
766 printf("Warning: Timestamp below last timeslice flush\n");
767 return -EINVAL;
768 }
769
Thomas Gleixner020bb752010-11-30 17:49:53 +0000770 if (!list_empty(sc)) {
771 new = list_entry(sc->next, struct sample_queue, list);
772 list_del(&new->list);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000773 } else if (os->sample_buffer) {
774 new = os->sample_buffer + os->sample_buffer_idx;
775 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
776 os->sample_buffer = NULL;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000777 } else {
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000778 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
779 if (!os->sample_buffer)
Thomas Gleixner020bb752010-11-30 17:49:53 +0000780 return -ENOMEM;
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000781 list_add(&os->sample_buffer->list, &os->to_free);
782 os->sample_buffer_idx = 2;
783 new = os->sample_buffer + 1;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000784 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200785
786 new->timestamp = timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000787 new->file_offset = file_offset;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000788 new->event = event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200789
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100790 __queue_event(new, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200791
792 return 0;
793}
794
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200795static void callchain__printf(struct perf_sample *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200796{
797 unsigned int i;
798
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200799 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200800
801 for (i = 0; i < sample->callchain->nr; i++)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200802 printf("..... %2d: %016" PRIx64 "\n",
803 i, sample->callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200804}
805
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100806static void branch_stack__printf(struct perf_sample *sample)
807{
808 uint64_t i;
809
810 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
811
812 for (i = 0; i < sample->branch_stack->nr; i++)
813 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n",
814 i, sample->branch_stack->entries[i].from,
815 sample->branch_stack->entries[i].to);
816}
817
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200818static void perf_session__print_tstamp(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200819 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200820 struct perf_sample *sample)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200821{
822 if (event->header.type != PERF_RECORD_SAMPLE &&
823 !session->sample_id_all) {
824 fputs("-1 -1 ", stdout);
825 return;
826 }
827
828 if ((session->sample_type & PERF_SAMPLE_CPU))
829 printf("%u ", sample->cpu);
830
831 if (session->sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200832 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200833}
834
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200835static void dump_event(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200836 u64 file_offset, struct perf_sample *sample)
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000837{
838 if (!dump_trace)
839 return;
840
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200841 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
842 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000843
844 trace_event(event);
845
846 if (sample)
847 perf_session__print_tstamp(session, event, sample);
848
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200849 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200850 event->header.size, perf_event__name(event->header.type));
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000851}
852
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200853static void dump_sample(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200854 struct perf_sample *sample)
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000855{
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200856 if (!dump_trace)
857 return;
858
David Ahern7cec0922011-05-30 13:08:23 -0600859 printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200860 event->header.misc, sample->pid, sample->tid, sample->ip,
David Ahern7cec0922011-05-30 13:08:23 -0600861 sample->period, sample->addr);
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000862
863 if (session->sample_type & PERF_SAMPLE_CALLCHAIN)
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200864 callchain__printf(sample);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100865
866 if (session->sample_type & PERF_SAMPLE_BRANCH_STACK)
867 branch_stack__printf(sample);
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000868}
869
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200870static struct machine *
871 perf_session__find_machine_for_cpumode(struct perf_session *session,
872 union perf_event *event)
873{
874 const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
875
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530876 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
877 u32 pid;
878
879 if (event->header.type == PERF_RECORD_MMAP)
880 pid = event->mmap.pid;
881 else
882 pid = event->ip.pid;
883
884 return perf_session__find_machine(session, pid);
885 }
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200886
887 return perf_session__find_host_machine(session);
888}
889
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100890static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200891 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200892 struct perf_sample *sample,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200893 struct perf_tool *tool,
Thomas Gleixner532e7262010-12-07 12:48:55 +0000894 u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100895{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300896 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200897 struct machine *machine;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300898
Thomas Gleixner532e7262010-12-07 12:48:55 +0000899 dump_event(session, event, file_offset, sample);
900
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200901 evsel = perf_evlist__id2evsel(session->evlist, sample->id);
902 if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) {
903 /*
904 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
905 * because the tools right now may apply filters, discarding
906 * some of the samples. For consistency, in the future we
907 * should have something like nr_filtered_samples and remove
908 * the sample->period from total_sample_period, etc, KISS for
909 * now tho.
910 *
911 * Also testing against NULL allows us to handle files without
912 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
913 * future probably it'll be a good idea to restrict event
914 * processing via perf_session to files with both set.
915 */
916 hists__inc_nr_events(&evsel->hists, event->header.type);
917 }
918
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200919 machine = perf_session__find_machine_for_cpumode(session, event);
920
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100921 switch (event->header.type) {
922 case PERF_RECORD_SAMPLE:
Thomas Gleixner532e7262010-12-07 12:48:55 +0000923 dump_sample(session, event, sample);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300924 if (evsel == NULL) {
925 ++session->hists.stats.nr_unknown_id;
Jiri Olsa67822062012-04-12 14:21:01 +0200926 return 0;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300927 }
Joerg Roedel0c095712012-02-10 18:05:04 +0100928 if (machine == NULL) {
929 ++session->hists.stats.nr_unprocessable_samples;
Jiri Olsa67822062012-04-12 14:21:01 +0200930 return 0;
Joerg Roedel0c095712012-02-10 18:05:04 +0100931 }
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200932 return tool->sample(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100933 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200934 return tool->mmap(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100935 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200936 return tool->comm(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100937 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200938 return tool->fork(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100939 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200940 return tool->exit(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100941 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200942 if (tool->lost == perf_event__process_lost)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200943 session->hists.stats.total_lost += event->lost.lost;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200944 return tool->lost(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100945 case PERF_RECORD_READ:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200946 return tool->read(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100947 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200948 return tool->throttle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100949 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200950 return tool->unthrottle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100951 default:
952 ++session->hists.stats.nr_unknown_events;
953 return -1;
954 }
955}
956
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000957static int perf_session__preprocess_sample(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200958 union perf_event *event, struct perf_sample *sample)
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000959{
960 if (event->header.type != PERF_RECORD_SAMPLE ||
961 !(session->sample_type & PERF_SAMPLE_CALLCHAIN))
962 return 0;
963
964 if (!ip_callchain__valid(sample->callchain, event)) {
965 pr_debug("call-chain problem with event, skipping it.\n");
966 ++session->hists.stats.nr_invalid_chains;
967 session->hists.stats.total_invalid_chains += sample->period;
968 return -EINVAL;
969 }
970 return 0;
971}
972
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200973static int perf_session__process_user_event(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200974 struct perf_tool *tool, u64 file_offset)
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000975{
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200976 int err;
977
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000978 dump_event(session, event, file_offset, NULL);
979
980 /* These events are processed right away */
981 switch (event->header.type) {
982 case PERF_RECORD_HEADER_ATTR:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200983 err = tool->attr(event, &session->evlist);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200984 if (err == 0)
985 perf_session__update_sample_type(session);
986 return err;
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000987 case PERF_RECORD_HEADER_EVENT_TYPE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200988 return tool->event_type(tool, event);
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000989 case PERF_RECORD_HEADER_TRACING_DATA:
990 /* setup for reading amidst mmap */
991 lseek(session->fd, file_offset, SEEK_SET);
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200992 return tool->tracing_data(event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000993 case PERF_RECORD_HEADER_BUILD_ID:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200994 return tool->build_id(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000995 case PERF_RECORD_FINISHED_ROUND:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200996 return tool->finished_round(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000997 default:
998 return -EINVAL;
999 }
1000}
1001
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001002static int perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001003 union perf_event *event,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001004 struct perf_tool *tool,
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001005 u64 file_offset)
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001006{
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001007 struct perf_sample sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001008 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001009
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001010 if (session->header.needs_swap &&
1011 perf_event__swap_ops[event->header.type])
1012 perf_event__swap_ops[event->header.type](event);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001013
Thomas Gleixner9aefcab2010-12-07 12:48:47 +00001014 if (event->header.type >= PERF_RECORD_HEADER_MAX)
1015 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001016
Thomas Gleixner9aefcab2010-12-07 12:48:47 +00001017 hists__inc_nr_events(&session->hists, event->header.type);
1018
1019 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001020 return perf_session__process_user_event(session, event, tool, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001021
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001022 /*
1023 * For all kernel events we get the sample data
1024 */
Frederic Weisbecker5538bec2011-05-22 02:17:22 +02001025 ret = perf_session__parse_sample(session, event, &sample);
1026 if (ret)
1027 return ret;
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001028
1029 /* Preprocess sample records - precheck callchains */
1030 if (perf_session__preprocess_sample(session, event, &sample))
1031 return 0;
1032
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001033 if (tool->ordered_samples) {
Thomas Gleixnere4c2df12010-12-07 12:48:50 +00001034 ret = perf_session_queue_event(session, event, &sample,
1035 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001036 if (ret != -ETIME)
1037 return ret;
1038 }
1039
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001040 return perf_session_deliver_event(session, event, &sample, tool,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +00001041 file_offset);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001042}
1043
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001044void perf_event_header__bswap(struct perf_event_header *self)
1045{
1046 self->type = bswap_32(self->type);
1047 self->misc = bswap_16(self->misc);
1048 self->size = bswap_16(self->size);
1049}
1050
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001051struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1052{
1053 return machine__findnew_thread(&session->host_machine, pid);
1054}
1055
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001056static struct thread *perf_session__register_idle_thread(struct perf_session *self)
1057{
1058 struct thread *thread = perf_session__findnew(self, 0);
1059
1060 if (thread == NULL || thread__set_comm(thread, "swapper")) {
1061 pr_err("problem inserting idle task.\n");
1062 thread = NULL;
1063 }
1064
1065 return thread;
1066}
1067
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001068static void perf_session__warn_about_errors(const struct perf_session *session,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001069 const struct perf_tool *tool)
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001070{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001071 if (tool->lost == perf_event__process_lost &&
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001072 session->hists.stats.nr_events[PERF_RECORD_LOST] != 0) {
1073 ui__warning("Processed %d events and lost %d chunks!\n\n"
1074 "Check IO/CPU overload!\n\n",
1075 session->hists.stats.nr_events[0],
1076 session->hists.stats.nr_events[PERF_RECORD_LOST]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001077 }
1078
1079 if (session->hists.stats.nr_unknown_events != 0) {
1080 ui__warning("Found %u unknown events!\n\n"
1081 "Is this an older tool processing a perf.data "
1082 "file generated by a more recent tool?\n\n"
1083 "If that is not the case, consider "
1084 "reporting to linux-kernel@vger.kernel.org.\n\n",
1085 session->hists.stats.nr_unknown_events);
1086 }
1087
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001088 if (session->hists.stats.nr_unknown_id != 0) {
1089 ui__warning("%u samples with id not present in the header\n",
1090 session->hists.stats.nr_unknown_id);
1091 }
1092
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001093 if (session->hists.stats.nr_invalid_chains != 0) {
1094 ui__warning("Found invalid callchains!\n\n"
1095 "%u out of %u events were discarded for this reason.\n\n"
1096 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1097 session->hists.stats.nr_invalid_chains,
1098 session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
1099 }
Joerg Roedel0c095712012-02-10 18:05:04 +01001100
1101 if (session->hists.stats.nr_unprocessable_samples != 0) {
1102 ui__warning("%u unprocessable samples recorded.\n"
1103 "Do you have a KVM guest running and not using 'perf kvm'?\n",
1104 session->hists.stats.nr_unprocessable_samples);
1105 }
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001106}
1107
Tom Zanussi8dc58102010-04-01 23:59:15 -05001108#define session_done() (*(volatile int *)(&session_done))
1109volatile int session_done;
1110
1111static int __perf_session__process_pipe_events(struct perf_session *self,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001112 struct perf_tool *tool)
Tom Zanussi8dc58102010-04-01 23:59:15 -05001113{
Stephane Eranian444d2862012-05-15 13:28:12 +02001114 union perf_event *event;
1115 uint32_t size, cur_size = 0;
1116 void *buf = NULL;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001117 int skip = 0;
1118 u64 head;
1119 int err;
1120 void *p;
1121
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001122 perf_tool__fill_defaults(tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001123
1124 head = 0;
Stephane Eranian444d2862012-05-15 13:28:12 +02001125 cur_size = sizeof(union perf_event);
1126
1127 buf = malloc(cur_size);
1128 if (!buf)
1129 return -errno;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001130more:
Stephane Eranian444d2862012-05-15 13:28:12 +02001131 event = buf;
1132 err = readn(self->fd, event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05001133 if (err <= 0) {
1134 if (err == 0)
1135 goto done;
1136
1137 pr_err("failed to read event header\n");
1138 goto out_err;
1139 }
1140
1141 if (self->header.needs_swap)
Stephane Eranian444d2862012-05-15 13:28:12 +02001142 perf_event_header__bswap(&event->header);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001143
Stephane Eranian444d2862012-05-15 13:28:12 +02001144 size = event->header.size;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001145 if (size == 0)
1146 size = 8;
1147
Stephane Eranian444d2862012-05-15 13:28:12 +02001148 if (size > cur_size) {
1149 void *new = realloc(buf, size);
1150 if (!new) {
1151 pr_err("failed to allocate memory to read event\n");
1152 goto out_err;
1153 }
1154 buf = new;
1155 cur_size = size;
1156 event = buf;
1157 }
1158 p = event;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001159 p += sizeof(struct perf_event_header);
1160
Tom Zanussi794e43b2010-05-05 00:27:40 -05001161 if (size - sizeof(struct perf_event_header)) {
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02001162 err = readn(self->fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -05001163 if (err <= 0) {
1164 if (err == 0) {
1165 pr_err("unexpected end of event stream\n");
1166 goto done;
1167 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001168
Tom Zanussi794e43b2010-05-05 00:27:40 -05001169 pr_err("failed to read event data\n");
1170 goto out_err;
1171 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001172 }
1173
Stephane Eranian444d2862012-05-15 13:28:12 +02001174 if ((skip = perf_session__process_event(self, event, tool, head)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001175 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
Stephane Eranian444d2862012-05-15 13:28:12 +02001176 head, event->header.size, event->header.type);
Jiri Olsa9389a462012-04-16 20:42:51 +02001177 err = -EINVAL;
1178 goto out_err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001179 }
1180
1181 head += size;
1182
Tom Zanussi8dc58102010-04-01 23:59:15 -05001183 if (skip > 0)
1184 head += skip;
1185
1186 if (!session_done())
1187 goto more;
1188done:
1189 err = 0;
1190out_err:
Stephane Eranian444d2862012-05-15 13:28:12 +02001191 free(buf);
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001192 perf_session__warn_about_errors(self, tool);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001193 perf_session_free_sample_buffers(self);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001194 return err;
1195}
1196
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001197static union perf_event *
1198fetch_mmaped_event(struct perf_session *session,
1199 u64 head, size_t mmap_size, char *buf)
1200{
1201 union perf_event *event;
1202
1203 /*
1204 * Ensure we have enough space remaining to read
1205 * the size of the event in the headers.
1206 */
1207 if (head + sizeof(event->header) > mmap_size)
1208 return NULL;
1209
1210 event = (union perf_event *)(buf + head);
1211
1212 if (session->header.needs_swap)
1213 perf_event_header__bswap(&event->header);
1214
1215 if (head + event->header.size > mmap_size)
1216 return NULL;
1217
1218 return event;
1219}
1220
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001221int __perf_session__process_events(struct perf_session *session,
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001222 u64 data_offset, u64 data_size,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001223 u64 file_size, struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001224{
Thomas Gleixner55b44622010-11-30 17:49:46 +00001225 u64 head, page_offset, file_offset, file_pos, progress_next;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001226 int err, mmap_prot, mmap_flags, map_idx = 0;
Thomas Gleixner55b44622010-11-30 17:49:46 +00001227 size_t page_size, mmap_size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001228 char *buf, *mmaps[8];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001229 union perf_event *event;
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001230 uint32_t size;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001231
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001232 perf_tool__fill_defaults(tool);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001233
Arnaldo Carvalho de Melo1b759622010-01-14 18:30:04 -02001234 page_size = sysconf(_SC_PAGESIZE);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001235
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001236 page_offset = page_size * (data_offset / page_size);
1237 file_offset = page_offset;
1238 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001239
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001240 if (data_offset + data_size < file_size)
1241 file_size = data_offset + data_size;
1242
Thomas Gleixner55b44622010-11-30 17:49:46 +00001243 progress_next = file_size / 16;
Thomas Gleixner55b44622010-11-30 17:49:46 +00001244
1245 mmap_size = session->mmap_window;
1246 if (mmap_size > file_size)
1247 mmap_size = file_size;
1248
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001249 memset(mmaps, 0, sizeof(mmaps));
1250
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001251 mmap_prot = PROT_READ;
1252 mmap_flags = MAP_SHARED;
1253
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001254 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001255 mmap_prot |= PROT_WRITE;
1256 mmap_flags = MAP_PRIVATE;
1257 }
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001258remap:
Thomas Gleixner55b44622010-11-30 17:49:46 +00001259 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
1260 file_offset);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001261 if (buf == MAP_FAILED) {
1262 pr_err("failed to mmap file\n");
1263 err = -errno;
1264 goto out_err;
1265 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001266 mmaps[map_idx] = buf;
1267 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001268 file_pos = file_offset + head;
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001269
1270more:
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001271 event = fetch_mmaped_event(session, head, mmap_size, buf);
1272 if (!event) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001273 if (mmaps[map_idx]) {
1274 munmap(mmaps[map_idx], mmap_size);
1275 mmaps[map_idx] = NULL;
1276 }
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001277
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001278 page_offset = page_size * (head / page_size);
1279 file_offset += page_offset;
1280 head -= page_offset;
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001281 goto remap;
1282 }
1283
1284 size = event->header.size;
1285
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001286 if (size == 0 ||
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001287 perf_session__process_event(session, event, tool, file_pos) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001288 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1289 file_offset + head, event->header.size,
1290 event->header.type);
1291 err = -EINVAL;
1292 goto out_err;
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001293 }
1294
1295 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001296 file_pos += size;
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001297
Thomas Gleixner55b44622010-11-30 17:49:46 +00001298 if (file_pos >= progress_next) {
1299 progress_next += file_size / 16;
Arnaldo Carvalho de Meloca59bcb2011-10-25 13:29:11 -02001300 ui_progress__update(file_pos, file_size,
1301 "Processing events...");
Thomas Gleixner55b44622010-11-30 17:49:46 +00001302 }
1303
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001304 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001305 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001306
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001307 err = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001308 /* do the final flush for ordered samples */
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001309 session->ordered_samples.next_flush = ULLONG_MAX;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001310 flush_sample_queue(session, tool);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001311out_err:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001312 perf_session__warn_about_errors(session, tool);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001313 perf_session_free_sample_buffers(session);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001314 return err;
1315}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001316
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001317int perf_session__process_events(struct perf_session *self,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001318 struct perf_tool *tool)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001319{
1320 int err;
1321
1322 if (perf_session__register_idle_thread(self) == NULL)
1323 return -ENOMEM;
1324
Tom Zanussi8dc58102010-04-01 23:59:15 -05001325 if (!self->fd_pipe)
1326 err = __perf_session__process_events(self,
1327 self->header.data_offset,
1328 self->header.data_size,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001329 self->size, tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001330 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001331 err = __perf_session__process_pipe_events(self, tool);
Dave Martin88ca8952010-07-27 11:46:12 -03001332
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001333 return err;
1334}
1335
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001336bool perf_session__has_traces(struct perf_session *self, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001337{
1338 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001339 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1340 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001341 }
1342
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001343 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001344}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001345
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001346int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1347 const char *symbol_name, u64 addr)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001348{
1349 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001350 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001351 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001352
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001353 ref = zalloc(sizeof(struct ref_reloc_sym));
1354 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001355 return -ENOMEM;
1356
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001357 ref->name = strdup(symbol_name);
1358 if (ref->name == NULL) {
1359 free(ref);
1360 return -ENOMEM;
1361 }
1362
1363 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001364 if (bracket)
1365 *bracket = '\0';
1366
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001367 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001368
1369 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001370 struct kmap *kmap = map__kmap(maps[i]);
1371 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001372 }
1373
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001374 return 0;
1375}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001376
1377size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
1378{
1379 return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
1380 __dsos__fprintf(&self->host_machine.user_dsos, fp) +
1381 machines__fprintf_dsos(&self->machines, fp);
1382}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001383
1384size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
1385 bool with_hits)
1386{
1387 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
1388 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
1389}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001390
1391size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1392{
1393 struct perf_evsel *pos;
1394 size_t ret = fprintf(fp, "Aggregated stats:\n");
1395
1396 ret += hists__fprintf_nr_events(&session->hists, fp);
1397
1398 list_for_each_entry(pos, &session->evlist->entries, node) {
1399 ret += fprintf(fp, "%s stats:\n", event_name(pos));
1400 ret += hists__fprintf_nr_events(&pos->hists, fp);
1401 }
1402
1403 return ret;
1404}
David Ahernc0230b22011-03-09 22:23:27 -07001405
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001406size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1407{
1408 /*
1409 * FIXME: Here we have to actually print all the machines in this
1410 * session, not just the host...
1411 */
1412 return machine__fprintf(&session->host_machine, fp);
1413}
1414
1415void perf_session__remove_thread(struct perf_session *session,
1416 struct thread *th)
1417{
1418 /*
1419 * FIXME: This one makes no sense, we need to remove the thread from
1420 * the machine it belongs to, perf_session can have many machines, so
1421 * doing it always on ->host_machine is wrong. Fix when auditing all
1422 * the 'perf kvm' code.
1423 */
1424 machine__remove_thread(&session->host_machine, th);
1425}
1426
David Ahern9cbdb702011-04-06 21:54:20 -06001427struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1428 unsigned int type)
1429{
1430 struct perf_evsel *pos;
1431
1432 list_for_each_entry(pos, &session->evlist->entries, node) {
1433 if (pos->attr.type == type)
1434 return pos;
1435 }
1436 return NULL;
1437}
1438
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001439void perf_event__print_ip(union perf_event *event, struct perf_sample *sample,
1440 struct machine *machine, struct perf_evsel *evsel,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001441 int print_sym, int print_dso, int print_symoffset)
David Ahernc0230b22011-03-09 22:23:27 -07001442{
1443 struct addr_location al;
David Ahernc0230b22011-03-09 22:23:27 -07001444 struct callchain_cursor_node *node;
1445
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001446 if (perf_event__preprocess_sample(event, machine, &al, sample,
David Ahernc0230b22011-03-09 22:23:27 -07001447 NULL) < 0) {
1448 error("problem processing %d event, skipping it.\n",
1449 event->header.type);
1450 return;
1451 }
1452
1453 if (symbol_conf.use_callchain && sample->callchain) {
1454
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001455 if (machine__resolve_callchain(machine, evsel, al.thread,
David Ahernc0230b22011-03-09 22:23:27 -07001456 sample->callchain, NULL) != 0) {
1457 if (verbose)
1458 error("Failed to resolve callchain. Skipping\n");
1459 return;
1460 }
Namhyung Kim47260642012-05-31 14:43:26 +09001461 callchain_cursor_commit(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001462
1463 while (1) {
Namhyung Kim47260642012-05-31 14:43:26 +09001464 node = callchain_cursor_current(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001465 if (!node)
1466 break;
1467
David Ahern787bef12011-05-27 14:28:43 -06001468 printf("\t%16" PRIx64, node->ip);
1469 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001470 printf(" ");
1471 symbol__fprintf_symname(node->sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001472 }
1473 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001474 printf(" (");
David Ahern52deff72012-05-29 22:58:26 -06001475 map__fprintf_dsoname(node->map, stdout);
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001476 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001477 }
1478 printf("\n");
David Ahernc0230b22011-03-09 22:23:27 -07001479
Namhyung Kim47260642012-05-31 14:43:26 +09001480 callchain_cursor_advance(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001481 }
1482
1483 } else {
David Ahernadc4bf92011-05-30 09:16:27 -06001484 printf("%16" PRIx64, sample->ip);
David Ahern787bef12011-05-27 14:28:43 -06001485 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001486 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001487 if (print_symoffset)
1488 symbol__fprintf_symname_offs(al.sym, &al,
1489 stdout);
1490 else
1491 symbol__fprintf_symname(al.sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001492 }
1493
1494 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001495 printf(" (");
1496 map__fprintf_dsoname(al.map, stdout);
1497 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001498 }
David Ahernc0230b22011-03-09 22:23:27 -07001499 }
1500}
Anton Blanchard5d67be92011-07-04 21:57:50 +10001501
1502int perf_session__cpu_bitmap(struct perf_session *session,
1503 const char *cpu_list, unsigned long *cpu_bitmap)
1504{
1505 int i;
1506 struct cpu_map *map;
1507
1508 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1509 struct perf_evsel *evsel;
1510
1511 evsel = perf_session__find_first_evtype(session, i);
1512 if (!evsel)
1513 continue;
1514
1515 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1516 pr_err("File does not contain CPU events. "
1517 "Remove -c option to proceed.\n");
1518 return -1;
1519 }
1520 }
1521
1522 map = cpu_map__new(cpu_list);
David Ahern47fbe532011-11-13 10:45:27 -07001523 if (map == NULL) {
1524 pr_err("Invalid cpu_list\n");
1525 return -1;
1526 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001527
1528 for (i = 0; i < map->nr; i++) {
1529 int cpu = map->map[i];
1530
1531 if (cpu >= MAX_NR_CPUS) {
1532 pr_err("Requested CPU %d too large. "
1533 "Consider raising MAX_NR_CPUS\n", cpu);
1534 return -1;
1535 }
1536
1537 set_bit(cpu, cpu_bitmap);
1538 }
1539
1540 return 0;
1541}
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001542
1543void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1544 bool full)
1545{
1546 struct stat st;
1547 int ret;
1548
1549 if (session == NULL || fp == NULL)
1550 return;
1551
1552 ret = fstat(session->fd, &st);
1553 if (ret == -1)
1554 return;
1555
1556 fprintf(fp, "# ========\n");
1557 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1558 perf_header__fprintf_info(session, fp, full);
1559 fprintf(fp, "# ========\n#\n");
1560}