| OLD | NEW |
| (Empty) |
| 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/v8.h" | |
| 8 | |
| 9 #include "src/api.h" | |
| 10 #include "src/base/platform/platform.h" | |
| 11 #include "src/full-codegen.h" | |
| 12 #include "src/snapshot.h" | |
| 13 | |
| 14 namespace v8 { | |
| 15 namespace internal { | |
| 16 | |
| 17 #ifdef DEBUG | |
| 18 bool Snapshot::SnapshotIsValid(v8::StartupData* snapshot_blob) { | |
| 19 return !Snapshot::ExtractStartupData(snapshot_blob).is_empty() && | |
| 20 !Snapshot::ExtractContextData(snapshot_blob).is_empty(); | |
| 21 } | |
| 22 #endif // DEBUG | |
| 23 | |
| 24 | |
| 25 bool Snapshot::EmbedsScript(Isolate* isolate) { | |
| 26 if (!isolate->snapshot_available()) return false; | |
| 27 return ExtractMetadata(isolate->snapshot_blob()).embeds_script(); | |
| 28 } | |
| 29 | |
| 30 | |
| 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)); | |
| 35 } | |
| 36 uint32_t size; | |
| 37 int offset = kFirstPageSizesOffset + (space - FIRST_PAGED_SPACE) * kInt32Size; | |
| 38 memcpy(&size, isolate->snapshot_blob()->data + offset, kInt32Size); | |
| 39 return size; | |
| 40 } | |
| 41 | |
| 42 | |
| 43 bool Snapshot::Initialize(Isolate* isolate) { | |
| 44 if (!isolate->snapshot_available()) return false; | |
| 45 base::ElapsedTimer timer; | |
| 46 if (FLAG_profile_deserialization) timer.Start(); | |
| 47 | |
| 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); | |
| 57 } | |
| 58 return success; | |
| 59 } | |
| 60 | |
| 61 | |
| 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(); | |
| 68 | |
| 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); | |
| 73 | |
| 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); | |
| 86 } | |
| 87 return Handle<Context>::cast(result); | |
| 88 } | |
| 89 | |
| 90 | |
| 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; | |
| 101 | |
| 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(); | |
| 107 } | |
| 108 for (auto& reservation : context_reservations) { | |
| 109 context_total += reservation.chunk_size(); | |
| 110 } | |
| 111 PrintF( | |
| 112 "Deserialization will reserve:\n" | |
| 113 "%10d bytes for startup\n" | |
| 114 "%10d bytes per context\n", | |
| 115 startup_total, context_total); | |
| 116 } | |
| 117 | |
| 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; | |
| 122 startup_index++; | |
| 123 } | |
| 124 while (!context_reservations[context_index].is_last()) { | |
| 125 single_chunk = false; | |
| 126 context_index++; | |
| 127 } | |
| 128 | |
| 129 uint32_t required = kMaxUInt32; | |
| 130 if (single_chunk) { | |
| 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; | |
| 140 } else { | |
| 141 // We expect the vanilla snapshot to only require on page per space. | |
| 142 DCHECK(!is_default_snapshot); | |
| 143 } | |
| 144 | |
| 145 if (space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE) { | |
| 146 uint32_t max_size = | |
| 147 MemoryAllocator::PageAreaSize(static_cast<AllocationSpace>(space)); | |
| 148 sizes_out[space - FIRST_PAGED_SPACE] = Min(required, max_size); | |
| 149 } else { | |
| 150 DCHECK(single_chunk); | |
| 151 } | |
| 152 startup_index++; | |
| 153 context_index++; | |
| 154 } | |
| 155 | |
| 156 DCHECK_EQ(startup_reservations.length(), startup_index); | |
| 157 DCHECK_EQ(context_reservations.length(), context_index); | |
| 158 } | |
| 159 | |
| 160 | |
| 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(); | |
| 168 | |
| 169 uint32_t first_page_sizes[kNumPagedSpaces]; | |
| 170 | |
| 171 CalculateFirstPageSizes(!metadata.embeds_script(), startup_snapshot, | |
| 172 context_snapshot, first_page_sizes); | |
| 173 | |
| 174 int startup_length = startup_data.length(); | |
| 175 int context_length = context_data.length(); | |
| 176 int context_offset = ContextOffset(startup_length); | |
| 177 | |
| 178 int length = context_offset + context_length; | |
| 179 char* data = new char[length]; | |
| 180 | |
| 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}; | |
| 188 | |
| 189 if (FLAG_profile_deserialization) { | |
| 190 PrintF( | |
| 191 "Snapshot blob consists of:\n" | |
| 192 "%10d bytes for startup\n" | |
| 193 "%10d bytes for context\n", | |
| 194 startup_length, context_length); | |
| 195 } | |
| 196 return result; | |
| 197 } | |
| 198 | |
| 199 | |
| 200 Snapshot::Metadata Snapshot::ExtractMetadata(const v8::StartupData* data) { | |
| 201 uint32_t raw; | |
| 202 memcpy(&raw, data->data + kMetadataOffset, kInt32Size); | |
| 203 return Metadata(raw); | |
| 204 } | |
| 205 | |
| 206 | |
| 207 Vector<const byte> Snapshot::ExtractStartupData(const v8::StartupData* data) { | |
| 208 DCHECK_LT(kIntSize, data->raw_size); | |
| 209 int startup_length; | |
| 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); | |
| 215 } | |
| 216 | |
| 217 | |
| 218 Vector<const byte> Snapshot::ExtractContextData(const v8::StartupData* data) { | |
| 219 DCHECK_LT(kIntSize, data->raw_size); | |
| 220 int startup_length; | |
| 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); | |
| 228 } | |
| 229 } } // namespace v8::internal | |
| OLD | NEW |