perf record: Use perf_evlist__mmap
[deliverable/linux.git] / tools / perf / builtin-record.c
CommitLineData
abaff32a 1/*
bf9e1876
IM
2 * 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.
abaff32a 7 */
b8f46c5a
XG
8#define _FILE_OFFSET_BITS 64
9
16f762a2 10#include "builtin.h"
bf9e1876
IM
11
12#include "perf.h"
13
6122e4e4 14#include "util/build-id.h"
6eda5838 15#include "util/util.h"
0e9b20b8 16#include "util/parse-options.h"
8ad8db37 17#include "util/parse-events.h"
6eda5838 18
7c6a1c65 19#include "util/header.h"
66e274f3 20#include "util/event.h"
361c99a6 21#include "util/evlist.h"
69aad6f1 22#include "util/evsel.h"
8f28827a 23#include "util/debug.h"
94c744b6 24#include "util/session.h"
8d06367f 25#include "util/symbol.h"
a12b51c4 26#include "util/cpumap.h"
7c6a1c65 27
97124d5e 28#include <unistd.h>
de9ac07b 29#include <sched.h>
a41794cd 30#include <sys/mman.h>
de9ac07b 31
69aad6f1 32#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
0a27d7f9 33#define SID(e, x, y) xyarray__entry(e->id, x, y)
69aad6f1 34
7865e817
FW
35enum write_mode_t {
36 WRITE_FORCE,
37 WRITE_APPEND
38};
39
3de29cab
SE
40static u64 user_interval = ULLONG_MAX;
41static u64 default_interval = 0;
640c03ce 42static u64 sample_type;
a21ca2ca 43
60d567e2 44static struct cpu_map *cpus;
de9ac07b 45static unsigned int page_size;
42e59d7d 46static unsigned int mmap_pages = 128;
f9212819 47static unsigned int user_freq = UINT_MAX;
42e59d7d 48static int freq = 1000;
de9ac07b 49static int output;
529870e3 50static int pipe_output = 0;
23ac9cbe 51static const char *output_name = "perf.data";
42e59d7d 52static int group = 0;
1967936d 53static int realtime_prio = 0;
acac03fa 54static bool nodelay = false;
c0555642 55static bool raw_samples = false;
9c90a61c 56static bool sample_id_all_avail = true;
c0555642 57static bool system_wide = false;
42e59d7d 58static pid_t target_pid = -1;
d6d901c2 59static pid_t target_tid = -1;
5c98d466 60static struct thread_map *threads;
42e59d7d 61static pid_t child_pid = -1;
2e6cdf99 62static bool no_inherit = false;
7865e817 63static enum write_mode_t write_mode = WRITE_FORCE;
c0555642
IM
64static bool call_graph = false;
65static bool inherit_stat = false;
66static bool no_samples = false;
67static bool sample_address = false;
9c90a61c 68static bool sample_time = false;
a1ac1d3c 69static bool no_buildid = false;
baa2f6ce 70static bool no_buildid_cache = false;
361c99a6 71static struct perf_evlist *evsel_list;
42e59d7d
IM
72
73static long samples = 0;
42e59d7d 74static u64 bytes_written = 0;
a21ca2ca 75
42e59d7d 76static int file_new = 1;
6122e4e4 77static off_t post_processing_offset;
7c6a1c65 78
94c744b6 79static struct perf_session *session;
c45c6ea2 80static const char *cpu_list;
f5970550 81
9215545e
TZ
82static void advance_output(size_t size)
83{
84 bytes_written += size;
85}
86
f5970550
PZ
87static void write_output(void *buf, size_t size)
88{
89 while (size) {
90 int ret = write(output, buf, size);
91
92 if (ret < 0)
93 die("failed to write");
94
95 size -= ret;
96 buf += ret;
97
98 bytes_written += ret;
99 }
100}
101
d8f66248 102static int process_synthesized_event(event_t *event,
640c03ce 103 struct sample_data *sample __used,
d8f66248 104 struct perf_session *self __used)
234fbbf5 105{
6122e4e4 106 write_output(event, event->header.size);
234fbbf5
ACM
107 return 0;
108}
109
744bd8aa 110static void mmap_read(struct perf_mmap *md)
de9ac07b 111{
744bd8aa 112 unsigned int head = perf_mmap__read_head(md);
de9ac07b
PZ
113 unsigned int old = md->prev;
114 unsigned char *data = md->base + page_size;
115 unsigned long size;
116 void *buf;
117 int diff;
118
de9ac07b
PZ
119 /*
120 * If we're further behind than half the buffer, there's a chance
2debbc83 121 * the writer will bite our tail and mess up the samples under us.
de9ac07b
PZ
122 *
123 * If we somehow ended up ahead of the head, we got messed up.
124 *
125 * In either case, truncate and restart at head.
126 */
127 diff = head - old;
9d91a6f7 128 if (diff < 0) {
ef365cef 129 fprintf(stderr, "WARNING: failed to keep up with mmap data\n");
de9ac07b
PZ
130 /*
131 * head points to a known good entry, start there.
132 */
133 old = head;
134 }
135
de9ac07b 136 if (old != head)
2debbc83 137 samples++;
de9ac07b
PZ
138
139 size = head - old;
140
141 if ((old & md->mask) + size != (head & md->mask)) {
142 buf = &data[old & md->mask];
143 size = md->mask + 1 - (old & md->mask);
144 old += size;
021e9f47 145
6122e4e4 146 write_output(buf, size);
de9ac07b
PZ
147 }
148
149 buf = &data[old & md->mask];
150 size = head - old;
151 old += size;
021e9f47 152
6122e4e4 153 write_output(buf, size);
de9ac07b
PZ
154
155 md->prev = old;
115d2d89 156 perf_mmap__write_tail(md, old);
de9ac07b
PZ
157}
158
159static volatile int done = 0;
f7b7c26e 160static volatile int signr = -1;
de9ac07b 161
16c8a109 162static void sig_handler(int sig)
de9ac07b 163{
16c8a109 164 done = 1;
f7b7c26e
PZ
165 signr = sig;
166}
167
168static void sig_atexit(void)
169{
5ffc8881 170 if (child_pid > 0)
933da83a
CW
171 kill(child_pid, SIGTERM);
172
18483b81 173 if (signr == -1 || signr == SIGUSR1)
f7b7c26e
PZ
174 return;
175
176 signal(signr, SIG_DFL);
177 kill(getpid(), signr);
de9ac07b
PZ
178}
179
cdd6c482 180static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
7c6a1c65
PZ
181{
182 struct perf_header_attr *h_attr;
183
94c744b6
ACM
184 if (nr < session->header.attrs) {
185 h_attr = session->header.attr[nr];
7c6a1c65
PZ
186 } else {
187 h_attr = perf_header_attr__new(a);
dc79c0fc 188 if (h_attr != NULL)
94c744b6 189 if (perf_header__add_attr(&session->header, h_attr) < 0) {
11deb1f9
ACM
190 perf_header_attr__delete(h_attr);
191 h_attr = NULL;
192 }
7c6a1c65
PZ
193 }
194
195 return h_attr;
196}
197
0a27d7f9 198static void create_counter(struct perf_evsel *evsel, int cpu)
de9ac07b 199{
69aad6f1
ACM
200 char *filter = evsel->filter;
201 struct perf_event_attr *attr = &evsel->attr;
7c6a1c65 202 struct perf_header_attr *h_attr;
0a27d7f9 203 struct perf_sample_id *sid;
d6d901c2 204 int thread_index;
c171b552 205 int ret;
dd7927f4
ACM
206
207 for (thread_index = 0; thread_index < threads->nr; thread_index++) {
208 h_attr = get_header_attr(attr, evsel->idx);
209 if (h_attr == NULL)
210 die("nomem\n");
211
212 if (!file_new) {
213 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
214 fprintf(stderr, "incompatible append\n");
215 exit(-1);
216 }
217 }
218
0a27d7f9
ACM
219 sid = SID(evsel, cpu, thread_index);
220 if (perf_header_attr__add_id(h_attr, sid->id) < 0) {
dd7927f4
ACM
221 pr_warning("Not enough memory to add id\n");
222 exit(-1);
223 }
224
dd7927f4
ACM
225 if (filter != NULL) {
226 ret = ioctl(FD(evsel, cpu, thread_index),
227 PERF_EVENT_IOC_SET_FILTER, filter);
228 if (ret) {
229 error("failed to set filter with %d (%s)\n", errno,
230 strerror(errno));
231 exit(-1);
232 }
233 }
234 }
235
236 if (!sample_type)
237 sample_type = attr->sample_type;
238}
239
240static void config_attr(struct perf_evsel *evsel, struct perf_evlist *evlist)
241{
242 struct perf_event_attr *attr = &evsel->attr;
243 int track = !evsel->idx; /* only the first counter needs these */
7c6a1c65
PZ
244
245 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
246 PERF_FORMAT_TOTAL_TIME_RUNNING |
247 PERF_FORMAT_ID;
16c8a109 248
3a9f131f 249 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
3efa1cc9 250
361c99a6 251 if (evlist->nr_entries > 1)
8907fd60
EM
252 attr->sample_type |= PERF_SAMPLE_ID;
253
f9212819
FW
254 /*
255 * We default some events to a 1 default interval. But keep
256 * it a weak assumption overridable by the user.
257 */
258 if (!attr->sample_period || (user_freq != UINT_MAX &&
3de29cab 259 user_interval != ULLONG_MAX)) {
f9212819
FW
260 if (freq) {
261 attr->sample_type |= PERF_SAMPLE_PERIOD;
262 attr->freq = 1;
263 attr->sample_freq = freq;
264 } else {
265 attr->sample_period = default_interval;
266 }
1dba15e7 267 }
3efa1cc9 268
649c48a9
PZ
269 if (no_samples)
270 attr->sample_freq = 0;
271
272 if (inherit_stat)
273 attr->inherit_stat = 1;
274
3af9e859 275 if (sample_address) {
4bba828d 276 attr->sample_type |= PERF_SAMPLE_ADDR;
3af9e859
EM
277 attr->mmap_data = track;
278 }
4bba828d 279
3efa1cc9
IM
280 if (call_graph)
281 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
282
f60f3593
AS
283 if (system_wide)
284 attr->sample_type |= PERF_SAMPLE_CPU;
285
a43d3f08
ACM
286 if (sample_id_all_avail &&
287 (sample_time || system_wide || !no_inherit || cpu_list))
9c90a61c
ACM
288 attr->sample_type |= PERF_SAMPLE_TIME;
289
cd6feeea 290 if (raw_samples) {
6ddf259d 291 attr->sample_type |= PERF_SAMPLE_TIME;
daac07b2 292 attr->sample_type |= PERF_SAMPLE_RAW;
cd6feeea
IM
293 attr->sample_type |= PERF_SAMPLE_CPU;
294 }
f413cdb8 295
acac03fa
KS
296 if (nodelay) {
297 attr->watermark = 0;
298 attr->wakeup_events = 1;
299 }
300
a21ca2ca
IM
301 attr->mmap = track;
302 attr->comm = track;
dd7927f4 303
2e6cdf99 304 if (target_pid == -1 && target_tid == -1 && !system_wide) {
46be604b 305 attr->disabled = 1;
bedbfdea 306 attr->enable_on_exec = 1;
46be604b 307 }
dd7927f4 308}
bedbfdea 309
dd7927f4
ACM
310static void open_counters(struct perf_evlist *evlist)
311{
312 struct perf_evsel *pos;
313 int cpu;
314
315 list_for_each_entry(pos, &evlist->entries, node) {
316 struct perf_event_attr *attr = &pos->attr;
317 /*
318 * Check if parse_single_tracepoint_event has already asked for
319 * PERF_SAMPLE_TIME.
320 *
321 * XXX this is kludgy but short term fix for problems introduced by
322 * eac23d1c that broke 'perf script' by having different sample_types
323 * when using multiple tracepoint events when we use a perf binary
324 * that tries to use sample_id_all on an older kernel.
325 *
326 * We need to move counter creation to perf_session, support
327 * different sample_types, etc.
328 */
329 bool time_needed = attr->sample_type & PERF_SAMPLE_TIME;
d6d901c2 330
dd7927f4
ACM
331 config_attr(pos, evlist);
332retry_sample_id:
333 attr->sample_id_all = sample_id_all_avail ? 1 : 0;
334try_again:
335 if (perf_evsel__open(pos, cpus, threads, group, !no_inherit) < 0) {
d6d901c2
ZY
336 int err = errno;
337
338 if (err == EPERM || err == EACCES)
339 die("Permission error - are you root?\n"
340 "\t Consider tweaking"
341 " /proc/sys/kernel/perf_event_paranoid.\n");
c45c6ea2 342 else if (err == ENODEV && cpu_list) {
d6d901c2
ZY
343 die("No such device - did you specify"
344 " an out-of-range profile CPU?\n");
9c90a61c
ACM
345 } else if (err == EINVAL && sample_id_all_avail) {
346 /*
347 * Old kernel, no attr->sample_id_type_all field
348 */
349 sample_id_all_avail = false;
a43d3f08 350 if (!sample_time && !raw_samples && !time_needed)
eac23d1c
IM
351 attr->sample_type &= ~PERF_SAMPLE_TIME;
352
9c90a61c 353 goto retry_sample_id;
d6d901c2 354 }
3da297a6 355
d6d901c2
ZY
356 /*
357 * If it's cycles then fall back to hrtimer
358 * based cpu-clock-tick sw counter, which
359 * is always available even if no PMU support:
360 */
361 if (attr->type == PERF_TYPE_HARDWARE
362 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
363
364 if (verbose)
365 warning(" ... trying to fall back to cpu-clock-ticks\n");
366 attr->type = PERF_TYPE_SOFTWARE;
367 attr->config = PERF_COUNT_SW_CPU_CLOCK;
368 goto try_again;
369 }
370 printf("\n");
d9cf837e 371 error("sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information.\n",
dd7927f4 372 err, strerror(err));
bfd45118
SK
373
374#if defined(__i386__) || defined(__x86_64__)
d6d901c2
ZY
375 if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP)
376 die("No hardware sampling interrupt available."
377 " No APIC? If so then you can boot the kernel"
378 " with the \"lapic\" boot parameter to"
379 " force-enable it.\n");
bfd45118
SK
380#endif
381
d6d901c2 382 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
c171b552
LZ
383 }
384 }
a43d3f08 385
0a27d7f9
ACM
386 if (perf_evlist__mmap(evlist, cpus, threads, mmap_pages, false) < 0)
387 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
388
dd7927f4
ACM
389 for (cpu = 0; cpu < cpus->nr; ++cpu) {
390 list_for_each_entry(pos, &evlist->entries, node)
0a27d7f9 391 create_counter(pos, cpu);
dd7927f4 392 }
16c8a109
PZ
393}
394
6122e4e4
ACM
395static int process_buildids(void)
396{
397 u64 size = lseek(output, 0, SEEK_CUR);
398
9f591fd7
ACM
399 if (size == 0)
400 return 0;
401
6122e4e4
ACM
402 session->fd = output;
403 return __perf_session__process_events(session, post_processing_offset,
404 size - post_processing_offset,
405 size, &build_id__mark_dso_hit_ops);
406}
407
f5970550
PZ
408static void atexit_header(void)
409{
c7929e47
TZ
410 if (!pipe_output) {
411 session->header.data_size += bytes_written;
f5970550 412
baa2f6ce
ACM
413 if (!no_buildid)
414 process_buildids();
361c99a6 415 perf_header__write(&session->header, evsel_list, output, true);
39d17dac 416 perf_session__delete(session);
361c99a6 417 perf_evlist__delete(evsel_list);
d65a458b 418 symbol__exit();
c7929e47 419 }
f5970550
PZ
420}
421
23346f21 422static void event__synthesize_guest_os(struct machine *machine, void *data)
a1645ce1
ZY
423{
424 int err;
23346f21 425 struct perf_session *psession = data;
a1645ce1 426
23346f21 427 if (machine__is_host(machine))
a1645ce1
ZY
428 return;
429
430 /*
431 *As for guest kernel when processing subcommand record&report,
432 *we arrange module mmap prior to guest kernel mmap and trigger
433 *a preload dso because default guest module symbols are loaded
434 *from guest kallsyms instead of /lib/modules/XXX/XXX. This
435 *method is used to avoid symbol missing when the first addr is
436 *in module instead of in guest kernel.
437 */
438 err = event__synthesize_modules(process_synthesized_event,
23346f21 439 psession, machine);
a1645ce1
ZY
440 if (err < 0)
441 pr_err("Couldn't record guest kernel [%d]'s reference"
23346f21 442 " relocation symbol.\n", machine->pid);
a1645ce1 443
a1645ce1
ZY
444 /*
445 * We use _stext for guest kernel because guest kernel's /proc/kallsyms
446 * have no _text sometimes.
447 */
448 err = event__synthesize_kernel_mmap(process_synthesized_event,
23346f21 449 psession, machine, "_text");
a1645ce1
ZY
450 if (err < 0)
451 err = event__synthesize_kernel_mmap(process_synthesized_event,
23346f21 452 psession, machine, "_stext");
a1645ce1
ZY
453 if (err < 0)
454 pr_err("Couldn't record guest kernel [%d]'s reference"
23346f21 455 " relocation symbol.\n", machine->pid);
a1645ce1
ZY
456}
457
98402807
FW
458static struct perf_event_header finished_round_event = {
459 .size = sizeof(struct perf_event_header),
460 .type = PERF_RECORD_FINISHED_ROUND,
461};
462
463static void mmap_read_all(void)
464{
0e2e63dd 465 int i;
98402807 466
dd7927f4 467 for (i = 0; i < cpus->nr; i++) {
0a27d7f9
ACM
468 if (evsel_list->mmap[i].base)
469 mmap_read(&evsel_list->mmap[i]);
98402807
FW
470 }
471
472 if (perf_header__has_feat(&session->header, HEADER_TRACE_INFO))
473 write_output(&finished_round_event, sizeof(finished_round_event));
474}
475
d4db3f16 476static int __cmd_record(int argc, const char **argv)
16c8a109 477{
69aad6f1 478 int i;
abaff32a 479 struct stat st;
abaff32a 480 int flags;
4dc0a04b 481 int err;
8b412664 482 unsigned long waking = 0;
856e9660 483 int child_ready_pipe[2], go_pipe[2];
46be604b 484 const bool forks = argc > 0;
856e9660 485 char buf;
23346f21 486 struct machine *machine;
de9ac07b
PZ
487
488 page_size = sysconf(_SC_PAGE_SIZE);
de9ac07b 489
f5970550
PZ
490 atexit(sig_atexit);
491 signal(SIGCHLD, sig_handler);
492 signal(SIGINT, sig_handler);
18483b81 493 signal(SIGUSR1, sig_handler);
f5970550 494
d4db3f16 495 if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
856e9660
PZ
496 perror("failed to create pipes");
497 exit(-1);
498 }
499
529870e3
TZ
500 if (!strcmp(output_name, "-"))
501 pipe_output = 1;
502 else if (!stat(output_name, &st) && st.st_size) {
7865e817 503 if (write_mode == WRITE_FORCE) {
b38d3464
ACM
504 char oldname[PATH_MAX];
505 snprintf(oldname, sizeof(oldname), "%s.old",
506 output_name);
507 unlink(oldname);
508 rename(output_name, oldname);
266e0e21 509 }
7865e817
FW
510 } else if (write_mode == WRITE_APPEND) {
511 write_mode = WRITE_FORCE;
97124d5e
PZ
512 }
513
f887f301 514 flags = O_CREAT|O_RDWR;
7865e817 515 if (write_mode == WRITE_APPEND)
f5970550 516 file_new = 0;
abaff32a
IM
517 else
518 flags |= O_TRUNC;
519
529870e3
TZ
520 if (pipe_output)
521 output = STDOUT_FILENO;
522 else
523 output = open(output_name, flags, S_IRUSR | S_IWUSR);
de9ac07b
PZ
524 if (output < 0) {
525 perror("failed to create output file");
526 exit(-1);
527 }
528
7865e817 529 session = perf_session__new(output_name, O_WRONLY,
21ef97f0 530 write_mode == WRITE_FORCE, false, NULL);
94c744b6 531 if (session == NULL) {
a9a70bbc
ACM
532 pr_err("Not enough memory for reading perf file header\n");
533 return -1;
534 }
535
baa2f6ce
ACM
536 if (!no_buildid)
537 perf_header__set_feat(&session->header, HEADER_BUILD_ID);
538
4dc0a04b 539 if (!file_new) {
8dc58101 540 err = perf_header__read(session, output);
4dc0a04b 541 if (err < 0)
39d17dac 542 goto out_delete_session;
4dc0a04b
ACM
543 }
544
361c99a6 545 if (have_tracepoints(&evsel_list->entries))
94c744b6 546 perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
03456a15 547
39d17dac
ACM
548 /*
549 * perf_session__delete(session) will be called at atexit_header()
550 */
f5970550
PZ
551 atexit(atexit_header);
552
d4db3f16 553 if (forks) {
46be604b 554 child_pid = fork();
2fb750e8 555 if (child_pid < 0) {
856e9660
PZ
556 perror("failed to fork");
557 exit(-1);
558 }
7c6a1c65 559
46be604b 560 if (!child_pid) {
529870e3
TZ
561 if (pipe_output)
562 dup2(2, 1);
856e9660
PZ
563 close(child_ready_pipe[0]);
564 close(go_pipe[1]);
565 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
566
567 /*
568 * Do a dummy execvp to get the PLT entry resolved,
569 * so we avoid the resolver overhead on the real
570 * execvp call.
571 */
572 execvp("", (char **)argv);
573
574 /*
575 * Tell the parent we're ready to go
576 */
577 close(child_ready_pipe[1]);
578
579 /*
580 * Wait until the parent tells us to go.
581 */
582 if (read(go_pipe[0], &buf, 1) == -1)
583 perror("unable to read pipe");
584
585 execvp(argv[0], (char **)argv);
586
587 perror(argv[0]);
18483b81 588 kill(getppid(), SIGUSR1);
856e9660 589 exit(-1);
0a5ac846 590 }
856e9660 591
d6d901c2 592 if (!system_wide && target_tid == -1 && target_pid == -1)
5c98d466 593 threads->map[0] = child_pid;
d6d901c2 594
856e9660
PZ
595 close(child_ready_pipe[1]);
596 close(go_pipe[0]);
597 /*
598 * wait for child to settle
599 */
600 if (read(child_ready_pipe[0], &buf, 1) == -1) {
601 perror("unable to read pipe");
602 exit(-1);
603 }
604 close(child_ready_pipe[0]);
605 }
606
dd7927f4 607 open_counters(evsel_list);
de9ac07b 608
640c03ce
ACM
609 perf_session__set_sample_type(session, sample_type);
610
529870e3
TZ
611 if (pipe_output) {
612 err = perf_header__write_pipe(output);
613 if (err < 0)
614 return err;
615 } else if (file_new) {
361c99a6
ACM
616 err = perf_header__write(&session->header, evsel_list,
617 output, false);
d5eed904
ACM
618 if (err < 0)
619 return err;
56b03f3c
ACM
620 }
621
6122e4e4
ACM
622 post_processing_offset = lseek(output, 0, SEEK_CUR);
623
9c90a61c
ACM
624 perf_session__set_sample_id_all(session, sample_id_all_avail);
625
2c46dbb5
TZ
626 if (pipe_output) {
627 err = event__synthesize_attrs(&session->header,
628 process_synthesized_event,
629 session);
630 if (err < 0) {
631 pr_err("Couldn't synthesize attrs.\n");
632 return err;
633 }
cd19a035
TZ
634
635 err = event__synthesize_event_types(process_synthesized_event,
636 session);
637 if (err < 0) {
638 pr_err("Couldn't synthesize event_types.\n");
639 return err;
640 }
9215545e 641
361c99a6 642 if (have_tracepoints(&evsel_list->entries)) {
63e0c771
TZ
643 /*
644 * FIXME err <= 0 here actually means that
645 * there were no tracepoints so its not really
646 * an error, just that we don't need to
647 * synthesize anything. We really have to
648 * return this more properly and also
649 * propagate errors that now are calling die()
650 */
361c99a6 651 err = event__synthesize_tracing_data(output, evsel_list,
63e0c771
TZ
652 process_synthesized_event,
653 session);
654 if (err <= 0) {
655 pr_err("Couldn't record tracing data.\n");
656 return err;
657 }
2c9faa06 658 advance_output(err);
63e0c771 659 }
2c46dbb5
TZ
660 }
661
23346f21
ACM
662 machine = perf_session__find_host_machine(session);
663 if (!machine) {
a1645ce1
ZY
664 pr_err("Couldn't find native kernel information.\n");
665 return -1;
666 }
667
56b03f3c 668 err = event__synthesize_kernel_mmap(process_synthesized_event,
23346f21 669 session, machine, "_text");
70162138
ACM
670 if (err < 0)
671 err = event__synthesize_kernel_mmap(process_synthesized_event,
23346f21 672 session, machine, "_stext");
c1a3a4b9
ACM
673 if (err < 0)
674 pr_err("Couldn't record kernel reference relocation symbol\n"
675 "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
676 "Check /proc/kallsyms permission or run as root.\n");
b7cece76 677
a1645ce1 678 err = event__synthesize_modules(process_synthesized_event,
23346f21 679 session, machine);
c1a3a4b9
ACM
680 if (err < 0)
681 pr_err("Couldn't record kernel module information.\n"
682 "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
683 "Check /proc/modules permission or run as root.\n");
684
a1645ce1 685 if (perf_guest)
23346f21 686 perf_session__process_machines(session, event__synthesize_guest_os);
7c6a1c65 687
cf103a14 688 if (!system_wide)
d6d901c2 689 event__synthesize_thread(target_tid, process_synthesized_event,
d8f66248 690 session);
234fbbf5 691 else
d8f66248 692 event__synthesize_threads(process_synthesized_event, session);
7c6a1c65 693
de9ac07b
PZ
694 if (realtime_prio) {
695 struct sched_param param;
696
697 param.sched_priority = realtime_prio;
698 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
6beba7ad 699 pr_err("Could not set realtime priority.\n");
de9ac07b
PZ
700 exit(-1);
701 }
702 }
703
856e9660
PZ
704 /*
705 * Let the child rip
706 */
d4db3f16
ACM
707 if (forks)
708 close(go_pipe[1]);
856e9660 709
649c48a9 710 for (;;) {
2debbc83 711 int hits = samples;
d6d901c2 712 int thread;
de9ac07b 713
98402807 714 mmap_read_all();
de9ac07b 715
649c48a9
PZ
716 if (hits == samples) {
717 if (done)
718 break;
5c581041 719 err = poll(evsel_list->pollfd, evsel_list->nr_fds, -1);
8b412664
PZ
720 waking++;
721 }
722
723 if (done) {
dd7927f4 724 for (i = 0; i < cpus->nr; i++) {
69aad6f1
ACM
725 struct perf_evsel *pos;
726
361c99a6 727 list_for_each_entry(pos, &evsel_list->entries, node) {
d6d901c2 728 for (thread = 0;
5c98d466 729 thread < threads->nr;
d6d901c2 730 thread++)
69aad6f1 731 ioctl(FD(pos, i, thread),
d6d901c2
ZY
732 PERF_EVENT_IOC_DISABLE);
733 }
8b412664 734 }
649c48a9 735 }
de9ac07b
PZ
736 }
737
18483b81 738 if (quiet || signr == SIGUSR1)
b44308f5
ACM
739 return 0;
740
8b412664
PZ
741 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
742
021e9f47
IM
743 /*
744 * Approximate RIP event size: 24 bytes.
745 */
746 fprintf(stderr,
9486aa38 747 "[ perf record: Captured and wrote %.3f MB %s (~%" PRIu64 " samples) ]\n",
021e9f47
IM
748 (double)bytes_written / 1024.0 / 1024.0,
749 output_name,
750 bytes_written / 24);
addc2785 751
de9ac07b 752 return 0;
39d17dac
ACM
753
754out_delete_session:
755 perf_session__delete(session);
756 return err;
de9ac07b 757}
0e9b20b8 758
0e9b20b8 759static const char * const record_usage[] = {
9e096753
MG
760 "perf record [<options>] [<command>]",
761 "perf record [<options>] -- <command> [<options>]",
0e9b20b8
IM
762 NULL
763};
764
7865e817
FW
765static bool force, append_file;
766
bca647aa 767const struct option record_options[] = {
361c99a6 768 OPT_CALLBACK('e', "event", &evsel_list, "event",
86847b62
TG
769 "event selector. use 'perf list' to list available events",
770 parse_events),
361c99a6 771 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
c171b552 772 "event filter", parse_filter),
0e9b20b8 773 OPT_INTEGER('p', "pid", &target_pid,
d6d901c2
ZY
774 "record events on existing process id"),
775 OPT_INTEGER('t', "tid", &target_tid,
776 "record events on existing thread id"),
0e9b20b8
IM
777 OPT_INTEGER('r', "realtime", &realtime_prio,
778 "collect data with this RT SCHED_FIFO priority"),
acac03fa
KS
779 OPT_BOOLEAN('D', "no-delay", &nodelay,
780 "collect data without buffering"),
daac07b2
FW
781 OPT_BOOLEAN('R', "raw-samples", &raw_samples,
782 "collect raw sample records from all opened counters"),
0e9b20b8
IM
783 OPT_BOOLEAN('a', "all-cpus", &system_wide,
784 "system-wide collection from all CPUs"),
abaff32a
IM
785 OPT_BOOLEAN('A', "append", &append_file,
786 "append to the output file to do incremental profiling"),
c45c6ea2
SE
787 OPT_STRING('C', "cpu", &cpu_list, "cpu",
788 "list of cpus to monitor"),
97124d5e 789 OPT_BOOLEAN('f', "force", &force,
7865e817 790 "overwrite existing data file (deprecated)"),
3de29cab 791 OPT_U64('c', "count", &user_interval, "event period to sample"),
abaff32a
IM
792 OPT_STRING('o', "output", &output_name, "file",
793 "output file name"),
2e6cdf99
SE
794 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
795 "child tasks do not inherit counters"),
1967936d
ACM
796 OPT_UINTEGER('F', "freq", &user_freq, "profile at this frequency"),
797 OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"),
3efa1cc9
IM
798 OPT_BOOLEAN('g', "call-graph", &call_graph,
799 "do call-graph (stack chain/backtrace) recording"),
c0555642 800 OPT_INCR('v', "verbose", &verbose,
3da297a6 801 "be more verbose (show counter open errors, etc)"),
b44308f5 802 OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"),
649c48a9
PZ
803 OPT_BOOLEAN('s', "stat", &inherit_stat,
804 "per thread counts"),
4bba828d
AB
805 OPT_BOOLEAN('d', "data", &sample_address,
806 "Sample addresses"),
9c90a61c 807 OPT_BOOLEAN('T', "timestamp", &sample_time, "Sample timestamps"),
649c48a9
PZ
808 OPT_BOOLEAN('n', "no-samples", &no_samples,
809 "don't sample"),
baa2f6ce 810 OPT_BOOLEAN('N', "no-buildid-cache", &no_buildid_cache,
a1ac1d3c 811 "do not update the buildid cache"),
baa2f6ce
ACM
812 OPT_BOOLEAN('B', "no-buildid", &no_buildid,
813 "do not collect buildids in perf.data"),
0e9b20b8
IM
814 OPT_END()
815};
816
f37a291c 817int cmd_record(int argc, const char **argv, const char *prefix __used)
0e9b20b8 818{
69aad6f1
ACM
819 int err = -ENOMEM;
820 struct perf_evsel *pos;
0e9b20b8 821
361c99a6
ACM
822 evsel_list = perf_evlist__new();
823 if (evsel_list == NULL)
824 return -ENOMEM;
825
bca647aa 826 argc = parse_options(argc, argv, record_options, record_usage,
655000e7 827 PARSE_OPT_STOP_AT_NON_OPTION);
d6d901c2 828 if (!argc && target_pid == -1 && target_tid == -1 &&
c45c6ea2 829 !system_wide && !cpu_list)
bca647aa 830 usage_with_options(record_usage, record_options);
0e9b20b8 831
7865e817
FW
832 if (force && append_file) {
833 fprintf(stderr, "Can't overwrite and append at the same time."
834 " You need to choose between -f and -A");
bca647aa 835 usage_with_options(record_usage, record_options);
7865e817
FW
836 } else if (append_file) {
837 write_mode = WRITE_APPEND;
838 } else {
839 write_mode = WRITE_FORCE;
840 }
841
655000e7 842 symbol__init();
baa2f6ce
ACM
843
844 if (no_buildid_cache || no_buildid)
a1ac1d3c 845 disable_buildid_cache();
655000e7 846
361c99a6
ACM
847 if (evsel_list->nr_entries == 0 &&
848 perf_evlist__add_default(evsel_list) < 0) {
69aad6f1
ACM
849 pr_err("Not enough memory for event selector list\n");
850 goto out_symbol_exit;
bbd36e5e 851 }
0e9b20b8 852
5c98d466 853 if (target_pid != -1)
d6d901c2 854 target_tid = target_pid;
d6d901c2 855
5c98d466
ACM
856 threads = thread_map__new(target_pid, target_tid);
857 if (threads == NULL) {
858 pr_err("Problems finding threads of monitor\n");
859 usage_with_options(record_usage, record_options);
d6d901c2
ZY
860 }
861
dd7927f4
ACM
862 if (target_tid != -1)
863 cpus = cpu_map__dummy_new();
864 else
865 cpus = cpu_map__new(cpu_list);
866
867 if (cpus == NULL)
868 usage_with_options(record_usage, record_options);
69aad6f1 869
361c99a6 870 list_for_each_entry(pos, &evsel_list->entries, node) {
5c98d466 871 if (perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0)
69aad6f1 872 goto out_free_fd;
ad7f4e3f
ACM
873 if (perf_header__push_event(pos->attr.config, event_name(pos)))
874 goto out_free_fd;
d6d901c2 875 }
5c581041
ACM
876
877 if (perf_evlist__alloc_pollfd(evsel_list, cpus->nr, threads->nr) < 0)
39d17dac 878 goto out_free_fd;
d6d901c2 879
3de29cab 880 if (user_interval != ULLONG_MAX)
f9212819
FW
881 default_interval = user_interval;
882 if (user_freq != UINT_MAX)
883 freq = user_freq;
884
7e4ff9e3
MG
885 /*
886 * User specified count overrides default frequency.
887 */
888 if (default_interval)
889 freq = 0;
890 else if (freq) {
891 default_interval = freq;
892 } else {
893 fprintf(stderr, "frequency and count are zero, aborting\n");
39d17dac 894 err = -EINVAL;
5c581041 895 goto out_free_fd;
7e4ff9e3
MG
896 }
897
39d17dac
ACM
898 err = __cmd_record(argc, argv);
899
39d17dac 900out_free_fd:
5c98d466
ACM
901 thread_map__delete(threads);
902 threads = NULL;
d65a458b
ACM
903out_symbol_exit:
904 symbol__exit();
39d17dac 905 return err;
0e9b20b8 906}
This page took 0.152605 seconds and 5 git commands to generate.