blob: f9bda64fcd1b62771880d5d823b53f797cb9c7f0 [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{
Robert Richter1b294f52009-07-09 14:56:25 +0200110 if (oprofile_ops.switch_events())
111 return;
112
113 atomic_inc(&oprofile_stats.multiplex_counter);
114 start_switch_worker();
Jason Yeh4d4036e2009-07-08 13:49:38 +0200115}
116
Jason Yeh4d4036e2009-07-08 13:49:38 +0200117/* User inputs in ms, converts to jiffies */
118int oprofile_set_timeout(unsigned long val_msec)
119{
120 int err = 0;
Robert Richter2051cad2009-07-15 15:44:18 +0200121 unsigned long time_slice;
Jason Yeh4d4036e2009-07-08 13:49:38 +0200122
123 mutex_lock(&start_mutex);
124
125 if (oprofile_started) {
126 err = -EBUSY;
127 goto out;
128 }
129
130 if (!oprofile_ops.switch_events) {
131 err = -EINVAL;
132 goto out;
133 }
134
Robert Richter2051cad2009-07-15 15:44:18 +0200135 time_slice = msecs_to_jiffies(val_msec);
136 if (time_slice == MAX_JIFFY_OFFSET) {
137 err = -EINVAL;
138 goto out;
139 }
140
Robert Richterafe1b50f2009-07-15 15:19:29 +0200141 oprofile_time_slice = time_slice;
Jason Yeh4d4036e2009-07-08 13:49:38 +0200142
143out:
144 mutex_unlock(&start_mutex);
145 return err;
146
147}
148
Robert Richtera5659d12009-06-19 16:45:34 +0200149#else
150
151static inline void start_switch_worker(void) { }
152static inline void stop_switch_worker(void) { }
153
Jason Yeh4d4036e2009-07-08 13:49:38 +0200154#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Robert Richtera5659d12009-06-19 16:45:34 +0200156/* Actually start profiling (echo 1>/dev/oprofile/enable) */
157int oprofile_start(void)
158{
159 int err = -EINVAL;
160
161 mutex_lock(&start_mutex);
162
163 if (!is_setup)
164 goto out;
165
166 err = 0;
167
168 if (oprofile_started)
169 goto out;
170
171 oprofile_reset_stats();
172
173 if ((err = oprofile_ops.start()))
174 goto out;
175
176 start_switch_worker();
177
178 oprofile_started = 1;
179out:
180 mutex_unlock(&start_mutex);
181 return err;
182}
183
184
185/* echo 0>/dev/oprofile/enable */
186void oprofile_stop(void)
187{
188 mutex_lock(&start_mutex);
189 if (!oprofile_started)
190 goto out;
191 oprofile_ops.stop();
192 oprofile_started = 0;
193
194 stop_switch_worker();
195
196 /* wake up the daemon to read what remains */
197 wake_up_buffer_waiter();
198out:
199 mutex_unlock(&start_mutex);
200}
201
202
203void oprofile_shutdown(void)
204{
205 mutex_lock(&start_mutex);
206 if (oprofile_ops.sync_stop) {
207 int sync_ret = oprofile_ops.sync_stop();
208 switch (sync_ret) {
209 case 0:
210 goto post_sync;
211 case 1:
212 goto do_generic;
213 default:
214 goto post_sync;
215 }
216 }
217do_generic:
218 sync_stop();
219post_sync:
220 if (oprofile_ops.shutdown)
221 oprofile_ops.shutdown();
222 is_setup = 0;
223 free_event_buffer();
224 free_cpu_buffers();
225 mutex_unlock(&start_mutex);
226}
227
Robert Richter7df01d92010-10-04 21:09:36 +0200228int oprofile_set_ulong(unsigned long *addr, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Robert Richter7df01d92010-10-04 21:09:36 +0200230 int err = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Markus Armbruster59cc1852006-06-25 05:47:33 -0700232 mutex_lock(&start_mutex);
Robert Richter7df01d92010-10-04 21:09:36 +0200233 if (!oprofile_started) {
234 *addr = val;
235 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
Markus Armbruster59cc1852006-06-25 05:47:33 -0700237 mutex_unlock(&start_mutex);
Robert Richter7df01d92010-10-04 21:09:36 +0200238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return err;
240}
241
242static int __init oprofile_init(void)
243{
244 int err;
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 err = oprofile_arch_init(&oprofile_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (err < 0 || timer) {
248 printk(KERN_INFO "oprofile: using timer interrupt.\n");
Martin Schwidefskybc078e42010-03-02 16:01:10 +0100249 err = oprofile_timer_init(&oprofile_ops);
250 if (err)
Will Deacon979048e2010-08-29 14:51:59 -0400251 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Will Deacon979048e2010-08-29 14:51:59 -0400253 return oprofilefs_register();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256
257static void __exit oprofile_exit(void)
258{
Martin Schwidefskybc078e42010-03-02 16:01:10 +0100259 oprofile_timer_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 oprofilefs_unregister();
261 oprofile_arch_exit();
262}
263
Robert Richterc92960f2008-09-05 17:12:36 +0200264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265module_init(oprofile_init);
266module_exit(oprofile_exit);
267
268module_param_named(timer, timer, int, 0644);
269MODULE_PARM_DESC(timer, "force use of timer interrupt");
Robert Richterc92960f2008-09-05 17:12:36 +0200270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271MODULE_LICENSE("GPL");
272MODULE_AUTHOR("John Levon <levon@movementarian.org>");
273MODULE_DESCRIPTION("OProfile system profiler");