trace_branch.c 8.9 KB

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