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