2016-12-12 15:35:41 +00:00
|
|
|
// Copyright 2016 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 "src/compiler-dispatcher/compiler-dispatcher.h"
|
2016-12-16 14:40:32 +00:00
|
|
|
|
2017-04-04 15:44:44 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
2016-12-16 14:40:32 +00:00
|
|
|
#include "include/v8-platform.h"
|
2019-05-17 12:13:44 +00:00
|
|
|
#include "src/api/api-inl.h"
|
2017-07-25 10:42:08 +00:00
|
|
|
#include "src/ast/ast-value-factory.h"
|
2018-09-20 13:18:16 +00:00
|
|
|
#include "src/ast/ast.h"
|
|
|
|
#include "src/ast/scopes.h"
|
2017-01-03 17:00:56 +00:00
|
|
|
#include "src/base/platform/semaphore.h"
|
2018-03-26 16:44:23 +00:00
|
|
|
#include "src/base/template-utils.h"
|
2019-05-21 09:30:15 +00:00
|
|
|
#include "src/codegen/compiler.h"
|
2019-05-24 13:51:59 +00:00
|
|
|
#include "src/flags/flags.h"
|
2019-05-22 12:44:24 +00:00
|
|
|
#include "src/handles/handles.h"
|
2019-05-24 13:51:59 +00:00
|
|
|
#include "src/init/v8.h"
|
2019-05-23 08:51:46 +00:00
|
|
|
#include "src/objects/objects-inl.h"
|
2017-01-25 22:41:57 +00:00
|
|
|
#include "src/parsing/parse-info.h"
|
2017-02-10 15:01:29 +00:00
|
|
|
#include "src/parsing/parsing.h"
|
2019-02-14 21:10:30 +00:00
|
|
|
#include "src/zone/zone-list-inl.h"
|
2017-04-18 14:01:12 +00:00
|
|
|
#include "test/unittests/test-helpers.h"
|
2016-12-12 15:35:41 +00:00
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2017-02-10 16:01:38 +00:00
|
|
|
class CompilerDispatcherTestFlags {
|
|
|
|
public:
|
|
|
|
static void SetFlagsForTest() {
|
2017-03-14 13:33:13 +00:00
|
|
|
CHECK_NULL(save_flags_);
|
|
|
|
save_flags_ = new SaveFlags();
|
|
|
|
FLAG_single_threaded = true;
|
|
|
|
FlagList::EnforceFlagImplications();
|
|
|
|
FLAG_compiler_dispatcher = true;
|
2017-02-10 16:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void RestoreFlags() {
|
2017-03-14 13:33:13 +00:00
|
|
|
CHECK_NOT_NULL(save_flags_);
|
|
|
|
delete save_flags_;
|
|
|
|
save_flags_ = nullptr;
|
2017-02-10 16:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-03-14 13:33:13 +00:00
|
|
|
static SaveFlags* save_flags_;
|
2017-02-10 16:01:38 +00:00
|
|
|
|
|
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(CompilerDispatcherTestFlags);
|
|
|
|
};
|
|
|
|
|
2017-03-14 13:33:13 +00:00
|
|
|
SaveFlags* CompilerDispatcherTestFlags::save_flags_ = nullptr;
|
2017-02-10 16:01:38 +00:00
|
|
|
|
2017-11-13 12:04:57 +00:00
|
|
|
class CompilerDispatcherTest : public TestWithNativeContext {
|
2017-01-02 12:59:07 +00:00
|
|
|
public:
|
|
|
|
CompilerDispatcherTest() = default;
|
|
|
|
~CompilerDispatcherTest() override = default;
|
|
|
|
|
|
|
|
static void SetUpTestCase() {
|
2017-02-10 16:01:38 +00:00
|
|
|
CompilerDispatcherTestFlags::SetFlagsForTest();
|
2019-03-08 12:19:31 +00:00
|
|
|
TestWithNativeContext::SetUpTestCase();
|
2017-01-02 12:59:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void TearDownTestCase() {
|
2019-03-08 12:19:31 +00:00
|
|
|
TestWithNativeContext::TearDownTestCase();
|
2017-02-10 16:01:38 +00:00
|
|
|
CompilerDispatcherTestFlags::RestoreFlags();
|
2017-01-02 12:59:07 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
static base::Optional<CompilerDispatcher::JobId> EnqueueUnoptimizedCompileJob(
|
|
|
|
CompilerDispatcher* dispatcher, Isolate* isolate,
|
|
|
|
Handle<SharedFunctionInfo> shared) {
|
|
|
|
std::unique_ptr<ParseInfo> outer_parse_info =
|
|
|
|
test::OuterParseInfoForShared(isolate, shared);
|
|
|
|
AstValueFactory* ast_value_factory =
|
|
|
|
outer_parse_info->GetOrCreateAstValueFactory();
|
|
|
|
AstNodeFactory ast_node_factory(ast_value_factory,
|
|
|
|
outer_parse_info->zone());
|
|
|
|
|
|
|
|
const AstRawString* function_name =
|
|
|
|
ast_value_factory->GetOneByteString("f");
|
|
|
|
DeclarationScope* script_scope = new (outer_parse_info->zone())
|
|
|
|
DeclarationScope(outer_parse_info->zone(), ast_value_factory);
|
|
|
|
DeclarationScope* function_scope =
|
|
|
|
new (outer_parse_info->zone()) DeclarationScope(
|
|
|
|
outer_parse_info->zone(), script_scope, FUNCTION_SCOPE);
|
|
|
|
function_scope->set_start_position(shared->StartPosition());
|
|
|
|
function_scope->set_end_position(shared->EndPosition());
|
2018-11-05 15:20:49 +00:00
|
|
|
std::vector<void*> pointer_buffer;
|
|
|
|
ScopedPtrList<Statement> statements(&pointer_buffer);
|
2018-09-20 13:18:16 +00:00
|
|
|
const FunctionLiteral* function_literal =
|
|
|
|
ast_node_factory.NewFunctionLiteral(
|
2018-11-05 15:20:49 +00:00
|
|
|
function_name, function_scope, statements, -1, -1, -1,
|
2018-09-20 13:18:16 +00:00
|
|
|
FunctionLiteral::kNoDuplicateParameters,
|
|
|
|
FunctionLiteral::kAnonymousExpression,
|
|
|
|
FunctionLiteral::kShouldEagerCompile, shared->StartPosition(), true,
|
2019-06-21 15:43:35 +00:00
|
|
|
shared->function_literal_id(), nullptr);
|
2018-09-20 13:18:16 +00:00
|
|
|
|
|
|
|
return dispatcher->Enqueue(outer_parse_info.get(), function_name,
|
|
|
|
function_literal);
|
|
|
|
}
|
|
|
|
|
2017-01-02 12:59:07 +00:00
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CompilerDispatcherTest);
|
|
|
|
};
|
|
|
|
|
2016-12-16 14:40:32 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class MockPlatform : public v8::Platform {
|
|
|
|
public:
|
2017-08-01 14:16:30 +00:00
|
|
|
MockPlatform()
|
2017-07-03 08:23:26 +00:00
|
|
|
: time_(0.0),
|
|
|
|
time_step_(0.0),
|
|
|
|
idle_task_(nullptr),
|
|
|
|
sem_(0),
|
2017-08-01 14:16:30 +00:00
|
|
|
tracing_controller_(V8::GetCurrentPlatform()->GetTracingController()) {}
|
2017-01-03 17:00:56 +00:00
|
|
|
~MockPlatform() override {
|
2018-10-12 13:52:49 +00:00
|
|
|
base::MutexGuard lock(&mutex_);
|
2017-01-04 13:45:56 +00:00
|
|
|
EXPECT_TRUE(foreground_tasks_.empty());
|
2018-03-26 16:44:23 +00:00
|
|
|
EXPECT_TRUE(worker_tasks_.empty());
|
2017-01-03 17:00:56 +00:00
|
|
|
EXPECT_TRUE(idle_task_ == nullptr);
|
|
|
|
}
|
|
|
|
|
2018-03-01 12:05:15 +00:00
|
|
|
int NumberOfWorkerThreads() override { return 1; }
|
2016-12-16 14:40:32 +00:00
|
|
|
|
2017-11-15 14:26:16 +00:00
|
|
|
std::shared_ptr<TaskRunner> GetForegroundTaskRunner(
|
|
|
|
v8::Isolate* isolate) override {
|
2018-04-30 16:52:06 +00:00
|
|
|
return std::make_shared<MockForegroundTaskRunner>(this);
|
2017-11-15 14:26:16 +00:00
|
|
|
}
|
|
|
|
|
2018-03-26 16:44:23 +00:00
|
|
|
void CallOnWorkerThread(std::unique_ptr<Task> task) override {
|
2018-10-12 13:52:49 +00:00
|
|
|
base::MutexGuard lock(&mutex_);
|
2018-03-26 16:44:23 +00:00
|
|
|
worker_tasks_.push_back(std::move(task));
|
2016-12-16 14:40:32 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 19:14:50 +00:00
|
|
|
void CallDelayedOnWorkerThread(std::unique_ptr<Task> task,
|
|
|
|
double delay_in_seconds) override {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
2016-12-16 14:40:32 +00:00
|
|
|
void CallOnForegroundThread(v8::Isolate* isolate, Task* task) override {
|
2018-10-12 13:52:49 +00:00
|
|
|
base::MutexGuard lock(&mutex_);
|
2018-03-26 16:44:23 +00:00
|
|
|
foreground_tasks_.push_back(std::unique_ptr<Task>(task));
|
2016-12-16 14:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CallDelayedOnForegroundThread(v8::Isolate* isolate, Task* task,
|
|
|
|
double delay_in_seconds) override {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallIdleOnForegroundThread(v8::Isolate* isolate,
|
|
|
|
IdleTask* task) override {
|
2018-10-12 13:52:49 +00:00
|
|
|
base::MutexGuard lock(&mutex_);
|
2017-01-03 17:00:56 +00:00
|
|
|
ASSERT_TRUE(idle_task_ == nullptr);
|
|
|
|
idle_task_ = task;
|
2016-12-16 14:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IdleTasksEnabled(v8::Isolate* isolate) override { return true; }
|
|
|
|
|
|
|
|
double MonotonicallyIncreasingTime() override {
|
|
|
|
time_ += time_step_;
|
|
|
|
return time_;
|
|
|
|
}
|
|
|
|
|
2017-08-25 07:12:51 +00:00
|
|
|
double CurrentClockTimeMillis() override {
|
|
|
|
return time_ * base::Time::kMillisecondsPerSecond;
|
|
|
|
}
|
|
|
|
|
2017-07-03 08:23:26 +00:00
|
|
|
v8::TracingController* GetTracingController() override {
|
|
|
|
return tracing_controller_;
|
|
|
|
}
|
|
|
|
|
2016-12-16 14:40:32 +00:00
|
|
|
void RunIdleTask(double deadline_in_seconds, double time_step) {
|
|
|
|
time_step_ = time_step;
|
2017-01-04 13:45:56 +00:00
|
|
|
IdleTask* task;
|
|
|
|
{
|
2018-10-12 13:52:49 +00:00
|
|
|
base::MutexGuard lock(&mutex_);
|
2017-01-04 13:45:56 +00:00
|
|
|
task = idle_task_;
|
|
|
|
ASSERT_TRUE(idle_task_ != nullptr);
|
|
|
|
idle_task_ = nullptr;
|
|
|
|
}
|
2016-12-16 14:40:32 +00:00
|
|
|
task->Run(deadline_in_seconds);
|
|
|
|
delete task;
|
|
|
|
}
|
|
|
|
|
2017-01-04 13:45:56 +00:00
|
|
|
bool IdleTaskPending() {
|
2018-10-12 13:52:49 +00:00
|
|
|
base::MutexGuard lock(&mutex_);
|
2017-01-04 13:45:56 +00:00
|
|
|
return idle_task_;
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:44:23 +00:00
|
|
|
bool WorkerTasksPending() {
|
2018-10-12 13:52:49 +00:00
|
|
|
base::MutexGuard lock(&mutex_);
|
2018-03-26 16:44:23 +00:00
|
|
|
return !worker_tasks_.empty();
|
2017-01-04 13:45:56 +00:00
|
|
|
}
|
2017-01-03 17:00:56 +00:00
|
|
|
|
2017-01-04 13:45:56 +00:00
|
|
|
bool ForegroundTasksPending() {
|
2018-10-12 13:52:49 +00:00
|
|
|
base::MutexGuard lock(&mutex_);
|
2017-01-04 13:45:56 +00:00
|
|
|
return !foreground_tasks_.empty();
|
|
|
|
}
|
2017-01-03 17:00:56 +00:00
|
|
|
|
2018-03-26 16:44:23 +00:00
|
|
|
void RunWorkerTasksAndBlock(Platform* platform) {
|
|
|
|
std::vector<std::unique_ptr<Task>> tasks;
|
2017-01-04 13:45:56 +00:00
|
|
|
{
|
2018-10-12 13:52:49 +00:00
|
|
|
base::MutexGuard lock(&mutex_);
|
2018-03-26 16:44:23 +00:00
|
|
|
tasks.swap(worker_tasks_);
|
2017-01-04 13:45:56 +00:00
|
|
|
}
|
2018-03-26 16:44:23 +00:00
|
|
|
platform->CallOnWorkerThread(
|
|
|
|
base::make_unique<TaskWrapper>(this, std::move(tasks), true));
|
2017-01-03 17:00:56 +00:00
|
|
|
sem_.Wait();
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:44:23 +00:00
|
|
|
void RunWorkerTasks(Platform* platform) {
|
|
|
|
std::vector<std::unique_ptr<Task>> tasks;
|
2017-01-04 13:45:56 +00:00
|
|
|
{
|
2018-10-12 13:52:49 +00:00
|
|
|
base::MutexGuard lock(&mutex_);
|
2018-03-26 16:44:23 +00:00
|
|
|
tasks.swap(worker_tasks_);
|
2017-01-04 13:45:56 +00:00
|
|
|
}
|
2018-03-26 16:44:23 +00:00
|
|
|
platform->CallOnWorkerThread(
|
|
|
|
base::make_unique<TaskWrapper>(this, std::move(tasks), false));
|
2017-01-03 17:00:56 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 13:45:56 +00:00
|
|
|
void RunForegroundTasks() {
|
2018-03-26 16:44:23 +00:00
|
|
|
std::vector<std::unique_ptr<Task>> tasks;
|
2017-01-04 13:45:56 +00:00
|
|
|
{
|
2018-10-12 13:52:49 +00:00
|
|
|
base::MutexGuard lock(&mutex_);
|
2017-01-04 13:45:56 +00:00
|
|
|
tasks.swap(foreground_tasks_);
|
|
|
|
}
|
|
|
|
for (auto& task : tasks) {
|
|
|
|
task->Run();
|
2018-03-26 16:44:23 +00:00
|
|
|
// Reset |task| before running the next one.
|
|
|
|
task.reset();
|
2017-01-04 13:45:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:44:23 +00:00
|
|
|
void ClearWorkerTasks() {
|
|
|
|
std::vector<std::unique_ptr<Task>> tasks;
|
2017-01-04 13:45:56 +00:00
|
|
|
{
|
2018-10-12 13:52:49 +00:00
|
|
|
base::MutexGuard lock(&mutex_);
|
2018-03-26 16:44:23 +00:00
|
|
|
tasks.swap(worker_tasks_);
|
2017-01-04 13:45:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearForegroundTasks() {
|
2018-03-26 16:44:23 +00:00
|
|
|
std::vector<std::unique_ptr<Task>> tasks;
|
2017-01-04 13:45:56 +00:00
|
|
|
{
|
2018-10-12 13:52:49 +00:00
|
|
|
base::MutexGuard lock(&mutex_);
|
2017-01-04 13:45:56 +00:00
|
|
|
tasks.swap(foreground_tasks_);
|
|
|
|
}
|
2017-01-03 17:00:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClearIdleTask() {
|
2018-10-12 13:52:49 +00:00
|
|
|
base::MutexGuard lock(&mutex_);
|
2017-01-03 17:00:56 +00:00
|
|
|
ASSERT_TRUE(idle_task_ != nullptr);
|
|
|
|
delete idle_task_;
|
|
|
|
idle_task_ = nullptr;
|
|
|
|
}
|
2016-12-16 14:40:32 +00:00
|
|
|
|
|
|
|
private:
|
2017-01-03 17:00:56 +00:00
|
|
|
class TaskWrapper : public Task {
|
|
|
|
public:
|
2018-03-26 16:44:23 +00:00
|
|
|
TaskWrapper(MockPlatform* platform,
|
|
|
|
std::vector<std::unique_ptr<Task>> tasks, bool signal)
|
|
|
|
: platform_(platform), tasks_(std::move(tasks)), signal_(signal) {}
|
2018-09-14 15:41:32 +00:00
|
|
|
~TaskWrapper() override = default;
|
2017-01-03 17:00:56 +00:00
|
|
|
|
|
|
|
void Run() override {
|
|
|
|
for (auto& task : tasks_) {
|
|
|
|
task->Run();
|
2018-03-26 16:44:23 +00:00
|
|
|
// Reset |task| before running the next one.
|
|
|
|
task.reset();
|
2017-01-03 17:00:56 +00:00
|
|
|
}
|
|
|
|
if (signal_) platform_->sem_.Signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
MockPlatform* platform_;
|
2018-03-26 16:44:23 +00:00
|
|
|
std::vector<std::unique_ptr<Task>> tasks_;
|
2017-01-03 17:00:56 +00:00
|
|
|
bool signal_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(TaskWrapper);
|
|
|
|
};
|
|
|
|
|
2018-04-30 16:52:06 +00:00
|
|
|
class MockForegroundTaskRunner final : public TaskRunner {
|
2017-11-15 14:26:16 +00:00
|
|
|
public:
|
2018-04-30 16:52:06 +00:00
|
|
|
explicit MockForegroundTaskRunner(MockPlatform* platform)
|
|
|
|
: platform_(platform) {}
|
2017-11-15 14:26:16 +00:00
|
|
|
|
|
|
|
void PostTask(std::unique_ptr<v8::Task> task) override {
|
2018-10-12 13:52:49 +00:00
|
|
|
base::MutexGuard lock(&platform_->mutex_);
|
2018-04-30 16:52:06 +00:00
|
|
|
platform_->foreground_tasks_.push_back(std::move(task));
|
2017-11-15 14:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PostDelayedTask(std::unique_ptr<Task> task,
|
|
|
|
double delay_in_seconds) override {
|
|
|
|
UNREACHABLE();
|
2019-02-13 00:33:17 +00:00
|
|
|
}
|
2017-11-15 14:26:16 +00:00
|
|
|
|
|
|
|
void PostIdleTask(std::unique_ptr<IdleTask> task) override {
|
|
|
|
DCHECK(IdleTasksEnabled());
|
2018-10-12 13:52:49 +00:00
|
|
|
base::MutexGuard lock(&platform_->mutex_);
|
2017-11-15 14:26:16 +00:00
|
|
|
ASSERT_TRUE(platform_->idle_task_ == nullptr);
|
|
|
|
platform_->idle_task_ = task.release();
|
|
|
|
}
|
|
|
|
|
2019-02-13 00:33:17 +00:00
|
|
|
bool IdleTasksEnabled() override { return true; }
|
2017-11-15 14:26:16 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
MockPlatform* platform_;
|
|
|
|
};
|
|
|
|
|
2016-12-16 14:40:32 +00:00
|
|
|
double time_;
|
|
|
|
double time_step_;
|
|
|
|
|
2017-01-04 13:45:56 +00:00
|
|
|
// Protects all *_tasks_.
|
|
|
|
base::Mutex mutex_;
|
|
|
|
|
|
|
|
IdleTask* idle_task_;
|
2018-03-26 16:44:23 +00:00
|
|
|
std::vector<std::unique_ptr<Task>> worker_tasks_;
|
|
|
|
std::vector<std::unique_ptr<Task>> foreground_tasks_;
|
2017-01-04 13:45:56 +00:00
|
|
|
|
2017-01-03 17:00:56 +00:00
|
|
|
base::Semaphore sem_;
|
|
|
|
|
2017-07-03 08:23:26 +00:00
|
|
|
v8::TracingController* tracing_controller_;
|
|
|
|
|
2016-12-16 14:40:32 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(MockPlatform);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-12-12 15:35:41 +00:00
|
|
|
TEST_F(CompilerDispatcherTest, Construct) {
|
2017-08-01 14:16:30 +00:00
|
|
|
MockPlatform platform;
|
2016-12-16 14:40:32 +00:00
|
|
|
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
|
2018-10-12 12:12:52 +00:00
|
|
|
dispatcher.AbortAll();
|
2016-12-12 15:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompilerDispatcherTest, IsEnqueued) {
|
2017-08-01 14:16:30 +00:00
|
|
|
MockPlatform platform;
|
2016-12-16 14:40:32 +00:00
|
|
|
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
|
2016-12-12 15:35:41 +00:00
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
Handle<SharedFunctionInfo> shared =
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), nullptr);
|
|
|
|
ASSERT_FALSE(shared->is_compiled());
|
|
|
|
ASSERT_FALSE(dispatcher.IsEnqueued(shared));
|
2017-11-13 12:04:57 +00:00
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
base::Optional<CompilerDispatcher::JobId> job_id =
|
|
|
|
EnqueueUnoptimizedCompileJob(&dispatcher, i_isolate(), shared);
|
2016-12-12 15:35:41 +00:00
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
ASSERT_TRUE(job_id);
|
|
|
|
ASSERT_TRUE(dispatcher.IsEnqueued(*job_id));
|
|
|
|
ASSERT_FALSE(dispatcher.IsEnqueued(shared)); // SFI not yet registered.
|
|
|
|
|
|
|
|
dispatcher.RegisterSharedFunctionInfo(*job_id, *shared);
|
|
|
|
ASSERT_TRUE(dispatcher.IsEnqueued(*job_id));
|
2016-12-16 14:40:32 +00:00
|
|
|
ASSERT_TRUE(dispatcher.IsEnqueued(shared));
|
2018-09-20 13:18:16 +00:00
|
|
|
|
2018-10-12 12:12:52 +00:00
|
|
|
dispatcher.AbortAll();
|
2018-09-20 13:18:16 +00:00
|
|
|
ASSERT_FALSE(dispatcher.IsEnqueued(*job_id));
|
2016-12-16 14:40:32 +00:00
|
|
|
ASSERT_FALSE(dispatcher.IsEnqueued(shared));
|
2018-10-09 09:55:47 +00:00
|
|
|
|
|
|
|
ASSERT_FALSE(platform.IdleTaskPending());
|
|
|
|
ASSERT_TRUE(platform.WorkerTasksPending());
|
2018-09-20 13:18:16 +00:00
|
|
|
platform.ClearWorkerTasks();
|
2016-12-12 15:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompilerDispatcherTest, FinishNow) {
|
2017-08-01 14:16:30 +00:00
|
|
|
MockPlatform platform;
|
2016-12-16 14:40:32 +00:00
|
|
|
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
|
2016-12-12 15:35:41 +00:00
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
Handle<SharedFunctionInfo> shared =
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), nullptr);
|
2016-12-19 15:41:04 +00:00
|
|
|
ASSERT_FALSE(shared->is_compiled());
|
2018-09-20 13:18:16 +00:00
|
|
|
|
|
|
|
base::Optional<CompilerDispatcher::JobId> job_id =
|
|
|
|
EnqueueUnoptimizedCompileJob(&dispatcher, i_isolate(), shared);
|
|
|
|
dispatcher.RegisterSharedFunctionInfo(*job_id, *shared);
|
|
|
|
|
2016-12-16 14:40:32 +00:00
|
|
|
ASSERT_TRUE(dispatcher.FinishNow(shared));
|
2016-12-12 15:35:41 +00:00
|
|
|
// Finishing removes the SFI from the queue.
|
2018-09-20 13:18:16 +00:00
|
|
|
ASSERT_FALSE(dispatcher.IsEnqueued(*job_id));
|
2016-12-16 14:40:32 +00:00
|
|
|
ASSERT_FALSE(dispatcher.IsEnqueued(shared));
|
2016-12-19 15:41:04 +00:00
|
|
|
ASSERT_TRUE(shared->is_compiled());
|
2018-10-09 09:55:47 +00:00
|
|
|
|
2018-03-26 16:44:23 +00:00
|
|
|
platform.ClearWorkerTasks();
|
2018-10-09 09:55:47 +00:00
|
|
|
ASSERT_FALSE(platform.IdleTaskPending());
|
2018-10-12 12:12:52 +00:00
|
|
|
dispatcher.AbortAll();
|
2017-04-04 15:44:44 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 09:55:47 +00:00
|
|
|
TEST_F(CompilerDispatcherTest, CompileAndFinalize) {
|
2017-08-01 14:16:30 +00:00
|
|
|
MockPlatform platform;
|
2016-12-16 14:40:32 +00:00
|
|
|
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
|
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
Handle<SharedFunctionInfo> shared =
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), nullptr);
|
|
|
|
ASSERT_FALSE(shared->is_compiled());
|
2016-12-16 14:40:32 +00:00
|
|
|
ASSERT_FALSE(platform.IdleTaskPending());
|
2018-09-20 13:18:16 +00:00
|
|
|
|
|
|
|
base::Optional<CompilerDispatcher::JobId> job_id =
|
|
|
|
EnqueueUnoptimizedCompileJob(&dispatcher, i_isolate(), shared);
|
2018-10-09 09:55:47 +00:00
|
|
|
ASSERT_TRUE(platform.WorkerTasksPending());
|
2016-12-16 14:40:32 +00:00
|
|
|
|
2018-10-09 09:55:47 +00:00
|
|
|
// Run compile steps.
|
|
|
|
platform.RunWorkerTasksAndBlock(V8::GetCurrentPlatform());
|
2016-12-16 14:40:32 +00:00
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
// Since we haven't yet registered the SFI for the job, it should still be
|
|
|
|
// enqueued and waiting.
|
|
|
|
ASSERT_TRUE(dispatcher.IsEnqueued(*job_id));
|
|
|
|
ASSERT_FALSE(shared->is_compiled());
|
|
|
|
ASSERT_FALSE(platform.IdleTaskPending());
|
|
|
|
|
2018-10-09 09:55:47 +00:00
|
|
|
// Register SFI, which should schedule another idle task to finalize the
|
2018-09-20 13:18:16 +00:00
|
|
|
// compilation.
|
|
|
|
dispatcher.RegisterSharedFunctionInfo(*job_id, *shared);
|
|
|
|
ASSERT_TRUE(platform.IdleTaskPending());
|
|
|
|
platform.RunIdleTask(1000.0, 0.0);
|
|
|
|
|
2016-12-16 14:40:32 +00:00
|
|
|
ASSERT_FALSE(dispatcher.IsEnqueued(shared));
|
2016-12-19 15:41:04 +00:00
|
|
|
ASSERT_TRUE(shared->is_compiled());
|
2018-10-09 09:55:47 +00:00
|
|
|
ASSERT_FALSE(platform.WorkerTasksPending());
|
|
|
|
ASSERT_FALSE(platform.IdleTaskPending());
|
2018-10-12 12:12:52 +00:00
|
|
|
dispatcher.AbortAll();
|
2016-12-12 15:35:41 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 09:55:47 +00:00
|
|
|
TEST_F(CompilerDispatcherTest, IdleTaskNoIdleTime) {
|
2017-08-01 14:16:30 +00:00
|
|
|
MockPlatform platform;
|
2016-12-16 14:40:32 +00:00
|
|
|
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
|
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
Handle<SharedFunctionInfo> shared =
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), nullptr);
|
|
|
|
ASSERT_FALSE(shared->is_compiled());
|
2016-12-16 14:40:32 +00:00
|
|
|
ASSERT_FALSE(platform.IdleTaskPending());
|
2018-09-20 13:18:16 +00:00
|
|
|
|
|
|
|
base::Optional<CompilerDispatcher::JobId> job_id =
|
|
|
|
EnqueueUnoptimizedCompileJob(&dispatcher, i_isolate(), shared);
|
|
|
|
dispatcher.RegisterSharedFunctionInfo(*job_id, *shared);
|
|
|
|
|
2018-10-09 09:55:47 +00:00
|
|
|
// Run compile steps.
|
|
|
|
platform.RunWorkerTasksAndBlock(V8::GetCurrentPlatform());
|
2016-12-16 14:40:32 +00:00
|
|
|
|
2018-10-09 09:55:47 +00:00
|
|
|
// Job should be ready to finalize.
|
2017-02-22 13:46:14 +00:00
|
|
|
ASSERT_EQ(dispatcher.jobs_.size(), 1u);
|
2018-10-09 09:55:47 +00:00
|
|
|
ASSERT_TRUE(dispatcher.jobs_.begin()->second->has_run);
|
|
|
|
ASSERT_TRUE(platform.IdleTaskPending());
|
2016-12-16 14:40:32 +00:00
|
|
|
|
2018-10-09 09:55:47 +00:00
|
|
|
// Grant no idle time and have time advance beyond it in one step.
|
|
|
|
platform.RunIdleTask(0.0, 1.0);
|
2016-12-16 14:40:32 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(dispatcher.IsEnqueued(shared));
|
2016-12-19 15:41:04 +00:00
|
|
|
ASSERT_FALSE(shared->is_compiled());
|
2016-12-16 14:40:32 +00:00
|
|
|
ASSERT_TRUE(platform.IdleTaskPending());
|
|
|
|
|
2018-10-09 09:55:47 +00:00
|
|
|
// Job should be ready to finalize.
|
2017-02-22 13:46:14 +00:00
|
|
|
ASSERT_EQ(dispatcher.jobs_.size(), 1u);
|
2018-10-09 09:55:47 +00:00
|
|
|
ASSERT_TRUE(dispatcher.jobs_.begin()->second->has_run);
|
2016-12-16 14:40:32 +00:00
|
|
|
|
2017-01-03 17:00:56 +00:00
|
|
|
// Now grant a lot of idle time and freeze time.
|
2016-12-16 14:40:32 +00:00
|
|
|
platform.RunIdleTask(1000.0, 0.0);
|
|
|
|
|
|
|
|
ASSERT_FALSE(dispatcher.IsEnqueued(shared));
|
2016-12-19 15:41:04 +00:00
|
|
|
ASSERT_TRUE(shared->is_compiled());
|
2016-12-16 14:40:32 +00:00
|
|
|
ASSERT_FALSE(platform.IdleTaskPending());
|
2018-10-09 09:55:47 +00:00
|
|
|
ASSERT_FALSE(platform.WorkerTasksPending());
|
2018-10-12 12:12:52 +00:00
|
|
|
dispatcher.AbortAll();
|
2018-10-09 09:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompilerDispatcherTest, IdleTaskSmallIdleTime) {
|
|
|
|
MockPlatform platform;
|
|
|
|
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
|
|
|
|
|
|
|
|
Handle<SharedFunctionInfo> shared_1 =
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), nullptr);
|
|
|
|
ASSERT_FALSE(shared_1->is_compiled());
|
|
|
|
Handle<SharedFunctionInfo> shared_2 =
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), nullptr);
|
|
|
|
ASSERT_FALSE(shared_2->is_compiled());
|
|
|
|
|
|
|
|
base::Optional<CompilerDispatcher::JobId> job_id_1 =
|
|
|
|
EnqueueUnoptimizedCompileJob(&dispatcher, i_isolate(), shared_1);
|
|
|
|
base::Optional<CompilerDispatcher::JobId> job_id_2 =
|
|
|
|
EnqueueUnoptimizedCompileJob(&dispatcher, i_isolate(), shared_2);
|
|
|
|
|
|
|
|
dispatcher.RegisterSharedFunctionInfo(*job_id_1, *shared_1);
|
|
|
|
dispatcher.RegisterSharedFunctionInfo(*job_id_2, *shared_2);
|
|
|
|
|
|
|
|
// Run compile steps.
|
|
|
|
platform.RunWorkerTasksAndBlock(V8::GetCurrentPlatform());
|
|
|
|
|
|
|
|
// Both jobs should be ready to finalize.
|
|
|
|
ASSERT_EQ(dispatcher.jobs_.size(), 2u);
|
|
|
|
ASSERT_TRUE(dispatcher.jobs_.begin()->second->has_run);
|
|
|
|
ASSERT_TRUE((++dispatcher.jobs_.begin())->second->has_run);
|
|
|
|
ASSERT_TRUE(platform.IdleTaskPending());
|
|
|
|
|
|
|
|
// Grant a small anount of idle time and have time advance beyond it in one
|
|
|
|
// step.
|
|
|
|
platform.RunIdleTask(2.0, 1.0);
|
|
|
|
|
|
|
|
// Only one of the jobs should be finalized.
|
|
|
|
ASSERT_EQ(dispatcher.jobs_.size(), 1u);
|
|
|
|
ASSERT_TRUE(dispatcher.jobs_.begin()->second->has_run);
|
|
|
|
ASSERT_NE(dispatcher.IsEnqueued(shared_1), dispatcher.IsEnqueued(shared_2));
|
|
|
|
ASSERT_NE(shared_1->is_compiled(), shared_2->is_compiled());
|
|
|
|
ASSERT_TRUE(platform.IdleTaskPending());
|
|
|
|
|
|
|
|
// Now grant a lot of idle time and freeze time.
|
|
|
|
platform.RunIdleTask(1000.0, 0.0);
|
|
|
|
|
|
|
|
ASSERT_FALSE(dispatcher.IsEnqueued(shared_1) ||
|
|
|
|
dispatcher.IsEnqueued(shared_2));
|
|
|
|
ASSERT_TRUE(shared_1->is_compiled() && shared_2->is_compiled());
|
|
|
|
ASSERT_FALSE(platform.IdleTaskPending());
|
|
|
|
ASSERT_FALSE(platform.WorkerTasksPending());
|
2018-10-12 12:12:52 +00:00
|
|
|
dispatcher.AbortAll();
|
2016-12-16 14:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompilerDispatcherTest, IdleTaskException) {
|
2017-08-01 14:16:30 +00:00
|
|
|
MockPlatform platform;
|
2016-12-16 14:40:32 +00:00
|
|
|
CompilerDispatcher dispatcher(i_isolate(), &platform, 50);
|
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
std::string raw_script("(x) { var a = ");
|
|
|
|
for (int i = 0; i < 1000; i++) {
|
2017-10-25 10:47:24 +00:00
|
|
|
// Alternate + and - to avoid n-ary operation nodes.
|
2018-09-20 13:18:16 +00:00
|
|
|
raw_script += "'x' + 'x' - ";
|
2016-12-16 14:40:32 +00:00
|
|
|
}
|
2018-09-20 13:18:16 +00:00
|
|
|
raw_script += " 'x'; };";
|
|
|
|
test::ScriptResource* script =
|
|
|
|
new test::ScriptResource(raw_script.c_str(), strlen(raw_script.c_str()));
|
|
|
|
Handle<SharedFunctionInfo> shared =
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), script);
|
|
|
|
ASSERT_FALSE(shared->is_compiled());
|
2016-12-16 14:40:32 +00:00
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
base::Optional<CompilerDispatcher::JobId> job_id =
|
|
|
|
EnqueueUnoptimizedCompileJob(&dispatcher, i_isolate(), shared);
|
|
|
|
dispatcher.RegisterSharedFunctionInfo(*job_id, *shared);
|
2016-12-16 14:40:32 +00:00
|
|
|
|
2018-10-09 09:55:47 +00:00
|
|
|
// Run compile steps and finalize.
|
|
|
|
platform.RunWorkerTasksAndBlock(V8::GetCurrentPlatform());
|
2016-12-16 14:40:32 +00:00
|
|
|
platform.RunIdleTask(1000.0, 0.0);
|
|
|
|
|
|
|
|
ASSERT_FALSE(dispatcher.IsEnqueued(shared));
|
2016-12-19 15:41:04 +00:00
|
|
|
ASSERT_FALSE(shared->is_compiled());
|
2017-01-04 10:07:59 +00:00
|
|
|
ASSERT_FALSE(i_isolate()->has_pending_exception());
|
2018-09-20 13:18:16 +00:00
|
|
|
platform.ClearWorkerTasks();
|
2018-10-12 12:12:52 +00:00
|
|
|
dispatcher.AbortAll();
|
2016-12-16 14:40:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-26 16:44:23 +00:00
|
|
|
TEST_F(CompilerDispatcherTest, FinishNowWithWorkerTask) {
|
2017-08-01 14:16:30 +00:00
|
|
|
MockPlatform platform;
|
2017-01-03 17:00:56 +00:00
|
|
|
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
|
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
Handle<SharedFunctionInfo> shared =
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), nullptr);
|
|
|
|
ASSERT_FALSE(shared->is_compiled());
|
2017-01-03 17:00:56 +00:00
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
base::Optional<CompilerDispatcher::JobId> job_id =
|
|
|
|
EnqueueUnoptimizedCompileJob(&dispatcher, i_isolate(), shared);
|
|
|
|
dispatcher.RegisterSharedFunctionInfo(*job_id, *shared);
|
2017-01-03 17:00:56 +00:00
|
|
|
|
2017-02-22 13:46:14 +00:00
|
|
|
ASSERT_EQ(dispatcher.jobs_.size(), 1u);
|
2018-10-09 09:55:47 +00:00
|
|
|
ASSERT_FALSE(dispatcher.jobs_.begin()->second->has_run);
|
2017-01-03 17:00:56 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(dispatcher.IsEnqueued(shared));
|
|
|
|
ASSERT_FALSE(shared->is_compiled());
|
2018-09-20 13:18:16 +00:00
|
|
|
ASSERT_EQ(dispatcher.jobs_.size(), 1u);
|
2018-10-09 09:55:47 +00:00
|
|
|
ASSERT_FALSE(dispatcher.jobs_.begin()->second->has_run);
|
2018-03-26 16:44:23 +00:00
|
|
|
ASSERT_TRUE(platform.WorkerTasksPending());
|
2017-01-03 17:00:56 +00:00
|
|
|
|
|
|
|
// This does not block, but races with the FinishNow() call below.
|
2018-03-26 16:44:23 +00:00
|
|
|
platform.RunWorkerTasks(V8::GetCurrentPlatform());
|
2017-01-03 17:00:56 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(dispatcher.FinishNow(shared));
|
|
|
|
// Finishing removes the SFI from the queue.
|
|
|
|
ASSERT_FALSE(dispatcher.IsEnqueued(shared));
|
|
|
|
ASSERT_TRUE(shared->is_compiled());
|
|
|
|
if (platform.IdleTaskPending()) platform.ClearIdleTask();
|
2018-03-26 16:44:23 +00:00
|
|
|
ASSERT_FALSE(platform.WorkerTasksPending());
|
2018-10-12 12:12:52 +00:00
|
|
|
dispatcher.AbortAll();
|
2017-01-03 17:00:56 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 10:07:59 +00:00
|
|
|
TEST_F(CompilerDispatcherTest, IdleTaskMultipleJobs) {
|
2017-08-01 14:16:30 +00:00
|
|
|
MockPlatform platform;
|
2017-01-04 10:07:59 +00:00
|
|
|
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
|
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
Handle<SharedFunctionInfo> shared_1 =
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), nullptr);
|
|
|
|
ASSERT_FALSE(shared_1->is_compiled());
|
|
|
|
Handle<SharedFunctionInfo> shared_2 =
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), nullptr);
|
|
|
|
ASSERT_FALSE(shared_2->is_compiled());
|
2017-01-04 10:07:59 +00:00
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
base::Optional<CompilerDispatcher::JobId> job_id_1 =
|
|
|
|
EnqueueUnoptimizedCompileJob(&dispatcher, i_isolate(), shared_1);
|
|
|
|
base::Optional<CompilerDispatcher::JobId> job_id_2 =
|
|
|
|
EnqueueUnoptimizedCompileJob(&dispatcher, i_isolate(), shared_2);
|
2017-01-04 10:07:59 +00:00
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
dispatcher.RegisterSharedFunctionInfo(*job_id_1, *shared_1);
|
|
|
|
dispatcher.RegisterSharedFunctionInfo(*job_id_2, *shared_2);
|
|
|
|
|
|
|
|
ASSERT_TRUE(dispatcher.IsEnqueued(shared_1));
|
|
|
|
ASSERT_TRUE(dispatcher.IsEnqueued(shared_2));
|
2017-01-04 10:07:59 +00:00
|
|
|
|
2018-10-09 09:55:47 +00:00
|
|
|
// Run compile steps and finalize.
|
|
|
|
platform.RunWorkerTasksAndBlock(V8::GetCurrentPlatform());
|
2017-01-04 10:07:59 +00:00
|
|
|
platform.RunIdleTask(1000.0, 0.0);
|
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
ASSERT_FALSE(dispatcher.IsEnqueued(shared_1));
|
|
|
|
ASSERT_FALSE(dispatcher.IsEnqueued(shared_2));
|
|
|
|
ASSERT_TRUE(shared_1->is_compiled());
|
|
|
|
ASSERT_TRUE(shared_2->is_compiled());
|
2018-10-09 09:55:47 +00:00
|
|
|
ASSERT_FALSE(platform.IdleTaskPending());
|
|
|
|
ASSERT_FALSE(platform.WorkerTasksPending());
|
2018-10-12 12:12:52 +00:00
|
|
|
dispatcher.AbortAll();
|
2017-01-04 10:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompilerDispatcherTest, FinishNowException) {
|
2017-08-01 14:16:30 +00:00
|
|
|
MockPlatform platform;
|
2017-01-04 10:07:59 +00:00
|
|
|
CompilerDispatcher dispatcher(i_isolate(), &platform, 50);
|
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
std::string raw_script("(x) { var a = ");
|
|
|
|
for (int i = 0; i < 1000; i++) {
|
2017-10-25 10:47:24 +00:00
|
|
|
// Alternate + and - to avoid n-ary operation nodes.
|
2018-09-20 13:18:16 +00:00
|
|
|
raw_script += "'x' + 'x' - ";
|
2017-01-04 10:07:59 +00:00
|
|
|
}
|
2018-09-20 13:18:16 +00:00
|
|
|
raw_script += " 'x'; };";
|
|
|
|
test::ScriptResource* script =
|
|
|
|
new test::ScriptResource(raw_script.c_str(), strlen(raw_script.c_str()));
|
|
|
|
Handle<SharedFunctionInfo> shared =
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), script);
|
|
|
|
ASSERT_FALSE(shared->is_compiled());
|
2017-01-04 10:07:59 +00:00
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
base::Optional<CompilerDispatcher::JobId> job_id =
|
|
|
|
EnqueueUnoptimizedCompileJob(&dispatcher, i_isolate(), shared);
|
|
|
|
dispatcher.RegisterSharedFunctionInfo(*job_id, *shared);
|
2017-01-04 10:07:59 +00:00
|
|
|
|
|
|
|
ASSERT_FALSE(dispatcher.FinishNow(shared));
|
|
|
|
|
|
|
|
ASSERT_FALSE(dispatcher.IsEnqueued(shared));
|
|
|
|
ASSERT_FALSE(shared->is_compiled());
|
|
|
|
ASSERT_TRUE(i_isolate()->has_pending_exception());
|
|
|
|
|
|
|
|
i_isolate()->clear_pending_exception();
|
2018-10-09 09:55:47 +00:00
|
|
|
ASSERT_FALSE(platform.IdleTaskPending());
|
2018-09-20 13:18:16 +00:00
|
|
|
platform.ClearWorkerTasks();
|
2018-10-12 12:12:52 +00:00
|
|
|
dispatcher.AbortAll();
|
2017-01-09 08:52:04 +00:00
|
|
|
}
|
|
|
|
|
2018-10-15 12:45:28 +00:00
|
|
|
TEST_F(CompilerDispatcherTest, AbortJobNotStarted) {
|
|
|
|
MockPlatform platform;
|
|
|
|
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
|
|
|
|
|
|
|
|
Handle<SharedFunctionInfo> shared =
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), nullptr);
|
|
|
|
ASSERT_FALSE(shared->is_compiled());
|
|
|
|
|
|
|
|
base::Optional<CompilerDispatcher::JobId> job_id =
|
|
|
|
EnqueueUnoptimizedCompileJob(&dispatcher, i_isolate(), shared);
|
|
|
|
|
|
|
|
ASSERT_EQ(dispatcher.jobs_.size(), 1u);
|
|
|
|
ASSERT_FALSE(dispatcher.jobs_.begin()->second->has_run);
|
|
|
|
|
|
|
|
ASSERT_TRUE(dispatcher.IsEnqueued(*job_id));
|
|
|
|
ASSERT_FALSE(shared->is_compiled());
|
|
|
|
ASSERT_EQ(dispatcher.jobs_.size(), 1u);
|
|
|
|
ASSERT_FALSE(dispatcher.jobs_.begin()->second->has_run);
|
|
|
|
ASSERT_TRUE(platform.WorkerTasksPending());
|
|
|
|
|
|
|
|
dispatcher.AbortJob(*job_id);
|
|
|
|
|
|
|
|
// Aborting removes the job from the queue.
|
|
|
|
ASSERT_FALSE(dispatcher.IsEnqueued(*job_id));
|
|
|
|
ASSERT_FALSE(shared->is_compiled());
|
|
|
|
ASSERT_FALSE(platform.IdleTaskPending());
|
|
|
|
platform.ClearWorkerTasks();
|
|
|
|
dispatcher.AbortAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompilerDispatcherTest, AbortJobAlreadyStarted) {
|
|
|
|
MockPlatform platform;
|
|
|
|
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
|
|
|
|
|
|
|
|
Handle<SharedFunctionInfo> shared =
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), nullptr);
|
|
|
|
ASSERT_FALSE(shared->is_compiled());
|
|
|
|
|
|
|
|
base::Optional<CompilerDispatcher::JobId> job_id =
|
|
|
|
EnqueueUnoptimizedCompileJob(&dispatcher, i_isolate(), shared);
|
|
|
|
|
|
|
|
ASSERT_EQ(dispatcher.jobs_.size(), 1u);
|
|
|
|
ASSERT_FALSE(dispatcher.jobs_.begin()->second->has_run);
|
|
|
|
|
|
|
|
ASSERT_TRUE(dispatcher.IsEnqueued(*job_id));
|
|
|
|
ASSERT_FALSE(shared->is_compiled());
|
|
|
|
ASSERT_EQ(dispatcher.jobs_.size(), 1u);
|
|
|
|
ASSERT_FALSE(dispatcher.jobs_.begin()->second->has_run);
|
|
|
|
ASSERT_TRUE(platform.WorkerTasksPending());
|
|
|
|
|
|
|
|
// Have dispatcher block on the background thread when running the job.
|
|
|
|
{
|
|
|
|
base::LockGuard<base::Mutex> lock(&dispatcher.mutex_);
|
|
|
|
dispatcher.block_for_testing_.SetValue(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start background thread and wait until it is about to run the job.
|
|
|
|
platform.RunWorkerTasks(V8::GetCurrentPlatform());
|
|
|
|
while (dispatcher.block_for_testing_.Value()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now abort while dispatcher is in the middle of running the job.
|
|
|
|
dispatcher.AbortJob(*job_id);
|
|
|
|
|
|
|
|
// Unblock background thread, and wait for job to complete.
|
|
|
|
{
|
|
|
|
base::LockGuard<base::Mutex> lock(&dispatcher.mutex_);
|
|
|
|
dispatcher.main_thread_blocking_on_job_ =
|
|
|
|
dispatcher.jobs_.begin()->second.get();
|
|
|
|
dispatcher.semaphore_for_testing_.Signal();
|
|
|
|
while (dispatcher.main_thread_blocking_on_job_ != nullptr) {
|
|
|
|
dispatcher.main_thread_blocking_signal_.Wait(&dispatcher.mutex_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Job should have finished running and then been aborted.
|
|
|
|
ASSERT_TRUE(dispatcher.IsEnqueued(*job_id));
|
|
|
|
ASSERT_FALSE(shared->is_compiled());
|
|
|
|
ASSERT_EQ(dispatcher.jobs_.size(), 1u);
|
|
|
|
ASSERT_TRUE(dispatcher.jobs_.begin()->second->has_run);
|
|
|
|
ASSERT_TRUE(dispatcher.jobs_.begin()->second->aborted);
|
|
|
|
ASSERT_FALSE(platform.WorkerTasksPending());
|
|
|
|
ASSERT_TRUE(platform.IdleTaskPending());
|
|
|
|
|
|
|
|
// Runt the pending idle task
|
|
|
|
platform.RunIdleTask(1000.0, 0.0);
|
|
|
|
|
|
|
|
// Aborting removes the SFI from the queue.
|
|
|
|
ASSERT_FALSE(dispatcher.IsEnqueued(*job_id));
|
|
|
|
ASSERT_FALSE(shared->is_compiled());
|
|
|
|
ASSERT_FALSE(platform.IdleTaskPending());
|
|
|
|
ASSERT_FALSE(platform.WorkerTasksPending());
|
|
|
|
dispatcher.AbortAll();
|
|
|
|
}
|
|
|
|
|
2017-02-10 18:46:31 +00:00
|
|
|
TEST_F(CompilerDispatcherTest, CompileLazyFinishesDispatcherJob) {
|
|
|
|
// Use the real dispatcher so that CompileLazy checks the same one for
|
|
|
|
// enqueued functions.
|
|
|
|
CompilerDispatcher* dispatcher = i_isolate()->compiler_dispatcher();
|
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
const char raw_script[] = "function lazy() { return 42; }; lazy;";
|
|
|
|
test::ScriptResource* script =
|
|
|
|
new test::ScriptResource(raw_script, strlen(raw_script));
|
2017-11-13 12:04:57 +00:00
|
|
|
Handle<JSFunction> f = RunJS<JSFunction>(script);
|
2017-02-10 18:46:31 +00:00
|
|
|
Handle<SharedFunctionInfo> shared(f->shared(), i_isolate());
|
|
|
|
ASSERT_FALSE(shared->is_compiled());
|
2018-09-20 13:18:16 +00:00
|
|
|
|
|
|
|
base::Optional<CompilerDispatcher::JobId> job_id =
|
|
|
|
EnqueueUnoptimizedCompileJob(dispatcher, i_isolate(), shared);
|
|
|
|
dispatcher->RegisterSharedFunctionInfo(*job_id, *shared);
|
2017-02-10 18:46:31 +00:00
|
|
|
|
|
|
|
// Now force the function to run and ensure CompileLazy finished and dequeues
|
|
|
|
// it from the dispatcher.
|
2017-11-16 12:58:16 +00:00
|
|
|
RunJS("lazy();");
|
2017-02-10 18:46:31 +00:00
|
|
|
ASSERT_TRUE(shared->is_compiled());
|
|
|
|
ASSERT_FALSE(dispatcher->IsEnqueued(shared));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompilerDispatcherTest, CompileLazy2FinishesDispatcherJob) {
|
|
|
|
// Use the real dispatcher so that CompileLazy checks the same one for
|
|
|
|
// enqueued functions.
|
|
|
|
CompilerDispatcher* dispatcher = i_isolate()->compiler_dispatcher();
|
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
const char raw_source_2[] = "function lazy2() { return 42; }; lazy2;";
|
|
|
|
test::ScriptResource* source_2 =
|
|
|
|
new test::ScriptResource(raw_source_2, strlen(raw_source_2));
|
|
|
|
Handle<JSFunction> lazy2 = RunJS<JSFunction>(source_2);
|
|
|
|
Handle<SharedFunctionInfo> shared_2(lazy2->shared(), i_isolate());
|
|
|
|
ASSERT_FALSE(shared_2->is_compiled());
|
2017-02-10 18:46:31 +00:00
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
const char raw_source_1[] = "function lazy1() { return lazy2(); }; lazy1;";
|
|
|
|
test::ScriptResource* source_1 =
|
|
|
|
new test::ScriptResource(raw_source_1, strlen(raw_source_1));
|
|
|
|
Handle<JSFunction> lazy1 = RunJS<JSFunction>(source_1);
|
|
|
|
Handle<SharedFunctionInfo> shared_1(lazy1->shared(), i_isolate());
|
|
|
|
ASSERT_FALSE(shared_1->is_compiled());
|
2017-02-10 18:46:31 +00:00
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
base::Optional<CompilerDispatcher::JobId> job_id_1 =
|
|
|
|
EnqueueUnoptimizedCompileJob(dispatcher, i_isolate(), shared_1);
|
|
|
|
dispatcher->RegisterSharedFunctionInfo(*job_id_1, *shared_1);
|
2017-02-10 18:46:31 +00:00
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
base::Optional<CompilerDispatcher::JobId> job_id_2 =
|
|
|
|
EnqueueUnoptimizedCompileJob(dispatcher, i_isolate(), shared_2);
|
|
|
|
dispatcher->RegisterSharedFunctionInfo(*job_id_2, *shared_2);
|
2017-02-10 18:46:31 +00:00
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
ASSERT_TRUE(dispatcher->IsEnqueued(shared_1));
|
|
|
|
ASSERT_TRUE(dispatcher->IsEnqueued(shared_2));
|
2017-02-10 22:12:39 +00:00
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
RunJS("lazy1();");
|
|
|
|
ASSERT_TRUE(shared_1->is_compiled());
|
|
|
|
ASSERT_TRUE(shared_2->is_compiled());
|
|
|
|
ASSERT_FALSE(dispatcher->IsEnqueued(shared_1));
|
|
|
|
ASSERT_FALSE(dispatcher->IsEnqueued(shared_2));
|
2017-02-10 22:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 17:00:22 +00:00
|
|
|
TEST_F(CompilerDispatcherTest, CompileMultipleOnBackgroundThread) {
|
2017-08-01 14:16:30 +00:00
|
|
|
MockPlatform platform;
|
2017-03-03 17:00:22 +00:00
|
|
|
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
|
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
Handle<SharedFunctionInfo> shared_1 =
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), nullptr);
|
|
|
|
ASSERT_FALSE(shared_1->is_compiled());
|
2017-03-03 17:00:22 +00:00
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
Handle<SharedFunctionInfo> shared_2 =
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), nullptr);
|
|
|
|
ASSERT_FALSE(shared_2->is_compiled());
|
|
|
|
|
|
|
|
base::Optional<CompilerDispatcher::JobId> job_id_1 =
|
|
|
|
EnqueueUnoptimizedCompileJob(&dispatcher, i_isolate(), shared_1);
|
|
|
|
dispatcher.RegisterSharedFunctionInfo(*job_id_1, *shared_1);
|
|
|
|
|
|
|
|
base::Optional<CompilerDispatcher::JobId> job_id_2 =
|
|
|
|
EnqueueUnoptimizedCompileJob(&dispatcher, i_isolate(), shared_2);
|
|
|
|
dispatcher.RegisterSharedFunctionInfo(*job_id_2, *shared_2);
|
2017-03-03 17:00:22 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(dispatcher.jobs_.size(), 2u);
|
2018-10-09 09:55:47 +00:00
|
|
|
ASSERT_FALSE(dispatcher.jobs_.begin()->second->has_run);
|
|
|
|
ASSERT_FALSE((++dispatcher.jobs_.begin())->second->has_run);
|
2017-03-03 17:00:22 +00:00
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
ASSERT_TRUE(dispatcher.IsEnqueued(shared_1));
|
|
|
|
ASSERT_TRUE(dispatcher.IsEnqueued(shared_2));
|
|
|
|
ASSERT_FALSE(shared_1->is_compiled());
|
|
|
|
ASSERT_FALSE(shared_2->is_compiled());
|
2018-10-09 09:55:47 +00:00
|
|
|
ASSERT_FALSE(platform.IdleTaskPending());
|
2018-03-26 16:44:23 +00:00
|
|
|
ASSERT_TRUE(platform.WorkerTasksPending());
|
2017-03-03 17:00:22 +00:00
|
|
|
|
2018-03-26 16:44:23 +00:00
|
|
|
platform.RunWorkerTasksAndBlock(V8::GetCurrentPlatform());
|
2017-03-03 17:00:22 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(platform.IdleTaskPending());
|
2018-03-26 16:44:23 +00:00
|
|
|
ASSERT_FALSE(platform.WorkerTasksPending());
|
2017-03-03 17:00:22 +00:00
|
|
|
ASSERT_EQ(dispatcher.jobs_.size(), 2u);
|
2018-10-09 09:55:47 +00:00
|
|
|
ASSERT_TRUE(dispatcher.jobs_.begin()->second->has_run);
|
|
|
|
ASSERT_TRUE((++dispatcher.jobs_.begin())->second->has_run);
|
2017-03-03 17:00:22 +00:00
|
|
|
|
|
|
|
// Now grant a lot of idle time and freeze time.
|
|
|
|
platform.RunIdleTask(1000.0, 0.0);
|
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
ASSERT_FALSE(dispatcher.IsEnqueued(shared_1));
|
|
|
|
ASSERT_FALSE(dispatcher.IsEnqueued(shared_2));
|
|
|
|
ASSERT_TRUE(shared_1->is_compiled());
|
|
|
|
ASSERT_TRUE(shared_2->is_compiled());
|
2017-03-03 17:00:22 +00:00
|
|
|
ASSERT_FALSE(platform.IdleTaskPending());
|
2018-10-12 12:12:52 +00:00
|
|
|
dispatcher.AbortAll();
|
2017-03-03 17:00:22 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 15:35:41 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|