blob: 173cf445475c3bdaa0071f0bf1b19f1b3cc19d1f [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
Adrian Hunterb25756d2018-12-21 14:06:20 +02001530/*
1531 * Threads are identified by pid and tid, and the idle task has pid == tid == 0.
1532 * So here a single thread is created for that, but actually there is a separate
1533 * idle task per cpu, so there should be one 'struct thread' per cpu, but there
1534 * is only 1. That causes problems for some tools, requiring workarounds. For
1535 * example get_idle_thread() in builtin-sched.c, or thread_stack__per_cpu().
1536 */
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001537int perf_session__register_idle_thread(struct perf_session *session)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001538{
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001539 struct thread *thread;
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001540 int err = 0;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001541
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001542 thread = machine__findnew_thread(&session->machines.host, 0, 0);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001543 if (thread == NULL || thread__set_comm(thread, "swapper", 0)) {
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001544 pr_err("problem inserting idle task.\n");
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001545 err = -1;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001546 }
1547
Hari Bathinif3b36142017-03-08 02:11:43 +05301548 if (thread == NULL || thread__set_namespaces(thread, 0, NULL)) {
1549 pr_err("problem inserting idle task.\n");
1550 err = -1;
1551 }
1552
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001553 /* machine__findnew_thread() got the thread, so put it */
1554 thread__put(thread);
1555 return err;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001556}
1557
Wang Nanf06149c2016-07-14 08:34:46 +00001558static void
1559perf_session__warn_order(const struct perf_session *session)
1560{
1561 const struct ordered_events *oe = &session->ordered_events;
1562 struct perf_evsel *evsel;
1563 bool should_warn = true;
1564
1565 evlist__for_each_entry(session->evlist, evsel) {
1566 if (evsel->attr.write_backward)
1567 should_warn = false;
1568 }
1569
1570 if (!should_warn)
1571 return;
1572 if (oe->nr_unordered_events != 0)
1573 ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events);
1574}
1575
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001576static void perf_session__warn_about_errors(const struct perf_session *session)
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001577{
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001578 const struct events_stats *stats = &session->evlist->stats;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001579
1580 if (session->tool->lost == perf_event__process_lost &&
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001581 stats->nr_events[PERF_RECORD_LOST] != 0) {
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001582 ui__warning("Processed %d events and lost %d chunks!\n\n"
1583 "Check IO/CPU overload!\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001584 stats->nr_events[0],
1585 stats->nr_events[PERF_RECORD_LOST]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001586 }
1587
Kan Liangc4937a92015-05-10 15:13:15 -04001588 if (session->tool->lost_samples == perf_event__process_lost_samples) {
1589 double drop_rate;
1590
1591 drop_rate = (double)stats->total_lost_samples /
1592 (double) (stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples);
1593 if (drop_rate > 0.05) {
Arnaldo Carvalho de Melo41a43da2018-04-05 14:34:09 -03001594 ui__warning("Processed %" PRIu64 " samples and lost %3.2f%%!\n\n",
Kan Liangc4937a92015-05-10 15:13:15 -04001595 stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples,
1596 drop_rate * 100.0);
1597 }
1598 }
1599
Adrian Huntera38f48e2015-09-25 16:15:37 +03001600 if (session->tool->aux == perf_event__process_aux &&
1601 stats->total_aux_lost != 0) {
1602 ui__warning("AUX data lost %" PRIu64 " times out of %u!\n\n",
1603 stats->total_aux_lost,
1604 stats->nr_events[PERF_RECORD_AUX]);
1605 }
1606
Alexander Shishkin05a1f472017-03-16 18:41:59 +02001607 if (session->tool->aux == perf_event__process_aux &&
1608 stats->total_aux_partial != 0) {
1609 bool vmm_exclusive = false;
1610
1611 (void)sysfs__read_bool("module/kvm_intel/parameters/vmm_exclusive",
1612 &vmm_exclusive);
1613
1614 ui__warning("AUX data had gaps in it %" PRIu64 " times out of %u!\n\n"
1615 "Are you running a KVM guest in the background?%s\n\n",
1616 stats->total_aux_partial,
1617 stats->nr_events[PERF_RECORD_AUX],
1618 vmm_exclusive ?
1619 "\nReloading kvm_intel module with vmm_exclusive=0\n"
1620 "will reduce the gaps to only guest's timeslices." :
1621 "");
1622 }
1623
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001624 if (stats->nr_unknown_events != 0) {
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001625 ui__warning("Found %u unknown events!\n\n"
1626 "Is this an older tool processing a perf.data "
1627 "file generated by a more recent tool?\n\n"
1628 "If that is not the case, consider "
1629 "reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001630 stats->nr_unknown_events);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001631 }
1632
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001633 if (stats->nr_unknown_id != 0) {
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001634 ui__warning("%u samples with id not present in the header\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001635 stats->nr_unknown_id);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001636 }
1637
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001638 if (stats->nr_invalid_chains != 0) {
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001639 ui__warning("Found invalid callchains!\n\n"
1640 "%u out of %u events were discarded for this reason.\n\n"
1641 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001642 stats->nr_invalid_chains,
1643 stats->nr_events[PERF_RECORD_SAMPLE]);
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001644 }
Joerg Roedel0c095712012-02-10 18:05:04 +01001645
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001646 if (stats->nr_unprocessable_samples != 0) {
Joerg Roedel0c095712012-02-10 18:05:04 +01001647 ui__warning("%u unprocessable samples recorded.\n"
1648 "Do you have a KVM guest running and not using 'perf kvm'?\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001649 stats->nr_unprocessable_samples);
Joerg Roedel0c095712012-02-10 18:05:04 +01001650 }
Jiri Olsaf61ff6c2014-11-26 16:39:31 +01001651
Wang Nanf06149c2016-07-14 08:34:46 +00001652 perf_session__warn_order(session);
Adrian Hunter85ed4722015-04-09 18:53:50 +03001653
1654 events_stats__auxtrace_error_warn(stats);
Kan Liang930e6fc2015-06-17 09:51:10 -04001655
1656 if (stats->nr_proc_map_timeout != 0) {
1657 ui__warning("%d map information files for pre-existing threads were\n"
1658 "not processed, if there are samples for addresses they\n"
1659 "will not be resolved, you may find out which are these\n"
1660 "threads by running with -v and redirecting the output\n"
Kan Liang9d9cad72015-06-17 09:51:11 -04001661 "to a file.\n"
1662 "The time limit to process proc map is too short?\n"
1663 "Increase it by --proc-map-timeout\n",
Kan Liang930e6fc2015-06-17 09:51:10 -04001664 stats->nr_proc_map_timeout);
1665 }
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001666}
1667
Adrian Huntera5499b32015-05-29 16:33:30 +03001668static int perf_session__flush_thread_stack(struct thread *thread,
1669 void *p __maybe_unused)
1670{
1671 return thread_stack__flush(thread);
1672}
1673
1674static int perf_session__flush_thread_stacks(struct perf_session *session)
1675{
1676 return machines__for_each_thread(&session->machines,
1677 perf_session__flush_thread_stack,
1678 NULL);
1679}
1680
Tom Zanussi8dc58102010-04-01 23:59:15 -05001681volatile int session_done;
1682
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001683static int __perf_session__process_pipe_events(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05001684{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001685 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001686 struct perf_tool *tool = session->tool;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001687 int fd = perf_data__fd(session->data);
Stephane Eranian444d2862012-05-15 13:28:12 +02001688 union perf_event *event;
1689 uint32_t size, cur_size = 0;
1690 void *buf = NULL;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001691 s64 skip = 0;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001692 u64 head;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001693 ssize_t err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001694 void *p;
1695
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001696 perf_tool__fill_defaults(tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001697
1698 head = 0;
Stephane Eranian444d2862012-05-15 13:28:12 +02001699 cur_size = sizeof(union perf_event);
1700
1701 buf = malloc(cur_size);
1702 if (!buf)
1703 return -errno;
David Carrillo-Cisneros1e0d4f02017-04-10 13:14:27 -07001704 ordered_events__set_copy_on_queue(oe, true);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001705more:
Stephane Eranian444d2862012-05-15 13:28:12 +02001706 event = buf;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001707 err = readn(fd, event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05001708 if (err <= 0) {
1709 if (err == 0)
1710 goto done;
1711
1712 pr_err("failed to read event header\n");
1713 goto out_err;
1714 }
1715
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001716 if (session->header.needs_swap)
Stephane Eranian444d2862012-05-15 13:28:12 +02001717 perf_event_header__bswap(&event->header);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001718
Stephane Eranian444d2862012-05-15 13:28:12 +02001719 size = event->header.size;
Adrian Hunter27389d72013-07-04 16:20:27 +03001720 if (size < sizeof(struct perf_event_header)) {
1721 pr_err("bad event header size\n");
1722 goto out_err;
1723 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001724
Stephane Eranian444d2862012-05-15 13:28:12 +02001725 if (size > cur_size) {
1726 void *new = realloc(buf, size);
1727 if (!new) {
1728 pr_err("failed to allocate memory to read event\n");
1729 goto out_err;
1730 }
1731 buf = new;
1732 cur_size = size;
1733 event = buf;
1734 }
1735 p = event;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001736 p += sizeof(struct perf_event_header);
1737
Tom Zanussi794e43b2010-05-05 00:27:40 -05001738 if (size - sizeof(struct perf_event_header)) {
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001739 err = readn(fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -05001740 if (err <= 0) {
1741 if (err == 0) {
1742 pr_err("unexpected end of event stream\n");
1743 goto done;
1744 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001745
Tom Zanussi794e43b2010-05-05 00:27:40 -05001746 pr_err("failed to read event data\n");
1747 goto out_err;
1748 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001749 }
1750
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001751 if ((skip = perf_session__process_event(session, event, head)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001752 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
Stephane Eranian444d2862012-05-15 13:28:12 +02001753 head, event->header.size, event->header.type);
Jiri Olsa9389a462012-04-16 20:42:51 +02001754 err = -EINVAL;
1755 goto out_err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001756 }
1757
1758 head += size;
1759
Tom Zanussi8dc58102010-04-01 23:59:15 -05001760 if (skip > 0)
1761 head += skip;
1762
1763 if (!session_done())
1764 goto more;
1765done:
Adrian Hunter8c16b642013-10-18 15:29:02 +03001766 /* do the final flush for ordered samples */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001767 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
Adrian Hunterc4468702015-04-09 18:53:48 +03001768 if (err)
1769 goto out_err;
1770 err = auxtrace__flush_events(session, tool);
Adrian Huntera5499b32015-05-29 16:33:30 +03001771 if (err)
1772 goto out_err;
1773 err = perf_session__flush_thread_stacks(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001774out_err:
Stephane Eranian444d2862012-05-15 13:28:12 +02001775 free(buf);
Jiri Olsa075ca1e2018-01-07 17:03:54 +01001776 if (!tool->no_warn)
1777 perf_session__warn_about_errors(session);
Jiri Olsaadc56ed2014-06-10 22:50:03 +02001778 ordered_events__free(&session->ordered_events);
Adrian Hunterc4468702015-04-09 18:53:48 +03001779 auxtrace__free_events(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001780 return err;
1781}
1782
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001783static union perf_event *
1784fetch_mmaped_event(struct perf_session *session,
1785 u64 head, size_t mmap_size, char *buf)
1786{
1787 union perf_event *event;
1788
1789 /*
1790 * Ensure we have enough space remaining to read
1791 * the size of the event in the headers.
1792 */
1793 if (head + sizeof(event->header) > mmap_size)
1794 return NULL;
1795
1796 event = (union perf_event *)(buf + head);
1797
1798 if (session->header.needs_swap)
1799 perf_event_header__bswap(&event->header);
1800
Adrian Hunter27389d72013-07-04 16:20:27 +03001801 if (head + event->header.size > mmap_size) {
1802 /* We're not fetching the event so swap back again */
1803 if (session->header.needs_swap)
1804 perf_event_header__bswap(&event->header);
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001805 return NULL;
Adrian Hunter27389d72013-07-04 16:20:27 +03001806 }
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001807
1808 return event;
1809}
1810
David Miller35d48dd2012-11-10 14:12:19 -05001811/*
1812 * On 64bit we can mmap the data file in one go. No need for tiny mmap
1813 * slices. On 32bit we use 32MB.
1814 */
1815#if BITS_PER_LONG == 64
1816#define MMAP_SIZE ULLONG_MAX
1817#define NUM_MMAPS 1
1818#else
1819#define MMAP_SIZE (32 * 1024 * 1024ULL)
1820#define NUM_MMAPS 128
1821#endif
1822
Jiri Olsa82715eb2019-01-10 11:12:58 +01001823struct reader {
1824 int fd;
Jiri Olsaf66f0952019-01-10 11:12:59 +01001825 u64 data_size;
Jiri Olsa71002bd2019-01-10 11:13:00 +01001826 u64 data_offset;
Jiri Olsa82715eb2019-01-10 11:12:58 +01001827};
1828
Jiri Olsa7ba4da12019-01-10 11:12:56 +01001829static int __perf_session__process_events(struct perf_session *session)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001830{
Jiri Olsa82715eb2019-01-10 11:12:58 +01001831 struct reader rd = {
1832 .fd = perf_data__fd(session->data),
Jiri Olsaf66f0952019-01-10 11:12:59 +01001833 .data_size = session->header.data_size,
Jiri Olsa71002bd2019-01-10 11:13:00 +01001834 .data_offset = session->header.data_offset,
Jiri Olsa82715eb2019-01-10 11:12:58 +01001835 };
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001836 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001837 struct perf_tool *tool = session->tool;
Jiri Olsaf66f0952019-01-10 11:12:59 +01001838 u64 data_size = rd.data_size;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001839 u64 head, page_offset, file_offset, file_pos, size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001840 int err, mmap_prot, mmap_flags, map_idx = 0;
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -03001841 size_t mmap_size;
David Miller35d48dd2012-11-10 14:12:19 -05001842 char *buf, *mmaps[NUM_MMAPS];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001843 union perf_event *event;
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001844 struct ui_progress prog;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001845 s64 skip;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001846
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001847 perf_tool__fill_defaults(tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001848
Jiri Olsa71002bd2019-01-10 11:13:00 +01001849 page_offset = page_size * (rd.data_offset / page_size);
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001850 file_offset = page_offset;
Jiri Olsa71002bd2019-01-10 11:13:00 +01001851 head = rd.data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001852
Mark Rutland381c02f2015-09-16 18:18:49 +01001853 if (data_size == 0)
1854 goto out;
1855
Jiri Olsa4f5a4732019-01-10 11:12:57 +01001856 ui_progress__init_size(&prog, data_size, "Processing events...");
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001857
Jiri Olsa71002bd2019-01-10 11:13:00 +01001858 data_size += rd.data_offset;
Thomas Gleixner55b446292010-11-30 17:49:46 +00001859
David Miller35d48dd2012-11-10 14:12:19 -05001860 mmap_size = MMAP_SIZE;
Jiri Olsa4f5a4732019-01-10 11:12:57 +01001861 if (mmap_size > data_size) {
1862 mmap_size = data_size;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001863 session->one_mmap = true;
1864 }
Thomas Gleixner55b446292010-11-30 17:49:46 +00001865
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001866 memset(mmaps, 0, sizeof(mmaps));
1867
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001868 mmap_prot = PROT_READ;
1869 mmap_flags = MAP_SHARED;
1870
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001871 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001872 mmap_prot |= PROT_WRITE;
1873 mmap_flags = MAP_PRIVATE;
1874 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001875remap:
Jiri Olsa82715eb2019-01-10 11:12:58 +01001876 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, rd.fd,
Thomas Gleixner55b446292010-11-30 17:49:46 +00001877 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001878 if (buf == MAP_FAILED) {
1879 pr_err("failed to mmap file\n");
1880 err = -errno;
1881 goto out_err;
1882 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001883 mmaps[map_idx] = buf;
1884 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001885 file_pos = file_offset + head;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001886 if (session->one_mmap) {
1887 session->one_mmap_addr = buf;
1888 session->one_mmap_offset = file_offset;
1889 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001890
1891more:
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001892 event = fetch_mmaped_event(session, head, mmap_size, buf);
1893 if (!event) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001894 if (mmaps[map_idx]) {
1895 munmap(mmaps[map_idx], mmap_size);
1896 mmaps[map_idx] = NULL;
1897 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001898
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001899 page_offset = page_size * (head / page_size);
1900 file_offset += page_offset;
1901 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001902 goto remap;
1903 }
1904
1905 size = event->header.size;
1906
Adrian Hunter27389d72013-07-04 16:20:27 +03001907 if (size < sizeof(struct perf_event_header) ||
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001908 (skip = perf_session__process_event(session, event, file_pos)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001909 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1910 file_offset + head, event->header.size,
1911 event->header.type);
1912 err = -EINVAL;
1913 goto out_err;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001914 }
1915
Adrian Hunter6f917c72014-07-23 22:19:57 +03001916 if (skip)
1917 size += skip;
1918
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001919 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001920 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001921
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001922 ui_progress__update(&prog, size);
Thomas Gleixner55b446292010-11-30 17:49:46 +00001923
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001924 if (session_done())
Adrian Hunter8c16b642013-10-18 15:29:02 +03001925 goto out;
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001926
Jiri Olsa4f5a4732019-01-10 11:12:57 +01001927 if (file_pos < data_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001928 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001929
Adrian Hunter8c16b642013-10-18 15:29:02 +03001930out:
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001931 /* do the final flush for ordered samples */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001932 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
Adrian Hunterc4468702015-04-09 18:53:48 +03001933 if (err)
1934 goto out_err;
1935 err = auxtrace__flush_events(session, tool);
Adrian Huntera5499b32015-05-29 16:33:30 +03001936 if (err)
1937 goto out_err;
1938 err = perf_session__flush_thread_stacks(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001939out_err:
Namhyung Kima5580f32012-11-13 22:30:34 +09001940 ui_progress__finish();
Jiri Olsa075ca1e2018-01-07 17:03:54 +01001941 if (!tool->no_warn)
1942 perf_session__warn_about_errors(session);
Wang Nanb26dc732016-04-13 08:21:04 +00001943 /*
1944 * We may switching perf.data output, make ordered_events
1945 * reusable.
1946 */
1947 ordered_events__reinit(&session->ordered_events);
Adrian Hunterc4468702015-04-09 18:53:48 +03001948 auxtrace__free_events(session);
Adrian Hunter919d86d2014-07-14 13:02:51 +03001949 session->one_mmap = false;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001950 return err;
1951}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001952
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001953int perf_session__process_events(struct perf_session *session)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001954{
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001955 if (perf_session__register_idle_thread(session) < 0)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001956 return -ENOMEM;
1957
Jiri Olsa7ba4da12019-01-10 11:12:56 +01001958 if (perf_data__is_pipe(session->data))
1959 return __perf_session__process_pipe_events(session);
Dave Martin88ca8952010-07-27 11:46:12 -03001960
Jiri Olsa7ba4da12019-01-10 11:12:56 +01001961 return __perf_session__process_events(session);
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001962}
1963
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001964bool perf_session__has_traces(struct perf_session *session, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001965{
David Ahern93ea01c292013-08-07 22:50:58 -04001966 struct perf_evsel *evsel;
1967
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001968 evlist__for_each_entry(session->evlist, evsel) {
David Ahern93ea01c292013-08-07 22:50:58 -04001969 if (evsel->attr.type == PERF_TYPE_TRACEPOINT)
1970 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001971 }
1972
David Ahern93ea01c292013-08-07 22:50:58 -04001973 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1974 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001975}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001976
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001977int map__set_kallsyms_ref_reloc_sym(struct map *map, const char *symbol_name, u64 addr)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001978{
1979 char *bracket;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001980 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001981 struct kmap *kmap;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001982
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001983 ref = zalloc(sizeof(struct ref_reloc_sym));
1984 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001985 return -ENOMEM;
1986
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001987 ref->name = strdup(symbol_name);
1988 if (ref->name == NULL) {
1989 free(ref);
1990 return -ENOMEM;
1991 }
1992
1993 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001994 if (bracket)
1995 *bracket = '\0';
1996
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001997 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001998
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001999 kmap = map__kmap(map);
2000 if (kmap)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002001 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02002002
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02002003 return 0;
2004}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03002005
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03002006size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03002007{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03002008 return machines__fprintf_dsos(&session->machines, fp);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03002009}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03002010
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03002011size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03002012 bool (skip)(struct dso *dso, int parm), int parm)
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03002013{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03002014 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03002015}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03002016
2017size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
2018{
Adrian Hunterc4468702015-04-09 18:53:48 +03002019 size_t ret;
2020 const char *msg = "";
2021
2022 if (perf_header__has_feat(&session->header, HEADER_AUXTRACE))
2023 msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)";
2024
Adrian Hunterfe692ac2015-06-23 10:52:49 +03002025 ret = fprintf(fp, "\nAggregated stats:%s\n", msg);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03002026
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03002027 ret += events_stats__fprintf(&session->evlist->stats, fp);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03002028 return ret;
2029}
David Ahernc0230b22011-03-09 22:23:27 -07002030
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02002031size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
2032{
2033 /*
2034 * FIXME: Here we have to actually print all the machines in this
2035 * session, not just the host...
2036 */
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03002037 return machine__fprintf(&session->machines.host, fp);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02002038}
2039
David Ahern9cbdb702011-04-06 21:54:20 -06002040struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
2041 unsigned int type)
2042{
2043 struct perf_evsel *pos;
2044
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002045 evlist__for_each_entry(session->evlist, pos) {
David Ahern9cbdb702011-04-06 21:54:20 -06002046 if (pos->attr.type == type)
2047 return pos;
2048 }
2049 return NULL;
2050}
2051
Anton Blanchard5d67be92011-07-04 21:57:50 +10002052int perf_session__cpu_bitmap(struct perf_session *session,
2053 const char *cpu_list, unsigned long *cpu_bitmap)
2054{
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04002055 int i, err = -1;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002056 struct cpu_map *map;
2057
2058 for (i = 0; i < PERF_TYPE_MAX; ++i) {
2059 struct perf_evsel *evsel;
2060
2061 evsel = perf_session__find_first_evtype(session, i);
2062 if (!evsel)
2063 continue;
2064
2065 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
2066 pr_err("File does not contain CPU events. "
Adrian Hunter30795462017-05-26 11:17:19 +03002067 "Remove -C option to proceed.\n");
Anton Blanchard5d67be92011-07-04 21:57:50 +10002068 return -1;
2069 }
2070 }
2071
2072 map = cpu_map__new(cpu_list);
David Ahern47fbe532011-11-13 10:45:27 -07002073 if (map == NULL) {
2074 pr_err("Invalid cpu_list\n");
2075 return -1;
2076 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10002077
2078 for (i = 0; i < map->nr; i++) {
2079 int cpu = map->map[i];
2080
2081 if (cpu >= MAX_NR_CPUS) {
2082 pr_err("Requested CPU %d too large. "
2083 "Consider raising MAX_NR_CPUS\n", cpu);
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04002084 goto out_delete_map;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002085 }
2086
2087 set_bit(cpu, cpu_bitmap);
2088 }
2089
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04002090 err = 0;
2091
2092out_delete_map:
Jiri Olsaf30a79b2015-06-23 00:36:04 +02002093 cpu_map__put(map);
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04002094 return err;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002095}
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002096
2097void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
2098 bool full)
2099{
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002100 if (session == NULL || fp == NULL)
2101 return;
2102
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002103 fprintf(fp, "# ========\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002104 perf_header__fprintf_info(session, fp, full);
2105 fprintf(fp, "# ========\n#\n");
2106}
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002107
2108
2109int __perf_session__set_tracepoints_handlers(struct perf_session *session,
2110 const struct perf_evsel_str_handler *assocs,
2111 size_t nr_assocs)
2112{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002113 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002114 size_t i;
2115 int err;
2116
2117 for (i = 0; i < nr_assocs; i++) {
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03002118 /*
2119 * Adding a handler for an event not in the session,
2120 * just ignore it.
2121 */
2122 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name);
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002123 if (evsel == NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03002124 continue;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002125
2126 err = -EEXIST;
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002127 if (evsel->handler != NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03002128 goto out;
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002129 evsel->handler = assocs[i].handler;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002130 }
2131
2132 err = 0;
2133out:
2134 return err;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002135}
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002136
Jiri Olsa89f16882018-09-13 14:54:03 +02002137int perf_event__process_id_index(struct perf_session *session,
2138 union perf_event *event)
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002139{
2140 struct perf_evlist *evlist = session->evlist;
2141 struct id_index_event *ie = &event->id_index;
2142 size_t i, nr, max_nr;
2143
2144 max_nr = (ie->header.size - sizeof(struct id_index_event)) /
2145 sizeof(struct id_index_entry);
2146 nr = ie->nr;
2147 if (nr > max_nr)
2148 return -EINVAL;
2149
2150 if (dump_trace)
2151 fprintf(stdout, " nr: %zu\n", nr);
2152
2153 for (i = 0; i < nr; i++) {
2154 struct id_index_entry *e = &ie->entries[i];
2155 struct perf_sample_id *sid;
2156
2157 if (dump_trace) {
2158 fprintf(stdout, " ... id: %"PRIu64, e->id);
2159 fprintf(stdout, " idx: %"PRIu64, e->idx);
2160 fprintf(stdout, " cpu: %"PRId64, e->cpu);
2161 fprintf(stdout, " tid: %"PRId64"\n", e->tid);
2162 }
2163
2164 sid = perf_evlist__id2sid(evlist, e->id);
2165 if (!sid)
2166 return -ENOENT;
2167 sid->idx = e->idx;
2168 sid->cpu = e->cpu;
2169 sid->tid = e->tid;
2170 }
2171 return 0;
2172}
2173
2174int perf_event__synthesize_id_index(struct perf_tool *tool,
2175 perf_event__handler_t process,
2176 struct perf_evlist *evlist,
2177 struct machine *machine)
2178{
2179 union perf_event *ev;
2180 struct perf_evsel *evsel;
2181 size_t nr = 0, i = 0, sz, max_nr, n;
2182 int err;
2183
2184 pr_debug2("Synthesizing id index\n");
2185
2186 max_nr = (UINT16_MAX - sizeof(struct id_index_event)) /
2187 sizeof(struct id_index_entry);
2188
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002189 evlist__for_each_entry(evlist, evsel)
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002190 nr += evsel->ids;
2191
2192 n = nr > max_nr ? max_nr : nr;
2193 sz = sizeof(struct id_index_event) + n * sizeof(struct id_index_entry);
2194 ev = zalloc(sz);
2195 if (!ev)
2196 return -ENOMEM;
2197
2198 ev->id_index.header.type = PERF_RECORD_ID_INDEX;
2199 ev->id_index.header.size = sz;
2200 ev->id_index.nr = n;
2201
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002202 evlist__for_each_entry(evlist, evsel) {
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002203 u32 j;
2204
2205 for (j = 0; j < evsel->ids; j++) {
2206 struct id_index_entry *e;
2207 struct perf_sample_id *sid;
2208
2209 if (i >= n) {
2210 err = process(tool, ev, NULL, machine);
2211 if (err)
2212 goto out_err;
2213 nr -= n;
2214 i = 0;
2215 }
2216
2217 e = &ev->id_index.entries[i++];
2218
2219 e->id = evsel->id[j];
2220
2221 sid = perf_evlist__id2sid(evlist, e->id);
2222 if (!sid) {
2223 free(ev);
2224 return -ENOENT;
2225 }
2226
2227 e->idx = sid->idx;
2228 e->cpu = sid->cpu;
2229 e->tid = sid->tid;
2230 }
2231 }
2232
2233 sz = sizeof(struct id_index_event) + nr * sizeof(struct id_index_entry);
2234 ev->id_index.header.size = sz;
2235 ev->id_index.nr = nr;
2236
2237 err = process(tool, ev, NULL, machine);
2238out_err:
2239 free(ev);
2240
2241 return err;
2242}