diff --git a/src/d8/d8-console.cc b/src/d8/d8-console.cc index 624abb262b..cb88ade93f 100644 --- a/src/d8/d8-console.cc +++ b/src/d8/d8-console.cc @@ -80,7 +80,12 @@ D8Console::D8Console(Isolate* isolate) : isolate_(isolate) { } D8Console::~D8Console() { - if (profiler_) profiler_->Dispose(); + if (profiler_) { + if (profiler_active_) { + profiler_->StopProfiling(String::Empty(isolate_)); + } + profiler_->Dispose(); + } } void D8Console::Assert(const debug::ConsoleCallArguments& args, @@ -121,8 +126,8 @@ void D8Console::Profile(const debug::ConsoleCallArguments& args, const v8::debug::ConsoleContext&) { if (!profiler_) { profiler_ = CpuProfiler::New(isolate_); - Shell::SetCpuProfiler(profiler_); } + profiler_active_ = true; profiler_->StartProfiling(String::Empty(isolate_), CpuProfilingOptions{}); } @@ -130,11 +135,11 @@ void D8Console::ProfileEnd(const debug::ConsoleCallArguments& args, const v8::debug::ConsoleContext&) { if (!profiler_) return; CpuProfile* profile = profiler_->StopProfiling(String::Empty(isolate_)); + profiler_active_ = false; if (Shell::HasOnProfileEndListener()) { StringOutputStream out; profile->Serialize(&out); Shell::TriggerOnProfileEndListener(isolate_, out.result()); - Shell::ResetCpuProfiler(); } else { FileOutputStream out(kCpuProfileOutputFilename); profile->Serialize(&out); diff --git a/src/d8/d8-console.h b/src/d8/d8-console.h index a2edacf072..5c87324db8 100644 --- a/src/d8/d8-console.h +++ b/src/d8/d8-console.h @@ -19,6 +19,8 @@ class D8Console : public debug::ConsoleDelegate { explicit D8Console(Isolate* isolate); ~D8Console() override; + CpuProfiler* profiler() const { return profiler_; } + private: void Assert(const debug::ConsoleCallArguments& args, const v8::debug::ConsoleContext&) override; @@ -49,6 +51,7 @@ class D8Console : public debug::ConsoleDelegate { std::map timers_; base::TimeTicks default_timer_; CpuProfiler* profiler_{nullptr}; + bool profiler_active_{false}; }; } // namespace v8 diff --git a/src/d8/d8.cc b/src/d8/d8.cc index 504c218a7e..21d95c2417 100644 --- a/src/d8/d8.cc +++ b/src/d8/d8.cc @@ -467,7 +467,6 @@ const base::TimeTicks Shell::kInitialTicks = base::TimeTicks::Now(); Global Shell::stringify_function_; Global Shell::profile_end_callback_; Global Shell::profile_end_callback_context_; -CpuProfiler* Shell::cpu_profiler_ = nullptr; base::LazyMutex Shell::workers_mutex_; bool Shell::allow_new_workers_ = true; std::unordered_set> Shell::running_workers_; @@ -1766,7 +1765,7 @@ uint64_t Shell::GetTracingTimestampFromPerformanceTimestamp( base::TimeDelta::FromMillisecondsD(performance_timestamp); // See TracingController::CurrentTimestampMicroseconds(). int64_t internal_value = (delta + kInitialTicks).ToInternalValue(); - DCHECK(internal_value >= 0); + DCHECK_GE(internal_value, 0); return internal_value; } @@ -2531,10 +2530,11 @@ bool Shell::HasOnProfileEndListener() { void Shell::ProfilerTriggerSample( const v8::FunctionCallbackInfo& args) { - if (cpu_profiler_) { - Isolate* isolate = args.GetIsolate(); - cpu_profiler_->CollectSample(isolate); - } + Isolate* isolate = args.GetIsolate(); + i::Isolate* i_isolate = reinterpret_cast(isolate); + D8Console* console = + reinterpret_cast(i_isolate->console_delegate()); + if (console->profiler()) console->profiler()->CollectSample(isolate); } void Shell::TriggerOnProfileEndListener(Isolate* isolate, std::string profile) { diff --git a/src/d8/d8.h b/src/d8/d8.h index 5ea0a86f0c..6b519a47c2 100644 --- a/src/d8/d8.h +++ b/src/d8/d8.h @@ -608,12 +608,6 @@ class Shell : public i::AllStatic { static void TriggerOnProfileEndListener(Isolate* isolate, std::string profile); - static void SetCpuProfiler(CpuProfiler* cpu_profiler) { - cpu_profiler_ = cpu_profiler; - } - - static void ResetCpuProfiler() { cpu_profiler_ = nullptr; } - static void Print(const v8::FunctionCallbackInfo& args); static void PrintErr(const v8::FunctionCallbackInfo& args); static void WriteStdout(const v8::FunctionCallbackInfo& args); @@ -753,7 +747,6 @@ class Shell : public i::AllStatic { static Global profile_end_callback_; static Global profile_end_callback_context_; - static CpuProfiler* cpu_profiler_; static const char* stringify_source_; static CounterMap* counter_map_;