4 * Copyright (C) 2007 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
8 #include <linux/kallsyms.h>
9 #include <linux/debugfs.h>
10 #include <linux/hrtimer.h>
11 #include <linux/uaccess.h>
12 #include <linux/ftrace.h>
13 #include <linux/module.h>
18 static struct trace_array *ctx_trace;
19 static int __read_mostly tracer_enabled;
21 static const unsigned long sample_period = 1000000;
24 * Per CPU hrtimers that do the profiling:
26 static DEFINE_PER_CPU(struct hrtimer, stack_trace_hrtimer);
28 static enum hrtimer_restart stack_trace_timer_fn(struct hrtimer *hrtimer)
33 hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
35 return HRTIMER_RESTART;
38 static void start_stack_timer(int cpu)
40 struct hrtimer *hrtimer = &per_cpu(stack_trace_hrtimer, cpu);
42 hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
43 hrtimer->function = stack_trace_timer_fn;
44 hrtimer->cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
46 hrtimer_start(hrtimer, ns_to_ktime(sample_period), HRTIMER_MODE_REL);
49 static void start_stack_timers(void)
51 cpumask_t saved_mask = current->cpus_allowed;
54 for_each_online_cpu(cpu) {
55 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
56 start_stack_timer(cpu);
57 printk("started timer on cpu%d\n", cpu);
59 set_cpus_allowed_ptr(current, &saved_mask);
62 static void stop_stack_timer(int cpu)
64 struct hrtimer *hrtimer = &per_cpu(stack_trace_hrtimer, cpu);
66 hrtimer_cancel(hrtimer);
67 printk("cancelled timer on cpu%d\n", cpu);
70 static void stop_stack_timers(void)
74 for_each_online_cpu(cpu)
75 stop_stack_timer(cpu);
78 static notrace void stack_reset(struct trace_array *tr)
82 tr->time_start = ftrace_now(tr->cpu);
84 for_each_online_cpu(cpu)
85 tracing_reset(tr->data[cpu]);
88 static notrace void start_stack_trace(struct trace_array *tr)
95 static notrace void stop_stack_trace(struct trace_array *tr)
101 static notrace void stack_trace_init(struct trace_array *tr)
106 start_stack_trace(tr);
109 static notrace void stack_trace_reset(struct trace_array *tr)
112 stop_stack_trace(tr);
115 static void stack_trace_ctrl_update(struct trace_array *tr)
117 /* When starting a new trace, reset the buffers */
119 start_stack_trace(tr);
121 stop_stack_trace(tr);
124 static struct tracer stack_trace __read_mostly =
127 .init = stack_trace_init,
128 .reset = stack_trace_reset,
129 .ctrl_update = stack_trace_ctrl_update,
130 #ifdef CONFIG_FTRACE_SELFTEST
131 .selftest = trace_selftest_startup_stack,
135 __init static int init_stack_trace(void)
137 return register_tracer(&stack_trace);
139 device_initcall(init_stack_trace);