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