blob: d43e56cd6f28e159dd131a4ac750d3720f638bad [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
Tom Zanussi454c4072010-05-01 01:41:20 -0500128struct perf_session *perf_session__new(const char *filename, int mode, bool force, bool repipe)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200129{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200130 size_t len = filename ? strlen(filename) + 1 : 0;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200131 struct perf_session *self = zalloc(sizeof(*self) + len);
132
133 if (self == NULL)
134 goto out;
135
136 if (perf_header__init(&self->header) < 0)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200137 goto out_free;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200138
139 memcpy(self->filename, filename, len);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200140 self->threads = RB_ROOT;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300141 INIT_LIST_HEAD(&self->dead_threads);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300142 self->hists_tree = RB_ROOT;
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200143 self->last_match = NULL;
Thomas Gleixner55b44622010-11-30 17:49:46 +0000144 /*
145 * On 64bit we can mmap the data file in one go. No need for tiny mmap
146 * slices. On 32bit we use 32MB.
147 */
148#if BITS_PER_LONG == 64
149 self->mmap_window = ULLONG_MAX;
150#else
151 self->mmap_window = 32 * 1024 * 1024ULL;
152#endif
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300153 self->machines = RB_ROOT;
Tom Zanussi454c4072010-05-01 01:41:20 -0500154 self->repipe = repipe;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000155 INIT_LIST_HEAD(&self->ordered_samples.samples);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000156 INIT_LIST_HEAD(&self->ordered_samples.sample_cache);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000157 INIT_LIST_HEAD(&self->ordered_samples.to_free);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300158 machine__init(&self->host_machine, "", HOST_KERNEL_ID);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200159
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200160 if (mode == O_RDONLY) {
161 if (perf_session__open(self, force) < 0)
162 goto out_delete;
163 } else if (mode == O_WRONLY) {
164 /*
165 * In O_RDONLY mode this will be performed when reading the
166 * kernel MMAP event, in event__process_mmap().
167 */
168 if (perf_session__create_kernel_maps(self) < 0)
169 goto out_delete;
170 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200171
Tom Zanussi8dc58102010-04-01 23:59:15 -0500172 perf_session__update_sample_type(self);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200173out:
174 return self;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200175out_free:
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200176 free(self);
177 return NULL;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200178out_delete:
179 perf_session__delete(self);
180 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200181}
182
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300183static void perf_session__delete_dead_threads(struct perf_session *self)
184{
185 struct thread *n, *t;
186
187 list_for_each_entry_safe(t, n, &self->dead_threads, node) {
188 list_del(&t->node);
189 thread__delete(t);
190 }
191}
192
193static void perf_session__delete_threads(struct perf_session *self)
194{
195 struct rb_node *nd = rb_first(&self->threads);
196
197 while (nd) {
198 struct thread *t = rb_entry(nd, struct thread, rb_node);
199
200 rb_erase(&t->rb_node, &self->threads);
201 nd = rb_next(nd);
202 thread__delete(t);
203 }
204}
205
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200206void perf_session__delete(struct perf_session *self)
207{
208 perf_header__exit(&self->header);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300209 perf_session__destroy_kernel_maps(self);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300210 perf_session__delete_dead_threads(self);
211 perf_session__delete_threads(self);
212 machine__exit(&self->host_machine);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200213 close(self->fd);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200214 free(self);
215}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200216
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300217void perf_session__remove_thread(struct perf_session *self, struct thread *th)
218{
Arnaldo Carvalho de Melo70597f22010-08-02 18:59:28 -0300219 self->last_match = NULL;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300220 rb_erase(&th->rb_node, &self->threads);
221 /*
222 * We may have references to this thread, for instance in some hist_entry
223 * instances, so just move them to a separate list.
224 */
225 list_add_tail(&th->node, &self->dead_threads);
226}
227
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200228static bool symbol__match_parent_regex(struct symbol *sym)
229{
230 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
231 return 1;
232
233 return 0;
234}
235
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300236struct map_symbol *perf_session__resolve_callchain(struct perf_session *self,
237 struct thread *thread,
238 struct ip_callchain *chain,
239 struct symbol **parent)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200240{
241 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200242 unsigned int i;
Arnaldo Carvalho de Meload5b2172010-04-02 10:04:18 -0300243 struct map_symbol *syms = calloc(chain->nr, sizeof(*syms));
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200244
Arnaldo Carvalho de Meload5b2172010-04-02 10:04:18 -0300245 if (!syms)
246 return NULL;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200247
248 for (i = 0; i < chain->nr; i++) {
249 u64 ip = chain->ips[i];
250 struct addr_location al;
251
252 if (ip >= PERF_CONTEXT_MAX) {
253 switch (ip) {
254 case PERF_CONTEXT_HV:
255 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
256 case PERF_CONTEXT_KERNEL:
257 cpumode = PERF_RECORD_MISC_KERNEL; break;
258 case PERF_CONTEXT_USER:
259 cpumode = PERF_RECORD_MISC_USER; break;
260 default:
261 break;
262 }
263 continue;
264 }
265
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800266 al.filtered = false;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200267 thread__find_addr_location(thread, self, cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800268 MAP__FUNCTION, thread->pid, ip, &al, NULL);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200269 if (al.sym != NULL) {
270 if (sort__has_parent && !*parent &&
271 symbol__match_parent_regex(al.sym))
272 *parent = al.sym;
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200273 if (!symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200274 break;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300275 syms[i].map = al.map;
276 syms[i].sym = al.sym;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200277 }
278 }
279
280 return syms;
281}
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200282
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200283static int process_event_synth_stub(event_t *event __used,
284 struct perf_session *session __used)
285{
286 dump_printf(": unhandled!\n");
287 return 0;
288}
289
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200290static int process_event_stub(event_t *event __used,
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200291 struct sample_data *sample __used,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200292 struct perf_session *session __used)
293{
294 dump_printf(": unhandled!\n");
295 return 0;
296}
297
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200298static int process_finished_round_stub(event_t *event __used,
299 struct perf_session *session __used,
300 struct perf_event_ops *ops __used)
301{
302 dump_printf(": unhandled!\n");
303 return 0;
304}
305
306static int process_finished_round(event_t *event,
307 struct perf_session *session,
308 struct perf_event_ops *ops);
309
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200310static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
311{
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200312 if (handler->sample == NULL)
313 handler->sample = process_event_stub;
314 if (handler->mmap == NULL)
315 handler->mmap = process_event_stub;
316 if (handler->comm == NULL)
317 handler->comm = process_event_stub;
318 if (handler->fork == NULL)
319 handler->fork = process_event_stub;
320 if (handler->exit == NULL)
321 handler->exit = process_event_stub;
322 if (handler->lost == NULL)
Arnaldo Carvalho de Melo37982ba2010-11-26 18:31:54 -0200323 handler->lost = event__process_lost;
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200324 if (handler->read == NULL)
325 handler->read = process_event_stub;
326 if (handler->throttle == NULL)
327 handler->throttle = process_event_stub;
328 if (handler->unthrottle == NULL)
329 handler->unthrottle = process_event_stub;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500330 if (handler->attr == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200331 handler->attr = process_event_synth_stub;
Tom Zanussicd19a032010-04-01 23:59:20 -0500332 if (handler->event_type == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200333 handler->event_type = process_event_synth_stub;
Tom Zanussi92155452010-04-01 23:59:21 -0500334 if (handler->tracing_data == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200335 handler->tracing_data = process_event_synth_stub;
Tom Zanussic7929e42010-04-01 23:59:22 -0500336 if (handler->build_id == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200337 handler->build_id = process_event_synth_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200338 if (handler->finished_round == NULL) {
339 if (handler->ordered_samples)
340 handler->finished_round = process_finished_round;
341 else
342 handler->finished_round = process_finished_round_stub;
343 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200344}
345
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200346void mem_bswap_64(void *src, int byte_size)
347{
348 u64 *m = src;
349
350 while (byte_size > 0) {
351 *m = bswap_64(*m);
352 byte_size -= sizeof(u64);
353 ++m;
354 }
355}
356
357static void event__all64_swap(event_t *self)
358{
359 struct perf_event_header *hdr = &self->header;
360 mem_bswap_64(hdr + 1, self->header.size - sizeof(*hdr));
361}
362
363static void event__comm_swap(event_t *self)
364{
365 self->comm.pid = bswap_32(self->comm.pid);
366 self->comm.tid = bswap_32(self->comm.tid);
367}
368
369static void event__mmap_swap(event_t *self)
370{
371 self->mmap.pid = bswap_32(self->mmap.pid);
372 self->mmap.tid = bswap_32(self->mmap.tid);
373 self->mmap.start = bswap_64(self->mmap.start);
374 self->mmap.len = bswap_64(self->mmap.len);
375 self->mmap.pgoff = bswap_64(self->mmap.pgoff);
376}
377
378static void event__task_swap(event_t *self)
379{
380 self->fork.pid = bswap_32(self->fork.pid);
381 self->fork.tid = bswap_32(self->fork.tid);
382 self->fork.ppid = bswap_32(self->fork.ppid);
383 self->fork.ptid = bswap_32(self->fork.ptid);
384 self->fork.time = bswap_64(self->fork.time);
385}
386
387static void event__read_swap(event_t *self)
388{
389 self->read.pid = bswap_32(self->read.pid);
390 self->read.tid = bswap_32(self->read.tid);
391 self->read.value = bswap_64(self->read.value);
392 self->read.time_enabled = bswap_64(self->read.time_enabled);
393 self->read.time_running = bswap_64(self->read.time_running);
394 self->read.id = bswap_64(self->read.id);
395}
396
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500397static void event__attr_swap(event_t *self)
398{
399 size_t size;
400
401 self->attr.attr.type = bswap_32(self->attr.attr.type);
402 self->attr.attr.size = bswap_32(self->attr.attr.size);
403 self->attr.attr.config = bswap_64(self->attr.attr.config);
404 self->attr.attr.sample_period = bswap_64(self->attr.attr.sample_period);
405 self->attr.attr.sample_type = bswap_64(self->attr.attr.sample_type);
406 self->attr.attr.read_format = bswap_64(self->attr.attr.read_format);
407 self->attr.attr.wakeup_events = bswap_32(self->attr.attr.wakeup_events);
408 self->attr.attr.bp_type = bswap_32(self->attr.attr.bp_type);
409 self->attr.attr.bp_addr = bswap_64(self->attr.attr.bp_addr);
410 self->attr.attr.bp_len = bswap_64(self->attr.attr.bp_len);
411
412 size = self->header.size;
413 size -= (void *)&self->attr.id - (void *)self;
414 mem_bswap_64(self->attr.id, size);
415}
416
Tom Zanussicd19a032010-04-01 23:59:20 -0500417static void event__event_type_swap(event_t *self)
418{
419 self->event_type.event_type.event_id =
420 bswap_64(self->event_type.event_type.event_id);
421}
422
Tom Zanussi92155452010-04-01 23:59:21 -0500423static void event__tracing_data_swap(event_t *self)
424{
425 self->tracing_data.size = bswap_32(self->tracing_data.size);
426}
427
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200428typedef void (*event__swap_op)(event_t *self);
429
430static event__swap_op event__swap_ops[] = {
431 [PERF_RECORD_MMAP] = event__mmap_swap,
432 [PERF_RECORD_COMM] = event__comm_swap,
433 [PERF_RECORD_FORK] = event__task_swap,
434 [PERF_RECORD_EXIT] = event__task_swap,
435 [PERF_RECORD_LOST] = event__all64_swap,
436 [PERF_RECORD_READ] = event__read_swap,
437 [PERF_RECORD_SAMPLE] = event__all64_swap,
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500438 [PERF_RECORD_HEADER_ATTR] = event__attr_swap,
Tom Zanussicd19a032010-04-01 23:59:20 -0500439 [PERF_RECORD_HEADER_EVENT_TYPE] = event__event_type_swap,
Tom Zanussi92155452010-04-01 23:59:21 -0500440 [PERF_RECORD_HEADER_TRACING_DATA] = event__tracing_data_swap,
Tom Zanussic7929e42010-04-01 23:59:22 -0500441 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
Tom Zanussi8dc58102010-04-01 23:59:15 -0500442 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200443};
444
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200445struct sample_queue {
446 u64 timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000447 u64 file_offset;
Thomas Gleixner28990f72010-11-30 17:49:35 +0000448 event_t *event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200449 struct list_head list;
450};
451
Thomas Gleixner020bb752010-11-30 17:49:53 +0000452static void perf_session_free_sample_buffers(struct perf_session *session)
453{
454 struct ordered_samples *os = &session->ordered_samples;
455
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000456 while (!list_empty(&os->to_free)) {
Thomas Gleixner020bb752010-11-30 17:49:53 +0000457 struct sample_queue *sq;
458
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000459 sq = list_entry(os->to_free.next, struct sample_queue, list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000460 list_del(&sq->list);
461 free(sq);
462 }
463}
464
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100465static int perf_session_deliver_event(struct perf_session *session,
466 event_t *event,
467 struct sample_data *sample,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000468 struct perf_event_ops *ops,
469 u64 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100470
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200471static void flush_sample_queue(struct perf_session *s,
472 struct perf_event_ops *ops)
473{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000474 struct ordered_samples *os = &s->ordered_samples;
475 struct list_head *head = &os->samples;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200476 struct sample_queue *tmp, *iter;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200477 struct sample_data sample;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000478 u64 limit = os->next_flush;
479 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200480
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200481 if (!ops->ordered_samples || !limit)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200482 return;
483
484 list_for_each_entry_safe(iter, tmp, head, list) {
485 if (iter->timestamp > limit)
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000486 break;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200487
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200488 event__parse_sample(iter->event, s, &sample);
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000489 perf_session_deliver_event(s, iter->event, &sample, ops,
490 iter->file_offset);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200491
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000492 os->last_flush = iter->timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200493 list_del(&iter->list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000494 list_add(&iter->list, &os->sample_cache);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200495 }
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000496
497 if (list_empty(head)) {
498 os->last_sample = NULL;
499 } else if (last_ts <= limit) {
500 os->last_sample =
501 list_entry(head->prev, struct sample_queue, list);
502 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200503}
504
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200505/*
506 * When perf record finishes a pass on every buffers, it records this pseudo
507 * event.
508 * We record the max timestamp t found in the pass n.
509 * Assuming these timestamps are monotonic across cpus, we know that if
510 * a buffer still has events with timestamps below t, they will be all
511 * available and then read in the pass n + 1.
512 * Hence when we start to read the pass n + 2, we can safely flush every
513 * events with timestamps below t.
514 *
515 * ============ PASS n =================
516 * CPU 0 | CPU 1
517 * |
518 * cnt1 timestamps | cnt2 timestamps
519 * 1 | 2
520 * 2 | 3
521 * - | 4 <--- max recorded
522 *
523 * ============ PASS n + 1 ==============
524 * CPU 0 | CPU 1
525 * |
526 * cnt1 timestamps | cnt2 timestamps
527 * 3 | 5
528 * 4 | 6
529 * 5 | 7 <---- max recorded
530 *
531 * Flush every events below timestamp 4
532 *
533 * ============ PASS n + 2 ==============
534 * CPU 0 | CPU 1
535 * |
536 * cnt1 timestamps | cnt2 timestamps
537 * 6 | 8
538 * 7 | 9
539 * - | 10
540 *
541 * Flush every events below timestamp 7
542 * etc...
543 */
544static int process_finished_round(event_t *event __used,
545 struct perf_session *session,
546 struct perf_event_ops *ops)
547{
548 flush_sample_queue(session, ops);
549 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
550
551 return 0;
552}
553
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200554/* The queue is ordered by time */
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100555static void __queue_event(struct sample_queue *new, struct perf_session *s)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200556{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000557 struct ordered_samples *os = &s->ordered_samples;
558 struct sample_queue *sample = os->last_sample;
559 u64 timestamp = new->timestamp;
560 struct list_head *p;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200561
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000562 os->last_sample = new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200563
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000564 if (!sample) {
565 list_add(&new->list, &os->samples);
566 os->max_timestamp = timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200567 return;
568 }
569
570 /*
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000571 * last_sample might point to some random place in the list as it's
572 * the last queued event. We expect that the new event is close to
573 * this.
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200574 */
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000575 if (sample->timestamp <= timestamp) {
576 while (sample->timestamp <= timestamp) {
577 p = sample->list.next;
578 if (p == &os->samples) {
579 list_add_tail(&new->list, &os->samples);
580 os->max_timestamp = timestamp;
581 return;
582 }
583 sample = list_entry(p, struct sample_queue, list);
584 }
585 list_add_tail(&new->list, &sample->list);
586 } else {
587 while (sample->timestamp > timestamp) {
588 p = sample->list.prev;
589 if (p == &os->samples) {
590 list_add(&new->list, &os->samples);
591 return;
592 }
593 sample = list_entry(p, struct sample_queue, list);
594 }
595 list_add(&new->list, &sample->list);
596 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200597}
598
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000599#define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
600
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100601static int perf_session_queue_event(struct perf_session *s, event_t *event,
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000602 struct sample_data *data, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200603{
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000604 struct ordered_samples *os = &s->ordered_samples;
605 struct list_head *sc = &os->sample_cache;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200606 u64 timestamp = data->time;
607 struct sample_queue *new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200608
Thomas Gleixner79a14c12010-12-07 12:48:44 +0000609 if (!timestamp || timestamp == ~0ULL)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100610 return -ETIME;
611
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200612 if (timestamp < s->ordered_samples.last_flush) {
613 printf("Warning: Timestamp below last timeslice flush\n");
614 return -EINVAL;
615 }
616
Thomas Gleixner020bb752010-11-30 17:49:53 +0000617 if (!list_empty(sc)) {
618 new = list_entry(sc->next, struct sample_queue, list);
619 list_del(&new->list);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000620 } else if (os->sample_buffer) {
621 new = os->sample_buffer + os->sample_buffer_idx;
622 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
623 os->sample_buffer = NULL;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000624 } else {
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000625 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
626 if (!os->sample_buffer)
Thomas Gleixner020bb752010-11-30 17:49:53 +0000627 return -ENOMEM;
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000628 list_add(&os->sample_buffer->list, &os->to_free);
629 os->sample_buffer_idx = 2;
630 new = os->sample_buffer + 1;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000631 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200632
633 new->timestamp = timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000634 new->file_offset = file_offset;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000635 new->event = event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200636
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100637 __queue_event(new, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200638
639 return 0;
640}
641
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200642static void callchain__dump(struct sample_data *sample)
643{
644 unsigned int i;
645
646 if (!dump_trace)
647 return;
648
649 printf("... chain: nr:%Lu\n", sample->callchain->nr);
650
651 for (i = 0; i < sample->callchain->nr; i++)
652 printf("..... %2d: %016Lx\n", i, sample->callchain->ips[i]);
653}
654
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200655static void perf_session__print_tstamp(struct perf_session *session,
656 event_t *event,
657 struct sample_data *sample)
658{
659 if (event->header.type != PERF_RECORD_SAMPLE &&
660 !session->sample_id_all) {
661 fputs("-1 -1 ", stdout);
662 return;
663 }
664
665 if ((session->sample_type & PERF_SAMPLE_CPU))
666 printf("%u ", sample->cpu);
667
668 if (session->sample_type & PERF_SAMPLE_TIME)
669 printf("%Lu ", sample->time);
670}
671
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000672static void dump_event(struct perf_session *session, event_t *event,
673 u64 file_offset, struct sample_data *sample)
674{
675 if (!dump_trace)
676 return;
677
678 dump_printf("\n%#Lx [%#x]: event: %d\n", file_offset,
679 event->header.size, event->header.type);
680
681 trace_event(event);
682
683 if (sample)
684 perf_session__print_tstamp(session, event, sample);
685
686 dump_printf("%#Lx [%#x]: PERF_RECORD_%s",
687 file_offset, event->header.size,
688 event__get_event_name(event->header.type));
689}
690
691static void dump_sample(struct perf_session *session, event_t *event,
692 struct sample_data *sample)
693{
694 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
695 sample->pid, sample->tid, sample->ip, sample->period);
696
697 if (session->sample_type & PERF_SAMPLE_CALLCHAIN)
698 callchain__dump(sample);
699}
700
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100701static int perf_session_deliver_event(struct perf_session *session,
702 event_t *event,
703 struct sample_data *sample,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000704 struct perf_event_ops *ops,
705 u64 file_offset __used)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100706{
707 switch (event->header.type) {
708 case PERF_RECORD_SAMPLE:
709 return ops->sample(event, sample, session);
710 case PERF_RECORD_MMAP:
711 return ops->mmap(event, sample, session);
712 case PERF_RECORD_COMM:
713 return ops->comm(event, sample, session);
714 case PERF_RECORD_FORK:
715 return ops->fork(event, sample, session);
716 case PERF_RECORD_EXIT:
717 return ops->exit(event, sample, session);
718 case PERF_RECORD_LOST:
719 return ops->lost(event, sample, session);
720 case PERF_RECORD_READ:
721 return ops->read(event, sample, session);
722 case PERF_RECORD_THROTTLE:
723 return ops->throttle(event, sample, session);
724 case PERF_RECORD_UNTHROTTLE:
725 return ops->unthrottle(event, sample, session);
726 default:
727 ++session->hists.stats.nr_unknown_events;
728 return -1;
729 }
730}
731
732static int perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200733 event_t *event,
734 struct perf_event_ops *ops,
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000735 u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200736{
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200737 struct sample_data sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100738 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200739
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100740 if (session->header.needs_swap && event__swap_ops[event->header.type])
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200741 event__swap_ops[event->header.type](event);
742
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000743 if (event->header.type >= PERF_RECORD_HEADER_MAX)
744 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200745
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000746 hists__inc_nr_events(&session->hists, event->header.type);
747
748 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
749 dump_event(session, event, file_offset, NULL);
750 else {
751 event__parse_sample(event, session, &sample);
752 dump_event(session, event, file_offset, &sample);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200753 }
754
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100755 /* These events are processed right away */
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200756 switch (event->header.type) {
757 case PERF_RECORD_SAMPLE:
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100758 if (session->sample_type & PERF_SAMPLE_CALLCHAIN) {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200759 if (!ip_callchain__valid(sample.callchain, event)) {
760 pr_debug("call-chain problem with event, "
761 "skipping it.\n");
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100762 ++session->hists.stats.nr_invalid_chains;
763 session->hists.stats.total_invalid_chains +=
764 sample.period;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200765 return 0;
766 }
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200767 }
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000768 dump_sample(session, event, &sample);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100769 break;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200770
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500771 case PERF_RECORD_HEADER_ATTR:
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100772 return ops->attr(event, session);
Tom Zanussicd19a032010-04-01 23:59:20 -0500773 case PERF_RECORD_HEADER_EVENT_TYPE:
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100774 return ops->event_type(event, session);
Tom Zanussi92155452010-04-01 23:59:21 -0500775 case PERF_RECORD_HEADER_TRACING_DATA:
776 /* setup for reading amidst mmap */
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100777 lseek(session->fd, file_offset, SEEK_SET);
778 return ops->tracing_data(event, session);
Tom Zanussic7929e42010-04-01 23:59:22 -0500779 case PERF_RECORD_HEADER_BUILD_ID:
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100780 return ops->build_id(event, session);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200781 case PERF_RECORD_FINISHED_ROUND:
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100782 return ops->finished_round(event, session, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200783 default:
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100784 break;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200785 }
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100786
787 if (ops->ordered_samples) {
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000788 ret = perf_session_queue_event(session, event, &sample,
789 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100790 if (ret != -ETIME)
791 return ret;
792 }
793
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000794 return perf_session_deliver_event(session, event, &sample, ops,
795 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200796}
797
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200798void perf_event_header__bswap(struct perf_event_header *self)
799{
800 self->type = bswap_32(self->type);
801 self->misc = bswap_16(self->misc);
802 self->size = bswap_16(self->size);
803}
804
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200805static struct thread *perf_session__register_idle_thread(struct perf_session *self)
806{
807 struct thread *thread = perf_session__findnew(self, 0);
808
809 if (thread == NULL || thread__set_comm(thread, "swapper")) {
810 pr_err("problem inserting idle task.\n");
811 thread = NULL;
812 }
813
814 return thread;
815}
816
Tom Zanussi8dc58102010-04-01 23:59:15 -0500817int do_read(int fd, void *buf, size_t size)
818{
819 void *buf_start = buf;
820
821 while (size) {
822 int ret = read(fd, buf, size);
823
824 if (ret <= 0)
825 return ret;
826
827 size -= ret;
828 buf += ret;
829 }
830
831 return buf - buf_start;
832}
833
834#define session_done() (*(volatile int *)(&session_done))
835volatile int session_done;
836
837static int __perf_session__process_pipe_events(struct perf_session *self,
838 struct perf_event_ops *ops)
839{
840 event_t event;
841 uint32_t size;
842 int skip = 0;
843 u64 head;
844 int err;
845 void *p;
846
847 perf_event_ops__fill_defaults(ops);
848
849 head = 0;
850more:
851 err = do_read(self->fd, &event, sizeof(struct perf_event_header));
852 if (err <= 0) {
853 if (err == 0)
854 goto done;
855
856 pr_err("failed to read event header\n");
857 goto out_err;
858 }
859
860 if (self->header.needs_swap)
861 perf_event_header__bswap(&event.header);
862
863 size = event.header.size;
864 if (size == 0)
865 size = 8;
866
867 p = &event;
868 p += sizeof(struct perf_event_header);
869
Tom Zanussi794e43b2010-05-05 00:27:40 -0500870 if (size - sizeof(struct perf_event_header)) {
871 err = do_read(self->fd, p,
872 size - sizeof(struct perf_event_header));
873 if (err <= 0) {
874 if (err == 0) {
875 pr_err("unexpected end of event stream\n");
876 goto done;
877 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500878
Tom Zanussi794e43b2010-05-05 00:27:40 -0500879 pr_err("failed to read event data\n");
880 goto out_err;
881 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500882 }
883
884 if (size == 0 ||
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000885 (skip = perf_session__process_event(self, &event, ops, head)) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -0500886 dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n",
887 head, event.header.size, event.header.type);
888 /*
889 * assume we lost track of the stream, check alignment, and
890 * increment a single u64 in the hope to catch on again 'soon'.
891 */
892 if (unlikely(head & 7))
893 head &= ~7ULL;
894
895 size = 8;
896 }
897
898 head += size;
899
Tom Zanussi8dc58102010-04-01 23:59:15 -0500900 if (skip > 0)
901 head += skip;
902
903 if (!session_done())
904 goto more;
905done:
906 err = 0;
907out_err:
Thomas Gleixner020bb752010-11-30 17:49:53 +0000908 perf_session_free_sample_buffers(self);
Tom Zanussi8dc58102010-04-01 23:59:15 -0500909 return err;
910}
911
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000912int __perf_session__process_events(struct perf_session *session,
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200913 u64 data_offset, u64 data_size,
914 u64 file_size, struct perf_event_ops *ops)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200915{
Thomas Gleixner55b44622010-11-30 17:49:46 +0000916 u64 head, page_offset, file_offset, file_pos, progress_next;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000917 int err, mmap_prot, mmap_flags, map_idx = 0;
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000918 struct ui_progress *progress;
Thomas Gleixner55b44622010-11-30 17:49:46 +0000919 size_t page_size, mmap_size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000920 char *buf, *mmaps[8];
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200921 event_t *event;
922 uint32_t size;
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000923
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200924 perf_event_ops__fill_defaults(ops);
925
Arnaldo Carvalho de Melo1b759622010-01-14 18:30:04 -0200926 page_size = sysconf(_SC_PAGESIZE);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200927
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000928 page_offset = page_size * (data_offset / page_size);
929 file_offset = page_offset;
930 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200931
Thomas Gleixnerd6513282010-11-30 17:49:44 +0000932 if (data_offset + data_size < file_size)
933 file_size = data_offset + data_size;
934
Thomas Gleixner55b44622010-11-30 17:49:46 +0000935 progress_next = file_size / 16;
936 progress = ui_progress__new("Processing events...", file_size);
937 if (progress == NULL)
938 return -1;
939
940 mmap_size = session->mmap_window;
941 if (mmap_size > file_size)
942 mmap_size = file_size;
943
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000944 memset(mmaps, 0, sizeof(mmaps));
945
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200946 mmap_prot = PROT_READ;
947 mmap_flags = MAP_SHARED;
948
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000949 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200950 mmap_prot |= PROT_WRITE;
951 mmap_flags = MAP_PRIVATE;
952 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200953remap:
Thomas Gleixner55b44622010-11-30 17:49:46 +0000954 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
955 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200956 if (buf == MAP_FAILED) {
957 pr_err("failed to mmap file\n");
958 err = -errno;
959 goto out_err;
960 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000961 mmaps[map_idx] = buf;
962 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +0000963 file_pos = file_offset + head;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200964
965more:
966 event = (event_t *)(buf + head);
967
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000968 if (session->header.needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200969 perf_event_header__bswap(&event->header);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200970 size = event->header.size;
971 if (size == 0)
972 size = 8;
973
Thomas Gleixner55b44622010-11-30 17:49:46 +0000974 if (head + event->header.size >= mmap_size) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000975 if (mmaps[map_idx]) {
976 munmap(mmaps[map_idx], mmap_size);
977 mmaps[map_idx] = NULL;
978 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200979
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000980 page_offset = page_size * (head / page_size);
981 file_offset += page_offset;
982 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200983 goto remap;
984 }
985
986 size = event->header.size;
987
Thomas Gleixnerd6513282010-11-30 17:49:44 +0000988 if (size == 0 ||
989 perf_session__process_event(session, event, ops, file_pos) < 0) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200990 dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n",
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000991 file_offset + head, event->header.size,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200992 event->header.type);
993 /*
994 * assume we lost track of the stream, check alignment, and
995 * increment a single u64 in the hope to catch on again 'soon'.
996 */
997 if (unlikely(head & 7))
998 head &= ~7ULL;
999
1000 size = 8;
1001 }
1002
1003 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001004 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001005
Thomas Gleixner55b44622010-11-30 17:49:46 +00001006 if (file_pos >= progress_next) {
1007 progress_next += file_size / 16;
1008 ui_progress__update(progress, file_pos);
1009 }
1010
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001011 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001012 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001013
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001014 err = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001015 /* do the final flush for ordered samples */
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001016 session->ordered_samples.next_flush = ULLONG_MAX;
1017 flush_sample_queue(session, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001018out_err:
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -03001019 ui_progress__delete(progress);
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -02001020
1021 if (ops->lost == event__process_lost &&
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001022 session->hists.stats.total_lost != 0) {
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -02001023 ui__warning("Processed %Lu events and LOST %Lu!\n\n"
1024 "Check IO/CPU overload!\n\n",
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001025 session->hists.stats.total_period,
1026 session->hists.stats.total_lost);
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -02001027 }
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001028
1029 if (session->hists.stats.nr_unknown_events != 0) {
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -02001030 ui__warning("Found %u unknown events!\n\n"
1031 "Is this an older tool processing a perf.data "
1032 "file generated by a more recent tool?\n\n"
1033 "If that is not the case, consider "
1034 "reporting to linux-kernel@vger.kernel.org.\n\n",
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001035 session->hists.stats.nr_unknown_events);
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -02001036 }
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001037
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001038 if (session->hists.stats.nr_invalid_chains != 0) {
1039 ui__warning("Found invalid callchains!\n\n"
1040 "%u out of %u events were discarded for this reason.\n\n"
1041 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1042 session->hists.stats.nr_invalid_chains,
1043 session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
1044 }
1045
Thomas Gleixner020bb752010-11-30 17:49:53 +00001046 perf_session_free_sample_buffers(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001047 return err;
1048}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001049
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001050int perf_session__process_events(struct perf_session *self,
1051 struct perf_event_ops *ops)
1052{
1053 int err;
1054
1055 if (perf_session__register_idle_thread(self) == NULL)
1056 return -ENOMEM;
1057
Tom Zanussi8dc58102010-04-01 23:59:15 -05001058 if (!self->fd_pipe)
1059 err = __perf_session__process_events(self,
1060 self->header.data_offset,
1061 self->header.data_size,
1062 self->size, ops);
1063 else
1064 err = __perf_session__process_pipe_events(self, ops);
Dave Martin88ca8952010-07-27 11:46:12 -03001065
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001066 return err;
1067}
1068
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001069bool perf_session__has_traces(struct perf_session *self, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001070{
1071 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001072 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1073 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001074 }
1075
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001076 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001077}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001078
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001079int perf_session__set_kallsyms_ref_reloc_sym(struct map **maps,
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001080 const char *symbol_name,
1081 u64 addr)
1082{
1083 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001084 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001085 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001086
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001087 ref = zalloc(sizeof(struct ref_reloc_sym));
1088 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001089 return -ENOMEM;
1090
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001091 ref->name = strdup(symbol_name);
1092 if (ref->name == NULL) {
1093 free(ref);
1094 return -ENOMEM;
1095 }
1096
1097 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001098 if (bracket)
1099 *bracket = '\0';
1100
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001101 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001102
1103 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001104 struct kmap *kmap = map__kmap(maps[i]);
1105 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001106 }
1107
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001108 return 0;
1109}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001110
1111size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
1112{
1113 return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
1114 __dsos__fprintf(&self->host_machine.user_dsos, fp) +
1115 machines__fprintf_dsos(&self->machines, fp);
1116}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001117
1118size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
1119 bool with_hits)
1120{
1121 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
1122 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
1123}