perf trace: Sample the CPU too
[linux-2.6.git] / tools / perf / builtin-record.c
1 /*
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.
7  */
8 #include "builtin.h"
9
10 #include "perf.h"
11
12 #include "util/util.h"
13 #include "util/parse-options.h"
14 #include "util/parse-events.h"
15 #include "util/string.h"
16
17 #include "util/header.h"
18 #include "util/event.h"
19 #include "util/debug.h"
20 #include "util/trace-event.h"
21
22 #include <unistd.h>
23 #include <sched.h>
24
25 #define ALIGN(x, a)             __ALIGN_MASK(x, (typeof(x))(a)-1)
26 #define __ALIGN_MASK(x, mask)   (((x)+(mask))&~(mask))
27
28 static int                      fd[MAX_NR_CPUS][MAX_COUNTERS];
29
30 static long                     default_interval                = 100000;
31
32 static int                      nr_cpus                         = 0;
33 static unsigned int             page_size;
34 static unsigned int             mmap_pages                      = 128;
35 static int                      freq                            = 0;
36 static int                      output;
37 static const char               *output_name                    = "perf.data";
38 static int                      group                           = 0;
39 static unsigned int             realtime_prio                   = 0;
40 static int                      raw_samples                     = 0;
41 static int                      system_wide                     = 0;
42 static int                      profile_cpu                     = -1;
43 static pid_t                    target_pid                      = -1;
44 static int                      inherit                         = 1;
45 static int                      force                           = 0;
46 static int                      append_file                     = 0;
47 static int                      call_graph                      = 0;
48 static int                      inherit_stat                    = 0;
49 static int                      no_samples                      = 0;
50 static int                      sample_address                  = 0;
51
52 static long                     samples;
53 static struct timeval           last_read;
54 static struct timeval           this_read;
55
56 static u64                      bytes_written;
57
58 static struct pollfd            event_array[MAX_NR_CPUS * MAX_COUNTERS];
59
60 static int                      nr_poll;
61 static int                      nr_cpu;
62
63 static int                      file_new = 1;
64
65 struct perf_header              *header;
66
67 struct mmap_data {
68         int                     counter;
69         void                    *base;
70         unsigned int            mask;
71         unsigned int            prev;
72 };
73
74 static struct mmap_data         mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
75
76 static unsigned long mmap_read_head(struct mmap_data *md)
77 {
78         struct perf_counter_mmap_page *pc = md->base;
79         long head;
80
81         head = pc->data_head;
82         rmb();
83
84         return head;
85 }
86
87 static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
88 {
89         struct perf_counter_mmap_page *pc = md->base;
90
91         /*
92          * ensure all reads are done before we write the tail out.
93          */
94         /* mb(); */
95         pc->data_tail = tail;
96 }
97
98 static void write_output(void *buf, size_t size)
99 {
100         while (size) {
101                 int ret = write(output, buf, size);
102
103                 if (ret < 0)
104                         die("failed to write");
105
106                 size -= ret;
107                 buf += ret;
108
109                 bytes_written += ret;
110         }
111 }
112
113 static void mmap_read(struct mmap_data *md)
114 {
115         unsigned int head = mmap_read_head(md);
116         unsigned int old = md->prev;
117         unsigned char *data = md->base + page_size;
118         unsigned long size;
119         void *buf;
120         int diff;
121
122         gettimeofday(&this_read, NULL);
123
124         /*
125          * If we're further behind than half the buffer, there's a chance
126          * the writer will bite our tail and mess up the samples under us.
127          *
128          * If we somehow ended up ahead of the head, we got messed up.
129          *
130          * In either case, truncate and restart at head.
131          */
132         diff = head - old;
133         if (diff < 0) {
134                 struct timeval iv;
135                 unsigned long msecs;
136
137                 timersub(&this_read, &last_read, &iv);
138                 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
139
140                 fprintf(stderr, "WARNING: failed to keep up with mmap data."
141                                 "  Last read %lu msecs ago.\n", msecs);
142
143                 /*
144                  * head points to a known good entry, start there.
145                  */
146                 old = head;
147         }
148
149         last_read = this_read;
150
151         if (old != head)
152                 samples++;
153
154         size = head - old;
155
156         if ((old & md->mask) + size != (head & md->mask)) {
157                 buf = &data[old & md->mask];
158                 size = md->mask + 1 - (old & md->mask);
159                 old += size;
160
161                 write_output(buf, size);
162         }
163
164         buf = &data[old & md->mask];
165         size = head - old;
166         old += size;
167
168         write_output(buf, size);
169
170         md->prev = old;
171         mmap_write_tail(md, old);
172 }
173
174 static volatile int done = 0;
175 static volatile int signr = -1;
176
177 static void sig_handler(int sig)
178 {
179         done = 1;
180         signr = sig;
181 }
182
183 static void sig_atexit(void)
184 {
185         if (signr == -1)
186                 return;
187
188         signal(signr, SIG_DFL);
189         kill(getpid(), signr);
190 }
191
192 static pid_t pid_synthesize_comm_event(pid_t pid, int full)
193 {
194         struct comm_event comm_ev;
195         char filename[PATH_MAX];
196         char bf[BUFSIZ];
197         FILE *fp;
198         size_t size = 0;
199         DIR *tasks;
200         struct dirent dirent, *next;
201         pid_t tgid = 0;
202
203         snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
204
205         fp = fopen(filename, "r");
206         if (fp == NULL) {
207                 /*
208                  * We raced with a task exiting - just return:
209                  */
210                 if (verbose)
211                         fprintf(stderr, "couldn't open %s\n", filename);
212                 return 0;
213         }
214
215         memset(&comm_ev, 0, sizeof(comm_ev));
216         while (!comm_ev.comm[0] || !comm_ev.pid) {
217                 if (fgets(bf, sizeof(bf), fp) == NULL)
218                         goto out_failure;
219
220                 if (memcmp(bf, "Name:", 5) == 0) {
221                         char *name = bf + 5;
222                         while (*name && isspace(*name))
223                                 ++name;
224                         size = strlen(name) - 1;
225                         memcpy(comm_ev.comm, name, size++);
226                 } else if (memcmp(bf, "Tgid:", 5) == 0) {
227                         char *tgids = bf + 5;
228                         while (*tgids && isspace(*tgids))
229                                 ++tgids;
230                         tgid = comm_ev.pid = atoi(tgids);
231                 }
232         }
233
234         comm_ev.header.type = PERF_EVENT_COMM;
235         size = ALIGN(size, sizeof(u64));
236         comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
237
238         if (!full) {
239                 comm_ev.tid = pid;
240
241                 write_output(&comm_ev, comm_ev.header.size);
242                 goto out_fclose;
243         }
244
245         snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
246
247         tasks = opendir(filename);
248         while (!readdir_r(tasks, &dirent, &next) && next) {
249                 char *end;
250                 pid = strtol(dirent.d_name, &end, 10);
251                 if (*end)
252                         continue;
253
254                 comm_ev.tid = pid;
255
256                 write_output(&comm_ev, comm_ev.header.size);
257         }
258         closedir(tasks);
259
260 out_fclose:
261         fclose(fp);
262         return tgid;
263
264 out_failure:
265         fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
266                 filename);
267         exit(EXIT_FAILURE);
268 }
269
270 static void pid_synthesize_mmap_samples(pid_t pid, pid_t tgid)
271 {
272         char filename[PATH_MAX];
273         FILE *fp;
274
275         snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
276
277         fp = fopen(filename, "r");
278         if (fp == NULL) {
279                 /*
280                  * We raced with a task exiting - just return:
281                  */
282                 if (verbose)
283                         fprintf(stderr, "couldn't open %s\n", filename);
284                 return;
285         }
286         while (1) {
287                 char bf[BUFSIZ], *pbf = bf;
288                 struct mmap_event mmap_ev = {
289                         .header = { .type = PERF_EVENT_MMAP },
290                 };
291                 int n;
292                 size_t size;
293                 if (fgets(bf, sizeof(bf), fp) == NULL)
294                         break;
295
296                 /* 00400000-0040c000 r-xp 00000000 fd:01 41038  /bin/cat */
297                 n = hex2u64(pbf, &mmap_ev.start);
298                 if (n < 0)
299                         continue;
300                 pbf += n + 1;
301                 n = hex2u64(pbf, &mmap_ev.len);
302                 if (n < 0)
303                         continue;
304                 pbf += n + 3;
305                 if (*pbf == 'x') { /* vm_exec */
306                         char *execname = strchr(bf, '/');
307
308                         /* Catch VDSO */
309                         if (execname == NULL)
310                                 execname = strstr(bf, "[vdso]");
311
312                         if (execname == NULL)
313                                 continue;
314
315                         size = strlen(execname);
316                         execname[size - 1] = '\0'; /* Remove \n */
317                         memcpy(mmap_ev.filename, execname, size);
318                         size = ALIGN(size, sizeof(u64));
319                         mmap_ev.len -= mmap_ev.start;
320                         mmap_ev.header.size = (sizeof(mmap_ev) -
321                                                (sizeof(mmap_ev.filename) - size));
322                         mmap_ev.pid = tgid;
323                         mmap_ev.tid = pid;
324
325                         write_output(&mmap_ev, mmap_ev.header.size);
326                 }
327         }
328
329         fclose(fp);
330 }
331
332 static void synthesize_all(void)
333 {
334         DIR *proc;
335         struct dirent dirent, *next;
336
337         proc = opendir("/proc");
338
339         while (!readdir_r(proc, &dirent, &next) && next) {
340                 char *end;
341                 pid_t pid, tgid;
342
343                 pid = strtol(dirent.d_name, &end, 10);
344                 if (*end) /* only interested in proper numerical dirents */
345                         continue;
346
347                 tgid = pid_synthesize_comm_event(pid, 1);
348                 pid_synthesize_mmap_samples(pid, tgid);
349         }
350
351         closedir(proc);
352 }
353
354 static int group_fd;
355
356 static struct perf_header_attr *get_header_attr(struct perf_counter_attr *a, int nr)
357 {
358         struct perf_header_attr *h_attr;
359
360         if (nr < header->attrs) {
361                 h_attr = header->attr[nr];
362         } else {
363                 h_attr = perf_header_attr__new(a);
364                 perf_header__add_attr(header, h_attr);
365         }
366
367         return h_attr;
368 }
369
370 static void create_counter(int counter, int cpu, pid_t pid)
371 {
372         struct perf_counter_attr *attr = attrs + counter;
373         struct perf_header_attr *h_attr;
374         int track = !counter; /* only the first counter needs these */
375         struct {
376                 u64 count;
377                 u64 time_enabled;
378                 u64 time_running;
379                 u64 id;
380         } read_data;
381
382         attr->read_format       = PERF_FORMAT_TOTAL_TIME_ENABLED |
383                                   PERF_FORMAT_TOTAL_TIME_RUNNING |
384                                   PERF_FORMAT_ID;
385
386         attr->sample_type       |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
387
388         if (freq) {
389                 attr->sample_type       |= PERF_SAMPLE_PERIOD;
390                 attr->freq              = 1;
391                 attr->sample_freq       = freq;
392         }
393
394         if (no_samples)
395                 attr->sample_freq = 0;
396
397         if (inherit_stat)
398                 attr->inherit_stat = 1;
399
400         if (sample_address)
401                 attr->sample_type       |= PERF_SAMPLE_ADDR;
402
403         if (call_graph)
404                 attr->sample_type       |= PERF_SAMPLE_CALLCHAIN;
405
406         if (raw_samples) {
407                 attr->sample_type       |= PERF_SAMPLE_RAW;
408                 attr->sample_type       |= PERF_SAMPLE_CPU;
409         }
410
411         attr->mmap              = track;
412         attr->comm              = track;
413         attr->inherit           = (cpu < 0) && inherit;
414         attr->disabled          = 1;
415
416 try_again:
417         fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0);
418
419         if (fd[nr_cpu][counter] < 0) {
420                 int err = errno;
421
422                 if (err == EPERM)
423                         die("Permission error - are you root?\n");
424                 else if (err ==  ENODEV && profile_cpu != -1)
425                         die("No such device - did you specify an out-of-range profile CPU?\n");
426
427                 /*
428                  * If it's cycles then fall back to hrtimer
429                  * based cpu-clock-tick sw counter, which
430                  * is always available even if no PMU support:
431                  */
432                 if (attr->type == PERF_TYPE_HARDWARE
433                         && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
434
435                         if (verbose)
436                                 warning(" ... trying to fall back to cpu-clock-ticks\n");
437                         attr->type = PERF_TYPE_SOFTWARE;
438                         attr->config = PERF_COUNT_SW_CPU_CLOCK;
439                         goto try_again;
440                 }
441                 printf("\n");
442                 error("perfcounter syscall returned with %d (%s)\n",
443                         fd[nr_cpu][counter], strerror(err));
444                 die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n");
445                 exit(-1);
446         }
447
448         h_attr = get_header_attr(attr, counter);
449
450         if (!file_new) {
451                 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
452                         fprintf(stderr, "incompatible append\n");
453                         exit(-1);
454                 }
455         }
456
457         if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
458                 perror("Unable to read perf file descriptor\n");
459                 exit(-1);
460         }
461
462         perf_header_attr__add_id(h_attr, read_data.id);
463
464         assert(fd[nr_cpu][counter] >= 0);
465         fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
466
467         /*
468          * First counter acts as the group leader:
469          */
470         if (group && group_fd == -1)
471                 group_fd = fd[nr_cpu][counter];
472
473         event_array[nr_poll].fd = fd[nr_cpu][counter];
474         event_array[nr_poll].events = POLLIN;
475         nr_poll++;
476
477         mmap_array[nr_cpu][counter].counter = counter;
478         mmap_array[nr_cpu][counter].prev = 0;
479         mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
480         mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
481                         PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
482         if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
483                 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
484                 exit(-1);
485         }
486
487         ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_ENABLE);
488 }
489
490 static void open_counters(int cpu, pid_t pid)
491 {
492         int counter;
493
494         group_fd = -1;
495         for (counter = 0; counter < nr_counters; counter++)
496                 create_counter(counter, cpu, pid);
497
498         nr_cpu++;
499 }
500
501 static void atexit_header(void)
502 {
503         header->data_size += bytes_written;
504
505         perf_header__write(header, output);
506 }
507
508 static int __cmd_record(int argc, const char **argv)
509 {
510         int i, counter;
511         struct stat st;
512         pid_t pid = 0;
513         int flags;
514         int ret;
515
516         page_size = sysconf(_SC_PAGE_SIZE);
517         nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
518         assert(nr_cpus <= MAX_NR_CPUS);
519         assert(nr_cpus >= 0);
520
521         atexit(sig_atexit);
522         signal(SIGCHLD, sig_handler);
523         signal(SIGINT, sig_handler);
524
525         if (!stat(output_name, &st) && st.st_size) {
526                 if (!force && !append_file) {
527                         fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
528                                         output_name);
529                         exit(-1);
530                 }
531         } else {
532                 append_file = 0;
533         }
534
535         flags = O_CREAT|O_RDWR;
536         if (append_file)
537                 file_new = 0;
538         else
539                 flags |= O_TRUNC;
540
541         output = open(output_name, flags, S_IRUSR|S_IWUSR);
542         if (output < 0) {
543                 perror("failed to create output file");
544                 exit(-1);
545         }
546
547         if (!file_new)
548                 header = perf_header__read(output);
549         else
550                 header = perf_header__new();
551
552
553         if (raw_samples) {
554                 read_tracing_data(attrs, nr_counters);
555         } else {
556                 for (i = 0; i < nr_counters; i++) {
557                         if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
558                                 read_tracing_data(attrs, nr_counters);
559                                 break;
560                         }
561                 }
562         }
563         atexit(atexit_header);
564
565         if (!system_wide) {
566                 pid = target_pid;
567                 if (pid == -1)
568                         pid = getpid();
569
570                 open_counters(profile_cpu, pid);
571         } else {
572                 if (profile_cpu != -1) {
573                         open_counters(profile_cpu, target_pid);
574                 } else {
575                         for (i = 0; i < nr_cpus; i++)
576                                 open_counters(i, target_pid);
577                 }
578         }
579
580         if (file_new)
581                 perf_header__write(header, output);
582
583         if (!system_wide) {
584                 pid_t tgid = pid_synthesize_comm_event(pid, 0);
585                 pid_synthesize_mmap_samples(pid, tgid);
586         } else
587                 synthesize_all();
588
589         if (target_pid == -1 && argc) {
590                 pid = fork();
591                 if (pid < 0)
592                         perror("failed to fork");
593
594                 if (!pid) {
595                         if (execvp(argv[0], (char **)argv)) {
596                                 perror(argv[0]);
597                                 exit(-1);
598                         }
599                 }
600         }
601
602         if (realtime_prio) {
603                 struct sched_param param;
604
605                 param.sched_priority = realtime_prio;
606                 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
607                         printf("Could not set realtime priority.\n");
608                         exit(-1);
609                 }
610         }
611
612         for (;;) {
613                 int hits = samples;
614
615                 for (i = 0; i < nr_cpu; i++) {
616                         for (counter = 0; counter < nr_counters; counter++)
617                                 mmap_read(&mmap_array[i][counter]);
618                 }
619
620                 if (hits == samples) {
621                         if (done)
622                                 break;
623                         ret = poll(event_array, nr_poll, 100);
624                 }
625         }
626
627         /*
628          * Approximate RIP event size: 24 bytes.
629          */
630         fprintf(stderr,
631                 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
632                 (double)bytes_written / 1024.0 / 1024.0,
633                 output_name,
634                 bytes_written / 24);
635
636         return 0;
637 }
638
639 static const char * const record_usage[] = {
640         "perf record [<options>] [<command>]",
641         "perf record [<options>] -- <command> [<options>]",
642         NULL
643 };
644
645 static const struct option options[] = {
646         OPT_CALLBACK('e', "event", NULL, "event",
647                      "event selector. use 'perf list' to list available events",
648                      parse_events),
649         OPT_INTEGER('p', "pid", &target_pid,
650                     "record events on existing pid"),
651         OPT_INTEGER('r', "realtime", &realtime_prio,
652                     "collect data with this RT SCHED_FIFO priority"),
653         OPT_BOOLEAN('R', "raw-samples", &raw_samples,
654                     "collect raw sample records from all opened counters"),
655         OPT_BOOLEAN('a', "all-cpus", &system_wide,
656                             "system-wide collection from all CPUs"),
657         OPT_BOOLEAN('A', "append", &append_file,
658                             "append to the output file to do incremental profiling"),
659         OPT_INTEGER('C', "profile_cpu", &profile_cpu,
660                             "CPU to profile on"),
661         OPT_BOOLEAN('f', "force", &force,
662                         "overwrite existing data file"),
663         OPT_LONG('c', "count", &default_interval,
664                     "event period to sample"),
665         OPT_STRING('o', "output", &output_name, "file",
666                     "output file name"),
667         OPT_BOOLEAN('i', "inherit", &inherit,
668                     "child tasks inherit counters"),
669         OPT_INTEGER('F', "freq", &freq,
670                     "profile at this frequency"),
671         OPT_INTEGER('m', "mmap-pages", &mmap_pages,
672                     "number of mmap data pages"),
673         OPT_BOOLEAN('g', "call-graph", &call_graph,
674                     "do call-graph (stack chain/backtrace) recording"),
675         OPT_BOOLEAN('v', "verbose", &verbose,
676                     "be more verbose (show counter open errors, etc)"),
677         OPT_BOOLEAN('s', "stat", &inherit_stat,
678                     "per thread counts"),
679         OPT_BOOLEAN('d', "data", &sample_address,
680                     "Sample addresses"),
681         OPT_BOOLEAN('n', "no-samples", &no_samples,
682                     "don't sample"),
683         OPT_END()
684 };
685
686 int cmd_record(int argc, const char **argv, const char *prefix __used)
687 {
688         int counter;
689
690         argc = parse_options(argc, argv, options, record_usage,
691                 PARSE_OPT_STOP_AT_NON_OPTION);
692         if (!argc && target_pid == -1 && !system_wide)
693                 usage_with_options(record_usage, options);
694
695         if (!nr_counters) {
696                 nr_counters     = 1;
697                 attrs[0].type   = PERF_TYPE_HARDWARE;
698                 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
699         }
700
701         for (counter = 0; counter < nr_counters; counter++) {
702                 if (attrs[counter].sample_period)
703                         continue;
704
705                 attrs[counter].sample_period = default_interval;
706         }
707
708         return __cmd_record(argc, argv);
709 }