This repository has been archived by the owner on Oct 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathoffcpu_proc.c
144 lines (121 loc) · 3.54 KB
/
offcpu_proc.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
/*
* Copyright (C) 2015 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/irq_work.h>
#include <linux/module.h>
#include <linux/poll.h>
#include <linux/proc_fs.h>
#include "offcpu.h"
struct offcpu_tracker *offcpu_alloc_priv(void)
{
struct offcpu_tracker *offcpu_priv;
offcpu_priv = kzalloc(sizeof(struct offcpu_tracker), GFP_KERNEL);
if (!offcpu_priv)
goto end;
offcpu_priv->reason = OFFCPU_TRACKER_WAIT;
/* limit to 1 evt/sec */
offcpu_priv->ns_rate_limit = 1000000000;
end:
return offcpu_priv;
}
static
void irq_wake(struct irq_work *entry)
{
struct offcpu_tracker *offcpu_priv = container_of(entry,
struct offcpu_tracker, w_irq);
offcpu_priv->reason = OFFCPU_TRACKER_WAKE_DATA;
wake_up_interruptible(&offcpu_priv->read_wait);
offcpu_priv->got_alert = true;
}
int offcpu_setup_priv(struct offcpu_tracker *offcpu_priv)
{
int ret;
init_irq_work(&offcpu_priv->w_irq, irq_wake);
offcpu_priv->proc_dentry = proc_create_data("offcpu_latency",
S_IRUSR|S_IRGRP|S_IROTH, NULL, &wakeup_tracker_fops,
offcpu_priv);
if (!offcpu_priv->proc_dentry) {
printk(KERN_ERR "Error creating tracker control file\n");
ret = -ENOMEM;
goto end;
}
init_waitqueue_head(&offcpu_priv->read_wait);
ret = 0;
end:
return ret;
}
void offcpu_destroy_priv(struct offcpu_tracker *offcpu_priv)
{
irq_work_sync(&offcpu_priv->w_irq);
if (offcpu_priv->proc_dentry)
remove_proc_entry("offcpu_latency", NULL);
kfree(offcpu_priv);
}
void offcpu_handle_proc(struct offcpu_tracker *offcpu_priv,
uint64_t end_ts)
{
/* Rate limiter */
if ((end_ts - offcpu_priv->last_alert_ts) <
offcpu_priv->ns_rate_limit)
return;
if (offcpu_priv->readers > 0)
irq_work_queue(&offcpu_priv->w_irq);
offcpu_priv->last_alert_ts = end_ts;
}
unsigned int tracker_proc_poll(struct file *filp,
poll_table *wait)
{
struct offcpu_tracker *offcpu_priv = filp->private_data;
unsigned int mask = 0;
if (filp->f_mode & FMODE_READ) {
poll_wait(filp, &offcpu_priv->read_wait, wait);
if (offcpu_priv->reason == OFFCPU_TRACKER_WAKE_DATA)
mask |= POLLIN;
else
mask |= POLLHUP;
}
return mask;
}
ssize_t tracker_proc_read(struct file *filp, char __user *buf, size_t n,
loff_t *offset)
{
struct offcpu_tracker *offcpu_priv = filp->private_data;
wait_event_interruptible(offcpu_priv->read_wait,
offcpu_priv->got_alert);
offcpu_priv->reason = OFFCPU_TRACKER_WAIT;
offcpu_priv->got_alert = false;
return 0;
}
int tracker_proc_open(struct inode *inode, struct file *filp)
{
struct offcpu_tracker *offcpu_priv = PDE_DATA(inode);
int ret;
offcpu_priv->got_alert = false;
offcpu_priv->readers++;
filp->private_data = offcpu_priv;
ret = try_module_get(THIS_MODULE);
if (!ret)
return -1;
return 0;
}
int tracker_proc_release(struct inode *inode, struct file *filp)
{
struct offcpu_tracker *offcpu_priv = filp->private_data;
offcpu_priv->readers--;
module_put(THIS_MODULE);
return 0;
}