1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifdef COMPRESS_STARTUP_DATA_BZ2
35 #include "bootstrapper.h"
38 #include "serialize.h"
43 static const unsigned int kMaxCounters = 256;
45 // A single counter in a counter collection.
48 static const int kMaxNameSize = 64;
49 int32_t* Bind(const char* name) {
51 for (i = 0; i < kMaxNameSize - 1 && name[i]; i++) {
59 uint8_t name_[kMaxNameSize];
63 // A set of counters and associated information. An instance of this
64 // class is stored directly in the memory-mapped counters file if
65 // the --save-counters options is used
66 class CounterCollection {
69 magic_number_ = 0xDEADFACE;
70 max_counters_ = kMaxCounters;
71 max_name_size_ = Counter::kMaxNameSize;
74 Counter* GetNextCounter() {
75 if (counters_in_use_ == kMaxCounters) return NULL;
76 return &counters_[counters_in_use_++];
79 uint32_t magic_number_;
80 uint32_t max_counters_;
81 uint32_t max_name_size_;
82 uint32_t counters_in_use_;
83 Counter counters_[kMaxCounters];
89 virtual ~Compressor() {}
90 virtual bool Compress(i::Vector<char> input) = 0;
91 virtual i::Vector<char>* output() = 0;
95 class PartialSnapshotSink : public i::SnapshotByteSink {
97 PartialSnapshotSink() : data_(), raw_size_(-1) { }
98 virtual ~PartialSnapshotSink() { data_.Free(); }
99 virtual void Put(int byte, const char* description) {
102 virtual int Position() { return data_.length(); }
103 void Print(FILE* fp) {
104 int length = Position();
105 for (int j = 0; j < length; j++) {
106 if ((j & 0x1f) == 0x1f) {
112 fprintf(fp, "%u", static_cast<unsigned char>(at(j)));
115 char at(int i) { return data_[i]; }
116 bool Compress(Compressor* compressor) {
117 ASSERT_EQ(-1, raw_size_);
118 raw_size_ = data_.length();
119 if (!compressor->Compress(data_.ToVector())) return false;
121 data_.AddAll(*compressor->output());
124 int raw_size() { return raw_size_; }
132 class CppByteSink : public PartialSnapshotSink {
134 explicit CppByteSink(const char* snapshot_file) {
135 fp_ = i::OS::FOpen(snapshot_file, "wb");
137 i::PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file);
140 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n");
141 fprintf(fp_, "#include \"v8.h\"\n");
142 fprintf(fp_, "#include \"platform.h\"\n\n");
143 fprintf(fp_, "#include \"snapshot.h\"\n\n");
144 fprintf(fp_, "namespace v8 {\nnamespace internal {\n\n");
145 fprintf(fp_, "const byte Snapshot::data_[] = {");
148 virtual ~CppByteSink() {
149 fprintf(fp_, "const int Snapshot::size_ = %d;\n", Position());
150 #ifdef COMPRESS_STARTUP_DATA_BZ2
151 fprintf(fp_, "const byte* Snapshot::raw_data_ = NULL;\n");
153 "const int Snapshot::raw_size_ = %d;\n\n",
157 "const byte* Snapshot::raw_data_ = Snapshot::data_;\n");
159 "const int Snapshot::raw_size_ = Snapshot::size_;\n\n");
161 fprintf(fp_, "} } // namespace v8::internal\n");
167 int pointer_space_used,
172 int large_space_used) {
173 fprintf(fp_, "const int Snapshot::new_space_used_ = %d;\n", new_space_used);
175 "const int Snapshot::pointer_space_used_ = %d;\n",
178 "const int Snapshot::data_space_used_ = %d;\n",
181 "const int Snapshot::code_space_used_ = %d;\n",
183 fprintf(fp_, "const int Snapshot::map_space_used_ = %d;\n", map_space_used);
185 "const int Snapshot::cell_space_used_ = %d;\n",
188 "const int Snapshot::large_space_used_ = %d;\n",
192 void WritePartialSnapshot() {
193 int length = partial_sink_.Position();
194 fprintf(fp_, "};\n\n");
195 fprintf(fp_, "const int Snapshot::context_size_ = %d;\n", length);
196 #ifdef COMPRESS_STARTUP_DATA_BZ2
198 "const int Snapshot::context_raw_size_ = %d;\n",
199 partial_sink_.raw_size());
202 "const int Snapshot::context_raw_size_ = "
203 "Snapshot::context_size_;\n");
205 fprintf(fp_, "const byte Snapshot::context_data_[] = {\n");
206 partial_sink_.Print(fp_);
207 fprintf(fp_, "};\n\n");
208 #ifdef COMPRESS_STARTUP_DATA_BZ2
209 fprintf(fp_, "const byte* Snapshot::context_raw_data_ = NULL;\n");
211 fprintf(fp_, "const byte* Snapshot::context_raw_data_ ="
212 " Snapshot::context_data_;\n");
216 void WriteSnapshot() {
220 PartialSnapshotSink* partial_sink() { return &partial_sink_; }
224 PartialSnapshotSink partial_sink_;
228 #ifdef COMPRESS_STARTUP_DATA_BZ2
229 class BZip2Compressor : public Compressor {
231 BZip2Compressor() : output_(NULL) {}
232 virtual ~BZip2Compressor() {
235 virtual bool Compress(i::Vector<char> input) {
237 output_ = new i::ScopedVector<char>((input.length() * 101) / 100 + 1000);
238 unsigned int output_length_ = output_->length();
239 int result = BZ2_bzBuffToBuffCompress(output_->start(), &output_length_,
240 input.start(), input.length(),
242 if (result == BZ_OK) {
243 output_->Truncate(output_length_);
246 fprintf(stderr, "bzlib error code: %d\n", result);
250 virtual i::Vector<char>* output() { return output_; }
253 i::ScopedVector<char>* output_;
257 class BZip2Decompressor : public StartupDataDecompressor {
259 virtual ~BZip2Decompressor() { }
262 virtual int DecompressData(char* raw_data,
264 const char* compressed_data,
265 int compressed_data_size) {
266 ASSERT_EQ(StartupData::kBZip2,
267 V8::GetCompressedStartupDataAlgorithm());
268 unsigned int decompressed_size = *raw_data_size;
270 BZ2_bzBuffToBuffDecompress(raw_data,
272 const_cast<char*>(compressed_data),
273 compressed_data_size,
275 if (result == BZ_OK) {
276 *raw_data_size = decompressed_size;
284 int main(int argc, char** argv) {
285 // By default, log code create information in the snapshot.
286 i::FLAG_log_code = true;
288 // Print the usage if an error occurs when parsing the command line
289 // flags or if the help flag is set.
290 int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
291 if (result > 0 || argc != 2 || i::FLAG_help) {
292 ::printf("Usage: %s [flag] ... outfile\n", argv[0]);
293 i::FlagList::PrintHelp();
294 return !i::FLAG_help;
296 #ifdef COMPRESS_STARTUP_DATA_BZ2
297 BZip2Decompressor natives_decompressor;
298 int bz2_result = natives_decompressor.Decompress();
299 if (bz2_result != BZ_OK) {
300 fprintf(stderr, "bzip error code: %d\n", bz2_result);
304 i::Serializer::Enable();
305 Persistent<Context> context = v8::Context::New();
306 ASSERT(!context.IsEmpty());
307 // Make sure all builtin scripts are cached.
309 for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) {
310 i::Isolate::Current()->bootstrapper()->NativesSourceLookup(i);
313 // If we don't do this then we end up with a stray root pointing at the
314 // context even after we have disposed of the context.
315 HEAP->CollectAllGarbage(i::Heap::kNoGCFlags, "mksnapshot");
316 i::Object* raw_context = *(v8::Utils::OpenHandle(*context));
318 CppByteSink sink(argv[1]);
319 // This results in a somewhat smaller snapshot, probably because it gets rid
320 // of some things that are cached between garbage collections.
321 i::StartupSerializer ser(&sink);
322 ser.SerializeStrongReferences();
324 i::PartialSerializer partial_ser(&ser, sink.partial_sink());
325 partial_ser.Serialize(&raw_context);
327 ser.SerializeWeakReferences();
329 #ifdef COMPRESS_STARTUP_DATA_BZ2
330 BZip2Compressor compressor;
331 if (!sink.Compress(&compressor))
333 if (!sink.partial_sink()->Compress(&compressor))
336 sink.WriteSnapshot();
337 sink.WritePartialSnapshot();
340 partial_ser.CurrentAllocationAddress(i::NEW_SPACE),
341 partial_ser.CurrentAllocationAddress(i::OLD_POINTER_SPACE),
342 partial_ser.CurrentAllocationAddress(i::OLD_DATA_SPACE),
343 partial_ser.CurrentAllocationAddress(i::CODE_SPACE),
344 partial_ser.CurrentAllocationAddress(i::MAP_SPACE),
345 partial_ser.CurrentAllocationAddress(i::CELL_SPACE),
346 partial_ser.CurrentAllocationAddress(i::LO_SPACE));