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