| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/compiler-dispatcher/compiler-dispatcher.h" | 5 #include "src/compiler-dispatcher/compiler-dispatcher.h" |
| 6 | 6 |
| 7 #include "include/v8-platform.h" | 7 #include "include/v8-platform.h" |
| 8 #include "src/base/platform/semaphore.h" | |
| 9 #include "src/compiler-dispatcher/compiler-dispatcher-job.h" | 8 #include "src/compiler-dispatcher/compiler-dispatcher-job.h" |
| 10 #include "src/compiler-dispatcher/compiler-dispatcher-tracer.h" | |
| 11 #include "src/flags.h" | 9 #include "src/flags.h" |
| 12 #include "src/handles.h" | 10 #include "src/handles.h" |
| 13 #include "src/objects-inl.h" | 11 #include "src/objects-inl.h" |
| 14 #include "src/v8.h" | |
| 15 #include "test/unittests/compiler-dispatcher/compiler-dispatcher-helper.h" | 12 #include "test/unittests/compiler-dispatcher/compiler-dispatcher-helper.h" |
| 16 #include "test/unittests/test-utils.h" | 13 #include "test/unittests/test-utils.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 15 |
| 19 namespace v8 { | 16 namespace v8 { |
| 20 namespace internal { | 17 namespace internal { |
| 21 | 18 |
| 22 class CompilerDispatcherTest : public TestWithContext { | 19 class CompilerDispatcherTest : public TestWithContext { |
| 23 public: | 20 public: |
| 24 CompilerDispatcherTest() = default; | 21 CompilerDispatcherTest() = default; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 36 } | 33 } |
| 37 | 34 |
| 38 private: | 35 private: |
| 39 static bool old_flag_; | 36 static bool old_flag_; |
| 40 | 37 |
| 41 DISALLOW_COPY_AND_ASSIGN(CompilerDispatcherTest); | 38 DISALLOW_COPY_AND_ASSIGN(CompilerDispatcherTest); |
| 42 }; | 39 }; |
| 43 | 40 |
| 44 bool CompilerDispatcherTest::old_flag_; | 41 bool CompilerDispatcherTest::old_flag_; |
| 45 | 42 |
| 46 class IgnitionCompilerDispatcherTest : public CompilerDispatcherTest { | |
| 47 public: | |
| 48 IgnitionCompilerDispatcherTest() = default; | |
| 49 ~IgnitionCompilerDispatcherTest() override = default; | |
| 50 | |
| 51 static void SetUpTestCase() { | |
| 52 old_flag_ = i::FLAG_ignition; | |
| 53 i::FLAG_ignition = true; | |
| 54 CompilerDispatcherTest::SetUpTestCase(); | |
| 55 } | |
| 56 | |
| 57 static void TearDownTestCase() { | |
| 58 CompilerDispatcherTest::TearDownTestCase(); | |
| 59 i::FLAG_ignition = old_flag_; | |
| 60 } | |
| 61 | |
| 62 private: | |
| 63 static bool old_flag_; | |
| 64 DISALLOW_COPY_AND_ASSIGN(IgnitionCompilerDispatcherTest); | |
| 65 }; | |
| 66 | |
| 67 bool IgnitionCompilerDispatcherTest::old_flag_; | |
| 68 | |
| 69 namespace { | 43 namespace { |
| 70 | 44 |
| 71 class MockPlatform : public v8::Platform { | 45 class MockPlatform : public v8::Platform { |
| 72 public: | 46 public: |
| 73 MockPlatform() : idle_task_(nullptr), time_(0.0), time_step_(0.0), sem_(0) {} | 47 MockPlatform() : task_(nullptr), time_(0.0), time_step_(0.0) {} |
| 74 ~MockPlatform() override { | 48 ~MockPlatform() override = default; |
| 75 EXPECT_TRUE(tasks_.empty()); | |
| 76 EXPECT_TRUE(idle_task_ == nullptr); | |
| 77 } | |
| 78 | |
| 79 size_t NumberOfAvailableBackgroundThreads() override { return 1; } | |
| 80 | 49 |
| 81 void CallOnBackgroundThread(Task* task, | 50 void CallOnBackgroundThread(Task* task, |
| 82 ExpectedRuntime expected_runtime) override { | 51 ExpectedRuntime expected_runtime) override { |
| 83 tasks_.push_back(task); | 52 UNREACHABLE(); |
| 84 } | 53 } |
| 85 | 54 |
| 86 void CallOnForegroundThread(v8::Isolate* isolate, Task* task) override { | 55 void CallOnForegroundThread(v8::Isolate* isolate, Task* task) override { |
| 87 UNREACHABLE(); | 56 UNREACHABLE(); |
| 88 } | 57 } |
| 89 | 58 |
| 90 void CallDelayedOnForegroundThread(v8::Isolate* isolate, Task* task, | 59 void CallDelayedOnForegroundThread(v8::Isolate* isolate, Task* task, |
| 91 double delay_in_seconds) override { | 60 double delay_in_seconds) override { |
| 92 UNREACHABLE(); | 61 UNREACHABLE(); |
| 93 } | 62 } |
| 94 | 63 |
| 95 void CallIdleOnForegroundThread(v8::Isolate* isolate, | 64 void CallIdleOnForegroundThread(v8::Isolate* isolate, |
| 96 IdleTask* task) override { | 65 IdleTask* task) override { |
| 97 ASSERT_TRUE(idle_task_ == nullptr); | 66 task_ = task; |
| 98 idle_task_ = task; | |
| 99 } | 67 } |
| 100 | 68 |
| 101 bool IdleTasksEnabled(v8::Isolate* isolate) override { return true; } | 69 bool IdleTasksEnabled(v8::Isolate* isolate) override { return true; } |
| 102 | 70 |
| 103 double MonotonicallyIncreasingTime() override { | 71 double MonotonicallyIncreasingTime() override { |
| 104 time_ += time_step_; | 72 time_ += time_step_; |
| 105 return time_; | 73 return time_; |
| 106 } | 74 } |
| 107 | 75 |
| 108 void RunIdleTask(double deadline_in_seconds, double time_step) { | 76 void RunIdleTask(double deadline_in_seconds, double time_step) { |
| 109 ASSERT_TRUE(idle_task_ != nullptr); | 77 ASSERT_TRUE(task_ != nullptr); |
| 110 time_step_ = time_step; | 78 time_step_ = time_step; |
| 111 IdleTask* task = idle_task_; | 79 IdleTask* task = task_; |
| 112 idle_task_ = nullptr; | 80 task_ = nullptr; |
| 113 task->Run(deadline_in_seconds); | 81 task->Run(deadline_in_seconds); |
| 114 delete task; | 82 delete task; |
| 115 } | 83 } |
| 116 | 84 |
| 117 bool IdleTaskPending() const { return idle_task_; } | 85 bool IdleTaskPending() const { return !!task_; } |
| 118 | |
| 119 bool BackgroundTasksPending() const { return !tasks_.empty(); } | |
| 120 | |
| 121 void RunBackgroundTasksAndBlock(Platform* platform) { | |
| 122 std::vector<Task*> tasks; | |
| 123 tasks.swap(tasks_); | |
| 124 platform->CallOnBackgroundThread(new TaskWrapper(this, tasks, true), | |
| 125 kShortRunningTask); | |
| 126 sem_.Wait(); | |
| 127 } | |
| 128 | |
| 129 void RunBackgroundTasks(Platform* platform) { | |
| 130 std::vector<Task*> tasks; | |
| 131 tasks.swap(tasks_); | |
| 132 platform->CallOnBackgroundThread(new TaskWrapper(this, tasks, false), | |
| 133 kShortRunningTask); | |
| 134 } | |
| 135 | |
| 136 void ClearBackgroundTasks() { | |
| 137 std::vector<Task*> tasks; | |
| 138 tasks.swap(tasks_); | |
| 139 for (auto& task : tasks) { | |
| 140 delete task; | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 void ClearIdleTask() { | |
| 145 ASSERT_TRUE(idle_task_ != nullptr); | |
| 146 delete idle_task_; | |
| 147 idle_task_ = nullptr; | |
| 148 } | |
| 149 | 86 |
| 150 private: | 87 private: |
| 151 class TaskWrapper : public Task { | 88 IdleTask* task_; |
| 152 public: | |
| 153 TaskWrapper(MockPlatform* platform, const std::vector<Task*>& tasks, | |
| 154 bool signal) | |
| 155 : platform_(platform), tasks_(tasks), signal_(signal) {} | |
| 156 ~TaskWrapper() = default; | |
| 157 | |
| 158 void Run() override { | |
| 159 for (auto& task : tasks_) { | |
| 160 task->Run(); | |
| 161 delete task; | |
| 162 } | |
| 163 if (signal_) platform_->sem_.Signal(); | |
| 164 } | |
| 165 | |
| 166 private: | |
| 167 MockPlatform* platform_; | |
| 168 std::vector<Task*> tasks_; | |
| 169 bool signal_; | |
| 170 | |
| 171 DISALLOW_COPY_AND_ASSIGN(TaskWrapper); | |
| 172 }; | |
| 173 | |
| 174 IdleTask* idle_task_; | |
| 175 double time_; | 89 double time_; |
| 176 double time_step_; | 90 double time_step_; |
| 177 | 91 |
| 178 std::vector<Task*> tasks_; | |
| 179 base::Semaphore sem_; | |
| 180 | |
| 181 DISALLOW_COPY_AND_ASSIGN(MockPlatform); | 92 DISALLOW_COPY_AND_ASSIGN(MockPlatform); |
| 182 }; | 93 }; |
| 183 | 94 |
| 184 } // namespace | 95 } // namespace |
| 185 | 96 |
| 186 TEST_F(CompilerDispatcherTest, Construct) { | 97 TEST_F(CompilerDispatcherTest, Construct) { |
| 187 MockPlatform platform; | 98 MockPlatform platform; |
| 188 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); | 99 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); |
| 189 } | 100 } |
| 190 | 101 |
| 191 TEST_F(CompilerDispatcherTest, IsEnqueued) { | 102 TEST_F(CompilerDispatcherTest, IsEnqueued) { |
| 192 MockPlatform platform; | 103 MockPlatform platform; |
| 193 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); | 104 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); |
| 194 | 105 |
| 195 const char script[] = | 106 const char script[] = |
| 196 "function g() { var y = 1; function f1(x) { return x * y }; return f1; } " | 107 "function g() { var y = 1; function f1(x) { return x * y }; return f1; } " |
| 197 "g();"; | 108 "g();"; |
| 198 Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); | 109 Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); |
| 199 Handle<SharedFunctionInfo> shared(f->shared(), i_isolate()); | 110 Handle<SharedFunctionInfo> shared(f->shared(), i_isolate()); |
| 200 | 111 |
| 201 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); | 112 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); |
| 202 ASSERT_TRUE(dispatcher.Enqueue(shared)); | 113 ASSERT_TRUE(dispatcher.Enqueue(shared)); |
| 203 ASSERT_TRUE(dispatcher.IsEnqueued(shared)); | 114 ASSERT_TRUE(dispatcher.IsEnqueued(shared)); |
| 204 dispatcher.AbortAll(CompilerDispatcher::BlockingBehavior::kBlock); | 115 dispatcher.Abort(shared, CompilerDispatcher::BlockingBehavior::kBlock); |
| 205 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); | 116 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); |
| 206 ASSERT_TRUE(platform.IdleTaskPending()); | |
| 207 platform.ClearIdleTask(); | |
| 208 } | 117 } |
| 209 | 118 |
| 210 TEST_F(CompilerDispatcherTest, FinishNow) { | 119 TEST_F(CompilerDispatcherTest, FinishNow) { |
| 211 MockPlatform platform; | 120 MockPlatform platform; |
| 212 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); | 121 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); |
| 213 | 122 |
| 214 const char script[] = | 123 const char script[] = |
| 215 "function g() { var y = 1; function f2(x) { return x * y }; return f2; } " | 124 "function g() { var y = 1; function f2(x) { return x * y }; return f2; } " |
| 216 "g();"; | 125 "g();"; |
| 217 Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); | 126 Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); |
| 218 Handle<SharedFunctionInfo> shared(f->shared(), i_isolate()); | 127 Handle<SharedFunctionInfo> shared(f->shared(), i_isolate()); |
| 219 | 128 |
| 220 ASSERT_FALSE(shared->is_compiled()); | 129 ASSERT_FALSE(shared->is_compiled()); |
| 221 ASSERT_TRUE(dispatcher.Enqueue(shared)); | 130 ASSERT_TRUE(dispatcher.Enqueue(shared)); |
| 222 ASSERT_TRUE(dispatcher.FinishNow(shared)); | 131 ASSERT_TRUE(dispatcher.FinishNow(shared)); |
| 223 // Finishing removes the SFI from the queue. | 132 // Finishing removes the SFI from the queue. |
| 224 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); | 133 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); |
| 225 ASSERT_TRUE(shared->is_compiled()); | 134 ASSERT_TRUE(shared->is_compiled()); |
| 226 ASSERT_TRUE(platform.IdleTaskPending()); | |
| 227 platform.ClearIdleTask(); | |
| 228 } | 135 } |
| 229 | 136 |
| 230 TEST_F(CompilerDispatcherTest, IdleTask) { | 137 TEST_F(CompilerDispatcherTest, IdleTask) { |
| 231 MockPlatform platform; | 138 MockPlatform platform; |
| 232 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); | 139 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); |
| 233 | 140 |
| 234 const char script[] = | 141 const char script[] = |
| 235 "function g() { var y = 1; function f3(x) { return x * y }; return f3; } " | 142 "function g() { var y = 1; function f3(x) { return x * y }; return f3; } " |
| 236 "g();"; | 143 "g();"; |
| 237 Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); | 144 Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 ASSERT_TRUE(dispatcher.IsEnqueued(shared)); | 181 ASSERT_TRUE(dispatcher.IsEnqueued(shared)); |
| 275 ASSERT_FALSE(shared->is_compiled()); | 182 ASSERT_FALSE(shared->is_compiled()); |
| 276 ASSERT_TRUE(platform.IdleTaskPending()); | 183 ASSERT_TRUE(platform.IdleTaskPending()); |
| 277 | 184 |
| 278 // The job should be still scheduled for the main thread, but ready for | 185 // The job should be still scheduled for the main thread, but ready for |
| 279 // parsing. | 186 // parsing. |
| 280 ASSERT_EQ(dispatcher.jobs_.size(), 1u); | 187 ASSERT_EQ(dispatcher.jobs_.size(), 1u); |
| 281 ASSERT_TRUE(dispatcher.jobs_.begin()->second->status() == | 188 ASSERT_TRUE(dispatcher.jobs_.begin()->second->status() == |
| 282 CompileJobStatus::kReadyToParse); | 189 CompileJobStatus::kReadyToParse); |
| 283 | 190 |
| 284 // Now grant a lot of idle time and freeze time. | 191 // Only grant a lot of idle time and freeze time. |
| 285 platform.RunIdleTask(1000.0, 0.0); | 192 platform.RunIdleTask(1000.0, 0.0); |
| 286 | 193 |
| 287 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); | 194 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); |
| 288 ASSERT_TRUE(shared->is_compiled()); | 195 ASSERT_TRUE(shared->is_compiled()); |
| 289 ASSERT_FALSE(platform.IdleTaskPending()); | 196 ASSERT_FALSE(platform.IdleTaskPending()); |
| 290 } | 197 } |
| 291 | 198 |
| 292 TEST_F(CompilerDispatcherTest, IdleTaskException) { | 199 TEST_F(CompilerDispatcherTest, IdleTaskException) { |
| 293 MockPlatform platform; | 200 MockPlatform platform; |
| 294 CompilerDispatcher dispatcher(i_isolate(), &platform, 50); | 201 CompilerDispatcher dispatcher(i_isolate(), &platform, 50); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 311 | 218 |
| 312 // Since time doesn't progress on the MockPlatform, this is enough idle time | 219 // Since time doesn't progress on the MockPlatform, this is enough idle time |
| 313 // to finish compiling the function. | 220 // to finish compiling the function. |
| 314 platform.RunIdleTask(1000.0, 0.0); | 221 platform.RunIdleTask(1000.0, 0.0); |
| 315 | 222 |
| 316 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); | 223 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); |
| 317 ASSERT_FALSE(shared->is_compiled()); | 224 ASSERT_FALSE(shared->is_compiled()); |
| 318 ASSERT_FALSE(try_catch.HasCaught()); | 225 ASSERT_FALSE(try_catch.HasCaught()); |
| 319 } | 226 } |
| 320 | 227 |
| 321 TEST_F(IgnitionCompilerDispatcherTest, CompileOnBackgroundThread) { | |
| 322 MockPlatform platform; | |
| 323 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); | |
| 324 | |
| 325 const char script[] = | |
| 326 "function g() { var y = 1; function f6(x) { return x * y }; return f6; } " | |
| 327 "g();"; | |
| 328 Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); | |
| 329 Handle<SharedFunctionInfo> shared(f->shared(), i_isolate()); | |
| 330 | |
| 331 ASSERT_FALSE(platform.IdleTaskPending()); | |
| 332 ASSERT_TRUE(dispatcher.Enqueue(shared)); | |
| 333 ASSERT_TRUE(platform.IdleTaskPending()); | |
| 334 | |
| 335 ASSERT_EQ(dispatcher.jobs_.size(), 1u); | |
| 336 ASSERT_TRUE(dispatcher.jobs_.begin()->second->status() == | |
| 337 CompileJobStatus::kInitial); | |
| 338 | |
| 339 // Make compiling super expensive, and advance job as much as possible on the | |
| 340 // foreground thread. | |
| 341 dispatcher.tracer_->RecordCompile(50000.0, 1); | |
| 342 platform.RunIdleTask(10.0, 0.0); | |
| 343 ASSERT_TRUE(dispatcher.jobs_.begin()->second->status() == | |
| 344 CompileJobStatus::kReadyToCompile); | |
| 345 | |
| 346 ASSERT_TRUE(dispatcher.IsEnqueued(shared)); | |
| 347 ASSERT_FALSE(shared->is_compiled()); | |
| 348 ASSERT_FALSE(platform.IdleTaskPending()); | |
| 349 ASSERT_TRUE(platform.BackgroundTasksPending()); | |
| 350 | |
| 351 platform.RunBackgroundTasksAndBlock(V8::GetCurrentPlatform()); | |
| 352 | |
| 353 ASSERT_TRUE(platform.IdleTaskPending()); | |
| 354 ASSERT_FALSE(platform.BackgroundTasksPending()); | |
| 355 ASSERT_TRUE(dispatcher.jobs_.begin()->second->status() == | |
| 356 CompileJobStatus::kCompiled); | |
| 357 | |
| 358 // Now grant a lot of idle time and freeze time. | |
| 359 platform.RunIdleTask(1000.0, 0.0); | |
| 360 | |
| 361 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); | |
| 362 ASSERT_TRUE(shared->is_compiled()); | |
| 363 ASSERT_FALSE(platform.IdleTaskPending()); | |
| 364 } | |
| 365 | |
| 366 TEST_F(IgnitionCompilerDispatcherTest, FinishNowWithBackgroundTask) { | |
| 367 MockPlatform platform; | |
| 368 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); | |
| 369 | |
| 370 const char script[] = | |
| 371 "function g() { var y = 1; function f7(x) { return x * y }; return f7; } " | |
| 372 "g();"; | |
| 373 Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); | |
| 374 Handle<SharedFunctionInfo> shared(f->shared(), i_isolate()); | |
| 375 | |
| 376 ASSERT_FALSE(platform.IdleTaskPending()); | |
| 377 ASSERT_TRUE(dispatcher.Enqueue(shared)); | |
| 378 ASSERT_TRUE(platform.IdleTaskPending()); | |
| 379 | |
| 380 ASSERT_EQ(dispatcher.jobs_.size(), 1u); | |
| 381 ASSERT_TRUE(dispatcher.jobs_.begin()->second->status() == | |
| 382 CompileJobStatus::kInitial); | |
| 383 | |
| 384 // Make compiling super expensive, and advance job as much as possible on the | |
| 385 // foreground thread. | |
| 386 dispatcher.tracer_->RecordCompile(50000.0, 1); | |
| 387 platform.RunIdleTask(10.0, 0.0); | |
| 388 ASSERT_TRUE(dispatcher.jobs_.begin()->second->status() == | |
| 389 CompileJobStatus::kReadyToCompile); | |
| 390 | |
| 391 ASSERT_TRUE(dispatcher.IsEnqueued(shared)); | |
| 392 ASSERT_FALSE(shared->is_compiled()); | |
| 393 ASSERT_FALSE(platform.IdleTaskPending()); | |
| 394 ASSERT_TRUE(platform.BackgroundTasksPending()); | |
| 395 | |
| 396 // This does not block, but races with the FinishNow() call below. | |
| 397 platform.RunBackgroundTasks(V8::GetCurrentPlatform()); | |
| 398 | |
| 399 ASSERT_TRUE(dispatcher.FinishNow(shared)); | |
| 400 // Finishing removes the SFI from the queue. | |
| 401 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); | |
| 402 ASSERT_TRUE(shared->is_compiled()); | |
| 403 ASSERT_FALSE(platform.IdleTaskPending()); | |
| 404 ASSERT_FALSE(platform.BackgroundTasksPending()); | |
| 405 } | |
| 406 | |
| 407 } // namespace internal | 228 } // namespace internal |
| 408 } // namespace v8 | 229 } // namespace v8 |
| OLD | NEW |