tracing-cpu-profiler.cc 2.1 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/profiler/tracing-cpu-profiler.h"

7
#include "src/init/v8.h"
8 9
#include "src/profiler/cpu-profiler.h"
#include "src/tracing/trace-event.h"
10 11 12 13

namespace v8 {
namespace internal {

14 15
TracingCpuProfilerImpl::TracingCpuProfilerImpl(Isolate* isolate)
    : isolate_(isolate), profiling_enabled_(false) {
16
  V8::GetCurrentPlatform()->GetTracingController()->AddTraceStateObserver(this);
17 18 19 20
}

TracingCpuProfilerImpl::~TracingCpuProfilerImpl() {
  StopProfiling();
21 22
  V8::GetCurrentPlatform()->GetTracingController()->RemoveTraceStateObserver(
      this);
23 24 25
}

void TracingCpuProfilerImpl::OnTraceEnabled() {
26 27 28 29
  bool enabled;
  TRACE_EVENT_CATEGORY_GROUP_ENABLED(
      TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler"), &enabled);
  if (!enabled) return;
30 31 32 33 34 35 36
  profiling_enabled_ = true;
  isolate_->RequestInterrupt(
      [](v8::Isolate*, void* data) {
        reinterpret_cast<TracingCpuProfilerImpl*>(data)->StartProfiling();
      },
      this);
}
37

38
void TracingCpuProfilerImpl::OnTraceDisabled() {
39
  base::MutexGuard lock(&mutex_);
40 41 42 43 44 45 46 47 48 49
  if (!profiling_enabled_) return;
  profiling_enabled_ = false;
  isolate_->RequestInterrupt(
      [](v8::Isolate*, void* data) {
        reinterpret_cast<TracingCpuProfilerImpl*>(data)->StopProfiling();
      },
      this);
}

void TracingCpuProfilerImpl::StartProfiling() {
50
  base::MutexGuard lock(&mutex_);
51
  if (!profiling_enabled_ || profiler_) return;
52 53 54 55
  bool enabled;
  TRACE_EVENT_CATEGORY_GROUP_ENABLED(
      TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler.hires"), &enabled);
  int sampling_interval_us = enabled ? 100 : 1000;
56
  profiler_.reset(new CpuProfiler(isolate_, kDebugNaming));
57 58
  profiler_->set_sampling_interval(
      base::TimeDelta::FromMicroseconds(sampling_interval_us));
59
  profiler_->StartProfiling("", {kLeafNodeLineNumbers});
60 61 62
}

void TracingCpuProfilerImpl::StopProfiling() {
63
  base::MutexGuard lock(&mutex_);
64 65 66 67
  if (!profiler_) return;
  profiler_->StopProfiling("");
  profiler_.reset();
}
68 69 70

}  // namespace internal
}  // namespace v8