Skip to content

Commit

Permalink
src: generate default snapshot with --predictable
Browse files Browse the repository at this point in the history
To improve determinism of snapshot generation, add --predictable
to the V8 flags used to initialize a process launched to generate
snapshot. Also add a kGeneratePredictableSnapshot flag
to ProcessInitializationFlags for this and moves the configuration
of these flags into node::InitializeOncePerProcess() so that
it can be shared by embedders.

PR-URL: #48749
Refs: nodejs/build#3043
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
  • Loading branch information
joyeecheung authored Oct 9, 2023
1 parent 81e4d2e commit 387e292
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,12 @@ static ExitCode InitializeNodeWithArgsInternal(
V8::SetFlagsFromString(NODE_V8_OPTIONS, sizeof(NODE_V8_OPTIONS) - 1);
#endif

if (!!(flags & ProcessInitializationFlags::kGeneratePredictableSnapshot) ||
per_process::cli_options->per_isolate->build_snapshot) {
v8::V8::SetFlagsFromString("--predictable");
v8::V8::SetFlagsFromString("--random_seed=42");
}

// Specify this explicitly to avoid being affected by V8 changes to the
// default value.
V8::SetFlagsFromString("--rehash-snapshot");
Expand Down
2 changes: 2 additions & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ enum Flags : uint32_t {
// cppgc::InitializeProcess() before creating a Node.js environment
// and call cppgc::ShutdownProcess() before process shutdown.
kNoInitializeCppgc = 1 << 13,
// Initialize the process for predictable snapshot generation.
kGeneratePredictableSnapshot = 1 << 14,

// Emulate the behavior of InitializeNodeWithArgs() when passing
// a flags argument to the InitializeOncePerProcess() replacement
Expand Down
5 changes: 2 additions & 3 deletions tools/snapshot/node_mksnapshot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ int main(int argc, char* argv[]) {
setvbuf(stderr, nullptr, _IONBF, 0);
#endif // _WIN32

v8::V8::SetFlagsFromString("--random_seed=42");
v8::V8::SetFlagsFromString("--harmony-import-assertions");
return BuildSnapshot(argc, argv);
}

Expand All @@ -65,7 +63,8 @@ int BuildSnapshot(int argc, char* argv[]) {

std::unique_ptr<node::InitializationResult> result =
node::InitializeOncePerProcess(
std::vector<std::string>(argv, argv + argc));
std::vector<std::string>(argv, argv + argc),
node::ProcessInitializationFlags::kGeneratePredictableSnapshot);

CHECK(!result->early_return());
CHECK_EQ(result->exit_code(), 0);
Expand Down

0 comments on commit 387e292

Please sign in to comment.