perf tools: Move the prototypes in util/string.h to util.h
[firefly-linux-kernel-4.4.55.git] / tools / perf / builtin-record.c
index 771533ced6a803d23886214d5551df1f0db440b1..dc61f1b68b40a745ce65dca57259e4277af74e63 100644 (file)
 #include "util/util.h"
 #include "util/parse-options.h"
 #include "util/parse-events.h"
-#include "util/string.h"
 
 #include "util/header.h"
 #include "util/event.h"
 #include "util/debug.h"
 #include "util/session.h"
 #include "util/symbol.h"
+#include "util/cpumap.h"
 
 #include <unistd.h>
 #include <sched.h>
 
-static int                     fd[MAX_NR_CPUS][MAX_COUNTERS];
+static int                     *fd[MAX_NR_CPUS][MAX_COUNTERS];
 
 static long                    default_interval                =      0;
 
@@ -42,6 +42,9 @@ static int                    raw_samples                     =      0;
 static int                     system_wide                     =      0;
 static int                     profile_cpu                     =     -1;
 static pid_t                   target_pid                      =     -1;
+static pid_t                   target_tid                      =     -1;
+static pid_t                   *all_tids                       =      NULL;
+static int                     thread_num                      =      0;
 static pid_t                   child_pid                       =     -1;
 static int                     inherit                         =      1;
 static int                     force                           =      0;
@@ -59,7 +62,7 @@ static struct timeval         this_read;
 
 static u64                     bytes_written                   =      0;
 
-static struct pollfd           event_array[MAX_NR_CPUS * MAX_COUNTERS];
+static struct pollfd           *event_array;
 
 static int                     nr_poll                         =      0;
 static int                     nr_cpu                          =      0;
@@ -76,7 +79,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][MAX_COUNTERS];
 
 static unsigned long mmap_read_head(struct mmap_data *md)
 {
@@ -224,12 +227,13 @@ static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int n
        return h_attr;
 }
 
-static void create_counter(int counter, int cpu, pid_t pid)
+static void create_counter(int counter, int cpu)
 {
        char *filter = filters[counter];
        struct perf_event_attr *attr = attrs + counter;
        struct perf_header_attr *h_attr;
        int track = !counter; /* only the first counter needs these */
+       int thread_index;
        int ret;
        struct {
                u64 count;
@@ -244,6 +248,9 @@ static void create_counter(int counter, int cpu, pid_t pid)
 
        attr->sample_type       |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
 
+       if (nr_counters > 1)
+               attr->sample_type |= PERF_SAMPLE_ID;
+
        if (freq) {
                attr->sample_type       |= PERF_SAMPLE_PERIOD;
                attr->freq              = 1;
@@ -271,118 +278,129 @@ static void create_counter(int counter, int cpu, pid_t pid)
        attr->mmap              = track;
        attr->comm              = track;
        attr->inherit           = inherit;
-       attr->disabled          = 1;
+       if (target_pid == -1 && !system_wide) {
+               attr->disabled = 1;
+               attr->enable_on_exec = 1;
+       }
 
+       for (thread_index = 0; thread_index < thread_num; thread_index++) {
 try_again:
-       fd[nr_cpu][counter] = sys_perf_event_open(attr, pid, cpu, group_fd, 0);
-
-       if (fd[nr_cpu][counter] < 0) {
-               int err = errno;
-
-               if (err == EPERM || err == EACCES)
-                       die("Permission error - are you root?\n");
-               else if (err ==  ENODEV && profile_cpu != -1)
-                       die("No such device - did you specify an out-of-range profile CPU?\n");
+               fd[nr_cpu][counter][thread_index] = sys_perf_event_open(attr,
+                               all_tids[thread_index], cpu, group_fd, 0);
+
+               if (fd[nr_cpu][counter][thread_index] < 0) {
+                       int err = errno;
+
+                       if (err == EPERM || err == EACCES)
+                               die("Permission error - are you root?\n"
+                                       "\t Consider tweaking"
+                                       " /proc/sys/kernel/perf_event_paranoid.\n");
+                       else if (err ==  ENODEV && profile_cpu != -1) {
+                               die("No such device - did you specify"
+                                       " an out-of-range profile CPU?\n");
+                       }
 
-               /*
-                * If it's cycles then fall back to hrtimer
-                * based cpu-clock-tick sw counter, which
-                * is always available even if no PMU support:
-                */
-               if (attr->type == PERF_TYPE_HARDWARE
-                       && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
-
-                       if (verbose)
-                               warning(" ... trying to fall back to cpu-clock-ticks\n");
-                       attr->type = PERF_TYPE_SOFTWARE;
-                       attr->config = PERF_COUNT_SW_CPU_CLOCK;
-                       goto try_again;
-               }
-               printf("\n");
-               error("perfcounter syscall returned with %d (%s)\n",
-                       fd[nr_cpu][counter], strerror(err));
+                       /*
+                        * If it's cycles then fall back to hrtimer
+                        * based cpu-clock-tick sw counter, which
+                        * is always available even if no PMU support:
+                        */
+                       if (attr->type == PERF_TYPE_HARDWARE
+                                       && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
+
+                               if (verbose)
+                                       warning(" ... trying to fall back to cpu-clock-ticks\n");
+                               attr->type = PERF_TYPE_SOFTWARE;
+                               attr->config = PERF_COUNT_SW_CPU_CLOCK;
+                               goto try_again;
+                       }
+                       printf("\n");
+                       error("perfcounter syscall returned with %d (%s)\n",
+                                       fd[nr_cpu][counter][thread_index], strerror(err));
 
 #if defined(__i386__) || defined(__x86_64__)
-               if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP)
-                       die("No hardware sampling interrupt available. No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.\n");
+                       if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP)
+                               die("No hardware sampling interrupt available."
+                                   " No APIC? If so then you can boot the kernel"
+                                   " with the \"lapic\" boot parameter to"
+                                   " force-enable it.\n");
 #endif
 
-               die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
-               exit(-1);
-       }
+                       die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
+                       exit(-1);
+               }
 
-       h_attr = get_header_attr(attr, counter);
-       if (h_attr == NULL)
-               die("nomem\n");
+               h_attr = get_header_attr(attr, counter);
+               if (h_attr == NULL)
+                       die("nomem\n");
 
-       if (!file_new) {
-               if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
-                       fprintf(stderr, "incompatible append\n");
-                       exit(-1);
+               if (!file_new) {
+                       if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
+                               fprintf(stderr, "incompatible append\n");
+                               exit(-1);
+                       }
                }
-       }
 
-       if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
-               perror("Unable to read perf file descriptor\n");
-               exit(-1);
-       }
+               if (read(fd[nr_cpu][counter][thread_index], &read_data, sizeof(read_data)) == -1) {
+                       perror("Unable to read perf file descriptor\n");
+                       exit(-1);
+               }
 
-       if (perf_header_attr__add_id(h_attr, read_data.id) < 0) {
-               pr_warning("Not enough memory to add id\n");
-               exit(-1);
-       }
+               if (perf_header_attr__add_id(h_attr, read_data.id) < 0) {
+                       pr_warning("Not enough memory to add id\n");
+                       exit(-1);
+               }
 
-       assert(fd[nr_cpu][counter] >= 0);
-       fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
+               assert(fd[nr_cpu][counter][thread_index] >= 0);
+               fcntl(fd[nr_cpu][counter][thread_index], F_SETFL, O_NONBLOCK);
 
-       /*
-        * First counter acts as the group leader:
-        */
-       if (group && group_fd == -1)
-               group_fd = fd[nr_cpu][counter];
-       if (multiplex && multiplex_fd == -1)
-               multiplex_fd = fd[nr_cpu][counter];
+               /*
+                * First counter acts as the group leader:
+                */
+               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] != multiplex_fd) {
+               if (multiplex && fd[nr_cpu][counter][thread_index] != multiplex_fd) {
 
-               ret = ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
-               assert(ret != -1);
-       } else {
-               event_array[nr_poll].fd = fd[nr_cpu][counter];
-               event_array[nr_poll].events = POLLIN;
-               nr_poll++;
-
-               mmap_array[nr_cpu][counter].counter = counter;
-               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|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);
+                       ret = ioctl(fd[nr_cpu][counter][thread_index], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
+                       assert(ret != -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,
+                               PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter][thread_index], 0);
+                       if (mmap_array[nr_cpu][counter][thread_index].base == MAP_FAILED) {
+                               error("failed to mmap with %d (%s)\n", errno, strerror(errno));
+                               exit(-1);
+                       }
                }
-       }
 
-       if (filter != NULL) {
-               ret = ioctl(fd[nr_cpu][counter],
-                           PERF_EVENT_IOC_SET_FILTER, filter);
-               if (ret) {
-                       error("failed to set filter with %d (%s)\n", errno,
-                             strerror(errno));
-                       exit(-1);
+               if (filter != NULL) {
+                       ret = ioctl(fd[nr_cpu][counter][thread_index],
+                                       PERF_EVENT_IOC_SET_FILTER, filter);
+                       if (ret) {
+                               error("failed to set filter with %d (%s)\n", errno,
+                                               strerror(errno));
+                               exit(-1);
+                       }
                }
        }
-
-       ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_ENABLE);
 }
 
-static void open_counters(int cpu, pid_t pid)
+static void open_counters(int cpu)
 {
        int counter;
 
        group_fd = -1;
        for (counter = 0; counter < nr_counters; counter++)
-               create_counter(counter, cpu, pid);
+               create_counter(counter, cpu);
 
        nr_cpu++;
 }
@@ -391,6 +409,9 @@ static int process_buildids(void)
 {
        u64 size = lseek(output, 0, SEEK_CUR);
 
+       if (size == 0)
+               return 0;
+
        session->fd = output;
        return __perf_session__process_events(session, post_processing_offset,
                                              size - post_processing_offset,
@@ -414,13 +435,10 @@ static int __cmd_record(int argc, const char **argv)
        int err;
        unsigned long waking = 0;
        int child_ready_pipe[2], go_pipe[2];
-       const bool forks = target_pid == -1 && argc > 0;
+       const bool forks = argc > 0;
        char buf;
 
        page_size = sysconf(_SC_PAGE_SIZE);
-       nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
-       assert(nr_cpus <= MAX_NR_CPUS);
-       assert(nr_cpus >= 0);
 
        atexit(sig_atexit);
        signal(SIGCHLD, sig_handler);
@@ -488,13 +506,13 @@ static int __cmd_record(int argc, const char **argv)
        atexit(atexit_header);
 
        if (forks) {
-               pid = fork();
+               child_pid = fork();
                if (pid < 0) {
                        perror("failed to fork");
                        exit(-1);
                }
 
-               if (!pid) {
+               if (!child_pid) {
                        close(child_ready_pipe[0]);
                        close(go_pipe[1]);
                        fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
@@ -523,10 +541,8 @@ static int __cmd_record(int argc, const char **argv)
                        exit(-1);
                }
 
-               child_pid = pid;
-
-               if (!system_wide)
-                       target_pid = pid;
+               if (!system_wide && target_tid == -1 && target_pid == -1)
+                       all_tids[0] = child_pid;
 
                close(child_ready_pipe[1]);
                close(go_pipe[0]);
@@ -540,12 +556,12 @@ static int __cmd_record(int argc, const char **argv)
                close(child_ready_pipe[0]);
        }
 
-
        if ((!system_wide && !inherit) || profile_cpu != -1) {
-               open_counters(profile_cpu, target_pid);
+               open_counters(profile_cpu);
        } else {
+               nr_cpus = read_cpu_map();
                for (i = 0; i < nr_cpus; i++)
-                       open_counters(i, target_pid);
+                       open_counters(cpumap[i]);
        }
 
        if (file_new) {
@@ -558,6 +574,9 @@ static int __cmd_record(int argc, const char **argv)
 
        err = event__synthesize_kernel_mmap(process_synthesized_event,
                                            session, "_text");
+       if (err < 0)
+               err = event__synthesize_kernel_mmap(process_synthesized_event,
+                                                   session, "_stext");
        if (err < 0) {
                pr_err("Couldn't record kernel reference relocation symbol.\n");
                return err;
@@ -570,7 +589,7 @@ static int __cmd_record(int argc, const char **argv)
        }
 
        if (!system_wide && profile_cpu == -1)
-               event__synthesize_thread(target_pid, process_synthesized_event,
+               event__synthesize_thread(target_tid, process_synthesized_event,
                                         session);
        else
                event__synthesize_threads(process_synthesized_event, session);
@@ -593,11 +612,16 @@ static int __cmd_record(int argc, const char **argv)
 
        for (;;) {
                int hits = samples;
+               int thread;
 
                for (i = 0; i < nr_cpu; i++) {
                        for (counter = 0; counter < nr_counters; counter++) {
-                               if (mmap_array[i][counter].base)
-                                       mmap_read(&mmap_array[i][counter]);
+                               for (thread = 0;
+                                       thread < thread_num; thread++) {
+                                       if (mmap_array[i][counter][thread].base)
+                                               mmap_read(&mmap_array[i][counter][thread]);
+                               }
+
                        }
                }
 
@@ -610,8 +634,15 @@ static int __cmd_record(int argc, const char **argv)
 
                if (done) {
                        for (i = 0; i < nr_cpu; i++) {
-                               for (counter = 0; counter < nr_counters; counter++)
-                                       ioctl(fd[i][counter], PERF_EVENT_IOC_DISABLE);
+                               for (counter = 0;
+                                       counter < nr_counters;
+                                       counter++) {
+                                       for (thread = 0;
+                                               thread < thread_num;
+                                               thread++)
+                                               ioctl(fd[i][counter][thread],
+                                                       PERF_EVENT_IOC_DISABLE);
+                               }
                        }
                }
        }
@@ -643,7 +674,9 @@ static const struct option options[] = {
        OPT_CALLBACK(0, "filter", NULL, "filter",
                     "event filter", parse_filter),
        OPT_INTEGER('p', "pid", &target_pid,
-                   "record events on existing pid"),
+                   "record events on existing process id"),
+       OPT_INTEGER('t', "tid", &target_tid,
+                   "record events on existing thread id"),
        OPT_INTEGER('r', "realtime", &realtime_prio,
                    "collect data with this RT SCHED_FIFO priority"),
        OPT_BOOLEAN('R', "raw-samples", &raw_samples,
@@ -684,10 +717,12 @@ static const struct option options[] = {
 int cmd_record(int argc, const char **argv, const char *prefix __used)
 {
        int counter;
+       int i,j;
 
        argc = parse_options(argc, argv, options, record_usage,
                            PARSE_OPT_STOP_AT_NON_OPTION);
-       if (!argc && target_pid == -1 && !system_wide && profile_cpu == -1)
+       if (!argc && target_pid == -1 && target_tid == -1 &&
+               !system_wide && profile_cpu == -1)
                usage_with_options(record_usage, options);
 
        symbol__init();
@@ -698,6 +733,37 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
                attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
        }
 
+       if (target_pid != -1) {
+               target_tid = target_pid;
+               thread_num = find_all_tid(target_pid, &all_tids);
+               if (thread_num <= 0) {
+                       fprintf(stderr, "Can't find all threads of pid %d\n",
+                                       target_pid);
+                       usage_with_options(record_usage, options);
+               }
+       } else {
+               all_tids=malloc(sizeof(pid_t));
+               if (!all_tids)
+                       return -ENOMEM;
+
+               all_tids[0] = target_tid;
+               thread_num = 1;
+       }
+
+       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])
+                               return -ENOMEM;
+               }
+       }
+       event_array = malloc(
+               sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num);
+       if (!event_array)
+               return -ENOMEM;
+
        /*
         * User specified count overrides default frequency.
         */