Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @file cpu_buffer.h |
| 3 | * |
| 4 | * @remark Copyright 2002 OProfile authors |
| 5 | * @remark Read the file COPYING |
| 6 | * |
| 7 | * @author John Levon <levon@movementarian.org> |
| 8 | */ |
| 9 | |
| 10 | #ifndef OPROFILE_CPU_BUFFER_H |
| 11 | #define OPROFILE_CPU_BUFFER_H |
| 12 | |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/spinlock.h> |
| 15 | #include <linux/workqueue.h> |
| 16 | #include <linux/cache.h> |
Mike Travis | 608dfdd | 2008-04-28 02:14:15 -0700 | [diff] [blame] | 17 | #include <linux/sched.h> |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 18 | #include <linux/ring_buffer.h> |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | struct task_struct; |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | int alloc_cpu_buffers(void); |
| 23 | void free_cpu_buffers(void); |
| 24 | |
| 25 | void start_cpu_work(void); |
| 26 | void end_cpu_work(void); |
| 27 | |
| 28 | /* CPU buffer is composed of such entries (which are |
| 29 | * also used for context switch notes) |
| 30 | */ |
| 31 | struct op_sample { |
| 32 | unsigned long eip; |
| 33 | unsigned long event; |
| 34 | }; |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 35 | |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 36 | struct op_entry { |
| 37 | struct ring_buffer_event *event; |
| 38 | struct op_sample *sample; |
| 39 | unsigned long irq_flags; |
| 40 | }; |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | struct oprofile_cpu_buffer { |
| 43 | volatile unsigned long head_pos; |
| 44 | volatile unsigned long tail_pos; |
| 45 | unsigned long buffer_size; |
Robert Richter | 25ad291 | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 46 | struct task_struct *last_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | int last_is_kernel; |
| 48 | int tracing; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | unsigned long sample_received; |
| 50 | unsigned long sample_lost_overflow; |
| 51 | unsigned long backtrace_aborted; |
Philippe Elie | df9d177 | 2007-11-14 16:58:48 -0800 | [diff] [blame] | 52 | unsigned long sample_invalid_eip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | int cpu; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 54 | struct delayed_work work; |
Eric Dumazet | 8b8b498 | 2008-05-14 16:05:31 -0700 | [diff] [blame] | 55 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 57 | extern struct ring_buffer *op_ring_buffer_read; |
| 58 | extern struct ring_buffer *op_ring_buffer_write; |
Mike Travis | 608dfdd | 2008-04-28 02:14:15 -0700 | [diff] [blame] | 59 | DECLARE_PER_CPU(struct oprofile_cpu_buffer, cpu_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Robert Richter | fbc9bf9 | 2008-12-04 16:27:00 +0100 | [diff] [blame] | 61 | /* |
| 62 | * Resets the cpu buffer to a sane state. |
| 63 | * |
| 64 | * reset these to invalid values; the next sample collected will |
| 65 | * populate the buffer with proper values to initialize the buffer |
| 66 | */ |
Robert Richter | 6d2c53f | 2008-12-24 16:53:53 +0100 | [diff] [blame^] | 67 | static inline void op_cpu_buffer_reset(int cpu) |
Robert Richter | fbc9bf9 | 2008-12-04 16:27:00 +0100 | [diff] [blame] | 68 | { |
| 69 | struct oprofile_cpu_buffer *cpu_buf = &per_cpu(cpu_buffer, cpu); |
| 70 | |
| 71 | cpu_buf->last_is_kernel = -1; |
| 72 | cpu_buf->last_task = NULL; |
| 73 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Robert Richter | 6d2c53f | 2008-12-24 16:53:53 +0100 | [diff] [blame^] | 75 | static inline int op_cpu_buffer_write_entry(struct op_entry *entry) |
Robert Richter | 7d468ab | 2008-11-27 10:57:09 +0100 | [diff] [blame] | 76 | { |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 77 | entry->event = ring_buffer_lock_reserve(op_ring_buffer_write, |
| 78 | sizeof(struct op_sample), |
| 79 | &entry->irq_flags); |
| 80 | if (entry->event) |
| 81 | entry->sample = ring_buffer_event_data(entry->event); |
Robert Richter | 229234a | 2008-11-27 18:36:08 +0100 | [diff] [blame] | 82 | else |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 83 | entry->sample = NULL; |
| 84 | |
| 85 | if (!entry->sample) |
| 86 | return -ENOMEM; |
| 87 | |
| 88 | return 0; |
Robert Richter | 229234a | 2008-11-27 18:36:08 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Robert Richter | 6d2c53f | 2008-12-24 16:53:53 +0100 | [diff] [blame^] | 91 | static inline int op_cpu_buffer_write_commit(struct op_entry *entry) |
Robert Richter | 7d468ab | 2008-11-27 10:57:09 +0100 | [diff] [blame] | 92 | { |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 93 | return ring_buffer_unlock_commit(op_ring_buffer_write, entry->event, |
| 94 | entry->irq_flags); |
| 95 | } |
| 96 | |
Robert Richter | 6d2c53f | 2008-12-24 16:53:53 +0100 | [diff] [blame^] | 97 | static inline struct op_sample *op_cpu_buffer_read_entry(int cpu) |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 98 | { |
| 99 | struct ring_buffer_event *e; |
| 100 | e = ring_buffer_consume(op_ring_buffer_read, cpu, NULL); |
| 101 | if (e) |
| 102 | return ring_buffer_event_data(e); |
| 103 | if (ring_buffer_swap_cpu(op_ring_buffer_read, |
| 104 | op_ring_buffer_write, |
| 105 | cpu)) |
| 106 | return NULL; |
| 107 | e = ring_buffer_consume(op_ring_buffer_read, cpu, NULL); |
| 108 | if (e) |
| 109 | return ring_buffer_event_data(e); |
| 110 | return NULL; |
Robert Richter | 7d468ab | 2008-11-27 10:57:09 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Robert Richter | bf589e3 | 2008-11-27 22:33:37 +0100 | [diff] [blame] | 113 | /* "acquire" as many cpu buffer slots as we can */ |
Robert Richter | 6d2c53f | 2008-12-24 16:53:53 +0100 | [diff] [blame^] | 114 | static inline unsigned long op_cpu_buffer_entries(int cpu) |
Robert Richter | bf589e3 | 2008-11-27 22:33:37 +0100 | [diff] [blame] | 115 | { |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 116 | return ring_buffer_entries_cpu(op_ring_buffer_read, cpu) |
| 117 | + ring_buffer_entries_cpu(op_ring_buffer_write, cpu); |
Robert Richter | bf589e3 | 2008-11-27 22:33:37 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | /* transient events for the CPU buffer -> event buffer */ |
| 121 | #define CPU_IS_KERNEL 1 |
| 122 | #define CPU_TRACE_BEGIN 2 |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 123 | #define IBS_FETCH_BEGIN 3 |
| 124 | #define IBS_OP_BEGIN 4 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
| 126 | #endif /* OPROFILE_CPU_BUFFER_H */ |