Skip to content

Commit e33b9fa

Browse files
bnoordhuisaddaleax
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. Backport-PR-URL: #18959 PR-URL: #18534 Fixes: #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 484e06d commit e33b9fa

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

54
#include <stdio.h>
65
#include <algorithm>
@@ -65,6 +64,15 @@ IsolateData::IsolateData(Isolate* isolate,
6564
IsolateData::~IsolateData() {
6665
if (platform_ != nullptr)
6766
platform_->UnregisterIsolate(this);
67+
if (cpu_profiler_ != nullptr)
68+
cpu_profiler_->Dispose();
69+
}
70+
71+
v8::CpuProfiler* IsolateData::GetCpuProfiler() {
72+
if (cpu_profiler_ != nullptr) return cpu_profiler_;
73+
cpu_profiler_ = v8::CpuProfiler::New(isolate());
74+
CHECK_NE(cpu_profiler_, nullptr);
75+
return cpu_profiler_;
6876
}
6977

7078
void Environment::Start(int argc,
@@ -150,12 +158,12 @@ void Environment::CleanupHandles() {
150158
void Environment::StartProfilerIdleNotifier() {
151159
uv_prepare_start(&idle_prepare_handle_, [](uv_prepare_t* handle) {
152160
Environment* env = ContainerOf(&Environment::idle_prepare_handle_, handle);
153-
env->isolate()->GetCpuProfiler()->SetIdle(true);
161+
env->isolate_data()->GetCpuProfiler()->SetIdle(true);
154162
});
155163

156164
uv_check_start(&idle_check_handle_, [](uv_check_t* handle) {
157165
Environment* env = ContainerOf(&Environment::idle_check_handle_, handle);
158-
env->isolate()->GetCpuProfiler()->SetIdle(false);
166+
env->isolate_data()->GetCpuProfiler()->SetIdle(false);
159167
});
160168
}
161169

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"
@@ -338,6 +339,8 @@ class IsolateData {
338339
std::unordered_map<nghttp2_rcbuf*, v8::Eternal<v8::String>> http2_static_strs;
339340
inline v8::Isolate* isolate() const;
340341

342+
v8::CpuProfiler* GetCpuProfiler();
343+
341344
private:
342345
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
343346
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
@@ -353,6 +356,7 @@ class IsolateData {
353356
uv_loop_t* const event_loop_;
354357
uint32_t* const zero_fill_field_;
355358
MultiIsolatePlatform* platform_;
359+
v8::CpuProfiler* cpu_profiler_ = nullptr;
356360

357361
DISALLOW_COPY_AND_ASSIGN(IsolateData);
358362
};

0 commit comments

Comments
 (0)