perf ftrace: Add support for tracing option 'irq-info'
[linux-2.6-microblaze.git] / tools / perf / builtin-ftrace.c
index 2bfc1b0..d1241fe 100644 (file)
@@ -26,6 +26,8 @@
 #include "thread_map.h"
 #include "util/cap.h"
 #include "util/config.h"
+#include "util/units.h"
+#include "util/parse-sublevel-options.h"
 
 #define DEFAULT_TRACER  "function_graph"
 
@@ -33,11 +35,18 @@ struct perf_ftrace {
        struct evlist           *evlist;
        struct target           target;
        const char              *tracer;
+       bool                    list_avail_functions;
        struct list_head        filters;
        struct list_head        notrace;
        struct list_head        graph_funcs;
        struct list_head        nograph_funcs;
        int                     graph_depth;
+       unsigned long           percpu_buffer_size;
+       bool                    inherit;
+       int                     func_stack_trace;
+       int                     func_irq_info;
+       int                     graph_nosleep_time;
+       int                     graph_noirqs;
 };
 
 struct filter_entry {
@@ -128,9 +137,82 @@ static int append_tracing_file(const char *name, const char *val)
        return __write_tracing_file(name, val, true);
 }
 
+static int read_tracing_file_to_stdout(const char *name)
+{
+       char buf[4096];
+       char *file;
+       int fd;
+       int ret = -1;
+
+       file = get_tracing_file(name);
+       if (!file) {
+               pr_debug("cannot get tracing file: %s\n", name);
+               return -1;
+       }
+
+       fd = open(file, O_RDONLY);
+       if (fd < 0) {
+               pr_debug("cannot open tracing file: %s: %s\n",
+                        name, str_error_r(errno, buf, sizeof(buf)));
+               goto out;
+       }
+
+       /* read contents to stdout */
+       while (true) {
+               int n = read(fd, buf, sizeof(buf));
+               if (n == 0)
+                       break;
+               else if (n < 0)
+                       goto out_close;
+
+               if (fwrite(buf, n, 1, stdout) != 1)
+                       goto out_close;
+       }
+       ret = 0;
+
+out_close:
+       close(fd);
+out:
+       put_tracing_file(file);
+       return ret;
+}
+
+static int write_tracing_file_int(const char *name, int value)
+{
+       char buf[16];
+
+       snprintf(buf, sizeof(buf), "%d", value);
+       if (write_tracing_file(name, buf) < 0)
+               return -1;
+
+       return 0;
+}
+
+static int write_tracing_option_file(const char *name, const char *val)
+{
+       char *file;
+       int ret;
+
+       if (asprintf(&file, "options/%s", name) < 0)
+               return -1;
+
+       ret = __write_tracing_file(file, val, false);
+       free(file);
+       return ret;
+}
+
 static int reset_tracing_cpu(void);
 static void reset_tracing_filters(void);
 
+static void reset_tracing_options(struct perf_ftrace *ftrace __maybe_unused)
+{
+       write_tracing_option_file("function-fork", "0");
+       write_tracing_option_file("func_stack_trace", "0");
+       write_tracing_option_file("sleep-time", "1");
+       write_tracing_option_file("funcgraph-irqs", "1");
+       write_tracing_option_file("irq-info", "0");
+}
+
 static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
 {
        if (write_tracing_file("tracing_on", "0") < 0)
@@ -149,6 +231,7 @@ static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
                return -1;
 
        reset_tracing_filters();
+       reset_tracing_options(ftrace);
        return 0;
 }
 
@@ -204,6 +287,28 @@ static int set_tracing_cpu(struct perf_ftrace *ftrace)
        return set_tracing_cpumask(cpumap);
 }
 
+static int set_tracing_func_stack_trace(struct perf_ftrace *ftrace)
+{
+       if (!ftrace->func_stack_trace)
+               return 0;
+
+       if (write_tracing_option_file("func_stack_trace", "1") < 0)
+               return -1;
+
+       return 0;
+}
+
+static int set_tracing_func_irqinfo(struct perf_ftrace *ftrace)
+{
+       if (!ftrace->func_irq_info)
+               return 0;
+
+       if (write_tracing_option_file("irq-info", "1") < 0)
+               return -1;
+
+       return 0;
+}
+
 static int reset_tracing_cpu(void)
 {
        struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL);
@@ -258,8 +363,6 @@ static void reset_tracing_filters(void)
 
 static int set_tracing_depth(struct perf_ftrace *ftrace)
 {
-       char buf[16];
-
        if (ftrace->graph_depth == 0)
                return 0;
 
@@ -268,9 +371,55 @@ static int set_tracing_depth(struct perf_ftrace *ftrace)
                return -1;
        }
 
-       snprintf(buf, sizeof(buf), "%d", ftrace->graph_depth);
+       if (write_tracing_file_int("max_graph_depth", ftrace->graph_depth) < 0)
+               return -1;
+
+       return 0;
+}
+
+static int set_tracing_percpu_buffer_size(struct perf_ftrace *ftrace)
+{
+       int ret;
+
+       if (ftrace->percpu_buffer_size == 0)
+               return 0;
+
+       ret = write_tracing_file_int("buffer_size_kb",
+                                    ftrace->percpu_buffer_size / 1024);
+       if (ret < 0)
+               return ret;
+
+       return 0;
+}
+
+static int set_tracing_trace_inherit(struct perf_ftrace *ftrace)
+{
+       if (!ftrace->inherit)
+               return 0;
+
+       if (write_tracing_option_file("function-fork", "1") < 0)
+               return -1;
+
+       return 0;
+}
+
+static int set_tracing_sleep_time(struct perf_ftrace *ftrace)
+{
+       if (!ftrace->graph_nosleep_time)
+               return 0;
+
+       if (write_tracing_option_file("sleep-time", "0") < 0)
+               return -1;
+
+       return 0;
+}
+
+static int set_tracing_funcgraph_irqs(struct perf_ftrace *ftrace)
+{
+       if (!ftrace->graph_noirqs)
+               return 0;
 
-       if (write_tracing_file("max_graph_depth", buf) < 0)
+       if (write_tracing_option_file("funcgraph-irqs", "0") < 0)
                return -1;
 
        return 0;
@@ -302,6 +451,9 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
        signal(SIGCHLD, sig_handler);
        signal(SIGPIPE, sig_handler);
 
+       if (ftrace->list_avail_functions)
+               return read_tracing_file_to_stdout("available_filter_functions");
+
        if (reset_tracing_files(ftrace) < 0) {
                pr_err("failed to reset ftrace\n");
                goto out;
@@ -327,6 +479,16 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
                goto out_reset;
        }
 
+       if (set_tracing_func_stack_trace(ftrace) < 0) {
+               pr_err("failed to set tracing option func_stack_trace\n");
+               goto out_reset;
+       }
+
+       if (set_tracing_func_irqinfo(ftrace) < 0) {
+               pr_err("failed to set tracing option irq-info\n");
+               goto out_reset;
+       }
+
        if (set_tracing_filters(ftrace) < 0) {
                pr_err("failed to set tracing filters\n");
                goto out_reset;
@@ -337,6 +499,26 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
                goto out_reset;
        }
 
+       if (set_tracing_percpu_buffer_size(ftrace) < 0) {
+               pr_err("failed to set tracing per-cpu buffer size\n");
+               goto out_reset;
+       }
+
+       if (set_tracing_trace_inherit(ftrace) < 0) {
+               pr_err("failed to set tracing option function-fork\n");
+               goto out_reset;
+       }
+
+       if (set_tracing_sleep_time(ftrace) < 0) {
+               pr_err("failed to set tracing option sleep-time\n");
+               goto out_reset;
+       }
+
+       if (set_tracing_funcgraph_irqs(ftrace) < 0) {
+               pr_err("failed to set tracing option funcgraph-irqs\n");
+               goto out_reset;
+       }
+
        if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
                pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
                goto out_reset;
@@ -362,6 +544,9 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
        fcntl(trace_fd, F_SETFL, O_NONBLOCK);
        pollfd.fd = trace_fd;
 
+       /* display column headers */
+       read_tracing_file_to_stdout("trace");
+
        if (write_tracing_file("tracing_on", "1") < 0) {
                pr_err("can't enable tracing\n");
                goto out_close_fd;
@@ -455,6 +640,96 @@ static void delete_filter_func(struct list_head *head)
        }
 }
 
+static int parse_buffer_size(const struct option *opt,
+                            const char *str, int unset)
+{
+       unsigned long *s = (unsigned long *)opt->value;
+       static struct parse_tag tags_size[] = {
+               { .tag  = 'B', .mult = 1       },
+               { .tag  = 'K', .mult = 1 << 10 },
+               { .tag  = 'M', .mult = 1 << 20 },
+               { .tag  = 'G', .mult = 1 << 30 },
+               { .tag  = 0 },
+       };
+       unsigned long val;
+
+       if (unset) {
+               *s = 0;
+               return 0;
+       }
+
+       val = parse_tag_value(str, tags_size);
+       if (val != (unsigned long) -1) {
+               if (val < 1024) {
+                       pr_err("buffer size too small, must larger than 1KB.");
+                       return -1;
+               }
+               *s = val;
+               return 0;
+       }
+
+       return -1;
+}
+
+static int parse_func_tracer_opts(const struct option *opt,
+                                 const char *str, int unset)
+{
+       int ret;
+       struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value;
+       struct sublevel_option func_tracer_opts[] = {
+               { .name = "call-graph", .value_ptr = &ftrace->func_stack_trace },
+               { .name = "irq-info",   .value_ptr = &ftrace->func_irq_info },
+               { .name = NULL, }
+       };
+
+       if (unset)
+               return 0;
+
+       ret = perf_parse_sublevel_options(str, func_tracer_opts);
+       if (ret)
+               return ret;
+
+       return 0;
+}
+
+static int parse_graph_tracer_opts(const struct option *opt,
+                                 const char *str, int unset)
+{
+       int ret;
+       struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value;
+       struct sublevel_option graph_tracer_opts[] = {
+               { .name = "nosleep-time",       .value_ptr = &ftrace->graph_nosleep_time },
+               { .name = "noirqs",             .value_ptr = &ftrace->graph_noirqs },
+               { .name = NULL, }
+       };
+
+       if (unset)
+               return 0;
+
+       ret = perf_parse_sublevel_options(str, graph_tracer_opts);
+       if (ret)
+               return ret;
+
+       return 0;
+}
+
+static void select_tracer(struct perf_ftrace *ftrace)
+{
+       bool graph = !list_empty(&ftrace->graph_funcs) ||
+                    !list_empty(&ftrace->nograph_funcs);
+       bool func = !list_empty(&ftrace->filters) ||
+                   !list_empty(&ftrace->notrace);
+
+       /* The function_graph has priority over function tracer. */
+       if (graph)
+               ftrace->tracer = "function_graph";
+       else if (func)
+               ftrace->tracer = "function";
+       /* Otherwise, the default tracer is used. */
+
+       pr_debug("%s tracer is used\n", ftrace->tracer);
+}
+
 int cmd_ftrace(int argc, const char **argv)
 {
        int ret;
@@ -470,6 +745,8 @@ int cmd_ftrace(int argc, const char **argv)
        const struct option ftrace_options[] = {
        OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
                   "tracer to use: function_graph(default) or function"),
+       OPT_BOOLEAN('F', "funcs", &ftrace.list_avail_functions,
+                   "Show available functions to filter"),
        OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
                   "trace on existing process id"),
        OPT_INCR('v', "verbose", &verbose,
@@ -479,15 +756,27 @@ int cmd_ftrace(int argc, const char **argv)
        OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
                    "list of cpus to monitor"),
        OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
-                    "trace given functions only", parse_filter_func),
+                    "trace given functions using function tracer",
+                    parse_filter_func),
        OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
                     "do not trace given functions", parse_filter_func),
+       OPT_CALLBACK(0, "func-opts", &ftrace, "options",
+                    "function tracer options, available options: call-graph,irq-info",
+                    parse_func_tracer_opts),
        OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
-                    "Set graph filter on given functions", parse_filter_func),
+                    "trace given functions using function_graph tracer",
+                    parse_filter_func),
        OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
                     "Set nograph filter on given functions", parse_filter_func),
        OPT_INTEGER('D', "graph-depth", &ftrace.graph_depth,
                    "Max depth for function graph tracer"),
+       OPT_CALLBACK(0, "graph-opts", &ftrace, "options",
+                    "graph tracer options, available options: nosleep-time,noirqs",
+                    parse_graph_tracer_opts),
+       OPT_CALLBACK('m', "buffer-size", &ftrace.percpu_buffer_size, "size",
+                    "size of per cpu buffer", parse_buffer_size),
+       OPT_BOOLEAN(0, "inherit", &ftrace.inherit,
+                   "trace children processes"),
        OPT_END()
        };
 
@@ -505,6 +794,8 @@ int cmd_ftrace(int argc, const char **argv)
        if (!argc && target__none(&ftrace.target))
                ftrace.target.system_wide = true;
 
+       select_tracer(&ftrace);
+
        ret = target__validate(&ftrace.target);
        if (ret) {
                char errbuf[512];