trace_branch.c 8.6 KB

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