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_persistence.h" |
| 11 #include "base/metrics/histogram_snapshot_manager.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 namespace metrics { |
| 20 |
| 21 class HistogramFlattenerDeltaRecorder : public base::HistogramFlattener { |
| 22 public: |
| 23 HistogramFlattenerDeltaRecorder() {} |
| 24 |
| 25 void RecordDelta(const base::HistogramBase& histogram, |
| 26 const base::HistogramSamples& snapshot) override { |
| 27 recorded_delta_histogram_names_.push_back(histogram.histogram_name()); |
| 28 } |
| 29 |
| 30 void InconsistencyDetected( |
| 31 base::HistogramBase::Inconsistency problem) override { |
| 32 ASSERT_TRUE(false); |
| 33 } |
| 34 |
| 35 void UniqueInconsistencyDetected( |
| 36 base::HistogramBase::Inconsistency problem) override { |
| 37 ASSERT_TRUE(false); |
| 38 } |
| 39 |
| 40 void InconsistencyDetectedInLoggedCount(int amount) override { |
| 41 ASSERT_TRUE(false); |
| 42 } |
| 43 |
| 44 std::vector<std::string> GetRecordedDeltaHistogramNames() { |
| 45 return recorded_delta_histogram_names_; |
| 46 } |
| 47 |
| 48 private: |
| 49 std::vector<std::string> recorded_delta_histogram_names_; |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(HistogramFlattenerDeltaRecorder); |
| 52 }; |
| 53 |
| 54 class SubprocessMetricsProviderTest : public testing::Test { |
| 55 protected: |
| 56 SubprocessMetricsProviderTest() { |
| 57 // Get this first so it isn't created inside a persistent allocator. |
| 58 base::GetCreateHistogramResultHistogram(); |
| 59 } |
| 60 |
| 61 void TearDown() override { |
| 62 delete base::ReleasePersistentHistogramMemoryAllocatorForTesting(); |
| 63 } |
| 64 |
| 65 SubprocessMetricsProvider* provider() { |
| 66 if (!provider_) |
| 67 provider_.reset(new SubprocessMetricsProvider()); |
| 68 return provider_.get(); |
| 69 } |
| 70 |
| 71 private: |
| 72 scoped_ptr<SubprocessMetricsProvider> provider_; |
| 73 |
| 74 DISALLOW_COPY_AND_ASSIGN(SubprocessMetricsProviderTest); |
| 75 }; |
| 76 |
| 77 TEST_F(SubprocessMetricsProviderTest, SnapshotMetrics) { |
| 78 scoped_ptr<base::SharedMemory> shmem1(new base::SharedMemory()); |
| 79 base::SharedMemoryHandle memory_handle; |
| 80 ASSERT_TRUE(shmem1->CreateAndMapAnonymous(TEST_MEMORY_SIZE)); |
| 81 ASSERT_TRUE(shmem1->ShareToProcess(base::GetCurrentProcessHandle(), |
| 82 &memory_handle)); |
| 83 |
| 84 base::SetPersistentHistogramMemoryAllocator( |
| 85 new base::SharedPersistentMemoryAllocator( |
| 86 std::move(shmem1), 0, "", false)); |
| 87 base::HistogramBase* foo = base::Histogram::FactoryGet("foo", 1, 100, 10, 0); |
| 88 base::HistogramBase* bar = base::Histogram::FactoryGet("bar", 1, 100, 10, 0); |
| 89 foo->Add(42); |
| 90 bar->Add(84); |
| 91 |
| 92 scoped_ptr<base::SharedMemory> shmem2( |
| 93 new base::SharedMemory(memory_handle, /*readonly=*/false)); |
| 94 ASSERT_TRUE(shmem2->Map(TEST_MEMORY_SIZE)); |
| 95 |
| 96 scoped_ptr<base::SharedPersistentMemoryAllocator> allocator( |
| 97 new base::SharedPersistentMemoryAllocator( |
| 98 std::move(shmem2), 0, "", false)); |
| 99 provider()->RegisterSubprocessAllocator(123, std::move(allocator)); |
| 100 |
| 101 // Recording should find the two histograms created in persistent memory. |
| 102 { |
| 103 HistogramFlattenerDeltaRecorder flattener; |
| 104 base::HistogramSnapshotManager snapshot_manager(&flattener); |
| 105 snapshot_manager.StartDeltas(); |
| 106 provider()->RecordHistogramSnapshots(&snapshot_manager); |
| 107 snapshot_manager.FinishDeltas(); |
| 108 EXPECT_EQ(2U, flattener.GetRecordedDeltaHistogramNames().size()); |
| 109 } |
| 110 |
| 111 // A second run should have nothing to producte except one internal metric. |
| 112 { |
| 113 HistogramFlattenerDeltaRecorder flattener; |
| 114 base::HistogramSnapshotManager snapshot_manager(&flattener); |
| 115 snapshot_manager.StartDeltas(); |
| 116 provider()->RecordHistogramSnapshots(&snapshot_manager); |
| 117 snapshot_manager.FinishDeltas(); |
| 118 EXPECT_EQ(1U, flattener.GetRecordedDeltaHistogramNames().size()); |
| 119 } |
| 120 |
| 121 // Create a new histogram and update existing ones. Should now report 4 items. |
| 122 base::HistogramBase* baz = base::Histogram::FactoryGet("baz", 1, 100, 10, 0); |
| 123 baz->Add(1969); |
| 124 foo->Add(10); |
| 125 bar->Add(20); |
| 126 { |
| 127 HistogramFlattenerDeltaRecorder flattener; |
| 128 base::HistogramSnapshotManager snapshot_manager(&flattener); |
| 129 snapshot_manager.StartDeltas(); |
| 130 provider()->RecordHistogramSnapshots(&snapshot_manager); |
| 131 snapshot_manager.FinishDeltas(); |
| 132 EXPECT_EQ(4U, flattener.GetRecordedDeltaHistogramNames().size()); |
| 133 } |
| 134 } |
| 135 |
| 136 } // namespace metrics |
OLD | NEW |