Skip to content

Commit

Permalink
Added option --snapshot-after-run to enable an application snapshot t…
Browse files Browse the repository at this point in the history
…o be taken after running the application.

R=srdjan@google.com

Review URL: https://codereview.chromium.org/1413753002 .
  • Loading branch information
a-siva committed Oct 17, 2015
1 parent 8915fb6 commit 040637a
Showing 1 changed file with 61 additions and 28 deletions.
89 changes: 61 additions & 28 deletions runtime/bin/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ extern const uint8_t* isolate_snapshot_buffer;

// Global state that stores a pointer to the application script snapshot.
static bool generate_script_snapshot = false;
static bool generate_script_snapshot_after_run = false;
static const char* snapshot_filename = NULL;


Expand Down Expand Up @@ -381,23 +382,42 @@ static bool ProcessDebugOption(const char* option_value,
}


static bool ProcessGenScriptSnapshotOption(const char* filename,
CommandLineOptions* vm_options) {
if (filename != NULL && strlen(filename) != 0) {
static bool ProcessScriptSnapshotOptionHelper(const char* filename,
bool* snapshot_option) {
*snapshot_option = false;
if ((filename != NULL) && (strlen(filename) != 0)) {
// 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");
return false;
}
snapshot_filename = filename;
generate_script_snapshot = true;
*snapshot_option = true;
if (generate_script_snapshot && generate_script_snapshot_after_run) {
Log::PrintErr("--snapshot and --snapshot-after-run options"
" cannot be specified at the same time\n");
return false;
}
return true;
}
return false;
}


static bool ProcessScriptSnapshotOption(const char* filename,
CommandLineOptions* vm_options) {
return ProcessScriptSnapshotOptionHelper(filename, &generate_script_snapshot);
}


static bool ProcessScriptSnapshotAfterRunOption(
const char* filename, CommandLineOptions* vm_options) {
return ProcessScriptSnapshotOptionHelper(filename,
&generate_script_snapshot_after_run);
}


static bool ProcessEnableVmServiceOption(const char* option_value,
CommandLineOptions* vm_options) {
ASSERT(option_value != NULL);
Expand Down Expand Up @@ -508,7 +528,8 @@ static struct {
{ "--observe", ProcessObserveOption },
{ "--run-precompiled-snapshot", ProcessRunPrecompiledSnapshotOption },
{ "--shutdown", ProcessShutdownOption },
{ "--snapshot=", ProcessGenScriptSnapshotOption },
{ "--snapshot=", ProcessScriptSnapshotOption },
{ "--snapshot-after-run=", ProcessScriptSnapshotAfterRunOption },
{ "--trace-debug-protocol", ProcessTraceDebugProtocolOption },
{ "--trace-loading", ProcessTraceLoadingOption },
{ NULL, NULL }
Expand Down Expand Up @@ -1106,6 +1127,34 @@ static void* LoadLibrarySymbol(const char* libname, const char* symname) {
}


static void GenerateScriptSnapshot() {
// First create a snapshot.
uint8_t* buffer = NULL;
intptr_t size = 0;
Dart_Handle result = Dart_CreateScriptSnapshot(&buffer, &size);
if (Dart_IsError(result)) {
ErrorExit(kErrorExitCode, "%s\n", Dart_GetError(result));
}

// Open the snapshot file.
File* snapshot_file = File::Open(snapshot_filename, File::kWriteTruncate);
if (snapshot_file == NULL) {
ErrorExit(kErrorExitCode,
"Unable to open file %s for writing the snapshot\n",
snapshot_filename);
}

// Write the magic number to indicate file is a script snapshot.
DartUtils::WriteMagicNumber(snapshot_file);

// Now write the snapshot out to specified file.
bool bytes_written = snapshot_file->WriteFully(buffer, size);
ASSERT(bytes_written);
delete snapshot_file;
snapshot_file = NULL;
}


#define CHECK_RESULT(result) \
if (Dart_IsError(result)) { \
if (Dart_IsVMRestartRequest(result)) { \
Expand All @@ -1118,6 +1167,7 @@ static void* LoadLibrarySymbol(const char* libname, const char* symname) {
ErrorExit(exit_code, "%s\n", Dart_GetError(result)); \
}


bool RunMainIsolate(const char* script_name,
CommandLineOptions* dart_options) {
// Call CreateIsolateAndSetup which creates an isolate and loads up
Expand Down Expand Up @@ -1164,29 +1214,7 @@ bool RunMainIsolate(const char* script_name,
Dart_EnterScope();

if (generate_script_snapshot) {
// First create a snapshot.
Dart_Handle result;
uint8_t* buffer = NULL;
intptr_t size = 0;
result = Dart_CreateScriptSnapshot(&buffer, &size);
CHECK_RESULT(result);

// Open the snapshot file.
File* snapshot_file = File::Open(snapshot_filename, File::kWriteTruncate);
if (snapshot_file == NULL) {
ErrorExit(kErrorExitCode,
"Unable to open file %s for writing the snapshot\n",
snapshot_filename);
}

// Write the magic number to indicate file is a script snapshot.
DartUtils::WriteMagicNumber(snapshot_file);

// Now write the snapshot out to specified file.
bool bytes_written = snapshot_file->WriteFully(buffer, size);
ASSERT(bytes_written);
delete snapshot_file;
snapshot_file = NULL;
GenerateScriptSnapshot();
} else {
// Lookup the library of the root script.
Dart_Handle root_lib = Dart_RootLibrary();
Expand Down Expand Up @@ -1299,6 +1327,11 @@ bool RunMainIsolate(const char* script_name,
// Keep handling messages until the last active receive port is closed.
result = Dart_RunLoop();
CHECK_RESULT(result);

// Generate a script snapshot after execution if specified.
if (generate_script_snapshot_after_run) {
GenerateScriptSnapshot();
}
}
}

Expand Down

0 comments on commit 040637a

Please sign in to comment.