blob: ee2cfce358b9810b5d1f06e28d18e86e1dec0a98 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
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>
Jason Yeh4d4036e2009-07-08 13:49:38 +020012#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#include "event_buffer.h"
15#include "oprofile_stats.h"
16#include "oprof.h"
Robert Richter6a180372008-10-16 15:01:40 +020017
Robert Richterbd2172f2008-12-16 16:19:54 +010018#define BUFFER_SIZE_DEFAULT 131072
19#define CPU_BUFFER_SIZE_DEFAULT 8192
20#define BUFFER_WATERSHED_DEFAULT 32768 /* FIXME: tune */
Robert Richter16422a62009-07-15 15:44:18 +020021#define TIME_SLICE_DEFAULT 1
Robert Richter37ca5eb2008-12-09 16:56:01 +010022
Robert Richterbd2172f2008-12-16 16:19:54 +010023unsigned long oprofile_buffer_size;
24unsigned long oprofile_cpu_buffer_size;
25unsigned long oprofile_buffer_watershed;
Robert Richterafe1b50f2009-07-15 15:19:29 +020026unsigned long oprofile_time_slice;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Jason Yeh4d4036e2009-07-08 13:49:38 +020028#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
29
30static ssize_t timeout_read(struct file *file, char __user *buf,
31 size_t count, loff_t *offset)
32{
Robert Richterafe1b50f2009-07-15 15:19:29 +020033 return oprofilefs_ulong_to_user(jiffies_to_msecs(oprofile_time_slice),
34 buf, count, offset);
Jason Yeh4d4036e2009-07-08 13:49:38 +020035}
36
37
38static ssize_t timeout_write(struct file *file, char const __user *buf,
39 size_t count, loff_t *offset)
40{
41 unsigned long val;
42 int retval;
43
44 if (*offset)
45 return -EINVAL;
46
47 retval = oprofilefs_ulong_from_user(&val, buf, count);
Robert Richter913050b2011-12-19 16:38:30 +010048 if (retval <= 0)
Jason Yeh4d4036e2009-07-08 13:49:38 +020049 return retval;
50
51 retval = oprofile_set_timeout(val);
52
53 if (retval)
54 return retval;
55 return count;
56}
57
58
59static const struct file_operations timeout_fops = {
60 .read = timeout_read,
61 .write = timeout_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +020062 .llseek = default_llseek,
Jason Yeh4d4036e2009-07-08 13:49:38 +020063};
64
65#endif
66
67
Robert Richter25ad29132008-09-05 17:12:36 +020068static ssize_t depth_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Robert Richterbd2172f2008-12-16 16:19:54 +010070 return oprofilefs_ulong_to_user(oprofile_backtrace_depth, buf, count,
71 offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
74
Robert Richter25ad29132008-09-05 17:12:36 +020075static ssize_t depth_write(struct file *file, char const __user *buf, size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 unsigned long val;
78 int retval;
79
80 if (*offset)
81 return -EINVAL;
82
Robert Richter7df01d92010-10-04 21:09:36 +020083 if (!oprofile_ops.backtrace)
84 return -EINVAL;
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 retval = oprofilefs_ulong_from_user(&val, buf, count);
Robert Richter913050b2011-12-19 16:38:30 +010087 if (retval <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return retval;
89
Robert Richter7df01d92010-10-04 21:09:36 +020090 retval = oprofile_set_ulong(&oprofile_backtrace_depth, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if (retval)
92 return retval;
Robert Richter7df01d92010-10-04 21:09:36 +020093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return count;
95}
96
97
Arjan van de Vend54b1fd2007-02-12 00:55:34 -080098static const struct file_operations depth_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 .read = depth_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200100 .write = depth_write,
101 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
Robert Richter6a180372008-10-16 15:01:40 +0200104
Robert Richter25ad29132008-09-05 17:12:36 +0200105static ssize_t pointer_size_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 return oprofilefs_ulong_to_user(sizeof(void *), buf, count, offset);
108}
109
110
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800111static const struct file_operations pointer_size_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 .read = pointer_size_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200113 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
116
Robert Richter25ad29132008-09-05 17:12:36 +0200117static ssize_t cpu_type_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 return oprofilefs_str_to_user(oprofile_ops.cpu_type, buf, count, offset);
120}
Robert Richter6a180372008-10-16 15:01:40 +0200121
122
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800123static const struct file_operations cpu_type_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .read = cpu_type_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200125 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
Robert Richter6a180372008-10-16 15:01:40 +0200127
128
Robert Richter25ad29132008-09-05 17:12:36 +0200129static ssize_t enable_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 return oprofilefs_ulong_to_user(oprofile_started, buf, count, offset);
132}
133
134
Robert Richter25ad29132008-09-05 17:12:36 +0200135static ssize_t enable_write(struct file *file, char const __user *buf, size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 unsigned long val;
138 int retval;
139
140 if (*offset)
141 return -EINVAL;
Robert Richter4c168ea2008-09-24 11:08:52 +0200142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 retval = oprofilefs_ulong_from_user(&val, buf, count);
Robert Richter913050b2011-12-19 16:38:30 +0100144 if (retval <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return retval;
Robert Richter6a180372008-10-16 15:01:40 +0200146
Robert Richter913050b2011-12-19 16:38:30 +0100147 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (val)
149 retval = oprofile_start();
150 else
151 oprofile_stop();
152
153 if (retval)
154 return retval;
155 return count;
156}
157
Robert Richter6a180372008-10-16 15:01:40 +0200158
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800159static const struct file_operations enable_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 .read = enable_read,
161 .write = enable_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200162 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163};
164
165
Robert Richter25ad29132008-09-05 17:12:36 +0200166static ssize_t dump_write(struct file *file, char const __user *buf, size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 wake_up_buffer_waiter();
169 return count;
170}
171
172
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800173static const struct file_operations dump_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 .write = dump_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200175 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176};
Robert Richter6a180372008-10-16 15:01:40 +0200177
Al Viroa9e599e2013-07-19 15:47:29 +0400178void oprofile_create_files(struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Robert Richter37ca5eb2008-12-09 16:56:01 +0100180 /* reinitialize default values */
Robert Richterbd2172f2008-12-16 16:19:54 +0100181 oprofile_buffer_size = BUFFER_SIZE_DEFAULT;
182 oprofile_cpu_buffer_size = CPU_BUFFER_SIZE_DEFAULT;
183 oprofile_buffer_watershed = BUFFER_WATERSHED_DEFAULT;
Robert Richter16422a62009-07-15 15:44:18 +0200184 oprofile_time_slice = msecs_to_jiffies(TIME_SLICE_DEFAULT);
Robert Richter37ca5eb2008-12-09 16:56:01 +0100185
Al Viro6af4ea02013-07-19 16:10:36 +0400186 oprofilefs_create_file(root, "enable", &enable_fops);
187 oprofilefs_create_file_perm(root, "dump", &dump_fops, 0666);
188 oprofilefs_create_file(root, "buffer", &event_buffer_fops);
189 oprofilefs_create_ulong(root, "buffer_size", &oprofile_buffer_size);
190 oprofilefs_create_ulong(root, "buffer_watershed", &oprofile_buffer_watershed);
191 oprofilefs_create_ulong(root, "cpu_buffer_size", &oprofile_cpu_buffer_size);
192 oprofilefs_create_file(root, "cpu_type", &cpu_type_fops);
193 oprofilefs_create_file(root, "backtrace_depth", &depth_fops);
194 oprofilefs_create_file(root, "pointer_size", &pointer_size_fops);
Jason Yeh4d4036e2009-07-08 13:49:38 +0200195#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
Al Viro6af4ea02013-07-19 16:10:36 +0400196 oprofilefs_create_file(root, "time_slice", &timeout_fops);
Jason Yeh4d4036e2009-07-08 13:49:38 +0200197#endif
Al Viro40437c72013-07-19 15:54:56 +0400198 oprofile_create_stats_files(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (oprofile_ops.create_files)
Al Viroef7bca12013-07-19 15:52:42 +0400200 oprofile_ops.create_files(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}