| 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" |
| 8 #include "src/compiler-dispatcher/compiler-dispatcher-job.h" | 9 #include "src/compiler-dispatcher/compiler-dispatcher-job.h" |
| 10 #include "src/compiler-dispatcher/compiler-dispatcher-tracer.h" |
| 9 #include "src/flags.h" | 11 #include "src/flags.h" |
| 10 #include "src/handles.h" | 12 #include "src/handles.h" |
| 11 #include "src/objects-inl.h" | 13 #include "src/objects-inl.h" |
| 14 #include "src/v8.h" |
| 12 #include "test/unittests/compiler-dispatcher/compiler-dispatcher-helper.h" | 15 #include "test/unittests/compiler-dispatcher/compiler-dispatcher-helper.h" |
| 13 #include "test/unittests/test-utils.h" | 16 #include "test/unittests/test-utils.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 18 |
| 16 namespace v8 { | 19 namespace v8 { |
| 17 namespace internal { | 20 namespace internal { |
| 18 | 21 |
| 19 class CompilerDispatcherTest : public TestWithContext { | 22 class CompilerDispatcherTest : public TestWithContext { |
| 20 public: | 23 public: |
| 21 CompilerDispatcherTest() = default; | 24 CompilerDispatcherTest() = default; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 33 } | 36 } |
| 34 | 37 |
| 35 private: | 38 private: |
| 36 static bool old_flag_; | 39 static bool old_flag_; |
| 37 | 40 |
| 38 DISALLOW_COPY_AND_ASSIGN(CompilerDispatcherTest); | 41 DISALLOW_COPY_AND_ASSIGN(CompilerDispatcherTest); |
| 39 }; | 42 }; |
| 40 | 43 |
| 41 bool CompilerDispatcherTest::old_flag_; | 44 bool CompilerDispatcherTest::old_flag_; |
| 42 | 45 |
| 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 |
| 43 namespace { | 69 namespace { |
| 44 | 70 |
| 45 class MockPlatform : public v8::Platform { | 71 class MockPlatform : public v8::Platform { |
| 46 public: | 72 public: |
| 47 MockPlatform() : task_(nullptr), time_(0.0), time_step_(0.0) {} | 73 MockPlatform() : idle_task_(nullptr), time_(0.0), time_step_(0.0), sem_(0) {} |
| 48 ~MockPlatform() override = default; | 74 ~MockPlatform() override { |
| 75 EXPECT_TRUE(tasks_.empty()); |
| 76 EXPECT_TRUE(idle_task_ == nullptr); |
| 77 } |
| 78 |
| 79 size_t NumberOfAvailableBackgroundThreads() override { return 1; } |
| 49 | 80 |
| 50 void CallOnBackgroundThread(Task* task, | 81 void CallOnBackgroundThread(Task* task, |
| 51 ExpectedRuntime expected_runtime) override { | 82 ExpectedRuntime expected_runtime) override { |
| 52 UNREACHABLE(); | 83 tasks_.push_back(task); |
| 53 } | 84 } |
| 54 | 85 |
| 55 void CallOnForegroundThread(v8::Isolate* isolate, Task* task) override { | 86 void CallOnForegroundThread(v8::Isolate* isolate, Task* task) override { |
| 56 UNREACHABLE(); | 87 UNREACHABLE(); |
| 57 } | 88 } |
| 58 | 89 |
| 59 void CallDelayedOnForegroundThread(v8::Isolate* isolate, Task* task, | 90 void CallDelayedOnForegroundThread(v8::Isolate* isolate, Task* task, |
| 60 double delay_in_seconds) override { | 91 double delay_in_seconds) override { |
| 61 UNREACHABLE(); | 92 UNREACHABLE(); |
| 62 } | 93 } |
| 63 | 94 |
| 64 void CallIdleOnForegroundThread(v8::Isolate* isolate, | 95 void CallIdleOnForegroundThread(v8::Isolate* isolate, |
| 65 IdleTask* task) override { | 96 IdleTask* task) override { |
| 66 task_ = task; | 97 ASSERT_TRUE(idle_task_ == nullptr); |
| 98 idle_task_ = task; |
| 67 } | 99 } |
| 68 | 100 |
| 69 bool IdleTasksEnabled(v8::Isolate* isolate) override { return true; } | 101 bool IdleTasksEnabled(v8::Isolate* isolate) override { return true; } |
| 70 | 102 |
| 71 double MonotonicallyIncreasingTime() override { | 103 double MonotonicallyIncreasingTime() override { |
| 72 time_ += time_step_; | 104 time_ += time_step_; |
| 73 return time_; | 105 return time_; |
| 74 } | 106 } |
| 75 | 107 |
| 76 void RunIdleTask(double deadline_in_seconds, double time_step) { | 108 void RunIdleTask(double deadline_in_seconds, double time_step) { |
| 77 ASSERT_TRUE(task_ != nullptr); | 109 ASSERT_TRUE(idle_task_ != nullptr); |
| 78 time_step_ = time_step; | 110 time_step_ = time_step; |
| 79 IdleTask* task = task_; | 111 IdleTask* task = idle_task_; |
| 80 task_ = nullptr; | 112 idle_task_ = nullptr; |
| 81 task->Run(deadline_in_seconds); | 113 task->Run(deadline_in_seconds); |
| 82 delete task; | 114 delete task; |
| 83 } | 115 } |
| 84 | 116 |
| 85 bool IdleTaskPending() const { return !!task_; } | 117 bool IdleTaskPending() const { return idle_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 } |
| 86 | 149 |
| 87 private: | 150 private: |
| 88 IdleTask* task_; | 151 class TaskWrapper : public 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_; |
| 89 double time_; | 175 double time_; |
| 90 double time_step_; | 176 double time_step_; |
| 91 | 177 |
| 178 std::vector<Task*> tasks_; |
| 179 base::Semaphore sem_; |
| 180 |
| 92 DISALLOW_COPY_AND_ASSIGN(MockPlatform); | 181 DISALLOW_COPY_AND_ASSIGN(MockPlatform); |
| 93 }; | 182 }; |
| 94 | 183 |
| 95 } // namespace | 184 } // namespace |
| 96 | 185 |
| 97 TEST_F(CompilerDispatcherTest, Construct) { | 186 TEST_F(CompilerDispatcherTest, Construct) { |
| 98 MockPlatform platform; | 187 MockPlatform platform; |
| 99 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); | 188 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); |
| 100 } | 189 } |
| 101 | 190 |
| 102 TEST_F(CompilerDispatcherTest, IsEnqueued) { | 191 TEST_F(CompilerDispatcherTest, IsEnqueued) { |
| 103 MockPlatform platform; | 192 MockPlatform platform; |
| 104 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); | 193 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); |
| 105 | 194 |
| 106 const char script[] = | 195 const char script[] = |
| 107 "function g() { var y = 1; function f1(x) { return x * y }; return f1; } " | 196 "function g() { var y = 1; function f1(x) { return x * y }; return f1; } " |
| 108 "g();"; | 197 "g();"; |
| 109 Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); | 198 Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); |
| 110 Handle<SharedFunctionInfo> shared(f->shared(), i_isolate()); | 199 Handle<SharedFunctionInfo> shared(f->shared(), i_isolate()); |
| 111 | 200 |
| 112 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); | 201 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); |
| 113 ASSERT_TRUE(dispatcher.Enqueue(shared)); | 202 ASSERT_TRUE(dispatcher.Enqueue(shared)); |
| 114 ASSERT_TRUE(dispatcher.IsEnqueued(shared)); | 203 ASSERT_TRUE(dispatcher.IsEnqueued(shared)); |
| 115 dispatcher.Abort(shared, CompilerDispatcher::BlockingBehavior::kBlock); | 204 dispatcher.AbortAll(CompilerDispatcher::BlockingBehavior::kBlock); |
| 116 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); | 205 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); |
| 206 ASSERT_TRUE(platform.IdleTaskPending()); |
| 207 platform.ClearIdleTask(); |
| 117 } | 208 } |
| 118 | 209 |
| 119 TEST_F(CompilerDispatcherTest, FinishNow) { | 210 TEST_F(CompilerDispatcherTest, FinishNow) { |
| 120 MockPlatform platform; | 211 MockPlatform platform; |
| 121 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); | 212 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); |
| 122 | 213 |
| 123 const char script[] = | 214 const char script[] = |
| 124 "function g() { var y = 1; function f2(x) { return x * y }; return f2; } " | 215 "function g() { var y = 1; function f2(x) { return x * y }; return f2; } " |
| 125 "g();"; | 216 "g();"; |
| 126 Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); | 217 Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); |
| 127 Handle<SharedFunctionInfo> shared(f->shared(), i_isolate()); | 218 Handle<SharedFunctionInfo> shared(f->shared(), i_isolate()); |
| 128 | 219 |
| 129 ASSERT_FALSE(shared->is_compiled()); | 220 ASSERT_FALSE(shared->is_compiled()); |
| 130 ASSERT_TRUE(dispatcher.Enqueue(shared)); | 221 ASSERT_TRUE(dispatcher.Enqueue(shared)); |
| 131 ASSERT_TRUE(dispatcher.FinishNow(shared)); | 222 ASSERT_TRUE(dispatcher.FinishNow(shared)); |
| 132 // Finishing removes the SFI from the queue. | 223 // Finishing removes the SFI from the queue. |
| 133 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); | 224 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); |
| 134 ASSERT_TRUE(shared->is_compiled()); | 225 ASSERT_TRUE(shared->is_compiled()); |
| 226 ASSERT_TRUE(platform.IdleTaskPending()); |
| 227 platform.ClearIdleTask(); |
| 135 } | 228 } |
| 136 | 229 |
| 137 TEST_F(CompilerDispatcherTest, IdleTask) { | 230 TEST_F(CompilerDispatcherTest, IdleTask) { |
| 138 MockPlatform platform; | 231 MockPlatform platform; |
| 139 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); | 232 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); |
| 140 | 233 |
| 141 const char script[] = | 234 const char script[] = |
| 142 "function g() { var y = 1; function f3(x) { return x * y }; return f3; } " | 235 "function g() { var y = 1; function f3(x) { return x * y }; return f3; } " |
| 143 "g();"; | 236 "g();"; |
| 144 Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); | 237 Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 ASSERT_TRUE(dispatcher.IsEnqueued(shared)); | 274 ASSERT_TRUE(dispatcher.IsEnqueued(shared)); |
| 182 ASSERT_FALSE(shared->is_compiled()); | 275 ASSERT_FALSE(shared->is_compiled()); |
| 183 ASSERT_TRUE(platform.IdleTaskPending()); | 276 ASSERT_TRUE(platform.IdleTaskPending()); |
| 184 | 277 |
| 185 // The job should be still scheduled for the main thread, but ready for | 278 // The job should be still scheduled for the main thread, but ready for |
| 186 // parsing. | 279 // parsing. |
| 187 ASSERT_EQ(dispatcher.jobs_.size(), 1u); | 280 ASSERT_EQ(dispatcher.jobs_.size(), 1u); |
| 188 ASSERT_TRUE(dispatcher.jobs_.begin()->second->status() == | 281 ASSERT_TRUE(dispatcher.jobs_.begin()->second->status() == |
| 189 CompileJobStatus::kReadyToParse); | 282 CompileJobStatus::kReadyToParse); |
| 190 | 283 |
| 191 // Only grant a lot of idle time and freeze time. | 284 // Now grant a lot of idle time and freeze time. |
| 192 platform.RunIdleTask(1000.0, 0.0); | 285 platform.RunIdleTask(1000.0, 0.0); |
| 193 | 286 |
| 194 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); | 287 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); |
| 195 ASSERT_TRUE(shared->is_compiled()); | 288 ASSERT_TRUE(shared->is_compiled()); |
| 196 ASSERT_FALSE(platform.IdleTaskPending()); | 289 ASSERT_FALSE(platform.IdleTaskPending()); |
| 197 } | 290 } |
| 198 | 291 |
| 199 TEST_F(CompilerDispatcherTest, IdleTaskException) { | 292 TEST_F(CompilerDispatcherTest, IdleTaskException) { |
| 200 MockPlatform platform; | 293 MockPlatform platform; |
| 201 CompilerDispatcher dispatcher(i_isolate(), &platform, 50); | 294 CompilerDispatcher dispatcher(i_isolate(), &platform, 50); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 218 | 311 |
| 219 // Since time doesn't progress on the MockPlatform, this is enough idle time | 312 // Since time doesn't progress on the MockPlatform, this is enough idle time |
| 220 // to finish compiling the function. | 313 // to finish compiling the function. |
| 221 platform.RunIdleTask(1000.0, 0.0); | 314 platform.RunIdleTask(1000.0, 0.0); |
| 222 | 315 |
| 223 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); | 316 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); |
| 224 ASSERT_FALSE(shared->is_compiled()); | 317 ASSERT_FALSE(shared->is_compiled()); |
| 225 ASSERT_FALSE(try_catch.HasCaught()); | 318 ASSERT_FALSE(try_catch.HasCaught()); |
| 226 } | 319 } |
| 227 | 320 |
| 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 if (platform.IdleTaskPending()) platform.ClearIdleTask(); |
| 404 ASSERT_FALSE(platform.BackgroundTasksPending()); |
| 405 } |
| 406 |
| 228 } // namespace internal | 407 } // namespace internal |
| 229 } // namespace v8 | 408 } // namespace v8 |
| OLD | NEW |