trace_hw_branches.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * h/w branch tracer for x86 based on BTS
  3. *
  4. * Copyright (C) 2008-2009 Intel Corporation.
  5. * Markus Metzger <markus.t.metzger@gmail.com>, 2008-2009
  6. */
  7. #include <linux/kallsyms.h>
  8. #include <linux/debugfs.h>
  9. #include <linux/ftrace.h>
  10. #include <linux/module.h>
  11. #include <linux/cpu.h>
  12. #include <linux/smp.h>
  13. #include <linux/fs.h>
  14. #include <asm/ds.h>
  15. #include "trace_output.h"
  16. #include "trace.h"
  17. #define BTS_BUFFER_SIZE (1 << 13)
  18. static DEFINE_PER_CPU(struct bts_tracer *, tracer);
  19. static DEFINE_PER_CPU(unsigned char[BTS_BUFFER_SIZE], buffer);
  20. #define this_tracer per_cpu(tracer, smp_processor_id())
  21. static int trace_hw_branches_enabled __read_mostly;
  22. static int trace_hw_branches_suspended __read_mostly;
  23. static struct trace_array *hw_branch_trace __read_mostly;
  24. static void bts_trace_init_cpu(int cpu)
  25. {
  26. per_cpu(tracer, cpu) =
  27. ds_request_bts_cpu(cpu, per_cpu(buffer, cpu), BTS_BUFFER_SIZE,
  28. NULL, (size_t)-1, BTS_KERNEL);
  29. if (IS_ERR(per_cpu(tracer, cpu)))
  30. per_cpu(tracer, cpu) = NULL;
  31. }
  32. static int bts_trace_init(struct trace_array *tr)
  33. {
  34. int cpu;
  35. hw_branch_trace = tr;
  36. trace_hw_branches_enabled = 0;
  37. get_online_cpus();
  38. for_each_online_cpu(cpu) {
  39. bts_trace_init_cpu(cpu);
  40. if (likely(per_cpu(tracer, cpu)))
  41. trace_hw_branches_enabled = 1;
  42. }
  43. trace_hw_branches_suspended = 0;
  44. put_online_cpus();
  45. /* If we could not enable tracing on a single cpu, we fail. */
  46. return trace_hw_branches_enabled ? 0 : -EOPNOTSUPP;
  47. }
  48. static void bts_trace_reset(struct trace_array *tr)
  49. {
  50. int cpu;
  51. get_online_cpus();
  52. for_each_online_cpu(cpu) {
  53. if (likely(per_cpu(tracer, cpu))) {
  54. ds_release_bts(per_cpu(tracer, cpu));
  55. per_cpu(tracer, cpu) = NULL;
  56. }
  57. }
  58. trace_hw_branches_enabled = 0;
  59. trace_hw_branches_suspended = 0;
  60. put_online_cpus();
  61. }
  62. static void bts_trace_start(struct trace_array *tr)
  63. {
  64. int cpu;
  65. get_online_cpus();
  66. for_each_online_cpu(cpu)
  67. if (likely(per_cpu(tracer, cpu)))
  68. ds_resume_bts(per_cpu(tracer, cpu));
  69. trace_hw_branches_suspended = 0;
  70. put_online_cpus();
  71. }
  72. static void bts_trace_stop(struct trace_array *tr)
  73. {
  74. int cpu;
  75. get_online_cpus();
  76. for_each_online_cpu(cpu)
  77. if (likely(per_cpu(tracer, cpu)))
  78. ds_suspend_bts(per_cpu(tracer, cpu));
  79. trace_hw_branches_suspended = 1;
  80. put_online_cpus();
  81. }
  82. static int __cpuinit bts_hotcpu_handler(struct notifier_block *nfb,
  83. unsigned long action, void *hcpu)
  84. {
  85. int cpu = (long)hcpu;
  86. switch (action) {
  87. case CPU_ONLINE:
  88. case CPU_DOWN_FAILED:
  89. /* The notification is sent with interrupts enabled. */
  90. if (trace_hw_branches_enabled) {
  91. bts_trace_init_cpu(cpu);
  92. if (trace_hw_branches_suspended &&
  93. likely(per_cpu(tracer, cpu)))
  94. ds_suspend_bts(per_cpu(tracer, cpu));
  95. }
  96. break;
  97. case CPU_DOWN_PREPARE:
  98. /* The notification is sent with interrupts enabled. */
  99. if (likely(per_cpu(tracer, cpu))) {
  100. ds_release_bts(per_cpu(tracer, cpu));
  101. per_cpu(tracer, cpu) = NULL;
  102. }
  103. }
  104. return NOTIFY_DONE;
  105. }
  106. static struct notifier_block bts_hotcpu_notifier __cpuinitdata = {
  107. .notifier_call = bts_hotcpu_handler
  108. };
  109. static void bts_trace_print_header(struct seq_file *m)
  110. {
  111. seq_puts(m, "# CPU# TO <- FROM\n");
  112. }
  113. static enum print_line_t bts_trace_print_line(struct trace_iterator *iter)
  114. {
  115. unsigned long symflags = TRACE_ITER_SYM_OFFSET;
  116. struct trace_entry *entry = iter->ent;
  117. struct trace_seq *seq = &iter->seq;
  118. struct hw_branch_entry *it;
  119. trace_assign_type(it, entry);
  120. if (entry->type == TRACE_HW_BRANCHES) {
  121. if (trace_seq_printf(seq, "%4d ", iter->cpu) &&
  122. seq_print_ip_sym(seq, it->to, symflags) &&
  123. trace_seq_printf(seq, "\t <- ") &&
  124. seq_print_ip_sym(seq, it->from, symflags) &&
  125. trace_seq_printf(seq, "\n"))
  126. return TRACE_TYPE_HANDLED;
  127. return TRACE_TYPE_PARTIAL_LINE;;
  128. }
  129. return TRACE_TYPE_UNHANDLED;
  130. }
  131. void trace_hw_branch(u64 from, u64 to)
  132. {
  133. struct ftrace_event_call *call = &event_hw_branch;
  134. struct trace_array *tr = hw_branch_trace;
  135. struct ring_buffer_event *event;
  136. struct hw_branch_entry *entry;
  137. unsigned long irq1;
  138. int cpu;
  139. if (unlikely(!tr))
  140. return;
  141. if (unlikely(!trace_hw_branches_enabled))
  142. return;
  143. local_irq_save(irq1);
  144. cpu = raw_smp_processor_id();
  145. if (atomic_inc_return(&tr->data[cpu]->disabled) != 1)
  146. goto out;
  147. event = trace_buffer_lock_reserve(tr, TRACE_HW_BRANCHES,
  148. sizeof(*entry), 0, 0);
  149. if (!event)
  150. goto out;
  151. entry = ring_buffer_event_data(event);
  152. tracing_generic_entry_update(&entry->ent, 0, from);
  153. entry->ent.type = TRACE_HW_BRANCHES;
  154. entry->from = from;
  155. entry->to = to;
  156. if (!filter_check_discard(call, entry, tr->buffer, event))
  157. trace_buffer_unlock_commit(tr, event, 0, 0);
  158. out:
  159. atomic_dec(&tr->data[cpu]->disabled);
  160. local_irq_restore(irq1);
  161. }
  162. static void trace_bts_at(const struct bts_trace *trace, void *at)
  163. {
  164. struct bts_struct bts;
  165. int err = 0;
  166. WARN_ON_ONCE(!trace->read);
  167. if (!trace->read)
  168. return;
  169. err = trace->read(this_tracer, at, &bts);
  170. if (err < 0)
  171. return;
  172. switch (bts.qualifier) {
  173. case BTS_BRANCH:
  174. trace_hw_branch(bts.variant.lbr.from, bts.variant.lbr.to);
  175. break;
  176. }
  177. }
  178. /*
  179. * Collect the trace on the current cpu and write it into the ftrace buffer.
  180. *
  181. * pre: tracing must be suspended on the current cpu
  182. */
  183. static void trace_bts_cpu(void *arg)
  184. {
  185. struct trace_array *tr = (struct trace_array *)arg;
  186. const struct bts_trace *trace;
  187. unsigned char *at;
  188. if (unlikely(!tr))
  189. return;
  190. if (unlikely(atomic_read(&tr->data[raw_smp_processor_id()]->disabled)))
  191. return;
  192. if (unlikely(!this_tracer))
  193. return;
  194. trace = ds_read_bts(this_tracer);
  195. if (!trace)
  196. return;
  197. for (at = trace->ds.top; (void *)at < trace->ds.end;
  198. at += trace->ds.size)
  199. trace_bts_at(trace, at);
  200. for (at = trace->ds.begin; (void *)at < trace->ds.top;
  201. at += trace->ds.size)
  202. trace_bts_at(trace, at);
  203. }
  204. static void trace_bts_prepare(struct trace_iterator *iter)
  205. {
  206. int cpu;
  207. get_online_cpus();
  208. for_each_online_cpu(cpu)
  209. if (likely(per_cpu(tracer, cpu)))
  210. ds_suspend_bts(per_cpu(tracer, cpu));
  211. /*
  212. * We need to collect the trace on the respective cpu since ftrace
  213. * implicitly adds the record for the current cpu.
  214. * Once that is more flexible, we could collect the data from any cpu.
  215. */
  216. on_each_cpu(trace_bts_cpu, iter->tr, 1);
  217. for_each_online_cpu(cpu)
  218. if (likely(per_cpu(tracer, cpu)))
  219. ds_resume_bts(per_cpu(tracer, cpu));
  220. put_online_cpus();
  221. }
  222. static void trace_bts_close(struct trace_iterator *iter)
  223. {
  224. tracing_reset_online_cpus(iter->tr);
  225. }
  226. void trace_hw_branch_oops(void)
  227. {
  228. if (this_tracer) {
  229. ds_suspend_bts_noirq(this_tracer);
  230. trace_bts_cpu(hw_branch_trace);
  231. ds_resume_bts_noirq(this_tracer);
  232. }
  233. }
  234. struct tracer bts_tracer __read_mostly =
  235. {
  236. .name = "hw-branch-tracer",
  237. .init = bts_trace_init,
  238. .reset = bts_trace_reset,
  239. .print_header = bts_trace_print_header,
  240. .print_line = bts_trace_print_line,
  241. .start = bts_trace_start,
  242. .stop = bts_trace_stop,
  243. .open = trace_bts_prepare,
  244. .close = trace_bts_close,
  245. #ifdef CONFIG_FTRACE_SELFTEST
  246. .selftest = trace_selftest_startup_hw_branches,
  247. #endif /* CONFIG_FTRACE_SELFTEST */
  248. };
  249. __init static int init_bts_trace(void)
  250. {
  251. register_hotcpu_notifier(&bts_hotcpu_notifier);
  252. return register_tracer(&bts_tracer);
  253. }
  254. device_initcall(init_bts_trace);