OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 5 #include "chrome/browser/metrics/subprocess_metrics_provider.h" |
| 6 |
| 7 #include "base/memory/shared_memory.h" |
| 8 #include "base/metrics/histogram.h" |
| 9 #include "base/metrics/histogram_flattener.h" |
| 10 #include "base/metrics/histogram_snapshot_manager.h" |
| 11 #include "base/metrics/persistent_histogram_allocator.h" |
| 12 #include "base/metrics/persistent_memory_allocator.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace { |
| 16 const uint32_t TEST_MEMORY_SIZE = 64 << 10; // 64 KiB |
| 17 } // namespace |
| 18 |
| 19 class HistogramFlattenerDeltaRecorder : public base::HistogramFlattener { |
| 20 public: |
| 21 HistogramFlattenerDeltaRecorder() {} |
| 22 |
| 23 void RecordDelta(const base::HistogramBase& histogram, |
| 24 const base::HistogramSamples& snapshot) override { |
| 25 recorded_delta_histogram_names_.push_back(histogram.histogram_name()); |
| 26 } |
| 27 |
| 28 void InconsistencyDetected( |
| 29 base::HistogramBase::Inconsistency problem) override { |
| 30 ASSERT_TRUE(false); |
| 31 } |
| 32 |
| 33 void UniqueInconsistencyDetected( |
| 34 base::HistogramBase::Inconsistency problem) override { |
| 35 ASSERT_TRUE(false); |
| 36 } |
| 37 |
| 38 void InconsistencyDetectedInLoggedCount(int amount) override { |
| 39 ASSERT_TRUE(false); |
| 40 } |
| 41 |
| 42 std::vector<std::string> GetRecordedDeltaHistogramNames() { |
| 43 return recorded_delta_histogram_names_; |
| 44 } |
| 45 |
| 46 private: |
| 47 std::vector<std::string> recorded_delta_histogram_names_; |
| 48 |
| 49 DISALLOW_COPY_AND_ASSIGN(HistogramFlattenerDeltaRecorder); |
| 50 }; |
| 51 |
| 52 class SubprocessMetricsProviderTest : public testing::Test { |
| 53 protected: |
| 54 SubprocessMetricsProviderTest() { |
| 55 // Get this first so it isn't created inside a persistent allocator. |
| 56 base::PersistentHistogramAllocator::GetCreateHistogramResultHistogram(); |
| 57 |
| 58 // Create some shared-memory to pass between global allocator and provider. |
| 59 scoped_ptr<base::SharedMemory> shmem(new base::SharedMemory()); |
| 60 DCHECK(shmem->CreateAndMapAnonymous(TEST_MEMORY_SIZE)); |
| 61 |
| 62 // Keep a weak-pointer to the shared memory since it will be hidden once |
| 63 // made part of the global histogram allocator. |
| 64 shared_memory_ = shmem.get(); |
| 65 |
| 66 // Use the shared memory for all histograms created by this process. |
| 67 base::PersistentHistogramAllocator::CreateGlobalAllocatorOnSharedMemory( |
| 68 std::move(shmem), TEST_MEMORY_SIZE, 0, ""); |
| 69 } |
| 70 |
| 71 void TearDown() override { |
| 72 base::PersistentHistogramAllocator::ReleaseGlobalAllocatorForTesting(); |
| 73 } |
| 74 |
| 75 SubprocessMetricsProvider* provider() { |
| 76 if (!provider_) { |
| 77 provider_.reset(new SubprocessMetricsProvider()); |
| 78 provider_->OnRecordingEnabled(); |
| 79 } |
| 80 return provider_.get(); |
| 81 } |
| 82 |
| 83 base::SharedMemory* shared_memory() { return shared_memory_; } |
| 84 |
| 85 scoped_ptr<base::PersistentHistogramAllocator> GetDuplicateAllocator() { |
| 86 base::SharedMemoryHandle memory_handle; |
| 87 DCHECK(shared_memory_->ShareToProcess(base::GetCurrentProcessHandle(), |
| 88 &memory_handle)); |
| 89 |
| 90 scoped_ptr<base::SharedMemory> shmem( |
| 91 new base::SharedMemory(memory_handle, /*readonly=*/false)); |
| 92 DCHECK(shmem->Map(TEST_MEMORY_SIZE)); |
| 93 |
| 94 return make_scoped_ptr(new base::PersistentHistogramAllocator( |
| 95 make_scoped_ptr(new base::SharedPersistentMemoryAllocator( |
| 96 std::move(shmem), 0, "", false)))); |
| 97 } |
| 98 |
| 99 size_t GetSnapshotHistogramCount() { |
| 100 HistogramFlattenerDeltaRecorder flattener; |
| 101 base::HistogramSnapshotManager snapshot_manager(&flattener); |
| 102 snapshot_manager.StartDeltas(); |
| 103 provider()->RecordHistogramSnapshots(&snapshot_manager); |
| 104 snapshot_manager.FinishDeltas(); |
| 105 return flattener.GetRecordedDeltaHistogramNames().size(); |
| 106 } |
| 107 |
| 108 void EnableRecording() { provider()->OnRecordingEnabled(); } |
| 109 void DisableRecording() { provider()->OnRecordingDisabled(); } |
| 110 |
| 111 void RegisterSubprocessAllocator( |
| 112 int id, |
| 113 scoped_ptr<base::PersistentHistogramAllocator> allocator) { |
| 114 provider()->RegisterSubprocessAllocator(id, std::move(allocator)); |
| 115 } |
| 116 |
| 117 void DeregisterSubprocessAllocator(int id) { |
| 118 provider()->DeregisterSubprocessAllocator(id); |
| 119 } |
| 120 |
| 121 private: |
| 122 base::SharedMemory* shared_memory_; |
| 123 scoped_ptr<SubprocessMetricsProvider> provider_; |
| 124 |
| 125 DISALLOW_COPY_AND_ASSIGN(SubprocessMetricsProviderTest); |
| 126 }; |
| 127 |
| 128 TEST_F(SubprocessMetricsProviderTest, SnapshotMetrics) { |
| 129 base::HistogramBase* foo = base::Histogram::FactoryGet("foo", 1, 100, 10, 0); |
| 130 base::HistogramBase* bar = base::Histogram::FactoryGet("bar", 1, 100, 10, 0); |
| 131 foo->Add(42); |
| 132 bar->Add(84); |
| 133 |
| 134 // Register an allocator that duplicates the global allocator. |
| 135 RegisterSubprocessAllocator(123, GetDuplicateAllocator()); |
| 136 |
| 137 // Recording should find the two histograms created in persistent memory. |
| 138 EXPECT_EQ(2U, GetSnapshotHistogramCount()); |
| 139 |
| 140 // A second run should have nothing to producte except one internal metric. |
| 141 EXPECT_EQ(1U, GetSnapshotHistogramCount()); |
| 142 |
| 143 // Create a new histogram and update existing ones. Should now report 4 items. |
| 144 base::HistogramBase* baz = base::Histogram::FactoryGet("baz", 1, 100, 10, 0); |
| 145 baz->Add(1969); |
| 146 foo->Add(10); |
| 147 bar->Add(20); |
| 148 EXPECT_EQ(4U, GetSnapshotHistogramCount()); |
| 149 |
| 150 // Ensure that deregistering still keeps allocator around for final report. |
| 151 DeregisterSubprocessAllocator(123); |
| 152 EXPECT_EQ(1U, GetSnapshotHistogramCount()); |
| 153 |
| 154 // Further snapshots should be empty. |
| 155 EXPECT_EQ(0U, GetSnapshotHistogramCount()); |
| 156 |
| 157 base::PersistentHistogramAllocator::ReleaseGlobalAllocatorForTesting(); |
| 158 } |
| 159 |
| 160 TEST_F(SubprocessMetricsProviderTest, EnableDisable) { |
| 161 base::HistogramBase* foo = base::Histogram::FactoryGet("foo", 1, 100, 10, 0); |
| 162 base::HistogramBase* bar = base::Histogram::FactoryGet("bar", 1, 100, 10, 0); |
| 163 |
| 164 // Simulate some "normal" operation... |
| 165 RegisterSubprocessAllocator(123, GetDuplicateAllocator()); |
| 166 foo->Add(42); |
| 167 bar->Add(84); |
| 168 EXPECT_EQ(2U, GetSnapshotHistogramCount()); |
| 169 foo->Add(42); |
| 170 bar->Add(84); |
| 171 EXPECT_EQ(2U, GetSnapshotHistogramCount()); |
| 172 |
| 173 // Ensure that disable/enable reporting won't affect "live" allocators. |
| 174 DisableRecording(); |
| 175 EnableRecording(); |
| 176 foo->Add(42); |
| 177 bar->Add(84); |
| 178 EXPECT_EQ(2U, GetSnapshotHistogramCount()); |
| 179 |
| 180 // Ensure that allocators are released when reporting is disabled. |
| 181 DisableRecording(); |
| 182 DeregisterSubprocessAllocator(123); |
| 183 EnableRecording(); |
| 184 foo->Add(42); |
| 185 bar->Add(84); |
| 186 EXPECT_EQ(0U, GetSnapshotHistogramCount()); |
| 187 |
| 188 // Ensure that allocators added when reporting disabled will work if enabled. |
| 189 DisableRecording(); |
| 190 RegisterSubprocessAllocator(123, GetDuplicateAllocator()); |
| 191 EnableRecording(); |
| 192 foo->Add(42); |
| 193 bar->Add(84); |
| 194 EXPECT_EQ(2U, GetSnapshotHistogramCount()); |
| 195 |
| 196 // Ensure that last-chance allocators are released if reporting is disabled. |
| 197 DeregisterSubprocessAllocator(123); |
| 198 DisableRecording(); |
| 199 EnableRecording(); |
| 200 foo->Add(42); |
| 201 bar->Add(84); |
| 202 EXPECT_EQ(0U, GetSnapshotHistogramCount()); |
| 203 } |
OLD | NEW |