blob: 08ae894e5829ec9f1460ff2ae261e7c16d754303 [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
Ian Rogers1d54e732013-05-02 21:10:01 -070017#include "dlmalloc_space.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070018
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070019#include "common_test.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070020#include "globals.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070021#include "UniquePtr.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070022
Ian Rogers3bb17a62012-01-27 23:56:44 -080023#include <stdint.h>
24
Carl Shapiro69759ea2011-07-21 18:13:35 -070025namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070026namespace gc {
27namespace space {
Carl Shapiro69759ea2011-07-21 18:13:35 -070028
Ian Rogers3bb17a62012-01-27 23:56:44 -080029class SpaceTest : public CommonTest {
30 public:
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070031 void SizeFootPrintGrowthLimitAndTrimBody(DlMallocSpace* space, intptr_t object_size,
Ian Rogers3bb17a62012-01-27 23:56:44 -080032 int round, size_t growth_limit);
33 void SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size);
Ian Rogers1d54e732013-05-02 21:10:01 -070034
35 void AddContinuousSpace(ContinuousSpace* space) {
36 Runtime::Current()->GetHeap()->AddContinuousSpace(space);
37 }
Ian Rogers3bb17a62012-01-27 23:56:44 -080038};
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070039
40TEST_F(SpaceTest, Init) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070041 {
jeffhaoc1160702011-10-27 15:48:45 -070042 // Init < max == growth
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070043 UniquePtr<Space> space(DlMallocSpace::Create("test", 16 * MB, 32 * MB, 32 * 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 Chartier1c23e1e2012-10-12 14:14:11 -070048 UniquePtr<Space> space(DlMallocSpace::Create("test", 16 * MB, 16 * MB, 16 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070049 EXPECT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070050 }
51 {
jeffhaoc1160702011-10-27 15:48:45 -070052 // Init > max == growth
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070053 UniquePtr<Space> space(DlMallocSpace::Create("test", 32 * MB, 16 * MB, 16 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070054 EXPECT_TRUE(space.get() == NULL);
55 }
56 {
57 // Growth == init < max
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070058 UniquePtr<Space> space(DlMallocSpace::Create("test", 16 * MB, 16 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070059 EXPECT_TRUE(space.get() != NULL);
60 }
61 {
62 // Growth < init < max
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070063 UniquePtr<Space> space(DlMallocSpace::Create("test", 16 * MB, 8 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070064 EXPECT_TRUE(space.get() == NULL);
65 }
66 {
67 // Init < growth < max
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070068 UniquePtr<Space> space(DlMallocSpace::Create("test", 8 * MB, 16 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070069 EXPECT_TRUE(space.get() != NULL);
70 }
71 {
72 // Init < max < growth
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070073 UniquePtr<Space> space(DlMallocSpace::Create("test", 8 * MB, 32 * MB, 16 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070074 EXPECT_TRUE(space.get() == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070075 }
76}
77
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070078// TODO: This test is not very good, we should improve it.
79// The test should do more allocations before the creation of the ZygoteSpace, and then do
80// allocations after the ZygoteSpace is created. The test should also do some GCs to ensure that
81// the GC works with the ZygoteSpace.
Mathieu Chartiercc236d72012-07-20 10:29:05 -070082TEST_F(SpaceTest, ZygoteSpace) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070083 DlMallocSpace* space(DlMallocSpace::Create("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Mathieu Chartiercc236d72012-07-20 10:29:05 -070084 ASSERT_TRUE(space != NULL);
85
Mathieu Chartier357e9be2012-08-01 11:00:14 -070086 // Make space findable to the heap, will also delete space when runtime is cleaned up
Ian Rogers1d54e732013-05-02 21:10:01 -070087 AddContinuousSpace(space);
Ian Rogers50b35e22012-10-04 10:09:15 -070088 Thread* self = Thread::Current();
Mathieu Chartiercc236d72012-07-20 10:29:05 -070089
90 // Succeeds, fits without adjusting the footprint limit.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 mirror::Object* ptr1 = space->Alloc(self, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070092 EXPECT_TRUE(ptr1 != NULL);
93
94 // Fails, requires a higher footprint limit.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095 mirror::Object* ptr2 = space->Alloc(self, 8 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070096 EXPECT_TRUE(ptr2 == NULL);
97
98 // Succeeds, adjusts the footprint.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099 mirror::Object* ptr3 = space->AllocWithGrowth(self, 8 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700100 EXPECT_TRUE(ptr3 != NULL);
101
102 // Fails, requires a higher footprint limit.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103 mirror::Object* ptr4 = space->Alloc(self, 8 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700104 EXPECT_TRUE(ptr4 == NULL);
105
106 // Also fails, requires a higher allowed footprint.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700108 EXPECT_TRUE(ptr5 == NULL);
109
110 // Release some memory.
111 size_t free3 = space->AllocationSize(ptr3);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700112 EXPECT_EQ(free3, space->Free(self, ptr3));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700113 EXPECT_LE(8U * MB, free3);
114
115 // Succeeds, now that memory has been freed.
Ian Rogers50b35e22012-10-04 10:09:15 -0700116 void* ptr6 = space->AllocWithGrowth(self, 9 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700117 EXPECT_TRUE(ptr6 != NULL);
118
119 // Final clean up.
120 size_t free1 = space->AllocationSize(ptr1);
Ian Rogers50b35e22012-10-04 10:09:15 -0700121 space->Free(self, ptr1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700122 EXPECT_LE(1U * MB, free1);
123
124 // Make sure that the zygote space isn't directly at the start of the space.
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700125 space->Alloc(self, 1U * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700126 space = space->CreateZygoteSpace();
127
128 // Make space findable to the heap, will also delete space when runtime is cleaned up
Ian Rogers1d54e732013-05-02 21:10:01 -0700129 AddContinuousSpace(space);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700130
131 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700132 ptr1 = space->Alloc(self, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700133 EXPECT_TRUE(ptr1 != NULL);
134
135 // Fails, requires a higher footprint limit.
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700136 ptr2 = space->Alloc(self, 8 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700137 EXPECT_TRUE(ptr2 == NULL);
138
139 // Succeeds, adjusts the footprint.
Ian Rogers50b35e22012-10-04 10:09:15 -0700140 ptr3 = space->AllocWithGrowth(self, 2 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700141 EXPECT_TRUE(ptr3 != NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -0700142 space->Free(self, ptr3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700143
144 // Final clean up.
145 free1 = space->AllocationSize(ptr1);
Ian Rogers50b35e22012-10-04 10:09:15 -0700146 space->Free(self, ptr1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700147 EXPECT_LE(1U * MB, free1);
148}
149
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -0700150TEST_F(SpaceTest, AllocAndFree) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700151 DlMallocSpace* space(DlMallocSpace::Create("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Ian Rogers30fab402012-01-23 15:43:46 -0800152 ASSERT_TRUE(space != NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -0700153 Thread* self = Thread::Current();
Ian Rogers30fab402012-01-23 15:43:46 -0800154
Ian Rogers3bb17a62012-01-27 23:56:44 -0800155 // Make space findable to the heap, will also delete space when runtime is cleaned up
Ian Rogers1d54e732013-05-02 21:10:01 -0700156 AddContinuousSpace(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700157
Ian Rogers3bb17a62012-01-27 23:56:44 -0800158 // Succeeds, fits without adjusting the footprint limit.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159 mirror::Object* ptr1 = space->Alloc(self, 1 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700160 EXPECT_TRUE(ptr1 != NULL);
161
Ian Rogers3bb17a62012-01-27 23:56:44 -0800162 // Fails, requires a higher footprint limit.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800163 mirror::Object* ptr2 = space->Alloc(self, 8 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700164 EXPECT_TRUE(ptr2 == NULL);
165
166 // Succeeds, adjusts the footprint.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800167 mirror::Object* ptr3 = space->AllocWithGrowth(self, 8 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700168 EXPECT_TRUE(ptr3 != NULL);
169
Ian Rogers3bb17a62012-01-27 23:56:44 -0800170 // Fails, requires a higher footprint limit.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171 mirror::Object* ptr4 = space->Alloc(self, 8 * MB);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800172 EXPECT_TRUE(ptr4 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700173
174 // Also fails, requires a higher allowed footprint.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800175 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800176 EXPECT_TRUE(ptr5 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700177
178 // Release some memory.
Ian Rogers30fab402012-01-23 15:43:46 -0800179 size_t free3 = space->AllocationSize(ptr3);
Ian Rogers50b35e22012-10-04 10:09:15 -0700180 space->Free(self, ptr3);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700181 EXPECT_LE(8U * MB, free3);
182
183 // Succeeds, now that memory has been freed.
Ian Rogers50b35e22012-10-04 10:09:15 -0700184 void* ptr6 = space->AllocWithGrowth(self, 9 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700185 EXPECT_TRUE(ptr6 != NULL);
186
187 // Final clean up.
Ian Rogers30fab402012-01-23 15:43:46 -0800188 size_t free1 = space->AllocationSize(ptr1);
Ian Rogers50b35e22012-10-04 10:09:15 -0700189 space->Free(self, ptr1);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700190 EXPECT_LE(1U * MB, free1);
191}
192
Ian Rogers3bb17a62012-01-27 23:56:44 -0800193TEST_F(SpaceTest, AllocAndFreeList) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700194 DlMallocSpace* space(DlMallocSpace::Create("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800195 ASSERT_TRUE(space != NULL);
196
197 // Make space findable to the heap, will also delete space when runtime is cleaned up
Ian Rogers1d54e732013-05-02 21:10:01 -0700198 AddContinuousSpace(space);
Ian Rogers50b35e22012-10-04 10:09:15 -0700199 Thread* self = Thread::Current();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800200
201 // Succeeds, fits without adjusting the max allowed footprint.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800202 mirror::Object* lots_of_objects[1024];
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700203 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700204 lots_of_objects[i] = space->Alloc(self, 16);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800205 EXPECT_TRUE(lots_of_objects[i] != NULL);
206 }
207
208 // Release memory and check pointers are NULL
Ian Rogers50b35e22012-10-04 10:09:15 -0700209 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700210 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800211 EXPECT_TRUE(lots_of_objects[i] == NULL);
212 }
213
214 // Succeeds, fits by adjusting the max allowed footprint.
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700215 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700216 lots_of_objects[i] = space->AllocWithGrowth(self, 1024);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800217 EXPECT_TRUE(lots_of_objects[i] != NULL);
218 }
219
220 // Release memory and check pointers are NULL
Ian Rogers50b35e22012-10-04 10:09:15 -0700221 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700222 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800223 EXPECT_TRUE(lots_of_objects[i] == NULL);
224 }
225}
226
227static size_t test_rand() {
228 // TODO: replace this with something random yet deterministic
229 return rand();
230}
231
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700232void SpaceTest::SizeFootPrintGrowthLimitAndTrimBody(DlMallocSpace* space, intptr_t object_size,
Ian Rogers3bb17a62012-01-27 23:56:44 -0800233 int round, size_t growth_limit) {
234 if (((object_size > 0 && object_size >= static_cast<intptr_t>(growth_limit))) ||
235 ((object_size < 0 && -object_size >= static_cast<intptr_t>(growth_limit)))) {
236 // No allocation can succeed
237 return;
238 }
239 // Mspace for raw dlmalloc operations
240 void* mspace = space->GetMspace();
241
242 // mspace's footprint equals amount of resources requested from system
243 size_t footprint = mspace_footprint(mspace);
244
245 // mspace must at least have its book keeping allocated
246 EXPECT_GT(footprint, 0u);
247
248 // mspace but it shouldn't exceed the initial size
249 EXPECT_LE(footprint, growth_limit);
250
251 // space's size shouldn't exceed the initial size
252 EXPECT_LE(space->Size(), growth_limit);
253
254 // this invariant should always hold or else the mspace has grown to be larger than what the
255 // space believes its size is (which will break invariants)
256 EXPECT_GE(space->Size(), footprint);
257
258 // Fill the space with lots of small objects up to the growth limit
259 size_t max_objects = (growth_limit / (object_size > 0 ? object_size : 8)) + 1;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800260 UniquePtr<mirror::Object*[]> lots_of_objects(new mirror::Object*[max_objects]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800261 size_t last_object = 0; // last object for which allocation succeeded
262 size_t amount_allocated = 0; // amount of space allocated
Ian Rogers50b35e22012-10-04 10:09:15 -0700263 Thread* self = Thread::Current();
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700264 for (size_t i = 0; i < max_objects; i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800265 size_t alloc_fails = 0; // number of failed allocations
266 size_t max_fails = 30; // number of times we fail allocation before giving up
267 for (; alloc_fails < max_fails; alloc_fails++) {
268 size_t alloc_size;
269 if (object_size > 0) {
270 alloc_size = object_size;
271 } else {
272 alloc_size = test_rand() % static_cast<size_t>(-object_size);
273 if (alloc_size < 8) {
274 alloc_size = 8;
275 }
276 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800277 mirror::Object* object;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800278 if (round <= 1) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700279 object = space->Alloc(self, alloc_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800280 } else {
Ian Rogers50b35e22012-10-04 10:09:15 -0700281 object = space->AllocWithGrowth(self, alloc_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800282 }
283 footprint = mspace_footprint(mspace);
284 EXPECT_GE(space->Size(), footprint); // invariant
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700285 if (object != NULL) { // allocation succeeded
Ian Rogers3bb17a62012-01-27 23:56:44 -0800286 lots_of_objects.get()[i] = object;
287 size_t allocation_size = space->AllocationSize(object);
288 if (object_size > 0) {
289 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
290 } else {
291 EXPECT_GE(allocation_size, 8u);
292 }
293 amount_allocated += allocation_size;
294 break;
295 }
296 }
297 if (alloc_fails == max_fails) {
298 last_object = i;
299 break;
300 }
301 }
302 CHECK_NE(last_object, 0u); // we should have filled the space
303 EXPECT_GT(amount_allocated, 0u);
304
305 // We shouldn't have gone past the growth_limit
306 EXPECT_LE(amount_allocated, growth_limit);
307 EXPECT_LE(footprint, growth_limit);
308 EXPECT_LE(space->Size(), growth_limit);
309
310 // footprint and size should agree with amount allocated
311 EXPECT_GE(footprint, amount_allocated);
312 EXPECT_GE(space->Size(), amount_allocated);
313
314 // Release storage in a semi-adhoc manner
315 size_t free_increment = 96;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700316 while (true) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800317 // Give the space a haircut
318 space->Trim();
319
320 // Bounds sanity
321 footprint = mspace_footprint(mspace);
322 EXPECT_LE(amount_allocated, growth_limit);
323 EXPECT_GE(footprint, amount_allocated);
324 EXPECT_LE(footprint, growth_limit);
325 EXPECT_GE(space->Size(), amount_allocated);
326 EXPECT_LE(space->Size(), growth_limit);
327
328 if (free_increment == 0) {
329 break;
330 }
331
332 // Free some objects
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700333 for (size_t i = 0; i < last_object; i += free_increment) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800334 mirror::Object* object = lots_of_objects.get()[i];
Ian Rogers3bb17a62012-01-27 23:56:44 -0800335 if (object == NULL) {
336 continue;
337 }
338 size_t allocation_size = space->AllocationSize(object);
339 if (object_size > 0) {
340 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
341 } else {
342 EXPECT_GE(allocation_size, 8u);
343 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700344 space->Free(self, object);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800345 lots_of_objects.get()[i] = NULL;
346 amount_allocated -= allocation_size;
347 footprint = mspace_footprint(mspace);
348 EXPECT_GE(space->Size(), footprint); // invariant
349 }
350
351 free_increment >>= 1;
352 }
353
354 // All memory was released, try a large allocation to check freed memory is being coalesced
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800355 mirror::Object* large_object;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800356 size_t three_quarters_space = (growth_limit / 2) + (growth_limit / 4);
357 if (round <= 1) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700358 large_object = space->Alloc(self, three_quarters_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800359 } else {
Ian Rogers50b35e22012-10-04 10:09:15 -0700360 large_object = space->AllocWithGrowth(self, three_quarters_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800361 }
362 EXPECT_TRUE(large_object != NULL);
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 // Clean up
Ian Rogers50b35e22012-10-04 10:09:15 -0700371 space->Free(self, large_object);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800372
373 // Sanity check footprint
374 footprint = mspace_footprint(mspace);
375 EXPECT_LE(footprint, growth_limit);
376 EXPECT_GE(space->Size(), footprint);
377 EXPECT_LE(space->Size(), growth_limit);
378}
379
380void SpaceTest::SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size) {
381 size_t initial_size = 4 * MB;
382 size_t growth_limit = 8 * MB;
383 size_t capacity = 16 * MB;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700384 DlMallocSpace* space(DlMallocSpace::Create("test", initial_size, growth_limit, capacity, NULL));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800385 ASSERT_TRUE(space != NULL);
386
387 // Basic sanity
388 EXPECT_EQ(space->Capacity(), growth_limit);
389 EXPECT_EQ(space->NonGrowthLimitCapacity(), capacity);
390
391 // Make space findable to the heap, will also delete space when runtime is cleaned up
Ian Rogers1d54e732013-05-02 21:10:01 -0700392 AddContinuousSpace(space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800393
394 // In this round we don't allocate with growth and therefore can't grow past the initial size.
395 // This effectively makes the growth_limit the initial_size, so assert this.
396 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 1, initial_size);
397 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 2, growth_limit);
398 // Remove growth limit
399 space->ClearGrowthLimit();
400 EXPECT_EQ(space->Capacity(), capacity);
401 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 3, capacity);
402}
403
404#define TEST_SizeFootPrintGrowthLimitAndTrim(name, size) \
405 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name) { \
406 SizeFootPrintGrowthLimitAndTrimDriver(size); \
407 } \
408 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name) { \
409 SizeFootPrintGrowthLimitAndTrimDriver(-size); \
410 }
411
412// Each size test is its own test so that we get a fresh heap each time
413TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_8B) {
414 SizeFootPrintGrowthLimitAndTrimDriver(8);
415}
416TEST_SizeFootPrintGrowthLimitAndTrim(16B, 16)
417TEST_SizeFootPrintGrowthLimitAndTrim(24B, 24)
418TEST_SizeFootPrintGrowthLimitAndTrim(32B, 32)
419TEST_SizeFootPrintGrowthLimitAndTrim(64B, 64)
420TEST_SizeFootPrintGrowthLimitAndTrim(128B, 128)
421TEST_SizeFootPrintGrowthLimitAndTrim(1KB, 1 * KB)
422TEST_SizeFootPrintGrowthLimitAndTrim(4KB, 4 * KB)
423TEST_SizeFootPrintGrowthLimitAndTrim(1MB, 1 * MB)
424TEST_SizeFootPrintGrowthLimitAndTrim(4MB, 4 * MB)
425TEST_SizeFootPrintGrowthLimitAndTrim(8MB, 8 * MB)
426
Ian Rogers1d54e732013-05-02 21:10:01 -0700427} // namespace space
428} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700429} // namespace art