blob: 002ebbf59f48e84aa69898de786b6227e2402ad7 [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);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200143
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200144 if (mode == O_RDONLY) {
145 if (perf_session__open(self, force) < 0)
146 goto out_delete;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300147 perf_session__update_sample_type(self);
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200148 } else if (mode == O_WRONLY) {
149 /*
150 * In O_RDONLY mode this will be performed when reading the
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200151 * kernel MMAP event, in perf_event__process_mmap().
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200152 */
153 if (perf_session__create_kernel_maps(self) < 0)
154 goto out_delete;
155 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200156
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200157 if (tool && tool->ordering_requires_timestamps &&
158 tool->ordered_samples && !self->sample_id_all) {
Ian Munsie21ef97f2010-12-10 14:09:16 +1100159 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200160 tool->ordered_samples = false;
Ian Munsie21ef97f2010-12-10 14:09:16 +1100161 }
162
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200163out:
164 return self;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200165out_delete:
166 perf_session__delete(self);
167 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200168}
169
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200170static void machine__delete_dead_threads(struct machine *machine)
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300171{
172 struct thread *n, *t;
173
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200174 list_for_each_entry_safe(t, n, &machine->dead_threads, node) {
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300175 list_del(&t->node);
176 thread__delete(t);
177 }
178}
179
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200180static void perf_session__delete_dead_threads(struct perf_session *session)
181{
182 machine__delete_dead_threads(&session->host_machine);
183}
184
185static void machine__delete_threads(struct machine *self)
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300186{
187 struct rb_node *nd = rb_first(&self->threads);
188
189 while (nd) {
190 struct thread *t = rb_entry(nd, struct thread, rb_node);
191
192 rb_erase(&t->rb_node, &self->threads);
193 nd = rb_next(nd);
194 thread__delete(t);
195 }
196}
197
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200198static void perf_session__delete_threads(struct perf_session *session)
199{
200 machine__delete_threads(&session->host_machine);
201}
202
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200203void perf_session__delete(struct perf_session *self)
204{
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300205 perf_session__destroy_kernel_maps(self);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300206 perf_session__delete_dead_threads(self);
207 perf_session__delete_threads(self);
208 machine__exit(&self->host_machine);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200209 close(self->fd);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200210 free(self);
211}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200212
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200213void machine__remove_thread(struct machine *self, struct thread *th)
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300214{
Arnaldo Carvalho de Melo70597f22010-08-02 18:59:28 -0300215 self->last_match = NULL;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300216 rb_erase(&th->rb_node, &self->threads);
217 /*
218 * We may have references to this thread, for instance in some hist_entry
219 * instances, so just move them to a separate list.
220 */
221 list_add_tail(&th->node, &self->dead_threads);
222}
223
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200224static bool symbol__match_parent_regex(struct symbol *sym)
225{
226 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
227 return 1;
228
229 return 0;
230}
231
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100232static const u8 cpumodes[] = {
233 PERF_RECORD_MISC_USER,
234 PERF_RECORD_MISC_KERNEL,
235 PERF_RECORD_MISC_GUEST_USER,
236 PERF_RECORD_MISC_GUEST_KERNEL
237};
238#define NCPUMODES (sizeof(cpumodes)/sizeof(u8))
239
240static void ip__resolve_ams(struct machine *self, struct thread *thread,
241 struct addr_map_symbol *ams,
242 u64 ip)
243{
244 struct addr_location al;
245 size_t i;
246 u8 m;
247
248 memset(&al, 0, sizeof(al));
249
250 for (i = 0; i < NCPUMODES; i++) {
251 m = cpumodes[i];
252 /*
253 * We cannot use the header.misc hint to determine whether a
254 * branch stack address is user, kernel, guest, hypervisor.
255 * Branches may straddle the kernel/user/hypervisor boundaries.
256 * Thus, we have to try consecutively until we find a match
257 * or else, the symbol is unknown
258 */
259 thread__find_addr_location(thread, self, m, MAP__FUNCTION,
260 ip, &al, NULL);
261 if (al.sym)
262 goto found;
263 }
264found:
265 ams->addr = ip;
Stephane Eraniana68c2c52012-03-08 23:47:48 +0100266 ams->al_addr = al.addr;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100267 ams->sym = al.sym;
268 ams->map = al.map;
269}
270
271struct branch_info *machine__resolve_bstack(struct machine *self,
272 struct thread *thr,
273 struct branch_stack *bs)
274{
275 struct branch_info *bi;
276 unsigned int i;
277
278 bi = calloc(bs->nr, sizeof(struct branch_info));
279 if (!bi)
280 return NULL;
281
282 for (i = 0; i < bs->nr; i++) {
283 ip__resolve_ams(self, thr, &bi[i].to, bs->entries[i].to);
284 ip__resolve_ams(self, thr, &bi[i].from, bs->entries[i].from);
285 bi[i].flags = bs->entries[i].flags;
286 }
287 return bi;
288}
289
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200290int machine__resolve_callchain(struct machine *self, struct perf_evsel *evsel,
291 struct thread *thread,
292 struct ip_callchain *chain,
293 struct symbol **parent)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200294{
295 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200296 unsigned int i;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100297 int err;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200298
Arnaldo Carvalho de Melo246d4ce2011-11-11 23:10:26 -0200299 callchain_cursor_reset(&evsel->hists.callchain_cursor);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200300
301 for (i = 0; i < chain->nr; i++) {
Sam Liaod797fdc2011-06-07 23:49:46 +0800302 u64 ip;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200303 struct addr_location al;
304
Sam Liaod797fdc2011-06-07 23:49:46 +0800305 if (callchain_param.order == ORDER_CALLEE)
306 ip = chain->ips[i];
307 else
308 ip = chain->ips[chain->nr - i - 1];
309
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200310 if (ip >= PERF_CONTEXT_MAX) {
311 switch (ip) {
312 case PERF_CONTEXT_HV:
313 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
314 case PERF_CONTEXT_KERNEL:
315 cpumode = PERF_RECORD_MISC_KERNEL; break;
316 case PERF_CONTEXT_USER:
317 cpumode = PERF_RECORD_MISC_USER; break;
318 default:
319 break;
320 }
321 continue;
322 }
323
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800324 al.filtered = false;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200325 thread__find_addr_location(thread, self, cpumode,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200326 MAP__FUNCTION, ip, &al, NULL);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200327 if (al.sym != NULL) {
328 if (sort__has_parent && !*parent &&
329 symbol__match_parent_regex(al.sym))
330 *parent = al.sym;
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200331 if (!symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200332 break;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200333 }
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100334
Arnaldo Carvalho de Melo246d4ce2011-11-11 23:10:26 -0200335 err = callchain_cursor_append(&evsel->hists.callchain_cursor,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100336 ip, al.map, al.sym);
337 if (err)
338 return err;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200339 }
340
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100341 return 0;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200342}
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200343
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200344static int process_event_synth_tracing_data_stub(union perf_event *event __used,
345 struct perf_session *session __used)
346{
347 dump_printf(": unhandled!\n");
348 return 0;
349}
350
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200351static int process_event_synth_attr_stub(union perf_event *event __used,
352 struct perf_evlist **pevlist __used)
353{
354 dump_printf(": unhandled!\n");
355 return 0;
356}
357
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200358static int process_event_sample_stub(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200359 union perf_event *event __used,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300360 struct perf_sample *sample __used,
361 struct perf_evsel *evsel __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200362 struct machine *machine __used)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300363{
364 dump_printf(": unhandled!\n");
365 return 0;
366}
367
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200368static int process_event_stub(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200369 union perf_event *event __used,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200370 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200371 struct machine *machine __used)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200372{
373 dump_printf(": unhandled!\n");
374 return 0;
375}
376
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200377static int process_finished_round_stub(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200378 union perf_event *event __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200379 struct perf_session *perf_session __used)
380{
381 dump_printf(": unhandled!\n");
382 return 0;
383}
384
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200385static int process_event_type_stub(struct perf_tool *tool __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200386 union perf_event *event __used)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200387{
388 dump_printf(": unhandled!\n");
389 return 0;
390}
391
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200392static int process_finished_round(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200393 union perf_event *event,
394 struct perf_session *session);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200395
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200396static void perf_tool__fill_defaults(struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200397{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200398 if (tool->sample == NULL)
399 tool->sample = process_event_sample_stub;
400 if (tool->mmap == NULL)
401 tool->mmap = process_event_stub;
402 if (tool->comm == NULL)
403 tool->comm = process_event_stub;
404 if (tool->fork == NULL)
405 tool->fork = process_event_stub;
406 if (tool->exit == NULL)
407 tool->exit = process_event_stub;
408 if (tool->lost == NULL)
409 tool->lost = perf_event__process_lost;
410 if (tool->read == NULL)
411 tool->read = process_event_sample_stub;
412 if (tool->throttle == NULL)
413 tool->throttle = process_event_stub;
414 if (tool->unthrottle == NULL)
415 tool->unthrottle = process_event_stub;
416 if (tool->attr == NULL)
417 tool->attr = process_event_synth_attr_stub;
418 if (tool->event_type == NULL)
419 tool->event_type = process_event_type_stub;
420 if (tool->tracing_data == NULL)
421 tool->tracing_data = process_event_synth_tracing_data_stub;
422 if (tool->build_id == NULL)
423 tool->build_id = process_finished_round_stub;
424 if (tool->finished_round == NULL) {
425 if (tool->ordered_samples)
426 tool->finished_round = process_finished_round;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200427 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200428 tool->finished_round = process_finished_round_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200429 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200430}
431
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200432void mem_bswap_64(void *src, int byte_size)
433{
434 u64 *m = src;
435
436 while (byte_size > 0) {
437 *m = bswap_64(*m);
438 byte_size -= sizeof(u64);
439 ++m;
440 }
441}
442
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200443static void perf_event__all64_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200444{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200445 struct perf_event_header *hdr = &event->header;
446 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200447}
448
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200449static void perf_event__comm_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200450{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200451 event->comm.pid = bswap_32(event->comm.pid);
452 event->comm.tid = bswap_32(event->comm.tid);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200453}
454
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200455static void perf_event__mmap_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200456{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200457 event->mmap.pid = bswap_32(event->mmap.pid);
458 event->mmap.tid = bswap_32(event->mmap.tid);
459 event->mmap.start = bswap_64(event->mmap.start);
460 event->mmap.len = bswap_64(event->mmap.len);
461 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200462}
463
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200464static void perf_event__task_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200465{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200466 event->fork.pid = bswap_32(event->fork.pid);
467 event->fork.tid = bswap_32(event->fork.tid);
468 event->fork.ppid = bswap_32(event->fork.ppid);
469 event->fork.ptid = bswap_32(event->fork.ptid);
470 event->fork.time = bswap_64(event->fork.time);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200471}
472
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200473static void perf_event__read_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200474{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200475 event->read.pid = bswap_32(event->read.pid);
476 event->read.tid = bswap_32(event->read.tid);
477 event->read.value = bswap_64(event->read.value);
478 event->read.time_enabled = bswap_64(event->read.time_enabled);
479 event->read.time_running = bswap_64(event->read.time_running);
480 event->read.id = bswap_64(event->read.id);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200481}
482
David Aherneda39132011-07-15 12:34:09 -0600483/* exported for swapping attributes in file header */
484void perf_event__attr_swap(struct perf_event_attr *attr)
485{
486 attr->type = bswap_32(attr->type);
487 attr->size = bswap_32(attr->size);
488 attr->config = bswap_64(attr->config);
489 attr->sample_period = bswap_64(attr->sample_period);
490 attr->sample_type = bswap_64(attr->sample_type);
491 attr->read_format = bswap_64(attr->read_format);
492 attr->wakeup_events = bswap_32(attr->wakeup_events);
493 attr->bp_type = bswap_32(attr->bp_type);
494 attr->bp_addr = bswap_64(attr->bp_addr);
495 attr->bp_len = bswap_64(attr->bp_len);
496}
497
498static void perf_event__hdr_attr_swap(union perf_event *event)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500499{
500 size_t size;
501
David Aherneda39132011-07-15 12:34:09 -0600502 perf_event__attr_swap(&event->attr.attr);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500503
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200504 size = event->header.size;
505 size -= (void *)&event->attr.id - (void *)event;
506 mem_bswap_64(event->attr.id, size);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500507}
508
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200509static void perf_event__event_type_swap(union perf_event *event)
Tom Zanussicd19a032010-04-01 23:59:20 -0500510{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200511 event->event_type.event_type.event_id =
512 bswap_64(event->event_type.event_type.event_id);
Tom Zanussicd19a032010-04-01 23:59:20 -0500513}
514
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200515static void perf_event__tracing_data_swap(union perf_event *event)
Tom Zanussi92155452010-04-01 23:59:21 -0500516{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200517 event->tracing_data.size = bswap_32(event->tracing_data.size);
Tom Zanussi92155452010-04-01 23:59:21 -0500518}
519
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200520typedef void (*perf_event__swap_op)(union perf_event *event);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200521
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200522static perf_event__swap_op perf_event__swap_ops[] = {
523 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
524 [PERF_RECORD_COMM] = perf_event__comm_swap,
525 [PERF_RECORD_FORK] = perf_event__task_swap,
526 [PERF_RECORD_EXIT] = perf_event__task_swap,
527 [PERF_RECORD_LOST] = perf_event__all64_swap,
528 [PERF_RECORD_READ] = perf_event__read_swap,
529 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
David Aherneda39132011-07-15 12:34:09 -0600530 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200531 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
532 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
533 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
534 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200535};
536
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200537struct sample_queue {
538 u64 timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000539 u64 file_offset;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200540 union perf_event *event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200541 struct list_head list;
542};
543
Thomas Gleixner020bb752010-11-30 17:49:53 +0000544static void perf_session_free_sample_buffers(struct perf_session *session)
545{
546 struct ordered_samples *os = &session->ordered_samples;
547
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000548 while (!list_empty(&os->to_free)) {
Thomas Gleixner020bb752010-11-30 17:49:53 +0000549 struct sample_queue *sq;
550
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000551 sq = list_entry(os->to_free.next, struct sample_queue, list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000552 list_del(&sq->list);
553 free(sq);
554 }
555}
556
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100557static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200558 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200559 struct perf_sample *sample,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200560 struct perf_tool *tool,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000561 u64 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100562
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200563static void flush_sample_queue(struct perf_session *s,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200564 struct perf_tool *tool)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200565{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000566 struct ordered_samples *os = &s->ordered_samples;
567 struct list_head *head = &os->samples;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200568 struct sample_queue *tmp, *iter;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200569 struct perf_sample sample;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000570 u64 limit = os->next_flush;
571 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200572 unsigned idx = 0, progress_next = os->nr_samples / 16;
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200573 int ret;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200574
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200575 if (!tool->ordered_samples || !limit)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200576 return;
577
578 list_for_each_entry_safe(iter, tmp, head, list) {
579 if (iter->timestamp > limit)
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000580 break;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200581
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200582 ret = perf_session__parse_sample(s, iter->event, &sample);
583 if (ret)
584 pr_err("Can't parse sample, err = %d\n", ret);
585 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200586 perf_session_deliver_event(s, iter->event, &sample, tool,
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200587 iter->file_offset);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200588
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000589 os->last_flush = iter->timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200590 list_del(&iter->list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000591 list_add(&iter->list, &os->sample_cache);
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200592 if (++idx >= progress_next) {
593 progress_next += os->nr_samples / 16;
594 ui_progress__update(idx, os->nr_samples,
595 "Processing time ordered events...");
596 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200597 }
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000598
599 if (list_empty(head)) {
600 os->last_sample = NULL;
601 } else if (last_ts <= limit) {
602 os->last_sample =
603 list_entry(head->prev, struct sample_queue, list);
604 }
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200605
606 os->nr_samples = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200607}
608
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200609/*
610 * When perf record finishes a pass on every buffers, it records this pseudo
611 * event.
612 * We record the max timestamp t found in the pass n.
613 * Assuming these timestamps are monotonic across cpus, we know that if
614 * a buffer still has events with timestamps below t, they will be all
615 * available and then read in the pass n + 1.
616 * Hence when we start to read the pass n + 2, we can safely flush every
617 * events with timestamps below t.
618 *
619 * ============ PASS n =================
620 * CPU 0 | CPU 1
621 * |
622 * cnt1 timestamps | cnt2 timestamps
623 * 1 | 2
624 * 2 | 3
625 * - | 4 <--- max recorded
626 *
627 * ============ PASS n + 1 ==============
628 * CPU 0 | CPU 1
629 * |
630 * cnt1 timestamps | cnt2 timestamps
631 * 3 | 5
632 * 4 | 6
633 * 5 | 7 <---- max recorded
634 *
635 * Flush every events below timestamp 4
636 *
637 * ============ PASS n + 2 ==============
638 * CPU 0 | CPU 1
639 * |
640 * cnt1 timestamps | cnt2 timestamps
641 * 6 | 8
642 * 7 | 9
643 * - | 10
644 *
645 * Flush every events below timestamp 7
646 * etc...
647 */
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200648static int process_finished_round(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200649 union perf_event *event __used,
650 struct perf_session *session)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200651{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200652 flush_sample_queue(session, tool);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200653 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
654
655 return 0;
656}
657
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200658/* The queue is ordered by time */
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100659static void __queue_event(struct sample_queue *new, struct perf_session *s)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200660{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000661 struct ordered_samples *os = &s->ordered_samples;
662 struct sample_queue *sample = os->last_sample;
663 u64 timestamp = new->timestamp;
664 struct list_head *p;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200665
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200666 ++os->nr_samples;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000667 os->last_sample = new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200668
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000669 if (!sample) {
670 list_add(&new->list, &os->samples);
671 os->max_timestamp = timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200672 return;
673 }
674
675 /*
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000676 * last_sample might point to some random place in the list as it's
677 * the last queued event. We expect that the new event is close to
678 * this.
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200679 */
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000680 if (sample->timestamp <= timestamp) {
681 while (sample->timestamp <= timestamp) {
682 p = sample->list.next;
683 if (p == &os->samples) {
684 list_add_tail(&new->list, &os->samples);
685 os->max_timestamp = timestamp;
686 return;
687 }
688 sample = list_entry(p, struct sample_queue, list);
689 }
690 list_add_tail(&new->list, &sample->list);
691 } else {
692 while (sample->timestamp > timestamp) {
693 p = sample->list.prev;
694 if (p == &os->samples) {
695 list_add(&new->list, &os->samples);
696 return;
697 }
698 sample = list_entry(p, struct sample_queue, list);
699 }
700 list_add(&new->list, &sample->list);
701 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200702}
703
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000704#define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
705
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200706static int perf_session_queue_event(struct perf_session *s, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200707 struct perf_sample *sample, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200708{
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000709 struct ordered_samples *os = &s->ordered_samples;
710 struct list_head *sc = &os->sample_cache;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200711 u64 timestamp = sample->time;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200712 struct sample_queue *new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200713
Thomas Gleixner79a14c12010-12-07 12:48:44 +0000714 if (!timestamp || timestamp == ~0ULL)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100715 return -ETIME;
716
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200717 if (timestamp < s->ordered_samples.last_flush) {
718 printf("Warning: Timestamp below last timeslice flush\n");
719 return -EINVAL;
720 }
721
Thomas Gleixner020bb752010-11-30 17:49:53 +0000722 if (!list_empty(sc)) {
723 new = list_entry(sc->next, struct sample_queue, list);
724 list_del(&new->list);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000725 } else if (os->sample_buffer) {
726 new = os->sample_buffer + os->sample_buffer_idx;
727 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
728 os->sample_buffer = NULL;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000729 } else {
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000730 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
731 if (!os->sample_buffer)
Thomas Gleixner020bb752010-11-30 17:49:53 +0000732 return -ENOMEM;
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000733 list_add(&os->sample_buffer->list, &os->to_free);
734 os->sample_buffer_idx = 2;
735 new = os->sample_buffer + 1;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000736 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200737
738 new->timestamp = timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000739 new->file_offset = file_offset;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000740 new->event = event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200741
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100742 __queue_event(new, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200743
744 return 0;
745}
746
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200747static void callchain__printf(struct perf_sample *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200748{
749 unsigned int i;
750
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200751 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200752
753 for (i = 0; i < sample->callchain->nr; i++)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200754 printf("..... %2d: %016" PRIx64 "\n",
755 i, sample->callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200756}
757
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100758static void branch_stack__printf(struct perf_sample *sample)
759{
760 uint64_t i;
761
762 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
763
764 for (i = 0; i < sample->branch_stack->nr; i++)
765 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n",
766 i, sample->branch_stack->entries[i].from,
767 sample->branch_stack->entries[i].to);
768}
769
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200770static void perf_session__print_tstamp(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200771 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200772 struct perf_sample *sample)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200773{
774 if (event->header.type != PERF_RECORD_SAMPLE &&
775 !session->sample_id_all) {
776 fputs("-1 -1 ", stdout);
777 return;
778 }
779
780 if ((session->sample_type & PERF_SAMPLE_CPU))
781 printf("%u ", sample->cpu);
782
783 if (session->sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200784 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200785}
786
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200787static void dump_event(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200788 u64 file_offset, struct perf_sample *sample)
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000789{
790 if (!dump_trace)
791 return;
792
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200793 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
794 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000795
796 trace_event(event);
797
798 if (sample)
799 perf_session__print_tstamp(session, event, sample);
800
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200801 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200802 event->header.size, perf_event__name(event->header.type));
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000803}
804
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200805static void dump_sample(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200806 struct perf_sample *sample)
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000807{
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200808 if (!dump_trace)
809 return;
810
David Ahern7cec0922011-05-30 13:08:23 -0600811 printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200812 event->header.misc, sample->pid, sample->tid, sample->ip,
David Ahern7cec0922011-05-30 13:08:23 -0600813 sample->period, sample->addr);
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000814
815 if (session->sample_type & PERF_SAMPLE_CALLCHAIN)
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200816 callchain__printf(sample);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100817
818 if (session->sample_type & PERF_SAMPLE_BRANCH_STACK)
819 branch_stack__printf(sample);
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000820}
821
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200822static struct machine *
823 perf_session__find_machine_for_cpumode(struct perf_session *session,
824 union perf_event *event)
825{
826 const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
827
828 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest)
829 return perf_session__find_machine(session, event->ip.pid);
830
831 return perf_session__find_host_machine(session);
832}
833
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100834static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200835 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200836 struct perf_sample *sample,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200837 struct perf_tool *tool,
Thomas Gleixner532e7262010-12-07 12:48:55 +0000838 u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100839{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300840 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200841 struct machine *machine;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300842
Thomas Gleixner532e7262010-12-07 12:48:55 +0000843 dump_event(session, event, file_offset, sample);
844
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200845 evsel = perf_evlist__id2evsel(session->evlist, sample->id);
846 if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) {
847 /*
848 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
849 * because the tools right now may apply filters, discarding
850 * some of the samples. For consistency, in the future we
851 * should have something like nr_filtered_samples and remove
852 * the sample->period from total_sample_period, etc, KISS for
853 * now tho.
854 *
855 * Also testing against NULL allows us to handle files without
856 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
857 * future probably it'll be a good idea to restrict event
858 * processing via perf_session to files with both set.
859 */
860 hists__inc_nr_events(&evsel->hists, event->header.type);
861 }
862
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200863 machine = perf_session__find_machine_for_cpumode(session, event);
864
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100865 switch (event->header.type) {
866 case PERF_RECORD_SAMPLE:
Thomas Gleixner532e7262010-12-07 12:48:55 +0000867 dump_sample(session, event, sample);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300868 if (evsel == NULL) {
869 ++session->hists.stats.nr_unknown_id;
870 return -1;
871 }
Joerg Roedel0c095712012-02-10 18:05:04 +0100872 if (machine == NULL) {
873 ++session->hists.stats.nr_unprocessable_samples;
874 return -1;
875 }
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200876 return tool->sample(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100877 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200878 return tool->mmap(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100879 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200880 return tool->comm(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100881 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200882 return tool->fork(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100883 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200884 return tool->exit(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100885 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200886 if (tool->lost == perf_event__process_lost)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200887 session->hists.stats.total_lost += event->lost.lost;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200888 return tool->lost(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100889 case PERF_RECORD_READ:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200890 return tool->read(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100891 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200892 return tool->throttle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100893 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200894 return tool->unthrottle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100895 default:
896 ++session->hists.stats.nr_unknown_events;
897 return -1;
898 }
899}
900
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000901static int perf_session__preprocess_sample(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200902 union perf_event *event, struct perf_sample *sample)
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000903{
904 if (event->header.type != PERF_RECORD_SAMPLE ||
905 !(session->sample_type & PERF_SAMPLE_CALLCHAIN))
906 return 0;
907
908 if (!ip_callchain__valid(sample->callchain, event)) {
909 pr_debug("call-chain problem with event, skipping it.\n");
910 ++session->hists.stats.nr_invalid_chains;
911 session->hists.stats.total_invalid_chains += sample->period;
912 return -EINVAL;
913 }
914 return 0;
915}
916
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200917static int perf_session__process_user_event(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200918 struct perf_tool *tool, u64 file_offset)
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000919{
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200920 int err;
921
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000922 dump_event(session, event, file_offset, NULL);
923
924 /* These events are processed right away */
925 switch (event->header.type) {
926 case PERF_RECORD_HEADER_ATTR:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200927 err = tool->attr(event, &session->evlist);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200928 if (err == 0)
929 perf_session__update_sample_type(session);
930 return err;
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000931 case PERF_RECORD_HEADER_EVENT_TYPE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200932 return tool->event_type(tool, event);
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000933 case PERF_RECORD_HEADER_TRACING_DATA:
934 /* setup for reading amidst mmap */
935 lseek(session->fd, file_offset, SEEK_SET);
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200936 return tool->tracing_data(event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000937 case PERF_RECORD_HEADER_BUILD_ID:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200938 return tool->build_id(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000939 case PERF_RECORD_FINISHED_ROUND:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200940 return tool->finished_round(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000941 default:
942 return -EINVAL;
943 }
944}
945
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100946static int perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200947 union perf_event *event,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200948 struct perf_tool *tool,
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000949 u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200950{
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200951 struct perf_sample sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100952 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200953
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200954 if (session->header.needs_swap &&
955 perf_event__swap_ops[event->header.type])
956 perf_event__swap_ops[event->header.type](event);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200957
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000958 if (event->header.type >= PERF_RECORD_HEADER_MAX)
959 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200960
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000961 hists__inc_nr_events(&session->hists, event->header.type);
962
963 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200964 return perf_session__process_user_event(session, event, tool, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100965
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000966 /*
967 * For all kernel events we get the sample data
968 */
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200969 ret = perf_session__parse_sample(session, event, &sample);
970 if (ret)
971 return ret;
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000972
973 /* Preprocess sample records - precheck callchains */
974 if (perf_session__preprocess_sample(session, event, &sample))
975 return 0;
976
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200977 if (tool->ordered_samples) {
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000978 ret = perf_session_queue_event(session, event, &sample,
979 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100980 if (ret != -ETIME)
981 return ret;
982 }
983
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200984 return perf_session_deliver_event(session, event, &sample, tool,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000985 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200986}
987
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200988void perf_event_header__bswap(struct perf_event_header *self)
989{
990 self->type = bswap_32(self->type);
991 self->misc = bswap_16(self->misc);
992 self->size = bswap_16(self->size);
993}
994
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200995struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
996{
997 return machine__findnew_thread(&session->host_machine, pid);
998}
999
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001000static struct thread *perf_session__register_idle_thread(struct perf_session *self)
1001{
1002 struct thread *thread = perf_session__findnew(self, 0);
1003
1004 if (thread == NULL || thread__set_comm(thread, "swapper")) {
1005 pr_err("problem inserting idle task.\n");
1006 thread = NULL;
1007 }
1008
1009 return thread;
1010}
1011
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001012static void perf_session__warn_about_errors(const struct perf_session *session,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001013 const struct perf_tool *tool)
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001014{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001015 if (tool->lost == perf_event__process_lost &&
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001016 session->hists.stats.nr_events[PERF_RECORD_LOST] != 0) {
1017 ui__warning("Processed %d events and lost %d chunks!\n\n"
1018 "Check IO/CPU overload!\n\n",
1019 session->hists.stats.nr_events[0],
1020 session->hists.stats.nr_events[PERF_RECORD_LOST]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001021 }
1022
1023 if (session->hists.stats.nr_unknown_events != 0) {
1024 ui__warning("Found %u unknown events!\n\n"
1025 "Is this an older tool processing a perf.data "
1026 "file generated by a more recent tool?\n\n"
1027 "If that is not the case, consider "
1028 "reporting to linux-kernel@vger.kernel.org.\n\n",
1029 session->hists.stats.nr_unknown_events);
1030 }
1031
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001032 if (session->hists.stats.nr_unknown_id != 0) {
1033 ui__warning("%u samples with id not present in the header\n",
1034 session->hists.stats.nr_unknown_id);
1035 }
1036
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001037 if (session->hists.stats.nr_invalid_chains != 0) {
1038 ui__warning("Found invalid callchains!\n\n"
1039 "%u out of %u events were discarded for this reason.\n\n"
1040 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1041 session->hists.stats.nr_invalid_chains,
1042 session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
1043 }
Joerg Roedel0c095712012-02-10 18:05:04 +01001044
1045 if (session->hists.stats.nr_unprocessable_samples != 0) {
1046 ui__warning("%u unprocessable samples recorded.\n"
1047 "Do you have a KVM guest running and not using 'perf kvm'?\n",
1048 session->hists.stats.nr_unprocessable_samples);
1049 }
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001050}
1051
Tom Zanussi8dc58102010-04-01 23:59:15 -05001052#define session_done() (*(volatile int *)(&session_done))
1053volatile int session_done;
1054
1055static int __perf_session__process_pipe_events(struct perf_session *self,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001056 struct perf_tool *tool)
Tom Zanussi8dc58102010-04-01 23:59:15 -05001057{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001058 union perf_event event;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001059 uint32_t size;
1060 int skip = 0;
1061 u64 head;
1062 int err;
1063 void *p;
1064
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001065 perf_tool__fill_defaults(tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001066
1067 head = 0;
1068more:
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02001069 err = readn(self->fd, &event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05001070 if (err <= 0) {
1071 if (err == 0)
1072 goto done;
1073
1074 pr_err("failed to read event header\n");
1075 goto out_err;
1076 }
1077
1078 if (self->header.needs_swap)
1079 perf_event_header__bswap(&event.header);
1080
1081 size = event.header.size;
1082 if (size == 0)
1083 size = 8;
1084
1085 p = &event;
1086 p += sizeof(struct perf_event_header);
1087
Tom Zanussi794e43b2010-05-05 00:27:40 -05001088 if (size - sizeof(struct perf_event_header)) {
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02001089 err = readn(self->fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -05001090 if (err <= 0) {
1091 if (err == 0) {
1092 pr_err("unexpected end of event stream\n");
1093 goto done;
1094 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001095
Tom Zanussi794e43b2010-05-05 00:27:40 -05001096 pr_err("failed to read event data\n");
1097 goto out_err;
1098 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001099 }
1100
Namhyung Kim29c98622011-12-28 00:35:48 +09001101 if ((skip = perf_session__process_event(self, &event, tool, head)) < 0) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001102 dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
Tom Zanussi8dc58102010-04-01 23:59:15 -05001103 head, event.header.size, event.header.type);
1104 /*
1105 * assume we lost track of the stream, check alignment, and
1106 * increment a single u64 in the hope to catch on again 'soon'.
1107 */
1108 if (unlikely(head & 7))
1109 head &= ~7ULL;
1110
1111 size = 8;
1112 }
1113
1114 head += size;
1115
Tom Zanussi8dc58102010-04-01 23:59:15 -05001116 if (skip > 0)
1117 head += skip;
1118
1119 if (!session_done())
1120 goto more;
1121done:
1122 err = 0;
1123out_err:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001124 perf_session__warn_about_errors(self, tool);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001125 perf_session_free_sample_buffers(self);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001126 return err;
1127}
1128
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001129static union perf_event *
1130fetch_mmaped_event(struct perf_session *session,
1131 u64 head, size_t mmap_size, char *buf)
1132{
1133 union perf_event *event;
1134
1135 /*
1136 * Ensure we have enough space remaining to read
1137 * the size of the event in the headers.
1138 */
1139 if (head + sizeof(event->header) > mmap_size)
1140 return NULL;
1141
1142 event = (union perf_event *)(buf + head);
1143
1144 if (session->header.needs_swap)
1145 perf_event_header__bswap(&event->header);
1146
1147 if (head + event->header.size > mmap_size)
1148 return NULL;
1149
1150 return event;
1151}
1152
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001153int __perf_session__process_events(struct perf_session *session,
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001154 u64 data_offset, u64 data_size,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001155 u64 file_size, struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001156{
Thomas Gleixner55b44622010-11-30 17:49:46 +00001157 u64 head, page_offset, file_offset, file_pos, progress_next;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001158 int err, mmap_prot, mmap_flags, map_idx = 0;
Thomas Gleixner55b44622010-11-30 17:49:46 +00001159 size_t page_size, mmap_size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001160 char *buf, *mmaps[8];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001161 union perf_event *event;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001162 uint32_t size;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001163
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001164 perf_tool__fill_defaults(tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001165
Arnaldo Carvalho de Melo1b759622010-01-14 18:30:04 -02001166 page_size = sysconf(_SC_PAGESIZE);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001167
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001168 page_offset = page_size * (data_offset / page_size);
1169 file_offset = page_offset;
1170 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001171
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001172 if (data_offset + data_size < file_size)
1173 file_size = data_offset + data_size;
1174
Thomas Gleixner55b44622010-11-30 17:49:46 +00001175 progress_next = file_size / 16;
Thomas Gleixner55b44622010-11-30 17:49:46 +00001176
1177 mmap_size = session->mmap_window;
1178 if (mmap_size > file_size)
1179 mmap_size = file_size;
1180
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001181 memset(mmaps, 0, sizeof(mmaps));
1182
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001183 mmap_prot = PROT_READ;
1184 mmap_flags = MAP_SHARED;
1185
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001186 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001187 mmap_prot |= PROT_WRITE;
1188 mmap_flags = MAP_PRIVATE;
1189 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001190remap:
Thomas Gleixner55b44622010-11-30 17:49:46 +00001191 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
1192 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001193 if (buf == MAP_FAILED) {
1194 pr_err("failed to mmap file\n");
1195 err = -errno;
1196 goto out_err;
1197 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001198 mmaps[map_idx] = buf;
1199 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001200 file_pos = file_offset + head;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001201
1202more:
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001203 event = fetch_mmaped_event(session, head, mmap_size, buf);
1204 if (!event) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001205 if (mmaps[map_idx]) {
1206 munmap(mmaps[map_idx], mmap_size);
1207 mmaps[map_idx] = NULL;
1208 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001209
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001210 page_offset = page_size * (head / page_size);
1211 file_offset += page_offset;
1212 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001213 goto remap;
1214 }
1215
1216 size = event->header.size;
1217
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001218 if (size == 0 ||
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001219 perf_session__process_event(session, event, tool, file_pos) < 0) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001220 dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001221 file_offset + head, event->header.size,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001222 event->header.type);
1223 /*
1224 * assume we lost track of the stream, check alignment, and
1225 * increment a single u64 in the hope to catch on again 'soon'.
1226 */
1227 if (unlikely(head & 7))
1228 head &= ~7ULL;
1229
1230 size = 8;
1231 }
1232
1233 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001234 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001235
Thomas Gleixner55b44622010-11-30 17:49:46 +00001236 if (file_pos >= progress_next) {
1237 progress_next += file_size / 16;
Arnaldo Carvalho de Meloca59bcb2011-10-25 13:29:11 -02001238 ui_progress__update(file_pos, file_size,
1239 "Processing events...");
Thomas Gleixner55b44622010-11-30 17:49:46 +00001240 }
1241
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001242 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001243 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001244
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001245 err = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001246 /* do the final flush for ordered samples */
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001247 session->ordered_samples.next_flush = ULLONG_MAX;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001248 flush_sample_queue(session, tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001249out_err:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001250 perf_session__warn_about_errors(session, tool);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001251 perf_session_free_sample_buffers(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001252 return err;
1253}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001254
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001255int perf_session__process_events(struct perf_session *self,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001256 struct perf_tool *tool)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001257{
1258 int err;
1259
1260 if (perf_session__register_idle_thread(self) == NULL)
1261 return -ENOMEM;
1262
Tom Zanussi8dc58102010-04-01 23:59:15 -05001263 if (!self->fd_pipe)
1264 err = __perf_session__process_events(self,
1265 self->header.data_offset,
1266 self->header.data_size,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001267 self->size, tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001268 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001269 err = __perf_session__process_pipe_events(self, tool);
Dave Martin88ca8952010-07-27 11:46:12 -03001270
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001271 return err;
1272}
1273
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001274bool perf_session__has_traces(struct perf_session *self, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001275{
1276 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001277 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1278 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001279 }
1280
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001281 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001282}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001283
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001284int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1285 const char *symbol_name, u64 addr)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001286{
1287 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001288 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001289 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001290
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001291 ref = zalloc(sizeof(struct ref_reloc_sym));
1292 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001293 return -ENOMEM;
1294
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001295 ref->name = strdup(symbol_name);
1296 if (ref->name == NULL) {
1297 free(ref);
1298 return -ENOMEM;
1299 }
1300
1301 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001302 if (bracket)
1303 *bracket = '\0';
1304
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001305 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001306
1307 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001308 struct kmap *kmap = map__kmap(maps[i]);
1309 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001310 }
1311
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001312 return 0;
1313}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001314
1315size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
1316{
1317 return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
1318 __dsos__fprintf(&self->host_machine.user_dsos, fp) +
1319 machines__fprintf_dsos(&self->machines, fp);
1320}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001321
1322size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
1323 bool with_hits)
1324{
1325 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
1326 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
1327}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001328
1329size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1330{
1331 struct perf_evsel *pos;
1332 size_t ret = fprintf(fp, "Aggregated stats:\n");
1333
1334 ret += hists__fprintf_nr_events(&session->hists, fp);
1335
1336 list_for_each_entry(pos, &session->evlist->entries, node) {
1337 ret += fprintf(fp, "%s stats:\n", event_name(pos));
1338 ret += hists__fprintf_nr_events(&pos->hists, fp);
1339 }
1340
1341 return ret;
1342}
David Ahernc0230b22011-03-09 22:23:27 -07001343
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001344size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1345{
1346 /*
1347 * FIXME: Here we have to actually print all the machines in this
1348 * session, not just the host...
1349 */
1350 return machine__fprintf(&session->host_machine, fp);
1351}
1352
1353void perf_session__remove_thread(struct perf_session *session,
1354 struct thread *th)
1355{
1356 /*
1357 * FIXME: This one makes no sense, we need to remove the thread from
1358 * the machine it belongs to, perf_session can have many machines, so
1359 * doing it always on ->host_machine is wrong. Fix when auditing all
1360 * the 'perf kvm' code.
1361 */
1362 machine__remove_thread(&session->host_machine, th);
1363}
1364
David Ahern9cbdb702011-04-06 21:54:20 -06001365struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1366 unsigned int type)
1367{
1368 struct perf_evsel *pos;
1369
1370 list_for_each_entry(pos, &session->evlist->entries, node) {
1371 if (pos->attr.type == type)
1372 return pos;
1373 }
1374 return NULL;
1375}
1376
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001377void perf_event__print_ip(union perf_event *event, struct perf_sample *sample,
1378 struct machine *machine, struct perf_evsel *evsel,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001379 int print_sym, int print_dso, int print_symoffset)
David Ahernc0230b22011-03-09 22:23:27 -07001380{
1381 struct addr_location al;
Arnaldo Carvalho de Melo246d4ce2011-11-11 23:10:26 -02001382 struct callchain_cursor *cursor = &evsel->hists.callchain_cursor;
David Ahernc0230b22011-03-09 22:23:27 -07001383 struct callchain_cursor_node *node;
1384
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001385 if (perf_event__preprocess_sample(event, machine, &al, sample,
David Ahernc0230b22011-03-09 22:23:27 -07001386 NULL) < 0) {
1387 error("problem processing %d event, skipping it.\n",
1388 event->header.type);
1389 return;
1390 }
1391
1392 if (symbol_conf.use_callchain && sample->callchain) {
1393
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001394 if (machine__resolve_callchain(machine, evsel, al.thread,
David Ahernc0230b22011-03-09 22:23:27 -07001395 sample->callchain, NULL) != 0) {
1396 if (verbose)
1397 error("Failed to resolve callchain. Skipping\n");
1398 return;
1399 }
1400 callchain_cursor_commit(cursor);
1401
1402 while (1) {
1403 node = callchain_cursor_current(cursor);
1404 if (!node)
1405 break;
1406
David Ahern787bef12011-05-27 14:28:43 -06001407 printf("\t%16" PRIx64, node->ip);
1408 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001409 printf(" ");
1410 symbol__fprintf_symname(node->sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001411 }
1412 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001413 printf(" (");
1414 map__fprintf_dsoname(al.map, stdout);
1415 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001416 }
1417 printf("\n");
David Ahernc0230b22011-03-09 22:23:27 -07001418
1419 callchain_cursor_advance(cursor);
1420 }
1421
1422 } else {
David Ahernadc4bf92011-05-30 09:16:27 -06001423 printf("%16" PRIx64, sample->ip);
David Ahern787bef12011-05-27 14:28:43 -06001424 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001425 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001426 if (print_symoffset)
1427 symbol__fprintf_symname_offs(al.sym, &al,
1428 stdout);
1429 else
1430 symbol__fprintf_symname(al.sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001431 }
1432
1433 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001434 printf(" (");
1435 map__fprintf_dsoname(al.map, stdout);
1436 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001437 }
David Ahernc0230b22011-03-09 22:23:27 -07001438 }
1439}
Anton Blanchard5d67be92011-07-04 21:57:50 +10001440
1441int perf_session__cpu_bitmap(struct perf_session *session,
1442 const char *cpu_list, unsigned long *cpu_bitmap)
1443{
1444 int i;
1445 struct cpu_map *map;
1446
1447 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1448 struct perf_evsel *evsel;
1449
1450 evsel = perf_session__find_first_evtype(session, i);
1451 if (!evsel)
1452 continue;
1453
1454 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1455 pr_err("File does not contain CPU events. "
1456 "Remove -c option to proceed.\n");
1457 return -1;
1458 }
1459 }
1460
1461 map = cpu_map__new(cpu_list);
David Ahern47fbe532011-11-13 10:45:27 -07001462 if (map == NULL) {
1463 pr_err("Invalid cpu_list\n");
1464 return -1;
1465 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001466
1467 for (i = 0; i < map->nr; i++) {
1468 int cpu = map->map[i];
1469
1470 if (cpu >= MAX_NR_CPUS) {
1471 pr_err("Requested CPU %d too large. "
1472 "Consider raising MAX_NR_CPUS\n", cpu);
1473 return -1;
1474 }
1475
1476 set_bit(cpu, cpu_bitmap);
1477 }
1478
1479 return 0;
1480}
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001481
1482void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1483 bool full)
1484{
1485 struct stat st;
1486 int ret;
1487
1488 if (session == NULL || fp == NULL)
1489 return;
1490
1491 ret = fstat(session->fd, &st);
1492 if (ret == -1)
1493 return;
1494
1495 fprintf(fp, "# ========\n");
1496 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1497 perf_header__fprintf_info(session, fp, full);
1498 fprintf(fp, "# ========\n#\n");
1499}