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/zone-pool.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 ZonePoolTest : public TestWithIsolate { |
14 public: | 14 public: |
15 ZonePoolTest() : zone_pool_(isolate()) {} | 15 ZonePoolTest() : zone_pool_(isolate()) {} |
16 | 16 |
17 protected: | 17 protected: |
18 ZonePool* zone_pool() { return &zone_pool_; } | 18 ZonePool* zone_pool() { return &zone_pool_; } |
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_pool()->GetCurrentAllocatedBytes()); |
22 ASSERT_EQ(max, zone_pool()->GetMaxAllocatedBytes()); | 22 ASSERT_EQ(max, zone_pool()->GetMaxAllocatedBytes()); |
23 ASSERT_EQ(total, zone_pool()->GetTotalAllocatedBytes()); | 23 ASSERT_EQ(total, zone_pool()->GetTotalAllocatedBytes()); |
24 } | 24 } |
25 | 25 |
26 void Expect(ZonePool::StatsScope* stats, size_t current, size_t max) { | 26 void Expect(ZonePool::StatsScope* stats, size_t current, size_t max, |
| 27 size_t total) { |
27 ASSERT_EQ(current, stats->GetCurrentAllocatedBytes()); | 28 ASSERT_EQ(current, stats->GetCurrentAllocatedBytes()); |
28 ASSERT_EQ(max, stats->GetMaxAllocatedBytes()); | 29 ASSERT_EQ(max, stats->GetMaxAllocatedBytes()); |
| 30 ASSERT_EQ(total, stats->GetTotalAllocatedBytes()); |
29 } | 31 } |
30 | 32 |
31 size_t Allocate(Zone* zone) { | 33 size_t Allocate(Zone* zone) { |
32 size_t bytes = rng.NextInt(25) + 7; | 34 size_t bytes = rng.NextInt(25) + 7; |
33 int size_before = zone->allocation_size(); | 35 int size_before = zone->allocation_size(); |
34 zone->New(static_cast<int>(bytes)); | 36 zone->New(static_cast<int>(bytes)); |
35 return static_cast<size_t>(zone->allocation_size() - size_before); | 37 return static_cast<size_t>(zone->allocation_size() - size_before); |
36 } | 38 } |
37 | 39 |
38 private: | 40 private: |
39 ZonePool zone_pool_; | 41 ZonePool zone_pool_; |
40 base::RandomNumberGenerator rng; | 42 base::RandomNumberGenerator rng; |
41 }; | 43 }; |
42 | 44 |
43 | 45 |
44 TEST_F(ZonePoolTest, Empty) { | 46 TEST_F(ZonePoolTest, Empty) { |
45 ExpectForPool(0, 0, 0); | 47 ExpectForPool(0, 0, 0); |
46 { | 48 { |
47 ZonePool::StatsScope stats(zone_pool()); | 49 ZonePool::StatsScope stats(zone_pool()); |
48 Expect(&stats, 0, 0); | 50 Expect(&stats, 0, 0, 0); |
49 } | 51 } |
50 ExpectForPool(0, 0, 0); | 52 ExpectForPool(0, 0, 0); |
51 { | 53 { |
52 ZonePool::Scope scope(zone_pool()); | 54 ZonePool::Scope scope(zone_pool()); |
53 scope.zone(); | 55 scope.zone(); |
54 } | 56 } |
55 ExpectForPool(0, 0, 0); | 57 ExpectForPool(0, 0, 0); |
56 } | 58 } |
57 | 59 |
58 | 60 |
(...skipping 11 matching lines...) Expand all Loading... |
70 | 72 |
71 ExpectForPool(before_stats, before_stats, before_stats); | 73 ExpectForPool(before_stats, before_stats, before_stats); |
72 | 74 |
73 ZonePool::StatsScope stats(zone_pool()); | 75 ZonePool::StatsScope stats(zone_pool()); |
74 | 76 |
75 size_t before_deletion = 0; | 77 size_t before_deletion = 0; |
76 for (size_t i = 0; i < kArraySize; ++i) { | 78 for (size_t i = 0; i < kArraySize; ++i) { |
77 before_deletion += Allocate(scopes[i]->zone()); // Add some stuff. | 79 before_deletion += Allocate(scopes[i]->zone()); // Add some stuff. |
78 } | 80 } |
79 | 81 |
80 Expect(&stats, before_deletion, before_deletion); | 82 Expect(&stats, before_deletion, before_deletion, before_deletion); |
81 ExpectForPool(before_stats + before_deletion, before_stats + before_deletion, | 83 ExpectForPool(before_stats + before_deletion, before_stats + before_deletion, |
82 before_stats + before_deletion); | 84 before_stats + before_deletion); |
83 | 85 |
84 // Delete the scopes and create new ones. | 86 // Delete the scopes and create new ones. |
85 for (size_t i = 0; i < kArraySize; ++i) { | 87 for (size_t i = 0; i < kArraySize; ++i) { |
86 delete scopes[i]; | 88 delete scopes[i]; |
87 scopes[i] = new ZonePool::Scope(zone_pool()); | 89 scopes[i] = new ZonePool::Scope(zone_pool()); |
88 } | 90 } |
89 | 91 |
90 Expect(&stats, 0, before_deletion); | 92 Expect(&stats, 0, before_deletion, before_deletion); |
91 ExpectForPool(0, before_stats + before_deletion, | 93 ExpectForPool(0, before_stats + before_deletion, |
92 before_stats + before_deletion); | 94 before_stats + before_deletion); |
93 | 95 |
94 size_t after_deletion = 0; | 96 size_t after_deletion = 0; |
95 for (size_t i = 0; i < kArraySize; ++i) { | 97 for (size_t i = 0; i < kArraySize; ++i) { |
96 after_deletion += Allocate(scopes[i]->zone()); // Add some stuff. | 98 after_deletion += Allocate(scopes[i]->zone()); // Add some stuff. |
97 } | 99 } |
98 | 100 |
99 Expect(&stats, after_deletion, std::max(after_deletion, before_deletion)); | 101 Expect(&stats, after_deletion, std::max(after_deletion, before_deletion), |
| 102 before_deletion + after_deletion); |
100 ExpectForPool(after_deletion, | 103 ExpectForPool(after_deletion, |
101 std::max(after_deletion, before_stats + before_deletion), | 104 std::max(after_deletion, before_stats + before_deletion), |
102 before_stats + before_deletion + after_deletion); | 105 before_stats + before_deletion + after_deletion); |
103 | 106 |
104 // Cleanup. | 107 // Cleanup. |
105 for (size_t i = 0; i < kArraySize; ++i) { | 108 for (size_t i = 0; i < kArraySize; ++i) { |
106 delete scopes[i]; | 109 delete scopes[i]; |
107 } | 110 } |
108 | 111 |
109 Expect(&stats, 0, std::max(after_deletion, before_deletion)); | 112 Expect(&stats, 0, std::max(after_deletion, before_deletion), |
| 113 before_deletion + after_deletion); |
110 ExpectForPool(0, std::max(after_deletion, before_stats + before_deletion), | 114 ExpectForPool(0, std::max(after_deletion, before_stats + before_deletion), |
111 before_stats + before_deletion + after_deletion); | 115 before_stats + before_deletion + after_deletion); |
112 } | 116 } |
113 | 117 |
114 | 118 |
115 TEST_F(ZonePoolTest, SimpleAllocationLoop) { | 119 TEST_F(ZonePoolTest, SimpleAllocationLoop) { |
116 int runs = 20; | 120 int runs = 20; |
117 size_t total_allocated = 0; | 121 size_t total_allocated = 0; |
118 size_t max_loop_allocation = 0; | 122 size_t max_loop_allocation = 0; |
119 ZonePool::StatsScope outer_stats(zone_pool()); | 123 ZonePool::StatsScope outer_stats(zone_pool()); |
120 { | 124 { |
121 ZonePool::Scope outer_scope(zone_pool()); | 125 ZonePool::Scope outer_scope(zone_pool()); |
122 size_t outer_allocated = 0; | 126 size_t outer_allocated = 0; |
123 for (int i = 0; i < runs; ++i) { | 127 for (int i = 0; i < runs; ++i) { |
124 { | 128 { |
125 size_t bytes = Allocate(outer_scope.zone()); | 129 size_t bytes = Allocate(outer_scope.zone()); |
126 outer_allocated += bytes; | 130 outer_allocated += bytes; |
127 total_allocated += bytes; | 131 total_allocated += bytes; |
128 } | 132 } |
129 ZonePool::StatsScope inner_stats(zone_pool()); | 133 ZonePool::StatsScope inner_stats(zone_pool()); |
130 size_t allocated = 0; | 134 size_t allocated = 0; |
131 { | 135 { |
132 ZonePool::Scope inner_scope(zone_pool()); | 136 ZonePool::Scope inner_scope(zone_pool()); |
133 for (int j = 0; j < 20; ++j) { | 137 for (int j = 0; j < 20; ++j) { |
134 size_t bytes = Allocate(inner_scope.zone()); | 138 size_t bytes = Allocate(inner_scope.zone()); |
135 allocated += bytes; | 139 allocated += bytes; |
136 total_allocated += bytes; | 140 total_allocated += bytes; |
137 max_loop_allocation = | 141 max_loop_allocation = |
138 std::max(max_loop_allocation, outer_allocated + allocated); | 142 std::max(max_loop_allocation, outer_allocated + allocated); |
139 Expect(&inner_stats, allocated, allocated); | 143 Expect(&inner_stats, allocated, allocated, allocated); |
140 Expect(&outer_stats, outer_allocated + allocated, | 144 Expect(&outer_stats, outer_allocated + allocated, max_loop_allocation, |
141 max_loop_allocation); | 145 total_allocated); |
142 ExpectForPool(outer_allocated + allocated, max_loop_allocation, | 146 ExpectForPool(outer_allocated + allocated, max_loop_allocation, |
143 total_allocated); | 147 total_allocated); |
144 } | 148 } |
145 } | 149 } |
146 Expect(&inner_stats, 0, allocated); | 150 Expect(&inner_stats, 0, allocated, allocated); |
147 Expect(&outer_stats, outer_allocated, max_loop_allocation); | 151 Expect(&outer_stats, outer_allocated, max_loop_allocation, |
| 152 total_allocated); |
148 ExpectForPool(outer_allocated, max_loop_allocation, total_allocated); | 153 ExpectForPool(outer_allocated, max_loop_allocation, total_allocated); |
149 } | 154 } |
150 } | 155 } |
151 Expect(&outer_stats, 0, max_loop_allocation); | 156 Expect(&outer_stats, 0, max_loop_allocation, total_allocated); |
152 ExpectForPool(0, max_loop_allocation, total_allocated); | 157 ExpectForPool(0, max_loop_allocation, total_allocated); |
153 } | 158 } |
154 | 159 |
155 } // namespace compiler | 160 } // namespace compiler |
156 } // namespace internal | 161 } // namespace internal |
157 } // namespace v8 | 162 } // namespace v8 |
OLD | NEW |