blob: 663a2fdab42cbe67ceb8565b6253b6ddca75abf0 [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 Olsab0a45202014-06-12 09:50:11 +020017#include "asm/bug.h"
Adrian Hunterc4468702015-04-09 18:53:48 +030018#include "auxtrace.h"
Adrian Huntera5499b32015-05-29 16:33:30 +030019#include "thread-stack.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020020
Adrian Hunterc4468702015-04-09 18:53:48 +030021static int perf_session__deliver_event(struct perf_session *session,
22 union perf_event *event,
23 struct perf_sample *sample,
24 struct perf_tool *tool,
25 u64 file_offset);
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -030026
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030027static int perf_session__open(struct perf_session *session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020028{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030029 struct perf_data_file *file = session->file;
Tom Zanussi8dc58102010-04-01 23:59:15 -050030
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030031 if (perf_session__read_header(session) < 0) {
Arnaldo Carvalho de Meloe87b4912015-11-09 17:12:03 -030032 pr_err("incompatible file format (rerun with -v to learn more)\n");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020033 return -1;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020034 }
35
Jiri Olsacc9784bd2013-10-15 16:27:34 +020036 if (perf_data_file__is_pipe(file))
37 return 0;
38
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030039 if (!perf_evlist__valid_sample_type(session->evlist)) {
Arnaldo Carvalho de Meloe87b4912015-11-09 17:12:03 -030040 pr_err("non matching sample_type\n");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020041 return -1;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030042 }
43
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030044 if (!perf_evlist__valid_sample_id_all(session->evlist)) {
Arnaldo Carvalho de Meloe87b4912015-11-09 17:12:03 -030045 pr_err("non matching sample_id_all\n");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020046 return -1;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030047 }
48
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030049 if (!perf_evlist__valid_read_format(session->evlist)) {
Arnaldo Carvalho de Meloe87b4912015-11-09 17:12:03 -030050 pr_err("non matching read_format\n");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020051 return -1;
Jiri Olsa9ede4732012-10-10 17:38:13 +020052 }
53
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020054 return 0;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020055}
56
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030057void perf_session__set_id_hdr_size(struct perf_session *session)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020058{
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030059 u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist);
60
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030061 machines__set_id_hdr_size(&session->machines, id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020062}
63
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030064int perf_session__create_kernel_maps(struct perf_session *session)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080065{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030066 int ret = machine__create_kernel_maps(&session->machines.host);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080067
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080068 if (ret >= 0)
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030069 ret = machines__create_guest_kernel_maps(&session->machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080070 return ret;
71}
72
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030073static void perf_session__destroy_kernel_maps(struct perf_session *session)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -030074{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030075 machines__destroy_kernel_maps(&session->machines);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -030076}
77
Adrian Huntercfe1c412014-07-31 09:00:45 +030078static bool perf_session__has_comm_exec(struct perf_session *session)
79{
80 struct perf_evsel *evsel;
81
82 evlist__for_each(session->evlist, evsel) {
83 if (evsel->attr.comm_exec)
84 return true;
85 }
86
87 return false;
88}
89
90static void perf_session__set_comm_exec(struct perf_session *session)
91{
92 bool comm_exec = perf_session__has_comm_exec(session);
93
94 machines__set_comm_exec(&session->machines, comm_exec);
95}
96
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -030097static int ordered_events__deliver_event(struct ordered_events *oe,
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -030098 struct ordered_event *event)
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -030099{
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300100 struct perf_sample sample;
101 struct perf_session *session = container_of(oe, struct perf_session,
102 ordered_events);
103 int ret = perf_evlist__parse_sample(session->evlist, event->event, &sample);
104
105 if (ret) {
106 pr_err("Can't parse sample, err = %d\n", ret);
107 return ret;
108 }
109
Adrian Hunterc4468702015-04-09 18:53:48 +0300110 return perf_session__deliver_event(session, event->event, &sample,
111 session->tool, event->file_offset);
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300112}
113
Jiri Olsaf5fc14122013-10-15 16:27:32 +0200114struct perf_session *perf_session__new(struct perf_data_file *file,
115 bool repipe, struct perf_tool *tool)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200116{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300117 struct perf_session *session = zalloc(sizeof(*session));
Robert Richterefad1412011-12-07 10:02:54 +0100118
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300119 if (!session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200120 goto out;
121
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300122 session->repipe = repipe;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300123 session->tool = tool;
Adrian Hunter99fa2982015-04-30 17:37:25 +0300124 INIT_LIST_HEAD(&session->auxtrace_index);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300125 machines__init(&session->machines);
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300126 ordered_events__init(&session->ordered_events, ordered_events__deliver_event);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200127
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200128 if (file) {
129 if (perf_data_file__open(file))
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200130 goto out_delete;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200131
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300132 session->file = file;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200133
134 if (perf_data_file__is_read(file)) {
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300135 if (perf_session__open(session) < 0)
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200136 goto out_close;
137
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300138 perf_session__set_id_hdr_size(session);
Adrian Huntercfe1c412014-07-31 09:00:45 +0300139 perf_session__set_comm_exec(session);
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200140 }
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -0300141 } else {
142 session->machines.host.env = &perf_env;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200143 }
144
145 if (!file || perf_data_file__is_write(file)) {
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200146 /*
147 * In O_RDONLY mode this will be performed when reading the
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200148 * kernel MMAP event, in perf_event__process_mmap().
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200149 */
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300150 if (perf_session__create_kernel_maps(session) < 0)
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700151 pr_warning("Cannot read kernel map\n");
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200152 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200153
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200154 if (tool && tool->ordering_requires_timestamps &&
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200155 tool->ordered_events && !perf_evlist__sample_id_all(session->evlist)) {
Ian Munsie21ef97f2010-12-10 14:09:16 +1100156 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200157 tool->ordered_events = false;
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300158 }
Ian Munsie21ef97f2010-12-10 14:09:16 +1100159
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300160 return session;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200161
162 out_close:
163 perf_data_file__close(file);
164 out_delete:
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300165 perf_session__delete(session);
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200166 out:
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200167 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200168}
169
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200170static void perf_session__delete_threads(struct perf_session *session)
171{
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300172 machine__delete_threads(&session->machines.host);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200173}
174
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300175void perf_session__delete(struct perf_session *session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200176{
Adrian Hunterc4468702015-04-09 18:53:48 +0300177 auxtrace__free(session);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300178 auxtrace_index__free(&session->auxtrace_index);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300179 perf_session__destroy_kernel_maps(session);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300180 perf_session__delete_threads(session);
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -0300181 perf_env__exit(&session->header.env);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300182 machines__exit(&session->machines);
183 if (session->file)
184 perf_data_file__close(session->file);
185 free(session);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200186}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200187
Adrian Hunter47c3d102013-07-04 16:20:21 +0300188static int process_event_synth_tracing_data_stub(struct perf_tool *tool
189 __maybe_unused,
190 union perf_event *event
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300191 __maybe_unused,
192 struct perf_session *session
193 __maybe_unused)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200194{
195 dump_printf(": unhandled!\n");
196 return 0;
197}
198
Adrian Hunter47c3d102013-07-04 16:20:21 +0300199static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused,
200 union perf_event *event __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300201 struct perf_evlist **pevlist
202 __maybe_unused)
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200203{
204 dump_printf(": unhandled!\n");
205 return 0;
206}
207
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300208static int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
209 union perf_event *event __maybe_unused,
210 struct perf_sample *sample __maybe_unused,
211 struct perf_evsel *evsel __maybe_unused,
212 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300213{
214 dump_printf(": unhandled!\n");
215 return 0;
216}
217
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300218static int process_event_stub(struct perf_tool *tool __maybe_unused,
219 union perf_event *event __maybe_unused,
220 struct perf_sample *sample __maybe_unused,
221 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200222{
223 dump_printf(": unhandled!\n");
224 return 0;
225}
226
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300227static int process_build_id_stub(struct perf_tool *tool __maybe_unused,
228 union perf_event *event __maybe_unused,
229 struct perf_session *session __maybe_unused)
230{
231 dump_printf(": unhandled!\n");
232 return 0;
233}
234
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300235static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
236 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300237 struct ordered_events *oe __maybe_unused)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200238{
239 dump_printf(": unhandled!\n");
240 return 0;
241}
242
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200243static int process_finished_round(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200244 union perf_event *event,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300245 struct ordered_events *oe);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200246
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200247static int process_id_index_stub(struct perf_tool *tool __maybe_unused,
248 union perf_event *event __maybe_unused,
249 struct perf_session *perf_session
250 __maybe_unused)
251{
252 dump_printf(": unhandled!\n");
253 return 0;
254}
255
Adrian Huntera16ac022015-04-09 18:53:43 +0300256static int process_event_auxtrace_info_stub(struct perf_tool *tool __maybe_unused,
257 union perf_event *event __maybe_unused,
258 struct perf_session *session __maybe_unused)
259{
260 dump_printf(": unhandled!\n");
261 return 0;
262}
263
264static int skipn(int fd, off_t n)
265{
266 char buf[4096];
267 ssize_t ret;
268
269 while (n > 0) {
270 ret = read(fd, buf, min(n, (off_t)sizeof(buf)));
271 if (ret <= 0)
272 return ret;
273 n -= ret;
274 }
275
276 return 0;
277}
278
279static s64 process_event_auxtrace_stub(struct perf_tool *tool __maybe_unused,
280 union perf_event *event,
281 struct perf_session *session
282 __maybe_unused)
283{
284 dump_printf(": unhandled!\n");
285 if (perf_data_file__is_pipe(session->file))
286 skipn(perf_data_file__fd(session->file), event->auxtrace.size);
287 return event->auxtrace.size;
288}
289
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300290static
291int process_event_auxtrace_error_stub(struct perf_tool *tool __maybe_unused,
292 union perf_event *event __maybe_unused,
293 struct perf_session *session __maybe_unused)
294{
295 dump_printf(": unhandled!\n");
296 return 0;
297}
298
Jiri Olsa5f3339d2015-10-25 15:51:19 +0100299
300static
301int process_event_thread_map_stub(struct perf_tool *tool __maybe_unused,
302 union perf_event *event __maybe_unused,
303 struct perf_session *session __maybe_unused)
304{
305 dump_printf(": unhandled!\n");
306 return 0;
307}
308
Jiri Olsa6640b6c2015-10-25 15:51:23 +0100309static
310int process_event_cpu_map_stub(struct perf_tool *tool __maybe_unused,
311 union perf_event *event __maybe_unused,
312 struct perf_session *session __maybe_unused)
313{
314 dump_printf(": unhandled!\n");
315 return 0;
316}
317
Jiri Olsa374fb9e2015-10-25 15:51:27 +0100318static
319int process_event_stat_config_stub(struct perf_tool *tool __maybe_unused,
320 union perf_event *event __maybe_unused,
321 struct perf_session *session __maybe_unused)
322{
323 dump_printf(": unhandled!\n");
324 return 0;
325}
326
Jiri Olsad80518c2015-10-25 15:51:30 +0100327static int process_stat_stub(struct perf_tool *tool __maybe_unused,
328 union perf_event *event __maybe_unused,
329 struct perf_session *perf_session
330 __maybe_unused)
331{
332 dump_printf(": unhandled!\n");
333 return 0;
334}
335
David Ahern9c501402013-08-02 14:05:41 -0600336void perf_tool__fill_defaults(struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200337{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200338 if (tool->sample == NULL)
339 tool->sample = process_event_sample_stub;
340 if (tool->mmap == NULL)
341 tool->mmap = process_event_stub;
David Ahern6adb0b02013-09-22 19:44:59 -0600342 if (tool->mmap2 == NULL)
343 tool->mmap2 = process_event_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200344 if (tool->comm == NULL)
345 tool->comm = process_event_stub;
346 if (tool->fork == NULL)
347 tool->fork = process_event_stub;
348 if (tool->exit == NULL)
349 tool->exit = process_event_stub;
350 if (tool->lost == NULL)
351 tool->lost = perf_event__process_lost;
Kan Liangc4937a92015-05-10 15:13:15 -0400352 if (tool->lost_samples == NULL)
353 tool->lost_samples = perf_event__process_lost_samples;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300354 if (tool->aux == NULL)
355 tool->aux = perf_event__process_aux;
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300356 if (tool->itrace_start == NULL)
357 tool->itrace_start = perf_event__process_itrace_start;
Adrian Hunter02860392015-07-21 12:44:03 +0300358 if (tool->context_switch == NULL)
359 tool->context_switch = perf_event__process_switch;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200360 if (tool->read == NULL)
361 tool->read = process_event_sample_stub;
362 if (tool->throttle == NULL)
363 tool->throttle = process_event_stub;
364 if (tool->unthrottle == NULL)
365 tool->unthrottle = process_event_stub;
366 if (tool->attr == NULL)
367 tool->attr = process_event_synth_attr_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200368 if (tool->tracing_data == NULL)
369 tool->tracing_data = process_event_synth_tracing_data_stub;
370 if (tool->build_id == NULL)
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300371 tool->build_id = process_build_id_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200372 if (tool->finished_round == NULL) {
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200373 if (tool->ordered_events)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200374 tool->finished_round = process_finished_round;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200375 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200376 tool->finished_round = process_finished_round_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200377 }
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200378 if (tool->id_index == NULL)
379 tool->id_index = process_id_index_stub;
Adrian Huntera16ac022015-04-09 18:53:43 +0300380 if (tool->auxtrace_info == NULL)
381 tool->auxtrace_info = process_event_auxtrace_info_stub;
382 if (tool->auxtrace == NULL)
383 tool->auxtrace = process_event_auxtrace_stub;
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300384 if (tool->auxtrace_error == NULL)
385 tool->auxtrace_error = process_event_auxtrace_error_stub;
Jiri Olsa5f3339d2015-10-25 15:51:19 +0100386 if (tool->thread_map == NULL)
387 tool->thread_map = process_event_thread_map_stub;
Jiri Olsa6640b6c2015-10-25 15:51:23 +0100388 if (tool->cpu_map == NULL)
389 tool->cpu_map = process_event_cpu_map_stub;
Jiri Olsa374fb9e2015-10-25 15:51:27 +0100390 if (tool->stat_config == NULL)
391 tool->stat_config = process_event_stat_config_stub;
Jiri Olsad80518c2015-10-25 15:51:30 +0100392 if (tool->stat == NULL)
393 tool->stat = process_stat_stub;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200394}
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300395
Jiri Olsa268fb202012-05-30 14:23:43 +0200396static void swap_sample_id_all(union perf_event *event, void *data)
397{
398 void *end = (void *) event + event->header.size;
399 int size = end - data;
400
401 BUG_ON(size % sizeof(u64));
402 mem_bswap_64(data, size);
403}
404
405static void perf_event__all64_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300406 bool sample_id_all __maybe_unused)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200407{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200408 struct perf_event_header *hdr = &event->header;
409 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200410}
411
Jiri Olsa268fb202012-05-30 14:23:43 +0200412static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200413{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200414 event->comm.pid = bswap_32(event->comm.pid);
415 event->comm.tid = bswap_32(event->comm.tid);
Jiri Olsa268fb202012-05-30 14:23:43 +0200416
417 if (sample_id_all) {
418 void *data = &event->comm.comm;
419
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300420 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200421 swap_sample_id_all(event, data);
422 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200423}
424
Jiri Olsa268fb202012-05-30 14:23:43 +0200425static void perf_event__mmap_swap(union perf_event *event,
426 bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200427{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200428 event->mmap.pid = bswap_32(event->mmap.pid);
429 event->mmap.tid = bswap_32(event->mmap.tid);
430 event->mmap.start = bswap_64(event->mmap.start);
431 event->mmap.len = bswap_64(event->mmap.len);
432 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
Jiri Olsa268fb202012-05-30 14:23:43 +0200433
434 if (sample_id_all) {
435 void *data = &event->mmap.filename;
436
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300437 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200438 swap_sample_id_all(event, data);
439 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200440}
441
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200442static void perf_event__mmap2_swap(union perf_event *event,
443 bool sample_id_all)
444{
445 event->mmap2.pid = bswap_32(event->mmap2.pid);
446 event->mmap2.tid = bswap_32(event->mmap2.tid);
447 event->mmap2.start = bswap_64(event->mmap2.start);
448 event->mmap2.len = bswap_64(event->mmap2.len);
449 event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
450 event->mmap2.maj = bswap_32(event->mmap2.maj);
451 event->mmap2.min = bswap_32(event->mmap2.min);
452 event->mmap2.ino = bswap_64(event->mmap2.ino);
453
454 if (sample_id_all) {
455 void *data = &event->mmap2.filename;
456
457 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
458 swap_sample_id_all(event, data);
459 }
460}
Jiri Olsa268fb202012-05-30 14:23:43 +0200461static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200462{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200463 event->fork.pid = bswap_32(event->fork.pid);
464 event->fork.tid = bswap_32(event->fork.tid);
465 event->fork.ppid = bswap_32(event->fork.ppid);
466 event->fork.ptid = bswap_32(event->fork.ptid);
467 event->fork.time = bswap_64(event->fork.time);
Jiri Olsa268fb202012-05-30 14:23:43 +0200468
469 if (sample_id_all)
470 swap_sample_id_all(event, &event->fork + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200471}
472
Jiri Olsa268fb202012-05-30 14:23:43 +0200473static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200474{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200475 event->read.pid = bswap_32(event->read.pid);
476 event->read.tid = bswap_32(event->read.tid);
477 event->read.value = bswap_64(event->read.value);
478 event->read.time_enabled = bswap_64(event->read.time_enabled);
479 event->read.time_running = bswap_64(event->read.time_running);
480 event->read.id = bswap_64(event->read.id);
Jiri Olsa268fb202012-05-30 14:23:43 +0200481
482 if (sample_id_all)
483 swap_sample_id_all(event, &event->read + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200484}
485
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300486static void perf_event__aux_swap(union perf_event *event, bool sample_id_all)
487{
488 event->aux.aux_offset = bswap_64(event->aux.aux_offset);
489 event->aux.aux_size = bswap_64(event->aux.aux_size);
490 event->aux.flags = bswap_64(event->aux.flags);
491
492 if (sample_id_all)
493 swap_sample_id_all(event, &event->aux + 1);
494}
495
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300496static void perf_event__itrace_start_swap(union perf_event *event,
497 bool sample_id_all)
498{
499 event->itrace_start.pid = bswap_32(event->itrace_start.pid);
500 event->itrace_start.tid = bswap_32(event->itrace_start.tid);
501
502 if (sample_id_all)
503 swap_sample_id_all(event, &event->itrace_start + 1);
504}
505
Adrian Hunter02860392015-07-21 12:44:03 +0300506static void perf_event__switch_swap(union perf_event *event, bool sample_id_all)
507{
508 if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
509 event->context_switch.next_prev_pid =
510 bswap_32(event->context_switch.next_prev_pid);
511 event->context_switch.next_prev_tid =
512 bswap_32(event->context_switch.next_prev_tid);
513 }
514
515 if (sample_id_all)
516 swap_sample_id_all(event, &event->context_switch + 1);
517}
518
Jiri Olsadd96c462013-09-01 12:36:15 +0200519static void perf_event__throttle_swap(union perf_event *event,
520 bool sample_id_all)
521{
522 event->throttle.time = bswap_64(event->throttle.time);
523 event->throttle.id = bswap_64(event->throttle.id);
524 event->throttle.stream_id = bswap_64(event->throttle.stream_id);
525
526 if (sample_id_all)
527 swap_sample_id_all(event, &event->throttle + 1);
528}
529
Jiri Olsae108c662012-05-16 08:59:03 +0200530static u8 revbyte(u8 b)
531{
532 int rev = (b >> 4) | ((b & 0xf) << 4);
533 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
534 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
535 return (u8) rev;
536}
537
538/*
539 * XXX this is hack in attempt to carry flags bitfield
540 * throught endian village. ABI says:
541 *
542 * Bit-fields are allocated from right to left (least to most significant)
543 * on little-endian implementations and from left to right (most to least
544 * significant) on big-endian implementations.
545 *
546 * The above seems to be byte specific, so we need to reverse each
547 * byte of the bitfield. 'Internet' also says this might be implementation
548 * specific and we probably need proper fix and carry perf_event_attr
549 * bitfield flags in separate data file FEAT_ section. Thought this seems
550 * to work for now.
551 */
552static void swap_bitfield(u8 *p, unsigned len)
553{
554 unsigned i;
555
556 for (i = 0; i < len; i++) {
557 *p = revbyte(*p);
558 p++;
559 }
560}
561
David Aherneda39132011-07-15 12:34:09 -0600562/* exported for swapping attributes in file header */
563void perf_event__attr_swap(struct perf_event_attr *attr)
564{
565 attr->type = bswap_32(attr->type);
566 attr->size = bswap_32(attr->size);
Jiri Olsae108c662012-05-16 08:59:03 +0200567
Wang Nanb30b6172015-06-17 09:56:39 +0000568#define bswap_safe(f, n) \
569 (attr->size > (offsetof(struct perf_event_attr, f) + \
570 sizeof(attr->f) * (n)))
571#define bswap_field(f, sz) \
572do { \
573 if (bswap_safe(f, 0)) \
574 attr->f = bswap_##sz(attr->f); \
575} while(0)
576#define bswap_field_32(f) bswap_field(f, 32)
577#define bswap_field_64(f) bswap_field(f, 64)
578
579 bswap_field_64(config);
580 bswap_field_64(sample_period);
581 bswap_field_64(sample_type);
582 bswap_field_64(read_format);
583 bswap_field_32(wakeup_events);
584 bswap_field_32(bp_type);
585 bswap_field_64(bp_addr);
586 bswap_field_64(bp_len);
587 bswap_field_64(branch_sample_type);
588 bswap_field_64(sample_regs_user);
589 bswap_field_32(sample_stack_user);
590 bswap_field_32(aux_watermark);
591
592 /*
593 * After read_format are bitfields. Check read_format because
594 * we are unable to use offsetof on bitfield.
595 */
596 if (bswap_safe(read_format, 1))
597 swap_bitfield((u8 *) (&attr->read_format + 1),
598 sizeof(u64));
599#undef bswap_field_64
600#undef bswap_field_32
601#undef bswap_field
602#undef bswap_safe
David Aherneda39132011-07-15 12:34:09 -0600603}
604
Jiri Olsa268fb202012-05-30 14:23:43 +0200605static void perf_event__hdr_attr_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300606 bool sample_id_all __maybe_unused)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500607{
608 size_t size;
609
David Aherneda39132011-07-15 12:34:09 -0600610 perf_event__attr_swap(&event->attr.attr);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500611
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200612 size = event->header.size;
613 size -= (void *)&event->attr.id - (void *)event;
614 mem_bswap_64(event->attr.id, size);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500615}
616
Jiri Olsa268fb202012-05-30 14:23:43 +0200617static void perf_event__event_type_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300618 bool sample_id_all __maybe_unused)
Tom Zanussicd19a032010-04-01 23:59:20 -0500619{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200620 event->event_type.event_type.event_id =
621 bswap_64(event->event_type.event_type.event_id);
Tom Zanussicd19a032010-04-01 23:59:20 -0500622}
623
Jiri Olsa268fb202012-05-30 14:23:43 +0200624static void perf_event__tracing_data_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300625 bool sample_id_all __maybe_unused)
Tom Zanussi92155452010-04-01 23:59:21 -0500626{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200627 event->tracing_data.size = bswap_32(event->tracing_data.size);
Tom Zanussi92155452010-04-01 23:59:21 -0500628}
629
Adrian Huntera16ac022015-04-09 18:53:43 +0300630static void perf_event__auxtrace_info_swap(union perf_event *event,
631 bool sample_id_all __maybe_unused)
632{
633 size_t size;
634
635 event->auxtrace_info.type = bswap_32(event->auxtrace_info.type);
636
637 size = event->header.size;
638 size -= (void *)&event->auxtrace_info.priv - (void *)event;
639 mem_bswap_64(event->auxtrace_info.priv, size);
640}
641
642static void perf_event__auxtrace_swap(union perf_event *event,
643 bool sample_id_all __maybe_unused)
644{
645 event->auxtrace.size = bswap_64(event->auxtrace.size);
646 event->auxtrace.offset = bswap_64(event->auxtrace.offset);
647 event->auxtrace.reference = bswap_64(event->auxtrace.reference);
648 event->auxtrace.idx = bswap_32(event->auxtrace.idx);
649 event->auxtrace.tid = bswap_32(event->auxtrace.tid);
650 event->auxtrace.cpu = bswap_32(event->auxtrace.cpu);
651}
652
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300653static void perf_event__auxtrace_error_swap(union perf_event *event,
654 bool sample_id_all __maybe_unused)
655{
656 event->auxtrace_error.type = bswap_32(event->auxtrace_error.type);
657 event->auxtrace_error.code = bswap_32(event->auxtrace_error.code);
658 event->auxtrace_error.cpu = bswap_32(event->auxtrace_error.cpu);
659 event->auxtrace_error.pid = bswap_32(event->auxtrace_error.pid);
660 event->auxtrace_error.tid = bswap_32(event->auxtrace_error.tid);
661 event->auxtrace_error.ip = bswap_64(event->auxtrace_error.ip);
662}
663
Jiri Olsa5f3339d2015-10-25 15:51:19 +0100664static void perf_event__thread_map_swap(union perf_event *event,
665 bool sample_id_all __maybe_unused)
666{
667 unsigned i;
668
669 event->thread_map.nr = bswap_64(event->thread_map.nr);
670
671 for (i = 0; i < event->thread_map.nr; i++)
672 event->thread_map.entries[i].pid = bswap_64(event->thread_map.entries[i].pid);
673}
674
Jiri Olsa6640b6c2015-10-25 15:51:23 +0100675static void perf_event__cpu_map_swap(union perf_event *event,
676 bool sample_id_all __maybe_unused)
677{
678 struct cpu_map_data *data = &event->cpu_map.data;
679 struct cpu_map_entries *cpus;
680 struct cpu_map_mask *mask;
681 unsigned i;
682
683 data->type = bswap_64(data->type);
684
685 switch (data->type) {
686 case PERF_CPU_MAP__CPUS:
687 cpus = (struct cpu_map_entries *)data->data;
688
689 cpus->nr = bswap_16(cpus->nr);
690
691 for (i = 0; i < cpus->nr; i++)
692 cpus->cpu[i] = bswap_16(cpus->cpu[i]);
693 break;
694 case PERF_CPU_MAP__MASK:
695 mask = (struct cpu_map_mask *) data->data;
696
697 mask->nr = bswap_16(mask->nr);
698 mask->long_size = bswap_16(mask->long_size);
699
700 switch (mask->long_size) {
701 case 4: mem_bswap_32(&mask->mask, mask->nr); break;
702 case 8: mem_bswap_64(&mask->mask, mask->nr); break;
703 default:
704 pr_err("cpu_map swap: unsupported long size\n");
705 }
706 default:
707 break;
708 }
709}
710
Jiri Olsa374fb9e2015-10-25 15:51:27 +0100711static void perf_event__stat_config_swap(union perf_event *event,
712 bool sample_id_all __maybe_unused)
713{
714 u64 size;
715
716 size = event->stat_config.nr * sizeof(event->stat_config.data[0]);
717 size += 1; /* nr item itself */
718 mem_bswap_64(&event->stat_config.nr, size);
719}
720
Jiri Olsad80518c2015-10-25 15:51:30 +0100721static void perf_event__stat_swap(union perf_event *event,
722 bool sample_id_all __maybe_unused)
723{
724 event->stat.id = bswap_64(event->stat.id);
725 event->stat.thread = bswap_32(event->stat.thread);
726 event->stat.cpu = bswap_32(event->stat.cpu);
727 event->stat.val = bswap_64(event->stat.val);
728 event->stat.ena = bswap_64(event->stat.ena);
729 event->stat.run = bswap_64(event->stat.run);
730}
731
Jiri Olsa268fb202012-05-30 14:23:43 +0200732typedef void (*perf_event__swap_op)(union perf_event *event,
733 bool sample_id_all);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200734
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200735static perf_event__swap_op perf_event__swap_ops[] = {
736 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200737 [PERF_RECORD_MMAP2] = perf_event__mmap2_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200738 [PERF_RECORD_COMM] = perf_event__comm_swap,
739 [PERF_RECORD_FORK] = perf_event__task_swap,
740 [PERF_RECORD_EXIT] = perf_event__task_swap,
741 [PERF_RECORD_LOST] = perf_event__all64_swap,
742 [PERF_RECORD_READ] = perf_event__read_swap,
Jiri Olsadd96c462013-09-01 12:36:15 +0200743 [PERF_RECORD_THROTTLE] = perf_event__throttle_swap,
744 [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200745 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300746 [PERF_RECORD_AUX] = perf_event__aux_swap,
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300747 [PERF_RECORD_ITRACE_START] = perf_event__itrace_start_swap,
Kan Liangc4937a92015-05-10 15:13:15 -0400748 [PERF_RECORD_LOST_SAMPLES] = perf_event__all64_swap,
Adrian Hunter02860392015-07-21 12:44:03 +0300749 [PERF_RECORD_SWITCH] = perf_event__switch_swap,
750 [PERF_RECORD_SWITCH_CPU_WIDE] = perf_event__switch_swap,
David Aherneda39132011-07-15 12:34:09 -0600751 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200752 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
753 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
754 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200755 [PERF_RECORD_ID_INDEX] = perf_event__all64_swap,
Adrian Huntera16ac022015-04-09 18:53:43 +0300756 [PERF_RECORD_AUXTRACE_INFO] = perf_event__auxtrace_info_swap,
757 [PERF_RECORD_AUXTRACE] = perf_event__auxtrace_swap,
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300758 [PERF_RECORD_AUXTRACE_ERROR] = perf_event__auxtrace_error_swap,
Jiri Olsa5f3339d2015-10-25 15:51:19 +0100759 [PERF_RECORD_THREAD_MAP] = perf_event__thread_map_swap,
Jiri Olsa6640b6c2015-10-25 15:51:23 +0100760 [PERF_RECORD_CPU_MAP] = perf_event__cpu_map_swap,
Jiri Olsa374fb9e2015-10-25 15:51:27 +0100761 [PERF_RECORD_STAT_CONFIG] = perf_event__stat_config_swap,
Jiri Olsad80518c2015-10-25 15:51:30 +0100762 [PERF_RECORD_STAT] = perf_event__stat_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200763 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200764};
765
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200766/*
767 * When perf record finishes a pass on every buffers, it records this pseudo
768 * event.
769 * We record the max timestamp t found in the pass n.
770 * Assuming these timestamps are monotonic across cpus, we know that if
771 * a buffer still has events with timestamps below t, they will be all
772 * available and then read in the pass n + 1.
773 * Hence when we start to read the pass n + 2, we can safely flush every
774 * events with timestamps below t.
775 *
776 * ============ PASS n =================
777 * CPU 0 | CPU 1
778 * |
779 * cnt1 timestamps | cnt2 timestamps
780 * 1 | 2
781 * 2 | 3
782 * - | 4 <--- max recorded
783 *
784 * ============ PASS n + 1 ==============
785 * CPU 0 | CPU 1
786 * |
787 * cnt1 timestamps | cnt2 timestamps
788 * 3 | 5
789 * 4 | 6
790 * 5 | 7 <---- max recorded
791 *
792 * Flush every events below timestamp 4
793 *
794 * ============ PASS n + 2 ==============
795 * CPU 0 | CPU 1
796 * |
797 * cnt1 timestamps | cnt2 timestamps
798 * 6 | 8
799 * 7 | 9
800 * - | 10
801 *
802 * Flush every events below timestamp 7
803 * etc...
804 */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300805static int process_finished_round(struct perf_tool *tool __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300806 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300807 struct ordered_events *oe)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200808{
Adrian Hunter5531e162015-06-23 10:52:48 +0300809 if (dump_trace)
810 fprintf(stdout, "\n");
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300811 return ordered_events__flush(oe, OE_FLUSH__ROUND);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200812}
813
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300814int perf_session__queue_event(struct perf_session *s, union perf_event *event,
815 struct perf_sample *sample, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200816{
Arnaldo Carvalho de Melo4a6b3622015-03-03 13:02:24 -0300817 return ordered_events__queue(&s->ordered_events, event, sample, file_offset);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200818}
819
Kan Liang384b6052015-01-05 13:23:05 -0500820static void callchain__lbr_callstack_printf(struct perf_sample *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200821{
Kan Liang384b6052015-01-05 13:23:05 -0500822 struct ip_callchain *callchain = sample->callchain;
823 struct branch_stack *lbr_stack = sample->branch_stack;
824 u64 kernel_callchain_nr = callchain->nr;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200825 unsigned int i;
826
Kan Liang384b6052015-01-05 13:23:05 -0500827 for (i = 0; i < kernel_callchain_nr; i++) {
828 if (callchain->ips[i] == PERF_CONTEXT_USER)
829 break;
830 }
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200831
Kan Liang384b6052015-01-05 13:23:05 -0500832 if ((i != kernel_callchain_nr) && lbr_stack->nr) {
833 u64 total_nr;
834 /*
835 * LBR callstack can only get user call chain,
836 * i is kernel call chain number,
837 * 1 is PERF_CONTEXT_USER.
838 *
839 * The user call chain is stored in LBR registers.
840 * LBR are pair registers. The caller is stored
841 * in "from" register, while the callee is stored
842 * in "to" register.
843 * For example, there is a call stack
844 * "A"->"B"->"C"->"D".
845 * The LBR registers will recorde like
846 * "C"->"D", "B"->"C", "A"->"B".
847 * So only the first "to" register and all "from"
848 * registers are needed to construct the whole stack.
849 */
850 total_nr = i + 1 + lbr_stack->nr + 1;
851 kernel_callchain_nr = i + 1;
852
853 printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr);
854
855 for (i = 0; i < kernel_callchain_nr; i++)
856 printf("..... %2d: %016" PRIx64 "\n",
857 i, callchain->ips[i]);
858
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200859 printf("..... %2d: %016" PRIx64 "\n",
Kan Liang384b6052015-01-05 13:23:05 -0500860 (int)(kernel_callchain_nr), lbr_stack->entries[0].to);
861 for (i = 0; i < lbr_stack->nr; i++)
862 printf("..... %2d: %016" PRIx64 "\n",
863 (int)(i + kernel_callchain_nr + 1), lbr_stack->entries[i].from);
864 }
865}
866
867static void callchain__printf(struct perf_evsel *evsel,
868 struct perf_sample *sample)
869{
870 unsigned int i;
871 struct ip_callchain *callchain = sample->callchain;
872
873 if (has_branch_callstack(evsel))
874 callchain__lbr_callstack_printf(sample);
875
876 printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr);
877
878 for (i = 0; i < callchain->nr; i++)
879 printf("..... %2d: %016" PRIx64 "\n",
880 i, callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200881}
882
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100883static void branch_stack__printf(struct perf_sample *sample)
884{
885 uint64_t i;
886
887 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
888
Andi Kleen0e332f02015-07-18 08:24:46 -0700889 for (i = 0; i < sample->branch_stack->nr; i++) {
890 struct branch_entry *e = &sample->branch_stack->entries[i];
891
892 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 " %hu cycles %s%s%s%s %x\n",
893 i, e->from, e->to,
894 e->flags.cycles,
895 e->flags.mispred ? "M" : " ",
896 e->flags.predicted ? "P" : " ",
897 e->flags.abort ? "A" : " ",
898 e->flags.in_tx ? "T" : " ",
899 (unsigned)e->flags.reserved);
900 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100901}
902
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200903static void regs_dump__printf(u64 mask, u64 *regs)
904{
905 unsigned rid, i = 0;
906
907 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
908 u64 val = regs[i++];
909
910 printf(".... %-5s 0x%" PRIx64 "\n",
911 perf_reg_name(rid), val);
912 }
913}
914
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200915static const char *regs_abi[] = {
916 [PERF_SAMPLE_REGS_ABI_NONE] = "none",
917 [PERF_SAMPLE_REGS_ABI_32] = "32-bit",
918 [PERF_SAMPLE_REGS_ABI_64] = "64-bit",
919};
920
921static inline const char *regs_dump_abi(struct regs_dump *d)
922{
923 if (d->abi > PERF_SAMPLE_REGS_ABI_64)
924 return "unknown";
925
926 return regs_abi[d->abi];
927}
928
929static void regs__printf(const char *type, struct regs_dump *regs)
930{
931 u64 mask = regs->mask;
932
933 printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n",
934 type,
935 mask,
936 regs_dump_abi(regs));
937
938 regs_dump__printf(mask, regs->regs);
939}
940
Jiri Olsa352ea452014-01-07 13:47:25 +0100941static void regs_user__printf(struct perf_sample *sample)
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200942{
943 struct regs_dump *user_regs = &sample->user_regs;
944
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200945 if (user_regs->regs)
946 regs__printf("user", user_regs);
947}
948
949static void regs_intr__printf(struct perf_sample *sample)
950{
951 struct regs_dump *intr_regs = &sample->intr_regs;
952
953 if (intr_regs->regs)
954 regs__printf("intr", intr_regs);
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200955}
956
957static void stack_user__printf(struct stack_dump *dump)
958{
959 printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
960 dump->size, dump->offset);
961}
962
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300963static void perf_evlist__print_tstamp(struct perf_evlist *evlist,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200964 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200965 struct perf_sample *sample)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200966{
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300967 u64 sample_type = __perf_evlist__combined_sample_type(evlist);
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300968
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200969 if (event->header.type != PERF_RECORD_SAMPLE &&
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300970 !perf_evlist__sample_id_all(evlist)) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200971 fputs("-1 -1 ", stdout);
972 return;
973 }
974
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300975 if ((sample_type & PERF_SAMPLE_CPU))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200976 printf("%u ", sample->cpu);
977
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300978 if (sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200979 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200980}
981
Jiri Olsa9ede4732012-10-10 17:38:13 +0200982static void sample_read__printf(struct perf_sample *sample, u64 read_format)
983{
984 printf("... sample_read:\n");
985
986 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
987 printf("...... time enabled %016" PRIx64 "\n",
988 sample->read.time_enabled);
989
990 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
991 printf("...... time running %016" PRIx64 "\n",
992 sample->read.time_running);
993
994 if (read_format & PERF_FORMAT_GROUP) {
995 u64 i;
996
997 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
998
999 for (i = 0; i < sample->read.group.nr; i++) {
1000 struct sample_read_value *value;
1001
1002 value = &sample->read.group.values[i];
1003 printf("..... id %016" PRIx64
1004 ", value %016" PRIx64 "\n",
1005 value->id, value->value);
1006 }
1007 } else
1008 printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
1009 sample->read.one.id, sample->read.one.value);
1010}
1011
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001012static void dump_event(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001013 u64 file_offset, struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001014{
1015 if (!dump_trace)
1016 return;
1017
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001018 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
1019 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001020
1021 trace_event(event);
1022
1023 if (sample)
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001024 perf_evlist__print_tstamp(evlist, event, sample);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001025
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001026 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001027 event->header.size, perf_event__name(event->header.type));
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001028}
1029
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001030static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001031 struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001032{
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001033 u64 sample_type;
1034
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -02001035 if (!dump_trace)
1036 return;
1037
Don Zickus0ea590a2014-02-25 22:43:46 -05001038 printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001039 event->header.misc, sample->pid, sample->tid, sample->ip,
David Ahern7cec0922011-05-30 13:08:23 -06001040 sample->period, sample->addr);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001041
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001042 sample_type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001043
1044 if (sample_type & PERF_SAMPLE_CALLCHAIN)
Kan Liang384b6052015-01-05 13:23:05 -05001045 callchain__printf(evsel, sample);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001046
Kan Liang384b6052015-01-05 13:23:05 -05001047 if ((sample_type & PERF_SAMPLE_BRANCH_STACK) && !has_branch_callstack(evsel))
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001048 branch_stack__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001049
1050 if (sample_type & PERF_SAMPLE_REGS_USER)
Jiri Olsa352ea452014-01-07 13:47:25 +01001051 regs_user__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001052
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001053 if (sample_type & PERF_SAMPLE_REGS_INTR)
1054 regs_intr__printf(sample);
1055
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001056 if (sample_type & PERF_SAMPLE_STACK_USER)
1057 stack_user__printf(&sample->user_stack);
Andi Kleen05484292013-01-24 16:10:29 +01001058
1059 if (sample_type & PERF_SAMPLE_WEIGHT)
1060 printf("... weight: %" PRIu64 "\n", sample->weight);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001061
1062 if (sample_type & PERF_SAMPLE_DATA_SRC)
1063 printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001064
Andi Kleen475eeab2013-09-20 07:40:43 -07001065 if (sample_type & PERF_SAMPLE_TRANSACTION)
1066 printf("... transaction: %" PRIx64 "\n", sample->transaction);
1067
Jiri Olsa9ede4732012-10-10 17:38:13 +02001068 if (sample_type & PERF_SAMPLE_READ)
1069 sample_read__printf(sample, evsel->attr.read_format);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001070}
1071
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -03001072static struct machine *machines__find_for_cpumode(struct machines *machines,
Adrian Hunteref893252013-08-27 11:23:06 +03001073 union perf_event *event,
1074 struct perf_sample *sample)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001075{
1076 const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Dongsheng Yangad85ace2013-12-20 13:41:47 -05001077 struct machine *machine;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001078
David Ahern7c0f4a42012-07-20 17:25:48 -06001079 if (perf_guest &&
1080 ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
1081 (cpumode == PERF_RECORD_MISC_GUEST_USER))) {
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +05301082 u32 pid;
1083
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001084 if (event->header.type == PERF_RECORD_MMAP
1085 || event->header.type == PERF_RECORD_MMAP2)
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +05301086 pid = event->mmap.pid;
1087 else
Adrian Hunteref893252013-08-27 11:23:06 +03001088 pid = sample->pid;
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +05301089
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -03001090 machine = machines__find(machines, pid);
Dongsheng Yangad85ace2013-12-20 13:41:47 -05001091 if (!machine)
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -03001092 machine = machines__find(machines, DEFAULT_GUEST_KERNEL_ID);
Dongsheng Yangad85ace2013-12-20 13:41:47 -05001093 return machine;
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +05301094 }
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001095
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -03001096 return &machines->host;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001097}
1098
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001099static int deliver_sample_value(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +02001100 struct perf_tool *tool,
1101 union perf_event *event,
1102 struct perf_sample *sample,
1103 struct sample_read_value *v,
1104 struct machine *machine)
1105{
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001106 struct perf_sample_id *sid = perf_evlist__id2sid(evlist, v->id);
Jiri Olsae4caec02012-10-10 18:52:24 +02001107
Jiri Olsae4caec02012-10-10 18:52:24 +02001108 if (sid) {
1109 sample->id = v->id;
1110 sample->period = v->value - sid->period;
1111 sid->period = v->value;
1112 }
1113
1114 if (!sid || sid->evsel == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001115 ++evlist->stats.nr_unknown_id;
Jiri Olsae4caec02012-10-10 18:52:24 +02001116 return 0;
1117 }
1118
1119 return tool->sample(tool, event, sample, sid->evsel, machine);
1120}
1121
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001122static int deliver_sample_group(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +02001123 struct perf_tool *tool,
1124 union perf_event *event,
1125 struct perf_sample *sample,
1126 struct machine *machine)
1127{
1128 int ret = -EINVAL;
1129 u64 i;
1130
1131 for (i = 0; i < sample->read.group.nr; i++) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001132 ret = deliver_sample_value(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001133 &sample->read.group.values[i],
1134 machine);
1135 if (ret)
1136 break;
1137 }
1138
1139 return ret;
1140}
1141
1142static int
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001143 perf_evlist__deliver_sample(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +02001144 struct perf_tool *tool,
1145 union perf_event *event,
1146 struct perf_sample *sample,
1147 struct perf_evsel *evsel,
1148 struct machine *machine)
1149{
1150 /* We know evsel != NULL. */
1151 u64 sample_type = evsel->attr.sample_type;
1152 u64 read_format = evsel->attr.read_format;
1153
1154 /* Standard sample delievery. */
1155 if (!(sample_type & PERF_SAMPLE_READ))
1156 return tool->sample(tool, event, sample, evsel, machine);
1157
1158 /* For PERF_SAMPLE_READ we have either single or group mode. */
1159 if (read_format & PERF_FORMAT_GROUP)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001160 return deliver_sample_group(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001161 machine);
1162 else
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001163 return deliver_sample_value(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001164 &sample->read.one, machine);
1165}
1166
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -03001167static int machines__deliver_event(struct machines *machines,
1168 struct perf_evlist *evlist,
1169 union perf_event *event,
1170 struct perf_sample *sample,
1171 struct perf_tool *tool, u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001172{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001173 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001174 struct machine *machine;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001175
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001176 dump_event(evlist, event, file_offset, sample);
Thomas Gleixner532e7262010-12-07 12:48:55 +00001177
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001178 evsel = perf_evlist__id2evsel(evlist, sample->id);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001179
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001180 machine = machines__find_for_cpumode(machines, event, sample);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001181
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001182 switch (event->header.type) {
1183 case PERF_RECORD_SAMPLE:
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001184 if (evsel == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001185 ++evlist->stats.nr_unknown_id;
Jiri Olsa67822062012-04-12 14:21:01 +02001186 return 0;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001187 }
Kan Liang1b29ac52015-09-03 08:31:00 -04001188 dump_sample(evsel, event, sample);
Joerg Roedel0c095712012-02-10 18:05:04 +01001189 if (machine == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001190 ++evlist->stats.nr_unprocessable_samples;
Jiri Olsa67822062012-04-12 14:21:01 +02001191 return 0;
Joerg Roedel0c095712012-02-10 18:05:04 +01001192 }
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001193 return perf_evlist__deliver_sample(evlist, tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001194 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001195 return tool->mmap(tool, event, sample, machine);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001196 case PERF_RECORD_MMAP2:
Kan Liang930e6fc2015-06-17 09:51:10 -04001197 if (event->header.misc & PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT)
1198 ++evlist->stats.nr_proc_map_timeout;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001199 return tool->mmap2(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001200 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001201 return tool->comm(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001202 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001203 return tool->fork(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001204 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001205 return tool->exit(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001206 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001207 if (tool->lost == perf_event__process_lost)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001208 evlist->stats.total_lost += event->lost.lost;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001209 return tool->lost(tool, event, sample, machine);
Kan Liangc4937a92015-05-10 15:13:15 -04001210 case PERF_RECORD_LOST_SAMPLES:
1211 if (tool->lost_samples == perf_event__process_lost_samples)
1212 evlist->stats.total_lost_samples += event->lost_samples.lost;
1213 return tool->lost_samples(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001214 case PERF_RECORD_READ:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001215 return tool->read(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001216 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001217 return tool->throttle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001218 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001219 return tool->unthrottle(tool, event, sample, machine);
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001220 case PERF_RECORD_AUX:
Adrian Huntera38f48e2015-09-25 16:15:37 +03001221 if (tool->aux == perf_event__process_aux &&
1222 (event->aux.flags & PERF_AUX_FLAG_TRUNCATED))
1223 evlist->stats.total_aux_lost += 1;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001224 return tool->aux(tool, event, sample, machine);
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001225 case PERF_RECORD_ITRACE_START:
1226 return tool->itrace_start(tool, event, sample, machine);
Adrian Hunter02860392015-07-21 12:44:03 +03001227 case PERF_RECORD_SWITCH:
1228 case PERF_RECORD_SWITCH_CPU_WIDE:
1229 return tool->context_switch(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001230 default:
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001231 ++evlist->stats.nr_unknown_events;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001232 return -1;
1233 }
1234}
1235
Adrian Hunterc4468702015-04-09 18:53:48 +03001236static int perf_session__deliver_event(struct perf_session *session,
1237 union perf_event *event,
1238 struct perf_sample *sample,
1239 struct perf_tool *tool,
1240 u64 file_offset)
1241{
1242 int ret;
1243
1244 ret = auxtrace__process_event(session, event, sample, tool);
1245 if (ret < 0)
1246 return ret;
1247 if (ret > 0)
1248 return 0;
1249
1250 return machines__deliver_event(&session->machines, session->evlist,
1251 event, sample, tool, file_offset);
1252}
1253
Adrian Hunterd5652d82014-07-23 22:19:58 +03001254static s64 perf_session__process_user_event(struct perf_session *session,
1255 union perf_event *event,
Adrian Hunterd5652d82014-07-23 22:19:58 +03001256 u64 file_offset)
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001257{
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -03001258 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001259 struct perf_tool *tool = session->tool;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001260 int fd = perf_data_file__fd(session->file);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001261 int err;
1262
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001263 dump_event(session->evlist, event, file_offset, NULL);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001264
1265 /* These events are processed right away */
1266 switch (event->header.type) {
1267 case PERF_RECORD_HEADER_ATTR:
Adrian Hunter47c3d102013-07-04 16:20:21 +03001268 err = tool->attr(tool, event, &session->evlist);
Adrian Huntercfe1c412014-07-31 09:00:45 +03001269 if (err == 0) {
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -03001270 perf_session__set_id_hdr_size(session);
Adrian Huntercfe1c412014-07-31 09:00:45 +03001271 perf_session__set_comm_exec(session);
1272 }
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001273 return err;
Jiri Olsaf67697b2014-02-04 15:37:48 +01001274 case PERF_RECORD_HEADER_EVENT_TYPE:
1275 /*
1276 * Depreceated, but we need to handle it for sake
1277 * of old data files create in pipe mode.
1278 */
1279 return 0;
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001280 case PERF_RECORD_HEADER_TRACING_DATA:
1281 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001282 lseek(fd, file_offset, SEEK_SET);
Adrian Hunter47c3d102013-07-04 16:20:21 +03001283 return tool->tracing_data(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001284 case PERF_RECORD_HEADER_BUILD_ID:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001285 return tool->build_id(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001286 case PERF_RECORD_FINISHED_ROUND:
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -03001287 return tool->finished_round(tool, event, oe);
Adrian Hunter3c659ee2014-10-27 15:49:22 +02001288 case PERF_RECORD_ID_INDEX:
1289 return tool->id_index(tool, event, session);
Adrian Huntera16ac022015-04-09 18:53:43 +03001290 case PERF_RECORD_AUXTRACE_INFO:
1291 return tool->auxtrace_info(tool, event, session);
1292 case PERF_RECORD_AUXTRACE:
1293 /* setup for reading amidst mmap */
1294 lseek(fd, file_offset + event->header.size, SEEK_SET);
1295 return tool->auxtrace(tool, event, session);
Adrian Huntere9bf54d2015-04-09 18:53:47 +03001296 case PERF_RECORD_AUXTRACE_ERROR:
Adrian Hunter85ed4722015-04-09 18:53:50 +03001297 perf_session__auxtrace_error_inc(session, event);
Adrian Huntere9bf54d2015-04-09 18:53:47 +03001298 return tool->auxtrace_error(tool, event, session);
Jiri Olsa5f3339d2015-10-25 15:51:19 +01001299 case PERF_RECORD_THREAD_MAP:
1300 return tool->thread_map(tool, event, session);
Jiri Olsa6640b6c2015-10-25 15:51:23 +01001301 case PERF_RECORD_CPU_MAP:
1302 return tool->cpu_map(tool, event, session);
Jiri Olsa374fb9e2015-10-25 15:51:27 +01001303 case PERF_RECORD_STAT_CONFIG:
1304 return tool->stat_config(tool, event, session);
Jiri Olsad80518c2015-10-25 15:51:30 +01001305 case PERF_RECORD_STAT:
1306 return tool->stat(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001307 default:
1308 return -EINVAL;
1309 }
1310}
1311
Adrian Huntera2938292014-10-27 15:49:23 +02001312int perf_session__deliver_synth_event(struct perf_session *session,
1313 union perf_event *event,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001314 struct perf_sample *sample)
Adrian Huntera2938292014-10-27 15:49:23 +02001315{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001316 struct perf_evlist *evlist = session->evlist;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001317 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001318
1319 events_stats__inc(&evlist->stats, event->header.type);
Adrian Huntera2938292014-10-27 15:49:23 +02001320
1321 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001322 return perf_session__process_user_event(session, event, 0);
Adrian Huntera2938292014-10-27 15:49:23 +02001323
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001324 return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0);
Adrian Huntera2938292014-10-27 15:49:23 +02001325}
1326
Jiri Olsa268fb202012-05-30 14:23:43 +02001327static void event_swap(union perf_event *event, bool sample_id_all)
1328{
1329 perf_event__swap_op swap;
1330
1331 swap = perf_event__swap_ops[event->header.type];
1332 if (swap)
1333 swap(event, sample_id_all);
1334}
1335
Adrian Hunter5a52f332014-07-31 09:00:57 +03001336int perf_session__peek_event(struct perf_session *session, off_t file_offset,
1337 void *buf, size_t buf_sz,
1338 union perf_event **event_ptr,
1339 struct perf_sample *sample)
1340{
1341 union perf_event *event;
1342 size_t hdr_sz, rest;
1343 int fd;
1344
1345 if (session->one_mmap && !session->header.needs_swap) {
1346 event = file_offset - session->one_mmap_offset +
1347 session->one_mmap_addr;
1348 goto out_parse_sample;
1349 }
1350
1351 if (perf_data_file__is_pipe(session->file))
1352 return -1;
1353
1354 fd = perf_data_file__fd(session->file);
1355 hdr_sz = sizeof(struct perf_event_header);
1356
1357 if (buf_sz < hdr_sz)
1358 return -1;
1359
1360 if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 ||
Adrian Hunter554e92e2015-05-19 16:05:45 +03001361 readn(fd, buf, hdr_sz) != (ssize_t)hdr_sz)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001362 return -1;
1363
1364 event = (union perf_event *)buf;
1365
1366 if (session->header.needs_swap)
1367 perf_event_header__bswap(&event->header);
1368
Adrian Hunter554e92e2015-05-19 16:05:45 +03001369 if (event->header.size < hdr_sz || event->header.size > buf_sz)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001370 return -1;
1371
1372 rest = event->header.size - hdr_sz;
1373
Adrian Hunter554e92e2015-05-19 16:05:45 +03001374 if (readn(fd, buf, rest) != (ssize_t)rest)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001375 return -1;
1376
1377 if (session->header.needs_swap)
1378 event_swap(event, perf_evlist__sample_id_all(session->evlist));
1379
1380out_parse_sample:
1381
1382 if (sample && event->header.type < PERF_RECORD_USER_TYPE_START &&
1383 perf_evlist__parse_sample(session->evlist, event, sample))
1384 return -1;
1385
1386 *event_ptr = event;
1387
1388 return 0;
1389}
1390
Adrian Hunterd5652d82014-07-23 22:19:58 +03001391static s64 perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001392 union perf_event *event, u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001393{
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001394 struct perf_evlist *evlist = session->evlist;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001395 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001396 struct perf_sample sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001397 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001398
Jiri Olsa268fb202012-05-30 14:23:43 +02001399 if (session->header.needs_swap)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001400 event_swap(event, perf_evlist__sample_id_all(evlist));
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001401
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001402 if (event->header.type >= PERF_RECORD_HEADER_MAX)
1403 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001404
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001405 events_stats__inc(&evlist->stats, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001406
1407 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001408 return perf_session__process_user_event(session, event, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001409
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001410 /*
1411 * For all kernel events we get the sample data
1412 */
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001413 ret = perf_evlist__parse_sample(evlist, event, &sample);
Frederic Weisbecker5538bec2011-05-22 02:17:22 +02001414 if (ret)
1415 return ret;
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001416
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001417 if (tool->ordered_events) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001418 ret = perf_session__queue_event(session, event, &sample, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001419 if (ret != -ETIME)
1420 return ret;
1421 }
1422
Adrian Hunterc4468702015-04-09 18:53:48 +03001423 return perf_session__deliver_event(session, event, &sample, tool,
1424 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001425}
1426
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001427void perf_event_header__bswap(struct perf_event_header *hdr)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001428{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001429 hdr->type = bswap_32(hdr->type);
1430 hdr->misc = bswap_16(hdr->misc);
1431 hdr->size = bswap_16(hdr->size);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001432}
1433
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001434struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1435{
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001436 return machine__findnew_thread(&session->machines.host, -1, pid);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001437}
1438
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001439int perf_session__register_idle_thread(struct perf_session *session)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001440{
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001441 struct thread *thread;
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001442 int err = 0;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001443
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001444 thread = machine__findnew_thread(&session->machines.host, 0, 0);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001445 if (thread == NULL || thread__set_comm(thread, "swapper", 0)) {
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001446 pr_err("problem inserting idle task.\n");
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001447 err = -1;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001448 }
1449
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001450 /* machine__findnew_thread() got the thread, so put it */
1451 thread__put(thread);
1452 return err;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001453}
1454
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001455static void perf_session__warn_about_errors(const struct perf_session *session)
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001456{
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001457 const struct events_stats *stats = &session->evlist->stats;
1458 const struct ordered_events *oe = &session->ordered_events;
1459
1460 if (session->tool->lost == perf_event__process_lost &&
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001461 stats->nr_events[PERF_RECORD_LOST] != 0) {
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001462 ui__warning("Processed %d events and lost %d chunks!\n\n"
1463 "Check IO/CPU overload!\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001464 stats->nr_events[0],
1465 stats->nr_events[PERF_RECORD_LOST]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001466 }
1467
Kan Liangc4937a92015-05-10 15:13:15 -04001468 if (session->tool->lost_samples == perf_event__process_lost_samples) {
1469 double drop_rate;
1470
1471 drop_rate = (double)stats->total_lost_samples /
1472 (double) (stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples);
1473 if (drop_rate > 0.05) {
He Kuang6ba29c22015-06-11 12:44:24 +00001474 ui__warning("Processed %" PRIu64 " samples and lost %3.2f%% samples!\n\n",
Kan Liangc4937a92015-05-10 15:13:15 -04001475 stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples,
1476 drop_rate * 100.0);
1477 }
1478 }
1479
Adrian Huntera38f48e2015-09-25 16:15:37 +03001480 if (session->tool->aux == perf_event__process_aux &&
1481 stats->total_aux_lost != 0) {
1482 ui__warning("AUX data lost %" PRIu64 " times out of %u!\n\n",
1483 stats->total_aux_lost,
1484 stats->nr_events[PERF_RECORD_AUX]);
1485 }
1486
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001487 if (stats->nr_unknown_events != 0) {
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001488 ui__warning("Found %u unknown events!\n\n"
1489 "Is this an older tool processing a perf.data "
1490 "file generated by a more recent tool?\n\n"
1491 "If that is not the case, consider "
1492 "reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001493 stats->nr_unknown_events);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001494 }
1495
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001496 if (stats->nr_unknown_id != 0) {
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001497 ui__warning("%u samples with id not present in the header\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001498 stats->nr_unknown_id);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001499 }
1500
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001501 if (stats->nr_invalid_chains != 0) {
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001502 ui__warning("Found invalid callchains!\n\n"
1503 "%u out of %u events were discarded for this reason.\n\n"
1504 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001505 stats->nr_invalid_chains,
1506 stats->nr_events[PERF_RECORD_SAMPLE]);
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001507 }
Joerg Roedel0c095712012-02-10 18:05:04 +01001508
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001509 if (stats->nr_unprocessable_samples != 0) {
Joerg Roedel0c095712012-02-10 18:05:04 +01001510 ui__warning("%u unprocessable samples recorded.\n"
1511 "Do you have a KVM guest running and not using 'perf kvm'?\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001512 stats->nr_unprocessable_samples);
Joerg Roedel0c095712012-02-10 18:05:04 +01001513 }
Jiri Olsaf61ff6c2014-11-26 16:39:31 +01001514
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001515 if (oe->nr_unordered_events != 0)
1516 ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events);
Adrian Hunter85ed4722015-04-09 18:53:50 +03001517
1518 events_stats__auxtrace_error_warn(stats);
Kan Liang930e6fc2015-06-17 09:51:10 -04001519
1520 if (stats->nr_proc_map_timeout != 0) {
1521 ui__warning("%d map information files for pre-existing threads were\n"
1522 "not processed, if there are samples for addresses they\n"
1523 "will not be resolved, you may find out which are these\n"
1524 "threads by running with -v and redirecting the output\n"
Kan Liang9d9cad72015-06-17 09:51:11 -04001525 "to a file.\n"
1526 "The time limit to process proc map is too short?\n"
1527 "Increase it by --proc-map-timeout\n",
Kan Liang930e6fc2015-06-17 09:51:10 -04001528 stats->nr_proc_map_timeout);
1529 }
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001530}
1531
Adrian Huntera5499b32015-05-29 16:33:30 +03001532static int perf_session__flush_thread_stack(struct thread *thread,
1533 void *p __maybe_unused)
1534{
1535 return thread_stack__flush(thread);
1536}
1537
1538static int perf_session__flush_thread_stacks(struct perf_session *session)
1539{
1540 return machines__for_each_thread(&session->machines,
1541 perf_session__flush_thread_stack,
1542 NULL);
1543}
1544
Tom Zanussi8dc58102010-04-01 23:59:15 -05001545volatile int session_done;
1546
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001547static int __perf_session__process_pipe_events(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05001548{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001549 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001550 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001551 int fd = perf_data_file__fd(session->file);
Stephane Eranian444d2862012-05-15 13:28:12 +02001552 union perf_event *event;
1553 uint32_t size, cur_size = 0;
1554 void *buf = NULL;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001555 s64 skip = 0;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001556 u64 head;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001557 ssize_t err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001558 void *p;
1559
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001560 perf_tool__fill_defaults(tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001561
1562 head = 0;
Stephane Eranian444d2862012-05-15 13:28:12 +02001563 cur_size = sizeof(union perf_event);
1564
1565 buf = malloc(cur_size);
1566 if (!buf)
1567 return -errno;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001568more:
Stephane Eranian444d2862012-05-15 13:28:12 +02001569 event = buf;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001570 err = readn(fd, event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05001571 if (err <= 0) {
1572 if (err == 0)
1573 goto done;
1574
1575 pr_err("failed to read event header\n");
1576 goto out_err;
1577 }
1578
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001579 if (session->header.needs_swap)
Stephane Eranian444d2862012-05-15 13:28:12 +02001580 perf_event_header__bswap(&event->header);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001581
Stephane Eranian444d2862012-05-15 13:28:12 +02001582 size = event->header.size;
Adrian Hunter27389d72013-07-04 16:20:27 +03001583 if (size < sizeof(struct perf_event_header)) {
1584 pr_err("bad event header size\n");
1585 goto out_err;
1586 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001587
Stephane Eranian444d2862012-05-15 13:28:12 +02001588 if (size > cur_size) {
1589 void *new = realloc(buf, size);
1590 if (!new) {
1591 pr_err("failed to allocate memory to read event\n");
1592 goto out_err;
1593 }
1594 buf = new;
1595 cur_size = size;
1596 event = buf;
1597 }
1598 p = event;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001599 p += sizeof(struct perf_event_header);
1600
Tom Zanussi794e43b2010-05-05 00:27:40 -05001601 if (size - sizeof(struct perf_event_header)) {
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001602 err = readn(fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -05001603 if (err <= 0) {
1604 if (err == 0) {
1605 pr_err("unexpected end of event stream\n");
1606 goto done;
1607 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001608
Tom Zanussi794e43b2010-05-05 00:27:40 -05001609 pr_err("failed to read event data\n");
1610 goto out_err;
1611 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001612 }
1613
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001614 if ((skip = perf_session__process_event(session, event, head)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001615 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
Stephane Eranian444d2862012-05-15 13:28:12 +02001616 head, event->header.size, event->header.type);
Jiri Olsa9389a462012-04-16 20:42:51 +02001617 err = -EINVAL;
1618 goto out_err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001619 }
1620
1621 head += size;
1622
Tom Zanussi8dc58102010-04-01 23:59:15 -05001623 if (skip > 0)
1624 head += skip;
1625
1626 if (!session_done())
1627 goto more;
1628done:
Adrian Hunter8c16b642013-10-18 15:29:02 +03001629 /* do the final flush for ordered samples */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001630 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
Adrian Hunterc4468702015-04-09 18:53:48 +03001631 if (err)
1632 goto out_err;
1633 err = auxtrace__flush_events(session, tool);
Adrian Huntera5499b32015-05-29 16:33:30 +03001634 if (err)
1635 goto out_err;
1636 err = perf_session__flush_thread_stacks(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001637out_err:
Stephane Eranian444d2862012-05-15 13:28:12 +02001638 free(buf);
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001639 perf_session__warn_about_errors(session);
Jiri Olsaadc56ed2014-06-10 22:50:03 +02001640 ordered_events__free(&session->ordered_events);
Adrian Hunterc4468702015-04-09 18:53:48 +03001641 auxtrace__free_events(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001642 return err;
1643}
1644
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001645static union perf_event *
1646fetch_mmaped_event(struct perf_session *session,
1647 u64 head, size_t mmap_size, char *buf)
1648{
1649 union perf_event *event;
1650
1651 /*
1652 * Ensure we have enough space remaining to read
1653 * the size of the event in the headers.
1654 */
1655 if (head + sizeof(event->header) > mmap_size)
1656 return NULL;
1657
1658 event = (union perf_event *)(buf + head);
1659
1660 if (session->header.needs_swap)
1661 perf_event_header__bswap(&event->header);
1662
Adrian Hunter27389d72013-07-04 16:20:27 +03001663 if (head + event->header.size > mmap_size) {
1664 /* We're not fetching the event so swap back again */
1665 if (session->header.needs_swap)
1666 perf_event_header__bswap(&event->header);
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001667 return NULL;
Adrian Hunter27389d72013-07-04 16:20:27 +03001668 }
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001669
1670 return event;
1671}
1672
David Miller35d48dd2012-11-10 14:12:19 -05001673/*
1674 * On 64bit we can mmap the data file in one go. No need for tiny mmap
1675 * slices. On 32bit we use 32MB.
1676 */
1677#if BITS_PER_LONG == 64
1678#define MMAP_SIZE ULLONG_MAX
1679#define NUM_MMAPS 1
1680#else
1681#define MMAP_SIZE (32 * 1024 * 1024ULL)
1682#define NUM_MMAPS 128
1683#endif
1684
Namhyung Kim4ac30cf2015-01-29 17:06:43 +09001685static int __perf_session__process_events(struct perf_session *session,
1686 u64 data_offset, u64 data_size,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001687 u64 file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001688{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001689 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001690 struct perf_tool *tool = session->tool;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001691 int fd = perf_data_file__fd(session->file);
Adrian Hunterd5652d82014-07-23 22:19:58 +03001692 u64 head, page_offset, file_offset, file_pos, size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001693 int err, mmap_prot, mmap_flags, map_idx = 0;
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -03001694 size_t mmap_size;
David Miller35d48dd2012-11-10 14:12:19 -05001695 char *buf, *mmaps[NUM_MMAPS];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001696 union perf_event *event;
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001697 struct ui_progress prog;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001698 s64 skip;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001699
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001700 perf_tool__fill_defaults(tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001701
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001702 page_offset = page_size * (data_offset / page_size);
1703 file_offset = page_offset;
1704 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001705
Mark Rutland381c02f2015-09-16 18:18:49 +01001706 if (data_size == 0)
1707 goto out;
1708
1709 if (data_offset + data_size < file_size)
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001710 file_size = data_offset + data_size;
1711
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001712 ui_progress__init(&prog, file_size, "Processing events...");
Thomas Gleixner55b446292010-11-30 17:49:46 +00001713
David Miller35d48dd2012-11-10 14:12:19 -05001714 mmap_size = MMAP_SIZE;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001715 if (mmap_size > file_size) {
Thomas Gleixner55b446292010-11-30 17:49:46 +00001716 mmap_size = file_size;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001717 session->one_mmap = true;
1718 }
Thomas Gleixner55b446292010-11-30 17:49:46 +00001719
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001720 memset(mmaps, 0, sizeof(mmaps));
1721
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001722 mmap_prot = PROT_READ;
1723 mmap_flags = MAP_SHARED;
1724
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001725 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001726 mmap_prot |= PROT_WRITE;
1727 mmap_flags = MAP_PRIVATE;
1728 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001729remap:
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001730 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd,
Thomas Gleixner55b446292010-11-30 17:49:46 +00001731 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001732 if (buf == MAP_FAILED) {
1733 pr_err("failed to mmap file\n");
1734 err = -errno;
1735 goto out_err;
1736 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001737 mmaps[map_idx] = buf;
1738 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001739 file_pos = file_offset + head;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001740 if (session->one_mmap) {
1741 session->one_mmap_addr = buf;
1742 session->one_mmap_offset = file_offset;
1743 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001744
1745more:
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001746 event = fetch_mmaped_event(session, head, mmap_size, buf);
1747 if (!event) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001748 if (mmaps[map_idx]) {
1749 munmap(mmaps[map_idx], mmap_size);
1750 mmaps[map_idx] = NULL;
1751 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001752
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001753 page_offset = page_size * (head / page_size);
1754 file_offset += page_offset;
1755 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001756 goto remap;
1757 }
1758
1759 size = event->header.size;
1760
Adrian Hunter27389d72013-07-04 16:20:27 +03001761 if (size < sizeof(struct perf_event_header) ||
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001762 (skip = perf_session__process_event(session, event, file_pos)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001763 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1764 file_offset + head, event->header.size,
1765 event->header.type);
1766 err = -EINVAL;
1767 goto out_err;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001768 }
1769
Adrian Hunter6f917c72014-07-23 22:19:57 +03001770 if (skip)
1771 size += skip;
1772
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001773 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001774 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001775
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001776 ui_progress__update(&prog, size);
Thomas Gleixner55b446292010-11-30 17:49:46 +00001777
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001778 if (session_done())
Adrian Hunter8c16b642013-10-18 15:29:02 +03001779 goto out;
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001780
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001781 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001782 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001783
Adrian Hunter8c16b642013-10-18 15:29:02 +03001784out:
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001785 /* do the final flush for ordered samples */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001786 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
Adrian Hunterc4468702015-04-09 18:53:48 +03001787 if (err)
1788 goto out_err;
1789 err = auxtrace__flush_events(session, tool);
Adrian Huntera5499b32015-05-29 16:33:30 +03001790 if (err)
1791 goto out_err;
1792 err = perf_session__flush_thread_stacks(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001793out_err:
Namhyung Kima5580f32012-11-13 22:30:34 +09001794 ui_progress__finish();
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001795 perf_session__warn_about_errors(session);
Jiri Olsaadc56ed2014-06-10 22:50:03 +02001796 ordered_events__free(&session->ordered_events);
Adrian Hunterc4468702015-04-09 18:53:48 +03001797 auxtrace__free_events(session);
Adrian Hunter919d86d2014-07-14 13:02:51 +03001798 session->one_mmap = false;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001799 return err;
1800}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001801
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001802int perf_session__process_events(struct perf_session *session)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001803{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001804 u64 size = perf_data_file__size(session->file);
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001805 int err;
1806
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001807 if (perf_session__register_idle_thread(session) < 0)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001808 return -ENOMEM;
1809
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001810 if (!perf_data_file__is_pipe(session->file))
1811 err = __perf_session__process_events(session,
1812 session->header.data_offset,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001813 session->header.data_size, size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001814 else
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001815 err = __perf_session__process_pipe_events(session);
Dave Martin88ca8952010-07-27 11:46:12 -03001816
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001817 return err;
1818}
1819
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001820bool perf_session__has_traces(struct perf_session *session, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001821{
David Ahern93ea01c292013-08-07 22:50:58 -04001822 struct perf_evsel *evsel;
1823
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001824 evlist__for_each(session->evlist, evsel) {
David Ahern93ea01c292013-08-07 22:50:58 -04001825 if (evsel->attr.type == PERF_TYPE_TRACEPOINT)
1826 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001827 }
1828
David Ahern93ea01c292013-08-07 22:50:58 -04001829 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1830 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001831}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001832
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001833int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1834 const char *symbol_name, u64 addr)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001835{
1836 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001837 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001838 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001839
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001840 ref = zalloc(sizeof(struct ref_reloc_sym));
1841 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001842 return -ENOMEM;
1843
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001844 ref->name = strdup(symbol_name);
1845 if (ref->name == NULL) {
1846 free(ref);
1847 return -ENOMEM;
1848 }
1849
1850 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001851 if (bracket)
1852 *bracket = '\0';
1853
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001854 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001855
1856 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001857 struct kmap *kmap = map__kmap(maps[i]);
Wang Nanba927322015-04-07 08:22:45 +00001858
1859 if (!kmap)
1860 continue;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001861 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001862 }
1863
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001864 return 0;
1865}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001866
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001867size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001868{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001869 return machines__fprintf_dsos(&session->machines, fp);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001870}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001871
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001872size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001873 bool (skip)(struct dso *dso, int parm), int parm)
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001874{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001875 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001876}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001877
1878size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1879{
Adrian Hunterc4468702015-04-09 18:53:48 +03001880 size_t ret;
1881 const char *msg = "";
1882
1883 if (perf_header__has_feat(&session->header, HEADER_AUXTRACE))
1884 msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)";
1885
Adrian Hunterfe692ac2015-06-23 10:52:49 +03001886 ret = fprintf(fp, "\nAggregated stats:%s\n", msg);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001887
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001888 ret += events_stats__fprintf(&session->evlist->stats, fp);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001889 return ret;
1890}
David Ahernc0230b22011-03-09 22:23:27 -07001891
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001892size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1893{
1894 /*
1895 * FIXME: Here we have to actually print all the machines in this
1896 * session, not just the host...
1897 */
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001898 return machine__fprintf(&session->machines.host, fp);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001899}
1900
David Ahern9cbdb702011-04-06 21:54:20 -06001901struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1902 unsigned int type)
1903{
1904 struct perf_evsel *pos;
1905
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001906 evlist__for_each(session->evlist, pos) {
David Ahern9cbdb702011-04-06 21:54:20 -06001907 if (pos->attr.type == type)
1908 return pos;
1909 }
1910 return NULL;
1911}
1912
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001913void perf_evsel__print_ip(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -03001914 struct addr_location *al,
David Ahern307cbb92013-08-07 22:50:53 -04001915 unsigned int print_opts, unsigned int stack_depth)
David Ahernc0230b22011-03-09 22:23:27 -07001916{
David Ahernc0230b22011-03-09 22:23:27 -07001917 struct callchain_cursor_node *node;
David Aherna6ffaf92013-08-07 22:50:51 -04001918 int print_ip = print_opts & PRINT_IP_OPT_IP;
1919 int print_sym = print_opts & PRINT_IP_OPT_SYM;
1920 int print_dso = print_opts & PRINT_IP_OPT_DSO;
1921 int print_symoffset = print_opts & PRINT_IP_OPT_SYMOFFSET;
David Ahernb0b35f02013-08-07 22:50:52 -04001922 int print_oneline = print_opts & PRINT_IP_OPT_ONELINE;
Adrian Huntercc8fae12013-12-06 09:42:57 +02001923 int print_srcline = print_opts & PRINT_IP_OPT_SRCLINE;
David Ahernb0b35f02013-08-07 22:50:52 -04001924 char s = print_oneline ? ' ' : '\t';
David Ahernc0230b22011-03-09 22:23:27 -07001925
David Ahernc0230b22011-03-09 22:23:27 -07001926 if (symbol_conf.use_callchain && sample->callchain) {
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001927 struct addr_location node_al;
David Ahernc0230b22011-03-09 22:23:27 -07001928
Arnaldo Carvalho de Melocc8b7c22014-10-23 15:26:17 -03001929 if (thread__resolve_callchain(al->thread, evsel,
1930 sample, NULL, NULL,
Adrian Hunter03cd1fe2015-09-25 16:15:49 +03001931 stack_depth) != 0) {
David Ahernc0230b22011-03-09 22:23:27 -07001932 if (verbose)
1933 error("Failed to resolve callchain. Skipping\n");
1934 return;
1935 }
Namhyung Kim47260642012-05-31 14:43:26 +09001936 callchain_cursor_commit(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001937
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001938 if (print_symoffset)
1939 node_al = *al;
1940
David Ahern307cbb92013-08-07 22:50:53 -04001941 while (stack_depth) {
Adrian Huntera4eb24a2013-12-06 09:42:56 +02001942 u64 addr = 0;
1943
Namhyung Kim47260642012-05-31 14:43:26 +09001944 node = callchain_cursor_current(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001945 if (!node)
1946 break;
1947
David Ahernd2ff1b12013-11-18 13:32:44 -07001948 if (node->sym && node->sym->ignore)
1949 goto next;
1950
David Aherna6ffaf92013-08-07 22:50:51 -04001951 if (print_ip)
David Ahernb0b35f02013-08-07 22:50:52 -04001952 printf("%c%16" PRIx64, s, node->ip);
David Aherna6ffaf92013-08-07 22:50:51 -04001953
Adrian Huntera4eb24a2013-12-06 09:42:56 +02001954 if (node->map)
1955 addr = node->map->map_ip(node->map, node->ip);
1956
David Ahern787bef12011-05-27 14:28:43 -06001957 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001958 printf(" ");
David Ahern251f4262013-07-28 09:14:34 -06001959 if (print_symoffset) {
Adrian Huntera4eb24a2013-12-06 09:42:56 +02001960 node_al.addr = addr;
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001961 node_al.map = node->map;
1962 symbol__fprintf_symname_offs(node->sym, &node_al, stdout);
David Ahern251f4262013-07-28 09:14:34 -06001963 } else
1964 symbol__fprintf_symname(node->sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001965 }
David Ahern251f4262013-07-28 09:14:34 -06001966
David Ahern610723f2011-05-27 14:28:44 -06001967 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001968 printf(" (");
David Ahern52deff72012-05-29 22:58:26 -06001969 map__fprintf_dsoname(node->map, stdout);
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001970 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001971 }
David Ahernb0b35f02013-08-07 22:50:52 -04001972
Adrian Huntercc8fae12013-12-06 09:42:57 +02001973 if (print_srcline)
1974 map__fprintf_srcline(node->map, addr, "\n ",
1975 stdout);
1976
David Ahernb0b35f02013-08-07 22:50:52 -04001977 if (!print_oneline)
1978 printf("\n");
David Ahernc0230b22011-03-09 22:23:27 -07001979
David Ahern307cbb92013-08-07 22:50:53 -04001980 stack_depth--;
David Ahernd2ff1b12013-11-18 13:32:44 -07001981next:
1982 callchain_cursor_advance(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001983 }
1984
1985 } else {
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001986 if (al->sym && al->sym->ignore)
David Ahernd2ff1b12013-11-18 13:32:44 -07001987 return;
1988
David Aherna6ffaf92013-08-07 22:50:51 -04001989 if (print_ip)
1990 printf("%16" PRIx64, sample->ip);
1991
David Ahern787bef12011-05-27 14:28:43 -06001992 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001993 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001994 if (print_symoffset)
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001995 symbol__fprintf_symname_offs(al->sym, al,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001996 stdout);
1997 else
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001998 symbol__fprintf_symname(al->sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001999 }
2000
2001 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09002002 printf(" (");
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02002003 map__fprintf_dsoname(al->map, stdout);
Akihiro Nagai547a92e2012-01-30 13:42:57 +09002004 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06002005 }
Adrian Huntercc8fae12013-12-06 09:42:57 +02002006
2007 if (print_srcline)
2008 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
David Ahernc0230b22011-03-09 22:23:27 -07002009 }
2010}
Anton Blanchard5d67be92011-07-04 21:57:50 +10002011
2012int perf_session__cpu_bitmap(struct perf_session *session,
2013 const char *cpu_list, unsigned long *cpu_bitmap)
2014{
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04002015 int i, err = -1;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002016 struct cpu_map *map;
2017
2018 for (i = 0; i < PERF_TYPE_MAX; ++i) {
2019 struct perf_evsel *evsel;
2020
2021 evsel = perf_session__find_first_evtype(session, i);
2022 if (!evsel)
2023 continue;
2024
2025 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
2026 pr_err("File does not contain CPU events. "
2027 "Remove -c option to proceed.\n");
2028 return -1;
2029 }
2030 }
2031
2032 map = cpu_map__new(cpu_list);
David Ahern47fbe532011-11-13 10:45:27 -07002033 if (map == NULL) {
2034 pr_err("Invalid cpu_list\n");
2035 return -1;
2036 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10002037
2038 for (i = 0; i < map->nr; i++) {
2039 int cpu = map->map[i];
2040
2041 if (cpu >= MAX_NR_CPUS) {
2042 pr_err("Requested CPU %d too large. "
2043 "Consider raising MAX_NR_CPUS\n", cpu);
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04002044 goto out_delete_map;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002045 }
2046
2047 set_bit(cpu, cpu_bitmap);
2048 }
2049
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04002050 err = 0;
2051
2052out_delete_map:
Jiri Olsaf30a79b2015-06-23 00:36:04 +02002053 cpu_map__put(map);
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04002054 return err;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002055}
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002056
2057void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
2058 bool full)
2059{
2060 struct stat st;
Masanari Iidac5765ec2014-05-15 02:13:38 +09002061 int fd, ret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002062
2063 if (session == NULL || fp == NULL)
2064 return;
2065
Masanari Iidac5765ec2014-05-15 02:13:38 +09002066 fd = perf_data_file__fd(session->file);
2067
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002068 ret = fstat(fd, &st);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002069 if (ret == -1)
2070 return;
2071
2072 fprintf(fp, "# ========\n");
2073 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
2074 perf_header__fprintf_info(session, fp, full);
2075 fprintf(fp, "# ========\n#\n");
2076}
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002077
2078
2079int __perf_session__set_tracepoints_handlers(struct perf_session *session,
2080 const struct perf_evsel_str_handler *assocs,
2081 size_t nr_assocs)
2082{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002083 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002084 size_t i;
2085 int err;
2086
2087 for (i = 0; i < nr_assocs; i++) {
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03002088 /*
2089 * Adding a handler for an event not in the session,
2090 * just ignore it.
2091 */
2092 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name);
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002093 if (evsel == NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03002094 continue;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002095
2096 err = -EEXIST;
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002097 if (evsel->handler != NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03002098 goto out;
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002099 evsel->handler = assocs[i].handler;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002100 }
2101
2102 err = 0;
2103out:
2104 return err;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002105}
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002106
2107int perf_event__process_id_index(struct perf_tool *tool __maybe_unused,
2108 union perf_event *event,
2109 struct perf_session *session)
2110{
2111 struct perf_evlist *evlist = session->evlist;
2112 struct id_index_event *ie = &event->id_index;
2113 size_t i, nr, max_nr;
2114
2115 max_nr = (ie->header.size - sizeof(struct id_index_event)) /
2116 sizeof(struct id_index_entry);
2117 nr = ie->nr;
2118 if (nr > max_nr)
2119 return -EINVAL;
2120
2121 if (dump_trace)
2122 fprintf(stdout, " nr: %zu\n", nr);
2123
2124 for (i = 0; i < nr; i++) {
2125 struct id_index_entry *e = &ie->entries[i];
2126 struct perf_sample_id *sid;
2127
2128 if (dump_trace) {
2129 fprintf(stdout, " ... id: %"PRIu64, e->id);
2130 fprintf(stdout, " idx: %"PRIu64, e->idx);
2131 fprintf(stdout, " cpu: %"PRId64, e->cpu);
2132 fprintf(stdout, " tid: %"PRId64"\n", e->tid);
2133 }
2134
2135 sid = perf_evlist__id2sid(evlist, e->id);
2136 if (!sid)
2137 return -ENOENT;
2138 sid->idx = e->idx;
2139 sid->cpu = e->cpu;
2140 sid->tid = e->tid;
2141 }
2142 return 0;
2143}
2144
2145int perf_event__synthesize_id_index(struct perf_tool *tool,
2146 perf_event__handler_t process,
2147 struct perf_evlist *evlist,
2148 struct machine *machine)
2149{
2150 union perf_event *ev;
2151 struct perf_evsel *evsel;
2152 size_t nr = 0, i = 0, sz, max_nr, n;
2153 int err;
2154
2155 pr_debug2("Synthesizing id index\n");
2156
2157 max_nr = (UINT16_MAX - sizeof(struct id_index_event)) /
2158 sizeof(struct id_index_entry);
2159
Arnaldo Carvalho de Melocba9b842014-10-29 11:31:54 -02002160 evlist__for_each(evlist, evsel)
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002161 nr += evsel->ids;
2162
2163 n = nr > max_nr ? max_nr : nr;
2164 sz = sizeof(struct id_index_event) + n * sizeof(struct id_index_entry);
2165 ev = zalloc(sz);
2166 if (!ev)
2167 return -ENOMEM;
2168
2169 ev->id_index.header.type = PERF_RECORD_ID_INDEX;
2170 ev->id_index.header.size = sz;
2171 ev->id_index.nr = n;
2172
Arnaldo Carvalho de Melocba9b842014-10-29 11:31:54 -02002173 evlist__for_each(evlist, evsel) {
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002174 u32 j;
2175
2176 for (j = 0; j < evsel->ids; j++) {
2177 struct id_index_entry *e;
2178 struct perf_sample_id *sid;
2179
2180 if (i >= n) {
2181 err = process(tool, ev, NULL, machine);
2182 if (err)
2183 goto out_err;
2184 nr -= n;
2185 i = 0;
2186 }
2187
2188 e = &ev->id_index.entries[i++];
2189
2190 e->id = evsel->id[j];
2191
2192 sid = perf_evlist__id2sid(evlist, e->id);
2193 if (!sid) {
2194 free(ev);
2195 return -ENOENT;
2196 }
2197
2198 e->idx = sid->idx;
2199 e->cpu = sid->cpu;
2200 e->tid = sid->tid;
2201 }
2202 }
2203
2204 sz = sizeof(struct id_index_event) + nr * sizeof(struct id_index_entry);
2205 ev->id_index.header.size = sz;
2206 ev->id_index.nr = nr;
2207
2208 err = process(tool, ev, NULL, machine);
2209out_err:
2210 free(ev);
2211
2212 return err;
2213}