blob: 0f4555ce90635a767f4a609b917905b5f58bdd0a [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
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -030010#include "evlist.h"
11#include "evsel.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020012#include "session.h"
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -020013#include "sort.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020014#include "util.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100015#include "cpumap.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020016
17static int perf_session__open(struct perf_session *self, bool force)
18{
19 struct stat input_stat;
20
Tom Zanussi8dc58102010-04-01 23:59:15 -050021 if (!strcmp(self->filename, "-")) {
22 self->fd_pipe = true;
23 self->fd = STDIN_FILENO;
24
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030025 if (perf_session__read_header(self, self->fd) < 0)
Tom Zanussi8dc58102010-04-01 23:59:15 -050026 pr_err("incompatible file format");
27
28 return 0;
29 }
30
Xiao Guangrongf887f302010-02-04 16:46:42 +080031 self->fd = open(self->filename, O_RDONLY);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020032 if (self->fd < 0) {
Andy Isaacson0f2c3de2010-06-11 20:36:15 -070033 int err = errno;
34
35 pr_err("failed to open %s: %s", self->filename, strerror(err));
36 if (err == ENOENT && !strcmp(self->filename, "perf.data"))
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020037 pr_err(" (try 'perf record' first)");
38 pr_err("\n");
39 return -errno;
40 }
41
42 if (fstat(self->fd, &input_stat) < 0)
43 goto out_close;
44
45 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
46 pr_err("file %s not owned by current user or root\n",
47 self->filename);
48 goto out_close;
49 }
50
51 if (!input_stat.st_size) {
52 pr_info("zero-sized file (%s), nothing to do!\n",
53 self->filename);
54 goto out_close;
55 }
56
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030057 if (perf_session__read_header(self, self->fd) < 0) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020058 pr_err("incompatible file format");
59 goto out_close;
60 }
61
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030062 if (!perf_evlist__valid_sample_type(self->evlist)) {
63 pr_err("non matching sample_type");
64 goto out_close;
65 }
66
67 if (!perf_evlist__valid_sample_id_all(self->evlist)) {
68 pr_err("non matching sample_id_all");
69 goto out_close;
70 }
71
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020072 self->size = input_stat.st_size;
73 return 0;
74
75out_close:
76 close(self->fd);
77 self->fd = -1;
78 return -1;
79}
80
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020081static void perf_session__id_header_size(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -050082{
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -020083 struct perf_sample *data;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020084 u64 sample_type = session->sample_type;
85 u16 size = 0;
86
87 if (!session->sample_id_all)
88 goto out;
89
90 if (sample_type & PERF_SAMPLE_TID)
91 size += sizeof(data->tid) * 2;
92
93 if (sample_type & PERF_SAMPLE_TIME)
94 size += sizeof(data->time);
95
96 if (sample_type & PERF_SAMPLE_ID)
97 size += sizeof(data->id);
98
99 if (sample_type & PERF_SAMPLE_STREAM_ID)
100 size += sizeof(data->stream_id);
101
102 if (sample_type & PERF_SAMPLE_CPU)
103 size += sizeof(data->cpu) * 2;
104out:
105 session->id_hdr_size = size;
106}
107
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200108void perf_session__update_sample_type(struct perf_session *self)
109{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300110 self->sample_type = perf_evlist__sample_type(self->evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300111 self->sample_size = __perf_evsel__sample_size(self->sample_type);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300112 self->sample_id_all = perf_evlist__sample_id_all(self->evlist);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200113 perf_session__id_header_size(self);
114}
115
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800116int perf_session__create_kernel_maps(struct perf_session *self)
117{
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -0300118 int ret = machine__create_kernel_maps(&self->host_machine);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800119
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800120 if (ret >= 0)
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -0300121 ret = machines__create_guest_kernel_maps(&self->machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800122 return ret;
123}
124
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300125static void perf_session__destroy_kernel_maps(struct perf_session *self)
126{
127 machine__destroy_kernel_maps(&self->host_machine);
128 machines__destroy_guest_kernel_maps(&self->machines);
129}
130
Ian Munsie21ef97f2010-12-10 14:09:16 +1100131struct perf_session *perf_session__new(const char *filename, int mode,
132 bool force, bool repipe,
133 struct perf_event_ops *ops)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200134{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200135 size_t len = filename ? strlen(filename) + 1 : 0;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200136 struct perf_session *self = zalloc(sizeof(*self) + len);
137
138 if (self == NULL)
139 goto out;
140
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200141 memcpy(self->filename, filename, len);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200142 self->threads = RB_ROOT;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300143 INIT_LIST_HEAD(&self->dead_threads);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200144 self->last_match = NULL;
Thomas Gleixner55b446292010-11-30 17:49:46 +0000145 /*
146 * On 64bit we can mmap the data file in one go. No need for tiny mmap
147 * slices. On 32bit we use 32MB.
148 */
149#if BITS_PER_LONG == 64
150 self->mmap_window = ULLONG_MAX;
151#else
152 self->mmap_window = 32 * 1024 * 1024ULL;
153#endif
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300154 self->machines = RB_ROOT;
Tom Zanussi454c4072010-05-01 01:41:20 -0500155 self->repipe = repipe;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000156 INIT_LIST_HEAD(&self->ordered_samples.samples);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000157 INIT_LIST_HEAD(&self->ordered_samples.sample_cache);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000158 INIT_LIST_HEAD(&self->ordered_samples.to_free);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300159 machine__init(&self->host_machine, "", HOST_KERNEL_ID);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200160
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200161 if (mode == O_RDONLY) {
162 if (perf_session__open(self, force) < 0)
163 goto out_delete;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300164 perf_session__update_sample_type(self);
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200165 } else if (mode == O_WRONLY) {
166 /*
167 * In O_RDONLY mode this will be performed when reading the
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200168 * kernel MMAP event, in perf_event__process_mmap().
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200169 */
170 if (perf_session__create_kernel_maps(self) < 0)
171 goto out_delete;
172 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200173
Ian Munsie21ef97f2010-12-10 14:09:16 +1100174 if (ops && ops->ordering_requires_timestamps &&
175 ops->ordered_samples && !self->sample_id_all) {
176 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
177 ops->ordered_samples = false;
178 }
179
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200180out:
181 return self;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200182out_delete:
183 perf_session__delete(self);
184 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200185}
186
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300187static void perf_session__delete_dead_threads(struct perf_session *self)
188{
189 struct thread *n, *t;
190
191 list_for_each_entry_safe(t, n, &self->dead_threads, node) {
192 list_del(&t->node);
193 thread__delete(t);
194 }
195}
196
197static void perf_session__delete_threads(struct perf_session *self)
198{
199 struct rb_node *nd = rb_first(&self->threads);
200
201 while (nd) {
202 struct thread *t = rb_entry(nd, struct thread, rb_node);
203
204 rb_erase(&t->rb_node, &self->threads);
205 nd = rb_next(nd);
206 thread__delete(t);
207 }
208}
209
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200210void perf_session__delete(struct perf_session *self)
211{
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300212 perf_session__destroy_kernel_maps(self);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300213 perf_session__delete_dead_threads(self);
214 perf_session__delete_threads(self);
215 machine__exit(&self->host_machine);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200216 close(self->fd);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200217 free(self);
218}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200219
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300220void perf_session__remove_thread(struct perf_session *self, struct thread *th)
221{
Arnaldo Carvalho de Melo70597f22010-08-02 18:59:28 -0300222 self->last_match = NULL;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300223 rb_erase(&th->rb_node, &self->threads);
224 /*
225 * We may have references to this thread, for instance in some hist_entry
226 * instances, so just move them to a separate list.
227 */
228 list_add_tail(&th->node, &self->dead_threads);
229}
230
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200231static bool symbol__match_parent_regex(struct symbol *sym)
232{
233 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
234 return 1;
235
236 return 0;
237}
238
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100239int perf_session__resolve_callchain(struct perf_session *self,
240 struct thread *thread,
241 struct ip_callchain *chain,
242 struct symbol **parent)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200243{
244 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200245 unsigned int i;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100246 int err;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200247
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100248 callchain_cursor_reset(&self->callchain_cursor);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200249
250 for (i = 0; i < chain->nr; i++) {
Sam Liaod797fdc2011-06-07 23:49:46 +0800251 u64 ip;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200252 struct addr_location al;
253
Sam Liaod797fdc2011-06-07 23:49:46 +0800254 if (callchain_param.order == ORDER_CALLEE)
255 ip = chain->ips[i];
256 else
257 ip = chain->ips[chain->nr - i - 1];
258
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200259 if (ip >= PERF_CONTEXT_MAX) {
260 switch (ip) {
261 case PERF_CONTEXT_HV:
262 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
263 case PERF_CONTEXT_KERNEL:
264 cpumode = PERF_RECORD_MISC_KERNEL; break;
265 case PERF_CONTEXT_USER:
266 cpumode = PERF_RECORD_MISC_USER; break;
267 default:
268 break;
269 }
270 continue;
271 }
272
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800273 al.filtered = false;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200274 thread__find_addr_location(thread, self, cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800275 MAP__FUNCTION, thread->pid, ip, &al, NULL);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200276 if (al.sym != NULL) {
277 if (sort__has_parent && !*parent &&
278 symbol__match_parent_regex(al.sym))
279 *parent = al.sym;
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200280 if (!symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200281 break;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200282 }
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100283
284 err = callchain_cursor_append(&self->callchain_cursor,
285 ip, al.map, al.sym);
286 if (err)
287 return err;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200288 }
289
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100290 return 0;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200291}
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200292
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200293static int process_event_synth_stub(union perf_event *event __used,
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200294 struct perf_session *session __used)
295{
296 dump_printf(": unhandled!\n");
297 return 0;
298}
299
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300300static int process_event_sample_stub(union perf_event *event __used,
301 struct perf_sample *sample __used,
302 struct perf_evsel *evsel __used,
303 struct perf_session *session __used)
304{
305 dump_printf(": unhandled!\n");
306 return 0;
307}
308
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200309static int process_event_stub(union perf_event *event __used,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200310 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200311 struct perf_session *session __used)
312{
313 dump_printf(": unhandled!\n");
314 return 0;
315}
316
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200317static int process_finished_round_stub(union perf_event *event __used,
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200318 struct perf_session *session __used,
319 struct perf_event_ops *ops __used)
320{
321 dump_printf(": unhandled!\n");
322 return 0;
323}
324
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200325static int process_finished_round(union perf_event *event,
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200326 struct perf_session *session,
327 struct perf_event_ops *ops);
328
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200329static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
330{
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200331 if (handler->sample == NULL)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300332 handler->sample = process_event_sample_stub;
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200333 if (handler->mmap == NULL)
334 handler->mmap = process_event_stub;
335 if (handler->comm == NULL)
336 handler->comm = process_event_stub;
337 if (handler->fork == NULL)
338 handler->fork = process_event_stub;
339 if (handler->exit == NULL)
340 handler->exit = process_event_stub;
341 if (handler->lost == NULL)
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200342 handler->lost = perf_event__process_lost;
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200343 if (handler->read == NULL)
344 handler->read = process_event_stub;
345 if (handler->throttle == NULL)
346 handler->throttle = process_event_stub;
347 if (handler->unthrottle == NULL)
348 handler->unthrottle = process_event_stub;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500349 if (handler->attr == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200350 handler->attr = process_event_synth_stub;
Tom Zanussicd19a032010-04-01 23:59:20 -0500351 if (handler->event_type == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200352 handler->event_type = process_event_synth_stub;
Tom Zanussi92155452010-04-01 23:59:21 -0500353 if (handler->tracing_data == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200354 handler->tracing_data = process_event_synth_stub;
Tom Zanussic7929e42010-04-01 23:59:22 -0500355 if (handler->build_id == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200356 handler->build_id = process_event_synth_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200357 if (handler->finished_round == NULL) {
358 if (handler->ordered_samples)
359 handler->finished_round = process_finished_round;
360 else
361 handler->finished_round = process_finished_round_stub;
362 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200363}
364
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200365void mem_bswap_64(void *src, int byte_size)
366{
367 u64 *m = src;
368
369 while (byte_size > 0) {
370 *m = bswap_64(*m);
371 byte_size -= sizeof(u64);
372 ++m;
373 }
374}
375
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200376static void perf_event__all64_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200377{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200378 struct perf_event_header *hdr = &event->header;
379 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200380}
381
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200382static void perf_event__comm_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200383{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200384 event->comm.pid = bswap_32(event->comm.pid);
385 event->comm.tid = bswap_32(event->comm.tid);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200386}
387
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200388static void perf_event__mmap_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200389{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200390 event->mmap.pid = bswap_32(event->mmap.pid);
391 event->mmap.tid = bswap_32(event->mmap.tid);
392 event->mmap.start = bswap_64(event->mmap.start);
393 event->mmap.len = bswap_64(event->mmap.len);
394 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200395}
396
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200397static void perf_event__task_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200398{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200399 event->fork.pid = bswap_32(event->fork.pid);
400 event->fork.tid = bswap_32(event->fork.tid);
401 event->fork.ppid = bswap_32(event->fork.ppid);
402 event->fork.ptid = bswap_32(event->fork.ptid);
403 event->fork.time = bswap_64(event->fork.time);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200404}
405
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200406static void perf_event__read_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200407{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200408 event->read.pid = bswap_32(event->read.pid);
409 event->read.tid = bswap_32(event->read.tid);
410 event->read.value = bswap_64(event->read.value);
411 event->read.time_enabled = bswap_64(event->read.time_enabled);
412 event->read.time_running = bswap_64(event->read.time_running);
413 event->read.id = bswap_64(event->read.id);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200414}
415
David Aherneda39132011-07-15 12:34:09 -0600416/* exported for swapping attributes in file header */
417void perf_event__attr_swap(struct perf_event_attr *attr)
418{
419 attr->type = bswap_32(attr->type);
420 attr->size = bswap_32(attr->size);
421 attr->config = bswap_64(attr->config);
422 attr->sample_period = bswap_64(attr->sample_period);
423 attr->sample_type = bswap_64(attr->sample_type);
424 attr->read_format = bswap_64(attr->read_format);
425 attr->wakeup_events = bswap_32(attr->wakeup_events);
426 attr->bp_type = bswap_32(attr->bp_type);
427 attr->bp_addr = bswap_64(attr->bp_addr);
428 attr->bp_len = bswap_64(attr->bp_len);
429}
430
431static void perf_event__hdr_attr_swap(union perf_event *event)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500432{
433 size_t size;
434
David Aherneda39132011-07-15 12:34:09 -0600435 perf_event__attr_swap(&event->attr.attr);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500436
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200437 size = event->header.size;
438 size -= (void *)&event->attr.id - (void *)event;
439 mem_bswap_64(event->attr.id, size);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500440}
441
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200442static void perf_event__event_type_swap(union perf_event *event)
Tom Zanussicd19a032010-04-01 23:59:20 -0500443{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200444 event->event_type.event_type.event_id =
445 bswap_64(event->event_type.event_type.event_id);
Tom Zanussicd19a032010-04-01 23:59:20 -0500446}
447
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200448static void perf_event__tracing_data_swap(union perf_event *event)
Tom Zanussi92155452010-04-01 23:59:21 -0500449{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200450 event->tracing_data.size = bswap_32(event->tracing_data.size);
Tom Zanussi92155452010-04-01 23:59:21 -0500451}
452
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200453typedef void (*perf_event__swap_op)(union perf_event *event);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200454
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200455static perf_event__swap_op perf_event__swap_ops[] = {
456 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
457 [PERF_RECORD_COMM] = perf_event__comm_swap,
458 [PERF_RECORD_FORK] = perf_event__task_swap,
459 [PERF_RECORD_EXIT] = perf_event__task_swap,
460 [PERF_RECORD_LOST] = perf_event__all64_swap,
461 [PERF_RECORD_READ] = perf_event__read_swap,
462 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
David Aherneda39132011-07-15 12:34:09 -0600463 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200464 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
465 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
466 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
467 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200468};
469
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200470struct sample_queue {
471 u64 timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000472 u64 file_offset;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200473 union perf_event *event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200474 struct list_head list;
475};
476
Thomas Gleixner020bb752010-11-30 17:49:53 +0000477static void perf_session_free_sample_buffers(struct perf_session *session)
478{
479 struct ordered_samples *os = &session->ordered_samples;
480
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000481 while (!list_empty(&os->to_free)) {
Thomas Gleixner020bb752010-11-30 17:49:53 +0000482 struct sample_queue *sq;
483
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000484 sq = list_entry(os->to_free.next, struct sample_queue, list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000485 list_del(&sq->list);
486 free(sq);
487 }
488}
489
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100490static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200491 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200492 struct perf_sample *sample,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000493 struct perf_event_ops *ops,
494 u64 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100495
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200496static void flush_sample_queue(struct perf_session *s,
497 struct perf_event_ops *ops)
498{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000499 struct ordered_samples *os = &s->ordered_samples;
500 struct list_head *head = &os->samples;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200501 struct sample_queue *tmp, *iter;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200502 struct perf_sample sample;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000503 u64 limit = os->next_flush;
504 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200505 unsigned idx = 0, progress_next = os->nr_samples / 16;
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200506 int ret;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200507
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200508 if (!ops->ordered_samples || !limit)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200509 return;
510
511 list_for_each_entry_safe(iter, tmp, head, list) {
512 if (iter->timestamp > limit)
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000513 break;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200514
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200515 ret = perf_session__parse_sample(s, iter->event, &sample);
516 if (ret)
517 pr_err("Can't parse sample, err = %d\n", ret);
518 else
519 perf_session_deliver_event(s, iter->event, &sample, ops,
520 iter->file_offset);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200521
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000522 os->last_flush = iter->timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200523 list_del(&iter->list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000524 list_add(&iter->list, &os->sample_cache);
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200525 if (++idx >= progress_next) {
526 progress_next += os->nr_samples / 16;
527 ui_progress__update(idx, os->nr_samples,
528 "Processing time ordered events...");
529 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200530 }
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000531
532 if (list_empty(head)) {
533 os->last_sample = NULL;
534 } else if (last_ts <= limit) {
535 os->last_sample =
536 list_entry(head->prev, struct sample_queue, list);
537 }
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200538
539 os->nr_samples = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200540}
541
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200542/*
543 * When perf record finishes a pass on every buffers, it records this pseudo
544 * event.
545 * We record the max timestamp t found in the pass n.
546 * Assuming these timestamps are monotonic across cpus, we know that if
547 * a buffer still has events with timestamps below t, they will be all
548 * available and then read in the pass n + 1.
549 * Hence when we start to read the pass n + 2, we can safely flush every
550 * events with timestamps below t.
551 *
552 * ============ PASS n =================
553 * CPU 0 | CPU 1
554 * |
555 * cnt1 timestamps | cnt2 timestamps
556 * 1 | 2
557 * 2 | 3
558 * - | 4 <--- max recorded
559 *
560 * ============ PASS n + 1 ==============
561 * CPU 0 | CPU 1
562 * |
563 * cnt1 timestamps | cnt2 timestamps
564 * 3 | 5
565 * 4 | 6
566 * 5 | 7 <---- max recorded
567 *
568 * Flush every events below timestamp 4
569 *
570 * ============ PASS n + 2 ==============
571 * CPU 0 | CPU 1
572 * |
573 * cnt1 timestamps | cnt2 timestamps
574 * 6 | 8
575 * 7 | 9
576 * - | 10
577 *
578 * Flush every events below timestamp 7
579 * etc...
580 */
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200581static int process_finished_round(union perf_event *event __used,
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200582 struct perf_session *session,
583 struct perf_event_ops *ops)
584{
585 flush_sample_queue(session, ops);
586 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
587
588 return 0;
589}
590
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200591/* The queue is ordered by time */
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100592static void __queue_event(struct sample_queue *new, struct perf_session *s)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200593{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000594 struct ordered_samples *os = &s->ordered_samples;
595 struct sample_queue *sample = os->last_sample;
596 u64 timestamp = new->timestamp;
597 struct list_head *p;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200598
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200599 ++os->nr_samples;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000600 os->last_sample = new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200601
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000602 if (!sample) {
603 list_add(&new->list, &os->samples);
604 os->max_timestamp = timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200605 return;
606 }
607
608 /*
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000609 * last_sample might point to some random place in the list as it's
610 * the last queued event. We expect that the new event is close to
611 * this.
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200612 */
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000613 if (sample->timestamp <= timestamp) {
614 while (sample->timestamp <= timestamp) {
615 p = sample->list.next;
616 if (p == &os->samples) {
617 list_add_tail(&new->list, &os->samples);
618 os->max_timestamp = timestamp;
619 return;
620 }
621 sample = list_entry(p, struct sample_queue, list);
622 }
623 list_add_tail(&new->list, &sample->list);
624 } else {
625 while (sample->timestamp > timestamp) {
626 p = sample->list.prev;
627 if (p == &os->samples) {
628 list_add(&new->list, &os->samples);
629 return;
630 }
631 sample = list_entry(p, struct sample_queue, list);
632 }
633 list_add(&new->list, &sample->list);
634 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200635}
636
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000637#define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
638
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200639static int perf_session_queue_event(struct perf_session *s, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200640 struct perf_sample *sample, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200641{
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000642 struct ordered_samples *os = &s->ordered_samples;
643 struct list_head *sc = &os->sample_cache;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200644 u64 timestamp = sample->time;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200645 struct sample_queue *new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200646
Thomas Gleixner79a14c12010-12-07 12:48:44 +0000647 if (!timestamp || timestamp == ~0ULL)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100648 return -ETIME;
649
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200650 if (timestamp < s->ordered_samples.last_flush) {
651 printf("Warning: Timestamp below last timeslice flush\n");
652 return -EINVAL;
653 }
654
Thomas Gleixner020bb752010-11-30 17:49:53 +0000655 if (!list_empty(sc)) {
656 new = list_entry(sc->next, struct sample_queue, list);
657 list_del(&new->list);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000658 } else if (os->sample_buffer) {
659 new = os->sample_buffer + os->sample_buffer_idx;
660 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
661 os->sample_buffer = NULL;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000662 } else {
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000663 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
664 if (!os->sample_buffer)
Thomas Gleixner020bb752010-11-30 17:49:53 +0000665 return -ENOMEM;
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000666 list_add(&os->sample_buffer->list, &os->to_free);
667 os->sample_buffer_idx = 2;
668 new = os->sample_buffer + 1;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000669 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200670
671 new->timestamp = timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000672 new->file_offset = file_offset;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000673 new->event = event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200674
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100675 __queue_event(new, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200676
677 return 0;
678}
679
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200680static void callchain__printf(struct perf_sample *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200681{
682 unsigned int i;
683
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200684 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200685
686 for (i = 0; i < sample->callchain->nr; i++)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200687 printf("..... %2d: %016" PRIx64 "\n",
688 i, sample->callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200689}
690
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200691static void perf_session__print_tstamp(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200692 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200693 struct perf_sample *sample)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200694{
695 if (event->header.type != PERF_RECORD_SAMPLE &&
696 !session->sample_id_all) {
697 fputs("-1 -1 ", stdout);
698 return;
699 }
700
701 if ((session->sample_type & PERF_SAMPLE_CPU))
702 printf("%u ", sample->cpu);
703
704 if (session->sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200705 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200706}
707
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200708static void dump_event(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200709 u64 file_offset, struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000710{
711 if (!dump_trace)
712 return;
713
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200714 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
715 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000716
717 trace_event(event);
718
719 if (sample)
720 perf_session__print_tstamp(session, event, sample);
721
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200722 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200723 event->header.size, perf_event__name(event->header.type));
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000724}
725
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200726static void dump_sample(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200727 struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000728{
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200729 if (!dump_trace)
730 return;
731
David Ahern7cec0922011-05-30 13:08:23 -0600732 printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200733 event->header.misc, sample->pid, sample->tid, sample->ip,
David Ahern7cec0922011-05-30 13:08:23 -0600734 sample->period, sample->addr);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000735
736 if (session->sample_type & PERF_SAMPLE_CALLCHAIN)
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200737 callchain__printf(sample);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000738}
739
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100740static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200741 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200742 struct perf_sample *sample,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000743 struct perf_event_ops *ops,
Thomas Gleixner532e7262010-12-07 12:48:55 +0000744 u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100745{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300746 struct perf_evsel *evsel;
747
Thomas Gleixner532e7262010-12-07 12:48:55 +0000748 dump_event(session, event, file_offset, sample);
749
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200750 evsel = perf_evlist__id2evsel(session->evlist, sample->id);
751 if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) {
752 /*
753 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
754 * because the tools right now may apply filters, discarding
755 * some of the samples. For consistency, in the future we
756 * should have something like nr_filtered_samples and remove
757 * the sample->period from total_sample_period, etc, KISS for
758 * now tho.
759 *
760 * Also testing against NULL allows us to handle files without
761 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
762 * future probably it'll be a good idea to restrict event
763 * processing via perf_session to files with both set.
764 */
765 hists__inc_nr_events(&evsel->hists, event->header.type);
766 }
767
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100768 switch (event->header.type) {
769 case PERF_RECORD_SAMPLE:
Thomas Gleixner532e7262010-12-07 12:48:55 +0000770 dump_sample(session, event, sample);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300771 if (evsel == NULL) {
772 ++session->hists.stats.nr_unknown_id;
773 return -1;
774 }
775 return ops->sample(event, sample, evsel, session);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100776 case PERF_RECORD_MMAP:
777 return ops->mmap(event, sample, session);
778 case PERF_RECORD_COMM:
779 return ops->comm(event, sample, session);
780 case PERF_RECORD_FORK:
781 return ops->fork(event, sample, session);
782 case PERF_RECORD_EXIT:
783 return ops->exit(event, sample, session);
784 case PERF_RECORD_LOST:
785 return ops->lost(event, sample, session);
786 case PERF_RECORD_READ:
787 return ops->read(event, sample, session);
788 case PERF_RECORD_THROTTLE:
789 return ops->throttle(event, sample, session);
790 case PERF_RECORD_UNTHROTTLE:
791 return ops->unthrottle(event, sample, session);
792 default:
793 ++session->hists.stats.nr_unknown_events;
794 return -1;
795 }
796}
797
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000798static int perf_session__preprocess_sample(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200799 union perf_event *event, struct perf_sample *sample)
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000800{
801 if (event->header.type != PERF_RECORD_SAMPLE ||
802 !(session->sample_type & PERF_SAMPLE_CALLCHAIN))
803 return 0;
804
805 if (!ip_callchain__valid(sample->callchain, event)) {
806 pr_debug("call-chain problem with event, skipping it.\n");
807 ++session->hists.stats.nr_invalid_chains;
808 session->hists.stats.total_invalid_chains += sample->period;
809 return -EINVAL;
810 }
811 return 0;
812}
813
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200814static int perf_session__process_user_event(struct perf_session *session, union perf_event *event,
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000815 struct perf_event_ops *ops, u64 file_offset)
816{
817 dump_event(session, event, file_offset, NULL);
818
819 /* These events are processed right away */
820 switch (event->header.type) {
821 case PERF_RECORD_HEADER_ATTR:
822 return ops->attr(event, session);
823 case PERF_RECORD_HEADER_EVENT_TYPE:
824 return ops->event_type(event, session);
825 case PERF_RECORD_HEADER_TRACING_DATA:
826 /* setup for reading amidst mmap */
827 lseek(session->fd, file_offset, SEEK_SET);
828 return ops->tracing_data(event, session);
829 case PERF_RECORD_HEADER_BUILD_ID:
830 return ops->build_id(event, session);
831 case PERF_RECORD_FINISHED_ROUND:
832 return ops->finished_round(event, session, ops);
833 default:
834 return -EINVAL;
835 }
836}
837
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100838static int perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200839 union perf_event *event,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200840 struct perf_event_ops *ops,
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000841 u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200842{
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200843 struct perf_sample sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100844 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200845
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200846 if (session->header.needs_swap &&
847 perf_event__swap_ops[event->header.type])
848 perf_event__swap_ops[event->header.type](event);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200849
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000850 if (event->header.type >= PERF_RECORD_HEADER_MAX)
851 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200852
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000853 hists__inc_nr_events(&session->hists, event->header.type);
854
855 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000856 return perf_session__process_user_event(session, event, ops, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100857
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000858 /*
859 * For all kernel events we get the sample data
860 */
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200861 ret = perf_session__parse_sample(session, event, &sample);
862 if (ret)
863 return ret;
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000864
865 /* Preprocess sample records - precheck callchains */
866 if (perf_session__preprocess_sample(session, event, &sample))
867 return 0;
868
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100869 if (ops->ordered_samples) {
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000870 ret = perf_session_queue_event(session, event, &sample,
871 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100872 if (ret != -ETIME)
873 return ret;
874 }
875
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000876 return perf_session_deliver_event(session, event, &sample, ops,
877 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200878}
879
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200880void perf_event_header__bswap(struct perf_event_header *self)
881{
882 self->type = bswap_32(self->type);
883 self->misc = bswap_16(self->misc);
884 self->size = bswap_16(self->size);
885}
886
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200887static struct thread *perf_session__register_idle_thread(struct perf_session *self)
888{
889 struct thread *thread = perf_session__findnew(self, 0);
890
891 if (thread == NULL || thread__set_comm(thread, "swapper")) {
892 pr_err("problem inserting idle task.\n");
893 thread = NULL;
894 }
895
896 return thread;
897}
898
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200899static void perf_session__warn_about_errors(const struct perf_session *session,
900 const struct perf_event_ops *ops)
901{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200902 if (ops->lost == perf_event__process_lost &&
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200903 session->hists.stats.nr_events[PERF_RECORD_LOST] != 0) {
904 ui__warning("Processed %d events and lost %d chunks!\n\n"
905 "Check IO/CPU overload!\n\n",
906 session->hists.stats.nr_events[0],
907 session->hists.stats.nr_events[PERF_RECORD_LOST]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200908 }
909
910 if (session->hists.stats.nr_unknown_events != 0) {
911 ui__warning("Found %u unknown events!\n\n"
912 "Is this an older tool processing a perf.data "
913 "file generated by a more recent tool?\n\n"
914 "If that is not the case, consider "
915 "reporting to linux-kernel@vger.kernel.org.\n\n",
916 session->hists.stats.nr_unknown_events);
917 }
918
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300919 if (session->hists.stats.nr_unknown_id != 0) {
920 ui__warning("%u samples with id not present in the header\n",
921 session->hists.stats.nr_unknown_id);
922 }
923
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200924 if (session->hists.stats.nr_invalid_chains != 0) {
925 ui__warning("Found invalid callchains!\n\n"
926 "%u out of %u events were discarded for this reason.\n\n"
927 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
928 session->hists.stats.nr_invalid_chains,
929 session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
930 }
931}
932
Tom Zanussi8dc58102010-04-01 23:59:15 -0500933#define session_done() (*(volatile int *)(&session_done))
934volatile int session_done;
935
936static int __perf_session__process_pipe_events(struct perf_session *self,
937 struct perf_event_ops *ops)
938{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200939 union perf_event event;
Tom Zanussi8dc58102010-04-01 23:59:15 -0500940 uint32_t size;
941 int skip = 0;
942 u64 head;
943 int err;
944 void *p;
945
946 perf_event_ops__fill_defaults(ops);
947
948 head = 0;
949more:
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200950 err = readn(self->fd, &event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -0500951 if (err <= 0) {
952 if (err == 0)
953 goto done;
954
955 pr_err("failed to read event header\n");
956 goto out_err;
957 }
958
959 if (self->header.needs_swap)
960 perf_event_header__bswap(&event.header);
961
962 size = event.header.size;
963 if (size == 0)
964 size = 8;
965
966 p = &event;
967 p += sizeof(struct perf_event_header);
968
Tom Zanussi794e43b2010-05-05 00:27:40 -0500969 if (size - sizeof(struct perf_event_header)) {
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200970 err = readn(self->fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -0500971 if (err <= 0) {
972 if (err == 0) {
973 pr_err("unexpected end of event stream\n");
974 goto done;
975 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500976
Tom Zanussi794e43b2010-05-05 00:27:40 -0500977 pr_err("failed to read event data\n");
978 goto out_err;
979 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500980 }
981
982 if (size == 0 ||
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000983 (skip = perf_session__process_event(self, &event, ops, head)) < 0) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200984 dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
Tom Zanussi8dc58102010-04-01 23:59:15 -0500985 head, event.header.size, event.header.type);
986 /*
987 * assume we lost track of the stream, check alignment, and
988 * increment a single u64 in the hope to catch on again 'soon'.
989 */
990 if (unlikely(head & 7))
991 head &= ~7ULL;
992
993 size = 8;
994 }
995
996 head += size;
997
Tom Zanussi8dc58102010-04-01 23:59:15 -0500998 if (skip > 0)
999 head += skip;
1000
1001 if (!session_done())
1002 goto more;
1003done:
1004 err = 0;
1005out_err:
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001006 perf_session__warn_about_errors(self, ops);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001007 perf_session_free_sample_buffers(self);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001008 return err;
1009}
1010
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001011static union perf_event *
1012fetch_mmaped_event(struct perf_session *session,
1013 u64 head, size_t mmap_size, char *buf)
1014{
1015 union perf_event *event;
1016
1017 /*
1018 * Ensure we have enough space remaining to read
1019 * the size of the event in the headers.
1020 */
1021 if (head + sizeof(event->header) > mmap_size)
1022 return NULL;
1023
1024 event = (union perf_event *)(buf + head);
1025
1026 if (session->header.needs_swap)
1027 perf_event_header__bswap(&event->header);
1028
1029 if (head + event->header.size > mmap_size)
1030 return NULL;
1031
1032 return event;
1033}
1034
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001035int __perf_session__process_events(struct perf_session *session,
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001036 u64 data_offset, u64 data_size,
1037 u64 file_size, struct perf_event_ops *ops)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001038{
Thomas Gleixner55b446292010-11-30 17:49:46 +00001039 u64 head, page_offset, file_offset, file_pos, progress_next;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001040 int err, mmap_prot, mmap_flags, map_idx = 0;
Thomas Gleixner55b446292010-11-30 17:49:46 +00001041 size_t page_size, mmap_size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001042 char *buf, *mmaps[8];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001043 union perf_event *event;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001044 uint32_t size;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001045
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001046 perf_event_ops__fill_defaults(ops);
1047
Arnaldo Carvalho de Melo1b759622010-01-14 18:30:04 -02001048 page_size = sysconf(_SC_PAGESIZE);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001049
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001050 page_offset = page_size * (data_offset / page_size);
1051 file_offset = page_offset;
1052 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001053
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001054 if (data_offset + data_size < file_size)
1055 file_size = data_offset + data_size;
1056
Thomas Gleixner55b446292010-11-30 17:49:46 +00001057 progress_next = file_size / 16;
Thomas Gleixner55b446292010-11-30 17:49:46 +00001058
1059 mmap_size = session->mmap_window;
1060 if (mmap_size > file_size)
1061 mmap_size = file_size;
1062
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001063 memset(mmaps, 0, sizeof(mmaps));
1064
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001065 mmap_prot = PROT_READ;
1066 mmap_flags = MAP_SHARED;
1067
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001068 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001069 mmap_prot |= PROT_WRITE;
1070 mmap_flags = MAP_PRIVATE;
1071 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001072remap:
Thomas Gleixner55b446292010-11-30 17:49:46 +00001073 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
1074 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001075 if (buf == MAP_FAILED) {
1076 pr_err("failed to mmap file\n");
1077 err = -errno;
1078 goto out_err;
1079 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001080 mmaps[map_idx] = buf;
1081 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001082 file_pos = file_offset + head;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001083
1084more:
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001085 event = fetch_mmaped_event(session, head, mmap_size, buf);
1086 if (!event) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001087 if (mmaps[map_idx]) {
1088 munmap(mmaps[map_idx], mmap_size);
1089 mmaps[map_idx] = NULL;
1090 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001091
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001092 page_offset = page_size * (head / page_size);
1093 file_offset += page_offset;
1094 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001095 goto remap;
1096 }
1097
1098 size = event->header.size;
1099
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001100 if (size == 0 ||
1101 perf_session__process_event(session, event, ops, file_pos) < 0) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001102 dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001103 file_offset + head, event->header.size,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001104 event->header.type);
1105 /*
1106 * assume we lost track of the stream, check alignment, and
1107 * increment a single u64 in the hope to catch on again 'soon'.
1108 */
1109 if (unlikely(head & 7))
1110 head &= ~7ULL;
1111
1112 size = 8;
1113 }
1114
1115 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001116 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001117
Thomas Gleixner55b446292010-11-30 17:49:46 +00001118 if (file_pos >= progress_next) {
1119 progress_next += file_size / 16;
Arnaldo Carvalho de Meloca59bcb2011-10-25 13:29:11 -02001120 ui_progress__update(file_pos, file_size,
1121 "Processing events...");
Thomas Gleixner55b446292010-11-30 17:49:46 +00001122 }
1123
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001124 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001125 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001126
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001127 err = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001128 /* do the final flush for ordered samples */
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001129 session->ordered_samples.next_flush = ULLONG_MAX;
1130 flush_sample_queue(session, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001131out_err:
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001132 perf_session__warn_about_errors(session, ops);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001133 perf_session_free_sample_buffers(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001134 return err;
1135}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001136
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001137int perf_session__process_events(struct perf_session *self,
1138 struct perf_event_ops *ops)
1139{
1140 int err;
1141
1142 if (perf_session__register_idle_thread(self) == NULL)
1143 return -ENOMEM;
1144
Tom Zanussi8dc58102010-04-01 23:59:15 -05001145 if (!self->fd_pipe)
1146 err = __perf_session__process_events(self,
1147 self->header.data_offset,
1148 self->header.data_size,
1149 self->size, ops);
1150 else
1151 err = __perf_session__process_pipe_events(self, ops);
Dave Martin88ca8952010-07-27 11:46:12 -03001152
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001153 return err;
1154}
1155
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001156bool perf_session__has_traces(struct perf_session *self, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001157{
1158 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001159 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1160 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001161 }
1162
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001163 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001164}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001165
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001166int perf_session__set_kallsyms_ref_reloc_sym(struct map **maps,
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001167 const char *symbol_name,
1168 u64 addr)
1169{
1170 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001171 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001172 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001173
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001174 ref = zalloc(sizeof(struct ref_reloc_sym));
1175 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001176 return -ENOMEM;
1177
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001178 ref->name = strdup(symbol_name);
1179 if (ref->name == NULL) {
1180 free(ref);
1181 return -ENOMEM;
1182 }
1183
1184 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001185 if (bracket)
1186 *bracket = '\0';
1187
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001188 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001189
1190 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001191 struct kmap *kmap = map__kmap(maps[i]);
1192 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001193 }
1194
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001195 return 0;
1196}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001197
1198size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
1199{
1200 return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
1201 __dsos__fprintf(&self->host_machine.user_dsos, fp) +
1202 machines__fprintf_dsos(&self->machines, fp);
1203}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001204
1205size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
1206 bool with_hits)
1207{
1208 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
1209 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
1210}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001211
1212size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1213{
1214 struct perf_evsel *pos;
1215 size_t ret = fprintf(fp, "Aggregated stats:\n");
1216
1217 ret += hists__fprintf_nr_events(&session->hists, fp);
1218
1219 list_for_each_entry(pos, &session->evlist->entries, node) {
1220 ret += fprintf(fp, "%s stats:\n", event_name(pos));
1221 ret += hists__fprintf_nr_events(&pos->hists, fp);
1222 }
1223
1224 return ret;
1225}
David Ahernc0230b22011-03-09 22:23:27 -07001226
David Ahern9cbdb702011-04-06 21:54:20 -06001227struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1228 unsigned int type)
1229{
1230 struct perf_evsel *pos;
1231
1232 list_for_each_entry(pos, &session->evlist->entries, node) {
1233 if (pos->attr.type == type)
1234 return pos;
1235 }
1236 return NULL;
1237}
1238
David Ahern787bef12011-05-27 14:28:43 -06001239void perf_session__print_ip(union perf_event *event,
1240 struct perf_sample *sample,
1241 struct perf_session *session,
David Ahern610723f2011-05-27 14:28:44 -06001242 int print_sym, int print_dso)
David Ahernc0230b22011-03-09 22:23:27 -07001243{
1244 struct addr_location al;
1245 const char *symname, *dsoname;
1246 struct callchain_cursor *cursor = &session->callchain_cursor;
1247 struct callchain_cursor_node *node;
1248
1249 if (perf_event__preprocess_sample(event, session, &al, sample,
1250 NULL) < 0) {
1251 error("problem processing %d event, skipping it.\n",
1252 event->header.type);
1253 return;
1254 }
1255
1256 if (symbol_conf.use_callchain && sample->callchain) {
1257
1258 if (perf_session__resolve_callchain(session, al.thread,
1259 sample->callchain, NULL) != 0) {
1260 if (verbose)
1261 error("Failed to resolve callchain. Skipping\n");
1262 return;
1263 }
1264 callchain_cursor_commit(cursor);
1265
1266 while (1) {
1267 node = callchain_cursor_current(cursor);
1268 if (!node)
1269 break;
1270
David Ahern787bef12011-05-27 14:28:43 -06001271 printf("\t%16" PRIx64, node->ip);
1272 if (print_sym) {
1273 if (node->sym && node->sym->name)
1274 symname = node->sym->name;
1275 else
1276 symname = "";
David Ahernc0230b22011-03-09 22:23:27 -07001277
David Ahern610723f2011-05-27 14:28:44 -06001278 printf(" %s", symname);
1279 }
1280 if (print_dso) {
David Ahern787bef12011-05-27 14:28:43 -06001281 if (node->map && node->map->dso && node->map->dso->name)
1282 dsoname = node->map->dso->name;
1283 else
1284 dsoname = "";
David Ahernc0230b22011-03-09 22:23:27 -07001285
David Ahern610723f2011-05-27 14:28:44 -06001286 printf(" (%s)", dsoname);
David Ahern787bef12011-05-27 14:28:43 -06001287 }
1288 printf("\n");
David Ahernc0230b22011-03-09 22:23:27 -07001289
1290 callchain_cursor_advance(cursor);
1291 }
1292
1293 } else {
David Ahernadc4bf92011-05-30 09:16:27 -06001294 printf("%16" PRIx64, sample->ip);
David Ahern787bef12011-05-27 14:28:43 -06001295 if (print_sym) {
1296 if (al.sym && al.sym->name)
1297 symname = al.sym->name;
1298 else
1299 symname = "";
David Ahernc0230b22011-03-09 22:23:27 -07001300
David Ahern610723f2011-05-27 14:28:44 -06001301 printf(" %s", symname);
1302 }
1303
1304 if (print_dso) {
David Ahern787bef12011-05-27 14:28:43 -06001305 if (al.map && al.map->dso && al.map->dso->name)
1306 dsoname = al.map->dso->name;
1307 else
1308 dsoname = "";
David Ahernc0230b22011-03-09 22:23:27 -07001309
David Ahern610723f2011-05-27 14:28:44 -06001310 printf(" (%s)", dsoname);
David Ahern787bef12011-05-27 14:28:43 -06001311 }
David Ahernc0230b22011-03-09 22:23:27 -07001312 }
1313}
Anton Blanchard5d67be92011-07-04 21:57:50 +10001314
1315int perf_session__cpu_bitmap(struct perf_session *session,
1316 const char *cpu_list, unsigned long *cpu_bitmap)
1317{
1318 int i;
1319 struct cpu_map *map;
1320
1321 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1322 struct perf_evsel *evsel;
1323
1324 evsel = perf_session__find_first_evtype(session, i);
1325 if (!evsel)
1326 continue;
1327
1328 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1329 pr_err("File does not contain CPU events. "
1330 "Remove -c option to proceed.\n");
1331 return -1;
1332 }
1333 }
1334
1335 map = cpu_map__new(cpu_list);
David Ahern47fbe532011-11-13 10:45:27 -07001336 if (map == NULL) {
1337 pr_err("Invalid cpu_list\n");
1338 return -1;
1339 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001340
1341 for (i = 0; i < map->nr; i++) {
1342 int cpu = map->map[i];
1343
1344 if (cpu >= MAX_NR_CPUS) {
1345 pr_err("Requested CPU %d too large. "
1346 "Consider raising MAX_NR_CPUS\n", cpu);
1347 return -1;
1348 }
1349
1350 set_bit(cpu, cpu_bitmap);
1351 }
1352
1353 return 0;
1354}
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001355
1356void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1357 bool full)
1358{
1359 struct stat st;
1360 int ret;
1361
1362 if (session == NULL || fp == NULL)
1363 return;
1364
1365 ret = fstat(session->fd, &st);
1366 if (ret == -1)
1367 return;
1368
1369 fprintf(fp, "# ========\n");
1370 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1371 perf_header__fprintf_info(session, fp, full);
1372 fprintf(fp, "# ========\n#\n");
1373}