trace_branch.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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_likely_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->data.file + strlen(f->data.file);
  60. while (p >= f->data.file && *p != '/')
  61. p--;
  62. p++;
  63. strncpy(entry->func, f->data.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->constant = f->constant;
  68. entry->line = f->data.line;
  69. entry->correct = val == expect;
  70. if (!call_filter_check_discard(call, entry, buffer, event))
  71. trace_buffer_unlock_commit_nostack(buffer, event);
  72. out:
  73. current->trace_recursion &= ~TRACE_BRANCH_BIT;
  74. raw_local_irq_restore(flags);
  75. }
  76. static inline
  77. void trace_likely_condition(struct ftrace_likely_data *f, int val, int expect)
  78. {
  79. if (!branch_tracing_enabled)
  80. return;
  81. probe_likely_condition(f, val, expect);
  82. }
  83. int enable_branch_tracing(struct trace_array *tr)
  84. {
  85. mutex_lock(&branch_tracing_mutex);
  86. branch_tracer = tr;
  87. /*
  88. * Must be seen before enabling. The reader is a condition
  89. * where we do not need a matching rmb()
  90. */
  91. smp_wmb();
  92. branch_tracing_enabled++;
  93. mutex_unlock(&branch_tracing_mutex);
  94. return 0;
  95. }
  96. void disable_branch_tracing(void)
  97. {
  98. mutex_lock(&branch_tracing_mutex);
  99. if (!branch_tracing_enabled)
  100. goto out_unlock;
  101. branch_tracing_enabled--;
  102. out_unlock:
  103. mutex_unlock(&branch_tracing_mutex);
  104. }
  105. static int branch_trace_init(struct trace_array *tr)
  106. {
  107. return enable_branch_tracing(tr);
  108. }
  109. static void branch_trace_reset(struct trace_array *tr)
  110. {
  111. disable_branch_tracing();
  112. }
  113. static enum print_line_t trace_branch_print(struct trace_iterator *iter,
  114. int flags, struct trace_event *event)
  115. {
  116. struct trace_branch *field;
  117. trace_assign_type(field, iter->ent);
  118. trace_seq_printf(&iter->seq, "[%s] %s:%s:%d\n",
  119. field->correct ? " ok " : " MISS ",
  120. field->func,
  121. field->file,
  122. field->line);
  123. return trace_handle_return(&iter->seq);
  124. }
  125. static void branch_print_header(struct seq_file *s)
  126. {
  127. seq_puts(s, "# TASK-PID CPU# TIMESTAMP CORRECT"
  128. " FUNC:FILE:LINE\n"
  129. "# | | | | | "
  130. " |\n");
  131. }
  132. static struct trace_event_functions trace_branch_funcs = {
  133. .trace = trace_branch_print,
  134. };
  135. static struct trace_event trace_branch_event = {
  136. .type = TRACE_BRANCH,
  137. .funcs = &trace_branch_funcs,
  138. };
  139. static struct tracer branch_trace __read_mostly =
  140. {
  141. .name = "branch",
  142. .init = branch_trace_init,
  143. .reset = branch_trace_reset,
  144. #ifdef CONFIG_FTRACE_SELFTEST
  145. .selftest = trace_selftest_startup_branch,
  146. #endif /* CONFIG_FTRACE_SELFTEST */
  147. .print_header = branch_print_header,
  148. };
  149. __init static int init_branch_tracer(void)
  150. {
  151. int ret;
  152. ret = register_trace_event(&trace_branch_event);
  153. if (!ret) {
  154. printk(KERN_WARNING "Warning: could not register "
  155. "branch events\n");
  156. return 1;
  157. }
  158. return register_tracer(&branch_trace);
  159. }
  160. core_initcall(init_branch_tracer);
  161. #else
  162. static inline
  163. void trace_likely_condition(struct ftrace_likely_data *f, int val, int expect)
  164. {
  165. }
  166. #endif /* CONFIG_BRANCH_TRACER */
  167. void ftrace_likely_update(struct ftrace_likely_data *f, int val,
  168. int expect, int is_constant)
  169. {
  170. /* A constant is always correct */
  171. if (is_constant) {
  172. f->constant++;
  173. val = expect;
  174. }
  175. /*
  176. * I would love to have a trace point here instead, but the
  177. * trace point code is so inundated with unlikely and likely
  178. * conditions that the recursive nightmare that exists is too
  179. * much to try to get working. At least for now.
  180. */
  181. trace_likely_condition(f, val, expect);
  182. /* FIXME: Make this atomic! */
  183. if (val == expect)
  184. f->data.correct++;
  185. else
  186. f->data.incorrect++;
  187. }
  188. EXPORT_SYMBOL(ftrace_likely_update);
  189. extern unsigned long __start_annotated_branch_profile[];
  190. extern unsigned long __stop_annotated_branch_profile[];
  191. static int annotated_branch_stat_headers(struct seq_file *m)
  192. {
  193. seq_puts(m, " correct incorrect % "
  194. " Function "
  195. " File Line\n"
  196. " ------- --------- - "
  197. " -------- "
  198. " ---- ----\n");
  199. return 0;
  200. }
  201. static inline long get_incorrect_percent(struct ftrace_branch_data *p)
  202. {
  203. long percent;
  204. if (p->correct) {
  205. percent = p->incorrect * 100;
  206. percent /= p->correct + p->incorrect;
  207. } else
  208. percent = p->incorrect ? 100 : -1;
  209. return percent;
  210. }
  211. static const char *branch_stat_process_file(struct ftrace_branch_data *p)
  212. {
  213. const char *f;
  214. /* Only print the file, not the path */
  215. f = p->file + strlen(p->file);
  216. while (f >= p->file && *f != '/')
  217. f--;
  218. return ++f;
  219. }
  220. static void branch_stat_show(struct seq_file *m,
  221. struct ftrace_branch_data *p, const char *f)
  222. {
  223. long percent;
  224. /*
  225. * The miss is overlayed on correct, and hit on incorrect.
  226. */
  227. percent = get_incorrect_percent(p);
  228. if (percent < 0)
  229. seq_puts(m, " X ");
  230. else
  231. seq_printf(m, "%3ld ", percent);
  232. seq_printf(m, "%-30.30s %-20.20s %d\n", p->func, f, p->line);
  233. }
  234. static int branch_stat_show_normal(struct seq_file *m,
  235. struct ftrace_branch_data *p, const char *f)
  236. {
  237. seq_printf(m, "%8lu %8lu ", p->correct, p->incorrect);
  238. branch_stat_show(m, p, f);
  239. return 0;
  240. }
  241. static int annotate_branch_stat_show(struct seq_file *m, void *v)
  242. {
  243. struct ftrace_likely_data *p = v;
  244. const char *f;
  245. int l;
  246. f = branch_stat_process_file(&p->data);
  247. if (!p->constant)
  248. return branch_stat_show_normal(m, &p->data, f);
  249. l = snprintf(NULL, 0, "/%lu", p->constant);
  250. l = l > 8 ? 0 : 8 - l;
  251. seq_printf(m, "%8lu/%lu %*lu ",
  252. p->data.correct, p->constant, l, p->data.incorrect);
  253. branch_stat_show(m, &p->data, f);
  254. return 0;
  255. }
  256. static void *annotated_branch_stat_start(struct tracer_stat *trace)
  257. {
  258. return __start_annotated_branch_profile;
  259. }
  260. static void *
  261. annotated_branch_stat_next(void *v, int idx)
  262. {
  263. struct ftrace_likely_data *p = v;
  264. ++p;
  265. if ((void *)p >= (void *)__stop_annotated_branch_profile)
  266. return NULL;
  267. return p;
  268. }
  269. static int annotated_branch_stat_cmp(void *p1, void *p2)
  270. {
  271. struct ftrace_branch_data *a = p1;
  272. struct ftrace_branch_data *b = p2;
  273. long percent_a, percent_b;
  274. percent_a = get_incorrect_percent(a);
  275. percent_b = get_incorrect_percent(b);
  276. if (percent_a < percent_b)
  277. return -1;
  278. if (percent_a > percent_b)
  279. return 1;
  280. if (a->incorrect < b->incorrect)
  281. return -1;
  282. if (a->incorrect > b->incorrect)
  283. return 1;
  284. /*
  285. * Since the above shows worse (incorrect) cases
  286. * first, we continue that by showing best (correct)
  287. * cases last.
  288. */
  289. if (a->correct > b->correct)
  290. return -1;
  291. if (a->correct < b->correct)
  292. return 1;
  293. return 0;
  294. }
  295. static struct tracer_stat annotated_branch_stats = {
  296. .name = "branch_annotated",
  297. .stat_start = annotated_branch_stat_start,
  298. .stat_next = annotated_branch_stat_next,
  299. .stat_cmp = annotated_branch_stat_cmp,
  300. .stat_headers = annotated_branch_stat_headers,
  301. .stat_show = annotate_branch_stat_show
  302. };
  303. __init static int init_annotated_branch_stats(void)
  304. {
  305. int ret;
  306. ret = register_stat_tracer(&annotated_branch_stats);
  307. if (!ret) {
  308. printk(KERN_WARNING "Warning: could not register "
  309. "annotated branches stats\n");
  310. return 1;
  311. }
  312. return 0;
  313. }
  314. fs_initcall(init_annotated_branch_stats);
  315. #ifdef CONFIG_PROFILE_ALL_BRANCHES
  316. extern unsigned long __start_branch_profile[];
  317. extern unsigned long __stop_branch_profile[];
  318. static int all_branch_stat_headers(struct seq_file *m)
  319. {
  320. seq_puts(m, " miss hit % "
  321. " Function "
  322. " File Line\n"
  323. " ------- --------- - "
  324. " -------- "
  325. " ---- ----\n");
  326. return 0;
  327. }
  328. static void *all_branch_stat_start(struct tracer_stat *trace)
  329. {
  330. return __start_branch_profile;
  331. }
  332. static void *
  333. all_branch_stat_next(void *v, int idx)
  334. {
  335. struct ftrace_branch_data *p = v;
  336. ++p;
  337. if ((void *)p >= (void *)__stop_branch_profile)
  338. return NULL;
  339. return p;
  340. }
  341. static int all_branch_stat_show(struct seq_file *m, void *v)
  342. {
  343. struct ftrace_branch_data *p = v;
  344. const char *f;
  345. f = branch_stat_process_file(p);
  346. return branch_stat_show_normal(m, p, f);
  347. }
  348. static struct tracer_stat all_branch_stats = {
  349. .name = "branch_all",
  350. .stat_start = all_branch_stat_start,
  351. .stat_next = all_branch_stat_next,
  352. .stat_headers = all_branch_stat_headers,
  353. .stat_show = all_branch_stat_show
  354. };
  355. __init static int all_annotated_branch_stats(void)
  356. {
  357. int ret;
  358. ret = register_stat_tracer(&all_branch_stats);
  359. if (!ret) {
  360. printk(KERN_WARNING "Warning: could not register "
  361. "all branches stats\n");
  362. return 1;
  363. }
  364. return 0;
  365. }
  366. fs_initcall(all_annotated_branch_stats);
  367. #endif /* CONFIG_PROFILE_ALL_BRANCHES */