@@ -1058,6 +1058,13 @@ Environment::~Environment() {
10581058 }
10591059
10601060 delete external_memory_accounter_;
1061+ if (cpu_profiler_) {
1062+ for (auto & it : pending_profiles_) {
1063+ cpu_profiler_->Stop (it.second );
1064+ }
1065+ cpu_profiler_->Dispose ();
1066+ cpu_profiler_ = nullptr ;
1067+ }
10611068}
10621069
10631070void Environment::InitializeLibuv () {
@@ -2221,4 +2228,33 @@ void Environment::MemoryInfo(MemoryTracker* tracker) const {
22212228void Environment::RunWeakRefCleanup () {
22222229 isolate ()->ClearKeptObjects ();
22232230}
2231+
2232+ v8::CpuProfilingResult Environment::StartCpuProfile (std::string_view name) {
2233+ HandleScope handle_scope (isolate ());
2234+ if (!cpu_profiler_) {
2235+ cpu_profiler_ = v8::CpuProfiler::New (isolate ());
2236+ }
2237+ Local<Value> title =
2238+ node::ToV8Value (context (), name, isolate ()).ToLocalChecked ();
2239+ v8::CpuProfilingResult result =
2240+ cpu_profiler_->Start (title.As <String>(), true );
2241+ if (result.status == v8::CpuProfilingStatus::kStarted ) {
2242+ pending_profiles_.emplace (name, result.id );
2243+ }
2244+ return result;
2245+ }
2246+
2247+ v8::CpuProfile* Environment::StopCpuProfile (std::string_view name) {
2248+ if (!cpu_profiler_) {
2249+ return nullptr ;
2250+ }
2251+ auto it = pending_profiles_.find (std::string (name));
2252+ if (it == pending_profiles_.end ()) {
2253+ return nullptr ;
2254+ }
2255+ v8::CpuProfile* profile = cpu_profiler_->Stop (it->second );
2256+ pending_profiles_.erase (it);
2257+ return profile;
2258+ }
2259+
22242260} // namespace node
0 commit comments