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