blob: 7dc1096264c575cbbd1cd41d07f041585a997453 [file] [log] [blame]
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03001#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03002#include <inttypes.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02003#include <linux/kernel.h>
Robert Richter4e319022013-06-11 17:29:18 +02004#include <traceevent/event-parse.h>
Alexander Shishkin05a1f472017-03-16 18:41:59 +02005#include <api/fs/fs.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02006
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02007#include <byteswap.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02008#include <unistd.h>
9#include <sys/types.h>
Arnaldo Carvalho de Meloa41794c2010-05-18 18:29:23 -030010#include <sys/mman.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020011
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -030012#include "evlist.h"
13#include "evsel.h"
Arnaldo Carvalho de Melo98521b32017-04-25 15:45:35 -030014#include "memswap.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020015#include "session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020016#include "tool.h"
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -020017#include "sort.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020018#include "util.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100019#include "cpumap.h"
Jiri Olsa0f6a3012012-08-07 15:20:45 +020020#include "perf_regs.h"
Jiri Olsab0a45202014-06-12 09:50:11 +020021#include "asm/bug.h"
Adrian Hunterc4468702015-04-09 18:53:48 +030022#include "auxtrace.h"
Arnaldo Carvalho de Meloe7ff8922017-04-19 21:34:35 -030023#include "thread.h"
Adrian Huntera5499b32015-05-29 16:33:30 +030024#include "thread-stack.h"
Jiri Olsa2d2aea62015-10-25 15:51:42 +010025#include "stat.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020026
Adrian Hunterc4468702015-04-09 18:53:48 +030027static int perf_session__deliver_event(struct perf_session *session,
28 union perf_event *event,
29 struct perf_sample *sample,
30 struct perf_tool *tool,
31 u64 file_offset);
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -030032
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030033static int perf_session__open(struct perf_session *session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020034{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030035 struct perf_data_file *file = session->file;
Tom Zanussi8dc58102010-04-01 23:59:15 -050036
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030037 if (perf_session__read_header(session) < 0) {
Arnaldo Carvalho de Meloe87b4912015-11-09 17:12:03 -030038 pr_err("incompatible file format (rerun with -v to learn more)\n");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020039 return -1;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020040 }
41
Jiri Olsacc9784bd2013-10-15 16:27:34 +020042 if (perf_data_file__is_pipe(file))
43 return 0;
44
Jiri Olsa3ba78bd2015-11-05 15:40:47 +010045 if (perf_header__has_feat(&session->header, HEADER_STAT))
46 return 0;
47
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030048 if (!perf_evlist__valid_sample_type(session->evlist)) {
Arnaldo Carvalho de Meloe87b4912015-11-09 17:12:03 -030049 pr_err("non matching sample_type\n");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020050 return -1;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030051 }
52
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030053 if (!perf_evlist__valid_sample_id_all(session->evlist)) {
Arnaldo Carvalho de Meloe87b4912015-11-09 17:12:03 -030054 pr_err("non matching sample_id_all\n");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020055 return -1;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030056 }
57
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030058 if (!perf_evlist__valid_read_format(session->evlist)) {
Arnaldo Carvalho de Meloe87b4912015-11-09 17:12:03 -030059 pr_err("non matching read_format\n");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020060 return -1;
Jiri Olsa9ede4732012-10-10 17:38:13 +020061 }
62
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020063 return 0;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020064}
65
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030066void perf_session__set_id_hdr_size(struct perf_session *session)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020067{
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030068 u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist);
69
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030070 machines__set_id_hdr_size(&session->machines, id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020071}
72
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030073int perf_session__create_kernel_maps(struct perf_session *session)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080074{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030075 int ret = machine__create_kernel_maps(&session->machines.host);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080076
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080077 if (ret >= 0)
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030078 ret = machines__create_guest_kernel_maps(&session->machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080079 return ret;
80}
81
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030082static void perf_session__destroy_kernel_maps(struct perf_session *session)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -030083{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030084 machines__destroy_kernel_maps(&session->machines);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -030085}
86
Adrian Huntercfe1c412014-07-31 09:00:45 +030087static bool perf_session__has_comm_exec(struct perf_session *session)
88{
89 struct perf_evsel *evsel;
90
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -030091 evlist__for_each_entry(session->evlist, evsel) {
Adrian Huntercfe1c412014-07-31 09:00:45 +030092 if (evsel->attr.comm_exec)
93 return true;
94 }
95
96 return false;
97}
98
99static void perf_session__set_comm_exec(struct perf_session *session)
100{
101 bool comm_exec = perf_session__has_comm_exec(session);
102
103 machines__set_comm_exec(&session->machines, comm_exec);
104}
105
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300106static int ordered_events__deliver_event(struct ordered_events *oe,
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300107 struct ordered_event *event)
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300108{
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300109 struct perf_sample sample;
110 struct perf_session *session = container_of(oe, struct perf_session,
111 ordered_events);
112 int ret = perf_evlist__parse_sample(session->evlist, event->event, &sample);
113
114 if (ret) {
115 pr_err("Can't parse sample, err = %d\n", ret);
116 return ret;
117 }
118
Adrian Hunterc4468702015-04-09 18:53:48 +0300119 return perf_session__deliver_event(session, event->event, &sample,
120 session->tool, event->file_offset);
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300121}
122
Jiri Olsaf5fc14122013-10-15 16:27:32 +0200123struct perf_session *perf_session__new(struct perf_data_file *file,
124 bool repipe, struct perf_tool *tool)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200125{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300126 struct perf_session *session = zalloc(sizeof(*session));
Robert Richterefad1412011-12-07 10:02:54 +0100127
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300128 if (!session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200129 goto out;
130
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300131 session->repipe = repipe;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300132 session->tool = tool;
Adrian Hunter99fa2982015-04-30 17:37:25 +0300133 INIT_LIST_HEAD(&session->auxtrace_index);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300134 machines__init(&session->machines);
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300135 ordered_events__init(&session->ordered_events, ordered_events__deliver_event);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200136
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200137 if (file) {
138 if (perf_data_file__open(file))
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200139 goto out_delete;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200140
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300141 session->file = file;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200142
143 if (perf_data_file__is_read(file)) {
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300144 if (perf_session__open(session) < 0)
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200145 goto out_close;
146
David Carrillo-Cisneros0973ad92017-04-10 13:14:30 -0700147 /*
148 * set session attributes that are present in perf.data
149 * but not in pipe-mode.
150 */
151 if (!file->is_pipe) {
152 perf_session__set_id_hdr_size(session);
153 perf_session__set_comm_exec(session);
154 }
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200155 }
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -0300156 } else {
157 session->machines.host.env = &perf_env;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200158 }
159
160 if (!file || perf_data_file__is_write(file)) {
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200161 /*
162 * In O_RDONLY mode this will be performed when reading the
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200163 * kernel MMAP event, in perf_event__process_mmap().
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200164 */
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300165 if (perf_session__create_kernel_maps(session) < 0)
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700166 pr_warning("Cannot read kernel map\n");
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200167 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200168
David Carrillo-Cisneros0973ad92017-04-10 13:14:30 -0700169 /*
170 * In pipe-mode, evlist is empty until PERF_RECORD_HEADER_ATTR is
171 * processed, so perf_evlist__sample_id_all is not meaningful here.
172 */
173 if ((!file || !file->is_pipe) && tool && tool->ordering_requires_timestamps &&
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200174 tool->ordered_events && !perf_evlist__sample_id_all(session->evlist)) {
Ian Munsie21ef97f2010-12-10 14:09:16 +1100175 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200176 tool->ordered_events = false;
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300177 }
Ian Munsie21ef97f2010-12-10 14:09:16 +1100178
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300179 return session;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200180
181 out_close:
182 perf_data_file__close(file);
183 out_delete:
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300184 perf_session__delete(session);
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200185 out:
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200186 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200187}
188
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200189static void perf_session__delete_threads(struct perf_session *session)
190{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300191 machine__delete_threads(&session->machines.host);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200192}
193
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300194void perf_session__delete(struct perf_session *session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200195{
Arnaldo Carvalho de Meloe1446552016-06-22 10:02:16 -0300196 if (session == NULL)
197 return;
Adrian Hunterc4468702015-04-09 18:53:48 +0300198 auxtrace__free(session);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300199 auxtrace_index__free(&session->auxtrace_index);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300200 perf_session__destroy_kernel_maps(session);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300201 perf_session__delete_threads(session);
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -0300202 perf_env__exit(&session->header.env);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300203 machines__exit(&session->machines);
204 if (session->file)
205 perf_data_file__close(session->file);
206 free(session);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200207}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200208
Adrian Hunter47c3d102013-07-04 16:20:21 +0300209static int process_event_synth_tracing_data_stub(struct perf_tool *tool
210 __maybe_unused,
211 union perf_event *event
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300212 __maybe_unused,
213 struct perf_session *session
214 __maybe_unused)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200215{
216 dump_printf(": unhandled!\n");
217 return 0;
218}
219
Adrian Hunter47c3d102013-07-04 16:20:21 +0300220static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused,
221 union perf_event *event __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300222 struct perf_evlist **pevlist
223 __maybe_unused)
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200224{
225 dump_printf(": unhandled!\n");
226 return 0;
227}
228
Jiri Olsaffe777252015-10-25 15:51:36 +0100229static int process_event_synth_event_update_stub(struct perf_tool *tool __maybe_unused,
230 union perf_event *event __maybe_unused,
231 struct perf_evlist **pevlist
232 __maybe_unused)
233{
Jiri Olsa2d2aea62015-10-25 15:51:42 +0100234 if (dump_trace)
235 perf_event__fprintf_event_update(event, stdout);
236
Jiri Olsaffe777252015-10-25 15:51:36 +0100237 dump_printf(": unhandled!\n");
238 return 0;
239}
240
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300241static int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
242 union perf_event *event __maybe_unused,
243 struct perf_sample *sample __maybe_unused,
244 struct perf_evsel *evsel __maybe_unused,
245 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300246{
247 dump_printf(": unhandled!\n");
248 return 0;
249}
250
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300251static int process_event_stub(struct perf_tool *tool __maybe_unused,
252 union perf_event *event __maybe_unused,
253 struct perf_sample *sample __maybe_unused,
254 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200255{
256 dump_printf(": unhandled!\n");
257 return 0;
258}
259
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300260static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
261 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300262 struct ordered_events *oe __maybe_unused)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200263{
264 dump_printf(": unhandled!\n");
265 return 0;
266}
267
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200268static int process_finished_round(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200269 union perf_event *event,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300270 struct ordered_events *oe);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200271
Adrian Huntera16ac022015-04-09 18:53:43 +0300272static int skipn(int fd, off_t n)
273{
274 char buf[4096];
275 ssize_t ret;
276
277 while (n > 0) {
278 ret = read(fd, buf, min(n, (off_t)sizeof(buf)));
279 if (ret <= 0)
280 return ret;
281 n -= ret;
282 }
283
284 return 0;
285}
286
287static s64 process_event_auxtrace_stub(struct perf_tool *tool __maybe_unused,
288 union perf_event *event,
289 struct perf_session *session
290 __maybe_unused)
291{
292 dump_printf(": unhandled!\n");
293 if (perf_data_file__is_pipe(session->file))
294 skipn(perf_data_file__fd(session->file), event->auxtrace.size);
295 return event->auxtrace.size;
296}
297
Adrian Hunter5fb0ac12016-03-07 16:44:39 -0300298static int process_event_op2_stub(struct perf_tool *tool __maybe_unused,
299 union perf_event *event __maybe_unused,
300 struct perf_session *session __maybe_unused)
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300301{
302 dump_printf(": unhandled!\n");
303 return 0;
304}
305
Jiri Olsa5f3339d2015-10-25 15:51:19 +0100306
307static
308int process_event_thread_map_stub(struct perf_tool *tool __maybe_unused,
309 union perf_event *event __maybe_unused,
310 struct perf_session *session __maybe_unused)
311{
Jiri Olsa2d2aea62015-10-25 15:51:42 +0100312 if (dump_trace)
313 perf_event__fprintf_thread_map(event, stdout);
314
Jiri Olsa5f3339d2015-10-25 15:51:19 +0100315 dump_printf(": unhandled!\n");
316 return 0;
317}
318
Jiri Olsa6640b6c2015-10-25 15:51:23 +0100319static
320int process_event_cpu_map_stub(struct perf_tool *tool __maybe_unused,
321 union perf_event *event __maybe_unused,
322 struct perf_session *session __maybe_unused)
323{
Jiri Olsa2d2aea62015-10-25 15:51:42 +0100324 if (dump_trace)
325 perf_event__fprintf_cpu_map(event, stdout);
326
Jiri Olsa6640b6c2015-10-25 15:51:23 +0100327 dump_printf(": unhandled!\n");
328 return 0;
329}
330
Jiri Olsa374fb9e2015-10-25 15:51:27 +0100331static
332int process_event_stat_config_stub(struct perf_tool *tool __maybe_unused,
333 union perf_event *event __maybe_unused,
334 struct perf_session *session __maybe_unused)
335{
Jiri Olsa2d2aea62015-10-25 15:51:42 +0100336 if (dump_trace)
337 perf_event__fprintf_stat_config(event, stdout);
338
Jiri Olsa374fb9e2015-10-25 15:51:27 +0100339 dump_printf(": unhandled!\n");
340 return 0;
341}
342
Jiri Olsad80518c2015-10-25 15:51:30 +0100343static int process_stat_stub(struct perf_tool *tool __maybe_unused,
344 union perf_event *event __maybe_unused,
345 struct perf_session *perf_session
346 __maybe_unused)
347{
Jiri Olsa2d2aea62015-10-25 15:51:42 +0100348 if (dump_trace)
349 perf_event__fprintf_stat(event, stdout);
350
Jiri Olsad80518c2015-10-25 15:51:30 +0100351 dump_printf(": unhandled!\n");
352 return 0;
353}
354
Jiri Olsa2d8f0f12015-10-25 15:51:33 +0100355static int process_stat_round_stub(struct perf_tool *tool __maybe_unused,
356 union perf_event *event __maybe_unused,
357 struct perf_session *perf_session
358 __maybe_unused)
359{
Jiri Olsa2d2aea62015-10-25 15:51:42 +0100360 if (dump_trace)
361 perf_event__fprintf_stat_round(event, stdout);
362
Jiri Olsa2d8f0f12015-10-25 15:51:33 +0100363 dump_printf(": unhandled!\n");
364 return 0;
365}
366
David Ahern9c501402013-08-02 14:05:41 -0600367void perf_tool__fill_defaults(struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200368{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200369 if (tool->sample == NULL)
370 tool->sample = process_event_sample_stub;
371 if (tool->mmap == NULL)
372 tool->mmap = process_event_stub;
David Ahern6adb0b02013-09-22 19:44:59 -0600373 if (tool->mmap2 == NULL)
374 tool->mmap2 = process_event_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200375 if (tool->comm == NULL)
376 tool->comm = process_event_stub;
377 if (tool->fork == NULL)
378 tool->fork = process_event_stub;
379 if (tool->exit == NULL)
380 tool->exit = process_event_stub;
381 if (tool->lost == NULL)
382 tool->lost = perf_event__process_lost;
Kan Liangc4937a92015-05-10 15:13:15 -0400383 if (tool->lost_samples == NULL)
384 tool->lost_samples = perf_event__process_lost_samples;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300385 if (tool->aux == NULL)
386 tool->aux = perf_event__process_aux;
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300387 if (tool->itrace_start == NULL)
388 tool->itrace_start = perf_event__process_itrace_start;
Adrian Hunter02860392015-07-21 12:44:03 +0300389 if (tool->context_switch == NULL)
390 tool->context_switch = perf_event__process_switch;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200391 if (tool->read == NULL)
392 tool->read = process_event_sample_stub;
393 if (tool->throttle == NULL)
394 tool->throttle = process_event_stub;
395 if (tool->unthrottle == NULL)
396 tool->unthrottle = process_event_stub;
397 if (tool->attr == NULL)
398 tool->attr = process_event_synth_attr_stub;
Jiri Olsaffe777252015-10-25 15:51:36 +0100399 if (tool->event_update == NULL)
400 tool->event_update = process_event_synth_event_update_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200401 if (tool->tracing_data == NULL)
402 tool->tracing_data = process_event_synth_tracing_data_stub;
403 if (tool->build_id == NULL)
Adrian Hunter5fb0ac12016-03-07 16:44:39 -0300404 tool->build_id = process_event_op2_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200405 if (tool->finished_round == NULL) {
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200406 if (tool->ordered_events)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200407 tool->finished_round = process_finished_round;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200408 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200409 tool->finished_round = process_finished_round_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200410 }
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200411 if (tool->id_index == NULL)
Adrian Hunter5fb0ac12016-03-07 16:44:39 -0300412 tool->id_index = process_event_op2_stub;
Adrian Huntera16ac022015-04-09 18:53:43 +0300413 if (tool->auxtrace_info == NULL)
Adrian Hunter5fb0ac12016-03-07 16:44:39 -0300414 tool->auxtrace_info = process_event_op2_stub;
Adrian Huntera16ac022015-04-09 18:53:43 +0300415 if (tool->auxtrace == NULL)
416 tool->auxtrace = process_event_auxtrace_stub;
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300417 if (tool->auxtrace_error == NULL)
Adrian Hunter5fb0ac12016-03-07 16:44:39 -0300418 tool->auxtrace_error = process_event_op2_stub;
Jiri Olsa5f3339d2015-10-25 15:51:19 +0100419 if (tool->thread_map == NULL)
420 tool->thread_map = process_event_thread_map_stub;
Jiri Olsa6640b6c2015-10-25 15:51:23 +0100421 if (tool->cpu_map == NULL)
422 tool->cpu_map = process_event_cpu_map_stub;
Jiri Olsa374fb9e2015-10-25 15:51:27 +0100423 if (tool->stat_config == NULL)
424 tool->stat_config = process_event_stat_config_stub;
Jiri Olsad80518c2015-10-25 15:51:30 +0100425 if (tool->stat == NULL)
426 tool->stat = process_stat_stub;
Jiri Olsa2d8f0f12015-10-25 15:51:33 +0100427 if (tool->stat_round == NULL)
428 tool->stat_round = process_stat_round_stub;
Adrian Hunter46bc29b2016-03-08 10:38:44 +0200429 if (tool->time_conv == NULL)
430 tool->time_conv = process_event_op2_stub;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200431}
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300432
Jiri Olsa268fb202012-05-30 14:23:43 +0200433static void swap_sample_id_all(union perf_event *event, void *data)
434{
435 void *end = (void *) event + event->header.size;
436 int size = end - data;
437
438 BUG_ON(size % sizeof(u64));
439 mem_bswap_64(data, size);
440}
441
442static void perf_event__all64_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300443 bool sample_id_all __maybe_unused)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200444{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200445 struct perf_event_header *hdr = &event->header;
446 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200447}
448
Jiri Olsa268fb202012-05-30 14:23:43 +0200449static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200450{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200451 event->comm.pid = bswap_32(event->comm.pid);
452 event->comm.tid = bswap_32(event->comm.tid);
Jiri Olsa268fb202012-05-30 14:23:43 +0200453
454 if (sample_id_all) {
455 void *data = &event->comm.comm;
456
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300457 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200458 swap_sample_id_all(event, data);
459 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200460}
461
Jiri Olsa268fb202012-05-30 14:23:43 +0200462static void perf_event__mmap_swap(union perf_event *event,
463 bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200464{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200465 event->mmap.pid = bswap_32(event->mmap.pid);
466 event->mmap.tid = bswap_32(event->mmap.tid);
467 event->mmap.start = bswap_64(event->mmap.start);
468 event->mmap.len = bswap_64(event->mmap.len);
469 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
Jiri Olsa268fb202012-05-30 14:23:43 +0200470
471 if (sample_id_all) {
472 void *data = &event->mmap.filename;
473
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300474 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200475 swap_sample_id_all(event, data);
476 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200477}
478
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200479static void perf_event__mmap2_swap(union perf_event *event,
480 bool sample_id_all)
481{
482 event->mmap2.pid = bswap_32(event->mmap2.pid);
483 event->mmap2.tid = bswap_32(event->mmap2.tid);
484 event->mmap2.start = bswap_64(event->mmap2.start);
485 event->mmap2.len = bswap_64(event->mmap2.len);
486 event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
487 event->mmap2.maj = bswap_32(event->mmap2.maj);
488 event->mmap2.min = bswap_32(event->mmap2.min);
489 event->mmap2.ino = bswap_64(event->mmap2.ino);
490
491 if (sample_id_all) {
492 void *data = &event->mmap2.filename;
493
494 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
495 swap_sample_id_all(event, data);
496 }
497}
Jiri Olsa268fb202012-05-30 14:23:43 +0200498static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200499{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200500 event->fork.pid = bswap_32(event->fork.pid);
501 event->fork.tid = bswap_32(event->fork.tid);
502 event->fork.ppid = bswap_32(event->fork.ppid);
503 event->fork.ptid = bswap_32(event->fork.ptid);
504 event->fork.time = bswap_64(event->fork.time);
Jiri Olsa268fb202012-05-30 14:23:43 +0200505
506 if (sample_id_all)
507 swap_sample_id_all(event, &event->fork + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200508}
509
Jiri Olsa268fb202012-05-30 14:23:43 +0200510static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200511{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200512 event->read.pid = bswap_32(event->read.pid);
513 event->read.tid = bswap_32(event->read.tid);
514 event->read.value = bswap_64(event->read.value);
515 event->read.time_enabled = bswap_64(event->read.time_enabled);
516 event->read.time_running = bswap_64(event->read.time_running);
517 event->read.id = bswap_64(event->read.id);
Jiri Olsa268fb202012-05-30 14:23:43 +0200518
519 if (sample_id_all)
520 swap_sample_id_all(event, &event->read + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200521}
522
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300523static void perf_event__aux_swap(union perf_event *event, bool sample_id_all)
524{
525 event->aux.aux_offset = bswap_64(event->aux.aux_offset);
526 event->aux.aux_size = bswap_64(event->aux.aux_size);
527 event->aux.flags = bswap_64(event->aux.flags);
528
529 if (sample_id_all)
530 swap_sample_id_all(event, &event->aux + 1);
531}
532
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300533static void perf_event__itrace_start_swap(union perf_event *event,
534 bool sample_id_all)
535{
536 event->itrace_start.pid = bswap_32(event->itrace_start.pid);
537 event->itrace_start.tid = bswap_32(event->itrace_start.tid);
538
539 if (sample_id_all)
540 swap_sample_id_all(event, &event->itrace_start + 1);
541}
542
Adrian Hunter02860392015-07-21 12:44:03 +0300543static void perf_event__switch_swap(union perf_event *event, bool sample_id_all)
544{
545 if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
546 event->context_switch.next_prev_pid =
547 bswap_32(event->context_switch.next_prev_pid);
548 event->context_switch.next_prev_tid =
549 bswap_32(event->context_switch.next_prev_tid);
550 }
551
552 if (sample_id_all)
553 swap_sample_id_all(event, &event->context_switch + 1);
554}
555
Jiri Olsadd96c462013-09-01 12:36:15 +0200556static void perf_event__throttle_swap(union perf_event *event,
557 bool sample_id_all)
558{
559 event->throttle.time = bswap_64(event->throttle.time);
560 event->throttle.id = bswap_64(event->throttle.id);
561 event->throttle.stream_id = bswap_64(event->throttle.stream_id);
562
563 if (sample_id_all)
564 swap_sample_id_all(event, &event->throttle + 1);
565}
566
Jiri Olsae108c662012-05-16 08:59:03 +0200567static u8 revbyte(u8 b)
568{
569 int rev = (b >> 4) | ((b & 0xf) << 4);
570 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
571 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
572 return (u8) rev;
573}
574
575/*
576 * XXX this is hack in attempt to carry flags bitfield
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -0800577 * through endian village. ABI says:
Jiri Olsae108c662012-05-16 08:59:03 +0200578 *
579 * Bit-fields are allocated from right to left (least to most significant)
580 * on little-endian implementations and from left to right (most to least
581 * significant) on big-endian implementations.
582 *
583 * The above seems to be byte specific, so we need to reverse each
584 * byte of the bitfield. 'Internet' also says this might be implementation
585 * specific and we probably need proper fix and carry perf_event_attr
586 * bitfield flags in separate data file FEAT_ section. Thought this seems
587 * to work for now.
588 */
589static void swap_bitfield(u8 *p, unsigned len)
590{
591 unsigned i;
592
593 for (i = 0; i < len; i++) {
594 *p = revbyte(*p);
595 p++;
596 }
597}
598
David Aherneda39132011-07-15 12:34:09 -0600599/* exported for swapping attributes in file header */
600void perf_event__attr_swap(struct perf_event_attr *attr)
601{
602 attr->type = bswap_32(attr->type);
603 attr->size = bswap_32(attr->size);
Jiri Olsae108c662012-05-16 08:59:03 +0200604
Wang Nanb30b6172015-06-17 09:56:39 +0000605#define bswap_safe(f, n) \
606 (attr->size > (offsetof(struct perf_event_attr, f) + \
607 sizeof(attr->f) * (n)))
608#define bswap_field(f, sz) \
609do { \
610 if (bswap_safe(f, 0)) \
611 attr->f = bswap_##sz(attr->f); \
612} while(0)
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300613#define bswap_field_16(f) bswap_field(f, 16)
Wang Nanb30b6172015-06-17 09:56:39 +0000614#define bswap_field_32(f) bswap_field(f, 32)
615#define bswap_field_64(f) bswap_field(f, 64)
616
617 bswap_field_64(config);
618 bswap_field_64(sample_period);
619 bswap_field_64(sample_type);
620 bswap_field_64(read_format);
621 bswap_field_32(wakeup_events);
622 bswap_field_32(bp_type);
623 bswap_field_64(bp_addr);
624 bswap_field_64(bp_len);
625 bswap_field_64(branch_sample_type);
626 bswap_field_64(sample_regs_user);
627 bswap_field_32(sample_stack_user);
628 bswap_field_32(aux_watermark);
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300629 bswap_field_16(sample_max_stack);
Wang Nanb30b6172015-06-17 09:56:39 +0000630
631 /*
632 * After read_format are bitfields. Check read_format because
633 * we are unable to use offsetof on bitfield.
634 */
635 if (bswap_safe(read_format, 1))
636 swap_bitfield((u8 *) (&attr->read_format + 1),
637 sizeof(u64));
638#undef bswap_field_64
639#undef bswap_field_32
640#undef bswap_field
641#undef bswap_safe
David Aherneda39132011-07-15 12:34:09 -0600642}
643
Jiri Olsa268fb202012-05-30 14:23:43 +0200644static void perf_event__hdr_attr_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300645 bool sample_id_all __maybe_unused)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500646{
647 size_t size;
648
David Aherneda39132011-07-15 12:34:09 -0600649 perf_event__attr_swap(&event->attr.attr);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500650
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200651 size = event->header.size;
652 size -= (void *)&event->attr.id - (void *)event;
653 mem_bswap_64(event->attr.id, size);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500654}
655
Jiri Olsaffe777252015-10-25 15:51:36 +0100656static void perf_event__event_update_swap(union perf_event *event,
657 bool sample_id_all __maybe_unused)
658{
659 event->event_update.type = bswap_64(event->event_update.type);
660 event->event_update.id = bswap_64(event->event_update.id);
661}
662
Jiri Olsa268fb202012-05-30 14:23:43 +0200663static void perf_event__event_type_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300664 bool sample_id_all __maybe_unused)
Tom Zanussicd19a032010-04-01 23:59:20 -0500665{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200666 event->event_type.event_type.event_id =
667 bswap_64(event->event_type.event_type.event_id);
Tom Zanussicd19a032010-04-01 23:59:20 -0500668}
669
Jiri Olsa268fb202012-05-30 14:23:43 +0200670static void perf_event__tracing_data_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300671 bool sample_id_all __maybe_unused)
Tom Zanussi92155452010-04-01 23:59:21 -0500672{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200673 event->tracing_data.size = bswap_32(event->tracing_data.size);
Tom Zanussi92155452010-04-01 23:59:21 -0500674}
675
Adrian Huntera16ac022015-04-09 18:53:43 +0300676static void perf_event__auxtrace_info_swap(union perf_event *event,
677 bool sample_id_all __maybe_unused)
678{
679 size_t size;
680
681 event->auxtrace_info.type = bswap_32(event->auxtrace_info.type);
682
683 size = event->header.size;
684 size -= (void *)&event->auxtrace_info.priv - (void *)event;
685 mem_bswap_64(event->auxtrace_info.priv, size);
686}
687
688static void perf_event__auxtrace_swap(union perf_event *event,
689 bool sample_id_all __maybe_unused)
690{
691 event->auxtrace.size = bswap_64(event->auxtrace.size);
692 event->auxtrace.offset = bswap_64(event->auxtrace.offset);
693 event->auxtrace.reference = bswap_64(event->auxtrace.reference);
694 event->auxtrace.idx = bswap_32(event->auxtrace.idx);
695 event->auxtrace.tid = bswap_32(event->auxtrace.tid);
696 event->auxtrace.cpu = bswap_32(event->auxtrace.cpu);
697}
698
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300699static void perf_event__auxtrace_error_swap(union perf_event *event,
700 bool sample_id_all __maybe_unused)
701{
702 event->auxtrace_error.type = bswap_32(event->auxtrace_error.type);
703 event->auxtrace_error.code = bswap_32(event->auxtrace_error.code);
704 event->auxtrace_error.cpu = bswap_32(event->auxtrace_error.cpu);
705 event->auxtrace_error.pid = bswap_32(event->auxtrace_error.pid);
706 event->auxtrace_error.tid = bswap_32(event->auxtrace_error.tid);
707 event->auxtrace_error.ip = bswap_64(event->auxtrace_error.ip);
708}
709
Jiri Olsa5f3339d2015-10-25 15:51:19 +0100710static void perf_event__thread_map_swap(union perf_event *event,
711 bool sample_id_all __maybe_unused)
712{
713 unsigned i;
714
715 event->thread_map.nr = bswap_64(event->thread_map.nr);
716
717 for (i = 0; i < event->thread_map.nr; i++)
718 event->thread_map.entries[i].pid = bswap_64(event->thread_map.entries[i].pid);
719}
720
Jiri Olsa6640b6c2015-10-25 15:51:23 +0100721static void perf_event__cpu_map_swap(union perf_event *event,
722 bool sample_id_all __maybe_unused)
723{
724 struct cpu_map_data *data = &event->cpu_map.data;
725 struct cpu_map_entries *cpus;
726 struct cpu_map_mask *mask;
727 unsigned i;
728
729 data->type = bswap_64(data->type);
730
731 switch (data->type) {
732 case PERF_CPU_MAP__CPUS:
733 cpus = (struct cpu_map_entries *)data->data;
734
735 cpus->nr = bswap_16(cpus->nr);
736
737 for (i = 0; i < cpus->nr; i++)
738 cpus->cpu[i] = bswap_16(cpus->cpu[i]);
739 break;
740 case PERF_CPU_MAP__MASK:
741 mask = (struct cpu_map_mask *) data->data;
742
743 mask->nr = bswap_16(mask->nr);
744 mask->long_size = bswap_16(mask->long_size);
745
746 switch (mask->long_size) {
747 case 4: mem_bswap_32(&mask->mask, mask->nr); break;
748 case 8: mem_bswap_64(&mask->mask, mask->nr); break;
749 default:
750 pr_err("cpu_map swap: unsupported long size\n");
751 }
752 default:
753 break;
754 }
755}
756
Jiri Olsa374fb9e2015-10-25 15:51:27 +0100757static void perf_event__stat_config_swap(union perf_event *event,
758 bool sample_id_all __maybe_unused)
759{
760 u64 size;
761
762 size = event->stat_config.nr * sizeof(event->stat_config.data[0]);
763 size += 1; /* nr item itself */
764 mem_bswap_64(&event->stat_config.nr, size);
765}
766
Jiri Olsad80518c2015-10-25 15:51:30 +0100767static void perf_event__stat_swap(union perf_event *event,
768 bool sample_id_all __maybe_unused)
769{
770 event->stat.id = bswap_64(event->stat.id);
771 event->stat.thread = bswap_32(event->stat.thread);
772 event->stat.cpu = bswap_32(event->stat.cpu);
773 event->stat.val = bswap_64(event->stat.val);
774 event->stat.ena = bswap_64(event->stat.ena);
775 event->stat.run = bswap_64(event->stat.run);
776}
777
Jiri Olsa2d8f0f12015-10-25 15:51:33 +0100778static void perf_event__stat_round_swap(union perf_event *event,
779 bool sample_id_all __maybe_unused)
780{
781 event->stat_round.type = bswap_64(event->stat_round.type);
782 event->stat_round.time = bswap_64(event->stat_round.time);
783}
784
Jiri Olsa268fb202012-05-30 14:23:43 +0200785typedef void (*perf_event__swap_op)(union perf_event *event,
786 bool sample_id_all);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200787
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200788static perf_event__swap_op perf_event__swap_ops[] = {
789 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200790 [PERF_RECORD_MMAP2] = perf_event__mmap2_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200791 [PERF_RECORD_COMM] = perf_event__comm_swap,
792 [PERF_RECORD_FORK] = perf_event__task_swap,
793 [PERF_RECORD_EXIT] = perf_event__task_swap,
794 [PERF_RECORD_LOST] = perf_event__all64_swap,
795 [PERF_RECORD_READ] = perf_event__read_swap,
Jiri Olsadd96c462013-09-01 12:36:15 +0200796 [PERF_RECORD_THROTTLE] = perf_event__throttle_swap,
797 [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200798 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300799 [PERF_RECORD_AUX] = perf_event__aux_swap,
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300800 [PERF_RECORD_ITRACE_START] = perf_event__itrace_start_swap,
Kan Liangc4937a92015-05-10 15:13:15 -0400801 [PERF_RECORD_LOST_SAMPLES] = perf_event__all64_swap,
Adrian Hunter02860392015-07-21 12:44:03 +0300802 [PERF_RECORD_SWITCH] = perf_event__switch_swap,
803 [PERF_RECORD_SWITCH_CPU_WIDE] = perf_event__switch_swap,
David Aherneda39132011-07-15 12:34:09 -0600804 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200805 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
806 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
807 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200808 [PERF_RECORD_ID_INDEX] = perf_event__all64_swap,
Adrian Huntera16ac022015-04-09 18:53:43 +0300809 [PERF_RECORD_AUXTRACE_INFO] = perf_event__auxtrace_info_swap,
810 [PERF_RECORD_AUXTRACE] = perf_event__auxtrace_swap,
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300811 [PERF_RECORD_AUXTRACE_ERROR] = perf_event__auxtrace_error_swap,
Jiri Olsa5f3339d2015-10-25 15:51:19 +0100812 [PERF_RECORD_THREAD_MAP] = perf_event__thread_map_swap,
Jiri Olsa6640b6c2015-10-25 15:51:23 +0100813 [PERF_RECORD_CPU_MAP] = perf_event__cpu_map_swap,
Jiri Olsa374fb9e2015-10-25 15:51:27 +0100814 [PERF_RECORD_STAT_CONFIG] = perf_event__stat_config_swap,
Jiri Olsad80518c2015-10-25 15:51:30 +0100815 [PERF_RECORD_STAT] = perf_event__stat_swap,
Jiri Olsa2d8f0f12015-10-25 15:51:33 +0100816 [PERF_RECORD_STAT_ROUND] = perf_event__stat_round_swap,
Jiri Olsaffe777252015-10-25 15:51:36 +0100817 [PERF_RECORD_EVENT_UPDATE] = perf_event__event_update_swap,
Adrian Hunter46bc29b2016-03-08 10:38:44 +0200818 [PERF_RECORD_TIME_CONV] = perf_event__all64_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200819 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200820};
821
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200822/*
823 * When perf record finishes a pass on every buffers, it records this pseudo
824 * event.
825 * We record the max timestamp t found in the pass n.
826 * Assuming these timestamps are monotonic across cpus, we know that if
827 * a buffer still has events with timestamps below t, they will be all
828 * available and then read in the pass n + 1.
829 * Hence when we start to read the pass n + 2, we can safely flush every
830 * events with timestamps below t.
831 *
832 * ============ PASS n =================
833 * CPU 0 | CPU 1
834 * |
835 * cnt1 timestamps | cnt2 timestamps
836 * 1 | 2
837 * 2 | 3
838 * - | 4 <--- max recorded
839 *
840 * ============ PASS n + 1 ==============
841 * CPU 0 | CPU 1
842 * |
843 * cnt1 timestamps | cnt2 timestamps
844 * 3 | 5
845 * 4 | 6
846 * 5 | 7 <---- max recorded
847 *
848 * Flush every events below timestamp 4
849 *
850 * ============ PASS n + 2 ==============
851 * CPU 0 | CPU 1
852 * |
853 * cnt1 timestamps | cnt2 timestamps
854 * 6 | 8
855 * 7 | 9
856 * - | 10
857 *
858 * Flush every events below timestamp 7
859 * etc...
860 */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300861static int process_finished_round(struct perf_tool *tool __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300862 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300863 struct ordered_events *oe)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200864{
Adrian Hunter5531e162015-06-23 10:52:48 +0300865 if (dump_trace)
866 fprintf(stdout, "\n");
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300867 return ordered_events__flush(oe, OE_FLUSH__ROUND);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200868}
869
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300870int perf_session__queue_event(struct perf_session *s, union perf_event *event,
871 struct perf_sample *sample, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200872{
Arnaldo Carvalho de Melo4a6b3622015-03-03 13:02:24 -0300873 return ordered_events__queue(&s->ordered_events, event, sample, file_offset);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200874}
875
Kan Liang384b6052015-01-05 13:23:05 -0500876static void callchain__lbr_callstack_printf(struct perf_sample *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200877{
Kan Liang384b6052015-01-05 13:23:05 -0500878 struct ip_callchain *callchain = sample->callchain;
879 struct branch_stack *lbr_stack = sample->branch_stack;
880 u64 kernel_callchain_nr = callchain->nr;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200881 unsigned int i;
882
Kan Liang384b6052015-01-05 13:23:05 -0500883 for (i = 0; i < kernel_callchain_nr; i++) {
884 if (callchain->ips[i] == PERF_CONTEXT_USER)
885 break;
886 }
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200887
Kan Liang384b6052015-01-05 13:23:05 -0500888 if ((i != kernel_callchain_nr) && lbr_stack->nr) {
889 u64 total_nr;
890 /*
891 * LBR callstack can only get user call chain,
892 * i is kernel call chain number,
893 * 1 is PERF_CONTEXT_USER.
894 *
895 * The user call chain is stored in LBR registers.
896 * LBR are pair registers. The caller is stored
897 * in "from" register, while the callee is stored
898 * in "to" register.
899 * For example, there is a call stack
900 * "A"->"B"->"C"->"D".
901 * The LBR registers will recorde like
902 * "C"->"D", "B"->"C", "A"->"B".
903 * So only the first "to" register and all "from"
904 * registers are needed to construct the whole stack.
905 */
906 total_nr = i + 1 + lbr_stack->nr + 1;
907 kernel_callchain_nr = i + 1;
908
909 printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr);
910
911 for (i = 0; i < kernel_callchain_nr; i++)
912 printf("..... %2d: %016" PRIx64 "\n",
913 i, callchain->ips[i]);
914
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200915 printf("..... %2d: %016" PRIx64 "\n",
Kan Liang384b6052015-01-05 13:23:05 -0500916 (int)(kernel_callchain_nr), lbr_stack->entries[0].to);
917 for (i = 0; i < lbr_stack->nr; i++)
918 printf("..... %2d: %016" PRIx64 "\n",
919 (int)(i + kernel_callchain_nr + 1), lbr_stack->entries[i].from);
920 }
921}
922
923static void callchain__printf(struct perf_evsel *evsel,
924 struct perf_sample *sample)
925{
926 unsigned int i;
927 struct ip_callchain *callchain = sample->callchain;
928
Arnaldo Carvalho de Meloacf2abb2016-04-18 10:35:03 -0300929 if (perf_evsel__has_branch_callstack(evsel))
Kan Liang384b6052015-01-05 13:23:05 -0500930 callchain__lbr_callstack_printf(sample);
931
932 printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr);
933
934 for (i = 0; i < callchain->nr; i++)
935 printf("..... %2d: %016" PRIx64 "\n",
936 i, callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200937}
938
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100939static void branch_stack__printf(struct perf_sample *sample)
940{
941 uint64_t i;
942
943 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
944
Andi Kleen0e332f02015-07-18 08:24:46 -0700945 for (i = 0; i < sample->branch_stack->nr; i++) {
946 struct branch_entry *e = &sample->branch_stack->entries[i];
947
948 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 " %hu cycles %s%s%s%s %x\n",
949 i, e->from, e->to,
Arnaldo Carvalho de Melo8074bf52017-02-17 12:27:26 -0300950 (unsigned short)e->flags.cycles,
Andi Kleen0e332f02015-07-18 08:24:46 -0700951 e->flags.mispred ? "M" : " ",
952 e->flags.predicted ? "P" : " ",
953 e->flags.abort ? "A" : " ",
954 e->flags.in_tx ? "T" : " ",
955 (unsigned)e->flags.reserved);
956 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100957}
958
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200959static void regs_dump__printf(u64 mask, u64 *regs)
960{
961 unsigned rid, i = 0;
962
963 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
964 u64 val = regs[i++];
965
966 printf(".... %-5s 0x%" PRIx64 "\n",
967 perf_reg_name(rid), val);
968 }
969}
970
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200971static const char *regs_abi[] = {
972 [PERF_SAMPLE_REGS_ABI_NONE] = "none",
973 [PERF_SAMPLE_REGS_ABI_32] = "32-bit",
974 [PERF_SAMPLE_REGS_ABI_64] = "64-bit",
975};
976
977static inline const char *regs_dump_abi(struct regs_dump *d)
978{
979 if (d->abi > PERF_SAMPLE_REGS_ABI_64)
980 return "unknown";
981
982 return regs_abi[d->abi];
983}
984
985static void regs__printf(const char *type, struct regs_dump *regs)
986{
987 u64 mask = regs->mask;
988
989 printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n",
990 type,
991 mask,
992 regs_dump_abi(regs));
993
994 regs_dump__printf(mask, regs->regs);
995}
996
Jiri Olsa352ea452014-01-07 13:47:25 +0100997static void regs_user__printf(struct perf_sample *sample)
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200998{
999 struct regs_dump *user_regs = &sample->user_regs;
1000
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001001 if (user_regs->regs)
1002 regs__printf("user", user_regs);
1003}
1004
1005static void regs_intr__printf(struct perf_sample *sample)
1006{
1007 struct regs_dump *intr_regs = &sample->intr_regs;
1008
1009 if (intr_regs->regs)
1010 regs__printf("intr", intr_regs);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001011}
1012
1013static void stack_user__printf(struct stack_dump *dump)
1014{
1015 printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
1016 dump->size, dump->offset);
1017}
1018
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001019static void perf_evlist__print_tstamp(struct perf_evlist *evlist,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001020 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001021 struct perf_sample *sample)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -02001022{
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001023 u64 sample_type = __perf_evlist__combined_sample_type(evlist);
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001024
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -02001025 if (event->header.type != PERF_RECORD_SAMPLE &&
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001026 !perf_evlist__sample_id_all(evlist)) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -02001027 fputs("-1 -1 ", stdout);
1028 return;
1029 }
1030
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001031 if ((sample_type & PERF_SAMPLE_CPU))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -02001032 printf("%u ", sample->cpu);
1033
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001034 if (sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001035 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -02001036}
1037
Jiri Olsa9ede4732012-10-10 17:38:13 +02001038static void sample_read__printf(struct perf_sample *sample, u64 read_format)
1039{
1040 printf("... sample_read:\n");
1041
1042 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1043 printf("...... time enabled %016" PRIx64 "\n",
1044 sample->read.time_enabled);
1045
1046 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1047 printf("...... time running %016" PRIx64 "\n",
1048 sample->read.time_running);
1049
1050 if (read_format & PERF_FORMAT_GROUP) {
1051 u64 i;
1052
1053 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
1054
1055 for (i = 0; i < sample->read.group.nr; i++) {
1056 struct sample_read_value *value;
1057
1058 value = &sample->read.group.values[i];
1059 printf("..... id %016" PRIx64
1060 ", value %016" PRIx64 "\n",
1061 value->id, value->value);
1062 }
1063 } else
1064 printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
1065 sample->read.one.id, sample->read.one.value);
1066}
1067
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001068static void dump_event(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001069 u64 file_offset, struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001070{
1071 if (!dump_trace)
1072 return;
1073
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001074 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
1075 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001076
1077 trace_event(event);
1078
1079 if (sample)
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001080 perf_evlist__print_tstamp(evlist, event, sample);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001081
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001082 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001083 event->header.size, perf_event__name(event->header.type));
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001084}
1085
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001086static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001087 struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001088{
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001089 u64 sample_type;
1090
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -02001091 if (!dump_trace)
1092 return;
1093
Don Zickus0ea590a2014-02-25 22:43:46 -05001094 printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001095 event->header.misc, sample->pid, sample->tid, sample->ip,
David Ahern7cec0922011-05-30 13:08:23 -06001096 sample->period, sample->addr);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001097
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001098 sample_type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001099
1100 if (sample_type & PERF_SAMPLE_CALLCHAIN)
Kan Liang384b6052015-01-05 13:23:05 -05001101 callchain__printf(evsel, sample);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001102
Arnaldo Carvalho de Meloacf2abb2016-04-18 10:35:03 -03001103 if ((sample_type & PERF_SAMPLE_BRANCH_STACK) && !perf_evsel__has_branch_callstack(evsel))
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001104 branch_stack__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001105
1106 if (sample_type & PERF_SAMPLE_REGS_USER)
Jiri Olsa352ea452014-01-07 13:47:25 +01001107 regs_user__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001108
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001109 if (sample_type & PERF_SAMPLE_REGS_INTR)
1110 regs_intr__printf(sample);
1111
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001112 if (sample_type & PERF_SAMPLE_STACK_USER)
1113 stack_user__printf(&sample->user_stack);
Andi Kleen05484292013-01-24 16:10:29 +01001114
1115 if (sample_type & PERF_SAMPLE_WEIGHT)
1116 printf("... weight: %" PRIu64 "\n", sample->weight);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001117
1118 if (sample_type & PERF_SAMPLE_DATA_SRC)
1119 printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001120
Andi Kleen475eeab2013-09-20 07:40:43 -07001121 if (sample_type & PERF_SAMPLE_TRANSACTION)
1122 printf("... transaction: %" PRIx64 "\n", sample->transaction);
1123
Jiri Olsa9ede4732012-10-10 17:38:13 +02001124 if (sample_type & PERF_SAMPLE_READ)
1125 sample_read__printf(sample, evsel->attr.read_format);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001126}
1127
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -03001128static struct machine *machines__find_for_cpumode(struct machines *machines,
Adrian Hunteref893252013-08-27 11:23:06 +03001129 union perf_event *event,
1130 struct perf_sample *sample)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001131{
Dongsheng Yangad85ace2013-12-20 13:41:47 -05001132 struct machine *machine;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001133
David Ahern7c0f4a42012-07-20 17:25:48 -06001134 if (perf_guest &&
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001135 ((sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
1136 (sample->cpumode == PERF_RECORD_MISC_GUEST_USER))) {
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +05301137 u32 pid;
1138
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001139 if (event->header.type == PERF_RECORD_MMAP
1140 || event->header.type == PERF_RECORD_MMAP2)
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +05301141 pid = event->mmap.pid;
1142 else
Adrian Hunteref893252013-08-27 11:23:06 +03001143 pid = sample->pid;
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +05301144
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -03001145 machine = machines__find(machines, pid);
Dongsheng Yangad85ace2013-12-20 13:41:47 -05001146 if (!machine)
Ravi Bangoria3caeaa52015-12-07 12:25:02 +05301147 machine = machines__findnew(machines, DEFAULT_GUEST_KERNEL_ID);
Dongsheng Yangad85ace2013-12-20 13:41:47 -05001148 return machine;
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +05301149 }
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001150
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -03001151 return &machines->host;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001152}
1153
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001154static int deliver_sample_value(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +02001155 struct perf_tool *tool,
1156 union perf_event *event,
1157 struct perf_sample *sample,
1158 struct sample_read_value *v,
1159 struct machine *machine)
1160{
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001161 struct perf_sample_id *sid = perf_evlist__id2sid(evlist, v->id);
Jiri Olsae4caec02012-10-10 18:52:24 +02001162
Jiri Olsae4caec02012-10-10 18:52:24 +02001163 if (sid) {
1164 sample->id = v->id;
1165 sample->period = v->value - sid->period;
1166 sid->period = v->value;
1167 }
1168
1169 if (!sid || sid->evsel == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001170 ++evlist->stats.nr_unknown_id;
Jiri Olsae4caec02012-10-10 18:52:24 +02001171 return 0;
1172 }
1173
1174 return tool->sample(tool, event, sample, sid->evsel, machine);
1175}
1176
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001177static int deliver_sample_group(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +02001178 struct perf_tool *tool,
1179 union perf_event *event,
1180 struct perf_sample *sample,
1181 struct machine *machine)
1182{
1183 int ret = -EINVAL;
1184 u64 i;
1185
1186 for (i = 0; i < sample->read.group.nr; i++) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001187 ret = deliver_sample_value(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001188 &sample->read.group.values[i],
1189 machine);
1190 if (ret)
1191 break;
1192 }
1193
1194 return ret;
1195}
1196
1197static int
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001198 perf_evlist__deliver_sample(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +02001199 struct perf_tool *tool,
1200 union perf_event *event,
1201 struct perf_sample *sample,
1202 struct perf_evsel *evsel,
1203 struct machine *machine)
1204{
1205 /* We know evsel != NULL. */
1206 u64 sample_type = evsel->attr.sample_type;
1207 u64 read_format = evsel->attr.read_format;
1208
Soramichi AKIYAMAd94386f2017-01-17 22:22:33 +09001209 /* Standard sample delivery. */
Jiri Olsae4caec02012-10-10 18:52:24 +02001210 if (!(sample_type & PERF_SAMPLE_READ))
1211 return tool->sample(tool, event, sample, evsel, machine);
1212
1213 /* For PERF_SAMPLE_READ we have either single or group mode. */
1214 if (read_format & PERF_FORMAT_GROUP)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001215 return deliver_sample_group(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001216 machine);
1217 else
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001218 return deliver_sample_value(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001219 &sample->read.one, machine);
1220}
1221
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -03001222static int machines__deliver_event(struct machines *machines,
1223 struct perf_evlist *evlist,
1224 union perf_event *event,
1225 struct perf_sample *sample,
1226 struct perf_tool *tool, u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001227{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001228 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001229 struct machine *machine;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001230
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001231 dump_event(evlist, event, file_offset, sample);
Thomas Gleixner532e7262010-12-07 12:48:55 +00001232
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001233 evsel = perf_evlist__id2evsel(evlist, sample->id);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001234
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001235 machine = machines__find_for_cpumode(machines, event, sample);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001236
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001237 switch (event->header.type) {
1238 case PERF_RECORD_SAMPLE:
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001239 if (evsel == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001240 ++evlist->stats.nr_unknown_id;
Jiri Olsa67822062012-04-12 14:21:01 +02001241 return 0;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001242 }
Kan Liang1b29ac52015-09-03 08:31:00 -04001243 dump_sample(evsel, event, sample);
Joerg Roedel0c095712012-02-10 18:05:04 +01001244 if (machine == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001245 ++evlist->stats.nr_unprocessable_samples;
Jiri Olsa67822062012-04-12 14:21:01 +02001246 return 0;
Joerg Roedel0c095712012-02-10 18:05:04 +01001247 }
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001248 return perf_evlist__deliver_sample(evlist, tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001249 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001250 return tool->mmap(tool, event, sample, machine);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001251 case PERF_RECORD_MMAP2:
Kan Liang930e6fc2015-06-17 09:51:10 -04001252 if (event->header.misc & PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT)
1253 ++evlist->stats.nr_proc_map_timeout;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001254 return tool->mmap2(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001255 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001256 return tool->comm(tool, event, sample, machine);
Hari Bathinif3b36142017-03-08 02:11:43 +05301257 case PERF_RECORD_NAMESPACES:
1258 return tool->namespaces(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001259 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001260 return tool->fork(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001261 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001262 return tool->exit(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001263 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001264 if (tool->lost == perf_event__process_lost)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001265 evlist->stats.total_lost += event->lost.lost;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001266 return tool->lost(tool, event, sample, machine);
Kan Liangc4937a92015-05-10 15:13:15 -04001267 case PERF_RECORD_LOST_SAMPLES:
1268 if (tool->lost_samples == perf_event__process_lost_samples)
1269 evlist->stats.total_lost_samples += event->lost_samples.lost;
1270 return tool->lost_samples(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001271 case PERF_RECORD_READ:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001272 return tool->read(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001273 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001274 return tool->throttle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001275 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001276 return tool->unthrottle(tool, event, sample, machine);
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001277 case PERF_RECORD_AUX:
Alexander Shishkin05a1f472017-03-16 18:41:59 +02001278 if (tool->aux == perf_event__process_aux) {
1279 if (event->aux.flags & PERF_AUX_FLAG_TRUNCATED)
1280 evlist->stats.total_aux_lost += 1;
1281 if (event->aux.flags & PERF_AUX_FLAG_PARTIAL)
1282 evlist->stats.total_aux_partial += 1;
1283 }
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001284 return tool->aux(tool, event, sample, machine);
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001285 case PERF_RECORD_ITRACE_START:
1286 return tool->itrace_start(tool, event, sample, machine);
Adrian Hunter02860392015-07-21 12:44:03 +03001287 case PERF_RECORD_SWITCH:
1288 case PERF_RECORD_SWITCH_CPU_WIDE:
1289 return tool->context_switch(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001290 default:
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001291 ++evlist->stats.nr_unknown_events;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001292 return -1;
1293 }
1294}
1295
Adrian Hunterc4468702015-04-09 18:53:48 +03001296static int perf_session__deliver_event(struct perf_session *session,
1297 union perf_event *event,
1298 struct perf_sample *sample,
1299 struct perf_tool *tool,
1300 u64 file_offset)
1301{
1302 int ret;
1303
1304 ret = auxtrace__process_event(session, event, sample, tool);
1305 if (ret < 0)
1306 return ret;
1307 if (ret > 0)
1308 return 0;
1309
1310 return machines__deliver_event(&session->machines, session->evlist,
1311 event, sample, tool, file_offset);
1312}
1313
Adrian Hunterd5652d82014-07-23 22:19:58 +03001314static s64 perf_session__process_user_event(struct perf_session *session,
1315 union perf_event *event,
Adrian Hunterd5652d82014-07-23 22:19:58 +03001316 u64 file_offset)
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001317{
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -03001318 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001319 struct perf_tool *tool = session->tool;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001320 int fd = perf_data_file__fd(session->file);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001321 int err;
1322
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001323 dump_event(session->evlist, event, file_offset, NULL);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001324
1325 /* These events are processed right away */
1326 switch (event->header.type) {
1327 case PERF_RECORD_HEADER_ATTR:
Adrian Hunter47c3d102013-07-04 16:20:21 +03001328 err = tool->attr(tool, event, &session->evlist);
Adrian Huntercfe1c412014-07-31 09:00:45 +03001329 if (err == 0) {
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -03001330 perf_session__set_id_hdr_size(session);
Adrian Huntercfe1c412014-07-31 09:00:45 +03001331 perf_session__set_comm_exec(session);
1332 }
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001333 return err;
Jiri Olsaffe777252015-10-25 15:51:36 +01001334 case PERF_RECORD_EVENT_UPDATE:
1335 return tool->event_update(tool, event, &session->evlist);
Jiri Olsaf67697b2014-02-04 15:37:48 +01001336 case PERF_RECORD_HEADER_EVENT_TYPE:
1337 /*
1338 * Depreceated, but we need to handle it for sake
1339 * of old data files create in pipe mode.
1340 */
1341 return 0;
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001342 case PERF_RECORD_HEADER_TRACING_DATA:
1343 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001344 lseek(fd, file_offset, SEEK_SET);
Adrian Hunter47c3d102013-07-04 16:20:21 +03001345 return tool->tracing_data(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001346 case PERF_RECORD_HEADER_BUILD_ID:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001347 return tool->build_id(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001348 case PERF_RECORD_FINISHED_ROUND:
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -03001349 return tool->finished_round(tool, event, oe);
Adrian Hunter3c659ee2014-10-27 15:49:22 +02001350 case PERF_RECORD_ID_INDEX:
1351 return tool->id_index(tool, event, session);
Adrian Huntera16ac022015-04-09 18:53:43 +03001352 case PERF_RECORD_AUXTRACE_INFO:
1353 return tool->auxtrace_info(tool, event, session);
1354 case PERF_RECORD_AUXTRACE:
1355 /* setup for reading amidst mmap */
1356 lseek(fd, file_offset + event->header.size, SEEK_SET);
1357 return tool->auxtrace(tool, event, session);
Adrian Huntere9bf54d2015-04-09 18:53:47 +03001358 case PERF_RECORD_AUXTRACE_ERROR:
Adrian Hunter85ed4722015-04-09 18:53:50 +03001359 perf_session__auxtrace_error_inc(session, event);
Adrian Huntere9bf54d2015-04-09 18:53:47 +03001360 return tool->auxtrace_error(tool, event, session);
Jiri Olsa5f3339d2015-10-25 15:51:19 +01001361 case PERF_RECORD_THREAD_MAP:
1362 return tool->thread_map(tool, event, session);
Jiri Olsa6640b6c2015-10-25 15:51:23 +01001363 case PERF_RECORD_CPU_MAP:
1364 return tool->cpu_map(tool, event, session);
Jiri Olsa374fb9e2015-10-25 15:51:27 +01001365 case PERF_RECORD_STAT_CONFIG:
1366 return tool->stat_config(tool, event, session);
Jiri Olsad80518c2015-10-25 15:51:30 +01001367 case PERF_RECORD_STAT:
1368 return tool->stat(tool, event, session);
Jiri Olsa2d8f0f12015-10-25 15:51:33 +01001369 case PERF_RECORD_STAT_ROUND:
1370 return tool->stat_round(tool, event, session);
Adrian Hunter46bc29b2016-03-08 10:38:44 +02001371 case PERF_RECORD_TIME_CONV:
1372 session->time_conv = event->time_conv;
1373 return tool->time_conv(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001374 default:
1375 return -EINVAL;
1376 }
1377}
1378
Adrian Huntera2938292014-10-27 15:49:23 +02001379int perf_session__deliver_synth_event(struct perf_session *session,
1380 union perf_event *event,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001381 struct perf_sample *sample)
Adrian Huntera2938292014-10-27 15:49:23 +02001382{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001383 struct perf_evlist *evlist = session->evlist;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001384 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001385
1386 events_stats__inc(&evlist->stats, event->header.type);
Adrian Huntera2938292014-10-27 15:49:23 +02001387
1388 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001389 return perf_session__process_user_event(session, event, 0);
Adrian Huntera2938292014-10-27 15:49:23 +02001390
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001391 return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0);
Adrian Huntera2938292014-10-27 15:49:23 +02001392}
1393
Jiri Olsa268fb202012-05-30 14:23:43 +02001394static void event_swap(union perf_event *event, bool sample_id_all)
1395{
1396 perf_event__swap_op swap;
1397
1398 swap = perf_event__swap_ops[event->header.type];
1399 if (swap)
1400 swap(event, sample_id_all);
1401}
1402
Adrian Hunter5a52f332014-07-31 09:00:57 +03001403int perf_session__peek_event(struct perf_session *session, off_t file_offset,
1404 void *buf, size_t buf_sz,
1405 union perf_event **event_ptr,
1406 struct perf_sample *sample)
1407{
1408 union perf_event *event;
1409 size_t hdr_sz, rest;
1410 int fd;
1411
1412 if (session->one_mmap && !session->header.needs_swap) {
1413 event = file_offset - session->one_mmap_offset +
1414 session->one_mmap_addr;
1415 goto out_parse_sample;
1416 }
1417
1418 if (perf_data_file__is_pipe(session->file))
1419 return -1;
1420
1421 fd = perf_data_file__fd(session->file);
1422 hdr_sz = sizeof(struct perf_event_header);
1423
1424 if (buf_sz < hdr_sz)
1425 return -1;
1426
1427 if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 ||
Adrian Hunter554e92e2015-05-19 16:05:45 +03001428 readn(fd, buf, hdr_sz) != (ssize_t)hdr_sz)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001429 return -1;
1430
1431 event = (union perf_event *)buf;
1432
1433 if (session->header.needs_swap)
1434 perf_event_header__bswap(&event->header);
1435
Adrian Hunter554e92e2015-05-19 16:05:45 +03001436 if (event->header.size < hdr_sz || event->header.size > buf_sz)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001437 return -1;
1438
1439 rest = event->header.size - hdr_sz;
1440
Adrian Hunter554e92e2015-05-19 16:05:45 +03001441 if (readn(fd, buf, rest) != (ssize_t)rest)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001442 return -1;
1443
1444 if (session->header.needs_swap)
1445 event_swap(event, perf_evlist__sample_id_all(session->evlist));
1446
1447out_parse_sample:
1448
1449 if (sample && event->header.type < PERF_RECORD_USER_TYPE_START &&
1450 perf_evlist__parse_sample(session->evlist, event, sample))
1451 return -1;
1452
1453 *event_ptr = event;
1454
1455 return 0;
1456}
1457
Adrian Hunterd5652d82014-07-23 22:19:58 +03001458static s64 perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001459 union perf_event *event, u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001460{
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001461 struct perf_evlist *evlist = session->evlist;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001462 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001463 struct perf_sample sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001464 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001465
Jiri Olsa268fb202012-05-30 14:23:43 +02001466 if (session->header.needs_swap)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001467 event_swap(event, perf_evlist__sample_id_all(evlist));
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001468
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001469 if (event->header.type >= PERF_RECORD_HEADER_MAX)
1470 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001471
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001472 events_stats__inc(&evlist->stats, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001473
1474 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001475 return perf_session__process_user_event(session, event, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001476
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001477 /*
1478 * For all kernel events we get the sample data
1479 */
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001480 ret = perf_evlist__parse_sample(evlist, event, &sample);
Frederic Weisbecker5538bec2011-05-22 02:17:22 +02001481 if (ret)
1482 return ret;
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001483
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001484 if (tool->ordered_events) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001485 ret = perf_session__queue_event(session, event, &sample, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001486 if (ret != -ETIME)
1487 return ret;
1488 }
1489
Adrian Hunterc4468702015-04-09 18:53:48 +03001490 return perf_session__deliver_event(session, event, &sample, tool,
1491 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001492}
1493
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001494void perf_event_header__bswap(struct perf_event_header *hdr)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001495{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001496 hdr->type = bswap_32(hdr->type);
1497 hdr->misc = bswap_16(hdr->misc);
1498 hdr->size = bswap_16(hdr->size);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001499}
1500
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001501struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1502{
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001503 return machine__findnew_thread(&session->machines.host, -1, pid);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001504}
1505
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001506int perf_session__register_idle_thread(struct perf_session *session)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001507{
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001508 struct thread *thread;
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001509 int err = 0;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001510
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001511 thread = machine__findnew_thread(&session->machines.host, 0, 0);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001512 if (thread == NULL || thread__set_comm(thread, "swapper", 0)) {
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001513 pr_err("problem inserting idle task.\n");
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001514 err = -1;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001515 }
1516
Hari Bathinif3b36142017-03-08 02:11:43 +05301517 if (thread == NULL || thread__set_namespaces(thread, 0, NULL)) {
1518 pr_err("problem inserting idle task.\n");
1519 err = -1;
1520 }
1521
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001522 /* machine__findnew_thread() got the thread, so put it */
1523 thread__put(thread);
1524 return err;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001525}
1526
Wang Nanf06149c2016-07-14 08:34:46 +00001527static void
1528perf_session__warn_order(const struct perf_session *session)
1529{
1530 const struct ordered_events *oe = &session->ordered_events;
1531 struct perf_evsel *evsel;
1532 bool should_warn = true;
1533
1534 evlist__for_each_entry(session->evlist, evsel) {
1535 if (evsel->attr.write_backward)
1536 should_warn = false;
1537 }
1538
1539 if (!should_warn)
1540 return;
1541 if (oe->nr_unordered_events != 0)
1542 ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events);
1543}
1544
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001545static void perf_session__warn_about_errors(const struct perf_session *session)
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001546{
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001547 const struct events_stats *stats = &session->evlist->stats;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001548
1549 if (session->tool->lost == perf_event__process_lost &&
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001550 stats->nr_events[PERF_RECORD_LOST] != 0) {
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001551 ui__warning("Processed %d events and lost %d chunks!\n\n"
1552 "Check IO/CPU overload!\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001553 stats->nr_events[0],
1554 stats->nr_events[PERF_RECORD_LOST]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001555 }
1556
Kan Liangc4937a92015-05-10 15:13:15 -04001557 if (session->tool->lost_samples == perf_event__process_lost_samples) {
1558 double drop_rate;
1559
1560 drop_rate = (double)stats->total_lost_samples /
1561 (double) (stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples);
1562 if (drop_rate > 0.05) {
He Kuang6ba29c22015-06-11 12:44:24 +00001563 ui__warning("Processed %" PRIu64 " samples and lost %3.2f%% samples!\n\n",
Kan Liangc4937a92015-05-10 15:13:15 -04001564 stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples,
1565 drop_rate * 100.0);
1566 }
1567 }
1568
Adrian Huntera38f48e2015-09-25 16:15:37 +03001569 if (session->tool->aux == perf_event__process_aux &&
1570 stats->total_aux_lost != 0) {
1571 ui__warning("AUX data lost %" PRIu64 " times out of %u!\n\n",
1572 stats->total_aux_lost,
1573 stats->nr_events[PERF_RECORD_AUX]);
1574 }
1575
Alexander Shishkin05a1f472017-03-16 18:41:59 +02001576 if (session->tool->aux == perf_event__process_aux &&
1577 stats->total_aux_partial != 0) {
1578 bool vmm_exclusive = false;
1579
1580 (void)sysfs__read_bool("module/kvm_intel/parameters/vmm_exclusive",
1581 &vmm_exclusive);
1582
1583 ui__warning("AUX data had gaps in it %" PRIu64 " times out of %u!\n\n"
1584 "Are you running a KVM guest in the background?%s\n\n",
1585 stats->total_aux_partial,
1586 stats->nr_events[PERF_RECORD_AUX],
1587 vmm_exclusive ?
1588 "\nReloading kvm_intel module with vmm_exclusive=0\n"
1589 "will reduce the gaps to only guest's timeslices." :
1590 "");
1591 }
1592
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001593 if (stats->nr_unknown_events != 0) {
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001594 ui__warning("Found %u unknown events!\n\n"
1595 "Is this an older tool processing a perf.data "
1596 "file generated by a more recent tool?\n\n"
1597 "If that is not the case, consider "
1598 "reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001599 stats->nr_unknown_events);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001600 }
1601
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001602 if (stats->nr_unknown_id != 0) {
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001603 ui__warning("%u samples with id not present in the header\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001604 stats->nr_unknown_id);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001605 }
1606
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001607 if (stats->nr_invalid_chains != 0) {
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001608 ui__warning("Found invalid callchains!\n\n"
1609 "%u out of %u events were discarded for this reason.\n\n"
1610 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001611 stats->nr_invalid_chains,
1612 stats->nr_events[PERF_RECORD_SAMPLE]);
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001613 }
Joerg Roedel0c095712012-02-10 18:05:04 +01001614
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001615 if (stats->nr_unprocessable_samples != 0) {
Joerg Roedel0c095712012-02-10 18:05:04 +01001616 ui__warning("%u unprocessable samples recorded.\n"
1617 "Do you have a KVM guest running and not using 'perf kvm'?\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001618 stats->nr_unprocessable_samples);
Joerg Roedel0c095712012-02-10 18:05:04 +01001619 }
Jiri Olsaf61ff6c2014-11-26 16:39:31 +01001620
Wang Nanf06149c2016-07-14 08:34:46 +00001621 perf_session__warn_order(session);
Adrian Hunter85ed4722015-04-09 18:53:50 +03001622
1623 events_stats__auxtrace_error_warn(stats);
Kan Liang930e6fc2015-06-17 09:51:10 -04001624
1625 if (stats->nr_proc_map_timeout != 0) {
1626 ui__warning("%d map information files for pre-existing threads were\n"
1627 "not processed, if there are samples for addresses they\n"
1628 "will not be resolved, you may find out which are these\n"
1629 "threads by running with -v and redirecting the output\n"
Kan Liang9d9cad72015-06-17 09:51:11 -04001630 "to a file.\n"
1631 "The time limit to process proc map is too short?\n"
1632 "Increase it by --proc-map-timeout\n",
Kan Liang930e6fc2015-06-17 09:51:10 -04001633 stats->nr_proc_map_timeout);
1634 }
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001635}
1636
Adrian Huntera5499b32015-05-29 16:33:30 +03001637static int perf_session__flush_thread_stack(struct thread *thread,
1638 void *p __maybe_unused)
1639{
1640 return thread_stack__flush(thread);
1641}
1642
1643static int perf_session__flush_thread_stacks(struct perf_session *session)
1644{
1645 return machines__for_each_thread(&session->machines,
1646 perf_session__flush_thread_stack,
1647 NULL);
1648}
1649
Tom Zanussi8dc58102010-04-01 23:59:15 -05001650volatile int session_done;
1651
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001652static int __perf_session__process_pipe_events(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05001653{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001654 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001655 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001656 int fd = perf_data_file__fd(session->file);
Stephane Eranian444d2862012-05-15 13:28:12 +02001657 union perf_event *event;
1658 uint32_t size, cur_size = 0;
1659 void *buf = NULL;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001660 s64 skip = 0;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001661 u64 head;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001662 ssize_t err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001663 void *p;
1664
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001665 perf_tool__fill_defaults(tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001666
1667 head = 0;
Stephane Eranian444d2862012-05-15 13:28:12 +02001668 cur_size = sizeof(union perf_event);
1669
1670 buf = malloc(cur_size);
1671 if (!buf)
1672 return -errno;
David Carrillo-Cisneros1e0d4f02017-04-10 13:14:27 -07001673 ordered_events__set_copy_on_queue(oe, true);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001674more:
Stephane Eranian444d2862012-05-15 13:28:12 +02001675 event = buf;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001676 err = readn(fd, event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05001677 if (err <= 0) {
1678 if (err == 0)
1679 goto done;
1680
1681 pr_err("failed to read event header\n");
1682 goto out_err;
1683 }
1684
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001685 if (session->header.needs_swap)
Stephane Eranian444d2862012-05-15 13:28:12 +02001686 perf_event_header__bswap(&event->header);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001687
Stephane Eranian444d2862012-05-15 13:28:12 +02001688 size = event->header.size;
Adrian Hunter27389d72013-07-04 16:20:27 +03001689 if (size < sizeof(struct perf_event_header)) {
1690 pr_err("bad event header size\n");
1691 goto out_err;
1692 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001693
Stephane Eranian444d2862012-05-15 13:28:12 +02001694 if (size > cur_size) {
1695 void *new = realloc(buf, size);
1696 if (!new) {
1697 pr_err("failed to allocate memory to read event\n");
1698 goto out_err;
1699 }
1700 buf = new;
1701 cur_size = size;
1702 event = buf;
1703 }
1704 p = event;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001705 p += sizeof(struct perf_event_header);
1706
Tom Zanussi794e43b2010-05-05 00:27:40 -05001707 if (size - sizeof(struct perf_event_header)) {
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001708 err = readn(fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -05001709 if (err <= 0) {
1710 if (err == 0) {
1711 pr_err("unexpected end of event stream\n");
1712 goto done;
1713 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001714
Tom Zanussi794e43b2010-05-05 00:27:40 -05001715 pr_err("failed to read event data\n");
1716 goto out_err;
1717 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001718 }
1719
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001720 if ((skip = perf_session__process_event(session, event, head)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001721 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
Stephane Eranian444d2862012-05-15 13:28:12 +02001722 head, event->header.size, event->header.type);
Jiri Olsa9389a462012-04-16 20:42:51 +02001723 err = -EINVAL;
1724 goto out_err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001725 }
1726
1727 head += size;
1728
Tom Zanussi8dc58102010-04-01 23:59:15 -05001729 if (skip > 0)
1730 head += skip;
1731
1732 if (!session_done())
1733 goto more;
1734done:
Adrian Hunter8c16b642013-10-18 15:29:02 +03001735 /* do the final flush for ordered samples */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001736 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
Adrian Hunterc4468702015-04-09 18:53:48 +03001737 if (err)
1738 goto out_err;
1739 err = auxtrace__flush_events(session, tool);
Adrian Huntera5499b32015-05-29 16:33:30 +03001740 if (err)
1741 goto out_err;
1742 err = perf_session__flush_thread_stacks(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001743out_err:
Stephane Eranian444d2862012-05-15 13:28:12 +02001744 free(buf);
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001745 perf_session__warn_about_errors(session);
Jiri Olsaadc56ed2014-06-10 22:50:03 +02001746 ordered_events__free(&session->ordered_events);
Adrian Hunterc4468702015-04-09 18:53:48 +03001747 auxtrace__free_events(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001748 return err;
1749}
1750
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001751static union perf_event *
1752fetch_mmaped_event(struct perf_session *session,
1753 u64 head, size_t mmap_size, char *buf)
1754{
1755 union perf_event *event;
1756
1757 /*
1758 * Ensure we have enough space remaining to read
1759 * the size of the event in the headers.
1760 */
1761 if (head + sizeof(event->header) > mmap_size)
1762 return NULL;
1763
1764 event = (union perf_event *)(buf + head);
1765
1766 if (session->header.needs_swap)
1767 perf_event_header__bswap(&event->header);
1768
Adrian Hunter27389d72013-07-04 16:20:27 +03001769 if (head + event->header.size > mmap_size) {
1770 /* We're not fetching the event so swap back again */
1771 if (session->header.needs_swap)
1772 perf_event_header__bswap(&event->header);
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001773 return NULL;
Adrian Hunter27389d72013-07-04 16:20:27 +03001774 }
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001775
1776 return event;
1777}
1778
David Miller35d48dd2012-11-10 14:12:19 -05001779/*
1780 * On 64bit we can mmap the data file in one go. No need for tiny mmap
1781 * slices. On 32bit we use 32MB.
1782 */
1783#if BITS_PER_LONG == 64
1784#define MMAP_SIZE ULLONG_MAX
1785#define NUM_MMAPS 1
1786#else
1787#define MMAP_SIZE (32 * 1024 * 1024ULL)
1788#define NUM_MMAPS 128
1789#endif
1790
Namhyung Kim4ac30cf2015-01-29 17:06:43 +09001791static int __perf_session__process_events(struct perf_session *session,
1792 u64 data_offset, u64 data_size,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001793 u64 file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001794{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001795 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001796 struct perf_tool *tool = session->tool;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001797 int fd = perf_data_file__fd(session->file);
Adrian Hunterd5652d82014-07-23 22:19:58 +03001798 u64 head, page_offset, file_offset, file_pos, size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001799 int err, mmap_prot, mmap_flags, map_idx = 0;
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -03001800 size_t mmap_size;
David Miller35d48dd2012-11-10 14:12:19 -05001801 char *buf, *mmaps[NUM_MMAPS];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001802 union perf_event *event;
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001803 struct ui_progress prog;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001804 s64 skip;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001805
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001806 perf_tool__fill_defaults(tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001807
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001808 page_offset = page_size * (data_offset / page_size);
1809 file_offset = page_offset;
1810 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001811
Mark Rutland381c02f2015-09-16 18:18:49 +01001812 if (data_size == 0)
1813 goto out;
1814
1815 if (data_offset + data_size < file_size)
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001816 file_size = data_offset + data_size;
1817
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001818 ui_progress__init(&prog, file_size, "Processing events...");
Thomas Gleixner55b446292010-11-30 17:49:46 +00001819
David Miller35d48dd2012-11-10 14:12:19 -05001820 mmap_size = MMAP_SIZE;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001821 if (mmap_size > file_size) {
Thomas Gleixner55b446292010-11-30 17:49:46 +00001822 mmap_size = file_size;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001823 session->one_mmap = true;
1824 }
Thomas Gleixner55b446292010-11-30 17:49:46 +00001825
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001826 memset(mmaps, 0, sizeof(mmaps));
1827
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001828 mmap_prot = PROT_READ;
1829 mmap_flags = MAP_SHARED;
1830
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001831 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001832 mmap_prot |= PROT_WRITE;
1833 mmap_flags = MAP_PRIVATE;
1834 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001835remap:
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001836 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd,
Thomas Gleixner55b446292010-11-30 17:49:46 +00001837 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001838 if (buf == MAP_FAILED) {
1839 pr_err("failed to mmap file\n");
1840 err = -errno;
1841 goto out_err;
1842 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001843 mmaps[map_idx] = buf;
1844 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001845 file_pos = file_offset + head;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001846 if (session->one_mmap) {
1847 session->one_mmap_addr = buf;
1848 session->one_mmap_offset = file_offset;
1849 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001850
1851more:
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001852 event = fetch_mmaped_event(session, head, mmap_size, buf);
1853 if (!event) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001854 if (mmaps[map_idx]) {
1855 munmap(mmaps[map_idx], mmap_size);
1856 mmaps[map_idx] = NULL;
1857 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001858
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001859 page_offset = page_size * (head / page_size);
1860 file_offset += page_offset;
1861 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001862 goto remap;
1863 }
1864
1865 size = event->header.size;
1866
Adrian Hunter27389d72013-07-04 16:20:27 +03001867 if (size < sizeof(struct perf_event_header) ||
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001868 (skip = perf_session__process_event(session, event, file_pos)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001869 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1870 file_offset + head, event->header.size,
1871 event->header.type);
1872 err = -EINVAL;
1873 goto out_err;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001874 }
1875
Adrian Hunter6f917c72014-07-23 22:19:57 +03001876 if (skip)
1877 size += skip;
1878
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001879 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001880 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001881
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001882 ui_progress__update(&prog, size);
Thomas Gleixner55b446292010-11-30 17:49:46 +00001883
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001884 if (session_done())
Adrian Hunter8c16b642013-10-18 15:29:02 +03001885 goto out;
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001886
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001887 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001888 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001889
Adrian Hunter8c16b642013-10-18 15:29:02 +03001890out:
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001891 /* do the final flush for ordered samples */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001892 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
Adrian Hunterc4468702015-04-09 18:53:48 +03001893 if (err)
1894 goto out_err;
1895 err = auxtrace__flush_events(session, tool);
Adrian Huntera5499b32015-05-29 16:33:30 +03001896 if (err)
1897 goto out_err;
1898 err = perf_session__flush_thread_stacks(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001899out_err:
Namhyung Kima5580f32012-11-13 22:30:34 +09001900 ui_progress__finish();
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001901 perf_session__warn_about_errors(session);
Wang Nanb26dc732016-04-13 08:21:04 +00001902 /*
1903 * We may switching perf.data output, make ordered_events
1904 * reusable.
1905 */
1906 ordered_events__reinit(&session->ordered_events);
Adrian Hunterc4468702015-04-09 18:53:48 +03001907 auxtrace__free_events(session);
Adrian Hunter919d86d2014-07-14 13:02:51 +03001908 session->one_mmap = false;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001909 return err;
1910}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001911
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001912int perf_session__process_events(struct perf_session *session)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001913{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001914 u64 size = perf_data_file__size(session->file);
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001915 int err;
1916
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001917 if (perf_session__register_idle_thread(session) < 0)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001918 return -ENOMEM;
1919
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001920 if (!perf_data_file__is_pipe(session->file))
1921 err = __perf_session__process_events(session,
1922 session->header.data_offset,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001923 session->header.data_size, size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001924 else
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001925 err = __perf_session__process_pipe_events(session);
Dave Martin88ca8952010-07-27 11:46:12 -03001926
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001927 return err;
1928}
1929
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001930bool perf_session__has_traces(struct perf_session *session, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001931{
David Ahern93ea01c292013-08-07 22:50:58 -04001932 struct perf_evsel *evsel;
1933
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001934 evlist__for_each_entry(session->evlist, evsel) {
David Ahern93ea01c292013-08-07 22:50:58 -04001935 if (evsel->attr.type == PERF_TYPE_TRACEPOINT)
1936 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001937 }
1938
David Ahern93ea01c292013-08-07 22:50:58 -04001939 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1940 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001941}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001942
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001943int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1944 const char *symbol_name, u64 addr)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001945{
1946 char *bracket;
Arnaldo Carvalho de Meloa0b2f5a2017-02-14 16:19:56 -03001947 int i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001948 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001949
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001950 ref = zalloc(sizeof(struct ref_reloc_sym));
1951 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001952 return -ENOMEM;
1953
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001954 ref->name = strdup(symbol_name);
1955 if (ref->name == NULL) {
1956 free(ref);
1957 return -ENOMEM;
1958 }
1959
1960 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001961 if (bracket)
1962 *bracket = '\0';
1963
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001964 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001965
1966 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001967 struct kmap *kmap = map__kmap(maps[i]);
Wang Nanba927322015-04-07 08:22:45 +00001968
1969 if (!kmap)
1970 continue;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001971 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001972 }
1973
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001974 return 0;
1975}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001976
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001977size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001978{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001979 return machines__fprintf_dsos(&session->machines, fp);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001980}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001981
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001982size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001983 bool (skip)(struct dso *dso, int parm), int parm)
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001984{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001985 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001986}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001987
1988size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1989{
Adrian Hunterc4468702015-04-09 18:53:48 +03001990 size_t ret;
1991 const char *msg = "";
1992
1993 if (perf_header__has_feat(&session->header, HEADER_AUXTRACE))
1994 msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)";
1995
Adrian Hunterfe692ac2015-06-23 10:52:49 +03001996 ret = fprintf(fp, "\nAggregated stats:%s\n", msg);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001997
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001998 ret += events_stats__fprintf(&session->evlist->stats, fp);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001999 return ret;
2000}
David Ahernc0230b22011-03-09 22:23:27 -07002001
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02002002size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
2003{
2004 /*
2005 * FIXME: Here we have to actually print all the machines in this
2006 * session, not just the host...
2007 */
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -03002008 return machine__fprintf(&session->machines.host, fp);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02002009}
2010
David Ahern9cbdb702011-04-06 21:54:20 -06002011struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
2012 unsigned int type)
2013{
2014 struct perf_evsel *pos;
2015
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002016 evlist__for_each_entry(session->evlist, pos) {
David Ahern9cbdb702011-04-06 21:54:20 -06002017 if (pos->attr.type == type)
2018 return pos;
2019 }
2020 return NULL;
2021}
2022
Anton Blanchard5d67be92011-07-04 21:57:50 +10002023int perf_session__cpu_bitmap(struct perf_session *session,
2024 const char *cpu_list, unsigned long *cpu_bitmap)
2025{
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04002026 int i, err = -1;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002027 struct cpu_map *map;
2028
2029 for (i = 0; i < PERF_TYPE_MAX; ++i) {
2030 struct perf_evsel *evsel;
2031
2032 evsel = perf_session__find_first_evtype(session, i);
2033 if (!evsel)
2034 continue;
2035
2036 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
2037 pr_err("File does not contain CPU events. "
2038 "Remove -c option to proceed.\n");
2039 return -1;
2040 }
2041 }
2042
2043 map = cpu_map__new(cpu_list);
David Ahern47fbe532011-11-13 10:45:27 -07002044 if (map == NULL) {
2045 pr_err("Invalid cpu_list\n");
2046 return -1;
2047 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10002048
2049 for (i = 0; i < map->nr; i++) {
2050 int cpu = map->map[i];
2051
2052 if (cpu >= MAX_NR_CPUS) {
2053 pr_err("Requested CPU %d too large. "
2054 "Consider raising MAX_NR_CPUS\n", cpu);
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04002055 goto out_delete_map;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002056 }
2057
2058 set_bit(cpu, cpu_bitmap);
2059 }
2060
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04002061 err = 0;
2062
2063out_delete_map:
Jiri Olsaf30a79b2015-06-23 00:36:04 +02002064 cpu_map__put(map);
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04002065 return err;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002066}
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002067
2068void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
2069 bool full)
2070{
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002071 if (session == NULL || fp == NULL)
2072 return;
2073
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002074 fprintf(fp, "# ========\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002075 perf_header__fprintf_info(session, fp, full);
2076 fprintf(fp, "# ========\n#\n");
2077}
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002078
2079
2080int __perf_session__set_tracepoints_handlers(struct perf_session *session,
2081 const struct perf_evsel_str_handler *assocs,
2082 size_t nr_assocs)
2083{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002084 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002085 size_t i;
2086 int err;
2087
2088 for (i = 0; i < nr_assocs; i++) {
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03002089 /*
2090 * Adding a handler for an event not in the session,
2091 * just ignore it.
2092 */
2093 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name);
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002094 if (evsel == NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03002095 continue;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002096
2097 err = -EEXIST;
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002098 if (evsel->handler != NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03002099 goto out;
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002100 evsel->handler = assocs[i].handler;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002101 }
2102
2103 err = 0;
2104out:
2105 return err;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002106}
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002107
2108int perf_event__process_id_index(struct perf_tool *tool __maybe_unused,
2109 union perf_event *event,
2110 struct perf_session *session)
2111{
2112 struct perf_evlist *evlist = session->evlist;
2113 struct id_index_event *ie = &event->id_index;
2114 size_t i, nr, max_nr;
2115
2116 max_nr = (ie->header.size - sizeof(struct id_index_event)) /
2117 sizeof(struct id_index_entry);
2118 nr = ie->nr;
2119 if (nr > max_nr)
2120 return -EINVAL;
2121
2122 if (dump_trace)
2123 fprintf(stdout, " nr: %zu\n", nr);
2124
2125 for (i = 0; i < nr; i++) {
2126 struct id_index_entry *e = &ie->entries[i];
2127 struct perf_sample_id *sid;
2128
2129 if (dump_trace) {
2130 fprintf(stdout, " ... id: %"PRIu64, e->id);
2131 fprintf(stdout, " idx: %"PRIu64, e->idx);
2132 fprintf(stdout, " cpu: %"PRId64, e->cpu);
2133 fprintf(stdout, " tid: %"PRId64"\n", e->tid);
2134 }
2135
2136 sid = perf_evlist__id2sid(evlist, e->id);
2137 if (!sid)
2138 return -ENOENT;
2139 sid->idx = e->idx;
2140 sid->cpu = e->cpu;
2141 sid->tid = e->tid;
2142 }
2143 return 0;
2144}
2145
2146int perf_event__synthesize_id_index(struct perf_tool *tool,
2147 perf_event__handler_t process,
2148 struct perf_evlist *evlist,
2149 struct machine *machine)
2150{
2151 union perf_event *ev;
2152 struct perf_evsel *evsel;
2153 size_t nr = 0, i = 0, sz, max_nr, n;
2154 int err;
2155
2156 pr_debug2("Synthesizing id index\n");
2157
2158 max_nr = (UINT16_MAX - sizeof(struct id_index_event)) /
2159 sizeof(struct id_index_entry);
2160
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002161 evlist__for_each_entry(evlist, evsel)
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002162 nr += evsel->ids;
2163
2164 n = nr > max_nr ? max_nr : nr;
2165 sz = sizeof(struct id_index_event) + n * sizeof(struct id_index_entry);
2166 ev = zalloc(sz);
2167 if (!ev)
2168 return -ENOMEM;
2169
2170 ev->id_index.header.type = PERF_RECORD_ID_INDEX;
2171 ev->id_index.header.size = sz;
2172 ev->id_index.nr = n;
2173
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002174 evlist__for_each_entry(evlist, evsel) {
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002175 u32 j;
2176
2177 for (j = 0; j < evsel->ids; j++) {
2178 struct id_index_entry *e;
2179 struct perf_sample_id *sid;
2180
2181 if (i >= n) {
2182 err = process(tool, ev, NULL, machine);
2183 if (err)
2184 goto out_err;
2185 nr -= n;
2186 i = 0;
2187 }
2188
2189 e = &ev->id_index.entries[i++];
2190
2191 e->id = evsel->id[j];
2192
2193 sid = perf_evlist__id2sid(evlist, e->id);
2194 if (!sid) {
2195 free(ev);
2196 return -ENOENT;
2197 }
2198
2199 e->idx = sid->idx;
2200 e->cpu = sid->cpu;
2201 e->tid = sid->tid;
2202 }
2203 }
2204
2205 sz = sizeof(struct id_index_event) + nr * sizeof(struct id_index_entry);
2206 ev->id_index.header.size = sz;
2207 ev->id_index.nr = nr;
2208
2209 err = process(tool, ev, NULL, machine);
2210out_err:
2211 free(ev);
2212
2213 return err;
2214}