Line data Source code
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/profiler/cpu-profiler.h"
8 : #include "src/tracing/trace-event.h"
9 : #include "src/v8.h"
10 :
11 : namespace v8 {
12 : namespace internal {
13 :
14 62883 : TracingCpuProfilerImpl::TracingCpuProfilerImpl(Isolate* isolate)
15 62883 : : isolate_(isolate), profiling_enabled_(false) {
16 62883 : V8::GetCurrentPlatform()->GetTracingController()->AddTraceStateObserver(this);
17 62883 : }
18 :
19 188604 : TracingCpuProfilerImpl::~TracingCpuProfilerImpl() {
20 62868 : StopProfiling();
21 62868 : V8::GetCurrentPlatform()->GetTracingController()->RemoveTraceStateObserver(
22 62868 : this);
23 125736 : }
24 :
25 1 : void TracingCpuProfilerImpl::OnTraceEnabled() {
26 : bool enabled;
27 2 : TRACE_EVENT_CATEGORY_GROUP_ENABLED(
28 : TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler"), &enabled);
29 2 : if (!enabled) return;
30 0 : profiling_enabled_ = true;
31 : isolate_->RequestInterrupt(
32 0 : [](v8::Isolate*, void* data) {
33 0 : reinterpret_cast<TracingCpuProfilerImpl*>(data)->StartProfiling();
34 0 : },
35 0 : this);
36 : }
37 :
38 0 : void TracingCpuProfilerImpl::OnTraceDisabled() {
39 0 : base::MutexGuard lock(&mutex_);
40 0 : if (!profiling_enabled_) return;
41 0 : profiling_enabled_ = false;
42 : isolate_->RequestInterrupt(
43 0 : [](v8::Isolate*, void* data) {
44 0 : reinterpret_cast<TracingCpuProfilerImpl*>(data)->StopProfiling();
45 0 : },
46 0 : this);
47 : }
48 :
49 0 : void TracingCpuProfilerImpl::StartProfiling() {
50 0 : base::MutexGuard lock(&mutex_);
51 0 : if (!profiling_enabled_ || profiler_) return;
52 : bool enabled;
53 0 : TRACE_EVENT_CATEGORY_GROUP_ENABLED(
54 : TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler.hires"), &enabled);
55 0 : int sampling_interval_us = enabled ? 100 : 1000;
56 0 : profiler_.reset(new CpuProfiler(isolate_));
57 : profiler_->set_sampling_interval(
58 0 : base::TimeDelta::FromMicroseconds(sampling_interval_us));
59 0 : profiler_->StartProfiling("", true);
60 : }
61 :
62 62868 : void TracingCpuProfilerImpl::StopProfiling() {
63 62868 : base::MutexGuard lock(&mutex_);
64 125736 : if (!profiler_) return;
65 0 : profiler_->StopProfiling("");
66 : profiler_.reset();
67 : }
68 :
69 : } // namespace internal
70 183867 : } // namespace v8
|