Commit 1aebd294 authored by Etienne Pierre-doray's avatar Etienne Pierre-doray Committed by Commit Bot

[tracing]: Add v8 unwinder to tracing sampler.

Track global pointer to main thread sampler to access from
ChromeContentRendererClient.

Bug: 1077046
Change-Id: Icf575c7a03905c844ce2bd8065129a0df341a9a6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2251087Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarMike Wittman <wittman@chromium.org>
Reviewed-by: default avataroysteine <oysteine@chromium.org>
Commit-Queue: Etienne Pierre-Doray <etiennep@chromium.org>
Cr-Commit-Position: refs/heads/master@{#783651}
parent cf2defe4
...@@ -70,6 +70,7 @@ include_rules = [ ...@@ -70,6 +70,7 @@ include_rules = [
"+ppapi/shared_impl", "+ppapi/shared_impl",
"+services/network/public/cpp", "+services/network/public/cpp",
"+services/network/public/mojom", "+services/network/public/mojom",
"+services/tracing/public/cpp",
"+skia", "+skia",
"+storage/common", "+storage/common",
"+third_party/blink/public/mojom", "+third_party/blink/public/mojom",
......
...@@ -128,6 +128,7 @@ ...@@ -128,6 +128,7 @@
#include "printing/buildflags/buildflags.h" #include "printing/buildflags/buildflags.h"
#include "services/network/public/cpp/is_potentially_trustworthy.h" #include "services/network/public/cpp/is_potentially_trustworthy.h"
#include "services/service_manager/public/cpp/interface_provider.h" #include "services/service_manager/public/cpp/interface_provider.h"
#include "services/tracing/public/cpp/stack_sampling/tracing_sampler_profiler.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h" #include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/common/features.h" #include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/privacy_budget/identifiability_study_settings.h" #include "third_party/blink/public/common/privacy_budget/identifiability_study_settings.h"
...@@ -356,6 +357,11 @@ void ChromeContentRendererClient::RenderThreadStarted() { ...@@ -356,6 +357,11 @@ void ChromeContentRendererClient::RenderThreadStarted() {
main_thread_profiler_->SetAuxUnwinderFactory(base::BindRepeating( main_thread_profiler_->SetAuxUnwinderFactory(base::BindRepeating(
&CreateV8Unwinder, base::Unretained(v8::Isolate::GetCurrent()))); &CreateV8Unwinder, base::Unretained(v8::Isolate::GetCurrent())));
// In the case of single process mode, the v8 unwinding will not work.
tracing::TracingSamplerProfiler::SetAuxUnwinderFactoryOnMainThread(
base::BindRepeating(&CreateV8Unwinder,
base::Unretained(v8::Isolate::GetCurrent())));
thread->SetRendererProcessType( thread->SetRendererProcessType(
IsStandaloneContentExtensionProcess() IsStandaloneContentExtensionProcess()
? blink::scheduler::WebRendererProcessType::kExtensionRenderer ? blink::scheduler::WebRendererProcessType::kExtensionRenderer
......
...@@ -58,6 +58,9 @@ namespace tracing { ...@@ -58,6 +58,9 @@ namespace tracing {
namespace { namespace {
// Pointer to the main thread instance, if any.
TracingSamplerProfiler* g_main_thread_instance = nullptr;
class TracingSamplerProfilerDataSource class TracingSamplerProfilerDataSource
: public PerfettoTracedProcess::DataSourceBase { : public PerfettoTracedProcess::DataSourceBase {
public: public:
...@@ -553,6 +556,11 @@ TracingSamplerProfiler::CreateOnMainThread() { ...@@ -553,6 +556,11 @@ TracingSamplerProfiler::CreateOnMainThread() {
InitializeLoaderLockSampling(); InitializeLoaderLockSampling();
profiler->EnableLoaderLockSampling(); profiler->EnableLoaderLockSampling();
#endif #endif
// If running in single process mode, there may be multiple "main thread"
// profilers created. In this case, we assume the first created one is the
// browser one.
if (!g_main_thread_instance)
g_main_thread_instance = profiler.get();
return profiler; return profiler;
} }
...@@ -577,6 +585,12 @@ void TracingSamplerProfiler::RegisterDataSource() { ...@@ -577,6 +585,12 @@ void TracingSamplerProfiler::RegisterDataSource() {
TracingSamplerProfilerDataSource::Get()); TracingSamplerProfilerDataSource::Get());
} }
void TracingSamplerProfiler::SetAuxUnwinderFactoryOnMainThread(
const base::RepeatingCallback<std::unique_ptr<base::Unwinder>()>& factory) {
DCHECK(g_main_thread_instance);
g_main_thread_instance->SetAuxUnwinderFactory(factory);
}
// static // static
void TracingSamplerProfiler::StartTracingForTesting( void TracingSamplerProfiler::StartTracingForTesting(
PerfettoProducer* producer) { PerfettoProducer* producer) {
...@@ -615,6 +629,16 @@ TracingSamplerProfiler::TracingSamplerProfiler( ...@@ -615,6 +629,16 @@ TracingSamplerProfiler::TracingSamplerProfiler(
TracingSamplerProfiler::~TracingSamplerProfiler() { TracingSamplerProfiler::~TracingSamplerProfiler() {
TracingSamplerProfilerDataSource::Get()->UnregisterProfiler(this); TracingSamplerProfilerDataSource::Get()->UnregisterProfiler(this);
if (g_main_thread_instance == this)
g_main_thread_instance = nullptr;
}
void TracingSamplerProfiler::SetAuxUnwinderFactory(
const base::RepeatingCallback<std::unique_ptr<base::Unwinder>()>& factory) {
base::AutoLock lock(lock_);
aux_unwinder_factory_ = factory;
if (profiler_)
profiler_->AddAuxUnwinder(aux_unwinder_factory_.Run());
} }
void TracingSamplerProfiler::SetSampleCallbackForTesting( void TracingSamplerProfiler::SetSampleCallbackForTesting(
...@@ -675,6 +699,8 @@ void TracingSamplerProfiler::StartTracing( ...@@ -675,6 +699,8 @@ void TracingSamplerProfiler::StartTracing(
#else // defined(OS_ANDROID) #else // defined(OS_ANDROID)
profiler_ = std::make_unique<base::StackSamplingProfiler>( profiler_ = std::make_unique<base::StackSamplingProfiler>(
sampled_thread_token_, params, std::move(profile_builder)); sampled_thread_token_, params, std::move(profile_builder));
if (aux_unwinder_factory_)
profiler_->AddAuxUnwinder(aux_unwinder_factory_.Run());
profiler_->Start(); profiler_->Start();
#endif // defined(OS_ANDROID) #endif // defined(OS_ANDROID)
} }
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/profiler/sampling_profiler_thread_token.h" #include "base/profiler/sampling_profiler_thread_token.h"
#include "base/profiler/stack_sampling_profiler.h" #include "base/profiler/stack_sampling_profiler.h"
#include "base/profiler/unwinder.h"
#include "base/sequence_checker.h" #include "base/sequence_checker.h"
#include "base/threading/platform_thread.h" #include "base/threading/platform_thread.h"
#include "build/build_config.h" #include "build/build_config.h"
...@@ -150,6 +151,12 @@ class COMPONENT_EXPORT(TRACING_CPP) TracingSamplerProfiler { ...@@ -150,6 +151,12 @@ class COMPONENT_EXPORT(TRACING_CPP) TracingSamplerProfiler {
// Registers the TracingSamplerProfiler as a Perfetto data source // Registers the TracingSamplerProfiler as a Perfetto data source
static void RegisterDataSource(); static void RegisterDataSource();
// Sets a callback to create auxiliary unwinders on the main thread profiler,
// for handling additional, non-native-code unwind scenarios.
static void SetAuxUnwinderFactoryOnMainThread(
const base::RepeatingCallback<std::unique_ptr<base::Unwinder>()>&
factory);
// For tests. // For tests.
static void SetupStartupTracingForTesting(); static void SetupStartupTracingForTesting();
static void DeleteOnChildThreadForTesting(); static void DeleteOnChildThreadForTesting();
...@@ -173,6 +180,13 @@ class COMPONENT_EXPORT(TRACING_CPP) TracingSamplerProfiler { ...@@ -173,6 +180,13 @@ class COMPONENT_EXPORT(TRACING_CPP) TracingSamplerProfiler {
base::SamplingProfilerThreadToken sampled_thread_token); base::SamplingProfilerThreadToken sampled_thread_token);
virtual ~TracingSamplerProfiler(); virtual ~TracingSamplerProfiler();
// Sets a callback to create auxiliary unwinders, for handling additional,
// non-native-code unwind scenarios. Currently used to support
// unwinding V8 JavaScript frames.
void SetAuxUnwinderFactory(
const base::RepeatingCallback<std::unique_ptr<base::Unwinder>()>&
factory);
// The given callback will be called for every received sample, and can be // The given callback will be called for every received sample, and can be
// called on any thread. Must be called before tracing is started. // called on any thread. Must be called before tracing is started.
void SetSampleCallbackForTesting( void SetSampleCallbackForTesting(
...@@ -185,6 +199,9 @@ class COMPONENT_EXPORT(TRACING_CPP) TracingSamplerProfiler { ...@@ -185,6 +199,9 @@ class COMPONENT_EXPORT(TRACING_CPP) TracingSamplerProfiler {
private: private:
const base::SamplingProfilerThreadToken sampled_thread_token_; const base::SamplingProfilerThreadToken sampled_thread_token_;
base::RepeatingCallback<std::unique_ptr<base::Unwinder>()>
aux_unwinder_factory_;
base::Lock lock_; base::Lock lock_;
std::unique_ptr<base::StackSamplingProfiler> profiler_; // under |lock_| std::unique_ptr<base::StackSamplingProfiler> profiler_; // under |lock_|
TracingProfileBuilder* profile_builder_ = nullptr; TracingProfileBuilder* profile_builder_ = nullptr;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment