/dev/mem: remove redundant test on len
[safe/jmp/linux-2.6] / tools / perf / builtin-stat.c
index 61b8282..c70d720 100644 (file)
 #include <sys/prctl.h>
 #include <math.h>
 
-static struct perf_counter_attr default_attrs[] = {
+static struct perf_event_attr default_attrs[] = {
 
-  { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK     },
-  { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES},
-  { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
-  { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS    },
+  { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK             },
+  { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES       },
+  { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS         },
+  { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS            },
 
-  { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES     },
-  { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS   },
-  { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_REFERENCES},
-  { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_MISSES   },
+  { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES             },
+  { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS           },
+  { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS    },
+  { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES          },
+  { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_REFERENCES       },
+  { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_MISSES           },
 
 };
 
@@ -69,7 +71,8 @@ static int                    run_idx                         =  0;
 static int                     run_count                       =  1;
 static int                     inherit                         =  1;
 static int                     scale                           =  1;
-static int                     target_pid                      = -1;
+static pid_t                   target_pid                      = -1;
+static pid_t                   child_pid                       = -1;
 static int                     null_run                        =  0;
 
 static int                     fd[MAX_NR_CPUS][MAX_COUNTERS];
@@ -124,17 +127,18 @@ struct stats                      event_res_stats[MAX_COUNTERS][3];
 struct stats                   runtime_nsecs_stats;
 struct stats                   walltime_nsecs_stats;
 struct stats                   runtime_cycles_stats;
+struct stats                   runtime_branches_stats;
 
 #define MATCH_EVENT(t, c, counter)                     \
        (attrs[counter].type == PERF_TYPE_##t &&        \
         attrs[counter].config == PERF_COUNT_##c)
 
 #define ERR_PERF_OPEN \
-"Error: counter %d, sys_perf_counter_open() syscall returned with %d (%s)\n"
+"Error: counter %d, sys_perf_event_open() syscall returned with %d (%s)\n"
 
 static void create_perf_stat_counter(int counter, int pid)
 {
-       struct perf_counter_attr *attr = attrs + counter;
+       struct perf_event_attr *attr = attrs + counter;
 
        if (scale)
                attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
@@ -144,7 +148,7 @@ static void create_perf_stat_counter(int counter, int pid)
                unsigned int cpu;
 
                for (cpu = 0; cpu < nr_cpus; cpu++) {
-                       fd[cpu][counter] = sys_perf_counter_open(attr, -1, cpu, -1, 0);
+                       fd[cpu][counter] = sys_perf_event_open(attr, -1, cpu, -1, 0);
                        if (fd[cpu][counter] < 0 && verbose)
                                fprintf(stderr, ERR_PERF_OPEN, counter,
                                        fd[cpu][counter], strerror(errno));
@@ -154,7 +158,7 @@ static void create_perf_stat_counter(int counter, int pid)
                attr->disabled       = 1;
                attr->enable_on_exec = 1;
 
-               fd[0][counter] = sys_perf_counter_open(attr, pid, -1, -1, 0);
+               fd[0][counter] = sys_perf_event_open(attr, pid, -1, -1, 0);
                if (fd[0][counter] < 0 && verbose)
                        fprintf(stderr, ERR_PERF_OPEN, counter,
                                fd[0][counter], strerror(errno));
@@ -234,6 +238,8 @@ static void read_counter(int counter)
                update_stats(&runtime_nsecs_stats, count[0]);
        if (MATCH_EVENT(HARDWARE, HW_CPU_CYCLES, counter))
                update_stats(&runtime_cycles_stats, count[0]);
+       if (MATCH_EVENT(HARDWARE, HW_BRANCH_INSTRUCTIONS, counter))
+               update_stats(&runtime_branches_stats, count[0]);
 }
 
 static int run_perf_stat(int argc __used, const char **argv)
@@ -285,6 +291,8 @@ static int run_perf_stat(int argc __used, const char **argv)
                exit(-1);
        }
 
+       child_pid = pid;
+
        /*
         * Wait for the child to be ready to exec.
         */
@@ -338,14 +346,33 @@ static void nsec_printout(int counter, double avg)
 
 static void abs_printout(int counter, double avg)
 {
+       double total, ratio = 0.0;
+
        fprintf(stderr, " %14.0f  %-24s", avg, event_name(counter));
 
        if (MATCH_EVENT(HARDWARE, HW_INSTRUCTIONS, counter)) {
-               fprintf(stderr, " # %10.3f IPC  ",
-                               avg / avg_stats(&runtime_cycles_stats));
-       } else {
-               fprintf(stderr, " # %10.3f M/sec",
-                               1000.0 * avg / avg_stats(&runtime_nsecs_stats));
+               total = avg_stats(&runtime_cycles_stats);
+
+               if (total)
+                       ratio = avg / total;
+
+               fprintf(stderr, " # %10.3f IPC  ", ratio);
+       } else if (MATCH_EVENT(HARDWARE, HW_BRANCH_MISSES, counter) &&
+                       runtime_branches_stats.n != 0) {
+               total = avg_stats(&runtime_branches_stats);
+
+               if (total)
+                       ratio = avg * 100 / total;
+
+               fprintf(stderr, " # %10.3f %%    ", ratio);
+
+       } else if (runtime_nsecs_stats.n != 0) {
+               total = avg_stats(&runtime_nsecs_stats);
+
+               if (total)
+                       ratio = 1000.0 * avg / total;
+
+               fprintf(stderr, " # %10.3f M/sec", ratio);
        }
 }
 
@@ -423,6 +450,9 @@ static void skip_signal(int signo)
 
 static void sig_atexit(void)
 {
+       if (child_pid != -1)
+               kill(child_pid, SIGTERM);
+
        if (signr == -1)
                return;