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 |
| 59 void TearDown() override { |
| 60 base::PersistentHistogramAllocator::ReleaseGlobalAllocatorForTesting(); |
| 61 } |
| 62 |
| 63 SubprocessMetricsProvider* provider() { |
| 64 if (!provider_) |
| 65 provider_.reset(new SubprocessMetricsProvider()); |
| 66 return provider_.get(); |
| 67 } |
| 68 |
| 69 private: |
| 70 scoped_ptr<SubprocessMetricsProvider> provider_; |
| 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(SubprocessMetricsProviderTest); |
| 73 }; |
| 74 |
| 75 TEST_F(SubprocessMetricsProviderTest, SnapshotMetrics) { |
| 76 scoped_ptr<base::SharedMemory> shmem1(new base::SharedMemory()); |
| 77 base::SharedMemoryHandle memory_handle; |
| 78 ASSERT_TRUE(shmem1->CreateAndMapAnonymous(TEST_MEMORY_SIZE)); |
| 79 ASSERT_TRUE(shmem1->ShareToProcess(base::GetCurrentProcessHandle(), |
| 80 &memory_handle)); |
| 81 |
| 82 base::PersistentHistogramAllocator::CreateGlobalAllocatorOnSharedMemory( |
| 83 std::move(shmem1), TEST_MEMORY_SIZE, 0, ""); |
| 84 base::HistogramBase* foo = base::Histogram::FactoryGet("foo", 1, 100, 10, 0); |
| 85 base::HistogramBase* bar = base::Histogram::FactoryGet("bar", 1, 100, 10, 0); |
| 86 foo->Add(42); |
| 87 bar->Add(84); |
| 88 |
| 89 scoped_ptr<base::SharedMemory> shmem2( |
| 90 new base::SharedMemory(memory_handle, /*readonly=*/false)); |
| 91 ASSERT_TRUE(shmem2->Map(TEST_MEMORY_SIZE)); |
| 92 |
| 93 scoped_ptr<base::SharedPersistentMemoryAllocator> allocator( |
| 94 new base::SharedPersistentMemoryAllocator( |
| 95 std::move(shmem2), 0, "", false)); |
| 96 provider()->RegisterSubprocessAllocator(123, make_scoped_ptr( |
| 97 new base::PersistentHistogramAllocator(std::move(allocator)))); |
| 98 |
| 99 // Recording should find the two histograms created in persistent memory. |
| 100 { |
| 101 HistogramFlattenerDeltaRecorder flattener; |
| 102 base::HistogramSnapshotManager snapshot_manager(&flattener); |
| 103 snapshot_manager.StartDeltas(); |
| 104 provider()->RecordHistogramSnapshots(&snapshot_manager); |
| 105 snapshot_manager.FinishDeltas(); |
| 106 EXPECT_EQ(2U, flattener.GetRecordedDeltaHistogramNames().size()); |
| 107 } |
| 108 |
| 109 // A second run should have nothing to producte except one internal metric. |
| 110 { |
| 111 HistogramFlattenerDeltaRecorder flattener; |
| 112 base::HistogramSnapshotManager snapshot_manager(&flattener); |
| 113 snapshot_manager.StartDeltas(); |
| 114 provider()->RecordHistogramSnapshots(&snapshot_manager); |
| 115 snapshot_manager.FinishDeltas(); |
| 116 EXPECT_EQ(1U, flattener.GetRecordedDeltaHistogramNames().size()); |
| 117 } |
| 118 |
| 119 // Create a new histogram and update existing ones. Should now report 4 items. |
| 120 base::HistogramBase* baz = base::Histogram::FactoryGet("baz", 1, 100, 10, 0); |
| 121 baz->Add(1969); |
| 122 foo->Add(10); |
| 123 bar->Add(20); |
| 124 { |
| 125 HistogramFlattenerDeltaRecorder flattener; |
| 126 base::HistogramSnapshotManager snapshot_manager(&flattener); |
| 127 snapshot_manager.StartDeltas(); |
| 128 provider()->RecordHistogramSnapshots(&snapshot_manager); |
| 129 snapshot_manager.FinishDeltas(); |
| 130 EXPECT_EQ(4U, flattener.GetRecordedDeltaHistogramNames().size()); |
| 131 } |
| 132 } |
OLD | NEW |