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 :
13 5 : std::unique_ptr<TracingCpuProfiler> TracingCpuProfiler::Create(
14 : v8::Isolate* isolate) {
15 : return std::unique_ptr<TracingCpuProfiler>(
16 : new internal::TracingCpuProfilerImpl(
17 10 : reinterpret_cast<internal::Isolate*>(isolate)));
18 : }
19 :
20 : namespace internal {
21 :
22 5 : TracingCpuProfilerImpl::TracingCpuProfilerImpl(Isolate* isolate)
23 5 : : isolate_(isolate), profiling_enabled_(false) {
24 : // Make sure tracing system notices profiler categories.
25 5 : TRACE_EVENT_WARMUP_CATEGORY(TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler"));
26 5 : TRACE_EVENT_WARMUP_CATEGORY(
27 : TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler.hires"));
28 5 : V8::GetCurrentPlatform()->GetTracingController()->AddTraceStateObserver(this);
29 5 : }
30 :
31 15 : TracingCpuProfilerImpl::~TracingCpuProfilerImpl() {
32 5 : StopProfiling();
33 5 : V8::GetCurrentPlatform()->GetTracingController()->RemoveTraceStateObserver(
34 5 : this);
35 10 : }
36 :
37 5 : void TracingCpuProfilerImpl::OnTraceEnabled() {
38 : bool enabled;
39 10 : TRACE_EVENT_CATEGORY_GROUP_ENABLED(
40 : TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler"), &enabled);
41 10 : if (!enabled) return;
42 5 : profiling_enabled_ = true;
43 : isolate_->RequestInterrupt(
44 5 : [](v8::Isolate*, void* data) {
45 5 : reinterpret_cast<TracingCpuProfilerImpl*>(data)->StartProfiling();
46 5 : },
47 5 : this);
48 : }
49 :
50 5 : void TracingCpuProfilerImpl::OnTraceDisabled() {
51 5 : base::LockGuard<base::Mutex> lock(&mutex_);
52 10 : if (!profiling_enabled_) return;
53 5 : profiling_enabled_ = false;
54 : isolate_->RequestInterrupt(
55 5 : [](v8::Isolate*, void* data) {
56 5 : reinterpret_cast<TracingCpuProfilerImpl*>(data)->StopProfiling();
57 5 : },
58 5 : this);
59 : }
60 :
61 5 : void TracingCpuProfilerImpl::StartProfiling() {
62 5 : base::LockGuard<base::Mutex> lock(&mutex_);
63 15 : if (!profiling_enabled_ || profiler_) return;
64 : bool enabled;
65 10 : TRACE_EVENT_CATEGORY_GROUP_ENABLED(
66 : TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler.hires"), &enabled);
67 5 : int sampling_interval_us = enabled ? 100 : 1000;
68 5 : profiler_.reset(new CpuProfiler(isolate_));
69 : profiler_->set_sampling_interval(
70 10 : base::TimeDelta::FromMicroseconds(sampling_interval_us));
71 5 : profiler_->StartProfiling("", true);
72 : }
73 :
74 10 : void TracingCpuProfilerImpl::StopProfiling() {
75 10 : base::LockGuard<base::Mutex> lock(&mutex_);
76 20 : if (!profiler_) return;
77 5 : profiler_->StopProfiling("");
78 : profiler_.reset();
79 : }
80 :
81 : } // namespace internal
82 : } // namespace v8
|