tracing/filters: use ring_buffer_discard_commit() in filter_check_discard()
[linux-2.6.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/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>
14 #include <linux/fs.h>
15
16 #include <asm/ds.h>
17
18 #include "trace.h"
19 #include "trace_output.h"
20
21
22 #define SIZEOF_BTS (1 << 13)
23
24 /*
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
33  */
34 static DEFINE_SPINLOCK(bts_tracer_lock);
35 static DEFINE_PER_CPU(struct bts_tracer *, tracer);
36 static DEFINE_PER_CPU(unsigned char[SIZEOF_BTS], buffer);
37
38 #define this_tracer per_cpu(tracer, smp_processor_id())
39 #define this_buffer per_cpu(buffer, smp_processor_id())
40
41 static int __read_mostly trace_hw_branches_enabled;
42 static struct trace_array *hw_branch_trace __read_mostly;
43
44
45 /*
46  * Start tracing on the current cpu.
47  * The argument is ignored.
48  *
49  * pre: bts_tracer_lock must be locked.
50  */
51 static void bts_trace_start_cpu(void *arg)
52 {
53         if (this_tracer)
54                 ds_release_bts(this_tracer);
55
56         this_tracer =
57                 ds_request_bts(/* task = */ NULL, this_buffer, SIZEOF_BTS,
58                                /* ovfl = */ NULL, /* th = */ (size_t)-1,
59                                BTS_KERNEL);
60         if (IS_ERR(this_tracer)) {
61                 this_tracer = NULL;
62                 return;
63         }
64 }
65
66 static void bts_trace_start(struct trace_array *tr)
67 {
68         spin_lock(&bts_tracer_lock);
69
70         on_each_cpu(bts_trace_start_cpu, NULL, 1);
71         trace_hw_branches_enabled = 1;
72
73         spin_unlock(&bts_tracer_lock);
74 }
75
76 /*
77  * Stop tracing on the current cpu.
78  * The argument is ignored.
79  *
80  * pre: bts_tracer_lock must be locked.
81  */
82 static void bts_trace_stop_cpu(void *arg)
83 {
84         if (this_tracer) {
85                 ds_release_bts(this_tracer);
86                 this_tracer = NULL;
87         }
88 }
89
90 static void bts_trace_stop(struct trace_array *tr)
91 {
92         spin_lock(&bts_tracer_lock);
93
94         trace_hw_branches_enabled = 0;
95         on_each_cpu(bts_trace_stop_cpu, NULL, 1);
96
97         spin_unlock(&bts_tracer_lock);
98 }
99
100 static int __cpuinit bts_hotcpu_handler(struct notifier_block *nfb,
101                                      unsigned long action, void *hcpu)
102 {
103         unsigned int cpu = (unsigned long)hcpu;
104
105         spin_lock(&bts_tracer_lock);
106
107         if (!trace_hw_branches_enabled)
108                 goto out;
109
110         switch (action) {
111         case CPU_ONLINE:
112         case CPU_DOWN_FAILED:
113                 smp_call_function_single(cpu, bts_trace_start_cpu, NULL, 1);
114                 break;
115         case CPU_DOWN_PREPARE:
116                 smp_call_function_single(cpu, bts_trace_stop_cpu, NULL, 1);
117                 break;
118         }
119
120  out:
121         spin_unlock(&bts_tracer_lock);
122         return NOTIFY_DONE;
123 }
124
125 static struct notifier_block bts_hotcpu_notifier __cpuinitdata = {
126         .notifier_call = bts_hotcpu_handler
127 };
128
129 static int bts_trace_init(struct trace_array *tr)
130 {
131         hw_branch_trace = tr;
132
133         bts_trace_start(tr);
134
135         return 0;
136 }
137
138 static void bts_trace_reset(struct trace_array *tr)
139 {
140         bts_trace_stop(tr);
141 }
142
143 static void bts_trace_print_header(struct seq_file *m)
144 {
145         seq_puts(m, "# CPU#        TO  <-  FROM\n");
146 }
147
148 static enum print_line_t bts_trace_print_line(struct trace_iterator *iter)
149 {
150         struct trace_entry *entry = iter->ent;
151         struct trace_seq *seq = &iter->seq;
152         struct hw_branch_entry *it;
153         unsigned long symflags = TRACE_ITER_SYM_OFFSET;
154
155         trace_assign_type(it, entry);
156
157         if (entry->type == TRACE_HW_BRANCHES) {
158                 if (trace_seq_printf(seq, "%4d  ", iter->cpu) &&
159                     seq_print_ip_sym(seq, it->to, symflags) &&
160                     trace_seq_printf(seq, "\t  <-  ") &&
161                     seq_print_ip_sym(seq, it->from, symflags) &&
162                     trace_seq_printf(seq, "\n"))
163                         return TRACE_TYPE_HANDLED;
164                 return TRACE_TYPE_PARTIAL_LINE;;
165         }
166         return TRACE_TYPE_UNHANDLED;
167 }
168
169 void trace_hw_branch(u64 from, u64 to)
170 {
171         struct ftrace_event_call *call = &event_hw_branch;
172         struct trace_array *tr = hw_branch_trace;
173         struct ring_buffer_event *event;
174         struct hw_branch_entry *entry;
175         unsigned long irq1;
176         int cpu;
177
178         if (unlikely(!tr))
179                 return;
180
181         if (unlikely(!trace_hw_branches_enabled))
182                 return;
183
184         local_irq_save(irq1);
185         cpu = raw_smp_processor_id();
186         if (atomic_inc_return(&tr->data[cpu]->disabled) != 1)
187                 goto out;
188
189         event = trace_buffer_lock_reserve(tr, TRACE_HW_BRANCHES,
190                                           sizeof(*entry), 0, 0);
191         if (!event)
192                 goto out;
193         entry   = ring_buffer_event_data(event);
194         tracing_generic_entry_update(&entry->ent, 0, from);
195         entry->ent.type = TRACE_HW_BRANCHES;
196         entry->from = from;
197         entry->to   = to;
198         if (!filter_check_discard(call, entry, tr->buffer, event))
199                 trace_buffer_unlock_commit(tr, event, 0, 0);
200
201  out:
202         atomic_dec(&tr->data[cpu]->disabled);
203         local_irq_restore(irq1);
204 }
205
206 static void trace_bts_at(const struct bts_trace *trace, void *at)
207 {
208         struct bts_struct bts;
209         int err = 0;
210
211         WARN_ON_ONCE(!trace->read);
212         if (!trace->read)
213                 return;
214
215         err = trace->read(this_tracer, at, &bts);
216         if (err < 0)
217                 return;
218
219         switch (bts.qualifier) {
220         case BTS_BRANCH:
221                 trace_hw_branch(bts.variant.lbr.from, bts.variant.lbr.to);
222                 break;
223         }
224 }
225
226 /*
227  * Collect the trace on the current cpu and write it into the ftrace buffer.
228  *
229  * pre: bts_tracer_lock must be locked
230  */
231 static void trace_bts_cpu(void *arg)
232 {
233         struct trace_array *tr = (struct trace_array *) arg;
234         const struct bts_trace *trace;
235         unsigned char *at;
236
237         if (unlikely(!tr))
238                 return;
239
240         if (unlikely(atomic_read(&tr->data[raw_smp_processor_id()]->disabled)))
241                 return;
242
243         if (unlikely(!this_tracer))
244                 return;
245
246         ds_suspend_bts(this_tracer);
247         trace = ds_read_bts(this_tracer);
248         if (!trace)
249                 goto out;
250
251         for (at = trace->ds.top; (void *)at < trace->ds.end;
252              at += trace->ds.size)
253                 trace_bts_at(trace, at);
254
255         for (at = trace->ds.begin; (void *)at < trace->ds.top;
256              at += trace->ds.size)
257                 trace_bts_at(trace, at);
258
259 out:
260         ds_resume_bts(this_tracer);
261 }
262
263 static void trace_bts_prepare(struct trace_iterator *iter)
264 {
265         spin_lock(&bts_tracer_lock);
266
267         on_each_cpu(trace_bts_cpu, iter->tr, 1);
268
269         spin_unlock(&bts_tracer_lock);
270 }
271
272 static void trace_bts_close(struct trace_iterator *iter)
273 {
274         tracing_reset_online_cpus(iter->tr);
275 }
276
277 void trace_hw_branch_oops(void)
278 {
279         spin_lock(&bts_tracer_lock);
280
281         trace_bts_cpu(hw_branch_trace);
282
283         spin_unlock(&bts_tracer_lock);
284 }
285
286 struct tracer bts_tracer __read_mostly =
287 {
288         .name           = "hw-branch-tracer",
289         .init           = bts_trace_init,
290         .reset          = bts_trace_reset,
291         .print_header   = bts_trace_print_header,
292         .print_line     = bts_trace_print_line,
293         .start          = bts_trace_start,
294         .stop           = bts_trace_stop,
295         .open           = trace_bts_prepare,
296         .close          = trace_bts_close
297 };
298
299 __init static int init_bts_trace(void)
300 {
301         register_hotcpu_notifier(&bts_hotcpu_notifier);
302         return register_tracer(&bts_tracer);
303 }
304 device_initcall(init_bts_trace);