trace_hw_branches.c 6.5 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. */
  8. #include <linux/module.h>
  9. #include <linux/fs.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/ftrace.h>
  12. #include <linux/kallsyms.h>
  13. #include <linux/mutex.h>
  14. #include <linux/cpu.h>
  15. #include <linux/smp.h>
  16. #include <asm/ds.h>
  17. #include "trace.h"
  18. #include "trace_output.h"
  19. #define SIZEOF_BTS (1 << 13)
  20. /* The tracer mutex 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_MUTEX(bts_tracer_mutex);
  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_mutex 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. mutex_lock(&bts_tracer_mutex);
  58. on_each_cpu(bts_trace_start_cpu, NULL, 1);
  59. trace_hw_branches_enabled = 1;
  60. mutex_unlock(&bts_tracer_mutex);
  61. }
  62. /*
  63. * Start tracing on the current cpu.
  64. * The argument is ignored.
  65. *
  66. * pre: bts_tracer_mutex 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. mutex_lock(&bts_tracer_mutex);
  78. trace_hw_branches_enabled = 0;
  79. on_each_cpu(bts_trace_stop_cpu, NULL, 1);
  80. mutex_unlock(&bts_tracer_mutex);
  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. mutex_lock(&bts_tracer_mutex);
  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. mutex_unlock(&bts_tracer_mutex);
  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. register_hotcpu_notifier(&bts_hotcpu_notifier);
  109. tracing_reset_online_cpus(tr);
  110. bts_trace_start(tr);
  111. return 0;
  112. }
  113. static void bts_trace_reset(struct trace_array *tr)
  114. {
  115. bts_trace_stop(tr);
  116. unregister_hotcpu_notifier(&bts_hotcpu_notifier);
  117. }
  118. static void bts_trace_print_header(struct seq_file *m)
  119. {
  120. seq_puts(m,
  121. "# CPU# FROM TO FUNCTION\n");
  122. seq_puts(m,
  123. "# | | | |\n");
  124. }
  125. static enum print_line_t bts_trace_print_line(struct trace_iterator *iter)
  126. {
  127. struct trace_entry *entry = iter->ent;
  128. struct trace_seq *seq = &iter->seq;
  129. struct hw_branch_entry *it;
  130. trace_assign_type(it, entry);
  131. if (entry->type == TRACE_HW_BRANCHES) {
  132. if (trace_seq_printf(seq, "%4d ", entry->cpu) &&
  133. trace_seq_printf(seq, "0x%016llx -> 0x%016llx ",
  134. it->from, it->to) &&
  135. (!it->from ||
  136. seq_print_ip_sym(seq, it->from, /* sym_flags = */ 0)) &&
  137. trace_seq_printf(seq, "\n"))
  138. return TRACE_TYPE_HANDLED;
  139. return TRACE_TYPE_PARTIAL_LINE;;
  140. }
  141. return TRACE_TYPE_UNHANDLED;
  142. }
  143. void trace_hw_branch(u64 from, u64 to)
  144. {
  145. struct trace_array *tr = hw_branch_trace;
  146. struct ring_buffer_event *event;
  147. struct hw_branch_entry *entry;
  148. unsigned long irq1, irq2;
  149. int cpu;
  150. if (unlikely(!tr))
  151. return;
  152. if (unlikely(!trace_hw_branches_enabled))
  153. return;
  154. local_irq_save(irq1);
  155. cpu = raw_smp_processor_id();
  156. if (atomic_inc_return(&tr->data[cpu]->disabled) != 1)
  157. goto out;
  158. event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), &irq2);
  159. if (!event)
  160. goto out;
  161. entry = ring_buffer_event_data(event);
  162. tracing_generic_entry_update(&entry->ent, 0, from);
  163. entry->ent.type = TRACE_HW_BRANCHES;
  164. entry->ent.cpu = cpu;
  165. entry->from = from;
  166. entry->to = to;
  167. ring_buffer_unlock_commit(tr->buffer, event, irq2);
  168. out:
  169. atomic_dec(&tr->data[cpu]->disabled);
  170. local_irq_restore(irq1);
  171. }
  172. static void trace_bts_at(const struct bts_trace *trace, void *at)
  173. {
  174. struct bts_struct bts;
  175. int err = 0;
  176. WARN_ON_ONCE(!trace->read);
  177. if (!trace->read)
  178. return;
  179. err = trace->read(this_tracer, at, &bts);
  180. if (err < 0)
  181. return;
  182. switch (bts.qualifier) {
  183. case BTS_BRANCH:
  184. trace_hw_branch(bts.variant.lbr.from, bts.variant.lbr.to);
  185. break;
  186. }
  187. }
  188. /*
  189. * Collect the trace on the current cpu and write it into the ftrace buffer.
  190. *
  191. * pre: bts_tracer_mutex must be locked
  192. */
  193. static void trace_bts_cpu(void *arg)
  194. {
  195. struct trace_array *tr = (struct trace_array *) arg;
  196. const struct bts_trace *trace;
  197. unsigned char *at;
  198. if (unlikely(!tr))
  199. return;
  200. if (unlikely(atomic_read(&tr->data[raw_smp_processor_id()]->disabled)))
  201. return;
  202. if (unlikely(!this_tracer))
  203. return;
  204. ds_suspend_bts(this_tracer);
  205. trace = ds_read_bts(this_tracer);
  206. if (!trace)
  207. goto out;
  208. for (at = trace->ds.top; (void *)at < trace->ds.end;
  209. at += trace->ds.size)
  210. trace_bts_at(trace, at);
  211. for (at = trace->ds.begin; (void *)at < trace->ds.top;
  212. at += trace->ds.size)
  213. trace_bts_at(trace, at);
  214. out:
  215. ds_resume_bts(this_tracer);
  216. }
  217. static void trace_bts_prepare(struct trace_iterator *iter)
  218. {
  219. mutex_lock(&bts_tracer_mutex);
  220. on_each_cpu(trace_bts_cpu, iter->tr, 1);
  221. mutex_unlock(&bts_tracer_mutex);
  222. }
  223. void trace_hw_branch_oops(void)
  224. {
  225. mutex_lock(&bts_tracer_mutex);
  226. trace_bts_cpu(hw_branch_trace);
  227. mutex_unlock(&bts_tracer_mutex);
  228. }
  229. struct tracer bts_tracer __read_mostly =
  230. {
  231. .name = "hw-branch-tracer",
  232. .init = bts_trace_init,
  233. .reset = bts_trace_reset,
  234. .print_header = bts_trace_print_header,
  235. .print_line = bts_trace_print_line,
  236. .start = bts_trace_start,
  237. .stop = bts_trace_stop,
  238. .open = trace_bts_prepare
  239. };
  240. __init static int init_bts_trace(void)
  241. {
  242. return register_tracer(&bts_tracer);
  243. }
  244. device_initcall(init_bts_trace);