Chromium Code Reviews| 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 ~SnapshotWriter() { | |
|
Sven Panne
2014/04/24 06:59:17
Here and below: We normally use 1 blank line betwe
vogelheim
2014/04/24 11:07:59
Done.
| |
| 76 fclose(fp_); | |
| 77 if (raw_file_) fclose(raw_file_); | |
| 78 if (raw_context_file_) fclose(raw_context_file_); | |
| 79 } | |
| 80 void SetCompressor(Compressor* compressor) { | |
| 81 compressor_ = compressor; | |
| 82 } | |
| 83 void SetOmit(bool omit) { | |
| 84 omit_ = omit; | |
| 85 } | |
| 86 void SetRawFiles(const char* raw_file, const char* raw_context_file) { | |
| 87 raw_file_ = GetFileDescriptorOrDie(raw_file); | |
| 88 raw_context_file_ = GetFileDescriptorOrDie(raw_context_file); | |
| 89 } | |
| 90 void WriteSnapshot( | |
| 91 const i::List<char>& snapshot_data, | |
| 92 const i::Serializer& serializer, | |
| 93 const i::List<char>& context_snapshot_data, | |
| 94 const i::Serializer& context_serializer) const { | |
| 95 WriteFilePrefix(); | |
| 96 WriteData("", snapshot_data, raw_file_); | |
| 97 WriteData("context_", context_snapshot_data, raw_context_file_); | |
| 98 WriteMeta("context_", context_serializer); | |
| 99 WriteMeta("", serializer); | |
| 100 WriteFileSuffix(); | |
| 101 } | |
| 102 | |
| 103 private: | |
| 104 void WriteFilePrefix() const { | |
| 100 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); | 105 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); |
| 101 fprintf(fp_, "#include \"v8.h\"\n"); | 106 fprintf(fp_, "#include \"v8.h\"\n"); |
| 102 fprintf(fp_, "#include \"platform.h\"\n\n"); | 107 fprintf(fp_, "#include \"platform.h\"\n\n"); |
| 103 fprintf(fp_, "#include \"snapshot.h\"\n\n"); | 108 fprintf(fp_, "#include \"snapshot.h\"\n\n"); |
| 104 fprintf(fp_, "namespace v8 {\nnamespace internal {\n\n"); | 109 fprintf(fp_, "namespace v8 {\n"); |
| 105 fprintf(fp_, "const byte Snapshot::data_[] = {"); | 110 fprintf(fp_, "namespace internal {\n\n"); |
| 111 } | |
| 112 void WriteFileSuffix() const { | |
| 113 fprintf(fp_, "} // namespace internal\n"); | |
| 114 fprintf(fp_, "} // namespace v8\n"); | |
| 115 } | |
| 116 void WriteData(const char* prefix, const i::List<char>& source_data, | |
|
Sven Panne
2014/04/24 06:59:17
Here and at other places: If parameters don't fit
vogelheim
2014/04/24 11:07:59
Done.
| |
| 117 FILE* raw_file) const { | |
| 118 const i::List <char>* data_to_be_written = NULL; | |
| 119 i::List<char> compressed_data; | |
| 120 if (!compressor_) { | |
| 121 data_to_be_written = &source_data; | |
| 122 } else if (compressor_->Compress(source_data.ToVector())) { | |
| 123 compressed_data.AddAll(*compressor_->output()); | |
| 124 data_to_be_written = &compressed_data; | |
| 125 } else { | |
| 126 i::PrintF("Compression failed. Aborting.\n"); | |
| 127 exit(1); | |
| 128 } | |
| 129 | |
| 130 ASSERT(data_to_be_written); | |
| 131 MaybeWriteRawFile(data_to_be_written, raw_file); | |
| 132 WriteData(prefix, source_data, data_to_be_written); | |
| 133 } | |
| 134 void MaybeWriteRawFile(const i::List<char>* data, FILE* raw_file) const { | |
| 135 if (!data || !raw_file) | |
| 136 return; | |
| 137 | |
| 138 // Sanity check, whether i::List iterators truly return pointers to an | |
| 139 // internal array. | |
| 140 ASSERT(data->end() - data->begin() == data->length()); | |
| 141 | |
| 142 size_t written = fwrite(data->begin(), 1, data->length(), raw_file); | |
| 143 if (written != (size_t)data->length()) { | |
| 144 i::PrintF("Writing raw file failed.. Aborting.\n"); | |
| 145 exit(1); | |
| 146 } | |
| 147 } | |
| 148 void WriteData(const char* prefix, const i::List<char>& source_data, | |
| 149 const i::List<char>* data_to_be_written) const { | |
| 150 fprintf(fp_, "const byte Snapshot::%sdata_[] = {\n", prefix); | |
| 151 if (!omit_) | |
| 152 WriteSnapshotData(data_to_be_written); | |
| 153 fprintf(fp_, "};\n"); | |
| 154 fprintf(fp_, "const int Snapshot::%ssize_ = %d;\n", prefix, | |
| 155 data_to_be_written->length()); | |
| 156 | |
| 157 if (data_to_be_written == &source_data && !omit_) { | |
| 158 fprintf(fp_, "const byte* Snapshot::%sraw_data_ = Snapshot::%sdata_;\n", | |
| 159 prefix, prefix); | |
| 160 fprintf(fp_, "const int Snapshot::%sraw_size_ = Snapshot::%ssize_;\n", | |
| 161 prefix, prefix); | |
| 162 } else { | |
| 163 fprintf(fp_, "const byte* Snapshot::%sraw_data_ = NULL;\n", prefix); | |
| 164 fprintf(fp_, "const int Snapshot::%sraw_size_ = %d;\n", | |
| 165 prefix, source_data.length()); | |
| 166 } | |
| 167 fprintf(fp_, "\n"); | |
| 168 } | |
| 169 void WriteMeta(const char* prefix, const i::Serializer& ser) const { | |
| 170 WriteSizeVar(ser, prefix, "new", i::NEW_SPACE); | |
| 171 WriteSizeVar(ser, prefix, "pointer", i::OLD_POINTER_SPACE); | |
| 172 WriteSizeVar(ser, prefix, "data", i::OLD_DATA_SPACE); | |
| 173 WriteSizeVar(ser, prefix, "code", i::CODE_SPACE); | |
| 174 WriteSizeVar(ser, prefix, "map", i::MAP_SPACE); | |
| 175 WriteSizeVar(ser, prefix, "cell", i::CELL_SPACE); | |
| 176 WriteSizeVar(ser, prefix, "property_cell", i::PROPERTY_CELL_SPACE); | |
| 177 fprintf(fp_, "\n"); | |
| 178 } | |
| 179 void WriteSizeVar(const i::Serializer& ser, const char* prefix, | |
| 180 const char* name, int space) const { | |
| 181 fprintf(fp_, "const int Snapshot::%s%s_space_used_ = %d;\n", | |
| 182 prefix, name, ser.CurrentAllocationAddress(space)); | |
| 183 } | |
| 184 void WriteSnapshotData(const i::List<char>* data) const { | |
| 185 for (int i = 0; i < data->length(); i++) { | |
| 186 if ((i & 0x1f) == 0x1f) | |
| 187 fprintf(fp_, "\n"); | |
| 188 if (i > 0) | |
| 189 fprintf(fp_, ","); | |
| 190 fprintf(fp_, "%u", static_cast<unsigned char>(data->at(i))); | |
| 191 } | |
| 192 fprintf(fp_, "\n"); | |
| 193 } | |
| 194 FILE* GetFileDescriptorOrDie(const char* filename) { | |
| 195 FILE* fp = i::OS::FOpen(filename, "wb"); | |
| 196 if (fp == NULL) { | |
| 197 i::PrintF("Unable to open file \"%s\" for writing.\n", filename); | |
| 198 exit(1); | |
| 199 } | |
| 200 return fp; | |
| 106 } | 201 } |
| 107 | 202 |
| 108 virtual ~CppByteSink() { | |
| 109 fprintf(fp_, "const int Snapshot::size_ = %d;\n", Position()); | |
| 110 #ifdef COMPRESS_STARTUP_DATA_BZ2 | |
| 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 } | |
| 124 | |
| 125 void WriteSpaceUsed( | |
| 126 const char* prefix, | |
| 127 int new_space_used, | |
| 128 int pointer_space_used, | |
| 129 int data_space_used, | |
| 130 int code_space_used, | |
| 131 int map_space_used, | |
| 132 int cell_space_used, | |
| 133 int property_cell_space_used) { | |
| 134 fprintf(fp_, | |
| 135 "const int Snapshot::%snew_space_used_ = %d;\n", | |
| 136 prefix, | |
| 137 new_space_used); | |
| 138 fprintf(fp_, | |
| 139 "const int Snapshot::%spointer_space_used_ = %d;\n", | |
| 140 prefix, | |
| 141 pointer_space_used); | |
| 142 fprintf(fp_, | |
| 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 } | |
| 163 | |
| 164 void WritePartialSnapshot() { | |
| 165 int length = partial_sink_.Position(); | |
| 166 fprintf(fp_, "};\n\n"); | |
| 167 fprintf(fp_, "const int Snapshot::context_size_ = %d;\n", length); | |
| 168 #ifdef COMPRESS_STARTUP_DATA_BZ2 | |
| 169 fprintf(fp_, | |
| 170 "const int Snapshot::context_raw_size_ = %d;\n", | |
| 171 partial_sink_.raw_size()); | |
| 172 #else | |
| 173 fprintf(fp_, | |
| 174 "const int Snapshot::context_raw_size_ = " | |
| 175 "Snapshot::context_size_;\n"); | |
| 176 #endif | |
| 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 } | |
| 187 | |
| 188 void WriteSnapshot() { | |
| 189 Print(fp_); | |
| 190 } | |
| 191 | |
| 192 PartialSnapshotSink* partial_sink() { return &partial_sink_; } | |
| 193 | |
| 194 private: | |
| 195 FILE* fp_; | 203 FILE* fp_; |
| 196 PartialSnapshotSink partial_sink_; | 204 FILE* raw_file_; |
| 205 FILE* raw_context_file_; | |
| 206 Compressor* compressor_; | |
| 207 bool omit_; | |
| 197 }; | 208 }; |
| 198 | 209 |
| 199 | 210 |
| 200 #ifdef COMPRESS_STARTUP_DATA_BZ2 | 211 #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 201 class BZip2Compressor : public Compressor { | 212 class BZip2Compressor : public Compressor { |
| 202 public: | 213 public: |
| 203 BZip2Compressor() : output_(NULL) {} | 214 BZip2Compressor() : output_(NULL) {} |
| 204 virtual ~BZip2Compressor() { | 215 virtual ~BZip2Compressor() { |
| 205 delete output_; | 216 delete output_; |
| 206 } | 217 } |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 352 for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) { | 363 for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) { |
| 353 internal_isolate->bootstrapper()->NativesSourceLookup(i); | 364 internal_isolate->bootstrapper()->NativesSourceLookup(i); |
| 354 } | 365 } |
| 355 } | 366 } |
| 356 // If we don't do this then we end up with a stray root pointing at the | 367 // 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. | 368 // context even after we have disposed of the context. |
| 358 internal_isolate->heap()->CollectAllGarbage( | 369 internal_isolate->heap()->CollectAllGarbage( |
| 359 i::Heap::kNoGCFlags, "mksnapshot"); | 370 i::Heap::kNoGCFlags, "mksnapshot"); |
| 360 i::Object* raw_context = *v8::Utils::OpenPersistent(context); | 371 i::Object* raw_context = *v8::Utils::OpenPersistent(context); |
| 361 context.Reset(); | 372 context.Reset(); |
| 362 CppByteSink sink(argv[1]); | 373 |
| 363 // This results in a somewhat smaller snapshot, probably because it gets rid | 374 // This results in a somewhat smaller snapshot, probably because it gets rid |
| 364 // of some things that are cached between garbage collections. | 375 // of some things that are cached between garbage collections. |
| 365 i::StartupSerializer ser(internal_isolate, &sink); | 376 i::List<char> snapshot_data; |
| 377 ListSnapshotSink snapshot_sink(&snapshot_data); | |
| 378 i::StartupSerializer ser(internal_isolate, &snapshot_sink); | |
| 366 ser.SerializeStrongReferences(); | 379 ser.SerializeStrongReferences(); |
| 367 | 380 |
| 368 i::PartialSerializer partial_ser( | 381 i::List<char> context_data; |
| 369 internal_isolate, &ser, sink.partial_sink()); | 382 ListSnapshotSink contex_sink(&context_data); |
| 370 partial_ser.Serialize(&raw_context); | 383 i::PartialSerializer context_ser(internal_isolate, &ser, &contex_sink); |
| 371 | 384 context_ser.Serialize(&raw_context); |
| 372 ser.SerializeWeakReferences(); | 385 ser.SerializeWeakReferences(); |
| 373 | 386 |
| 387 { | |
| 388 SnapshotWriter writer(argv[1]); | |
| 389 writer.SetOmit(i::FLAG_omit); | |
| 390 if (i::FLAG_raw_file && i::FLAG_raw_context_file) | |
| 391 writer.SetRawFiles(i::FLAG_raw_file, i::FLAG_raw_context_file); | |
| 374 #ifdef COMPRESS_STARTUP_DATA_BZ2 | 392 #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 375 BZip2Compressor compressor; | 393 BZip2Compressor bzip2; |
| 376 if (!sink.Compress(&compressor)) | 394 writer.SetCompressor(&bzip2); |
| 377 return 1; | |
| 378 if (!sink.partial_sink()->Compress(&compressor)) | |
| 379 return 1; | |
| 380 #endif | 395 #endif |
| 381 sink.WriteSnapshot(); | 396 writer.WriteSnapshot(snapshot_data, ser, context_data, context_ser); |
| 382 sink.WritePartialSnapshot(); | 397 } |
| 383 | 398 |
| 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(); | 399 isolate->Exit(); |
| 403 isolate->Dispose(); | 400 isolate->Dispose(); |
| 404 V8::Dispose(); | 401 V8::Dispose(); |
| 405 return 0; | 402 return 0; |
| 406 } | 403 } |
| OLD | NEW |