Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @file cpu_buffer.h |
| 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> |
Robert Richter | 2cc28b9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 8 | * @author Robert Richter <robert.richter@amd.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #ifndef OPROFILE_CPU_BUFFER_H |
| 12 | #define OPROFILE_CPU_BUFFER_H |
| 13 | |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/spinlock.h> |
| 16 | #include <linux/workqueue.h> |
| 17 | #include <linux/cache.h> |
Mike Travis | 608dfdd | 2008-04-28 02:14:15 -0700 | [diff] [blame] | 18 | #include <linux/sched.h> |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 19 | #include <linux/ring_buffer.h> |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | struct task_struct; |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | int alloc_cpu_buffers(void); |
| 24 | void free_cpu_buffers(void); |
| 25 | |
| 26 | void start_cpu_work(void); |
| 27 | void end_cpu_work(void); |
Tejun Heo | 3d7851b | 2010-10-15 09:51:08 -0400 | [diff] [blame] | 28 | void flush_cpu_work(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | /* CPU buffer is composed of such entries (which are |
| 31 | * also used for context switch notes) |
| 32 | */ |
| 33 | struct op_sample { |
| 34 | unsigned long eip; |
| 35 | unsigned long event; |
Robert Richter | 2cc28b9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 36 | unsigned long data[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | }; |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 38 | |
Robert Richter | 14f0ca8 | 2009-01-07 21:50:22 +0100 | [diff] [blame] | 39 | struct op_entry; |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | struct oprofile_cpu_buffer { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | unsigned long buffer_size; |
Robert Richter | 25ad291 | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 43 | struct task_struct *last_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | int last_is_kernel; |
| 45 | int tracing; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | unsigned long sample_received; |
| 47 | unsigned long sample_lost_overflow; |
| 48 | unsigned long backtrace_aborted; |
Philippe Elie | df9d177 | 2007-11-14 16:58:48 -0800 | [diff] [blame] | 49 | unsigned long sample_invalid_eip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | int cpu; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 51 | struct delayed_work work; |
Eric Dumazet | 8b8b498 | 2008-05-14 16:05:31 -0700 | [diff] [blame] | 52 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Tejun Heo | b3e9f67 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 54 | DECLARE_PER_CPU(struct oprofile_cpu_buffer, op_cpu_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Robert Richter | fbc9bf9 | 2008-12-04 16:27:00 +0100 | [diff] [blame] | 56 | /* |
| 57 | * Resets the cpu buffer to a sane state. |
| 58 | * |
| 59 | * reset these to invalid values; the next sample collected will |
| 60 | * populate the buffer with proper values to initialize the buffer |
| 61 | */ |
Robert Richter | 6d2c53f | 2008-12-24 16:53:53 +0100 | [diff] [blame] | 62 | static inline void op_cpu_buffer_reset(int cpu) |
Robert Richter | fbc9bf9 | 2008-12-04 16:27:00 +0100 | [diff] [blame] | 63 | { |
Tejun Heo | b3e9f67 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 64 | struct oprofile_cpu_buffer *cpu_buf = &per_cpu(op_cpu_buffer, cpu); |
Robert Richter | fbc9bf9 | 2008-12-04 16:27:00 +0100 | [diff] [blame] | 65 | |
| 66 | cpu_buf->last_is_kernel = -1; |
| 67 | cpu_buf->last_task = NULL; |
| 68 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Robert Richter | fdb6a8f | 2009-01-17 17:13:27 +0100 | [diff] [blame] | 70 | /* |
| 71 | * op_cpu_buffer_add_data() and op_cpu_buffer_write_commit() may be |
| 72 | * called only if op_cpu_buffer_write_reserve() did not return NULL or |
| 73 | * entry->event != NULL, otherwise entry->size or entry->event will be |
| 74 | * used uninitialized. |
| 75 | */ |
| 76 | |
Robert Richter | 2cc28b9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 77 | struct op_sample |
| 78 | *op_cpu_buffer_write_reserve(struct op_entry *entry, unsigned long size); |
Robert Richter | 9966718 | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 79 | int op_cpu_buffer_write_commit(struct op_entry *entry); |
Robert Richter | 2d87b14 | 2008-12-30 04:10:46 +0100 | [diff] [blame] | 80 | 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] | 81 | unsigned long op_cpu_buffer_entries(int cpu); |
Robert Richter | bf589e3 | 2008-11-27 22:33:37 +0100 | [diff] [blame] | 82 | |
Robert Richter | d9928c2 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 83 | /* returns the remaining free size of data in the entry */ |
| 84 | static inline |
| 85 | int op_cpu_buffer_add_data(struct op_entry *entry, unsigned long val) |
| 86 | { |
| 87 | if (!entry->size) |
| 88 | return 0; |
| 89 | *entry->data = val; |
| 90 | entry->size--; |
| 91 | entry->data++; |
| 92 | return entry->size; |
| 93 | } |
| 94 | |
Robert Richter | bd7dc46 | 2009-01-06 03:56:50 +0100 | [diff] [blame] | 95 | /* returns the size of data in the entry */ |
| 96 | static inline |
| 97 | int op_cpu_buffer_get_size(struct op_entry *entry) |
| 98 | { |
| 99 | return entry->size; |
| 100 | } |
| 101 | |
| 102 | /* returns 0 if empty or the size of data including the current value */ |
| 103 | static inline |
| 104 | int op_cpu_buffer_get_data(struct op_entry *entry, unsigned long *val) |
| 105 | { |
| 106 | int size = entry->size; |
| 107 | if (!size) |
| 108 | return 0; |
| 109 | *val = *entry->data; |
| 110 | entry->size--; |
| 111 | entry->data++; |
| 112 | return size; |
| 113 | } |
| 114 | |
Robert Richter | ae735e9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 115 | /* extra data flags */ |
| 116 | #define KERNEL_CTX_SWITCH (1UL << 0) |
| 117 | #define IS_KERNEL (1UL << 1) |
| 118 | #define TRACE_BEGIN (1UL << 2) |
| 119 | #define USER_CTX_SWITCH (1UL << 3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
| 121 | #endif /* OPROFILE_CPU_BUFFER_H */ |