Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @file oprofile.h |
| 3 | * |
| 4 | * API for machine-specific interrupts to interface |
| 5 | * to oprofile. |
| 6 | * |
| 7 | * @remark Copyright 2002 OProfile authors |
| 8 | * @remark Read the file COPYING |
| 9 | * |
| 10 | * @author John Levon <levon@movementarian.org> |
| 11 | */ |
| 12 | |
| 13 | #ifndef OPROFILE_H |
| 14 | #define OPROFILE_H |
| 15 | |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/spinlock.h> |
Matt Fleming | 3d90a00 | 2010-09-27 20:45:08 +0100 | [diff] [blame] | 18 | #include <linux/init.h> |
Ari Kauppi | d14dd7e | 2011-01-20 13:57:19 -0500 | [diff] [blame] | 19 | #include <linux/errno.h> |
| 20 | #include <linux/printk.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 21 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 23 | /* Each escaped entry is prefixed by ESCAPE_CODE |
| 24 | * then one of the following codes, then the |
| 25 | * relevant data. |
| 26 | * These #defines live in this file so that arch-specific |
| 27 | * buffer sync'ing code can access them. |
| 28 | */ |
| 29 | #define ESCAPE_CODE ~0UL |
| 30 | #define CTX_SWITCH_CODE 1 |
| 31 | #define CPU_SWITCH_CODE 2 |
| 32 | #define COOKIE_SWITCH_CODE 3 |
| 33 | #define KERNEL_ENTER_SWITCH_CODE 4 |
| 34 | #define KERNEL_EXIT_SWITCH_CODE 5 |
| 35 | #define MODULE_LOADED_CODE 6 |
| 36 | #define CTX_TGID_CODE 7 |
| 37 | #define TRACE_BEGIN_CODE 8 |
| 38 | #define TRACE_END_CODE 9 |
| 39 | #define XEN_ENTER_SWITCH_CODE 10 |
| 40 | #define SPU_PROFILING_CODE 11 |
| 41 | #define SPU_CTX_SWITCH_CODE 12 |
Robert Richter | ee648bc | 2008-07-22 21:08:53 +0200 | [diff] [blame] | 42 | #define IBS_FETCH_CODE 13 |
| 43 | #define IBS_OP_CODE 14 |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | struct dentry; |
| 46 | struct file_operations; |
| 47 | struct pt_regs; |
| 48 | |
| 49 | /* Operations structure to be filled in */ |
| 50 | struct oprofile_operations { |
| 51 | /* create any necessary configuration files in the oprofile fs. |
| 52 | * Optional. */ |
Al Viro | ef7bca1 | 2013-07-19 15:52:42 +0400 | [diff] [blame] | 53 | int (*create_files)(struct dentry * root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | /* Do any necessary interrupt setup. Optional. */ |
| 55 | int (*setup)(void); |
| 56 | /* Do any necessary interrupt shutdown. Optional. */ |
| 57 | void (*shutdown)(void); |
| 58 | /* Start delivering interrupts. */ |
| 59 | int (*start)(void); |
| 60 | /* Stop delivering interrupts. */ |
| 61 | void (*stop)(void); |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 62 | /* Arch-specific buffer sync functions. |
| 63 | * Return value = 0: Success |
| 64 | * Return value = -1: Failure |
| 65 | * Return value = 1: Run generic sync function |
| 66 | */ |
| 67 | int (*sync_start)(void); |
| 68 | int (*sync_stop)(void); |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | /* Initiate a stack backtrace. Optional. */ |
| 71 | void (*backtrace)(struct pt_regs * const regs, unsigned int depth); |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 72 | |
| 73 | /* Multiplex between different events. Optional. */ |
| 74 | int (*switch_events)(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | /* CPU identification string. */ |
| 76 | char * cpu_type; |
| 77 | }; |
| 78 | |
| 79 | /** |
| 80 | * One-time initialisation. *ops must be set to a filled-in |
| 81 | * operations structure. This is called even in timer interrupt |
| 82 | * mode so an arch can set a backtrace callback. |
| 83 | * |
| 84 | * If an error occurs, the fields should be left untouched. |
| 85 | */ |
| 86 | int oprofile_arch_init(struct oprofile_operations * ops); |
| 87 | |
| 88 | /** |
| 89 | * One-time exit/cleanup for the arch. |
| 90 | */ |
| 91 | void oprofile_arch_exit(void); |
| 92 | |
| 93 | /** |
Robert Richter | 58494487 | 2008-11-26 12:02:53 +0100 | [diff] [blame] | 94 | * Add a sample. This may be called from any context. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | */ |
| 96 | void oprofile_add_sample(struct pt_regs * const regs, unsigned long event); |
| 97 | |
Brian Rogan | 2735771 | 2006-03-28 01:56:20 -0800 | [diff] [blame] | 98 | /** |
| 99 | * Add an extended sample. Use this when the PC is not from the regs, and |
| 100 | * we cannot determine if we're in kernel mode from the regs. |
| 101 | * |
| 102 | * This function does perform a backtrace. |
| 103 | * |
| 104 | */ |
| 105 | void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs, |
| 106 | unsigned long event, int is_kernel); |
| 107 | |
Heinz Graalfs | 54ebbe7 | 2011-01-21 10:06:54 +0000 | [diff] [blame] | 108 | /** |
| 109 | * Add an hardware sample. |
| 110 | */ |
| 111 | void oprofile_add_ext_hw_sample(unsigned long pc, struct pt_regs * const regs, |
| 112 | unsigned long event, int is_kernel, |
| 113 | struct task_struct *task); |
| 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | /* Use this instead when the PC value is not from the regs. Doesn't |
| 116 | * backtrace. */ |
| 117 | void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event); |
| 118 | |
| 119 | /* add a backtrace entry, to be called from the ->backtrace callback */ |
| 120 | void oprofile_add_trace(unsigned long eip); |
| 121 | |
| 122 | |
| 123 | /** |
| 124 | * Create a file of the given name as a child of the given root, with |
| 125 | * the specified file operations. |
| 126 | */ |
Al Viro | 6af4ea0 | 2013-07-19 16:10:36 +0400 | [diff] [blame] | 127 | int oprofilefs_create_file(struct dentry * root, |
Arjan van de Ven | 99ac48f | 2006-03-28 01:56:41 -0800 | [diff] [blame] | 128 | char const * name, const struct file_operations * fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Al Viro | 6af4ea0 | 2013-07-19 16:10:36 +0400 | [diff] [blame] | 130 | int oprofilefs_create_file_perm(struct dentry * root, |
Arjan van de Ven | 99ac48f | 2006-03-28 01:56:41 -0800 | [diff] [blame] | 131 | char const * name, const struct file_operations * fops, int perm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | /** Create a file for read/write access to an unsigned long. */ |
Al Viro | 6af4ea0 | 2013-07-19 16:10:36 +0400 | [diff] [blame] | 134 | int oprofilefs_create_ulong(struct dentry * root, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | char const * name, ulong * val); |
| 136 | |
| 137 | /** Create a file for read-only access to an unsigned long. */ |
Al Viro | 6af4ea0 | 2013-07-19 16:10:36 +0400 | [diff] [blame] | 138 | int oprofilefs_create_ro_ulong(struct dentry * root, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | char const * name, ulong * val); |
| 140 | |
| 141 | /** Create a file for read-only access to an atomic_t. */ |
Al Viro | 6af4ea0 | 2013-07-19 16:10:36 +0400 | [diff] [blame] | 142 | int oprofilefs_create_ro_atomic(struct dentry * root, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | char const * name, atomic_t * val); |
| 144 | |
| 145 | /** create a directory */ |
Al Viro | ecde282 | 2013-07-19 15:58:27 +0400 | [diff] [blame] | 146 | struct dentry *oprofilefs_mkdir(struct dentry *parent, char const *name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * Write the given asciz string to the given user buffer @buf, updating *offset |
| 150 | * appropriately. Returns bytes written or -EFAULT. |
| 151 | */ |
| 152 | ssize_t oprofilefs_str_to_user(char const * str, char __user * buf, size_t count, loff_t * offset); |
| 153 | |
| 154 | /** |
| 155 | * Convert an unsigned long value into ASCII and copy it to the user buffer @buf, |
| 156 | * updating *offset appropriately. Returns bytes written or -EFAULT. |
| 157 | */ |
| 158 | ssize_t oprofilefs_ulong_to_user(unsigned long val, char __user * buf, size_t count, loff_t * offset); |
| 159 | |
| 160 | /** |
| 161 | * Read an ASCII string for a number from a userspace buffer and fill *val on success. |
| 162 | * Returns 0 on success, < 0 on error. |
| 163 | */ |
| 164 | int oprofilefs_ulong_from_user(unsigned long * val, char const __user * buf, size_t count); |
| 165 | |
| 166 | /** lock for read/write safety */ |
Thomas Gleixner | 2d21a29 | 2009-07-25 16:18:34 +0200 | [diff] [blame] | 167 | extern raw_spinlock_t oprofilefs_lock; |
Carl Love | a5598ca | 2008-10-14 23:37:01 +0000 | [diff] [blame] | 168 | |
| 169 | /** |
| 170 | * Add the contents of a circular buffer to the event buffer. |
| 171 | */ |
| 172 | void oprofile_put_buff(unsigned long *buf, unsigned int start, |
| 173 | unsigned int stop, unsigned int max); |
| 174 | |
| 175 | unsigned long oprofile_get_cpu_buffer_size(void); |
| 176 | void oprofile_cpu_buffer_inc_smpl_lost(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Robert Richter | 14f0ca8 | 2009-01-07 21:50:22 +0100 | [diff] [blame] | 178 | /* cpu buffer functions */ |
| 179 | |
| 180 | struct op_sample; |
| 181 | |
| 182 | struct op_entry { |
| 183 | struct ring_buffer_event *event; |
| 184 | struct op_sample *sample; |
Robert Richter | 14f0ca8 | 2009-01-07 21:50:22 +0100 | [diff] [blame] | 185 | unsigned long size; |
| 186 | unsigned long *data; |
| 187 | }; |
| 188 | |
| 189 | void oprofile_write_reserve(struct op_entry *entry, |
| 190 | struct pt_regs * const regs, |
| 191 | unsigned long pc, int code, int size); |
| 192 | int oprofile_add_data(struct op_entry *entry, unsigned long val); |
Robert Richter | 51563a0 | 2009-06-03 20:54:56 +0200 | [diff] [blame] | 193 | int oprofile_add_data64(struct op_entry *entry, u64 val); |
Robert Richter | 14f0ca8 | 2009-01-07 21:50:22 +0100 | [diff] [blame] | 194 | int oprofile_write_commit(struct op_entry *entry); |
| 195 | |
Ari Kauppi | 1ea1bdf | 2011-01-20 13:57:18 -0500 | [diff] [blame] | 196 | #ifdef CONFIG_HW_PERF_EVENTS |
Matt Fleming | 3d90a00 | 2010-09-27 20:45:08 +0100 | [diff] [blame] | 197 | int __init oprofile_perf_init(struct oprofile_operations *ops); |
Anand Gadiyar | b3b3a9b | 2010-10-14 11:31:42 -0400 | [diff] [blame] | 198 | void oprofile_perf_exit(void); |
Matt Fleming | 5694633 | 2010-10-08 21:42:17 +0100 | [diff] [blame] | 199 | char *op_name_from_perf_id(void); |
Ari Kauppi | d14dd7e | 2011-01-20 13:57:19 -0500 | [diff] [blame] | 200 | #else |
| 201 | static inline int __init oprofile_perf_init(struct oprofile_operations *ops) |
| 202 | { |
| 203 | pr_info("oprofile: hardware counters not available\n"); |
| 204 | return -ENODEV; |
| 205 | } |
| 206 | static inline void oprofile_perf_exit(void) { } |
Ari Kauppi | 1ea1bdf | 2011-01-20 13:57:18 -0500 | [diff] [blame] | 207 | #endif /* CONFIG_HW_PERF_EVENTS */ |
Matt Fleming | 5694633 | 2010-10-08 21:42:17 +0100 | [diff] [blame] | 208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | #endif /* OPROFILE_H */ |