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