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