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 #include <errno.h> | 5 #include <errno.h> |
6 #include <signal.h> | 6 #include <signal.h> |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 | 8 |
9 #include "src/v8.h" | 9 #include "src/v8.h" |
10 | 10 |
11 #include "include/libplatform/libplatform.h" | 11 #include "include/libplatform/libplatform.h" |
12 #include "src/assembler.h" | 12 #include "src/assembler.h" |
13 #include "src/base/platform/platform.h" | 13 #include "src/base/platform/platform.h" |
14 #include "src/bootstrapper.h" | 14 #include "src/bootstrapper.h" |
15 #include "src/flags.h" | 15 #include "src/flags.h" |
16 #include "src/list.h" | 16 #include "src/list.h" |
17 #include "src/natives.h" | 17 #include "src/natives.h" |
18 #include "src/serialize.h" | 18 #include "src/serialize.h" |
19 | 19 |
20 | 20 |
21 using namespace v8; | 21 using namespace v8; |
22 | 22 |
23 | |
24 class Compressor { | |
25 public: | |
26 virtual ~Compressor() {} | |
27 virtual bool Compress(i::Vector<i::byte> input) = 0; | |
28 virtual i::Vector<i::byte>* output() = 0; | |
29 }; | |
30 | |
31 | |
32 class SnapshotWriter { | 23 class SnapshotWriter { |
33 public: | 24 public: |
34 explicit SnapshotWriter(const char* snapshot_file) | 25 explicit SnapshotWriter(const char* snapshot_file) |
35 : fp_(GetFileDescriptorOrDie(snapshot_file)) | 26 : fp_(GetFileDescriptorOrDie(snapshot_file)), |
36 , raw_file_(NULL) | 27 raw_file_(NULL), |
37 , raw_context_file_(NULL) | 28 raw_context_file_(NULL), |
38 , startup_blob_file_(NULL) | 29 startup_blob_file_(NULL) {} |
39 , compressor_(NULL) { | |
40 } | |
41 | 30 |
42 ~SnapshotWriter() { | 31 ~SnapshotWriter() { |
43 fclose(fp_); | 32 fclose(fp_); |
44 if (raw_file_) fclose(raw_file_); | 33 if (raw_file_) fclose(raw_file_); |
45 if (raw_context_file_) fclose(raw_context_file_); | 34 if (raw_context_file_) fclose(raw_context_file_); |
46 if (startup_blob_file_) fclose(startup_blob_file_); | 35 if (startup_blob_file_) fclose(startup_blob_file_); |
47 } | 36 } |
48 | 37 |
49 void SetCompressor(Compressor* compressor) { | |
50 compressor_ = compressor; | |
51 } | |
52 | |
53 void SetRawFiles(const char* raw_file, const char* raw_context_file) { | 38 void SetRawFiles(const char* raw_file, const char* raw_context_file) { |
54 raw_file_ = GetFileDescriptorOrDie(raw_file); | 39 raw_file_ = GetFileDescriptorOrDie(raw_file); |
55 raw_context_file_ = GetFileDescriptorOrDie(raw_context_file); | 40 raw_context_file_ = GetFileDescriptorOrDie(raw_context_file); |
56 } | 41 } |
57 | 42 |
58 void SetStartupBlobFile(const char* startup_blob_file) { | 43 void SetStartupBlobFile(const char* startup_blob_file) { |
59 if (startup_blob_file != NULL) | 44 if (startup_blob_file != NULL) |
60 startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file); | 45 startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file); |
61 } | 46 } |
62 | 47 |
63 void WriteSnapshot(const i::List<i::byte>& snapshot_data, | 48 void WriteSnapshot(const i::SnapshotData& sd, |
64 const i::Serializer& serializer, | 49 const i::SnapshotData& csd) const { |
65 const i::List<i::byte>& context_snapshot_data, | 50 WriteSnapshotFile(sd, csd); |
66 const i::Serializer& context_serializer) const { | 51 MaybeWriteStartupBlob(sd, csd); |
67 WriteSnapshotFile(snapshot_data, serializer, | |
68 context_snapshot_data, context_serializer); | |
69 MaybeWriteStartupBlob(snapshot_data, serializer, | |
70 context_snapshot_data, context_serializer); | |
71 } | 52 } |
72 | 53 |
73 private: | 54 private: |
74 void MaybeWriteStartupBlob(const i::List<i::byte>& snapshot_data, | 55 void MaybeWriteStartupBlob(const i::SnapshotData& sd, |
75 const i::Serializer& serializer, | 56 const i::SnapshotData& csd) const { |
76 const i::List<i::byte>& context_snapshot_data, | |
77 const i::Serializer& context_serializer) const { | |
78 if (!startup_blob_file_) return; | 57 if (!startup_blob_file_) return; |
79 | 58 |
80 i::SnapshotByteSink sink; | 59 i::SnapshotByteSink sink; |
81 | 60 |
82 int spaces[] = {i::NEW_SPACE, i::OLD_POINTER_SPACE, | 61 sink.PutBlob(sd.RawData(), "snapshot"); |
83 i::OLD_DATA_SPACE, i::CODE_SPACE, | 62 sink.PutBlob(csd.RawData(), "context"); |
84 i::MAP_SPACE, i::CELL_SPACE, | |
85 i::PROPERTY_CELL_SPACE, i::LO_SPACE}; | |
86 | |
87 i::byte* snapshot_bytes = snapshot_data.begin(); | |
88 sink.PutBlob(snapshot_bytes, snapshot_data.length(), "snapshot"); | |
89 for (size_t i = 0; i < arraysize(spaces); ++i) { | |
90 i::Vector<const uint32_t> chunks = | |
91 serializer.FinalAllocationChunks(spaces[i]); | |
92 // For the start-up snapshot, none of the reservations has more than | |
93 // one chunk (reservation for each space fits onto a single page). | |
94 CHECK_EQ(1, chunks.length()); | |
95 sink.PutInt(chunks[0], "spaces"); | |
96 } | |
97 | |
98 i::byte* context_bytes = context_snapshot_data.begin(); | |
99 sink.PutBlob(context_bytes, context_snapshot_data.length(), "context"); | |
100 for (size_t i = 0; i < arraysize(spaces); ++i) { | |
101 i::Vector<const uint32_t> chunks = | |
102 context_serializer.FinalAllocationChunks(spaces[i]); | |
103 // For the context snapshot, none of the reservations has more than | |
104 // one chunk (reservation for each space fits onto a single page). | |
105 CHECK_EQ(1, chunks.length()); | |
106 sink.PutInt(chunks[0], "spaces"); | |
107 } | |
108 | 63 |
109 const i::List<i::byte>& startup_blob = sink.data(); | 64 const i::List<i::byte>& startup_blob = sink.data(); |
110 size_t written = fwrite(startup_blob.begin(), 1, startup_blob.length(), | 65 size_t written = fwrite(startup_blob.begin(), 1, startup_blob.length(), |
111 startup_blob_file_); | 66 startup_blob_file_); |
112 if (written != static_cast<size_t>(startup_blob.length())) { | 67 if (written != static_cast<size_t>(startup_blob.length())) { |
113 i::PrintF("Writing snapshot file failed.. Aborting.\n"); | 68 i::PrintF("Writing snapshot file failed.. Aborting.\n"); |
114 exit(1); | 69 exit(1); |
115 } | 70 } |
116 } | 71 } |
117 | 72 |
118 void WriteSnapshotFile(const i::List<i::byte>& snapshot_data, | 73 void WriteSnapshotFile(const i::SnapshotData& snapshot_data, |
119 const i::Serializer& serializer, | 74 const i::SnapshotData& context_snapshot_data) const { |
120 const i::List<i::byte>& context_snapshot_data, | |
121 const i::Serializer& context_serializer) const { | |
122 WriteFilePrefix(); | 75 WriteFilePrefix(); |
123 WriteData("", snapshot_data, raw_file_); | 76 WriteData("", snapshot_data.RawData(), raw_file_); |
124 WriteData("context_", context_snapshot_data, raw_context_file_); | 77 WriteData("context_", context_snapshot_data.RawData(), raw_context_file_); |
125 WriteMeta("context_", context_serializer); | |
126 WriteMeta("", serializer); | |
127 WriteFileSuffix(); | 78 WriteFileSuffix(); |
128 } | 79 } |
129 | 80 |
130 void WriteFilePrefix() const { | 81 void WriteFilePrefix() const { |
131 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); | 82 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); |
132 fprintf(fp_, "#include \"src/v8.h\"\n"); | 83 fprintf(fp_, "#include \"src/v8.h\"\n"); |
133 fprintf(fp_, "#include \"src/base/platform/platform.h\"\n\n"); | 84 fprintf(fp_, "#include \"src/base/platform/platform.h\"\n\n"); |
134 fprintf(fp_, "#include \"src/snapshot.h\"\n\n"); | 85 fprintf(fp_, "#include \"src/snapshot.h\"\n\n"); |
135 fprintf(fp_, "namespace v8 {\n"); | 86 fprintf(fp_, "namespace v8 {\n"); |
136 fprintf(fp_, "namespace internal {\n\n"); | 87 fprintf(fp_, "namespace internal {\n\n"); |
137 } | 88 } |
138 | 89 |
139 void WriteFileSuffix() const { | 90 void WriteFileSuffix() const { |
140 fprintf(fp_, "} // namespace internal\n"); | 91 fprintf(fp_, "} // namespace internal\n"); |
141 fprintf(fp_, "} // namespace v8\n"); | 92 fprintf(fp_, "} // namespace v8\n"); |
142 } | 93 } |
143 | 94 |
144 void WriteData(const char* prefix, const i::List<i::byte>& source_data, | 95 void WriteData(const char* prefix, |
| 96 const i::Vector<const i::byte>& source_data, |
145 FILE* raw_file) const { | 97 FILE* raw_file) const { |
146 const i::List<i::byte>* data_to_be_written = NULL; | 98 MaybeWriteRawFile(&source_data, raw_file); |
147 i::List<i::byte> compressed_data; | 99 WriteData(prefix, source_data); |
148 if (!compressor_) { | |
149 data_to_be_written = &source_data; | |
150 } else if (compressor_->Compress(source_data.ToVector())) { | |
151 compressed_data.AddAll(*compressor_->output()); | |
152 data_to_be_written = &compressed_data; | |
153 } else { | |
154 i::PrintF("Compression failed. Aborting.\n"); | |
155 exit(1); | |
156 } | |
157 | |
158 DCHECK(data_to_be_written); | |
159 MaybeWriteRawFile(data_to_be_written, raw_file); | |
160 WriteData(prefix, source_data, data_to_be_written); | |
161 } | 100 } |
162 | 101 |
163 void MaybeWriteRawFile(const i::List<i::byte>* data, FILE* raw_file) const { | 102 void MaybeWriteRawFile(const i::Vector<const i::byte>* data, |
164 if (!data || !raw_file) | 103 FILE* raw_file) const { |
165 return; | 104 if (!data || !raw_file) return; |
166 | 105 |
167 // Sanity check, whether i::List iterators truly return pointers to an | 106 // Sanity check, whether i::List iterators truly return pointers to an |
168 // internal array. | 107 // internal array. |
169 DCHECK(data->end() - data->begin() == data->length()); | 108 DCHECK(data->end() - data->begin() == data->length()); |
170 | 109 |
171 size_t written = fwrite(data->begin(), 1, data->length(), raw_file); | 110 size_t written = fwrite(data->begin(), 1, data->length(), raw_file); |
172 if (written != (size_t)data->length()) { | 111 if (written != static_cast<size_t>(data->length())) { |
173 i::PrintF("Writing raw file failed.. Aborting.\n"); | 112 i::PrintF("Writing raw file failed.. Aborting.\n"); |
174 exit(1); | 113 exit(1); |
175 } | 114 } |
176 } | 115 } |
177 | 116 |
178 void WriteData(const char* prefix, const i::List<i::byte>& source_data, | 117 void WriteData(const char* prefix, |
179 const i::List<i::byte>* data_to_be_written) const { | 118 const i::Vector<const i::byte>& source_data) const { |
180 fprintf(fp_, "const byte Snapshot::%sdata_[] = {\n", prefix); | 119 fprintf(fp_, "const byte Snapshot::%sdata_[] = {\n", prefix); |
181 WriteSnapshotData(data_to_be_written); | 120 WriteSnapshotData(source_data); |
182 fprintf(fp_, "};\n"); | 121 fprintf(fp_, "};\n"); |
183 fprintf(fp_, "const int Snapshot::%ssize_ = %d;\n", prefix, | 122 fprintf(fp_, "const int Snapshot::%ssize_ = %d;\n", prefix, |
184 data_to_be_written->length()); | 123 source_data.length()); |
| 124 fprintf(fp_, "\n"); |
| 125 } |
185 | 126 |
186 if (data_to_be_written == &source_data) { | 127 void WriteSnapshotData(const i::Vector<const i::byte>& data) const { |
187 fprintf(fp_, "const byte* Snapshot::%sraw_data_ = Snapshot::%sdata_;\n", | 128 for (int i = 0; i < data.length(); i++) { |
188 prefix, prefix); | 129 if ((i & 0x1f) == 0x1f) fprintf(fp_, "\n"); |
189 fprintf(fp_, "const int Snapshot::%sraw_size_ = Snapshot::%ssize_;\n", | 130 if (i > 0) fprintf(fp_, ","); |
190 prefix, prefix); | 131 fprintf(fp_, "%u", static_cast<unsigned char>(data.at(i))); |
191 } else { | |
192 fprintf(fp_, "const byte* Snapshot::%sraw_data_ = NULL;\n", prefix); | |
193 fprintf(fp_, "const int Snapshot::%sraw_size_ = %d;\n", | |
194 prefix, source_data.length()); | |
195 } | 132 } |
196 fprintf(fp_, "\n"); | 133 fprintf(fp_, "\n"); |
197 } | 134 } |
198 | |
199 void WriteMeta(const char* prefix, const i::Serializer& ser) const { | |
200 WriteSizeVar(ser, prefix, "new", i::NEW_SPACE); | |
201 WriteSizeVar(ser, prefix, "pointer", i::OLD_POINTER_SPACE); | |
202 WriteSizeVar(ser, prefix, "data", i::OLD_DATA_SPACE); | |
203 WriteSizeVar(ser, prefix, "code", i::CODE_SPACE); | |
204 WriteSizeVar(ser, prefix, "map", i::MAP_SPACE); | |
205 WriteSizeVar(ser, prefix, "cell", i::CELL_SPACE); | |
206 WriteSizeVar(ser, prefix, "property_cell", i::PROPERTY_CELL_SPACE); | |
207 WriteSizeVar(ser, prefix, "lo", i::LO_SPACE); | |
208 fprintf(fp_, "\n"); | |
209 } | |
210 | |
211 void WriteSizeVar(const i::Serializer& ser, const char* prefix, | |
212 const char* name, int space) const { | |
213 i::Vector<const uint32_t> chunks = ser.FinalAllocationChunks(space); | |
214 // For the start-up snapshot, none of the reservations has more than | |
215 // one chunk (total reservation fits into a single page). | |
216 CHECK_EQ(1, chunks.length()); | |
217 fprintf(fp_, "const int Snapshot::%s%s_space_used_ = %d;\n", prefix, name, | |
218 chunks[0]); | |
219 } | |
220 | |
221 void WriteSnapshotData(const i::List<i::byte>* data) const { | |
222 for (int i = 0; i < data->length(); i++) { | |
223 if ((i & 0x1f) == 0x1f) | |
224 fprintf(fp_, "\n"); | |
225 if (i > 0) | |
226 fprintf(fp_, ","); | |
227 fprintf(fp_, "%u", static_cast<unsigned char>(data->at(i))); | |
228 } | |
229 fprintf(fp_, "\n"); | |
230 } | |
231 | 135 |
232 FILE* GetFileDescriptorOrDie(const char* filename) { | 136 FILE* GetFileDescriptorOrDie(const char* filename) { |
233 FILE* fp = base::OS::FOpen(filename, "wb"); | 137 FILE* fp = base::OS::FOpen(filename, "wb"); |
234 if (fp == NULL) { | 138 if (fp == NULL) { |
235 i::PrintF("Unable to open file \"%s\" for writing.\n", filename); | 139 i::PrintF("Unable to open file \"%s\" for writing.\n", filename); |
236 exit(1); | 140 exit(1); |
237 } | 141 } |
238 return fp; | 142 return fp; |
239 } | 143 } |
240 | 144 |
241 FILE* fp_; | 145 FILE* fp_; |
242 FILE* raw_file_; | 146 FILE* raw_file_; |
243 FILE* raw_context_file_; | 147 FILE* raw_context_file_; |
244 FILE* startup_blob_file_; | 148 FILE* startup_blob_file_; |
245 Compressor* compressor_; | |
246 }; | 149 }; |
247 | 150 |
248 | 151 |
249 void DumpException(Handle<Message> message) { | 152 void DumpException(Handle<Message> message) { |
250 String::Utf8Value message_string(message->Get()); | 153 String::Utf8Value message_string(message->Get()); |
251 String::Utf8Value message_line(message->GetSourceLine()); | 154 String::Utf8Value message_line(message->GetSourceLine()); |
252 fprintf(stderr, "%s at line %d\n", *message_string, message->GetLineNumber()); | 155 fprintf(stderr, "%s at line %d\n", *message_string, message->GetLineNumber()); |
253 fprintf(stderr, "%s\n", *message_line); | 156 fprintf(stderr, "%s\n", *message_line); |
254 for (int i = 0; i <= message->GetEndColumn(); ++i) { | 157 for (int i = 0; i <= message->GetEndColumn(); ++i) { |
255 fprintf(stderr, "%c", i < message->GetStartColumn() ? ' ' : '^'); | 158 fprintf(stderr, "%c", i < message->GetStartColumn() ? ' ' : '^'); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 // rid of some things that are cached between garbage collections. | 261 // rid of some things that are cached between garbage collections. |
359 i::SnapshotByteSink snapshot_sink; | 262 i::SnapshotByteSink snapshot_sink; |
360 i::StartupSerializer ser(internal_isolate, &snapshot_sink); | 263 i::StartupSerializer ser(internal_isolate, &snapshot_sink); |
361 ser.SerializeStrongReferences(); | 264 ser.SerializeStrongReferences(); |
362 | 265 |
363 i::SnapshotByteSink context_sink; | 266 i::SnapshotByteSink context_sink; |
364 i::PartialSerializer context_ser(internal_isolate, &ser, &context_sink); | 267 i::PartialSerializer context_ser(internal_isolate, &ser, &context_sink); |
365 context_ser.Serialize(&raw_context); | 268 context_ser.Serialize(&raw_context); |
366 ser.SerializeWeakReferences(); | 269 ser.SerializeWeakReferences(); |
367 | 270 |
368 context_ser.FinalizeAllocation(); | |
369 ser.FinalizeAllocation(); | |
370 | |
371 { | 271 { |
372 SnapshotWriter writer(argv[1]); | 272 SnapshotWriter writer(argv[1]); |
373 if (i::FLAG_raw_file && i::FLAG_raw_context_file) | 273 if (i::FLAG_raw_file && i::FLAG_raw_context_file) |
374 writer.SetRawFiles(i::FLAG_raw_file, i::FLAG_raw_context_file); | 274 writer.SetRawFiles(i::FLAG_raw_file, i::FLAG_raw_context_file); |
375 if (i::FLAG_startup_blob) | 275 if (i::FLAG_startup_blob) |
376 writer.SetStartupBlobFile(i::FLAG_startup_blob); | 276 writer.SetStartupBlobFile(i::FLAG_startup_blob); |
377 writer.WriteSnapshot(snapshot_sink.data(), ser, context_sink.data(), | 277 i::SnapshotData sd(snapshot_sink, ser); |
378 context_ser); | 278 i::SnapshotData csd(context_sink, context_ser); |
| 279 writer.WriteSnapshot(sd, csd); |
379 } | 280 } |
380 } | 281 } |
381 | 282 |
382 isolate->Dispose(); | 283 isolate->Dispose(); |
383 V8::Dispose(); | 284 V8::Dispose(); |
384 V8::ShutdownPlatform(); | 285 V8::ShutdownPlatform(); |
385 delete platform; | 286 delete platform; |
386 return 0; | 287 return 0; |
387 } | 288 } |
OLD | NEW |