blob: 2b7ae366ceb151f1c197fd116b251d55fdeb3b74 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
2 * @file event_buffer.c
3 *
4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
6 *
7 * @author John Levon <levon@movementarian.org>
8 *
9 * This is the global event buffer that the user-space
10 * daemon reads from. The event buffer is an untyped array
11 * of unsigned longs. Entries are prefixed by the
12 * escape value ESCAPE_CODE followed by an identifying code.
13 */
14
15#include <linux/vmalloc.h>
16#include <linux/oprofile.h>
17#include <linux/sched.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080018#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/dcookies.h>
20#include <linux/fs.h>
21#include <asm/uaccess.h>
Robert Richter6a180372008-10-16 15:01:40 +020022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "oprof.h"
24#include "event_buffer.h"
25#include "oprofile_stats.h"
26
Markus Armbruster59cc1852006-06-25 05:47:33 -070027DEFINE_MUTEX(buffer_mutex);
Robert Richter6a180372008-10-16 15:01:40 +020028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static unsigned long buffer_opened;
30static DECLARE_WAIT_QUEUE_HEAD(buffer_wait);
Robert Richter25ad2912008-09-05 17:12:36 +020031static unsigned long *event_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static unsigned long buffer_size;
33static unsigned long buffer_watershed;
34static size_t buffer_pos;
Markus Armbruster59cc1852006-06-25 05:47:33 -070035/* atomic_t because wait_event checks it outside of buffer_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static atomic_t buffer_ready = ATOMIC_INIT(0);
37
38/* Add an entry to the event buffer. When we
39 * get near to the end we wake up the process
40 * sleeping on the read() of the file.
41 */
42void add_event_entry(unsigned long value)
43{
44 if (buffer_pos == buffer_size) {
45 atomic_inc(&oprofile_stats.event_lost_overflow);
46 return;
47 }
48
49 event_buffer[buffer_pos] = value;
50 if (++buffer_pos == buffer_size - buffer_watershed) {
51 atomic_set(&buffer_ready, 1);
52 wake_up(&buffer_wait);
53 }
54}
55
56
57/* Wake up the waiting process if any. This happens
58 * on "echo 0 >/dev/oprofile/enable" so the daemon
59 * processes the data remaining in the event buffer.
60 */
61void wake_up_buffer_waiter(void)
62{
Markus Armbruster59cc1852006-06-25 05:47:33 -070063 mutex_lock(&buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 atomic_set(&buffer_ready, 1);
65 wake_up(&buffer_wait);
Markus Armbruster59cc1852006-06-25 05:47:33 -070066 mutex_unlock(&buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Robert Richter6a180372008-10-16 15:01:40 +020069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070int alloc_event_buffer(void)
71{
72 int err = -ENOMEM;
Jiri Kosina4dfc8962007-03-28 18:12:34 +020073 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Jiri Kosina4dfc8962007-03-28 18:12:34 +020075 spin_lock_irqsave(&oprofilefs_lock, flags);
Robert Richterbd2172f2008-12-16 16:19:54 +010076 buffer_size = oprofile_buffer_size;
77 buffer_watershed = oprofile_buffer_watershed;
Jiri Kosina4dfc8962007-03-28 18:12:34 +020078 spin_unlock_irqrestore(&oprofilefs_lock, flags);
Robert Richter6a180372008-10-16 15:01:40 +020079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (buffer_watershed >= buffer_size)
81 return -EINVAL;
Robert Richter6a180372008-10-16 15:01:40 +020082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 event_buffer = vmalloc(sizeof(unsigned long) * buffer_size);
84 if (!event_buffer)
Robert Richter6a180372008-10-16 15:01:40 +020085 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 err = 0;
88out:
89 return err;
90}
91
92
93void free_event_buffer(void)
94{
95 vfree(event_buffer);
Carl Lovef4156d12008-08-11 17:25:43 +100096
97 event_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Robert Richter6a180372008-10-16 15:01:40 +0200100
Robert Richter25ad2912008-09-05 17:12:36 +0200101static int event_buffer_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 int err = -EPERM;
104
105 if (!capable(CAP_SYS_ADMIN))
106 return -EPERM;
107
Nick Piggincae042a2008-10-23 16:25:54 +0200108 if (test_and_set_bit_lock(0, &buffer_opened))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return -EBUSY;
110
111 /* Register as a user of dcookies
112 * to ensure they persist for the lifetime of
113 * the open event file
114 */
115 err = -EINVAL;
116 file->private_data = dcookie_register();
117 if (!file->private_data)
118 goto out;
Robert Richter6a180372008-10-16 15:01:40 +0200119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if ((err = oprofile_setup()))
121 goto fail;
122
123 /* NB: the actual start happens from userspace
124 * echo 1 >/dev/oprofile/enable
125 */
Robert Richter6a180372008-10-16 15:01:40 +0200126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return 0;
128
129fail:
130 dcookie_unregister(file->private_data);
131out:
Nick Piggincae042a2008-10-23 16:25:54 +0200132 __clear_bit_unlock(0, &buffer_opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return err;
134}
135
136
Robert Richter25ad2912008-09-05 17:12:36 +0200137static int event_buffer_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 oprofile_stop();
140 oprofile_shutdown();
141 dcookie_unregister(file->private_data);
142 buffer_pos = 0;
143 atomic_set(&buffer_ready, 0);
Nick Piggincae042a2008-10-23 16:25:54 +0200144 __clear_bit_unlock(0, &buffer_opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return 0;
146}
147
148
Robert Richter25ad2912008-09-05 17:12:36 +0200149static ssize_t event_buffer_read(struct file *file, char __user *buf,
150 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 int retval = -EINVAL;
153 size_t const max = buffer_size * sizeof(unsigned long);
154
155 /* handling partial reads is more trouble than it's worth */
156 if (count != max || *offset)
157 return -EINVAL;
158
159 wait_event_interruptible(buffer_wait, atomic_read(&buffer_ready));
160
161 if (signal_pending(current))
162 return -EINTR;
163
164 /* can't currently happen */
165 if (!atomic_read(&buffer_ready))
166 return -EAGAIN;
167
Markus Armbruster59cc1852006-06-25 05:47:33 -0700168 mutex_lock(&buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 atomic_set(&buffer_ready, 0);
171
172 retval = -EFAULT;
173
174 count = buffer_pos * sizeof(unsigned long);
Robert Richter6a180372008-10-16 15:01:40 +0200175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (copy_to_user(buf, event_buffer, count))
177 goto out;
178
179 retval = count;
180 buffer_pos = 0;
Robert Richter6a180372008-10-16 15:01:40 +0200181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182out:
Markus Armbruster59cc1852006-06-25 05:47:33 -0700183 mutex_unlock(&buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return retval;
185}
Robert Richter6a180372008-10-16 15:01:40 +0200186
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800187const struct file_operations event_buffer_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 .open = event_buffer_open,
189 .release = event_buffer_release,
190 .read = event_buffer_read,
191};