blob: 9401909fa283c02756dcd5ca7193fc7c29bd67fe [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>
8
9#include "session.h"
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -020010#include "sort.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020011#include "util.h"
12
13static int perf_session__open(struct perf_session *self, bool force)
14{
15 struct stat input_stat;
16
Tom Zanussi8dc58102010-04-01 23:59:15 -050017 if (!strcmp(self->filename, "-")) {
18 self->fd_pipe = true;
19 self->fd = STDIN_FILENO;
20
21 if (perf_header__read(self, self->fd) < 0)
22 pr_err("incompatible file format");
23
24 return 0;
25 }
26
Xiao Guangrongf887f302010-02-04 16:46:42 +080027 self->fd = open(self->filename, O_RDONLY);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020028 if (self->fd < 0) {
29 pr_err("failed to open file: %s", self->filename);
30 if (!strcmp(self->filename, "perf.data"))
31 pr_err(" (try 'perf record' first)");
32 pr_err("\n");
33 return -errno;
34 }
35
36 if (fstat(self->fd, &input_stat) < 0)
37 goto out_close;
38
39 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
40 pr_err("file %s not owned by current user or root\n",
41 self->filename);
42 goto out_close;
43 }
44
45 if (!input_stat.st_size) {
46 pr_info("zero-sized file (%s), nothing to do!\n",
47 self->filename);
48 goto out_close;
49 }
50
Tom Zanussi8dc58102010-04-01 23:59:15 -050051 if (perf_header__read(self, self->fd) < 0) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020052 pr_err("incompatible file format");
53 goto out_close;
54 }
55
56 self->size = input_stat.st_size;
57 return 0;
58
59out_close:
60 close(self->fd);
61 self->fd = -1;
62 return -1;
63}
64
Tom Zanussi8dc58102010-04-01 23:59:15 -050065void perf_session__update_sample_type(struct perf_session *self)
66{
67 self->sample_type = perf_header__sample_type(&self->header);
68}
69
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080070int perf_session__create_kernel_maps(struct perf_session *self)
71{
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030072 struct rb_root *machines = &self->machines;
73 int ret = machines__create_kernel_maps(machines, HOST_KERNEL_ID);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080074
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080075 if (ret >= 0)
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030076 ret = machines__create_guest_kernel_maps(machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080077 return ret;
78}
79
Tom Zanussi454c4072010-05-01 01:41:20 -050080struct perf_session *perf_session__new(const char *filename, int mode, bool force, bool repipe)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020081{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020082 size_t len = filename ? strlen(filename) + 1 : 0;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020083 struct perf_session *self = zalloc(sizeof(*self) + len);
84
85 if (self == NULL)
86 goto out;
87
88 if (perf_header__init(&self->header) < 0)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -020089 goto out_free;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020090
91 memcpy(self->filename, filename, len);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020092 self->threads = RB_ROOT;
Eric B Munsoncb8f0932010-03-05 12:51:07 -030093 self->stats_by_id = RB_ROOT;
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020094 self->last_match = NULL;
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -020095 self->mmap_window = 32;
96 self->cwd = NULL;
97 self->cwdlen = 0;
Arnaldo Carvalho de Melo31d337c2009-12-27 21:37:03 -020098 self->unknown_events = 0;
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 Melo94c744b2009-12-11 21:24:02 -0200102
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200103 if (mode == O_RDONLY) {
104 if (perf_session__open(self, force) < 0)
105 goto out_delete;
106 } else if (mode == O_WRONLY) {
107 /*
108 * In O_RDONLY mode this will be performed when reading the
109 * kernel MMAP event, in event__process_mmap().
110 */
111 if (perf_session__create_kernel_maps(self) < 0)
112 goto out_delete;
113 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200114
Tom Zanussi8dc58102010-04-01 23:59:15 -0500115 perf_session__update_sample_type(self);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200116out:
117 return self;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200118out_free:
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200119 free(self);
120 return NULL;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200121out_delete:
122 perf_session__delete(self);
123 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200124}
125
126void perf_session__delete(struct perf_session *self)
127{
128 perf_header__exit(&self->header);
129 close(self->fd);
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -0200130 free(self->cwd);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200131 free(self);
132}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200133
134static bool symbol__match_parent_regex(struct symbol *sym)
135{
136 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
137 return 1;
138
139 return 0;
140}
141
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300142struct map_symbol *perf_session__resolve_callchain(struct perf_session *self,
143 struct thread *thread,
144 struct ip_callchain *chain,
145 struct symbol **parent)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200146{
147 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200148 unsigned int i;
Arnaldo Carvalho de Meload5b2172010-04-02 10:04:18 -0300149 struct map_symbol *syms = calloc(chain->nr, sizeof(*syms));
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200150
Arnaldo Carvalho de Meload5b2172010-04-02 10:04:18 -0300151 if (!syms)
152 return NULL;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200153
154 for (i = 0; i < chain->nr; i++) {
155 u64 ip = chain->ips[i];
156 struct addr_location al;
157
158 if (ip >= PERF_CONTEXT_MAX) {
159 switch (ip) {
160 case PERF_CONTEXT_HV:
161 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
162 case PERF_CONTEXT_KERNEL:
163 cpumode = PERF_RECORD_MISC_KERNEL; break;
164 case PERF_CONTEXT_USER:
165 cpumode = PERF_RECORD_MISC_USER; break;
166 default:
167 break;
168 }
169 continue;
170 }
171
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800172 al.filtered = false;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200173 thread__find_addr_location(thread, self, cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800174 MAP__FUNCTION, thread->pid, ip, &al, NULL);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200175 if (al.sym != NULL) {
176 if (sort__has_parent && !*parent &&
177 symbol__match_parent_regex(al.sym))
178 *parent = al.sym;
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200179 if (!symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200180 break;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300181 syms[i].map = al.map;
182 syms[i].sym = al.sym;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200183 }
184 }
185
186 return syms;
187}
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200188
189static int process_event_stub(event_t *event __used,
190 struct perf_session *session __used)
191{
192 dump_printf(": unhandled!\n");
193 return 0;
194}
195
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200196static int process_finished_round_stub(event_t *event __used,
197 struct perf_session *session __used,
198 struct perf_event_ops *ops __used)
199{
200 dump_printf(": unhandled!\n");
201 return 0;
202}
203
204static int process_finished_round(event_t *event,
205 struct perf_session *session,
206 struct perf_event_ops *ops);
207
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200208static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
209{
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200210 if (handler->sample == NULL)
211 handler->sample = process_event_stub;
212 if (handler->mmap == NULL)
213 handler->mmap = process_event_stub;
214 if (handler->comm == NULL)
215 handler->comm = process_event_stub;
216 if (handler->fork == NULL)
217 handler->fork = process_event_stub;
218 if (handler->exit == NULL)
219 handler->exit = process_event_stub;
220 if (handler->lost == NULL)
221 handler->lost = process_event_stub;
222 if (handler->read == NULL)
223 handler->read = process_event_stub;
224 if (handler->throttle == NULL)
225 handler->throttle = process_event_stub;
226 if (handler->unthrottle == NULL)
227 handler->unthrottle = process_event_stub;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500228 if (handler->attr == NULL)
229 handler->attr = process_event_stub;
Tom Zanussicd19a032010-04-01 23:59:20 -0500230 if (handler->event_type == NULL)
231 handler->event_type = process_event_stub;
Tom Zanussi92155452010-04-01 23:59:21 -0500232 if (handler->tracing_data == NULL)
233 handler->tracing_data = process_event_stub;
Tom Zanussic7929e42010-04-01 23:59:22 -0500234 if (handler->build_id == NULL)
235 handler->build_id = process_event_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200236 if (handler->finished_round == NULL) {
237 if (handler->ordered_samples)
238 handler->finished_round = process_finished_round;
239 else
240 handler->finished_round = process_finished_round_stub;
241 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200242}
243
244static const char *event__name[] = {
245 [0] = "TOTAL",
246 [PERF_RECORD_MMAP] = "MMAP",
247 [PERF_RECORD_LOST] = "LOST",
248 [PERF_RECORD_COMM] = "COMM",
249 [PERF_RECORD_EXIT] = "EXIT",
250 [PERF_RECORD_THROTTLE] = "THROTTLE",
251 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
252 [PERF_RECORD_FORK] = "FORK",
253 [PERF_RECORD_READ] = "READ",
254 [PERF_RECORD_SAMPLE] = "SAMPLE",
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500255 [PERF_RECORD_HEADER_ATTR] = "ATTR",
Tom Zanussicd19a032010-04-01 23:59:20 -0500256 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
Tom Zanussi92155452010-04-01 23:59:21 -0500257 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
Tom Zanussic7929e42010-04-01 23:59:22 -0500258 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200259};
260
Tom Zanussi8dc58102010-04-01 23:59:15 -0500261unsigned long event__total[PERF_RECORD_HEADER_MAX];
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200262
263void event__print_totals(void)
264{
265 int i;
Tom Zanussi8dc58102010-04-01 23:59:15 -0500266 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
267 if (!event__name[i])
268 continue;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200269 pr_info("%10s events: %10ld\n",
270 event__name[i], event__total[i]);
Tom Zanussi8dc58102010-04-01 23:59:15 -0500271 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200272}
273
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200274void mem_bswap_64(void *src, int byte_size)
275{
276 u64 *m = src;
277
278 while (byte_size > 0) {
279 *m = bswap_64(*m);
280 byte_size -= sizeof(u64);
281 ++m;
282 }
283}
284
285static void event__all64_swap(event_t *self)
286{
287 struct perf_event_header *hdr = &self->header;
288 mem_bswap_64(hdr + 1, self->header.size - sizeof(*hdr));
289}
290
291static void event__comm_swap(event_t *self)
292{
293 self->comm.pid = bswap_32(self->comm.pid);
294 self->comm.tid = bswap_32(self->comm.tid);
295}
296
297static void event__mmap_swap(event_t *self)
298{
299 self->mmap.pid = bswap_32(self->mmap.pid);
300 self->mmap.tid = bswap_32(self->mmap.tid);
301 self->mmap.start = bswap_64(self->mmap.start);
302 self->mmap.len = bswap_64(self->mmap.len);
303 self->mmap.pgoff = bswap_64(self->mmap.pgoff);
304}
305
306static void event__task_swap(event_t *self)
307{
308 self->fork.pid = bswap_32(self->fork.pid);
309 self->fork.tid = bswap_32(self->fork.tid);
310 self->fork.ppid = bswap_32(self->fork.ppid);
311 self->fork.ptid = bswap_32(self->fork.ptid);
312 self->fork.time = bswap_64(self->fork.time);
313}
314
315static void event__read_swap(event_t *self)
316{
317 self->read.pid = bswap_32(self->read.pid);
318 self->read.tid = bswap_32(self->read.tid);
319 self->read.value = bswap_64(self->read.value);
320 self->read.time_enabled = bswap_64(self->read.time_enabled);
321 self->read.time_running = bswap_64(self->read.time_running);
322 self->read.id = bswap_64(self->read.id);
323}
324
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500325static void event__attr_swap(event_t *self)
326{
327 size_t size;
328
329 self->attr.attr.type = bswap_32(self->attr.attr.type);
330 self->attr.attr.size = bswap_32(self->attr.attr.size);
331 self->attr.attr.config = bswap_64(self->attr.attr.config);
332 self->attr.attr.sample_period = bswap_64(self->attr.attr.sample_period);
333 self->attr.attr.sample_type = bswap_64(self->attr.attr.sample_type);
334 self->attr.attr.read_format = bswap_64(self->attr.attr.read_format);
335 self->attr.attr.wakeup_events = bswap_32(self->attr.attr.wakeup_events);
336 self->attr.attr.bp_type = bswap_32(self->attr.attr.bp_type);
337 self->attr.attr.bp_addr = bswap_64(self->attr.attr.bp_addr);
338 self->attr.attr.bp_len = bswap_64(self->attr.attr.bp_len);
339
340 size = self->header.size;
341 size -= (void *)&self->attr.id - (void *)self;
342 mem_bswap_64(self->attr.id, size);
343}
344
Tom Zanussicd19a032010-04-01 23:59:20 -0500345static void event__event_type_swap(event_t *self)
346{
347 self->event_type.event_type.event_id =
348 bswap_64(self->event_type.event_type.event_id);
349}
350
Tom Zanussi92155452010-04-01 23:59:21 -0500351static void event__tracing_data_swap(event_t *self)
352{
353 self->tracing_data.size = bswap_32(self->tracing_data.size);
354}
355
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200356typedef void (*event__swap_op)(event_t *self);
357
358static event__swap_op event__swap_ops[] = {
359 [PERF_RECORD_MMAP] = event__mmap_swap,
360 [PERF_RECORD_COMM] = event__comm_swap,
361 [PERF_RECORD_FORK] = event__task_swap,
362 [PERF_RECORD_EXIT] = event__task_swap,
363 [PERF_RECORD_LOST] = event__all64_swap,
364 [PERF_RECORD_READ] = event__read_swap,
365 [PERF_RECORD_SAMPLE] = event__all64_swap,
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500366 [PERF_RECORD_HEADER_ATTR] = event__attr_swap,
Tom Zanussicd19a032010-04-01 23:59:20 -0500367 [PERF_RECORD_HEADER_EVENT_TYPE] = event__event_type_swap,
Tom Zanussi92155452010-04-01 23:59:21 -0500368 [PERF_RECORD_HEADER_TRACING_DATA] = event__tracing_data_swap,
Tom Zanussic7929e42010-04-01 23:59:22 -0500369 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
Tom Zanussi8dc58102010-04-01 23:59:15 -0500370 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200371};
372
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200373struct sample_queue {
374 u64 timestamp;
375 struct sample_event *event;
376 struct list_head list;
377};
378
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200379static void flush_sample_queue(struct perf_session *s,
380 struct perf_event_ops *ops)
381{
382 struct list_head *head = &s->ordered_samples.samples_head;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200383 u64 limit = s->ordered_samples.next_flush;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200384 struct sample_queue *tmp, *iter;
385
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200386 if (!ops->ordered_samples || !limit)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200387 return;
388
389 list_for_each_entry_safe(iter, tmp, head, list) {
390 if (iter->timestamp > limit)
391 return;
392
393 if (iter == s->ordered_samples.last_inserted)
394 s->ordered_samples.last_inserted = NULL;
395
396 ops->sample((event_t *)iter->event, s);
397
398 s->ordered_samples.last_flush = iter->timestamp;
399 list_del(&iter->list);
400 free(iter->event);
401 free(iter);
402 }
403}
404
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200405/*
406 * When perf record finishes a pass on every buffers, it records this pseudo
407 * event.
408 * We record the max timestamp t found in the pass n.
409 * Assuming these timestamps are monotonic across cpus, we know that if
410 * a buffer still has events with timestamps below t, they will be all
411 * available and then read in the pass n + 1.
412 * Hence when we start to read the pass n + 2, we can safely flush every
413 * events with timestamps below t.
414 *
415 * ============ PASS n =================
416 * CPU 0 | CPU 1
417 * |
418 * cnt1 timestamps | cnt2 timestamps
419 * 1 | 2
420 * 2 | 3
421 * - | 4 <--- max recorded
422 *
423 * ============ PASS n + 1 ==============
424 * CPU 0 | CPU 1
425 * |
426 * cnt1 timestamps | cnt2 timestamps
427 * 3 | 5
428 * 4 | 6
429 * 5 | 7 <---- max recorded
430 *
431 * Flush every events below timestamp 4
432 *
433 * ============ PASS n + 2 ==============
434 * CPU 0 | CPU 1
435 * |
436 * cnt1 timestamps | cnt2 timestamps
437 * 6 | 8
438 * 7 | 9
439 * - | 10
440 *
441 * Flush every events below timestamp 7
442 * etc...
443 */
444static int process_finished_round(event_t *event __used,
445 struct perf_session *session,
446 struct perf_event_ops *ops)
447{
448 flush_sample_queue(session, ops);
449 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
450
451 return 0;
452}
453
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200454static void __queue_sample_end(struct sample_queue *new, struct list_head *head)
455{
456 struct sample_queue *iter;
457
458 list_for_each_entry_reverse(iter, head, list) {
459 if (iter->timestamp < new->timestamp) {
460 list_add(&new->list, &iter->list);
461 return;
462 }
463 }
464
465 list_add(&new->list, head);
466}
467
468static void __queue_sample_before(struct sample_queue *new,
469 struct sample_queue *iter,
470 struct list_head *head)
471{
472 list_for_each_entry_continue_reverse(iter, head, list) {
473 if (iter->timestamp < new->timestamp) {
474 list_add(&new->list, &iter->list);
475 return;
476 }
477 }
478
479 list_add(&new->list, head);
480}
481
482static void __queue_sample_after(struct sample_queue *new,
483 struct sample_queue *iter,
484 struct list_head *head)
485{
486 list_for_each_entry_continue(iter, head, list) {
487 if (iter->timestamp > new->timestamp) {
488 list_add_tail(&new->list, &iter->list);
489 return;
490 }
491 }
492 list_add_tail(&new->list, head);
493}
494
495/* The queue is ordered by time */
496static void __queue_sample_event(struct sample_queue *new,
497 struct perf_session *s)
498{
499 struct sample_queue *last_inserted = s->ordered_samples.last_inserted;
500 struct list_head *head = &s->ordered_samples.samples_head;
501
502
503 if (!last_inserted) {
504 __queue_sample_end(new, head);
505 return;
506 }
507
508 /*
509 * Most of the time the current event has a timestamp
510 * very close to the last event inserted, unless we just switched
511 * to another event buffer. Having a sorting based on a list and
512 * on the last inserted event that is close to the current one is
513 * probably more efficient than an rbtree based sorting.
514 */
515 if (last_inserted->timestamp >= new->timestamp)
516 __queue_sample_before(new, last_inserted, head);
517 else
518 __queue_sample_after(new, last_inserted, head);
519}
520
521static int queue_sample_event(event_t *event, struct sample_data *data,
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200522 struct perf_session *s)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200523{
524 u64 timestamp = data->time;
525 struct sample_queue *new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200526
527
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200528 if (timestamp < s->ordered_samples.last_flush) {
529 printf("Warning: Timestamp below last timeslice flush\n");
530 return -EINVAL;
531 }
532
533 new = malloc(sizeof(*new));
534 if (!new)
535 return -ENOMEM;
536
537 new->timestamp = timestamp;
538
539 new->event = malloc(event->header.size);
540 if (!new->event) {
541 free(new);
542 return -ENOMEM;
543 }
544
545 memcpy(new->event, event, event->header.size);
546
547 __queue_sample_event(new, s);
548 s->ordered_samples.last_inserted = new;
549
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200550 if (new->timestamp > s->ordered_samples.max_timestamp)
551 s->ordered_samples.max_timestamp = new->timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200552
553 return 0;
554}
555
556static int perf_session__process_sample(event_t *event, struct perf_session *s,
557 struct perf_event_ops *ops)
558{
559 struct sample_data data;
560
561 if (!ops->ordered_samples)
562 return ops->sample(event, s);
563
564 bzero(&data, sizeof(struct sample_data));
565 event__parse_sample(event, s->sample_type, &data);
566
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200567 queue_sample_event(event, &data, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200568
569 return 0;
570}
571
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200572static int perf_session__process_event(struct perf_session *self,
573 event_t *event,
574 struct perf_event_ops *ops,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200575 u64 offset, u64 head)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200576{
577 trace_event(event);
578
Tom Zanussi8dc58102010-04-01 23:59:15 -0500579 if (event->header.type < PERF_RECORD_HEADER_MAX) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200580 dump_printf("%#Lx [%#x]: PERF_RECORD_%s",
Arnaldo Carvalho de Melo0d755032010-01-14 12:23:09 -0200581 offset + head, event->header.size,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200582 event__name[event->header.type]);
583 ++event__total[0];
584 ++event__total[event->header.type];
585 }
586
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200587 if (self->header.needs_swap && event__swap_ops[event->header.type])
588 event__swap_ops[event->header.type](event);
589
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200590 switch (event->header.type) {
591 case PERF_RECORD_SAMPLE:
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200592 return perf_session__process_sample(event, self, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200593 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200594 return ops->mmap(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200595 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200596 return ops->comm(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200597 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200598 return ops->fork(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200599 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200600 return ops->exit(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200601 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200602 return ops->lost(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200603 case PERF_RECORD_READ:
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200604 return ops->read(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200605 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200606 return ops->throttle(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200607 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200608 return ops->unthrottle(event, self);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500609 case PERF_RECORD_HEADER_ATTR:
610 return ops->attr(event, self);
Tom Zanussicd19a032010-04-01 23:59:20 -0500611 case PERF_RECORD_HEADER_EVENT_TYPE:
612 return ops->event_type(event, self);
Tom Zanussi92155452010-04-01 23:59:21 -0500613 case PERF_RECORD_HEADER_TRACING_DATA:
614 /* setup for reading amidst mmap */
615 lseek(self->fd, offset + head, SEEK_SET);
616 return ops->tracing_data(event, self);
Tom Zanussic7929e42010-04-01 23:59:22 -0500617 case PERF_RECORD_HEADER_BUILD_ID:
618 return ops->build_id(event, self);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200619 case PERF_RECORD_FINISHED_ROUND:
620 return ops->finished_round(event, self, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200621 default:
Arnaldo Carvalho de Melo31d337c2009-12-27 21:37:03 -0200622 self->unknown_events++;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200623 return -1;
624 }
625}
626
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200627void perf_event_header__bswap(struct perf_event_header *self)
628{
629 self->type = bswap_32(self->type);
630 self->misc = bswap_16(self->misc);
631 self->size = bswap_16(self->size);
632}
633
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200634static struct thread *perf_session__register_idle_thread(struct perf_session *self)
635{
636 struct thread *thread = perf_session__findnew(self, 0);
637
638 if (thread == NULL || thread__set_comm(thread, "swapper")) {
639 pr_err("problem inserting idle task.\n");
640 thread = NULL;
641 }
642
643 return thread;
644}
645
Tom Zanussi8dc58102010-04-01 23:59:15 -0500646int do_read(int fd, void *buf, size_t size)
647{
648 void *buf_start = buf;
649
650 while (size) {
651 int ret = read(fd, buf, size);
652
653 if (ret <= 0)
654 return ret;
655
656 size -= ret;
657 buf += ret;
658 }
659
660 return buf - buf_start;
661}
662
663#define session_done() (*(volatile int *)(&session_done))
664volatile int session_done;
665
666static int __perf_session__process_pipe_events(struct perf_session *self,
667 struct perf_event_ops *ops)
668{
669 event_t event;
670 uint32_t size;
671 int skip = 0;
672 u64 head;
673 int err;
674 void *p;
675
676 perf_event_ops__fill_defaults(ops);
677
678 head = 0;
679more:
680 err = do_read(self->fd, &event, sizeof(struct perf_event_header));
681 if (err <= 0) {
682 if (err == 0)
683 goto done;
684
685 pr_err("failed to read event header\n");
686 goto out_err;
687 }
688
689 if (self->header.needs_swap)
690 perf_event_header__bswap(&event.header);
691
692 size = event.header.size;
693 if (size == 0)
694 size = 8;
695
696 p = &event;
697 p += sizeof(struct perf_event_header);
698
699 err = do_read(self->fd, p, size - sizeof(struct perf_event_header));
700 if (err <= 0) {
701 if (err == 0) {
702 pr_err("unexpected end of event stream\n");
703 goto done;
704 }
705
706 pr_err("failed to read event data\n");
707 goto out_err;
708 }
709
710 if (size == 0 ||
711 (skip = perf_session__process_event(self, &event, ops,
712 0, head)) < 0) {
713 dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n",
714 head, event.header.size, event.header.type);
715 /*
716 * assume we lost track of the stream, check alignment, and
717 * increment a single u64 in the hope to catch on again 'soon'.
718 */
719 if (unlikely(head & 7))
720 head &= ~7ULL;
721
722 size = 8;
723 }
724
725 head += size;
726
727 dump_printf("\n%#Lx [%#x]: event: %d\n",
728 head, event.header.size, event.header.type);
729
730 if (skip > 0)
731 head += skip;
732
733 if (!session_done())
734 goto more;
735done:
736 err = 0;
737out_err:
738 return err;
739}
740
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200741int __perf_session__process_events(struct perf_session *self,
742 u64 data_offset, u64 data_size,
743 u64 file_size, struct perf_event_ops *ops)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200744{
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200745 int err, mmap_prot, mmap_flags;
746 u64 head, shift;
747 u64 offset = 0;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200748 size_t page_size;
749 event_t *event;
750 uint32_t size;
751 char *buf;
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300752 struct ui_progress *progress = ui_progress__new("Processing events...",
753 self->size);
754 if (progress == NULL)
755 return -1;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200756
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200757 perf_event_ops__fill_defaults(ops);
758
Arnaldo Carvalho de Melo1b759622010-01-14 18:30:04 -0200759 page_size = sysconf(_SC_PAGESIZE);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200760
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200761 head = data_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200762 shift = page_size * (head / page_size);
763 offset += shift;
764 head -= shift;
765
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200766 mmap_prot = PROT_READ;
767 mmap_flags = MAP_SHARED;
768
769 if (self->header.needs_swap) {
770 mmap_prot |= PROT_WRITE;
771 mmap_flags = MAP_PRIVATE;
772 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200773remap:
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200774 buf = mmap(NULL, page_size * self->mmap_window, mmap_prot,
775 mmap_flags, self->fd, offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200776 if (buf == MAP_FAILED) {
777 pr_err("failed to mmap file\n");
778 err = -errno;
779 goto out_err;
780 }
781
782more:
783 event = (event_t *)(buf + head);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300784 ui_progress__update(progress, offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200785
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200786 if (self->header.needs_swap)
787 perf_event_header__bswap(&event->header);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200788 size = event->header.size;
789 if (size == 0)
790 size = 8;
791
792 if (head + event->header.size >= page_size * self->mmap_window) {
793 int munmap_ret;
794
795 shift = page_size * (head / page_size);
796
797 munmap_ret = munmap(buf, page_size * self->mmap_window);
798 assert(munmap_ret == 0);
799
800 offset += shift;
801 head -= shift;
802 goto remap;
803 }
804
805 size = event->header.size;
806
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200807 dump_printf("\n%#Lx [%#x]: event: %d\n",
Arnaldo Carvalho de Melo0d755032010-01-14 12:23:09 -0200808 offset + head, event->header.size, event->header.type);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200809
810 if (size == 0 ||
811 perf_session__process_event(self, event, ops, offset, head) < 0) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200812 dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n",
Arnaldo Carvalho de Melo0d755032010-01-14 12:23:09 -0200813 offset + head, event->header.size,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200814 event->header.type);
815 /*
816 * assume we lost track of the stream, check alignment, and
817 * increment a single u64 in the hope to catch on again 'soon'.
818 */
819 if (unlikely(head & 7))
820 head &= ~7ULL;
821
822 size = 8;
823 }
824
825 head += size;
826
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200827 if (offset + head >= data_offset + data_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200828 goto done;
829
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200830 if (offset + head < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200831 goto more;
832done:
833 err = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200834 /* do the final flush for ordered samples */
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200835 self->ordered_samples.next_flush = ULLONG_MAX;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200836 flush_sample_queue(self, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200837out_err:
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300838 ui_progress__delete(progress);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200839 return err;
840}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -0200841
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200842int perf_session__process_events(struct perf_session *self,
843 struct perf_event_ops *ops)
844{
845 int err;
846
847 if (perf_session__register_idle_thread(self) == NULL)
848 return -ENOMEM;
849
850 if (!symbol_conf.full_paths) {
851 char bf[PATH_MAX];
852
853 if (getcwd(bf, sizeof(bf)) == NULL) {
854 err = -errno;
855out_getcwd_err:
856 pr_err("failed to get the current directory\n");
857 goto out_err;
858 }
859 self->cwd = strdup(bf);
860 if (self->cwd == NULL) {
861 err = -ENOMEM;
862 goto out_getcwd_err;
863 }
864 self->cwdlen = strlen(self->cwd);
865 }
866
Tom Zanussi8dc58102010-04-01 23:59:15 -0500867 if (!self->fd_pipe)
868 err = __perf_session__process_events(self,
869 self->header.data_offset,
870 self->header.data_size,
871 self->size, ops);
872 else
873 err = __perf_session__process_pipe_events(self, ops);
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200874out_err:
875 return err;
876}
877
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200878bool perf_session__has_traces(struct perf_session *self, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -0200879{
880 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200881 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
882 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -0200883 }
884
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200885 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -0200886}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200887
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800888int perf_session__set_kallsyms_ref_reloc_sym(struct map **maps,
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200889 const char *symbol_name,
890 u64 addr)
891{
892 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200893 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800894 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200895
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800896 ref = zalloc(sizeof(struct ref_reloc_sym));
897 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200898 return -ENOMEM;
899
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800900 ref->name = strdup(symbol_name);
901 if (ref->name == NULL) {
902 free(ref);
903 return -ENOMEM;
904 }
905
906 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200907 if (bracket)
908 *bracket = '\0';
909
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800910 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200911
912 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800913 struct kmap *kmap = map__kmap(maps[i]);
914 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200915 }
916
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200917 return 0;
918}