blob: f5a8fbdd3f76b51f9b5abf0bd551c964c3d301ed [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
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030061 if (!perf_evlist__valid_sample_type(self->evlist)) {
62 pr_err("non matching sample_type");
63 goto out_close;
64 }
65
66 if (!perf_evlist__valid_sample_id_all(self->evlist)) {
67 pr_err("non matching sample_id_all");
68 goto out_close;
69 }
70
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020071 self->size = input_stat.st_size;
72 return 0;
73
74out_close:
75 close(self->fd);
76 self->fd = -1;
77 return -1;
78}
79
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020080static void perf_session__id_header_size(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -050081{
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -020082 struct perf_sample *data;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020083 u64 sample_type = session->sample_type;
84 u16 size = 0;
85
86 if (!session->sample_id_all)
87 goto out;
88
89 if (sample_type & PERF_SAMPLE_TID)
90 size += sizeof(data->tid) * 2;
91
92 if (sample_type & PERF_SAMPLE_TIME)
93 size += sizeof(data->time);
94
95 if (sample_type & PERF_SAMPLE_ID)
96 size += sizeof(data->id);
97
98 if (sample_type & PERF_SAMPLE_STREAM_ID)
99 size += sizeof(data->stream_id);
100
101 if (sample_type & PERF_SAMPLE_CPU)
102 size += sizeof(data->cpu) * 2;
103out:
104 session->id_hdr_size = size;
105}
106
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200107void perf_session__update_sample_type(struct perf_session *self)
108{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300109 self->sample_type = perf_evlist__sample_type(self->evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300110 self->sample_size = __perf_evsel__sample_size(self->sample_type);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300111 self->sample_id_all = perf_evlist__sample_id_all(self->evlist);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200112 perf_session__id_header_size(self);
113}
114
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800115int perf_session__create_kernel_maps(struct perf_session *self)
116{
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -0300117 int ret = machine__create_kernel_maps(&self->host_machine);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800118
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800119 if (ret >= 0)
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -0300120 ret = machines__create_guest_kernel_maps(&self->machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800121 return ret;
122}
123
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300124static void perf_session__destroy_kernel_maps(struct perf_session *self)
125{
126 machine__destroy_kernel_maps(&self->host_machine);
127 machines__destroy_guest_kernel_maps(&self->machines);
128}
129
Ian Munsie21ef97f2010-12-10 14:09:16 +1100130struct perf_session *perf_session__new(const char *filename, int mode,
131 bool force, bool repipe,
132 struct perf_event_ops *ops)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200133{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200134 size_t len = filename ? strlen(filename) + 1 : 0;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200135 struct perf_session *self = zalloc(sizeof(*self) + len);
136
137 if (self == NULL)
138 goto out;
139
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200140 memcpy(self->filename, filename, len);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200141 self->threads = RB_ROOT;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300142 INIT_LIST_HEAD(&self->dead_threads);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200143 self->last_match = NULL;
Thomas Gleixner55b446292010-11-30 17:49:46 +0000144 /*
145 * On 64bit we can mmap the data file in one go. No need for tiny mmap
146 * slices. On 32bit we use 32MB.
147 */
148#if BITS_PER_LONG == 64
149 self->mmap_window = ULLONG_MAX;
150#else
151 self->mmap_window = 32 * 1024 * 1024ULL;
152#endif
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300153 self->machines = RB_ROOT;
Tom Zanussi454c4072010-05-01 01:41:20 -0500154 self->repipe = repipe;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000155 INIT_LIST_HEAD(&self->ordered_samples.samples);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000156 INIT_LIST_HEAD(&self->ordered_samples.sample_cache);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000157 INIT_LIST_HEAD(&self->ordered_samples.to_free);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300158 machine__init(&self->host_machine, "", HOST_KERNEL_ID);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200159
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200160 if (mode == O_RDONLY) {
161 if (perf_session__open(self, force) < 0)
162 goto out_delete;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300163 perf_session__update_sample_type(self);
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200164 } else if (mode == O_WRONLY) {
165 /*
166 * In O_RDONLY mode this will be performed when reading the
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200167 * kernel MMAP event, in perf_event__process_mmap().
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200168 */
169 if (perf_session__create_kernel_maps(self) < 0)
170 goto out_delete;
171 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200172
Ian Munsie21ef97f2010-12-10 14:09:16 +1100173 if (ops && ops->ordering_requires_timestamps &&
174 ops->ordered_samples && !self->sample_id_all) {
175 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
176 ops->ordered_samples = false;
177 }
178
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200179out:
180 return self;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200181out_delete:
182 perf_session__delete(self);
183 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200184}
185
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300186static void perf_session__delete_dead_threads(struct perf_session *self)
187{
188 struct thread *n, *t;
189
190 list_for_each_entry_safe(t, n, &self->dead_threads, node) {
191 list_del(&t->node);
192 thread__delete(t);
193 }
194}
195
196static void perf_session__delete_threads(struct perf_session *self)
197{
198 struct rb_node *nd = rb_first(&self->threads);
199
200 while (nd) {
201 struct thread *t = rb_entry(nd, struct thread, rb_node);
202
203 rb_erase(&t->rb_node, &self->threads);
204 nd = rb_next(nd);
205 thread__delete(t);
206 }
207}
208
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200209void perf_session__delete(struct perf_session *self)
210{
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300211 perf_session__destroy_kernel_maps(self);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300212 perf_session__delete_dead_threads(self);
213 perf_session__delete_threads(self);
214 machine__exit(&self->host_machine);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200215 close(self->fd);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200216 free(self);
217}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200218
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300219void perf_session__remove_thread(struct perf_session *self, struct thread *th)
220{
Arnaldo Carvalho de Melo70597f22010-08-02 18:59:28 -0300221 self->last_match = NULL;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300222 rb_erase(&th->rb_node, &self->threads);
223 /*
224 * We may have references to this thread, for instance in some hist_entry
225 * instances, so just move them to a separate list.
226 */
227 list_add_tail(&th->node, &self->dead_threads);
228}
229
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200230static bool symbol__match_parent_regex(struct symbol *sym)
231{
232 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
233 return 1;
234
235 return 0;
236}
237
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100238int perf_session__resolve_callchain(struct perf_session *self,
239 struct thread *thread,
240 struct ip_callchain *chain,
241 struct symbol **parent)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200242{
243 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200244 unsigned int i;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100245 int err;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200246
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100247 callchain_cursor_reset(&self->callchain_cursor);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200248
249 for (i = 0; i < chain->nr; i++) {
250 u64 ip = chain->ips[i];
251 struct addr_location al;
252
253 if (ip >= PERF_CONTEXT_MAX) {
254 switch (ip) {
255 case PERF_CONTEXT_HV:
256 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
257 case PERF_CONTEXT_KERNEL:
258 cpumode = PERF_RECORD_MISC_KERNEL; break;
259 case PERF_CONTEXT_USER:
260 cpumode = PERF_RECORD_MISC_USER; break;
261 default:
262 break;
263 }
264 continue;
265 }
266
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800267 al.filtered = false;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200268 thread__find_addr_location(thread, self, cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800269 MAP__FUNCTION, thread->pid, ip, &al, NULL);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200270 if (al.sym != NULL) {
271 if (sort__has_parent && !*parent &&
272 symbol__match_parent_regex(al.sym))
273 *parent = al.sym;
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200274 if (!symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200275 break;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200276 }
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100277
278 err = callchain_cursor_append(&self->callchain_cursor,
279 ip, al.map, al.sym);
280 if (err)
281 return err;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200282 }
283
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100284 return 0;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200285}
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200286
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200287static int process_event_synth_stub(union perf_event *event __used,
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200288 struct perf_session *session __used)
289{
290 dump_printf(": unhandled!\n");
291 return 0;
292}
293
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300294static int process_event_sample_stub(union perf_event *event __used,
295 struct perf_sample *sample __used,
296 struct perf_evsel *evsel __used,
297 struct perf_session *session __used)
298{
299 dump_printf(": unhandled!\n");
300 return 0;
301}
302
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200303static int process_event_stub(union perf_event *event __used,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200304 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200305 struct perf_session *session __used)
306{
307 dump_printf(": unhandled!\n");
308 return 0;
309}
310
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200311static int process_finished_round_stub(union perf_event *event __used,
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200312 struct perf_session *session __used,
313 struct perf_event_ops *ops __used)
314{
315 dump_printf(": unhandled!\n");
316 return 0;
317}
318
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200319static int process_finished_round(union perf_event *event,
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200320 struct perf_session *session,
321 struct perf_event_ops *ops);
322
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200323static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
324{
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200325 if (handler->sample == NULL)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300326 handler->sample = process_event_sample_stub;
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200327 if (handler->mmap == NULL)
328 handler->mmap = process_event_stub;
329 if (handler->comm == NULL)
330 handler->comm = process_event_stub;
331 if (handler->fork == NULL)
332 handler->fork = process_event_stub;
333 if (handler->exit == NULL)
334 handler->exit = process_event_stub;
335 if (handler->lost == NULL)
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200336 handler->lost = perf_event__process_lost;
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200337 if (handler->read == NULL)
338 handler->read = process_event_stub;
339 if (handler->throttle == NULL)
340 handler->throttle = process_event_stub;
341 if (handler->unthrottle == NULL)
342 handler->unthrottle = process_event_stub;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500343 if (handler->attr == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200344 handler->attr = process_event_synth_stub;
Tom Zanussicd19a032010-04-01 23:59:20 -0500345 if (handler->event_type == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200346 handler->event_type = process_event_synth_stub;
Tom Zanussi92155452010-04-01 23:59:21 -0500347 if (handler->tracing_data == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200348 handler->tracing_data = process_event_synth_stub;
Tom Zanussic7929e42010-04-01 23:59:22 -0500349 if (handler->build_id == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200350 handler->build_id = process_event_synth_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200351 if (handler->finished_round == NULL) {
352 if (handler->ordered_samples)
353 handler->finished_round = process_finished_round;
354 else
355 handler->finished_round = process_finished_round_stub;
356 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200357}
358
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200359void mem_bswap_64(void *src, int byte_size)
360{
361 u64 *m = src;
362
363 while (byte_size > 0) {
364 *m = bswap_64(*m);
365 byte_size -= sizeof(u64);
366 ++m;
367 }
368}
369
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200370static void perf_event__all64_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200371{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200372 struct perf_event_header *hdr = &event->header;
373 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200374}
375
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200376static void perf_event__comm_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200377{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200378 event->comm.pid = bswap_32(event->comm.pid);
379 event->comm.tid = bswap_32(event->comm.tid);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200380}
381
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200382static void perf_event__mmap_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200383{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200384 event->mmap.pid = bswap_32(event->mmap.pid);
385 event->mmap.tid = bswap_32(event->mmap.tid);
386 event->mmap.start = bswap_64(event->mmap.start);
387 event->mmap.len = bswap_64(event->mmap.len);
388 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200389}
390
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200391static void perf_event__task_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200392{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200393 event->fork.pid = bswap_32(event->fork.pid);
394 event->fork.tid = bswap_32(event->fork.tid);
395 event->fork.ppid = bswap_32(event->fork.ppid);
396 event->fork.ptid = bswap_32(event->fork.ptid);
397 event->fork.time = bswap_64(event->fork.time);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200398}
399
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200400static void perf_event__read_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200401{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200402 event->read.pid = bswap_32(event->read.pid);
403 event->read.tid = bswap_32(event->read.tid);
404 event->read.value = bswap_64(event->read.value);
405 event->read.time_enabled = bswap_64(event->read.time_enabled);
406 event->read.time_running = bswap_64(event->read.time_running);
407 event->read.id = bswap_64(event->read.id);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200408}
409
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200410static void perf_event__attr_swap(union perf_event *event)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500411{
412 size_t size;
413
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200414 event->attr.attr.type = bswap_32(event->attr.attr.type);
415 event->attr.attr.size = bswap_32(event->attr.attr.size);
416 event->attr.attr.config = bswap_64(event->attr.attr.config);
417 event->attr.attr.sample_period = bswap_64(event->attr.attr.sample_period);
418 event->attr.attr.sample_type = bswap_64(event->attr.attr.sample_type);
419 event->attr.attr.read_format = bswap_64(event->attr.attr.read_format);
420 event->attr.attr.wakeup_events = bswap_32(event->attr.attr.wakeup_events);
421 event->attr.attr.bp_type = bswap_32(event->attr.attr.bp_type);
422 event->attr.attr.bp_addr = bswap_64(event->attr.attr.bp_addr);
423 event->attr.attr.bp_len = bswap_64(event->attr.attr.bp_len);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500424
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200425 size = event->header.size;
426 size -= (void *)&event->attr.id - (void *)event;
427 mem_bswap_64(event->attr.id, size);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500428}
429
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200430static void perf_event__event_type_swap(union perf_event *event)
Tom Zanussicd19a032010-04-01 23:59:20 -0500431{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200432 event->event_type.event_type.event_id =
433 bswap_64(event->event_type.event_type.event_id);
Tom Zanussicd19a032010-04-01 23:59:20 -0500434}
435
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200436static void perf_event__tracing_data_swap(union perf_event *event)
Tom Zanussi92155452010-04-01 23:59:21 -0500437{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200438 event->tracing_data.size = bswap_32(event->tracing_data.size);
Tom Zanussi92155452010-04-01 23:59:21 -0500439}
440
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200441typedef void (*perf_event__swap_op)(union perf_event *event);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200442
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200443static perf_event__swap_op perf_event__swap_ops[] = {
444 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
445 [PERF_RECORD_COMM] = perf_event__comm_swap,
446 [PERF_RECORD_FORK] = perf_event__task_swap,
447 [PERF_RECORD_EXIT] = perf_event__task_swap,
448 [PERF_RECORD_LOST] = perf_event__all64_swap,
449 [PERF_RECORD_READ] = perf_event__read_swap,
450 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
451 [PERF_RECORD_HEADER_ATTR] = perf_event__attr_swap,
452 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
453 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
454 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
455 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200456};
457
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200458struct sample_queue {
459 u64 timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000460 u64 file_offset;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200461 union perf_event *event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200462 struct list_head list;
463};
464
Thomas Gleixner020bb752010-11-30 17:49:53 +0000465static void perf_session_free_sample_buffers(struct perf_session *session)
466{
467 struct ordered_samples *os = &session->ordered_samples;
468
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000469 while (!list_empty(&os->to_free)) {
Thomas Gleixner020bb752010-11-30 17:49:53 +0000470 struct sample_queue *sq;
471
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000472 sq = list_entry(os->to_free.next, struct sample_queue, list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000473 list_del(&sq->list);
474 free(sq);
475 }
476}
477
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100478static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200479 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200480 struct perf_sample *sample,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000481 struct perf_event_ops *ops,
482 u64 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100483
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200484static void flush_sample_queue(struct perf_session *s,
485 struct perf_event_ops *ops)
486{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000487 struct ordered_samples *os = &s->ordered_samples;
488 struct list_head *head = &os->samples;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200489 struct sample_queue *tmp, *iter;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200490 struct perf_sample sample;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000491 u64 limit = os->next_flush;
492 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200493 int ret;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200494
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200495 if (!ops->ordered_samples || !limit)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200496 return;
497
498 list_for_each_entry_safe(iter, tmp, head, list) {
499 if (iter->timestamp > limit)
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000500 break;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200501
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200502 ret = perf_session__parse_sample(s, iter->event, &sample);
503 if (ret)
504 pr_err("Can't parse sample, err = %d\n", ret);
505 else
506 perf_session_deliver_event(s, iter->event, &sample, ops,
507 iter->file_offset);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200508
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000509 os->last_flush = iter->timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200510 list_del(&iter->list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000511 list_add(&iter->list, &os->sample_cache);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200512 }
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000513
514 if (list_empty(head)) {
515 os->last_sample = NULL;
516 } else if (last_ts <= limit) {
517 os->last_sample =
518 list_entry(head->prev, struct sample_queue, list);
519 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200520}
521
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200522/*
523 * When perf record finishes a pass on every buffers, it records this pseudo
524 * event.
525 * We record the max timestamp t found in the pass n.
526 * Assuming these timestamps are monotonic across cpus, we know that if
527 * a buffer still has events with timestamps below t, they will be all
528 * available and then read in the pass n + 1.
529 * Hence when we start to read the pass n + 2, we can safely flush every
530 * events with timestamps below t.
531 *
532 * ============ PASS n =================
533 * CPU 0 | CPU 1
534 * |
535 * cnt1 timestamps | cnt2 timestamps
536 * 1 | 2
537 * 2 | 3
538 * - | 4 <--- max recorded
539 *
540 * ============ PASS n + 1 ==============
541 * CPU 0 | CPU 1
542 * |
543 * cnt1 timestamps | cnt2 timestamps
544 * 3 | 5
545 * 4 | 6
546 * 5 | 7 <---- max recorded
547 *
548 * Flush every events below timestamp 4
549 *
550 * ============ PASS n + 2 ==============
551 * CPU 0 | CPU 1
552 * |
553 * cnt1 timestamps | cnt2 timestamps
554 * 6 | 8
555 * 7 | 9
556 * - | 10
557 *
558 * Flush every events below timestamp 7
559 * etc...
560 */
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200561static int process_finished_round(union perf_event *event __used,
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200562 struct perf_session *session,
563 struct perf_event_ops *ops)
564{
565 flush_sample_queue(session, ops);
566 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
567
568 return 0;
569}
570
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200571/* The queue is ordered by time */
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100572static void __queue_event(struct sample_queue *new, struct perf_session *s)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200573{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000574 struct ordered_samples *os = &s->ordered_samples;
575 struct sample_queue *sample = os->last_sample;
576 u64 timestamp = new->timestamp;
577 struct list_head *p;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200578
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000579 os->last_sample = new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200580
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000581 if (!sample) {
582 list_add(&new->list, &os->samples);
583 os->max_timestamp = timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200584 return;
585 }
586
587 /*
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000588 * last_sample might point to some random place in the list as it's
589 * the last queued event. We expect that the new event is close to
590 * this.
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200591 */
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000592 if (sample->timestamp <= timestamp) {
593 while (sample->timestamp <= timestamp) {
594 p = sample->list.next;
595 if (p == &os->samples) {
596 list_add_tail(&new->list, &os->samples);
597 os->max_timestamp = timestamp;
598 return;
599 }
600 sample = list_entry(p, struct sample_queue, list);
601 }
602 list_add_tail(&new->list, &sample->list);
603 } else {
604 while (sample->timestamp > timestamp) {
605 p = sample->list.prev;
606 if (p == &os->samples) {
607 list_add(&new->list, &os->samples);
608 return;
609 }
610 sample = list_entry(p, struct sample_queue, list);
611 }
612 list_add(&new->list, &sample->list);
613 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200614}
615
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000616#define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
617
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200618static int perf_session_queue_event(struct perf_session *s, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200619 struct perf_sample *sample, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200620{
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000621 struct ordered_samples *os = &s->ordered_samples;
622 struct list_head *sc = &os->sample_cache;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200623 u64 timestamp = sample->time;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200624 struct sample_queue *new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200625
Thomas Gleixner79a14c12010-12-07 12:48:44 +0000626 if (!timestamp || timestamp == ~0ULL)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100627 return -ETIME;
628
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200629 if (timestamp < s->ordered_samples.last_flush) {
630 printf("Warning: Timestamp below last timeslice flush\n");
631 return -EINVAL;
632 }
633
Thomas Gleixner020bb752010-11-30 17:49:53 +0000634 if (!list_empty(sc)) {
635 new = list_entry(sc->next, struct sample_queue, list);
636 list_del(&new->list);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000637 } else if (os->sample_buffer) {
638 new = os->sample_buffer + os->sample_buffer_idx;
639 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
640 os->sample_buffer = NULL;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000641 } else {
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000642 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
643 if (!os->sample_buffer)
Thomas Gleixner020bb752010-11-30 17:49:53 +0000644 return -ENOMEM;
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000645 list_add(&os->sample_buffer->list, &os->to_free);
646 os->sample_buffer_idx = 2;
647 new = os->sample_buffer + 1;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000648 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200649
650 new->timestamp = timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000651 new->file_offset = file_offset;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000652 new->event = event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200653
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100654 __queue_event(new, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200655
656 return 0;
657}
658
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200659static void callchain__printf(struct perf_sample *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200660{
661 unsigned int i;
662
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200663 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200664
665 for (i = 0; i < sample->callchain->nr; i++)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200666 printf("..... %2d: %016" PRIx64 "\n",
667 i, sample->callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200668}
669
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200670static void perf_session__print_tstamp(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200671 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200672 struct perf_sample *sample)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200673{
674 if (event->header.type != PERF_RECORD_SAMPLE &&
675 !session->sample_id_all) {
676 fputs("-1 -1 ", stdout);
677 return;
678 }
679
680 if ((session->sample_type & PERF_SAMPLE_CPU))
681 printf("%u ", sample->cpu);
682
683 if (session->sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200684 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200685}
686
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200687static void dump_event(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200688 u64 file_offset, struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000689{
690 if (!dump_trace)
691 return;
692
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200693 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
694 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000695
696 trace_event(event);
697
698 if (sample)
699 perf_session__print_tstamp(session, event, sample);
700
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200701 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200702 event->header.size, perf_event__name(event->header.type));
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000703}
704
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200705static void dump_sample(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200706 struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000707{
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200708 if (!dump_trace)
709 return;
710
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200711 printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 "\n",
712 event->header.misc, sample->pid, sample->tid, sample->ip,
713 sample->period);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000714
715 if (session->sample_type & PERF_SAMPLE_CALLCHAIN)
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200716 callchain__printf(sample);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000717}
718
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100719static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200720 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200721 struct perf_sample *sample,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000722 struct perf_event_ops *ops,
Thomas Gleixner532e7262010-12-07 12:48:55 +0000723 u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100724{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300725 struct perf_evsel *evsel;
726
Thomas Gleixner532e7262010-12-07 12:48:55 +0000727 dump_event(session, event, file_offset, sample);
728
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100729 switch (event->header.type) {
730 case PERF_RECORD_SAMPLE:
Thomas Gleixner532e7262010-12-07 12:48:55 +0000731 dump_sample(session, event, sample);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300732 evsel = perf_evlist__id2evsel(session->evlist, sample->id);
733 if (evsel == NULL) {
734 ++session->hists.stats.nr_unknown_id;
735 return -1;
736 }
737 return ops->sample(event, sample, evsel, session);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100738 case PERF_RECORD_MMAP:
739 return ops->mmap(event, sample, session);
740 case PERF_RECORD_COMM:
741 return ops->comm(event, sample, session);
742 case PERF_RECORD_FORK:
743 return ops->fork(event, sample, session);
744 case PERF_RECORD_EXIT:
745 return ops->exit(event, sample, session);
746 case PERF_RECORD_LOST:
747 return ops->lost(event, sample, session);
748 case PERF_RECORD_READ:
749 return ops->read(event, sample, session);
750 case PERF_RECORD_THROTTLE:
751 return ops->throttle(event, sample, session);
752 case PERF_RECORD_UNTHROTTLE:
753 return ops->unthrottle(event, sample, session);
754 default:
755 ++session->hists.stats.nr_unknown_events;
756 return -1;
757 }
758}
759
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000760static int perf_session__preprocess_sample(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200761 union perf_event *event, struct perf_sample *sample)
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000762{
763 if (event->header.type != PERF_RECORD_SAMPLE ||
764 !(session->sample_type & PERF_SAMPLE_CALLCHAIN))
765 return 0;
766
767 if (!ip_callchain__valid(sample->callchain, event)) {
768 pr_debug("call-chain problem with event, skipping it.\n");
769 ++session->hists.stats.nr_invalid_chains;
770 session->hists.stats.total_invalid_chains += sample->period;
771 return -EINVAL;
772 }
773 return 0;
774}
775
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200776static int perf_session__process_user_event(struct perf_session *session, union perf_event *event,
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000777 struct perf_event_ops *ops, u64 file_offset)
778{
779 dump_event(session, event, file_offset, NULL);
780
781 /* These events are processed right away */
782 switch (event->header.type) {
783 case PERF_RECORD_HEADER_ATTR:
784 return ops->attr(event, session);
785 case PERF_RECORD_HEADER_EVENT_TYPE:
786 return ops->event_type(event, session);
787 case PERF_RECORD_HEADER_TRACING_DATA:
788 /* setup for reading amidst mmap */
789 lseek(session->fd, file_offset, SEEK_SET);
790 return ops->tracing_data(event, session);
791 case PERF_RECORD_HEADER_BUILD_ID:
792 return ops->build_id(event, session);
793 case PERF_RECORD_FINISHED_ROUND:
794 return ops->finished_round(event, session, ops);
795 default:
796 return -EINVAL;
797 }
798}
799
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100800static int perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200801 union perf_event *event,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200802 struct perf_event_ops *ops,
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000803 u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200804{
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200805 struct perf_sample sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100806 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200807
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200808 if (session->header.needs_swap &&
809 perf_event__swap_ops[event->header.type])
810 perf_event__swap_ops[event->header.type](event);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200811
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000812 if (event->header.type >= PERF_RECORD_HEADER_MAX)
813 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200814
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000815 hists__inc_nr_events(&session->hists, event->header.type);
816
817 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000818 return perf_session__process_user_event(session, event, ops, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100819
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000820 /*
821 * For all kernel events we get the sample data
822 */
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200823 ret = perf_session__parse_sample(session, event, &sample);
824 if (ret)
825 return ret;
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000826
827 /* Preprocess sample records - precheck callchains */
828 if (perf_session__preprocess_sample(session, event, &sample))
829 return 0;
830
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100831 if (ops->ordered_samples) {
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000832 ret = perf_session_queue_event(session, event, &sample,
833 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100834 if (ret != -ETIME)
835 return ret;
836 }
837
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000838 return perf_session_deliver_event(session, event, &sample, ops,
839 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200840}
841
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200842void perf_event_header__bswap(struct perf_event_header *self)
843{
844 self->type = bswap_32(self->type);
845 self->misc = bswap_16(self->misc);
846 self->size = bswap_16(self->size);
847}
848
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200849static struct thread *perf_session__register_idle_thread(struct perf_session *self)
850{
851 struct thread *thread = perf_session__findnew(self, 0);
852
853 if (thread == NULL || thread__set_comm(thread, "swapper")) {
854 pr_err("problem inserting idle task.\n");
855 thread = NULL;
856 }
857
858 return thread;
859}
860
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200861static void perf_session__warn_about_errors(const struct perf_session *session,
862 const struct perf_event_ops *ops)
863{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200864 if (ops->lost == perf_event__process_lost &&
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200865 session->hists.stats.total_lost != 0) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200866 ui__warning("Processed %" PRIu64 " events and LOST %" PRIu64
867 "!\n\nCheck IO/CPU overload!\n\n",
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200868 session->hists.stats.total_period,
869 session->hists.stats.total_lost);
870 }
871
872 if (session->hists.stats.nr_unknown_events != 0) {
873 ui__warning("Found %u unknown events!\n\n"
874 "Is this an older tool processing a perf.data "
875 "file generated by a more recent tool?\n\n"
876 "If that is not the case, consider "
877 "reporting to linux-kernel@vger.kernel.org.\n\n",
878 session->hists.stats.nr_unknown_events);
879 }
880
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300881 if (session->hists.stats.nr_unknown_id != 0) {
882 ui__warning("%u samples with id not present in the header\n",
883 session->hists.stats.nr_unknown_id);
884 }
885
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200886 if (session->hists.stats.nr_invalid_chains != 0) {
887 ui__warning("Found invalid callchains!\n\n"
888 "%u out of %u events were discarded for this reason.\n\n"
889 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
890 session->hists.stats.nr_invalid_chains,
891 session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
892 }
893}
894
Tom Zanussi8dc58102010-04-01 23:59:15 -0500895#define session_done() (*(volatile int *)(&session_done))
896volatile int session_done;
897
898static int __perf_session__process_pipe_events(struct perf_session *self,
899 struct perf_event_ops *ops)
900{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200901 union perf_event event;
Tom Zanussi8dc58102010-04-01 23:59:15 -0500902 uint32_t size;
903 int skip = 0;
904 u64 head;
905 int err;
906 void *p;
907
908 perf_event_ops__fill_defaults(ops);
909
910 head = 0;
911more:
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200912 err = readn(self->fd, &event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -0500913 if (err <= 0) {
914 if (err == 0)
915 goto done;
916
917 pr_err("failed to read event header\n");
918 goto out_err;
919 }
920
921 if (self->header.needs_swap)
922 perf_event_header__bswap(&event.header);
923
924 size = event.header.size;
925 if (size == 0)
926 size = 8;
927
928 p = &event;
929 p += sizeof(struct perf_event_header);
930
Tom Zanussi794e43b2010-05-05 00:27:40 -0500931 if (size - sizeof(struct perf_event_header)) {
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200932 err = readn(self->fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -0500933 if (err <= 0) {
934 if (err == 0) {
935 pr_err("unexpected end of event stream\n");
936 goto done;
937 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500938
Tom Zanussi794e43b2010-05-05 00:27:40 -0500939 pr_err("failed to read event data\n");
940 goto out_err;
941 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500942 }
943
944 if (size == 0 ||
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000945 (skip = perf_session__process_event(self, &event, ops, head)) < 0) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200946 dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
Tom Zanussi8dc58102010-04-01 23:59:15 -0500947 head, event.header.size, event.header.type);
948 /*
949 * assume we lost track of the stream, check alignment, and
950 * increment a single u64 in the hope to catch on again 'soon'.
951 */
952 if (unlikely(head & 7))
953 head &= ~7ULL;
954
955 size = 8;
956 }
957
958 head += size;
959
Tom Zanussi8dc58102010-04-01 23:59:15 -0500960 if (skip > 0)
961 head += skip;
962
963 if (!session_done())
964 goto more;
965done:
966 err = 0;
967out_err:
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200968 perf_session__warn_about_errors(self, ops);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000969 perf_session_free_sample_buffers(self);
Tom Zanussi8dc58102010-04-01 23:59:15 -0500970 return err;
971}
972
Frederic Weisbecker998bedc2011-05-23 13:06:28 +0200973static union perf_event *
974fetch_mmaped_event(struct perf_session *session,
975 u64 head, size_t mmap_size, char *buf)
976{
977 union perf_event *event;
978
979 /*
980 * Ensure we have enough space remaining to read
981 * the size of the event in the headers.
982 */
983 if (head + sizeof(event->header) > mmap_size)
984 return NULL;
985
986 event = (union perf_event *)(buf + head);
987
988 if (session->header.needs_swap)
989 perf_event_header__bswap(&event->header);
990
991 if (head + event->header.size > mmap_size)
992 return NULL;
993
994 return event;
995}
996
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000997int __perf_session__process_events(struct perf_session *session,
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200998 u64 data_offset, u64 data_size,
999 u64 file_size, struct perf_event_ops *ops)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001000{
Thomas Gleixner55b446292010-11-30 17:49:46 +00001001 u64 head, page_offset, file_offset, file_pos, progress_next;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001002 int err, mmap_prot, mmap_flags, map_idx = 0;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001003 struct ui_progress *progress;
Thomas Gleixner55b446292010-11-30 17:49:46 +00001004 size_t page_size, mmap_size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001005 char *buf, *mmaps[8];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001006 union perf_event *event;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001007 uint32_t size;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001008
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001009 perf_event_ops__fill_defaults(ops);
1010
Arnaldo Carvalho de Melo1b759622010-01-14 18:30:04 -02001011 page_size = sysconf(_SC_PAGESIZE);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001012
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001013 page_offset = page_size * (data_offset / page_size);
1014 file_offset = page_offset;
1015 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001016
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001017 if (data_offset + data_size < file_size)
1018 file_size = data_offset + data_size;
1019
Thomas Gleixner55b446292010-11-30 17:49:46 +00001020 progress_next = file_size / 16;
1021 progress = ui_progress__new("Processing events...", file_size);
1022 if (progress == NULL)
1023 return -1;
1024
1025 mmap_size = session->mmap_window;
1026 if (mmap_size > file_size)
1027 mmap_size = file_size;
1028
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001029 memset(mmaps, 0, sizeof(mmaps));
1030
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001031 mmap_prot = PROT_READ;
1032 mmap_flags = MAP_SHARED;
1033
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001034 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001035 mmap_prot |= PROT_WRITE;
1036 mmap_flags = MAP_PRIVATE;
1037 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001038remap:
Thomas Gleixner55b446292010-11-30 17:49:46 +00001039 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
1040 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001041 if (buf == MAP_FAILED) {
1042 pr_err("failed to mmap file\n");
1043 err = -errno;
1044 goto out_err;
1045 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001046 mmaps[map_idx] = buf;
1047 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001048 file_pos = file_offset + head;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001049
1050more:
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001051 event = fetch_mmaped_event(session, head, mmap_size, buf);
1052 if (!event) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001053 if (mmaps[map_idx]) {
1054 munmap(mmaps[map_idx], mmap_size);
1055 mmaps[map_idx] = NULL;
1056 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001057
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001058 page_offset = page_size * (head / page_size);
1059 file_offset += page_offset;
1060 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001061 goto remap;
1062 }
1063
1064 size = event->header.size;
1065
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001066 if (size == 0 ||
1067 perf_session__process_event(session, event, ops, file_pos) < 0) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001068 dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001069 file_offset + head, event->header.size,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001070 event->header.type);
1071 /*
1072 * assume we lost track of the stream, check alignment, and
1073 * increment a single u64 in the hope to catch on again 'soon'.
1074 */
1075 if (unlikely(head & 7))
1076 head &= ~7ULL;
1077
1078 size = 8;
1079 }
1080
1081 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001082 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001083
Thomas Gleixner55b446292010-11-30 17:49:46 +00001084 if (file_pos >= progress_next) {
1085 progress_next += file_size / 16;
1086 ui_progress__update(progress, file_pos);
1087 }
1088
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001089 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001090 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001091
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001092 err = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001093 /* do the final flush for ordered samples */
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001094 session->ordered_samples.next_flush = ULLONG_MAX;
1095 flush_sample_queue(session, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001096out_err:
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -03001097 ui_progress__delete(progress);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001098 perf_session__warn_about_errors(session, ops);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001099 perf_session_free_sample_buffers(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001100 return err;
1101}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001102
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001103int perf_session__process_events(struct perf_session *self,
1104 struct perf_event_ops *ops)
1105{
1106 int err;
1107
1108 if (perf_session__register_idle_thread(self) == NULL)
1109 return -ENOMEM;
1110
Tom Zanussi8dc58102010-04-01 23:59:15 -05001111 if (!self->fd_pipe)
1112 err = __perf_session__process_events(self,
1113 self->header.data_offset,
1114 self->header.data_size,
1115 self->size, ops);
1116 else
1117 err = __perf_session__process_pipe_events(self, ops);
Dave Martin88ca8952010-07-27 11:46:12 -03001118
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001119 return err;
1120}
1121
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001122bool perf_session__has_traces(struct perf_session *self, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001123{
1124 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001125 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1126 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001127 }
1128
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001129 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001130}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001131
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001132int perf_session__set_kallsyms_ref_reloc_sym(struct map **maps,
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001133 const char *symbol_name,
1134 u64 addr)
1135{
1136 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001137 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001138 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001139
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001140 ref = zalloc(sizeof(struct ref_reloc_sym));
1141 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001142 return -ENOMEM;
1143
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001144 ref->name = strdup(symbol_name);
1145 if (ref->name == NULL) {
1146 free(ref);
1147 return -ENOMEM;
1148 }
1149
1150 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001151 if (bracket)
1152 *bracket = '\0';
1153
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001154 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001155
1156 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001157 struct kmap *kmap = map__kmap(maps[i]);
1158 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001159 }
1160
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001161 return 0;
1162}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001163
1164size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
1165{
1166 return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
1167 __dsos__fprintf(&self->host_machine.user_dsos, fp) +
1168 machines__fprintf_dsos(&self->machines, fp);
1169}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001170
1171size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
1172 bool with_hits)
1173{
1174 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
1175 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
1176}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001177
1178size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1179{
1180 struct perf_evsel *pos;
1181 size_t ret = fprintf(fp, "Aggregated stats:\n");
1182
1183 ret += hists__fprintf_nr_events(&session->hists, fp);
1184
1185 list_for_each_entry(pos, &session->evlist->entries, node) {
1186 ret += fprintf(fp, "%s stats:\n", event_name(pos));
1187 ret += hists__fprintf_nr_events(&pos->hists, fp);
1188 }
1189
1190 return ret;
1191}
David Ahernc0230b22011-03-09 22:23:27 -07001192
David Ahern9cbdb702011-04-06 21:54:20 -06001193struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1194 unsigned int type)
1195{
1196 struct perf_evsel *pos;
1197
1198 list_for_each_entry(pos, &session->evlist->entries, node) {
1199 if (pos->attr.type == type)
1200 return pos;
1201 }
1202 return NULL;
1203}
1204
David Ahernc0230b22011-03-09 22:23:27 -07001205void perf_session__print_symbols(union perf_event *event,
1206 struct perf_sample *sample,
1207 struct perf_session *session)
1208{
1209 struct addr_location al;
1210 const char *symname, *dsoname;
1211 struct callchain_cursor *cursor = &session->callchain_cursor;
1212 struct callchain_cursor_node *node;
1213
1214 if (perf_event__preprocess_sample(event, session, &al, sample,
1215 NULL) < 0) {
1216 error("problem processing %d event, skipping it.\n",
1217 event->header.type);
1218 return;
1219 }
1220
1221 if (symbol_conf.use_callchain && sample->callchain) {
1222
1223 if (perf_session__resolve_callchain(session, al.thread,
1224 sample->callchain, NULL) != 0) {
1225 if (verbose)
1226 error("Failed to resolve callchain. Skipping\n");
1227 return;
1228 }
1229 callchain_cursor_commit(cursor);
1230
1231 while (1) {
1232 node = callchain_cursor_current(cursor);
1233 if (!node)
1234 break;
1235
1236 if (node->sym && node->sym->name)
1237 symname = node->sym->name;
1238 else
1239 symname = "";
1240
1241 if (node->map && node->map->dso && node->map->dso->name)
1242 dsoname = node->map->dso->name;
1243 else
1244 dsoname = "";
1245
1246 printf("\t%16" PRIx64 " %s (%s)\n", node->ip, symname, dsoname);
1247
1248 callchain_cursor_advance(cursor);
1249 }
1250
1251 } else {
1252 if (al.sym && al.sym->name)
1253 symname = al.sym->name;
1254 else
1255 symname = "";
1256
1257 if (al.map && al.map->dso && al.map->dso->name)
1258 dsoname = al.map->dso->name;
1259 else
1260 dsoname = "";
1261
1262 printf("%16" PRIx64 " %s (%s)", al.addr, symname, dsoname);
1263 }
1264}