Merge branch 'tracing/hw-branch-tracing' into tracing/core
[kernel.git] / kernel / trace / trace_hw_branches.c
1 /*
2  * h/w branch tracer for x86 based on BTS
3  *
4  * Copyright (C) 2008-2009 Intel Corporation.
5  * Markus Metzger <markus.t.metzger@gmail.com>, 2008-2009
6  */
7 #include <linux/kallsyms.h>
8 #include <linux/debugfs.h>
9 #include <linux/ftrace.h>
10 #include <linux/module.h>
11 #include <linux/cpu.h>
12 #include <linux/smp.h>
13 #include <linux/fs.h>
14
15 #include <asm/ds.h>
16
17 #include "trace_output.h"
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 #define this_tracer per_cpu(tracer, smp_processor_id())
27
28 static int trace_hw_branches_enabled __read_mostly;
29 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         per_cpu(tracer, cpu) =
36                 ds_request_bts_cpu(cpu, per_cpu(buffer, cpu), BTS_BUFFER_SIZE,
37                                    NULL, (size_t)-1, BTS_KERNEL);
38
39         if (IS_ERR(per_cpu(tracer, cpu)))
40                 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         get_online_cpus();
51         for_each_online_cpu(cpu) {
52                 bts_trace_init_cpu(cpu);
53
54                 if (likely(per_cpu(tracer, cpu)))
55                         trace_hw_branches_enabled = 1;
56         }
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         get_online_cpus();
69         for_each_online_cpu(cpu) {
70                 if (likely(per_cpu(tracer, cpu))) {
71                         ds_release_bts(per_cpu(tracer, cpu));
72                         per_cpu(tracer, cpu) = NULL;
73                 }
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         get_online_cpus();
85         for_each_online_cpu(cpu)
86                 if (likely(per_cpu(tracer, cpu)))
87                         ds_resume_bts(per_cpu(tracer, cpu));
88         trace_hw_branches_suspended = 0;
89         put_online_cpus();
90 }
91
92 static void bts_trace_stop(struct trace_array *tr)
93 {
94         int cpu;
95
96         get_online_cpus();
97         for_each_online_cpu(cpu)
98                 if (likely(per_cpu(tracer, cpu)))
99                         ds_suspend_bts(per_cpu(tracer, cpu));
100         trace_hw_branches_suspended = 1;
101         put_online_cpus();
102 }
103
104 static int __cpuinit bts_hotcpu_handler(struct notifier_block *nfb,
105                                      unsigned long action, void *hcpu)
106 {
107         int cpu = (long)hcpu;
108
109         switch (action) {
110         case CPU_ONLINE:
111         case CPU_DOWN_FAILED:
112                 /* The notification is sent with interrupts enabled. */
113                 if (trace_hw_branches_enabled) {
114                         bts_trace_init_cpu(cpu);
115
116                         if (trace_hw_branches_suspended &&
117                             likely(per_cpu(tracer, cpu)))
118                                 ds_suspend_bts(per_cpu(tracer, cpu));
119                 }
120                 break;
121
122         case CPU_DOWN_PREPARE:
123                 /* The notification is sent with interrupts enabled. */
124                 if (likely(per_cpu(tracer, cpu))) {
125                         ds_release_bts(per_cpu(tracer, cpu));
126                         per_cpu(tracer, cpu) = NULL;
127                 }
128         }
129
130         return NOTIFY_DONE;
131 }
132
133 static struct notifier_block bts_hotcpu_notifier __cpuinitdata = {
134         .notifier_call = bts_hotcpu_handler
135 };
136
137 static void bts_trace_print_header(struct seq_file *m)
138 {
139         seq_puts(m, "# CPU#        TO  <-  FROM\n");
140 }
141
142 static enum print_line_t bts_trace_print_line(struct trace_iterator *iter)
143 {
144         unsigned long symflags = TRACE_ITER_SYM_OFFSET;
145         struct trace_entry *entry = iter->ent;
146         struct trace_seq *seq = &iter->seq;
147         struct hw_branch_entry *it;
148
149         trace_assign_type(it, entry);
150
151         if (entry->type == TRACE_HW_BRANCHES) {
152                 if (trace_seq_printf(seq, "%4d  ", iter->cpu) &&
153                     seq_print_ip_sym(seq, it->to, symflags) &&
154                     trace_seq_printf(seq, "\t  <-  ") &&
155                     seq_print_ip_sym(seq, it->from, symflags) &&
156                     trace_seq_printf(seq, "\n"))
157                         return TRACE_TYPE_HANDLED;
158                 return TRACE_TYPE_PARTIAL_LINE;;
159         }
160         return TRACE_TYPE_UNHANDLED;
161 }
162
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         if (unlikely(!tr))
173                 return;
174
175         if (unlikely(!trace_hw_branches_enabled))
176                 return;
177
178         local_irq_save(irq1);
179         cpu = raw_smp_processor_id();
180         if (atomic_inc_return(&tr->data[cpu]->disabled) != 1)
181                 goto out;
182
183         event = trace_buffer_lock_reserve(tr, TRACE_HW_BRANCHES,
184                                           sizeof(*entry), 0, 0);
185         if (!event)
186                 goto out;
187         entry   = ring_buffer_event_data(event);
188         tracing_generic_entry_update(&entry->ent, 0, from);
189         entry->ent.type = TRACE_HW_BRANCHES;
190         entry->from = from;
191         entry->to   = to;
192         if (!filter_check_discard(call, entry, tr->buffer, event))
193                 trace_buffer_unlock_commit(tr, event, 0, 0);
194
195  out:
196         atomic_dec(&tr->data[cpu]->disabled);
197         local_irq_restore(irq1);
198 }
199
200 static void trace_bts_at(const struct bts_trace *trace, void *at)
201 {
202         struct bts_struct bts;
203         int err = 0;
204
205         WARN_ON_ONCE(!trace->read);
206         if (!trace->read)
207                 return;
208
209         err = trace->read(this_tracer, at, &bts);
210         if (err < 0)
211                 return;
212
213         switch (bts.qualifier) {
214         case BTS_BRANCH:
215                 trace_hw_branch(bts.variant.lbr.from, bts.variant.lbr.to);
216                 break;
217         }
218 }
219
220 /*
221  * Collect the trace on the current cpu and write it into the ftrace buffer.
222  *
223  * pre: tracing must be suspended on the current cpu
224  */
225 static void trace_bts_cpu(void *arg)
226 {
227         struct trace_array *tr = (struct trace_array *)arg;
228         const struct bts_trace *trace;
229         unsigned char *at;
230
231         if (unlikely(!tr))
232                 return;
233
234         if (unlikely(atomic_read(&tr->data[raw_smp_processor_id()]->disabled)))
235                 return;
236
237         if (unlikely(!this_tracer))
238                 return;
239
240         trace = ds_read_bts(this_tracer);
241         if (!trace)
242                 return;
243
244         for (at = trace->ds.top; (void *)at < trace->ds.end;
245              at += trace->ds.size)
246                 trace_bts_at(trace, at);
247
248         for (at = trace->ds.begin; (void *)at < trace->ds.top;
249              at += trace->ds.size)
250                 trace_bts_at(trace, at);
251 }
252
253 static void trace_bts_prepare(struct trace_iterator *iter)
254 {
255         int cpu;
256
257         get_online_cpus();
258         for_each_online_cpu(cpu)
259                 if (likely(per_cpu(tracer, cpu)))
260                         ds_suspend_bts(per_cpu(tracer, cpu));
261         /*
262          * We need to collect the trace on the respective cpu since ftrace
263          * implicitly adds the record for the current cpu.
264          * Once that is more flexible, we could collect the data from any cpu.
265          */
266         on_each_cpu(trace_bts_cpu, iter->tr, 1);
267
268         for_each_online_cpu(cpu)
269                 if (likely(per_cpu(tracer, cpu)))
270                         ds_resume_bts(per_cpu(tracer, cpu));
271         put_online_cpus();
272 }
273
274 static void trace_bts_close(struct trace_iterator *iter)
275 {
276         tracing_reset_online_cpus(iter->tr);
277 }
278
279 void trace_hw_branch_oops(void)
280 {
281         if (this_tracer) {
282                 ds_suspend_bts_noirq(this_tracer);
283                 trace_bts_cpu(hw_branch_trace);
284                 ds_resume_bts_noirq(this_tracer);
285         }
286 }
287
288 struct tracer bts_tracer __read_mostly =
289 {
290         .name           = "hw-branch-tracer",
291         .init           = bts_trace_init,
292         .reset          = bts_trace_reset,
293         .print_header   = bts_trace_print_header,
294         .print_line     = bts_trace_print_line,
295         .start          = bts_trace_start,
296         .stop           = bts_trace_stop,
297         .open           = trace_bts_prepare,
298         .close          = trace_bts_close,
299 #ifdef CONFIG_FTRACE_SELFTEST
300         .selftest       = trace_selftest_startup_hw_branches,
301 #endif /* CONFIG_FTRACE_SELFTEST */
302 };
303
304 __init static int init_bts_trace(void)
305 {
306         register_hotcpu_notifier(&bts_hotcpu_notifier);
307         return register_tracer(&bts_tracer);
308 }
309 device_initcall(init_bts_trace);