trace_branch.c 8.2 KB

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