@@ -1068,6 +1068,13 @@ Environment::~Environment(){
10681068 }
10691069
10701070delete external_memory_accounter_;
1071+ if (cpu_profiler_){
1072+ for (auto & it : pending_profiles_){
1073+ cpu_profiler_->Stop (it.second );
1074+ }
1075+ cpu_profiler_->Dispose ();
1076+ cpu_profiler_ = nullptr ;
1077+ }
10711078}
10721079
10731080void Environment::InitializeLibuv (){
@@ -2231,4 +2238,35 @@ void Environment::MemoryInfo(MemoryTracker* tracker) const{
22312238void Environment::RunWeakRefCleanup (){
22322239isolate ()->ClearKeptObjects ();
22332240}
2241+
2242+ v8::CpuProfilingResult Environment::StartCpuProfile (
2243+ std::string_view name){
2244+ if (!cpu_profiler_){
2245+ cpu_profiler_ = v8::CpuProfiler::New (isolate_);
2246+ }
2247+ Local<Value> title = node::ToV8Value (context (),
2248+ name,
2249+ isolate ()).ToLocalChecked ();
2250+ v8::CpuProfilingResult result = cpu_profiler_->Start (title.As <String>(),
2251+ true );
2252+ if (result.status == v8::CpuProfilingStatus::kStarted ){
2253+ pending_profiles_.emplace (name, result.id );
2254+ }
2255+ return result;
2256+ }
2257+
2258+ v8::CpuProfile* Environment::StopCpuProfile (
2259+ std::string_view name){
2260+ if (!cpu_profiler_){
2261+ return nullptr ;
2262+ }
2263+ auto it = pending_profiles_.find (std::string (name));
2264+ if (it == pending_profiles_.end ()){
2265+ return nullptr ;
2266+ }
2267+ v8::CpuProfile* profile = cpu_profiler_->Stop (it->second );
2268+ pending_profiles_.erase (it);
2269+ return profile;
2270+ }
2271+
22342272} // namespace node
0 commit comments