blob: caa224522fea9b0ff04984595a09e2eccd31e438 [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 Meloa3286262009-12-14 14:22:59 -020013#include "sort.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020014#include "util.h"
15
16static int perf_session__open(struct perf_session *self, bool force)
17{
18 struct stat input_stat;
19
Tom Zanussi8dc58102010-04-01 23:59:15 -050020 if (!strcmp(self->filename, "-")) {
21 self->fd_pipe = true;
22 self->fd = STDIN_FILENO;
23
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030024 if (perf_session__read_header(self, self->fd) < 0)
Tom Zanussi8dc58102010-04-01 23:59:15 -050025 pr_err("incompatible file format");
26
27 return 0;
28 }
29
Xiao Guangrongf887f302010-02-04 16:46:42 +080030 self->fd = open(self->filename, O_RDONLY);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020031 if (self->fd < 0) {
Andy Isaacson0f2c3de2010-06-11 20:36:15 -070032 int err = errno;
33
34 pr_err("failed to open %s: %s", self->filename, strerror(err));
35 if (err == ENOENT && !strcmp(self->filename, "perf.data"))
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020036 pr_err(" (try 'perf record' first)");
37 pr_err("\n");
38 return -errno;
39 }
40
41 if (fstat(self->fd, &input_stat) < 0)
42 goto out_close;
43
44 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
45 pr_err("file %s not owned by current user or root\n",
46 self->filename);
47 goto out_close;
48 }
49
50 if (!input_stat.st_size) {
51 pr_info("zero-sized file (%s), nothing to do!\n",
52 self->filename);
53 goto out_close;
54 }
55
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030056 if (perf_session__read_header(self, self->fd) < 0) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020057 pr_err("incompatible file format");
58 goto out_close;
59 }
60
61 self->size = input_stat.st_size;
62 return 0;
63
64out_close:
65 close(self->fd);
66 self->fd = -1;
67 return -1;
68}
69
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020070static void perf_session__id_header_size(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -050071{
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -020072 struct perf_sample *data;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020073 u64 sample_type = session->sample_type;
74 u16 size = 0;
75
76 if (!session->sample_id_all)
77 goto out;
78
79 if (sample_type & PERF_SAMPLE_TID)
80 size += sizeof(data->tid) * 2;
81
82 if (sample_type & PERF_SAMPLE_TIME)
83 size += sizeof(data->time);
84
85 if (sample_type & PERF_SAMPLE_ID)
86 size += sizeof(data->id);
87
88 if (sample_type & PERF_SAMPLE_STREAM_ID)
89 size += sizeof(data->stream_id);
90
91 if (sample_type & PERF_SAMPLE_CPU)
92 size += sizeof(data->cpu) * 2;
93out:
94 session->id_hdr_size = size;
95}
96
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020097void perf_session__update_sample_type(struct perf_session *self)
98{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030099 self->sample_type = perf_evlist__sample_type(self->evlist);
100 self->sample_id_all = perf_evlist__sample_id_all(self->evlist);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200101 perf_session__id_header_size(self);
102}
103
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800104int perf_session__create_kernel_maps(struct perf_session *self)
105{
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -0300106 int ret = machine__create_kernel_maps(&self->host_machine);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800107
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800108 if (ret >= 0)
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -0300109 ret = machines__create_guest_kernel_maps(&self->machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800110 return ret;
111}
112
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300113static void perf_session__destroy_kernel_maps(struct perf_session *self)
114{
115 machine__destroy_kernel_maps(&self->host_machine);
116 machines__destroy_guest_kernel_maps(&self->machines);
117}
118
Ian Munsie21ef97f2010-12-10 14:09:16 +1100119struct perf_session *perf_session__new(const char *filename, int mode,
120 bool force, bool repipe,
121 struct perf_event_ops *ops)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200122{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200123 size_t len = filename ? strlen(filename) + 1 : 0;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200124 struct perf_session *self = zalloc(sizeof(*self) + len);
125
126 if (self == NULL)
127 goto out;
128
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200129 memcpy(self->filename, filename, len);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200130 self->threads = RB_ROOT;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300131 INIT_LIST_HEAD(&self->dead_threads);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200132 self->last_match = NULL;
Thomas Gleixner55b44622010-11-30 17:49:46 +0000133 /*
134 * On 64bit we can mmap the data file in one go. No need for tiny mmap
135 * slices. On 32bit we use 32MB.
136 */
137#if BITS_PER_LONG == 64
138 self->mmap_window = ULLONG_MAX;
139#else
140 self->mmap_window = 32 * 1024 * 1024ULL;
141#endif
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300142 self->machines = RB_ROOT;
Tom Zanussi454c4072010-05-01 01:41:20 -0500143 self->repipe = repipe;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000144 INIT_LIST_HEAD(&self->ordered_samples.samples);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000145 INIT_LIST_HEAD(&self->ordered_samples.sample_cache);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000146 INIT_LIST_HEAD(&self->ordered_samples.to_free);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300147 machine__init(&self->host_machine, "", HOST_KERNEL_ID);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200148
Arnaldo Carvalho de Melo64abebf2010-01-27 21:05:52 -0200149 if (mode == O_RDONLY) {
150 if (perf_session__open(self, force) < 0)
151 goto out_delete;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300152 perf_session__update_sample_type(self);
Arnaldo Carvalho de Melo64abebf2010-01-27 21:05:52 -0200153 } else if (mode == O_WRONLY) {
154 /*
155 * In O_RDONLY mode this will be performed when reading the
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200156 * kernel MMAP event, in perf_event__process_mmap().
Arnaldo Carvalho de Melo64abebf2010-01-27 21:05:52 -0200157 */
158 if (perf_session__create_kernel_maps(self) < 0)
159 goto out_delete;
160 }
Arnaldo Carvalho de Melod549c762009-12-27 21:37:02 -0200161
Ian Munsie21ef97f2010-12-10 14:09:16 +1100162 if (ops && ops->ordering_requires_timestamps &&
163 ops->ordered_samples && !self->sample_id_all) {
164 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
165 ops->ordered_samples = false;
166 }
167
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200168out:
169 return self;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200170out_delete:
171 perf_session__delete(self);
172 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200173}
174
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300175static void perf_session__delete_dead_threads(struct perf_session *self)
176{
177 struct thread *n, *t;
178
179 list_for_each_entry_safe(t, n, &self->dead_threads, node) {
180 list_del(&t->node);
181 thread__delete(t);
182 }
183}
184
185static void perf_session__delete_threads(struct perf_session *self)
186{
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 Melo94c744b2009-12-11 21:24:02 -0200198void perf_session__delete(struct perf_session *self)
199{
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300200 perf_session__destroy_kernel_maps(self);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300201 perf_session__delete_dead_threads(self);
202 perf_session__delete_threads(self);
203 machine__exit(&self->host_machine);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200204 close(self->fd);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200205 free(self);
206}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200207
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300208void perf_session__remove_thread(struct perf_session *self, struct thread *th)
209{
Arnaldo Carvalho de Melo70597f22010-08-02 18:59:28 -0300210 self->last_match = NULL;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300211 rb_erase(&th->rb_node, &self->threads);
212 /*
213 * We may have references to this thread, for instance in some hist_entry
214 * instances, so just move them to a separate list.
215 */
216 list_add_tail(&th->node, &self->dead_threads);
217}
218
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200219static bool symbol__match_parent_regex(struct symbol *sym)
220{
221 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
222 return 1;
223
224 return 0;
225}
226
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100227int perf_session__resolve_callchain(struct perf_session *self,
228 struct thread *thread,
229 struct ip_callchain *chain,
230 struct symbol **parent)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200231{
232 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200233 unsigned int i;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100234 int err;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200235
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100236 callchain_cursor_reset(&self->callchain_cursor);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200237
238 for (i = 0; i < chain->nr; i++) {
239 u64 ip = chain->ips[i];
240 struct addr_location al;
241
242 if (ip >= PERF_CONTEXT_MAX) {
243 switch (ip) {
244 case PERF_CONTEXT_HV:
245 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
246 case PERF_CONTEXT_KERNEL:
247 cpumode = PERF_RECORD_MISC_KERNEL; break;
248 case PERF_CONTEXT_USER:
249 cpumode = PERF_RECORD_MISC_USER; break;
250 default:
251 break;
252 }
253 continue;
254 }
255
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800256 al.filtered = false;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200257 thread__find_addr_location(thread, self, cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800258 MAP__FUNCTION, thread->pid, ip, &al, NULL);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200259 if (al.sym != NULL) {
260 if (sort__has_parent && !*parent &&
261 symbol__match_parent_regex(al.sym))
262 *parent = al.sym;
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200263 if (!symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200264 break;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200265 }
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100266
267 err = callchain_cursor_append(&self->callchain_cursor,
268 ip, al.map, al.sym);
269 if (err)
270 return err;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200271 }
272
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100273 return 0;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200274}
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200275
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200276static int process_event_synth_stub(union perf_event *event __used,
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200277 struct perf_session *session __used)
278{
279 dump_printf(": unhandled!\n");
280 return 0;
281}
282
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300283static int process_event_sample_stub(union perf_event *event __used,
284 struct perf_sample *sample __used,
285 struct perf_evsel *evsel __used,
286 struct perf_session *session __used)
287{
288 dump_printf(": unhandled!\n");
289 return 0;
290}
291
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200292static int process_event_stub(union perf_event *event __used,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200293 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200294 struct perf_session *session __used)
295{
296 dump_printf(": unhandled!\n");
297 return 0;
298}
299
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200300static int process_finished_round_stub(union perf_event *event __used,
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200301 struct perf_session *session __used,
302 struct perf_event_ops *ops __used)
303{
304 dump_printf(": unhandled!\n");
305 return 0;
306}
307
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200308static int process_finished_round(union perf_event *event,
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200309 struct perf_session *session,
310 struct perf_event_ops *ops);
311
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200312static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
313{
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200314 if (handler->sample == NULL)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300315 handler->sample = process_event_sample_stub;
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200316 if (handler->mmap == NULL)
317 handler->mmap = process_event_stub;
318 if (handler->comm == NULL)
319 handler->comm = process_event_stub;
320 if (handler->fork == NULL)
321 handler->fork = process_event_stub;
322 if (handler->exit == NULL)
323 handler->exit = process_event_stub;
324 if (handler->lost == NULL)
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200325 handler->lost = perf_event__process_lost;
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200326 if (handler->read == NULL)
327 handler->read = process_event_stub;
328 if (handler->throttle == NULL)
329 handler->throttle = process_event_stub;
330 if (handler->unthrottle == NULL)
331 handler->unthrottle = process_event_stub;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500332 if (handler->attr == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200333 handler->attr = process_event_synth_stub;
Tom Zanussicd19a032010-04-01 23:59:20 -0500334 if (handler->event_type == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200335 handler->event_type = process_event_synth_stub;
Tom Zanussi92155452010-04-01 23:59:21 -0500336 if (handler->tracing_data == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200337 handler->tracing_data = process_event_synth_stub;
Tom Zanussic7929e42010-04-01 23:59:22 -0500338 if (handler->build_id == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200339 handler->build_id = process_event_synth_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200340 if (handler->finished_round == NULL) {
341 if (handler->ordered_samples)
342 handler->finished_round = process_finished_round;
343 else
344 handler->finished_round = process_finished_round_stub;
345 }
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200346}
347
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200348void mem_bswap_64(void *src, int byte_size)
349{
350 u64 *m = src;
351
352 while (byte_size > 0) {
353 *m = bswap_64(*m);
354 byte_size -= sizeof(u64);
355 ++m;
356 }
357}
358
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200359static void perf_event__all64_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200360{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200361 struct perf_event_header *hdr = &event->header;
362 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200363}
364
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200365static void perf_event__comm_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200366{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200367 event->comm.pid = bswap_32(event->comm.pid);
368 event->comm.tid = bswap_32(event->comm.tid);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200369}
370
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200371static void perf_event__mmap_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200372{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200373 event->mmap.pid = bswap_32(event->mmap.pid);
374 event->mmap.tid = bswap_32(event->mmap.tid);
375 event->mmap.start = bswap_64(event->mmap.start);
376 event->mmap.len = bswap_64(event->mmap.len);
377 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200378}
379
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200380static void perf_event__task_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200381{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200382 event->fork.pid = bswap_32(event->fork.pid);
383 event->fork.tid = bswap_32(event->fork.tid);
384 event->fork.ppid = bswap_32(event->fork.ppid);
385 event->fork.ptid = bswap_32(event->fork.ptid);
386 event->fork.time = bswap_64(event->fork.time);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200387}
388
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200389static void perf_event__read_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200390{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200391 event->read.pid = bswap_32(event->read.pid);
392 event->read.tid = bswap_32(event->read.tid);
393 event->read.value = bswap_64(event->read.value);
394 event->read.time_enabled = bswap_64(event->read.time_enabled);
395 event->read.time_running = bswap_64(event->read.time_running);
396 event->read.id = bswap_64(event->read.id);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200397}
398
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200399static void perf_event__attr_swap(union perf_event *event)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500400{
401 size_t size;
402
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200403 event->attr.attr.type = bswap_32(event->attr.attr.type);
404 event->attr.attr.size = bswap_32(event->attr.attr.size);
405 event->attr.attr.config = bswap_64(event->attr.attr.config);
406 event->attr.attr.sample_period = bswap_64(event->attr.attr.sample_period);
407 event->attr.attr.sample_type = bswap_64(event->attr.attr.sample_type);
408 event->attr.attr.read_format = bswap_64(event->attr.attr.read_format);
409 event->attr.attr.wakeup_events = bswap_32(event->attr.attr.wakeup_events);
410 event->attr.attr.bp_type = bswap_32(event->attr.attr.bp_type);
411 event->attr.attr.bp_addr = bswap_64(event->attr.attr.bp_addr);
412 event->attr.attr.bp_len = bswap_64(event->attr.attr.bp_len);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500413
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200414 size = event->header.size;
415 size -= (void *)&event->attr.id - (void *)event;
416 mem_bswap_64(event->attr.id, size);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500417}
418
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200419static void perf_event__event_type_swap(union perf_event *event)
Tom Zanussicd19a032010-04-01 23:59:20 -0500420{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200421 event->event_type.event_type.event_id =
422 bswap_64(event->event_type.event_type.event_id);
Tom Zanussicd19a032010-04-01 23:59:20 -0500423}
424
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200425static void perf_event__tracing_data_swap(union perf_event *event)
Tom Zanussi92155452010-04-01 23:59:21 -0500426{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200427 event->tracing_data.size = bswap_32(event->tracing_data.size);
Tom Zanussi92155452010-04-01 23:59:21 -0500428}
429
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200430typedef void (*perf_event__swap_op)(union perf_event *event);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200431
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200432static perf_event__swap_op perf_event__swap_ops[] = {
433 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
434 [PERF_RECORD_COMM] = perf_event__comm_swap,
435 [PERF_RECORD_FORK] = perf_event__task_swap,
436 [PERF_RECORD_EXIT] = perf_event__task_swap,
437 [PERF_RECORD_LOST] = perf_event__all64_swap,
438 [PERF_RECORD_READ] = perf_event__read_swap,
439 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
440 [PERF_RECORD_HEADER_ATTR] = perf_event__attr_swap,
441 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
442 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
443 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
444 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200445};
446
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200447struct sample_queue {
448 u64 timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000449 u64 file_offset;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200450 union perf_event *event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200451 struct list_head list;
452};
453
Thomas Gleixner020bb752010-11-30 17:49:53 +0000454static void perf_session_free_sample_buffers(struct perf_session *session)
455{
456 struct ordered_samples *os = &session->ordered_samples;
457
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000458 while (!list_empty(&os->to_free)) {
Thomas Gleixner020bb752010-11-30 17:49:53 +0000459 struct sample_queue *sq;
460
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000461 sq = list_entry(os->to_free.next, struct sample_queue, list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000462 list_del(&sq->list);
463 free(sq);
464 }
465}
466
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100467static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200468 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200469 struct perf_sample *sample,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000470 struct perf_event_ops *ops,
471 u64 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100472
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200473static void flush_sample_queue(struct perf_session *s,
474 struct perf_event_ops *ops)
475{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000476 struct ordered_samples *os = &s->ordered_samples;
477 struct list_head *head = &os->samples;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200478 struct sample_queue *tmp, *iter;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200479 struct perf_sample sample;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000480 u64 limit = os->next_flush;
481 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200482
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200483 if (!ops->ordered_samples || !limit)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200484 return;
485
486 list_for_each_entry_safe(iter, tmp, head, list) {
487 if (iter->timestamp > limit)
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000488 break;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200489
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200490 perf_session__parse_sample(s, iter->event, &sample);
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000491 perf_session_deliver_event(s, iter->event, &sample, ops,
492 iter->file_offset);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200493
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000494 os->last_flush = iter->timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200495 list_del(&iter->list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000496 list_add(&iter->list, &os->sample_cache);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200497 }
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000498
499 if (list_empty(head)) {
500 os->last_sample = NULL;
501 } else if (last_ts <= limit) {
502 os->last_sample =
503 list_entry(head->prev, struct sample_queue, list);
504 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200505}
506
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200507/*
508 * When perf record finishes a pass on every buffers, it records this pseudo
509 * event.
510 * We record the max timestamp t found in the pass n.
511 * Assuming these timestamps are monotonic across cpus, we know that if
512 * a buffer still has events with timestamps below t, they will be all
513 * available and then read in the pass n + 1.
514 * Hence when we start to read the pass n + 2, we can safely flush every
515 * events with timestamps below t.
516 *
517 * ============ PASS n =================
518 * CPU 0 | CPU 1
519 * |
520 * cnt1 timestamps | cnt2 timestamps
521 * 1 | 2
522 * 2 | 3
523 * - | 4 <--- max recorded
524 *
525 * ============ PASS n + 1 ==============
526 * CPU 0 | CPU 1
527 * |
528 * cnt1 timestamps | cnt2 timestamps
529 * 3 | 5
530 * 4 | 6
531 * 5 | 7 <---- max recorded
532 *
533 * Flush every events below timestamp 4
534 *
535 * ============ PASS n + 2 ==============
536 * CPU 0 | CPU 1
537 * |
538 * cnt1 timestamps | cnt2 timestamps
539 * 6 | 8
540 * 7 | 9
541 * - | 10
542 *
543 * Flush every events below timestamp 7
544 * etc...
545 */
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200546static int process_finished_round(union perf_event *event __used,
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200547 struct perf_session *session,
548 struct perf_event_ops *ops)
549{
550 flush_sample_queue(session, ops);
551 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
552
553 return 0;
554}
555
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200556/* The queue is ordered by time */
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100557static void __queue_event(struct sample_queue *new, struct perf_session *s)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200558{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000559 struct ordered_samples *os = &s->ordered_samples;
560 struct sample_queue *sample = os->last_sample;
561 u64 timestamp = new->timestamp;
562 struct list_head *p;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200563
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000564 os->last_sample = new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200565
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000566 if (!sample) {
567 list_add(&new->list, &os->samples);
568 os->max_timestamp = timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200569 return;
570 }
571
572 /*
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000573 * last_sample might point to some random place in the list as it's
574 * the last queued event. We expect that the new event is close to
575 * this.
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200576 */
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000577 if (sample->timestamp <= timestamp) {
578 while (sample->timestamp <= timestamp) {
579 p = sample->list.next;
580 if (p == &os->samples) {
581 list_add_tail(&new->list, &os->samples);
582 os->max_timestamp = timestamp;
583 return;
584 }
585 sample = list_entry(p, struct sample_queue, list);
586 }
587 list_add_tail(&new->list, &sample->list);
588 } else {
589 while (sample->timestamp > timestamp) {
590 p = sample->list.prev;
591 if (p == &os->samples) {
592 list_add(&new->list, &os->samples);
593 return;
594 }
595 sample = list_entry(p, struct sample_queue, list);
596 }
597 list_add(&new->list, &sample->list);
598 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200599}
600
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000601#define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
602
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200603static int perf_session_queue_event(struct perf_session *s, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200604 struct perf_sample *sample, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200605{
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000606 struct ordered_samples *os = &s->ordered_samples;
607 struct list_head *sc = &os->sample_cache;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200608 u64 timestamp = sample->time;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200609 struct sample_queue *new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200610
Thomas Gleixner79a14c12010-12-07 12:48:44 +0000611 if (!timestamp || timestamp == ~0ULL)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100612 return -ETIME;
613
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200614 if (timestamp < s->ordered_samples.last_flush) {
615 printf("Warning: Timestamp below last timeslice flush\n");
616 return -EINVAL;
617 }
618
Thomas Gleixner020bb752010-11-30 17:49:53 +0000619 if (!list_empty(sc)) {
620 new = list_entry(sc->next, struct sample_queue, list);
621 list_del(&new->list);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000622 } else if (os->sample_buffer) {
623 new = os->sample_buffer + os->sample_buffer_idx;
624 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
625 os->sample_buffer = NULL;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000626 } else {
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000627 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
628 if (!os->sample_buffer)
Thomas Gleixner020bb752010-11-30 17:49:53 +0000629 return -ENOMEM;
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000630 list_add(&os->sample_buffer->list, &os->to_free);
631 os->sample_buffer_idx = 2;
632 new = os->sample_buffer + 1;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000633 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200634
635 new->timestamp = timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000636 new->file_offset = file_offset;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000637 new->event = event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200638
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100639 __queue_event(new, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200640
641 return 0;
642}
643
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200644static void callchain__printf(struct perf_sample *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200645{
646 unsigned int i;
647
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200648 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200649
650 for (i = 0; i < sample->callchain->nr; i++)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200651 printf("..... %2d: %016" PRIx64 "\n",
652 i, sample->callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200653}
654
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200655static void perf_session__print_tstamp(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200656 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200657 struct perf_sample *sample)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200658{
659 if (event->header.type != PERF_RECORD_SAMPLE &&
660 !session->sample_id_all) {
661 fputs("-1 -1 ", stdout);
662 return;
663 }
664
665 if ((session->sample_type & PERF_SAMPLE_CPU))
666 printf("%u ", sample->cpu);
667
668 if (session->sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200669 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200670}
671
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200672static void dump_event(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200673 u64 file_offset, struct perf_sample *sample)
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000674{
675 if (!dump_trace)
676 return;
677
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200678 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
679 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000680
681 trace_event(event);
682
683 if (sample)
684 perf_session__print_tstamp(session, event, sample);
685
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200686 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200687 event->header.size, perf_event__name(event->header.type));
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000688}
689
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200690static void dump_sample(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200691 struct perf_sample *sample)
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000692{
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200693 if (!dump_trace)
694 return;
695
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200696 printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 "\n",
697 event->header.misc, sample->pid, sample->tid, sample->ip,
698 sample->period);
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000699
700 if (session->sample_type & PERF_SAMPLE_CALLCHAIN)
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200701 callchain__printf(sample);
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000702}
703
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100704static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200705 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200706 struct perf_sample *sample,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000707 struct perf_event_ops *ops,
Thomas Gleixner532e7262010-12-07 12:48:55 +0000708 u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100709{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300710 struct perf_evsel *evsel;
711
Thomas Gleixner532e7262010-12-07 12:48:55 +0000712 dump_event(session, event, file_offset, sample);
713
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100714 switch (event->header.type) {
715 case PERF_RECORD_SAMPLE:
Thomas Gleixner532e7262010-12-07 12:48:55 +0000716 dump_sample(session, event, sample);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300717 evsel = perf_evlist__id2evsel(session->evlist, sample->id);
718 if (evsel == NULL) {
719 ++session->hists.stats.nr_unknown_id;
720 return -1;
721 }
722 return ops->sample(event, sample, evsel, session);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100723 case PERF_RECORD_MMAP:
724 return ops->mmap(event, sample, session);
725 case PERF_RECORD_COMM:
726 return ops->comm(event, sample, session);
727 case PERF_RECORD_FORK:
728 return ops->fork(event, sample, session);
729 case PERF_RECORD_EXIT:
730 return ops->exit(event, sample, session);
731 case PERF_RECORD_LOST:
732 return ops->lost(event, sample, session);
733 case PERF_RECORD_READ:
734 return ops->read(event, sample, session);
735 case PERF_RECORD_THROTTLE:
736 return ops->throttle(event, sample, session);
737 case PERF_RECORD_UNTHROTTLE:
738 return ops->unthrottle(event, sample, session);
739 default:
740 ++session->hists.stats.nr_unknown_events;
741 return -1;
742 }
743}
744
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000745static int perf_session__preprocess_sample(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200746 union perf_event *event, struct perf_sample *sample)
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000747{
748 if (event->header.type != PERF_RECORD_SAMPLE ||
749 !(session->sample_type & PERF_SAMPLE_CALLCHAIN))
750 return 0;
751
752 if (!ip_callchain__valid(sample->callchain, event)) {
753 pr_debug("call-chain problem with event, skipping it.\n");
754 ++session->hists.stats.nr_invalid_chains;
755 session->hists.stats.total_invalid_chains += sample->period;
756 return -EINVAL;
757 }
758 return 0;
759}
760
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200761static int perf_session__process_user_event(struct perf_session *session, union perf_event *event,
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000762 struct perf_event_ops *ops, u64 file_offset)
763{
764 dump_event(session, event, file_offset, NULL);
765
766 /* These events are processed right away */
767 switch (event->header.type) {
768 case PERF_RECORD_HEADER_ATTR:
769 return ops->attr(event, session);
770 case PERF_RECORD_HEADER_EVENT_TYPE:
771 return ops->event_type(event, session);
772 case PERF_RECORD_HEADER_TRACING_DATA:
773 /* setup for reading amidst mmap */
774 lseek(session->fd, file_offset, SEEK_SET);
775 return ops->tracing_data(event, session);
776 case PERF_RECORD_HEADER_BUILD_ID:
777 return ops->build_id(event, session);
778 case PERF_RECORD_FINISHED_ROUND:
779 return ops->finished_round(event, session, ops);
780 default:
781 return -EINVAL;
782 }
783}
784
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100785static int perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200786 union perf_event *event,
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200787 struct perf_event_ops *ops,
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000788 u64 file_offset)
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200789{
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200790 struct perf_sample sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100791 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200792
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200793 if (session->header.needs_swap &&
794 perf_event__swap_ops[event->header.type])
795 perf_event__swap_ops[event->header.type](event);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200796
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000797 if (event->header.type >= PERF_RECORD_HEADER_MAX)
798 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200799
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000800 hists__inc_nr_events(&session->hists, event->header.type);
801
802 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000803 return perf_session__process_user_event(session, event, ops, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100804
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000805 /*
806 * For all kernel events we get the sample data
807 */
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200808 perf_session__parse_sample(session, event, &sample);
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000809
810 /* Preprocess sample records - precheck callchains */
811 if (perf_session__preprocess_sample(session, event, &sample))
812 return 0;
813
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100814 if (ops->ordered_samples) {
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000815 ret = perf_session_queue_event(session, event, &sample,
816 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100817 if (ret != -ETIME)
818 return ret;
819 }
820
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000821 return perf_session_deliver_event(session, event, &sample, ops,
822 file_offset);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200823}
824
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200825void perf_event_header__bswap(struct perf_event_header *self)
826{
827 self->type = bswap_32(self->type);
828 self->misc = bswap_16(self->misc);
829 self->size = bswap_16(self->size);
830}
831
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200832static struct thread *perf_session__register_idle_thread(struct perf_session *self)
833{
834 struct thread *thread = perf_session__findnew(self, 0);
835
836 if (thread == NULL || thread__set_comm(thread, "swapper")) {
837 pr_err("problem inserting idle task.\n");
838 thread = NULL;
839 }
840
841 return thread;
842}
843
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200844static void perf_session__warn_about_errors(const struct perf_session *session,
845 const struct perf_event_ops *ops)
846{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200847 if (ops->lost == perf_event__process_lost &&
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200848 session->hists.stats.total_lost != 0) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200849 ui__warning("Processed %" PRIu64 " events and LOST %" PRIu64
850 "!\n\nCheck IO/CPU overload!\n\n",
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200851 session->hists.stats.total_period,
852 session->hists.stats.total_lost);
853 }
854
855 if (session->hists.stats.nr_unknown_events != 0) {
856 ui__warning("Found %u unknown events!\n\n"
857 "Is this an older tool processing a perf.data "
858 "file generated by a more recent tool?\n\n"
859 "If that is not the case, consider "
860 "reporting to linux-kernel@vger.kernel.org.\n\n",
861 session->hists.stats.nr_unknown_events);
862 }
863
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300864 if (session->hists.stats.nr_unknown_id != 0) {
865 ui__warning("%u samples with id not present in the header\n",
866 session->hists.stats.nr_unknown_id);
867 }
868
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200869 if (session->hists.stats.nr_invalid_chains != 0) {
870 ui__warning("Found invalid callchains!\n\n"
871 "%u out of %u events were discarded for this reason.\n\n"
872 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
873 session->hists.stats.nr_invalid_chains,
874 session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
875 }
876}
877
Tom Zanussi8dc58102010-04-01 23:59:15 -0500878#define session_done() (*(volatile int *)(&session_done))
879volatile int session_done;
880
881static int __perf_session__process_pipe_events(struct perf_session *self,
882 struct perf_event_ops *ops)
883{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200884 union perf_event event;
Tom Zanussi8dc58102010-04-01 23:59:15 -0500885 uint32_t size;
886 int skip = 0;
887 u64 head;
888 int err;
889 void *p;
890
891 perf_event_ops__fill_defaults(ops);
892
893 head = 0;
894more:
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200895 err = readn(self->fd, &event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -0500896 if (err <= 0) {
897 if (err == 0)
898 goto done;
899
900 pr_err("failed to read event header\n");
901 goto out_err;
902 }
903
904 if (self->header.needs_swap)
905 perf_event_header__bswap(&event.header);
906
907 size = event.header.size;
908 if (size == 0)
909 size = 8;
910
911 p = &event;
912 p += sizeof(struct perf_event_header);
913
Tom Zanussi794e43b2010-05-05 00:27:40 -0500914 if (size - sizeof(struct perf_event_header)) {
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200915 err = readn(self->fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -0500916 if (err <= 0) {
917 if (err == 0) {
918 pr_err("unexpected end of event stream\n");
919 goto done;
920 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500921
Tom Zanussi794e43b2010-05-05 00:27:40 -0500922 pr_err("failed to read event data\n");
923 goto out_err;
924 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500925 }
926
927 if (size == 0 ||
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000928 (skip = perf_session__process_event(self, &event, ops, head)) < 0) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200929 dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
Tom Zanussi8dc58102010-04-01 23:59:15 -0500930 head, event.header.size, event.header.type);
931 /*
932 * assume we lost track of the stream, check alignment, and
933 * increment a single u64 in the hope to catch on again 'soon'.
934 */
935 if (unlikely(head & 7))
936 head &= ~7ULL;
937
938 size = 8;
939 }
940
941 head += size;
942
Tom Zanussi8dc58102010-04-01 23:59:15 -0500943 if (skip > 0)
944 head += skip;
945
946 if (!session_done())
947 goto more;
948done:
949 err = 0;
950out_err:
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200951 perf_session__warn_about_errors(self, ops);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000952 perf_session_free_sample_buffers(self);
Tom Zanussi8dc58102010-04-01 23:59:15 -0500953 return err;
954}
955
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000956int __perf_session__process_events(struct perf_session *session,
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200957 u64 data_offset, u64 data_size,
958 u64 file_size, struct perf_event_ops *ops)
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200959{
Thomas Gleixner55b44622010-11-30 17:49:46 +0000960 u64 head, page_offset, file_offset, file_pos, progress_next;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000961 int err, mmap_prot, mmap_flags, map_idx = 0;
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000962 struct ui_progress *progress;
Thomas Gleixner55b44622010-11-30 17:49:46 +0000963 size_t page_size, mmap_size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000964 char *buf, *mmaps[8];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200965 union perf_event *event;
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200966 uint32_t size;
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000967
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200968 perf_event_ops__fill_defaults(ops);
969
Arnaldo Carvalho de Melo1b759622010-01-14 18:30:04 -0200970 page_size = sysconf(_SC_PAGESIZE);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200971
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000972 page_offset = page_size * (data_offset / page_size);
973 file_offset = page_offset;
974 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200975
Thomas Gleixnerd6513282010-11-30 17:49:44 +0000976 if (data_offset + data_size < file_size)
977 file_size = data_offset + data_size;
978
Thomas Gleixner55b44622010-11-30 17:49:46 +0000979 progress_next = file_size / 16;
980 progress = ui_progress__new("Processing events...", file_size);
981 if (progress == NULL)
982 return -1;
983
984 mmap_size = session->mmap_window;
985 if (mmap_size > file_size)
986 mmap_size = file_size;
987
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000988 memset(mmaps, 0, sizeof(mmaps));
989
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200990 mmap_prot = PROT_READ;
991 mmap_flags = MAP_SHARED;
992
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000993 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200994 mmap_prot |= PROT_WRITE;
995 mmap_flags = MAP_PRIVATE;
996 }
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200997remap:
Thomas Gleixner55b44622010-11-30 17:49:46 +0000998 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
999 file_offset);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001000 if (buf == MAP_FAILED) {
1001 pr_err("failed to mmap file\n");
1002 err = -errno;
1003 goto out_err;
1004 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001005 mmaps[map_idx] = buf;
1006 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001007 file_pos = file_offset + head;
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001008
1009more:
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001010 event = (union perf_event *)(buf + head);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001011
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001012 if (session->header.needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001013 perf_event_header__bswap(&event->header);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001014 size = event->header.size;
1015 if (size == 0)
1016 size = 8;
1017
Arnaldo Carvalho de Melo3d03e2e2011-01-10 21:37:57 -02001018 if (head + event->header.size > mmap_size) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001019 if (mmaps[map_idx]) {
1020 munmap(mmaps[map_idx], mmap_size);
1021 mmaps[map_idx] = NULL;
1022 }
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001023
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001024 page_offset = page_size * (head / page_size);
1025 file_offset += page_offset;
1026 head -= page_offset;
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001027 goto remap;
1028 }
1029
1030 size = event->header.size;
1031
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001032 if (size == 0 ||
1033 perf_session__process_event(session, event, ops, file_pos) < 0) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001034 dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001035 file_offset + head, event->header.size,
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001036 event->header.type);
1037 /*
1038 * assume we lost track of the stream, check alignment, and
1039 * increment a single u64 in the hope to catch on again 'soon'.
1040 */
1041 if (unlikely(head & 7))
1042 head &= ~7ULL;
1043
1044 size = 8;
1045 }
1046
1047 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001048 file_pos += size;
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001049
Thomas Gleixner55b44622010-11-30 17:49:46 +00001050 if (file_pos >= progress_next) {
1051 progress_next += file_size / 16;
1052 ui_progress__update(progress, file_pos);
1053 }
1054
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001055 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001056 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001057
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001058 err = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001059 /* do the final flush for ordered samples */
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001060 session->ordered_samples.next_flush = ULLONG_MAX;
1061 flush_sample_queue(session, ops);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001062out_err:
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -03001063 ui_progress__delete(progress);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001064 perf_session__warn_about_errors(session, ops);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001065 perf_session_free_sample_buffers(session);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -02001066 return err;
1067}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001068
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001069int perf_session__process_events(struct perf_session *self,
1070 struct perf_event_ops *ops)
1071{
1072 int err;
1073
1074 if (perf_session__register_idle_thread(self) == NULL)
1075 return -ENOMEM;
1076
Tom Zanussi8dc58102010-04-01 23:59:15 -05001077 if (!self->fd_pipe)
1078 err = __perf_session__process_events(self,
1079 self->header.data_offset,
1080 self->header.data_size,
1081 self->size, ops);
1082 else
1083 err = __perf_session__process_pipe_events(self, ops);
Dave Martin88ca8952010-07-27 11:46:12 -03001084
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001085 return err;
1086}
1087
Arnaldo Carvalho de Melod549c762009-12-27 21:37:02 -02001088bool perf_session__has_traces(struct perf_session *self, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001089{
1090 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
Arnaldo Carvalho de Melod549c762009-12-27 21:37:02 -02001091 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1092 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001093 }
1094
Arnaldo Carvalho de Melod549c762009-12-27 21:37:02 -02001095 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001096}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001097
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001098int perf_session__set_kallsyms_ref_reloc_sym(struct map **maps,
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001099 const char *symbol_name,
1100 u64 addr)
1101{
1102 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001103 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001104 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001105
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001106 ref = zalloc(sizeof(struct ref_reloc_sym));
1107 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001108 return -ENOMEM;
1109
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001110 ref->name = strdup(symbol_name);
1111 if (ref->name == NULL) {
1112 free(ref);
1113 return -ENOMEM;
1114 }
1115
1116 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001117 if (bracket)
1118 *bracket = '\0';
1119
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001120 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001121
1122 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001123 struct kmap *kmap = map__kmap(maps[i]);
1124 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001125 }
1126
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001127 return 0;
1128}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001129
1130size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
1131{
1132 return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
1133 __dsos__fprintf(&self->host_machine.user_dsos, fp) +
1134 machines__fprintf_dsos(&self->machines, fp);
1135}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001136
1137size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
1138 bool with_hits)
1139{
1140 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
1141 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
1142}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001143
1144size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1145{
1146 struct perf_evsel *pos;
1147 size_t ret = fprintf(fp, "Aggregated stats:\n");
1148
1149 ret += hists__fprintf_nr_events(&session->hists, fp);
1150
1151 list_for_each_entry(pos, &session->evlist->entries, node) {
1152 ret += fprintf(fp, "%s stats:\n", event_name(pos));
1153 ret += hists__fprintf_nr_events(&pos->hists, fp);
1154 }
1155
1156 return ret;
1157}
David Ahernc0230b22011-03-09 22:23:27 -07001158
1159void perf_session__print_symbols(union perf_event *event,
1160 struct perf_sample *sample,
1161 struct perf_session *session)
1162{
1163 struct addr_location al;
1164 const char *symname, *dsoname;
1165 struct callchain_cursor *cursor = &session->callchain_cursor;
1166 struct callchain_cursor_node *node;
1167
1168 if (perf_event__preprocess_sample(event, session, &al, sample,
1169 NULL) < 0) {
1170 error("problem processing %d event, skipping it.\n",
1171 event->header.type);
1172 return;
1173 }
1174
1175 if (symbol_conf.use_callchain && sample->callchain) {
1176
1177 if (perf_session__resolve_callchain(session, al.thread,
1178 sample->callchain, NULL) != 0) {
1179 if (verbose)
1180 error("Failed to resolve callchain. Skipping\n");
1181 return;
1182 }
1183 callchain_cursor_commit(cursor);
1184
1185 while (1) {
1186 node = callchain_cursor_current(cursor);
1187 if (!node)
1188 break;
1189
1190 if (node->sym && node->sym->name)
1191 symname = node->sym->name;
1192 else
1193 symname = "";
1194
1195 if (node->map && node->map->dso && node->map->dso->name)
1196 dsoname = node->map->dso->name;
1197 else
1198 dsoname = "";
1199
1200 printf("\t%16" PRIx64 " %s (%s)\n", node->ip, symname, dsoname);
1201
1202 callchain_cursor_advance(cursor);
1203 }
1204
1205 } else {
1206 if (al.sym && al.sym->name)
1207 symname = al.sym->name;
1208 else
1209 symname = "";
1210
1211 if (al.map && al.map->dso && al.map->dso->name)
1212 dsoname = al.map->dso->name;
1213 else
1214 dsoname = "";
1215
1216 printf("%16" PRIx64 " %s (%s)", al.addr, symname, dsoname);
1217 }
1218}