perf-report: Add modes for inherited stats and no-samples
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / tools / perf / builtin-record.c
blob798a56d890e5ecb59ebd401cf5a7fad8baa77b0d
1 /*
2 * 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.
7 */
8 #include "builtin.h"
10 #include "perf.h"
12 #include "util/util.h"
13 #include "util/parse-options.h"
14 #include "util/parse-events.h"
15 #include "util/string.h"
17 #include "util/header.h"
19 #include <unistd.h>
20 #include <sched.h>
22 #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
23 #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
25 static int fd[MAX_NR_CPUS][MAX_COUNTERS];
27 static long default_interval = 100000;
29 static int nr_cpus = 0;
30 static unsigned int page_size;
31 static unsigned int mmap_pages = 128;
32 static int freq = 0;
33 static int output;
34 static const char *output_name = "perf.data";
35 static int group = 0;
36 static unsigned int realtime_prio = 0;
37 static int system_wide = 0;
38 static pid_t target_pid = -1;
39 static int inherit = 1;
40 static int force = 0;
41 static int append_file = 0;
42 static int call_graph = 0;
43 static int verbose = 0;
44 static int inherit_stat = 0;
45 static int no_samples = 0;
47 static long samples;
48 static struct timeval last_read;
49 static struct timeval this_read;
51 static u64 bytes_written;
53 static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
55 static int nr_poll;
56 static int nr_cpu;
58 static int file_new = 1;
60 struct perf_header *header;
62 struct mmap_event {
63 struct perf_event_header header;
64 u32 pid;
65 u32 tid;
66 u64 start;
67 u64 len;
68 u64 pgoff;
69 char filename[PATH_MAX];
72 struct comm_event {
73 struct perf_event_header header;
74 u32 pid;
75 u32 tid;
76 char comm[16];
80 struct mmap_data {
81 int counter;
82 void *base;
83 unsigned int mask;
84 unsigned int prev;
87 static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
89 static unsigned long mmap_read_head(struct mmap_data *md)
91 struct perf_counter_mmap_page *pc = md->base;
92 long head;
94 head = pc->data_head;
95 rmb();
97 return head;
100 static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
102 struct perf_counter_mmap_page *pc = md->base;
105 * ensure all reads are done before we write the tail out.
107 /* mb(); */
108 pc->data_tail = tail;
111 static void write_output(void *buf, size_t size)
113 while (size) {
114 int ret = write(output, buf, size);
116 if (ret < 0)
117 die("failed to write");
119 size -= ret;
120 buf += ret;
122 bytes_written += ret;
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;
131 unsigned long size;
132 void *buf;
133 int diff;
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.
145 diff = head - old;
146 if (diff < 0) {
147 struct timeval iv;
148 unsigned long msecs;
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.
159 old = head;
162 last_read = this_read;
164 if (old != head)
165 samples++;
167 size = head - old;
169 if ((old & md->mask) + size != (head & md->mask)) {
170 buf = &data[old & md->mask];
171 size = md->mask + 1 - (old & md->mask);
172 old += size;
174 write_output(buf, size);
177 buf = &data[old & md->mask];
178 size = head - old;
179 old += size;
181 write_output(buf, size);
183 md->prev = old;
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)
192 done = 1;
193 signr = sig;
196 static void sig_atexit(void)
198 if (signr == -1)
199 return;
201 signal(signr, SIG_DFL);
202 kill(getpid(), signr);
205 static void pid_synthesize_comm_event(pid_t pid, int full)
207 struct comm_event comm_ev;
208 char filename[PATH_MAX];
209 char bf[BUFSIZ];
210 int fd;
211 size_t size;
212 char *field, *sep;
213 DIR *tasks;
214 struct dirent dirent, *next;
216 snprintf(filename, sizeof(filename), "/proc/%d/stat", pid);
218 fd = open(filename, O_RDONLY);
219 if (fd < 0) {
221 * We raced with a task exiting - just return:
223 if (verbose)
224 fprintf(stderr, "couldn't open %s\n", filename);
225 return;
227 if (read(fd, bf, sizeof(bf)) < 0) {
228 fprintf(stderr, "couldn't read %s\n", filename);
229 exit(EXIT_FAILURE);
231 close(fd);
233 /* 9027 (cat) R 6747 9027 6747 34816 9027 ... */
234 memset(&comm_ev, 0, sizeof(comm_ev));
235 field = strchr(bf, '(');
236 if (field == NULL)
237 goto out_failure;
238 sep = strchr(++field, ')');
239 if (sep == NULL)
240 goto out_failure;
241 size = sep - field;
242 memcpy(comm_ev.comm, field, size++);
244 comm_ev.pid = pid;
245 comm_ev.header.type = PERF_EVENT_COMM;
246 size = ALIGN(size, sizeof(u64));
247 comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
249 if (!full) {
250 comm_ev.tid = pid;
252 write_output(&comm_ev, comm_ev.header.size);
253 return;
256 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
258 tasks = opendir(filename);
259 while (!readdir_r(tasks, &dirent, &next) && next) {
260 char *end;
261 pid = strtol(dirent.d_name, &end, 10);
262 if (*end)
263 continue;
265 comm_ev.tid = pid;
267 write_output(&comm_ev, comm_ev.header.size);
269 closedir(tasks);
270 return;
272 out_failure:
273 fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
274 filename);
275 exit(EXIT_FAILURE);
278 static void pid_synthesize_mmap_samples(pid_t pid)
280 char filename[PATH_MAX];
281 FILE *fp;
283 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
285 fp = fopen(filename, "r");
286 if (fp == NULL) {
288 * We raced with a task exiting - just return:
290 if (verbose)
291 fprintf(stderr, "couldn't open %s\n", filename);
292 return;
294 while (1) {
295 char bf[BUFSIZ], *pbf = bf;
296 struct mmap_event mmap_ev = {
297 .header.type = PERF_EVENT_MMAP,
299 int n;
300 size_t size;
301 if (fgets(bf, sizeof(bf), fp) == NULL)
302 break;
304 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
305 n = hex2u64(pbf, &mmap_ev.start);
306 if (n < 0)
307 continue;
308 pbf += n + 1;
309 n = hex2u64(pbf, &mmap_ev.len);
310 if (n < 0)
311 continue;
312 pbf += n + 3;
313 if (*pbf == 'x') { /* vm_exec */
314 char *execname = strchr(bf, '/');
316 if (execname == NULL)
317 continue;
319 size = strlen(execname);
320 execname[size - 1] = '\0'; /* Remove \n */
321 memcpy(mmap_ev.filename, execname, size);
322 size = ALIGN(size, sizeof(u64));
323 mmap_ev.len -= mmap_ev.start;
324 mmap_ev.header.size = (sizeof(mmap_ev) -
325 (sizeof(mmap_ev.filename) - size));
326 mmap_ev.pid = pid;
327 mmap_ev.tid = pid;
329 write_output(&mmap_ev, mmap_ev.header.size);
333 fclose(fp);
336 static void synthesize_all(void)
338 DIR *proc;
339 struct dirent dirent, *next;
341 proc = opendir("/proc");
343 while (!readdir_r(proc, &dirent, &next) && next) {
344 char *end;
345 pid_t pid;
347 pid = strtol(dirent.d_name, &end, 10);
348 if (*end) /* only interested in proper numerical dirents */
349 continue;
351 pid_synthesize_comm_event(pid, 1);
352 pid_synthesize_mmap_samples(pid);
355 closedir(proc);
358 static int group_fd;
360 static struct perf_header_attr *get_header_attr(struct perf_counter_attr *a, int nr)
362 struct perf_header_attr *h_attr;
364 if (nr < header->attrs) {
365 h_attr = header->attr[nr];
366 } else {
367 h_attr = perf_header_attr__new(a);
368 perf_header__add_attr(header, h_attr);
371 return h_attr;
374 static void create_counter(int counter, int cpu, pid_t pid)
376 struct perf_counter_attr *attr = attrs + counter;
377 struct perf_header_attr *h_attr;
378 int track = !counter; /* only the first counter needs these */
379 struct {
380 u64 count;
381 u64 time_enabled;
382 u64 time_running;
383 u64 id;
384 } read_data;
386 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
387 PERF_FORMAT_TOTAL_TIME_RUNNING |
388 PERF_FORMAT_ID;
390 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
392 if (freq) {
393 attr->sample_type |= PERF_SAMPLE_PERIOD;
394 attr->freq = 1;
395 attr->sample_freq = freq;
398 if (no_samples)
399 attr->sample_freq = 0;
401 if (inherit_stat)
402 attr->inherit_stat = 1;
404 if (call_graph)
405 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
407 attr->mmap = track;
408 attr->comm = track;
409 attr->inherit = (cpu < 0) && inherit;
410 attr->disabled = 1;
412 try_again:
413 fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0);
415 if (fd[nr_cpu][counter] < 0) {
416 int err = errno;
418 if (err == EPERM)
419 die("Permission error - are you root?\n");
422 * If it's cycles then fall back to hrtimer
423 * based cpu-clock-tick sw counter, which
424 * is always available even if no PMU support:
426 if (attr->type == PERF_TYPE_HARDWARE
427 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
429 if (verbose)
430 warning(" ... trying to fall back to cpu-clock-ticks\n");
431 attr->type = PERF_TYPE_SOFTWARE;
432 attr->config = PERF_COUNT_SW_CPU_CLOCK;
433 goto try_again;
435 printf("\n");
436 error("perfcounter syscall returned with %d (%s)\n",
437 fd[nr_cpu][counter], strerror(err));
438 die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n");
439 exit(-1);
442 h_attr = get_header_attr(attr, counter);
444 if (!file_new) {
445 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
446 fprintf(stderr, "incompatible append\n");
447 exit(-1);
451 read(fd[nr_cpu][counter], &read_data, sizeof(read_data));
453 perf_header_attr__add_id(h_attr, read_data.id);
455 assert(fd[nr_cpu][counter] >= 0);
456 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
459 * First counter acts as the group leader:
461 if (group && group_fd == -1)
462 group_fd = fd[nr_cpu][counter];
464 event_array[nr_poll].fd = fd[nr_cpu][counter];
465 event_array[nr_poll].events = POLLIN;
466 nr_poll++;
468 mmap_array[nr_cpu][counter].counter = counter;
469 mmap_array[nr_cpu][counter].prev = 0;
470 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
471 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
472 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
473 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
474 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
475 exit(-1);
478 ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_ENABLE);
481 static void open_counters(int cpu, pid_t pid)
483 int counter;
485 group_fd = -1;
486 for (counter = 0; counter < nr_counters; counter++)
487 create_counter(counter, cpu, pid);
489 nr_cpu++;
492 static void atexit_header(void)
494 header->data_size += bytes_written;
496 perf_header__write(header, output);
499 static int __cmd_record(int argc, const char **argv)
501 int i, counter;
502 struct stat st;
503 pid_t pid = 0;
504 int flags;
505 int ret;
507 page_size = sysconf(_SC_PAGE_SIZE);
508 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
509 assert(nr_cpus <= MAX_NR_CPUS);
510 assert(nr_cpus >= 0);
512 atexit(sig_atexit);
513 signal(SIGCHLD, sig_handler);
514 signal(SIGINT, sig_handler);
516 if (!stat(output_name, &st) && !force && !append_file) {
517 fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
518 output_name);
519 exit(-1);
522 flags = O_CREAT|O_RDWR;
523 if (append_file)
524 file_new = 0;
525 else
526 flags |= O_TRUNC;
528 output = open(output_name, flags, S_IRUSR|S_IWUSR);
529 if (output < 0) {
530 perror("failed to create output file");
531 exit(-1);
534 if (!file_new)
535 header = perf_header__read(output);
536 else
537 header = perf_header__new();
539 atexit(atexit_header);
541 if (!system_wide) {
542 pid = target_pid;
543 if (pid == -1)
544 pid = getpid();
546 open_counters(-1, pid);
547 } else for (i = 0; i < nr_cpus; i++)
548 open_counters(i, target_pid);
550 if (file_new)
551 perf_header__write(header, output);
553 if (!system_wide) {
554 pid_synthesize_comm_event(pid, 0);
555 pid_synthesize_mmap_samples(pid);
556 } else
557 synthesize_all();
559 if (target_pid == -1 && argc) {
560 pid = fork();
561 if (pid < 0)
562 perror("failed to fork");
564 if (!pid) {
565 if (execvp(argv[0], (char **)argv)) {
566 perror(argv[0]);
567 exit(-1);
572 if (realtime_prio) {
573 struct sched_param param;
575 param.sched_priority = realtime_prio;
576 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
577 printf("Could not set realtime priority.\n");
578 exit(-1);
582 for (;;) {
583 int hits = samples;
585 for (i = 0; i < nr_cpu; i++) {
586 for (counter = 0; counter < nr_counters; counter++)
587 mmap_read(&mmap_array[i][counter]);
590 if (hits == samples) {
591 if (done)
592 break;
593 ret = poll(event_array, nr_poll, 100);
598 * Approximate RIP event size: 24 bytes.
600 fprintf(stderr,
601 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
602 (double)bytes_written / 1024.0 / 1024.0,
603 output_name,
604 bytes_written / 24);
606 return 0;
609 static const char * const record_usage[] = {
610 "perf record [<options>] [<command>]",
611 "perf record [<options>] -- <command> [<options>]",
612 NULL
615 static const struct option options[] = {
616 OPT_CALLBACK('e', "event", NULL, "event",
617 "event selector. use 'perf list' to list available events",
618 parse_events),
619 OPT_INTEGER('p', "pid", &target_pid,
620 "record events on existing pid"),
621 OPT_INTEGER('r', "realtime", &realtime_prio,
622 "collect data with this RT SCHED_FIFO priority"),
623 OPT_BOOLEAN('a', "all-cpus", &system_wide,
624 "system-wide collection from all CPUs"),
625 OPT_BOOLEAN('A', "append", &append_file,
626 "append to the output file to do incremental profiling"),
627 OPT_BOOLEAN('f', "force", &force,
628 "overwrite existing data file"),
629 OPT_LONG('c', "count", &default_interval,
630 "event period to sample"),
631 OPT_STRING('o', "output", &output_name, "file",
632 "output file name"),
633 OPT_BOOLEAN('i', "inherit", &inherit,
634 "child tasks inherit counters"),
635 OPT_INTEGER('F', "freq", &freq,
636 "profile at this frequency"),
637 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
638 "number of mmap data pages"),
639 OPT_BOOLEAN('g', "call-graph", &call_graph,
640 "do call-graph (stack chain/backtrace) recording"),
641 OPT_BOOLEAN('v', "verbose", &verbose,
642 "be more verbose (show counter open errors, etc)"),
643 OPT_BOOLEAN('s', "stat", &inherit_stat,
644 "per thread counts"),
645 OPT_BOOLEAN('n', "no-samples", &no_samples,
646 "don't sample"),
647 OPT_END()
650 int cmd_record(int argc, const char **argv, const char *prefix)
652 int counter;
654 argc = parse_options(argc, argv, options, record_usage, 0);
655 if (!argc && target_pid == -1 && !system_wide)
656 usage_with_options(record_usage, options);
658 if (!nr_counters) {
659 nr_counters = 1;
660 attrs[0].type = PERF_TYPE_HARDWARE;
661 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
664 for (counter = 0; counter < nr_counters; counter++) {
665 if (attrs[counter].sample_period)
666 continue;
668 attrs[counter].sample_period = default_interval;
671 return __cmd_record(argc, argv);