Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / oprofile / oprof.c
blobb3f1cd6a24c151943691e074d0d693bc4d4c88f3
1 /**
2 * @file oprof.c
4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
7 * @author John Levon <levon@movementarian.org>
8 */
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/semaphore.h>
17 #include "oprof.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 backtrace_depth;
27 static unsigned long is_setup;
28 static DECLARE_MUTEX(start_sem);
30 /* timer
31 0 - use performance monitoring hardware if available
32 1 - use the timer int mechanism regardless
34 static int timer = 0;
36 int oprofile_setup(void)
38 int err;
40 down(&start_sem);
42 if ((err = alloc_cpu_buffers()))
43 goto out;
45 if ((err = alloc_event_buffer()))
46 goto out1;
48 if (oprofile_ops.setup && (err = oprofile_ops.setup()))
49 goto out2;
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 ((err = sync_start()))
57 goto out3;
59 is_setup = 1;
60 up(&start_sem);
61 return 0;
63 out3:
64 if (oprofile_ops.shutdown)
65 oprofile_ops.shutdown();
66 out2:
67 free_event_buffer();
68 out1:
69 free_cpu_buffers();
70 out:
71 up(&start_sem);
72 return err;
76 /* Actually start profiling (echo 1>/dev/oprofile/enable) */
77 int oprofile_start(void)
79 int err = -EINVAL;
81 down(&start_sem);
83 if (!is_setup)
84 goto out;
86 err = 0;
88 if (oprofile_started)
89 goto out;
91 oprofile_reset_stats();
93 if ((err = oprofile_ops.start()))
94 goto out;
96 oprofile_started = 1;
97 out:
98 up(&start_sem);
99 return err;
103 /* echo 0>/dev/oprofile/enable */
104 void oprofile_stop(void)
106 down(&start_sem);
107 if (!oprofile_started)
108 goto out;
109 oprofile_ops.stop();
110 oprofile_started = 0;
111 /* wake up the daemon to read what remains */
112 wake_up_buffer_waiter();
113 out:
114 up(&start_sem);
118 void oprofile_shutdown(void)
120 down(&start_sem);
121 sync_stop();
122 if (oprofile_ops.shutdown)
123 oprofile_ops.shutdown();
124 is_setup = 0;
125 free_event_buffer();
126 free_cpu_buffers();
127 up(&start_sem);
131 int oprofile_set_backtrace(unsigned long val)
133 int err = 0;
135 down(&start_sem);
137 if (oprofile_started) {
138 err = -EBUSY;
139 goto out;
142 if (!oprofile_ops.backtrace) {
143 err = -EINVAL;
144 goto out;
147 backtrace_depth = val;
149 out:
150 up(&start_sem);
151 return err;
154 static int __init oprofile_init(void)
156 int err;
158 err = oprofile_arch_init(&oprofile_ops);
160 if (err < 0 || timer) {
161 printk(KERN_INFO "oprofile: using timer interrupt.\n");
162 oprofile_timer_init(&oprofile_ops);
165 err = oprofilefs_register();
166 if (err)
167 oprofile_arch_exit();
169 return err;
173 static void __exit oprofile_exit(void)
175 oprofilefs_unregister();
176 oprofile_arch_exit();
180 module_init(oprofile_init);
181 module_exit(oprofile_exit);
183 module_param_named(timer, timer, int, 0644);
184 MODULE_PARM_DESC(timer, "force use of timer interrupt");
186 MODULE_LICENSE("GPL");
187 MODULE_AUTHOR("John Levon <levon@movementarian.org>");
188 MODULE_DESCRIPTION("OProfile system profiler");