trace_hw_branches.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 ring_buffer *buf;
  137. struct hw_branch_entry *entry;
  138. unsigned long irq1;
  139. int cpu;
  140. if (unlikely(!tr))
  141. return;
  142. if (unlikely(!trace_hw_branches_enabled))
  143. return;
  144. local_irq_save(irq1);
  145. cpu = raw_smp_processor_id();
  146. if (atomic_inc_return(&tr->data[cpu]->disabled) != 1)
  147. goto out;
  148. buf = tr->buffer;
  149. event = trace_buffer_lock_reserve(buf, TRACE_HW_BRANCHES,
  150. sizeof(*entry), 0, 0);
  151. if (!event)
  152. goto out;
  153. entry = ring_buffer_event_data(event);
  154. tracing_generic_entry_update(&entry->ent, 0, from);
  155. entry->ent.type = TRACE_HW_BRANCHES;
  156. entry->from = from;
  157. entry->to = to;
  158. if (!filter_check_discard(call, entry, buf, event))
  159. trace_buffer_unlock_commit(buf, event, 0, 0);
  160. out:
  161. atomic_dec(&tr->data[cpu]->disabled);
  162. local_irq_restore(irq1);
  163. }
  164. static void trace_bts_at(const struct bts_trace *trace, void *at)
  165. {
  166. struct bts_struct bts;
  167. int err = 0;
  168. WARN_ON_ONCE(!trace->read);
  169. if (!trace->read)
  170. return;
  171. err = trace->read(this_tracer, at, &bts);
  172. if (err < 0)
  173. return;
  174. switch (bts.qualifier) {
  175. case BTS_BRANCH:
  176. trace_hw_branch(bts.variant.lbr.from, bts.variant.lbr.to);
  177. break;
  178. }
  179. }
  180. /*
  181. * Collect the trace on the current cpu and write it into the ftrace buffer.
  182. *
  183. * pre: tracing must be suspended on the current cpu
  184. */
  185. static void trace_bts_cpu(void *arg)
  186. {
  187. struct trace_array *tr = (struct trace_array *)arg;
  188. const struct bts_trace *trace;
  189. unsigned char *at;
  190. if (unlikely(!tr))
  191. return;
  192. if (unlikely(atomic_read(&tr->data[raw_smp_processor_id()]->disabled)))
  193. return;
  194. if (unlikely(!this_tracer))
  195. return;
  196. trace = ds_read_bts(this_tracer);
  197. if (!trace)
  198. return;
  199. for (at = trace->ds.top; (void *)at < trace->ds.end;
  200. at += trace->ds.size)
  201. trace_bts_at(trace, at);
  202. for (at = trace->ds.begin; (void *)at < trace->ds.top;
  203. at += trace->ds.size)
  204. trace_bts_at(trace, at);
  205. }
  206. static void trace_bts_prepare(struct trace_iterator *iter)
  207. {
  208. int cpu;
  209. get_online_cpus();
  210. for_each_online_cpu(cpu)
  211. if (likely(per_cpu(tracer, cpu)))
  212. ds_suspend_bts(per_cpu(tracer, cpu));
  213. /*
  214. * We need to collect the trace on the respective cpu since ftrace
  215. * implicitly adds the record for the current cpu.
  216. * Once that is more flexible, we could collect the data from any cpu.
  217. */
  218. on_each_cpu(trace_bts_cpu, iter->tr, 1);
  219. for_each_online_cpu(cpu)
  220. if (likely(per_cpu(tracer, cpu)))
  221. ds_resume_bts(per_cpu(tracer, cpu));
  222. put_online_cpus();
  223. }
  224. static void trace_bts_close(struct trace_iterator *iter)
  225. {
  226. tracing_reset_online_cpus(iter->tr);
  227. }
  228. void trace_hw_branch_oops(void)
  229. {
  230. if (this_tracer) {
  231. ds_suspend_bts_noirq(this_tracer);
  232. trace_bts_cpu(hw_branch_trace);
  233. ds_resume_bts_noirq(this_tracer);
  234. }
  235. }
  236. struct tracer bts_tracer __read_mostly =
  237. {
  238. .name = "hw-branch-tracer",
  239. .init = bts_trace_init,
  240. .reset = bts_trace_reset,
  241. .print_header = bts_trace_print_header,
  242. .print_line = bts_trace_print_line,
  243. .start = bts_trace_start,
  244. .stop = bts_trace_stop,
  245. .open = trace_bts_prepare,
  246. .close = trace_bts_close,
  247. #ifdef CONFIG_FTRACE_SELFTEST
  248. .selftest = trace_selftest_startup_hw_branches,
  249. #endif /* CONFIG_FTRACE_SELFTEST */
  250. };
  251. __init static int init_bts_trace(void)
  252. {
  253. register_hotcpu_notifier(&bts_hotcpu_notifier);
  254. return register_tracer(&bts_tracer);
  255. }
  256. device_initcall(init_bts_trace);