perf_counter tools: Add a data file header
[safe/jmp/linux-2.6] / tools / perf / builtin-record.c
index 29259e7..2830467 100644 (file)
@@ -37,6 +37,7 @@ static pid_t                  target_pid                      = -1;
 static int                     inherit                         = 1;
 static int                     force                           = 0;
 static int                     append_file                     = 0;
+static int                     call_graph                      = 0;
 static int                     verbose                         = 0;
 
 static long                    samples;
@@ -50,6 +51,9 @@ static struct pollfd          event_array[MAX_NR_CPUS * MAX_COUNTERS];
 static int                     nr_poll;
 static int                     nr_cpu;
 
+static int                     file_new = 1;
+static struct perf_file_header file_header;
+
 struct mmap_event {
        struct perf_event_header        header;
        __u32                           pid;
@@ -77,10 +81,10 @@ struct mmap_data {
 
 static struct mmap_data                mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
 
-static unsigned int mmap_read_head(struct mmap_data *md)
+static unsigned long mmap_read_head(struct mmap_data *md)
 {
        struct perf_counter_mmap_page *pc = md->base;
-       int head;
+       long head;
 
        head = pc->data_head;
        rmb();
@@ -88,6 +92,32 @@ static unsigned int mmap_read_head(struct mmap_data *md)
        return head;
 }
 
+static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
+{
+       struct perf_counter_mmap_page *pc = md->base;
+
+       /*
+        * ensure all reads are done before we write the tail out.
+        */
+       /* mb(); */
+       pc->data_tail = tail;
+}
+
+static void write_output(void *buf, size_t size)
+{
+       while (size) {
+               int ret = write(output, buf, size);
+
+               if (ret < 0)
+                       die("failed to write");
+
+               size -= ret;
+               buf += ret;
+
+               bytes_written += ret;
+       }
+}
+
 static void mmap_read(struct mmap_data *md)
 {
        unsigned int head = mmap_read_head(md);
@@ -108,7 +138,7 @@ static void mmap_read(struct mmap_data *md)
         * In either case, truncate and restart at head.
         */
        diff = head - old;
-       if (diff > md->mask / 2 || diff < 0) {
+       if (diff < 0) {
                struct timeval iv;
                unsigned long msecs;
 
@@ -136,36 +166,17 @@ static void mmap_read(struct mmap_data *md)
                size = md->mask + 1 - (old & md->mask);
                old += size;
 
-               while (size) {
-                       int ret = write(output, buf, size);
-
-                       if (ret < 0)
-                               die("failed to write");
-
-                       size -= ret;
-                       buf += ret;
-
-                       bytes_written += ret;
-               }
+               write_output(buf, size);
        }
 
        buf = &data[old & md->mask];
        size = head - old;
        old += size;
 
-       while (size) {
-               int ret = write(output, buf, size);
-
-               if (ret < 0)
-                       die("failed to write");
-
-               size -= ret;
-               buf += ret;
-
-               bytes_written += ret;
-       }
+       write_output(buf, size);
 
        md->prev = old;
+       mmap_write_tail(md, old);
 }
 
 static volatile int done = 0;
@@ -191,7 +202,7 @@ static void pid_synthesize_comm_event(pid_t pid, int full)
        struct comm_event comm_ev;
        char filename[PATH_MAX];
        char bf[BUFSIZ];
-       int fd, ret;
+       int fd;
        size_t size;
        char *field, *sep;
        DIR *tasks;
@@ -201,8 +212,12 @@ static void pid_synthesize_comm_event(pid_t pid, int full)
 
        fd = open(filename, O_RDONLY);
        if (fd < 0) {
-               fprintf(stderr, "couldn't open %s\n", filename);
-               exit(EXIT_FAILURE);
+               /*
+                * We raced with a task exiting - just return:
+                */
+               if (verbose)
+                       fprintf(stderr, "couldn't open %s\n", filename);
+               return;
        }
        if (read(fd, bf, sizeof(bf)) < 0) {
                fprintf(stderr, "couldn't read %s\n", filename);
@@ -229,11 +244,7 @@ static void pid_synthesize_comm_event(pid_t pid, int full)
        if (!full) {
                comm_ev.tid = pid;
 
-               ret = write(output, &comm_ev, comm_ev.header.size);
-               if (ret < 0) {
-                       perror("failed to write");
-                       exit(-1);
-               }
+               write_output(&comm_ev, comm_ev.header.size);
                return;
        }
 
@@ -248,11 +259,7 @@ static void pid_synthesize_comm_event(pid_t pid, int full)
 
                comm_ev.tid = pid;
 
-               ret = write(output, &comm_ev, comm_ev.header.size);
-               if (ret < 0) {
-                       perror("failed to write");
-                       exit(-1);
-               }
+               write_output(&comm_ev, comm_ev.header.size);
        }
        closedir(tasks);
        return;
@@ -272,8 +279,12 @@ static void pid_synthesize_mmap_samples(pid_t pid)
 
        fp = fopen(filename, "r");
        if (fp == NULL) {
-               fprintf(stderr, "couldn't open %s\n", filename);
-               exit(EXIT_FAILURE);
+               /*
+                * We raced with a task exiting - just return:
+                */
+               if (verbose)
+                       fprintf(stderr, "couldn't open %s\n", filename);
+               return;
        }
        while (1) {
                char bf[BUFSIZ], *pbf = bf;
@@ -311,10 +322,7 @@ static void pid_synthesize_mmap_samples(pid_t pid)
                        mmap_ev.pid = pid;
                        mmap_ev.tid = pid;
 
-                       if (write(output, &mmap_ev, mmap_ev.header.size) < 0) {
-                               perror("failed to write");
-                               exit(-1);
-                       }
+                       write_output(&mmap_ev, mmap_ev.header.size);
                }
        }
 
@@ -351,11 +359,25 @@ static void create_counter(int counter, int cpu, pid_t pid)
        int track = 1;
 
        attr->sample_type       = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
+
        if (freq) {
                attr->sample_type       |= PERF_SAMPLE_PERIOD;
                attr->freq              = 1;
                attr->sample_freq       = freq;
        }
+
+       if (call_graph)
+               attr->sample_type       |= PERF_SAMPLE_CALLCHAIN;
+
+       if (file_new) {
+               file_header.sample_type = attr->sample_type;
+       } else {
+               if (file_header.sample_type != attr->sample_type) {
+                       fprintf(stderr, "incompatible append\n");
+                       exit(-1);
+               }
+       }
+
        attr->mmap              = track;
        attr->comm              = track;
        attr->inherit           = (cpu < 0) && inherit;
@@ -410,7 +432,7 @@ try_again:
        mmap_array[nr_cpu][counter].prev = 0;
        mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
        mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
-                       PROT_READ, MAP_SHARED, fd[nr_cpu][counter], 0);
+                       PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
        if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
                error("failed to mmap with %d (%s)\n", errno, strerror(errno));
                exit(-1);
@@ -435,6 +457,13 @@ static void open_counters(int cpu, pid_t pid)
        nr_cpu++;
 }
 
+static void atexit_header(void)
+{
+       file_header.data_size += bytes_written;
+
+       pwrite(output, &file_header, sizeof(file_header), 0);
+}
+
 static int __cmd_record(int argc, const char **argv)
 {
        int i, counter;
@@ -448,6 +477,10 @@ static int __cmd_record(int argc, const char **argv)
        assert(nr_cpus <= MAX_NR_CPUS);
        assert(nr_cpus >= 0);
 
+       atexit(sig_atexit);
+       signal(SIGCHLD, sig_handler);
+       signal(SIGINT, sig_handler);
+
        if (!stat(output_name, &st) && !force && !append_file) {
                fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
                                output_name);
@@ -456,7 +489,7 @@ static int __cmd_record(int argc, const char **argv)
 
        flags = O_CREAT|O_RDWR;
        if (append_file)
-               flags |= O_APPEND;
+               file_new = 0;
        else
                flags |= O_TRUNC;
 
@@ -466,15 +499,18 @@ static int __cmd_record(int argc, const char **argv)
                exit(-1);
        }
 
+       if (!file_new) {
+               read(output, &file_header, sizeof(file_header));
+               lseek(output, file_header.data_size, SEEK_CUR);
+       }
+
+       atexit(atexit_header);
+
        if (!system_wide) {
                open_counters(-1, target_pid != -1 ? target_pid : getpid());
        } else for (i = 0; i < nr_cpus; i++)
                open_counters(i, target_pid);
 
-       atexit(sig_atexit);
-       signal(SIGCHLD, sig_handler);
-       signal(SIGINT, sig_handler);
-
        if (target_pid == -1 && argc) {
                pid = fork();
                if (pid < 0)
@@ -555,6 +591,8 @@ static const struct option options[] = {
                    "profile at this frequency"),
        OPT_INTEGER('m', "mmap-pages", &mmap_pages,
                    "number of mmap data pages"),
+       OPT_BOOLEAN('g', "call-graph", &call_graph,
+                   "do call-graph (stack chain/backtrace) recording"),
        OPT_BOOLEAN('v', "verbose", &verbose,
                    "be more verbose (show counter open errors, etc)"),
        OPT_END()
@@ -568,8 +606,11 @@ int cmd_record(int argc, const char **argv, const char *prefix)
        if (!argc && target_pid == -1 && !system_wide)
                usage_with_options(record_usage, options);
 
-       if (!nr_counters)
-               nr_counters = 1;
+       if (!nr_counters) {
+               nr_counters     = 1;
+               attrs[0].type   = PERF_TYPE_HARDWARE;
+               attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
+       }
 
        for (counter = 0; counter < nr_counters; counter++) {
                if (attrs[counter].sample_period)