Chromium Code Reviews| Index: runtime/bin/main.cc |
| diff --git a/runtime/bin/main.cc b/runtime/bin/main.cc |
| index eaa30c4f5293da01a7ade5512dce7af6f3cdb183..19ff86e83db6728dcc85671ab898ef29cff85af9 100644 |
| --- a/runtime/bin/main.cc |
| +++ b/runtime/bin/main.cc |
| @@ -76,6 +76,18 @@ static bool has_compile_all = false; |
| static bool has_precompile = false; |
| +// Global flag that is used to indicate that we want to run from a precompiled |
| +// snapshot. |
| +static bool has_precompiled = false; |
| + |
| + |
| +extern const char* kPrecompiledLibraryName; |
| +extern const char* kPrecompiledSymbolName; |
| +static const char* kPrecompiledVmIsolateName = "precompiled.vmisolate"; |
| +static const char* kPrecompiledIsolateName = "precompiled.isolate"; |
| +static const char* kPrecompiledInstructionsName = "precompiled.instructions"; |
| + |
| + |
| // Global flag that is used to indicate that we want to trace resolution of |
| // URIs and the loading of libraries, parts and scripts. |
| static bool has_trace_loading = false; |
| @@ -298,12 +310,30 @@ static bool ProcessPrecompileOption(const char* arg, |
| if (*arg != '\0') { |
| return false; |
| } |
| + // Ensure that we are not already running using a full snapshot. |
| + if (isolate_snapshot_buffer != NULL) { |
| + Log::PrintErr("Precompiled snapshots must be generated with" |
| + " dart_no_snapshot."); |
| + return false; |
| + } |
| has_precompile = true; |
| vm_options->AddArgument("--precompile"); |
| return true; |
| } |
| +static bool ProcessPrecompiledOption(const char* arg, |
| + CommandLineOptions* vm_options) { |
| + ASSERT(arg != NULL); |
| + if (*arg != '\0') { |
| + return false; |
| + } |
| + has_precompiled = true; |
| + vm_options->AddArgument("--precompile"); |
| + return true; |
| +} |
| + |
| + |
| static bool ProcessDebugOption(const char* option_value, |
| CommandLineOptions* vm_options) { |
| ASSERT(option_value != NULL); |
| @@ -323,7 +353,7 @@ static bool ProcessDebugOption(const char* option_value, |
| static bool ProcessGenScriptSnapshotOption(const char* filename, |
| CommandLineOptions* vm_options) { |
| if (filename != NULL && strlen(filename) != 0) { |
| - // Ensure that are already running using a full snapshot. |
| + // Ensure that we are already running using a full snapshot. |
| if (isolate_snapshot_buffer == NULL) { |
| Log::PrintErr("Script snapshots cannot be generated in this version of" |
| " dart\n"); |
| @@ -415,6 +445,7 @@ static struct { |
| { "--break-at=", ProcessBreakpointOption }, |
| { "--compile_all", ProcessCompileAllOption }, |
| { "--precompile", ProcessPrecompileOption }, |
| + { "--precompiled", ProcessPrecompiledOption }, |
|
siva
2015/09/02 21:16:37
maybe we should call these
--gen-precompiled-snaps
rmacnak
2015/09/02 22:58:50
Done.
|
| { "--debug", ProcessDebugOption }, |
| { "--snapshot=", ProcessGenScriptSnapshotOption }, |
| { "--enable-vm-service", ProcessEnableVmServiceOption }, |
| @@ -962,6 +993,50 @@ static void ServiceStreamCancelCallback(const char* stream_id) { |
| } |
| +static void WriteSnapshotFile(const char* filename, |
| + const uint8_t* buffer, |
| + const intptr_t size) { |
| + File* file = File::Open(filename, File::kWriteTruncate); |
| + ASSERT(file != NULL); |
| + if (!file->WriteFully(buffer, size)) { |
| + Log::PrintErr("Error: Failed to write snapshot file.\n\n"); |
| + } |
| + delete file; |
| +} |
| + |
| + |
| +static void ReadSnapshotFile(const char* filename, |
| + const uint8_t** buffer) { |
| + void* file = DartUtils::OpenFile(filename, false); |
| + if (file == NULL) { |
| + Log::PrintErr("Error: Failed to open '%s'.\n\n", filename); |
| + exit(kErrorExitCode); |
| + } |
| + intptr_t len = -1; |
| + DartUtils::ReadFile(buffer, &len, file); |
| + if (*buffer == NULL || len == -1) { |
| + Log::PrintErr("Error: Failed to read '%s'.\n\n", filename); |
| + exit(kErrorExitCode); |
| + } |
| + DartUtils::CloseFile(file); |
| +} |
| + |
| + |
| +static void* LoadLibrarySymbol(const char* libname, const char* symname) { |
| + void* library = Extensions::LoadExtensionLibrary(libname); |
| + if (library == NULL) { |
| + Log::PrintErr("Error: Failed to load library '%s'.\n\n", libname); |
| + exit(kErrorExitCode); |
| + } |
| + void* symbol = Extensions::ResolveSymbol(library, symname); |
| + if (symbol == NULL) { |
| + Log::PrintErr("Failed to load symbol '%s'\n", symname); |
| + exit(kErrorExitCode); |
| + } |
| + return symbol; |
| +} |
| + |
| + |
| void main(int argc, char** argv) { |
| char* script_name; |
| const int EXTRA_VM_ARGUMENTS = 2; |
| @@ -1035,8 +1110,16 @@ void main(int argc, char** argv) { |
| DebuggerConnectionHandler::InitForVmService(); |
| } |
| + const uint8_t* instructions_snapshot = NULL; |
| + if (has_precompiled) { |
| + instructions_snapshot = reinterpret_cast<const uint8_t*>( |
| + LoadLibrarySymbol(kPrecompiledLibraryName, kPrecompiledSymbolName)); |
| + ReadSnapshotFile(kPrecompiledVmIsolateName, &vm_isolate_snapshot_buffer); |
| + ReadSnapshotFile(kPrecompiledIsolateName, &isolate_snapshot_buffer); |
| + } |
| + |
| // Initialize the Dart VM. |
| - if (!Dart_Initialize(vm_isolate_snapshot_buffer, NULL, |
| + if (!Dart_Initialize(vm_isolate_snapshot_buffer, instructions_snapshot, |
| CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate, |
| DartUtils::OpenFile, |
| DartUtils::ReadFile, |
| @@ -1123,6 +1206,29 @@ void main(int argc, char** argv) { |
| if (has_precompile) { |
| result = Dart_Precompile(); |
| DartExitOnError(result); |
| + |
| + uint8_t* vm_isolate_buffer = NULL; |
| + intptr_t vm_isolate_size = 0; |
| + uint8_t* isolate_buffer = NULL; |
| + intptr_t isolate_size = 0; |
| + uint8_t* instructions_buffer = NULL; |
| + intptr_t instructions_size = 0; |
| + result = Dart_CreatePrecompiledSnapshot(&vm_isolate_buffer, |
| + &vm_isolate_size, |
| + &isolate_buffer, |
| + &isolate_size, |
| + &instructions_buffer, |
| + &instructions_size); |
| + DartExitOnError(result); |
| + WriteSnapshotFile(kPrecompiledVmIsolateName, |
| + vm_isolate_buffer, |
| + vm_isolate_size); |
| + WriteSnapshotFile(kPrecompiledIsolateName, |
| + isolate_buffer, |
| + isolate_size); |
| + WriteSnapshotFile(kPrecompiledInstructionsName, |
| + instructions_buffer, |
| + instructions_size); |
| } else if (has_compile_all) { |
| result = Dart_CompileAll(); |
| DartExitOnError(result); |