blob: 8f83a1835766e5d1a6e509547d14559529bbba23 [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) {
30 pr_err("failed to open file: %s", self->filename);
31 if (!strcmp(self->filename, "perf.data"))
32 pr_err(" (try 'perf record' first)");
33 pr_err("\n");
34 return -errno;
35 }
36
37 if (fstat(self->fd, &input_stat) < 0)
38 goto out_close;
39
40 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
41 pr_err("file %s not owned by current user or root\n",
42 self->filename);
43 goto out_close;
44 }
45
46 if (!input_stat.st_size) {
47 pr_info("zero-sized file (%s), nothing to do!\n",
48 self->filename);
49 goto out_close;
50 }
51
Tom Zanussi8dc58102010-04-01 23:59:15 -050052 if (perf_header__read(self, self->fd) < 0) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020053 pr_err("incompatible file format");
54 goto out_close;
55 }
56
57 self->size = input_stat.st_size;
58 return 0;
59
60out_close:
61 close(self->fd);
62 self->fd = -1;
63 return -1;
64}
65
Tom Zanussi8dc58102010-04-01 23:59:15 -050066void perf_session__update_sample_type(struct perf_session *self)
67{
68 self->sample_type = perf_header__sample_type(&self->header);
69}
70
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080071int perf_session__create_kernel_maps(struct perf_session *self)
72{
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -030073 int ret = machine__create_kernel_maps(&self->host_machine);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080074
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080075 if (ret >= 0)
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -030076 ret = machines__create_guest_kernel_maps(&self->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;
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -030093 self->hists_tree = 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 Melo23346f22010-04-27 21:17:50 -030098 self->machines = RB_ROOT;
Tom Zanussi454c4072010-05-01 01:41:20 -050099 self->repipe = repipe;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200100 INIT_LIST_HEAD(&self->ordered_samples.samples_head);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300101 machine__init(&self->host_machine, "", HOST_KERNEL_ID);
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
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200244void mem_bswap_64(void *src, int byte_size)
245{
246 u64 *m = src;
247
248 while (byte_size > 0) {
249 *m = bswap_64(*m);
250 byte_size -= sizeof(u64);
251 ++m;
252 }
253}
254
255static void event__all64_swap(event_t *self)
256{
257 struct perf_event_header *hdr = &self->header;
258 mem_bswap_64(hdr + 1, self->header.size - sizeof(*hdr));
259}
260
261static void event__comm_swap(event_t *self)
262{
263 self->comm.pid = bswap_32(self->comm.pid);
264 self->comm.tid = bswap_32(self->comm.tid);
265}
266
267static void event__mmap_swap(event_t *self)
268{
269 self->mmap.pid = bswap_32(self->mmap.pid);
270 self->mmap.tid = bswap_32(self->mmap.tid);
271 self->mmap.start = bswap_64(self->mmap.start);
272 self->mmap.len = bswap_64(self->mmap.len);
273 self->mmap.pgoff = bswap_64(self->mmap.pgoff);
274}
275
276static void event__task_swap(event_t *self)
277{
278 self->fork.pid = bswap_32(self->fork.pid);
279 self->fork.tid = bswap_32(self->fork.tid);
280 self->fork.ppid = bswap_32(self->fork.ppid);
281 self->fork.ptid = bswap_32(self->fork.ptid);
282 self->fork.time = bswap_64(self->fork.time);
283}
284
285static void event__read_swap(event_t *self)
286{
287 self->read.pid = bswap_32(self->read.pid);
288 self->read.tid = bswap_32(self->read.tid);
289 self->read.value = bswap_64(self->read.value);
290 self->read.time_enabled = bswap_64(self->read.time_enabled);
291 self->read.time_running = bswap_64(self->read.time_running);
292 self->read.id = bswap_64(self->read.id);
293}
294
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500295static void event__attr_swap(event_t *self)
296{
297 size_t size;
298
299 self->attr.attr.type = bswap_32(self->attr.attr.type);
300 self->attr.attr.size = bswap_32(self->attr.attr.size);
301 self->attr.attr.config = bswap_64(self->attr.attr.config);
302 self->attr.attr.sample_period = bswap_64(self->attr.attr.sample_period);
303 self->attr.attr.sample_type = bswap_64(self->attr.attr.sample_type);
304 self->attr.attr.read_format = bswap_64(self->attr.attr.read_format);
305 self->attr.attr.wakeup_events = bswap_32(self->attr.attr.wakeup_events);
306 self->attr.attr.bp_type = bswap_32(self->attr.attr.bp_type);
307 self->attr.attr.bp_addr = bswap_64(self->attr.attr.bp_addr);
308 self->attr.attr.bp_len = bswap_64(self->attr.attr.bp_len);
309
310 size = self->header.size;
311 size -= (void *)&self->attr.id - (void *)self;
312 mem_bswap_64(self->attr.id, size);
313}
314
Tom Zanussicd19a032010-04-01 23:59:20 -0500315static void event__event_type_swap(event_t *self)
316{
317 self->event_type.event_type.event_id =
318 bswap_64(self->event_type.event_type.event_id);
319}
320
Tom Zanussi92155452010-04-01 23:59:21 -0500321static void event__tracing_data_swap(event_t *self)
322{
323 self->tracing_data.size = bswap_32(self->tracing_data.size);
324}
325
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200326typedef void (*event__swap_op)(event_t *self);
327
328static event__swap_op event__swap_ops[] = {
329 [PERF_RECORD_MMAP] = event__mmap_swap,
330 [PERF_RECORD_COMM] = event__comm_swap,
331 [PERF_RECORD_FORK] = event__task_swap,
332 [PERF_RECORD_EXIT] = event__task_swap,
333 [PERF_RECORD_LOST] = event__all64_swap,
334 [PERF_RECORD_READ] = event__read_swap,
335 [PERF_RECORD_SAMPLE] = event__all64_swap,
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500336 [PERF_RECORD_HEADER_ATTR] = event__attr_swap,
Tom Zanussicd19a032010-04-01 23:59:20 -0500337 [PERF_RECORD_HEADER_EVENT_TYPE] = event__event_type_swap,
Tom Zanussi92155452010-04-01 23:59:21 -0500338 [PERF_RECORD_HEADER_TRACING_DATA] = event__tracing_data_swap,
Tom Zanussic7929e42010-04-01 23:59:22 -0500339 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
Tom Zanussi8dc58102010-04-01 23:59:15 -0500340 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200341};
342
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200343struct sample_queue {
344 u64 timestamp;
345 struct sample_event *event;
346 struct list_head list;
347};
348
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200349static void flush_sample_queue(struct perf_session *s,
350 struct perf_event_ops *ops)
351{
352 struct list_head *head = &s->ordered_samples.samples_head;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200353 u64 limit = s->ordered_samples.next_flush;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200354 struct sample_queue *tmp, *iter;
355
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200356 if (!ops->ordered_samples || !limit)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200357 return;
358
359 list_for_each_entry_safe(iter, tmp, head, list) {
360 if (iter->timestamp > limit)
361 return;
362
363 if (iter == s->ordered_samples.last_inserted)
364 s->ordered_samples.last_inserted = NULL;
365
366 ops->sample((event_t *)iter->event, s);
367
368 s->ordered_samples.last_flush = iter->timestamp;
369 list_del(&iter->list);
370 free(iter->event);
371 free(iter);
372 }
373}
374
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200375/*
376 * When perf record finishes a pass on every buffers, it records this pseudo
377 * event.
378 * We record the max timestamp t found in the pass n.
379 * Assuming these timestamps are monotonic across cpus, we know that if
380 * a buffer still has events with timestamps below t, they will be all
381 * available and then read in the pass n + 1.
382 * Hence when we start to read the pass n + 2, we can safely flush every
383 * events with timestamps below t.
384 *
385 * ============ PASS n =================
386 * CPU 0 | CPU 1
387 * |
388 * cnt1 timestamps | cnt2 timestamps
389 * 1 | 2
390 * 2 | 3
391 * - | 4 <--- max recorded
392 *
393 * ============ PASS n + 1 ==============
394 * CPU 0 | CPU 1
395 * |
396 * cnt1 timestamps | cnt2 timestamps
397 * 3 | 5
398 * 4 | 6
399 * 5 | 7 <---- max recorded
400 *
401 * Flush every events below timestamp 4
402 *
403 * ============ PASS n + 2 ==============
404 * CPU 0 | CPU 1
405 * |
406 * cnt1 timestamps | cnt2 timestamps
407 * 6 | 8
408 * 7 | 9
409 * - | 10
410 *
411 * Flush every events below timestamp 7
412 * etc...
413 */
414static int process_finished_round(event_t *event __used,
415 struct perf_session *session,
416 struct perf_event_ops *ops)
417{
418 flush_sample_queue(session, ops);
419 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
420
421 return 0;
422}
423
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200424static void __queue_sample_end(struct sample_queue *new, struct list_head *head)
425{
426 struct sample_queue *iter;
427
428 list_for_each_entry_reverse(iter, head, list) {
429 if (iter->timestamp < new->timestamp) {
430 list_add(&new->list, &iter->list);
431 return;
432 }
433 }
434
435 list_add(&new->list, head);
436}
437
438static void __queue_sample_before(struct sample_queue *new,
439 struct sample_queue *iter,
440 struct list_head *head)
441{
442 list_for_each_entry_continue_reverse(iter, head, list) {
443 if (iter->timestamp < new->timestamp) {
444 list_add(&new->list, &iter->list);
445 return;
446 }
447 }
448
449 list_add(&new->list, head);
450}
451
452static void __queue_sample_after(struct sample_queue *new,
453 struct sample_queue *iter,
454 struct list_head *head)
455{
456 list_for_each_entry_continue(iter, head, list) {
457 if (iter->timestamp > new->timestamp) {
458 list_add_tail(&new->list, &iter->list);
459 return;
460 }
461 }
462 list_add_tail(&new->list, head);
463}
464
465/* The queue is ordered by time */
466static void __queue_sample_event(struct sample_queue *new,
467 struct perf_session *s)
468{
469 struct sample_queue *last_inserted = s->ordered_samples.last_inserted;
470 struct list_head *head = &s->ordered_samples.samples_head;
471
472
473 if (!last_inserted) {
474 __queue_sample_end(new, head);
475 return;
476 }
477
478 /*
479 * Most of the time the current event has a timestamp
480 * very close to the last event inserted, unless we just switched
481 * to another event buffer. Having a sorting based on a list and
482 * on the last inserted event that is close to the current one is
483 * probably more efficient than an rbtree based sorting.
484 */
485 if (last_inserted->timestamp >= new->timestamp)
486 __queue_sample_before(new, last_inserted, head);
487 else
488 __queue_sample_after(new, last_inserted, head);
489}
490
491static int queue_sample_event(event_t *event, struct sample_data *data,
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200492 struct perf_session *s)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200493{
494 u64 timestamp = data->time;
495 struct sample_queue *new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200496
497
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200498 if (timestamp < s->ordered_samples.last_flush) {
499 printf("Warning: Timestamp below last timeslice flush\n");
500 return -EINVAL;
501 }
502
503 new = malloc(sizeof(*new));
504 if (!new)
505 return -ENOMEM;
506
507 new->timestamp = timestamp;
508
509 new->event = malloc(event->header.size);
510 if (!new->event) {
511 free(new);
512 return -ENOMEM;
513 }
514
515 memcpy(new->event, event, event->header.size);
516
517 __queue_sample_event(new, s);
518 s->ordered_samples.last_inserted = new;
519
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200520 if (new->timestamp > s->ordered_samples.max_timestamp)
521 s->ordered_samples.max_timestamp = new->timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200522
523 return 0;
524}
525
526static int perf_session__process_sample(event_t *event, struct perf_session *s,
527 struct perf_event_ops *ops)
528{
529 struct sample_data data;
530
531 if (!ops->ordered_samples)
532 return ops->sample(event, s);
533
534 bzero(&data, sizeof(struct sample_data));
535 event__parse_sample(event, s->sample_type, &data);
536
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200537 queue_sample_event(event, &data, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200538
539 return 0;
540}
541
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200542static int perf_session__process_event(struct perf_session *self,
543 event_t *event,
544 struct perf_event_ops *ops,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200545 u64 offset, u64 head)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200546{
547 trace_event(event);
548
Tom Zanussi8dc58102010-04-01 23:59:15 -0500549 if (event->header.type < PERF_RECORD_HEADER_MAX) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200550 dump_printf("%#Lx [%#x]: PERF_RECORD_%s",
Arnaldo Carvalho de Melo0d755032010-01-14 12:23:09 -0200551 offset + head, event->header.size,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200552 event__name[event->header.type]);
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300553 hists__inc_nr_events(&self->hists, event->header.type);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200554 }
555
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200556 if (self->header.needs_swap && event__swap_ops[event->header.type])
557 event__swap_ops[event->header.type](event);
558
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200559 switch (event->header.type) {
560 case PERF_RECORD_SAMPLE:
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200561 return perf_session__process_sample(event, self, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200562 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200563 return ops->mmap(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200564 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200565 return ops->comm(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200566 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200567 return ops->fork(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200568 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200569 return ops->exit(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200570 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200571 return ops->lost(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200572 case PERF_RECORD_READ:
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200573 return ops->read(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200574 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200575 return ops->throttle(event, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200576 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200577 return ops->unthrottle(event, self);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500578 case PERF_RECORD_HEADER_ATTR:
579 return ops->attr(event, self);
Tom Zanussicd19a032010-04-01 23:59:20 -0500580 case PERF_RECORD_HEADER_EVENT_TYPE:
581 return ops->event_type(event, self);
Tom Zanussi92155452010-04-01 23:59:21 -0500582 case PERF_RECORD_HEADER_TRACING_DATA:
583 /* setup for reading amidst mmap */
584 lseek(self->fd, offset + head, SEEK_SET);
585 return ops->tracing_data(event, self);
Tom Zanussic7929e42010-04-01 23:59:22 -0500586 case PERF_RECORD_HEADER_BUILD_ID:
587 return ops->build_id(event, self);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200588 case PERF_RECORD_FINISHED_ROUND:
589 return ops->finished_round(event, self, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200590 default:
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300591 ++self->hists.stats.nr_unknown_events;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200592 return -1;
593 }
594}
595
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200596void perf_event_header__bswap(struct perf_event_header *self)
597{
598 self->type = bswap_32(self->type);
599 self->misc = bswap_16(self->misc);
600 self->size = bswap_16(self->size);
601}
602
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200603static struct thread *perf_session__register_idle_thread(struct perf_session *self)
604{
605 struct thread *thread = perf_session__findnew(self, 0);
606
607 if (thread == NULL || thread__set_comm(thread, "swapper")) {
608 pr_err("problem inserting idle task.\n");
609 thread = NULL;
610 }
611
612 return thread;
613}
614
Tom Zanussi8dc58102010-04-01 23:59:15 -0500615int do_read(int fd, void *buf, size_t size)
616{
617 void *buf_start = buf;
618
619 while (size) {
620 int ret = read(fd, buf, size);
621
622 if (ret <= 0)
623 return ret;
624
625 size -= ret;
626 buf += ret;
627 }
628
629 return buf - buf_start;
630}
631
632#define session_done() (*(volatile int *)(&session_done))
633volatile int session_done;
634
635static int __perf_session__process_pipe_events(struct perf_session *self,
636 struct perf_event_ops *ops)
637{
638 event_t event;
639 uint32_t size;
640 int skip = 0;
641 u64 head;
642 int err;
643 void *p;
644
645 perf_event_ops__fill_defaults(ops);
646
647 head = 0;
648more:
649 err = do_read(self->fd, &event, sizeof(struct perf_event_header));
650 if (err <= 0) {
651 if (err == 0)
652 goto done;
653
654 pr_err("failed to read event header\n");
655 goto out_err;
656 }
657
658 if (self->header.needs_swap)
659 perf_event_header__bswap(&event.header);
660
661 size = event.header.size;
662 if (size == 0)
663 size = 8;
664
665 p = &event;
666 p += sizeof(struct perf_event_header);
667
Tom Zanussi794e43b2010-05-05 00:27:40 -0500668 if (size - sizeof(struct perf_event_header)) {
669 err = do_read(self->fd, p,
670 size - sizeof(struct perf_event_header));
671 if (err <= 0) {
672 if (err == 0) {
673 pr_err("unexpected end of event stream\n");
674 goto done;
675 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500676
Tom Zanussi794e43b2010-05-05 00:27:40 -0500677 pr_err("failed to read event data\n");
678 goto out_err;
679 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500680 }
681
682 if (size == 0 ||
683 (skip = perf_session__process_event(self, &event, ops,
684 0, head)) < 0) {
685 dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n",
686 head, event.header.size, event.header.type);
687 /*
688 * assume we lost track of the stream, check alignment, and
689 * increment a single u64 in the hope to catch on again 'soon'.
690 */
691 if (unlikely(head & 7))
692 head &= ~7ULL;
693
694 size = 8;
695 }
696
697 head += size;
698
699 dump_printf("\n%#Lx [%#x]: event: %d\n",
700 head, event.header.size, event.header.type);
701
702 if (skip > 0)
703 head += skip;
704
705 if (!session_done())
706 goto more;
707done:
708 err = 0;
709out_err:
710 return err;
711}
712
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200713int __perf_session__process_events(struct perf_session *self,
714 u64 data_offset, u64 data_size,
715 u64 file_size, struct perf_event_ops *ops)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200716{
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200717 int err, mmap_prot, mmap_flags;
718 u64 head, shift;
719 u64 offset = 0;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200720 size_t page_size;
721 event_t *event;
722 uint32_t size;
723 char *buf;
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300724 struct ui_progress *progress = ui_progress__new("Processing events...",
725 self->size);
726 if (progress == NULL)
727 return -1;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200728
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200729 perf_event_ops__fill_defaults(ops);
730
Arnaldo Carvalho de Melo1b759622010-01-14 18:30:04 -0200731 page_size = sysconf(_SC_PAGESIZE);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200732
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200733 head = data_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200734 shift = page_size * (head / page_size);
735 offset += shift;
736 head -= shift;
737
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200738 mmap_prot = PROT_READ;
739 mmap_flags = MAP_SHARED;
740
741 if (self->header.needs_swap) {
742 mmap_prot |= PROT_WRITE;
743 mmap_flags = MAP_PRIVATE;
744 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200745remap:
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200746 buf = mmap(NULL, page_size * self->mmap_window, mmap_prot,
747 mmap_flags, self->fd, offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200748 if (buf == MAP_FAILED) {
749 pr_err("failed to mmap file\n");
750 err = -errno;
751 goto out_err;
752 }
753
754more:
755 event = (event_t *)(buf + head);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300756 ui_progress__update(progress, offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200757
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200758 if (self->header.needs_swap)
759 perf_event_header__bswap(&event->header);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200760 size = event->header.size;
761 if (size == 0)
762 size = 8;
763
764 if (head + event->header.size >= page_size * self->mmap_window) {
765 int munmap_ret;
766
767 shift = page_size * (head / page_size);
768
769 munmap_ret = munmap(buf, page_size * self->mmap_window);
770 assert(munmap_ret == 0);
771
772 offset += shift;
773 head -= shift;
774 goto remap;
775 }
776
777 size = event->header.size;
778
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200779 dump_printf("\n%#Lx [%#x]: event: %d\n",
Arnaldo Carvalho de Melo0d755032010-01-14 12:23:09 -0200780 offset + head, event->header.size, event->header.type);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200781
782 if (size == 0 ||
783 perf_session__process_event(self, event, ops, offset, head) < 0) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200784 dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n",
Arnaldo Carvalho de Melo0d755032010-01-14 12:23:09 -0200785 offset + head, event->header.size,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200786 event->header.type);
787 /*
788 * assume we lost track of the stream, check alignment, and
789 * increment a single u64 in the hope to catch on again 'soon'.
790 */
791 if (unlikely(head & 7))
792 head &= ~7ULL;
793
794 size = 8;
795 }
796
797 head += size;
798
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200799 if (offset + head >= data_offset + data_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200800 goto done;
801
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200802 if (offset + head < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200803 goto more;
804done:
805 err = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200806 /* do the final flush for ordered samples */
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200807 self->ordered_samples.next_flush = ULLONG_MAX;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200808 flush_sample_queue(self, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200809out_err:
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300810 ui_progress__delete(progress);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200811 return err;
812}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -0200813
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200814int perf_session__process_events(struct perf_session *self,
815 struct perf_event_ops *ops)
816{
817 int err;
818
819 if (perf_session__register_idle_thread(self) == NULL)
820 return -ENOMEM;
821
822 if (!symbol_conf.full_paths) {
823 char bf[PATH_MAX];
824
825 if (getcwd(bf, sizeof(bf)) == NULL) {
826 err = -errno;
827out_getcwd_err:
828 pr_err("failed to get the current directory\n");
829 goto out_err;
830 }
831 self->cwd = strdup(bf);
832 if (self->cwd == NULL) {
833 err = -ENOMEM;
834 goto out_getcwd_err;
835 }
836 self->cwdlen = strlen(self->cwd);
837 }
838
Tom Zanussi8dc58102010-04-01 23:59:15 -0500839 if (!self->fd_pipe)
840 err = __perf_session__process_events(self,
841 self->header.data_offset,
842 self->header.data_size,
843 self->size, ops);
844 else
845 err = __perf_session__process_pipe_events(self, ops);
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200846out_err:
847 return err;
848}
849
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200850bool perf_session__has_traces(struct perf_session *self, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -0200851{
852 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200853 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
854 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -0200855 }
856
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200857 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -0200858}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200859
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800860int perf_session__set_kallsyms_ref_reloc_sym(struct map **maps,
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200861 const char *symbol_name,
862 u64 addr)
863{
864 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200865 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800866 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200867
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800868 ref = zalloc(sizeof(struct ref_reloc_sym));
869 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200870 return -ENOMEM;
871
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800872 ref->name = strdup(symbol_name);
873 if (ref->name == NULL) {
874 free(ref);
875 return -ENOMEM;
876 }
877
878 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200879 if (bracket)
880 *bracket = '\0';
881
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800882 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200883
884 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800885 struct kmap *kmap = map__kmap(maps[i]);
886 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200887 }
888
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200889 return 0;
890}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300891
892size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
893{
894 return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
895 __dsos__fprintf(&self->host_machine.user_dsos, fp) +
896 machines__fprintf_dsos(&self->machines, fp);
897}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300898
899size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
900 bool with_hits)
901{
902 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
903 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
904}