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 struct oprofile_operations oprofile_ops;
27 unsigned long oprofile_started;
28 unsigned long oprofile_backtrace_depth;
29 static unsigned long is_setup;
30 static DEFINE_MUTEX(start_mutex);
32 #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
34 static void switch_worker(struct work_struct *work);
35 static DECLARE_DELAYED_WORK(switch_work, switch_worker);
36 #define TIME_SLICE_DEFAULT 1
41 0 - use performance monitoring hardware if available
42 1 - use the timer int mechanism regardless
46 int oprofile_setup(void)
50 mutex_lock(&start_mutex);
52 if ((err = alloc_cpu_buffers()))
55 if ((err = alloc_event_buffer()))
58 if (oprofile_ops.setup && (err = oprofile_ops.setup()))
61 /* Note even though this starts part of the
62 * profiling overhead, it's necessary to prevent
63 * us missing task deaths and eventually oopsing
64 * when trying to process the event buffer.
66 if (oprofile_ops.sync_start) {
67 int sync_ret = oprofile_ops.sync_start();
80 if ((err = sync_start()))
85 mutex_unlock(&start_mutex);
89 if (oprofile_ops.shutdown)
90 oprofile_ops.shutdown();
96 mutex_unlock(&start_mutex);
100 #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
102 static void start_switch_worker(void)
104 schedule_delayed_work(&switch_work, oprofile_time_slice);
107 static void switch_worker(struct work_struct *work)
109 if (!oprofile_ops.switch_events())
110 start_switch_worker();
115 /* Actually start profiling (echo 1>/dev/oprofile/enable) */
116 int oprofile_start(void)
120 mutex_lock(&start_mutex);
127 if (oprofile_started)
130 oprofile_reset_stats();
132 if ((err = oprofile_ops.start()))
135 #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
136 if (oprofile_ops.switch_events)
137 start_switch_worker();
140 oprofile_started = 1;
142 mutex_unlock(&start_mutex);
147 /* echo 0>/dev/oprofile/enable */
148 void oprofile_stop(void)
150 mutex_lock(&start_mutex);
151 if (!oprofile_started)
154 oprofile_started = 0;
156 #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
157 cancel_delayed_work_sync(&switch_work);
160 /* wake up the daemon to read what remains */
161 wake_up_buffer_waiter();
163 mutex_unlock(&start_mutex);
167 void oprofile_shutdown(void)
169 mutex_lock(&start_mutex);
170 if (oprofile_ops.sync_stop) {
171 int sync_ret = oprofile_ops.sync_stop();
184 if (oprofile_ops.shutdown)
185 oprofile_ops.shutdown();
189 mutex_unlock(&start_mutex);
192 #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
194 /* User inputs in ms, converts to jiffies */
195 int oprofile_set_timeout(unsigned long val_msec)
198 unsigned long time_slice;
200 mutex_lock(&start_mutex);
202 if (oprofile_started) {
207 if (!oprofile_ops.switch_events) {
212 time_slice = msecs_to_jiffies(val_msec);
213 if (time_slice == MAX_JIFFY_OFFSET) {
218 oprofile_time_slice = time_slice;
221 mutex_unlock(&start_mutex);
228 int oprofile_set_backtrace(unsigned long val)
232 mutex_lock(&start_mutex);
234 if (oprofile_started) {
239 if (!oprofile_ops.backtrace) {
244 oprofile_backtrace_depth = val;
247 mutex_unlock(&start_mutex);
251 #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
253 static void __init oprofile_multiplexing_init(void)
255 oprofile_time_slice = msecs_to_jiffies(TIME_SLICE_DEFAULT);
260 static int __init oprofile_init(void)
264 #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
265 oprofile_multiplexing_init();
268 err = oprofile_arch_init(&oprofile_ops);
270 if (err < 0 || timer) {
271 printk(KERN_INFO "oprofile: using timer interrupt.\n");
272 oprofile_timer_init(&oprofile_ops);
275 err = oprofilefs_register();
277 oprofile_arch_exit();
283 static void __exit oprofile_exit(void)
285 oprofilefs_unregister();
286 oprofile_arch_exit();
290 module_init(oprofile_init);
291 module_exit(oprofile_exit);
293 module_param_named(timer, timer, int, 0644);
294 MODULE_PARM_DESC(timer, "force use of timer interrupt");
296 MODULE_LICENSE("GPL");
297 MODULE_AUTHOR("John Levon <levon@movementarian.org>");
298 MODULE_DESCRIPTION("OProfile system profiler");