Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @file oprof.c |
| 3 | * |
| 4 | * @remark Copyright 2002 OProfile authors |
| 5 | * @remark Read the file COPYING |
| 6 | * |
| 7 | * @author John Levon <levon@movementarian.org> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/oprofile.h> |
| 14 | #include <linux/moduleparam.h> |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 15 | #include <linux/workqueue.h> |
| 16 | #include <linux/time.h> |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 17 | #include <asm/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | #include "oprof.h" |
| 20 | #include "event_buffer.h" |
| 21 | #include "cpu_buffer.h" |
| 22 | #include "buffer_sync.h" |
| 23 | #include "oprofile_stats.h" |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | struct oprofile_operations oprofile_ops; |
| 26 | |
| 27 | unsigned long oprofile_started; |
Robert Richter | bd2172f | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 28 | unsigned long oprofile_backtrace_depth; |
Robert Richter | 4c168ea | 2008-09-24 11:08:52 +0200 | [diff] [blame] | 29 | static unsigned long is_setup; |
| 30 | static DEFINE_MUTEX(start_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 32 | #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX |
| 33 | |
| 34 | static void switch_worker(struct work_struct *work); |
| 35 | static DECLARE_DELAYED_WORK(switch_work, switch_worker); |
| 36 | unsigned long timeout_jiffies; |
| 37 | #define MULTIPLEXING_TIMER_DEFAULT 1 |
| 38 | |
| 39 | #endif |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /* timer |
| 42 | 0 - use performance monitoring hardware if available |
| 43 | 1 - use the timer int mechanism regardless |
| 44 | */ |
| 45 | static int timer = 0; |
| 46 | |
| 47 | int oprofile_setup(void) |
| 48 | { |
| 49 | int err; |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 50 | |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 51 | mutex_lock(&start_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | if ((err = alloc_cpu_buffers())) |
| 54 | goto out; |
| 55 | |
| 56 | if ((err = alloc_event_buffer())) |
| 57 | goto out1; |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | if (oprofile_ops.setup && (err = oprofile_ops.setup())) |
| 60 | goto out2; |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | /* Note even though this starts part of the |
| 63 | * profiling overhead, it's necessary to prevent |
| 64 | * us missing task deaths and eventually oopsing |
| 65 | * when trying to process the event buffer. |
| 66 | */ |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 67 | if (oprofile_ops.sync_start) { |
| 68 | int sync_ret = oprofile_ops.sync_start(); |
| 69 | switch (sync_ret) { |
| 70 | case 0: |
| 71 | goto post_sync; |
| 72 | case 1: |
| 73 | goto do_generic; |
| 74 | case -1: |
| 75 | goto out3; |
| 76 | default: |
| 77 | goto out3; |
| 78 | } |
| 79 | } |
| 80 | do_generic: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | if ((err = sync_start())) |
| 82 | goto out3; |
| 83 | |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 84 | post_sync: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | is_setup = 1; |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 86 | mutex_unlock(&start_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | return 0; |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | out3: |
| 90 | if (oprofile_ops.shutdown) |
| 91 | oprofile_ops.shutdown(); |
| 92 | out2: |
| 93 | free_event_buffer(); |
| 94 | out1: |
| 95 | free_cpu_buffers(); |
| 96 | out: |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 97 | mutex_unlock(&start_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | return err; |
| 99 | } |
| 100 | |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 101 | #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX |
| 102 | |
| 103 | static void start_switch_worker(void) |
| 104 | { |
| 105 | schedule_delayed_work(&switch_work, timeout_jiffies); |
| 106 | } |
| 107 | |
| 108 | static void switch_worker(struct work_struct *work) |
| 109 | { |
| 110 | if (!oprofile_ops.switch_events()) |
| 111 | start_switch_worker(); |
| 112 | } |
| 113 | |
| 114 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | /* Actually start profiling (echo 1>/dev/oprofile/enable) */ |
| 117 | int oprofile_start(void) |
| 118 | { |
| 119 | int err = -EINVAL; |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 120 | |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 121 | mutex_lock(&start_mutex); |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | if (!is_setup) |
| 124 | goto out; |
| 125 | |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 126 | err = 0; |
| 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | if (oprofile_started) |
| 129 | goto out; |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | oprofile_reset_stats(); |
| 132 | |
| 133 | if ((err = oprofile_ops.start())) |
| 134 | goto out; |
| 135 | |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 136 | #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX |
| 137 | if (oprofile_ops.switch_events) |
| 138 | start_switch_worker(); |
| 139 | #endif |
| 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | oprofile_started = 1; |
| 142 | out: |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 143 | mutex_unlock(&start_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | return err; |
| 145 | } |
| 146 | |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | /* echo 0>/dev/oprofile/enable */ |
| 149 | void oprofile_stop(void) |
| 150 | { |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 151 | mutex_lock(&start_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | if (!oprofile_started) |
| 153 | goto out; |
| 154 | oprofile_ops.stop(); |
| 155 | oprofile_started = 0; |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 156 | |
| 157 | #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX |
| 158 | cancel_delayed_work_sync(&switch_work); |
| 159 | #endif |
| 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | /* wake up the daemon to read what remains */ |
| 162 | wake_up_buffer_waiter(); |
| 163 | out: |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 164 | mutex_unlock(&start_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | |
| 168 | void oprofile_shutdown(void) |
| 169 | { |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 170 | mutex_lock(&start_mutex); |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 171 | if (oprofile_ops.sync_stop) { |
| 172 | int sync_ret = oprofile_ops.sync_stop(); |
| 173 | switch (sync_ret) { |
| 174 | case 0: |
| 175 | goto post_sync; |
| 176 | case 1: |
| 177 | goto do_generic; |
| 178 | default: |
| 179 | goto post_sync; |
| 180 | } |
| 181 | } |
| 182 | do_generic: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | sync_stop(); |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 184 | post_sync: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | if (oprofile_ops.shutdown) |
| 186 | oprofile_ops.shutdown(); |
| 187 | is_setup = 0; |
| 188 | free_event_buffer(); |
| 189 | free_cpu_buffers(); |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 190 | mutex_unlock(&start_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 193 | #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX |
| 194 | |
| 195 | /* User inputs in ms, converts to jiffies */ |
| 196 | int oprofile_set_timeout(unsigned long val_msec) |
| 197 | { |
| 198 | int err = 0; |
Robert Richter | 2051cad | 2009-07-15 15:44:18 +0200 | [diff] [blame^] | 199 | unsigned long time_slice; |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 200 | |
| 201 | mutex_lock(&start_mutex); |
| 202 | |
| 203 | if (oprofile_started) { |
| 204 | err = -EBUSY; |
| 205 | goto out; |
| 206 | } |
| 207 | |
| 208 | if (!oprofile_ops.switch_events) { |
| 209 | err = -EINVAL; |
| 210 | goto out; |
| 211 | } |
| 212 | |
Robert Richter | 2051cad | 2009-07-15 15:44:18 +0200 | [diff] [blame^] | 213 | time_slice = msecs_to_jiffies(val_msec); |
| 214 | if (time_slice == MAX_JIFFY_OFFSET) { |
| 215 | err = -EINVAL; |
| 216 | goto out; |
| 217 | } |
| 218 | |
| 219 | timeout_jiffies = time_slice; |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 220 | |
| 221 | out: |
| 222 | mutex_unlock(&start_mutex); |
| 223 | return err; |
| 224 | |
| 225 | } |
| 226 | |
| 227 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
| 229 | int oprofile_set_backtrace(unsigned long val) |
| 230 | { |
| 231 | int err = 0; |
| 232 | |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 233 | mutex_lock(&start_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
| 235 | if (oprofile_started) { |
| 236 | err = -EBUSY; |
| 237 | goto out; |
| 238 | } |
| 239 | |
| 240 | if (!oprofile_ops.backtrace) { |
| 241 | err = -EINVAL; |
| 242 | goto out; |
| 243 | } |
| 244 | |
Robert Richter | bd2172f | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 245 | oprofile_backtrace_depth = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
| 247 | out: |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 248 | mutex_unlock(&start_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | return err; |
| 250 | } |
| 251 | |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 252 | #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX |
| 253 | |
| 254 | static void __init oprofile_multiplexing_init(void) |
| 255 | { |
| 256 | timeout_jiffies = msecs_to_jiffies(MULTIPLEXING_TIMER_DEFAULT); |
| 257 | } |
| 258 | |
| 259 | #endif |
| 260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | static int __init oprofile_init(void) |
| 262 | { |
| 263 | int err; |
| 264 | |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 265 | #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX |
| 266 | oprofile_multiplexing_init(); |
| 267 | #endif |
| 268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | err = oprofile_arch_init(&oprofile_ops); |
| 270 | |
| 271 | if (err < 0 || timer) { |
| 272 | printk(KERN_INFO "oprofile: using timer interrupt.\n"); |
| 273 | oprofile_timer_init(&oprofile_ops); |
| 274 | } |
| 275 | |
| 276 | err = oprofilefs_register(); |
Robert Richter | 4c50d9e | 2009-01-22 14:14:14 +0100 | [diff] [blame] | 277 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | oprofile_arch_exit(); |
| 279 | |
| 280 | return err; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | static void __exit oprofile_exit(void) |
| 285 | { |
| 286 | oprofilefs_unregister(); |
| 287 | oprofile_arch_exit(); |
| 288 | } |
| 289 | |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | module_init(oprofile_init); |
| 292 | module_exit(oprofile_exit); |
| 293 | |
| 294 | module_param_named(timer, timer, int, 0644); |
| 295 | MODULE_PARM_DESC(timer, "force use of timer interrupt"); |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | MODULE_LICENSE("GPL"); |
| 298 | MODULE_AUTHOR("John Levon <levon@movementarian.org>"); |
| 299 | MODULE_DESCRIPTION("OProfile system profiler"); |