diff options
author | theanarkh <theratliter@gmail.com> | 2022-09-08 12:33:20 +0300 |
---|---|---|
committer | Juan José Arboleda <soyjuanarbol@gmail.com> | 2022-10-11 22:45:23 +0300 |
commit | 0beedb7f1c6867b69886c7b3560ff9268017cc66 (patch) | |
tree | f26e1e5883db9989588f384d2259589805f51891 /src | |
parent | a021f1974a912431930d50b80027f12fae8a752a (diff) |
v8: add setHeapSnapshotNearHeapLimit
PR-URL: https://github.com/nodejs/node/pull/44420
Refs: https://github.com/nodejs/node/pull/33010
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/env-inl.h | 18 | ||||
-rw-r--r-- | src/env.cc | 19 | ||||
-rw-r--r-- | src/env.h | 10 | ||||
-rw-r--r-- | src/node.cc | 5 | ||||
-rw-r--r-- | src/node_v8.cc | 12 |
5 files changed, 50 insertions, 14 deletions
diff --git a/src/env-inl.h b/src/env-inl.h index 97a8bf37629..cb0eb2151ed 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -896,6 +896,24 @@ v8::Local<v8::Context> Environment::context() const { return PersistentToLocal::Strong(context_); } +inline void Environment::set_heap_snapshot_near_heap_limit(uint32_t limit) { + heap_snapshot_near_heap_limit_ = limit; +} + +inline void Environment::AddHeapSnapshotNearHeapLimitCallback() { + DCHECK(!heapsnapshot_near_heap_limit_callback_added_); + heapsnapshot_near_heap_limit_callback_added_ = true; + isolate_->AddNearHeapLimitCallback(Environment::NearHeapLimitCallback, this); +} + +inline void Environment::RemoveHeapSnapshotNearHeapLimitCallback( + size_t heap_limit) { + DCHECK(heapsnapshot_near_heap_limit_callback_added_); + heapsnapshot_near_heap_limit_callback_added_ = false; + isolate_->RemoveNearHeapLimitCallback(Environment::NearHeapLimitCallback, + heap_limit); +} + } // namespace node // These two files depend on each other. Including base_object-inl.h after this diff --git a/src/env.cc b/src/env.cc index 77b894bd361..0ddced56d58 100644 --- a/src/env.cc +++ b/src/env.cc @@ -770,6 +770,9 @@ Environment::Environment(IsolateData* isolate_data, inspector_host_port_ = std::make_shared<ExclusiveAccess<HostPort>>( options_->debug_options().host_port); + heap_snapshot_near_heap_limit_ = + static_cast<uint32_t>(options_->heap_snapshot_near_heap_limit); + if (!(flags_ & EnvironmentFlags::kOwnsProcessState)) { set_abort_on_uncaught_exception(false); } @@ -884,9 +887,8 @@ Environment::~Environment() { // FreeEnvironment() should have set this. CHECK(is_stopping()); - if (options_->heap_snapshot_near_heap_limit > heap_limit_snapshot_taken_) { - isolate_->RemoveNearHeapLimitCallback(Environment::NearHeapLimitCallback, - 0); + if (heapsnapshot_near_heap_limit_callback_added_) { + RemoveHeapSnapshotNearHeapLimitCallback(0); } isolate()->GetHeapProfiler()->RemoveBuildEmbedderGraphCallback( @@ -2029,8 +2031,7 @@ size_t Environment::NearHeapLimitCallback(void* data, Debug(env, DebugCategory::DIAGNOSTICS, "Not generating snapshots because it's too risky.\n"); - env->isolate()->RemoveNearHeapLimitCallback(NearHeapLimitCallback, - initial_heap_limit); + env->RemoveHeapSnapshotNearHeapLimitCallback(initial_heap_limit); // The new limit must be higher than current_heap_limit or V8 might // crash. return current_heap_limit + 1; @@ -2050,17 +2051,15 @@ size_t Environment::NearHeapLimitCallback(void* data, // Remove the callback first in case it's triggered when generating // the snapshot. - env->isolate()->RemoveNearHeapLimitCallback(NearHeapLimitCallback, - initial_heap_limit); + env->RemoveHeapSnapshotNearHeapLimitCallback(initial_heap_limit); heap::WriteSnapshot(env->isolate(), filename.c_str()); env->heap_limit_snapshot_taken_ += 1; // Don't take more snapshots than the number specified by // --heapsnapshot-near-heap-limit. - if (env->heap_limit_snapshot_taken_ < - env->options_->heap_snapshot_near_heap_limit) { - env->isolate()->AddNearHeapLimitCallback(NearHeapLimitCallback, env); + if (env->heap_limit_snapshot_taken_ < env->heap_snapshot_near_heap_limit_) { + env->AddHeapSnapshotNearHeapLimitCallback(); } FPrintF(stderr, "Wrote snapshot to %s\n", filename.c_str()); diff --git a/src/env.h b/src/env.h index 51cdbb9e7cb..21a095194c4 100644 --- a/src/env.h +++ b/src/env.h @@ -1459,6 +1459,12 @@ class Environment : public MemoryRetainer { template <typename T> void ForEachBindingData(T&& iterator); + inline void set_heap_snapshot_near_heap_limit(uint32_t limit); + + inline void AddHeapSnapshotNearHeapLimitCallback(); + + inline void RemoveHeapSnapshotNearHeapLimitCallback(size_t heap_limit); + private: inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>), const char* errmsg); @@ -1516,7 +1522,9 @@ class Environment : public MemoryRetainer { std::string exec_path_; bool is_processing_heap_limit_callback_ = false; - int64_t heap_limit_snapshot_taken_ = 0; + uint32_t heap_limit_snapshot_taken_ = 0; + uint32_t heap_snapshot_near_heap_limit_ = 0; + bool heapsnapshot_near_heap_limit_callback_added_ = false; uint32_t module_id_counter_ = 0; uint32_t script_id_counter_ = 0; diff --git a/src/node.cc b/src/node.cc index 69e817c8031..dd1c8c81280 100644 --- a/src/node.cc +++ b/src/node.cc @@ -282,9 +282,8 @@ static void AtomicsWaitCallback(Isolate::AtomicsWaitEvent event, void Environment::InitializeDiagnostics() { isolate_->GetHeapProfiler()->AddBuildEmbedderGraphCallback( Environment::BuildEmbedderGraph, this); - if (options_->heap_snapshot_near_heap_limit > 0) { - isolate_->AddNearHeapLimitCallback(Environment::NearHeapLimitCallback, - this); + if (heap_snapshot_near_heap_limit_ > 0) { + AddHeapSnapshotNearHeapLimitCallback(); } if (options_->trace_uncaught) isolate_->SetCaptureStackTraceForUncaughtExceptions(true); diff --git a/src/node_v8.cc b/src/node_v8.cc index 70b294f1eca..b24b6f7973c 100644 --- a/src/node_v8.cc +++ b/src/node_v8.cc @@ -155,6 +155,15 @@ void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) { args.GetReturnValue().Set(result); } +void SetHeapSnapshotNearHeapLimit(const FunctionCallbackInfo<Value>& args) { + CHECK(args[0]->IsUint32()); + Environment* env = Environment::GetCurrent(args); + uint32_t limit = args[0].As<v8::Uint32>()->Value(); + CHECK_GT(limit, 0); + env->AddHeapSnapshotNearHeapLimitCallback(); + env->set_heap_snapshot_near_heap_limit(limit); +} + void UpdateHeapStatisticsBuffer(const FunctionCallbackInfo<Value>& args) { BindingData* data = Environment::GetBindingData<BindingData>(args); HeapStatistics s; @@ -212,6 +221,8 @@ void Initialize(Local<Object> target, CachedDataVersionTag); env->SetMethod( target, "updateHeapStatisticsBuffer", UpdateHeapStatisticsBuffer); + env->SetMethodNoSideEffect(target, "setHeapSnapshotNearHeapLimit", + SetHeapSnapshotNearHeapLimit); env->SetMethod( target, "updateHeapCodeStatisticsBuffer", UpdateHeapCodeStatisticsBuffer); @@ -259,6 +270,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(UpdateHeapCodeStatisticsBuffer); registry->Register(UpdateHeapSpaceStatisticsBuffer); registry->Register(SetFlagsFromString); + registry->Register(SetHeapSnapshotNearHeapLimit); } } // namespace v8_utils |