OLD | NEW |
---|---|
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project 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 "src/base/utils/random-number-generator.h" | 5 #include "src/base/utils/random-number-generator.h" |
6 #include "src/compiler/zone-pool.h" | 6 #include "src/compiler/zonestats.h" |
7 #include "test/unittests/test-utils.h" | 7 #include "test/unittests/test-utils.h" |
8 | 8 |
9 namespace v8 { | 9 namespace v8 { |
10 namespace internal { | 10 namespace internal { |
11 namespace compiler { | 11 namespace compiler { |
12 | 12 |
13 class ZonePoolTest : public TestWithIsolate { | 13 class ZoneStatsTest : public TestWithIsolate { |
Michael Starzinger
2016/09/20 10:35:39
nit: The file should be renamed to zone-stats-unit
| |
14 public: | 14 public: |
15 ZonePoolTest() : zone_pool_(&allocator_) {} | 15 ZoneStatsTest() : zone_stats_(&allocator_) {} |
16 | 16 |
17 protected: | 17 protected: |
18 ZonePool* zone_pool() { return &zone_pool_; } | 18 ZoneStats* zone_stats() { return &zone_stats_; } |
19 | 19 |
20 void ExpectForPool(size_t current, size_t max, size_t total) { | 20 void ExpectForPool(size_t current, size_t max, size_t total) { |
21 ASSERT_EQ(current, zone_pool()->GetCurrentAllocatedBytes()); | 21 ASSERT_EQ(current, zone_stats()->GetCurrentAllocatedBytes()); |
22 ASSERT_EQ(max, zone_pool()->GetMaxAllocatedBytes()); | 22 ASSERT_EQ(max, zone_stats()->GetMaxAllocatedBytes()); |
23 ASSERT_EQ(total, zone_pool()->GetTotalAllocatedBytes()); | 23 ASSERT_EQ(total, zone_stats()->GetTotalAllocatedBytes()); |
24 } | 24 } |
25 | 25 |
26 void Expect(ZonePool::StatsScope* stats, size_t current, size_t max, | 26 void Expect(ZoneStats::StatsScope* stats, size_t current, size_t max, |
27 size_t total) { | 27 size_t total) { |
28 ASSERT_EQ(current, stats->GetCurrentAllocatedBytes()); | 28 ASSERT_EQ(current, stats->GetCurrentAllocatedBytes()); |
29 ASSERT_EQ(max, stats->GetMaxAllocatedBytes()); | 29 ASSERT_EQ(max, stats->GetMaxAllocatedBytes()); |
30 ASSERT_EQ(total, stats->GetTotalAllocatedBytes()); | 30 ASSERT_EQ(total, stats->GetTotalAllocatedBytes()); |
31 } | 31 } |
32 | 32 |
33 size_t Allocate(Zone* zone) { | 33 size_t Allocate(Zone* zone) { |
34 size_t bytes = rng.NextInt(25) + 7; | 34 size_t bytes = rng.NextInt(25) + 7; |
35 size_t size_before = zone->allocation_size(); | 35 size_t size_before = zone->allocation_size(); |
36 zone->New(bytes); | 36 zone->New(bytes); |
37 return zone->allocation_size() - size_before; | 37 return zone->allocation_size() - size_before; |
38 } | 38 } |
39 | 39 |
40 private: | 40 private: |
41 v8::internal::AccountingAllocator allocator_; | 41 v8::internal::AccountingAllocator allocator_; |
42 ZonePool zone_pool_; | 42 ZoneStats zone_stats_; |
43 base::RandomNumberGenerator rng; | 43 base::RandomNumberGenerator rng; |
44 }; | 44 }; |
45 | 45 |
46 | 46 TEST_F(ZoneStatsTest, Empty) { |
47 TEST_F(ZonePoolTest, Empty) { | |
48 ExpectForPool(0, 0, 0); | 47 ExpectForPool(0, 0, 0); |
49 { | 48 { |
50 ZonePool::StatsScope stats(zone_pool()); | 49 ZoneStats::StatsScope stats(zone_stats()); |
51 Expect(&stats, 0, 0, 0); | 50 Expect(&stats, 0, 0, 0); |
52 } | 51 } |
53 ExpectForPool(0, 0, 0); | 52 ExpectForPool(0, 0, 0); |
54 { | 53 { |
55 ZonePool::Scope scope(zone_pool()); | 54 ZoneStats::Scope scope(zone_stats()); |
56 scope.zone(); | 55 scope.zone(); |
57 } | 56 } |
58 ExpectForPool(0, 0, 0); | 57 ExpectForPool(0, 0, 0); |
59 } | 58 } |
60 | 59 |
61 | 60 TEST_F(ZoneStatsTest, MultipleZonesWithDeletion) { |
62 TEST_F(ZonePoolTest, MultipleZonesWithDeletion) { | |
63 static const size_t kArraySize = 10; | 61 static const size_t kArraySize = 10; |
64 | 62 |
65 ZonePool::Scope* scopes[kArraySize]; | 63 ZoneStats::Scope* scopes[kArraySize]; |
66 | 64 |
67 // Initialize. | 65 // Initialize. |
68 size_t before_stats = 0; | 66 size_t before_stats = 0; |
69 for (size_t i = 0; i < kArraySize; ++i) { | 67 for (size_t i = 0; i < kArraySize; ++i) { |
70 scopes[i] = new ZonePool::Scope(zone_pool()); | 68 scopes[i] = new ZoneStats::Scope(zone_stats()); |
71 before_stats += Allocate(scopes[i]->zone()); // Add some stuff. | 69 before_stats += Allocate(scopes[i]->zone()); // Add some stuff. |
72 } | 70 } |
73 | 71 |
74 ExpectForPool(before_stats, before_stats, before_stats); | 72 ExpectForPool(before_stats, before_stats, before_stats); |
75 | 73 |
76 ZonePool::StatsScope stats(zone_pool()); | 74 ZoneStats::StatsScope stats(zone_stats()); |
77 | 75 |
78 size_t before_deletion = 0; | 76 size_t before_deletion = 0; |
79 for (size_t i = 0; i < kArraySize; ++i) { | 77 for (size_t i = 0; i < kArraySize; ++i) { |
80 before_deletion += Allocate(scopes[i]->zone()); // Add some stuff. | 78 before_deletion += Allocate(scopes[i]->zone()); // Add some stuff. |
81 } | 79 } |
82 | 80 |
83 Expect(&stats, before_deletion, before_deletion, before_deletion); | 81 Expect(&stats, before_deletion, before_deletion, before_deletion); |
84 ExpectForPool(before_stats + before_deletion, before_stats + before_deletion, | 82 ExpectForPool(before_stats + before_deletion, before_stats + before_deletion, |
85 before_stats + before_deletion); | 83 before_stats + before_deletion); |
86 | 84 |
87 // Delete the scopes and create new ones. | 85 // Delete the scopes and create new ones. |
88 for (size_t i = 0; i < kArraySize; ++i) { | 86 for (size_t i = 0; i < kArraySize; ++i) { |
89 delete scopes[i]; | 87 delete scopes[i]; |
90 scopes[i] = new ZonePool::Scope(zone_pool()); | 88 scopes[i] = new ZoneStats::Scope(zone_stats()); |
91 } | 89 } |
92 | 90 |
93 Expect(&stats, 0, before_deletion, before_deletion); | 91 Expect(&stats, 0, before_deletion, before_deletion); |
94 ExpectForPool(0, before_stats + before_deletion, | 92 ExpectForPool(0, before_stats + before_deletion, |
95 before_stats + before_deletion); | 93 before_stats + before_deletion); |
96 | 94 |
97 size_t after_deletion = 0; | 95 size_t after_deletion = 0; |
98 for (size_t i = 0; i < kArraySize; ++i) { | 96 for (size_t i = 0; i < kArraySize; ++i) { |
99 after_deletion += Allocate(scopes[i]->zone()); // Add some stuff. | 97 after_deletion += Allocate(scopes[i]->zone()); // Add some stuff. |
100 } | 98 } |
101 | 99 |
102 Expect(&stats, after_deletion, std::max(after_deletion, before_deletion), | 100 Expect(&stats, after_deletion, std::max(after_deletion, before_deletion), |
103 before_deletion + after_deletion); | 101 before_deletion + after_deletion); |
104 ExpectForPool(after_deletion, | 102 ExpectForPool(after_deletion, |
105 std::max(after_deletion, before_stats + before_deletion), | 103 std::max(after_deletion, before_stats + before_deletion), |
106 before_stats + before_deletion + after_deletion); | 104 before_stats + before_deletion + after_deletion); |
107 | 105 |
108 // Cleanup. | 106 // Cleanup. |
109 for (size_t i = 0; i < kArraySize; ++i) { | 107 for (size_t i = 0; i < kArraySize; ++i) { |
110 delete scopes[i]; | 108 delete scopes[i]; |
111 } | 109 } |
112 | 110 |
113 Expect(&stats, 0, std::max(after_deletion, before_deletion), | 111 Expect(&stats, 0, std::max(after_deletion, before_deletion), |
114 before_deletion + after_deletion); | 112 before_deletion + after_deletion); |
115 ExpectForPool(0, std::max(after_deletion, before_stats + before_deletion), | 113 ExpectForPool(0, std::max(after_deletion, before_stats + before_deletion), |
116 before_stats + before_deletion + after_deletion); | 114 before_stats + before_deletion + after_deletion); |
117 } | 115 } |
118 | 116 |
119 | 117 TEST_F(ZoneStatsTest, SimpleAllocationLoop) { |
120 TEST_F(ZonePoolTest, SimpleAllocationLoop) { | |
121 int runs = 20; | 118 int runs = 20; |
122 size_t total_allocated = 0; | 119 size_t total_allocated = 0; |
123 size_t max_loop_allocation = 0; | 120 size_t max_loop_allocation = 0; |
124 ZonePool::StatsScope outer_stats(zone_pool()); | 121 ZoneStats::StatsScope outer_stats(zone_stats()); |
125 { | 122 { |
126 ZonePool::Scope outer_scope(zone_pool()); | 123 ZoneStats::Scope outer_scope(zone_stats()); |
127 size_t outer_allocated = 0; | 124 size_t outer_allocated = 0; |
128 for (int i = 0; i < runs; ++i) { | 125 for (int i = 0; i < runs; ++i) { |
129 { | 126 { |
130 size_t bytes = Allocate(outer_scope.zone()); | 127 size_t bytes = Allocate(outer_scope.zone()); |
131 outer_allocated += bytes; | 128 outer_allocated += bytes; |
132 total_allocated += bytes; | 129 total_allocated += bytes; |
133 } | 130 } |
134 ZonePool::StatsScope inner_stats(zone_pool()); | 131 ZoneStats::StatsScope inner_stats(zone_stats()); |
135 size_t allocated = 0; | 132 size_t allocated = 0; |
136 { | 133 { |
137 ZonePool::Scope inner_scope(zone_pool()); | 134 ZoneStats::Scope inner_scope(zone_stats()); |
138 for (int j = 0; j < 20; ++j) { | 135 for (int j = 0; j < 20; ++j) { |
139 size_t bytes = Allocate(inner_scope.zone()); | 136 size_t bytes = Allocate(inner_scope.zone()); |
140 allocated += bytes; | 137 allocated += bytes; |
141 total_allocated += bytes; | 138 total_allocated += bytes; |
142 max_loop_allocation = | 139 max_loop_allocation = |
143 std::max(max_loop_allocation, outer_allocated + allocated); | 140 std::max(max_loop_allocation, outer_allocated + allocated); |
144 Expect(&inner_stats, allocated, allocated, allocated); | 141 Expect(&inner_stats, allocated, allocated, allocated); |
145 Expect(&outer_stats, outer_allocated + allocated, max_loop_allocation, | 142 Expect(&outer_stats, outer_allocated + allocated, max_loop_allocation, |
146 total_allocated); | 143 total_allocated); |
147 ExpectForPool(outer_allocated + allocated, max_loop_allocation, | 144 ExpectForPool(outer_allocated + allocated, max_loop_allocation, |
148 total_allocated); | 145 total_allocated); |
149 } | 146 } |
150 } | 147 } |
151 Expect(&inner_stats, 0, allocated, allocated); | 148 Expect(&inner_stats, 0, allocated, allocated); |
152 Expect(&outer_stats, outer_allocated, max_loop_allocation, | 149 Expect(&outer_stats, outer_allocated, max_loop_allocation, |
153 total_allocated); | 150 total_allocated); |
154 ExpectForPool(outer_allocated, max_loop_allocation, total_allocated); | 151 ExpectForPool(outer_allocated, max_loop_allocation, total_allocated); |
155 } | 152 } |
156 } | 153 } |
157 Expect(&outer_stats, 0, max_loop_allocation, total_allocated); | 154 Expect(&outer_stats, 0, max_loop_allocation, total_allocated); |
158 ExpectForPool(0, max_loop_allocation, total_allocated); | 155 ExpectForPool(0, max_loop_allocation, total_allocated); |
159 } | 156 } |
160 | 157 |
161 } // namespace compiler | 158 } // namespace compiler |
162 } // namespace internal | 159 } // namespace internal |
163 } // namespace v8 | 160 } // namespace v8 |
OLD | NEW |