Merge to Fedora kernel-2.6.17-1.2187_FC5 patched with stable patch-2.6.17.13-vs2...
[linux-2.6.git] / drivers / oprofile / oprof.c
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  * Modified by Aravind Menon for Xen
10  * These modifications are:
11  * Copyright (C) 2005 Hewlett-Packard Co.
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/oprofile.h>
18 #include <linux/moduleparam.h>
19 #include <asm/semaphore.h>
20
21 #include "oprof.h"
22 #include "event_buffer.h"
23 #include "cpu_buffer.h"
24 #include "buffer_sync.h"
25 #include "oprofile_stats.h"
26
27 struct oprofile_operations oprofile_ops;
28
29 unsigned long oprofile_started;
30 unsigned long backtrace_depth;
31 static unsigned long is_setup;
32 static DECLARE_MUTEX(start_sem);
33
34 /* timer
35    0 - use performance monitoring hardware if available
36    1 - use the timer int mechanism regardless
37  */
38 static int timer = 0;
39
40 #ifdef CONFIG_XEN
41 int oprofile_set_active(int active_domains[], unsigned int adomains)
42 {
43         int err;
44
45         if (!oprofile_ops.set_active)
46                 return -EINVAL;
47
48         down(&start_sem);
49         err = oprofile_ops.set_active(active_domains, adomains);
50         up(&start_sem);
51         return err;
52 }
53
54 int oprofile_set_passive(int passive_domains[], unsigned int pdomains)
55 {
56         int err;
57
58         if (!oprofile_ops.set_passive)
59                 return -EINVAL;
60
61         down(&start_sem);
62         err = oprofile_ops.set_passive(passive_domains, pdomains);
63         up(&start_sem);
64         return err;
65 }
66 #endif
67
68 int oprofile_setup(void)
69 {
70         int err;
71  
72         down(&start_sem);
73
74         if ((err = alloc_cpu_buffers()))
75                 goto out;
76
77         if ((err = alloc_event_buffer()))
78                 goto out1;
79  
80         if (oprofile_ops.setup && (err = oprofile_ops.setup()))
81                 goto out2;
82  
83         /* Note even though this starts part of the
84          * profiling overhead, it's necessary to prevent
85          * us missing task deaths and eventually oopsing
86          * when trying to process the event buffer.
87          */
88         if ((err = sync_start()))
89                 goto out3;
90
91         is_setup = 1;
92         up(&start_sem);
93         return 0;
94  
95 out3:
96         if (oprofile_ops.shutdown)
97                 oprofile_ops.shutdown();
98 out2:
99         free_event_buffer();
100 out1:
101         free_cpu_buffers();
102 out:
103         up(&start_sem);
104         return err;
105 }
106
107
108 /* Actually start profiling (echo 1>/dev/oprofile/enable) */
109 int oprofile_start(void)
110 {
111         int err = -EINVAL;
112  
113         down(&start_sem);
114  
115         if (!is_setup)
116                 goto out;
117
118         err = 0; 
119  
120         if (oprofile_started)
121                 goto out;
122  
123         oprofile_reset_stats();
124
125         if ((err = oprofile_ops.start()))
126                 goto out;
127
128         oprofile_started = 1;
129 out:
130         up(&start_sem); 
131         return err;
132 }
133
134  
135 /* echo 0>/dev/oprofile/enable */
136 void oprofile_stop(void)
137 {
138         down(&start_sem);
139         if (!oprofile_started)
140                 goto out;
141         oprofile_ops.stop();
142         oprofile_started = 0;
143         /* wake up the daemon to read what remains */
144         wake_up_buffer_waiter();
145 out:
146         up(&start_sem);
147 }
148
149
150 void oprofile_shutdown(void)
151 {
152         down(&start_sem);
153         sync_stop();
154         if (oprofile_ops.shutdown)
155                 oprofile_ops.shutdown();
156         is_setup = 0;
157         free_event_buffer();
158         free_cpu_buffers();
159         up(&start_sem);
160 }
161
162
163 int oprofile_set_backtrace(unsigned long val)
164 {
165         int err = 0;
166
167         down(&start_sem);
168
169         if (oprofile_started) {
170                 err = -EBUSY;
171                 goto out;
172         }
173
174         if (!oprofile_ops.backtrace) {
175                 err = -EINVAL;
176                 goto out;
177         }
178
179         backtrace_depth = val;
180
181 out:
182         up(&start_sem);
183         return err;
184 }
185
186 static int __init oprofile_init(void)
187 {
188         int err;
189
190         err = oprofile_arch_init(&oprofile_ops);
191
192         if (err < 0 || timer) {
193                 printk(KERN_INFO "oprofile: using timer interrupt.\n");
194                 oprofile_timer_init(&oprofile_ops);
195         }
196
197         err = oprofilefs_register();
198         if (err)
199                 oprofile_arch_exit();
200
201         return err;
202 }
203
204
205 static void __exit oprofile_exit(void)
206 {
207         oprofilefs_unregister();
208         oprofile_arch_exit();
209 }
210
211  
212 module_init(oprofile_init);
213 module_exit(oprofile_exit);
214
215 module_param_named(timer, timer, int, 0644);
216 MODULE_PARM_DESC(timer, "force use of timer interrupt");
217  
218 MODULE_LICENSE("GPL");
219 MODULE_AUTHOR("John Levon <levon@movementarian.org>");
220 MODULE_DESCRIPTION("OProfile system profiler");