blob: 04a3b3db9e90824a1f5c931fb13219d385a9e74e [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
Tom Zanussi8dc58102010-04-01 23:59:15 -050068void perf_session__update_sample_type(struct perf_session *self)
69{
70 self->sample_type = perf_header__sample_type(&self->header);
71}
72
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080073int perf_session__create_kernel_maps(struct perf_session *self)
74{
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -030075 int ret = machine__create_kernel_maps(&self->host_machine);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080076
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080077 if (ret >= 0)
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -030078 ret = machines__create_guest_kernel_maps(&self->machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080079 return ret;
80}
81
Tom Zanussi454c4072010-05-01 01:41:20 -050082struct perf_session *perf_session__new(const char *filename, int mode, bool force, bool repipe)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020083{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020084 size_t len = filename ? strlen(filename) + 1 : 0;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020085 struct perf_session *self = zalloc(sizeof(*self) + len);
86
87 if (self == NULL)
88 goto out;
89
90 if (perf_header__init(&self->header) < 0)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -020091 goto out_free;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020092
93 memcpy(self->filename, filename, len);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020094 self->threads = RB_ROOT;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -030095 INIT_LIST_HEAD(&self->dead_threads);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -030096 self->hists_tree = RB_ROOT;
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020097 self->last_match = NULL;
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -020098 self->mmap_window = 32;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -030099 self->machines = RB_ROOT;
Tom Zanussi454c4072010-05-01 01:41:20 -0500100 self->repipe = repipe;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200101 INIT_LIST_HEAD(&self->ordered_samples.samples_head);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300102 machine__init(&self->host_machine, "", HOST_KERNEL_ID);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200103
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200104 if (mode == O_RDONLY) {
105 if (perf_session__open(self, force) < 0)
106 goto out_delete;
107 } else if (mode == O_WRONLY) {
108 /*
109 * In O_RDONLY mode this will be performed when reading the
110 * kernel MMAP event, in event__process_mmap().
111 */
112 if (perf_session__create_kernel_maps(self) < 0)
113 goto out_delete;
114 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200115
Tom Zanussi8dc58102010-04-01 23:59:15 -0500116 perf_session__update_sample_type(self);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200117out:
118 return self;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200119out_free:
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200120 free(self);
121 return NULL;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200122out_delete:
123 perf_session__delete(self);
124 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200125}
126
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300127static void perf_session__delete_dead_threads(struct perf_session *self)
128{
129 struct thread *n, *t;
130
131 list_for_each_entry_safe(t, n, &self->dead_threads, node) {
132 list_del(&t->node);
133 thread__delete(t);
134 }
135}
136
137static void perf_session__delete_threads(struct perf_session *self)
138{
139 struct rb_node *nd = rb_first(&self->threads);
140
141 while (nd) {
142 struct thread *t = rb_entry(nd, struct thread, rb_node);
143
144 rb_erase(&t->rb_node, &self->threads);
145 nd = rb_next(nd);
146 thread__delete(t);
147 }
148}
149
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200150void perf_session__delete(struct perf_session *self)
151{
152 perf_header__exit(&self->header);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300153 perf_session__delete_dead_threads(self);
154 perf_session__delete_threads(self);
155 machine__exit(&self->host_machine);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200156 close(self->fd);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200157 free(self);
158}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200159
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300160void perf_session__remove_thread(struct perf_session *self, struct thread *th)
161{
162 rb_erase(&th->rb_node, &self->threads);
163 /*
164 * We may have references to this thread, for instance in some hist_entry
165 * instances, so just move them to a separate list.
166 */
167 list_add_tail(&th->node, &self->dead_threads);
168}
169
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200170static bool symbol__match_parent_regex(struct symbol *sym)
171{
172 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
173 return 1;
174
175 return 0;
176}
177
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300178struct map_symbol *perf_session__resolve_callchain(struct perf_session *self,
179 struct thread *thread,
180 struct ip_callchain *chain,
181 struct symbol **parent)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200182{
183 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200184 unsigned int i;
Arnaldo Carvalho de Meload5b2172010-04-02 10:04:18 -0300185 struct map_symbol *syms = calloc(chain->nr, sizeof(*syms));
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200186
Arnaldo Carvalho de Meload5b2172010-04-02 10:04:18 -0300187 if (!syms)
188 return NULL;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200189
190 for (i = 0; i < chain->nr; i++) {
191 u64 ip = chain->ips[i];
192 struct addr_location al;
193
194 if (ip >= PERF_CONTEXT_MAX) {
195 switch (ip) {
196 case PERF_CONTEXT_HV:
197 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
198 case PERF_CONTEXT_KERNEL:
199 cpumode = PERF_RECORD_MISC_KERNEL; break;
200 case PERF_CONTEXT_USER:
201 cpumode = PERF_RECORD_MISC_USER; break;
202 default:
203 break;
204 }
205 continue;
206 }
207
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800208 al.filtered = false;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200209 thread__find_addr_location(thread, self, cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800210 MAP__FUNCTION, thread->pid, ip, &al, NULL);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200211 if (al.sym != NULL) {
212 if (sort__has_parent && !*parent &&
213 symbol__match_parent_regex(al.sym))
214 *parent = al.sym;
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200215 if (!symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200216 break;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300217 syms[i].map = al.map;
218 syms[i].sym = al.sym;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200219 }
220 }
221
222 return syms;
223}
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200224
225static int process_event_stub(event_t *event __used,
226 struct perf_session *session __used)
227{
228 dump_printf(": unhandled!\n");
229 return 0;
230}
231
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200232static int process_finished_round_stub(event_t *event __used,
233 struct perf_session *session __used,
234 struct perf_event_ops *ops __used)
235{
236 dump_printf(": unhandled!\n");
237 return 0;
238}
239
240static int process_finished_round(event_t *event,
241 struct perf_session *session,
242 struct perf_event_ops *ops);
243
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200244static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
245{
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200246 if (handler->sample == NULL)
247 handler->sample = process_event_stub;
248 if (handler->mmap == NULL)
249 handler->mmap = process_event_stub;
250 if (handler->comm == NULL)
251 handler->comm = process_event_stub;
252 if (handler->fork == NULL)
253 handler->fork = process_event_stub;
254 if (handler->exit == NULL)
255 handler->exit = process_event_stub;
256 if (handler->lost == NULL)
257 handler->lost = process_event_stub;
258 if (handler->read == NULL)
259 handler->read = process_event_stub;
260 if (handler->throttle == NULL)
261 handler->throttle = process_event_stub;
262 if (handler->unthrottle == NULL)
263 handler->unthrottle = process_event_stub;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500264 if (handler->attr == NULL)
265 handler->attr = process_event_stub;
Tom Zanussicd19a032010-04-01 23:59:20 -0500266 if (handler->event_type == NULL)
267 handler->event_type = process_event_stub;
Tom Zanussi92155452010-04-01 23:59:21 -0500268 if (handler->tracing_data == NULL)
269 handler->tracing_data = process_event_stub;
Tom Zanussic7929e42010-04-01 23:59:22 -0500270 if (handler->build_id == NULL)
271 handler->build_id = process_event_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200272 if (handler->finished_round == NULL) {
273 if (handler->ordered_samples)
274 handler->finished_round = process_finished_round;
275 else
276 handler->finished_round = process_finished_round_stub;
277 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200278}
279
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200280void mem_bswap_64(void *src, int byte_size)
281{
282 u64 *m = src;
283
284 while (byte_size > 0) {
285 *m = bswap_64(*m);
286 byte_size -= sizeof(u64);
287 ++m;
288 }
289}
290
291static void event__all64_swap(event_t *self)
292{
293 struct perf_event_header *hdr = &self->header;
294 mem_bswap_64(hdr + 1, self->header.size - sizeof(*hdr));
295}
296
297static void event__comm_swap(event_t *self)
298{
299 self->comm.pid = bswap_32(self->comm.pid);
300 self->comm.tid = bswap_32(self->comm.tid);
301}
302
303static void event__mmap_swap(event_t *self)
304{
305 self->mmap.pid = bswap_32(self->mmap.pid);
306 self->mmap.tid = bswap_32(self->mmap.tid);
307 self->mmap.start = bswap_64(self->mmap.start);
308 self->mmap.len = bswap_64(self->mmap.len);
309 self->mmap.pgoff = bswap_64(self->mmap.pgoff);
310}
311
312static void event__task_swap(event_t *self)
313{
314 self->fork.pid = bswap_32(self->fork.pid);
315 self->fork.tid = bswap_32(self->fork.tid);
316 self->fork.ppid = bswap_32(self->fork.ppid);
317 self->fork.ptid = bswap_32(self->fork.ptid);
318 self->fork.time = bswap_64(self->fork.time);
319}
320
321static void event__read_swap(event_t *self)
322{
323 self->read.pid = bswap_32(self->read.pid);
324 self->read.tid = bswap_32(self->read.tid);
325 self->read.value = bswap_64(self->read.value);
326 self->read.time_enabled = bswap_64(self->read.time_enabled);
327 self->read.time_running = bswap_64(self->read.time_running);
328 self->read.id = bswap_64(self->read.id);
329}
330
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500331static void event__attr_swap(event_t *self)
332{
333 size_t size;
334
335 self->attr.attr.type = bswap_32(self->attr.attr.type);
336 self->attr.attr.size = bswap_32(self->attr.attr.size);
337 self->attr.attr.config = bswap_64(self->attr.attr.config);
338 self->attr.attr.sample_period = bswap_64(self->attr.attr.sample_period);
339 self->attr.attr.sample_type = bswap_64(self->attr.attr.sample_type);
340 self->attr.attr.read_format = bswap_64(self->attr.attr.read_format);
341 self->attr.attr.wakeup_events = bswap_32(self->attr.attr.wakeup_events);
342 self->attr.attr.bp_type = bswap_32(self->attr.attr.bp_type);
343 self->attr.attr.bp_addr = bswap_64(self->attr.attr.bp_addr);
344 self->attr.attr.bp_len = bswap_64(self->attr.attr.bp_len);
345
346 size = self->header.size;
347 size -= (void *)&self->attr.id - (void *)self;
348 mem_bswap_64(self->attr.id, size);
349}
350
Tom Zanussicd19a032010-04-01 23:59:20 -0500351static void event__event_type_swap(event_t *self)
352{
353 self->event_type.event_type.event_id =
354 bswap_64(self->event_type.event_type.event_id);
355}
356
Tom Zanussi92155452010-04-01 23:59:21 -0500357static void event__tracing_data_swap(event_t *self)
358{
359 self->tracing_data.size = bswap_32(self->tracing_data.size);
360}
361
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200362typedef void (*event__swap_op)(event_t *self);
363
364static event__swap_op event__swap_ops[] = {
365 [PERF_RECORD_MMAP] = event__mmap_swap,
366 [PERF_RECORD_COMM] = event__comm_swap,
367 [PERF_RECORD_FORK] = event__task_swap,
368 [PERF_RECORD_EXIT] = event__task_swap,
369 [PERF_RECORD_LOST] = event__all64_swap,
370 [PERF_RECORD_READ] = event__read_swap,
371 [PERF_RECORD_SAMPLE] = event__all64_swap,
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500372 [PERF_RECORD_HEADER_ATTR] = event__attr_swap,
Tom Zanussicd19a032010-04-01 23:59:20 -0500373 [PERF_RECORD_HEADER_EVENT_TYPE] = event__event_type_swap,
Tom Zanussi92155452010-04-01 23:59:21 -0500374 [PERF_RECORD_HEADER_TRACING_DATA] = event__tracing_data_swap,
Tom Zanussic7929e42010-04-01 23:59:22 -0500375 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
Tom Zanussi8dc58102010-04-01 23:59:15 -0500376 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200377};
378
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200379struct sample_queue {
380 u64 timestamp;
381 struct sample_event *event;
382 struct list_head list;
383};
384
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200385static void flush_sample_queue(struct perf_session *s,
386 struct perf_event_ops *ops)
387{
388 struct list_head *head = &s->ordered_samples.samples_head;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200389 u64 limit = s->ordered_samples.next_flush;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200390 struct sample_queue *tmp, *iter;
391
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200392 if (!ops->ordered_samples || !limit)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200393 return;
394
395 list_for_each_entry_safe(iter, tmp, head, list) {
396 if (iter->timestamp > limit)
397 return;
398
399 if (iter == s->ordered_samples.last_inserted)
400 s->ordered_samples.last_inserted = NULL;
401
402 ops->sample((event_t *)iter->event, s);
403
404 s->ordered_samples.last_flush = iter->timestamp;
405 list_del(&iter->list);
406 free(iter->event);
407 free(iter);
408 }
409}
410
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200411/*
412 * When perf record finishes a pass on every buffers, it records this pseudo
413 * event.
414 * We record the max timestamp t found in the pass n.
415 * Assuming these timestamps are monotonic across cpus, we know that if
416 * a buffer still has events with timestamps below t, they will be all
417 * available and then read in the pass n + 1.
418 * Hence when we start to read the pass n + 2, we can safely flush every
419 * events with timestamps below t.
420 *
421 * ============ PASS n =================
422 * CPU 0 | CPU 1
423 * |
424 * cnt1 timestamps | cnt2 timestamps
425 * 1 | 2
426 * 2 | 3
427 * - | 4 <--- max recorded
428 *
429 * ============ PASS n + 1 ==============
430 * CPU 0 | CPU 1
431 * |
432 * cnt1 timestamps | cnt2 timestamps
433 * 3 | 5
434 * 4 | 6
435 * 5 | 7 <---- max recorded
436 *
437 * Flush every events below timestamp 4
438 *
439 * ============ PASS n + 2 ==============
440 * CPU 0 | CPU 1
441 * |
442 * cnt1 timestamps | cnt2 timestamps
443 * 6 | 8
444 * 7 | 9
445 * - | 10
446 *
447 * Flush every events below timestamp 7
448 * etc...
449 */
450static int process_finished_round(event_t *event __used,
451 struct perf_session *session,
452 struct perf_event_ops *ops)
453{
454 flush_sample_queue(session, ops);
455 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
456
457 return 0;
458}
459
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200460static void __queue_sample_end(struct sample_queue *new, struct list_head *head)
461{
462 struct sample_queue *iter;
463
464 list_for_each_entry_reverse(iter, head, list) {
465 if (iter->timestamp < new->timestamp) {
466 list_add(&new->list, &iter->list);
467 return;
468 }
469 }
470
471 list_add(&new->list, head);
472}
473
474static void __queue_sample_before(struct sample_queue *new,
475 struct sample_queue *iter,
476 struct list_head *head)
477{
478 list_for_each_entry_continue_reverse(iter, head, list) {
479 if (iter->timestamp < new->timestamp) {
480 list_add(&new->list, &iter->list);
481 return;
482 }
483 }
484
485 list_add(&new->list, head);
486}
487
488static void __queue_sample_after(struct sample_queue *new,
489 struct sample_queue *iter,
490 struct list_head *head)
491{
492 list_for_each_entry_continue(iter, head, list) {
493 if (iter->timestamp > new->timestamp) {
494 list_add_tail(&new->list, &iter->list);
495 return;
496 }
497 }
498 list_add_tail(&new->list, head);
499}
500
501/* The queue is ordered by time */
502static void __queue_sample_event(struct sample_queue *new,
503 struct perf_session *s)
504{
505 struct sample_queue *last_inserted = s->ordered_samples.last_inserted;
506 struct list_head *head = &s->ordered_samples.samples_head;
507
508
509 if (!last_inserted) {
510 __queue_sample_end(new, head);
511 return;
512 }
513
514 /*
515 * Most of the time the current event has a timestamp
516 * very close to the last event inserted, unless we just switched
517 * to another event buffer. Having a sorting based on a list and
518 * on the last inserted event that is close to the current one is
519 * probably more efficient than an rbtree based sorting.
520 */
521 if (last_inserted->timestamp >= new->timestamp)
522 __queue_sample_before(new, last_inserted, head);
523 else
524 __queue_sample_after(new, last_inserted, head);
525}
526
527static int queue_sample_event(event_t *event, struct sample_data *data,
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200528 struct perf_session *s)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200529{
530 u64 timestamp = data->time;
531 struct sample_queue *new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200532
533
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200534 if (timestamp < s->ordered_samples.last_flush) {
535 printf("Warning: Timestamp below last timeslice flush\n");
536 return -EINVAL;
537 }
538
539 new = malloc(sizeof(*new));
540 if (!new)
541 return -ENOMEM;
542
543 new->timestamp = timestamp;
544
545 new->event = malloc(event->header.size);
546 if (!new->event) {
547 free(new);
548 return -ENOMEM;
549 }
550
551 memcpy(new->event, event, event->header.size);
552
553 __queue_sample_event(new, s);
554 s->ordered_samples.last_inserted = new;
555
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200556 if (new->timestamp > s->ordered_samples.max_timestamp)
557 s->ordered_samples.max_timestamp = new->timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200558
559 return 0;
560}
561
562static int perf_session__process_sample(event_t *event, struct perf_session *s,
563 struct perf_event_ops *ops)
564{
565 struct sample_data data;
566
567 if (!ops->ordered_samples)
568 return ops->sample(event, s);
569
570 bzero(&data, sizeof(struct sample_data));
571 event__parse_sample(event, s->sample_type, &data);
572
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200573 queue_sample_event(event, &data, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200574
575 return 0;
576}
577
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200578static int perf_session__process_event(struct perf_session *self,
579 event_t *event,
580 struct perf_event_ops *ops,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200581 u64 offset, u64 head)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200582{
583 trace_event(event);
584
Tom Zanussi8dc58102010-04-01 23:59:15 -0500585 if (event->header.type < PERF_RECORD_HEADER_MAX) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200586 dump_printf("%#Lx [%#x]: PERF_RECORD_%s",
Arnaldo Carvalho de Melo0d755032010-01-14 12:23:09 -0200587 offset + head, event->header.size,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200588 event__name[event->header.type]);
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300589 hists__inc_nr_events(&self->hists, event->header.type);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200590 }
591
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200592 if (self->header.needs_swap && event__swap_ops[event->header.type])
593 event__swap_ops[event->header.type](event);
594
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200595 switch (event->header.type) {
596 case PERF_RECORD_SAMPLE:
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200597 return perf_session__process_sample(event, self, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200598 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200599 return ops->mmap(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200600 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200601 return ops->comm(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200602 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200603 return ops->fork(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200604 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200605 return ops->exit(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200606 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200607 return ops->lost(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200608 case PERF_RECORD_READ:
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200609 return ops->read(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200610 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200611 return ops->throttle(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200612 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200613 return ops->unthrottle(event, self);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500614 case PERF_RECORD_HEADER_ATTR:
615 return ops->attr(event, self);
Tom Zanussicd19a032010-04-01 23:59:20 -0500616 case PERF_RECORD_HEADER_EVENT_TYPE:
617 return ops->event_type(event, self);
Tom Zanussi92155452010-04-01 23:59:21 -0500618 case PERF_RECORD_HEADER_TRACING_DATA:
619 /* setup for reading amidst mmap */
620 lseek(self->fd, offset + head, SEEK_SET);
621 return ops->tracing_data(event, self);
Tom Zanussic7929e42010-04-01 23:59:22 -0500622 case PERF_RECORD_HEADER_BUILD_ID:
623 return ops->build_id(event, self);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200624 case PERF_RECORD_FINISHED_ROUND:
625 return ops->finished_round(event, self, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200626 default:
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300627 ++self->hists.stats.nr_unknown_events;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200628 return -1;
629 }
630}
631
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200632void perf_event_header__bswap(struct perf_event_header *self)
633{
634 self->type = bswap_32(self->type);
635 self->misc = bswap_16(self->misc);
636 self->size = bswap_16(self->size);
637}
638
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200639static struct thread *perf_session__register_idle_thread(struct perf_session *self)
640{
641 struct thread *thread = perf_session__findnew(self, 0);
642
643 if (thread == NULL || thread__set_comm(thread, "swapper")) {
644 pr_err("problem inserting idle task.\n");
645 thread = NULL;
646 }
647
648 return thread;
649}
650
Tom Zanussi8dc58102010-04-01 23:59:15 -0500651int do_read(int fd, void *buf, size_t size)
652{
653 void *buf_start = buf;
654
655 while (size) {
656 int ret = read(fd, buf, size);
657
658 if (ret <= 0)
659 return ret;
660
661 size -= ret;
662 buf += ret;
663 }
664
665 return buf - buf_start;
666}
667
668#define session_done() (*(volatile int *)(&session_done))
669volatile int session_done;
670
671static int __perf_session__process_pipe_events(struct perf_session *self,
672 struct perf_event_ops *ops)
673{
674 event_t event;
675 uint32_t size;
676 int skip = 0;
677 u64 head;
678 int err;
679 void *p;
680
681 perf_event_ops__fill_defaults(ops);
682
683 head = 0;
684more:
685 err = do_read(self->fd, &event, sizeof(struct perf_event_header));
686 if (err <= 0) {
687 if (err == 0)
688 goto done;
689
690 pr_err("failed to read event header\n");
691 goto out_err;
692 }
693
694 if (self->header.needs_swap)
695 perf_event_header__bswap(&event.header);
696
697 size = event.header.size;
698 if (size == 0)
699 size = 8;
700
701 p = &event;
702 p += sizeof(struct perf_event_header);
703
Tom Zanussi794e43b2010-05-05 00:27:40 -0500704 if (size - sizeof(struct perf_event_header)) {
705 err = do_read(self->fd, p,
706 size - sizeof(struct perf_event_header));
707 if (err <= 0) {
708 if (err == 0) {
709 pr_err("unexpected end of event stream\n");
710 goto done;
711 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500712
Tom Zanussi794e43b2010-05-05 00:27:40 -0500713 pr_err("failed to read event data\n");
714 goto out_err;
715 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500716 }
717
718 if (size == 0 ||
719 (skip = perf_session__process_event(self, &event, ops,
720 0, head)) < 0) {
721 dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n",
722 head, event.header.size, event.header.type);
723 /*
724 * assume we lost track of the stream, check alignment, and
725 * increment a single u64 in the hope to catch on again 'soon'.
726 */
727 if (unlikely(head & 7))
728 head &= ~7ULL;
729
730 size = 8;
731 }
732
733 head += size;
734
735 dump_printf("\n%#Lx [%#x]: event: %d\n",
736 head, event.header.size, event.header.type);
737
738 if (skip > 0)
739 head += skip;
740
741 if (!session_done())
742 goto more;
743done:
744 err = 0;
745out_err:
746 return err;
747}
748
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200749int __perf_session__process_events(struct perf_session *self,
750 u64 data_offset, u64 data_size,
751 u64 file_size, struct perf_event_ops *ops)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200752{
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200753 int err, mmap_prot, mmap_flags;
754 u64 head, shift;
755 u64 offset = 0;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200756 size_t page_size;
757 event_t *event;
758 uint32_t size;
759 char *buf;
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300760 struct ui_progress *progress = ui_progress__new("Processing events...",
761 self->size);
762 if (progress == NULL)
763 return -1;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200764
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200765 perf_event_ops__fill_defaults(ops);
766
Arnaldo Carvalho de Melo1b759622010-01-14 18:30:04 -0200767 page_size = sysconf(_SC_PAGESIZE);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200768
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200769 head = data_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200770 shift = page_size * (head / page_size);
771 offset += shift;
772 head -= shift;
773
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200774 mmap_prot = PROT_READ;
775 mmap_flags = MAP_SHARED;
776
777 if (self->header.needs_swap) {
778 mmap_prot |= PROT_WRITE;
779 mmap_flags = MAP_PRIVATE;
780 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200781remap:
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200782 buf = mmap(NULL, page_size * self->mmap_window, mmap_prot,
783 mmap_flags, self->fd, offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200784 if (buf == MAP_FAILED) {
785 pr_err("failed to mmap file\n");
786 err = -errno;
787 goto out_err;
788 }
789
790more:
791 event = (event_t *)(buf + head);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300792 ui_progress__update(progress, offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200793
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200794 if (self->header.needs_swap)
795 perf_event_header__bswap(&event->header);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200796 size = event->header.size;
797 if (size == 0)
798 size = 8;
799
800 if (head + event->header.size >= page_size * self->mmap_window) {
801 int munmap_ret;
802
803 shift = page_size * (head / page_size);
804
805 munmap_ret = munmap(buf, page_size * self->mmap_window);
806 assert(munmap_ret == 0);
807
808 offset += shift;
809 head -= shift;
810 goto remap;
811 }
812
813 size = event->header.size;
814
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200815 dump_printf("\n%#Lx [%#x]: event: %d\n",
Arnaldo Carvalho de Melo0d755032010-01-14 12:23:09 -0200816 offset + head, event->header.size, event->header.type);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200817
818 if (size == 0 ||
819 perf_session__process_event(self, event, ops, offset, head) < 0) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200820 dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n",
Arnaldo Carvalho de Melo0d755032010-01-14 12:23:09 -0200821 offset + head, event->header.size,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200822 event->header.type);
823 /*
824 * assume we lost track of the stream, check alignment, and
825 * increment a single u64 in the hope to catch on again 'soon'.
826 */
827 if (unlikely(head & 7))
828 head &= ~7ULL;
829
830 size = 8;
831 }
832
833 head += size;
834
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200835 if (offset + head >= data_offset + data_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200836 goto done;
837
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200838 if (offset + head < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200839 goto more;
840done:
841 err = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200842 /* do the final flush for ordered samples */
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200843 self->ordered_samples.next_flush = ULLONG_MAX;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200844 flush_sample_queue(self, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200845out_err:
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300846 ui_progress__delete(progress);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200847 return err;
848}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -0200849
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200850int perf_session__process_events(struct perf_session *self,
851 struct perf_event_ops *ops)
852{
853 int err;
854
855 if (perf_session__register_idle_thread(self) == NULL)
856 return -ENOMEM;
857
Tom Zanussi8dc58102010-04-01 23:59:15 -0500858 if (!self->fd_pipe)
859 err = __perf_session__process_events(self,
860 self->header.data_offset,
861 self->header.data_size,
862 self->size, ops);
863 else
864 err = __perf_session__process_pipe_events(self, ops);
Dave Martin88ca8952010-07-27 11:46:12 -0300865
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200866 return err;
867}
868
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200869bool perf_session__has_traces(struct perf_session *self, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -0200870{
871 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200872 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
873 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -0200874 }
875
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200876 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -0200877}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200878
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800879int perf_session__set_kallsyms_ref_reloc_sym(struct map **maps,
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200880 const char *symbol_name,
881 u64 addr)
882{
883 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200884 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800885 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200886
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800887 ref = zalloc(sizeof(struct ref_reloc_sym));
888 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200889 return -ENOMEM;
890
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800891 ref->name = strdup(symbol_name);
892 if (ref->name == NULL) {
893 free(ref);
894 return -ENOMEM;
895 }
896
897 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200898 if (bracket)
899 *bracket = '\0';
900
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800901 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200902
903 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800904 struct kmap *kmap = map__kmap(maps[i]);
905 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200906 }
907
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200908 return 0;
909}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300910
911size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
912{
913 return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
914 __dsos__fprintf(&self->host_machine.user_dsos, fp) +
915 machines__fprintf_dsos(&self->machines, fp);
916}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300917
918size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
919 bool with_hits)
920{
921 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
922 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
923}