blob: 22dd05d3680ce4e81281e00938cc3a256451ec51 [file] [log] [blame]
Ingo Molnarabaff322009-06-02 22:59:57 +02001/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02002 * builtin-record.c
3 *
4 * Builtin record command: Record the profile of a workload
5 * (or a CPU, or a PID) into the perf.data output file - for
6 * later analysis via perf report.
Ingo Molnarabaff322009-06-02 22:59:57 +02007 */
Xiao Guangrongb8f46c52010-02-03 11:53:14 +08008#define _FILE_OFFSET_BITS 64
9
Ingo Molnar16f762a2009-05-27 09:10:38 +020010#include "builtin.h"
Ingo Molnarbf9e1872009-06-02 23:37:05 +020011
12#include "perf.h"
13
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -020014#include "util/build-id.h"
Thomas Gleixner6eda5832009-05-01 18:29:57 +020015#include "util/util.h"
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020016#include "util/parse-options.h"
Ingo Molnar8ad8db32009-05-26 11:10:09 +020017#include "util/parse-events.h"
Thomas Gleixner6eda5832009-05-01 18:29:57 +020018
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020019#include "util/header.h"
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020020#include "util/event.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020021#include "util/evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020022#include "util/evsel.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020023#include "util/debug.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020024#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020025#include "util/tool.h"
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020026#include "util/symbol.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110027#include "util/cpumap.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020028#include "util/thread_map.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020029
Peter Zijlstra97124d5e2009-06-02 15:52:24 +020030#include <unistd.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020031#include <sched.h>
Arnaldo Carvalho de Meloa41794c2010-05-18 18:29:23 -030032#include <sys/mman.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020033
Jiri Olsa26d33022012-08-07 15:20:47 +020034#define CALLCHAIN_HELP "do call-graph (stack chain/backtrace) recording: "
35
36#ifdef NO_LIBUNWIND_SUPPORT
37static char callchain_help[] = CALLCHAIN_HELP "[fp]";
38#else
39static unsigned long default_stack_dump_size = 8192;
40static char callchain_help[] = CALLCHAIN_HELP "[fp] dwarf";
41#endif
42
Frederic Weisbecker7865e812010-04-14 19:42:07 +020043enum write_mode_t {
44 WRITE_FORCE,
45 WRITE_APPEND
46};
47
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020048struct perf_record {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020049 struct perf_tool tool;
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020050 struct perf_record_opts opts;
51 u64 bytes_written;
52 const char *output_name;
53 struct perf_evlist *evlist;
54 struct perf_session *session;
55 const char *progname;
56 int output;
57 unsigned int page_size;
58 int realtime_prio;
59 enum write_mode_t write_mode;
60 bool no_buildid;
61 bool no_buildid_cache;
62 bool force;
63 bool file_new;
64 bool append_file;
65 long samples;
66 off_t post_processing_offset;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -020067};
Ingo Molnara21ca2c2009-06-06 09:58:57 +020068
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020069static void advance_output(struct perf_record *rec, size_t size)
Tom Zanussi92155452010-04-01 23:59:21 -050070{
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020071 rec->bytes_written += size;
Tom Zanussi92155452010-04-01 23:59:21 -050072}
73
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020074static void write_output(struct perf_record *rec, void *buf, size_t size)
Peter Zijlstraf5970552009-06-18 23:22:55 +020075{
76 while (size) {
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020077 int ret = write(rec->output, buf, size);
Peter Zijlstraf5970552009-06-18 23:22:55 +020078
79 if (ret < 0)
80 die("failed to write");
81
82 size -= ret;
83 buf += ret;
84
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020085 rec->bytes_written += ret;
Peter Zijlstraf5970552009-06-18 23:22:55 +020086 }
87}
88
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020089static int process_synthesized_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020090 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -020091 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020092 struct machine *machine __used)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020093{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020094 struct perf_record *rec = container_of(tool, struct perf_record, tool);
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020095 write_output(rec, event, event->header.size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020096 return 0;
97}
98
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020099static void perf_record__mmap_read(struct perf_record *rec,
100 struct perf_mmap *md)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200101{
Arnaldo Carvalho de Melo744bd8a2011-01-12 17:07:28 -0200102 unsigned int head = perf_mmap__read_head(md);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200103 unsigned int old = md->prev;
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200104 unsigned char *data = md->base + rec->page_size;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200105 unsigned long size;
106 void *buf;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200107
Arnaldo Carvalho de Melodc820092011-01-28 14:49:19 -0200108 if (old == head)
109 return;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200110
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200111 rec->samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200112
113 size = head - old;
114
115 if ((old & md->mask) + size != (head & md->mask)) {
116 buf = &data[old & md->mask];
117 size = md->mask + 1 - (old & md->mask);
118 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200119
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200120 write_output(rec, buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200121 }
122
123 buf = &data[old & md->mask];
124 size = head - old;
125 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200126
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200127 write_output(rec, buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200128
129 md->prev = old;
Arnaldo Carvalho de Melo115d2d82011-01-12 17:11:53 -0200130 perf_mmap__write_tail(md, old);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200131}
132
133static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200134static volatile int signr = -1;
Andi Kleen33e49ea2011-09-15 14:31:40 -0700135static volatile int child_finished = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200136
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200137static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200138{
Andi Kleen33e49ea2011-09-15 14:31:40 -0700139 if (sig == SIGCHLD)
140 child_finished = 1;
141
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200142 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200143 signr = sig;
144}
145
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200146static void perf_record__sig_exit(int exit_status __used, void *arg)
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200147{
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200148 struct perf_record *rec = arg;
Andi Kleen33e49ea2011-09-15 14:31:40 -0700149 int status;
150
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200151 if (rec->evlist->workload.pid > 0) {
Andi Kleen33e49ea2011-09-15 14:31:40 -0700152 if (!child_finished)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200153 kill(rec->evlist->workload.pid, SIGTERM);
Andi Kleen33e49ea2011-09-15 14:31:40 -0700154
155 wait(&status);
156 if (WIFSIGNALED(status))
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200157 psignal(WTERMSIG(status), rec->progname);
Andi Kleen33e49ea2011-09-15 14:31:40 -0700158 }
Chris Wilson933da832009-10-04 01:35:01 +0100159
Arnaldo Carvalho de Melo18483b82010-12-06 15:13:38 -0200160 if (signr == -1 || signr == SIGUSR1)
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200161 return;
162
163 signal(signr, SIG_DFL);
164 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200165}
166
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300167static bool perf_evlist__equal(struct perf_evlist *evlist,
168 struct perf_evlist *other)
169{
170 struct perf_evsel *pos, *pair;
171
172 if (evlist->nr_entries != other->nr_entries)
173 return false;
174
175 pair = list_entry(other->entries.next, struct perf_evsel, node);
176
177 list_for_each_entry(pos, &evlist->entries, node) {
178 if (memcmp(&pos->attr, &pair->attr, sizeof(pos->attr) != 0))
179 return false;
180 pair = list_entry(pair->node.next, struct perf_evsel, node);
181 }
182
183 return true;
184}
185
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200186static void perf_record__open(struct perf_record *rec)
Arnaldo Carvalho de Melodd7927f2011-01-12 14:28:51 -0200187{
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200188 struct perf_evsel *pos, *first;
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200189 struct perf_evlist *evlist = rec->evlist;
190 struct perf_session *session = rec->session;
191 struct perf_record_opts *opts = &rec->opts;
Arnaldo Carvalho de Melodd7927f2011-01-12 14:28:51 -0200192
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200193 first = list_entry(evlist->entries.next, struct perf_evsel, node);
194
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200195 perf_evlist__config_attrs(evlist, opts);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200196
Arnaldo Carvalho de Melodd7927f2011-01-12 14:28:51 -0200197 list_for_each_entry(pos, &evlist->entries, node) {
198 struct perf_event_attr *attr = &pos->attr;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200199 struct xyarray *group_fd = NULL;
Arnaldo Carvalho de Melodd7927f2011-01-12 14:28:51 -0200200 /*
201 * Check if parse_single_tracepoint_event has already asked for
202 * PERF_SAMPLE_TIME.
203 *
204 * XXX this is kludgy but short term fix for problems introduced by
205 * eac23d1c that broke 'perf script' by having different sample_types
206 * when using multiple tracepoint events when we use a perf binary
207 * that tries to use sample_id_all on an older kernel.
208 *
209 * We need to move counter creation to perf_session, support
210 * different sample_types, etc.
211 */
212 bool time_needed = attr->sample_type & PERF_SAMPLE_TIME;
213
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200214 if (opts->group && pos != first)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200215 group_fd = first->fd;
Arnaldo Carvalho de Melo0c978122012-02-14 14:05:30 -0200216fallback_missing_features:
217 if (opts->exclude_guest_missing)
218 attr->exclude_guest = attr->exclude_host = 0;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200219retry_sample_id:
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200220 attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
Ingo Molnar3da297a2009-06-07 17:39:02 +0200221try_again:
Arnaldo Carvalho de Meloed80f582011-11-11 15:12:56 -0200222 if (perf_evsel__open(pos, evlist->cpus, evlist->threads,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200223 opts->group, group_fd) < 0) {
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300224 int err = errno;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200225
Arnaldo Carvalho de Meloc286c412011-03-28 09:50:11 -0300226 if (err == EPERM || err == EACCES) {
Arnaldo Carvalho de Melob8631e62011-10-26 08:02:55 -0200227 ui__error_paranoid();
Arnaldo Carvalho de Meloc286c412011-03-28 09:50:11 -0300228 exit(EXIT_FAILURE);
Namhyung Kimbea03402012-04-26 14:15:15 +0900229 } else if (err == ENODEV && opts->target.cpu_list) {
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300230 die("No such device - did you specify"
231 " an out-of-range profile CPU?\n");
Arnaldo Carvalho de Melo0c978122012-02-14 14:05:30 -0200232 } else if (err == EINVAL) {
233 if (!opts->exclude_guest_missing &&
234 (attr->exclude_guest || attr->exclude_host)) {
235 pr_debug("Old kernel, cannot exclude "
236 "guest or host samples.\n");
237 opts->exclude_guest_missing = true;
238 goto fallback_missing_features;
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200239 } else if (!opts->sample_id_all_missing) {
Arnaldo Carvalho de Melo0c978122012-02-14 14:05:30 -0200240 /*
241 * Old kernel, no attr->sample_id_type_all field
242 */
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200243 opts->sample_id_all_missing = true;
Arnaldo Carvalho de Melo0c978122012-02-14 14:05:30 -0200244 if (!opts->sample_time && !opts->raw_samples && !time_needed)
245 attr->sample_type &= ~PERF_SAMPLE_TIME;
Ian Munsieeac23d12010-12-09 16:33:53 +1100246
Arnaldo Carvalho de Melo0c978122012-02-14 14:05:30 -0200247 goto retry_sample_id;
248 }
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300249 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200250
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300251 /*
252 * If it's cycles then fall back to hrtimer
253 * based cpu-clock-tick sw counter, which
David Ahern028d4552012-05-08 09:28:57 -0600254 * is always available even if no PMU support.
255 *
256 * PPC returns ENXIO until 2.6.37 (behavior changed
257 * with commit b0a873e).
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300258 */
David Ahern028d4552012-05-08 09:28:57 -0600259 if ((err == ENOENT || err == ENXIO)
260 && attr->type == PERF_TYPE_HARDWARE
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300261 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200262
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300263 if (verbose)
David Ahernca6a4252011-03-25 13:11:11 -0600264 ui__warning("The cycles event is not supported, "
265 "trying to fall back to cpu-clock-ticks\n");
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300266 attr->type = PERF_TYPE_SOFTWARE;
267 attr->config = PERF_COUNT_SW_CPU_CLOCK;
David Ahernd1cae342012-05-08 10:50:11 -0600268 if (pos->name) {
269 free(pos->name);
270 pos->name = NULL;
271 }
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300272 goto try_again;
273 }
David Ahernca6a4252011-03-25 13:11:11 -0600274
275 if (err == ENOENT) {
Namhyung Kim3780f482012-05-29 13:22:57 +0900276 ui__error("The %s event is not supported.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300277 perf_evsel__name(pos));
David Ahernca6a4252011-03-25 13:11:11 -0600278 exit(EXIT_FAILURE);
279 }
280
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300281 printf("\n");
Corey Ashfordd9cf8372010-11-19 17:37:24 -0800282 error("sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information.\n",
Arnaldo Carvalho de Melodd7927f2011-01-12 14:28:51 -0200283 err, strerror(err));
Simon Kaempfleinbfd45112009-11-16 15:25:53 +1000284
285#if defined(__i386__) || defined(__x86_64__)
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300286 if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP)
287 die("No hardware sampling interrupt available."
288 " No APIC? If so then you can boot the kernel"
289 " with the \"lapic\" boot parameter to"
290 " force-enable it.\n");
Simon Kaempfleinbfd45112009-11-16 15:25:53 +1000291#endif
292
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300293 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300294 }
Li Zefanc171b552009-10-15 11:22:07 +0800295 }
Arnaldo Carvalho de Meloa43d3f02010-12-25 12:12:25 -0200296
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100297 if (perf_evlist__set_filters(evlist)) {
298 error("failed to set filter with %d (%s)\n", errno,
299 strerror(errno));
300 exit(-1);
301 }
302
Nelson Elhage18e60932011-12-19 08:39:31 -0500303 if (perf_evlist__mmap(evlist, opts->mmap_pages, false) < 0) {
304 if (errno == EPERM)
305 die("Permission error mapping pages.\n"
306 "Consider increasing "
307 "/proc/sys/kernel/perf_event_mlock_kb,\n"
308 "or try again with a smaller value of -m/--mmap_pages.\n"
309 "(current value: %d)\n", opts->mmap_pages);
Nelson Elhage41d0d932011-12-19 08:39:32 -0500310 else if (!is_power_of_2(opts->mmap_pages))
311 die("--mmap_pages/-m value must be a power of two.");
312
Arnaldo Carvalho de Melo0a27d7f2011-01-14 15:50:51 -0200313 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
Nelson Elhage18e60932011-12-19 08:39:31 -0500314 }
Arnaldo Carvalho de Melo0a27d7f2011-01-14 15:50:51 -0200315
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200316 if (rec->file_new)
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300317 session->evlist = evlist;
318 else {
319 if (!perf_evlist__equal(session->evlist, evlist)) {
320 fprintf(stderr, "incompatible append\n");
321 exit(-1);
322 }
323 }
324
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -0300325 perf_session__set_id_hdr_size(session);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200326}
327
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200328static int process_buildids(struct perf_record *rec)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200329{
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200330 u64 size = lseek(rec->output, 0, SEEK_CUR);
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200331
Arnaldo Carvalho de Melo9f591fd2010-03-11 15:53:11 -0300332 if (size == 0)
333 return 0;
334
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200335 rec->session->fd = rec->output;
336 return __perf_session__process_events(rec->session, rec->post_processing_offset,
337 size - rec->post_processing_offset,
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200338 size, &build_id__mark_dso_hit_ops);
339}
340
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200341static void perf_record__exit(int status __used, void *arg)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200342{
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200343 struct perf_record *rec = arg;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200344
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200345 if (!rec->opts.pipe_output) {
346 rec->session->header.data_size += rec->bytes_written;
347
348 if (!rec->no_buildid)
349 process_buildids(rec);
350 perf_session__write_header(rec->session, rec->evlist,
351 rec->output, true);
352 perf_session__delete(rec->session);
353 perf_evlist__delete(rec->evlist);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300354 symbol__exit();
Tom Zanussic7929e42010-04-01 23:59:22 -0500355 }
Peter Zijlstraf5970552009-06-18 23:22:55 +0200356}
357
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200358static void perf_event__synthesize_guest_os(struct machine *machine, void *data)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800359{
360 int err;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200361 struct perf_tool *tool = data;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800362
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300363 if (machine__is_host(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800364 return;
365
366 /*
367 *As for guest kernel when processing subcommand record&report,
368 *we arrange module mmap prior to guest kernel mmap and trigger
369 *a preload dso because default guest module symbols are loaded
370 *from guest kallsyms instead of /lib/modules/XXX/XXX. This
371 *method is used to avoid symbol missing when the first addr is
372 *in module instead of in guest kernel.
373 */
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200374 err = perf_event__synthesize_modules(tool, process_synthesized_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200375 machine);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800376 if (err < 0)
377 pr_err("Couldn't record guest kernel [%d]'s reference"
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300378 " relocation symbol.\n", machine->pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800379
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800380 /*
381 * We use _stext for guest kernel because guest kernel's /proc/kallsyms
382 * have no _text sometimes.
383 */
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200384 err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200385 machine, "_text");
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800386 if (err < 0)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200387 err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200388 machine, "_stext");
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800389 if (err < 0)
390 pr_err("Couldn't record guest kernel [%d]'s reference"
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300391 " relocation symbol.\n", machine->pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800392}
393
Frederic Weisbecker98402802010-05-02 22:05:29 +0200394static struct perf_event_header finished_round_event = {
395 .size = sizeof(struct perf_event_header),
396 .type = PERF_RECORD_FINISHED_ROUND,
397};
398
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200399static void perf_record__mmap_read_all(struct perf_record *rec)
Frederic Weisbecker98402802010-05-02 22:05:29 +0200400{
Peter Zijlstra0e2e63d2010-05-20 14:45:26 +0200401 int i;
Frederic Weisbecker98402802010-05-02 22:05:29 +0200402
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200403 for (i = 0; i < rec->evlist->nr_mmaps; i++) {
404 if (rec->evlist->mmap[i].base)
405 perf_record__mmap_read(rec, &rec->evlist->mmap[i]);
Frederic Weisbecker98402802010-05-02 22:05:29 +0200406 }
407
Stephane Eranian2eeaaa02012-05-15 13:28:13 +0200408 if (perf_header__has_feat(&rec->session->header, HEADER_TRACING_DATA))
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200409 write_output(rec, &finished_round_event, sizeof(finished_round_event));
Frederic Weisbecker98402802010-05-02 22:05:29 +0200410}
411
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200412static int __cmd_record(struct perf_record *rec, int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200413{
Peter Zijlstra97124d5e2009-06-02 15:52:24 +0200414 struct stat st;
Ingo Molnarabaff322009-06-02 22:59:57 +0200415 int flags;
Robert Richter781ba9d2011-12-15 17:32:40 +0100416 int err, output, feat;
Peter Zijlstra8b412662009-09-17 19:59:05 +0200417 unsigned long waking = 0;
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300418 const bool forks = argc > 0;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300419 struct machine *machine;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200420 struct perf_tool *tool = &rec->tool;
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200421 struct perf_record_opts *opts = &rec->opts;
422 struct perf_evlist *evsel_list = rec->evlist;
423 const char *output_name = rec->output_name;
424 struct perf_session *session;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200425
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200426 rec->progname = argv[0];
Andi Kleen33e49ea2011-09-15 14:31:40 -0700427
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200428 rec->page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200429
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200430 on_exit(perf_record__sig_exit, rec);
Peter Zijlstraf5970552009-06-18 23:22:55 +0200431 signal(SIGCHLD, sig_handler);
432 signal(SIGINT, sig_handler);
Arnaldo Carvalho de Melo18483b82010-12-06 15:13:38 -0200433 signal(SIGUSR1, sig_handler);
Peter Zijlstraf5970552009-06-18 23:22:55 +0200434
Franck Bui-Huud7065ad2011-01-16 17:14:45 +0100435 if (!output_name) {
436 if (!fstat(STDOUT_FILENO, &st) && S_ISFIFO(st.st_mode))
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200437 opts->pipe_output = true;
Franck Bui-Huud7065ad2011-01-16 17:14:45 +0100438 else
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200439 rec->output_name = output_name = "perf.data";
Franck Bui-Huud7065ad2011-01-16 17:14:45 +0100440 }
441 if (output_name) {
442 if (!strcmp(output_name, "-"))
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200443 opts->pipe_output = true;
Franck Bui-Huud7065ad2011-01-16 17:14:45 +0100444 else if (!stat(output_name, &st) && st.st_size) {
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200445 if (rec->write_mode == WRITE_FORCE) {
Franck Bui-Huud7065ad2011-01-16 17:14:45 +0100446 char oldname[PATH_MAX];
447 snprintf(oldname, sizeof(oldname), "%s.old",
448 output_name);
449 unlink(oldname);
450 rename(output_name, oldname);
451 }
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200452 } else if (rec->write_mode == WRITE_APPEND) {
453 rec->write_mode = WRITE_FORCE;
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200454 }
Peter Zijlstra97124d5e2009-06-02 15:52:24 +0200455 }
456
Xiao Guangrongf887f302010-02-04 16:46:42 +0800457 flags = O_CREAT|O_RDWR;
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200458 if (rec->write_mode == WRITE_APPEND)
459 rec->file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200460 else
461 flags |= O_TRUNC;
462
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200463 if (opts->pipe_output)
Tom Zanussi529870e2010-04-01 23:59:16 -0500464 output = STDOUT_FILENO;
465 else
466 output = open(output_name, flags, S_IRUSR | S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200467 if (output < 0) {
468 perror("failed to create output file");
469 exit(-1);
470 }
471
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200472 rec->output = output;
473
Frederic Weisbecker7865e812010-04-14 19:42:07 +0200474 session = perf_session__new(output_name, O_WRONLY,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200475 rec->write_mode == WRITE_FORCE, false, NULL);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200476 if (session == NULL) {
Arnaldo Carvalho de Meloa9a70bb2009-11-17 01:18:11 -0200477 pr_err("Not enough memory for reading perf file header\n");
478 return -1;
479 }
480
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200481 rec->session = session;
482
Robert Richter781ba9d2011-12-15 17:32:40 +0100483 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
484 perf_header__set_feat(&session->header, feat);
485
486 if (rec->no_buildid)
487 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
488
489 if (!have_tracepoints(&evsel_list->entries))
Stephane Eranian2eeaaa02012-05-15 13:28:13 +0200490 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -0200491
Stephane Eranian330aa672012-03-08 23:47:46 +0100492 if (!rec->opts.branch_stack)
493 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
494
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200495 if (!rec->file_new) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300496 err = perf_session__read_header(session, output);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200497 if (err < 0)
Arnaldo Carvalho de Melo39d17da2010-07-29 14:08:55 -0300498 goto out_delete_session;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200499 }
500
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200501 if (forks) {
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200502 err = perf_evlist__prepare_workload(evsel_list, opts, argv);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200503 if (err < 0) {
504 pr_err("Couldn't run the workload!\n");
505 goto out_delete_session;
Jens Axboe0a5ac842009-08-12 11:18:01 +0200506 }
Peter Zijlstra856e9662009-12-16 17:55:55 +0100507 }
508
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200509 perf_record__open(rec);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200510
Arnaldo Carvalho de Melo712a4b62011-02-17 12:18:42 -0200511 /*
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200512 * perf_session__delete(session) will be called at perf_record__exit()
Arnaldo Carvalho de Melo712a4b62011-02-17 12:18:42 -0200513 */
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200514 on_exit(perf_record__exit, rec);
Arnaldo Carvalho de Melo712a4b62011-02-17 12:18:42 -0200515
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200516 if (opts->pipe_output) {
Tom Zanussi529870e2010-04-01 23:59:16 -0500517 err = perf_header__write_pipe(output);
518 if (err < 0)
519 return err;
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200520 } else if (rec->file_new) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300521 err = perf_session__write_header(session, evsel_list,
522 output, false);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200523 if (err < 0)
524 return err;
525 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200526
David Ahernd3665492012-02-06 15:27:52 -0700527 if (!rec->no_buildid
Robert Richtere20960c2011-12-07 10:02:55 +0100528 && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) {
David Ahernd3665492012-02-06 15:27:52 -0700529 pr_err("Couldn't generate buildids. "
Robert Richtere20960c2011-12-07 10:02:55 +0100530 "Use --no-buildid to profile anyway.\n");
531 return -1;
532 }
533
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200534 rec->post_processing_offset = lseek(output, 0, SEEK_CUR);
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200535
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200536 machine = perf_session__find_host_machine(session);
537 if (!machine) {
538 pr_err("Couldn't find native kernel information.\n");
539 return -1;
540 }
541
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200542 if (opts->pipe_output) {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200543 err = perf_event__synthesize_attrs(tool, session,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200544 process_synthesized_event);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500545 if (err < 0) {
546 pr_err("Couldn't synthesize attrs.\n");
547 return err;
548 }
Tom Zanussicd19a032010-04-01 23:59:20 -0500549
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200550 err = perf_event__synthesize_event_types(tool, process_synthesized_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200551 machine);
Tom Zanussicd19a032010-04-01 23:59:20 -0500552 if (err < 0) {
553 pr_err("Couldn't synthesize event_types.\n");
554 return err;
555 }
Tom Zanussi92155452010-04-01 23:59:21 -0500556
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200557 if (have_tracepoints(&evsel_list->entries)) {
Tom Zanussi63e0c772010-05-03 00:14:48 -0500558 /*
559 * FIXME err <= 0 here actually means that
560 * there were no tracepoints so its not really
561 * an error, just that we don't need to
562 * synthesize anything. We really have to
563 * return this more properly and also
564 * propagate errors that now are calling die()
565 */
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200566 err = perf_event__synthesize_tracing_data(tool, output, evsel_list,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200567 process_synthesized_event);
Tom Zanussi63e0c772010-05-03 00:14:48 -0500568 if (err <= 0) {
569 pr_err("Couldn't record tracing data.\n");
570 return err;
571 }
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200572 advance_output(rec, err);
Tom Zanussi63e0c772010-05-03 00:14:48 -0500573 }
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500574 }
575
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200576 err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200577 machine, "_text");
Arnaldo Carvalho de Melo70162132010-03-30 18:27:39 -0300578 if (err < 0)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200579 err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200580 machine, "_stext");
Arnaldo Carvalho de Meloc1a3a4b2010-11-22 14:01:55 -0200581 if (err < 0)
582 pr_err("Couldn't record kernel reference relocation symbol\n"
583 "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
584 "Check /proc/kallsyms permission or run as root.\n");
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200585
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200586 err = perf_event__synthesize_modules(tool, process_synthesized_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200587 machine);
Arnaldo Carvalho de Meloc1a3a4b2010-11-22 14:01:55 -0200588 if (err < 0)
589 pr_err("Couldn't record kernel module information.\n"
590 "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
591 "Check /proc/modules permission or run as root.\n");
592
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800593 if (perf_guest)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200594 perf_session__process_machines(session, tool,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200595 perf_event__synthesize_guest_os);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200596
Namhyung Kimbea03402012-04-26 14:15:15 +0900597 if (!opts->target.system_wide)
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200598 perf_event__synthesize_thread_map(tool, evsel_list->threads,
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -0200599 process_synthesized_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200600 machine);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200601 else
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200602 perf_event__synthesize_threads(tool, process_synthesized_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200603 machine);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200604
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200605 if (rec->realtime_prio) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200606 struct sched_param param;
607
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200608 param.sched_priority = rec->realtime_prio;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200609 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200610 pr_err("Could not set realtime priority.\n");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200611 exit(-1);
612 }
613 }
614
David Ahern764e16a32011-08-25 10:17:55 -0600615 perf_evlist__enable(evsel_list);
616
Peter Zijlstra856e9662009-12-16 17:55:55 +0100617 /*
618 * Let the child rip
619 */
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200620 if (forks)
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200621 perf_evlist__start_workload(evsel_list);
Peter Zijlstra856e9662009-12-16 17:55:55 +0100622
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200623 for (;;) {
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200624 int hits = rec->samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200625
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200626 perf_record__mmap_read_all(rec);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200627
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200628 if (hits == rec->samples) {
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200629 if (done)
630 break;
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200631 err = poll(evsel_list->pollfd, evsel_list->nr_fds, -1);
Peter Zijlstra8b412662009-09-17 19:59:05 +0200632 waking++;
633 }
634
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300635 if (done)
636 perf_evlist__disable(evsel_list);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200637 }
638
Arnaldo Carvalho de Melo18483b82010-12-06 15:13:38 -0200639 if (quiet || signr == SIGUSR1)
Arnaldo Carvalho de Melob44308f2010-10-26 15:20:09 -0200640 return 0;
641
Peter Zijlstra8b412662009-09-17 19:59:05 +0200642 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
643
Ingo Molnar021e9f42009-06-03 19:27:19 +0200644 /*
645 * Approximate RIP event size: 24 bytes.
646 */
647 fprintf(stderr,
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200648 "[ perf record: Captured and wrote %.3f MB %s (~%" PRIu64 " samples) ]\n",
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200649 (double)rec->bytes_written / 1024.0 / 1024.0,
Ingo Molnar021e9f42009-06-03 19:27:19 +0200650 output_name,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200651 rec->bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200652
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200653 return 0;
Arnaldo Carvalho de Melo39d17da2010-07-29 14:08:55 -0300654
655out_delete_session:
656 perf_session__delete(session);
657 return err;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200658}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200659
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100660#define BRANCH_OPT(n, m) \
661 { .name = n, .mode = (m) }
662
663#define BRANCH_END { .name = NULL }
664
665struct branch_mode {
666 const char *name;
667 int mode;
668};
669
670static const struct branch_mode branch_modes[] = {
671 BRANCH_OPT("u", PERF_SAMPLE_BRANCH_USER),
672 BRANCH_OPT("k", PERF_SAMPLE_BRANCH_KERNEL),
673 BRANCH_OPT("hv", PERF_SAMPLE_BRANCH_HV),
674 BRANCH_OPT("any", PERF_SAMPLE_BRANCH_ANY),
675 BRANCH_OPT("any_call", PERF_SAMPLE_BRANCH_ANY_CALL),
676 BRANCH_OPT("any_ret", PERF_SAMPLE_BRANCH_ANY_RETURN),
677 BRANCH_OPT("ind_call", PERF_SAMPLE_BRANCH_IND_CALL),
678 BRANCH_END
679};
680
681static int
Stephane Eraniana5aabda2012-03-08 23:47:45 +0100682parse_branch_stack(const struct option *opt, const char *str, int unset)
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100683{
684#define ONLY_PLM \
685 (PERF_SAMPLE_BRANCH_USER |\
686 PERF_SAMPLE_BRANCH_KERNEL |\
687 PERF_SAMPLE_BRANCH_HV)
688
689 uint64_t *mode = (uint64_t *)opt->value;
690 const struct branch_mode *br;
Stephane Eraniana5aabda2012-03-08 23:47:45 +0100691 char *s, *os = NULL, *p;
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100692 int ret = -1;
693
Stephane Eraniana5aabda2012-03-08 23:47:45 +0100694 if (unset)
695 return 0;
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100696
Stephane Eraniana5aabda2012-03-08 23:47:45 +0100697 /*
698 * cannot set it twice, -b + --branch-filter for instance
699 */
700 if (*mode)
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100701 return -1;
702
Stephane Eraniana5aabda2012-03-08 23:47:45 +0100703 /* str may be NULL in case no arg is passed to -b */
704 if (str) {
705 /* because str is read-only */
706 s = os = strdup(str);
707 if (!s)
708 return -1;
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100709
Stephane Eraniana5aabda2012-03-08 23:47:45 +0100710 for (;;) {
711 p = strchr(s, ',');
712 if (p)
713 *p = '\0';
714
715 for (br = branch_modes; br->name; br++) {
716 if (!strcasecmp(s, br->name))
717 break;
718 }
719 if (!br->name) {
720 ui__warning("unknown branch filter %s,"
721 " check man page\n", s);
722 goto error;
723 }
724
725 *mode |= br->mode;
726
727 if (!p)
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100728 break;
Stephane Eraniana5aabda2012-03-08 23:47:45 +0100729
730 s = p + 1;
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100731 }
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100732 }
733 ret = 0;
734
Stephane Eraniana5aabda2012-03-08 23:47:45 +0100735 /* default to any branch */
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100736 if ((*mode & ~ONLY_PLM) == 0) {
Stephane Eraniana5aabda2012-03-08 23:47:45 +0100737 *mode = PERF_SAMPLE_BRANCH_ANY;
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100738 }
739error:
740 free(os);
741 return ret;
742}
743
Jiri Olsa26d33022012-08-07 15:20:47 +0200744#ifndef NO_LIBUNWIND_SUPPORT
745static int get_stack_size(char *str, unsigned long *_size)
746{
747 char *endptr;
748 unsigned long size;
749 unsigned long max_size = round_down(USHRT_MAX, sizeof(u64));
750
751 size = strtoul(str, &endptr, 0);
752
753 do {
754 if (*endptr)
755 break;
756
757 size = round_up(size, sizeof(u64));
758 if (!size || size > max_size)
759 break;
760
761 *_size = size;
762 return 0;
763
764 } while (0);
765
766 pr_err("callchain: Incorrect stack dump size (max %ld): %s\n",
767 max_size, str);
768 return -1;
769}
770#endif /* !NO_LIBUNWIND_SUPPORT */
771
772static int
773parse_callchain_opt(const struct option *opt __used, const char *arg,
774 int unset)
775{
776 struct perf_record *rec = (struct perf_record *)opt->value;
777 char *tok, *name, *saveptr = NULL;
778 char *buf;
779 int ret = -1;
780
781 /* --no-call-graph */
782 if (unset)
783 return 0;
784
785 /* We specified default option if none is provided. */
786 BUG_ON(!arg);
787
788 /* We need buffer that we know we can write to. */
789 buf = malloc(strlen(arg) + 1);
790 if (!buf)
791 return -ENOMEM;
792
793 strcpy(buf, arg);
794
795 tok = strtok_r((char *)buf, ",", &saveptr);
796 name = tok ? : (char *)buf;
797
798 do {
799 /* Framepointer style */
800 if (!strncmp(name, "fp", sizeof("fp"))) {
801 if (!strtok_r(NULL, ",", &saveptr)) {
802 rec->opts.call_graph = CALLCHAIN_FP;
803 ret = 0;
804 } else
805 pr_err("callchain: No more arguments "
806 "needed for -g fp\n");
807 break;
808
809#ifndef NO_LIBUNWIND_SUPPORT
810 /* Dwarf style */
811 } else if (!strncmp(name, "dwarf", sizeof("dwarf"))) {
812 ret = 0;
813 rec->opts.call_graph = CALLCHAIN_DWARF;
814 rec->opts.stack_dump_size = default_stack_dump_size;
815
816 tok = strtok_r(NULL, ",", &saveptr);
817 if (tok) {
818 unsigned long size = 0;
819
820 ret = get_stack_size(tok, &size);
821 rec->opts.stack_dump_size = size;
822 }
823
824 if (!ret)
825 pr_debug("callchain: stack dump size %d\n",
826 rec->opts.stack_dump_size);
827#endif /* !NO_LIBUNWIND_SUPPORT */
828 } else {
829 pr_err("callchain: Unknown -g option "
830 "value: %s\n", arg);
831 break;
832 }
833
834 } while (0);
835
836 free(buf);
837
838 if (!ret)
839 pr_debug("callchain: type %d\n", rec->opts.call_graph);
840
841 return ret;
842}
843
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200844static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200845 "perf record [<options>] [<command>]",
846 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200847 NULL
848};
849
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200850/*
851 * XXX Ideally would be local to cmd_record() and passed to a perf_record__new
852 * because we need to have access to it in perf_record__exit, that is called
853 * after cmd_record() exits, but since record_options need to be accessible to
854 * builtin-script, leave it here.
855 *
856 * At least we don't ouch it in all the other functions here directly.
857 *
858 * Just say no to tons of global variables, sigh.
859 */
860static struct perf_record record = {
861 .opts = {
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200862 .mmap_pages = UINT_MAX,
863 .user_freq = UINT_MAX,
864 .user_interval = ULLONG_MAX,
Arnaldo Carvalho de Melo447a6012012-05-22 13:14:18 -0300865 .freq = 4000,
Namhyung Kimd1cb9fc2012-05-16 18:45:49 +0900866 .target = {
867 .uses_mmap = true,
868 },
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200869 },
870 .write_mode = WRITE_FORCE,
871 .file_new = true,
872};
Frederic Weisbecker7865e812010-04-14 19:42:07 +0200873
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200874/*
875 * XXX Will stay a global variable till we fix builtin-script.c to stop messing
876 * with it and switch to use the library functions in perf_evlist that came
877 * from builtin-record.c, i.e. use perf_record_opts,
878 * perf_evlist__prepare_workload, etc instead of fork+exec'in 'perf record',
879 * using pipes, etc.
880 */
Tom Zanussibca647a2010-11-10 08:11:30 -0600881const struct option record_options[] = {
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200882 OPT_CALLBACK('e', "event", &record.evlist, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200883 "event selector. use 'perf list' to list available events",
Jiri Olsaf120f9d2011-07-14 11:25:32 +0200884 parse_events_option),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200885 OPT_CALLBACK(0, "filter", &record.evlist, "filter",
Li Zefanc171b552009-10-15 11:22:07 +0800886 "event filter", parse_filter),
Namhyung Kimbea03402012-04-26 14:15:15 +0900887 OPT_STRING('p', "pid", &record.opts.target.pid, "pid",
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300888 "record events on existing process id"),
Namhyung Kimbea03402012-04-26 14:15:15 +0900889 OPT_STRING('t', "tid", &record.opts.target.tid, "tid",
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300890 "record events on existing thread id"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200891 OPT_INTEGER('r', "realtime", &record.realtime_prio,
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200892 "collect data with this RT SCHED_FIFO priority"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200893 OPT_BOOLEAN('D', "no-delay", &record.opts.no_delay,
Kirill Smelkovacac03f2011-01-12 17:59:36 +0300894 "collect data without buffering"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200895 OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples,
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200896 "collect raw sample records from all opened counters"),
Namhyung Kimbea03402012-04-26 14:15:15 +0900897 OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide,
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200898 "system-wide collection from all CPUs"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200899 OPT_BOOLEAN('A', "append", &record.append_file,
Ingo Molnarabaff322009-06-02 22:59:57 +0200900 "append to the output file to do incremental profiling"),
Namhyung Kimbea03402012-04-26 14:15:15 +0900901 OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu",
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200902 "list of cpus to monitor"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200903 OPT_BOOLEAN('f', "force", &record.force,
Frederic Weisbecker7865e812010-04-14 19:42:07 +0200904 "overwrite existing data file (deprecated)"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200905 OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"),
906 OPT_STRING('o', "output", &record.output_name, "file",
Ingo Molnarabaff322009-06-02 22:59:57 +0200907 "output file name"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200908 OPT_BOOLEAN('i', "no-inherit", &record.opts.no_inherit,
Stephane Eranian2e6cdf92010-05-12 10:40:01 +0200909 "child tasks do not inherit counters"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200910 OPT_UINTEGER('F', "freq", &record.opts.user_freq, "profile at this frequency"),
911 OPT_UINTEGER('m', "mmap-pages", &record.opts.mmap_pages,
Arnaldo Carvalho de Melo01c2d992011-11-09 09:16:26 -0200912 "number of mmap data pages"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200913 OPT_BOOLEAN(0, "group", &record.opts.group,
Lin Ming43bece72011-08-17 18:42:07 +0800914 "put the counters into a counter group"),
Jiri Olsa26d33022012-08-07 15:20:47 +0200915 OPT_CALLBACK_DEFAULT('g', "call-graph", &record, "mode[,dump_size]",
916 callchain_help, &parse_callchain_opt,
917 "fp"),
Ian Munsiec0555642010-04-13 18:37:33 +1000918 OPT_INCR('v', "verbose", &verbose,
Ingo Molnar3da297a2009-06-07 17:39:02 +0200919 "be more verbose (show counter open errors, etc)"),
Arnaldo Carvalho de Melob44308f2010-10-26 15:20:09 -0200920 OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200921 OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat,
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200922 "per thread counts"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200923 OPT_BOOLEAN('d', "data", &record.opts.sample_address,
Anton Blanchard4bba8282009-07-16 15:44:29 +0200924 "Sample addresses"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200925 OPT_BOOLEAN('T', "timestamp", &record.opts.sample_time, "Sample timestamps"),
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300926 OPT_BOOLEAN('P', "period", &record.opts.period, "Sample period"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200927 OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples,
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200928 "don't sample"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200929 OPT_BOOLEAN('N', "no-buildid-cache", &record.no_buildid_cache,
Stephane Eraniana1ac1d32010-06-17 11:39:01 +0200930 "do not update the buildid cache"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200931 OPT_BOOLEAN('B', "no-buildid", &record.no_buildid,
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -0200932 "do not collect buildids in perf.data"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200933 OPT_CALLBACK('G', "cgroup", &record.evlist, "name",
Stephane Eranian023695d2011-02-14 11:20:01 +0200934 "monitor event in cgroup name only",
935 parse_cgroups),
Namhyung Kimbea03402012-04-26 14:15:15 +0900936 OPT_STRING('u', "uid", &record.opts.target.uid_str, "user",
937 "user to profile"),
Stephane Eraniana5aabda2012-03-08 23:47:45 +0100938
939 OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack,
940 "branch any", "sample any taken branches",
941 parse_branch_stack),
942
943 OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack,
944 "branch filter mask", "branch stack filter modes",
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100945 parse_branch_stack),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200946 OPT_END()
947};
948
Ingo Molnarf37a2912009-07-01 12:37:06 +0200949int cmd_record(int argc, const char **argv, const char *prefix __used)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200950{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200951 int err = -ENOMEM;
952 struct perf_evsel *pos;
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200953 struct perf_evlist *evsel_list;
954 struct perf_record *rec = &record;
Namhyung Kim16ad2ff2012-05-07 14:09:02 +0900955 char errbuf[BUFSIZ];
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200956
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200957 evsel_list = perf_evlist__new(NULL, NULL);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200958 if (evsel_list == NULL)
959 return -ENOMEM;
960
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200961 rec->evlist = evsel_list;
962
Tom Zanussibca647a2010-11-10 08:11:30 -0600963 argc = parse_options(argc, argv, record_options, record_usage,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200964 PARSE_OPT_STOP_AT_NON_OPTION);
Namhyung Kimd67356e2012-05-07 14:09:03 +0900965 if (!argc && perf_target__none(&rec->opts.target))
Tom Zanussibca647a2010-11-10 08:11:30 -0600966 usage_with_options(record_usage, record_options);
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200967
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200968 if (rec->force && rec->append_file) {
Namhyung Kim3780f482012-05-29 13:22:57 +0900969 ui__error("Can't overwrite and append at the same time."
970 " You need to choose between -f and -A");
Tom Zanussibca647a2010-11-10 08:11:30 -0600971 usage_with_options(record_usage, record_options);
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200972 } else if (rec->append_file) {
973 rec->write_mode = WRITE_APPEND;
Frederic Weisbecker7865e812010-04-14 19:42:07 +0200974 } else {
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200975 rec->write_mode = WRITE_FORCE;
Frederic Weisbecker7865e812010-04-14 19:42:07 +0200976 }
977
Namhyung Kimbea03402012-04-26 14:15:15 +0900978 if (nr_cgroups && !rec->opts.target.system_wide) {
Namhyung Kim3780f482012-05-29 13:22:57 +0900979 ui__error("cgroup monitoring only available in"
980 " system-wide mode\n");
Stephane Eranian023695d2011-02-14 11:20:01 +0200981 usage_with_options(record_usage, record_options);
982 }
983
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200984 symbol__init();
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -0200985
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300986 if (symbol_conf.kptr_restrict)
Arnaldo Carvalho de Melo646aaea2011-05-27 11:00:41 -0300987 pr_warning(
988"WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n"
989"check /proc/sys/kernel/kptr_restrict.\n\n"
990"Samples in kernel functions may not be resolved if a suitable vmlinux\n"
991"file is not found in the buildid cache or in the vmlinux path.\n\n"
992"Samples in kernel modules won't be resolved at all.\n\n"
993"If some relocation was applied (e.g. kexec) symbols may be misresolved\n"
994"even with a suitable vmlinux or kallsyms file.\n\n");
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300995
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200996 if (rec->no_buildid_cache || rec->no_buildid)
Stephane Eraniana1ac1d32010-06-17 11:39:01 +0200997 disable_buildid_cache();
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200998
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200999 if (evsel_list->nr_entries == 0 &&
1000 perf_evlist__add_default(evsel_list) < 0) {
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001001 pr_err("Not enough memory for event selector list\n");
1002 goto out_symbol_exit;
Peter Zijlstrabbd36e52009-06-11 23:11:50 +02001003 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +02001004
Namhyung Kim16ad2ff2012-05-07 14:09:02 +09001005 err = perf_target__validate(&rec->opts.target);
1006 if (err) {
1007 perf_target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
1008 ui__warning("%s", errbuf);
1009 }
Namhyung Kim4bd0f2d2012-04-26 14:15:18 +09001010
Namhyung Kim16ad2ff2012-05-07 14:09:02 +09001011 err = perf_target__parse_uid(&rec->opts.target);
1012 if (err) {
1013 int saved_errno = errno;
1014
1015 perf_target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
Namhyung Kim3780f482012-05-29 13:22:57 +09001016 ui__error("%s", errbuf);
Namhyung Kim16ad2ff2012-05-07 14:09:02 +09001017
1018 err = -saved_errno;
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -02001019 goto out_free_fd;
Namhyung Kim16ad2ff2012-05-07 14:09:02 +09001020 }
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -02001021
Namhyung Kim16ad2ff2012-05-07 14:09:02 +09001022 err = -ENOMEM;
Namhyung Kimb809ac12012-04-26 14:15:19 +09001023 if (perf_evlist__create_maps(evsel_list, &rec->opts.target) < 0)
Arnaldo Carvalho de Melodd7927f2011-01-12 14:28:51 -02001024 usage_with_options(record_usage, record_options);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001025
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001026 list_for_each_entry(pos, &evsel_list->entries, node) {
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -03001027 if (perf_header__push_event(pos->attr.config, perf_evsel__name(pos)))
Arnaldo Carvalho de Meload7f4e32011-01-17 18:28:13 -02001028 goto out_free_fd;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001029 }
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -02001030
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001031 if (rec->opts.user_interval != ULLONG_MAX)
1032 rec->opts.default_interval = rec->opts.user_interval;
1033 if (rec->opts.user_freq != UINT_MAX)
1034 rec->opts.freq = rec->opts.user_freq;
Frederic Weisbeckerf9212812010-04-14 22:09:02 +02001035
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001036 /*
1037 * User specified count overrides default frequency.
1038 */
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001039 if (rec->opts.default_interval)
1040 rec->opts.freq = 0;
1041 else if (rec->opts.freq) {
1042 rec->opts.default_interval = rec->opts.freq;
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001043 } else {
Namhyung Kim3780f482012-05-29 13:22:57 +09001044 ui__error("frequency and count are zero, aborting\n");
Arnaldo Carvalho de Melo39d17da2010-07-29 14:08:55 -03001045 err = -EINVAL;
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -02001046 goto out_free_fd;
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001047 }
1048
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001049 err = __cmd_record(&record, argc, argv);
Arnaldo Carvalho de Melo39d17da2010-07-29 14:08:55 -03001050out_free_fd:
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001051 perf_evlist__delete_maps(evsel_list);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03001052out_symbol_exit:
1053 symbol__exit();
Arnaldo Carvalho de Melo39d17da2010-07-29 14:08:55 -03001054 return err;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +02001055}