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> |
| 9 | #include <linux/debugfs.h> |
| 10 | #include <linux/uaccess.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/ftrace.h> |
| 13 | #include <linux/hash.h> |
| 14 | #include <linux/fs.h> |
| 15 | #include <asm/local.h> |
| 16 | #include "trace.h" |
| 17 | |
Steven Rostedt | 2ed84ee | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 18 | #ifdef CONFIG_BRANCH_TRACER |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 19 | |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 20 | static int branch_tracing_enabled __read_mostly; |
| 21 | static DEFINE_MUTEX(branch_tracing_mutex); |
| 22 | static struct trace_array *branch_tracer; |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 23 | |
| 24 | static void |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 25 | probe_likely_condition(struct ftrace_branch_data *f, int val, int expect) |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 26 | { |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 27 | struct trace_array *tr = branch_tracer; |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 28 | struct ring_buffer_event *event; |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 29 | struct trace_branch *entry; |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 30 | unsigned long flags, irq_flags; |
| 31 | int cpu, pc; |
| 32 | const char *p; |
| 33 | |
| 34 | /* |
| 35 | * I would love to save just the ftrace_likely_data pointer, but |
| 36 | * this code can also be used by modules. Ugly things can happen |
| 37 | * if the module is unloaded, and then we go and read the |
| 38 | * pointer. This is slower, but much safer. |
| 39 | */ |
| 40 | |
| 41 | if (unlikely(!tr)) |
| 42 | return; |
| 43 | |
| 44 | local_irq_save(flags); |
| 45 | cpu = raw_smp_processor_id(); |
| 46 | if (atomic_inc_return(&tr->data[cpu]->disabled) != 1) |
| 47 | goto out; |
| 48 | |
| 49 | event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), |
| 50 | &irq_flags); |
| 51 | if (!event) |
| 52 | goto out; |
| 53 | |
| 54 | pc = preempt_count(); |
| 55 | entry = ring_buffer_event_data(event); |
| 56 | tracing_generic_entry_update(&entry->ent, flags, pc); |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 57 | entry->ent.type = TRACE_BRANCH; |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 58 | |
| 59 | /* Strip off the path, only save the file */ |
| 60 | p = f->file + strlen(f->file); |
| 61 | while (p >= f->file && *p != '/') |
| 62 | p--; |
| 63 | p++; |
| 64 | |
| 65 | strncpy(entry->func, f->func, TRACE_FUNC_SIZE); |
| 66 | strncpy(entry->file, p, TRACE_FILE_SIZE); |
| 67 | entry->func[TRACE_FUNC_SIZE] = 0; |
| 68 | entry->file[TRACE_FILE_SIZE] = 0; |
| 69 | entry->line = f->line; |
| 70 | entry->correct = val == expect; |
| 71 | |
| 72 | ring_buffer_unlock_commit(tr->buffer, event, irq_flags); |
| 73 | |
| 74 | out: |
| 75 | atomic_dec(&tr->data[cpu]->disabled); |
| 76 | local_irq_restore(flags); |
| 77 | } |
| 78 | |
| 79 | static inline |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 80 | 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] | 81 | { |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 82 | if (!branch_tracing_enabled) |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 83 | return; |
| 84 | |
| 85 | probe_likely_condition(f, val, expect); |
| 86 | } |
| 87 | |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 88 | int enable_branch_tracing(struct trace_array *tr) |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 89 | { |
| 90 | int ret = 0; |
| 91 | |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 92 | mutex_lock(&branch_tracing_mutex); |
| 93 | branch_tracer = tr; |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 94 | /* |
| 95 | * Must be seen before enabling. The reader is a condition |
| 96 | * where we do not need a matching rmb() |
| 97 | */ |
| 98 | smp_wmb(); |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 99 | branch_tracing_enabled++; |
| 100 | mutex_unlock(&branch_tracing_mutex); |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 101 | |
| 102 | return ret; |
| 103 | } |
| 104 | |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 105 | void disable_branch_tracing(void) |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 106 | { |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 107 | mutex_lock(&branch_tracing_mutex); |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 108 | |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 109 | if (!branch_tracing_enabled) |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 110 | goto out_unlock; |
| 111 | |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 112 | branch_tracing_enabled--; |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 113 | |
| 114 | out_unlock: |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 115 | mutex_unlock(&branch_tracing_mutex); |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 116 | } |
| 117 | #else |
| 118 | static inline |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 119 | 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] | 120 | { |
| 121 | } |
Steven Rostedt | 2ed84ee | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 122 | #endif /* CONFIG_BRANCH_TRACER */ |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 123 | |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 124 | 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] | 125 | { |
Steven Rostedt | 52f232c | 2008-11-12 00:14:40 -0500 | [diff] [blame] | 126 | /* |
| 127 | * I would love to have a trace point here instead, but the |
| 128 | * trace point code is so inundated with unlikely and likely |
| 129 | * conditions that the recursive nightmare that exists is too |
| 130 | * much to try to get working. At least for now. |
| 131 | */ |
| 132 | trace_likely_condition(f, val, expect); |
| 133 | |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 134 | /* FIXME: Make this atomic! */ |
| 135 | if (val == expect) |
| 136 | f->correct++; |
| 137 | else |
| 138 | f->incorrect++; |
| 139 | } |
| 140 | EXPORT_SYMBOL(ftrace_likely_update); |
| 141 | |
| 142 | struct ftrace_pointer { |
| 143 | void *start; |
| 144 | void *stop; |
| 145 | }; |
| 146 | |
| 147 | static void * |
| 148 | t_next(struct seq_file *m, void *v, loff_t *pos) |
| 149 | { |
| 150 | struct ftrace_pointer *f = m->private; |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 151 | struct ftrace_branch_data *p = v; |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 152 | |
| 153 | (*pos)++; |
| 154 | |
| 155 | if (v == (void *)1) |
| 156 | return f->start; |
| 157 | |
| 158 | ++p; |
| 159 | |
| 160 | if ((void *)p >= (void *)f->stop) |
| 161 | return NULL; |
| 162 | |
| 163 | return p; |
| 164 | } |
| 165 | |
| 166 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 167 | { |
| 168 | void *t = (void *)1; |
| 169 | loff_t l = 0; |
| 170 | |
| 171 | for (; t && l < *pos; t = t_next(m, t, &l)) |
| 172 | ; |
| 173 | |
| 174 | return t; |
| 175 | } |
| 176 | |
| 177 | static void t_stop(struct seq_file *m, void *p) |
| 178 | { |
| 179 | } |
| 180 | |
| 181 | static int t_show(struct seq_file *m, void *v) |
| 182 | { |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 183 | struct ftrace_branch_data *p = v; |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 184 | const char *f; |
| 185 | unsigned long percent; |
| 186 | |
| 187 | if (v == (void *)1) { |
| 188 | seq_printf(m, " correct incorrect %% " |
| 189 | " Function " |
| 190 | " File Line\n" |
| 191 | " ------- --------- - " |
| 192 | " -------- " |
| 193 | " ---- ----\n"); |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | /* Only print the file, not the path */ |
| 198 | f = p->file + strlen(p->file); |
| 199 | while (f >= p->file && *f != '/') |
| 200 | f--; |
| 201 | f++; |
| 202 | |
| 203 | if (p->correct) { |
| 204 | percent = p->incorrect * 100; |
| 205 | percent /= p->correct + p->incorrect; |
| 206 | } else |
| 207 | percent = p->incorrect ? 100 : 0; |
| 208 | |
| 209 | seq_printf(m, "%8lu %8lu %3lu ", p->correct, p->incorrect, percent); |
| 210 | seq_printf(m, "%-30.30s %-20.20s %d\n", p->func, f, p->line); |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | static struct seq_operations tracing_likely_seq_ops = { |
| 215 | .start = t_start, |
| 216 | .next = t_next, |
| 217 | .stop = t_stop, |
| 218 | .show = t_show, |
| 219 | }; |
| 220 | |
| 221 | static int tracing_likely_open(struct inode *inode, struct file *file) |
| 222 | { |
| 223 | int ret; |
| 224 | |
| 225 | ret = seq_open(file, &tracing_likely_seq_ops); |
| 226 | if (!ret) { |
| 227 | struct seq_file *m = file->private_data; |
| 228 | m->private = (void *)inode->i_private; |
| 229 | } |
| 230 | |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | static struct file_operations tracing_likely_fops = { |
| 235 | .open = tracing_likely_open, |
| 236 | .read = seq_read, |
| 237 | .llseek = seq_lseek, |
| 238 | }; |
| 239 | |
| 240 | extern unsigned long __start_likely_profile[]; |
| 241 | extern unsigned long __stop_likely_profile[]; |
| 242 | extern unsigned long __start_unlikely_profile[]; |
| 243 | extern unsigned long __stop_unlikely_profile[]; |
| 244 | |
| 245 | static struct ftrace_pointer ftrace_likely_pos = { |
| 246 | .start = __start_likely_profile, |
| 247 | .stop = __stop_likely_profile, |
| 248 | }; |
| 249 | |
| 250 | static struct ftrace_pointer ftrace_unlikely_pos = { |
| 251 | .start = __start_unlikely_profile, |
| 252 | .stop = __stop_unlikely_profile, |
| 253 | }; |
| 254 | |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 255 | static __init int ftrace_branch_init(void) |
Steven Rostedt | 1f0d69a | 2008-11-12 00:14:39 -0500 | [diff] [blame] | 256 | { |
| 257 | struct dentry *d_tracer; |
| 258 | struct dentry *entry; |
| 259 | |
| 260 | d_tracer = tracing_init_dentry(); |
| 261 | |
| 262 | entry = debugfs_create_file("profile_likely", 0444, d_tracer, |
| 263 | &ftrace_likely_pos, |
| 264 | &tracing_likely_fops); |
| 265 | if (!entry) |
| 266 | pr_warning("Could not create debugfs 'profile_likely' entry\n"); |
| 267 | |
| 268 | entry = debugfs_create_file("profile_unlikely", 0444, d_tracer, |
| 269 | &ftrace_unlikely_pos, |
| 270 | &tracing_likely_fops); |
| 271 | if (!entry) |
| 272 | pr_warning("Could not create debugfs" |
| 273 | " 'profile_unlikely' entry\n"); |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame^] | 278 | device_initcall(ftrace_branch_init); |