]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - tools/perf/builtin-timechart.c
pktgen: correctly handle failures when adding a device
[linux-2.6.git] / tools / perf / builtin-timechart.c
index 0a2f22261c3abdac107da1ce96dda168ba5d08cd..3b75b2e21ea55e51739a7200ffeeb61aa8e613ad 100644 (file)
@@ -19,9 +19,9 @@
 #include "util/color.h"
 #include <linux/list.h>
 #include "util/cache.h"
+#include "util/evsel.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"
+#include "util/tool.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 const char      *input_name;
+static const char      *output_name = "output.svg";
 
 static unsigned int    numcpus;
 static u64             min_freq;       /* Lowest CPU frequency seen */
@@ -46,11 +48,9 @@ static u64           turbo_frequency;
 
 static u64             first_time, last_time;
 
-static int             power_only;
+static bool            power_only;
 
 
-static struct perf_header      *header;
-
 struct per_pid;
 struct per_pidcomm;
 
@@ -83,8 +83,6 @@ struct per_pid {
 
        struct per_pidcomm *all;
        struct per_pidcomm *current;
-
-       int painted;
 };
 
 
@@ -151,14 +149,11 @@ 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;
+       char                    *name;
+       int                     pid;
+       struct process_filter   *next;
 };
 
 static struct process_filter *process_filter;
@@ -271,9 +266,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
@@ -283,39 +275,55 @@ 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(struct perf_tool *tool __used,
+                             union perf_event *event,
+                             struct perf_sample *sample __used,
+                             struct machine *machine __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(struct perf_tool *tool __used,
+                             union perf_event *event,
+                             struct perf_sample *sample __used,
+                             struct machine *machine __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(struct perf_tool *tool __used,
+                             union perf_event *event,
+                             struct perf_sample *sample __used,
+                             struct machine *machine __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;
+};
+
+#ifdef SUPPORT_OLD_POWER_EVENTS
+static int use_old_power_events;
+struct power_entry_old {
+       struct trace_entry te;
+       u64     type;
+       u64     value;
+       u64     cpu_id;
 };
+#endif
 
-struct power_entry {
+struct power_processor_entry {
        struct trace_entry te;
-       s64     type;
-       s64     value;
+       u32     state;
+       u32     cpu_id;
 };
 
 #define TASK_COMM_LEN 16
@@ -468,8 +476,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) {
@@ -483,70 +491,80 @@ 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(struct perf_tool *tool __used,
+                               union perf_event *event __used,
+                               struct perf_sample *sample,
+                               struct perf_evsel *evsel,
+                               struct machine *machine __used)
 {
-       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 (evsel->attr.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 ((evsel->attr.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;
 }
@@ -599,89 +617,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
  */
@@ -1045,152 +980,29 @@ 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)
-{
-       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_tool perf_timechart = {
+       .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);
-       }
-
-       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;
+       struct perf_session *session = perf_session__new(input_name, O_RDONLY,
+                                                        0, false, &perf_timechart);
+       int ret = -EINVAL;
 
-               shift = page_size * (head / page_size);
+       if (session == NULL)
+               return -ENOMEM;
 
-               ret2 = munmap(buf, page_size * mmap_window);
-               assert(ret2 == 0);
+       if (!perf_session__has_traces(session, "timechart record"))
+               goto out_delete;
 
-               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 (offset + head < (unsigned long)statbuf.st_size)
-               goto more;
-
-done:
-       rc = EXIT_SUCCESS;
-       close(input);
-
-
-       process_samples();
+       ret = perf_session__process_events(session, &perf_timechart);
+       if (ret)
+               goto out_delete;
 
        end_sample_processing();
 
@@ -1200,8 +1012,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[] = {
@@ -1209,11 +1022,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",
@@ -1222,16 +1035,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++)
@@ -1260,19 +1100,19 @@ static const struct option options[] = {
        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)