x86/oprofile: Implement mux_clone()
[linux-2.6-block.git] / drivers / oprofile / oprof.c
CommitLineData
1da177e4
LT
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>
4d4036e0
JY
15#include <linux/workqueue.h>
16#include <linux/time.h>
59cc185a 17#include <asm/mutex.h>
1da177e4
LT
18
19#include "oprof.h"
20#include "event_buffer.h"
21#include "cpu_buffer.h"
22#include "buffer_sync.h"
23#include "oprofile_stats.h"
c92960fc 24
1da177e4
LT
25struct oprofile_operations oprofile_ops;
26
27unsigned long oprofile_started;
bd2172f5 28unsigned long oprofile_backtrace_depth;
4c168eaf
RR
29static unsigned long is_setup;
30static DEFINE_MUTEX(start_mutex);
1da177e4
LT
31
32/* timer
33 0 - use performance monitoring hardware if available
34 1 - use the timer int mechanism regardless
35 */
36static int timer = 0;
37
38int oprofile_setup(void)
39{
40 int err;
c92960fc 41
59cc185a 42 mutex_lock(&start_mutex);
1da177e4
LT
43
44 if ((err = alloc_cpu_buffers()))
45 goto out;
46
47 if ((err = alloc_event_buffer()))
48 goto out1;
c92960fc 49
1da177e4
LT
50 if (oprofile_ops.setup && (err = oprofile_ops.setup()))
51 goto out2;
c92960fc 52
1da177e4
LT
53 /* Note even though this starts part of the
54 * profiling overhead, it's necessary to prevent
55 * us missing task deaths and eventually oopsing
56 * when trying to process the event buffer.
57 */
1474855d
BN
58 if (oprofile_ops.sync_start) {
59 int sync_ret = oprofile_ops.sync_start();
60 switch (sync_ret) {
61 case 0:
62 goto post_sync;
63 case 1:
64 goto do_generic;
65 case -1:
66 goto out3;
67 default:
68 goto out3;
69 }
70 }
71do_generic:
1da177e4
LT
72 if ((err = sync_start()))
73 goto out3;
74
1474855d 75post_sync:
1da177e4 76 is_setup = 1;
59cc185a 77 mutex_unlock(&start_mutex);
1da177e4 78 return 0;
c92960fc 79
1da177e4
LT
80out3:
81 if (oprofile_ops.shutdown)
82 oprofile_ops.shutdown();
83out2:
84 free_event_buffer();
85out1:
86 free_cpu_buffers();
87out:
59cc185a 88 mutex_unlock(&start_mutex);
1da177e4
LT
89 return err;
90}
91
4d4036e0
JY
92#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
93
a5659d17
RR
94static void switch_worker(struct work_struct *work);
95static DECLARE_DELAYED_WORK(switch_work, switch_worker);
96
4d4036e0
JY
97static void start_switch_worker(void)
98{
a5659d17
RR
99 if (oprofile_ops.switch_events)
100 schedule_delayed_work(&switch_work, oprofile_time_slice);
101}
102
103static void stop_switch_worker(void)
104{
105 cancel_delayed_work_sync(&switch_work);
4d4036e0
JY
106}
107
108static void switch_worker(struct work_struct *work)
109{
110 if (!oprofile_ops.switch_events())
111 start_switch_worker();
112}
113
a5659d17
RR
114/* User inputs in ms, converts to jiffies */
115int oprofile_set_timeout(unsigned long val_msec)
116{
117 int err = 0;
118 unsigned long time_slice;
119
120 mutex_lock(&start_mutex);
121
122 if (oprofile_started) {
123 err = -EBUSY;
124 goto out;
125 }
126
127 if (!oprofile_ops.switch_events) {
128 err = -EINVAL;
129 goto out;
130 }
131
132 time_slice = msecs_to_jiffies(val_msec);
133 if (time_slice == MAX_JIFFY_OFFSET) {
134 err = -EINVAL;
135 goto out;
136 }
137
138 oprofile_time_slice = time_slice;
139
140out:
141 mutex_unlock(&start_mutex);
142 return err;
143
144}
145
146#else
147
148static inline void start_switch_worker(void) { }
149static inline void stop_switch_worker(void) { }
150
4d4036e0 151#endif
1da177e4
LT
152
153/* Actually start profiling (echo 1>/dev/oprofile/enable) */
154int oprofile_start(void)
155{
156 int err = -EINVAL;
c92960fc 157
59cc185a 158 mutex_lock(&start_mutex);
6a18037d 159
1da177e4
LT
160 if (!is_setup)
161 goto out;
162
c92960fc
RR
163 err = 0;
164
1da177e4
LT
165 if (oprofile_started)
166 goto out;
c92960fc 167
1da177e4
LT
168 oprofile_reset_stats();
169
170 if ((err = oprofile_ops.start()))
171 goto out;
172
a5659d17 173 start_switch_worker();
4d4036e0 174
1da177e4
LT
175 oprofile_started = 1;
176out:
59cc185a 177 mutex_unlock(&start_mutex);
1da177e4
LT
178 return err;
179}
180
c92960fc 181
1da177e4
LT
182/* echo 0>/dev/oprofile/enable */
183void oprofile_stop(void)
184{
59cc185a 185 mutex_lock(&start_mutex);
1da177e4
LT
186 if (!oprofile_started)
187 goto out;
188 oprofile_ops.stop();
189 oprofile_started = 0;
4d4036e0 190
a5659d17 191 stop_switch_worker();
4d4036e0 192
1da177e4
LT
193 /* wake up the daemon to read what remains */
194 wake_up_buffer_waiter();
195out:
59cc185a 196 mutex_unlock(&start_mutex);
1da177e4
LT
197}
198
199
200void oprofile_shutdown(void)
201{
59cc185a 202 mutex_lock(&start_mutex);
1474855d
BN
203 if (oprofile_ops.sync_stop) {
204 int sync_ret = oprofile_ops.sync_stop();
205 switch (sync_ret) {
206 case 0:
207 goto post_sync;
208 case 1:
209 goto do_generic;
210 default:
211 goto post_sync;
212 }
213 }
214do_generic:
1da177e4 215 sync_stop();
1474855d 216post_sync:
1da177e4
LT
217 if (oprofile_ops.shutdown)
218 oprofile_ops.shutdown();
219 is_setup = 0;
220 free_event_buffer();
221 free_cpu_buffers();
59cc185a 222 mutex_unlock(&start_mutex);
1da177e4
LT
223}
224
1da177e4
LT
225int oprofile_set_backtrace(unsigned long val)
226{
227 int err = 0;
228
59cc185a 229 mutex_lock(&start_mutex);
1da177e4
LT
230
231 if (oprofile_started) {
232 err = -EBUSY;
233 goto out;
234 }
235
236 if (!oprofile_ops.backtrace) {
237 err = -EINVAL;
238 goto out;
239 }
240
bd2172f5 241 oprofile_backtrace_depth = val;
1da177e4
LT
242
243out:
59cc185a 244 mutex_unlock(&start_mutex);
1da177e4
LT
245 return err;
246}
247
248static int __init oprofile_init(void)
249{
250 int err;
251
252 err = oprofile_arch_init(&oprofile_ops);
253
254 if (err < 0 || timer) {
255 printk(KERN_INFO "oprofile: using timer interrupt.\n");
256 oprofile_timer_init(&oprofile_ops);
257 }
258
259 err = oprofilefs_register();
4c50d9ea 260 if (err)
1da177e4
LT
261 oprofile_arch_exit();
262
263 return err;
264}
265
266
267static void __exit oprofile_exit(void)
268{
269 oprofilefs_unregister();
270 oprofile_arch_exit();
271}
272
c92960fc 273
1da177e4
LT
274module_init(oprofile_init);
275module_exit(oprofile_exit);
276
277module_param_named(timer, timer, int, 0644);
278MODULE_PARM_DESC(timer, "force use of timer interrupt");
c92960fc 279
1da177e4
LT
280MODULE_LICENSE("GPL");
281MODULE_AUTHOR("John Levon <levon@movementarian.org>");
282MODULE_DESCRIPTION("OProfile system profiler");