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> |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 15 | #include <asm/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | #include "oprof.h" |
| 18 | #include "event_buffer.h" |
| 19 | #include "cpu_buffer.h" |
| 20 | #include "buffer_sync.h" |
| 21 | #include "oprofile_stats.h" |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | struct oprofile_operations oprofile_ops; |
| 24 | |
| 25 | unsigned long oprofile_started; |
| 26 | unsigned long backtrace_depth; |
Robert Richter | 4c168ea | 2008-09-24 11:08:52 +0200 | [diff] [blame] | 27 | static unsigned long is_setup; |
| 28 | static DEFINE_MUTEX(start_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | /* timer |
| 31 | 0 - use performance monitoring hardware if available |
| 32 | 1 - use the timer int mechanism regardless |
| 33 | */ |
| 34 | static int timer = 0; |
| 35 | |
| 36 | int oprofile_setup(void) |
| 37 | { |
| 38 | int err; |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 39 | |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 40 | mutex_lock(&start_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | if ((err = alloc_cpu_buffers())) |
| 43 | goto out; |
| 44 | |
| 45 | if ((err = alloc_event_buffer())) |
| 46 | goto out1; |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | if (oprofile_ops.setup && (err = oprofile_ops.setup())) |
| 49 | goto out2; |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | /* Note even though this starts part of the |
| 52 | * profiling overhead, it's necessary to prevent |
| 53 | * us missing task deaths and eventually oopsing |
| 54 | * when trying to process the event buffer. |
| 55 | */ |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 56 | if (oprofile_ops.sync_start) { |
| 57 | int sync_ret = oprofile_ops.sync_start(); |
| 58 | switch (sync_ret) { |
| 59 | case 0: |
| 60 | goto post_sync; |
| 61 | case 1: |
| 62 | goto do_generic; |
| 63 | case -1: |
| 64 | goto out3; |
| 65 | default: |
| 66 | goto out3; |
| 67 | } |
| 68 | } |
| 69 | do_generic: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | if ((err = sync_start())) |
| 71 | goto out3; |
| 72 | |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 73 | post_sync: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | is_setup = 1; |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 75 | mutex_unlock(&start_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | return 0; |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | out3: |
| 79 | if (oprofile_ops.shutdown) |
| 80 | oprofile_ops.shutdown(); |
| 81 | out2: |
| 82 | free_event_buffer(); |
| 83 | out1: |
| 84 | free_cpu_buffers(); |
| 85 | out: |
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 err; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /* Actually start profiling (echo 1>/dev/oprofile/enable) */ |
| 92 | int oprofile_start(void) |
| 93 | { |
| 94 | int err = -EINVAL; |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 95 | |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 96 | mutex_lock(&start_mutex); |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | if (!is_setup) |
| 99 | goto out; |
| 100 | |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 101 | err = 0; |
| 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | if (oprofile_started) |
| 104 | goto out; |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | oprofile_reset_stats(); |
| 107 | |
| 108 | if ((err = oprofile_ops.start())) |
| 109 | goto out; |
| 110 | |
| 111 | oprofile_started = 1; |
| 112 | out: |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 113 | mutex_unlock(&start_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | return err; |
| 115 | } |
| 116 | |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | /* echo 0>/dev/oprofile/enable */ |
| 119 | void oprofile_stop(void) |
| 120 | { |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 121 | mutex_lock(&start_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | if (!oprofile_started) |
| 123 | goto out; |
| 124 | oprofile_ops.stop(); |
| 125 | oprofile_started = 0; |
| 126 | /* wake up the daemon to read what remains */ |
| 127 | wake_up_buffer_waiter(); |
| 128 | out: |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 129 | mutex_unlock(&start_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | |
| 133 | void oprofile_shutdown(void) |
| 134 | { |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 135 | mutex_lock(&start_mutex); |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 136 | if (oprofile_ops.sync_stop) { |
| 137 | int sync_ret = oprofile_ops.sync_stop(); |
| 138 | switch (sync_ret) { |
| 139 | case 0: |
| 140 | goto post_sync; |
| 141 | case 1: |
| 142 | goto do_generic; |
| 143 | default: |
| 144 | goto post_sync; |
| 145 | } |
| 146 | } |
| 147 | do_generic: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | sync_stop(); |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 149 | post_sync: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | if (oprofile_ops.shutdown) |
| 151 | oprofile_ops.shutdown(); |
| 152 | is_setup = 0; |
| 153 | free_event_buffer(); |
| 154 | free_cpu_buffers(); |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 155 | mutex_unlock(&start_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | |
| 159 | int oprofile_set_backtrace(unsigned long val) |
| 160 | { |
| 161 | int err = 0; |
| 162 | |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 163 | mutex_lock(&start_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | if (oprofile_started) { |
| 166 | err = -EBUSY; |
| 167 | goto out; |
| 168 | } |
| 169 | |
| 170 | if (!oprofile_ops.backtrace) { |
| 171 | err = -EINVAL; |
| 172 | goto out; |
| 173 | } |
| 174 | |
| 175 | backtrace_depth = val; |
| 176 | |
| 177 | out: |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 178 | mutex_unlock(&start_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | return err; |
| 180 | } |
| 181 | |
| 182 | static int __init oprofile_init(void) |
| 183 | { |
| 184 | int err; |
| 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | err = oprofile_arch_init(&oprofile_ops); |
| 187 | |
| 188 | if (err < 0 || timer) { |
| 189 | printk(KERN_INFO "oprofile: using timer interrupt.\n"); |
| 190 | oprofile_timer_init(&oprofile_ops); |
| 191 | } |
| 192 | |
| 193 | err = oprofilefs_register(); |
| 194 | if (err) |
| 195 | oprofile_arch_exit(); |
| 196 | |
| 197 | return err; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | static void __exit oprofile_exit(void) |
| 202 | { |
| 203 | oprofilefs_unregister(); |
| 204 | oprofile_arch_exit(); |
| 205 | } |
| 206 | |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | module_init(oprofile_init); |
| 209 | module_exit(oprofile_exit); |
| 210 | |
| 211 | module_param_named(timer, timer, int, 0644); |
| 212 | MODULE_PARM_DESC(timer, "force use of timer interrupt"); |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | MODULE_LICENSE("GPL"); |
| 215 | MODULE_AUTHOR("John Levon <levon@movementarian.org>"); |
| 216 | MODULE_DESCRIPTION("OProfile system profiler"); |