OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/histogram.h" | 5 #include "base/metrics/histogram.h" |
6 | 6 |
7 #include <limits.h> | 7 #include <limits.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
(...skipping 19 matching lines...) Expand all Loading... |
30 // Test parameter indicates if a persistent memory allocator should be used | 30 // Test parameter indicates if a persistent memory allocator should be used |
31 // for histogram allocation. False will allocate histograms from the process | 31 // for histogram allocation. False will allocate histograms from the process |
32 // heap. | 32 // heap. |
33 class HistogramTest : public testing::TestWithParam<bool> { | 33 class HistogramTest : public testing::TestWithParam<bool> { |
34 protected: | 34 protected: |
35 const int32_t kAllocatorMemorySize = 8 << 20; // 8 MiB | 35 const int32_t kAllocatorMemorySize = 8 << 20; // 8 MiB |
36 | 36 |
37 HistogramTest() : use_persistent_histogram_allocator_(GetParam()) {} | 37 HistogramTest() : use_persistent_histogram_allocator_(GetParam()) {} |
38 | 38 |
39 void SetUp() override { | 39 void SetUp() override { |
| 40 if (use_persistent_histogram_allocator_) |
| 41 CreatePersistentHistogramAllocator(); |
| 42 |
40 // Each test will have a clean state (no Histogram / BucketRanges | 43 // Each test will have a clean state (no Histogram / BucketRanges |
41 // registered). | 44 // registered). |
42 InitializeStatisticsRecorder(); | 45 InitializeStatisticsRecorder(); |
43 if (use_persistent_histogram_allocator_) | |
44 CreatePersistentHistogramAllocator(); | |
45 } | 46 } |
46 | 47 |
47 void TearDown() override { | 48 void TearDown() override { |
48 if (allocator_) { | 49 if (allocator_) { |
49 ASSERT_FALSE(allocator_->IsFull()); | 50 ASSERT_FALSE(allocator_->IsFull()); |
50 ASSERT_FALSE(allocator_->IsCorrupt()); | 51 ASSERT_FALSE(allocator_->IsCorrupt()); |
51 } | 52 } |
52 UninitializeStatisticsRecorder(); | 53 UninitializeStatisticsRecorder(); |
53 DestroyPersistentHistogramAllocator(); | 54 DestroyPersistentHistogramAllocator(); |
54 } | 55 } |
55 | 56 |
56 void InitializeStatisticsRecorder() { | 57 void InitializeStatisticsRecorder() { |
57 StatisticsRecorder::ResetForTesting(); | 58 StatisticsRecorder::ResetForTesting(); |
58 statistics_recorder_ = new StatisticsRecorder(); | 59 statistics_recorder_ = new StatisticsRecorder(); |
59 } | 60 } |
60 | 61 |
61 void UninitializeStatisticsRecorder() { | 62 void UninitializeStatisticsRecorder() { |
62 delete statistics_recorder_; | 63 delete statistics_recorder_; |
63 statistics_recorder_ = NULL; | 64 statistics_recorder_ = NULL; |
64 } | 65 } |
65 | 66 |
66 void CreatePersistentHistogramAllocator() { | 67 void CreatePersistentHistogramAllocator() { |
67 // By getting the results-histogram before any persistent allocator | 68 // By getting the results-histogram before any persistent allocator |
68 // is attached, that histogram is guaranteed not to be stored in | 69 // is attached, that histogram is guaranteed not to be stored in |
69 // any persistent memory segment (which simplifies some tests). | 70 // any persistent memory segment (which simplifies some tests). |
70 PersistentHistogramAllocator::GetCreateHistogramResultHistogram(); | 71 PersistentHistogramAllocator::GetCreateHistogramResultHistogram(); |
71 | 72 |
72 if (!allocator_memory_) | 73 PersistentHistogramAllocator::CreateGlobalAllocatorOnLocalMemory( |
73 allocator_memory_.reset(new char[kAllocatorMemorySize]); | 74 kAllocatorMemorySize, 0, "HistogramAllocatorTest"); |
74 | |
75 PersistentHistogramAllocator::ReleaseGlobalAllocatorForTesting(); | |
76 memset(allocator_memory_.get(), 0, kAllocatorMemorySize); | |
77 PersistentHistogramAllocator::CreateGlobalAllocatorOnPersistentMemory( | |
78 allocator_memory_.get(), kAllocatorMemorySize, 0, 0, | |
79 "HistogramAllocatorTest"); | |
80 allocator_ = | 75 allocator_ = |
81 PersistentHistogramAllocator::GetGlobalAllocator()->memory_allocator(); | 76 PersistentHistogramAllocator::GetGlobalAllocator()->memory_allocator(); |
82 } | 77 } |
83 | 78 |
84 void DestroyPersistentHistogramAllocator() { | 79 void DestroyPersistentHistogramAllocator() { |
85 allocator_ = nullptr; | 80 allocator_ = nullptr; |
86 PersistentHistogramAllocator::ReleaseGlobalAllocatorForTesting(); | 81 PersistentHistogramAllocator::ReleaseGlobalAllocatorForTesting(); |
87 } | 82 } |
88 | 83 |
89 const bool use_persistent_histogram_allocator_; | 84 const bool use_persistent_histogram_allocator_; |
(...skipping 618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
708 // CustomHistogram needs at least 1 valid range. | 703 // CustomHistogram needs at least 1 valid range. |
709 custom_ranges.clear(); | 704 custom_ranges.clear(); |
710 custom_ranges.push_back(0); | 705 custom_ranges.push_back(0); |
711 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, | 706 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, |
712 HistogramBase::kNoFlags), | 707 HistogramBase::kNoFlags), |
713 ""); | 708 ""); |
714 } | 709 } |
715 #endif | 710 #endif |
716 | 711 |
717 } // namespace base | 712 } // namespace base |
OLD | NEW |