MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / oprofile / oprof.c
bloba85d33dec8c42b9a636eac62260aeaafa8ec4ca0
1 /**
2 * @file oprof.c
4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
7 * @author John Levon <levon@movementarian.org>
8 */
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>
17 #include "oprof.h"
18 #include "event_buffer.h"
19 #include "cpu_buffer.h"
20 #include "buffer_sync.h"
21 #include "oprofile_stats.h"
23 struct oprofile_operations * oprofile_ops;
24 unsigned long oprofile_started;
25 static unsigned long is_setup;
26 static DECLARE_MUTEX(start_sem);
28 /* timer
29 0 - use performance monitoring hardware if available
30 1 - use the timer int mechanism regardless
32 static int timer = 0;
34 int oprofile_setup(void)
36 int err;
38 down(&start_sem);
40 if ((err = alloc_cpu_buffers()))
41 goto out;
43 if ((err = alloc_event_buffer()))
44 goto out1;
46 if (oprofile_ops->setup && (err = oprofile_ops->setup()))
47 goto out2;
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.
54 if ((err = sync_start()))
55 goto out3;
57 is_setup = 1;
58 up(&start_sem);
59 return 0;
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;
74 /* Actually start profiling (echo 1>/dev/oprofile/enable) */
75 int oprofile_start(void)
77 int err = -EINVAL;
79 down(&start_sem);
81 if (!is_setup)
82 goto out;
84 err = 0;
86 if (oprofile_started)
87 goto out;
89 oprofile_reset_stats();
91 if ((err = oprofile_ops->start()))
92 goto out;
94 oprofile_started = 1;
95 out:
96 up(&start_sem);
97 return err;
101 /* echo 0>/dev/oprofile/enable */
102 void oprofile_stop(void)
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);
116 void oprofile_shutdown(void)
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);
129 extern void timer_init(struct oprofile_operations ** ops);
132 static int __init oprofile_init(void)
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.
138 int err = oprofile_arch_init(&oprofile_ops);
140 if (err == -ENODEV || timer) {
141 timer_init(&oprofile_ops);
142 err = 0;
143 } else if (err) {
144 goto out;
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();
154 if (err)
155 goto out_exit;
156 out:
157 return err;
158 out_exit:
159 oprofile_arch_exit();
160 goto out;
164 static void __exit oprofile_exit(void)
166 oprofilefs_unregister();
167 oprofile_arch_exit();
171 module_init(oprofile_init);
172 module_exit(oprofile_exit);
174 module_param_named(timer, timer, int, 0644);
175 MODULE_PARM_DESC(timer, "force use of timer interrupt");
177 MODULE_LICENSE("GPL");
178 MODULE_AUTHOR("John Levon <levon@movementarian.org>");
179 MODULE_DESCRIPTION("OProfile system profiler");