Skip to content

Commit 04193b0

Browse files
committed
src: generate default snapshot with --predictable
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.
1 parent 773fde2 commit 04193b0

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

src/node.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,13 @@ InitializeOncePerProcessInternal(const std::vector<std::string>& args,
932932
}
933933
}
934934

935+
if (!!(flags & ProcessInitializationFlags::kGeneratePredictableSnapshot) ||
936+
per_process::cli_options->per_isolate->build_snapshot) {
937+
v8::V8::SetFlagsFromString("--predictable");
938+
v8::V8::SetFlagsFromString("--random_seed=42");
939+
v8::V8::SetFlagsFromString("--harmony-import-assertions");
940+
}
941+
935942
if (!(flags & ProcessInitializationFlags::kNoUseLargePages) &&
936943
(per_process::cli_options->use_largepages == "on" ||
937944
per_process::cli_options->use_largepages == "silent")) {

src/node.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ enum Flags : uint32_t {
261261
kNoUseLargePages = 1 << 11,
262262
// Skip printing output for --help, --version, --v8-options.
263263
kNoPrintHelpOrVersionOutput = 1 << 12,
264+
// Initialize the process for predictable snapshot generation.
265+
kGeneratePredictableSnapshot = 1 << 13,
264266

265267
// Emulate the behavior of InitializeNodeWithArgs() when passing
266268
// a flags argument to the InitializeOncePerProcess() replacement

tools/snapshot/node_mksnapshot.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ int main(int argc, char* argv[]) {
5151
setvbuf(stderr, nullptr, _IONBF, 0);
5252
#endif // _WIN32
5353

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

@@ -66,7 +64,8 @@ int BuildSnapshot(int argc, char* argv[]) {
6664

6765
std::unique_ptr<node::InitializationResult> result =
6866
node::InitializeOncePerProcess(
69-
std::vector<std::string>(argv, argv + argc));
67+
std::vector<std::string>(argv, argv + argc),
68+
node::ProcessInitializationFlags::kGeneratePredictableSnapshot);
7069

7170
CHECK(!result->early_return());
7271
CHECK_EQ(result->exit_code(), 0);

0 commit comments

Comments
 (0)