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

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: The version I'll commit Created 9 years, 7 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
« no previous file with comments | « src/list-inl.h ('k') | src/snapshot.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_(), 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
145 class CppByteSink : public PartialSnapshotSink {
146 public:
147 explicit CppByteSink(const char* snapshot_file) {
103 fp_ = i::OS::FOpen(snapshot_file, "wb"); 148 fp_ = i::OS::FOpen(snapshot_file, "wb");
104 if (fp_ == NULL) { 149 if (fp_ == NULL) {
105 i::PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file); 150 i::PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file);
106 exit(1); 151 exit(1);
107 } 152 }
108 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); 153 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n");
109 fprintf(fp_, "#include \"v8.h\"\n"); 154 fprintf(fp_, "#include \"v8.h\"\n");
110 fprintf(fp_, "#include \"platform.h\"\n\n"); 155 fprintf(fp_, "#include \"platform.h\"\n\n");
111 fprintf(fp_, "#include \"snapshot.h\"\n\n"); 156 fprintf(fp_, "#include \"snapshot.h\"\n\n");
112 fprintf(fp_, "namespace v8 {\nnamespace internal {\n\n"); 157 fprintf(fp_, "namespace v8 {\nnamespace internal {\n\n");
113 fprintf(fp_, "const byte Snapshot::data_[] = {"); 158 fprintf(fp_, "const byte Snapshot::data_[] = {");
114 } 159 }
115 160
116 virtual ~CppByteSink() { 161 virtual ~CppByteSink() {
117 fprintf(fp_, "const int Snapshot::size_ = %d;\n\n", bytes_written_); 162 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
118 fprintf(fp_, "} } // namespace v8::internal\n"); 174 fprintf(fp_, "} } // namespace v8::internal\n");
119 fclose(fp_); 175 fclose(fp_);
120 } 176 }
121 177
122 void WriteSpaceUsed( 178 void WriteSpaceUsed(
123 int new_space_used, 179 int new_space_used,
124 int pointer_space_used, 180 int pointer_space_used,
125 int data_space_used, 181 int data_space_used,
126 int code_space_used, 182 int code_space_used,
127 int map_space_used, 183 int map_space_used,
128 int cell_space_used, 184 int cell_space_used,
129 int large_space_used) { 185 int large_space_used) {
130 fprintf(fp_, "};\n\n");
131 fprintf(fp_, "const int Snapshot::new_space_used_ = %d;\n", new_space_used); 186 fprintf(fp_, "const int Snapshot::new_space_used_ = %d;\n", new_space_used);
132 fprintf(fp_, 187 fprintf(fp_,
133 "const int Snapshot::pointer_space_used_ = %d;\n", 188 "const int Snapshot::pointer_space_used_ = %d;\n",
134 pointer_space_used); 189 pointer_space_used);
135 fprintf(fp_, 190 fprintf(fp_,
136 "const int Snapshot::data_space_used_ = %d;\n", 191 "const int Snapshot::data_space_used_ = %d;\n",
137 data_space_used); 192 data_space_used);
138 fprintf(fp_, 193 fprintf(fp_,
139 "const int Snapshot::code_space_used_ = %d;\n", 194 "const int Snapshot::code_space_used_ = %d;\n",
140 code_space_used); 195 code_space_used);
141 fprintf(fp_, "const int Snapshot::map_space_used_ = %d;\n", map_space_used); 196 fprintf(fp_, "const int Snapshot::map_space_used_ = %d;\n", map_space_used);
142 fprintf(fp_, 197 fprintf(fp_,
143 "const int Snapshot::cell_space_used_ = %d;\n", 198 "const int Snapshot::cell_space_used_ = %d;\n",
144 cell_space_used); 199 cell_space_used);
145 fprintf(fp_, 200 fprintf(fp_,
146 "const int Snapshot::large_space_used_ = %d;\n", 201 "const int Snapshot::large_space_used_ = %d;\n",
147 large_space_used); 202 large_space_used);
148 } 203 }
149 204
150 void WritePartialSnapshot() { 205 void WritePartialSnapshot() {
151 int length = partial_sink_.Position(); 206 int length = partial_sink_.Position();
152 fprintf(fp_, "};\n\n"); 207 fprintf(fp_, "};\n\n");
153 fprintf(fp_, "const int Snapshot::context_size_ = %d;\n", length); 208 fprintf(fp_, "const int Snapshot::context_size_ = %d;\n", length);
209 fprintf(fp_,
210 "const int Snapshot::context_raw_size_ = %d;\n",
211 partial_sink_.raw_size());
154 fprintf(fp_, "const byte Snapshot::context_data_[] = {\n"); 212 fprintf(fp_, "const byte Snapshot::context_data_[] = {\n");
155 for (int j = 0; j < length; j++) { 213 partial_sink_.Print(fp_);
156 if ((j & 0x1f) == 0x1f) { 214 fprintf(fp_, "};\n\n");
157 fprintf(fp_, "\n"); 215 #ifdef COMPRESS_STARTUP_DATA_BZ2
158 } 216 fprintf(fp_, "const byte* Snapshot::context_raw_data_ = NULL;\n");
159 char byte = partial_sink_.at(j); 217 #else
160 if (j != 0) { 218 fprintf(fp_, "const byte* Snapshot::context_raw_data_ ="
161 fprintf(fp_, ","); 219 " Snapshot::context_data_;\n");
162 } 220 #endif
163 fprintf(fp_, "%d", byte);
164 }
165 } 221 }
166 222
167 virtual void Put(int byte, const char* description) { 223 void WriteSnapshot() {
168 if (bytes_written_ != 0) { 224 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 } 225 }
177 226
178 virtual int Position() { 227 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 228
200 private: 229 private:
201 FILE* fp_; 230 FILE* fp_;
202 int bytes_written_;
203 PartialSnapshotSink partial_sink_; 231 PartialSnapshotSink partial_sink_;
204 }; 232 };
205 233
206 234
235 #ifdef COMPRESS_STARTUP_DATA_BZ2
236 class BZip2Compressor : public Compressor {
237 public:
238 BZip2Compressor() : output_(NULL) {}
239 virtual ~BZip2Compressor() {
240 delete output_;
241 }
242 virtual bool Compress(i::Vector<char> input) {
243 delete output_;
244 output_ = new i::ScopedVector<char>((input.length() * 101) / 100 + 1000);
245 unsigned int output_length_ = output_->length();
246 int result = BZ2_bzBuffToBuffCompress(output_->start(), &output_length_,
247 input.start(), input.length(),
248 9, 1, 0);
249 if (result == BZ_OK) {
250 output_->Truncate(output_length_);
251 return true;
252 } else {
253 fprintf(stderr, "bzlib error code: %d\n", result);
254 return false;
255 }
256 }
257 virtual i::Vector<char>* output() { return output_; }
258
259 private:
260 i::ScopedVector<char>* output_;
261 };
262 #endif
263
264
207 int main(int argc, char** argv) { 265 int main(int argc, char** argv) {
208 #ifdef ENABLE_LOGGING_AND_PROFILING 266 #ifdef ENABLE_LOGGING_AND_PROFILING
209 // By default, log code create information in the snapshot. 267 // By default, log code create information in the snapshot.
210 i::FLAG_log_code = true; 268 i::FLAG_log_code = true;
211 #endif 269 #endif
212 // Print the usage if an error occurs when parsing the command line 270 // Print the usage if an error occurs when parsing the command line
213 // flags or if the help flag is set. 271 // flags or if the help flag is set.
214 int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true); 272 int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
215 if (result > 0 || argc != 2 || i::FLAG_help) { 273 if (result > 0 || argc != 2 || i::FLAG_help) {
216 ::printf("Usage: %s [flag] ... outfile\n", argv[0]); 274 ::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 293 // This results in a somewhat smaller snapshot, probably because it gets rid
236 // of some things that are cached between garbage collections. 294 // of some things that are cached between garbage collections.
237 i::StartupSerializer ser(&sink); 295 i::StartupSerializer ser(&sink);
238 ser.SerializeStrongReferences(); 296 ser.SerializeStrongReferences();
239 297
240 i::PartialSerializer partial_ser(&ser, sink.partial_sink()); 298 i::PartialSerializer partial_ser(&ser, sink.partial_sink());
241 partial_ser.Serialize(&raw_context); 299 partial_ser.Serialize(&raw_context);
242 300
243 ser.SerializeWeakReferences(); 301 ser.SerializeWeakReferences();
244 302
303 #ifdef COMPRESS_STARTUP_DATA_BZ2
304 BZip2Compressor compressor;
305 if (!sink.Compress(&compressor))
306 return 1;
307 if (!sink.partial_sink()->Compress(&compressor))
308 return 1;
309 #endif
310 sink.WriteSnapshot();
245 sink.WritePartialSnapshot(); 311 sink.WritePartialSnapshot();
246 312
247 sink.WriteSpaceUsed( 313 sink.WriteSpaceUsed(
248 partial_ser.CurrentAllocationAddress(i::NEW_SPACE), 314 partial_ser.CurrentAllocationAddress(i::NEW_SPACE),
249 partial_ser.CurrentAllocationAddress(i::OLD_POINTER_SPACE), 315 partial_ser.CurrentAllocationAddress(i::OLD_POINTER_SPACE),
250 partial_ser.CurrentAllocationAddress(i::OLD_DATA_SPACE), 316 partial_ser.CurrentAllocationAddress(i::OLD_DATA_SPACE),
251 partial_ser.CurrentAllocationAddress(i::CODE_SPACE), 317 partial_ser.CurrentAllocationAddress(i::CODE_SPACE),
252 partial_ser.CurrentAllocationAddress(i::MAP_SPACE), 318 partial_ser.CurrentAllocationAddress(i::MAP_SPACE),
253 partial_ser.CurrentAllocationAddress(i::CELL_SPACE), 319 partial_ser.CurrentAllocationAddress(i::CELL_SPACE),
254 partial_ser.CurrentAllocationAddress(i::LO_SPACE)); 320 partial_ser.CurrentAllocationAddress(i::LO_SPACE));
255 return 0; 321 return 0;
256 } 322 }
OLDNEW
« no previous file with comments | « src/list-inl.h ('k') | src/snapshot.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698