- patches.suse/slab-handle-memoryless-nodes-v2a.patch: Refresh.
[linux-flexiantxendom0-3.2.10.git] / tools / perf / builtin-sched.c
index ce2d5be..80209df 100644 (file)
@@ -6,13 +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 <sys/types.h>
 #include <sys/prctl.h>
 
 #include <semaphore.h>
 #include <math.h>
 
 static char                    const *input_name = "perf.data";
-static int                     input;
-static unsigned long           page_size;
-static unsigned long           mmap_window = 32;
-
-static unsigned long           total_comm = 0;
-
-static struct rb_root          threads;
-static struct thread           *last_match;
-
-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 int                     profile_cpu = -1;
+
 #define PR_SET_NAME            15               /* Set process name */
 #define MAX_CPUS               4096
 
-#define BUG_ON(x)              assert(!(x))
-
 static u64                     run_measurement_overhead;
 static u64                     sleep_measurement_overhead;
 
@@ -74,6 +63,7 @@ enum sched_event_type {
        SCHED_EVENT_RUN,
        SCHED_EVENT_SLEEP,
        SCHED_EVENT_WAKEUP,
+       SCHED_EVENT_MIGRATION,
 };
 
 struct sched_atom {
@@ -147,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;
@@ -226,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;
 
@@ -294,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;
@@ -324,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);
@@ -398,6 +389,8 @@ process_sched_event(struct task_desc *this_task __used, struct sched_atom *atom)
                                ret = sem_post(atom->wait_sem);
                        BUG_ON(ret);
                        break;
+               case SCHED_EVENT_MIGRATION:
+                       break;
                default:
                        BUG_ON(1);
        }
@@ -418,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;
+
+       fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
 
-       return total;
+       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)
@@ -454,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);
@@ -466,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);
 
@@ -632,34 +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;
-
-       thread = threads__findnew(event->comm.pid, &threads, &last_match);
-
-       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)
 
@@ -745,20 +710,39 @@ struct trace_fork_event {
        u32 child_pid;
 };
 
+struct trace_migrate_task_event {
+       u32 size;
+
+       u16 common_type;
+       u8 common_flags;
+       u8 common_preempt_count;
+       u32 common_pid;
+       u32 common_tgid;
+
+       char comm[16];
+       u32 pid;
+
+       u32 prio;
+       u32 cpu;
+};
+
 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,
@@ -769,11 +753,19 @@ struct trace_sched_handler {
                           int cpu,
                           u64 timestamp,
                           struct thread *thread);
+
+       void (*migrate_task_event)(struct trace_migrate_task_event *,
+                          struct perf_session *session,
+                          struct event *,
+                          int cpu,
+                          u64 timestamp,
+                          struct thread *thread);
 };
 
 
 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,
@@ -800,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,
@@ -941,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");
 
@@ -975,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");
 
@@ -1028,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,
@@ -1058,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, &threads, &last_match);
-       sched_in = threads__findnew(switch_event->next_pid, &threads, &last_match);
+       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) {
@@ -1087,18 +1079,16 @@ 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 work_atoms *atoms;
-       struct thread *thread;
+       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);
-
-       thread = threads__findnew(runtime_event->pid, &threads, &last_match);
-       atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
        if (!atoms) {
                thread_atoms_insert(thread);
                atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
@@ -1112,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,
@@ -1125,7 +1116,7 @@ latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
        if (!wakeup_event->success)
                return;
 
-       wakee = threads__findnew(wakeup_event->pid, &threads, &last_match);
+       wakee = perf_session__findnew(session, wakeup_event->pid);
        atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
        if (!atoms) {
                thread_atoms_insert(wakee);
@@ -1139,7 +1130,12 @@ latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
 
        atom = list_entry(atoms->work_list.prev, struct work_atom, list);
 
-       if (atom->state != THREAD_SLEEPING)
+       /*
+        * You WILL be missing events if you've recorded only
+        * one CPU, or are only looking at only one, so don't
+        * make useless noise.
+        */
+       if (profile_cpu == -1 && atom->state != THREAD_SLEEPING)
                nr_state_machine_bugs++;
 
        nr_timestamps++;
@@ -1152,11 +1148,52 @@ latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
        atom->wake_up_time = timestamp;
 }
 
+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,
+                    struct thread *thread __used)
+{
+       struct work_atoms *atoms;
+       struct work_atom *atom;
+       struct thread *migrant;
+
+       /*
+        * Only need to worry about migration when profiling one CPU.
+        */
+       if (profile_cpu == -1)
+               return;
+
+       migrant = perf_session__findnew(session, migrate_task_event->pid);
+       atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
+       if (!atoms) {
+               thread_atoms_insert(migrant);
+               register_pid(migrant->pid, migrant->comm);
+               atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
+               if (!atoms)
+                       die("migration-event: Internal tree error");
+               add_sched_out_event(atoms, 'R', timestamp);
+       }
+
+       BUG_ON(list_empty(&atoms->work_list));
+
+       atom = list_entry(atoms->work_list.prev, struct work_atom, list);
+       atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
+
+       nr_timestamps++;
+
+       if (atom->sched_out_time > timestamp)
+               nr_unordered_timestamps++;
+}
+
 static struct trace_sched_handler lat_ops  = {
        .wakeup_event           = latency_wakeup_event,
        .switch_event           = latency_switch_event,
        .runtime_event          = latency_runtime_event,
        .fork_event             = latency_fork_event,
+       .migrate_task_event     = latency_migrate_task_event,
 };
 
 static void output_lat_thread(struct work_atoms *work_list)
@@ -1183,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)
@@ -1323,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,
@@ -1331,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);
 }
 
 /*
@@ -1358,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,
@@ -1385,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, &threads, &last_match);
-       sched_in = threads__findnew(switch_event->next_pid, &threads, &last_match);
+       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;
 
@@ -1436,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,
@@ -1444,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) {
                /*
@@ -1463,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,
@@ -1477,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,
@@ -1495,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
@@ -1517,233 +1559,129 @@ process_sched_exit_event(struct event *event,
 }
 
 static void
-process_raw_event(event_t *raw_event __used, void *more_data,
-                 int cpu, u64 timestamp, struct thread *thread)
+process_sched_migrate_task_event(void *data, struct perf_session *session,
+                          struct event *event,
+                          int cpu __used,
+                          u64 timestamp __used,
+                          struct thread *thread __used)
+{
+       struct trace_migrate_task_event migrate_task_event;
+
+       FILL_COMMON_FIELDS(migrate_task_event, event, 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, session,
+                                                event, cpu, timestamp, thread);
+}
+
+static void
+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(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)
 {
-       char level;
-       int show = 0;
-       struct dso *dso = NULL;
+       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;
-       int cpumode;
-
-       thread = threads__findnew(event->ip.pid, &threads, &last_match);
-
-       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 (!(session->sample_type & PERF_SAMPLE_RAW))
+               return 0;
 
-       if (sample_type & PERF_SAMPLE_PERIOD) {
-               period = *(u64 *)more_data;
-               more_data += sizeof(u64);
-       }
+       memset(&data, 0, sizeof(data));
+       data.time = -1;
+       data.cpu = -1;
+       data.period = -1;
 
-       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);
+       event__parse_sample(event, session->sample_type, &data);
 
-       dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
+       dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
+               event->header.misc,
+               data.pid, data.tid,
+               (void *)(long)data.ip,
+               (long long)data.period);
 
+       thread = perf_session__findnew(session, data.pid);
        if (thread == NULL) {
-               eprintf("problem processing %d event, skipping it.\n",
-                       event->header.type);
+               pr_debug("problem processing %d event, skipping it.\n",
+                        event->header.type);
                return -1;
        }
 
-       cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
-
-       if (cpumode == PERF_RECORD_MISC_KERNEL) {
-               show = SHOW_KERNEL;
-               level = 'k';
-
-               dso = kernel_dso;
-
-               dump_printf(" ...... dso: %s\n", dso->name);
-
-       } else if (cpumode == PERF_RECORD_MISC_USER) {
-
-               show = SHOW_USER;
-               level = '.';
-
-       } else {
-               show = SHOW_HV;
-               level = 'H';
-
-               dso = hypervisor_dso;
+       dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
 
-               dump_printf(" ...... dso: [hypervisor]\n");
-       }
+       if (profile_cpu != -1 && profile_cpu != (int)data.cpu)
+               return 0;
 
-       if (sample_type & PERF_SAMPLE_RAW)
-               process_raw_event(event, more_data, cpu, timestamp, thread);
+       process_raw_event(event, session, data.raw_data, data.cpu, data.time, thread);
 
        return 0;
 }
 
-static int
-process_event(event_t *event, unsigned long offset, unsigned long head)
+static int process_lost_event(event_t *event __used,
+                             struct perf_session *session __used)
 {
-       trace_event(event);
-
-       nr_events++;
-       switch (event->header.type) {
-       case PERF_RECORD_MMAP:
-               return 0;
-       case PERF_RECORD_LOST:
-               nr_lost_chunks++;
-               nr_lost_events += event->lost.lost;
-               return 0;
+       nr_lost_chunks++;
+       nr_lost_events += event->lost.lost;
 
-       case PERF_RECORD_COMM:
-               return process_comm_event(event, offset, head);
-
-       case PERF_RECORD_EXIT ... PERF_RECORD_READ:
-               return 0;
-
-       case PERF_RECORD_SAMPLE:
-               return process_sample_event(event, offset, head);
+       return 0;
+}
 
-       case PERF_RECORD_MAX:
-       default:
+static int sample_type_check(struct perf_session *session __used)
+{
+       if (!(session->sample_type & PERF_SAMPLE_RAW)) {
+               fprintf(stderr,
+                       "No trace sample to read. Did you call perf record "
+                       "without -R?");
                return -1;
        }
 
        return 0;
 }
 
+static struct perf_event_ops event_ops = {
+       .process_sample_event   = process_sample_event,
+       .process_comm_event     = event__process_comm,
+       .process_lost_event     = process_lost_event,
+       .sample_type_check      = sample_type_check,
+};
+
 static int read_events(void)
 {
-       int ret, rc = EXIT_FAILURE;
-       unsigned long offset = 0;
-       unsigned long head = 0;
-       struct stat perf_stat;
-       event_t *event;
-       uint32_t size;
-       char *buf;
-
-       trace_report();
-       register_idle_thread(&threads, &last_match);
-
-       input = open(input_name, O_RDONLY);
-       if (input < 0) {
-               perror("failed to open file");
-               exit(-1);
-       }
-
-       ret = fstat(input, &perf_stat);
-       if (ret < 0) {
-               perror("failed to stat file");
-               exit(-1);
-       }
-
-       if (!perf_stat.st_size) {
-               fprintf(stderr, "zero-sized file, nothing to do!\n");
-               exit(0);
-       }
-       header = perf_header__read(input);
-       head = header->data_offset;
-       sample_type = perf_header__sample_type(header);
-
-       if (!(sample_type & PERF_SAMPLE_RAW))
-               die("No trace sample to read. Did you call perf record "
-                   "without -R?");
-
-       if (load_kernel() < 0) {
-               perror("failed to load kernel symbols");
-               return EXIT_FAILURE;
-       }
-
-remap:
-       buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
-                          MAP_SHARED, input, offset);
-       if (buf == MAP_FAILED) {
-               perror("failed to mmap file");
-               exit(-1);
-       }
-
-more:
-       event = (event_t *)(buf + head);
-
-       size = event->header.size;
-       if (!size)
-               size = 8;
-
-       if (head + event->header.size >= page_size * mmap_window) {
-               unsigned long shift = page_size * (head / page_size);
-               int res;
-
-               res = munmap(buf, page_size * mmap_window);
-               assert(res == 0);
-
-               offset += shift;
-               head -= shift;
-               goto remap;
-       }
-
-       size = event->header.size;
-
-
-       if (!size || process_event(event, offset, head) < 0) {
-
-               /*
-                * assume we lost track of the stream, check alignment, and
-                * increment a single u64 in the hope to catch on again 'soon'.
-                */
-
-               if (unlikely(head & 7))
-                       head &= ~7ULL;
-
-               size = 8;
-       }
-
-       head += size;
-
-       if (offset + head < (unsigned long)perf_stat.st_size)
-               goto more;
-
-       rc = EXIT_SUCCESS;
-       close(input);
+       int err;
+       struct perf_session *session = perf_session__new(input_name, O_RDONLY, 0);
+       if (session == NULL)
+               return -ENOMEM;
 
-       return rc;
+       err = perf_session__process_events(session, &event_ops);
+       perf_session__delete(session);
+       return err;
 }
 
 static void print_bad_events(void)
@@ -1784,9 +1722,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);
 
@@ -1883,6 +1821,8 @@ static const struct option latency_options[] = {
                   "sort by key(s): runtime, switch, avg, max"),
        OPT_BOOLEAN('v', "verbose", &verbose,
                    "be more verbose (show symbol address, etc)"),
+       OPT_INTEGER('C', "CPU", &profile_cpu,
+                   "CPU to profile on"),
        OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
                    "dump raw trace in ASCII"),
        OPT_END()
@@ -1960,14 +1900,18 @@ static int __cmd_record(int argc, const char **argv)
 
 int cmd_sched(int argc, const char **argv, const char *prefix __used)
 {
-       symbol__init();
-       page_size = getpagesize();
-
        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)) {
@@ -1991,11 +1935,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);
        }