#include "base/profiler/stack_sampling_profiler.h"
#include <stddef.h>
#include <stdint.h>
#include <cstdlib>
#include <memory>
#include <set>
#include <utility>
#include <vector>
#include "base/compiler_specific.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/metrics_hashes.h"
#include "base/notimplemented.h"
#include "base/profiler/profiler_buildflags.h"
#include "base/profiler/sample_metadata.h"
#include "base/profiler/stack_sampler.h"
#include "base/profiler/stack_sampling_profiler_test_util.h"
#include "base/profiler/unwinder.h"
#include "base/ranges/algorithm.h"
#include "base/run_loop.h"
#include "base/scoped_native_library.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "base/threading/simple_thread.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#include <intrin.h>
#include <malloc.h>
#else
#include <alloca.h>
#endif
#if (BUILDFLAG(IS_WIN) && defined(ARCH_CPU_X86_64)) || (BUILDFLAG(IS_MAC)) || \
(BUILDFLAG(IS_IOS) && defined(ARCH_CPU_64_BITS)) || \
(BUILDFLAG(IS_ANDROID) && BUILDFLAG(ENABLE_ARM_CFI_TABLE)) || \
(BUILDFLAG(IS_CHROMEOS) && \
(defined(ARCH_CPU_X86_64) || defined(ARCH_CPU_ARM64)) && \
!defined(MEMORY_SANITIZER))
#define STACK_SAMPLING_PROFILER_SUPPORTED …
#endif
namespace base {
#if defined(STACK_SAMPLING_PROFILER_SUPPORTED)
#define PROFILER_TEST_F …
#else
#define PROFILER_TEST_F(TestClass, TestName) …
#endif
SamplingParams;
namespace {
struct RetrospectiveMetadata { … };
struct Profile { … };
ProfileCompletedCallback;
class TestProfileBuilder : public ProfileBuilder { … };
TestProfileBuilder::TestProfileBuilder(ModuleCache* module_cache,
ProfileCompletedCallback callback)
: … { … }
TestProfileBuilder::~TestProfileBuilder() = default;
ModuleCache* TestProfileBuilder::GetModuleCache() { … }
void TestProfileBuilder::RecordMetadata(
const MetadataRecorder::MetadataProvider& metadata_provider) { … }
void TestProfileBuilder::ApplyMetadataRetrospectively(
TimeTicks period_start,
TimeTicks period_end,
const MetadataRecorder::Item& item) { … }
void TestProfileBuilder::AddProfileMetadata(
const MetadataRecorder::Item& item) { … }
void TestProfileBuilder::OnSampleCompleted(std::vector<Frame> sample,
TimeTicks sample_timestamp) { … }
void TestProfileBuilder::OnProfileCompleted(TimeDelta profile_duration,
TimeDelta sampling_period) { … }
void SynchronousUnloadNativeLibrary(NativeLibrary library) { … }
void WithTargetThread(ProfileCallback profile_callback) { … }
struct TestProfilerInfo { … };
std::vector<std::vector<Frame>> CaptureSamples(const SamplingParams& params,
TimeDelta profiler_wait_time,
ModuleCache* module_cache) { … }
size_t WaitForSamplingComplete(
const std::vector<std::unique_ptr<TestProfilerInfo>>& infos) { … }
TimeDelta AVeryLongTimeDelta() { … }
void TestLibraryUnload(bool wait_until_unloaded, ModuleCache* module_cache) { … }
class StackSamplingProfilerTest : public testing::Test { … };
}
#if (defined(ADDRESS_SANITIZER) && BUILDFLAG(IS_APPLE)) || \
(defined(ADDRESS_SANITIZER) && BUILDFLAG(IS_ANDROID)) || \
(BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_CHROMEOS_DEVICE))
#define MAYBE_Basic …
#else
#define MAYBE_Basic …
#endif
PROFILER_TEST_F(StackSamplingProfilerTest, MAYBE_Basic) { … }
class TestAuxUnwinder : public Unwinder { … };
#if (defined(ADDRESS_SANITIZER) && BUILDFLAG(IS_APPLE)) || \
BUILDFLAG(IS_ANDROID) || \
(BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_CHROMEOS_DEVICE))
#define MAYBE_Alloca …
#else
#define MAYBE_Alloca …
#endif
PROFILER_TEST_F(StackSamplingProfilerTest, MAYBE_Alloca) { … }
#if (defined(ADDRESS_SANITIZER) && BUILDFLAG(IS_APPLE)) || \
BUILDFLAG(IS_IOS) || \
(BUILDFLAG(IS_ANDROID) && BUILDFLAG(EXCLUDE_UNWIND_TABLES)) || \
(BUILDFLAG(IS_ANDROID) && defined(ADDRESS_SANITIZER)) || \
(BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_CHROMEOS_DEVICE))
#define MAYBE_OtherLibrary …
#else
#define MAYBE_OtherLibrary …
#endif
PROFILER_TEST_F(StackSamplingProfilerTest, MAYBE_OtherLibrary) { … }
#if BUILDFLAG(IS_APPLE) || \
(BUILDFLAG(IS_ANDROID) && BUILDFLAG(EXCLUDE_UNWIND_TABLES)) || \
(BUILDFLAG(IS_ANDROID) && defined(ADDRESS_SANITIZER)) || \
(BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_CHROMEOS_DEVICE))
#define MAYBE_UnloadingLibrary …
#else
#define MAYBE_UnloadingLibrary …
#endif
PROFILER_TEST_F(StackSamplingProfilerTest, MAYBE_UnloadingLibrary) { … }
#if (defined(ADDRESS_SANITIZER) && BUILDFLAG(IS_APPLE)) || \
BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) || \
(BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_CHROMEOS_DEVICE))
#define MAYBE_UnloadedLibrary …
#else
#define MAYBE_UnloadedLibrary …
#endif
PROFILER_TEST_F(StackSamplingProfilerTest, MAYBE_UnloadedLibrary) { … }
PROFILER_TEST_F(StackSamplingProfilerTest, StopWithoutStarting) { … }
PROFILER_TEST_F(StackSamplingProfilerTest, StopSafely) { … }
PROFILER_TEST_F(StackSamplingProfilerTest, StopDuringInitialDelay) { … }
PROFILER_TEST_F(StackSamplingProfilerTest, StopDuringInterSampleInterval) { … }
PROFILER_TEST_F(StackSamplingProfilerTest, GetNextSampleTime_NormalExecution) { … }
PROFILER_TEST_F(StackSamplingProfilerTest, GetNextSampleTime_DelayedExecution) { … }
PROFILER_TEST_F(StackSamplingProfilerTest, DestroyProfilerWhileProfiling) { … }
PROFILER_TEST_F(StackSamplingProfilerTest, CanRunMultipleProfilers) { … }
PROFILER_TEST_F(StackSamplingProfilerTest, MultipleStart) { … }
PROFILER_TEST_F(StackSamplingProfilerTest, ProfileGeneralInfo) { … }
PROFILER_TEST_F(StackSamplingProfilerTest, SamplerIdleShutdown) { … }
PROFILER_TEST_F(StackSamplingProfilerTest,
WillRestartSamplerAfterIdleShutdown) { … }
PROFILER_TEST_F(StackSamplingProfilerTest, StopAfterIdleShutdown) { … }
PROFILER_TEST_F(StackSamplingProfilerTest,
ProfileBeforeAndAfterSamplingThreadRunning) { … }
PROFILER_TEST_F(StackSamplingProfilerTest, IdleShutdownAbort) { … }
PROFILER_TEST_F(StackSamplingProfilerTest, ConcurrentProfiling_InSync) { … }
PROFILER_TEST_F(StackSamplingProfilerTest, ConcurrentProfiling_Mixed) { … }
PROFILER_TEST_F(StackSamplingProfilerTest, MultipleSampledThreads) { … }
class ProfilerThread : public SimpleThread { … };
PROFILER_TEST_F(StackSamplingProfilerTest, MultipleProfilerThreads) { … }
PROFILER_TEST_F(StackSamplingProfilerTest, AddAuxUnwinder_BeforeStart) { … }
PROFILER_TEST_F(StackSamplingProfilerTest, AddAuxUnwinder_AfterStart) { … }
PROFILER_TEST_F(StackSamplingProfilerTest, AddAuxUnwinder_AfterStop) { … }
PROFILER_TEST_F(StackSamplingProfilerTest,
ApplyMetadataToPastSamples_PassedToProfileBuilder) { … }
PROFILER_TEST_F(
StackSamplingProfilerTest,
ApplyMetadataToPastSamples_PassedToProfileBuilder_MultipleCollections) { … }
PROFILER_TEST_F(StackSamplingProfilerTest,
AddProfileMetadata_PassedToProfileBuilder) { … }
PROFILER_TEST_F(StackSamplingProfilerTest,
AddProfileMetadata_PassedToProfileBuilder_MultipleCollections) { … }
}