• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/profiler/tracing-cpu-profiler.h"
6 
7 #include "src/execution/isolate.h"
8 #include "src/init/v8.h"
9 #include "src/profiler/cpu-profiler.h"
10 #include "src/tracing/trace-event.h"
11 
12 namespace v8 {
13 namespace internal {
14 
TracingCpuProfilerImpl(Isolate * isolate)15 TracingCpuProfilerImpl::TracingCpuProfilerImpl(Isolate* isolate)
16     : isolate_(isolate), profiling_enabled_(false) {
17   V8::GetCurrentPlatform()->GetTracingController()->AddTraceStateObserver(this);
18 }
19 
~TracingCpuProfilerImpl()20 TracingCpuProfilerImpl::~TracingCpuProfilerImpl() {
21   StopProfiling();
22   V8::GetCurrentPlatform()->GetTracingController()->RemoveTraceStateObserver(
23       this);
24 }
25 
OnTraceEnabled()26 void TracingCpuProfilerImpl::OnTraceEnabled() {
27   bool enabled;
28   TRACE_EVENT_CATEGORY_GROUP_ENABLED(
29       TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler"), &enabled);
30   if (!enabled) return;
31   profiling_enabled_ = true;
32   isolate_->RequestInterrupt(
33       [](v8::Isolate*, void* data) {
34         reinterpret_cast<TracingCpuProfilerImpl*>(data)->StartProfiling();
35       },
36       this);
37 }
38 
OnTraceDisabled()39 void TracingCpuProfilerImpl::OnTraceDisabled() {
40   base::MutexGuard lock(&mutex_);
41   if (!profiling_enabled_) return;
42   profiling_enabled_ = false;
43   isolate_->RequestInterrupt(
44       [](v8::Isolate*, void* data) {
45         reinterpret_cast<TracingCpuProfilerImpl*>(data)->StopProfiling();
46       },
47       this);
48 }
49 
StartProfiling()50 void TracingCpuProfilerImpl::StartProfiling() {
51   base::MutexGuard lock(&mutex_);
52   if (!profiling_enabled_ || profiler_) return;
53   int sampling_interval_us = 100;
54   profiler_.reset(new CpuProfiler(isolate_, kDebugNaming));
55   profiler_->set_sampling_interval(
56       base::TimeDelta::FromMicroseconds(sampling_interval_us));
57   profiler_->StartProfiling("", {kLeafNodeLineNumbers});
58 }
59 
StopProfiling()60 void TracingCpuProfilerImpl::StopProfiling() {
61   base::MutexGuard lock(&mutex_);
62   if (!profiler_) return;
63   profiler_->StopProfiling("");
64   profiler_.reset();
65 }
66 
67 }  // namespace internal
68 }  // namespace v8
69