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 <memory> | 5 #include "src/compiler-dispatcher/compiler-dispatcher.h" |
6 | 6 |
7 #include "src/compiler-dispatcher/compiler-dispatcher.h" | 7 #include "include/v8-platform.h" |
| 8 #include "src/compiler-dispatcher/compiler-dispatcher-job.h" |
8 #include "src/flags.h" | 9 #include "src/flags.h" |
9 #include "src/handles.h" | 10 #include "src/handles.h" |
10 #include "src/objects-inl.h" | 11 #include "src/objects-inl.h" |
11 #include "test/unittests/compiler-dispatcher/compiler-dispatcher-helper.h" | 12 #include "test/unittests/compiler-dispatcher/compiler-dispatcher-helper.h" |
12 #include "test/unittests/test-utils.h" | 13 #include "test/unittests/test-utils.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 namespace v8 { | 16 namespace v8 { |
16 namespace internal { | 17 namespace internal { |
17 | 18 |
18 typedef TestWithContext CompilerDispatcherTest; | 19 typedef TestWithContext CompilerDispatcherTest; |
19 | 20 |
| 21 namespace { |
| 22 |
| 23 class MockPlatform : public v8::Platform { |
| 24 public: |
| 25 MockPlatform() : task_(nullptr), time_(0.0), time_step_(0.0) {} |
| 26 ~MockPlatform() override = default; |
| 27 |
| 28 void CallOnBackgroundThread(Task* task, |
| 29 ExpectedRuntime expected_runtime) override { |
| 30 UNREACHABLE(); |
| 31 } |
| 32 |
| 33 void CallOnForegroundThread(v8::Isolate* isolate, Task* task) override { |
| 34 UNREACHABLE(); |
| 35 } |
| 36 |
| 37 void CallDelayedOnForegroundThread(v8::Isolate* isolate, Task* task, |
| 38 double delay_in_seconds) override { |
| 39 UNREACHABLE(); |
| 40 } |
| 41 |
| 42 void CallIdleOnForegroundThread(v8::Isolate* isolate, |
| 43 IdleTask* task) override { |
| 44 task_ = task; |
| 45 } |
| 46 |
| 47 bool IdleTasksEnabled(v8::Isolate* isolate) override { return true; } |
| 48 |
| 49 double MonotonicallyIncreasingTime() override { |
| 50 time_ += time_step_; |
| 51 return time_; |
| 52 } |
| 53 |
| 54 void RunIdleTask(double deadline_in_seconds, double time_step) { |
| 55 ASSERT_TRUE(task_ != nullptr); |
| 56 time_step_ = time_step; |
| 57 IdleTask* task = task_; |
| 58 task_ = nullptr; |
| 59 task->Run(deadline_in_seconds); |
| 60 delete task; |
| 61 } |
| 62 |
| 63 bool IdleTaskPending() const { return !!task_; } |
| 64 |
| 65 private: |
| 66 IdleTask* task_; |
| 67 double time_; |
| 68 double time_step_; |
| 69 |
| 70 DISALLOW_COPY_AND_ASSIGN(MockPlatform); |
| 71 }; |
| 72 |
| 73 } // namespace |
| 74 |
20 TEST_F(CompilerDispatcherTest, Construct) { | 75 TEST_F(CompilerDispatcherTest, Construct) { |
21 std::unique_ptr<CompilerDispatcher> dispatcher( | 76 MockPlatform platform; |
22 new CompilerDispatcher(i_isolate(), FLAG_stack_size)); | 77 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); |
23 } | 78 } |
24 | 79 |
25 TEST_F(CompilerDispatcherTest, IsEnqueued) { | 80 TEST_F(CompilerDispatcherTest, IsEnqueued) { |
26 std::unique_ptr<CompilerDispatcher> dispatcher( | 81 MockPlatform platform; |
27 new CompilerDispatcher(i_isolate(), FLAG_stack_size)); | 82 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); |
28 | 83 |
29 const char script[] = | 84 const char script[] = |
30 "function g() { var y = 1; function f(x) { return x * y }; return f; } " | 85 "function g() { var y = 1; function f1(x) { return x * y }; return f1; } " |
31 "g();"; | 86 "g();"; |
32 Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); | 87 Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); |
33 Handle<SharedFunctionInfo> shared(f->shared(), i_isolate()); | 88 Handle<SharedFunctionInfo> shared(f->shared(), i_isolate()); |
34 | 89 |
35 ASSERT_FALSE(dispatcher->IsEnqueued(shared)); | 90 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); |
36 ASSERT_TRUE(dispatcher->Enqueue(shared)); | 91 ASSERT_TRUE(dispatcher.Enqueue(shared)); |
37 ASSERT_TRUE(dispatcher->IsEnqueued(shared)); | 92 ASSERT_TRUE(dispatcher.IsEnqueued(shared)); |
38 dispatcher->Abort(shared, CompilerDispatcher::BlockingBehavior::kBlock); | 93 dispatcher.Abort(shared, CompilerDispatcher::BlockingBehavior::kBlock); |
39 ASSERT_FALSE(dispatcher->IsEnqueued(shared)); | 94 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); |
40 } | 95 } |
41 | 96 |
42 TEST_F(CompilerDispatcherTest, FinishNow) { | 97 TEST_F(CompilerDispatcherTest, FinishNow) { |
43 std::unique_ptr<CompilerDispatcher> dispatcher( | 98 MockPlatform platform; |
44 new CompilerDispatcher(i_isolate(), FLAG_stack_size)); | 99 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); |
45 | 100 |
46 const char script[] = | 101 const char script[] = |
47 "function g() { var y = 1; function f(x) { return x * y }; return f; } " | 102 "function g() { var y = 1; function f2(x) { return x * y }; return f2; } " |
48 "g();"; | 103 "g();"; |
49 Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); | 104 Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); |
50 Handle<SharedFunctionInfo> shared(f->shared(), i_isolate()); | 105 Handle<SharedFunctionInfo> shared(f->shared(), i_isolate()); |
51 | 106 |
52 ASSERT_FALSE(shared->HasBaselineCode()); | 107 ASSERT_FALSE(shared->HasBaselineCode()); |
53 ASSERT_TRUE(dispatcher->Enqueue(shared)); | 108 ASSERT_TRUE(dispatcher.Enqueue(shared)); |
54 ASSERT_TRUE(dispatcher->FinishNow(shared)); | 109 ASSERT_TRUE(dispatcher.FinishNow(shared)); |
55 // Finishing removes the SFI from the queue. | 110 // Finishing removes the SFI from the queue. |
56 ASSERT_FALSE(dispatcher->IsEnqueued(shared)); | 111 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); |
57 ASSERT_TRUE(shared->HasBaselineCode()); | 112 ASSERT_TRUE(shared->HasBaselineCode()); |
58 } | 113 } |
59 | 114 |
| 115 TEST_F(CompilerDispatcherTest, IdleTask) { |
| 116 MockPlatform platform; |
| 117 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); |
| 118 |
| 119 const char script[] = |
| 120 "function g() { var y = 1; function f3(x) { return x * y }; return f3; } " |
| 121 "g();"; |
| 122 Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); |
| 123 Handle<SharedFunctionInfo> shared(f->shared(), i_isolate()); |
| 124 |
| 125 ASSERT_FALSE(platform.IdleTaskPending()); |
| 126 ASSERT_TRUE(dispatcher.Enqueue(shared)); |
| 127 ASSERT_TRUE(platform.IdleTaskPending()); |
| 128 |
| 129 // Since time doesn't progress on the MockPlatform, this is enough idle time |
| 130 // to finish compiling the function. |
| 131 platform.RunIdleTask(1000.0, 0.0); |
| 132 |
| 133 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); |
| 134 ASSERT_TRUE(shared->HasBaselineCode()); |
| 135 } |
| 136 |
| 137 TEST_F(CompilerDispatcherTest, IdleTaskSmallIdleTime) { |
| 138 MockPlatform platform; |
| 139 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); |
| 140 |
| 141 const char script[] = |
| 142 "function g() { var y = 1; function f4(x) { return x * y }; return f4; } " |
| 143 "g();"; |
| 144 Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); |
| 145 Handle<SharedFunctionInfo> shared(f->shared(), i_isolate()); |
| 146 |
| 147 ASSERT_FALSE(platform.IdleTaskPending()); |
| 148 ASSERT_TRUE(dispatcher.Enqueue(shared)); |
| 149 ASSERT_TRUE(platform.IdleTaskPending()); |
| 150 |
| 151 // The job should be scheduled for the main thread. |
| 152 ASSERT_EQ(dispatcher.jobs_.size(), 1u); |
| 153 ASSERT_TRUE(dispatcher.jobs_.begin()->second->status() == |
| 154 CompileJobStatus::kInitial); |
| 155 |
| 156 // Only grant a little idle time and have time advance beyond it in one step. |
| 157 platform.RunIdleTask(2.0, 1.0); |
| 158 |
| 159 ASSERT_TRUE(dispatcher.IsEnqueued(shared)); |
| 160 ASSERT_FALSE(shared->HasBaselineCode()); |
| 161 ASSERT_TRUE(platform.IdleTaskPending()); |
| 162 |
| 163 // The job should be still scheduled for the main thread, but ready for |
| 164 // parsing. |
| 165 ASSERT_EQ(dispatcher.jobs_.size(), 1u); |
| 166 ASSERT_TRUE(dispatcher.jobs_.begin()->second->status() == |
| 167 CompileJobStatus::kReadyToParse); |
| 168 |
| 169 // Only grant a lot of idle time and freeze time. |
| 170 platform.RunIdleTask(1000.0, 0.0); |
| 171 |
| 172 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); |
| 173 ASSERT_TRUE(shared->HasBaselineCode()); |
| 174 ASSERT_FALSE(platform.IdleTaskPending()); |
| 175 } |
| 176 |
| 177 TEST_F(CompilerDispatcherTest, IdleTaskException) { |
| 178 MockPlatform platform; |
| 179 CompilerDispatcher dispatcher(i_isolate(), &platform, 50); |
| 180 |
| 181 std::string script("function g() { function f5(x) { var a = "); |
| 182 for (int i = 0; i < 1000; i++) { |
| 183 script += "'x' + "; |
| 184 } |
| 185 script += " 'x'; }; return f5; } g();"; |
| 186 Handle<JSFunction> f = |
| 187 Handle<JSFunction>::cast(RunJS(isolate(), script.c_str())); |
| 188 Handle<SharedFunctionInfo> shared(f->shared(), i_isolate()); |
| 189 |
| 190 ASSERT_FALSE(platform.IdleTaskPending()); |
| 191 ASSERT_TRUE(dispatcher.Enqueue(shared)); |
| 192 ASSERT_TRUE(platform.IdleTaskPending()); |
| 193 |
| 194 // Idle tasks shouldn't leave exceptions behind. |
| 195 v8::TryCatch try_catch(isolate()); |
| 196 |
| 197 // Since time doesn't progress on the MockPlatform, this is enough idle time |
| 198 // to finish compiling the function. |
| 199 platform.RunIdleTask(1000.0, 0.0); |
| 200 |
| 201 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); |
| 202 ASSERT_FALSE(shared->HasBaselineCode()); |
| 203 ASSERT_FALSE(try_catch.HasCaught()); |
| 204 } |
| 205 |
60 } // namespace internal | 206 } // namespace internal |
61 } // namespace v8 | 207 } // namespace v8 |
OLD | NEW |