OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 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 | 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 // The common functionality when building with or without snapshots. | 5 // The common functionality when building with or without snapshots. |
6 | 6 |
7 #include "src/snapshot/snapshot.h" | 7 #include "src/snapshot/snapshot.h" |
8 | 8 |
9 #include "src/api.h" | 9 #include "src/api.h" |
10 #include "src/base/platform/platform.h" | 10 #include "src/base/platform/platform.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 #endif // DEBUG | 24 #endif // DEBUG |
25 | 25 |
26 | 26 |
27 bool Snapshot::HaveASnapshotToStartFrom(Isolate* isolate) { | 27 bool Snapshot::HaveASnapshotToStartFrom(Isolate* isolate) { |
28 // Do not use snapshots if the isolate is used to create snapshots. | 28 // Do not use snapshots if the isolate is used to create snapshots. |
29 return isolate->snapshot_blob() != NULL && | 29 return isolate->snapshot_blob() != NULL && |
30 isolate->snapshot_blob()->data != NULL; | 30 isolate->snapshot_blob()->data != NULL; |
31 } | 31 } |
32 | 32 |
33 | 33 |
34 bool Snapshot::EmbedsScript(Isolate* isolate) { | |
35 if (!isolate->snapshot_available()) return false; | |
36 return ExtractMetadata(isolate->snapshot_blob()).embeds_script(); | |
37 } | |
38 | |
39 | |
40 uint32_t Snapshot::SizeOfFirstPage(Isolate* isolate, AllocationSpace space) { | 34 uint32_t Snapshot::SizeOfFirstPage(Isolate* isolate, AllocationSpace space) { |
41 DCHECK(space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE); | 35 DCHECK(space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE); |
42 if (!isolate->snapshot_available()) { | 36 if (!isolate->snapshot_available()) { |
43 return static_cast<uint32_t>(MemoryAllocator::PageAreaSize(space)); | 37 return static_cast<uint32_t>(MemoryAllocator::PageAreaSize(space)); |
44 } | 38 } |
45 uint32_t size; | 39 uint32_t size; |
46 int offset = kFirstPageSizesOffset + (space - FIRST_PAGED_SPACE) * kInt32Size; | 40 int offset = kFirstPageSizesOffset + (space - FIRST_PAGED_SPACE) * kInt32Size; |
47 memcpy(&size, isolate->snapshot_blob()->data + offset, kInt32Size); | 41 memcpy(&size, isolate->snapshot_blob()->data + offset, kInt32Size); |
48 return size; | 42 return size; |
49 } | 43 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 if (!maybe_context.ToHandle(&result)) return MaybeHandle<Context>(); | 79 if (!maybe_context.ToHandle(&result)) return MaybeHandle<Context>(); |
86 CHECK(result->IsContext()); | 80 CHECK(result->IsContext()); |
87 if (FLAG_profile_deserialization) { | 81 if (FLAG_profile_deserialization) { |
88 double ms = timer.Elapsed().InMillisecondsF(); | 82 double ms = timer.Elapsed().InMillisecondsF(); |
89 int bytes = context_data.length(); | 83 int bytes = context_data.length(); |
90 PrintF("[Deserializing context (%d bytes) took %0.3f ms]\n", bytes, ms); | 84 PrintF("[Deserializing context (%d bytes) took %0.3f ms]\n", bytes, ms); |
91 } | 85 } |
92 return Handle<Context>::cast(result); | 86 return Handle<Context>::cast(result); |
93 } | 87 } |
94 | 88 |
95 | 89 void CalculateFirstPageSizes(const SnapshotData& startup_snapshot, |
96 void CalculateFirstPageSizes(bool is_default_snapshot, | |
97 const SnapshotData& startup_snapshot, | |
98 const SnapshotData& context_snapshot, | 90 const SnapshotData& context_snapshot, |
99 uint32_t* sizes_out) { | 91 uint32_t* sizes_out) { |
100 Vector<const SerializedData::Reservation> startup_reservations = | 92 Vector<const SerializedData::Reservation> startup_reservations = |
101 startup_snapshot.Reservations(); | 93 startup_snapshot.Reservations(); |
102 Vector<const SerializedData::Reservation> context_reservations = | 94 Vector<const SerializedData::Reservation> context_reservations = |
103 context_snapshot.Reservations(); | 95 context_snapshot.Reservations(); |
104 int startup_index = 0; | 96 int startup_index = 0; |
105 int context_index = 0; | 97 int context_index = 0; |
106 | 98 |
107 if (FLAG_profile_deserialization) { | 99 if (FLAG_profile_deserialization) { |
(...skipping 27 matching lines...) Expand all Loading... |
135 if (single_chunk) { | 127 if (single_chunk) { |
136 // If both the startup snapshot data and the context snapshot data on | 128 // If both the startup snapshot data and the context snapshot data on |
137 // this space fit in a single page, then we consider limiting the size | 129 // this space fit in a single page, then we consider limiting the size |
138 // of the first page. For this, we add the chunk sizes and some extra | 130 // of the first page. For this, we add the chunk sizes and some extra |
139 // allowance. This way we achieve a smaller startup memory footprint. | 131 // allowance. This way we achieve a smaller startup memory footprint. |
140 required = (startup_reservations[startup_index].chunk_size() + | 132 required = (startup_reservations[startup_index].chunk_size() + |
141 2 * context_reservations[context_index].chunk_size()) + | 133 2 * context_reservations[context_index].chunk_size()) + |
142 Page::kObjectStartOffset; | 134 Page::kObjectStartOffset; |
143 // Add a small allowance to the code space for small scripts. | 135 // Add a small allowance to the code space for small scripts. |
144 if (space == CODE_SPACE) required += 32 * KB; | 136 if (space == CODE_SPACE) required += 32 * KB; |
145 } else if (!FLAG_debug_code) { | |
146 // We expect the vanilla snapshot to only require one page per space, | |
147 // unless we are emitting debug code. | |
148 DCHECK(!is_default_snapshot); | |
149 } | 137 } |
150 | 138 |
151 if (space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE) { | 139 if (space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE) { |
152 uint32_t max_size = | 140 uint32_t max_size = |
153 MemoryAllocator::PageAreaSize(static_cast<AllocationSpace>(space)); | 141 MemoryAllocator::PageAreaSize(static_cast<AllocationSpace>(space)); |
154 sizes_out[space - FIRST_PAGED_SPACE] = Min(required, max_size); | 142 sizes_out[space - FIRST_PAGED_SPACE] = Min(required, max_size); |
155 } else { | 143 } else { |
156 DCHECK(single_chunk); | 144 DCHECK(single_chunk); |
157 } | 145 } |
158 startup_index++; | 146 startup_index++; |
159 context_index++; | 147 context_index++; |
160 } | 148 } |
161 | 149 |
162 DCHECK_EQ(startup_reservations.length(), startup_index); | 150 DCHECK_EQ(startup_reservations.length(), startup_index); |
163 DCHECK_EQ(context_reservations.length(), context_index); | 151 DCHECK_EQ(context_reservations.length(), context_index); |
164 } | 152 } |
165 | 153 |
166 | |
167 v8::StartupData Snapshot::CreateSnapshotBlob( | 154 v8::StartupData Snapshot::CreateSnapshotBlob( |
168 const i::StartupSerializer& startup_ser, | 155 const i::StartupSerializer& startup_ser, |
169 const i::PartialSerializer& context_ser, Snapshot::Metadata metadata) { | 156 const i::PartialSerializer& context_ser) { |
170 SnapshotData startup_snapshot(startup_ser); | 157 SnapshotData startup_snapshot(startup_ser); |
171 SnapshotData context_snapshot(context_ser); | 158 SnapshotData context_snapshot(context_ser); |
172 Vector<const byte> startup_data = startup_snapshot.RawData(); | 159 Vector<const byte> startup_data = startup_snapshot.RawData(); |
173 Vector<const byte> context_data = context_snapshot.RawData(); | 160 Vector<const byte> context_data = context_snapshot.RawData(); |
174 | 161 |
175 uint32_t first_page_sizes[kNumPagedSpaces]; | 162 uint32_t first_page_sizes[kNumPagedSpaces]; |
176 | 163 |
177 CalculateFirstPageSizes(!metadata.embeds_script(), startup_snapshot, | 164 CalculateFirstPageSizes(startup_snapshot, context_snapshot, first_page_sizes); |
178 context_snapshot, first_page_sizes); | |
179 | 165 |
180 int startup_length = startup_data.length(); | 166 int startup_length = startup_data.length(); |
181 int context_length = context_data.length(); | 167 int context_length = context_data.length(); |
182 int context_offset = ContextOffset(startup_length); | 168 int context_offset = ContextOffset(startup_length); |
183 | 169 |
184 int length = context_offset + context_length; | 170 int length = context_offset + context_length; |
185 char* data = new char[length]; | 171 char* data = new char[length]; |
186 | 172 |
187 memcpy(data + kMetadataOffset, &metadata.RawValue(), kInt32Size); | |
188 memcpy(data + kFirstPageSizesOffset, first_page_sizes, | 173 memcpy(data + kFirstPageSizesOffset, first_page_sizes, |
189 kNumPagedSpaces * kInt32Size); | 174 kNumPagedSpaces * kInt32Size); |
190 memcpy(data + kStartupLengthOffset, &startup_length, kInt32Size); | 175 memcpy(data + kStartupLengthOffset, &startup_length, kInt32Size); |
191 memcpy(data + kStartupDataOffset, startup_data.begin(), startup_length); | 176 memcpy(data + kStartupDataOffset, startup_data.begin(), startup_length); |
192 memcpy(data + context_offset, context_data.begin(), context_length); | 177 memcpy(data + context_offset, context_data.begin(), context_length); |
193 v8::StartupData result = {data, length}; | 178 v8::StartupData result = {data, length}; |
194 | 179 |
195 if (FLAG_profile_deserialization) { | 180 if (FLAG_profile_deserialization) { |
196 PrintF( | 181 PrintF( |
197 "Snapshot blob consists of:\n" | 182 "Snapshot blob consists of:\n" |
198 "%10d bytes for startup\n" | 183 "%10d bytes for startup\n" |
199 "%10d bytes for context\n", | 184 "%10d bytes for context\n", |
200 startup_length, context_length); | 185 startup_length, context_length); |
201 } | 186 } |
202 return result; | 187 return result; |
203 } | 188 } |
204 | 189 |
205 | |
206 Snapshot::Metadata Snapshot::ExtractMetadata(const v8::StartupData* data) { | |
207 uint32_t raw; | |
208 memcpy(&raw, data->data + kMetadataOffset, kInt32Size); | |
209 return Metadata(raw); | |
210 } | |
211 | |
212 | |
213 Vector<const byte> Snapshot::ExtractStartupData(const v8::StartupData* data) { | 190 Vector<const byte> Snapshot::ExtractStartupData(const v8::StartupData* data) { |
214 DCHECK_LT(kIntSize, data->raw_size); | 191 DCHECK_LT(kIntSize, data->raw_size); |
215 int startup_length; | 192 int startup_length; |
216 memcpy(&startup_length, data->data + kStartupLengthOffset, kInt32Size); | 193 memcpy(&startup_length, data->data + kStartupLengthOffset, kInt32Size); |
217 DCHECK_LT(startup_length, data->raw_size); | 194 DCHECK_LT(startup_length, data->raw_size); |
218 const byte* startup_data = | 195 const byte* startup_data = |
219 reinterpret_cast<const byte*>(data->data + kStartupDataOffset); | 196 reinterpret_cast<const byte*>(data->data + kStartupDataOffset); |
220 return Vector<const byte>(startup_data, startup_length); | 197 return Vector<const byte>(startup_data, startup_length); |
221 } | 198 } |
222 | 199 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 Vector<const byte> SnapshotData::Payload() const { | 251 Vector<const byte> SnapshotData::Payload() const { |
275 int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size; | 252 int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size; |
276 const byte* payload = data_ + kHeaderSize + reservations_size; | 253 const byte* payload = data_ + kHeaderSize + reservations_size; |
277 int length = GetHeaderValue(kPayloadLengthOffset); | 254 int length = GetHeaderValue(kPayloadLengthOffset); |
278 DCHECK_EQ(data_ + size_, payload + length); | 255 DCHECK_EQ(data_ + size_, payload + length); |
279 return Vector<const byte>(payload, length); | 256 return Vector<const byte>(payload, length); |
280 } | 257 } |
281 | 258 |
282 } // namespace internal | 259 } // namespace internal |
283 } // namespace v8 | 260 } // namespace v8 |
OLD | NEW |