KVM: MMU: invalidate and flush on spte small->large page size change
[safe/jmp/linux-2.6] / tools / perf / builtin-sched.c
index dbf089b..55f3b5d 100644 (file)
@@ -6,14 +6,13 @@
 #include "util/symbol.h"
 #include "util/thread.h"
 #include "util/header.h"
+#include "util/session.h"
 
 #include "util/parse-options.h"
 #include "util/trace-event.h"
 
 #include "util/debug.h"
-#include "util/data_map.h"
 
-#include <sys/types.h>
 #include <sys/prctl.h>
 
 #include <semaphore.h>
 
 static char                    const *input_name = "perf.data";
 
-static unsigned long           total_comm = 0;
-
-static struct perf_header      *header;
-static u64                     sample_type;
-
 static char                    default_sort_order[] = "avg, max, switch, runtime";
-static char                    *sort_order = default_sort_order;
+static const char              *sort_order = default_sort_order;
 
 static int                     profile_cpu = -1;
 
-static char                    *cwd;
-static int                     cwdlen;
-
 #define PR_SET_NAME            15               /* Set process name */
 #define MAX_CPUS               4096
 
@@ -77,10 +68,10 @@ enum sched_event_type {
 
 struct sched_atom {
        enum sched_event_type   type;
+       int                     specific_wait;
        u64                     timestamp;
        u64                     duration;
        unsigned long           nr;
-       int                     specific_wait;
        sem_t                   *wait_sem;
        struct task_desc        *wakee;
 };
@@ -114,7 +105,7 @@ static u64                  sum_runtime;
 static u64                     sum_fluct;
 static u64                     run_avg;
 
-static unsigned long           replay_repeat = 10;
+static unsigned int            replay_repeat = 10;
 static unsigned long           nr_timestamps;
 static unsigned long           nr_unordered_timestamps;
 static unsigned long           nr_state_machine_bugs;
@@ -146,6 +137,7 @@ struct work_atoms {
        struct thread           *thread;
        struct rb_node          node;
        u64                     max_lat;
+       u64                     max_lat_at;
        u64                     total_lat;
        u64                     nb_atoms;
        u64                     total_runtime;
@@ -225,7 +217,7 @@ static void calibrate_sleep_measurement_overhead(void)
 static struct sched_atom *
 get_new_event(struct task_desc *task, u64 timestamp)
 {
-       struct sched_atom *event = calloc(1, sizeof(*event));
+       struct sched_atom *event = zalloc(sizeof(*event));
        unsigned long idx = task->nr_events;
        size_t size;
 
@@ -293,7 +285,7 @@ add_sched_event_wakeup(struct task_desc *task, u64 timestamp,
                return;
        }
 
-       wakee_event->wait_sem = calloc(1, sizeof(*wakee_event->wait_sem));
+       wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
        sem_init(wakee_event->wait_sem, 0, 0);
        wakee_event->specific_wait = 1;
        event->wait_sem = wakee_event->wait_sem;
@@ -323,7 +315,7 @@ static struct task_desc *register_pid(unsigned long pid, const char *comm)
        if (task)
                return task;
 
-       task = calloc(1, sizeof(*task));
+       task = zalloc(sizeof(*task));
        task->pid = pid;
        task->nr = nr_tasks;
        strcpy(task->comm, comm);
@@ -419,34 +411,33 @@ static u64 get_cpu_usage_nsec_parent(void)
        return sum;
 }
 
-static u64 get_cpu_usage_nsec_self(void)
+static int self_open_counters(void)
 {
-       char filename [] = "/proc/1234567890/sched";
-       unsigned long msecs, nsecs;
-       char *line = NULL;
-       u64 total = 0;
-       size_t len = 0;
-       ssize_t chars;
-       FILE *file;
-       int ret;
+       struct perf_event_attr attr;
+       int fd;
 
-       sprintf(filename, "/proc/%d/sched", getpid());
-       file = fopen(filename, "r");
-       BUG_ON(!file);
+       memset(&attr, 0, sizeof(attr));
 
-       while ((chars = getline(&line, &len, file)) != -1) {
-               ret = sscanf(line, "se.sum_exec_runtime : %ld.%06ld\n",
-                       &msecs, &nsecs);
-               if (ret == 2) {
-                       total = msecs*1e6 + nsecs;
-                       break;
-               }
-       }
-       if (line)
-               free(line);
-       fclose(file);
+       attr.type = PERF_TYPE_SOFTWARE;
+       attr.config = PERF_COUNT_SW_TASK_CLOCK;
 
-       return total;
+       fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
+
+       if (fd < 0)
+               die("Error: sys_perf_event_open() syscall returned"
+                   "with %d (%s)\n", fd, strerror(errno));
+       return fd;
+}
+
+static u64 get_cpu_usage_nsec_self(int fd)
+{
+       u64 runtime;
+       int ret;
+
+       ret = read(fd, &runtime, sizeof(runtime));
+       BUG_ON(ret != sizeof(runtime));
+
+       return runtime;
 }
 
 static void *thread_func(void *ctx)
@@ -455,9 +446,11 @@ static void *thread_func(void *ctx)
        u64 cpu_usage_0, cpu_usage_1;
        unsigned long i, ret;
        char comm2[22];
+       int fd;
 
        sprintf(comm2, ":%s", this_task->comm);
        prctl(PR_SET_NAME, comm2);
+       fd = self_open_counters();
 
 again:
        ret = sem_post(&this_task->ready_for_work);
@@ -467,16 +460,15 @@ again:
        ret = pthread_mutex_unlock(&start_work_mutex);
        BUG_ON(ret);
 
-       cpu_usage_0 = get_cpu_usage_nsec_self();
+       cpu_usage_0 = get_cpu_usage_nsec_self(fd);
 
        for (i = 0; i < this_task->nr_events; i++) {
                this_task->curr_event = i;
                process_sched_event(this_task, this_task->atoms[i]);
        }
 
-       cpu_usage_1 = get_cpu_usage_nsec_self();
+       cpu_usage_1 = get_cpu_usage_nsec_self(fd);
        this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
-
        ret = sem_post(&this_task->work_done_sem);
        BUG_ON(ret);
 
@@ -633,32 +625,6 @@ static void test_calibrations(void)
        printf("the sleep test took %Ld nsecs\n", T1-T0);
 }
 
-static int
-process_comm_event(event_t *event, unsigned long offset, unsigned long head)
-{
-       struct thread *thread = threads__findnew(event->comm.tid);
-
-       dump_printf("%p [%p]: perf_event_comm: %s:%d\n",
-               (void *)(offset + head),
-               (void *)(long)(event->header.size),
-               event->comm.comm, event->comm.pid);
-
-       if (thread == NULL ||
-           thread__set_comm(thread, event->comm.comm)) {
-               dump_printf("problem processing perf_event_comm, skipping event.\n");
-               return -1;
-       }
-       total_comm++;
-
-       return 0;
-}
-
-
-struct raw_event_sample {
-       u32 size;
-       char data[0];
-};
-
 #define FILL_FIELD(ptr, field, event, data)    \
        ptr.field = (typeof(ptr.field)) raw_field_value(event, #field, data)
 
@@ -762,18 +728,21 @@ struct trace_migrate_task_event {
 
 struct trace_sched_handler {
        void (*switch_event)(struct trace_switch_event *,
+                            struct perf_session *,
                             struct event *,
                             int cpu,
                             u64 timestamp,
                             struct thread *thread);
 
        void (*runtime_event)(struct trace_runtime_event *,
+                             struct perf_session *,
                              struct event *,
                              int cpu,
                              u64 timestamp,
                              struct thread *thread);
 
        void (*wakeup_event)(struct trace_wakeup_event *,
+                            struct perf_session *,
                             struct event *,
                             int cpu,
                             u64 timestamp,
@@ -786,6 +755,7 @@ struct trace_sched_handler {
                           struct thread *thread);
 
        void (*migrate_task_event)(struct trace_migrate_task_event *,
+                          struct perf_session *session,
                           struct event *,
                           int cpu,
                           u64 timestamp,
@@ -795,6 +765,7 @@ struct trace_sched_handler {
 
 static void
 replay_wakeup_event(struct trace_wakeup_event *wakeup_event,
+                   struct perf_session *session __used,
                    struct event *event,
                    int cpu __used,
                    u64 timestamp __used,
@@ -821,6 +792,7 @@ static u64 cpu_last_switched[MAX_CPUS];
 
 static void
 replay_switch_event(struct trace_switch_event *switch_event,
+                   struct perf_session *session __used,
                    struct event *event,
                    int cpu,
                    u64 timestamp,
@@ -962,9 +934,7 @@ __thread_latency_insert(struct rb_root *root, struct work_atoms *data,
 
 static void thread_atoms_insert(struct thread *thread)
 {
-       struct work_atoms *atoms;
-
-       atoms = calloc(sizeof(*atoms), 1);
+       struct work_atoms *atoms = zalloc(sizeof(*atoms));
        if (!atoms)
                die("No memory");
 
@@ -996,9 +966,7 @@ add_sched_out_event(struct work_atoms *atoms,
                    char run_state,
                    u64 timestamp)
 {
-       struct work_atom *atom;
-
-       atom = calloc(sizeof(*atom), 1);
+       struct work_atom *atom = zalloc(sizeof(*atom));
        if (!atom)
                die("Non memory");
 
@@ -1049,13 +1017,16 @@ add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
 
        delta = atom->sched_in_time - atom->wake_up_time;
        atoms->total_lat += delta;
-       if (delta > atoms->max_lat)
+       if (delta > atoms->max_lat) {
                atoms->max_lat = delta;
+               atoms->max_lat_at = timestamp;
+       }
        atoms->nb_atoms++;
 }
 
 static void
 latency_switch_event(struct trace_switch_event *switch_event,
+                    struct perf_session *session,
                     struct event *event __used,
                     int cpu,
                     u64 timestamp,
@@ -1079,8 +1050,8 @@ latency_switch_event(struct trace_switch_event *switch_event,
                die("hm, delta: %Ld < 0 ?\n", delta);
 
 
-       sched_out = threads__findnew(switch_event->prev_pid);
-       sched_in = threads__findnew(switch_event->next_pid);
+       sched_out = perf_session__findnew(session, switch_event->prev_pid);
+       sched_in = perf_session__findnew(session, switch_event->next_pid);
 
        out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
        if (!out_events) {
@@ -1108,12 +1079,13 @@ latency_switch_event(struct trace_switch_event *switch_event,
 
 static void
 latency_runtime_event(struct trace_runtime_event *runtime_event,
+                    struct perf_session *session,
                     struct event *event __used,
                     int cpu,
                     u64 timestamp,
                     struct thread *this_thread __used)
 {
-       struct thread *thread = threads__findnew(runtime_event->pid);
+       struct thread *thread = perf_session__findnew(session, runtime_event->pid);
        struct work_atoms *atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
 
        BUG_ON(cpu >= MAX_CPUS || cpu < 0);
@@ -1130,6 +1102,7 @@ latency_runtime_event(struct trace_runtime_event *runtime_event,
 
 static void
 latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
+                    struct perf_session *session,
                     struct event *__event __used,
                     int cpu __used,
                     u64 timestamp,
@@ -1143,7 +1116,7 @@ latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
        if (!wakeup_event->success)
                return;
 
-       wakee = threads__findnew(wakeup_event->pid);
+       wakee = perf_session__findnew(session, wakeup_event->pid);
        atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
        if (!atoms) {
                thread_atoms_insert(wakee);
@@ -1177,6 +1150,7 @@ latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
 
 static void
 latency_migrate_task_event(struct trace_migrate_task_event *migrate_task_event,
+                    struct perf_session *session,
                     struct event *__event __used,
                     int cpu __used,
                     u64 timestamp,
@@ -1192,7 +1166,7 @@ latency_migrate_task_event(struct trace_migrate_task_event *migrate_task_event,
        if (profile_cpu == -1)
                return;
 
-       migrant = threads__findnew(migrate_task_event->pid);
+       migrant = perf_session__findnew(session, migrate_task_event->pid);
        atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
        if (!atoms) {
                thread_atoms_insert(migrant);
@@ -1246,10 +1220,11 @@ static void output_lat_thread(struct work_atoms *work_list)
 
        avg = work_list->total_lat / work_list->nb_atoms;
 
-       printf("|%11.3f ms |%9llu | avg:%9.3f ms | max:%9.3f ms |\n",
+       printf("|%11.3f ms |%9llu | avg:%9.3f ms | max:%9.3f ms | max at: %9.6f s\n",
              (double)work_list->total_runtime / 1e6,
                 work_list->nb_atoms, (double)avg / 1e6,
-                (double)work_list->max_lat / 1e6);
+                (double)work_list->max_lat / 1e6,
+                (double)work_list->max_lat_at / 1e9);
 }
 
 static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
@@ -1386,7 +1361,7 @@ static void sort_lat(void)
 static struct trace_sched_handler *trace_handler;
 
 static void
-process_sched_wakeup_event(struct raw_event_sample *raw,
+process_sched_wakeup_event(void *data, struct perf_session *session,
                           struct event *event,
                           int cpu __used,
                           u64 timestamp __used,
@@ -1394,16 +1369,17 @@ process_sched_wakeup_event(struct raw_event_sample *raw,
 {
        struct trace_wakeup_event wakeup_event;
 
-       FILL_COMMON_FIELDS(wakeup_event, event, raw->data);
+       FILL_COMMON_FIELDS(wakeup_event, event, data);
 
-       FILL_ARRAY(wakeup_event, comm, event, raw->data);
-       FILL_FIELD(wakeup_event, pid, event, raw->data);
-       FILL_FIELD(wakeup_event, prio, event, raw->data);
-       FILL_FIELD(wakeup_event, success, event, raw->data);
-       FILL_FIELD(wakeup_event, cpu, event, raw->data);
+       FILL_ARRAY(wakeup_event, comm, event, data);
+       FILL_FIELD(wakeup_event, pid, event, data);
+       FILL_FIELD(wakeup_event, prio, event, data);
+       FILL_FIELD(wakeup_event, success, event, data);
+       FILL_FIELD(wakeup_event, cpu, event, data);
 
        if (trace_handler->wakeup_event)
-               trace_handler->wakeup_event(&wakeup_event, event, cpu, timestamp, thread);
+               trace_handler->wakeup_event(&wakeup_event, session, event,
+                                           cpu, timestamp, thread);
 }
 
 /*
@@ -1421,6 +1397,7 @@ static char next_shortname2 = '0';
 
 static void
 map_switch_event(struct trace_switch_event *switch_event,
+                struct perf_session *session,
                 struct event *event __used,
                 int this_cpu,
                 u64 timestamp,
@@ -1448,8 +1425,8 @@ map_switch_event(struct trace_switch_event *switch_event,
                die("hm, delta: %Ld < 0 ?\n", delta);
 
 
-       sched_out = threads__findnew(switch_event->prev_pid);
-       sched_in = threads__findnew(switch_event->next_pid);
+       sched_out = perf_session__findnew(session, switch_event->prev_pid);
+       sched_in = perf_session__findnew(session, switch_event->next_pid);
 
        curr_thread[this_cpu] = sched_in;
 
@@ -1499,7 +1476,7 @@ map_switch_event(struct trace_switch_event *switch_event,
 
 
 static void
-process_sched_switch_event(struct raw_event_sample *raw,
+process_sched_switch_event(void *data, struct perf_session *session,
                           struct event *event,
                           int this_cpu,
                           u64 timestamp __used,
@@ -1507,15 +1484,15 @@ process_sched_switch_event(struct raw_event_sample *raw,
 {
        struct trace_switch_event switch_event;
 
-       FILL_COMMON_FIELDS(switch_event, event, raw->data);
+       FILL_COMMON_FIELDS(switch_event, event, data);
 
-       FILL_ARRAY(switch_event, prev_comm, event, raw->data);
-       FILL_FIELD(switch_event, prev_pid, event, raw->data);
-       FILL_FIELD(switch_event, prev_prio, event, raw->data);
-       FILL_FIELD(switch_event, prev_state, event, raw->data);
-       FILL_ARRAY(switch_event, next_comm, event, raw->data);
-       FILL_FIELD(switch_event, next_pid, event, raw->data);
-       FILL_FIELD(switch_event, next_prio, event, raw->data);
+       FILL_ARRAY(switch_event, prev_comm, event, data);
+       FILL_FIELD(switch_event, prev_pid, event, data);
+       FILL_FIELD(switch_event, prev_prio, event, data);
+       FILL_FIELD(switch_event, prev_state, event, data);
+       FILL_ARRAY(switch_event, next_comm, event, data);
+       FILL_FIELD(switch_event, next_pid, event, data);
+       FILL_FIELD(switch_event, next_prio, event, data);
 
        if (curr_pid[this_cpu] != (u32)-1) {
                /*
@@ -1526,13 +1503,14 @@ process_sched_switch_event(struct raw_event_sample *raw,
                        nr_context_switch_bugs++;
        }
        if (trace_handler->switch_event)
-               trace_handler->switch_event(&switch_event, event, this_cpu, timestamp, thread);
+               trace_handler->switch_event(&switch_event, session, event,
+                                           this_cpu, timestamp, thread);
 
        curr_pid[this_cpu] = switch_event.next_pid;
 }
 
 static void
-process_sched_runtime_event(struct raw_event_sample *raw,
+process_sched_runtime_event(void *data, struct perf_session *session,
                           struct event *event,
                           int cpu __used,
                           u64 timestamp __used,
@@ -1540,17 +1518,17 @@ process_sched_runtime_event(struct raw_event_sample *raw,
 {
        struct trace_runtime_event runtime_event;
 
-       FILL_ARRAY(runtime_event, comm, event, raw->data);
-       FILL_FIELD(runtime_event, pid, event, raw->data);
-       FILL_FIELD(runtime_event, runtime, event, raw->data);
-       FILL_FIELD(runtime_event, vruntime, event, raw->data);
+       FILL_ARRAY(runtime_event, comm, event, data);
+       FILL_FIELD(runtime_event, pid, event, data);
+       FILL_FIELD(runtime_event, runtime, event, data);
+       FILL_FIELD(runtime_event, vruntime, event, data);
 
        if (trace_handler->runtime_event)
-               trace_handler->runtime_event(&runtime_event, event, cpu, timestamp, thread);
+               trace_handler->runtime_event(&runtime_event, session, event, cpu, timestamp, thread);
 }
 
 static void
-process_sched_fork_event(struct raw_event_sample *raw,
+process_sched_fork_event(void *data,
                         struct event *event,
                         int cpu __used,
                         u64 timestamp __used,
@@ -1558,15 +1536,16 @@ process_sched_fork_event(struct raw_event_sample *raw,
 {
        struct trace_fork_event fork_event;
 
-       FILL_COMMON_FIELDS(fork_event, event, raw->data);
+       FILL_COMMON_FIELDS(fork_event, event, data);
 
-       FILL_ARRAY(fork_event, parent_comm, event, raw->data);
-       FILL_FIELD(fork_event, parent_pid, event, raw->data);
-       FILL_ARRAY(fork_event, child_comm, event, raw->data);
-       FILL_FIELD(fork_event, child_pid, event, raw->data);
+       FILL_ARRAY(fork_event, parent_comm, event, data);
+       FILL_FIELD(fork_event, parent_pid, event, data);
+       FILL_ARRAY(fork_event, child_comm, event, data);
+       FILL_FIELD(fork_event, child_pid, event, data);
 
        if (trace_handler->fork_event)
-               trace_handler->fork_event(&fork_event, event, cpu, timestamp, thread);
+               trace_handler->fork_event(&fork_event, event,
+                                         cpu, timestamp, thread);
 }
 
 static void
@@ -1580,7 +1559,7 @@ process_sched_exit_event(struct event *event,
 }
 
 static void
-process_sched_migrate_task_event(struct raw_event_sample *raw,
+process_sched_migrate_task_event(void *data, struct perf_session *session,
                           struct event *event,
                           int cpu __used,
                           u64 timestamp __used,
@@ -1588,83 +1567,64 @@ process_sched_migrate_task_event(struct raw_event_sample *raw,
 {
        struct trace_migrate_task_event migrate_task_event;
 
-       FILL_COMMON_FIELDS(migrate_task_event, event, raw->data);
+       FILL_COMMON_FIELDS(migrate_task_event, event, data);
 
-       FILL_ARRAY(migrate_task_event, comm, event, raw->data);
-       FILL_FIELD(migrate_task_event, pid, event, raw->data);
-       FILL_FIELD(migrate_task_event, prio, event, raw->data);
-       FILL_FIELD(migrate_task_event, cpu, event, raw->data);
+       FILL_ARRAY(migrate_task_event, comm, event, data);
+       FILL_FIELD(migrate_task_event, pid, event, data);
+       FILL_FIELD(migrate_task_event, prio, event, data);
+       FILL_FIELD(migrate_task_event, cpu, event, data);
 
        if (trace_handler->migrate_task_event)
-               trace_handler->migrate_task_event(&migrate_task_event, event, cpu, timestamp, thread);
+               trace_handler->migrate_task_event(&migrate_task_event, session,
+                                                event, cpu, timestamp, thread);
 }
 
 static void
-process_raw_event(event_t *raw_event __used, void *more_data,
-                 int cpu, u64 timestamp, struct thread *thread)
+process_raw_event(event_t *raw_event __used, struct perf_session *session,
+                 void *data, int cpu, u64 timestamp, struct thread *thread)
 {
-       struct raw_event_sample *raw = more_data;
        struct event *event;
        int type;
 
-       type = trace_parse_common_type(raw->data);
+
+       type = trace_parse_common_type(data);
        event = trace_find_event(type);
 
        if (!strcmp(event->name, "sched_switch"))
-               process_sched_switch_event(raw, event, cpu, timestamp, thread);
+               process_sched_switch_event(data, session, event, cpu, timestamp, thread);
        if (!strcmp(event->name, "sched_stat_runtime"))
-               process_sched_runtime_event(raw, event, cpu, timestamp, thread);
+               process_sched_runtime_event(data, session, event, cpu, timestamp, thread);
        if (!strcmp(event->name, "sched_wakeup"))
-               process_sched_wakeup_event(raw, event, cpu, timestamp, thread);
+               process_sched_wakeup_event(data, session, event, cpu, timestamp, thread);
        if (!strcmp(event->name, "sched_wakeup_new"))
-               process_sched_wakeup_event(raw, event, cpu, timestamp, thread);
+               process_sched_wakeup_event(data, session, event, cpu, timestamp, thread);
        if (!strcmp(event->name, "sched_process_fork"))
-               process_sched_fork_event(raw, event, cpu, timestamp, thread);
+               process_sched_fork_event(data, event, cpu, timestamp, thread);
        if (!strcmp(event->name, "sched_process_exit"))
                process_sched_exit_event(event, cpu, timestamp, thread);
        if (!strcmp(event->name, "sched_migrate_task"))
-               process_sched_migrate_task_event(raw, event, cpu, timestamp, thread);
+               process_sched_migrate_task_event(data, session, event, cpu, timestamp, thread);
 }
 
-static int
-process_sample_event(event_t *event, unsigned long offset, unsigned long head)
+static int process_sample_event(event_t *event, struct perf_session *session)
 {
+       struct sample_data data;
        struct thread *thread;
-       u64 ip = event->ip.ip;
-       u64 timestamp = -1;
-       u32 cpu = -1;
-       u64 period = 1;
-       void *more_data = event->ip.__more_data;
 
-       if (!(sample_type & PERF_SAMPLE_RAW))
+       if (!(session->sample_type & PERF_SAMPLE_RAW))
                return 0;
 
-       thread = threads__findnew(event->ip.pid);
+       memset(&data, 0, sizeof(data));
+       data.time = -1;
+       data.cpu = -1;
+       data.period = -1;
 
-       if (sample_type & PERF_SAMPLE_TIME) {
-               timestamp = *(u64 *)more_data;
-               more_data += sizeof(u64);
-       }
-
-       if (sample_type & PERF_SAMPLE_CPU) {
-               cpu = *(u32 *)more_data;
-               more_data += sizeof(u32);
-               more_data += sizeof(u32); /* reserved */
-       }
-
-       if (sample_type & PERF_SAMPLE_PERIOD) {
-               period = *(u64 *)more_data;
-               more_data += sizeof(u64);
-       }
+       event__parse_sample(event, session->sample_type, &data);
 
-       dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
-               (void *)(offset + head),
-               (void *)(long)(event->header.size),
-               event->header.misc,
-               event->ip.pid, event->ip.tid,
-               (void *)(long)ip,
-               (long long)period);
+       dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
+                   data.pid, data.tid, data.ip, data.period);
 
+       thread = perf_session__findnew(session, data.pid);
        if (thread == NULL) {
                pr_debug("problem processing %d event, skipping it.\n",
                         event->header.type);
@@ -1673,53 +1633,38 @@ process_sample_event(event_t *event, unsigned long offset, unsigned long head)
 
        dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
 
-       if (profile_cpu != -1 && profile_cpu != (int) cpu)
+       if (profile_cpu != -1 && profile_cpu != (int)data.cpu)
                return 0;
 
-       process_raw_event(event, more_data, cpu, timestamp, thread);
-
-       return 0;
-}
-
-static int
-process_lost_event(event_t *event __used,
-                  unsigned long offset __used,
-                  unsigned long head __used)
-{
-       nr_lost_chunks++;
-       nr_lost_events += event->lost.lost;
-
-       return 0;
-}
-
-static int sample_type_check(u64 type)
-{
-       sample_type = type;
-
-       if (!(sample_type & PERF_SAMPLE_RAW)) {
-               fprintf(stderr,
-                       "No trace sample to read. Did you call perf record "
-                       "without -R?");
-               return -1;
-       }
+       process_raw_event(event, session, data.raw_data, data.cpu, data.time, thread);
 
        return 0;
 }
 
-static struct perf_file_handler file_handler = {
-       .process_sample_event   = process_sample_event,
-       .process_comm_event     = process_comm_event,
-       .process_lost_event     = process_lost_event,
-       .sample_type_check      = sample_type_check,
+static struct perf_event_ops event_ops = {
+       .sample                 = process_sample_event,
+       .comm                   = event__process_comm,
+       .lost                   = event__process_lost,
+       .fork                   = event__process_task,
+       .ordered_samples        = true,
 };
 
 static int read_events(void)
 {
-       register_idle_thread();
-       register_perf_file_handler(&file_handler);
+       int err = -EINVAL;
+       struct perf_session *session = perf_session__new(input_name, O_RDONLY, 0, false);
+       if (session == NULL)
+               return -ENOMEM;
+
+       if (perf_session__has_traces(session, "record -R")) {
+               err = perf_session__process_events(session, &event_ops);
+               nr_events      = session->hists.stats.nr_events[0];
+               nr_lost_events = session->hists.stats.total_lost;
+               nr_lost_chunks = session->hists.stats.nr_events[PERF_RECORD_LOST];
+       }
 
-       return mmap_dispatch_perf_file(&header, input_name, 0, 0,
-                                      &cwdlen, &cwd);
+       perf_session__delete(session);
+       return err;
 }
 
 static void print_bad_events(void)
@@ -1760,9 +1705,9 @@ static void __cmd_lat(void)
        read_events();
        sort_lat();
 
-       printf("\n -----------------------------------------------------------------------------------------\n");
-       printf("  Task                  |   Runtime ms  | Switches | Average delay ms | Maximum delay ms |\n");
-       printf(" -----------------------------------------------------------------------------------------\n");
+       printf("\n ---------------------------------------------------------------------------------------------------------------\n");
+       printf("  Task                  |   Runtime ms  | Switches | Average delay ms | Maximum delay ms | Maximum delay at     |\n");
+       printf(" ---------------------------------------------------------------------------------------------------------------\n");
 
        next = rb_first(&sorted_atom_root);
 
@@ -1842,7 +1787,7 @@ static const char * const sched_usage[] = {
 static const struct option sched_options[] = {
        OPT_STRING('i', "input", &input_name, "file",
                    "input file name"),
-       OPT_BOOLEAN('v', "verbose", &verbose,
+       OPT_INCR('v', "verbose", &verbose,
                    "be more verbose (show symbol address, etc)"),
        OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
                    "dump raw trace in ASCII"),
@@ -1857,7 +1802,7 @@ static const char * const latency_usage[] = {
 static const struct option latency_options[] = {
        OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
                   "sort by key(s): runtime, switch, avg, max"),
-       OPT_BOOLEAN('v', "verbose", &verbose,
+       OPT_INCR('v', "verbose", &verbose,
                    "be more verbose (show symbol address, etc)"),
        OPT_INTEGER('C', "CPU", &profile_cpu,
                    "CPU to profile on"),
@@ -1872,9 +1817,9 @@ static const char * const replay_usage[] = {
 };
 
 static const struct option replay_options[] = {
-       OPT_INTEGER('r', "repeat", &replay_repeat,
-                   "repeat the workload replay N times (-1: infinite)"),
-       OPT_BOOLEAN('v', "verbose", &verbose,
+       OPT_UINTEGER('r', "repeat", &replay_repeat,
+                    "repeat the workload replay N times (-1: infinite)"),
+       OPT_INCR('v', "verbose", &verbose,
                    "be more verbose (show symbol address, etc)"),
        OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
                    "dump raw trace in ASCII"),
@@ -1902,7 +1847,6 @@ static const char *record_args[] = {
        "record",
        "-a",
        "-R",
-       "-M",
        "-f",
        "-m", "1024",
        "-c", "1",
@@ -1938,13 +1882,18 @@ static int __cmd_record(int argc, const char **argv)
 
 int cmd_sched(int argc, const char **argv, const char *prefix __used)
 {
-       symbol__init(0);
-
        argc = parse_options(argc, argv, sched_options, sched_usage,
                             PARSE_OPT_STOP_AT_NON_OPTION);
        if (!argc)
                usage_with_options(sched_usage, sched_options);
 
+       /*
+        * Aliased to 'perf trace' for now:
+        */
+       if (!strcmp(argv[0], "trace"))
+               return cmd_trace(argc, argv, prefix);
+
+       symbol__init();
        if (!strncmp(argv[0], "rec", 3)) {
                return __cmd_record(argc, argv);
        } else if (!strncmp(argv[0], "lat", 3)) {
@@ -1968,11 +1917,6 @@ int cmd_sched(int argc, const char **argv, const char *prefix __used)
                                usage_with_options(replay_usage, replay_options);
                }
                __cmd_replay();
-       } else if (!strcmp(argv[0], "trace")) {
-               /*
-                * Aliased to 'perf trace' for now:
-                */
-               return cmd_trace(argc, argv, prefix);
        } else {
                usage_with_options(sched_usage, sched_options);
        }