1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/core/timing/profiler_group.h"
#include <algorithm>
#include "base/time/time.h"
#include "build/build_config.h"
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/renderer/bindings/core/v8/profiler_trace_builder.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_profiler_init_options.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_profiler_trace.h"
#include "third_party/blink/renderer/core/dom/events/event.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/settings.h"
#include "third_party/blink/renderer/core/timing/profiler.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread_scheduler.h"
#include "third_party/blink/renderer/platform/weborigin/security_origin.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"
#include "v8/include/v8-profiler.h"
#include "v8/include/v8.h"
namespace blink {
namespace {
#if BUILDFLAG(IS_WIN)
// On Windows, assume we have the coarsest possible timer.
static constexpr int kBaseSampleIntervalMs =
base::Time::kMinLowResolutionThresholdMs;
#else
// Default to a 10ms base sampling interval on other platforms.
// TODO(acomminos): Reevaluate based on empirical overhead.
static constexpr int kBaseSampleIntervalMs = 10;
#endif // BUILDFLAG(IS_WIN)
} // namespace
class ProfilerGroup::ProfilingContextObserver
: public GarbageCollected<ProfilingContextObserver>,
public ExecutionContextLifecycleObserver {
public:
ProfilingContextObserver(ProfilerGroup* profiler_group,
ExecutionContext* context)
: ExecutionContextLifecycleObserver(context),
profiler_group_(profiler_group) {}
void ContextDestroyed() override {
DCHECK(profiler_group_);
profiler_group_->OnProfilingContextDestroyed(this);
}
void Trace(Visitor* visitor) const override {
visitor->Trace(profiler_group_);
ExecutionContextLifecycleObserver::Trace(visitor);
}
// Invariant: ProfilerGroup will outlive the tracked execution context, as
// the execution context must live as long as the isolate.
Member<ProfilerGroup> profiler_group_;
};
bool ProfilerGroup::CanProfile(LocalDOMWindow* local_window,
ExceptionState* exception_state,
ReportOptions report_options) {
DCHECK(local_window);
if (!local_window->IsFeatureEnabled(
mojom::blink::DocumentPolicyFeature::kJSProfiling, report_options)) {
if (exception_state) {
exception_state->ThrowDOMException(
DOMExceptionCode::kNotAllowedError,
"JS profiling is disabled by Document Policy.");
}
return false;
}
return true;
}
void ProfilerGroup::InitializeIfEnabled(LocalDOMWindow* local_window) {
if (ProfilerGroup::CanProfile(local_window)) {
auto* profiler_group = ProfilerGroup::From(local_window->GetIsolate());
profiler_group->OnProfilingContextAdded(local_window);
}
}
ProfilerGroup* ProfilerGroup::From(v8::Isolate* isolate) {
auto* isolate_data = V8PerIsolateData::From(isolate);
auto* profiler_group =
reinterpret_cast<ProfilerGroup*>(isolate_data->GetUserData(
V8PerIsolateData::UserData::Key::kProfileGroup));
if (!profiler_group) {
profiler_group = MakeGarbageCollected<ProfilerGroup>(isolate);
isolate_data->SetUserData(V8PerIsolateData::UserData::Key::kProfileGroup,
profiler_group);
}
return profiler_group;
}
base::TimeDelta ProfilerGroup::GetBaseSampleInterval() {
return base::Milliseconds(kBaseSampleIntervalMs);
}
ProfilerGroup::ProfilerGroup(v8::Isolate* isolate)
: isolate_(isolate),
cpu_profiler_(nullptr),
next_profiler_id_(0),
num_active_profilers_(0) {}
void DiscardedSamplesDelegate::Notify() {
if (profiler_group_) {
profiler_group_->DispatchSampleBufferFullEvent(profiler_id_);
}
}
void ProfilerGroup::OnProfilingContextAdded(ExecutionContext* context) {
// Retain an observer for the context's lifetime. During which, keep the V8
// profiler alive.
auto* observer =
MakeGarbageCollected<ProfilingContextObserver>(this, context);
context_observers_.insert(observer);
if (!cpu_profiler_) {
InitV8Profiler();
DCHECK(cpu_profiler_);
}
}
void ProfilerGroup::DispatchSampleBufferFullEvent(String profiler_id) {
for (const auto& profiler : profilers_) {
if (profiler->ProfilerId() == profiler_id) {
profiler->DispatchEvent(
*Event::Create(event_type_names::kSamplebufferfull));
break;
}
}
}
Profiler* ProfilerGroup::CreateProfiler(ScriptState* script_state,
const ProfilerInitOptions& init_options,
base::TimeTicks time_origin,
ExceptionState& exception_state) {
DCHECK_EQ(script_state->GetIsolate(), isolate_);
DCHECK(init_options.hasSampleInterval());
const base::TimeDelta sample_interval =
base::Milliseconds(init_options.sampleInterval());
const int64_t sample_interval_us = sample_interval.InMicroseconds();
if (sample_interval_us < 0 ||
sample_interval_us > std::numeric_limits<int>::max()) {
exception_state.ThrowRangeError("Invalid sample interval");
return nullptr;
}
if (!cpu_profiler_) {
DCHECK(false);
exception_state.ThrowTypeError("Error creating profiler");
return nullptr;
}
String profiler_id = NextProfilerId();
v8::CpuProfilingStatus status = cpu_profiler_->StartProfiling(
V8String(isolate_, profiler_id),
v8::CpuProfilingOptions(
v8::kLeafNodeLineNumbers, init_options.maxBufferSize(),
static_cast<int>(sample_interval_us), script_state->GetContext()),
std::make_unique<DiscardedSamplesDelegate>(this, profiler_id));
switch (status) {
case v8::CpuProfilingStatus::kErrorTooManyProfilers: {
exception_state.ThrowTypeError(
"Reached maximum concurrent amount of profilers");
return nullptr;
}
case v8::CpuProfilingStatus::kAlreadyStarted: {
// Since we increment the profiler id for every invocation of
// StartProfiling, we do not expect to hit kAlreadyStarted status
DCHECK(false);
return nullptr;
}
case v8::CpuProfilingStatus::kStarted: {
// Limit non-crossorigin script frames to the origin that started the
// profiler.
auto* execution_context = ExecutionContext::From(script_state);
scoped_refptr<const SecurityOrigin> source_origin(
execution_context->GetSecurityOrigin());
// The V8 CPU profiler ticks in multiples of the base sampling interval.
// This effectively means that we gather samples at the multiple of the
// base sampling interval that's greater than or equal to the requested
// interval.
int effective_sample_interval_ms =
static_cast<int>(sample_interval.InMilliseconds());
if (effective_sample_interval_ms % kBaseSampleIntervalMs != 0 ||
effective_sample_interval_ms == 0) {
effective_sample_interval_ms +=
(kBaseSampleIntervalMs -
effective_sample_interval_ms % kBaseSampleIntervalMs);
}
auto* profiler = MakeGarbageCollected<Profiler>(
this, script_state, profiler_id, effective_sample_interval_ms,
source_origin, time_origin);
profilers_.insert(profiler);
num_active_profilers_++;
return profiler;
}
}
}
ProfilerGroup::~ProfilerGroup() {
// v8::CpuProfiler should have been torn down by WillBeDestroyed.
DCHECK(!cpu_profiler_);
}
void ProfilerGroup::WillBeDestroyed() {
while (!profilers_.empty()) {
Profiler* profiler = profilers_.begin()->Get();
DCHECK(profiler);
CancelProfiler(profiler);
profiler->RemovedFromProfilerGroup();
DCHECK(profiler->stopped());
DCHECK(!profilers_.Contains(profiler));
}
StopDetachedProfilers();
if (cpu_profiler_)
TeardownV8Profiler();
}
void ProfilerGroup::Trace(Visitor* visitor) const {
visitor->Trace(profilers_);
visitor->Trace(context_observers_);
V8PerIsolateData::UserData::Trace(visitor);
}
void ProfilerGroup::OnProfilingContextDestroyed(
ProfilingContextObserver* observer) {
context_observers_.erase(observer);
if (context_observers_.size() == 0) {
WillBeDestroyed();
}
}
void ProfilerGroup::InitV8Profiler() {
DCHECK(!cpu_profiler_);
DCHECK_EQ(num_active_profilers_, 0);
cpu_profiler_ =
v8::CpuProfiler::New(isolate_, v8::kStandardNaming, v8::kEagerLogging);
#if BUILDFLAG(IS_WIN)
// Avoid busy-waiting on Windows, clamping us to the system clock interrupt
// interval in the worst case.
cpu_profiler_->SetUsePreciseSampling(false);
#endif // BUILDFLAG(IS_WIN)
cpu_profiler_->SetSamplingInterval(kBaseSampleIntervalMs *
base::Time::kMicrosecondsPerMillisecond);
}
void ProfilerGroup::TeardownV8Profiler() {
DCHECK(cpu_profiler_);
DCHECK_EQ(num_active_profilers_, 0);
cpu_profiler_->Dispose();
cpu_profiler_ = nullptr;
}
void ProfilerGroup::StopProfiler(
ScriptState* script_state,
Profiler* profiler,
ScriptPromiseResolver<ProfilerTrace>* resolver) {
DCHECK(cpu_profiler_);
DCHECK(!profiler->stopped());
v8::Local<v8::String> profiler_id =
V8String(isolate_, profiler->ProfilerId());
auto* profile = cpu_profiler_->StopProfiling(profiler_id);
auto* trace = ProfilerTraceBuilder::FromProfile(
script_state, profile, profiler->SourceOrigin(), profiler->TimeOrigin());
resolver->Resolve(trace);
if (profile)
profile->Delete();
profilers_.erase(profiler);
--num_active_profilers_;
}
void ProfilerGroup::CancelProfiler(Profiler* profiler) {
DCHECK(cpu_profiler_);
DCHECK(!profiler->stopped());
profilers_.erase(profiler);
CancelProfilerImpl(profiler->ProfilerId());
}
void ProfilerGroup::CancelProfilerAsync(ScriptState* script_state,
Profiler* profiler) {
DCHECK(IsMainThread());
DCHECK(cpu_profiler_);
DCHECK(!profiler->stopped());
profilers_.erase(profiler);
// register the profiler to be cleaned up in case its associated context
// gets destroyed before the cleanup task is executed.
detached_profiler_ids_.push_back(profiler->ProfilerId());
// Since it's possible for the profiler to get destructed along with its
// associated context, dispatch a task to cleanup context-independent isolate
// resources (rather than use the context's task runner).
ThreadScheduler::Current()->V8TaskRunner()->PostTask(
FROM_HERE, WTF::BindOnce(&ProfilerGroup::StopDetachedProfiler,
WrapPersistent(this), profiler->ProfilerId()));
}
void ProfilerGroup::StopDetachedProfiler(String profiler_id) {
DCHECK(IsMainThread());
// we use a vector instead of a map because the expected number of profiler
// is expected to be very small
auto it = std::ranges::find(detached_profiler_ids_, profiler_id);
if (it == detached_profiler_ids_.end()) {
// Profiler already stopped
return;
}
CancelProfilerImpl(profiler_id);
detached_profiler_ids_.erase(it);
}
void ProfilerGroup::StopDetachedProfilers() {
DCHECK(IsMainThread());
for (auto& detached_profiler_id : detached_profiler_ids_) {
CancelProfilerImpl(detached_profiler_id);
}
detached_profiler_ids_.clear();
}
void ProfilerGroup::CancelProfilerImpl(String profiler_id) {
if (!cpu_profiler_)
return;
v8::HandleScope scope(isolate_);
v8::Local<v8::String> v8_profiler_id = V8String(isolate_, profiler_id);
auto* profile = cpu_profiler_->StopProfiling(v8_profiler_id);
profile->Delete();
--num_active_profilers_;
}
String ProfilerGroup::NextProfilerId() {
auto id = String::Format("blink::Profiler[%d]", next_profiler_id_);
++next_profiler_id_;
return id;
}
} // namespace blink
|