|
1 /* |
|
2 * Copyright (c) 2019, SAP SE. All rights reserved. |
|
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
5 * |
|
6 * This code is free software; you can redistribute it and/or modify it |
|
7 * under the terms of the GNU General Public License version 2 only, as |
|
8 * published by the Free Software Foundation. |
|
9 * |
|
10 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 * version 2 for more details (a copy is included in the LICENSE file that |
|
14 * accompanied this code). |
|
15 * |
|
16 * You should have received a copy of the GNU General Public License version |
|
17 * 2 along with this work; if not, write to the Free Software Foundation, |
|
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 * |
|
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
21 * or visit www.oracle.com if you need additional information or have any |
|
22 * questions. |
|
23 */ |
|
24 |
|
25 |
|
26 #include "precompiled.hpp" |
|
27 |
|
28 //#define LOG_PLEASE |
|
29 |
|
30 #include "metaspaceTestsCommon.hpp" |
|
31 |
|
32 class LeftOverBinsTest { |
|
33 |
|
34 // A simple preallocated buffer used to "feed" the allocator. |
|
35 // Mimicks chunk retirement leftover blocks. |
|
36 class FeederBuffer { |
|
37 |
|
38 static const size_t buf_word_size = 512 * K; |
|
39 MetaWord* _buf; |
|
40 size_t _used; |
|
41 |
|
42 public: |
|
43 |
|
44 FeederBuffer() : _used(0) { |
|
45 _buf = NEW_C_HEAP_ARRAY(MetaWord, buf_word_size, mtInternal); |
|
46 } |
|
47 |
|
48 ~FeederBuffer() { |
|
49 FREE_C_HEAP_ARRAY(MetaWord, _buf); |
|
50 } |
|
51 |
|
52 MetaWord* get(size_t word_size) { |
|
53 if (_used > (buf_word_size - word_size)) { |
|
54 return NULL; |
|
55 } |
|
56 MetaWord* p = _buf + _used; |
|
57 _used += word_size; |
|
58 return p; |
|
59 } |
|
60 |
|
61 }; |
|
62 |
|
63 FeederBuffer _fb; |
|
64 LeftOverManager _lom; |
|
65 |
|
66 // random generator for block feeding |
|
67 RandSizeGenerator _rgen_feeding; |
|
68 |
|
69 // random generator for allocations (and, hence, deallocations) |
|
70 RandSizeGenerator _rgen_allocations; |
|
71 |
|
72 SizeCounter _allocated_words; |
|
73 |
|
74 struct allocation_t { |
|
75 allocation_t* next; |
|
76 size_t word_size; |
|
77 MetaWord* p; |
|
78 }; |
|
79 |
|
80 // Array of the same size as the pool max capacity; holds the allocated elements. |
|
81 allocation_t* _allocations; |
|
82 |
|
83 |
|
84 int _num_allocs; |
|
85 int _num_deallocs; |
|
86 int _num_feeds; |
|
87 |
|
88 bool feed_some() { |
|
89 size_t word_size = _rgen_feeding.get(); |
|
90 MetaWord* p = _fb.get(word_size); |
|
91 if (p != NULL) { |
|
92 _lom.add_block(p, word_size); |
|
93 return true; |
|
94 } |
|
95 return false; |
|
96 } |
|
97 |
|
98 void deallocate_top() { |
|
99 |
|
100 allocation_t* a = _allocations; |
|
101 if (a != NULL) { |
|
102 _allocations = a->next; |
|
103 check_marked_range(a->p, a->word_size); |
|
104 _lom.add_block(a->p, a->word_size); |
|
105 delete a; |
|
106 DEBUG_ONLY(_lom.verify();) |
|
107 } |
|
108 } |
|
109 |
|
110 bool allocate() { |
|
111 |
|
112 size_t word_size = MAX2(_rgen_allocations.get(), _lom.minimal_word_size()); |
|
113 MetaWord* p = _lom.get_block(word_size); |
|
114 if (p != NULL) { |
|
115 _allocated_words.increment_by(word_size); |
|
116 allocation_t* a = new allocation_t; |
|
117 a->p = p; a->word_size = word_size; |
|
118 a->next = _allocations; |
|
119 _allocations = a; |
|
120 DEBUG_ONLY(_lom.verify();) |
|
121 mark_range(p, word_size); |
|
122 return true; |
|
123 } |
|
124 return false; |
|
125 } |
|
126 |
|
127 void test_all_marked_ranges() { |
|
128 for (allocation_t* a = _allocations; a != NULL; a = a->next) { |
|
129 check_marked_range(a->p, a->word_size); |
|
130 } |
|
131 } |
|
132 |
|
133 void test_loop() { |
|
134 // We loop and in each iteration execute one of three operations: |
|
135 // - allocation from lom |
|
136 // - deallocation to lom of a previously allocated block |
|
137 // - feeding a new larger block into the lom (mimicks chunk retiring) |
|
138 // When we have fed all large blocks into the lom (feedbuffer empty), we |
|
139 // switch to draining the lom completely (only allocs) |
|
140 bool forcefeed = false; |
|
141 bool draining = false; |
|
142 bool stop = false; |
|
143 int iter = 100000; // safety stop |
|
144 while (!stop && iter > 0) { |
|
145 iter --; |
|
146 int surprise = (int)os::random() % 10; |
|
147 if (!draining && (surprise >= 7 || forcefeed)) { |
|
148 forcefeed = false; |
|
149 if (feed_some()) { |
|
150 _num_feeds ++; |
|
151 } else { |
|
152 // We fed all input memory into the LOM. Now lets proceed until the lom is drained. |
|
153 draining = true; |
|
154 } |
|
155 } else if (!draining && surprise < 1) { |
|
156 deallocate_top(); |
|
157 _num_deallocs ++; |
|
158 } else { |
|
159 if (allocate()) { |
|
160 _num_allocs ++; |
|
161 } else { |
|
162 if (draining) { |
|
163 stop = _lom.total_word_size() < 512; |
|
164 } else { |
|
165 forcefeed = true; |
|
166 } |
|
167 } |
|
168 } |
|
169 if ((iter % 1000) == 0) { |
|
170 DEBUG_ONLY(_lom.verify();) |
|
171 test_all_marked_ranges(); |
|
172 LOG("a %d (" SIZE_FORMAT "), d %d, f %d", _num_allocs, _allocated_words.get(), _num_deallocs, _num_feeds); |
|
173 #ifdef LOG_PLEASE |
|
174 _lom.print(tty, true); |
|
175 tty->cr(); |
|
176 #endif |
|
177 } |
|
178 } |
|
179 |
|
180 // Drain |
|
181 |
|
182 |
|
183 } |
|
184 |
|
185 |
|
186 |
|
187 public: |
|
188 |
|
189 LeftOverBinsTest(size_t avg_alloc_size) : |
|
190 _fb(), _lom(), |
|
191 _rgen_feeding(128, 4096), |
|
192 _rgen_allocations(avg_alloc_size / 4, avg_alloc_size * 2, 0.01f, avg_alloc_size / 3, avg_alloc_size * 30), |
|
193 _allocations(NULL), |
|
194 _num_allocs(0), _num_deallocs(0), _num_feeds(0) |
|
195 { |
|
196 // some initial feeding |
|
197 _lom.add_block(_fb.get(1024), 1024); |
|
198 } |
|
199 |
|
200 |
|
201 static void test_small_allocations() { |
|
202 LeftOverBinsTest test(10); |
|
203 test.test_loop(); |
|
204 } |
|
205 |
|
206 static void test_medium_allocations() { |
|
207 LeftOverBinsTest test(30); |
|
208 test.test_loop(); |
|
209 } |
|
210 |
|
211 static void test_large_allocations() { |
|
212 LeftOverBinsTest test(150); |
|
213 test.test_loop(); |
|
214 } |
|
215 |
|
216 |
|
217 }; |
|
218 |
|
219 TEST_VM(metaspace, leftoverbins_mask_basic) { |
|
220 // Basic tests |
|
221 metaspace::BinMap map; |
|
222 EXPECT_TRUE(map.all_zero()); |
|
223 for (int i = 0; i < map.size(); i ++) { |
|
224 map.set_bit(i); |
|
225 EXPECT_TRUE(map.get_bit(i)); |
|
226 map.clr_bit(i); |
|
227 EXPECT_FALSE(map.get_bit(i)); |
|
228 EXPECT_TRUE(map.all_zero()); |
|
229 } |
|
230 } |
|
231 |
|
232 TEST_VM(metaspace, leftoverbins_mask_find_next_set_bit) { |
|
233 metaspace::BinMap map; |
|
234 EXPECT_TRUE(map.all_zero()); |
|
235 for (int i = 0; i < map.size(); i ++) { |
|
236 map.set_bit(i); |
|
237 for (int j = 0; j < i; j ++) { |
|
238 int n = map.find_next_set_bit(j); |
|
239 if (j <= i) { |
|
240 EXPECT_EQ(n, i); |
|
241 } else { |
|
242 EXPECT_EQ(n, -1); |
|
243 } |
|
244 } |
|
245 map.clr_bit(i); |
|
246 } |
|
247 } |
|
248 |
|
249 TEST_VM(metaspace, leftoverbins_basics) { |
|
250 |
|
251 LeftOverManager lom; |
|
252 MetaWord tmp[1024]; |
|
253 metaspace::block_stats_t stats; |
|
254 |
|
255 lom.add_block(tmp, 1024); |
|
256 DEBUG_ONLY(lom.verify();) |
|
257 |
|
258 lom.statistics(&stats); |
|
259 EXPECT_EQ(stats.num_blocks, 1); |
|
260 EXPECT_EQ(stats.word_size, (size_t)1024); |
|
261 |
|
262 MetaWord* p = lom.get_block(1024); |
|
263 EXPECT_EQ(p, tmp); |
|
264 DEBUG_ONLY(lom.verify();) |
|
265 |
|
266 lom.statistics(&stats); |
|
267 EXPECT_EQ(stats.num_blocks, 0); |
|
268 EXPECT_EQ(stats.word_size, (size_t)0); |
|
269 } |
|
270 |
|
271 TEST_VM(metaspace, leftoverbins_small) { |
|
272 LeftOverBinsTest::test_small_allocations(); |
|
273 } |
|
274 |
|
275 TEST_VM(metaspace, leftoverbins_medium) { |
|
276 LeftOverBinsTest::test_medium_allocations(); |
|
277 } |
|
278 |
|
279 TEST_VM(metaspace, leftoverbins_large) { |
|
280 LeftOverBinsTest::test_large_allocations(); |
|
281 } |
|
282 |