Merge branch 'linus' into tracing/hw-branch-tracing
[sfrench/cifs-2.6.git] / kernel / trace / trace_selftest.c
index bc8e80a86bca4c0cf730fcf6b7bc568f2808f7e7..499d01c44cd17b9a95e843b5b062b900185c4886 100644 (file)
@@ -1,5 +1,6 @@
 /* Include in trace.c */
 
+#include <linux/stringify.h>
 #include <linux/kthread.h>
 #include <linux/delay.h>
 
@@ -9,11 +10,13 @@ static inline int trace_valid_entry(struct trace_entry *entry)
        case TRACE_FN:
        case TRACE_CTX:
        case TRACE_WAKE:
-       case TRACE_CONT:
        case TRACE_STACK:
        case TRACE_PRINT:
        case TRACE_SPECIAL:
        case TRACE_BRANCH:
+       case TRACE_GRAPH_ENT:
+       case TRACE_GRAPH_RET:
+       case TRACE_HW_BRANCHES:
                return 1;
        }
        return 0;
@@ -99,9 +102,6 @@ static inline void warn_failed_init_tracer(struct tracer *trace, int init_ret)
 
 #ifdef CONFIG_DYNAMIC_FTRACE
 
-#define __STR(x) #x
-#define STR(x) __STR(x)
-
 /* Test dynamic code modification and ftrace filters */
 int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
                                           struct trace_array *tr,
@@ -125,17 +125,17 @@ int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
        func();
 
        /*
-        * Some archs *cough*PowerPC*cough* add charachters to the
+        * Some archs *cough*PowerPC*cough* add characters to the
         * start of the function names. We simply put a '*' to
-        * accomodate them.
+        * accommodate them.
         */
-       func_name = "*" STR(DYN_FTRACE_TEST_NAME);
+       func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
 
        /* filter only on our function */
        ftrace_set_filter(func_name, strlen(func_name), 1);
 
        /* enable tracing */
-       ret = trace->init(tr);
+       ret = tracer_init(trace, tr);
        if (ret) {
                warn_failed_init_tracer(trace, ret);
                goto out;
@@ -189,6 +189,7 @@ int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
 #else
 # define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
 #endif /* CONFIG_DYNAMIC_FTRACE */
+
 /*
  * Simple verification test of ftrace function tracer.
  * Enable ftrace, sleep 1/10 second, and then read the trace
@@ -209,7 +210,7 @@ trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
        ftrace_enabled = 1;
        tracer_enabled = 1;
 
-       ret = trace->init(tr);
+       ret = tracer_init(trace, tr);
        if (ret) {
                warn_failed_init_tracer(trace, ret);
                goto out;
@@ -247,6 +248,90 @@ trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
 }
 #endif /* CONFIG_FUNCTION_TRACER */
 
+
+#ifdef CONFIG_FUNCTION_GRAPH_TRACER
+
+/* Maximum number of functions to trace before diagnosing a hang */
+#define GRAPH_MAX_FUNC_TEST    100000000
+
+static void __ftrace_dump(bool disable_tracing);
+static unsigned int graph_hang_thresh;
+
+/* Wrap the real function entry probe to avoid possible hanging */
+static int trace_graph_entry_watchdog(struct ftrace_graph_ent *trace)
+{
+       /* This is harmlessly racy, we want to approximately detect a hang */
+       if (unlikely(++graph_hang_thresh > GRAPH_MAX_FUNC_TEST)) {
+               ftrace_graph_stop();
+               printk(KERN_WARNING "BUG: Function graph tracer hang!\n");
+               if (ftrace_dump_on_oops)
+                       __ftrace_dump(false);
+               return 0;
+       }
+
+       return trace_graph_entry(trace);
+}
+
+/*
+ * Pretty much the same than for the function tracer from which the selftest
+ * has been borrowed.
+ */
+int
+trace_selftest_startup_function_graph(struct tracer *trace,
+                                       struct trace_array *tr)
+{
+       int ret;
+       unsigned long count;
+
+       /*
+        * Simulate the init() callback but we attach a watchdog callback
+        * to detect and recover from possible hangs
+        */
+       tracing_reset_online_cpus(tr);
+       ret = register_ftrace_graph(&trace_graph_return,
+                                   &trace_graph_entry_watchdog);
+       if (ret) {
+               warn_failed_init_tracer(trace, ret);
+               goto out;
+       }
+       tracing_start_cmdline_record();
+
+       /* Sleep for a 1/10 of a second */
+       msleep(100);
+
+       /* Have we just recovered from a hang? */
+       if (graph_hang_thresh > GRAPH_MAX_FUNC_TEST) {
+               tracing_selftest_disabled = true;
+               ret = -1;
+               goto out;
+       }
+
+       tracing_stop();
+
+       /* check the trace buffer */
+       ret = trace_test_buffer(tr, &count);
+
+       trace->reset(tr);
+       tracing_start();
+
+       if (!ret && !count) {
+               printk(KERN_CONT ".. no entries found ..");
+               ret = -1;
+               goto out;
+       }
+
+       /* Don't test dynamic tracing, the function tracer already did */
+
+out:
+       /* Stop it if we failed */
+       if (ret)
+               ftrace_graph_stop();
+
+       return ret;
+}
+#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
+
+
 #ifdef CONFIG_IRQSOFF_TRACER
 int
 trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
@@ -256,7 +341,7 @@ trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
        int ret;
 
        /* start the tracing */
-       ret = trace->init(tr);
+       ret = tracer_init(trace, tr);
        if (ret) {
                warn_failed_init_tracer(trace, ret);
                return ret;
@@ -268,6 +353,14 @@ trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
        local_irq_disable();
        udelay(100);
        local_irq_enable();
+
+       /*
+        * Stop the tracer to avoid a warning subsequent
+        * to buffer flipping failure because tracing_stop()
+        * disables the tr and max buffers, making flipping impossible
+        * in case of parallels max irqs off latencies.
+        */
+       trace->stop(tr);
        /* stop the tracing. */
        tracing_stop();
        /* check both trace buffers */
@@ -310,7 +403,7 @@ trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
        }
 
        /* start the tracing */
-       ret = trace->init(tr);
+       ret = tracer_init(trace, tr);
        if (ret) {
                warn_failed_init_tracer(trace, ret);
                return ret;
@@ -322,6 +415,14 @@ trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
        preempt_disable();
        udelay(100);
        preempt_enable();
+
+       /*
+        * Stop the tracer to avoid a warning subsequent
+        * to buffer flipping failure because tracing_stop()
+        * disables the tr and max buffers, making flipping impossible
+        * in case of parallels max preempt off latencies.
+        */
+       trace->stop(tr);
        /* stop the tracing. */
        tracing_stop();
        /* check both trace buffers */
@@ -364,10 +465,10 @@ trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *
        }
 
        /* start the tracing */
-       ret = trace->init(tr);
+       ret = tracer_init(trace, tr);
        if (ret) {
                warn_failed_init_tracer(trace, ret);
-               goto out;
+               goto out_no_start;
        }
 
        /* reset the max latency */
@@ -381,31 +482,35 @@ trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *
        /* reverse the order of preempt vs irqs */
        local_irq_enable();
 
+       /*
+        * Stop the tracer to avoid a warning subsequent
+        * to buffer flipping failure because tracing_stop()
+        * disables the tr and max buffers, making flipping impossible
+        * in case of parallels max irqs/preempt off latencies.
+        */
+       trace->stop(tr);
        /* stop the tracing. */
        tracing_stop();
        /* check both trace buffers */
        ret = trace_test_buffer(tr, NULL);
-       if (ret) {
-               tracing_start();
+       if (ret)
                goto out;
-       }
 
        ret = trace_test_buffer(&max_tr, &count);
-       if (ret) {
-               tracing_start();
+       if (ret)
                goto out;
-       }
 
        if (!ret && !count) {
                printk(KERN_CONT ".. no entries found ..");
                ret = -1;
-               tracing_start();
                goto out;
        }
 
        /* do the test by disabling interrupts first this time */
        tracing_max_latency = 0;
        tracing_start();
+       trace->start(tr);
+
        preempt_disable();
        local_irq_disable();
        udelay(100);
@@ -413,6 +518,7 @@ trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *
        /* reverse the order of preempt vs irqs */
        local_irq_enable();
 
+       trace->stop(tr);
        /* stop the tracing. */
        tracing_stop();
        /* check both trace buffers */
@@ -428,9 +534,10 @@ trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *
                goto out;
        }
 
- out:
-       trace->reset(tr);
+out:
        tracing_start();
+out_no_start:
+       trace->reset(tr);
        tracing_max_latency = save_max;
 
        return ret;
@@ -496,7 +603,7 @@ trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
        wait_for_completion(&isrt);
 
        /* start the tracing */
-       ret = trace->init(tr);
+       ret = tracer_init(trace, tr);
        if (ret) {
                warn_failed_init_tracer(trace, ret);
                return ret;
@@ -557,7 +664,7 @@ trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr
        int ret;
 
        /* start the tracing */
-       ret = trace->init(tr);
+       ret = tracer_init(trace, tr);
        if (ret) {
                warn_failed_init_tracer(trace, ret);
                return ret;
@@ -589,10 +696,10 @@ trace_selftest_startup_sysprof(struct tracer *trace, struct trace_array *tr)
        int ret;
 
        /* start the tracing */
-       ret = trace->init(tr);
+       ret = tracer_init(trace, tr);
        if (ret) {
                warn_failed_init_tracer(trace, ret);
-               return 0;
+               return ret;
        }
 
        /* Sleep for a 1/10 of a second */
@@ -604,6 +711,11 @@ trace_selftest_startup_sysprof(struct tracer *trace, struct trace_array *tr)
        trace->reset(tr);
        tracing_start();
 
+       if (!ret && !count) {
+               printk(KERN_CONT ".. no entries found ..");
+               ret = -1;
+       }
+
        return ret;
 }
 #endif /* CONFIG_SYSPROF_TRACER */
@@ -616,7 +728,7 @@ trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr)
        int ret;
 
        /* start the tracing */
-       ret = trace->init(tr);
+       ret = tracer_init(trace, tr);
        if (ret) {
                warn_failed_init_tracer(trace, ret);
                return ret;
@@ -631,6 +743,63 @@ trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr)
        trace->reset(tr);
        tracing_start();
 
+       if (!ret && !count) {
+               printk(KERN_CONT ".. no entries found ..");
+               ret = -1;
+       }
+
        return ret;
 }
 #endif /* CONFIG_BRANCH_TRACER */
+
+#ifdef CONFIG_HW_BRANCH_TRACER
+int
+trace_selftest_startup_hw_branches(struct tracer *trace,
+                                  struct trace_array *tr)
+{
+       struct trace_iterator iter;
+       struct tracer tracer;
+       unsigned long count;
+       int ret;
+
+       if (!trace->open) {
+               printk(KERN_CONT "missing open function...");
+               return -1;
+       }
+
+       ret = tracer_init(trace, tr);
+       if (ret) {
+               warn_failed_init_tracer(trace, ret);
+               return ret;
+       }
+
+       /*
+        * The hw-branch tracer needs to collect the trace from the various
+        * cpu trace buffers - before tracing is stopped.
+        */
+       memset(&iter, 0, sizeof(iter));
+       memcpy(&tracer, trace, sizeof(tracer));
+
+       iter.trace = &tracer;
+       iter.tr = tr;
+       iter.pos = -1;
+       mutex_init(&iter.mutex);
+
+       trace->open(&iter);
+
+       mutex_destroy(&iter.mutex);
+
+       tracing_stop();
+
+       ret = trace_test_buffer(tr, &count);
+       trace->reset(tr);
+       tracing_start();
+
+       if (!ret && !count) {
+               printk(KERN_CONT "no entries found..");
+               ret = -1;
+       }
+
+       return ret;
+}
+#endif /* CONFIG_HW_BRANCH_TRACER */