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
3 // found in the LICENSE file.
5 #include "src/base/utils/random-number-generator.h"
6 #include "src/compiler/zone-pool.h"
7 #include "test/unittests/test-utils.h"
13 class ZonePoolTest : public TestWithIsolate {
15 ZonePoolTest() : zone_pool_(isolate()) {}
18 ZonePool* zone_pool() { return &zone_pool_; }
20 void ExpectForPool(size_t current, size_t max, size_t total) {
21 ASSERT_EQ(current, zone_pool()->GetCurrentAllocatedBytes());
22 ASSERT_EQ(max, zone_pool()->GetMaxAllocatedBytes());
23 ASSERT_EQ(total, zone_pool()->GetTotalAllocatedBytes());
26 void Expect(ZonePool::StatsScope* stats, size_t current, size_t max,
28 ASSERT_EQ(current, stats->GetCurrentAllocatedBytes());
29 ASSERT_EQ(max, stats->GetMaxAllocatedBytes());
30 ASSERT_EQ(total, stats->GetTotalAllocatedBytes());
33 size_t Allocate(Zone* zone) {
34 size_t bytes = rng.NextInt(25) + 7;
35 int size_before = zone->allocation_size();
36 zone->New(static_cast<int>(bytes));
37 return static_cast<size_t>(zone->allocation_size() - size_before);
42 base::RandomNumberGenerator rng;
46 TEST_F(ZonePoolTest, Empty) {
47 ExpectForPool(0, 0, 0);
49 ZonePool::StatsScope stats(zone_pool());
50 Expect(&stats, 0, 0, 0);
52 ExpectForPool(0, 0, 0);
54 ZonePool::Scope scope(zone_pool());
57 ExpectForPool(0, 0, 0);
61 TEST_F(ZonePoolTest, MultipleZonesWithDeletion) {
62 static const size_t kArraySize = 10;
64 ZonePool::Scope* scopes[kArraySize];
67 size_t before_stats = 0;
68 for (size_t i = 0; i < kArraySize; ++i) {
69 scopes[i] = new ZonePool::Scope(zone_pool());
70 before_stats += Allocate(scopes[i]->zone()); // Add some stuff.
73 ExpectForPool(before_stats, before_stats, before_stats);
75 ZonePool::StatsScope stats(zone_pool());
77 size_t before_deletion = 0;
78 for (size_t i = 0; i < kArraySize; ++i) {
79 before_deletion += Allocate(scopes[i]->zone()); // Add some stuff.
82 Expect(&stats, before_deletion, before_deletion, before_deletion);
83 ExpectForPool(before_stats + before_deletion, before_stats + before_deletion,
84 before_stats + before_deletion);
86 // Delete the scopes and create new ones.
87 for (size_t i = 0; i < kArraySize; ++i) {
89 scopes[i] = new ZonePool::Scope(zone_pool());
92 Expect(&stats, 0, before_deletion, before_deletion);
93 ExpectForPool(0, before_stats + before_deletion,
94 before_stats + before_deletion);
96 size_t after_deletion = 0;
97 for (size_t i = 0; i < kArraySize; ++i) {
98 after_deletion += Allocate(scopes[i]->zone()); // Add some stuff.
101 Expect(&stats, after_deletion, std::max(after_deletion, before_deletion),
102 before_deletion + after_deletion);
103 ExpectForPool(after_deletion,
104 std::max(after_deletion, before_stats + before_deletion),
105 before_stats + before_deletion + after_deletion);
108 for (size_t i = 0; i < kArraySize; ++i) {
112 Expect(&stats, 0, std::max(after_deletion, before_deletion),
113 before_deletion + after_deletion);
114 ExpectForPool(0, std::max(after_deletion, before_stats + before_deletion),
115 before_stats + before_deletion + after_deletion);
119 TEST_F(ZonePoolTest, SimpleAllocationLoop) {
121 size_t total_allocated = 0;
122 size_t max_loop_allocation = 0;
123 ZonePool::StatsScope outer_stats(zone_pool());
125 ZonePool::Scope outer_scope(zone_pool());
126 size_t outer_allocated = 0;
127 for (int i = 0; i < runs; ++i) {
129 size_t bytes = Allocate(outer_scope.zone());
130 outer_allocated += bytes;
131 total_allocated += bytes;
133 ZonePool::StatsScope inner_stats(zone_pool());
134 size_t allocated = 0;
136 ZonePool::Scope inner_scope(zone_pool());
137 for (int j = 0; j < 20; ++j) {
138 size_t bytes = Allocate(inner_scope.zone());
140 total_allocated += bytes;
141 max_loop_allocation =
142 std::max(max_loop_allocation, outer_allocated + allocated);
143 Expect(&inner_stats, allocated, allocated, allocated);
144 Expect(&outer_stats, outer_allocated + allocated, max_loop_allocation,
146 ExpectForPool(outer_allocated + allocated, max_loop_allocation,
150 Expect(&inner_stats, 0, allocated, allocated);
151 Expect(&outer_stats, outer_allocated, max_loop_allocation,
153 ExpectForPool(outer_allocated, max_loop_allocation, total_allocated);
156 Expect(&outer_stats, 0, max_loop_allocation, total_allocated);
157 ExpectForPool(0, max_loop_allocation, total_allocated);
160 } // namespace compiler
161 } // namespace internal