Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[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  * Modified by Aravind Menon for Xen
10  * These modifications are:
11  * Copyright (C) 2005 Hewlett-Packard Co.       
12  */
13
14 #include <linux/fs.h>
15 #include <linux/oprofile.h>
16 #include <asm/uaccess.h>
17 #include <linux/ctype.h>
18
19 #include "event_buffer.h"
20 #include "oprofile_stats.h"
21 #include "oprof.h"
22
23 unsigned long fs_buffer_size = 131072;
24 unsigned long fs_cpu_buffer_size = 8192;
25 unsigned long fs_buffer_watershed = 32768; /* FIXME: tune */
26
27 static ssize_t depth_read(struct file * file, char __user * buf, size_t count, loff_t * offset)
28 {
29         return oprofilefs_ulong_to_user(backtrace_depth, buf, count, offset);
30 }
31
32
33 static ssize_t depth_write(struct file * file, char const __user * buf, size_t count, loff_t * offset)
34 {
35         unsigned long val;
36         int retval;
37
38         if (*offset)
39                 return -EINVAL;
40
41         retval = oprofilefs_ulong_from_user(&val, buf, count);
42         if (retval)
43                 return retval;
44
45         retval = oprofile_set_backtrace(val);
46
47         if (retval)
48                 return retval;
49         return count;
50 }
51
52
53 static struct file_operations depth_fops = {
54         .read           = depth_read,
55         .write          = depth_write
56 };
57
58  
59 static ssize_t pointer_size_read(struct file * file, char __user * buf, size_t count, loff_t * offset)
60 {
61         return oprofilefs_ulong_to_user(sizeof(void *), buf, count, offset);
62 }
63
64
65 static struct file_operations pointer_size_fops = {
66         .read           = pointer_size_read,
67 };
68
69
70 static ssize_t cpu_type_read(struct file * file, char __user * buf, size_t count, loff_t * offset)
71 {
72         return oprofilefs_str_to_user(oprofile_ops.cpu_type, buf, count, offset);
73 }
74  
75  
76 static struct file_operations cpu_type_fops = {
77         .read           = cpu_type_read,
78 };
79  
80  
81 static ssize_t enable_read(struct file * file, char __user * buf, size_t count, loff_t * offset)
82 {
83         return oprofilefs_ulong_to_user(oprofile_started, buf, count, offset);
84 }
85
86
87 static ssize_t enable_write(struct file * file, char const __user * buf, size_t count, loff_t * offset)
88 {
89         unsigned long val;
90         int retval;
91
92         if (*offset)
93                 return -EINVAL;
94
95         retval = oprofilefs_ulong_from_user(&val, buf, count);
96         if (retval)
97                 return retval;
98  
99         if (val)
100                 retval = oprofile_start();
101         else
102                 oprofile_stop();
103
104         if (retval)
105                 return retval;
106         return count;
107 }
108
109  
110 static struct file_operations enable_fops = {
111         .read           = enable_read,
112         .write          = enable_write,
113 };
114
115
116 static ssize_t dump_write(struct file * file, char const __user * buf, size_t count, loff_t * offset)
117 {
118         wake_up_buffer_waiter();
119         return count;
120 }
121
122
123 static struct file_operations dump_fops = {
124         .write          = dump_write,
125 };
126
127 #ifdef CONFIG_XEN
128
129 #define TMPBUFSIZE 512
130
131 static unsigned int adomains = 0;
132 static int active_domains[MAX_OPROF_DOMAINS + 1];
133 static DEFINE_MUTEX(adom_mutex);
134
135 static ssize_t adomain_write(struct file * file, char const __user * buf, 
136                              size_t count, loff_t * offset)
137 {
138         char *tmpbuf;
139         char *startp, *endp;
140         int i;
141         unsigned long val;
142         ssize_t retval = count;
143         
144         if (*offset)
145                 return -EINVAL; 
146         if (count > TMPBUFSIZE - 1)
147                 return -EINVAL;
148
149         if (!(tmpbuf = kmalloc(TMPBUFSIZE, GFP_KERNEL)))
150                 return -ENOMEM;
151
152         if (copy_from_user(tmpbuf, buf, count)) {
153                 kfree(tmpbuf);
154                 return -EFAULT;
155         }
156         tmpbuf[count] = 0;
157
158         mutex_lock(&adom_mutex);
159
160         startp = tmpbuf;
161         /* Parse one more than MAX_OPROF_DOMAINS, for easy error checking */
162         for (i = 0; i <= MAX_OPROF_DOMAINS; i++) {
163                 val = simple_strtoul(startp, &endp, 0);
164                 if (endp == startp)
165                         break;
166                 while (ispunct(*endp) || isspace(*endp))
167                         endp++;
168                 active_domains[i] = val;
169                 if (active_domains[i] != val)
170                         /* Overflow, force error below */
171                         i = MAX_OPROF_DOMAINS + 1;
172                 startp = endp;
173         }
174         /* Force error on trailing junk */
175         adomains = *startp ? MAX_OPROF_DOMAINS + 1 : i;
176
177         kfree(tmpbuf);
178
179         if (adomains > MAX_OPROF_DOMAINS
180             || oprofile_set_active(active_domains, adomains)) {
181                 adomains = 0;
182                 retval = -EINVAL;
183         }
184
185         mutex_unlock(&adom_mutex);
186         return retval;
187 }
188
189 static ssize_t adomain_read(struct file * file, char __user * buf, 
190                             size_t count, loff_t * offset)
191 {
192         char * tmpbuf;
193         size_t len;
194         int i;
195         ssize_t retval;
196
197         if (!(tmpbuf = kmalloc(TMPBUFSIZE, GFP_KERNEL)))
198                 return -ENOMEM;
199
200         mutex_lock(&adom_mutex);
201
202         len = 0;
203         for (i = 0; i < adomains; i++)
204                 len += snprintf(tmpbuf + len,
205                                 len < TMPBUFSIZE ? TMPBUFSIZE - len : 0,
206                                 "%u ", active_domains[i]);
207         WARN_ON(len > TMPBUFSIZE);
208         if (len != 0 && len <= TMPBUFSIZE)
209                 tmpbuf[len-1] = '\n';
210
211         mutex_unlock(&adom_mutex);
212
213         retval = simple_read_from_buffer(buf, count, offset, tmpbuf, len);
214
215         kfree(tmpbuf);
216         return retval;
217 }
218
219
220 static struct file_operations active_domain_ops = {
221         .read           = adomain_read,
222         .write          = adomain_write,
223 };
224
225 static unsigned int pdomains = 0;
226 static int passive_domains[MAX_OPROF_DOMAINS];
227 static DEFINE_MUTEX(pdom_mutex);
228
229 static ssize_t pdomain_write(struct file * file, char const __user * buf, 
230                              size_t count, loff_t * offset)
231 {
232         char *tmpbuf;
233         char *startp, *endp;
234         int i;
235         unsigned long val;
236         ssize_t retval = count;
237         
238         if (*offset)
239                 return -EINVAL; 
240         if (count > TMPBUFSIZE - 1)
241                 return -EINVAL;
242
243         if (!(tmpbuf = kmalloc(TMPBUFSIZE, GFP_KERNEL)))
244                 return -ENOMEM;
245
246         if (copy_from_user(tmpbuf, buf, count)) {
247                 kfree(tmpbuf);
248                 return -EFAULT;
249         }
250         tmpbuf[count] = 0;
251
252         mutex_lock(&pdom_mutex);
253
254         startp = tmpbuf;
255         /* Parse one more than MAX_OPROF_DOMAINS, for easy error checking */
256         for (i = 0; i <= MAX_OPROF_DOMAINS; i++) {
257                 val = simple_strtoul(startp, &endp, 0);
258                 if (endp == startp)
259                         break;
260                 while (ispunct(*endp) || isspace(*endp))
261                         endp++;
262                 passive_domains[i] = val;
263                 if (passive_domains[i] != val)
264                         /* Overflow, force error below */
265                         i = MAX_OPROF_DOMAINS + 1;
266                 startp = endp;
267         }
268         /* Force error on trailing junk */
269         pdomains = *startp ? MAX_OPROF_DOMAINS + 1 : i;
270
271         kfree(tmpbuf);
272
273         if (pdomains > MAX_OPROF_DOMAINS
274             || oprofile_set_passive(passive_domains, pdomains)) {
275                 pdomains = 0;
276                 retval = -EINVAL;
277         }
278
279         mutex_unlock(&pdom_mutex);
280         return retval;
281 }
282
283 static ssize_t pdomain_read(struct file * file, char __user * buf, 
284                             size_t count, loff_t * offset)
285 {
286         char * tmpbuf;
287         size_t len;
288         int i;
289         ssize_t retval;
290
291         if (!(tmpbuf = kmalloc(TMPBUFSIZE, GFP_KERNEL)))
292                 return -ENOMEM;
293
294         mutex_lock(&pdom_mutex);
295
296         len = 0;
297         for (i = 0; i < pdomains; i++)
298                 len += snprintf(tmpbuf + len,
299                                 len < TMPBUFSIZE ? TMPBUFSIZE - len : 0,
300                                 "%u ", passive_domains[i]);
301         WARN_ON(len > TMPBUFSIZE);
302         if (len != 0 && len <= TMPBUFSIZE)
303                 tmpbuf[len-1] = '\n';
304
305         mutex_unlock(&pdom_mutex);
306
307         retval = simple_read_from_buffer(buf, count, offset, tmpbuf, len);
308
309         kfree(tmpbuf);
310         return retval;
311 }
312
313 static struct file_operations passive_domain_ops = {
314         .read           = pdomain_read,
315         .write          = pdomain_write,
316 };
317
318 #endif /* CONFIG_XEN */
319
320 void oprofile_create_files(struct super_block * sb, struct dentry * root)
321 {
322         oprofilefs_create_file(sb, root, "enable", &enable_fops);
323         oprofilefs_create_file_perm(sb, root, "dump", &dump_fops, 0666);
324 #ifdef CONFIG_XEN
325         oprofilefs_create_file(sb, root, "active_domains", &active_domain_ops);
326         oprofilefs_create_file(sb, root, "passive_domains", &passive_domain_ops);
327 #endif
328         oprofilefs_create_file(sb, root, "buffer", &event_buffer_fops);
329         oprofilefs_create_ulong(sb, root, "buffer_size", &fs_buffer_size);
330         oprofilefs_create_ulong(sb, root, "buffer_watershed", &fs_buffer_watershed);
331         oprofilefs_create_ulong(sb, root, "cpu_buffer_size", &fs_cpu_buffer_size);
332         oprofilefs_create_file(sb, root, "cpu_type", &cpu_type_fops); 
333         oprofilefs_create_file(sb, root, "backtrace_depth", &depth_fops);
334         oprofilefs_create_file(sb, root, "pointer_size", &pointer_size_fops);
335         oprofile_create_stats_files(sb, root);
336         if (oprofile_ops.create_files)
337                 oprofile_ops.create_files(sb, root);
338 }