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 "base/metrics/persistent_histogram_allocator.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/metrics/bucket_ranges.h" |
| 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/metrics/persistent_memory_allocator.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace base { |
| 14 |
| 15 class PersistentHistogramAllocatorTest : public testing::Test { |
| 16 protected: |
| 17 const int32_t kAllocatorMemorySize = 64 << 10; // 64 KiB |
| 18 |
| 19 PersistentHistogramAllocatorTest() { CreatePersistentHistogramAllocator(); } |
| 20 ~PersistentHistogramAllocatorTest() override { |
| 21 DestroyPersistentHistogramAllocator(); |
| 22 } |
| 23 |
| 24 void CreatePersistentHistogramAllocator() { |
| 25 allocator_memory_.reset(new char[kAllocatorMemorySize]); |
| 26 |
| 27 PersistentHistogramAllocator::ReleaseGlobalAllocatorForTesting(); |
| 28 memset(allocator_memory_.get(), 0, kAllocatorMemorySize); |
| 29 PersistentHistogramAllocator::GetCreateHistogramResultHistogram(); |
| 30 PersistentHistogramAllocator::CreateGlobalAllocatorOnPersistentMemory( |
| 31 allocator_memory_.get(), kAllocatorMemorySize, 0, 0, |
| 32 "PersistentHistogramAllocatorTest"); |
| 33 allocator_ = |
| 34 PersistentHistogramAllocator::GetGlobalAllocator()->memory_allocator(); |
| 35 } |
| 36 |
| 37 void DestroyPersistentHistogramAllocator() { |
| 38 allocator_ = nullptr; |
| 39 PersistentHistogramAllocator::ReleaseGlobalAllocatorForTesting(); |
| 40 } |
| 41 |
| 42 scoped_ptr<char[]> allocator_memory_; |
| 43 PersistentMemoryAllocator* allocator_ = nullptr; |
| 44 |
| 45 private: |
| 46 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocatorTest); |
| 47 }; |
| 48 |
| 49 TEST_F(PersistentHistogramAllocatorTest, CreateAndIterateTest) { |
| 50 PersistentMemoryAllocator::MemoryInfo meminfo0; |
| 51 allocator_->GetMemoryInfo(&meminfo0); |
| 52 |
| 53 // Try basic construction |
| 54 HistogramBase* histogram = Histogram::FactoryGet( |
| 55 "TestHistogram", 1, 1000, 10, HistogramBase::kIsPersistent); |
| 56 EXPECT_TRUE(histogram); |
| 57 histogram->CheckName("TestHistogram"); |
| 58 PersistentMemoryAllocator::MemoryInfo meminfo1; |
| 59 allocator_->GetMemoryInfo(&meminfo1); |
| 60 EXPECT_GT(meminfo0.free, meminfo1.free); |
| 61 |
| 62 HistogramBase* linear_histogram = LinearHistogram::FactoryGet( |
| 63 "TestLinearHistogram", 1, 1000, 10, HistogramBase::kIsPersistent); |
| 64 EXPECT_TRUE(linear_histogram); |
| 65 linear_histogram->CheckName("TestLinearHistogram"); |
| 66 PersistentMemoryAllocator::MemoryInfo meminfo2; |
| 67 allocator_->GetMemoryInfo(&meminfo2); |
| 68 EXPECT_GT(meminfo1.free, meminfo2.free); |
| 69 |
| 70 HistogramBase* boolean_histogram = BooleanHistogram::FactoryGet( |
| 71 "TestBooleanHistogram", HistogramBase::kIsPersistent); |
| 72 EXPECT_TRUE(boolean_histogram); |
| 73 boolean_histogram->CheckName("TestBooleanHistogram"); |
| 74 PersistentMemoryAllocator::MemoryInfo meminfo3; |
| 75 allocator_->GetMemoryInfo(&meminfo3); |
| 76 EXPECT_GT(meminfo2.free, meminfo3.free); |
| 77 |
| 78 std::vector<int> custom_ranges; |
| 79 custom_ranges.push_back(1); |
| 80 custom_ranges.push_back(5); |
| 81 HistogramBase* custom_histogram = CustomHistogram::FactoryGet( |
| 82 "TestCustomHistogram", custom_ranges, HistogramBase::kIsPersistent); |
| 83 EXPECT_TRUE(custom_histogram); |
| 84 custom_histogram->CheckName("TestCustomHistogram"); |
| 85 PersistentMemoryAllocator::MemoryInfo meminfo4; |
| 86 allocator_->GetMemoryInfo(&meminfo4); |
| 87 EXPECT_GT(meminfo3.free, meminfo4.free); |
| 88 |
| 89 PersistentMemoryAllocator::Iterator iter; |
| 90 uint32_t type; |
| 91 allocator_->CreateIterator(&iter); |
| 92 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // Histogram |
| 93 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // LinearHistogram |
| 94 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // BooleanHistogram |
| 95 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // CustomHistogram |
| 96 EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type)); |
| 97 |
| 98 // Create a second allocator and have it access the memory of the first. |
| 99 scoped_ptr<HistogramBase> recovered; |
| 100 PersistentHistogramAllocator recovery( |
| 101 make_scoped_ptr(new PersistentMemoryAllocator( |
| 102 allocator_memory_.get(), kAllocatorMemorySize, 0, 0, "", false))); |
| 103 PersistentHistogramAllocator::Iterator histogram_iter; |
| 104 recovery.CreateIterator(&histogram_iter); |
| 105 |
| 106 recovered = recovery.GetNextHistogram(&histogram_iter); |
| 107 ASSERT_TRUE(recovered); |
| 108 recovered->CheckName("TestHistogram"); |
| 109 |
| 110 recovered = recovery.GetNextHistogram(&histogram_iter); |
| 111 ASSERT_TRUE(recovered); |
| 112 recovered->CheckName("TestLinearHistogram"); |
| 113 |
| 114 recovered = recovery.GetNextHistogram(&histogram_iter); |
| 115 ASSERT_TRUE(recovered); |
| 116 recovered->CheckName("TestBooleanHistogram"); |
| 117 |
| 118 recovered = recovery.GetNextHistogram(&histogram_iter); |
| 119 ASSERT_TRUE(recovered); |
| 120 recovered->CheckName("TestCustomHistogram"); |
| 121 |
| 122 recovered = recovery.GetNextHistogram(&histogram_iter); |
| 123 EXPECT_FALSE(recovered); |
| 124 } |
| 125 |
| 126 } // namespace base |
OLD | NEW |