Skip to content

Commit f02b74d

Browse files
bnoordhuisdanbev
authored andcommitted
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. PR-URL: nodejs/node#18534 Fixes: nodejs/node#18039 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]>
1 parent 4a881e0 commit f02b74d

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/env.cc

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "node_internals.h"
22
#include "async_wrap.h"
3-
#include "v8-profiler.h"
43
#include "node_buffer.h"
54
#include "node_platform.h"
65

@@ -67,6 +66,15 @@ IsolateData::IsolateData(Isolate* isolate,
6766
IsolateData::~IsolateData() {
6867
if (platform_ != nullptr)
6968
platform_->UnregisterIsolate(this);
69+
if (cpu_profiler_ != nullptr)
70+
cpu_profiler_->Dispose();
71+
}
72+
73+
v8::CpuProfiler* IsolateData::GetCpuProfiler() {
74+
if (cpu_profiler_ != nullptr) return cpu_profiler_;
75+
cpu_profiler_ = v8::CpuProfiler::New(isolate());
76+
CHECK_NE(cpu_profiler_, nullptr);
77+
return cpu_profiler_;
7078
}
7179

7280
void Environment::Start(int argc,
@@ -152,12 +160,12 @@ void Environment::CleanupHandles() {
152160
void Environment::StartProfilerIdleNotifier() {
153161
uv_prepare_start(&idle_prepare_handle_, [](uv_prepare_t* handle) {
154162
Environment* env = ContainerOf(&Environment::idle_prepare_handle_, handle);
155-
env->isolate()->GetCpuProfiler()->SetIdle(true);
163+
env->isolate_data()->GetCpuProfiler()->SetIdle(true);
156164
});
157165

158166
uv_check_start(&idle_check_handle_, [](uv_check_t* handle) {
159167
Environment* env = ContainerOf(&Environment::idle_check_handle_, handle);
160-
env->isolate()->GetCpuProfiler()->SetIdle(false);
168+
env->isolate_data()->GetCpuProfiler()->SetIdle(false);
161169
});
162170
}
163171

src/env.h

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "req_wrap.h"
3434
#include "util.h"
3535
#include "uv.h"
36+
#include "v8-profiler.h"
3637
#include "v8.h"
3738
#include "node.h"
3839
#include "node_http2_state.h"
@@ -340,6 +341,8 @@ class IsolateData {
340341
std::unordered_map<nghttp2_rcbuf*, v8::Eternal<v8::String>> http2_static_strs;
341342
inline v8::Isolate* isolate() const;
342343

344+
v8::CpuProfiler* GetCpuProfiler();
345+
343346
private:
344347
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
345348
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
@@ -355,6 +358,7 @@ class IsolateData {
355358
uv_loop_t* const event_loop_;
356359
uint32_t* const zero_fill_field_;
357360
MultiIsolatePlatform* platform_;
361+
v8::CpuProfiler* cpu_profiler_ = nullptr;
358362

359363
DISALLOW_COPY_AND_ASSIGN(IsolateData);
360364
};

0 commit comments

Comments
 (0)