blob: 6b305fbcc986d0b3127392716c6154523fab7f15 [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 Gleixner55b446292010-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 Melo64abebf72010-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 Melo64abebf72010-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 Melo64abebf72010-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,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200292 struct thread *thread,
293 struct ip_callchain *chain,
294 struct symbol **parent)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200295{
296 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200297 unsigned int i;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100298 int err;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200299
Namhyung Kim47260642012-05-31 14:43:26 +0900300 callchain_cursor_reset(&callchain_cursor);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200301
Namhyung Kim114067b2012-05-31 14:43:27 +0900302 if (chain->nr > PERF_MAX_STACK_DEPTH) {
303 pr_warning("corrupted callchain. skipping...\n");
304 return 0;
305 }
306
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200307 for (i = 0; i < chain->nr; i++) {
Sam Liaod797fdc2011-06-07 23:49:46 +0800308 u64 ip;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200309 struct addr_location al;
310
Sam Liaod797fdc2011-06-07 23:49:46 +0800311 if (callchain_param.order == ORDER_CALLEE)
312 ip = chain->ips[i];
313 else
314 ip = chain->ips[chain->nr - i - 1];
315
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200316 if (ip >= PERF_CONTEXT_MAX) {
317 switch (ip) {
318 case PERF_CONTEXT_HV:
319 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
320 case PERF_CONTEXT_KERNEL:
321 cpumode = PERF_RECORD_MISC_KERNEL; break;
322 case PERF_CONTEXT_USER:
323 cpumode = PERF_RECORD_MISC_USER; break;
324 default:
Namhyung Kim114067b2012-05-31 14:43:27 +0900325 pr_debug("invalid callchain context: "
326 "%"PRId64"\n", (s64) ip);
327 /*
328 * It seems the callchain is corrupted.
329 * Discard all.
330 */
331 callchain_cursor_reset(&callchain_cursor);
332 return 0;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200333 }
334 continue;
335 }
336
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800337 al.filtered = false;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200338 thread__find_addr_location(thread, self, cpumode,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200339 MAP__FUNCTION, ip, &al, NULL);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200340 if (al.sym != NULL) {
341 if (sort__has_parent && !*parent &&
342 symbol__match_parent_regex(al.sym))
343 *parent = al.sym;
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200344 if (!symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200345 break;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200346 }
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100347
Namhyung Kim47260642012-05-31 14:43:26 +0900348 err = callchain_cursor_append(&callchain_cursor,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100349 ip, al.map, al.sym);
350 if (err)
351 return err;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200352 }
353
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100354 return 0;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200355}
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200356
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200357static int process_event_synth_tracing_data_stub(union perf_event *event __used,
358 struct perf_session *session __used)
359{
360 dump_printf(": unhandled!\n");
361 return 0;
362}
363
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200364static int process_event_synth_attr_stub(union perf_event *event __used,
365 struct perf_evlist **pevlist __used)
366{
367 dump_printf(": unhandled!\n");
368 return 0;
369}
370
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200371static int process_event_sample_stub(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200372 union perf_event *event __used,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300373 struct perf_sample *sample __used,
374 struct perf_evsel *evsel __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200375 struct machine *machine __used)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300376{
377 dump_printf(": unhandled!\n");
378 return 0;
379}
380
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200381static int process_event_stub(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200382 union perf_event *event __used,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200383 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200384 struct machine *machine __used)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200385{
386 dump_printf(": unhandled!\n");
387 return 0;
388}
389
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200390static int process_finished_round_stub(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200391 union perf_event *event __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200392 struct perf_session *perf_session __used)
393{
394 dump_printf(": unhandled!\n");
395 return 0;
396}
397
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200398static int process_event_type_stub(struct perf_tool *tool __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200399 union perf_event *event __used)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200400{
401 dump_printf(": unhandled!\n");
402 return 0;
403}
404
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200405static int process_finished_round(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200406 union perf_event *event,
407 struct perf_session *session);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200408
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200409static void perf_tool__fill_defaults(struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200410{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200411 if (tool->sample == NULL)
412 tool->sample = process_event_sample_stub;
413 if (tool->mmap == NULL)
414 tool->mmap = process_event_stub;
415 if (tool->comm == NULL)
416 tool->comm = process_event_stub;
417 if (tool->fork == NULL)
418 tool->fork = process_event_stub;
419 if (tool->exit == NULL)
420 tool->exit = process_event_stub;
421 if (tool->lost == NULL)
422 tool->lost = perf_event__process_lost;
423 if (tool->read == NULL)
424 tool->read = process_event_sample_stub;
425 if (tool->throttle == NULL)
426 tool->throttle = process_event_stub;
427 if (tool->unthrottle == NULL)
428 tool->unthrottle = process_event_stub;
429 if (tool->attr == NULL)
430 tool->attr = process_event_synth_attr_stub;
431 if (tool->event_type == NULL)
432 tool->event_type = process_event_type_stub;
433 if (tool->tracing_data == NULL)
434 tool->tracing_data = process_event_synth_tracing_data_stub;
435 if (tool->build_id == NULL)
436 tool->build_id = process_finished_round_stub;
437 if (tool->finished_round == NULL) {
438 if (tool->ordered_samples)
439 tool->finished_round = process_finished_round;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200440 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200441 tool->finished_round = process_finished_round_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200442 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200443}
David Ahern80c01202012-06-08 11:47:51 -0300444
445void mem_bswap_32(void *src, int byte_size)
446{
447 u32 *m = src;
448 while (byte_size > 0) {
449 *m = bswap_32(*m);
450 byte_size -= sizeof(u32);
451 ++m;
452 }
453}
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200454
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200455void mem_bswap_64(void *src, int byte_size)
456{
457 u64 *m = src;
458
459 while (byte_size > 0) {
460 *m = bswap_64(*m);
461 byte_size -= sizeof(u64);
462 ++m;
463 }
464}
465
Jiri Olsa268fb202012-05-30 14:23:43 +0200466static void swap_sample_id_all(union perf_event *event, void *data)
467{
468 void *end = (void *) event + event->header.size;
469 int size = end - data;
470
471 BUG_ON(size % sizeof(u64));
472 mem_bswap_64(data, size);
473}
474
475static void perf_event__all64_swap(union perf_event *event,
476 bool sample_id_all __used)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200477{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200478 struct perf_event_header *hdr = &event->header;
479 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200480}
481
Jiri Olsa268fb202012-05-30 14:23:43 +0200482static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200483{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200484 event->comm.pid = bswap_32(event->comm.pid);
485 event->comm.tid = bswap_32(event->comm.tid);
Jiri Olsa268fb202012-05-30 14:23:43 +0200486
487 if (sample_id_all) {
488 void *data = &event->comm.comm;
489
490 data += ALIGN(strlen(data) + 1, sizeof(u64));
491 swap_sample_id_all(event, data);
492 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200493}
494
Jiri Olsa268fb202012-05-30 14:23:43 +0200495static void perf_event__mmap_swap(union perf_event *event,
496 bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200497{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200498 event->mmap.pid = bswap_32(event->mmap.pid);
499 event->mmap.tid = bswap_32(event->mmap.tid);
500 event->mmap.start = bswap_64(event->mmap.start);
501 event->mmap.len = bswap_64(event->mmap.len);
502 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
Jiri Olsa268fb202012-05-30 14:23:43 +0200503
504 if (sample_id_all) {
505 void *data = &event->mmap.filename;
506
507 data += ALIGN(strlen(data) + 1, sizeof(u64));
508 swap_sample_id_all(event, data);
509 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200510}
511
Jiri Olsa268fb202012-05-30 14:23:43 +0200512static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200513{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200514 event->fork.pid = bswap_32(event->fork.pid);
515 event->fork.tid = bswap_32(event->fork.tid);
516 event->fork.ppid = bswap_32(event->fork.ppid);
517 event->fork.ptid = bswap_32(event->fork.ptid);
518 event->fork.time = bswap_64(event->fork.time);
Jiri Olsa268fb202012-05-30 14:23:43 +0200519
520 if (sample_id_all)
521 swap_sample_id_all(event, &event->fork + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200522}
523
Jiri Olsa268fb202012-05-30 14:23:43 +0200524static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200525{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200526 event->read.pid = bswap_32(event->read.pid);
527 event->read.tid = bswap_32(event->read.tid);
528 event->read.value = bswap_64(event->read.value);
529 event->read.time_enabled = bswap_64(event->read.time_enabled);
530 event->read.time_running = bswap_64(event->read.time_running);
531 event->read.id = bswap_64(event->read.id);
Jiri Olsa268fb202012-05-30 14:23:43 +0200532
533 if (sample_id_all)
534 swap_sample_id_all(event, &event->read + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200535}
536
Jiri Olsae108c662012-05-16 08:59:03 +0200537static u8 revbyte(u8 b)
538{
539 int rev = (b >> 4) | ((b & 0xf) << 4);
540 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
541 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
542 return (u8) rev;
543}
544
545/*
546 * XXX this is hack in attempt to carry flags bitfield
547 * throught endian village. ABI says:
548 *
549 * Bit-fields are allocated from right to left (least to most significant)
550 * on little-endian implementations and from left to right (most to least
551 * significant) on big-endian implementations.
552 *
553 * The above seems to be byte specific, so we need to reverse each
554 * byte of the bitfield. 'Internet' also says this might be implementation
555 * specific and we probably need proper fix and carry perf_event_attr
556 * bitfield flags in separate data file FEAT_ section. Thought this seems
557 * to work for now.
558 */
559static void swap_bitfield(u8 *p, unsigned len)
560{
561 unsigned i;
562
563 for (i = 0; i < len; i++) {
564 *p = revbyte(*p);
565 p++;
566 }
567}
568
David Aherneda39132011-07-15 12:34:09 -0600569/* exported for swapping attributes in file header */
570void perf_event__attr_swap(struct perf_event_attr *attr)
571{
572 attr->type = bswap_32(attr->type);
573 attr->size = bswap_32(attr->size);
574 attr->config = bswap_64(attr->config);
575 attr->sample_period = bswap_64(attr->sample_period);
576 attr->sample_type = bswap_64(attr->sample_type);
577 attr->read_format = bswap_64(attr->read_format);
578 attr->wakeup_events = bswap_32(attr->wakeup_events);
579 attr->bp_type = bswap_32(attr->bp_type);
580 attr->bp_addr = bswap_64(attr->bp_addr);
581 attr->bp_len = bswap_64(attr->bp_len);
Jiri Olsae108c662012-05-16 08:59:03 +0200582
583 swap_bitfield((u8 *) (&attr->read_format + 1), sizeof(u64));
David Aherneda39132011-07-15 12:34:09 -0600584}
585
Jiri Olsa268fb202012-05-30 14:23:43 +0200586static void perf_event__hdr_attr_swap(union perf_event *event,
587 bool sample_id_all __used)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500588{
589 size_t size;
590
David Aherneda39132011-07-15 12:34:09 -0600591 perf_event__attr_swap(&event->attr.attr);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500592
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200593 size = event->header.size;
594 size -= (void *)&event->attr.id - (void *)event;
595 mem_bswap_64(event->attr.id, size);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500596}
597
Jiri Olsa268fb202012-05-30 14:23:43 +0200598static void perf_event__event_type_swap(union perf_event *event,
599 bool sample_id_all __used)
Tom Zanussicd19a032010-04-01 23:59:20 -0500600{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200601 event->event_type.event_type.event_id =
602 bswap_64(event->event_type.event_type.event_id);
Tom Zanussicd19a032010-04-01 23:59:20 -0500603}
604
Jiri Olsa268fb202012-05-30 14:23:43 +0200605static void perf_event__tracing_data_swap(union perf_event *event,
606 bool sample_id_all __used)
Tom Zanussi92155452010-04-01 23:59:21 -0500607{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200608 event->tracing_data.size = bswap_32(event->tracing_data.size);
Tom Zanussi92155452010-04-01 23:59:21 -0500609}
610
Jiri Olsa268fb202012-05-30 14:23:43 +0200611typedef void (*perf_event__swap_op)(union perf_event *event,
612 bool sample_id_all);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200613
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200614static perf_event__swap_op perf_event__swap_ops[] = {
615 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
616 [PERF_RECORD_COMM] = perf_event__comm_swap,
617 [PERF_RECORD_FORK] = perf_event__task_swap,
618 [PERF_RECORD_EXIT] = perf_event__task_swap,
619 [PERF_RECORD_LOST] = perf_event__all64_swap,
620 [PERF_RECORD_READ] = perf_event__read_swap,
621 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
David Aherneda39132011-07-15 12:34:09 -0600622 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200623 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
624 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
625 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
626 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200627};
628
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200629struct sample_queue {
630 u64 timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000631 u64 file_offset;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200632 union perf_event *event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200633 struct list_head list;
634};
635
Thomas Gleixner020bb752010-11-30 17:49:53 +0000636static void perf_session_free_sample_buffers(struct perf_session *session)
637{
638 struct ordered_samples *os = &session->ordered_samples;
639
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000640 while (!list_empty(&os->to_free)) {
Thomas Gleixner020bb752010-11-30 17:49:53 +0000641 struct sample_queue *sq;
642
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000643 sq = list_entry(os->to_free.next, struct sample_queue, list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000644 list_del(&sq->list);
645 free(sq);
646 }
647}
648
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100649static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200650 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200651 struct perf_sample *sample,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200652 struct perf_tool *tool,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000653 u64 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100654
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200655static void flush_sample_queue(struct perf_session *s,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200656 struct perf_tool *tool)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200657{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000658 struct ordered_samples *os = &s->ordered_samples;
659 struct list_head *head = &os->samples;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200660 struct sample_queue *tmp, *iter;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200661 struct perf_sample sample;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000662 u64 limit = os->next_flush;
663 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200664 unsigned idx = 0, progress_next = os->nr_samples / 16;
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200665 int ret;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200666
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200667 if (!tool->ordered_samples || !limit)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200668 return;
669
670 list_for_each_entry_safe(iter, tmp, head, list) {
671 if (iter->timestamp > limit)
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000672 break;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200673
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200674 ret = perf_session__parse_sample(s, iter->event, &sample);
675 if (ret)
676 pr_err("Can't parse sample, err = %d\n", ret);
677 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200678 perf_session_deliver_event(s, iter->event, &sample, tool,
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200679 iter->file_offset);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200680
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000681 os->last_flush = iter->timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200682 list_del(&iter->list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000683 list_add(&iter->list, &os->sample_cache);
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200684 if (++idx >= progress_next) {
685 progress_next += os->nr_samples / 16;
686 ui_progress__update(idx, os->nr_samples,
687 "Processing time ordered events...");
688 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200689 }
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000690
691 if (list_empty(head)) {
692 os->last_sample = NULL;
693 } else if (last_ts <= limit) {
694 os->last_sample =
695 list_entry(head->prev, struct sample_queue, list);
696 }
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200697
698 os->nr_samples = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200699}
700
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200701/*
702 * When perf record finishes a pass on every buffers, it records this pseudo
703 * event.
704 * We record the max timestamp t found in the pass n.
705 * Assuming these timestamps are monotonic across cpus, we know that if
706 * a buffer still has events with timestamps below t, they will be all
707 * available and then read in the pass n + 1.
708 * Hence when we start to read the pass n + 2, we can safely flush every
709 * events with timestamps below t.
710 *
711 * ============ PASS n =================
712 * CPU 0 | CPU 1
713 * |
714 * cnt1 timestamps | cnt2 timestamps
715 * 1 | 2
716 * 2 | 3
717 * - | 4 <--- max recorded
718 *
719 * ============ PASS n + 1 ==============
720 * CPU 0 | CPU 1
721 * |
722 * cnt1 timestamps | cnt2 timestamps
723 * 3 | 5
724 * 4 | 6
725 * 5 | 7 <---- max recorded
726 *
727 * Flush every events below timestamp 4
728 *
729 * ============ PASS n + 2 ==============
730 * CPU 0 | CPU 1
731 * |
732 * cnt1 timestamps | cnt2 timestamps
733 * 6 | 8
734 * 7 | 9
735 * - | 10
736 *
737 * Flush every events below timestamp 7
738 * etc...
739 */
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200740static int process_finished_round(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200741 union perf_event *event __used,
742 struct perf_session *session)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200743{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200744 flush_sample_queue(session, tool);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200745 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
746
747 return 0;
748}
749
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200750/* The queue is ordered by time */
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100751static void __queue_event(struct sample_queue *new, struct perf_session *s)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200752{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000753 struct ordered_samples *os = &s->ordered_samples;
754 struct sample_queue *sample = os->last_sample;
755 u64 timestamp = new->timestamp;
756 struct list_head *p;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200757
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200758 ++os->nr_samples;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000759 os->last_sample = new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200760
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000761 if (!sample) {
762 list_add(&new->list, &os->samples);
763 os->max_timestamp = timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200764 return;
765 }
766
767 /*
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000768 * last_sample might point to some random place in the list as it's
769 * the last queued event. We expect that the new event is close to
770 * this.
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200771 */
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000772 if (sample->timestamp <= timestamp) {
773 while (sample->timestamp <= timestamp) {
774 p = sample->list.next;
775 if (p == &os->samples) {
776 list_add_tail(&new->list, &os->samples);
777 os->max_timestamp = timestamp;
778 return;
779 }
780 sample = list_entry(p, struct sample_queue, list);
781 }
782 list_add_tail(&new->list, &sample->list);
783 } else {
784 while (sample->timestamp > timestamp) {
785 p = sample->list.prev;
786 if (p == &os->samples) {
787 list_add(&new->list, &os->samples);
788 return;
789 }
790 sample = list_entry(p, struct sample_queue, list);
791 }
792 list_add(&new->list, &sample->list);
793 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200794}
795
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000796#define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
797
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200798static int perf_session_queue_event(struct perf_session *s, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200799 struct perf_sample *sample, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200800{
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000801 struct ordered_samples *os = &s->ordered_samples;
802 struct list_head *sc = &os->sample_cache;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200803 u64 timestamp = sample->time;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200804 struct sample_queue *new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200805
Thomas Gleixner79a14c12010-12-07 12:48:44 +0000806 if (!timestamp || timestamp == ~0ULL)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100807 return -ETIME;
808
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200809 if (timestamp < s->ordered_samples.last_flush) {
810 printf("Warning: Timestamp below last timeslice flush\n");
811 return -EINVAL;
812 }
813
Thomas Gleixner020bb752010-11-30 17:49:53 +0000814 if (!list_empty(sc)) {
815 new = list_entry(sc->next, struct sample_queue, list);
816 list_del(&new->list);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000817 } else if (os->sample_buffer) {
818 new = os->sample_buffer + os->sample_buffer_idx;
819 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
820 os->sample_buffer = NULL;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000821 } else {
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000822 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
823 if (!os->sample_buffer)
Thomas Gleixner020bb752010-11-30 17:49:53 +0000824 return -ENOMEM;
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000825 list_add(&os->sample_buffer->list, &os->to_free);
826 os->sample_buffer_idx = 2;
827 new = os->sample_buffer + 1;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000828 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200829
830 new->timestamp = timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000831 new->file_offset = file_offset;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000832 new->event = event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200833
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100834 __queue_event(new, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200835
836 return 0;
837}
838
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200839static void callchain__printf(struct perf_sample *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200840{
841 unsigned int i;
842
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200843 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200844
845 for (i = 0; i < sample->callchain->nr; i++)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200846 printf("..... %2d: %016" PRIx64 "\n",
847 i, sample->callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200848}
849
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100850static void branch_stack__printf(struct perf_sample *sample)
851{
852 uint64_t i;
853
854 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
855
856 for (i = 0; i < sample->branch_stack->nr; i++)
857 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n",
858 i, sample->branch_stack->entries[i].from,
859 sample->branch_stack->entries[i].to);
860}
861
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200862static void perf_session__print_tstamp(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200863 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200864 struct perf_sample *sample)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200865{
866 if (event->header.type != PERF_RECORD_SAMPLE &&
867 !session->sample_id_all) {
868 fputs("-1 -1 ", stdout);
869 return;
870 }
871
872 if ((session->sample_type & PERF_SAMPLE_CPU))
873 printf("%u ", sample->cpu);
874
875 if (session->sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200876 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200877}
878
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200879static void dump_event(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200880 u64 file_offset, struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000881{
882 if (!dump_trace)
883 return;
884
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200885 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
886 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000887
888 trace_event(event);
889
890 if (sample)
891 perf_session__print_tstamp(session, event, sample);
892
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200893 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200894 event->header.size, perf_event__name(event->header.type));
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000895}
896
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200897static void dump_sample(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200898 struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000899{
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200900 if (!dump_trace)
901 return;
902
David Ahern7cec0922011-05-30 13:08:23 -0600903 printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200904 event->header.misc, sample->pid, sample->tid, sample->ip,
David Ahern7cec0922011-05-30 13:08:23 -0600905 sample->period, sample->addr);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000906
907 if (session->sample_type & PERF_SAMPLE_CALLCHAIN)
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200908 callchain__printf(sample);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100909
910 if (session->sample_type & PERF_SAMPLE_BRANCH_STACK)
911 branch_stack__printf(sample);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000912}
913
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200914static struct machine *
915 perf_session__find_machine_for_cpumode(struct perf_session *session,
916 union perf_event *event)
917{
918 const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
919
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530920 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
921 u32 pid;
922
923 if (event->header.type == PERF_RECORD_MMAP)
924 pid = event->mmap.pid;
925 else
926 pid = event->ip.pid;
927
928 return perf_session__find_machine(session, pid);
929 }
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200930
931 return perf_session__find_host_machine(session);
932}
933
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100934static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200935 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200936 struct perf_sample *sample,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200937 struct perf_tool *tool,
Thomas Gleixner532e7262010-12-07 12:48:55 +0000938 u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100939{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300940 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200941 struct machine *machine;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300942
Thomas Gleixner532e7262010-12-07 12:48:55 +0000943 dump_event(session, event, file_offset, sample);
944
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200945 evsel = perf_evlist__id2evsel(session->evlist, sample->id);
946 if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) {
947 /*
948 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
949 * because the tools right now may apply filters, discarding
950 * some of the samples. For consistency, in the future we
951 * should have something like nr_filtered_samples and remove
952 * the sample->period from total_sample_period, etc, KISS for
953 * now tho.
954 *
955 * Also testing against NULL allows us to handle files without
956 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
957 * future probably it'll be a good idea to restrict event
958 * processing via perf_session to files with both set.
959 */
960 hists__inc_nr_events(&evsel->hists, event->header.type);
961 }
962
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200963 machine = perf_session__find_machine_for_cpumode(session, event);
964
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100965 switch (event->header.type) {
966 case PERF_RECORD_SAMPLE:
Thomas Gleixner532e7262010-12-07 12:48:55 +0000967 dump_sample(session, event, sample);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300968 if (evsel == NULL) {
969 ++session->hists.stats.nr_unknown_id;
Jiri Olsa67822062012-04-12 14:21:01 +0200970 return 0;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300971 }
Joerg Roedel0c095712012-02-10 18:05:04 +0100972 if (machine == NULL) {
973 ++session->hists.stats.nr_unprocessable_samples;
Jiri Olsa67822062012-04-12 14:21:01 +0200974 return 0;
Joerg Roedel0c095712012-02-10 18:05:04 +0100975 }
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200976 return tool->sample(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100977 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200978 return tool->mmap(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100979 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200980 return tool->comm(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100981 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200982 return tool->fork(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100983 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200984 return tool->exit(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100985 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200986 if (tool->lost == perf_event__process_lost)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200987 session->hists.stats.total_lost += event->lost.lost;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200988 return tool->lost(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100989 case PERF_RECORD_READ:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200990 return tool->read(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100991 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200992 return tool->throttle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100993 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200994 return tool->unthrottle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100995 default:
996 ++session->hists.stats.nr_unknown_events;
997 return -1;
998 }
999}
1000
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001001static int perf_session__preprocess_sample(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001002 union perf_event *event, struct perf_sample *sample)
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001003{
1004 if (event->header.type != PERF_RECORD_SAMPLE ||
1005 !(session->sample_type & PERF_SAMPLE_CALLCHAIN))
1006 return 0;
1007
1008 if (!ip_callchain__valid(sample->callchain, event)) {
1009 pr_debug("call-chain problem with event, skipping it.\n");
1010 ++session->hists.stats.nr_invalid_chains;
1011 session->hists.stats.total_invalid_chains += sample->period;
1012 return -EINVAL;
1013 }
1014 return 0;
1015}
1016
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001017static int perf_session__process_user_event(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001018 struct perf_tool *tool, u64 file_offset)
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001019{
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001020 int err;
1021
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001022 dump_event(session, event, file_offset, NULL);
1023
1024 /* These events are processed right away */
1025 switch (event->header.type) {
1026 case PERF_RECORD_HEADER_ATTR:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001027 err = tool->attr(event, &session->evlist);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001028 if (err == 0)
1029 perf_session__update_sample_type(session);
1030 return err;
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001031 case PERF_RECORD_HEADER_EVENT_TYPE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001032 return tool->event_type(tool, event);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001033 case PERF_RECORD_HEADER_TRACING_DATA:
1034 /* setup for reading amidst mmap */
1035 lseek(session->fd, file_offset, SEEK_SET);
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001036 return tool->tracing_data(event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001037 case PERF_RECORD_HEADER_BUILD_ID:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001038 return tool->build_id(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001039 case PERF_RECORD_FINISHED_ROUND:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001040 return tool->finished_round(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001041 default:
1042 return -EINVAL;
1043 }
1044}
1045
Jiri Olsa268fb202012-05-30 14:23:43 +02001046static void event_swap(union perf_event *event, bool sample_id_all)
1047{
1048 perf_event__swap_op swap;
1049
1050 swap = perf_event__swap_ops[event->header.type];
1051 if (swap)
1052 swap(event, sample_id_all);
1053}
1054
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001055static int perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001056 union perf_event *event,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001057 struct perf_tool *tool,
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001058 u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001059{
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001060 struct perf_sample sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001061 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001062
Jiri Olsa268fb202012-05-30 14:23:43 +02001063 if (session->header.needs_swap)
1064 event_swap(event, session->sample_id_all);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001065
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001066 if (event->header.type >= PERF_RECORD_HEADER_MAX)
1067 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001068
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001069 hists__inc_nr_events(&session->hists, event->header.type);
1070
1071 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001072 return perf_session__process_user_event(session, event, tool, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001073
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001074 /*
1075 * For all kernel events we get the sample data
1076 */
Frederic Weisbecker5538bec2011-05-22 02:17:22 +02001077 ret = perf_session__parse_sample(session, event, &sample);
1078 if (ret)
1079 return ret;
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001080
1081 /* Preprocess sample records - precheck callchains */
1082 if (perf_session__preprocess_sample(session, event, &sample))
1083 return 0;
1084
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001085 if (tool->ordered_samples) {
Thomas Gleixnere4c2df12010-12-07 12:48:50 +00001086 ret = perf_session_queue_event(session, event, &sample,
1087 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001088 if (ret != -ETIME)
1089 return ret;
1090 }
1091
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001092 return perf_session_deliver_event(session, event, &sample, tool,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +00001093 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001094}
1095
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001096void perf_event_header__bswap(struct perf_event_header *self)
1097{
1098 self->type = bswap_32(self->type);
1099 self->misc = bswap_16(self->misc);
1100 self->size = bswap_16(self->size);
1101}
1102
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001103struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1104{
1105 return machine__findnew_thread(&session->host_machine, pid);
1106}
1107
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001108static struct thread *perf_session__register_idle_thread(struct perf_session *self)
1109{
1110 struct thread *thread = perf_session__findnew(self, 0);
1111
1112 if (thread == NULL || thread__set_comm(thread, "swapper")) {
1113 pr_err("problem inserting idle task.\n");
1114 thread = NULL;
1115 }
1116
1117 return thread;
1118}
1119
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001120static void perf_session__warn_about_errors(const struct perf_session *session,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001121 const struct perf_tool *tool)
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001122{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001123 if (tool->lost == perf_event__process_lost &&
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001124 session->hists.stats.nr_events[PERF_RECORD_LOST] != 0) {
1125 ui__warning("Processed %d events and lost %d chunks!\n\n"
1126 "Check IO/CPU overload!\n\n",
1127 session->hists.stats.nr_events[0],
1128 session->hists.stats.nr_events[PERF_RECORD_LOST]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001129 }
1130
1131 if (session->hists.stats.nr_unknown_events != 0) {
1132 ui__warning("Found %u unknown events!\n\n"
1133 "Is this an older tool processing a perf.data "
1134 "file generated by a more recent tool?\n\n"
1135 "If that is not the case, consider "
1136 "reporting to linux-kernel@vger.kernel.org.\n\n",
1137 session->hists.stats.nr_unknown_events);
1138 }
1139
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001140 if (session->hists.stats.nr_unknown_id != 0) {
1141 ui__warning("%u samples with id not present in the header\n",
1142 session->hists.stats.nr_unknown_id);
1143 }
1144
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001145 if (session->hists.stats.nr_invalid_chains != 0) {
1146 ui__warning("Found invalid callchains!\n\n"
1147 "%u out of %u events were discarded for this reason.\n\n"
1148 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1149 session->hists.stats.nr_invalid_chains,
1150 session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
1151 }
Joerg Roedel0c095712012-02-10 18:05:04 +01001152
1153 if (session->hists.stats.nr_unprocessable_samples != 0) {
1154 ui__warning("%u unprocessable samples recorded.\n"
1155 "Do you have a KVM guest running and not using 'perf kvm'?\n",
1156 session->hists.stats.nr_unprocessable_samples);
1157 }
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001158}
1159
Tom Zanussi8dc58102010-04-01 23:59:15 -05001160#define session_done() (*(volatile int *)(&session_done))
1161volatile int session_done;
1162
1163static int __perf_session__process_pipe_events(struct perf_session *self,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001164 struct perf_tool *tool)
Tom Zanussi8dc58102010-04-01 23:59:15 -05001165{
Stephane Eranian444d2862012-05-15 13:28:12 +02001166 union perf_event *event;
1167 uint32_t size, cur_size = 0;
1168 void *buf = NULL;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001169 int skip = 0;
1170 u64 head;
1171 int err;
1172 void *p;
1173
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001174 perf_tool__fill_defaults(tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001175
1176 head = 0;
Stephane Eranian444d2862012-05-15 13:28:12 +02001177 cur_size = sizeof(union perf_event);
1178
1179 buf = malloc(cur_size);
1180 if (!buf)
1181 return -errno;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001182more:
Stephane Eranian444d2862012-05-15 13:28:12 +02001183 event = buf;
1184 err = readn(self->fd, event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05001185 if (err <= 0) {
1186 if (err == 0)
1187 goto done;
1188
1189 pr_err("failed to read event header\n");
1190 goto out_err;
1191 }
1192
1193 if (self->header.needs_swap)
Stephane Eranian444d2862012-05-15 13:28:12 +02001194 perf_event_header__bswap(&event->header);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001195
Stephane Eranian444d2862012-05-15 13:28:12 +02001196 size = event->header.size;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001197 if (size == 0)
1198 size = 8;
1199
Stephane Eranian444d2862012-05-15 13:28:12 +02001200 if (size > cur_size) {
1201 void *new = realloc(buf, size);
1202 if (!new) {
1203 pr_err("failed to allocate memory to read event\n");
1204 goto out_err;
1205 }
1206 buf = new;
1207 cur_size = size;
1208 event = buf;
1209 }
1210 p = event;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001211 p += sizeof(struct perf_event_header);
1212
Tom Zanussi794e43b2010-05-05 00:27:40 -05001213 if (size - sizeof(struct perf_event_header)) {
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02001214 err = readn(self->fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -05001215 if (err <= 0) {
1216 if (err == 0) {
1217 pr_err("unexpected end of event stream\n");
1218 goto done;
1219 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001220
Tom Zanussi794e43b2010-05-05 00:27:40 -05001221 pr_err("failed to read event data\n");
1222 goto out_err;
1223 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001224 }
1225
Stephane Eranian444d2862012-05-15 13:28:12 +02001226 if ((skip = perf_session__process_event(self, event, tool, head)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001227 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
Stephane Eranian444d2862012-05-15 13:28:12 +02001228 head, event->header.size, event->header.type);
Jiri Olsa9389a462012-04-16 20:42:51 +02001229 err = -EINVAL;
1230 goto out_err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001231 }
1232
1233 head += size;
1234
Tom Zanussi8dc58102010-04-01 23:59:15 -05001235 if (skip > 0)
1236 head += skip;
1237
1238 if (!session_done())
1239 goto more;
1240done:
1241 err = 0;
1242out_err:
Stephane Eranian444d2862012-05-15 13:28:12 +02001243 free(buf);
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001244 perf_session__warn_about_errors(self, tool);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001245 perf_session_free_sample_buffers(self);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001246 return err;
1247}
1248
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001249static union perf_event *
1250fetch_mmaped_event(struct perf_session *session,
1251 u64 head, size_t mmap_size, char *buf)
1252{
1253 union perf_event *event;
1254
1255 /*
1256 * Ensure we have enough space remaining to read
1257 * the size of the event in the headers.
1258 */
1259 if (head + sizeof(event->header) > mmap_size)
1260 return NULL;
1261
1262 event = (union perf_event *)(buf + head);
1263
1264 if (session->header.needs_swap)
1265 perf_event_header__bswap(&event->header);
1266
1267 if (head + event->header.size > mmap_size)
1268 return NULL;
1269
1270 return event;
1271}
1272
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001273int __perf_session__process_events(struct perf_session *session,
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001274 u64 data_offset, u64 data_size,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001275 u64 file_size, struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001276{
Thomas Gleixner55b446292010-11-30 17:49:46 +00001277 u64 head, page_offset, file_offset, file_pos, progress_next;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001278 int err, mmap_prot, mmap_flags, map_idx = 0;
Thomas Gleixner55b446292010-11-30 17:49:46 +00001279 size_t page_size, mmap_size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001280 char *buf, *mmaps[8];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001281 union perf_event *event;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001282 uint32_t size;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001283
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001284 perf_tool__fill_defaults(tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001285
Arnaldo Carvalho de Melo1b759622010-01-14 18:30:04 -02001286 page_size = sysconf(_SC_PAGESIZE);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001287
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001288 page_offset = page_size * (data_offset / page_size);
1289 file_offset = page_offset;
1290 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001291
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001292 if (data_offset + data_size < file_size)
1293 file_size = data_offset + data_size;
1294
Thomas Gleixner55b446292010-11-30 17:49:46 +00001295 progress_next = file_size / 16;
Thomas Gleixner55b446292010-11-30 17:49:46 +00001296
1297 mmap_size = session->mmap_window;
1298 if (mmap_size > file_size)
1299 mmap_size = file_size;
1300
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001301 memset(mmaps, 0, sizeof(mmaps));
1302
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001303 mmap_prot = PROT_READ;
1304 mmap_flags = MAP_SHARED;
1305
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001306 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001307 mmap_prot |= PROT_WRITE;
1308 mmap_flags = MAP_PRIVATE;
1309 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001310remap:
Thomas Gleixner55b446292010-11-30 17:49:46 +00001311 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
1312 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001313 if (buf == MAP_FAILED) {
1314 pr_err("failed to mmap file\n");
1315 err = -errno;
1316 goto out_err;
1317 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001318 mmaps[map_idx] = buf;
1319 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001320 file_pos = file_offset + head;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001321
1322more:
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001323 event = fetch_mmaped_event(session, head, mmap_size, buf);
1324 if (!event) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001325 if (mmaps[map_idx]) {
1326 munmap(mmaps[map_idx], mmap_size);
1327 mmaps[map_idx] = NULL;
1328 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001329
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001330 page_offset = page_size * (head / page_size);
1331 file_offset += page_offset;
1332 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001333 goto remap;
1334 }
1335
1336 size = event->header.size;
1337
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001338 if (size == 0 ||
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001339 perf_session__process_event(session, event, tool, file_pos) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001340 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1341 file_offset + head, event->header.size,
1342 event->header.type);
1343 err = -EINVAL;
1344 goto out_err;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001345 }
1346
1347 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001348 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001349
Thomas Gleixner55b446292010-11-30 17:49:46 +00001350 if (file_pos >= progress_next) {
1351 progress_next += file_size / 16;
Arnaldo Carvalho de Meloca59bcb2011-10-25 13:29:11 -02001352 ui_progress__update(file_pos, file_size,
1353 "Processing events...");
Thomas Gleixner55b446292010-11-30 17:49:46 +00001354 }
1355
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001356 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001357 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001358
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001359 err = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001360 /* do the final flush for ordered samples */
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001361 session->ordered_samples.next_flush = ULLONG_MAX;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001362 flush_sample_queue(session, tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001363out_err:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001364 perf_session__warn_about_errors(session, tool);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001365 perf_session_free_sample_buffers(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001366 return err;
1367}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001368
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001369int perf_session__process_events(struct perf_session *self,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001370 struct perf_tool *tool)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001371{
1372 int err;
1373
1374 if (perf_session__register_idle_thread(self) == NULL)
1375 return -ENOMEM;
1376
Tom Zanussi8dc58102010-04-01 23:59:15 -05001377 if (!self->fd_pipe)
1378 err = __perf_session__process_events(self,
1379 self->header.data_offset,
1380 self->header.data_size,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001381 self->size, tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001382 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001383 err = __perf_session__process_pipe_events(self, tool);
Dave Martin88ca8952010-07-27 11:46:12 -03001384
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001385 return err;
1386}
1387
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001388bool perf_session__has_traces(struct perf_session *self, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001389{
1390 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001391 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1392 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001393 }
1394
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001395 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001396}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001397
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001398int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1399 const char *symbol_name, u64 addr)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001400{
1401 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001402 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001403 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001404
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001405 ref = zalloc(sizeof(struct ref_reloc_sym));
1406 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001407 return -ENOMEM;
1408
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001409 ref->name = strdup(symbol_name);
1410 if (ref->name == NULL) {
1411 free(ref);
1412 return -ENOMEM;
1413 }
1414
1415 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001416 if (bracket)
1417 *bracket = '\0';
1418
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001419 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001420
1421 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001422 struct kmap *kmap = map__kmap(maps[i]);
1423 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001424 }
1425
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001426 return 0;
1427}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001428
1429size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
1430{
1431 return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
1432 __dsos__fprintf(&self->host_machine.user_dsos, fp) +
1433 machines__fprintf_dsos(&self->machines, fp);
1434}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001435
1436size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
1437 bool with_hits)
1438{
1439 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
1440 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
1441}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001442
1443size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1444{
1445 struct perf_evsel *pos;
1446 size_t ret = fprintf(fp, "Aggregated stats:\n");
1447
1448 ret += hists__fprintf_nr_events(&session->hists, fp);
1449
1450 list_for_each_entry(pos, &session->evlist->entries, node) {
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -03001451 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001452 ret += hists__fprintf_nr_events(&pos->hists, fp);
1453 }
1454
1455 return ret;
1456}
David Ahernc0230b22011-03-09 22:23:27 -07001457
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001458size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1459{
1460 /*
1461 * FIXME: Here we have to actually print all the machines in this
1462 * session, not just the host...
1463 */
1464 return machine__fprintf(&session->host_machine, fp);
1465}
1466
1467void perf_session__remove_thread(struct perf_session *session,
1468 struct thread *th)
1469{
1470 /*
1471 * FIXME: This one makes no sense, we need to remove the thread from
1472 * the machine it belongs to, perf_session can have many machines, so
1473 * doing it always on ->host_machine is wrong. Fix when auditing all
1474 * the 'perf kvm' code.
1475 */
1476 machine__remove_thread(&session->host_machine, th);
1477}
1478
David Ahern9cbdb702011-04-06 21:54:20 -06001479struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1480 unsigned int type)
1481{
1482 struct perf_evsel *pos;
1483
1484 list_for_each_entry(pos, &session->evlist->entries, node) {
1485 if (pos->attr.type == type)
1486 return pos;
1487 }
1488 return NULL;
1489}
1490
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001491void perf_event__print_ip(union perf_event *event, struct perf_sample *sample,
Jiri Olsaa9c34a92012-06-11 15:20:03 +02001492 struct machine *machine, int print_sym,
1493 int print_dso, int print_symoffset)
David Ahernc0230b22011-03-09 22:23:27 -07001494{
1495 struct addr_location al;
David Ahernc0230b22011-03-09 22:23:27 -07001496 struct callchain_cursor_node *node;
1497
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001498 if (perf_event__preprocess_sample(event, machine, &al, sample,
David Ahernc0230b22011-03-09 22:23:27 -07001499 NULL) < 0) {
1500 error("problem processing %d event, skipping it.\n",
1501 event->header.type);
1502 return;
1503 }
1504
1505 if (symbol_conf.use_callchain && sample->callchain) {
1506
Jiri Olsaa9c34a92012-06-11 15:20:03 +02001507 if (machine__resolve_callchain(machine, al.thread,
David Ahernc0230b22011-03-09 22:23:27 -07001508 sample->callchain, NULL) != 0) {
1509 if (verbose)
1510 error("Failed to resolve callchain. Skipping\n");
1511 return;
1512 }
Namhyung Kim47260642012-05-31 14:43:26 +09001513 callchain_cursor_commit(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001514
1515 while (1) {
Namhyung Kim47260642012-05-31 14:43:26 +09001516 node = callchain_cursor_current(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001517 if (!node)
1518 break;
1519
David Ahern787bef12011-05-27 14:28:43 -06001520 printf("\t%16" PRIx64, node->ip);
1521 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001522 printf(" ");
1523 symbol__fprintf_symname(node->sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001524 }
1525 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001526 printf(" (");
David Ahern52deff72012-05-29 22:58:26 -06001527 map__fprintf_dsoname(node->map, stdout);
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001528 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001529 }
1530 printf("\n");
David Ahernc0230b22011-03-09 22:23:27 -07001531
Namhyung Kim47260642012-05-31 14:43:26 +09001532 callchain_cursor_advance(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001533 }
1534
1535 } else {
David Ahernadc4bf92011-05-30 09:16:27 -06001536 printf("%16" PRIx64, sample->ip);
David Ahern787bef12011-05-27 14:28:43 -06001537 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001538 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001539 if (print_symoffset)
1540 symbol__fprintf_symname_offs(al.sym, &al,
1541 stdout);
1542 else
1543 symbol__fprintf_symname(al.sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001544 }
1545
1546 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001547 printf(" (");
1548 map__fprintf_dsoname(al.map, stdout);
1549 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001550 }
David Ahernc0230b22011-03-09 22:23:27 -07001551 }
1552}
Anton Blanchard5d67be92011-07-04 21:57:50 +10001553
1554int perf_session__cpu_bitmap(struct perf_session *session,
1555 const char *cpu_list, unsigned long *cpu_bitmap)
1556{
1557 int i;
1558 struct cpu_map *map;
1559
1560 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1561 struct perf_evsel *evsel;
1562
1563 evsel = perf_session__find_first_evtype(session, i);
1564 if (!evsel)
1565 continue;
1566
1567 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1568 pr_err("File does not contain CPU events. "
1569 "Remove -c option to proceed.\n");
1570 return -1;
1571 }
1572 }
1573
1574 map = cpu_map__new(cpu_list);
David Ahern47fbe532011-11-13 10:45:27 -07001575 if (map == NULL) {
1576 pr_err("Invalid cpu_list\n");
1577 return -1;
1578 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001579
1580 for (i = 0; i < map->nr; i++) {
1581 int cpu = map->map[i];
1582
1583 if (cpu >= MAX_NR_CPUS) {
1584 pr_err("Requested CPU %d too large. "
1585 "Consider raising MAX_NR_CPUS\n", cpu);
1586 return -1;
1587 }
1588
1589 set_bit(cpu, cpu_bitmap);
1590 }
1591
1592 return 0;
1593}
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001594
1595void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1596 bool full)
1597{
1598 struct stat st;
1599 int ret;
1600
1601 if (session == NULL || fp == NULL)
1602 return;
1603
1604 ret = fstat(session->fd, &st);
1605 if (ret == -1)
1606 return;
1607
1608 fprintf(fp, "# ========\n");
1609 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1610 perf_header__fprintf_info(session, fp, full);
1611 fprintf(fp, "# ========\n#\n");
1612}