Merge branch 'perf/live' into perf/core
[safe/jmp/linux-2.6] / tools / perf / builtin-record.c
index ca2affc..a1b99ee 100644 (file)
@@ -42,6 +42,7 @@ static unsigned int           mmap_pages                      =    128;
 static unsigned int            user_freq                       = UINT_MAX;
 static int                     freq                            =   1000;
 static int                     output;
+static int                     pipe_output                     =      0;
 static const char              *output_name                    = "perf.data";
 static int                     group                           =      0;
 static unsigned int            realtime_prio                   =      0;
@@ -109,6 +110,11 @@ static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
        pc->data_tail = tail;
 }
 
+static void advance_output(size_t size)
+{
+       bytes_written += size;
+}
+
 static void write_output(void *buf, size_t size)
 {
        while (size) {
@@ -435,10 +441,19 @@ static int process_buildids(void)
 
 static void atexit_header(void)
 {
-       session->header.data_size += bytes_written;
+       if (!pipe_output) {
+               session->header.data_size += bytes_written;
 
-       process_buildids();
-       perf_header__write(&session->header, output, true);
+               process_buildids();
+               perf_header__write(&session->header, output, true);
+       } else {
+               int err;
+
+               err = event__synthesize_build_ids(process_synthesized_event,
+                                                 session);
+               if (err < 0)
+                       pr_err("Couldn't synthesize build ids.\n");
+       }
 }
 
 static int __cmd_record(int argc, const char **argv)
@@ -464,7 +479,9 @@ static int __cmd_record(int argc, const char **argv)
                exit(-1);
        }
 
-       if (!stat(output_name, &st) && st.st_size) {
+       if (!strcmp(output_name, "-"))
+               pipe_output = 1;
+       else if (!stat(output_name, &st) && st.st_size) {
                if (write_mode == WRITE_FORCE) {
                        char oldname[PATH_MAX];
                        snprintf(oldname, sizeof(oldname), "%s.old",
@@ -482,7 +499,10 @@ static int __cmd_record(int argc, const char **argv)
        else
                flags |= O_TRUNC;
 
-       output = open(output_name, flags, S_IRUSR|S_IWUSR);
+       if (pipe_output)
+               output = STDOUT_FILENO;
+       else
+               output = open(output_name, flags, S_IRUSR | S_IWUSR);
        if (output < 0) {
                perror("failed to create output file");
                exit(-1);
@@ -496,7 +516,7 @@ static int __cmd_record(int argc, const char **argv)
        }
 
        if (!file_new) {
-               err = perf_header__read(&session->header, output);
+               err = perf_header__read(session, output);
                if (err < 0)
                        return err;
        }
@@ -522,6 +542,8 @@ static int __cmd_record(int argc, const char **argv)
                }
 
                if (!child_pid) {
+                       if (pipe_output)
+                               dup2(2, 1);
                        close(child_ready_pipe[0]);
                        close(go_pipe[1]);
                        fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
@@ -573,7 +595,11 @@ static int __cmd_record(int argc, const char **argv)
                        open_counters(cpumap[i]);
        }
 
-       if (file_new) {
+       if (pipe_output) {
+               err = perf_header__write_pipe(output);
+               if (err < 0)
+                       return err;
+       } else if (file_new) {
                err = perf_header__write(&session->header, output, false);
                if (err < 0)
                        return err;
@@ -581,6 +607,34 @@ static int __cmd_record(int argc, const char **argv)
 
        post_processing_offset = lseek(output, 0, SEEK_CUR);
 
+       if (pipe_output) {
+               err = event__synthesize_attrs(&session->header,
+                                             process_synthesized_event,
+                                             session);
+               if (err < 0) {
+                       pr_err("Couldn't synthesize attrs.\n");
+                       return err;
+               }
+
+               err = event__synthesize_event_types(process_synthesized_event,
+                                                   session);
+               if (err < 0) {
+                       pr_err("Couldn't synthesize event_types.\n");
+                       return err;
+               }
+
+               err = event__synthesize_tracing_data(output, attrs,
+                                                    nr_counters,
+                                                    process_synthesized_event,
+                                                    session);
+               if (err <= 0) {
+                       pr_err("Couldn't record tracing data.\n");
+                       return err;
+               }
+
+               advance_output(err);
+       }
+
        err = event__synthesize_kernel_mmap(process_synthesized_event,
                                            session, "_text");
        if (err < 0)