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

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: Turn static variables into pointers and remove v8 init in net 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
13 #ifdef OS_MACOSX
14 #include "base/mac/bundle_locations.h"
15 #endif
16
rmcilroy 2014/09/23 14:05:40 nit - Generally Ifdef'ed defines are placed in a s
baixo 2014/09/23 19:30:30 Done.
17 #include "base/path_service.h"
18 #endif // V8_USE_EXTERNAL_STARTUP_DATA
11 #include "base/rand_util.h" 19 #include "base/rand_util.h"
12 #include "base/sys_info.h" 20 #include "base/sys_info.h"
13 #include "gin/array_buffer.h" 21 #include "gin/array_buffer.h"
14 #include "gin/debug_impl.h" 22 #include "gin/debug_impl.h"
15 #include "gin/function_template.h" 23 #include "gin/function_template.h"
16 #include "gin/per_isolate_data.h" 24 #include "gin/per_isolate_data.h"
17 #include "gin/public/v8_platform.h" 25 #include "gin/public/v8_platform.h"
18 26
19 namespace gin { 27 namespace gin {
20 28
21 namespace { 29 namespace {
22 30
23 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL; 31 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL;
24 32
25 bool GenerateEntropy(unsigned char* buffer, size_t amount) { 33 bool GenerateEntropy(unsigned char* buffer, size_t amount) {
26 base::RandBytes(buffer, amount); 34 base::RandBytes(buffer, amount);
27 return true; 35 return true;
28 } 36 }
29 37
30 } // namespace 38 } // namespace
31 39
40 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
41 // static
42 base::MemoryMappedFile* IsolateHolder::mapped_natives = NULL;
43 base::MemoryMappedFile* IsolateHolder::mapped_snapshot = NULL;
rmcilroy 2014/09/23 14:05:40 As mentioned, these should be globals, not static
baixo 2014/09/23 19:30:30 Done.
44 #endif // V8_USE_EXTERNAL_STARTUP_DATA
45
46 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) && defined(OS_ANDROID)
47 //static
48 bool IsolateHolder::LoadV8SnapshotFD(int natives_fd, int snapshot_fd) {
49 if (mapped_natives && mapped_snapshot)
50 return true;
51
52 mapped_natives = new base::MemoryMappedFile;
53 CHECK(mapped_natives);
54 if (!mapped_natives->IsValid()) {
55 if (!mapped_natives->Initialize(base::File(natives_fd))) {
rmcilroy 2014/09/23 14:05:39 create the new mapped_snapshot here rather than ab
baixo 2014/09/23 19:30:30 We need the created object to initialize the file
56 LOG(ERROR) << "Couldn't mmap v8 natives data file";
57 return false;
58 }
59 }
60
61 mapped_snapshot = new base::MemoryMappedFile;
rmcilroy 2014/09/23 14:05:39 ditto
baixo 2014/09/23 19:30:30 Same as above.
62 CHECK(mapped_snapshot);
63 if (!mapped_snapshot->IsValid()) {
64 if (!mapped_snapshot->Initialize(base::File(snapshot_fd))) {
65 LOG(ERROR) << "Couldn't mmap v8 snapshot data file";
66 return false;
67 }
68 }
69
70 return true;
71 }
72 #endif // V8_USE_EXTERNAL_STARTUP_DATA && OS_ANDROID
73
74 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
75 // static
76 bool IsolateHolder::LoadV8Snapshot() {
77 if (mapped_natives && mapped_snapshot)
78 return true;
79
80 #if !defined(OS_MACOSX)
81 base::FilePath data_path;
82 PathService::Get(
83 #if defined(OS_ANDROID)
84 base::DIR_ANDROID_APP_DATA,
85 #elif defined(OS_POSIX) || defined(OS_WIN)
86 base::DIR_EXE,
87 #endif
88 &data_path);
89 #else
90 base::FilePath data_path = base::mac::FrameworkBundlePath();
91 #endif
92 DCHECK(!data_path.empty());
93
94 mapped_natives = new base::MemoryMappedFile;
95 CHECK(mapped_natives);
96 if (!mapped_natives->IsValid()) {
97 base::FilePath natives_data_path =
98 data_path.AppendASCII("natives_blob.bin");
99 if (!mapped_natives->Initialize(natives_data_path)) {
100 LOG(ERROR) << "Couldn't mmap v8 natives data file";
101 return false;
102 }
103 }
rmcilroy 2014/09/23 14:05:39 There seems like a lot of duplication here between
baixo 2014/09/23 19:30:30 Done.
104
105 mapped_snapshot = new base::MemoryMappedFile;
106 CHECK(mapped_snapshot);
107 if (!mapped_snapshot->IsValid()) {
108 base::FilePath snapshot_data_path =
109 data_path.AppendASCII("snapshot_blob.bin");
110 if (!mapped_snapshot->Initialize(snapshot_data_path)) {
111 LOG(ERROR) << "Couldn't mmap v8 snapshot data file";
112 return false;
113 }
114 }
115
116 return true;
117 }
118 #endif // V8_USE_EXTERNAL_STARTUP_DATA
119
32 IsolateHolder::IsolateHolder() { 120 IsolateHolder::IsolateHolder() {
33 CHECK(g_array_buffer_allocator) 121 CHECK(g_array_buffer_allocator)
34 << "You need to invoke gin::IsolateHolder::Initialize first"; 122 << "You need to invoke gin::IsolateHolder::Initialize first";
35 v8::Isolate::CreateParams params; 123 v8::Isolate::CreateParams params;
36 params.entry_hook = DebugImpl::GetFunctionEntryHook(); 124 params.entry_hook = DebugImpl::GetFunctionEntryHook();
37 params.code_event_handler = DebugImpl::GetJitCodeEventHandler(); 125 params.code_event_handler = DebugImpl::GetJitCodeEventHandler();
38 params.constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), 126 params.constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(),
39 base::SysInfo::AmountOfVirtualMemory(), 127 base::SysInfo::AmountOfVirtualMemory(),
40 base::SysInfo::NumberOfProcessors()); 128 base::SysInfo::NumberOfProcessors());
41 isolate_ = v8::Isolate::New(params); 129 isolate_ = v8::Isolate::New(params);
(...skipping 13 matching lines...) Expand all
55 if (v8_is_initialized) 143 if (v8_is_initialized)
56 return; 144 return;
57 v8::V8::InitializePlatform(V8Platform::Get()); 145 v8::V8::InitializePlatform(V8Platform::Get());
58 v8::V8::SetArrayBufferAllocator(allocator); 146 v8::V8::SetArrayBufferAllocator(allocator);
59 g_array_buffer_allocator = allocator; 147 g_array_buffer_allocator = allocator;
60 if (mode == gin::IsolateHolder::kStrictMode) { 148 if (mode == gin::IsolateHolder::kStrictMode) {
61 static const char v8_flags[] = "--use_strict"; 149 static const char v8_flags[] = "--use_strict";
62 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); 150 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1);
63 } 151 }
64 v8::V8::SetEntropySource(&GenerateEntropy); 152 v8::V8::SetEntropySource(&GenerateEntropy);
153 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
154 v8::StartupData natives;
155 natives.data = reinterpret_cast<const char*>(mapped_natives->data());
156 natives.raw_size = mapped_natives->length();
157 natives.compressed_size = mapped_natives->length();
158 v8::V8::SetNativesDataBlob(&natives);
159
160 v8::StartupData snapshot;
161 snapshot.data = reinterpret_cast<const char*>(mapped_snapshot->data());
162 snapshot.raw_size = mapped_snapshot->length();
163 snapshot.compressed_size = mapped_snapshot->length();
164 v8::V8::SetSnapshotDataBlob(&snapshot);
165 #endif // V8_USE_EXTERNAL_STARTUP_DATA
65 v8::V8::Initialize(); 166 v8::V8::Initialize();
66 v8_is_initialized = true; 167 v8_is_initialized = true;
67 } 168 }
68 169
69 } // namespace gin 170 } // namespace gin
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698