blob: c15222a010731498e6639625cba4915e0f55b954 [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/debugfs.h>
11#include <linux/uaccess.h>
12#include <linux/module.h>
13#include <linux/ftrace.h>
14#include <linux/hash.h>
15#include <linux/fs.h>
16#include <asm/local.h>
Steven Rostedtf633cef2008-12-23 23:24:13 -050017
Steven Rostedt1f0d69a2008-11-12 00:14:39 -050018#include "trace.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
Steven Rostedt9f029e82008-11-12 15:24:24 -050023static int branch_tracing_enabled __read_mostly;
24static DEFINE_MUTEX(branch_tracing_mutex);
25static struct trace_array *branch_tracer;
Steven Rostedt52f232c2008-11-12 00:14:40 -050026
27static void
Steven Rostedt9f029e82008-11-12 15:24:24 -050028probe_likely_condition(struct ftrace_branch_data *f, int val, int expect)
Steven Rostedt52f232c2008-11-12 00:14:40 -050029{
Steven Rostedt9f029e82008-11-12 15:24:24 -050030 struct trace_array *tr = branch_tracer;
Steven Rostedt52f232c2008-11-12 00:14:40 -050031 struct ring_buffer_event *event;
Steven Rostedt9f029e82008-11-12 15:24:24 -050032 struct trace_branch *entry;
Steven Rostedt52f232c2008-11-12 00:14:40 -050033 unsigned long flags, irq_flags;
34 int cpu, pc;
35 const char *p;
36
37 /*
38 * I would love to save just the ftrace_likely_data pointer, but
39 * this code can also be used by modules. Ugly things can happen
40 * if the module is unloaded, and then we go and read the
41 * pointer. This is slower, but much safer.
42 */
43
44 if (unlikely(!tr))
45 return;
46
Steven Rostedta5e25882008-12-02 15:34:05 -050047 local_irq_save(flags);
Steven Rostedt52f232c2008-11-12 00:14:40 -050048 cpu = raw_smp_processor_id();
49 if (atomic_inc_return(&tr->data[cpu]->disabled) != 1)
50 goto out;
51
52 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
53 &irq_flags);
54 if (!event)
55 goto out;
56
57 pc = preempt_count();
58 entry = ring_buffer_event_data(event);
59 tracing_generic_entry_update(&entry->ent, flags, pc);
Steven Rostedt9f029e82008-11-12 15:24:24 -050060 entry->ent.type = TRACE_BRANCH;
Steven Rostedt52f232c2008-11-12 00:14:40 -050061
62 /* Strip off the path, only save the file */
63 p = f->file + strlen(f->file);
64 while (p >= f->file && *p != '/')
65 p--;
66 p++;
67
68 strncpy(entry->func, f->func, TRACE_FUNC_SIZE);
69 strncpy(entry->file, p, TRACE_FILE_SIZE);
70 entry->func[TRACE_FUNC_SIZE] = 0;
71 entry->file[TRACE_FILE_SIZE] = 0;
72 entry->line = f->line;
73 entry->correct = val == expect;
74
75 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
76
77 out:
78 atomic_dec(&tr->data[cpu]->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -050079 local_irq_restore(flags);
Steven Rostedt52f232c2008-11-12 00:14:40 -050080}
81
82static inline
Steven Rostedt9f029e82008-11-12 15:24:24 -050083void trace_likely_condition(struct ftrace_branch_data *f, int val, int expect)
Steven Rostedt52f232c2008-11-12 00:14:40 -050084{
Steven Rostedt9f029e82008-11-12 15:24:24 -050085 if (!branch_tracing_enabled)
Steven Rostedt52f232c2008-11-12 00:14:40 -050086 return;
87
88 probe_likely_condition(f, val, expect);
89}
90
Steven Rostedt9f029e82008-11-12 15:24:24 -050091int enable_branch_tracing(struct trace_array *tr)
Steven Rostedt52f232c2008-11-12 00:14:40 -050092{
93 int ret = 0;
94
Steven Rostedt9f029e82008-11-12 15:24:24 -050095 mutex_lock(&branch_tracing_mutex);
96 branch_tracer = tr;
Steven Rostedt52f232c2008-11-12 00:14:40 -050097 /*
98 * Must be seen before enabling. The reader is a condition
99 * where we do not need a matching rmb()
100 */
101 smp_wmb();
Steven Rostedt9f029e82008-11-12 15:24:24 -0500102 branch_tracing_enabled++;
103 mutex_unlock(&branch_tracing_mutex);
Steven Rostedt52f232c2008-11-12 00:14:40 -0500104
105 return ret;
106}
107
Steven Rostedt9f029e82008-11-12 15:24:24 -0500108void disable_branch_tracing(void)
Steven Rostedt52f232c2008-11-12 00:14:40 -0500109{
Steven Rostedt9f029e82008-11-12 15:24:24 -0500110 mutex_lock(&branch_tracing_mutex);
Steven Rostedt52f232c2008-11-12 00:14:40 -0500111
Steven Rostedt9f029e82008-11-12 15:24:24 -0500112 if (!branch_tracing_enabled)
Steven Rostedt52f232c2008-11-12 00:14:40 -0500113 goto out_unlock;
114
Steven Rostedt9f029e82008-11-12 15:24:24 -0500115 branch_tracing_enabled--;
Steven Rostedt52f232c2008-11-12 00:14:40 -0500116
117 out_unlock:
Steven Rostedt9f029e82008-11-12 15:24:24 -0500118 mutex_unlock(&branch_tracing_mutex);
Steven Rostedt52f232c2008-11-12 00:14:40 -0500119}
Steven Rostedt80e5ea42008-11-12 15:24:24 -0500120
121static void start_branch_trace(struct trace_array *tr)
122{
123 enable_branch_tracing(tr);
124}
125
126static void stop_branch_trace(struct trace_array *tr)
127{
128 disable_branch_tracing();
129}
130
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100131static int branch_trace_init(struct trace_array *tr)
Steven Rostedt80e5ea42008-11-12 15:24:24 -0500132{
133 int cpu;
134
135 for_each_online_cpu(cpu)
136 tracing_reset(tr, cpu);
137
138 start_branch_trace(tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100139 return 0;
Steven Rostedt80e5ea42008-11-12 15:24:24 -0500140}
141
142static void branch_trace_reset(struct trace_array *tr)
143{
144 stop_branch_trace(tr);
145}
146
Steven Rostedtf633cef2008-12-23 23:24:13 -0500147static int
148trace_print_print(struct trace_seq *s, struct trace_entry *entry, int flags)
149{
150 struct print_entry *field;
151
152 trace_assign_type(field, entry);
153
154 if (seq_print_ip_sym(s, field->ip, flags))
155 goto partial;
156
157 if (trace_seq_printf(s, ": %s", field->buf))
158 goto partial;
159
160 partial:
161 return TRACE_TYPE_PARTIAL_LINE;
162}
163
164static int
165trace_branch_print(struct trace_seq *s, struct trace_entry *entry, int flags)
166{
167 struct trace_branch *field;
168
169 trace_assign_type(field, entry);
170
171 if (trace_seq_printf(s, "[%s] %s:%s:%d\n",
172 field->correct ? " ok " : " MISS ",
173 field->func,
174 field->file,
175 field->line))
176 return TRACE_TYPE_PARTIAL_LINE;
177
178 return 0;
179}
180
181static struct trace_event trace_branch_event = {
182 .type = TRACE_BRANCH,
183 .trace = trace_branch_print,
184 .latency_trace = trace_branch_print,
185 .raw = trace_nop_print,
186 .hex = trace_nop_print,
187 .binary = trace_nop_print,
188};
189
Steven Rostedt80e5ea42008-11-12 15:24:24 -0500190struct tracer branch_trace __read_mostly =
191{
192 .name = "branch",
193 .init = branch_trace_init,
194 .reset = branch_trace_reset,
195#ifdef CONFIG_FTRACE_SELFTEST
196 .selftest = trace_selftest_startup_branch,
197#endif
198};
199
200__init static int init_branch_trace(void)
201{
Steven Rostedtf633cef2008-12-23 23:24:13 -0500202 int ret;
203
204 ret = register_ftrace_event(&trace_branch_event);
205 if (!ret) {
206 printk(KERN_WARNING "Warning: could not register branch events\n");
207 return 1;
208 }
209
Steven Rostedt80e5ea42008-11-12 15:24:24 -0500210 return register_tracer(&branch_trace);
211}
212
213device_initcall(init_branch_trace);
Steven Rostedt52f232c2008-11-12 00:14:40 -0500214#else
215static inline
Steven Rostedt9f029e82008-11-12 15:24:24 -0500216void trace_likely_condition(struct ftrace_branch_data *f, int val, int expect)
Steven Rostedt52f232c2008-11-12 00:14:40 -0500217{
218}
Steven Rostedt2ed84ee2008-11-12 15:24:24 -0500219#endif /* CONFIG_BRANCH_TRACER */
Steven Rostedt52f232c2008-11-12 00:14:40 -0500220
Steven Rostedt9f029e82008-11-12 15:24:24 -0500221void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect)
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500222{
Steven Rostedt52f232c2008-11-12 00:14:40 -0500223 /*
224 * I would love to have a trace point here instead, but the
225 * trace point code is so inundated with unlikely and likely
226 * conditions that the recursive nightmare that exists is too
227 * much to try to get working. At least for now.
228 */
229 trace_likely_condition(f, val, expect);
230
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500231 /* FIXME: Make this atomic! */
232 if (val == expect)
233 f->correct++;
234 else
235 f->incorrect++;
236}
237EXPORT_SYMBOL(ftrace_likely_update);
238
239struct ftrace_pointer {
240 void *start;
241 void *stop;
Steven Rostedt2bcd5212008-11-21 01:30:54 -0500242 int hit;
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500243};
244
245static void *
246t_next(struct seq_file *m, void *v, loff_t *pos)
247{
Steven Rostedt04291492008-11-21 14:44:57 -0500248 const struct ftrace_pointer *f = m->private;
Steven Rostedt9f029e82008-11-12 15:24:24 -0500249 struct ftrace_branch_data *p = v;
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500250
251 (*pos)++;
252
253 if (v == (void *)1)
254 return f->start;
255
256 ++p;
257
258 if ((void *)p >= (void *)f->stop)
259 return NULL;
260
261 return p;
262}
263
264static void *t_start(struct seq_file *m, loff_t *pos)
265{
266 void *t = (void *)1;
267 loff_t l = 0;
268
269 for (; t && l < *pos; t = t_next(m, t, &l))
270 ;
271
272 return t;
273}
274
275static void t_stop(struct seq_file *m, void *p)
276{
277}
278
279static int t_show(struct seq_file *m, void *v)
280{
Steven Rostedt04291492008-11-21 14:44:57 -0500281 const struct ftrace_pointer *fp = m->private;
Steven Rostedt9f029e82008-11-12 15:24:24 -0500282 struct ftrace_branch_data *p = v;
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500283 const char *f;
Steven Rostedtbac28bf2008-11-21 01:51:53 -0500284 long percent;
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500285
286 if (v == (void *)1) {
Steven Rostedt2bcd5212008-11-21 01:30:54 -0500287 if (fp->hit)
288 seq_printf(m, " miss hit %% ");
289 else
290 seq_printf(m, " correct incorrect %% ");
291 seq_printf(m, " Function "
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500292 " File Line\n"
293 " ------- --------- - "
294 " -------- "
295 " ---- ----\n");
296 return 0;
297 }
298
299 /* Only print the file, not the path */
300 f = p->file + strlen(p->file);
301 while (f >= p->file && *f != '/')
302 f--;
303 f++;
304
Steven Rostedt2bcd5212008-11-21 01:30:54 -0500305 /*
306 * The miss is overlayed on correct, and hit on incorrect.
307 */
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500308 if (p->correct) {
309 percent = p->incorrect * 100;
310 percent /= p->correct + p->incorrect;
311 } else
Steven Rostedtbac28bf2008-11-21 01:51:53 -0500312 percent = p->incorrect ? 100 : -1;
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500313
Steven Rostedtbac28bf2008-11-21 01:51:53 -0500314 seq_printf(m, "%8lu %8lu ", p->correct, p->incorrect);
315 if (percent < 0)
316 seq_printf(m, " X ");
317 else
318 seq_printf(m, "%3ld ", percent);
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500319 seq_printf(m, "%-30.30s %-20.20s %d\n", p->func, f, p->line);
320 return 0;
321}
322
323static struct seq_operations tracing_likely_seq_ops = {
324 .start = t_start,
325 .next = t_next,
326 .stop = t_stop,
327 .show = t_show,
328};
329
Steven Rostedt45b79742008-11-21 00:40:40 -0500330static int tracing_branch_open(struct inode *inode, struct file *file)
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500331{
332 int ret;
333
334 ret = seq_open(file, &tracing_likely_seq_ops);
335 if (!ret) {
336 struct seq_file *m = file->private_data;
337 m->private = (void *)inode->i_private;
338 }
339
340 return ret;
341}
342
Steven Rostedt45b79742008-11-21 00:40:40 -0500343static const struct file_operations tracing_branch_fops = {
344 .open = tracing_branch_open,
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500345 .read = seq_read,
346 .llseek = seq_lseek,
347};
348
Steven Rostedt2bcd5212008-11-21 01:30:54 -0500349#ifdef CONFIG_PROFILE_ALL_BRANCHES
350extern unsigned long __start_branch_profile[];
351extern unsigned long __stop_branch_profile[];
352
Steven Rostedt04291492008-11-21 14:44:57 -0500353static const struct ftrace_pointer ftrace_branch_pos = {
Steven Rostedt2bcd5212008-11-21 01:30:54 -0500354 .start = __start_branch_profile,
355 .stop = __stop_branch_profile,
356 .hit = 1,
357};
358
359#endif /* CONFIG_PROFILE_ALL_BRANCHES */
360
Steven Rostedt45b79742008-11-21 00:40:40 -0500361extern unsigned long __start_annotated_branch_profile[];
362extern unsigned long __stop_annotated_branch_profile[];
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500363
Steven Rostedt45b79742008-11-21 00:40:40 -0500364static const struct ftrace_pointer ftrace_annotated_branch_pos = {
365 .start = __start_annotated_branch_profile,
366 .stop = __stop_annotated_branch_profile,
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500367};
368
Steven Rostedt9f029e82008-11-12 15:24:24 -0500369static __init int ftrace_branch_init(void)
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500370{
371 struct dentry *d_tracer;
372 struct dentry *entry;
373
374 d_tracer = tracing_init_dentry();
375
Steven Rostedt45b79742008-11-21 00:40:40 -0500376 entry = debugfs_create_file("profile_annotated_branch", 0444, d_tracer,
Steven Rostedt04291492008-11-21 14:44:57 -0500377 (void *)&ftrace_annotated_branch_pos,
Steven Rostedt45b79742008-11-21 00:40:40 -0500378 &tracing_branch_fops);
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500379 if (!entry)
Steven Rostedt45b79742008-11-21 00:40:40 -0500380 pr_warning("Could not create debugfs "
381 "'profile_annotatet_branch' entry\n");
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500382
Steven Rostedt2bcd5212008-11-21 01:30:54 -0500383#ifdef CONFIG_PROFILE_ALL_BRANCHES
384 entry = debugfs_create_file("profile_branch", 0444, d_tracer,
Steven Rostedt04291492008-11-21 14:44:57 -0500385 (void *)&ftrace_branch_pos,
Steven Rostedt2bcd5212008-11-21 01:30:54 -0500386 &tracing_branch_fops);
387 if (!entry)
388 pr_warning("Could not create debugfs"
389 " 'profile_branch' entry\n");
390#endif
391
Steven Rostedt1f0d69a2008-11-12 00:14:39 -0500392 return 0;
393}
394
Steven Rostedt9f029e82008-11-12 15:24:24 -0500395device_initcall(ftrace_branch_init);