blob: 7bc64af7cf990562507e67bc72735fae50296b9a [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
Jason Yeh4d4036e2009-07-08 13:49:38 +020032#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
33
34static void switch_worker(struct work_struct *work);
35static DECLARE_DELAYED_WORK(switch_work, switch_worker);
36unsigned long timeout_jiffies;
37#define MULTIPLEXING_TIMER_DEFAULT 1
38
39#endif
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/* timer
42 0 - use performance monitoring hardware if available
43 1 - use the timer int mechanism regardless
44 */
45static int timer = 0;
46
47int oprofile_setup(void)
48{
49 int err;
Robert Richterc92960f2008-09-05 17:12:36 +020050
Markus Armbruster59cc1852006-06-25 05:47:33 -070051 mutex_lock(&start_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 if ((err = alloc_cpu_buffers()))
54 goto out;
55
56 if ((err = alloc_event_buffer()))
57 goto out1;
Robert Richterc92960f2008-09-05 17:12:36 +020058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (oprofile_ops.setup && (err = oprofile_ops.setup()))
60 goto out2;
Robert Richterc92960f2008-09-05 17:12:36 +020061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 /* Note even though this starts part of the
63 * profiling overhead, it's necessary to prevent
64 * us missing task deaths and eventually oopsing
65 * when trying to process the event buffer.
66 */
Bob Nelson14748552007-07-20 21:39:53 +020067 if (oprofile_ops.sync_start) {
68 int sync_ret = oprofile_ops.sync_start();
69 switch (sync_ret) {
70 case 0:
71 goto post_sync;
72 case 1:
73 goto do_generic;
74 case -1:
75 goto out3;
76 default:
77 goto out3;
78 }
79 }
80do_generic:
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if ((err = sync_start()))
82 goto out3;
83
Bob Nelson14748552007-07-20 21:39:53 +020084post_sync:
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 is_setup = 1;
Markus Armbruster59cc1852006-06-25 05:47:33 -070086 mutex_unlock(&start_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return 0;
Robert Richterc92960f2008-09-05 17:12:36 +020088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089out3:
90 if (oprofile_ops.shutdown)
91 oprofile_ops.shutdown();
92out2:
93 free_event_buffer();
94out1:
95 free_cpu_buffers();
96out:
Markus Armbruster59cc1852006-06-25 05:47:33 -070097 mutex_unlock(&start_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return err;
99}
100
Jason Yeh4d4036e2009-07-08 13:49:38 +0200101#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
102
103static void start_switch_worker(void)
104{
105 schedule_delayed_work(&switch_work, timeout_jiffies);
106}
107
108static void switch_worker(struct work_struct *work)
109{
110 if (!oprofile_ops.switch_events())
111 start_switch_worker();
112}
113
114#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/* Actually start profiling (echo 1>/dev/oprofile/enable) */
117int oprofile_start(void)
118{
119 int err = -EINVAL;
Robert Richterc92960f2008-09-05 17:12:36 +0200120
Markus Armbruster59cc1852006-06-25 05:47:33 -0700121 mutex_lock(&start_mutex);
Robert Richter6a180372008-10-16 15:01:40 +0200122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (!is_setup)
124 goto out;
125
Robert Richterc92960f2008-09-05 17:12:36 +0200126 err = 0;
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (oprofile_started)
129 goto out;
Robert Richterc92960f2008-09-05 17:12:36 +0200130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 oprofile_reset_stats();
132
133 if ((err = oprofile_ops.start()))
134 goto out;
135
Jason Yeh4d4036e2009-07-08 13:49:38 +0200136#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
137 if (oprofile_ops.switch_events)
138 start_switch_worker();
139#endif
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 oprofile_started = 1;
142out:
Markus Armbruster59cc1852006-06-25 05:47:33 -0700143 mutex_unlock(&start_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return err;
145}
146
Robert Richterc92960f2008-09-05 17:12:36 +0200147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/* echo 0>/dev/oprofile/enable */
149void oprofile_stop(void)
150{
Markus Armbruster59cc1852006-06-25 05:47:33 -0700151 mutex_lock(&start_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (!oprofile_started)
153 goto out;
154 oprofile_ops.stop();
155 oprofile_started = 0;
Jason Yeh4d4036e2009-07-08 13:49:38 +0200156
157#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
158 cancel_delayed_work_sync(&switch_work);
159#endif
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 /* wake up the daemon to read what remains */
162 wake_up_buffer_waiter();
163out:
Markus Armbruster59cc1852006-06-25 05:47:33 -0700164 mutex_unlock(&start_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167
168void oprofile_shutdown(void)
169{
Markus Armbruster59cc1852006-06-25 05:47:33 -0700170 mutex_lock(&start_mutex);
Bob Nelson14748552007-07-20 21:39:53 +0200171 if (oprofile_ops.sync_stop) {
172 int sync_ret = oprofile_ops.sync_stop();
173 switch (sync_ret) {
174 case 0:
175 goto post_sync;
176 case 1:
177 goto do_generic;
178 default:
179 goto post_sync;
180 }
181 }
182do_generic:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 sync_stop();
Bob Nelson14748552007-07-20 21:39:53 +0200184post_sync:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (oprofile_ops.shutdown)
186 oprofile_ops.shutdown();
187 is_setup = 0;
188 free_event_buffer();
189 free_cpu_buffers();
Markus Armbruster59cc1852006-06-25 05:47:33 -0700190 mutex_unlock(&start_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Jason Yeh4d4036e2009-07-08 13:49:38 +0200193#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
194
195/* User inputs in ms, converts to jiffies */
196int oprofile_set_timeout(unsigned long val_msec)
197{
198 int err = 0;
199
200 mutex_lock(&start_mutex);
201
202 if (oprofile_started) {
203 err = -EBUSY;
204 goto out;
205 }
206
207 if (!oprofile_ops.switch_events) {
208 err = -EINVAL;
209 goto out;
210 }
211
212 timeout_jiffies = msecs_to_jiffies(val_msec);
213 if (timeout_jiffies == MAX_JIFFY_OFFSET)
214 timeout_jiffies = msecs_to_jiffies(MULTIPLEXING_TIMER_DEFAULT);
215
216out:
217 mutex_unlock(&start_mutex);
218 return err;
219
220}
221
222#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224int oprofile_set_backtrace(unsigned long val)
225{
226 int err = 0;
227
Markus Armbruster59cc1852006-06-25 05:47:33 -0700228 mutex_lock(&start_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 if (oprofile_started) {
231 err = -EBUSY;
232 goto out;
233 }
234
235 if (!oprofile_ops.backtrace) {
236 err = -EINVAL;
237 goto out;
238 }
239
Robert Richterbd2172f2008-12-16 16:19:54 +0100240 oprofile_backtrace_depth = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242out:
Markus Armbruster59cc1852006-06-25 05:47:33 -0700243 mutex_unlock(&start_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return err;
245}
246
Jason Yeh4d4036e2009-07-08 13:49:38 +0200247#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
248
249static void __init oprofile_multiplexing_init(void)
250{
251 timeout_jiffies = msecs_to_jiffies(MULTIPLEXING_TIMER_DEFAULT);
252}
253
254#endif
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256static int __init oprofile_init(void)
257{
258 int err;
259
Jason Yeh4d4036e2009-07-08 13:49:38 +0200260#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
261 oprofile_multiplexing_init();
262#endif
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 err = oprofile_arch_init(&oprofile_ops);
265
266 if (err < 0 || timer) {
267 printk(KERN_INFO "oprofile: using timer interrupt.\n");
268 oprofile_timer_init(&oprofile_ops);
269 }
270
271 err = oprofilefs_register();
Robert Richter4c50d9e2009-01-22 14:14:14 +0100272 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 oprofile_arch_exit();
274
275 return err;
276}
277
278
279static void __exit oprofile_exit(void)
280{
281 oprofilefs_unregister();
282 oprofile_arch_exit();
283}
284
Robert Richterc92960f2008-09-05 17:12:36 +0200285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286module_init(oprofile_init);
287module_exit(oprofile_exit);
288
289module_param_named(timer, timer, int, 0644);
290MODULE_PARM_DESC(timer, "force use of timer interrupt");
Robert Richterc92960f2008-09-05 17:12:36 +0200291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292MODULE_LICENSE("GPL");
293MODULE_AUTHOR("John Levon <levon@movementarian.org>");
294MODULE_DESCRIPTION("OProfile system profiler");