blob: 6fb4694d05fa1e2c89e5ac91207c025214031f38 [file] [log] [blame]
Xiao Guangrongb8f46c52010-02-03 11:53:14 +08001#define _FILE_OFFSET_BITS 64
2
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02003#include <linux/kernel.h>
4
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02005#include <byteswap.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02006#include <unistd.h>
7#include <sys/types.h>
Arnaldo Carvalho de Meloa41794c2010-05-18 18:29:23 -03008#include <sys/mman.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02009
10#include "session.h"
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -020011#include "sort.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020012#include "util.h"
13
14static int perf_session__open(struct perf_session *self, bool force)
15{
16 struct stat input_stat;
17
Tom Zanussi8dc58102010-04-01 23:59:15 -050018 if (!strcmp(self->filename, "-")) {
19 self->fd_pipe = true;
20 self->fd = STDIN_FILENO;
21
22 if (perf_header__read(self, self->fd) < 0)
23 pr_err("incompatible file format");
24
25 return 0;
26 }
27
Xiao Guangrongf887f302010-02-04 16:46:42 +080028 self->fd = open(self->filename, O_RDONLY);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020029 if (self->fd < 0) {
Andy Isaacson0f2c3de2010-06-11 20:36:15 -070030 int err = errno;
31
32 pr_err("failed to open %s: %s", self->filename, strerror(err));
33 if (err == ENOENT && !strcmp(self->filename, "perf.data"))
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020034 pr_err(" (try 'perf record' first)");
35 pr_err("\n");
36 return -errno;
37 }
38
39 if (fstat(self->fd, &input_stat) < 0)
40 goto out_close;
41
42 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
43 pr_err("file %s not owned by current user or root\n",
44 self->filename);
45 goto out_close;
46 }
47
48 if (!input_stat.st_size) {
49 pr_info("zero-sized file (%s), nothing to do!\n",
50 self->filename);
51 goto out_close;
52 }
53
Tom Zanussi8dc58102010-04-01 23:59:15 -050054 if (perf_header__read(self, self->fd) < 0) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020055 pr_err("incompatible file format");
56 goto out_close;
57 }
58
59 self->size = input_stat.st_size;
60 return 0;
61
62out_close:
63 close(self->fd);
64 self->fd = -1;
65 return -1;
66}
67
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020068static void perf_session__id_header_size(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -050069{
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020070 struct sample_data *data;
71 u64 sample_type = session->sample_type;
72 u16 size = 0;
73
74 if (!session->sample_id_all)
75 goto out;
76
77 if (sample_type & PERF_SAMPLE_TID)
78 size += sizeof(data->tid) * 2;
79
80 if (sample_type & PERF_SAMPLE_TIME)
81 size += sizeof(data->time);
82
83 if (sample_type & PERF_SAMPLE_ID)
84 size += sizeof(data->id);
85
86 if (sample_type & PERF_SAMPLE_STREAM_ID)
87 size += sizeof(data->stream_id);
88
89 if (sample_type & PERF_SAMPLE_CPU)
90 size += sizeof(data->cpu) * 2;
91out:
92 session->id_hdr_size = size;
93}
94
95void perf_session__set_sample_id_all(struct perf_session *session, bool value)
96{
97 session->sample_id_all = value;
98 perf_session__id_header_size(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -050099}
100
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200101void perf_session__set_sample_type(struct perf_session *session, u64 type)
102{
103 session->sample_type = type;
104}
105
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200106void perf_session__update_sample_type(struct perf_session *self)
107{
108 self->sample_type = perf_header__sample_type(&self->header);
109 self->sample_id_all = perf_header__sample_id_all(&self->header);
110 perf_session__id_header_size(self);
111}
112
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800113int perf_session__create_kernel_maps(struct perf_session *self)
114{
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -0300115 int ret = machine__create_kernel_maps(&self->host_machine);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800116
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800117 if (ret >= 0)
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -0300118 ret = machines__create_guest_kernel_maps(&self->machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800119 return ret;
120}
121
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300122static void perf_session__destroy_kernel_maps(struct perf_session *self)
123{
124 machine__destroy_kernel_maps(&self->host_machine);
125 machines__destroy_guest_kernel_maps(&self->machines);
126}
127
Ian Munsie21ef97f2010-12-10 14:09:16 +1100128struct perf_session *perf_session__new(const char *filename, int mode,
129 bool force, bool repipe,
130 struct perf_event_ops *ops)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200131{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200132 size_t len = filename ? strlen(filename) + 1 : 0;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200133 struct perf_session *self = zalloc(sizeof(*self) + len);
134
135 if (self == NULL)
136 goto out;
137
138 if (perf_header__init(&self->header) < 0)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200139 goto out_free;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200140
141 memcpy(self->filename, filename, len);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200142 self->threads = RB_ROOT;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300143 INIT_LIST_HEAD(&self->dead_threads);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300144 self->hists_tree = RB_ROOT;
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200145 self->last_match = NULL;
Thomas Gleixner55b44622010-11-30 17:49:46 +0000146 /*
147 * On 64bit we can mmap the data file in one go. No need for tiny mmap
148 * slices. On 32bit we use 32MB.
149 */
150#if BITS_PER_LONG == 64
151 self->mmap_window = ULLONG_MAX;
152#else
153 self->mmap_window = 32 * 1024 * 1024ULL;
154#endif
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300155 self->machines = RB_ROOT;
Tom Zanussi454c4072010-05-01 01:41:20 -0500156 self->repipe = repipe;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000157 INIT_LIST_HEAD(&self->ordered_samples.samples);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000158 INIT_LIST_HEAD(&self->ordered_samples.sample_cache);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000159 INIT_LIST_HEAD(&self->ordered_samples.to_free);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300160 machine__init(&self->host_machine, "", HOST_KERNEL_ID);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200161
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200162 if (mode == O_RDONLY) {
163 if (perf_session__open(self, force) < 0)
164 goto out_delete;
165 } else if (mode == O_WRONLY) {
166 /*
167 * In O_RDONLY mode this will be performed when reading the
168 * kernel MMAP event, in event__process_mmap().
169 */
170 if (perf_session__create_kernel_maps(self) < 0)
171 goto out_delete;
172 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200173
Tom Zanussi8dc58102010-04-01 23:59:15 -0500174 perf_session__update_sample_type(self);
Ian Munsie21ef97f2010-12-10 14:09:16 +1100175
176 if (ops && ops->ordering_requires_timestamps &&
177 ops->ordered_samples && !self->sample_id_all) {
178 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
179 ops->ordered_samples = false;
180 }
181
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200182out:
183 return self;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200184out_free:
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200185 free(self);
186 return NULL;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200187out_delete:
188 perf_session__delete(self);
189 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200190}
191
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300192static void perf_session__delete_dead_threads(struct perf_session *self)
193{
194 struct thread *n, *t;
195
196 list_for_each_entry_safe(t, n, &self->dead_threads, node) {
197 list_del(&t->node);
198 thread__delete(t);
199 }
200}
201
202static void perf_session__delete_threads(struct perf_session *self)
203{
204 struct rb_node *nd = rb_first(&self->threads);
205
206 while (nd) {
207 struct thread *t = rb_entry(nd, struct thread, rb_node);
208
209 rb_erase(&t->rb_node, &self->threads);
210 nd = rb_next(nd);
211 thread__delete(t);
212 }
213}
214
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200215void perf_session__delete(struct perf_session *self)
216{
217 perf_header__exit(&self->header);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300218 perf_session__destroy_kernel_maps(self);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300219 perf_session__delete_dead_threads(self);
220 perf_session__delete_threads(self);
221 machine__exit(&self->host_machine);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200222 close(self->fd);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200223 free(self);
224}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200225
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300226void perf_session__remove_thread(struct perf_session *self, struct thread *th)
227{
Arnaldo Carvalho de Melo70597f22010-08-02 18:59:28 -0300228 self->last_match = NULL;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300229 rb_erase(&th->rb_node, &self->threads);
230 /*
231 * We may have references to this thread, for instance in some hist_entry
232 * instances, so just move them to a separate list.
233 */
234 list_add_tail(&th->node, &self->dead_threads);
235}
236
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200237static bool symbol__match_parent_regex(struct symbol *sym)
238{
239 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
240 return 1;
241
242 return 0;
243}
244
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300245struct map_symbol *perf_session__resolve_callchain(struct perf_session *self,
246 struct thread *thread,
247 struct ip_callchain *chain,
248 struct symbol **parent)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200249{
250 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200251 unsigned int i;
Arnaldo Carvalho de Meload5b2172010-04-02 10:04:18 -0300252 struct map_symbol *syms = calloc(chain->nr, sizeof(*syms));
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200253
Arnaldo Carvalho de Meload5b2172010-04-02 10:04:18 -0300254 if (!syms)
255 return NULL;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200256
257 for (i = 0; i < chain->nr; i++) {
258 u64 ip = chain->ips[i];
259 struct addr_location al;
260
261 if (ip >= PERF_CONTEXT_MAX) {
262 switch (ip) {
263 case PERF_CONTEXT_HV:
264 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
265 case PERF_CONTEXT_KERNEL:
266 cpumode = PERF_RECORD_MISC_KERNEL; break;
267 case PERF_CONTEXT_USER:
268 cpumode = PERF_RECORD_MISC_USER; break;
269 default:
270 break;
271 }
272 continue;
273 }
274
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800275 al.filtered = false;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200276 thread__find_addr_location(thread, self, cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800277 MAP__FUNCTION, thread->pid, ip, &al, NULL);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200278 if (al.sym != NULL) {
279 if (sort__has_parent && !*parent &&
280 symbol__match_parent_regex(al.sym))
281 *parent = al.sym;
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200282 if (!symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200283 break;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300284 syms[i].map = al.map;
285 syms[i].sym = al.sym;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200286 }
287 }
288
289 return syms;
290}
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200291
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200292static int process_event_synth_stub(event_t *event __used,
293 struct perf_session *session __used)
294{
295 dump_printf(": unhandled!\n");
296 return 0;
297}
298
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200299static int process_event_stub(event_t *event __used,
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200300 struct sample_data *sample __used,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200301 struct perf_session *session __used)
302{
303 dump_printf(": unhandled!\n");
304 return 0;
305}
306
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200307static int process_finished_round_stub(event_t *event __used,
308 struct perf_session *session __used,
309 struct perf_event_ops *ops __used)
310{
311 dump_printf(": unhandled!\n");
312 return 0;
313}
314
315static int process_finished_round(event_t *event,
316 struct perf_session *session,
317 struct perf_event_ops *ops);
318
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200319static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
320{
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200321 if (handler->sample == NULL)
322 handler->sample = process_event_stub;
323 if (handler->mmap == NULL)
324 handler->mmap = process_event_stub;
325 if (handler->comm == NULL)
326 handler->comm = process_event_stub;
327 if (handler->fork == NULL)
328 handler->fork = process_event_stub;
329 if (handler->exit == NULL)
330 handler->exit = process_event_stub;
331 if (handler->lost == NULL)
Arnaldo Carvalho de Melo37982ba2010-11-26 18:31:54 -0200332 handler->lost = event__process_lost;
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200333 if (handler->read == NULL)
334 handler->read = process_event_stub;
335 if (handler->throttle == NULL)
336 handler->throttle = process_event_stub;
337 if (handler->unthrottle == NULL)
338 handler->unthrottle = process_event_stub;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500339 if (handler->attr == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200340 handler->attr = process_event_synth_stub;
Tom Zanussicd19a032010-04-01 23:59:20 -0500341 if (handler->event_type == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200342 handler->event_type = process_event_synth_stub;
Tom Zanussi92155452010-04-01 23:59:21 -0500343 if (handler->tracing_data == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200344 handler->tracing_data = process_event_synth_stub;
Tom Zanussic7929e42010-04-01 23:59:22 -0500345 if (handler->build_id == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200346 handler->build_id = process_event_synth_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200347 if (handler->finished_round == NULL) {
348 if (handler->ordered_samples)
349 handler->finished_round = process_finished_round;
350 else
351 handler->finished_round = process_finished_round_stub;
352 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200353}
354
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200355void mem_bswap_64(void *src, int byte_size)
356{
357 u64 *m = src;
358
359 while (byte_size > 0) {
360 *m = bswap_64(*m);
361 byte_size -= sizeof(u64);
362 ++m;
363 }
364}
365
366static void event__all64_swap(event_t *self)
367{
368 struct perf_event_header *hdr = &self->header;
369 mem_bswap_64(hdr + 1, self->header.size - sizeof(*hdr));
370}
371
372static void event__comm_swap(event_t *self)
373{
374 self->comm.pid = bswap_32(self->comm.pid);
375 self->comm.tid = bswap_32(self->comm.tid);
376}
377
378static void event__mmap_swap(event_t *self)
379{
380 self->mmap.pid = bswap_32(self->mmap.pid);
381 self->mmap.tid = bswap_32(self->mmap.tid);
382 self->mmap.start = bswap_64(self->mmap.start);
383 self->mmap.len = bswap_64(self->mmap.len);
384 self->mmap.pgoff = bswap_64(self->mmap.pgoff);
385}
386
387static void event__task_swap(event_t *self)
388{
389 self->fork.pid = bswap_32(self->fork.pid);
390 self->fork.tid = bswap_32(self->fork.tid);
391 self->fork.ppid = bswap_32(self->fork.ppid);
392 self->fork.ptid = bswap_32(self->fork.ptid);
393 self->fork.time = bswap_64(self->fork.time);
394}
395
396static void event__read_swap(event_t *self)
397{
398 self->read.pid = bswap_32(self->read.pid);
399 self->read.tid = bswap_32(self->read.tid);
400 self->read.value = bswap_64(self->read.value);
401 self->read.time_enabled = bswap_64(self->read.time_enabled);
402 self->read.time_running = bswap_64(self->read.time_running);
403 self->read.id = bswap_64(self->read.id);
404}
405
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500406static void event__attr_swap(event_t *self)
407{
408 size_t size;
409
410 self->attr.attr.type = bswap_32(self->attr.attr.type);
411 self->attr.attr.size = bswap_32(self->attr.attr.size);
412 self->attr.attr.config = bswap_64(self->attr.attr.config);
413 self->attr.attr.sample_period = bswap_64(self->attr.attr.sample_period);
414 self->attr.attr.sample_type = bswap_64(self->attr.attr.sample_type);
415 self->attr.attr.read_format = bswap_64(self->attr.attr.read_format);
416 self->attr.attr.wakeup_events = bswap_32(self->attr.attr.wakeup_events);
417 self->attr.attr.bp_type = bswap_32(self->attr.attr.bp_type);
418 self->attr.attr.bp_addr = bswap_64(self->attr.attr.bp_addr);
419 self->attr.attr.bp_len = bswap_64(self->attr.attr.bp_len);
420
421 size = self->header.size;
422 size -= (void *)&self->attr.id - (void *)self;
423 mem_bswap_64(self->attr.id, size);
424}
425
Tom Zanussicd19a032010-04-01 23:59:20 -0500426static void event__event_type_swap(event_t *self)
427{
428 self->event_type.event_type.event_id =
429 bswap_64(self->event_type.event_type.event_id);
430}
431
Tom Zanussi92155452010-04-01 23:59:21 -0500432static void event__tracing_data_swap(event_t *self)
433{
434 self->tracing_data.size = bswap_32(self->tracing_data.size);
435}
436
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200437typedef void (*event__swap_op)(event_t *self);
438
439static event__swap_op event__swap_ops[] = {
440 [PERF_RECORD_MMAP] = event__mmap_swap,
441 [PERF_RECORD_COMM] = event__comm_swap,
442 [PERF_RECORD_FORK] = event__task_swap,
443 [PERF_RECORD_EXIT] = event__task_swap,
444 [PERF_RECORD_LOST] = event__all64_swap,
445 [PERF_RECORD_READ] = event__read_swap,
446 [PERF_RECORD_SAMPLE] = event__all64_swap,
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500447 [PERF_RECORD_HEADER_ATTR] = event__attr_swap,
Tom Zanussicd19a032010-04-01 23:59:20 -0500448 [PERF_RECORD_HEADER_EVENT_TYPE] = event__event_type_swap,
Tom Zanussi92155452010-04-01 23:59:21 -0500449 [PERF_RECORD_HEADER_TRACING_DATA] = event__tracing_data_swap,
Tom Zanussic7929e42010-04-01 23:59:22 -0500450 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
Tom Zanussi8dc58102010-04-01 23:59:15 -0500451 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200452};
453
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200454struct sample_queue {
455 u64 timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000456 u64 file_offset;
Thomas Gleixner28990f72010-11-30 17:49:35 +0000457 event_t *event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200458 struct list_head list;
459};
460
Thomas Gleixner020bb752010-11-30 17:49:53 +0000461static void perf_session_free_sample_buffers(struct perf_session *session)
462{
463 struct ordered_samples *os = &session->ordered_samples;
464
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000465 while (!list_empty(&os->to_free)) {
Thomas Gleixner020bb752010-11-30 17:49:53 +0000466 struct sample_queue *sq;
467
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000468 sq = list_entry(os->to_free.next, struct sample_queue, list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000469 list_del(&sq->list);
470 free(sq);
471 }
472}
473
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100474static int perf_session_deliver_event(struct perf_session *session,
475 event_t *event,
476 struct sample_data *sample,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000477 struct perf_event_ops *ops,
478 u64 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100479
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200480static void flush_sample_queue(struct perf_session *s,
481 struct perf_event_ops *ops)
482{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000483 struct ordered_samples *os = &s->ordered_samples;
484 struct list_head *head = &os->samples;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200485 struct sample_queue *tmp, *iter;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200486 struct sample_data sample;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000487 u64 limit = os->next_flush;
488 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200489
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200490 if (!ops->ordered_samples || !limit)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200491 return;
492
493 list_for_each_entry_safe(iter, tmp, head, list) {
494 if (iter->timestamp > limit)
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000495 break;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200496
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200497 event__parse_sample(iter->event, s, &sample);
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000498 perf_session_deliver_event(s, iter->event, &sample, ops,
499 iter->file_offset);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200500
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000501 os->last_flush = iter->timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200502 list_del(&iter->list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000503 list_add(&iter->list, &os->sample_cache);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200504 }
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000505
506 if (list_empty(head)) {
507 os->last_sample = NULL;
508 } else if (last_ts <= limit) {
509 os->last_sample =
510 list_entry(head->prev, struct sample_queue, list);
511 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200512}
513
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200514/*
515 * When perf record finishes a pass on every buffers, it records this pseudo
516 * event.
517 * We record the max timestamp t found in the pass n.
518 * Assuming these timestamps are monotonic across cpus, we know that if
519 * a buffer still has events with timestamps below t, they will be all
520 * available and then read in the pass n + 1.
521 * Hence when we start to read the pass n + 2, we can safely flush every
522 * events with timestamps below t.
523 *
524 * ============ PASS n =================
525 * CPU 0 | CPU 1
526 * |
527 * cnt1 timestamps | cnt2 timestamps
528 * 1 | 2
529 * 2 | 3
530 * - | 4 <--- max recorded
531 *
532 * ============ PASS n + 1 ==============
533 * CPU 0 | CPU 1
534 * |
535 * cnt1 timestamps | cnt2 timestamps
536 * 3 | 5
537 * 4 | 6
538 * 5 | 7 <---- max recorded
539 *
540 * Flush every events below timestamp 4
541 *
542 * ============ PASS n + 2 ==============
543 * CPU 0 | CPU 1
544 * |
545 * cnt1 timestamps | cnt2 timestamps
546 * 6 | 8
547 * 7 | 9
548 * - | 10
549 *
550 * Flush every events below timestamp 7
551 * etc...
552 */
553static int process_finished_round(event_t *event __used,
554 struct perf_session *session,
555 struct perf_event_ops *ops)
556{
557 flush_sample_queue(session, ops);
558 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
559
560 return 0;
561}
562
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200563/* The queue is ordered by time */
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100564static void __queue_event(struct sample_queue *new, struct perf_session *s)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200565{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000566 struct ordered_samples *os = &s->ordered_samples;
567 struct sample_queue *sample = os->last_sample;
568 u64 timestamp = new->timestamp;
569 struct list_head *p;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200570
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000571 os->last_sample = new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200572
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000573 if (!sample) {
574 list_add(&new->list, &os->samples);
575 os->max_timestamp = timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200576 return;
577 }
578
579 /*
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000580 * last_sample might point to some random place in the list as it's
581 * the last queued event. We expect that the new event is close to
582 * this.
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200583 */
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000584 if (sample->timestamp <= timestamp) {
585 while (sample->timestamp <= timestamp) {
586 p = sample->list.next;
587 if (p == &os->samples) {
588 list_add_tail(&new->list, &os->samples);
589 os->max_timestamp = timestamp;
590 return;
591 }
592 sample = list_entry(p, struct sample_queue, list);
593 }
594 list_add_tail(&new->list, &sample->list);
595 } else {
596 while (sample->timestamp > timestamp) {
597 p = sample->list.prev;
598 if (p == &os->samples) {
599 list_add(&new->list, &os->samples);
600 return;
601 }
602 sample = list_entry(p, struct sample_queue, list);
603 }
604 list_add(&new->list, &sample->list);
605 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200606}
607
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000608#define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
609
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100610static int perf_session_queue_event(struct perf_session *s, event_t *event,
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000611 struct sample_data *data, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200612{
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000613 struct ordered_samples *os = &s->ordered_samples;
614 struct list_head *sc = &os->sample_cache;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200615 u64 timestamp = data->time;
616 struct sample_queue *new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200617
Thomas Gleixner79a14c12010-12-07 12:48:44 +0000618 if (!timestamp || timestamp == ~0ULL)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100619 return -ETIME;
620
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200621 if (timestamp < s->ordered_samples.last_flush) {
622 printf("Warning: Timestamp below last timeslice flush\n");
623 return -EINVAL;
624 }
625
Thomas Gleixner020bb752010-11-30 17:49:53 +0000626 if (!list_empty(sc)) {
627 new = list_entry(sc->next, struct sample_queue, list);
628 list_del(&new->list);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000629 } else if (os->sample_buffer) {
630 new = os->sample_buffer + os->sample_buffer_idx;
631 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
632 os->sample_buffer = NULL;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000633 } else {
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000634 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
635 if (!os->sample_buffer)
Thomas Gleixner020bb752010-11-30 17:49:53 +0000636 return -ENOMEM;
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000637 list_add(&os->sample_buffer->list, &os->to_free);
638 os->sample_buffer_idx = 2;
639 new = os->sample_buffer + 1;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000640 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200641
642 new->timestamp = timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000643 new->file_offset = file_offset;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000644 new->event = event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200645
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100646 __queue_event(new, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200647
648 return 0;
649}
650
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200651static void callchain__printf(struct sample_data *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200652{
653 unsigned int i;
654
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200655 printf("... chain: nr:%Lu\n", sample->callchain->nr);
656
657 for (i = 0; i < sample->callchain->nr; i++)
658 printf("..... %2d: %016Lx\n", i, sample->callchain->ips[i]);
659}
660
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200661static void perf_session__print_tstamp(struct perf_session *session,
662 event_t *event,
663 struct sample_data *sample)
664{
665 if (event->header.type != PERF_RECORD_SAMPLE &&
666 !session->sample_id_all) {
667 fputs("-1 -1 ", stdout);
668 return;
669 }
670
671 if ((session->sample_type & PERF_SAMPLE_CPU))
672 printf("%u ", sample->cpu);
673
674 if (session->sample_type & PERF_SAMPLE_TIME)
675 printf("%Lu ", sample->time);
676}
677
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000678static void dump_event(struct perf_session *session, event_t *event,
679 u64 file_offset, struct sample_data *sample)
680{
681 if (!dump_trace)
682 return;
683
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200684 printf("\n%#Lx [%#x]: event: %d\n", file_offset, event->header.size,
685 event->header.type);
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000686
687 trace_event(event);
688
689 if (sample)
690 perf_session__print_tstamp(session, event, sample);
691
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200692 printf("%#Lx [%#x]: PERF_RECORD_%s", file_offset, event->header.size,
693 event__get_event_name(event->header.type));
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000694}
695
696static void dump_sample(struct perf_session *session, event_t *event,
697 struct sample_data *sample)
698{
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200699 if (!dump_trace)
700 return;
701
702 printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
703 sample->pid, sample->tid, sample->ip, sample->period);
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000704
705 if (session->sample_type & PERF_SAMPLE_CALLCHAIN)
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200706 callchain__printf(sample);
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000707}
708
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100709static int perf_session_deliver_event(struct perf_session *session,
710 event_t *event,
711 struct sample_data *sample,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000712 struct perf_event_ops *ops,
Thomas Gleixner532e7262010-12-07 12:48:55 +0000713 u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100714{
Thomas Gleixner532e7262010-12-07 12:48:55 +0000715 dump_event(session, event, file_offset, sample);
716
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100717 switch (event->header.type) {
718 case PERF_RECORD_SAMPLE:
Thomas Gleixner532e7262010-12-07 12:48:55 +0000719 dump_sample(session, event, sample);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100720 return ops->sample(event, sample, session);
721 case PERF_RECORD_MMAP:
722 return ops->mmap(event, sample, session);
723 case PERF_RECORD_COMM:
724 return ops->comm(event, sample, session);
725 case PERF_RECORD_FORK:
726 return ops->fork(event, sample, session);
727 case PERF_RECORD_EXIT:
728 return ops->exit(event, sample, session);
729 case PERF_RECORD_LOST:
730 return ops->lost(event, sample, session);
731 case PERF_RECORD_READ:
732 return ops->read(event, sample, session);
733 case PERF_RECORD_THROTTLE:
734 return ops->throttle(event, sample, session);
735 case PERF_RECORD_UNTHROTTLE:
736 return ops->unthrottle(event, sample, session);
737 default:
738 ++session->hists.stats.nr_unknown_events;
739 return -1;
740 }
741}
742
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000743static int perf_session__preprocess_sample(struct perf_session *session,
744 event_t *event, struct sample_data *sample)
745{
746 if (event->header.type != PERF_RECORD_SAMPLE ||
747 !(session->sample_type & PERF_SAMPLE_CALLCHAIN))
748 return 0;
749
750 if (!ip_callchain__valid(sample->callchain, event)) {
751 pr_debug("call-chain problem with event, skipping it.\n");
752 ++session->hists.stats.nr_invalid_chains;
753 session->hists.stats.total_invalid_chains += sample->period;
754 return -EINVAL;
755 }
756 return 0;
757}
758
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000759static int perf_session__process_user_event(struct perf_session *session, event_t *event,
760 struct perf_event_ops *ops, u64 file_offset)
761{
762 dump_event(session, event, file_offset, NULL);
763
764 /* These events are processed right away */
765 switch (event->header.type) {
766 case PERF_RECORD_HEADER_ATTR:
767 return ops->attr(event, session);
768 case PERF_RECORD_HEADER_EVENT_TYPE:
769 return ops->event_type(event, session);
770 case PERF_RECORD_HEADER_TRACING_DATA:
771 /* setup for reading amidst mmap */
772 lseek(session->fd, file_offset, SEEK_SET);
773 return ops->tracing_data(event, session);
774 case PERF_RECORD_HEADER_BUILD_ID:
775 return ops->build_id(event, session);
776 case PERF_RECORD_FINISHED_ROUND:
777 return ops->finished_round(event, session, ops);
778 default:
779 return -EINVAL;
780 }
781}
782
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100783static int perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200784 event_t *event,
785 struct perf_event_ops *ops,
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000786 u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200787{
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200788 struct sample_data sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100789 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200790
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100791 if (session->header.needs_swap && event__swap_ops[event->header.type])
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200792 event__swap_ops[event->header.type](event);
793
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000794 if (event->header.type >= PERF_RECORD_HEADER_MAX)
795 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200796
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000797 hists__inc_nr_events(&session->hists, event->header.type);
798
799 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000800 return perf_session__process_user_event(session, event, ops, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100801
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000802 /*
803 * For all kernel events we get the sample data
804 */
805 event__parse_sample(event, session, &sample);
806
807 /* Preprocess sample records - precheck callchains */
808 if (perf_session__preprocess_sample(session, event, &sample))
809 return 0;
810
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100811 if (ops->ordered_samples) {
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000812 ret = perf_session_queue_event(session, event, &sample,
813 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100814 if (ret != -ETIME)
815 return ret;
816 }
817
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000818 return perf_session_deliver_event(session, event, &sample, ops,
819 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200820}
821
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200822void perf_event_header__bswap(struct perf_event_header *self)
823{
824 self->type = bswap_32(self->type);
825 self->misc = bswap_16(self->misc);
826 self->size = bswap_16(self->size);
827}
828
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200829static struct thread *perf_session__register_idle_thread(struct perf_session *self)
830{
831 struct thread *thread = perf_session__findnew(self, 0);
832
833 if (thread == NULL || thread__set_comm(thread, "swapper")) {
834 pr_err("problem inserting idle task.\n");
835 thread = NULL;
836 }
837
838 return thread;
839}
840
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200841static void perf_session__warn_about_errors(const struct perf_session *session,
842 const struct perf_event_ops *ops)
843{
844 if (ops->lost == event__process_lost &&
845 session->hists.stats.total_lost != 0) {
846 ui__warning("Processed %Lu events and LOST %Lu!\n\n"
847 "Check IO/CPU overload!\n\n",
848 session->hists.stats.total_period,
849 session->hists.stats.total_lost);
850 }
851
852 if (session->hists.stats.nr_unknown_events != 0) {
853 ui__warning("Found %u unknown events!\n\n"
854 "Is this an older tool processing a perf.data "
855 "file generated by a more recent tool?\n\n"
856 "If that is not the case, consider "
857 "reporting to linux-kernel@vger.kernel.org.\n\n",
858 session->hists.stats.nr_unknown_events);
859 }
860
861 if (session->hists.stats.nr_invalid_chains != 0) {
862 ui__warning("Found invalid callchains!\n\n"
863 "%u out of %u events were discarded for this reason.\n\n"
864 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
865 session->hists.stats.nr_invalid_chains,
866 session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
867 }
868}
869
Tom Zanussi8dc58102010-04-01 23:59:15 -0500870#define session_done() (*(volatile int *)(&session_done))
871volatile int session_done;
872
873static int __perf_session__process_pipe_events(struct perf_session *self,
874 struct perf_event_ops *ops)
875{
876 event_t event;
877 uint32_t size;
878 int skip = 0;
879 u64 head;
880 int err;
881 void *p;
882
883 perf_event_ops__fill_defaults(ops);
884
885 head = 0;
886more:
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200887 err = readn(self->fd, &event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -0500888 if (err <= 0) {
889 if (err == 0)
890 goto done;
891
892 pr_err("failed to read event header\n");
893 goto out_err;
894 }
895
896 if (self->header.needs_swap)
897 perf_event_header__bswap(&event.header);
898
899 size = event.header.size;
900 if (size == 0)
901 size = 8;
902
903 p = &event;
904 p += sizeof(struct perf_event_header);
905
Tom Zanussi794e43b2010-05-05 00:27:40 -0500906 if (size - sizeof(struct perf_event_header)) {
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200907 err = readn(self->fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -0500908 if (err <= 0) {
909 if (err == 0) {
910 pr_err("unexpected end of event stream\n");
911 goto done;
912 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500913
Tom Zanussi794e43b2010-05-05 00:27:40 -0500914 pr_err("failed to read event data\n");
915 goto out_err;
916 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500917 }
918
919 if (size == 0 ||
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000920 (skip = perf_session__process_event(self, &event, ops, head)) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -0500921 dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n",
922 head, event.header.size, event.header.type);
923 /*
924 * assume we lost track of the stream, check alignment, and
925 * increment a single u64 in the hope to catch on again 'soon'.
926 */
927 if (unlikely(head & 7))
928 head &= ~7ULL;
929
930 size = 8;
931 }
932
933 head += size;
934
Tom Zanussi8dc58102010-04-01 23:59:15 -0500935 if (skip > 0)
936 head += skip;
937
938 if (!session_done())
939 goto more;
940done:
941 err = 0;
942out_err:
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200943 perf_session__warn_about_errors(self, ops);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000944 perf_session_free_sample_buffers(self);
Tom Zanussi8dc58102010-04-01 23:59:15 -0500945 return err;
946}
947
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000948int __perf_session__process_events(struct perf_session *session,
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200949 u64 data_offset, u64 data_size,
950 u64 file_size, struct perf_event_ops *ops)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200951{
Thomas Gleixner55b44622010-11-30 17:49:46 +0000952 u64 head, page_offset, file_offset, file_pos, progress_next;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000953 int err, mmap_prot, mmap_flags, map_idx = 0;
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000954 struct ui_progress *progress;
Thomas Gleixner55b44622010-11-30 17:49:46 +0000955 size_t page_size, mmap_size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000956 char *buf, *mmaps[8];
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200957 event_t *event;
958 uint32_t size;
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000959
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200960 perf_event_ops__fill_defaults(ops);
961
Arnaldo Carvalho de Melo1b759622010-01-14 18:30:04 -0200962 page_size = sysconf(_SC_PAGESIZE);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200963
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000964 page_offset = page_size * (data_offset / page_size);
965 file_offset = page_offset;
966 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200967
Thomas Gleixnerd6513282010-11-30 17:49:44 +0000968 if (data_offset + data_size < file_size)
969 file_size = data_offset + data_size;
970
Thomas Gleixner55b44622010-11-30 17:49:46 +0000971 progress_next = file_size / 16;
972 progress = ui_progress__new("Processing events...", file_size);
973 if (progress == NULL)
974 return -1;
975
976 mmap_size = session->mmap_window;
977 if (mmap_size > file_size)
978 mmap_size = file_size;
979
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000980 memset(mmaps, 0, sizeof(mmaps));
981
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200982 mmap_prot = PROT_READ;
983 mmap_flags = MAP_SHARED;
984
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000985 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200986 mmap_prot |= PROT_WRITE;
987 mmap_flags = MAP_PRIVATE;
988 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200989remap:
Thomas Gleixner55b44622010-11-30 17:49:46 +0000990 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
991 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200992 if (buf == MAP_FAILED) {
993 pr_err("failed to mmap file\n");
994 err = -errno;
995 goto out_err;
996 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000997 mmaps[map_idx] = buf;
998 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +0000999 file_pos = file_offset + head;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001000
1001more:
1002 event = (event_t *)(buf + head);
1003
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001004 if (session->header.needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001005 perf_event_header__bswap(&event->header);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001006 size = event->header.size;
1007 if (size == 0)
1008 size = 8;
1009
Thomas Gleixner55b44622010-11-30 17:49:46 +00001010 if (head + event->header.size >= mmap_size) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001011 if (mmaps[map_idx]) {
1012 munmap(mmaps[map_idx], mmap_size);
1013 mmaps[map_idx] = NULL;
1014 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001015
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001016 page_offset = page_size * (head / page_size);
1017 file_offset += page_offset;
1018 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001019 goto remap;
1020 }
1021
1022 size = event->header.size;
1023
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001024 if (size == 0 ||
1025 perf_session__process_event(session, event, ops, file_pos) < 0) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001026 dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n",
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001027 file_offset + head, event->header.size,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001028 event->header.type);
1029 /*
1030 * assume we lost track of the stream, check alignment, and
1031 * increment a single u64 in the hope to catch on again 'soon'.
1032 */
1033 if (unlikely(head & 7))
1034 head &= ~7ULL;
1035
1036 size = 8;
1037 }
1038
1039 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001040 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001041
Thomas Gleixner55b44622010-11-30 17:49:46 +00001042 if (file_pos >= progress_next) {
1043 progress_next += file_size / 16;
1044 ui_progress__update(progress, file_pos);
1045 }
1046
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001047 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001048 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001049
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001050 err = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001051 /* do the final flush for ordered samples */
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001052 session->ordered_samples.next_flush = ULLONG_MAX;
1053 flush_sample_queue(session, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001054out_err:
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -03001055 ui_progress__delete(progress);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001056 perf_session__warn_about_errors(session, ops);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001057 perf_session_free_sample_buffers(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001058 return err;
1059}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001060
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001061int perf_session__process_events(struct perf_session *self,
1062 struct perf_event_ops *ops)
1063{
1064 int err;
1065
1066 if (perf_session__register_idle_thread(self) == NULL)
1067 return -ENOMEM;
1068
Tom Zanussi8dc58102010-04-01 23:59:15 -05001069 if (!self->fd_pipe)
1070 err = __perf_session__process_events(self,
1071 self->header.data_offset,
1072 self->header.data_size,
1073 self->size, ops);
1074 else
1075 err = __perf_session__process_pipe_events(self, ops);
Dave Martin88ca8952010-07-27 11:46:12 -03001076
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001077 return err;
1078}
1079
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001080bool perf_session__has_traces(struct perf_session *self, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001081{
1082 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001083 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1084 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001085 }
1086
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001087 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001088}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001089
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001090int perf_session__set_kallsyms_ref_reloc_sym(struct map **maps,
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001091 const char *symbol_name,
1092 u64 addr)
1093{
1094 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001095 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001096 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001097
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001098 ref = zalloc(sizeof(struct ref_reloc_sym));
1099 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001100 return -ENOMEM;
1101
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001102 ref->name = strdup(symbol_name);
1103 if (ref->name == NULL) {
1104 free(ref);
1105 return -ENOMEM;
1106 }
1107
1108 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001109 if (bracket)
1110 *bracket = '\0';
1111
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001112 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001113
1114 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001115 struct kmap *kmap = map__kmap(maps[i]);
1116 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001117 }
1118
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001119 return 0;
1120}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001121
1122size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
1123{
1124 return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
1125 __dsos__fprintf(&self->host_machine.user_dsos, fp) +
1126 machines__fprintf_dsos(&self->machines, fp);
1127}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001128
1129size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
1130 bool with_hits)
1131{
1132 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
1133 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
1134}