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