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 / 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 #define TMPBUFSIZE 512
128
129 #ifdef CONFIG_XEN
130 static unsigned int adomains = 0;
131 static int active_domains[MAX_OPROF_DOMAINS + 1];
132 static DEFINE_MUTEX(adom_mutex);
133
134 static ssize_t adomain_write(struct file * file, char const __user * buf, 
135                              size_t count, loff_t * offset)
136 {
137         char *tmpbuf;
138         char *startp, *endp;
139         int i;
140         unsigned long val;
141         ssize_t retval = count;
142         
143         if (*offset)
144                 return -EINVAL; 
145         if (count > TMPBUFSIZE - 1)
146                 return -EINVAL;
147
148         if (!(tmpbuf = kmalloc(TMPBUFSIZE, GFP_KERNEL)))
149                 return -ENOMEM;
150
151         if (copy_from_user(tmpbuf, buf, count)) {
152                 kfree(tmpbuf);
153                 return -EFAULT;
154         }
155         tmpbuf[count] = 0;
156
157         mutex_lock(&adom_mutex);
158
159         startp = tmpbuf;
160         /* Parse one more than MAX_OPROF_DOMAINS, for easy error checking */
161         for (i = 0; i <= MAX_OPROF_DOMAINS; i++) {
162                 val = simple_strtoul(startp, &endp, 0);
163                 if (endp == startp)
164                         break;
165                 while (ispunct(*endp) || isspace(*endp))
166                         endp++;
167                 active_domains[i] = val;
168                 if (active_domains[i] != val)
169                         /* Overflow, force error below */
170                         i = MAX_OPROF_DOMAINS + 1;
171                 startp = endp;
172         }
173         /* Force error on trailing junk */
174         adomains = *startp ? MAX_OPROF_DOMAINS + 1 : i;
175
176         kfree(tmpbuf);
177
178         if (adomains > MAX_OPROF_DOMAINS
179             || oprofile_set_active(active_domains, adomains)) {
180                 adomains = 0;
181                 retval = -EINVAL;
182         }
183
184         mutex_unlock(&adom_mutex);
185         return retval;
186 }
187
188 static ssize_t adomain_read(struct file * file, char __user * buf, 
189                             size_t count, loff_t * offset)
190 {
191         char * tmpbuf;
192         size_t len;
193         int i;
194         ssize_t retval;
195
196         if (!(tmpbuf = kmalloc(TMPBUFSIZE, GFP_KERNEL)))
197                 return -ENOMEM;
198
199         mutex_lock(&adom_mutex);
200
201         len = 0;
202         for (i = 0; i < adomains; i++)
203                 len += snprintf(tmpbuf + len,
204                                 len < TMPBUFSIZE ? TMPBUFSIZE - len : 0,
205                                 "%u ", active_domains[i]);
206         WARN_ON(len > TMPBUFSIZE);
207         if (len != 0 && len <= TMPBUFSIZE)
208                 tmpbuf[len-1] = '\n';
209
210         mutex_unlock(&adom_mutex);
211
212         retval = simple_read_from_buffer(buf, count, offset, tmpbuf, len);
213
214         kfree(tmpbuf);
215         return retval;
216 }
217
218
219 static struct file_operations active_domain_ops = {
220         .read           = adomain_read,
221         .write          = adomain_write,
222 };
223
224 static unsigned int pdomains = 0;
225 static int passive_domains[MAX_OPROF_DOMAINS];
226 static DEFINE_MUTEX(pdom_mutex);
227
228 static ssize_t pdomain_write(struct file * file, char const __user * buf, 
229                              size_t count, loff_t * offset)
230 {
231         char *tmpbuf;
232         char *startp, *endp;
233         int i;
234         unsigned long val;
235         ssize_t retval = count;
236         
237         if (*offset)
238                 return -EINVAL; 
239         if (count > TMPBUFSIZE - 1)
240                 return -EINVAL;
241
242         if (!(tmpbuf = kmalloc(TMPBUFSIZE, GFP_KERNEL)))
243                 return -ENOMEM;
244
245         if (copy_from_user(tmpbuf, buf, count)) {
246                 kfree(tmpbuf);
247                 return -EFAULT;
248         }
249         tmpbuf[count] = 0;
250
251         mutex_lock(&pdom_mutex);
252
253         startp = tmpbuf;
254         /* Parse one more than MAX_OPROF_DOMAINS, for easy error checking */
255         for (i = 0; i <= MAX_OPROF_DOMAINS; i++) {
256                 val = simple_strtoul(startp, &endp, 0);
257                 if (endp == startp)
258                         break;
259                 while (ispunct(*endp) || isspace(*endp))
260                         endp++;
261                 passive_domains[i] = val;
262                 if (passive_domains[i] != val)
263                         /* Overflow, force error below */
264                         i = MAX_OPROF_DOMAINS + 1;
265                 startp = endp;
266         }
267         /* Force error on trailing junk */
268         pdomains = *startp ? MAX_OPROF_DOMAINS + 1 : i;
269
270         kfree(tmpbuf);
271
272         if (pdomains > MAX_OPROF_DOMAINS
273             || oprofile_set_passive(passive_domains, pdomains)) {
274                 pdomains = 0;
275                 retval = -EINVAL;
276         }
277
278         mutex_unlock(&pdom_mutex);
279         return retval;
280 }
281
282 static ssize_t pdomain_read(struct file * file, char __user * buf, 
283                             size_t count, loff_t * offset)
284 {
285         char * tmpbuf;
286         size_t len;
287         int i;
288         ssize_t retval;
289
290         if (!(tmpbuf = kmalloc(TMPBUFSIZE, GFP_KERNEL)))
291                 return -ENOMEM;
292
293         mutex_lock(&pdom_mutex);
294
295         len = 0;
296         for (i = 0; i < pdomains; i++)
297                 len += snprintf(tmpbuf + len,
298                                 len < TMPBUFSIZE ? TMPBUFSIZE - len : 0,
299                                 "%u ", passive_domains[i]);
300         WARN_ON(len > TMPBUFSIZE);
301         if (len != 0 && len <= TMPBUFSIZE)
302                 tmpbuf[len-1] = '\n';
303
304         mutex_unlock(&pdom_mutex);
305
306         retval = simple_read_from_buffer(buf, count, offset, tmpbuf, len);
307
308         kfree(tmpbuf);
309         return retval;
310 }
311
312 static struct file_operations passive_domain_ops = {
313         .read           = pdomain_read,
314         .write          = pdomain_write,
315 };
316 #endif /* CONFIG_XEN */
317
318 void oprofile_create_files(struct super_block * sb, struct dentry * root)
319 {
320         oprofilefs_create_file(sb, root, "enable", &enable_fops);
321         oprofilefs_create_file_perm(sb, root, "dump", &dump_fops, 0666);
322 #ifdef CONFIG_XEN
323         oprofilefs_create_file(sb, root, "active_domains", &active_domain_ops);
324         oprofilefs_create_file(sb, root, "passive_domains", &passive_domain_ops);
325 #endif
326         oprofilefs_create_file(sb, root, "buffer", &event_buffer_fops);
327         oprofilefs_create_ulong(sb, root, "buffer_size", &fs_buffer_size);
328         oprofilefs_create_ulong(sb, root, "buffer_watershed", &fs_buffer_watershed);
329         oprofilefs_create_ulong(sb, root, "cpu_buffer_size", &fs_cpu_buffer_size);
330         oprofilefs_create_file(sb, root, "cpu_type", &cpu_type_fops); 
331         oprofilefs_create_file(sb, root, "backtrace_depth", &depth_fops);
332         oprofilefs_create_file(sb, root, "pointer_size", &pointer_size_fops);
333         oprofile_create_stats_files(sb, root);
334         if (oprofile_ops.create_files)
335                 oprofile_ops.create_files(sb, root);
336 }