trace_hw_branches.c 7.0 KB
Newer Older
1
/*
I
Ingo Molnar 已提交
2
 * h/w branch tracer for x86 based on BTS
3
 *
4 5
 * Copyright (C) 2008-2009 Intel Corporation.
 * Markus Metzger <markus.t.metzger@gmail.com>, 2008-2009
6
 */
7
#include <linux/kallsyms.h>
8 9
#include <linux/debugfs.h>
#include <linux/ftrace.h>
10
#include <linux/module.h>
11 12
#include <linux/cpu.h>
#include <linux/smp.h>
13
#include <linux/fs.h>
14 15 16

#include <asm/ds.h>

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


21
#define BTS_BUFFER_SIZE (1 << 13)
22 23

static DEFINE_PER_CPU(struct bts_tracer *, tracer);
24
static DEFINE_PER_CPU(unsigned char[BTS_BUFFER_SIZE], buffer);
25 26 27

#define this_tracer per_cpu(tracer, smp_processor_id())

28 29
static int trace_hw_branches_enabled __read_mostly;
static int trace_hw_branches_suspended __read_mostly;
30
static struct trace_array *hw_branch_trace __read_mostly;
31

32

33
static void bts_trace_init_cpu(int cpu)
34
{
35 36 37
	per_cpu(tracer, cpu) =
		ds_request_bts_cpu(cpu, per_cpu(buffer, cpu), BTS_BUFFER_SIZE,
				   NULL, (size_t)-1, BTS_KERNEL);
38

39 40
	if (IS_ERR(per_cpu(tracer, cpu)))
		per_cpu(tracer, cpu) = NULL;
41 42
}

43
static int bts_trace_init(struct trace_array *tr)
44
{
45
	int cpu;
46

47
	hw_branch_trace = tr;
48
	trace_hw_branches_enabled = 0;
49

50 51 52
	get_online_cpus();
	for_each_online_cpu(cpu) {
		bts_trace_init_cpu(cpu);
53

54 55 56
		if (likely(per_cpu(tracer, cpu)))
			trace_hw_branches_enabled = 1;
	}
57
	trace_hw_branches_suspended = 0;
58
	put_online_cpus();
59 60

	/* If we could not enable tracing on a single cpu, we fail. */
61
	return trace_hw_branches_enabled ? 0 : -EOPNOTSUPP;
62 63
}

64
static void bts_trace_reset(struct trace_array *tr)
65
{
66
	int cpu;
67

68 69 70 71 72 73
	get_online_cpus();
	for_each_online_cpu(cpu) {
		if (likely(per_cpu(tracer, cpu))) {
			ds_release_bts(per_cpu(tracer, cpu));
			per_cpu(tracer, cpu) = NULL;
		}
74
	}
75
	trace_hw_branches_enabled = 0;
76
	trace_hw_branches_suspended = 0;
77
	put_online_cpus();
78 79
}

80
static void bts_trace_start(struct trace_array *tr)
81
{
82
	int cpu;
83

84 85 86 87
	get_online_cpus();
	for_each_online_cpu(cpu)
		if (likely(per_cpu(tracer, cpu)))
			ds_resume_bts(per_cpu(tracer, cpu));
88
	trace_hw_branches_suspended = 0;
89
	put_online_cpus();
90
}
91

92 93
static void bts_trace_stop(struct trace_array *tr)
{
94
	int cpu;
95

96 97 98 99
	get_online_cpus();
	for_each_online_cpu(cpu)
		if (likely(per_cpu(tracer, cpu)))
			ds_suspend_bts(per_cpu(tracer, cpu));
100
	trace_hw_branches_suspended = 1;
101
	put_online_cpus();
102 103 104 105 106
}

static int __cpuinit bts_hotcpu_handler(struct notifier_block *nfb,
				     unsigned long action, void *hcpu)
{
107
	int cpu = (long)hcpu;
108 109 110 111

	switch (action) {
	case CPU_ONLINE:
	case CPU_DOWN_FAILED:
112 113 114 115 116 117 118 119
		/* The notification is sent with interrupts enabled. */
		if (trace_hw_branches_enabled) {
			bts_trace_init_cpu(cpu);

			if (trace_hw_branches_suspended &&
			    likely(per_cpu(tracer, cpu)))
				ds_suspend_bts(per_cpu(tracer, cpu));
		}
120
		break;
121

122
	case CPU_DOWN_PREPARE:
123 124 125 126 127
		/* The notification is sent with interrupts enabled. */
		if (likely(per_cpu(tracer, cpu))) {
			ds_release_bts(per_cpu(tracer, cpu));
			per_cpu(tracer, cpu) = NULL;
		}
128 129 130
	}

	return NOTIFY_DONE;
131 132
}

133 134 135 136
static struct notifier_block bts_hotcpu_notifier __cpuinitdata = {
	.notifier_call = bts_hotcpu_handler
};

137 138
static void bts_trace_print_header(struct seq_file *m)
{
139
	seq_puts(m, "# CPU#        TO  <-  FROM\n");
140 141 142 143
}

static enum print_line_t bts_trace_print_line(struct trace_iterator *iter)
{
I
Ingo Molnar 已提交
144
	unsigned long symflags = TRACE_ITER_SYM_OFFSET;
145 146
	struct trace_entry *entry = iter->ent;
	struct trace_seq *seq = &iter->seq;
147
	struct hw_branch_entry *it;
148 149 150

	trace_assign_type(it, entry);

151
	if (entry->type == TRACE_HW_BRANCHES) {
152
		if (trace_seq_printf(seq, "%4d  ", iter->cpu) &&
153 154 155
		    seq_print_ip_sym(seq, it->to, symflags) &&
		    trace_seq_printf(seq, "\t  <-  ") &&
		    seq_print_ip_sym(seq, it->from, symflags) &&
156 157
		    trace_seq_printf(seq, "\n"))
			return TRACE_TYPE_HANDLED;
158
		return TRACE_TYPE_PARTIAL_LINE;
159 160 161 162
	}
	return TRACE_TYPE_UNHANDLED;
}

163
void trace_hw_branch(u64 from, u64 to)
164
{
165
	struct ftrace_event_call *call = &event_hw_branch;
166
	struct trace_array *tr = hw_branch_trace;
167
	struct ring_buffer_event *event;
168
	struct hw_branch_entry *entry;
169
	unsigned long irq1;
170
	int cpu;
171

172 173 174 175
	if (unlikely(!tr))
		return;

	if (unlikely(!trace_hw_branches_enabled))
176
		return;
177 178 179 180 181 182

	local_irq_save(irq1);
	cpu = raw_smp_processor_id();
	if (atomic_inc_return(&tr->data[cpu]->disabled) != 1)
		goto out;

183 184
	event = trace_buffer_lock_reserve(tr, TRACE_HW_BRANCHES,
					  sizeof(*entry), 0, 0);
185 186
	if (!event)
		goto out;
187 188
	entry	= ring_buffer_event_data(event);
	tracing_generic_entry_update(&entry->ent, 0, from);
189
	entry->ent.type = TRACE_HW_BRANCHES;
190 191
	entry->from = from;
	entry->to   = to;
192 193
	if (!filter_check_discard(call, entry, tr->buffer, event))
		trace_buffer_unlock_commit(tr, event, 0, 0);
194 195 196 197

 out:
	atomic_dec(&tr->data[cpu]->disabled);
	local_irq_restore(irq1);
198 199
}

200
static void trace_bts_at(const struct bts_trace *trace, void *at)
201
{
202 203
	struct bts_struct bts;
	int err = 0;
204

205 206
	WARN_ON_ONCE(!trace->read);
	if (!trace->read)
207 208
		return;

209 210 211
	err = trace->read(this_tracer, at, &bts);
	if (err < 0)
		return;
212

213 214
	switch (bts.qualifier) {
	case BTS_BRANCH:
215
		trace_hw_branch(bts.variant.lbr.from, bts.variant.lbr.to);
216 217
		break;
	}
218 219
}

220 221 222
/*
 * Collect the trace on the current cpu and write it into the ftrace buffer.
 *
223
 * pre: tracing must be suspended on the current cpu
224
 */
225 226
static void trace_bts_cpu(void *arg)
{
227
	struct trace_array *tr = (struct trace_array *)arg;
228 229
	const struct bts_trace *trace;
	unsigned char *at;
230

231
	if (unlikely(!tr))
232 233
		return;

234 235 236
	if (unlikely(atomic_read(&tr->data[raw_smp_processor_id()]->disabled)))
		return;

237 238 239
	if (unlikely(!this_tracer))
		return;

240 241
	trace = ds_read_bts(this_tracer);
	if (!trace)
242
		return;
243

244 245
	for (at = trace->ds.top; (void *)at < trace->ds.end;
	     at += trace->ds.size)
246
		trace_bts_at(trace, at);
247

248 249
	for (at = trace->ds.begin; (void *)at < trace->ds.top;
	     at += trace->ds.size)
250
		trace_bts_at(trace, at);
251 252 253 254
}

static void trace_bts_prepare(struct trace_iterator *iter)
{
255
	int cpu;
256

257 258 259 260 261 262 263 264 265
	get_online_cpus();
	for_each_online_cpu(cpu)
		if (likely(per_cpu(tracer, cpu)))
			ds_suspend_bts(per_cpu(tracer, cpu));
	/*
	 * We need to collect the trace on the respective cpu since ftrace
	 * implicitly adds the record for the current cpu.
	 * Once that is more flexible, we could collect the data from any cpu.
	 */
266
	on_each_cpu(trace_bts_cpu, iter->tr, 1);
267

268 269 270 271
	for_each_online_cpu(cpu)
		if (likely(per_cpu(tracer, cpu)))
			ds_resume_bts(per_cpu(tracer, cpu));
	put_online_cpus();
272 273
}

274 275 276 277 278
static void trace_bts_close(struct trace_iterator *iter)
{
	tracing_reset_online_cpus(iter->tr);
}

279 280
void trace_hw_branch_oops(void)
{
281 282
	if (this_tracer) {
		ds_suspend_bts_noirq(this_tracer);
283
		trace_bts_cpu(hw_branch_trace);
284 285
		ds_resume_bts_noirq(this_tracer);
	}
286 287
}

288 289
struct tracer bts_tracer __read_mostly =
{
290
	.name		= "hw-branch-tracer",
291
	.init		= bts_trace_init,
292
	.reset		= bts_trace_reset,
293 294 295 296
	.print_header	= bts_trace_print_header,
	.print_line	= bts_trace_print_line,
	.start		= bts_trace_start,
	.stop		= bts_trace_stop,
297
	.open		= trace_bts_prepare,
298 299 300 301
	.close		= trace_bts_close,
#ifdef CONFIG_FTRACE_SELFTEST
	.selftest	= trace_selftest_startup_hw_branches,
#endif /* CONFIG_FTRACE_SELFTEST */
302 303 304 305
};

__init static int init_bts_trace(void)
{
306
	register_hotcpu_notifier(&bts_hotcpu_notifier);
307 308 309
	return register_tracer(&bts_tracer);
}
device_initcall(init_bts_trace);