From e33b9fa7b59f34ebbce8f40ed77e8007b6fa1faf Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Fri, 2 Feb 2018 17:39:34 +0100 Subject: [PATCH] src: fix GetCpuProfiler() deprecation warning Replace `v8::Isolate::GetCpuProfiler()` with `v8::CpuProfiler::New()` and cache the instance; creating and disposing an instance every loop tick is too expensive. Backport-PR-URL: https://github.com/nodejs/node/pull/18959 PR-URL: https://github.com/nodejs/node/pull/18534 Fixes: https://github.com/nodejs/node/issues/18039 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Anna Henningsen Reviewed-By: Gireesh Punathil Reviewed-By: Daniel Bevenius --- src/env.cc | 14 +++++++++++--- src/env.h | 4 ++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/env.cc b/src/env.cc index 8881f37b18e38d..ca023125c2627c 100644 --- a/src/env.cc +++ b/src/env.cc @@ -1,6 +1,5 @@ #include "node_internals.h" #include "async_wrap.h" -#include "v8-profiler.h" #include #include @@ -65,6 +64,15 @@ IsolateData::IsolateData(Isolate* isolate, IsolateData::~IsolateData() { if (platform_ != nullptr) platform_->UnregisterIsolate(this); + if (cpu_profiler_ != nullptr) + cpu_profiler_->Dispose(); +} + +v8::CpuProfiler* IsolateData::GetCpuProfiler() { + if (cpu_profiler_ != nullptr) return cpu_profiler_; + cpu_profiler_ = v8::CpuProfiler::New(isolate()); + CHECK_NE(cpu_profiler_, nullptr); + return cpu_profiler_; } void Environment::Start(int argc, @@ -150,12 +158,12 @@ void Environment::CleanupHandles() { void Environment::StartProfilerIdleNotifier() { uv_prepare_start(&idle_prepare_handle_, [](uv_prepare_t* handle) { Environment* env = ContainerOf(&Environment::idle_prepare_handle_, handle); - env->isolate()->GetCpuProfiler()->SetIdle(true); + env->isolate_data()->GetCpuProfiler()->SetIdle(true); }); uv_check_start(&idle_check_handle_, [](uv_check_t* handle) { Environment* env = ContainerOf(&Environment::idle_check_handle_, handle); - env->isolate()->GetCpuProfiler()->SetIdle(false); + env->isolate_data()->GetCpuProfiler()->SetIdle(false); }); } diff --git a/src/env.h b/src/env.h index be8791b5344471..490ec9baed7cb9 100644 --- a/src/env.h +++ b/src/env.h @@ -33,6 +33,7 @@ #include "req_wrap.h" #include "util.h" #include "uv.h" +#include "v8-profiler.h" #include "v8.h" #include "node.h" #include "node_http2_state.h" @@ -338,6 +339,8 @@ class IsolateData { std::unordered_map> http2_static_strs; inline v8::Isolate* isolate() const; + v8::CpuProfiler* GetCpuProfiler(); + private: #define VP(PropertyName, StringValue) V(v8::Private, PropertyName) #define VS(PropertyName, StringValue) V(v8::String, PropertyName) @@ -353,6 +356,7 @@ class IsolateData { uv_loop_t* const event_loop_; uint32_t* const zero_fill_field_; MultiIsolatePlatform* platform_; + v8::CpuProfiler* cpu_profiler_ = nullptr; DISALLOW_COPY_AND_ASSIGN(IsolateData); };