perf tools: Remove junk code in mmap size handling
[linux-2.6.git] / tools / perf / builtin-timechart.c
index 6004063..aa26f4d 100644 (file)
@@ -21,7 +21,6 @@
 #include "util/cache.h"
 #include <linux/rbtree.h>
 #include "util/symbol.h"
-#include "util/string.h"
 #include "util/callchain.h"
 #include "util/strlist.h"
 
 #include "util/header.h"
 #include "util/parse-options.h"
 #include "util/parse-events.h"
+#include "util/event.h"
+#include "util/session.h"
 #include "util/svghelper.h"
 
-static char            const *input_name = "perf.data";
-static char            const *output_name = "output.svg";
+#define SUPPORT_OLD_POWER_EVENTS 1
+#define PWR_EVENT_EXIT -1
 
 
-static unsigned long   page_size;
-static unsigned long   mmap_window = 32;
-static u64             sample_type;
+static char            const *input_name = "perf.data";
+static char            const *output_name = "output.svg";
 
 static unsigned int    numcpus;
 static u64             min_freq;       /* Lowest CPU frequency seen */
@@ -46,8 +46,8 @@ static u64            turbo_frequency;
 
 static u64             first_time, last_time;
 
+static bool            power_only;
 
-static struct perf_header      *header;
 
 struct per_pid;
 struct per_pidcomm;
@@ -81,8 +81,6 @@ struct per_pid {
 
        struct per_pidcomm *all;
        struct per_pidcomm *current;
-
-       int painted;
 };
 
 
@@ -149,7 +147,15 @@ struct wake_event {
 static struct power_event    *power_events;
 static struct wake_event     *wake_events;
 
-struct sample_wrapper *all_samples;
+struct process_filter;
+struct process_filter {
+       char                    *name;
+       int                     pid;
+       struct process_filter   *next;
+};
+
+static struct process_filter *process_filter;
+
 
 static struct per_pid *find_create_pid(int pid)
 {
@@ -258,9 +264,6 @@ pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end)
                c->start_time = start;
        if (p->start_time == 0 || p->start_time > start)
                p->start_time = start;
-
-       if (cpu > numcpus)
-               numcpus = cpu;
 }
 
 #define MAX_CPUS 4096
@@ -270,39 +273,52 @@ static int cpus_cstate_state[MAX_CPUS];
 static u64 cpus_pstate_start_times[MAX_CPUS];
 static u64 cpus_pstate_state[MAX_CPUS];
 
-static int
-process_comm_event(event_t *event)
+static int process_comm_event(union perf_event *event,
+                             struct perf_sample *sample __used,
+                             struct perf_session *session __used)
 {
-       pid_set_comm(event->comm.pid, event->comm.comm);
+       pid_set_comm(event->comm.tid, event->comm.comm);
        return 0;
 }
-static int
-process_fork_event(event_t *event)
+
+static int process_fork_event(union perf_event *event,
+                             struct perf_sample *sample __used,
+                             struct perf_session *session __used)
 {
        pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
        return 0;
 }
 
-static int
-process_exit_event(event_t *event)
+static int process_exit_event(union perf_event *event,
+                             struct perf_sample *sample __used,
+                             struct perf_session *session __used)
 {
        pid_exit(event->fork.pid, event->fork.time);
        return 0;
 }
 
 struct trace_entry {
-       u32                     size;
        unsigned short          type;
        unsigned char           flags;
        unsigned char           preempt_count;
        int                     pid;
-       int                     tgid;
+       int                     lock_depth;
 };
 
-struct power_entry {
+#ifdef SUPPORT_OLD_POWER_EVENTS
+static int use_old_power_events;
+struct power_entry_old {
        struct trace_entry te;
-       s64     type;
-       s64     value;
+       u64     type;
+       u64     value;
+       u64     cpu_id;
+};
+#endif
+
+struct power_processor_entry {
+       struct trace_entry te;
+       u32     state;
+       u32     cpu_id;
 };
 
 #define TASK_COMM_LEN 16
@@ -455,8 +471,8 @@ static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
                if (p->current->state != TYPE_NONE)
                        pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
 
-                       p->current->state_since = timestamp;
-                       p->current->state = TYPE_RUNNING;
+               p->current->state_since = timestamp;
+               p->current->state = TYPE_RUNNING;
        }
 
        if (prev_p->current) {
@@ -470,70 +486,79 @@ static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
 }
 
 
-static int
-process_sample_event(event_t *event)
+static int process_sample_event(union perf_event *event __used,
+                               struct perf_sample *sample,
+                               struct perf_evsel *evsel __used,
+                               struct perf_session *session)
 {
-       int cursor = 0;
-       u64 addr = 0;
-       u64 stamp = 0;
-       u32 cpu = 0;
-       u32 pid = 0;
        struct trace_entry *te;
 
-       if (sample_type & PERF_SAMPLE_IP)
-               cursor++;
-
-       if (sample_type & PERF_SAMPLE_TID) {
-               pid = event->sample.array[cursor]>>32;
-               cursor++;
+       if (session->sample_type & PERF_SAMPLE_TIME) {
+               if (!first_time || first_time > sample->time)
+                       first_time = sample->time;
+               if (last_time < sample->time)
+                       last_time = sample->time;
        }
-       if (sample_type & PERF_SAMPLE_TIME) {
-               stamp = event->sample.array[cursor++];
-
-               if (!first_time || first_time > stamp)
-                       first_time = stamp;
-               if (last_time < stamp)
-                       last_time = stamp;
 
-       }
-       if (sample_type & PERF_SAMPLE_ADDR)
-               addr = event->sample.array[cursor++];
-       if (sample_type & PERF_SAMPLE_ID)
-               cursor++;
-       if (sample_type & PERF_SAMPLE_STREAM_ID)
-               cursor++;
-       if (sample_type & PERF_SAMPLE_CPU)
-               cpu = event->sample.array[cursor++] & 0xFFFFFFFF;
-       if (sample_type & PERF_SAMPLE_PERIOD)
-               cursor++;
-
-       te = (void *)&event->sample.array[cursor];
-
-       if (sample_type & PERF_SAMPLE_RAW && te->size > 0) {
+       te = (void *)sample->raw_data;
+       if (session->sample_type & PERF_SAMPLE_RAW && sample->raw_size > 0) {
                char *event_str;
-               struct power_entry *pe;
-
-               pe = (void *)te;
-
+#ifdef SUPPORT_OLD_POWER_EVENTS
+               struct power_entry_old *peo;
+               peo = (void *)te;
+#endif
+               /*
+                * FIXME: use evsel, its already mapped from id to perf_evsel,
+                * remove perf_header__find_event infrastructure bits.
+                * Mapping all these "power:cpu_idle" strings to the tracepoint
+                * ID and then just comparing against evsel->attr.config.
+                *
+                * e.g.:
+                *
+                * if (evsel->attr.config == power_cpu_idle_id)
+                */
                event_str = perf_header__find_event(te->type);
 
                if (!event_str)
                        return 0;
 
-               if (strcmp(event_str, "power:power_start") == 0)
-                       c_state_start(cpu, stamp, pe->value);
+               if (sample->cpu > numcpus)
+                       numcpus = sample->cpu;
 
-               if (strcmp(event_str, "power:power_end") == 0)
-                       c_state_end(cpu, stamp);
+               if (strcmp(event_str, "power:cpu_idle") == 0) {
+                       struct power_processor_entry *ppe = (void *)te;
+                       if (ppe->state == (u32)PWR_EVENT_EXIT)
+                               c_state_end(ppe->cpu_id, sample->time);
+                       else
+                               c_state_start(ppe->cpu_id, sample->time,
+                                             ppe->state);
+               }
+               else if (strcmp(event_str, "power:cpu_frequency") == 0) {
+                       struct power_processor_entry *ppe = (void *)te;
+                       p_state_change(ppe->cpu_id, sample->time, ppe->state);
+               }
 
-               if (strcmp(event_str, "power:power_frequency") == 0)
-                       p_state_change(cpu, stamp, pe->value);
+               else if (strcmp(event_str, "sched:sched_wakeup") == 0)
+                       sched_wakeup(sample->cpu, sample->time, sample->pid, te);
 
-               if (strcmp(event_str, "sched:sched_wakeup") == 0)
-                       sched_wakeup(cpu, stamp, pid, te);
+               else if (strcmp(event_str, "sched:sched_switch") == 0)
+                       sched_switch(sample->cpu, sample->time, te);
 
-               if (strcmp(event_str, "sched:sched_switch") == 0)
-                       sched_switch(cpu, stamp, te);
+#ifdef SUPPORT_OLD_POWER_EVENTS
+               if (use_old_power_events) {
+                       if (strcmp(event_str, "power:power_start") == 0)
+                               c_state_start(peo->cpu_id, sample->time,
+                                             peo->value);
+
+                       else if (strcmp(event_str, "power:power_end") == 0)
+                               c_state_end(sample->cpu, sample->time);
+
+                       else if (strcmp(event_str,
+                                       "power:power_frequency") == 0)
+                               p_state_change(peo->cpu_id, sample->time,
+                                              peo->value);
+               }
+#endif
        }
        return 0;
 }
@@ -547,7 +572,7 @@ static void end_sample_processing(void)
        u64 cpu;
        struct power_event *pwr;
 
-       for (cpu = 0; cpu < numcpus; cpu++) {
+       for (cpu = 0; cpu <= numcpus; cpu++) {
                pwr = malloc(sizeof(struct power_event));
                if (!pwr)
                        return;
@@ -586,89 +611,6 @@ static void end_sample_processing(void)
        }
 }
 
-static u64 sample_time(event_t *event)
-{
-       int cursor;
-
-       cursor = 0;
-       if (sample_type & PERF_SAMPLE_IP)
-               cursor++;
-       if (sample_type & PERF_SAMPLE_TID)
-               cursor++;
-       if (sample_type & PERF_SAMPLE_TIME)
-               return event->sample.array[cursor];
-       return 0;
-}
-
-
-/*
- * We first queue all events, sorted backwards by insertion.
- * The order will get flipped later.
- */
-static int
-queue_sample_event(event_t *event)
-{
-       struct sample_wrapper *copy, *prev;
-       int size;
-
-       size = event->sample.header.size + sizeof(struct sample_wrapper) + 8;
-
-       copy = malloc(size);
-       if (!copy)
-               return 1;
-
-       memset(copy, 0, size);
-
-       copy->next = NULL;
-       copy->timestamp = sample_time(event);
-
-       memcpy(&copy->data, event, event->sample.header.size);
-
-       /* insert in the right place in the list */
-
-       if (!all_samples) {
-               /* first sample ever */
-               all_samples = copy;
-               return 0;
-       }
-
-       if (all_samples->timestamp < copy->timestamp) {
-               /* insert at the head of the list */
-               copy->next = all_samples;
-               all_samples = copy;
-               return 0;
-       }
-
-       prev = all_samples;
-       while (prev->next) {
-               if (prev->next->timestamp < copy->timestamp) {
-                       copy->next = prev->next;
-                       prev->next = copy;
-                       return 0;
-               }
-               prev = prev->next;
-       }
-       /* insert at the end of the list */
-       prev->next = copy;
-
-       return 0;
-}
-
-static void sort_queued_samples(void)
-{
-       struct sample_wrapper *cursor, *next;
-
-       cursor = all_samples;
-       all_samples = NULL;
-
-       while (cursor) {
-               next = cursor->next;
-               cursor->next = all_samples;
-               all_samples = cursor;
-               cursor = next;
-       }
-}
-
 /*
  * Sort the pid datastructure
  */
@@ -761,21 +703,42 @@ static void draw_wakeups(void)
                                c = p->all;
                                while (c) {
                                        if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
-                                               if (p->pid == we->waker) {
+                                               if (p->pid == we->waker && !from) {
                                                        from = c->Y;
-                                                       task_from = c->comm;
+                                                       task_from = strdup(c->comm);
                                                }
-                                               if (p->pid == we->wakee) {
+                                               if (p->pid == we->wakee && !to) {
                                                        to = c->Y;
-                                                       task_to = c->comm;
+                                                       task_to = strdup(c->comm);
                                                }
                                        }
                                        c = c->next;
                                }
+                               c = p->all;
+                               while (c) {
+                                       if (p->pid == we->waker && !from) {
+                                               from = c->Y;
+                                               task_from = strdup(c->comm);
+                                       }
+                                       if (p->pid == we->wakee && !to) {
+                                               to = c->Y;
+                                               task_to = strdup(c->comm);
+                                       }
+                                       c = c->next;
+                               }
                        }
                        p = p->next;
                }
 
+               if (!task_from) {
+                       task_from = malloc(40);
+                       sprintf(task_from, "[%i]", we->waker);
+               }
+               if (!task_to) {
+                       task_to = malloc(40);
+                       sprintf(task_to, "[%i]", we->wakee);
+               }
+
                if (we->waker == -1)
                        svg_interrupt(we->time, to);
                else if (from && to && abs(from - to) == 1)
@@ -783,6 +746,9 @@ static void draw_wakeups(void)
                else
                        svg_partial_wakeline(we->time, from, task_from, to, task_to);
                we = we->next;
+
+               free(task_from);
+               free(task_to);
        }
 }
 
@@ -856,12 +822,89 @@ static void draw_process_bars(void)
        }
 }
 
+static void add_process_filter(const char *string)
+{
+       struct process_filter *filt;
+       int pid;
+
+       pid = strtoull(string, NULL, 10);
+       filt = malloc(sizeof(struct process_filter));
+       if (!filt)
+               return;
+
+       filt->name = strdup(string);
+       filt->pid  = pid;
+       filt->next = process_filter;
+
+       process_filter = filt;
+}
+
+static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
+{
+       struct process_filter *filt;
+       if (!process_filter)
+               return 1;
+
+       filt = process_filter;
+       while (filt) {
+               if (filt->pid && p->pid == filt->pid)
+                       return 1;
+               if (strcmp(filt->name, c->comm) == 0)
+                       return 1;
+               filt = filt->next;
+       }
+       return 0;
+}
+
+static int determine_display_tasks_filtered(void)
+{
+       struct per_pid *p;
+       struct per_pidcomm *c;
+       int count = 0;
+
+       p = all_data;
+       while (p) {
+               p->display = 0;
+               if (p->start_time == 1)
+                       p->start_time = first_time;
+
+               /* no exit marker, task kept running to the end */
+               if (p->end_time == 0)
+                       p->end_time = last_time;
+
+               c = p->all;
+
+               while (c) {
+                       c->display = 0;
+
+                       if (c->start_time == 1)
+                               c->start_time = first_time;
+
+                       if (passes_filter(p, c)) {
+                               c->display = 1;
+                               p->display = 1;
+                               count++;
+                       }
+
+                       if (c->end_time == 0)
+                               c->end_time = last_time;
+
+                       c = c->next;
+               }
+               p = p->next;
+       }
+       return count;
+}
+
 static int determine_display_tasks(u64 threshold)
 {
        struct per_pid *p;
        struct per_pidcomm *c;
        int count = 0;
 
+       if (process_filter)
+               return determine_display_tasks_filtered();
+
        p = all_data;
        while (p) {
                p->display = 0;
@@ -871,7 +914,7 @@ static int determine_display_tasks(u64 threshold)
                /* no exit marker, task kept running to the end */
                if (p->end_time == 0)
                        p->end_time = last_time;
-               if (p->total_time >= threshold)
+               if (p->total_time >= threshold && !power_only)
                        p->display = 1;
 
                c = p->all;
@@ -882,7 +925,7 @@ static int determine_display_tasks(u64 threshold)
                        if (c->start_time == 1)
                                c->start_time = first_time;
 
-                       if (c->total_time >= threshold) {
+                       if (c->total_time >= threshold && !power_only) {
                                c->display = 1;
                                count++;
                        }
@@ -931,154 +974,29 @@ static void write_svg_file(const char *filename)
        svg_close();
 }
 
-static int
-process_event(event_t *event)
-{
-
-       switch (event->header.type) {
-
-       case PERF_EVENT_COMM:
-               return process_comm_event(event);
-       case PERF_EVENT_FORK:
-               return process_fork_event(event);
-       case PERF_EVENT_EXIT:
-               return process_exit_event(event);
-       case PERF_EVENT_SAMPLE:
-               return queue_sample_event(event);
-
-       /*
-        * We dont process them right now but they are fine:
-        */
-       case PERF_EVENT_MMAP:
-       case PERF_EVENT_THROTTLE:
-       case PERF_EVENT_UNTHROTTLE:
-               return 0;
-
-       default:
-               return -1;
-       }
-
-       return 0;
-}
-
-static void process_samples(void)
-{
-       struct sample_wrapper *cursor;
-       event_t *event;
-
-       sort_queued_samples();
-
-       cursor = all_samples;
-       while (cursor) {
-               event = (void *)&cursor->data;
-               cursor = cursor->next;
-               process_sample_event(event);
-       }
-}
-
+static struct perf_event_ops event_ops = {
+       .comm                   = process_comm_event,
+       .fork                   = process_fork_event,
+       .exit                   = process_exit_event,
+       .sample                 = process_sample_event,
+       .ordered_samples        = true,
+};
 
 static int __cmd_timechart(void)
 {
-       int ret, rc = EXIT_FAILURE;
-       unsigned long offset = 0;
-       unsigned long head, shift;
-       struct stat statbuf;
-       event_t *event;
-       uint32_t size;
-       char *buf;
-       int input;
-
-       input = open(input_name, O_RDONLY);
-       if (input < 0) {
-               fprintf(stderr, " failed to open file: %s", input_name);
-               if (!strcmp(input_name, "perf.data"))
-                       fprintf(stderr, "  (try 'perf record' first)");
-               fprintf(stderr, "\n");
-               exit(-1);
-       }
+       struct perf_session *session = perf_session__new(input_name, O_RDONLY,
+                                                        0, false, &event_ops);
+       int ret = -EINVAL;
 
-       ret = fstat(input, &statbuf);
-       if (ret < 0) {
-               perror("failed to stat file");
-               exit(-1);
-       }
+       if (session == NULL)
+               return -ENOMEM;
 
-       if (!statbuf.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);
-
-       shift = page_size * (head / page_size);
-       offset += shift;
-       head -= shift;
-
-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) {
-               int ret2;
-
-               shift = page_size * (head / page_size);
-
-               ret2 = munmap(buf, page_size * mmap_window);
-               assert(ret2 == 0);
-
-               offset += shift;
-               head -= shift;
-               goto remap;
-       }
-
-       size = event->header.size;
-
-       if (!size || process_event(event) < 0) {
-
-               printf("%p [%p]: skipping unknown header type: %d\n",
-                       (void *)(offset + head),
-                       (void *)(long)(event->header.size),
-                       event->header.type);
-
-               /*
-                * 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 >= header->data_offset + header->data_size)
-               goto done;
-
-       if (offset + head < (unsigned long)statbuf.st_size)
-               goto more;
-
-done:
-       rc = EXIT_SUCCESS;
-       close(input);
+       if (!perf_session__has_traces(session, "timechart record"))
+               goto out_delete;
 
-
-       process_samples();
+       ret = perf_session__process_events(session, &event_ops);
+       if (ret)
+               goto out_delete;
 
        end_sample_processing();
 
@@ -1086,9 +1004,11 @@ done:
 
        write_svg_file(output_name);
 
-       printf("Written %2.1f seconds of trace to %s.\n", (last_time - first_time) / 1000000000.0, output_name);
-
-       return rc;
+       pr_info("Written %2.1f seconds of trace to %s.\n",
+               (last_time - first_time) / 1000000000.0, output_name);
+out_delete:
+       perf_session__delete(session);
+       return ret;
 }
 
 static const char * const timechart_usage[] = {
@@ -1096,11 +1016,11 @@ static const char * const timechart_usage[] = {
        NULL
 };
 
-static const char *record_args[] = {
+#ifdef SUPPORT_OLD_POWER_EVENTS
+static const char * const record_old_args[] = {
        "record",
        "-a",
        "-R",
-       "-M",
        "-f",
        "-c", "1",
        "-e", "power:power_start",
@@ -1109,16 +1029,43 @@ static const char *record_args[] = {
        "-e", "sched:sched_wakeup",
        "-e", "sched:sched_switch",
 };
+#endif
+
+static const char * const record_new_args[] = {
+       "record",
+       "-a",
+       "-R",
+       "-f",
+       "-c", "1",
+       "-e", "power:cpu_frequency",
+       "-e", "power:cpu_idle",
+       "-e", "sched:sched_wakeup",
+       "-e", "sched:sched_switch",
+};
 
 static int __cmd_record(int argc, const char **argv)
 {
        unsigned int rec_argc, i, j;
        const char **rec_argv;
+       const char * const *record_args = record_new_args;
+       unsigned int record_elems = ARRAY_SIZE(record_new_args);
+
+#ifdef SUPPORT_OLD_POWER_EVENTS
+       if (!is_valid_tracepoint("power:cpu_idle") &&
+           is_valid_tracepoint("power:power_start")) {
+               use_old_power_events = 1;
+               record_args = record_old_args;
+               record_elems = ARRAY_SIZE(record_old_args);
+       }
+#endif
 
-       rec_argc = ARRAY_SIZE(record_args) + argc - 1;
+       rec_argc = record_elems + argc - 1;
        rec_argv = calloc(rec_argc + 1, sizeof(char *));
 
-       for (i = 0; i < ARRAY_SIZE(record_args); i++)
+       if (rec_argv == NULL)
+               return -ENOMEM;
+
+       for (i = 0; i < record_elems; i++)
                rec_argv[i] = strdup(record_args[i]);
 
        for (j = 1; j < (unsigned int)argc; j++, i++)
@@ -1127,6 +1074,14 @@ static int __cmd_record(int argc, const char **argv)
        return cmd_record(i, rec_argv, NULL);
 }
 
+static int
+parse_process(const struct option *opt __used, const char *arg, int __used unset)
+{
+       if (arg)
+               add_process_filter(arg);
+       return 0;
+}
+
 static const struct option options[] = {
        OPT_STRING('i', "input", &input_name, "file",
                    "input file name"),
@@ -1134,19 +1089,24 @@ static const struct option options[] = {
                    "output file name"),
        OPT_INTEGER('w', "width", &svg_page_width,
                    "page width"),
+       OPT_BOOLEAN('P', "power-only", &power_only,
+                   "output power data only"),
+       OPT_CALLBACK('p', "process", NULL, "process",
+                     "process selector. Pass a pid or process name.",
+                      parse_process),
+       OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
+                   "Look for files with symbols relative to this directory"),
        OPT_END()
 };
 
 
 int cmd_timechart(int argc, const char **argv, const char *prefix __used)
 {
-       symbol__init();
-
-       page_size = getpagesize();
-
        argc = parse_options(argc, argv, options, timechart_usage,
                        PARSE_OPT_STOP_AT_NON_OPTION);
 
+       symbol__init();
+
        if (argc && !strncmp(argv[0], "rec", 3))
                return __cmd_record(argc, argv);
        else if (argc)