stack_sampling_profiler_test_util.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_PROFILER_STACK_SAMPLING_PROFILER_TEST_UTIL_H_
  5. #define BASE_PROFILER_STACK_SAMPLING_PROFILER_TEST_UTIL_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/callback.h"
  9. #include "base/native_library.h"
  10. #include "base/profiler/frame.h"
  11. #include "base/profiler/sampling_profiler_thread_token.h"
  12. #include "base/profiler/stack_sampling_profiler.h"
  13. #include "base/synchronization/waitable_event.h"
  14. #include "base/threading/platform_thread.h"
  15. namespace base {
  16. class Unwinder;
  17. class ModuleCache;
  18. // A thread to target for profiling that will run the supplied closure.
  19. class TargetThread : public PlatformThread::Delegate {
  20. public:
  21. TargetThread(OnceClosure to_run);
  22. ~TargetThread() override;
  23. // PlatformThread::Delegate:
  24. void ThreadMain() override;
  25. SamplingProfilerThreadToken thread_token() const { return thread_token_; }
  26. private:
  27. SamplingProfilerThreadToken thread_token_ = {0};
  28. OnceClosure to_run_;
  29. DISALLOW_COPY_AND_ASSIGN(TargetThread);
  30. };
  31. // Addresses near the start and end of a function.
  32. struct FunctionAddressRange {
  33. const void* start;
  34. const void* end;
  35. };
  36. // Represents a stack unwind scenario to be sampled by the
  37. // StackSamplingProfiler.
  38. class UnwindScenario {
  39. public:
  40. // A callback provided by the caller that sets up the unwind scenario, then
  41. // calls into the passed closure to wait for a sample to be taken. Returns the
  42. // address range of the function that sets up the unwind scenario. The passed
  43. // closure will be null when invoked solely to obtain the address range.
  44. using SetupFunction = RepeatingCallback<FunctionAddressRange(OnceClosure)>;
  45. // Events to coordinate the sampling.
  46. struct SampleEvents {
  47. WaitableEvent ready_for_sample;
  48. WaitableEvent sample_finished;
  49. };
  50. explicit UnwindScenario(const SetupFunction& setup_function);
  51. ~UnwindScenario();
  52. UnwindScenario(const UnwindScenario&) = delete;
  53. UnwindScenario& operator=(const UnwindScenario&) = delete;
  54. // The address range of the innermost function that waits for the sample.
  55. FunctionAddressRange GetWaitForSampleAddressRange() const;
  56. // The address range of the provided setup function.
  57. FunctionAddressRange GetSetupFunctionAddressRange() const;
  58. // The address range of the outer function that indirectly invokes the setup
  59. // function.
  60. FunctionAddressRange GetOuterFunctionAddressRange() const;
  61. // Executes the scenario.
  62. void Execute(SampleEvents* events);
  63. private:
  64. static FunctionAddressRange InvokeSetupFunction(
  65. const SetupFunction& setup_function,
  66. SampleEvents* events);
  67. static FunctionAddressRange WaitForSample(SampleEvents* events);
  68. const SetupFunction setup_function_;
  69. };
  70. // UnwindScenario setup function that calls into |wait_for_sample| without doing
  71. // any special unwinding setup, to exercise the "normal" unwind scenario.
  72. FunctionAddressRange CallWithPlainFunction(OnceClosure wait_for_sample);
  73. // Calls into |wait_for_sample| after using alloca(), to test unwinding with a
  74. // frame pointer.
  75. FunctionAddressRange CallWithAlloca(OnceClosure wait_for_sample);
  76. // Calls into |wait_for_sample| through a function within another library, to
  77. // test unwinding through multiple modules and scenarios involving unloaded
  78. // modules.
  79. FunctionAddressRange CallThroughOtherLibrary(NativeLibrary library,
  80. OnceClosure wait_for_sample);
  81. // The callback to perform profiling on the provided thread.
  82. using ProfileCallback = OnceCallback<void(SamplingProfilerThreadToken)>;
  83. // Executes |profile_callback| while running |scenario| on the target
  84. // thread. Performs all necessary target thread startup and shutdown work before
  85. // and afterward.
  86. void WithTargetThread(UnwindScenario* scenario,
  87. ProfileCallback profile_callback);
  88. using UnwinderFactory = OnceCallback<std::unique_ptr<Unwinder>()>;
  89. // Returns the sample seen when taking one sample of |scenario|.
  90. std::vector<Frame> SampleScenario(
  91. UnwindScenario* scenario,
  92. ModuleCache* module_cache,
  93. UnwinderFactory aux_unwinder_factory = UnwinderFactory());
  94. // Formats a sample into a string that can be output for test diagnostics.
  95. std::string FormatSampleForDiagnosticOutput(const std::vector<Frame>& sample);
  96. // Expects that the stack contains the functions with the specified address
  97. // ranges, in the specified order.
  98. void ExpectStackContains(const std::vector<Frame>& stack,
  99. const std::vector<FunctionAddressRange>& functions);
  100. // Expects that the stack does not contain the functions with the specified
  101. // address ranges.
  102. void ExpectStackDoesNotContain(
  103. const std::vector<Frame>& stack,
  104. const std::vector<FunctionAddressRange>& functions);
  105. // Loads the other library, which defines a function to be called in the
  106. // WITH_OTHER_LIBRARY configuration.
  107. NativeLibrary LoadOtherLibrary();
  108. uintptr_t GetAddressInOtherLibrary(NativeLibrary library);
  109. // Creates a list of core unwinders required for StackSamplingProfilerTest.
  110. // This is useful notably on Android, which requires ChromeUnwinderAndroid in
  111. // addition to the native one.
  112. StackSamplingProfiler::UnwindersFactory CreateCoreUnwindersFactoryForTesting(
  113. ModuleCache* module_cache);
  114. } // namespace base
  115. #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_TEST_UTIL_H_