tracing-cpu-profiler.cc 2 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/execution/isolate.h"
8
#include "src/init/v8.h"
9 10
#include "src/profiler/cpu-profiler.h"
#include "src/tracing/trace-event.h"
11 12 13 14

namespace v8 {
namespace internal {

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

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

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

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

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

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

}  // namespace internal
}  // namespace v8