Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @file cpu_buffer.c |
| 3 | * |
Robert Richter | 2cc28b9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 4 | * @remark Copyright 2002-2009 OProfile authors |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * @remark Read the file COPYING |
| 6 | * |
| 7 | * @author John Levon <levon@movementarian.org> |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 8 | * @author Barry Kasindorf <barry.kasindorf@amd.com> |
Robert Richter | 2cc28b9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 9 | * @author Robert Richter <robert.richter@amd.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
| 11 | * Each CPU has a local buffer that stores PC value/event |
| 12 | * pairs. We also log context switches when we notice them. |
| 13 | * Eventually each CPU's buffer is processed into the global |
| 14 | * event buffer by sync_buffer(). |
| 15 | * |
| 16 | * We use a local buffer for two reasons: an NMI or similar |
| 17 | * interrupt cannot synchronise, and high sampling rates |
| 18 | * would lead to catastrophic global synchronisation if |
| 19 | * a global buffer was used. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/sched.h> |
| 23 | #include <linux/oprofile.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/errno.h> |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 25 | |
Ingo Molnar | 4cf421e | 2017-02-03 10:03:42 +0100 | [diff] [blame] | 26 | #include <asm/ptrace.h> |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "event_buffer.h" |
| 29 | #include "cpu_buffer.h" |
| 30 | #include "buffer_sync.h" |
| 31 | #include "oprof.h" |
| 32 | |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 33 | #define OP_BUFFER_FLAGS 0 |
| 34 | |
Andi Kleen | cb6e943 | 2010-04-01 03:17:25 +0200 | [diff] [blame] | 35 | static struct ring_buffer *op_ring_buffer; |
Tejun Heo | b3e9f67 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 36 | DEFINE_PER_CPU(struct oprofile_cpu_buffer, op_cpu_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 38 | static void wq_sync_buffer(struct work_struct *work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | #define DEFAULT_TIMER_EXPIRE (HZ / 10) |
| 41 | static int work_enabled; |
| 42 | |
Carl Love | a5598ca | 2008-10-14 23:37:01 +0000 | [diff] [blame] | 43 | unsigned long oprofile_get_cpu_buffer_size(void) |
| 44 | { |
Robert Richter | bd2172f | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 45 | return oprofile_cpu_buffer_size; |
Carl Love | a5598ca | 2008-10-14 23:37:01 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void oprofile_cpu_buffer_inc_smpl_lost(void) |
| 49 | { |
Christoph Lameter | 879d927 | 2014-08-17 12:30:31 -0500 | [diff] [blame] | 50 | struct oprofile_cpu_buffer *cpu_buf = this_cpu_ptr(&op_cpu_buffer); |
Carl Love | a5598ca | 2008-10-14 23:37:01 +0000 | [diff] [blame] | 51 | |
| 52 | cpu_buf->sample_lost_overflow++; |
| 53 | } |
| 54 | |
Robert Richter | 3001577 | 2008-12-23 01:35:12 +0100 | [diff] [blame] | 55 | void free_cpu_buffers(void) |
| 56 | { |
Andi Kleen | cb6e943 | 2010-04-01 03:17:25 +0200 | [diff] [blame] | 57 | if (op_ring_buffer) |
| 58 | ring_buffer_free(op_ring_buffer); |
| 59 | op_ring_buffer = NULL; |
Robert Richter | 3001577 | 2008-12-23 01:35:12 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Robert Richter | 54f2c84 | 2009-05-07 17:28:59 +0200 | [diff] [blame] | 62 | #define RB_EVENT_HDR_SIZE 4 |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | int alloc_cpu_buffers(void) |
| 65 | { |
| 66 | int i; |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 67 | |
Robert Richter | bd2172f | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 68 | unsigned long buffer_size = oprofile_cpu_buffer_size; |
Robert Richter | 54f2c84 | 2009-05-07 17:28:59 +0200 | [diff] [blame] | 69 | unsigned long byte_size = buffer_size * (sizeof(struct op_sample) + |
| 70 | RB_EVENT_HDR_SIZE); |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 71 | |
Andi Kleen | cb6e943 | 2010-04-01 03:17:25 +0200 | [diff] [blame] | 72 | op_ring_buffer = ring_buffer_alloc(byte_size, OP_BUFFER_FLAGS); |
| 73 | if (!op_ring_buffer) |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 74 | goto fail; |
| 75 | |
Chris J Arges | 4bd9b9d | 2008-10-15 11:03:39 -0500 | [diff] [blame] | 76 | for_each_possible_cpu(i) { |
Tejun Heo | b3e9f67 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 77 | struct oprofile_cpu_buffer *b = &per_cpu(op_cpu_buffer, i); |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 78 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | b->last_task = NULL; |
| 80 | b->last_is_kernel = -1; |
| 81 | b->tracing = 0; |
| 82 | b->buffer_size = buffer_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | b->sample_received = 0; |
| 84 | b->sample_lost_overflow = 0; |
Philippe Elie | df9d177 | 2007-11-14 16:58:48 -0800 | [diff] [blame] | 85 | b->backtrace_aborted = 0; |
| 86 | b->sample_invalid_eip = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | b->cpu = i; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 88 | INIT_DELAYED_WORK(&b->work, wq_sync_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | return 0; |
| 91 | |
| 92 | fail: |
| 93 | free_cpu_buffers(); |
| 94 | return -ENOMEM; |
| 95 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | void start_cpu_work(void) |
| 98 | { |
| 99 | int i; |
| 100 | |
| 101 | work_enabled = 1; |
| 102 | |
| 103 | for_each_online_cpu(i) { |
Tejun Heo | b3e9f67 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 104 | struct oprofile_cpu_buffer *b = &per_cpu(op_cpu_buffer, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | /* |
| 107 | * Spread the work by 1 jiffy per cpu so they dont all |
| 108 | * fire at once. |
| 109 | */ |
| 110 | schedule_delayed_work_on(i, &b->work, DEFAULT_TIMER_EXPIRE + i); |
| 111 | } |
| 112 | } |
| 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | void end_cpu_work(void) |
| 115 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | work_enabled = 0; |
Tejun Heo | 3d7851b | 2010-10-15 09:51:08 -0400 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void flush_cpu_work(void) |
| 120 | { |
| 121 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
| 123 | for_each_online_cpu(i) { |
Tejun Heo | b3e9f67 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 124 | struct oprofile_cpu_buffer *b = &per_cpu(op_cpu_buffer, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Tejun Heo | 3d7851b | 2010-10-15 09:51:08 -0400 | [diff] [blame] | 126 | /* these works are per-cpu, no need for flush_sync */ |
| 127 | flush_delayed_work(&b->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Robert Richter | 2cc28b9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 131 | /* |
| 132 | * This function prepares the cpu buffer to write a sample. |
| 133 | * |
| 134 | * Struct op_entry is used during operations on the ring buffer while |
| 135 | * struct op_sample contains the data that is stored in the ring |
| 136 | * buffer. Struct entry can be uninitialized. The function reserves a |
| 137 | * data array that is specified by size. Use |
| 138 | * op_cpu_buffer_write_commit() after preparing the sample. In case of |
| 139 | * errors a null pointer is returned, otherwise the pointer to the |
| 140 | * sample. |
| 141 | * |
| 142 | */ |
| 143 | struct op_sample |
| 144 | *op_cpu_buffer_write_reserve(struct op_entry *entry, unsigned long size) |
Robert Richter | 9966718 | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 145 | { |
Robert Richter | 2cc28b9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 146 | entry->event = ring_buffer_lock_reserve |
Andi Kleen | cb6e943 | 2010-04-01 03:17:25 +0200 | [diff] [blame] | 147 | (op_ring_buffer, sizeof(struct op_sample) + |
Ingo Molnar | 304cc6a | 2009-02-06 01:12:02 +0100 | [diff] [blame] | 148 | size * sizeof(entry->sample->data[0])); |
Andi Kleen | cb6e943 | 2010-04-01 03:17:25 +0200 | [diff] [blame] | 149 | if (!entry->event) |
Robert Richter | 2cc28b9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 150 | return NULL; |
Andi Kleen | cb6e943 | 2010-04-01 03:17:25 +0200 | [diff] [blame] | 151 | entry->sample = ring_buffer_event_data(entry->event); |
Robert Richter | 2cc28b9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 152 | entry->size = size; |
| 153 | entry->data = entry->sample->data; |
| 154 | |
| 155 | return entry->sample; |
Robert Richter | 9966718 | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | int op_cpu_buffer_write_commit(struct op_entry *entry) |
| 159 | { |
Andi Kleen | cb6e943 | 2010-04-01 03:17:25 +0200 | [diff] [blame] | 160 | return ring_buffer_unlock_commit(op_ring_buffer, entry->event); |
Robert Richter | 9966718 | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Robert Richter | 2d87b14 | 2008-12-30 04:10:46 +0100 | [diff] [blame] | 163 | struct op_sample *op_cpu_buffer_read_entry(struct op_entry *entry, int cpu) |
Robert Richter | 9966718 | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 164 | { |
| 165 | struct ring_buffer_event *e; |
Robert Richter | b971f06 | 2010-04-23 16:47:51 +0200 | [diff] [blame] | 166 | e = ring_buffer_consume(op_ring_buffer, cpu, NULL, NULL); |
Andi Kleen | cb6e943 | 2010-04-01 03:17:25 +0200 | [diff] [blame] | 167 | if (!e) |
Robert Richter | 9966718 | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 168 | return NULL; |
Robert Richter | 2d87b14 | 2008-12-30 04:10:46 +0100 | [diff] [blame] | 169 | |
Robert Richter | 2d87b14 | 2008-12-30 04:10:46 +0100 | [diff] [blame] | 170 | entry->event = e; |
| 171 | entry->sample = ring_buffer_event_data(e); |
| 172 | entry->size = (ring_buffer_event_length(e) - sizeof(struct op_sample)) |
| 173 | / sizeof(entry->sample->data[0]); |
| 174 | entry->data = entry->sample->data; |
| 175 | return entry->sample; |
Robert Richter | 9966718 | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | unsigned long op_cpu_buffer_entries(int cpu) |
| 179 | { |
Andi Kleen | cb6e943 | 2010-04-01 03:17:25 +0200 | [diff] [blame] | 180 | return ring_buffer_entries_cpu(op_ring_buffer, cpu); |
Robert Richter | 9966718 | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Robert Richter | ae735e9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 183 | static int |
| 184 | op_add_code(struct oprofile_cpu_buffer *cpu_buf, unsigned long backtrace, |
| 185 | int is_kernel, struct task_struct *task) |
| 186 | { |
| 187 | struct op_entry entry; |
| 188 | struct op_sample *sample; |
| 189 | unsigned long flags; |
| 190 | int size; |
| 191 | |
| 192 | flags = 0; |
| 193 | |
| 194 | if (backtrace) |
| 195 | flags |= TRACE_BEGIN; |
| 196 | |
| 197 | /* notice a switch from user->kernel or vice versa */ |
| 198 | is_kernel = !!is_kernel; |
| 199 | if (cpu_buf->last_is_kernel != is_kernel) { |
| 200 | cpu_buf->last_is_kernel = is_kernel; |
| 201 | flags |= KERNEL_CTX_SWITCH; |
| 202 | if (is_kernel) |
| 203 | flags |= IS_KERNEL; |
| 204 | } |
| 205 | |
| 206 | /* notice a task switch */ |
| 207 | if (cpu_buf->last_task != task) { |
| 208 | cpu_buf->last_task = task; |
| 209 | flags |= USER_CTX_SWITCH; |
| 210 | } |
| 211 | |
| 212 | if (!flags) |
| 213 | /* nothing to do */ |
| 214 | return 0; |
| 215 | |
| 216 | if (flags & USER_CTX_SWITCH) |
| 217 | size = 1; |
| 218 | else |
| 219 | size = 0; |
| 220 | |
| 221 | sample = op_cpu_buffer_write_reserve(&entry, size); |
| 222 | if (!sample) |
| 223 | return -ENOMEM; |
| 224 | |
| 225 | sample->eip = ESCAPE_CODE; |
| 226 | sample->event = flags; |
| 227 | |
| 228 | if (size) |
Robert Richter | d9928c2 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 229 | op_cpu_buffer_add_data(&entry, (unsigned long)task); |
Robert Richter | ae735e9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 230 | |
| 231 | op_cpu_buffer_write_commit(&entry); |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 236 | static inline int |
Robert Richter | d0e2338 | 2008-12-23 04:03:05 +0100 | [diff] [blame] | 237 | op_add_sample(struct oprofile_cpu_buffer *cpu_buf, |
| 238 | unsigned long pc, unsigned long event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 240 | struct op_entry entry; |
Robert Richter | 2cc28b9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 241 | struct op_sample *sample; |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 242 | |
Robert Richter | 2cc28b9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 243 | sample = op_cpu_buffer_write_reserve(&entry, 0); |
| 244 | if (!sample) |
| 245 | return -ENOMEM; |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 246 | |
Robert Richter | 2cc28b9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 247 | sample->eip = pc; |
| 248 | sample->event = event; |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 249 | |
Robert Richter | 3967e93 | 2008-12-30 05:10:58 +0100 | [diff] [blame] | 250 | return op_cpu_buffer_write_commit(&entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Robert Richter | ae735e9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 253 | /* |
| 254 | * This must be safe from any context. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | * |
| 256 | * is_kernel is needed because on some architectures you cannot |
| 257 | * tell if you are in kernel or user space simply by looking at |
| 258 | * pc. We tag this in the buffer by generating kernel enter/exit |
| 259 | * events whenever is_kernel changes |
| 260 | */ |
Robert Richter | ae735e9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 261 | static int |
| 262 | log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc, |
Heinz Graalfs | 54ebbe7 | 2011-01-21 10:06:54 +0000 | [diff] [blame] | 263 | unsigned long backtrace, int is_kernel, unsigned long event, |
| 264 | struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | { |
Heinz Graalfs | 54ebbe7 | 2011-01-21 10:06:54 +0000 | [diff] [blame] | 266 | struct task_struct *tsk = task ? task : current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | cpu_buf->sample_received++; |
| 268 | |
Philippe Elie | df9d177 | 2007-11-14 16:58:48 -0800 | [diff] [blame] | 269 | if (pc == ESCAPE_CODE) { |
| 270 | cpu_buf->sample_invalid_eip++; |
| 271 | return 0; |
| 272 | } |
| 273 | |
Heinz Graalfs | 54ebbe7 | 2011-01-21 10:06:54 +0000 | [diff] [blame] | 274 | if (op_add_code(cpu_buf, backtrace, is_kernel, tsk)) |
Robert Richter | ae735e9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 275 | goto fail; |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 276 | |
Robert Richter | d0e2338 | 2008-12-23 04:03:05 +0100 | [diff] [blame] | 277 | if (op_add_sample(cpu_buf, pc, event)) |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 278 | goto fail; |
| 279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | return 1; |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 281 | |
| 282 | fail: |
| 283 | cpu_buf->sample_lost_overflow++; |
| 284 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Robert Richter | 6352d92 | 2008-12-18 22:09:13 +0100 | [diff] [blame] | 287 | static inline void oprofile_begin_trace(struct oprofile_cpu_buffer *cpu_buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | cpu_buf->tracing = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Robert Richter | 6352d92 | 2008-12-18 22:09:13 +0100 | [diff] [blame] | 292 | static inline void oprofile_end_trace(struct oprofile_cpu_buffer *cpu_buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | { |
| 294 | cpu_buf->tracing = 0; |
| 295 | } |
| 296 | |
Robert Richter | d45d23b | 2008-12-16 12:00:10 +0100 | [diff] [blame] | 297 | static inline void |
| 298 | __oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs, |
Heinz Graalfs | 54ebbe7 | 2011-01-21 10:06:54 +0000 | [diff] [blame] | 299 | unsigned long event, int is_kernel, |
| 300 | struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | { |
Christoph Lameter | 879d927 | 2014-08-17 12:30:31 -0500 | [diff] [blame] | 302 | struct oprofile_cpu_buffer *cpu_buf = this_cpu_ptr(&op_cpu_buffer); |
Robert Richter | ae735e9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 303 | unsigned long backtrace = oprofile_backtrace_depth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Robert Richter | fd13f6c | 2008-10-19 21:00:09 +0200 | [diff] [blame] | 305 | /* |
| 306 | * if log_sample() fail we can't backtrace since we lost the |
| 307 | * source of this event |
| 308 | */ |
Heinz Graalfs | 54ebbe7 | 2011-01-21 10:06:54 +0000 | [diff] [blame] | 309 | if (!log_sample(cpu_buf, pc, backtrace, is_kernel, event, task)) |
Robert Richter | ae735e9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 310 | /* failed */ |
| 311 | return; |
Robert Richter | 6352d92 | 2008-12-18 22:09:13 +0100 | [diff] [blame] | 312 | |
Robert Richter | ae735e9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 313 | if (!backtrace) |
| 314 | return; |
| 315 | |
| 316 | oprofile_begin_trace(cpu_buf); |
| 317 | oprofile_ops.backtrace(regs, backtrace); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | oprofile_end_trace(cpu_buf); |
| 319 | } |
| 320 | |
Heinz Graalfs | 54ebbe7 | 2011-01-21 10:06:54 +0000 | [diff] [blame] | 321 | void oprofile_add_ext_hw_sample(unsigned long pc, struct pt_regs * const regs, |
| 322 | unsigned long event, int is_kernel, |
| 323 | struct task_struct *task) |
| 324 | { |
| 325 | __oprofile_add_ext_sample(pc, regs, event, is_kernel, task); |
| 326 | } |
| 327 | |
Robert Richter | d45d23b | 2008-12-16 12:00:10 +0100 | [diff] [blame] | 328 | void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs, |
| 329 | unsigned long event, int is_kernel) |
| 330 | { |
Heinz Graalfs | 54ebbe7 | 2011-01-21 10:06:54 +0000 | [diff] [blame] | 331 | __oprofile_add_ext_sample(pc, regs, event, is_kernel, NULL); |
Robert Richter | d45d23b | 2008-12-16 12:00:10 +0100 | [diff] [blame] | 332 | } |
| 333 | |
Brian Rogan | 2735771 | 2006-03-28 01:56:20 -0800 | [diff] [blame] | 334 | void oprofile_add_sample(struct pt_regs * const regs, unsigned long event) |
| 335 | { |
Phil Carmody | 9414e99 | 2010-04-28 12:09:16 -0500 | [diff] [blame] | 336 | int is_kernel; |
| 337 | unsigned long pc; |
| 338 | |
| 339 | if (likely(regs)) { |
| 340 | is_kernel = !user_mode(regs); |
| 341 | pc = profile_pc(regs); |
| 342 | } else { |
| 343 | is_kernel = 0; /* This value will not be used */ |
| 344 | pc = ESCAPE_CODE; /* as this causes an early return. */ |
| 345 | } |
Brian Rogan | 2735771 | 2006-03-28 01:56:20 -0800 | [diff] [blame] | 346 | |
Heinz Graalfs | 54ebbe7 | 2011-01-21 10:06:54 +0000 | [diff] [blame] | 347 | __oprofile_add_ext_sample(pc, regs, event, is_kernel, NULL); |
Brian Rogan | 2735771 | 2006-03-28 01:56:20 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Robert Richter | 1acda87 | 2009-01-05 10:35:31 +0100 | [diff] [blame] | 350 | /* |
| 351 | * Add samples with data to the ring buffer. |
| 352 | * |
Robert Richter | 14f0ca8 | 2009-01-07 21:50:22 +0100 | [diff] [blame] | 353 | * Use oprofile_add_data(&entry, val) to add data and |
| 354 | * oprofile_write_commit(&entry) to commit the sample. |
Robert Richter | 1acda87 | 2009-01-05 10:35:31 +0100 | [diff] [blame] | 355 | */ |
Robert Richter | 14f0ca8 | 2009-01-07 21:50:22 +0100 | [diff] [blame] | 356 | void |
| 357 | oprofile_write_reserve(struct op_entry *entry, struct pt_regs * const regs, |
Robert Richter | 1acda87 | 2009-01-05 10:35:31 +0100 | [diff] [blame] | 358 | unsigned long pc, int code, int size) |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 359 | { |
Robert Richter | 1acda87 | 2009-01-05 10:35:31 +0100 | [diff] [blame] | 360 | struct op_sample *sample; |
Robert Richter | e2fee27 | 2008-07-18 17:36:20 +0200 | [diff] [blame] | 361 | int is_kernel = !user_mode(regs); |
Christoph Lameter | 879d927 | 2014-08-17 12:30:31 -0500 | [diff] [blame] | 362 | struct oprofile_cpu_buffer *cpu_buf = this_cpu_ptr(&op_cpu_buffer); |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 363 | |
| 364 | cpu_buf->sample_received++; |
| 365 | |
Robert Richter | 1acda87 | 2009-01-05 10:35:31 +0100 | [diff] [blame] | 366 | /* no backtraces for samples with data */ |
| 367 | if (op_add_code(cpu_buf, 0, is_kernel, current)) |
| 368 | goto fail; |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 369 | |
Robert Richter | 1acda87 | 2009-01-05 10:35:31 +0100 | [diff] [blame] | 370 | sample = op_cpu_buffer_write_reserve(entry, size + 2); |
| 371 | if (!sample) |
| 372 | goto fail; |
| 373 | sample->eip = ESCAPE_CODE; |
| 374 | sample->event = 0; /* no flags */ |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 375 | |
Robert Richter | 1acda87 | 2009-01-05 10:35:31 +0100 | [diff] [blame] | 376 | op_cpu_buffer_add_data(entry, code); |
| 377 | op_cpu_buffer_add_data(entry, pc); |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 378 | |
Robert Richter | 1acda87 | 2009-01-05 10:35:31 +0100 | [diff] [blame] | 379 | return; |
| 380 | |
| 381 | fail: |
Robert Richter | fdb6a8f | 2009-01-17 17:13:27 +0100 | [diff] [blame] | 382 | entry->event = NULL; |
Robert Richter | 1acda87 | 2009-01-05 10:35:31 +0100 | [diff] [blame] | 383 | cpu_buf->sample_lost_overflow++; |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 384 | } |
| 385 | |
Robert Richter | 14f0ca8 | 2009-01-07 21:50:22 +0100 | [diff] [blame] | 386 | int oprofile_add_data(struct op_entry *entry, unsigned long val) |
| 387 | { |
Robert Richter | fdb6a8f | 2009-01-17 17:13:27 +0100 | [diff] [blame] | 388 | if (!entry->event) |
| 389 | return 0; |
Robert Richter | 14f0ca8 | 2009-01-07 21:50:22 +0100 | [diff] [blame] | 390 | return op_cpu_buffer_add_data(entry, val); |
| 391 | } |
| 392 | |
Robert Richter | 51563a0 | 2009-06-03 20:54:56 +0200 | [diff] [blame] | 393 | int oprofile_add_data64(struct op_entry *entry, u64 val) |
| 394 | { |
| 395 | if (!entry->event) |
| 396 | return 0; |
| 397 | if (op_cpu_buffer_get_size(entry) < 2) |
| 398 | /* |
| 399 | * the function returns 0 to indicate a too small |
| 400 | * buffer, even if there is some space left |
| 401 | */ |
| 402 | return 0; |
| 403 | if (!op_cpu_buffer_add_data(entry, (u32)val)) |
| 404 | return 0; |
| 405 | return op_cpu_buffer_add_data(entry, (u32)(val >> 32)); |
| 406 | } |
| 407 | |
Robert Richter | 14f0ca8 | 2009-01-07 21:50:22 +0100 | [diff] [blame] | 408 | int oprofile_write_commit(struct op_entry *entry) |
| 409 | { |
Robert Richter | fdb6a8f | 2009-01-17 17:13:27 +0100 | [diff] [blame] | 410 | if (!entry->event) |
| 411 | return -EINVAL; |
Robert Richter | 14f0ca8 | 2009-01-07 21:50:22 +0100 | [diff] [blame] | 412 | return op_cpu_buffer_write_commit(entry); |
| 413 | } |
| 414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event) |
| 416 | { |
Christoph Lameter | 879d927 | 2014-08-17 12:30:31 -0500 | [diff] [blame] | 417 | struct oprofile_cpu_buffer *cpu_buf = this_cpu_ptr(&op_cpu_buffer); |
Heinz Graalfs | 54ebbe7 | 2011-01-21 10:06:54 +0000 | [diff] [blame] | 418 | log_sample(cpu_buf, pc, 0, is_kernel, event, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | void oprofile_add_trace(unsigned long pc) |
| 422 | { |
Christoph Lameter | 879d927 | 2014-08-17 12:30:31 -0500 | [diff] [blame] | 423 | struct oprofile_cpu_buffer *cpu_buf = this_cpu_ptr(&op_cpu_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
| 425 | if (!cpu_buf->tracing) |
| 426 | return; |
| 427 | |
Robert Richter | fd13f6c | 2008-10-19 21:00:09 +0200 | [diff] [blame] | 428 | /* |
| 429 | * broken frame can give an eip with the same value as an |
| 430 | * escape code, abort the trace if we get it |
| 431 | */ |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 432 | if (pc == ESCAPE_CODE) |
| 433 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Robert Richter | d0e2338 | 2008-12-23 04:03:05 +0100 | [diff] [blame] | 435 | if (op_add_sample(cpu_buf, pc, 0)) |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 436 | goto fail; |
| 437 | |
| 438 | return; |
| 439 | fail: |
| 440 | cpu_buf->tracing = 0; |
| 441 | cpu_buf->backtrace_aborted++; |
| 442 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | /* |
| 446 | * This serves to avoid cpu buffer overflow, and makes sure |
| 447 | * the task mortuary progresses |
| 448 | * |
| 449 | * By using schedule_delayed_work_on and then schedule_delayed_work |
| 450 | * we guarantee this will stay on the correct cpu |
| 451 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 452 | static void wq_sync_buffer(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | { |
Robert Richter | 25ad291 | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 454 | struct oprofile_cpu_buffer *b = |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 455 | container_of(work, struct oprofile_cpu_buffer, work.work); |
Robert Richter | 61bccf1 | 2012-08-22 09:23:51 +0200 | [diff] [blame] | 456 | if (b->cpu != smp_processor_id() && !cpu_online(b->cpu)) { |
| 457 | cancel_delayed_work(&b->work); |
| 458 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } |
| 460 | sync_buffer(b->cpu); |
| 461 | |
| 462 | /* don't re-add the work if we're shutting down */ |
| 463 | if (work_enabled) |
| 464 | schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE); |
| 465 | } |