-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnative.cc
408 lines (355 loc) · 12.1 KB
/
native.cc
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <iostream>
#include <node.h>
#include <optional>
#include <string>
#include <thread>
#include <uv.h>
using namespace std::chrono_literals;
using namespace v8;
/**
* Appends a V8 string to a std::u16string.
*/
void AppendV8String(Isolate *isolate, std::u16string &r, Local<String> s) {
if (s.IsEmpty()) {
r += u"(unknown)";
return;
}
std::size_t sz = r.size();
r.resize(sz + s->Length());
s->Write(isolate, (uint16_t *)(&r[sz]));
}
/**
* Appends an integer to a std::u16string.
*/
void AppendNumber(std::u16string &r, int num) {
std::string s = std::to_string(num);
for (const auto &ch : s) {
r.push_back((char16_t)ch);
}
}
// Comments starting with ^ indicate PlusCal code, and are extracted by the
// build script.
//
//^ This is a formal proof that this code does not deadlock.
//^ In particular, there are multiple threads of execution:
//^ `^\begin{enumerate}
//^ \item The "watchdog" thread, which listens for heartbeats and schedules
//^ a Javascript interrupt if it has been too long since a heartbeat.
//^
//^ \item The "javascript" thread, which executes user's JavaScript and
//^ runs the interrupts.
//^
//^ \item Other threads, which may terminate the JavaScript thread.
//^ \end{enumerate}^'
//^
//^ ---- MODULE EventLoopBlockage ----
//^ EXTENDS TLC
//^ (*--algorithm Poller {
//^ variables
//^ request_interrupt_begin = FALSE;
//^ fork_watchdog = FALSE;
//^ watchdog_joined = FALSE;
class PerIsolateData {
public:
explicit PerIsolateData(Isolate *isolate)
: //^ should_stop = FALSE;
should_stop_(false),
//^ missed_heartbeat = FALSE;
last_heartbeat_(std::chrono::steady_clock::now().time_since_epoch()),
//^ interrupt_done = FALSE;
interrupt_done_(false),
//^ stack_ready = FALSE;
stack_ready_(false), isolate_(isolate) {
//^ begin_terminate = FALSE;
node::AddEnvironmentCleanupHook(isolate, DeleteInstance, this);
}
//^ process (watchdog = "watchdog") {
/**
* Begins the watchdog. The watchdog waits and enqueues an interrupt if there
* is no heartbeat from the isolate for longer than threshold_ms milliseconds.
*/
bool StartWatchdog(Local<Function> callback, uint64_t interval_ms,
uint64_t threshold_ms) {
//^ WatchdogWaitingFork:
//^ await fork_watchdog \/ should_stop;
if (watchdog_) {
return false;
}
callback_.Reset(isolate_, callback);
interval_ms_ = interval_ms;
threshold_ms_ = threshold_ms;
watchdog_ = std::thread([this]() {
//^ WatchdogStart:
//^ while (~should_stop) {
while (!should_stop_) {
//^ MarkHeartbeatMissed:
//^ missed_heartbeat := TRUE;
std::this_thread::sleep_for(threshold_ms_ * 1ms);
//^ CheckHeartbeatMissed:
//^ if (~missed_heartbeat) {
//^ skip;
//^ } else {
auto now = std::chrono::steady_clock::now().time_since_epoch();
auto last_heartbeat = last_heartbeat_.load();
uint64_t estimated_event_loop_blockage_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(
now - last_heartbeat)
.count();
if (estimated_event_loop_blockage_ms <= threshold_ms_) {
continue;
}
//^ MarkInterruptNotDone:
//^ interrupt_done := FALSE;
interrupt_done_ = false;
//^ RequestInterruptBeginProfiler:
//^ request_interrupt_begin := TRUE;
// TODO(keyhan): should we be using env->RequestInterrupt?
isolate_->RequestInterrupt(BeginInterruptCallback, this);
//^ CheckShouldStopBeforeBegin1:
//^ if (should_stop) {
//^ goto WatchdogEnd;
//^ };
if (should_stop_) {
return;
}
//^ WaitUntilBeginProfiler:
//^ await interrupt_done;
{
std::unique_lock<std::mutex> lk(interrupt_done_m_);
interrupt_done_cv_.wait(lk, [this] { return !!interrupt_done_; });
}
//^ MarkStackReady:
//^ stack_ready := TRUE;
stack_ready_ = true;
//^ CheckShouldStopBeforeBegin2:
//^ if (should_stop) {
//^ goto WatchdogEnd;
//^ };
if (should_stop_) {
return;
}
//^ WaitUntilStackNotReady:
//^ await ~stack_ready;
{
std::unique_lock<std::mutex> lk(stack_ready_m_);
stack_ready_cv_.wait(lk, [this] { return !stack_ready_; });
}
//^ }
//^ };
}
//^ WatchdogEnd:
//^ watchdog_joined := TRUE;
//^ }
});
return true;
}
//^ process (javascript = "javascript") {
//^ Executing:
//^ while (~begin_terminate) {
//^ either {
//^ ForkWatchdog:
//^ fork_watchdog := TRUE;
//^ } or {
/**
* Heartbeat and record the time it occurred. If there is a stack pending,
* then a recent heartbeat was late, and we invoke callback_ with the
* blockage duration and the stack from the interrupt.
*/
void Heartbeat() {
auto now = std::chrono::steady_clock::now().time_since_epoch();
//^ CheckForStack:
//^ if (stack_ready) {
if (stack_ready_) {
uint64_t estimated_event_loop_blockage_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(
now - last_heartbeat_.load())
.count();
// In expectation, the delay will "begin" halfway through the polling
// cycle. Subtract off this number to compute it correctly.
// (Note there is still some overestimation, as other timers may execute
// before the heartbeat does.)
uint64_t bias = interval_ms_ / 2;
if (estimated_event_loop_blockage_ms < bias) {
estimated_event_loop_blockage_ms = 0;
} else {
estimated_event_loop_blockage_ms -= bias;
}
MaybeLocal<String> maybe_stack = String::NewFromTwoByte(
isolate_, (uint16_t *)captured_stack_trace_.data(),
NewStringType::kNormal, captured_stack_trace_.length());
// Clear the captured stack so the next interrupt can use it.
captured_stack_trace_.clear();
Local<Value> stack;
if (!maybe_stack.ToLocal(&stack)) {
// This is probably impossible as it would mean the string exceeded the
// string size limit, but handle it gracefully anyway.
stack = Null(isolate_);
}
Local<Number> blockage_ms =
Number::New(isolate_, estimated_event_loop_blockage_ms);
Local<Value> argv[2] = {blockage_ms, stack};
Local<Context> context = isolate_->GetCurrentContext();
// Ignore the function results.
// TODO(kvakil): should we throw an error?
(void)(Local<Function>::New(isolate_, callback_)
->Call(context, Null(isolate_), 2, argv));
//^ MarkStackReceived:
//^ stack_ready := FALSE;
stack_ready_ = false;
stack_ready_cv_.notify_one();
//^ };
}
//^ Heartbeat:
//^ missed_heartbeat := FALSE;
last_heartbeat_ = now;
//^ } or {
}
private:
//^ InterruptBegin:
/**
* Captures the isolate's current stack trace and formats it as a UTF16
* string. Queues the stack string up to be consumed by Heartbeat.
*/
void BeginInterrupt() {
//^ if (request_interrupt_begin) {
//^ request_interrupt_begin := FALSE;
Local<StackTrace> stack_trace = StackTrace::CurrentStackTrace(isolate_, 32);
int frame_count = stack_trace->GetFrameCount();
for (int i = 0; i < frame_count; i++) {
captured_stack_trace_ += u" at ";
Local<StackFrame> stack_frame = stack_trace->GetFrame(isolate_, i);
if (stack_frame->IsConstructor()) {
captured_stack_trace_ += u"new ";
}
AppendV8String(isolate_, captured_stack_trace_,
stack_frame->GetFunctionName());
captured_stack_trace_ += u" ";
if (stack_frame->IsWasm()) {
captured_stack_trace_ += u"<WASM> ";
}
captured_stack_trace_ += u"(";
if (stack_frame->IsEval()) {
captured_stack_trace_ += u"[eval]";
} else {
AppendV8String(isolate_, captured_stack_trace_,
stack_frame->GetScriptName());
}
captured_stack_trace_ += u":";
auto line_number = stack_frame->GetLineNumber();
if (line_number != Message::kNoLineNumberInfo) {
AppendNumber(captured_stack_trace_, line_number);
} else {
captured_stack_trace_ += u"(unknown)";
}
captured_stack_trace_ += u":";
auto column_number = stack_frame->GetColumn();
if (column_number != Message::kNoColumnInfo) {
AppendNumber(captured_stack_trace_, column_number);
} else {
captured_stack_trace_ += u"(unknown)";
}
captured_stack_trace_ += u")";
if (i != frame_count - 1) {
captured_stack_trace_ += u"\n";
}
}
//^ interrupt_done := TRUE;
interrupt_done_ = true;
interrupt_done_cv_.notify_one();
//^ }
//^ } or {
}
~PerIsolateData() {
//^ BeginTermination:
//^ begin_terminate := TRUE;
//^ }
//^ };
if (!watchdog_) {
return;
}
//^ DoTerminate:
//^ should_stop := TRUE;
should_stop_ = true;
//^ DoTerminate2:
//^ interrupt_done := TRUE;
interrupt_done_ = true;
interrupt_done_cv_.notify_one();
//^ DoTerminate3:
//^ stack_ready := FALSE;
stack_ready_ = false;
stack_ready_cv_.notify_one();
//^ WaitWatchdog:
//^ await watchdog_joined;
watchdog_->join();
//^ }
}
//^ } *)
//^ ====
static void DeleteInstance(void *data) {
delete static_cast<PerIsolateData *>(data);
}
static void BeginInterruptCallback(Isolate *isolate, void *data) {
static_cast<PerIsolateData *>(data)->BeginInterrupt();
}
std::u16string captured_stack_trace_;
std::optional<std::thread> watchdog_;
uint64_t interval_ms_;
uint64_t threshold_ms_;
std::atomic<bool> should_stop_;
std::atomic<std::chrono::steady_clock::duration> last_heartbeat_;
std::condition_variable interrupt_done_cv_;
std::mutex interrupt_done_m_;
std::atomic<bool> interrupt_done_;
std::condition_variable stack_ready_cv_;
std::mutex stack_ready_m_;
std::atomic<bool> stack_ready_;
Isolate *isolate_;
Persistent<Function> callback_;
};
static void StartWatchdog(const FunctionCallbackInfo<Value> &info) {
PerIsolateData *data =
reinterpret_cast<PerIsolateData *>(info.Data().As<External>()->Value());
if (!info[0]->IsFunction() || !info[1]->IsNumber() || !info[2]->IsNumber()) {
info.GetIsolate()->ThrowException(Exception::TypeError(
String::NewFromUtf8(info.GetIsolate(), "bad arguments")
.ToLocalChecked()));
return;
}
Local<Function> f = info[0].As<Function>();
uint64_t interval_ms = info[1].As<Number>()->Value();
uint64_t threshold_ms = info[2].As<Number>()->Value();
info.GetReturnValue().Set(data->StartWatchdog(f, interval_ms, threshold_ms));
}
static void Heartbeat(const FunctionCallbackInfo<Value> &info) {
PerIsolateData *data =
reinterpret_cast<PerIsolateData *>(info.Data().As<External>()->Value());
data->Heartbeat();
}
NODE_MODULE_INIT() {
Isolate *isolate = context->GetIsolate();
PerIsolateData *data = new PerIsolateData(isolate);
Local<External> external = External::New(isolate, data);
exports
->Set(context, String::NewFromUtf8(isolate, "heartbeat").ToLocalChecked(),
FunctionTemplate::New(isolate, Heartbeat, external)
->GetFunction(context)
.ToLocalChecked())
.FromJust();
exports
->Set(context,
String::NewFromUtf8(isolate, "startWatchdog").ToLocalChecked(),
FunctionTemplate::New(isolate, StartWatchdog, external)
->GetFunction(context)
.ToLocalChecked())
.FromJust();
// TODO(kvakil): implement stopWatchdog. This is tricky, since the Javascript
// thread may (theoretically?) have an interrupt scheduled while we stop.
}