diff --git a/base/profiler/stack_sampling_profiler.cc b/base/profiler/stack_sampling_profiler.cc index 773de388cb12f5..5561bb9bd6470b 100644 --- a/base/profiler/stack_sampling_profiler.cc +++ b/base/profiler/stack_sampling_profiler.cc @@ -28,6 +28,15 @@ #include "base/threading/thread_task_runner_handle.h" #include "base/time/time.h" #include "base/trace_event/base_tracing.h" +#include "build/build_config.h" + +#if defined(OS_WIN) +#include "base/win/static_constants.h" +#endif + +#if defined(OS_MACOSX) +#include "base/mac/mac_util.h" +#endif namespace base { @@ -729,6 +738,31 @@ TimeTicks StackSamplingProfiler::TestPeer::GetNextSampleTime( now); } +// static +// The profiler is currently only implemented for Windows x64 and MacOSX. +// TODO(https://crbug.com/1004855): enable for Android arm. +bool StackSamplingProfiler::IsSupported() { +#if (defined(OS_WIN) && defined(ARCH_CPU_X86_64)) || \ + (defined(OS_MACOSX) && !defined(OS_IOS)) +#if defined(OS_MACOSX) + // TODO(https://crbug.com/1098119): Fix unwinding on OS X 10.16. The OS + // has moved all system libraries into the dyld shared cache and this + // seems to break the sampling profiler. + if (base::mac::IsOSLaterThan10_15_DontCallThis()) + return false; +#endif +#if defined(OS_WIN) + // Do not start the profiler when Application Verifier is in use; running them + // simultaneously can cause crashes and has no known use case. + if (GetModuleHandleA(base::win::kApplicationVerifierDllName)) + return false; +#endif + return true; +#else + return false; +#endif +} + StackSamplingProfiler::StackSamplingProfiler( SamplingProfilerThreadToken thread_token, const SamplingParams& params, diff --git a/base/profiler/stack_sampling_profiler.h b/base/profiler/stack_sampling_profiler.h index 12c432669d32c3..b4cf5ae31ce0b2 100644 --- a/base/profiler/stack_sampling_profiler.h +++ b/base/profiler/stack_sampling_profiler.h @@ -75,6 +75,10 @@ class BASE_EXPORT StackSamplingProfiler { TimeDelta sampling_interval = TimeDelta::FromMilliseconds(100); }; + // Returns true if the profiler is supported on the current platform + // configuration. + static bool IsSupported(); + // Creates a profiler for the specified thread. |unwinders| is required on // Android since the unwinder is provided outside StackSamplingProfiler, but // must be empty on other platforms. When attempting to unwind, the relative diff --git a/chrome/common/profiler/stack_sampling_configuration.cc b/chrome/common/profiler/stack_sampling_configuration.cc index 647dbf08ec9fbb..d91a988d8410cb 100644 --- a/chrome/common/profiler/stack_sampling_configuration.cc +++ b/chrome/common/profiler/stack_sampling_configuration.cc @@ -37,10 +37,7 @@ namespace { base::LazyInstance::Leaky g_configuration = LAZY_INSTANCE_INITIALIZER; -// The profiler is currently only implemented for Windows x64 and Mac x64. -// TODO(https://crbug.com/1004855): enable for Android arm. -bool IsProfilerSupportedForPlatformAndChannel() { -#if (defined(OS_WIN) && defined(ARCH_CPU_X86_64)) || defined(OS_MACOSX) +bool IsProfilerEnabledForChannel() { #if BUILDFLAG(GOOGLE_CHROME_BRANDING) // Only run on canary and dev. const version_info::Channel channel = chrome::GetChannel(); @@ -49,9 +46,6 @@ bool IsProfilerSupportedForPlatformAndChannel() { #else return true; #endif -#else - return false; -#endif } // Returns true if the current execution is taking place in the browser process. @@ -131,7 +125,9 @@ bool StackSamplingConfiguration::GetSyntheticFieldTrial( std::string* group_name) const { DCHECK(IsBrowserProcess()); - if (!IsProfilerSupportedForPlatformAndChannel()) + if (!base::StackSamplingProfiler::IsSupported()) + return false; + if (!IsProfilerEnabledForChannel()) return false; *trial_name = "SyntheticStackProfilingConfiguration"; @@ -224,16 +220,10 @@ StackSamplingConfiguration::GenerateConfiguration() { if (!IsBrowserProcess()) return PROFILE_FROM_COMMAND_LINE; - if (!IsProfilerSupportedForPlatformAndChannel()) + if (!base::StackSamplingProfiler::IsSupported()) return PROFILE_DISABLED; - -#if defined(OS_MACOSX) - // TODO(https://crbug.com/1098119): Fix unwinding on OS X 10.16. The OS has - // moved all system libraries into the dyld shared cache and this seems to - // break the sampling profiler. - if (base::mac::IsOSLaterThan10_15_DontCallThis()) + if (!IsProfilerEnabledForChannel()) return PROFILE_DISABLED; -#endif #if defined(OS_ANDROID) // Allow profiling if the Android Java/native unwinder module is available at @@ -258,13 +248,6 @@ StackSamplingConfiguration::GenerateConfiguration() { } #endif -#if defined(OS_WIN) - // Do not start the profiler when Application Verifier is in use; running them - // simultaneously can cause crashes and has no known use case. - if (GetModuleHandleA(base::win::kApplicationVerifierDllName)) - return PROFILE_DISABLED; -#endif - switch (chrome::GetChannel()) { // Enable the profiler unconditionally for development/waterfall builds. case version_info::Channel::UNKNOWN: diff --git a/services/tracing/public/cpp/stack_sampling/tracing_sampler_profiler.cc b/services/tracing/public/cpp/stack_sampling/tracing_sampler_profiler.cc index b1784d4614d8b9..77208f0e11cc43 100644 --- a/services/tracing/public/cpp/stack_sampling/tracing_sampler_profiler.cc +++ b/services/tracing/public/cpp/stack_sampling/tracing_sampler_profiler.cc @@ -35,10 +35,6 @@ #include "base/android/reached_code_profiler.h" #endif -#if defined(OS_MACOSX) -#include "base/mac/mac_util.h" -#endif - #if defined(OS_ANDROID) && BUILDFLAG(CAN_UNWIND_WITH_CFI_TABLE) && \ defined(OFFICIAL_BUILD) #include @@ -665,13 +661,8 @@ void TracingSamplerProfiler::StartTracing( return; #endif -#if defined(OS_MACOSX) - // TODO(https://crbug.com/1098119): Fix unwinding on OS X 10.16. The OS has - // moved all system libraries into the dyld shared cache and this seems to - // break the sampling profiler. - if (base::mac::IsOSLaterThan10_15_DontCallThis()) + if (!base::StackSamplingProfiler::IsSupported()) return; -#endif base::StackSamplingProfiler::SamplingParams params; params.samples_per_profile = std::numeric_limits::max();