1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // The common functionality when building with or without snapshots.
6 
7 #include "src/snapshot/snapshot.h"
8 
9 #include "src/api.h"
10 #include "src/base/platform/platform.h"
11 #include "src/full-codegen/full-codegen.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 #ifdef DEBUG
SnapshotIsValid(v8::StartupData * snapshot_blob)17 bool Snapshot::SnapshotIsValid(v8::StartupData* snapshot_blob) {
18   return !Snapshot::ExtractStartupData(snapshot_blob).is_empty() &&
19          !Snapshot::ExtractContextData(snapshot_blob).is_empty();
20 }
21 #endif  // DEBUG
22 
23 
HaveASnapshotToStartFrom(Isolate * isolate)24 bool Snapshot::HaveASnapshotToStartFrom(Isolate* isolate) {
25   // Do not use snapshots if the isolate is used to create snapshots.
26   return isolate->snapshot_blob() != NULL &&
27          isolate->snapshot_blob()->data != NULL;
28 }
29 
30 
EmbedsScript(Isolate * isolate)31 bool Snapshot::EmbedsScript(Isolate* isolate) {
32   if (!isolate->snapshot_available()) return false;
33   return ExtractMetadata(isolate->snapshot_blob()).embeds_script();
34 }
35 
36 
SizeOfFirstPage(Isolate * isolate,AllocationSpace space)37 uint32_t Snapshot::SizeOfFirstPage(Isolate* isolate, AllocationSpace space) {
38   DCHECK(space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE);
39   if (!isolate->snapshot_available()) {
40     return static_cast<uint32_t>(MemoryAllocator::PageAreaSize(space));
41   }
42   uint32_t size;
43   int offset = kFirstPageSizesOffset + (space - FIRST_PAGED_SPACE) * kInt32Size;
44   memcpy(&size, isolate->snapshot_blob()->data + offset, kInt32Size);
45   return size;
46 }
47 
48 
Initialize(Isolate * isolate)49 bool Snapshot::Initialize(Isolate* isolate) {
50   if (!isolate->snapshot_available()) return false;
51   base::ElapsedTimer timer;
52   if (FLAG_profile_deserialization) timer.Start();
53 
54   const v8::StartupData* blob = isolate->snapshot_blob();
55   Vector<const byte> startup_data = ExtractStartupData(blob);
56   SnapshotData snapshot_data(startup_data);
57   Deserializer deserializer(&snapshot_data);
58   bool success = isolate->Init(&deserializer);
59   if (FLAG_profile_deserialization) {
60     double ms = timer.Elapsed().InMillisecondsF();
61     int bytes = startup_data.length();
62     PrintF("[Deserializing isolate (%d bytes) took %0.3f ms]\n", bytes, ms);
63   }
64   return success;
65 }
66 
67 
NewContextFromSnapshot(Isolate * isolate,Handle<JSGlobalProxy> global_proxy)68 MaybeHandle<Context> Snapshot::NewContextFromSnapshot(
69     Isolate* isolate, Handle<JSGlobalProxy> global_proxy) {
70   if (!isolate->snapshot_available()) return Handle<Context>();
71   base::ElapsedTimer timer;
72   if (FLAG_profile_deserialization) timer.Start();
73 
74   const v8::StartupData* blob = isolate->snapshot_blob();
75   Vector<const byte> context_data = ExtractContextData(blob);
76   SnapshotData snapshot_data(context_data);
77   Deserializer deserializer(&snapshot_data);
78 
79   MaybeHandle<Object> maybe_context =
80       deserializer.DeserializePartial(isolate, global_proxy);
81   Handle<Object> result;
82   if (!maybe_context.ToHandle(&result)) return MaybeHandle<Context>();
83   CHECK(result->IsContext());
84   if (FLAG_profile_deserialization) {
85     double ms = timer.Elapsed().InMillisecondsF();
86     int bytes = context_data.length();
87     PrintF("[Deserializing context (%d bytes) took %0.3f ms]\n", bytes, ms);
88   }
89   return Handle<Context>::cast(result);
90 }
91 
92 
CalculateFirstPageSizes(bool is_default_snapshot,const SnapshotData & startup_snapshot,const SnapshotData & context_snapshot,uint32_t * sizes_out)93 void CalculateFirstPageSizes(bool is_default_snapshot,
94                              const SnapshotData& startup_snapshot,
95                              const SnapshotData& context_snapshot,
96                              uint32_t* sizes_out) {
97   Vector<const SerializedData::Reservation> startup_reservations =
98       startup_snapshot.Reservations();
99   Vector<const SerializedData::Reservation> context_reservations =
100       context_snapshot.Reservations();
101   int startup_index = 0;
102   int context_index = 0;
103 
104   if (FLAG_profile_deserialization) {
105     int startup_total = 0;
106     int context_total = 0;
107     for (auto& reservation : startup_reservations) {
108       startup_total += reservation.chunk_size();
109     }
110     for (auto& reservation : context_reservations) {
111       context_total += reservation.chunk_size();
112     }
113     PrintF(
114         "Deserialization will reserve:\n"
115         "%10d bytes per isolate\n"
116         "%10d bytes per context\n",
117         startup_total, context_total);
118   }
119 
120   for (int space = 0; space < i::Serializer::kNumberOfSpaces; space++) {
121     bool single_chunk = true;
122     while (!startup_reservations[startup_index].is_last()) {
123       single_chunk = false;
124       startup_index++;
125     }
126     while (!context_reservations[context_index].is_last()) {
127       single_chunk = false;
128       context_index++;
129     }
130 
131     uint32_t required = kMaxUInt32;
132     if (single_chunk) {
133       // If both the startup snapshot data and the context snapshot data on
134       // this space fit in a single page, then we consider limiting the size
135       // of the first page. For this, we add the chunk sizes and some extra
136       // allowance. This way we achieve a smaller startup memory footprint.
137       required = (startup_reservations[startup_index].chunk_size() +
138                   2 * context_reservations[context_index].chunk_size()) +
139                  Page::kObjectStartOffset;
140       // Add a small allowance to the code space for small scripts.
141       if (space == CODE_SPACE) required += 32 * KB;
142     } else {
143       // We expect the vanilla snapshot to only require on page per space.
144       DCHECK(!is_default_snapshot);
145     }
146 
147     if (space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE) {
148       uint32_t max_size =
149           MemoryAllocator::PageAreaSize(static_cast<AllocationSpace>(space));
150       sizes_out[space - FIRST_PAGED_SPACE] = Min(required, max_size);
151     } else {
152       DCHECK(single_chunk);
153     }
154     startup_index++;
155     context_index++;
156   }
157 
158   DCHECK_EQ(startup_reservations.length(), startup_index);
159   DCHECK_EQ(context_reservations.length(), context_index);
160 }
161 
162 
CreateSnapshotBlob(const i::StartupSerializer & startup_ser,const i::PartialSerializer & context_ser,Snapshot::Metadata metadata)163 v8::StartupData Snapshot::CreateSnapshotBlob(
164     const i::StartupSerializer& startup_ser,
165     const i::PartialSerializer& context_ser, Snapshot::Metadata metadata) {
166   SnapshotData startup_snapshot(startup_ser);
167   SnapshotData context_snapshot(context_ser);
168   Vector<const byte> startup_data = startup_snapshot.RawData();
169   Vector<const byte> context_data = context_snapshot.RawData();
170 
171   uint32_t first_page_sizes[kNumPagedSpaces];
172 
173   CalculateFirstPageSizes(!metadata.embeds_script(), startup_snapshot,
174                           context_snapshot, first_page_sizes);
175 
176   int startup_length = startup_data.length();
177   int context_length = context_data.length();
178   int context_offset = ContextOffset(startup_length);
179 
180   int length = context_offset + context_length;
181   char* data = new char[length];
182 
183   memcpy(data + kMetadataOffset, &metadata.RawValue(), kInt32Size);
184   memcpy(data + kFirstPageSizesOffset, first_page_sizes,
185          kNumPagedSpaces * kInt32Size);
186   memcpy(data + kStartupLengthOffset, &startup_length, kInt32Size);
187   memcpy(data + kStartupDataOffset, startup_data.begin(), startup_length);
188   memcpy(data + context_offset, context_data.begin(), context_length);
189   v8::StartupData result = {data, length};
190 
191   if (FLAG_profile_deserialization) {
192     PrintF(
193         "Snapshot blob consists of:\n"
194         "%10d bytes for startup\n"
195         "%10d bytes for context\n",
196         startup_length, context_length);
197   }
198   return result;
199 }
200 
201 
ExtractMetadata(const v8::StartupData * data)202 Snapshot::Metadata Snapshot::ExtractMetadata(const v8::StartupData* data) {
203   uint32_t raw;
204   memcpy(&raw, data->data + kMetadataOffset, kInt32Size);
205   return Metadata(raw);
206 }
207 
208 
ExtractStartupData(const v8::StartupData * data)209 Vector<const byte> Snapshot::ExtractStartupData(const v8::StartupData* data) {
210   DCHECK_LT(kIntSize, data->raw_size);
211   int startup_length;
212   memcpy(&startup_length, data->data + kStartupLengthOffset, kInt32Size);
213   DCHECK_LT(startup_length, data->raw_size);
214   const byte* startup_data =
215       reinterpret_cast<const byte*>(data->data + kStartupDataOffset);
216   return Vector<const byte>(startup_data, startup_length);
217 }
218 
219 
ExtractContextData(const v8::StartupData * data)220 Vector<const byte> Snapshot::ExtractContextData(const v8::StartupData* data) {
221   DCHECK_LT(kIntSize, data->raw_size);
222   int startup_length;
223   memcpy(&startup_length, data->data + kStartupLengthOffset, kIntSize);
224   int context_offset = ContextOffset(startup_length);
225   const byte* context_data =
226       reinterpret_cast<const byte*>(data->data + context_offset);
227   DCHECK_LT(context_offset, data->raw_size);
228   int context_length = data->raw_size - context_offset;
229   return Vector<const byte>(context_data, context_length);
230 }
231 }  // namespace internal
232 }  // namespace v8
233