5 #include "src/profiler/tracing-cpu-profiler.h" 7 #include "src/profiler/cpu-profiler.h" 8 #include "src/tracing/trace-event.h" 14 TracingCpuProfilerImpl::TracingCpuProfilerImpl(Isolate* isolate)
15 : isolate_(isolate), profiling_enabled_(false) {
17 TRACE_EVENT_WARMUP_CATEGORY(TRACE_DISABLED_BY_DEFAULT(
"v8.cpu_profiler"));
18 TRACE_EVENT_WARMUP_CATEGORY(
19 TRACE_DISABLED_BY_DEFAULT(
"v8.cpu_profiler.hires"));
20 V8::GetCurrentPlatform()->GetTracingController()->AddTraceStateObserver(
this);
23 TracingCpuProfilerImpl::~TracingCpuProfilerImpl() {
25 V8::GetCurrentPlatform()->GetTracingController()->RemoveTraceStateObserver(
29 void TracingCpuProfilerImpl::OnTraceEnabled() {
31 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
32 TRACE_DISABLED_BY_DEFAULT(
"v8.cpu_profiler"), &enabled);
34 profiling_enabled_ =
true;
35 isolate_->RequestInterrupt(
36 [](v8::Isolate*,
void* data) {
37 reinterpret_cast<TracingCpuProfilerImpl*
>(data)->StartProfiling();
42 void TracingCpuProfilerImpl::OnTraceDisabled() {
43 base::MutexGuard lock(&mutex_);
44 if (!profiling_enabled_)
return;
45 profiling_enabled_ =
false;
46 isolate_->RequestInterrupt(
47 [](v8::Isolate*,
void* data) {
48 reinterpret_cast<TracingCpuProfilerImpl*
>(data)->StopProfiling();
53 void TracingCpuProfilerImpl::StartProfiling() {
54 base::MutexGuard lock(&mutex_);
55 if (!profiling_enabled_ || profiler_)
return;
57 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
58 TRACE_DISABLED_BY_DEFAULT(
"v8.cpu_profiler.hires"), &enabled);
59 int sampling_interval_us = enabled ? 100 : 1000;
60 profiler_.reset(
new CpuProfiler(isolate_));
61 profiler_->set_sampling_interval(
62 base::TimeDelta::FromMicroseconds(sampling_interval_us));
63 profiler_->StartProfiling(
"",
true);
66 void TracingCpuProfilerImpl::StopProfiling() {
67 base::MutexGuard lock(&mutex_);
68 if (!profiler_)
return;
69 profiler_->StopProfiling(
"");