v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
tracing-cpu-profiler.cc
Go to the documentation of this file.
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
6
8#include "src/init/v8.h"
11
12namespace v8 {
13namespace internal {
14
16 : isolate_(isolate), profiling_enabled_(false) {
17#if defined(V8_USE_PERFETTO)
18 TrackEvent::AddSessionObserver(this);
19 // Fire the observer if tracing is already in progress.
20 if (TrackEvent::IsEnabled()) OnStart({});
21#else
23#endif
24}
25
28#if defined(V8_USE_PERFETTO)
29 TrackEvent::RemoveSessionObserver(this);
30#else
32 this);
33#endif
34}
35
36#if defined(V8_USE_PERFETTO)
37void TracingCpuProfilerImpl::OnStart(
38 const perfetto::DataSourceBase::StartArgs&) {
39#else
41#endif
42 bool enabled;
44 TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler"), &enabled);
45 if (!enabled) return;
46 profiling_enabled_ = true;
48 [](v8::Isolate*, void* data) {
49 reinterpret_cast<TracingCpuProfilerImpl*>(data)->StartProfiling();
50 },
51 this);
52}
53
54namespace {
55class RunInterruptsTask : public v8::Task {
56 public:
57 explicit RunInterruptsTask(v8::internal::Isolate* isolate)
58 : isolate_(isolate) {}
59 void Run() override { isolate_->stack_guard()->HandleInterrupts(); }
60
61 private:
63};
64} // namespace
65
66#if defined(V8_USE_PERFETTO)
67void TracingCpuProfilerImpl::OnStop(const perfetto::DataSourceBase::StopArgs&) {
68#else
70#endif
72 if (!profiling_enabled_) return;
73 profiling_enabled_ = false;
75 [](v8::Isolate*, void* data) {
76 reinterpret_cast<TracingCpuProfilerImpl*>(data)->StopProfiling();
77 },
78 this);
79 // It could be a long time until the Isolate next runs any JS which could be
80 // interrupted, and we'd rather not leave the sampler thread running during
81 // that time, so also post a task to run any interrupts.
83 ->GetForegroundTaskRunner(reinterpret_cast<v8::Isolate*>(isolate_))
84 ->PostTask(std::make_unique<RunInterruptsTask>(isolate_));
85}
86
89 if (!profiling_enabled_ || profiler_) return;
90 int sampling_interval_us = 100;
92 profiler_->set_sampling_interval(
93 base::TimeDelta::FromMicroseconds(sampling_interval_us));
94 profiler_->StartProfiling("", {kLeafNodeLineNumbers});
95}
96
99 if (!profiler_) return;
100 profiler_->StopProfiling("");
101 profiler_.reset();
102}
103
104} // namespace internal
105} // namespace v8
Isolate * isolate_
union v8::internal::@341::BuiltinMetadata::KindSpecificData data
virtual TracingController * GetTracingController()=0
std::shared_ptr< v8::TaskRunner > GetForegroundTaskRunner(Isolate *isolate)
void PostTask(std::unique_ptr< Task > task, const SourceLocation &location=SourceLocation::Current())
Definition v8-platform.h:82
virtual void AddTraceStateObserver(TraceStateObserver *)
virtual void RemoveTraceStateObserver(TraceStateObserver *)
static constexpr TimeDelta FromMicroseconds(int64_t microseconds)
Definition time.h:87
void RequestInterrupt(InterruptCallback callback, void *data)
Definition isolate.cc:1960
std::unique_ptr< CpuProfiler > profiler_
static V8_EXPORT_PRIVATE v8::Platform * GetCurrentPlatform()
Definition v8.cc:282
@ kLeafNodeLineNumbers
@ kDebugNaming
#define TRACE_DISABLED_BY_DEFAULT(name)
#define TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, ret)