blob: 18722e774a69c5c8fdbbc1c55454b0b85a0e1b16 [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 Melo4c7de492015-07-29 12:18:24 -0300173static void perf_session_env__exit(struct perf_session_env *env)
Namhyung Kim03cd2092012-11-21 13:43:19 +0900174{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300175 zfree(&env->hostname);
176 zfree(&env->os_release);
177 zfree(&env->version);
178 zfree(&env->arch);
179 zfree(&env->cpu_desc);
180 zfree(&env->cpuid);
Namhyung Kim03cd2092012-11-21 13:43:19 +0900181
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300182 zfree(&env->cmdline);
Jiri Olsa768dd3f2015-07-21 14:31:31 +0200183 zfree(&env->cmdline_argv);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300184 zfree(&env->sibling_cores);
185 zfree(&env->sibling_threads);
186 zfree(&env->numa_nodes);
187 zfree(&env->pmu_mappings);
Namhyung Kim03cd2092012-11-21 13:43:19 +0900188}
189
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300190void perf_session__delete(struct perf_session *session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200191{
Adrian Hunterc4468702015-04-09 18:53:48 +0300192 auxtrace__free(session);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300193 auxtrace_index__free(&session->auxtrace_index);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300194 perf_session__destroy_kernel_maps(session);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300195 perf_session__delete_threads(session);
Arnaldo Carvalho de Melo4c7de492015-07-29 12:18:24 -0300196 perf_session_env__exit(&session->header.env);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300197 machines__exit(&session->machines);
198 if (session->file)
199 perf_data_file__close(session->file);
200 free(session);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200201}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200202
Adrian Hunter47c3d102013-07-04 16:20:21 +0300203static int process_event_synth_tracing_data_stub(struct perf_tool *tool
204 __maybe_unused,
205 union perf_event *event
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300206 __maybe_unused,
207 struct perf_session *session
208 __maybe_unused)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200209{
210 dump_printf(": unhandled!\n");
211 return 0;
212}
213
Adrian Hunter47c3d102013-07-04 16:20:21 +0300214static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused,
215 union perf_event *event __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300216 struct perf_evlist **pevlist
217 __maybe_unused)
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200218{
219 dump_printf(": unhandled!\n");
220 return 0;
221}
222
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300223static int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
224 union perf_event *event __maybe_unused,
225 struct perf_sample *sample __maybe_unused,
226 struct perf_evsel *evsel __maybe_unused,
227 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300228{
229 dump_printf(": unhandled!\n");
230 return 0;
231}
232
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300233static int process_event_stub(struct perf_tool *tool __maybe_unused,
234 union perf_event *event __maybe_unused,
235 struct perf_sample *sample __maybe_unused,
236 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200237{
238 dump_printf(": unhandled!\n");
239 return 0;
240}
241
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300242static int process_build_id_stub(struct perf_tool *tool __maybe_unused,
243 union perf_event *event __maybe_unused,
244 struct perf_session *session __maybe_unused)
245{
246 dump_printf(": unhandled!\n");
247 return 0;
248}
249
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300250static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
251 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300252 struct ordered_events *oe __maybe_unused)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200253{
254 dump_printf(": unhandled!\n");
255 return 0;
256}
257
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200258static int process_finished_round(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200259 union perf_event *event,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300260 struct ordered_events *oe);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200261
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200262static int process_id_index_stub(struct perf_tool *tool __maybe_unused,
263 union perf_event *event __maybe_unused,
264 struct perf_session *perf_session
265 __maybe_unused)
266{
267 dump_printf(": unhandled!\n");
268 return 0;
269}
270
Adrian Huntera16ac022015-04-09 18:53:43 +0300271static int process_event_auxtrace_info_stub(struct perf_tool *tool __maybe_unused,
272 union perf_event *event __maybe_unused,
273 struct perf_session *session __maybe_unused)
274{
275 dump_printf(": unhandled!\n");
276 return 0;
277}
278
279static int skipn(int fd, off_t n)
280{
281 char buf[4096];
282 ssize_t ret;
283
284 while (n > 0) {
285 ret = read(fd, buf, min(n, (off_t)sizeof(buf)));
286 if (ret <= 0)
287 return ret;
288 n -= ret;
289 }
290
291 return 0;
292}
293
294static s64 process_event_auxtrace_stub(struct perf_tool *tool __maybe_unused,
295 union perf_event *event,
296 struct perf_session *session
297 __maybe_unused)
298{
299 dump_printf(": unhandled!\n");
300 if (perf_data_file__is_pipe(session->file))
301 skipn(perf_data_file__fd(session->file), event->auxtrace.size);
302 return event->auxtrace.size;
303}
304
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300305static
306int process_event_auxtrace_error_stub(struct perf_tool *tool __maybe_unused,
307 union perf_event *event __maybe_unused,
308 struct perf_session *session __maybe_unused)
309{
310 dump_printf(": unhandled!\n");
311 return 0;
312}
313
David Ahern9c501402013-08-02 14:05:41 -0600314void perf_tool__fill_defaults(struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200315{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200316 if (tool->sample == NULL)
317 tool->sample = process_event_sample_stub;
318 if (tool->mmap == NULL)
319 tool->mmap = process_event_stub;
David Ahern6adb0b02013-09-22 19:44:59 -0600320 if (tool->mmap2 == NULL)
321 tool->mmap2 = process_event_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200322 if (tool->comm == NULL)
323 tool->comm = process_event_stub;
324 if (tool->fork == NULL)
325 tool->fork = process_event_stub;
326 if (tool->exit == NULL)
327 tool->exit = process_event_stub;
328 if (tool->lost == NULL)
329 tool->lost = perf_event__process_lost;
Kan Liangc4937a92015-05-10 15:13:15 -0400330 if (tool->lost_samples == NULL)
331 tool->lost_samples = perf_event__process_lost_samples;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300332 if (tool->aux == NULL)
333 tool->aux = perf_event__process_aux;
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300334 if (tool->itrace_start == NULL)
335 tool->itrace_start = perf_event__process_itrace_start;
Adrian Hunter02860392015-07-21 12:44:03 +0300336 if (tool->context_switch == NULL)
337 tool->context_switch = perf_event__process_switch;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200338 if (tool->read == NULL)
339 tool->read = process_event_sample_stub;
340 if (tool->throttle == NULL)
341 tool->throttle = process_event_stub;
342 if (tool->unthrottle == NULL)
343 tool->unthrottle = process_event_stub;
344 if (tool->attr == NULL)
345 tool->attr = process_event_synth_attr_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200346 if (tool->tracing_data == NULL)
347 tool->tracing_data = process_event_synth_tracing_data_stub;
348 if (tool->build_id == NULL)
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300349 tool->build_id = process_build_id_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200350 if (tool->finished_round == NULL) {
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200351 if (tool->ordered_events)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200352 tool->finished_round = process_finished_round;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200353 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200354 tool->finished_round = process_finished_round_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200355 }
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200356 if (tool->id_index == NULL)
357 tool->id_index = process_id_index_stub;
Adrian Huntera16ac022015-04-09 18:53:43 +0300358 if (tool->auxtrace_info == NULL)
359 tool->auxtrace_info = process_event_auxtrace_info_stub;
360 if (tool->auxtrace == NULL)
361 tool->auxtrace = process_event_auxtrace_stub;
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300362 if (tool->auxtrace_error == NULL)
363 tool->auxtrace_error = process_event_auxtrace_error_stub;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200364}
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300365
Jiri Olsa268fb202012-05-30 14:23:43 +0200366static void swap_sample_id_all(union perf_event *event, void *data)
367{
368 void *end = (void *) event + event->header.size;
369 int size = end - data;
370
371 BUG_ON(size % sizeof(u64));
372 mem_bswap_64(data, size);
373}
374
375static void perf_event__all64_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300376 bool sample_id_all __maybe_unused)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200377{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200378 struct perf_event_header *hdr = &event->header;
379 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200380}
381
Jiri Olsa268fb202012-05-30 14:23:43 +0200382static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200383{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200384 event->comm.pid = bswap_32(event->comm.pid);
385 event->comm.tid = bswap_32(event->comm.tid);
Jiri Olsa268fb202012-05-30 14:23:43 +0200386
387 if (sample_id_all) {
388 void *data = &event->comm.comm;
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
Jiri Olsa268fb202012-05-30 14:23:43 +0200395static void perf_event__mmap_swap(union perf_event *event,
396 bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200397{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200398 event->mmap.pid = bswap_32(event->mmap.pid);
399 event->mmap.tid = bswap_32(event->mmap.tid);
400 event->mmap.start = bswap_64(event->mmap.start);
401 event->mmap.len = bswap_64(event->mmap.len);
402 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
Jiri Olsa268fb202012-05-30 14:23:43 +0200403
404 if (sample_id_all) {
405 void *data = &event->mmap.filename;
406
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300407 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200408 swap_sample_id_all(event, data);
409 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200410}
411
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200412static void perf_event__mmap2_swap(union perf_event *event,
413 bool sample_id_all)
414{
415 event->mmap2.pid = bswap_32(event->mmap2.pid);
416 event->mmap2.tid = bswap_32(event->mmap2.tid);
417 event->mmap2.start = bswap_64(event->mmap2.start);
418 event->mmap2.len = bswap_64(event->mmap2.len);
419 event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
420 event->mmap2.maj = bswap_32(event->mmap2.maj);
421 event->mmap2.min = bswap_32(event->mmap2.min);
422 event->mmap2.ino = bswap_64(event->mmap2.ino);
423
424 if (sample_id_all) {
425 void *data = &event->mmap2.filename;
426
427 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
428 swap_sample_id_all(event, data);
429 }
430}
Jiri Olsa268fb202012-05-30 14:23:43 +0200431static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200432{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200433 event->fork.pid = bswap_32(event->fork.pid);
434 event->fork.tid = bswap_32(event->fork.tid);
435 event->fork.ppid = bswap_32(event->fork.ppid);
436 event->fork.ptid = bswap_32(event->fork.ptid);
437 event->fork.time = bswap_64(event->fork.time);
Jiri Olsa268fb202012-05-30 14:23:43 +0200438
439 if (sample_id_all)
440 swap_sample_id_all(event, &event->fork + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200441}
442
Jiri Olsa268fb202012-05-30 14:23:43 +0200443static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200444{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200445 event->read.pid = bswap_32(event->read.pid);
446 event->read.tid = bswap_32(event->read.tid);
447 event->read.value = bswap_64(event->read.value);
448 event->read.time_enabled = bswap_64(event->read.time_enabled);
449 event->read.time_running = bswap_64(event->read.time_running);
450 event->read.id = bswap_64(event->read.id);
Jiri Olsa268fb202012-05-30 14:23:43 +0200451
452 if (sample_id_all)
453 swap_sample_id_all(event, &event->read + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200454}
455
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300456static void perf_event__aux_swap(union perf_event *event, bool sample_id_all)
457{
458 event->aux.aux_offset = bswap_64(event->aux.aux_offset);
459 event->aux.aux_size = bswap_64(event->aux.aux_size);
460 event->aux.flags = bswap_64(event->aux.flags);
461
462 if (sample_id_all)
463 swap_sample_id_all(event, &event->aux + 1);
464}
465
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300466static void perf_event__itrace_start_swap(union perf_event *event,
467 bool sample_id_all)
468{
469 event->itrace_start.pid = bswap_32(event->itrace_start.pid);
470 event->itrace_start.tid = bswap_32(event->itrace_start.tid);
471
472 if (sample_id_all)
473 swap_sample_id_all(event, &event->itrace_start + 1);
474}
475
Adrian Hunter02860392015-07-21 12:44:03 +0300476static void perf_event__switch_swap(union perf_event *event, bool sample_id_all)
477{
478 if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
479 event->context_switch.next_prev_pid =
480 bswap_32(event->context_switch.next_prev_pid);
481 event->context_switch.next_prev_tid =
482 bswap_32(event->context_switch.next_prev_tid);
483 }
484
485 if (sample_id_all)
486 swap_sample_id_all(event, &event->context_switch + 1);
487}
488
Jiri Olsadd96c462013-09-01 12:36:15 +0200489static void perf_event__throttle_swap(union perf_event *event,
490 bool sample_id_all)
491{
492 event->throttle.time = bswap_64(event->throttle.time);
493 event->throttle.id = bswap_64(event->throttle.id);
494 event->throttle.stream_id = bswap_64(event->throttle.stream_id);
495
496 if (sample_id_all)
497 swap_sample_id_all(event, &event->throttle + 1);
498}
499
Jiri Olsae108c662012-05-16 08:59:03 +0200500static u8 revbyte(u8 b)
501{
502 int rev = (b >> 4) | ((b & 0xf) << 4);
503 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
504 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
505 return (u8) rev;
506}
507
508/*
509 * XXX this is hack in attempt to carry flags bitfield
510 * throught endian village. ABI says:
511 *
512 * Bit-fields are allocated from right to left (least to most significant)
513 * on little-endian implementations and from left to right (most to least
514 * significant) on big-endian implementations.
515 *
516 * The above seems to be byte specific, so we need to reverse each
517 * byte of the bitfield. 'Internet' also says this might be implementation
518 * specific and we probably need proper fix and carry perf_event_attr
519 * bitfield flags in separate data file FEAT_ section. Thought this seems
520 * to work for now.
521 */
522static void swap_bitfield(u8 *p, unsigned len)
523{
524 unsigned i;
525
526 for (i = 0; i < len; i++) {
527 *p = revbyte(*p);
528 p++;
529 }
530}
531
David Aherneda39132011-07-15 12:34:09 -0600532/* exported for swapping attributes in file header */
533void perf_event__attr_swap(struct perf_event_attr *attr)
534{
535 attr->type = bswap_32(attr->type);
536 attr->size = bswap_32(attr->size);
Jiri Olsae108c662012-05-16 08:59:03 +0200537
Wang Nanb30b6172015-06-17 09:56:39 +0000538#define bswap_safe(f, n) \
539 (attr->size > (offsetof(struct perf_event_attr, f) + \
540 sizeof(attr->f) * (n)))
541#define bswap_field(f, sz) \
542do { \
543 if (bswap_safe(f, 0)) \
544 attr->f = bswap_##sz(attr->f); \
545} while(0)
546#define bswap_field_32(f) bswap_field(f, 32)
547#define bswap_field_64(f) bswap_field(f, 64)
548
549 bswap_field_64(config);
550 bswap_field_64(sample_period);
551 bswap_field_64(sample_type);
552 bswap_field_64(read_format);
553 bswap_field_32(wakeup_events);
554 bswap_field_32(bp_type);
555 bswap_field_64(bp_addr);
556 bswap_field_64(bp_len);
557 bswap_field_64(branch_sample_type);
558 bswap_field_64(sample_regs_user);
559 bswap_field_32(sample_stack_user);
560 bswap_field_32(aux_watermark);
561
562 /*
563 * After read_format are bitfields. Check read_format because
564 * we are unable to use offsetof on bitfield.
565 */
566 if (bswap_safe(read_format, 1))
567 swap_bitfield((u8 *) (&attr->read_format + 1),
568 sizeof(u64));
569#undef bswap_field_64
570#undef bswap_field_32
571#undef bswap_field
572#undef bswap_safe
David Aherneda39132011-07-15 12:34:09 -0600573}
574
Jiri Olsa268fb202012-05-30 14:23:43 +0200575static void perf_event__hdr_attr_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300576 bool sample_id_all __maybe_unused)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500577{
578 size_t size;
579
David Aherneda39132011-07-15 12:34:09 -0600580 perf_event__attr_swap(&event->attr.attr);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500581
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200582 size = event->header.size;
583 size -= (void *)&event->attr.id - (void *)event;
584 mem_bswap_64(event->attr.id, size);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500585}
586
Jiri Olsa268fb202012-05-30 14:23:43 +0200587static void perf_event__event_type_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300588 bool sample_id_all __maybe_unused)
Tom Zanussicd19a032010-04-01 23:59:20 -0500589{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200590 event->event_type.event_type.event_id =
591 bswap_64(event->event_type.event_type.event_id);
Tom Zanussicd19a032010-04-01 23:59:20 -0500592}
593
Jiri Olsa268fb202012-05-30 14:23:43 +0200594static void perf_event__tracing_data_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300595 bool sample_id_all __maybe_unused)
Tom Zanussi92155452010-04-01 23:59:21 -0500596{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200597 event->tracing_data.size = bswap_32(event->tracing_data.size);
Tom Zanussi92155452010-04-01 23:59:21 -0500598}
599
Adrian Huntera16ac022015-04-09 18:53:43 +0300600static void perf_event__auxtrace_info_swap(union perf_event *event,
601 bool sample_id_all __maybe_unused)
602{
603 size_t size;
604
605 event->auxtrace_info.type = bswap_32(event->auxtrace_info.type);
606
607 size = event->header.size;
608 size -= (void *)&event->auxtrace_info.priv - (void *)event;
609 mem_bswap_64(event->auxtrace_info.priv, size);
610}
611
612static void perf_event__auxtrace_swap(union perf_event *event,
613 bool sample_id_all __maybe_unused)
614{
615 event->auxtrace.size = bswap_64(event->auxtrace.size);
616 event->auxtrace.offset = bswap_64(event->auxtrace.offset);
617 event->auxtrace.reference = bswap_64(event->auxtrace.reference);
618 event->auxtrace.idx = bswap_32(event->auxtrace.idx);
619 event->auxtrace.tid = bswap_32(event->auxtrace.tid);
620 event->auxtrace.cpu = bswap_32(event->auxtrace.cpu);
621}
622
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300623static void perf_event__auxtrace_error_swap(union perf_event *event,
624 bool sample_id_all __maybe_unused)
625{
626 event->auxtrace_error.type = bswap_32(event->auxtrace_error.type);
627 event->auxtrace_error.code = bswap_32(event->auxtrace_error.code);
628 event->auxtrace_error.cpu = bswap_32(event->auxtrace_error.cpu);
629 event->auxtrace_error.pid = bswap_32(event->auxtrace_error.pid);
630 event->auxtrace_error.tid = bswap_32(event->auxtrace_error.tid);
631 event->auxtrace_error.ip = bswap_64(event->auxtrace_error.ip);
632}
633
Jiri Olsa268fb202012-05-30 14:23:43 +0200634typedef void (*perf_event__swap_op)(union perf_event *event,
635 bool sample_id_all);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200636
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200637static perf_event__swap_op perf_event__swap_ops[] = {
638 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200639 [PERF_RECORD_MMAP2] = perf_event__mmap2_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200640 [PERF_RECORD_COMM] = perf_event__comm_swap,
641 [PERF_RECORD_FORK] = perf_event__task_swap,
642 [PERF_RECORD_EXIT] = perf_event__task_swap,
643 [PERF_RECORD_LOST] = perf_event__all64_swap,
644 [PERF_RECORD_READ] = perf_event__read_swap,
Jiri Olsadd96c462013-09-01 12:36:15 +0200645 [PERF_RECORD_THROTTLE] = perf_event__throttle_swap,
646 [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200647 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300648 [PERF_RECORD_AUX] = perf_event__aux_swap,
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300649 [PERF_RECORD_ITRACE_START] = perf_event__itrace_start_swap,
Kan Liangc4937a92015-05-10 15:13:15 -0400650 [PERF_RECORD_LOST_SAMPLES] = perf_event__all64_swap,
Adrian Hunter02860392015-07-21 12:44:03 +0300651 [PERF_RECORD_SWITCH] = perf_event__switch_swap,
652 [PERF_RECORD_SWITCH_CPU_WIDE] = perf_event__switch_swap,
David Aherneda39132011-07-15 12:34:09 -0600653 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200654 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
655 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
656 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200657 [PERF_RECORD_ID_INDEX] = perf_event__all64_swap,
Adrian Huntera16ac022015-04-09 18:53:43 +0300658 [PERF_RECORD_AUXTRACE_INFO] = perf_event__auxtrace_info_swap,
659 [PERF_RECORD_AUXTRACE] = perf_event__auxtrace_swap,
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300660 [PERF_RECORD_AUXTRACE_ERROR] = perf_event__auxtrace_error_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200661 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200662};
663
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200664/*
665 * When perf record finishes a pass on every buffers, it records this pseudo
666 * event.
667 * We record the max timestamp t found in the pass n.
668 * Assuming these timestamps are monotonic across cpus, we know that if
669 * a buffer still has events with timestamps below t, they will be all
670 * available and then read in the pass n + 1.
671 * Hence when we start to read the pass n + 2, we can safely flush every
672 * events with timestamps below t.
673 *
674 * ============ PASS n =================
675 * CPU 0 | CPU 1
676 * |
677 * cnt1 timestamps | cnt2 timestamps
678 * 1 | 2
679 * 2 | 3
680 * - | 4 <--- max recorded
681 *
682 * ============ PASS n + 1 ==============
683 * CPU 0 | CPU 1
684 * |
685 * cnt1 timestamps | cnt2 timestamps
686 * 3 | 5
687 * 4 | 6
688 * 5 | 7 <---- max recorded
689 *
690 * Flush every events below timestamp 4
691 *
692 * ============ PASS n + 2 ==============
693 * CPU 0 | CPU 1
694 * |
695 * cnt1 timestamps | cnt2 timestamps
696 * 6 | 8
697 * 7 | 9
698 * - | 10
699 *
700 * Flush every events below timestamp 7
701 * etc...
702 */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300703static int process_finished_round(struct perf_tool *tool __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300704 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300705 struct ordered_events *oe)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200706{
Adrian Hunter5531e162015-06-23 10:52:48 +0300707 if (dump_trace)
708 fprintf(stdout, "\n");
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300709 return ordered_events__flush(oe, OE_FLUSH__ROUND);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200710}
711
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300712int perf_session__queue_event(struct perf_session *s, union perf_event *event,
713 struct perf_sample *sample, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200714{
Arnaldo Carvalho de Melo4a6b3622015-03-03 13:02:24 -0300715 return ordered_events__queue(&s->ordered_events, event, sample, file_offset);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200716}
717
Kan Liang384b6052015-01-05 13:23:05 -0500718static void callchain__lbr_callstack_printf(struct perf_sample *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200719{
Kan Liang384b6052015-01-05 13:23:05 -0500720 struct ip_callchain *callchain = sample->callchain;
721 struct branch_stack *lbr_stack = sample->branch_stack;
722 u64 kernel_callchain_nr = callchain->nr;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200723 unsigned int i;
724
Kan Liang384b6052015-01-05 13:23:05 -0500725 for (i = 0; i < kernel_callchain_nr; i++) {
726 if (callchain->ips[i] == PERF_CONTEXT_USER)
727 break;
728 }
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200729
Kan Liang384b6052015-01-05 13:23:05 -0500730 if ((i != kernel_callchain_nr) && lbr_stack->nr) {
731 u64 total_nr;
732 /*
733 * LBR callstack can only get user call chain,
734 * i is kernel call chain number,
735 * 1 is PERF_CONTEXT_USER.
736 *
737 * The user call chain is stored in LBR registers.
738 * LBR are pair registers. The caller is stored
739 * in "from" register, while the callee is stored
740 * in "to" register.
741 * For example, there is a call stack
742 * "A"->"B"->"C"->"D".
743 * The LBR registers will recorde like
744 * "C"->"D", "B"->"C", "A"->"B".
745 * So only the first "to" register and all "from"
746 * registers are needed to construct the whole stack.
747 */
748 total_nr = i + 1 + lbr_stack->nr + 1;
749 kernel_callchain_nr = i + 1;
750
751 printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr);
752
753 for (i = 0; i < kernel_callchain_nr; i++)
754 printf("..... %2d: %016" PRIx64 "\n",
755 i, callchain->ips[i]);
756
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200757 printf("..... %2d: %016" PRIx64 "\n",
Kan Liang384b6052015-01-05 13:23:05 -0500758 (int)(kernel_callchain_nr), lbr_stack->entries[0].to);
759 for (i = 0; i < lbr_stack->nr; i++)
760 printf("..... %2d: %016" PRIx64 "\n",
761 (int)(i + kernel_callchain_nr + 1), lbr_stack->entries[i].from);
762 }
763}
764
765static void callchain__printf(struct perf_evsel *evsel,
766 struct perf_sample *sample)
767{
768 unsigned int i;
769 struct ip_callchain *callchain = sample->callchain;
770
771 if (has_branch_callstack(evsel))
772 callchain__lbr_callstack_printf(sample);
773
774 printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr);
775
776 for (i = 0; i < callchain->nr; i++)
777 printf("..... %2d: %016" PRIx64 "\n",
778 i, callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200779}
780
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100781static void branch_stack__printf(struct perf_sample *sample)
782{
783 uint64_t i;
784
785 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
786
Andi Kleen0e332f02015-07-18 08:24:46 -0700787 for (i = 0; i < sample->branch_stack->nr; i++) {
788 struct branch_entry *e = &sample->branch_stack->entries[i];
789
790 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 " %hu cycles %s%s%s%s %x\n",
791 i, e->from, e->to,
792 e->flags.cycles,
793 e->flags.mispred ? "M" : " ",
794 e->flags.predicted ? "P" : " ",
795 e->flags.abort ? "A" : " ",
796 e->flags.in_tx ? "T" : " ",
797 (unsigned)e->flags.reserved);
798 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100799}
800
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200801static void regs_dump__printf(u64 mask, u64 *regs)
802{
803 unsigned rid, i = 0;
804
805 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
806 u64 val = regs[i++];
807
808 printf(".... %-5s 0x%" PRIx64 "\n",
809 perf_reg_name(rid), val);
810 }
811}
812
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200813static const char *regs_abi[] = {
814 [PERF_SAMPLE_REGS_ABI_NONE] = "none",
815 [PERF_SAMPLE_REGS_ABI_32] = "32-bit",
816 [PERF_SAMPLE_REGS_ABI_64] = "64-bit",
817};
818
819static inline const char *regs_dump_abi(struct regs_dump *d)
820{
821 if (d->abi > PERF_SAMPLE_REGS_ABI_64)
822 return "unknown";
823
824 return regs_abi[d->abi];
825}
826
827static void regs__printf(const char *type, struct regs_dump *regs)
828{
829 u64 mask = regs->mask;
830
831 printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n",
832 type,
833 mask,
834 regs_dump_abi(regs));
835
836 regs_dump__printf(mask, regs->regs);
837}
838
Jiri Olsa352ea452014-01-07 13:47:25 +0100839static void regs_user__printf(struct perf_sample *sample)
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200840{
841 struct regs_dump *user_regs = &sample->user_regs;
842
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200843 if (user_regs->regs)
844 regs__printf("user", user_regs);
845}
846
847static void regs_intr__printf(struct perf_sample *sample)
848{
849 struct regs_dump *intr_regs = &sample->intr_regs;
850
851 if (intr_regs->regs)
852 regs__printf("intr", intr_regs);
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200853}
854
855static void stack_user__printf(struct stack_dump *dump)
856{
857 printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
858 dump->size, dump->offset);
859}
860
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300861static void perf_evlist__print_tstamp(struct perf_evlist *evlist,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200862 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200863 struct perf_sample *sample)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200864{
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300865 u64 sample_type = __perf_evlist__combined_sample_type(evlist);
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300866
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200867 if (event->header.type != PERF_RECORD_SAMPLE &&
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300868 !perf_evlist__sample_id_all(evlist)) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200869 fputs("-1 -1 ", stdout);
870 return;
871 }
872
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300873 if ((sample_type & PERF_SAMPLE_CPU))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200874 printf("%u ", sample->cpu);
875
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300876 if (sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200877 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200878}
879
Jiri Olsa9ede4732012-10-10 17:38:13 +0200880static void sample_read__printf(struct perf_sample *sample, u64 read_format)
881{
882 printf("... sample_read:\n");
883
884 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
885 printf("...... time enabled %016" PRIx64 "\n",
886 sample->read.time_enabled);
887
888 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
889 printf("...... time running %016" PRIx64 "\n",
890 sample->read.time_running);
891
892 if (read_format & PERF_FORMAT_GROUP) {
893 u64 i;
894
895 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
896
897 for (i = 0; i < sample->read.group.nr; i++) {
898 struct sample_read_value *value;
899
900 value = &sample->read.group.values[i];
901 printf("..... id %016" PRIx64
902 ", value %016" PRIx64 "\n",
903 value->id, value->value);
904 }
905 } else
906 printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
907 sample->read.one.id, sample->read.one.value);
908}
909
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300910static void dump_event(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200911 u64 file_offset, struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000912{
913 if (!dump_trace)
914 return;
915
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200916 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
917 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000918
919 trace_event(event);
920
921 if (sample)
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300922 perf_evlist__print_tstamp(evlist, event, sample);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000923
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200924 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200925 event->header.size, perf_event__name(event->header.type));
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000926}
927
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200928static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200929 struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000930{
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300931 u64 sample_type;
932
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200933 if (!dump_trace)
934 return;
935
Don Zickus0ea590a2014-02-25 22:43:46 -0500936 printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200937 event->header.misc, sample->pid, sample->tid, sample->ip,
David Ahern7cec0922011-05-30 13:08:23 -0600938 sample->period, sample->addr);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000939
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200940 sample_type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300941
942 if (sample_type & PERF_SAMPLE_CALLCHAIN)
Kan Liang384b6052015-01-05 13:23:05 -0500943 callchain__printf(evsel, sample);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100944
Kan Liang384b6052015-01-05 13:23:05 -0500945 if ((sample_type & PERF_SAMPLE_BRANCH_STACK) && !has_branch_callstack(evsel))
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100946 branch_stack__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200947
948 if (sample_type & PERF_SAMPLE_REGS_USER)
Jiri Olsa352ea452014-01-07 13:47:25 +0100949 regs_user__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200950
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200951 if (sample_type & PERF_SAMPLE_REGS_INTR)
952 regs_intr__printf(sample);
953
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200954 if (sample_type & PERF_SAMPLE_STACK_USER)
955 stack_user__printf(&sample->user_stack);
Andi Kleen05484292013-01-24 16:10:29 +0100956
957 if (sample_type & PERF_SAMPLE_WEIGHT)
958 printf("... weight: %" PRIu64 "\n", sample->weight);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100959
960 if (sample_type & PERF_SAMPLE_DATA_SRC)
961 printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
Jiri Olsa9ede4732012-10-10 17:38:13 +0200962
Andi Kleen475eeab2013-09-20 07:40:43 -0700963 if (sample_type & PERF_SAMPLE_TRANSACTION)
964 printf("... transaction: %" PRIx64 "\n", sample->transaction);
965
Jiri Olsa9ede4732012-10-10 17:38:13 +0200966 if (sample_type & PERF_SAMPLE_READ)
967 sample_read__printf(sample, evsel->attr.read_format);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000968}
969
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -0300970static struct machine *machines__find_for_cpumode(struct machines *machines,
Adrian Hunteref893252013-08-27 11:23:06 +0300971 union perf_event *event,
972 struct perf_sample *sample)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200973{
974 const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Dongsheng Yangad85ace2013-12-20 13:41:47 -0500975 struct machine *machine;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200976
David Ahern7c0f4a42012-07-20 17:25:48 -0600977 if (perf_guest &&
978 ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
979 (cpumode == PERF_RECORD_MISC_GUEST_USER))) {
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530980 u32 pid;
981
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200982 if (event->header.type == PERF_RECORD_MMAP
983 || event->header.type == PERF_RECORD_MMAP2)
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530984 pid = event->mmap.pid;
985 else
Adrian Hunteref893252013-08-27 11:23:06 +0300986 pid = sample->pid;
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530987
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -0300988 machine = machines__find(machines, pid);
Dongsheng Yangad85ace2013-12-20 13:41:47 -0500989 if (!machine)
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -0300990 machine = machines__find(machines, DEFAULT_GUEST_KERNEL_ID);
Dongsheng Yangad85ace2013-12-20 13:41:47 -0500991 return machine;
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530992 }
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200993
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -0300994 return &machines->host;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200995}
996
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -0300997static int deliver_sample_value(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +0200998 struct perf_tool *tool,
999 union perf_event *event,
1000 struct perf_sample *sample,
1001 struct sample_read_value *v,
1002 struct machine *machine)
1003{
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001004 struct perf_sample_id *sid = perf_evlist__id2sid(evlist, v->id);
Jiri Olsae4caec02012-10-10 18:52:24 +02001005
Jiri Olsae4caec02012-10-10 18:52:24 +02001006 if (sid) {
1007 sample->id = v->id;
1008 sample->period = v->value - sid->period;
1009 sid->period = v->value;
1010 }
1011
1012 if (!sid || sid->evsel == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001013 ++evlist->stats.nr_unknown_id;
Jiri Olsae4caec02012-10-10 18:52:24 +02001014 return 0;
1015 }
1016
1017 return tool->sample(tool, event, sample, sid->evsel, machine);
1018}
1019
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001020static int deliver_sample_group(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +02001021 struct perf_tool *tool,
1022 union perf_event *event,
1023 struct perf_sample *sample,
1024 struct machine *machine)
1025{
1026 int ret = -EINVAL;
1027 u64 i;
1028
1029 for (i = 0; i < sample->read.group.nr; i++) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001030 ret = deliver_sample_value(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001031 &sample->read.group.values[i],
1032 machine);
1033 if (ret)
1034 break;
1035 }
1036
1037 return ret;
1038}
1039
1040static int
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001041 perf_evlist__deliver_sample(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +02001042 struct perf_tool *tool,
1043 union perf_event *event,
1044 struct perf_sample *sample,
1045 struct perf_evsel *evsel,
1046 struct machine *machine)
1047{
1048 /* We know evsel != NULL. */
1049 u64 sample_type = evsel->attr.sample_type;
1050 u64 read_format = evsel->attr.read_format;
1051
1052 /* Standard sample delievery. */
1053 if (!(sample_type & PERF_SAMPLE_READ))
1054 return tool->sample(tool, event, sample, evsel, machine);
1055
1056 /* For PERF_SAMPLE_READ we have either single or group mode. */
1057 if (read_format & PERF_FORMAT_GROUP)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001058 return deliver_sample_group(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001059 machine);
1060 else
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001061 return deliver_sample_value(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +02001062 &sample->read.one, machine);
1063}
1064
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -03001065static int machines__deliver_event(struct machines *machines,
1066 struct perf_evlist *evlist,
1067 union perf_event *event,
1068 struct perf_sample *sample,
1069 struct perf_tool *tool, u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001070{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001071 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001072 struct machine *machine;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001073
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001074 dump_event(evlist, event, file_offset, sample);
Thomas Gleixner532e7262010-12-07 12:48:55 +00001075
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001076 evsel = perf_evlist__id2evsel(evlist, sample->id);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001077
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001078 machine = machines__find_for_cpumode(machines, event, sample);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001079
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001080 switch (event->header.type) {
1081 case PERF_RECORD_SAMPLE:
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001082 dump_sample(evsel, event, sample);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001083 if (evsel == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001084 ++evlist->stats.nr_unknown_id;
Jiri Olsa67822062012-04-12 14:21:01 +02001085 return 0;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001086 }
Joerg Roedel0c095712012-02-10 18:05:04 +01001087 if (machine == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001088 ++evlist->stats.nr_unprocessable_samples;
Jiri Olsa67822062012-04-12 14:21:01 +02001089 return 0;
Joerg Roedel0c095712012-02-10 18:05:04 +01001090 }
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001091 return perf_evlist__deliver_sample(evlist, tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001092 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001093 return tool->mmap(tool, event, sample, machine);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001094 case PERF_RECORD_MMAP2:
Kan Liang930e6fc2015-06-17 09:51:10 -04001095 if (event->header.misc & PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT)
1096 ++evlist->stats.nr_proc_map_timeout;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001097 return tool->mmap2(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001098 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001099 return tool->comm(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001100 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001101 return tool->fork(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001102 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001103 return tool->exit(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001104 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001105 if (tool->lost == perf_event__process_lost)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001106 evlist->stats.total_lost += event->lost.lost;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001107 return tool->lost(tool, event, sample, machine);
Kan Liangc4937a92015-05-10 15:13:15 -04001108 case PERF_RECORD_LOST_SAMPLES:
1109 if (tool->lost_samples == perf_event__process_lost_samples)
1110 evlist->stats.total_lost_samples += event->lost_samples.lost;
1111 return tool->lost_samples(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001112 case PERF_RECORD_READ:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001113 return tool->read(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001114 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001115 return tool->throttle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001116 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001117 return tool->unthrottle(tool, event, sample, machine);
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001118 case PERF_RECORD_AUX:
1119 return tool->aux(tool, event, sample, machine);
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001120 case PERF_RECORD_ITRACE_START:
1121 return tool->itrace_start(tool, event, sample, machine);
Adrian Hunter02860392015-07-21 12:44:03 +03001122 case PERF_RECORD_SWITCH:
1123 case PERF_RECORD_SWITCH_CPU_WIDE:
1124 return tool->context_switch(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001125 default:
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001126 ++evlist->stats.nr_unknown_events;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001127 return -1;
1128 }
1129}
1130
Adrian Hunterc4468702015-04-09 18:53:48 +03001131static int perf_session__deliver_event(struct perf_session *session,
1132 union perf_event *event,
1133 struct perf_sample *sample,
1134 struct perf_tool *tool,
1135 u64 file_offset)
1136{
1137 int ret;
1138
1139 ret = auxtrace__process_event(session, event, sample, tool);
1140 if (ret < 0)
1141 return ret;
1142 if (ret > 0)
1143 return 0;
1144
1145 return machines__deliver_event(&session->machines, session->evlist,
1146 event, sample, tool, file_offset);
1147}
1148
Adrian Hunterd5652d82014-07-23 22:19:58 +03001149static s64 perf_session__process_user_event(struct perf_session *session,
1150 union perf_event *event,
Adrian Hunterd5652d82014-07-23 22:19:58 +03001151 u64 file_offset)
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001152{
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -03001153 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001154 struct perf_tool *tool = session->tool;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001155 int fd = perf_data_file__fd(session->file);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001156 int err;
1157
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001158 dump_event(session->evlist, event, file_offset, NULL);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001159
1160 /* These events are processed right away */
1161 switch (event->header.type) {
1162 case PERF_RECORD_HEADER_ATTR:
Adrian Hunter47c3d102013-07-04 16:20:21 +03001163 err = tool->attr(tool, event, &session->evlist);
Adrian Huntercfe1c412014-07-31 09:00:45 +03001164 if (err == 0) {
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -03001165 perf_session__set_id_hdr_size(session);
Adrian Huntercfe1c412014-07-31 09:00:45 +03001166 perf_session__set_comm_exec(session);
1167 }
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001168 return err;
Jiri Olsaf67697b2014-02-04 15:37:48 +01001169 case PERF_RECORD_HEADER_EVENT_TYPE:
1170 /*
1171 * Depreceated, but we need to handle it for sake
1172 * of old data files create in pipe mode.
1173 */
1174 return 0;
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001175 case PERF_RECORD_HEADER_TRACING_DATA:
1176 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001177 lseek(fd, file_offset, SEEK_SET);
Adrian Hunter47c3d102013-07-04 16:20:21 +03001178 return tool->tracing_data(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001179 case PERF_RECORD_HEADER_BUILD_ID:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001180 return tool->build_id(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001181 case PERF_RECORD_FINISHED_ROUND:
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -03001182 return tool->finished_round(tool, event, oe);
Adrian Hunter3c659ee2014-10-27 15:49:22 +02001183 case PERF_RECORD_ID_INDEX:
1184 return tool->id_index(tool, event, session);
Adrian Huntera16ac022015-04-09 18:53:43 +03001185 case PERF_RECORD_AUXTRACE_INFO:
1186 return tool->auxtrace_info(tool, event, session);
1187 case PERF_RECORD_AUXTRACE:
1188 /* setup for reading amidst mmap */
1189 lseek(fd, file_offset + event->header.size, SEEK_SET);
1190 return tool->auxtrace(tool, event, session);
Adrian Huntere9bf54d2015-04-09 18:53:47 +03001191 case PERF_RECORD_AUXTRACE_ERROR:
Adrian Hunter85ed4722015-04-09 18:53:50 +03001192 perf_session__auxtrace_error_inc(session, event);
Adrian Huntere9bf54d2015-04-09 18:53:47 +03001193 return tool->auxtrace_error(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001194 default:
1195 return -EINVAL;
1196 }
1197}
1198
Adrian Huntera2938292014-10-27 15:49:23 +02001199int perf_session__deliver_synth_event(struct perf_session *session,
1200 union perf_event *event,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001201 struct perf_sample *sample)
Adrian Huntera2938292014-10-27 15:49:23 +02001202{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001203 struct perf_evlist *evlist = session->evlist;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001204 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001205
1206 events_stats__inc(&evlist->stats, event->header.type);
Adrian Huntera2938292014-10-27 15:49:23 +02001207
1208 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001209 return perf_session__process_user_event(session, event, 0);
Adrian Huntera2938292014-10-27 15:49:23 +02001210
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001211 return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0);
Adrian Huntera2938292014-10-27 15:49:23 +02001212}
1213
Jiri Olsa268fb202012-05-30 14:23:43 +02001214static void event_swap(union perf_event *event, bool sample_id_all)
1215{
1216 perf_event__swap_op swap;
1217
1218 swap = perf_event__swap_ops[event->header.type];
1219 if (swap)
1220 swap(event, sample_id_all);
1221}
1222
Adrian Hunter5a52f332014-07-31 09:00:57 +03001223int perf_session__peek_event(struct perf_session *session, off_t file_offset,
1224 void *buf, size_t buf_sz,
1225 union perf_event **event_ptr,
1226 struct perf_sample *sample)
1227{
1228 union perf_event *event;
1229 size_t hdr_sz, rest;
1230 int fd;
1231
1232 if (session->one_mmap && !session->header.needs_swap) {
1233 event = file_offset - session->one_mmap_offset +
1234 session->one_mmap_addr;
1235 goto out_parse_sample;
1236 }
1237
1238 if (perf_data_file__is_pipe(session->file))
1239 return -1;
1240
1241 fd = perf_data_file__fd(session->file);
1242 hdr_sz = sizeof(struct perf_event_header);
1243
1244 if (buf_sz < hdr_sz)
1245 return -1;
1246
1247 if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 ||
Adrian Hunter554e92e2015-05-19 16:05:45 +03001248 readn(fd, buf, hdr_sz) != (ssize_t)hdr_sz)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001249 return -1;
1250
1251 event = (union perf_event *)buf;
1252
1253 if (session->header.needs_swap)
1254 perf_event_header__bswap(&event->header);
1255
Adrian Hunter554e92e2015-05-19 16:05:45 +03001256 if (event->header.size < hdr_sz || event->header.size > buf_sz)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001257 return -1;
1258
1259 rest = event->header.size - hdr_sz;
1260
Adrian Hunter554e92e2015-05-19 16:05:45 +03001261 if (readn(fd, buf, rest) != (ssize_t)rest)
Adrian Hunter5a52f332014-07-31 09:00:57 +03001262 return -1;
1263
1264 if (session->header.needs_swap)
1265 event_swap(event, perf_evlist__sample_id_all(session->evlist));
1266
1267out_parse_sample:
1268
1269 if (sample && event->header.type < PERF_RECORD_USER_TYPE_START &&
1270 perf_evlist__parse_sample(session->evlist, event, sample))
1271 return -1;
1272
1273 *event_ptr = event;
1274
1275 return 0;
1276}
1277
Adrian Hunterd5652d82014-07-23 22:19:58 +03001278static s64 perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001279 union perf_event *event, u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001280{
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001281 struct perf_evlist *evlist = session->evlist;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001282 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001283 struct perf_sample sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001284 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001285
Jiri Olsa268fb202012-05-30 14:23:43 +02001286 if (session->header.needs_swap)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001287 event_swap(event, perf_evlist__sample_id_all(evlist));
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001288
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001289 if (event->header.type >= PERF_RECORD_HEADER_MAX)
1290 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001291
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001292 events_stats__inc(&evlist->stats, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001293
1294 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001295 return perf_session__process_user_event(session, event, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001296
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001297 /*
1298 * For all kernel events we get the sample data
1299 */
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001300 ret = perf_evlist__parse_sample(evlist, event, &sample);
Frederic Weisbecker5538bec2011-05-22 02:17:22 +02001301 if (ret)
1302 return ret;
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001303
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001304 if (tool->ordered_events) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001305 ret = perf_session__queue_event(session, event, &sample, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001306 if (ret != -ETIME)
1307 return ret;
1308 }
1309
Adrian Hunterc4468702015-04-09 18:53:48 +03001310 return perf_session__deliver_event(session, event, &sample, tool,
1311 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001312}
1313
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001314void perf_event_header__bswap(struct perf_event_header *hdr)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001315{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001316 hdr->type = bswap_32(hdr->type);
1317 hdr->misc = bswap_16(hdr->misc);
1318 hdr->size = bswap_16(hdr->size);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001319}
1320
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001321struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1322{
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001323 return machine__findnew_thread(&session->machines.host, -1, pid);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001324}
1325
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001326static struct thread *perf_session__register_idle_thread(struct perf_session *session)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001327{
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001328 struct thread *thread;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001329
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001330 thread = machine__findnew_thread(&session->machines.host, 0, 0);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001331 if (thread == NULL || thread__set_comm(thread, "swapper", 0)) {
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001332 pr_err("problem inserting idle task.\n");
1333 thread = NULL;
1334 }
1335
1336 return thread;
1337}
1338
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001339static void perf_session__warn_about_errors(const struct perf_session *session)
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001340{
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001341 const struct events_stats *stats = &session->evlist->stats;
1342 const struct ordered_events *oe = &session->ordered_events;
1343
1344 if (session->tool->lost == perf_event__process_lost &&
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001345 stats->nr_events[PERF_RECORD_LOST] != 0) {
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001346 ui__warning("Processed %d events and lost %d chunks!\n\n"
1347 "Check IO/CPU overload!\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001348 stats->nr_events[0],
1349 stats->nr_events[PERF_RECORD_LOST]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001350 }
1351
Kan Liangc4937a92015-05-10 15:13:15 -04001352 if (session->tool->lost_samples == perf_event__process_lost_samples) {
1353 double drop_rate;
1354
1355 drop_rate = (double)stats->total_lost_samples /
1356 (double) (stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples);
1357 if (drop_rate > 0.05) {
He Kuang6ba29c22015-06-11 12:44:24 +00001358 ui__warning("Processed %" PRIu64 " samples and lost %3.2f%% samples!\n\n",
Kan Liangc4937a92015-05-10 15:13:15 -04001359 stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples,
1360 drop_rate * 100.0);
1361 }
1362 }
1363
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001364 if (stats->nr_unknown_events != 0) {
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001365 ui__warning("Found %u unknown events!\n\n"
1366 "Is this an older tool processing a perf.data "
1367 "file generated by a more recent tool?\n\n"
1368 "If that is not the case, consider "
1369 "reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001370 stats->nr_unknown_events);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001371 }
1372
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001373 if (stats->nr_unknown_id != 0) {
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001374 ui__warning("%u samples with id not present in the header\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001375 stats->nr_unknown_id);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001376 }
1377
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001378 if (stats->nr_invalid_chains != 0) {
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001379 ui__warning("Found invalid callchains!\n\n"
1380 "%u out of %u events were discarded for this reason.\n\n"
1381 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001382 stats->nr_invalid_chains,
1383 stats->nr_events[PERF_RECORD_SAMPLE]);
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001384 }
Joerg Roedel0c095712012-02-10 18:05:04 +01001385
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001386 if (stats->nr_unprocessable_samples != 0) {
Joerg Roedel0c095712012-02-10 18:05:04 +01001387 ui__warning("%u unprocessable samples recorded.\n"
1388 "Do you have a KVM guest running and not using 'perf kvm'?\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001389 stats->nr_unprocessable_samples);
Joerg Roedel0c095712012-02-10 18:05:04 +01001390 }
Jiri Olsaf61ff6c2014-11-26 16:39:31 +01001391
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001392 if (oe->nr_unordered_events != 0)
1393 ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events);
Adrian Hunter85ed4722015-04-09 18:53:50 +03001394
1395 events_stats__auxtrace_error_warn(stats);
Kan Liang930e6fc2015-06-17 09:51:10 -04001396
1397 if (stats->nr_proc_map_timeout != 0) {
1398 ui__warning("%d map information files for pre-existing threads were\n"
1399 "not processed, if there are samples for addresses they\n"
1400 "will not be resolved, you may find out which are these\n"
1401 "threads by running with -v and redirecting the output\n"
Kan Liang9d9cad72015-06-17 09:51:11 -04001402 "to a file.\n"
1403 "The time limit to process proc map is too short?\n"
1404 "Increase it by --proc-map-timeout\n",
Kan Liang930e6fc2015-06-17 09:51:10 -04001405 stats->nr_proc_map_timeout);
1406 }
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001407}
1408
Adrian Huntera5499b32015-05-29 16:33:30 +03001409static int perf_session__flush_thread_stack(struct thread *thread,
1410 void *p __maybe_unused)
1411{
1412 return thread_stack__flush(thread);
1413}
1414
1415static int perf_session__flush_thread_stacks(struct perf_session *session)
1416{
1417 return machines__for_each_thread(&session->machines,
1418 perf_session__flush_thread_stack,
1419 NULL);
1420}
1421
Tom Zanussi8dc58102010-04-01 23:59:15 -05001422volatile int session_done;
1423
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001424static int __perf_session__process_pipe_events(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05001425{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001426 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001427 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001428 int fd = perf_data_file__fd(session->file);
Stephane Eranian444d2862012-05-15 13:28:12 +02001429 union perf_event *event;
1430 uint32_t size, cur_size = 0;
1431 void *buf = NULL;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001432 s64 skip = 0;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001433 u64 head;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001434 ssize_t err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001435 void *p;
1436
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001437 perf_tool__fill_defaults(tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001438
1439 head = 0;
Stephane Eranian444d2862012-05-15 13:28:12 +02001440 cur_size = sizeof(union perf_event);
1441
1442 buf = malloc(cur_size);
1443 if (!buf)
1444 return -errno;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001445more:
Stephane Eranian444d2862012-05-15 13:28:12 +02001446 event = buf;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001447 err = readn(fd, event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05001448 if (err <= 0) {
1449 if (err == 0)
1450 goto done;
1451
1452 pr_err("failed to read event header\n");
1453 goto out_err;
1454 }
1455
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001456 if (session->header.needs_swap)
Stephane Eranian444d2862012-05-15 13:28:12 +02001457 perf_event_header__bswap(&event->header);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001458
Stephane Eranian444d2862012-05-15 13:28:12 +02001459 size = event->header.size;
Adrian Hunter27389d72013-07-04 16:20:27 +03001460 if (size < sizeof(struct perf_event_header)) {
1461 pr_err("bad event header size\n");
1462 goto out_err;
1463 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001464
Stephane Eranian444d2862012-05-15 13:28:12 +02001465 if (size > cur_size) {
1466 void *new = realloc(buf, size);
1467 if (!new) {
1468 pr_err("failed to allocate memory to read event\n");
1469 goto out_err;
1470 }
1471 buf = new;
1472 cur_size = size;
1473 event = buf;
1474 }
1475 p = event;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001476 p += sizeof(struct perf_event_header);
1477
Tom Zanussi794e43b2010-05-05 00:27:40 -05001478 if (size - sizeof(struct perf_event_header)) {
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001479 err = readn(fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -05001480 if (err <= 0) {
1481 if (err == 0) {
1482 pr_err("unexpected end of event stream\n");
1483 goto done;
1484 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001485
Tom Zanussi794e43b2010-05-05 00:27:40 -05001486 pr_err("failed to read event data\n");
1487 goto out_err;
1488 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001489 }
1490
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001491 if ((skip = perf_session__process_event(session, event, head)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001492 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
Stephane Eranian444d2862012-05-15 13:28:12 +02001493 head, event->header.size, event->header.type);
Jiri Olsa9389a462012-04-16 20:42:51 +02001494 err = -EINVAL;
1495 goto out_err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001496 }
1497
1498 head += size;
1499
Tom Zanussi8dc58102010-04-01 23:59:15 -05001500 if (skip > 0)
1501 head += skip;
1502
1503 if (!session_done())
1504 goto more;
1505done:
Adrian Hunter8c16b642013-10-18 15:29:02 +03001506 /* do the final flush for ordered samples */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001507 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
Adrian Hunterc4468702015-04-09 18:53:48 +03001508 if (err)
1509 goto out_err;
1510 err = auxtrace__flush_events(session, tool);
Adrian Huntera5499b32015-05-29 16:33:30 +03001511 if (err)
1512 goto out_err;
1513 err = perf_session__flush_thread_stacks(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001514out_err:
Stephane Eranian444d2862012-05-15 13:28:12 +02001515 free(buf);
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001516 perf_session__warn_about_errors(session);
Jiri Olsaadc56ed2014-06-10 22:50:03 +02001517 ordered_events__free(&session->ordered_events);
Adrian Hunterc4468702015-04-09 18:53:48 +03001518 auxtrace__free_events(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001519 return err;
1520}
1521
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001522static union perf_event *
1523fetch_mmaped_event(struct perf_session *session,
1524 u64 head, size_t mmap_size, char *buf)
1525{
1526 union perf_event *event;
1527
1528 /*
1529 * Ensure we have enough space remaining to read
1530 * the size of the event in the headers.
1531 */
1532 if (head + sizeof(event->header) > mmap_size)
1533 return NULL;
1534
1535 event = (union perf_event *)(buf + head);
1536
1537 if (session->header.needs_swap)
1538 perf_event_header__bswap(&event->header);
1539
Adrian Hunter27389d72013-07-04 16:20:27 +03001540 if (head + event->header.size > mmap_size) {
1541 /* We're not fetching the event so swap back again */
1542 if (session->header.needs_swap)
1543 perf_event_header__bswap(&event->header);
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001544 return NULL;
Adrian Hunter27389d72013-07-04 16:20:27 +03001545 }
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001546
1547 return event;
1548}
1549
David Miller35d48dd2012-11-10 14:12:19 -05001550/*
1551 * On 64bit we can mmap the data file in one go. No need for tiny mmap
1552 * slices. On 32bit we use 32MB.
1553 */
1554#if BITS_PER_LONG == 64
1555#define MMAP_SIZE ULLONG_MAX
1556#define NUM_MMAPS 1
1557#else
1558#define MMAP_SIZE (32 * 1024 * 1024ULL)
1559#define NUM_MMAPS 128
1560#endif
1561
Namhyung Kim4ac30cf2015-01-29 17:06:43 +09001562static int __perf_session__process_events(struct perf_session *session,
1563 u64 data_offset, u64 data_size,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001564 u64 file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001565{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001566 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001567 struct perf_tool *tool = session->tool;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001568 int fd = perf_data_file__fd(session->file);
Adrian Hunterd5652d82014-07-23 22:19:58 +03001569 u64 head, page_offset, file_offset, file_pos, size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001570 int err, mmap_prot, mmap_flags, map_idx = 0;
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -03001571 size_t mmap_size;
David Miller35d48dd2012-11-10 14:12:19 -05001572 char *buf, *mmaps[NUM_MMAPS];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001573 union perf_event *event;
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001574 struct ui_progress prog;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001575 s64 skip;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001576
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001577 perf_tool__fill_defaults(tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001578
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001579 page_offset = page_size * (data_offset / page_size);
1580 file_offset = page_offset;
1581 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001582
Namhyung Kimb314e5c2013-09-30 17:19:48 +09001583 if (data_size && (data_offset + data_size < file_size))
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001584 file_size = data_offset + data_size;
1585
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001586 ui_progress__init(&prog, file_size, "Processing events...");
Thomas Gleixner55b446292010-11-30 17:49:46 +00001587
David Miller35d48dd2012-11-10 14:12:19 -05001588 mmap_size = MMAP_SIZE;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001589 if (mmap_size > file_size) {
Thomas Gleixner55b446292010-11-30 17:49:46 +00001590 mmap_size = file_size;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001591 session->one_mmap = true;
1592 }
Thomas Gleixner55b446292010-11-30 17:49:46 +00001593
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001594 memset(mmaps, 0, sizeof(mmaps));
1595
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001596 mmap_prot = PROT_READ;
1597 mmap_flags = MAP_SHARED;
1598
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001599 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001600 mmap_prot |= PROT_WRITE;
1601 mmap_flags = MAP_PRIVATE;
1602 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001603remap:
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001604 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd,
Thomas Gleixner55b446292010-11-30 17:49:46 +00001605 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001606 if (buf == MAP_FAILED) {
1607 pr_err("failed to mmap file\n");
1608 err = -errno;
1609 goto out_err;
1610 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001611 mmaps[map_idx] = buf;
1612 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001613 file_pos = file_offset + head;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001614 if (session->one_mmap) {
1615 session->one_mmap_addr = buf;
1616 session->one_mmap_offset = file_offset;
1617 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001618
1619more:
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001620 event = fetch_mmaped_event(session, head, mmap_size, buf);
1621 if (!event) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001622 if (mmaps[map_idx]) {
1623 munmap(mmaps[map_idx], mmap_size);
1624 mmaps[map_idx] = NULL;
1625 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001626
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001627 page_offset = page_size * (head / page_size);
1628 file_offset += page_offset;
1629 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001630 goto remap;
1631 }
1632
1633 size = event->header.size;
1634
Adrian Hunter27389d72013-07-04 16:20:27 +03001635 if (size < sizeof(struct perf_event_header) ||
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001636 (skip = perf_session__process_event(session, event, file_pos)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001637 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1638 file_offset + head, event->header.size,
1639 event->header.type);
1640 err = -EINVAL;
1641 goto out_err;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001642 }
1643
Adrian Hunter6f917c72014-07-23 22:19:57 +03001644 if (skip)
1645 size += skip;
1646
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001647 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001648 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001649
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001650 ui_progress__update(&prog, size);
Thomas Gleixner55b446292010-11-30 17:49:46 +00001651
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001652 if (session_done())
Adrian Hunter8c16b642013-10-18 15:29:02 +03001653 goto out;
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001654
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001655 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001656 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001657
Adrian Hunter8c16b642013-10-18 15:29:02 +03001658out:
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001659 /* do the final flush for ordered samples */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001660 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
Adrian Hunterc4468702015-04-09 18:53:48 +03001661 if (err)
1662 goto out_err;
1663 err = auxtrace__flush_events(session, tool);
Adrian Huntera5499b32015-05-29 16:33:30 +03001664 if (err)
1665 goto out_err;
1666 err = perf_session__flush_thread_stacks(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001667out_err:
Namhyung Kima5580f32012-11-13 22:30:34 +09001668 ui_progress__finish();
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001669 perf_session__warn_about_errors(session);
Jiri Olsaadc56ed2014-06-10 22:50:03 +02001670 ordered_events__free(&session->ordered_events);
Adrian Hunterc4468702015-04-09 18:53:48 +03001671 auxtrace__free_events(session);
Adrian Hunter919d86d2014-07-14 13:02:51 +03001672 session->one_mmap = false;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001673 return err;
1674}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001675
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001676int perf_session__process_events(struct perf_session *session)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001677{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001678 u64 size = perf_data_file__size(session->file);
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001679 int err;
1680
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001681 if (perf_session__register_idle_thread(session) == NULL)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001682 return -ENOMEM;
1683
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001684 if (!perf_data_file__is_pipe(session->file))
1685 err = __perf_session__process_events(session,
1686 session->header.data_offset,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001687 session->header.data_size, size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001688 else
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001689 err = __perf_session__process_pipe_events(session);
Dave Martin88ca8952010-07-27 11:46:12 -03001690
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001691 return err;
1692}
1693
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001694bool perf_session__has_traces(struct perf_session *session, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001695{
David Ahern93ea01c292013-08-07 22:50:58 -04001696 struct perf_evsel *evsel;
1697
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001698 evlist__for_each(session->evlist, evsel) {
David Ahern93ea01c292013-08-07 22:50:58 -04001699 if (evsel->attr.type == PERF_TYPE_TRACEPOINT)
1700 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001701 }
1702
David Ahern93ea01c292013-08-07 22:50:58 -04001703 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1704 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001705}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001706
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001707int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1708 const char *symbol_name, u64 addr)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001709{
1710 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001711 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001712 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001713
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001714 ref = zalloc(sizeof(struct ref_reloc_sym));
1715 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001716 return -ENOMEM;
1717
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001718 ref->name = strdup(symbol_name);
1719 if (ref->name == NULL) {
1720 free(ref);
1721 return -ENOMEM;
1722 }
1723
1724 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001725 if (bracket)
1726 *bracket = '\0';
1727
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001728 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001729
1730 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001731 struct kmap *kmap = map__kmap(maps[i]);
Wang Nanba927322015-04-07 08:22:45 +00001732
1733 if (!kmap)
1734 continue;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001735 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001736 }
1737
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001738 return 0;
1739}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001740
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001741size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001742{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001743 return machines__fprintf_dsos(&session->machines, fp);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001744}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001745
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001746size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001747 bool (skip)(struct dso *dso, int parm), int parm)
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001748{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001749 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001750}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001751
1752size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1753{
Adrian Hunterc4468702015-04-09 18:53:48 +03001754 size_t ret;
1755 const char *msg = "";
1756
1757 if (perf_header__has_feat(&session->header, HEADER_AUXTRACE))
1758 msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)";
1759
Adrian Hunterfe692ac2015-06-23 10:52:49 +03001760 ret = fprintf(fp, "\nAggregated stats:%s\n", msg);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001761
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001762 ret += events_stats__fprintf(&session->evlist->stats, fp);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001763 return ret;
1764}
David Ahernc0230b22011-03-09 22:23:27 -07001765
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001766size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1767{
1768 /*
1769 * FIXME: Here we have to actually print all the machines in this
1770 * session, not just the host...
1771 */
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001772 return machine__fprintf(&session->machines.host, fp);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001773}
1774
David Ahern9cbdb702011-04-06 21:54:20 -06001775struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1776 unsigned int type)
1777{
1778 struct perf_evsel *pos;
1779
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001780 evlist__for_each(session->evlist, pos) {
David Ahern9cbdb702011-04-06 21:54:20 -06001781 if (pos->attr.type == type)
1782 return pos;
1783 }
1784 return NULL;
1785}
1786
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001787void perf_evsel__print_ip(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -03001788 struct addr_location *al,
David Ahern307cbb92013-08-07 22:50:53 -04001789 unsigned int print_opts, unsigned int stack_depth)
David Ahernc0230b22011-03-09 22:23:27 -07001790{
David Ahernc0230b22011-03-09 22:23:27 -07001791 struct callchain_cursor_node *node;
David Aherna6ffaf92013-08-07 22:50:51 -04001792 int print_ip = print_opts & PRINT_IP_OPT_IP;
1793 int print_sym = print_opts & PRINT_IP_OPT_SYM;
1794 int print_dso = print_opts & PRINT_IP_OPT_DSO;
1795 int print_symoffset = print_opts & PRINT_IP_OPT_SYMOFFSET;
David Ahernb0b35f02013-08-07 22:50:52 -04001796 int print_oneline = print_opts & PRINT_IP_OPT_ONELINE;
Adrian Huntercc8fae12013-12-06 09:42:57 +02001797 int print_srcline = print_opts & PRINT_IP_OPT_SRCLINE;
David Ahernb0b35f02013-08-07 22:50:52 -04001798 char s = print_oneline ? ' ' : '\t';
David Ahernc0230b22011-03-09 22:23:27 -07001799
David Ahernc0230b22011-03-09 22:23:27 -07001800 if (symbol_conf.use_callchain && sample->callchain) {
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001801 struct addr_location node_al;
David Ahernc0230b22011-03-09 22:23:27 -07001802
Arnaldo Carvalho de Melocc8b7c22014-10-23 15:26:17 -03001803 if (thread__resolve_callchain(al->thread, evsel,
1804 sample, NULL, NULL,
1805 PERF_MAX_STACK_DEPTH) != 0) {
David Ahernc0230b22011-03-09 22:23:27 -07001806 if (verbose)
1807 error("Failed to resolve callchain. Skipping\n");
1808 return;
1809 }
Namhyung Kim47260642012-05-31 14:43:26 +09001810 callchain_cursor_commit(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001811
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001812 if (print_symoffset)
1813 node_al = *al;
1814
David Ahern307cbb92013-08-07 22:50:53 -04001815 while (stack_depth) {
Adrian Huntera4eb24a2013-12-06 09:42:56 +02001816 u64 addr = 0;
1817
Namhyung Kim47260642012-05-31 14:43:26 +09001818 node = callchain_cursor_current(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001819 if (!node)
1820 break;
1821
David Ahernd2ff1b12013-11-18 13:32:44 -07001822 if (node->sym && node->sym->ignore)
1823 goto next;
1824
David Aherna6ffaf92013-08-07 22:50:51 -04001825 if (print_ip)
David Ahernb0b35f02013-08-07 22:50:52 -04001826 printf("%c%16" PRIx64, s, node->ip);
David Aherna6ffaf92013-08-07 22:50:51 -04001827
Adrian Huntera4eb24a2013-12-06 09:42:56 +02001828 if (node->map)
1829 addr = node->map->map_ip(node->map, node->ip);
1830
David Ahern787bef12011-05-27 14:28:43 -06001831 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001832 printf(" ");
David Ahern251f4262013-07-28 09:14:34 -06001833 if (print_symoffset) {
Adrian Huntera4eb24a2013-12-06 09:42:56 +02001834 node_al.addr = addr;
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001835 node_al.map = node->map;
1836 symbol__fprintf_symname_offs(node->sym, &node_al, stdout);
David Ahern251f4262013-07-28 09:14:34 -06001837 } else
1838 symbol__fprintf_symname(node->sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001839 }
David Ahern251f4262013-07-28 09:14:34 -06001840
David Ahern610723f2011-05-27 14:28:44 -06001841 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001842 printf(" (");
David Ahern52deff72012-05-29 22:58:26 -06001843 map__fprintf_dsoname(node->map, stdout);
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001844 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001845 }
David Ahernb0b35f02013-08-07 22:50:52 -04001846
Adrian Huntercc8fae12013-12-06 09:42:57 +02001847 if (print_srcline)
1848 map__fprintf_srcline(node->map, addr, "\n ",
1849 stdout);
1850
David Ahernb0b35f02013-08-07 22:50:52 -04001851 if (!print_oneline)
1852 printf("\n");
David Ahernc0230b22011-03-09 22:23:27 -07001853
David Ahern307cbb92013-08-07 22:50:53 -04001854 stack_depth--;
David Ahernd2ff1b12013-11-18 13:32:44 -07001855next:
1856 callchain_cursor_advance(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001857 }
1858
1859 } else {
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001860 if (al->sym && al->sym->ignore)
David Ahernd2ff1b12013-11-18 13:32:44 -07001861 return;
1862
David Aherna6ffaf92013-08-07 22:50:51 -04001863 if (print_ip)
1864 printf("%16" PRIx64, sample->ip);
1865
David Ahern787bef12011-05-27 14:28:43 -06001866 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001867 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001868 if (print_symoffset)
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001869 symbol__fprintf_symname_offs(al->sym, al,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001870 stdout);
1871 else
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001872 symbol__fprintf_symname(al->sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001873 }
1874
1875 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001876 printf(" (");
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001877 map__fprintf_dsoname(al->map, stdout);
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001878 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001879 }
Adrian Huntercc8fae12013-12-06 09:42:57 +02001880
1881 if (print_srcline)
1882 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
David Ahernc0230b22011-03-09 22:23:27 -07001883 }
1884}
Anton Blanchard5d67be92011-07-04 21:57:50 +10001885
1886int perf_session__cpu_bitmap(struct perf_session *session,
1887 const char *cpu_list, unsigned long *cpu_bitmap)
1888{
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04001889 int i, err = -1;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001890 struct cpu_map *map;
1891
1892 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1893 struct perf_evsel *evsel;
1894
1895 evsel = perf_session__find_first_evtype(session, i);
1896 if (!evsel)
1897 continue;
1898
1899 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1900 pr_err("File does not contain CPU events. "
1901 "Remove -c option to proceed.\n");
1902 return -1;
1903 }
1904 }
1905
1906 map = cpu_map__new(cpu_list);
David Ahern47fbe532011-11-13 10:45:27 -07001907 if (map == NULL) {
1908 pr_err("Invalid cpu_list\n");
1909 return -1;
1910 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001911
1912 for (i = 0; i < map->nr; i++) {
1913 int cpu = map->map[i];
1914
1915 if (cpu >= MAX_NR_CPUS) {
1916 pr_err("Requested CPU %d too large. "
1917 "Consider raising MAX_NR_CPUS\n", cpu);
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04001918 goto out_delete_map;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001919 }
1920
1921 set_bit(cpu, cpu_bitmap);
1922 }
1923
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04001924 err = 0;
1925
1926out_delete_map:
Jiri Olsaf30a79b2015-06-23 00:36:04 +02001927 cpu_map__put(map);
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04001928 return err;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001929}
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001930
1931void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1932 bool full)
1933{
1934 struct stat st;
Masanari Iidac5765ec2014-05-15 02:13:38 +09001935 int fd, ret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001936
1937 if (session == NULL || fp == NULL)
1938 return;
1939
Masanari Iidac5765ec2014-05-15 02:13:38 +09001940 fd = perf_data_file__fd(session->file);
1941
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001942 ret = fstat(fd, &st);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001943 if (ret == -1)
1944 return;
1945
1946 fprintf(fp, "# ========\n");
1947 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1948 perf_header__fprintf_info(session, fp, full);
1949 fprintf(fp, "# ========\n#\n");
1950}
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001951
1952
1953int __perf_session__set_tracepoints_handlers(struct perf_session *session,
1954 const struct perf_evsel_str_handler *assocs,
1955 size_t nr_assocs)
1956{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001957 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001958 size_t i;
1959 int err;
1960
1961 for (i = 0; i < nr_assocs; i++) {
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001962 /*
1963 * Adding a handler for an event not in the session,
1964 * just ignore it.
1965 */
1966 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name);
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001967 if (evsel == NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001968 continue;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001969
1970 err = -EEXIST;
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001971 if (evsel->handler != NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001972 goto out;
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001973 evsel->handler = assocs[i].handler;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001974 }
1975
1976 err = 0;
1977out:
1978 return err;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001979}
Adrian Hunter3c659ee2014-10-27 15:49:22 +02001980
1981int perf_event__process_id_index(struct perf_tool *tool __maybe_unused,
1982 union perf_event *event,
1983 struct perf_session *session)
1984{
1985 struct perf_evlist *evlist = session->evlist;
1986 struct id_index_event *ie = &event->id_index;
1987 size_t i, nr, max_nr;
1988
1989 max_nr = (ie->header.size - sizeof(struct id_index_event)) /
1990 sizeof(struct id_index_entry);
1991 nr = ie->nr;
1992 if (nr > max_nr)
1993 return -EINVAL;
1994
1995 if (dump_trace)
1996 fprintf(stdout, " nr: %zu\n", nr);
1997
1998 for (i = 0; i < nr; i++) {
1999 struct id_index_entry *e = &ie->entries[i];
2000 struct perf_sample_id *sid;
2001
2002 if (dump_trace) {
2003 fprintf(stdout, " ... id: %"PRIu64, e->id);
2004 fprintf(stdout, " idx: %"PRIu64, e->idx);
2005 fprintf(stdout, " cpu: %"PRId64, e->cpu);
2006 fprintf(stdout, " tid: %"PRId64"\n", e->tid);
2007 }
2008
2009 sid = perf_evlist__id2sid(evlist, e->id);
2010 if (!sid)
2011 return -ENOENT;
2012 sid->idx = e->idx;
2013 sid->cpu = e->cpu;
2014 sid->tid = e->tid;
2015 }
2016 return 0;
2017}
2018
2019int perf_event__synthesize_id_index(struct perf_tool *tool,
2020 perf_event__handler_t process,
2021 struct perf_evlist *evlist,
2022 struct machine *machine)
2023{
2024 union perf_event *ev;
2025 struct perf_evsel *evsel;
2026 size_t nr = 0, i = 0, sz, max_nr, n;
2027 int err;
2028
2029 pr_debug2("Synthesizing id index\n");
2030
2031 max_nr = (UINT16_MAX - sizeof(struct id_index_event)) /
2032 sizeof(struct id_index_entry);
2033
Arnaldo Carvalho de Melocba9b842014-10-29 11:31:54 -02002034 evlist__for_each(evlist, evsel)
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002035 nr += evsel->ids;
2036
2037 n = nr > max_nr ? max_nr : nr;
2038 sz = sizeof(struct id_index_event) + n * sizeof(struct id_index_entry);
2039 ev = zalloc(sz);
2040 if (!ev)
2041 return -ENOMEM;
2042
2043 ev->id_index.header.type = PERF_RECORD_ID_INDEX;
2044 ev->id_index.header.size = sz;
2045 ev->id_index.nr = n;
2046
Arnaldo Carvalho de Melocba9b842014-10-29 11:31:54 -02002047 evlist__for_each(evlist, evsel) {
Adrian Hunter3c659ee2014-10-27 15:49:22 +02002048 u32 j;
2049
2050 for (j = 0; j < evsel->ids; j++) {
2051 struct id_index_entry *e;
2052 struct perf_sample_id *sid;
2053
2054 if (i >= n) {
2055 err = process(tool, ev, NULL, machine);
2056 if (err)
2057 goto out_err;
2058 nr -= n;
2059 i = 0;
2060 }
2061
2062 e = &ev->id_index.entries[i++];
2063
2064 e->id = evsel->id[j];
2065
2066 sid = perf_evlist__id2sid(evlist, e->id);
2067 if (!sid) {
2068 free(ev);
2069 return -ENOENT;
2070 }
2071
2072 e->idx = sid->idx;
2073 e->cpu = sid->cpu;
2074 e->tid = sid->tid;
2075 }
2076 }
2077
2078 sz = sizeof(struct id_index_event) + nr * sizeof(struct id_index_entry);
2079 ev->id_index.header.size = sz;
2080 ev->id_index.nr = nr;
2081
2082 err = process(tool, ev, NULL, machine);
2083out_err:
2084 free(ev);
2085
2086 return err;
2087}