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 <asm/mutex.h>
18 #include "event_buffer.h"
19 #include "cpu_buffer.h"
20 #include "buffer_sync.h"
21 #include "oprofile_stats.h"
23 struct oprofile_operations oprofile_ops;
25 unsigned long oprofile_started;
26 unsigned long oprofile_backtrace_depth;
27 static unsigned long is_setup;
28 static DEFINE_MUTEX(start_mutex);
31 0 - use performance monitoring hardware if available
32 1 - use the timer int mechanism regardless
36 int oprofile_setup(void)
40 mutex_lock(&start_mutex);
42 if ((err = alloc_cpu_buffers()))
45 if ((err = alloc_event_buffer()))
48 if (oprofile_ops.setup && (err = oprofile_ops.setup()))
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.
56 if (oprofile_ops.sync_start) {
57 int sync_ret = oprofile_ops.sync_start();
70 if ((err = sync_start()))
75 mutex_unlock(&start_mutex);
79 if (oprofile_ops.shutdown)
80 oprofile_ops.shutdown();
86 mutex_unlock(&start_mutex);
91 /* Actually start profiling (echo 1>/dev/oprofile/enable) */
92 int oprofile_start(void)
96 mutex_lock(&start_mutex);
103 if (oprofile_started)
106 oprofile_reset_stats();
108 if ((err = oprofile_ops.start()))
111 oprofile_started = 1;
113 mutex_unlock(&start_mutex);
118 /* echo 0>/dev/oprofile/enable */
119 void oprofile_stop(void)
121 mutex_lock(&start_mutex);
122 if (!oprofile_started)
125 oprofile_started = 0;
126 /* wake up the daemon to read what remains */
127 wake_up_buffer_waiter();
129 mutex_unlock(&start_mutex);
133 void oprofile_shutdown(void)
135 mutex_lock(&start_mutex);
136 if (oprofile_ops.sync_stop) {
137 int sync_ret = oprofile_ops.sync_stop();
150 if (oprofile_ops.shutdown)
151 oprofile_ops.shutdown();
155 mutex_unlock(&start_mutex);
159 int oprofile_set_backtrace(unsigned long val)
163 mutex_lock(&start_mutex);
165 if (oprofile_started) {
170 if (!oprofile_ops.backtrace) {
175 oprofile_backtrace_depth = val;
178 mutex_unlock(&start_mutex);
182 static int __init oprofile_init(void)
186 err = buffer_sync_init();
190 err = oprofile_arch_init(&oprofile_ops);
192 if (err < 0 || timer) {
193 printk(KERN_INFO "oprofile: using timer interrupt.\n");
194 oprofile_timer_init(&oprofile_ops);
197 err = oprofilefs_register();
199 oprofile_arch_exit();
200 buffer_sync_cleanup();
207 static void __exit oprofile_exit(void)
209 oprofilefs_unregister();
210 oprofile_arch_exit();
211 buffer_sync_cleanup();
215 module_init(oprofile_init);
216 module_exit(oprofile_exit);
218 module_param_named(timer, timer, int, 0644);
219 MODULE_PARM_DESC(timer, "force use of timer interrupt");
221 MODULE_LICENSE("GPL");
222 MODULE_AUTHOR("John Levon <levon@movementarian.org>");
223 MODULE_DESCRIPTION("OProfile system profiler");