Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dledford/rdma
[linux-2.6-block.git] / kernel / trace / trace_branch.c
CommitLineData
1f0d69a9
SR
1/*
2 * unlikely profiler
3 *
4 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
5 */
6#include <linux/kallsyms.h>
7#include <linux/seq_file.h>
8#include <linux/spinlock.h>
65c6dc6a 9#include <linux/irqflags.h>
1f0d69a9
SR
10#include <linux/uaccess.h>
11#include <linux/module.h>
12#include <linux/ftrace.h>
13#include <linux/hash.h>
14#include <linux/fs.h>
15#include <asm/local.h>
f633cef0 16
1f0d69a9 17#include "trace.h"
002bb86d 18#include "trace_stat.h"
f633cef0 19#include "trace_output.h"
1f0d69a9 20
2ed84eeb 21#ifdef CONFIG_BRANCH_TRACER
52f232cb 22
002bb86d 23static struct tracer branch_trace;
9f029e83
SR
24static int branch_tracing_enabled __read_mostly;
25static DEFINE_MUTEX(branch_tracing_mutex);
e302cf3f 26
9f029e83 27static struct trace_array *branch_tracer;
52f232cb
SR
28
29static void
9f029e83 30probe_likely_condition(struct ftrace_branch_data *f, int val, int expect)
52f232cb 31{
2425bcb9 32 struct trace_event_call *call = &event_branch;
9f029e83 33 struct trace_array *tr = branch_tracer;
a7603ff4 34 struct trace_array_cpu *data;
52f232cb 35 struct ring_buffer_event *event;
9f029e83 36 struct trace_branch *entry;
8f6e8a31 37 struct ring_buffer *buffer;
0a987751 38 unsigned long flags;
6224beb1 39 int pc;
52f232cb
SR
40 const char *p;
41
6224beb1
SRRH
42 if (current->trace_recursion & TRACE_BRANCH_BIT)
43 return;
44
52f232cb
SR
45 /*
46 * I would love to save just the ftrace_likely_data pointer, but
47 * this code can also be used by modules. Ugly things can happen
48 * if the module is unloaded, and then we go and read the
49 * pointer. This is slower, but much safer.
50 */
51
52 if (unlikely(!tr))
53 return;
54
6224beb1
SRRH
55 raw_local_irq_save(flags);
56 current->trace_recursion |= TRACE_BRANCH_BIT;
57 data = this_cpu_ptr(tr->trace_buffer.data);
58 if (atomic_read(&data->disabled))
52f232cb
SR
59 goto out;
60
51a763dd 61 pc = preempt_count();
153e8ed9 62 buffer = tr->trace_buffer.buffer;
8f6e8a31 63 event = trace_buffer_lock_reserve(buffer, TRACE_BRANCH,
51a763dd 64 sizeof(*entry), flags, pc);
52f232cb
SR
65 if (!event)
66 goto out;
67
52f232cb 68 entry = ring_buffer_event_data(event);
52f232cb
SR
69
70 /* Strip off the path, only save the file */
71 p = f->file + strlen(f->file);
72 while (p >= f->file && *p != '/')
73 p--;
74 p++;
75
76 strncpy(entry->func, f->func, TRACE_FUNC_SIZE);
77 strncpy(entry->file, p, TRACE_FILE_SIZE);
78 entry->func[TRACE_FUNC_SIZE] = 0;
79 entry->file[TRACE_FILE_SIZE] = 0;
80 entry->line = f->line;
81 entry->correct = val == expect;
82
f306cc82 83 if (!call_filter_check_discard(call, entry, buffer, event))
7ffbd48d 84 __buffer_unlock_commit(buffer, event);
52f232cb
SR
85
86 out:
6224beb1
SRRH
87 current->trace_recursion &= ~TRACE_BRANCH_BIT;
88 raw_local_irq_restore(flags);
52f232cb
SR
89}
90
91static inline
9f029e83 92void trace_likely_condition(struct ftrace_branch_data *f, int val, int expect)
52f232cb 93{
9f029e83 94 if (!branch_tracing_enabled)
52f232cb
SR
95 return;
96
97 probe_likely_condition(f, val, expect);
98}
99
9f029e83 100int enable_branch_tracing(struct trace_array *tr)
52f232cb 101{
9f029e83
SR
102 mutex_lock(&branch_tracing_mutex);
103 branch_tracer = tr;
52f232cb
SR
104 /*
105 * Must be seen before enabling. The reader is a condition
106 * where we do not need a matching rmb()
107 */
108 smp_wmb();
9f029e83
SR
109 branch_tracing_enabled++;
110 mutex_unlock(&branch_tracing_mutex);
52f232cb 111
f54fc98a 112 return 0;
52f232cb
SR
113}
114
9f029e83 115void disable_branch_tracing(void)
52f232cb 116{
9f029e83 117 mutex_lock(&branch_tracing_mutex);
52f232cb 118
9f029e83 119 if (!branch_tracing_enabled)
52f232cb
SR
120 goto out_unlock;
121
9f029e83 122 branch_tracing_enabled--;
52f232cb
SR
123
124 out_unlock:
9f029e83 125 mutex_unlock(&branch_tracing_mutex);
52f232cb 126}
80e5ea45
SR
127
128static void start_branch_trace(struct trace_array *tr)
129{
130 enable_branch_tracing(tr);
131}
132
133static void stop_branch_trace(struct trace_array *tr)
134{
135 disable_branch_tracing();
136}
137
1c80025a 138static int branch_trace_init(struct trace_array *tr)
80e5ea45 139{
80e5ea45 140 start_branch_trace(tr);
1c80025a 141 return 0;
80e5ea45
SR
142}
143
144static void branch_trace_reset(struct trace_array *tr)
145{
146 stop_branch_trace(tr);
147}
148
ae7462b4 149static enum print_line_t trace_branch_print(struct trace_iterator *iter,
a9a57763 150 int flags, struct trace_event *event)
f633cef0
SR
151{
152 struct trace_branch *field;
153
2c9b238e 154 trace_assign_type(field, iter->ent);
f633cef0 155
7d40f671
SRRH
156 trace_seq_printf(&iter->seq, "[%s] %s:%s:%d\n",
157 field->correct ? " ok " : " MISS ",
158 field->func,
159 field->file,
160 field->line);
161
162 return trace_handle_return(&iter->seq);
f633cef0
SR
163}
164
557055be
Z
165static void branch_print_header(struct seq_file *s)
166{
167 seq_puts(s, "# TASK-PID CPU# TIMESTAMP CORRECT"
d79ac28f
RV
168 " FUNC:FILE:LINE\n"
169 "# | | | | | "
170 " |\n");
557055be 171}
e302cf3f 172
a9a57763
SR
173static struct trace_event_functions trace_branch_funcs = {
174 .trace = trace_branch_print,
175};
176
f633cef0 177static struct trace_event trace_branch_event = {
ef18012b 178 .type = TRACE_BRANCH,
a9a57763 179 .funcs = &trace_branch_funcs,
f633cef0
SR
180};
181
002bb86d
FW
182static struct tracer branch_trace __read_mostly =
183{
184 .name = "branch",
185 .init = branch_trace_init,
186 .reset = branch_trace_reset,
187#ifdef CONFIG_FTRACE_SELFTEST
188 .selftest = trace_selftest_startup_branch,
189#endif /* CONFIG_FTRACE_SELFTEST */
557055be 190 .print_header = branch_print_header,
002bb86d
FW
191};
192
193__init static int init_branch_tracer(void)
194{
195 int ret;
196
9023c930 197 ret = register_trace_event(&trace_branch_event);
002bb86d
FW
198 if (!ret) {
199 printk(KERN_WARNING "Warning: could not register "
200 "branch events\n");
201 return 1;
202 }
203 return register_tracer(&branch_trace);
204}
6f415672 205core_initcall(init_branch_tracer);
002bb86d 206
52f232cb
SR
207#else
208static inline
9f029e83 209void trace_likely_condition(struct ftrace_branch_data *f, int val, int expect)
52f232cb
SR
210{
211}
2ed84eeb 212#endif /* CONFIG_BRANCH_TRACER */
52f232cb 213
9f029e83 214void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect)
1f0d69a9 215{
52f232cb
SR
216 /*
217 * I would love to have a trace point here instead, but the
218 * trace point code is so inundated with unlikely and likely
219 * conditions that the recursive nightmare that exists is too
220 * much to try to get working. At least for now.
221 */
222 trace_likely_condition(f, val, expect);
223
1f0d69a9
SR
224 /* FIXME: Make this atomic! */
225 if (val == expect)
226 f->correct++;
227 else
228 f->incorrect++;
229}
230EXPORT_SYMBOL(ftrace_likely_update);
231
e302cf3f
FW
232extern unsigned long __start_annotated_branch_profile[];
233extern unsigned long __stop_annotated_branch_profile[];
1f0d69a9 234
e302cf3f 235static int annotated_branch_stat_headers(struct seq_file *m)
1f0d69a9 236{
d79ac28f
RV
237 seq_puts(m, " correct incorrect % "
238 " Function "
fa6f0cc7
RV
239 " File Line\n"
240 " ------- --------- - "
241 " -------- "
242 " ---- ----\n");
e302cf3f 243 return 0;
1f0d69a9
SR
244}
245
e302cf3f 246static inline long get_incorrect_percent(struct ftrace_branch_data *p)
1f0d69a9 247{
e302cf3f 248 long percent;
1f0d69a9 249
e302cf3f
FW
250 if (p->correct) {
251 percent = p->incorrect * 100;
252 percent /= p->correct + p->incorrect;
253 } else
254 percent = p->incorrect ? 100 : -1;
1f0d69a9 255
e302cf3f 256 return percent;
1f0d69a9
SR
257}
258
e302cf3f 259static int branch_stat_show(struct seq_file *m, void *v)
1f0d69a9 260{
9f029e83 261 struct ftrace_branch_data *p = v;
1f0d69a9 262 const char *f;
bac28bfe 263 long percent;
1f0d69a9 264
1f0d69a9
SR
265 /* Only print the file, not the path */
266 f = p->file + strlen(p->file);
267 while (f >= p->file && *f != '/')
268 f--;
269 f++;
270
2bcd521a
SR
271 /*
272 * The miss is overlayed on correct, and hit on incorrect.
273 */
e302cf3f 274 percent = get_incorrect_percent(p);
1f0d69a9 275
bac28bfe
SR
276 seq_printf(m, "%8lu %8lu ", p->correct, p->incorrect);
277 if (percent < 0)
fa6f0cc7 278 seq_puts(m, " X ");
bac28bfe
SR
279 else
280 seq_printf(m, "%3ld ", percent);
1f0d69a9
SR
281 seq_printf(m, "%-30.30s %-20.20s %d\n", p->func, f, p->line);
282 return 0;
283}
284
42548008 285static void *annotated_branch_stat_start(struct tracer_stat *trace)
e302cf3f
FW
286{
287 return __start_annotated_branch_profile;
288}
1f0d69a9 289
e302cf3f
FW
290static void *
291annotated_branch_stat_next(void *v, int idx)
1f0d69a9 292{
e302cf3f 293 struct ftrace_branch_data *p = v;
1f0d69a9 294
e302cf3f 295 ++p;
1f0d69a9 296
e302cf3f
FW
297 if ((void *)p >= (void *)__stop_annotated_branch_profile)
298 return NULL;
299
300 return p;
1f0d69a9
SR
301}
302
e302cf3f
FW
303static int annotated_branch_stat_cmp(void *p1, void *p2)
304{
305 struct ftrace_branch_data *a = p1;
306 struct ftrace_branch_data *b = p2;
307
308 long percent_a, percent_b;
309
310 percent_a = get_incorrect_percent(a);
311 percent_b = get_incorrect_percent(b);
312
313 if (percent_a < percent_b)
314 return -1;
315 if (percent_a > percent_b)
316 return 1;
ede55c9d
SR
317
318 if (a->incorrect < b->incorrect)
319 return -1;
320 if (a->incorrect > b->incorrect)
321 return 1;
322
323 /*
324 * Since the above shows worse (incorrect) cases
325 * first, we continue that by showing best (correct)
326 * cases last.
327 */
328 if (a->correct > b->correct)
329 return -1;
330 if (a->correct < b->correct)
331 return 1;
332
333 return 0;
e302cf3f 334}
1f0d69a9 335
002bb86d
FW
336static struct tracer_stat annotated_branch_stats = {
337 .name = "branch_annotated",
338 .stat_start = annotated_branch_stat_start,
339 .stat_next = annotated_branch_stat_next,
340 .stat_cmp = annotated_branch_stat_cmp,
341 .stat_headers = annotated_branch_stat_headers,
342 .stat_show = branch_stat_show
343};
344
345__init static int init_annotated_branch_stats(void)
346{
347 int ret;
348
349 ret = register_stat_tracer(&annotated_branch_stats);
350 if (!ret) {
351 printk(KERN_WARNING "Warning: could not register "
352 "annotated branches stats\n");
353 return 1;
354 }
355 return 0;
356}
357fs_initcall(init_annotated_branch_stats);
358
2bcd521a 359#ifdef CONFIG_PROFILE_ALL_BRANCHES
2bcd521a 360
e302cf3f
FW
361extern unsigned long __start_branch_profile[];
362extern unsigned long __stop_branch_profile[];
1f0d69a9 363
e302cf3f
FW
364static int all_branch_stat_headers(struct seq_file *m)
365{
d79ac28f
RV
366 seq_puts(m, " miss hit % "
367 " Function "
fa6f0cc7
RV
368 " File Line\n"
369 " ------- --------- - "
370 " -------- "
371 " ---- ----\n");
e302cf3f
FW
372 return 0;
373}
1f0d69a9 374
42548008 375static void *all_branch_stat_start(struct tracer_stat *trace)
1f0d69a9 376{
e302cf3f
FW
377 return __start_branch_profile;
378}
379
380static void *
381all_branch_stat_next(void *v, int idx)
382{
383 struct ftrace_branch_data *p = v;
1f0d69a9 384
e302cf3f 385 ++p;
1f0d69a9 386
e302cf3f
FW
387 if ((void *)p >= (void *)__stop_branch_profile)
388 return NULL;
1f0d69a9 389
e302cf3f
FW
390 return p;
391}
2bcd521a 392
002bb86d
FW
393static struct tracer_stat all_branch_stats = {
394 .name = "branch_all",
034939b6
FW
395 .stat_start = all_branch_stat_start,
396 .stat_next = all_branch_stat_next,
397 .stat_headers = all_branch_stat_headers,
002bb86d 398 .stat_show = branch_stat_show
034939b6 399};
e302cf3f 400
002bb86d 401__init static int all_annotated_branch_stats(void)
e302cf3f 402{
e302cf3f 403 int ret;
002bb86d
FW
404
405 ret = register_stat_tracer(&all_branch_stats);
e302cf3f 406 if (!ret) {
002bb86d
FW
407 printk(KERN_WARNING "Warning: could not register "
408 "all branches stats\n");
e302cf3f
FW
409 return 1;
410 }
002bb86d 411 return 0;
e302cf3f 412}
002bb86d
FW
413fs_initcall(all_annotated_branch_stats);
414#endif /* CONFIG_PROFILE_ALL_BRANCHES */