blob: 03191471c4d1301c6c1fbaab004e1ac236b4f360 [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
Mathieu Chartier2fde5332012-09-14 14:51:54 -070038 UniquePtr<Space> space(AllocSpace::Create("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
Mathieu Chartier2fde5332012-09-14 14:51:54 -070043 UniquePtr<Space> space(AllocSpace::Create("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
Mathieu Chartier2fde5332012-09-14 14:51:54 -070048 UniquePtr<Space> space(AllocSpace::Create("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
Mathieu Chartier2fde5332012-09-14 14:51:54 -070053 UniquePtr<Space> space(AllocSpace::Create("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
Mathieu Chartier2fde5332012-09-14 14:51:54 -070058 UniquePtr<Space> space(AllocSpace::Create("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
Mathieu Chartier2fde5332012-09-14 14:51:54 -070063 UniquePtr<Space> space(AllocSpace::Create("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
Mathieu Chartier2fde5332012-09-14 14:51:54 -070068 UniquePtr<Space> space(AllocSpace::Create("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) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -070078 AllocSpace* space(AllocSpace::Create("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Mathieu Chartiercc236d72012-07-20 10:29:05 -070079 ASSERT_TRUE(space != NULL);
80
Mathieu Chartier357e9be2012-08-01 11:00:14 -070081 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartiercc236d72012-07-20 10:29:05 -070082 Runtime::Current()->GetHeap()->AddSpace(space);
Ian Rogers50b35e22012-10-04 10:09:15 -070083 Thread* self = Thread::Current();
Mathieu Chartiercc236d72012-07-20 10:29:05 -070084
85 // Succeeds, fits without adjusting the footprint limit.
Ian Rogers50b35e22012-10-04 10:09:15 -070086 Object* ptr1 = space->AllocWithoutGrowth(self, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070087 EXPECT_TRUE(ptr1 != NULL);
88
89 // Fails, requires a higher footprint limit.
Ian Rogers50b35e22012-10-04 10:09:15 -070090 Object* ptr2 = space->AllocWithoutGrowth(self, 8 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070091 EXPECT_TRUE(ptr2 == NULL);
92
93 // Succeeds, adjusts the footprint.
Ian Rogers50b35e22012-10-04 10:09:15 -070094 Object* ptr3 = space->AllocWithGrowth(self, 8 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070095 EXPECT_TRUE(ptr3 != NULL);
96
97 // Fails, requires a higher footprint limit.
Ian Rogers50b35e22012-10-04 10:09:15 -070098 Object* ptr4 = space->AllocWithoutGrowth(self, 8 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070099 EXPECT_TRUE(ptr4 == NULL);
100
101 // Also fails, requires a higher allowed footprint.
Ian Rogers50b35e22012-10-04 10:09:15 -0700102 Object* ptr5 = space->AllocWithGrowth(self, 8 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700103 EXPECT_TRUE(ptr5 == NULL);
104
105 // Release some memory.
106 size_t free3 = space->AllocationSize(ptr3);
Ian Rogers50b35e22012-10-04 10:09:15 -0700107 space->Free(self, ptr3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700108 EXPECT_LE(8U * MB, free3);
109
110 // Succeeds, now that memory has been freed.
Ian Rogers50b35e22012-10-04 10:09:15 -0700111 void* ptr6 = space->AllocWithGrowth(self, 9 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700112 EXPECT_TRUE(ptr6 != NULL);
113
114 // Final clean up.
115 size_t free1 = space->AllocationSize(ptr1);
Ian Rogers50b35e22012-10-04 10:09:15 -0700116 space->Free(self, ptr1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700117 EXPECT_LE(1U * MB, free1);
118
119 // Make sure that the zygote space isn't directly at the start of the space.
Ian Rogers50b35e22012-10-04 10:09:15 -0700120 space->AllocWithoutGrowth(self, 1U * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700121 space = space->CreateZygoteSpace();
122
123 // Make space findable to the heap, will also delete space when runtime is cleaned up
124 Runtime::Current()->GetHeap()->AddSpace(space);
125
126 // Succeeds, fits without adjusting the footprint limit.
Ian Rogers50b35e22012-10-04 10:09:15 -0700127 ptr1 = space->AllocWithoutGrowth(self, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700128 EXPECT_TRUE(ptr1 != NULL);
129
130 // Fails, requires a higher footprint limit.
Ian Rogers50b35e22012-10-04 10:09:15 -0700131 ptr2 = space->AllocWithoutGrowth(self, 8 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700132 EXPECT_TRUE(ptr2 == NULL);
133
134 // Succeeds, adjusts the footprint.
Ian Rogers50b35e22012-10-04 10:09:15 -0700135 ptr3 = space->AllocWithGrowth(self, 2 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700136 EXPECT_TRUE(ptr3 != NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -0700137 space->Free(self, ptr3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700138
139 // Final clean up.
140 free1 = space->AllocationSize(ptr1);
Ian Rogers50b35e22012-10-04 10:09:15 -0700141 space->Free(self, ptr1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700142 EXPECT_LE(1U * MB, free1);
143}
144
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -0700145TEST_F(SpaceTest, AllocAndFree) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700146 AllocSpace* space(AllocSpace::Create("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Ian Rogers30fab402012-01-23 15:43:46 -0800147 ASSERT_TRUE(space != NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -0700148 Thread* self = Thread::Current();
Ian Rogers30fab402012-01-23 15:43:46 -0800149
Ian Rogers3bb17a62012-01-27 23:56:44 -0800150 // Make space findable to the heap, will also delete space when runtime is cleaned up
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800151 Runtime::Current()->GetHeap()->AddSpace(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700152
Ian Rogers3bb17a62012-01-27 23:56:44 -0800153 // Succeeds, fits without adjusting the footprint limit.
Ian Rogers50b35e22012-10-04 10:09:15 -0700154 Object* ptr1 = space->AllocWithoutGrowth(self, 1 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700155 EXPECT_TRUE(ptr1 != NULL);
156
Ian Rogers3bb17a62012-01-27 23:56:44 -0800157 // Fails, requires a higher footprint limit.
Ian Rogers50b35e22012-10-04 10:09:15 -0700158 Object* ptr2 = space->AllocWithoutGrowth(self, 8 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700159 EXPECT_TRUE(ptr2 == NULL);
160
161 // Succeeds, adjusts the footprint.
Ian Rogers50b35e22012-10-04 10:09:15 -0700162 Object* ptr3 = space->AllocWithGrowth(self, 8 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700163 EXPECT_TRUE(ptr3 != NULL);
164
Ian Rogers3bb17a62012-01-27 23:56:44 -0800165 // Fails, requires a higher footprint limit.
Ian Rogers50b35e22012-10-04 10:09:15 -0700166 Object* ptr4 = space->AllocWithoutGrowth(self, 8 * MB);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800167 EXPECT_TRUE(ptr4 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700168
169 // Also fails, requires a higher allowed footprint.
Ian Rogers50b35e22012-10-04 10:09:15 -0700170 Object* ptr5 = space->AllocWithGrowth(self, 8 * MB);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800171 EXPECT_TRUE(ptr5 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700172
173 // Release some memory.
Ian Rogers30fab402012-01-23 15:43:46 -0800174 size_t free3 = space->AllocationSize(ptr3);
Ian Rogers50b35e22012-10-04 10:09:15 -0700175 space->Free(self, ptr3);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700176 EXPECT_LE(8U * MB, free3);
177
178 // Succeeds, now that memory has been freed.
Ian Rogers50b35e22012-10-04 10:09:15 -0700179 void* ptr6 = space->AllocWithGrowth(self, 9 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700180 EXPECT_TRUE(ptr6 != NULL);
181
182 // Final clean up.
Ian Rogers30fab402012-01-23 15:43:46 -0800183 size_t free1 = space->AllocationSize(ptr1);
Ian Rogers50b35e22012-10-04 10:09:15 -0700184 space->Free(self, ptr1);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700185 EXPECT_LE(1U * MB, free1);
186}
187
Ian Rogers3bb17a62012-01-27 23:56:44 -0800188TEST_F(SpaceTest, AllocAndFreeList) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700189 AllocSpace* space(AllocSpace::Create("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800190 ASSERT_TRUE(space != NULL);
191
192 // Make space findable to the heap, will also delete space when runtime is cleaned up
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800193 Runtime::Current()->GetHeap()->AddSpace(space);
Ian Rogers50b35e22012-10-04 10:09:15 -0700194 Thread* self = Thread::Current();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800195
196 // Succeeds, fits without adjusting the max allowed footprint.
197 Object* lots_of_objects[1024];
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700198 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700199 lots_of_objects[i] = space->AllocWithoutGrowth(self, 16);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800200 EXPECT_TRUE(lots_of_objects[i] != NULL);
201 }
202
203 // Release memory and check pointers are NULL
Ian Rogers50b35e22012-10-04 10:09:15 -0700204 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700205 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800206 EXPECT_TRUE(lots_of_objects[i] == NULL);
207 }
208
209 // Succeeds, fits by adjusting the max allowed footprint.
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700210 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700211 lots_of_objects[i] = space->AllocWithGrowth(self, 1024);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800212 EXPECT_TRUE(lots_of_objects[i] != NULL);
213 }
214
215 // Release memory and check pointers are NULL
Ian Rogers50b35e22012-10-04 10:09:15 -0700216 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700217 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800218 EXPECT_TRUE(lots_of_objects[i] == NULL);
219 }
220}
221
222static size_t test_rand() {
223 // TODO: replace this with something random yet deterministic
224 return rand();
225}
226
227void SpaceTest::SizeFootPrintGrowthLimitAndTrimBody(AllocSpace* space, intptr_t object_size,
228 int round, size_t growth_limit) {
229 if (((object_size > 0 && object_size >= static_cast<intptr_t>(growth_limit))) ||
230 ((object_size < 0 && -object_size >= static_cast<intptr_t>(growth_limit)))) {
231 // No allocation can succeed
232 return;
233 }
234 // Mspace for raw dlmalloc operations
235 void* mspace = space->GetMspace();
236
237 // mspace's footprint equals amount of resources requested from system
238 size_t footprint = mspace_footprint(mspace);
239
240 // mspace must at least have its book keeping allocated
241 EXPECT_GT(footprint, 0u);
242
243 // mspace but it shouldn't exceed the initial size
244 EXPECT_LE(footprint, growth_limit);
245
246 // space's size shouldn't exceed the initial size
247 EXPECT_LE(space->Size(), growth_limit);
248
249 // this invariant should always hold or else the mspace has grown to be larger than what the
250 // space believes its size is (which will break invariants)
251 EXPECT_GE(space->Size(), footprint);
252
253 // Fill the space with lots of small objects up to the growth limit
254 size_t max_objects = (growth_limit / (object_size > 0 ? object_size : 8)) + 1;
Elliott Hughesf34f1742012-03-16 18:56:00 -0700255 UniquePtr<Object*[]> lots_of_objects(new Object*[max_objects]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800256 size_t last_object = 0; // last object for which allocation succeeded
257 size_t amount_allocated = 0; // amount of space allocated
Ian Rogers50b35e22012-10-04 10:09:15 -0700258 Thread* self = Thread::Current();
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700259 for (size_t i = 0; i < max_objects; i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800260 size_t alloc_fails = 0; // number of failed allocations
261 size_t max_fails = 30; // number of times we fail allocation before giving up
262 for (; alloc_fails < max_fails; alloc_fails++) {
263 size_t alloc_size;
264 if (object_size > 0) {
265 alloc_size = object_size;
266 } else {
267 alloc_size = test_rand() % static_cast<size_t>(-object_size);
268 if (alloc_size < 8) {
269 alloc_size = 8;
270 }
271 }
272 Object* object;
273 if (round <= 1) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700274 object = space->AllocWithoutGrowth(self, alloc_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800275 } else {
Ian Rogers50b35e22012-10-04 10:09:15 -0700276 object = space->AllocWithGrowth(self, alloc_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800277 }
278 footprint = mspace_footprint(mspace);
279 EXPECT_GE(space->Size(), footprint); // invariant
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700280 if (object != NULL) { // allocation succeeded
Ian Rogers3bb17a62012-01-27 23:56:44 -0800281 lots_of_objects.get()[i] = object;
282 size_t allocation_size = space->AllocationSize(object);
283 if (object_size > 0) {
284 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
285 } else {
286 EXPECT_GE(allocation_size, 8u);
287 }
288 amount_allocated += allocation_size;
289 break;
290 }
291 }
292 if (alloc_fails == max_fails) {
293 last_object = i;
294 break;
295 }
296 }
297 CHECK_NE(last_object, 0u); // we should have filled the space
298 EXPECT_GT(amount_allocated, 0u);
299
300 // We shouldn't have gone past the growth_limit
301 EXPECT_LE(amount_allocated, growth_limit);
302 EXPECT_LE(footprint, growth_limit);
303 EXPECT_LE(space->Size(), growth_limit);
304
305 // footprint and size should agree with amount allocated
306 EXPECT_GE(footprint, amount_allocated);
307 EXPECT_GE(space->Size(), amount_allocated);
308
309 // Release storage in a semi-adhoc manner
310 size_t free_increment = 96;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700311 while (true) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800312 // Give the space a haircut
313 space->Trim();
314
315 // Bounds sanity
316 footprint = mspace_footprint(mspace);
317 EXPECT_LE(amount_allocated, growth_limit);
318 EXPECT_GE(footprint, amount_allocated);
319 EXPECT_LE(footprint, growth_limit);
320 EXPECT_GE(space->Size(), amount_allocated);
321 EXPECT_LE(space->Size(), growth_limit);
322
323 if (free_increment == 0) {
324 break;
325 }
326
327 // Free some objects
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700328 for (size_t i = 0; i < last_object; i += free_increment) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800329 Object* object = lots_of_objects.get()[i];
330 if (object == NULL) {
331 continue;
332 }
333 size_t allocation_size = space->AllocationSize(object);
334 if (object_size > 0) {
335 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
336 } else {
337 EXPECT_GE(allocation_size, 8u);
338 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700339 space->Free(self, object);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800340 lots_of_objects.get()[i] = NULL;
341 amount_allocated -= allocation_size;
342 footprint = mspace_footprint(mspace);
343 EXPECT_GE(space->Size(), footprint); // invariant
344 }
345
346 free_increment >>= 1;
347 }
348
349 // All memory was released, try a large allocation to check freed memory is being coalesced
350 Object* large_object;
351 size_t three_quarters_space = (growth_limit / 2) + (growth_limit / 4);
352 if (round <= 1) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700353 large_object = space->AllocWithoutGrowth(self, three_quarters_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800354 } else {
Ian Rogers50b35e22012-10-04 10:09:15 -0700355 large_object = space->AllocWithGrowth(self, three_quarters_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800356 }
357 EXPECT_TRUE(large_object != NULL);
358
359 // Sanity check footprint
360 footprint = mspace_footprint(mspace);
361 EXPECT_LE(footprint, growth_limit);
362 EXPECT_GE(space->Size(), footprint);
363 EXPECT_LE(space->Size(), growth_limit);
364
365 // Clean up
Ian Rogers50b35e22012-10-04 10:09:15 -0700366 space->Free(self, large_object);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800367
368 // Sanity check footprint
369 footprint = mspace_footprint(mspace);
370 EXPECT_LE(footprint, growth_limit);
371 EXPECT_GE(space->Size(), footprint);
372 EXPECT_LE(space->Size(), growth_limit);
373}
374
375void SpaceTest::SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size) {
376 size_t initial_size = 4 * MB;
377 size_t growth_limit = 8 * MB;
378 size_t capacity = 16 * MB;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700379 AllocSpace* space(AllocSpace::Create("test", initial_size, growth_limit, capacity, NULL));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800380 ASSERT_TRUE(space != NULL);
381
382 // Basic sanity
383 EXPECT_EQ(space->Capacity(), growth_limit);
384 EXPECT_EQ(space->NonGrowthLimitCapacity(), capacity);
385
386 // Make space findable to the heap, will also delete space when runtime is cleaned up
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800387 Runtime::Current()->GetHeap()->AddSpace(space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800388
389 // In this round we don't allocate with growth and therefore can't grow past the initial size.
390 // This effectively makes the growth_limit the initial_size, so assert this.
391 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 1, initial_size);
392 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 2, growth_limit);
393 // Remove growth limit
394 space->ClearGrowthLimit();
395 EXPECT_EQ(space->Capacity(), capacity);
396 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 3, capacity);
397}
398
399#define TEST_SizeFootPrintGrowthLimitAndTrim(name, size) \
400 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name) { \
401 SizeFootPrintGrowthLimitAndTrimDriver(size); \
402 } \
403 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name) { \
404 SizeFootPrintGrowthLimitAndTrimDriver(-size); \
405 }
406
407// Each size test is its own test so that we get a fresh heap each time
408TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_8B) {
409 SizeFootPrintGrowthLimitAndTrimDriver(8);
410}
411TEST_SizeFootPrintGrowthLimitAndTrim(16B, 16)
412TEST_SizeFootPrintGrowthLimitAndTrim(24B, 24)
413TEST_SizeFootPrintGrowthLimitAndTrim(32B, 32)
414TEST_SizeFootPrintGrowthLimitAndTrim(64B, 64)
415TEST_SizeFootPrintGrowthLimitAndTrim(128B, 128)
416TEST_SizeFootPrintGrowthLimitAndTrim(1KB, 1 * KB)
417TEST_SizeFootPrintGrowthLimitAndTrim(4KB, 4 * KB)
418TEST_SizeFootPrintGrowthLimitAndTrim(1MB, 1 * MB)
419TEST_SizeFootPrintGrowthLimitAndTrim(4MB, 4 * MB)
420TEST_SizeFootPrintGrowthLimitAndTrim(8MB, 8 * MB)
421
Carl Shapiro69759ea2011-07-21 18:13:35 -0700422} // namespace art