4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
7 * @author John Levon <levon@movementarian.org>
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>
15 #include <linux/workqueue.h>
16 #include <linux/time.h>
17 #include <asm/mutex.h>
20 #include "event_buffer.h"
21 #include "cpu_buffer.h"
22 #include "buffer_sync.h"
23 #include "oprofile_stats.h"
25 static unsigned long is_setup;
26 static void switch_worker(struct work_struct *work);
27 static DECLARE_DELAYED_WORK(switch_work, switch_worker);
28 static DEFINE_MUTEX(start_mutex);
30 struct oprofile_operations oprofile_ops;
32 unsigned long timeout_jiffies;
33 unsigned long oprofile_started;
34 unsigned long backtrace_depth;
35 /* Multiplexing defaults at 1 msec*/
38 0 - use performance monitoring hardware if available
39 1 - use the timer int mechanism regardless
43 int oprofile_setup(void)
47 mutex_lock(&start_mutex);
49 if ((err = alloc_cpu_buffers()))
52 if ((err = alloc_event_buffer()))
55 if (oprofile_ops.setup && (err = oprofile_ops.setup()))
58 /* Note even though this starts part of the
59 * profiling overhead, it's necessary to prevent
60 * us missing task deaths and eventually oopsing
61 * when trying to process the event buffer.
63 if (oprofile_ops.sync_start) {
64 int sync_ret = oprofile_ops.sync_start();
77 if ((err = sync_start()))
82 mutex_unlock(&start_mutex);
86 if (oprofile_ops.shutdown)
87 oprofile_ops.shutdown();
93 mutex_unlock(&start_mutex);
97 static void start_switch_worker(void)
99 schedule_delayed_work(&switch_work, timeout_jiffies);
102 static void switch_worker(struct work_struct *work)
104 if (!oprofile_ops.switch_events())
105 start_switch_worker();
108 /* Actually start profiling (echo 1>/dev/oprofile/enable) */
109 int oprofile_start(void)
113 mutex_lock(&start_mutex);
119 if (oprofile_started)
122 oprofile_reset_stats();
124 if ((err = oprofile_ops.start()))
127 if (oprofile_ops.switch_events)
128 start_switch_worker();
130 oprofile_started = 1;
132 mutex_unlock(&start_mutex);
137 /* echo 0>/dev/oprofile/enable */
138 void oprofile_stop(void)
140 mutex_lock(&start_mutex);
141 if (!oprofile_started)
144 oprofile_started = 0;
145 cancel_delayed_work_sync(&switch_work);
146 /* wake up the daemon to read what remains */
147 wake_up_buffer_waiter();
149 mutex_unlock(&start_mutex);
153 void oprofile_shutdown(void)
155 mutex_lock(&start_mutex);
156 if (oprofile_ops.sync_stop) {
157 int sync_ret = oprofile_ops.sync_stop();
170 if (oprofile_ops.shutdown)
171 oprofile_ops.shutdown();
175 mutex_unlock(&start_mutex);
178 /* User inputs in ms, converts to jiffies */
179 int oprofile_set_timeout(unsigned long val_msec)
183 mutex_lock(&start_mutex);
185 if (oprofile_started) {
190 if (!oprofile_ops.switch_events) {
195 timeout_jiffies = msecs_to_jiffies(val_msec);
196 if (timeout_jiffies == MAX_JIFFY_OFFSET)
197 timeout_jiffies = msecs_to_jiffies(1);
200 mutex_unlock(&start_mutex);
205 int oprofile_set_backtrace(unsigned long val)
209 mutex_lock(&start_mutex);
211 if (oprofile_started) {
216 if (!oprofile_ops.backtrace) {
221 backtrace_depth = val;
224 mutex_unlock(&start_mutex);
228 static void __init oprofile_switch_timer_init(void)
230 timeout_jiffies = msecs_to_jiffies(1);
233 static int __init oprofile_init(void)
237 oprofile_switch_timer_init();
238 err = oprofile_arch_init(&oprofile_ops);
240 if (err < 0 || timer) {
241 printk(KERN_INFO "oprofile: using timer interrupt.\n");
242 oprofile_timer_init(&oprofile_ops);
245 err = oprofilefs_register();
247 oprofile_arch_exit();
253 static void __exit oprofile_exit(void)
255 oprofilefs_unregister();
256 oprofile_arch_exit();
260 module_init(oprofile_init);
261 module_exit(oprofile_exit);
263 module_param_named(timer, timer, int, 0644);
264 MODULE_PARM_DESC(timer, "force use of timer interrupt");
266 MODULE_LICENSE("GPL");
267 MODULE_AUTHOR("John Levon <levon@movementarian.org>");
268 MODULE_DESCRIPTION("OProfile system profiler");