blob: 78a067777144af9e67425c6d36167d3326f40eed [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03002#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03003#include <inttypes.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02004#include <linux/kernel.h>
Robert Richter4e319022013-06-11 17:29:18 +02005#include <traceevent/event-parse.h>
Alexander Shishkin05a1f472017-03-16 18:41:59 +02006#include <api/fs/fs.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02007
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02008#include <byteswap.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02009#include <unistd.h>
10#include <sys/types.h>
Arnaldo Carvalho de Meloa41794c2010-05-18 18:29:23 -030011#include <sys/mman.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020012
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -030013#include "evlist.h"
14#include "evsel.h"
Arnaldo Carvalho de Melo98521b32017-04-25 15:45:35 -030015#include "memswap.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020016#include "session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020017#include "tool.h"
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -020018#include "sort.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020019#include "util.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100020#include "cpumap.h"
Jiri Olsa0f6a3012012-08-07 15:20:45 +020021#include "perf_regs.h"
Jiri Olsab0a45202014-06-12 09:50:11 +020022#include "asm/bug.h"
Adrian Hunterc4468702015-04-09 18:53:48 +030023#include "auxtrace.h"
Arnaldo Carvalho de Meloe7ff8922017-04-19 21:34:35 -030024#include "thread.h"
Adrian Huntera5499b32015-05-29 16:33:30 +030025#include "thread-stack.h"
Jiri Olsa2d2aea62015-10-25 15:51:42 +010026#include "stat.h"
Adrian Hunterec1891a2018-11-06 23:07:10 +020027#include "arch/common.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020028
Adrian Hunterc4468702015-04-09 18:53:48 +030029static int perf_session__deliver_event(struct perf_session *session,
30 union perf_event *event,
Adrian Hunterc4468702015-04-09 18:53:48 +030031 struct perf_tool *tool,
32 u64 file_offset);
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -030033
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030034static int perf_session__open(struct perf_session *session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020035{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010036 struct perf_data *data = session->data;
Tom Zanussi8dc58102010-04-01 23:59:15 -050037
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030038 if (perf_session__read_header(session) < 0) {
Arnaldo Carvalho de Meloe87b4912015-11-09 17:12:03 -030039 pr_err("incompatible file format (rerun with -v to learn more)\n");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020040 return -1;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020041 }
42
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010043 if (perf_data__is_pipe(data))
Jiri Olsacc9784bd2013-10-15 16:27:34 +020044 return 0;
45
Jiri Olsa3ba78bd2015-11-05 15:40:47 +010046 if (perf_header__has_feat(&session->header, HEADER_STAT))
47 return 0;
48
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030049 if (!perf_evlist__valid_sample_type(session->evlist)) {
Arnaldo Carvalho de Meloe87b4912015-11-09 17:12:03 -030050 pr_err("non matching sample_type\n");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020051 return -1;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030052 }
53
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030054 if (!perf_evlist__valid_sample_id_all(session->evlist)) {
Arnaldo Carvalho de Meloe87b4912015-11-09 17:12:03 -030055 pr_err("non matching sample_id_all\n");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020056 return -1;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030057 }
58
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030059 if (!perf_evlist__valid_read_format(session->evlist)) {
Arnaldo Carvalho de Meloe87b4912015-11-09 17:12:03 -030060 pr_err("non matching read_format\n");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020061 return -1;
Jiri Olsa9ede4732012-10-10 17:38:13 +020062 }
63
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020064 return 0;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020065}
66
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030067void perf_session__set_id_hdr_size(struct perf_session *session)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020068{
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030069 u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist);
70
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030071 machines__set_id_hdr_size(&session->machines, id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020072}
73
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030074int perf_session__create_kernel_maps(struct perf_session *session)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080075{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030076 int ret = machine__create_kernel_maps(&session->machines.host);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080077
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080078 if (ret >= 0)
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030079 ret = machines__create_guest_kernel_maps(&session->machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080080 return ret;
81}
82
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030083static void perf_session__destroy_kernel_maps(struct perf_session *session)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -030084{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030085 machines__destroy_kernel_maps(&session->machines);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -030086}
87
Adrian Huntercfe1c412014-07-31 09:00:45 +030088static bool perf_session__has_comm_exec(struct perf_session *session)
89{
90 struct perf_evsel *evsel;
91
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -030092 evlist__for_each_entry(session->evlist, evsel) {
Adrian Huntercfe1c412014-07-31 09:00:45 +030093 if (evsel->attr.comm_exec)
94 return true;
95 }
96
97 return false;
98}
99
100static void perf_session__set_comm_exec(struct perf_session *session)
101{
102 bool comm_exec = perf_session__has_comm_exec(session);
103
104 machines__set_comm_exec(&session->machines, comm_exec);
105}
106
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300107static int ordered_events__deliver_event(struct ordered_events *oe,
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300108 struct ordered_event *event)
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300109{
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300110 struct perf_session *session = container_of(oe, struct perf_session,
111 ordered_events);
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300112
Jiri Olsa93d10af2017-08-03 13:21:14 +0200113 return perf_session__deliver_event(session, event->event,
Adrian Hunterc4468702015-04-09 18:53:48 +0300114 session->tool, event->file_offset);
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300115}
116
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100117struct perf_session *perf_session__new(struct perf_data *data,
Jiri Olsaf5fc14122013-10-15 16:27:32 +0200118 bool repipe, struct perf_tool *tool)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200119{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300120 struct perf_session *session = zalloc(sizeof(*session));
Robert Richterefad1412011-12-07 10:02:54 +0100121
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300122 if (!session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200123 goto out;
124
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300125 session->repipe = repipe;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300126 session->tool = tool;
Adrian Hunter99fa2982015-04-30 17:37:25 +0300127 INIT_LIST_HEAD(&session->auxtrace_index);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300128 machines__init(&session->machines);
Jiri Olsaa4a66682018-11-07 16:58:36 +0100129 ordered_events__init(&session->ordered_events,
130 ordered_events__deliver_event, NULL);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200131
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100132 if (data) {
133 if (perf_data__open(data))
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200134 goto out_delete;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200135
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100136 session->data = data;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200137
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100138 if (perf_data__is_read(data)) {
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300139 if (perf_session__open(session) < 0)
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200140 goto out_close;
141
David Carrillo-Cisneros0973ad92017-04-10 13:14:30 -0700142 /*
143 * set session attributes that are present in perf.data
144 * but not in pipe-mode.
145 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100146 if (!data->is_pipe) {
David Carrillo-Cisneros0973ad92017-04-10 13:14:30 -0700147 perf_session__set_id_hdr_size(session);
148 perf_session__set_comm_exec(session);
149 }
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200150 }
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -0300151 } else {
152 session->machines.host.env = &perf_env;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200153 }
154
Adrian Hunterec1891a2018-11-06 23:07:10 +0200155 session->machines.host.single_address_space =
156 perf_env__single_address_space(session->machines.host.env);
157
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100158 if (!data || perf_data__is_write(data)) {
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200159 /*
160 * In O_RDONLY mode this will be performed when reading the
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200161 * kernel MMAP event, in perf_event__process_mmap().
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200162 */
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300163 if (perf_session__create_kernel_maps(session) < 0)
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700164 pr_warning("Cannot read kernel map\n");
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200165 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200166
David Carrillo-Cisneros0973ad92017-04-10 13:14:30 -0700167 /*
168 * In pipe-mode, evlist is empty until PERF_RECORD_HEADER_ATTR is
169 * processed, so perf_evlist__sample_id_all is not meaningful here.
170 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100171 if ((!data || !data->is_pipe) && tool && tool->ordering_requires_timestamps &&
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200172 tool->ordered_events && !perf_evlist__sample_id_all(session->evlist)) {
Ian Munsie21ef97f2010-12-10 14:09:16 +1100173 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200174 tool->ordered_events = false;
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300175 }
Ian Munsie21ef97f2010-12-10 14:09:16 +1100176
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300177 return session;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200178
179 out_close:
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100180 perf_data__close(data);
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200181 out_delete:
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300182 perf_session__delete(session);
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200183 out:
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200184 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200185}
186
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200187static void perf_session__delete_threads(struct perf_session *session)
188{
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300189 machine__delete_threads(&session->machines.host);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200190}
191
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300192void perf_session__delete(struct perf_session *session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200193{
Arnaldo Carvalho de Meloe1446552016-06-22 10:02:16 -0300194 if (session == NULL)
195 return;
Adrian Hunterc4468702015-04-09 18:53:48 +0300196 auxtrace__free(session);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300197 auxtrace_index__free(&session->auxtrace_index);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300198 perf_session__destroy_kernel_maps(session);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300199 perf_session__delete_threads(session);
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -0300200 perf_env__exit(&session->header.env);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300201 machines__exit(&session->machines);
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100202 if (session->data)
203 perf_data__close(session->data);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300204 free(session);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200205}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200206
Jiri Olsa89f16882018-09-13 14:54:03 +0200207static int process_event_synth_tracing_data_stub(struct perf_session *session
Adrian Hunter47c3d102013-07-04 16:20:21 +0300208 __maybe_unused,
209 union perf_event *event
Jiri Olsa89f16882018-09-13 14:54:03 +0200210 __maybe_unused)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200211{
212 dump_printf(": unhandled!\n");
213 return 0;
214}
215
Adrian Hunter47c3d102013-07-04 16:20:21 +0300216static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused,
217 union perf_event *event __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300218 struct perf_evlist **pevlist
219 __maybe_unused)
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200220{
221 dump_printf(": unhandled!\n");
222 return 0;
223}
224
Jiri Olsaffe777252015-10-25 15:51:36 +0100225static int process_event_synth_event_update_stub(struct perf_tool *tool __maybe_unused,
226 union perf_event *event __maybe_unused,
227 struct perf_evlist **pevlist
228 __maybe_unused)
229{
Jiri Olsa2d2aea62015-10-25 15:51:42 +0100230 if (dump_trace)
231 perf_event__fprintf_event_update(event, stdout);
232
Jiri Olsaffe777252015-10-25 15:51:36 +0100233 dump_printf(": unhandled!\n");
234 return 0;
235}
236
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300237static int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
238 union perf_event *event __maybe_unused,
239 struct perf_sample *sample __maybe_unused,
240 struct perf_evsel *evsel __maybe_unused,
241 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300242{
243 dump_printf(": unhandled!\n");
244 return 0;
245}
246
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300247static int process_event_stub(struct perf_tool *tool __maybe_unused,
248 union perf_event *event __maybe_unused,
249 struct perf_sample *sample __maybe_unused,
250 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200251{
252 dump_printf(": unhandled!\n");
253 return 0;
254}
255
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300256static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
257 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300258 struct ordered_events *oe __maybe_unused)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200259{
260 dump_printf(": unhandled!\n");
261 return 0;
262}
263
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200264static int process_finished_round(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200265 union perf_event *event,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300266 struct ordered_events *oe);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200267
Adrian Huntera16ac022015-04-09 18:53:43 +0300268static int skipn(int fd, off_t n)
269{
270 char buf[4096];
271 ssize_t ret;
272
273 while (n > 0) {
274 ret = read(fd, buf, min(n, (off_t)sizeof(buf)));
275 if (ret <= 0)
276 return ret;
277 n -= ret;
278 }
279
280 return 0;
281}
282
Jiri Olsa73365552018-09-13 14:54:04 +0200283static s64 process_event_auxtrace_stub(struct perf_session *session __maybe_unused,
284 union perf_event *event)
Adrian Huntera16ac022015-04-09 18:53:43 +0300285{
286 dump_printf(": unhandled!\n");
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100287 if (perf_data__is_pipe(session->data))
288 skipn(perf_data__fd(session->data), event->auxtrace.size);
Adrian Huntera16ac022015-04-09 18:53:43 +0300289 return event->auxtrace.size;
290}
291
Jiri Olsa89f16882018-09-13 14:54:03 +0200292static int process_event_op2_stub(struct perf_session *session __maybe_unused,
293 union perf_event *event __maybe_unused)
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300294{
295 dump_printf(": unhandled!\n");
296 return 0;
297}
298
Jiri Olsa5f3339d2015-10-25 15:51:19 +0100299
300static
Jiri Olsa89f16882018-09-13 14:54:03 +0200301int process_event_thread_map_stub(struct perf_session *session __maybe_unused,
302 union perf_event *event __maybe_unused)
Jiri Olsa5f3339d2015-10-25 15:51:19 +0100303{
Jiri Olsa2d2aea62015-10-25 15:51:42 +0100304 if (dump_trace)
305 perf_event__fprintf_thread_map(event, stdout);
306
Jiri Olsa5f3339d2015-10-25 15:51:19 +0100307 dump_printf(": unhandled!\n");
308 return 0;
309}
310
Jiri Olsa6640b6c2015-10-25 15:51:23 +0100311static
Jiri Olsa89f16882018-09-13 14:54:03 +0200312int process_event_cpu_map_stub(struct perf_session *session __maybe_unused,
313 union perf_event *event __maybe_unused)
Jiri Olsa6640b6c2015-10-25 15:51:23 +0100314{
Jiri Olsa2d2aea62015-10-25 15:51:42 +0100315 if (dump_trace)
316 perf_event__fprintf_cpu_map(event, stdout);
317
Jiri Olsa6640b6c2015-10-25 15:51:23 +0100318 dump_printf(": unhandled!\n");
319 return 0;
320}
321
Jiri Olsa374fb9e2015-10-25 15:51:27 +0100322static
Jiri Olsa89f16882018-09-13 14:54:03 +0200323int process_event_stat_config_stub(struct perf_session *session __maybe_unused,
324 union perf_event *event __maybe_unused)
Jiri Olsa374fb9e2015-10-25 15:51:27 +0100325{
Jiri Olsa2d2aea62015-10-25 15:51:42 +0100326 if (dump_trace)
327 perf_event__fprintf_stat_config(event, stdout);
328
Jiri Olsa374fb9e2015-10-25 15:51:27 +0100329 dump_printf(": unhandled!\n");
330 return 0;
331}
332
Jiri Olsa89f16882018-09-13 14:54:03 +0200333static int process_stat_stub(struct perf_session *perf_session __maybe_unused,
334 union perf_event *event)
Jiri Olsad80518c2015-10-25 15:51:30 +0100335{
Jiri Olsa2d2aea62015-10-25 15:51:42 +0100336 if (dump_trace)
337 perf_event__fprintf_stat(event, stdout);
338
Jiri Olsad80518c2015-10-25 15:51:30 +0100339 dump_printf(": unhandled!\n");
340 return 0;
341}
342
Jiri Olsa89f16882018-09-13 14:54:03 +0200343static int process_stat_round_stub(struct perf_session *perf_session __maybe_unused,
344 union perf_event *event)
Jiri Olsa2d8f0f12015-10-25 15:51:33 +0100345{
Jiri Olsa2d2aea62015-10-25 15:51:42 +0100346 if (dump_trace)
347 perf_event__fprintf_stat_round(event, stdout);
348
Jiri Olsa2d8f0f12015-10-25 15:51:33 +0100349 dump_printf(": unhandled!\n");
350 return 0;
351}
352
David Ahern9c501402013-08-02 14:05:41 -0600353void perf_tool__fill_defaults(struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200354{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200355 if (tool->sample == NULL)
356 tool->sample = process_event_sample_stub;
357 if (tool->mmap == NULL)
358 tool->mmap = process_event_stub;
David Ahern6adb0b02013-09-22 19:44:59 -0600359 if (tool->mmap2 == NULL)
360 tool->mmap2 = process_event_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200361 if (tool->comm == NULL)
362 tool->comm = process_event_stub;
Namhyung Kim7f0cd232017-10-17 22:29:00 +0900363 if (tool->namespaces == NULL)
364 tool->namespaces = process_event_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200365 if (tool->fork == NULL)
366 tool->fork = process_event_stub;
367 if (tool->exit == NULL)
368 tool->exit = process_event_stub;
369 if (tool->lost == NULL)
370 tool->lost = perf_event__process_lost;
Kan Liangc4937a92015-05-10 15:13:15 -0400371 if (tool->lost_samples == NULL)
372 tool->lost_samples = perf_event__process_lost_samples;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300373 if (tool->aux == NULL)
374 tool->aux = perf_event__process_aux;
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300375 if (tool->itrace_start == NULL)
376 tool->itrace_start = perf_event__process_itrace_start;
Adrian Hunter02860392015-07-21 12:44:03 +0300377 if (tool->context_switch == NULL)
378 tool->context_switch = perf_event__process_switch;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200379 if (tool->read == NULL)
380 tool->read = process_event_sample_stub;
381 if (tool->throttle == NULL)
382 tool->throttle = process_event_stub;
383 if (tool->unthrottle == NULL)
384 tool->unthrottle = process_event_stub;
385 if (tool->attr == NULL)
386 tool->attr = process_event_synth_attr_stub;
Jiri Olsaffe777252015-10-25 15:51:36 +0100387 if (tool->event_update == NULL)
388 tool->event_update = process_event_synth_event_update_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200389 if (tool->tracing_data == NULL)
390 tool->tracing_data = process_event_synth_tracing_data_stub;
391 if (tool->build_id == NULL)
Adrian Hunter5fb0ac12016-03-07 16:44:39 -0300392 tool->build_id = process_event_op2_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200393 if (tool->finished_round == NULL) {
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200394 if (tool->ordered_events)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200395 tool->finished_round = process_finished_round;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200396 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200397 tool->finished_round = process_finished_round_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200398 }
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200399 if (tool->id_index == NULL)
Adrian Hunter5fb0ac12016-03-07 16:44:39 -0300400 tool->id_index = process_event_op2_stub;
Adrian Huntera16ac022015-04-09 18:53:43 +0300401 if (tool->auxtrace_info == NULL)
Adrian Hunter5fb0ac12016-03-07 16:44:39 -0300402 tool->auxtrace_info = process_event_op2_stub;
Adrian Huntera16ac022015-04-09 18:53:43 +0300403 if (tool->auxtrace == NULL)
404 tool->auxtrace = process_event_auxtrace_stub;
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300405 if (tool->auxtrace_error == NULL)
Adrian Hunter5fb0ac12016-03-07 16:44:39 -0300406 tool->auxtrace_error = process_event_op2_stub;
Jiri Olsa5f3339d2015-10-25 15:51:19 +0100407 if (tool->thread_map == NULL)
408 tool->thread_map = process_event_thread_map_stub;
Jiri Olsa6640b6c2015-10-25 15:51:23 +0100409 if (tool->cpu_map == NULL)
410 tool->cpu_map = process_event_cpu_map_stub;
Jiri Olsa374fb9e2015-10-25 15:51:27 +0100411 if (tool->stat_config == NULL)
412 tool->stat_config = process_event_stat_config_stub;
Jiri Olsad80518c2015-10-25 15:51:30 +0100413 if (tool->stat == NULL)
414 tool->stat = process_stat_stub;
Jiri Olsa2d8f0f12015-10-25 15:51:33 +0100415 if (tool->stat_round == NULL)
416 tool->stat_round = process_stat_round_stub;
Adrian Hunter46bc29b2016-03-08 10:38:44 +0200417 if (tool->time_conv == NULL)
418 tool->time_conv = process_event_op2_stub;
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -0700419 if (tool->feature == NULL)
420 tool->feature = process_event_op2_stub;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200421}
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300422
Jiri Olsa268fb202012-05-30 14:23:43 +0200423static void swap_sample_id_all(union perf_event *event, void *data)
424{
425 void *end = (void *) event + event->header.size;
426 int size = end - data;
427
428 BUG_ON(size % sizeof(u64));
429 mem_bswap_64(data, size);
430}
431
432static void perf_event__all64_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300433 bool sample_id_all __maybe_unused)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200434{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200435 struct perf_event_header *hdr = &event->header;
436 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200437}
438
Jiri Olsa268fb202012-05-30 14:23:43 +0200439static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200440{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200441 event->comm.pid = bswap_32(event->comm.pid);
442 event->comm.tid = bswap_32(event->comm.tid);
Jiri Olsa268fb202012-05-30 14:23:43 +0200443
444 if (sample_id_all) {
445 void *data = &event->comm.comm;
446
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300447 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200448 swap_sample_id_all(event, data);
449 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200450}
451
Jiri Olsa268fb202012-05-30 14:23:43 +0200452static void perf_event__mmap_swap(union perf_event *event,
453 bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200454{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200455 event->mmap.pid = bswap_32(event->mmap.pid);
456 event->mmap.tid = bswap_32(event->mmap.tid);
457 event->mmap.start = bswap_64(event->mmap.start);
458 event->mmap.len = bswap_64(event->mmap.len);
459 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
Jiri Olsa268fb202012-05-30 14:23:43 +0200460
461 if (sample_id_all) {
462 void *data = &event->mmap.filename;
463
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300464 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200465 swap_sample_id_all(event, data);
466 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200467}
468
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200469static void perf_event__mmap2_swap(union perf_event *event,
470 bool sample_id_all)
471{
472 event->mmap2.pid = bswap_32(event->mmap2.pid);
473 event->mmap2.tid = bswap_32(event->mmap2.tid);
474 event->mmap2.start = bswap_64(event->mmap2.start);
475 event->mmap2.len = bswap_64(event->mmap2.len);
476 event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
477 event->mmap2.maj = bswap_32(event->mmap2.maj);
478 event->mmap2.min = bswap_32(event->mmap2.min);
479 event->mmap2.ino = bswap_64(event->mmap2.ino);
480
481 if (sample_id_all) {
482 void *data = &event->mmap2.filename;
483
484 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
485 swap_sample_id_all(event, data);
486 }
487}
Jiri Olsa268fb202012-05-30 14:23:43 +0200488static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200489{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200490 event->fork.pid = bswap_32(event->fork.pid);
491 event->fork.tid = bswap_32(event->fork.tid);
492 event->fork.ppid = bswap_32(event->fork.ppid);
493 event->fork.ptid = bswap_32(event->fork.ptid);
494 event->fork.time = bswap_64(event->fork.time);
Jiri Olsa268fb202012-05-30 14:23:43 +0200495
496 if (sample_id_all)
497 swap_sample_id_all(event, &event->fork + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200498}
499
Jiri Olsa268fb202012-05-30 14:23:43 +0200500static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200501{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200502 event->read.pid = bswap_32(event->read.pid);
503 event->read.tid = bswap_32(event->read.tid);
504 event->read.value = bswap_64(event->read.value);
505 event->read.time_enabled = bswap_64(event->read.time_enabled);
506 event->read.time_running = bswap_64(event->read.time_running);
507 event->read.id = bswap_64(event->read.id);
Jiri Olsa268fb202012-05-30 14:23:43 +0200508
509 if (sample_id_all)
510 swap_sample_id_all(event, &event->read + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200511}
512
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300513static void perf_event__aux_swap(union perf_event *event, bool sample_id_all)
514{
515 event->aux.aux_offset = bswap_64(event->aux.aux_offset);
516 event->aux.aux_size = bswap_64(event->aux.aux_size);
517 event->aux.flags = bswap_64(event->aux.flags);
518
519 if (sample_id_all)
520 swap_sample_id_all(event, &event->aux + 1);
521}
522
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300523static void perf_event__itrace_start_swap(union perf_event *event,
524 bool sample_id_all)
525{
526 event->itrace_start.pid = bswap_32(event->itrace_start.pid);
527 event->itrace_start.tid = bswap_32(event->itrace_start.tid);
528
529 if (sample_id_all)
530 swap_sample_id_all(event, &event->itrace_start + 1);
531}
532
Adrian Hunter02860392015-07-21 12:44:03 +0300533static void perf_event__switch_swap(union perf_event *event, bool sample_id_all)
534{
535 if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
536 event->context_switch.next_prev_pid =
537 bswap_32(event->context_switch.next_prev_pid);
538 event->context_switch.next_prev_tid =
539 bswap_32(event->context_switch.next_prev_tid);
540 }
541
542 if (sample_id_all)
543 swap_sample_id_all(event, &event->context_switch + 1);
544}
545
Jiri Olsadd96c462013-09-01 12:36:15 +0200546static void perf_event__throttle_swap(union perf_event *event,
547 bool sample_id_all)
548{
549 event->throttle.time = bswap_64(event->throttle.time);
550 event->throttle.id = bswap_64(event->throttle.id);
551 event->throttle.stream_id = bswap_64(event->throttle.stream_id);
552
553 if (sample_id_all)
554 swap_sample_id_all(event, &event->throttle + 1);
555}
556
Jiri Olsae108c662012-05-16 08:59:03 +0200557static u8 revbyte(u8 b)
558{
559 int rev = (b >> 4) | ((b & 0xf) << 4);
560 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
561 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
562 return (u8) rev;
563}
564
565/*
566 * XXX this is hack in attempt to carry flags bitfield
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -0800567 * through endian village. ABI says:
Jiri Olsae108c662012-05-16 08:59:03 +0200568 *
569 * Bit-fields are allocated from right to left (least to most significant)
570 * on little-endian implementations and from left to right (most to least
571 * significant) on big-endian implementations.
572 *
573 * The above seems to be byte specific, so we need to reverse each
574 * byte of the bitfield. 'Internet' also says this might be implementation
575 * specific and we probably need proper fix and carry perf_event_attr
576 * bitfield flags in separate data file FEAT_ section. Thought this seems
577 * to work for now.
578 */
579static void swap_bitfield(u8 *p, unsigned len)
580{
581 unsigned i;
582
583 for (i = 0; i < len; i++) {
584 *p = revbyte(*p);
585 p++;
586 }
587}
588
David Aherneda39132011-07-15 12:34:09 -0600589/* exported for swapping attributes in file header */
590void perf_event__attr_swap(struct perf_event_attr *attr)
591{
592 attr->type = bswap_32(attr->type);
593 attr->size = bswap_32(attr->size);
Jiri Olsae108c662012-05-16 08:59:03 +0200594
Wang Nanb30b6172015-06-17 09:56:39 +0000595#define bswap_safe(f, n) \
596 (attr->size > (offsetof(struct perf_event_attr, f) + \
597 sizeof(attr->f) * (n)))
598#define bswap_field(f, sz) \
599do { \
600 if (bswap_safe(f, 0)) \
601 attr->f = bswap_##sz(attr->f); \
602} while(0)
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300603#define bswap_field_16(f) bswap_field(f, 16)
Wang Nanb30b6172015-06-17 09:56:39 +0000604#define bswap_field_32(f) bswap_field(f, 32)
605#define bswap_field_64(f) bswap_field(f, 64)
606
607 bswap_field_64(config);
608 bswap_field_64(sample_period);
609 bswap_field_64(sample_type);
610 bswap_field_64(read_format);
611 bswap_field_32(wakeup_events);
612 bswap_field_32(bp_type);
613 bswap_field_64(bp_addr);
614 bswap_field_64(bp_len);
615 bswap_field_64(branch_sample_type);
616 bswap_field_64(sample_regs_user);
617 bswap_field_32(sample_stack_user);
618 bswap_field_32(aux_watermark);
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300619 bswap_field_16(sample_max_stack);
Wang Nanb30b6172015-06-17 09:56:39 +0000620
621 /*
622 * After read_format are bitfields. Check read_format because
623 * we are unable to use offsetof on bitfield.
624 */
625 if (bswap_safe(read_format, 1))
626 swap_bitfield((u8 *) (&attr->read_format + 1),
627 sizeof(u64));
628#undef bswap_field_64
629#undef bswap_field_32
630#undef bswap_field
631#undef bswap_safe
David Aherneda39132011-07-15 12:34:09 -0600632}
633
Jiri Olsa268fb202012-05-30 14:23:43 +0200634static void perf_event__hdr_attr_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300635 bool sample_id_all __maybe_unused)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500636{
637 size_t size;
638
David Aherneda39132011-07-15 12:34:09 -0600639 perf_event__attr_swap(&event->attr.attr);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500640
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200641 size = event->header.size;
642 size -= (void *)&event->attr.id - (void *)event;
643 mem_bswap_64(event->attr.id, size);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500644}
645
Jiri Olsaffe777252015-10-25 15:51:36 +0100646static void perf_event__event_update_swap(union perf_event *event,
647 bool sample_id_all __maybe_unused)
648{
649 event->event_update.type = bswap_64(event->event_update.type);
650 event->event_update.id = bswap_64(event->event_update.id);
651}
652
Jiri Olsa268fb202012-05-30 14:23:43 +0200653static void perf_event__event_type_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300654 bool sample_id_all __maybe_unused)
Tom Zanussicd19a032010-04-01 23:59:20 -0500655{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200656 event->event_type.event_type.event_id =
657 bswap_64(event->event_type.event_type.event_id);
Tom Zanussicd19a032010-04-01 23:59:20 -0500658}
659
Jiri Olsa268fb202012-05-30 14:23:43 +0200660static void perf_event__tracing_data_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300661 bool sample_id_all __maybe_unused)
Tom Zanussi92155452010-04-01 23:59:21 -0500662{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200663 event->tracing_data.size = bswap_32(event->tracing_data.size);
Tom Zanussi92155452010-04-01 23:59:21 -0500664}
665
Adrian Huntera16ac022015-04-09 18:53:43 +0300666static void perf_event__auxtrace_info_swap(union perf_event *event,
667 bool sample_id_all __maybe_unused)
668{
669 size_t size;
670
671 event->auxtrace_info.type = bswap_32(event->auxtrace_info.type);
672
673 size = event->header.size;
674 size -= (void *)&event->auxtrace_info.priv - (void *)event;
675 mem_bswap_64(event->auxtrace_info.priv, size);
676}
677
678static void perf_event__auxtrace_swap(union perf_event *event,
679 bool sample_id_all __maybe_unused)
680{
681 event->auxtrace.size = bswap_64(event->auxtrace.size);
682 event->auxtrace.offset = bswap_64(event->auxtrace.offset);
683 event->auxtrace.reference = bswap_64(event->auxtrace.reference);
684 event->auxtrace.idx = bswap_32(event->auxtrace.idx);
685 event->auxtrace.tid = bswap_32(event->auxtrace.tid);
686 event->auxtrace.cpu = bswap_32(event->auxtrace.cpu);
687}
688
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300689static void perf_event__auxtrace_error_swap(union perf_event *event,
690 bool sample_id_all __maybe_unused)
691{
692 event->auxtrace_error.type = bswap_32(event->auxtrace_error.type);
693 event->auxtrace_error.code = bswap_32(event->auxtrace_error.code);
694 event->auxtrace_error.cpu = bswap_32(event->auxtrace_error.cpu);
695 event->auxtrace_error.pid = bswap_32(event->auxtrace_error.pid);
696 event->auxtrace_error.tid = bswap_32(event->auxtrace_error.tid);
697 event->auxtrace_error.ip = bswap_64(event->auxtrace_error.ip);
698}
699
Jiri Olsa5f3339d2015-10-25 15:51:19 +0100700static void perf_event__thread_map_swap(union perf_event *event,
701 bool sample_id_all __maybe_unused)
702{
703 unsigned i;
704
705 event->thread_map.nr = bswap_64(event->thread_map.nr);
706
707 for (i = 0; i < event->thread_map.nr; i++)
708 event->thread_map.entries[i].pid = bswap_64(event->thread_map.entries[i].pid);
709}
710
Jiri Olsa6640b6c2015-10-25 15:51:23 +0100711static void perf_event__cpu_map_swap(union perf_event *event,
712 bool sample_id_all __maybe_unused)
713{
714 struct cpu_map_data *data = &event->cpu_map.data;
715 struct cpu_map_entries *cpus;
716 struct cpu_map_mask *mask;
717 unsigned i;
718
719 data->type = bswap_64(data->type);
720
721 switch (data->type) {
722 case PERF_CPU_MAP__CPUS:
723 cpus = (struct cpu_map_entries *)data->data;
724
725 cpus->nr = bswap_16(cpus->nr);
726
727 for (i = 0; i < cpus->nr; i++)
728 cpus->cpu[i] = bswap_16(cpus->cpu[i]);
729 break;
730 case PERF_CPU_MAP__MASK:
731 mask = (struct cpu_map_mask *) data->data;
732
733 mask->nr = bswap_16(mask->nr);
734 mask->long_size = bswap_16(mask->long_size);
735
736 switch (mask->long_size) {
737 case 4: mem_bswap_32(&mask->mask, mask->nr); break;
738 case 8: mem_bswap_64(&mask->mask, mask->nr); break;
739 default:
740 pr_err("cpu_map swap: unsupported long size\n");
741 }
742 default:
743 break;
744 }
745}
746
Jiri Olsa374fb9e2015-10-25 15:51:27 +0100747static void perf_event__stat_config_swap(union perf_event *event,
748 bool sample_id_all __maybe_unused)
749{
750 u64 size;
751
752 size = event->stat_config.nr * sizeof(event->stat_config.data[0]);
753 size += 1; /* nr item itself */
754 mem_bswap_64(&event->stat_config.nr, size);
755}
756
Jiri Olsad80518c2015-10-25 15:51:30 +0100757static void perf_event__stat_swap(union perf_event *event,
758 bool sample_id_all __maybe_unused)
759{
760 event->stat.id = bswap_64(event->stat.id);
761 event->stat.thread = bswap_32(event->stat.thread);
762 event->stat.cpu = bswap_32(event->stat.cpu);
763 event->stat.val = bswap_64(event->stat.val);
764 event->stat.ena = bswap_64(event->stat.ena);
765 event->stat.run = bswap_64(event->stat.run);
766}
767
Jiri Olsa2d8f0f12015-10-25 15:51:33 +0100768static void perf_event__stat_round_swap(union perf_event *event,
769 bool sample_id_all __maybe_unused)
770{
771 event->stat_round.type = bswap_64(event->stat_round.type);
772 event->stat_round.time = bswap_64(event->stat_round.time);
773}
774
Jiri Olsa268fb202012-05-30 14:23:43 +0200775typedef void (*perf_event__swap_op)(union perf_event *event,
776 bool sample_id_all);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200777
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200778static perf_event__swap_op perf_event__swap_ops[] = {
779 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200780 [PERF_RECORD_MMAP2] = perf_event__mmap2_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200781 [PERF_RECORD_COMM] = perf_event__comm_swap,
782 [PERF_RECORD_FORK] = perf_event__task_swap,
783 [PERF_RECORD_EXIT] = perf_event__task_swap,
784 [PERF_RECORD_LOST] = perf_event__all64_swap,
785 [PERF_RECORD_READ] = perf_event__read_swap,
Jiri Olsadd96c462013-09-01 12:36:15 +0200786 [PERF_RECORD_THROTTLE] = perf_event__throttle_swap,
787 [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200788 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300789 [PERF_RECORD_AUX] = perf_event__aux_swap,
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300790 [PERF_RECORD_ITRACE_START] = perf_event__itrace_start_swap,
Kan Liangc4937a92015-05-10 15:13:15 -0400791 [PERF_RECORD_LOST_SAMPLES] = perf_event__all64_swap,
Adrian Hunter02860392015-07-21 12:44:03 +0300792 [PERF_RECORD_SWITCH] = perf_event__switch_swap,
793 [PERF_RECORD_SWITCH_CPU_WIDE] = perf_event__switch_swap,
David Aherneda39132011-07-15 12:34:09 -0600794 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200795 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
796 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
797 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200798 [PERF_RECORD_ID_INDEX] = perf_event__all64_swap,
Adrian Huntera16ac022015-04-09 18:53:43 +0300799 [PERF_RECORD_AUXTRACE_INFO] = perf_event__auxtrace_info_swap,
800 [PERF_RECORD_AUXTRACE] = perf_event__auxtrace_swap,
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300801 [PERF_RECORD_AUXTRACE_ERROR] = perf_event__auxtrace_error_swap,
Jiri Olsa5f3339d2015-10-25 15:51:19 +0100802 [PERF_RECORD_THREAD_MAP] = perf_event__thread_map_swap,
Jiri Olsa6640b6c2015-10-25 15:51:23 +0100803 [PERF_RECORD_CPU_MAP] = perf_event__cpu_map_swap,
Jiri Olsa374fb9e2015-10-25 15:51:27 +0100804 [PERF_RECORD_STAT_CONFIG] = perf_event__stat_config_swap,
Jiri Olsad80518c2015-10-25 15:51:30 +0100805 [PERF_RECORD_STAT] = perf_event__stat_swap,
Jiri Olsa2d8f0f12015-10-25 15:51:33 +0100806 [PERF_RECORD_STAT_ROUND] = perf_event__stat_round_swap,
Jiri Olsaffe777252015-10-25 15:51:36 +0100807 [PERF_RECORD_EVENT_UPDATE] = perf_event__event_update_swap,
Adrian Hunter46bc29b2016-03-08 10:38:44 +0200808 [PERF_RECORD_TIME_CONV] = perf_event__all64_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200809 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200810};
811
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200812/*
813 * When perf record finishes a pass on every buffers, it records this pseudo
814 * event.
815 * We record the max timestamp t found in the pass n.
816 * Assuming these timestamps are monotonic across cpus, we know that if
817 * a buffer still has events with timestamps below t, they will be all
818 * available and then read in the pass n + 1.
819 * Hence when we start to read the pass n + 2, we can safely flush every
820 * events with timestamps below t.
821 *
822 * ============ PASS n =================
823 * CPU 0 | CPU 1
824 * |
825 * cnt1 timestamps | cnt2 timestamps
826 * 1 | 2
827 * 2 | 3
828 * - | 4 <--- max recorded
829 *
830 * ============ PASS n + 1 ==============
831 * CPU 0 | CPU 1
832 * |
833 * cnt1 timestamps | cnt2 timestamps
834 * 3 | 5
835 * 4 | 6
836 * 5 | 7 <---- max recorded
837 *
838 * Flush every events below timestamp 4
839 *
840 * ============ PASS n + 2 ==============
841 * CPU 0 | CPU 1
842 * |
843 * cnt1 timestamps | cnt2 timestamps
844 * 6 | 8
845 * 7 | 9
846 * - | 10
847 *
848 * Flush every events below timestamp 7
849 * etc...
850 */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300851static int process_finished_round(struct perf_tool *tool __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300852 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300853 struct ordered_events *oe)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200854{
Adrian Hunter5531e162015-06-23 10:52:48 +0300855 if (dump_trace)
856 fprintf(stdout, "\n");
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300857 return ordered_events__flush(oe, OE_FLUSH__ROUND);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200858}
859
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300860int perf_session__queue_event(struct perf_session *s, union perf_event *event,
Jiri Olsadc83e132017-08-03 13:24:33 +0200861 u64 timestamp, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200862{
Jiri Olsadc83e132017-08-03 13:24:33 +0200863 return ordered_events__queue(&s->ordered_events, event, timestamp, file_offset);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200864}
865
Kan Liang384b6052015-01-05 13:23:05 -0500866static void callchain__lbr_callstack_printf(struct perf_sample *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200867{
Kan Liang384b6052015-01-05 13:23:05 -0500868 struct ip_callchain *callchain = sample->callchain;
869 struct branch_stack *lbr_stack = sample->branch_stack;
870 u64 kernel_callchain_nr = callchain->nr;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200871 unsigned int i;
872
Kan Liang384b6052015-01-05 13:23:05 -0500873 for (i = 0; i < kernel_callchain_nr; i++) {
874 if (callchain->ips[i] == PERF_CONTEXT_USER)
875 break;
876 }
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200877
Kan Liang384b6052015-01-05 13:23:05 -0500878 if ((i != kernel_callchain_nr) && lbr_stack->nr) {
879 u64 total_nr;
880 /*
881 * LBR callstack can only get user call chain,
882 * i is kernel call chain number,
883 * 1 is PERF_CONTEXT_USER.
884 *
885 * The user call chain is stored in LBR registers.
886 * LBR are pair registers. The caller is stored
887 * in "from" register, while the callee is stored
888 * in "to" register.
889 * For example, there is a call stack
890 * "A"->"B"->"C"->"D".
891 * The LBR registers will recorde like
892 * "C"->"D", "B"->"C", "A"->"B".
893 * So only the first "to" register and all "from"
894 * registers are needed to construct the whole stack.
895 */
896 total_nr = i + 1 + lbr_stack->nr + 1;
897 kernel_callchain_nr = i + 1;
898
899 printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr);
900
901 for (i = 0; i < kernel_callchain_nr; i++)
902 printf("..... %2d: %016" PRIx64 "\n",
903 i, callchain->ips[i]);
904
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200905 printf("..... %2d: %016" PRIx64 "\n",
Kan Liang384b6052015-01-05 13:23:05 -0500906 (int)(kernel_callchain_nr), lbr_stack->entries[0].to);
907 for (i = 0; i < lbr_stack->nr; i++)
908 printf("..... %2d: %016" PRIx64 "\n",
909 (int)(i + kernel_callchain_nr + 1), lbr_stack->entries[i].from);
910 }
911}
912
913static void callchain__printf(struct perf_evsel *evsel,
914 struct perf_sample *sample)
915{
916 unsigned int i;
917 struct ip_callchain *callchain = sample->callchain;
918
Arnaldo Carvalho de Meloacf2abb2016-04-18 10:35:03 -0300919 if (perf_evsel__has_branch_callstack(evsel))
Kan Liang384b6052015-01-05 13:23:05 -0500920 callchain__lbr_callstack_printf(sample);
921
922 printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr);
923
924 for (i = 0; i < callchain->nr; i++)
925 printf("..... %2d: %016" PRIx64 "\n",
926 i, callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200927}
928
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100929static void branch_stack__printf(struct perf_sample *sample)
930{
931 uint64_t i;
932
933 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
934
Andi Kleen0e332f02015-07-18 08:24:46 -0700935 for (i = 0; i < sample->branch_stack->nr; i++) {
936 struct branch_entry *e = &sample->branch_stack->entries[i];
937
938 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 " %hu cycles %s%s%s%s %x\n",
939 i, e->from, e->to,
Arnaldo Carvalho de Melo8074bf52017-02-17 12:27:26 -0300940 (unsigned short)e->flags.cycles,
Andi Kleen0e332f02015-07-18 08:24:46 -0700941 e->flags.mispred ? "M" : " ",
942 e->flags.predicted ? "P" : " ",
943 e->flags.abort ? "A" : " ",
944 e->flags.in_tx ? "T" : " ",
945 (unsigned)e->flags.reserved);
946 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100947}
948
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200949static void regs_dump__printf(u64 mask, u64 *regs)
950{
951 unsigned rid, i = 0;
952
953 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
954 u64 val = regs[i++];
955
956 printf(".... %-5s 0x%" PRIx64 "\n",
957 perf_reg_name(rid), val);
958 }
959}
960
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200961static const char *regs_abi[] = {
962 [PERF_SAMPLE_REGS_ABI_NONE] = "none",
963 [PERF_SAMPLE_REGS_ABI_32] = "32-bit",
964 [PERF_SAMPLE_REGS_ABI_64] = "64-bit",
965};
966
967static inline const char *regs_dump_abi(struct regs_dump *d)
968{
969 if (d->abi > PERF_SAMPLE_REGS_ABI_64)
970 return "unknown";
971
972 return regs_abi[d->abi];
973}
974
975static void regs__printf(const char *type, struct regs_dump *regs)
976{
977 u64 mask = regs->mask;
978
979 printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n",
980 type,
981 mask,
982 regs_dump_abi(regs));
983
984 regs_dump__printf(mask, regs->regs);
985}
986
Jiri Olsa352ea452014-01-07 13:47:25 +0100987static void regs_user__printf(struct perf_sample *sample)
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200988{
989 struct regs_dump *user_regs = &sample->user_regs;
990
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200991 if (user_regs->regs)
992 regs__printf("user", user_regs);
993}
994
995static void regs_intr__printf(struct perf_sample *sample)
996{
997 struct regs_dump *intr_regs = &sample->intr_regs;
998
999 if (intr_regs->regs)
1000 regs__printf("intr", intr_regs);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001001}
1002
1003static void stack_user__printf(struct stack_dump *dump)
1004{
1005 printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
1006 dump->size, dump->offset);
1007}
1008
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001009static void perf_evlist__print_tstamp(struct perf_evlist *evlist,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001010 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001011 struct perf_sample *sample)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -02001012{
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001013 u64 sample_type = __perf_evlist__combined_sample_type(evlist);
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001014
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -02001015 if (event->header.type != PERF_RECORD_SAMPLE &&
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001016 !perf_evlist__sample_id_all(evlist)) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -02001017 fputs("-1 -1 ", stdout);
1018 return;
1019 }
1020
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001021 if ((sample_type & PERF_SAMPLE_CPU))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -02001022 printf("%u ", sample->cpu);
1023
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001024 if (sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001025 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -02001026}
1027
Jiri Olsa9ede4732012-10-10 17:38:13 +02001028static void sample_read__printf(struct perf_sample *sample, u64 read_format)
1029{
1030 printf("... sample_read:\n");
1031
1032 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1033 printf("...... time enabled %016" PRIx64 "\n",
1034 sample->read.time_enabled);
1035
1036 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1037 printf("...... time running %016" PRIx64 "\n",
1038 sample->read.time_running);
1039
1040 if (read_format & PERF_FORMAT_GROUP) {
1041 u64 i;
1042
1043 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
1044
1045 for (i = 0; i < sample->read.group.nr; i++) {
1046 struct sample_read_value *value;
1047
1048 value = &sample->read.group.values[i];
1049 printf("..... id %016" PRIx64
1050 ", value %016" PRIx64 "\n",
1051 value->id, value->value);
1052 }
1053 } else
1054 printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
1055 sample->read.one.id, sample->read.one.value);
1056}
1057
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001058static void dump_event(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001059 u64 file_offset, struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001060{
1061 if (!dump_trace)
1062 return;
1063
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001064 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
1065 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001066
1067 trace_event(event);
1068
1069 if (sample)
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001070 perf_evlist__print_tstamp(evlist, event, sample);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001071
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001072 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001073 event->header.size, perf_event__name(event->header.type));
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001074}
1075
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001076static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001077 struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001078{
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001079 u64 sample_type;
1080
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -02001081 if (!dump_trace)
1082 return;
1083
Don Zickus0ea590a2014-02-25 22:43:46 -05001084 printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001085 event->header.misc, sample->pid, sample->tid, sample->ip,
David Ahern7cec0922011-05-30 13:08:23 -06001086 sample->period, sample->addr);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001087
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001088 sample_type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001089
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -03001090 if (evsel__has_callchain(evsel))
Kan Liang384b6052015-01-05 13:23:05 -05001091 callchain__printf(evsel, sample);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001092
Arnaldo Carvalho de Meloacf2abb2016-04-18 10:35:03 -03001093 if ((sample_type & PERF_SAMPLE_BRANCH_STACK) && !perf_evsel__has_branch_callstack(evsel))
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001094 branch_stack__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001095
1096 if (sample_type & PERF_SAMPLE_REGS_USER)
Jiri Olsa352ea452014-01-07 13:47:25 +01001097 regs_user__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001098
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001099 if (sample_type & PERF_SAMPLE_REGS_INTR)
1100 regs_intr__printf(sample);
1101
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001102 if (sample_type & PERF_SAMPLE_STACK_USER)
1103 stack_user__printf(&sample->user_stack);
Andi Kleen05484292013-01-24 16:10:29 +01001104
1105 if (sample_type & PERF_SAMPLE_WEIGHT)
1106 printf("... weight: %" PRIu64 "\n", sample->weight);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001107
1108 if (sample_type & PERF_SAMPLE_DATA_SRC)
1109 printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001110
Kan Liang8780fb22017-08-29 13:11:09 -04001111 if (sample_type & PERF_SAMPLE_PHYS_ADDR)
1112 printf(" .. phys_addr: 0x%"PRIx64"\n", sample->phys_addr);
1113
Andi Kleen475eeab2013-09-20 07:40:43 -07001114 if (sample_type & PERF_SAMPLE_TRANSACTION)
1115 printf("... transaction: %" PRIx64 "\n", sample->transaction);
1116
Jiri Olsa9ede4732012-10-10 17:38:13 +02001117 if (sample_type & PERF_SAMPLE_READ)
1118 sample_read__printf(sample, evsel->attr.read_format);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001119}
1120
Jiri Olsadac7f6b2017-08-24 18:27:32 +02001121static void dump_read(struct perf_evsel *evsel, union perf_event *event)
1122{
1123 struct read_event *read_event = &event->read;
1124 u64 read_format;
1125
1126 if (!dump_trace)
1127 return;
1128
1129 printf(": %d %d %s %" PRIu64 "\n", event->read.pid, event->read.tid,
1130 evsel ? perf_evsel__name(evsel) : "FAIL",
1131 event->read.value);
1132
1133 read_format = evsel->attr.read_format;
1134
1135 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1136 printf("... time enabled : %" PRIu64 "\n", read_event->time_enabled);
1137
1138 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1139 printf("... time running : %" PRIu64 "\n", read_event->time_running);
1140
1141 if (read_format & PERF_FORMAT_ID)
1142 printf("... id : %" PRIu64 "\n", read_event->id);
1143}
1144
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -03001145static struct machine *machines__find_for_cpumode(struct machines *machines,
Adrian Hunteref893252013-08-27 11:23:06 +03001146 union perf_event *event,
1147 struct perf_sample *sample)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001148{
Dongsheng Yangad85ace2013-12-20 13:41:47 -05001149 struct machine *machine;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001150
David Ahern7c0f4a42012-07-20 17:25:48 -06001151 if (perf_guest &&
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001152 ((sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
1153 (sample->cpumode == PERF_RECORD_MISC_GUEST_USER))) {
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +05301154 u32 pid;
1155
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001156 if (event->header.type == PERF_RECORD_MMAP
1157 || event->header.type == PERF_RECORD_MMAP2)
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +05301158 pid = event->mmap.pid;
1159 else
Adrian Hunteref893252013-08-27 11:23:06 +03001160 pid = sample->pid;
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +05301161
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -03001162 machine = machines__find(machines, pid);
Dongsheng Yangad85ace2013-12-20 13:41:47 -05001163 if (!machine)
Ravi Bangoria3caeaa52015-12-07 12:25:02 +05301164 machine = machines__findnew(machines, DEFAULT_GUEST_KERNEL_ID);
Dongsheng Yangad85ace2013-12-20 13:41:47 -05001165 return machine;
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +05301166 }
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001167
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -03001168 return &machines->host;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001169}
1170
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001171static int deliver_sample_value(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +02001172 struct perf_tool *tool,
1173 union perf_event *event,
1174 struct perf_sample *sample,
1175 struct sample_read_value *v,
1176 struct machine *machine)
1177{
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001178 struct perf_sample_id *sid = perf_evlist__id2sid(evlist, v->id);
Jiri Olsae4caec02012-10-10 18:52:24 +02001179
Jiri Olsae4caec02012-10-10 18:52:24 +02001180 if (sid) {
1181 sample->id = v->id;
1182 sample->period = v->value - sid->period;
1183 sid->period = v->value;
1184 }
1185
1186 if (!sid || sid->evsel == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001187 ++evlist->stats.nr_unknown_id;
Jiri Olsae4caec02012-10-10 18:52:24 +02001188 return 0;
1189 }
1190
1191 return tool->sample(tool, event, sample, sid->evsel, machine);
1192}
1193
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001194static int deliver_sample_group(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +02001195 struct perf_tool *tool,
1196 union perf_event *event,
1197 struct perf_sample *sample,
1198 struct machine *machine)
1199{
1200 int ret = -EINVAL;
1201 u64 i;
1202
1203 for (i = 0; i < sample->read.group.nr; i++) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001204 ret = deliver_sample_value(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001205 &sample->read.group.values[i],
1206 machine);
1207 if (ret)
1208 break;
1209 }
1210
1211 return ret;
1212}
1213
1214static int
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001215 perf_evlist__deliver_sample(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +02001216 struct perf_tool *tool,
1217 union perf_event *event,
1218 struct perf_sample *sample,
1219 struct perf_evsel *evsel,
1220 struct machine *machine)
1221{
1222 /* We know evsel != NULL. */
1223 u64 sample_type = evsel->attr.sample_type;
1224 u64 read_format = evsel->attr.read_format;
1225
Soramichi AKIYAMAd94386f2017-01-17 22:22:33 +09001226 /* Standard sample delivery. */
Jiri Olsae4caec02012-10-10 18:52:24 +02001227 if (!(sample_type & PERF_SAMPLE_READ))
1228 return tool->sample(tool, event, sample, evsel, machine);
1229
1230 /* For PERF_SAMPLE_READ we have either single or group mode. */
1231 if (read_format & PERF_FORMAT_GROUP)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001232 return deliver_sample_group(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001233 machine);
1234 else
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001235 return deliver_sample_value(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001236 &sample->read.one, machine);
1237}
1238
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -03001239static int machines__deliver_event(struct machines *machines,
1240 struct perf_evlist *evlist,
1241 union perf_event *event,
1242 struct perf_sample *sample,
1243 struct perf_tool *tool, u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001244{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001245 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001246 struct machine *machine;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001247
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001248 dump_event(evlist, event, file_offset, sample);
Thomas Gleixner532e7262010-12-07 12:48:55 +00001249
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001250 evsel = perf_evlist__id2evsel(evlist, sample->id);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001251
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001252 machine = machines__find_for_cpumode(machines, event, sample);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001253
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001254 switch (event->header.type) {
1255 case PERF_RECORD_SAMPLE:
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001256 if (evsel == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001257 ++evlist->stats.nr_unknown_id;
Jiri Olsa67822062012-04-12 14:21:01 +02001258 return 0;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001259 }
Kan Liang1b29ac52015-09-03 08:31:00 -04001260 dump_sample(evsel, event, sample);
Joerg Roedel0c095712012-02-10 18:05:04 +01001261 if (machine == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001262 ++evlist->stats.nr_unprocessable_samples;
Jiri Olsa67822062012-04-12 14:21:01 +02001263 return 0;
Joerg Roedel0c095712012-02-10 18:05:04 +01001264 }
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001265 return perf_evlist__deliver_sample(evlist, tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001266 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001267 return tool->mmap(tool, event, sample, machine);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001268 case PERF_RECORD_MMAP2:
Kan Liang930e6fc2015-06-17 09:51:10 -04001269 if (event->header.misc & PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT)
1270 ++evlist->stats.nr_proc_map_timeout;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001271 return tool->mmap2(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001272 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001273 return tool->comm(tool, event, sample, machine);
Hari Bathinif3b36142017-03-08 02:11:43 +05301274 case PERF_RECORD_NAMESPACES:
1275 return tool->namespaces(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001276 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001277 return tool->fork(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001278 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001279 return tool->exit(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001280 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001281 if (tool->lost == perf_event__process_lost)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001282 evlist->stats.total_lost += event->lost.lost;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001283 return tool->lost(tool, event, sample, machine);
Kan Liangc4937a92015-05-10 15:13:15 -04001284 case PERF_RECORD_LOST_SAMPLES:
1285 if (tool->lost_samples == perf_event__process_lost_samples)
1286 evlist->stats.total_lost_samples += event->lost_samples.lost;
1287 return tool->lost_samples(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001288 case PERF_RECORD_READ:
Jiri Olsadac7f6b2017-08-24 18:27:32 +02001289 dump_read(evsel, event);
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001290 return tool->read(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001291 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001292 return tool->throttle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001293 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001294 return tool->unthrottle(tool, event, sample, machine);
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001295 case PERF_RECORD_AUX:
Alexander Shishkin05a1f472017-03-16 18:41:59 +02001296 if (tool->aux == perf_event__process_aux) {
1297 if (event->aux.flags & PERF_AUX_FLAG_TRUNCATED)
1298 evlist->stats.total_aux_lost += 1;
1299 if (event->aux.flags & PERF_AUX_FLAG_PARTIAL)
1300 evlist->stats.total_aux_partial += 1;
1301 }
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001302 return tool->aux(tool, event, sample, machine);
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001303 case PERF_RECORD_ITRACE_START:
1304 return tool->itrace_start(tool, event, sample, machine);
Adrian Hunter02860392015-07-21 12:44:03 +03001305 case PERF_RECORD_SWITCH:
1306 case PERF_RECORD_SWITCH_CPU_WIDE:
1307 return tool->context_switch(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001308 default:
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001309 ++evlist->stats.nr_unknown_events;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001310 return -1;
1311 }
1312}
1313
Adrian Hunterc4468702015-04-09 18:53:48 +03001314static int perf_session__deliver_event(struct perf_session *session,
1315 union perf_event *event,
Adrian Hunterc4468702015-04-09 18:53:48 +03001316 struct perf_tool *tool,
1317 u64 file_offset)
1318{
Jiri Olsa93d10af2017-08-03 13:21:14 +02001319 struct perf_sample sample;
Adrian Hunterc4468702015-04-09 18:53:48 +03001320 int ret;
1321
Jiri Olsa93d10af2017-08-03 13:21:14 +02001322 ret = perf_evlist__parse_sample(session->evlist, event, &sample);
1323 if (ret) {
1324 pr_err("Can't parse sample, err = %d\n", ret);
1325 return ret;
1326 }
1327
1328 ret = auxtrace__process_event(session, event, &sample, tool);
Adrian Hunterc4468702015-04-09 18:53:48 +03001329 if (ret < 0)
1330 return ret;
1331 if (ret > 0)
1332 return 0;
1333
1334 return machines__deliver_event(&session->machines, session->evlist,
Jiri Olsa93d10af2017-08-03 13:21:14 +02001335 event, &sample, tool, file_offset);
Adrian Hunterc4468702015-04-09 18:53:48 +03001336}
1337
Adrian Hunterd5652d82014-07-23 22:19:58 +03001338static s64 perf_session__process_user_event(struct perf_session *session,
1339 union perf_event *event,
Adrian Hunterd5652d82014-07-23 22:19:58 +03001340 u64 file_offset)
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001341{
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -03001342 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001343 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melof250b092017-11-23 15:35:04 -03001344 struct perf_sample sample = { .time = 0, };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001345 int fd = perf_data__fd(session->data);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001346 int err;
1347
Arnaldo Carvalho de Melof250b092017-11-23 15:35:04 -03001348 dump_event(session->evlist, event, file_offset, &sample);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001349
1350 /* These events are processed right away */
1351 switch (event->header.type) {
1352 case PERF_RECORD_HEADER_ATTR:
Adrian Hunter47c3d102013-07-04 16:20:21 +03001353 err = tool->attr(tool, event, &session->evlist);
Adrian Huntercfe1c412014-07-31 09:00:45 +03001354 if (err == 0) {
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -03001355 perf_session__set_id_hdr_size(session);
Adrian Huntercfe1c412014-07-31 09:00:45 +03001356 perf_session__set_comm_exec(session);
1357 }
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001358 return err;
Jiri Olsaffe777252015-10-25 15:51:36 +01001359 case PERF_RECORD_EVENT_UPDATE:
1360 return tool->event_update(tool, event, &session->evlist);
Jiri Olsaf67697b2014-02-04 15:37:48 +01001361 case PERF_RECORD_HEADER_EVENT_TYPE:
1362 /*
1363 * Depreceated, but we need to handle it for sake
1364 * of old data files create in pipe mode.
1365 */
1366 return 0;
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001367 case PERF_RECORD_HEADER_TRACING_DATA:
1368 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001369 lseek(fd, file_offset, SEEK_SET);
Jiri Olsa89f16882018-09-13 14:54:03 +02001370 return tool->tracing_data(session, event);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001371 case PERF_RECORD_HEADER_BUILD_ID:
Jiri Olsa89f16882018-09-13 14:54:03 +02001372 return tool->build_id(session, event);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001373 case PERF_RECORD_FINISHED_ROUND:
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -03001374 return tool->finished_round(tool, event, oe);
Adrian Hunter3c659ee2014-10-27 15:49:22 +02001375 case PERF_RECORD_ID_INDEX:
Jiri Olsa89f16882018-09-13 14:54:03 +02001376 return tool->id_index(session, event);
Adrian Huntera16ac022015-04-09 18:53:43 +03001377 case PERF_RECORD_AUXTRACE_INFO:
Jiri Olsa89f16882018-09-13 14:54:03 +02001378 return tool->auxtrace_info(session, event);
Adrian Huntera16ac022015-04-09 18:53:43 +03001379 case PERF_RECORD_AUXTRACE:
1380 /* setup for reading amidst mmap */
1381 lseek(fd, file_offset + event->header.size, SEEK_SET);
Jiri Olsa73365552018-09-13 14:54:04 +02001382 return tool->auxtrace(session, event);
Adrian Huntere9bf54d2015-04-09 18:53:47 +03001383 case PERF_RECORD_AUXTRACE_ERROR:
Adrian Hunter85ed4722015-04-09 18:53:50 +03001384 perf_session__auxtrace_error_inc(session, event);
Jiri Olsa89f16882018-09-13 14:54:03 +02001385 return tool->auxtrace_error(session, event);
Jiri Olsa5f3339d2015-10-25 15:51:19 +01001386 case PERF_RECORD_THREAD_MAP:
Jiri Olsa89f16882018-09-13 14:54:03 +02001387 return tool->thread_map(session, event);
Jiri Olsa6640b6c2015-10-25 15:51:23 +01001388 case PERF_RECORD_CPU_MAP:
Jiri Olsa89f16882018-09-13 14:54:03 +02001389 return tool->cpu_map(session, event);
Jiri Olsa374fb9e2015-10-25 15:51:27 +01001390 case PERF_RECORD_STAT_CONFIG:
Jiri Olsa89f16882018-09-13 14:54:03 +02001391 return tool->stat_config(session, event);
Jiri Olsad80518c2015-10-25 15:51:30 +01001392 case PERF_RECORD_STAT:
Jiri Olsa89f16882018-09-13 14:54:03 +02001393 return tool->stat(session, event);
Jiri Olsa2d8f0f12015-10-25 15:51:33 +01001394 case PERF_RECORD_STAT_ROUND:
Jiri Olsa89f16882018-09-13 14:54:03 +02001395 return tool->stat_round(session, event);
Adrian Hunter46bc29b2016-03-08 10:38:44 +02001396 case PERF_RECORD_TIME_CONV:
1397 session->time_conv = event->time_conv;
Jiri Olsa89f16882018-09-13 14:54:03 +02001398 return tool->time_conv(session, event);
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07001399 case PERF_RECORD_HEADER_FEATURE:
Jiri Olsa89f16882018-09-13 14:54:03 +02001400 return tool->feature(session, event);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001401 default:
1402 return -EINVAL;
1403 }
1404}
1405
Adrian Huntera2938292014-10-27 15:49:23 +02001406int perf_session__deliver_synth_event(struct perf_session *session,
1407 union perf_event *event,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001408 struct perf_sample *sample)
Adrian Huntera2938292014-10-27 15:49:23 +02001409{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001410 struct perf_evlist *evlist = session->evlist;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001411 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001412
1413 events_stats__inc(&evlist->stats, event->header.type);
Adrian Huntera2938292014-10-27 15:49:23 +02001414
1415 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001416 return perf_session__process_user_event(session, event, 0);
Adrian Huntera2938292014-10-27 15:49:23 +02001417
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001418 return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0);
Adrian Huntera2938292014-10-27 15:49:23 +02001419}
1420
Jiri Olsa268fb202012-05-30 14:23:43 +02001421static void event_swap(union perf_event *event, bool sample_id_all)
1422{
1423 perf_event__swap_op swap;
1424
1425 swap = perf_event__swap_ops[event->header.type];
1426 if (swap)
1427 swap(event, sample_id_all);
1428}
1429
Adrian Hunter5a52f332014-07-31 09:00:57 +03001430int perf_session__peek_event(struct perf_session *session, off_t file_offset,
1431 void *buf, size_t buf_sz,
1432 union perf_event **event_ptr,
1433 struct perf_sample *sample)
1434{
1435 union perf_event *event;
1436 size_t hdr_sz, rest;
1437 int fd;
1438
1439 if (session->one_mmap && !session->header.needs_swap) {
1440 event = file_offset - session->one_mmap_offset +
1441 session->one_mmap_addr;
1442 goto out_parse_sample;
1443 }
1444
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001445 if (perf_data__is_pipe(session->data))
Adrian Hunter5a52f332014-07-31 09:00:57 +03001446 return -1;
1447
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001448 fd = perf_data__fd(session->data);
Adrian Hunter5a52f332014-07-31 09:00:57 +03001449 hdr_sz = sizeof(struct perf_event_header);
1450
1451 if (buf_sz < hdr_sz)
1452 return -1;
1453
1454 if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 ||
Adrian Hunter554e92e2015-05-19 16:05:45 +03001455 readn(fd, buf, hdr_sz) != (ssize_t)hdr_sz)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001456 return -1;
1457
1458 event = (union perf_event *)buf;
1459
1460 if (session->header.needs_swap)
1461 perf_event_header__bswap(&event->header);
1462
Adrian Hunter554e92e2015-05-19 16:05:45 +03001463 if (event->header.size < hdr_sz || event->header.size > buf_sz)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001464 return -1;
1465
1466 rest = event->header.size - hdr_sz;
1467
Adrian Hunter554e92e2015-05-19 16:05:45 +03001468 if (readn(fd, buf, rest) != (ssize_t)rest)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001469 return -1;
1470
1471 if (session->header.needs_swap)
1472 event_swap(event, perf_evlist__sample_id_all(session->evlist));
1473
1474out_parse_sample:
1475
1476 if (sample && event->header.type < PERF_RECORD_USER_TYPE_START &&
1477 perf_evlist__parse_sample(session->evlist, event, sample))
1478 return -1;
1479
1480 *event_ptr = event;
1481
1482 return 0;
1483}
1484
Adrian Hunterd5652d82014-07-23 22:19:58 +03001485static s64 perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001486 union perf_event *event, u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001487{
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001488 struct perf_evlist *evlist = session->evlist;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001489 struct perf_tool *tool = session->tool;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001490 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001491
Jiri Olsa268fb202012-05-30 14:23:43 +02001492 if (session->header.needs_swap)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001493 event_swap(event, perf_evlist__sample_id_all(evlist));
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001494
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001495 if (event->header.type >= PERF_RECORD_HEADER_MAX)
1496 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001497
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001498 events_stats__inc(&evlist->stats, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001499
1500 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001501 return perf_session__process_user_event(session, event, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001502
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001503 if (tool->ordered_events) {
Mathieu Poirier631e8f02018-01-10 13:31:52 -07001504 u64 timestamp = -1ULL;
Jiri Olsa93d10af2017-08-03 13:21:14 +02001505
1506 ret = perf_evlist__parse_sample_timestamp(evlist, event, &timestamp);
Mathieu Poirier631e8f02018-01-10 13:31:52 -07001507 if (ret && ret != -1)
Jiri Olsa93d10af2017-08-03 13:21:14 +02001508 return ret;
1509
1510 ret = perf_session__queue_event(session, event, timestamp, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001511 if (ret != -ETIME)
1512 return ret;
1513 }
1514
Jiri Olsa93d10af2017-08-03 13:21:14 +02001515 return perf_session__deliver_event(session, event, tool, file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001516}
1517
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001518void perf_event_header__bswap(struct perf_event_header *hdr)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001519{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001520 hdr->type = bswap_32(hdr->type);
1521 hdr->misc = bswap_16(hdr->misc);
1522 hdr->size = bswap_16(hdr->size);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001523}
1524
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001525struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1526{
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001527 return machine__findnew_thread(&session->machines.host, -1, pid);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001528}
1529
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001530int perf_session__register_idle_thread(struct perf_session *session)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001531{
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001532 struct thread *thread;
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001533 int err = 0;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001534
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001535 thread = machine__findnew_thread(&session->machines.host, 0, 0);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001536 if (thread == NULL || thread__set_comm(thread, "swapper", 0)) {
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001537 pr_err("problem inserting idle task.\n");
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001538 err = -1;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001539 }
1540
Hari Bathinif3b36142017-03-08 02:11:43 +05301541 if (thread == NULL || thread__set_namespaces(thread, 0, NULL)) {
1542 pr_err("problem inserting idle task.\n");
1543 err = -1;
1544 }
1545
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001546 /* machine__findnew_thread() got the thread, so put it */
1547 thread__put(thread);
1548 return err;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001549}
1550
Wang Nanf06149c2016-07-14 08:34:46 +00001551static void
1552perf_session__warn_order(const struct perf_session *session)
1553{
1554 const struct ordered_events *oe = &session->ordered_events;
1555 struct perf_evsel *evsel;
1556 bool should_warn = true;
1557
1558 evlist__for_each_entry(session->evlist, evsel) {
1559 if (evsel->attr.write_backward)
1560 should_warn = false;
1561 }
1562
1563 if (!should_warn)
1564 return;
1565 if (oe->nr_unordered_events != 0)
1566 ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events);
1567}
1568
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001569static void perf_session__warn_about_errors(const struct perf_session *session)
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001570{
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001571 const struct events_stats *stats = &session->evlist->stats;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001572
1573 if (session->tool->lost == perf_event__process_lost &&
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001574 stats->nr_events[PERF_RECORD_LOST] != 0) {
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001575 ui__warning("Processed %d events and lost %d chunks!\n\n"
1576 "Check IO/CPU overload!\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001577 stats->nr_events[0],
1578 stats->nr_events[PERF_RECORD_LOST]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001579 }
1580
Kan Liangc4937a92015-05-10 15:13:15 -04001581 if (session->tool->lost_samples == perf_event__process_lost_samples) {
1582 double drop_rate;
1583
1584 drop_rate = (double)stats->total_lost_samples /
1585 (double) (stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples);
1586 if (drop_rate > 0.05) {
Arnaldo Carvalho de Melo41a43da2018-04-05 14:34:09 -03001587 ui__warning("Processed %" PRIu64 " samples and lost %3.2f%%!\n\n",
Kan Liangc4937a92015-05-10 15:13:15 -04001588 stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples,
1589 drop_rate * 100.0);
1590 }
1591 }
1592
Adrian Huntera38f48e2015-09-25 16:15:37 +03001593 if (session->tool->aux == perf_event__process_aux &&
1594 stats->total_aux_lost != 0) {
1595 ui__warning("AUX data lost %" PRIu64 " times out of %u!\n\n",
1596 stats->total_aux_lost,
1597 stats->nr_events[PERF_RECORD_AUX]);
1598 }
1599
Alexander Shishkin05a1f472017-03-16 18:41:59 +02001600 if (session->tool->aux == perf_event__process_aux &&
1601 stats->total_aux_partial != 0) {
1602 bool vmm_exclusive = false;
1603
1604 (void)sysfs__read_bool("module/kvm_intel/parameters/vmm_exclusive",
1605 &vmm_exclusive);
1606
1607 ui__warning("AUX data had gaps in it %" PRIu64 " times out of %u!\n\n"
1608 "Are you running a KVM guest in the background?%s\n\n",
1609 stats->total_aux_partial,
1610 stats->nr_events[PERF_RECORD_AUX],
1611 vmm_exclusive ?
1612 "\nReloading kvm_intel module with vmm_exclusive=0\n"
1613 "will reduce the gaps to only guest's timeslices." :
1614 "");
1615 }
1616
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001617 if (stats->nr_unknown_events != 0) {
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001618 ui__warning("Found %u unknown events!\n\n"
1619 "Is this an older tool processing a perf.data "
1620 "file generated by a more recent tool?\n\n"
1621 "If that is not the case, consider "
1622 "reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001623 stats->nr_unknown_events);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001624 }
1625
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001626 if (stats->nr_unknown_id != 0) {
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001627 ui__warning("%u samples with id not present in the header\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001628 stats->nr_unknown_id);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001629 }
1630
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001631 if (stats->nr_invalid_chains != 0) {
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001632 ui__warning("Found invalid callchains!\n\n"
1633 "%u out of %u events were discarded for this reason.\n\n"
1634 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001635 stats->nr_invalid_chains,
1636 stats->nr_events[PERF_RECORD_SAMPLE]);
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001637 }
Joerg Roedel0c095712012-02-10 18:05:04 +01001638
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001639 if (stats->nr_unprocessable_samples != 0) {
Joerg Roedel0c095712012-02-10 18:05:04 +01001640 ui__warning("%u unprocessable samples recorded.\n"
1641 "Do you have a KVM guest running and not using 'perf kvm'?\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001642 stats->nr_unprocessable_samples);
Joerg Roedel0c095712012-02-10 18:05:04 +01001643 }
Jiri Olsaf61ff6c2014-11-26 16:39:31 +01001644
Wang Nanf06149c2016-07-14 08:34:46 +00001645 perf_session__warn_order(session);
Adrian Hunter85ed4722015-04-09 18:53:50 +03001646
1647 events_stats__auxtrace_error_warn(stats);
Kan Liang930e6fc2015-06-17 09:51:10 -04001648
1649 if (stats->nr_proc_map_timeout != 0) {
1650 ui__warning("%d map information files for pre-existing threads were\n"
1651 "not processed, if there are samples for addresses they\n"
1652 "will not be resolved, you may find out which are these\n"
1653 "threads by running with -v and redirecting the output\n"
Kan Liang9d9cad72015-06-17 09:51:11 -04001654 "to a file.\n"
1655 "The time limit to process proc map is too short?\n"
1656 "Increase it by --proc-map-timeout\n",
Kan Liang930e6fc2015-06-17 09:51:10 -04001657 stats->nr_proc_map_timeout);
1658 }
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001659}
1660
Adrian Huntera5499b32015-05-29 16:33:30 +03001661static int perf_session__flush_thread_stack(struct thread *thread,
1662 void *p __maybe_unused)
1663{
1664 return thread_stack__flush(thread);
1665}
1666
1667static int perf_session__flush_thread_stacks(struct perf_session *session)
1668{
1669 return machines__for_each_thread(&session->machines,
1670 perf_session__flush_thread_stack,
1671 NULL);
1672}
1673
Tom Zanussi8dc58102010-04-01 23:59:15 -05001674volatile int session_done;
1675
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001676static int __perf_session__process_pipe_events(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05001677{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001678 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001679 struct perf_tool *tool = session->tool;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001680 int fd = perf_data__fd(session->data);
Stephane Eranian444d2862012-05-15 13:28:12 +02001681 union perf_event *event;
1682 uint32_t size, cur_size = 0;
1683 void *buf = NULL;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001684 s64 skip = 0;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001685 u64 head;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001686 ssize_t err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001687 void *p;
1688
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001689 perf_tool__fill_defaults(tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001690
1691 head = 0;
Stephane Eranian444d2862012-05-15 13:28:12 +02001692 cur_size = sizeof(union perf_event);
1693
1694 buf = malloc(cur_size);
1695 if (!buf)
1696 return -errno;
David Carrillo-Cisneros1e0d4f02017-04-10 13:14:27 -07001697 ordered_events__set_copy_on_queue(oe, true);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001698more:
Stephane Eranian444d2862012-05-15 13:28:12 +02001699 event = buf;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001700 err = readn(fd, event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05001701 if (err <= 0) {
1702 if (err == 0)
1703 goto done;
1704
1705 pr_err("failed to read event header\n");
1706 goto out_err;
1707 }
1708
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001709 if (session->header.needs_swap)
Stephane Eranian444d2862012-05-15 13:28:12 +02001710 perf_event_header__bswap(&event->header);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001711
Stephane Eranian444d2862012-05-15 13:28:12 +02001712 size = event->header.size;
Adrian Hunter27389d72013-07-04 16:20:27 +03001713 if (size < sizeof(struct perf_event_header)) {
1714 pr_err("bad event header size\n");
1715 goto out_err;
1716 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001717
Stephane Eranian444d2862012-05-15 13:28:12 +02001718 if (size > cur_size) {
1719 void *new = realloc(buf, size);
1720 if (!new) {
1721 pr_err("failed to allocate memory to read event\n");
1722 goto out_err;
1723 }
1724 buf = new;
1725 cur_size = size;
1726 event = buf;
1727 }
1728 p = event;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001729 p += sizeof(struct perf_event_header);
1730
Tom Zanussi794e43b2010-05-05 00:27:40 -05001731 if (size - sizeof(struct perf_event_header)) {
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001732 err = readn(fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -05001733 if (err <= 0) {
1734 if (err == 0) {
1735 pr_err("unexpected end of event stream\n");
1736 goto done;
1737 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001738
Tom Zanussi794e43b2010-05-05 00:27:40 -05001739 pr_err("failed to read event data\n");
1740 goto out_err;
1741 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001742 }
1743
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001744 if ((skip = perf_session__process_event(session, event, head)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001745 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
Stephane Eranian444d2862012-05-15 13:28:12 +02001746 head, event->header.size, event->header.type);
Jiri Olsa9389a462012-04-16 20:42:51 +02001747 err = -EINVAL;
1748 goto out_err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001749 }
1750
1751 head += size;
1752
Tom Zanussi8dc58102010-04-01 23:59:15 -05001753 if (skip > 0)
1754 head += skip;
1755
1756 if (!session_done())
1757 goto more;
1758done:
Adrian Hunter8c16b642013-10-18 15:29:02 +03001759 /* do the final flush for ordered samples */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001760 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
Adrian Hunterc4468702015-04-09 18:53:48 +03001761 if (err)
1762 goto out_err;
1763 err = auxtrace__flush_events(session, tool);
Adrian Huntera5499b32015-05-29 16:33:30 +03001764 if (err)
1765 goto out_err;
1766 err = perf_session__flush_thread_stacks(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001767out_err:
Stephane Eranian444d2862012-05-15 13:28:12 +02001768 free(buf);
Jiri Olsa075ca1e2018-01-07 17:03:54 +01001769 if (!tool->no_warn)
1770 perf_session__warn_about_errors(session);
Jiri Olsaadc56ed2014-06-10 22:50:03 +02001771 ordered_events__free(&session->ordered_events);
Adrian Hunterc4468702015-04-09 18:53:48 +03001772 auxtrace__free_events(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001773 return err;
1774}
1775
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001776static union perf_event *
1777fetch_mmaped_event(struct perf_session *session,
1778 u64 head, size_t mmap_size, char *buf)
1779{
1780 union perf_event *event;
1781
1782 /*
1783 * Ensure we have enough space remaining to read
1784 * the size of the event in the headers.
1785 */
1786 if (head + sizeof(event->header) > mmap_size)
1787 return NULL;
1788
1789 event = (union perf_event *)(buf + head);
1790
1791 if (session->header.needs_swap)
1792 perf_event_header__bswap(&event->header);
1793
Adrian Hunter27389d72013-07-04 16:20:27 +03001794 if (head + event->header.size > mmap_size) {
1795 /* We're not fetching the event so swap back again */
1796 if (session->header.needs_swap)
1797 perf_event_header__bswap(&event->header);
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001798 return NULL;
Adrian Hunter27389d72013-07-04 16:20:27 +03001799 }
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001800
1801 return event;
1802}
1803
David Miller35d48dd2012-11-10 14:12:19 -05001804/*
1805 * On 64bit we can mmap the data file in one go. No need for tiny mmap
1806 * slices. On 32bit we use 32MB.
1807 */
1808#if BITS_PER_LONG == 64
1809#define MMAP_SIZE ULLONG_MAX
1810#define NUM_MMAPS 1
1811#else
1812#define MMAP_SIZE (32 * 1024 * 1024ULL)
1813#define NUM_MMAPS 128
1814#endif
1815
Namhyung Kim4ac30cf2015-01-29 17:06:43 +09001816static int __perf_session__process_events(struct perf_session *session,
1817 u64 data_offset, u64 data_size,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001818 u64 file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001819{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001820 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001821 struct perf_tool *tool = session->tool;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001822 int fd = perf_data__fd(session->data);
Adrian Hunterd5652d82014-07-23 22:19:58 +03001823 u64 head, page_offset, file_offset, file_pos, size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001824 int err, mmap_prot, mmap_flags, map_idx = 0;
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -03001825 size_t mmap_size;
David Miller35d48dd2012-11-10 14:12:19 -05001826 char *buf, *mmaps[NUM_MMAPS];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001827 union perf_event *event;
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001828 struct ui_progress prog;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001829 s64 skip;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001830
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001831 perf_tool__fill_defaults(tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001832
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001833 page_offset = page_size * (data_offset / page_size);
1834 file_offset = page_offset;
1835 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001836
Mark Rutland381c02f2015-09-16 18:18:49 +01001837 if (data_size == 0)
1838 goto out;
1839
1840 if (data_offset + data_size < file_size)
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001841 file_size = data_offset + data_size;
1842
Jiri Olsa82338222017-09-08 14:05:10 +02001843 ui_progress__init_size(&prog, file_size, "Processing events...");
Thomas Gleixner55b446292010-11-30 17:49:46 +00001844
David Miller35d48dd2012-11-10 14:12:19 -05001845 mmap_size = MMAP_SIZE;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001846 if (mmap_size > file_size) {
Thomas Gleixner55b446292010-11-30 17:49:46 +00001847 mmap_size = file_size;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001848 session->one_mmap = true;
1849 }
Thomas Gleixner55b446292010-11-30 17:49:46 +00001850
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001851 memset(mmaps, 0, sizeof(mmaps));
1852
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001853 mmap_prot = PROT_READ;
1854 mmap_flags = MAP_SHARED;
1855
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001856 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001857 mmap_prot |= PROT_WRITE;
1858 mmap_flags = MAP_PRIVATE;
1859 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001860remap:
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001861 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd,
Thomas Gleixner55b446292010-11-30 17:49:46 +00001862 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001863 if (buf == MAP_FAILED) {
1864 pr_err("failed to mmap file\n");
1865 err = -errno;
1866 goto out_err;
1867 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001868 mmaps[map_idx] = buf;
1869 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001870 file_pos = file_offset + head;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001871 if (session->one_mmap) {
1872 session->one_mmap_addr = buf;
1873 session->one_mmap_offset = file_offset;
1874 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001875
1876more:
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001877 event = fetch_mmaped_event(session, head, mmap_size, buf);
1878 if (!event) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001879 if (mmaps[map_idx]) {
1880 munmap(mmaps[map_idx], mmap_size);
1881 mmaps[map_idx] = NULL;
1882 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001883
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001884 page_offset = page_size * (head / page_size);
1885 file_offset += page_offset;
1886 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001887 goto remap;
1888 }
1889
1890 size = event->header.size;
1891
Adrian Hunter27389d72013-07-04 16:20:27 +03001892 if (size < sizeof(struct perf_event_header) ||
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001893 (skip = perf_session__process_event(session, event, file_pos)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001894 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1895 file_offset + head, event->header.size,
1896 event->header.type);
1897 err = -EINVAL;
1898 goto out_err;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001899 }
1900
Adrian Hunter6f917c72014-07-23 22:19:57 +03001901 if (skip)
1902 size += skip;
1903
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001904 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001905 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001906
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001907 ui_progress__update(&prog, size);
Thomas Gleixner55b446292010-11-30 17:49:46 +00001908
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001909 if (session_done())
Adrian Hunter8c16b642013-10-18 15:29:02 +03001910 goto out;
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001911
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001912 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001913 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001914
Adrian Hunter8c16b642013-10-18 15:29:02 +03001915out:
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001916 /* do the final flush for ordered samples */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001917 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
Adrian Hunterc4468702015-04-09 18:53:48 +03001918 if (err)
1919 goto out_err;
1920 err = auxtrace__flush_events(session, tool);
Adrian Huntera5499b32015-05-29 16:33:30 +03001921 if (err)
1922 goto out_err;
1923 err = perf_session__flush_thread_stacks(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001924out_err:
Namhyung Kima5580f32012-11-13 22:30:34 +09001925 ui_progress__finish();
Jiri Olsa075ca1e2018-01-07 17:03:54 +01001926 if (!tool->no_warn)
1927 perf_session__warn_about_errors(session);
Wang Nanb26dc732016-04-13 08:21:04 +00001928 /*
1929 * We may switching perf.data output, make ordered_events
1930 * reusable.
1931 */
1932 ordered_events__reinit(&session->ordered_events);
Adrian Hunterc4468702015-04-09 18:53:48 +03001933 auxtrace__free_events(session);
Adrian Hunter919d86d2014-07-14 13:02:51 +03001934 session->one_mmap = false;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001935 return err;
1936}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001937
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001938int perf_session__process_events(struct perf_session *session)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001939{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001940 u64 size = perf_data__size(session->data);
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001941 int err;
1942
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001943 if (perf_session__register_idle_thread(session) < 0)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001944 return -ENOMEM;
1945
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001946 if (!perf_data__is_pipe(session->data))
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001947 err = __perf_session__process_events(session,
1948 session->header.data_offset,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001949 session->header.data_size, size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001950 else
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001951 err = __perf_session__process_pipe_events(session);
Dave Martin88ca8952010-07-27 11:46:12 -03001952
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001953 return err;
1954}
1955
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001956bool perf_session__has_traces(struct perf_session *session, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001957{
David Ahern93ea01c292013-08-07 22:50:58 -04001958 struct perf_evsel *evsel;
1959
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001960 evlist__for_each_entry(session->evlist, evsel) {
David Ahern93ea01c292013-08-07 22:50:58 -04001961 if (evsel->attr.type == PERF_TYPE_TRACEPOINT)
1962 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001963 }
1964
David Ahern93ea01c292013-08-07 22:50:58 -04001965 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1966 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001967}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001968
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001969int map__set_kallsyms_ref_reloc_sym(struct map *map, const char *symbol_name, u64 addr)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001970{
1971 char *bracket;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001972 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001973 struct kmap *kmap;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001974
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001975 ref = zalloc(sizeof(struct ref_reloc_sym));
1976 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001977 return -ENOMEM;
1978
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001979 ref->name = strdup(symbol_name);
1980 if (ref->name == NULL) {
1981 free(ref);
1982 return -ENOMEM;
1983 }
1984
1985 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001986 if (bracket)
1987 *bracket = '\0';
1988
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001989 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001990
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001991 kmap = map__kmap(map);
1992 if (kmap)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001993 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001994
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001995 return 0;
1996}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001997
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001998size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001999{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03002000 return machines__fprintf_dsos(&session->machines, fp);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03002001}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03002002
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03002003size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03002004 bool (skip)(struct dso *dso, int parm), int parm)
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03002005{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03002006 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03002007}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03002008
2009size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
2010{
Adrian Hunterc4468702015-04-09 18:53:48 +03002011 size_t ret;
2012 const char *msg = "";
2013
2014 if (perf_header__has_feat(&session->header, HEADER_AUXTRACE))
2015 msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)";
2016
Adrian Hunterfe692ac2015-06-23 10:52:49 +03002017 ret = fprintf(fp, "\nAggregated stats:%s\n", msg);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03002018
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03002019 ret += events_stats__fprintf(&session->evlist->stats, fp);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03002020 return ret;
2021}
David Ahernc0230b22011-03-09 22:23:27 -07002022
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02002023size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
2024{
2025 /*
2026 * FIXME: Here we have to actually print all the machines in this
2027 * session, not just the host...
2028 */
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03002029 return machine__fprintf(&session->machines.host, fp);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02002030}
2031
David Ahern9cbdb702011-04-06 21:54:20 -06002032struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
2033 unsigned int type)
2034{
2035 struct perf_evsel *pos;
2036
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002037 evlist__for_each_entry(session->evlist, pos) {
David Ahern9cbdb702011-04-06 21:54:20 -06002038 if (pos->attr.type == type)
2039 return pos;
2040 }
2041 return NULL;
2042}
2043
Anton Blanchard5d67be92011-07-04 21:57:50 +10002044int perf_session__cpu_bitmap(struct perf_session *session,
2045 const char *cpu_list, unsigned long *cpu_bitmap)
2046{
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04002047 int i, err = -1;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002048 struct cpu_map *map;
2049
2050 for (i = 0; i < PERF_TYPE_MAX; ++i) {
2051 struct perf_evsel *evsel;
2052
2053 evsel = perf_session__find_first_evtype(session, i);
2054 if (!evsel)
2055 continue;
2056
2057 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
2058 pr_err("File does not contain CPU events. "
Adrian Hunter30795462017-05-26 11:17:19 +03002059 "Remove -C option to proceed.\n");
Anton Blanchard5d67be92011-07-04 21:57:50 +10002060 return -1;
2061 }
2062 }
2063
2064 map = cpu_map__new(cpu_list);
David Ahern47fbe532011-11-13 10:45:27 -07002065 if (map == NULL) {
2066 pr_err("Invalid cpu_list\n");
2067 return -1;
2068 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10002069
2070 for (i = 0; i < map->nr; i++) {
2071 int cpu = map->map[i];
2072
2073 if (cpu >= MAX_NR_CPUS) {
2074 pr_err("Requested CPU %d too large. "
2075 "Consider raising MAX_NR_CPUS\n", cpu);
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04002076 goto out_delete_map;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002077 }
2078
2079 set_bit(cpu, cpu_bitmap);
2080 }
2081
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04002082 err = 0;
2083
2084out_delete_map:
Jiri Olsaf30a79b2015-06-23 00:36:04 +02002085 cpu_map__put(map);
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04002086 return err;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002087}
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002088
2089void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
2090 bool full)
2091{
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002092 if (session == NULL || fp == NULL)
2093 return;
2094
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002095 fprintf(fp, "# ========\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002096 perf_header__fprintf_info(session, fp, full);
2097 fprintf(fp, "# ========\n#\n");
2098}
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002099
2100
2101int __perf_session__set_tracepoints_handlers(struct perf_session *session,
2102 const struct perf_evsel_str_handler *assocs,
2103 size_t nr_assocs)
2104{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002105 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002106 size_t i;
2107 int err;
2108
2109 for (i = 0; i < nr_assocs; i++) {
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03002110 /*
2111 * Adding a handler for an event not in the session,
2112 * just ignore it.
2113 */
2114 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name);
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002115 if (evsel == NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03002116 continue;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002117
2118 err = -EEXIST;
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002119 if (evsel->handler != NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03002120 goto out;
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002121 evsel->handler = assocs[i].handler;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002122 }
2123
2124 err = 0;
2125out:
2126 return err;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002127}
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002128
Jiri Olsa89f16882018-09-13 14:54:03 +02002129int perf_event__process_id_index(struct perf_session *session,
2130 union perf_event *event)
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002131{
2132 struct perf_evlist *evlist = session->evlist;
2133 struct id_index_event *ie = &event->id_index;
2134 size_t i, nr, max_nr;
2135
2136 max_nr = (ie->header.size - sizeof(struct id_index_event)) /
2137 sizeof(struct id_index_entry);
2138 nr = ie->nr;
2139 if (nr > max_nr)
2140 return -EINVAL;
2141
2142 if (dump_trace)
2143 fprintf(stdout, " nr: %zu\n", nr);
2144
2145 for (i = 0; i < nr; i++) {
2146 struct id_index_entry *e = &ie->entries[i];
2147 struct perf_sample_id *sid;
2148
2149 if (dump_trace) {
2150 fprintf(stdout, " ... id: %"PRIu64, e->id);
2151 fprintf(stdout, " idx: %"PRIu64, e->idx);
2152 fprintf(stdout, " cpu: %"PRId64, e->cpu);
2153 fprintf(stdout, " tid: %"PRId64"\n", e->tid);
2154 }
2155
2156 sid = perf_evlist__id2sid(evlist, e->id);
2157 if (!sid)
2158 return -ENOENT;
2159 sid->idx = e->idx;
2160 sid->cpu = e->cpu;
2161 sid->tid = e->tid;
2162 }
2163 return 0;
2164}
2165
2166int perf_event__synthesize_id_index(struct perf_tool *tool,
2167 perf_event__handler_t process,
2168 struct perf_evlist *evlist,
2169 struct machine *machine)
2170{
2171 union perf_event *ev;
2172 struct perf_evsel *evsel;
2173 size_t nr = 0, i = 0, sz, max_nr, n;
2174 int err;
2175
2176 pr_debug2("Synthesizing id index\n");
2177
2178 max_nr = (UINT16_MAX - sizeof(struct id_index_event)) /
2179 sizeof(struct id_index_entry);
2180
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002181 evlist__for_each_entry(evlist, evsel)
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002182 nr += evsel->ids;
2183
2184 n = nr > max_nr ? max_nr : nr;
2185 sz = sizeof(struct id_index_event) + n * sizeof(struct id_index_entry);
2186 ev = zalloc(sz);
2187 if (!ev)
2188 return -ENOMEM;
2189
2190 ev->id_index.header.type = PERF_RECORD_ID_INDEX;
2191 ev->id_index.header.size = sz;
2192 ev->id_index.nr = n;
2193
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002194 evlist__for_each_entry(evlist, evsel) {
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002195 u32 j;
2196
2197 for (j = 0; j < evsel->ids; j++) {
2198 struct id_index_entry *e;
2199 struct perf_sample_id *sid;
2200
2201 if (i >= n) {
2202 err = process(tool, ev, NULL, machine);
2203 if (err)
2204 goto out_err;
2205 nr -= n;
2206 i = 0;
2207 }
2208
2209 e = &ev->id_index.entries[i++];
2210
2211 e->id = evsel->id[j];
2212
2213 sid = perf_evlist__id2sid(evlist, e->id);
2214 if (!sid) {
2215 free(ev);
2216 return -ENOENT;
2217 }
2218
2219 e->idx = sid->idx;
2220 e->cpu = sid->cpu;
2221 e->tid = sid->tid;
2222 }
2223 }
2224
2225 sz = sizeof(struct id_index_event) + nr * sizeof(struct id_index_entry);
2226 ev->id_index.header.size = sz;
2227 ev->id_index.nr = nr;
2228
2229 err = process(tool, ev, NULL, machine);
2230out_err:
2231 free(ev);
2232
2233 return err;
2234}