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