trace_hw_branches.c 7.7 KB

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