Skip to content

Commit

Permalink
gin: Make v8_context_snapshot.bin as a default snapshot blob
Browse files Browse the repository at this point in the history
To make Chrome independent from snapshot_blob.bin, this CL
makes v8_context_snapshot.bin as the default snapshot.

As a background issue, if we want to use JS (=V8), we have to
load a snapshot file on most platforms. It means we have to
load either snapshot_blob.bin or v8_context_snapshot.bin.

And some unit tests, e.g. net_unittests, do not need to use
v8_context_snapshot.bin, and they don't want to depend on
blink component. (It takes very long time just to create the
snapshot.)

This CL makes it possible to load either snapshot file
depending on the order of function calls, and make dependencies
clear.

TBR=liberato

Bug: 789964
Change-Id: I4df90ed5fe7be37ab969a7f7d5db79bf572ed02a
Reviewed-on: https://chromium-review.googlesource.com/859577
Reviewed-by: Hitoshi Yoshida <peria@chromium.org>
Reviewed-by: Ross McIlroy <rmcilroy@chromium.org>
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Reviewed-by: Ken Rockot <rockot@chromium.org>
Reviewed-by: Kinuko Yasuda <kinuko@chromium.org>
Commit-Queue: Hitoshi Yoshida <peria@chromium.org>
Cr-Commit-Position: refs/heads/master@{#530539}
  • Loading branch information
peria authored and Commit Bot committed Jan 19, 2018
1 parent 40534c4 commit 9aff02e
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 196 deletions.
1 change: 1 addition & 0 deletions content/app/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ template("implement_content_app") {
extra_configs = [
"//build/config/compiler:wexit_time_destructors",
"//content:content_implementation",
"//tools/v8_context_snapshot:use_v8_context_snapshot",
"//v8:external_startup_data",
]

Expand Down
37 changes: 16 additions & 21 deletions content/app/content_main_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,39 +169,35 @@ void InitializeFieldTrialAndFeatureList(
}

#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
void LoadV8ContextSnapshotFile() {
#if defined(OS_POSIX) && !defined(OS_MACOSX)
base::FileDescriptorStore& file_descriptor_store =
base::FileDescriptorStore::GetInstance();
base::MemoryMappedFile::Region region;
base::ScopedFD fd = file_descriptor_store.MaybeTakeFD(
kV8ContextSnapshotDataDescriptor, &region);
if (fd.is_valid()) {
gin::V8Initializer::LoadV8ContextSnapshotFromFD(fd.get(), region.offset,
region.size);
return;
}
#endif // OS_POSIX && !OS_MACOSX
#if !defined(CHROME_MULTIPLE_DLL_BROWSER)
gin::V8Initializer::LoadV8ContextSnapshot();
#endif // !CHROME_MULTIPLE_DLL_BROWSER
}

void LoadV8SnapshotFile() {
#if defined(USE_V8_CONTEXT_SNAPSHOT)
static constexpr gin::V8Initializer::V8SnapshotFileType kSnapshotType =
gin::V8Initializer::V8SnapshotFileType::kWithAdditionalContext;
static const char* snapshot_data_descriptor =
kV8ContextSnapshotDataDescriptor;
#else
static constexpr gin::V8Initializer::V8SnapshotFileType kSnapshotType =
gin::V8Initializer::V8SnapshotFileType::kDefault;
static const char* snapshot_data_descriptor = kV8SnapshotDataDescriptor;
#endif // USE_V8_CONTEXT_SNAPSHOT
ALLOW_UNUSED_LOCAL(kSnapshotType);
ALLOW_UNUSED_LOCAL(snapshot_data_descriptor);

#if defined(OS_POSIX) && !defined(OS_MACOSX)
base::FileDescriptorStore& file_descriptor_store =
base::FileDescriptorStore::GetInstance();
base::MemoryMappedFile::Region region;
base::ScopedFD fd =
file_descriptor_store.MaybeTakeFD(kV8SnapshotDataDescriptor, &region);
file_descriptor_store.MaybeTakeFD(snapshot_data_descriptor, &region);
if (fd.is_valid()) {
gin::V8Initializer::LoadV8SnapshotFromFD(fd.get(), region.offset,
region.size);
region.size, kSnapshotType);
return;
}
#endif // OS_POSIX && !OS_MACOSX
#if !defined(CHROME_MULTIPLE_DLL_BROWSER)
gin::V8Initializer::LoadV8Snapshot();
gin::V8Initializer::LoadV8Snapshot(kSnapshotType);
#endif // !CHROME_MULTIPLE_DLL_BROWSER
}

Expand Down Expand Up @@ -232,7 +228,6 @@ void InitializeV8IfNeeded(const base::CommandLine& command_line,
#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
LoadV8SnapshotFile();
LoadV8NativesFile();
LoadV8ContextSnapshotFile();
#endif // V8_USE_EXTERNAL_STARTUP_DATA
}

Expand Down
18 changes: 13 additions & 5 deletions content/public/test/content_test_suite_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@

namespace content {

namespace {
#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
#if defined(USE_V8_CONTEXT_SNAPSHOT)
constexpr gin::V8Initializer::V8SnapshotFileType kSnapshotType =
gin::V8Initializer::V8SnapshotFileType::kWithAdditionalContext;
#else
constexpr gin::V8Initializer::V8SnapshotFileType kSnapshotType =
gin::V8Initializer::V8SnapshotFileType::kDefault;
#endif
#endif
}

ContentTestSuiteBase::ContentTestSuiteBase(int argc, char** argv)
: base::TestSuite(argc, argv) {
}
Expand All @@ -40,14 +52,10 @@ void ContentTestSuiteBase::Initialize() {
base::TestSuite::Initialize();

#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
gin::V8Initializer::LoadV8Snapshot();
gin::V8Initializer::LoadV8Snapshot(kSnapshotType);
gin::V8Initializer::LoadV8Natives();
#endif

#if defined(USE_V8_CONTEXT_SNAPSHOT)
gin::V8Initializer::LoadV8ContextSnapshot();
#endif

#if defined(OS_ANDROID) && !defined(USE_AURA)
content::Compositor::Initialize();
#endif
Expand Down
4 changes: 0 additions & 4 deletions content/shell/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -566,10 +566,6 @@ if (is_android) {
"//tools/v8_context_snapshot:v8_context_snapshot",
]

data = [
"$root_out_dir/v8_context_snapshot.bin",
]

if (is_win) {
deps += [ "//sandbox" ]

Expand Down
15 changes: 11 additions & 4 deletions content/test/test_blink_web_unit_test_support.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ class WebURLLoaderFactoryWithMock : public blink::WebURLLoaderFactory {
DISALLOW_COPY_AND_ASSIGN(WebURLLoaderFactoryWithMock);
};

#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
#if defined(USE_V8_CONTEXT_SNAPSHOT)
constexpr gin::V8Initializer::V8SnapshotFileType kSnapshotType =
gin::V8Initializer::V8SnapshotFileType::kWithAdditionalContext;
#else
constexpr gin::V8Initializer::V8SnapshotFileType kSnapshotType =
gin::V8Initializer::V8SnapshotFileType::kDefault;
#endif
#endif

} // namespace

namespace content {
Expand All @@ -131,13 +141,10 @@ TestBlinkWebUnitTestSupport::TestBlinkWebUnitTestSupport()
mock_clipboard_.reset(new MockWebClipboardImpl());

#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
gin::V8Initializer::LoadV8Snapshot();
gin::V8Initializer::LoadV8Snapshot(kSnapshotType);
gin::V8Initializer::LoadV8Natives();
#endif

#if defined(USE_V8_CONTEXT_SNAPSHOT)
gin::V8Initializer::LoadV8ContextSnapshot();
#endif

scoped_refptr<base::SingleThreadTaskRunner> dummy_task_runner;
std::unique_ptr<base::ThreadTaskRunnerHandle> dummy_task_runner_handle;
Expand Down
97 changes: 37 additions & 60 deletions gin/isolate_holder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,56 +41,55 @@ IsolateHolder::IsolateHolder(
: IsolateHolder(std::move(task_runner),
access_mode,
kAllowAtomicsWait,
nullptr) {}
IsolateCreationMode::kNormal) {}

IsolateHolder::IsolateHolder(
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
AccessMode access_mode,
AllowAtomicsWaitMode atomics_wait_mode,
v8::StartupData* startup_data)
IsolateCreationMode isolate_creation_mode)
: access_mode_(access_mode) {
DCHECK(task_runner);
DCHECK(task_runner->BelongsToCurrentThread());

v8::ArrayBuffer::Allocator* allocator = g_array_buffer_allocator;
CHECK(allocator) << "You need to invoke gin::IsolateHolder::Initialize first";

v8::Isolate::CreateParams params;
params.entry_hook = DebugImpl::GetFunctionEntryHook();
params.code_event_handler = DebugImpl::GetJitCodeEventHandler();
params.constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(),
base::SysInfo::AmountOfVirtualMemory());
params.array_buffer_allocator = allocator;
params.allow_atomics_wait = atomics_wait_mode == kAllowAtomicsWait;
params.external_references = g_reference_table;

if (startup_data) {
CHECK(g_reference_table);
V8Initializer::GetV8ContextSnapshotData(startup_data);
if (startup_data->data) {
params.snapshot_blob = startup_data;
}
if (isolate_creation_mode == IsolateCreationMode::kCreateSnapshot) {
// This branch is called when creating a V8 snapshot for Blink.
// Note SnapshotCreator calls isolate->Enter() in its construction.
snapshot_creator_.reset(new v8::SnapshotCreator(g_reference_table));
isolate_ = snapshot_creator_->GetIsolate();
} else {
v8::Isolate::CreateParams params;
params.entry_hook = DebugImpl::GetFunctionEntryHook();
params.code_event_handler = DebugImpl::GetJitCodeEventHandler();
params.constraints.ConfigureDefaults(
base::SysInfo::AmountOfPhysicalMemory(),
base::SysInfo::AmountOfVirtualMemory());
params.array_buffer_allocator = allocator;
params.allow_atomics_wait =
atomics_wait_mode == AllowAtomicsWaitMode::kAllowAtomicsWait;
params.external_references = g_reference_table;

isolate_ = v8::Isolate::New(params);
}
isolate_ = v8::Isolate::New(params);

SetUp(task_runner);
}

IsolateHolder::IsolateHolder(
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
v8::StartupData* existing_blob)
: access_mode_(AccessMode::kSingleThread) {
CHECK(existing_blob);

v8::StartupData unused_natives;
V8Initializer::GetV8ExternalSnapshotData(&unused_natives, existing_blob);
if (!existing_blob->data) {
existing_blob = nullptr;
isolate_data_.reset(
new PerIsolateData(isolate_, allocator, access_mode_, task_runner));
isolate_memory_dump_provider_.reset(
new V8IsolateMemoryDumpProvider(this, task_runner));
#if defined(OS_WIN)
{
void* code_range;
size_t size;
isolate_->GetCodeRange(&code_range, &size);
Debug::CodeRangeCreatedCallback callback =
DebugImpl::GetCodeRangeCreatedCallback();
if (code_range && size && callback)
callback(code_range, size);
}

snapshot_creator_.reset(
new v8::SnapshotCreator(g_reference_table, existing_blob));
isolate_ = snapshot_creator_->GetIsolate();

DCHECK(task_runner->BelongsToCurrentThread());
SetUp(task_runner);
#endif
}

IsolateHolder::~IsolateHolder() {
Expand Down Expand Up @@ -142,26 +141,4 @@ void IsolateHolder::EnableIdleTasks(
isolate_data_->EnableIdleTasks(std::move(idle_task_runner));
}

void IsolateHolder::SetUp(
scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
DCHECK(task_runner);
v8::ArrayBuffer::Allocator* allocator = g_array_buffer_allocator;
CHECK(allocator) << "You need to invoke gin::IsolateHolder::Initialize first";
isolate_data_.reset(
new PerIsolateData(isolate_, allocator, access_mode_, task_runner));
isolate_memory_dump_provider_.reset(
new V8IsolateMemoryDumpProvider(this, task_runner));
#if defined(OS_WIN)
{
void* code_range;
size_t size;
isolate_->GetCodeRange(&code_range, &size);
Debug::CodeRangeCreatedCallback callback =
DebugImpl::GetCodeRangeCreatedCallback();
if (code_range && size && callback)
callback(code_range, size);
}
#endif
}

} // namespace gin
21 changes: 11 additions & 10 deletions gin/public/isolate_holder.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,21 @@ class GIN_EXPORT IsolateHolder {
kStableAndExperimentalV8Extras,
};

// Indicates how the Isolate instance will be created.
enum class IsolateCreationMode {
kNormal,
kCreateSnapshot,
};

explicit IsolateHolder(
scoped_refptr<base::SingleThreadTaskRunner> task_runner);
IsolateHolder(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
AccessMode access_mode);
IsolateHolder(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
AccessMode access_mode,
AllowAtomicsWaitMode atomics_wait_mode,
v8::StartupData* startup_data);

// This constructor is to create V8 snapshot for Blink.
// Note this constructor calls isolate->Enter() internally.
IsolateHolder(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
v8::StartupData* existing_blob);

IsolateHolder(
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
AccessMode access_mode,
AllowAtomicsWaitMode atomics_wait_mode,
IsolateCreationMode isolate_creation_mode = IsolateCreationMode::kNormal);
~IsolateHolder();

// Should be invoked once before creating IsolateHolder instances to
Expand Down
Loading

0 comments on commit 9aff02e

Please sign in to comment.