blob: a48294a8ebe837bf1804c5958a699f20f5b6e28a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
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>
Jason Yeh4d4036e2009-07-08 13:49:38 +020015#include <linux/workqueue.h>
16#include <linux/time.h>
Markus Armbruster59cc1852006-06-25 05:47:33 -070017#include <asm/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include "oprof.h"
20#include "event_buffer.h"
21#include "cpu_buffer.h"
22#include "buffer_sync.h"
23#include "oprofile_stats.h"
Robert Richterc92960f2008-09-05 17:12:36 +020024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025struct oprofile_operations oprofile_ops;
26
27unsigned long oprofile_started;
Robert Richterbd2172f2008-12-16 16:19:54 +010028unsigned long oprofile_backtrace_depth;
Robert Richter4c168ea2008-09-24 11:08:52 +020029static unsigned long is_setup;
30static DEFINE_MUTEX(start_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
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;
Robert Richterc92960f2008-09-05 17:12:36 +020041
Markus Armbruster59cc1852006-06-25 05:47:33 -070042 mutex_lock(&start_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 if ((err = alloc_cpu_buffers()))
45 goto out;
46
47 if ((err = alloc_event_buffer()))
48 goto out1;
Robert Richterc92960f2008-09-05 17:12:36 +020049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 if (oprofile_ops.setup && (err = oprofile_ops.setup()))
51 goto out2;
Robert Richterc92960f2008-09-05 17:12:36 +020052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 /* 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 */
Bob Nelson14748552007-07-20 21:39:53 +020058 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:
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if ((err = sync_start()))
73 goto out3;
74
Bob Nelson14748552007-07-20 21:39:53 +020075post_sync:
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 is_setup = 1;
Markus Armbruster59cc1852006-06-25 05:47:33 -070077 mutex_unlock(&start_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return 0;
Robert Richterc92960f2008-09-05 17:12:36 +020079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080out3:
81 if (oprofile_ops.shutdown)
82 oprofile_ops.shutdown();
83out2:
84 free_event_buffer();
85out1:
86 free_cpu_buffers();
87out:
Markus Armbruster59cc1852006-06-25 05:47:33 -070088 mutex_unlock(&start_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return err;
90}
91
Jason Yeh4d4036e2009-07-08 13:49:38 +020092#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
93
Robert Richtera5659d12009-06-19 16:45:34 +020094static void switch_worker(struct work_struct *work);
95static DECLARE_DELAYED_WORK(switch_work, switch_worker);
96
Jason Yeh4d4036e2009-07-08 13:49:38 +020097static void start_switch_worker(void)
98{
Robert Richtera5659d12009-06-19 16:45:34 +020099 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);
Jason Yeh4d4036e2009-07-08 13:49:38 +0200106}
107
108static void switch_worker(struct work_struct *work)
109{
110 if (!oprofile_ops.switch_events())
111 start_switch_worker();
112}
113
Jason Yeh4d4036e2009-07-08 13:49:38 +0200114/* User inputs in ms, converts to jiffies */
115int oprofile_set_timeout(unsigned long val_msec)
116{
117 int err = 0;
Robert Richter2051cad2009-07-15 15:44:18 +0200118 unsigned long time_slice;
Jason Yeh4d4036e2009-07-08 13:49:38 +0200119
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
Robert Richter2051cad2009-07-15 15:44:18 +0200132 time_slice = msecs_to_jiffies(val_msec);
133 if (time_slice == MAX_JIFFY_OFFSET) {
134 err = -EINVAL;
135 goto out;
136 }
137
Robert Richterafe1b50f2009-07-15 15:19:29 +0200138 oprofile_time_slice = time_slice;
Jason Yeh4d4036e2009-07-08 13:49:38 +0200139
140out:
141 mutex_unlock(&start_mutex);
142 return err;
143
144}
145
Robert Richtera5659d12009-06-19 16:45:34 +0200146#else
147
148static inline void start_switch_worker(void) { }
149static inline void stop_switch_worker(void) { }
150
Jason Yeh4d4036e2009-07-08 13:49:38 +0200151#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Robert Richtera5659d12009-06-19 16:45:34 +0200153/* Actually start profiling (echo 1>/dev/oprofile/enable) */
154int oprofile_start(void)
155{
156 int err = -EINVAL;
157
158 mutex_lock(&start_mutex);
159
160 if (!is_setup)
161 goto out;
162
163 err = 0;
164
165 if (oprofile_started)
166 goto out;
167
168 oprofile_reset_stats();
169
170 if ((err = oprofile_ops.start()))
171 goto out;
172
173 start_switch_worker();
174
175 oprofile_started = 1;
176out:
177 mutex_unlock(&start_mutex);
178 return err;
179}
180
181
182/* echo 0>/dev/oprofile/enable */
183void oprofile_stop(void)
184{
185 mutex_lock(&start_mutex);
186 if (!oprofile_started)
187 goto out;
188 oprofile_ops.stop();
189 oprofile_started = 0;
190
191 stop_switch_worker();
192
193 /* wake up the daemon to read what remains */
194 wake_up_buffer_waiter();
195out:
196 mutex_unlock(&start_mutex);
197}
198
199
200void oprofile_shutdown(void)
201{
202 mutex_lock(&start_mutex);
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:
215 sync_stop();
216post_sync:
217 if (oprofile_ops.shutdown)
218 oprofile_ops.shutdown();
219 is_setup = 0;
220 free_event_buffer();
221 free_cpu_buffers();
222 mutex_unlock(&start_mutex);
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225int oprofile_set_backtrace(unsigned long val)
226{
227 int err = 0;
228
Markus Armbruster59cc1852006-06-25 05:47:33 -0700229 mutex_lock(&start_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
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
Robert Richterbd2172f2008-12-16 16:19:54 +0100241 oprofile_backtrace_depth = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243out:
Markus Armbruster59cc1852006-06-25 05:47:33 -0700244 mutex_unlock(&start_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return err;
246}
247
248static int __init oprofile_init(void)
249{
250 int err;
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 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();
Robert Richter4c50d9ea2009-01-22 14:14:14 +0100260 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 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
Robert Richterc92960f2008-09-05 17:12:36 +0200273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274module_init(oprofile_init);
275module_exit(oprofile_exit);
276
277module_param_named(timer, timer, int, 0644);
278MODULE_PARM_DESC(timer, "force use of timer interrupt");
Robert Richterc92960f2008-09-05 17:12:36 +0200279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280MODULE_LICENSE("GPL");
281MODULE_AUTHOR("John Levon <levon@movementarian.org>");
282MODULE_DESCRIPTION("OProfile system profiler");