Index: runtime/bin/main.cc |
diff --git a/runtime/bin/main.cc b/runtime/bin/main.cc |
index 4e2b24acce608c4e11f99cce29d897f7384bef8b..b657bff5cd2dc23a9e482df11f2f1fa15d2ff094 100644 |
--- a/runtime/bin/main.cc |
+++ b/runtime/bin/main.cc |
@@ -76,6 +76,11 @@ 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; |
+ |
+ |
// 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; |
@@ -301,6 +306,18 @@ static bool ProcessPrecompileOption(const char* arg, |
} |
+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); |
@@ -412,6 +429,7 @@ static struct { |
{ "--break-at=", ProcessBreakpointOption }, |
{ "--compile_all", ProcessCompileAllOption }, |
{ "--precompile", ProcessPrecompileOption }, |
+ { "--precompiled", ProcessPrecompiledOption }, |
{ "--debug", ProcessDebugOption }, |
{ "--snapshot=", ProcessGenScriptSnapshotOption }, |
{ "--enable-vm-service", ProcessEnableVmServiceOption }, |
@@ -959,6 +977,18 @@ 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; |
+} |
+ |
+ |
void main(int argc, char** argv) { |
char* script_name; |
const int EXTRA_VM_ARGUMENTS = 2; |
@@ -1032,8 +1062,62 @@ void main(int argc, char** argv) { |
DebuggerConnectionHandler::InitForVmService(); |
} |
+ const uint8_t* instructions_snapshot = NULL; |
+ if (has_precompiled) { |
+ const char* kPrecompiledLibName = "libprecompiled.so"; |
siva
2015/09/01 20:58:48
This name seems specific to linux, maybe it should
rmacnak
2015/09/01 23:43:47
Done.
|
+ const char* kPrecompiledSymName = "kInstructionsSnapshot"; |
+ const char* kPrecompiledVmIsolateName = "precompiled.vmisolate"; |
+ const char* kPrecompiledIsolateName = "precompiled.isolate"; |
+ |
+ void* precompiled_lib = |
+ Extensions::LoadExtensionLibrary(kPrecompiledLibName); |
+ if (precompiled_lib == NULL) { |
+ fprintf(stderr, "Failed to load library '%s'\n", kPrecompiledLibName); |
+ fflush(stderr); |
+ exit(kErrorExitCode); |
+ } |
+ |
+ instructions_snapshot = reinterpret_cast<const uint8_t*>( |
+ Extensions::ResolveSymbol(precompiled_lib, kPrecompiledSymName)); |
+ if (instructions_snapshot == NULL) { |
+ fprintf(stderr, "Failed to load symbol '%s'\n", kPrecompiledSymName); |
+ fflush(stderr); |
+ exit(kErrorExitCode); |
+ } |
+ |
+ void* file = DartUtils::OpenFile(kPrecompiledVmIsolateName, false); |
+ if (file == NULL) { |
+ fprintf(stderr, "Failed to open '%s'\n", kPrecompiledVmIsolateName); |
+ fflush(stderr); |
+ exit(kErrorExitCode); |
+ } |
+ intptr_t len = -1; |
+ DartUtils::ReadFile(&vm_isolate_snapshot_buffer, &len, file); |
+ if (vm_isolate_snapshot_buffer == NULL || len == -1) { |
+ fprintf(stderr, "Failed to read '%s'\n", kPrecompiledVmIsolateName); |
+ fflush(stderr); |
+ exit(kErrorExitCode); |
+ } |
+ DartUtils::CloseFile(file); |
+ |
+ file = DartUtils::OpenFile(kPrecompiledIsolateName, false); |
+ if (file == NULL) { |
+ fprintf(stderr, "Failed to open '%s'\n", kPrecompiledIsolateName); |
+ fflush(stderr); |
+ exit(kErrorExitCode); |
+ } |
+ len = -1; |
+ DartUtils::ReadFile(&isolate_snapshot_buffer, &len, file); |
+ if (isolate_snapshot_buffer == NULL || len == -1) { |
+ fprintf(stderr, "Failed to read '%s'\n", kPrecompiledVmIsolateName); |
+ fflush(stderr); |
+ exit(kErrorExitCode); |
+ } |
+ DartUtils::CloseFile(file); |
siva
2015/09/01 20:58:49
The above code of reading a snapshot file is repea
rmacnak
2015/09/01 23:43:47
Done.
|
+ } |
+ |
// 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, |
@@ -1114,6 +1198,32 @@ 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("precompiled.vmisolate", |
+ vm_isolate_buffer, |
+ vm_isolate_size); |
+ WriteSnapshotFile("precompiled.isolate", |
+ isolate_buffer, |
+ isolate_size); |
+ WriteSnapshotFile("precompiled.instructions", |
+ instructions_buffer, |
+ instructions_size); |
siva
2015/09/01 20:58:48
You should extract these names as constants and us
rmacnak
2015/09/01 23:43:47
Done.
|
+ |
} else if (has_compile_all) { |
result = Dart_CompileAll(); |
DartExitOnError(result); |