blob: 427d5471d82cb10b967fab0624f5d2b0a60a81f7 [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"
Mathieu Chartiera1602f22014-01-13 17:19:19 -080019#include "zygote_space.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070020
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070021#include "common_test.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070022#include "globals.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070023#include "UniquePtr.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070024#include "mirror/array-inl.h"
25#include "mirror/object-inl.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070026
Ian Rogers3bb17a62012-01-27 23:56:44 -080027#include <stdint.h>
28
Carl Shapiro69759ea2011-07-21 18:13:35 -070029namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070030namespace gc {
31namespace space {
Carl Shapiro69759ea2011-07-21 18:13:35 -070032
Ian Rogers3bb17a62012-01-27 23:56:44 -080033class SpaceTest : public CommonTest {
34 public:
Mathieu Chartier590fee92013-09-13 13:46:47 -070035 void AddSpace(ContinuousSpace* space) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070036 // For RosAlloc, revoke the thread local runs before moving onto a
37 // new alloc space.
38 Runtime::Current()->GetHeap()->RevokeAllThreadLocalBuffers();
Mathieu Chartier590fee92013-09-13 13:46:47 -070039 Runtime::Current()->GetHeap()->AddSpace(space);
Ian Rogers1d54e732013-05-02 21:10:01 -070040 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070041 void InstallClass(mirror::Object* o, size_t size) NO_THREAD_SAFETY_ANALYSIS {
42 // Note the minimum size, which is the size of a zero-length byte array, is 12.
43 EXPECT_GE(size, static_cast<size_t>(12));
44 SirtRef<mirror::ClassLoader> null_loader(Thread::Current(), NULL);
45 mirror::Class* byte_array_class = Runtime::Current()->GetClassLinker()->FindClass("[B", null_loader);
46 EXPECT_TRUE(byte_array_class != NULL);
47 o->SetClass(byte_array_class);
48 mirror::Array* arr = o->AsArray();
49 // size_t header_size = sizeof(mirror::Object) + 4;
50 size_t header_size = arr->DataOffset(1).Uint32Value();
51 int32_t length = size - header_size;
52 arr->SetLength(length);
53 EXPECT_EQ(arr->SizeOf(), size);
54 }
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080055
56 static MallocSpace* CreateDlMallocSpace(const std::string& name, size_t initial_size, size_t growth_limit,
57 size_t capacity, byte* requested_begin) {
58 return DlMallocSpace::Create(name, initial_size, growth_limit, capacity, requested_begin);
59 }
60 static MallocSpace* CreateRosAllocSpace(const std::string& name, size_t initial_size, size_t growth_limit,
61 size_t capacity, byte* requested_begin) {
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -080062 return RosAllocSpace::Create(name, initial_size, growth_limit, capacity, requested_begin,
63 Runtime::Current()->GetHeap()->IsLowMemoryMode());
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080064 }
65
66 typedef MallocSpace* (*CreateSpaceFn)(const std::string& name, size_t initial_size, size_t growth_limit,
67 size_t capacity, byte* requested_begin);
68 void InitTestBody(CreateSpaceFn create_space);
69 void ZygoteSpaceTestBody(CreateSpaceFn create_space);
70 void AllocAndFreeTestBody(CreateSpaceFn create_space);
71 void AllocAndFreeListTestBody(CreateSpaceFn create_space);
72
73 void SizeFootPrintGrowthLimitAndTrimBody(MallocSpace* space, intptr_t object_size,
74 int round, size_t growth_limit);
75 void SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size, CreateSpaceFn create_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -080076};
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070077
Mathieu Chartiereb5710e2013-07-25 15:19:42 -070078static size_t test_rand(size_t* seed) {
79 *seed = *seed * 1103515245 + 12345;
80 return *seed;
81}
82
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080083void SpaceTest::InitTestBody(CreateSpaceFn create_space) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070084 {
jeffhaoc1160702011-10-27 15:48:45 -070085 // Init < max == growth
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080086 UniquePtr<Space> space(create_space("test", 16 * MB, 32 * MB, 32 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070087 EXPECT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070088 }
89 {
jeffhaoc1160702011-10-27 15:48:45 -070090 // Init == max == growth
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080091 UniquePtr<Space> space(create_space("test", 16 * MB, 16 * MB, 16 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070092 EXPECT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070093 }
94 {
jeffhaoc1160702011-10-27 15:48:45 -070095 // Init > max == growth
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080096 UniquePtr<Space> space(create_space("test", 32 * MB, 16 * MB, 16 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070097 EXPECT_TRUE(space.get() == NULL);
98 }
99 {
100 // Growth == init < max
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800101 UniquePtr<Space> space(create_space("test", 16 * MB, 16 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -0700102 EXPECT_TRUE(space.get() != NULL);
103 }
104 {
105 // Growth < init < max
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800106 UniquePtr<Space> space(create_space("test", 16 * MB, 8 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -0700107 EXPECT_TRUE(space.get() == NULL);
108 }
109 {
110 // Init < growth < max
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800111 UniquePtr<Space> space(create_space("test", 8 * MB, 16 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -0700112 EXPECT_TRUE(space.get() != NULL);
113 }
114 {
115 // Init < max < growth
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800116 UniquePtr<Space> space(create_space("test", 8 * MB, 32 * MB, 16 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -0700117 EXPECT_TRUE(space.get() == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700118 }
119}
120
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800121TEST_F(SpaceTest, Init_DlMallocSpace) {
122 InitTestBody(SpaceTest::CreateDlMallocSpace);
123}
124TEST_F(SpaceTest, Init_RosAllocSpace) {
125 InitTestBody(SpaceTest::CreateRosAllocSpace);
126}
127
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700128// TODO: This test is not very good, we should improve it.
129// The test should do more allocations before the creation of the ZygoteSpace, and then do
130// allocations after the ZygoteSpace is created. The test should also do some GCs to ensure that
131// the GC works with the ZygoteSpace.
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800132void SpaceTest::ZygoteSpaceTestBody(CreateSpaceFn create_space) {
133 size_t dummy = 0;
134 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, NULL));
135 ASSERT_TRUE(space != NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700136
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800137 // Make space findable to the heap, will also delete space when runtime is cleaned up
138 AddSpace(space);
139 Thread* self = Thread::Current();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700140
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800141 // Succeeds, fits without adjusting the footprint limit.
142 mirror::Object* ptr1 = space->Alloc(self, 1 * MB, &dummy);
143 EXPECT_TRUE(ptr1 != NULL);
144 InstallClass(ptr1, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700145
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800146 // Fails, requires a higher footprint limit.
147 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy);
148 EXPECT_TRUE(ptr2 == NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700149
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800150 // Succeeds, adjusts the footprint.
151 size_t ptr3_bytes_allocated;
152 mirror::Object* ptr3 = space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated);
153 EXPECT_TRUE(ptr3 != NULL);
154 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
155 InstallClass(ptr3, 8 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700156
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800157 // Fails, requires a higher footprint limit.
158 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy);
159 EXPECT_TRUE(ptr4 == NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700160
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800161 // Also fails, requires a higher allowed footprint.
162 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy);
163 EXPECT_TRUE(ptr5 == NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700164
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800165 // Release some memory.
166 size_t free3 = space->AllocationSize(ptr3);
167 EXPECT_EQ(free3, ptr3_bytes_allocated);
168 EXPECT_EQ(free3, space->Free(self, ptr3));
169 EXPECT_LE(8U * MB, free3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700170
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800171 // Succeeds, now that memory has been freed.
172 mirror::Object* ptr6 = space->AllocWithGrowth(self, 9 * MB, &dummy);
173 EXPECT_TRUE(ptr6 != NULL);
174 InstallClass(ptr6, 9 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700175
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800176 // Final clean up.
177 size_t free1 = space->AllocationSize(ptr1);
178 space->Free(self, ptr1);
179 EXPECT_LE(1U * MB, free1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700180
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800181 // Make sure that the zygote space isn't directly at the start of the space.
182 space->Alloc(self, 1U * MB, &dummy);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800183
184 gc::Heap* heap = Runtime::Current()->GetHeap();
185 space::Space* old_space = space;
186 heap->RemoveSpace(old_space);
187 space::ZygoteSpace* zygote_space = space->CreateZygoteSpace("alloc space",
188 heap->IsLowMemoryMode(),
189 &space);
190 delete old_space;
191 // Add the zygote space.
192 AddSpace(zygote_space);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700193
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800194 // Make space findable to the heap, will also delete space when runtime is cleaned up
195 AddSpace(space);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700196
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800197 // Succeeds, fits without adjusting the footprint limit.
198 ptr1 = space->Alloc(self, 1 * MB, &dummy);
199 EXPECT_TRUE(ptr1 != NULL);
200 InstallClass(ptr1, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700201
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800202 // Fails, requires a higher footprint limit.
203 ptr2 = space->Alloc(self, 8 * MB, &dummy);
204 EXPECT_TRUE(ptr2 == NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700205
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800206 // Succeeds, adjusts the footprint.
207 ptr3 = space->AllocWithGrowth(self, 2 * MB, &dummy);
208 EXPECT_TRUE(ptr3 != NULL);
209 InstallClass(ptr3, 2 * MB);
210 space->Free(self, ptr3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700211
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800212 // Final clean up.
213 free1 = space->AllocationSize(ptr1);
214 space->Free(self, ptr1);
215 EXPECT_LE(1U * MB, free1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700216}
217
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800218TEST_F(SpaceTest, ZygoteSpace_DlMallocSpace) {
219 ZygoteSpaceTestBody(SpaceTest::CreateDlMallocSpace);
220}
221
222TEST_F(SpaceTest, ZygoteSpace_RosAllocSpace) {
223 ZygoteSpaceTestBody(SpaceTest::CreateRosAllocSpace);
224}
225
226void SpaceTest::AllocAndFreeTestBody(CreateSpaceFn create_space) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700227 size_t dummy = 0;
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800228 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Ian Rogers30fab402012-01-23 15:43:46 -0800229 ASSERT_TRUE(space != NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -0700230 Thread* self = Thread::Current();
Ian Rogers30fab402012-01-23 15:43:46 -0800231
Ian Rogers3bb17a62012-01-27 23:56:44 -0800232 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700233 AddSpace(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700234
Ian Rogers3bb17a62012-01-27 23:56:44 -0800235 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700236 mirror::Object* ptr1 = space->Alloc(self, 1 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700237 EXPECT_TRUE(ptr1 != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700238 InstallClass(ptr1, 1 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700239
Ian Rogers3bb17a62012-01-27 23:56:44 -0800240 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700241 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700242 EXPECT_TRUE(ptr2 == NULL);
243
244 // Succeeds, adjusts the footprint.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700245 size_t ptr3_bytes_allocated;
246 mirror::Object* ptr3 = space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700247 EXPECT_TRUE(ptr3 != NULL);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700248 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700249 InstallClass(ptr3, 8 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700250
Ian Rogers3bb17a62012-01-27 23:56:44 -0800251 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700252 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800253 EXPECT_TRUE(ptr4 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700254
255 // Also fails, requires a higher allowed footprint.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700256 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800257 EXPECT_TRUE(ptr5 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700258
259 // Release some memory.
Ian Rogers30fab402012-01-23 15:43:46 -0800260 size_t free3 = space->AllocationSize(ptr3);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700261 EXPECT_EQ(free3, ptr3_bytes_allocated);
Ian Rogers50b35e22012-10-04 10:09:15 -0700262 space->Free(self, ptr3);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700263 EXPECT_LE(8U * MB, free3);
264
265 // Succeeds, now that memory has been freed.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700266 mirror::Object* ptr6 = space->AllocWithGrowth(self, 9 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700267 EXPECT_TRUE(ptr6 != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700268 InstallClass(ptr6, 9 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700269
270 // Final clean up.
Ian Rogers30fab402012-01-23 15:43:46 -0800271 size_t free1 = space->AllocationSize(ptr1);
Ian Rogers50b35e22012-10-04 10:09:15 -0700272 space->Free(self, ptr1);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700273 EXPECT_LE(1U * MB, free1);
274}
275
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800276TEST_F(SpaceTest, AllocAndFree_DlMallocSpace) {
277 AllocAndFreeTestBody(SpaceTest::CreateDlMallocSpace);
278}
279TEST_F(SpaceTest, AllocAndFree_RosAllocSpace) {
280 AllocAndFreeTestBody(SpaceTest::CreateRosAllocSpace);
281}
282
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700283TEST_F(SpaceTest, LargeObjectTest) {
284 size_t rand_seed = 0;
285 for (size_t i = 0; i < 2; ++i) {
286 LargeObjectSpace* los = NULL;
287 if (i == 0) {
288 los = space::LargeObjectMapSpace::Create("large object space");
289 } else {
290 los = space::FreeListSpace::Create("large object space", NULL, 128 * MB);
291 }
292
293 static const size_t num_allocations = 64;
294 static const size_t max_allocation_size = 0x100000;
295 std::vector<std::pair<mirror::Object*, size_t> > requests;
296
297 for (size_t phase = 0; phase < 2; ++phase) {
298 while (requests.size() < num_allocations) {
299 size_t request_size = test_rand(&rand_seed) % max_allocation_size;
300 size_t allocation_size = 0;
301 mirror::Object* obj = los->Alloc(Thread::Current(), request_size, &allocation_size);
302 ASSERT_TRUE(obj != NULL);
303 ASSERT_EQ(allocation_size, los->AllocationSize(obj));
304 ASSERT_GE(allocation_size, request_size);
305 // Fill in our magic value.
306 byte magic = (request_size & 0xFF) | 1;
307 memset(obj, magic, request_size);
308 requests.push_back(std::make_pair(obj, request_size));
309 }
310
311 // "Randomly" shuffle the requests.
312 for (size_t k = 0; k < 10; ++k) {
313 for (size_t j = 0; j < requests.size(); ++j) {
314 std::swap(requests[j], requests[test_rand(&rand_seed) % requests.size()]);
315 }
316 }
317
318 // Free 1 / 2 the allocations the first phase, and all the second phase.
319 size_t limit = !phase ? requests.size() / 2 : 0;
320 while (requests.size() > limit) {
321 mirror::Object* obj = requests.back().first;
322 size_t request_size = requests.back().second;
323 requests.pop_back();
324 byte magic = (request_size & 0xFF) | 1;
325 for (size_t k = 0; k < request_size; ++k) {
326 ASSERT_EQ(reinterpret_cast<const byte*>(obj)[k], magic);
327 }
328 ASSERT_GE(los->Free(Thread::Current(), obj), request_size);
329 }
330 }
331
332 size_t bytes_allocated = 0;
333 // Checks that the coalescing works.
334 mirror::Object* obj = los->Alloc(Thread::Current(), 100 * MB, &bytes_allocated);
335 EXPECT_TRUE(obj != NULL);
336 los->Free(Thread::Current(), obj);
337
338 EXPECT_EQ(0U, los->GetBytesAllocated());
339 EXPECT_EQ(0U, los->GetObjectsAllocated());
340 delete los;
341 }
342}
343
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800344void SpaceTest::AllocAndFreeListTestBody(CreateSpaceFn create_space) {
345 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800346 ASSERT_TRUE(space != NULL);
347
348 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700349 AddSpace(space);
Ian Rogers50b35e22012-10-04 10:09:15 -0700350 Thread* self = Thread::Current();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800351
352 // Succeeds, fits without adjusting the max allowed footprint.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800353 mirror::Object* lots_of_objects[1024];
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700354 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700355 size_t allocation_size = 0;
356 lots_of_objects[i] = space->Alloc(self, 16, &allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800357 EXPECT_TRUE(lots_of_objects[i] != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700358 InstallClass(lots_of_objects[i], 16);
359 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i]));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800360 }
361
362 // Release memory and check pointers are NULL
Ian Rogers50b35e22012-10-04 10:09:15 -0700363 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700364 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800365 EXPECT_TRUE(lots_of_objects[i] == NULL);
366 }
367
368 // Succeeds, fits by adjusting the max allowed footprint.
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700369 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700370 size_t allocation_size = 0;
371 lots_of_objects[i] = space->AllocWithGrowth(self, 1024, &allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800372 EXPECT_TRUE(lots_of_objects[i] != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700373 InstallClass(lots_of_objects[i], 1024);
374 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i]));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800375 }
376
377 // Release memory and check pointers are NULL
Ian Rogers50b35e22012-10-04 10:09:15 -0700378 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700379 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800380 EXPECT_TRUE(lots_of_objects[i] == NULL);
381 }
382}
383
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800384TEST_F(SpaceTest, AllocAndFreeList_DlMallocSpace) {
385 AllocAndFreeListTestBody(SpaceTest::CreateDlMallocSpace);
386}
387TEST_F(SpaceTest, AllocAndFreeList_RosAllocSpace) {
388 AllocAndFreeListTestBody(SpaceTest::CreateRosAllocSpace);
389}
390
391void SpaceTest::SizeFootPrintGrowthLimitAndTrimBody(MallocSpace* space, intptr_t object_size,
Ian Rogers3bb17a62012-01-27 23:56:44 -0800392 int round, size_t growth_limit) {
393 if (((object_size > 0 && object_size >= static_cast<intptr_t>(growth_limit))) ||
394 ((object_size < 0 && -object_size >= static_cast<intptr_t>(growth_limit)))) {
395 // No allocation can succeed
396 return;
397 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800398
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700399 // The space's footprint equals amount of resources requested from system
400 size_t footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800401
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700402 // The space must at least have its book keeping allocated
Ian Rogers3bb17a62012-01-27 23:56:44 -0800403 EXPECT_GT(footprint, 0u);
404
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700405 // But it shouldn't exceed the initial size
Ian Rogers3bb17a62012-01-27 23:56:44 -0800406 EXPECT_LE(footprint, growth_limit);
407
408 // space's size shouldn't exceed the initial size
409 EXPECT_LE(space->Size(), growth_limit);
410
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700411 // this invariant should always hold or else the space has grown to be larger than what the
Ian Rogers3bb17a62012-01-27 23:56:44 -0800412 // space believes its size is (which will break invariants)
413 EXPECT_GE(space->Size(), footprint);
414
415 // Fill the space with lots of small objects up to the growth limit
416 size_t max_objects = (growth_limit / (object_size > 0 ? object_size : 8)) + 1;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800417 UniquePtr<mirror::Object*[]> lots_of_objects(new mirror::Object*[max_objects]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800418 size_t last_object = 0; // last object for which allocation succeeded
419 size_t amount_allocated = 0; // amount of space allocated
Ian Rogers50b35e22012-10-04 10:09:15 -0700420 Thread* self = Thread::Current();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700421 size_t rand_seed = 123456789;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700422 for (size_t i = 0; i < max_objects; i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800423 size_t alloc_fails = 0; // number of failed allocations
424 size_t max_fails = 30; // number of times we fail allocation before giving up
425 for (; alloc_fails < max_fails; alloc_fails++) {
426 size_t alloc_size;
427 if (object_size > 0) {
428 alloc_size = object_size;
429 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700430 alloc_size = test_rand(&rand_seed) % static_cast<size_t>(-object_size);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700431 // Note the minimum size, which is the size of a zero-length byte array, is 12.
432 if (alloc_size < 12) {
433 alloc_size = 12;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800434 }
435 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800436 mirror::Object* object;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700437 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800438 if (round <= 1) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700439 object = space->Alloc(self, alloc_size, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800440 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700441 object = space->AllocWithGrowth(self, alloc_size, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800442 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700443 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800444 EXPECT_GE(space->Size(), footprint); // invariant
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700445 if (object != NULL) { // allocation succeeded
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700446 InstallClass(object, alloc_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800447 lots_of_objects.get()[i] = object;
448 size_t allocation_size = space->AllocationSize(object);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700449 EXPECT_EQ(bytes_allocated, allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800450 if (object_size > 0) {
451 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
452 } else {
453 EXPECT_GE(allocation_size, 8u);
454 }
455 amount_allocated += allocation_size;
456 break;
457 }
458 }
459 if (alloc_fails == max_fails) {
460 last_object = i;
461 break;
462 }
463 }
464 CHECK_NE(last_object, 0u); // we should have filled the space
465 EXPECT_GT(amount_allocated, 0u);
466
467 // We shouldn't have gone past the growth_limit
468 EXPECT_LE(amount_allocated, growth_limit);
469 EXPECT_LE(footprint, growth_limit);
470 EXPECT_LE(space->Size(), growth_limit);
471
472 // footprint and size should agree with amount allocated
473 EXPECT_GE(footprint, amount_allocated);
474 EXPECT_GE(space->Size(), amount_allocated);
475
476 // Release storage in a semi-adhoc manner
477 size_t free_increment = 96;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700478 while (true) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800479 // Give the space a haircut
480 space->Trim();
481
482 // Bounds sanity
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700483 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800484 EXPECT_LE(amount_allocated, growth_limit);
485 EXPECT_GE(footprint, amount_allocated);
486 EXPECT_LE(footprint, growth_limit);
487 EXPECT_GE(space->Size(), amount_allocated);
488 EXPECT_LE(space->Size(), growth_limit);
489
490 if (free_increment == 0) {
491 break;
492 }
493
494 // Free some objects
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700495 for (size_t i = 0; i < last_object; i += free_increment) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800496 mirror::Object* object = lots_of_objects.get()[i];
Ian Rogers3bb17a62012-01-27 23:56:44 -0800497 if (object == NULL) {
498 continue;
499 }
500 size_t allocation_size = space->AllocationSize(object);
501 if (object_size > 0) {
502 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
503 } else {
504 EXPECT_GE(allocation_size, 8u);
505 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700506 space->Free(self, object);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800507 lots_of_objects.get()[i] = NULL;
508 amount_allocated -= allocation_size;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700509 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800510 EXPECT_GE(space->Size(), footprint); // invariant
511 }
512
513 free_increment >>= 1;
514 }
515
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700516 // The space has become empty here before allocating a large object
517 // below. For RosAlloc, revoke thread-local runs, which are kept
518 // even when empty for a performance reason, so that they won't
519 // cause the following large object allocation to fail due to
520 // potential fragmentation. Note they are normally revoked at each
521 // GC (but no GC here.)
522 space->RevokeAllThreadLocalBuffers();
523
Ian Rogers3bb17a62012-01-27 23:56:44 -0800524 // All memory was released, try a large allocation to check freed memory is being coalesced
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800525 mirror::Object* large_object;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800526 size_t three_quarters_space = (growth_limit / 2) + (growth_limit / 4);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700527 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800528 if (round <= 1) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700529 large_object = space->Alloc(self, three_quarters_space, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800530 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700531 large_object = space->AllocWithGrowth(self, three_quarters_space, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800532 }
533 EXPECT_TRUE(large_object != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700534 InstallClass(large_object, three_quarters_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800535
536 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700537 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800538 EXPECT_LE(footprint, growth_limit);
539 EXPECT_GE(space->Size(), footprint);
540 EXPECT_LE(space->Size(), growth_limit);
541
542 // Clean up
Ian Rogers50b35e22012-10-04 10:09:15 -0700543 space->Free(self, large_object);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800544
545 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700546 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800547 EXPECT_LE(footprint, growth_limit);
548 EXPECT_GE(space->Size(), footprint);
549 EXPECT_LE(space->Size(), growth_limit);
550}
551
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800552void SpaceTest::SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size, CreateSpaceFn create_space) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800553 size_t initial_size = 4 * MB;
554 size_t growth_limit = 8 * MB;
555 size_t capacity = 16 * MB;
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800556 MallocSpace* space(create_space("test", initial_size, growth_limit, capacity, NULL));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800557 ASSERT_TRUE(space != NULL);
558
559 // Basic sanity
560 EXPECT_EQ(space->Capacity(), growth_limit);
561 EXPECT_EQ(space->NonGrowthLimitCapacity(), capacity);
562
563 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700564 AddSpace(space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800565
566 // In this round we don't allocate with growth and therefore can't grow past the initial size.
567 // This effectively makes the growth_limit the initial_size, so assert this.
568 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 1, initial_size);
569 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 2, growth_limit);
570 // Remove growth limit
571 space->ClearGrowthLimit();
572 EXPECT_EQ(space->Capacity(), capacity);
573 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 3, capacity);
574}
575
576#define TEST_SizeFootPrintGrowthLimitAndTrim(name, size) \
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800577 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name##_DlMallocSpace) { \
578 SizeFootPrintGrowthLimitAndTrimDriver(size, SpaceTest::CreateDlMallocSpace); \
Ian Rogers3bb17a62012-01-27 23:56:44 -0800579 } \
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800580 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name##_DlMallocSpace) { \
581 SizeFootPrintGrowthLimitAndTrimDriver(-size, SpaceTest::CreateDlMallocSpace); \
582 } \
583 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name##_RosAllocSpace) { \
584 SizeFootPrintGrowthLimitAndTrimDriver(size, SpaceTest::CreateRosAllocSpace); \
585 } \
586 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name##_RosAllocSpace) { \
587 SizeFootPrintGrowthLimitAndTrimDriver(-size, SpaceTest::CreateRosAllocSpace); \
Ian Rogers3bb17a62012-01-27 23:56:44 -0800588 }
589
590// Each size test is its own test so that we get a fresh heap each time
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800591TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_12B_DlMallocSpace) {
592 SizeFootPrintGrowthLimitAndTrimDriver(12, SpaceTest::CreateDlMallocSpace);
593}
594TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_12B_RosAllocSpace) {
595 SizeFootPrintGrowthLimitAndTrimDriver(12, SpaceTest::CreateRosAllocSpace);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800596}
597TEST_SizeFootPrintGrowthLimitAndTrim(16B, 16)
598TEST_SizeFootPrintGrowthLimitAndTrim(24B, 24)
599TEST_SizeFootPrintGrowthLimitAndTrim(32B, 32)
600TEST_SizeFootPrintGrowthLimitAndTrim(64B, 64)
601TEST_SizeFootPrintGrowthLimitAndTrim(128B, 128)
602TEST_SizeFootPrintGrowthLimitAndTrim(1KB, 1 * KB)
603TEST_SizeFootPrintGrowthLimitAndTrim(4KB, 4 * KB)
604TEST_SizeFootPrintGrowthLimitAndTrim(1MB, 1 * MB)
605TEST_SizeFootPrintGrowthLimitAndTrim(4MB, 4 * MB)
606TEST_SizeFootPrintGrowthLimitAndTrim(8MB, 8 * MB)
607
Ian Rogers1d54e732013-05-02 21:10:01 -0700608} // namespace space
609} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700610} // namespace art