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 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 | 45 |
46 | 46 |
47 class Compressor { | 47 class Compressor { |
48 public: | 48 public: |
49 virtual ~Compressor() {} | 49 virtual ~Compressor() {} |
50 virtual bool Compress(i::Vector<char> input) = 0; | 50 virtual bool Compress(i::Vector<char> input) = 0; |
51 virtual i::Vector<char>* output() = 0; | 51 virtual i::Vector<char>* output() = 0; |
52 }; | 52 }; |
53 | 53 |
54 | 54 |
55 class PartialSnapshotSink : public i::SnapshotByteSink { | 55 class ListSnapshotSink : public i::SnapshotByteSink { |
56 public: | 56 public: |
57 PartialSnapshotSink() : data_(), raw_size_(-1) { } | 57 explicit ListSnapshotSink(i::List<char>* data) : data_(data) { } |
58 virtual ~PartialSnapshotSink() { data_.Free(); } | 58 virtual ~ListSnapshotSink() {} |
59 virtual void Put(int byte, const char* description) { | 59 virtual void Put(int byte, const char* description) { data_->Add(byte); } |
60 data_.Add(byte); | 60 virtual int Position() { return data_->length(); } |
61 } | |
62 virtual int Position() { return data_.length(); } | |
63 void Print(FILE* fp) { | |
64 int length = Position(); | |
65 for (int j = 0; j < length; j++) { | |
66 if ((j & 0x1f) == 0x1f) { | |
67 fprintf(fp, "\n"); | |
68 } | |
69 if (j != 0) { | |
70 fprintf(fp, ","); | |
71 } | |
72 fprintf(fp, "%u", static_cast<unsigned char>(at(j))); | |
73 } | |
74 } | |
75 char at(int i) { return data_[i]; } | |
76 bool Compress(Compressor* compressor) { | |
77 ASSERT_EQ(-1, raw_size_); | |
78 raw_size_ = data_.length(); | |
79 if (!compressor->Compress(data_.ToVector())) return false; | |
80 data_.Clear(); | |
81 data_.AddAll(*compressor->output()); | |
82 return true; | |
83 } | |
84 int raw_size() { return raw_size_; } | |
85 | |
86 private: | 61 private: |
87 i::List<char> data_; | 62 i::List<char>* data_; |
88 int raw_size_; | |
89 }; | 63 }; |
90 | 64 |
91 | 65 |
92 class CppByteSink : public PartialSnapshotSink { | 66 class SnapshotWriter { |
93 public: | 67 public: |
94 explicit CppByteSink(const char* snapshot_file) { | 68 explicit SnapshotWriter(const char* snapshot_file) |
95 fp_ = i::OS::FOpen(snapshot_file, "wb"); | 69 : fp_(GetFileDescriptorOrDie(snapshot_file)) |
96 if (fp_ == NULL) { | 70 , raw_file_(NULL) |
97 i::PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file); | 71 , raw_context_file_(NULL) |
98 exit(1); | 72 , compressor_(NULL) |
99 } | 73 , omit_(false) { |
| 74 } |
| 75 |
| 76 ~SnapshotWriter() { |
| 77 fclose(fp_); |
| 78 if (raw_file_) fclose(raw_file_); |
| 79 if (raw_context_file_) fclose(raw_context_file_); |
| 80 } |
| 81 |
| 82 void SetCompressor(Compressor* compressor) { |
| 83 compressor_ = compressor; |
| 84 } |
| 85 |
| 86 void SetOmit(bool omit) { |
| 87 omit_ = omit; |
| 88 } |
| 89 |
| 90 void SetRawFiles(const char* raw_file, const char* raw_context_file) { |
| 91 raw_file_ = GetFileDescriptorOrDie(raw_file); |
| 92 raw_context_file_ = GetFileDescriptorOrDie(raw_context_file); |
| 93 } |
| 94 |
| 95 void WriteSnapshot(const i::List<char>& snapshot_data, |
| 96 const i::Serializer& serializer, |
| 97 const i::List<char>& context_snapshot_data, |
| 98 const i::Serializer& context_serializer) const { |
| 99 WriteFilePrefix(); |
| 100 WriteData("", snapshot_data, raw_file_); |
| 101 WriteData("context_", context_snapshot_data, raw_context_file_); |
| 102 WriteMeta("context_", context_serializer); |
| 103 WriteMeta("", serializer); |
| 104 WriteFileSuffix(); |
| 105 } |
| 106 |
| 107 private: |
| 108 void WriteFilePrefix() const { |
100 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); | 109 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); |
101 fprintf(fp_, "#include \"v8.h\"\n"); | 110 fprintf(fp_, "#include \"v8.h\"\n"); |
102 fprintf(fp_, "#include \"platform.h\"\n\n"); | 111 fprintf(fp_, "#include \"platform.h\"\n\n"); |
103 fprintf(fp_, "#include \"snapshot.h\"\n\n"); | 112 fprintf(fp_, "#include \"snapshot.h\"\n\n"); |
104 fprintf(fp_, "namespace v8 {\nnamespace internal {\n\n"); | 113 fprintf(fp_, "namespace v8 {\n"); |
105 fprintf(fp_, "const byte Snapshot::data_[] = {"); | 114 fprintf(fp_, "namespace internal {\n\n"); |
106 } | 115 } |
107 | 116 |
108 virtual ~CppByteSink() { | 117 void WriteFileSuffix() const { |
109 fprintf(fp_, "const int Snapshot::size_ = %d;\n", Position()); | 118 fprintf(fp_, "} // namespace internal\n"); |
110 #ifdef COMPRESS_STARTUP_DATA_BZ2 | 119 fprintf(fp_, "} // namespace v8\n"); |
111 fprintf(fp_, "const byte* Snapshot::raw_data_ = NULL;\n"); | |
112 fprintf(fp_, | |
113 "const int Snapshot::raw_size_ = %d;\n\n", | |
114 raw_size()); | |
115 #else | |
116 fprintf(fp_, | |
117 "const byte* Snapshot::raw_data_ = Snapshot::data_;\n"); | |
118 fprintf(fp_, | |
119 "const int Snapshot::raw_size_ = Snapshot::size_;\n\n"); | |
120 #endif | |
121 fprintf(fp_, "} } // namespace v8::internal\n"); | |
122 fclose(fp_); | |
123 } | 120 } |
124 | 121 |
125 void WriteSpaceUsed( | 122 void WriteData(const char* prefix, |
126 const char* prefix, | 123 const i::List<char>& source_data, |
127 int new_space_used, | 124 FILE* raw_file) const { |
128 int pointer_space_used, | 125 const i::List <char>* data_to_be_written = NULL; |
129 int data_space_used, | 126 i::List<char> compressed_data; |
130 int code_space_used, | 127 if (!compressor_) { |
131 int map_space_used, | 128 data_to_be_written = &source_data; |
132 int cell_space_used, | 129 } else if (compressor_->Compress(source_data.ToVector())) { |
133 int property_cell_space_used) { | 130 compressed_data.AddAll(*compressor_->output()); |
134 fprintf(fp_, | 131 data_to_be_written = &compressed_data; |
135 "const int Snapshot::%snew_space_used_ = %d;\n", | 132 } else { |
136 prefix, | 133 i::PrintF("Compression failed. Aborting.\n"); |
137 new_space_used); | 134 exit(1); |
138 fprintf(fp_, | 135 } |
139 "const int Snapshot::%spointer_space_used_ = %d;\n", | 136 |
140 prefix, | 137 ASSERT(data_to_be_written); |
141 pointer_space_used); | 138 MaybeWriteRawFile(data_to_be_written, raw_file); |
142 fprintf(fp_, | 139 WriteData(prefix, source_data, data_to_be_written); |
143 "const int Snapshot::%sdata_space_used_ = %d;\n", | |
144 prefix, | |
145 data_space_used); | |
146 fprintf(fp_, | |
147 "const int Snapshot::%scode_space_used_ = %d;\n", | |
148 prefix, | |
149 code_space_used); | |
150 fprintf(fp_, | |
151 "const int Snapshot::%smap_space_used_ = %d;\n", | |
152 prefix, | |
153 map_space_used); | |
154 fprintf(fp_, | |
155 "const int Snapshot::%scell_space_used_ = %d;\n", | |
156 prefix, | |
157 cell_space_used); | |
158 fprintf(fp_, | |
159 "const int Snapshot::%sproperty_cell_space_used_ = %d;\n", | |
160 prefix, | |
161 property_cell_space_used); | |
162 } | 140 } |
163 | 141 |
164 void WritePartialSnapshot() { | 142 void MaybeWriteRawFile(const i::List<char>* data, FILE* raw_file) const { |
165 int length = partial_sink_.Position(); | 143 if (!data || !raw_file) |
166 fprintf(fp_, "};\n\n"); | 144 return; |
167 fprintf(fp_, "const int Snapshot::context_size_ = %d;\n", length); | 145 |
168 #ifdef COMPRESS_STARTUP_DATA_BZ2 | 146 // Sanity check, whether i::List iterators truly return pointers to an |
169 fprintf(fp_, | 147 // internal array. |
170 "const int Snapshot::context_raw_size_ = %d;\n", | 148 ASSERT(data->end() - data->begin() == data->length()); |
171 partial_sink_.raw_size()); | 149 |
172 #else | 150 size_t written = fwrite(data->begin(), 1, data->length(), raw_file); |
173 fprintf(fp_, | 151 if (written != (size_t)data->length()) { |
174 "const int Snapshot::context_raw_size_ = " | 152 i::PrintF("Writing raw file failed.. Aborting.\n"); |
175 "Snapshot::context_size_;\n"); | 153 exit(1); |
176 #endif | 154 } |
177 fprintf(fp_, "const byte Snapshot::context_data_[] = {\n"); | |
178 partial_sink_.Print(fp_); | |
179 fprintf(fp_, "};\n\n"); | |
180 #ifdef COMPRESS_STARTUP_DATA_BZ2 | |
181 fprintf(fp_, "const byte* Snapshot::context_raw_data_ = NULL;\n"); | |
182 #else | |
183 fprintf(fp_, "const byte* Snapshot::context_raw_data_ =" | |
184 " Snapshot::context_data_;\n"); | |
185 #endif | |
186 } | 155 } |
187 | 156 |
188 void WriteSnapshot() { | 157 void WriteData(const char* prefix, |
189 Print(fp_); | 158 const i::List<char>& source_data, |
| 159 const i::List<char>* data_to_be_written) const { |
| 160 fprintf(fp_, "const byte Snapshot::%sdata_[] = {\n", prefix); |
| 161 if (!omit_) |
| 162 WriteSnapshotData(data_to_be_written); |
| 163 fprintf(fp_, "};\n"); |
| 164 fprintf(fp_, "const int Snapshot::%ssize_ = %d;\n", prefix, |
| 165 data_to_be_written->length()); |
| 166 |
| 167 if (data_to_be_written == &source_data && !omit_) { |
| 168 fprintf(fp_, "const byte* Snapshot::%sraw_data_ = Snapshot::%sdata_;\n", |
| 169 prefix, prefix); |
| 170 fprintf(fp_, "const int Snapshot::%sraw_size_ = Snapshot::%ssize_;\n", |
| 171 prefix, prefix); |
| 172 } else { |
| 173 fprintf(fp_, "const byte* Snapshot::%sraw_data_ = NULL;\n", prefix); |
| 174 fprintf(fp_, "const int Snapshot::%sraw_size_ = %d;\n", |
| 175 prefix, source_data.length()); |
| 176 } |
| 177 fprintf(fp_, "\n"); |
190 } | 178 } |
191 | 179 |
192 PartialSnapshotSink* partial_sink() { return &partial_sink_; } | 180 void WriteMeta(const char* prefix, const i::Serializer& ser) const { |
| 181 WriteSizeVar(ser, prefix, "new", i::NEW_SPACE); |
| 182 WriteSizeVar(ser, prefix, "pointer", i::OLD_POINTER_SPACE); |
| 183 WriteSizeVar(ser, prefix, "data", i::OLD_DATA_SPACE); |
| 184 WriteSizeVar(ser, prefix, "code", i::CODE_SPACE); |
| 185 WriteSizeVar(ser, prefix, "map", i::MAP_SPACE); |
| 186 WriteSizeVar(ser, prefix, "cell", i::CELL_SPACE); |
| 187 WriteSizeVar(ser, prefix, "property_cell", i::PROPERTY_CELL_SPACE); |
| 188 fprintf(fp_, "\n"); |
| 189 } |
193 | 190 |
194 private: | 191 void WriteSizeVar(const i::Serializer& ser, const char* prefix, |
| 192 const char* name, int space) const { |
| 193 fprintf(fp_, "const int Snapshot::%s%s_space_used_ = %d;\n", |
| 194 prefix, name, ser.CurrentAllocationAddress(space)); |
| 195 } |
| 196 |
| 197 void WriteSnapshotData(const i::List<char>* data) const { |
| 198 for (int i = 0; i < data->length(); i++) { |
| 199 if ((i & 0x1f) == 0x1f) |
| 200 fprintf(fp_, "\n"); |
| 201 if (i > 0) |
| 202 fprintf(fp_, ","); |
| 203 fprintf(fp_, "%u", static_cast<unsigned char>(data->at(i))); |
| 204 } |
| 205 fprintf(fp_, "\n"); |
| 206 } |
| 207 |
| 208 FILE* GetFileDescriptorOrDie(const char* filename) { |
| 209 FILE* fp = i::OS::FOpen(filename, "wb"); |
| 210 if (fp == NULL) { |
| 211 i::PrintF("Unable to open file \"%s\" for writing.\n", filename); |
| 212 exit(1); |
| 213 } |
| 214 return fp; |
| 215 } |
| 216 |
195 FILE* fp_; | 217 FILE* fp_; |
196 PartialSnapshotSink partial_sink_; | 218 FILE* raw_file_; |
| 219 FILE* raw_context_file_; |
| 220 Compressor* compressor_; |
| 221 bool omit_; |
197 }; | 222 }; |
198 | 223 |
199 | 224 |
200 #ifdef COMPRESS_STARTUP_DATA_BZ2 | 225 #ifdef COMPRESS_STARTUP_DATA_BZ2 |
201 class BZip2Compressor : public Compressor { | 226 class BZip2Compressor : public Compressor { |
202 public: | 227 public: |
203 BZip2Compressor() : output_(NULL) {} | 228 BZip2Compressor() : output_(NULL) {} |
204 virtual ~BZip2Compressor() { | 229 virtual ~BZip2Compressor() { |
205 delete output_; | 230 delete output_; |
206 } | 231 } |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) { | 377 for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) { |
353 internal_isolate->bootstrapper()->NativesSourceLookup(i); | 378 internal_isolate->bootstrapper()->NativesSourceLookup(i); |
354 } | 379 } |
355 } | 380 } |
356 // If we don't do this then we end up with a stray root pointing at the | 381 // If we don't do this then we end up with a stray root pointing at the |
357 // context even after we have disposed of the context. | 382 // context even after we have disposed of the context. |
358 internal_isolate->heap()->CollectAllGarbage( | 383 internal_isolate->heap()->CollectAllGarbage( |
359 i::Heap::kNoGCFlags, "mksnapshot"); | 384 i::Heap::kNoGCFlags, "mksnapshot"); |
360 i::Object* raw_context = *v8::Utils::OpenPersistent(context); | 385 i::Object* raw_context = *v8::Utils::OpenPersistent(context); |
361 context.Reset(); | 386 context.Reset(); |
362 CppByteSink sink(argv[1]); | 387 |
363 // This results in a somewhat smaller snapshot, probably because it gets rid | 388 // This results in a somewhat smaller snapshot, probably because it gets rid |
364 // of some things that are cached between garbage collections. | 389 // of some things that are cached between garbage collections. |
365 i::StartupSerializer ser(internal_isolate, &sink); | 390 i::List<char> snapshot_data; |
| 391 ListSnapshotSink snapshot_sink(&snapshot_data); |
| 392 i::StartupSerializer ser(internal_isolate, &snapshot_sink); |
366 ser.SerializeStrongReferences(); | 393 ser.SerializeStrongReferences(); |
367 | 394 |
368 i::PartialSerializer partial_ser( | 395 i::List<char> context_data; |
369 internal_isolate, &ser, sink.partial_sink()); | 396 ListSnapshotSink contex_sink(&context_data); |
370 partial_ser.Serialize(&raw_context); | 397 i::PartialSerializer context_ser(internal_isolate, &ser, &contex_sink); |
371 | 398 context_ser.Serialize(&raw_context); |
372 ser.SerializeWeakReferences(); | 399 ser.SerializeWeakReferences(); |
373 | 400 |
| 401 { |
| 402 SnapshotWriter writer(argv[1]); |
| 403 writer.SetOmit(i::FLAG_omit); |
| 404 if (i::FLAG_raw_file && i::FLAG_raw_context_file) |
| 405 writer.SetRawFiles(i::FLAG_raw_file, i::FLAG_raw_context_file); |
374 #ifdef COMPRESS_STARTUP_DATA_BZ2 | 406 #ifdef COMPRESS_STARTUP_DATA_BZ2 |
375 BZip2Compressor compressor; | 407 BZip2Compressor bzip2; |
376 if (!sink.Compress(&compressor)) | 408 writer.SetCompressor(&bzip2); |
377 return 1; | |
378 if (!sink.partial_sink()->Compress(&compressor)) | |
379 return 1; | |
380 #endif | 409 #endif |
381 sink.WriteSnapshot(); | 410 writer.WriteSnapshot(snapshot_data, ser, context_data, context_ser); |
382 sink.WritePartialSnapshot(); | 411 } |
383 | 412 |
384 sink.WriteSpaceUsed( | |
385 "context_", | |
386 partial_ser.CurrentAllocationAddress(i::NEW_SPACE), | |
387 partial_ser.CurrentAllocationAddress(i::OLD_POINTER_SPACE), | |
388 partial_ser.CurrentAllocationAddress(i::OLD_DATA_SPACE), | |
389 partial_ser.CurrentAllocationAddress(i::CODE_SPACE), | |
390 partial_ser.CurrentAllocationAddress(i::MAP_SPACE), | |
391 partial_ser.CurrentAllocationAddress(i::CELL_SPACE), | |
392 partial_ser.CurrentAllocationAddress(i::PROPERTY_CELL_SPACE)); | |
393 sink.WriteSpaceUsed( | |
394 "", | |
395 ser.CurrentAllocationAddress(i::NEW_SPACE), | |
396 ser.CurrentAllocationAddress(i::OLD_POINTER_SPACE), | |
397 ser.CurrentAllocationAddress(i::OLD_DATA_SPACE), | |
398 ser.CurrentAllocationAddress(i::CODE_SPACE), | |
399 ser.CurrentAllocationAddress(i::MAP_SPACE), | |
400 ser.CurrentAllocationAddress(i::CELL_SPACE), | |
401 ser.CurrentAllocationAddress(i::PROPERTY_CELL_SPACE)); | |
402 isolate->Exit(); | 413 isolate->Exit(); |
403 isolate->Dispose(); | 414 isolate->Dispose(); |
404 V8::Dispose(); | 415 V8::Dispose(); |
405 return 0; | 416 return 0; |
406 } | 417 } |
OLD | NEW |