blob: 275c8acc832c279dc530cd2285993cc15cf620de [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
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.
15//
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.
27
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000028#include <errno.h>
29#include <stdio.h>
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +000030#ifdef COMPRESS_STARTUP_DATA_BZ2
31#include <bzlib.h>
32#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033#include <signal.h>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000034
35#include "v8.h"
36
37#include "bootstrapper.h"
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000038#include "flags.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039#include "natives.h"
40#include "platform.h"
41#include "serialize.h"
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000042#include "list.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044using namespace v8;
45
46static const unsigned int kMaxCounters = 256;
47
48// A single counter in a counter collection.
49class Counter {
50 public:
51 static const int kMaxNameSize = 64;
ager@chromium.orga74f0da2008-12-03 16:05:52 +000052 int32_t* Bind(const char* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053 int i;
54 for (i = 0; i < kMaxNameSize - 1 && name[i]; i++) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +000055 name_[i] = name[i];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000056 }
57 name_[i] = '\0';
58 return &counter_;
59 }
60 private:
61 int32_t counter_;
62 uint8_t name_[kMaxNameSize];
63};
64
65
66// A set of counters and associated information. An instance of this
67// class is stored directly in the memory-mapped counters file if
68// the --save-counters options is used
69class CounterCollection {
70 public:
71 CounterCollection() {
72 magic_number_ = 0xDEADFACE;
73 max_counters_ = kMaxCounters;
74 max_name_size_ = Counter::kMaxNameSize;
75 counters_in_use_ = 0;
76 }
77 Counter* GetNextCounter() {
78 if (counters_in_use_ == kMaxCounters) return NULL;
79 return &counters_[counters_in_use_++];
80 }
81 private:
82 uint32_t magic_number_;
83 uint32_t max_counters_;
84 uint32_t max_name_size_;
85 uint32_t counters_in_use_;
86 Counter counters_[kMaxCounters];
87};
88
89
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +000090class Compressor {
ager@chromium.orgc4c92722009-11-18 14:12:51 +000091 public:
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +000092 virtual ~Compressor() {}
93 virtual bool Compress(i::Vector<char> input) = 0;
94 virtual i::Vector<char>* output() = 0;
95};
96
97
98class PartialSnapshotSink : public i::SnapshotByteSink {
99 public:
100 PartialSnapshotSink() : data_(), raw_size_(-1) { }
101 virtual ~PartialSnapshotSink() { data_.Free(); }
102 virtual void Put(int byte, const char* description) {
103 data_.Add(byte);
104 }
105 virtual int Position() { return data_.length(); }
106 void Print(FILE* fp) {
107 int length = Position();
108 for (int j = 0; j < length; j++) {
109 if ((j & 0x1f) == 0x1f) {
110 fprintf(fp, "\n");
111 }
112 if (j != 0) {
113 fprintf(fp, ",");
114 }
ricow@chromium.org7ad65222011-12-19 12:13:11 +0000115 fprintf(fp, "%u", static_cast<unsigned char>(at(j)));
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000116 }
117 }
118 char at(int i) { return data_[i]; }
119 bool Compress(Compressor* compressor) {
120 ASSERT_EQ(-1, raw_size_);
121 raw_size_ = data_.length();
122 if (!compressor->Compress(data_.ToVector())) return false;
123 data_.Clear();
124 data_.AddAll(*compressor->output());
125 return true;
126 }
127 int raw_size() { return raw_size_; }
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000128
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000129 private:
130 i::List<char> data_;
131 int raw_size_;
132};
133
134
135class CppByteSink : public PartialSnapshotSink {
136 public:
137 explicit CppByteSink(const char* snapshot_file) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000138 fp_ = i::OS::FOpen(snapshot_file, "wb");
139 if (fp_ == NULL) {
140 i::PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file);
141 exit(1);
142 }
143 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n");
144 fprintf(fp_, "#include \"v8.h\"\n");
145 fprintf(fp_, "#include \"platform.h\"\n\n");
146 fprintf(fp_, "#include \"snapshot.h\"\n\n");
147 fprintf(fp_, "namespace v8 {\nnamespace internal {\n\n");
148 fprintf(fp_, "const byte Snapshot::data_[] = {");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000150
151 virtual ~CppByteSink() {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000152 fprintf(fp_, "const int Snapshot::size_ = %d;\n", Position());
153#ifdef COMPRESS_STARTUP_DATA_BZ2
154 fprintf(fp_, "const byte* Snapshot::raw_data_ = NULL;\n");
155 fprintf(fp_,
156 "const int Snapshot::raw_size_ = %d;\n\n",
157 raw_size());
158#else
159 fprintf(fp_,
160 "const byte* Snapshot::raw_data_ = Snapshot::data_;\n");
161 fprintf(fp_,
162 "const int Snapshot::raw_size_ = Snapshot::size_;\n\n");
163#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000164 fprintf(fp_, "} } // namespace v8::internal\n");
165 fclose(fp_);
166 }
167
168 void WriteSpaceUsed(
169 int new_space_used,
170 int pointer_space_used,
171 int data_space_used,
172 int code_space_used,
173 int map_space_used,
174 int cell_space_used,
175 int large_space_used) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000176 fprintf(fp_, "const int Snapshot::new_space_used_ = %d;\n", new_space_used);
177 fprintf(fp_,
178 "const int Snapshot::pointer_space_used_ = %d;\n",
179 pointer_space_used);
180 fprintf(fp_,
181 "const int Snapshot::data_space_used_ = %d;\n",
182 data_space_used);
183 fprintf(fp_,
184 "const int Snapshot::code_space_used_ = %d;\n",
185 code_space_used);
186 fprintf(fp_, "const int Snapshot::map_space_used_ = %d;\n", map_space_used);
187 fprintf(fp_,
188 "const int Snapshot::cell_space_used_ = %d;\n",
189 cell_space_used);
190 fprintf(fp_,
191 "const int Snapshot::large_space_used_ = %d;\n",
192 large_space_used);
193 }
194
195 void WritePartialSnapshot() {
196 int length = partial_sink_.Position();
197 fprintf(fp_, "};\n\n");
198 fprintf(fp_, "const int Snapshot::context_size_ = %d;\n", length);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000199#ifdef COMPRESS_STARTUP_DATA_BZ2
200 fprintf(fp_,
201 "const int Snapshot::context_raw_size_ = %d;\n",
202 partial_sink_.raw_size());
203#else
204 fprintf(fp_,
205 "const int Snapshot::context_raw_size_ = "
206 "Snapshot::context_size_;\n");
207#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000208 fprintf(fp_, "const byte Snapshot::context_data_[] = {\n");
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000209 partial_sink_.Print(fp_);
210 fprintf(fp_, "};\n\n");
211#ifdef COMPRESS_STARTUP_DATA_BZ2
212 fprintf(fp_, "const byte* Snapshot::context_raw_data_ = NULL;\n");
213#else
214 fprintf(fp_, "const byte* Snapshot::context_raw_data_ ="
215 " Snapshot::context_data_;\n");
216#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000218
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000219 void WriteSnapshot() {
220 Print(fp_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000221 }
222
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000223 PartialSnapshotSink* partial_sink() { return &partial_sink_; }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000224
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000225 private:
226 FILE* fp_;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000227 PartialSnapshotSink partial_sink_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000228};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000229
230
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000231#ifdef COMPRESS_STARTUP_DATA_BZ2
232class BZip2Compressor : public Compressor {
233 public:
234 BZip2Compressor() : output_(NULL) {}
235 virtual ~BZip2Compressor() {
236 delete output_;
237 }
238 virtual bool Compress(i::Vector<char> input) {
239 delete output_;
240 output_ = new i::ScopedVector<char>((input.length() * 101) / 100 + 1000);
241 unsigned int output_length_ = output_->length();
242 int result = BZ2_bzBuffToBuffCompress(output_->start(), &output_length_,
243 input.start(), input.length(),
244 9, 1, 0);
245 if (result == BZ_OK) {
246 output_->Truncate(output_length_);
247 return true;
248 } else {
249 fprintf(stderr, "bzlib error code: %d\n", result);
250 return false;
251 }
252 }
253 virtual i::Vector<char>* output() { return output_; }
254
255 private:
256 i::ScopedVector<char>* output_;
257};
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000258
259
260class BZip2Decompressor : public StartupDataDecompressor {
261 public:
262 virtual ~BZip2Decompressor() { }
263
264 protected:
265 virtual int DecompressData(char* raw_data,
266 int* raw_data_size,
267 const char* compressed_data,
268 int compressed_data_size) {
269 ASSERT_EQ(StartupData::kBZip2,
270 V8::GetCompressedStartupDataAlgorithm());
271 unsigned int decompressed_size = *raw_data_size;
272 int result =
273 BZ2_bzBuffToBuffDecompress(raw_data,
274 &decompressed_size,
275 const_cast<char*>(compressed_data),
276 compressed_data_size,
277 0, 1);
278 if (result == BZ_OK) {
279 *raw_data_size = decompressed_size;
280 }
281 return result;
282 }
283};
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000284#endif
285
286
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287int main(int argc, char** argv) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288 // By default, log code create information in the snapshot.
289 i::FLAG_log_code = true;
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000290
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291 // Print the usage if an error occurs when parsing the command line
292 // flags or if the help flag is set.
293 int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000294 if (result > 0 || argc != 2 || i::FLAG_help) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295 ::printf("Usage: %s [flag] ... outfile\n", argv[0]);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000296 i::FlagList::PrintHelp();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000297 return !i::FLAG_help;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298 }
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000299#ifdef COMPRESS_STARTUP_DATA_BZ2
300 BZip2Decompressor natives_decompressor;
301 int bz2_result = natives_decompressor.Decompress();
302 if (bz2_result != BZ_OK) {
303 fprintf(stderr, "bzip error code: %d\n", bz2_result);
304 exit(1);
305 }
306#endif
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000307 i::Serializer::Enable();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000308 Persistent<Context> context = v8::Context::New();
verwaest@chromium.org37141392012-05-31 13:27:02 +0000309 if (context.IsEmpty()) {
310 fprintf(stderr,
311 "\nException thrown while compiling natives - see above.\n\n");
312 exit(1);
313 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000314 if (i::FLAG_extra_code != NULL) {
315 context->Enter();
316 // Capture 100 frames if anything happens.
317 V8::SetCaptureStackTraceForUncaughtExceptions(true, 100);
318 HandleScope scope;
319 const char* name = i::FLAG_extra_code;
320 FILE* file = i::OS::FOpen(name, "rb");
321 if (file == NULL) {
322 fprintf(stderr, "Failed to open '%s': errno %d\n", name, errno);
323 exit(1);
324 }
325
326 fseek(file, 0, SEEK_END);
327 int size = ftell(file);
328 rewind(file);
329
330 char* chars = new char[size + 1];
331 chars[size] = '\0';
332 for (int i = 0; i < size;) {
333 int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
334 if (read < 0) {
335 fprintf(stderr, "Failed to read '%s': errno %d\n", name, errno);
336 exit(1);
337 }
338 i += read;
339 }
340 fclose(file);
341 Local<String> source = String::New(chars);
342 TryCatch try_catch;
343 Local<Script> script = Script::Compile(source);
344 if (try_catch.HasCaught()) {
345 fprintf(stderr, "Failure compiling '%s' (see above)\n", name);
346 exit(1);
347 }
348 script->Run();
349 if (try_catch.HasCaught()) {
350 fprintf(stderr, "Failure running '%s'\n", name);
351 Local<Message> message = try_catch.Message();
352 Local<String> message_string = message->Get();
353 Local<String> message_line = message->GetSourceLine();
354 int len = 2 + message_string->Utf8Length() + message_line->Utf8Length();
355 char* buf = new char(len);
356 message_string->WriteUtf8(buf);
357 fprintf(stderr, "%s at line %d\n", buf, message->GetLineNumber());
358 message_line->WriteUtf8(buf);
359 fprintf(stderr, "%s\n", buf);
360 int from = message->GetStartColumn();
361 int to = message->GetEndColumn();
362 int i;
363 for (i = 0; i < from; i++) fprintf(stderr, " ");
364 for ( ; i <= to; i++) fprintf(stderr, "^");
365 fprintf(stderr, "\n");
366 exit(1);
367 }
368 context->Exit();
369 }
kasper.lund7276f142008-07-30 08:49:36 +0000370 // Make sure all builtin scripts are cached.
371 { HandleScope scope;
372 for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000373 i::Isolate::Current()->bootstrapper()->NativesSourceLookup(i);
kasper.lund7276f142008-07-30 08:49:36 +0000374 }
375 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000376 // If we don't do this then we end up with a stray root pointing at the
377 // context even after we have disposed of the context.
rossberg@chromium.org994edf62012-02-06 10:12:55 +0000378 HEAP->CollectAllGarbage(i::Heap::kNoGCFlags, "mksnapshot");
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000379 i::Object* raw_context = *(v8::Utils::OpenHandle(*context));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000380 context.Dispose();
381 CppByteSink sink(argv[1]);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000382 // This results in a somewhat smaller snapshot, probably because it gets rid
383 // of some things that are cached between garbage collections.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000384 i::StartupSerializer ser(&sink);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000385 ser.SerializeStrongReferences();
386
387 i::PartialSerializer partial_ser(&ser, sink.partial_sink());
388 partial_ser.Serialize(&raw_context);
389
390 ser.SerializeWeakReferences();
391
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000392#ifdef COMPRESS_STARTUP_DATA_BZ2
393 BZip2Compressor compressor;
394 if (!sink.Compress(&compressor))
395 return 1;
396 if (!sink.partial_sink()->Compress(&compressor))
397 return 1;
398#endif
399 sink.WriteSnapshot();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000400 sink.WritePartialSnapshot();
401
402 sink.WriteSpaceUsed(
403 partial_ser.CurrentAllocationAddress(i::NEW_SPACE),
404 partial_ser.CurrentAllocationAddress(i::OLD_POINTER_SPACE),
405 partial_ser.CurrentAllocationAddress(i::OLD_DATA_SPACE),
406 partial_ser.CurrentAllocationAddress(i::CODE_SPACE),
407 partial_ser.CurrentAllocationAddress(i::MAP_SPACE),
408 partial_ser.CurrentAllocationAddress(i::CELL_SPACE),
409 partial_ser.CurrentAllocationAddress(i::LO_SPACE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000410 return 0;
411}