ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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
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>
15 #include <asm/semaphore.h>
16
17 #include "oprof.h"
18 #include "event_buffer.h"
19 #include "cpu_buffer.h"
20 #include "buffer_sync.h"
21 #include "oprofile_stats.h"
22  
23 struct oprofile_operations * oprofile_ops;
24 unsigned long oprofile_started;
25 static unsigned long is_setup;
26 static DECLARE_MUTEX(start_sem);
27
28 /* timer
29    0 - use performance monitoring hardware if available
30    1 - use the timer int mechanism regardless
31  */
32 static int timer = 0;
33
34 int oprofile_setup(void)
35 {
36         int err;
37  
38         down(&start_sem);
39
40         if ((err = alloc_cpu_buffers()))
41                 goto out;
42
43         if ((err = alloc_event_buffer()))
44                 goto out1;
45  
46         if (oprofile_ops->setup && (err = oprofile_ops->setup()))
47                 goto out2;
48  
49         /* Note even though this starts part of the
50          * profiling overhead, it's necessary to prevent
51          * us missing task deaths and eventually oopsing
52          * when trying to process the event buffer.
53          */
54         if ((err = sync_start()))
55                 goto out3;
56
57         is_setup = 1;
58         up(&start_sem);
59         return 0;
60  
61 out3:
62         if (oprofile_ops->shutdown)
63                 oprofile_ops->shutdown();
64 out2:
65         free_event_buffer();
66 out1:
67         free_cpu_buffers();
68 out:
69         up(&start_sem);
70         return err;
71 }
72
73
74 /* Actually start profiling (echo 1>/dev/oprofile/enable) */
75 int oprofile_start(void)
76 {
77         int err = -EINVAL;
78  
79         down(&start_sem);
80  
81         if (!is_setup)
82                 goto out;
83
84         err = 0; 
85  
86         if (oprofile_started)
87                 goto out;
88  
89         oprofile_reset_stats();
90
91         if ((err = oprofile_ops->start()))
92                 goto out;
93
94         oprofile_started = 1;
95 out:
96         up(&start_sem); 
97         return err;
98 }
99
100  
101 /* echo 0>/dev/oprofile/enable */
102 void oprofile_stop(void)
103 {
104         down(&start_sem);
105         if (!oprofile_started)
106                 goto out;
107         oprofile_ops->stop();
108         oprofile_started = 0;
109         /* wake up the daemon to read what remains */
110         wake_up_buffer_waiter();
111 out:
112         up(&start_sem);
113 }
114
115
116 void oprofile_shutdown(void)
117 {
118         down(&start_sem);
119         sync_stop();
120         if (oprofile_ops->shutdown)
121                 oprofile_ops->shutdown(); 
122         is_setup = 0;
123         free_event_buffer();
124         free_cpu_buffers();
125         up(&start_sem);
126 }
127
128
129 extern void timer_init(struct oprofile_operations ** ops);
130
131
132 static int __init oprofile_init(void)
133 {
134         /* Architecture must fill in the interrupt ops and the
135          * logical CPU type, or we can fall back to the timer
136          * interrupt profiler.
137          */
138         int err = oprofile_arch_init(&oprofile_ops);
139
140         if (err == -ENODEV || timer) {
141                 timer_init(&oprofile_ops);
142                 err = 0;
143         } else if (err) {
144                 goto out;
145         }
146
147         if (!oprofile_ops->cpu_type) {
148                 printk(KERN_ERR "oprofile: cpu_type not set !\n");
149                 err = -EFAULT;
150         } else {
151                 err = oprofilefs_register();
152         }
153  
154         if (err)
155                 goto out_exit;
156 out:
157         return err;
158 out_exit:
159         oprofile_arch_exit();
160         goto out;
161 }
162
163
164 static void __exit oprofile_exit(void)
165 {
166         oprofilefs_unregister();
167         oprofile_arch_exit();
168 }
169
170  
171 module_init(oprofile_init);
172 module_exit(oprofile_exit);
173
174 module_param_named(timer, timer, int, 0644);
175 MODULE_PARM_DESC(timer, "force use of timer interrupt");
176  
177 MODULE_LICENSE("GPL");
178 MODULE_AUTHOR("John Levon <levon@movementarian.org>");
179 MODULE_DESCRIPTION("OProfile system profiler");