| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <errno.h> | |
| 6 #include <signal.h> | |
| 7 #include <stdio.h> | |
| 8 | |
| 9 #include "src/v8.h" | 5 #include "src/v8.h" |
| 10 | 6 |
| 11 #include "include/libplatform/libplatform.h" | |
| 12 #include "src/assembler.h" | |
| 13 #include "src/base/platform/platform.h" | |
| 14 #include "src/bootstrapper.h" | |
| 15 #include "src/flags.h" | |
| 16 #include "src/list.h" | |
| 17 #include "src/natives.h" | |
| 18 #include "src/serialize.h" | |
| 19 | |
| 20 | |
| 21 using namespace v8; | |
| 22 | |
| 23 | |
| 24 class Compressor { | |
| 25 public: | |
| 26 virtual ~Compressor() {} | |
| 27 virtual bool Compress(i::Vector<i::byte> input) = 0; | |
| 28 virtual i::Vector<i::byte>* output() = 0; | |
| 29 }; | |
| 30 | |
| 31 | |
| 32 class SnapshotWriter { | |
| 33 public: | |
| 34 explicit SnapshotWriter(const char* snapshot_file) | |
| 35 : fp_(GetFileDescriptorOrDie(snapshot_file)) | |
| 36 , raw_file_(NULL) | |
| 37 , raw_context_file_(NULL) | |
| 38 , startup_blob_file_(NULL) | |
| 39 , compressor_(NULL) { | |
| 40 } | |
| 41 | |
| 42 ~SnapshotWriter() { | |
| 43 fclose(fp_); | |
| 44 if (raw_file_) fclose(raw_file_); | |
| 45 if (raw_context_file_) fclose(raw_context_file_); | |
| 46 if (startup_blob_file_) fclose(startup_blob_file_); | |
| 47 } | |
| 48 | |
| 49 void SetCompressor(Compressor* compressor) { | |
| 50 compressor_ = compressor; | |
| 51 } | |
| 52 | |
| 53 void SetRawFiles(const char* raw_file, const char* raw_context_file) { | |
| 54 raw_file_ = GetFileDescriptorOrDie(raw_file); | |
| 55 raw_context_file_ = GetFileDescriptorOrDie(raw_context_file); | |
| 56 } | |
| 57 | |
| 58 void SetStartupBlobFile(const char* startup_blob_file) { | |
| 59 if (startup_blob_file != NULL) | |
| 60 startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file); | |
| 61 } | |
| 62 | |
| 63 void WriteSnapshot(const i::List<i::byte>& snapshot_data, | |
| 64 const i::Serializer& serializer, | |
| 65 const i::List<i::byte>& context_snapshot_data, | |
| 66 const i::Serializer& context_serializer) const { | |
| 67 WriteSnapshotFile(snapshot_data, serializer, | |
| 68 context_snapshot_data, context_serializer); | |
| 69 MaybeWriteStartupBlob(snapshot_data, serializer, | |
| 70 context_snapshot_data, context_serializer); | |
| 71 } | |
| 72 | |
| 73 private: | |
| 74 void MaybeWriteStartupBlob(const i::List<i::byte>& snapshot_data, | |
| 75 const i::Serializer& serializer, | |
| 76 const i::List<i::byte>& context_snapshot_data, | |
| 77 const i::Serializer& context_serializer) const { | |
| 78 if (!startup_blob_file_) return; | |
| 79 | |
| 80 i::SnapshotByteSink sink; | |
| 81 | |
| 82 int spaces[] = {i::NEW_SPACE, i::OLD_POINTER_SPACE, | |
| 83 i::OLD_DATA_SPACE, i::CODE_SPACE, | |
| 84 i::MAP_SPACE, i::CELL_SPACE, | |
| 85 i::PROPERTY_CELL_SPACE, i::LO_SPACE}; | |
| 86 | |
| 87 i::byte* snapshot_bytes = snapshot_data.begin(); | |
| 88 sink.PutBlob(snapshot_bytes, snapshot_data.length(), "snapshot"); | |
| 89 for (size_t i = 0; i < arraysize(spaces); ++i) { | |
| 90 i::Vector<const uint32_t> chunks = | |
| 91 serializer.FinalAllocationChunks(spaces[i]); | |
| 92 // For the start-up snapshot, none of the reservations has more than | |
| 93 // one chunk (reservation for each space fits onto a single page). | |
| 94 CHECK_EQ(1, chunks.length()); | |
| 95 sink.PutInt(chunks[0], "spaces"); | |
| 96 } | |
| 97 | |
| 98 i::byte* context_bytes = context_snapshot_data.begin(); | |
| 99 sink.PutBlob(context_bytes, context_snapshot_data.length(), "context"); | |
| 100 for (size_t i = 0; i < arraysize(spaces); ++i) { | |
| 101 i::Vector<const uint32_t> chunks = | |
| 102 context_serializer.FinalAllocationChunks(spaces[i]); | |
| 103 // For the context snapshot, none of the reservations has more than | |
| 104 // one chunk (reservation for each space fits onto a single page). | |
| 105 CHECK_EQ(1, chunks.length()); | |
| 106 sink.PutInt(chunks[0], "spaces"); | |
| 107 } | |
| 108 | |
| 109 const i::List<i::byte>& startup_blob = sink.data(); | |
| 110 size_t written = fwrite(startup_blob.begin(), 1, startup_blob.length(), | |
| 111 startup_blob_file_); | |
| 112 if (written != static_cast<size_t>(startup_blob.length())) { | |
| 113 i::PrintF("Writing snapshot file failed.. Aborting.\n"); | |
| 114 exit(1); | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 void WriteSnapshotFile(const i::List<i::byte>& snapshot_data, | |
| 119 const i::Serializer& serializer, | |
| 120 const i::List<i::byte>& context_snapshot_data, | |
| 121 const i::Serializer& context_serializer) const { | |
| 122 WriteFilePrefix(); | |
| 123 WriteData("", snapshot_data, raw_file_); | |
| 124 WriteData("context_", context_snapshot_data, raw_context_file_); | |
| 125 WriteMeta("context_", context_serializer); | |
| 126 WriteMeta("", serializer); | |
| 127 WriteFileSuffix(); | |
| 128 } | |
| 129 | |
| 130 void WriteFilePrefix() const { | |
| 131 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); | |
| 132 fprintf(fp_, "#include \"src/v8.h\"\n"); | |
| 133 fprintf(fp_, "#include \"src/base/platform/platform.h\"\n\n"); | |
| 134 fprintf(fp_, "#include \"src/snapshot.h\"\n\n"); | |
| 135 fprintf(fp_, "namespace v8 {\n"); | |
| 136 fprintf(fp_, "namespace internal {\n\n"); | |
| 137 } | |
| 138 | |
| 139 void WriteFileSuffix() const { | |
| 140 fprintf(fp_, "} // namespace internal\n"); | |
| 141 fprintf(fp_, "} // namespace v8\n"); | |
| 142 } | |
| 143 | |
| 144 void WriteData(const char* prefix, const i::List<i::byte>& source_data, | |
| 145 FILE* raw_file) const { | |
| 146 const i::List<i::byte>* data_to_be_written = NULL; | |
| 147 i::List<i::byte> compressed_data; | |
| 148 if (!compressor_) { | |
| 149 data_to_be_written = &source_data; | |
| 150 } else if (compressor_->Compress(source_data.ToVector())) { | |
| 151 compressed_data.AddAll(*compressor_->output()); | |
| 152 data_to_be_written = &compressed_data; | |
| 153 } else { | |
| 154 i::PrintF("Compression failed. Aborting.\n"); | |
| 155 exit(1); | |
| 156 } | |
| 157 | |
| 158 DCHECK(data_to_be_written); | |
| 159 MaybeWriteRawFile(data_to_be_written, raw_file); | |
| 160 WriteData(prefix, source_data, data_to_be_written); | |
| 161 } | |
| 162 | |
| 163 void MaybeWriteRawFile(const i::List<i::byte>* data, FILE* raw_file) const { | |
| 164 if (!data || !raw_file) | |
| 165 return; | |
| 166 | |
| 167 // Sanity check, whether i::List iterators truly return pointers to an | |
| 168 // internal array. | |
| 169 DCHECK(data->end() - data->begin() == data->length()); | |
| 170 | |
| 171 size_t written = fwrite(data->begin(), 1, data->length(), raw_file); | |
| 172 if (written != (size_t)data->length()) { | |
| 173 i::PrintF("Writing raw file failed.. Aborting.\n"); | |
| 174 exit(1); | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 void WriteData(const char* prefix, const i::List<i::byte>& source_data, | |
| 179 const i::List<i::byte>* data_to_be_written) const { | |
| 180 fprintf(fp_, "const byte Snapshot::%sdata_[] = {\n", prefix); | |
| 181 WriteSnapshotData(data_to_be_written); | |
| 182 fprintf(fp_, "};\n"); | |
| 183 fprintf(fp_, "const int Snapshot::%ssize_ = %d;\n", prefix, | |
| 184 data_to_be_written->length()); | |
| 185 | |
| 186 if (data_to_be_written == &source_data) { | |
| 187 fprintf(fp_, "const byte* Snapshot::%sraw_data_ = Snapshot::%sdata_;\n", | |
| 188 prefix, prefix); | |
| 189 fprintf(fp_, "const int Snapshot::%sraw_size_ = Snapshot::%ssize_;\n", | |
| 190 prefix, prefix); | |
| 191 } else { | |
| 192 fprintf(fp_, "const byte* Snapshot::%sraw_data_ = NULL;\n", prefix); | |
| 193 fprintf(fp_, "const int Snapshot::%sraw_size_ = %d;\n", | |
| 194 prefix, source_data.length()); | |
| 195 } | |
| 196 fprintf(fp_, "\n"); | |
| 197 } | |
| 198 | |
| 199 void WriteMeta(const char* prefix, const i::Serializer& ser) const { | |
| 200 WriteSizeVar(ser, prefix, "new", i::NEW_SPACE); | |
| 201 WriteSizeVar(ser, prefix, "pointer", i::OLD_POINTER_SPACE); | |
| 202 WriteSizeVar(ser, prefix, "data", i::OLD_DATA_SPACE); | |
| 203 WriteSizeVar(ser, prefix, "code", i::CODE_SPACE); | |
| 204 WriteSizeVar(ser, prefix, "map", i::MAP_SPACE); | |
| 205 WriteSizeVar(ser, prefix, "cell", i::CELL_SPACE); | |
| 206 WriteSizeVar(ser, prefix, "property_cell", i::PROPERTY_CELL_SPACE); | |
| 207 WriteSizeVar(ser, prefix, "lo", i::LO_SPACE); | |
| 208 fprintf(fp_, "\n"); | |
| 209 } | |
| 210 | |
| 211 void WriteSizeVar(const i::Serializer& ser, const char* prefix, | |
| 212 const char* name, int space) const { | |
| 213 i::Vector<const uint32_t> chunks = ser.FinalAllocationChunks(space); | |
| 214 // For the start-up snapshot, none of the reservations has more than | |
| 215 // one chunk (total reservation fits into a single page). | |
| 216 CHECK_EQ(1, chunks.length()); | |
| 217 fprintf(fp_, "const int Snapshot::%s%s_space_used_ = %d;\n", prefix, name, | |
| 218 chunks[0]); | |
| 219 } | |
| 220 | |
| 221 void WriteSnapshotData(const i::List<i::byte>* data) const { | |
| 222 for (int i = 0; i < data->length(); i++) { | |
| 223 if ((i & 0x1f) == 0x1f) | |
| 224 fprintf(fp_, "\n"); | |
| 225 if (i > 0) | |
| 226 fprintf(fp_, ","); | |
| 227 fprintf(fp_, "%u", static_cast<unsigned char>(data->at(i))); | |
| 228 } | |
| 229 fprintf(fp_, "\n"); | |
| 230 } | |
| 231 | |
| 232 FILE* GetFileDescriptorOrDie(const char* filename) { | |
| 233 FILE* fp = base::OS::FOpen(filename, "wb"); | |
| 234 if (fp == NULL) { | |
| 235 i::PrintF("Unable to open file \"%s\" for writing.\n", filename); | |
| 236 exit(1); | |
| 237 } | |
| 238 return fp; | |
| 239 } | |
| 240 | |
| 241 FILE* fp_; | |
| 242 FILE* raw_file_; | |
| 243 FILE* raw_context_file_; | |
| 244 FILE* startup_blob_file_; | |
| 245 Compressor* compressor_; | |
| 246 }; | |
| 247 | |
| 248 | |
| 249 void DumpException(Handle<Message> message) { | |
| 250 String::Utf8Value message_string(message->Get()); | |
| 251 String::Utf8Value message_line(message->GetSourceLine()); | |
| 252 fprintf(stderr, "%s at line %d\n", *message_string, message->GetLineNumber()); | |
| 253 fprintf(stderr, "%s\n", *message_line); | |
| 254 for (int i = 0; i <= message->GetEndColumn(); ++i) { | |
| 255 fprintf(stderr, "%c", i < message->GetStartColumn() ? ' ' : '^'); | |
| 256 } | |
| 257 fprintf(stderr, "\n"); | |
| 258 } | |
| 259 | |
| 260 | |
| 261 int main(int argc, char** argv) { | 7 int main(int argc, char** argv) { |
| 262 // By default, log code create information in the snapshot. | 8 // By default, log code create information in the snapshot. |
| 263 i::FLAG_log_code = true; | 9 i::FLAG_log_code = true; |
| 264 | 10 |
| 265 // Omit from the snapshot natives for features that can be turned off | 11 // Omit from the snapshot natives for features that can be turned off |
| 266 // at runtime. | 12 // at runtime. |
| 267 i::FLAG_harmony_shipping = false; | 13 i::FLAG_harmony_shipping = false; |
| 268 | 14 |
| 269 // Print the usage if an error occurs when parsing the command line | 15 // Print the usage if an error occurs when parsing the command line |
| 270 // flags or if the help flag is set. | 16 // flags or if the help flag is set. |
| 271 int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true); | 17 int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true); |
| 272 if (result > 0 || argc != 2 || i::FLAG_help) { | 18 if (result > 0 || argc != 2 || i::FLAG_help) { |
| 273 ::printf("Usage: %s [flag] ... outfile\n", argv[0]); | 19 ::printf("Usage: %s [flag] ... outfile\n", argv[0]); |
| 274 i::FlagList::PrintHelp(); | 20 i::FlagList::PrintHelp(); |
| 275 return !i::FLAG_help; | 21 return !i::FLAG_help; |
| 276 } | 22 } |
| 277 | 23 |
| 278 i::CpuFeatures::Probe(true); | |
| 279 V8::InitializeICU(); | |
| 280 v8::Platform* platform = v8::platform::CreateDefaultPlatform(); | |
| 281 v8::V8::InitializePlatform(platform); | |
| 282 v8::V8::Initialize(); | |
| 283 | |
| 284 i::FLAG_logfile_per_isolate = false; | 24 i::FLAG_logfile_per_isolate = false; |
| 285 | 25 |
| 286 Isolate::CreateParams params; | 26 v8::V8::CreateV8Snapshot(i::FLAG_raw_file, i::FLAG_raw_context_file, |
| 287 params.enable_serializer = true; | 27 i::FLAG_startup_blob, i::FLAG_extra_code, argv[1]); |
| 288 Isolate* isolate = v8::Isolate::New(params); | |
| 289 { Isolate::Scope isolate_scope(isolate); | |
| 290 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate); | |
| 291 | |
| 292 Persistent<Context> context; | |
| 293 { | |
| 294 HandleScope handle_scope(isolate); | |
| 295 context.Reset(isolate, Context::New(isolate)); | |
| 296 } | |
| 297 | |
| 298 if (context.IsEmpty()) { | |
| 299 fprintf(stderr, | |
| 300 "\nException thrown while compiling natives - see above.\n\n"); | |
| 301 exit(1); | |
| 302 } | |
| 303 if (i::FLAG_extra_code != NULL) { | |
| 304 // Capture 100 frames if anything happens. | |
| 305 V8::SetCaptureStackTraceForUncaughtExceptions(true, 100); | |
| 306 HandleScope scope(isolate); | |
| 307 v8::Context::Scope cscope(v8::Local<v8::Context>::New(isolate, context)); | |
| 308 const char* name = i::FLAG_extra_code; | |
| 309 FILE* file = base::OS::FOpen(name, "rb"); | |
| 310 if (file == NULL) { | |
| 311 fprintf(stderr, "Failed to open '%s': errno %d\n", name, errno); | |
| 312 exit(1); | |
| 313 } | |
| 314 | |
| 315 fseek(file, 0, SEEK_END); | |
| 316 int size = ftell(file); | |
| 317 rewind(file); | |
| 318 | |
| 319 char* chars = new char[size + 1]; | |
| 320 chars[size] = '\0'; | |
| 321 for (int i = 0; i < size;) { | |
| 322 int read = static_cast<int>(fread(&chars[i], 1, size - i, file)); | |
| 323 if (read < 0) { | |
| 324 fprintf(stderr, "Failed to read '%s': errno %d\n", name, errno); | |
| 325 exit(1); | |
| 326 } | |
| 327 i += read; | |
| 328 } | |
| 329 fclose(file); | |
| 330 Local<String> source = String::NewFromUtf8(isolate, chars); | |
| 331 TryCatch try_catch; | |
| 332 Local<Script> script = Script::Compile(source); | |
| 333 if (try_catch.HasCaught()) { | |
| 334 fprintf(stderr, "Failure compiling '%s'\n", name); | |
| 335 DumpException(try_catch.Message()); | |
| 336 exit(1); | |
| 337 } | |
| 338 script->Run(); | |
| 339 if (try_catch.HasCaught()) { | |
| 340 fprintf(stderr, "Failure running '%s'\n", name); | |
| 341 DumpException(try_catch.Message()); | |
| 342 exit(1); | |
| 343 } | |
| 344 } | |
| 345 // Make sure all builtin scripts are cached. | |
| 346 { HandleScope scope(isolate); | |
| 347 for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) { | |
| 348 internal_isolate->bootstrapper()->NativesSourceLookup(i); | |
| 349 } | |
| 350 } | |
| 351 // If we don't do this then we end up with a stray root pointing at the | |
| 352 // context even after we have disposed of the context. | |
| 353 internal_isolate->heap()->CollectAllAvailableGarbage("mksnapshot"); | |
| 354 i::Object* raw_context = *v8::Utils::OpenPersistent(context); | |
| 355 context.Reset(); | |
| 356 | |
| 357 // This results in a somewhat smaller snapshot, probably because it gets | |
| 358 // rid of some things that are cached between garbage collections. | |
| 359 i::SnapshotByteSink snapshot_sink; | |
| 360 i::StartupSerializer ser(internal_isolate, &snapshot_sink); | |
| 361 ser.SerializeStrongReferences(); | |
| 362 | |
| 363 i::SnapshotByteSink context_sink; | |
| 364 i::PartialSerializer context_ser(internal_isolate, &ser, &context_sink); | |
| 365 context_ser.Serialize(&raw_context); | |
| 366 ser.SerializeWeakReferences(); | |
| 367 | |
| 368 context_ser.FinalizeAllocation(); | |
| 369 ser.FinalizeAllocation(); | |
| 370 | |
| 371 { | |
| 372 SnapshotWriter writer(argv[1]); | |
| 373 if (i::FLAG_raw_file && i::FLAG_raw_context_file) | |
| 374 writer.SetRawFiles(i::FLAG_raw_file, i::FLAG_raw_context_file); | |
| 375 if (i::FLAG_startup_blob) | |
| 376 writer.SetStartupBlobFile(i::FLAG_startup_blob); | |
| 377 writer.WriteSnapshot(snapshot_sink.data(), ser, context_sink.data(), | |
| 378 context_ser); | |
| 379 } | |
| 380 } | |
| 381 | |
| 382 isolate->Dispose(); | |
| 383 V8::Dispose(); | |
| 384 V8::ShutdownPlatform(); | |
| 385 delete platform; | |
| 386 return 0; | 28 return 0; |
| 387 } | 29 } |
| OLD | NEW |