blob: 08ec018966a8454607e7efc7627ea32cdf8aea72 [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
Tom Zanussi8dc58102010-04-01 23:59:15 -050068void perf_session__update_sample_type(struct perf_session *self)
69{
70 self->sample_type = perf_header__sample_type(&self->header);
71}
72
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020073void perf_session__set_sample_type(struct perf_session *session, u64 type)
74{
75 session->sample_type = type;
76}
77
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080078int perf_session__create_kernel_maps(struct perf_session *self)
79{
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -030080 int ret = machine__create_kernel_maps(&self->host_machine);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080081
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080082 if (ret >= 0)
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -030083 ret = machines__create_guest_kernel_maps(&self->machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080084 return ret;
85}
86
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -030087static void perf_session__destroy_kernel_maps(struct perf_session *self)
88{
89 machine__destroy_kernel_maps(&self->host_machine);
90 machines__destroy_guest_kernel_maps(&self->machines);
91}
92
Tom Zanussi454c4072010-05-01 01:41:20 -050093struct perf_session *perf_session__new(const char *filename, int mode, bool force, bool repipe)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020094{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020095 size_t len = filename ? strlen(filename) + 1 : 0;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020096 struct perf_session *self = zalloc(sizeof(*self) + len);
97
98 if (self == NULL)
99 goto out;
100
101 if (perf_header__init(&self->header) < 0)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200102 goto out_free;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200103
104 memcpy(self->filename, filename, len);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200105 self->threads = RB_ROOT;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300106 INIT_LIST_HEAD(&self->dead_threads);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300107 self->hists_tree = RB_ROOT;
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200108 self->last_match = NULL;
Thomas Gleixner55b44622010-11-30 17:49:46 +0000109 /*
110 * On 64bit we can mmap the data file in one go. No need for tiny mmap
111 * slices. On 32bit we use 32MB.
112 */
113#if BITS_PER_LONG == 64
114 self->mmap_window = ULLONG_MAX;
115#else
116 self->mmap_window = 32 * 1024 * 1024ULL;
117#endif
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300118 self->machines = RB_ROOT;
Tom Zanussi454c4072010-05-01 01:41:20 -0500119 self->repipe = repipe;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000120 INIT_LIST_HEAD(&self->ordered_samples.samples);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000121 INIT_LIST_HEAD(&self->ordered_samples.sample_cache);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000122 INIT_LIST_HEAD(&self->ordered_samples.to_free);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300123 machine__init(&self->host_machine, "", HOST_KERNEL_ID);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200124
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200125 if (mode == O_RDONLY) {
126 if (perf_session__open(self, force) < 0)
127 goto out_delete;
128 } else if (mode == O_WRONLY) {
129 /*
130 * In O_RDONLY mode this will be performed when reading the
131 * kernel MMAP event, in event__process_mmap().
132 */
133 if (perf_session__create_kernel_maps(self) < 0)
134 goto out_delete;
135 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200136
Tom Zanussi8dc58102010-04-01 23:59:15 -0500137 perf_session__update_sample_type(self);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200138out:
139 return self;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200140out_free:
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200141 free(self);
142 return NULL;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200143out_delete:
144 perf_session__delete(self);
145 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200146}
147
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300148static void perf_session__delete_dead_threads(struct perf_session *self)
149{
150 struct thread *n, *t;
151
152 list_for_each_entry_safe(t, n, &self->dead_threads, node) {
153 list_del(&t->node);
154 thread__delete(t);
155 }
156}
157
158static void perf_session__delete_threads(struct perf_session *self)
159{
160 struct rb_node *nd = rb_first(&self->threads);
161
162 while (nd) {
163 struct thread *t = rb_entry(nd, struct thread, rb_node);
164
165 rb_erase(&t->rb_node, &self->threads);
166 nd = rb_next(nd);
167 thread__delete(t);
168 }
169}
170
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200171void perf_session__delete(struct perf_session *self)
172{
173 perf_header__exit(&self->header);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300174 perf_session__destroy_kernel_maps(self);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300175 perf_session__delete_dead_threads(self);
176 perf_session__delete_threads(self);
177 machine__exit(&self->host_machine);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200178 close(self->fd);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200179 free(self);
180}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200181
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300182void perf_session__remove_thread(struct perf_session *self, struct thread *th)
183{
Arnaldo Carvalho de Melo70597f22010-08-02 18:59:28 -0300184 self->last_match = NULL;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300185 rb_erase(&th->rb_node, &self->threads);
186 /*
187 * We may have references to this thread, for instance in some hist_entry
188 * instances, so just move them to a separate list.
189 */
190 list_add_tail(&th->node, &self->dead_threads);
191}
192
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200193static bool symbol__match_parent_regex(struct symbol *sym)
194{
195 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
196 return 1;
197
198 return 0;
199}
200
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300201struct map_symbol *perf_session__resolve_callchain(struct perf_session *self,
202 struct thread *thread,
203 struct ip_callchain *chain,
204 struct symbol **parent)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200205{
206 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200207 unsigned int i;
Arnaldo Carvalho de Meload5b2172010-04-02 10:04:18 -0300208 struct map_symbol *syms = calloc(chain->nr, sizeof(*syms));
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200209
Arnaldo Carvalho de Meload5b2172010-04-02 10:04:18 -0300210 if (!syms)
211 return NULL;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200212
213 for (i = 0; i < chain->nr; i++) {
214 u64 ip = chain->ips[i];
215 struct addr_location al;
216
217 if (ip >= PERF_CONTEXT_MAX) {
218 switch (ip) {
219 case PERF_CONTEXT_HV:
220 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
221 case PERF_CONTEXT_KERNEL:
222 cpumode = PERF_RECORD_MISC_KERNEL; break;
223 case PERF_CONTEXT_USER:
224 cpumode = PERF_RECORD_MISC_USER; break;
225 default:
226 break;
227 }
228 continue;
229 }
230
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800231 al.filtered = false;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200232 thread__find_addr_location(thread, self, cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800233 MAP__FUNCTION, thread->pid, ip, &al, NULL);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200234 if (al.sym != NULL) {
235 if (sort__has_parent && !*parent &&
236 symbol__match_parent_regex(al.sym))
237 *parent = al.sym;
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200238 if (!symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200239 break;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300240 syms[i].map = al.map;
241 syms[i].sym = al.sym;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200242 }
243 }
244
245 return syms;
246}
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200247
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200248static int process_event_synth_stub(event_t *event __used,
249 struct perf_session *session __used)
250{
251 dump_printf(": unhandled!\n");
252 return 0;
253}
254
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200255static int process_event_stub(event_t *event __used,
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200256 struct sample_data *sample __used,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200257 struct perf_session *session __used)
258{
259 dump_printf(": unhandled!\n");
260 return 0;
261}
262
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200263static int process_finished_round_stub(event_t *event __used,
264 struct perf_session *session __used,
265 struct perf_event_ops *ops __used)
266{
267 dump_printf(": unhandled!\n");
268 return 0;
269}
270
271static int process_finished_round(event_t *event,
272 struct perf_session *session,
273 struct perf_event_ops *ops);
274
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200275static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
276{
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200277 if (handler->sample == NULL)
278 handler->sample = process_event_stub;
279 if (handler->mmap == NULL)
280 handler->mmap = process_event_stub;
281 if (handler->comm == NULL)
282 handler->comm = process_event_stub;
283 if (handler->fork == NULL)
284 handler->fork = process_event_stub;
285 if (handler->exit == NULL)
286 handler->exit = process_event_stub;
287 if (handler->lost == NULL)
Arnaldo Carvalho de Melo37982ba2010-11-26 18:31:54 -0200288 handler->lost = event__process_lost;
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200289 if (handler->read == NULL)
290 handler->read = process_event_stub;
291 if (handler->throttle == NULL)
292 handler->throttle = process_event_stub;
293 if (handler->unthrottle == NULL)
294 handler->unthrottle = process_event_stub;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500295 if (handler->attr == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200296 handler->attr = process_event_synth_stub;
Tom Zanussicd19a032010-04-01 23:59:20 -0500297 if (handler->event_type == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200298 handler->event_type = process_event_synth_stub;
Tom Zanussi92155452010-04-01 23:59:21 -0500299 if (handler->tracing_data == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200300 handler->tracing_data = process_event_synth_stub;
Tom Zanussic7929e42010-04-01 23:59:22 -0500301 if (handler->build_id == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200302 handler->build_id = process_event_synth_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200303 if (handler->finished_round == NULL) {
304 if (handler->ordered_samples)
305 handler->finished_round = process_finished_round;
306 else
307 handler->finished_round = process_finished_round_stub;
308 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200309}
310
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200311void mem_bswap_64(void *src, int byte_size)
312{
313 u64 *m = src;
314
315 while (byte_size > 0) {
316 *m = bswap_64(*m);
317 byte_size -= sizeof(u64);
318 ++m;
319 }
320}
321
322static void event__all64_swap(event_t *self)
323{
324 struct perf_event_header *hdr = &self->header;
325 mem_bswap_64(hdr + 1, self->header.size - sizeof(*hdr));
326}
327
328static void event__comm_swap(event_t *self)
329{
330 self->comm.pid = bswap_32(self->comm.pid);
331 self->comm.tid = bswap_32(self->comm.tid);
332}
333
334static void event__mmap_swap(event_t *self)
335{
336 self->mmap.pid = bswap_32(self->mmap.pid);
337 self->mmap.tid = bswap_32(self->mmap.tid);
338 self->mmap.start = bswap_64(self->mmap.start);
339 self->mmap.len = bswap_64(self->mmap.len);
340 self->mmap.pgoff = bswap_64(self->mmap.pgoff);
341}
342
343static void event__task_swap(event_t *self)
344{
345 self->fork.pid = bswap_32(self->fork.pid);
346 self->fork.tid = bswap_32(self->fork.tid);
347 self->fork.ppid = bswap_32(self->fork.ppid);
348 self->fork.ptid = bswap_32(self->fork.ptid);
349 self->fork.time = bswap_64(self->fork.time);
350}
351
352static void event__read_swap(event_t *self)
353{
354 self->read.pid = bswap_32(self->read.pid);
355 self->read.tid = bswap_32(self->read.tid);
356 self->read.value = bswap_64(self->read.value);
357 self->read.time_enabled = bswap_64(self->read.time_enabled);
358 self->read.time_running = bswap_64(self->read.time_running);
359 self->read.id = bswap_64(self->read.id);
360}
361
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500362static void event__attr_swap(event_t *self)
363{
364 size_t size;
365
366 self->attr.attr.type = bswap_32(self->attr.attr.type);
367 self->attr.attr.size = bswap_32(self->attr.attr.size);
368 self->attr.attr.config = bswap_64(self->attr.attr.config);
369 self->attr.attr.sample_period = bswap_64(self->attr.attr.sample_period);
370 self->attr.attr.sample_type = bswap_64(self->attr.attr.sample_type);
371 self->attr.attr.read_format = bswap_64(self->attr.attr.read_format);
372 self->attr.attr.wakeup_events = bswap_32(self->attr.attr.wakeup_events);
373 self->attr.attr.bp_type = bswap_32(self->attr.attr.bp_type);
374 self->attr.attr.bp_addr = bswap_64(self->attr.attr.bp_addr);
375 self->attr.attr.bp_len = bswap_64(self->attr.attr.bp_len);
376
377 size = self->header.size;
378 size -= (void *)&self->attr.id - (void *)self;
379 mem_bswap_64(self->attr.id, size);
380}
381
Tom Zanussicd19a032010-04-01 23:59:20 -0500382static void event__event_type_swap(event_t *self)
383{
384 self->event_type.event_type.event_id =
385 bswap_64(self->event_type.event_type.event_id);
386}
387
Tom Zanussi92155452010-04-01 23:59:21 -0500388static void event__tracing_data_swap(event_t *self)
389{
390 self->tracing_data.size = bswap_32(self->tracing_data.size);
391}
392
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200393typedef void (*event__swap_op)(event_t *self);
394
395static event__swap_op event__swap_ops[] = {
396 [PERF_RECORD_MMAP] = event__mmap_swap,
397 [PERF_RECORD_COMM] = event__comm_swap,
398 [PERF_RECORD_FORK] = event__task_swap,
399 [PERF_RECORD_EXIT] = event__task_swap,
400 [PERF_RECORD_LOST] = event__all64_swap,
401 [PERF_RECORD_READ] = event__read_swap,
402 [PERF_RECORD_SAMPLE] = event__all64_swap,
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500403 [PERF_RECORD_HEADER_ATTR] = event__attr_swap,
Tom Zanussicd19a032010-04-01 23:59:20 -0500404 [PERF_RECORD_HEADER_EVENT_TYPE] = event__event_type_swap,
Tom Zanussi92155452010-04-01 23:59:21 -0500405 [PERF_RECORD_HEADER_TRACING_DATA] = event__tracing_data_swap,
Tom Zanussic7929e42010-04-01 23:59:22 -0500406 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
Tom Zanussi8dc58102010-04-01 23:59:15 -0500407 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200408};
409
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200410struct sample_queue {
411 u64 timestamp;
Thomas Gleixner28990f72010-11-30 17:49:35 +0000412 event_t *event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200413 struct list_head list;
414};
415
Thomas Gleixner020bb752010-11-30 17:49:53 +0000416static void perf_session_free_sample_buffers(struct perf_session *session)
417{
418 struct ordered_samples *os = &session->ordered_samples;
419
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000420 while (!list_empty(&os->to_free)) {
Thomas Gleixner020bb752010-11-30 17:49:53 +0000421 struct sample_queue *sq;
422
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000423 sq = list_entry(os->to_free.next, struct sample_queue, list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000424 list_del(&sq->list);
425 free(sq);
426 }
427}
428
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200429static void flush_sample_queue(struct perf_session *s,
430 struct perf_event_ops *ops)
431{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000432 struct ordered_samples *os = &s->ordered_samples;
433 struct list_head *head = &os->samples;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200434 struct sample_queue *tmp, *iter;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200435 struct sample_data sample;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000436 u64 limit = os->next_flush;
437 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200438
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200439 if (!ops->ordered_samples || !limit)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200440 return;
441
442 list_for_each_entry_safe(iter, tmp, head, list) {
443 if (iter->timestamp > limit)
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000444 break;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200445
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200446 event__parse_sample(iter->event, s->sample_type, &sample);
447 ops->sample(iter->event, &sample, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200448
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000449 os->last_flush = iter->timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200450 list_del(&iter->list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000451 list_add(&iter->list, &os->sample_cache);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200452 }
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000453
454 if (list_empty(head)) {
455 os->last_sample = NULL;
456 } else if (last_ts <= limit) {
457 os->last_sample =
458 list_entry(head->prev, struct sample_queue, list);
459 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200460}
461
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200462/*
463 * When perf record finishes a pass on every buffers, it records this pseudo
464 * event.
465 * We record the max timestamp t found in the pass n.
466 * Assuming these timestamps are monotonic across cpus, we know that if
467 * a buffer still has events with timestamps below t, they will be all
468 * available and then read in the pass n + 1.
469 * Hence when we start to read the pass n + 2, we can safely flush every
470 * events with timestamps below t.
471 *
472 * ============ PASS n =================
473 * CPU 0 | CPU 1
474 * |
475 * cnt1 timestamps | cnt2 timestamps
476 * 1 | 2
477 * 2 | 3
478 * - | 4 <--- max recorded
479 *
480 * ============ PASS n + 1 ==============
481 * CPU 0 | CPU 1
482 * |
483 * cnt1 timestamps | cnt2 timestamps
484 * 3 | 5
485 * 4 | 6
486 * 5 | 7 <---- max recorded
487 *
488 * Flush every events below timestamp 4
489 *
490 * ============ PASS n + 2 ==============
491 * CPU 0 | CPU 1
492 * |
493 * cnt1 timestamps | cnt2 timestamps
494 * 6 | 8
495 * 7 | 9
496 * - | 10
497 *
498 * Flush every events below timestamp 7
499 * etc...
500 */
501static int process_finished_round(event_t *event __used,
502 struct perf_session *session,
503 struct perf_event_ops *ops)
504{
505 flush_sample_queue(session, ops);
506 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
507
508 return 0;
509}
510
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200511/* The queue is ordered by time */
512static void __queue_sample_event(struct sample_queue *new,
513 struct perf_session *s)
514{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000515 struct ordered_samples *os = &s->ordered_samples;
516 struct sample_queue *sample = os->last_sample;
517 u64 timestamp = new->timestamp;
518 struct list_head *p;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200519
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000520 os->last_sample = new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200521
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000522 if (!sample) {
523 list_add(&new->list, &os->samples);
524 os->max_timestamp = timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200525 return;
526 }
527
528 /*
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000529 * last_sample might point to some random place in the list as it's
530 * the last queued event. We expect that the new event is close to
531 * this.
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200532 */
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000533 if (sample->timestamp <= timestamp) {
534 while (sample->timestamp <= timestamp) {
535 p = sample->list.next;
536 if (p == &os->samples) {
537 list_add_tail(&new->list, &os->samples);
538 os->max_timestamp = timestamp;
539 return;
540 }
541 sample = list_entry(p, struct sample_queue, list);
542 }
543 list_add_tail(&new->list, &sample->list);
544 } else {
545 while (sample->timestamp > timestamp) {
546 p = sample->list.prev;
547 if (p == &os->samples) {
548 list_add(&new->list, &os->samples);
549 return;
550 }
551 sample = list_entry(p, struct sample_queue, list);
552 }
553 list_add(&new->list, &sample->list);
554 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200555}
556
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000557#define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
558
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200559static int queue_sample_event(event_t *event, struct sample_data *data,
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200560 struct perf_session *s)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200561{
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000562 struct ordered_samples *os = &s->ordered_samples;
563 struct list_head *sc = &os->sample_cache;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200564 u64 timestamp = data->time;
565 struct sample_queue *new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200566
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200567 if (timestamp < s->ordered_samples.last_flush) {
568 printf("Warning: Timestamp below last timeslice flush\n");
569 return -EINVAL;
570 }
571
Thomas Gleixner020bb752010-11-30 17:49:53 +0000572 if (!list_empty(sc)) {
573 new = list_entry(sc->next, struct sample_queue, list);
574 list_del(&new->list);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000575 } else if (os->sample_buffer) {
576 new = os->sample_buffer + os->sample_buffer_idx;
577 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
578 os->sample_buffer = NULL;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000579 } else {
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000580 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
581 if (!os->sample_buffer)
Thomas Gleixner020bb752010-11-30 17:49:53 +0000582 return -ENOMEM;
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000583 list_add(&os->sample_buffer->list, &os->to_free);
584 os->sample_buffer_idx = 2;
585 new = os->sample_buffer + 1;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000586 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200587
588 new->timestamp = timestamp;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000589 new->event = event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200590
591 __queue_sample_event(new, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200592
593 return 0;
594}
595
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200596static int perf_session__process_sample(event_t *event,
597 struct sample_data *sample,
598 struct perf_session *s,
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200599 struct perf_event_ops *ops)
600{
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200601 if (!ops->ordered_samples)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200602 return ops->sample(event, sample, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200603
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200604 queue_sample_event(event, sample, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200605 return 0;
606}
607
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200608static void callchain__dump(struct sample_data *sample)
609{
610 unsigned int i;
611
612 if (!dump_trace)
613 return;
614
615 printf("... chain: nr:%Lu\n", sample->callchain->nr);
616
617 for (i = 0; i < sample->callchain->nr; i++)
618 printf("..... %2d: %016Lx\n", i, sample->callchain->ips[i]);
619}
620
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200621static int perf_session__process_event(struct perf_session *self,
622 event_t *event,
623 struct perf_event_ops *ops,
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000624 u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200625{
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200626 struct sample_data sample;
627
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200628 trace_event(event);
629
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200630 if (self->header.needs_swap && event__swap_ops[event->header.type])
631 event__swap_ops[event->header.type](event);
632
633 if (event->header.type == PERF_RECORD_SAMPLE)
634 event__parse_sample(event, self->sample_type, &sample);
635
Tom Zanussi8dc58102010-04-01 23:59:15 -0500636 if (event->header.type < PERF_RECORD_HEADER_MAX) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200637 dump_printf("%#Lx [%#x]: PERF_RECORD_%s",
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000638 file_offset, event->header.size,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200639 event__name[event->header.type]);
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300640 hists__inc_nr_events(&self->hists, event->header.type);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200641 }
642
643 switch (event->header.type) {
644 case PERF_RECORD_SAMPLE:
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200645 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
646 sample.pid, sample.tid, sample.ip, sample.period);
647
648 if (self->sample_type & PERF_SAMPLE_CALLCHAIN) {
649 if (!ip_callchain__valid(sample.callchain, event)) {
650 pr_debug("call-chain problem with event, "
651 "skipping it.\n");
652 ++self->hists.stats.nr_invalid_chains;
653 self->hists.stats.total_invalid_chains += sample.period;
654 return 0;
655 }
656
657 callchain__dump(&sample);
658 }
659
660 return perf_session__process_sample(event, &sample, self, ops);
661
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200662 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200663 return ops->mmap(event, &sample, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200664 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200665 return ops->comm(event, &sample, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200666 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200667 return ops->fork(event, &sample, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200668 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200669 return ops->exit(event, &sample, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200670 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200671 return ops->lost(event, &sample, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200672 case PERF_RECORD_READ:
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200673 return ops->read(event, &sample, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200674 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200675 return ops->throttle(event, &sample, self);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200676 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200677 return ops->unthrottle(event, &sample, self);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500678 case PERF_RECORD_HEADER_ATTR:
679 return ops->attr(event, self);
Tom Zanussicd19a032010-04-01 23:59:20 -0500680 case PERF_RECORD_HEADER_EVENT_TYPE:
681 return ops->event_type(event, self);
Tom Zanussi92155452010-04-01 23:59:21 -0500682 case PERF_RECORD_HEADER_TRACING_DATA:
683 /* setup for reading amidst mmap */
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000684 lseek(self->fd, file_offset, SEEK_SET);
Tom Zanussi92155452010-04-01 23:59:21 -0500685 return ops->tracing_data(event, self);
Tom Zanussic7929e42010-04-01 23:59:22 -0500686 case PERF_RECORD_HEADER_BUILD_ID:
687 return ops->build_id(event, self);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200688 case PERF_RECORD_FINISHED_ROUND:
689 return ops->finished_round(event, self, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200690 default:
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300691 ++self->hists.stats.nr_unknown_events;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200692 return -1;
693 }
694}
695
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200696void perf_event_header__bswap(struct perf_event_header *self)
697{
698 self->type = bswap_32(self->type);
699 self->misc = bswap_16(self->misc);
700 self->size = bswap_16(self->size);
701}
702
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200703static struct thread *perf_session__register_idle_thread(struct perf_session *self)
704{
705 struct thread *thread = perf_session__findnew(self, 0);
706
707 if (thread == NULL || thread__set_comm(thread, "swapper")) {
708 pr_err("problem inserting idle task.\n");
709 thread = NULL;
710 }
711
712 return thread;
713}
714
Tom Zanussi8dc58102010-04-01 23:59:15 -0500715int do_read(int fd, void *buf, size_t size)
716{
717 void *buf_start = buf;
718
719 while (size) {
720 int ret = read(fd, buf, size);
721
722 if (ret <= 0)
723 return ret;
724
725 size -= ret;
726 buf += ret;
727 }
728
729 return buf - buf_start;
730}
731
732#define session_done() (*(volatile int *)(&session_done))
733volatile int session_done;
734
735static int __perf_session__process_pipe_events(struct perf_session *self,
736 struct perf_event_ops *ops)
737{
738 event_t event;
739 uint32_t size;
740 int skip = 0;
741 u64 head;
742 int err;
743 void *p;
744
745 perf_event_ops__fill_defaults(ops);
746
747 head = 0;
748more:
749 err = do_read(self->fd, &event, sizeof(struct perf_event_header));
750 if (err <= 0) {
751 if (err == 0)
752 goto done;
753
754 pr_err("failed to read event header\n");
755 goto out_err;
756 }
757
758 if (self->header.needs_swap)
759 perf_event_header__bswap(&event.header);
760
761 size = event.header.size;
762 if (size == 0)
763 size = 8;
764
765 p = &event;
766 p += sizeof(struct perf_event_header);
767
Tom Zanussi794e43b2010-05-05 00:27:40 -0500768 if (size - sizeof(struct perf_event_header)) {
769 err = do_read(self->fd, p,
770 size - sizeof(struct perf_event_header));
771 if (err <= 0) {
772 if (err == 0) {
773 pr_err("unexpected end of event stream\n");
774 goto done;
775 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500776
Tom Zanussi794e43b2010-05-05 00:27:40 -0500777 pr_err("failed to read event data\n");
778 goto out_err;
779 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500780 }
781
782 if (size == 0 ||
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000783 (skip = perf_session__process_event(self, &event, ops, head)) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -0500784 dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n",
785 head, event.header.size, event.header.type);
786 /*
787 * assume we lost track of the stream, check alignment, and
788 * increment a single u64 in the hope to catch on again 'soon'.
789 */
790 if (unlikely(head & 7))
791 head &= ~7ULL;
792
793 size = 8;
794 }
795
796 head += size;
797
798 dump_printf("\n%#Lx [%#x]: event: %d\n",
799 head, event.header.size, event.header.type);
800
801 if (skip > 0)
802 head += skip;
803
804 if (!session_done())
805 goto more;
806done:
807 err = 0;
808out_err:
Thomas Gleixner020bb752010-11-30 17:49:53 +0000809 perf_session_free_sample_buffers(self);
Tom Zanussi8dc58102010-04-01 23:59:15 -0500810 return err;
811}
812
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000813int __perf_session__process_events(struct perf_session *session,
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200814 u64 data_offset, u64 data_size,
815 u64 file_size, struct perf_event_ops *ops)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200816{
Thomas Gleixner55b44622010-11-30 17:49:46 +0000817 u64 head, page_offset, file_offset, file_pos, progress_next;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000818 int err, mmap_prot, mmap_flags, map_idx = 0;
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000819 struct ui_progress *progress;
Thomas Gleixner55b44622010-11-30 17:49:46 +0000820 size_t page_size, mmap_size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000821 char *buf, *mmaps[8];
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200822 event_t *event;
823 uint32_t size;
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000824
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200825 perf_event_ops__fill_defaults(ops);
826
Arnaldo Carvalho de Melo1b759622010-01-14 18:30:04 -0200827 page_size = sysconf(_SC_PAGESIZE);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200828
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000829 page_offset = page_size * (data_offset / page_size);
830 file_offset = page_offset;
831 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200832
Thomas Gleixnerd6513282010-11-30 17:49:44 +0000833 if (data_offset + data_size < file_size)
834 file_size = data_offset + data_size;
835
Thomas Gleixner55b44622010-11-30 17:49:46 +0000836 progress_next = file_size / 16;
837 progress = ui_progress__new("Processing events...", file_size);
838 if (progress == NULL)
839 return -1;
840
841 mmap_size = session->mmap_window;
842 if (mmap_size > file_size)
843 mmap_size = file_size;
844
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000845 memset(mmaps, 0, sizeof(mmaps));
846
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200847 mmap_prot = PROT_READ;
848 mmap_flags = MAP_SHARED;
849
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000850 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200851 mmap_prot |= PROT_WRITE;
852 mmap_flags = MAP_PRIVATE;
853 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200854remap:
Thomas Gleixner55b44622010-11-30 17:49:46 +0000855 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
856 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200857 if (buf == MAP_FAILED) {
858 pr_err("failed to mmap file\n");
859 err = -errno;
860 goto out_err;
861 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000862 mmaps[map_idx] = buf;
863 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +0000864 file_pos = file_offset + head;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200865
866more:
867 event = (event_t *)(buf + head);
868
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000869 if (session->header.needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200870 perf_event_header__bswap(&event->header);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200871 size = event->header.size;
872 if (size == 0)
873 size = 8;
874
Thomas Gleixner55b44622010-11-30 17:49:46 +0000875 if (head + event->header.size >= mmap_size) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000876 if (mmaps[map_idx]) {
877 munmap(mmaps[map_idx], mmap_size);
878 mmaps[map_idx] = NULL;
879 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200880
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000881 page_offset = page_size * (head / page_size);
882 file_offset += page_offset;
883 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200884 goto remap;
885 }
886
887 size = event->header.size;
888
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200889 dump_printf("\n%#Lx [%#x]: event: %d\n",
Thomas Gleixnerd6513282010-11-30 17:49:44 +0000890 file_pos, event->header.size, event->header.type);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200891
Thomas Gleixnerd6513282010-11-30 17:49:44 +0000892 if (size == 0 ||
893 perf_session__process_event(session, event, ops, file_pos) < 0) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200894 dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n",
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000895 file_offset + head, event->header.size,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200896 event->header.type);
897 /*
898 * assume we lost track of the stream, check alignment, and
899 * increment a single u64 in the hope to catch on again 'soon'.
900 */
901 if (unlikely(head & 7))
902 head &= ~7ULL;
903
904 size = 8;
905 }
906
907 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +0000908 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200909
Thomas Gleixner55b44622010-11-30 17:49:46 +0000910 if (file_pos >= progress_next) {
911 progress_next += file_size / 16;
912 ui_progress__update(progress, file_pos);
913 }
914
Thomas Gleixnerd6513282010-11-30 17:49:44 +0000915 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200916 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +0000917
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200918 err = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200919 /* do the final flush for ordered samples */
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000920 session->ordered_samples.next_flush = ULLONG_MAX;
921 flush_sample_queue(session, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200922out_err:
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300923 ui_progress__delete(progress);
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -0200924
925 if (ops->lost == event__process_lost &&
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000926 session->hists.stats.total_lost != 0) {
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -0200927 ui__warning("Processed %Lu events and LOST %Lu!\n\n"
928 "Check IO/CPU overload!\n\n",
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000929 session->hists.stats.total_period,
930 session->hists.stats.total_lost);
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -0200931 }
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000932
933 if (session->hists.stats.nr_unknown_events != 0) {
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -0200934 ui__warning("Found %u unknown events!\n\n"
935 "Is this an older tool processing a perf.data "
936 "file generated by a more recent tool?\n\n"
937 "If that is not the case, consider "
938 "reporting to linux-kernel@vger.kernel.org.\n\n",
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000939 session->hists.stats.nr_unknown_events);
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -0200940 }
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000941
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200942 if (session->hists.stats.nr_invalid_chains != 0) {
943 ui__warning("Found invalid callchains!\n\n"
944 "%u out of %u events were discarded for this reason.\n\n"
945 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
946 session->hists.stats.nr_invalid_chains,
947 session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
948 }
949
Thomas Gleixner020bb752010-11-30 17:49:53 +0000950 perf_session_free_sample_buffers(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200951 return err;
952}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -0200953
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200954int perf_session__process_events(struct perf_session *self,
955 struct perf_event_ops *ops)
956{
957 int err;
958
959 if (perf_session__register_idle_thread(self) == NULL)
960 return -ENOMEM;
961
Tom Zanussi8dc58102010-04-01 23:59:15 -0500962 if (!self->fd_pipe)
963 err = __perf_session__process_events(self,
964 self->header.data_offset,
965 self->header.data_size,
966 self->size, ops);
967 else
968 err = __perf_session__process_pipe_events(self, ops);
Dave Martin88ca8952010-07-27 11:46:12 -0300969
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200970 return err;
971}
972
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200973bool perf_session__has_traces(struct perf_session *self, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -0200974{
975 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200976 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
977 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -0200978 }
979
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200980 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -0200981}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200982
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800983int perf_session__set_kallsyms_ref_reloc_sym(struct map **maps,
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200984 const char *symbol_name,
985 u64 addr)
986{
987 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200988 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800989 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200990
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800991 ref = zalloc(sizeof(struct ref_reloc_sym));
992 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200993 return -ENOMEM;
994
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800995 ref->name = strdup(symbol_name);
996 if (ref->name == NULL) {
997 free(ref);
998 return -ENOMEM;
999 }
1000
1001 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001002 if (bracket)
1003 *bracket = '\0';
1004
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001005 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001006
1007 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001008 struct kmap *kmap = map__kmap(maps[i]);
1009 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001010 }
1011
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001012 return 0;
1013}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001014
1015size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
1016{
1017 return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
1018 __dsos__fprintf(&self->host_machine.user_dsos, fp) +
1019 machines__fprintf_dsos(&self->machines, fp);
1020}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001021
1022size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
1023 bool with_hits)
1024{
1025 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
1026 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
1027}