blob: 046b057c8f7f437e3531181bd114bcf044055868 [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 Melo45694aa2011-11-28 08:30:20 -020013#include "tool.h"
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -020014#include "sort.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020015#include "util.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100016#include "cpumap.h"
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -030017#include "event-parse.h"
Jiri Olsa0f6a3012012-08-07 15:20:45 +020018#include "perf_regs.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020019#include "vdso.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020020
21static int perf_session__open(struct perf_session *self, bool force)
22{
23 struct stat input_stat;
24
Tom Zanussi8dc58102010-04-01 23:59:15 -050025 if (!strcmp(self->filename, "-")) {
26 self->fd_pipe = true;
27 self->fd = STDIN_FILENO;
28
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030029 if (perf_session__read_header(self, self->fd) < 0)
Stephane Eranian69996df2012-02-09 23:21:06 +010030 pr_err("incompatible file format (rerun with -v to learn more)");
Tom Zanussi8dc58102010-04-01 23:59:15 -050031
32 return 0;
33 }
34
Xiao Guangrongf887f302010-02-04 16:46:42 +080035 self->fd = open(self->filename, O_RDONLY);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020036 if (self->fd < 0) {
Andy Isaacson0f2c3de2010-06-11 20:36:15 -070037 int err = errno;
38
39 pr_err("failed to open %s: %s", self->filename, strerror(err));
40 if (err == ENOENT && !strcmp(self->filename, "perf.data"))
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020041 pr_err(" (try 'perf record' first)");
42 pr_err("\n");
43 return -errno;
44 }
45
46 if (fstat(self->fd, &input_stat) < 0)
47 goto out_close;
48
49 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
50 pr_err("file %s not owned by current user or root\n",
51 self->filename);
52 goto out_close;
53 }
54
55 if (!input_stat.st_size) {
56 pr_info("zero-sized file (%s), nothing to do!\n",
57 self->filename);
58 goto out_close;
59 }
60
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030061 if (perf_session__read_header(self, self->fd) < 0) {
Stephane Eranian69996df2012-02-09 23:21:06 +010062 pr_err("incompatible file format (rerun with -v to learn more)");
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020063 goto out_close;
64 }
65
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030066 if (!perf_evlist__valid_sample_type(self->evlist)) {
67 pr_err("non matching sample_type");
68 goto out_close;
69 }
70
71 if (!perf_evlist__valid_sample_id_all(self->evlist)) {
72 pr_err("non matching sample_id_all");
73 goto out_close;
74 }
75
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020076 self->size = input_stat.st_size;
77 return 0;
78
79out_close:
80 close(self->fd);
81 self->fd = -1;
82 return -1;
83}
84
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030085void perf_session__set_id_hdr_size(struct perf_session *session)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020086{
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030087 u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist);
88
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030089 machines__set_id_hdr_size(&session->machines, id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020090}
91
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080092int perf_session__create_kernel_maps(struct perf_session *self)
93{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -030094 int ret = machine__create_kernel_maps(&self->machines.host);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080095
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080096 if (ret >= 0)
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -030097 ret = machines__create_guest_kernel_maps(&self->machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080098 return ret;
99}
100
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300101static void perf_session__destroy_kernel_maps(struct perf_session *self)
102{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300103 machines__destroy_kernel_maps(&self->machines);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300104}
105
Ian Munsie21ef97f2010-12-10 14:09:16 +1100106struct perf_session *perf_session__new(const char *filename, int mode,
107 bool force, bool repipe,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200108 struct perf_tool *tool)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200109{
Robert Richterefad1412011-12-07 10:02:54 +0100110 struct perf_session *self;
111 struct stat st;
112 size_t len;
113
114 if (!filename || !strlen(filename)) {
115 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
116 filename = "-";
117 else
118 filename = "perf.data";
119 }
120
121 len = strlen(filename);
122 self = zalloc(sizeof(*self) + len);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200123
124 if (self == NULL)
125 goto out;
126
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200127 memcpy(self->filename, filename, len);
Tom Zanussi454c4072010-05-01 01:41:20 -0500128 self->repipe = repipe;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000129 INIT_LIST_HEAD(&self->ordered_samples.samples);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000130 INIT_LIST_HEAD(&self->ordered_samples.sample_cache);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000131 INIT_LIST_HEAD(&self->ordered_samples.to_free);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300132 machines__init(&self->machines);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200133
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200134 if (mode == O_RDONLY) {
135 if (perf_session__open(self, force) < 0)
136 goto out_delete;
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -0300137 perf_session__set_id_hdr_size(self);
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200138 } else if (mode == O_WRONLY) {
139 /*
140 * In O_RDONLY mode this will be performed when reading the
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200141 * kernel MMAP event, in perf_event__process_mmap().
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200142 */
143 if (perf_session__create_kernel_maps(self) < 0)
144 goto out_delete;
145 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200146
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200147 if (tool && tool->ordering_requires_timestamps &&
Arnaldo Carvalho de Melo5e562472012-08-01 19:25:26 -0300148 tool->ordered_samples && !perf_evlist__sample_id_all(self->evlist)) {
Ian Munsie21ef97f2010-12-10 14:09:16 +1100149 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200150 tool->ordered_samples = false;
Ian Munsie21ef97f2010-12-10 14:09:16 +1100151 }
152
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200153out:
154 return self;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200155out_delete:
156 perf_session__delete(self);
157 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200158}
159
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200160static void perf_session__delete_dead_threads(struct perf_session *session)
161{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300162 machine__delete_dead_threads(&session->machines.host);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200163}
164
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200165static void perf_session__delete_threads(struct perf_session *session)
166{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300167 machine__delete_threads(&session->machines.host);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200168}
169
Namhyung Kim03cd2092012-11-21 13:43:19 +0900170static void perf_session_env__delete(struct perf_session_env *env)
171{
172 free(env->hostname);
173 free(env->os_release);
174 free(env->version);
175 free(env->arch);
176 free(env->cpu_desc);
177 free(env->cpuid);
178
179 free(env->cmdline);
180 free(env->sibling_cores);
181 free(env->sibling_threads);
182 free(env->numa_nodes);
183 free(env->pmu_mappings);
184}
185
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200186void perf_session__delete(struct perf_session *self)
187{
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300188 perf_session__destroy_kernel_maps(self);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300189 perf_session__delete_dead_threads(self);
190 perf_session__delete_threads(self);
Namhyung Kim03cd2092012-11-21 13:43:19 +0900191 perf_session_env__delete(&self->header.env);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300192 machines__exit(&self->machines);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200193 close(self->fd);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200194 free(self);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200195 vdso__exit();
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200196}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200197
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300198static int process_event_synth_tracing_data_stub(union perf_event *event
199 __maybe_unused,
200 struct perf_session *session
201 __maybe_unused)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200202{
203 dump_printf(": unhandled!\n");
204 return 0;
205}
206
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300207static int process_event_synth_attr_stub(union perf_event *event __maybe_unused,
208 struct perf_evlist **pevlist
209 __maybe_unused)
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200210{
211 dump_printf(": unhandled!\n");
212 return 0;
213}
214
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300215static int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
216 union perf_event *event __maybe_unused,
217 struct perf_sample *sample __maybe_unused,
218 struct perf_evsel *evsel __maybe_unused,
219 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300220{
221 dump_printf(": unhandled!\n");
222 return 0;
223}
224
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300225static int process_event_stub(struct perf_tool *tool __maybe_unused,
226 union perf_event *event __maybe_unused,
227 struct perf_sample *sample __maybe_unused,
228 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200229{
230 dump_printf(": unhandled!\n");
231 return 0;
232}
233
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300234static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
235 union perf_event *event __maybe_unused,
236 struct perf_session *perf_session
237 __maybe_unused)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200238{
239 dump_printf(": unhandled!\n");
240 return 0;
241}
242
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300243static int process_event_type_stub(struct perf_tool *tool __maybe_unused,
244 union perf_event *event __maybe_unused)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200245{
246 dump_printf(": unhandled!\n");
247 return 0;
248}
249
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200250static int process_finished_round(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200251 union perf_event *event,
252 struct perf_session *session);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200253
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200254static void perf_tool__fill_defaults(struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200255{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200256 if (tool->sample == NULL)
257 tool->sample = process_event_sample_stub;
258 if (tool->mmap == NULL)
259 tool->mmap = process_event_stub;
260 if (tool->comm == NULL)
261 tool->comm = process_event_stub;
262 if (tool->fork == NULL)
263 tool->fork = process_event_stub;
264 if (tool->exit == NULL)
265 tool->exit = process_event_stub;
266 if (tool->lost == NULL)
267 tool->lost = perf_event__process_lost;
268 if (tool->read == NULL)
269 tool->read = process_event_sample_stub;
270 if (tool->throttle == NULL)
271 tool->throttle = process_event_stub;
272 if (tool->unthrottle == NULL)
273 tool->unthrottle = process_event_stub;
274 if (tool->attr == NULL)
275 tool->attr = process_event_synth_attr_stub;
276 if (tool->event_type == NULL)
277 tool->event_type = process_event_type_stub;
278 if (tool->tracing_data == NULL)
279 tool->tracing_data = process_event_synth_tracing_data_stub;
280 if (tool->build_id == NULL)
281 tool->build_id = process_finished_round_stub;
282 if (tool->finished_round == NULL) {
283 if (tool->ordered_samples)
284 tool->finished_round = process_finished_round;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200285 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200286 tool->finished_round = process_finished_round_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200287 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200288}
David Ahern80c01202012-06-08 11:47:51 -0300289
290void mem_bswap_32(void *src, int byte_size)
291{
292 u32 *m = src;
293 while (byte_size > 0) {
294 *m = bswap_32(*m);
295 byte_size -= sizeof(u32);
296 ++m;
297 }
298}
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200299
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200300void mem_bswap_64(void *src, int byte_size)
301{
302 u64 *m = src;
303
304 while (byte_size > 0) {
305 *m = bswap_64(*m);
306 byte_size -= sizeof(u64);
307 ++m;
308 }
309}
310
Jiri Olsa268fb202012-05-30 14:23:43 +0200311static void swap_sample_id_all(union perf_event *event, void *data)
312{
313 void *end = (void *) event + event->header.size;
314 int size = end - data;
315
316 BUG_ON(size % sizeof(u64));
317 mem_bswap_64(data, size);
318}
319
320static void perf_event__all64_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300321 bool sample_id_all __maybe_unused)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200322{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200323 struct perf_event_header *hdr = &event->header;
324 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200325}
326
Jiri Olsa268fb202012-05-30 14:23:43 +0200327static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200328{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200329 event->comm.pid = bswap_32(event->comm.pid);
330 event->comm.tid = bswap_32(event->comm.tid);
Jiri Olsa268fb202012-05-30 14:23:43 +0200331
332 if (sample_id_all) {
333 void *data = &event->comm.comm;
334
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300335 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200336 swap_sample_id_all(event, data);
337 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200338}
339
Jiri Olsa268fb202012-05-30 14:23:43 +0200340static void perf_event__mmap_swap(union perf_event *event,
341 bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200342{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200343 event->mmap.pid = bswap_32(event->mmap.pid);
344 event->mmap.tid = bswap_32(event->mmap.tid);
345 event->mmap.start = bswap_64(event->mmap.start);
346 event->mmap.len = bswap_64(event->mmap.len);
347 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
Jiri Olsa268fb202012-05-30 14:23:43 +0200348
349 if (sample_id_all) {
350 void *data = &event->mmap.filename;
351
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300352 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200353 swap_sample_id_all(event, data);
354 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200355}
356
Jiri Olsa268fb202012-05-30 14:23:43 +0200357static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200358{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200359 event->fork.pid = bswap_32(event->fork.pid);
360 event->fork.tid = bswap_32(event->fork.tid);
361 event->fork.ppid = bswap_32(event->fork.ppid);
362 event->fork.ptid = bswap_32(event->fork.ptid);
363 event->fork.time = bswap_64(event->fork.time);
Jiri Olsa268fb202012-05-30 14:23:43 +0200364
365 if (sample_id_all)
366 swap_sample_id_all(event, &event->fork + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200367}
368
Jiri Olsa268fb202012-05-30 14:23:43 +0200369static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200370{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200371 event->read.pid = bswap_32(event->read.pid);
372 event->read.tid = bswap_32(event->read.tid);
373 event->read.value = bswap_64(event->read.value);
374 event->read.time_enabled = bswap_64(event->read.time_enabled);
375 event->read.time_running = bswap_64(event->read.time_running);
376 event->read.id = bswap_64(event->read.id);
Jiri Olsa268fb202012-05-30 14:23:43 +0200377
378 if (sample_id_all)
379 swap_sample_id_all(event, &event->read + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200380}
381
Jiri Olsae108c662012-05-16 08:59:03 +0200382static u8 revbyte(u8 b)
383{
384 int rev = (b >> 4) | ((b & 0xf) << 4);
385 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
386 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
387 return (u8) rev;
388}
389
390/*
391 * XXX this is hack in attempt to carry flags bitfield
392 * throught endian village. ABI says:
393 *
394 * Bit-fields are allocated from right to left (least to most significant)
395 * on little-endian implementations and from left to right (most to least
396 * significant) on big-endian implementations.
397 *
398 * The above seems to be byte specific, so we need to reverse each
399 * byte of the bitfield. 'Internet' also says this might be implementation
400 * specific and we probably need proper fix and carry perf_event_attr
401 * bitfield flags in separate data file FEAT_ section. Thought this seems
402 * to work for now.
403 */
404static void swap_bitfield(u8 *p, unsigned len)
405{
406 unsigned i;
407
408 for (i = 0; i < len; i++) {
409 *p = revbyte(*p);
410 p++;
411 }
412}
413
David Aherneda39132011-07-15 12:34:09 -0600414/* exported for swapping attributes in file header */
415void perf_event__attr_swap(struct perf_event_attr *attr)
416{
417 attr->type = bswap_32(attr->type);
418 attr->size = bswap_32(attr->size);
419 attr->config = bswap_64(attr->config);
420 attr->sample_period = bswap_64(attr->sample_period);
421 attr->sample_type = bswap_64(attr->sample_type);
422 attr->read_format = bswap_64(attr->read_format);
423 attr->wakeup_events = bswap_32(attr->wakeup_events);
424 attr->bp_type = bswap_32(attr->bp_type);
425 attr->bp_addr = bswap_64(attr->bp_addr);
426 attr->bp_len = bswap_64(attr->bp_len);
Jiri Olsae108c662012-05-16 08:59:03 +0200427
428 swap_bitfield((u8 *) (&attr->read_format + 1), sizeof(u64));
David Aherneda39132011-07-15 12:34:09 -0600429}
430
Jiri Olsa268fb202012-05-30 14:23:43 +0200431static void perf_event__hdr_attr_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300432 bool sample_id_all __maybe_unused)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500433{
434 size_t size;
435
David Aherneda39132011-07-15 12:34:09 -0600436 perf_event__attr_swap(&event->attr.attr);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500437
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200438 size = event->header.size;
439 size -= (void *)&event->attr.id - (void *)event;
440 mem_bswap_64(event->attr.id, size);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500441}
442
Jiri Olsa268fb202012-05-30 14:23:43 +0200443static void perf_event__event_type_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300444 bool sample_id_all __maybe_unused)
Tom Zanussicd19a032010-04-01 23:59:20 -0500445{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200446 event->event_type.event_type.event_id =
447 bswap_64(event->event_type.event_type.event_id);
Tom Zanussicd19a032010-04-01 23:59:20 -0500448}
449
Jiri Olsa268fb202012-05-30 14:23:43 +0200450static void perf_event__tracing_data_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300451 bool sample_id_all __maybe_unused)
Tom Zanussi92155452010-04-01 23:59:21 -0500452{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200453 event->tracing_data.size = bswap_32(event->tracing_data.size);
Tom Zanussi92155452010-04-01 23:59:21 -0500454}
455
Jiri Olsa268fb202012-05-30 14:23:43 +0200456typedef void (*perf_event__swap_op)(union perf_event *event,
457 bool sample_id_all);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200458
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200459static perf_event__swap_op perf_event__swap_ops[] = {
460 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
461 [PERF_RECORD_COMM] = perf_event__comm_swap,
462 [PERF_RECORD_FORK] = perf_event__task_swap,
463 [PERF_RECORD_EXIT] = perf_event__task_swap,
464 [PERF_RECORD_LOST] = perf_event__all64_swap,
465 [PERF_RECORD_READ] = perf_event__read_swap,
466 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
David Aherneda39132011-07-15 12:34:09 -0600467 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200468 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
469 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
470 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
471 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200472};
473
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200474struct sample_queue {
475 u64 timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000476 u64 file_offset;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200477 union perf_event *event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200478 struct list_head list;
479};
480
Thomas Gleixner020bb752010-11-30 17:49:53 +0000481static void perf_session_free_sample_buffers(struct perf_session *session)
482{
483 struct ordered_samples *os = &session->ordered_samples;
484
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000485 while (!list_empty(&os->to_free)) {
Thomas Gleixner020bb752010-11-30 17:49:53 +0000486 struct sample_queue *sq;
487
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000488 sq = list_entry(os->to_free.next, struct sample_queue, list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000489 list_del(&sq->list);
490 free(sq);
491 }
492}
493
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100494static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200495 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200496 struct perf_sample *sample,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200497 struct perf_tool *tool,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000498 u64 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100499
David Ahernd25380cd2012-08-26 12:24:41 -0600500static int flush_sample_queue(struct perf_session *s,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200501 struct perf_tool *tool)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200502{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000503 struct ordered_samples *os = &s->ordered_samples;
504 struct list_head *head = &os->samples;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200505 struct sample_queue *tmp, *iter;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200506 struct perf_sample sample;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000507 u64 limit = os->next_flush;
508 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200509 unsigned idx = 0, progress_next = os->nr_samples / 16;
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200510 int ret;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200511
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200512 if (!tool->ordered_samples || !limit)
David Ahernd25380cd2012-08-26 12:24:41 -0600513 return 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200514
515 list_for_each_entry_safe(iter, tmp, head, list) {
516 if (iter->timestamp > limit)
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000517 break;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200518
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300519 ret = perf_evlist__parse_sample(s->evlist, iter->event, &sample);
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200520 if (ret)
521 pr_err("Can't parse sample, err = %d\n", ret);
David Ahernd25380cd2012-08-26 12:24:41 -0600522 else {
523 ret = perf_session_deliver_event(s, iter->event, &sample, tool,
524 iter->file_offset);
525 if (ret)
526 return ret;
527 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200528
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000529 os->last_flush = iter->timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200530 list_del(&iter->list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000531 list_add(&iter->list, &os->sample_cache);
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200532 if (++idx >= progress_next) {
533 progress_next += os->nr_samples / 16;
534 ui_progress__update(idx, os->nr_samples,
535 "Processing time ordered events...");
536 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200537 }
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000538
539 if (list_empty(head)) {
540 os->last_sample = NULL;
541 } else if (last_ts <= limit) {
542 os->last_sample =
543 list_entry(head->prev, struct sample_queue, list);
544 }
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200545
546 os->nr_samples = 0;
David Ahernd25380cd2012-08-26 12:24:41 -0600547
548 return 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200549}
550
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200551/*
552 * When perf record finishes a pass on every buffers, it records this pseudo
553 * event.
554 * We record the max timestamp t found in the pass n.
555 * Assuming these timestamps are monotonic across cpus, we know that if
556 * a buffer still has events with timestamps below t, they will be all
557 * available and then read in the pass n + 1.
558 * Hence when we start to read the pass n + 2, we can safely flush every
559 * events with timestamps below t.
560 *
561 * ============ PASS n =================
562 * CPU 0 | CPU 1
563 * |
564 * cnt1 timestamps | cnt2 timestamps
565 * 1 | 2
566 * 2 | 3
567 * - | 4 <--- max recorded
568 *
569 * ============ PASS n + 1 ==============
570 * CPU 0 | CPU 1
571 * |
572 * cnt1 timestamps | cnt2 timestamps
573 * 3 | 5
574 * 4 | 6
575 * 5 | 7 <---- max recorded
576 *
577 * Flush every events below timestamp 4
578 *
579 * ============ PASS n + 2 ==============
580 * CPU 0 | CPU 1
581 * |
582 * cnt1 timestamps | cnt2 timestamps
583 * 6 | 8
584 * 7 | 9
585 * - | 10
586 *
587 * Flush every events below timestamp 7
588 * etc...
589 */
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200590static int process_finished_round(struct perf_tool *tool,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300591 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200592 struct perf_session *session)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200593{
David Ahernd25380cd2012-08-26 12:24:41 -0600594 int ret = flush_sample_queue(session, tool);
595 if (!ret)
596 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200597
David Ahernd25380cd2012-08-26 12:24:41 -0600598 return ret;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200599}
600
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200601/* The queue is ordered by time */
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100602static void __queue_event(struct sample_queue *new, struct perf_session *s)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200603{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000604 struct ordered_samples *os = &s->ordered_samples;
605 struct sample_queue *sample = os->last_sample;
606 u64 timestamp = new->timestamp;
607 struct list_head *p;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200608
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200609 ++os->nr_samples;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000610 os->last_sample = new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200611
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000612 if (!sample) {
613 list_add(&new->list, &os->samples);
614 os->max_timestamp = timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200615 return;
616 }
617
618 /*
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000619 * last_sample might point to some random place in the list as it's
620 * the last queued event. We expect that the new event is close to
621 * this.
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200622 */
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000623 if (sample->timestamp <= timestamp) {
624 while (sample->timestamp <= timestamp) {
625 p = sample->list.next;
626 if (p == &os->samples) {
627 list_add_tail(&new->list, &os->samples);
628 os->max_timestamp = timestamp;
629 return;
630 }
631 sample = list_entry(p, struct sample_queue, list);
632 }
633 list_add_tail(&new->list, &sample->list);
634 } else {
635 while (sample->timestamp > timestamp) {
636 p = sample->list.prev;
637 if (p == &os->samples) {
638 list_add(&new->list, &os->samples);
639 return;
640 }
641 sample = list_entry(p, struct sample_queue, list);
642 }
643 list_add(&new->list, &sample->list);
644 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200645}
646
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000647#define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
648
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200649static int perf_session_queue_event(struct perf_session *s, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200650 struct perf_sample *sample, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200651{
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000652 struct ordered_samples *os = &s->ordered_samples;
653 struct list_head *sc = &os->sample_cache;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200654 u64 timestamp = sample->time;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200655 struct sample_queue *new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200656
Thomas Gleixner79a14c12010-12-07 12:48:44 +0000657 if (!timestamp || timestamp == ~0ULL)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100658 return -ETIME;
659
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200660 if (timestamp < s->ordered_samples.last_flush) {
661 printf("Warning: Timestamp below last timeslice flush\n");
662 return -EINVAL;
663 }
664
Thomas Gleixner020bb752010-11-30 17:49:53 +0000665 if (!list_empty(sc)) {
666 new = list_entry(sc->next, struct sample_queue, list);
667 list_del(&new->list);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000668 } else if (os->sample_buffer) {
669 new = os->sample_buffer + os->sample_buffer_idx;
670 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
671 os->sample_buffer = NULL;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000672 } else {
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000673 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
674 if (!os->sample_buffer)
Thomas Gleixner020bb752010-11-30 17:49:53 +0000675 return -ENOMEM;
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000676 list_add(&os->sample_buffer->list, &os->to_free);
677 os->sample_buffer_idx = 2;
678 new = os->sample_buffer + 1;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000679 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200680
681 new->timestamp = timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000682 new->file_offset = file_offset;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000683 new->event = event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200684
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100685 __queue_event(new, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200686
687 return 0;
688}
689
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200690static void callchain__printf(struct perf_sample *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200691{
692 unsigned int i;
693
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200694 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200695
696 for (i = 0; i < sample->callchain->nr; i++)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200697 printf("..... %2d: %016" PRIx64 "\n",
698 i, sample->callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200699}
700
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100701static void branch_stack__printf(struct perf_sample *sample)
702{
703 uint64_t i;
704
705 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
706
707 for (i = 0; i < sample->branch_stack->nr; i++)
708 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n",
709 i, sample->branch_stack->entries[i].from,
710 sample->branch_stack->entries[i].to);
711}
712
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200713static void regs_dump__printf(u64 mask, u64 *regs)
714{
715 unsigned rid, i = 0;
716
717 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
718 u64 val = regs[i++];
719
720 printf(".... %-5s 0x%" PRIx64 "\n",
721 perf_reg_name(rid), val);
722 }
723}
724
725static void regs_user__printf(struct perf_sample *sample, u64 mask)
726{
727 struct regs_dump *user_regs = &sample->user_regs;
728
729 if (user_regs->regs) {
730 printf("... user regs: mask 0x%" PRIx64 "\n", mask);
731 regs_dump__printf(mask, user_regs->regs);
732 }
733}
734
735static void stack_user__printf(struct stack_dump *dump)
736{
737 printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
738 dump->size, dump->offset);
739}
740
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200741static void perf_session__print_tstamp(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200742 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200743 struct perf_sample *sample)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200744{
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300745 u64 sample_type = perf_evlist__sample_type(session->evlist);
746
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200747 if (event->header.type != PERF_RECORD_SAMPLE &&
Arnaldo Carvalho de Melo5e562472012-08-01 19:25:26 -0300748 !perf_evlist__sample_id_all(session->evlist)) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200749 fputs("-1 -1 ", stdout);
750 return;
751 }
752
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300753 if ((sample_type & PERF_SAMPLE_CPU))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200754 printf("%u ", sample->cpu);
755
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300756 if (sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200757 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200758}
759
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200760static void dump_event(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200761 u64 file_offset, struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000762{
763 if (!dump_trace)
764 return;
765
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200766 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
767 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000768
769 trace_event(event);
770
771 if (sample)
772 perf_session__print_tstamp(session, event, sample);
773
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200774 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200775 event->header.size, perf_event__name(event->header.type));
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000776}
777
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200778static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200779 struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000780{
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300781 u64 sample_type;
782
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200783 if (!dump_trace)
784 return;
785
David Ahern7cec0922011-05-30 13:08:23 -0600786 printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200787 event->header.misc, sample->pid, sample->tid, sample->ip,
David Ahern7cec0922011-05-30 13:08:23 -0600788 sample->period, sample->addr);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000789
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200790 sample_type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300791
792 if (sample_type & PERF_SAMPLE_CALLCHAIN)
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200793 callchain__printf(sample);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100794
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300795 if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100796 branch_stack__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200797
798 if (sample_type & PERF_SAMPLE_REGS_USER)
799 regs_user__printf(sample, evsel->attr.sample_regs_user);
800
801 if (sample_type & PERF_SAMPLE_STACK_USER)
802 stack_user__printf(&sample->user_stack);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000803}
804
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200805static struct machine *
806 perf_session__find_machine_for_cpumode(struct perf_session *session,
807 union perf_event *event)
808{
809 const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
810
David Ahern7c0f4a42012-07-20 17:25:48 -0600811 if (perf_guest &&
812 ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
813 (cpumode == PERF_RECORD_MISC_GUEST_USER))) {
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530814 u32 pid;
815
816 if (event->header.type == PERF_RECORD_MMAP)
817 pid = event->mmap.pid;
818 else
819 pid = event->ip.pid;
820
David Ahern207b5792012-07-01 16:11:37 -0600821 return perf_session__findnew_machine(session, pid);
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530822 }
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200823
824 return perf_session__find_host_machine(session);
825}
826
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100827static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200828 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200829 struct perf_sample *sample,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200830 struct perf_tool *tool,
Thomas Gleixner532e7262010-12-07 12:48:55 +0000831 u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100832{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300833 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200834 struct machine *machine;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300835
Thomas Gleixner532e7262010-12-07 12:48:55 +0000836 dump_event(session, event, file_offset, sample);
837
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200838 evsel = perf_evlist__id2evsel(session->evlist, sample->id);
839 if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) {
840 /*
841 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
842 * because the tools right now may apply filters, discarding
843 * some of the samples. For consistency, in the future we
844 * should have something like nr_filtered_samples and remove
845 * the sample->period from total_sample_period, etc, KISS for
846 * now tho.
847 *
848 * Also testing against NULL allows us to handle files without
849 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
850 * future probably it'll be a good idea to restrict event
851 * processing via perf_session to files with both set.
852 */
853 hists__inc_nr_events(&evsel->hists, event->header.type);
854 }
855
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200856 machine = perf_session__find_machine_for_cpumode(session, event);
857
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100858 switch (event->header.type) {
859 case PERF_RECORD_SAMPLE:
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200860 dump_sample(evsel, event, sample);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300861 if (evsel == NULL) {
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300862 ++session->stats.nr_unknown_id;
Jiri Olsa67822062012-04-12 14:21:01 +0200863 return 0;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300864 }
Joerg Roedel0c095712012-02-10 18:05:04 +0100865 if (machine == NULL) {
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300866 ++session->stats.nr_unprocessable_samples;
Jiri Olsa67822062012-04-12 14:21:01 +0200867 return 0;
Joerg Roedel0c095712012-02-10 18:05:04 +0100868 }
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200869 return tool->sample(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100870 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200871 return tool->mmap(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100872 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200873 return tool->comm(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100874 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200875 return tool->fork(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100876 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200877 return tool->exit(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100878 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200879 if (tool->lost == perf_event__process_lost)
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300880 session->stats.total_lost += event->lost.lost;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200881 return tool->lost(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100882 case PERF_RECORD_READ:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200883 return tool->read(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100884 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200885 return tool->throttle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100886 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200887 return tool->unthrottle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100888 default:
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300889 ++session->stats.nr_unknown_events;
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100890 return -1;
891 }
892}
893
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000894static int perf_session__preprocess_sample(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200895 union perf_event *event, struct perf_sample *sample)
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000896{
897 if (event->header.type != PERF_RECORD_SAMPLE ||
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300898 !(perf_evlist__sample_type(session->evlist) & PERF_SAMPLE_CALLCHAIN))
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000899 return 0;
900
901 if (!ip_callchain__valid(sample->callchain, event)) {
902 pr_debug("call-chain problem with event, skipping it.\n");
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300903 ++session->stats.nr_invalid_chains;
904 session->stats.total_invalid_chains += sample->period;
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000905 return -EINVAL;
906 }
907 return 0;
908}
909
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200910static int perf_session__process_user_event(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200911 struct perf_tool *tool, u64 file_offset)
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000912{
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200913 int err;
914
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000915 dump_event(session, event, file_offset, NULL);
916
917 /* These events are processed right away */
918 switch (event->header.type) {
919 case PERF_RECORD_HEADER_ATTR:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200920 err = tool->attr(event, &session->evlist);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200921 if (err == 0)
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -0300922 perf_session__set_id_hdr_size(session);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200923 return err;
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000924 case PERF_RECORD_HEADER_EVENT_TYPE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200925 return tool->event_type(tool, event);
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000926 case PERF_RECORD_HEADER_TRACING_DATA:
927 /* setup for reading amidst mmap */
928 lseek(session->fd, file_offset, SEEK_SET);
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200929 return tool->tracing_data(event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000930 case PERF_RECORD_HEADER_BUILD_ID:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200931 return tool->build_id(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000932 case PERF_RECORD_FINISHED_ROUND:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200933 return tool->finished_round(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000934 default:
935 return -EINVAL;
936 }
937}
938
Jiri Olsa268fb202012-05-30 14:23:43 +0200939static void event_swap(union perf_event *event, bool sample_id_all)
940{
941 perf_event__swap_op swap;
942
943 swap = perf_event__swap_ops[event->header.type];
944 if (swap)
945 swap(event, sample_id_all);
946}
947
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100948static int perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200949 union perf_event *event,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200950 struct perf_tool *tool,
Thomas Gleixner0331ee02010-11-30 17:49:38 +0000951 u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200952{
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200953 struct perf_sample sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100954 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200955
Jiri Olsa268fb202012-05-30 14:23:43 +0200956 if (session->header.needs_swap)
Arnaldo Carvalho de Melo5e562472012-08-01 19:25:26 -0300957 event_swap(event, perf_evlist__sample_id_all(session->evlist));
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200958
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000959 if (event->header.type >= PERF_RECORD_HEADER_MAX)
960 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200961
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300962 events_stats__inc(&session->stats, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000963
964 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200965 return perf_session__process_user_event(session, event, tool, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100966
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000967 /*
968 * For all kernel events we get the sample data
969 */
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300970 ret = perf_evlist__parse_sample(session->evlist, event, &sample);
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200971 if (ret)
972 return ret;
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000973
974 /* Preprocess sample records - precheck callchains */
975 if (perf_session__preprocess_sample(session, event, &sample))
976 return 0;
977
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200978 if (tool->ordered_samples) {
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000979 ret = perf_session_queue_event(session, event, &sample,
980 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100981 if (ret != -ETIME)
982 return ret;
983 }
984
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200985 return perf_session_deliver_event(session, event, &sample, tool,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000986 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200987}
988
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200989void perf_event_header__bswap(struct perf_event_header *self)
990{
991 self->type = bswap_32(self->type);
992 self->misc = bswap_16(self->misc);
993 self->size = bswap_16(self->size);
994}
995
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200996struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
997{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300998 return machine__findnew_thread(&session->machines.host, pid);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200999}
1000
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001001static struct thread *perf_session__register_idle_thread(struct perf_session *self)
1002{
1003 struct thread *thread = perf_session__findnew(self, 0);
1004
1005 if (thread == NULL || thread__set_comm(thread, "swapper")) {
1006 pr_err("problem inserting idle task.\n");
1007 thread = NULL;
1008 }
1009
1010 return thread;
1011}
1012
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001013static void perf_session__warn_about_errors(const struct perf_session *session,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001014 const struct perf_tool *tool)
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001015{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001016 if (tool->lost == perf_event__process_lost &&
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001017 session->stats.nr_events[PERF_RECORD_LOST] != 0) {
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001018 ui__warning("Processed %d events and lost %d chunks!\n\n"
1019 "Check IO/CPU overload!\n\n",
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001020 session->stats.nr_events[0],
1021 session->stats.nr_events[PERF_RECORD_LOST]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001022 }
1023
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001024 if (session->stats.nr_unknown_events != 0) {
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001025 ui__warning("Found %u unknown events!\n\n"
1026 "Is this an older tool processing a perf.data "
1027 "file generated by a more recent tool?\n\n"
1028 "If that is not the case, consider "
1029 "reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001030 session->stats.nr_unknown_events);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001031 }
1032
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001033 if (session->stats.nr_unknown_id != 0) {
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001034 ui__warning("%u samples with id not present in the header\n",
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001035 session->stats.nr_unknown_id);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001036 }
1037
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001038 if (session->stats.nr_invalid_chains != 0) {
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001039 ui__warning("Found invalid callchains!\n\n"
1040 "%u out of %u events were discarded for this reason.\n\n"
1041 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001042 session->stats.nr_invalid_chains,
1043 session->stats.nr_events[PERF_RECORD_SAMPLE]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001044 }
Joerg Roedel0c095712012-02-10 18:05:04 +01001045
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001046 if (session->stats.nr_unprocessable_samples != 0) {
Joerg Roedel0c095712012-02-10 18:05:04 +01001047 ui__warning("%u unprocessable samples recorded.\n"
1048 "Do you have a KVM guest running and not using 'perf kvm'?\n",
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001049 session->stats.nr_unprocessable_samples);
Joerg Roedel0c095712012-02-10 18:05:04 +01001050 }
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001051}
1052
Tom Zanussi8dc58102010-04-01 23:59:15 -05001053#define session_done() (*(volatile int *)(&session_done))
1054volatile int session_done;
1055
1056static int __perf_session__process_pipe_events(struct perf_session *self,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001057 struct perf_tool *tool)
Tom Zanussi8dc58102010-04-01 23:59:15 -05001058{
Stephane Eranian444d2862012-05-15 13:28:12 +02001059 union perf_event *event;
1060 uint32_t size, cur_size = 0;
1061 void *buf = NULL;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001062 int skip = 0;
1063 u64 head;
1064 int err;
1065 void *p;
1066
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001067 perf_tool__fill_defaults(tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001068
1069 head = 0;
Stephane Eranian444d2862012-05-15 13:28:12 +02001070 cur_size = sizeof(union perf_event);
1071
1072 buf = malloc(cur_size);
1073 if (!buf)
1074 return -errno;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001075more:
Stephane Eranian444d2862012-05-15 13:28:12 +02001076 event = buf;
1077 err = readn(self->fd, event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05001078 if (err <= 0) {
1079 if (err == 0)
1080 goto done;
1081
1082 pr_err("failed to read event header\n");
1083 goto out_err;
1084 }
1085
1086 if (self->header.needs_swap)
Stephane Eranian444d2862012-05-15 13:28:12 +02001087 perf_event_header__bswap(&event->header);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001088
Stephane Eranian444d2862012-05-15 13:28:12 +02001089 size = event->header.size;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001090 if (size == 0)
1091 size = 8;
1092
Stephane Eranian444d2862012-05-15 13:28:12 +02001093 if (size > cur_size) {
1094 void *new = realloc(buf, size);
1095 if (!new) {
1096 pr_err("failed to allocate memory to read event\n");
1097 goto out_err;
1098 }
1099 buf = new;
1100 cur_size = size;
1101 event = buf;
1102 }
1103 p = event;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001104 p += sizeof(struct perf_event_header);
1105
Tom Zanussi794e43b2010-05-05 00:27:40 -05001106 if (size - sizeof(struct perf_event_header)) {
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02001107 err = readn(self->fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -05001108 if (err <= 0) {
1109 if (err == 0) {
1110 pr_err("unexpected end of event stream\n");
1111 goto done;
1112 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001113
Tom Zanussi794e43b2010-05-05 00:27:40 -05001114 pr_err("failed to read event data\n");
1115 goto out_err;
1116 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001117 }
1118
Stephane Eranian444d2862012-05-15 13:28:12 +02001119 if ((skip = perf_session__process_event(self, event, tool, head)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001120 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
Stephane Eranian444d2862012-05-15 13:28:12 +02001121 head, event->header.size, event->header.type);
Jiri Olsa9389a462012-04-16 20:42:51 +02001122 err = -EINVAL;
1123 goto out_err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001124 }
1125
1126 head += size;
1127
Tom Zanussi8dc58102010-04-01 23:59:15 -05001128 if (skip > 0)
1129 head += skip;
1130
1131 if (!session_done())
1132 goto more;
1133done:
1134 err = 0;
1135out_err:
Stephane Eranian444d2862012-05-15 13:28:12 +02001136 free(buf);
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001137 perf_session__warn_about_errors(self, tool);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001138 perf_session_free_sample_buffers(self);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001139 return err;
1140}
1141
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001142static union perf_event *
1143fetch_mmaped_event(struct perf_session *session,
1144 u64 head, size_t mmap_size, char *buf)
1145{
1146 union perf_event *event;
1147
1148 /*
1149 * Ensure we have enough space remaining to read
1150 * the size of the event in the headers.
1151 */
1152 if (head + sizeof(event->header) > mmap_size)
1153 return NULL;
1154
1155 event = (union perf_event *)(buf + head);
1156
1157 if (session->header.needs_swap)
1158 perf_event_header__bswap(&event->header);
1159
1160 if (head + event->header.size > mmap_size)
1161 return NULL;
1162
1163 return event;
1164}
1165
David Miller35d48dd2012-11-10 14:12:19 -05001166/*
1167 * On 64bit we can mmap the data file in one go. No need for tiny mmap
1168 * slices. On 32bit we use 32MB.
1169 */
1170#if BITS_PER_LONG == 64
1171#define MMAP_SIZE ULLONG_MAX
1172#define NUM_MMAPS 1
1173#else
1174#define MMAP_SIZE (32 * 1024 * 1024ULL)
1175#define NUM_MMAPS 128
1176#endif
1177
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001178int __perf_session__process_events(struct perf_session *session,
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001179 u64 data_offset, u64 data_size,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001180 u64 file_size, struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001181{
Thomas Gleixner55b446292010-11-30 17:49:46 +00001182 u64 head, page_offset, file_offset, file_pos, progress_next;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001183 int err, mmap_prot, mmap_flags, map_idx = 0;
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -03001184 size_t mmap_size;
David Miller35d48dd2012-11-10 14:12:19 -05001185 char *buf, *mmaps[NUM_MMAPS];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001186 union perf_event *event;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001187 uint32_t size;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001188
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001189 perf_tool__fill_defaults(tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001190
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001191 page_offset = page_size * (data_offset / page_size);
1192 file_offset = page_offset;
1193 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001194
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001195 if (data_offset + data_size < file_size)
1196 file_size = data_offset + data_size;
1197
Thomas Gleixner55b446292010-11-30 17:49:46 +00001198 progress_next = file_size / 16;
Thomas Gleixner55b446292010-11-30 17:49:46 +00001199
David Miller35d48dd2012-11-10 14:12:19 -05001200 mmap_size = MMAP_SIZE;
Thomas Gleixner55b446292010-11-30 17:49:46 +00001201 if (mmap_size > file_size)
1202 mmap_size = file_size;
1203
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001204 memset(mmaps, 0, sizeof(mmaps));
1205
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001206 mmap_prot = PROT_READ;
1207 mmap_flags = MAP_SHARED;
1208
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001209 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001210 mmap_prot |= PROT_WRITE;
1211 mmap_flags = MAP_PRIVATE;
1212 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001213remap:
Thomas Gleixner55b446292010-11-30 17:49:46 +00001214 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
1215 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001216 if (buf == MAP_FAILED) {
1217 pr_err("failed to mmap file\n");
1218 err = -errno;
1219 goto out_err;
1220 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001221 mmaps[map_idx] = buf;
1222 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001223 file_pos = file_offset + head;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001224
1225more:
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001226 event = fetch_mmaped_event(session, head, mmap_size, buf);
1227 if (!event) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001228 if (mmaps[map_idx]) {
1229 munmap(mmaps[map_idx], mmap_size);
1230 mmaps[map_idx] = NULL;
1231 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001232
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001233 page_offset = page_size * (head / page_size);
1234 file_offset += page_offset;
1235 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001236 goto remap;
1237 }
1238
1239 size = event->header.size;
1240
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001241 if (size == 0 ||
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001242 perf_session__process_event(session, event, tool, file_pos) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001243 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1244 file_offset + head, event->header.size,
1245 event->header.type);
1246 err = -EINVAL;
1247 goto out_err;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001248 }
1249
1250 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001251 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001252
Thomas Gleixner55b446292010-11-30 17:49:46 +00001253 if (file_pos >= progress_next) {
1254 progress_next += file_size / 16;
Arnaldo Carvalho de Meloca59bcb2011-10-25 13:29:11 -02001255 ui_progress__update(file_pos, file_size,
1256 "Processing events...");
Thomas Gleixner55b446292010-11-30 17:49:46 +00001257 }
1258
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001259 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001260 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001261
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001262 err = 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001263 /* do the final flush for ordered samples */
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001264 session->ordered_samples.next_flush = ULLONG_MAX;
David Ahernd25380cd2012-08-26 12:24:41 -06001265 err = flush_sample_queue(session, tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001266out_err:
Namhyung Kima5580f32012-11-13 22:30:34 +09001267 ui_progress__finish();
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001268 perf_session__warn_about_errors(session, tool);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001269 perf_session_free_sample_buffers(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001270 return err;
1271}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001272
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001273int perf_session__process_events(struct perf_session *self,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001274 struct perf_tool *tool)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001275{
1276 int err;
1277
1278 if (perf_session__register_idle_thread(self) == NULL)
1279 return -ENOMEM;
1280
Tom Zanussi8dc58102010-04-01 23:59:15 -05001281 if (!self->fd_pipe)
1282 err = __perf_session__process_events(self,
1283 self->header.data_offset,
1284 self->header.data_size,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001285 self->size, tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001286 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001287 err = __perf_session__process_pipe_events(self, tool);
Dave Martin88ca8952010-07-27 11:46:12 -03001288
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001289 return err;
1290}
1291
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001292bool perf_session__has_traces(struct perf_session *session, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001293{
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001294 if (!(perf_evlist__sample_type(session->evlist) & PERF_SAMPLE_RAW)) {
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001295 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1296 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001297 }
1298
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001299 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001300}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001301
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001302int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1303 const char *symbol_name, u64 addr)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001304{
1305 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001306 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001307 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001308
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001309 ref = zalloc(sizeof(struct ref_reloc_sym));
1310 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001311 return -ENOMEM;
1312
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001313 ref->name = strdup(symbol_name);
1314 if (ref->name == NULL) {
1315 free(ref);
1316 return -ENOMEM;
1317 }
1318
1319 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001320 if (bracket)
1321 *bracket = '\0';
1322
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001323 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001324
1325 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001326 struct kmap *kmap = map__kmap(maps[i]);
1327 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001328 }
1329
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001330 return 0;
1331}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001332
1333size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
1334{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -03001335 return machines__fprintf_dsos(&self->machines, fp);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001336}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001337
1338size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001339 bool (skip)(struct dso *dso, int parm), int parm)
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001340{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -03001341 return machines__fprintf_dsos_buildid(&self->machines, fp, skip, parm);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001342}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001343
1344size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1345{
1346 struct perf_evsel *pos;
1347 size_t ret = fprintf(fp, "Aggregated stats:\n");
1348
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001349 ret += events_stats__fprintf(&session->stats, fp);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001350
1351 list_for_each_entry(pos, &session->evlist->entries, node) {
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -03001352 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
Arnaldo Carvalho de Melo52168ee2012-12-18 16:02:17 -03001353 ret += events_stats__fprintf(&pos->hists.stats, fp);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001354 }
1355
1356 return ret;
1357}
David Ahernc0230b22011-03-09 22:23:27 -07001358
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001359size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1360{
1361 /*
1362 * FIXME: Here we have to actually print all the machines in this
1363 * session, not just the host...
1364 */
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -03001365 return machine__fprintf(&session->machines.host, fp);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001366}
1367
1368void perf_session__remove_thread(struct perf_session *session,
1369 struct thread *th)
1370{
1371 /*
1372 * FIXME: This one makes no sense, we need to remove the thread from
1373 * the machine it belongs to, perf_session can have many machines, so
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -03001374 * doing it always on ->machines.host is wrong. Fix when auditing all
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001375 * the 'perf kvm' code.
1376 */
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -03001377 machine__remove_thread(&session->machines.host, th);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001378}
1379
David Ahern9cbdb702011-04-06 21:54:20 -06001380struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1381 unsigned int type)
1382{
1383 struct perf_evsel *pos;
1384
1385 list_for_each_entry(pos, &session->evlist->entries, node) {
1386 if (pos->attr.type == type)
1387 return pos;
1388 }
1389 return NULL;
1390}
1391
Jiri Olsa71ad0f52012-08-07 15:20:46 +02001392void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
1393 struct perf_sample *sample, struct machine *machine,
1394 int print_sym, int print_dso, int print_symoffset)
David Ahernc0230b22011-03-09 22:23:27 -07001395{
1396 struct addr_location al;
David Ahernc0230b22011-03-09 22:23:27 -07001397 struct callchain_cursor_node *node;
1398
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001399 if (perf_event__preprocess_sample(event, machine, &al, sample,
David Ahernc0230b22011-03-09 22:23:27 -07001400 NULL) < 0) {
1401 error("problem processing %d event, skipping it.\n",
1402 event->header.type);
1403 return;
1404 }
1405
1406 if (symbol_conf.use_callchain && sample->callchain) {
1407
Jiri Olsa71ad0f52012-08-07 15:20:46 +02001408
1409 if (machine__resolve_callchain(machine, evsel, al.thread,
1410 sample, NULL) != 0) {
David Ahernc0230b22011-03-09 22:23:27 -07001411 if (verbose)
1412 error("Failed to resolve callchain. Skipping\n");
1413 return;
1414 }
Namhyung Kim47260642012-05-31 14:43:26 +09001415 callchain_cursor_commit(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001416
1417 while (1) {
Namhyung Kim47260642012-05-31 14:43:26 +09001418 node = callchain_cursor_current(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001419 if (!node)
1420 break;
1421
David Ahern787bef12011-05-27 14:28:43 -06001422 printf("\t%16" PRIx64, node->ip);
1423 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001424 printf(" ");
1425 symbol__fprintf_symname(node->sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001426 }
1427 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001428 printf(" (");
David Ahern52deff72012-05-29 22:58:26 -06001429 map__fprintf_dsoname(node->map, stdout);
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001430 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001431 }
1432 printf("\n");
David Ahernc0230b22011-03-09 22:23:27 -07001433
Namhyung Kim47260642012-05-31 14:43:26 +09001434 callchain_cursor_advance(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001435 }
1436
1437 } else {
David Ahernadc4bf92011-05-30 09:16:27 -06001438 printf("%16" PRIx64, sample->ip);
David Ahern787bef12011-05-27 14:28:43 -06001439 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001440 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001441 if (print_symoffset)
1442 symbol__fprintf_symname_offs(al.sym, &al,
1443 stdout);
1444 else
1445 symbol__fprintf_symname(al.sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001446 }
1447
1448 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001449 printf(" (");
1450 map__fprintf_dsoname(al.map, stdout);
1451 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001452 }
David Ahernc0230b22011-03-09 22:23:27 -07001453 }
1454}
Anton Blanchard5d67be92011-07-04 21:57:50 +10001455
1456int perf_session__cpu_bitmap(struct perf_session *session,
1457 const char *cpu_list, unsigned long *cpu_bitmap)
1458{
1459 int i;
1460 struct cpu_map *map;
1461
1462 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1463 struct perf_evsel *evsel;
1464
1465 evsel = perf_session__find_first_evtype(session, i);
1466 if (!evsel)
1467 continue;
1468
1469 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1470 pr_err("File does not contain CPU events. "
1471 "Remove -c option to proceed.\n");
1472 return -1;
1473 }
1474 }
1475
1476 map = cpu_map__new(cpu_list);
David Ahern47fbe532011-11-13 10:45:27 -07001477 if (map == NULL) {
1478 pr_err("Invalid cpu_list\n");
1479 return -1;
1480 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001481
1482 for (i = 0; i < map->nr; i++) {
1483 int cpu = map->map[i];
1484
1485 if (cpu >= MAX_NR_CPUS) {
1486 pr_err("Requested CPU %d too large. "
1487 "Consider raising MAX_NR_CPUS\n", cpu);
1488 return -1;
1489 }
1490
1491 set_bit(cpu, cpu_bitmap);
1492 }
1493
1494 return 0;
1495}
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001496
1497void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1498 bool full)
1499{
1500 struct stat st;
1501 int ret;
1502
1503 if (session == NULL || fp == NULL)
1504 return;
1505
1506 ret = fstat(session->fd, &st);
1507 if (ret == -1)
1508 return;
1509
1510 fprintf(fp, "# ========\n");
1511 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1512 perf_header__fprintf_info(session, fp, full);
1513 fprintf(fp, "# ========\n#\n");
1514}
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001515
1516
1517int __perf_session__set_tracepoints_handlers(struct perf_session *session,
1518 const struct perf_evsel_str_handler *assocs,
1519 size_t nr_assocs)
1520{
1521 struct perf_evlist *evlist = session->evlist;
1522 struct event_format *format;
1523 struct perf_evsel *evsel;
1524 char *tracepoint, *name;
1525 size_t i;
1526 int err;
1527
1528 for (i = 0; i < nr_assocs; i++) {
1529 err = -ENOMEM;
1530 tracepoint = strdup(assocs[i].name);
1531 if (tracepoint == NULL)
1532 goto out;
1533
1534 err = -ENOENT;
1535 name = strchr(tracepoint, ':');
1536 if (name == NULL)
1537 goto out_free;
1538
1539 *name++ = '\0';
1540 format = pevent_find_event_by_name(session->pevent,
1541 tracepoint, name);
1542 if (format == NULL) {
1543 /*
1544 * Adding a handler for an event not in the session,
1545 * just ignore it.
1546 */
1547 goto next;
1548 }
1549
1550 evsel = perf_evlist__find_tracepoint_by_id(evlist, format->id);
1551 if (evsel == NULL)
1552 goto next;
1553
1554 err = -EEXIST;
1555 if (evsel->handler.func != NULL)
1556 goto out_free;
1557 evsel->handler.func = assocs[i].handler;
1558next:
1559 free(tracepoint);
1560 }
1561
1562 err = 0;
1563out:
1564 return err;
1565
1566out_free:
1567 free(tracepoint);
1568 goto out;
1569}