trace_branch.c 8.6 KB

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