perf: Add non-exec mmap() tracking
[deliverable/linux.git] / tools / perf / builtin-record.c
index cb46c7d0ea99436863cbfed0c6e9a67f8618e9fe..39c7247bc54aa707b5fa4bef05b5e2bae0127d01 100644 (file)
@@ -25,6 +25,7 @@
 
 #include <unistd.h>
 #include <sched.h>
+#include <sys/mman.h>
 
 enum write_mode_t {
        WRITE_FORCE,
@@ -48,7 +49,6 @@ static int                    group                           =      0;
 static int                     realtime_prio                   =      0;
 static bool                    raw_samples                     =  false;
 static bool                    system_wide                     =  false;
-static int                     profile_cpu                     =     -1;
 static pid_t                   target_pid                      =     -1;
 static pid_t                   target_tid                      =     -1;
 static pid_t                   *all_tids                       =      NULL;
@@ -60,13 +60,8 @@ 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;
@@ -78,6 +73,7 @@ static int                    file_new                        =      1;
 static off_t                   post_processing_offset;
 
 static struct perf_session     *session;
+static const char              *cpu_list;
 
 struct mmap_data {
        int                     counter;
@@ -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++;
 
@@ -284,12 +268,17 @@ static void create_counter(int counter, int cpu)
        if (inherit_stat)
                attr->inherit_stat = 1;
 
-       if (sample_address)
+       if (sample_address) {
                attr->sample_type       |= PERF_SAMPLE_ADDR;
+               attr->mmap_data = track;
+       }
 
        if (call_graph)
                attr->sample_type       |= PERF_SAMPLE_CALLCHAIN;
 
+       if (system_wide)
+               attr->sample_type       |= PERF_SAMPLE_CPU;
+
        if (raw_samples) {
                attr->sample_type       |= PERF_SAMPLE_TIME;
                attr->sample_type       |= PERF_SAMPLE_RAW;
@@ -316,7 +305,7 @@ try_again:
                                die("Permission error - are you root?\n"
                                        "\t Consider tweaking"
                                        " /proc/sys/kernel/perf_event_paranoid.\n");
-                       else if (err ==  ENODEV && profile_cpu != -1) {
+                       else if (err ==  ENODEV && cpu_list) {
                                die("No such device - did you specify"
                                        " an out-of-range profile CPU?\n");
                        }
@@ -380,27 +369,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) {
@@ -501,16 +493,11 @@ static struct perf_event_header finished_round_event = {
 
 static void mmap_read_all(void)
 {
-       int i, counter, thread;
+       int i;
 
        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]);
-                       }
-
-               }
+               if (mmap_array[i].base)
+                       mmap_read(&mmap_array[i]);
        }
 
        if (perf_header__has_feat(&session->header, HEADER_TRACE_INFO))
@@ -521,7 +508,6 @@ 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;
@@ -590,7 +576,7 @@ static int __cmd_record(int argc, const char **argv)
 
        if (forks) {
                child_pid = fork();
-               if (pid < 0) {
+               if (child_pid < 0) {
                        perror("failed to fork");
                        exit(-1);
                }
@@ -641,10 +627,15 @@ static int __cmd_record(int argc, const char **argv)
                close(child_ready_pipe[0]);
        }
 
-       if ((!system_wide && no_inherit) || profile_cpu != -1) {
-               open_counters(profile_cpu);
+       nr_cpus = read_cpu_map(cpu_list);
+       if (nr_cpus < 1) {
+               perror("failed to collect number of CPUs\n");
+               return -1;
+       }
+
+       if (!system_wide && no_inherit && !cpu_list) {
+               open_counters(-1);
        } else {
-               nr_cpus = read_cpu_map();
                for (i = 0; i < nr_cpus; i++)
                        open_counters(cpumap[i]);
        }
@@ -723,7 +714,7 @@ static int __cmd_record(int argc, const char **argv)
        if (perf_guest)
                perf_session__process_machines(session, event__synthesize_guest_os);
 
-       if (!system_wide && profile_cpu == -1)
+       if (!system_wide && cpu_list)
                event__synthesize_thread(target_tid, process_synthesized_event,
                                         session);
        else
@@ -813,8 +804,8 @@ static const struct option options[] = {
                            "system-wide collection from all CPUs"),
        OPT_BOOLEAN('A', "append", &append_file,
                            "append to the output file to do incremental profiling"),
-       OPT_INTEGER('C', "profile_cpu", &profile_cpu,
-                           "CPU to profile on"),
+       OPT_STRING('C', "cpu", &cpu_list, "cpu",
+                   "list of cpus to monitor"),
        OPT_BOOLEAN('f', "force", &force,
                        "overwrite existing data file (deprecated)"),
        OPT_U64('c', "count", &user_interval, "event period to sample"),
@@ -834,8 +825,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()
 };
 
@@ -846,7 +835,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
        argc = parse_options(argc, argv, options, record_usage,
                            PARSE_OPT_STOP_AT_NON_OPTION);
        if (!argc && target_pid == -1 && target_tid == -1 &&
-               !system_wide && profile_cpu == -1)
+               !system_wide && !cpu_list)
                usage_with_options(record_usage, options);
 
        if (force && append_file) {
@@ -887,9 +876,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;
                }
        }
This page took 0.14734 seconds and 5 git commands to generate.