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
/
userspace.c
177 lines (142 loc) · 4.34 KB
/
userspace.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
/*
* userspace.c
*
* Copyright (C) 2017 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/jhash.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <asm/syscall.h>
#include "../latency_tracker.h"
#include "../tracker_debugfs.h"
#include "../wrapper/tracepoint.h"
#include "../wrapper/trace-clock.h"
#include "../wrapper/lt_probe.h"
#include <trace/events/latency_tracker.h>
/*
* Limited to MAX_FILTER_STR_VAL (256) for ftrace compatibility.
*/
#define LT_MAX_COOKIE_SIZE MAX_FILTER_STR_VAL
struct userspace_key {
char cookie[LT_MAX_COOKIE_SIZE];
int cookie_size;
} __attribute__((__packed__));
#undef MAX_KEY_SIZE
#define MAX_KEY_SIZE sizeof(struct userspace_key)
static struct latency_tracker *tracker;
static int cnt = 0;
static
void userspace_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);
u64 delay;
/*
* TODO: output an event in ftrace
struct userspace_key *key = (struct userspace_key *)
latency_tracker_event_ctx_get_key(ctx)->key;
*/
if (cb_flag != LATENCY_TRACKER_CB_NORMAL)
return;
delay = end_ts - start_ts;
cnt++;
latency_tracker_debugfs_wakeup_pipe(tracker);
}
LT_PROBE_DEFINE(tracker_begin, char *tp_data, size_t len)
{
struct userspace_key key;
enum latency_tracker_event_in_ret ret;
if (!latency_tracker_get_tracking_on(tracker))
return;
if (!tp_data)
return;
/*
* Cookies must be strings, just a "echo > work_begin" is not
* accepted.
*/
if (len == 1 && (tp_data[0] == '\n' || tp_data[0] == '\0'))
return;
memset(&key.cookie, 0, sizeof(key.cookie));
memcpy(&key.cookie, tp_data, len);
ret = latency_tracker_event_in(tracker, &key, sizeof(key), 1, NULL);
WARN_ON_ONCE(ret);
}
LT_PROBE_DEFINE(tracker_end, char *tp_data, size_t len)
{
struct userspace_key key;
if (!latency_tracker_get_tracking_on(tracker))
return;
if (!tp_data)
return;
/*
* Cookies must be strings, just a "echo > work_begin" is not
* accepted.
*/
if (len == 1 && (tp_data[0] == '\n' || tp_data[0] == '\0'))
return;
memset(&key.cookie, 0, sizeof(key.cookie));
memcpy(&key.cookie, tp_data, len);
latency_tracker_event_out(tracker, NULL, &key, sizeof(key), 0, 0);
}
static
int __init userspace_init(void)
{
int ret;
tracker = latency_tracker_create("userspace");
if (!tracker)
goto error;
latency_tracker_set_callback(tracker, userspace_cb);
latency_tracker_set_key_size(tracker, MAX_KEY_SIZE);
ret = latency_tracker_debugfs_setup_wakeup_pipe(tracker);
if (ret != 0)
goto error;
ret = lttng_wrapper_tracepoint_probe_register("latency_tracker_begin",
probe_tracker_begin, NULL);
WARN_ON(ret);
ret = lttng_wrapper_tracepoint_probe_register("latency_tracker_end",
probe_tracker_end, NULL);
WARN_ON(ret);
goto end;
error:
ret = -1;
end:
return ret;
}
module_init(userspace_init);
static
void __exit userspace_exit(void)
{
uint64_t skipped;
lttng_wrapper_tracepoint_probe_unregister("latency_tracker_begin",
probe_tracker_begin, NULL);
lttng_wrapper_tracepoint_probe_unregister("latency_tracker_end",
probe_tracker_end, NULL);
tracepoint_synchronize_unregister();
skipped = latency_tracker_skipped_count(tracker);
latency_tracker_destroy(tracker);
printk("Missed events : %llu\n", skipped);
printk("Total userspace alerts : %d\n", cnt);
}
module_exit(userspace_exit);
MODULE_AUTHOR("Julien Desfossez <[email protected]>");
MODULE_LICENSE("GPL and additional rights");
MODULE_VERSION("1.0");