block: bd_start_claiming cleanup
[safe/jmp/linux-2.6] / tools / perf / builtin-record.c
index 83b308a..dc3435e 100644 (file)
@@ -25,6 +25,7 @@
 
 #include <unistd.h>
 #include <sched.h>
+#include <sys/mman.h>
 
 enum write_mode_t {
        WRITE_FORCE,
@@ -33,8 +34,8 @@ enum write_mode_t {
 
 static int                     *fd[MAX_NR_CPUS][MAX_COUNTERS];
 
-static unsigned int            user_interval                   = UINT_MAX;
-static long                    default_interval                =      0;
+static u64                     user_interval                   = ULLONG_MAX;
+static u64                     default_interval                =      0;
 
 static int                     nr_cpus                         =      0;
 static unsigned int            page_size;
@@ -45,7 +46,7 @@ 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;
+static int                     realtime_prio                   =      0;
 static bool                    raw_samples                     =  false;
 static bool                    system_wide                     =  false;
 static int                     profile_cpu                     =     -1;
@@ -54,19 +55,14 @@ static pid_t                        target_tid                      =     -1;
 static pid_t                   *all_tids                       =      NULL;
 static int                     thread_num                      =      0;
 static pid_t                   child_pid                       =     -1;
-static bool                    inherit                         =   true;
+static bool                    no_inherit                      =  false;
 static enum write_mode_t       write_mode                      = WRITE_FORCE;
 static bool                    call_graph                      =  false;
 static bool                    inherit_stat                    =  false;
 static bool                    no_samples                      =  false;
 static bool                    sample_address                  =  false;
-static bool                    multiplex                       =  false;
-static int                     multiplex_fd                    =     -1;
 
 static long                    samples                         =      0;
-static struct timeval          last_read;
-static struct timeval          this_read;
-
 static u64                     bytes_written                   =      0;
 
 static struct pollfd           *event_array;
@@ -86,7 +82,7 @@ struct mmap_data {
        unsigned int            prev;
 };
 
-static struct mmap_data                *mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
+static struct mmap_data                mmap_array[MAX_NR_CPUS];
 
 static unsigned long mmap_read_head(struct mmap_data *md)
 {
@@ -146,8 +142,6 @@ static void mmap_read(struct mmap_data *md)
        void *buf;
        int diff;
 
-       gettimeofday(&this_read, NULL);
-
        /*
         * If we're further behind than half the buffer, there's a chance
         * the writer will bite our tail and mess up the samples under us.
@@ -158,23 +152,13 @@ static void mmap_read(struct mmap_data *md)
         */
        diff = head - old;
        if (diff < 0) {
-               struct timeval iv;
-               unsigned long msecs;
-
-               timersub(&this_read, &last_read, &iv);
-               msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
-
-               fprintf(stderr, "WARNING: failed to keep up with mmap data."
-                               "  Last read %lu msecs ago.\n", msecs);
-
+               fprintf(stderr, "WARNING: failed to keep up with mmap data\n");
                /*
                 * head points to a known good entry, start there.
                 */
                old = head;
        }
 
-       last_read = this_read;
-
        if (old != head)
                samples++;
 
@@ -268,7 +252,7 @@ static void create_counter(int counter, int cpu)
         * it a weak assumption overridable by the user.
         */
        if (!attr->sample_period || (user_freq != UINT_MAX &&
-                                    user_interval != UINT_MAX)) {
+                                    user_interval != ULLONG_MAX)) {
                if (freq) {
                        attr->sample_type       |= PERF_SAMPLE_PERIOD;
                        attr->freq              = 1;
@@ -298,8 +282,8 @@ static void create_counter(int counter, int cpu)
 
        attr->mmap              = track;
        attr->comm              = track;
-       attr->inherit           = inherit;
-       if (target_pid == -1 && !system_wide) {
+       attr->inherit           = !no_inherit;
+       if (target_pid == -1 && target_tid == -1 && !system_wide) {
                attr->disabled = 1;
                attr->enable_on_exec = 1;
        }
@@ -380,27 +364,30 @@ try_again:
                 */
                if (group && group_fd == -1)
                        group_fd = fd[nr_cpu][counter][thread_index];
-               if (multiplex && multiplex_fd == -1)
-                       multiplex_fd = fd[nr_cpu][counter][thread_index];
 
-               if (multiplex && fd[nr_cpu][counter][thread_index] != multiplex_fd) {
-
-                       ret = ioctl(fd[nr_cpu][counter][thread_index], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
-                       assert(ret != -1);
+               if (counter || thread_index) {
+                       ret = ioctl(fd[nr_cpu][counter][thread_index],
+                                       PERF_EVENT_IOC_SET_OUTPUT,
+                                       fd[nr_cpu][0][0]);
+                       if (ret) {
+                               error("failed to set output: %d (%s)\n", errno,
+                                               strerror(errno));
+                               exit(-1);
+                       }
                } else {
-                       event_array[nr_poll].fd = fd[nr_cpu][counter][thread_index];
-                       event_array[nr_poll].events = POLLIN;
-                       nr_poll++;
-
-                       mmap_array[nr_cpu][counter][thread_index].counter = counter;
-                       mmap_array[nr_cpu][counter][thread_index].prev = 0;
-                       mmap_array[nr_cpu][counter][thread_index].mask = mmap_pages*page_size - 1;
-                       mmap_array[nr_cpu][counter][thread_index].base = mmap(NULL, (mmap_pages+1)*page_size,
+                       mmap_array[nr_cpu].counter = counter;
+                       mmap_array[nr_cpu].prev = 0;
+                       mmap_array[nr_cpu].mask = mmap_pages*page_size - 1;
+                       mmap_array[nr_cpu].base = mmap(NULL, (mmap_pages+1)*page_size,
                                PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter][thread_index], 0);
-                       if (mmap_array[nr_cpu][counter][thread_index].base == MAP_FAILED) {
+                       if (mmap_array[nr_cpu].base == MAP_FAILED) {
                                error("failed to mmap with %d (%s)\n", errno, strerror(errno));
                                exit(-1);
                        }
+
+                       event_array[nr_poll].fd = fd[nr_cpu][counter][thread_index];
+                       event_array[nr_poll].events = POLLIN;
+                       nr_poll++;
                }
 
                if (filter != NULL) {
@@ -446,13 +433,6 @@ static void atexit_header(void)
 
                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");
        }
 }
 
@@ -501,11 +481,28 @@ static void event__synthesize_guest_os(struct machine *machine, void *data)
                       " relocation symbol.\n", machine->pid);
 }
 
+static struct perf_event_header finished_round_event = {
+       .size = sizeof(struct perf_event_header),
+       .type = PERF_RECORD_FINISHED_ROUND,
+};
+
+static void mmap_read_all(void)
+{
+       int i;
+
+       for (i = 0; i < nr_cpu; i++) {
+               if (mmap_array[i].base)
+                       mmap_read(&mmap_array[i]);
+       }
+
+       if (perf_header__has_feat(&session->header, HEADER_TRACE_INFO))
+               write_output(&finished_round_event, sizeof(finished_round_event));
+}
+
 static int __cmd_record(int argc, const char **argv)
 {
        int i, counter;
        struct stat st;
-       pid_t pid = 0;
        int flags;
        int err;
        unsigned long waking = 0;
@@ -555,7 +552,7 @@ static int __cmd_record(int argc, const char **argv)
        }
 
        session = perf_session__new(output_name, O_WRONLY,
-                                   write_mode == WRITE_FORCE);
+                                   write_mode == WRITE_FORCE, false);
        if (session == NULL) {
                pr_err("Not enough memory for reading perf file header\n");
                return -1;
@@ -567,22 +564,14 @@ static int __cmd_record(int argc, const char **argv)
                        return err;
        }
 
-       if (raw_samples) {
+       if (have_tracepoints(attrs, nr_counters))
                perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
-       } else {
-               for (i = 0; i < nr_counters; i++) {
-                       if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
-                               perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
-                               break;
-                       }
-               }
-       }
 
        atexit(atexit_header);
 
        if (forks) {
                child_pid = fork();
-               if (pid < 0) {
+               if (child_pid < 0) {
                        perror("failed to fork");
                        exit(-1);
                }
@@ -633,7 +622,7 @@ static int __cmd_record(int argc, const char **argv)
                close(child_ready_pipe[0]);
        }
 
-       if ((!system_wide && !inherit) || profile_cpu != -1) {
+       if ((!system_wide && no_inherit) || profile_cpu != -1) {
                open_counters(profile_cpu);
        } else {
                nr_cpus = read_cpu_map();
@@ -669,16 +658,25 @@ static int __cmd_record(int argc, const char **argv)
                        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;
+               if (have_tracepoints(attrs, nr_counters)) {
+                       /*
+                        * FIXME err <= 0 here actually means that
+                        * there were no tracepoints so its not really
+                        * an error, just that we don't need to
+                        * synthesize anything.  We really have to
+                        * return this more properly and also
+                        * propagate errors that now are calling die()
+                        */
+                       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);
                }
-
-               advance_output(err);
        }
 
        machine = perf_session__find_host_machine(session);
@@ -732,16 +730,7 @@ static int __cmd_record(int argc, const char **argv)
                int hits = samples;
                int thread;
 
-               for (i = 0; i < nr_cpu; i++) {
-                       for (counter = 0; counter < nr_counters; counter++) {
-                               for (thread = 0;
-                                       thread < thread_num; thread++) {
-                                       if (mmap_array[i][counter][thread].base)
-                                               mmap_read(&mmap_array[i][counter][thread]);
-                               }
-
-                       }
-               }
+               mmap_read_all();
 
                if (hits == samples) {
                        if (done)
@@ -809,16 +798,13 @@ static const struct option options[] = {
                            "CPU to profile on"),
        OPT_BOOLEAN('f', "force", &force,
                        "overwrite existing data file (deprecated)"),
-       OPT_LONG('c', "count", &user_interval,
-                   "event period to sample"),
+       OPT_U64('c', "count", &user_interval, "event period to sample"),
        OPT_STRING('o', "output", &output_name, "file",
                    "output file name"),
-       OPT_BOOLEAN('i', "inherit", &inherit,
-                   "child tasks inherit counters"),
-       OPT_INTEGER('F', "freq", &user_freq,
-                   "profile at this frequency"),
-       OPT_INTEGER('m', "mmap-pages", &mmap_pages,
-                   "number of mmap data pages"),
+       OPT_BOOLEAN('i', "no-inherit", &no_inherit,
+                   "child tasks do not inherit counters"),
+       OPT_UINTEGER('F', "freq", &user_freq, "profile at this frequency"),
+       OPT_UINTEGER('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_INCR('v', "verbose", &verbose,
@@ -829,8 +815,6 @@ static const struct option options[] = {
                    "Sample addresses"),
        OPT_BOOLEAN('n', "no-samples", &no_samples,
                    "don't sample"),
-       OPT_BOOLEAN('M', "multiplex", &multiplex,
-                   "multiplex counter output in a single channel"),
        OPT_END()
 };
 
@@ -882,9 +866,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
        for (i = 0; i < MAX_NR_CPUS; i++) {
                for (j = 0; j < MAX_COUNTERS; j++) {
                        fd[i][j] = malloc(sizeof(int)*thread_num);
-                       mmap_array[i][j] = zalloc(
-                               sizeof(struct mmap_data)*thread_num);
-                       if (!fd[i][j] || !mmap_array[i][j])
+                       if (!fd[i][j])
                                return -ENOMEM;
                }
        }
@@ -893,7 +875,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
        if (!event_array)
                return -ENOMEM;
 
-       if (user_interval != UINT_MAX)
+       if (user_interval != ULLONG_MAX)
                default_interval = user_interval;
        if (user_freq != UINT_MAX)
                freq = user_freq;