trace_hw_branches.c 6.4 KB
Newer Older
1
/*
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 8
#include <linux/spinlock.h>
#include <linux/kallsyms.h>
9 10
#include <linux/debugfs.h>
#include <linux/ftrace.h>
11
#include <linux/module.h>
12 13
#include <linux/cpu.h>
#include <linux/smp.h>
14
#include <linux/fs.h>
15 16 17 18

#include <asm/ds.h>

#include "trace.h"
19
#include "trace_output.h"
20 21 22 23


#define SIZEOF_BTS (1 << 13)

24 25 26 27 28 29 30 31 32 33 34
/*
 * The tracer lock protects the below per-cpu tracer array.
 * It needs to be held to:
 * - start tracing on all cpus
 * - stop tracing on all cpus
 * - start tracing on a single hotplug cpu
 * - stop tracing on a single hotplug cpu
 * - read the trace from all cpus
 * - read the trace from a single cpu
 */
static DEFINE_SPINLOCK(bts_tracer_lock);
35 36 37 38 39 40
static DEFINE_PER_CPU(struct bts_tracer *, tracer);
static DEFINE_PER_CPU(unsigned char[SIZEOF_BTS], buffer);

#define this_tracer per_cpu(tracer, smp_processor_id())
#define this_buffer per_cpu(buffer, smp_processor_id())

41
static int __read_mostly trace_hw_branches_enabled;
42
static struct trace_array *hw_branch_trace __read_mostly;
43

44 45 46 47 48

/*
 * Start tracing on the current cpu.
 * The argument is ignored.
 *
49
 * pre: bts_tracer_lock must be locked.
50
 */
51 52
static void bts_trace_start_cpu(void *arg)
{
53 54 55
	if (this_tracer)
		ds_release_bts(this_tracer);

56 57
	this_tracer =
		ds_request_bts(/* task = */ NULL, this_buffer, SIZEOF_BTS,
58 59
			       /* ovfl = */ NULL, /* th = */ (size_t)-1,
			       BTS_KERNEL);
60 61 62 63 64 65 66 67
	if (IS_ERR(this_tracer)) {
		this_tracer = NULL;
		return;
	}
}

static void bts_trace_start(struct trace_array *tr)
{
68
	spin_lock(&bts_tracer_lock);
69

70 71
	on_each_cpu(bts_trace_start_cpu, NULL, 1);
	trace_hw_branches_enabled = 1;
72

73
	spin_unlock(&bts_tracer_lock);
74 75
}

76
/*
W
Wenji Huang 已提交
77
 * Stop tracing on the current cpu.
78 79
 * The argument is ignored.
 *
80
 * pre: bts_tracer_lock must be locked.
81
 */
82 83 84 85 86 87 88 89 90 91
static void bts_trace_stop_cpu(void *arg)
{
	if (this_tracer) {
		ds_release_bts(this_tracer);
		this_tracer = NULL;
	}
}

static void bts_trace_stop(struct trace_array *tr)
{
92
	spin_lock(&bts_tracer_lock);
93 94 95

	trace_hw_branches_enabled = 0;
	on_each_cpu(bts_trace_stop_cpu, NULL, 1);
96

97
	spin_unlock(&bts_tracer_lock);
98 99 100 101 102 103 104
}

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

105
	spin_lock(&bts_tracer_lock);
106 107 108 109 110 111 112 113 114 115

	if (!trace_hw_branches_enabled)
		goto out;

	switch (action) {
	case CPU_ONLINE:
	case CPU_DOWN_FAILED:
		smp_call_function_single(cpu, bts_trace_start_cpu, NULL, 1);
		break;
	case CPU_DOWN_PREPARE:
116
		smp_call_function_single(cpu, bts_trace_stop_cpu, NULL, 1);
117 118 119 120
		break;
	}

 out:
121
	spin_unlock(&bts_tracer_lock);
122
	return NOTIFY_DONE;
123 124
}

125 126 127 128
static struct notifier_block bts_hotcpu_notifier __cpuinitdata = {
	.notifier_call = bts_hotcpu_handler
};

129
static int bts_trace_init(struct trace_array *tr)
130
{
131 132
	hw_branch_trace = tr;

133 134 135 136 137
	bts_trace_start(tr);

	return 0;
}

138
static void bts_trace_reset(struct trace_array *tr)
139 140 141 142
{
	bts_trace_stop(tr);
}

143 144
static void bts_trace_print_header(struct seq_file *m)
{
145
	seq_puts(m, "# CPU#        TO  <-  FROM\n");
146 147 148 149 150 151
}

static enum print_line_t bts_trace_print_line(struct trace_iterator *iter)
{
	struct trace_entry *entry = iter->ent;
	struct trace_seq *seq = &iter->seq;
152
	struct hw_branch_entry *it;
153
	unsigned long symflags = TRACE_ITER_SYM_OFFSET;
154 155 156

	trace_assign_type(it, entry);

157
	if (entry->type == TRACE_HW_BRANCHES) {
158
		if (trace_seq_printf(seq, "%4d  ", iter->cpu) &&
159 160 161
		    seq_print_ip_sym(seq, it->to, symflags) &&
		    trace_seq_printf(seq, "\t  <-  ") &&
		    seq_print_ip_sym(seq, it->from, symflags) &&
162 163 164
		    trace_seq_printf(seq, "\n"))
			return TRACE_TYPE_HANDLED;
		return TRACE_TYPE_PARTIAL_LINE;;
165 166 167 168
	}
	return TRACE_TYPE_UNHANDLED;
}

169
void trace_hw_branch(u64 from, u64 to)
170
{
171
	struct trace_array *tr = hw_branch_trace;
172
	struct ring_buffer_event *event;
173
	struct hw_branch_entry *entry;
174
	unsigned long irq1;
175
	int cpu;
176

177 178 179 180
	if (unlikely(!tr))
		return;

	if (unlikely(!trace_hw_branches_enabled))
181
		return;
182 183 184 185 186 187

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

188 189
	event = trace_buffer_lock_reserve(tr, TRACE_HW_BRANCHES,
					  sizeof(*entry), 0, 0);
190 191
	if (!event)
		goto out;
192 193
	entry	= ring_buffer_event_data(event);
	tracing_generic_entry_update(&entry->ent, 0, from);
194
	entry->ent.type = TRACE_HW_BRANCHES;
195 196
	entry->from = from;
	entry->to   = to;
197
	trace_buffer_unlock_commit(tr, event, 0, 0);
198 199 200 201

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

204
static void trace_bts_at(const struct bts_trace *trace, void *at)
205
{
206 207
	struct bts_struct bts;
	int err = 0;
208

209 210
	WARN_ON_ONCE(!trace->read);
	if (!trace->read)
211 212
		return;

213 214 215
	err = trace->read(this_tracer, at, &bts);
	if (err < 0)
		return;
216

217 218
	switch (bts.qualifier) {
	case BTS_BRANCH:
219
		trace_hw_branch(bts.variant.lbr.from, bts.variant.lbr.to);
220 221
		break;
	}
222 223
}

224 225 226
/*
 * Collect the trace on the current cpu and write it into the ftrace buffer.
 *
227
 * pre: bts_tracer_lock must be locked
228
 */
229 230 231
static void trace_bts_cpu(void *arg)
{
	struct trace_array *tr = (struct trace_array *) arg;
232 233
	const struct bts_trace *trace;
	unsigned char *at;
234

235
	if (unlikely(!tr))
236 237
		return;

238 239 240
	if (unlikely(atomic_read(&tr->data[raw_smp_processor_id()]->disabled)))
		return;

241 242 243
	if (unlikely(!this_tracer))
		return;

244 245 246
	ds_suspend_bts(this_tracer);
	trace = ds_read_bts(this_tracer);
	if (!trace)
247 248
		goto out;

249 250
	for (at = trace->ds.top; (void *)at < trace->ds.end;
	     at += trace->ds.size)
251
		trace_bts_at(trace, at);
252

253 254
	for (at = trace->ds.begin; (void *)at < trace->ds.top;
	     at += trace->ds.size)
255
		trace_bts_at(trace, at);
256 257

out:
258
	ds_resume_bts(this_tracer);
259 260 261 262
}

static void trace_bts_prepare(struct trace_iterator *iter)
{
263
	spin_lock(&bts_tracer_lock);
264 265

	on_each_cpu(trace_bts_cpu, iter->tr, 1);
266

267
	spin_unlock(&bts_tracer_lock);
268 269
}

270 271 272 273 274
static void trace_bts_close(struct trace_iterator *iter)
{
	tracing_reset_online_cpus(iter->tr);
}

275 276
void trace_hw_branch_oops(void)
{
277
	spin_lock(&bts_tracer_lock);
278 279 280

	trace_bts_cpu(hw_branch_trace);

281
	spin_unlock(&bts_tracer_lock);
282 283
}

284 285
struct tracer bts_tracer __read_mostly =
{
286
	.name		= "hw-branch-tracer",
287
	.init		= bts_trace_init,
288
	.reset		= bts_trace_reset,
289 290 291 292
	.print_header	= bts_trace_print_header,
	.print_line	= bts_trace_print_line,
	.start		= bts_trace_start,
	.stop		= bts_trace_stop,
293 294
	.open		= trace_bts_prepare,
	.close		= trace_bts_close
295 296 297 298
};

__init static int init_bts_trace(void)
{
299
	register_hotcpu_notifier(&bts_hotcpu_notifier);
300 301 302
	return register_tracer(&bts_tracer);
}
device_initcall(init_bts_trace);