trace_sysprof.c 6.5 KB
Newer Older
I
Ingo Molnar 已提交
1 2 3
/*
 * trace stack traces
 *
I
Ingo Molnar 已提交
4
 * Copyright (C) 2004-2008, Soeren Sandmann
I
Ingo Molnar 已提交
5 6 7 8
 * Copyright (C) 2007 Steven Rostedt <srostedt@redhat.com>
 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
 */
#include <linux/kallsyms.h>
I
Ingo Molnar 已提交
9 10
#include <linux/debugfs.h>
#include <linux/hrtimer.h>
I
Ingo Molnar 已提交
11 12
#include <linux/uaccess.h>
#include <linux/ftrace.h>
I
Ingo Molnar 已提交
13
#include <linux/module.h>
14
#include <linux/irq.h>
I
Ingo Molnar 已提交
15
#include <linux/fs.h>
I
Ingo Molnar 已提交
16

S
Soeren Sandmann Pedersen 已提交
17 18
#include <asm/stacktrace.h>

I
Ingo Molnar 已提交
19 20
#include "trace.h"

21
static struct trace_array	*sysprof_trace;
I
Ingo Molnar 已提交
22 23
static int __read_mostly	tracer_enabled;

24
/*
I
Ingo Molnar 已提交
25
 * 1 msec sample interval by default:
26
 */
I
Ingo Molnar 已提交
27
static unsigned long sample_period = 1000000;
I
Ingo Molnar 已提交
28
static const unsigned int sample_max_depth = 512;
I
Ingo Molnar 已提交
29

I
Ingo Molnar 已提交
30
static DEFINE_MUTEX(sample_timer_lock);
I
Ingo Molnar 已提交
31 32 33 34 35
/*
 * Per CPU hrtimers that do the profiling:
 */
static DEFINE_PER_CPU(struct hrtimer, stack_trace_hrtimer);

36
struct stack_frame_user {
37 38 39 40
	const void __user	*next_fp;
	unsigned long		return_address;
};

41 42
static int
copy_stack_frame(const void __user *fp, struct stack_frame_user *frame)
43
{
I
Ingo Molnar 已提交
44 45
	int ret;

46 47 48
	if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
		return 0;

I
Ingo Molnar 已提交
49 50 51 52 53
	ret = 1;
	pagefault_disable();
	if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
		ret = 0;
	pagefault_enable();
54

I
Ingo Molnar 已提交
55
	return ret;
56 57
}

S
Soeren Sandmann Pedersen 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
struct backtrace_info {
	struct trace_array_cpu	*data;
	struct trace_array	*tr;
	int			pos;
};

static void
backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
{
	/* Ignore warnings */
}

static void backtrace_warning(void *data, char *msg)
{
	/* Ignore warnings */
}

static int backtrace_stack(void *data, char *name)
{
	/* Don't bother with IRQ stacks for now */
	return -1;
}

static void backtrace_address(void *data, unsigned long addr, int reliable)
{
	struct backtrace_info *info = data;

	if (info->pos < sample_max_depth && reliable) {
		__trace_special(info->tr, info->data, 1, addr, 0);

		info->pos++;
	}
}

92
static const struct stacktrace_ops backtrace_ops = {
S
Soeren Sandmann Pedersen 已提交
93 94 95 96
	.warning		= backtrace_warning,
	.warning_symbol		= backtrace_warning_symbol,
	.stack			= backtrace_stack,
	.address		= backtrace_address,
97
	.walk_stack		= print_context_stack,
S
Soeren Sandmann Pedersen 已提交
98 99
};

100
static int
S
Soeren Sandmann Pedersen 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
trace_kernel(struct pt_regs *regs, struct trace_array *tr,
	     struct trace_array_cpu *data)
{
	struct backtrace_info info;
	unsigned long bp;
	char *stack;

	info.tr = tr;
	info.data = data;
	info.pos = 1;

	__trace_special(info.tr, info.data, 1, regs->ip, 0);

	stack = ((char *)regs + sizeof(struct pt_regs));
#ifdef CONFIG_FRAME_POINTER
	bp = regs->bp;
#else
	bp = 0;
#endif

	dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, &info);

123
	return info.pos;
S
Soeren Sandmann Pedersen 已提交
124 125
}

126 127 128
static void timer_notify(struct pt_regs *regs, int cpu)
{
	struct trace_array_cpu *data;
129
	struct stack_frame_user frame;
130
	struct trace_array *tr;
I
Ingo Molnar 已提交
131
	const void __user *fp;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	int is_user;
	int i;

	if (!regs)
		return;

	tr = sysprof_trace;
	data = tr->data[cpu];
	is_user = user_mode(regs);

	if (!current || current->pid == 0)
		return;

	if (is_user && current->state != TASK_RUNNING)
		return;

S
Soeren Sandmann Pedersen 已提交
148
	__trace_special(tr, data, 0, 0, current->pid);
149

S
Soeren Sandmann Pedersen 已提交
150
	if (!is_user)
151 152 153
		i = trace_kernel(regs, tr, data);
	else
		i = 0;
154

155 156 157 158 159
	/*
	 * Trace user stack if we are not a kernel thread
	 */
	if (current->mm && i < sample_max_depth) {
		regs = (struct pt_regs *)current->thread.sp0 - 1;
160

161
		fp = (void __user *)regs->bp;
S
Soeren Sandmann Pedersen 已提交
162

163
		__trace_special(tr, data, 2, regs->ip, 0);
164

165
		while (i < sample_max_depth) {
166
			frame.next_fp = NULL;
167 168 169 170 171
			frame.return_address = 0;
			if (!copy_stack_frame(fp, &frame))
				break;
			if ((unsigned long)fp < regs->sp)
				break;
172

173 174 175 176 177 178 179 180
			__trace_special(tr, data, 2, frame.return_address,
					(unsigned long)fp);
			fp = frame.next_fp;

			i++;
		}

	}
181

I
Ingo Molnar 已提交
182 183 184
	/*
	 * Special trace entry if we overflow the max depth:
	 */
I
Ingo Molnar 已提交
185
	if (i == sample_max_depth)
T
Thomas Gleixner 已提交
186
		__trace_special(tr, data, -1, -1, -1);
187 188

	__trace_special(tr, data, 3, current->pid, i);
189 190
}

I
Ingo Molnar 已提交
191 192 193
static enum hrtimer_restart stack_trace_timer_fn(struct hrtimer *hrtimer)
{
	/* trace here */
194
	timer_notify(get_irq_regs(), smp_processor_id());
I
Ingo Molnar 已提交
195 196 197 198 199 200

	hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));

	return HRTIMER_RESTART;
}

201
static void start_stack_timer(void *unused)
I
Ingo Molnar 已提交
202
{
203
	struct hrtimer *hrtimer = &__get_cpu_var(stack_trace_hrtimer);
I
Ingo Molnar 已提交
204 205 206 207

	hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
	hrtimer->function = stack_trace_timer_fn;

208 209
	hrtimer_start(hrtimer, ns_to_ktime(sample_period),
		      HRTIMER_MODE_REL_PINNED);
I
Ingo Molnar 已提交
210 211 212 213
}

static void start_stack_timers(void)
{
214
	on_each_cpu(start_stack_timer, NULL, 1);
I
Ingo Molnar 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
}

static void stop_stack_timer(int cpu)
{
	struct hrtimer *hrtimer = &per_cpu(stack_trace_hrtimer, cpu);

	hrtimer_cancel(hrtimer);
}

static void stop_stack_timers(void)
{
	int cpu;

	for_each_online_cpu(cpu)
		stop_stack_timer(cpu);
}

T
Thomas Gleixner 已提交
232
static void stop_stack_trace(struct trace_array *tr)
I
Ingo Molnar 已提交
233
{
I
Ingo Molnar 已提交
234
	mutex_lock(&sample_timer_lock);
I
Ingo Molnar 已提交
235
	stop_stack_timers();
I
Ingo Molnar 已提交
236
	tracer_enabled = 0;
I
Ingo Molnar 已提交
237
	mutex_unlock(&sample_timer_lock);
I
Ingo Molnar 已提交
238 239
}

240
static int stack_trace_init(struct trace_array *tr)
I
Ingo Molnar 已提交
241
{
242
	sysprof_trace = tr;
I
Ingo Molnar 已提交
243

244 245
	tracing_start_cmdline_record();

246 247 248 249
	mutex_lock(&sample_timer_lock);
	start_stack_timers();
	tracer_enabled = 1;
	mutex_unlock(&sample_timer_lock);
250
	return 0;
I
Ingo Molnar 已提交
251 252
}

T
Thomas Gleixner 已提交
253
static void stack_trace_reset(struct trace_array *tr)
I
Ingo Molnar 已提交
254
{
255
	tracing_stop_cmdline_record();
S
Steven Rostedt 已提交
256
	stop_stack_trace(tr);
I
Ingo Molnar 已提交
257 258 259 260 261 262 263 264
}

static struct tracer stack_trace __read_mostly =
{
	.name		= "sysprof",
	.init		= stack_trace_init,
	.reset		= stack_trace_reset,
#ifdef CONFIG_FTRACE_SELFTEST
265
	.selftest    = trace_selftest_startup_sysprof,
I
Ingo Molnar 已提交
266 267 268 269 270 271 272 273
#endif
};

__init static int init_stack_trace(void)
{
	return register_tracer(&stack_trace);
}
device_initcall(init_stack_trace);
I
Ingo Molnar 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

#define MAX_LONG_DIGITS 22

static ssize_t
sysprof_sample_read(struct file *filp, char __user *ubuf,
		    size_t cnt, loff_t *ppos)
{
	char buf[MAX_LONG_DIGITS];
	int r;

	r = sprintf(buf, "%ld\n", nsecs_to_usecs(sample_period));

	return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
}

static ssize_t
sysprof_sample_write(struct file *filp, const char __user *ubuf,
		     size_t cnt, loff_t *ppos)
{
	char buf[MAX_LONG_DIGITS];
	unsigned long val;

	if (cnt > MAX_LONG_DIGITS-1)
		cnt = MAX_LONG_DIGITS-1;

	if (copy_from_user(&buf, ubuf, cnt))
		return -EFAULT;

	buf[cnt] = 0;

	val = simple_strtoul(buf, NULL, 10);
	/*
	 * Enforce a minimum sample period of 100 usecs:
	 */
	if (val < 100)
		val = 100;

	mutex_lock(&sample_timer_lock);
	stop_stack_timers();
	sample_period = val * 1000;
	start_stack_timers();
	mutex_unlock(&sample_timer_lock);

	return cnt;
}

320
static const struct file_operations sysprof_sample_fops = {
I
Ingo Molnar 已提交
321 322 323 324 325 326 327
	.read		= sysprof_sample_read,
	.write		= sysprof_sample_write,
};

void init_tracer_sysprof_debugfs(struct dentry *d_tracer)
{

328
	trace_create_file("sysprof_sample_period", 0644,
I
Ingo Molnar 已提交
329 330
			d_tracer, NULL, &sysprof_sample_fops);
}