blob: 66b8c11b59542c103dcb9ba8e22941c681ddd4dc [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.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070091 mirror::Object* ptr1 = space->Alloc(self, 1 * MB, NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070092 EXPECT_TRUE(ptr1 != NULL);
93
94 // Fails, requires a higher footprint limit.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070095 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070096 EXPECT_TRUE(ptr2 == NULL);
97
98 // Succeeds, adjusts the footprint.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070099 size_t ptr3_bytes_allocated;
100 mirror::Object* ptr3 = space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700101 EXPECT_TRUE(ptr3 != NULL);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700102 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700103
104 // Fails, requires a higher footprint limit.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700105 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700106 EXPECT_TRUE(ptr4 == NULL);
107
108 // Also fails, requires a higher allowed footprint.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700109 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700110 EXPECT_TRUE(ptr5 == NULL);
111
112 // Release some memory.
113 size_t free3 = space->AllocationSize(ptr3);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700114 EXPECT_EQ(free3, ptr3_bytes_allocated);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700115 EXPECT_EQ(free3, space->Free(self, ptr3));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700116 EXPECT_LE(8U * MB, free3);
117
118 // Succeeds, now that memory has been freed.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700119 void* ptr6 = space->AllocWithGrowth(self, 9 * MB, NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700120 EXPECT_TRUE(ptr6 != NULL);
121
122 // Final clean up.
123 size_t free1 = space->AllocationSize(ptr1);
Ian Rogers50b35e22012-10-04 10:09:15 -0700124 space->Free(self, ptr1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700125 EXPECT_LE(1U * MB, free1);
126
127 // Make sure that the zygote space isn't directly at the start of the space.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700128 space->Alloc(self, 1U * MB, NULL);
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700129 space = space->CreateZygoteSpace("alloc space");
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700130
131 // Make space findable to the heap, will also delete space when runtime is cleaned up
Ian Rogers1d54e732013-05-02 21:10:01 -0700132 AddContinuousSpace(space);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700133
134 // Succeeds, fits without adjusting the footprint limit.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700135 ptr1 = space->Alloc(self, 1 * MB, NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700136 EXPECT_TRUE(ptr1 != NULL);
137
138 // Fails, requires a higher footprint limit.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700139 ptr2 = space->Alloc(self, 8 * MB, NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700140 EXPECT_TRUE(ptr2 == NULL);
141
142 // Succeeds, adjusts the footprint.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700143 ptr3 = space->AllocWithGrowth(self, 2 * MB, NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700144 EXPECT_TRUE(ptr3 != NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -0700145 space->Free(self, ptr3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700146
147 // Final clean up.
148 free1 = space->AllocationSize(ptr1);
Ian Rogers50b35e22012-10-04 10:09:15 -0700149 space->Free(self, ptr1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700150 EXPECT_LE(1U * MB, free1);
151}
152
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -0700153TEST_F(SpaceTest, AllocAndFree) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700154 DlMallocSpace* space(DlMallocSpace::Create("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Ian Rogers30fab402012-01-23 15:43:46 -0800155 ASSERT_TRUE(space != NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -0700156 Thread* self = Thread::Current();
Ian Rogers30fab402012-01-23 15:43:46 -0800157
Ian Rogers3bb17a62012-01-27 23:56:44 -0800158 // Make space findable to the heap, will also delete space when runtime is cleaned up
Ian Rogers1d54e732013-05-02 21:10:01 -0700159 AddContinuousSpace(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700160
Ian Rogers3bb17a62012-01-27 23:56:44 -0800161 // Succeeds, fits without adjusting the footprint limit.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700162 mirror::Object* ptr1 = space->Alloc(self, 1 * MB, NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700163 EXPECT_TRUE(ptr1 != NULL);
164
Ian Rogers3bb17a62012-01-27 23:56:44 -0800165 // Fails, requires a higher footprint limit.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700166 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700167 EXPECT_TRUE(ptr2 == NULL);
168
169 // Succeeds, adjusts the footprint.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700170 size_t ptr3_bytes_allocated;
171 mirror::Object* ptr3 = space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700172 EXPECT_TRUE(ptr3 != NULL);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700173 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700174
Ian Rogers3bb17a62012-01-27 23:56:44 -0800175 // Fails, requires a higher footprint limit.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700176 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, NULL);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800177 EXPECT_TRUE(ptr4 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700178
179 // Also fails, requires a higher allowed footprint.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700180 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, NULL);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800181 EXPECT_TRUE(ptr5 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700182
183 // Release some memory.
Ian Rogers30fab402012-01-23 15:43:46 -0800184 size_t free3 = space->AllocationSize(ptr3);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700185 EXPECT_EQ(free3, ptr3_bytes_allocated);
Ian Rogers50b35e22012-10-04 10:09:15 -0700186 space->Free(self, ptr3);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700187 EXPECT_LE(8U * MB, free3);
188
189 // Succeeds, now that memory has been freed.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700190 void* ptr6 = space->AllocWithGrowth(self, 9 * MB, NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700191 EXPECT_TRUE(ptr6 != NULL);
192
193 // Final clean up.
Ian Rogers30fab402012-01-23 15:43:46 -0800194 size_t free1 = space->AllocationSize(ptr1);
Ian Rogers50b35e22012-10-04 10:09:15 -0700195 space->Free(self, ptr1);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700196 EXPECT_LE(1U * MB, free1);
197}
198
Ian Rogers3bb17a62012-01-27 23:56:44 -0800199TEST_F(SpaceTest, AllocAndFreeList) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700200 DlMallocSpace* space(DlMallocSpace::Create("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800201 ASSERT_TRUE(space != NULL);
202
203 // Make space findable to the heap, will also delete space when runtime is cleaned up
Ian Rogers1d54e732013-05-02 21:10:01 -0700204 AddContinuousSpace(space);
Ian Rogers50b35e22012-10-04 10:09:15 -0700205 Thread* self = Thread::Current();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800206
207 // Succeeds, fits without adjusting the max allowed footprint.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800208 mirror::Object* lots_of_objects[1024];
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700209 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700210 lots_of_objects[i] = space->Alloc(self, 16, NULL);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800211 EXPECT_TRUE(lots_of_objects[i] != NULL);
212 }
213
214 // Release memory and check pointers are NULL
Ian Rogers50b35e22012-10-04 10:09:15 -0700215 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700216 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800217 EXPECT_TRUE(lots_of_objects[i] == NULL);
218 }
219
220 // Succeeds, fits by adjusting the max allowed footprint.
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700221 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700222 lots_of_objects[i] = space->AllocWithGrowth(self, 1024, NULL);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800223 EXPECT_TRUE(lots_of_objects[i] != NULL);
224 }
225
226 // Release memory and check pointers are NULL
Ian Rogers50b35e22012-10-04 10:09:15 -0700227 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700228 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800229 EXPECT_TRUE(lots_of_objects[i] == NULL);
230 }
231}
232
233static size_t test_rand() {
234 // TODO: replace this with something random yet deterministic
235 return rand();
236}
237
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700238void SpaceTest::SizeFootPrintGrowthLimitAndTrimBody(DlMallocSpace* space, intptr_t object_size,
Ian Rogers3bb17a62012-01-27 23:56:44 -0800239 int round, size_t growth_limit) {
240 if (((object_size > 0 && object_size >= static_cast<intptr_t>(growth_limit))) ||
241 ((object_size < 0 && -object_size >= static_cast<intptr_t>(growth_limit)))) {
242 // No allocation can succeed
243 return;
244 }
245 // Mspace for raw dlmalloc operations
246 void* mspace = space->GetMspace();
247
248 // mspace's footprint equals amount of resources requested from system
249 size_t footprint = mspace_footprint(mspace);
250
251 // mspace must at least have its book keeping allocated
252 EXPECT_GT(footprint, 0u);
253
254 // mspace but it shouldn't exceed the initial size
255 EXPECT_LE(footprint, growth_limit);
256
257 // space's size shouldn't exceed the initial size
258 EXPECT_LE(space->Size(), growth_limit);
259
260 // this invariant should always hold or else the mspace has grown to be larger than what the
261 // space believes its size is (which will break invariants)
262 EXPECT_GE(space->Size(), footprint);
263
264 // Fill the space with lots of small objects up to the growth limit
265 size_t max_objects = (growth_limit / (object_size > 0 ? object_size : 8)) + 1;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266 UniquePtr<mirror::Object*[]> lots_of_objects(new mirror::Object*[max_objects]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800267 size_t last_object = 0; // last object for which allocation succeeded
268 size_t amount_allocated = 0; // amount of space allocated
Ian Rogers50b35e22012-10-04 10:09:15 -0700269 Thread* self = Thread::Current();
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700270 for (size_t i = 0; i < max_objects; i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800271 size_t alloc_fails = 0; // number of failed allocations
272 size_t max_fails = 30; // number of times we fail allocation before giving up
273 for (; alloc_fails < max_fails; alloc_fails++) {
274 size_t alloc_size;
275 if (object_size > 0) {
276 alloc_size = object_size;
277 } else {
278 alloc_size = test_rand() % static_cast<size_t>(-object_size);
279 if (alloc_size < 8) {
280 alloc_size = 8;
281 }
282 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800283 mirror::Object* object;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800284 if (round <= 1) {
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700285 object = space->Alloc(self, alloc_size, NULL);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800286 } else {
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700287 object = space->AllocWithGrowth(self, alloc_size, NULL);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800288 }
289 footprint = mspace_footprint(mspace);
290 EXPECT_GE(space->Size(), footprint); // invariant
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700291 if (object != NULL) { // allocation succeeded
Ian Rogers3bb17a62012-01-27 23:56:44 -0800292 lots_of_objects.get()[i] = object;
293 size_t allocation_size = space->AllocationSize(object);
294 if (object_size > 0) {
295 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
296 } else {
297 EXPECT_GE(allocation_size, 8u);
298 }
299 amount_allocated += allocation_size;
300 break;
301 }
302 }
303 if (alloc_fails == max_fails) {
304 last_object = i;
305 break;
306 }
307 }
308 CHECK_NE(last_object, 0u); // we should have filled the space
309 EXPECT_GT(amount_allocated, 0u);
310
311 // We shouldn't have gone past the growth_limit
312 EXPECT_LE(amount_allocated, growth_limit);
313 EXPECT_LE(footprint, growth_limit);
314 EXPECT_LE(space->Size(), growth_limit);
315
316 // footprint and size should agree with amount allocated
317 EXPECT_GE(footprint, amount_allocated);
318 EXPECT_GE(space->Size(), amount_allocated);
319
320 // Release storage in a semi-adhoc manner
321 size_t free_increment = 96;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700322 while (true) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800323 // Give the space a haircut
324 space->Trim();
325
326 // Bounds sanity
327 footprint = mspace_footprint(mspace);
328 EXPECT_LE(amount_allocated, growth_limit);
329 EXPECT_GE(footprint, amount_allocated);
330 EXPECT_LE(footprint, growth_limit);
331 EXPECT_GE(space->Size(), amount_allocated);
332 EXPECT_LE(space->Size(), growth_limit);
333
334 if (free_increment == 0) {
335 break;
336 }
337
338 // Free some objects
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700339 for (size_t i = 0; i < last_object; i += free_increment) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800340 mirror::Object* object = lots_of_objects.get()[i];
Ian Rogers3bb17a62012-01-27 23:56:44 -0800341 if (object == NULL) {
342 continue;
343 }
344 size_t allocation_size = space->AllocationSize(object);
345 if (object_size > 0) {
346 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
347 } else {
348 EXPECT_GE(allocation_size, 8u);
349 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700350 space->Free(self, object);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800351 lots_of_objects.get()[i] = NULL;
352 amount_allocated -= allocation_size;
353 footprint = mspace_footprint(mspace);
354 EXPECT_GE(space->Size(), footprint); // invariant
355 }
356
357 free_increment >>= 1;
358 }
359
360 // All memory was released, try a large allocation to check freed memory is being coalesced
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800361 mirror::Object* large_object;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800362 size_t three_quarters_space = (growth_limit / 2) + (growth_limit / 4);
363 if (round <= 1) {
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700364 large_object = space->Alloc(self, three_quarters_space, NULL);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800365 } else {
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700366 large_object = space->AllocWithGrowth(self, three_quarters_space, NULL);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800367 }
368 EXPECT_TRUE(large_object != NULL);
369
370 // Sanity check footprint
371 footprint = mspace_footprint(mspace);
372 EXPECT_LE(footprint, growth_limit);
373 EXPECT_GE(space->Size(), footprint);
374 EXPECT_LE(space->Size(), growth_limit);
375
376 // Clean up
Ian Rogers50b35e22012-10-04 10:09:15 -0700377 space->Free(self, large_object);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800378
379 // Sanity check footprint
380 footprint = mspace_footprint(mspace);
381 EXPECT_LE(footprint, growth_limit);
382 EXPECT_GE(space->Size(), footprint);
383 EXPECT_LE(space->Size(), growth_limit);
384}
385
386void SpaceTest::SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size) {
387 size_t initial_size = 4 * MB;
388 size_t growth_limit = 8 * MB;
389 size_t capacity = 16 * MB;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700390 DlMallocSpace* space(DlMallocSpace::Create("test", initial_size, growth_limit, capacity, NULL));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800391 ASSERT_TRUE(space != NULL);
392
393 // Basic sanity
394 EXPECT_EQ(space->Capacity(), growth_limit);
395 EXPECT_EQ(space->NonGrowthLimitCapacity(), capacity);
396
397 // Make space findable to the heap, will also delete space when runtime is cleaned up
Ian Rogers1d54e732013-05-02 21:10:01 -0700398 AddContinuousSpace(space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800399
400 // In this round we don't allocate with growth and therefore can't grow past the initial size.
401 // This effectively makes the growth_limit the initial_size, so assert this.
402 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 1, initial_size);
403 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 2, growth_limit);
404 // Remove growth limit
405 space->ClearGrowthLimit();
406 EXPECT_EQ(space->Capacity(), capacity);
407 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 3, capacity);
408}
409
410#define TEST_SizeFootPrintGrowthLimitAndTrim(name, size) \
411 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name) { \
412 SizeFootPrintGrowthLimitAndTrimDriver(size); \
413 } \
414 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name) { \
415 SizeFootPrintGrowthLimitAndTrimDriver(-size); \
416 }
417
418// Each size test is its own test so that we get a fresh heap each time
419TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_8B) {
420 SizeFootPrintGrowthLimitAndTrimDriver(8);
421}
422TEST_SizeFootPrintGrowthLimitAndTrim(16B, 16)
423TEST_SizeFootPrintGrowthLimitAndTrim(24B, 24)
424TEST_SizeFootPrintGrowthLimitAndTrim(32B, 32)
425TEST_SizeFootPrintGrowthLimitAndTrim(64B, 64)
426TEST_SizeFootPrintGrowthLimitAndTrim(128B, 128)
427TEST_SizeFootPrintGrowthLimitAndTrim(1KB, 1 * KB)
428TEST_SizeFootPrintGrowthLimitAndTrim(4KB, 4 * KB)
429TEST_SizeFootPrintGrowthLimitAndTrim(1MB, 1 * MB)
430TEST_SizeFootPrintGrowthLimitAndTrim(4MB, 4 * MB)
431TEST_SizeFootPrintGrowthLimitAndTrim(8MB, 8 * MB)
432
Ian Rogers1d54e732013-05-02 21:10:01 -0700433} // namespace space
434} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700435} // namespace art