blob: 4f5fe96a9027e822f4b359555f436842a0774d81 [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
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +000028#ifdef COMPRESS_STARTUP_DATA_BZ2
29#include <bzlib.h>
30#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000031#include <signal.h>
32#include <string>
33#include <map>
34
35#include "v8.h"
36
37#include "bootstrapper.h"
38#include "natives.h"
39#include "platform.h"
40#include "serialize.h"
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000041#include "list.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043using namespace v8;
44
45static const unsigned int kMaxCounters = 256;
46
47// A single counter in a counter collection.
48class Counter {
49 public:
50 static const int kMaxNameSize = 64;
ager@chromium.orga74f0da2008-12-03 16:05:52 +000051 int32_t* Bind(const char* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052 int i;
53 for (i = 0; i < kMaxNameSize - 1 && name[i]; i++) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +000054 name_[i] = name[i];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000055 }
56 name_[i] = '\0';
57 return &counter_;
58 }
59 private:
60 int32_t counter_;
61 uint8_t name_[kMaxNameSize];
62};
63
64
65// A set of counters and associated information. An instance of this
66// class is stored directly in the memory-mapped counters file if
67// the --save-counters options is used
68class CounterCollection {
69 public:
70 CounterCollection() {
71 magic_number_ = 0xDEADFACE;
72 max_counters_ = kMaxCounters;
73 max_name_size_ = Counter::kMaxNameSize;
74 counters_in_use_ = 0;
75 }
76 Counter* GetNextCounter() {
77 if (counters_in_use_ == kMaxCounters) return NULL;
78 return &counters_[counters_in_use_++];
79 }
80 private:
81 uint32_t magic_number_;
82 uint32_t max_counters_;
83 uint32_t max_name_size_;
84 uint32_t counters_in_use_;
85 Counter counters_[kMaxCounters];
86};
87
88
89// We statically allocate a set of local counters to be used if we
90// don't want to store the stats in a memory-mapped file
91static CounterCollection local_counters;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000092
93
ager@chromium.orga74f0da2008-12-03 16:05:52 +000094typedef std::map<std::string, int*> CounterMap;
95typedef std::map<std::string, int*>::iterator CounterMapIterator;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000096static CounterMap counter_table_;
97
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +000099class Compressor {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000100 public:
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000101 virtual ~Compressor() {}
102 virtual bool Compress(i::Vector<char> input) = 0;
103 virtual i::Vector<char>* output() = 0;
104};
105
106
107class PartialSnapshotSink : public i::SnapshotByteSink {
108 public:
109 PartialSnapshotSink() : data_(), raw_size_(-1) { }
110 virtual ~PartialSnapshotSink() { data_.Free(); }
111 virtual void Put(int byte, const char* description) {
112 data_.Add(byte);
113 }
114 virtual int Position() { return data_.length(); }
115 void Print(FILE* fp) {
116 int length = Position();
117 for (int j = 0; j < length; j++) {
118 if ((j & 0x1f) == 0x1f) {
119 fprintf(fp, "\n");
120 }
121 if (j != 0) {
122 fprintf(fp, ",");
123 }
124 fprintf(fp, "%d", at(j));
125 }
126 }
127 char at(int i) { return data_[i]; }
128 bool Compress(Compressor* compressor) {
129 ASSERT_EQ(-1, raw_size_);
130 raw_size_ = data_.length();
131 if (!compressor->Compress(data_.ToVector())) return false;
132 data_.Clear();
133 data_.AddAll(*compressor->output());
134 return true;
135 }
136 int raw_size() { return raw_size_; }
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000137
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000138 private:
139 i::List<char> data_;
140 int raw_size_;
141};
142
143
144class CppByteSink : public PartialSnapshotSink {
145 public:
146 explicit CppByteSink(const char* snapshot_file) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000147 fp_ = i::OS::FOpen(snapshot_file, "wb");
148 if (fp_ == NULL) {
149 i::PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file);
150 exit(1);
151 }
152 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n");
153 fprintf(fp_, "#include \"v8.h\"\n");
154 fprintf(fp_, "#include \"platform.h\"\n\n");
155 fprintf(fp_, "#include \"snapshot.h\"\n\n");
156 fprintf(fp_, "namespace v8 {\nnamespace internal {\n\n");
157 fprintf(fp_, "const byte Snapshot::data_[] = {");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000159
160 virtual ~CppByteSink() {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000161 fprintf(fp_, "const int Snapshot::size_ = %d;\n", Position());
162#ifdef COMPRESS_STARTUP_DATA_BZ2
163 fprintf(fp_, "const byte* Snapshot::raw_data_ = NULL;\n");
164 fprintf(fp_,
165 "const int Snapshot::raw_size_ = %d;\n\n",
166 raw_size());
167#else
168 fprintf(fp_,
169 "const byte* Snapshot::raw_data_ = Snapshot::data_;\n");
170 fprintf(fp_,
171 "const int Snapshot::raw_size_ = Snapshot::size_;\n\n");
172#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000173 fprintf(fp_, "} } // namespace v8::internal\n");
174 fclose(fp_);
175 }
176
177 void WriteSpaceUsed(
178 int new_space_used,
179 int pointer_space_used,
180 int data_space_used,
181 int code_space_used,
182 int map_space_used,
183 int cell_space_used,
184 int large_space_used) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000185 fprintf(fp_, "const int Snapshot::new_space_used_ = %d;\n", new_space_used);
186 fprintf(fp_,
187 "const int Snapshot::pointer_space_used_ = %d;\n",
188 pointer_space_used);
189 fprintf(fp_,
190 "const int Snapshot::data_space_used_ = %d;\n",
191 data_space_used);
192 fprintf(fp_,
193 "const int Snapshot::code_space_used_ = %d;\n",
194 code_space_used);
195 fprintf(fp_, "const int Snapshot::map_space_used_ = %d;\n", map_space_used);
196 fprintf(fp_,
197 "const int Snapshot::cell_space_used_ = %d;\n",
198 cell_space_used);
199 fprintf(fp_,
200 "const int Snapshot::large_space_used_ = %d;\n",
201 large_space_used);
202 }
203
204 void WritePartialSnapshot() {
205 int length = partial_sink_.Position();
206 fprintf(fp_, "};\n\n");
207 fprintf(fp_, "const int Snapshot::context_size_ = %d;\n", length);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000208#ifdef COMPRESS_STARTUP_DATA_BZ2
209 fprintf(fp_,
210 "const int Snapshot::context_raw_size_ = %d;\n",
211 partial_sink_.raw_size());
212#else
213 fprintf(fp_,
214 "const int Snapshot::context_raw_size_ = "
215 "Snapshot::context_size_;\n");
216#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000217 fprintf(fp_, "const byte Snapshot::context_data_[] = {\n");
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000218 partial_sink_.Print(fp_);
219 fprintf(fp_, "};\n\n");
220#ifdef COMPRESS_STARTUP_DATA_BZ2
221 fprintf(fp_, "const byte* Snapshot::context_raw_data_ = NULL;\n");
222#else
223 fprintf(fp_, "const byte* Snapshot::context_raw_data_ ="
224 " Snapshot::context_data_;\n");
225#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000226 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000227
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000228 void WriteSnapshot() {
229 Print(fp_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000230 }
231
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000232 PartialSnapshotSink* partial_sink() { return &partial_sink_; }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000233
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000234 private:
235 FILE* fp_;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000236 PartialSnapshotSink partial_sink_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000237};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000238
239
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000240#ifdef COMPRESS_STARTUP_DATA_BZ2
241class BZip2Compressor : public Compressor {
242 public:
243 BZip2Compressor() : output_(NULL) {}
244 virtual ~BZip2Compressor() {
245 delete output_;
246 }
247 virtual bool Compress(i::Vector<char> input) {
248 delete output_;
249 output_ = new i::ScopedVector<char>((input.length() * 101) / 100 + 1000);
250 unsigned int output_length_ = output_->length();
251 int result = BZ2_bzBuffToBuffCompress(output_->start(), &output_length_,
252 input.start(), input.length(),
253 9, 1, 0);
254 if (result == BZ_OK) {
255 output_->Truncate(output_length_);
256 return true;
257 } else {
258 fprintf(stderr, "bzlib error code: %d\n", result);
259 return false;
260 }
261 }
262 virtual i::Vector<char>* output() { return output_; }
263
264 private:
265 i::ScopedVector<char>* output_;
266};
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000267
268
269class BZip2Decompressor : public StartupDataDecompressor {
270 public:
271 virtual ~BZip2Decompressor() { }
272
273 protected:
274 virtual int DecompressData(char* raw_data,
275 int* raw_data_size,
276 const char* compressed_data,
277 int compressed_data_size) {
278 ASSERT_EQ(StartupData::kBZip2,
279 V8::GetCompressedStartupDataAlgorithm());
280 unsigned int decompressed_size = *raw_data_size;
281 int result =
282 BZ2_bzBuffToBuffDecompress(raw_data,
283 &decompressed_size,
284 const_cast<char*>(compressed_data),
285 compressed_data_size,
286 0, 1);
287 if (result == BZ_OK) {
288 *raw_data_size = decompressed_size;
289 }
290 return result;
291 }
292};
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000293#endif
294
295
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296int main(int argc, char** argv) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000297 // By default, log code create information in the snapshot.
298 i::FLAG_log_code = true;
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000299
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300 // Print the usage if an error occurs when parsing the command line
301 // flags or if the help flag is set.
302 int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000303 if (result > 0 || argc != 2 || i::FLAG_help) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000304 ::printf("Usage: %s [flag] ... outfile\n", argv[0]);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000305 i::FlagList::PrintHelp();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000306 return !i::FLAG_help;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000307 }
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000308#ifdef COMPRESS_STARTUP_DATA_BZ2
309 BZip2Decompressor natives_decompressor;
310 int bz2_result = natives_decompressor.Decompress();
311 if (bz2_result != BZ_OK) {
312 fprintf(stderr, "bzip error code: %d\n", bz2_result);
313 exit(1);
314 }
315#endif
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000316 i::Serializer::Enable();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000317 Persistent<Context> context = v8::Context::New();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000318 ASSERT(!context.IsEmpty());
kasper.lund7276f142008-07-30 08:49:36 +0000319 // Make sure all builtin scripts are cached.
320 { HandleScope scope;
321 for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000322 i::Isolate::Current()->bootstrapper()->NativesSourceLookup(i);
kasper.lund7276f142008-07-30 08:49:36 +0000323 }
324 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000325 // If we don't do this then we end up with a stray root pointing at the
326 // context even after we have disposed of the context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000327 HEAP->CollectAllGarbage(true);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000328 i::Object* raw_context = *(v8::Utils::OpenHandle(*context));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000329 context.Dispose();
330 CppByteSink sink(argv[1]);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000331 // This results in a somewhat smaller snapshot, probably because it gets rid
332 // of some things that are cached between garbage collections.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000333 i::StartupSerializer ser(&sink);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000334 ser.SerializeStrongReferences();
335
336 i::PartialSerializer partial_ser(&ser, sink.partial_sink());
337 partial_ser.Serialize(&raw_context);
338
339 ser.SerializeWeakReferences();
340
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000341#ifdef COMPRESS_STARTUP_DATA_BZ2
342 BZip2Compressor compressor;
343 if (!sink.Compress(&compressor))
344 return 1;
345 if (!sink.partial_sink()->Compress(&compressor))
346 return 1;
347#endif
348 sink.WriteSnapshot();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000349 sink.WritePartialSnapshot();
350
351 sink.WriteSpaceUsed(
352 partial_ser.CurrentAllocationAddress(i::NEW_SPACE),
353 partial_ser.CurrentAllocationAddress(i::OLD_POINTER_SPACE),
354 partial_ser.CurrentAllocationAddress(i::OLD_DATA_SPACE),
355 partial_ser.CurrentAllocationAddress(i::CODE_SPACE),
356 partial_ser.CurrentAllocationAddress(i::MAP_SPACE),
357 partial_ser.CurrentAllocationAddress(i::CELL_SPACE),
358 partial_ser.CurrentAllocationAddress(i::LO_SPACE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000359 return 0;
360}