blob: dd491166bd470b65eee30c8c8d562dffd9f8642f [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_; }
139 private:
140 i::List<char> data_;
141 int raw_size_;
142};
143
144
145class CppByteSink : public PartialSnapshotSink {
146 public:
147 explicit CppByteSink(const char* snapshot_file) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000148 fp_ = i::OS::FOpen(snapshot_file, "wb");
149 if (fp_ == NULL) {
150 i::PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file);
151 exit(1);
152 }
153 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n");
154 fprintf(fp_, "#include \"v8.h\"\n");
155 fprintf(fp_, "#include \"platform.h\"\n\n");
156 fprintf(fp_, "#include \"snapshot.h\"\n\n");
157 fprintf(fp_, "namespace v8 {\nnamespace internal {\n\n");
158 fprintf(fp_, "const byte Snapshot::data_[] = {");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000160
161 virtual ~CppByteSink() {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000162 fprintf(fp_, "const int Snapshot::size_ = %d;\n", Position());
163#ifdef COMPRESS_STARTUP_DATA_BZ2
164 fprintf(fp_, "const byte* Snapshot::raw_data_ = NULL;\n");
165 fprintf(fp_,
166 "const int Snapshot::raw_size_ = %d;\n\n",
167 raw_size());
168#else
169 fprintf(fp_,
170 "const byte* Snapshot::raw_data_ = Snapshot::data_;\n");
171 fprintf(fp_,
172 "const int Snapshot::raw_size_ = Snapshot::size_;\n\n");
173#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000174 fprintf(fp_, "} } // namespace v8::internal\n");
175 fclose(fp_);
176 }
177
178 void WriteSpaceUsed(
179 int new_space_used,
180 int pointer_space_used,
181 int data_space_used,
182 int code_space_used,
183 int map_space_used,
184 int cell_space_used,
185 int large_space_used) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000186 fprintf(fp_, "const int Snapshot::new_space_used_ = %d;\n", new_space_used);
187 fprintf(fp_,
188 "const int Snapshot::pointer_space_used_ = %d;\n",
189 pointer_space_used);
190 fprintf(fp_,
191 "const int Snapshot::data_space_used_ = %d;\n",
192 data_space_used);
193 fprintf(fp_,
194 "const int Snapshot::code_space_used_ = %d;\n",
195 code_space_used);
196 fprintf(fp_, "const int Snapshot::map_space_used_ = %d;\n", map_space_used);
197 fprintf(fp_,
198 "const int Snapshot::cell_space_used_ = %d;\n",
199 cell_space_used);
200 fprintf(fp_,
201 "const int Snapshot::large_space_used_ = %d;\n",
202 large_space_used);
203 }
204
205 void WritePartialSnapshot() {
206 int length = partial_sink_.Position();
207 fprintf(fp_, "};\n\n");
208 fprintf(fp_, "const int Snapshot::context_size_ = %d;\n", length);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000209#ifdef COMPRESS_STARTUP_DATA_BZ2
210 fprintf(fp_,
211 "const int Snapshot::context_raw_size_ = %d;\n",
212 partial_sink_.raw_size());
213#else
214 fprintf(fp_,
215 "const int Snapshot::context_raw_size_ = "
216 "Snapshot::context_size_;\n");
217#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000218 fprintf(fp_, "const byte Snapshot::context_data_[] = {\n");
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000219 partial_sink_.Print(fp_);
220 fprintf(fp_, "};\n\n");
221#ifdef COMPRESS_STARTUP_DATA_BZ2
222 fprintf(fp_, "const byte* Snapshot::context_raw_data_ = NULL;\n");
223#else
224 fprintf(fp_, "const byte* Snapshot::context_raw_data_ ="
225 " Snapshot::context_data_;\n");
226#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000227 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000228
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000229 void WriteSnapshot() {
230 Print(fp_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000231 }
232
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000233 PartialSnapshotSink* partial_sink() { return &partial_sink_; }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000234
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000235 private:
236 FILE* fp_;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000237 PartialSnapshotSink partial_sink_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000238};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239
240
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000241#ifdef COMPRESS_STARTUP_DATA_BZ2
242class BZip2Compressor : public Compressor {
243 public:
244 BZip2Compressor() : output_(NULL) {}
245 virtual ~BZip2Compressor() {
246 delete output_;
247 }
248 virtual bool Compress(i::Vector<char> input) {
249 delete output_;
250 output_ = new i::ScopedVector<char>((input.length() * 101) / 100 + 1000);
251 unsigned int output_length_ = output_->length();
252 int result = BZ2_bzBuffToBuffCompress(output_->start(), &output_length_,
253 input.start(), input.length(),
254 9, 1, 0);
255 if (result == BZ_OK) {
256 output_->Truncate(output_length_);
257 return true;
258 } else {
259 fprintf(stderr, "bzlib error code: %d\n", result);
260 return false;
261 }
262 }
263 virtual i::Vector<char>* output() { return output_; }
264
265 private:
266 i::ScopedVector<char>* output_;
267};
268#endif
269
270
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271int main(int argc, char** argv) {
272#ifdef ENABLE_LOGGING_AND_PROFILING
273 // By default, log code create information in the snapshot.
274 i::FLAG_log_code = true;
275#endif
276 // Print the usage if an error occurs when parsing the command line
277 // flags or if the help flag is set.
278 int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000279 if (result > 0 || argc != 2 || i::FLAG_help) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280 ::printf("Usage: %s [flag] ... outfile\n", argv[0]);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000281 i::FlagList::PrintHelp();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000282 return !i::FLAG_help;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283 }
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000284 i::Serializer::Enable();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000285 Persistent<Context> context = v8::Context::New();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000286 ASSERT(!context.IsEmpty());
kasper.lund7276f142008-07-30 08:49:36 +0000287 // Make sure all builtin scripts are cached.
288 { HandleScope scope;
289 for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000290 i::Isolate::Current()->bootstrapper()->NativesSourceLookup(i);
kasper.lund7276f142008-07-30 08:49:36 +0000291 }
292 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000293 // If we don't do this then we end up with a stray root pointing at the
294 // context even after we have disposed of the context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000295 HEAP->CollectAllGarbage(true);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000296 i::Object* raw_context = *(v8::Utils::OpenHandle(*context));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000297 context.Dispose();
298 CppByteSink sink(argv[1]);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000299 // This results in a somewhat smaller snapshot, probably because it gets rid
300 // of some things that are cached between garbage collections.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000301 i::StartupSerializer ser(&sink);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000302 ser.SerializeStrongReferences();
303
304 i::PartialSerializer partial_ser(&ser, sink.partial_sink());
305 partial_ser.Serialize(&raw_context);
306
307 ser.SerializeWeakReferences();
308
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000309#ifdef COMPRESS_STARTUP_DATA_BZ2
310 BZip2Compressor compressor;
311 if (!sink.Compress(&compressor))
312 return 1;
313 if (!sink.partial_sink()->Compress(&compressor))
314 return 1;
315#endif
316 sink.WriteSnapshot();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000317 sink.WritePartialSnapshot();
318
319 sink.WriteSpaceUsed(
320 partial_ser.CurrentAllocationAddress(i::NEW_SPACE),
321 partial_ser.CurrentAllocationAddress(i::OLD_POINTER_SPACE),
322 partial_ser.CurrentAllocationAddress(i::OLD_DATA_SPACE),
323 partial_ser.CurrentAllocationAddress(i::CODE_SPACE),
324 partial_ser.CurrentAllocationAddress(i::MAP_SPACE),
325 partial_ser.CurrentAllocationAddress(i::CELL_SPACE),
326 partial_ser.CurrentAllocationAddress(i::LO_SPACE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327 return 0;
328}