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

Side by Side Diff: src/snapshot-common.cc

Issue 932823002: Limit size of first page based on serialized data. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 10 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 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/v8.h" 7 #include "src/v8.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"
11 #include "src/serialize.h" 11 #include "src/full-codegen.h"
12 #include "src/snapshot.h" 12 #include "src/snapshot.h"
13 13
14 namespace v8 { 14 namespace v8 {
15 namespace internal { 15 namespace internal {
16 16
17 bool Snapshot::HaveASnapshotToStartFrom() { 17 bool Snapshot::HaveASnapshotToStartFrom() {
18 return SnapshotBlob().data != NULL; 18 return SnapshotBlob().data != NULL;
19 } 19 }
20 20
21 21
22 #ifdef DEBUG 22 #ifdef DEBUG
23 bool Snapshot::SnapshotIsValid(v8::StartupData* snapshot_blob) { 23 bool Snapshot::SnapshotIsValid(v8::StartupData* snapshot_blob) {
24 return !Snapshot::ExtractStartupData(snapshot_blob).is_empty() && 24 return !Snapshot::ExtractStartupData(snapshot_blob).is_empty() &&
25 !Snapshot::ExtractContextData(snapshot_blob).is_empty(); 25 !Snapshot::ExtractContextData(snapshot_blob).is_empty();
26 } 26 }
27 #endif // DEBUG 27 #endif // DEBUG
28 28
29 29
30 bool Snapshot::EmbedsScript() { 30 bool Snapshot::EmbedsScript() {
31 if (!HaveASnapshotToStartFrom()) return false; 31 if (!HaveASnapshotToStartFrom()) return false;
32 const v8::StartupData blob = SnapshotBlob(); 32 const v8::StartupData blob = SnapshotBlob();
33 return ExtractMetadata(&blob).embeds_script(); 33 return ExtractMetadata(&blob).embeds_script();
34 } 34 }
35 35
36 36
37 uint32_t Snapshot::SizeOfFirstPage(AllocationSpace space) {
38 DCHECK(space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE);
39 if (!HaveASnapshotToStartFrom()) {
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, SnapshotBlob().data + offset, kInt32Size);
45 return size;
46 }
47
48
37 bool Snapshot::Initialize(Isolate* isolate) { 49 bool Snapshot::Initialize(Isolate* isolate) {
38 if (!HaveASnapshotToStartFrom()) return false; 50 if (!HaveASnapshotToStartFrom()) return false;
39 base::ElapsedTimer timer; 51 base::ElapsedTimer timer;
40 if (FLAG_profile_deserialization) timer.Start(); 52 if (FLAG_profile_deserialization) timer.Start();
41 53
42 const v8::StartupData blob = SnapshotBlob(); 54 const v8::StartupData blob = SnapshotBlob();
43 Vector<const byte> startup_data = ExtractStartupData(&blob); 55 Vector<const byte> startup_data = ExtractStartupData(&blob);
44 SnapshotData snapshot_data(startup_data); 56 SnapshotData snapshot_data(startup_data);
45 Deserializer deserializer(&snapshot_data); 57 Deserializer deserializer(&snapshot_data);
46 bool success = isolate->Init(&deserializer); 58 bool success = isolate->Init(&deserializer);
(...skipping 28 matching lines...) Expand all
75 CHECK(EmbedsScript() || (*outdated_contexts_out)->length() == 1); 87 CHECK(EmbedsScript() || (*outdated_contexts_out)->length() == 1);
76 if (FLAG_profile_deserialization) { 88 if (FLAG_profile_deserialization) {
77 double ms = timer.Elapsed().InMillisecondsF(); 89 double ms = timer.Elapsed().InMillisecondsF();
78 int bytes = context_data.length(); 90 int bytes = context_data.length();
79 PrintF("[Deserializing context (%d bytes) took %0.3f ms]\n", bytes, ms); 91 PrintF("[Deserializing context (%d bytes) took %0.3f ms]\n", bytes, ms);
80 } 92 }
81 return Handle<Context>::cast(result); 93 return Handle<Context>::cast(result);
82 } 94 }
83 95
84 96
97 void CalculateFirstPageSizes(bool is_default_snapshot,
98 const SnapshotData& startup_snapshot,
99 const SnapshotData& context_snapshot,
100 uint32_t* sizes_out) {
101 Vector<const SerializedData::Reservation> startup_reservations =
102 startup_snapshot.Reservations();
103 Vector<const SerializedData::Reservation> context_reservations =
104 context_snapshot.Reservations();
105 int startup_index = 0;
106 int context_index = 0;
107 for (int space = 0; space < i::Serializer::kNumberOfSpaces; space++) {
108 bool single_chunk = true;
109 while (!startup_reservations[startup_index].is_last()) {
110 single_chunk = false;
111 startup_index++;
112 }
113 while (!context_reservations[context_index].is_last()) {
114 single_chunk = false;
115 context_index++;
116 }
117
118 uint32_t required = kMaxUInt32;
119 if (single_chunk) {
120 // If both the startup snapshot data and the context snapshot data on
121 // this space fit in a single page, then we consider limiting the size
122 // of the first page. For this, we add the chunk sizes and an additional
123 // 25% allowance. This way we achieve a smaller startup memory footprint.
124 required = (startup_reservations[startup_index].chunk_size() +
125 context_reservations[context_index].chunk_size()) /
126 4 * 5 +
127 Page::kObjectStartOffset;
128 } else {
129 // We expect the vanilla snapshot to only require on page per space.
130 DCHECK(!is_default_snapshot);
131 }
132
133 if (space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE) {
134 uint32_t max_size =
135 MemoryAllocator::PageAreaSize(static_cast<AllocationSpace>(space));
136 sizes_out[space - FIRST_PAGED_SPACE] = Min(required, max_size);
137 } else {
138 DCHECK(single_chunk);
139 }
140 startup_index++;
141 context_index++;
142 }
143
144 DCHECK_EQ(startup_reservations.length(), startup_index);
145 DCHECK_EQ(context_reservations.length(), context_index);
146 }
147
148
85 v8::StartupData Snapshot::CreateSnapshotBlob( 149 v8::StartupData Snapshot::CreateSnapshotBlob(
86 const Vector<const byte> startup_data, 150 const i::StartupSerializer& startup_ser,
87 const Vector<const byte> context_data, Snapshot::Metadata metadata) { 151 const i::PartialSerializer& context_ser, Snapshot::Metadata metadata) {
152 SnapshotData startup_snapshot(startup_ser);
153 SnapshotData context_snapshot(context_ser);
154 Vector<const byte> startup_data = startup_snapshot.RawData();
155 Vector<const byte> context_data = context_snapshot.RawData();
156
157 uint32_t first_page_sizes[kNumPagedSpaces];
158
159 CalculateFirstPageSizes(metadata.embeds_script(), startup_snapshot,
160 context_snapshot, first_page_sizes);
161
88 int startup_length = startup_data.length(); 162 int startup_length = startup_data.length();
89 int context_length = context_data.length(); 163 int context_length = context_data.length();
90 int context_offset = ContextOffset(startup_length); 164 int context_offset = ContextOffset(startup_length);
91 165
92 int length = context_offset + context_length; 166 int length = context_offset + context_length;
93 char* data = new char[length]; 167 char* data = new char[length];
94 168
95 memcpy(data + kMetadataOffset, &metadata.RawValue(), kInt32Size); 169 memcpy(data + kMetadataOffset, &metadata.RawValue(), kInt32Size);
170 memcpy(data + kFirstPageSizesOffset, first_page_sizes,
171 kNumPagedSpaces * kInt32Size);
96 memcpy(data + kStartupLengthOffset, &startup_length, kInt32Size); 172 memcpy(data + kStartupLengthOffset, &startup_length, kInt32Size);
97 memcpy(data + kStartupDataOffset, startup_data.begin(), startup_length); 173 memcpy(data + kStartupDataOffset, startup_data.begin(), startup_length);
98 memcpy(data + context_offset, context_data.begin(), context_length); 174 memcpy(data + context_offset, context_data.begin(), context_length);
99 v8::StartupData result = {data, length}; 175 v8::StartupData result = {data, length};
100 return result; 176 return result;
101 } 177 }
102 178
103 179
104 Snapshot::Metadata Snapshot::ExtractMetadata(const v8::StartupData* data) { 180 Snapshot::Metadata Snapshot::ExtractMetadata(const v8::StartupData* data) {
105 uint32_t raw; 181 uint32_t raw;
(...skipping 18 matching lines...) Expand all
124 int startup_length; 200 int startup_length;
125 memcpy(&startup_length, data->data + kStartupLengthOffset, kIntSize); 201 memcpy(&startup_length, data->data + kStartupLengthOffset, kIntSize);
126 int context_offset = ContextOffset(startup_length); 202 int context_offset = ContextOffset(startup_length);
127 const byte* context_data = 203 const byte* context_data =
128 reinterpret_cast<const byte*>(data->data + context_offset); 204 reinterpret_cast<const byte*>(data->data + context_offset);
129 DCHECK_LT(context_offset, data->raw_size); 205 DCHECK_LT(context_offset, data->raw_size);
130 int context_length = data->raw_size - context_offset; 206 int context_length = data->raw_size - context_offset;
131 return Vector<const byte>(context_data, context_length); 207 return Vector<const byte>(context_data, context_length);
132 } 208 }
133 } } // namespace v8::internal 209 } } // namespace v8::internal
OLDNEW
« src/serialize.h ('K') | « src/snapshot.h ('k') | test/cctest/test-heap.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698