trace_branch.c 8.5 KB

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