blob: 455168c90f8978d78af8bf277934ff2783c0e810 [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"
Mathieu Chartiereb5710e2013-07-25 15:19:42 -070018#include "large_object_space.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070019
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070020#include "common_test.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 {
Ian Rogers1d54e732013-05-02 21:10:01 -070027namespace gc {
28namespace space {
Carl Shapiro69759ea2011-07-21 18:13:35 -070029
Ian Rogers3bb17a62012-01-27 23:56:44 -080030class SpaceTest : public CommonTest {
31 public:
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070032 void SizeFootPrintGrowthLimitAndTrimBody(DlMallocSpace* space, intptr_t object_size,
Ian Rogers3bb17a62012-01-27 23:56:44 -080033 int round, size_t growth_limit);
34 void SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size);
Ian Rogers1d54e732013-05-02 21:10:01 -070035
36 void AddContinuousSpace(ContinuousSpace* space) {
37 Runtime::Current()->GetHeap()->AddContinuousSpace(space);
38 }
Ian Rogers3bb17a62012-01-27 23:56:44 -080039};
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070040
Mathieu Chartiereb5710e2013-07-25 15:19:42 -070041static size_t test_rand(size_t* seed) {
42 *seed = *seed * 1103515245 + 12345;
43 return *seed;
44}
45
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070046TEST_F(SpaceTest, Init) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070047 {
jeffhaoc1160702011-10-27 15:48:45 -070048 // Init < max == growth
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070049 UniquePtr<Space> space(DlMallocSpace::Create("test", 16 * MB, 32 * MB, 32 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070050 EXPECT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070051 }
52 {
jeffhaoc1160702011-10-27 15:48:45 -070053 // Init == max == growth
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070054 UniquePtr<Space> space(DlMallocSpace::Create("test", 16 * MB, 16 * MB, 16 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070055 EXPECT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070056 }
57 {
jeffhaoc1160702011-10-27 15:48:45 -070058 // Init > max == growth
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070059 UniquePtr<Space> space(DlMallocSpace::Create("test", 32 * MB, 16 * MB, 16 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070060 EXPECT_TRUE(space.get() == NULL);
61 }
62 {
63 // Growth == init < max
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070064 UniquePtr<Space> space(DlMallocSpace::Create("test", 16 * MB, 16 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070065 EXPECT_TRUE(space.get() != NULL);
66 }
67 {
68 // Growth < init < max
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070069 UniquePtr<Space> space(DlMallocSpace::Create("test", 16 * MB, 8 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070070 EXPECT_TRUE(space.get() == NULL);
71 }
72 {
73 // Init < growth < max
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070074 UniquePtr<Space> space(DlMallocSpace::Create("test", 8 * MB, 16 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070075 EXPECT_TRUE(space.get() != NULL);
76 }
77 {
78 // Init < max < growth
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070079 UniquePtr<Space> space(DlMallocSpace::Create("test", 8 * MB, 32 * MB, 16 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070080 EXPECT_TRUE(space.get() == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070081 }
82}
83
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070084// TODO: This test is not very good, we should improve it.
85// The test should do more allocations before the creation of the ZygoteSpace, and then do
86// allocations after the ZygoteSpace is created. The test should also do some GCs to ensure that
87// the GC works with the ZygoteSpace.
Mathieu Chartiercc236d72012-07-20 10:29:05 -070088TEST_F(SpaceTest, ZygoteSpace) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -070089 size_t dummy = 0;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070090 DlMallocSpace* space(DlMallocSpace::Create("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Mathieu Chartiercc236d72012-07-20 10:29:05 -070091 ASSERT_TRUE(space != NULL);
92
Mathieu Chartier357e9be2012-08-01 11:00:14 -070093 // Make space findable to the heap, will also delete space when runtime is cleaned up
Ian Rogers1d54e732013-05-02 21:10:01 -070094 AddContinuousSpace(space);
Ian Rogers50b35e22012-10-04 10:09:15 -070095 Thread* self = Thread::Current();
Mathieu Chartiercc236d72012-07-20 10:29:05 -070096
97 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -070098 mirror::Object* ptr1 = space->Alloc(self, 1 * MB, &dummy);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070099 EXPECT_TRUE(ptr1 != NULL);
100
101 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700102 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700103 EXPECT_TRUE(ptr2 == NULL);
104
105 // Succeeds, adjusts the footprint.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700106 size_t ptr3_bytes_allocated;
107 mirror::Object* ptr3 = space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700108 EXPECT_TRUE(ptr3 != NULL);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700109 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700110
111 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700112 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700113 EXPECT_TRUE(ptr4 == NULL);
114
115 // Also fails, requires a higher allowed footprint.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700116 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700117 EXPECT_TRUE(ptr5 == NULL);
118
119 // Release some memory.
120 size_t free3 = space->AllocationSize(ptr3);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700121 EXPECT_EQ(free3, ptr3_bytes_allocated);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700122 EXPECT_EQ(free3, space->Free(self, ptr3));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700123 EXPECT_LE(8U * MB, free3);
124
125 // Succeeds, now that memory has been freed.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700126 void* ptr6 = space->AllocWithGrowth(self, 9 * MB, &dummy);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700127 EXPECT_TRUE(ptr6 != NULL);
128
129 // Final clean up.
130 size_t free1 = space->AllocationSize(ptr1);
Ian Rogers50b35e22012-10-04 10:09:15 -0700131 space->Free(self, ptr1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700132 EXPECT_LE(1U * MB, free1);
133
134 // Make sure that the zygote space isn't directly at the start of the space.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700135 space->Alloc(self, 1U * MB, &dummy);
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700136 space = space->CreateZygoteSpace("alloc space");
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700137
138 // Make space findable to the heap, will also delete space when runtime is cleaned up
Ian Rogers1d54e732013-05-02 21:10:01 -0700139 AddContinuousSpace(space);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700140
141 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700142 ptr1 = space->Alloc(self, 1 * MB, &dummy);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700143 EXPECT_TRUE(ptr1 != NULL);
144
145 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700146 ptr2 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700147 EXPECT_TRUE(ptr2 == NULL);
148
149 // Succeeds, adjusts the footprint.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700150 ptr3 = space->AllocWithGrowth(self, 2 * MB, &dummy);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700151 EXPECT_TRUE(ptr3 != NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -0700152 space->Free(self, ptr3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700153
154 // Final clean up.
155 free1 = space->AllocationSize(ptr1);
Ian Rogers50b35e22012-10-04 10:09:15 -0700156 space->Free(self, ptr1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700157 EXPECT_LE(1U * MB, free1);
158}
159
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -0700160TEST_F(SpaceTest, AllocAndFree) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700161 size_t dummy = 0;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700162 DlMallocSpace* space(DlMallocSpace::Create("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Ian Rogers30fab402012-01-23 15:43:46 -0800163 ASSERT_TRUE(space != NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -0700164 Thread* self = Thread::Current();
Ian Rogers30fab402012-01-23 15:43:46 -0800165
Ian Rogers3bb17a62012-01-27 23:56:44 -0800166 // Make space findable to the heap, will also delete space when runtime is cleaned up
Ian Rogers1d54e732013-05-02 21:10:01 -0700167 AddContinuousSpace(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700168
Ian Rogers3bb17a62012-01-27 23:56:44 -0800169 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700170 mirror::Object* ptr1 = space->Alloc(self, 1 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700171 EXPECT_TRUE(ptr1 != NULL);
172
Ian Rogers3bb17a62012-01-27 23:56:44 -0800173 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700174 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700175 EXPECT_TRUE(ptr2 == NULL);
176
177 // Succeeds, adjusts the footprint.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700178 size_t ptr3_bytes_allocated;
179 mirror::Object* ptr3 = space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700180 EXPECT_TRUE(ptr3 != NULL);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700181 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700182
Ian Rogers3bb17a62012-01-27 23:56:44 -0800183 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700184 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800185 EXPECT_TRUE(ptr4 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700186
187 // Also fails, requires a higher allowed footprint.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700188 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800189 EXPECT_TRUE(ptr5 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700190
191 // Release some memory.
Ian Rogers30fab402012-01-23 15:43:46 -0800192 size_t free3 = space->AllocationSize(ptr3);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700193 EXPECT_EQ(free3, ptr3_bytes_allocated);
Ian Rogers50b35e22012-10-04 10:09:15 -0700194 space->Free(self, ptr3);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700195 EXPECT_LE(8U * MB, free3);
196
197 // Succeeds, now that memory has been freed.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700198 void* ptr6 = space->AllocWithGrowth(self, 9 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700199 EXPECT_TRUE(ptr6 != NULL);
200
201 // Final clean up.
Ian Rogers30fab402012-01-23 15:43:46 -0800202 size_t free1 = space->AllocationSize(ptr1);
Ian Rogers50b35e22012-10-04 10:09:15 -0700203 space->Free(self, ptr1);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700204 EXPECT_LE(1U * MB, free1);
205}
206
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700207TEST_F(SpaceTest, LargeObjectTest) {
208 size_t rand_seed = 0;
209 for (size_t i = 0; i < 2; ++i) {
210 LargeObjectSpace* los = NULL;
211 if (i == 0) {
212 los = space::LargeObjectMapSpace::Create("large object space");
213 } else {
214 los = space::FreeListSpace::Create("large object space", NULL, 128 * MB);
215 }
216
217 static const size_t num_allocations = 64;
218 static const size_t max_allocation_size = 0x100000;
219 std::vector<std::pair<mirror::Object*, size_t> > requests;
220
221 for (size_t phase = 0; phase < 2; ++phase) {
222 while (requests.size() < num_allocations) {
223 size_t request_size = test_rand(&rand_seed) % max_allocation_size;
224 size_t allocation_size = 0;
225 mirror::Object* obj = los->Alloc(Thread::Current(), request_size, &allocation_size);
226 ASSERT_TRUE(obj != NULL);
227 ASSERT_EQ(allocation_size, los->AllocationSize(obj));
228 ASSERT_GE(allocation_size, request_size);
229 // Fill in our magic value.
230 byte magic = (request_size & 0xFF) | 1;
231 memset(obj, magic, request_size);
232 requests.push_back(std::make_pair(obj, request_size));
233 }
234
235 // "Randomly" shuffle the requests.
236 for (size_t k = 0; k < 10; ++k) {
237 for (size_t j = 0; j < requests.size(); ++j) {
238 std::swap(requests[j], requests[test_rand(&rand_seed) % requests.size()]);
239 }
240 }
241
242 // Free 1 / 2 the allocations the first phase, and all the second phase.
243 size_t limit = !phase ? requests.size() / 2 : 0;
244 while (requests.size() > limit) {
245 mirror::Object* obj = requests.back().first;
246 size_t request_size = requests.back().second;
247 requests.pop_back();
248 byte magic = (request_size & 0xFF) | 1;
249 for (size_t k = 0; k < request_size; ++k) {
250 ASSERT_EQ(reinterpret_cast<const byte*>(obj)[k], magic);
251 }
252 ASSERT_GE(los->Free(Thread::Current(), obj), request_size);
253 }
254 }
255
256 size_t bytes_allocated = 0;
257 // Checks that the coalescing works.
258 mirror::Object* obj = los->Alloc(Thread::Current(), 100 * MB, &bytes_allocated);
259 EXPECT_TRUE(obj != NULL);
260 los->Free(Thread::Current(), obj);
261
262 EXPECT_EQ(0U, los->GetBytesAllocated());
263 EXPECT_EQ(0U, los->GetObjectsAllocated());
264 delete los;
265 }
266}
267
Ian Rogers3bb17a62012-01-27 23:56:44 -0800268TEST_F(SpaceTest, AllocAndFreeList) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700269 DlMallocSpace* space(DlMallocSpace::Create("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800270 ASSERT_TRUE(space != NULL);
271
272 // Make space findable to the heap, will also delete space when runtime is cleaned up
Ian Rogers1d54e732013-05-02 21:10:01 -0700273 AddContinuousSpace(space);
Ian Rogers50b35e22012-10-04 10:09:15 -0700274 Thread* self = Thread::Current();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800275
276 // Succeeds, fits without adjusting the max allowed footprint.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800277 mirror::Object* lots_of_objects[1024];
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700278 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700279 size_t allocation_size = 0;
280 lots_of_objects[i] = space->Alloc(self, 16, &allocation_size);
281 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i]));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800282 EXPECT_TRUE(lots_of_objects[i] != NULL);
283 }
284
285 // Release memory and check pointers are NULL
Ian Rogers50b35e22012-10-04 10:09:15 -0700286 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700287 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800288 EXPECT_TRUE(lots_of_objects[i] == NULL);
289 }
290
291 // Succeeds, fits by adjusting the max allowed footprint.
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700292 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700293 size_t allocation_size = 0;
294 lots_of_objects[i] = space->AllocWithGrowth(self, 1024, &allocation_size);
295 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i]));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800296 EXPECT_TRUE(lots_of_objects[i] != NULL);
297 }
298
299 // Release memory and check pointers are NULL
Ian Rogers50b35e22012-10-04 10:09:15 -0700300 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700301 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800302 EXPECT_TRUE(lots_of_objects[i] == NULL);
303 }
304}
305
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700306void SpaceTest::SizeFootPrintGrowthLimitAndTrimBody(DlMallocSpace* space, intptr_t object_size,
Ian Rogers3bb17a62012-01-27 23:56:44 -0800307 int round, size_t growth_limit) {
308 if (((object_size > 0 && object_size >= static_cast<intptr_t>(growth_limit))) ||
309 ((object_size < 0 && -object_size >= static_cast<intptr_t>(growth_limit)))) {
310 // No allocation can succeed
311 return;
312 }
313 // Mspace for raw dlmalloc operations
314 void* mspace = space->GetMspace();
315
316 // mspace's footprint equals amount of resources requested from system
317 size_t footprint = mspace_footprint(mspace);
318
319 // mspace must at least have its book keeping allocated
320 EXPECT_GT(footprint, 0u);
321
322 // mspace but it shouldn't exceed the initial size
323 EXPECT_LE(footprint, growth_limit);
324
325 // space's size shouldn't exceed the initial size
326 EXPECT_LE(space->Size(), growth_limit);
327
328 // this invariant should always hold or else the mspace has grown to be larger than what the
329 // space believes its size is (which will break invariants)
330 EXPECT_GE(space->Size(), footprint);
331
332 // Fill the space with lots of small objects up to the growth limit
333 size_t max_objects = (growth_limit / (object_size > 0 ? object_size : 8)) + 1;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800334 UniquePtr<mirror::Object*[]> lots_of_objects(new mirror::Object*[max_objects]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800335 size_t last_object = 0; // last object for which allocation succeeded
336 size_t amount_allocated = 0; // amount of space allocated
Ian Rogers50b35e22012-10-04 10:09:15 -0700337 Thread* self = Thread::Current();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700338 size_t rand_seed = 123456789;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700339 for (size_t i = 0; i < max_objects; i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800340 size_t alloc_fails = 0; // number of failed allocations
341 size_t max_fails = 30; // number of times we fail allocation before giving up
342 for (; alloc_fails < max_fails; alloc_fails++) {
343 size_t alloc_size;
344 if (object_size > 0) {
345 alloc_size = object_size;
346 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700347 alloc_size = test_rand(&rand_seed) % static_cast<size_t>(-object_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800348 if (alloc_size < 8) {
349 alloc_size = 8;
350 }
351 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800352 mirror::Object* object;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700353 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800354 if (round <= 1) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700355 object = space->Alloc(self, alloc_size, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800356 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700357 object = space->AllocWithGrowth(self, alloc_size, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800358 }
359 footprint = mspace_footprint(mspace);
360 EXPECT_GE(space->Size(), footprint); // invariant
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700361 if (object != NULL) { // allocation succeeded
Ian Rogers3bb17a62012-01-27 23:56:44 -0800362 lots_of_objects.get()[i] = object;
363 size_t allocation_size = space->AllocationSize(object);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700364 EXPECT_EQ(bytes_allocated, allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800365 if (object_size > 0) {
366 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
367 } else {
368 EXPECT_GE(allocation_size, 8u);
369 }
370 amount_allocated += allocation_size;
371 break;
372 }
373 }
374 if (alloc_fails == max_fails) {
375 last_object = i;
376 break;
377 }
378 }
379 CHECK_NE(last_object, 0u); // we should have filled the space
380 EXPECT_GT(amount_allocated, 0u);
381
382 // We shouldn't have gone past the growth_limit
383 EXPECT_LE(amount_allocated, growth_limit);
384 EXPECT_LE(footprint, growth_limit);
385 EXPECT_LE(space->Size(), growth_limit);
386
387 // footprint and size should agree with amount allocated
388 EXPECT_GE(footprint, amount_allocated);
389 EXPECT_GE(space->Size(), amount_allocated);
390
391 // Release storage in a semi-adhoc manner
392 size_t free_increment = 96;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700393 while (true) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800394 // Give the space a haircut
395 space->Trim();
396
397 // Bounds sanity
398 footprint = mspace_footprint(mspace);
399 EXPECT_LE(amount_allocated, growth_limit);
400 EXPECT_GE(footprint, amount_allocated);
401 EXPECT_LE(footprint, growth_limit);
402 EXPECT_GE(space->Size(), amount_allocated);
403 EXPECT_LE(space->Size(), growth_limit);
404
405 if (free_increment == 0) {
406 break;
407 }
408
409 // Free some objects
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700410 for (size_t i = 0; i < last_object; i += free_increment) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800411 mirror::Object* object = lots_of_objects.get()[i];
Ian Rogers3bb17a62012-01-27 23:56:44 -0800412 if (object == NULL) {
413 continue;
414 }
415 size_t allocation_size = space->AllocationSize(object);
416 if (object_size > 0) {
417 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
418 } else {
419 EXPECT_GE(allocation_size, 8u);
420 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700421 space->Free(self, object);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800422 lots_of_objects.get()[i] = NULL;
423 amount_allocated -= allocation_size;
424 footprint = mspace_footprint(mspace);
425 EXPECT_GE(space->Size(), footprint); // invariant
426 }
427
428 free_increment >>= 1;
429 }
430
431 // All memory was released, try a large allocation to check freed memory is being coalesced
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800432 mirror::Object* large_object;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800433 size_t three_quarters_space = (growth_limit / 2) + (growth_limit / 4);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700434 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800435 if (round <= 1) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700436 large_object = space->Alloc(self, three_quarters_space, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800437 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700438 large_object = space->AllocWithGrowth(self, three_quarters_space, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800439 }
440 EXPECT_TRUE(large_object != NULL);
441
442 // Sanity check footprint
443 footprint = mspace_footprint(mspace);
444 EXPECT_LE(footprint, growth_limit);
445 EXPECT_GE(space->Size(), footprint);
446 EXPECT_LE(space->Size(), growth_limit);
447
448 // Clean up
Ian Rogers50b35e22012-10-04 10:09:15 -0700449 space->Free(self, large_object);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800450
451 // Sanity check footprint
452 footprint = mspace_footprint(mspace);
453 EXPECT_LE(footprint, growth_limit);
454 EXPECT_GE(space->Size(), footprint);
455 EXPECT_LE(space->Size(), growth_limit);
456}
457
458void SpaceTest::SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size) {
459 size_t initial_size = 4 * MB;
460 size_t growth_limit = 8 * MB;
461 size_t capacity = 16 * MB;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700462 DlMallocSpace* space(DlMallocSpace::Create("test", initial_size, growth_limit, capacity, NULL));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800463 ASSERT_TRUE(space != NULL);
464
465 // Basic sanity
466 EXPECT_EQ(space->Capacity(), growth_limit);
467 EXPECT_EQ(space->NonGrowthLimitCapacity(), capacity);
468
469 // Make space findable to the heap, will also delete space when runtime is cleaned up
Ian Rogers1d54e732013-05-02 21:10:01 -0700470 AddContinuousSpace(space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800471
472 // In this round we don't allocate with growth and therefore can't grow past the initial size.
473 // This effectively makes the growth_limit the initial_size, so assert this.
474 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 1, initial_size);
475 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 2, growth_limit);
476 // Remove growth limit
477 space->ClearGrowthLimit();
478 EXPECT_EQ(space->Capacity(), capacity);
479 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 3, capacity);
480}
481
482#define TEST_SizeFootPrintGrowthLimitAndTrim(name, size) \
483 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name) { \
484 SizeFootPrintGrowthLimitAndTrimDriver(size); \
485 } \
486 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name) { \
487 SizeFootPrintGrowthLimitAndTrimDriver(-size); \
488 }
489
490// Each size test is its own test so that we get a fresh heap each time
491TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_8B) {
492 SizeFootPrintGrowthLimitAndTrimDriver(8);
493}
494TEST_SizeFootPrintGrowthLimitAndTrim(16B, 16)
495TEST_SizeFootPrintGrowthLimitAndTrim(24B, 24)
496TEST_SizeFootPrintGrowthLimitAndTrim(32B, 32)
497TEST_SizeFootPrintGrowthLimitAndTrim(64B, 64)
498TEST_SizeFootPrintGrowthLimitAndTrim(128B, 128)
499TEST_SizeFootPrintGrowthLimitAndTrim(1KB, 1 * KB)
500TEST_SizeFootPrintGrowthLimitAndTrim(4KB, 4 * KB)
501TEST_SizeFootPrintGrowthLimitAndTrim(1MB, 1 * MB)
502TEST_SizeFootPrintGrowthLimitAndTrim(4MB, 4 * MB)
503TEST_SizeFootPrintGrowthLimitAndTrim(8MB, 8 * MB)
504
Ian Rogers1d54e732013-05-02 21:10:01 -0700505} // namespace space
506} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700507} // namespace art