blob: 441ae181fea0b2646b540373a7f668c81161f021 [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
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000028// To avoid warnings from <map> on windows we disable exceptions.
29#define _HAS_EXCEPTIONS 0
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000030#include <signal.h>
31#include <string>
32#include <map>
33
34#include "v8.h"
35
36#include "bootstrapper.h"
37#include "natives.h"
38#include "platform.h"
39#include "serialize.h"
40
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041// use explicit namespace to avoid clashing with types in namespace v8
42namespace i = v8::internal;
43using 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;
92static CounterCollection* counters = &local_counters;
93
94
ager@chromium.orga74f0da2008-12-03 16:05:52 +000095typedef std::map<std::string, int*> CounterMap;
96typedef std::map<std::string, int*>::iterator CounterMapIterator;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097static CounterMap counter_table_;
98
99// Callback receiver when v8 has a counter to track.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000100static int* counter_callback(const char* name) {
101 std::string counter = name;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000102 // See if this counter name is already known.
103 if (counter_table_.find(counter) != counter_table_.end())
104 return counter_table_[counter];
105
106 Counter* ctr = counters->GetNextCounter();
107 if (ctr == NULL) return NULL;
108 int* ptr = ctr->Bind(name);
109 counter_table_[counter] = ptr;
110 return ptr;
111}
112
113
114// Write C++ code that defines Snapshot::snapshot_ to contain the snapshot
115// to the file given by filename. Only the first size chars are written.
116static int WriteInternalSnapshotToFile(const char* filename,
117 const char* str,
118 int size) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000119 FILE* f = i::OS::FOpen(filename, "wb");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000120 if (f == NULL) {
121 i::OS::PrintError("Cannot open file %s for reading.\n", filename);
122 return 0;
123 }
124 fprintf(f, "// Autogenerated snapshot file. Do not edit.\n\n");
125 fprintf(f, "#include \"v8.h\"\n");
126 fprintf(f, "#include \"platform.h\"\n\n");
127 fprintf(f, "#include \"snapshot.h\"\n\n");
128 fprintf(f, "namespace v8 {\nnamespace internal {\n\n");
kasper.lund44510672008-07-25 07:37:58 +0000129 fprintf(f, "const char Snapshot::data_[] = {");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000130 int written = 0;
131 written += fprintf(f, "%i", str[0]);
132 for (int i = 1; i < size; ++i) {
133 written += fprintf(f, ",%i", str[i]);
134 // The following is needed to keep the line length low on Visual C++:
135 if (i % 512 == 0) fprintf(f, "\n");
136 }
137 fprintf(f, "};\n\n");
138 fprintf(f, "int Snapshot::size_ = %d;\n\n", size);
139 fprintf(f, "} } // namespace v8::internal\n");
140 fclose(f);
141 return written;
142}
143
144
145int main(int argc, char** argv) {
146#ifdef ENABLE_LOGGING_AND_PROFILING
147 // By default, log code create information in the snapshot.
148 i::FLAG_log_code = true;
149#endif
150 // Print the usage if an error occurs when parsing the command line
151 // flags or if the help flag is set.
152 int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000153 if (result > 0 || argc != 2 || i::FLAG_help) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154 ::printf("Usage: %s [flag] ... outfile\n", argv[0]);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000155 i::FlagList::PrintHelp();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000156 return !i::FLAG_help;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000157 }
158
159 v8::V8::SetCounterFunction(counter_callback);
160 v8::HandleScope scope;
161
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000162 const int kExtensionCount = 1;
163 const char* extension_list[kExtensionCount] = { "v8/gc" };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164 v8::ExtensionConfiguration extensions(kExtensionCount, extension_list);
165 v8::Context::New(&extensions);
166
kasper.lund7276f142008-07-30 08:49:36 +0000167 // Make sure all builtin scripts are cached.
168 { HandleScope scope;
169 for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) {
170 i::Bootstrapper::NativesSourceLookup(i);
171 }
172 }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000173 // Get rid of unreferenced scripts with a global GC.
174 i::Heap::CollectAllGarbage();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000175 i::Serializer ser;
176 ser.Serialize();
177 char* str;
178 int len;
179 ser.Finalize(&str, &len);
180
181 WriteInternalSnapshotToFile(argv[1], str, len);
182
183 i::DeleteArray(str);
184
185 return 0;
186}