forked from efficios/latency-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracker_debugfs.c
330 lines (269 loc) · 7.21 KB
/
tracker_debugfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* Copyright (C) 2014 Julien Desfossez <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; only
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <linux/module.h>
#include <linux/debugfs.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include "tracker_debugfs.h"
#include "latency_tracker.h"
#include "tracker_private.h"
#include "wrapper/trace-clock.h"
#define DEBUGFSNAME "latency"
static struct dentry *debugfs_root;
static struct dentry *debugfs_create_tracking_on(umode_t mode,
struct dentry *parent, int *value,
struct latency_tracker *tracker);
int latency_tracker_debugfs_setup(void)
{
debugfs_root = debugfs_create_dir(DEBUGFSNAME, NULL);
if (!debugfs_root)
goto error;
return 0;
error:
return -1;
}
void latency_tracker_debugfs_cleanup(void)
{
debugfs_remove_recursive(debugfs_root);
}
int setup_default_entries(struct latency_tracker *tracker)
{
struct dentry *dir;
dir = debugfs_create_u64("threshold", S_IRUSR|S_IWUSR,
tracker->debugfs_dir, &tracker->threshold);
if (!dir)
goto error;
dir = debugfs_create_u64("timeout", S_IRUSR|S_IWUSR,
tracker->debugfs_dir, &tracker->timeout);
if (!dir)
goto error;
dir = debugfs_create_tracking_on(S_IRUSR|S_IWUSR,
tracker->debugfs_dir, &tracker->tracking_on, tracker);
if (!dir)
goto error;
return 0;
error:
return -1;
}
static
ssize_t read_wakeup_pipe(struct file *filp, char __user *ubuf,
size_t count, loff_t *ppos)
{
struct latency_tracker *tracker = filp->private_data;
wait_event_interruptible(tracker->read_wait,
tracker->got_alert);
tracker->got_alert = false;
return 0;
}
void latency_tracker_debugfs_wakeup_pipe(struct latency_tracker *tracker)
{
uint64_t ts = trace_clock_read64();
/* Rate limiter */
if ((ts - tracker->last_wakeup_ts) < tracker->wakeup_rate_limit_ns)
return;
if (atomic_read(&tracker->wakeup_readers))
irq_work_queue(&tracker->wake_irq);
tracker->last_wakeup_ts = ts;
}
static
int open_wakeup_pipe(struct inode *inode, struct file *filp)
{
int ret;
struct latency_tracker *tracker = inode->i_private;
filp->private_data = tracker;
atomic_inc(&tracker->wakeup_readers);
ret = try_module_get(THIS_MODULE);
if (!ret)
return -1;
return 0;
}
static
int release_wakeup_pipe(struct inode *inode, struct file *filp)
{
struct latency_tracker *tracker = filp->private_data;
atomic_dec(&tracker->wakeup_readers);
module_put(THIS_MODULE);
return 0;
}
static
const struct file_operations wakeup_pipe_fops = {
.open = open_wakeup_pipe,
.release = release_wakeup_pipe,
.read = read_wakeup_pipe,
.llseek = default_llseek,
/* TODO: poll */
};
static
void irq_wake(struct irq_work *entry)
{
struct latency_tracker *tracker = container_of(entry,
struct latency_tracker, wake_irq);
tracker->got_alert = true;
wake_up_interruptible(&tracker->read_wait);
}
int latency_tracker_debugfs_setup_wakeup_pipe(struct latency_tracker *tracker)
{
init_irq_work(&tracker->wake_irq, irq_wake);
init_waitqueue_head(&tracker->read_wait);
tracker->got_alert = false;
/* FIXME: param */
tracker->wakeup_rate_limit_ns = 1000000000;
tracker->wakeup_pipe = debugfs_create_file("wakeup_pipe", S_IRUSR,
tracker->debugfs_dir, tracker, &wakeup_pipe_fops);
if (!tracker->wakeup_pipe)
return -1;
return 0;
}
static
void destroy_wakeup_pipe(struct latency_tracker *tracker)
{
if (!tracker->wakeup_pipe)
return;
irq_work_sync(&tracker->wake_irq);
debugfs_remove(tracker->wakeup_pipe);
tracker->wakeup_pipe = NULL;
}
int latency_tracker_debugfs_add_tracker(
struct latency_tracker *tracker)
{
struct dentry *dir;
int ret;
dir = debugfs_create_dir(tracker->tracker_name, debugfs_root);
if (!dir)
goto error;
tracker->debugfs_dir = dir;
ret = setup_default_entries(tracker);
if (ret != 0)
goto error_cleanup;
return 0;
error_cleanup:
latency_tracker_debugfs_remove_tracker(tracker);
error:
return -1;
}
void latency_tracker_debugfs_remove_tracker(struct latency_tracker *tracker)
{
if (!tracker->debugfs_dir)
return;
destroy_wakeup_pipe(tracker);
debugfs_remove_recursive(tracker->debugfs_dir);
}
struct dentry *latency_tracker_debugfs_add_subfolder(
struct latency_tracker *tracker, const char *name)
{
struct dentry *dir;
if (!tracker->debugfs_dir)
goto error;
dir = debugfs_create_dir(name, tracker->debugfs_dir);
if (!dir)
goto error;
return dir;
error:
return NULL;
}
int latency_open_generic(struct inode *inode, struct file *filp)
{
filp->private_data = inode->i_private;
return 0;
}
static
ssize_t read_int(struct file *filp, char __user *ubuf,
size_t count, loff_t *ppos)
{
int r, ret;
char buf[64];
int *val = filp->private_data;
r = snprintf(buf, min_t(size_t, count, 64), "%d\n", *val);
ret = simple_read_from_buffer(ubuf, count, ppos, buf, r);
return ret;
}
static
ssize_t write_int(struct file *filp, const char __user *ubuf,
size_t count, loff_t *ppos)
{
int val, ret;
int *out_val;
ret = kstrtoint_from_user(ubuf, count, 10, &val);
if (ret)
return ret;
/* must have at least 1 entry */
if (!val)
return -EINVAL;
out_val = filp->private_data;
*out_val = val;
return count;
}
static
int open_int(struct inode *inode, struct file *filp)
{
filp->private_data = inode->i_private;
return 0;
}
static
const struct file_operations fops_int = {
.open = open_int,
.read = read_int,
.write = write_int,
.llseek = default_llseek,
};
struct dentry *debugfs_create_int(const char *name, umode_t mode,
struct dentry *parent, int *value)
{
return debugfs_create_file(name, mode, parent, value, &fops_int);
}
static
ssize_t tracking_on_read(struct file *filp, char __user *ubuf,
size_t count, loff_t *ppos)
{
int r, ret;
char buf[64];
struct latency_tracker *tracker = filp->private_data;
r = snprintf(buf, min_t(size_t, count, 64), "%d\n",
tracker->tracking_on);
ret = simple_read_from_buffer(ubuf, count, ppos, buf, r);
return ret;
}
static
ssize_t tracking_on_write(struct file *filp, const char __user *ubuf,
size_t count, loff_t *ppos)
{
int val, ret;
struct latency_tracker *tracker = filp->private_data;
ret = kstrtoint_from_user(ubuf, count, 10, &val);
if (ret)
return ret;
if (tracker->tracking_on == val)
goto end;
latency_tracker_set_tracking_on(tracker, val, 1);
end:
return count;
}
static
const struct file_operations fops_tracking_on = {
.open = open_int,
.read = tracking_on_read,
.write = tracking_on_write,
.llseek = default_llseek,
};
static
struct dentry *debugfs_create_tracking_on(umode_t mode, struct dentry *parent,
int *value, struct latency_tracker *tracker)
{
return debugfs_create_file("tracking_on", mode, parent,
tracker, &fops_tracking_on);
}