blob: a4339548de67207659fc1d3fe0fc48d229801fd6 [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 Gleixner55b446292010-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,
468 struct perf_event_ops *ops);
469
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200470static void flush_sample_queue(struct perf_session *s,
471 struct perf_event_ops *ops)
472{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000473 struct ordered_samples *os = &s->ordered_samples;
474 struct list_head *head = &os->samples;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200475 struct sample_queue *tmp, *iter;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200476 struct sample_data sample;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000477 u64 limit = os->next_flush;
478 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200479
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200480 if (!ops->ordered_samples || !limit)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200481 return;
482
483 list_for_each_entry_safe(iter, tmp, head, list) {
484 if (iter->timestamp > limit)
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000485 break;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200486
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200487 event__parse_sample(iter->event, s, &sample);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100488 perf_session_deliver_event(s, iter->event, &sample, ops);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200489
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000490 os->last_flush = iter->timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200491 list_del(&iter->list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000492 list_add(&iter->list, &os->sample_cache);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200493 }
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000494
495 if (list_empty(head)) {
496 os->last_sample = NULL;
497 } else if (last_ts <= limit) {
498 os->last_sample =
499 list_entry(head->prev, struct sample_queue, list);
500 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200501}
502
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200503/*
504 * When perf record finishes a pass on every buffers, it records this pseudo
505 * event.
506 * We record the max timestamp t found in the pass n.
507 * Assuming these timestamps are monotonic across cpus, we know that if
508 * a buffer still has events with timestamps below t, they will be all
509 * available and then read in the pass n + 1.
510 * Hence when we start to read the pass n + 2, we can safely flush every
511 * events with timestamps below t.
512 *
513 * ============ PASS n =================
514 * CPU 0 | CPU 1
515 * |
516 * cnt1 timestamps | cnt2 timestamps
517 * 1 | 2
518 * 2 | 3
519 * - | 4 <--- max recorded
520 *
521 * ============ PASS n + 1 ==============
522 * CPU 0 | CPU 1
523 * |
524 * cnt1 timestamps | cnt2 timestamps
525 * 3 | 5
526 * 4 | 6
527 * 5 | 7 <---- max recorded
528 *
529 * Flush every events below timestamp 4
530 *
531 * ============ PASS n + 2 ==============
532 * CPU 0 | CPU 1
533 * |
534 * cnt1 timestamps | cnt2 timestamps
535 * 6 | 8
536 * 7 | 9
537 * - | 10
538 *
539 * Flush every events below timestamp 7
540 * etc...
541 */
542static int process_finished_round(event_t *event __used,
543 struct perf_session *session,
544 struct perf_event_ops *ops)
545{
546 flush_sample_queue(session, ops);
547 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
548
549 return 0;
550}
551
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200552/* The queue is ordered by time */
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100553static void __queue_event(struct sample_queue *new, struct perf_session *s)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200554{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000555 struct ordered_samples *os = &s->ordered_samples;
556 struct sample_queue *sample = os->last_sample;
557 u64 timestamp = new->timestamp;
558 struct list_head *p;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200559
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000560 os->last_sample = new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200561
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000562 if (!sample) {
563 list_add(&new->list, &os->samples);
564 os->max_timestamp = timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200565 return;
566 }
567
568 /*
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000569 * last_sample might point to some random place in the list as it's
570 * the last queued event. We expect that the new event is close to
571 * this.
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200572 */
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000573 if (sample->timestamp <= timestamp) {
574 while (sample->timestamp <= timestamp) {
575 p = sample->list.next;
576 if (p == &os->samples) {
577 list_add_tail(&new->list, &os->samples);
578 os->max_timestamp = timestamp;
579 return;
580 }
581 sample = list_entry(p, struct sample_queue, list);
582 }
583 list_add_tail(&new->list, &sample->list);
584 } else {
585 while (sample->timestamp > timestamp) {
586 p = sample->list.prev;
587 if (p == &os->samples) {
588 list_add(&new->list, &os->samples);
589 return;
590 }
591 sample = list_entry(p, struct sample_queue, list);
592 }
593 list_add(&new->list, &sample->list);
594 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200595}
596
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000597#define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
598
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100599static int perf_session_queue_event(struct perf_session *s, event_t *event,
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000600 struct sample_data *data, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200601{
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000602 struct ordered_samples *os = &s->ordered_samples;
603 struct list_head *sc = &os->sample_cache;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200604 u64 timestamp = data->time;
605 struct sample_queue *new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200606
Thomas Gleixner79a14c12010-12-07 12:48:44 +0000607 if (!timestamp || timestamp == ~0ULL)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100608 return -ETIME;
609
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200610 if (timestamp < s->ordered_samples.last_flush) {
611 printf("Warning: Timestamp below last timeslice flush\n");
612 return -EINVAL;
613 }
614
Thomas Gleixner020bb752010-11-30 17:49:53 +0000615 if (!list_empty(sc)) {
616 new = list_entry(sc->next, struct sample_queue, list);
617 list_del(&new->list);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000618 } else if (os->sample_buffer) {
619 new = os->sample_buffer + os->sample_buffer_idx;
620 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
621 os->sample_buffer = NULL;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000622 } else {
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000623 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
624 if (!os->sample_buffer)
Thomas Gleixner020bb752010-11-30 17:49:53 +0000625 return -ENOMEM;
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000626 list_add(&os->sample_buffer->list, &os->to_free);
627 os->sample_buffer_idx = 2;
628 new = os->sample_buffer + 1;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000629 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200630
631 new->timestamp = timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000632 new->file_offset = file_offset;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000633 new->event = event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200634
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100635 __queue_event(new, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200636
637 return 0;
638}
639
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200640static void callchain__dump(struct sample_data *sample)
641{
642 unsigned int i;
643
644 if (!dump_trace)
645 return;
646
647 printf("... chain: nr:%Lu\n", sample->callchain->nr);
648
649 for (i = 0; i < sample->callchain->nr; i++)
650 printf("..... %2d: %016Lx\n", i, sample->callchain->ips[i]);
651}
652
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200653static void perf_session__print_tstamp(struct perf_session *session,
654 event_t *event,
655 struct sample_data *sample)
656{
657 if (event->header.type != PERF_RECORD_SAMPLE &&
658 !session->sample_id_all) {
659 fputs("-1 -1 ", stdout);
660 return;
661 }
662
663 if ((session->sample_type & PERF_SAMPLE_CPU))
664 printf("%u ", sample->cpu);
665
666 if (session->sample_type & PERF_SAMPLE_TIME)
667 printf("%Lu ", sample->time);
668}
669
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000670static void dump_event(struct perf_session *session, event_t *event,
671 u64 file_offset, struct sample_data *sample)
672{
673 if (!dump_trace)
674 return;
675
676 dump_printf("\n%#Lx [%#x]: event: %d\n", file_offset,
677 event->header.size, event->header.type);
678
679 trace_event(event);
680
681 if (sample)
682 perf_session__print_tstamp(session, event, sample);
683
684 dump_printf("%#Lx [%#x]: PERF_RECORD_%s",
685 file_offset, event->header.size,
686 event__get_event_name(event->header.type));
687}
688
689static void dump_sample(struct perf_session *session, event_t *event,
690 struct sample_data *sample)
691{
692 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
693 sample->pid, sample->tid, sample->ip, sample->period);
694
695 if (session->sample_type & PERF_SAMPLE_CALLCHAIN)
696 callchain__dump(sample);
697}
698
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100699static int perf_session_deliver_event(struct perf_session *session,
700 event_t *event,
701 struct sample_data *sample,
702 struct perf_event_ops *ops)
703{
704 switch (event->header.type) {
705 case PERF_RECORD_SAMPLE:
706 return ops->sample(event, sample, session);
707 case PERF_RECORD_MMAP:
708 return ops->mmap(event, sample, session);
709 case PERF_RECORD_COMM:
710 return ops->comm(event, sample, session);
711 case PERF_RECORD_FORK:
712 return ops->fork(event, sample, session);
713 case PERF_RECORD_EXIT:
714 return ops->exit(event, sample, session);
715 case PERF_RECORD_LOST:
716 return ops->lost(event, sample, session);
717 case PERF_RECORD_READ:
718 return ops->read(event, sample, session);
719 case PERF_RECORD_THROTTLE:
720 return ops->throttle(event, sample, session);
721 case PERF_RECORD_UNTHROTTLE:
722 return ops->unthrottle(event, sample, session);
723 default:
724 ++session->hists.stats.nr_unknown_events;
725 return -1;
726 }
727}
728
729static int perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200730 event_t *event,
731 struct perf_event_ops *ops,
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000732 u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200733{
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200734 struct sample_data sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100735 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200736
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100737 if (session->header.needs_swap && event__swap_ops[event->header.type])
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200738 event__swap_ops[event->header.type](event);
739
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000740 if (event->header.type >= PERF_RECORD_HEADER_MAX)
741 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200742
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000743 hists__inc_nr_events(&session->hists, event->header.type);
744
745 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
746 dump_event(session, event, file_offset, NULL);
747 else {
748 event__parse_sample(event, session, &sample);
749 dump_event(session, event, file_offset, &sample);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200750 }
751
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100752 /* These events are processed right away */
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200753 switch (event->header.type) {
754 case PERF_RECORD_SAMPLE:
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100755 if (session->sample_type & PERF_SAMPLE_CALLCHAIN) {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200756 if (!ip_callchain__valid(sample.callchain, event)) {
757 pr_debug("call-chain problem with event, "
758 "skipping it.\n");
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100759 ++session->hists.stats.nr_invalid_chains;
760 session->hists.stats.total_invalid_chains +=
761 sample.period;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200762 return 0;
763 }
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200764 }
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000765 dump_sample(session, event, &sample);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100766 break;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200767
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500768 case PERF_RECORD_HEADER_ATTR:
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100769 return ops->attr(event, session);
Tom Zanussicd19a032010-04-01 23:59:20 -0500770 case PERF_RECORD_HEADER_EVENT_TYPE:
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100771 return ops->event_type(event, session);
Tom Zanussi92155452010-04-01 23:59:21 -0500772 case PERF_RECORD_HEADER_TRACING_DATA:
773 /* setup for reading amidst mmap */
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100774 lseek(session->fd, file_offset, SEEK_SET);
775 return ops->tracing_data(event, session);
Tom Zanussic7929e42010-04-01 23:59:22 -0500776 case PERF_RECORD_HEADER_BUILD_ID:
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100777 return ops->build_id(event, session);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200778 case PERF_RECORD_FINISHED_ROUND:
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100779 return ops->finished_round(event, session, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200780 default:
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100781 break;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200782 }
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100783
784 if (ops->ordered_samples) {
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000785 ret = perf_session_queue_event(session, event, &sample,
786 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100787 if (ret != -ETIME)
788 return ret;
789 }
790
791 return perf_session_deliver_event(session, event, &sample, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200792}
793
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200794void perf_event_header__bswap(struct perf_event_header *self)
795{
796 self->type = bswap_32(self->type);
797 self->misc = bswap_16(self->misc);
798 self->size = bswap_16(self->size);
799}
800
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200801static struct thread *perf_session__register_idle_thread(struct perf_session *self)
802{
803 struct thread *thread = perf_session__findnew(self, 0);
804
805 if (thread == NULL || thread__set_comm(thread, "swapper")) {
806 pr_err("problem inserting idle task.\n");
807 thread = NULL;
808 }
809
810 return thread;
811}
812
Tom Zanussi8dc58102010-04-01 23:59:15 -0500813int do_read(int fd, void *buf, size_t size)
814{
815 void *buf_start = buf;
816
817 while (size) {
818 int ret = read(fd, buf, size);
819
820 if (ret <= 0)
821 return ret;
822
823 size -= ret;
824 buf += ret;
825 }
826
827 return buf - buf_start;
828}
829
830#define session_done() (*(volatile int *)(&session_done))
831volatile int session_done;
832
833static int __perf_session__process_pipe_events(struct perf_session *self,
834 struct perf_event_ops *ops)
835{
836 event_t event;
837 uint32_t size;
838 int skip = 0;
839 u64 head;
840 int err;
841 void *p;
842
843 perf_event_ops__fill_defaults(ops);
844
845 head = 0;
846more:
847 err = do_read(self->fd, &event, sizeof(struct perf_event_header));
848 if (err <= 0) {
849 if (err == 0)
850 goto done;
851
852 pr_err("failed to read event header\n");
853 goto out_err;
854 }
855
856 if (self->header.needs_swap)
857 perf_event_header__bswap(&event.header);
858
859 size = event.header.size;
860 if (size == 0)
861 size = 8;
862
863 p = &event;
864 p += sizeof(struct perf_event_header);
865
Tom Zanussi794e43b2010-05-05 00:27:40 -0500866 if (size - sizeof(struct perf_event_header)) {
867 err = do_read(self->fd, p,
868 size - sizeof(struct perf_event_header));
869 if (err <= 0) {
870 if (err == 0) {
871 pr_err("unexpected end of event stream\n");
872 goto done;
873 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500874
Tom Zanussi794e43b2010-05-05 00:27:40 -0500875 pr_err("failed to read event data\n");
876 goto out_err;
877 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500878 }
879
880 if (size == 0 ||
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000881 (skip = perf_session__process_event(self, &event, ops, head)) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -0500882 dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n",
883 head, event.header.size, event.header.type);
884 /*
885 * assume we lost track of the stream, check alignment, and
886 * increment a single u64 in the hope to catch on again 'soon'.
887 */
888 if (unlikely(head & 7))
889 head &= ~7ULL;
890
891 size = 8;
892 }
893
894 head += size;
895
Tom Zanussi8dc58102010-04-01 23:59:15 -0500896 if (skip > 0)
897 head += skip;
898
899 if (!session_done())
900 goto more;
901done:
902 err = 0;
903out_err:
Thomas Gleixner020bb752010-11-30 17:49:53 +0000904 perf_session_free_sample_buffers(self);
Tom Zanussi8dc58102010-04-01 23:59:15 -0500905 return err;
906}
907
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000908int __perf_session__process_events(struct perf_session *session,
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200909 u64 data_offset, u64 data_size,
910 u64 file_size, struct perf_event_ops *ops)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200911{
Thomas Gleixner55b446292010-11-30 17:49:46 +0000912 u64 head, page_offset, file_offset, file_pos, progress_next;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000913 int err, mmap_prot, mmap_flags, map_idx = 0;
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000914 struct ui_progress *progress;
Thomas Gleixner55b446292010-11-30 17:49:46 +0000915 size_t page_size, mmap_size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000916 char *buf, *mmaps[8];
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200917 event_t *event;
918 uint32_t size;
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000919
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200920 perf_event_ops__fill_defaults(ops);
921
Arnaldo Carvalho de Melo1b759622010-01-14 18:30:04 -0200922 page_size = sysconf(_SC_PAGESIZE);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200923
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000924 page_offset = page_size * (data_offset / page_size);
925 file_offset = page_offset;
926 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200927
Thomas Gleixnerd6513282010-11-30 17:49:44 +0000928 if (data_offset + data_size < file_size)
929 file_size = data_offset + data_size;
930
Thomas Gleixner55b446292010-11-30 17:49:46 +0000931 progress_next = file_size / 16;
932 progress = ui_progress__new("Processing events...", file_size);
933 if (progress == NULL)
934 return -1;
935
936 mmap_size = session->mmap_window;
937 if (mmap_size > file_size)
938 mmap_size = file_size;
939
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000940 memset(mmaps, 0, sizeof(mmaps));
941
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200942 mmap_prot = PROT_READ;
943 mmap_flags = MAP_SHARED;
944
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000945 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200946 mmap_prot |= PROT_WRITE;
947 mmap_flags = MAP_PRIVATE;
948 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200949remap:
Thomas Gleixner55b446292010-11-30 17:49:46 +0000950 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
951 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200952 if (buf == MAP_FAILED) {
953 pr_err("failed to mmap file\n");
954 err = -errno;
955 goto out_err;
956 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000957 mmaps[map_idx] = buf;
958 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +0000959 file_pos = file_offset + head;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200960
961more:
962 event = (event_t *)(buf + head);
963
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000964 if (session->header.needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200965 perf_event_header__bswap(&event->header);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200966 size = event->header.size;
967 if (size == 0)
968 size = 8;
969
Thomas Gleixner55b446292010-11-30 17:49:46 +0000970 if (head + event->header.size >= mmap_size) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000971 if (mmaps[map_idx]) {
972 munmap(mmaps[map_idx], mmap_size);
973 mmaps[map_idx] = NULL;
974 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200975
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000976 page_offset = page_size * (head / page_size);
977 file_offset += page_offset;
978 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200979 goto remap;
980 }
981
982 size = event->header.size;
983
Thomas Gleixnerd6513282010-11-30 17:49:44 +0000984 if (size == 0 ||
985 perf_session__process_event(session, event, ops, file_pos) < 0) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200986 dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n",
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000987 file_offset + head, event->header.size,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200988 event->header.type);
989 /*
990 * assume we lost track of the stream, check alignment, and
991 * increment a single u64 in the hope to catch on again 'soon'.
992 */
993 if (unlikely(head & 7))
994 head &= ~7ULL;
995
996 size = 8;
997 }
998
999 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001000 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001001
Thomas Gleixner55b446292010-11-30 17:49:46 +00001002 if (file_pos >= progress_next) {
1003 progress_next += file_size / 16;
1004 ui_progress__update(progress, file_pos);
1005 }
1006
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001007 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001008 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001009
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001010 err = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001011 /* do the final flush for ordered samples */
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001012 session->ordered_samples.next_flush = ULLONG_MAX;
1013 flush_sample_queue(session, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001014out_err:
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -03001015 ui_progress__delete(progress);
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -02001016
1017 if (ops->lost == event__process_lost &&
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001018 session->hists.stats.total_lost != 0) {
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -02001019 ui__warning("Processed %Lu events and LOST %Lu!\n\n"
1020 "Check IO/CPU overload!\n\n",
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001021 session->hists.stats.total_period,
1022 session->hists.stats.total_lost);
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -02001023 }
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001024
1025 if (session->hists.stats.nr_unknown_events != 0) {
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -02001026 ui__warning("Found %u unknown events!\n\n"
1027 "Is this an older tool processing a perf.data "
1028 "file generated by a more recent tool?\n\n"
1029 "If that is not the case, consider "
1030 "reporting to linux-kernel@vger.kernel.org.\n\n",
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001031 session->hists.stats.nr_unknown_events);
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -02001032 }
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001033
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001034 if (session->hists.stats.nr_invalid_chains != 0) {
1035 ui__warning("Found invalid callchains!\n\n"
1036 "%u out of %u events were discarded for this reason.\n\n"
1037 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1038 session->hists.stats.nr_invalid_chains,
1039 session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
1040 }
1041
Thomas Gleixner020bb752010-11-30 17:49:53 +00001042 perf_session_free_sample_buffers(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001043 return err;
1044}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001045
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001046int perf_session__process_events(struct perf_session *self,
1047 struct perf_event_ops *ops)
1048{
1049 int err;
1050
1051 if (perf_session__register_idle_thread(self) == NULL)
1052 return -ENOMEM;
1053
Tom Zanussi8dc58102010-04-01 23:59:15 -05001054 if (!self->fd_pipe)
1055 err = __perf_session__process_events(self,
1056 self->header.data_offset,
1057 self->header.data_size,
1058 self->size, ops);
1059 else
1060 err = __perf_session__process_pipe_events(self, ops);
Dave Martin88ca8952010-07-27 11:46:12 -03001061
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001062 return err;
1063}
1064
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001065bool perf_session__has_traces(struct perf_session *self, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001066{
1067 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001068 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1069 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001070 }
1071
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001072 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001073}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001074
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001075int perf_session__set_kallsyms_ref_reloc_sym(struct map **maps,
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001076 const char *symbol_name,
1077 u64 addr)
1078{
1079 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001080 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001081 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001082
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001083 ref = zalloc(sizeof(struct ref_reloc_sym));
1084 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001085 return -ENOMEM;
1086
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001087 ref->name = strdup(symbol_name);
1088 if (ref->name == NULL) {
1089 free(ref);
1090 return -ENOMEM;
1091 }
1092
1093 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001094 if (bracket)
1095 *bracket = '\0';
1096
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001097 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001098
1099 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001100 struct kmap *kmap = map__kmap(maps[i]);
1101 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001102 }
1103
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001104 return 0;
1105}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001106
1107size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
1108{
1109 return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
1110 __dsos__fprintf(&self->host_machine.user_dsos, fp) +
1111 machines__fprintf_dsos(&self->machines, fp);
1112}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001113
1114size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
1115 bool with_hits)
1116{
1117 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
1118 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
1119}