trace_branch.c 9.0 KB

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