trace_hw_branches.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/spinlock.h>
  8. #include <linux/kallsyms.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/ftrace.h>
  11. #include <linux/module.h>
  12. #include <linux/cpu.h>
  13. #include <linux/smp.h>
  14. #include <linux/fs.h>
  15. #include <asm/ds.h>
  16. #include "trace.h"
  17. #include "trace_output.h"
  18. #define SIZEOF_BTS (1 << 13)
  19. /*
  20. * The tracer lock protects the below per-cpu tracer array.
  21. * It needs to be held to:
  22. * - start tracing on all cpus
  23. * - stop tracing on all cpus
  24. * - start tracing on a single hotplug cpu
  25. * - stop tracing on a single hotplug cpu
  26. * - read the trace from all cpus
  27. * - read the trace from a single cpu
  28. */
  29. static DEFINE_SPINLOCK(bts_tracer_lock);
  30. static DEFINE_PER_CPU(struct bts_tracer *, tracer);
  31. static DEFINE_PER_CPU(unsigned char[SIZEOF_BTS], buffer);
  32. #define this_tracer per_cpu(tracer, smp_processor_id())
  33. #define this_buffer per_cpu(buffer, smp_processor_id())
  34. static int __read_mostly trace_hw_branches_enabled;
  35. static struct trace_array *hw_branch_trace __read_mostly;
  36. /*
  37. * Start tracing on the current cpu.
  38. * The argument is ignored.
  39. *
  40. * pre: bts_tracer_lock must be locked.
  41. */
  42. static void bts_trace_start_cpu(void *arg)
  43. {
  44. if (this_tracer)
  45. ds_release_bts(this_tracer);
  46. this_tracer =
  47. ds_request_bts(/* task = */ NULL, this_buffer, SIZEOF_BTS,
  48. /* ovfl = */ NULL, /* th = */ (size_t)-1,
  49. BTS_KERNEL);
  50. if (IS_ERR(this_tracer)) {
  51. this_tracer = NULL;
  52. return;
  53. }
  54. }
  55. static void bts_trace_start(struct trace_array *tr)
  56. {
  57. spin_lock(&bts_tracer_lock);
  58. on_each_cpu(bts_trace_start_cpu, NULL, 1);
  59. trace_hw_branches_enabled = 1;
  60. spin_unlock(&bts_tracer_lock);
  61. }
  62. /*
  63. * Stop tracing on the current cpu.
  64. * The argument is ignored.
  65. *
  66. * pre: bts_tracer_lock must be locked.
  67. */
  68. static void bts_trace_stop_cpu(void *arg)
  69. {
  70. if (this_tracer) {
  71. ds_release_bts(this_tracer);
  72. this_tracer = NULL;
  73. }
  74. }
  75. static void bts_trace_stop(struct trace_array *tr)
  76. {
  77. spin_lock(&bts_tracer_lock);
  78. trace_hw_branches_enabled = 0;
  79. on_each_cpu(bts_trace_stop_cpu, NULL, 1);
  80. spin_unlock(&bts_tracer_lock);
  81. }
  82. static int __cpuinit bts_hotcpu_handler(struct notifier_block *nfb,
  83. unsigned long action, void *hcpu)
  84. {
  85. unsigned int cpu = (unsigned long)hcpu;
  86. spin_lock(&bts_tracer_lock);
  87. if (!trace_hw_branches_enabled)
  88. goto out;
  89. switch (action) {
  90. case CPU_ONLINE:
  91. case CPU_DOWN_FAILED:
  92. smp_call_function_single(cpu, bts_trace_start_cpu, NULL, 1);
  93. break;
  94. case CPU_DOWN_PREPARE:
  95. smp_call_function_single(cpu, bts_trace_stop_cpu, NULL, 1);
  96. break;
  97. }
  98. out:
  99. spin_unlock(&bts_tracer_lock);
  100. return NOTIFY_DONE;
  101. }
  102. static struct notifier_block bts_hotcpu_notifier __cpuinitdata = {
  103. .notifier_call = bts_hotcpu_handler
  104. };
  105. static int bts_trace_init(struct trace_array *tr)
  106. {
  107. hw_branch_trace = tr;
  108. bts_trace_start(tr);
  109. return 0;
  110. }
  111. static void bts_trace_reset(struct trace_array *tr)
  112. {
  113. bts_trace_stop(tr);
  114. }
  115. static void bts_trace_print_header(struct seq_file *m)
  116. {
  117. seq_puts(m, "# CPU# TO <- FROM\n");
  118. }
  119. static enum print_line_t bts_trace_print_line(struct trace_iterator *iter)
  120. {
  121. struct trace_entry *entry = iter->ent;
  122. struct trace_seq *seq = &iter->seq;
  123. struct hw_branch_entry *it;
  124. unsigned long symflags = TRACE_ITER_SYM_OFFSET;
  125. trace_assign_type(it, entry);
  126. if (entry->type == TRACE_HW_BRANCHES) {
  127. if (trace_seq_printf(seq, "%4d ", iter->cpu) &&
  128. seq_print_ip_sym(seq, it->to, symflags) &&
  129. trace_seq_printf(seq, "\t <- ") &&
  130. seq_print_ip_sym(seq, it->from, symflags) &&
  131. trace_seq_printf(seq, "\n"))
  132. return TRACE_TYPE_HANDLED;
  133. return TRACE_TYPE_PARTIAL_LINE;;
  134. }
  135. return TRACE_TYPE_UNHANDLED;
  136. }
  137. void trace_hw_branch(u64 from, u64 to)
  138. {
  139. struct trace_array *tr = hw_branch_trace;
  140. struct ring_buffer_event *event;
  141. struct hw_branch_entry *entry;
  142. unsigned long irq1;
  143. int cpu;
  144. if (unlikely(!tr))
  145. return;
  146. if (unlikely(!trace_hw_branches_enabled))
  147. return;
  148. local_irq_save(irq1);
  149. cpu = raw_smp_processor_id();
  150. if (atomic_inc_return(&tr->data[cpu]->disabled) != 1)
  151. goto out;
  152. event = trace_buffer_lock_reserve(tr, TRACE_HW_BRANCHES,
  153. sizeof(*entry), 0, 0);
  154. if (!event)
  155. goto out;
  156. entry = ring_buffer_event_data(event);
  157. tracing_generic_entry_update(&entry->ent, 0, from);
  158. entry->ent.type = TRACE_HW_BRANCHES;
  159. entry->from = from;
  160. entry->to = to;
  161. trace_buffer_unlock_commit(tr, event, 0, 0);
  162. out:
  163. atomic_dec(&tr->data[cpu]->disabled);
  164. local_irq_restore(irq1);
  165. }
  166. static void trace_bts_at(const struct bts_trace *trace, void *at)
  167. {
  168. struct bts_struct bts;
  169. int err = 0;
  170. WARN_ON_ONCE(!trace->read);
  171. if (!trace->read)
  172. return;
  173. err = trace->read(this_tracer, at, &bts);
  174. if (err < 0)
  175. return;
  176. switch (bts.qualifier) {
  177. case BTS_BRANCH:
  178. trace_hw_branch(bts.variant.lbr.from, bts.variant.lbr.to);
  179. break;
  180. }
  181. }
  182. /*
  183. * Collect the trace on the current cpu and write it into the ftrace buffer.
  184. *
  185. * pre: bts_tracer_lock must be locked
  186. */
  187. static void trace_bts_cpu(void *arg)
  188. {
  189. struct trace_array *tr = (struct trace_array *) arg;
  190. const struct bts_trace *trace;
  191. unsigned char *at;
  192. if (unlikely(!tr))
  193. return;
  194. if (unlikely(atomic_read(&tr->data[raw_smp_processor_id()]->disabled)))
  195. return;
  196. if (unlikely(!this_tracer))
  197. return;
  198. ds_suspend_bts(this_tracer);
  199. trace = ds_read_bts(this_tracer);
  200. if (!trace)
  201. goto out;
  202. for (at = trace->ds.top; (void *)at < trace->ds.end;
  203. at += trace->ds.size)
  204. trace_bts_at(trace, at);
  205. for (at = trace->ds.begin; (void *)at < trace->ds.top;
  206. at += trace->ds.size)
  207. trace_bts_at(trace, at);
  208. out:
  209. ds_resume_bts(this_tracer);
  210. }
  211. static void trace_bts_prepare(struct trace_iterator *iter)
  212. {
  213. spin_lock(&bts_tracer_lock);
  214. on_each_cpu(trace_bts_cpu, iter->tr, 1);
  215. spin_unlock(&bts_tracer_lock);
  216. }
  217. static void trace_bts_close(struct trace_iterator *iter)
  218. {
  219. tracing_reset_online_cpus(iter->tr);
  220. }
  221. void trace_hw_branch_oops(void)
  222. {
  223. spin_lock(&bts_tracer_lock);
  224. trace_bts_cpu(hw_branch_trace);
  225. spin_unlock(&bts_tracer_lock);
  226. }
  227. struct tracer bts_tracer __read_mostly =
  228. {
  229. .name = "hw-branch-tracer",
  230. .init = bts_trace_init,
  231. .reset = bts_trace_reset,
  232. .print_header = bts_trace_print_header,
  233. .print_line = bts_trace_print_line,
  234. .start = bts_trace_start,
  235. .stop = bts_trace_stop,
  236. .open = trace_bts_prepare,
  237. .close = trace_bts_close
  238. };
  239. __init static int init_bts_trace(void)
  240. {
  241. register_hotcpu_notifier(&bts_hotcpu_notifier);
  242. return register_tracer(&bts_tracer);
  243. }
  244. device_initcall(init_bts_trace);