trace_branch.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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_buffer *buffer;
  31. struct trace_array_cpu *data;
  32. struct ring_buffer_event *event;
  33. struct trace_branch *entry;
  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->array_buffer.data);
  50. if (atomic_read(&data->disabled))
  51. goto out;
  52. pc = preempt_count();
  53. buffer = tr->array_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. unsigned long flags = user_access_save();
  172. /* A constant is always correct */
  173. if (is_constant) {
  174. f->constant++;
  175. val = expect;
  176. }
  177. /*
  178. * I would love to have a trace point here instead, but the
  179. * trace point code is so inundated with unlikely and likely
  180. * conditions that the recursive nightmare that exists is too
  181. * much to try to get working. At least for now.
  182. */
  183. trace_likely_condition(f, val, expect);
  184. /* FIXME: Make this atomic! */
  185. if (val == expect)
  186. f->data.correct++;
  187. else
  188. f->data.incorrect++;
  189. user_access_restore(flags);
  190. }
  191. EXPORT_SYMBOL(ftrace_likely_update);
  192. extern unsigned long __start_annotated_branch_profile[];
  193. extern unsigned long __stop_annotated_branch_profile[];
  194. static int annotated_branch_stat_headers(struct seq_file *m)
  195. {
  196. seq_puts(m, " correct incorrect % "
  197. " Function "
  198. " File Line\n"
  199. " ------- --------- - "
  200. " -------- "
  201. " ---- ----\n");
  202. return 0;
  203. }
  204. static inline long get_incorrect_percent(const struct ftrace_branch_data *p)
  205. {
  206. long percent;
  207. if (p->correct) {
  208. percent = p->incorrect * 100;
  209. percent /= p->correct + p->incorrect;
  210. } else
  211. percent = p->incorrect ? 100 : -1;
  212. return percent;
  213. }
  214. static const char *branch_stat_process_file(struct ftrace_branch_data *p)
  215. {
  216. const char *f;
  217. /* Only print the file, not the path */
  218. f = p->file + strlen(p->file);
  219. while (f >= p->file && *f != '/')
  220. f--;
  221. return ++f;
  222. }
  223. static void branch_stat_show(struct seq_file *m,
  224. struct ftrace_branch_data *p, const char *f)
  225. {
  226. long percent;
  227. /*
  228. * The miss is overlayed on correct, and hit on incorrect.
  229. */
  230. percent = get_incorrect_percent(p);
  231. if (percent < 0)
  232. seq_puts(m, " X ");
  233. else
  234. seq_printf(m, "%3ld ", percent);
  235. seq_printf(m, "%-30.30s %-20.20s %d\n", p->func, f, p->line);
  236. }
  237. static int branch_stat_show_normal(struct seq_file *m,
  238. struct ftrace_branch_data *p, const char *f)
  239. {
  240. seq_printf(m, "%8lu %8lu ", p->correct, p->incorrect);
  241. branch_stat_show(m, p, f);
  242. return 0;
  243. }
  244. static int annotate_branch_stat_show(struct seq_file *m, void *v)
  245. {
  246. struct ftrace_likely_data *p = v;
  247. const char *f;
  248. int l;
  249. f = branch_stat_process_file(&p->data);
  250. if (!p->constant)
  251. return branch_stat_show_normal(m, &p->data, f);
  252. l = snprintf(NULL, 0, "/%lu", p->constant);
  253. l = l > 8 ? 0 : 8 - l;
  254. seq_printf(m, "%8lu/%lu %*lu ",
  255. p->data.correct, p->constant, l, p->data.incorrect);
  256. branch_stat_show(m, &p->data, f);
  257. return 0;
  258. }
  259. static void *annotated_branch_stat_start(struct tracer_stat *trace)
  260. {
  261. return __start_annotated_branch_profile;
  262. }
  263. static void *
  264. annotated_branch_stat_next(void *v, int idx)
  265. {
  266. struct ftrace_likely_data *p = v;
  267. ++p;
  268. if ((void *)p >= (void *)__stop_annotated_branch_profile)
  269. return NULL;
  270. return p;
  271. }
  272. static int annotated_branch_stat_cmp(const void *p1, const void *p2)
  273. {
  274. const struct ftrace_branch_data *a = p1;
  275. const struct ftrace_branch_data *b = p2;
  276. long percent_a, percent_b;
  277. percent_a = get_incorrect_percent(a);
  278. percent_b = get_incorrect_percent(b);
  279. if (percent_a < percent_b)
  280. return -1;
  281. if (percent_a > percent_b)
  282. return 1;
  283. if (a->incorrect < b->incorrect)
  284. return -1;
  285. if (a->incorrect > b->incorrect)
  286. return 1;
  287. /*
  288. * Since the above shows worse (incorrect) cases
  289. * first, we continue that by showing best (correct)
  290. * cases last.
  291. */
  292. if (a->correct > b->correct)
  293. return -1;
  294. if (a->correct < b->correct)
  295. return 1;
  296. return 0;
  297. }
  298. static struct tracer_stat annotated_branch_stats = {
  299. .name = "branch_annotated",
  300. .stat_start = annotated_branch_stat_start,
  301. .stat_next = annotated_branch_stat_next,
  302. .stat_cmp = annotated_branch_stat_cmp,
  303. .stat_headers = annotated_branch_stat_headers,
  304. .stat_show = annotate_branch_stat_show
  305. };
  306. __init static int init_annotated_branch_stats(void)
  307. {
  308. int ret;
  309. ret = register_stat_tracer(&annotated_branch_stats);
  310. if (!ret) {
  311. printk(KERN_WARNING "Warning: could not register "
  312. "annotated branches stats\n");
  313. return 1;
  314. }
  315. return 0;
  316. }
  317. fs_initcall(init_annotated_branch_stats);
  318. #ifdef CONFIG_PROFILE_ALL_BRANCHES
  319. extern unsigned long __start_branch_profile[];
  320. extern unsigned long __stop_branch_profile[];
  321. static int all_branch_stat_headers(struct seq_file *m)
  322. {
  323. seq_puts(m, " miss hit % "
  324. " Function "
  325. " File Line\n"
  326. " ------- --------- - "
  327. " -------- "
  328. " ---- ----\n");
  329. return 0;
  330. }
  331. static void *all_branch_stat_start(struct tracer_stat *trace)
  332. {
  333. return __start_branch_profile;
  334. }
  335. static void *
  336. all_branch_stat_next(void *v, int idx)
  337. {
  338. struct ftrace_branch_data *p = v;
  339. ++p;
  340. if ((void *)p >= (void *)__stop_branch_profile)
  341. return NULL;
  342. return p;
  343. }
  344. static int all_branch_stat_show(struct seq_file *m, void *v)
  345. {
  346. struct ftrace_branch_data *p = v;
  347. const char *f;
  348. f = branch_stat_process_file(p);
  349. return branch_stat_show_normal(m, p, f);
  350. }
  351. static struct tracer_stat all_branch_stats = {
  352. .name = "branch_all",
  353. .stat_start = all_branch_stat_start,
  354. .stat_next = all_branch_stat_next,
  355. .stat_headers = all_branch_stat_headers,
  356. .stat_show = all_branch_stat_show
  357. };
  358. __init static int all_annotated_branch_stats(void)
  359. {
  360. int ret;
  361. ret = register_stat_tracer(&all_branch_stats);
  362. if (!ret) {
  363. printk(KERN_WARNING "Warning: could not register "
  364. "all branches stats\n");
  365. return 1;
  366. }
  367. return 0;
  368. }
  369. fs_initcall(all_annotated_branch_stats);
  370. #endif /* CONFIG_PROFILE_ALL_BRANCHES */