blob: 3a2a73716a5bd9ef56bec370f1eef4df3a7e0a0d [file] [log] [blame]
Steven Rostedt1f0d69a2008-11-12 00:14:39 -05001/*
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>
Frederic Weisbecker65c6dc62008-11-29 04:12:46 +01009#include <linux/irqflags.h>
Steven Rostedt1f0d69a2008-11-12 00:14:39 -050010#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>
Steven Rostedtf633cef2008-12-23 23:24:13 -050016
Steven Rostedt1f0d69a2008-11-12 00:14:39 -050017#include "trace.h"
Frederic Weisbecker002bb862009-01-10 11:34:13 -080018#include "trace_stat.h"
Steven Rostedtf633cef2008-12-23 23:24:13 -050019#include "trace_output.h"
Steven Rostedt1f0d69a2008-11-12 00:14:39 -050020
Steven Rostedt2ed84ee2008-11-12 15:24:24 -050021#ifdef CONFIG_BRANCH_TRACER
Steven Rostedt52f232c2008-11-12 00:14:40 -050022
Frederic Weisbecker002bb862009-01-10 11:34:13 -080023static struct tracer branch_trace;
Steven Rostedt9f029e82008-11-12 15:24:24 -050024static int branch_tracing_enabled __read_mostly;
25static DEFINE_MUTEX(branch_tracing_mutex);
Frederic Weisbeckere302cf32008-12-27 23:25:38 +010026
Steven Rostedt9f029e82008-11-12 15:24:24 -050027static struct trace_array *branch_tracer;
Steven Rostedt52f232c2008-11-12 00:14:40 -050028
29static void
Steven Rostedt9f029e82008-11-12 15:24:24 -050030probe_likely_condition(struct ftrace_branch_data *f, int val, int expect)
Steven Rostedt52f232c2008-11-12 00:14:40 -050031{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -040032 struct trace_event_call *call = &event_branch;
Steven Rostedt9f029e82008-11-12 15:24:24 -050033 struct trace_array *tr = branch_tracer;
Steven Rostedta7603ff2012-08-06 16:24:11 -040034 struct trace_array_cpu *data;
Steven Rostedt52f232c2008-11-12 00:14:40 -050035 struct ring_buffer_event *event;
Steven Rostedt9f029e82008-11-12 15:24:24 -050036 struct trace_branch *entry;
Steven Rostedt8f6e8a32009-10-07 21:53:41 -040037 struct ring_buffer *buffer;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -020038 unsigned long flags;
Steven Rostedt (Red Hat)6224beb2015-07-07 15:05:03 -040039 int pc;
Steven Rostedt52f232c2008-11-12 00:14:40 -050040 const char *p;
41
Steven Rostedt (Red Hat)6224beb2015-07-07 15:05:03 -040042 if (current->trace_recursion & TRACE_BRANCH_BIT)
43 return;
44
Steven Rostedt52f232c2008-11-12 00:14:40 -050045 /*
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
Steven Rostedt (Red Hat)6224beb2015-07-07 15:05:03 -040055 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))
Steven Rostedt52f232c2008-11-12 00:14:40 -050059 goto out;
60
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -020061 pc = preempt_count();
Steven Rostedt (Red Hat)153e8ed2013-03-08 10:40:07 -050062 buffer = tr->trace_buffer.buffer;
Steven Rostedt8f6e8a32009-10-07 21:53:41 -040063 event = trace_buffer_lock_reserve(buffer, TRACE_BRANCH,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -020064 sizeof(*entry), flags, pc);
Steven Rostedt52f232c2008-11-12 00:14:40 -050065 if (!event)
66 goto out;
67
Steven Rostedt52f232c2008-11-12 00:14:40 -050068 entry = ring_buffer_event_data(event);
Steven Rostedt52f232c2008-11-12 00:14:40 -050069
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
Tom Zanussif306cc82013-10-24 08:34:17 -050083 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -040084 __buffer_unlock_commit(buffer, event);
Steven Rostedt52f232c2008-11-12 00:14:40 -050085
86 out:
Steven Rostedt (Red Hat)6224beb2015-07-07 15:05:03 -040087 current->trace_recursion &= ~TRACE_BRANCH_BIT;
88 raw_local_irq_restore(flags);
Steven Rostedt52f232c2008-11-12 00:14:40 -050089}
90
91static inline
Steven Rostedt9f029e82008-11-12 15:24:24 -050092void trace_likely_condition(struct ftrace_branch_data *f, int val, int expect)
Steven Rostedt52f232c2008-11-12 00:14:40 -050093{
Steven Rostedt9f029e82008-11-12 15:24:24 -050094 if (!branch_tracing_enabled)
Steven Rostedt52f232c2008-11-12 00:14:40 -050095 return;
96
97 probe_likely_condition(f, val, expect);
98}
99
Steven Rostedt9f029e82008-11-12 15:24:24 -0500100int enable_branch_tracing(struct trace_array *tr)
Steven Rostedt52f232c2008-11-12 00:14:40 -0500101{
Steven Rostedt9f029e82008-11-12 15:24:24 -0500102 mutex_lock(&branch_tracing_mutex);
103 branch_tracer = tr;
Steven Rostedt52f232c2008-11-12 00:14:40 -0500104 /*
105 * Must be seen before enabling. The reader is a condition
106 * where we do not need a matching rmb()
107 */
108 smp_wmb();
Steven Rostedt9f029e82008-11-12 15:24:24 -0500109 branch_tracing_enabled++;
110 mutex_unlock(&branch_tracing_mutex);
Steven Rostedt52f232c2008-11-12 00:14:40 -0500111
Wenji Huangf54fc982009-02-10 01:02:46 -0500112 return 0;
Steven Rostedt52f232c2008-11-12 00:14:40 -0500113}
114
Steven Rostedt9f029e82008-11-12 15:24:24 -0500115void disable_branch_tracing(void)
Steven Rostedt52f232c2008-11-12 00:14:40 -0500116{
Steven Rostedt9f029e82008-11-12 15:24:24 -0500117 mutex_lock(&branch_tracing_mutex);
Steven Rostedt52f232c2008-11-12 00:14:40 -0500118
Steven Rostedt9f029e82008-11-12 15:24:24 -0500119 if (!branch_tracing_enabled)
Steven Rostedt52f232c2008-11-12 00:14:40 -0500120 goto out_unlock;
121
Steven Rostedt9f029e82008-11-12 15:24:24 -0500122 branch_tracing_enabled--;
Steven Rostedt52f232c2008-11-12 00:14:40 -0500123
124 out_unlock:
Steven Rostedt9f029e82008-11-12 15:24:24 -0500125 mutex_unlock(&branch_tracing_mutex);
Steven Rostedt52f232c2008-11-12 00:14:40 -0500126}
Steven Rostedt80e5ea42008-11-12 15:24:24 -0500127
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100128static int branch_trace_init(struct trace_array *tr)
Steven Rostedt80e5ea42008-11-12 15:24:24 -0500129{
Dmitry Safonov30616922015-10-16 16:04:49 +0300130 return enable_branch_tracing(tr);
Steven Rostedt80e5ea42008-11-12 15:24:24 -0500131}
132
133static void branch_trace_reset(struct trace_array *tr)
134{
Dmitry Safonov30616922015-10-16 16:04:49 +0300135 disable_branch_tracing();
Steven Rostedt80e5ea42008-11-12 15:24:24 -0500136}
137
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -0200138static enum print_line_t trace_branch_print(struct trace_iterator *iter,
Steven Rostedta9a57762010-04-22 18:46:14 -0400139 int flags, struct trace_event *event)
Steven Rostedtf633cef2008-12-23 23:24:13 -0500140{
141 struct trace_branch *field;
142
Arnaldo Carvalho de Melo2c9b2382009-02-02 20:30:12 -0200143 trace_assign_type(field, iter->ent);
Steven Rostedtf633cef2008-12-23 23:24:13 -0500144
Steven Rostedt (Red Hat)7d40f672014-11-12 13:19:06 -0500145 trace_seq_printf(&iter->seq, "[%s] %s:%s:%d\n",
146 field->correct ? " ok " : " MISS ",
147 field->func,
148 field->file,
149 field->line);
Steven Rostedtf633cef2008-12-23 23:24:13 -0500150
Steven Rostedt (Red Hat)7d40f672014-11-12 13:19:06 -0500151 return trace_handle_return(&iter->seq);
Steven Rostedtf633cef2008-12-23 23:24:13 -0500152}
153
Zhaolei557055b2009-04-13 16:02:34 +0800154static void branch_print_header(struct seq_file *s)
155{
156 seq_puts(s, "# TASK-PID CPU# TIMESTAMP CORRECT"
Rasmus Villemoesd79ac282014-11-08 21:42:11 +0100157 " FUNC:FILE:LINE\n"
158 "# | | | | | "
159 " |\n");
Zhaolei557055b2009-04-13 16:02:34 +0800160}
Frederic Weisbeckere302cf32008-12-27 23:25:38 +0100161
Steven Rostedta9a57762010-04-22 18:46:14 -0400162static struct trace_event_functions trace_branch_funcs = {
163 .trace = trace_branch_print,
164};
165
Steven Rostedtf633cef2008-12-23 23:24:13 -0500166static struct trace_event trace_branch_event = {
Steven Rostedtef180122009-03-10 14:10:56 -0400167 .type = TRACE_BRANCH,
Steven Rostedta9a57762010-04-22 18:46:14 -0400168 .funcs = &trace_branch_funcs,
Steven Rostedtf633cef2008-12-23 23:24:13 -0500169};
170
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800171static struct tracer branch_trace __read_mostly =
172{
173 .name = "branch",
174 .init = branch_trace_init,
175 .reset = branch_trace_reset,
176#ifdef CONFIG_FTRACE_SELFTEST
177 .selftest = trace_selftest_startup_branch,
178#endif /* CONFIG_FTRACE_SELFTEST */
Zhaolei557055b2009-04-13 16:02:34 +0800179 .print_header = branch_print_header,
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800180};
181
182__init static int init_branch_tracer(void)
183{
184 int ret;
185
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -0400186 ret = register_trace_event(&trace_branch_event);
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800187 if (!ret) {
188 printk(KERN_WARNING "Warning: could not register "
189 "branch events\n");
190 return 1;
191 }
192 return register_tracer(&branch_trace);
193}
Steven Rostedt6f415672012-10-05 12:13:07 -0400194core_initcall(init_branch_tracer);
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800195
Steven Rostedt52f232c2008-11-12 00:14:40 -0500196#else
197static inline
Steven Rostedt9f029e82008-11-12 15:24:24 -0500198void trace_likely_condition(struct ftrace_branch_data *f, int val, int expect)
Steven Rostedt52f232c2008-11-12 00:14:40 -0500199{
200}
Steven Rostedt2ed84ee2008-11-12 15:24:24 -0500201#endif /* CONFIG_BRANCH_TRACER */
Steven Rostedt52f232c2008-11-12 00:14:40 -0500202
Steven Rostedt9f029e82008-11-12 15:24:24 -0500203void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect)
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500204{
Steven Rostedt52f232c2008-11-12 00:14:40 -0500205 /*
206 * I would love to have a trace point here instead, but the
207 * trace point code is so inundated with unlikely and likely
208 * conditions that the recursive nightmare that exists is too
209 * much to try to get working. At least for now.
210 */
211 trace_likely_condition(f, val, expect);
212
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500213 /* FIXME: Make this atomic! */
214 if (val == expect)
215 f->correct++;
216 else
217 f->incorrect++;
218}
219EXPORT_SYMBOL(ftrace_likely_update);
220
Frederic Weisbeckere302cf32008-12-27 23:25:38 +0100221extern unsigned long __start_annotated_branch_profile[];
222extern unsigned long __stop_annotated_branch_profile[];
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500223
Frederic Weisbeckere302cf32008-12-27 23:25:38 +0100224static int annotated_branch_stat_headers(struct seq_file *m)
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500225{
Rasmus Villemoesd79ac282014-11-08 21:42:11 +0100226 seq_puts(m, " correct incorrect % "
227 " Function "
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100228 " File Line\n"
229 " ------- --------- - "
230 " -------- "
231 " ---- ----\n");
Frederic Weisbeckere302cf32008-12-27 23:25:38 +0100232 return 0;
233}
234
235static inline long get_incorrect_percent(struct ftrace_branch_data *p)
236{
237 long percent;
238
239 if (p->correct) {
240 percent = p->incorrect * 100;
241 percent /= p->correct + p->incorrect;
242 } else
243 percent = p->incorrect ? 100 : -1;
244
245 return percent;
246}
247
248static int branch_stat_show(struct seq_file *m, void *v)
249{
250 struct ftrace_branch_data *p = v;
251 const char *f;
252 long percent;
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500253
254 /* Only print the file, not the path */
255 f = p->file + strlen(p->file);
256 while (f >= p->file && *f != '/')
257 f--;
258 f++;
259
Steven Rostedt2bcd5212008-11-21 01:30:54 -0500260 /*
261 * The miss is overlayed on correct, and hit on incorrect.
262 */
Frederic Weisbeckere302cf32008-12-27 23:25:38 +0100263 percent = get_incorrect_percent(p);
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500264
Steven Rostedtbac28bf2008-11-21 01:51:53 -0500265 seq_printf(m, "%8lu %8lu ", p->correct, p->incorrect);
266 if (percent < 0)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100267 seq_puts(m, " X ");
Steven Rostedtbac28bf2008-11-21 01:51:53 -0500268 else
269 seq_printf(m, "%3ld ", percent);
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500270 seq_printf(m, "%-30.30s %-20.20s %d\n", p->func, f, p->line);
271 return 0;
272}
273
Steven Rostedt42548002009-03-24 13:38:36 -0400274static void *annotated_branch_stat_start(struct tracer_stat *trace)
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500275{
Frederic Weisbeckere302cf32008-12-27 23:25:38 +0100276 return __start_annotated_branch_profile;
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500277}
278
Frederic Weisbeckere302cf32008-12-27 23:25:38 +0100279static void *
280annotated_branch_stat_next(void *v, int idx)
281{
282 struct ftrace_branch_data *p = v;
283
284 ++p;
285
286 if ((void *)p >= (void *)__stop_annotated_branch_profile)
287 return NULL;
288
289 return p;
290}
291
292static int annotated_branch_stat_cmp(void *p1, void *p2)
293{
294 struct ftrace_branch_data *a = p1;
295 struct ftrace_branch_data *b = p2;
296
297 long percent_a, percent_b;
298
299 percent_a = get_incorrect_percent(a);
300 percent_b = get_incorrect_percent(b);
301
302 if (percent_a < percent_b)
303 return -1;
304 if (percent_a > percent_b)
305 return 1;
Steven Rostedtede55c92010-01-27 11:25:54 -0500306
307 if (a->incorrect < b->incorrect)
308 return -1;
309 if (a->incorrect > b->incorrect)
310 return 1;
311
312 /*
313 * Since the above shows worse (incorrect) cases
314 * first, we continue that by showing best (correct)
315 * cases last.
316 */
317 if (a->correct > b->correct)
318 return -1;
319 if (a->correct < b->correct)
320 return 1;
321
322 return 0;
Frederic Weisbeckere302cf32008-12-27 23:25:38 +0100323}
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500324
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800325static struct tracer_stat annotated_branch_stats = {
326 .name = "branch_annotated",
327 .stat_start = annotated_branch_stat_start,
328 .stat_next = annotated_branch_stat_next,
329 .stat_cmp = annotated_branch_stat_cmp,
330 .stat_headers = annotated_branch_stat_headers,
331 .stat_show = branch_stat_show
332};
333
334__init static int init_annotated_branch_stats(void)
335{
336 int ret;
337
338 ret = register_stat_tracer(&annotated_branch_stats);
339 if (!ret) {
340 printk(KERN_WARNING "Warning: could not register "
341 "annotated branches stats\n");
342 return 1;
343 }
344 return 0;
345}
346fs_initcall(init_annotated_branch_stats);
347
Steven Rostedt2bcd5212008-11-21 01:30:54 -0500348#ifdef CONFIG_PROFILE_ALL_BRANCHES
Frederic Weisbeckere302cf32008-12-27 23:25:38 +0100349
Steven Rostedt2bcd5212008-11-21 01:30:54 -0500350extern unsigned long __start_branch_profile[];
351extern unsigned long __stop_branch_profile[];
352
Frederic Weisbeckere302cf32008-12-27 23:25:38 +0100353static int all_branch_stat_headers(struct seq_file *m)
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500354{
Rasmus Villemoesd79ac282014-11-08 21:42:11 +0100355 seq_puts(m, " miss hit % "
356 " Function "
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100357 " File Line\n"
358 " ------- --------- - "
359 " -------- "
360 " ---- ----\n");
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500361 return 0;
362}
363
Steven Rostedt42548002009-03-24 13:38:36 -0400364static void *all_branch_stat_start(struct tracer_stat *trace)
Frederic Weisbeckere302cf32008-12-27 23:25:38 +0100365{
366 return __start_branch_profile;
367}
368
369static void *
370all_branch_stat_next(void *v, int idx)
371{
372 struct ftrace_branch_data *p = v;
373
374 ++p;
375
376 if ((void *)p >= (void *)__stop_branch_profile)
377 return NULL;
378
379 return p;
380}
381
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800382static struct tracer_stat all_branch_stats = {
383 .name = "branch_all",
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800384 .stat_start = all_branch_stat_start,
385 .stat_next = all_branch_stat_next,
386 .stat_headers = all_branch_stat_headers,
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800387 .stat_show = branch_stat_show
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800388};
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800389
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800390__init static int all_annotated_branch_stats(void)
Frederic Weisbeckere302cf32008-12-27 23:25:38 +0100391{
Frederic Weisbeckere302cf32008-12-27 23:25:38 +0100392 int ret;
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800393
394 ret = register_stat_tracer(&all_branch_stats);
Frederic Weisbeckere302cf32008-12-27 23:25:38 +0100395 if (!ret) {
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800396 printk(KERN_WARNING "Warning: could not register "
397 "all branches stats\n");
Frederic Weisbeckere302cf32008-12-27 23:25:38 +0100398 return 1;
399 }
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800400 return 0;
Frederic Weisbeckere302cf32008-12-27 23:25:38 +0100401}
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800402fs_initcall(all_annotated_branch_stats);
403#endif /* CONFIG_PROFILE_ALL_BRANCHES */