forked from efficios/latency-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwakeup_latency.c
247 lines (211 loc) · 6.86 KB
/
wakeup_latency.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
/*
* wakeup_latency.c
*
* Example of usage of latency_tracker with kernel tracepoints.
*
* In this example, we call the callback function wakeup_cb when the delay
* between a sched wakeup and its completion (sched_switch) takes more than
* DEFAULT_USEC_WAKEUP_LATENCY_THRESH microseconds. Moreover, if the task is
* still not scheduled after DEFAULT_USEC_WAKEUP_LATENCY_TIMEOUT microseconds,
* the callback is called with timeout = 1.
*
* The 2 parameters can be controlled at run-time by writing the value in
* micro-seconds in:
* /sys/module/wakeup_latency/parameters/usec_threshold and
* /sys/module/wakeup_latency/parameters/usec_timeout
*
* It is possible to use nanoseconds, but you have to write manually the value
* in this source code.
*
* 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/moduleparam.h>
#include <linux/file.h>
#include <linux/dcache.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/proc_fs.h>
#include "wakeup_latency.h"
#include "../latency_tracker.h"
#include "../wrapper/tracepoint.h"
#include "../wrapper/lt_probe.h"
#include "../wrapper/trace-clock.h"
#include <trace/events/latency_tracker.h>
/*
* Threshold to execute the callback (microseconds).
*/
#define DEFAULT_USEC_WAKEUP_LATENCY_THRESH 5 * 1000
/*
* Timeout to execute the callback (microseconds).
*/
#define DEFAULT_USEC_WAKEUP_LATENCY_TIMEOUT 0
static pid_t current_pid[NR_CPUS];
/*
* microseconds because we can't guarantee the passing of 64-bit
* arguments to insmod on all architectures.
*/
static unsigned long usec_threshold = DEFAULT_USEC_WAKEUP_LATENCY_THRESH;
module_param(usec_threshold, ulong, 0644);
MODULE_PARM_DESC(usec_threshold, "Threshold in microseconds");
static unsigned long usec_timeout = DEFAULT_USEC_WAKEUP_LATENCY_TIMEOUT;
module_param(usec_timeout, ulong, 0644);
MODULE_PARM_DESC(usec_timeout, "Timeout in microseconds");
struct schedkey {
pid_t pid;
} __attribute__((__packed__));
#undef MAX_KEY_SIZE
#define MAX_KEY_SIZE sizeof(struct schedkey)
static struct latency_tracker *tracker;
static int cnt = 0;
static
void wakeup_cb(struct latency_tracker_event_ctx *ctx)
{
uint64_t end_ts = latency_tracker_event_ctx_get_end_ts(ctx);
uint64_t start_ts = latency_tracker_event_ctx_get_start_ts(ctx);
enum latency_tracker_cb_flag cb_flag = latency_tracker_event_ctx_get_cb_flag(ctx);
struct schedkey *key = (struct schedkey *) latency_tracker_event_ctx_get_key(ctx)->key;
struct wakeup_tracker *wakeup_priv = latency_tracker_event_ctx_get_priv(ctx);
struct task_struct *p;
u64 delay;
if (cb_flag != LATENCY_TRACKER_CB_NORMAL)
return;
delay = (end_ts - start_ts) / 1000;
#ifdef SCHEDWORST
usec_threshold = delay;
#endif
rcu_read_lock();
p = pid_task(find_vpid(key->pid), PIDTYPE_PID);
if (!p)
goto end_unlock;
trace_latency_tracker_wakeup(p->comm, key->pid, end_ts - start_ts,
cb_flag);
rcu_read_unlock();
cnt++;
wakeup_handle_proc(wakeup_priv, end_ts);
goto end;
end_unlock:
rcu_read_unlock();
end:
return;
}
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,3,0) || \
LT_RT_KERNEL_RANGE(4,1,10,11, 4,2,0,0))
LT_PROBE_DEFINE(sched_waking, struct task_struct *p)
#else
LT_PROBE_DEFINE(sched_waking, struct task_struct *p, int success)
#endif
{
struct schedkey key;
int i;
enum latency_tracker_event_in_ret ret;
if (!latency_tracker_get_tracking_on(tracker))
return;
if (!p || !p->pid)
return;
/*
* Make sure we won't wait for a process already running on another CPU.
*/
for (i = 0; i < NR_CPUS; i++)
if (current_pid[i] == p->pid)
return;
key.pid = p->pid;
ret = latency_tracker_event_in(tracker, &key, sizeof(key),
1, latency_tracker_get_priv(tracker));
if (ret == LATENCY_TRACKER_FULL) {
// printk("latency_tracker sched: no more free events, consider "
// "increasing the max_events parameter\n");
} else if (ret) {
// printk("latency_tracker sched: error adding event\n");
}
}
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,4,0))
LT_PROBE_DEFINE(sched_switch, bool preempt, struct task_struct *prev,
struct task_struct *next)
#else
LT_PROBE_DEFINE(sched_switch, struct task_struct *prev,
struct task_struct *next)
#endif
{
struct schedkey key;
if (!latency_tracker_get_tracking_on(tracker))
return;
if (!next || !next->pid)
return;
current_pid[prev->on_cpu] = next->pid;
key.pid = next->pid;
latency_tracker_event_out(tracker, NULL, &key, sizeof(key), 0, 0);
}
static
int __init wakeup_latency_init(void)
{
int ret;
struct wakeup_tracker *wakeup_priv;
wakeup_priv = wakeup_alloc_priv();
if (!wakeup_priv) {
ret = -ENOMEM;
goto end;
}
tracker = latency_tracker_create("wakeup");
if (!tracker)
goto error;
latency_tracker_set_timer_period(tracker, 100000000);
latency_tracker_set_max_resize(tracker, 1000);
latency_tracker_set_startup_events(tracker, 200);
latency_tracker_set_priv(tracker, wakeup_priv);
latency_tracker_set_threshold(tracker, usec_threshold * 1000);
latency_tracker_set_timeout(tracker, usec_timeout * 1000);
latency_tracker_set_callback(tracker, wakeup_cb);
latency_tracker_set_key_size(tracker, MAX_KEY_SIZE);
ret = latency_tracker_enable(tracker);
if (ret)
goto error;
ret = lttng_wrapper_tracepoint_probe_register("sched_waking",
probe_sched_waking, NULL);
WARN_ON(ret);
ret = lttng_wrapper_tracepoint_probe_register("sched_switch",
probe_sched_switch, NULL);
WARN_ON(ret);
ret = wakeup_setup_priv(wakeup_priv);
goto end;
error:
wakeup_destroy_priv(wakeup_priv);
ret = -1;
end:
return ret;
}
module_init(wakeup_latency_init);
static
void __exit wakeup_latency_exit(void)
{
uint64_t skipped;
struct wakeup_tracker *wakeup_priv;
lttng_wrapper_tracepoint_probe_unregister("sched_waking",
probe_sched_waking, NULL);
lttng_wrapper_tracepoint_probe_unregister("sched_switch",
probe_sched_switch, NULL);
tracepoint_synchronize_unregister();
skipped = latency_tracker_skipped_count(tracker);
wakeup_priv = latency_tracker_get_priv(tracker);
wakeup_destroy_priv(wakeup_priv);
latency_tracker_destroy(tracker);
printk("Missed events : %llu\n", skipped);
printk("Total wakeup alerts : %d\n", cnt);
}
module_exit(wakeup_latency_exit);
MODULE_AUTHOR("Julien Desfossez <[email protected]>");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.0");