blob: b58a48a5e5a9a7a29fea122fd2f4ca560acbd045 [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
10#include "session.h"
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -020011#include "sort.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020012#include "util.h"
13
14static int perf_session__open(struct perf_session *self, bool force)
15{
16 struct stat input_stat;
17
Tom Zanussi8dc58102010-04-01 23:59:15 -050018 if (!strcmp(self->filename, "-")) {
19 self->fd_pipe = true;
20 self->fd = STDIN_FILENO;
21
22 if (perf_header__read(self, self->fd) < 0)
23 pr_err("incompatible file format");
24
25 return 0;
26 }
27
Xiao Guangrongf887f302010-02-04 16:46:42 +080028 self->fd = open(self->filename, O_RDONLY);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020029 if (self->fd < 0) {
Andy Isaacson0f2c3de2010-06-11 20:36:15 -070030 int err = errno;
31
32 pr_err("failed to open %s: %s", self->filename, strerror(err));
33 if (err == ENOENT && !strcmp(self->filename, "perf.data"))
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020034 pr_err(" (try 'perf record' first)");
35 pr_err("\n");
36 return -errno;
37 }
38
39 if (fstat(self->fd, &input_stat) < 0)
40 goto out_close;
41
42 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
43 pr_err("file %s not owned by current user or root\n",
44 self->filename);
45 goto out_close;
46 }
47
48 if (!input_stat.st_size) {
49 pr_info("zero-sized file (%s), nothing to do!\n",
50 self->filename);
51 goto out_close;
52 }
53
Tom Zanussi8dc58102010-04-01 23:59:15 -050054 if (perf_header__read(self, self->fd) < 0) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020055 pr_err("incompatible file format");
56 goto out_close;
57 }
58
59 self->size = input_stat.st_size;
60 return 0;
61
62out_close:
63 close(self->fd);
64 self->fd = -1;
65 return -1;
66}
67
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020068static void perf_session__id_header_size(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -050069{
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020070 struct sample_data *data;
71 u64 sample_type = session->sample_type;
72 u16 size = 0;
73
74 if (!session->sample_id_all)
75 goto out;
76
77 if (sample_type & PERF_SAMPLE_TID)
78 size += sizeof(data->tid) * 2;
79
80 if (sample_type & PERF_SAMPLE_TIME)
81 size += sizeof(data->time);
82
83 if (sample_type & PERF_SAMPLE_ID)
84 size += sizeof(data->id);
85
86 if (sample_type & PERF_SAMPLE_STREAM_ID)
87 size += sizeof(data->stream_id);
88
89 if (sample_type & PERF_SAMPLE_CPU)
90 size += sizeof(data->cpu) * 2;
91out:
92 session->id_hdr_size = size;
93}
94
95void perf_session__set_sample_id_all(struct perf_session *session, bool value)
96{
97 session->sample_id_all = value;
98 perf_session__id_header_size(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -050099}
100
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200101void perf_session__set_sample_type(struct perf_session *session, u64 type)
102{
103 session->sample_type = type;
104}
105
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200106void perf_session__update_sample_type(struct perf_session *self)
107{
108 self->sample_type = perf_header__sample_type(&self->header);
109 self->sample_id_all = perf_header__sample_id_all(&self->header);
110 perf_session__id_header_size(self);
111}
112
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800113int perf_session__create_kernel_maps(struct perf_session *self)
114{
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -0300115 int ret = machine__create_kernel_maps(&self->host_machine);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800116
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800117 if (ret >= 0)
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -0300118 ret = machines__create_guest_kernel_maps(&self->machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800119 return ret;
120}
121
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300122static void perf_session__destroy_kernel_maps(struct perf_session *self)
123{
124 machine__destroy_kernel_maps(&self->host_machine);
125 machines__destroy_guest_kernel_maps(&self->machines);
126}
127
Ian Munsie21ef97f2010-12-10 14:09:16 +1100128struct perf_session *perf_session__new(const char *filename, int mode,
129 bool force, bool repipe,
130 struct perf_event_ops *ops)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200131{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200132 size_t len = filename ? strlen(filename) + 1 : 0;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200133 struct perf_session *self = zalloc(sizeof(*self) + len);
134
135 if (self == NULL)
136 goto out;
137
138 if (perf_header__init(&self->header) < 0)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200139 goto out_free;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200140
141 memcpy(self->filename, filename, len);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200142 self->threads = RB_ROOT;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300143 INIT_LIST_HEAD(&self->dead_threads);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300144 self->hists_tree = RB_ROOT;
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200145 self->last_match = NULL;
Thomas Gleixner55b446292010-11-30 17:49:46 +0000146 /*
147 * On 64bit we can mmap the data file in one go. No need for tiny mmap
148 * slices. On 32bit we use 32MB.
149 */
150#if BITS_PER_LONG == 64
151 self->mmap_window = ULLONG_MAX;
152#else
153 self->mmap_window = 32 * 1024 * 1024ULL;
154#endif
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300155 self->machines = RB_ROOT;
Tom Zanussi454c4072010-05-01 01:41:20 -0500156 self->repipe = repipe;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000157 INIT_LIST_HEAD(&self->ordered_samples.samples);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000158 INIT_LIST_HEAD(&self->ordered_samples.sample_cache);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000159 INIT_LIST_HEAD(&self->ordered_samples.to_free);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300160 machine__init(&self->host_machine, "", HOST_KERNEL_ID);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200161
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200162 if (mode == O_RDONLY) {
163 if (perf_session__open(self, force) < 0)
164 goto out_delete;
165 } else if (mode == O_WRONLY) {
166 /*
167 * In O_RDONLY mode this will be performed when reading the
168 * kernel MMAP event, in event__process_mmap().
169 */
170 if (perf_session__create_kernel_maps(self) < 0)
171 goto out_delete;
172 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200173
Tom Zanussi8dc58102010-04-01 23:59:15 -0500174 perf_session__update_sample_type(self);
Ian Munsie21ef97f2010-12-10 14:09:16 +1100175
176 if (ops && ops->ordering_requires_timestamps &&
177 ops->ordered_samples && !self->sample_id_all) {
178 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
179 ops->ordered_samples = false;
180 }
181
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200182out:
183 return self;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200184out_free:
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200185 free(self);
186 return NULL;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200187out_delete:
188 perf_session__delete(self);
189 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200190}
191
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300192static void perf_session__delete_dead_threads(struct perf_session *self)
193{
194 struct thread *n, *t;
195
196 list_for_each_entry_safe(t, n, &self->dead_threads, node) {
197 list_del(&t->node);
198 thread__delete(t);
199 }
200}
201
202static void perf_session__delete_threads(struct perf_session *self)
203{
204 struct rb_node *nd = rb_first(&self->threads);
205
206 while (nd) {
207 struct thread *t = rb_entry(nd, struct thread, rb_node);
208
209 rb_erase(&t->rb_node, &self->threads);
210 nd = rb_next(nd);
211 thread__delete(t);
212 }
213}
214
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200215void perf_session__delete(struct perf_session *self)
216{
217 perf_header__exit(&self->header);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300218 perf_session__destroy_kernel_maps(self);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300219 perf_session__delete_dead_threads(self);
220 perf_session__delete_threads(self);
221 machine__exit(&self->host_machine);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200222 close(self->fd);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200223 free(self);
224}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200225
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300226void perf_session__remove_thread(struct perf_session *self, struct thread *th)
227{
Arnaldo Carvalho de Melo70597f22010-08-02 18:59:28 -0300228 self->last_match = NULL;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300229 rb_erase(&th->rb_node, &self->threads);
230 /*
231 * We may have references to this thread, for instance in some hist_entry
232 * instances, so just move them to a separate list.
233 */
234 list_add_tail(&th->node, &self->dead_threads);
235}
236
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200237static bool symbol__match_parent_regex(struct symbol *sym)
238{
239 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
240 return 1;
241
242 return 0;
243}
244
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100245int perf_session__resolve_callchain(struct perf_session *self,
246 struct thread *thread,
247 struct ip_callchain *chain,
248 struct symbol **parent)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200249{
250 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200251 unsigned int i;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100252 int err;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200253
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100254 callchain_cursor_reset(&self->callchain_cursor);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200255
256 for (i = 0; i < chain->nr; i++) {
257 u64 ip = chain->ips[i];
258 struct addr_location al;
259
260 if (ip >= PERF_CONTEXT_MAX) {
261 switch (ip) {
262 case PERF_CONTEXT_HV:
263 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
264 case PERF_CONTEXT_KERNEL:
265 cpumode = PERF_RECORD_MISC_KERNEL; break;
266 case PERF_CONTEXT_USER:
267 cpumode = PERF_RECORD_MISC_USER; break;
268 default:
269 break;
270 }
271 continue;
272 }
273
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800274 al.filtered = false;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200275 thread__find_addr_location(thread, self, cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800276 MAP__FUNCTION, thread->pid, ip, &al, NULL);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200277 if (al.sym != NULL) {
278 if (sort__has_parent && !*parent &&
279 symbol__match_parent_regex(al.sym))
280 *parent = al.sym;
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200281 if (!symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200282 break;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200283 }
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100284
285 err = callchain_cursor_append(&self->callchain_cursor,
286 ip, al.map, al.sym);
287 if (err)
288 return err;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200289 }
290
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100291 return 0;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200292}
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200293
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200294static int process_event_synth_stub(event_t *event __used,
295 struct perf_session *session __used)
296{
297 dump_printf(": unhandled!\n");
298 return 0;
299}
300
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200301static int process_event_stub(event_t *event __used,
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200302 struct sample_data *sample __used,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200303 struct perf_session *session __used)
304{
305 dump_printf(": unhandled!\n");
306 return 0;
307}
308
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200309static int process_finished_round_stub(event_t *event __used,
310 struct perf_session *session __used,
311 struct perf_event_ops *ops __used)
312{
313 dump_printf(": unhandled!\n");
314 return 0;
315}
316
317static int process_finished_round(event_t *event,
318 struct perf_session *session,
319 struct perf_event_ops *ops);
320
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200321static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
322{
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200323 if (handler->sample == NULL)
324 handler->sample = process_event_stub;
325 if (handler->mmap == NULL)
326 handler->mmap = process_event_stub;
327 if (handler->comm == NULL)
328 handler->comm = process_event_stub;
329 if (handler->fork == NULL)
330 handler->fork = process_event_stub;
331 if (handler->exit == NULL)
332 handler->exit = process_event_stub;
333 if (handler->lost == NULL)
Arnaldo Carvalho de Melo37982ba2010-11-26 18:31:54 -0200334 handler->lost = event__process_lost;
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200335 if (handler->read == NULL)
336 handler->read = process_event_stub;
337 if (handler->throttle == NULL)
338 handler->throttle = process_event_stub;
339 if (handler->unthrottle == NULL)
340 handler->unthrottle = process_event_stub;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500341 if (handler->attr == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200342 handler->attr = process_event_synth_stub;
Tom Zanussicd19a032010-04-01 23:59:20 -0500343 if (handler->event_type == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200344 handler->event_type = process_event_synth_stub;
Tom Zanussi92155452010-04-01 23:59:21 -0500345 if (handler->tracing_data == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200346 handler->tracing_data = process_event_synth_stub;
Tom Zanussic7929e42010-04-01 23:59:22 -0500347 if (handler->build_id == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200348 handler->build_id = process_event_synth_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200349 if (handler->finished_round == NULL) {
350 if (handler->ordered_samples)
351 handler->finished_round = process_finished_round;
352 else
353 handler->finished_round = process_finished_round_stub;
354 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200355}
356
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200357void mem_bswap_64(void *src, int byte_size)
358{
359 u64 *m = src;
360
361 while (byte_size > 0) {
362 *m = bswap_64(*m);
363 byte_size -= sizeof(u64);
364 ++m;
365 }
366}
367
368static void event__all64_swap(event_t *self)
369{
370 struct perf_event_header *hdr = &self->header;
371 mem_bswap_64(hdr + 1, self->header.size - sizeof(*hdr));
372}
373
374static void event__comm_swap(event_t *self)
375{
376 self->comm.pid = bswap_32(self->comm.pid);
377 self->comm.tid = bswap_32(self->comm.tid);
378}
379
380static void event__mmap_swap(event_t *self)
381{
382 self->mmap.pid = bswap_32(self->mmap.pid);
383 self->mmap.tid = bswap_32(self->mmap.tid);
384 self->mmap.start = bswap_64(self->mmap.start);
385 self->mmap.len = bswap_64(self->mmap.len);
386 self->mmap.pgoff = bswap_64(self->mmap.pgoff);
387}
388
389static void event__task_swap(event_t *self)
390{
391 self->fork.pid = bswap_32(self->fork.pid);
392 self->fork.tid = bswap_32(self->fork.tid);
393 self->fork.ppid = bswap_32(self->fork.ppid);
394 self->fork.ptid = bswap_32(self->fork.ptid);
395 self->fork.time = bswap_64(self->fork.time);
396}
397
398static void event__read_swap(event_t *self)
399{
400 self->read.pid = bswap_32(self->read.pid);
401 self->read.tid = bswap_32(self->read.tid);
402 self->read.value = bswap_64(self->read.value);
403 self->read.time_enabled = bswap_64(self->read.time_enabled);
404 self->read.time_running = bswap_64(self->read.time_running);
405 self->read.id = bswap_64(self->read.id);
406}
407
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500408static void event__attr_swap(event_t *self)
409{
410 size_t size;
411
412 self->attr.attr.type = bswap_32(self->attr.attr.type);
413 self->attr.attr.size = bswap_32(self->attr.attr.size);
414 self->attr.attr.config = bswap_64(self->attr.attr.config);
415 self->attr.attr.sample_period = bswap_64(self->attr.attr.sample_period);
416 self->attr.attr.sample_type = bswap_64(self->attr.attr.sample_type);
417 self->attr.attr.read_format = bswap_64(self->attr.attr.read_format);
418 self->attr.attr.wakeup_events = bswap_32(self->attr.attr.wakeup_events);
419 self->attr.attr.bp_type = bswap_32(self->attr.attr.bp_type);
420 self->attr.attr.bp_addr = bswap_64(self->attr.attr.bp_addr);
421 self->attr.attr.bp_len = bswap_64(self->attr.attr.bp_len);
422
423 size = self->header.size;
424 size -= (void *)&self->attr.id - (void *)self;
425 mem_bswap_64(self->attr.id, size);
426}
427
Tom Zanussicd19a032010-04-01 23:59:20 -0500428static void event__event_type_swap(event_t *self)
429{
430 self->event_type.event_type.event_id =
431 bswap_64(self->event_type.event_type.event_id);
432}
433
Tom Zanussi92155452010-04-01 23:59:21 -0500434static void event__tracing_data_swap(event_t *self)
435{
436 self->tracing_data.size = bswap_32(self->tracing_data.size);
437}
438
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200439typedef void (*event__swap_op)(event_t *self);
440
441static event__swap_op event__swap_ops[] = {
442 [PERF_RECORD_MMAP] = event__mmap_swap,
443 [PERF_RECORD_COMM] = event__comm_swap,
444 [PERF_RECORD_FORK] = event__task_swap,
445 [PERF_RECORD_EXIT] = event__task_swap,
446 [PERF_RECORD_LOST] = event__all64_swap,
447 [PERF_RECORD_READ] = event__read_swap,
448 [PERF_RECORD_SAMPLE] = event__all64_swap,
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500449 [PERF_RECORD_HEADER_ATTR] = event__attr_swap,
Tom Zanussicd19a032010-04-01 23:59:20 -0500450 [PERF_RECORD_HEADER_EVENT_TYPE] = event__event_type_swap,
Tom Zanussi92155452010-04-01 23:59:21 -0500451 [PERF_RECORD_HEADER_TRACING_DATA] = event__tracing_data_swap,
Tom Zanussic7929e42010-04-01 23:59:22 -0500452 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
Tom Zanussi8dc58102010-04-01 23:59:15 -0500453 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200454};
455
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200456struct sample_queue {
457 u64 timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000458 u64 file_offset;
Thomas Gleixner28990f72010-11-30 17:49:35 +0000459 event_t *event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200460 struct list_head list;
461};
462
Thomas Gleixner020bb752010-11-30 17:49:53 +0000463static void perf_session_free_sample_buffers(struct perf_session *session)
464{
465 struct ordered_samples *os = &session->ordered_samples;
466
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000467 while (!list_empty(&os->to_free)) {
Thomas Gleixner020bb752010-11-30 17:49:53 +0000468 struct sample_queue *sq;
469
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000470 sq = list_entry(os->to_free.next, struct sample_queue, list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000471 list_del(&sq->list);
472 free(sq);
473 }
474}
475
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100476static int perf_session_deliver_event(struct perf_session *session,
477 event_t *event,
478 struct sample_data *sample,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000479 struct perf_event_ops *ops,
480 u64 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100481
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200482static void flush_sample_queue(struct perf_session *s,
483 struct perf_event_ops *ops)
484{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000485 struct ordered_samples *os = &s->ordered_samples;
486 struct list_head *head = &os->samples;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200487 struct sample_queue *tmp, *iter;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200488 struct sample_data sample;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000489 u64 limit = os->next_flush;
490 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200491
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200492 if (!ops->ordered_samples || !limit)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200493 return;
494
495 list_for_each_entry_safe(iter, tmp, head, list) {
496 if (iter->timestamp > limit)
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000497 break;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200498
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200499 event__parse_sample(iter->event, s, &sample);
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000500 perf_session_deliver_event(s, iter->event, &sample, ops,
501 iter->file_offset);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200502
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000503 os->last_flush = iter->timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200504 list_del(&iter->list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000505 list_add(&iter->list, &os->sample_cache);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200506 }
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000507
508 if (list_empty(head)) {
509 os->last_sample = NULL;
510 } else if (last_ts <= limit) {
511 os->last_sample =
512 list_entry(head->prev, struct sample_queue, list);
513 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200514}
515
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200516/*
517 * When perf record finishes a pass on every buffers, it records this pseudo
518 * event.
519 * We record the max timestamp t found in the pass n.
520 * Assuming these timestamps are monotonic across cpus, we know that if
521 * a buffer still has events with timestamps below t, they will be all
522 * available and then read in the pass n + 1.
523 * Hence when we start to read the pass n + 2, we can safely flush every
524 * events with timestamps below t.
525 *
526 * ============ PASS n =================
527 * CPU 0 | CPU 1
528 * |
529 * cnt1 timestamps | cnt2 timestamps
530 * 1 | 2
531 * 2 | 3
532 * - | 4 <--- max recorded
533 *
534 * ============ PASS n + 1 ==============
535 * CPU 0 | CPU 1
536 * |
537 * cnt1 timestamps | cnt2 timestamps
538 * 3 | 5
539 * 4 | 6
540 * 5 | 7 <---- max recorded
541 *
542 * Flush every events below timestamp 4
543 *
544 * ============ PASS n + 2 ==============
545 * CPU 0 | CPU 1
546 * |
547 * cnt1 timestamps | cnt2 timestamps
548 * 6 | 8
549 * 7 | 9
550 * - | 10
551 *
552 * Flush every events below timestamp 7
553 * etc...
554 */
555static int process_finished_round(event_t *event __used,
556 struct perf_session *session,
557 struct perf_event_ops *ops)
558{
559 flush_sample_queue(session, ops);
560 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
561
562 return 0;
563}
564
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200565/* The queue is ordered by time */
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100566static void __queue_event(struct sample_queue *new, struct perf_session *s)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200567{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000568 struct ordered_samples *os = &s->ordered_samples;
569 struct sample_queue *sample = os->last_sample;
570 u64 timestamp = new->timestamp;
571 struct list_head *p;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200572
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000573 os->last_sample = new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200574
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000575 if (!sample) {
576 list_add(&new->list, &os->samples);
577 os->max_timestamp = timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200578 return;
579 }
580
581 /*
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000582 * last_sample might point to some random place in the list as it's
583 * the last queued event. We expect that the new event is close to
584 * this.
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200585 */
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000586 if (sample->timestamp <= timestamp) {
587 while (sample->timestamp <= timestamp) {
588 p = sample->list.next;
589 if (p == &os->samples) {
590 list_add_tail(&new->list, &os->samples);
591 os->max_timestamp = timestamp;
592 return;
593 }
594 sample = list_entry(p, struct sample_queue, list);
595 }
596 list_add_tail(&new->list, &sample->list);
597 } else {
598 while (sample->timestamp > timestamp) {
599 p = sample->list.prev;
600 if (p == &os->samples) {
601 list_add(&new->list, &os->samples);
602 return;
603 }
604 sample = list_entry(p, struct sample_queue, list);
605 }
606 list_add(&new->list, &sample->list);
607 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200608}
609
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000610#define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
611
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100612static int perf_session_queue_event(struct perf_session *s, event_t *event,
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000613 struct sample_data *data, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200614{
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000615 struct ordered_samples *os = &s->ordered_samples;
616 struct list_head *sc = &os->sample_cache;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200617 u64 timestamp = data->time;
618 struct sample_queue *new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200619
Thomas Gleixner79a14c12010-12-07 12:48:44 +0000620 if (!timestamp || timestamp == ~0ULL)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100621 return -ETIME;
622
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200623 if (timestamp < s->ordered_samples.last_flush) {
624 printf("Warning: Timestamp below last timeslice flush\n");
625 return -EINVAL;
626 }
627
Thomas Gleixner020bb752010-11-30 17:49:53 +0000628 if (!list_empty(sc)) {
629 new = list_entry(sc->next, struct sample_queue, list);
630 list_del(&new->list);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000631 } else if (os->sample_buffer) {
632 new = os->sample_buffer + os->sample_buffer_idx;
633 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
634 os->sample_buffer = NULL;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000635 } else {
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000636 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
637 if (!os->sample_buffer)
Thomas Gleixner020bb752010-11-30 17:49:53 +0000638 return -ENOMEM;
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000639 list_add(&os->sample_buffer->list, &os->to_free);
640 os->sample_buffer_idx = 2;
641 new = os->sample_buffer + 1;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000642 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200643
644 new->timestamp = timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000645 new->file_offset = file_offset;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000646 new->event = event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200647
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100648 __queue_event(new, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200649
650 return 0;
651}
652
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200653static void callchain__printf(struct sample_data *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200654{
655 unsigned int i;
656
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200657 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200658
659 for (i = 0; i < sample->callchain->nr; i++)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200660 printf("..... %2d: %016" PRIx64 "\n",
661 i, sample->callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200662}
663
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200664static void perf_session__print_tstamp(struct perf_session *session,
665 event_t *event,
666 struct sample_data *sample)
667{
668 if (event->header.type != PERF_RECORD_SAMPLE &&
669 !session->sample_id_all) {
670 fputs("-1 -1 ", stdout);
671 return;
672 }
673
674 if ((session->sample_type & PERF_SAMPLE_CPU))
675 printf("%u ", sample->cpu);
676
677 if (session->sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200678 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200679}
680
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000681static void dump_event(struct perf_session *session, event_t *event,
682 u64 file_offset, struct sample_data *sample)
683{
684 if (!dump_trace)
685 return;
686
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200687 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
688 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000689
690 trace_event(event);
691
692 if (sample)
693 perf_session__print_tstamp(session, event, sample);
694
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200695 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
696 event->header.size, event__get_event_name(event->header.type));
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000697}
698
699static void dump_sample(struct perf_session *session, event_t *event,
700 struct sample_data *sample)
701{
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200702 if (!dump_trace)
703 return;
704
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200705 printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 "\n",
706 event->header.misc, sample->pid, sample->tid, sample->ip,
707 sample->period);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000708
709 if (session->sample_type & PERF_SAMPLE_CALLCHAIN)
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200710 callchain__printf(sample);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000711}
712
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100713static int perf_session_deliver_event(struct perf_session *session,
714 event_t *event,
715 struct sample_data *sample,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000716 struct perf_event_ops *ops,
Thomas Gleixner532e7262010-12-07 12:48:55 +0000717 u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100718{
Thomas Gleixner532e7262010-12-07 12:48:55 +0000719 dump_event(session, event, file_offset, sample);
720
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100721 switch (event->header.type) {
722 case PERF_RECORD_SAMPLE:
Thomas Gleixner532e7262010-12-07 12:48:55 +0000723 dump_sample(session, event, sample);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100724 return ops->sample(event, sample, session);
725 case PERF_RECORD_MMAP:
726 return ops->mmap(event, sample, session);
727 case PERF_RECORD_COMM:
728 return ops->comm(event, sample, session);
729 case PERF_RECORD_FORK:
730 return ops->fork(event, sample, session);
731 case PERF_RECORD_EXIT:
732 return ops->exit(event, sample, session);
733 case PERF_RECORD_LOST:
734 return ops->lost(event, sample, session);
735 case PERF_RECORD_READ:
736 return ops->read(event, sample, session);
737 case PERF_RECORD_THROTTLE:
738 return ops->throttle(event, sample, session);
739 case PERF_RECORD_UNTHROTTLE:
740 return ops->unthrottle(event, sample, session);
741 default:
742 ++session->hists.stats.nr_unknown_events;
743 return -1;
744 }
745}
746
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000747static int perf_session__preprocess_sample(struct perf_session *session,
748 event_t *event, struct sample_data *sample)
749{
750 if (event->header.type != PERF_RECORD_SAMPLE ||
751 !(session->sample_type & PERF_SAMPLE_CALLCHAIN))
752 return 0;
753
754 if (!ip_callchain__valid(sample->callchain, event)) {
755 pr_debug("call-chain problem with event, skipping it.\n");
756 ++session->hists.stats.nr_invalid_chains;
757 session->hists.stats.total_invalid_chains += sample->period;
758 return -EINVAL;
759 }
760 return 0;
761}
762
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000763static int perf_session__process_user_event(struct perf_session *session, event_t *event,
764 struct perf_event_ops *ops, u64 file_offset)
765{
766 dump_event(session, event, file_offset, NULL);
767
768 /* These events are processed right away */
769 switch (event->header.type) {
770 case PERF_RECORD_HEADER_ATTR:
771 return ops->attr(event, session);
772 case PERF_RECORD_HEADER_EVENT_TYPE:
773 return ops->event_type(event, session);
774 case PERF_RECORD_HEADER_TRACING_DATA:
775 /* setup for reading amidst mmap */
776 lseek(session->fd, file_offset, SEEK_SET);
777 return ops->tracing_data(event, session);
778 case PERF_RECORD_HEADER_BUILD_ID:
779 return ops->build_id(event, session);
780 case PERF_RECORD_FINISHED_ROUND:
781 return ops->finished_round(event, session, ops);
782 default:
783 return -EINVAL;
784 }
785}
786
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100787static int perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200788 event_t *event,
789 struct perf_event_ops *ops,
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000790 u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200791{
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200792 struct sample_data sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100793 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200794
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100795 if (session->header.needs_swap && event__swap_ops[event->header.type])
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200796 event__swap_ops[event->header.type](event);
797
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000798 if (event->header.type >= PERF_RECORD_HEADER_MAX)
799 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200800
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000801 hists__inc_nr_events(&session->hists, event->header.type);
802
803 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000804 return perf_session__process_user_event(session, event, ops, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100805
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000806 /*
807 * For all kernel events we get the sample data
808 */
809 event__parse_sample(event, session, &sample);
810
811 /* Preprocess sample records - precheck callchains */
812 if (perf_session__preprocess_sample(session, event, &sample))
813 return 0;
814
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100815 if (ops->ordered_samples) {
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000816 ret = perf_session_queue_event(session, event, &sample,
817 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100818 if (ret != -ETIME)
819 return ret;
820 }
821
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000822 return perf_session_deliver_event(session, event, &sample, ops,
823 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200824}
825
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200826void perf_event_header__bswap(struct perf_event_header *self)
827{
828 self->type = bswap_32(self->type);
829 self->misc = bswap_16(self->misc);
830 self->size = bswap_16(self->size);
831}
832
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200833static struct thread *perf_session__register_idle_thread(struct perf_session *self)
834{
835 struct thread *thread = perf_session__findnew(self, 0);
836
837 if (thread == NULL || thread__set_comm(thread, "swapper")) {
838 pr_err("problem inserting idle task.\n");
839 thread = NULL;
840 }
841
842 return thread;
843}
844
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200845static void perf_session__warn_about_errors(const struct perf_session *session,
846 const struct perf_event_ops *ops)
847{
848 if (ops->lost == event__process_lost &&
849 session->hists.stats.total_lost != 0) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200850 ui__warning("Processed %" PRIu64 " events and LOST %" PRIu64
851 "!\n\nCheck IO/CPU overload!\n\n",
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200852 session->hists.stats.total_period,
853 session->hists.stats.total_lost);
854 }
855
856 if (session->hists.stats.nr_unknown_events != 0) {
857 ui__warning("Found %u unknown events!\n\n"
858 "Is this an older tool processing a perf.data "
859 "file generated by a more recent tool?\n\n"
860 "If that is not the case, consider "
861 "reporting to linux-kernel@vger.kernel.org.\n\n",
862 session->hists.stats.nr_unknown_events);
863 }
864
865 if (session->hists.stats.nr_invalid_chains != 0) {
866 ui__warning("Found invalid callchains!\n\n"
867 "%u out of %u events were discarded for this reason.\n\n"
868 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
869 session->hists.stats.nr_invalid_chains,
870 session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
871 }
872}
873
Tom Zanussi8dc58102010-04-01 23:59:15 -0500874#define session_done() (*(volatile int *)(&session_done))
875volatile int session_done;
876
877static int __perf_session__process_pipe_events(struct perf_session *self,
878 struct perf_event_ops *ops)
879{
880 event_t event;
881 uint32_t size;
882 int skip = 0;
883 u64 head;
884 int err;
885 void *p;
886
887 perf_event_ops__fill_defaults(ops);
888
889 head = 0;
890more:
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200891 err = readn(self->fd, &event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -0500892 if (err <= 0) {
893 if (err == 0)
894 goto done;
895
896 pr_err("failed to read event header\n");
897 goto out_err;
898 }
899
900 if (self->header.needs_swap)
901 perf_event_header__bswap(&event.header);
902
903 size = event.header.size;
904 if (size == 0)
905 size = 8;
906
907 p = &event;
908 p += sizeof(struct perf_event_header);
909
Tom Zanussi794e43b2010-05-05 00:27:40 -0500910 if (size - sizeof(struct perf_event_header)) {
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200911 err = readn(self->fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -0500912 if (err <= 0) {
913 if (err == 0) {
914 pr_err("unexpected end of event stream\n");
915 goto done;
916 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500917
Tom Zanussi794e43b2010-05-05 00:27:40 -0500918 pr_err("failed to read event data\n");
919 goto out_err;
920 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500921 }
922
923 if (size == 0 ||
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000924 (skip = perf_session__process_event(self, &event, ops, head)) < 0) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200925 dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
Tom Zanussi8dc58102010-04-01 23:59:15 -0500926 head, event.header.size, event.header.type);
927 /*
928 * assume we lost track of the stream, check alignment, and
929 * increment a single u64 in the hope to catch on again 'soon'.
930 */
931 if (unlikely(head & 7))
932 head &= ~7ULL;
933
934 size = 8;
935 }
936
937 head += size;
938
Tom Zanussi8dc58102010-04-01 23:59:15 -0500939 if (skip > 0)
940 head += skip;
941
942 if (!session_done())
943 goto more;
944done:
945 err = 0;
946out_err:
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200947 perf_session__warn_about_errors(self, ops);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000948 perf_session_free_sample_buffers(self);
Tom Zanussi8dc58102010-04-01 23:59:15 -0500949 return err;
950}
951
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000952int __perf_session__process_events(struct perf_session *session,
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200953 u64 data_offset, u64 data_size,
954 u64 file_size, struct perf_event_ops *ops)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200955{
Thomas Gleixner55b446292010-11-30 17:49:46 +0000956 u64 head, page_offset, file_offset, file_pos, progress_next;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000957 int err, mmap_prot, mmap_flags, map_idx = 0;
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000958 struct ui_progress *progress;
Thomas Gleixner55b446292010-11-30 17:49:46 +0000959 size_t page_size, mmap_size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000960 char *buf, *mmaps[8];
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200961 event_t *event;
962 uint32_t size;
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000963
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200964 perf_event_ops__fill_defaults(ops);
965
Arnaldo Carvalho de Melo1b759622010-01-14 18:30:04 -0200966 page_size = sysconf(_SC_PAGESIZE);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200967
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000968 page_offset = page_size * (data_offset / page_size);
969 file_offset = page_offset;
970 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200971
Thomas Gleixnerd6513282010-11-30 17:49:44 +0000972 if (data_offset + data_size < file_size)
973 file_size = data_offset + data_size;
974
Thomas Gleixner55b446292010-11-30 17:49:46 +0000975 progress_next = file_size / 16;
976 progress = ui_progress__new("Processing events...", file_size);
977 if (progress == NULL)
978 return -1;
979
980 mmap_size = session->mmap_window;
981 if (mmap_size > file_size)
982 mmap_size = file_size;
983
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000984 memset(mmaps, 0, sizeof(mmaps));
985
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200986 mmap_prot = PROT_READ;
987 mmap_flags = MAP_SHARED;
988
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000989 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200990 mmap_prot |= PROT_WRITE;
991 mmap_flags = MAP_PRIVATE;
992 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200993remap:
Thomas Gleixner55b446292010-11-30 17:49:46 +0000994 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
995 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200996 if (buf == MAP_FAILED) {
997 pr_err("failed to mmap file\n");
998 err = -errno;
999 goto out_err;
1000 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001001 mmaps[map_idx] = buf;
1002 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001003 file_pos = file_offset + head;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001004
1005more:
1006 event = (event_t *)(buf + head);
1007
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001008 if (session->header.needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001009 perf_event_header__bswap(&event->header);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001010 size = event->header.size;
1011 if (size == 0)
1012 size = 8;
1013
Arnaldo Carvalho de Melo3d03e2e2011-01-10 21:37:57 -02001014 if (head + event->header.size > mmap_size) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001015 if (mmaps[map_idx]) {
1016 munmap(mmaps[map_idx], mmap_size);
1017 mmaps[map_idx] = NULL;
1018 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001019
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001020 page_offset = page_size * (head / page_size);
1021 file_offset += page_offset;
1022 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001023 goto remap;
1024 }
1025
1026 size = event->header.size;
1027
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001028 if (size == 0 ||
1029 perf_session__process_event(session, event, ops, file_pos) < 0) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001030 dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001031 file_offset + head, event->header.size,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001032 event->header.type);
1033 /*
1034 * assume we lost track of the stream, check alignment, and
1035 * increment a single u64 in the hope to catch on again 'soon'.
1036 */
1037 if (unlikely(head & 7))
1038 head &= ~7ULL;
1039
1040 size = 8;
1041 }
1042
1043 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001044 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001045
Thomas Gleixner55b446292010-11-30 17:49:46 +00001046 if (file_pos >= progress_next) {
1047 progress_next += file_size / 16;
1048 ui_progress__update(progress, file_pos);
1049 }
1050
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001051 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001052 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001053
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001054 err = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001055 /* do the final flush for ordered samples */
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001056 session->ordered_samples.next_flush = ULLONG_MAX;
1057 flush_sample_queue(session, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001058out_err:
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -03001059 ui_progress__delete(progress);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001060 perf_session__warn_about_errors(session, ops);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001061 perf_session_free_sample_buffers(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001062 return err;
1063}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001064
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001065int perf_session__process_events(struct perf_session *self,
1066 struct perf_event_ops *ops)
1067{
1068 int err;
1069
1070 if (perf_session__register_idle_thread(self) == NULL)
1071 return -ENOMEM;
1072
Tom Zanussi8dc58102010-04-01 23:59:15 -05001073 if (!self->fd_pipe)
1074 err = __perf_session__process_events(self,
1075 self->header.data_offset,
1076 self->header.data_size,
1077 self->size, ops);
1078 else
1079 err = __perf_session__process_pipe_events(self, ops);
Dave Martin88ca8952010-07-27 11:46:12 -03001080
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001081 return err;
1082}
1083
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001084bool perf_session__has_traces(struct perf_session *self, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001085{
1086 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001087 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1088 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001089 }
1090
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001091 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001092}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001093
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001094int perf_session__set_kallsyms_ref_reloc_sym(struct map **maps,
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001095 const char *symbol_name,
1096 u64 addr)
1097{
1098 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001099 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001100 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001101
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001102 ref = zalloc(sizeof(struct ref_reloc_sym));
1103 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001104 return -ENOMEM;
1105
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001106 ref->name = strdup(symbol_name);
1107 if (ref->name == NULL) {
1108 free(ref);
1109 return -ENOMEM;
1110 }
1111
1112 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001113 if (bracket)
1114 *bracket = '\0';
1115
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001116 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001117
1118 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001119 struct kmap *kmap = map__kmap(maps[i]);
1120 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001121 }
1122
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001123 return 0;
1124}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001125
1126size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
1127{
1128 return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
1129 __dsos__fprintf(&self->host_machine.user_dsos, fp) +
1130 machines__fprintf_dsos(&self->machines, fp);
1131}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001132
1133size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
1134 bool with_hits)
1135{
1136 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
1137 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
1138}