blob: 0b4646cd5245790135a3c07ec2fc02dcad4f1505 [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"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020019
Adrian Hunterc4468702015-04-09 18:53:48 +030020static int perf_session__deliver_event(struct perf_session *session,
21 union perf_event *event,
22 struct perf_sample *sample,
23 struct perf_tool *tool,
24 u64 file_offset);
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -030025
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030026static int perf_session__open(struct perf_session *session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020027{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030028 struct perf_data_file *file = session->file;
Tom Zanussi8dc58102010-04-01 23:59:15 -050029
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030030 if (perf_session__read_header(session) < 0) {
Stephane Eranian69996df2012-02-09 23:21:06 +010031 pr_err("incompatible file format (rerun with -v to learn more)");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020032 return -1;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020033 }
34
Jiri Olsacc9784bd2013-10-15 16:27:34 +020035 if (perf_data_file__is_pipe(file))
36 return 0;
37
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030038 if (!perf_evlist__valid_sample_type(session->evlist)) {
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030039 pr_err("non matching sample_type");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020040 return -1;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030041 }
42
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030043 if (!perf_evlist__valid_sample_id_all(session->evlist)) {
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030044 pr_err("non matching sample_id_all");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020045 return -1;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030046 }
47
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030048 if (!perf_evlist__valid_read_format(session->evlist)) {
Jiri Olsa9ede4732012-10-10 17:38:13 +020049 pr_err("non matching read_format");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020050 return -1;
Jiri Olsa9ede4732012-10-10 17:38:13 +020051 }
52
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020053 return 0;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020054}
55
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030056void perf_session__set_id_hdr_size(struct perf_session *session)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020057{
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030058 u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist);
59
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030060 machines__set_id_hdr_size(&session->machines, id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020061}
62
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030063int perf_session__create_kernel_maps(struct perf_session *session)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080064{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030065 int ret = machine__create_kernel_maps(&session->machines.host);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080066
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080067 if (ret >= 0)
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030068 ret = machines__create_guest_kernel_maps(&session->machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080069 return ret;
70}
71
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030072static void perf_session__destroy_kernel_maps(struct perf_session *session)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -030073{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030074 machines__destroy_kernel_maps(&session->machines);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -030075}
76
Adrian Huntercfe1c412014-07-31 09:00:45 +030077static bool perf_session__has_comm_exec(struct perf_session *session)
78{
79 struct perf_evsel *evsel;
80
81 evlist__for_each(session->evlist, evsel) {
82 if (evsel->attr.comm_exec)
83 return true;
84 }
85
86 return false;
87}
88
89static void perf_session__set_comm_exec(struct perf_session *session)
90{
91 bool comm_exec = perf_session__has_comm_exec(session);
92
93 machines__set_comm_exec(&session->machines, comm_exec);
94}
95
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -030096static int ordered_events__deliver_event(struct ordered_events *oe,
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -030097 struct ordered_event *event)
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -030098{
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -030099 struct perf_sample sample;
100 struct perf_session *session = container_of(oe, struct perf_session,
101 ordered_events);
102 int ret = perf_evlist__parse_sample(session->evlist, event->event, &sample);
103
104 if (ret) {
105 pr_err("Can't parse sample, err = %d\n", ret);
106 return ret;
107 }
108
Adrian Hunterc4468702015-04-09 18:53:48 +0300109 return perf_session__deliver_event(session, event->event, &sample,
110 session->tool, event->file_offset);
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300111}
112
Jiri Olsaf5fc14122013-10-15 16:27:32 +0200113struct perf_session *perf_session__new(struct perf_data_file *file,
114 bool repipe, struct perf_tool *tool)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200115{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300116 struct perf_session *session = zalloc(sizeof(*session));
Robert Richterefad1412011-12-07 10:02:54 +0100117
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300118 if (!session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200119 goto out;
120
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300121 session->repipe = repipe;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300122 session->tool = tool;
Adrian Hunter99fa2982015-04-30 17:37:25 +0300123 INIT_LIST_HEAD(&session->auxtrace_index);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300124 machines__init(&session->machines);
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300125 ordered_events__init(&session->ordered_events, ordered_events__deliver_event);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200126
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200127 if (file) {
128 if (perf_data_file__open(file))
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200129 goto out_delete;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200130
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300131 session->file = file;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200132
133 if (perf_data_file__is_read(file)) {
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300134 if (perf_session__open(session) < 0)
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200135 goto out_close;
136
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300137 perf_session__set_id_hdr_size(session);
Adrian Huntercfe1c412014-07-31 09:00:45 +0300138 perf_session__set_comm_exec(session);
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200139 }
140 }
141
142 if (!file || perf_data_file__is_write(file)) {
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200143 /*
144 * In O_RDONLY mode this will be performed when reading the
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200145 * kernel MMAP event, in perf_event__process_mmap().
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200146 */
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300147 if (perf_session__create_kernel_maps(session) < 0)
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700148 pr_warning("Cannot read kernel map\n");
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200149 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200150
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200151 if (tool && tool->ordering_requires_timestamps &&
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200152 tool->ordered_events && !perf_evlist__sample_id_all(session->evlist)) {
Ian Munsie21ef97f2010-12-10 14:09:16 +1100153 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200154 tool->ordered_events = false;
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300155 }
Ian Munsie21ef97f2010-12-10 14:09:16 +1100156
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300157 return session;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200158
159 out_close:
160 perf_data_file__close(file);
161 out_delete:
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300162 perf_session__delete(session);
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200163 out:
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200164 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200165}
166
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200167static void perf_session__delete_threads(struct perf_session *session)
168{
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300169 machine__delete_threads(&session->machines.host);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200170}
171
Namhyung Kim03cd2092012-11-21 13:43:19 +0900172static void perf_session_env__delete(struct perf_session_env *env)
173{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300174 zfree(&env->hostname);
175 zfree(&env->os_release);
176 zfree(&env->version);
177 zfree(&env->arch);
178 zfree(&env->cpu_desc);
179 zfree(&env->cpuid);
Namhyung Kim03cd2092012-11-21 13:43:19 +0900180
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300181 zfree(&env->cmdline);
182 zfree(&env->sibling_cores);
183 zfree(&env->sibling_threads);
184 zfree(&env->numa_nodes);
185 zfree(&env->pmu_mappings);
Namhyung Kim03cd2092012-11-21 13:43:19 +0900186}
187
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300188void perf_session__delete(struct perf_session *session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200189{
Adrian Hunterc4468702015-04-09 18:53:48 +0300190 auxtrace__free(session);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300191 auxtrace_index__free(&session->auxtrace_index);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300192 perf_session__destroy_kernel_maps(session);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300193 perf_session__delete_threads(session);
194 perf_session_env__delete(&session->header.env);
195 machines__exit(&session->machines);
196 if (session->file)
197 perf_data_file__close(session->file);
198 free(session);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200199}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200200
Adrian Hunter47c3d102013-07-04 16:20:21 +0300201static int process_event_synth_tracing_data_stub(struct perf_tool *tool
202 __maybe_unused,
203 union perf_event *event
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300204 __maybe_unused,
205 struct perf_session *session
206 __maybe_unused)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200207{
208 dump_printf(": unhandled!\n");
209 return 0;
210}
211
Adrian Hunter47c3d102013-07-04 16:20:21 +0300212static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused,
213 union perf_event *event __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300214 struct perf_evlist **pevlist
215 __maybe_unused)
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200216{
217 dump_printf(": unhandled!\n");
218 return 0;
219}
220
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300221static int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
222 union perf_event *event __maybe_unused,
223 struct perf_sample *sample __maybe_unused,
224 struct perf_evsel *evsel __maybe_unused,
225 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300226{
227 dump_printf(": unhandled!\n");
228 return 0;
229}
230
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300231static int process_event_stub(struct perf_tool *tool __maybe_unused,
232 union perf_event *event __maybe_unused,
233 struct perf_sample *sample __maybe_unused,
234 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200235{
236 dump_printf(": unhandled!\n");
237 return 0;
238}
239
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300240static int process_build_id_stub(struct perf_tool *tool __maybe_unused,
241 union perf_event *event __maybe_unused,
242 struct perf_session *session __maybe_unused)
243{
244 dump_printf(": unhandled!\n");
245 return 0;
246}
247
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300248static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
249 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300250 struct ordered_events *oe __maybe_unused)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200251{
252 dump_printf(": unhandled!\n");
253 return 0;
254}
255
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200256static int process_finished_round(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200257 union perf_event *event,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300258 struct ordered_events *oe);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200259
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200260static int process_id_index_stub(struct perf_tool *tool __maybe_unused,
261 union perf_event *event __maybe_unused,
262 struct perf_session *perf_session
263 __maybe_unused)
264{
265 dump_printf(": unhandled!\n");
266 return 0;
267}
268
Adrian Huntera16ac022015-04-09 18:53:43 +0300269static int process_event_auxtrace_info_stub(struct perf_tool *tool __maybe_unused,
270 union perf_event *event __maybe_unused,
271 struct perf_session *session __maybe_unused)
272{
273 dump_printf(": unhandled!\n");
274 return 0;
275}
276
277static int skipn(int fd, off_t n)
278{
279 char buf[4096];
280 ssize_t ret;
281
282 while (n > 0) {
283 ret = read(fd, buf, min(n, (off_t)sizeof(buf)));
284 if (ret <= 0)
285 return ret;
286 n -= ret;
287 }
288
289 return 0;
290}
291
292static s64 process_event_auxtrace_stub(struct perf_tool *tool __maybe_unused,
293 union perf_event *event,
294 struct perf_session *session
295 __maybe_unused)
296{
297 dump_printf(": unhandled!\n");
298 if (perf_data_file__is_pipe(session->file))
299 skipn(perf_data_file__fd(session->file), event->auxtrace.size);
300 return event->auxtrace.size;
301}
302
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300303static
304int process_event_auxtrace_error_stub(struct perf_tool *tool __maybe_unused,
305 union perf_event *event __maybe_unused,
306 struct perf_session *session __maybe_unused)
307{
308 dump_printf(": unhandled!\n");
309 return 0;
310}
311
David Ahern9c501402013-08-02 14:05:41 -0600312void perf_tool__fill_defaults(struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200313{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200314 if (tool->sample == NULL)
315 tool->sample = process_event_sample_stub;
316 if (tool->mmap == NULL)
317 tool->mmap = process_event_stub;
David Ahern6adb0b02013-09-22 19:44:59 -0600318 if (tool->mmap2 == NULL)
319 tool->mmap2 = process_event_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200320 if (tool->comm == NULL)
321 tool->comm = process_event_stub;
322 if (tool->fork == NULL)
323 tool->fork = process_event_stub;
324 if (tool->exit == NULL)
325 tool->exit = process_event_stub;
326 if (tool->lost == NULL)
327 tool->lost = perf_event__process_lost;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300328 if (tool->aux == NULL)
329 tool->aux = perf_event__process_aux;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200330 if (tool->read == NULL)
331 tool->read = process_event_sample_stub;
332 if (tool->throttle == NULL)
333 tool->throttle = process_event_stub;
334 if (tool->unthrottle == NULL)
335 tool->unthrottle = process_event_stub;
336 if (tool->attr == NULL)
337 tool->attr = process_event_synth_attr_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200338 if (tool->tracing_data == NULL)
339 tool->tracing_data = process_event_synth_tracing_data_stub;
340 if (tool->build_id == NULL)
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300341 tool->build_id = process_build_id_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200342 if (tool->finished_round == NULL) {
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200343 if (tool->ordered_events)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200344 tool->finished_round = process_finished_round;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200345 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200346 tool->finished_round = process_finished_round_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200347 }
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200348 if (tool->id_index == NULL)
349 tool->id_index = process_id_index_stub;
Adrian Huntera16ac022015-04-09 18:53:43 +0300350 if (tool->auxtrace_info == NULL)
351 tool->auxtrace_info = process_event_auxtrace_info_stub;
352 if (tool->auxtrace == NULL)
353 tool->auxtrace = process_event_auxtrace_stub;
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300354 if (tool->auxtrace_error == NULL)
355 tool->auxtrace_error = process_event_auxtrace_error_stub;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200356}
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300357
Jiri Olsa268fb202012-05-30 14:23:43 +0200358static void swap_sample_id_all(union perf_event *event, void *data)
359{
360 void *end = (void *) event + event->header.size;
361 int size = end - data;
362
363 BUG_ON(size % sizeof(u64));
364 mem_bswap_64(data, size);
365}
366
367static void perf_event__all64_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300368 bool sample_id_all __maybe_unused)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200369{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200370 struct perf_event_header *hdr = &event->header;
371 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200372}
373
Jiri Olsa268fb202012-05-30 14:23:43 +0200374static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200375{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200376 event->comm.pid = bswap_32(event->comm.pid);
377 event->comm.tid = bswap_32(event->comm.tid);
Jiri Olsa268fb202012-05-30 14:23:43 +0200378
379 if (sample_id_all) {
380 void *data = &event->comm.comm;
381
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300382 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200383 swap_sample_id_all(event, data);
384 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200385}
386
Jiri Olsa268fb202012-05-30 14:23:43 +0200387static void perf_event__mmap_swap(union perf_event *event,
388 bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200389{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200390 event->mmap.pid = bswap_32(event->mmap.pid);
391 event->mmap.tid = bswap_32(event->mmap.tid);
392 event->mmap.start = bswap_64(event->mmap.start);
393 event->mmap.len = bswap_64(event->mmap.len);
394 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
Jiri Olsa268fb202012-05-30 14:23:43 +0200395
396 if (sample_id_all) {
397 void *data = &event->mmap.filename;
398
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300399 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200400 swap_sample_id_all(event, data);
401 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200402}
403
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200404static void perf_event__mmap2_swap(union perf_event *event,
405 bool sample_id_all)
406{
407 event->mmap2.pid = bswap_32(event->mmap2.pid);
408 event->mmap2.tid = bswap_32(event->mmap2.tid);
409 event->mmap2.start = bswap_64(event->mmap2.start);
410 event->mmap2.len = bswap_64(event->mmap2.len);
411 event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
412 event->mmap2.maj = bswap_32(event->mmap2.maj);
413 event->mmap2.min = bswap_32(event->mmap2.min);
414 event->mmap2.ino = bswap_64(event->mmap2.ino);
415
416 if (sample_id_all) {
417 void *data = &event->mmap2.filename;
418
419 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
420 swap_sample_id_all(event, data);
421 }
422}
Jiri Olsa268fb202012-05-30 14:23:43 +0200423static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200424{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200425 event->fork.pid = bswap_32(event->fork.pid);
426 event->fork.tid = bswap_32(event->fork.tid);
427 event->fork.ppid = bswap_32(event->fork.ppid);
428 event->fork.ptid = bswap_32(event->fork.ptid);
429 event->fork.time = bswap_64(event->fork.time);
Jiri Olsa268fb202012-05-30 14:23:43 +0200430
431 if (sample_id_all)
432 swap_sample_id_all(event, &event->fork + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200433}
434
Jiri Olsa268fb202012-05-30 14:23:43 +0200435static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200436{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200437 event->read.pid = bswap_32(event->read.pid);
438 event->read.tid = bswap_32(event->read.tid);
439 event->read.value = bswap_64(event->read.value);
440 event->read.time_enabled = bswap_64(event->read.time_enabled);
441 event->read.time_running = bswap_64(event->read.time_running);
442 event->read.id = bswap_64(event->read.id);
Jiri Olsa268fb202012-05-30 14:23:43 +0200443
444 if (sample_id_all)
445 swap_sample_id_all(event, &event->read + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200446}
447
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300448static void perf_event__aux_swap(union perf_event *event, bool sample_id_all)
449{
450 event->aux.aux_offset = bswap_64(event->aux.aux_offset);
451 event->aux.aux_size = bswap_64(event->aux.aux_size);
452 event->aux.flags = bswap_64(event->aux.flags);
453
454 if (sample_id_all)
455 swap_sample_id_all(event, &event->aux + 1);
456}
457
Jiri Olsadd96c462013-09-01 12:36:15 +0200458static void perf_event__throttle_swap(union perf_event *event,
459 bool sample_id_all)
460{
461 event->throttle.time = bswap_64(event->throttle.time);
462 event->throttle.id = bswap_64(event->throttle.id);
463 event->throttle.stream_id = bswap_64(event->throttle.stream_id);
464
465 if (sample_id_all)
466 swap_sample_id_all(event, &event->throttle + 1);
467}
468
Jiri Olsae108c662012-05-16 08:59:03 +0200469static u8 revbyte(u8 b)
470{
471 int rev = (b >> 4) | ((b & 0xf) << 4);
472 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
473 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
474 return (u8) rev;
475}
476
477/*
478 * XXX this is hack in attempt to carry flags bitfield
479 * throught endian village. ABI says:
480 *
481 * Bit-fields are allocated from right to left (least to most significant)
482 * on little-endian implementations and from left to right (most to least
483 * significant) on big-endian implementations.
484 *
485 * The above seems to be byte specific, so we need to reverse each
486 * byte of the bitfield. 'Internet' also says this might be implementation
487 * specific and we probably need proper fix and carry perf_event_attr
488 * bitfield flags in separate data file FEAT_ section. Thought this seems
489 * to work for now.
490 */
491static void swap_bitfield(u8 *p, unsigned len)
492{
493 unsigned i;
494
495 for (i = 0; i < len; i++) {
496 *p = revbyte(*p);
497 p++;
498 }
499}
500
David Aherneda39132011-07-15 12:34:09 -0600501/* exported for swapping attributes in file header */
502void perf_event__attr_swap(struct perf_event_attr *attr)
503{
504 attr->type = bswap_32(attr->type);
505 attr->size = bswap_32(attr->size);
506 attr->config = bswap_64(attr->config);
507 attr->sample_period = bswap_64(attr->sample_period);
508 attr->sample_type = bswap_64(attr->sample_type);
509 attr->read_format = bswap_64(attr->read_format);
510 attr->wakeup_events = bswap_32(attr->wakeup_events);
511 attr->bp_type = bswap_32(attr->bp_type);
512 attr->bp_addr = bswap_64(attr->bp_addr);
513 attr->bp_len = bswap_64(attr->bp_len);
Adrian Hunter7db59522013-10-18 15:29:03 +0300514 attr->branch_sample_type = bswap_64(attr->branch_sample_type);
515 attr->sample_regs_user = bswap_64(attr->sample_regs_user);
516 attr->sample_stack_user = bswap_32(attr->sample_stack_user);
Adrian Hunter70d73de2015-04-09 18:54:06 +0300517 attr->aux_watermark = bswap_32(attr->aux_watermark);
Jiri Olsae108c662012-05-16 08:59:03 +0200518
519 swap_bitfield((u8 *) (&attr->read_format + 1), sizeof(u64));
David Aherneda39132011-07-15 12:34:09 -0600520}
521
Jiri Olsa268fb202012-05-30 14:23:43 +0200522static void perf_event__hdr_attr_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300523 bool sample_id_all __maybe_unused)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500524{
525 size_t size;
526
David Aherneda39132011-07-15 12:34:09 -0600527 perf_event__attr_swap(&event->attr.attr);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500528
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200529 size = event->header.size;
530 size -= (void *)&event->attr.id - (void *)event;
531 mem_bswap_64(event->attr.id, size);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500532}
533
Jiri Olsa268fb202012-05-30 14:23:43 +0200534static void perf_event__event_type_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300535 bool sample_id_all __maybe_unused)
Tom Zanussicd19a032010-04-01 23:59:20 -0500536{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200537 event->event_type.event_type.event_id =
538 bswap_64(event->event_type.event_type.event_id);
Tom Zanussicd19a032010-04-01 23:59:20 -0500539}
540
Jiri Olsa268fb202012-05-30 14:23:43 +0200541static void perf_event__tracing_data_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300542 bool sample_id_all __maybe_unused)
Tom Zanussi92155452010-04-01 23:59:21 -0500543{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200544 event->tracing_data.size = bswap_32(event->tracing_data.size);
Tom Zanussi92155452010-04-01 23:59:21 -0500545}
546
Adrian Huntera16ac022015-04-09 18:53:43 +0300547static void perf_event__auxtrace_info_swap(union perf_event *event,
548 bool sample_id_all __maybe_unused)
549{
550 size_t size;
551
552 event->auxtrace_info.type = bswap_32(event->auxtrace_info.type);
553
554 size = event->header.size;
555 size -= (void *)&event->auxtrace_info.priv - (void *)event;
556 mem_bswap_64(event->auxtrace_info.priv, size);
557}
558
559static void perf_event__auxtrace_swap(union perf_event *event,
560 bool sample_id_all __maybe_unused)
561{
562 event->auxtrace.size = bswap_64(event->auxtrace.size);
563 event->auxtrace.offset = bswap_64(event->auxtrace.offset);
564 event->auxtrace.reference = bswap_64(event->auxtrace.reference);
565 event->auxtrace.idx = bswap_32(event->auxtrace.idx);
566 event->auxtrace.tid = bswap_32(event->auxtrace.tid);
567 event->auxtrace.cpu = bswap_32(event->auxtrace.cpu);
568}
569
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300570static void perf_event__auxtrace_error_swap(union perf_event *event,
571 bool sample_id_all __maybe_unused)
572{
573 event->auxtrace_error.type = bswap_32(event->auxtrace_error.type);
574 event->auxtrace_error.code = bswap_32(event->auxtrace_error.code);
575 event->auxtrace_error.cpu = bswap_32(event->auxtrace_error.cpu);
576 event->auxtrace_error.pid = bswap_32(event->auxtrace_error.pid);
577 event->auxtrace_error.tid = bswap_32(event->auxtrace_error.tid);
578 event->auxtrace_error.ip = bswap_64(event->auxtrace_error.ip);
579}
580
Jiri Olsa268fb202012-05-30 14:23:43 +0200581typedef void (*perf_event__swap_op)(union perf_event *event,
582 bool sample_id_all);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200583
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200584static perf_event__swap_op perf_event__swap_ops[] = {
585 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200586 [PERF_RECORD_MMAP2] = perf_event__mmap2_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200587 [PERF_RECORD_COMM] = perf_event__comm_swap,
588 [PERF_RECORD_FORK] = perf_event__task_swap,
589 [PERF_RECORD_EXIT] = perf_event__task_swap,
590 [PERF_RECORD_LOST] = perf_event__all64_swap,
591 [PERF_RECORD_READ] = perf_event__read_swap,
Jiri Olsadd96c462013-09-01 12:36:15 +0200592 [PERF_RECORD_THROTTLE] = perf_event__throttle_swap,
593 [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200594 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300595 [PERF_RECORD_AUX] = perf_event__aux_swap,
David Aherneda39132011-07-15 12:34:09 -0600596 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200597 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
598 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
599 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200600 [PERF_RECORD_ID_INDEX] = perf_event__all64_swap,
Adrian Huntera16ac022015-04-09 18:53:43 +0300601 [PERF_RECORD_AUXTRACE_INFO] = perf_event__auxtrace_info_swap,
602 [PERF_RECORD_AUXTRACE] = perf_event__auxtrace_swap,
Adrian Huntere9bf54d2015-04-09 18:53:47 +0300603 [PERF_RECORD_AUXTRACE_ERROR] = perf_event__auxtrace_error_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200604 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200605};
606
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200607/*
608 * When perf record finishes a pass on every buffers, it records this pseudo
609 * event.
610 * We record the max timestamp t found in the pass n.
611 * Assuming these timestamps are monotonic across cpus, we know that if
612 * a buffer still has events with timestamps below t, they will be all
613 * available and then read in the pass n + 1.
614 * Hence when we start to read the pass n + 2, we can safely flush every
615 * events with timestamps below t.
616 *
617 * ============ PASS n =================
618 * CPU 0 | CPU 1
619 * |
620 * cnt1 timestamps | cnt2 timestamps
621 * 1 | 2
622 * 2 | 3
623 * - | 4 <--- max recorded
624 *
625 * ============ PASS n + 1 ==============
626 * CPU 0 | CPU 1
627 * |
628 * cnt1 timestamps | cnt2 timestamps
629 * 3 | 5
630 * 4 | 6
631 * 5 | 7 <---- max recorded
632 *
633 * Flush every events below timestamp 4
634 *
635 * ============ PASS n + 2 ==============
636 * CPU 0 | CPU 1
637 * |
638 * cnt1 timestamps | cnt2 timestamps
639 * 6 | 8
640 * 7 | 9
641 * - | 10
642 *
643 * Flush every events below timestamp 7
644 * etc...
645 */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300646static int process_finished_round(struct perf_tool *tool __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300647 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300648 struct ordered_events *oe)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200649{
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300650 return ordered_events__flush(oe, OE_FLUSH__ROUND);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200651}
652
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300653int perf_session__queue_event(struct perf_session *s, union perf_event *event,
654 struct perf_sample *sample, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200655{
Arnaldo Carvalho de Melo4a6b3622015-03-03 13:02:24 -0300656 return ordered_events__queue(&s->ordered_events, event, sample, file_offset);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200657}
658
Kan Liang384b6052015-01-05 13:23:05 -0500659static void callchain__lbr_callstack_printf(struct perf_sample *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200660{
Kan Liang384b6052015-01-05 13:23:05 -0500661 struct ip_callchain *callchain = sample->callchain;
662 struct branch_stack *lbr_stack = sample->branch_stack;
663 u64 kernel_callchain_nr = callchain->nr;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200664 unsigned int i;
665
Kan Liang384b6052015-01-05 13:23:05 -0500666 for (i = 0; i < kernel_callchain_nr; i++) {
667 if (callchain->ips[i] == PERF_CONTEXT_USER)
668 break;
669 }
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200670
Kan Liang384b6052015-01-05 13:23:05 -0500671 if ((i != kernel_callchain_nr) && lbr_stack->nr) {
672 u64 total_nr;
673 /*
674 * LBR callstack can only get user call chain,
675 * i is kernel call chain number,
676 * 1 is PERF_CONTEXT_USER.
677 *
678 * The user call chain is stored in LBR registers.
679 * LBR are pair registers. The caller is stored
680 * in "from" register, while the callee is stored
681 * in "to" register.
682 * For example, there is a call stack
683 * "A"->"B"->"C"->"D".
684 * The LBR registers will recorde like
685 * "C"->"D", "B"->"C", "A"->"B".
686 * So only the first "to" register and all "from"
687 * registers are needed to construct the whole stack.
688 */
689 total_nr = i + 1 + lbr_stack->nr + 1;
690 kernel_callchain_nr = i + 1;
691
692 printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr);
693
694 for (i = 0; i < kernel_callchain_nr; i++)
695 printf("..... %2d: %016" PRIx64 "\n",
696 i, callchain->ips[i]);
697
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200698 printf("..... %2d: %016" PRIx64 "\n",
Kan Liang384b6052015-01-05 13:23:05 -0500699 (int)(kernel_callchain_nr), lbr_stack->entries[0].to);
700 for (i = 0; i < lbr_stack->nr; i++)
701 printf("..... %2d: %016" PRIx64 "\n",
702 (int)(i + kernel_callchain_nr + 1), lbr_stack->entries[i].from);
703 }
704}
705
706static void callchain__printf(struct perf_evsel *evsel,
707 struct perf_sample *sample)
708{
709 unsigned int i;
710 struct ip_callchain *callchain = sample->callchain;
711
712 if (has_branch_callstack(evsel))
713 callchain__lbr_callstack_printf(sample);
714
715 printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr);
716
717 for (i = 0; i < callchain->nr; i++)
718 printf("..... %2d: %016" PRIx64 "\n",
719 i, callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200720}
721
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100722static void branch_stack__printf(struct perf_sample *sample)
723{
724 uint64_t i;
725
726 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
727
728 for (i = 0; i < sample->branch_stack->nr; i++)
729 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n",
730 i, sample->branch_stack->entries[i].from,
731 sample->branch_stack->entries[i].to);
732}
733
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200734static void regs_dump__printf(u64 mask, u64 *regs)
735{
736 unsigned rid, i = 0;
737
738 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
739 u64 val = regs[i++];
740
741 printf(".... %-5s 0x%" PRIx64 "\n",
742 perf_reg_name(rid), val);
743 }
744}
745
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200746static const char *regs_abi[] = {
747 [PERF_SAMPLE_REGS_ABI_NONE] = "none",
748 [PERF_SAMPLE_REGS_ABI_32] = "32-bit",
749 [PERF_SAMPLE_REGS_ABI_64] = "64-bit",
750};
751
752static inline const char *regs_dump_abi(struct regs_dump *d)
753{
754 if (d->abi > PERF_SAMPLE_REGS_ABI_64)
755 return "unknown";
756
757 return regs_abi[d->abi];
758}
759
760static void regs__printf(const char *type, struct regs_dump *regs)
761{
762 u64 mask = regs->mask;
763
764 printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n",
765 type,
766 mask,
767 regs_dump_abi(regs));
768
769 regs_dump__printf(mask, regs->regs);
770}
771
Jiri Olsa352ea452014-01-07 13:47:25 +0100772static void regs_user__printf(struct perf_sample *sample)
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200773{
774 struct regs_dump *user_regs = &sample->user_regs;
775
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200776 if (user_regs->regs)
777 regs__printf("user", user_regs);
778}
779
780static void regs_intr__printf(struct perf_sample *sample)
781{
782 struct regs_dump *intr_regs = &sample->intr_regs;
783
784 if (intr_regs->regs)
785 regs__printf("intr", intr_regs);
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200786}
787
788static void stack_user__printf(struct stack_dump *dump)
789{
790 printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
791 dump->size, dump->offset);
792}
793
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300794static void perf_evlist__print_tstamp(struct perf_evlist *evlist,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200795 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200796 struct perf_sample *sample)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200797{
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300798 u64 sample_type = __perf_evlist__combined_sample_type(evlist);
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300799
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200800 if (event->header.type != PERF_RECORD_SAMPLE &&
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300801 !perf_evlist__sample_id_all(evlist)) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200802 fputs("-1 -1 ", stdout);
803 return;
804 }
805
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300806 if ((sample_type & PERF_SAMPLE_CPU))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200807 printf("%u ", sample->cpu);
808
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300809 if (sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200810 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200811}
812
Jiri Olsa9ede4732012-10-10 17:38:13 +0200813static void sample_read__printf(struct perf_sample *sample, u64 read_format)
814{
815 printf("... sample_read:\n");
816
817 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
818 printf("...... time enabled %016" PRIx64 "\n",
819 sample->read.time_enabled);
820
821 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
822 printf("...... time running %016" PRIx64 "\n",
823 sample->read.time_running);
824
825 if (read_format & PERF_FORMAT_GROUP) {
826 u64 i;
827
828 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
829
830 for (i = 0; i < sample->read.group.nr; i++) {
831 struct sample_read_value *value;
832
833 value = &sample->read.group.values[i];
834 printf("..... id %016" PRIx64
835 ", value %016" PRIx64 "\n",
836 value->id, value->value);
837 }
838 } else
839 printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
840 sample->read.one.id, sample->read.one.value);
841}
842
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300843static void dump_event(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200844 u64 file_offset, struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000845{
846 if (!dump_trace)
847 return;
848
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200849 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
850 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000851
852 trace_event(event);
853
854 if (sample)
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -0300855 perf_evlist__print_tstamp(evlist, event, sample);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000856
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200857 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200858 event->header.size, perf_event__name(event->header.type));
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000859}
860
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200861static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200862 struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000863{
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300864 u64 sample_type;
865
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200866 if (!dump_trace)
867 return;
868
Don Zickus0ea590a2014-02-25 22:43:46 -0500869 printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200870 event->header.misc, sample->pid, sample->tid, sample->ip,
David Ahern7cec0922011-05-30 13:08:23 -0600871 sample->period, sample->addr);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000872
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200873 sample_type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300874
875 if (sample_type & PERF_SAMPLE_CALLCHAIN)
Kan Liang384b6052015-01-05 13:23:05 -0500876 callchain__printf(evsel, sample);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100877
Kan Liang384b6052015-01-05 13:23:05 -0500878 if ((sample_type & PERF_SAMPLE_BRANCH_STACK) && !has_branch_callstack(evsel))
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100879 branch_stack__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200880
881 if (sample_type & PERF_SAMPLE_REGS_USER)
Jiri Olsa352ea452014-01-07 13:47:25 +0100882 regs_user__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200883
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200884 if (sample_type & PERF_SAMPLE_REGS_INTR)
885 regs_intr__printf(sample);
886
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200887 if (sample_type & PERF_SAMPLE_STACK_USER)
888 stack_user__printf(&sample->user_stack);
Andi Kleen05484292013-01-24 16:10:29 +0100889
890 if (sample_type & PERF_SAMPLE_WEIGHT)
891 printf("... weight: %" PRIu64 "\n", sample->weight);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100892
893 if (sample_type & PERF_SAMPLE_DATA_SRC)
894 printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
Jiri Olsa9ede4732012-10-10 17:38:13 +0200895
Andi Kleen475eeab2013-09-20 07:40:43 -0700896 if (sample_type & PERF_SAMPLE_TRANSACTION)
897 printf("... transaction: %" PRIx64 "\n", sample->transaction);
898
Jiri Olsa9ede4732012-10-10 17:38:13 +0200899 if (sample_type & PERF_SAMPLE_READ)
900 sample_read__printf(sample, evsel->attr.read_format);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000901}
902
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -0300903static struct machine *machines__find_for_cpumode(struct machines *machines,
Adrian Hunteref893252013-08-27 11:23:06 +0300904 union perf_event *event,
905 struct perf_sample *sample)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200906{
907 const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Dongsheng Yangad85ace2013-12-20 13:41:47 -0500908 struct machine *machine;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200909
David Ahern7c0f4a42012-07-20 17:25:48 -0600910 if (perf_guest &&
911 ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
912 (cpumode == PERF_RECORD_MISC_GUEST_USER))) {
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530913 u32 pid;
914
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200915 if (event->header.type == PERF_RECORD_MMAP
916 || event->header.type == PERF_RECORD_MMAP2)
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530917 pid = event->mmap.pid;
918 else
Adrian Hunteref893252013-08-27 11:23:06 +0300919 pid = sample->pid;
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530920
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -0300921 machine = machines__find(machines, pid);
Dongsheng Yangad85ace2013-12-20 13:41:47 -0500922 if (!machine)
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -0300923 machine = machines__find(machines, DEFAULT_GUEST_KERNEL_ID);
Dongsheng Yangad85ace2013-12-20 13:41:47 -0500924 return machine;
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530925 }
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200926
Arnaldo Carvalho de Melo54245fd2015-02-14 14:26:15 -0300927 return &machines->host;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200928}
929
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -0300930static int deliver_sample_value(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +0200931 struct perf_tool *tool,
932 union perf_event *event,
933 struct perf_sample *sample,
934 struct sample_read_value *v,
935 struct machine *machine)
936{
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -0300937 struct perf_sample_id *sid = perf_evlist__id2sid(evlist, v->id);
Jiri Olsae4caec02012-10-10 18:52:24 +0200938
Jiri Olsae4caec02012-10-10 18:52:24 +0200939 if (sid) {
940 sample->id = v->id;
941 sample->period = v->value - sid->period;
942 sid->period = v->value;
943 }
944
945 if (!sid || sid->evsel == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -0300946 ++evlist->stats.nr_unknown_id;
Jiri Olsae4caec02012-10-10 18:52:24 +0200947 return 0;
948 }
949
950 return tool->sample(tool, event, sample, sid->evsel, machine);
951}
952
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -0300953static int deliver_sample_group(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +0200954 struct perf_tool *tool,
955 union perf_event *event,
956 struct perf_sample *sample,
957 struct machine *machine)
958{
959 int ret = -EINVAL;
960 u64 i;
961
962 for (i = 0; i < sample->read.group.nr; i++) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -0300963 ret = deliver_sample_value(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +0200964 &sample->read.group.values[i],
965 machine);
966 if (ret)
967 break;
968 }
969
970 return ret;
971}
972
973static int
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -0300974 perf_evlist__deliver_sample(struct perf_evlist *evlist,
Jiri Olsae4caec02012-10-10 18:52:24 +0200975 struct perf_tool *tool,
976 union perf_event *event,
977 struct perf_sample *sample,
978 struct perf_evsel *evsel,
979 struct machine *machine)
980{
981 /* We know evsel != NULL. */
982 u64 sample_type = evsel->attr.sample_type;
983 u64 read_format = evsel->attr.read_format;
984
985 /* Standard sample delievery. */
986 if (!(sample_type & PERF_SAMPLE_READ))
987 return tool->sample(tool, event, sample, evsel, machine);
988
989 /* For PERF_SAMPLE_READ we have either single or group mode. */
990 if (read_format & PERF_FORMAT_GROUP)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -0300991 return deliver_sample_group(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +0200992 machine);
993 else
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -0300994 return deliver_sample_value(evlist, tool, event, sample,
Jiri Olsae4caec02012-10-10 18:52:24 +0200995 &sample->read.one, machine);
996}
997
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300998static int machines__deliver_event(struct machines *machines,
999 struct perf_evlist *evlist,
1000 union perf_event *event,
1001 struct perf_sample *sample,
1002 struct perf_tool *tool, u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001003{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001004 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001005 struct machine *machine;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001006
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001007 dump_event(evlist, event, file_offset, sample);
Thomas Gleixner532e7262010-12-07 12:48:55 +00001008
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001009 evsel = perf_evlist__id2evsel(evlist, sample->id);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001010
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001011 machine = machines__find_for_cpumode(machines, event, sample);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001012
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001013 switch (event->header.type) {
1014 case PERF_RECORD_SAMPLE:
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001015 dump_sample(evsel, event, sample);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001016 if (evsel == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001017 ++evlist->stats.nr_unknown_id;
Jiri Olsa67822062012-04-12 14:21:01 +02001018 return 0;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001019 }
Joerg Roedel0c095712012-02-10 18:05:04 +01001020 if (machine == NULL) {
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001021 ++evlist->stats.nr_unprocessable_samples;
Jiri Olsa67822062012-04-12 14:21:01 +02001022 return 0;
Joerg Roedel0c095712012-02-10 18:05:04 +01001023 }
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001024 return perf_evlist__deliver_sample(evlist, tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001025 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001026 return tool->mmap(tool, event, sample, machine);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001027 case PERF_RECORD_MMAP2:
1028 return tool->mmap2(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001029 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001030 return tool->comm(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001031 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001032 return tool->fork(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001033 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001034 return tool->exit(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001035 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001036 if (tool->lost == perf_event__process_lost)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001037 evlist->stats.total_lost += event->lost.lost;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001038 return tool->lost(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001039 case PERF_RECORD_READ:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001040 return tool->read(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001041 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001042 return tool->throttle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001043 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001044 return tool->unthrottle(tool, event, sample, machine);
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001045 case PERF_RECORD_AUX:
1046 return tool->aux(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001047 default:
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001048 ++evlist->stats.nr_unknown_events;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001049 return -1;
1050 }
1051}
1052
Adrian Hunterc4468702015-04-09 18:53:48 +03001053static int perf_session__deliver_event(struct perf_session *session,
1054 union perf_event *event,
1055 struct perf_sample *sample,
1056 struct perf_tool *tool,
1057 u64 file_offset)
1058{
1059 int ret;
1060
1061 ret = auxtrace__process_event(session, event, sample, tool);
1062 if (ret < 0)
1063 return ret;
1064 if (ret > 0)
1065 return 0;
1066
1067 return machines__deliver_event(&session->machines, session->evlist,
1068 event, sample, tool, file_offset);
1069}
1070
Adrian Hunterd5652d82014-07-23 22:19:58 +03001071static s64 perf_session__process_user_event(struct perf_session *session,
1072 union perf_event *event,
Adrian Hunterd5652d82014-07-23 22:19:58 +03001073 u64 file_offset)
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001074{
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -03001075 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001076 struct perf_tool *tool = session->tool;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001077 int fd = perf_data_file__fd(session->file);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001078 int err;
1079
Arnaldo Carvalho de Melo9fa87272015-02-14 15:08:51 -03001080 dump_event(session->evlist, event, file_offset, NULL);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001081
1082 /* These events are processed right away */
1083 switch (event->header.type) {
1084 case PERF_RECORD_HEADER_ATTR:
Adrian Hunter47c3d102013-07-04 16:20:21 +03001085 err = tool->attr(tool, event, &session->evlist);
Adrian Huntercfe1c412014-07-31 09:00:45 +03001086 if (err == 0) {
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -03001087 perf_session__set_id_hdr_size(session);
Adrian Huntercfe1c412014-07-31 09:00:45 +03001088 perf_session__set_comm_exec(session);
1089 }
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001090 return err;
Jiri Olsaf67697b2014-02-04 15:37:48 +01001091 case PERF_RECORD_HEADER_EVENT_TYPE:
1092 /*
1093 * Depreceated, but we need to handle it for sake
1094 * of old data files create in pipe mode.
1095 */
1096 return 0;
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001097 case PERF_RECORD_HEADER_TRACING_DATA:
1098 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001099 lseek(fd, file_offset, SEEK_SET);
Adrian Hunter47c3d102013-07-04 16:20:21 +03001100 return tool->tracing_data(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001101 case PERF_RECORD_HEADER_BUILD_ID:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001102 return tool->build_id(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001103 case PERF_RECORD_FINISHED_ROUND:
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -03001104 return tool->finished_round(tool, event, oe);
Adrian Hunter3c659ee2014-10-27 15:49:22 +02001105 case PERF_RECORD_ID_INDEX:
1106 return tool->id_index(tool, event, session);
Adrian Huntera16ac022015-04-09 18:53:43 +03001107 case PERF_RECORD_AUXTRACE_INFO:
1108 return tool->auxtrace_info(tool, event, session);
1109 case PERF_RECORD_AUXTRACE:
1110 /* setup for reading amidst mmap */
1111 lseek(fd, file_offset + event->header.size, SEEK_SET);
1112 return tool->auxtrace(tool, event, session);
Adrian Huntere9bf54d2015-04-09 18:53:47 +03001113 case PERF_RECORD_AUXTRACE_ERROR:
Adrian Hunter85ed4722015-04-09 18:53:50 +03001114 perf_session__auxtrace_error_inc(session, event);
Adrian Huntere9bf54d2015-04-09 18:53:47 +03001115 return tool->auxtrace_error(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001116 default:
1117 return -EINVAL;
1118 }
1119}
1120
Adrian Huntera2938292014-10-27 15:49:23 +02001121int perf_session__deliver_synth_event(struct perf_session *session,
1122 union perf_event *event,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001123 struct perf_sample *sample)
Adrian Huntera2938292014-10-27 15:49:23 +02001124{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001125 struct perf_evlist *evlist = session->evlist;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001126 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001127
1128 events_stats__inc(&evlist->stats, event->header.type);
Adrian Huntera2938292014-10-27 15:49:23 +02001129
1130 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001131 return perf_session__process_user_event(session, event, 0);
Adrian Huntera2938292014-10-27 15:49:23 +02001132
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001133 return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0);
Adrian Huntera2938292014-10-27 15:49:23 +02001134}
1135
Jiri Olsa268fb202012-05-30 14:23:43 +02001136static void event_swap(union perf_event *event, bool sample_id_all)
1137{
1138 perf_event__swap_op swap;
1139
1140 swap = perf_event__swap_ops[event->header.type];
1141 if (swap)
1142 swap(event, sample_id_all);
1143}
1144
Adrian Hunter5a52f332014-07-31 09:00:57 +03001145int perf_session__peek_event(struct perf_session *session, off_t file_offset,
1146 void *buf, size_t buf_sz,
1147 union perf_event **event_ptr,
1148 struct perf_sample *sample)
1149{
1150 union perf_event *event;
1151 size_t hdr_sz, rest;
1152 int fd;
1153
1154 if (session->one_mmap && !session->header.needs_swap) {
1155 event = file_offset - session->one_mmap_offset +
1156 session->one_mmap_addr;
1157 goto out_parse_sample;
1158 }
1159
1160 if (perf_data_file__is_pipe(session->file))
1161 return -1;
1162
1163 fd = perf_data_file__fd(session->file);
1164 hdr_sz = sizeof(struct perf_event_header);
1165
1166 if (buf_sz < hdr_sz)
1167 return -1;
1168
1169 if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 ||
1170 readn(fd, &buf, hdr_sz) != (ssize_t)hdr_sz)
1171 return -1;
1172
1173 event = (union perf_event *)buf;
1174
1175 if (session->header.needs_swap)
1176 perf_event_header__bswap(&event->header);
1177
1178 if (event->header.size < hdr_sz)
1179 return -1;
1180
1181 rest = event->header.size - hdr_sz;
1182
1183 if (readn(fd, &buf, rest) != (ssize_t)rest)
1184 return -1;
1185
1186 if (session->header.needs_swap)
1187 event_swap(event, perf_evlist__sample_id_all(session->evlist));
1188
1189out_parse_sample:
1190
1191 if (sample && event->header.type < PERF_RECORD_USER_TYPE_START &&
1192 perf_evlist__parse_sample(session->evlist, event, sample))
1193 return -1;
1194
1195 *event_ptr = event;
1196
1197 return 0;
1198}
1199
Adrian Hunterd5652d82014-07-23 22:19:58 +03001200static s64 perf_session__process_event(struct perf_session *session,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001201 union perf_event *event, u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001202{
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -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 Melo8d50e5b2011-01-29 13:02:00 -02001205 struct perf_sample sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001206 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001207
Jiri Olsa268fb202012-05-30 14:23:43 +02001208 if (session->header.needs_swap)
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001209 event_swap(event, perf_evlist__sample_id_all(evlist));
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001210
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001211 if (event->header.type >= PERF_RECORD_HEADER_MAX)
1212 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001213
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001214 events_stats__inc(&evlist->stats, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001215
1216 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001217 return perf_session__process_user_event(session, event, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001218
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001219 /*
1220 * For all kernel events we get the sample data
1221 */
Arnaldo Carvalho de Melo313e53b2015-02-14 15:05:28 -03001222 ret = perf_evlist__parse_sample(evlist, event, &sample);
Frederic Weisbecker5538bec2011-05-22 02:17:22 +02001223 if (ret)
1224 return ret;
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001225
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001226 if (tool->ordered_events) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001227 ret = perf_session__queue_event(session, event, &sample, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001228 if (ret != -ETIME)
1229 return ret;
1230 }
1231
Adrian Hunterc4468702015-04-09 18:53:48 +03001232 return perf_session__deliver_event(session, event, &sample, tool,
1233 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001234}
1235
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001236void perf_event_header__bswap(struct perf_event_header *hdr)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001237{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001238 hdr->type = bswap_32(hdr->type);
1239 hdr->misc = bswap_16(hdr->misc);
1240 hdr->size = bswap_16(hdr->size);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001241}
1242
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001243struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1244{
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001245 return machine__findnew_thread(&session->machines.host, -1, pid);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001246}
1247
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001248static struct thread *perf_session__register_idle_thread(struct perf_session *session)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001249{
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001250 struct thread *thread;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001251
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001252 thread = machine__findnew_thread(&session->machines.host, 0, 0);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001253 if (thread == NULL || thread__set_comm(thread, "swapper", 0)) {
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001254 pr_err("problem inserting idle task.\n");
1255 thread = NULL;
1256 }
1257
1258 return thread;
1259}
1260
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001261static void perf_session__warn_about_errors(const struct perf_session *session)
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001262{
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001263 const struct events_stats *stats = &session->evlist->stats;
1264 const struct ordered_events *oe = &session->ordered_events;
1265
1266 if (session->tool->lost == perf_event__process_lost &&
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001267 stats->nr_events[PERF_RECORD_LOST] != 0) {
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001268 ui__warning("Processed %d events and lost %d chunks!\n\n"
1269 "Check IO/CPU overload!\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001270 stats->nr_events[0],
1271 stats->nr_events[PERF_RECORD_LOST]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001272 }
1273
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001274 if (stats->nr_unknown_events != 0) {
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001275 ui__warning("Found %u unknown events!\n\n"
1276 "Is this an older tool processing a perf.data "
1277 "file generated by a more recent tool?\n\n"
1278 "If that is not the case, consider "
1279 "reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001280 stats->nr_unknown_events);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001281 }
1282
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001283 if (stats->nr_unknown_id != 0) {
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001284 ui__warning("%u samples with id not present in the header\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001285 stats->nr_unknown_id);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001286 }
1287
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001288 if (stats->nr_invalid_chains != 0) {
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001289 ui__warning("Found invalid callchains!\n\n"
1290 "%u out of %u events were discarded for this reason.\n\n"
1291 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001292 stats->nr_invalid_chains,
1293 stats->nr_events[PERF_RECORD_SAMPLE]);
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001294 }
Joerg Roedel0c095712012-02-10 18:05:04 +01001295
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001296 if (stats->nr_unprocessable_samples != 0) {
Joerg Roedel0c095712012-02-10 18:05:04 +01001297 ui__warning("%u unprocessable samples recorded.\n"
1298 "Do you have a KVM guest running and not using 'perf kvm'?\n",
Arnaldo Carvalho de Meloccda0682015-02-14 14:57:13 -03001299 stats->nr_unprocessable_samples);
Joerg Roedel0c095712012-02-10 18:05:04 +01001300 }
Jiri Olsaf61ff6c2014-11-26 16:39:31 +01001301
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001302 if (oe->nr_unordered_events != 0)
1303 ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events);
Adrian Hunter85ed4722015-04-09 18:53:50 +03001304
1305 events_stats__auxtrace_error_warn(stats);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001306}
1307
Tom Zanussi8dc58102010-04-01 23:59:15 -05001308volatile int session_done;
1309
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001310static int __perf_session__process_pipe_events(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05001311{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001312 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001313 struct perf_tool *tool = session->tool;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001314 int fd = perf_data_file__fd(session->file);
Stephane Eranian444d2862012-05-15 13:28:12 +02001315 union perf_event *event;
1316 uint32_t size, cur_size = 0;
1317 void *buf = NULL;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001318 s64 skip = 0;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001319 u64 head;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001320 ssize_t err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001321 void *p;
1322
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001323 perf_tool__fill_defaults(tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001324
1325 head = 0;
Stephane Eranian444d2862012-05-15 13:28:12 +02001326 cur_size = sizeof(union perf_event);
1327
1328 buf = malloc(cur_size);
1329 if (!buf)
1330 return -errno;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001331more:
Stephane Eranian444d2862012-05-15 13:28:12 +02001332 event = buf;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001333 err = readn(fd, event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05001334 if (err <= 0) {
1335 if (err == 0)
1336 goto done;
1337
1338 pr_err("failed to read event header\n");
1339 goto out_err;
1340 }
1341
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001342 if (session->header.needs_swap)
Stephane Eranian444d2862012-05-15 13:28:12 +02001343 perf_event_header__bswap(&event->header);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001344
Stephane Eranian444d2862012-05-15 13:28:12 +02001345 size = event->header.size;
Adrian Hunter27389d72013-07-04 16:20:27 +03001346 if (size < sizeof(struct perf_event_header)) {
1347 pr_err("bad event header size\n");
1348 goto out_err;
1349 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001350
Stephane Eranian444d2862012-05-15 13:28:12 +02001351 if (size > cur_size) {
1352 void *new = realloc(buf, size);
1353 if (!new) {
1354 pr_err("failed to allocate memory to read event\n");
1355 goto out_err;
1356 }
1357 buf = new;
1358 cur_size = size;
1359 event = buf;
1360 }
1361 p = event;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001362 p += sizeof(struct perf_event_header);
1363
Tom Zanussi794e43b2010-05-05 00:27:40 -05001364 if (size - sizeof(struct perf_event_header)) {
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001365 err = readn(fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -05001366 if (err <= 0) {
1367 if (err == 0) {
1368 pr_err("unexpected end of event stream\n");
1369 goto done;
1370 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001371
Tom Zanussi794e43b2010-05-05 00:27:40 -05001372 pr_err("failed to read event data\n");
1373 goto out_err;
1374 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001375 }
1376
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001377 if ((skip = perf_session__process_event(session, event, head)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001378 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
Stephane Eranian444d2862012-05-15 13:28:12 +02001379 head, event->header.size, event->header.type);
Jiri Olsa9389a462012-04-16 20:42:51 +02001380 err = -EINVAL;
1381 goto out_err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001382 }
1383
1384 head += size;
1385
Tom Zanussi8dc58102010-04-01 23:59:15 -05001386 if (skip > 0)
1387 head += skip;
1388
1389 if (!session_done())
1390 goto more;
1391done:
Adrian Hunter8c16b642013-10-18 15:29:02 +03001392 /* do the final flush for ordered samples */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001393 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
Adrian Hunterc4468702015-04-09 18:53:48 +03001394 if (err)
1395 goto out_err;
1396 err = auxtrace__flush_events(session, tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001397out_err:
Stephane Eranian444d2862012-05-15 13:28:12 +02001398 free(buf);
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001399 perf_session__warn_about_errors(session);
Jiri Olsaadc56ed2014-06-10 22:50:03 +02001400 ordered_events__free(&session->ordered_events);
Adrian Hunterc4468702015-04-09 18:53:48 +03001401 auxtrace__free_events(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001402 return err;
1403}
1404
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001405static union perf_event *
1406fetch_mmaped_event(struct perf_session *session,
1407 u64 head, size_t mmap_size, char *buf)
1408{
1409 union perf_event *event;
1410
1411 /*
1412 * Ensure we have enough space remaining to read
1413 * the size of the event in the headers.
1414 */
1415 if (head + sizeof(event->header) > mmap_size)
1416 return NULL;
1417
1418 event = (union perf_event *)(buf + head);
1419
1420 if (session->header.needs_swap)
1421 perf_event_header__bswap(&event->header);
1422
Adrian Hunter27389d72013-07-04 16:20:27 +03001423 if (head + event->header.size > mmap_size) {
1424 /* We're not fetching the event so swap back again */
1425 if (session->header.needs_swap)
1426 perf_event_header__bswap(&event->header);
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001427 return NULL;
Adrian Hunter27389d72013-07-04 16:20:27 +03001428 }
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001429
1430 return event;
1431}
1432
David Miller35d48dd2012-11-10 14:12:19 -05001433/*
1434 * On 64bit we can mmap the data file in one go. No need for tiny mmap
1435 * slices. On 32bit we use 32MB.
1436 */
1437#if BITS_PER_LONG == 64
1438#define MMAP_SIZE ULLONG_MAX
1439#define NUM_MMAPS 1
1440#else
1441#define MMAP_SIZE (32 * 1024 * 1024ULL)
1442#define NUM_MMAPS 128
1443#endif
1444
Namhyung Kim4ac30cf2015-01-29 17:06:43 +09001445static int __perf_session__process_events(struct perf_session *session,
1446 u64 data_offset, u64 data_size,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001447 u64 file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001448{
Arnaldo Carvalho de Melofa713a4e2015-03-03 11:48:12 -03001449 struct ordered_events *oe = &session->ordered_events;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001450 struct perf_tool *tool = session->tool;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001451 int fd = perf_data_file__fd(session->file);
Adrian Hunterd5652d82014-07-23 22:19:58 +03001452 u64 head, page_offset, file_offset, file_pos, size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001453 int err, mmap_prot, mmap_flags, map_idx = 0;
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -03001454 size_t mmap_size;
David Miller35d48dd2012-11-10 14:12:19 -05001455 char *buf, *mmaps[NUM_MMAPS];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001456 union perf_event *event;
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001457 struct ui_progress prog;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001458 s64 skip;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001459
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001460 perf_tool__fill_defaults(tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001461
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001462 page_offset = page_size * (data_offset / page_size);
1463 file_offset = page_offset;
1464 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001465
Namhyung Kimb314e5c2013-09-30 17:19:48 +09001466 if (data_size && (data_offset + data_size < file_size))
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001467 file_size = data_offset + data_size;
1468
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001469 ui_progress__init(&prog, file_size, "Processing events...");
Thomas Gleixner55b446292010-11-30 17:49:46 +00001470
David Miller35d48dd2012-11-10 14:12:19 -05001471 mmap_size = MMAP_SIZE;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001472 if (mmap_size > file_size) {
Thomas Gleixner55b446292010-11-30 17:49:46 +00001473 mmap_size = file_size;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001474 session->one_mmap = true;
1475 }
Thomas Gleixner55b446292010-11-30 17:49:46 +00001476
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001477 memset(mmaps, 0, sizeof(mmaps));
1478
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001479 mmap_prot = PROT_READ;
1480 mmap_flags = MAP_SHARED;
1481
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001482 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001483 mmap_prot |= PROT_WRITE;
1484 mmap_flags = MAP_PRIVATE;
1485 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001486remap:
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001487 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd,
Thomas Gleixner55b446292010-11-30 17:49:46 +00001488 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001489 if (buf == MAP_FAILED) {
1490 pr_err("failed to mmap file\n");
1491 err = -errno;
1492 goto out_err;
1493 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001494 mmaps[map_idx] = buf;
1495 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001496 file_pos = file_offset + head;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001497 if (session->one_mmap) {
1498 session->one_mmap_addr = buf;
1499 session->one_mmap_offset = file_offset;
1500 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001501
1502more:
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001503 event = fetch_mmaped_event(session, head, mmap_size, buf);
1504 if (!event) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001505 if (mmaps[map_idx]) {
1506 munmap(mmaps[map_idx], mmap_size);
1507 mmaps[map_idx] = NULL;
1508 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001509
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001510 page_offset = page_size * (head / page_size);
1511 file_offset += page_offset;
1512 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001513 goto remap;
1514 }
1515
1516 size = event->header.size;
1517
Adrian Hunter27389d72013-07-04 16:20:27 +03001518 if (size < sizeof(struct perf_event_header) ||
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001519 (skip = perf_session__process_event(session, event, file_pos)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001520 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1521 file_offset + head, event->header.size,
1522 event->header.type);
1523 err = -EINVAL;
1524 goto out_err;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001525 }
1526
Adrian Hunter6f917c72014-07-23 22:19:57 +03001527 if (skip)
1528 size += skip;
1529
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001530 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001531 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001532
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001533 ui_progress__update(&prog, size);
Thomas Gleixner55b446292010-11-30 17:49:46 +00001534
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001535 if (session_done())
Adrian Hunter8c16b642013-10-18 15:29:02 +03001536 goto out;
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001537
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001538 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001539 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001540
Adrian Hunter8c16b642013-10-18 15:29:02 +03001541out:
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001542 /* do the final flush for ordered samples */
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001543 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
Adrian Hunterc4468702015-04-09 18:53:48 +03001544 if (err)
1545 goto out_err;
1546 err = auxtrace__flush_events(session, tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001547out_err:
Namhyung Kima5580f32012-11-13 22:30:34 +09001548 ui_progress__finish();
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -03001549 perf_session__warn_about_errors(session);
Jiri Olsaadc56ed2014-06-10 22:50:03 +02001550 ordered_events__free(&session->ordered_events);
Adrian Hunterc4468702015-04-09 18:53:48 +03001551 auxtrace__free_events(session);
Adrian Hunter919d86d2014-07-14 13:02:51 +03001552 session->one_mmap = false;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001553 return err;
1554}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001555
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001556int perf_session__process_events(struct perf_session *session)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001557{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001558 u64 size = perf_data_file__size(session->file);
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001559 int err;
1560
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001561 if (perf_session__register_idle_thread(session) == NULL)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001562 return -ENOMEM;
1563
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001564 if (!perf_data_file__is_pipe(session->file))
1565 err = __perf_session__process_events(session,
1566 session->header.data_offset,
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001567 session->header.data_size, size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001568 else
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001569 err = __perf_session__process_pipe_events(session);
Dave Martin88ca8952010-07-27 11:46:12 -03001570
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001571 return err;
1572}
1573
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001574bool perf_session__has_traces(struct perf_session *session, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001575{
David Ahern93ea01c292013-08-07 22:50:58 -04001576 struct perf_evsel *evsel;
1577
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001578 evlist__for_each(session->evlist, evsel) {
David Ahern93ea01c292013-08-07 22:50:58 -04001579 if (evsel->attr.type == PERF_TYPE_TRACEPOINT)
1580 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001581 }
1582
David Ahern93ea01c292013-08-07 22:50:58 -04001583 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1584 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001585}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001586
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001587int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1588 const char *symbol_name, u64 addr)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001589{
1590 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001591 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001592 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001593
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001594 ref = zalloc(sizeof(struct ref_reloc_sym));
1595 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001596 return -ENOMEM;
1597
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001598 ref->name = strdup(symbol_name);
1599 if (ref->name == NULL) {
1600 free(ref);
1601 return -ENOMEM;
1602 }
1603
1604 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001605 if (bracket)
1606 *bracket = '\0';
1607
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001608 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001609
1610 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001611 struct kmap *kmap = map__kmap(maps[i]);
Wang Nanba927322015-04-07 08:22:45 +00001612
1613 if (!kmap)
1614 continue;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001615 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001616 }
1617
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001618 return 0;
1619}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001620
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001621size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001622{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001623 return machines__fprintf_dsos(&session->machines, fp);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001624}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001625
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001626size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001627 bool (skip)(struct dso *dso, int parm), int parm)
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001628{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001629 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001630}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001631
1632size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1633{
Adrian Hunterc4468702015-04-09 18:53:48 +03001634 size_t ret;
1635 const char *msg = "";
1636
1637 if (perf_header__has_feat(&session->header, HEADER_AUXTRACE))
1638 msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)";
1639
1640 ret = fprintf(fp, "Aggregated stats:%s\n", msg);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001641
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001642 ret += events_stats__fprintf(&session->evlist->stats, fp);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001643 return ret;
1644}
David Ahernc0230b22011-03-09 22:23:27 -07001645
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001646size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1647{
1648 /*
1649 * FIXME: Here we have to actually print all the machines in this
1650 * session, not just the host...
1651 */
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001652 return machine__fprintf(&session->machines.host, fp);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001653}
1654
David Ahern9cbdb702011-04-06 21:54:20 -06001655struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1656 unsigned int type)
1657{
1658 struct perf_evsel *pos;
1659
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001660 evlist__for_each(session->evlist, pos) {
David Ahern9cbdb702011-04-06 21:54:20 -06001661 if (pos->attr.type == type)
1662 return pos;
1663 }
1664 return NULL;
1665}
1666
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001667void perf_evsel__print_ip(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -03001668 struct addr_location *al,
David Ahern307cbb92013-08-07 22:50:53 -04001669 unsigned int print_opts, unsigned int stack_depth)
David Ahernc0230b22011-03-09 22:23:27 -07001670{
David Ahernc0230b22011-03-09 22:23:27 -07001671 struct callchain_cursor_node *node;
David Aherna6ffaf92013-08-07 22:50:51 -04001672 int print_ip = print_opts & PRINT_IP_OPT_IP;
1673 int print_sym = print_opts & PRINT_IP_OPT_SYM;
1674 int print_dso = print_opts & PRINT_IP_OPT_DSO;
1675 int print_symoffset = print_opts & PRINT_IP_OPT_SYMOFFSET;
David Ahernb0b35f02013-08-07 22:50:52 -04001676 int print_oneline = print_opts & PRINT_IP_OPT_ONELINE;
Adrian Huntercc8fae12013-12-06 09:42:57 +02001677 int print_srcline = print_opts & PRINT_IP_OPT_SRCLINE;
David Ahernb0b35f02013-08-07 22:50:52 -04001678 char s = print_oneline ? ' ' : '\t';
David Ahernc0230b22011-03-09 22:23:27 -07001679
David Ahernc0230b22011-03-09 22:23:27 -07001680 if (symbol_conf.use_callchain && sample->callchain) {
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001681 struct addr_location node_al;
David Ahernc0230b22011-03-09 22:23:27 -07001682
Arnaldo Carvalho de Melocc8b7c22014-10-23 15:26:17 -03001683 if (thread__resolve_callchain(al->thread, evsel,
1684 sample, NULL, NULL,
1685 PERF_MAX_STACK_DEPTH) != 0) {
David Ahernc0230b22011-03-09 22:23:27 -07001686 if (verbose)
1687 error("Failed to resolve callchain. Skipping\n");
1688 return;
1689 }
Namhyung Kim47260642012-05-31 14:43:26 +09001690 callchain_cursor_commit(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001691
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001692 if (print_symoffset)
1693 node_al = *al;
1694
David Ahern307cbb92013-08-07 22:50:53 -04001695 while (stack_depth) {
Adrian Huntera4eb24a2013-12-06 09:42:56 +02001696 u64 addr = 0;
1697
Namhyung Kim47260642012-05-31 14:43:26 +09001698 node = callchain_cursor_current(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001699 if (!node)
1700 break;
1701
David Ahernd2ff1b12013-11-18 13:32:44 -07001702 if (node->sym && node->sym->ignore)
1703 goto next;
1704
David Aherna6ffaf92013-08-07 22:50:51 -04001705 if (print_ip)
David Ahernb0b35f02013-08-07 22:50:52 -04001706 printf("%c%16" PRIx64, s, node->ip);
David Aherna6ffaf92013-08-07 22:50:51 -04001707
Adrian Huntera4eb24a2013-12-06 09:42:56 +02001708 if (node->map)
1709 addr = node->map->map_ip(node->map, node->ip);
1710
David Ahern787bef12011-05-27 14:28:43 -06001711 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001712 printf(" ");
David Ahern251f4262013-07-28 09:14:34 -06001713 if (print_symoffset) {
Adrian Huntera4eb24a2013-12-06 09:42:56 +02001714 node_al.addr = addr;
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001715 node_al.map = node->map;
1716 symbol__fprintf_symname_offs(node->sym, &node_al, stdout);
David Ahern251f4262013-07-28 09:14:34 -06001717 } else
1718 symbol__fprintf_symname(node->sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001719 }
David Ahern251f4262013-07-28 09:14:34 -06001720
David Ahern610723f2011-05-27 14:28:44 -06001721 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001722 printf(" (");
David Ahern52deff72012-05-29 22:58:26 -06001723 map__fprintf_dsoname(node->map, stdout);
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001724 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001725 }
David Ahernb0b35f02013-08-07 22:50:52 -04001726
Adrian Huntercc8fae12013-12-06 09:42:57 +02001727 if (print_srcline)
1728 map__fprintf_srcline(node->map, addr, "\n ",
1729 stdout);
1730
David Ahernb0b35f02013-08-07 22:50:52 -04001731 if (!print_oneline)
1732 printf("\n");
David Ahernc0230b22011-03-09 22:23:27 -07001733
David Ahern307cbb92013-08-07 22:50:53 -04001734 stack_depth--;
David Ahernd2ff1b12013-11-18 13:32:44 -07001735next:
1736 callchain_cursor_advance(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001737 }
1738
1739 } else {
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001740 if (al->sym && al->sym->ignore)
David Ahernd2ff1b12013-11-18 13:32:44 -07001741 return;
1742
David Aherna6ffaf92013-08-07 22:50:51 -04001743 if (print_ip)
1744 printf("%16" PRIx64, sample->ip);
1745
David Ahern787bef12011-05-27 14:28:43 -06001746 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001747 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001748 if (print_symoffset)
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001749 symbol__fprintf_symname_offs(al->sym, al,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001750 stdout);
1751 else
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001752 symbol__fprintf_symname(al->sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001753 }
1754
1755 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001756 printf(" (");
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001757 map__fprintf_dsoname(al->map, stdout);
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001758 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001759 }
Adrian Huntercc8fae12013-12-06 09:42:57 +02001760
1761 if (print_srcline)
1762 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
David Ahernc0230b22011-03-09 22:23:27 -07001763 }
1764}
Anton Blanchard5d67be92011-07-04 21:57:50 +10001765
1766int perf_session__cpu_bitmap(struct perf_session *session,
1767 const char *cpu_list, unsigned long *cpu_bitmap)
1768{
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04001769 int i, err = -1;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001770 struct cpu_map *map;
1771
1772 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1773 struct perf_evsel *evsel;
1774
1775 evsel = perf_session__find_first_evtype(session, i);
1776 if (!evsel)
1777 continue;
1778
1779 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1780 pr_err("File does not contain CPU events. "
1781 "Remove -c option to proceed.\n");
1782 return -1;
1783 }
1784 }
1785
1786 map = cpu_map__new(cpu_list);
David Ahern47fbe532011-11-13 10:45:27 -07001787 if (map == NULL) {
1788 pr_err("Invalid cpu_list\n");
1789 return -1;
1790 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001791
1792 for (i = 0; i < map->nr; i++) {
1793 int cpu = map->map[i];
1794
1795 if (cpu >= MAX_NR_CPUS) {
1796 pr_err("Requested CPU %d too large. "
1797 "Consider raising MAX_NR_CPUS\n", cpu);
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04001798 goto out_delete_map;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001799 }
1800
1801 set_bit(cpu, cpu_bitmap);
1802 }
1803
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04001804 err = 0;
1805
1806out_delete_map:
1807 cpu_map__delete(map);
1808 return err;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001809}
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001810
1811void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1812 bool full)
1813{
1814 struct stat st;
Masanari Iidac5765ec2014-05-15 02:13:38 +09001815 int fd, ret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001816
1817 if (session == NULL || fp == NULL)
1818 return;
1819
Masanari Iidac5765ec2014-05-15 02:13:38 +09001820 fd = perf_data_file__fd(session->file);
1821
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001822 ret = fstat(fd, &st);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001823 if (ret == -1)
1824 return;
1825
1826 fprintf(fp, "# ========\n");
1827 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1828 perf_header__fprintf_info(session, fp, full);
1829 fprintf(fp, "# ========\n#\n");
1830}
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001831
1832
1833int __perf_session__set_tracepoints_handlers(struct perf_session *session,
1834 const struct perf_evsel_str_handler *assocs,
1835 size_t nr_assocs)
1836{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001837 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001838 size_t i;
1839 int err;
1840
1841 for (i = 0; i < nr_assocs; i++) {
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001842 /*
1843 * Adding a handler for an event not in the session,
1844 * just ignore it.
1845 */
1846 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name);
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001847 if (evsel == NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001848 continue;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001849
1850 err = -EEXIST;
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001851 if (evsel->handler != NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001852 goto out;
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001853 evsel->handler = assocs[i].handler;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001854 }
1855
1856 err = 0;
1857out:
1858 return err;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001859}
Adrian Hunter3c659ee2014-10-27 15:49:22 +02001860
1861int perf_event__process_id_index(struct perf_tool *tool __maybe_unused,
1862 union perf_event *event,
1863 struct perf_session *session)
1864{
1865 struct perf_evlist *evlist = session->evlist;
1866 struct id_index_event *ie = &event->id_index;
1867 size_t i, nr, max_nr;
1868
1869 max_nr = (ie->header.size - sizeof(struct id_index_event)) /
1870 sizeof(struct id_index_entry);
1871 nr = ie->nr;
1872 if (nr > max_nr)
1873 return -EINVAL;
1874
1875 if (dump_trace)
1876 fprintf(stdout, " nr: %zu\n", nr);
1877
1878 for (i = 0; i < nr; i++) {
1879 struct id_index_entry *e = &ie->entries[i];
1880 struct perf_sample_id *sid;
1881
1882 if (dump_trace) {
1883 fprintf(stdout, " ... id: %"PRIu64, e->id);
1884 fprintf(stdout, " idx: %"PRIu64, e->idx);
1885 fprintf(stdout, " cpu: %"PRId64, e->cpu);
1886 fprintf(stdout, " tid: %"PRId64"\n", e->tid);
1887 }
1888
1889 sid = perf_evlist__id2sid(evlist, e->id);
1890 if (!sid)
1891 return -ENOENT;
1892 sid->idx = e->idx;
1893 sid->cpu = e->cpu;
1894 sid->tid = e->tid;
1895 }
1896 return 0;
1897}
1898
1899int perf_event__synthesize_id_index(struct perf_tool *tool,
1900 perf_event__handler_t process,
1901 struct perf_evlist *evlist,
1902 struct machine *machine)
1903{
1904 union perf_event *ev;
1905 struct perf_evsel *evsel;
1906 size_t nr = 0, i = 0, sz, max_nr, n;
1907 int err;
1908
1909 pr_debug2("Synthesizing id index\n");
1910
1911 max_nr = (UINT16_MAX - sizeof(struct id_index_event)) /
1912 sizeof(struct id_index_entry);
1913
Arnaldo Carvalho de Melocba9b842014-10-29 11:31:54 -02001914 evlist__for_each(evlist, evsel)
Adrian Hunter3c659ee2014-10-27 15:49:22 +02001915 nr += evsel->ids;
1916
1917 n = nr > max_nr ? max_nr : nr;
1918 sz = sizeof(struct id_index_event) + n * sizeof(struct id_index_entry);
1919 ev = zalloc(sz);
1920 if (!ev)
1921 return -ENOMEM;
1922
1923 ev->id_index.header.type = PERF_RECORD_ID_INDEX;
1924 ev->id_index.header.size = sz;
1925 ev->id_index.nr = n;
1926
Arnaldo Carvalho de Melocba9b842014-10-29 11:31:54 -02001927 evlist__for_each(evlist, evsel) {
Adrian Hunter3c659ee2014-10-27 15:49:22 +02001928 u32 j;
1929
1930 for (j = 0; j < evsel->ids; j++) {
1931 struct id_index_entry *e;
1932 struct perf_sample_id *sid;
1933
1934 if (i >= n) {
1935 err = process(tool, ev, NULL, machine);
1936 if (err)
1937 goto out_err;
1938 nr -= n;
1939 i = 0;
1940 }
1941
1942 e = &ev->id_index.entries[i++];
1943
1944 e->id = evsel->id[j];
1945
1946 sid = perf_evlist__id2sid(evlist, e->id);
1947 if (!sid) {
1948 free(ev);
1949 return -ENOENT;
1950 }
1951
1952 e->idx = sid->idx;
1953 e->cpu = sid->cpu;
1954 e->tid = sid->tid;
1955 }
1956 }
1957
1958 sz = sizeof(struct id_index_event) + nr * sizeof(struct id_index_entry);
1959 ev->id_index.header.size = sz;
1960 ev->id_index.nr = nr;
1961
1962 err = process(tool, ev, NULL, machine);
1963out_err:
1964 free(ev);
1965
1966 return err;
1967}