blob: 0ccf051247f629099a7aace8cdf697c78dd0ebb1 [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"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020017
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030018static int perf_session__open(struct perf_session *session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020019{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030020 struct perf_data_file *file = session->file;
Tom Zanussi8dc58102010-04-01 23:59:15 -050021
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030022 if (perf_session__read_header(session) < 0) {
Stephane Eranian69996df2012-02-09 23:21:06 +010023 pr_err("incompatible file format (rerun with -v to learn more)");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020024 return -1;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020025 }
26
Jiri Olsacc9784bd2013-10-15 16:27:34 +020027 if (perf_data_file__is_pipe(file))
28 return 0;
29
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030030 if (!perf_evlist__valid_sample_type(session->evlist)) {
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030031 pr_err("non matching sample_type");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020032 return -1;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030033 }
34
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030035 if (!perf_evlist__valid_sample_id_all(session->evlist)) {
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030036 pr_err("non matching sample_id_all");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020037 return -1;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030038 }
39
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030040 if (!perf_evlist__valid_read_format(session->evlist)) {
Jiri Olsa9ede4732012-10-10 17:38:13 +020041 pr_err("non matching read_format");
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020042 return -1;
Jiri Olsa9ede4732012-10-10 17:38:13 +020043 }
44
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020045 return 0;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020046}
47
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030048void perf_session__set_id_hdr_size(struct perf_session *session)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020049{
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030050 u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist);
51
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -030052 machines__set_id_hdr_size(&session->machines, id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020053}
54
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030055int perf_session__create_kernel_maps(struct perf_session *session)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080056{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030057 int ret = machine__create_kernel_maps(&session->machines.host);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080058
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080059 if (ret >= 0)
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030060 ret = machines__create_guest_kernel_maps(&session->machines);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080061 return ret;
62}
63
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030064static void perf_session__destroy_kernel_maps(struct perf_session *session)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -030065{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030066 machines__destroy_kernel_maps(&session->machines);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -030067}
68
Jiri Olsaf5fc1412013-10-15 16:27:32 +020069struct perf_session *perf_session__new(struct perf_data_file *file,
70 bool repipe, struct perf_tool *tool)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020071{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030072 struct perf_session *session = zalloc(sizeof(*session));
Robert Richterefad1412011-12-07 10:02:54 +010073
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030074 if (!session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020075 goto out;
76
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030077 session->repipe = repipe;
Jiri Olsafc124822014-06-09 23:11:30 +020078 INIT_LIST_HEAD(&session->ordered_events.events);
79 INIT_LIST_HEAD(&session->ordered_events.cache);
Jiri Olsa37e39aa2014-07-06 14:23:03 +020080 INIT_LIST_HEAD(&session->ordered_events.to_free);
Jiri Olsa8d99a6c2014-06-11 15:09:35 +020081 session->ordered_events.max_alloc_size = (u64) -1;
82 session->ordered_events.cur_alloc_size = 0;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030083 machines__init(&session->machines);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020084
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020085 if (file) {
86 if (perf_data_file__open(file))
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -020087 goto out_delete;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020088
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030089 session->file = file;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020090
91 if (perf_data_file__is_read(file)) {
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030092 if (perf_session__open(session) < 0)
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020093 goto out_close;
94
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030095 perf_session__set_id_hdr_size(session);
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020096 }
97 }
98
99 if (!file || perf_data_file__is_write(file)) {
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200100 /*
101 * In O_RDONLY mode this will be performed when reading the
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200102 * kernel MMAP event, in perf_event__process_mmap().
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200103 */
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300104 if (perf_session__create_kernel_maps(session) < 0)
Arnaldo Carvalho de Melo64abebf72010-01-27 21:05:52 -0200105 goto out_delete;
106 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200107
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200108 if (tool && tool->ordering_requires_timestamps &&
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200109 tool->ordered_events && !perf_evlist__sample_id_all(session->evlist)) {
Ian Munsie21ef97f2010-12-10 14:09:16 +1100110 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200111 tool->ordered_events = false;
Ian Munsie21ef97f2010-12-10 14:09:16 +1100112 }
113
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300114 return session;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200115
116 out_close:
117 perf_data_file__close(file);
118 out_delete:
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300119 perf_session__delete(session);
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200120 out:
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200121 return NULL;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200122}
123
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200124static void perf_session__delete_dead_threads(struct perf_session *session)
125{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300126 machine__delete_dead_threads(&session->machines.host);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200127}
128
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200129static void perf_session__delete_threads(struct perf_session *session)
130{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300131 machine__delete_threads(&session->machines.host);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200132}
133
Namhyung Kim03cd2092012-11-21 13:43:19 +0900134static void perf_session_env__delete(struct perf_session_env *env)
135{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300136 zfree(&env->hostname);
137 zfree(&env->os_release);
138 zfree(&env->version);
139 zfree(&env->arch);
140 zfree(&env->cpu_desc);
141 zfree(&env->cpuid);
Namhyung Kim03cd2092012-11-21 13:43:19 +0900142
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300143 zfree(&env->cmdline);
144 zfree(&env->sibling_cores);
145 zfree(&env->sibling_threads);
146 zfree(&env->numa_nodes);
147 zfree(&env->pmu_mappings);
Namhyung Kim03cd2092012-11-21 13:43:19 +0900148}
149
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300150void perf_session__delete(struct perf_session *session)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200151{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300152 perf_session__destroy_kernel_maps(session);
153 perf_session__delete_dead_threads(session);
154 perf_session__delete_threads(session);
155 perf_session_env__delete(&session->header.env);
156 machines__exit(&session->machines);
157 if (session->file)
158 perf_data_file__close(session->file);
159 free(session);
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) {
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200243 if (tool->ordered_events)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200244 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
Jiri Olsa268fb202012-05-30 14:23:43 +0200250static void swap_sample_id_all(union perf_event *event, void *data)
251{
252 void *end = (void *) event + event->header.size;
253 int size = end - data;
254
255 BUG_ON(size % sizeof(u64));
256 mem_bswap_64(data, size);
257}
258
259static void perf_event__all64_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300260 bool sample_id_all __maybe_unused)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200261{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200262 struct perf_event_header *hdr = &event->header;
263 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200264}
265
Jiri Olsa268fb202012-05-30 14:23:43 +0200266static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200267{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200268 event->comm.pid = bswap_32(event->comm.pid);
269 event->comm.tid = bswap_32(event->comm.tid);
Jiri Olsa268fb202012-05-30 14:23:43 +0200270
271 if (sample_id_all) {
272 void *data = &event->comm.comm;
273
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300274 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200275 swap_sample_id_all(event, data);
276 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200277}
278
Jiri Olsa268fb202012-05-30 14:23:43 +0200279static void perf_event__mmap_swap(union perf_event *event,
280 bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200281{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200282 event->mmap.pid = bswap_32(event->mmap.pid);
283 event->mmap.tid = bswap_32(event->mmap.tid);
284 event->mmap.start = bswap_64(event->mmap.start);
285 event->mmap.len = bswap_64(event->mmap.len);
286 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
Jiri Olsa268fb202012-05-30 14:23:43 +0200287
288 if (sample_id_all) {
289 void *data = &event->mmap.filename;
290
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300291 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
Jiri Olsa268fb202012-05-30 14:23:43 +0200292 swap_sample_id_all(event, data);
293 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200294}
295
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200296static void perf_event__mmap2_swap(union perf_event *event,
297 bool sample_id_all)
298{
299 event->mmap2.pid = bswap_32(event->mmap2.pid);
300 event->mmap2.tid = bswap_32(event->mmap2.tid);
301 event->mmap2.start = bswap_64(event->mmap2.start);
302 event->mmap2.len = bswap_64(event->mmap2.len);
303 event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
304 event->mmap2.maj = bswap_32(event->mmap2.maj);
305 event->mmap2.min = bswap_32(event->mmap2.min);
306 event->mmap2.ino = bswap_64(event->mmap2.ino);
307
308 if (sample_id_all) {
309 void *data = &event->mmap2.filename;
310
311 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
312 swap_sample_id_all(event, data);
313 }
314}
Jiri Olsa268fb202012-05-30 14:23:43 +0200315static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200316{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200317 event->fork.pid = bswap_32(event->fork.pid);
318 event->fork.tid = bswap_32(event->fork.tid);
319 event->fork.ppid = bswap_32(event->fork.ppid);
320 event->fork.ptid = bswap_32(event->fork.ptid);
321 event->fork.time = bswap_64(event->fork.time);
Jiri Olsa268fb202012-05-30 14:23:43 +0200322
323 if (sample_id_all)
324 swap_sample_id_all(event, &event->fork + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200325}
326
Jiri Olsa268fb202012-05-30 14:23:43 +0200327static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200328{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200329 event->read.pid = bswap_32(event->read.pid);
330 event->read.tid = bswap_32(event->read.tid);
331 event->read.value = bswap_64(event->read.value);
332 event->read.time_enabled = bswap_64(event->read.time_enabled);
333 event->read.time_running = bswap_64(event->read.time_running);
334 event->read.id = bswap_64(event->read.id);
Jiri Olsa268fb202012-05-30 14:23:43 +0200335
336 if (sample_id_all)
337 swap_sample_id_all(event, &event->read + 1);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200338}
339
Jiri Olsadd96c462013-09-01 12:36:15 +0200340static void perf_event__throttle_swap(union perf_event *event,
341 bool sample_id_all)
342{
343 event->throttle.time = bswap_64(event->throttle.time);
344 event->throttle.id = bswap_64(event->throttle.id);
345 event->throttle.stream_id = bswap_64(event->throttle.stream_id);
346
347 if (sample_id_all)
348 swap_sample_id_all(event, &event->throttle + 1);
349}
350
Jiri Olsae108c662012-05-16 08:59:03 +0200351static u8 revbyte(u8 b)
352{
353 int rev = (b >> 4) | ((b & 0xf) << 4);
354 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
355 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
356 return (u8) rev;
357}
358
359/*
360 * XXX this is hack in attempt to carry flags bitfield
361 * throught endian village. ABI says:
362 *
363 * Bit-fields are allocated from right to left (least to most significant)
364 * on little-endian implementations and from left to right (most to least
365 * significant) on big-endian implementations.
366 *
367 * The above seems to be byte specific, so we need to reverse each
368 * byte of the bitfield. 'Internet' also says this might be implementation
369 * specific and we probably need proper fix and carry perf_event_attr
370 * bitfield flags in separate data file FEAT_ section. Thought this seems
371 * to work for now.
372 */
373static void swap_bitfield(u8 *p, unsigned len)
374{
375 unsigned i;
376
377 for (i = 0; i < len; i++) {
378 *p = revbyte(*p);
379 p++;
380 }
381}
382
David Aherneda39132011-07-15 12:34:09 -0600383/* exported for swapping attributes in file header */
384void perf_event__attr_swap(struct perf_event_attr *attr)
385{
386 attr->type = bswap_32(attr->type);
387 attr->size = bswap_32(attr->size);
388 attr->config = bswap_64(attr->config);
389 attr->sample_period = bswap_64(attr->sample_period);
390 attr->sample_type = bswap_64(attr->sample_type);
391 attr->read_format = bswap_64(attr->read_format);
392 attr->wakeup_events = bswap_32(attr->wakeup_events);
393 attr->bp_type = bswap_32(attr->bp_type);
394 attr->bp_addr = bswap_64(attr->bp_addr);
395 attr->bp_len = bswap_64(attr->bp_len);
Adrian Hunter7db59522013-10-18 15:29:03 +0300396 attr->branch_sample_type = bswap_64(attr->branch_sample_type);
397 attr->sample_regs_user = bswap_64(attr->sample_regs_user);
398 attr->sample_stack_user = bswap_32(attr->sample_stack_user);
Jiri Olsae108c662012-05-16 08:59:03 +0200399
400 swap_bitfield((u8 *) (&attr->read_format + 1), sizeof(u64));
David Aherneda39132011-07-15 12:34:09 -0600401}
402
Jiri Olsa268fb202012-05-30 14:23:43 +0200403static void perf_event__hdr_attr_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300404 bool sample_id_all __maybe_unused)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500405{
406 size_t size;
407
David Aherneda39132011-07-15 12:34:09 -0600408 perf_event__attr_swap(&event->attr.attr);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500409
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200410 size = event->header.size;
411 size -= (void *)&event->attr.id - (void *)event;
412 mem_bswap_64(event->attr.id, size);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500413}
414
Jiri Olsa268fb202012-05-30 14:23:43 +0200415static void perf_event__event_type_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300416 bool sample_id_all __maybe_unused)
Tom Zanussicd19a032010-04-01 23:59:20 -0500417{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200418 event->event_type.event_type.event_id =
419 bswap_64(event->event_type.event_type.event_id);
Tom Zanussicd19a032010-04-01 23:59:20 -0500420}
421
Jiri Olsa268fb202012-05-30 14:23:43 +0200422static void perf_event__tracing_data_swap(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300423 bool sample_id_all __maybe_unused)
Tom Zanussi92155452010-04-01 23:59:21 -0500424{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200425 event->tracing_data.size = bswap_32(event->tracing_data.size);
Tom Zanussi92155452010-04-01 23:59:21 -0500426}
427
Jiri Olsa268fb202012-05-30 14:23:43 +0200428typedef void (*perf_event__swap_op)(union perf_event *event,
429 bool sample_id_all);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200430
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200431static perf_event__swap_op perf_event__swap_ops[] = {
432 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200433 [PERF_RECORD_MMAP2] = perf_event__mmap2_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200434 [PERF_RECORD_COMM] = perf_event__comm_swap,
435 [PERF_RECORD_FORK] = perf_event__task_swap,
436 [PERF_RECORD_EXIT] = perf_event__task_swap,
437 [PERF_RECORD_LOST] = perf_event__all64_swap,
438 [PERF_RECORD_READ] = perf_event__read_swap,
Jiri Olsadd96c462013-09-01 12:36:15 +0200439 [PERF_RECORD_THROTTLE] = perf_event__throttle_swap,
440 [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200441 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
David Aherneda39132011-07-15 12:34:09 -0600442 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200443 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
444 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
445 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
446 [PERF_RECORD_HEADER_MAX] = NULL,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200447};
448
Thomas Gleixner020bb752010-11-30 17:49:53 +0000449static void perf_session_free_sample_buffers(struct perf_session *session)
450{
Jiri Olsa37e39aa2014-07-06 14:23:03 +0200451 struct ordered_events *oe = &session->ordered_events;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000452
Jiri Olsa37e39aa2014-07-06 14:23:03 +0200453 while (!list_empty(&oe->to_free)) {
454 struct ordered_event *event;
Thomas Gleixner020bb752010-11-30 17:49:53 +0000455
Jiri Olsa37e39aa2014-07-06 14:23:03 +0200456 event = list_entry(oe->to_free.next, struct ordered_event, list);
457 list_del(&event->list);
458 free(event);
Thomas Gleixner020bb752010-11-30 17:49:53 +0000459 }
460}
461
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200462/*
463 * When perf record finishes a pass on every buffers, it records this pseudo
464 * event.
465 * We record the max timestamp t found in the pass n.
466 * Assuming these timestamps are monotonic across cpus, we know that if
467 * a buffer still has events with timestamps below t, they will be all
468 * available and then read in the pass n + 1.
469 * Hence when we start to read the pass n + 2, we can safely flush every
470 * events with timestamps below t.
471 *
472 * ============ PASS n =================
473 * CPU 0 | CPU 1
474 * |
475 * cnt1 timestamps | cnt2 timestamps
476 * 1 | 2
477 * 2 | 3
478 * - | 4 <--- max recorded
479 *
480 * ============ PASS n + 1 ==============
481 * CPU 0 | CPU 1
482 * |
483 * cnt1 timestamps | cnt2 timestamps
484 * 3 | 5
485 * 4 | 6
486 * 5 | 7 <---- max recorded
487 *
488 * Flush every events below timestamp 4
489 *
490 * ============ PASS n + 2 ==============
491 * CPU 0 | CPU 1
492 * |
493 * cnt1 timestamps | cnt2 timestamps
494 * 6 | 8
495 * 7 | 9
496 * - | 10
497 *
498 * Flush every events below timestamp 7
499 * etc...
500 */
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200501static int process_finished_round(struct perf_tool *tool,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300502 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200503 struct perf_session *session)
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200504{
Jiri Olsad8836b52014-06-05 10:29:45 +0200505 return ordered_events__flush(session, tool, OE_FLUSH__ROUND);
Frederic Weisbeckerd6b17be2010-05-03 15:14:33 +0200506}
507
David Aherne30b88a2013-08-05 21:41:33 -0400508int perf_session_queue_event(struct perf_session *s, union perf_event *event,
Jiri Olsad40b4a12014-08-01 13:01:04 -0300509 struct perf_tool *tool, struct perf_sample *sample,
510 u64 file_offset)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200511{
Jiri Olsa37e39aa2014-07-06 14:23:03 +0200512 struct ordered_events *oe = &s->ordered_events;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200513 u64 timestamp = sample->time;
Jiri Olsa37e39aa2014-07-06 14:23:03 +0200514 struct ordered_event *new;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200515
Thomas Gleixner79a14c12010-12-07 12:48:44 +0000516 if (!timestamp || timestamp == ~0ULL)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100517 return -ETIME;
518
Jiri Olsa37e39aa2014-07-06 14:23:03 +0200519 if (timestamp < s->ordered_events.last_flush) {
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200520 printf("Warning: Timestamp below last timeslice flush\n");
521 return -EINVAL;
522 }
523
Jiri Olsac64c7e12014-06-10 21:58:02 +0200524 new = ordered_events__new(oe, timestamp);
Jiri Olsad40b4a12014-08-01 13:01:04 -0300525 if (!new) {
526 ordered_events__flush(s, tool, OE_FLUSH__HALF);
527 new = ordered_events__new(oe, timestamp);
528 }
529
Jiri Olsac64c7e12014-06-10 21:58:02 +0200530 if (!new)
531 return -ENOMEM;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200532
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000533 new->file_offset = file_offset;
Thomas Gleixnerfe174202010-11-30 17:49:49 +0000534 new->event = event;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200535 return 0;
536}
537
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200538static void callchain__printf(struct perf_sample *sample)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200539{
540 unsigned int i;
541
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200542 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200543
544 for (i = 0; i < sample->callchain->nr; i++)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200545 printf("..... %2d: %016" PRIx64 "\n",
546 i, sample->callchain->ips[i]);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200547}
548
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100549static void branch_stack__printf(struct perf_sample *sample)
550{
551 uint64_t i;
552
553 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
554
555 for (i = 0; i < sample->branch_stack->nr; i++)
556 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n",
557 i, sample->branch_stack->entries[i].from,
558 sample->branch_stack->entries[i].to);
559}
560
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200561static void regs_dump__printf(u64 mask, u64 *regs)
562{
563 unsigned rid, i = 0;
564
565 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
566 u64 val = regs[i++];
567
568 printf(".... %-5s 0x%" PRIx64 "\n",
569 perf_reg_name(rid), val);
570 }
571}
572
Jiri Olsa352ea452014-01-07 13:47:25 +0100573static void regs_user__printf(struct perf_sample *sample)
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200574{
575 struct regs_dump *user_regs = &sample->user_regs;
576
577 if (user_regs->regs) {
Jiri Olsa352ea452014-01-07 13:47:25 +0100578 u64 mask = user_regs->mask;
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200579 printf("... user regs: mask 0x%" PRIx64 "\n", mask);
580 regs_dump__printf(mask, user_regs->regs);
581 }
582}
583
584static void stack_user__printf(struct stack_dump *dump)
585{
586 printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
587 dump->size, dump->offset);
588}
589
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200590static void perf_session__print_tstamp(struct perf_session *session,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200591 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200592 struct perf_sample *sample)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200593{
Adrian Hunter75562572013-08-27 11:23:09 +0300594 u64 sample_type = __perf_evlist__combined_sample_type(session->evlist);
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300595
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200596 if (event->header.type != PERF_RECORD_SAMPLE &&
Arnaldo Carvalho de Melo5e562472012-08-01 19:25:26 -0300597 !perf_evlist__sample_id_all(session->evlist)) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200598 fputs("-1 -1 ", stdout);
599 return;
600 }
601
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300602 if ((sample_type & PERF_SAMPLE_CPU))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200603 printf("%u ", sample->cpu);
604
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300605 if (sample_type & PERF_SAMPLE_TIME)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200606 printf("%" PRIu64 " ", sample->time);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200607}
608
Jiri Olsa9ede4732012-10-10 17:38:13 +0200609static void sample_read__printf(struct perf_sample *sample, u64 read_format)
610{
611 printf("... sample_read:\n");
612
613 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
614 printf("...... time enabled %016" PRIx64 "\n",
615 sample->read.time_enabled);
616
617 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
618 printf("...... time running %016" PRIx64 "\n",
619 sample->read.time_running);
620
621 if (read_format & PERF_FORMAT_GROUP) {
622 u64 i;
623
624 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
625
626 for (i = 0; i < sample->read.group.nr; i++) {
627 struct sample_read_value *value;
628
629 value = &sample->read.group.values[i];
630 printf("..... id %016" PRIx64
631 ", value %016" PRIx64 "\n",
632 value->id, value->value);
633 }
634 } else
635 printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
636 sample->read.one.id, sample->read.one.value);
637}
638
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200639static void dump_event(struct perf_session *session, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200640 u64 file_offset, struct perf_sample *sample)
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000641{
642 if (!dump_trace)
643 return;
644
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200645 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
646 file_offset, event->header.size, event->header.type);
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000647
648 trace_event(event);
649
650 if (sample)
651 perf_session__print_tstamp(session, event, sample);
652
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200653 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200654 event->header.size, perf_event__name(event->header.type));
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000655}
656
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200657static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200658 struct perf_sample *sample)
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000659{
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300660 u64 sample_type;
661
Arnaldo Carvalho de Meloddbc24b72010-12-09 12:20:20 -0200662 if (!dump_trace)
663 return;
664
Don Zickus0ea590a2014-02-25 22:43:46 -0500665 printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200666 event->header.misc, sample->pid, sample->tid, sample->ip,
David Ahern7cec0922011-05-30 13:08:23 -0600667 sample->period, sample->addr);
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000668
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200669 sample_type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300670
671 if (sample_type & PERF_SAMPLE_CALLCHAIN)
Arnaldo Carvalho de Meloddbc24b72010-12-09 12:20:20 -0200672 callchain__printf(sample);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100673
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300674 if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100675 branch_stack__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200676
677 if (sample_type & PERF_SAMPLE_REGS_USER)
Jiri Olsa352ea452014-01-07 13:47:25 +0100678 regs_user__printf(sample);
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200679
680 if (sample_type & PERF_SAMPLE_STACK_USER)
681 stack_user__printf(&sample->user_stack);
Andi Kleen05484292013-01-24 16:10:29 +0100682
683 if (sample_type & PERF_SAMPLE_WEIGHT)
684 printf("... weight: %" PRIu64 "\n", sample->weight);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100685
686 if (sample_type & PERF_SAMPLE_DATA_SRC)
687 printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
Jiri Olsa9ede4732012-10-10 17:38:13 +0200688
Andi Kleen475eeab2013-09-20 07:40:43 -0700689 if (sample_type & PERF_SAMPLE_TRANSACTION)
690 printf("... transaction: %" PRIx64 "\n", sample->transaction);
691
Jiri Olsa9ede4732012-10-10 17:38:13 +0200692 if (sample_type & PERF_SAMPLE_READ)
693 sample_read__printf(sample, evsel->attr.read_format);
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000694}
695
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200696static struct machine *
697 perf_session__find_machine_for_cpumode(struct perf_session *session,
Adrian Hunteref893252013-08-27 11:23:06 +0300698 union perf_event *event,
699 struct perf_sample *sample)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200700{
701 const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Dongsheng Yangad85ace2013-12-20 13:41:47 -0500702 struct machine *machine;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200703
David Ahern7c0f4a42012-07-20 17:25:48 -0600704 if (perf_guest &&
705 ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
706 (cpumode == PERF_RECORD_MISC_GUEST_USER))) {
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530707 u32 pid;
708
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200709 if (event->header.type == PERF_RECORD_MMAP
710 || event->header.type == PERF_RECORD_MMAP2)
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530711 pid = event->mmap.pid;
712 else
Adrian Hunteref893252013-08-27 11:23:06 +0300713 pid = sample->pid;
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530714
Dongsheng Yangad85ace2013-12-20 13:41:47 -0500715 machine = perf_session__find_machine(session, pid);
716 if (!machine)
717 machine = perf_session__findnew_machine(session,
718 DEFAULT_GUEST_KERNEL_ID);
719 return machine;
Nikunj A. Dadhania7fb0a5e2012-04-09 13:52:23 +0530720 }
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200721
Arnaldo Carvalho de Melo34ba5122012-12-19 09:04:24 -0300722 return &session->machines.host;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200723}
724
Jiri Olsae4caec02012-10-10 18:52:24 +0200725static int deliver_sample_value(struct perf_session *session,
726 struct perf_tool *tool,
727 union perf_event *event,
728 struct perf_sample *sample,
729 struct sample_read_value *v,
730 struct machine *machine)
731{
732 struct perf_sample_id *sid;
733
734 sid = perf_evlist__id2sid(session->evlist, v->id);
735 if (sid) {
736 sample->id = v->id;
737 sample->period = v->value - sid->period;
738 sid->period = v->value;
739 }
740
741 if (!sid || sid->evsel == NULL) {
742 ++session->stats.nr_unknown_id;
743 return 0;
744 }
745
746 return tool->sample(tool, event, sample, sid->evsel, machine);
747}
748
749static int deliver_sample_group(struct perf_session *session,
750 struct perf_tool *tool,
751 union perf_event *event,
752 struct perf_sample *sample,
753 struct machine *machine)
754{
755 int ret = -EINVAL;
756 u64 i;
757
758 for (i = 0; i < sample->read.group.nr; i++) {
759 ret = deliver_sample_value(session, tool, event, sample,
760 &sample->read.group.values[i],
761 machine);
762 if (ret)
763 break;
764 }
765
766 return ret;
767}
768
769static int
770perf_session__deliver_sample(struct perf_session *session,
771 struct perf_tool *tool,
772 union perf_event *event,
773 struct perf_sample *sample,
774 struct perf_evsel *evsel,
775 struct machine *machine)
776{
777 /* We know evsel != NULL. */
778 u64 sample_type = evsel->attr.sample_type;
779 u64 read_format = evsel->attr.read_format;
780
781 /* Standard sample delievery. */
782 if (!(sample_type & PERF_SAMPLE_READ))
783 return tool->sample(tool, event, sample, evsel, machine);
784
785 /* For PERF_SAMPLE_READ we have either single or group mode. */
786 if (read_format & PERF_FORMAT_GROUP)
787 return deliver_sample_group(session, tool, event, sample,
788 machine);
789 else
790 return deliver_sample_value(session, tool, event, sample,
791 &sample->read.one, machine);
792}
793
Jiri Olsa79a30fe2014-06-10 22:31:35 +0200794int perf_session__deliver_event(struct perf_session *session,
795 union perf_event *event,
796 struct perf_sample *sample,
797 struct perf_tool *tool, u64 file_offset)
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100798{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300799 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200800 struct machine *machine;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300801
Thomas Gleixner532e7262010-12-07 12:48:55 +0000802 dump_event(session, event, file_offset, sample);
803
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200804 evsel = perf_evlist__id2evsel(session->evlist, sample->id);
805 if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) {
806 /*
807 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
808 * because the tools right now may apply filters, discarding
809 * some of the samples. For consistency, in the future we
810 * should have something like nr_filtered_samples and remove
811 * the sample->period from total_sample_period, etc, KISS for
812 * now tho.
813 *
814 * Also testing against NULL allows us to handle files without
815 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
816 * future probably it'll be a good idea to restrict event
817 * processing via perf_session to files with both set.
818 */
819 hists__inc_nr_events(&evsel->hists, event->header.type);
820 }
821
Adrian Hunteref893252013-08-27 11:23:06 +0300822 machine = perf_session__find_machine_for_cpumode(session, event,
823 sample);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200824
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100825 switch (event->header.type) {
826 case PERF_RECORD_SAMPLE:
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200827 dump_sample(evsel, event, sample);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300828 if (evsel == NULL) {
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300829 ++session->stats.nr_unknown_id;
Jiri Olsa67822062012-04-12 14:21:01 +0200830 return 0;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300831 }
Joerg Roedel0c095712012-02-10 18:05:04 +0100832 if (machine == NULL) {
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300833 ++session->stats.nr_unprocessable_samples;
Jiri Olsa67822062012-04-12 14:21:01 +0200834 return 0;
Joerg Roedel0c095712012-02-10 18:05:04 +0100835 }
Jiri Olsae4caec02012-10-10 18:52:24 +0200836 return perf_session__deliver_sample(session, tool, event,
837 sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100838 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200839 return tool->mmap(tool, event, sample, machine);
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200840 case PERF_RECORD_MMAP2:
841 return tool->mmap2(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100842 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200843 return tool->comm(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100844 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200845 return tool->fork(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100846 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200847 return tool->exit(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100848 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200849 if (tool->lost == perf_event__process_lost)
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300850 session->stats.total_lost += event->lost.lost;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200851 return tool->lost(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100852 case PERF_RECORD_READ:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200853 return tool->read(tool, event, sample, evsel, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100854 case PERF_RECORD_THROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200855 return tool->throttle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100856 case PERF_RECORD_UNTHROTTLE:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200857 return tool->unthrottle(tool, event, sample, machine);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100858 default:
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300859 ++session->stats.nr_unknown_events;
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100860 return -1;
861 }
862}
863
Adrian Hunterd5652d82014-07-23 22:19:58 +0300864static s64 perf_session__process_user_event(struct perf_session *session,
865 union perf_event *event,
866 struct perf_tool *tool,
867 u64 file_offset)
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000868{
Jiri Olsacc9784bd2013-10-15 16:27:34 +0200869 int fd = perf_data_file__fd(session->file);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200870 int err;
871
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000872 dump_event(session, event, file_offset, NULL);
873
874 /* These events are processed right away */
875 switch (event->header.type) {
876 case PERF_RECORD_HEADER_ATTR:
Adrian Hunter47c3d102013-07-04 16:20:21 +0300877 err = tool->attr(tool, event, &session->evlist);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200878 if (err == 0)
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -0300879 perf_session__set_id_hdr_size(session);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200880 return err;
Jiri Olsaf67697b2014-02-04 15:37:48 +0100881 case PERF_RECORD_HEADER_EVENT_TYPE:
882 /*
883 * Depreceated, but we need to handle it for sake
884 * of old data files create in pipe mode.
885 */
886 return 0;
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000887 case PERF_RECORD_HEADER_TRACING_DATA:
888 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +0200889 lseek(fd, file_offset, SEEK_SET);
Adrian Hunter47c3d102013-07-04 16:20:21 +0300890 return tool->tracing_data(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000891 case PERF_RECORD_HEADER_BUILD_ID:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200892 return tool->build_id(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000893 case PERF_RECORD_FINISHED_ROUND:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200894 return tool->finished_round(tool, event, session);
Thomas Gleixnerba74f062010-12-07 12:49:01 +0000895 default:
896 return -EINVAL;
897 }
898}
899
Jiri Olsa268fb202012-05-30 14:23:43 +0200900static void event_swap(union perf_event *event, bool sample_id_all)
901{
902 perf_event__swap_op swap;
903
904 swap = perf_event__swap_ops[event->header.type];
905 if (swap)
906 swap(event, sample_id_all);
907}
908
Adrian Hunterd5652d82014-07-23 22:19:58 +0300909static s64 perf_session__process_event(struct perf_session *session,
David Aherne30b88a2013-08-05 21:41:33 -0400910 union perf_event *event,
911 struct perf_tool *tool,
912 u64 file_offset)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200913{
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200914 struct perf_sample sample;
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100915 int ret;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200916
Jiri Olsa268fb202012-05-30 14:23:43 +0200917 if (session->header.needs_swap)
Arnaldo Carvalho de Melo5e562472012-08-01 19:25:26 -0300918 event_swap(event, perf_evlist__sample_id_all(session->evlist));
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200919
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000920 if (event->header.type >= PERF_RECORD_HEADER_MAX)
921 return -EINVAL;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200922
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300923 events_stats__inc(&session->stats, event->header.type);
Thomas Gleixner9aefcab2010-12-07 12:48:47 +0000924
925 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200926 return perf_session__process_user_event(session, event, tool, file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100927
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000928 /*
929 * For all kernel events we get the sample data
930 */
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300931 ret = perf_evlist__parse_sample(session->evlist, event, &sample);
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200932 if (ret)
933 return ret;
Thomas Gleixner3dfc2c02010-12-07 12:48:58 +0000934
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200935 if (tool->ordered_events) {
Jiri Olsad40b4a12014-08-01 13:01:04 -0300936 ret = perf_session_queue_event(session, event, tool, &sample,
Thomas Gleixnere4c2df12010-12-07 12:48:50 +0000937 file_offset);
Thomas Gleixnercbf41642010-12-05 14:32:55 +0100938 if (ret != -ETIME)
939 return ret;
940 }
941
Jiri Olsa79a30fe2014-06-10 22:31:35 +0200942 return perf_session__deliver_event(session, event, &sample, tool,
943 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200944}
945
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300946void perf_event_header__bswap(struct perf_event_header *hdr)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200947{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300948 hdr->type = bswap_32(hdr->type);
949 hdr->misc = bswap_16(hdr->misc);
950 hdr->size = bswap_16(hdr->size);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200951}
952
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200953struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
954{
Adrian Hunter1fcb8762014-07-14 13:02:25 +0300955 return machine__findnew_thread(&session->machines.host, -1, pid);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200956}
957
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300958static struct thread *perf_session__register_idle_thread(struct perf_session *session)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200959{
Adrian Hunter1fcb8762014-07-14 13:02:25 +0300960 struct thread *thread;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200961
Adrian Hunter1fcb8762014-07-14 13:02:25 +0300962 thread = machine__findnew_thread(&session->machines.host, 0, 0);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200963 if (thread == NULL || thread__set_comm(thread, "swapper", 0)) {
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -0200964 pr_err("problem inserting idle task.\n");
965 thread = NULL;
966 }
967
968 return thread;
969}
970
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200971static void perf_session__warn_about_errors(const struct perf_session *session,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200972 const struct perf_tool *tool)
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200973{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200974 if (tool->lost == perf_event__process_lost &&
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300975 session->stats.nr_events[PERF_RECORD_LOST] != 0) {
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200976 ui__warning("Processed %d events and lost %d chunks!\n\n"
977 "Check IO/CPU overload!\n\n",
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300978 session->stats.nr_events[0],
979 session->stats.nr_events[PERF_RECORD_LOST]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200980 }
981
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300982 if (session->stats.nr_unknown_events != 0) {
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200983 ui__warning("Found %u unknown events!\n\n"
984 "Is this an older tool processing a perf.data "
985 "file generated by a more recent tool?\n\n"
986 "If that is not the case, consider "
987 "reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300988 session->stats.nr_unknown_events);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200989 }
990
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300991 if (session->stats.nr_unknown_id != 0) {
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300992 ui__warning("%u samples with id not present in the header\n",
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300993 session->stats.nr_unknown_id);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300994 }
995
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300996 if (session->stats.nr_invalid_chains != 0) {
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -0200997 ui__warning("Found invalid callchains!\n\n"
998 "%u out of %u events were discarded for this reason.\n\n"
999 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001000 session->stats.nr_invalid_chains,
1001 session->stats.nr_events[PERF_RECORD_SAMPLE]);
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001002 }
Joerg Roedel0c095712012-02-10 18:05:04 +01001003
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001004 if (session->stats.nr_unprocessable_samples != 0) {
Joerg Roedel0c095712012-02-10 18:05:04 +01001005 ui__warning("%u unprocessable samples recorded.\n"
1006 "Do you have a KVM guest running and not using 'perf kvm'?\n",
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001007 session->stats.nr_unprocessable_samples);
Joerg Roedel0c095712012-02-10 18:05:04 +01001008 }
Arnaldo Carvalho de Melo11095992011-01-04 16:25:15 -02001009}
1010
Tom Zanussi8dc58102010-04-01 23:59:15 -05001011volatile int session_done;
1012
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001013static int __perf_session__process_pipe_events(struct perf_session *session,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001014 struct perf_tool *tool)
Tom Zanussi8dc58102010-04-01 23:59:15 -05001015{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001016 int fd = perf_data_file__fd(session->file);
Stephane Eranian444d2862012-05-15 13:28:12 +02001017 union perf_event *event;
1018 uint32_t size, cur_size = 0;
1019 void *buf = NULL;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001020 s64 skip = 0;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001021 u64 head;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001022 ssize_t err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001023 void *p;
1024
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001025 perf_tool__fill_defaults(tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001026
1027 head = 0;
Stephane Eranian444d2862012-05-15 13:28:12 +02001028 cur_size = sizeof(union perf_event);
1029
1030 buf = malloc(cur_size);
1031 if (!buf)
1032 return -errno;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001033more:
Stephane Eranian444d2862012-05-15 13:28:12 +02001034 event = buf;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001035 err = readn(fd, event, sizeof(struct perf_event_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05001036 if (err <= 0) {
1037 if (err == 0)
1038 goto done;
1039
1040 pr_err("failed to read event header\n");
1041 goto out_err;
1042 }
1043
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001044 if (session->header.needs_swap)
Stephane Eranian444d2862012-05-15 13:28:12 +02001045 perf_event_header__bswap(&event->header);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001046
Stephane Eranian444d2862012-05-15 13:28:12 +02001047 size = event->header.size;
Adrian Hunter27389d72013-07-04 16:20:27 +03001048 if (size < sizeof(struct perf_event_header)) {
1049 pr_err("bad event header size\n");
1050 goto out_err;
1051 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001052
Stephane Eranian444d2862012-05-15 13:28:12 +02001053 if (size > cur_size) {
1054 void *new = realloc(buf, size);
1055 if (!new) {
1056 pr_err("failed to allocate memory to read event\n");
1057 goto out_err;
1058 }
1059 buf = new;
1060 cur_size = size;
1061 event = buf;
1062 }
1063 p = event;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001064 p += sizeof(struct perf_event_header);
1065
Tom Zanussi794e43b2010-05-05 00:27:40 -05001066 if (size - sizeof(struct perf_event_header)) {
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001067 err = readn(fd, p, size - sizeof(struct perf_event_header));
Tom Zanussi794e43b2010-05-05 00:27:40 -05001068 if (err <= 0) {
1069 if (err == 0) {
1070 pr_err("unexpected end of event stream\n");
1071 goto done;
1072 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001073
Tom Zanussi794e43b2010-05-05 00:27:40 -05001074 pr_err("failed to read event data\n");
1075 goto out_err;
1076 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05001077 }
1078
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001079 if ((skip = perf_session__process_event(session, event, tool, head)) < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001080 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
Stephane Eranian444d2862012-05-15 13:28:12 +02001081 head, event->header.size, event->header.type);
Jiri Olsa9389a462012-04-16 20:42:51 +02001082 err = -EINVAL;
1083 goto out_err;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001084 }
1085
1086 head += size;
1087
Tom Zanussi8dc58102010-04-01 23:59:15 -05001088 if (skip > 0)
1089 head += skip;
1090
1091 if (!session_done())
1092 goto more;
1093done:
Adrian Hunter8c16b642013-10-18 15:29:02 +03001094 /* do the final flush for ordered samples */
Jiri Olsad8836b52014-06-05 10:29:45 +02001095 err = ordered_events__flush(session, tool, OE_FLUSH__FINAL);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001096out_err:
Stephane Eranian444d2862012-05-15 13:28:12 +02001097 free(buf);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001098 perf_session__warn_about_errors(session, tool);
1099 perf_session_free_sample_buffers(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001100 return err;
1101}
1102
Frederic Weisbecker998bedc82011-05-23 13:06:28 +02001103static union perf_event *
1104fetch_mmaped_event(struct perf_session *session,
1105 u64 head, size_t mmap_size, char *buf)
1106{
1107 union perf_event *event;
1108
1109 /*
1110 * Ensure we have enough space remaining to read
1111 * the size of the event in the headers.
1112 */
1113 if (head + sizeof(event->header) > mmap_size)
1114 return NULL;
1115
1116 event = (union perf_event *)(buf + head);
1117
1118 if (session->header.needs_swap)
1119 perf_event_header__bswap(&event->header);
1120
Adrian Hunter27389d72013-07-04 16:20:27 +03001121 if (head + event->header.size > mmap_size) {
1122 /* We're not fetching the event so swap back again */
1123 if (session->header.needs_swap)
1124 perf_event_header__bswap(&event->header);
Frederic Weisbecker998bedc82011-05-23 13:06:28 +02001125 return NULL;
Adrian Hunter27389d72013-07-04 16:20:27 +03001126 }
Frederic Weisbecker998bedc82011-05-23 13:06:28 +02001127
1128 return event;
1129}
1130
David Miller35d48dd2012-11-10 14:12:19 -05001131/*
1132 * On 64bit we can mmap the data file in one go. No need for tiny mmap
1133 * slices. On 32bit we use 32MB.
1134 */
1135#if BITS_PER_LONG == 64
1136#define MMAP_SIZE ULLONG_MAX
1137#define NUM_MMAPS 1
1138#else
1139#define MMAP_SIZE (32 * 1024 * 1024ULL)
1140#define NUM_MMAPS 128
1141#endif
1142
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001143int __perf_session__process_events(struct perf_session *session,
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001144 u64 data_offset, u64 data_size,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001145 u64 file_size, struct perf_tool *tool)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001146{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001147 int fd = perf_data_file__fd(session->file);
Adrian Hunterd5652d82014-07-23 22:19:58 +03001148 u64 head, page_offset, file_offset, file_pos, size;
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001149 int err, mmap_prot, mmap_flags, map_idx = 0;
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -03001150 size_t mmap_size;
David Miller35d48dd2012-11-10 14:12:19 -05001151 char *buf, *mmaps[NUM_MMAPS];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001152 union perf_event *event;
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001153 struct ui_progress prog;
Adrian Hunterd5652d82014-07-23 22:19:58 +03001154 s64 skip;
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001155
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001156 perf_tool__fill_defaults(tool);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001157
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001158 page_offset = page_size * (data_offset / page_size);
1159 file_offset = page_offset;
1160 head = data_offset - page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001161
Namhyung Kimb314e5c2013-09-30 17:19:48 +09001162 if (data_size && (data_offset + data_size < file_size))
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001163 file_size = data_offset + data_size;
1164
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001165 ui_progress__init(&prog, file_size, "Processing events...");
Thomas Gleixner55b44622010-11-30 17:49:46 +00001166
David Miller35d48dd2012-11-10 14:12:19 -05001167 mmap_size = MMAP_SIZE;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001168 if (mmap_size > file_size) {
Thomas Gleixner55b44622010-11-30 17:49:46 +00001169 mmap_size = file_size;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001170 session->one_mmap = true;
1171 }
Thomas Gleixner55b44622010-11-30 17:49:46 +00001172
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001173 memset(mmaps, 0, sizeof(mmaps));
1174
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001175 mmap_prot = PROT_READ;
1176 mmap_flags = MAP_SHARED;
1177
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001178 if (session->header.needs_swap) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001179 mmap_prot |= PROT_WRITE;
1180 mmap_flags = MAP_PRIVATE;
1181 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001182remap:
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001183 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd,
Thomas Gleixner55b44622010-11-30 17:49:46 +00001184 file_offset);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001185 if (buf == MAP_FAILED) {
1186 pr_err("failed to mmap file\n");
1187 err = -errno;
1188 goto out_err;
1189 }
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001190 mmaps[map_idx] = buf;
1191 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001192 file_pos = file_offset + head;
Adrian Hunter919d86d2014-07-14 13:02:51 +03001193 if (session->one_mmap) {
1194 session->one_mmap_addr = buf;
1195 session->one_mmap_offset = file_offset;
1196 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001197
1198more:
Frederic Weisbecker998bedc82011-05-23 13:06:28 +02001199 event = fetch_mmaped_event(session, head, mmap_size, buf);
1200 if (!event) {
Thomas Gleixnerfe174202010-11-30 17:49:49 +00001201 if (mmaps[map_idx]) {
1202 munmap(mmaps[map_idx], mmap_size);
1203 mmaps[map_idx] = NULL;
1204 }
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001205
Thomas Gleixner0331ee02010-11-30 17:49:38 +00001206 page_offset = page_size * (head / page_size);
1207 file_offset += page_offset;
1208 head -= page_offset;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001209 goto remap;
1210 }
1211
1212 size = event->header.size;
1213
Adrian Hunter27389d72013-07-04 16:20:27 +03001214 if (size < sizeof(struct perf_event_header) ||
Adrian Hunter6f917c72014-07-23 22:19:57 +03001215 (skip = perf_session__process_event(session, event, tool, file_pos))
1216 < 0) {
Jiri Olsa9389a462012-04-16 20:42:51 +02001217 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1218 file_offset + head, event->header.size,
1219 event->header.type);
1220 err = -EINVAL;
1221 goto out_err;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001222 }
1223
Adrian Hunter6f917c72014-07-23 22:19:57 +03001224 if (skip)
1225 size += skip;
1226
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001227 head += size;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001228 file_pos += size;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001229
Arnaldo Carvalho de Melo4d3001f2013-10-23 15:40:38 -03001230 ui_progress__update(&prog, size);
Thomas Gleixner55b44622010-11-30 17:49:46 +00001231
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001232 if (session_done())
Adrian Hunter8c16b642013-10-18 15:29:02 +03001233 goto out;
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001234
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001235 if (file_pos < file_size)
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001236 goto more;
Thomas Gleixnerd6513282010-11-30 17:49:44 +00001237
Adrian Hunter8c16b642013-10-18 15:29:02 +03001238out:
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +02001239 /* do the final flush for ordered samples */
Jiri Olsad8836b52014-06-05 10:29:45 +02001240 err = ordered_events__flush(session, tool, OE_FLUSH__FINAL);
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001241out_err:
Namhyung Kima5580f32012-11-13 22:30:34 +09001242 ui_progress__finish();
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001243 perf_session__warn_about_errors(session, tool);
Thomas Gleixner020bb752010-11-30 17:49:53 +00001244 perf_session_free_sample_buffers(session);
Adrian Hunter919d86d2014-07-14 13:02:51 +03001245 session->one_mmap = false;
Arnaldo Carvalho de Melo06aae5902009-12-27 21:36:59 -02001246 return err;
1247}
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001248
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001249int perf_session__process_events(struct perf_session *session,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001250 struct perf_tool *tool)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001251{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001252 u64 size = perf_data_file__size(session->file);
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001253 int err;
1254
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001255 if (perf_session__register_idle_thread(session) == NULL)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001256 return -ENOMEM;
1257
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001258 if (!perf_data_file__is_pipe(session->file))
1259 err = __perf_session__process_events(session,
1260 session->header.data_offset,
1261 session->header.data_size,
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001262 size, tool);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001263 else
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001264 err = __perf_session__process_pipe_events(session, tool);
Dave Martin88ca8952010-07-27 11:46:12 -03001265
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001266 return err;
1267}
1268
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -03001269bool perf_session__has_traces(struct perf_session *session, const char *msg)
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001270{
David Ahern93ea01c292013-08-07 22:50:58 -04001271 struct perf_evsel *evsel;
1272
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001273 evlist__for_each(session->evlist, evsel) {
David Ahern93ea01c292013-08-07 22:50:58 -04001274 if (evsel->attr.type == PERF_TYPE_TRACEPOINT)
1275 return true;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001276 }
1277
David Ahern93ea01c292013-08-07 22:50:58 -04001278 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1279 return false;
Arnaldo Carvalho de Melo27295592009-12-27 21:37:01 -02001280}
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001281
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001282int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1283 const char *symbol_name, u64 addr)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001284{
1285 char *bracket;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001286 enum map_type i;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001287 struct ref_reloc_sym *ref;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001288
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001289 ref = zalloc(sizeof(struct ref_reloc_sym));
1290 if (ref == NULL)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001291 return -ENOMEM;
1292
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001293 ref->name = strdup(symbol_name);
1294 if (ref->name == NULL) {
1295 free(ref);
1296 return -ENOMEM;
1297 }
1298
1299 bracket = strchr(ref->name, ']');
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001300 if (bracket)
1301 *bracket = '\0';
1302
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001303 ref->addr = addr;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001304
1305 for (i = 0; i < MAP__NR_TYPES; ++i) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001306 struct kmap *kmap = map__kmap(maps[i]);
1307 kmap->ref_reloc_sym = ref;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001308 }
1309
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001310 return 0;
1311}
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001312
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001313size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001314{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001315 return machines__fprintf_dsos(&session->machines, fp);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001316}
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001317
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001318size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001319 bool (skip)(struct dso *dso, int parm), int parm)
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001320{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001321 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001322}
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001323
1324size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1325{
1326 struct perf_evsel *pos;
1327 size_t ret = fprintf(fp, "Aggregated stats:\n");
1328
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001329 ret += events_stats__fprintf(&session->stats, fp);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001330
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001331 evlist__for_each(session->evlist, pos) {
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -03001332 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
Arnaldo Carvalho de Melo52168ee2012-12-18 16:02:17 -03001333 ret += events_stats__fprintf(&pos->hists.stats, fp);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001334 }
1335
1336 return ret;
1337}
David Ahernc0230b22011-03-09 22:23:27 -07001338
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001339size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1340{
1341 /*
1342 * FIXME: Here we have to actually print all the machines in this
1343 * session, not just the host...
1344 */
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -03001345 return machine__fprintf(&session->machines.host, fp);
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -02001346}
1347
David Ahern9cbdb702011-04-06 21:54:20 -06001348struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1349 unsigned int type)
1350{
1351 struct perf_evsel *pos;
1352
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001353 evlist__for_each(session->evlist, pos) {
David Ahern9cbdb702011-04-06 21:54:20 -06001354 if (pos->attr.type == type)
1355 return pos;
1356 }
1357 return NULL;
1358}
1359
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001360void perf_evsel__print_ip(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -03001361 struct addr_location *al,
David Ahern307cbb92013-08-07 22:50:53 -04001362 unsigned int print_opts, unsigned int stack_depth)
David Ahernc0230b22011-03-09 22:23:27 -07001363{
David Ahernc0230b22011-03-09 22:23:27 -07001364 struct callchain_cursor_node *node;
David Aherna6ffaf92013-08-07 22:50:51 -04001365 int print_ip = print_opts & PRINT_IP_OPT_IP;
1366 int print_sym = print_opts & PRINT_IP_OPT_SYM;
1367 int print_dso = print_opts & PRINT_IP_OPT_DSO;
1368 int print_symoffset = print_opts & PRINT_IP_OPT_SYMOFFSET;
David Ahernb0b35f02013-08-07 22:50:52 -04001369 int print_oneline = print_opts & PRINT_IP_OPT_ONELINE;
Adrian Huntercc8fae12013-12-06 09:42:57 +02001370 int print_srcline = print_opts & PRINT_IP_OPT_SRCLINE;
David Ahernb0b35f02013-08-07 22:50:52 -04001371 char s = print_oneline ? ' ' : '\t';
David Ahernc0230b22011-03-09 22:23:27 -07001372
David Ahernc0230b22011-03-09 22:23:27 -07001373 if (symbol_conf.use_callchain && sample->callchain) {
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001374 struct addr_location node_al;
David Ahernc0230b22011-03-09 22:23:27 -07001375
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -03001376 if (machine__resolve_callchain(al->machine, evsel, al->thread,
Waiman Long91e95612013-10-18 10:38:48 -04001377 sample, NULL, NULL,
1378 PERF_MAX_STACK_DEPTH) != 0) {
David Ahernc0230b22011-03-09 22:23:27 -07001379 if (verbose)
1380 error("Failed to resolve callchain. Skipping\n");
1381 return;
1382 }
Namhyung Kim47260642012-05-31 14:43:26 +09001383 callchain_cursor_commit(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001384
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001385 if (print_symoffset)
1386 node_al = *al;
1387
David Ahern307cbb92013-08-07 22:50:53 -04001388 while (stack_depth) {
Adrian Huntera4eb24a2013-12-06 09:42:56 +02001389 u64 addr = 0;
1390
Namhyung Kim47260642012-05-31 14:43:26 +09001391 node = callchain_cursor_current(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001392 if (!node)
1393 break;
1394
David Ahernd2ff1b12013-11-18 13:32:44 -07001395 if (node->sym && node->sym->ignore)
1396 goto next;
1397
David Aherna6ffaf92013-08-07 22:50:51 -04001398 if (print_ip)
David Ahernb0b35f02013-08-07 22:50:52 -04001399 printf("%c%16" PRIx64, s, node->ip);
David Aherna6ffaf92013-08-07 22:50:51 -04001400
Adrian Huntera4eb24a2013-12-06 09:42:56 +02001401 if (node->map)
1402 addr = node->map->map_ip(node->map, node->ip);
1403
David Ahern787bef12011-05-27 14:28:43 -06001404 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001405 printf(" ");
David Ahern251f4262013-07-28 09:14:34 -06001406 if (print_symoffset) {
Adrian Huntera4eb24a2013-12-06 09:42:56 +02001407 node_al.addr = addr;
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001408 node_al.map = node->map;
1409 symbol__fprintf_symname_offs(node->sym, &node_al, stdout);
David Ahern251f4262013-07-28 09:14:34 -06001410 } else
1411 symbol__fprintf_symname(node->sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001412 }
David Ahern251f4262013-07-28 09:14:34 -06001413
David Ahern610723f2011-05-27 14:28:44 -06001414 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001415 printf(" (");
David Ahern52deff72012-05-29 22:58:26 -06001416 map__fprintf_dsoname(node->map, stdout);
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001417 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001418 }
David Ahernb0b35f02013-08-07 22:50:52 -04001419
Adrian Huntercc8fae12013-12-06 09:42:57 +02001420 if (print_srcline)
1421 map__fprintf_srcline(node->map, addr, "\n ",
1422 stdout);
1423
David Ahernb0b35f02013-08-07 22:50:52 -04001424 if (!print_oneline)
1425 printf("\n");
David Ahernc0230b22011-03-09 22:23:27 -07001426
David Ahern307cbb92013-08-07 22:50:53 -04001427 stack_depth--;
David Ahernd2ff1b12013-11-18 13:32:44 -07001428next:
1429 callchain_cursor_advance(&callchain_cursor);
David Ahernc0230b22011-03-09 22:23:27 -07001430 }
1431
1432 } else {
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001433 if (al->sym && al->sym->ignore)
David Ahernd2ff1b12013-11-18 13:32:44 -07001434 return;
1435
David Aherna6ffaf92013-08-07 22:50:51 -04001436 if (print_ip)
1437 printf("%16" PRIx64, sample->ip);
1438
David Ahern787bef12011-05-27 14:28:43 -06001439 if (print_sym) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001440 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001441 if (print_symoffset)
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001442 symbol__fprintf_symname_offs(al->sym, al,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001443 stdout);
1444 else
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001445 symbol__fprintf_symname(al->sym, stdout);
David Ahern610723f2011-05-27 14:28:44 -06001446 }
1447
1448 if (print_dso) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001449 printf(" (");
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001450 map__fprintf_dsoname(al->map, stdout);
Akihiro Nagai547a92e2012-01-30 13:42:57 +09001451 printf(")");
David Ahern787bef12011-05-27 14:28:43 -06001452 }
Adrian Huntercc8fae12013-12-06 09:42:57 +02001453
1454 if (print_srcline)
1455 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
David Ahernc0230b22011-03-09 22:23:27 -07001456 }
1457}
Anton Blanchard5d67be92011-07-04 21:57:50 +10001458
1459int perf_session__cpu_bitmap(struct perf_session *session,
1460 const char *cpu_list, unsigned long *cpu_bitmap)
1461{
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04001462 int i, err = -1;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001463 struct cpu_map *map;
1464
1465 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1466 struct perf_evsel *evsel;
1467
1468 evsel = perf_session__find_first_evtype(session, i);
1469 if (!evsel)
1470 continue;
1471
1472 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1473 pr_err("File does not contain CPU events. "
1474 "Remove -c option to proceed.\n");
1475 return -1;
1476 }
1477 }
1478
1479 map = cpu_map__new(cpu_list);
David Ahern47fbe532011-11-13 10:45:27 -07001480 if (map == NULL) {
1481 pr_err("Invalid cpu_list\n");
1482 return -1;
1483 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001484
1485 for (i = 0; i < map->nr; i++) {
1486 int cpu = map->map[i];
1487
1488 if (cpu >= MAX_NR_CPUS) {
1489 pr_err("Requested CPU %d too large. "
1490 "Consider raising MAX_NR_CPUS\n", cpu);
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04001491 goto out_delete_map;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001492 }
1493
1494 set_bit(cpu, cpu_bitmap);
1495 }
1496
Stanislav Fomichev8bac41c2014-01-20 15:39:39 +04001497 err = 0;
1498
1499out_delete_map:
1500 cpu_map__delete(map);
1501 return err;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001502}
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001503
1504void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1505 bool full)
1506{
1507 struct stat st;
Masanari Iidac5765ec2014-05-15 02:13:38 +09001508 int fd, ret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001509
1510 if (session == NULL || fp == NULL)
1511 return;
1512
Masanari Iidac5765ec2014-05-15 02:13:38 +09001513 fd = perf_data_file__fd(session->file);
1514
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001515 ret = fstat(fd, &st);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001516 if (ret == -1)
1517 return;
1518
1519 fprintf(fp, "# ========\n");
1520 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1521 perf_header__fprintf_info(session, fp, full);
1522 fprintf(fp, "# ========\n#\n");
1523}
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001524
1525
1526int __perf_session__set_tracepoints_handlers(struct perf_session *session,
1527 const struct perf_evsel_str_handler *assocs,
1528 size_t nr_assocs)
1529{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001530 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001531 size_t i;
1532 int err;
1533
1534 for (i = 0; i < nr_assocs; i++) {
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001535 /*
1536 * Adding a handler for an event not in the session,
1537 * just ignore it.
1538 */
1539 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name);
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001540 if (evsel == NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001541 continue;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001542
1543 err = -EEXIST;
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001544 if (evsel->handler != NULL)
Arnaldo Carvalho de Meloccf53ea2013-09-06 15:19:01 -03001545 goto out;
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001546 evsel->handler = assocs[i].handler;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001547 }
1548
1549 err = 0;
1550out:
1551 return err;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001552}