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
|
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <unordered_map>
#include "include/v8-platform.h"
#include "src/base/logging.h"
#include "src/base/macros.h"
#include "src/base/platform/mutex.h"
#include "src/base/platform/platform.h"
#include "src/base/platform/time.h"
#include "src/base/utils/random-number-generator.h"
#include "src/d8/d8-platforms.h"
namespace v8 {
class PredictablePlatform : public Platform {
public:
explicit PredictablePlatform(std::unique_ptr<Platform> platform)
: platform_(std::move(platform)) {
DCHECK_NOT_NULL(platform_);
}
PageAllocator* GetPageAllocator() override {
return platform_->GetPageAllocator();
}
void OnCriticalMemoryPressure() override {
platform_->OnCriticalMemoryPressure();
}
bool OnCriticalMemoryPressure(size_t length) override {
return platform_->OnCriticalMemoryPressure(length);
}
std::shared_ptr<TaskRunner> GetForegroundTaskRunner(
v8::Isolate* isolate) override {
return platform_->GetForegroundTaskRunner(isolate);
}
int NumberOfWorkerThreads() override { return 0; }
void CallOnWorkerThread(std::unique_ptr<Task> task) override {
// We post worker tasks on the foreground task runner of the
// {kProcessGlobalPredictablePlatformWorkerTaskQueue} isolate. The task
// queue of the {kProcessGlobalPredictablePlatformWorkerTaskQueue} isolate
// is then executed on the main thread to achieve predictable behavior.
//
// In this context here it is okay to call {GetForegroundTaskRunner} from a
// background thread. The reason is that code is executed sequentially with
// the PredictablePlatform, and that the {DefaultPlatform} does not access
// the isolate but only uses it as the key in a HashMap.
GetForegroundTaskRunner(kProcessGlobalPredictablePlatformWorkerTaskQueue)
->PostTask(std::move(task));
}
void CallDelayedOnWorkerThread(std::unique_ptr<Task> task,
double delay_in_seconds) override {
// Never run delayed tasks.
}
bool IdleTasksEnabled(Isolate* isolate) override { return false; }
double MonotonicallyIncreasingTime() override {
return synthetic_time_in_sec_ += 0.00001;
}
double CurrentClockTimeMillis() override {
return MonotonicallyIncreasingTime() * base::Time::kMillisecondsPerSecond;
}
v8::TracingController* GetTracingController() override {
return platform_->GetTracingController();
}
Platform* platform() const { return platform_.get(); }
private:
double synthetic_time_in_sec_ = 0.0;
std::unique_ptr<Platform> platform_;
DISALLOW_COPY_AND_ASSIGN(PredictablePlatform);
};
std::unique_ptr<Platform> MakePredictablePlatform(
std::unique_ptr<Platform> platform) {
return std::make_unique<PredictablePlatform>(std::move(platform));
}
class DelayedTasksPlatform : public Platform {
public:
explicit DelayedTasksPlatform(std::unique_ptr<Platform> platform)
: platform_(std::move(platform)) {
DCHECK_NOT_NULL(platform_);
}
explicit DelayedTasksPlatform(std::unique_ptr<Platform> platform,
int64_t random_seed)
: platform_(std::move(platform)), rng_(random_seed) {
DCHECK_NOT_NULL(platform_);
}
~DelayedTasksPlatform() {
// When the platform shuts down, all task runners must be freed.
DCHECK_EQ(0, delayed_task_runners_.size());
}
PageAllocator* GetPageAllocator() override {
return platform_->GetPageAllocator();
}
void OnCriticalMemoryPressure() override {
platform_->OnCriticalMemoryPressure();
}
bool OnCriticalMemoryPressure(size_t length) override {
return platform_->OnCriticalMemoryPressure(length);
}
std::shared_ptr<TaskRunner> GetForegroundTaskRunner(
v8::Isolate* isolate) override {
std::shared_ptr<TaskRunner> runner =
platform_->GetForegroundTaskRunner(isolate);
base::MutexGuard lock_guard(&mutex_);
// Check if we can re-materialize the weak ptr in our map.
std::weak_ptr<DelayedTaskRunner>& weak_delayed_runner =
delayed_task_runners_[runner.get()];
std::shared_ptr<DelayedTaskRunner> delayed_runner =
weak_delayed_runner.lock();
if (!delayed_runner) {
// Create a new {DelayedTaskRunner} and keep a weak reference in our map.
delayed_runner.reset(new DelayedTaskRunner(runner, this),
DelayedTaskRunnerDeleter{});
weak_delayed_runner = delayed_runner;
}
return std::move(delayed_runner);
}
int NumberOfWorkerThreads() override {
return platform_->NumberOfWorkerThreads();
}
void CallOnWorkerThread(std::unique_ptr<Task> task) override {
platform_->CallOnWorkerThread(MakeDelayedTask(std::move(task)));
}
void CallDelayedOnWorkerThread(std::unique_ptr<Task> task,
double delay_in_seconds) override {
platform_->CallDelayedOnWorkerThread(MakeDelayedTask(std::move(task)),
delay_in_seconds);
}
bool IdleTasksEnabled(Isolate* isolate) override {
return platform_->IdleTasksEnabled(isolate);
}
double MonotonicallyIncreasingTime() override {
return platform_->MonotonicallyIncreasingTime();
}
double CurrentClockTimeMillis() override {
return platform_->CurrentClockTimeMillis();
}
v8::TracingController* GetTracingController() override {
return platform_->GetTracingController();
}
private:
class DelayedTaskRunnerDeleter;
class DelayedTaskRunner final : public TaskRunner {
public:
DelayedTaskRunner(std::shared_ptr<TaskRunner> task_runner,
DelayedTasksPlatform* platform)
: task_runner_(task_runner), platform_(platform) {}
void PostTask(std::unique_ptr<Task> task) final {
task_runner_->PostTask(platform_->MakeDelayedTask(std::move(task)));
}
void PostNonNestableTask(std::unique_ptr<Task> task) final {
task_runner_->PostNonNestableTask(
platform_->MakeDelayedTask(std::move(task)));
}
void PostDelayedTask(std::unique_ptr<Task> task,
double delay_in_seconds) final {
task_runner_->PostDelayedTask(platform_->MakeDelayedTask(std::move(task)),
delay_in_seconds);
}
void PostIdleTask(std::unique_ptr<IdleTask> task) final {
task_runner_->PostIdleTask(
platform_->MakeDelayedIdleTask(std::move(task)));
}
bool IdleTasksEnabled() final { return task_runner_->IdleTasksEnabled(); }
bool NonNestableTasksEnabled() const final {
return task_runner_->NonNestableTasksEnabled();
}
private:
friend class DelayedTaskRunnerDeleter;
std::shared_ptr<TaskRunner> task_runner_;
DelayedTasksPlatform* platform_;
};
class DelayedTaskRunnerDeleter {
public:
void operator()(DelayedTaskRunner* runner) const {
TaskRunner* original_runner = runner->task_runner_.get();
base::MutexGuard lock_guard(&runner->platform_->mutex_);
auto& delayed_task_runners = runner->platform_->delayed_task_runners_;
DCHECK_EQ(1, delayed_task_runners.count(original_runner));
delayed_task_runners.erase(original_runner);
}
};
class DelayedTask : public Task {
public:
DelayedTask(std::unique_ptr<Task> task, int32_t delay_ms)
: task_(std::move(task)), delay_ms_(delay_ms) {}
void Run() final {
base::OS::Sleep(base::TimeDelta::FromMicroseconds(delay_ms_));
task_->Run();
}
private:
std::unique_ptr<Task> task_;
int32_t delay_ms_;
};
class DelayedIdleTask : public IdleTask {
public:
DelayedIdleTask(std::unique_ptr<IdleTask> task, int32_t delay_ms)
: task_(std::move(task)), delay_ms_(delay_ms) {}
void Run(double deadline_in_seconds) final {
base::OS::Sleep(base::TimeDelta::FromMicroseconds(delay_ms_));
task_->Run(deadline_in_seconds);
}
private:
std::unique_ptr<IdleTask> task_;
int32_t delay_ms_;
};
std::unique_ptr<Platform> platform_;
// The Mutex protects the RNG, which is used by foreground and background
// threads, and the {delayed_task_runners_} map might be accessed concurrently
// by the shared_ptr destructor.
base::Mutex mutex_;
base::RandomNumberGenerator rng_;
std::unordered_map<TaskRunner*, std::weak_ptr<DelayedTaskRunner>>
delayed_task_runners_;
int32_t GetRandomDelayInMilliseconds() {
base::MutexGuard lock_guard(&mutex_);
double delay_fraction = rng_.NextDouble();
// Sleep up to 100ms (100000us). Square {delay_fraction} to shift
// distribution towards shorter sleeps.
return 1e5 * (delay_fraction * delay_fraction);
}
std::unique_ptr<Task> MakeDelayedTask(std::unique_ptr<Task> task) {
return std::make_unique<DelayedTask>(std::move(task),
GetRandomDelayInMilliseconds());
}
std::unique_ptr<IdleTask> MakeDelayedIdleTask(
std::unique_ptr<IdleTask> task) {
return std::make_unique<DelayedIdleTask>(std::move(task),
GetRandomDelayInMilliseconds());
}
DISALLOW_COPY_AND_ASSIGN(DelayedTasksPlatform);
};
std::unique_ptr<Platform> MakeDelayedTasksPlatform(
std::unique_ptr<Platform> platform, int64_t random_seed) {
if (random_seed) {
return std::make_unique<DelayedTasksPlatform>(std::move(platform),
random_seed);
}
return std::make_unique<DelayedTasksPlatform>(std::move(platform));
}
} // namespace v8
|