trace_hw_branches.c 7.2 KB

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