OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/metrics/persistent_histogram_allocator.h" | 5 #include "base/metrics/persistent_histogram_allocator.h" |
6 | 6 |
| 7 #include "base/files/file.h" |
| 8 #include "base/files/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
10 #include "base/metrics/bucket_ranges.h" | 12 #include "base/metrics/bucket_ranges.h" |
11 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
12 #include "base/metrics/persistent_memory_allocator.h" | 14 #include "base/metrics/persistent_memory_allocator.h" |
13 #include "base/metrics/statistics_recorder.h" | 15 #include "base/metrics/statistics_recorder.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
15 | 17 |
16 namespace base { | 18 namespace base { |
(...skipping 28 matching lines...) Expand all Loading... |
45 } | 47 } |
46 | 48 |
47 std::unique_ptr<StatisticsRecorder> statistics_recorder_; | 49 std::unique_ptr<StatisticsRecorder> statistics_recorder_; |
48 std::unique_ptr<char[]> allocator_memory_; | 50 std::unique_ptr<char[]> allocator_memory_; |
49 PersistentMemoryAllocator* allocator_ = nullptr; | 51 PersistentMemoryAllocator* allocator_ = nullptr; |
50 | 52 |
51 private: | 53 private: |
52 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocatorTest); | 54 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocatorTest); |
53 }; | 55 }; |
54 | 56 |
55 TEST_F(PersistentHistogramAllocatorTest, CreateAndIterateTest) { | 57 TEST_F(PersistentHistogramAllocatorTest, CreateAndIterate) { |
56 PersistentMemoryAllocator::MemoryInfo meminfo0; | 58 PersistentMemoryAllocator::MemoryInfo meminfo0; |
57 allocator_->GetMemoryInfo(&meminfo0); | 59 allocator_->GetMemoryInfo(&meminfo0); |
58 | 60 |
59 // Try basic construction | 61 // Try basic construction |
60 HistogramBase* histogram = Histogram::FactoryGet( | 62 HistogramBase* histogram = Histogram::FactoryGet( |
61 "TestHistogram", 1, 1000, 10, HistogramBase::kIsPersistent); | 63 "TestHistogram", 1, 1000, 10, HistogramBase::kIsPersistent); |
62 EXPECT_TRUE(histogram); | 64 EXPECT_TRUE(histogram); |
63 histogram->CheckName("TestHistogram"); | 65 histogram->CheckName("TestHistogram"); |
64 PersistentMemoryAllocator::MemoryInfo meminfo1; | 66 PersistentMemoryAllocator::MemoryInfo meminfo1; |
65 allocator_->GetMemoryInfo(&meminfo1); | 67 allocator_->GetMemoryInfo(&meminfo1); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 recovered->CheckName("TestBooleanHistogram"); | 121 recovered->CheckName("TestBooleanHistogram"); |
120 | 122 |
121 recovered = histogram_iter.GetNext(); | 123 recovered = histogram_iter.GetNext(); |
122 ASSERT_TRUE(recovered); | 124 ASSERT_TRUE(recovered); |
123 recovered->CheckName("TestCustomHistogram"); | 125 recovered->CheckName("TestCustomHistogram"); |
124 | 126 |
125 recovered = histogram_iter.GetNext(); | 127 recovered = histogram_iter.GetNext(); |
126 EXPECT_FALSE(recovered); | 128 EXPECT_FALSE(recovered); |
127 } | 129 } |
128 | 130 |
129 TEST_F(PersistentHistogramAllocatorTest, CreateWithFileTest) { | 131 TEST_F(PersistentHistogramAllocatorTest, CreateWithFile) { |
130 const char temp_name[] = "CreateWithFileTest"; | 132 const char temp_name[] = "CreateWithFileTest"; |
131 ScopedTempDir temp_dir; | 133 ScopedTempDir temp_dir; |
132 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 134 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
133 FilePath temp_file = temp_dir.GetPath().AppendASCII(temp_name); | 135 FilePath temp_file = temp_dir.GetPath().AppendASCII(temp_name); |
134 const size_t temp_size = 64 << 10; // 64 KiB | 136 const size_t temp_size = 64 << 10; // 64 KiB |
135 | 137 |
136 // Test creation of a new file. | 138 // Test creation of a new file. |
137 GlobalHistogramAllocator::ReleaseForTesting(); | 139 GlobalHistogramAllocator::ReleaseForTesting(); |
138 GlobalHistogramAllocator::CreateWithFile(temp_file, temp_size, 0, temp_name); | 140 GlobalHistogramAllocator::CreateWithFile(temp_file, temp_size, 0, temp_name); |
139 EXPECT_EQ(std::string(temp_name), | 141 EXPECT_EQ(std::string(temp_name), |
140 GlobalHistogramAllocator::Get()->memory_allocator()->Name()); | 142 GlobalHistogramAllocator::Get()->memory_allocator()->Name()); |
141 | 143 |
142 // Test re-open of a possibly-existing file. | 144 // Test re-open of a possibly-existing file. |
143 GlobalHistogramAllocator::ReleaseForTesting(); | 145 GlobalHistogramAllocator::ReleaseForTesting(); |
144 GlobalHistogramAllocator::CreateWithFile(temp_file, temp_size, 0, ""); | 146 GlobalHistogramAllocator::CreateWithFile(temp_file, temp_size, 0, ""); |
145 EXPECT_EQ(std::string(temp_name), | 147 EXPECT_EQ(std::string(temp_name), |
146 GlobalHistogramAllocator::Get()->memory_allocator()->Name()); | 148 GlobalHistogramAllocator::Get()->memory_allocator()->Name()); |
147 | 149 |
148 // Test re-open of an known-existing file. | 150 // Test re-open of an known-existing file. |
149 GlobalHistogramAllocator::ReleaseForTesting(); | 151 GlobalHistogramAllocator::ReleaseForTesting(); |
150 GlobalHistogramAllocator::CreateWithFile(temp_file, 0, 0, ""); | 152 GlobalHistogramAllocator::CreateWithFile(temp_file, 0, 0, ""); |
151 EXPECT_EQ(std::string(temp_name), | 153 EXPECT_EQ(std::string(temp_name), |
152 GlobalHistogramAllocator::Get()->memory_allocator()->Name()); | 154 GlobalHistogramAllocator::Get()->memory_allocator()->Name()); |
153 | 155 |
154 // Final release so file and temp-dir can be removed. | 156 // Final release so file and temp-dir can be removed. |
155 GlobalHistogramAllocator::ReleaseForTesting(); | 157 GlobalHistogramAllocator::ReleaseForTesting(); |
156 } | 158 } |
157 | 159 |
158 TEST_F(PersistentHistogramAllocatorTest, StatisticsRecorderMergeTest) { | 160 TEST_F(PersistentHistogramAllocatorTest, CreateSpareFile) { |
| 161 const char temp_name[] = "CreateSpareFileTest.pma"; |
| 162 ScopedTempDir temp_dir; |
| 163 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 164 FilePath temp_file = temp_dir.GetPath().AppendASCII(temp_name); |
| 165 const size_t temp_size = 64 << 10; // 64 KiB |
| 166 |
| 167 ASSERT_TRUE(GlobalHistogramAllocator::CreateSpareFile(temp_file, temp_size)); |
| 168 |
| 169 File file(temp_file, File::FLAG_OPEN | File::FLAG_READ); |
| 170 ASSERT_TRUE(file.IsValid()); |
| 171 EXPECT_EQ(static_cast<int64_t>(temp_size), file.GetLength()); |
| 172 |
| 173 char buffer[256]; |
| 174 for (size_t pos = 0; pos < temp_size; pos += sizeof(buffer)) { |
| 175 ASSERT_EQ(static_cast<int>(sizeof(buffer)), |
| 176 file.ReadAtCurrentPos(buffer, sizeof(buffer))); |
| 177 for (size_t i = 0; i < sizeof(buffer); ++i) |
| 178 EXPECT_EQ(0, buffer[i]); |
| 179 } |
| 180 } |
| 181 |
| 182 TEST_F(PersistentHistogramAllocatorTest, StatisticsRecorderMerge) { |
159 const char LinearHistogramName[] = "SRTLinearHistogram"; | 183 const char LinearHistogramName[] = "SRTLinearHistogram"; |
160 const char SparseHistogramName[] = "SRTSparseHistogram"; | 184 const char SparseHistogramName[] = "SRTSparseHistogram"; |
161 const size_t starting_sr_count = StatisticsRecorder::GetHistogramCount(); | 185 const size_t starting_sr_count = StatisticsRecorder::GetHistogramCount(); |
162 | 186 |
163 // Create a local StatisticsRecorder in which the newly created histogram | 187 // Create a local StatisticsRecorder in which the newly created histogram |
164 // will be recorded. The global allocator must be replaced after because the | 188 // will be recorded. The global allocator must be replaced after because the |
165 // act of releasing will cause the active SR to forget about all histograms | 189 // act of releasing will cause the active SR to forget about all histograms |
166 // in the relased memory. | 190 // in the relased memory. |
167 std::unique_ptr<StatisticsRecorder> local_sr = | 191 std::unique_ptr<StatisticsRecorder> local_sr = |
168 StatisticsRecorder::CreateTemporaryForTesting(); | 192 StatisticsRecorder::CreateTemporaryForTesting(); |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 | 336 |
313 uint32_t* data1 = | 337 uint32_t* data1 = |
314 allocator_->GetAsArray<uint32_t>(ref1, 0, kRangesRefIndex + 1); | 338 allocator_->GetAsArray<uint32_t>(ref1, 0, kRangesRefIndex + 1); |
315 uint32_t* data2 = | 339 uint32_t* data2 = |
316 allocator_->GetAsArray<uint32_t>(ref2, 0, kRangesRefIndex + 1); | 340 allocator_->GetAsArray<uint32_t>(ref2, 0, kRangesRefIndex + 1); |
317 EXPECT_EQ(ranges_ref, data1[kRangesRefIndex]); | 341 EXPECT_EQ(ranges_ref, data1[kRangesRefIndex]); |
318 EXPECT_EQ(ranges_ref, data2[kRangesRefIndex]); | 342 EXPECT_EQ(ranges_ref, data2[kRangesRefIndex]); |
319 } | 343 } |
320 | 344 |
321 } // namespace base | 345 } // namespace base |
OLD | NEW |