2 * h/w branch tracer for x86 based on BTS
4 * Copyright (C) 2008-2009 Intel Corporation.
5 * Markus Metzger <markus.t.metzger@gmail.com>, 2008-2009
7 #include <linux/spinlock.h>
8 #include <linux/kallsyms.h>
9 #include <linux/debugfs.h>
10 #include <linux/ftrace.h>
11 #include <linux/module.h>
12 #include <linux/cpu.h>
13 #include <linux/smp.h>
18 #include "trace_output.h"
22 #define BTS_BUFFER_SIZE (1 << 13)
25 * The tracer lock protects the below per-cpu tracer array.
26 * It needs to be held to:
27 * - start tracing on all cpus
28 * - stop tracing on all cpus
29 * - start tracing on a single hotplug cpu
30 * - stop tracing on a single hotplug cpu
31 * - read the trace from all cpus
32 * - read the trace from a single cpu
34 static DEFINE_SPINLOCK(bts_tracer_lock);
35 static DEFINE_PER_CPU(struct bts_tracer *, tracer);
36 static DEFINE_PER_CPU(unsigned char[BTS_BUFFER_SIZE], buffer);
38 #define this_tracer per_cpu(tracer, smp_processor_id())
39 #define this_buffer per_cpu(buffer, smp_processor_id())
41 static int trace_hw_branches_enabled __read_mostly;
42 static int trace_hw_branches_suspended __read_mostly;
43 static struct trace_array *hw_branch_trace __read_mostly;
47 * Initialize the tracer for the current cpu.
48 * The argument is ignored.
50 * pre: bts_tracer_lock must be locked.
52 static void bts_trace_init_cpu(void *arg)
55 ds_release_bts(this_tracer);
57 this_tracer = ds_request_bts(NULL, this_buffer, BTS_BUFFER_SIZE,
58 NULL, (size_t)-1, BTS_KERNEL);
59 if (IS_ERR(this_tracer)) {
65 static int bts_trace_init(struct trace_array *tr)
69 spin_lock(&bts_tracer_lock);
73 on_each_cpu(bts_trace_init_cpu, NULL, 1);
75 /* Check on how many cpus we could enable tracing */
77 for_each_online_cpu(cpu)
78 if (per_cpu(tracer, cpu))
81 trace_hw_branches_enabled = (avail ? 1 : 0);
82 trace_hw_branches_suspended = 0;
84 spin_unlock(&bts_tracer_lock);
87 /* If we could not enable tracing on a single cpu, we fail. */
88 return avail ? 0 : -EOPNOTSUPP;
92 * Release the tracer for the current cpu.
93 * The argument is ignored.
95 * pre: bts_tracer_lock must be locked.
97 static void bts_trace_release_cpu(void *arg)
100 ds_release_bts(this_tracer);
105 static void bts_trace_reset(struct trace_array *tr)
107 spin_lock(&bts_tracer_lock);
109 on_each_cpu(bts_trace_release_cpu, NULL, 1);
110 trace_hw_branches_enabled = 0;
111 trace_hw_branches_suspended = 0;
113 spin_unlock(&bts_tracer_lock);
117 * Resume tracing on the current cpu.
118 * The argument is ignored.
120 * pre: bts_tracer_lock must be locked.
122 static void bts_trace_resume_cpu(void *arg)
125 ds_resume_bts(this_tracer);
128 static void bts_trace_start(struct trace_array *tr)
130 spin_lock(&bts_tracer_lock);
132 on_each_cpu(bts_trace_resume_cpu, NULL, 1);
133 trace_hw_branches_suspended = 0;
135 spin_unlock(&bts_tracer_lock);
139 * Suspend tracing on the current cpu.
140 * The argument is ignored.
142 * pre: bts_tracer_lock must be locked.
144 static void bts_trace_suspend_cpu(void *arg)
147 ds_suspend_bts(this_tracer);
150 static void bts_trace_stop(struct trace_array *tr)
152 spin_lock(&bts_tracer_lock);
154 on_each_cpu(bts_trace_suspend_cpu, NULL, 1);
155 trace_hw_branches_suspended = 1;
157 spin_unlock(&bts_tracer_lock);
160 static int __cpuinit bts_hotcpu_handler(struct notifier_block *nfb,
161 unsigned long action, void *hcpu)
163 unsigned int cpu = (unsigned long)hcpu;
165 spin_lock(&bts_tracer_lock);
167 if (!trace_hw_branches_enabled)
172 case CPU_DOWN_FAILED:
173 smp_call_function_single(cpu, bts_trace_init_cpu, NULL, 1);
175 if (trace_hw_branches_suspended)
176 smp_call_function_single(cpu, bts_trace_suspend_cpu,
179 case CPU_DOWN_PREPARE:
180 smp_call_function_single(cpu, bts_trace_release_cpu, NULL, 1);
185 spin_unlock(&bts_tracer_lock);
189 static struct notifier_block bts_hotcpu_notifier __cpuinitdata = {
190 .notifier_call = bts_hotcpu_handler
193 static void bts_trace_print_header(struct seq_file *m)
195 seq_puts(m, "# CPU# TO <- FROM\n");
198 static enum print_line_t bts_trace_print_line(struct trace_iterator *iter)
200 unsigned long symflags = TRACE_ITER_SYM_OFFSET;
201 struct trace_entry *entry = iter->ent;
202 struct trace_seq *seq = &iter->seq;
203 struct hw_branch_entry *it;
205 trace_assign_type(it, entry);
207 if (entry->type == TRACE_HW_BRANCHES) {
208 if (trace_seq_printf(seq, "%4d ", iter->cpu) &&
209 seq_print_ip_sym(seq, it->to, symflags) &&
210 trace_seq_printf(seq, "\t <- ") &&
211 seq_print_ip_sym(seq, it->from, symflags) &&
212 trace_seq_printf(seq, "\n"))
213 return TRACE_TYPE_HANDLED;
214 return TRACE_TYPE_PARTIAL_LINE;;
216 return TRACE_TYPE_UNHANDLED;
219 void trace_hw_branch(u64 from, u64 to)
221 struct trace_array *tr = hw_branch_trace;
222 struct ring_buffer_event *event;
223 struct hw_branch_entry *entry;
230 if (unlikely(!trace_hw_branches_enabled))
233 local_irq_save(irq1);
234 cpu = raw_smp_processor_id();
235 if (atomic_inc_return(&tr->data[cpu]->disabled) != 1)
238 event = trace_buffer_lock_reserve(tr, TRACE_HW_BRANCHES,
239 sizeof(*entry), 0, 0);
242 entry = ring_buffer_event_data(event);
243 tracing_generic_entry_update(&entry->ent, 0, from);
244 entry->ent.type = TRACE_HW_BRANCHES;
247 trace_buffer_unlock_commit(tr, event, 0, 0);
250 atomic_dec(&tr->data[cpu]->disabled);
251 local_irq_restore(irq1);
254 static void trace_bts_at(const struct bts_trace *trace, void *at)
256 struct bts_struct bts;
259 WARN_ON_ONCE(!trace->read);
263 err = trace->read(this_tracer, at, &bts);
267 switch (bts.qualifier) {
269 trace_hw_branch(bts.variant.lbr.from, bts.variant.lbr.to);
275 * Collect the trace on the current cpu and write it into the ftrace buffer.
277 * pre: bts_tracer_lock must be locked
279 static void trace_bts_cpu(void *arg)
281 struct trace_array *tr = (struct trace_array *)arg;
282 const struct bts_trace *trace;
288 if (unlikely(atomic_read(&tr->data[raw_smp_processor_id()]->disabled)))
291 if (unlikely(!this_tracer))
294 ds_suspend_bts(this_tracer);
295 trace = ds_read_bts(this_tracer);
299 for (at = trace->ds.top; (void *)at < trace->ds.end;
300 at += trace->ds.size)
301 trace_bts_at(trace, at);
303 for (at = trace->ds.begin; (void *)at < trace->ds.top;
304 at += trace->ds.size)
305 trace_bts_at(trace, at);
308 ds_resume_bts(this_tracer);
311 static void trace_bts_prepare(struct trace_iterator *iter)
313 spin_lock(&bts_tracer_lock);
315 on_each_cpu(trace_bts_cpu, iter->tr, 1);
317 spin_unlock(&bts_tracer_lock);
320 static void trace_bts_close(struct trace_iterator *iter)
322 tracing_reset_online_cpus(iter->tr);
325 void trace_hw_branch_oops(void)
327 spin_lock(&bts_tracer_lock);
329 if (trace_hw_branches_enabled)
330 trace_bts_cpu(hw_branch_trace);
332 spin_unlock(&bts_tracer_lock);
335 struct tracer bts_tracer __read_mostly =
337 .name = "hw-branch-tracer",
338 .init = bts_trace_init,
339 .reset = bts_trace_reset,
340 .print_header = bts_trace_print_header,
341 .print_line = bts_trace_print_line,
342 .start = bts_trace_start,
343 .stop = bts_trace_stop,
344 .open = trace_bts_prepare,
345 .close = trace_bts_close,
346 #ifdef CONFIG_FTRACE_SELFTEST
347 .selftest = trace_selftest_startup_hw_branches,
348 #endif /* CONFIG_FTRACE_SELFTEST */
351 __init static int init_bts_trace(void)
353 register_hotcpu_notifier(&bts_hotcpu_notifier);
354 return register_tracer(&bts_tracer);
356 device_initcall(init_bts_trace);