Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Side by Side Diff: gin/isolate_holder.cc

Issue 594603003: Infrastructure for reading V8's initial snapshot from external files (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Properly override v8_use_external_snapshot in v8 Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium 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 "gin/public/isolate_holder.h" 5 #include "gin/public/isolate_holder.h"
6 6
7 #include <stdlib.h> 7 #include <stdlib.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
12 #include "base/path_service.h"
13 #endif // V8_USE_EXTERNAL_STARTUP_DATA
11 #include "base/rand_util.h" 14 #include "base/rand_util.h"
12 #include "base/sys_info.h" 15 #include "base/sys_info.h"
13 #include "gin/array_buffer.h" 16 #include "gin/array_buffer.h"
14 #include "gin/debug_impl.h" 17 #include "gin/debug_impl.h"
15 #include "gin/function_template.h" 18 #include "gin/function_template.h"
16 #include "gin/per_isolate_data.h" 19 #include "gin/per_isolate_data.h"
17 #include "gin/public/v8_platform.h" 20 #include "gin/public/v8_platform.h"
18 21
19 namespace gin { 22 namespace gin {
20 23
21 namespace { 24 namespace {
22 25
23 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL; 26 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL;
24 27
25 bool GenerateEntropy(unsigned char* buffer, size_t amount) { 28 bool GenerateEntropy(unsigned char* buffer, size_t amount) {
26 base::RandBytes(buffer, amount); 29 base::RandBytes(buffer, amount);
27 return true; 30 return true;
28 } 31 }
29 32
30 } // namespace 33 } // namespace
31 34
35 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
36 // static
37 base::MemoryMappedFile IsolateHolder::mapped_natives;
38 base::MemoryMappedFile IsolateHolder::mapped_snapshot;
rmcilroy 2014/09/23 10:04:20 We only allow POD types (not class objects) as sta
baixo 2014/09/23 11:36:46 Done.
39 bool IsolateHolder::read_snapshot = false;
40 #endif // V8_USE_EXTERNAL_STARTUP_DATA
41
42 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) && defined(OS_ANDROID)
43 //static
44 bool IsolateHolder::LoadV8SnapshotFD(int natives_fd, int snapshot_fd) {
45 if (read_snapshot)
46 return true;
47
48 if (!mapped_natives.IsValid()) {
49 if (!mapped_natives.Initialize(base::File(natives_fd))) {
50 LOG(ERROR) << "Couldn't mmap v8 natives data file";
51 return false;
52 }
53 }
54
55 if (!mapped_snapshot.IsValid()) {
56 if (!mapped_snapshot.Initialize(base::File(snapshot_fd))) {
57 LOG(ERROR) << "Couldn't mmap v8 snapshot data file";
58 return false;
59 }
60 }
61
62 read_snapshot = true;
63 return true;
64 }
65 #endif // V8_USE_EXTERNAL_STARTUP_DATA && OS_ANDROID
66
67 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
68 // static
69 bool IsolateHolder::LoadV8Snapshot() {
70 if (read_snapshot)
71 return true;
72
73 #if !defined(OS_MACOSX)
74 base::FilePath data_path;
75 PathService::Get(
76 #if defined(OS_ANDROID)
77 base::DIR_ANDROID_APP_DATA,
78 #elif defined(OS_POSIX) || defined(OS_WIN)
79 base::DIR_EXE,
80 #endif
81 &data_path);
82 #else
83 base::FilePath data_path = base::mac::FrameworkBundlePath();
84 #endif
85 DCHECK(!data_path.empty());
86
87 if (!mapped_natives.IsValid()) {
88 base::FilePath natives_data_path =
89 data_path.AppendASCII("natives_blob.bin");
90 if (!mapped_natives.Initialize(natives_data_path)) {
91 LOG(ERROR) << "Couldn't mmap v8 natives data file";
92 return false;
93 }
94 }
95 if (!mapped_snapshot.IsValid()) {
96 base::FilePath snapshot_data_path =
97 data_path.AppendASCII("snapshot_blob.bin");
98 if (!mapped_snapshot.Initialize(snapshot_data_path)) {
99 LOG(ERROR) << "Couldn't mmap v8 snapshot data file";
100 return false;
101 }
102 }
103
104 read_snapshot = true;
105 return true;
106 }
107 #endif // V8_USE_EXTERNAL_STARTUP_DATA
108
32 IsolateHolder::IsolateHolder() { 109 IsolateHolder::IsolateHolder() {
33 CHECK(g_array_buffer_allocator) 110 CHECK(g_array_buffer_allocator)
34 << "You need to invoke gin::IsolateHolder::Initialize first"; 111 << "You need to invoke gin::IsolateHolder::Initialize first";
35 v8::Isolate::CreateParams params; 112 v8::Isolate::CreateParams params;
36 params.entry_hook = DebugImpl::GetFunctionEntryHook(); 113 params.entry_hook = DebugImpl::GetFunctionEntryHook();
37 params.code_event_handler = DebugImpl::GetJitCodeEventHandler(); 114 params.code_event_handler = DebugImpl::GetJitCodeEventHandler();
38 params.constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), 115 params.constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(),
39 base::SysInfo::AmountOfVirtualMemory(), 116 base::SysInfo::AmountOfVirtualMemory(),
40 base::SysInfo::NumberOfProcessors()); 117 base::SysInfo::NumberOfProcessors());
41 isolate_ = v8::Isolate::New(params); 118 isolate_ = v8::Isolate::New(params);
(...skipping 13 matching lines...) Expand all
55 if (v8_is_initialized) 132 if (v8_is_initialized)
56 return; 133 return;
57 v8::V8::InitializePlatform(V8Platform::Get()); 134 v8::V8::InitializePlatform(V8Platform::Get());
58 v8::V8::SetArrayBufferAllocator(allocator); 135 v8::V8::SetArrayBufferAllocator(allocator);
59 g_array_buffer_allocator = allocator; 136 g_array_buffer_allocator = allocator;
60 if (mode == gin::IsolateHolder::kStrictMode) { 137 if (mode == gin::IsolateHolder::kStrictMode) {
61 static const char v8_flags[] = "--use_strict"; 138 static const char v8_flags[] = "--use_strict";
62 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); 139 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1);
63 } 140 }
64 v8::V8::SetEntropySource(&GenerateEntropy); 141 v8::V8::SetEntropySource(&GenerateEntropy);
142 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
143 v8::StartupData natives;
144 natives.data = reinterpret_cast<const char*>(mapped_natives.data());
145 natives.raw_size = mapped_natives.length();
146 natives.compressed_size = mapped_natives.length();
147 v8::V8::SetNativesDataBlob(&natives);
148
149 v8::StartupData snapshot;
150 snapshot.data = reinterpret_cast<const char*>(mapped_snapshot.data());
151 snapshot.raw_size = mapped_snapshot.length();
152 snapshot.compressed_size = mapped_snapshot.length();
153 v8::V8::SetSnapshotDataBlob(&snapshot);
154 #endif // V8_USE_EXTERNAL_STARTUP_DATA
65 v8::V8::Initialize(); 155 v8::V8::Initialize();
66 v8_is_initialized = true; 156 v8_is_initialized = true;
67 } 157 }
68 158
69 } // namespace gin 159 } // namespace gin
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698