blob: 734358b51ed15b8085f246357c686a947871db61 [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 -020081void perf_session__update_sample_type(struct perf_session *self)
82{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030083 self->sample_type = perf_evlist__sample_type(self->evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030084 self->sample_size = __perf_evsel__sample_size(self->sample_type);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030085 self->sample_id_all = perf_evlist__sample_id_all(self->evlist);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -020086 self->id_hdr_size = perf_evlist__id_hdr_size(self->evlist);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020087}
88
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080089int perf_session__create_kernel_maps(struct perf_session *self)
90{
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -030091 int ret = machine__create_kernel_maps(&self->host_machine);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080092
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080093 if (ret >= 0)
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -030094 ret = machines__create_guest_kernel_maps(&self->machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080095 return ret;
96}
97
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -030098static void perf_session__destroy_kernel_maps(struct perf_session *self)
99{
100 machine__destroy_kernel_maps(&self->host_machine);
101 machines__destroy_guest_kernel_maps(&self->machines);
102}
103
Ian Munsie21ef97f2010-12-10 14:09:16 +1100104struct perf_session *perf_session__new(const char *filename, int mode,
105 bool force, bool repipe,
106 struct perf_event_ops *ops)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200107{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200108 size_t len = filename ? strlen(filename) + 1 : 0;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200109 struct perf_session *self = zalloc(sizeof(*self) + len);
110
111 if (self == NULL)
112 goto out;
113
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200114 memcpy(self->filename, filename, len);
Thomas Gleixner55b446292010-11-30 17:49:46 +0000115 /*
116 * On 64bit we can mmap the data file in one go. No need for tiny mmap
117 * slices. On 32bit we use 32MB.
118 */
119#if BITS_PER_LONG == 64
120 self->mmap_window = ULLONG_MAX;
121#else
122 self->mmap_window = 32 * 1024 * 1024ULL;
123#endif
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300124 self->machines = RB_ROOT;
Tom Zanussi454c4072010-05-01 01:41:20 -0500125 self->repipe = repipe;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000126 INIT_LIST_HEAD(&self->ordered_samples.samples);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000127 INIT_LIST_HEAD(&self->ordered_samples.sample_cache);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000128 INIT_LIST_HEAD(&self->ordered_samples.to_free);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300129 machine__init(&self->host_machine, "", HOST_KERNEL_ID);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200130
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200131 if (mode == O_RDONLY) {
132 if (perf_session__open(self, force) < 0)
133 goto out_delete;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300134 perf_session__update_sample_type(self);
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200135 } else if (mode == O_WRONLY) {
136 /*
137 * In O_RDONLY mode this will be performed when reading the
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200138 * kernel MMAP event, in perf_event__process_mmap().
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200139 */
140 if (perf_session__create_kernel_maps(self) < 0)
141 goto out_delete;
142 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200143
Ian Munsie21ef97f2010-12-10 14:09:16 +1100144 if (ops && ops->ordering_requires_timestamps &&
145 ops->ordered_samples && !self->sample_id_all) {
146 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
147 ops->ordered_samples = false;
148 }
149
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200150out:
151 return self;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200152out_delete:
153 perf_session__delete(self);
154 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200155}
156
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200157static void machine__delete_dead_threads(struct machine *machine)
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300158{
159 struct thread *n, *t;
160
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200161 list_for_each_entry_safe(t, n, &machine->dead_threads, node) {
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300162 list_del(&t->node);
163 thread__delete(t);
164 }
165}
166
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200167static void perf_session__delete_dead_threads(struct perf_session *session)
168{
169 machine__delete_dead_threads(&session->host_machine);
170}
171
172static void machine__delete_threads(struct machine *self)
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300173{
174 struct rb_node *nd = rb_first(&self->threads);
175
176 while (nd) {
177 struct thread *t = rb_entry(nd, struct thread, rb_node);
178
179 rb_erase(&t->rb_node, &self->threads);
180 nd = rb_next(nd);
181 thread__delete(t);
182 }
183}
184
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200185static void perf_session__delete_threads(struct perf_session *session)
186{
187 machine__delete_threads(&session->host_machine);
188}
189
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200190void perf_session__delete(struct perf_session *self)
191{
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300192 perf_session__destroy_kernel_maps(self);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300193 perf_session__delete_dead_threads(self);
194 perf_session__delete_threads(self);
195 machine__exit(&self->host_machine);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200196 close(self->fd);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200197 free(self);
198}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200199
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200200void machine__remove_thread(struct machine *self, struct thread *th)
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300201{
Arnaldo Carvalho de Melo70597f22010-08-02 18:59:28 -0300202 self->last_match = NULL;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300203 rb_erase(&th->rb_node, &self->threads);
204 /*
205 * We may have references to this thread, for instance in some hist_entry
206 * instances, so just move them to a separate list.
207 */
208 list_add_tail(&th->node, &self->dead_threads);
209}
210
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200211static bool symbol__match_parent_regex(struct symbol *sym)
212{
213 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
214 return 1;
215
216 return 0;
217}
218
Arnaldo Carvalho de Melo246d4ce2011-11-11 23:10:26 -0200219int perf_session__resolve_callchain(struct perf_session *self, struct perf_evsel *evsel,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100220 struct thread *thread,
221 struct ip_callchain *chain,
222 struct symbol **parent)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200223{
224 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200225 unsigned int i;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100226 int err;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200227
Arnaldo Carvalho de Melo246d4ce2011-11-11 23:10:26 -0200228 callchain_cursor_reset(&evsel->hists.callchain_cursor);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200229
230 for (i = 0; i < chain->nr; i++) {
Sam Liaod797fdc2011-06-07 23:49:46 +0800231 u64 ip;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200232 struct addr_location al;
233
Sam Liaod797fdc2011-06-07 23:49:46 +0800234 if (callchain_param.order == ORDER_CALLEE)
235 ip = chain->ips[i];
236 else
237 ip = chain->ips[chain->nr - i - 1];
238
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200239 if (ip >= PERF_CONTEXT_MAX) {
240 switch (ip) {
241 case PERF_CONTEXT_HV:
242 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
243 case PERF_CONTEXT_KERNEL:
244 cpumode = PERF_RECORD_MISC_KERNEL; break;
245 case PERF_CONTEXT_USER:
246 cpumode = PERF_RECORD_MISC_USER; break;
247 default:
248 break;
249 }
250 continue;
251 }
252
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800253 al.filtered = false;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200254 thread__find_addr_location(thread, self, cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800255 MAP__FUNCTION, thread->pid, ip, &al, NULL);
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200256 if (al.sym != NULL) {
257 if (sort__has_parent && !*parent &&
258 symbol__match_parent_regex(al.sym))
259 *parent = al.sym;
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200260 if (!symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200261 break;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200262 }
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100263
Arnaldo Carvalho de Melo246d4ce2011-11-11 23:10:26 -0200264 err = callchain_cursor_append(&evsel->hists.callchain_cursor,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100265 ip, al.map, al.sym);
266 if (err)
267 return err;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200268 }
269
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100270 return 0;
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200271}
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200272
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200273static int process_event_synth_stub(union perf_event *event __used,
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200274 struct perf_session *session __used)
275{
276 dump_printf(": unhandled!\n");
277 return 0;
278}
279
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200280static int process_event_synth_attr_stub(union perf_event *event __used,
281 struct perf_evlist **pevlist __used)
282{
283 dump_printf(": unhandled!\n");
284 return 0;
285}
286
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300287static int process_event_sample_stub(union perf_event *event __used,
288 struct perf_sample *sample __used,
289 struct perf_evsel *evsel __used,
290 struct perf_session *session __used)
291{
292 dump_printf(": unhandled!\n");
293 return 0;
294}
295
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200296static int process_event_stub(union perf_event *event __used,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200297 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200298 struct perf_session *session __used)
299{
300 dump_printf(": unhandled!\n");
301 return 0;
302}
303
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200304static int process_finished_round_stub(union perf_event *event __used,
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200305 struct perf_session *session __used,
306 struct perf_event_ops *ops __used)
307{
308 dump_printf(": unhandled!\n");
309 return 0;
310}
311
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200312static int process_finished_round(union perf_event *event,
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200313 struct perf_session *session,
314 struct perf_event_ops *ops);
315
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200316static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
317{
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200318 if (handler->sample == NULL)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300319 handler->sample = process_event_sample_stub;
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200320 if (handler->mmap == NULL)
321 handler->mmap = process_event_stub;
322 if (handler->comm == NULL)
323 handler->comm = process_event_stub;
324 if (handler->fork == NULL)
325 handler->fork = process_event_stub;
326 if (handler->exit == NULL)
327 handler->exit = process_event_stub;
328 if (handler->lost == NULL)
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200329 handler->lost = perf_event__process_lost;
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200330 if (handler->read == NULL)
331 handler->read = process_event_stub;
332 if (handler->throttle == NULL)
333 handler->throttle = process_event_stub;
334 if (handler->unthrottle == NULL)
335 handler->unthrottle = process_event_stub;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500336 if (handler->attr == NULL)
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200337 handler->attr = process_event_synth_attr_stub;
Tom Zanussicd19a032010-04-01 23:59:20 -0500338 if (handler->event_type == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200339 handler->event_type = process_event_synth_stub;
Tom Zanussi92155452010-04-01 23:59:21 -0500340 if (handler->tracing_data == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200341 handler->tracing_data = process_event_synth_stub;
Tom Zanussic7929e42010-04-01 23:59:22 -0500342 if (handler->build_id == NULL)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200343 handler->build_id = process_event_synth_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200344 if (handler->finished_round == NULL) {
345 if (handler->ordered_samples)
346 handler->finished_round = process_finished_round;
347 else
348 handler->finished_round = process_finished_round_stub;
349 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200350}
351
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200352void mem_bswap_64(void *src, int byte_size)
353{
354 u64 *m = src;
355
356 while (byte_size > 0) {
357 *m = bswap_64(*m);
358 byte_size -= sizeof(u64);
359 ++m;
360 }
361}
362
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200363static void perf_event__all64_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200364{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200365 struct perf_event_header *hdr = &event->header;
366 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200367}
368
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200369static void perf_event__comm_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200370{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200371 event->comm.pid = bswap_32(event->comm.pid);
372 event->comm.tid = bswap_32(event->comm.tid);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200373}
374
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200375static void perf_event__mmap_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200376{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200377 event->mmap.pid = bswap_32(event->mmap.pid);
378 event->mmap.tid = bswap_32(event->mmap.tid);
379 event->mmap.start = bswap_64(event->mmap.start);
380 event->mmap.len = bswap_64(event->mmap.len);
381 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200382}
383
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200384static void perf_event__task_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200385{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200386 event->fork.pid = bswap_32(event->fork.pid);
387 event->fork.tid = bswap_32(event->fork.tid);
388 event->fork.ppid = bswap_32(event->fork.ppid);
389 event->fork.ptid = bswap_32(event->fork.ptid);
390 event->fork.time = bswap_64(event->fork.time);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200391}
392
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200393static void perf_event__read_swap(union perf_event *event)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200394{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200395 event->read.pid = bswap_32(event->read.pid);
396 event->read.tid = bswap_32(event->read.tid);
397 event->read.value = bswap_64(event->read.value);
398 event->read.time_enabled = bswap_64(event->read.time_enabled);
399 event->read.time_running = bswap_64(event->read.time_running);
400 event->read.id = bswap_64(event->read.id);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200401}
402
David Aherneda39132011-07-15 12:34:09 -0600403/* exported for swapping attributes in file header */
404void perf_event__attr_swap(struct perf_event_attr *attr)
405{
406 attr->type = bswap_32(attr->type);
407 attr->size = bswap_32(attr->size);
408 attr->config = bswap_64(attr->config);
409 attr->sample_period = bswap_64(attr->sample_period);
410 attr->sample_type = bswap_64(attr->sample_type);
411 attr->read_format = bswap_64(attr->read_format);
412 attr->wakeup_events = bswap_32(attr->wakeup_events);
413 attr->bp_type = bswap_32(attr->bp_type);
414 attr->bp_addr = bswap_64(attr->bp_addr);
415 attr->bp_len = bswap_64(attr->bp_len);
416}
417
418static void perf_event__hdr_attr_swap(union perf_event *event)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500419{
420 size_t size;
421
David Aherneda39132011-07-15 12:34:09 -0600422 perf_event__attr_swap(&event->attr.attr);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500423
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200424 size = event->header.size;
425 size -= (void *)&event->attr.id - (void *)event;
426 mem_bswap_64(event->attr.id, size);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500427}
428
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200429static void perf_event__event_type_swap(union perf_event *event)
Tom Zanussicd19a032010-04-01 23:59:20 -0500430{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200431 event->event_type.event_type.event_id =
432 bswap_64(event->event_type.event_type.event_id);
Tom Zanussicd19a032010-04-01 23:59:20 -0500433}
434
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200435static void perf_event__tracing_data_swap(union perf_event *event)
Tom Zanussi92155452010-04-01 23:59:21 -0500436{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200437 event->tracing_data.size = bswap_32(event->tracing_data.size);
Tom Zanussi92155452010-04-01 23:59:21 -0500438}
439
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200440typedef void (*perf_event__swap_op)(union perf_event *event);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200441
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200442static perf_event__swap_op perf_event__swap_ops[] = {
443 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
444 [PERF_RECORD_COMM] = perf_event__comm_swap,
445 [PERF_RECORD_FORK] = perf_event__task_swap,
446 [PERF_RECORD_EXIT] = perf_event__task_swap,
447 [PERF_RECORD_LOST] = perf_event__all64_swap,
448 [PERF_RECORD_READ] = perf_event__read_swap,
449 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
David Aherneda39132011-07-15 12:34:09 -0600450 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200451 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
452 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
453 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
454 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200455};
456
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200457struct sample_queue {
458 u64 timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000459 u64 file_offset;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200460 union perf_event *event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200461 struct list_head list;
462};
463
Thomas Gleixner020bb752010-11-30 17:49:53 +0000464static void perf_session_free_sample_buffers(struct perf_session *session)
465{
466 struct ordered_samples *os = &session->ordered_samples;
467
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000468 while (!list_empty(&os->to_free)) {
Thomas Gleixner020bb752010-11-30 17:49:53 +0000469 struct sample_queue *sq;
470
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000471 sq = list_entry(os->to_free.next, struct sample_queue, list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000472 list_del(&sq->list);
473 free(sq);
474 }
475}
476
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100477static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200478 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200479 struct perf_sample *sample,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000480 struct perf_event_ops *ops,
481 u64 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100482
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200483static void flush_sample_queue(struct perf_session *s,
484 struct perf_event_ops *ops)
485{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000486 struct ordered_samples *os = &s->ordered_samples;
487 struct list_head *head = &os->samples;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200488 struct sample_queue *tmp, *iter;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200489 struct perf_sample sample;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000490 u64 limit = os->next_flush;
491 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200492 unsigned idx = 0, progress_next = os->nr_samples / 16;
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200493 int ret;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200494
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200495 if (!ops->ordered_samples || !limit)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200496 return;
497
498 list_for_each_entry_safe(iter, tmp, head, list) {
499 if (iter->timestamp > limit)
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000500 break;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200501
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200502 ret = perf_session__parse_sample(s, iter->event, &sample);
503 if (ret)
504 pr_err("Can't parse sample, err = %d\n", ret);
505 else
506 perf_session_deliver_event(s, iter->event, &sample, ops,
507 iter->file_offset);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200508
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000509 os->last_flush = iter->timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200510 list_del(&iter->list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000511 list_add(&iter->list, &os->sample_cache);
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200512 if (++idx >= progress_next) {
513 progress_next += os->nr_samples / 16;
514 ui_progress__update(idx, os->nr_samples,
515 "Processing time ordered events...");
516 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200517 }
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000518
519 if (list_empty(head)) {
520 os->last_sample = NULL;
521 } else if (last_ts <= limit) {
522 os->last_sample =
523 list_entry(head->prev, struct sample_queue, list);
524 }
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200525
526 os->nr_samples = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200527}
528
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200529/*
530 * When perf record finishes a pass on every buffers, it records this pseudo
531 * event.
532 * We record the max timestamp t found in the pass n.
533 * Assuming these timestamps are monotonic across cpus, we know that if
534 * a buffer still has events with timestamps below t, they will be all
535 * available and then read in the pass n + 1.
536 * Hence when we start to read the pass n + 2, we can safely flush every
537 * events with timestamps below t.
538 *
539 * ============ PASS n =================
540 * CPU 0 | CPU 1
541 * |
542 * cnt1 timestamps | cnt2 timestamps
543 * 1 | 2
544 * 2 | 3
545 * - | 4 <--- max recorded
546 *
547 * ============ PASS n + 1 ==============
548 * CPU 0 | CPU 1
549 * |
550 * cnt1 timestamps | cnt2 timestamps
551 * 3 | 5
552 * 4 | 6
553 * 5 | 7 <---- max recorded
554 *
555 * Flush every events below timestamp 4
556 *
557 * ============ PASS n + 2 ==============
558 * CPU 0 | CPU 1
559 * |
560 * cnt1 timestamps | cnt2 timestamps
561 * 6 | 8
562 * 7 | 9
563 * - | 10
564 *
565 * Flush every events below timestamp 7
566 * etc...
567 */
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200568static int process_finished_round(union perf_event *event __used,
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200569 struct perf_session *session,
570 struct perf_event_ops *ops)
571{
572 flush_sample_queue(session, ops);
573 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
574
575 return 0;
576}
577
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200578/* The queue is ordered by time */
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100579static void __queue_event(struct sample_queue *new, struct perf_session *s)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200580{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000581 struct ordered_samples *os = &s->ordered_samples;
582 struct sample_queue *sample = os->last_sample;
583 u64 timestamp = new->timestamp;
584 struct list_head *p;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200585
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200586 ++os->nr_samples;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000587 os->last_sample = new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200588
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000589 if (!sample) {
590 list_add(&new->list, &os->samples);
591 os->max_timestamp = timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200592 return;
593 }
594
595 /*
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000596 * last_sample might point to some random place in the list as it's
597 * the last queued event. We expect that the new event is close to
598 * this.
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200599 */
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000600 if (sample->timestamp <= timestamp) {
601 while (sample->timestamp <= timestamp) {
602 p = sample->list.next;
603 if (p == &os->samples) {
604 list_add_tail(&new->list, &os->samples);
605 os->max_timestamp = timestamp;
606 return;
607 }
608 sample = list_entry(p, struct sample_queue, list);
609 }
610 list_add_tail(&new->list, &sample->list);
611 } else {
612 while (sample->timestamp > timestamp) {
613 p = sample->list.prev;
614 if (p == &os->samples) {
615 list_add(&new->list, &os->samples);
616 return;
617 }
618 sample = list_entry(p, struct sample_queue, list);
619 }
620 list_add(&new->list, &sample->list);
621 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200622}
623
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000624#define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
625
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200626static int perf_session_queue_event(struct perf_session *s, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200627 struct perf_sample *sample, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200628{
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000629 struct ordered_samples *os = &s->ordered_samples;
630 struct list_head *sc = &os->sample_cache;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200631 u64 timestamp = sample->time;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200632 struct sample_queue *new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200633
Thomas Gleixner79a14c12010-12-07 12:48:44 +0000634 if (!timestamp || timestamp == ~0ULL)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100635 return -ETIME;
636
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200637 if (timestamp < s->ordered_samples.last_flush) {
638 printf("Warning: Timestamp below last timeslice flush\n");
639 return -EINVAL;
640 }
641
Thomas Gleixner020bb752010-11-30 17:49:53 +0000642 if (!list_empty(sc)) {
643 new = list_entry(sc->next, struct sample_queue, list);
644 list_del(&new->list);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000645 } else if (os->sample_buffer) {
646 new = os->sample_buffer + os->sample_buffer_idx;
647 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
648 os->sample_buffer = NULL;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000649 } else {
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000650 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
651 if (!os->sample_buffer)
Thomas Gleixner020bb752010-11-30 17:49:53 +0000652 return -ENOMEM;
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000653 list_add(&os->sample_buffer->list, &os->to_free);
654 os->sample_buffer_idx = 2;
655 new = os->sample_buffer + 1;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000656 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200657
658 new->timestamp = timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000659 new->file_offset = file_offset;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000660 new->event = event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200661
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100662 __queue_event(new, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200663
664 return 0;
665}
666
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200667static void callchain__printf(struct perf_sample *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200668{
669 unsigned int i;
670
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200671 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200672
673 for (i = 0; i < sample->callchain->nr; i++)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200674 printf("..... %2d: %016" PRIx64 "\n",
675 i, sample->callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200676}
677
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200678static void perf_session__print_tstamp(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200679 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200680 struct perf_sample *sample)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200681{
682 if (event->header.type != PERF_RECORD_SAMPLE &&
683 !session->sample_id_all) {
684 fputs("-1 -1 ", stdout);
685 return;
686 }
687
688 if ((session->sample_type & PERF_SAMPLE_CPU))
689 printf("%u ", sample->cpu);
690
691 if (session->sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200692 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200693}
694
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200695static void dump_event(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200696 u64 file_offset, struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000697{
698 if (!dump_trace)
699 return;
700
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200701 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
702 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000703
704 trace_event(event);
705
706 if (sample)
707 perf_session__print_tstamp(session, event, sample);
708
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200709 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200710 event->header.size, perf_event__name(event->header.type));
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000711}
712
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200713static void dump_sample(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200714 struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000715{
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200716 if (!dump_trace)
717 return;
718
David Ahern7cec0922011-05-30 13:08:23 -0600719 printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200720 event->header.misc, sample->pid, sample->tid, sample->ip,
David Ahern7cec0922011-05-30 13:08:23 -0600721 sample->period, sample->addr);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000722
723 if (session->sample_type & PERF_SAMPLE_CALLCHAIN)
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200724 callchain__printf(sample);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000725}
726
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100727static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200728 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200729 struct perf_sample *sample,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000730 struct perf_event_ops *ops,
Thomas Gleixner532e7262010-12-07 12:48:55 +0000731 u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100732{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300733 struct perf_evsel *evsel;
734
Thomas Gleixner532e7262010-12-07 12:48:55 +0000735 dump_event(session, event, file_offset, sample);
736
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200737 evsel = perf_evlist__id2evsel(session->evlist, sample->id);
738 if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) {
739 /*
740 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
741 * because the tools right now may apply filters, discarding
742 * some of the samples. For consistency, in the future we
743 * should have something like nr_filtered_samples and remove
744 * the sample->period from total_sample_period, etc, KISS for
745 * now tho.
746 *
747 * Also testing against NULL allows us to handle files without
748 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
749 * future probably it'll be a good idea to restrict event
750 * processing via perf_session to files with both set.
751 */
752 hists__inc_nr_events(&evsel->hists, event->header.type);
753 }
754
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100755 switch (event->header.type) {
756 case PERF_RECORD_SAMPLE:
Thomas Gleixner532e7262010-12-07 12:48:55 +0000757 dump_sample(session, event, sample);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300758 if (evsel == NULL) {
759 ++session->hists.stats.nr_unknown_id;
760 return -1;
761 }
762 return ops->sample(event, sample, evsel, session);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100763 case PERF_RECORD_MMAP:
764 return ops->mmap(event, sample, session);
765 case PERF_RECORD_COMM:
766 return ops->comm(event, sample, session);
767 case PERF_RECORD_FORK:
768 return ops->fork(event, sample, session);
769 case PERF_RECORD_EXIT:
770 return ops->exit(event, sample, session);
771 case PERF_RECORD_LOST:
772 return ops->lost(event, sample, session);
773 case PERF_RECORD_READ:
774 return ops->read(event, sample, session);
775 case PERF_RECORD_THROTTLE:
776 return ops->throttle(event, sample, session);
777 case PERF_RECORD_UNTHROTTLE:
778 return ops->unthrottle(event, sample, session);
779 default:
780 ++session->hists.stats.nr_unknown_events;
781 return -1;
782 }
783}
784
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000785static int perf_session__preprocess_sample(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200786 union perf_event *event, struct perf_sample *sample)
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000787{
788 if (event->header.type != PERF_RECORD_SAMPLE ||
789 !(session->sample_type & PERF_SAMPLE_CALLCHAIN))
790 return 0;
791
792 if (!ip_callchain__valid(sample->callchain, event)) {
793 pr_debug("call-chain problem with event, skipping it.\n");
794 ++session->hists.stats.nr_invalid_chains;
795 session->hists.stats.total_invalid_chains += sample->period;
796 return -EINVAL;
797 }
798 return 0;
799}
800
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200801static int perf_session__process_user_event(struct perf_session *session, union perf_event *event,
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000802 struct perf_event_ops *ops, u64 file_offset)
803{
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200804 int err;
805
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000806 dump_event(session, event, file_offset, NULL);
807
808 /* These events are processed right away */
809 switch (event->header.type) {
810 case PERF_RECORD_HEADER_ATTR:
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200811 err = ops->attr(event, &session->evlist);
812 if (err == 0)
813 perf_session__update_sample_type(session);
814 return err;
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000815 case PERF_RECORD_HEADER_EVENT_TYPE:
816 return ops->event_type(event, session);
817 case PERF_RECORD_HEADER_TRACING_DATA:
818 /* setup for reading amidst mmap */
819 lseek(session->fd, file_offset, SEEK_SET);
820 return ops->tracing_data(event, session);
821 case PERF_RECORD_HEADER_BUILD_ID:
822 return ops->build_id(event, session);
823 case PERF_RECORD_FINISHED_ROUND:
824 return ops->finished_round(event, session, ops);
825 default:
826 return -EINVAL;
827 }
828}
829
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100830static int perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200831 union perf_event *event,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200832 struct perf_event_ops *ops,
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000833 u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200834{
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200835 struct perf_sample sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100836 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200837
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200838 if (session->header.needs_swap &&
839 perf_event__swap_ops[event->header.type])
840 perf_event__swap_ops[event->header.type](event);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200841
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000842 if (event->header.type >= PERF_RECORD_HEADER_MAX)
843 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200844
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000845 hists__inc_nr_events(&session->hists, event->header.type);
846
847 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000848 return perf_session__process_user_event(session, event, ops, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100849
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000850 /*
851 * For all kernel events we get the sample data
852 */
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200853 ret = perf_session__parse_sample(session, event, &sample);
854 if (ret)
855 return ret;
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000856
857 /* Preprocess sample records - precheck callchains */
858 if (perf_session__preprocess_sample(session, event, &sample))
859 return 0;
860
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100861 if (ops->ordered_samples) {
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000862 ret = perf_session_queue_event(session, event, &sample,
863 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100864 if (ret != -ETIME)
865 return ret;
866 }
867
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000868 return perf_session_deliver_event(session, event, &sample, ops,
869 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200870}
871
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200872void perf_event_header__bswap(struct perf_event_header *self)
873{
874 self->type = bswap_32(self->type);
875 self->misc = bswap_16(self->misc);
876 self->size = bswap_16(self->size);
877}
878
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200879struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
880{
881 return machine__findnew_thread(&session->host_machine, pid);
882}
883
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200884static struct thread *perf_session__register_idle_thread(struct perf_session *self)
885{
886 struct thread *thread = perf_session__findnew(self, 0);
887
888 if (thread == NULL || thread__set_comm(thread, "swapper")) {
889 pr_err("problem inserting idle task.\n");
890 thread = NULL;
891 }
892
893 return thread;
894}
895
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200896static void perf_session__warn_about_errors(const struct perf_session *session,
897 const struct perf_event_ops *ops)
898{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200899 if (ops->lost == perf_event__process_lost &&
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200900 session->hists.stats.nr_events[PERF_RECORD_LOST] != 0) {
901 ui__warning("Processed %d events and lost %d chunks!\n\n"
902 "Check IO/CPU overload!\n\n",
903 session->hists.stats.nr_events[0],
904 session->hists.stats.nr_events[PERF_RECORD_LOST]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200905 }
906
907 if (session->hists.stats.nr_unknown_events != 0) {
908 ui__warning("Found %u unknown events!\n\n"
909 "Is this an older tool processing a perf.data "
910 "file generated by a more recent tool?\n\n"
911 "If that is not the case, consider "
912 "reporting to linux-kernel@vger.kernel.org.\n\n",
913 session->hists.stats.nr_unknown_events);
914 }
915
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300916 if (session->hists.stats.nr_unknown_id != 0) {
917 ui__warning("%u samples with id not present in the header\n",
918 session->hists.stats.nr_unknown_id);
919 }
920
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200921 if (session->hists.stats.nr_invalid_chains != 0) {
922 ui__warning("Found invalid callchains!\n\n"
923 "%u out of %u events were discarded for this reason.\n\n"
924 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
925 session->hists.stats.nr_invalid_chains,
926 session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
927 }
928}
929
Tom Zanussi8dc58102010-04-01 23:59:15 -0500930#define session_done() (*(volatile int *)(&session_done))
931volatile int session_done;
932
933static int __perf_session__process_pipe_events(struct perf_session *self,
934 struct perf_event_ops *ops)
935{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200936 union perf_event event;
Tom Zanussi8dc58102010-04-01 23:59:15 -0500937 uint32_t size;
938 int skip = 0;
939 u64 head;
940 int err;
941 void *p;
942
943 perf_event_ops__fill_defaults(ops);
944
945 head = 0;
946more:
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200947 err = readn(self->fd, &event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -0500948 if (err <= 0) {
949 if (err == 0)
950 goto done;
951
952 pr_err("failed to read event header\n");
953 goto out_err;
954 }
955
956 if (self->header.needs_swap)
957 perf_event_header__bswap(&event.header);
958
959 size = event.header.size;
960 if (size == 0)
961 size = 8;
962
963 p = &event;
964 p += sizeof(struct perf_event_header);
965
Tom Zanussi794e43b2010-05-05 00:27:40 -0500966 if (size - sizeof(struct perf_event_header)) {
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200967 err = readn(self->fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -0500968 if (err <= 0) {
969 if (err == 0) {
970 pr_err("unexpected end of event stream\n");
971 goto done;
972 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500973
Tom Zanussi794e43b2010-05-05 00:27:40 -0500974 pr_err("failed to read event data\n");
975 goto out_err;
976 }
Tom Zanussi8dc58102010-04-01 23:59:15 -0500977 }
978
979 if (size == 0 ||
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000980 (skip = perf_session__process_event(self, &event, ops, head)) < 0) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200981 dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
Tom Zanussi8dc58102010-04-01 23:59:15 -0500982 head, event.header.size, event.header.type);
983 /*
984 * assume we lost track of the stream, check alignment, and
985 * increment a single u64 in the hope to catch on again 'soon'.
986 */
987 if (unlikely(head & 7))
988 head &= ~7ULL;
989
990 size = 8;
991 }
992
993 head += size;
994
Tom Zanussi8dc58102010-04-01 23:59:15 -0500995 if (skip > 0)
996 head += skip;
997
998 if (!session_done())
999 goto more;
1000done:
1001 err = 0;
1002out_err:
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001003 perf_session__warn_about_errors(self, ops);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001004 perf_session_free_sample_buffers(self);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001005 return err;
1006}
1007
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001008static union perf_event *
1009fetch_mmaped_event(struct perf_session *session,
1010 u64 head, size_t mmap_size, char *buf)
1011{
1012 union perf_event *event;
1013
1014 /*
1015 * Ensure we have enough space remaining to read
1016 * the size of the event in the headers.
1017 */
1018 if (head + sizeof(event->header) > mmap_size)
1019 return NULL;
1020
1021 event = (union perf_event *)(buf + head);
1022
1023 if (session->header.needs_swap)
1024 perf_event_header__bswap(&event->header);
1025
1026 if (head + event->header.size > mmap_size)
1027 return NULL;
1028
1029 return event;
1030}
1031
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001032int __perf_session__process_events(struct perf_session *session,
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001033 u64 data_offset, u64 data_size,
1034 u64 file_size, struct perf_event_ops *ops)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001035{
Thomas Gleixner55b446292010-11-30 17:49:46 +00001036 u64 head, page_offset, file_offset, file_pos, progress_next;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001037 int err, mmap_prot, mmap_flags, map_idx = 0;
Thomas Gleixner55b446292010-11-30 17:49:46 +00001038 size_t page_size, mmap_size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001039 char *buf, *mmaps[8];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001040 union perf_event *event;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001041 uint32_t size;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001042
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001043 perf_event_ops__fill_defaults(ops);
1044
Arnaldo Carvalho de Melo1b759622010-01-14 18:30:04 -02001045 page_size = sysconf(_SC_PAGESIZE);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001046
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001047 page_offset = page_size * (data_offset / page_size);
1048 file_offset = page_offset;
1049 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001050
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001051 if (data_offset + data_size < file_size)
1052 file_size = data_offset + data_size;
1053
Thomas Gleixner55b446292010-11-30 17:49:46 +00001054 progress_next = file_size / 16;
Thomas Gleixner55b446292010-11-30 17:49:46 +00001055
1056 mmap_size = session->mmap_window;
1057 if (mmap_size > file_size)
1058 mmap_size = file_size;
1059
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001060 memset(mmaps, 0, sizeof(mmaps));
1061
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001062 mmap_prot = PROT_READ;
1063 mmap_flags = MAP_SHARED;
1064
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001065 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001066 mmap_prot |= PROT_WRITE;
1067 mmap_flags = MAP_PRIVATE;
1068 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001069remap:
Thomas Gleixner55b446292010-11-30 17:49:46 +00001070 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
1071 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001072 if (buf == MAP_FAILED) {
1073 pr_err("failed to mmap file\n");
1074 err = -errno;
1075 goto out_err;
1076 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001077 mmaps[map_idx] = buf;
1078 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001079 file_pos = file_offset + head;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001080
1081more:
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001082 event = fetch_mmaped_event(session, head, mmap_size, buf);
1083 if (!event) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001084 if (mmaps[map_idx]) {
1085 munmap(mmaps[map_idx], mmap_size);
1086 mmaps[map_idx] = NULL;
1087 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001088
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001089 page_offset = page_size * (head / page_size);
1090 file_offset += page_offset;
1091 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001092 goto remap;
1093 }
1094
1095 size = event->header.size;
1096
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001097 if (size == 0 ||
1098 perf_session__process_event(session, event, ops, file_pos) < 0) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001099 dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001100 file_offset + head, event->header.size,
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001101 event->header.type);
1102 /*
1103 * assume we lost track of the stream, check alignment, and
1104 * increment a single u64 in the hope to catch on again 'soon'.
1105 */
1106 if (unlikely(head & 7))
1107 head &= ~7ULL;
1108
1109 size = 8;
1110 }
1111
1112 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001113 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001114
Thomas Gleixner55b446292010-11-30 17:49:46 +00001115 if (file_pos >= progress_next) {
1116 progress_next += file_size / 16;
Arnaldo Carvalho de Meloca59bcb2011-10-25 13:29:11 -02001117 ui_progress__update(file_pos, file_size,
1118 "Processing events...");
Thomas Gleixner55b446292010-11-30 17:49:46 +00001119 }
1120
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001121 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001122 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001123
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001124 err = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001125 /* do the final flush for ordered samples */
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001126 session->ordered_samples.next_flush = ULLONG_MAX;
1127 flush_sample_queue(session, ops);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001128out_err:
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001129 perf_session__warn_about_errors(session, ops);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001130 perf_session_free_sample_buffers(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001131 return err;
1132}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001133
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001134int perf_session__process_events(struct perf_session *self,
1135 struct perf_event_ops *ops)
1136{
1137 int err;
1138
1139 if (perf_session__register_idle_thread(self) == NULL)
1140 return -ENOMEM;
1141
Tom Zanussi8dc58102010-04-01 23:59:15 -05001142 if (!self->fd_pipe)
1143 err = __perf_session__process_events(self,
1144 self->header.data_offset,
1145 self->header.data_size,
1146 self->size, ops);
1147 else
1148 err = __perf_session__process_pipe_events(self, ops);
Dave Martin88ca8952010-07-27 11:46:12 -03001149
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001150 return err;
1151}
1152
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001153bool perf_session__has_traces(struct perf_session *self, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001154{
1155 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001156 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1157 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001158 }
1159
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001160 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001161}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001162
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001163int perf_session__set_kallsyms_ref_reloc_sym(struct map **maps,
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001164 const char *symbol_name,
1165 u64 addr)
1166{
1167 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001168 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001169 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001170
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001171 ref = zalloc(sizeof(struct ref_reloc_sym));
1172 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001173 return -ENOMEM;
1174
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001175 ref->name = strdup(symbol_name);
1176 if (ref->name == NULL) {
1177 free(ref);
1178 return -ENOMEM;
1179 }
1180
1181 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001182 if (bracket)
1183 *bracket = '\0';
1184
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001185 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001186
1187 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001188 struct kmap *kmap = map__kmap(maps[i]);
1189 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001190 }
1191
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001192 return 0;
1193}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001194
1195size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
1196{
1197 return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
1198 __dsos__fprintf(&self->host_machine.user_dsos, fp) +
1199 machines__fprintf_dsos(&self->machines, fp);
1200}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001201
1202size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
1203 bool with_hits)
1204{
1205 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
1206 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
1207}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001208
1209size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1210{
1211 struct perf_evsel *pos;
1212 size_t ret = fprintf(fp, "Aggregated stats:\n");
1213
1214 ret += hists__fprintf_nr_events(&session->hists, fp);
1215
1216 list_for_each_entry(pos, &session->evlist->entries, node) {
1217 ret += fprintf(fp, "%s stats:\n", event_name(pos));
1218 ret += hists__fprintf_nr_events(&pos->hists, fp);
1219 }
1220
1221 return ret;
1222}
David Ahernc0230b22011-03-09 22:23:27 -07001223
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001224size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1225{
1226 /*
1227 * FIXME: Here we have to actually print all the machines in this
1228 * session, not just the host...
1229 */
1230 return machine__fprintf(&session->host_machine, fp);
1231}
1232
1233void perf_session__remove_thread(struct perf_session *session,
1234 struct thread *th)
1235{
1236 /*
1237 * FIXME: This one makes no sense, we need to remove the thread from
1238 * the machine it belongs to, perf_session can have many machines, so
1239 * doing it always on ->host_machine is wrong. Fix when auditing all
1240 * the 'perf kvm' code.
1241 */
1242 machine__remove_thread(&session->host_machine, th);
1243}
1244
David Ahern9cbdb702011-04-06 21:54:20 -06001245struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1246 unsigned int type)
1247{
1248 struct perf_evsel *pos;
1249
1250 list_for_each_entry(pos, &session->evlist->entries, node) {
1251 if (pos->attr.type == type)
1252 return pos;
1253 }
1254 return NULL;
1255}
1256
Arnaldo Carvalho de Melo246d4ce2011-11-11 23:10:26 -02001257void perf_session__print_ip(union perf_event *event, struct perf_evsel *evsel,
David Ahern787bef12011-05-27 14:28:43 -06001258 struct perf_sample *sample,
1259 struct perf_session *session,
David Ahern610723f2011-05-27 14:28:44 -06001260 int print_sym, int print_dso)
David Ahernc0230b22011-03-09 22:23:27 -07001261{
1262 struct addr_location al;
1263 const char *symname, *dsoname;
Arnaldo Carvalho de Melo246d4ce2011-11-11 23:10:26 -02001264 struct callchain_cursor *cursor = &evsel->hists.callchain_cursor;
David Ahernc0230b22011-03-09 22:23:27 -07001265 struct callchain_cursor_node *node;
1266
1267 if (perf_event__preprocess_sample(event, session, &al, sample,
1268 NULL) < 0) {
1269 error("problem processing %d event, skipping it.\n",
1270 event->header.type);
1271 return;
1272 }
1273
1274 if (symbol_conf.use_callchain && sample->callchain) {
1275
Arnaldo Carvalho de Melo246d4ce2011-11-11 23:10:26 -02001276 if (perf_session__resolve_callchain(session, evsel, al.thread,
David Ahernc0230b22011-03-09 22:23:27 -07001277 sample->callchain, NULL) != 0) {
1278 if (verbose)
1279 error("Failed to resolve callchain. Skipping\n");
1280 return;
1281 }
1282 callchain_cursor_commit(cursor);
1283
1284 while (1) {
1285 node = callchain_cursor_current(cursor);
1286 if (!node)
1287 break;
1288
David Ahern787bef12011-05-27 14:28:43 -06001289 printf("\t%16" PRIx64, node->ip);
1290 if (print_sym) {
1291 if (node->sym && node->sym->name)
1292 symname = node->sym->name;
1293 else
1294 symname = "";
David Ahernc0230b22011-03-09 22:23:27 -07001295
David Ahern610723f2011-05-27 14:28:44 -06001296 printf(" %s", symname);
1297 }
1298 if (print_dso) {
David Ahern787bef12011-05-27 14:28:43 -06001299 if (node->map && node->map->dso && node->map->dso->name)
1300 dsoname = node->map->dso->name;
1301 else
1302 dsoname = "";
David Ahernc0230b22011-03-09 22:23:27 -07001303
David Ahern610723f2011-05-27 14:28:44 -06001304 printf(" (%s)", dsoname);
David Ahern787bef12011-05-27 14:28:43 -06001305 }
1306 printf("\n");
David Ahernc0230b22011-03-09 22:23:27 -07001307
1308 callchain_cursor_advance(cursor);
1309 }
1310
1311 } else {
David Ahernadc4bf92011-05-30 09:16:27 -06001312 printf("%16" PRIx64, sample->ip);
David Ahern787bef12011-05-27 14:28:43 -06001313 if (print_sym) {
1314 if (al.sym && al.sym->name)
1315 symname = al.sym->name;
1316 else
1317 symname = "";
David Ahernc0230b22011-03-09 22:23:27 -07001318
David Ahern610723f2011-05-27 14:28:44 -06001319 printf(" %s", symname);
1320 }
1321
1322 if (print_dso) {
David Ahern787bef12011-05-27 14:28:43 -06001323 if (al.map && al.map->dso && al.map->dso->name)
1324 dsoname = al.map->dso->name;
1325 else
1326 dsoname = "";
David Ahernc0230b22011-03-09 22:23:27 -07001327
David Ahern610723f2011-05-27 14:28:44 -06001328 printf(" (%s)", dsoname);
David Ahern787bef12011-05-27 14:28:43 -06001329 }
David Ahernc0230b22011-03-09 22:23:27 -07001330 }
1331}
Anton Blanchard5d67be92011-07-04 21:57:50 +10001332
1333int perf_session__cpu_bitmap(struct perf_session *session,
1334 const char *cpu_list, unsigned long *cpu_bitmap)
1335{
1336 int i;
1337 struct cpu_map *map;
1338
1339 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1340 struct perf_evsel *evsel;
1341
1342 evsel = perf_session__find_first_evtype(session, i);
1343 if (!evsel)
1344 continue;
1345
1346 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1347 pr_err("File does not contain CPU events. "
1348 "Remove -c option to proceed.\n");
1349 return -1;
1350 }
1351 }
1352
1353 map = cpu_map__new(cpu_list);
1354
1355 for (i = 0; i < map->nr; i++) {
1356 int cpu = map->map[i];
1357
1358 if (cpu >= MAX_NR_CPUS) {
1359 pr_err("Requested CPU %d too large. "
1360 "Consider raising MAX_NR_CPUS\n", cpu);
1361 return -1;
1362 }
1363
1364 set_bit(cpu, cpu_bitmap);
1365 }
1366
1367 return 0;
1368}
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001369
1370void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1371 bool full)
1372{
1373 struct stat st;
1374 int ret;
1375
1376 if (session == NULL || fp == NULL)
1377 return;
1378
1379 ret = fstat(session->fd, &st);
1380 if (ret == -1)
1381 return;
1382
1383 fprintf(fp, "# ========\n");
1384 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1385 perf_header__fprintf_info(session, fp, full);
1386 fprintf(fp, "# ========\n#\n");
1387}