blob: 0ce46943d62794dcd1e86abf716c69e46951d716 [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 Olsa7dbf4dc2012-09-10 18:50:19 +020017#include "vdso.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020018
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030019static int perf_session__open(struct perf_session *session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020020{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030021 struct perf_data_file *file = session->file;
Tom Zanussi8dc58102010-04-01 23:59:15 -050022
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030023 if (perf_session__read_header(session) < 0) {
Stephane Eranian69996df2012-02-09 23:21:06 +010024 pr_err("incompatible file format (rerun with -v to learn more)");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020025 return -1;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020026 }
27
Jiri Olsacc9784bd2013-10-15 16:27:34 +020028 if (perf_data_file__is_pipe(file))
29 return 0;
30
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030031 if (!perf_evlist__valid_sample_type(session->evlist)) {
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030032 pr_err("non matching sample_type");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020033 return -1;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030034 }
35
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030036 if (!perf_evlist__valid_sample_id_all(session->evlist)) {
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030037 pr_err("non matching sample_id_all");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020038 return -1;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030039 }
40
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030041 if (!perf_evlist__valid_read_format(session->evlist)) {
Jiri Olsa9ede4732012-10-10 17:38:13 +020042 pr_err("non matching read_format");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020043 return -1;
Jiri Olsa9ede4732012-10-10 17:38:13 +020044 }
45
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020046 return 0;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020047}
48
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030049void perf_session__set_id_hdr_size(struct perf_session *session)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020050{
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030051 u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist);
52
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030053 machines__set_id_hdr_size(&session->machines, id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020054}
55
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030056int perf_session__create_kernel_maps(struct perf_session *session)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080057{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030058 int ret = machine__create_kernel_maps(&session->machines.host);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080059
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080060 if (ret >= 0)
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030061 ret = machines__create_guest_kernel_maps(&session->machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080062 return ret;
63}
64
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030065static void perf_session__destroy_kernel_maps(struct perf_session *session)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -030066{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030067 machines__destroy_kernel_maps(&session->machines);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -030068}
69
Jiri Olsaf5fc14122013-10-15 16:27:32 +020070struct perf_session *perf_session__new(struct perf_data_file *file,
71 bool repipe, struct perf_tool *tool)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020072{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030073 struct perf_session *session = zalloc(sizeof(*session));
Robert Richterefad1412011-12-07 10:02:54 +010074
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030075 if (!session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020076 goto out;
77
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030078 session->repipe = repipe;
79 INIT_LIST_HEAD(&session->ordered_samples.samples);
80 INIT_LIST_HEAD(&session->ordered_samples.sample_cache);
81 INIT_LIST_HEAD(&session->ordered_samples.to_free);
82 machines__init(&session->machines);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020083
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020084 if (file) {
85 if (perf_data_file__open(file))
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -020086 goto out_delete;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020087
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030088 session->file = file;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020089
90 if (perf_data_file__is_read(file)) {
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030091 if (perf_session__open(session) < 0)
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020092 goto out_close;
93
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030094 perf_session__set_id_hdr_size(session);
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020095 }
96 }
97
98 if (!file || perf_data_file__is_write(file)) {
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -020099 /*
100 * In O_RDONLY mode this will be performed when reading the
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200101 * kernel MMAP event, in perf_event__process_mmap().
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200102 */
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300103 if (perf_session__create_kernel_maps(session) < 0)
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200104 goto out_delete;
105 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200106
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200107 if (tool && tool->ordering_requires_timestamps &&
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300108 tool->ordered_samples && !perf_evlist__sample_id_all(session->evlist)) {
Ian Munsie21ef97f2010-12-10 14:09:16 +1100109 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200110 tool->ordered_samples = false;
Ian Munsie21ef97f2010-12-10 14:09:16 +1100111 }
112
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300113 return session;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200114
115 out_close:
116 perf_data_file__close(file);
117 out_delete:
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300118 perf_session__delete(session);
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200119 out:
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200120 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200121}
122
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200123static void perf_session__delete_dead_threads(struct perf_session *session)
124{
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300125 machine__delete_dead_threads(&session->machines.host);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200126}
127
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200128static void perf_session__delete_threads(struct perf_session *session)
129{
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300130 machine__delete_threads(&session->machines.host);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200131}
132
Namhyung Kim03cd2092012-11-21 13:43:19 +0900133static void perf_session_env__delete(struct perf_session_env *env)
134{
135 free(env->hostname);
136 free(env->os_release);
137 free(env->version);
138 free(env->arch);
139 free(env->cpu_desc);
140 free(env->cpuid);
141
142 free(env->cmdline);
143 free(env->sibling_cores);
144 free(env->sibling_threads);
145 free(env->numa_nodes);
146 free(env->pmu_mappings);
147}
148
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300149void perf_session__delete(struct perf_session *session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200150{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300151 perf_session__destroy_kernel_maps(session);
152 perf_session__delete_dead_threads(session);
153 perf_session__delete_threads(session);
154 perf_session_env__delete(&session->header.env);
155 machines__exit(&session->machines);
156 if (session->file)
157 perf_data_file__close(session->file);
158 free(session);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200159 vdso__exit();
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200160}
Arnaldo Carvalho de Meloa3286262009-12-14 14:22:59 -0200161
Adrian Hunter47c3d102013-07-04 16:20:21 +0300162static int process_event_synth_tracing_data_stub(struct perf_tool *tool
163 __maybe_unused,
164 union perf_event *event
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300165 __maybe_unused,
166 struct perf_session *session
167 __maybe_unused)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200168{
169 dump_printf(": unhandled!\n");
170 return 0;
171}
172
Adrian Hunter47c3d102013-07-04 16:20:21 +0300173static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused,
174 union perf_event *event __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300175 struct perf_evlist **pevlist
176 __maybe_unused)
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200177{
178 dump_printf(": unhandled!\n");
179 return 0;
180}
181
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300182static int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
183 union perf_event *event __maybe_unused,
184 struct perf_sample *sample __maybe_unused,
185 struct perf_evsel *evsel __maybe_unused,
186 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300187{
188 dump_printf(": unhandled!\n");
189 return 0;
190}
191
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300192static int process_event_stub(struct perf_tool *tool __maybe_unused,
193 union perf_event *event __maybe_unused,
194 struct perf_sample *sample __maybe_unused,
195 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200196{
197 dump_printf(": unhandled!\n");
198 return 0;
199}
200
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300201static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
202 union perf_event *event __maybe_unused,
203 struct perf_session *perf_session
204 __maybe_unused)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200205{
206 dump_printf(": unhandled!\n");
207 return 0;
208}
209
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200210static int process_finished_round(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200211 union perf_event *event,
212 struct perf_session *session);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200213
David Ahern9c501402013-08-02 14:05:41 -0600214void perf_tool__fill_defaults(struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200215{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200216 if (tool->sample == NULL)
217 tool->sample = process_event_sample_stub;
218 if (tool->mmap == NULL)
219 tool->mmap = process_event_stub;
David Ahern6adb0b02013-09-22 19:44:59 -0600220 if (tool->mmap2 == NULL)
221 tool->mmap2 = process_event_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200222 if (tool->comm == NULL)
223 tool->comm = process_event_stub;
224 if (tool->fork == NULL)
225 tool->fork = process_event_stub;
226 if (tool->exit == NULL)
227 tool->exit = process_event_stub;
228 if (tool->lost == NULL)
229 tool->lost = perf_event__process_lost;
230 if (tool->read == NULL)
231 tool->read = process_event_sample_stub;
232 if (tool->throttle == NULL)
233 tool->throttle = process_event_stub;
234 if (tool->unthrottle == NULL)
235 tool->unthrottle = process_event_stub;
236 if (tool->attr == NULL)
237 tool->attr = process_event_synth_attr_stub;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200238 if (tool->tracing_data == NULL)
239 tool->tracing_data = process_event_synth_tracing_data_stub;
240 if (tool->build_id == NULL)
241 tool->build_id = process_finished_round_stub;
242 if (tool->finished_round == NULL) {
243 if (tool->ordered_samples)
244 tool->finished_round = process_finished_round;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200245 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200246 tool->finished_round = process_finished_round_stub;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200247 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200248}
David Ahern80c01202012-06-08 11:47:51 -0300249
250void mem_bswap_32(void *src, int byte_size)
251{
252 u32 *m = src;
253 while (byte_size > 0) {
254 *m = bswap_32(*m);
255 byte_size -= sizeof(u32);
256 ++m;
257 }
258}
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200259
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200260void mem_bswap_64(void *src, int byte_size)
261{
262 u64 *m = src;
263
264 while (byte_size > 0) {
265 *m = bswap_64(*m);
266 byte_size -= sizeof(u64);
267 ++m;
268 }
269}
270
Jiri Olsa268fb202012-05-30 14:23:43 +0200271static void swap_sample_id_all(union perf_event *event, void *data)
272{
273 void *end = (void *) event + event->header.size;
274 int size = end - data;
275
276 BUG_ON(size % sizeof(u64));
277 mem_bswap_64(data, size);
278}
279
280static void perf_event__all64_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300281 bool sample_id_all __maybe_unused)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200282{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200283 struct perf_event_header *hdr = &event->header;
284 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200285}
286
Jiri Olsa268fb202012-05-30 14:23:43 +0200287static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200288{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200289 event->comm.pid = bswap_32(event->comm.pid);
290 event->comm.tid = bswap_32(event->comm.tid);
Jiri Olsa268fb202012-05-30 14:23:43 +0200291
292 if (sample_id_all) {
293 void *data = &event->comm.comm;
294
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300295 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200296 swap_sample_id_all(event, data);
297 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200298}
299
Jiri Olsa268fb202012-05-30 14:23:43 +0200300static void perf_event__mmap_swap(union perf_event *event,
301 bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200302{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200303 event->mmap.pid = bswap_32(event->mmap.pid);
304 event->mmap.tid = bswap_32(event->mmap.tid);
305 event->mmap.start = bswap_64(event->mmap.start);
306 event->mmap.len = bswap_64(event->mmap.len);
307 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
Jiri Olsa268fb202012-05-30 14:23:43 +0200308
309 if (sample_id_all) {
310 void *data = &event->mmap.filename;
311
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300312 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200313 swap_sample_id_all(event, data);
314 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200315}
316
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200317static void perf_event__mmap2_swap(union perf_event *event,
318 bool sample_id_all)
319{
320 event->mmap2.pid = bswap_32(event->mmap2.pid);
321 event->mmap2.tid = bswap_32(event->mmap2.tid);
322 event->mmap2.start = bswap_64(event->mmap2.start);
323 event->mmap2.len = bswap_64(event->mmap2.len);
324 event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
325 event->mmap2.maj = bswap_32(event->mmap2.maj);
326 event->mmap2.min = bswap_32(event->mmap2.min);
327 event->mmap2.ino = bswap_64(event->mmap2.ino);
328
329 if (sample_id_all) {
330 void *data = &event->mmap2.filename;
331
332 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
333 swap_sample_id_all(event, data);
334 }
335}
Jiri Olsa268fb202012-05-30 14:23:43 +0200336static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200337{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200338 event->fork.pid = bswap_32(event->fork.pid);
339 event->fork.tid = bswap_32(event->fork.tid);
340 event->fork.ppid = bswap_32(event->fork.ppid);
341 event->fork.ptid = bswap_32(event->fork.ptid);
342 event->fork.time = bswap_64(event->fork.time);
Jiri Olsa268fb202012-05-30 14:23:43 +0200343
344 if (sample_id_all)
345 swap_sample_id_all(event, &event->fork + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200346}
347
Jiri Olsa268fb202012-05-30 14:23:43 +0200348static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200349{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200350 event->read.pid = bswap_32(event->read.pid);
351 event->read.tid = bswap_32(event->read.tid);
352 event->read.value = bswap_64(event->read.value);
353 event->read.time_enabled = bswap_64(event->read.time_enabled);
354 event->read.time_running = bswap_64(event->read.time_running);
355 event->read.id = bswap_64(event->read.id);
Jiri Olsa268fb202012-05-30 14:23:43 +0200356
357 if (sample_id_all)
358 swap_sample_id_all(event, &event->read + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200359}
360
Jiri Olsadd96c462013-09-01 12:36:15 +0200361static void perf_event__throttle_swap(union perf_event *event,
362 bool sample_id_all)
363{
364 event->throttle.time = bswap_64(event->throttle.time);
365 event->throttle.id = bswap_64(event->throttle.id);
366 event->throttle.stream_id = bswap_64(event->throttle.stream_id);
367
368 if (sample_id_all)
369 swap_sample_id_all(event, &event->throttle + 1);
370}
371
Jiri Olsae108c662012-05-16 08:59:03 +0200372static u8 revbyte(u8 b)
373{
374 int rev = (b >> 4) | ((b & 0xf) << 4);
375 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
376 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
377 return (u8) rev;
378}
379
380/*
381 * XXX this is hack in attempt to carry flags bitfield
382 * throught endian village. ABI says:
383 *
384 * Bit-fields are allocated from right to left (least to most significant)
385 * on little-endian implementations and from left to right (most to least
386 * significant) on big-endian implementations.
387 *
388 * The above seems to be byte specific, so we need to reverse each
389 * byte of the bitfield. 'Internet' also says this might be implementation
390 * specific and we probably need proper fix and carry perf_event_attr
391 * bitfield flags in separate data file FEAT_ section. Thought this seems
392 * to work for now.
393 */
394static void swap_bitfield(u8 *p, unsigned len)
395{
396 unsigned i;
397
398 for (i = 0; i < len; i++) {
399 *p = revbyte(*p);
400 p++;
401 }
402}
403
David Aherneda39132011-07-15 12:34:09 -0600404/* exported for swapping attributes in file header */
405void perf_event__attr_swap(struct perf_event_attr *attr)
406{
407 attr->type = bswap_32(attr->type);
408 attr->size = bswap_32(attr->size);
409 attr->config = bswap_64(attr->config);
410 attr->sample_period = bswap_64(attr->sample_period);
411 attr->sample_type = bswap_64(attr->sample_type);
412 attr->read_format = bswap_64(attr->read_format);
413 attr->wakeup_events = bswap_32(attr->wakeup_events);
414 attr->bp_type = bswap_32(attr->bp_type);
415 attr->bp_addr = bswap_64(attr->bp_addr);
416 attr->bp_len = bswap_64(attr->bp_len);
Adrian Hunter7db59522013-10-18 15:29:03 +0300417 attr->branch_sample_type = bswap_64(attr->branch_sample_type);
418 attr->sample_regs_user = bswap_64(attr->sample_regs_user);
419 attr->sample_stack_user = bswap_32(attr->sample_stack_user);
Jiri Olsae108c662012-05-16 08:59:03 +0200420
421 swap_bitfield((u8 *) (&attr->read_format + 1), sizeof(u64));
David Aherneda39132011-07-15 12:34:09 -0600422}
423
Jiri Olsa268fb202012-05-30 14:23:43 +0200424static void perf_event__hdr_attr_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300425 bool sample_id_all __maybe_unused)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500426{
427 size_t size;
428
David Aherneda39132011-07-15 12:34:09 -0600429 perf_event__attr_swap(&event->attr.attr);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500430
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200431 size = event->header.size;
432 size -= (void *)&event->attr.id - (void *)event;
433 mem_bswap_64(event->attr.id, size);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500434}
435
Jiri Olsa268fb202012-05-30 14:23:43 +0200436static void perf_event__event_type_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300437 bool sample_id_all __maybe_unused)
Tom Zanussicd19a032010-04-01 23:59:20 -0500438{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200439 event->event_type.event_type.event_id =
440 bswap_64(event->event_type.event_type.event_id);
Tom Zanussicd19a032010-04-01 23:59:20 -0500441}
442
Jiri Olsa268fb202012-05-30 14:23:43 +0200443static void perf_event__tracing_data_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300444 bool sample_id_all __maybe_unused)
Tom Zanussi92155452010-04-01 23:59:21 -0500445{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200446 event->tracing_data.size = bswap_32(event->tracing_data.size);
Tom Zanussi92155452010-04-01 23:59:21 -0500447}
448
Jiri Olsa268fb202012-05-30 14:23:43 +0200449typedef void (*perf_event__swap_op)(union perf_event *event,
450 bool sample_id_all);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200451
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200452static perf_event__swap_op perf_event__swap_ops[] = {
453 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200454 [PERF_RECORD_MMAP2] = perf_event__mmap2_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200455 [PERF_RECORD_COMM] = perf_event__comm_swap,
456 [PERF_RECORD_FORK] = perf_event__task_swap,
457 [PERF_RECORD_EXIT] = perf_event__task_swap,
458 [PERF_RECORD_LOST] = perf_event__all64_swap,
459 [PERF_RECORD_READ] = perf_event__read_swap,
Jiri Olsadd96c462013-09-01 12:36:15 +0200460 [PERF_RECORD_THROTTLE] = perf_event__throttle_swap,
461 [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200462 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
David Aherneda39132011-07-15 12:34:09 -0600463 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200464 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
465 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
466 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
467 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200468};
469
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200470struct sample_queue {
471 u64 timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000472 u64 file_offset;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200473 union perf_event *event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200474 struct list_head list;
475};
476
Thomas Gleixner020bb752010-11-30 17:49:53 +0000477static void perf_session_free_sample_buffers(struct perf_session *session)
478{
479 struct ordered_samples *os = &session->ordered_samples;
480
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000481 while (!list_empty(&os->to_free)) {
Thomas Gleixner020bb752010-11-30 17:49:53 +0000482 struct sample_queue *sq;
483
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000484 sq = list_entry(os->to_free.next, struct sample_queue, list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000485 list_del(&sq->list);
486 free(sq);
487 }
488}
489
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100490static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200491 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200492 struct perf_sample *sample,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200493 struct perf_tool *tool,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +0000494 u64 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100495
David Ahernd25380cd2012-08-26 12:24:41 -0600496static int flush_sample_queue(struct perf_session *s,
David Ahern9c501402013-08-02 14:05:41 -0600497 struct perf_tool *tool)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200498{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000499 struct ordered_samples *os = &s->ordered_samples;
500 struct list_head *head = &os->samples;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200501 struct sample_queue *tmp, *iter;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200502 struct perf_sample sample;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000503 u64 limit = os->next_flush;
504 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
Jiri Olsa526fd8d2013-09-05 11:14:50 +0200505 bool show_progress = limit == ULLONG_MAX;
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -0300506 struct ui_progress prog;
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200507 int ret;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200508
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200509 if (!tool->ordered_samples || !limit)
David Ahernd25380cd2012-08-26 12:24:41 -0600510 return 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200511
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -0300512 if (show_progress)
513 ui_progress__init(&prog, os->nr_samples, "Processing time ordered events...");
514
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200515 list_for_each_entry_safe(iter, tmp, head, list) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -0300516 if (session_done())
517 return 0;
518
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200519 if (iter->timestamp > limit)
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000520 break;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200521
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300522 ret = perf_evlist__parse_sample(s->evlist, iter->event, &sample);
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200523 if (ret)
524 pr_err("Can't parse sample, err = %d\n", ret);
David Ahernd25380cd2012-08-26 12:24:41 -0600525 else {
526 ret = perf_session_deliver_event(s, iter->event, &sample, tool,
527 iter->file_offset);
528 if (ret)
529 return ret;
530 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200531
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000532 os->last_flush = iter->timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200533 list_del(&iter->list);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000534 list_add(&iter->list, &os->sample_cache);
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -0300535
536 if (show_progress)
537 ui_progress__update(&prog, 1);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200538 }
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000539
540 if (list_empty(head)) {
541 os->last_sample = NULL;
542 } else if (last_ts <= limit) {
543 os->last_sample =
544 list_entry(head->prev, struct sample_queue, list);
545 }
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200546
547 os->nr_samples = 0;
David Ahernd25380cd2012-08-26 12:24:41 -0600548
549 return 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200550}
551
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200552/*
553 * When perf record finishes a pass on every buffers, it records this pseudo
554 * event.
555 * We record the max timestamp t found in the pass n.
556 * Assuming these timestamps are monotonic across cpus, we know that if
557 * a buffer still has events with timestamps below t, they will be all
558 * available and then read in the pass n + 1.
559 * Hence when we start to read the pass n + 2, we can safely flush every
560 * events with timestamps below t.
561 *
562 * ============ PASS n =================
563 * CPU 0 | CPU 1
564 * |
565 * cnt1 timestamps | cnt2 timestamps
566 * 1 | 2
567 * 2 | 3
568 * - | 4 <--- max recorded
569 *
570 * ============ PASS n + 1 ==============
571 * CPU 0 | CPU 1
572 * |
573 * cnt1 timestamps | cnt2 timestamps
574 * 3 | 5
575 * 4 | 6
576 * 5 | 7 <---- max recorded
577 *
578 * Flush every events below timestamp 4
579 *
580 * ============ PASS n + 2 ==============
581 * CPU 0 | CPU 1
582 * |
583 * cnt1 timestamps | cnt2 timestamps
584 * 6 | 8
585 * 7 | 9
586 * - | 10
587 *
588 * Flush every events below timestamp 7
589 * etc...
590 */
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200591static int process_finished_round(struct perf_tool *tool,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300592 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200593 struct perf_session *session)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200594{
David Ahernd25380cd2012-08-26 12:24:41 -0600595 int ret = flush_sample_queue(session, tool);
596 if (!ret)
597 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200598
David Ahernd25380cd2012-08-26 12:24:41 -0600599 return ret;
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200600}
601
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200602/* The queue is ordered by time */
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100603static void __queue_event(struct sample_queue *new, struct perf_session *s)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200604{
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000605 struct ordered_samples *os = &s->ordered_samples;
606 struct sample_queue *sample = os->last_sample;
607 u64 timestamp = new->timestamp;
608 struct list_head *p;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200609
Arnaldo Carvalho de Melo88660562011-10-29 12:41:45 -0200610 ++os->nr_samples;
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000611 os->last_sample = new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200612
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000613 if (!sample) {
614 list_add(&new->list, &os->samples);
615 os->max_timestamp = timestamp;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200616 return;
617 }
618
619 /*
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000620 * last_sample might point to some random place in the list as it's
621 * the last queued event. We expect that the new event is close to
622 * this.
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200623 */
Thomas Gleixnera1225de2010-11-30 17:49:33 +0000624 if (sample->timestamp <= timestamp) {
625 while (sample->timestamp <= timestamp) {
626 p = sample->list.next;
627 if (p == &os->samples) {
628 list_add_tail(&new->list, &os->samples);
629 os->max_timestamp = timestamp;
630 return;
631 }
632 sample = list_entry(p, struct sample_queue, list);
633 }
634 list_add_tail(&new->list, &sample->list);
635 } else {
636 while (sample->timestamp > timestamp) {
637 p = sample->list.prev;
638 if (p == &os->samples) {
639 list_add(&new->list, &os->samples);
640 return;
641 }
642 sample = list_entry(p, struct sample_queue, list);
643 }
644 list_add(&new->list, &sample->list);
645 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200646}
647
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000648#define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
649
David Aherne30b88a2013-08-05 21:41:33 -0400650int perf_session_queue_event(struct perf_session *s, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200651 struct perf_sample *sample, u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200652{
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000653 struct ordered_samples *os = &s->ordered_samples;
654 struct list_head *sc = &os->sample_cache;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200655 u64 timestamp = sample->time;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200656 struct sample_queue *new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200657
Thomas Gleixner79a14c12010-12-07 12:48:44 +0000658 if (!timestamp || timestamp == ~0ULL)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100659 return -ETIME;
660
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200661 if (timestamp < s->ordered_samples.last_flush) {
662 printf("Warning: Timestamp below last timeslice flush\n");
663 return -EINVAL;
664 }
665
Thomas Gleixner020bb752010-11-30 17:49:53 +0000666 if (!list_empty(sc)) {
667 new = list_entry(sc->next, struct sample_queue, list);
668 list_del(&new->list);
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000669 } else if (os->sample_buffer) {
670 new = os->sample_buffer + os->sample_buffer_idx;
671 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
672 os->sample_buffer = NULL;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000673 } else {
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000674 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
675 if (!os->sample_buffer)
Thomas Gleixner020bb752010-11-30 17:49:53 +0000676 return -ENOMEM;
Thomas Gleixner5c891f32010-11-30 17:49:55 +0000677 list_add(&os->sample_buffer->list, &os->to_free);
678 os->sample_buffer_idx = 2;
679 new = os->sample_buffer + 1;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000680 }
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200681
682 new->timestamp = timestamp;
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000683 new->file_offset = file_offset;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000684 new->event = event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200685
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100686 __queue_event(new, s);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200687
688 return 0;
689}
690
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200691static void callchain__printf(struct perf_sample *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200692{
693 unsigned int i;
694
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200695 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200696
697 for (i = 0; i < sample->callchain->nr; i++)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200698 printf("..... %2d: %016" PRIx64 "\n",
699 i, sample->callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200700}
701
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100702static void branch_stack__printf(struct perf_sample *sample)
703{
704 uint64_t i;
705
706 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
707
708 for (i = 0; i < sample->branch_stack->nr; i++)
709 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n",
710 i, sample->branch_stack->entries[i].from,
711 sample->branch_stack->entries[i].to);
712}
713
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200714static void regs_dump__printf(u64 mask, u64 *regs)
715{
716 unsigned rid, i = 0;
717
718 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
719 u64 val = regs[i++];
720
721 printf(".... %-5s 0x%" PRIx64 "\n",
722 perf_reg_name(rid), val);
723 }
724}
725
726static void regs_user__printf(struct perf_sample *sample, u64 mask)
727{
728 struct regs_dump *user_regs = &sample->user_regs;
729
730 if (user_regs->regs) {
731 printf("... user regs: mask 0x%" PRIx64 "\n", mask);
732 regs_dump__printf(mask, user_regs->regs);
733 }
734}
735
736static void stack_user__printf(struct stack_dump *dump)
737{
738 printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
739 dump->size, dump->offset);
740}
741
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200742static void perf_session__print_tstamp(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200743 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200744 struct perf_sample *sample)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200745{
Adrian Hunter75562572013-08-27 11:23:09 +0300746 u64 sample_type = __perf_evlist__combined_sample_type(session->evlist);
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300747
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200748 if (event->header.type != PERF_RECORD_SAMPLE &&
Arnaldo Carvalho de Melo5e562472012-08-01 19:25:26 -0300749 !perf_evlist__sample_id_all(session->evlist)) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200750 fputs("-1 -1 ", stdout);
751 return;
752 }
753
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300754 if ((sample_type & PERF_SAMPLE_CPU))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200755 printf("%u ", sample->cpu);
756
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300757 if (sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200758 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200759}
760
Jiri Olsa9ede4732012-10-10 17:38:13 +0200761static void sample_read__printf(struct perf_sample *sample, u64 read_format)
762{
763 printf("... sample_read:\n");
764
765 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
766 printf("...... time enabled %016" PRIx64 "\n",
767 sample->read.time_enabled);
768
769 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
770 printf("...... time running %016" PRIx64 "\n",
771 sample->read.time_running);
772
773 if (read_format & PERF_FORMAT_GROUP) {
774 u64 i;
775
776 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
777
778 for (i = 0; i < sample->read.group.nr; i++) {
779 struct sample_read_value *value;
780
781 value = &sample->read.group.values[i];
782 printf("..... id %016" PRIx64
783 ", value %016" PRIx64 "\n",
784 value->id, value->value);
785 }
786 } else
787 printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
788 sample->read.one.id, sample->read.one.value);
789}
790
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200791static void dump_event(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200792 u64 file_offset, struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000793{
794 if (!dump_trace)
795 return;
796
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200797 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
798 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000799
800 trace_event(event);
801
802 if (sample)
803 perf_session__print_tstamp(session, event, sample);
804
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200805 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200806 event->header.size, perf_event__name(event->header.type));
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000807}
808
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200809static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200810 struct perf_sample *sample)
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000811{
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300812 u64 sample_type;
813
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200814 if (!dump_trace)
815 return;
816
David Ahern7cec0922011-05-30 13:08:23 -0600817 printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200818 event->header.misc, sample->pid, sample->tid, sample->ip,
David Ahern7cec0922011-05-30 13:08:23 -0600819 sample->period, sample->addr);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000820
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200821 sample_type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300822
823 if (sample_type & PERF_SAMPLE_CALLCHAIN)
Arnaldo Carvalho de Meloddbc24b2010-12-09 12:20:20 -0200824 callchain__printf(sample);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100825
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300826 if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100827 branch_stack__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200828
829 if (sample_type & PERF_SAMPLE_REGS_USER)
830 regs_user__printf(sample, evsel->attr.sample_regs_user);
831
832 if (sample_type & PERF_SAMPLE_STACK_USER)
833 stack_user__printf(&sample->user_stack);
Andi Kleen05484292013-01-24 16:10:29 +0100834
835 if (sample_type & PERF_SAMPLE_WEIGHT)
836 printf("... weight: %" PRIu64 "\n", sample->weight);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100837
838 if (sample_type & PERF_SAMPLE_DATA_SRC)
839 printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
Jiri Olsa9ede4732012-10-10 17:38:13 +0200840
Andi Kleen475eeab2013-09-20 07:40:43 -0700841 if (sample_type & PERF_SAMPLE_TRANSACTION)
842 printf("... transaction: %" PRIx64 "\n", sample->transaction);
843
Jiri Olsa9ede4732012-10-10 17:38:13 +0200844 if (sample_type & PERF_SAMPLE_READ)
845 sample_read__printf(sample, evsel->attr.read_format);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +0000846}
847
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200848static struct machine *
849 perf_session__find_machine_for_cpumode(struct perf_session *session,
Adrian Hunteref893252013-08-27 11:23:06 +0300850 union perf_event *event,
851 struct perf_sample *sample)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200852{
853 const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
854
David Ahern7c0f4a42012-07-20 17:25:48 -0600855 if (perf_guest &&
856 ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
857 (cpumode == PERF_RECORD_MISC_GUEST_USER))) {
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530858 u32 pid;
859
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200860 if (event->header.type == PERF_RECORD_MMAP
861 || event->header.type == PERF_RECORD_MMAP2)
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530862 pid = event->mmap.pid;
863 else
Adrian Hunteref893252013-08-27 11:23:06 +0300864 pid = sample->pid;
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530865
David Ahern207b5792012-07-01 16:11:37 -0600866 return perf_session__findnew_machine(session, pid);
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530867 }
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200868
Arnaldo Carvalho de Melo34ba5122012-12-19 09:04:24 -0300869 return &session->machines.host;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200870}
871
Jiri Olsae4caec02012-10-10 18:52:24 +0200872static int deliver_sample_value(struct perf_session *session,
873 struct perf_tool *tool,
874 union perf_event *event,
875 struct perf_sample *sample,
876 struct sample_read_value *v,
877 struct machine *machine)
878{
879 struct perf_sample_id *sid;
880
881 sid = perf_evlist__id2sid(session->evlist, v->id);
882 if (sid) {
883 sample->id = v->id;
884 sample->period = v->value - sid->period;
885 sid->period = v->value;
886 }
887
888 if (!sid || sid->evsel == NULL) {
889 ++session->stats.nr_unknown_id;
890 return 0;
891 }
892
893 return tool->sample(tool, event, sample, sid->evsel, machine);
894}
895
896static int deliver_sample_group(struct perf_session *session,
897 struct perf_tool *tool,
898 union perf_event *event,
899 struct perf_sample *sample,
900 struct machine *machine)
901{
902 int ret = -EINVAL;
903 u64 i;
904
905 for (i = 0; i < sample->read.group.nr; i++) {
906 ret = deliver_sample_value(session, tool, event, sample,
907 &sample->read.group.values[i],
908 machine);
909 if (ret)
910 break;
911 }
912
913 return ret;
914}
915
916static int
917perf_session__deliver_sample(struct perf_session *session,
918 struct perf_tool *tool,
919 union perf_event *event,
920 struct perf_sample *sample,
921 struct perf_evsel *evsel,
922 struct machine *machine)
923{
924 /* We know evsel != NULL. */
925 u64 sample_type = evsel->attr.sample_type;
926 u64 read_format = evsel->attr.read_format;
927
928 /* Standard sample delievery. */
929 if (!(sample_type & PERF_SAMPLE_READ))
930 return tool->sample(tool, event, sample, evsel, machine);
931
932 /* For PERF_SAMPLE_READ we have either single or group mode. */
933 if (read_format & PERF_FORMAT_GROUP)
934 return deliver_sample_group(session, tool, event, sample,
935 machine);
936 else
937 return deliver_sample_value(session, tool, event, sample,
938 &sample->read.one, machine);
939}
940
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100941static int perf_session_deliver_event(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200942 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200943 struct perf_sample *sample,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200944 struct perf_tool *tool,
Thomas Gleixner532e7262010-12-07 12:48:55 +0000945 u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100946{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300947 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200948 struct machine *machine;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300949
Thomas Gleixner532e7262010-12-07 12:48:55 +0000950 dump_event(session, event, file_offset, sample);
951
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200952 evsel = perf_evlist__id2evsel(session->evlist, sample->id);
953 if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) {
954 /*
955 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
956 * because the tools right now may apply filters, discarding
957 * some of the samples. For consistency, in the future we
958 * should have something like nr_filtered_samples and remove
959 * the sample->period from total_sample_period, etc, KISS for
960 * now tho.
961 *
962 * Also testing against NULL allows us to handle files without
963 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
964 * future probably it'll be a good idea to restrict event
965 * processing via perf_session to files with both set.
966 */
967 hists__inc_nr_events(&evsel->hists, event->header.type);
968 }
969
Adrian Hunteref893252013-08-27 11:23:06 +0300970 machine = perf_session__find_machine_for_cpumode(session, event,
971 sample);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200972
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100973 switch (event->header.type) {
974 case PERF_RECORD_SAMPLE:
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200975 dump_sample(evsel, event, sample);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300976 if (evsel == NULL) {
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300977 ++session->stats.nr_unknown_id;
Jiri Olsa67822062012-04-12 14:21:01 +0200978 return 0;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300979 }
Joerg Roedel0c095712012-02-10 18:05:04 +0100980 if (machine == NULL) {
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300981 ++session->stats.nr_unprocessable_samples;
Jiri Olsa67822062012-04-12 14:21:01 +0200982 return 0;
Joerg Roedel0c095712012-02-10 18:05:04 +0100983 }
Jiri Olsae4caec02012-10-10 18:52:24 +0200984 return perf_session__deliver_sample(session, tool, event,
985 sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100986 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200987 return tool->mmap(tool, event, sample, machine);
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200988 case PERF_RECORD_MMAP2:
989 return tool->mmap2(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100990 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200991 return tool->comm(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100992 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200993 return tool->fork(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100994 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200995 return tool->exit(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100996 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200997 if (tool->lost == perf_event__process_lost)
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300998 session->stats.total_lost += event->lost.lost;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200999 return tool->lost(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001000 case PERF_RECORD_READ:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001001 return tool->read(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001002 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001003 return tool->throttle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001004 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001005 return tool->unthrottle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001006 default:
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001007 ++session->stats.nr_unknown_events;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001008 return -1;
1009 }
1010}
1011
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001012static int perf_session__process_user_event(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001013 struct perf_tool *tool, u64 file_offset)
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001014{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001015 int fd = perf_data_file__fd(session->file);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001016 int err;
1017
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001018 dump_event(session, event, file_offset, NULL);
1019
1020 /* These events are processed right away */
1021 switch (event->header.type) {
1022 case PERF_RECORD_HEADER_ATTR:
Adrian Hunter47c3d102013-07-04 16:20:21 +03001023 err = tool->attr(tool, event, &session->evlist);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001024 if (err == 0)
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -03001025 perf_session__set_id_hdr_size(session);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02001026 return err;
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001027 case PERF_RECORD_HEADER_TRACING_DATA:
1028 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001029 lseek(fd, file_offset, SEEK_SET);
Adrian Hunter47c3d102013-07-04 16:20:21 +03001030 return tool->tracing_data(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001031 case PERF_RECORD_HEADER_BUILD_ID:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001032 return tool->build_id(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001033 case PERF_RECORD_FINISHED_ROUND:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001034 return tool->finished_round(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +00001035 default:
1036 return -EINVAL;
1037 }
1038}
1039
Jiri Olsa268fb202012-05-30 14:23:43 +02001040static void event_swap(union perf_event *event, bool sample_id_all)
1041{
1042 perf_event__swap_op swap;
1043
1044 swap = perf_event__swap_ops[event->header.type];
1045 if (swap)
1046 swap(event, sample_id_all);
1047}
1048
David Aherne30b88a2013-08-05 21:41:33 -04001049static int perf_session__process_event(struct perf_session *session,
1050 union perf_event *event,
1051 struct perf_tool *tool,
1052 u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001053{
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001054 struct perf_sample sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001055 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001056
Jiri Olsa268fb202012-05-30 14:23:43 +02001057 if (session->header.needs_swap)
Arnaldo Carvalho de Melo5e562472012-08-01 19:25:26 -03001058 event_swap(event, perf_evlist__sample_id_all(session->evlist));
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001059
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001060 if (event->header.type >= PERF_RECORD_HEADER_MAX)
1061 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001062
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001063 events_stats__inc(&session->stats, event->header.type);
Thomas Gleixner9aefcab02010-12-07 12:48:47 +00001064
1065 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001066 return perf_session__process_user_event(session, event, tool, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001067
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001068 /*
1069 * For all kernel events we get the sample data
1070 */
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001071 ret = perf_evlist__parse_sample(session->evlist, event, &sample);
Frederic Weisbecker5538bec2011-05-22 02:17:22 +02001072 if (ret)
1073 return ret;
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +00001074
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001075 if (tool->ordered_samples) {
Thomas Gleixnere4c2df12010-12-07 12:48:50 +00001076 ret = perf_session_queue_event(session, event, &sample,
1077 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +01001078 if (ret != -ETIME)
1079 return ret;
1080 }
1081
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001082 return perf_session_deliver_event(session, event, &sample, tool,
Thomas Gleixnerf74725d2010-12-07 12:48:53 +00001083 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001084}
1085
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001086void perf_event_header__bswap(struct perf_event_header *hdr)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001087{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001088 hdr->type = bswap_32(hdr->type);
1089 hdr->misc = bswap_16(hdr->misc);
1090 hdr->size = bswap_16(hdr->size);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001091}
1092
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001093struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1094{
Adrian Hunter314add62013-08-27 11:23:03 +03001095 return machine__findnew_thread(&session->machines.host, 0, pid);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001096}
1097
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001098static struct thread *perf_session__register_idle_thread(struct perf_session *session)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001099{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001100 struct thread *thread = perf_session__findnew(session, 0);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001101
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001102 if (thread == NULL || thread__set_comm(thread, "swapper", 0)) {
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001103 pr_err("problem inserting idle task.\n");
1104 thread = NULL;
1105 }
1106
1107 return thread;
1108}
1109
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001110static void perf_session__warn_about_errors(const struct perf_session *session,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001111 const struct perf_tool *tool)
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001112{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001113 if (tool->lost == perf_event__process_lost &&
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001114 session->stats.nr_events[PERF_RECORD_LOST] != 0) {
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001115 ui__warning("Processed %d events and lost %d chunks!\n\n"
1116 "Check IO/CPU overload!\n\n",
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001117 session->stats.nr_events[0],
1118 session->stats.nr_events[PERF_RECORD_LOST]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001119 }
1120
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001121 if (session->stats.nr_unknown_events != 0) {
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001122 ui__warning("Found %u unknown events!\n\n"
1123 "Is this an older tool processing a perf.data "
1124 "file generated by a more recent tool?\n\n"
1125 "If that is not the case, consider "
1126 "reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001127 session->stats.nr_unknown_events);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001128 }
1129
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001130 if (session->stats.nr_unknown_id != 0) {
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001131 ui__warning("%u samples with id not present in the header\n",
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001132 session->stats.nr_unknown_id);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001133 }
1134
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001135 if (session->stats.nr_invalid_chains != 0) {
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001136 ui__warning("Found invalid callchains!\n\n"
1137 "%u out of %u events were discarded for this reason.\n\n"
1138 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001139 session->stats.nr_invalid_chains,
1140 session->stats.nr_events[PERF_RECORD_SAMPLE]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001141 }
Joerg Roedel0c095712012-02-10 18:05:04 +01001142
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001143 if (session->stats.nr_unprocessable_samples != 0) {
Joerg Roedel0c095712012-02-10 18:05:04 +01001144 ui__warning("%u unprocessable samples recorded.\n"
1145 "Do you have a KVM guest running and not using 'perf kvm'?\n",
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001146 session->stats.nr_unprocessable_samples);
Joerg Roedel0c095712012-02-10 18:05:04 +01001147 }
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001148}
1149
Tom Zanussi8dc58102010-04-01 23:59:15 -05001150volatile int session_done;
1151
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001152static int __perf_session__process_pipe_events(struct perf_session *session,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001153 struct perf_tool *tool)
Tom Zanussi8dc58102010-04-01 23:59:15 -05001154{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001155 int fd = perf_data_file__fd(session->file);
Stephane Eranian444d2862012-05-15 13:28:12 +02001156 union perf_event *event;
1157 uint32_t size, cur_size = 0;
1158 void *buf = NULL;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001159 int skip = 0;
1160 u64 head;
1161 int err;
1162 void *p;
1163
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001164 perf_tool__fill_defaults(tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001165
1166 head = 0;
Stephane Eranian444d2862012-05-15 13:28:12 +02001167 cur_size = sizeof(union perf_event);
1168
1169 buf = malloc(cur_size);
1170 if (!buf)
1171 return -errno;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001172more:
Stephane Eranian444d2862012-05-15 13:28:12 +02001173 event = buf;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001174 err = readn(fd, event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05001175 if (err <= 0) {
1176 if (err == 0)
1177 goto done;
1178
1179 pr_err("failed to read event header\n");
1180 goto out_err;
1181 }
1182
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001183 if (session->header.needs_swap)
Stephane Eranian444d2862012-05-15 13:28:12 +02001184 perf_event_header__bswap(&event->header);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001185
Stephane Eranian444d2862012-05-15 13:28:12 +02001186 size = event->header.size;
Adrian Hunter27389d72013-07-04 16:20:27 +03001187 if (size < sizeof(struct perf_event_header)) {
1188 pr_err("bad event header size\n");
1189 goto out_err;
1190 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001191
Stephane Eranian444d2862012-05-15 13:28:12 +02001192 if (size > cur_size) {
1193 void *new = realloc(buf, size);
1194 if (!new) {
1195 pr_err("failed to allocate memory to read event\n");
1196 goto out_err;
1197 }
1198 buf = new;
1199 cur_size = size;
1200 event = buf;
1201 }
1202 p = event;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001203 p += sizeof(struct perf_event_header);
1204
Tom Zanussi794e43b2010-05-05 00:27:40 -05001205 if (size - sizeof(struct perf_event_header)) {
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001206 err = readn(fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -05001207 if (err <= 0) {
1208 if (err == 0) {
1209 pr_err("unexpected end of event stream\n");
1210 goto done;
1211 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001212
Tom Zanussi794e43b2010-05-05 00:27:40 -05001213 pr_err("failed to read event data\n");
1214 goto out_err;
1215 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001216 }
1217
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001218 if ((skip = perf_session__process_event(session, event, tool, head)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001219 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
Stephane Eranian444d2862012-05-15 13:28:12 +02001220 head, event->header.size, event->header.type);
Jiri Olsa9389a462012-04-16 20:42:51 +02001221 err = -EINVAL;
1222 goto out_err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001223 }
1224
1225 head += size;
1226
Tom Zanussi8dc58102010-04-01 23:59:15 -05001227 if (skip > 0)
1228 head += skip;
1229
1230 if (!session_done())
1231 goto more;
1232done:
Adrian Hunter8c16b642013-10-18 15:29:02 +03001233 /* do the final flush for ordered samples */
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001234 session->ordered_samples.next_flush = ULLONG_MAX;
1235 err = flush_sample_queue(session, tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001236out_err:
Stephane Eranian444d2862012-05-15 13:28:12 +02001237 free(buf);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001238 perf_session__warn_about_errors(session, tool);
1239 perf_session_free_sample_buffers(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001240 return err;
1241}
1242
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001243static union perf_event *
1244fetch_mmaped_event(struct perf_session *session,
1245 u64 head, size_t mmap_size, char *buf)
1246{
1247 union perf_event *event;
1248
1249 /*
1250 * Ensure we have enough space remaining to read
1251 * the size of the event in the headers.
1252 */
1253 if (head + sizeof(event->header) > mmap_size)
1254 return NULL;
1255
1256 event = (union perf_event *)(buf + head);
1257
1258 if (session->header.needs_swap)
1259 perf_event_header__bswap(&event->header);
1260
Adrian Hunter27389d72013-07-04 16:20:27 +03001261 if (head + event->header.size > mmap_size) {
1262 /* We're not fetching the event so swap back again */
1263 if (session->header.needs_swap)
1264 perf_event_header__bswap(&event->header);
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001265 return NULL;
Adrian Hunter27389d72013-07-04 16:20:27 +03001266 }
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001267
1268 return event;
1269}
1270
David Miller35d48dd2012-11-10 14:12:19 -05001271/*
1272 * On 64bit we can mmap the data file in one go. No need for tiny mmap
1273 * slices. On 32bit we use 32MB.
1274 */
1275#if BITS_PER_LONG == 64
1276#define MMAP_SIZE ULLONG_MAX
1277#define NUM_MMAPS 1
1278#else
1279#define MMAP_SIZE (32 * 1024 * 1024ULL)
1280#define NUM_MMAPS 128
1281#endif
1282
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001283int __perf_session__process_events(struct perf_session *session,
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001284 u64 data_offset, u64 data_size,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001285 u64 file_size, struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001286{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001287 int fd = perf_data_file__fd(session->file);
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001288 u64 head, page_offset, file_offset, file_pos;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001289 int err, mmap_prot, mmap_flags, map_idx = 0;
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -03001290 size_t mmap_size;
David Miller35d48dd2012-11-10 14:12:19 -05001291 char *buf, *mmaps[NUM_MMAPS];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001292 union perf_event *event;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001293 uint32_t size;
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001294 struct ui_progress prog;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001295
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001296 perf_tool__fill_defaults(tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001297
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001298 page_offset = page_size * (data_offset / page_size);
1299 file_offset = page_offset;
1300 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001301
Namhyung Kimb314e5c2013-09-30 17:19:48 +09001302 if (data_size && (data_offset + data_size < file_size))
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001303 file_size = data_offset + data_size;
1304
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001305 ui_progress__init(&prog, file_size, "Processing events...");
Thomas Gleixner55b446292010-11-30 17:49:46 +00001306
David Miller35d48dd2012-11-10 14:12:19 -05001307 mmap_size = MMAP_SIZE;
Thomas Gleixner55b446292010-11-30 17:49:46 +00001308 if (mmap_size > file_size)
1309 mmap_size = file_size;
1310
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001311 memset(mmaps, 0, sizeof(mmaps));
1312
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001313 mmap_prot = PROT_READ;
1314 mmap_flags = MAP_SHARED;
1315
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001316 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001317 mmap_prot |= PROT_WRITE;
1318 mmap_flags = MAP_PRIVATE;
1319 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001320remap:
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001321 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd,
Thomas Gleixner55b446292010-11-30 17:49:46 +00001322 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001323 if (buf == MAP_FAILED) {
1324 pr_err("failed to mmap file\n");
1325 err = -errno;
1326 goto out_err;
1327 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001328 mmaps[map_idx] = buf;
1329 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001330 file_pos = file_offset + head;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001331
1332more:
Frederic Weisbecker998bedc2011-05-23 13:06:28 +02001333 event = fetch_mmaped_event(session, head, mmap_size, buf);
1334 if (!event) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001335 if (mmaps[map_idx]) {
1336 munmap(mmaps[map_idx], mmap_size);
1337 mmaps[map_idx] = NULL;
1338 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001339
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001340 page_offset = page_size * (head / page_size);
1341 file_offset += page_offset;
1342 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001343 goto remap;
1344 }
1345
1346 size = event->header.size;
1347
Adrian Hunter27389d72013-07-04 16:20:27 +03001348 if (size < sizeof(struct perf_event_header) ||
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001349 perf_session__process_event(session, event, tool, file_pos) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001350 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1351 file_offset + head, event->header.size,
1352 event->header.type);
1353 err = -EINVAL;
1354 goto out_err;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001355 }
1356
1357 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001358 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001359
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001360 ui_progress__update(&prog, size);
Thomas Gleixner55b446292010-11-30 17:49:46 +00001361
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001362 if (session_done())
Adrian Hunter8c16b642013-10-18 15:29:02 +03001363 goto out;
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001364
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001365 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001366 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001367
Adrian Hunter8c16b642013-10-18 15:29:02 +03001368out:
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001369 /* do the final flush for ordered samples */
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001370 session->ordered_samples.next_flush = ULLONG_MAX;
David Ahernd25380cd2012-08-26 12:24:41 -06001371 err = flush_sample_queue(session, tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001372out_err:
Namhyung Kima5580f32012-11-13 22:30:34 +09001373 ui_progress__finish();
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001374 perf_session__warn_about_errors(session, tool);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001375 perf_session_free_sample_buffers(session);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001376 return err;
1377}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001378
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001379int perf_session__process_events(struct perf_session *session,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001380 struct perf_tool *tool)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001381{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001382 u64 size = perf_data_file__size(session->file);
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001383 int err;
1384
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001385 if (perf_session__register_idle_thread(session) == NULL)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001386 return -ENOMEM;
1387
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001388 if (!perf_data_file__is_pipe(session->file))
1389 err = __perf_session__process_events(session,
1390 session->header.data_offset,
1391 session->header.data_size,
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001392 size, tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001393 else
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001394 err = __perf_session__process_pipe_events(session, tool);
Dave Martin88ca8952010-07-27 11:46:12 -03001395
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001396 return err;
1397}
1398
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001399bool perf_session__has_traces(struct perf_session *session, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001400{
David Ahern93ea01c292013-08-07 22:50:58 -04001401 struct perf_evsel *evsel;
1402
1403 list_for_each_entry(evsel, &session->evlist->entries, node) {
1404 if (evsel->attr.type == PERF_TYPE_TRACEPOINT)
1405 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001406 }
1407
David Ahern93ea01c292013-08-07 22:50:58 -04001408 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1409 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001410}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001411
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001412int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1413 const char *symbol_name, u64 addr)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001414{
1415 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001416 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001417 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001418
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001419 ref = zalloc(sizeof(struct ref_reloc_sym));
1420 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001421 return -ENOMEM;
1422
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001423 ref->name = strdup(symbol_name);
1424 if (ref->name == NULL) {
1425 free(ref);
1426 return -ENOMEM;
1427 }
1428
1429 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001430 if (bracket)
1431 *bracket = '\0';
1432
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001433 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001434
1435 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001436 struct kmap *kmap = map__kmap(maps[i]);
1437 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001438 }
1439
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001440 return 0;
1441}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001442
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001443size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001444{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001445 return machines__fprintf_dsos(&session->machines, fp);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001446}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001447
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001448size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001449 bool (skip)(struct dso *dso, int parm), int parm)
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001450{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001451 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001452}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001453
1454size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1455{
1456 struct perf_evsel *pos;
1457 size_t ret = fprintf(fp, "Aggregated stats:\n");
1458
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001459 ret += events_stats__fprintf(&session->stats, fp);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001460
1461 list_for_each_entry(pos, &session->evlist->entries, node) {
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -03001462 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
Arnaldo Carvalho de Melo52168ee2012-12-18 16:02:17 -03001463 ret += events_stats__fprintf(&pos->hists.stats, fp);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001464 }
1465
1466 return ret;
1467}
David Ahernc0230b22011-03-09 22:23:27 -07001468
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001469size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1470{
1471 /*
1472 * FIXME: Here we have to actually print all the machines in this
1473 * session, not just the host...
1474 */
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001475 return machine__fprintf(&session->machines.host, fp);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001476}
1477
David Ahern9cbdb702011-04-06 21:54:20 -06001478struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1479 unsigned int type)
1480{
1481 struct perf_evsel *pos;
1482
1483 list_for_each_entry(pos, &session->evlist->entries, node) {
1484 if (pos->attr.type == type)
1485 return pos;
1486 }
1487 return NULL;
1488}
1489
Jiri Olsa71ad0f52012-08-07 15:20:46 +02001490void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
1491 struct perf_sample *sample, struct machine *machine,
David Ahern307cbb92013-08-07 22:50:53 -04001492 unsigned int print_opts, unsigned int stack_depth)
David Ahernc0230b22011-03-09 22:23:27 -07001493{
1494 struct addr_location al;
David Ahernc0230b22011-03-09 22:23:27 -07001495 struct callchain_cursor_node *node;
David Aherna6ffaf92013-08-07 22:50:51 -04001496 int print_ip = print_opts & PRINT_IP_OPT_IP;
1497 int print_sym = print_opts & PRINT_IP_OPT_SYM;
1498 int print_dso = print_opts & PRINT_IP_OPT_DSO;
1499 int print_symoffset = print_opts & PRINT_IP_OPT_SYMOFFSET;
David Ahernb0b35f02013-08-07 22:50:52 -04001500 int print_oneline = print_opts & PRINT_IP_OPT_ONELINE;
1501 char s = print_oneline ? ' ' : '\t';
David Ahernc0230b22011-03-09 22:23:27 -07001502
Adrian Huntere44baa32013-08-08 14:32:25 +03001503 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
David Ahernc0230b22011-03-09 22:23:27 -07001504 error("problem processing %d event, skipping it.\n",
1505 event->header.type);
1506 return;
1507 }
1508
1509 if (symbol_conf.use_callchain && sample->callchain) {
1510
Jiri Olsa71ad0f52012-08-07 15:20:46 +02001511 if (machine__resolve_callchain(machine, evsel, al.thread,
Waiman Long91e95612013-10-18 10:38:48 -04001512 sample, NULL, NULL,
1513 PERF_MAX_STACK_DEPTH) != 0) {
David Ahernc0230b22011-03-09 22:23:27 -07001514 if (verbose)
1515 error("Failed to resolve callchain. Skipping\n");
1516 return;
1517 }
Namhyung Kim47260642012-05-31 14:43:26 +09001518 callchain_cursor_commit(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001519
David Ahern307cbb92013-08-07 22:50:53 -04001520 while (stack_depth) {
Namhyung Kim47260642012-05-31 14:43:26 +09001521 node = callchain_cursor_current(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001522 if (!node)
1523 break;
1524
David Aherna6ffaf92013-08-07 22:50:51 -04001525 if (print_ip)
David Ahernb0b35f02013-08-07 22:50:52 -04001526 printf("%c%16" PRIx64, s, node->ip);
David Aherna6ffaf92013-08-07 22:50:51 -04001527
David Ahern787bef12011-05-27 14:28:43 -06001528 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001529 printf(" ");
David Ahern251f4262013-07-28 09:14:34 -06001530 if (print_symoffset) {
1531 al.addr = node->ip;
David Ahern0b8c25d2013-07-28 09:48:32 -06001532 al.map = node->map;
David Ahern251f4262013-07-28 09:14:34 -06001533 symbol__fprintf_symname_offs(node->sym, &al, stdout);
1534 } else
1535 symbol__fprintf_symname(node->sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001536 }
David Ahern251f4262013-07-28 09:14:34 -06001537
David Ahern610723f2011-05-27 14:28:44 -06001538 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001539 printf(" (");
David Ahern52deff72012-05-29 22:58:26 -06001540 map__fprintf_dsoname(node->map, stdout);
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001541 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001542 }
David Ahernb0b35f02013-08-07 22:50:52 -04001543
1544 if (!print_oneline)
1545 printf("\n");
David Ahernc0230b22011-03-09 22:23:27 -07001546
Namhyung Kim47260642012-05-31 14:43:26 +09001547 callchain_cursor_advance(&callchain_cursor);
David Ahern307cbb92013-08-07 22:50:53 -04001548
1549 stack_depth--;
David Ahernc0230b22011-03-09 22:23:27 -07001550 }
1551
1552 } else {
David Aherna6ffaf92013-08-07 22:50:51 -04001553 if (print_ip)
1554 printf("%16" PRIx64, sample->ip);
1555
David Ahern787bef12011-05-27 14:28:43 -06001556 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001557 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001558 if (print_symoffset)
1559 symbol__fprintf_symname_offs(al.sym, &al,
1560 stdout);
1561 else
1562 symbol__fprintf_symname(al.sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001563 }
1564
1565 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001566 printf(" (");
1567 map__fprintf_dsoname(al.map, stdout);
1568 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001569 }
David Ahernc0230b22011-03-09 22:23:27 -07001570 }
1571}
Anton Blanchard5d67be92011-07-04 21:57:50 +10001572
1573int perf_session__cpu_bitmap(struct perf_session *session,
1574 const char *cpu_list, unsigned long *cpu_bitmap)
1575{
1576 int i;
1577 struct cpu_map *map;
1578
1579 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1580 struct perf_evsel *evsel;
1581
1582 evsel = perf_session__find_first_evtype(session, i);
1583 if (!evsel)
1584 continue;
1585
1586 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1587 pr_err("File does not contain CPU events. "
1588 "Remove -c option to proceed.\n");
1589 return -1;
1590 }
1591 }
1592
1593 map = cpu_map__new(cpu_list);
David Ahern47fbe532011-11-13 10:45:27 -07001594 if (map == NULL) {
1595 pr_err("Invalid cpu_list\n");
1596 return -1;
1597 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001598
1599 for (i = 0; i < map->nr; i++) {
1600 int cpu = map->map[i];
1601
1602 if (cpu >= MAX_NR_CPUS) {
1603 pr_err("Requested CPU %d too large. "
1604 "Consider raising MAX_NR_CPUS\n", cpu);
1605 return -1;
1606 }
1607
1608 set_bit(cpu, cpu_bitmap);
1609 }
1610
1611 return 0;
1612}
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001613
1614void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1615 bool full)
1616{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001617 int fd = perf_data_file__fd(session->file);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001618 struct stat st;
1619 int ret;
1620
1621 if (session == NULL || fp == NULL)
1622 return;
1623
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001624 ret = fstat(fd, &st);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001625 if (ret == -1)
1626 return;
1627
1628 fprintf(fp, "# ========\n");
1629 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1630 perf_header__fprintf_info(session, fp, full);
1631 fprintf(fp, "# ========\n#\n");
1632}
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001633
1634
1635int __perf_session__set_tracepoints_handlers(struct perf_session *session,
1636 const struct perf_evsel_str_handler *assocs,
1637 size_t nr_assocs)
1638{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001639 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001640 size_t i;
1641 int err;
1642
1643 for (i = 0; i < nr_assocs; i++) {
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001644 /*
1645 * Adding a handler for an event not in the session,
1646 * just ignore it.
1647 */
1648 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name);
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001649 if (evsel == NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001650 continue;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001651
1652 err = -EEXIST;
1653 if (evsel->handler.func != NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001654 goto out;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001655 evsel->handler.func = assocs[i].handler;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001656 }
1657
1658 err = 0;
1659out:
1660 return err;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001661}