forked from microsoft/WSL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry.cpp
More file actions
282 lines (234 loc) · 9.48 KB
/
telemetry.cpp
File metadata and controls
282 lines (234 loc) · 9.48 KB
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
/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
telemetry.cpp
Abstract:
This file contains the telemetry agent implementation.
--*/
#include "common.h"
#include <dirent.h>
#include <errno.h>
#include <libgen.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <linux/connector.h>
#include <linux/netlink.h>
#include <linux/cn_proc.h>
#include <string>
#include <lxwil.h>
#include "util.h"
#include "mountutil.h"
#include "SocketChannel.h"
#include "message.h"
namespace {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wgnu-variable-sized-type-not-at-end"
union messageBuffer
{
struct
{
nlmsghdr netlinkHeader;
cn_msg connectorMessage;
proc_cn_mcast_op operation;
} Send;
struct
{
nlmsghdr netlinkHeader;
cn_msg connectorMessage;
proc_event event;
} Receive;
};
#pragma GCC diagnostic pop
// Map containing the binaries and commands that are filesystem-intensive that we want to warn users against using in DrvFs.
const std::map<std::string, std::string> g_drvFsUsageMap = {
{"git", "clone"}, {"node", "/usr/bin/npm install"}, {"cargo", "build"}};
bool g_drvFsUsageEnabled = true;
} // namespace
std::pair<std::string, bool> GetProcessInformation(int pid)
{
// N.B. Procfs files may no longer be present for short-lived processes that exit before the
// process creation notification can be processed.
const std::string procPidPath = std::format("/proc/{}", pid);
wil::unique_fd fd{open(std::format("{}/cmdline", procPidPath).c_str(), O_RDONLY)};
if (!fd)
{
return {};
}
// /proc/pid/cmdline contains all the arguments separated by NULL characters.
LX_MINI_INIT_TELEMETRY_MESSAGE message{};
message.Header.MessageSize = sizeof(message);
message.Header.MessageType = LX_MINI_INIT_TELEMETRY_MESSAGE::Type;
std::string commandLine(256, '\0');
auto bytesRead = TEMP_FAILURE_RETRY(read(fd.get(), commandLine.data(), commandLine.size()));
if (bytesRead <= 0)
{
return {};
}
commandLine.resize(bytesRead - 1);
const auto* executable = basename(commandLine.data());
bool showDrvfsNotification = false;
// Determine if the DrvFs perf notification should be displayed.
if (g_drvFsUsageEnabled)
{
// Check the if the binary name and first argument are in the list of scenarios.
auto found = g_drvFsUsageMap.find(executable);
if (found != g_drvFsUsageMap.end())
{
auto length = strlen(executable);
if (bytesRead > length + 1)
{
auto argument = std::string_view(commandLine.data(), bytesRead).substr(length + 1);
if (strcmp(argument.data(), found->second.c_str()) == 0)
{
// Determine if the current working directory is a DrvFs mount.
std::error_code errorCode;
auto cwd = std::filesystem::read_symlink(std::format("{}/cwd", procPidPath), errorCode);
if (!errorCode)
{
auto mountInfo = std::format("{}{}", procPidPath, MOUNT_INFO_FILE_NAME);
size_t prefixLength;
auto drvFsPrefix = UtilFindMount(mountInfo.c_str(), cwd.c_str(), false, &prefixLength);
if (!drvFsPrefix.empty())
{
showDrvfsNotification = true;
g_drvFsUsageEnabled = false;
}
}
}
}
}
}
return std::make_pair(executable, showDrvfsNotification);
}
unsigned int StartTelemetryAgent()
{
try
{
constexpr auto flushPeriod = std::chrono::minutes(30);
// The telemetry agent is only supported on VM mode.
if (!UtilIsUtilityVm())
{
return 1;
}
// Initialize logging.
InitializeLogging(false);
// Open and bind a netlink socket.
wil::unique_fd fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
THROW_LAST_ERROR_IF(!fd);
sockaddr_nl address{};
address.nl_family = AF_NETLINK;
address.nl_groups = CN_IDX_PROC;
address.nl_pid = getpid();
THROW_LAST_ERROR_IF(bind(fd.get(), reinterpret_cast<sockaddr*>(&address), sizeof(address)) < 0);
// Fill in the netlink header and connector message and send a message.
messageBuffer buffer{};
buffer.Send.netlinkHeader.nlmsg_len = sizeof(buffer.Send);
buffer.Send.netlinkHeader.nlmsg_type = NLMSG_DONE;
buffer.Send.netlinkHeader.nlmsg_pid = getpid();
buffer.Send.connectorMessage.id.idx = CN_IDX_PROC;
buffer.Send.connectorMessage.id.val = CN_VAL_PROC;
buffer.Send.connectorMessage.len = sizeof(buffer.Send.operation);
buffer.Send.operation = proc_cn_mcast_op::PROC_CN_MCAST_LISTEN;
auto bytes = send(fd.get(), &buffer, sizeof(buffer.Send), 0);
THROW_LAST_ERROR_IF(bytes != sizeof(buffer.Send));
// Set the receive timeout to 1 minute so the thread has an opportunity to flush even when no events are received.
timeval tv{};
tv.tv_sec = 10;
THROW_LAST_ERROR_IF(setsockopt(fd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0);
wsl::shared::SocketChannel channel({STDOUT_FILENO}, "Telemetry");
std::map<std::string, size_t> events;
std::optional<std::string> drvfsNotifyCommand;
// Schedule the next flush in 30 seconds so that some events are captured even if WSL shuts down quickly.
auto nextFlush = std::chrono::steady_clock::now() + std::chrono::seconds(30);
// Begin reading netlink messages.
for (;;)
{
memset(&buffer, 0, sizeof(buffer));
sockaddr_nl fromAddress{};
fromAddress.nl_family = AF_NETLINK;
fromAddress.nl_groups = CN_IDX_PROC;
fromAddress.nl_pid = 1;
socklen_t addressLength = sizeof(fromAddress);
bytes = TEMP_FAILURE_RETRY(recvfrom(fd.get(), &buffer, sizeof(buffer), 0, reinterpret_cast<sockaddr*>(&fromAddress), &addressLength));
if (bytes <= 0)
{
THROW_LAST_ERROR_IF(errno != ETIMEDOUT && errno != EAGAIN);
}
else
{
for (nlmsghdr* netlinkHeader = &buffer.Receive.netlinkHeader; NLMSG_OK(netlinkHeader, bytes);
netlinkHeader = NLMSG_NEXT(netlinkHeader, bytes))
{
if ((netlinkHeader->nlmsg_type == NLMSG_ERROR) || (netlinkHeader->nlmsg_type == NLMSG_OVERRUN))
{
break;
}
if (netlinkHeader->nlmsg_type == NLMSG_NOOP)
{
continue;
}
// For exec events, log app usage telemetry.
auto event = reinterpret_cast<proc_event*>(((cn_msg*)NLMSG_DATA(netlinkHeader))->data);
if (event->what == PROC_EVENT_EXEC)
{
auto [executable, showNotification] = GetProcessInformation(event->event_data.exec.process_pid);
if (showNotification)
{
drvfsNotifyCommand = executable;
}
// Make sure the name doesn't contain a '/' so it doesn't break our message format.
if (!executable.empty() && executable.find("/") == std::string::npos)
{
auto it = events.find(executable);
if (it == events.end())
{
events.emplace(std::move(executable), 1);
}
else
{
it->second++;
}
}
}
if (netlinkHeader->nlmsg_type == NLMSG_DONE)
{
break;
}
}
}
// Regularily flush messages back to the service.
auto now = std::chrono::steady_clock::now();
if (drvfsNotifyCommand.has_value() || now > nextFlush)
{
if (!events.empty())
{
std::stringstream content;
if (drvfsNotifyCommand.has_value())
{
content << drvfsNotifyCommand.value() << "/1/";
}
for (auto [executable, count] : events)
{
// Having an extra ',' at the end makes parsing simpler.
content << executable << '/' << count << '/';
}
wsl::shared::MessageWriter<LX_MINI_INIT_TELEMETRY_MESSAGE> message;
message->ShowDrvFsNotification = drvfsNotifyCommand.has_value();
message.WriteString(content.str());
channel.SendMessage<LX_MINI_INIT_TELEMETRY_MESSAGE>(message.Span());
events.clear();
drvfsNotifyCommand = {};
}
nextFlush = now + flushPeriod;
}
}
return 0;
}
catch (...)
{
LOG_CAUGHT_EXCEPTION();
return 1;
}
return 0;
}