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.
8 #define _FILE_OFFSET_BITS 64
14 #include "util/build-id.h"
15 #include "util/util.h"
16 #include "util/parse-options.h"
17 #include "util/parse-events.h"
18 #include "util/string.h"
20 #include "util/header.h"
21 #include "util/event.h"
22 #include "util/debug.h"
23 #include "util/session.h"
24 #include "util/symbol.h"
25 #include "util/cpumap.h"
30 static int fd
[MAX_NR_CPUS
][MAX_COUNTERS
];
32 static long default_interval
= 0;
34 static int nr_cpus
= 0;
35 static unsigned int page_size
;
36 static unsigned int mmap_pages
= 128;
37 static int freq
= 1000;
39 static const char *output_name
= "perf.data";
41 static unsigned int realtime_prio
= 0;
42 static int raw_samples
= 0;
43 static int system_wide
= 0;
44 static int profile_cpu
= -1;
45 static pid_t target_pid
= -1;
46 static pid_t child_pid
= -1;
47 static int inherit
= 1;
49 static int append_file
= 0;
50 static int call_graph
= 0;
51 static int inherit_stat
= 0;
52 static int no_samples
= 0;
53 static int sample_address
= 0;
54 static int multiplex
= 0;
55 static int multiplex_fd
= -1;
57 static long samples
= 0;
58 static struct timeval last_read
;
59 static struct timeval this_read
;
61 static u64 bytes_written
= 0;
63 static struct pollfd event_array
[MAX_NR_CPUS
* MAX_COUNTERS
];
65 static int nr_poll
= 0;
66 static int nr_cpu
= 0;
68 static int file_new
= 1;
69 static off_t post_processing_offset
;
71 static struct perf_session
*session
;
80 static struct mmap_data mmap_array
[MAX_NR_CPUS
][MAX_COUNTERS
];
82 static unsigned long mmap_read_head(struct mmap_data
*md
)
84 struct perf_event_mmap_page
*pc
= md
->base
;
93 static void mmap_write_tail(struct mmap_data
*md
, unsigned long tail
)
95 struct perf_event_mmap_page
*pc
= md
->base
;
98 * ensure all reads are done before we write the tail out.
101 pc
->data_tail
= tail
;
104 static void write_output(void *buf
, size_t size
)
107 int ret
= write(output
, buf
, size
);
110 die("failed to write");
115 bytes_written
+= ret
;
119 static int process_synthesized_event(event_t
*event
,
120 struct perf_session
*self __used
)
122 write_output(event
, event
->header
.size
);
126 static void mmap_read(struct mmap_data
*md
)
128 unsigned int head
= mmap_read_head(md
);
129 unsigned int old
= md
->prev
;
130 unsigned char *data
= md
->base
+ page_size
;
135 gettimeofday(&this_read
, NULL
);
138 * If we're further behind than half the buffer, there's a chance
139 * the writer will bite our tail and mess up the samples under us.
141 * If we somehow ended up ahead of the head, we got messed up.
143 * In either case, truncate and restart at head.
150 timersub(&this_read
, &last_read
, &iv
);
151 msecs
= iv
.tv_sec
*1000 + iv
.tv_usec
/1000;
153 fprintf(stderr
, "WARNING: failed to keep up with mmap data."
154 " Last read %lu msecs ago.\n", msecs
);
157 * head points to a known good entry, start there.
162 last_read
= this_read
;
169 if ((old
& md
->mask
) + size
!= (head
& md
->mask
)) {
170 buf
= &data
[old
& md
->mask
];
171 size
= md
->mask
+ 1 - (old
& md
->mask
);
174 write_output(buf
, size
);
177 buf
= &data
[old
& md
->mask
];
181 write_output(buf
, size
);
184 mmap_write_tail(md
, old
);
187 static volatile int done
= 0;
188 static volatile int signr
= -1;
190 static void sig_handler(int sig
)
196 static void sig_atexit(void)
199 kill(child_pid
, SIGTERM
);
204 signal(signr
, SIG_DFL
);
205 kill(getpid(), signr
);
210 static struct perf_header_attr
*get_header_attr(struct perf_event_attr
*a
, int nr
)
212 struct perf_header_attr
*h_attr
;
214 if (nr
< session
->header
.attrs
) {
215 h_attr
= session
->header
.attr
[nr
];
217 h_attr
= perf_header_attr__new(a
);
219 if (perf_header__add_attr(&session
->header
, h_attr
) < 0) {
220 perf_header_attr__delete(h_attr
);
228 static void create_counter(int counter
, int cpu
, pid_t pid
)
230 char *filter
= filters
[counter
];
231 struct perf_event_attr
*attr
= attrs
+ counter
;
232 struct perf_header_attr
*h_attr
;
233 int track
= !counter
; /* only the first counter needs these */
242 attr
->read_format
= PERF_FORMAT_TOTAL_TIME_ENABLED
|
243 PERF_FORMAT_TOTAL_TIME_RUNNING
|
246 attr
->sample_type
|= PERF_SAMPLE_IP
| PERF_SAMPLE_TID
;
249 attr
->sample_type
|= PERF_SAMPLE_ID
;
252 attr
->sample_type
|= PERF_SAMPLE_PERIOD
;
254 attr
->sample_freq
= freq
;
258 attr
->sample_freq
= 0;
261 attr
->inherit_stat
= 1;
264 attr
->sample_type
|= PERF_SAMPLE_ADDR
;
267 attr
->sample_type
|= PERF_SAMPLE_CALLCHAIN
;
270 attr
->sample_type
|= PERF_SAMPLE_TIME
;
271 attr
->sample_type
|= PERF_SAMPLE_RAW
;
272 attr
->sample_type
|= PERF_SAMPLE_CPU
;
277 attr
->inherit
= inherit
;
281 fd
[nr_cpu
][counter
] = sys_perf_event_open(attr
, pid
, cpu
, group_fd
, 0);
283 if (fd
[nr_cpu
][counter
] < 0) {
286 if (err
== EPERM
|| err
== EACCES
)
287 die("Permission error - are you root?\n");
288 else if (err
== ENODEV
&& profile_cpu
!= -1)
289 die("No such device - did you specify an out-of-range profile CPU?\n");
292 * If it's cycles then fall back to hrtimer
293 * based cpu-clock-tick sw counter, which
294 * is always available even if no PMU support:
296 if (attr
->type
== PERF_TYPE_HARDWARE
297 && attr
->config
== PERF_COUNT_HW_CPU_CYCLES
) {
300 warning(" ... trying to fall back to cpu-clock-ticks\n");
301 attr
->type
= PERF_TYPE_SOFTWARE
;
302 attr
->config
= PERF_COUNT_SW_CPU_CLOCK
;
306 error("perfcounter syscall returned with %d (%s)\n",
307 fd
[nr_cpu
][counter
], strerror(err
));
309 #if defined(__i386__) || defined(__x86_64__)
310 if (attr
->type
== PERF_TYPE_HARDWARE
&& err
== EOPNOTSUPP
)
311 die("No hardware sampling interrupt available. No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.\n");
314 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
318 h_attr
= get_header_attr(attr
, counter
);
323 if (memcmp(&h_attr
->attr
, attr
, sizeof(*attr
))) {
324 fprintf(stderr
, "incompatible append\n");
329 if (read(fd
[nr_cpu
][counter
], &read_data
, sizeof(read_data
)) == -1) {
330 perror("Unable to read perf file descriptor\n");
334 if (perf_header_attr__add_id(h_attr
, read_data
.id
) < 0) {
335 pr_warning("Not enough memory to add id\n");
339 assert(fd
[nr_cpu
][counter
] >= 0);
340 fcntl(fd
[nr_cpu
][counter
], F_SETFL
, O_NONBLOCK
);
343 * First counter acts as the group leader:
345 if (group
&& group_fd
== -1)
346 group_fd
= fd
[nr_cpu
][counter
];
347 if (multiplex
&& multiplex_fd
== -1)
348 multiplex_fd
= fd
[nr_cpu
][counter
];
350 if (multiplex
&& fd
[nr_cpu
][counter
] != multiplex_fd
) {
352 ret
= ioctl(fd
[nr_cpu
][counter
], PERF_EVENT_IOC_SET_OUTPUT
, multiplex_fd
);
355 event_array
[nr_poll
].fd
= fd
[nr_cpu
][counter
];
356 event_array
[nr_poll
].events
= POLLIN
;
359 mmap_array
[nr_cpu
][counter
].counter
= counter
;
360 mmap_array
[nr_cpu
][counter
].prev
= 0;
361 mmap_array
[nr_cpu
][counter
].mask
= mmap_pages
*page_size
- 1;
362 mmap_array
[nr_cpu
][counter
].base
= mmap(NULL
, (mmap_pages
+1)*page_size
,
363 PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
[nr_cpu
][counter
], 0);
364 if (mmap_array
[nr_cpu
][counter
].base
== MAP_FAILED
) {
365 error("failed to mmap with %d (%s)\n", errno
, strerror(errno
));
370 if (filter
!= NULL
) {
371 ret
= ioctl(fd
[nr_cpu
][counter
],
372 PERF_EVENT_IOC_SET_FILTER
, filter
);
374 error("failed to set filter with %d (%s)\n", errno
,
380 ioctl(fd
[nr_cpu
][counter
], PERF_EVENT_IOC_ENABLE
);
383 static void open_counters(int cpu
, pid_t pid
)
388 for (counter
= 0; counter
< nr_counters
; counter
++)
389 create_counter(counter
, cpu
, pid
);
394 static int process_buildids(void)
396 u64 size
= lseek(output
, 0, SEEK_CUR
);
401 session
->fd
= output
;
402 return __perf_session__process_events(session
, post_processing_offset
,
403 size
- post_processing_offset
,
404 size
, &build_id__mark_dso_hit_ops
);
407 static void atexit_header(void)
409 session
->header
.data_size
+= bytes_written
;
412 perf_header__write(&session
->header
, output
, true);
415 static int __cmd_record(int argc
, const char **argv
)
422 unsigned long waking
= 0;
423 int child_ready_pipe
[2], go_pipe
[2];
424 const bool forks
= target_pid
== -1 && argc
> 0;
427 page_size
= sysconf(_SC_PAGE_SIZE
);
430 signal(SIGCHLD
, sig_handler
);
431 signal(SIGINT
, sig_handler
);
433 if (forks
&& (pipe(child_ready_pipe
) < 0 || pipe(go_pipe
) < 0)) {
434 perror("failed to create pipes");
438 if (!stat(output_name
, &st
) && st
.st_size
) {
441 pr_err("Error, output file %s exists, use -A "
442 "to append or -f to overwrite.\n",
447 char oldname
[PATH_MAX
];
448 snprintf(oldname
, sizeof(oldname
), "%s.old",
451 rename(output_name
, oldname
);
457 flags
= O_CREAT
|O_RDWR
;
463 output
= open(output_name
, flags
, S_IRUSR
|S_IWUSR
);
465 perror("failed to create output file");
469 session
= perf_session__new(output_name
, O_WRONLY
, force
);
470 if (session
== NULL
) {
471 pr_err("Not enough memory for reading perf file header\n");
476 err
= perf_header__read(&session
->header
, output
);
482 perf_header__set_feat(&session
->header
, HEADER_TRACE_INFO
);
484 for (i
= 0; i
< nr_counters
; i
++) {
485 if (attrs
[i
].sample_type
& PERF_SAMPLE_RAW
) {
486 perf_header__set_feat(&session
->header
, HEADER_TRACE_INFO
);
492 atexit(atexit_header
);
497 perror("failed to fork");
502 close(child_ready_pipe
[0]);
504 fcntl(go_pipe
[0], F_SETFD
, FD_CLOEXEC
);
507 * Do a dummy execvp to get the PLT entry resolved,
508 * so we avoid the resolver overhead on the real
511 execvp("", (char **)argv
);
514 * Tell the parent we're ready to go
516 close(child_ready_pipe
[1]);
519 * Wait until the parent tells us to go.
521 if (read(go_pipe
[0], &buf
, 1) == -1)
522 perror("unable to read pipe");
524 execvp(argv
[0], (char **)argv
);
535 close(child_ready_pipe
[1]);
538 * wait for child to settle
540 if (read(child_ready_pipe
[0], &buf
, 1) == -1) {
541 perror("unable to read pipe");
544 close(child_ready_pipe
[0]);
548 if ((!system_wide
&& !inherit
) || profile_cpu
!= -1) {
549 open_counters(profile_cpu
, target_pid
);
551 nr_cpus
= read_cpu_map();
552 for (i
= 0; i
< nr_cpus
; i
++)
553 open_counters(cpumap
[i
], target_pid
);
557 err
= perf_header__write(&session
->header
, output
, false);
562 post_processing_offset
= lseek(output
, 0, SEEK_CUR
);
564 err
= event__synthesize_kernel_mmap(process_synthesized_event
,
567 pr_err("Couldn't record kernel reference relocation symbol.\n");
571 err
= event__synthesize_modules(process_synthesized_event
, session
);
573 pr_err("Couldn't record kernel reference relocation symbol.\n");
577 if (!system_wide
&& profile_cpu
== -1)
578 event__synthesize_thread(target_pid
, process_synthesized_event
,
581 event__synthesize_threads(process_synthesized_event
, session
);
584 struct sched_param param
;
586 param
.sched_priority
= realtime_prio
;
587 if (sched_setscheduler(0, SCHED_FIFO
, ¶m
)) {
588 pr_err("Could not set realtime priority.\n");
602 for (i
= 0; i
< nr_cpu
; i
++) {
603 for (counter
= 0; counter
< nr_counters
; counter
++) {
604 if (mmap_array
[i
][counter
].base
)
605 mmap_read(&mmap_array
[i
][counter
]);
609 if (hits
== samples
) {
612 err
= poll(event_array
, nr_poll
, -1);
617 for (i
= 0; i
< nr_cpu
; i
++) {
618 for (counter
= 0; counter
< nr_counters
; counter
++)
619 ioctl(fd
[i
][counter
], PERF_EVENT_IOC_DISABLE
);
624 fprintf(stderr
, "[ perf record: Woken up %ld times to write data ]\n", waking
);
627 * Approximate RIP event size: 24 bytes.
630 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
631 (double)bytes_written
/ 1024.0 / 1024.0,
638 static const char * const record_usage
[] = {
639 "perf record [<options>] [<command>]",
640 "perf record [<options>] -- <command> [<options>]",
644 static const struct option options
[] = {
645 OPT_CALLBACK('e', "event", NULL
, "event",
646 "event selector. use 'perf list' to list available events",
648 OPT_CALLBACK(0, "filter", NULL
, "filter",
649 "event filter", parse_filter
),
650 OPT_INTEGER('p', "pid", &target_pid
,
651 "record events on existing pid"),
652 OPT_INTEGER('r', "realtime", &realtime_prio
,
653 "collect data with this RT SCHED_FIFO priority"),
654 OPT_BOOLEAN('R', "raw-samples", &raw_samples
,
655 "collect raw sample records from all opened counters"),
656 OPT_BOOLEAN('a', "all-cpus", &system_wide
,
657 "system-wide collection from all CPUs"),
658 OPT_BOOLEAN('A', "append", &append_file
,
659 "append to the output file to do incremental profiling"),
660 OPT_INTEGER('C', "profile_cpu", &profile_cpu
,
661 "CPU to profile on"),
662 OPT_BOOLEAN('f', "force", &force
,
663 "overwrite existing data file"),
664 OPT_LONG('c', "count", &default_interval
,
665 "event period to sample"),
666 OPT_STRING('o', "output", &output_name
, "file",
668 OPT_BOOLEAN('i', "inherit", &inherit
,
669 "child tasks inherit counters"),
670 OPT_INTEGER('F', "freq", &freq
,
671 "profile at this frequency"),
672 OPT_INTEGER('m', "mmap-pages", &mmap_pages
,
673 "number of mmap data pages"),
674 OPT_BOOLEAN('g', "call-graph", &call_graph
,
675 "do call-graph (stack chain/backtrace) recording"),
676 OPT_BOOLEAN('v', "verbose", &verbose
,
677 "be more verbose (show counter open errors, etc)"),
678 OPT_BOOLEAN('s', "stat", &inherit_stat
,
679 "per thread counts"),
680 OPT_BOOLEAN('d', "data", &sample_address
,
682 OPT_BOOLEAN('n', "no-samples", &no_samples
,
684 OPT_BOOLEAN('M', "multiplex", &multiplex
,
685 "multiplex counter output in a single channel"),
689 int cmd_record(int argc
, const char **argv
, const char *prefix __used
)
693 argc
= parse_options(argc
, argv
, options
, record_usage
,
694 PARSE_OPT_STOP_AT_NON_OPTION
);
695 if (!argc
&& target_pid
== -1 && !system_wide
&& profile_cpu
== -1)
696 usage_with_options(record_usage
, options
);
702 attrs
[0].type
= PERF_TYPE_HARDWARE
;
703 attrs
[0].config
= PERF_COUNT_HW_CPU_CYCLES
;
707 * User specified count overrides default frequency.
709 if (default_interval
)
712 default_interval
= freq
;
714 fprintf(stderr
, "frequency and count are zero, aborting\n");
718 for (counter
= 0; counter
< nr_counters
; counter
++) {
719 if (attrs
[counter
].sample_period
)
722 attrs
[counter
].sample_period
= default_interval
;
725 return __cmd_record(argc
, argv
);