blob: d1a43a322f966967fc20fd30ede9e7c1030afd19 [file] [log] [blame]
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001#include <linux/kernel.h>
Robert Richter4e319022013-06-11 17:29:18 +02002#include <traceevent/event-parse.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02003
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02004#include <byteswap.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02005#include <unistd.h>
6#include <sys/types.h>
Arnaldo Carvalho de Meloa41794c2010-05-18 18:29:23 -03007#include <sys/mman.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02008
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03009#include "evlist.h"
10#include "evsel.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020011#include "session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020012#include "tool.h"
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -020013#include "sort.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020014#include "util.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100015#include "cpumap.h"
Jiri Olsa0f6a3012012-08-07 15:20:45 +020016#include "perf_regs.h"
Jiri Olsab0a45202014-06-12 09:50:11 +020017#include "asm/bug.h"
Adrian Hunterc4468702015-04-09 18:53:48 +030018#include "auxtrace.h"
Adrian Huntera5499b32015-05-29 16:33:30 +030019#include "thread-stack.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020020
Adrian Hunterc4468702015-04-09 18:53:48 +030021static int perf_session__deliver_event(struct perf_session *session,
22 union perf_event *event,
23 struct perf_sample *sample,
24 struct perf_tool *tool,
25 u64 file_offset);
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -030026
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030027static int perf_session__open(struct perf_session *session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020028{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030029 struct perf_data_file *file = session->file;
Tom Zanussi8dc58102010-04-01 23:59:15 -050030
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030031 if (perf_session__read_header(session) < 0) {
Stephane Eranian69996df2012-02-09 23:21:06 +010032 pr_err("incompatible file format (rerun with -v to learn more)");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020033 return -1;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020034 }
35
Jiri Olsacc9784bd2013-10-15 16:27:34 +020036 if (perf_data_file__is_pipe(file))
37 return 0;
38
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030039 if (!perf_evlist__valid_sample_type(session->evlist)) {
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030040 pr_err("non matching sample_type");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020041 return -1;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030042 }
43
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030044 if (!perf_evlist__valid_sample_id_all(session->evlist)) {
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030045 pr_err("non matching sample_id_all");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020046 return -1;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030047 }
48
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030049 if (!perf_evlist__valid_read_format(session->evlist)) {
Jiri Olsa9ede4732012-10-10 17:38:13 +020050 pr_err("non matching read_format");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020051 return -1;
Jiri Olsa9ede4732012-10-10 17:38:13 +020052 }
53
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020054 return 0;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020055}
56
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030057void perf_session__set_id_hdr_size(struct perf_session *session)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020058{
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030059 u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist);
60
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030061 machines__set_id_hdr_size(&session->machines, id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020062}
63
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030064int perf_session__create_kernel_maps(struct perf_session *session)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080065{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030066 int ret = machine__create_kernel_maps(&session->machines.host);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080067
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080068 if (ret >= 0)
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030069 ret = machines__create_guest_kernel_maps(&session->machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080070 return ret;
71}
72
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030073static void perf_session__destroy_kernel_maps(struct perf_session *session)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -030074{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030075 machines__destroy_kernel_maps(&session->machines);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -030076}
77
Adrian Huntercfe1c412014-07-31 09:00:45 +030078static bool perf_session__has_comm_exec(struct perf_session *session)
79{
80 struct perf_evsel *evsel;
81
82 evlist__for_each(session->evlist, evsel) {
83 if (evsel->attr.comm_exec)
84 return true;
85 }
86
87 return false;
88}
89
90static void perf_session__set_comm_exec(struct perf_session *session)
91{
92 bool comm_exec = perf_session__has_comm_exec(session);
93
94 machines__set_comm_exec(&session->machines, comm_exec);
95}
96
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -030097static int ordered_events__deliver_event(struct ordered_events *oe,
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -030098 struct ordered_event *event)
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -030099{
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300100 struct perf_sample sample;
101 struct perf_session *session = container_of(oe, struct perf_session,
102 ordered_events);
103 int ret = perf_evlist__parse_sample(session->evlist, event->event, &sample);
104
105 if (ret) {
106 pr_err("Can't parse sample, err = %d\n", ret);
107 return ret;
108 }
109
Adrian Hunterc4468702015-04-09 18:53:48 +0300110 return perf_session__deliver_event(session, event->event, &sample,
111 session->tool, event->file_offset);
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300112}
113
Jiri Olsaf5fc14122013-10-15 16:27:32 +0200114struct perf_session *perf_session__new(struct perf_data_file *file,
115 bool repipe, struct perf_tool *tool)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200116{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300117 struct perf_session *session = zalloc(sizeof(*session));
Robert Richterefad1412011-12-07 10:02:54 +0100118
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300119 if (!session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200120 goto out;
121
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300122 session->repipe = repipe;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300123 session->tool = tool;
Adrian Hunter99fa2982015-04-30 17:37:25 +0300124 INIT_LIST_HEAD(&session->auxtrace_index);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300125 machines__init(&session->machines);
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300126 ordered_events__init(&session->ordered_events, ordered_events__deliver_event);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200127
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200128 if (file) {
129 if (perf_data_file__open(file))
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200130 goto out_delete;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200131
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300132 session->file = file;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200133
134 if (perf_data_file__is_read(file)) {
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300135 if (perf_session__open(session) < 0)
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200136 goto out_close;
137
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300138 perf_session__set_id_hdr_size(session);
Adrian Huntercfe1c412014-07-31 09:00:45 +0300139 perf_session__set_comm_exec(session);
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200140 }
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -0300141 } else {
142 session->machines.host.env = &perf_env;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200143 }
144
145 if (!file || perf_data_file__is_write(file)) {
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200146 /*
147 * In O_RDONLY mode this will be performed when reading the
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200148 * kernel MMAP event, in perf_event__process_mmap().
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200149 */
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300150 if (perf_session__create_kernel_maps(session) < 0)
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700151 pr_warning("Cannot read kernel map\n");
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200152 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200153
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200154 if (tool && tool->ordering_requires_timestamps &&
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200155 tool->ordered_events && !perf_evlist__sample_id_all(session->evlist)) {
Ian Munsie21ef97f2010-12-10 14:09:16 +1100156 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200157 tool->ordered_events = false;
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300158 }
Ian Munsie21ef97f2010-12-10 14:09:16 +1100159
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300160 return session;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200161
162 out_close:
163 perf_data_file__close(file);
164 out_delete:
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300165 perf_session__delete(session);
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200166 out:
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200167 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200168}
169
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200170static void perf_session__delete_threads(struct perf_session *session)
171{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300172 machine__delete_threads(&session->machines.host);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200173}
174
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300175void perf_session__delete(struct perf_session *session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200176{
Adrian Hunterc4468702015-04-09 18:53:48 +0300177 auxtrace__free(session);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300178 auxtrace_index__free(&session->auxtrace_index);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300179 perf_session__destroy_kernel_maps(session);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300180 perf_session__delete_threads(session);
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -0300181 perf_env__exit(&session->header.env);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300182 machines__exit(&session->machines);
183 if (session->file)
184 perf_data_file__close(session->file);
185 free(session);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200186}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200187
Adrian Hunter47c3d102013-07-04 16:20:21 +0300188static int process_event_synth_tracing_data_stub(struct perf_tool *tool
189 __maybe_unused,
190 union perf_event *event
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300191 __maybe_unused,
192 struct perf_session *session
193 __maybe_unused)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200194{
195 dump_printf(": unhandled!\n");
196 return 0;
197}
198
Adrian Hunter47c3d102013-07-04 16:20:21 +0300199static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused,
200 union perf_event *event __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300201 struct perf_evlist **pevlist
202 __maybe_unused)
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200203{
204 dump_printf(": unhandled!\n");
205 return 0;
206}
207
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300208static int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
209 union perf_event *event __maybe_unused,
210 struct perf_sample *sample __maybe_unused,
211 struct perf_evsel *evsel __maybe_unused,
212 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300213{
214 dump_printf(": unhandled!\n");
215 return 0;
216}
217
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300218static int process_event_stub(struct perf_tool *tool __maybe_unused,
219 union perf_event *event __maybe_unused,
220 struct perf_sample *sample __maybe_unused,
221 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200222{
223 dump_printf(": unhandled!\n");
224 return 0;
225}
226
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300227static int process_build_id_stub(struct perf_tool *tool __maybe_unused,
228 union perf_event *event __maybe_unused,
229 struct perf_session *session __maybe_unused)
230{
231 dump_printf(": unhandled!\n");
232 return 0;
233}
234
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300235static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
236 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300237 struct ordered_events *oe __maybe_unused)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200238{
239 dump_printf(": unhandled!\n");
240 return 0;
241}
242
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200243static int process_finished_round(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200244 union perf_event *event,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300245 struct ordered_events *oe);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200246
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200247static int process_id_index_stub(struct perf_tool *tool __maybe_unused,
248 union perf_event *event __maybe_unused,
249 struct perf_session *perf_session
250 __maybe_unused)
251{
252 dump_printf(": unhandled!\n");
253 return 0;
254}
255
Adrian Huntera16ac022015-04-09 18:53:43 +0300256static int process_event_auxtrace_info_stub(struct perf_tool *tool __maybe_unused,
257 union perf_event *event __maybe_unused,
258 struct perf_session *session __maybe_unused)
259{
260 dump_printf(": unhandled!\n");
261 return 0;
262}
263
264static int skipn(int fd, off_t n)
265{
266 char buf[4096];
267 ssize_t ret;
268
269 while (n > 0) {
270 ret = read(fd, buf, min(n, (off_t)sizeof(buf)));
271 if (ret <= 0)
272 return ret;
273 n -= ret;
274 }
275
276 return 0;
277}
278
279static s64 process_event_auxtrace_stub(struct perf_tool *tool __maybe_unused,
280 union perf_event *event,
281 struct perf_session *session
282 __maybe_unused)
283{
284 dump_printf(": unhandled!\n");
285 if (perf_data_file__is_pipe(session->file))
286 skipn(perf_data_file__fd(session->file), event->auxtrace.size);
287 return event->auxtrace.size;
288}
289
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300290static
291int process_event_auxtrace_error_stub(struct perf_tool *tool __maybe_unused,
292 union perf_event *event __maybe_unused,
293 struct perf_session *session __maybe_unused)
294{
295 dump_printf(": unhandled!\n");
296 return 0;
297}
298
David Ahern9c501402013-08-02 14:05:41 -0600299void perf_tool__fill_defaults(struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200300{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200301 if (tool->sample == NULL)
302 tool->sample = process_event_sample_stub;
303 if (tool->mmap == NULL)
304 tool->mmap = process_event_stub;
David Ahern6adb0b02013-09-22 19:44:59 -0600305 if (tool->mmap2 == NULL)
306 tool->mmap2 = process_event_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200307 if (tool->comm == NULL)
308 tool->comm = process_event_stub;
309 if (tool->fork == NULL)
310 tool->fork = process_event_stub;
311 if (tool->exit == NULL)
312 tool->exit = process_event_stub;
313 if (tool->lost == NULL)
314 tool->lost = perf_event__process_lost;
Kan Liangc4937a92015-05-10 15:13:15 -0400315 if (tool->lost_samples == NULL)
316 tool->lost_samples = perf_event__process_lost_samples;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300317 if (tool->aux == NULL)
318 tool->aux = perf_event__process_aux;
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300319 if (tool->itrace_start == NULL)
320 tool->itrace_start = perf_event__process_itrace_start;
Adrian Hunter02860392015-07-21 12:44:03 +0300321 if (tool->context_switch == NULL)
322 tool->context_switch = perf_event__process_switch;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200323 if (tool->read == NULL)
324 tool->read = process_event_sample_stub;
325 if (tool->throttle == NULL)
326 tool->throttle = process_event_stub;
327 if (tool->unthrottle == NULL)
328 tool->unthrottle = process_event_stub;
329 if (tool->attr == NULL)
330 tool->attr = process_event_synth_attr_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200331 if (tool->tracing_data == NULL)
332 tool->tracing_data = process_event_synth_tracing_data_stub;
333 if (tool->build_id == NULL)
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300334 tool->build_id = process_build_id_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200335 if (tool->finished_round == NULL) {
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200336 if (tool->ordered_events)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200337 tool->finished_round = process_finished_round;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200338 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200339 tool->finished_round = process_finished_round_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200340 }
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200341 if (tool->id_index == NULL)
342 tool->id_index = process_id_index_stub;
Adrian Huntera16ac022015-04-09 18:53:43 +0300343 if (tool->auxtrace_info == NULL)
344 tool->auxtrace_info = process_event_auxtrace_info_stub;
345 if (tool->auxtrace == NULL)
346 tool->auxtrace = process_event_auxtrace_stub;
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300347 if (tool->auxtrace_error == NULL)
348 tool->auxtrace_error = process_event_auxtrace_error_stub;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200349}
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300350
Jiri Olsa268fb202012-05-30 14:23:43 +0200351static void swap_sample_id_all(union perf_event *event, void *data)
352{
353 void *end = (void *) event + event->header.size;
354 int size = end - data;
355
356 BUG_ON(size % sizeof(u64));
357 mem_bswap_64(data, size);
358}
359
360static void perf_event__all64_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300361 bool sample_id_all __maybe_unused)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200362{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200363 struct perf_event_header *hdr = &event->header;
364 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200365}
366
Jiri Olsa268fb202012-05-30 14:23:43 +0200367static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200368{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200369 event->comm.pid = bswap_32(event->comm.pid);
370 event->comm.tid = bswap_32(event->comm.tid);
Jiri Olsa268fb202012-05-30 14:23:43 +0200371
372 if (sample_id_all) {
373 void *data = &event->comm.comm;
374
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300375 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200376 swap_sample_id_all(event, data);
377 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200378}
379
Jiri Olsa268fb202012-05-30 14:23:43 +0200380static void perf_event__mmap_swap(union perf_event *event,
381 bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200382{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200383 event->mmap.pid = bswap_32(event->mmap.pid);
384 event->mmap.tid = bswap_32(event->mmap.tid);
385 event->mmap.start = bswap_64(event->mmap.start);
386 event->mmap.len = bswap_64(event->mmap.len);
387 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
Jiri Olsa268fb202012-05-30 14:23:43 +0200388
389 if (sample_id_all) {
390 void *data = &event->mmap.filename;
391
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300392 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200393 swap_sample_id_all(event, data);
394 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200395}
396
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200397static void perf_event__mmap2_swap(union perf_event *event,
398 bool sample_id_all)
399{
400 event->mmap2.pid = bswap_32(event->mmap2.pid);
401 event->mmap2.tid = bswap_32(event->mmap2.tid);
402 event->mmap2.start = bswap_64(event->mmap2.start);
403 event->mmap2.len = bswap_64(event->mmap2.len);
404 event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
405 event->mmap2.maj = bswap_32(event->mmap2.maj);
406 event->mmap2.min = bswap_32(event->mmap2.min);
407 event->mmap2.ino = bswap_64(event->mmap2.ino);
408
409 if (sample_id_all) {
410 void *data = &event->mmap2.filename;
411
412 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
413 swap_sample_id_all(event, data);
414 }
415}
Jiri Olsa268fb202012-05-30 14:23:43 +0200416static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200417{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200418 event->fork.pid = bswap_32(event->fork.pid);
419 event->fork.tid = bswap_32(event->fork.tid);
420 event->fork.ppid = bswap_32(event->fork.ppid);
421 event->fork.ptid = bswap_32(event->fork.ptid);
422 event->fork.time = bswap_64(event->fork.time);
Jiri Olsa268fb202012-05-30 14:23:43 +0200423
424 if (sample_id_all)
425 swap_sample_id_all(event, &event->fork + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200426}
427
Jiri Olsa268fb202012-05-30 14:23:43 +0200428static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200429{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200430 event->read.pid = bswap_32(event->read.pid);
431 event->read.tid = bswap_32(event->read.tid);
432 event->read.value = bswap_64(event->read.value);
433 event->read.time_enabled = bswap_64(event->read.time_enabled);
434 event->read.time_running = bswap_64(event->read.time_running);
435 event->read.id = bswap_64(event->read.id);
Jiri Olsa268fb202012-05-30 14:23:43 +0200436
437 if (sample_id_all)
438 swap_sample_id_all(event, &event->read + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200439}
440
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300441static void perf_event__aux_swap(union perf_event *event, bool sample_id_all)
442{
443 event->aux.aux_offset = bswap_64(event->aux.aux_offset);
444 event->aux.aux_size = bswap_64(event->aux.aux_size);
445 event->aux.flags = bswap_64(event->aux.flags);
446
447 if (sample_id_all)
448 swap_sample_id_all(event, &event->aux + 1);
449}
450
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300451static void perf_event__itrace_start_swap(union perf_event *event,
452 bool sample_id_all)
453{
454 event->itrace_start.pid = bswap_32(event->itrace_start.pid);
455 event->itrace_start.tid = bswap_32(event->itrace_start.tid);
456
457 if (sample_id_all)
458 swap_sample_id_all(event, &event->itrace_start + 1);
459}
460
Adrian Hunter02860392015-07-21 12:44:03 +0300461static void perf_event__switch_swap(union perf_event *event, bool sample_id_all)
462{
463 if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
464 event->context_switch.next_prev_pid =
465 bswap_32(event->context_switch.next_prev_pid);
466 event->context_switch.next_prev_tid =
467 bswap_32(event->context_switch.next_prev_tid);
468 }
469
470 if (sample_id_all)
471 swap_sample_id_all(event, &event->context_switch + 1);
472}
473
Jiri Olsadd96c462013-09-01 12:36:15 +0200474static void perf_event__throttle_swap(union perf_event *event,
475 bool sample_id_all)
476{
477 event->throttle.time = bswap_64(event->throttle.time);
478 event->throttle.id = bswap_64(event->throttle.id);
479 event->throttle.stream_id = bswap_64(event->throttle.stream_id);
480
481 if (sample_id_all)
482 swap_sample_id_all(event, &event->throttle + 1);
483}
484
Jiri Olsae108c662012-05-16 08:59:03 +0200485static u8 revbyte(u8 b)
486{
487 int rev = (b >> 4) | ((b & 0xf) << 4);
488 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
489 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
490 return (u8) rev;
491}
492
493/*
494 * XXX this is hack in attempt to carry flags bitfield
495 * throught endian village. ABI says:
496 *
497 * Bit-fields are allocated from right to left (least to most significant)
498 * on little-endian implementations and from left to right (most to least
499 * significant) on big-endian implementations.
500 *
501 * The above seems to be byte specific, so we need to reverse each
502 * byte of the bitfield. 'Internet' also says this might be implementation
503 * specific and we probably need proper fix and carry perf_event_attr
504 * bitfield flags in separate data file FEAT_ section. Thought this seems
505 * to work for now.
506 */
507static void swap_bitfield(u8 *p, unsigned len)
508{
509 unsigned i;
510
511 for (i = 0; i < len; i++) {
512 *p = revbyte(*p);
513 p++;
514 }
515}
516
David Aherneda39132011-07-15 12:34:09 -0600517/* exported for swapping attributes in file header */
518void perf_event__attr_swap(struct perf_event_attr *attr)
519{
520 attr->type = bswap_32(attr->type);
521 attr->size = bswap_32(attr->size);
Jiri Olsae108c662012-05-16 08:59:03 +0200522
Wang Nanb30b6172015-06-17 09:56:39 +0000523#define bswap_safe(f, n) \
524 (attr->size > (offsetof(struct perf_event_attr, f) + \
525 sizeof(attr->f) * (n)))
526#define bswap_field(f, sz) \
527do { \
528 if (bswap_safe(f, 0)) \
529 attr->f = bswap_##sz(attr->f); \
530} while(0)
531#define bswap_field_32(f) bswap_field(f, 32)
532#define bswap_field_64(f) bswap_field(f, 64)
533
534 bswap_field_64(config);
535 bswap_field_64(sample_period);
536 bswap_field_64(sample_type);
537 bswap_field_64(read_format);
538 bswap_field_32(wakeup_events);
539 bswap_field_32(bp_type);
540 bswap_field_64(bp_addr);
541 bswap_field_64(bp_len);
542 bswap_field_64(branch_sample_type);
543 bswap_field_64(sample_regs_user);
544 bswap_field_32(sample_stack_user);
545 bswap_field_32(aux_watermark);
546
547 /*
548 * After read_format are bitfields. Check read_format because
549 * we are unable to use offsetof on bitfield.
550 */
551 if (bswap_safe(read_format, 1))
552 swap_bitfield((u8 *) (&attr->read_format + 1),
553 sizeof(u64));
554#undef bswap_field_64
555#undef bswap_field_32
556#undef bswap_field
557#undef bswap_safe
David Aherneda39132011-07-15 12:34:09 -0600558}
559
Jiri Olsa268fb202012-05-30 14:23:43 +0200560static void perf_event__hdr_attr_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300561 bool sample_id_all __maybe_unused)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500562{
563 size_t size;
564
David Aherneda39132011-07-15 12:34:09 -0600565 perf_event__attr_swap(&event->attr.attr);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500566
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200567 size = event->header.size;
568 size -= (void *)&event->attr.id - (void *)event;
569 mem_bswap_64(event->attr.id, size);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500570}
571
Jiri Olsa268fb202012-05-30 14:23:43 +0200572static void perf_event__event_type_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300573 bool sample_id_all __maybe_unused)
Tom Zanussicd19a032010-04-01 23:59:20 -0500574{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200575 event->event_type.event_type.event_id =
576 bswap_64(event->event_type.event_type.event_id);
Tom Zanussicd19a032010-04-01 23:59:20 -0500577}
578
Jiri Olsa268fb202012-05-30 14:23:43 +0200579static void perf_event__tracing_data_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300580 bool sample_id_all __maybe_unused)
Tom Zanussi92155452010-04-01 23:59:21 -0500581{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200582 event->tracing_data.size = bswap_32(event->tracing_data.size);
Tom Zanussi92155452010-04-01 23:59:21 -0500583}
584
Adrian Huntera16ac022015-04-09 18:53:43 +0300585static void perf_event__auxtrace_info_swap(union perf_event *event,
586 bool sample_id_all __maybe_unused)
587{
588 size_t size;
589
590 event->auxtrace_info.type = bswap_32(event->auxtrace_info.type);
591
592 size = event->header.size;
593 size -= (void *)&event->auxtrace_info.priv - (void *)event;
594 mem_bswap_64(event->auxtrace_info.priv, size);
595}
596
597static void perf_event__auxtrace_swap(union perf_event *event,
598 bool sample_id_all __maybe_unused)
599{
600 event->auxtrace.size = bswap_64(event->auxtrace.size);
601 event->auxtrace.offset = bswap_64(event->auxtrace.offset);
602 event->auxtrace.reference = bswap_64(event->auxtrace.reference);
603 event->auxtrace.idx = bswap_32(event->auxtrace.idx);
604 event->auxtrace.tid = bswap_32(event->auxtrace.tid);
605 event->auxtrace.cpu = bswap_32(event->auxtrace.cpu);
606}
607
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300608static void perf_event__auxtrace_error_swap(union perf_event *event,
609 bool sample_id_all __maybe_unused)
610{
611 event->auxtrace_error.type = bswap_32(event->auxtrace_error.type);
612 event->auxtrace_error.code = bswap_32(event->auxtrace_error.code);
613 event->auxtrace_error.cpu = bswap_32(event->auxtrace_error.cpu);
614 event->auxtrace_error.pid = bswap_32(event->auxtrace_error.pid);
615 event->auxtrace_error.tid = bswap_32(event->auxtrace_error.tid);
616 event->auxtrace_error.ip = bswap_64(event->auxtrace_error.ip);
617}
618
Jiri Olsa268fb202012-05-30 14:23:43 +0200619typedef void (*perf_event__swap_op)(union perf_event *event,
620 bool sample_id_all);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200621
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200622static perf_event__swap_op perf_event__swap_ops[] = {
623 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200624 [PERF_RECORD_MMAP2] = perf_event__mmap2_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200625 [PERF_RECORD_COMM] = perf_event__comm_swap,
626 [PERF_RECORD_FORK] = perf_event__task_swap,
627 [PERF_RECORD_EXIT] = perf_event__task_swap,
628 [PERF_RECORD_LOST] = perf_event__all64_swap,
629 [PERF_RECORD_READ] = perf_event__read_swap,
Jiri Olsadd96c462013-09-01 12:36:15 +0200630 [PERF_RECORD_THROTTLE] = perf_event__throttle_swap,
631 [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200632 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300633 [PERF_RECORD_AUX] = perf_event__aux_swap,
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300634 [PERF_RECORD_ITRACE_START] = perf_event__itrace_start_swap,
Kan Liangc4937a92015-05-10 15:13:15 -0400635 [PERF_RECORD_LOST_SAMPLES] = perf_event__all64_swap,
Adrian Hunter02860392015-07-21 12:44:03 +0300636 [PERF_RECORD_SWITCH] = perf_event__switch_swap,
637 [PERF_RECORD_SWITCH_CPU_WIDE] = perf_event__switch_swap,
David Aherneda39132011-07-15 12:34:09 -0600638 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200639 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
640 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
641 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200642 [PERF_RECORD_ID_INDEX] = perf_event__all64_swap,
Adrian Huntera16ac022015-04-09 18:53:43 +0300643 [PERF_RECORD_AUXTRACE_INFO] = perf_event__auxtrace_info_swap,
644 [PERF_RECORD_AUXTRACE] = perf_event__auxtrace_swap,
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300645 [PERF_RECORD_AUXTRACE_ERROR] = perf_event__auxtrace_error_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200646 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200647};
648
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200649/*
650 * When perf record finishes a pass on every buffers, it records this pseudo
651 * event.
652 * We record the max timestamp t found in the pass n.
653 * Assuming these timestamps are monotonic across cpus, we know that if
654 * a buffer still has events with timestamps below t, they will be all
655 * available and then read in the pass n + 1.
656 * Hence when we start to read the pass n + 2, we can safely flush every
657 * events with timestamps below t.
658 *
659 * ============ PASS n =================
660 * CPU 0 | CPU 1
661 * |
662 * cnt1 timestamps | cnt2 timestamps
663 * 1 | 2
664 * 2 | 3
665 * - | 4 <--- max recorded
666 *
667 * ============ PASS n + 1 ==============
668 * CPU 0 | CPU 1
669 * |
670 * cnt1 timestamps | cnt2 timestamps
671 * 3 | 5
672 * 4 | 6
673 * 5 | 7 <---- max recorded
674 *
675 * Flush every events below timestamp 4
676 *
677 * ============ PASS n + 2 ==============
678 * CPU 0 | CPU 1
679 * |
680 * cnt1 timestamps | cnt2 timestamps
681 * 6 | 8
682 * 7 | 9
683 * - | 10
684 *
685 * Flush every events below timestamp 7
686 * etc...
687 */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300688static int process_finished_round(struct perf_tool *tool __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300689 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300690 struct ordered_events *oe)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200691{
Adrian Hunter5531e162015-06-23 10:52:48 +0300692 if (dump_trace)
693 fprintf(stdout, "\n");
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300694 return ordered_events__flush(oe, OE_FLUSH__ROUND);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200695}
696
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300697int perf_session__queue_event(struct perf_session *s, union perf_event *event,
698 struct perf_sample *sample, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200699{
Arnaldo Carvalho de Melo4a6b3622015-03-03 13:02:24 -0300700 return ordered_events__queue(&s->ordered_events, event, sample, file_offset);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200701}
702
Kan Liang384b6052015-01-05 13:23:05 -0500703static void callchain__lbr_callstack_printf(struct perf_sample *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200704{
Kan Liang384b6052015-01-05 13:23:05 -0500705 struct ip_callchain *callchain = sample->callchain;
706 struct branch_stack *lbr_stack = sample->branch_stack;
707 u64 kernel_callchain_nr = callchain->nr;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200708 unsigned int i;
709
Kan Liang384b6052015-01-05 13:23:05 -0500710 for (i = 0; i < kernel_callchain_nr; i++) {
711 if (callchain->ips[i] == PERF_CONTEXT_USER)
712 break;
713 }
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200714
Kan Liang384b6052015-01-05 13:23:05 -0500715 if ((i != kernel_callchain_nr) && lbr_stack->nr) {
716 u64 total_nr;
717 /*
718 * LBR callstack can only get user call chain,
719 * i is kernel call chain number,
720 * 1 is PERF_CONTEXT_USER.
721 *
722 * The user call chain is stored in LBR registers.
723 * LBR are pair registers. The caller is stored
724 * in "from" register, while the callee is stored
725 * in "to" register.
726 * For example, there is a call stack
727 * "A"->"B"->"C"->"D".
728 * The LBR registers will recorde like
729 * "C"->"D", "B"->"C", "A"->"B".
730 * So only the first "to" register and all "from"
731 * registers are needed to construct the whole stack.
732 */
733 total_nr = i + 1 + lbr_stack->nr + 1;
734 kernel_callchain_nr = i + 1;
735
736 printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr);
737
738 for (i = 0; i < kernel_callchain_nr; i++)
739 printf("..... %2d: %016" PRIx64 "\n",
740 i, callchain->ips[i]);
741
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200742 printf("..... %2d: %016" PRIx64 "\n",
Kan Liang384b6052015-01-05 13:23:05 -0500743 (int)(kernel_callchain_nr), lbr_stack->entries[0].to);
744 for (i = 0; i < lbr_stack->nr; i++)
745 printf("..... %2d: %016" PRIx64 "\n",
746 (int)(i + kernel_callchain_nr + 1), lbr_stack->entries[i].from);
747 }
748}
749
750static void callchain__printf(struct perf_evsel *evsel,
751 struct perf_sample *sample)
752{
753 unsigned int i;
754 struct ip_callchain *callchain = sample->callchain;
755
756 if (has_branch_callstack(evsel))
757 callchain__lbr_callstack_printf(sample);
758
759 printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr);
760
761 for (i = 0; i < callchain->nr; i++)
762 printf("..... %2d: %016" PRIx64 "\n",
763 i, callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200764}
765
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100766static void branch_stack__printf(struct perf_sample *sample)
767{
768 uint64_t i;
769
770 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
771
Andi Kleen0e332f02015-07-18 08:24:46 -0700772 for (i = 0; i < sample->branch_stack->nr; i++) {
773 struct branch_entry *e = &sample->branch_stack->entries[i];
774
775 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 " %hu cycles %s%s%s%s %x\n",
776 i, e->from, e->to,
777 e->flags.cycles,
778 e->flags.mispred ? "M" : " ",
779 e->flags.predicted ? "P" : " ",
780 e->flags.abort ? "A" : " ",
781 e->flags.in_tx ? "T" : " ",
782 (unsigned)e->flags.reserved);
783 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100784}
785
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200786static void regs_dump__printf(u64 mask, u64 *regs)
787{
788 unsigned rid, i = 0;
789
790 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
791 u64 val = regs[i++];
792
793 printf(".... %-5s 0x%" PRIx64 "\n",
794 perf_reg_name(rid), val);
795 }
796}
797
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200798static const char *regs_abi[] = {
799 [PERF_SAMPLE_REGS_ABI_NONE] = "none",
800 [PERF_SAMPLE_REGS_ABI_32] = "32-bit",
801 [PERF_SAMPLE_REGS_ABI_64] = "64-bit",
802};
803
804static inline const char *regs_dump_abi(struct regs_dump *d)
805{
806 if (d->abi > PERF_SAMPLE_REGS_ABI_64)
807 return "unknown";
808
809 return regs_abi[d->abi];
810}
811
812static void regs__printf(const char *type, struct regs_dump *regs)
813{
814 u64 mask = regs->mask;
815
816 printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n",
817 type,
818 mask,
819 regs_dump_abi(regs));
820
821 regs_dump__printf(mask, regs->regs);
822}
823
Jiri Olsa352ea452014-01-07 13:47:25 +0100824static void regs_user__printf(struct perf_sample *sample)
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200825{
826 struct regs_dump *user_regs = &sample->user_regs;
827
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200828 if (user_regs->regs)
829 regs__printf("user", user_regs);
830}
831
832static void regs_intr__printf(struct perf_sample *sample)
833{
834 struct regs_dump *intr_regs = &sample->intr_regs;
835
836 if (intr_regs->regs)
837 regs__printf("intr", intr_regs);
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200838}
839
840static void stack_user__printf(struct stack_dump *dump)
841{
842 printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
843 dump->size, dump->offset);
844}
845
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300846static void perf_evlist__print_tstamp(struct perf_evlist *evlist,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200847 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200848 struct perf_sample *sample)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200849{
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300850 u64 sample_type = __perf_evlist__combined_sample_type(evlist);
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300851
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200852 if (event->header.type != PERF_RECORD_SAMPLE &&
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300853 !perf_evlist__sample_id_all(evlist)) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200854 fputs("-1 -1 ", stdout);
855 return;
856 }
857
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300858 if ((sample_type & PERF_SAMPLE_CPU))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200859 printf("%u ", sample->cpu);
860
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300861 if (sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200862 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200863}
864
Jiri Olsa9ede4732012-10-10 17:38:13 +0200865static void sample_read__printf(struct perf_sample *sample, u64 read_format)
866{
867 printf("... sample_read:\n");
868
869 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
870 printf("...... time enabled %016" PRIx64 "\n",
871 sample->read.time_enabled);
872
873 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
874 printf("...... time running %016" PRIx64 "\n",
875 sample->read.time_running);
876
877 if (read_format & PERF_FORMAT_GROUP) {
878 u64 i;
879
880 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
881
882 for (i = 0; i < sample->read.group.nr; i++) {
883 struct sample_read_value *value;
884
885 value = &sample->read.group.values[i];
886 printf("..... id %016" PRIx64
887 ", value %016" PRIx64 "\n",
888 value->id, value->value);
889 }
890 } else
891 printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
892 sample->read.one.id, sample->read.one.value);
893}
894
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300895static void dump_event(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200896 u64 file_offset, struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000897{
898 if (!dump_trace)
899 return;
900
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200901 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
902 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000903
904 trace_event(event);
905
906 if (sample)
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300907 perf_evlist__print_tstamp(evlist, event, sample);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000908
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200909 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200910 event->header.size, perf_event__name(event->header.type));
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000911}
912
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200913static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200914 struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000915{
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300916 u64 sample_type;
917
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200918 if (!dump_trace)
919 return;
920
Don Zickus0ea590a2014-02-25 22:43:46 -0500921 printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200922 event->header.misc, sample->pid, sample->tid, sample->ip,
David Ahern7cec0922011-05-30 13:08:23 -0600923 sample->period, sample->addr);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000924
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200925 sample_type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300926
927 if (sample_type & PERF_SAMPLE_CALLCHAIN)
Kan Liang384b6052015-01-05 13:23:05 -0500928 callchain__printf(evsel, sample);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100929
Kan Liang384b6052015-01-05 13:23:05 -0500930 if ((sample_type & PERF_SAMPLE_BRANCH_STACK) && !has_branch_callstack(evsel))
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100931 branch_stack__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200932
933 if (sample_type & PERF_SAMPLE_REGS_USER)
Jiri Olsa352ea452014-01-07 13:47:25 +0100934 regs_user__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200935
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200936 if (sample_type & PERF_SAMPLE_REGS_INTR)
937 regs_intr__printf(sample);
938
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200939 if (sample_type & PERF_SAMPLE_STACK_USER)
940 stack_user__printf(&sample->user_stack);
Andi Kleen05484292013-01-24 16:10:29 +0100941
942 if (sample_type & PERF_SAMPLE_WEIGHT)
943 printf("... weight: %" PRIu64 "\n", sample->weight);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100944
945 if (sample_type & PERF_SAMPLE_DATA_SRC)
946 printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
Jiri Olsa9ede4732012-10-10 17:38:13 +0200947
Andi Kleen475eeab2013-09-20 07:40:43 -0700948 if (sample_type & PERF_SAMPLE_TRANSACTION)
949 printf("... transaction: %" PRIx64 "\n", sample->transaction);
950
Jiri Olsa9ede4732012-10-10 17:38:13 +0200951 if (sample_type & PERF_SAMPLE_READ)
952 sample_read__printf(sample, evsel->attr.read_format);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000953}
954
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -0300955static struct machine *machines__find_for_cpumode(struct machines *machines,
Adrian Hunteref893252013-08-27 11:23:06 +0300956 union perf_event *event,
957 struct perf_sample *sample)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200958{
959 const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Dongsheng Yangad85ace2013-12-20 13:41:47 -0500960 struct machine *machine;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200961
David Ahern7c0f4a42012-07-20 17:25:48 -0600962 if (perf_guest &&
963 ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
964 (cpumode == PERF_RECORD_MISC_GUEST_USER))) {
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530965 u32 pid;
966
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200967 if (event->header.type == PERF_RECORD_MMAP
968 || event->header.type == PERF_RECORD_MMAP2)
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530969 pid = event->mmap.pid;
970 else
Adrian Hunteref893252013-08-27 11:23:06 +0300971 pid = sample->pid;
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530972
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -0300973 machine = machines__find(machines, pid);
Dongsheng Yangad85ace2013-12-20 13:41:47 -0500974 if (!machine)
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -0300975 machine = machines__find(machines, DEFAULT_GUEST_KERNEL_ID);
Dongsheng Yangad85ace2013-12-20 13:41:47 -0500976 return machine;
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530977 }
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200978
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -0300979 return &machines->host;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200980}
981
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -0300982static int deliver_sample_value(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +0200983 struct perf_tool *tool,
984 union perf_event *event,
985 struct perf_sample *sample,
986 struct sample_read_value *v,
987 struct machine *machine)
988{
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -0300989 struct perf_sample_id *sid = perf_evlist__id2sid(evlist, v->id);
Jiri Olsae4caec02012-10-10 18:52:24 +0200990
Jiri Olsae4caec02012-10-10 18:52:24 +0200991 if (sid) {
992 sample->id = v->id;
993 sample->period = v->value - sid->period;
994 sid->period = v->value;
995 }
996
997 if (!sid || sid->evsel == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -0300998 ++evlist->stats.nr_unknown_id;
Jiri Olsae4caec02012-10-10 18:52:24 +0200999 return 0;
1000 }
1001
1002 return tool->sample(tool, event, sample, sid->evsel, machine);
1003}
1004
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001005static int deliver_sample_group(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +02001006 struct perf_tool *tool,
1007 union perf_event *event,
1008 struct perf_sample *sample,
1009 struct machine *machine)
1010{
1011 int ret = -EINVAL;
1012 u64 i;
1013
1014 for (i = 0; i < sample->read.group.nr; i++) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001015 ret = deliver_sample_value(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001016 &sample->read.group.values[i],
1017 machine);
1018 if (ret)
1019 break;
1020 }
1021
1022 return ret;
1023}
1024
1025static int
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001026 perf_evlist__deliver_sample(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +02001027 struct perf_tool *tool,
1028 union perf_event *event,
1029 struct perf_sample *sample,
1030 struct perf_evsel *evsel,
1031 struct machine *machine)
1032{
1033 /* We know evsel != NULL. */
1034 u64 sample_type = evsel->attr.sample_type;
1035 u64 read_format = evsel->attr.read_format;
1036
1037 /* Standard sample delievery. */
1038 if (!(sample_type & PERF_SAMPLE_READ))
1039 return tool->sample(tool, event, sample, evsel, machine);
1040
1041 /* For PERF_SAMPLE_READ we have either single or group mode. */
1042 if (read_format & PERF_FORMAT_GROUP)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001043 return deliver_sample_group(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001044 machine);
1045 else
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001046 return deliver_sample_value(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001047 &sample->read.one, machine);
1048}
1049
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -03001050static int machines__deliver_event(struct machines *machines,
1051 struct perf_evlist *evlist,
1052 union perf_event *event,
1053 struct perf_sample *sample,
1054 struct perf_tool *tool, u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001055{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001056 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001057 struct machine *machine;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001058
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001059 dump_event(evlist, event, file_offset, sample);
Thomas Gleixner532e7262010-12-07 12:48:55 +00001060
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001061 evsel = perf_evlist__id2evsel(evlist, sample->id);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001062
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001063 machine = machines__find_for_cpumode(machines, event, sample);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001064
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001065 switch (event->header.type) {
1066 case PERF_RECORD_SAMPLE:
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001067 if (evsel == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001068 ++evlist->stats.nr_unknown_id;
Jiri Olsa67822062012-04-12 14:21:01 +02001069 return 0;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001070 }
Kan Liang1b29ac52015-09-03 08:31:00 -04001071 dump_sample(evsel, event, sample);
Joerg Roedel0c095712012-02-10 18:05:04 +01001072 if (machine == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001073 ++evlist->stats.nr_unprocessable_samples;
Jiri Olsa67822062012-04-12 14:21:01 +02001074 return 0;
Joerg Roedel0c095712012-02-10 18:05:04 +01001075 }
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001076 return perf_evlist__deliver_sample(evlist, tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001077 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001078 return tool->mmap(tool, event, sample, machine);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001079 case PERF_RECORD_MMAP2:
Kan Liang930e6fc2015-06-17 09:51:10 -04001080 if (event->header.misc & PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT)
1081 ++evlist->stats.nr_proc_map_timeout;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001082 return tool->mmap2(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001083 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001084 return tool->comm(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001085 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001086 return tool->fork(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001087 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001088 return tool->exit(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001089 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001090 if (tool->lost == perf_event__process_lost)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001091 evlist->stats.total_lost += event->lost.lost;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001092 return tool->lost(tool, event, sample, machine);
Kan Liangc4937a92015-05-10 15:13:15 -04001093 case PERF_RECORD_LOST_SAMPLES:
1094 if (tool->lost_samples == perf_event__process_lost_samples)
1095 evlist->stats.total_lost_samples += event->lost_samples.lost;
1096 return tool->lost_samples(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001097 case PERF_RECORD_READ:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001098 return tool->read(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001099 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001100 return tool->throttle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001101 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001102 return tool->unthrottle(tool, event, sample, machine);
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001103 case PERF_RECORD_AUX:
1104 return tool->aux(tool, event, sample, machine);
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001105 case PERF_RECORD_ITRACE_START:
1106 return tool->itrace_start(tool, event, sample, machine);
Adrian Hunter02860392015-07-21 12:44:03 +03001107 case PERF_RECORD_SWITCH:
1108 case PERF_RECORD_SWITCH_CPU_WIDE:
1109 return tool->context_switch(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001110 default:
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001111 ++evlist->stats.nr_unknown_events;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001112 return -1;
1113 }
1114}
1115
Adrian Hunterc4468702015-04-09 18:53:48 +03001116static int perf_session__deliver_event(struct perf_session *session,
1117 union perf_event *event,
1118 struct perf_sample *sample,
1119 struct perf_tool *tool,
1120 u64 file_offset)
1121{
1122 int ret;
1123
1124 ret = auxtrace__process_event(session, event, sample, tool);
1125 if (ret < 0)
1126 return ret;
1127 if (ret > 0)
1128 return 0;
1129
1130 return machines__deliver_event(&session->machines, session->evlist,
1131 event, sample, tool, file_offset);
1132}
1133
Adrian Hunterd5652d82014-07-23 22:19:58 +03001134static s64 perf_session__process_user_event(struct perf_session *session,
1135 union perf_event *event,
Adrian Hunterd5652d82014-07-23 22:19:58 +03001136 u64 file_offset)
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001137{
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -03001138 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001139 struct perf_tool *tool = session->tool;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001140 int fd = perf_data_file__fd(session->file);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001141 int err;
1142
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001143 dump_event(session->evlist, event, file_offset, NULL);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001144
1145 /* These events are processed right away */
1146 switch (event->header.type) {
1147 case PERF_RECORD_HEADER_ATTR:
Adrian Hunter47c3d102013-07-04 16:20:21 +03001148 err = tool->attr(tool, event, &session->evlist);
Adrian Huntercfe1c412014-07-31 09:00:45 +03001149 if (err == 0) {
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -03001150 perf_session__set_id_hdr_size(session);
Adrian Huntercfe1c412014-07-31 09:00:45 +03001151 perf_session__set_comm_exec(session);
1152 }
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001153 return err;
Jiri Olsaf67697b2014-02-04 15:37:48 +01001154 case PERF_RECORD_HEADER_EVENT_TYPE:
1155 /*
1156 * Depreceated, but we need to handle it for sake
1157 * of old data files create in pipe mode.
1158 */
1159 return 0;
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001160 case PERF_RECORD_HEADER_TRACING_DATA:
1161 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001162 lseek(fd, file_offset, SEEK_SET);
Adrian Hunter47c3d102013-07-04 16:20:21 +03001163 return tool->tracing_data(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001164 case PERF_RECORD_HEADER_BUILD_ID:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001165 return tool->build_id(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001166 case PERF_RECORD_FINISHED_ROUND:
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -03001167 return tool->finished_round(tool, event, oe);
Adrian Hunter3c659ee2014-10-27 15:49:22 +02001168 case PERF_RECORD_ID_INDEX:
1169 return tool->id_index(tool, event, session);
Adrian Huntera16ac022015-04-09 18:53:43 +03001170 case PERF_RECORD_AUXTRACE_INFO:
1171 return tool->auxtrace_info(tool, event, session);
1172 case PERF_RECORD_AUXTRACE:
1173 /* setup for reading amidst mmap */
1174 lseek(fd, file_offset + event->header.size, SEEK_SET);
1175 return tool->auxtrace(tool, event, session);
Adrian Huntere9bf54d2015-04-09 18:53:47 +03001176 case PERF_RECORD_AUXTRACE_ERROR:
Adrian Hunter85ed4722015-04-09 18:53:50 +03001177 perf_session__auxtrace_error_inc(session, event);
Adrian Huntere9bf54d2015-04-09 18:53:47 +03001178 return tool->auxtrace_error(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001179 default:
1180 return -EINVAL;
1181 }
1182}
1183
Adrian Huntera2938292014-10-27 15:49:23 +02001184int perf_session__deliver_synth_event(struct perf_session *session,
1185 union perf_event *event,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001186 struct perf_sample *sample)
Adrian Huntera2938292014-10-27 15:49:23 +02001187{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001188 struct perf_evlist *evlist = session->evlist;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001189 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001190
1191 events_stats__inc(&evlist->stats, event->header.type);
Adrian Huntera2938292014-10-27 15:49:23 +02001192
1193 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001194 return perf_session__process_user_event(session, event, 0);
Adrian Huntera2938292014-10-27 15:49:23 +02001195
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001196 return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0);
Adrian Huntera2938292014-10-27 15:49:23 +02001197}
1198
Jiri Olsa268fb202012-05-30 14:23:43 +02001199static void event_swap(union perf_event *event, bool sample_id_all)
1200{
1201 perf_event__swap_op swap;
1202
1203 swap = perf_event__swap_ops[event->header.type];
1204 if (swap)
1205 swap(event, sample_id_all);
1206}
1207
Adrian Hunter5a52f332014-07-31 09:00:57 +03001208int perf_session__peek_event(struct perf_session *session, off_t file_offset,
1209 void *buf, size_t buf_sz,
1210 union perf_event **event_ptr,
1211 struct perf_sample *sample)
1212{
1213 union perf_event *event;
1214 size_t hdr_sz, rest;
1215 int fd;
1216
1217 if (session->one_mmap && !session->header.needs_swap) {
1218 event = file_offset - session->one_mmap_offset +
1219 session->one_mmap_addr;
1220 goto out_parse_sample;
1221 }
1222
1223 if (perf_data_file__is_pipe(session->file))
1224 return -1;
1225
1226 fd = perf_data_file__fd(session->file);
1227 hdr_sz = sizeof(struct perf_event_header);
1228
1229 if (buf_sz < hdr_sz)
1230 return -1;
1231
1232 if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 ||
Adrian Hunter554e92e2015-05-19 16:05:45 +03001233 readn(fd, buf, hdr_sz) != (ssize_t)hdr_sz)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001234 return -1;
1235
1236 event = (union perf_event *)buf;
1237
1238 if (session->header.needs_swap)
1239 perf_event_header__bswap(&event->header);
1240
Adrian Hunter554e92e2015-05-19 16:05:45 +03001241 if (event->header.size < hdr_sz || event->header.size > buf_sz)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001242 return -1;
1243
1244 rest = event->header.size - hdr_sz;
1245
Adrian Hunter554e92e2015-05-19 16:05:45 +03001246 if (readn(fd, buf, rest) != (ssize_t)rest)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001247 return -1;
1248
1249 if (session->header.needs_swap)
1250 event_swap(event, perf_evlist__sample_id_all(session->evlist));
1251
1252out_parse_sample:
1253
1254 if (sample && event->header.type < PERF_RECORD_USER_TYPE_START &&
1255 perf_evlist__parse_sample(session->evlist, event, sample))
1256 return -1;
1257
1258 *event_ptr = event;
1259
1260 return 0;
1261}
1262
Adrian Hunterd5652d82014-07-23 22:19:58 +03001263static s64 perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001264 union perf_event *event, u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001265{
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001266 struct perf_evlist *evlist = session->evlist;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001267 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001268 struct perf_sample sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001269 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001270
Jiri Olsa268fb202012-05-30 14:23:43 +02001271 if (session->header.needs_swap)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001272 event_swap(event, perf_evlist__sample_id_all(evlist));
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001273
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001274 if (event->header.type >= PERF_RECORD_HEADER_MAX)
1275 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001276
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001277 events_stats__inc(&evlist->stats, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001278
1279 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001280 return perf_session__process_user_event(session, event, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001281
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001282 /*
1283 * For all kernel events we get the sample data
1284 */
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001285 ret = perf_evlist__parse_sample(evlist, event, &sample);
Frederic Weisbecker5538bec2011-05-22 02:17:22 +02001286 if (ret)
1287 return ret;
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001288
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001289 if (tool->ordered_events) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001290 ret = perf_session__queue_event(session, event, &sample, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001291 if (ret != -ETIME)
1292 return ret;
1293 }
1294
Adrian Hunterc4468702015-04-09 18:53:48 +03001295 return perf_session__deliver_event(session, event, &sample, tool,
1296 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001297}
1298
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001299void perf_event_header__bswap(struct perf_event_header *hdr)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001300{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001301 hdr->type = bswap_32(hdr->type);
1302 hdr->misc = bswap_16(hdr->misc);
1303 hdr->size = bswap_16(hdr->size);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001304}
1305
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001306struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1307{
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001308 return machine__findnew_thread(&session->machines.host, -1, pid);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001309}
1310
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001311static struct thread *perf_session__register_idle_thread(struct perf_session *session)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001312{
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001313 struct thread *thread;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001314
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001315 thread = machine__findnew_thread(&session->machines.host, 0, 0);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001316 if (thread == NULL || thread__set_comm(thread, "swapper", 0)) {
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001317 pr_err("problem inserting idle task.\n");
1318 thread = NULL;
1319 }
1320
1321 return thread;
1322}
1323
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001324static void perf_session__warn_about_errors(const struct perf_session *session)
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001325{
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001326 const struct events_stats *stats = &session->evlist->stats;
1327 const struct ordered_events *oe = &session->ordered_events;
1328
1329 if (session->tool->lost == perf_event__process_lost &&
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001330 stats->nr_events[PERF_RECORD_LOST] != 0) {
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001331 ui__warning("Processed %d events and lost %d chunks!\n\n"
1332 "Check IO/CPU overload!\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001333 stats->nr_events[0],
1334 stats->nr_events[PERF_RECORD_LOST]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001335 }
1336
Kan Liangc4937a92015-05-10 15:13:15 -04001337 if (session->tool->lost_samples == perf_event__process_lost_samples) {
1338 double drop_rate;
1339
1340 drop_rate = (double)stats->total_lost_samples /
1341 (double) (stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples);
1342 if (drop_rate > 0.05) {
He Kuang6ba29c22015-06-11 12:44:24 +00001343 ui__warning("Processed %" PRIu64 " samples and lost %3.2f%% samples!\n\n",
Kan Liangc4937a92015-05-10 15:13:15 -04001344 stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples,
1345 drop_rate * 100.0);
1346 }
1347 }
1348
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001349 if (stats->nr_unknown_events != 0) {
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001350 ui__warning("Found %u unknown events!\n\n"
1351 "Is this an older tool processing a perf.data "
1352 "file generated by a more recent tool?\n\n"
1353 "If that is not the case, consider "
1354 "reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001355 stats->nr_unknown_events);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001356 }
1357
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001358 if (stats->nr_unknown_id != 0) {
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001359 ui__warning("%u samples with id not present in the header\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001360 stats->nr_unknown_id);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001361 }
1362
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001363 if (stats->nr_invalid_chains != 0) {
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001364 ui__warning("Found invalid callchains!\n\n"
1365 "%u out of %u events were discarded for this reason.\n\n"
1366 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001367 stats->nr_invalid_chains,
1368 stats->nr_events[PERF_RECORD_SAMPLE]);
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001369 }
Joerg Roedel0c095712012-02-10 18:05:04 +01001370
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001371 if (stats->nr_unprocessable_samples != 0) {
Joerg Roedel0c095712012-02-10 18:05:04 +01001372 ui__warning("%u unprocessable samples recorded.\n"
1373 "Do you have a KVM guest running and not using 'perf kvm'?\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001374 stats->nr_unprocessable_samples);
Joerg Roedel0c095712012-02-10 18:05:04 +01001375 }
Jiri Olsaf61ff6c2014-11-26 16:39:31 +01001376
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001377 if (oe->nr_unordered_events != 0)
1378 ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events);
Adrian Hunter85ed4722015-04-09 18:53:50 +03001379
1380 events_stats__auxtrace_error_warn(stats);
Kan Liang930e6fc2015-06-17 09:51:10 -04001381
1382 if (stats->nr_proc_map_timeout != 0) {
1383 ui__warning("%d map information files for pre-existing threads were\n"
1384 "not processed, if there are samples for addresses they\n"
1385 "will not be resolved, you may find out which are these\n"
1386 "threads by running with -v and redirecting the output\n"
Kan Liang9d9cad72015-06-17 09:51:11 -04001387 "to a file.\n"
1388 "The time limit to process proc map is too short?\n"
1389 "Increase it by --proc-map-timeout\n",
Kan Liang930e6fc2015-06-17 09:51:10 -04001390 stats->nr_proc_map_timeout);
1391 }
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001392}
1393
Adrian Huntera5499b32015-05-29 16:33:30 +03001394static int perf_session__flush_thread_stack(struct thread *thread,
1395 void *p __maybe_unused)
1396{
1397 return thread_stack__flush(thread);
1398}
1399
1400static int perf_session__flush_thread_stacks(struct perf_session *session)
1401{
1402 return machines__for_each_thread(&session->machines,
1403 perf_session__flush_thread_stack,
1404 NULL);
1405}
1406
Tom Zanussi8dc58102010-04-01 23:59:15 -05001407volatile int session_done;
1408
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001409static int __perf_session__process_pipe_events(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05001410{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001411 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001412 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001413 int fd = perf_data_file__fd(session->file);
Stephane Eranian444d2862012-05-15 13:28:12 +02001414 union perf_event *event;
1415 uint32_t size, cur_size = 0;
1416 void *buf = NULL;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001417 s64 skip = 0;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001418 u64 head;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001419 ssize_t err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001420 void *p;
1421
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001422 perf_tool__fill_defaults(tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001423
1424 head = 0;
Stephane Eranian444d2862012-05-15 13:28:12 +02001425 cur_size = sizeof(union perf_event);
1426
1427 buf = malloc(cur_size);
1428 if (!buf)
1429 return -errno;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001430more:
Stephane Eranian444d2862012-05-15 13:28:12 +02001431 event = buf;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001432 err = readn(fd, event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05001433 if (err <= 0) {
1434 if (err == 0)
1435 goto done;
1436
1437 pr_err("failed to read event header\n");
1438 goto out_err;
1439 }
1440
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001441 if (session->header.needs_swap)
Stephane Eranian444d2862012-05-15 13:28:12 +02001442 perf_event_header__bswap(&event->header);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001443
Stephane Eranian444d2862012-05-15 13:28:12 +02001444 size = event->header.size;
Adrian Hunter27389d72013-07-04 16:20:27 +03001445 if (size < sizeof(struct perf_event_header)) {
1446 pr_err("bad event header size\n");
1447 goto out_err;
1448 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001449
Stephane Eranian444d2862012-05-15 13:28:12 +02001450 if (size > cur_size) {
1451 void *new = realloc(buf, size);
1452 if (!new) {
1453 pr_err("failed to allocate memory to read event\n");
1454 goto out_err;
1455 }
1456 buf = new;
1457 cur_size = size;
1458 event = buf;
1459 }
1460 p = event;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001461 p += sizeof(struct perf_event_header);
1462
Tom Zanussi794e43b2010-05-05 00:27:40 -05001463 if (size - sizeof(struct perf_event_header)) {
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001464 err = readn(fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -05001465 if (err <= 0) {
1466 if (err == 0) {
1467 pr_err("unexpected end of event stream\n");
1468 goto done;
1469 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001470
Tom Zanussi794e43b2010-05-05 00:27:40 -05001471 pr_err("failed to read event data\n");
1472 goto out_err;
1473 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001474 }
1475
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001476 if ((skip = perf_session__process_event(session, event, head)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001477 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
Stephane Eranian444d2862012-05-15 13:28:12 +02001478 head, event->header.size, event->header.type);
Jiri Olsa9389a462012-04-16 20:42:51 +02001479 err = -EINVAL;
1480 goto out_err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001481 }
1482
1483 head += size;
1484
Tom Zanussi8dc58102010-04-01 23:59:15 -05001485 if (skip > 0)
1486 head += skip;
1487
1488 if (!session_done())
1489 goto more;
1490done:
Adrian Hunter8c16b642013-10-18 15:29:02 +03001491 /* do the final flush for ordered samples */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001492 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
Adrian Hunterc4468702015-04-09 18:53:48 +03001493 if (err)
1494 goto out_err;
1495 err = auxtrace__flush_events(session, tool);
Adrian Huntera5499b32015-05-29 16:33:30 +03001496 if (err)
1497 goto out_err;
1498 err = perf_session__flush_thread_stacks(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001499out_err:
Stephane Eranian444d2862012-05-15 13:28:12 +02001500 free(buf);
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001501 perf_session__warn_about_errors(session);
Jiri Olsaadc56ed2014-06-10 22:50:03 +02001502 ordered_events__free(&session->ordered_events);
Adrian Hunterc4468702015-04-09 18:53:48 +03001503 auxtrace__free_events(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001504 return err;
1505}
1506
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001507static union perf_event *
1508fetch_mmaped_event(struct perf_session *session,
1509 u64 head, size_t mmap_size, char *buf)
1510{
1511 union perf_event *event;
1512
1513 /*
1514 * Ensure we have enough space remaining to read
1515 * the size of the event in the headers.
1516 */
1517 if (head + sizeof(event->header) > mmap_size)
1518 return NULL;
1519
1520 event = (union perf_event *)(buf + head);
1521
1522 if (session->header.needs_swap)
1523 perf_event_header__bswap(&event->header);
1524
Adrian Hunter27389d72013-07-04 16:20:27 +03001525 if (head + event->header.size > mmap_size) {
1526 /* We're not fetching the event so swap back again */
1527 if (session->header.needs_swap)
1528 perf_event_header__bswap(&event->header);
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001529 return NULL;
Adrian Hunter27389d72013-07-04 16:20:27 +03001530 }
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001531
1532 return event;
1533}
1534
David Miller35d48dd2012-11-10 14:12:19 -05001535/*
1536 * On 64bit we can mmap the data file in one go. No need for tiny mmap
1537 * slices. On 32bit we use 32MB.
1538 */
1539#if BITS_PER_LONG == 64
1540#define MMAP_SIZE ULLONG_MAX
1541#define NUM_MMAPS 1
1542#else
1543#define MMAP_SIZE (32 * 1024 * 1024ULL)
1544#define NUM_MMAPS 128
1545#endif
1546
Namhyung Kim4ac30cf2015-01-29 17:06:43 +09001547static int __perf_session__process_events(struct perf_session *session,
1548 u64 data_offset, u64 data_size,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001549 u64 file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001550{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001551 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001552 struct perf_tool *tool = session->tool;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001553 int fd = perf_data_file__fd(session->file);
Adrian Hunterd5652d82014-07-23 22:19:58 +03001554 u64 head, page_offset, file_offset, file_pos, size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001555 int err, mmap_prot, mmap_flags, map_idx = 0;
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -03001556 size_t mmap_size;
David Miller35d48dd2012-11-10 14:12:19 -05001557 char *buf, *mmaps[NUM_MMAPS];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001558 union perf_event *event;
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001559 struct ui_progress prog;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001560 s64 skip;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001561
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001562 perf_tool__fill_defaults(tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001563
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001564 page_offset = page_size * (data_offset / page_size);
1565 file_offset = page_offset;
1566 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001567
Namhyung Kimb314e5c2013-09-30 17:19:48 +09001568 if (data_size && (data_offset + data_size < file_size))
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001569 file_size = data_offset + data_size;
1570
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001571 ui_progress__init(&prog, file_size, "Processing events...");
Thomas Gleixner55b446292010-11-30 17:49:46 +00001572
David Miller35d48dd2012-11-10 14:12:19 -05001573 mmap_size = MMAP_SIZE;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001574 if (mmap_size > file_size) {
Thomas Gleixner55b446292010-11-30 17:49:46 +00001575 mmap_size = file_size;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001576 session->one_mmap = true;
1577 }
Thomas Gleixner55b446292010-11-30 17:49:46 +00001578
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001579 memset(mmaps, 0, sizeof(mmaps));
1580
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001581 mmap_prot = PROT_READ;
1582 mmap_flags = MAP_SHARED;
1583
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001584 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001585 mmap_prot |= PROT_WRITE;
1586 mmap_flags = MAP_PRIVATE;
1587 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001588remap:
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001589 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd,
Thomas Gleixner55b446292010-11-30 17:49:46 +00001590 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001591 if (buf == MAP_FAILED) {
1592 pr_err("failed to mmap file\n");
1593 err = -errno;
1594 goto out_err;
1595 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001596 mmaps[map_idx] = buf;
1597 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001598 file_pos = file_offset + head;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001599 if (session->one_mmap) {
1600 session->one_mmap_addr = buf;
1601 session->one_mmap_offset = file_offset;
1602 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001603
1604more:
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001605 event = fetch_mmaped_event(session, head, mmap_size, buf);
1606 if (!event) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001607 if (mmaps[map_idx]) {
1608 munmap(mmaps[map_idx], mmap_size);
1609 mmaps[map_idx] = NULL;
1610 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001611
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001612 page_offset = page_size * (head / page_size);
1613 file_offset += page_offset;
1614 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001615 goto remap;
1616 }
1617
1618 size = event->header.size;
1619
Adrian Hunter27389d72013-07-04 16:20:27 +03001620 if (size < sizeof(struct perf_event_header) ||
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001621 (skip = perf_session__process_event(session, event, file_pos)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001622 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1623 file_offset + head, event->header.size,
1624 event->header.type);
1625 err = -EINVAL;
1626 goto out_err;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001627 }
1628
Adrian Hunter6f917c72014-07-23 22:19:57 +03001629 if (skip)
1630 size += skip;
1631
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001632 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001633 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001634
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001635 ui_progress__update(&prog, size);
Thomas Gleixner55b446292010-11-30 17:49:46 +00001636
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001637 if (session_done())
Adrian Hunter8c16b642013-10-18 15:29:02 +03001638 goto out;
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001639
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001640 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001641 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001642
Adrian Hunter8c16b642013-10-18 15:29:02 +03001643out:
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001644 /* do the final flush for ordered samples */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001645 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
Adrian Hunterc4468702015-04-09 18:53:48 +03001646 if (err)
1647 goto out_err;
1648 err = auxtrace__flush_events(session, tool);
Adrian Huntera5499b32015-05-29 16:33:30 +03001649 if (err)
1650 goto out_err;
1651 err = perf_session__flush_thread_stacks(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001652out_err:
Namhyung Kima5580f32012-11-13 22:30:34 +09001653 ui_progress__finish();
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001654 perf_session__warn_about_errors(session);
Jiri Olsaadc56ed2014-06-10 22:50:03 +02001655 ordered_events__free(&session->ordered_events);
Adrian Hunterc4468702015-04-09 18:53:48 +03001656 auxtrace__free_events(session);
Adrian Hunter919d86d2014-07-14 13:02:51 +03001657 session->one_mmap = false;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001658 return err;
1659}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001660
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001661int perf_session__process_events(struct perf_session *session)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001662{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001663 u64 size = perf_data_file__size(session->file);
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001664 int err;
1665
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001666 if (perf_session__register_idle_thread(session) == NULL)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001667 return -ENOMEM;
1668
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001669 if (!perf_data_file__is_pipe(session->file))
1670 err = __perf_session__process_events(session,
1671 session->header.data_offset,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001672 session->header.data_size, size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001673 else
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001674 err = __perf_session__process_pipe_events(session);
Dave Martin88ca8952010-07-27 11:46:12 -03001675
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001676 return err;
1677}
1678
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001679bool perf_session__has_traces(struct perf_session *session, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001680{
David Ahern93ea01c292013-08-07 22:50:58 -04001681 struct perf_evsel *evsel;
1682
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001683 evlist__for_each(session->evlist, evsel) {
David Ahern93ea01c292013-08-07 22:50:58 -04001684 if (evsel->attr.type == PERF_TYPE_TRACEPOINT)
1685 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001686 }
1687
David Ahern93ea01c292013-08-07 22:50:58 -04001688 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1689 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001690}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001691
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001692int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1693 const char *symbol_name, u64 addr)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001694{
1695 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001696 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001697 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001698
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001699 ref = zalloc(sizeof(struct ref_reloc_sym));
1700 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001701 return -ENOMEM;
1702
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001703 ref->name = strdup(symbol_name);
1704 if (ref->name == NULL) {
1705 free(ref);
1706 return -ENOMEM;
1707 }
1708
1709 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001710 if (bracket)
1711 *bracket = '\0';
1712
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001713 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001714
1715 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001716 struct kmap *kmap = map__kmap(maps[i]);
Wang Nanba927322015-04-07 08:22:45 +00001717
1718 if (!kmap)
1719 continue;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001720 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001721 }
1722
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001723 return 0;
1724}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001725
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001726size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001727{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001728 return machines__fprintf_dsos(&session->machines, fp);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001729}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001730
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001731size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001732 bool (skip)(struct dso *dso, int parm), int parm)
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001733{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001734 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001735}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001736
1737size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1738{
Adrian Hunterc4468702015-04-09 18:53:48 +03001739 size_t ret;
1740 const char *msg = "";
1741
1742 if (perf_header__has_feat(&session->header, HEADER_AUXTRACE))
1743 msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)";
1744
Adrian Hunterfe692ac2015-06-23 10:52:49 +03001745 ret = fprintf(fp, "\nAggregated stats:%s\n", msg);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001746
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001747 ret += events_stats__fprintf(&session->evlist->stats, fp);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001748 return ret;
1749}
David Ahernc0230b22011-03-09 22:23:27 -07001750
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001751size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1752{
1753 /*
1754 * FIXME: Here we have to actually print all the machines in this
1755 * session, not just the host...
1756 */
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -03001757 return machine__fprintf(&session->machines.host, fp);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001758}
1759
David Ahern9cbdb702011-04-06 21:54:20 -06001760struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1761 unsigned int type)
1762{
1763 struct perf_evsel *pos;
1764
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001765 evlist__for_each(session->evlist, pos) {
David Ahern9cbdb702011-04-06 21:54:20 -06001766 if (pos->attr.type == type)
1767 return pos;
1768 }
1769 return NULL;
1770}
1771
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001772void perf_evsel__print_ip(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -03001773 struct addr_location *al,
David Ahern307cbb92013-08-07 22:50:53 -04001774 unsigned int print_opts, unsigned int stack_depth)
David Ahernc0230b22011-03-09 22:23:27 -07001775{
David Ahernc0230b22011-03-09 22:23:27 -07001776 struct callchain_cursor_node *node;
David Aherna6ffaf92013-08-07 22:50:51 -04001777 int print_ip = print_opts & PRINT_IP_OPT_IP;
1778 int print_sym = print_opts & PRINT_IP_OPT_SYM;
1779 int print_dso = print_opts & PRINT_IP_OPT_DSO;
1780 int print_symoffset = print_opts & PRINT_IP_OPT_SYMOFFSET;
David Ahernb0b35f02013-08-07 22:50:52 -04001781 int print_oneline = print_opts & PRINT_IP_OPT_ONELINE;
Adrian Huntercc8fae12013-12-06 09:42:57 +02001782 int print_srcline = print_opts & PRINT_IP_OPT_SRCLINE;
David Ahernb0b35f02013-08-07 22:50:52 -04001783 char s = print_oneline ? ' ' : '\t';
David Ahernc0230b22011-03-09 22:23:27 -07001784
David Ahernc0230b22011-03-09 22:23:27 -07001785 if (symbol_conf.use_callchain && sample->callchain) {
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001786 struct addr_location node_al;
David Ahernc0230b22011-03-09 22:23:27 -07001787
Arnaldo Carvalho de Melocc8b7c22014-10-23 15:26:17 -03001788 if (thread__resolve_callchain(al->thread, evsel,
1789 sample, NULL, NULL,
1790 PERF_MAX_STACK_DEPTH) != 0) {
David Ahernc0230b22011-03-09 22:23:27 -07001791 if (verbose)
1792 error("Failed to resolve callchain. Skipping\n");
1793 return;
1794 }
Namhyung Kim47260642012-05-31 14:43:26 +09001795 callchain_cursor_commit(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001796
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001797 if (print_symoffset)
1798 node_al = *al;
1799
David Ahern307cbb92013-08-07 22:50:53 -04001800 while (stack_depth) {
Adrian Huntera4eb24a2013-12-06 09:42:56 +02001801 u64 addr = 0;
1802
Namhyung Kim47260642012-05-31 14:43:26 +09001803 node = callchain_cursor_current(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001804 if (!node)
1805 break;
1806
David Ahernd2ff1b12013-11-18 13:32:44 -07001807 if (node->sym && node->sym->ignore)
1808 goto next;
1809
David Aherna6ffaf92013-08-07 22:50:51 -04001810 if (print_ip)
David Ahernb0b35f02013-08-07 22:50:52 -04001811 printf("%c%16" PRIx64, s, node->ip);
David Aherna6ffaf92013-08-07 22:50:51 -04001812
Adrian Huntera4eb24a2013-12-06 09:42:56 +02001813 if (node->map)
1814 addr = node->map->map_ip(node->map, node->ip);
1815
David Ahern787bef12011-05-27 14:28:43 -06001816 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001817 printf(" ");
David Ahern251f4262013-07-28 09:14:34 -06001818 if (print_symoffset) {
Adrian Huntera4eb24a2013-12-06 09:42:56 +02001819 node_al.addr = addr;
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001820 node_al.map = node->map;
1821 symbol__fprintf_symname_offs(node->sym, &node_al, stdout);
David Ahern251f4262013-07-28 09:14:34 -06001822 } else
1823 symbol__fprintf_symname(node->sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001824 }
David Ahern251f4262013-07-28 09:14:34 -06001825
David Ahern610723f2011-05-27 14:28:44 -06001826 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001827 printf(" (");
David Ahern52deff72012-05-29 22:58:26 -06001828 map__fprintf_dsoname(node->map, stdout);
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001829 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001830 }
David Ahernb0b35f02013-08-07 22:50:52 -04001831
Adrian Huntercc8fae12013-12-06 09:42:57 +02001832 if (print_srcline)
1833 map__fprintf_srcline(node->map, addr, "\n ",
1834 stdout);
1835
David Ahernb0b35f02013-08-07 22:50:52 -04001836 if (!print_oneline)
1837 printf("\n");
David Ahernc0230b22011-03-09 22:23:27 -07001838
David Ahern307cbb92013-08-07 22:50:53 -04001839 stack_depth--;
David Ahernd2ff1b12013-11-18 13:32:44 -07001840next:
1841 callchain_cursor_advance(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001842 }
1843
1844 } else {
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001845 if (al->sym && al->sym->ignore)
David Ahernd2ff1b12013-11-18 13:32:44 -07001846 return;
1847
David Aherna6ffaf92013-08-07 22:50:51 -04001848 if (print_ip)
1849 printf("%16" PRIx64, sample->ip);
1850
David Ahern787bef12011-05-27 14:28:43 -06001851 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001852 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001853 if (print_symoffset)
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001854 symbol__fprintf_symname_offs(al->sym, al,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001855 stdout);
1856 else
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001857 symbol__fprintf_symname(al->sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001858 }
1859
1860 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001861 printf(" (");
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001862 map__fprintf_dsoname(al->map, stdout);
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001863 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001864 }
Adrian Huntercc8fae12013-12-06 09:42:57 +02001865
1866 if (print_srcline)
1867 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
David Ahernc0230b22011-03-09 22:23:27 -07001868 }
1869}
Anton Blanchard5d67be92011-07-04 21:57:50 +10001870
1871int perf_session__cpu_bitmap(struct perf_session *session,
1872 const char *cpu_list, unsigned long *cpu_bitmap)
1873{
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04001874 int i, err = -1;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001875 struct cpu_map *map;
1876
1877 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1878 struct perf_evsel *evsel;
1879
1880 evsel = perf_session__find_first_evtype(session, i);
1881 if (!evsel)
1882 continue;
1883
1884 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1885 pr_err("File does not contain CPU events. "
1886 "Remove -c option to proceed.\n");
1887 return -1;
1888 }
1889 }
1890
1891 map = cpu_map__new(cpu_list);
David Ahern47fbe532011-11-13 10:45:27 -07001892 if (map == NULL) {
1893 pr_err("Invalid cpu_list\n");
1894 return -1;
1895 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001896
1897 for (i = 0; i < map->nr; i++) {
1898 int cpu = map->map[i];
1899
1900 if (cpu >= MAX_NR_CPUS) {
1901 pr_err("Requested CPU %d too large. "
1902 "Consider raising MAX_NR_CPUS\n", cpu);
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04001903 goto out_delete_map;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001904 }
1905
1906 set_bit(cpu, cpu_bitmap);
1907 }
1908
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04001909 err = 0;
1910
1911out_delete_map:
Jiri Olsaf30a79b2015-06-23 00:36:04 +02001912 cpu_map__put(map);
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04001913 return err;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001914}
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001915
1916void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1917 bool full)
1918{
1919 struct stat st;
Masanari Iidac5765ec2014-05-15 02:13:38 +09001920 int fd, ret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001921
1922 if (session == NULL || fp == NULL)
1923 return;
1924
Masanari Iidac5765ec2014-05-15 02:13:38 +09001925 fd = perf_data_file__fd(session->file);
1926
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001927 ret = fstat(fd, &st);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001928 if (ret == -1)
1929 return;
1930
1931 fprintf(fp, "# ========\n");
1932 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1933 perf_header__fprintf_info(session, fp, full);
1934 fprintf(fp, "# ========\n#\n");
1935}
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001936
1937
1938int __perf_session__set_tracepoints_handlers(struct perf_session *session,
1939 const struct perf_evsel_str_handler *assocs,
1940 size_t nr_assocs)
1941{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001942 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001943 size_t i;
1944 int err;
1945
1946 for (i = 0; i < nr_assocs; i++) {
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001947 /*
1948 * Adding a handler for an event not in the session,
1949 * just ignore it.
1950 */
1951 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name);
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001952 if (evsel == NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001953 continue;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001954
1955 err = -EEXIST;
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001956 if (evsel->handler != NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001957 goto out;
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001958 evsel->handler = assocs[i].handler;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001959 }
1960
1961 err = 0;
1962out:
1963 return err;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001964}
Adrian Hunter3c659ee2014-10-27 15:49:22 +02001965
1966int perf_event__process_id_index(struct perf_tool *tool __maybe_unused,
1967 union perf_event *event,
1968 struct perf_session *session)
1969{
1970 struct perf_evlist *evlist = session->evlist;
1971 struct id_index_event *ie = &event->id_index;
1972 size_t i, nr, max_nr;
1973
1974 max_nr = (ie->header.size - sizeof(struct id_index_event)) /
1975 sizeof(struct id_index_entry);
1976 nr = ie->nr;
1977 if (nr > max_nr)
1978 return -EINVAL;
1979
1980 if (dump_trace)
1981 fprintf(stdout, " nr: %zu\n", nr);
1982
1983 for (i = 0; i < nr; i++) {
1984 struct id_index_entry *e = &ie->entries[i];
1985 struct perf_sample_id *sid;
1986
1987 if (dump_trace) {
1988 fprintf(stdout, " ... id: %"PRIu64, e->id);
1989 fprintf(stdout, " idx: %"PRIu64, e->idx);
1990 fprintf(stdout, " cpu: %"PRId64, e->cpu);
1991 fprintf(stdout, " tid: %"PRId64"\n", e->tid);
1992 }
1993
1994 sid = perf_evlist__id2sid(evlist, e->id);
1995 if (!sid)
1996 return -ENOENT;
1997 sid->idx = e->idx;
1998 sid->cpu = e->cpu;
1999 sid->tid = e->tid;
2000 }
2001 return 0;
2002}
2003
2004int perf_event__synthesize_id_index(struct perf_tool *tool,
2005 perf_event__handler_t process,
2006 struct perf_evlist *evlist,
2007 struct machine *machine)
2008{
2009 union perf_event *ev;
2010 struct perf_evsel *evsel;
2011 size_t nr = 0, i = 0, sz, max_nr, n;
2012 int err;
2013
2014 pr_debug2("Synthesizing id index\n");
2015
2016 max_nr = (UINT16_MAX - sizeof(struct id_index_event)) /
2017 sizeof(struct id_index_entry);
2018
Arnaldo Carvalho de Melocba9b842014-10-29 11:31:54 -02002019 evlist__for_each(evlist, evsel)
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002020 nr += evsel->ids;
2021
2022 n = nr > max_nr ? max_nr : nr;
2023 sz = sizeof(struct id_index_event) + n * sizeof(struct id_index_entry);
2024 ev = zalloc(sz);
2025 if (!ev)
2026 return -ENOMEM;
2027
2028 ev->id_index.header.type = PERF_RECORD_ID_INDEX;
2029 ev->id_index.header.size = sz;
2030 ev->id_index.nr = n;
2031
Arnaldo Carvalho de Melocba9b842014-10-29 11:31:54 -02002032 evlist__for_each(evlist, evsel) {
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002033 u32 j;
2034
2035 for (j = 0; j < evsel->ids; j++) {
2036 struct id_index_entry *e;
2037 struct perf_sample_id *sid;
2038
2039 if (i >= n) {
2040 err = process(tool, ev, NULL, machine);
2041 if (err)
2042 goto out_err;
2043 nr -= n;
2044 i = 0;
2045 }
2046
2047 e = &ev->id_index.entries[i++];
2048
2049 e->id = evsel->id[j];
2050
2051 sid = perf_evlist__id2sid(evlist, e->id);
2052 if (!sid) {
2053 free(ev);
2054 return -ENOENT;
2055 }
2056
2057 e->idx = sid->idx;
2058 e->cpu = sid->cpu;
2059 e->tid = sid->tid;
2060 }
2061 }
2062
2063 sz = sizeof(struct id_index_event) + nr * sizeof(struct id_index_entry);
2064 ev->id_index.header.size = sz;
2065 ev->id_index.nr = nr;
2066
2067 err = process(tool, ev, NULL, machine);
2068out_err:
2069 free(ev);
2070
2071 return err;
2072}