]>
Commit | Line | Data |
---|---|---|
1f0d69a9 SR |
1 | /* |
2 | * unlikely profiler | |
3 | * | |
4 | * Copyright (C) 2008 Steven Rostedt <[email protected]> | |
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/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> | |
f633cef0 | 17 | |
1f0d69a9 | 18 | #include "trace.h" |
f633cef0 | 19 | #include "trace_output.h" |
1f0d69a9 | 20 | |
2ed84eeb | 21 | #ifdef CONFIG_BRANCH_TRACER |
52f232cb | 22 | |
9f029e83 SR |
23 | static int branch_tracing_enabled __read_mostly; |
24 | static DEFINE_MUTEX(branch_tracing_mutex); | |
25 | static struct trace_array *branch_tracer; | |
52f232cb SR |
26 | |
27 | static void | |
9f029e83 | 28 | probe_likely_condition(struct ftrace_branch_data *f, int val, int expect) |
52f232cb | 29 | { |
9f029e83 | 30 | struct trace_array *tr = branch_tracer; |
52f232cb | 31 | struct ring_buffer_event *event; |
9f029e83 | 32 | struct trace_branch *entry; |
52f232cb SR |
33 | 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 | ||
a5e25883 | 47 | local_irq_save(flags); |
52f232cb SR |
48 | 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); | |
9f029e83 | 60 | entry->ent.type = TRACE_BRANCH; |
52f232cb SR |
61 | |
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); | |
a5e25883 | 79 | local_irq_restore(flags); |
52f232cb SR |
80 | } |
81 | ||
82 | static inline | |
9f029e83 | 83 | void trace_likely_condition(struct ftrace_branch_data *f, int val, int expect) |
52f232cb | 84 | { |
9f029e83 | 85 | if (!branch_tracing_enabled) |
52f232cb SR |
86 | return; |
87 | ||
88 | probe_likely_condition(f, val, expect); | |
89 | } | |
90 | ||
9f029e83 | 91 | int enable_branch_tracing(struct trace_array *tr) |
52f232cb SR |
92 | { |
93 | int ret = 0; | |
94 | ||
9f029e83 SR |
95 | mutex_lock(&branch_tracing_mutex); |
96 | branch_tracer = tr; | |
52f232cb SR |
97 | /* |
98 | * Must be seen before enabling. The reader is a condition | |
99 | * where we do not need a matching rmb() | |
100 | */ | |
101 | smp_wmb(); | |
9f029e83 SR |
102 | branch_tracing_enabled++; |
103 | mutex_unlock(&branch_tracing_mutex); | |
52f232cb SR |
104 | |
105 | return ret; | |
106 | } | |
107 | ||
9f029e83 | 108 | void disable_branch_tracing(void) |
52f232cb | 109 | { |
9f029e83 | 110 | mutex_lock(&branch_tracing_mutex); |
52f232cb | 111 | |
9f029e83 | 112 | if (!branch_tracing_enabled) |
52f232cb SR |
113 | goto out_unlock; |
114 | ||
9f029e83 | 115 | branch_tracing_enabled--; |
52f232cb SR |
116 | |
117 | out_unlock: | |
9f029e83 | 118 | mutex_unlock(&branch_tracing_mutex); |
52f232cb | 119 | } |
80e5ea45 SR |
120 | |
121 | static void start_branch_trace(struct trace_array *tr) | |
122 | { | |
123 | enable_branch_tracing(tr); | |
124 | } | |
125 | ||
126 | static void stop_branch_trace(struct trace_array *tr) | |
127 | { | |
128 | disable_branch_tracing(); | |
129 | } | |
130 | ||
1c80025a | 131 | static int branch_trace_init(struct trace_array *tr) |
80e5ea45 SR |
132 | { |
133 | int cpu; | |
134 | ||
135 | for_each_online_cpu(cpu) | |
136 | tracing_reset(tr, cpu); | |
137 | ||
138 | start_branch_trace(tr); | |
1c80025a | 139 | return 0; |
80e5ea45 SR |
140 | } |
141 | ||
142 | static void branch_trace_reset(struct trace_array *tr) | |
143 | { | |
144 | stop_branch_trace(tr); | |
145 | } | |
146 | ||
f633cef0 SR |
147 | static int |
148 | trace_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 | ||
164 | static int | |
165 | trace_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 | ||
181 | static 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 | ||
80e5ea45 SR |
190 | struct 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 | { | |
f633cef0 SR |
202 | 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 | ||
80e5ea45 SR |
210 | return register_tracer(&branch_trace); |
211 | } | |
212 | ||
213 | device_initcall(init_branch_trace); | |
52f232cb SR |
214 | #else |
215 | static inline | |
9f029e83 | 216 | void trace_likely_condition(struct ftrace_branch_data *f, int val, int expect) |
52f232cb SR |
217 | { |
218 | } | |
2ed84eeb | 219 | #endif /* CONFIG_BRANCH_TRACER */ |
52f232cb | 220 | |
9f029e83 | 221 | void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect) |
1f0d69a9 | 222 | { |
52f232cb SR |
223 | /* |
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 | ||
1f0d69a9 SR |
231 | /* FIXME: Make this atomic! */ |
232 | if (val == expect) | |
233 | f->correct++; | |
234 | else | |
235 | f->incorrect++; | |
236 | } | |
237 | EXPORT_SYMBOL(ftrace_likely_update); | |
238 | ||
239 | struct ftrace_pointer { | |
240 | void *start; | |
241 | void *stop; | |
2bcd521a | 242 | int hit; |
1f0d69a9 SR |
243 | }; |
244 | ||
245 | static void * | |
246 | t_next(struct seq_file *m, void *v, loff_t *pos) | |
247 | { | |
0429149f | 248 | const struct ftrace_pointer *f = m->private; |
9f029e83 | 249 | struct ftrace_branch_data *p = v; |
1f0d69a9 SR |
250 | |
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 | ||
264 | static 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 | ||
275 | static void t_stop(struct seq_file *m, void *p) | |
276 | { | |
277 | } | |
278 | ||
279 | static int t_show(struct seq_file *m, void *v) | |
280 | { | |
0429149f | 281 | const struct ftrace_pointer *fp = m->private; |
9f029e83 | 282 | struct ftrace_branch_data *p = v; |
1f0d69a9 | 283 | const char *f; |
bac28bfe | 284 | long percent; |
1f0d69a9 SR |
285 | |
286 | if (v == (void *)1) { | |
2bcd521a SR |
287 | if (fp->hit) |
288 | seq_printf(m, " miss hit %% "); | |
289 | else | |
290 | seq_printf(m, " correct incorrect %% "); | |
291 | seq_printf(m, " Function " | |
1f0d69a9 SR |
292 | " 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 | ||
2bcd521a SR |
305 | /* |
306 | * The miss is overlayed on correct, and hit on incorrect. | |
307 | */ | |
1f0d69a9 SR |
308 | if (p->correct) { |
309 | percent = p->incorrect * 100; | |
310 | percent /= p->correct + p->incorrect; | |
311 | } else | |
bac28bfe | 312 | percent = p->incorrect ? 100 : -1; |
1f0d69a9 | 313 | |
bac28bfe SR |
314 | 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); | |
1f0d69a9 SR |
319 | seq_printf(m, "%-30.30s %-20.20s %d\n", p->func, f, p->line); |
320 | return 0; | |
321 | } | |
322 | ||
323 | static 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 | ||
45b79749 | 330 | static int tracing_branch_open(struct inode *inode, struct file *file) |
1f0d69a9 SR |
331 | { |
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 | ||
45b79749 SR |
343 | static const struct file_operations tracing_branch_fops = { |
344 | .open = tracing_branch_open, | |
1f0d69a9 SR |
345 | .read = seq_read, |
346 | .llseek = seq_lseek, | |
347 | }; | |
348 | ||
2bcd521a SR |
349 | #ifdef CONFIG_PROFILE_ALL_BRANCHES |
350 | extern unsigned long __start_branch_profile[]; | |
351 | extern unsigned long __stop_branch_profile[]; | |
352 | ||
0429149f | 353 | static const struct ftrace_pointer ftrace_branch_pos = { |
2bcd521a SR |
354 | .start = __start_branch_profile, |
355 | .stop = __stop_branch_profile, | |
356 | .hit = 1, | |
357 | }; | |
358 | ||
359 | #endif /* CONFIG_PROFILE_ALL_BRANCHES */ | |
360 | ||
45b79749 SR |
361 | extern unsigned long __start_annotated_branch_profile[]; |
362 | extern unsigned long __stop_annotated_branch_profile[]; | |
1f0d69a9 | 363 | |
45b79749 SR |
364 | static const struct ftrace_pointer ftrace_annotated_branch_pos = { |
365 | .start = __start_annotated_branch_profile, | |
366 | .stop = __stop_annotated_branch_profile, | |
1f0d69a9 SR |
367 | }; |
368 | ||
9f029e83 | 369 | static __init int ftrace_branch_init(void) |
1f0d69a9 SR |
370 | { |
371 | struct dentry *d_tracer; | |
372 | struct dentry *entry; | |
373 | ||
374 | d_tracer = tracing_init_dentry(); | |
375 | ||
45b79749 | 376 | entry = debugfs_create_file("profile_annotated_branch", 0444, d_tracer, |
0429149f | 377 | (void *)&ftrace_annotated_branch_pos, |
45b79749 | 378 | &tracing_branch_fops); |
1f0d69a9 | 379 | if (!entry) |
45b79749 SR |
380 | pr_warning("Could not create debugfs " |
381 | "'profile_annotatet_branch' entry\n"); | |
1f0d69a9 | 382 | |
2bcd521a SR |
383 | #ifdef CONFIG_PROFILE_ALL_BRANCHES |
384 | entry = debugfs_create_file("profile_branch", 0444, d_tracer, | |
0429149f | 385 | (void *)&ftrace_branch_pos, |
2bcd521a SR |
386 | &tracing_branch_fops); |
387 | if (!entry) | |
388 | pr_warning("Could not create debugfs" | |
389 | " 'profile_branch' entry\n"); | |
390 | #endif | |
391 | ||
1f0d69a9 SR |
392 | return 0; |
393 | } | |
394 | ||
9f029e83 | 395 | device_initcall(ftrace_branch_init); |