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