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 62422 : TracingCpuProfilerImpl::TracingCpuProfilerImpl(Isolate* isolate)
15 124844 : : isolate_(isolate), profiling_enabled_(false) {
16 62422 : V8::GetCurrentPlatform()->GetTracingController()->AddTraceStateObserver(this);
17 62422 : }
18 :
19 249626 : TracingCpuProfilerImpl::~TracingCpuProfilerImpl() {
20 62406 : StopProfiling();
21 124812 : V8::GetCurrentPlatform()->GetTracingController()->RemoveTraceStateObserver(
22 124812 : this);
23 124813 : }
24 :
25 6 : void TracingCpuProfilerImpl::OnTraceEnabled() {
26 : bool enabled;
27 12 : TRACE_EVENT_CATEGORY_GROUP_ENABLED(
28 : TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler"), &enabled);
29 6 : if (!enabled) return;
30 5 : profiling_enabled_ = true;
31 5 : isolate_->RequestInterrupt(
32 5 : [](v8::Isolate*, void* data) {
33 5 : reinterpret_cast<TracingCpuProfilerImpl*>(data)->StartProfiling();
34 5 : },
35 5 : this);
36 : }
37 :
38 5 : void TracingCpuProfilerImpl::OnTraceDisabled() {
39 5 : base::MutexGuard lock(&mutex_);
40 5 : if (!profiling_enabled_) return;
41 5 : profiling_enabled_ = false;
42 5 : isolate_->RequestInterrupt(
43 5 : [](v8::Isolate*, void* data) {
44 5 : reinterpret_cast<TracingCpuProfilerImpl*>(data)->StopProfiling();
45 5 : },
46 5 : this);
47 : }
48 :
49 5 : void TracingCpuProfilerImpl::StartProfiling() {
50 5 : base::MutexGuard lock(&mutex_);
51 10 : if (!profiling_enabled_ || profiler_) return;
52 : bool enabled;
53 10 : TRACE_EVENT_CATEGORY_GROUP_ENABLED(
54 : TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler.hires"), &enabled);
55 5 : int sampling_interval_us = enabled ? 100 : 1000;
56 5 : profiler_.reset(new CpuProfiler(isolate_));
57 5 : profiler_->set_sampling_interval(
58 5 : base::TimeDelta::FromMicroseconds(sampling_interval_us));
59 5 : profiler_->StartProfiling("", true);
60 : }
61 :
62 62411 : void TracingCpuProfilerImpl::StopProfiling() {
63 62411 : base::MutexGuard lock(&mutex_);
64 62412 : if (!profiler_) return;
65 5 : profiler_->StopProfiling("");
66 5 : profiler_.reset();
67 : }
68 :
69 : } // namespace internal
70 121996 : } // namespace v8
|