trace_branch.c 8.8 KB

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