]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - tools/perf/builtin-record.c
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.
12 #include "util/util.h"
13 #include "util/parse-options.h"
14 #include "util/parse-events.h"
15 #include "util/string.h"
20 #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
21 #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
23 static int fd
[MAX_NR_CPUS
][MAX_COUNTERS
];
25 static long default_interval
= 100000;
27 static int nr_cpus
= 0;
28 static unsigned int page_size
;
29 static unsigned int mmap_pages
= 128;
32 static const char *output_name
= "perf.data";
34 static unsigned int realtime_prio
= 0;
35 static int system_wide
= 0;
36 static pid_t target_pid
= -1;
37 static int inherit
= 1;
39 static int append_file
= 0;
40 static int call_graph
= 0;
41 static int verbose
= 0;
44 static struct timeval last_read
;
45 static struct timeval this_read
;
47 static __u64 bytes_written
;
49 static struct pollfd event_array
[MAX_NR_CPUS
* MAX_COUNTERS
];
55 struct perf_event_header header
;
61 char filename
[PATH_MAX
];
65 struct perf_event_header header
;
79 static struct mmap_data mmap_array
[MAX_NR_CPUS
][MAX_COUNTERS
];
81 static unsigned int mmap_read_head(struct mmap_data
*md
)
83 struct perf_counter_mmap_page
*pc
= md
->base
;
92 static void mmap_read(struct mmap_data
*md
)
94 unsigned int head
= mmap_read_head(md
);
95 unsigned int old
= md
->prev
;
96 unsigned char *data
= md
->base
+ page_size
;
101 gettimeofday(&this_read
, NULL
);
104 * If we're further behind than half the buffer, there's a chance
105 * the writer will bite our tail and mess up the samples under us.
107 * If we somehow ended up ahead of the head, we got messed up.
109 * In either case, truncate and restart at head.
112 if (diff
> md
->mask
/ 2 || diff
< 0) {
116 timersub(&this_read
, &last_read
, &iv
);
117 msecs
= iv
.tv_sec
*1000 + iv
.tv_usec
/1000;
119 fprintf(stderr
, "WARNING: failed to keep up with mmap data."
120 " Last read %lu msecs ago.\n", msecs
);
123 * head points to a known good entry, start there.
128 last_read
= this_read
;
135 if ((old
& md
->mask
) + size
!= (head
& md
->mask
)) {
136 buf
= &data
[old
& md
->mask
];
137 size
= md
->mask
+ 1 - (old
& md
->mask
);
141 int ret
= write(output
, buf
, size
);
144 die("failed to write");
149 bytes_written
+= ret
;
153 buf
= &data
[old
& md
->mask
];
158 int ret
= write(output
, buf
, size
);
161 die("failed to write");
166 bytes_written
+= ret
;
172 static volatile int done
= 0;
173 static volatile int signr
= -1;
175 static void sig_handler(int sig
)
181 static void sig_atexit(void)
186 signal(signr
, SIG_DFL
);
187 kill(getpid(), signr
);
190 static void pid_synthesize_comm_event(pid_t pid
, int full
)
192 struct comm_event comm_ev
;
193 char filename
[PATH_MAX
];
199 struct dirent dirent
, *next
;
201 snprintf(filename
, sizeof(filename
), "/proc/%d/stat", pid
);
203 fd
= open(filename
, O_RDONLY
);
206 * We raced with a task exiting - just return:
209 fprintf(stderr
, "couldn't open %s\n", filename
);
212 if (read(fd
, bf
, sizeof(bf
)) < 0) {
213 fprintf(stderr
, "couldn't read %s\n", filename
);
218 /* 9027 (cat) R 6747 9027 6747 34816 9027 ... */
219 memset(&comm_ev
, 0, sizeof(comm_ev
));
220 field
= strchr(bf
, '(');
223 sep
= strchr(++field
, ')');
227 memcpy(comm_ev
.comm
, field
, size
++);
230 comm_ev
.header
.type
= PERF_EVENT_COMM
;
231 size
= ALIGN(size
, sizeof(__u64
));
232 comm_ev
.header
.size
= sizeof(comm_ev
) - (sizeof(comm_ev
.comm
) - size
);
237 ret
= write(output
, &comm_ev
, comm_ev
.header
.size
);
239 perror("failed to write");
245 snprintf(filename
, sizeof(filename
), "/proc/%d/task", pid
);
247 tasks
= opendir(filename
);
248 while (!readdir_r(tasks
, &dirent
, &next
) && next
) {
250 pid
= strtol(dirent
.d_name
, &end
, 10);
256 ret
= write(output
, &comm_ev
, comm_ev
.header
.size
);
258 perror("failed to write");
266 fprintf(stderr
, "couldn't get COMM and pgid, malformed %s\n",
271 static void pid_synthesize_mmap_samples(pid_t pid
)
273 char filename
[PATH_MAX
];
276 snprintf(filename
, sizeof(filename
), "/proc/%d/maps", pid
);
278 fp
= fopen(filename
, "r");
281 * We raced with a task exiting - just return:
284 fprintf(stderr
, "couldn't open %s\n", filename
);
288 char bf
[BUFSIZ
], *pbf
= bf
;
289 struct mmap_event mmap_ev
= {
290 .header
.type
= PERF_EVENT_MMAP
,
294 if (fgets(bf
, sizeof(bf
), fp
) == NULL
)
297 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
298 n
= hex2u64(pbf
, &mmap_ev
.start
);
302 n
= hex2u64(pbf
, &mmap_ev
.len
);
306 if (*pbf
== 'x') { /* vm_exec */
307 char *execname
= strrchr(bf
, ' ');
309 if (execname
== NULL
|| execname
[1] != '/')
313 size
= strlen(execname
);
314 execname
[size
- 1] = '\0'; /* Remove \n */
315 memcpy(mmap_ev
.filename
, execname
, size
);
316 size
= ALIGN(size
, sizeof(__u64
));
317 mmap_ev
.len
-= mmap_ev
.start
;
318 mmap_ev
.header
.size
= (sizeof(mmap_ev
) -
319 (sizeof(mmap_ev
.filename
) - size
));
323 if (write(output
, &mmap_ev
, mmap_ev
.header
.size
) < 0) {
324 perror("failed to write");
333 static void synthesize_samples(void)
336 struct dirent dirent
, *next
;
338 proc
= opendir("/proc");
340 while (!readdir_r(proc
, &dirent
, &next
) && next
) {
344 pid
= strtol(dirent
.d_name
, &end
, 10);
345 if (*end
) /* only interested in proper numerical dirents */
348 pid_synthesize_comm_event(pid
, 1);
349 pid_synthesize_mmap_samples(pid
);
357 static void create_counter(int counter
, int cpu
, pid_t pid
)
359 struct perf_counter_attr
*attr
= attrs
+ counter
;
362 attr
->sample_type
= PERF_SAMPLE_IP
| PERF_SAMPLE_TID
;
365 attr
->sample_type
|= PERF_SAMPLE_PERIOD
;
367 attr
->sample_freq
= freq
;
371 attr
->sample_type
|= PERF_SAMPLE_CALLCHAIN
;
375 attr
->inherit
= (cpu
< 0) && inherit
;
378 track
= 0; /* only the first counter needs these */
381 fd
[nr_cpu
][counter
] = sys_perf_counter_open(attr
, pid
, cpu
, group_fd
, 0);
383 if (fd
[nr_cpu
][counter
] < 0) {
387 die("Permission error - are you root?\n");
390 * If it's cycles then fall back to hrtimer
391 * based cpu-clock-tick sw counter, which
392 * is always available even if no PMU support:
394 if (attr
->type
== PERF_TYPE_HARDWARE
395 && attr
->config
== PERF_COUNT_HW_CPU_CYCLES
) {
398 warning(" ... trying to fall back to cpu-clock-ticks\n");
399 attr
->type
= PERF_TYPE_SOFTWARE
;
400 attr
->config
= PERF_COUNT_SW_CPU_CLOCK
;
404 error("perfcounter syscall returned with %d (%s)\n",
405 fd
[nr_cpu
][counter
], strerror(err
));
406 die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n");
410 assert(fd
[nr_cpu
][counter
] >= 0);
411 fcntl(fd
[nr_cpu
][counter
], F_SETFL
, O_NONBLOCK
);
414 * First counter acts as the group leader:
416 if (group
&& group_fd
== -1)
417 group_fd
= fd
[nr_cpu
][counter
];
419 event_array
[nr_poll
].fd
= fd
[nr_cpu
][counter
];
420 event_array
[nr_poll
].events
= POLLIN
;
423 mmap_array
[nr_cpu
][counter
].counter
= counter
;
424 mmap_array
[nr_cpu
][counter
].prev
= 0;
425 mmap_array
[nr_cpu
][counter
].mask
= mmap_pages
*page_size
- 1;
426 mmap_array
[nr_cpu
][counter
].base
= mmap(NULL
, (mmap_pages
+1)*page_size
,
427 PROT_READ
, MAP_SHARED
, fd
[nr_cpu
][counter
], 0);
428 if (mmap_array
[nr_cpu
][counter
].base
== MAP_FAILED
) {
429 error("failed to mmap with %d (%s)\n", errno
, strerror(errno
));
433 ioctl(fd
[nr_cpu
][counter
], PERF_COUNTER_IOC_ENABLE
);
436 static void open_counters(int cpu
, pid_t pid
)
441 pid_synthesize_comm_event(pid
, 0);
442 pid_synthesize_mmap_samples(pid
);
446 for (counter
= 0; counter
< nr_counters
; counter
++)
447 create_counter(counter
, cpu
, pid
);
452 static int __cmd_record(int argc
, const char **argv
)
460 page_size
= sysconf(_SC_PAGE_SIZE
);
461 nr_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
462 assert(nr_cpus
<= MAX_NR_CPUS
);
463 assert(nr_cpus
>= 0);
465 if (!stat(output_name
, &st
) && !force
&& !append_file
) {
466 fprintf(stderr
, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
471 flags
= O_CREAT
|O_RDWR
;
477 output
= open(output_name
, flags
, S_IRUSR
|S_IWUSR
);
479 perror("failed to create output file");
484 open_counters(-1, target_pid
!= -1 ? target_pid
: getpid());
485 } else for (i
= 0; i
< nr_cpus
; i
++)
486 open_counters(i
, target_pid
);
489 signal(SIGCHLD
, sig_handler
);
490 signal(SIGINT
, sig_handler
);
492 if (target_pid
== -1 && argc
) {
495 perror("failed to fork");
498 if (execvp(argv
[0], (char **)argv
)) {
506 struct sched_param param
;
508 param
.sched_priority
= realtime_prio
;
509 if (sched_setscheduler(0, SCHED_FIFO
, ¶m
)) {
510 printf("Could not set realtime priority.\n");
516 synthesize_samples();
521 for (i
= 0; i
< nr_cpu
; i
++) {
522 for (counter
= 0; counter
< nr_counters
; counter
++)
523 mmap_read(&mmap_array
[i
][counter
]);
527 ret
= poll(event_array
, nr_poll
, 100);
531 * Approximate RIP event size: 24 bytes.
534 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
535 (double)bytes_written
/ 1024.0 / 1024.0,
542 static const char * const record_usage
[] = {
543 "perf record [<options>] [<command>]",
544 "perf record [<options>] -- <command> [<options>]",
548 static const struct option options
[] = {
549 OPT_CALLBACK('e', "event", NULL
, "event",
550 "event selector. use 'perf list' to list available events",
552 OPT_INTEGER('p', "pid", &target_pid
,
553 "record events on existing pid"),
554 OPT_INTEGER('r', "realtime", &realtime_prio
,
555 "collect data with this RT SCHED_FIFO priority"),
556 OPT_BOOLEAN('a', "all-cpus", &system_wide
,
557 "system-wide collection from all CPUs"),
558 OPT_BOOLEAN('A', "append", &append_file
,
559 "append to the output file to do incremental profiling"),
560 OPT_BOOLEAN('f', "force", &force
,
561 "overwrite existing data file"),
562 OPT_LONG('c', "count", &default_interval
,
563 "event period to sample"),
564 OPT_STRING('o', "output", &output_name
, "file",
566 OPT_BOOLEAN('i', "inherit", &inherit
,
567 "child tasks inherit counters"),
568 OPT_INTEGER('F', "freq", &freq
,
569 "profile at this frequency"),
570 OPT_INTEGER('m', "mmap-pages", &mmap_pages
,
571 "number of mmap data pages"),
572 OPT_BOOLEAN('g', "call-graph", &call_graph
,
573 "do call-graph (stack chain/backtrace) recording"),
574 OPT_BOOLEAN('v', "verbose", &verbose
,
575 "be more verbose (show counter open errors, etc)"),
579 int cmd_record(int argc
, const char **argv
, const char *prefix
)
583 argc
= parse_options(argc
, argv
, options
, record_usage
, 0);
584 if (!argc
&& target_pid
== -1 && !system_wide
)
585 usage_with_options(record_usage
, options
);
589 attrs
[0].type
= PERF_TYPE_HARDWARE
;
590 attrs
[0].config
= PERF_COUNT_HW_CPU_CYCLES
;
593 for (counter
= 0; counter
< nr_counters
; counter
++) {
594 if (attrs
[counter
].sample_period
)
597 attrs
[counter
].sample_period
= default_interval
;
600 return __cmd_record(argc
, argv
);