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.
5 // The common functionality when building with or without snapshots.
10 #include "src/base/platform/platform.h"
11 #include "src/full-codegen.h"
12 #include "src/snapshot/snapshot.h"
18 bool Snapshot::SnapshotIsValid(v8::StartupData* snapshot_blob) {
19 return !Snapshot::ExtractStartupData(snapshot_blob).is_empty() &&
20 !Snapshot::ExtractContextData(snapshot_blob).is_empty();
25 bool Snapshot::EmbedsScript(Isolate* isolate) {
26 if (!isolate->snapshot_available()) return false;
27 return ExtractMetadata(isolate->snapshot_blob()).embeds_script();
31 uint32_t Snapshot::SizeOfFirstPage(Isolate* isolate, AllocationSpace space) {
32 DCHECK(space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE);
33 if (!isolate->snapshot_available()) {
34 return static_cast<uint32_t>(MemoryAllocator::PageAreaSize(space));
37 int offset = kFirstPageSizesOffset + (space - FIRST_PAGED_SPACE) * kInt32Size;
38 memcpy(&size, isolate->snapshot_blob()->data + offset, kInt32Size);
43 bool Snapshot::Initialize(Isolate* isolate) {
44 if (!isolate->snapshot_available()) return false;
45 base::ElapsedTimer timer;
46 if (FLAG_profile_deserialization) timer.Start();
48 const v8::StartupData* blob = isolate->snapshot_blob();
49 Vector<const byte> startup_data = ExtractStartupData(blob);
50 SnapshotData snapshot_data(startup_data);
51 Deserializer deserializer(&snapshot_data);
52 bool success = isolate->Init(&deserializer);
53 if (FLAG_profile_deserialization) {
54 double ms = timer.Elapsed().InMillisecondsF();
55 int bytes = startup_data.length();
56 PrintF("[Deserializing isolate (%d bytes) took %0.3f ms]\n", bytes, ms);
62 MaybeHandle<Context> Snapshot::NewContextFromSnapshot(
63 Isolate* isolate, Handle<JSGlobalProxy> global_proxy,
64 Handle<FixedArray>* outdated_contexts_out) {
65 if (!isolate->snapshot_available()) return Handle<Context>();
66 base::ElapsedTimer timer;
67 if (FLAG_profile_deserialization) timer.Start();
69 const v8::StartupData* blob = isolate->snapshot_blob();
70 Vector<const byte> context_data = ExtractContextData(blob);
71 SnapshotData snapshot_data(context_data);
72 Deserializer deserializer(&snapshot_data);
74 MaybeHandle<Object> maybe_context = deserializer.DeserializePartial(
75 isolate, global_proxy, outdated_contexts_out);
76 Handle<Object> result;
77 if (!maybe_context.ToHandle(&result)) return MaybeHandle<Context>();
78 CHECK(result->IsContext());
79 // If the snapshot does not contain a custom script, we need to update
80 // the global object for exactly one context.
81 CHECK(EmbedsScript(isolate) || (*outdated_contexts_out)->length() == 1);
82 if (FLAG_profile_deserialization) {
83 double ms = timer.Elapsed().InMillisecondsF();
84 int bytes = context_data.length();
85 PrintF("[Deserializing context (%d bytes) took %0.3f ms]\n", bytes, ms);
87 return Handle<Context>::cast(result);
91 void CalculateFirstPageSizes(bool is_default_snapshot,
92 const SnapshotData& startup_snapshot,
93 const SnapshotData& context_snapshot,
94 uint32_t* sizes_out) {
95 Vector<const SerializedData::Reservation> startup_reservations =
96 startup_snapshot.Reservations();
97 Vector<const SerializedData::Reservation> context_reservations =
98 context_snapshot.Reservations();
99 int startup_index = 0;
100 int context_index = 0;
102 if (FLAG_profile_deserialization) {
103 int startup_total = 0;
104 int context_total = 0;
105 for (auto& reservation : startup_reservations) {
106 startup_total += reservation.chunk_size();
108 for (auto& reservation : context_reservations) {
109 context_total += reservation.chunk_size();
112 "Deserialization will reserve:\n"
113 "%10d bytes for startup\n"
114 "%10d bytes per context\n",
115 startup_total, context_total);
118 for (int space = 0; space < i::Serializer::kNumberOfSpaces; space++) {
119 bool single_chunk = true;
120 while (!startup_reservations[startup_index].is_last()) {
121 single_chunk = false;
124 while (!context_reservations[context_index].is_last()) {
125 single_chunk = false;
129 uint32_t required = kMaxUInt32;
131 // If both the startup snapshot data and the context snapshot data on
132 // this space fit in a single page, then we consider limiting the size
133 // of the first page. For this, we add the chunk sizes and some extra
134 // allowance. This way we achieve a smaller startup memory footprint.
135 required = (startup_reservations[startup_index].chunk_size() +
136 2 * context_reservations[context_index].chunk_size()) +
137 Page::kObjectStartOffset;
138 // Add a small allowance to the code space for small scripts.
139 if (space == CODE_SPACE) required += 32 * KB;
141 // We expect the vanilla snapshot to only require on page per space.
142 DCHECK(!is_default_snapshot);
145 if (space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE) {
147 MemoryAllocator::PageAreaSize(static_cast<AllocationSpace>(space));
148 sizes_out[space - FIRST_PAGED_SPACE] = Min(required, max_size);
150 DCHECK(single_chunk);
156 DCHECK_EQ(startup_reservations.length(), startup_index);
157 DCHECK_EQ(context_reservations.length(), context_index);
161 v8::StartupData Snapshot::CreateSnapshotBlob(
162 const i::StartupSerializer& startup_ser,
163 const i::PartialSerializer& context_ser, Snapshot::Metadata metadata) {
164 SnapshotData startup_snapshot(startup_ser);
165 SnapshotData context_snapshot(context_ser);
166 Vector<const byte> startup_data = startup_snapshot.RawData();
167 Vector<const byte> context_data = context_snapshot.RawData();
169 uint32_t first_page_sizes[kNumPagedSpaces];
171 CalculateFirstPageSizes(!metadata.embeds_script(), startup_snapshot,
172 context_snapshot, first_page_sizes);
174 int startup_length = startup_data.length();
175 int context_length = context_data.length();
176 int context_offset = ContextOffset(startup_length);
178 int length = context_offset + context_length;
179 char* data = new char[length];
181 memcpy(data + kMetadataOffset, &metadata.RawValue(), kInt32Size);
182 memcpy(data + kFirstPageSizesOffset, first_page_sizes,
183 kNumPagedSpaces * kInt32Size);
184 memcpy(data + kStartupLengthOffset, &startup_length, kInt32Size);
185 memcpy(data + kStartupDataOffset, startup_data.begin(), startup_length);
186 memcpy(data + context_offset, context_data.begin(), context_length);
187 v8::StartupData result = {data, length};
189 if (FLAG_profile_deserialization) {
191 "Snapshot blob consists of:\n"
192 "%10d bytes for startup\n"
193 "%10d bytes for context\n",
194 startup_length, context_length);
200 Snapshot::Metadata Snapshot::ExtractMetadata(const v8::StartupData* data) {
202 memcpy(&raw, data->data + kMetadataOffset, kInt32Size);
203 return Metadata(raw);
207 Vector<const byte> Snapshot::ExtractStartupData(const v8::StartupData* data) {
208 DCHECK_LT(kIntSize, data->raw_size);
210 memcpy(&startup_length, data->data + kStartupLengthOffset, kInt32Size);
211 DCHECK_LT(startup_length, data->raw_size);
212 const byte* startup_data =
213 reinterpret_cast<const byte*>(data->data + kStartupDataOffset);
214 return Vector<const byte>(startup_data, startup_length);
218 Vector<const byte> Snapshot::ExtractContextData(const v8::StartupData* data) {
219 DCHECK_LT(kIntSize, data->raw_size);
221 memcpy(&startup_length, data->data + kStartupLengthOffset, kIntSize);
222 int context_offset = ContextOffset(startup_length);
223 const byte* context_data =
224 reinterpret_cast<const byte*>(data->data + context_offset);
225 DCHECK_LT(context_offset, data->raw_size);
226 int context_length = data->raw_size - context_offset;
227 return Vector<const byte>(context_data, context_length);
229 } } // namespace v8::internal