@@ -1080,6 +1080,14 @@ Environment::~Environment() {
10801080 addon.Close ();
10811081 }
10821082 }
1083+
1084+ if (cpu_profiler_) {
1085+ for (auto & it : pending_profiles_) {
1086+ cpu_profiler_->Stop (it.second );
1087+ }
1088+ cpu_profiler_->Dispose ();
1089+ cpu_profiler_ = nullptr ;
1090+ }
10831091}
10841092
10851093void Environment::InitializeLibuv () {
@@ -2243,4 +2251,33 @@ void Environment::MemoryInfo(MemoryTracker* tracker) const {
22432251void Environment::RunWeakRefCleanup () {
22442252 isolate ()->ClearKeptObjects ();
22452253}
2254+
2255+ v8::CpuProfilingResult Environment::StartCpuProfile (std::string_view name) {
2256+ HandleScope handle_scope (isolate ());
2257+ if (!cpu_profiler_) {
2258+ cpu_profiler_ = v8::CpuProfiler::New (isolate ());
2259+ }
2260+ Local<Value> title =
2261+ node::ToV8Value (context (), name, isolate ()).ToLocalChecked ();
2262+ v8::CpuProfilingResult result =
2263+ cpu_profiler_->Start (title.As <String>(), true );
2264+ if (result.status == v8::CpuProfilingStatus::kStarted ) {
2265+ pending_profiles_.emplace (name, result.id );
2266+ }
2267+ return result;
2268+ }
2269+
2270+ v8::CpuProfile* Environment::StopCpuProfile (std::string_view name) {
2271+ if (!cpu_profiler_) {
2272+ return nullptr ;
2273+ }
2274+ auto it = pending_profiles_.find (std::string (name));
2275+ if (it == pending_profiles_.end ()) {
2276+ return nullptr ;
2277+ }
2278+ v8::CpuProfile* profile = cpu_profiler_->Stop (it->second );
2279+ pending_profiles_.erase (it);
2280+ return profile;
2281+ }
2282+
22462283} // namespace node
0 commit comments