Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(536)

Side by Side Diff: src/mksnapshot.cc

Issue 6901090: Add support for startup data (snapshot) compression. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution. 11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its 12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived 13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission. 14 // from this software without specific prior written permission.
15 // 15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #ifdef COMPRESS_STARTUP_DATA_BZ2
29 #include <bzlib.h>
30 #endif
28 #include <signal.h> 31 #include <signal.h>
29 #include <string> 32 #include <string>
30 #include <map> 33 #include <map>
31 34
32 #include "v8.h" 35 #include "v8.h"
33 36
34 #include "bootstrapper.h" 37 #include "bootstrapper.h"
35 #include "natives.h" 38 #include "natives.h"
36 #include "platform.h" 39 #include "platform.h"
37 #include "serialize.h" 40 #include "serialize.h"
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 // We statically allocate a set of local counters to be used if we 91 // We statically allocate a set of local counters to be used if we
89 // don't want to store the stats in a memory-mapped file 92 // don't want to store the stats in a memory-mapped file
90 static CounterCollection local_counters; 93 static CounterCollection local_counters;
91 94
92 95
93 typedef std::map<std::string, int*> CounterMap; 96 typedef std::map<std::string, int*> CounterMap;
94 typedef std::map<std::string, int*>::iterator CounterMapIterator; 97 typedef std::map<std::string, int*>::iterator CounterMapIterator;
95 static CounterMap counter_table_; 98 static CounterMap counter_table_;
96 99
97 100
98 class CppByteSink : public i::SnapshotByteSink { 101 class Compressor {
99 public: 102 public:
100 explicit CppByteSink(const char* snapshot_file) 103 virtual ~Compressor() {}
101 : bytes_written_(0), 104 virtual bool Compress(i::Vector<char> input) = 0;
102 partial_sink_(this) { 105 virtual i::Vector<char>* output() = 0;
106 };
107
108
109 class PartialSnapshotSink : public i::SnapshotByteSink {
110 public:
111 PartialSnapshotSink() : data_(), decompressed_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) {
Søren Thygesen Gjesse 2011/04/29 06:50:29 ASSERT_EQ(-1, decompressed_size_)?
mnaganov (inactive) 2011/04/29 12:07:58 Done.
131 decompressed_size_ = data_.length();
132 if (!compressor->Compress(data_.ToVector())) return false;
133 data_.Clear();
134 data_.AddAll(*compressor->output());
135 return false;
136 }
137 int decompressed_size() { return decompressed_size_; }
138 private:
139 i::List<char> data_;
140 int decompressed_size_;
141 };
142
143
144 class CppByteSink : public PartialSnapshotSink {
145 public:
146 explicit CppByteSink(const char* snapshot_file) {
103 fp_ = i::OS::FOpen(snapshot_file, "wb"); 147 fp_ = i::OS::FOpen(snapshot_file, "wb");
104 if (fp_ == NULL) { 148 if (fp_ == NULL) {
105 i::PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file); 149 i::PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file);
106 exit(1); 150 exit(1);
107 } 151 }
108 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); 152 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n");
109 fprintf(fp_, "#include \"v8.h\"\n"); 153 fprintf(fp_, "#include \"v8.h\"\n");
110 fprintf(fp_, "#include \"platform.h\"\n\n"); 154 fprintf(fp_, "#include \"platform.h\"\n\n");
111 fprintf(fp_, "#include \"snapshot.h\"\n\n"); 155 fprintf(fp_, "#include \"snapshot.h\"\n\n");
112 fprintf(fp_, "namespace v8 {\nnamespace internal {\n\n"); 156 fprintf(fp_, "namespace v8 {\nnamespace internal {\n\n");
113 fprintf(fp_, "const byte Snapshot::data_[] = {"); 157 fprintf(fp_, "const byte Snapshot::data_[] = {");
114 } 158 }
115 159
116 virtual ~CppByteSink() { 160 virtual ~CppByteSink() {
117 fprintf(fp_, "const int Snapshot::size_ = %d;\n\n", bytes_written_); 161 fprintf(fp_, "const int Snapshot::size_ = %d;\n", Position());
162 #ifdef COMPRESS_STARTUP_DATA_BZ2
163 fprintf(fp_, "const byte* Snapshot::decompressed_data_ = NULL;\n");
164 fprintf(fp_,
165 "const int Snapshot::decompressed_size_ = %d;\n\n",
166 decompressed_size());
167 #else
168 fprintf(fp_,
169 "const byte* Snapshot::decompressed_data_ = Snapshot::data_;\n");
170 fprintf(fp_,
171 "const int Snapshot::decompressed_size_ = Snapshot::size_;\n\n");
172 #endif
118 fprintf(fp_, "} } // namespace v8::internal\n"); 173 fprintf(fp_, "} } // namespace v8::internal\n");
119 fclose(fp_); 174 fclose(fp_);
120 } 175 }
121 176
122 void WriteSpaceUsed( 177 void WriteSpaceUsed(
123 int new_space_used, 178 int new_space_used,
124 int pointer_space_used, 179 int pointer_space_used,
125 int data_space_used, 180 int data_space_used,
126 int code_space_used, 181 int code_space_used,
127 int map_space_used, 182 int map_space_used,
128 int cell_space_used, 183 int cell_space_used,
129 int large_space_used) { 184 int large_space_used) {
130 fprintf(fp_, "};\n\n");
131 fprintf(fp_, "const int Snapshot::new_space_used_ = %d;\n", new_space_used); 185 fprintf(fp_, "const int Snapshot::new_space_used_ = %d;\n", new_space_used);
132 fprintf(fp_, 186 fprintf(fp_,
133 "const int Snapshot::pointer_space_used_ = %d;\n", 187 "const int Snapshot::pointer_space_used_ = %d;\n",
134 pointer_space_used); 188 pointer_space_used);
135 fprintf(fp_, 189 fprintf(fp_,
136 "const int Snapshot::data_space_used_ = %d;\n", 190 "const int Snapshot::data_space_used_ = %d;\n",
137 data_space_used); 191 data_space_used);
138 fprintf(fp_, 192 fprintf(fp_,
139 "const int Snapshot::code_space_used_ = %d;\n", 193 "const int Snapshot::code_space_used_ = %d;\n",
140 code_space_used); 194 code_space_used);
141 fprintf(fp_, "const int Snapshot::map_space_used_ = %d;\n", map_space_used); 195 fprintf(fp_, "const int Snapshot::map_space_used_ = %d;\n", map_space_used);
142 fprintf(fp_, 196 fprintf(fp_,
143 "const int Snapshot::cell_space_used_ = %d;\n", 197 "const int Snapshot::cell_space_used_ = %d;\n",
144 cell_space_used); 198 cell_space_used);
145 fprintf(fp_, 199 fprintf(fp_,
146 "const int Snapshot::large_space_used_ = %d;\n", 200 "const int Snapshot::large_space_used_ = %d;\n",
147 large_space_used); 201 large_space_used);
148 } 202 }
149 203
150 void WritePartialSnapshot() { 204 void WritePartialSnapshot() {
151 int length = partial_sink_.Position(); 205 int length = partial_sink_.Position();
152 fprintf(fp_, "};\n\n"); 206 fprintf(fp_, "};\n\n");
153 fprintf(fp_, "const int Snapshot::context_size_ = %d;\n", length); 207 fprintf(fp_, "const int Snapshot::context_size_ = %d;\n", length);
208 fprintf(fp_,
209 "const int Snapshot::context_decompressed_size_ = %d;\n",
210 partial_sink_.decompressed_size());
154 fprintf(fp_, "const byte Snapshot::context_data_[] = {\n"); 211 fprintf(fp_, "const byte Snapshot::context_data_[] = {\n");
155 for (int j = 0; j < length; j++) { 212 partial_sink_.Print(fp_);
156 if ((j & 0x1f) == 0x1f) { 213 fprintf(fp_, "};\n\n");
157 fprintf(fp_, "\n"); 214 #ifdef COMPRESS_STARTUP_DATA_BZ2
158 } 215 fprintf(fp_, "const byte* Snapshot::context_decompressed_data_ = NULL;\n");
159 char byte = partial_sink_.at(j); 216 #else
160 if (j != 0) { 217 fprintf(fp_, "const byte* Snapshot::context_decompressed_data_ ="
161 fprintf(fp_, ","); 218 " Snapshot::context_data_;\n");
162 } 219 #endif
163 fprintf(fp_, "%d", byte);
164 }
165 } 220 }
166 221
167 virtual void Put(int byte, const char* description) { 222 void WriteSnapshot() {
168 if (bytes_written_ != 0) { 223 Print(fp_);
169 fprintf(fp_, ",");
170 }
171 fprintf(fp_, "%d", byte);
172 bytes_written_++;
173 if ((bytes_written_ & 0x1f) == 0) {
174 fprintf(fp_, "\n");
175 }
176 } 224 }
177 225
178 virtual int Position() { 226 PartialSnapshotSink* partial_sink() { return &partial_sink_; }
179 return bytes_written_;
180 }
181
182 i::SnapshotByteSink* partial_sink() { return &partial_sink_; }
183
184 class PartialSnapshotSink : public i::SnapshotByteSink {
185 public:
186 explicit PartialSnapshotSink(CppByteSink* parent)
187 : parent_(parent),
188 data_() { }
189 virtual ~PartialSnapshotSink() { data_.Free(); }
190 virtual void Put(int byte, const char* description) {
191 data_.Add(byte);
192 }
193 virtual int Position() { return data_.length(); }
194 char at(int i) { return data_[i]; }
195 private:
196 CppByteSink* parent_;
197 i::List<char> data_;
198 };
199 227
200 private: 228 private:
201 FILE* fp_; 229 FILE* fp_;
202 int bytes_written_;
203 PartialSnapshotSink partial_sink_; 230 PartialSnapshotSink partial_sink_;
204 }; 231 };
205 232
206 233
234 #ifdef COMPRESS_STARTUP_DATA_BZ2
235 class BZip2Compressor : public Compressor {
236 public:
237 BZip2Compressor() : output_(NULL) {}
238 virtual ~BZip2Compressor() {
239 delete output_;
240 }
241 virtual bool Compress(i::Vector<char> input) {
242 output_ = new i::ScopedVector<char>((input.length() * 101) / 100 + 1000);
243 unsigned int output_length_;
244 int result = BZ2_bzBuffToBuffCompress(output_->start(), &output_length_,
245 input.start(), input.length(),
246 9, 1, 0);
247 if (result == BZ_OK) {
248 output_->Truncate(output_length_);
249 return true;
250 } else {
251 fprintf(stderr, "bzlib error code: %d\n", result);
252 return false;
253 }
254 }
255 virtual i::Vector<char>* output() { return output_; }
256
257 private:
258 i::ScopedVector<char>* output_;
259 };
260 #endif
261
262
207 int main(int argc, char** argv) { 263 int main(int argc, char** argv) {
208 #ifdef ENABLE_LOGGING_AND_PROFILING 264 #ifdef ENABLE_LOGGING_AND_PROFILING
209 // By default, log code create information in the snapshot. 265 // By default, log code create information in the snapshot.
210 i::FLAG_log_code = true; 266 i::FLAG_log_code = true;
211 #endif 267 #endif
212 // Print the usage if an error occurs when parsing the command line 268 // Print the usage if an error occurs when parsing the command line
213 // flags or if the help flag is set. 269 // flags or if the help flag is set.
214 int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true); 270 int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
215 if (result > 0 || argc != 2 || i::FLAG_help) { 271 if (result > 0 || argc != 2 || i::FLAG_help) {
216 ::printf("Usage: %s [flag] ... outfile\n", argv[0]); 272 ::printf("Usage: %s [flag] ... outfile\n", argv[0]);
(...skipping 18 matching lines...) Expand all
235 // This results in a somewhat smaller snapshot, probably because it gets rid 291 // This results in a somewhat smaller snapshot, probably because it gets rid
236 // of some things that are cached between garbage collections. 292 // of some things that are cached between garbage collections.
237 i::StartupSerializer ser(&sink); 293 i::StartupSerializer ser(&sink);
238 ser.SerializeStrongReferences(); 294 ser.SerializeStrongReferences();
239 295
240 i::PartialSerializer partial_ser(&ser, sink.partial_sink()); 296 i::PartialSerializer partial_ser(&ser, sink.partial_sink());
241 partial_ser.Serialize(&raw_context); 297 partial_ser.Serialize(&raw_context);
242 298
243 ser.SerializeWeakReferences(); 299 ser.SerializeWeakReferences();
244 300
301 #ifdef COMPRESS_STARTUP_DATA_BZ2
302 BZip2Compressor compressor;
303 sink.Compress(&compressor);
304 sink.partial_sink()->Compress(&compressor);
305 #endif
306 sink.WriteSnapshot();
245 sink.WritePartialSnapshot(); 307 sink.WritePartialSnapshot();
246 308
247 sink.WriteSpaceUsed( 309 sink.WriteSpaceUsed(
248 partial_ser.CurrentAllocationAddress(i::NEW_SPACE), 310 partial_ser.CurrentAllocationAddress(i::NEW_SPACE),
249 partial_ser.CurrentAllocationAddress(i::OLD_POINTER_SPACE), 311 partial_ser.CurrentAllocationAddress(i::OLD_POINTER_SPACE),
250 partial_ser.CurrentAllocationAddress(i::OLD_DATA_SPACE), 312 partial_ser.CurrentAllocationAddress(i::OLD_DATA_SPACE),
251 partial_ser.CurrentAllocationAddress(i::CODE_SPACE), 313 partial_ser.CurrentAllocationAddress(i::CODE_SPACE),
252 partial_ser.CurrentAllocationAddress(i::MAP_SPACE), 314 partial_ser.CurrentAllocationAddress(i::MAP_SPACE),
253 partial_ser.CurrentAllocationAddress(i::CELL_SPACE), 315 partial_ser.CurrentAllocationAddress(i::CELL_SPACE),
254 partial_ser.CurrentAllocationAddress(i::LO_SPACE)); 316 partial_ser.CurrentAllocationAddress(i::LO_SPACE));
255 return 0; 317 return 0;
256 } 318 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698