blob: 9412e3b05f6888c9504c7ab6b3dd0effe73de627 [file] [log] [blame]
Xiao Guangrongb8f46c52010-02-03 11:53:14 +08001#define _FILE_OFFSET_BITS 64
2
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02003#include <linux/kernel.h>
4
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02005#include <byteswap.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02006#include <unistd.h>
7#include <sys/types.h>
Arnaldo Carvalho de Meloa41794c2010-05-18 18:29:23 -03008#include <sys/mman.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02009
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -030010#include "evlist.h"
11#include "evsel.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020012#include "session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020013#include "tool.h"
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -020014#include "sort.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020015#include "util.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100016#include "cpumap.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020017
18static int perf_session__open(struct perf_session *self, bool force)
19{
20 struct stat input_stat;
21
Tom Zanussi8dc58102010-04-01 23:59:15 -050022 if (!strcmp(self->filename, "-")) {
23 self->fd_pipe = true;
24 self->fd = STDIN_FILENO;
25
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030026 if (perf_session__read_header(self, self->fd) < 0)
Stephane Eranian69996df2012-02-09 23:21:06 +010027 pr_err("incompatible file format (rerun with -v to learn more)");
Tom Zanussi8dc58102010-04-01 23:59:15 -050028
29 return 0;
30 }
31
Xiao Guangrongf887f302010-02-04 16:46:42 +080032 self->fd = open(self->filename, O_RDONLY);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020033 if (self->fd < 0) {
Andy Isaacson0f2c3de2010-06-11 20:36:15 -070034 int err = errno;
35
36 pr_err("failed to open %s: %s", self->filename, strerror(err));
37 if (err == ENOENT && !strcmp(self->filename, "perf.data"))
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020038 pr_err(" (try 'perf record' first)");
39 pr_err("\n");
40 return -errno;
41 }
42
43 if (fstat(self->fd, &input_stat) < 0)
44 goto out_close;
45
46 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
47 pr_err("file %s not owned by current user or root\n",
48 self->filename);
49 goto out_close;
50 }
51
52 if (!input_stat.st_size) {
53 pr_info("zero-sized file (%s), nothing to do!\n",
54 self->filename);
55 goto out_close;
56 }
57
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030058 if (perf_session__read_header(self, self->fd) < 0) {
Stephane Eranian69996df2012-02-09 23:21:06 +010059 pr_err("incompatible file format (rerun with -v to learn more)");
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020060 goto out_close;
61 }
62
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030063 if (!perf_evlist__valid_sample_type(self->evlist)) {
64 pr_err("non matching sample_type");
65 goto out_close;
66 }
67
68 if (!perf_evlist__valid_sample_id_all(self->evlist)) {
69 pr_err("non matching sample_id_all");
70 goto out_close;
71 }
72
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020073 self->size = input_stat.st_size;
74 return 0;
75
76out_close:
77 close(self->fd);
78 self->fd = -1;
79 return -1;
80}
81
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020082void perf_session__update_sample_type(struct perf_session *self)
83{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030084 self->sample_type = perf_evlist__sample_type(self->evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030085 self->sample_size = __perf_evsel__sample_size(self->sample_type);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030086 self->sample_id_all = perf_evlist__sample_id_all(self->evlist);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -020087 self->id_hdr_size = perf_evlist__id_hdr_size(self->evlist);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020088 self->host_machine.id_hdr_size = self->id_hdr_size;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020089}
90
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080091int perf_session__create_kernel_maps(struct perf_session *self)
92{
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -030093 int ret = machine__create_kernel_maps(&self->host_machine);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080094
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080095 if (ret >= 0)
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -030096 ret = machines__create_guest_kernel_maps(&self->machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080097 return ret;
98}
99
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300100static void perf_session__destroy_kernel_maps(struct perf_session *self)
101{
102 machine__destroy_kernel_maps(&self->host_machine);
103 machines__destroy_guest_kernel_maps(&self->machines);
104}
105
Ian Munsie21ef97f2010-12-10 14:09:16 +1100106struct perf_session *perf_session__new(const char *filename, int mode,
107 bool force, bool repipe,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200108 struct perf_tool *tool)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200109{
Robert Richterefad1412011-12-07 10:02:54 +0100110 struct perf_session *self;
111 struct stat st;
112 size_t len;
113
114 if (!filename || !strlen(filename)) {
115 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
116 filename = "-";
117 else
118 filename = "perf.data";
119 }
120
121 len = strlen(filename);
122 self = zalloc(sizeof(*self) + len);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200123
124 if (self == NULL)
125 goto out;
126
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200127 memcpy(self->filename, filename, len);
Thomas Gleixner55b44622010-11-30 17:49:46 +0000128 /*
129 * On 64bit we can mmap the data file in one go. No need for tiny mmap
130 * slices. On 32bit we use 32MB.
131 */
132#if BITS_PER_LONG == 64
133 self->mmap_window = ULLONG_MAX;
134#else
135 self->mmap_window = 32 * 1024 * 1024ULL;
136#endif
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300137 self->machines = RB_ROOT;
Tom Zanussi454c4072010-05-01 01:41:20 -0500138 self->repipe = repipe;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000139 INIT_LIST_HEAD(&self->ordered_samples.samples);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000140 INIT_LIST_HEAD(&self->ordered_samples.sample_cache);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000141 INIT_LIST_HEAD(&self->ordered_samples.to_free);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300142 machine__init(&self->host_machine, "", HOST_KERNEL_ID);
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100143 hists__init(&self->hists);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200144
Arnaldo Carvalho de 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
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200291int machine__resolve_callchain(struct machine *self, struct perf_evsel *evsel,
292 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
Arnaldo Carvalho de Melo246d4ce2011-11-11 23:10:26 -0200300 callchain_cursor_reset(&evsel->hists.callchain_cursor);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200301
302 for (i = 0; i < chain->nr; i++) {
Sam Liaod797fdc2011-06-07 23:49:46 +0800303 u64 ip;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200304 struct addr_location al;
305
Sam Liaod797fdc2011-06-07 23:49:46 +0800306 if (callchain_param.order == ORDER_CALLEE)
307 ip = chain->ips[i];
308 else
309 ip = chain->ips[chain->nr - i - 1];
310
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200311 if (ip >= PERF_CONTEXT_MAX) {
312 switch (ip) {
313 case PERF_CONTEXT_HV:
314 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
315 case PERF_CONTEXT_KERNEL:
316 cpumode = PERF_RECORD_MISC_KERNEL; break;
317 case PERF_CONTEXT_USER:
318 cpumode = PERF_RECORD_MISC_USER; break;
319 default:
320 break;
321 }
322 continue;
323 }
324
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800325 al.filtered = false;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200326 thread__find_addr_location(thread, self, cpumode,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200327 MAP__FUNCTION, ip, &al, NULL);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200328 if (al.sym != NULL) {
329 if (sort__has_parent && !*parent &&
330 symbol__match_parent_regex(al.sym))
331 *parent = al.sym;
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200332 if (!symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200333 break;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200334 }
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100335
Arnaldo Carvalho de Melo246d4ce2011-11-11 23:10:26 -0200336 err = callchain_cursor_append(&evsel->hists.callchain_cursor,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100337 ip, al.map, al.sym);
338 if (err)
339 return err;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200340 }
341
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100342 return 0;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200343}
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200344
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200345static int process_event_synth_tracing_data_stub(union perf_event *event __used,
346 struct perf_session *session __used)
347{
348 dump_printf(": unhandled!\n");
349 return 0;
350}
351
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200352static int process_event_synth_attr_stub(union perf_event *event __used,
353 struct perf_evlist **pevlist __used)
354{
355 dump_printf(": unhandled!\n");
356 return 0;
357}
358
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200359static int process_event_sample_stub(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200360 union perf_event *event __used,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300361 struct perf_sample *sample __used,
362 struct perf_evsel *evsel __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200363 struct machine *machine __used)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300364{
365 dump_printf(": unhandled!\n");
366 return 0;
367}
368
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200369static int process_event_stub(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200370 union perf_event *event __used,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200371 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200372 struct machine *machine __used)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200373{
374 dump_printf(": unhandled!\n");
375 return 0;
376}
377
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200378static int process_finished_round_stub(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200379 union perf_event *event __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200380 struct perf_session *perf_session __used)
381{
382 dump_printf(": unhandled!\n");
383 return 0;
384}
385
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200386static int process_event_type_stub(struct perf_tool *tool __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200387 union perf_event *event __used)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200388{
389 dump_printf(": unhandled!\n");
390 return 0;
391}
392
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200393static int process_finished_round(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200394 union perf_event *event,
395 struct perf_session *session);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200396
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200397static void perf_tool__fill_defaults(struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200398{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200399 if (tool->sample == NULL)
400 tool->sample = process_event_sample_stub;
401 if (tool->mmap == NULL)
402 tool->mmap = process_event_stub;
403 if (tool->comm == NULL)
404 tool->comm = process_event_stub;
405 if (tool->fork == NULL)
406 tool->fork = process_event_stub;
407 if (tool->exit == NULL)
408 tool->exit = process_event_stub;
409 if (tool->lost == NULL)
410 tool->lost = perf_event__process_lost;
411 if (tool->read == NULL)
412 tool->read = process_event_sample_stub;
413 if (tool->throttle == NULL)
414 tool->throttle = process_event_stub;
415 if (tool->unthrottle == NULL)
416 tool->unthrottle = process_event_stub;
417 if (tool->attr == NULL)
418 tool->attr = process_event_synth_attr_stub;
419 if (tool->event_type == NULL)
420 tool->event_type = process_event_type_stub;
421 if (tool->tracing_data == NULL)
422 tool->tracing_data = process_event_synth_tracing_data_stub;
423 if (tool->build_id == NULL)
424 tool->build_id = process_finished_round_stub;
425 if (tool->finished_round == NULL) {
426 if (tool->ordered_samples)
427 tool->finished_round = process_finished_round;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200428 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200429 tool->finished_round = process_finished_round_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200430 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200431}
432
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200433void mem_bswap_64(void *src, int byte_size)
434{
435 u64 *m = src;
436
437 while (byte_size > 0) {
438 *m = bswap_64(*m);
439 byte_size -= sizeof(u64);
440 ++m;
441 }
442}
443
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200444static void perf_event__all64_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200445{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200446 struct perf_event_header *hdr = &event->header;
447 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200448}
449
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200450static void perf_event__comm_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200451{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200452 event->comm.pid = bswap_32(event->comm.pid);
453 event->comm.tid = bswap_32(event->comm.tid);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200454}
455
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200456static void perf_event__mmap_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200457{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200458 event->mmap.pid = bswap_32(event->mmap.pid);
459 event->mmap.tid = bswap_32(event->mmap.tid);
460 event->mmap.start = bswap_64(event->mmap.start);
461 event->mmap.len = bswap_64(event->mmap.len);
462 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200463}
464
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200465static void perf_event__task_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200466{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200467 event->fork.pid = bswap_32(event->fork.pid);
468 event->fork.tid = bswap_32(event->fork.tid);
469 event->fork.ppid = bswap_32(event->fork.ppid);
470 event->fork.ptid = bswap_32(event->fork.ptid);
471 event->fork.time = bswap_64(event->fork.time);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200472}
473
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200474static void perf_event__read_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200475{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200476 event->read.pid = bswap_32(event->read.pid);
477 event->read.tid = bswap_32(event->read.tid);
478 event->read.value = bswap_64(event->read.value);
479 event->read.time_enabled = bswap_64(event->read.time_enabled);
480 event->read.time_running = bswap_64(event->read.time_running);
481 event->read.id = bswap_64(event->read.id);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200482}
483
David Aherneda39132011-07-15 12:34:09 -0600484/* exported for swapping attributes in file header */
485void perf_event__attr_swap(struct perf_event_attr *attr)
486{
487 attr->type = bswap_32(attr->type);
488 attr->size = bswap_32(attr->size);
489 attr->config = bswap_64(attr->config);
490 attr->sample_period = bswap_64(attr->sample_period);
491 attr->sample_type = bswap_64(attr->sample_type);
492 attr->read_format = bswap_64(attr->read_format);
493 attr->wakeup_events = bswap_32(attr->wakeup_events);
494 attr->bp_type = bswap_32(attr->bp_type);
495 attr->bp_addr = bswap_64(attr->bp_addr);
496 attr->bp_len = bswap_64(attr->bp_len);
497}
498
499static void perf_event__hdr_attr_swap(union perf_event *event)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500500{
501 size_t size;
502
David Aherneda39132011-07-15 12:34:09 -0600503 perf_event__attr_swap(&event->attr.attr);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500504
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200505 size = event->header.size;
506 size -= (void *)&event->attr.id - (void *)event;
507 mem_bswap_64(event->attr.id, size);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500508}
509
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200510static void perf_event__event_type_swap(union perf_event *event)
Tom Zanussicd19a032010-04-01 23:59:20 -0500511{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200512 event->event_type.event_type.event_id =
513 bswap_64(event->event_type.event_type.event_id);
Tom Zanussicd19a032010-04-01 23:59:20 -0500514}
515
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200516static void perf_event__tracing_data_swap(union perf_event *event)
Tom Zanussi92155452010-04-01 23:59:21 -0500517{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200518 event->tracing_data.size = bswap_32(event->tracing_data.size);
Tom Zanussi92155452010-04-01 23:59:21 -0500519}
520
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200521typedef void (*perf_event__swap_op)(union perf_event *event);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200522
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200523static perf_event__swap_op perf_event__swap_ops[] = {
524 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
525 [PERF_RECORD_COMM] = perf_event__comm_swap,
526 [PERF_RECORD_FORK] = perf_event__task_swap,
527 [PERF_RECORD_EXIT] = perf_event__task_swap,
528 [PERF_RECORD_LOST] = perf_event__all64_swap,
529 [PERF_RECORD_READ] = perf_event__read_swap,
530 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
David Aherneda39132011-07-15 12:34:09 -0600531 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200532 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
533 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
534 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
535 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200536};
537
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200538struct sample_queue {
539 u64 timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000540 u64 file_offset;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200541 union perf_event *event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200542 struct list_head list;
543};
544
Thomas Gleixner020bb752010-11-30 17:49:53 +0000545static void perf_session_free_sample_buffers(struct perf_session *session)
546{
547 struct ordered_samples *os = &session->ordered_samples;
548
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000549 while (!list_empty(&os->to_free)) {
Thomas Gleixner020bb752010-11-30 17:49:53 +0000550 struct sample_queue *sq;
551
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000552 sq = list_entry(os->to_free.next, struct sample_queue, list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000553 list_del(&sq->list);
554 free(sq);
555 }
556}
557
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100558static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200559 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200560 struct perf_sample *sample,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200561 struct perf_tool *tool,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000562 u64 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100563
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200564static void flush_sample_queue(struct perf_session *s,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200565 struct perf_tool *tool)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200566{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000567 struct ordered_samples *os = &s->ordered_samples;
568 struct list_head *head = &os->samples;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200569 struct sample_queue *tmp, *iter;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200570 struct perf_sample sample;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000571 u64 limit = os->next_flush;
572 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200573 unsigned idx = 0, progress_next = os->nr_samples / 16;
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200574 int ret;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200575
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200576 if (!tool->ordered_samples || !limit)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200577 return;
578
579 list_for_each_entry_safe(iter, tmp, head, list) {
580 if (iter->timestamp > limit)
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000581 break;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200582
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200583 ret = perf_session__parse_sample(s, iter->event, &sample);
584 if (ret)
585 pr_err("Can't parse sample, err = %d\n", ret);
586 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200587 perf_session_deliver_event(s, iter->event, &sample, tool,
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200588 iter->file_offset);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200589
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000590 os->last_flush = iter->timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200591 list_del(&iter->list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000592 list_add(&iter->list, &os->sample_cache);
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200593 if (++idx >= progress_next) {
594 progress_next += os->nr_samples / 16;
595 ui_progress__update(idx, os->nr_samples,
596 "Processing time ordered events...");
597 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200598 }
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000599
600 if (list_empty(head)) {
601 os->last_sample = NULL;
602 } else if (last_ts <= limit) {
603 os->last_sample =
604 list_entry(head->prev, struct sample_queue, list);
605 }
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200606
607 os->nr_samples = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200608}
609
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200610/*
611 * When perf record finishes a pass on every buffers, it records this pseudo
612 * event.
613 * We record the max timestamp t found in the pass n.
614 * Assuming these timestamps are monotonic across cpus, we know that if
615 * a buffer still has events with timestamps below t, they will be all
616 * available and then read in the pass n + 1.
617 * Hence when we start to read the pass n + 2, we can safely flush every
618 * events with timestamps below t.
619 *
620 * ============ PASS n =================
621 * CPU 0 | CPU 1
622 * |
623 * cnt1 timestamps | cnt2 timestamps
624 * 1 | 2
625 * 2 | 3
626 * - | 4 <--- max recorded
627 *
628 * ============ PASS n + 1 ==============
629 * CPU 0 | CPU 1
630 * |
631 * cnt1 timestamps | cnt2 timestamps
632 * 3 | 5
633 * 4 | 6
634 * 5 | 7 <---- max recorded
635 *
636 * Flush every events below timestamp 4
637 *
638 * ============ PASS n + 2 ==============
639 * CPU 0 | CPU 1
640 * |
641 * cnt1 timestamps | cnt2 timestamps
642 * 6 | 8
643 * 7 | 9
644 * - | 10
645 *
646 * Flush every events below timestamp 7
647 * etc...
648 */
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200649static int process_finished_round(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200650 union perf_event *event __used,
651 struct perf_session *session)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200652{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200653 flush_sample_queue(session, tool);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200654 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
655
656 return 0;
657}
658
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200659/* The queue is ordered by time */
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100660static void __queue_event(struct sample_queue *new, struct perf_session *s)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200661{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000662 struct ordered_samples *os = &s->ordered_samples;
663 struct sample_queue *sample = os->last_sample;
664 u64 timestamp = new->timestamp;
665 struct list_head *p;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200666
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200667 ++os->nr_samples;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000668 os->last_sample = new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200669
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000670 if (!sample) {
671 list_add(&new->list, &os->samples);
672 os->max_timestamp = timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200673 return;
674 }
675
676 /*
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000677 * last_sample might point to some random place in the list as it's
678 * the last queued event. We expect that the new event is close to
679 * this.
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200680 */
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000681 if (sample->timestamp <= timestamp) {
682 while (sample->timestamp <= timestamp) {
683 p = sample->list.next;
684 if (p == &os->samples) {
685 list_add_tail(&new->list, &os->samples);
686 os->max_timestamp = timestamp;
687 return;
688 }
689 sample = list_entry(p, struct sample_queue, list);
690 }
691 list_add_tail(&new->list, &sample->list);
692 } else {
693 while (sample->timestamp > timestamp) {
694 p = sample->list.prev;
695 if (p == &os->samples) {
696 list_add(&new->list, &os->samples);
697 return;
698 }
699 sample = list_entry(p, struct sample_queue, list);
700 }
701 list_add(&new->list, &sample->list);
702 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200703}
704
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000705#define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
706
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200707static int perf_session_queue_event(struct perf_session *s, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200708 struct perf_sample *sample, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200709{
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000710 struct ordered_samples *os = &s->ordered_samples;
711 struct list_head *sc = &os->sample_cache;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200712 u64 timestamp = sample->time;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200713 struct sample_queue *new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200714
Thomas Gleixner79a14c12010-12-07 12:48:44 +0000715 if (!timestamp || timestamp == ~0ULL)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100716 return -ETIME;
717
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200718 if (timestamp < s->ordered_samples.last_flush) {
719 printf("Warning: Timestamp below last timeslice flush\n");
720 return -EINVAL;
721 }
722
Thomas Gleixner020bb752010-11-30 17:49:53 +0000723 if (!list_empty(sc)) {
724 new = list_entry(sc->next, struct sample_queue, list);
725 list_del(&new->list);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000726 } else if (os->sample_buffer) {
727 new = os->sample_buffer + os->sample_buffer_idx;
728 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
729 os->sample_buffer = NULL;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000730 } else {
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000731 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
732 if (!os->sample_buffer)
Thomas Gleixner020bb752010-11-30 17:49:53 +0000733 return -ENOMEM;
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000734 list_add(&os->sample_buffer->list, &os->to_free);
735 os->sample_buffer_idx = 2;
736 new = os->sample_buffer + 1;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000737 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200738
739 new->timestamp = timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000740 new->file_offset = file_offset;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000741 new->event = event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200742
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100743 __queue_event(new, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200744
745 return 0;
746}
747
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200748static void callchain__printf(struct perf_sample *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200749{
750 unsigned int i;
751
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200752 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200753
754 for (i = 0; i < sample->callchain->nr; i++)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200755 printf("..... %2d: %016" PRIx64 "\n",
756 i, sample->callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200757}
758
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100759static void branch_stack__printf(struct perf_sample *sample)
760{
761 uint64_t i;
762
763 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
764
765 for (i = 0; i < sample->branch_stack->nr; i++)
766 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n",
767 i, sample->branch_stack->entries[i].from,
768 sample->branch_stack->entries[i].to);
769}
770
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200771static void perf_session__print_tstamp(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200772 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200773 struct perf_sample *sample)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200774{
775 if (event->header.type != PERF_RECORD_SAMPLE &&
776 !session->sample_id_all) {
777 fputs("-1 -1 ", stdout);
778 return;
779 }
780
781 if ((session->sample_type & PERF_SAMPLE_CPU))
782 printf("%u ", sample->cpu);
783
784 if (session->sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200785 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200786}
787
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200788static void dump_event(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200789 u64 file_offset, struct perf_sample *sample)
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000790{
791 if (!dump_trace)
792 return;
793
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200794 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
795 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000796
797 trace_event(event);
798
799 if (sample)
800 perf_session__print_tstamp(session, event, sample);
801
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200802 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200803 event->header.size, perf_event__name(event->header.type));
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000804}
805
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200806static void dump_sample(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200807 struct perf_sample *sample)
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000808{
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200809 if (!dump_trace)
810 return;
811
David Ahern7cec0922011-05-30 13:08:23 -0600812 printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200813 event->header.misc, sample->pid, sample->tid, sample->ip,
David Ahern7cec0922011-05-30 13:08:23 -0600814 sample->period, sample->addr);
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000815
816 if (session->sample_type & PERF_SAMPLE_CALLCHAIN)
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200817 callchain__printf(sample);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100818
819 if (session->sample_type & PERF_SAMPLE_BRANCH_STACK)
820 branch_stack__printf(sample);
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000821}
822
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200823static struct machine *
824 perf_session__find_machine_for_cpumode(struct perf_session *session,
825 union perf_event *event)
826{
827 const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
828
829 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest)
830 return perf_session__find_machine(session, event->ip.pid);
831
832 return perf_session__find_host_machine(session);
833}
834
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100835static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200836 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200837 struct perf_sample *sample,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200838 struct perf_tool *tool,
Thomas Gleixner532e7262010-12-07 12:48:55 +0000839 u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100840{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300841 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200842 struct machine *machine;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300843
Thomas Gleixner532e7262010-12-07 12:48:55 +0000844 dump_event(session, event, file_offset, sample);
845
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200846 evsel = perf_evlist__id2evsel(session->evlist, sample->id);
847 if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) {
848 /*
849 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
850 * because the tools right now may apply filters, discarding
851 * some of the samples. For consistency, in the future we
852 * should have something like nr_filtered_samples and remove
853 * the sample->period from total_sample_period, etc, KISS for
854 * now tho.
855 *
856 * Also testing against NULL allows us to handle files without
857 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
858 * future probably it'll be a good idea to restrict event
859 * processing via perf_session to files with both set.
860 */
861 hists__inc_nr_events(&evsel->hists, event->header.type);
862 }
863
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200864 machine = perf_session__find_machine_for_cpumode(session, event);
865
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100866 switch (event->header.type) {
867 case PERF_RECORD_SAMPLE:
Thomas Gleixner532e7262010-12-07 12:48:55 +0000868 dump_sample(session, event, sample);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300869 if (evsel == NULL) {
870 ++session->hists.stats.nr_unknown_id;
871 return -1;
872 }
Joerg Roedel0c095712012-02-10 18:05:04 +0100873 if (machine == NULL) {
874 ++session->hists.stats.nr_unprocessable_samples;
875 return -1;
876 }
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200877 return tool->sample(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100878 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200879 return tool->mmap(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100880 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200881 return tool->comm(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100882 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200883 return tool->fork(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100884 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200885 return tool->exit(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100886 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200887 if (tool->lost == perf_event__process_lost)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200888 session->hists.stats.total_lost += event->lost.lost;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200889 return tool->lost(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100890 case PERF_RECORD_READ:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200891 return tool->read(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100892 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200893 return tool->throttle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100894 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200895 return tool->unthrottle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100896 default:
897 ++session->hists.stats.nr_unknown_events;
898 return -1;
899 }
900}
901
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000902static int perf_session__preprocess_sample(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200903 union perf_event *event, struct perf_sample *sample)
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000904{
905 if (event->header.type != PERF_RECORD_SAMPLE ||
906 !(session->sample_type & PERF_SAMPLE_CALLCHAIN))
907 return 0;
908
909 if (!ip_callchain__valid(sample->callchain, event)) {
910 pr_debug("call-chain problem with event, skipping it.\n");
911 ++session->hists.stats.nr_invalid_chains;
912 session->hists.stats.total_invalid_chains += sample->period;
913 return -EINVAL;
914 }
915 return 0;
916}
917
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200918static int perf_session__process_user_event(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200919 struct perf_tool *tool, u64 file_offset)
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000920{
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200921 int err;
922
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000923 dump_event(session, event, file_offset, NULL);
924
925 /* These events are processed right away */
926 switch (event->header.type) {
927 case PERF_RECORD_HEADER_ATTR:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200928 err = tool->attr(event, &session->evlist);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200929 if (err == 0)
930 perf_session__update_sample_type(session);
931 return err;
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000932 case PERF_RECORD_HEADER_EVENT_TYPE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200933 return tool->event_type(tool, event);
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000934 case PERF_RECORD_HEADER_TRACING_DATA:
935 /* setup for reading amidst mmap */
936 lseek(session->fd, file_offset, SEEK_SET);
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200937 return tool->tracing_data(event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000938 case PERF_RECORD_HEADER_BUILD_ID:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200939 return tool->build_id(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000940 case PERF_RECORD_FINISHED_ROUND:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200941 return tool->finished_round(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000942 default:
943 return -EINVAL;
944 }
945}
946
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100947static int perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200948 union perf_event *event,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200949 struct perf_tool *tool,
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000950 u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200951{
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200952 struct perf_sample sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100953 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200954
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200955 if (session->header.needs_swap &&
956 perf_event__swap_ops[event->header.type])
957 perf_event__swap_ops[event->header.type](event);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200958
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000959 if (event->header.type >= PERF_RECORD_HEADER_MAX)
960 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200961
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000962 hists__inc_nr_events(&session->hists, event->header.type);
963
964 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200965 return perf_session__process_user_event(session, event, tool, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100966
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000967 /*
968 * For all kernel events we get the sample data
969 */
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200970 ret = perf_session__parse_sample(session, event, &sample);
971 if (ret)
972 return ret;
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000973
974 /* Preprocess sample records - precheck callchains */
975 if (perf_session__preprocess_sample(session, event, &sample))
976 return 0;
977
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200978 if (tool->ordered_samples) {
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000979 ret = perf_session_queue_event(session, event, &sample,
980 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100981 if (ret != -ETIME)
982 return ret;
983 }
984
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200985 return perf_session_deliver_event(session, event, &sample, tool,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000986 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200987}
988
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200989void perf_event_header__bswap(struct perf_event_header *self)
990{
991 self->type = bswap_32(self->type);
992 self->misc = bswap_16(self->misc);
993 self->size = bswap_16(self->size);
994}
995
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200996struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
997{
998 return machine__findnew_thread(&session->host_machine, pid);
999}
1000
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001001static struct thread *perf_session__register_idle_thread(struct perf_session *self)
1002{
1003 struct thread *thread = perf_session__findnew(self, 0);
1004
1005 if (thread == NULL || thread__set_comm(thread, "swapper")) {
1006 pr_err("problem inserting idle task.\n");
1007 thread = NULL;
1008 }
1009
1010 return thread;
1011}
1012
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001013static void perf_session__warn_about_errors(const struct perf_session *session,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001014 const struct perf_tool *tool)
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001015{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001016 if (tool->lost == perf_event__process_lost &&
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001017 session->hists.stats.nr_events[PERF_RECORD_LOST] != 0) {
1018 ui__warning("Processed %d events and lost %d chunks!\n\n"
1019 "Check IO/CPU overload!\n\n",
1020 session->hists.stats.nr_events[0],
1021 session->hists.stats.nr_events[PERF_RECORD_LOST]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001022 }
1023
1024 if (session->hists.stats.nr_unknown_events != 0) {
1025 ui__warning("Found %u unknown events!\n\n"
1026 "Is this an older tool processing a perf.data "
1027 "file generated by a more recent tool?\n\n"
1028 "If that is not the case, consider "
1029 "reporting to linux-kernel@vger.kernel.org.\n\n",
1030 session->hists.stats.nr_unknown_events);
1031 }
1032
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001033 if (session->hists.stats.nr_unknown_id != 0) {
1034 ui__warning("%u samples with id not present in the header\n",
1035 session->hists.stats.nr_unknown_id);
1036 }
1037
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001038 if (session->hists.stats.nr_invalid_chains != 0) {
1039 ui__warning("Found invalid callchains!\n\n"
1040 "%u out of %u events were discarded for this reason.\n\n"
1041 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1042 session->hists.stats.nr_invalid_chains,
1043 session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
1044 }
Joerg Roedel0c095712012-02-10 18:05:04 +01001045
1046 if (session->hists.stats.nr_unprocessable_samples != 0) {
1047 ui__warning("%u unprocessable samples recorded.\n"
1048 "Do you have a KVM guest running and not using 'perf kvm'?\n",
1049 session->hists.stats.nr_unprocessable_samples);
1050 }
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001051}
1052
Tom Zanussi8dc58102010-04-01 23:59:15 -05001053#define session_done() (*(volatile int *)(&session_done))
1054volatile int session_done;
1055
1056static int __perf_session__process_pipe_events(struct perf_session *self,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001057 struct perf_tool *tool)
Tom Zanussi8dc58102010-04-01 23:59:15 -05001058{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001059 union perf_event event;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001060 uint32_t size;
1061 int skip = 0;
1062 u64 head;
1063 int err;
1064 void *p;
1065
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001066 perf_tool__fill_defaults(tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001067
1068 head = 0;
1069more:
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02001070 err = readn(self->fd, &event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05001071 if (err <= 0) {
1072 if (err == 0)
1073 goto done;
1074
1075 pr_err("failed to read event header\n");
1076 goto out_err;
1077 }
1078
1079 if (self->header.needs_swap)
1080 perf_event_header__bswap(&event.header);
1081
1082 size = event.header.size;
1083 if (size == 0)
1084 size = 8;
1085
1086 p = &event;
1087 p += sizeof(struct perf_event_header);
1088
Tom Zanussi794e43b2010-05-05 00:27:40 -05001089 if (size - sizeof(struct perf_event_header)) {
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02001090 err = readn(self->fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -05001091 if (err <= 0) {
1092 if (err == 0) {
1093 pr_err("unexpected end of event stream\n");
1094 goto done;
1095 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001096
Tom Zanussi794e43b2010-05-05 00:27:40 -05001097 pr_err("failed to read event data\n");
1098 goto out_err;
1099 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001100 }
1101
Namhyung Kim29c98622011-12-28 00:35:48 +09001102 if ((skip = perf_session__process_event(self, &event, tool, head)) < 0) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001103 dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
Tom Zanussi8dc58102010-04-01 23:59:15 -05001104 head, event.header.size, event.header.type);
1105 /*
1106 * assume we lost track of the stream, check alignment, and
1107 * increment a single u64 in the hope to catch on again 'soon'.
1108 */
1109 if (unlikely(head & 7))
1110 head &= ~7ULL;
1111
1112 size = 8;
1113 }
1114
1115 head += size;
1116
Tom Zanussi8dc58102010-04-01 23:59:15 -05001117 if (skip > 0)
1118 head += skip;
1119
1120 if (!session_done())
1121 goto more;
1122done:
1123 err = 0;
1124out_err:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001125 perf_session__warn_about_errors(self, tool);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001126 perf_session_free_sample_buffers(self);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001127 return err;
1128}
1129
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001130static union perf_event *
1131fetch_mmaped_event(struct perf_session *session,
1132 u64 head, size_t mmap_size, char *buf)
1133{
1134 union perf_event *event;
1135
1136 /*
1137 * Ensure we have enough space remaining to read
1138 * the size of the event in the headers.
1139 */
1140 if (head + sizeof(event->header) > mmap_size)
1141 return NULL;
1142
1143 event = (union perf_event *)(buf + head);
1144
1145 if (session->header.needs_swap)
1146 perf_event_header__bswap(&event->header);
1147
1148 if (head + event->header.size > mmap_size)
1149 return NULL;
1150
1151 return event;
1152}
1153
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001154int __perf_session__process_events(struct perf_session *session,
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001155 u64 data_offset, u64 data_size,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001156 u64 file_size, struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001157{
Thomas Gleixner55b44622010-11-30 17:49:46 +00001158 u64 head, page_offset, file_offset, file_pos, progress_next;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001159 int err, mmap_prot, mmap_flags, map_idx = 0;
Thomas Gleixner55b44622010-11-30 17:49:46 +00001160 size_t page_size, mmap_size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001161 char *buf, *mmaps[8];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001162 union perf_event *event;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001163 uint32_t size;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001164
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001165 perf_tool__fill_defaults(tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001166
Arnaldo Carvalho de Melo1b759622010-01-14 18:30:04 -02001167 page_size = sysconf(_SC_PAGESIZE);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001168
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001169 page_offset = page_size * (data_offset / page_size);
1170 file_offset = page_offset;
1171 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001172
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001173 if (data_offset + data_size < file_size)
1174 file_size = data_offset + data_size;
1175
Thomas Gleixner55b44622010-11-30 17:49:46 +00001176 progress_next = file_size / 16;
Thomas Gleixner55b44622010-11-30 17:49:46 +00001177
1178 mmap_size = session->mmap_window;
1179 if (mmap_size > file_size)
1180 mmap_size = file_size;
1181
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001182 memset(mmaps, 0, sizeof(mmaps));
1183
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001184 mmap_prot = PROT_READ;
1185 mmap_flags = MAP_SHARED;
1186
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001187 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001188 mmap_prot |= PROT_WRITE;
1189 mmap_flags = MAP_PRIVATE;
1190 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001191remap:
Thomas Gleixner55b44622010-11-30 17:49:46 +00001192 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
1193 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001194 if (buf == MAP_FAILED) {
1195 pr_err("failed to mmap file\n");
1196 err = -errno;
1197 goto out_err;
1198 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001199 mmaps[map_idx] = buf;
1200 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001201 file_pos = file_offset + head;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001202
1203more:
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001204 event = fetch_mmaped_event(session, head, mmap_size, buf);
1205 if (!event) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001206 if (mmaps[map_idx]) {
1207 munmap(mmaps[map_idx], mmap_size);
1208 mmaps[map_idx] = NULL;
1209 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001210
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001211 page_offset = page_size * (head / page_size);
1212 file_offset += page_offset;
1213 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001214 goto remap;
1215 }
1216
1217 size = event->header.size;
1218
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001219 if (size == 0 ||
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001220 perf_session__process_event(session, event, tool, file_pos) < 0) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001221 dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001222 file_offset + head, event->header.size,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001223 event->header.type);
1224 /*
1225 * assume we lost track of the stream, check alignment, and
1226 * increment a single u64 in the hope to catch on again 'soon'.
1227 */
1228 if (unlikely(head & 7))
1229 head &= ~7ULL;
1230
1231 size = 8;
1232 }
1233
1234 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001235 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001236
Thomas Gleixner55b44622010-11-30 17:49:46 +00001237 if (file_pos >= progress_next) {
1238 progress_next += file_size / 16;
Arnaldo Carvalho de Meloca59bcb2011-10-25 13:29:11 -02001239 ui_progress__update(file_pos, file_size,
1240 "Processing events...");
Thomas Gleixner55b44622010-11-30 17:49:46 +00001241 }
1242
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001243 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001244 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001245
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001246 err = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001247 /* do the final flush for ordered samples */
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001248 session->ordered_samples.next_flush = ULLONG_MAX;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001249 flush_sample_queue(session, tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001250out_err:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001251 perf_session__warn_about_errors(session, tool);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001252 perf_session_free_sample_buffers(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001253 return err;
1254}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001255
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001256int perf_session__process_events(struct perf_session *self,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001257 struct perf_tool *tool)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001258{
1259 int err;
1260
1261 if (perf_session__register_idle_thread(self) == NULL)
1262 return -ENOMEM;
1263
Tom Zanussi8dc58102010-04-01 23:59:15 -05001264 if (!self->fd_pipe)
1265 err = __perf_session__process_events(self,
1266 self->header.data_offset,
1267 self->header.data_size,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001268 self->size, tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001269 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001270 err = __perf_session__process_pipe_events(self, tool);
Dave Martin88ca8952010-07-27 11:46:12 -03001271
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001272 return err;
1273}
1274
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001275bool perf_session__has_traces(struct perf_session *self, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001276{
1277 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001278 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1279 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001280 }
1281
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001282 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001283}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001284
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001285int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1286 const char *symbol_name, u64 addr)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001287{
1288 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001289 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001290 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001291
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001292 ref = zalloc(sizeof(struct ref_reloc_sym));
1293 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001294 return -ENOMEM;
1295
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001296 ref->name = strdup(symbol_name);
1297 if (ref->name == NULL) {
1298 free(ref);
1299 return -ENOMEM;
1300 }
1301
1302 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001303 if (bracket)
1304 *bracket = '\0';
1305
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001306 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001307
1308 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001309 struct kmap *kmap = map__kmap(maps[i]);
1310 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001311 }
1312
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001313 return 0;
1314}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001315
1316size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
1317{
1318 return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
1319 __dsos__fprintf(&self->host_machine.user_dsos, fp) +
1320 machines__fprintf_dsos(&self->machines, fp);
1321}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001322
1323size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
1324 bool with_hits)
1325{
1326 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
1327 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
1328}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001329
1330size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1331{
1332 struct perf_evsel *pos;
1333 size_t ret = fprintf(fp, "Aggregated stats:\n");
1334
1335 ret += hists__fprintf_nr_events(&session->hists, fp);
1336
1337 list_for_each_entry(pos, &session->evlist->entries, node) {
1338 ret += fprintf(fp, "%s stats:\n", event_name(pos));
1339 ret += hists__fprintf_nr_events(&pos->hists, fp);
1340 }
1341
1342 return ret;
1343}
David Ahernc0230b22011-03-09 22:23:27 -07001344
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001345size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1346{
1347 /*
1348 * FIXME: Here we have to actually print all the machines in this
1349 * session, not just the host...
1350 */
1351 return machine__fprintf(&session->host_machine, fp);
1352}
1353
1354void perf_session__remove_thread(struct perf_session *session,
1355 struct thread *th)
1356{
1357 /*
1358 * FIXME: This one makes no sense, we need to remove the thread from
1359 * the machine it belongs to, perf_session can have many machines, so
1360 * doing it always on ->host_machine is wrong. Fix when auditing all
1361 * the 'perf kvm' code.
1362 */
1363 machine__remove_thread(&session->host_machine, th);
1364}
1365
David Ahern9cbdb702011-04-06 21:54:20 -06001366struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1367 unsigned int type)
1368{
1369 struct perf_evsel *pos;
1370
1371 list_for_each_entry(pos, &session->evlist->entries, node) {
1372 if (pos->attr.type == type)
1373 return pos;
1374 }
1375 return NULL;
1376}
1377
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001378void perf_event__print_ip(union perf_event *event, struct perf_sample *sample,
1379 struct machine *machine, struct perf_evsel *evsel,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001380 int print_sym, int print_dso, int print_symoffset)
David Ahernc0230b22011-03-09 22:23:27 -07001381{
1382 struct addr_location al;
Arnaldo Carvalho de Melo246d4ce2011-11-11 23:10:26 -02001383 struct callchain_cursor *cursor = &evsel->hists.callchain_cursor;
David Ahernc0230b22011-03-09 22:23:27 -07001384 struct callchain_cursor_node *node;
1385
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001386 if (perf_event__preprocess_sample(event, machine, &al, sample,
David Ahernc0230b22011-03-09 22:23:27 -07001387 NULL) < 0) {
1388 error("problem processing %d event, skipping it.\n",
1389 event->header.type);
1390 return;
1391 }
1392
1393 if (symbol_conf.use_callchain && sample->callchain) {
1394
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001395 if (machine__resolve_callchain(machine, evsel, al.thread,
David Ahernc0230b22011-03-09 22:23:27 -07001396 sample->callchain, NULL) != 0) {
1397 if (verbose)
1398 error("Failed to resolve callchain. Skipping\n");
1399 return;
1400 }
1401 callchain_cursor_commit(cursor);
1402
1403 while (1) {
1404 node = callchain_cursor_current(cursor);
1405 if (!node)
1406 break;
1407
David Ahern787bef12011-05-27 14:28:43 -06001408 printf("\t%16" PRIx64, node->ip);
1409 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001410 printf(" ");
1411 symbol__fprintf_symname(node->sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001412 }
1413 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001414 printf(" (");
1415 map__fprintf_dsoname(al.map, stdout);
1416 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001417 }
1418 printf("\n");
David Ahernc0230b22011-03-09 22:23:27 -07001419
1420 callchain_cursor_advance(cursor);
1421 }
1422
1423 } else {
David Ahernadc4bf92011-05-30 09:16:27 -06001424 printf("%16" PRIx64, sample->ip);
David Ahern787bef12011-05-27 14:28:43 -06001425 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001426 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001427 if (print_symoffset)
1428 symbol__fprintf_symname_offs(al.sym, &al,
1429 stdout);
1430 else
1431 symbol__fprintf_symname(al.sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001432 }
1433
1434 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001435 printf(" (");
1436 map__fprintf_dsoname(al.map, stdout);
1437 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001438 }
David Ahernc0230b22011-03-09 22:23:27 -07001439 }
1440}
Anton Blanchard5d67be92011-07-04 21:57:50 +10001441
1442int perf_session__cpu_bitmap(struct perf_session *session,
1443 const char *cpu_list, unsigned long *cpu_bitmap)
1444{
1445 int i;
1446 struct cpu_map *map;
1447
1448 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1449 struct perf_evsel *evsel;
1450
1451 evsel = perf_session__find_first_evtype(session, i);
1452 if (!evsel)
1453 continue;
1454
1455 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1456 pr_err("File does not contain CPU events. "
1457 "Remove -c option to proceed.\n");
1458 return -1;
1459 }
1460 }
1461
1462 map = cpu_map__new(cpu_list);
David Ahern47fbe532011-11-13 10:45:27 -07001463 if (map == NULL) {
1464 pr_err("Invalid cpu_list\n");
1465 return -1;
1466 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001467
1468 for (i = 0; i < map->nr; i++) {
1469 int cpu = map->map[i];
1470
1471 if (cpu >= MAX_NR_CPUS) {
1472 pr_err("Requested CPU %d too large. "
1473 "Consider raising MAX_NR_CPUS\n", cpu);
1474 return -1;
1475 }
1476
1477 set_bit(cpu, cpu_bitmap);
1478 }
1479
1480 return 0;
1481}
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001482
1483void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1484 bool full)
1485{
1486 struct stat st;
1487 int ret;
1488
1489 if (session == NULL || fp == NULL)
1490 return;
1491
1492 ret = fstat(session->fd, &st);
1493 if (ret == -1)
1494 return;
1495
1496 fprintf(fp, "# ========\n");
1497 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1498 perf_header__fprintf_info(session, fp, full);
1499 fprintf(fp, "# ========\n#\n");
1500}