From 45d610612981223a9b4e4a8d1d8f21caecceb700 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 18 Apr 2019 22:09:28 +0800 Subject: [PATCH] src: enable snapshot with per-isolate data Enable serializing the isolate from an isolate snapshot generated by node_mksnapshot with per-isolate data. PR-URL: https://github.com/nodejs/node/pull/27321 Refs: https://github.com/nodejs/node/issues/17058 Reviewed-By: Anna Henningsen Reviewed-By: Refael Ackermann Reviewed-By: James M Snell Reviewed-By: Ben Noordhuis --- node.gyp | 29 +++++++++++++++++++++++++++++ src/node.cc | 15 ++++++++++++++- src/node_main_instance.cc | 1 + src/node_main_instance.h | 5 +++++ src/node_snapshot_stub.cc | 13 +++++++++++++ 5 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/node_snapshot_stub.cc diff --git a/node.gyp b/node.gyp index 8019a16599a20f..82efba25d62efa 100644 --- a/node.gyp +++ b/node.gyp @@ -223,6 +223,7 @@ 'deps/acorn/acorn/dist/acorn.js', 'deps/acorn/acorn-walk/dist/walk.js', ], + 'node_mksnapshot_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)node_mksnapshot<(EXECUTABLE_SUFFIX)', 'mkcodecache_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)mkcodecache<(EXECUTABLE_SUFFIX)', 'conditions': [ [ 'node_shared=="true"', { @@ -430,6 +431,31 @@ 'src/node_code_cache_stub.cc' ], }], + ['want_separate_host_toolset==0', { + 'dependencies': [ + 'node_mksnapshot', + ], + 'actions': [ + { + 'action_name': 'node_mksnapshot', + 'process_outputs_as_sources': 1, + 'inputs': [ + '<(node_mksnapshot_exec)', + ], + 'outputs': [ + '<(SHARED_INTERMEDIATE_DIR)/node_snapshot.cc', + ], + 'action': [ + '<@(_inputs)', + '<@(_outputs)', + ], + }, + ], + }, { + 'sources': [ + 'src/node_snapshot_stub.cc' + ], + }], ], }, # node_core_target_name { @@ -1038,6 +1064,7 @@ 'defines': [ 'NODE_WANT_INTERNALS=1' ], 'sources': [ + 'src/node_snapshot_stub.cc', 'src/node_code_cache_stub.cc', 'test/cctest/gtest/gtest-all.cc', 'test/cctest/gtest/gtest_main.cc', @@ -1131,6 +1158,7 @@ 'NODE_WANT_INTERNALS=1' ], 'sources': [ + 'src/node_snapshot_stub.cc', 'src/node_code_cache_stub.cc', 'tools/code_cache/mkcodecache.cc', 'tools/code_cache/cache_builder.cc', @@ -1180,6 +1208,7 @@ 'defines': [ 'NODE_WANT_INTERNALS=1' ], 'sources': [ + 'src/node_snapshot_stub.cc', 'src/node_code_cache_stub.cc', 'tools/snapshot/node_mksnapshot.cc', 'tools/snapshot/snapshot_builder.cc', diff --git a/src/node.cc b/src/node.cc index 9e969d33dc1e9f..66e6db74724a32 100644 --- a/src/node.cc +++ b/src/node.cc @@ -887,11 +887,24 @@ int Start(int argc, char** argv) { { Isolate::CreateParams params; + // TODO(joyeecheung): collect external references and set it in + // params.external_references. + std::vector external_references = { + reinterpret_cast(nullptr)}; + v8::StartupData* blob = NodeMainInstance::GetEmbeddedSnapshotBlob(); + const NodeMainInstance::IndexArray* indexes = + NodeMainInstance::GetIsolateDataIndexes(); + if (blob != nullptr) { + params.external_references = external_references.data(); + params.snapshot_blob = blob; + } + NodeMainInstance main_instance(¶ms, uv_default_loop(), per_process::v8_platform.Platform(), result.args, - result.exec_args); + result.exec_args, + indexes); result.exit_code = main_instance.Run(); } diff --git a/src/node_main_instance.cc b/src/node_main_instance.cc index 11dfadabb9422d..419fd767720e4b 100644 --- a/src/node_main_instance.cc +++ b/src/node_main_instance.cc @@ -175,6 +175,7 @@ std::unique_ptr NodeMainInstance::CreateMainEnvironment( if (deserialize_mode_) { SetIsolateUpForNode(isolate_, IsolateSettingCategories::kErrorHandlers); } + CHECK(!context.IsEmpty()); Context::Scope context_scope(context); diff --git a/src/node_main_instance.h b/src/node_main_instance.h index 615fda1f4945c3..98c17cc8f011dd 100644 --- a/src/node_main_instance.h +++ b/src/node_main_instance.h @@ -70,6 +70,11 @@ class NodeMainInstance { // and the environment creation routine in workers somehow. std::unique_ptr CreateMainEnvironment(int* exit_code); + // If nullptr is returned, the binary is not built with embedded + // snapshot. + static const IndexArray* GetIsolateDataIndexes(); + static v8::StartupData* GetEmbeddedSnapshotBlob(); + private: NodeMainInstance(const NodeMainInstance&) = delete; NodeMainInstance& operator=(const NodeMainInstance&) = delete; diff --git a/src/node_snapshot_stub.cc b/src/node_snapshot_stub.cc new file mode 100644 index 00000000000000..c0604237d537c1 --- /dev/null +++ b/src/node_snapshot_stub.cc @@ -0,0 +1,13 @@ +#include "node_main_instance.h" + +namespace node { + +v8::StartupData* NodeMainInstance::GetEmbeddedSnapshotBlob() { + return nullptr; +} + +const NodeMainInstance::IndexArray* NodeMainInstance::GetIsolateDataIndexes() { + return nullptr; +} + +} // namespace node