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
9 #include <linux/module.h>
11 #include <linux/debugfs.h>
12 #include <linux/ftrace.h>
13 #include <linux/kallsyms.h>
14 #include <linux/mutex.h>
15 #include <linux/cpu.h>
16 #include <linux/smp.h>
21 #include "trace_output.h"
24 #define SIZEOF_BTS (1 << 13)
26 /* The tracer mutex protects the below per-cpu tracer array.
27 It needs to be held to:
28 - start tracing on all cpus
29 - stop tracing on all cpus
30 - start tracing on a single hotplug cpu
31 - stop tracing on a single hotplug cpu
32 - read the trace from all cpus
33 - read the trace from a single cpu
35 static DEFINE_MUTEX(bts_tracer_mutex);
36 static DEFINE_PER_CPU(struct bts_tracer *, tracer);
37 static DEFINE_PER_CPU(unsigned char[SIZEOF_BTS], buffer);
39 #define this_tracer per_cpu(tracer, smp_processor_id())
40 #define this_buffer per_cpu(buffer, smp_processor_id())
42 static int __read_mostly trace_hw_branches_enabled;
43 static struct trace_array *hw_branch_trace __read_mostly;
47 * Start tracing on the current cpu.
48 * The argument is ignored.
50 * pre: bts_tracer_mutex must be locked.
52 static void bts_trace_start_cpu(void *arg)
55 ds_release_bts(this_tracer);
58 ds_request_bts(/* task = */ NULL, this_buffer, SIZEOF_BTS,
59 /* ovfl = */ NULL, /* th = */ (size_t)-1,
61 if (IS_ERR(this_tracer)) {
67 static void bts_trace_start(struct trace_array *tr)
69 mutex_lock(&bts_tracer_mutex);
71 on_each_cpu(bts_trace_start_cpu, NULL, 1);
72 trace_hw_branches_enabled = 1;
74 mutex_unlock(&bts_tracer_mutex);
78 * Stop tracing on the current cpu.
79 * The argument is ignored.
81 * pre: bts_tracer_mutex must be locked.
83 static void bts_trace_stop_cpu(void *arg)
86 ds_release_bts(this_tracer);
91 static void bts_trace_stop(struct trace_array *tr)
93 mutex_lock(&bts_tracer_mutex);
95 trace_hw_branches_enabled = 0;
96 on_each_cpu(bts_trace_stop_cpu, NULL, 1);
98 mutex_unlock(&bts_tracer_mutex);
101 static int __cpuinit bts_hotcpu_handler(struct notifier_block *nfb,
102 unsigned long action, void *hcpu)
104 unsigned int cpu = (unsigned long)hcpu;
106 mutex_lock(&bts_tracer_mutex);
108 if (!trace_hw_branches_enabled)
113 case CPU_DOWN_FAILED:
114 smp_call_function_single(cpu, bts_trace_start_cpu, NULL, 1);
116 case CPU_DOWN_PREPARE:
117 smp_call_function_single(cpu, bts_trace_stop_cpu, NULL, 1);
122 mutex_unlock(&bts_tracer_mutex);
126 static struct notifier_block bts_hotcpu_notifier __cpuinitdata = {
127 .notifier_call = bts_hotcpu_handler
130 static int bts_trace_init(struct trace_array *tr)
132 hw_branch_trace = tr;
134 register_hotcpu_notifier(&bts_hotcpu_notifier);
140 static void bts_trace_reset(struct trace_array *tr)
143 unregister_hotcpu_notifier(&bts_hotcpu_notifier);
146 static void bts_trace_print_header(struct seq_file *m)
148 seq_puts(m, "# CPU# TO <- FROM\n");
151 static enum print_line_t bts_trace_print_line(struct trace_iterator *iter)
153 struct trace_entry *entry = iter->ent;
154 struct trace_seq *seq = &iter->seq;
155 struct hw_branch_entry *it;
156 unsigned long symflags = TRACE_ITER_SYM_OFFSET;
158 trace_assign_type(it, entry);
160 if (entry->type == TRACE_HW_BRANCHES) {
161 if (trace_seq_printf(seq, "%4d ", iter->cpu) &&
162 seq_print_ip_sym(seq, it->to, symflags) &&
163 trace_seq_printf(seq, "\t <- ") &&
164 seq_print_ip_sym(seq, it->from, symflags) &&
165 trace_seq_printf(seq, "\n"))
166 return TRACE_TYPE_HANDLED;
167 return TRACE_TYPE_PARTIAL_LINE;;
169 return TRACE_TYPE_UNHANDLED;
172 void trace_hw_branch(u64 from, u64 to)
174 struct trace_array *tr = hw_branch_trace;
175 struct ring_buffer_event *event;
176 struct hw_branch_entry *entry;
183 if (unlikely(!trace_hw_branches_enabled))
186 local_irq_save(irq1);
187 cpu = raw_smp_processor_id();
188 if (atomic_inc_return(&tr->data[cpu]->disabled) != 1)
191 event = trace_buffer_lock_reserve(tr, TRACE_HW_BRANCHES,
192 sizeof(*entry), 0, 0);
195 entry = ring_buffer_event_data(event);
196 tracing_generic_entry_update(&entry->ent, 0, from);
197 entry->ent.type = TRACE_HW_BRANCHES;
200 trace_buffer_unlock_commit(tr, event, 0, 0);
203 atomic_dec(&tr->data[cpu]->disabled);
204 local_irq_restore(irq1);
207 static void trace_bts_at(const struct bts_trace *trace, void *at)
209 struct bts_struct bts;
212 WARN_ON_ONCE(!trace->read);
216 err = trace->read(this_tracer, at, &bts);
220 switch (bts.qualifier) {
222 trace_hw_branch(bts.variant.lbr.from, bts.variant.lbr.to);
228 * Collect the trace on the current cpu and write it into the ftrace buffer.
230 * pre: bts_tracer_mutex must be locked
232 static void trace_bts_cpu(void *arg)
234 struct trace_array *tr = (struct trace_array *) arg;
235 const struct bts_trace *trace;
241 if (unlikely(atomic_read(&tr->data[raw_smp_processor_id()]->disabled)))
244 if (unlikely(!this_tracer))
247 ds_suspend_bts(this_tracer);
248 trace = ds_read_bts(this_tracer);
252 for (at = trace->ds.top; (void *)at < trace->ds.end;
253 at += trace->ds.size)
254 trace_bts_at(trace, at);
256 for (at = trace->ds.begin; (void *)at < trace->ds.top;
257 at += trace->ds.size)
258 trace_bts_at(trace, at);
261 ds_resume_bts(this_tracer);
264 static void trace_bts_prepare(struct trace_iterator *iter)
266 mutex_lock(&bts_tracer_mutex);
268 on_each_cpu(trace_bts_cpu, iter->tr, 1);
270 mutex_unlock(&bts_tracer_mutex);
273 static void trace_bts_close(struct trace_iterator *iter)
275 tracing_reset_online_cpus(iter->tr);
278 void trace_hw_branch_oops(void)
280 mutex_lock(&bts_tracer_mutex);
282 trace_bts_cpu(hw_branch_trace);
284 mutex_unlock(&bts_tracer_mutex);
287 struct tracer bts_tracer __read_mostly =
289 .name = "hw-branch-tracer",
290 .init = bts_trace_init,
291 .reset = bts_trace_reset,
292 .print_header = bts_trace_print_header,
293 .print_line = bts_trace_print_line,
294 .start = bts_trace_start,
295 .stop = bts_trace_stop,
296 .open = trace_bts_prepare,
297 .close = trace_bts_close
300 __init static int init_bts_trace(void)
302 return register_tracer(&bts_tracer);
304 device_initcall(init_bts_trace);