blob: 728cb115fbb83903d6df7445080fa59aa9e8ab7a [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 }
141 }
142
143 if (!file || perf_data_file__is_write(file)) {
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200144 /*
145 * In O_RDONLY mode this will be performed when reading the
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200146 * kernel MMAP event, in perf_event__process_mmap().
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200147 */
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300148 if (perf_session__create_kernel_maps(session) < 0)
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700149 pr_warning("Cannot read kernel map\n");
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200150 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200151
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200152 if (tool && tool->ordering_requires_timestamps &&
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200153 tool->ordered_events && !perf_evlist__sample_id_all(session->evlist)) {
Ian Munsie21ef97f2010-12-10 14:09:16 +1100154 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200155 tool->ordered_events = false;
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300156 }
Ian Munsie21ef97f2010-12-10 14:09:16 +1100157
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300158 return session;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200159
160 out_close:
161 perf_data_file__close(file);
162 out_delete:
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300163 perf_session__delete(session);
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200164 out:
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200165 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200166}
167
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200168static void perf_session__delete_threads(struct perf_session *session)
169{
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300170 machine__delete_threads(&session->machines.host);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200171}
172
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300173void perf_session__delete(struct perf_session *session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200174{
Adrian Hunterc4468702015-04-09 18:53:48 +0300175 auxtrace__free(session);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300176 auxtrace_index__free(&session->auxtrace_index);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300177 perf_session__destroy_kernel_maps(session);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300178 perf_session__delete_threads(session);
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -0300179 perf_env__exit(&session->header.env);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300180 machines__exit(&session->machines);
181 if (session->file)
182 perf_data_file__close(session->file);
183 free(session);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200184}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200185
Adrian Hunter47c3d102013-07-04 16:20:21 +0300186static int process_event_synth_tracing_data_stub(struct perf_tool *tool
187 __maybe_unused,
188 union perf_event *event
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300189 __maybe_unused,
190 struct perf_session *session
191 __maybe_unused)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200192{
193 dump_printf(": unhandled!\n");
194 return 0;
195}
196
Adrian Hunter47c3d102013-07-04 16:20:21 +0300197static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused,
198 union perf_event *event __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300199 struct perf_evlist **pevlist
200 __maybe_unused)
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200201{
202 dump_printf(": unhandled!\n");
203 return 0;
204}
205
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300206static int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
207 union perf_event *event __maybe_unused,
208 struct perf_sample *sample __maybe_unused,
209 struct perf_evsel *evsel __maybe_unused,
210 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300211{
212 dump_printf(": unhandled!\n");
213 return 0;
214}
215
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300216static int process_event_stub(struct perf_tool *tool __maybe_unused,
217 union perf_event *event __maybe_unused,
218 struct perf_sample *sample __maybe_unused,
219 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200220{
221 dump_printf(": unhandled!\n");
222 return 0;
223}
224
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300225static int process_build_id_stub(struct perf_tool *tool __maybe_unused,
226 union perf_event *event __maybe_unused,
227 struct perf_session *session __maybe_unused)
228{
229 dump_printf(": unhandled!\n");
230 return 0;
231}
232
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300233static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
234 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300235 struct ordered_events *oe __maybe_unused)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200236{
237 dump_printf(": unhandled!\n");
238 return 0;
239}
240
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200241static int process_finished_round(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200242 union perf_event *event,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300243 struct ordered_events *oe);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200244
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200245static int process_id_index_stub(struct perf_tool *tool __maybe_unused,
246 union perf_event *event __maybe_unused,
247 struct perf_session *perf_session
248 __maybe_unused)
249{
250 dump_printf(": unhandled!\n");
251 return 0;
252}
253
Adrian Huntera16ac022015-04-09 18:53:43 +0300254static int process_event_auxtrace_info_stub(struct perf_tool *tool __maybe_unused,
255 union perf_event *event __maybe_unused,
256 struct perf_session *session __maybe_unused)
257{
258 dump_printf(": unhandled!\n");
259 return 0;
260}
261
262static int skipn(int fd, off_t n)
263{
264 char buf[4096];
265 ssize_t ret;
266
267 while (n > 0) {
268 ret = read(fd, buf, min(n, (off_t)sizeof(buf)));
269 if (ret <= 0)
270 return ret;
271 n -= ret;
272 }
273
274 return 0;
275}
276
277static s64 process_event_auxtrace_stub(struct perf_tool *tool __maybe_unused,
278 union perf_event *event,
279 struct perf_session *session
280 __maybe_unused)
281{
282 dump_printf(": unhandled!\n");
283 if (perf_data_file__is_pipe(session->file))
284 skipn(perf_data_file__fd(session->file), event->auxtrace.size);
285 return event->auxtrace.size;
286}
287
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300288static
289int process_event_auxtrace_error_stub(struct perf_tool *tool __maybe_unused,
290 union perf_event *event __maybe_unused,
291 struct perf_session *session __maybe_unused)
292{
293 dump_printf(": unhandled!\n");
294 return 0;
295}
296
David Ahern9c501402013-08-02 14:05:41 -0600297void perf_tool__fill_defaults(struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200298{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200299 if (tool->sample == NULL)
300 tool->sample = process_event_sample_stub;
301 if (tool->mmap == NULL)
302 tool->mmap = process_event_stub;
David Ahern6adb0b02013-09-22 19:44:59 -0600303 if (tool->mmap2 == NULL)
304 tool->mmap2 = process_event_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200305 if (tool->comm == NULL)
306 tool->comm = process_event_stub;
307 if (tool->fork == NULL)
308 tool->fork = process_event_stub;
309 if (tool->exit == NULL)
310 tool->exit = process_event_stub;
311 if (tool->lost == NULL)
312 tool->lost = perf_event__process_lost;
Kan Liangc4937a92015-05-10 15:13:15 -0400313 if (tool->lost_samples == NULL)
314 tool->lost_samples = perf_event__process_lost_samples;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300315 if (tool->aux == NULL)
316 tool->aux = perf_event__process_aux;
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300317 if (tool->itrace_start == NULL)
318 tool->itrace_start = perf_event__process_itrace_start;
Adrian Hunter02860392015-07-21 12:44:03 +0300319 if (tool->context_switch == NULL)
320 tool->context_switch = perf_event__process_switch;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200321 if (tool->read == NULL)
322 tool->read = process_event_sample_stub;
323 if (tool->throttle == NULL)
324 tool->throttle = process_event_stub;
325 if (tool->unthrottle == NULL)
326 tool->unthrottle = process_event_stub;
327 if (tool->attr == NULL)
328 tool->attr = process_event_synth_attr_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200329 if (tool->tracing_data == NULL)
330 tool->tracing_data = process_event_synth_tracing_data_stub;
331 if (tool->build_id == NULL)
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300332 tool->build_id = process_build_id_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200333 if (tool->finished_round == NULL) {
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200334 if (tool->ordered_events)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200335 tool->finished_round = process_finished_round;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200336 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200337 tool->finished_round = process_finished_round_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200338 }
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200339 if (tool->id_index == NULL)
340 tool->id_index = process_id_index_stub;
Adrian Huntera16ac022015-04-09 18:53:43 +0300341 if (tool->auxtrace_info == NULL)
342 tool->auxtrace_info = process_event_auxtrace_info_stub;
343 if (tool->auxtrace == NULL)
344 tool->auxtrace = process_event_auxtrace_stub;
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300345 if (tool->auxtrace_error == NULL)
346 tool->auxtrace_error = process_event_auxtrace_error_stub;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200347}
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300348
Jiri Olsa268fb202012-05-30 14:23:43 +0200349static void swap_sample_id_all(union perf_event *event, void *data)
350{
351 void *end = (void *) event + event->header.size;
352 int size = end - data;
353
354 BUG_ON(size % sizeof(u64));
355 mem_bswap_64(data, size);
356}
357
358static void perf_event__all64_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300359 bool sample_id_all __maybe_unused)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200360{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200361 struct perf_event_header *hdr = &event->header;
362 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200363}
364
Jiri Olsa268fb202012-05-30 14:23:43 +0200365static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200366{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200367 event->comm.pid = bswap_32(event->comm.pid);
368 event->comm.tid = bswap_32(event->comm.tid);
Jiri Olsa268fb202012-05-30 14:23:43 +0200369
370 if (sample_id_all) {
371 void *data = &event->comm.comm;
372
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300373 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200374 swap_sample_id_all(event, data);
375 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200376}
377
Jiri Olsa268fb202012-05-30 14:23:43 +0200378static void perf_event__mmap_swap(union perf_event *event,
379 bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200380{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200381 event->mmap.pid = bswap_32(event->mmap.pid);
382 event->mmap.tid = bswap_32(event->mmap.tid);
383 event->mmap.start = bswap_64(event->mmap.start);
384 event->mmap.len = bswap_64(event->mmap.len);
385 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
Jiri Olsa268fb202012-05-30 14:23:43 +0200386
387 if (sample_id_all) {
388 void *data = &event->mmap.filename;
389
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300390 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200391 swap_sample_id_all(event, data);
392 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200393}
394
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200395static void perf_event__mmap2_swap(union perf_event *event,
396 bool sample_id_all)
397{
398 event->mmap2.pid = bswap_32(event->mmap2.pid);
399 event->mmap2.tid = bswap_32(event->mmap2.tid);
400 event->mmap2.start = bswap_64(event->mmap2.start);
401 event->mmap2.len = bswap_64(event->mmap2.len);
402 event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
403 event->mmap2.maj = bswap_32(event->mmap2.maj);
404 event->mmap2.min = bswap_32(event->mmap2.min);
405 event->mmap2.ino = bswap_64(event->mmap2.ino);
406
407 if (sample_id_all) {
408 void *data = &event->mmap2.filename;
409
410 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
411 swap_sample_id_all(event, data);
412 }
413}
Jiri Olsa268fb202012-05-30 14:23:43 +0200414static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200415{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200416 event->fork.pid = bswap_32(event->fork.pid);
417 event->fork.tid = bswap_32(event->fork.tid);
418 event->fork.ppid = bswap_32(event->fork.ppid);
419 event->fork.ptid = bswap_32(event->fork.ptid);
420 event->fork.time = bswap_64(event->fork.time);
Jiri Olsa268fb202012-05-30 14:23:43 +0200421
422 if (sample_id_all)
423 swap_sample_id_all(event, &event->fork + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200424}
425
Jiri Olsa268fb202012-05-30 14:23:43 +0200426static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200427{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200428 event->read.pid = bswap_32(event->read.pid);
429 event->read.tid = bswap_32(event->read.tid);
430 event->read.value = bswap_64(event->read.value);
431 event->read.time_enabled = bswap_64(event->read.time_enabled);
432 event->read.time_running = bswap_64(event->read.time_running);
433 event->read.id = bswap_64(event->read.id);
Jiri Olsa268fb202012-05-30 14:23:43 +0200434
435 if (sample_id_all)
436 swap_sample_id_all(event, &event->read + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200437}
438
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300439static void perf_event__aux_swap(union perf_event *event, bool sample_id_all)
440{
441 event->aux.aux_offset = bswap_64(event->aux.aux_offset);
442 event->aux.aux_size = bswap_64(event->aux.aux_size);
443 event->aux.flags = bswap_64(event->aux.flags);
444
445 if (sample_id_all)
446 swap_sample_id_all(event, &event->aux + 1);
447}
448
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300449static void perf_event__itrace_start_swap(union perf_event *event,
450 bool sample_id_all)
451{
452 event->itrace_start.pid = bswap_32(event->itrace_start.pid);
453 event->itrace_start.tid = bswap_32(event->itrace_start.tid);
454
455 if (sample_id_all)
456 swap_sample_id_all(event, &event->itrace_start + 1);
457}
458
Adrian Hunter02860392015-07-21 12:44:03 +0300459static void perf_event__switch_swap(union perf_event *event, bool sample_id_all)
460{
461 if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
462 event->context_switch.next_prev_pid =
463 bswap_32(event->context_switch.next_prev_pid);
464 event->context_switch.next_prev_tid =
465 bswap_32(event->context_switch.next_prev_tid);
466 }
467
468 if (sample_id_all)
469 swap_sample_id_all(event, &event->context_switch + 1);
470}
471
Jiri Olsadd96c462013-09-01 12:36:15 +0200472static void perf_event__throttle_swap(union perf_event *event,
473 bool sample_id_all)
474{
475 event->throttle.time = bswap_64(event->throttle.time);
476 event->throttle.id = bswap_64(event->throttle.id);
477 event->throttle.stream_id = bswap_64(event->throttle.stream_id);
478
479 if (sample_id_all)
480 swap_sample_id_all(event, &event->throttle + 1);
481}
482
Jiri Olsae108c662012-05-16 08:59:03 +0200483static u8 revbyte(u8 b)
484{
485 int rev = (b >> 4) | ((b & 0xf) << 4);
486 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
487 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
488 return (u8) rev;
489}
490
491/*
492 * XXX this is hack in attempt to carry flags bitfield
493 * throught endian village. ABI says:
494 *
495 * Bit-fields are allocated from right to left (least to most significant)
496 * on little-endian implementations and from left to right (most to least
497 * significant) on big-endian implementations.
498 *
499 * The above seems to be byte specific, so we need to reverse each
500 * byte of the bitfield. 'Internet' also says this might be implementation
501 * specific and we probably need proper fix and carry perf_event_attr
502 * bitfield flags in separate data file FEAT_ section. Thought this seems
503 * to work for now.
504 */
505static void swap_bitfield(u8 *p, unsigned len)
506{
507 unsigned i;
508
509 for (i = 0; i < len; i++) {
510 *p = revbyte(*p);
511 p++;
512 }
513}
514
David Aherneda39132011-07-15 12:34:09 -0600515/* exported for swapping attributes in file header */
516void perf_event__attr_swap(struct perf_event_attr *attr)
517{
518 attr->type = bswap_32(attr->type);
519 attr->size = bswap_32(attr->size);
Jiri Olsae108c662012-05-16 08:59:03 +0200520
Wang Nanb30b6172015-06-17 09:56:39 +0000521#define bswap_safe(f, n) \
522 (attr->size > (offsetof(struct perf_event_attr, f) + \
523 sizeof(attr->f) * (n)))
524#define bswap_field(f, sz) \
525do { \
526 if (bswap_safe(f, 0)) \
527 attr->f = bswap_##sz(attr->f); \
528} while(0)
529#define bswap_field_32(f) bswap_field(f, 32)
530#define bswap_field_64(f) bswap_field(f, 64)
531
532 bswap_field_64(config);
533 bswap_field_64(sample_period);
534 bswap_field_64(sample_type);
535 bswap_field_64(read_format);
536 bswap_field_32(wakeup_events);
537 bswap_field_32(bp_type);
538 bswap_field_64(bp_addr);
539 bswap_field_64(bp_len);
540 bswap_field_64(branch_sample_type);
541 bswap_field_64(sample_regs_user);
542 bswap_field_32(sample_stack_user);
543 bswap_field_32(aux_watermark);
544
545 /*
546 * After read_format are bitfields. Check read_format because
547 * we are unable to use offsetof on bitfield.
548 */
549 if (bswap_safe(read_format, 1))
550 swap_bitfield((u8 *) (&attr->read_format + 1),
551 sizeof(u64));
552#undef bswap_field_64
553#undef bswap_field_32
554#undef bswap_field
555#undef bswap_safe
David Aherneda39132011-07-15 12:34:09 -0600556}
557
Jiri Olsa268fb202012-05-30 14:23:43 +0200558static void perf_event__hdr_attr_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300559 bool sample_id_all __maybe_unused)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500560{
561 size_t size;
562
David Aherneda39132011-07-15 12:34:09 -0600563 perf_event__attr_swap(&event->attr.attr);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500564
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200565 size = event->header.size;
566 size -= (void *)&event->attr.id - (void *)event;
567 mem_bswap_64(event->attr.id, size);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500568}
569
Jiri Olsa268fb202012-05-30 14:23:43 +0200570static void perf_event__event_type_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300571 bool sample_id_all __maybe_unused)
Tom Zanussicd19a032010-04-01 23:59:20 -0500572{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200573 event->event_type.event_type.event_id =
574 bswap_64(event->event_type.event_type.event_id);
Tom Zanussicd19a032010-04-01 23:59:20 -0500575}
576
Jiri Olsa268fb202012-05-30 14:23:43 +0200577static void perf_event__tracing_data_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300578 bool sample_id_all __maybe_unused)
Tom Zanussi92155452010-04-01 23:59:21 -0500579{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200580 event->tracing_data.size = bswap_32(event->tracing_data.size);
Tom Zanussi92155452010-04-01 23:59:21 -0500581}
582
Adrian Huntera16ac022015-04-09 18:53:43 +0300583static void perf_event__auxtrace_info_swap(union perf_event *event,
584 bool sample_id_all __maybe_unused)
585{
586 size_t size;
587
588 event->auxtrace_info.type = bswap_32(event->auxtrace_info.type);
589
590 size = event->header.size;
591 size -= (void *)&event->auxtrace_info.priv - (void *)event;
592 mem_bswap_64(event->auxtrace_info.priv, size);
593}
594
595static void perf_event__auxtrace_swap(union perf_event *event,
596 bool sample_id_all __maybe_unused)
597{
598 event->auxtrace.size = bswap_64(event->auxtrace.size);
599 event->auxtrace.offset = bswap_64(event->auxtrace.offset);
600 event->auxtrace.reference = bswap_64(event->auxtrace.reference);
601 event->auxtrace.idx = bswap_32(event->auxtrace.idx);
602 event->auxtrace.tid = bswap_32(event->auxtrace.tid);
603 event->auxtrace.cpu = bswap_32(event->auxtrace.cpu);
604}
605
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300606static void perf_event__auxtrace_error_swap(union perf_event *event,
607 bool sample_id_all __maybe_unused)
608{
609 event->auxtrace_error.type = bswap_32(event->auxtrace_error.type);
610 event->auxtrace_error.code = bswap_32(event->auxtrace_error.code);
611 event->auxtrace_error.cpu = bswap_32(event->auxtrace_error.cpu);
612 event->auxtrace_error.pid = bswap_32(event->auxtrace_error.pid);
613 event->auxtrace_error.tid = bswap_32(event->auxtrace_error.tid);
614 event->auxtrace_error.ip = bswap_64(event->auxtrace_error.ip);
615}
616
Jiri Olsa268fb202012-05-30 14:23:43 +0200617typedef void (*perf_event__swap_op)(union perf_event *event,
618 bool sample_id_all);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200619
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200620static perf_event__swap_op perf_event__swap_ops[] = {
621 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200622 [PERF_RECORD_MMAP2] = perf_event__mmap2_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200623 [PERF_RECORD_COMM] = perf_event__comm_swap,
624 [PERF_RECORD_FORK] = perf_event__task_swap,
625 [PERF_RECORD_EXIT] = perf_event__task_swap,
626 [PERF_RECORD_LOST] = perf_event__all64_swap,
627 [PERF_RECORD_READ] = perf_event__read_swap,
Jiri Olsadd96c462013-09-01 12:36:15 +0200628 [PERF_RECORD_THROTTLE] = perf_event__throttle_swap,
629 [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200630 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300631 [PERF_RECORD_AUX] = perf_event__aux_swap,
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300632 [PERF_RECORD_ITRACE_START] = perf_event__itrace_start_swap,
Kan Liangc4937a92015-05-10 15:13:15 -0400633 [PERF_RECORD_LOST_SAMPLES] = perf_event__all64_swap,
Adrian Hunter02860392015-07-21 12:44:03 +0300634 [PERF_RECORD_SWITCH] = perf_event__switch_swap,
635 [PERF_RECORD_SWITCH_CPU_WIDE] = perf_event__switch_swap,
David Aherneda39132011-07-15 12:34:09 -0600636 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200637 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
638 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
639 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200640 [PERF_RECORD_ID_INDEX] = perf_event__all64_swap,
Adrian Huntera16ac022015-04-09 18:53:43 +0300641 [PERF_RECORD_AUXTRACE_INFO] = perf_event__auxtrace_info_swap,
642 [PERF_RECORD_AUXTRACE] = perf_event__auxtrace_swap,
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300643 [PERF_RECORD_AUXTRACE_ERROR] = perf_event__auxtrace_error_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200644 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200645};
646
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200647/*
648 * When perf record finishes a pass on every buffers, it records this pseudo
649 * event.
650 * We record the max timestamp t found in the pass n.
651 * Assuming these timestamps are monotonic across cpus, we know that if
652 * a buffer still has events with timestamps below t, they will be all
653 * available and then read in the pass n + 1.
654 * Hence when we start to read the pass n + 2, we can safely flush every
655 * events with timestamps below t.
656 *
657 * ============ PASS n =================
658 * CPU 0 | CPU 1
659 * |
660 * cnt1 timestamps | cnt2 timestamps
661 * 1 | 2
662 * 2 | 3
663 * - | 4 <--- max recorded
664 *
665 * ============ PASS n + 1 ==============
666 * CPU 0 | CPU 1
667 * |
668 * cnt1 timestamps | cnt2 timestamps
669 * 3 | 5
670 * 4 | 6
671 * 5 | 7 <---- max recorded
672 *
673 * Flush every events below timestamp 4
674 *
675 * ============ PASS n + 2 ==============
676 * CPU 0 | CPU 1
677 * |
678 * cnt1 timestamps | cnt2 timestamps
679 * 6 | 8
680 * 7 | 9
681 * - | 10
682 *
683 * Flush every events below timestamp 7
684 * etc...
685 */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300686static int process_finished_round(struct perf_tool *tool __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300687 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300688 struct ordered_events *oe)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200689{
Adrian Hunter5531e162015-06-23 10:52:48 +0300690 if (dump_trace)
691 fprintf(stdout, "\n");
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300692 return ordered_events__flush(oe, OE_FLUSH__ROUND);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200693}
694
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300695int perf_session__queue_event(struct perf_session *s, union perf_event *event,
696 struct perf_sample *sample, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200697{
Arnaldo Carvalho de Melo4a6b3622015-03-03 13:02:24 -0300698 return ordered_events__queue(&s->ordered_events, event, sample, file_offset);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200699}
700
Kan Liang384b6052015-01-05 13:23:05 -0500701static void callchain__lbr_callstack_printf(struct perf_sample *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200702{
Kan Liang384b6052015-01-05 13:23:05 -0500703 struct ip_callchain *callchain = sample->callchain;
704 struct branch_stack *lbr_stack = sample->branch_stack;
705 u64 kernel_callchain_nr = callchain->nr;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200706 unsigned int i;
707
Kan Liang384b6052015-01-05 13:23:05 -0500708 for (i = 0; i < kernel_callchain_nr; i++) {
709 if (callchain->ips[i] == PERF_CONTEXT_USER)
710 break;
711 }
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200712
Kan Liang384b6052015-01-05 13:23:05 -0500713 if ((i != kernel_callchain_nr) && lbr_stack->nr) {
714 u64 total_nr;
715 /*
716 * LBR callstack can only get user call chain,
717 * i is kernel call chain number,
718 * 1 is PERF_CONTEXT_USER.
719 *
720 * The user call chain is stored in LBR registers.
721 * LBR are pair registers. The caller is stored
722 * in "from" register, while the callee is stored
723 * in "to" register.
724 * For example, there is a call stack
725 * "A"->"B"->"C"->"D".
726 * The LBR registers will recorde like
727 * "C"->"D", "B"->"C", "A"->"B".
728 * So only the first "to" register and all "from"
729 * registers are needed to construct the whole stack.
730 */
731 total_nr = i + 1 + lbr_stack->nr + 1;
732 kernel_callchain_nr = i + 1;
733
734 printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr);
735
736 for (i = 0; i < kernel_callchain_nr; i++)
737 printf("..... %2d: %016" PRIx64 "\n",
738 i, callchain->ips[i]);
739
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200740 printf("..... %2d: %016" PRIx64 "\n",
Kan Liang384b6052015-01-05 13:23:05 -0500741 (int)(kernel_callchain_nr), lbr_stack->entries[0].to);
742 for (i = 0; i < lbr_stack->nr; i++)
743 printf("..... %2d: %016" PRIx64 "\n",
744 (int)(i + kernel_callchain_nr + 1), lbr_stack->entries[i].from);
745 }
746}
747
748static void callchain__printf(struct perf_evsel *evsel,
749 struct perf_sample *sample)
750{
751 unsigned int i;
752 struct ip_callchain *callchain = sample->callchain;
753
754 if (has_branch_callstack(evsel))
755 callchain__lbr_callstack_printf(sample);
756
757 printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr);
758
759 for (i = 0; i < callchain->nr; i++)
760 printf("..... %2d: %016" PRIx64 "\n",
761 i, callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200762}
763
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100764static void branch_stack__printf(struct perf_sample *sample)
765{
766 uint64_t i;
767
768 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
769
Andi Kleen0e332f02015-07-18 08:24:46 -0700770 for (i = 0; i < sample->branch_stack->nr; i++) {
771 struct branch_entry *e = &sample->branch_stack->entries[i];
772
773 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 " %hu cycles %s%s%s%s %x\n",
774 i, e->from, e->to,
775 e->flags.cycles,
776 e->flags.mispred ? "M" : " ",
777 e->flags.predicted ? "P" : " ",
778 e->flags.abort ? "A" : " ",
779 e->flags.in_tx ? "T" : " ",
780 (unsigned)e->flags.reserved);
781 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100782}
783
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200784static void regs_dump__printf(u64 mask, u64 *regs)
785{
786 unsigned rid, i = 0;
787
788 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
789 u64 val = regs[i++];
790
791 printf(".... %-5s 0x%" PRIx64 "\n",
792 perf_reg_name(rid), val);
793 }
794}
795
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200796static const char *regs_abi[] = {
797 [PERF_SAMPLE_REGS_ABI_NONE] = "none",
798 [PERF_SAMPLE_REGS_ABI_32] = "32-bit",
799 [PERF_SAMPLE_REGS_ABI_64] = "64-bit",
800};
801
802static inline const char *regs_dump_abi(struct regs_dump *d)
803{
804 if (d->abi > PERF_SAMPLE_REGS_ABI_64)
805 return "unknown";
806
807 return regs_abi[d->abi];
808}
809
810static void regs__printf(const char *type, struct regs_dump *regs)
811{
812 u64 mask = regs->mask;
813
814 printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n",
815 type,
816 mask,
817 regs_dump_abi(regs));
818
819 regs_dump__printf(mask, regs->regs);
820}
821
Jiri Olsa352ea452014-01-07 13:47:25 +0100822static void regs_user__printf(struct perf_sample *sample)
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200823{
824 struct regs_dump *user_regs = &sample->user_regs;
825
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200826 if (user_regs->regs)
827 regs__printf("user", user_regs);
828}
829
830static void regs_intr__printf(struct perf_sample *sample)
831{
832 struct regs_dump *intr_regs = &sample->intr_regs;
833
834 if (intr_regs->regs)
835 regs__printf("intr", intr_regs);
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200836}
837
838static void stack_user__printf(struct stack_dump *dump)
839{
840 printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
841 dump->size, dump->offset);
842}
843
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300844static void perf_evlist__print_tstamp(struct perf_evlist *evlist,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200845 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200846 struct perf_sample *sample)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200847{
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300848 u64 sample_type = __perf_evlist__combined_sample_type(evlist);
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300849
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200850 if (event->header.type != PERF_RECORD_SAMPLE &&
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300851 !perf_evlist__sample_id_all(evlist)) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200852 fputs("-1 -1 ", stdout);
853 return;
854 }
855
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300856 if ((sample_type & PERF_SAMPLE_CPU))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200857 printf("%u ", sample->cpu);
858
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300859 if (sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200860 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200861}
862
Jiri Olsa9ede4732012-10-10 17:38:13 +0200863static void sample_read__printf(struct perf_sample *sample, u64 read_format)
864{
865 printf("... sample_read:\n");
866
867 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
868 printf("...... time enabled %016" PRIx64 "\n",
869 sample->read.time_enabled);
870
871 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
872 printf("...... time running %016" PRIx64 "\n",
873 sample->read.time_running);
874
875 if (read_format & PERF_FORMAT_GROUP) {
876 u64 i;
877
878 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
879
880 for (i = 0; i < sample->read.group.nr; i++) {
881 struct sample_read_value *value;
882
883 value = &sample->read.group.values[i];
884 printf("..... id %016" PRIx64
885 ", value %016" PRIx64 "\n",
886 value->id, value->value);
887 }
888 } else
889 printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
890 sample->read.one.id, sample->read.one.value);
891}
892
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300893static void dump_event(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200894 u64 file_offset, struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000895{
896 if (!dump_trace)
897 return;
898
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200899 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
900 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000901
902 trace_event(event);
903
904 if (sample)
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300905 perf_evlist__print_tstamp(evlist, event, sample);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000906
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200907 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200908 event->header.size, perf_event__name(event->header.type));
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000909}
910
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200911static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200912 struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000913{
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300914 u64 sample_type;
915
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200916 if (!dump_trace)
917 return;
918
Don Zickus0ea590a2014-02-25 22:43:46 -0500919 printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200920 event->header.misc, sample->pid, sample->tid, sample->ip,
David Ahern7cec0922011-05-30 13:08:23 -0600921 sample->period, sample->addr);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000922
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200923 sample_type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300924
925 if (sample_type & PERF_SAMPLE_CALLCHAIN)
Kan Liang384b6052015-01-05 13:23:05 -0500926 callchain__printf(evsel, sample);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100927
Kan Liang384b6052015-01-05 13:23:05 -0500928 if ((sample_type & PERF_SAMPLE_BRANCH_STACK) && !has_branch_callstack(evsel))
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100929 branch_stack__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200930
931 if (sample_type & PERF_SAMPLE_REGS_USER)
Jiri Olsa352ea452014-01-07 13:47:25 +0100932 regs_user__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200933
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200934 if (sample_type & PERF_SAMPLE_REGS_INTR)
935 regs_intr__printf(sample);
936
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200937 if (sample_type & PERF_SAMPLE_STACK_USER)
938 stack_user__printf(&sample->user_stack);
Andi Kleen05484292013-01-24 16:10:29 +0100939
940 if (sample_type & PERF_SAMPLE_WEIGHT)
941 printf("... weight: %" PRIu64 "\n", sample->weight);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100942
943 if (sample_type & PERF_SAMPLE_DATA_SRC)
944 printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
Jiri Olsa9ede4732012-10-10 17:38:13 +0200945
Andi Kleen475eeab2013-09-20 07:40:43 -0700946 if (sample_type & PERF_SAMPLE_TRANSACTION)
947 printf("... transaction: %" PRIx64 "\n", sample->transaction);
948
Jiri Olsa9ede4732012-10-10 17:38:13 +0200949 if (sample_type & PERF_SAMPLE_READ)
950 sample_read__printf(sample, evsel->attr.read_format);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000951}
952
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -0300953static struct machine *machines__find_for_cpumode(struct machines *machines,
Adrian Hunteref893252013-08-27 11:23:06 +0300954 union perf_event *event,
955 struct perf_sample *sample)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200956{
957 const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Dongsheng Yangad85ace2013-12-20 13:41:47 -0500958 struct machine *machine;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200959
David Ahern7c0f4a42012-07-20 17:25:48 -0600960 if (perf_guest &&
961 ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
962 (cpumode == PERF_RECORD_MISC_GUEST_USER))) {
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530963 u32 pid;
964
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200965 if (event->header.type == PERF_RECORD_MMAP
966 || event->header.type == PERF_RECORD_MMAP2)
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530967 pid = event->mmap.pid;
968 else
Adrian Hunteref893252013-08-27 11:23:06 +0300969 pid = sample->pid;
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530970
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -0300971 machine = machines__find(machines, pid);
Dongsheng Yangad85ace2013-12-20 13:41:47 -0500972 if (!machine)
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -0300973 machine = machines__find(machines, DEFAULT_GUEST_KERNEL_ID);
Dongsheng Yangad85ace2013-12-20 13:41:47 -0500974 return machine;
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530975 }
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200976
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -0300977 return &machines->host;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200978}
979
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -0300980static int deliver_sample_value(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +0200981 struct perf_tool *tool,
982 union perf_event *event,
983 struct perf_sample *sample,
984 struct sample_read_value *v,
985 struct machine *machine)
986{
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -0300987 struct perf_sample_id *sid = perf_evlist__id2sid(evlist, v->id);
Jiri Olsae4caec02012-10-10 18:52:24 +0200988
Jiri Olsae4caec02012-10-10 18:52:24 +0200989 if (sid) {
990 sample->id = v->id;
991 sample->period = v->value - sid->period;
992 sid->period = v->value;
993 }
994
995 if (!sid || sid->evsel == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -0300996 ++evlist->stats.nr_unknown_id;
Jiri Olsae4caec02012-10-10 18:52:24 +0200997 return 0;
998 }
999
1000 return tool->sample(tool, event, sample, sid->evsel, machine);
1001}
1002
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001003static int deliver_sample_group(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +02001004 struct perf_tool *tool,
1005 union perf_event *event,
1006 struct perf_sample *sample,
1007 struct machine *machine)
1008{
1009 int ret = -EINVAL;
1010 u64 i;
1011
1012 for (i = 0; i < sample->read.group.nr; i++) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001013 ret = deliver_sample_value(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001014 &sample->read.group.values[i],
1015 machine);
1016 if (ret)
1017 break;
1018 }
1019
1020 return ret;
1021}
1022
1023static int
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001024 perf_evlist__deliver_sample(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +02001025 struct perf_tool *tool,
1026 union perf_event *event,
1027 struct perf_sample *sample,
1028 struct perf_evsel *evsel,
1029 struct machine *machine)
1030{
1031 /* We know evsel != NULL. */
1032 u64 sample_type = evsel->attr.sample_type;
1033 u64 read_format = evsel->attr.read_format;
1034
1035 /* Standard sample delievery. */
1036 if (!(sample_type & PERF_SAMPLE_READ))
1037 return tool->sample(tool, event, sample, evsel, machine);
1038
1039 /* For PERF_SAMPLE_READ we have either single or group mode. */
1040 if (read_format & PERF_FORMAT_GROUP)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001041 return deliver_sample_group(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001042 machine);
1043 else
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001044 return deliver_sample_value(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001045 &sample->read.one, machine);
1046}
1047
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -03001048static int machines__deliver_event(struct machines *machines,
1049 struct perf_evlist *evlist,
1050 union perf_event *event,
1051 struct perf_sample *sample,
1052 struct perf_tool *tool, u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001053{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001054 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001055 struct machine *machine;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001056
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001057 dump_event(evlist, event, file_offset, sample);
Thomas Gleixner532e7262010-12-07 12:48:55 +00001058
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001059 evsel = perf_evlist__id2evsel(evlist, sample->id);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001060
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001061 machine = machines__find_for_cpumode(machines, event, sample);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001062
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001063 switch (event->header.type) {
1064 case PERF_RECORD_SAMPLE:
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001065 if (evsel == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001066 ++evlist->stats.nr_unknown_id;
Jiri Olsa67822062012-04-12 14:21:01 +02001067 return 0;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001068 }
Kan Liang1b29ac52015-09-03 08:31:00 -04001069 dump_sample(evsel, event, sample);
Joerg Roedel0c095712012-02-10 18:05:04 +01001070 if (machine == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001071 ++evlist->stats.nr_unprocessable_samples;
Jiri Olsa67822062012-04-12 14:21:01 +02001072 return 0;
Joerg Roedel0c095712012-02-10 18:05:04 +01001073 }
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001074 return perf_evlist__deliver_sample(evlist, tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001075 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001076 return tool->mmap(tool, event, sample, machine);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001077 case PERF_RECORD_MMAP2:
Kan Liang930e6fc2015-06-17 09:51:10 -04001078 if (event->header.misc & PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT)
1079 ++evlist->stats.nr_proc_map_timeout;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001080 return tool->mmap2(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001081 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001082 return tool->comm(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001083 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001084 return tool->fork(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001085 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001086 return tool->exit(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001087 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001088 if (tool->lost == perf_event__process_lost)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001089 evlist->stats.total_lost += event->lost.lost;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001090 return tool->lost(tool, event, sample, machine);
Kan Liangc4937a92015-05-10 15:13:15 -04001091 case PERF_RECORD_LOST_SAMPLES:
1092 if (tool->lost_samples == perf_event__process_lost_samples)
1093 evlist->stats.total_lost_samples += event->lost_samples.lost;
1094 return tool->lost_samples(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001095 case PERF_RECORD_READ:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001096 return tool->read(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001097 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001098 return tool->throttle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001099 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001100 return tool->unthrottle(tool, event, sample, machine);
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001101 case PERF_RECORD_AUX:
1102 return tool->aux(tool, event, sample, machine);
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001103 case PERF_RECORD_ITRACE_START:
1104 return tool->itrace_start(tool, event, sample, machine);
Adrian Hunter02860392015-07-21 12:44:03 +03001105 case PERF_RECORD_SWITCH:
1106 case PERF_RECORD_SWITCH_CPU_WIDE:
1107 return tool->context_switch(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001108 default:
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001109 ++evlist->stats.nr_unknown_events;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001110 return -1;
1111 }
1112}
1113
Adrian Hunterc4468702015-04-09 18:53:48 +03001114static int perf_session__deliver_event(struct perf_session *session,
1115 union perf_event *event,
1116 struct perf_sample *sample,
1117 struct perf_tool *tool,
1118 u64 file_offset)
1119{
1120 int ret;
1121
1122 ret = auxtrace__process_event(session, event, sample, tool);
1123 if (ret < 0)
1124 return ret;
1125 if (ret > 0)
1126 return 0;
1127
1128 return machines__deliver_event(&session->machines, session->evlist,
1129 event, sample, tool, file_offset);
1130}
1131
Adrian Hunterd5652d82014-07-23 22:19:58 +03001132static s64 perf_session__process_user_event(struct perf_session *session,
1133 union perf_event *event,
Adrian Hunterd5652d82014-07-23 22:19:58 +03001134 u64 file_offset)
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001135{
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -03001136 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001137 struct perf_tool *tool = session->tool;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001138 int fd = perf_data_file__fd(session->file);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001139 int err;
1140
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001141 dump_event(session->evlist, event, file_offset, NULL);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001142
1143 /* These events are processed right away */
1144 switch (event->header.type) {
1145 case PERF_RECORD_HEADER_ATTR:
Adrian Hunter47c3d102013-07-04 16:20:21 +03001146 err = tool->attr(tool, event, &session->evlist);
Adrian Huntercfe1c412014-07-31 09:00:45 +03001147 if (err == 0) {
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -03001148 perf_session__set_id_hdr_size(session);
Adrian Huntercfe1c412014-07-31 09:00:45 +03001149 perf_session__set_comm_exec(session);
1150 }
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001151 return err;
Jiri Olsaf67697b2014-02-04 15:37:48 +01001152 case PERF_RECORD_HEADER_EVENT_TYPE:
1153 /*
1154 * Depreceated, but we need to handle it for sake
1155 * of old data files create in pipe mode.
1156 */
1157 return 0;
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001158 case PERF_RECORD_HEADER_TRACING_DATA:
1159 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001160 lseek(fd, file_offset, SEEK_SET);
Adrian Hunter47c3d102013-07-04 16:20:21 +03001161 return tool->tracing_data(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001162 case PERF_RECORD_HEADER_BUILD_ID:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001163 return tool->build_id(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001164 case PERF_RECORD_FINISHED_ROUND:
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -03001165 return tool->finished_round(tool, event, oe);
Adrian Hunter3c659ee2014-10-27 15:49:22 +02001166 case PERF_RECORD_ID_INDEX:
1167 return tool->id_index(tool, event, session);
Adrian Huntera16ac022015-04-09 18:53:43 +03001168 case PERF_RECORD_AUXTRACE_INFO:
1169 return tool->auxtrace_info(tool, event, session);
1170 case PERF_RECORD_AUXTRACE:
1171 /* setup for reading amidst mmap */
1172 lseek(fd, file_offset + event->header.size, SEEK_SET);
1173 return tool->auxtrace(tool, event, session);
Adrian Huntere9bf54d2015-04-09 18:53:47 +03001174 case PERF_RECORD_AUXTRACE_ERROR:
Adrian Hunter85ed4722015-04-09 18:53:50 +03001175 perf_session__auxtrace_error_inc(session, event);
Adrian Huntere9bf54d2015-04-09 18:53:47 +03001176 return tool->auxtrace_error(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001177 default:
1178 return -EINVAL;
1179 }
1180}
1181
Adrian Huntera2938292014-10-27 15:49:23 +02001182int perf_session__deliver_synth_event(struct perf_session *session,
1183 union perf_event *event,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001184 struct perf_sample *sample)
Adrian Huntera2938292014-10-27 15:49:23 +02001185{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001186 struct perf_evlist *evlist = session->evlist;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001187 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001188
1189 events_stats__inc(&evlist->stats, event->header.type);
Adrian Huntera2938292014-10-27 15:49:23 +02001190
1191 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001192 return perf_session__process_user_event(session, event, 0);
Adrian Huntera2938292014-10-27 15:49:23 +02001193
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001194 return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0);
Adrian Huntera2938292014-10-27 15:49:23 +02001195}
1196
Jiri Olsa268fb202012-05-30 14:23:43 +02001197static void event_swap(union perf_event *event, bool sample_id_all)
1198{
1199 perf_event__swap_op swap;
1200
1201 swap = perf_event__swap_ops[event->header.type];
1202 if (swap)
1203 swap(event, sample_id_all);
1204}
1205
Adrian Hunter5a52f332014-07-31 09:00:57 +03001206int perf_session__peek_event(struct perf_session *session, off_t file_offset,
1207 void *buf, size_t buf_sz,
1208 union perf_event **event_ptr,
1209 struct perf_sample *sample)
1210{
1211 union perf_event *event;
1212 size_t hdr_sz, rest;
1213 int fd;
1214
1215 if (session->one_mmap && !session->header.needs_swap) {
1216 event = file_offset - session->one_mmap_offset +
1217 session->one_mmap_addr;
1218 goto out_parse_sample;
1219 }
1220
1221 if (perf_data_file__is_pipe(session->file))
1222 return -1;
1223
1224 fd = perf_data_file__fd(session->file);
1225 hdr_sz = sizeof(struct perf_event_header);
1226
1227 if (buf_sz < hdr_sz)
1228 return -1;
1229
1230 if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 ||
Adrian Hunter554e92e2015-05-19 16:05:45 +03001231 readn(fd, buf, hdr_sz) != (ssize_t)hdr_sz)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001232 return -1;
1233
1234 event = (union perf_event *)buf;
1235
1236 if (session->header.needs_swap)
1237 perf_event_header__bswap(&event->header);
1238
Adrian Hunter554e92e2015-05-19 16:05:45 +03001239 if (event->header.size < hdr_sz || event->header.size > buf_sz)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001240 return -1;
1241
1242 rest = event->header.size - hdr_sz;
1243
Adrian Hunter554e92e2015-05-19 16:05:45 +03001244 if (readn(fd, buf, rest) != (ssize_t)rest)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001245 return -1;
1246
1247 if (session->header.needs_swap)
1248 event_swap(event, perf_evlist__sample_id_all(session->evlist));
1249
1250out_parse_sample:
1251
1252 if (sample && event->header.type < PERF_RECORD_USER_TYPE_START &&
1253 perf_evlist__parse_sample(session->evlist, event, sample))
1254 return -1;
1255
1256 *event_ptr = event;
1257
1258 return 0;
1259}
1260
Adrian Hunterd5652d82014-07-23 22:19:58 +03001261static s64 perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001262 union perf_event *event, u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001263{
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001264 struct perf_evlist *evlist = session->evlist;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001265 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001266 struct perf_sample sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001267 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001268
Jiri Olsa268fb202012-05-30 14:23:43 +02001269 if (session->header.needs_swap)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001270 event_swap(event, perf_evlist__sample_id_all(evlist));
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001271
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001272 if (event->header.type >= PERF_RECORD_HEADER_MAX)
1273 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001274
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001275 events_stats__inc(&evlist->stats, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001276
1277 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001278 return perf_session__process_user_event(session, event, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001279
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001280 /*
1281 * For all kernel events we get the sample data
1282 */
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001283 ret = perf_evlist__parse_sample(evlist, event, &sample);
Frederic Weisbecker5538bec2011-05-22 02:17:22 +02001284 if (ret)
1285 return ret;
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001286
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001287 if (tool->ordered_events) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001288 ret = perf_session__queue_event(session, event, &sample, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001289 if (ret != -ETIME)
1290 return ret;
1291 }
1292
Adrian Hunterc4468702015-04-09 18:53:48 +03001293 return perf_session__deliver_event(session, event, &sample, tool,
1294 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001295}
1296
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001297void perf_event_header__bswap(struct perf_event_header *hdr)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001298{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001299 hdr->type = bswap_32(hdr->type);
1300 hdr->misc = bswap_16(hdr->misc);
1301 hdr->size = bswap_16(hdr->size);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001302}
1303
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001304struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1305{
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001306 return machine__findnew_thread(&session->machines.host, -1, pid);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001307}
1308
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001309static struct thread *perf_session__register_idle_thread(struct perf_session *session)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001310{
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001311 struct thread *thread;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001312
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001313 thread = machine__findnew_thread(&session->machines.host, 0, 0);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001314 if (thread == NULL || thread__set_comm(thread, "swapper", 0)) {
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001315 pr_err("problem inserting idle task.\n");
1316 thread = NULL;
1317 }
1318
1319 return thread;
1320}
1321
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001322static void perf_session__warn_about_errors(const struct perf_session *session)
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001323{
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001324 const struct events_stats *stats = &session->evlist->stats;
1325 const struct ordered_events *oe = &session->ordered_events;
1326
1327 if (session->tool->lost == perf_event__process_lost &&
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001328 stats->nr_events[PERF_RECORD_LOST] != 0) {
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001329 ui__warning("Processed %d events and lost %d chunks!\n\n"
1330 "Check IO/CPU overload!\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001331 stats->nr_events[0],
1332 stats->nr_events[PERF_RECORD_LOST]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001333 }
1334
Kan Liangc4937a92015-05-10 15:13:15 -04001335 if (session->tool->lost_samples == perf_event__process_lost_samples) {
1336 double drop_rate;
1337
1338 drop_rate = (double)stats->total_lost_samples /
1339 (double) (stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples);
1340 if (drop_rate > 0.05) {
He Kuang6ba29c22015-06-11 12:44:24 +00001341 ui__warning("Processed %" PRIu64 " samples and lost %3.2f%% samples!\n\n",
Kan Liangc4937a92015-05-10 15:13:15 -04001342 stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples,
1343 drop_rate * 100.0);
1344 }
1345 }
1346
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001347 if (stats->nr_unknown_events != 0) {
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001348 ui__warning("Found %u unknown events!\n\n"
1349 "Is this an older tool processing a perf.data "
1350 "file generated by a more recent tool?\n\n"
1351 "If that is not the case, consider "
1352 "reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001353 stats->nr_unknown_events);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001354 }
1355
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001356 if (stats->nr_unknown_id != 0) {
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001357 ui__warning("%u samples with id not present in the header\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001358 stats->nr_unknown_id);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001359 }
1360
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001361 if (stats->nr_invalid_chains != 0) {
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001362 ui__warning("Found invalid callchains!\n\n"
1363 "%u out of %u events were discarded for this reason.\n\n"
1364 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001365 stats->nr_invalid_chains,
1366 stats->nr_events[PERF_RECORD_SAMPLE]);
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001367 }
Joerg Roedel0c095712012-02-10 18:05:04 +01001368
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001369 if (stats->nr_unprocessable_samples != 0) {
Joerg Roedel0c095712012-02-10 18:05:04 +01001370 ui__warning("%u unprocessable samples recorded.\n"
1371 "Do you have a KVM guest running and not using 'perf kvm'?\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001372 stats->nr_unprocessable_samples);
Joerg Roedel0c095712012-02-10 18:05:04 +01001373 }
Jiri Olsaf61ff6c2014-11-26 16:39:31 +01001374
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001375 if (oe->nr_unordered_events != 0)
1376 ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events);
Adrian Hunter85ed4722015-04-09 18:53:50 +03001377
1378 events_stats__auxtrace_error_warn(stats);
Kan Liang930e6fc2015-06-17 09:51:10 -04001379
1380 if (stats->nr_proc_map_timeout != 0) {
1381 ui__warning("%d map information files for pre-existing threads were\n"
1382 "not processed, if there are samples for addresses they\n"
1383 "will not be resolved, you may find out which are these\n"
1384 "threads by running with -v and redirecting the output\n"
Kan Liang9d9cad72015-06-17 09:51:11 -04001385 "to a file.\n"
1386 "The time limit to process proc map is too short?\n"
1387 "Increase it by --proc-map-timeout\n",
Kan Liang930e6fc2015-06-17 09:51:10 -04001388 stats->nr_proc_map_timeout);
1389 }
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001390}
1391
Adrian Huntera5499b32015-05-29 16:33:30 +03001392static int perf_session__flush_thread_stack(struct thread *thread,
1393 void *p __maybe_unused)
1394{
1395 return thread_stack__flush(thread);
1396}
1397
1398static int perf_session__flush_thread_stacks(struct perf_session *session)
1399{
1400 return machines__for_each_thread(&session->machines,
1401 perf_session__flush_thread_stack,
1402 NULL);
1403}
1404
Tom Zanussi8dc58102010-04-01 23:59:15 -05001405volatile int session_done;
1406
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001407static int __perf_session__process_pipe_events(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05001408{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001409 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001410 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001411 int fd = perf_data_file__fd(session->file);
Stephane Eranian444d2862012-05-15 13:28:12 +02001412 union perf_event *event;
1413 uint32_t size, cur_size = 0;
1414 void *buf = NULL;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001415 s64 skip = 0;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001416 u64 head;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001417 ssize_t err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001418 void *p;
1419
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001420 perf_tool__fill_defaults(tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001421
1422 head = 0;
Stephane Eranian444d2862012-05-15 13:28:12 +02001423 cur_size = sizeof(union perf_event);
1424
1425 buf = malloc(cur_size);
1426 if (!buf)
1427 return -errno;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001428more:
Stephane Eranian444d2862012-05-15 13:28:12 +02001429 event = buf;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001430 err = readn(fd, event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05001431 if (err <= 0) {
1432 if (err == 0)
1433 goto done;
1434
1435 pr_err("failed to read event header\n");
1436 goto out_err;
1437 }
1438
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001439 if (session->header.needs_swap)
Stephane Eranian444d2862012-05-15 13:28:12 +02001440 perf_event_header__bswap(&event->header);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001441
Stephane Eranian444d2862012-05-15 13:28:12 +02001442 size = event->header.size;
Adrian Hunter27389d72013-07-04 16:20:27 +03001443 if (size < sizeof(struct perf_event_header)) {
1444 pr_err("bad event header size\n");
1445 goto out_err;
1446 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001447
Stephane Eranian444d2862012-05-15 13:28:12 +02001448 if (size > cur_size) {
1449 void *new = realloc(buf, size);
1450 if (!new) {
1451 pr_err("failed to allocate memory to read event\n");
1452 goto out_err;
1453 }
1454 buf = new;
1455 cur_size = size;
1456 event = buf;
1457 }
1458 p = event;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001459 p += sizeof(struct perf_event_header);
1460
Tom Zanussi794e43b2010-05-05 00:27:40 -05001461 if (size - sizeof(struct perf_event_header)) {
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001462 err = readn(fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -05001463 if (err <= 0) {
1464 if (err == 0) {
1465 pr_err("unexpected end of event stream\n");
1466 goto done;
1467 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001468
Tom Zanussi794e43b2010-05-05 00:27:40 -05001469 pr_err("failed to read event data\n");
1470 goto out_err;
1471 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001472 }
1473
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001474 if ((skip = perf_session__process_event(session, event, head)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001475 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
Stephane Eranian444d2862012-05-15 13:28:12 +02001476 head, event->header.size, event->header.type);
Jiri Olsa9389a462012-04-16 20:42:51 +02001477 err = -EINVAL;
1478 goto out_err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001479 }
1480
1481 head += size;
1482
Tom Zanussi8dc58102010-04-01 23:59:15 -05001483 if (skip > 0)
1484 head += skip;
1485
1486 if (!session_done())
1487 goto more;
1488done:
Adrian Hunter8c16b642013-10-18 15:29:02 +03001489 /* do the final flush for ordered samples */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001490 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
Adrian Hunterc4468702015-04-09 18:53:48 +03001491 if (err)
1492 goto out_err;
1493 err = auxtrace__flush_events(session, tool);
Adrian Huntera5499b32015-05-29 16:33:30 +03001494 if (err)
1495 goto out_err;
1496 err = perf_session__flush_thread_stacks(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001497out_err:
Stephane Eranian444d2862012-05-15 13:28:12 +02001498 free(buf);
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001499 perf_session__warn_about_errors(session);
Jiri Olsaadc56ed2014-06-10 22:50:03 +02001500 ordered_events__free(&session->ordered_events);
Adrian Hunterc4468702015-04-09 18:53:48 +03001501 auxtrace__free_events(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001502 return err;
1503}
1504
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001505static union perf_event *
1506fetch_mmaped_event(struct perf_session *session,
1507 u64 head, size_t mmap_size, char *buf)
1508{
1509 union perf_event *event;
1510
1511 /*
1512 * Ensure we have enough space remaining to read
1513 * the size of the event in the headers.
1514 */
1515 if (head + sizeof(event->header) > mmap_size)
1516 return NULL;
1517
1518 event = (union perf_event *)(buf + head);
1519
1520 if (session->header.needs_swap)
1521 perf_event_header__bswap(&event->header);
1522
Adrian Hunter27389d72013-07-04 16:20:27 +03001523 if (head + event->header.size > mmap_size) {
1524 /* We're not fetching the event so swap back again */
1525 if (session->header.needs_swap)
1526 perf_event_header__bswap(&event->header);
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001527 return NULL;
Adrian Hunter27389d72013-07-04 16:20:27 +03001528 }
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001529
1530 return event;
1531}
1532
David Miller35d48dd2012-11-10 14:12:19 -05001533/*
1534 * On 64bit we can mmap the data file in one go. No need for tiny mmap
1535 * slices. On 32bit we use 32MB.
1536 */
1537#if BITS_PER_LONG == 64
1538#define MMAP_SIZE ULLONG_MAX
1539#define NUM_MMAPS 1
1540#else
1541#define MMAP_SIZE (32 * 1024 * 1024ULL)
1542#define NUM_MMAPS 128
1543#endif
1544
Namhyung Kim4ac30cf2015-01-29 17:06:43 +09001545static int __perf_session__process_events(struct perf_session *session,
1546 u64 data_offset, u64 data_size,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001547 u64 file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001548{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001549 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001550 struct perf_tool *tool = session->tool;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001551 int fd = perf_data_file__fd(session->file);
Adrian Hunterd5652d82014-07-23 22:19:58 +03001552 u64 head, page_offset, file_offset, file_pos, size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001553 int err, mmap_prot, mmap_flags, map_idx = 0;
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -03001554 size_t mmap_size;
David Miller35d48dd2012-11-10 14:12:19 -05001555 char *buf, *mmaps[NUM_MMAPS];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001556 union perf_event *event;
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001557 struct ui_progress prog;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001558 s64 skip;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001559
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001560 perf_tool__fill_defaults(tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001561
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001562 page_offset = page_size * (data_offset / page_size);
1563 file_offset = page_offset;
1564 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001565
Namhyung Kimb314e5c2013-09-30 17:19:48 +09001566 if (data_size && (data_offset + data_size < file_size))
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001567 file_size = data_offset + data_size;
1568
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001569 ui_progress__init(&prog, file_size, "Processing events...");
Thomas Gleixner55b446292010-11-30 17:49:46 +00001570
David Miller35d48dd2012-11-10 14:12:19 -05001571 mmap_size = MMAP_SIZE;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001572 if (mmap_size > file_size) {
Thomas Gleixner55b446292010-11-30 17:49:46 +00001573 mmap_size = file_size;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001574 session->one_mmap = true;
1575 }
Thomas Gleixner55b446292010-11-30 17:49:46 +00001576
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001577 memset(mmaps, 0, sizeof(mmaps));
1578
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001579 mmap_prot = PROT_READ;
1580 mmap_flags = MAP_SHARED;
1581
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001582 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001583 mmap_prot |= PROT_WRITE;
1584 mmap_flags = MAP_PRIVATE;
1585 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001586remap:
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001587 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd,
Thomas Gleixner55b446292010-11-30 17:49:46 +00001588 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001589 if (buf == MAP_FAILED) {
1590 pr_err("failed to mmap file\n");
1591 err = -errno;
1592 goto out_err;
1593 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001594 mmaps[map_idx] = buf;
1595 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001596 file_pos = file_offset + head;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001597 if (session->one_mmap) {
1598 session->one_mmap_addr = buf;
1599 session->one_mmap_offset = file_offset;
1600 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001601
1602more:
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001603 event = fetch_mmaped_event(session, head, mmap_size, buf);
1604 if (!event) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001605 if (mmaps[map_idx]) {
1606 munmap(mmaps[map_idx], mmap_size);
1607 mmaps[map_idx] = NULL;
1608 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001609
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001610 page_offset = page_size * (head / page_size);
1611 file_offset += page_offset;
1612 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001613 goto remap;
1614 }
1615
1616 size = event->header.size;
1617
Adrian Hunter27389d72013-07-04 16:20:27 +03001618 if (size < sizeof(struct perf_event_header) ||
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001619 (skip = perf_session__process_event(session, event, file_pos)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001620 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1621 file_offset + head, event->header.size,
1622 event->header.type);
1623 err = -EINVAL;
1624 goto out_err;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001625 }
1626
Adrian Hunter6f917c72014-07-23 22:19:57 +03001627 if (skip)
1628 size += skip;
1629
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001630 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001631 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001632
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001633 ui_progress__update(&prog, size);
Thomas Gleixner55b446292010-11-30 17:49:46 +00001634
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001635 if (session_done())
Adrian Hunter8c16b642013-10-18 15:29:02 +03001636 goto out;
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001637
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001638 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001639 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001640
Adrian Hunter8c16b642013-10-18 15:29:02 +03001641out:
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001642 /* do the final flush for ordered samples */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001643 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
Adrian Hunterc4468702015-04-09 18:53:48 +03001644 if (err)
1645 goto out_err;
1646 err = auxtrace__flush_events(session, tool);
Adrian Huntera5499b32015-05-29 16:33:30 +03001647 if (err)
1648 goto out_err;
1649 err = perf_session__flush_thread_stacks(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001650out_err:
Namhyung Kima5580f32012-11-13 22:30:34 +09001651 ui_progress__finish();
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001652 perf_session__warn_about_errors(session);
Jiri Olsaadc56ed2014-06-10 22:50:03 +02001653 ordered_events__free(&session->ordered_events);
Adrian Hunterc4468702015-04-09 18:53:48 +03001654 auxtrace__free_events(session);
Adrian Hunter919d86d2014-07-14 13:02:51 +03001655 session->one_mmap = false;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001656 return err;
1657}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001658
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001659int perf_session__process_events(struct perf_session *session)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001660{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001661 u64 size = perf_data_file__size(session->file);
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001662 int err;
1663
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001664 if (perf_session__register_idle_thread(session) == NULL)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001665 return -ENOMEM;
1666
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001667 if (!perf_data_file__is_pipe(session->file))
1668 err = __perf_session__process_events(session,
1669 session->header.data_offset,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001670 session->header.data_size, size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001671 else
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001672 err = __perf_session__process_pipe_events(session);
Dave Martin88ca8952010-07-27 11:46:12 -03001673
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001674 return err;
1675}
1676
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001677bool perf_session__has_traces(struct perf_session *session, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001678{
David Ahern93ea01c292013-08-07 22:50:58 -04001679 struct perf_evsel *evsel;
1680
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001681 evlist__for_each(session->evlist, evsel) {
David Ahern93ea01c292013-08-07 22:50:58 -04001682 if (evsel->attr.type == PERF_TYPE_TRACEPOINT)
1683 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001684 }
1685
David Ahern93ea01c292013-08-07 22:50:58 -04001686 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1687 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001688}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001689
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001690int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1691 const char *symbol_name, u64 addr)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001692{
1693 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001694 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001695 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001696
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001697 ref = zalloc(sizeof(struct ref_reloc_sym));
1698 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001699 return -ENOMEM;
1700
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001701 ref->name = strdup(symbol_name);
1702 if (ref->name == NULL) {
1703 free(ref);
1704 return -ENOMEM;
1705 }
1706
1707 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001708 if (bracket)
1709 *bracket = '\0';
1710
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001711 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001712
1713 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001714 struct kmap *kmap = map__kmap(maps[i]);
Wang Nanba927322015-04-07 08:22:45 +00001715
1716 if (!kmap)
1717 continue;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001718 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001719 }
1720
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001721 return 0;
1722}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001723
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001724size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001725{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001726 return machines__fprintf_dsos(&session->machines, fp);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001727}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001728
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001729size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001730 bool (skip)(struct dso *dso, int parm), int parm)
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001731{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001732 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001733}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001734
1735size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1736{
Adrian Hunterc4468702015-04-09 18:53:48 +03001737 size_t ret;
1738 const char *msg = "";
1739
1740 if (perf_header__has_feat(&session->header, HEADER_AUXTRACE))
1741 msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)";
1742
Adrian Hunterfe692ac2015-06-23 10:52:49 +03001743 ret = fprintf(fp, "\nAggregated stats:%s\n", msg);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001744
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001745 ret += events_stats__fprintf(&session->evlist->stats, fp);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001746 return ret;
1747}
David Ahernc0230b22011-03-09 22:23:27 -07001748
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001749size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1750{
1751 /*
1752 * FIXME: Here we have to actually print all the machines in this
1753 * session, not just the host...
1754 */
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001755 return machine__fprintf(&session->machines.host, fp);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001756}
1757
David Ahern9cbdb702011-04-06 21:54:20 -06001758struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1759 unsigned int type)
1760{
1761 struct perf_evsel *pos;
1762
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001763 evlist__for_each(session->evlist, pos) {
David Ahern9cbdb702011-04-06 21:54:20 -06001764 if (pos->attr.type == type)
1765 return pos;
1766 }
1767 return NULL;
1768}
1769
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001770void perf_evsel__print_ip(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -03001771 struct addr_location *al,
David Ahern307cbb92013-08-07 22:50:53 -04001772 unsigned int print_opts, unsigned int stack_depth)
David Ahernc0230b22011-03-09 22:23:27 -07001773{
David Ahernc0230b22011-03-09 22:23:27 -07001774 struct callchain_cursor_node *node;
David Aherna6ffaf92013-08-07 22:50:51 -04001775 int print_ip = print_opts & PRINT_IP_OPT_IP;
1776 int print_sym = print_opts & PRINT_IP_OPT_SYM;
1777 int print_dso = print_opts & PRINT_IP_OPT_DSO;
1778 int print_symoffset = print_opts & PRINT_IP_OPT_SYMOFFSET;
David Ahernb0b35f02013-08-07 22:50:52 -04001779 int print_oneline = print_opts & PRINT_IP_OPT_ONELINE;
Adrian Huntercc8fae12013-12-06 09:42:57 +02001780 int print_srcline = print_opts & PRINT_IP_OPT_SRCLINE;
David Ahernb0b35f02013-08-07 22:50:52 -04001781 char s = print_oneline ? ' ' : '\t';
David Ahernc0230b22011-03-09 22:23:27 -07001782
David Ahernc0230b22011-03-09 22:23:27 -07001783 if (symbol_conf.use_callchain && sample->callchain) {
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001784 struct addr_location node_al;
David Ahernc0230b22011-03-09 22:23:27 -07001785
Arnaldo Carvalho de Melocc8b7c22014-10-23 15:26:17 -03001786 if (thread__resolve_callchain(al->thread, evsel,
1787 sample, NULL, NULL,
1788 PERF_MAX_STACK_DEPTH) != 0) {
David Ahernc0230b22011-03-09 22:23:27 -07001789 if (verbose)
1790 error("Failed to resolve callchain. Skipping\n");
1791 return;
1792 }
Namhyung Kim47260642012-05-31 14:43:26 +09001793 callchain_cursor_commit(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001794
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001795 if (print_symoffset)
1796 node_al = *al;
1797
David Ahern307cbb92013-08-07 22:50:53 -04001798 while (stack_depth) {
Adrian Huntera4eb24a2013-12-06 09:42:56 +02001799 u64 addr = 0;
1800
Namhyung Kim47260642012-05-31 14:43:26 +09001801 node = callchain_cursor_current(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001802 if (!node)
1803 break;
1804
David Ahernd2ff1b12013-11-18 13:32:44 -07001805 if (node->sym && node->sym->ignore)
1806 goto next;
1807
David Aherna6ffaf92013-08-07 22:50:51 -04001808 if (print_ip)
David Ahernb0b35f02013-08-07 22:50:52 -04001809 printf("%c%16" PRIx64, s, node->ip);
David Aherna6ffaf92013-08-07 22:50:51 -04001810
Adrian Huntera4eb24a2013-12-06 09:42:56 +02001811 if (node->map)
1812 addr = node->map->map_ip(node->map, node->ip);
1813
David Ahern787bef12011-05-27 14:28:43 -06001814 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001815 printf(" ");
David Ahern251f4262013-07-28 09:14:34 -06001816 if (print_symoffset) {
Adrian Huntera4eb24a2013-12-06 09:42:56 +02001817 node_al.addr = addr;
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001818 node_al.map = node->map;
1819 symbol__fprintf_symname_offs(node->sym, &node_al, stdout);
David Ahern251f4262013-07-28 09:14:34 -06001820 } else
1821 symbol__fprintf_symname(node->sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001822 }
David Ahern251f4262013-07-28 09:14:34 -06001823
David Ahern610723f2011-05-27 14:28:44 -06001824 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001825 printf(" (");
David Ahern52deff72012-05-29 22:58:26 -06001826 map__fprintf_dsoname(node->map, stdout);
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001827 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001828 }
David Ahernb0b35f02013-08-07 22:50:52 -04001829
Adrian Huntercc8fae12013-12-06 09:42:57 +02001830 if (print_srcline)
1831 map__fprintf_srcline(node->map, addr, "\n ",
1832 stdout);
1833
David Ahernb0b35f02013-08-07 22:50:52 -04001834 if (!print_oneline)
1835 printf("\n");
David Ahernc0230b22011-03-09 22:23:27 -07001836
David Ahern307cbb92013-08-07 22:50:53 -04001837 stack_depth--;
David Ahernd2ff1b12013-11-18 13:32:44 -07001838next:
1839 callchain_cursor_advance(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001840 }
1841
1842 } else {
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001843 if (al->sym && al->sym->ignore)
David Ahernd2ff1b12013-11-18 13:32:44 -07001844 return;
1845
David Aherna6ffaf92013-08-07 22:50:51 -04001846 if (print_ip)
1847 printf("%16" PRIx64, sample->ip);
1848
David Ahern787bef12011-05-27 14:28:43 -06001849 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001850 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001851 if (print_symoffset)
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001852 symbol__fprintf_symname_offs(al->sym, al,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001853 stdout);
1854 else
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001855 symbol__fprintf_symname(al->sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001856 }
1857
1858 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001859 printf(" (");
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001860 map__fprintf_dsoname(al->map, stdout);
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001861 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001862 }
Adrian Huntercc8fae12013-12-06 09:42:57 +02001863
1864 if (print_srcline)
1865 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
David Ahernc0230b22011-03-09 22:23:27 -07001866 }
1867}
Anton Blanchard5d67be92011-07-04 21:57:50 +10001868
1869int perf_session__cpu_bitmap(struct perf_session *session,
1870 const char *cpu_list, unsigned long *cpu_bitmap)
1871{
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04001872 int i, err = -1;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001873 struct cpu_map *map;
1874
1875 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1876 struct perf_evsel *evsel;
1877
1878 evsel = perf_session__find_first_evtype(session, i);
1879 if (!evsel)
1880 continue;
1881
1882 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1883 pr_err("File does not contain CPU events. "
1884 "Remove -c option to proceed.\n");
1885 return -1;
1886 }
1887 }
1888
1889 map = cpu_map__new(cpu_list);
David Ahern47fbe532011-11-13 10:45:27 -07001890 if (map == NULL) {
1891 pr_err("Invalid cpu_list\n");
1892 return -1;
1893 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001894
1895 for (i = 0; i < map->nr; i++) {
1896 int cpu = map->map[i];
1897
1898 if (cpu >= MAX_NR_CPUS) {
1899 pr_err("Requested CPU %d too large. "
1900 "Consider raising MAX_NR_CPUS\n", cpu);
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04001901 goto out_delete_map;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001902 }
1903
1904 set_bit(cpu, cpu_bitmap);
1905 }
1906
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04001907 err = 0;
1908
1909out_delete_map:
Jiri Olsaf30a79b2015-06-23 00:36:04 +02001910 cpu_map__put(map);
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04001911 return err;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001912}
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001913
1914void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1915 bool full)
1916{
1917 struct stat st;
Masanari Iidac5765ec2014-05-15 02:13:38 +09001918 int fd, ret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001919
1920 if (session == NULL || fp == NULL)
1921 return;
1922
Masanari Iidac5765ec2014-05-15 02:13:38 +09001923 fd = perf_data_file__fd(session->file);
1924
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001925 ret = fstat(fd, &st);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001926 if (ret == -1)
1927 return;
1928
1929 fprintf(fp, "# ========\n");
1930 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1931 perf_header__fprintf_info(session, fp, full);
1932 fprintf(fp, "# ========\n#\n");
1933}
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001934
1935
1936int __perf_session__set_tracepoints_handlers(struct perf_session *session,
1937 const struct perf_evsel_str_handler *assocs,
1938 size_t nr_assocs)
1939{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001940 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001941 size_t i;
1942 int err;
1943
1944 for (i = 0; i < nr_assocs; i++) {
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001945 /*
1946 * Adding a handler for an event not in the session,
1947 * just ignore it.
1948 */
1949 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name);
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001950 if (evsel == NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001951 continue;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001952
1953 err = -EEXIST;
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001954 if (evsel->handler != NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001955 goto out;
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001956 evsel->handler = assocs[i].handler;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001957 }
1958
1959 err = 0;
1960out:
1961 return err;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001962}
Adrian Hunter3c659ee2014-10-27 15:49:22 +02001963
1964int perf_event__process_id_index(struct perf_tool *tool __maybe_unused,
1965 union perf_event *event,
1966 struct perf_session *session)
1967{
1968 struct perf_evlist *evlist = session->evlist;
1969 struct id_index_event *ie = &event->id_index;
1970 size_t i, nr, max_nr;
1971
1972 max_nr = (ie->header.size - sizeof(struct id_index_event)) /
1973 sizeof(struct id_index_entry);
1974 nr = ie->nr;
1975 if (nr > max_nr)
1976 return -EINVAL;
1977
1978 if (dump_trace)
1979 fprintf(stdout, " nr: %zu\n", nr);
1980
1981 for (i = 0; i < nr; i++) {
1982 struct id_index_entry *e = &ie->entries[i];
1983 struct perf_sample_id *sid;
1984
1985 if (dump_trace) {
1986 fprintf(stdout, " ... id: %"PRIu64, e->id);
1987 fprintf(stdout, " idx: %"PRIu64, e->idx);
1988 fprintf(stdout, " cpu: %"PRId64, e->cpu);
1989 fprintf(stdout, " tid: %"PRId64"\n", e->tid);
1990 }
1991
1992 sid = perf_evlist__id2sid(evlist, e->id);
1993 if (!sid)
1994 return -ENOENT;
1995 sid->idx = e->idx;
1996 sid->cpu = e->cpu;
1997 sid->tid = e->tid;
1998 }
1999 return 0;
2000}
2001
2002int perf_event__synthesize_id_index(struct perf_tool *tool,
2003 perf_event__handler_t process,
2004 struct perf_evlist *evlist,
2005 struct machine *machine)
2006{
2007 union perf_event *ev;
2008 struct perf_evsel *evsel;
2009 size_t nr = 0, i = 0, sz, max_nr, n;
2010 int err;
2011
2012 pr_debug2("Synthesizing id index\n");
2013
2014 max_nr = (UINT16_MAX - sizeof(struct id_index_event)) /
2015 sizeof(struct id_index_entry);
2016
Arnaldo Carvalho de Melocba9b842014-10-29 11:31:54 -02002017 evlist__for_each(evlist, evsel)
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002018 nr += evsel->ids;
2019
2020 n = nr > max_nr ? max_nr : nr;
2021 sz = sizeof(struct id_index_event) + n * sizeof(struct id_index_entry);
2022 ev = zalloc(sz);
2023 if (!ev)
2024 return -ENOMEM;
2025
2026 ev->id_index.header.type = PERF_RECORD_ID_INDEX;
2027 ev->id_index.header.size = sz;
2028 ev->id_index.nr = n;
2029
Arnaldo Carvalho de Melocba9b842014-10-29 11:31:54 -02002030 evlist__for_each(evlist, evsel) {
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002031 u32 j;
2032
2033 for (j = 0; j < evsel->ids; j++) {
2034 struct id_index_entry *e;
2035 struct perf_sample_id *sid;
2036
2037 if (i >= n) {
2038 err = process(tool, ev, NULL, machine);
2039 if (err)
2040 goto out_err;
2041 nr -= n;
2042 i = 0;
2043 }
2044
2045 e = &ev->id_index.entries[i++];
2046
2047 e->id = evsel->id[j];
2048
2049 sid = perf_evlist__id2sid(evlist, e->id);
2050 if (!sid) {
2051 free(ev);
2052 return -ENOENT;
2053 }
2054
2055 e->idx = sid->idx;
2056 e->cpu = sid->cpu;
2057 e->tid = sid->tid;
2058 }
2059 }
2060
2061 sz = sizeof(struct id_index_event) + nr * sizeof(struct id_index_entry);
2062 ev->id_index.header.size = sz;
2063 ev->id_index.nr = nr;
2064
2065 err = process(tool, ev, NULL, machine);
2066out_err:
2067 free(ev);
2068
2069 return err;
2070}