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