perf probe: Use elfutils-libdw for analyzing debuginfo
[safe/jmp/linux-2.6] / tools / perf / builtin-timechart.c
index 0a2f222..0d4d8ff 100644 (file)
 #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";
 
-
-static unsigned long   page_size;
-static unsigned long   mmap_window = 32;
-static u64             sample_type;
-
 static unsigned int    numcpus;
 static u64             min_freq;       /* Lowest CPU frequency seen */
 static u64             max_freq;       /* Highest CPU frequency seen */
@@ -49,8 +46,6 @@ static u64            first_time, last_time;
 static int             power_only;
 
 
-static struct perf_header      *header;
-
 struct per_pid;
 struct per_pidcomm;
 
@@ -156,9 +151,9 @@ struct sample_wrapper *all_samples;
 
 struct process_filter;
 struct process_filter {
-       char                    *name;
-       int                     pid;
-       struct process_filter   *next;
+       char                    *name;
+       int                     pid;
+       struct process_filter   *next;
 };
 
 static struct process_filter *process_filter;
@@ -283,33 +278,30 @@ 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(event_t *event, 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(event_t *event, 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(event_t *event, 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 {
@@ -483,46 +475,24 @@ 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(event_t *event, struct perf_session *session)
 {
-       int cursor = 0;
-       u64 addr = 0;
-       u64 stamp = 0;
-       u32 cpu = 0;
-       u32 pid = 0;
+       struct sample_data data;
        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 (sample_type & PERF_SAMPLE_TIME) {
-               stamp = event->sample.array[cursor++];
+       memset(&data, 0, sizeof(data));
 
-               if (!first_time || first_time > stamp)
-                       first_time = stamp;
-               if (last_time < stamp)
-                       last_time = stamp;
+       event__parse_sample(event, session->sample_type, &data);
 
+       if (session->sample_type & PERF_SAMPLE_TIME) {
+               if (!first_time || first_time > data.time)
+                       first_time = data.time;
+               if (last_time < data.time)
+                       last_time = data.time;
        }
-       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 *)data.raw_data;
+       if (session->sample_type & PERF_SAMPLE_RAW && data.raw_size > 0) {
                char *event_str;
                struct power_entry *pe;
 
@@ -534,19 +504,19 @@ process_sample_event(event_t *event)
                        return 0;
 
                if (strcmp(event_str, "power:power_start") == 0)
-                       c_state_start(cpu, stamp, pe->value);
+                       c_state_start(data.cpu, data.time, pe->value);
 
                if (strcmp(event_str, "power:power_end") == 0)
-                       c_state_end(cpu, stamp);
+                       c_state_end(data.cpu, data.time);
 
                if (strcmp(event_str, "power:power_frequency") == 0)
-                       p_state_change(cpu, stamp, pe->value);
+                       p_state_change(data.cpu, data.time, pe->value);
 
                if (strcmp(event_str, "sched:sched_wakeup") == 0)
-                       sched_wakeup(cpu, stamp, pid, te);
+                       sched_wakeup(data.cpu, data.time, data.pid, te);
 
                if (strcmp(event_str, "sched:sched_switch") == 0)
-                       sched_switch(cpu, stamp, te);
+                       sched_switch(data.cpu, data.time, te);
        }
        return 0;
 }
@@ -599,16 +569,16 @@ static void end_sample_processing(void)
        }
 }
 
-static u64 sample_time(event_t *event)
+static u64 sample_time(event_t *event, const struct perf_session *session)
 {
        int cursor;
 
        cursor = 0;
-       if (sample_type & PERF_SAMPLE_IP)
+       if (session->sample_type & PERF_SAMPLE_IP)
                cursor++;
-       if (sample_type & PERF_SAMPLE_TID)
+       if (session->sample_type & PERF_SAMPLE_TID)
                cursor++;
-       if (sample_type & PERF_SAMPLE_TIME)
+       if (session->sample_type & PERF_SAMPLE_TIME)
                return event->sample.array[cursor];
        return 0;
 }
@@ -618,8 +588,7 @@ static u64 sample_time(event_t *event)
  * We first queue all events, sorted backwards by insertion.
  * The order will get flipped later.
  */
-static int
-queue_sample_event(event_t *event)
+static int queue_sample_event(event_t *event, struct perf_session *session)
 {
        struct sample_wrapper *copy, *prev;
        int size;
@@ -633,7 +602,7 @@ queue_sample_event(event_t *event)
        memset(copy, 0, size);
 
        copy->next = NULL;
-       copy->timestamp = sample_time(event);
+       copy->timestamp = sample_time(event, session);
 
        memcpy(&copy->data, event, event->sample.header.size);
 
@@ -1045,37 +1014,7 @@ static void write_svg_file(const char *filename)
        svg_close();
 }
 
-static int
-process_event(event_t *event)
-{
-
-       switch (event->header.type) {
-
-       case PERF_RECORD_COMM:
-               return process_comm_event(event);
-       case PERF_RECORD_FORK:
-               return process_fork_event(event);
-       case PERF_RECORD_EXIT:
-               return process_exit_event(event);
-       case PERF_RECORD_SAMPLE:
-               return queue_sample_event(event);
-
-       /*
-        * We dont process them right now but they are fine:
-        */
-       case PERF_RECORD_MMAP:
-       case PERF_RECORD_THROTTLE:
-       case PERF_RECORD_UNTHROTTLE:
-               return 0;
-
-       default:
-               return -1;
-       }
-
-       return 0;
-}
-
-static void process_samples(void)
+static void process_samples(struct perf_session *session)
 {
        struct sample_wrapper *cursor;
        event_t *event;
@@ -1086,111 +1025,33 @@ static void process_samples(void)
        while (cursor) {
                event = (void *)&cursor->data;
                cursor = cursor->next;
-               process_sample_event(event);
+               process_sample_event(event, session);
        }
 }
 
+static struct perf_event_ops event_ops = {
+       .comm   = process_comm_event,
+       .fork   = process_fork_event,
+       .exit   = process_exit_event,
+       .sample = queue_sample_event,
+};
 
 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);
-       }
-
-       ret = fstat(input, &statbuf);
-       if (ret < 0) {
-               perror("failed to stat file");
-               exit(-1);
-       }
-
-       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);
+       struct perf_session *session = perf_session__new(input_name, O_RDONLY, 0);
+       int ret = -EINVAL;
 
-               ret2 = munmap(buf, page_size * mmap_window);
-               assert(ret2 == 0);
+       if (session == NULL)
+               return -ENOMEM;
 
-               offset += shift;
-               head -= shift;
-               goto remap;
-       }
-
-       size = event->header.size;
-
-       if (!size || process_event(event) < 0) {
-               pr_warning("%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 (!perf_session__has_traces(session, "timechart record"))
+               goto out_delete;
 
-       if (offset + head < (unsigned long)statbuf.st_size)
-               goto more;
+       ret = perf_session__process_events(session, &event_ops);
+       if (ret)
+               goto out_delete;
 
-done:
-       rc = EXIT_SUCCESS;
-       close(input);
-
-
-       process_samples();
+       process_samples(session);
 
        end_sample_processing();
 
@@ -1200,8 +1061,9 @@ done:
 
        pr_info("Written %2.1f seconds of trace to %s.\n",
                (last_time - first_time) / 1000000000.0, output_name);
-
-       return rc;
+out_delete:
+       perf_session__delete(session);
+       return ret;
 }
 
 static const char * const timechart_usage[] = {
@@ -1266,13 +1128,11 @@ static const struct option options[] = {
 
 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)