summaryrefslogtreecommitdiffstats
path: root/chromium/base/sampling_heap_profiler/poisson_allocation_sampler.h
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2022-02-04 17:20:24 +0100
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2022-02-12 08:15:25 +0000
commit8fa0776f1f79e91fc9c0b9c1ba11a0a29c05196b (patch)
tree788d8d7549712682703a0310ca4a0f0860d4802b /chromium/base/sampling_heap_profiler/poisson_allocation_sampler.h
parent606d85f2a5386472314d39923da28c70c60dc8e7 (diff)
BASELINE: Update Chromium to 98.0.4758.90
Change-Id: Ib7c41539bf8a8e0376bd639f27d68294de90f3c8 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'chromium/base/sampling_heap_profiler/poisson_allocation_sampler.h')
-rw-r--r--chromium/base/sampling_heap_profiler/poisson_allocation_sampler.h53
1 files changed, 48 insertions, 5 deletions
diff --git a/chromium/base/sampling_heap_profiler/poisson_allocation_sampler.h b/chromium/base/sampling_heap_profiler/poisson_allocation_sampler.h
index 7fd1491275f..0f26f5165f7 100644
--- a/chromium/base/sampling_heap_profiler/poisson_allocation_sampler.h
+++ b/chromium/base/sampling_heap_profiler/poisson_allocation_sampler.h
@@ -9,14 +9,20 @@
#include "base/base_export.h"
#include "base/compiler_specific.h"
-#include "base/macros.h"
+#include "base/gtest_prod_util.h"
#include "base/no_destructor.h"
#include "base/sampling_heap_profiler/lock_free_address_hash_set.h"
#include "base/synchronization/lock.h"
#include "base/thread_annotations.h"
+namespace heap_profiling {
+class HeapProfilerControllerTest;
+}
+
namespace base {
+class SamplingHeapProfilerTest;
+
// This singleton class implements Poisson sampling of the incoming allocations
// stream. It hooks onto base::allocator and base::PartitionAlloc.
// An extra custom allocator can be hooked via SetHooksInstallCallback method.
@@ -31,7 +37,10 @@ namespace base {
//
class BASE_EXPORT PoissonAllocationSampler {
public:
- enum AllocatorType : uint32_t { kMalloc, kPartitionAlloc };
+ // The type of hooked allocator that is the source of a sample.
+ // kManualForTesting is for unit tests calling RecordAlloc directly without
+ // going through a hooked allocator.
+ enum AllocatorType : uint32_t { kMalloc, kPartitionAlloc, kManualForTesting };
class SamplesObserver {
public:
@@ -44,7 +53,7 @@ class BASE_EXPORT PoissonAllocationSampler {
virtual void SampleRemoved(void* address) = 0;
};
- // The instance of this class makes sampler do not report samples generated
+ // An instance of this class makes the sampler not report samples generated
// within the object scope for the current thread.
// It allows observers to allocate/deallocate memory while holding a lock
// without a chance to get into reentrancy problems.
@@ -54,6 +63,9 @@ class BASE_EXPORT PoissonAllocationSampler {
ScopedMuteThreadSamples();
~ScopedMuteThreadSamples();
+ ScopedMuteThreadSamples(const ScopedMuteThreadSamples&) = delete;
+ ScopedMuteThreadSamples& operator=(const ScopedMuteThreadSamples&) = delete;
+
static bool IsMuted();
};
@@ -95,12 +107,42 @@ class BASE_EXPORT PoissonAllocationSampler {
PoissonAllocationSampler(const PoissonAllocationSampler&) = delete;
PoissonAllocationSampler& operator=(const PoissonAllocationSampler&) = delete;
+ // Returns true if a ScopedMuteHookedSamplesForTesting exists. Only friends
+ // can create a ScopedMuteHookedSamplesForTesting but anyone can check the
+ // status of this. This can be read from any thread.
+ static bool AreHookedSamplesMuted();
+
private:
+ // An instance of this class makes the sampler only report samples with
+ // AllocatorType kManualForTesting, not those from hooked allocators. This
+ // allows unit tests to set test expectations based on only explicit calls to
+ // RecordAlloc and RecordFree.
+ //
+ // The accumulated bytes on the thread that creates a
+ // ScopedMuteHookedSamplesForTesting will also be reset to 0, and restored
+ // when the object leaves scope. This gives tests a known state to start
+ // recording samples on one thread: a full interval must pass to record a
+ // sample. Other threads will still have a random number of accumulated bytes.
+ //
+ // Only one instance may exist at a time.
+ class BASE_EXPORT ScopedMuteHookedSamplesForTesting {
+ public:
+ ScopedMuteHookedSamplesForTesting();
+ ~ScopedMuteHookedSamplesForTesting();
+
+ ScopedMuteHookedSamplesForTesting(
+ const ScopedMuteHookedSamplesForTesting&) = delete;
+ ScopedMuteHookedSamplesForTesting& operator=(
+ const ScopedMuteHookedSamplesForTesting&) = delete;
+
+ private:
+ intptr_t accumulated_bytes_snapshot_;
+ };
+
PoissonAllocationSampler();
~PoissonAllocationSampler() = delete;
static void InstallAllocatorHooksOnce();
- static bool InstallAllocatorHooks();
static size_t GetNextSampleInterval(size_t base_interval);
static LockFreeAddressHashSet& sampled_addresses_set();
@@ -123,9 +165,10 @@ class BASE_EXPORT PoissonAllocationSampler {
static PoissonAllocationSampler* instance_;
+ friend class heap_profiling::HeapProfilerControllerTest;
friend class NoDestructor<PoissonAllocationSampler>;
friend class SamplingHeapProfilerTest;
- friend class ScopedMuteThreadSamples;
+ FRIEND_TEST_ALL_PREFIXES(SamplingHeapProfilerTest, HookedAllocatorMuted);
};
// static