blob: 0fdf3ebef1e9d349a6256c95c2fc70dd8feea2fe [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
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -020070struct perf_session *perf_session__new(const char *filename, int mode, bool force)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020071{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020072 size_t len = filename ? strlen(filename) + 1 : 0;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020073 struct perf_session *self = zalloc(sizeof(*self) + len);
74
75 if (self == NULL)
76 goto out;
77
78 if (perf_header__init(&self->header) < 0)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -020079 goto out_free;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020080
81 memcpy(self->filename, filename, len);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020082 self->threads = RB_ROOT;
Eric B Munsoncb8f0932010-03-05 12:51:07 -030083 self->stats_by_id = RB_ROOT;
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020084 self->last_match = NULL;
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -020085 self->mmap_window = 32;
86 self->cwd = NULL;
87 self->cwdlen = 0;
Arnaldo Carvalho de Melo31d337c2009-12-27 21:37:03 -020088 self->unknown_events = 0;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -020089 map_groups__init(&self->kmaps);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020090
Arnaldo Carvalho de Melo64abebf2010-01-27 21:05:52 -020091 if (mode == O_RDONLY) {
92 if (perf_session__open(self, force) < 0)
93 goto out_delete;
94 } else if (mode == O_WRONLY) {
95 /*
96 * In O_RDONLY mode this will be performed when reading the
97 * kernel MMAP event, in event__process_mmap().
98 */
99 if (perf_session__create_kernel_maps(self) < 0)
100 goto out_delete;
101 }
Arnaldo Carvalho de Melod549c762009-12-27 21:37:02 -0200102
Tom Zanussi8dc58102010-04-01 23:59:15 -0500103 perf_session__update_sample_type(self);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200104out:
105 return self;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200106out_free:
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200107 free(self);
108 return NULL;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200109out_delete:
110 perf_session__delete(self);
111 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200112}
113
114void perf_session__delete(struct perf_session *self)
115{
116 perf_header__exit(&self->header);
117 close(self->fd);
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -0200118 free(self->cwd);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200119 free(self);
120}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200121
122static bool symbol__match_parent_regex(struct symbol *sym)
123{
124 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
125 return 1;
126
127 return 0;
128}
129
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300130struct map_symbol *perf_session__resolve_callchain(struct perf_session *self,
131 struct thread *thread,
132 struct ip_callchain *chain,
133 struct symbol **parent)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200134{
135 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200136 unsigned int i;
Arnaldo Carvalho de Meload5b2172010-04-02 10:04:18 -0300137 struct map_symbol *syms = calloc(chain->nr, sizeof(*syms));
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200138
Arnaldo Carvalho de Meload5b2172010-04-02 10:04:18 -0300139 if (!syms)
140 return NULL;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200141
142 for (i = 0; i < chain->nr; i++) {
143 u64 ip = chain->ips[i];
144 struct addr_location al;
145
146 if (ip >= PERF_CONTEXT_MAX) {
147 switch (ip) {
148 case PERF_CONTEXT_HV:
149 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
150 case PERF_CONTEXT_KERNEL:
151 cpumode = PERF_RECORD_MISC_KERNEL; break;
152 case PERF_CONTEXT_USER:
153 cpumode = PERF_RECORD_MISC_USER; break;
154 default:
155 break;
156 }
157 continue;
158 }
159
160 thread__find_addr_location(thread, self, cpumode,
161 MAP__FUNCTION, ip, &al, NULL);
162 if (al.sym != NULL) {
163 if (sort__has_parent && !*parent &&
164 symbol__match_parent_regex(al.sym))
165 *parent = al.sym;
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200166 if (!symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200167 break;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300168 syms[i].map = al.map;
169 syms[i].sym = al.sym;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200170 }
171 }
172
173 return syms;
174}
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200175
176static int process_event_stub(event_t *event __used,
177 struct perf_session *session __used)
178{
179 dump_printf(": unhandled!\n");
180 return 0;
181}
182
183static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
184{
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200185 if (handler->sample == NULL)
186 handler->sample = process_event_stub;
187 if (handler->mmap == NULL)
188 handler->mmap = process_event_stub;
189 if (handler->comm == NULL)
190 handler->comm = process_event_stub;
191 if (handler->fork == NULL)
192 handler->fork = process_event_stub;
193 if (handler->exit == NULL)
194 handler->exit = process_event_stub;
195 if (handler->lost == NULL)
196 handler->lost = process_event_stub;
197 if (handler->read == NULL)
198 handler->read = process_event_stub;
199 if (handler->throttle == NULL)
200 handler->throttle = process_event_stub;
201 if (handler->unthrottle == NULL)
202 handler->unthrottle = process_event_stub;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500203 if (handler->attr == NULL)
204 handler->attr = process_event_stub;
Tom Zanussicd19a032010-04-01 23:59:20 -0500205 if (handler->event_type == NULL)
206 handler->event_type = process_event_stub;
Tom Zanussi92155452010-04-01 23:59:21 -0500207 if (handler->tracing_data == NULL)
208 handler->tracing_data = process_event_stub;
Tom Zanussic7929e42010-04-01 23:59:22 -0500209 if (handler->build_id == NULL)
210 handler->build_id = process_event_stub;
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200211}
212
213static const char *event__name[] = {
214 [0] = "TOTAL",
215 [PERF_RECORD_MMAP] = "MMAP",
216 [PERF_RECORD_LOST] = "LOST",
217 [PERF_RECORD_COMM] = "COMM",
218 [PERF_RECORD_EXIT] = "EXIT",
219 [PERF_RECORD_THROTTLE] = "THROTTLE",
220 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
221 [PERF_RECORD_FORK] = "FORK",
222 [PERF_RECORD_READ] = "READ",
223 [PERF_RECORD_SAMPLE] = "SAMPLE",
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500224 [PERF_RECORD_HEADER_ATTR] = "ATTR",
Tom Zanussicd19a032010-04-01 23:59:20 -0500225 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
Tom Zanussi92155452010-04-01 23:59:21 -0500226 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
Tom Zanussic7929e42010-04-01 23:59:22 -0500227 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200228};
229
Tom Zanussi8dc58102010-04-01 23:59:15 -0500230unsigned long event__total[PERF_RECORD_HEADER_MAX];
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200231
232void event__print_totals(void)
233{
234 int i;
Tom Zanussi8dc58102010-04-01 23:59:15 -0500235 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
236 if (!event__name[i])
237 continue;
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200238 pr_info("%10s events: %10ld\n",
239 event__name[i], event__total[i]);
Tom Zanussi8dc58102010-04-01 23:59:15 -0500240 }
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200241}
242
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200243void mem_bswap_64(void *src, int byte_size)
244{
245 u64 *m = src;
246
247 while (byte_size > 0) {
248 *m = bswap_64(*m);
249 byte_size -= sizeof(u64);
250 ++m;
251 }
252}
253
254static void event__all64_swap(event_t *self)
255{
256 struct perf_event_header *hdr = &self->header;
257 mem_bswap_64(hdr + 1, self->header.size - sizeof(*hdr));
258}
259
260static void event__comm_swap(event_t *self)
261{
262 self->comm.pid = bswap_32(self->comm.pid);
263 self->comm.tid = bswap_32(self->comm.tid);
264}
265
266static void event__mmap_swap(event_t *self)
267{
268 self->mmap.pid = bswap_32(self->mmap.pid);
269 self->mmap.tid = bswap_32(self->mmap.tid);
270 self->mmap.start = bswap_64(self->mmap.start);
271 self->mmap.len = bswap_64(self->mmap.len);
272 self->mmap.pgoff = bswap_64(self->mmap.pgoff);
273}
274
275static void event__task_swap(event_t *self)
276{
277 self->fork.pid = bswap_32(self->fork.pid);
278 self->fork.tid = bswap_32(self->fork.tid);
279 self->fork.ppid = bswap_32(self->fork.ppid);
280 self->fork.ptid = bswap_32(self->fork.ptid);
281 self->fork.time = bswap_64(self->fork.time);
282}
283
284static void event__read_swap(event_t *self)
285{
286 self->read.pid = bswap_32(self->read.pid);
287 self->read.tid = bswap_32(self->read.tid);
288 self->read.value = bswap_64(self->read.value);
289 self->read.time_enabled = bswap_64(self->read.time_enabled);
290 self->read.time_running = bswap_64(self->read.time_running);
291 self->read.id = bswap_64(self->read.id);
292}
293
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500294static void event__attr_swap(event_t *self)
295{
296 size_t size;
297
298 self->attr.attr.type = bswap_32(self->attr.attr.type);
299 self->attr.attr.size = bswap_32(self->attr.attr.size);
300 self->attr.attr.config = bswap_64(self->attr.attr.config);
301 self->attr.attr.sample_period = bswap_64(self->attr.attr.sample_period);
302 self->attr.attr.sample_type = bswap_64(self->attr.attr.sample_type);
303 self->attr.attr.read_format = bswap_64(self->attr.attr.read_format);
304 self->attr.attr.wakeup_events = bswap_32(self->attr.attr.wakeup_events);
305 self->attr.attr.bp_type = bswap_32(self->attr.attr.bp_type);
306 self->attr.attr.bp_addr = bswap_64(self->attr.attr.bp_addr);
307 self->attr.attr.bp_len = bswap_64(self->attr.attr.bp_len);
308
309 size = self->header.size;
310 size -= (void *)&self->attr.id - (void *)self;
311 mem_bswap_64(self->attr.id, size);
312}
313
Tom Zanussicd19a032010-04-01 23:59:20 -0500314static void event__event_type_swap(event_t *self)
315{
316 self->event_type.event_type.event_id =
317 bswap_64(self->event_type.event_type.event_id);
318}
319
Tom Zanussi92155452010-04-01 23:59:21 -0500320static void event__tracing_data_swap(event_t *self)
321{
322 self->tracing_data.size = bswap_32(self->tracing_data.size);
323}
324
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200325typedef void (*event__swap_op)(event_t *self);
326
327static event__swap_op event__swap_ops[] = {
328 [PERF_RECORD_MMAP] = event__mmap_swap,
329 [PERF_RECORD_COMM] = event__comm_swap,
330 [PERF_RECORD_FORK] = event__task_swap,
331 [PERF_RECORD_EXIT] = event__task_swap,
332 [PERF_RECORD_LOST] = event__all64_swap,
333 [PERF_RECORD_READ] = event__read_swap,
334 [PERF_RECORD_SAMPLE] = event__all64_swap,
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500335 [PERF_RECORD_HEADER_ATTR] = event__attr_swap,
Tom Zanussicd19a032010-04-01 23:59:20 -0500336 [PERF_RECORD_HEADER_EVENT_TYPE] = event__event_type_swap,
Tom Zanussi92155452010-04-01 23:59:21 -0500337 [PERF_RECORD_HEADER_TRACING_DATA] = event__tracing_data_swap,
Tom Zanussic7929e42010-04-01 23:59:22 -0500338 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
Tom Zanussi8dc58102010-04-01 23:59:15 -0500339 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200340};
341
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200342static int perf_session__process_event(struct perf_session *self,
343 event_t *event,
344 struct perf_event_ops *ops,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200345 u64 offset, u64 head)
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200346{
347 trace_event(event);
348
Tom Zanussi8dc58102010-04-01 23:59:15 -0500349 if (event->header.type < PERF_RECORD_HEADER_MAX) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200350 dump_printf("%#Lx [%#x]: PERF_RECORD_%s",
Arnaldo Carvalho de Melo0d755032010-01-14 12:23:09 -0200351 offset + head, event->header.size,
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200352 event__name[event->header.type]);
353 ++event__total[0];
354 ++event__total[event->header.type];
355 }
356
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200357 if (self->header.needs_swap && event__swap_ops[event->header.type])
358 event__swap_ops[event->header.type](event);
359
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200360 switch (event->header.type) {
361 case PERF_RECORD_SAMPLE:
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200362 return ops->sample(event, self);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200363 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200364 return ops->mmap(event, self);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200365 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200366 return ops->comm(event, self);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200367 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200368 return ops->fork(event, self);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200369 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200370 return ops->exit(event, self);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200371 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200372 return ops->lost(event, self);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200373 case PERF_RECORD_READ:
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200374 return ops->read(event, self);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200375 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200376 return ops->throttle(event, self);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200377 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200378 return ops->unthrottle(event, self);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500379 case PERF_RECORD_HEADER_ATTR:
380 return ops->attr(event, self);
Tom Zanussicd19a032010-04-01 23:59:20 -0500381 case PERF_RECORD_HEADER_EVENT_TYPE:
382 return ops->event_type(event, self);
Tom Zanussi92155452010-04-01 23:59:21 -0500383 case PERF_RECORD_HEADER_TRACING_DATA:
384 /* setup for reading amidst mmap */
385 lseek(self->fd, offset + head, SEEK_SET);
386 return ops->tracing_data(event, self);
Tom Zanussic7929e42010-04-01 23:59:22 -0500387 case PERF_RECORD_HEADER_BUILD_ID:
388 return ops->build_id(event, self);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200389 default:
Arnaldo Carvalho de Melo31d337c2009-12-27 21:37:03 -0200390 self->unknown_events++;
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200391 return -1;
392 }
393}
394
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200395void perf_event_header__bswap(struct perf_event_header *self)
396{
397 self->type = bswap_32(self->type);
398 self->misc = bswap_16(self->misc);
399 self->size = bswap_16(self->size);
400}
401
402int perf_header__read_build_ids(struct perf_header *self,
403 int input, u64 offset, u64 size)
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200404{
405 struct build_id_event bev;
406 char filename[PATH_MAX];
407 u64 limit = offset + size;
408 int err = -1;
409
410 while (offset < limit) {
411 struct dso *dso;
412 ssize_t len;
Arnaldo Carvalho de Meloa89e5ab2010-01-07 19:59:39 -0200413 struct list_head *head = &dsos__user;
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200414
415 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
416 goto out;
417
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200418 if (self->needs_swap)
419 perf_event_header__bswap(&bev.header);
420
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200421 len = bev.header.size - sizeof(bev);
422 if (read(input, filename, len) != len)
423 goto out;
424
Arnaldo Carvalho de Meloa89e5ab2010-01-07 19:59:39 -0200425 if (bev.header.misc & PERF_RECORD_MISC_KERNEL)
426 head = &dsos__kernel;
427
428 dso = __dsos__findnew(head, filename);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200429 if (dso != NULL) {
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200430 dso__set_build_id(dso, &bev.build_id);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200431 if (head == &dsos__kernel && filename[0] == '[')
432 dso->kernel = 1;
433 }
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200434
435 offset += bev.header.size;
436 }
437 err = 0;
438out:
439 return err;
440}
441
442static struct thread *perf_session__register_idle_thread(struct perf_session *self)
443{
444 struct thread *thread = perf_session__findnew(self, 0);
445
446 if (thread == NULL || thread__set_comm(thread, "swapper")) {
447 pr_err("problem inserting idle task.\n");
448 thread = NULL;
449 }
450
451 return thread;
452}
453
Tom Zanussi8dc58102010-04-01 23:59:15 -0500454int do_read(int fd, void *buf, size_t size)
455{
456 void *buf_start = buf;
457
458 while (size) {
459 int ret = read(fd, buf, size);
460
461 if (ret <= 0)
462 return ret;
463
464 size -= ret;
465 buf += ret;
466 }
467
468 return buf - buf_start;
469}
470
471#define session_done() (*(volatile int *)(&session_done))
472volatile int session_done;
473
474static int __perf_session__process_pipe_events(struct perf_session *self,
475 struct perf_event_ops *ops)
476{
477 event_t event;
478 uint32_t size;
479 int skip = 0;
480 u64 head;
481 int err;
482 void *p;
483
484 perf_event_ops__fill_defaults(ops);
485
486 head = 0;
487more:
488 err = do_read(self->fd, &event, sizeof(struct perf_event_header));
489 if (err <= 0) {
490 if (err == 0)
491 goto done;
492
493 pr_err("failed to read event header\n");
494 goto out_err;
495 }
496
497 if (self->header.needs_swap)
498 perf_event_header__bswap(&event.header);
499
500 size = event.header.size;
501 if (size == 0)
502 size = 8;
503
504 p = &event;
505 p += sizeof(struct perf_event_header);
506
507 err = do_read(self->fd, p, size - sizeof(struct perf_event_header));
508 if (err <= 0) {
509 if (err == 0) {
510 pr_err("unexpected end of event stream\n");
511 goto done;
512 }
513
514 pr_err("failed to read event data\n");
515 goto out_err;
516 }
517
518 if (size == 0 ||
519 (skip = perf_session__process_event(self, &event, ops,
520 0, head)) < 0) {
521 dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n",
522 head, event.header.size, event.header.type);
523 /*
524 * assume we lost track of the stream, check alignment, and
525 * increment a single u64 in the hope to catch on again 'soon'.
526 */
527 if (unlikely(head & 7))
528 head &= ~7ULL;
529
530 size = 8;
531 }
532
533 head += size;
534
535 dump_printf("\n%#Lx [%#x]: event: %d\n",
536 head, event.header.size, event.header.type);
537
538 if (skip > 0)
539 head += skip;
540
541 if (!session_done())
542 goto more;
543done:
544 err = 0;
545out_err:
546 return err;
547}
548
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200549int __perf_session__process_events(struct perf_session *self,
550 u64 data_offset, u64 data_size,
551 u64 file_size, struct perf_event_ops *ops)
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200552{
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200553 int err, mmap_prot, mmap_flags;
554 u64 head, shift;
555 u64 offset = 0;
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200556 size_t page_size;
557 event_t *event;
558 uint32_t size;
559 char *buf;
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300560 struct ui_progress *progress = ui_progress__new("Processing events...",
561 self->size);
562 if (progress == NULL)
563 return -1;
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200564
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200565 perf_event_ops__fill_defaults(ops);
566
Arnaldo Carvalho de Melo1b759622010-01-14 18:30:04 -0200567 page_size = sysconf(_SC_PAGESIZE);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200568
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200569 head = data_offset;
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200570 shift = page_size * (head / page_size);
571 offset += shift;
572 head -= shift;
573
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200574 mmap_prot = PROT_READ;
575 mmap_flags = MAP_SHARED;
576
577 if (self->header.needs_swap) {
578 mmap_prot |= PROT_WRITE;
579 mmap_flags = MAP_PRIVATE;
580 }
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200581remap:
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200582 buf = mmap(NULL, page_size * self->mmap_window, mmap_prot,
583 mmap_flags, self->fd, offset);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200584 if (buf == MAP_FAILED) {
585 pr_err("failed to mmap file\n");
586 err = -errno;
587 goto out_err;
588 }
589
590more:
591 event = (event_t *)(buf + head);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300592 ui_progress__update(progress, offset);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200593
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200594 if (self->header.needs_swap)
595 perf_event_header__bswap(&event->header);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200596 size = event->header.size;
597 if (size == 0)
598 size = 8;
599
600 if (head + event->header.size >= page_size * self->mmap_window) {
601 int munmap_ret;
602
603 shift = page_size * (head / page_size);
604
605 munmap_ret = munmap(buf, page_size * self->mmap_window);
606 assert(munmap_ret == 0);
607
608 offset += shift;
609 head -= shift;
610 goto remap;
611 }
612
613 size = event->header.size;
614
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200615 dump_printf("\n%#Lx [%#x]: event: %d\n",
Arnaldo Carvalho de Melo0d755032010-01-14 12:23:09 -0200616 offset + head, event->header.size, event->header.type);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200617
618 if (size == 0 ||
619 perf_session__process_event(self, event, ops, offset, head) < 0) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200620 dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n",
Arnaldo Carvalho de Melo0d755032010-01-14 12:23:09 -0200621 offset + head, event->header.size,
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200622 event->header.type);
623 /*
624 * assume we lost track of the stream, check alignment, and
625 * increment a single u64 in the hope to catch on again 'soon'.
626 */
627 if (unlikely(head & 7))
628 head &= ~7ULL;
629
630 size = 8;
631 }
632
633 head += size;
634
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200635 if (offset + head >= data_offset + data_size)
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200636 goto done;
637
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200638 if (offset + head < file_size)
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200639 goto more;
640done:
641 err = 0;
642out_err:
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300643 ui_progress__delete(progress);
Arnaldo Carvalho de Melo06aae592009-12-27 21:36:59 -0200644 return err;
645}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -0200646
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200647int perf_session__process_events(struct perf_session *self,
648 struct perf_event_ops *ops)
649{
650 int err;
651
652 if (perf_session__register_idle_thread(self) == NULL)
653 return -ENOMEM;
654
655 if (!symbol_conf.full_paths) {
656 char bf[PATH_MAX];
657
658 if (getcwd(bf, sizeof(bf)) == NULL) {
659 err = -errno;
660out_getcwd_err:
661 pr_err("failed to get the current directory\n");
662 goto out_err;
663 }
664 self->cwd = strdup(bf);
665 if (self->cwd == NULL) {
666 err = -ENOMEM;
667 goto out_getcwd_err;
668 }
669 self->cwdlen = strlen(self->cwd);
670 }
671
Tom Zanussi8dc58102010-04-01 23:59:15 -0500672 if (!self->fd_pipe)
673 err = __perf_session__process_events(self,
674 self->header.data_offset,
675 self->header.data_size,
676 self->size, ops);
677 else
678 err = __perf_session__process_pipe_events(self, ops);
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200679out_err:
680 return err;
681}
682
Arnaldo Carvalho de Melod549c762009-12-27 21:37:02 -0200683bool perf_session__has_traces(struct perf_session *self, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -0200684{
685 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
Arnaldo Carvalho de Melod549c762009-12-27 21:37:02 -0200686 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
687 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -0200688 }
689
Arnaldo Carvalho de Melod549c762009-12-27 21:37:02 -0200690 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -0200691}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200692
693int perf_session__set_kallsyms_ref_reloc_sym(struct perf_session *self,
694 const char *symbol_name,
695 u64 addr)
696{
697 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200698 enum map_type i;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200699
700 self->ref_reloc_sym.name = strdup(symbol_name);
701 if (self->ref_reloc_sym.name == NULL)
702 return -ENOMEM;
703
704 bracket = strchr(self->ref_reloc_sym.name, ']');
705 if (bracket)
706 *bracket = '\0';
707
708 self->ref_reloc_sym.addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200709
710 for (i = 0; i < MAP__NR_TYPES; ++i) {
711 struct kmap *kmap = map__kmap(self->vmlinux_maps[i]);
712 kmap->ref_reloc_sym = &self->ref_reloc_sym;
713 }
714
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200715 return 0;
716}