blob: c1c1dca8951b4ef58c6af6f1f14ece3a097c2646 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro69759ea2011-07-21 18:13:35 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "space.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070018
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070019#include "common_test.h"
Ian Rogers3bb17a62012-01-27 23:56:44 -080020#include "dlmalloc.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070021#include "globals.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070022#include "UniquePtr.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070023
Ian Rogers3bb17a62012-01-27 23:56:44 -080024#include <stdint.h>
25
Carl Shapiro69759ea2011-07-21 18:13:35 -070026namespace art {
27
Ian Rogers3bb17a62012-01-27 23:56:44 -080028class SpaceTest : public CommonTest {
29 public:
30 void SizeFootPrintGrowthLimitAndTrimBody(AllocSpace* space, intptr_t object_size,
31 int round, size_t growth_limit);
32 void SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size);
33};
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070034
35TEST_F(SpaceTest, Init) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070036 {
jeffhaoc1160702011-10-27 15:48:45 -070037 // Init < max == growth
Ian Rogers30fab402012-01-23 15:43:46 -080038 UniquePtr<Space> space(Space::CreateAllocSpace("test", 16 * MB, 32 * MB, 32 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070039 EXPECT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070040 }
41 {
jeffhaoc1160702011-10-27 15:48:45 -070042 // Init == max == growth
Ian Rogers30fab402012-01-23 15:43:46 -080043 UniquePtr<Space> space(Space::CreateAllocSpace("test", 16 * MB, 16 * MB, 16 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070044 EXPECT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070045 }
46 {
jeffhaoc1160702011-10-27 15:48:45 -070047 // Init > max == growth
Ian Rogers30fab402012-01-23 15:43:46 -080048 UniquePtr<Space> space(Space::CreateAllocSpace("test", 32 * MB, 16 * MB, 16 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070049 EXPECT_TRUE(space.get() == NULL);
50 }
51 {
52 // Growth == init < max
Ian Rogers30fab402012-01-23 15:43:46 -080053 UniquePtr<Space> space(Space::CreateAllocSpace("test", 16 * MB, 16 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070054 EXPECT_TRUE(space.get() != NULL);
55 }
56 {
57 // Growth < init < max
Ian Rogers30fab402012-01-23 15:43:46 -080058 UniquePtr<Space> space(Space::CreateAllocSpace("test", 16 * MB, 8 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070059 EXPECT_TRUE(space.get() == NULL);
60 }
61 {
62 // Init < growth < max
Ian Rogers30fab402012-01-23 15:43:46 -080063 UniquePtr<Space> space(Space::CreateAllocSpace("test", 8 * MB, 16 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070064 EXPECT_TRUE(space.get() != NULL);
65 }
66 {
67 // Init < max < growth
Ian Rogers30fab402012-01-23 15:43:46 -080068 UniquePtr<Space> space(Space::CreateAllocSpace("test", 8 * MB, 32 * MB, 16 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070069 EXPECT_TRUE(space.get() == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070070 }
71}
72
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070073// TODO: This test is not very good, we should improve it.
74// The test should do more allocations before the creation of the ZygoteSpace, and then do
75// allocations after the ZygoteSpace is created. The test should also do some GCs to ensure that
76// the GC works with the ZygoteSpace.
Mathieu Chartiercc236d72012-07-20 10:29:05 -070077TEST_F(SpaceTest, ZygoteSpace) {
78 AllocSpace* space(Space::CreateAllocSpace("test", 4 * MB, 16 * MB, 16 * MB, NULL));
79 ASSERT_TRUE(space != NULL);
80
81 // Make space findable to the heap, will also delete space when runtime is cleaned up
82 Runtime::Current()->GetHeap()->AddSpace(space);
83
84 // Succeeds, fits without adjusting the footprint limit.
85 Object* ptr1 = space->AllocWithoutGrowth(1 * MB);
86 EXPECT_TRUE(ptr1 != NULL);
87
88 // Fails, requires a higher footprint limit.
89 Object* ptr2 = space->AllocWithoutGrowth(8 * MB);
90 EXPECT_TRUE(ptr2 == NULL);
91
92 // Succeeds, adjusts the footprint.
93 Object* ptr3 = space->AllocWithGrowth(8 * MB);
94 EXPECT_TRUE(ptr3 != NULL);
95
96 // Fails, requires a higher footprint limit.
97 Object* ptr4 = space->AllocWithoutGrowth(8 * MB);
98 EXPECT_TRUE(ptr4 == NULL);
99
100 // Also fails, requires a higher allowed footprint.
101 Object* ptr5 = space->AllocWithGrowth(8 * MB);
102 EXPECT_TRUE(ptr5 == NULL);
103
104 // Release some memory.
105 size_t free3 = space->AllocationSize(ptr3);
106 space->Free(ptr3);
107 EXPECT_LE(8U * MB, free3);
108
109 // Succeeds, now that memory has been freed.
110 void* ptr6 = space->AllocWithGrowth(9 * MB);
111 EXPECT_TRUE(ptr6 != NULL);
112
113 // Final clean up.
114 size_t free1 = space->AllocationSize(ptr1);
115 space->Free(ptr1);
116 EXPECT_LE(1U * MB, free1);
117
118 // Make sure that the zygote space isn't directly at the start of the space.
119 space->AllocWithoutGrowth(1U * MB);
120 space = space->CreateZygoteSpace();
121
122 // Make space findable to the heap, will also delete space when runtime is cleaned up
123 Runtime::Current()->GetHeap()->AddSpace(space);
124
125 // Succeeds, fits without adjusting the footprint limit.
126 ptr1 = space->AllocWithoutGrowth(1 * MB);
127 EXPECT_TRUE(ptr1 != NULL);
128
129 // Fails, requires a higher footprint limit.
130 ptr2 = space->AllocWithoutGrowth(8 * MB);
131 EXPECT_TRUE(ptr2 == NULL);
132
133 // Succeeds, adjusts the footprint.
134 ptr3 = space->AllocWithGrowth(2 * MB);
135 EXPECT_TRUE(ptr3 != NULL);
136 space->Free(ptr3);
137
138 // Final clean up.
139 free1 = space->AllocationSize(ptr1);
140 space->Free(ptr1);
141 EXPECT_LE(1U * MB, free1);
142}
143
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -0700144TEST_F(SpaceTest, AllocAndFree) {
Ian Rogers30fab402012-01-23 15:43:46 -0800145 AllocSpace* space(Space::CreateAllocSpace("test", 4 * MB, 16 * MB, 16 * MB, NULL));
146 ASSERT_TRUE(space != NULL);
147
Ian Rogers3bb17a62012-01-27 23:56:44 -0800148 // Make space findable to the heap, will also delete space when runtime is cleaned up
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800149 Runtime::Current()->GetHeap()->AddSpace(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700150
Ian Rogers3bb17a62012-01-27 23:56:44 -0800151 // Succeeds, fits without adjusting the footprint limit.
Ian Rogers30fab402012-01-23 15:43:46 -0800152 Object* ptr1 = space->AllocWithoutGrowth(1 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700153 EXPECT_TRUE(ptr1 != NULL);
154
Ian Rogers3bb17a62012-01-27 23:56:44 -0800155 // Fails, requires a higher footprint limit.
Ian Rogers30fab402012-01-23 15:43:46 -0800156 Object* ptr2 = space->AllocWithoutGrowth(8 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700157 EXPECT_TRUE(ptr2 == NULL);
158
159 // Succeeds, adjusts the footprint.
Ian Rogers30fab402012-01-23 15:43:46 -0800160 Object* ptr3 = space->AllocWithGrowth(8 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700161 EXPECT_TRUE(ptr3 != NULL);
162
Ian Rogers3bb17a62012-01-27 23:56:44 -0800163 // Fails, requires a higher footprint limit.
Ian Rogers30fab402012-01-23 15:43:46 -0800164 Object* ptr4 = space->AllocWithoutGrowth(8 * MB);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800165 EXPECT_TRUE(ptr4 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700166
167 // Also fails, requires a higher allowed footprint.
Ian Rogers30fab402012-01-23 15:43:46 -0800168 Object* ptr5 = space->AllocWithGrowth(8 * MB);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800169 EXPECT_TRUE(ptr5 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700170
171 // Release some memory.
Ian Rogers30fab402012-01-23 15:43:46 -0800172 size_t free3 = space->AllocationSize(ptr3);
173 space->Free(ptr3);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700174 EXPECT_LE(8U * MB, free3);
175
176 // Succeeds, now that memory has been freed.
177 void* ptr6 = space->AllocWithGrowth(9 * MB);
178 EXPECT_TRUE(ptr6 != NULL);
179
180 // Final clean up.
Ian Rogers30fab402012-01-23 15:43:46 -0800181 size_t free1 = space->AllocationSize(ptr1);
182 space->Free(ptr1);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700183 EXPECT_LE(1U * MB, free1);
184}
185
Ian Rogers3bb17a62012-01-27 23:56:44 -0800186TEST_F(SpaceTest, AllocAndFreeList) {
187 AllocSpace* space(Space::CreateAllocSpace("test", 4 * MB, 16 * MB, 16 * MB, NULL));
188 ASSERT_TRUE(space != NULL);
189
190 // Make space findable to the heap, will also delete space when runtime is cleaned up
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800191 Runtime::Current()->GetHeap()->AddSpace(space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800192
193 // Succeeds, fits without adjusting the max allowed footprint.
194 Object* lots_of_objects[1024];
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700195 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800196 lots_of_objects[i] = space->AllocWithoutGrowth(16);
197 EXPECT_TRUE(lots_of_objects[i] != NULL);
198 }
199
200 // Release memory and check pointers are NULL
201 space->FreeList(arraysize(lots_of_objects), lots_of_objects);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700202 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800203 EXPECT_TRUE(lots_of_objects[i] == NULL);
204 }
205
206 // Succeeds, fits by adjusting the max allowed footprint.
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700207 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800208 lots_of_objects[i] = space->AllocWithGrowth(1024);
209 EXPECT_TRUE(lots_of_objects[i] != NULL);
210 }
211
212 // Release memory and check pointers are NULL
213 space->FreeList(arraysize(lots_of_objects), lots_of_objects);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700214 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800215 EXPECT_TRUE(lots_of_objects[i] == NULL);
216 }
217}
218
219static size_t test_rand() {
220 // TODO: replace this with something random yet deterministic
221 return rand();
222}
223
224void SpaceTest::SizeFootPrintGrowthLimitAndTrimBody(AllocSpace* space, intptr_t object_size,
225 int round, size_t growth_limit) {
226 if (((object_size > 0 && object_size >= static_cast<intptr_t>(growth_limit))) ||
227 ((object_size < 0 && -object_size >= static_cast<intptr_t>(growth_limit)))) {
228 // No allocation can succeed
229 return;
230 }
231 // Mspace for raw dlmalloc operations
232 void* mspace = space->GetMspace();
233
234 // mspace's footprint equals amount of resources requested from system
235 size_t footprint = mspace_footprint(mspace);
236
237 // mspace must at least have its book keeping allocated
238 EXPECT_GT(footprint, 0u);
239
240 // mspace but it shouldn't exceed the initial size
241 EXPECT_LE(footprint, growth_limit);
242
243 // space's size shouldn't exceed the initial size
244 EXPECT_LE(space->Size(), growth_limit);
245
246 // this invariant should always hold or else the mspace has grown to be larger than what the
247 // space believes its size is (which will break invariants)
248 EXPECT_GE(space->Size(), footprint);
249
250 // Fill the space with lots of small objects up to the growth limit
251 size_t max_objects = (growth_limit / (object_size > 0 ? object_size : 8)) + 1;
Elliott Hughesf34f1742012-03-16 18:56:00 -0700252 UniquePtr<Object*[]> lots_of_objects(new Object*[max_objects]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800253 size_t last_object = 0; // last object for which allocation succeeded
254 size_t amount_allocated = 0; // amount of space allocated
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700255 for (size_t i = 0; i < max_objects; i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800256 size_t alloc_fails = 0; // number of failed allocations
257 size_t max_fails = 30; // number of times we fail allocation before giving up
258 for (; alloc_fails < max_fails; alloc_fails++) {
259 size_t alloc_size;
260 if (object_size > 0) {
261 alloc_size = object_size;
262 } else {
263 alloc_size = test_rand() % static_cast<size_t>(-object_size);
264 if (alloc_size < 8) {
265 alloc_size = 8;
266 }
267 }
268 Object* object;
269 if (round <= 1) {
270 object = space->AllocWithoutGrowth(alloc_size);
271 } else {
272 object = space->AllocWithGrowth(alloc_size);
273 }
274 footprint = mspace_footprint(mspace);
275 EXPECT_GE(space->Size(), footprint); // invariant
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700276 if (object != NULL) { // allocation succeeded
Ian Rogers3bb17a62012-01-27 23:56:44 -0800277 lots_of_objects.get()[i] = object;
278 size_t allocation_size = space->AllocationSize(object);
279 if (object_size > 0) {
280 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
281 } else {
282 EXPECT_GE(allocation_size, 8u);
283 }
284 amount_allocated += allocation_size;
285 break;
286 }
287 }
288 if (alloc_fails == max_fails) {
289 last_object = i;
290 break;
291 }
292 }
293 CHECK_NE(last_object, 0u); // we should have filled the space
294 EXPECT_GT(amount_allocated, 0u);
295
296 // We shouldn't have gone past the growth_limit
297 EXPECT_LE(amount_allocated, growth_limit);
298 EXPECT_LE(footprint, growth_limit);
299 EXPECT_LE(space->Size(), growth_limit);
300
301 // footprint and size should agree with amount allocated
302 EXPECT_GE(footprint, amount_allocated);
303 EXPECT_GE(space->Size(), amount_allocated);
304
305 // Release storage in a semi-adhoc manner
306 size_t free_increment = 96;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700307 while (true) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800308 // Give the space a haircut
309 space->Trim();
310
311 // Bounds sanity
312 footprint = mspace_footprint(mspace);
313 EXPECT_LE(amount_allocated, growth_limit);
314 EXPECT_GE(footprint, amount_allocated);
315 EXPECT_LE(footprint, growth_limit);
316 EXPECT_GE(space->Size(), amount_allocated);
317 EXPECT_LE(space->Size(), growth_limit);
318
319 if (free_increment == 0) {
320 break;
321 }
322
323 // Free some objects
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700324 for (size_t i = 0; i < last_object; i += free_increment) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800325 Object* object = lots_of_objects.get()[i];
326 if (object == NULL) {
327 continue;
328 }
329 size_t allocation_size = space->AllocationSize(object);
330 if (object_size > 0) {
331 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
332 } else {
333 EXPECT_GE(allocation_size, 8u);
334 }
335 space->Free(object);
336 lots_of_objects.get()[i] = NULL;
337 amount_allocated -= allocation_size;
338 footprint = mspace_footprint(mspace);
339 EXPECT_GE(space->Size(), footprint); // invariant
340 }
341
342 free_increment >>= 1;
343 }
344
345 // All memory was released, try a large allocation to check freed memory is being coalesced
346 Object* large_object;
347 size_t three_quarters_space = (growth_limit / 2) + (growth_limit / 4);
348 if (round <= 1) {
349 large_object = space->AllocWithoutGrowth(three_quarters_space);
350 } else {
351 large_object = space->AllocWithGrowth(three_quarters_space);
352 }
353 EXPECT_TRUE(large_object != NULL);
354
355 // Sanity check footprint
356 footprint = mspace_footprint(mspace);
357 EXPECT_LE(footprint, growth_limit);
358 EXPECT_GE(space->Size(), footprint);
359 EXPECT_LE(space->Size(), growth_limit);
360
361 // Clean up
362 space->Free(large_object);
363
364 // Sanity check footprint
365 footprint = mspace_footprint(mspace);
366 EXPECT_LE(footprint, growth_limit);
367 EXPECT_GE(space->Size(), footprint);
368 EXPECT_LE(space->Size(), growth_limit);
369}
370
371void SpaceTest::SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size) {
372 size_t initial_size = 4 * MB;
373 size_t growth_limit = 8 * MB;
374 size_t capacity = 16 * MB;
375 AllocSpace* space(Space::CreateAllocSpace("test", initial_size, growth_limit, capacity, NULL));
376 ASSERT_TRUE(space != NULL);
377
378 // Basic sanity
379 EXPECT_EQ(space->Capacity(), growth_limit);
380 EXPECT_EQ(space->NonGrowthLimitCapacity(), capacity);
381
382 // Make space findable to the heap, will also delete space when runtime is cleaned up
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800383 Runtime::Current()->GetHeap()->AddSpace(space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800384
385 // In this round we don't allocate with growth and therefore can't grow past the initial size.
386 // This effectively makes the growth_limit the initial_size, so assert this.
387 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 1, initial_size);
388 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 2, growth_limit);
389 // Remove growth limit
390 space->ClearGrowthLimit();
391 EXPECT_EQ(space->Capacity(), capacity);
392 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 3, capacity);
393}
394
395#define TEST_SizeFootPrintGrowthLimitAndTrim(name, size) \
396 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name) { \
397 SizeFootPrintGrowthLimitAndTrimDriver(size); \
398 } \
399 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name) { \
400 SizeFootPrintGrowthLimitAndTrimDriver(-size); \
401 }
402
403// Each size test is its own test so that we get a fresh heap each time
404TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_8B) {
405 SizeFootPrintGrowthLimitAndTrimDriver(8);
406}
407TEST_SizeFootPrintGrowthLimitAndTrim(16B, 16)
408TEST_SizeFootPrintGrowthLimitAndTrim(24B, 24)
409TEST_SizeFootPrintGrowthLimitAndTrim(32B, 32)
410TEST_SizeFootPrintGrowthLimitAndTrim(64B, 64)
411TEST_SizeFootPrintGrowthLimitAndTrim(128B, 128)
412TEST_SizeFootPrintGrowthLimitAndTrim(1KB, 1 * KB)
413TEST_SizeFootPrintGrowthLimitAndTrim(4KB, 4 * KB)
414TEST_SizeFootPrintGrowthLimitAndTrim(1MB, 1 * MB)
415TEST_SizeFootPrintGrowthLimitAndTrim(4MB, 4 * MB)
416TEST_SizeFootPrintGrowthLimitAndTrim(8MB, 8 * MB)
417
Carl Shapiro69759ea2011-07-21 18:13:35 -0700418} // namespace art