Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 1 | /* |
| 2 | * unlikely profiler |
| 3 | * |
| 4 | * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com> |
| 5 | */ |
| 6 | #include <linux/kallsyms.h> |
| 7 | #include <linux/seq_file.h> |
| 8 | #include <linux/spinlock.h> |
Frederic Weisbecker | 65c6dc6 | 2008-11-29 04:12:46 +0100 | [diff] [blame] | 9 | #include <linux/irqflags.h> |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 10 | #include <linux/debugfs.h> |
| 11 | #include <linux/uaccess.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/ftrace.h> |
| 14 | #include <linux/hash.h> |
| 15 | #include <linux/fs.h> |
| 16 | #include <asm/local.h> |
| 17 | #include "trace.h" |
| 18 | |
Steven Rostedt | 2ed84ee | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 19 | #ifdef CONFIG_BRANCH_TRACER |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 20 | |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 21 | static int branch_tracing_enabled __read_mostly; |
| 22 | static DEFINE_MUTEX(branch_tracing_mutex); |
| 23 | static struct trace_array *branch_tracer; |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 24 | |
| 25 | static void |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 26 | probe_likely_condition(struct ftrace_branch_data *f, int val, int expect) |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 27 | { |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 28 | struct trace_array *tr = branch_tracer; |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 29 | struct ring_buffer_event *event; |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 30 | struct trace_branch *entry; |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 31 | unsigned long flags, irq_flags; |
| 32 | int cpu, pc; |
| 33 | const char *p; |
| 34 | |
| 35 | /* |
| 36 | * I would love to save just the ftrace_likely_data pointer, but |
| 37 | * this code can also be used by modules. Ugly things can happen |
| 38 | * if the module is unloaded, and then we go and read the |
| 39 | * pointer. This is slower, but much safer. |
| 40 | */ |
| 41 | |
| 42 | if (unlikely(!tr)) |
| 43 | return; |
| 44 | |
Steven Rostedt | a5e2588 | 2008-12-02 15:34:05 -0500 | [diff] [blame] | 45 | local_irq_save(flags); |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 46 | cpu = raw_smp_processor_id(); |
| 47 | if (atomic_inc_return(&tr->data[cpu]->disabled) != 1) |
| 48 | goto out; |
| 49 | |
| 50 | event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), |
| 51 | &irq_flags); |
| 52 | if (!event) |
| 53 | goto out; |
| 54 | |
| 55 | pc = preempt_count(); |
| 56 | entry = ring_buffer_event_data(event); |
| 57 | tracing_generic_entry_update(&entry->ent, flags, pc); |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 58 | entry->ent.type = TRACE_BRANCH; |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 59 | |
| 60 | /* Strip off the path, only save the file */ |
| 61 | p = f->file + strlen(f->file); |
| 62 | while (p >= f->file && *p != '/') |
| 63 | p--; |
| 64 | p++; |
| 65 | |
| 66 | strncpy(entry->func, f->func, TRACE_FUNC_SIZE); |
| 67 | strncpy(entry->file, p, TRACE_FILE_SIZE); |
| 68 | entry->func[TRACE_FUNC_SIZE] = 0; |
| 69 | entry->file[TRACE_FILE_SIZE] = 0; |
| 70 | entry->line = f->line; |
| 71 | entry->correct = val == expect; |
| 72 | |
| 73 | ring_buffer_unlock_commit(tr->buffer, event, irq_flags); |
| 74 | |
| 75 | out: |
| 76 | atomic_dec(&tr->data[cpu]->disabled); |
Steven Rostedt | a5e2588 | 2008-12-02 15:34:05 -0500 | [diff] [blame] | 77 | local_irq_restore(flags); |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static inline |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 81 | void trace_likely_condition(struct ftrace_branch_data *f, int val, int expect) |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 82 | { |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 83 | if (!branch_tracing_enabled) |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 84 | return; |
| 85 | |
| 86 | probe_likely_condition(f, val, expect); |
| 87 | } |
| 88 | |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 89 | int enable_branch_tracing(struct trace_array *tr) |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 90 | { |
| 91 | int ret = 0; |
| 92 | |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 93 | mutex_lock(&branch_tracing_mutex); |
| 94 | branch_tracer = tr; |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 95 | /* |
| 96 | * Must be seen before enabling. The reader is a condition |
| 97 | * where we do not need a matching rmb() |
| 98 | */ |
| 99 | smp_wmb(); |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 100 | branch_tracing_enabled++; |
| 101 | mutex_unlock(&branch_tracing_mutex); |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 102 | |
| 103 | return ret; |
| 104 | } |
| 105 | |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 106 | void disable_branch_tracing(void) |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 107 | { |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 108 | mutex_lock(&branch_tracing_mutex); |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 109 | |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 110 | if (!branch_tracing_enabled) |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 111 | goto out_unlock; |
| 112 | |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 113 | branch_tracing_enabled--; |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 114 | |
| 115 | out_unlock: |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 116 | mutex_unlock(&branch_tracing_mutex); |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 117 | } |
Steven Rostedt | 80e5ea4 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 118 | |
| 119 | static void start_branch_trace(struct trace_array *tr) |
| 120 | { |
| 121 | enable_branch_tracing(tr); |
| 122 | } |
| 123 | |
| 124 | static void stop_branch_trace(struct trace_array *tr) |
| 125 | { |
| 126 | disable_branch_tracing(); |
| 127 | } |
| 128 | |
Frederic Weisbecker | 1c80025 | 2008-11-16 05:57:26 +0100 | [diff] [blame] | 129 | static int branch_trace_init(struct trace_array *tr) |
Steven Rostedt | 80e5ea4 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 130 | { |
| 131 | int cpu; |
| 132 | |
| 133 | for_each_online_cpu(cpu) |
| 134 | tracing_reset(tr, cpu); |
| 135 | |
| 136 | start_branch_trace(tr); |
Frederic Weisbecker | 1c80025 | 2008-11-16 05:57:26 +0100 | [diff] [blame] | 137 | return 0; |
Steven Rostedt | 80e5ea4 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static void branch_trace_reset(struct trace_array *tr) |
| 141 | { |
| 142 | stop_branch_trace(tr); |
| 143 | } |
| 144 | |
| 145 | struct tracer branch_trace __read_mostly = |
| 146 | { |
| 147 | .name = "branch", |
| 148 | .init = branch_trace_init, |
| 149 | .reset = branch_trace_reset, |
| 150 | #ifdef CONFIG_FTRACE_SELFTEST |
| 151 | .selftest = trace_selftest_startup_branch, |
| 152 | #endif |
| 153 | }; |
| 154 | |
| 155 | __init static int init_branch_trace(void) |
| 156 | { |
| 157 | return register_tracer(&branch_trace); |
| 158 | } |
| 159 | |
| 160 | device_initcall(init_branch_trace); |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 161 | #else |
| 162 | static inline |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 163 | void trace_likely_condition(struct ftrace_branch_data *f, int val, int expect) |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 164 | { |
| 165 | } |
Steven Rostedt | 2ed84ee | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 166 | #endif /* CONFIG_BRANCH_TRACER */ |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 167 | |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 168 | void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect) |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 169 | { |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 170 | /* |
| 171 | * I would love to have a trace point here instead, but the |
| 172 | * trace point code is so inundated with unlikely and likely |
| 173 | * conditions that the recursive nightmare that exists is too |
| 174 | * much to try to get working. At least for now. |
| 175 | */ |
| 176 | trace_likely_condition(f, val, expect); |
| 177 | |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 178 | /* FIXME: Make this atomic! */ |
| 179 | if (val == expect) |
| 180 | f->correct++; |
| 181 | else |
| 182 | f->incorrect++; |
| 183 | } |
| 184 | EXPORT_SYMBOL(ftrace_likely_update); |
| 185 | |
| 186 | struct ftrace_pointer { |
| 187 | void *start; |
| 188 | void *stop; |
Steven Rostedt | 2bcd521 | 2008-11-21 01:30:54 -0500 | [diff] [blame] | 189 | int hit; |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | static void * |
| 193 | t_next(struct seq_file *m, void *v, loff_t *pos) |
| 194 | { |
Steven Rostedt | 0429149 | 2008-11-21 14:44:57 -0500 | [diff] [blame] | 195 | const struct ftrace_pointer *f = m->private; |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 196 | struct ftrace_branch_data *p = v; |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 197 | |
| 198 | (*pos)++; |
| 199 | |
| 200 | if (v == (void *)1) |
| 201 | return f->start; |
| 202 | |
| 203 | ++p; |
| 204 | |
| 205 | if ((void *)p >= (void *)f->stop) |
| 206 | return NULL; |
| 207 | |
| 208 | return p; |
| 209 | } |
| 210 | |
| 211 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 212 | { |
| 213 | void *t = (void *)1; |
| 214 | loff_t l = 0; |
| 215 | |
| 216 | for (; t && l < *pos; t = t_next(m, t, &l)) |
| 217 | ; |
| 218 | |
| 219 | return t; |
| 220 | } |
| 221 | |
| 222 | static void t_stop(struct seq_file *m, void *p) |
| 223 | { |
| 224 | } |
| 225 | |
| 226 | static int t_show(struct seq_file *m, void *v) |
| 227 | { |
Steven Rostedt | 0429149 | 2008-11-21 14:44:57 -0500 | [diff] [blame] | 228 | const struct ftrace_pointer *fp = m->private; |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 229 | struct ftrace_branch_data *p = v; |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 230 | const char *f; |
Steven Rostedt | bac28bf | 2008-11-21 01:51:53 -0500 | [diff] [blame] | 231 | long percent; |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 232 | |
| 233 | if (v == (void *)1) { |
Steven Rostedt | 2bcd521 | 2008-11-21 01:30:54 -0500 | [diff] [blame] | 234 | if (fp->hit) |
| 235 | seq_printf(m, " miss hit %% "); |
| 236 | else |
| 237 | seq_printf(m, " correct incorrect %% "); |
| 238 | seq_printf(m, " Function " |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 239 | " File Line\n" |
| 240 | " ------- --------- - " |
| 241 | " -------- " |
| 242 | " ---- ----\n"); |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | /* Only print the file, not the path */ |
| 247 | f = p->file + strlen(p->file); |
| 248 | while (f >= p->file && *f != '/') |
| 249 | f--; |
| 250 | f++; |
| 251 | |
Steven Rostedt | 2bcd521 | 2008-11-21 01:30:54 -0500 | [diff] [blame] | 252 | /* |
| 253 | * The miss is overlayed on correct, and hit on incorrect. |
| 254 | */ |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 255 | if (p->correct) { |
| 256 | percent = p->incorrect * 100; |
| 257 | percent /= p->correct + p->incorrect; |
| 258 | } else |
Steven Rostedt | bac28bf | 2008-11-21 01:51:53 -0500 | [diff] [blame] | 259 | percent = p->incorrect ? 100 : -1; |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 260 | |
Steven Rostedt | bac28bf | 2008-11-21 01:51:53 -0500 | [diff] [blame] | 261 | seq_printf(m, "%8lu %8lu ", p->correct, p->incorrect); |
| 262 | if (percent < 0) |
| 263 | seq_printf(m, " X "); |
| 264 | else |
| 265 | seq_printf(m, "%3ld ", percent); |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 266 | seq_printf(m, "%-30.30s %-20.20s %d\n", p->func, f, p->line); |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static struct seq_operations tracing_likely_seq_ops = { |
| 271 | .start = t_start, |
| 272 | .next = t_next, |
| 273 | .stop = t_stop, |
| 274 | .show = t_show, |
| 275 | }; |
| 276 | |
Steven Rostedt | 45b7974 | 2008-11-21 00:40:40 -0500 | [diff] [blame] | 277 | static int tracing_branch_open(struct inode *inode, struct file *file) |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 278 | { |
| 279 | int ret; |
| 280 | |
| 281 | ret = seq_open(file, &tracing_likely_seq_ops); |
| 282 | if (!ret) { |
| 283 | struct seq_file *m = file->private_data; |
| 284 | m->private = (void *)inode->i_private; |
| 285 | } |
| 286 | |
| 287 | return ret; |
| 288 | } |
| 289 | |
Steven Rostedt | 45b7974 | 2008-11-21 00:40:40 -0500 | [diff] [blame] | 290 | static const struct file_operations tracing_branch_fops = { |
| 291 | .open = tracing_branch_open, |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 292 | .read = seq_read, |
| 293 | .llseek = seq_lseek, |
| 294 | }; |
| 295 | |
Steven Rostedt | 2bcd521 | 2008-11-21 01:30:54 -0500 | [diff] [blame] | 296 | #ifdef CONFIG_PROFILE_ALL_BRANCHES |
| 297 | extern unsigned long __start_branch_profile[]; |
| 298 | extern unsigned long __stop_branch_profile[]; |
| 299 | |
Steven Rostedt | 0429149 | 2008-11-21 14:44:57 -0500 | [diff] [blame] | 300 | static const struct ftrace_pointer ftrace_branch_pos = { |
Steven Rostedt | 2bcd521 | 2008-11-21 01:30:54 -0500 | [diff] [blame] | 301 | .start = __start_branch_profile, |
| 302 | .stop = __stop_branch_profile, |
| 303 | .hit = 1, |
| 304 | }; |
| 305 | |
| 306 | #endif /* CONFIG_PROFILE_ALL_BRANCHES */ |
| 307 | |
Steven Rostedt | 45b7974 | 2008-11-21 00:40:40 -0500 | [diff] [blame] | 308 | extern unsigned long __start_annotated_branch_profile[]; |
| 309 | extern unsigned long __stop_annotated_branch_profile[]; |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 310 | |
Steven Rostedt | 45b7974 | 2008-11-21 00:40:40 -0500 | [diff] [blame] | 311 | static const struct ftrace_pointer ftrace_annotated_branch_pos = { |
| 312 | .start = __start_annotated_branch_profile, |
| 313 | .stop = __stop_annotated_branch_profile, |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 314 | }; |
| 315 | |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 316 | static __init int ftrace_branch_init(void) |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 317 | { |
| 318 | struct dentry *d_tracer; |
| 319 | struct dentry *entry; |
| 320 | |
| 321 | d_tracer = tracing_init_dentry(); |
| 322 | |
Steven Rostedt | 45b7974 | 2008-11-21 00:40:40 -0500 | [diff] [blame] | 323 | entry = debugfs_create_file("profile_annotated_branch", 0444, d_tracer, |
Steven Rostedt | 0429149 | 2008-11-21 14:44:57 -0500 | [diff] [blame] | 324 | (void *)&ftrace_annotated_branch_pos, |
Steven Rostedt | 45b7974 | 2008-11-21 00:40:40 -0500 | [diff] [blame] | 325 | &tracing_branch_fops); |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 326 | if (!entry) |
Steven Rostedt | 45b7974 | 2008-11-21 00:40:40 -0500 | [diff] [blame] | 327 | pr_warning("Could not create debugfs " |
| 328 | "'profile_annotatet_branch' entry\n"); |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 329 | |
Steven Rostedt | 2bcd521 | 2008-11-21 01:30:54 -0500 | [diff] [blame] | 330 | #ifdef CONFIG_PROFILE_ALL_BRANCHES |
| 331 | entry = debugfs_create_file("profile_branch", 0444, d_tracer, |
Steven Rostedt | 0429149 | 2008-11-21 14:44:57 -0500 | [diff] [blame] | 332 | (void *)&ftrace_branch_pos, |
Steven Rostedt | 2bcd521 | 2008-11-21 01:30:54 -0500 | [diff] [blame] | 333 | &tracing_branch_fops); |
| 334 | if (!entry) |
| 335 | pr_warning("Could not create debugfs" |
| 336 | " 'profile_branch' entry\n"); |
| 337 | #endif |
| 338 | |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 342 | device_initcall(ftrace_branch_init); |