blob: 854c5aa4db0d2277386756c42306e3d7b0c80cfe [file] [log] [blame]
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001#include <linux/kernel.h>
Robert Richter4e319022013-06-11 17:29:18 +02002#include <traceevent/event-parse.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02003
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02004#include <byteswap.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02005#include <unistd.h>
6#include <sys/types.h>
Arnaldo Carvalho de Meloa41794c2010-05-18 18:29:23 -03007#include <sys/mman.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02008
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03009#include "evlist.h"
10#include "evsel.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020011#include "session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020012#include "tool.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"
Jiri Olsa0f6a3012012-08-07 15:20:45 +020016#include "perf_regs.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020017#include "vdso.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020018
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020019static int perf_session__open(struct perf_session *self)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020020{
Jiri Olsacc9784bd2013-10-15 16:27:34 +020021 struct perf_data_file *file = self->file;
Tom Zanussi8dc58102010-04-01 23:59:15 -050022
Jiri Olsad4339562013-07-17 19:49:41 +020023 if (perf_session__read_header(self) < 0) {
Stephane Eranian69996df2012-02-09 23:21:06 +010024 pr_err("incompatible file format (rerun with -v to learn more)");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020025 return -1;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020026 }
27
Jiri Olsacc9784bd2013-10-15 16:27:34 +020028 if (perf_data_file__is_pipe(file))
29 return 0;
30
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030031 if (!perf_evlist__valid_sample_type(self->evlist)) {
32 pr_err("non matching sample_type");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020033 return -1;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030034 }
35
36 if (!perf_evlist__valid_sample_id_all(self->evlist)) {
37 pr_err("non matching sample_id_all");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020038 return -1;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030039 }
40
Jiri Olsa9ede4732012-10-10 17:38:13 +020041 if (!perf_evlist__valid_read_format(self->evlist)) {
42 pr_err("non matching read_format");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020043 return -1;
Jiri Olsa9ede4732012-10-10 17:38:13 +020044 }
45
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020046 return 0;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020047}
48
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030049void perf_session__set_id_hdr_size(struct perf_session *session)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020050{
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030051 u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist);
52
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030053 machines__set_id_hdr_size(&session->machines, id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020054}
55
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080056int perf_session__create_kernel_maps(struct perf_session *self)
57{
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -030058 int ret = machine__create_kernel_maps(&self->machines.host);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080059
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080060 if (ret >= 0)
Arnaldo Carvalho de Melod118f8b2010-05-10 12:51:05 -030061 ret = machines__create_guest_kernel_maps(&self->machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080062 return ret;
63}
64
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -030065static void perf_session__destroy_kernel_maps(struct perf_session *self)
66{
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -030067 machines__destroy_kernel_maps(&self->machines);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -030068}
69
Jiri Olsaf5fc14122013-10-15 16:27:32 +020070struct perf_session *perf_session__new(struct perf_data_file *file,
71 bool repipe, struct perf_tool *tool)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020072{
Robert Richterefad1412011-12-07 10:02:54 +010073 struct perf_session *self;
Robert Richterefad1412011-12-07 10:02:54 +010074
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020075 self = zalloc(sizeof(*self));
76 if (!self)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020077 goto out;
78
Tom Zanussi454c4072010-05-01 01:41:20 -050079 self->repipe = repipe;
Thomas Gleixnera1225de2010-11-30 17:49:33 +000080 INIT_LIST_HEAD(&self->ordered_samples.samples);
Thomas Gleixner020bb752010-11-30 17:49:53 +000081 INIT_LIST_HEAD(&self->ordered_samples.sample_cache);
Thomas Gleixner5c891f32010-11-30 17:49:55 +000082 INIT_LIST_HEAD(&self->ordered_samples.to_free);
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -030083 machines__init(&self->machines);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020084
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020085 if (file) {
86 if (perf_data_file__open(file))
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -020087 goto out_delete;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020088
Jiri Olsacc9784bd2013-10-15 16:27:34 +020089 self->file = file;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020090
91 if (perf_data_file__is_read(file)) {
92 if (perf_session__open(self) < 0)
93 goto out_close;
94
95 perf_session__set_id_hdr_size(self);
96 }
97 }
98
99 if (!file || perf_data_file__is_write(file)) {
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200100 /*
101 * In O_RDONLY mode this will be performed when reading the
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200102 * kernel MMAP event, in perf_event__process_mmap().
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200103 */
104 if (perf_session__create_kernel_maps(self) < 0)
105 goto out_delete;
106 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200107
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200108 if (tool && tool->ordering_requires_timestamps &&
Arnaldo Carvalho de Melo5e562472012-08-01 19:25:26 -0300109 tool->ordered_samples && !perf_evlist__sample_id_all(self->evlist)) {
Ian Munsie21ef97f2010-12-10 14:09:16 +1100110 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200111 tool->ordered_samples = false;
Ian Munsie21ef97f2010-12-10 14:09:16 +1100112 }
113
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200114 return self;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200115
116 out_close:
117 perf_data_file__close(file);
118 out_delete:
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200119 perf_session__delete(self);
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200120 out:
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200121 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200122}
123
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200124static void perf_session__delete_dead_threads(struct perf_session *session)
125{
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300126 machine__delete_dead_threads(&session->machines.host);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200127}
128
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200129static void perf_session__delete_threads(struct perf_session *session)
130{
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300131 machine__delete_threads(&session->machines.host);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200132}
133
Namhyung Kim03cd2092012-11-21 13:43:19 +0900134static void perf_session_env__delete(struct perf_session_env *env)
135{
136 free(env->hostname);
137 free(env->os_release);
138 free(env->version);
139 free(env->arch);
140 free(env->cpu_desc);
141 free(env->cpuid);
142
143 free(env->cmdline);
144 free(env->sibling_cores);
145 free(env->sibling_threads);
146 free(env->numa_nodes);
147 free(env->pmu_mappings);
148}
149
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200150void perf_session__delete(struct perf_session *self)
151{
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300152 perf_session__destroy_kernel_maps(self);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300153 perf_session__delete_dead_threads(self);
154 perf_session__delete_threads(self);
Namhyung Kim03cd2092012-11-21 13:43:19 +0900155 perf_session_env__delete(&self->header.env);
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300156 machines__exit(&self->machines);
Jiri Olsacc9784bd2013-10-15 16:27:34 +0200157 if (self->file)
158 perf_data_file__close(self->file);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200159 free(self);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200160 vdso__exit();
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200161}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200162
Adrian Hunter47c3d102013-07-04 16:20:21 +0300163static int process_event_synth_tracing_data_stub(struct perf_tool *tool
164 __maybe_unused,
165 union perf_event *event
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300166 __maybe_unused,
167 struct perf_session *session
168 __maybe_unused)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200169{
170 dump_printf(": unhandled!\n");
171 return 0;
172}
173
Adrian Hunter47c3d102013-07-04 16:20:21 +0300174static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused,
175 union perf_event *event __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300176 struct perf_evlist **pevlist
177 __maybe_unused)
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200178{
179 dump_printf(": unhandled!\n");
180 return 0;
181}
182
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300183static int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
184 union perf_event *event __maybe_unused,
185 struct perf_sample *sample __maybe_unused,
186 struct perf_evsel *evsel __maybe_unused,
187 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300188{
189 dump_printf(": unhandled!\n");
190 return 0;
191}
192
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300193static int process_event_stub(struct perf_tool *tool __maybe_unused,
194 union perf_event *event __maybe_unused,
195 struct perf_sample *sample __maybe_unused,
196 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200197{
198 dump_printf(": unhandled!\n");
199 return 0;
200}
201
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300202static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
203 union perf_event *event __maybe_unused,
204 struct perf_session *perf_session
205 __maybe_unused)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200206{
207 dump_printf(": unhandled!\n");
208 return 0;
209}
210
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200211static int process_finished_round(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200212 union perf_event *event,
213 struct perf_session *session);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200214
David Ahern9c501402013-08-02 14:05:41 -0600215void perf_tool__fill_defaults(struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200216{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200217 if (tool->sample == NULL)
218 tool->sample = process_event_sample_stub;
219 if (tool->mmap == NULL)
220 tool->mmap = process_event_stub;
David Ahern6adb0b02013-09-22 19:44:59 -0600221 if (tool->mmap2 == NULL)
222 tool->mmap2 = process_event_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200223 if (tool->comm == NULL)
224 tool->comm = process_event_stub;
225 if (tool->fork == NULL)
226 tool->fork = process_event_stub;
227 if (tool->exit == NULL)
228 tool->exit = process_event_stub;
229 if (tool->lost == NULL)
230 tool->lost = perf_event__process_lost;
231 if (tool->read == NULL)
232 tool->read = process_event_sample_stub;
233 if (tool->throttle == NULL)
234 tool->throttle = process_event_stub;
235 if (tool->unthrottle == NULL)
236 tool->unthrottle = process_event_stub;
237 if (tool->attr == NULL)
238 tool->attr = process_event_synth_attr_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200239 if (tool->tracing_data == NULL)
240 tool->tracing_data = process_event_synth_tracing_data_stub;
241 if (tool->build_id == NULL)
242 tool->build_id = process_finished_round_stub;
243 if (tool->finished_round == NULL) {
244 if (tool->ordered_samples)
245 tool->finished_round = process_finished_round;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200246 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200247 tool->finished_round = process_finished_round_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200248 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200249}
David Ahern80c01202012-06-08 11:47:51 -0300250
251void mem_bswap_32(void *src, int byte_size)
252{
253 u32 *m = src;
254 while (byte_size > 0) {
255 *m = bswap_32(*m);
256 byte_size -= sizeof(u32);
257 ++m;
258 }
259}
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200260
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200261void mem_bswap_64(void *src, int byte_size)
262{
263 u64 *m = src;
264
265 while (byte_size > 0) {
266 *m = bswap_64(*m);
267 byte_size -= sizeof(u64);
268 ++m;
269 }
270}
271
Jiri Olsa268fb202012-05-30 14:23:43 +0200272static void swap_sample_id_all(union perf_event *event, void *data)
273{
274 void *end = (void *) event + event->header.size;
275 int size = end - data;
276
277 BUG_ON(size % sizeof(u64));
278 mem_bswap_64(data, size);
279}
280
281static void perf_event__all64_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300282 bool sample_id_all __maybe_unused)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200283{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200284 struct perf_event_header *hdr = &event->header;
285 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200286}
287
Jiri Olsa268fb202012-05-30 14:23:43 +0200288static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200289{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200290 event->comm.pid = bswap_32(event->comm.pid);
291 event->comm.tid = bswap_32(event->comm.tid);
Jiri Olsa268fb202012-05-30 14:23:43 +0200292
293 if (sample_id_all) {
294 void *data = &event->comm.comm;
295
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300296 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200297 swap_sample_id_all(event, data);
298 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200299}
300
Jiri Olsa268fb202012-05-30 14:23:43 +0200301static void perf_event__mmap_swap(union perf_event *event,
302 bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200303{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200304 event->mmap.pid = bswap_32(event->mmap.pid);
305 event->mmap.tid = bswap_32(event->mmap.tid);
306 event->mmap.start = bswap_64(event->mmap.start);
307 event->mmap.len = bswap_64(event->mmap.len);
308 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
Jiri Olsa268fb202012-05-30 14:23:43 +0200309
310 if (sample_id_all) {
311 void *data = &event->mmap.filename;
312
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300313 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200314 swap_sample_id_all(event, data);
315 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200316}
317
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200318static void perf_event__mmap2_swap(union perf_event *event,
319 bool sample_id_all)
320{
321 event->mmap2.pid = bswap_32(event->mmap2.pid);
322 event->mmap2.tid = bswap_32(event->mmap2.tid);
323 event->mmap2.start = bswap_64(event->mmap2.start);
324 event->mmap2.len = bswap_64(event->mmap2.len);
325 event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
326 event->mmap2.maj = bswap_32(event->mmap2.maj);
327 event->mmap2.min = bswap_32(event->mmap2.min);
328 event->mmap2.ino = bswap_64(event->mmap2.ino);
329
330 if (sample_id_all) {
331 void *data = &event->mmap2.filename;
332
333 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
334 swap_sample_id_all(event, data);
335 }
336}
Jiri Olsa268fb202012-05-30 14:23:43 +0200337static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200338{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200339 event->fork.pid = bswap_32(event->fork.pid);
340 event->fork.tid = bswap_32(event->fork.tid);
341 event->fork.ppid = bswap_32(event->fork.ppid);
342 event->fork.ptid = bswap_32(event->fork.ptid);
343 event->fork.time = bswap_64(event->fork.time);
Jiri Olsa268fb202012-05-30 14:23:43 +0200344
345 if (sample_id_all)
346 swap_sample_id_all(event, &event->fork + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200347}
348
Jiri Olsa268fb202012-05-30 14:23:43 +0200349static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200350{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200351 event->read.pid = bswap_32(event->read.pid);
352 event->read.tid = bswap_32(event->read.tid);
353 event->read.value = bswap_64(event->read.value);
354 event->read.time_enabled = bswap_64(event->read.time_enabled);
355 event->read.time_running = bswap_64(event->read.time_running);
356 event->read.id = bswap_64(event->read.id);
Jiri Olsa268fb202012-05-30 14:23:43 +0200357
358 if (sample_id_all)
359 swap_sample_id_all(event, &event->read + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200360}
361
Jiri Olsadd96c462013-09-01 12:36:15 +0200362static void perf_event__throttle_swap(union perf_event *event,
363 bool sample_id_all)
364{
365 event->throttle.time = bswap_64(event->throttle.time);
366 event->throttle.id = bswap_64(event->throttle.id);
367 event->throttle.stream_id = bswap_64(event->throttle.stream_id);
368
369 if (sample_id_all)
370 swap_sample_id_all(event, &event->throttle + 1);
371}
372
Jiri Olsae108c662012-05-16 08:59:03 +0200373static u8 revbyte(u8 b)
374{
375 int rev = (b >> 4) | ((b & 0xf) << 4);
376 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
377 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
378 return (u8) rev;
379}
380
381/*
382 * XXX this is hack in attempt to carry flags bitfield
383 * throught endian village. ABI says:
384 *
385 * Bit-fields are allocated from right to left (least to most significant)
386 * on little-endian implementations and from left to right (most to least
387 * significant) on big-endian implementations.
388 *
389 * The above seems to be byte specific, so we need to reverse each
390 * byte of the bitfield. 'Internet' also says this might be implementation
391 * specific and we probably need proper fix and carry perf_event_attr
392 * bitfield flags in separate data file FEAT_ section. Thought this seems
393 * to work for now.
394 */
395static void swap_bitfield(u8 *p, unsigned len)
396{
397 unsigned i;
398
399 for (i = 0; i < len; i++) {
400 *p = revbyte(*p);
401 p++;
402 }
403}
404
David Aherneda39132011-07-15 12:34:09 -0600405/* exported for swapping attributes in file header */
406void perf_event__attr_swap(struct perf_event_attr *attr)
407{
408 attr->type = bswap_32(attr->type);
409 attr->size = bswap_32(attr->size);
410 attr->config = bswap_64(attr->config);
411 attr->sample_period = bswap_64(attr->sample_period);
412 attr->sample_type = bswap_64(attr->sample_type);
413 attr->read_format = bswap_64(attr->read_format);
414 attr->wakeup_events = bswap_32(attr->wakeup_events);
415 attr->bp_type = bswap_32(attr->bp_type);
416 attr->bp_addr = bswap_64(attr->bp_addr);
417 attr->bp_len = bswap_64(attr->bp_len);
Adrian Hunter7db59522013-10-18 15:29:03 +0300418 attr->branch_sample_type = bswap_64(attr->branch_sample_type);
419 attr->sample_regs_user = bswap_64(attr->sample_regs_user);
420 attr->sample_stack_user = bswap_32(attr->sample_stack_user);
Jiri Olsae108c662012-05-16 08:59:03 +0200421
422 swap_bitfield((u8 *) (&attr->read_format + 1), sizeof(u64));
David Aherneda39132011-07-15 12:34:09 -0600423}
424
Jiri Olsa268fb202012-05-30 14:23:43 +0200425static void perf_event__hdr_attr_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300426 bool sample_id_all __maybe_unused)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500427{
428 size_t size;
429
David Aherneda39132011-07-15 12:34:09 -0600430 perf_event__attr_swap(&event->attr.attr);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500431
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200432 size = event->header.size;
433 size -= (void *)&event->attr.id - (void *)event;
434 mem_bswap_64(event->attr.id, size);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500435}
436
Jiri Olsa268fb202012-05-30 14:23:43 +0200437static void perf_event__event_type_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300438 bool sample_id_all __maybe_unused)
Tom Zanussicd19a032010-04-01 23:59:20 -0500439{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200440 event->event_type.event_type.event_id =
441 bswap_64(event->event_type.event_type.event_id);
Tom Zanussicd19a032010-04-01 23:59:20 -0500442}
443
Jiri Olsa268fb202012-05-30 14:23:43 +0200444static void perf_event__tracing_data_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300445 bool sample_id_all __maybe_unused)
Tom Zanussi92155452010-04-01 23:59:21 -0500446{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200447 event->tracing_data.size = bswap_32(event->tracing_data.size);
Tom Zanussi92155452010-04-01 23:59:21 -0500448}
449
Jiri Olsa268fb202012-05-30 14:23:43 +0200450typedef void (*perf_event__swap_op)(union perf_event *event,
451 bool sample_id_all);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200452
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200453static perf_event__swap_op perf_event__swap_ops[] = {
454 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200455 [PERF_RECORD_MMAP2] = perf_event__mmap2_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200456 [PERF_RECORD_COMM] = perf_event__comm_swap,
457 [PERF_RECORD_FORK] = perf_event__task_swap,
458 [PERF_RECORD_EXIT] = perf_event__task_swap,
459 [PERF_RECORD_LOST] = perf_event__all64_swap,
460 [PERF_RECORD_READ] = perf_event__read_swap,
Jiri Olsadd96c462013-09-01 12:36:15 +0200461 [PERF_RECORD_THROTTLE] = perf_event__throttle_swap,
462 [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200463 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
David Aherneda39132011-07-15 12:34:09 -0600464 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200465 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
466 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
467 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
468 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200469};
470
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200471struct sample_queue {
472 u64 timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000473 u64 file_offset;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200474 union perf_event *event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200475 struct list_head list;
476};
477
Thomas Gleixner020bb752010-11-30 17:49:53 +0000478static void perf_session_free_sample_buffers(struct perf_session *session)
479{
480 struct ordered_samples *os = &session->ordered_samples;
481
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000482 while (!list_empty(&os->to_free)) {
Thomas Gleixner020bb752010-11-30 17:49:53 +0000483 struct sample_queue *sq;
484
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000485 sq = list_entry(os->to_free.next, struct sample_queue, list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000486 list_del(&sq->list);
487 free(sq);
488 }
489}
490
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100491static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200492 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200493 struct perf_sample *sample,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200494 struct perf_tool *tool,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000495 u64 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100496
David Ahernd25380cd2012-08-26 12:24:41 -0600497static int flush_sample_queue(struct perf_session *s,
David Ahern9c501402013-08-02 14:05:41 -0600498 struct perf_tool *tool)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200499{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000500 struct ordered_samples *os = &s->ordered_samples;
501 struct list_head *head = &os->samples;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200502 struct sample_queue *tmp, *iter;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200503 struct perf_sample sample;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000504 u64 limit = os->next_flush;
505 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200506 unsigned idx = 0, progress_next = os->nr_samples / 16;
Jiri Olsa526fd8d2013-09-05 11:14:50 +0200507 bool show_progress = limit == ULLONG_MAX;
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200508 int ret;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200509
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200510 if (!tool->ordered_samples || !limit)
David Ahernd25380cd2012-08-26 12:24:41 -0600511 return 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200512
513 list_for_each_entry_safe(iter, tmp, head, list) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -0300514 if (session_done())
515 return 0;
516
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200517 if (iter->timestamp > limit)
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000518 break;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200519
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300520 ret = perf_evlist__parse_sample(s->evlist, iter->event, &sample);
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200521 if (ret)
522 pr_err("Can't parse sample, err = %d\n", ret);
David Ahernd25380cd2012-08-26 12:24:41 -0600523 else {
524 ret = perf_session_deliver_event(s, iter->event, &sample, tool,
525 iter->file_offset);
526 if (ret)
527 return ret;
528 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200529
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000530 os->last_flush = iter->timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200531 list_del(&iter->list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000532 list_add(&iter->list, &os->sample_cache);
Jiri Olsa526fd8d2013-09-05 11:14:50 +0200533 if (show_progress && (++idx >= progress_next)) {
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200534 progress_next += os->nr_samples / 16;
535 ui_progress__update(idx, os->nr_samples,
536 "Processing time ordered events...");
537 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200538 }
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000539
540 if (list_empty(head)) {
541 os->last_sample = NULL;
542 } else if (last_ts <= limit) {
543 os->last_sample =
544 list_entry(head->prev, struct sample_queue, list);
545 }
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200546
547 os->nr_samples = 0;
David Ahernd25380cd2012-08-26 12:24:41 -0600548
549 return 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200550}
551
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200552/*
553 * When perf record finishes a pass on every buffers, it records this pseudo
554 * event.
555 * We record the max timestamp t found in the pass n.
556 * Assuming these timestamps are monotonic across cpus, we know that if
557 * a buffer still has events with timestamps below t, they will be all
558 * available and then read in the pass n + 1.
559 * Hence when we start to read the pass n + 2, we can safely flush every
560 * events with timestamps below t.
561 *
562 * ============ PASS n =================
563 * CPU 0 | CPU 1
564 * |
565 * cnt1 timestamps | cnt2 timestamps
566 * 1 | 2
567 * 2 | 3
568 * - | 4 <--- max recorded
569 *
570 * ============ PASS n + 1 ==============
571 * CPU 0 | CPU 1
572 * |
573 * cnt1 timestamps | cnt2 timestamps
574 * 3 | 5
575 * 4 | 6
576 * 5 | 7 <---- max recorded
577 *
578 * Flush every events below timestamp 4
579 *
580 * ============ PASS n + 2 ==============
581 * CPU 0 | CPU 1
582 * |
583 * cnt1 timestamps | cnt2 timestamps
584 * 6 | 8
585 * 7 | 9
586 * - | 10
587 *
588 * Flush every events below timestamp 7
589 * etc...
590 */
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200591static int process_finished_round(struct perf_tool *tool,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300592 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200593 struct perf_session *session)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200594{
David Ahernd25380cd2012-08-26 12:24:41 -0600595 int ret = flush_sample_queue(session, tool);
596 if (!ret)
597 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200598
David Ahernd25380cd2012-08-26 12:24:41 -0600599 return ret;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200600}
601
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200602/* The queue is ordered by time */
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100603static void __queue_event(struct sample_queue *new, struct perf_session *s)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200604{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000605 struct ordered_samples *os = &s->ordered_samples;
606 struct sample_queue *sample = os->last_sample;
607 u64 timestamp = new->timestamp;
608 struct list_head *p;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200609
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200610 ++os->nr_samples;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000611 os->last_sample = new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200612
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000613 if (!sample) {
614 list_add(&new->list, &os->samples);
615 os->max_timestamp = timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200616 return;
617 }
618
619 /*
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000620 * last_sample might point to some random place in the list as it's
621 * the last queued event. We expect that the new event is close to
622 * this.
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200623 */
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000624 if (sample->timestamp <= timestamp) {
625 while (sample->timestamp <= timestamp) {
626 p = sample->list.next;
627 if (p == &os->samples) {
628 list_add_tail(&new->list, &os->samples);
629 os->max_timestamp = timestamp;
630 return;
631 }
632 sample = list_entry(p, struct sample_queue, list);
633 }
634 list_add_tail(&new->list, &sample->list);
635 } else {
636 while (sample->timestamp > timestamp) {
637 p = sample->list.prev;
638 if (p == &os->samples) {
639 list_add(&new->list, &os->samples);
640 return;
641 }
642 sample = list_entry(p, struct sample_queue, list);
643 }
644 list_add(&new->list, &sample->list);
645 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200646}
647
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000648#define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
649
David Aherne30b88a2013-08-05 21:41:33 -0400650int perf_session_queue_event(struct perf_session *s, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200651 struct perf_sample *sample, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200652{
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000653 struct ordered_samples *os = &s->ordered_samples;
654 struct list_head *sc = &os->sample_cache;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200655 u64 timestamp = sample->time;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200656 struct sample_queue *new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200657
Thomas Gleixner79a14c12010-12-07 12:48:44 +0000658 if (!timestamp || timestamp == ~0ULL)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100659 return -ETIME;
660
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200661 if (timestamp < s->ordered_samples.last_flush) {
662 printf("Warning: Timestamp below last timeslice flush\n");
663 return -EINVAL;
664 }
665
Thomas Gleixner020bb752010-11-30 17:49:53 +0000666 if (!list_empty(sc)) {
667 new = list_entry(sc->next, struct sample_queue, list);
668 list_del(&new->list);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000669 } else if (os->sample_buffer) {
670 new = os->sample_buffer + os->sample_buffer_idx;
671 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
672 os->sample_buffer = NULL;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000673 } else {
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000674 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
675 if (!os->sample_buffer)
Thomas Gleixner020bb752010-11-30 17:49:53 +0000676 return -ENOMEM;
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000677 list_add(&os->sample_buffer->list, &os->to_free);
678 os->sample_buffer_idx = 2;
679 new = os->sample_buffer + 1;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000680 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200681
682 new->timestamp = timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000683 new->file_offset = file_offset;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000684 new->event = event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200685
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100686 __queue_event(new, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200687
688 return 0;
689}
690
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200691static void callchain__printf(struct perf_sample *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200692{
693 unsigned int i;
694
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200695 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200696
697 for (i = 0; i < sample->callchain->nr; i++)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200698 printf("..... %2d: %016" PRIx64 "\n",
699 i, sample->callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200700}
701
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100702static void branch_stack__printf(struct perf_sample *sample)
703{
704 uint64_t i;
705
706 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
707
708 for (i = 0; i < sample->branch_stack->nr; i++)
709 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n",
710 i, sample->branch_stack->entries[i].from,
711 sample->branch_stack->entries[i].to);
712}
713
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200714static void regs_dump__printf(u64 mask, u64 *regs)
715{
716 unsigned rid, i = 0;
717
718 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
719 u64 val = regs[i++];
720
721 printf(".... %-5s 0x%" PRIx64 "\n",
722 perf_reg_name(rid), val);
723 }
724}
725
726static void regs_user__printf(struct perf_sample *sample, u64 mask)
727{
728 struct regs_dump *user_regs = &sample->user_regs;
729
730 if (user_regs->regs) {
731 printf("... user regs: mask 0x%" PRIx64 "\n", mask);
732 regs_dump__printf(mask, user_regs->regs);
733 }
734}
735
736static void stack_user__printf(struct stack_dump *dump)
737{
738 printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
739 dump->size, dump->offset);
740}
741
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200742static void perf_session__print_tstamp(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200743 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200744 struct perf_sample *sample)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200745{
Adrian Hunter75562572013-08-27 11:23:09 +0300746 u64 sample_type = __perf_evlist__combined_sample_type(session->evlist);
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300747
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200748 if (event->header.type != PERF_RECORD_SAMPLE &&
Arnaldo Carvalho de Melo5e562472012-08-01 19:25:26 -0300749 !perf_evlist__sample_id_all(session->evlist)) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200750 fputs("-1 -1 ", stdout);
751 return;
752 }
753
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300754 if ((sample_type & PERF_SAMPLE_CPU))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200755 printf("%u ", sample->cpu);
756
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300757 if (sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200758 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200759}
760
Jiri Olsa9ede4732012-10-10 17:38:13 +0200761static void sample_read__printf(struct perf_sample *sample, u64 read_format)
762{
763 printf("... sample_read:\n");
764
765 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
766 printf("...... time enabled %016" PRIx64 "\n",
767 sample->read.time_enabled);
768
769 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
770 printf("...... time running %016" PRIx64 "\n",
771 sample->read.time_running);
772
773 if (read_format & PERF_FORMAT_GROUP) {
774 u64 i;
775
776 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
777
778 for (i = 0; i < sample->read.group.nr; i++) {
779 struct sample_read_value *value;
780
781 value = &sample->read.group.values[i];
782 printf("..... id %016" PRIx64
783 ", value %016" PRIx64 "\n",
784 value->id, value->value);
785 }
786 } else
787 printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
788 sample->read.one.id, sample->read.one.value);
789}
790
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200791static void dump_event(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200792 u64 file_offset, struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000793{
794 if (!dump_trace)
795 return;
796
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200797 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
798 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000799
800 trace_event(event);
801
802 if (sample)
803 perf_session__print_tstamp(session, event, sample);
804
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200805 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200806 event->header.size, perf_event__name(event->header.type));
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000807}
808
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200809static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200810 struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000811{
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300812 u64 sample_type;
813
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200814 if (!dump_trace)
815 return;
816
David Ahern7cec0922011-05-30 13:08:23 -0600817 printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200818 event->header.misc, sample->pid, sample->tid, sample->ip,
David Ahern7cec0922011-05-30 13:08:23 -0600819 sample->period, sample->addr);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000820
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200821 sample_type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300822
823 if (sample_type & PERF_SAMPLE_CALLCHAIN)
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200824 callchain__printf(sample);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100825
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300826 if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100827 branch_stack__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200828
829 if (sample_type & PERF_SAMPLE_REGS_USER)
830 regs_user__printf(sample, evsel->attr.sample_regs_user);
831
832 if (sample_type & PERF_SAMPLE_STACK_USER)
833 stack_user__printf(&sample->user_stack);
Andi Kleen05484292013-01-24 16:10:29 +0100834
835 if (sample_type & PERF_SAMPLE_WEIGHT)
836 printf("... weight: %" PRIu64 "\n", sample->weight);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100837
838 if (sample_type & PERF_SAMPLE_DATA_SRC)
839 printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
Jiri Olsa9ede4732012-10-10 17:38:13 +0200840
Andi Kleen475eeab2013-09-20 07:40:43 -0700841 if (sample_type & PERF_SAMPLE_TRANSACTION)
842 printf("... transaction: %" PRIx64 "\n", sample->transaction);
843
Jiri Olsa9ede4732012-10-10 17:38:13 +0200844 if (sample_type & PERF_SAMPLE_READ)
845 sample_read__printf(sample, evsel->attr.read_format);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000846}
847
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200848static struct machine *
849 perf_session__find_machine_for_cpumode(struct perf_session *session,
Adrian Hunteref893252013-08-27 11:23:06 +0300850 union perf_event *event,
851 struct perf_sample *sample)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200852{
853 const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
854
David Ahern7c0f4a42012-07-20 17:25:48 -0600855 if (perf_guest &&
856 ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
857 (cpumode == PERF_RECORD_MISC_GUEST_USER))) {
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530858 u32 pid;
859
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200860 if (event->header.type == PERF_RECORD_MMAP
861 || event->header.type == PERF_RECORD_MMAP2)
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530862 pid = event->mmap.pid;
863 else
Adrian Hunteref893252013-08-27 11:23:06 +0300864 pid = sample->pid;
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530865
David Ahern207b5792012-07-01 16:11:37 -0600866 return perf_session__findnew_machine(session, pid);
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530867 }
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200868
Arnaldo Carvalho de Melo34ba5122012-12-19 09:04:24 -0300869 return &session->machines.host;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200870}
871
Jiri Olsae4caec02012-10-10 18:52:24 +0200872static int deliver_sample_value(struct perf_session *session,
873 struct perf_tool *tool,
874 union perf_event *event,
875 struct perf_sample *sample,
876 struct sample_read_value *v,
877 struct machine *machine)
878{
879 struct perf_sample_id *sid;
880
881 sid = perf_evlist__id2sid(session->evlist, v->id);
882 if (sid) {
883 sample->id = v->id;
884 sample->period = v->value - sid->period;
885 sid->period = v->value;
886 }
887
888 if (!sid || sid->evsel == NULL) {
889 ++session->stats.nr_unknown_id;
890 return 0;
891 }
892
893 return tool->sample(tool, event, sample, sid->evsel, machine);
894}
895
896static int deliver_sample_group(struct perf_session *session,
897 struct perf_tool *tool,
898 union perf_event *event,
899 struct perf_sample *sample,
900 struct machine *machine)
901{
902 int ret = -EINVAL;
903 u64 i;
904
905 for (i = 0; i < sample->read.group.nr; i++) {
906 ret = deliver_sample_value(session, tool, event, sample,
907 &sample->read.group.values[i],
908 machine);
909 if (ret)
910 break;
911 }
912
913 return ret;
914}
915
916static int
917perf_session__deliver_sample(struct perf_session *session,
918 struct perf_tool *tool,
919 union perf_event *event,
920 struct perf_sample *sample,
921 struct perf_evsel *evsel,
922 struct machine *machine)
923{
924 /* We know evsel != NULL. */
925 u64 sample_type = evsel->attr.sample_type;
926 u64 read_format = evsel->attr.read_format;
927
928 /* Standard sample delievery. */
929 if (!(sample_type & PERF_SAMPLE_READ))
930 return tool->sample(tool, event, sample, evsel, machine);
931
932 /* For PERF_SAMPLE_READ we have either single or group mode. */
933 if (read_format & PERF_FORMAT_GROUP)
934 return deliver_sample_group(session, tool, event, sample,
935 machine);
936 else
937 return deliver_sample_value(session, tool, event, sample,
938 &sample->read.one, machine);
939}
940
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100941static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200942 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200943 struct perf_sample *sample,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200944 struct perf_tool *tool,
Thomas Gleixner532e7262010-12-07 12:48:55 +0000945 u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100946{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300947 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200948 struct machine *machine;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300949
Thomas Gleixner532e7262010-12-07 12:48:55 +0000950 dump_event(session, event, file_offset, sample);
951
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200952 evsel = perf_evlist__id2evsel(session->evlist, sample->id);
953 if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) {
954 /*
955 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
956 * because the tools right now may apply filters, discarding
957 * some of the samples. For consistency, in the future we
958 * should have something like nr_filtered_samples and remove
959 * the sample->period from total_sample_period, etc, KISS for
960 * now tho.
961 *
962 * Also testing against NULL allows us to handle files without
963 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
964 * future probably it'll be a good idea to restrict event
965 * processing via perf_session to files with both set.
966 */
967 hists__inc_nr_events(&evsel->hists, event->header.type);
968 }
969
Adrian Hunteref893252013-08-27 11:23:06 +0300970 machine = perf_session__find_machine_for_cpumode(session, event,
971 sample);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200972
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100973 switch (event->header.type) {
974 case PERF_RECORD_SAMPLE:
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200975 dump_sample(evsel, event, sample);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300976 if (evsel == NULL) {
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300977 ++session->stats.nr_unknown_id;
Jiri Olsa67822062012-04-12 14:21:01 +0200978 return 0;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300979 }
Joerg Roedel0c095712012-02-10 18:05:04 +0100980 if (machine == NULL) {
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300981 ++session->stats.nr_unprocessable_samples;
Jiri Olsa67822062012-04-12 14:21:01 +0200982 return 0;
Joerg Roedel0c095712012-02-10 18:05:04 +0100983 }
Jiri Olsae4caec02012-10-10 18:52:24 +0200984 return perf_session__deliver_sample(session, tool, event,
985 sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100986 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200987 return tool->mmap(tool, event, sample, machine);
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200988 case PERF_RECORD_MMAP2:
989 return tool->mmap2(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100990 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200991 return tool->comm(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100992 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200993 return tool->fork(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100994 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200995 return tool->exit(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100996 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200997 if (tool->lost == perf_event__process_lost)
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300998 session->stats.total_lost += event->lost.lost;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200999 return tool->lost(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001000 case PERF_RECORD_READ:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001001 return tool->read(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001002 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001003 return tool->throttle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001004 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001005 return tool->unthrottle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001006 default:
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001007 ++session->stats.nr_unknown_events;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001008 return -1;
1009 }
1010}
1011
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001012static int perf_session__process_user_event(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001013 struct perf_tool *tool, u64 file_offset)
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001014{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001015 int fd = perf_data_file__fd(session->file);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001016 int err;
1017
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001018 dump_event(session, event, file_offset, NULL);
1019
1020 /* These events are processed right away */
1021 switch (event->header.type) {
1022 case PERF_RECORD_HEADER_ATTR:
Adrian Hunter47c3d102013-07-04 16:20:21 +03001023 err = tool->attr(tool, event, &session->evlist);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001024 if (err == 0)
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -03001025 perf_session__set_id_hdr_size(session);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001026 return err;
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001027 case PERF_RECORD_HEADER_TRACING_DATA:
1028 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001029 lseek(fd, file_offset, SEEK_SET);
Adrian Hunter47c3d102013-07-04 16:20:21 +03001030 return tool->tracing_data(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001031 case PERF_RECORD_HEADER_BUILD_ID:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001032 return tool->build_id(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001033 case PERF_RECORD_FINISHED_ROUND:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001034 return tool->finished_round(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001035 default:
1036 return -EINVAL;
1037 }
1038}
1039
Jiri Olsa268fb202012-05-30 14:23:43 +02001040static void event_swap(union perf_event *event, bool sample_id_all)
1041{
1042 perf_event__swap_op swap;
1043
1044 swap = perf_event__swap_ops[event->header.type];
1045 if (swap)
1046 swap(event, sample_id_all);
1047}
1048
David Aherne30b88a2013-08-05 21:41:33 -04001049static int perf_session__process_event(struct perf_session *session,
1050 union perf_event *event,
1051 struct perf_tool *tool,
1052 u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001053{
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001054 struct perf_sample sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001055 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001056
Jiri Olsa268fb202012-05-30 14:23:43 +02001057 if (session->header.needs_swap)
Arnaldo Carvalho de Melo5e562472012-08-01 19:25:26 -03001058 event_swap(event, perf_evlist__sample_id_all(session->evlist));
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001059
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001060 if (event->header.type >= PERF_RECORD_HEADER_MAX)
1061 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001062
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001063 events_stats__inc(&session->stats, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001064
1065 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001066 return perf_session__process_user_event(session, event, tool, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001067
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001068 /*
1069 * For all kernel events we get the sample data
1070 */
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001071 ret = perf_evlist__parse_sample(session->evlist, event, &sample);
Frederic Weisbecker5538bec2011-05-22 02:17:22 +02001072 if (ret)
1073 return ret;
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001074
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001075 if (tool->ordered_samples) {
Thomas Gleixnere4c2df12010-12-07 12:48:50 +00001076 ret = perf_session_queue_event(session, event, &sample,
1077 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001078 if (ret != -ETIME)
1079 return ret;
1080 }
1081
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001082 return perf_session_deliver_event(session, event, &sample, tool,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +00001083 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001084}
1085
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001086void perf_event_header__bswap(struct perf_event_header *self)
1087{
1088 self->type = bswap_32(self->type);
1089 self->misc = bswap_16(self->misc);
1090 self->size = bswap_16(self->size);
1091}
1092
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001093struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1094{
Adrian Hunter314add62013-08-27 11:23:03 +03001095 return machine__findnew_thread(&session->machines.host, 0, pid);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001096}
1097
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001098static struct thread *perf_session__register_idle_thread(struct perf_session *self)
1099{
1100 struct thread *thread = perf_session__findnew(self, 0);
1101
1102 if (thread == NULL || thread__set_comm(thread, "swapper")) {
1103 pr_err("problem inserting idle task.\n");
1104 thread = NULL;
1105 }
1106
1107 return thread;
1108}
1109
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001110static void perf_session__warn_about_errors(const struct perf_session *session,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001111 const struct perf_tool *tool)
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001112{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001113 if (tool->lost == perf_event__process_lost &&
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001114 session->stats.nr_events[PERF_RECORD_LOST] != 0) {
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001115 ui__warning("Processed %d events and lost %d chunks!\n\n"
1116 "Check IO/CPU overload!\n\n",
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001117 session->stats.nr_events[0],
1118 session->stats.nr_events[PERF_RECORD_LOST]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001119 }
1120
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001121 if (session->stats.nr_unknown_events != 0) {
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001122 ui__warning("Found %u unknown events!\n\n"
1123 "Is this an older tool processing a perf.data "
1124 "file generated by a more recent tool?\n\n"
1125 "If that is not the case, consider "
1126 "reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001127 session->stats.nr_unknown_events);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001128 }
1129
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001130 if (session->stats.nr_unknown_id != 0) {
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001131 ui__warning("%u samples with id not present in the header\n",
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001132 session->stats.nr_unknown_id);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001133 }
1134
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001135 if (session->stats.nr_invalid_chains != 0) {
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001136 ui__warning("Found invalid callchains!\n\n"
1137 "%u out of %u events were discarded for this reason.\n\n"
1138 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001139 session->stats.nr_invalid_chains,
1140 session->stats.nr_events[PERF_RECORD_SAMPLE]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001141 }
Joerg Roedel0c095712012-02-10 18:05:04 +01001142
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001143 if (session->stats.nr_unprocessable_samples != 0) {
Joerg Roedel0c095712012-02-10 18:05:04 +01001144 ui__warning("%u unprocessable samples recorded.\n"
1145 "Do you have a KVM guest running and not using 'perf kvm'?\n",
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001146 session->stats.nr_unprocessable_samples);
Joerg Roedel0c095712012-02-10 18:05:04 +01001147 }
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001148}
1149
Tom Zanussi8dc58102010-04-01 23:59:15 -05001150volatile int session_done;
1151
1152static int __perf_session__process_pipe_events(struct perf_session *self,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001153 struct perf_tool *tool)
Tom Zanussi8dc58102010-04-01 23:59:15 -05001154{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001155 int fd = perf_data_file__fd(self->file);
Stephane Eranian444d2862012-05-15 13:28:12 +02001156 union perf_event *event;
1157 uint32_t size, cur_size = 0;
1158 void *buf = NULL;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001159 int skip = 0;
1160 u64 head;
1161 int err;
1162 void *p;
1163
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001164 perf_tool__fill_defaults(tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001165
1166 head = 0;
Stephane Eranian444d2862012-05-15 13:28:12 +02001167 cur_size = sizeof(union perf_event);
1168
1169 buf = malloc(cur_size);
1170 if (!buf)
1171 return -errno;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001172more:
Stephane Eranian444d2862012-05-15 13:28:12 +02001173 event = buf;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001174 err = readn(fd, event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05001175 if (err <= 0) {
1176 if (err == 0)
1177 goto done;
1178
1179 pr_err("failed to read event header\n");
1180 goto out_err;
1181 }
1182
1183 if (self->header.needs_swap)
Stephane Eranian444d2862012-05-15 13:28:12 +02001184 perf_event_header__bswap(&event->header);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001185
Stephane Eranian444d2862012-05-15 13:28:12 +02001186 size = event->header.size;
Adrian Hunter27389d72013-07-04 16:20:27 +03001187 if (size < sizeof(struct perf_event_header)) {
1188 pr_err("bad event header size\n");
1189 goto out_err;
1190 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001191
Stephane Eranian444d2862012-05-15 13:28:12 +02001192 if (size > cur_size) {
1193 void *new = realloc(buf, size);
1194 if (!new) {
1195 pr_err("failed to allocate memory to read event\n");
1196 goto out_err;
1197 }
1198 buf = new;
1199 cur_size = size;
1200 event = buf;
1201 }
1202 p = event;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001203 p += sizeof(struct perf_event_header);
1204
Tom Zanussi794e43b2010-05-05 00:27:40 -05001205 if (size - sizeof(struct perf_event_header)) {
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001206 err = readn(fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -05001207 if (err <= 0) {
1208 if (err == 0) {
1209 pr_err("unexpected end of event stream\n");
1210 goto done;
1211 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001212
Tom Zanussi794e43b2010-05-05 00:27:40 -05001213 pr_err("failed to read event data\n");
1214 goto out_err;
1215 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001216 }
1217
Stephane Eranian444d2862012-05-15 13:28:12 +02001218 if ((skip = perf_session__process_event(self, event, tool, head)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001219 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
Stephane Eranian444d2862012-05-15 13:28:12 +02001220 head, event->header.size, event->header.type);
Jiri Olsa9389a462012-04-16 20:42:51 +02001221 err = -EINVAL;
1222 goto out_err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001223 }
1224
1225 head += size;
1226
Tom Zanussi8dc58102010-04-01 23:59:15 -05001227 if (skip > 0)
1228 head += skip;
1229
1230 if (!session_done())
1231 goto more;
1232done:
Adrian Hunter8c16b642013-10-18 15:29:02 +03001233 /* do the final flush for ordered samples */
1234 self->ordered_samples.next_flush = ULLONG_MAX;
1235 err = flush_sample_queue(self, tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001236out_err:
Stephane Eranian444d2862012-05-15 13:28:12 +02001237 free(buf);
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001238 perf_session__warn_about_errors(self, tool);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001239 perf_session_free_sample_buffers(self);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001240 return err;
1241}
1242
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001243static union perf_event *
1244fetch_mmaped_event(struct perf_session *session,
1245 u64 head, size_t mmap_size, char *buf)
1246{
1247 union perf_event *event;
1248
1249 /*
1250 * Ensure we have enough space remaining to read
1251 * the size of the event in the headers.
1252 */
1253 if (head + sizeof(event->header) > mmap_size)
1254 return NULL;
1255
1256 event = (union perf_event *)(buf + head);
1257
1258 if (session->header.needs_swap)
1259 perf_event_header__bswap(&event->header);
1260
Adrian Hunter27389d72013-07-04 16:20:27 +03001261 if (head + event->header.size > mmap_size) {
1262 /* We're not fetching the event so swap back again */
1263 if (session->header.needs_swap)
1264 perf_event_header__bswap(&event->header);
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001265 return NULL;
Adrian Hunter27389d72013-07-04 16:20:27 +03001266 }
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001267
1268 return event;
1269}
1270
David Miller35d48dd2012-11-10 14:12:19 -05001271/*
1272 * On 64bit we can mmap the data file in one go. No need for tiny mmap
1273 * slices. On 32bit we use 32MB.
1274 */
1275#if BITS_PER_LONG == 64
1276#define MMAP_SIZE ULLONG_MAX
1277#define NUM_MMAPS 1
1278#else
1279#define MMAP_SIZE (32 * 1024 * 1024ULL)
1280#define NUM_MMAPS 128
1281#endif
1282
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001283int __perf_session__process_events(struct perf_session *session,
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001284 u64 data_offset, u64 data_size,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001285 u64 file_size, struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001286{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001287 int fd = perf_data_file__fd(session->file);
Thomas Gleixner55b446292010-11-30 17:49:46 +00001288 u64 head, page_offset, file_offset, file_pos, progress_next;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001289 int err, mmap_prot, mmap_flags, map_idx = 0;
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -03001290 size_t mmap_size;
David Miller35d48dd2012-11-10 14:12:19 -05001291 char *buf, *mmaps[NUM_MMAPS];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001292 union perf_event *event;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001293 uint32_t size;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001294
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001295 perf_tool__fill_defaults(tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001296
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001297 page_offset = page_size * (data_offset / page_size);
1298 file_offset = page_offset;
1299 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001300
Namhyung Kimb314e5c2013-09-30 17:19:48 +09001301 if (data_size && (data_offset + data_size < file_size))
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001302 file_size = data_offset + data_size;
1303
Thomas Gleixner55b446292010-11-30 17:49:46 +00001304 progress_next = file_size / 16;
Thomas Gleixner55b446292010-11-30 17:49:46 +00001305
David Miller35d48dd2012-11-10 14:12:19 -05001306 mmap_size = MMAP_SIZE;
Thomas Gleixner55b446292010-11-30 17:49:46 +00001307 if (mmap_size > file_size)
1308 mmap_size = file_size;
1309
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001310 memset(mmaps, 0, sizeof(mmaps));
1311
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001312 mmap_prot = PROT_READ;
1313 mmap_flags = MAP_SHARED;
1314
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001315 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001316 mmap_prot |= PROT_WRITE;
1317 mmap_flags = MAP_PRIVATE;
1318 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001319remap:
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001320 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd,
Thomas Gleixner55b446292010-11-30 17:49:46 +00001321 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001322 if (buf == MAP_FAILED) {
1323 pr_err("failed to mmap file\n");
1324 err = -errno;
1325 goto out_err;
1326 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001327 mmaps[map_idx] = buf;
1328 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001329 file_pos = file_offset + head;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001330
1331more:
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001332 event = fetch_mmaped_event(session, head, mmap_size, buf);
1333 if (!event) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001334 if (mmaps[map_idx]) {
1335 munmap(mmaps[map_idx], mmap_size);
1336 mmaps[map_idx] = NULL;
1337 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001338
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001339 page_offset = page_size * (head / page_size);
1340 file_offset += page_offset;
1341 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001342 goto remap;
1343 }
1344
1345 size = event->header.size;
1346
Adrian Hunter27389d72013-07-04 16:20:27 +03001347 if (size < sizeof(struct perf_event_header) ||
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001348 perf_session__process_event(session, event, tool, file_pos) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001349 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1350 file_offset + head, event->header.size,
1351 event->header.type);
1352 err = -EINVAL;
1353 goto out_err;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001354 }
1355
1356 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001357 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001358
Thomas Gleixner55b446292010-11-30 17:49:46 +00001359 if (file_pos >= progress_next) {
1360 progress_next += file_size / 16;
Arnaldo Carvalho de Meloca59bcb2011-10-25 13:29:11 -02001361 ui_progress__update(file_pos, file_size,
1362 "Processing events...");
Thomas Gleixner55b446292010-11-30 17:49:46 +00001363 }
1364
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001365 if (session_done())
Adrian Hunter8c16b642013-10-18 15:29:02 +03001366 goto out;
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001367
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001368 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001369 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001370
Adrian Hunter8c16b642013-10-18 15:29:02 +03001371out:
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001372 /* do the final flush for ordered samples */
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001373 session->ordered_samples.next_flush = ULLONG_MAX;
David Ahernd25380cd2012-08-26 12:24:41 -06001374 err = flush_sample_queue(session, tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001375out_err:
Namhyung Kima5580f32012-11-13 22:30:34 +09001376 ui_progress__finish();
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001377 perf_session__warn_about_errors(session, tool);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001378 perf_session_free_sample_buffers(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001379 return err;
1380}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001381
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001382int perf_session__process_events(struct perf_session *self,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001383 struct perf_tool *tool)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001384{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001385 u64 size = perf_data_file__size(self->file);
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001386 int err;
1387
1388 if (perf_session__register_idle_thread(self) == NULL)
1389 return -ENOMEM;
1390
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001391 if (!perf_data_file__is_pipe(self->file))
Tom Zanussi8dc58102010-04-01 23:59:15 -05001392 err = __perf_session__process_events(self,
1393 self->header.data_offset,
1394 self->header.data_size,
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001395 size, tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001396 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001397 err = __perf_session__process_pipe_events(self, tool);
Dave Martin88ca8952010-07-27 11:46:12 -03001398
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001399 return err;
1400}
1401
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001402bool perf_session__has_traces(struct perf_session *session, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001403{
David Ahern93ea01c292013-08-07 22:50:58 -04001404 struct perf_evsel *evsel;
1405
1406 list_for_each_entry(evsel, &session->evlist->entries, node) {
1407 if (evsel->attr.type == PERF_TYPE_TRACEPOINT)
1408 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001409 }
1410
David Ahern93ea01c292013-08-07 22:50:58 -04001411 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1412 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001413}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001414
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001415int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1416 const char *symbol_name, u64 addr)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001417{
1418 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001419 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001420 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001421
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001422 ref = zalloc(sizeof(struct ref_reloc_sym));
1423 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001424 return -ENOMEM;
1425
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001426 ref->name = strdup(symbol_name);
1427 if (ref->name == NULL) {
1428 free(ref);
1429 return -ENOMEM;
1430 }
1431
1432 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001433 if (bracket)
1434 *bracket = '\0';
1435
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001436 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001437
1438 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001439 struct kmap *kmap = map__kmap(maps[i]);
1440 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001441 }
1442
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001443 return 0;
1444}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001445
1446size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
1447{
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001448 return machines__fprintf_dsos(&self->machines, fp);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001449}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001450
1451size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001452 bool (skip)(struct dso *dso, int parm), int parm)
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001453{
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001454 return machines__fprintf_dsos_buildid(&self->machines, fp, skip, parm);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001455}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001456
1457size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1458{
1459 struct perf_evsel *pos;
1460 size_t ret = fprintf(fp, "Aggregated stats:\n");
1461
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001462 ret += events_stats__fprintf(&session->stats, fp);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001463
1464 list_for_each_entry(pos, &session->evlist->entries, node) {
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -03001465 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
Arnaldo Carvalho de Melo52168ee2012-12-18 16:02:17 -03001466 ret += events_stats__fprintf(&pos->hists.stats, fp);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001467 }
1468
1469 return ret;
1470}
David Ahernc0230b22011-03-09 22:23:27 -07001471
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001472size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1473{
1474 /*
1475 * FIXME: Here we have to actually print all the machines in this
1476 * session, not just the host...
1477 */
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001478 return machine__fprintf(&session->machines.host, fp);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001479}
1480
David Ahern9cbdb702011-04-06 21:54:20 -06001481struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1482 unsigned int type)
1483{
1484 struct perf_evsel *pos;
1485
1486 list_for_each_entry(pos, &session->evlist->entries, node) {
1487 if (pos->attr.type == type)
1488 return pos;
1489 }
1490 return NULL;
1491}
1492
Jiri Olsa71ad0f52012-08-07 15:20:46 +02001493void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
1494 struct perf_sample *sample, struct machine *machine,
David Ahern307cbb92013-08-07 22:50:53 -04001495 unsigned int print_opts, unsigned int stack_depth)
David Ahernc0230b22011-03-09 22:23:27 -07001496{
1497 struct addr_location al;
David Ahernc0230b22011-03-09 22:23:27 -07001498 struct callchain_cursor_node *node;
David Aherna6ffaf92013-08-07 22:50:51 -04001499 int print_ip = print_opts & PRINT_IP_OPT_IP;
1500 int print_sym = print_opts & PRINT_IP_OPT_SYM;
1501 int print_dso = print_opts & PRINT_IP_OPT_DSO;
1502 int print_symoffset = print_opts & PRINT_IP_OPT_SYMOFFSET;
David Ahernb0b35f02013-08-07 22:50:52 -04001503 int print_oneline = print_opts & PRINT_IP_OPT_ONELINE;
1504 char s = print_oneline ? ' ' : '\t';
David Ahernc0230b22011-03-09 22:23:27 -07001505
Adrian Huntere44baa32013-08-08 14:32:25 +03001506 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
David Ahernc0230b22011-03-09 22:23:27 -07001507 error("problem processing %d event, skipping it.\n",
1508 event->header.type);
1509 return;
1510 }
1511
1512 if (symbol_conf.use_callchain && sample->callchain) {
1513
Jiri Olsa71ad0f52012-08-07 15:20:46 +02001514 if (machine__resolve_callchain(machine, evsel, al.thread,
Waiman Long91e95612013-10-18 10:38:48 -04001515 sample, NULL, NULL,
1516 PERF_MAX_STACK_DEPTH) != 0) {
David Ahernc0230b22011-03-09 22:23:27 -07001517 if (verbose)
1518 error("Failed to resolve callchain. Skipping\n");
1519 return;
1520 }
Namhyung Kim47260642012-05-31 14:43:26 +09001521 callchain_cursor_commit(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001522
David Ahern307cbb92013-08-07 22:50:53 -04001523 while (stack_depth) {
Namhyung Kim47260642012-05-31 14:43:26 +09001524 node = callchain_cursor_current(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001525 if (!node)
1526 break;
1527
David Aherna6ffaf92013-08-07 22:50:51 -04001528 if (print_ip)
David Ahernb0b35f02013-08-07 22:50:52 -04001529 printf("%c%16" PRIx64, s, node->ip);
David Aherna6ffaf92013-08-07 22:50:51 -04001530
David Ahern787bef12011-05-27 14:28:43 -06001531 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001532 printf(" ");
David Ahern251f4262013-07-28 09:14:34 -06001533 if (print_symoffset) {
1534 al.addr = node->ip;
David Ahern0b8c25d2013-07-28 09:48:32 -06001535 al.map = node->map;
David Ahern251f4262013-07-28 09:14:34 -06001536 symbol__fprintf_symname_offs(node->sym, &al, stdout);
1537 } else
1538 symbol__fprintf_symname(node->sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001539 }
David Ahern251f4262013-07-28 09:14:34 -06001540
David Ahern610723f2011-05-27 14:28:44 -06001541 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001542 printf(" (");
David Ahern52deff72012-05-29 22:58:26 -06001543 map__fprintf_dsoname(node->map, stdout);
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001544 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001545 }
David Ahernb0b35f02013-08-07 22:50:52 -04001546
1547 if (!print_oneline)
1548 printf("\n");
David Ahernc0230b22011-03-09 22:23:27 -07001549
Namhyung Kim47260642012-05-31 14:43:26 +09001550 callchain_cursor_advance(&callchain_cursor);
David Ahern307cbb92013-08-07 22:50:53 -04001551
1552 stack_depth--;
David Ahernc0230b22011-03-09 22:23:27 -07001553 }
1554
1555 } else {
David Aherna6ffaf92013-08-07 22:50:51 -04001556 if (print_ip)
1557 printf("%16" PRIx64, sample->ip);
1558
David Ahern787bef12011-05-27 14:28:43 -06001559 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001560 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001561 if (print_symoffset)
1562 symbol__fprintf_symname_offs(al.sym, &al,
1563 stdout);
1564 else
1565 symbol__fprintf_symname(al.sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001566 }
1567
1568 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001569 printf(" (");
1570 map__fprintf_dsoname(al.map, stdout);
1571 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001572 }
David Ahernc0230b22011-03-09 22:23:27 -07001573 }
1574}
Anton Blanchard5d67be92011-07-04 21:57:50 +10001575
1576int perf_session__cpu_bitmap(struct perf_session *session,
1577 const char *cpu_list, unsigned long *cpu_bitmap)
1578{
1579 int i;
1580 struct cpu_map *map;
1581
1582 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1583 struct perf_evsel *evsel;
1584
1585 evsel = perf_session__find_first_evtype(session, i);
1586 if (!evsel)
1587 continue;
1588
1589 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1590 pr_err("File does not contain CPU events. "
1591 "Remove -c option to proceed.\n");
1592 return -1;
1593 }
1594 }
1595
1596 map = cpu_map__new(cpu_list);
David Ahern47fbe532011-11-13 10:45:27 -07001597 if (map == NULL) {
1598 pr_err("Invalid cpu_list\n");
1599 return -1;
1600 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001601
1602 for (i = 0; i < map->nr; i++) {
1603 int cpu = map->map[i];
1604
1605 if (cpu >= MAX_NR_CPUS) {
1606 pr_err("Requested CPU %d too large. "
1607 "Consider raising MAX_NR_CPUS\n", cpu);
1608 return -1;
1609 }
1610
1611 set_bit(cpu, cpu_bitmap);
1612 }
1613
1614 return 0;
1615}
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001616
1617void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1618 bool full)
1619{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001620 int fd = perf_data_file__fd(session->file);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001621 struct stat st;
1622 int ret;
1623
1624 if (session == NULL || fp == NULL)
1625 return;
1626
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001627 ret = fstat(fd, &st);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001628 if (ret == -1)
1629 return;
1630
1631 fprintf(fp, "# ========\n");
1632 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1633 perf_header__fprintf_info(session, fp, full);
1634 fprintf(fp, "# ========\n#\n");
1635}
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001636
1637
1638int __perf_session__set_tracepoints_handlers(struct perf_session *session,
1639 const struct perf_evsel_str_handler *assocs,
1640 size_t nr_assocs)
1641{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001642 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001643 size_t i;
1644 int err;
1645
1646 for (i = 0; i < nr_assocs; i++) {
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001647 /*
1648 * Adding a handler for an event not in the session,
1649 * just ignore it.
1650 */
1651 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name);
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001652 if (evsel == NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001653 continue;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001654
1655 err = -EEXIST;
1656 if (evsel->handler.func != NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001657 goto out;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001658 evsel->handler.func = assocs[i].handler;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001659 }
1660
1661 err = 0;
1662out:
1663 return err;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001664}