sysprof: kernel trace
[deliverable/linux.git] / kernel / trace / trace_sysprof.c
CommitLineData
f06c3810
IM
1/*
2 * trace stack traces
3 *
4 * Copyright (C) 2007 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
56a08bdc 6 * Copyright (C) 2004, 2005, Soeren Sandmann
f06c3810 7 */
f06c3810 8#include <linux/kallsyms.h>
0075fa80
IM
9#include <linux/debugfs.h>
10#include <linux/hrtimer.h>
f06c3810 11#include <linux/uaccess.h>
f06c3810 12#include <linux/ftrace.h>
0075fa80 13#include <linux/module.h>
56a08bdc 14#include <linux/irq.h>
0075fa80 15#include <linux/fs.h>
f06c3810 16
cd2134b1
SSP
17#include <asm/stacktrace.h>
18
f06c3810
IM
19#include "trace.h"
20
56a08bdc 21static struct trace_array *sysprof_trace;
f06c3810
IM
22static int __read_mostly tracer_enabled;
23
56a08bdc 24/*
d618b3e6 25 * 1 msec sample interval by default:
56a08bdc 26 */
d618b3e6 27static unsigned long sample_period = 1000000;
842af315 28static const unsigned int sample_max_depth = 512;
0075fa80 29
d618b3e6 30static DEFINE_MUTEX(sample_timer_lock);
0075fa80
IM
31/*
32 * Per CPU hrtimers that do the profiling:
33 */
34static DEFINE_PER_CPU(struct hrtimer, stack_trace_hrtimer);
35
56a08bdc
IM
36struct stack_frame {
37 const void __user *next_fp;
38 unsigned long return_address;
39};
40
41static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
42{
9f6b4e3f
IM
43 int ret;
44
56a08bdc
IM
45 if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
46 return 0;
47
9f6b4e3f
IM
48 ret = 1;
49 pagefault_disable();
50 if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
51 ret = 0;
52 pagefault_enable();
56a08bdc 53
9f6b4e3f 54 return ret;
56a08bdc
IM
55}
56
cd2134b1
SSP
57struct backtrace_info {
58 struct trace_array_cpu *data;
59 struct trace_array *tr;
60 int pos;
61};
62
63static void
64backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
65{
66 /* Ignore warnings */
67}
68
69static void backtrace_warning(void *data, char *msg)
70{
71 /* Ignore warnings */
72}
73
74static int backtrace_stack(void *data, char *name)
75{
76 /* Don't bother with IRQ stacks for now */
77 return -1;
78}
79
80static void backtrace_address(void *data, unsigned long addr, int reliable)
81{
82 struct backtrace_info *info = data;
83
84 if (info->pos < sample_max_depth && reliable) {
85 __trace_special(info->tr, info->data, 1, addr, 0);
86
87 info->pos++;
88 }
89}
90
91const static struct stacktrace_ops backtrace_ops = {
92 .warning = backtrace_warning,
93 .warning_symbol = backtrace_warning_symbol,
94 .stack = backtrace_stack,
95 .address = backtrace_address,
96};
97
98static struct pt_regs *
99trace_kernel(struct pt_regs *regs, struct trace_array *tr,
100 struct trace_array_cpu *data)
101{
102 struct backtrace_info info;
103 unsigned long bp;
104 char *user_stack;
105 char *stack;
106
107 info.tr = tr;
108 info.data = data;
109 info.pos = 1;
110
111 __trace_special(info.tr, info.data, 1, regs->ip, 0);
112
113 stack = ((char *)regs + sizeof(struct pt_regs));
114#ifdef CONFIG_FRAME_POINTER
115 bp = regs->bp;
116#else
117 bp = 0;
118#endif
119
120 dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, &info);
121
122 /* Now trace the user stack */
123 user_stack = ((char *)current->thread.sp0 - sizeof(struct pt_regs));
124
125 return (struct pt_regs *)user_stack;
126}
127
56a08bdc
IM
128static void timer_notify(struct pt_regs *regs, int cpu)
129{
56a08bdc
IM
130 struct trace_array_cpu *data;
131 struct stack_frame frame;
132 struct trace_array *tr;
9f6b4e3f 133 const void __user *fp;
56a08bdc
IM
134 int is_user;
135 int i;
136
137 if (!regs)
138 return;
139
140 tr = sysprof_trace;
141 data = tr->data[cpu];
142 is_user = user_mode(regs);
143
144 if (!current || current->pid == 0)
145 return;
146
147 if (is_user && current->state != TASK_RUNNING)
148 return;
149
cd2134b1 150 __trace_special(tr, data, 0, 0, current->pid);
56a08bdc 151
cd2134b1
SSP
152 if (!is_user)
153 regs = trace_kernel(regs, tr, data);
56a08bdc 154
9f6b4e3f 155 fp = (void __user *)regs->bp;
56a08bdc 156
cd2134b1
SSP
157 __trace_special(tr, data, 2, regs->ip, 0);
158
842af315 159 for (i = 0; i < sample_max_depth; i++) {
9f6b4e3f
IM
160 frame.next_fp = 0;
161 frame.return_address = 0;
162 if (!copy_stack_frame(fp, &frame))
56a08bdc 163 break;
9f6b4e3f 164 if ((unsigned long)fp < regs->sp)
56a08bdc
IM
165 break;
166
cd2134b1 167 __trace_special(tr, data, 2, frame.return_address,
9f6b4e3f
IM
168 (unsigned long)fp);
169 fp = frame.next_fp;
56a08bdc
IM
170 }
171
cd2134b1 172 __trace_special(tr, data, 3, current->pid, i);
56a08bdc 173
9f6b4e3f
IM
174 /*
175 * Special trace entry if we overflow the max depth:
176 */
842af315 177 if (i == sample_max_depth)
9caee613 178 __trace_special(tr, data, -1, -1, -1);
56a08bdc
IM
179}
180
0075fa80
IM
181static enum hrtimer_restart stack_trace_timer_fn(struct hrtimer *hrtimer)
182{
183 /* trace here */
56a08bdc 184 timer_notify(get_irq_regs(), smp_processor_id());
0075fa80
IM
185
186 hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
187
188 return HRTIMER_RESTART;
189}
190
191static void start_stack_timer(int cpu)
192{
193 struct hrtimer *hrtimer = &per_cpu(stack_trace_hrtimer, cpu);
194
195 hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
196 hrtimer->function = stack_trace_timer_fn;
197 hrtimer->cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
198
199 hrtimer_start(hrtimer, ns_to_ktime(sample_period), HRTIMER_MODE_REL);
200}
201
202static void start_stack_timers(void)
203{
204 cpumask_t saved_mask = current->cpus_allowed;
205 int cpu;
206
207 for_each_online_cpu(cpu) {
208 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
209 start_stack_timer(cpu);
0075fa80
IM
210 }
211 set_cpus_allowed_ptr(current, &saved_mask);
212}
213
214static void stop_stack_timer(int cpu)
215{
216 struct hrtimer *hrtimer = &per_cpu(stack_trace_hrtimer, cpu);
217
218 hrtimer_cancel(hrtimer);
0075fa80
IM
219}
220
221static void stop_stack_timers(void)
222{
223 int cpu;
224
225 for_each_online_cpu(cpu)
226 stop_stack_timer(cpu);
227}
228
ada6b835 229static void stack_reset(struct trace_array *tr)
f06c3810
IM
230{
231 int cpu;
232
233 tr->time_start = ftrace_now(tr->cpu);
234
235 for_each_online_cpu(cpu)
236 tracing_reset(tr->data[cpu]);
237}
238
ada6b835 239static void start_stack_trace(struct trace_array *tr)
f06c3810 240{
d618b3e6 241 mutex_lock(&sample_timer_lock);
f06c3810 242 stack_reset(tr);
0075fa80 243 start_stack_timers();
f06c3810 244 tracer_enabled = 1;
d618b3e6 245 mutex_unlock(&sample_timer_lock);
f06c3810
IM
246}
247
ada6b835 248static void stop_stack_trace(struct trace_array *tr)
f06c3810 249{
d618b3e6 250 mutex_lock(&sample_timer_lock);
0075fa80 251 stop_stack_timers();
f06c3810 252 tracer_enabled = 0;
d618b3e6 253 mutex_unlock(&sample_timer_lock);
f06c3810
IM
254}
255
ada6b835 256static void stack_trace_init(struct trace_array *tr)
f06c3810 257{
56a08bdc 258 sysprof_trace = tr;
f06c3810
IM
259
260 if (tr->ctrl)
261 start_stack_trace(tr);
262}
263
ada6b835 264static void stack_trace_reset(struct trace_array *tr)
f06c3810
IM
265{
266 if (tr->ctrl)
267 stop_stack_trace(tr);
268}
269
270static void stack_trace_ctrl_update(struct trace_array *tr)
271{
272 /* When starting a new trace, reset the buffers */
273 if (tr->ctrl)
274 start_stack_trace(tr);
275 else
276 stop_stack_trace(tr);
277}
278
279static struct tracer stack_trace __read_mostly =
280{
281 .name = "sysprof",
282 .init = stack_trace_init,
283 .reset = stack_trace_reset,
284 .ctrl_update = stack_trace_ctrl_update,
285#ifdef CONFIG_FTRACE_SELFTEST
a6dd24f8 286 .selftest = trace_selftest_startup_sysprof,
f06c3810
IM
287#endif
288};
289
290__init static int init_stack_trace(void)
291{
292 return register_tracer(&stack_trace);
293}
294device_initcall(init_stack_trace);
d618b3e6
IM
295
296#define MAX_LONG_DIGITS 22
297
298static ssize_t
299sysprof_sample_read(struct file *filp, char __user *ubuf,
300 size_t cnt, loff_t *ppos)
301{
302 char buf[MAX_LONG_DIGITS];
303 int r;
304
305 r = sprintf(buf, "%ld\n", nsecs_to_usecs(sample_period));
306
307 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
308}
309
310static ssize_t
311sysprof_sample_write(struct file *filp, const char __user *ubuf,
312 size_t cnt, loff_t *ppos)
313{
314 char buf[MAX_LONG_DIGITS];
315 unsigned long val;
316
317 if (cnt > MAX_LONG_DIGITS-1)
318 cnt = MAX_LONG_DIGITS-1;
319
320 if (copy_from_user(&buf, ubuf, cnt))
321 return -EFAULT;
322
323 buf[cnt] = 0;
324
325 val = simple_strtoul(buf, NULL, 10);
326 /*
327 * Enforce a minimum sample period of 100 usecs:
328 */
329 if (val < 100)
330 val = 100;
331
332 mutex_lock(&sample_timer_lock);
333 stop_stack_timers();
334 sample_period = val * 1000;
335 start_stack_timers();
336 mutex_unlock(&sample_timer_lock);
337
338 return cnt;
339}
340
341static struct file_operations sysprof_sample_fops = {
342 .read = sysprof_sample_read,
343 .write = sysprof_sample_write,
344};
345
346void init_tracer_sysprof_debugfs(struct dentry *d_tracer)
347{
348 struct dentry *entry;
349
350 entry = debugfs_create_file("sysprof_sample_period", 0644,
351 d_tracer, NULL, &sysprof_sample_fops);
352 if (entry)
353 return;
354 pr_warning("Could not create debugfs 'dyn_ftrace_total_info' entry\n");
355}
This page took 0.038121 seconds and 5 git commands to generate.