oprofile: Implement performance counter multiplexing
[linux-2.6.git] / drivers / oprofile / oprofile_files.c
1 /**
2  * @file oprofile_files.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/fs.h>
11 #include <linux/oprofile.h>
12 #include <linux/jiffies.h>
13
14 #include "event_buffer.h"
15 #include "oprofile_stats.h"
16 #include "oprof.h"
17
18 #define BUFFER_SIZE_DEFAULT             131072
19 #define CPU_BUFFER_SIZE_DEFAULT         8192
20 #define BUFFER_WATERSHED_DEFAULT        32768   /* FIXME: tune */
21
22 unsigned long oprofile_buffer_size;
23 unsigned long oprofile_cpu_buffer_size;
24 unsigned long oprofile_buffer_watershed;
25
26 #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
27
28 static ssize_t timeout_read(struct file *file, char __user *buf,
29                 size_t count, loff_t *offset)
30 {
31         return oprofilefs_ulong_to_user(jiffies_to_msecs(timeout_jiffies),
32                                 buf, count, offset);
33 }
34
35
36 static ssize_t timeout_write(struct file *file, char const __user *buf,
37                 size_t count, loff_t *offset)
38 {
39         unsigned long val;
40         int retval;
41
42         if (*offset)
43                 return -EINVAL;
44
45         retval = oprofilefs_ulong_from_user(&val, buf, count);
46         if (retval)
47                 return retval;
48
49         retval = oprofile_set_timeout(val);
50
51         if (retval)
52                 return retval;
53         return count;
54 }
55
56
57 static const struct file_operations timeout_fops = {
58         .read           = timeout_read,
59         .write          = timeout_write,
60 };
61
62 #endif
63
64
65 static ssize_t depth_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
66 {
67         return oprofilefs_ulong_to_user(oprofile_backtrace_depth, buf, count,
68                                         offset);
69 }
70
71
72 static ssize_t depth_write(struct file *file, char const __user *buf, size_t count, loff_t *offset)
73 {
74         unsigned long val;
75         int retval;
76
77         if (*offset)
78                 return -EINVAL;
79
80         retval = oprofilefs_ulong_from_user(&val, buf, count);
81         if (retval)
82                 return retval;
83
84         retval = oprofile_set_backtrace(val);
85
86         if (retval)
87                 return retval;
88         return count;
89 }
90
91
92 static const struct file_operations depth_fops = {
93         .read           = depth_read,
94         .write          = depth_write
95 };
96
97
98 static ssize_t pointer_size_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
99 {
100         return oprofilefs_ulong_to_user(sizeof(void *), buf, count, offset);
101 }
102
103
104 static const struct file_operations pointer_size_fops = {
105         .read           = pointer_size_read,
106 };
107
108
109 static ssize_t cpu_type_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
110 {
111         return oprofilefs_str_to_user(oprofile_ops.cpu_type, buf, count, offset);
112 }
113
114
115 static const struct file_operations cpu_type_fops = {
116         .read           = cpu_type_read,
117 };
118
119
120 static ssize_t enable_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
121 {
122         return oprofilefs_ulong_to_user(oprofile_started, buf, count, offset);
123 }
124
125
126 static ssize_t enable_write(struct file *file, char const __user *buf, size_t count, loff_t *offset)
127 {
128         unsigned long val;
129         int retval;
130
131         if (*offset)
132                 return -EINVAL;
133
134         retval = oprofilefs_ulong_from_user(&val, buf, count);
135         if (retval)
136                 return retval;
137
138         if (val)
139                 retval = oprofile_start();
140         else
141                 oprofile_stop();
142
143         if (retval)
144                 return retval;
145         return count;
146 }
147
148
149 static const struct file_operations enable_fops = {
150         .read           = enable_read,
151         .write          = enable_write,
152 };
153
154
155 static ssize_t dump_write(struct file *file, char const __user *buf, size_t count, loff_t *offset)
156 {
157         wake_up_buffer_waiter();
158         return count;
159 }
160
161
162 static const struct file_operations dump_fops = {
163         .write          = dump_write,
164 };
165
166 void oprofile_create_files(struct super_block *sb, struct dentry *root)
167 {
168         /* reinitialize default values */
169         oprofile_buffer_size =          BUFFER_SIZE_DEFAULT;
170         oprofile_cpu_buffer_size =      CPU_BUFFER_SIZE_DEFAULT;
171         oprofile_buffer_watershed =     BUFFER_WATERSHED_DEFAULT;
172
173         oprofilefs_create_file(sb, root, "enable", &enable_fops);
174         oprofilefs_create_file_perm(sb, root, "dump", &dump_fops, 0666);
175         oprofilefs_create_file(sb, root, "buffer", &event_buffer_fops);
176         oprofilefs_create_ulong(sb, root, "buffer_size", &oprofile_buffer_size);
177         oprofilefs_create_ulong(sb, root, "buffer_watershed", &oprofile_buffer_watershed);
178         oprofilefs_create_ulong(sb, root, "cpu_buffer_size", &oprofile_cpu_buffer_size);
179         oprofilefs_create_file(sb, root, "cpu_type", &cpu_type_fops);
180         oprofilefs_create_file(sb, root, "backtrace_depth", &depth_fops);
181         oprofilefs_create_file(sb, root, "pointer_size", &pointer_size_fops);
182 #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
183         oprofilefs_create_file(sb, root, "time_slice", &timeout_fops);
184 #endif
185         oprofile_create_stats_files(sb, root);
186         if (oprofile_ops.create_files)
187                 oprofile_ops.create_files(sb, root);
188 }