trace_branch.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * unlikely profiler
  4. *
  5. * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
  6. */
  7. #include <linux/kallsyms.h>
  8. #include <linux/seq_file.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/irqflags.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_likely_data *f, int val, int expect)
  27. {
  28. struct trace_event_call *call = &event_branch;
  29. struct trace_array *tr = branch_tracer;
  30. struct trace_array_cpu *data;
  31. struct ring_buffer_event *event;
  32. struct trace_branch *entry;
  33. struct ring_buffer *buffer;
  34. unsigned long flags;
  35. int pc;
  36. const char *p;
  37. if (current->trace_recursion & TRACE_BRANCH_BIT)
  38. return;
  39. /*
  40. * I would love to save just the ftrace_likely_data pointer, but
  41. * this code can also be used by modules. Ugly things can happen
  42. * if the module is unloaded, and then we go and read the
  43. * pointer. This is slower, but much safer.
  44. */
  45. if (unlikely(!tr))
  46. return;
  47. raw_local_irq_save(flags);
  48. current->trace_recursion |= TRACE_BRANCH_BIT;
  49. data = this_cpu_ptr(tr->trace_buffer.data);
  50. if (atomic_read(&data->disabled))
  51. goto out;
  52. pc = preempt_count();
  53. buffer = tr->trace_buffer.buffer;
  54. event = trace_buffer_lock_reserve(buffer, TRACE_BRANCH,
  55. sizeof(*entry), flags, pc);
  56. if (!event)
  57. goto out;
  58. entry = ring_buffer_event_data(event);
  59. /* Strip off the path, only save the file */
  60. p = f->data.file + strlen(f->data.file);
  61. while (p >= f->data.file && *p != '/')
  62. p--;
  63. p++;
  64. strncpy(entry->func, f->data.func, TRACE_FUNC_SIZE);
  65. strncpy(entry->file, p, TRACE_FILE_SIZE);
  66. entry->func[TRACE_FUNC_SIZE] = 0;
  67. entry->file[TRACE_FILE_SIZE] = 0;
  68. entry->constant = f->constant;
  69. entry->line = f->data.line;
  70. entry->correct = val == expect;
  71. if (!call_filter_check_discard(call, entry, buffer, event))
  72. trace_buffer_unlock_commit_nostack(buffer, event);
  73. out:
  74. current->trace_recursion &= ~TRACE_BRANCH_BIT;
  75. raw_local_irq_restore(flags);
  76. }
  77. static inline
  78. void trace_likely_condition(struct ftrace_likely_data *f, int val, int expect)
  79. {
  80. if (!branch_tracing_enabled)
  81. return;
  82. probe_likely_condition(f, val, expect);
  83. }
  84. int enable_branch_tracing(struct trace_array *tr)
  85. {
  86. mutex_lock(&branch_tracing_mutex);
  87. branch_tracer = tr;
  88. /*
  89. * Must be seen before enabling. The reader is a condition
  90. * where we do not need a matching rmb()
  91. */
  92. smp_wmb();
  93. branch_tracing_enabled++;
  94. mutex_unlock(&branch_tracing_mutex);
  95. return 0;
  96. }
  97. void disable_branch_tracing(void)
  98. {
  99. mutex_lock(&branch_tracing_mutex);
  100. if (!branch_tracing_enabled)
  101. goto out_unlock;
  102. branch_tracing_enabled--;
  103. out_unlock:
  104. mutex_unlock(&branch_tracing_mutex);
  105. }
  106. static int branch_trace_init(struct trace_array *tr)
  107. {
  108. return enable_branch_tracing(tr);
  109. }
  110. static void branch_trace_reset(struct trace_array *tr)
  111. {
  112. disable_branch_tracing();
  113. }
  114. static enum print_line_t trace_branch_print(struct trace_iterator *iter,
  115. int flags, struct trace_event *event)
  116. {
  117. struct trace_branch *field;
  118. trace_assign_type(field, iter->ent);
  119. 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_handle_return(&iter->seq);
  125. }
  126. static void branch_print_header(struct seq_file *s)
  127. {
  128. seq_puts(s, "# TASK-PID CPU# TIMESTAMP CORRECT"
  129. " FUNC:FILE:LINE\n"
  130. "# | | | | | "
  131. " |\n");
  132. }
  133. static struct trace_event_functions trace_branch_funcs = {
  134. .trace = trace_branch_print,
  135. };
  136. static struct trace_event trace_branch_event = {
  137. .type = TRACE_BRANCH,
  138. .funcs = &trace_branch_funcs,
  139. };
  140. static struct tracer branch_trace __read_mostly =
  141. {
  142. .name = "branch",
  143. .init = branch_trace_init,
  144. .reset = branch_trace_reset,
  145. #ifdef CONFIG_FTRACE_SELFTEST
  146. .selftest = trace_selftest_startup_branch,
  147. #endif /* CONFIG_FTRACE_SELFTEST */
  148. .print_header = branch_print_header,
  149. };
  150. __init static int init_branch_tracer(void)
  151. {
  152. int ret;
  153. ret = register_trace_event(&trace_branch_event);
  154. if (!ret) {
  155. printk(KERN_WARNING "Warning: could not register "
  156. "branch events\n");
  157. return 1;
  158. }
  159. return register_tracer(&branch_trace);
  160. }
  161. core_initcall(init_branch_tracer);
  162. #else
  163. static inline
  164. void trace_likely_condition(struct ftrace_likely_data *f, int val, int expect)
  165. {
  166. }
  167. #endif /* CONFIG_BRANCH_TRACER */
  168. void ftrace_likely_update(struct ftrace_likely_data *f, int val,
  169. int expect, int is_constant)
  170. {
  171. /* A constant is always correct */
  172. if (is_constant) {
  173. f->constant++;
  174. val = expect;
  175. }
  176. /*
  177. * I would love to have a trace point here instead, but the
  178. * trace point code is so inundated with unlikely and likely
  179. * conditions that the recursive nightmare that exists is too
  180. * much to try to get working. At least for now.
  181. */
  182. trace_likely_condition(f, val, expect);
  183. /* FIXME: Make this atomic! */
  184. if (val == expect)
  185. f->data.correct++;
  186. else
  187. f->data.incorrect++;
  188. }
  189. EXPORT_SYMBOL(ftrace_likely_update);
  190. extern unsigned long __start_annotated_branch_profile[];
  191. extern unsigned long __stop_annotated_branch_profile[];
  192. static int annotated_branch_stat_headers(struct seq_file *m)
  193. {
  194. seq_puts(m, " correct incorrect % "
  195. " Function "
  196. " File Line\n"
  197. " ------- --------- - "
  198. " -------- "
  199. " ---- ----\n");
  200. return 0;
  201. }
  202. static inline long get_incorrect_percent(struct ftrace_branch_data *p)
  203. {
  204. long percent;
  205. if (p->correct) {
  206. percent = p->incorrect * 100;
  207. percent /= p->correct + p->incorrect;
  208. } else
  209. percent = p->incorrect ? 100 : -1;
  210. return percent;
  211. }
  212. static const char *branch_stat_process_file(struct ftrace_branch_data *p)
  213. {
  214. const char *f;
  215. /* Only print the file, not the path */
  216. f = p->file + strlen(p->file);
  217. while (f >= p->file && *f != '/')
  218. f--;
  219. return ++f;
  220. }
  221. static void branch_stat_show(struct seq_file *m,
  222. struct ftrace_branch_data *p, const char *f)
  223. {
  224. long percent;
  225. /*
  226. * The miss is overlayed on correct, and hit on incorrect.
  227. */
  228. percent = get_incorrect_percent(p);
  229. if (percent < 0)
  230. seq_puts(m, " X ");
  231. else
  232. seq_printf(m, "%3ld ", percent);
  233. seq_printf(m, "%-30.30s %-20.20s %d\n", p->func, f, p->line);
  234. }
  235. static int branch_stat_show_normal(struct seq_file *m,
  236. struct ftrace_branch_data *p, const char *f)
  237. {
  238. seq_printf(m, "%8lu %8lu ", p->correct, p->incorrect);
  239. branch_stat_show(m, p, f);
  240. return 0;
  241. }
  242. static int annotate_branch_stat_show(struct seq_file *m, void *v)
  243. {
  244. struct ftrace_likely_data *p = v;
  245. const char *f;
  246. int l;
  247. f = branch_stat_process_file(&p->data);
  248. if (!p->constant)
  249. return branch_stat_show_normal(m, &p->data, f);
  250. l = snprintf(NULL, 0, "/%lu", p->constant);
  251. l = l > 8 ? 0 : 8 - l;
  252. seq_printf(m, "%8lu/%lu %*lu ",
  253. p->data.correct, p->constant, l, p->data.incorrect);
  254. branch_stat_show(m, &p->data, f);
  255. return 0;
  256. }
  257. static void *annotated_branch_stat_start(struct tracer_stat *trace)
  258. {
  259. return __start_annotated_branch_profile;
  260. }
  261. static void *
  262. annotated_branch_stat_next(void *v, int idx)
  263. {
  264. struct ftrace_likely_data *p = v;
  265. ++p;
  266. if ((void *)p >= (void *)__stop_annotated_branch_profile)
  267. return NULL;
  268. return p;
  269. }
  270. static int annotated_branch_stat_cmp(void *p1, void *p2)
  271. {
  272. struct ftrace_branch_data *a = p1;
  273. struct ftrace_branch_data *b = p2;
  274. long percent_a, percent_b;
  275. percent_a = get_incorrect_percent(a);
  276. percent_b = get_incorrect_percent(b);
  277. if (percent_a < percent_b)
  278. return -1;
  279. if (percent_a > percent_b)
  280. return 1;
  281. if (a->incorrect < b->incorrect)
  282. return -1;
  283. if (a->incorrect > b->incorrect)
  284. return 1;
  285. /*
  286. * Since the above shows worse (incorrect) cases
  287. * first, we continue that by showing best (correct)
  288. * cases last.
  289. */
  290. if (a->correct > b->correct)
  291. return -1;
  292. if (a->correct < b->correct)
  293. return 1;
  294. return 0;
  295. }
  296. static struct tracer_stat annotated_branch_stats = {
  297. .name = "branch_annotated",
  298. .stat_start = annotated_branch_stat_start,
  299. .stat_next = annotated_branch_stat_next,
  300. .stat_cmp = annotated_branch_stat_cmp,
  301. .stat_headers = annotated_branch_stat_headers,
  302. .stat_show = annotate_branch_stat_show
  303. };
  304. __init static int init_annotated_branch_stats(void)
  305. {
  306. int ret;
  307. ret = register_stat_tracer(&annotated_branch_stats);
  308. if (!ret) {
  309. printk(KERN_WARNING "Warning: could not register "
  310. "annotated branches stats\n");
  311. return 1;
  312. }
  313. return 0;
  314. }
  315. fs_initcall(init_annotated_branch_stats);
  316. #ifdef CONFIG_PROFILE_ALL_BRANCHES
  317. extern unsigned long __start_branch_profile[];
  318. extern unsigned long __stop_branch_profile[];
  319. static int all_branch_stat_headers(struct seq_file *m)
  320. {
  321. seq_puts(m, " miss hit % "
  322. " Function "
  323. " File Line\n"
  324. " ------- --------- - "
  325. " -------- "
  326. " ---- ----\n");
  327. return 0;
  328. }
  329. static void *all_branch_stat_start(struct tracer_stat *trace)
  330. {
  331. return __start_branch_profile;
  332. }
  333. static void *
  334. all_branch_stat_next(void *v, int idx)
  335. {
  336. struct ftrace_branch_data *p = v;
  337. ++p;
  338. if ((void *)p >= (void *)__stop_branch_profile)
  339. return NULL;
  340. return p;
  341. }
  342. static int all_branch_stat_show(struct seq_file *m, void *v)
  343. {
  344. struct ftrace_branch_data *p = v;
  345. const char *f;
  346. f = branch_stat_process_file(p);
  347. return branch_stat_show_normal(m, p, f);
  348. }
  349. static struct tracer_stat all_branch_stats = {
  350. .name = "branch_all",
  351. .stat_start = all_branch_stat_start,
  352. .stat_next = all_branch_stat_next,
  353. .stat_headers = all_branch_stat_headers,
  354. .stat_show = all_branch_stat_show
  355. };
  356. __init static int all_annotated_branch_stats(void)
  357. {
  358. int ret;
  359. ret = register_stat_tracer(&all_branch_stats);
  360. if (!ret) {
  361. printk(KERN_WARNING "Warning: could not register "
  362. "all branches stats\n");
  363. return 1;
  364. }
  365. return 0;
  366. }
  367. fs_initcall(all_annotated_branch_stats);
  368. #endif /* CONFIG_PROFILE_ALL_BRANCHES */