blob: 24259bc2c598faa4187f8eb3a4538560cc913552 [file] [log] [blame]
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001#include <linux/kernel.h>
Robert Richter4e319022013-06-11 17:29:18 +02002#include <traceevent/event-parse.h>
Alexander Shishkin05a1f472017-03-16 18:41:59 +02003#include <api/fs/fs.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02004
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02005#include <byteswap.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02006#include <unistd.h>
7#include <sys/types.h>
Arnaldo Carvalho de Meloa41794c2010-05-18 18:29:23 -03008#include <sys/mman.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02009
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -030010#include "evlist.h"
11#include "evsel.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020012#include "session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020013#include "tool.h"
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -020014#include "sort.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020015#include "util.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100016#include "cpumap.h"
Jiri Olsa0f6a3012012-08-07 15:20:45 +020017#include "perf_regs.h"
Jiri Olsab0a45202014-06-12 09:50:11 +020018#include "asm/bug.h"
Adrian Hunterc4468702015-04-09 18:53:48 +030019#include "auxtrace.h"
Adrian Huntera5499b32015-05-29 16:33:30 +030020#include "thread-stack.h"
Jiri Olsa2d2aea62015-10-25 15:51:42 +010021#include "stat.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020022
Adrian Hunterc4468702015-04-09 18:53:48 +030023static int perf_session__deliver_event(struct perf_session *session,
24 union perf_event *event,
25 struct perf_sample *sample,
26 struct perf_tool *tool,
27 u64 file_offset);
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -030028
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030029static int perf_session__open(struct perf_session *session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020030{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030031 struct perf_data_file *file = session->file;
Tom Zanussi8dc58102010-04-01 23:59:15 -050032
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030033 if (perf_session__read_header(session) < 0) {
Arnaldo Carvalho de Meloe87b4912015-11-09 17:12:03 -030034 pr_err("incompatible file format (rerun with -v to learn more)\n");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020035 return -1;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020036 }
37
Jiri Olsacc9784bd2013-10-15 16:27:34 +020038 if (perf_data_file__is_pipe(file))
39 return 0;
40
Jiri Olsa3ba78bd2015-11-05 15:40:47 +010041 if (perf_header__has_feat(&session->header, HEADER_STAT))
42 return 0;
43
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030044 if (!perf_evlist__valid_sample_type(session->evlist)) {
Arnaldo Carvalho de Meloe87b4912015-11-09 17:12:03 -030045 pr_err("non matching sample_type\n");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020046 return -1;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030047 }
48
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030049 if (!perf_evlist__valid_sample_id_all(session->evlist)) {
Arnaldo Carvalho de Meloe87b4912015-11-09 17:12:03 -030050 pr_err("non matching sample_id_all\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_read_format(session->evlist)) {
Arnaldo Carvalho de Meloe87b4912015-11-09 17:12:03 -030055 pr_err("non matching read_format\n");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020056 return -1;
Jiri Olsa9ede4732012-10-10 17:38:13 +020057 }
58
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020059 return 0;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020060}
61
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030062void perf_session__set_id_hdr_size(struct perf_session *session)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020063{
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030064 u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist);
65
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030066 machines__set_id_hdr_size(&session->machines, id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020067}
68
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030069int perf_session__create_kernel_maps(struct perf_session *session)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080070{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030071 int ret = machine__create_kernel_maps(&session->machines.host);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080072
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080073 if (ret >= 0)
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030074 ret = machines__create_guest_kernel_maps(&session->machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080075 return ret;
76}
77
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030078static void perf_session__destroy_kernel_maps(struct perf_session *session)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -030079{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030080 machines__destroy_kernel_maps(&session->machines);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -030081}
82
Adrian Huntercfe1c412014-07-31 09:00:45 +030083static bool perf_session__has_comm_exec(struct perf_session *session)
84{
85 struct perf_evsel *evsel;
86
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -030087 evlist__for_each_entry(session->evlist, evsel) {
Adrian Huntercfe1c412014-07-31 09:00:45 +030088 if (evsel->attr.comm_exec)
89 return true;
90 }
91
92 return false;
93}
94
95static void perf_session__set_comm_exec(struct perf_session *session)
96{
97 bool comm_exec = perf_session__has_comm_exec(session);
98
99 machines__set_comm_exec(&session->machines, comm_exec);
100}
101
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300102static int ordered_events__deliver_event(struct ordered_events *oe,
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300103 struct ordered_event *event)
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300104{
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300105 struct perf_sample sample;
106 struct perf_session *session = container_of(oe, struct perf_session,
107 ordered_events);
108 int ret = perf_evlist__parse_sample(session->evlist, event->event, &sample);
109
110 if (ret) {
111 pr_err("Can't parse sample, err = %d\n", ret);
112 return ret;
113 }
114
Adrian Hunterc4468702015-04-09 18:53:48 +0300115 return perf_session__deliver_event(session, event->event, &sample,
116 session->tool, event->file_offset);
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300117}
118
Jiri Olsaf5fc14122013-10-15 16:27:32 +0200119struct perf_session *perf_session__new(struct perf_data_file *file,
120 bool repipe, struct perf_tool *tool)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200121{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300122 struct perf_session *session = zalloc(sizeof(*session));
Robert Richterefad1412011-12-07 10:02:54 +0100123
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300124 if (!session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200125 goto out;
126
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300127 session->repipe = repipe;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300128 session->tool = tool;
Adrian Hunter99fa2982015-04-30 17:37:25 +0300129 INIT_LIST_HEAD(&session->auxtrace_index);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300130 machines__init(&session->machines);
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300131 ordered_events__init(&session->ordered_events, ordered_events__deliver_event);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200132
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200133 if (file) {
134 if (perf_data_file__open(file))
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200135 goto out_delete;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200136
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300137 session->file = file;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200138
139 if (perf_data_file__is_read(file)) {
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300140 if (perf_session__open(session) < 0)
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200141 goto out_close;
142
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300143 perf_session__set_id_hdr_size(session);
Adrian Huntercfe1c412014-07-31 09:00:45 +0300144 perf_session__set_comm_exec(session);
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200145 }
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -0300146 } else {
147 session->machines.host.env = &perf_env;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200148 }
149
150 if (!file || perf_data_file__is_write(file)) {
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200151 /*
152 * In O_RDONLY mode this will be performed when reading the
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200153 * kernel MMAP event, in perf_event__process_mmap().
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200154 */
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300155 if (perf_session__create_kernel_maps(session) < 0)
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700156 pr_warning("Cannot read kernel map\n");
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200157 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200158
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200159 if (tool && tool->ordering_requires_timestamps &&
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200160 tool->ordered_events && !perf_evlist__sample_id_all(session->evlist)) {
Ian Munsie21ef97f2010-12-10 14:09:16 +1100161 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200162 tool->ordered_events = false;
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300163 }
Ian Munsie21ef97f2010-12-10 14:09:16 +1100164
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300165 return session;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200166
167 out_close:
168 perf_data_file__close(file);
169 out_delete:
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300170 perf_session__delete(session);
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200171 out:
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200172 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200173}
174
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200175static void perf_session__delete_threads(struct perf_session *session)
176{
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300177 machine__delete_threads(&session->machines.host);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200178}
179
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300180void perf_session__delete(struct perf_session *session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200181{
Arnaldo Carvalho de Meloe1446552016-06-22 10:02:16 -0300182 if (session == NULL)
183 return;
Adrian Hunterc4468702015-04-09 18:53:48 +0300184 auxtrace__free(session);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300185 auxtrace_index__free(&session->auxtrace_index);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300186 perf_session__destroy_kernel_maps(session);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300187 perf_session__delete_threads(session);
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -0300188 perf_env__exit(&session->header.env);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300189 machines__exit(&session->machines);
190 if (session->file)
191 perf_data_file__close(session->file);
192 free(session);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200193}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200194
Adrian Hunter47c3d102013-07-04 16:20:21 +0300195static int process_event_synth_tracing_data_stub(struct perf_tool *tool
196 __maybe_unused,
197 union perf_event *event
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300198 __maybe_unused,
199 struct perf_session *session
200 __maybe_unused)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200201{
202 dump_printf(": unhandled!\n");
203 return 0;
204}
205
Adrian Hunter47c3d102013-07-04 16:20:21 +0300206static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused,
207 union perf_event *event __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300208 struct perf_evlist **pevlist
209 __maybe_unused)
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200210{
211 dump_printf(": unhandled!\n");
212 return 0;
213}
214
Jiri Olsaffe777252015-10-25 15:51:36 +0100215static int process_event_synth_event_update_stub(struct perf_tool *tool __maybe_unused,
216 union perf_event *event __maybe_unused,
217 struct perf_evlist **pevlist
218 __maybe_unused)
219{
Jiri Olsa2d2aea62015-10-25 15:51:42 +0100220 if (dump_trace)
221 perf_event__fprintf_event_update(event, stdout);
222
Jiri Olsaffe777252015-10-25 15:51:36 +0100223 dump_printf(": unhandled!\n");
224 return 0;
225}
226
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300227static int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
228 union perf_event *event __maybe_unused,
229 struct perf_sample *sample __maybe_unused,
230 struct perf_evsel *evsel __maybe_unused,
231 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300232{
233 dump_printf(": unhandled!\n");
234 return 0;
235}
236
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300237static int process_event_stub(struct perf_tool *tool __maybe_unused,
238 union perf_event *event __maybe_unused,
239 struct perf_sample *sample __maybe_unused,
240 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200241{
242 dump_printf(": unhandled!\n");
243 return 0;
244}
245
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300246static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
247 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300248 struct ordered_events *oe __maybe_unused)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200249{
250 dump_printf(": unhandled!\n");
251 return 0;
252}
253
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200254static int process_finished_round(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200255 union perf_event *event,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300256 struct ordered_events *oe);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200257
Adrian Huntera16ac022015-04-09 18:53:43 +0300258static int skipn(int fd, off_t n)
259{
260 char buf[4096];
261 ssize_t ret;
262
263 while (n > 0) {
264 ret = read(fd, buf, min(n, (off_t)sizeof(buf)));
265 if (ret <= 0)
266 return ret;
267 n -= ret;
268 }
269
270 return 0;
271}
272
273static s64 process_event_auxtrace_stub(struct perf_tool *tool __maybe_unused,
274 union perf_event *event,
275 struct perf_session *session
276 __maybe_unused)
277{
278 dump_printf(": unhandled!\n");
279 if (perf_data_file__is_pipe(session->file))
280 skipn(perf_data_file__fd(session->file), event->auxtrace.size);
281 return event->auxtrace.size;
282}
283
Adrian Hunter5fb0ac12016-03-07 16:44:39 -0300284static int process_event_op2_stub(struct perf_tool *tool __maybe_unused,
285 union perf_event *event __maybe_unused,
286 struct perf_session *session __maybe_unused)
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300287{
288 dump_printf(": unhandled!\n");
289 return 0;
290}
291
Jiri Olsa5f3339d2015-10-25 15:51:19 +0100292
293static
294int process_event_thread_map_stub(struct perf_tool *tool __maybe_unused,
295 union perf_event *event __maybe_unused,
296 struct perf_session *session __maybe_unused)
297{
Jiri Olsa2d2aea62015-10-25 15:51:42 +0100298 if (dump_trace)
299 perf_event__fprintf_thread_map(event, stdout);
300
Jiri Olsa5f3339d2015-10-25 15:51:19 +0100301 dump_printf(": unhandled!\n");
302 return 0;
303}
304
Jiri Olsa6640b6c2015-10-25 15:51:23 +0100305static
306int process_event_cpu_map_stub(struct perf_tool *tool __maybe_unused,
307 union perf_event *event __maybe_unused,
308 struct perf_session *session __maybe_unused)
309{
Jiri Olsa2d2aea62015-10-25 15:51:42 +0100310 if (dump_trace)
311 perf_event__fprintf_cpu_map(event, stdout);
312
Jiri Olsa6640b6c2015-10-25 15:51:23 +0100313 dump_printf(": unhandled!\n");
314 return 0;
315}
316
Jiri Olsa374fb9e2015-10-25 15:51:27 +0100317static
318int process_event_stat_config_stub(struct perf_tool *tool __maybe_unused,
319 union perf_event *event __maybe_unused,
320 struct perf_session *session __maybe_unused)
321{
Jiri Olsa2d2aea62015-10-25 15:51:42 +0100322 if (dump_trace)
323 perf_event__fprintf_stat_config(event, stdout);
324
Jiri Olsa374fb9e2015-10-25 15:51:27 +0100325 dump_printf(": unhandled!\n");
326 return 0;
327}
328
Jiri Olsad80518c2015-10-25 15:51:30 +0100329static int process_stat_stub(struct perf_tool *tool __maybe_unused,
330 union perf_event *event __maybe_unused,
331 struct perf_session *perf_session
332 __maybe_unused)
333{
Jiri Olsa2d2aea62015-10-25 15:51:42 +0100334 if (dump_trace)
335 perf_event__fprintf_stat(event, stdout);
336
Jiri Olsad80518c2015-10-25 15:51:30 +0100337 dump_printf(": unhandled!\n");
338 return 0;
339}
340
Jiri Olsa2d8f0f12015-10-25 15:51:33 +0100341static int process_stat_round_stub(struct perf_tool *tool __maybe_unused,
342 union perf_event *event __maybe_unused,
343 struct perf_session *perf_session
344 __maybe_unused)
345{
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;
363 if (tool->fork == NULL)
364 tool->fork = process_event_stub;
365 if (tool->exit == NULL)
366 tool->exit = process_event_stub;
367 if (tool->lost == NULL)
368 tool->lost = perf_event__process_lost;
Kan Liangc4937a92015-05-10 15:13:15 -0400369 if (tool->lost_samples == NULL)
370 tool->lost_samples = perf_event__process_lost_samples;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300371 if (tool->aux == NULL)
372 tool->aux = perf_event__process_aux;
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300373 if (tool->itrace_start == NULL)
374 tool->itrace_start = perf_event__process_itrace_start;
Adrian Hunter02860392015-07-21 12:44:03 +0300375 if (tool->context_switch == NULL)
376 tool->context_switch = perf_event__process_switch;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200377 if (tool->read == NULL)
378 tool->read = process_event_sample_stub;
379 if (tool->throttle == NULL)
380 tool->throttle = process_event_stub;
381 if (tool->unthrottle == NULL)
382 tool->unthrottle = process_event_stub;
383 if (tool->attr == NULL)
384 tool->attr = process_event_synth_attr_stub;
Jiri Olsaffe777252015-10-25 15:51:36 +0100385 if (tool->event_update == NULL)
386 tool->event_update = process_event_synth_event_update_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200387 if (tool->tracing_data == NULL)
388 tool->tracing_data = process_event_synth_tracing_data_stub;
389 if (tool->build_id == NULL)
Adrian Hunter5fb0ac12016-03-07 16:44:39 -0300390 tool->build_id = process_event_op2_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200391 if (tool->finished_round == NULL) {
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200392 if (tool->ordered_events)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200393 tool->finished_round = process_finished_round;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200394 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200395 tool->finished_round = process_finished_round_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200396 }
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200397 if (tool->id_index == NULL)
Adrian Hunter5fb0ac12016-03-07 16:44:39 -0300398 tool->id_index = process_event_op2_stub;
Adrian Huntera16ac022015-04-09 18:53:43 +0300399 if (tool->auxtrace_info == NULL)
Adrian Hunter5fb0ac12016-03-07 16:44:39 -0300400 tool->auxtrace_info = process_event_op2_stub;
Adrian Huntera16ac022015-04-09 18:53:43 +0300401 if (tool->auxtrace == NULL)
402 tool->auxtrace = process_event_auxtrace_stub;
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300403 if (tool->auxtrace_error == NULL)
Adrian Hunter5fb0ac12016-03-07 16:44:39 -0300404 tool->auxtrace_error = process_event_op2_stub;
Jiri Olsa5f3339d2015-10-25 15:51:19 +0100405 if (tool->thread_map == NULL)
406 tool->thread_map = process_event_thread_map_stub;
Jiri Olsa6640b6c2015-10-25 15:51:23 +0100407 if (tool->cpu_map == NULL)
408 tool->cpu_map = process_event_cpu_map_stub;
Jiri Olsa374fb9e2015-10-25 15:51:27 +0100409 if (tool->stat_config == NULL)
410 tool->stat_config = process_event_stat_config_stub;
Jiri Olsad80518c2015-10-25 15:51:30 +0100411 if (tool->stat == NULL)
412 tool->stat = process_stat_stub;
Jiri Olsa2d8f0f12015-10-25 15:51:33 +0100413 if (tool->stat_round == NULL)
414 tool->stat_round = process_stat_round_stub;
Adrian Hunter46bc29b2016-03-08 10:38:44 +0200415 if (tool->time_conv == NULL)
416 tool->time_conv = process_event_op2_stub;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200417}
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300418
Jiri Olsa268fb202012-05-30 14:23:43 +0200419static void swap_sample_id_all(union perf_event *event, void *data)
420{
421 void *end = (void *) event + event->header.size;
422 int size = end - data;
423
424 BUG_ON(size % sizeof(u64));
425 mem_bswap_64(data, size);
426}
427
428static void perf_event__all64_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300429 bool sample_id_all __maybe_unused)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200430{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200431 struct perf_event_header *hdr = &event->header;
432 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200433}
434
Jiri Olsa268fb202012-05-30 14:23:43 +0200435static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200436{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200437 event->comm.pid = bswap_32(event->comm.pid);
438 event->comm.tid = bswap_32(event->comm.tid);
Jiri Olsa268fb202012-05-30 14:23:43 +0200439
440 if (sample_id_all) {
441 void *data = &event->comm.comm;
442
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300443 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200444 swap_sample_id_all(event, data);
445 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200446}
447
Jiri Olsa268fb202012-05-30 14:23:43 +0200448static void perf_event__mmap_swap(union perf_event *event,
449 bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200450{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200451 event->mmap.pid = bswap_32(event->mmap.pid);
452 event->mmap.tid = bswap_32(event->mmap.tid);
453 event->mmap.start = bswap_64(event->mmap.start);
454 event->mmap.len = bswap_64(event->mmap.len);
455 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
Jiri Olsa268fb202012-05-30 14:23:43 +0200456
457 if (sample_id_all) {
458 void *data = &event->mmap.filename;
459
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300460 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200461 swap_sample_id_all(event, data);
462 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200463}
464
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200465static void perf_event__mmap2_swap(union perf_event *event,
466 bool sample_id_all)
467{
468 event->mmap2.pid = bswap_32(event->mmap2.pid);
469 event->mmap2.tid = bswap_32(event->mmap2.tid);
470 event->mmap2.start = bswap_64(event->mmap2.start);
471 event->mmap2.len = bswap_64(event->mmap2.len);
472 event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
473 event->mmap2.maj = bswap_32(event->mmap2.maj);
474 event->mmap2.min = bswap_32(event->mmap2.min);
475 event->mmap2.ino = bswap_64(event->mmap2.ino);
476
477 if (sample_id_all) {
478 void *data = &event->mmap2.filename;
479
480 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
481 swap_sample_id_all(event, data);
482 }
483}
Jiri Olsa268fb202012-05-30 14:23:43 +0200484static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200485{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200486 event->fork.pid = bswap_32(event->fork.pid);
487 event->fork.tid = bswap_32(event->fork.tid);
488 event->fork.ppid = bswap_32(event->fork.ppid);
489 event->fork.ptid = bswap_32(event->fork.ptid);
490 event->fork.time = bswap_64(event->fork.time);
Jiri Olsa268fb202012-05-30 14:23:43 +0200491
492 if (sample_id_all)
493 swap_sample_id_all(event, &event->fork + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200494}
495
Jiri Olsa268fb202012-05-30 14:23:43 +0200496static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200497{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200498 event->read.pid = bswap_32(event->read.pid);
499 event->read.tid = bswap_32(event->read.tid);
500 event->read.value = bswap_64(event->read.value);
501 event->read.time_enabled = bswap_64(event->read.time_enabled);
502 event->read.time_running = bswap_64(event->read.time_running);
503 event->read.id = bswap_64(event->read.id);
Jiri Olsa268fb202012-05-30 14:23:43 +0200504
505 if (sample_id_all)
506 swap_sample_id_all(event, &event->read + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200507}
508
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300509static void perf_event__aux_swap(union perf_event *event, bool sample_id_all)
510{
511 event->aux.aux_offset = bswap_64(event->aux.aux_offset);
512 event->aux.aux_size = bswap_64(event->aux.aux_size);
513 event->aux.flags = bswap_64(event->aux.flags);
514
515 if (sample_id_all)
516 swap_sample_id_all(event, &event->aux + 1);
517}
518
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300519static void perf_event__itrace_start_swap(union perf_event *event,
520 bool sample_id_all)
521{
522 event->itrace_start.pid = bswap_32(event->itrace_start.pid);
523 event->itrace_start.tid = bswap_32(event->itrace_start.tid);
524
525 if (sample_id_all)
526 swap_sample_id_all(event, &event->itrace_start + 1);
527}
528
Adrian Hunter02860392015-07-21 12:44:03 +0300529static void perf_event__switch_swap(union perf_event *event, bool sample_id_all)
530{
531 if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
532 event->context_switch.next_prev_pid =
533 bswap_32(event->context_switch.next_prev_pid);
534 event->context_switch.next_prev_tid =
535 bswap_32(event->context_switch.next_prev_tid);
536 }
537
538 if (sample_id_all)
539 swap_sample_id_all(event, &event->context_switch + 1);
540}
541
Jiri Olsadd96c462013-09-01 12:36:15 +0200542static void perf_event__throttle_swap(union perf_event *event,
543 bool sample_id_all)
544{
545 event->throttle.time = bswap_64(event->throttle.time);
546 event->throttle.id = bswap_64(event->throttle.id);
547 event->throttle.stream_id = bswap_64(event->throttle.stream_id);
548
549 if (sample_id_all)
550 swap_sample_id_all(event, &event->throttle + 1);
551}
552
Jiri Olsae108c662012-05-16 08:59:03 +0200553static u8 revbyte(u8 b)
554{
555 int rev = (b >> 4) | ((b & 0xf) << 4);
556 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
557 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
558 return (u8) rev;
559}
560
561/*
562 * XXX this is hack in attempt to carry flags bitfield
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -0800563 * through endian village. ABI says:
Jiri Olsae108c662012-05-16 08:59:03 +0200564 *
565 * Bit-fields are allocated from right to left (least to most significant)
566 * on little-endian implementations and from left to right (most to least
567 * significant) on big-endian implementations.
568 *
569 * The above seems to be byte specific, so we need to reverse each
570 * byte of the bitfield. 'Internet' also says this might be implementation
571 * specific and we probably need proper fix and carry perf_event_attr
572 * bitfield flags in separate data file FEAT_ section. Thought this seems
573 * to work for now.
574 */
575static void swap_bitfield(u8 *p, unsigned len)
576{
577 unsigned i;
578
579 for (i = 0; i < len; i++) {
580 *p = revbyte(*p);
581 p++;
582 }
583}
584
David Aherneda39132011-07-15 12:34:09 -0600585/* exported for swapping attributes in file header */
586void perf_event__attr_swap(struct perf_event_attr *attr)
587{
588 attr->type = bswap_32(attr->type);
589 attr->size = bswap_32(attr->size);
Jiri Olsae108c662012-05-16 08:59:03 +0200590
Wang Nanb30b6172015-06-17 09:56:39 +0000591#define bswap_safe(f, n) \
592 (attr->size > (offsetof(struct perf_event_attr, f) + \
593 sizeof(attr->f) * (n)))
594#define bswap_field(f, sz) \
595do { \
596 if (bswap_safe(f, 0)) \
597 attr->f = bswap_##sz(attr->f); \
598} while(0)
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300599#define bswap_field_16(f) bswap_field(f, 16)
Wang Nanb30b6172015-06-17 09:56:39 +0000600#define bswap_field_32(f) bswap_field(f, 32)
601#define bswap_field_64(f) bswap_field(f, 64)
602
603 bswap_field_64(config);
604 bswap_field_64(sample_period);
605 bswap_field_64(sample_type);
606 bswap_field_64(read_format);
607 bswap_field_32(wakeup_events);
608 bswap_field_32(bp_type);
609 bswap_field_64(bp_addr);
610 bswap_field_64(bp_len);
611 bswap_field_64(branch_sample_type);
612 bswap_field_64(sample_regs_user);
613 bswap_field_32(sample_stack_user);
614 bswap_field_32(aux_watermark);
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300615 bswap_field_16(sample_max_stack);
Wang Nanb30b6172015-06-17 09:56:39 +0000616
617 /*
618 * After read_format are bitfields. Check read_format because
619 * we are unable to use offsetof on bitfield.
620 */
621 if (bswap_safe(read_format, 1))
622 swap_bitfield((u8 *) (&attr->read_format + 1),
623 sizeof(u64));
624#undef bswap_field_64
625#undef bswap_field_32
626#undef bswap_field
627#undef bswap_safe
David Aherneda39132011-07-15 12:34:09 -0600628}
629
Jiri Olsa268fb202012-05-30 14:23:43 +0200630static void perf_event__hdr_attr_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300631 bool sample_id_all __maybe_unused)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500632{
633 size_t size;
634
David Aherneda39132011-07-15 12:34:09 -0600635 perf_event__attr_swap(&event->attr.attr);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500636
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200637 size = event->header.size;
638 size -= (void *)&event->attr.id - (void *)event;
639 mem_bswap_64(event->attr.id, size);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500640}
641
Jiri Olsaffe777252015-10-25 15:51:36 +0100642static void perf_event__event_update_swap(union perf_event *event,
643 bool sample_id_all __maybe_unused)
644{
645 event->event_update.type = bswap_64(event->event_update.type);
646 event->event_update.id = bswap_64(event->event_update.id);
647}
648
Jiri Olsa268fb202012-05-30 14:23:43 +0200649static void perf_event__event_type_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300650 bool sample_id_all __maybe_unused)
Tom Zanussicd19a032010-04-01 23:59:20 -0500651{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200652 event->event_type.event_type.event_id =
653 bswap_64(event->event_type.event_type.event_id);
Tom Zanussicd19a032010-04-01 23:59:20 -0500654}
655
Jiri Olsa268fb202012-05-30 14:23:43 +0200656static void perf_event__tracing_data_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300657 bool sample_id_all __maybe_unused)
Tom Zanussi92155452010-04-01 23:59:21 -0500658{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200659 event->tracing_data.size = bswap_32(event->tracing_data.size);
Tom Zanussi92155452010-04-01 23:59:21 -0500660}
661
Adrian Huntera16ac022015-04-09 18:53:43 +0300662static void perf_event__auxtrace_info_swap(union perf_event *event,
663 bool sample_id_all __maybe_unused)
664{
665 size_t size;
666
667 event->auxtrace_info.type = bswap_32(event->auxtrace_info.type);
668
669 size = event->header.size;
670 size -= (void *)&event->auxtrace_info.priv - (void *)event;
671 mem_bswap_64(event->auxtrace_info.priv, size);
672}
673
674static void perf_event__auxtrace_swap(union perf_event *event,
675 bool sample_id_all __maybe_unused)
676{
677 event->auxtrace.size = bswap_64(event->auxtrace.size);
678 event->auxtrace.offset = bswap_64(event->auxtrace.offset);
679 event->auxtrace.reference = bswap_64(event->auxtrace.reference);
680 event->auxtrace.idx = bswap_32(event->auxtrace.idx);
681 event->auxtrace.tid = bswap_32(event->auxtrace.tid);
682 event->auxtrace.cpu = bswap_32(event->auxtrace.cpu);
683}
684
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300685static void perf_event__auxtrace_error_swap(union perf_event *event,
686 bool sample_id_all __maybe_unused)
687{
688 event->auxtrace_error.type = bswap_32(event->auxtrace_error.type);
689 event->auxtrace_error.code = bswap_32(event->auxtrace_error.code);
690 event->auxtrace_error.cpu = bswap_32(event->auxtrace_error.cpu);
691 event->auxtrace_error.pid = bswap_32(event->auxtrace_error.pid);
692 event->auxtrace_error.tid = bswap_32(event->auxtrace_error.tid);
693 event->auxtrace_error.ip = bswap_64(event->auxtrace_error.ip);
694}
695
Jiri Olsa5f3339d2015-10-25 15:51:19 +0100696static void perf_event__thread_map_swap(union perf_event *event,
697 bool sample_id_all __maybe_unused)
698{
699 unsigned i;
700
701 event->thread_map.nr = bswap_64(event->thread_map.nr);
702
703 for (i = 0; i < event->thread_map.nr; i++)
704 event->thread_map.entries[i].pid = bswap_64(event->thread_map.entries[i].pid);
705}
706
Jiri Olsa6640b6c2015-10-25 15:51:23 +0100707static void perf_event__cpu_map_swap(union perf_event *event,
708 bool sample_id_all __maybe_unused)
709{
710 struct cpu_map_data *data = &event->cpu_map.data;
711 struct cpu_map_entries *cpus;
712 struct cpu_map_mask *mask;
713 unsigned i;
714
715 data->type = bswap_64(data->type);
716
717 switch (data->type) {
718 case PERF_CPU_MAP__CPUS:
719 cpus = (struct cpu_map_entries *)data->data;
720
721 cpus->nr = bswap_16(cpus->nr);
722
723 for (i = 0; i < cpus->nr; i++)
724 cpus->cpu[i] = bswap_16(cpus->cpu[i]);
725 break;
726 case PERF_CPU_MAP__MASK:
727 mask = (struct cpu_map_mask *) data->data;
728
729 mask->nr = bswap_16(mask->nr);
730 mask->long_size = bswap_16(mask->long_size);
731
732 switch (mask->long_size) {
733 case 4: mem_bswap_32(&mask->mask, mask->nr); break;
734 case 8: mem_bswap_64(&mask->mask, mask->nr); break;
735 default:
736 pr_err("cpu_map swap: unsupported long size\n");
737 }
738 default:
739 break;
740 }
741}
742
Jiri Olsa374fb9e2015-10-25 15:51:27 +0100743static void perf_event__stat_config_swap(union perf_event *event,
744 bool sample_id_all __maybe_unused)
745{
746 u64 size;
747
748 size = event->stat_config.nr * sizeof(event->stat_config.data[0]);
749 size += 1; /* nr item itself */
750 mem_bswap_64(&event->stat_config.nr, size);
751}
752
Jiri Olsad80518c2015-10-25 15:51:30 +0100753static void perf_event__stat_swap(union perf_event *event,
754 bool sample_id_all __maybe_unused)
755{
756 event->stat.id = bswap_64(event->stat.id);
757 event->stat.thread = bswap_32(event->stat.thread);
758 event->stat.cpu = bswap_32(event->stat.cpu);
759 event->stat.val = bswap_64(event->stat.val);
760 event->stat.ena = bswap_64(event->stat.ena);
761 event->stat.run = bswap_64(event->stat.run);
762}
763
Jiri Olsa2d8f0f12015-10-25 15:51:33 +0100764static void perf_event__stat_round_swap(union perf_event *event,
765 bool sample_id_all __maybe_unused)
766{
767 event->stat_round.type = bswap_64(event->stat_round.type);
768 event->stat_round.time = bswap_64(event->stat_round.time);
769}
770
Jiri Olsa268fb202012-05-30 14:23:43 +0200771typedef void (*perf_event__swap_op)(union perf_event *event,
772 bool sample_id_all);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200773
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200774static perf_event__swap_op perf_event__swap_ops[] = {
775 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200776 [PERF_RECORD_MMAP2] = perf_event__mmap2_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200777 [PERF_RECORD_COMM] = perf_event__comm_swap,
778 [PERF_RECORD_FORK] = perf_event__task_swap,
779 [PERF_RECORD_EXIT] = perf_event__task_swap,
780 [PERF_RECORD_LOST] = perf_event__all64_swap,
781 [PERF_RECORD_READ] = perf_event__read_swap,
Jiri Olsadd96c462013-09-01 12:36:15 +0200782 [PERF_RECORD_THROTTLE] = perf_event__throttle_swap,
783 [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200784 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300785 [PERF_RECORD_AUX] = perf_event__aux_swap,
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300786 [PERF_RECORD_ITRACE_START] = perf_event__itrace_start_swap,
Kan Liangc4937a92015-05-10 15:13:15 -0400787 [PERF_RECORD_LOST_SAMPLES] = perf_event__all64_swap,
Adrian Hunter02860392015-07-21 12:44:03 +0300788 [PERF_RECORD_SWITCH] = perf_event__switch_swap,
789 [PERF_RECORD_SWITCH_CPU_WIDE] = perf_event__switch_swap,
David Aherneda39132011-07-15 12:34:09 -0600790 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200791 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
792 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
793 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200794 [PERF_RECORD_ID_INDEX] = perf_event__all64_swap,
Adrian Huntera16ac022015-04-09 18:53:43 +0300795 [PERF_RECORD_AUXTRACE_INFO] = perf_event__auxtrace_info_swap,
796 [PERF_RECORD_AUXTRACE] = perf_event__auxtrace_swap,
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300797 [PERF_RECORD_AUXTRACE_ERROR] = perf_event__auxtrace_error_swap,
Jiri Olsa5f3339d2015-10-25 15:51:19 +0100798 [PERF_RECORD_THREAD_MAP] = perf_event__thread_map_swap,
Jiri Olsa6640b6c2015-10-25 15:51:23 +0100799 [PERF_RECORD_CPU_MAP] = perf_event__cpu_map_swap,
Jiri Olsa374fb9e2015-10-25 15:51:27 +0100800 [PERF_RECORD_STAT_CONFIG] = perf_event__stat_config_swap,
Jiri Olsad80518c2015-10-25 15:51:30 +0100801 [PERF_RECORD_STAT] = perf_event__stat_swap,
Jiri Olsa2d8f0f12015-10-25 15:51:33 +0100802 [PERF_RECORD_STAT_ROUND] = perf_event__stat_round_swap,
Jiri Olsaffe777252015-10-25 15:51:36 +0100803 [PERF_RECORD_EVENT_UPDATE] = perf_event__event_update_swap,
Adrian Hunter46bc29b2016-03-08 10:38:44 +0200804 [PERF_RECORD_TIME_CONV] = perf_event__all64_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200805 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200806};
807
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200808/*
809 * When perf record finishes a pass on every buffers, it records this pseudo
810 * event.
811 * We record the max timestamp t found in the pass n.
812 * Assuming these timestamps are monotonic across cpus, we know that if
813 * a buffer still has events with timestamps below t, they will be all
814 * available and then read in the pass n + 1.
815 * Hence when we start to read the pass n + 2, we can safely flush every
816 * events with timestamps below t.
817 *
818 * ============ PASS n =================
819 * CPU 0 | CPU 1
820 * |
821 * cnt1 timestamps | cnt2 timestamps
822 * 1 | 2
823 * 2 | 3
824 * - | 4 <--- max recorded
825 *
826 * ============ PASS n + 1 ==============
827 * CPU 0 | CPU 1
828 * |
829 * cnt1 timestamps | cnt2 timestamps
830 * 3 | 5
831 * 4 | 6
832 * 5 | 7 <---- max recorded
833 *
834 * Flush every events below timestamp 4
835 *
836 * ============ PASS n + 2 ==============
837 * CPU 0 | CPU 1
838 * |
839 * cnt1 timestamps | cnt2 timestamps
840 * 6 | 8
841 * 7 | 9
842 * - | 10
843 *
844 * Flush every events below timestamp 7
845 * etc...
846 */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300847static int process_finished_round(struct perf_tool *tool __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300848 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300849 struct ordered_events *oe)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200850{
Adrian Hunter5531e162015-06-23 10:52:48 +0300851 if (dump_trace)
852 fprintf(stdout, "\n");
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300853 return ordered_events__flush(oe, OE_FLUSH__ROUND);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200854}
855
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300856int perf_session__queue_event(struct perf_session *s, union perf_event *event,
857 struct perf_sample *sample, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200858{
Arnaldo Carvalho de Melo4a6b3622015-03-03 13:02:24 -0300859 return ordered_events__queue(&s->ordered_events, event, sample, file_offset);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200860}
861
Kan Liang384b6052015-01-05 13:23:05 -0500862static void callchain__lbr_callstack_printf(struct perf_sample *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200863{
Kan Liang384b6052015-01-05 13:23:05 -0500864 struct ip_callchain *callchain = sample->callchain;
865 struct branch_stack *lbr_stack = sample->branch_stack;
866 u64 kernel_callchain_nr = callchain->nr;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200867 unsigned int i;
868
Kan Liang384b6052015-01-05 13:23:05 -0500869 for (i = 0; i < kernel_callchain_nr; i++) {
870 if (callchain->ips[i] == PERF_CONTEXT_USER)
871 break;
872 }
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200873
Kan Liang384b6052015-01-05 13:23:05 -0500874 if ((i != kernel_callchain_nr) && lbr_stack->nr) {
875 u64 total_nr;
876 /*
877 * LBR callstack can only get user call chain,
878 * i is kernel call chain number,
879 * 1 is PERF_CONTEXT_USER.
880 *
881 * The user call chain is stored in LBR registers.
882 * LBR are pair registers. The caller is stored
883 * in "from" register, while the callee is stored
884 * in "to" register.
885 * For example, there is a call stack
886 * "A"->"B"->"C"->"D".
887 * The LBR registers will recorde like
888 * "C"->"D", "B"->"C", "A"->"B".
889 * So only the first "to" register and all "from"
890 * registers are needed to construct the whole stack.
891 */
892 total_nr = i + 1 + lbr_stack->nr + 1;
893 kernel_callchain_nr = i + 1;
894
895 printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr);
896
897 for (i = 0; i < kernel_callchain_nr; i++)
898 printf("..... %2d: %016" PRIx64 "\n",
899 i, callchain->ips[i]);
900
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200901 printf("..... %2d: %016" PRIx64 "\n",
Kan Liang384b6052015-01-05 13:23:05 -0500902 (int)(kernel_callchain_nr), lbr_stack->entries[0].to);
903 for (i = 0; i < lbr_stack->nr; i++)
904 printf("..... %2d: %016" PRIx64 "\n",
905 (int)(i + kernel_callchain_nr + 1), lbr_stack->entries[i].from);
906 }
907}
908
909static void callchain__printf(struct perf_evsel *evsel,
910 struct perf_sample *sample)
911{
912 unsigned int i;
913 struct ip_callchain *callchain = sample->callchain;
914
Arnaldo Carvalho de Meloacf2abb2016-04-18 10:35:03 -0300915 if (perf_evsel__has_branch_callstack(evsel))
Kan Liang384b6052015-01-05 13:23:05 -0500916 callchain__lbr_callstack_printf(sample);
917
918 printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr);
919
920 for (i = 0; i < callchain->nr; i++)
921 printf("..... %2d: %016" PRIx64 "\n",
922 i, callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200923}
924
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100925static void branch_stack__printf(struct perf_sample *sample)
926{
927 uint64_t i;
928
929 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
930
Andi Kleen0e332f02015-07-18 08:24:46 -0700931 for (i = 0; i < sample->branch_stack->nr; i++) {
932 struct branch_entry *e = &sample->branch_stack->entries[i];
933
934 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 " %hu cycles %s%s%s%s %x\n",
935 i, e->from, e->to,
Arnaldo Carvalho de Melo8074bf52017-02-17 12:27:26 -0300936 (unsigned short)e->flags.cycles,
Andi Kleen0e332f02015-07-18 08:24:46 -0700937 e->flags.mispred ? "M" : " ",
938 e->flags.predicted ? "P" : " ",
939 e->flags.abort ? "A" : " ",
940 e->flags.in_tx ? "T" : " ",
941 (unsigned)e->flags.reserved);
942 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100943}
944
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200945static void regs_dump__printf(u64 mask, u64 *regs)
946{
947 unsigned rid, i = 0;
948
949 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
950 u64 val = regs[i++];
951
952 printf(".... %-5s 0x%" PRIx64 "\n",
953 perf_reg_name(rid), val);
954 }
955}
956
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200957static const char *regs_abi[] = {
958 [PERF_SAMPLE_REGS_ABI_NONE] = "none",
959 [PERF_SAMPLE_REGS_ABI_32] = "32-bit",
960 [PERF_SAMPLE_REGS_ABI_64] = "64-bit",
961};
962
963static inline const char *regs_dump_abi(struct regs_dump *d)
964{
965 if (d->abi > PERF_SAMPLE_REGS_ABI_64)
966 return "unknown";
967
968 return regs_abi[d->abi];
969}
970
971static void regs__printf(const char *type, struct regs_dump *regs)
972{
973 u64 mask = regs->mask;
974
975 printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n",
976 type,
977 mask,
978 regs_dump_abi(regs));
979
980 regs_dump__printf(mask, regs->regs);
981}
982
Jiri Olsa352ea452014-01-07 13:47:25 +0100983static void regs_user__printf(struct perf_sample *sample)
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200984{
985 struct regs_dump *user_regs = &sample->user_regs;
986
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200987 if (user_regs->regs)
988 regs__printf("user", user_regs);
989}
990
991static void regs_intr__printf(struct perf_sample *sample)
992{
993 struct regs_dump *intr_regs = &sample->intr_regs;
994
995 if (intr_regs->regs)
996 regs__printf("intr", intr_regs);
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200997}
998
999static void stack_user__printf(struct stack_dump *dump)
1000{
1001 printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
1002 dump->size, dump->offset);
1003}
1004
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001005static void perf_evlist__print_tstamp(struct perf_evlist *evlist,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001006 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001007 struct perf_sample *sample)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -02001008{
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001009 u64 sample_type = __perf_evlist__combined_sample_type(evlist);
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001010
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -02001011 if (event->header.type != PERF_RECORD_SAMPLE &&
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001012 !perf_evlist__sample_id_all(evlist)) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -02001013 fputs("-1 -1 ", stdout);
1014 return;
1015 }
1016
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001017 if ((sample_type & PERF_SAMPLE_CPU))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -02001018 printf("%u ", sample->cpu);
1019
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001020 if (sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001021 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -02001022}
1023
Jiri Olsa9ede4732012-10-10 17:38:13 +02001024static void sample_read__printf(struct perf_sample *sample, u64 read_format)
1025{
1026 printf("... sample_read:\n");
1027
1028 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1029 printf("...... time enabled %016" PRIx64 "\n",
1030 sample->read.time_enabled);
1031
1032 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1033 printf("...... time running %016" PRIx64 "\n",
1034 sample->read.time_running);
1035
1036 if (read_format & PERF_FORMAT_GROUP) {
1037 u64 i;
1038
1039 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
1040
1041 for (i = 0; i < sample->read.group.nr; i++) {
1042 struct sample_read_value *value;
1043
1044 value = &sample->read.group.values[i];
1045 printf("..... id %016" PRIx64
1046 ", value %016" PRIx64 "\n",
1047 value->id, value->value);
1048 }
1049 } else
1050 printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
1051 sample->read.one.id, sample->read.one.value);
1052}
1053
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001054static void dump_event(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001055 u64 file_offset, struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001056{
1057 if (!dump_trace)
1058 return;
1059
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001060 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
1061 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001062
1063 trace_event(event);
1064
1065 if (sample)
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001066 perf_evlist__print_tstamp(evlist, event, sample);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001067
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001068 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001069 event->header.size, perf_event__name(event->header.type));
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001070}
1071
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001072static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001073 struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001074{
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001075 u64 sample_type;
1076
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -02001077 if (!dump_trace)
1078 return;
1079
Don Zickus0ea590a2014-02-25 22:43:46 -05001080 printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001081 event->header.misc, sample->pid, sample->tid, sample->ip,
David Ahern7cec0922011-05-30 13:08:23 -06001082 sample->period, sample->addr);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001083
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001084 sample_type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001085
1086 if (sample_type & PERF_SAMPLE_CALLCHAIN)
Kan Liang384b6052015-01-05 13:23:05 -05001087 callchain__printf(evsel, sample);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001088
Arnaldo Carvalho de Meloacf2abb2016-04-18 10:35:03 -03001089 if ((sample_type & PERF_SAMPLE_BRANCH_STACK) && !perf_evsel__has_branch_callstack(evsel))
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001090 branch_stack__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001091
1092 if (sample_type & PERF_SAMPLE_REGS_USER)
Jiri Olsa352ea452014-01-07 13:47:25 +01001093 regs_user__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001094
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001095 if (sample_type & PERF_SAMPLE_REGS_INTR)
1096 regs_intr__printf(sample);
1097
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001098 if (sample_type & PERF_SAMPLE_STACK_USER)
1099 stack_user__printf(&sample->user_stack);
Andi Kleen05484292013-01-24 16:10:29 +01001100
1101 if (sample_type & PERF_SAMPLE_WEIGHT)
1102 printf("... weight: %" PRIu64 "\n", sample->weight);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001103
1104 if (sample_type & PERF_SAMPLE_DATA_SRC)
1105 printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001106
Andi Kleen475eeab2013-09-20 07:40:43 -07001107 if (sample_type & PERF_SAMPLE_TRANSACTION)
1108 printf("... transaction: %" PRIx64 "\n", sample->transaction);
1109
Jiri Olsa9ede4732012-10-10 17:38:13 +02001110 if (sample_type & PERF_SAMPLE_READ)
1111 sample_read__printf(sample, evsel->attr.read_format);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001112}
1113
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -03001114static struct machine *machines__find_for_cpumode(struct machines *machines,
Adrian Hunteref893252013-08-27 11:23:06 +03001115 union perf_event *event,
1116 struct perf_sample *sample)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001117{
Dongsheng Yangad85ace2013-12-20 13:41:47 -05001118 struct machine *machine;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001119
David Ahern7c0f4a42012-07-20 17:25:48 -06001120 if (perf_guest &&
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001121 ((sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
1122 (sample->cpumode == PERF_RECORD_MISC_GUEST_USER))) {
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +05301123 u32 pid;
1124
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001125 if (event->header.type == PERF_RECORD_MMAP
1126 || event->header.type == PERF_RECORD_MMAP2)
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +05301127 pid = event->mmap.pid;
1128 else
Adrian Hunteref893252013-08-27 11:23:06 +03001129 pid = sample->pid;
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +05301130
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -03001131 machine = machines__find(machines, pid);
Dongsheng Yangad85ace2013-12-20 13:41:47 -05001132 if (!machine)
Ravi Bangoria3caeaa52015-12-07 12:25:02 +05301133 machine = machines__findnew(machines, DEFAULT_GUEST_KERNEL_ID);
Dongsheng Yangad85ace2013-12-20 13:41:47 -05001134 return machine;
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +05301135 }
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001136
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -03001137 return &machines->host;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001138}
1139
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001140static int deliver_sample_value(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +02001141 struct perf_tool *tool,
1142 union perf_event *event,
1143 struct perf_sample *sample,
1144 struct sample_read_value *v,
1145 struct machine *machine)
1146{
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001147 struct perf_sample_id *sid = perf_evlist__id2sid(evlist, v->id);
Jiri Olsae4caec02012-10-10 18:52:24 +02001148
Jiri Olsae4caec02012-10-10 18:52:24 +02001149 if (sid) {
1150 sample->id = v->id;
1151 sample->period = v->value - sid->period;
1152 sid->period = v->value;
1153 }
1154
1155 if (!sid || sid->evsel == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001156 ++evlist->stats.nr_unknown_id;
Jiri Olsae4caec02012-10-10 18:52:24 +02001157 return 0;
1158 }
1159
1160 return tool->sample(tool, event, sample, sid->evsel, machine);
1161}
1162
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001163static int deliver_sample_group(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +02001164 struct perf_tool *tool,
1165 union perf_event *event,
1166 struct perf_sample *sample,
1167 struct machine *machine)
1168{
1169 int ret = -EINVAL;
1170 u64 i;
1171
1172 for (i = 0; i < sample->read.group.nr; i++) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001173 ret = deliver_sample_value(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001174 &sample->read.group.values[i],
1175 machine);
1176 if (ret)
1177 break;
1178 }
1179
1180 return ret;
1181}
1182
1183static int
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001184 perf_evlist__deliver_sample(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +02001185 struct perf_tool *tool,
1186 union perf_event *event,
1187 struct perf_sample *sample,
1188 struct perf_evsel *evsel,
1189 struct machine *machine)
1190{
1191 /* We know evsel != NULL. */
1192 u64 sample_type = evsel->attr.sample_type;
1193 u64 read_format = evsel->attr.read_format;
1194
Soramichi AKIYAMAd94386f2017-01-17 22:22:33 +09001195 /* Standard sample delivery. */
Jiri Olsae4caec02012-10-10 18:52:24 +02001196 if (!(sample_type & PERF_SAMPLE_READ))
1197 return tool->sample(tool, event, sample, evsel, machine);
1198
1199 /* For PERF_SAMPLE_READ we have either single or group mode. */
1200 if (read_format & PERF_FORMAT_GROUP)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001201 return deliver_sample_group(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001202 machine);
1203 else
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001204 return deliver_sample_value(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001205 &sample->read.one, machine);
1206}
1207
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -03001208static int machines__deliver_event(struct machines *machines,
1209 struct perf_evlist *evlist,
1210 union perf_event *event,
1211 struct perf_sample *sample,
1212 struct perf_tool *tool, u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001213{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001214 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001215 struct machine *machine;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001216
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001217 dump_event(evlist, event, file_offset, sample);
Thomas Gleixner532e7262010-12-07 12:48:55 +00001218
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001219 evsel = perf_evlist__id2evsel(evlist, sample->id);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001220
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001221 machine = machines__find_for_cpumode(machines, event, sample);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001222
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001223 switch (event->header.type) {
1224 case PERF_RECORD_SAMPLE:
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001225 if (evsel == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001226 ++evlist->stats.nr_unknown_id;
Jiri Olsa67822062012-04-12 14:21:01 +02001227 return 0;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001228 }
Kan Liang1b29ac52015-09-03 08:31:00 -04001229 dump_sample(evsel, event, sample);
Joerg Roedel0c095712012-02-10 18:05:04 +01001230 if (machine == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001231 ++evlist->stats.nr_unprocessable_samples;
Jiri Olsa67822062012-04-12 14:21:01 +02001232 return 0;
Joerg Roedel0c095712012-02-10 18:05:04 +01001233 }
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001234 return perf_evlist__deliver_sample(evlist, tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001235 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001236 return tool->mmap(tool, event, sample, machine);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001237 case PERF_RECORD_MMAP2:
Kan Liang930e6fc2015-06-17 09:51:10 -04001238 if (event->header.misc & PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT)
1239 ++evlist->stats.nr_proc_map_timeout;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001240 return tool->mmap2(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001241 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001242 return tool->comm(tool, event, sample, machine);
Hari Bathinif3b36142017-03-08 02:11:43 +05301243 case PERF_RECORD_NAMESPACES:
1244 return tool->namespaces(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001245 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001246 return tool->fork(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001247 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001248 return tool->exit(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001249 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001250 if (tool->lost == perf_event__process_lost)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001251 evlist->stats.total_lost += event->lost.lost;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001252 return tool->lost(tool, event, sample, machine);
Kan Liangc4937a92015-05-10 15:13:15 -04001253 case PERF_RECORD_LOST_SAMPLES:
1254 if (tool->lost_samples == perf_event__process_lost_samples)
1255 evlist->stats.total_lost_samples += event->lost_samples.lost;
1256 return tool->lost_samples(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001257 case PERF_RECORD_READ:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001258 return tool->read(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001259 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001260 return tool->throttle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001261 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001262 return tool->unthrottle(tool, event, sample, machine);
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001263 case PERF_RECORD_AUX:
Alexander Shishkin05a1f472017-03-16 18:41:59 +02001264 if (tool->aux == perf_event__process_aux) {
1265 if (event->aux.flags & PERF_AUX_FLAG_TRUNCATED)
1266 evlist->stats.total_aux_lost += 1;
1267 if (event->aux.flags & PERF_AUX_FLAG_PARTIAL)
1268 evlist->stats.total_aux_partial += 1;
1269 }
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001270 return tool->aux(tool, event, sample, machine);
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001271 case PERF_RECORD_ITRACE_START:
1272 return tool->itrace_start(tool, event, sample, machine);
Adrian Hunter02860392015-07-21 12:44:03 +03001273 case PERF_RECORD_SWITCH:
1274 case PERF_RECORD_SWITCH_CPU_WIDE:
1275 return tool->context_switch(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001276 default:
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001277 ++evlist->stats.nr_unknown_events;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001278 return -1;
1279 }
1280}
1281
Adrian Hunterc4468702015-04-09 18:53:48 +03001282static int perf_session__deliver_event(struct perf_session *session,
1283 union perf_event *event,
1284 struct perf_sample *sample,
1285 struct perf_tool *tool,
1286 u64 file_offset)
1287{
1288 int ret;
1289
1290 ret = auxtrace__process_event(session, event, sample, tool);
1291 if (ret < 0)
1292 return ret;
1293 if (ret > 0)
1294 return 0;
1295
1296 return machines__deliver_event(&session->machines, session->evlist,
1297 event, sample, tool, file_offset);
1298}
1299
Adrian Hunterd5652d82014-07-23 22:19:58 +03001300static s64 perf_session__process_user_event(struct perf_session *session,
1301 union perf_event *event,
Adrian Hunterd5652d82014-07-23 22:19:58 +03001302 u64 file_offset)
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001303{
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -03001304 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001305 struct perf_tool *tool = session->tool;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001306 int fd = perf_data_file__fd(session->file);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001307 int err;
1308
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001309 dump_event(session->evlist, event, file_offset, NULL);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001310
1311 /* These events are processed right away */
1312 switch (event->header.type) {
1313 case PERF_RECORD_HEADER_ATTR:
Adrian Hunter47c3d102013-07-04 16:20:21 +03001314 err = tool->attr(tool, event, &session->evlist);
Adrian Huntercfe1c412014-07-31 09:00:45 +03001315 if (err == 0) {
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -03001316 perf_session__set_id_hdr_size(session);
Adrian Huntercfe1c412014-07-31 09:00:45 +03001317 perf_session__set_comm_exec(session);
1318 }
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001319 return err;
Jiri Olsaffe777252015-10-25 15:51:36 +01001320 case PERF_RECORD_EVENT_UPDATE:
1321 return tool->event_update(tool, event, &session->evlist);
Jiri Olsaf67697b2014-02-04 15:37:48 +01001322 case PERF_RECORD_HEADER_EVENT_TYPE:
1323 /*
1324 * Depreceated, but we need to handle it for sake
1325 * of old data files create in pipe mode.
1326 */
1327 return 0;
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001328 case PERF_RECORD_HEADER_TRACING_DATA:
1329 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001330 lseek(fd, file_offset, SEEK_SET);
Adrian Hunter47c3d102013-07-04 16:20:21 +03001331 return tool->tracing_data(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001332 case PERF_RECORD_HEADER_BUILD_ID:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001333 return tool->build_id(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001334 case PERF_RECORD_FINISHED_ROUND:
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -03001335 return tool->finished_round(tool, event, oe);
Adrian Hunter3c659ee2014-10-27 15:49:22 +02001336 case PERF_RECORD_ID_INDEX:
1337 return tool->id_index(tool, event, session);
Adrian Huntera16ac022015-04-09 18:53:43 +03001338 case PERF_RECORD_AUXTRACE_INFO:
1339 return tool->auxtrace_info(tool, event, session);
1340 case PERF_RECORD_AUXTRACE:
1341 /* setup for reading amidst mmap */
1342 lseek(fd, file_offset + event->header.size, SEEK_SET);
1343 return tool->auxtrace(tool, event, session);
Adrian Huntere9bf54d2015-04-09 18:53:47 +03001344 case PERF_RECORD_AUXTRACE_ERROR:
Adrian Hunter85ed4722015-04-09 18:53:50 +03001345 perf_session__auxtrace_error_inc(session, event);
Adrian Huntere9bf54d2015-04-09 18:53:47 +03001346 return tool->auxtrace_error(tool, event, session);
Jiri Olsa5f3339d2015-10-25 15:51:19 +01001347 case PERF_RECORD_THREAD_MAP:
1348 return tool->thread_map(tool, event, session);
Jiri Olsa6640b6c2015-10-25 15:51:23 +01001349 case PERF_RECORD_CPU_MAP:
1350 return tool->cpu_map(tool, event, session);
Jiri Olsa374fb9e2015-10-25 15:51:27 +01001351 case PERF_RECORD_STAT_CONFIG:
1352 return tool->stat_config(tool, event, session);
Jiri Olsad80518c2015-10-25 15:51:30 +01001353 case PERF_RECORD_STAT:
1354 return tool->stat(tool, event, session);
Jiri Olsa2d8f0f12015-10-25 15:51:33 +01001355 case PERF_RECORD_STAT_ROUND:
1356 return tool->stat_round(tool, event, session);
Adrian Hunter46bc29b2016-03-08 10:38:44 +02001357 case PERF_RECORD_TIME_CONV:
1358 session->time_conv = event->time_conv;
1359 return tool->time_conv(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001360 default:
1361 return -EINVAL;
1362 }
1363}
1364
Adrian Huntera2938292014-10-27 15:49:23 +02001365int perf_session__deliver_synth_event(struct perf_session *session,
1366 union perf_event *event,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001367 struct perf_sample *sample)
Adrian Huntera2938292014-10-27 15:49:23 +02001368{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001369 struct perf_evlist *evlist = session->evlist;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001370 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001371
1372 events_stats__inc(&evlist->stats, event->header.type);
Adrian Huntera2938292014-10-27 15:49:23 +02001373
1374 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001375 return perf_session__process_user_event(session, event, 0);
Adrian Huntera2938292014-10-27 15:49:23 +02001376
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001377 return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0);
Adrian Huntera2938292014-10-27 15:49:23 +02001378}
1379
Jiri Olsa268fb202012-05-30 14:23:43 +02001380static void event_swap(union perf_event *event, bool sample_id_all)
1381{
1382 perf_event__swap_op swap;
1383
1384 swap = perf_event__swap_ops[event->header.type];
1385 if (swap)
1386 swap(event, sample_id_all);
1387}
1388
Adrian Hunter5a52f332014-07-31 09:00:57 +03001389int perf_session__peek_event(struct perf_session *session, off_t file_offset,
1390 void *buf, size_t buf_sz,
1391 union perf_event **event_ptr,
1392 struct perf_sample *sample)
1393{
1394 union perf_event *event;
1395 size_t hdr_sz, rest;
1396 int fd;
1397
1398 if (session->one_mmap && !session->header.needs_swap) {
1399 event = file_offset - session->one_mmap_offset +
1400 session->one_mmap_addr;
1401 goto out_parse_sample;
1402 }
1403
1404 if (perf_data_file__is_pipe(session->file))
1405 return -1;
1406
1407 fd = perf_data_file__fd(session->file);
1408 hdr_sz = sizeof(struct perf_event_header);
1409
1410 if (buf_sz < hdr_sz)
1411 return -1;
1412
1413 if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 ||
Adrian Hunter554e92e2015-05-19 16:05:45 +03001414 readn(fd, buf, hdr_sz) != (ssize_t)hdr_sz)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001415 return -1;
1416
1417 event = (union perf_event *)buf;
1418
1419 if (session->header.needs_swap)
1420 perf_event_header__bswap(&event->header);
1421
Adrian Hunter554e92e2015-05-19 16:05:45 +03001422 if (event->header.size < hdr_sz || event->header.size > buf_sz)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001423 return -1;
1424
1425 rest = event->header.size - hdr_sz;
1426
Adrian Hunter554e92e2015-05-19 16:05:45 +03001427 if (readn(fd, buf, rest) != (ssize_t)rest)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001428 return -1;
1429
1430 if (session->header.needs_swap)
1431 event_swap(event, perf_evlist__sample_id_all(session->evlist));
1432
1433out_parse_sample:
1434
1435 if (sample && event->header.type < PERF_RECORD_USER_TYPE_START &&
1436 perf_evlist__parse_sample(session->evlist, event, sample))
1437 return -1;
1438
1439 *event_ptr = event;
1440
1441 return 0;
1442}
1443
Adrian Hunterd5652d82014-07-23 22:19:58 +03001444static s64 perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001445 union perf_event *event, u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001446{
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001447 struct perf_evlist *evlist = session->evlist;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001448 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001449 struct perf_sample sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001450 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001451
Jiri Olsa268fb202012-05-30 14:23:43 +02001452 if (session->header.needs_swap)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001453 event_swap(event, perf_evlist__sample_id_all(evlist));
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001454
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001455 if (event->header.type >= PERF_RECORD_HEADER_MAX)
1456 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001457
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001458 events_stats__inc(&evlist->stats, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001459
1460 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001461 return perf_session__process_user_event(session, event, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001462
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001463 /*
1464 * For all kernel events we get the sample data
1465 */
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001466 ret = perf_evlist__parse_sample(evlist, event, &sample);
Frederic Weisbecker5538bec2011-05-22 02:17:22 +02001467 if (ret)
1468 return ret;
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001469
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001470 if (tool->ordered_events) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001471 ret = perf_session__queue_event(session, event, &sample, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001472 if (ret != -ETIME)
1473 return ret;
1474 }
1475
Adrian Hunterc4468702015-04-09 18:53:48 +03001476 return perf_session__deliver_event(session, event, &sample, tool,
1477 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001478}
1479
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001480void perf_event_header__bswap(struct perf_event_header *hdr)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001481{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001482 hdr->type = bswap_32(hdr->type);
1483 hdr->misc = bswap_16(hdr->misc);
1484 hdr->size = bswap_16(hdr->size);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001485}
1486
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001487struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1488{
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001489 return machine__findnew_thread(&session->machines.host, -1, pid);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001490}
1491
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001492int perf_session__register_idle_thread(struct perf_session *session)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001493{
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001494 struct thread *thread;
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001495 int err = 0;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001496
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001497 thread = machine__findnew_thread(&session->machines.host, 0, 0);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001498 if (thread == NULL || thread__set_comm(thread, "swapper", 0)) {
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001499 pr_err("problem inserting idle task.\n");
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001500 err = -1;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001501 }
1502
Hari Bathinif3b36142017-03-08 02:11:43 +05301503 if (thread == NULL || thread__set_namespaces(thread, 0, NULL)) {
1504 pr_err("problem inserting idle task.\n");
1505 err = -1;
1506 }
1507
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001508 /* machine__findnew_thread() got the thread, so put it */
1509 thread__put(thread);
1510 return err;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001511}
1512
Wang Nanf06149c2016-07-14 08:34:46 +00001513static void
1514perf_session__warn_order(const struct perf_session *session)
1515{
1516 const struct ordered_events *oe = &session->ordered_events;
1517 struct perf_evsel *evsel;
1518 bool should_warn = true;
1519
1520 evlist__for_each_entry(session->evlist, evsel) {
1521 if (evsel->attr.write_backward)
1522 should_warn = false;
1523 }
1524
1525 if (!should_warn)
1526 return;
1527 if (oe->nr_unordered_events != 0)
1528 ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events);
1529}
1530
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001531static void perf_session__warn_about_errors(const struct perf_session *session)
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001532{
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001533 const struct events_stats *stats = &session->evlist->stats;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001534
1535 if (session->tool->lost == perf_event__process_lost &&
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001536 stats->nr_events[PERF_RECORD_LOST] != 0) {
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001537 ui__warning("Processed %d events and lost %d chunks!\n\n"
1538 "Check IO/CPU overload!\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001539 stats->nr_events[0],
1540 stats->nr_events[PERF_RECORD_LOST]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001541 }
1542
Kan Liangc4937a92015-05-10 15:13:15 -04001543 if (session->tool->lost_samples == perf_event__process_lost_samples) {
1544 double drop_rate;
1545
1546 drop_rate = (double)stats->total_lost_samples /
1547 (double) (stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples);
1548 if (drop_rate > 0.05) {
He Kuang6ba29c22015-06-11 12:44:24 +00001549 ui__warning("Processed %" PRIu64 " samples and lost %3.2f%% samples!\n\n",
Kan Liangc4937a92015-05-10 15:13:15 -04001550 stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples,
1551 drop_rate * 100.0);
1552 }
1553 }
1554
Adrian Huntera38f48e2015-09-25 16:15:37 +03001555 if (session->tool->aux == perf_event__process_aux &&
1556 stats->total_aux_lost != 0) {
1557 ui__warning("AUX data lost %" PRIu64 " times out of %u!\n\n",
1558 stats->total_aux_lost,
1559 stats->nr_events[PERF_RECORD_AUX]);
1560 }
1561
Alexander Shishkin05a1f472017-03-16 18:41:59 +02001562 if (session->tool->aux == perf_event__process_aux &&
1563 stats->total_aux_partial != 0) {
1564 bool vmm_exclusive = false;
1565
1566 (void)sysfs__read_bool("module/kvm_intel/parameters/vmm_exclusive",
1567 &vmm_exclusive);
1568
1569 ui__warning("AUX data had gaps in it %" PRIu64 " times out of %u!\n\n"
1570 "Are you running a KVM guest in the background?%s\n\n",
1571 stats->total_aux_partial,
1572 stats->nr_events[PERF_RECORD_AUX],
1573 vmm_exclusive ?
1574 "\nReloading kvm_intel module with vmm_exclusive=0\n"
1575 "will reduce the gaps to only guest's timeslices." :
1576 "");
1577 }
1578
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001579 if (stats->nr_unknown_events != 0) {
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001580 ui__warning("Found %u unknown events!\n\n"
1581 "Is this an older tool processing a perf.data "
1582 "file generated by a more recent tool?\n\n"
1583 "If that is not the case, consider "
1584 "reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001585 stats->nr_unknown_events);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001586 }
1587
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001588 if (stats->nr_unknown_id != 0) {
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001589 ui__warning("%u samples with id not present in the header\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001590 stats->nr_unknown_id);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001591 }
1592
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001593 if (stats->nr_invalid_chains != 0) {
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001594 ui__warning("Found invalid callchains!\n\n"
1595 "%u out of %u events were discarded for this reason.\n\n"
1596 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001597 stats->nr_invalid_chains,
1598 stats->nr_events[PERF_RECORD_SAMPLE]);
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001599 }
Joerg Roedel0c095712012-02-10 18:05:04 +01001600
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001601 if (stats->nr_unprocessable_samples != 0) {
Joerg Roedel0c095712012-02-10 18:05:04 +01001602 ui__warning("%u unprocessable samples recorded.\n"
1603 "Do you have a KVM guest running and not using 'perf kvm'?\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001604 stats->nr_unprocessable_samples);
Joerg Roedel0c095712012-02-10 18:05:04 +01001605 }
Jiri Olsaf61ff6c2014-11-26 16:39:31 +01001606
Wang Nanf06149c2016-07-14 08:34:46 +00001607 perf_session__warn_order(session);
Adrian Hunter85ed4722015-04-09 18:53:50 +03001608
1609 events_stats__auxtrace_error_warn(stats);
Kan Liang930e6fc2015-06-17 09:51:10 -04001610
1611 if (stats->nr_proc_map_timeout != 0) {
1612 ui__warning("%d map information files for pre-existing threads were\n"
1613 "not processed, if there are samples for addresses they\n"
1614 "will not be resolved, you may find out which are these\n"
1615 "threads by running with -v and redirecting the output\n"
Kan Liang9d9cad72015-06-17 09:51:11 -04001616 "to a file.\n"
1617 "The time limit to process proc map is too short?\n"
1618 "Increase it by --proc-map-timeout\n",
Kan Liang930e6fc2015-06-17 09:51:10 -04001619 stats->nr_proc_map_timeout);
1620 }
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001621}
1622
Adrian Huntera5499b32015-05-29 16:33:30 +03001623static int perf_session__flush_thread_stack(struct thread *thread,
1624 void *p __maybe_unused)
1625{
1626 return thread_stack__flush(thread);
1627}
1628
1629static int perf_session__flush_thread_stacks(struct perf_session *session)
1630{
1631 return machines__for_each_thread(&session->machines,
1632 perf_session__flush_thread_stack,
1633 NULL);
1634}
1635
Tom Zanussi8dc58102010-04-01 23:59:15 -05001636volatile int session_done;
1637
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001638static int __perf_session__process_pipe_events(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05001639{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001640 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001641 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001642 int fd = perf_data_file__fd(session->file);
Stephane Eranian444d2862012-05-15 13:28:12 +02001643 union perf_event *event;
1644 uint32_t size, cur_size = 0;
1645 void *buf = NULL;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001646 s64 skip = 0;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001647 u64 head;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001648 ssize_t err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001649 void *p;
1650
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001651 perf_tool__fill_defaults(tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001652
1653 head = 0;
Stephane Eranian444d2862012-05-15 13:28:12 +02001654 cur_size = sizeof(union perf_event);
1655
1656 buf = malloc(cur_size);
1657 if (!buf)
1658 return -errno;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001659more:
Stephane Eranian444d2862012-05-15 13:28:12 +02001660 event = buf;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001661 err = readn(fd, event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05001662 if (err <= 0) {
1663 if (err == 0)
1664 goto done;
1665
1666 pr_err("failed to read event header\n");
1667 goto out_err;
1668 }
1669
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001670 if (session->header.needs_swap)
Stephane Eranian444d2862012-05-15 13:28:12 +02001671 perf_event_header__bswap(&event->header);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001672
Stephane Eranian444d2862012-05-15 13:28:12 +02001673 size = event->header.size;
Adrian Hunter27389d72013-07-04 16:20:27 +03001674 if (size < sizeof(struct perf_event_header)) {
1675 pr_err("bad event header size\n");
1676 goto out_err;
1677 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001678
Stephane Eranian444d2862012-05-15 13:28:12 +02001679 if (size > cur_size) {
1680 void *new = realloc(buf, size);
1681 if (!new) {
1682 pr_err("failed to allocate memory to read event\n");
1683 goto out_err;
1684 }
1685 buf = new;
1686 cur_size = size;
1687 event = buf;
1688 }
1689 p = event;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001690 p += sizeof(struct perf_event_header);
1691
Tom Zanussi794e43b2010-05-05 00:27:40 -05001692 if (size - sizeof(struct perf_event_header)) {
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001693 err = readn(fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -05001694 if (err <= 0) {
1695 if (err == 0) {
1696 pr_err("unexpected end of event stream\n");
1697 goto done;
1698 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001699
Tom Zanussi794e43b2010-05-05 00:27:40 -05001700 pr_err("failed to read event data\n");
1701 goto out_err;
1702 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001703 }
1704
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001705 if ((skip = perf_session__process_event(session, event, head)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001706 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
Stephane Eranian444d2862012-05-15 13:28:12 +02001707 head, event->header.size, event->header.type);
Jiri Olsa9389a462012-04-16 20:42:51 +02001708 err = -EINVAL;
1709 goto out_err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001710 }
1711
1712 head += size;
1713
Tom Zanussi8dc58102010-04-01 23:59:15 -05001714 if (skip > 0)
1715 head += skip;
1716
1717 if (!session_done())
1718 goto more;
1719done:
Adrian Hunter8c16b642013-10-18 15:29:02 +03001720 /* do the final flush for ordered samples */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001721 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
Adrian Hunterc4468702015-04-09 18:53:48 +03001722 if (err)
1723 goto out_err;
1724 err = auxtrace__flush_events(session, tool);
Adrian Huntera5499b32015-05-29 16:33:30 +03001725 if (err)
1726 goto out_err;
1727 err = perf_session__flush_thread_stacks(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001728out_err:
Stephane Eranian444d2862012-05-15 13:28:12 +02001729 free(buf);
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001730 perf_session__warn_about_errors(session);
Jiri Olsaadc56ed2014-06-10 22:50:03 +02001731 ordered_events__free(&session->ordered_events);
Adrian Hunterc4468702015-04-09 18:53:48 +03001732 auxtrace__free_events(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001733 return err;
1734}
1735
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001736static union perf_event *
1737fetch_mmaped_event(struct perf_session *session,
1738 u64 head, size_t mmap_size, char *buf)
1739{
1740 union perf_event *event;
1741
1742 /*
1743 * Ensure we have enough space remaining to read
1744 * the size of the event in the headers.
1745 */
1746 if (head + sizeof(event->header) > mmap_size)
1747 return NULL;
1748
1749 event = (union perf_event *)(buf + head);
1750
1751 if (session->header.needs_swap)
1752 perf_event_header__bswap(&event->header);
1753
Adrian Hunter27389d72013-07-04 16:20:27 +03001754 if (head + event->header.size > mmap_size) {
1755 /* We're not fetching the event so swap back again */
1756 if (session->header.needs_swap)
1757 perf_event_header__bswap(&event->header);
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001758 return NULL;
Adrian Hunter27389d72013-07-04 16:20:27 +03001759 }
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001760
1761 return event;
1762}
1763
David Miller35d48dd2012-11-10 14:12:19 -05001764/*
1765 * On 64bit we can mmap the data file in one go. No need for tiny mmap
1766 * slices. On 32bit we use 32MB.
1767 */
1768#if BITS_PER_LONG == 64
1769#define MMAP_SIZE ULLONG_MAX
1770#define NUM_MMAPS 1
1771#else
1772#define MMAP_SIZE (32 * 1024 * 1024ULL)
1773#define NUM_MMAPS 128
1774#endif
1775
Namhyung Kim4ac30cf2015-01-29 17:06:43 +09001776static int __perf_session__process_events(struct perf_session *session,
1777 u64 data_offset, u64 data_size,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001778 u64 file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001779{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001780 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001781 struct perf_tool *tool = session->tool;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001782 int fd = perf_data_file__fd(session->file);
Adrian Hunterd5652d82014-07-23 22:19:58 +03001783 u64 head, page_offset, file_offset, file_pos, size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001784 int err, mmap_prot, mmap_flags, map_idx = 0;
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -03001785 size_t mmap_size;
David Miller35d48dd2012-11-10 14:12:19 -05001786 char *buf, *mmaps[NUM_MMAPS];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001787 union perf_event *event;
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001788 struct ui_progress prog;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001789 s64 skip;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001790
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001791 perf_tool__fill_defaults(tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001792
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001793 page_offset = page_size * (data_offset / page_size);
1794 file_offset = page_offset;
1795 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001796
Mark Rutland381c02f2015-09-16 18:18:49 +01001797 if (data_size == 0)
1798 goto out;
1799
1800 if (data_offset + data_size < file_size)
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001801 file_size = data_offset + data_size;
1802
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001803 ui_progress__init(&prog, file_size, "Processing events...");
Thomas Gleixner55b446292010-11-30 17:49:46 +00001804
David Miller35d48dd2012-11-10 14:12:19 -05001805 mmap_size = MMAP_SIZE;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001806 if (mmap_size > file_size) {
Thomas Gleixner55b446292010-11-30 17:49:46 +00001807 mmap_size = file_size;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001808 session->one_mmap = true;
1809 }
Thomas Gleixner55b446292010-11-30 17:49:46 +00001810
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001811 memset(mmaps, 0, sizeof(mmaps));
1812
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001813 mmap_prot = PROT_READ;
1814 mmap_flags = MAP_SHARED;
1815
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001816 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001817 mmap_prot |= PROT_WRITE;
1818 mmap_flags = MAP_PRIVATE;
1819 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001820remap:
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001821 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd,
Thomas Gleixner55b446292010-11-30 17:49:46 +00001822 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001823 if (buf == MAP_FAILED) {
1824 pr_err("failed to mmap file\n");
1825 err = -errno;
1826 goto out_err;
1827 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001828 mmaps[map_idx] = buf;
1829 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001830 file_pos = file_offset + head;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001831 if (session->one_mmap) {
1832 session->one_mmap_addr = buf;
1833 session->one_mmap_offset = file_offset;
1834 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001835
1836more:
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001837 event = fetch_mmaped_event(session, head, mmap_size, buf);
1838 if (!event) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001839 if (mmaps[map_idx]) {
1840 munmap(mmaps[map_idx], mmap_size);
1841 mmaps[map_idx] = NULL;
1842 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001843
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001844 page_offset = page_size * (head / page_size);
1845 file_offset += page_offset;
1846 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001847 goto remap;
1848 }
1849
1850 size = event->header.size;
1851
Adrian Hunter27389d72013-07-04 16:20:27 +03001852 if (size < sizeof(struct perf_event_header) ||
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001853 (skip = perf_session__process_event(session, event, file_pos)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001854 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1855 file_offset + head, event->header.size,
1856 event->header.type);
1857 err = -EINVAL;
1858 goto out_err;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001859 }
1860
Adrian Hunter6f917c72014-07-23 22:19:57 +03001861 if (skip)
1862 size += skip;
1863
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001864 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001865 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001866
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001867 ui_progress__update(&prog, size);
Thomas Gleixner55b446292010-11-30 17:49:46 +00001868
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001869 if (session_done())
Adrian Hunter8c16b642013-10-18 15:29:02 +03001870 goto out;
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001871
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001872 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001873 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001874
Adrian Hunter8c16b642013-10-18 15:29:02 +03001875out:
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001876 /* do the final flush for ordered samples */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001877 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
Adrian Hunterc4468702015-04-09 18:53:48 +03001878 if (err)
1879 goto out_err;
1880 err = auxtrace__flush_events(session, tool);
Adrian Huntera5499b32015-05-29 16:33:30 +03001881 if (err)
1882 goto out_err;
1883 err = perf_session__flush_thread_stacks(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001884out_err:
Namhyung Kima5580f32012-11-13 22:30:34 +09001885 ui_progress__finish();
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001886 perf_session__warn_about_errors(session);
Wang Nanb26dc732016-04-13 08:21:04 +00001887 /*
1888 * We may switching perf.data output, make ordered_events
1889 * reusable.
1890 */
1891 ordered_events__reinit(&session->ordered_events);
Adrian Hunterc4468702015-04-09 18:53:48 +03001892 auxtrace__free_events(session);
Adrian Hunter919d86d2014-07-14 13:02:51 +03001893 session->one_mmap = false;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001894 return err;
1895}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001896
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001897int perf_session__process_events(struct perf_session *session)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001898{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001899 u64 size = perf_data_file__size(session->file);
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001900 int err;
1901
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001902 if (perf_session__register_idle_thread(session) < 0)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001903 return -ENOMEM;
1904
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001905 if (!perf_data_file__is_pipe(session->file))
1906 err = __perf_session__process_events(session,
1907 session->header.data_offset,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001908 session->header.data_size, size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001909 else
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001910 err = __perf_session__process_pipe_events(session);
Dave Martin88ca8952010-07-27 11:46:12 -03001911
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001912 return err;
1913}
1914
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001915bool perf_session__has_traces(struct perf_session *session, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001916{
David Ahern93ea01c292013-08-07 22:50:58 -04001917 struct perf_evsel *evsel;
1918
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001919 evlist__for_each_entry(session->evlist, evsel) {
David Ahern93ea01c292013-08-07 22:50:58 -04001920 if (evsel->attr.type == PERF_TYPE_TRACEPOINT)
1921 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001922 }
1923
David Ahern93ea01c292013-08-07 22:50:58 -04001924 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1925 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001926}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001927
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001928int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1929 const char *symbol_name, u64 addr)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001930{
1931 char *bracket;
Arnaldo Carvalho de Meloa0b2f5a2017-02-14 16:19:56 -03001932 int i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001933 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001934
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001935 ref = zalloc(sizeof(struct ref_reloc_sym));
1936 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001937 return -ENOMEM;
1938
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001939 ref->name = strdup(symbol_name);
1940 if (ref->name == NULL) {
1941 free(ref);
1942 return -ENOMEM;
1943 }
1944
1945 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001946 if (bracket)
1947 *bracket = '\0';
1948
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001949 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001950
1951 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001952 struct kmap *kmap = map__kmap(maps[i]);
Wang Nanba927322015-04-07 08:22:45 +00001953
1954 if (!kmap)
1955 continue;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001956 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001957 }
1958
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001959 return 0;
1960}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001961
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001962size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001963{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001964 return machines__fprintf_dsos(&session->machines, fp);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001965}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001966
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001967size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001968 bool (skip)(struct dso *dso, int parm), int parm)
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001969{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001970 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001971}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001972
1973size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1974{
Adrian Hunterc4468702015-04-09 18:53:48 +03001975 size_t ret;
1976 const char *msg = "";
1977
1978 if (perf_header__has_feat(&session->header, HEADER_AUXTRACE))
1979 msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)";
1980
Adrian Hunterfe692ac2015-06-23 10:52:49 +03001981 ret = fprintf(fp, "\nAggregated stats:%s\n", msg);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001982
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001983 ret += events_stats__fprintf(&session->evlist->stats, fp);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001984 return ret;
1985}
David Ahernc0230b22011-03-09 22:23:27 -07001986
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001987size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1988{
1989 /*
1990 * FIXME: Here we have to actually print all the machines in this
1991 * session, not just the host...
1992 */
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001993 return machine__fprintf(&session->machines.host, fp);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001994}
1995
David Ahern9cbdb702011-04-06 21:54:20 -06001996struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1997 unsigned int type)
1998{
1999 struct perf_evsel *pos;
2000
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002001 evlist__for_each_entry(session->evlist, pos) {
David Ahern9cbdb702011-04-06 21:54:20 -06002002 if (pos->attr.type == type)
2003 return pos;
2004 }
2005 return NULL;
2006}
2007
Anton Blanchard5d67be92011-07-04 21:57:50 +10002008int perf_session__cpu_bitmap(struct perf_session *session,
2009 const char *cpu_list, unsigned long *cpu_bitmap)
2010{
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04002011 int i, err = -1;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002012 struct cpu_map *map;
2013
2014 for (i = 0; i < PERF_TYPE_MAX; ++i) {
2015 struct perf_evsel *evsel;
2016
2017 evsel = perf_session__find_first_evtype(session, i);
2018 if (!evsel)
2019 continue;
2020
2021 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
2022 pr_err("File does not contain CPU events. "
2023 "Remove -c option to proceed.\n");
2024 return -1;
2025 }
2026 }
2027
2028 map = cpu_map__new(cpu_list);
David Ahern47fbe532011-11-13 10:45:27 -07002029 if (map == NULL) {
2030 pr_err("Invalid cpu_list\n");
2031 return -1;
2032 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10002033
2034 for (i = 0; i < map->nr; i++) {
2035 int cpu = map->map[i];
2036
2037 if (cpu >= MAX_NR_CPUS) {
2038 pr_err("Requested CPU %d too large. "
2039 "Consider raising MAX_NR_CPUS\n", cpu);
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04002040 goto out_delete_map;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002041 }
2042
2043 set_bit(cpu, cpu_bitmap);
2044 }
2045
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04002046 err = 0;
2047
2048out_delete_map:
Jiri Olsaf30a79b2015-06-23 00:36:04 +02002049 cpu_map__put(map);
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04002050 return err;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002051}
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002052
2053void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
2054 bool full)
2055{
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002056 if (session == NULL || fp == NULL)
2057 return;
2058
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002059 fprintf(fp, "# ========\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002060 perf_header__fprintf_info(session, fp, full);
2061 fprintf(fp, "# ========\n#\n");
2062}
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002063
2064
2065int __perf_session__set_tracepoints_handlers(struct perf_session *session,
2066 const struct perf_evsel_str_handler *assocs,
2067 size_t nr_assocs)
2068{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002069 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002070 size_t i;
2071 int err;
2072
2073 for (i = 0; i < nr_assocs; i++) {
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03002074 /*
2075 * Adding a handler for an event not in the session,
2076 * just ignore it.
2077 */
2078 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name);
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002079 if (evsel == NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03002080 continue;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002081
2082 err = -EEXIST;
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002083 if (evsel->handler != NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03002084 goto out;
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002085 evsel->handler = assocs[i].handler;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002086 }
2087
2088 err = 0;
2089out:
2090 return err;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002091}
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002092
2093int perf_event__process_id_index(struct perf_tool *tool __maybe_unused,
2094 union perf_event *event,
2095 struct perf_session *session)
2096{
2097 struct perf_evlist *evlist = session->evlist;
2098 struct id_index_event *ie = &event->id_index;
2099 size_t i, nr, max_nr;
2100
2101 max_nr = (ie->header.size - sizeof(struct id_index_event)) /
2102 sizeof(struct id_index_entry);
2103 nr = ie->nr;
2104 if (nr > max_nr)
2105 return -EINVAL;
2106
2107 if (dump_trace)
2108 fprintf(stdout, " nr: %zu\n", nr);
2109
2110 for (i = 0; i < nr; i++) {
2111 struct id_index_entry *e = &ie->entries[i];
2112 struct perf_sample_id *sid;
2113
2114 if (dump_trace) {
2115 fprintf(stdout, " ... id: %"PRIu64, e->id);
2116 fprintf(stdout, " idx: %"PRIu64, e->idx);
2117 fprintf(stdout, " cpu: %"PRId64, e->cpu);
2118 fprintf(stdout, " tid: %"PRId64"\n", e->tid);
2119 }
2120
2121 sid = perf_evlist__id2sid(evlist, e->id);
2122 if (!sid)
2123 return -ENOENT;
2124 sid->idx = e->idx;
2125 sid->cpu = e->cpu;
2126 sid->tid = e->tid;
2127 }
2128 return 0;
2129}
2130
2131int perf_event__synthesize_id_index(struct perf_tool *tool,
2132 perf_event__handler_t process,
2133 struct perf_evlist *evlist,
2134 struct machine *machine)
2135{
2136 union perf_event *ev;
2137 struct perf_evsel *evsel;
2138 size_t nr = 0, i = 0, sz, max_nr, n;
2139 int err;
2140
2141 pr_debug2("Synthesizing id index\n");
2142
2143 max_nr = (UINT16_MAX - sizeof(struct id_index_event)) /
2144 sizeof(struct id_index_entry);
2145
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002146 evlist__for_each_entry(evlist, evsel)
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002147 nr += evsel->ids;
2148
2149 n = nr > max_nr ? max_nr : nr;
2150 sz = sizeof(struct id_index_event) + n * sizeof(struct id_index_entry);
2151 ev = zalloc(sz);
2152 if (!ev)
2153 return -ENOMEM;
2154
2155 ev->id_index.header.type = PERF_RECORD_ID_INDEX;
2156 ev->id_index.header.size = sz;
2157 ev->id_index.nr = n;
2158
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002159 evlist__for_each_entry(evlist, evsel) {
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002160 u32 j;
2161
2162 for (j = 0; j < evsel->ids; j++) {
2163 struct id_index_entry *e;
2164 struct perf_sample_id *sid;
2165
2166 if (i >= n) {
2167 err = process(tool, ev, NULL, machine);
2168 if (err)
2169 goto out_err;
2170 nr -= n;
2171 i = 0;
2172 }
2173
2174 e = &ev->id_index.entries[i++];
2175
2176 e->id = evsel->id[j];
2177
2178 sid = perf_evlist__id2sid(evlist, e->id);
2179 if (!sid) {
2180 free(ev);
2181 return -ENOENT;
2182 }
2183
2184 e->idx = sid->idx;
2185 e->cpu = sid->cpu;
2186 e->tid = sid->tid;
2187 }
2188 }
2189
2190 sz = sizeof(struct id_index_event) + nr * sizeof(struct id_index_entry);
2191 ev->id_index.header.size = sz;
2192 ev->id_index.nr = nr;
2193
2194 err = process(tool, ev, NULL, machine);
2195out_err:
2196 free(ev);
2197
2198 return err;
2199}