blob: 9989ffefaf3b3f149105e8e0195cc812b16e266a [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.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800166 ScopedObjectAccess soa(self);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800167 size_t free3 = space->AllocationSize(ptr3);
168 EXPECT_EQ(free3, ptr3_bytes_allocated);
169 EXPECT_EQ(free3, space->Free(self, ptr3));
170 EXPECT_LE(8U * MB, free3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700171
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800172 // Succeeds, now that memory has been freed.
173 mirror::Object* ptr6 = space->AllocWithGrowth(self, 9 * MB, &dummy);
174 EXPECT_TRUE(ptr6 != NULL);
175 InstallClass(ptr6, 9 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700176
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800177 // Final clean up.
178 size_t free1 = space->AllocationSize(ptr1);
179 space->Free(self, ptr1);
180 EXPECT_LE(1U * MB, free1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700181
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800182 // Make sure that the zygote space isn't directly at the start of the space.
183 space->Alloc(self, 1U * MB, &dummy);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800184
185 gc::Heap* heap = Runtime::Current()->GetHeap();
186 space::Space* old_space = space;
187 heap->RemoveSpace(old_space);
188 space::ZygoteSpace* zygote_space = space->CreateZygoteSpace("alloc space",
189 heap->IsLowMemoryMode(),
190 &space);
191 delete old_space;
192 // Add the zygote space.
193 AddSpace(zygote_space);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700194
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800195 // Make space findable to the heap, will also delete space when runtime is cleaned up
196 AddSpace(space);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700197
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800198 // Succeeds, fits without adjusting the footprint limit.
199 ptr1 = space->Alloc(self, 1 * MB, &dummy);
200 EXPECT_TRUE(ptr1 != NULL);
201 InstallClass(ptr1, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700202
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800203 // Fails, requires a higher footprint limit.
204 ptr2 = space->Alloc(self, 8 * MB, &dummy);
205 EXPECT_TRUE(ptr2 == NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700206
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800207 // Succeeds, adjusts the footprint.
208 ptr3 = space->AllocWithGrowth(self, 2 * MB, &dummy);
209 EXPECT_TRUE(ptr3 != NULL);
210 InstallClass(ptr3, 2 * MB);
211 space->Free(self, ptr3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700212
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800213 // Final clean up.
214 free1 = space->AllocationSize(ptr1);
215 space->Free(self, ptr1);
216 EXPECT_LE(1U * MB, free1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700217}
218
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800219TEST_F(SpaceTest, ZygoteSpace_DlMallocSpace) {
220 ZygoteSpaceTestBody(SpaceTest::CreateDlMallocSpace);
221}
222
223TEST_F(SpaceTest, ZygoteSpace_RosAllocSpace) {
224 ZygoteSpaceTestBody(SpaceTest::CreateRosAllocSpace);
225}
226
227void SpaceTest::AllocAndFreeTestBody(CreateSpaceFn create_space) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700228 size_t dummy = 0;
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800229 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Ian Rogers30fab402012-01-23 15:43:46 -0800230 ASSERT_TRUE(space != NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -0700231 Thread* self = Thread::Current();
Ian Rogers30fab402012-01-23 15:43:46 -0800232
Ian Rogers3bb17a62012-01-27 23:56:44 -0800233 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700234 AddSpace(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700235
Ian Rogers3bb17a62012-01-27 23:56:44 -0800236 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700237 mirror::Object* ptr1 = space->Alloc(self, 1 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700238 EXPECT_TRUE(ptr1 != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700239 InstallClass(ptr1, 1 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700240
Ian Rogers3bb17a62012-01-27 23:56:44 -0800241 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700242 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700243 EXPECT_TRUE(ptr2 == NULL);
244
245 // Succeeds, adjusts the footprint.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700246 size_t ptr3_bytes_allocated;
247 mirror::Object* ptr3 = space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700248 EXPECT_TRUE(ptr3 != NULL);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700249 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700250 InstallClass(ptr3, 8 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700251
Ian Rogers3bb17a62012-01-27 23:56:44 -0800252 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700253 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800254 EXPECT_TRUE(ptr4 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700255
256 // Also fails, requires a higher allowed footprint.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700257 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800258 EXPECT_TRUE(ptr5 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700259
260 // Release some memory.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800261 ScopedObjectAccess soa(self);
Ian Rogers30fab402012-01-23 15:43:46 -0800262 size_t free3 = space->AllocationSize(ptr3);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700263 EXPECT_EQ(free3, ptr3_bytes_allocated);
Ian Rogers50b35e22012-10-04 10:09:15 -0700264 space->Free(self, ptr3);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700265 EXPECT_LE(8U * MB, free3);
266
267 // Succeeds, now that memory has been freed.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700268 mirror::Object* ptr6 = space->AllocWithGrowth(self, 9 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700269 EXPECT_TRUE(ptr6 != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700270 InstallClass(ptr6, 9 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700271
272 // Final clean up.
Ian Rogers30fab402012-01-23 15:43:46 -0800273 size_t free1 = space->AllocationSize(ptr1);
Ian Rogers50b35e22012-10-04 10:09:15 -0700274 space->Free(self, ptr1);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700275 EXPECT_LE(1U * MB, free1);
276}
277
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800278TEST_F(SpaceTest, AllocAndFree_DlMallocSpace) {
279 AllocAndFreeTestBody(SpaceTest::CreateDlMallocSpace);
280}
281TEST_F(SpaceTest, AllocAndFree_RosAllocSpace) {
282 AllocAndFreeTestBody(SpaceTest::CreateRosAllocSpace);
283}
284
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700285TEST_F(SpaceTest, LargeObjectTest) {
286 size_t rand_seed = 0;
287 for (size_t i = 0; i < 2; ++i) {
288 LargeObjectSpace* los = NULL;
289 if (i == 0) {
290 los = space::LargeObjectMapSpace::Create("large object space");
291 } else {
292 los = space::FreeListSpace::Create("large object space", NULL, 128 * MB);
293 }
294
295 static const size_t num_allocations = 64;
296 static const size_t max_allocation_size = 0x100000;
297 std::vector<std::pair<mirror::Object*, size_t> > requests;
298
299 for (size_t phase = 0; phase < 2; ++phase) {
300 while (requests.size() < num_allocations) {
301 size_t request_size = test_rand(&rand_seed) % max_allocation_size;
302 size_t allocation_size = 0;
303 mirror::Object* obj = los->Alloc(Thread::Current(), request_size, &allocation_size);
304 ASSERT_TRUE(obj != NULL);
305 ASSERT_EQ(allocation_size, los->AllocationSize(obj));
306 ASSERT_GE(allocation_size, request_size);
307 // Fill in our magic value.
308 byte magic = (request_size & 0xFF) | 1;
309 memset(obj, magic, request_size);
310 requests.push_back(std::make_pair(obj, request_size));
311 }
312
313 // "Randomly" shuffle the requests.
314 for (size_t k = 0; k < 10; ++k) {
315 for (size_t j = 0; j < requests.size(); ++j) {
316 std::swap(requests[j], requests[test_rand(&rand_seed) % requests.size()]);
317 }
318 }
319
320 // Free 1 / 2 the allocations the first phase, and all the second phase.
321 size_t limit = !phase ? requests.size() / 2 : 0;
322 while (requests.size() > limit) {
323 mirror::Object* obj = requests.back().first;
324 size_t request_size = requests.back().second;
325 requests.pop_back();
326 byte magic = (request_size & 0xFF) | 1;
327 for (size_t k = 0; k < request_size; ++k) {
328 ASSERT_EQ(reinterpret_cast<const byte*>(obj)[k], magic);
329 }
330 ASSERT_GE(los->Free(Thread::Current(), obj), request_size);
331 }
332 }
333
334 size_t bytes_allocated = 0;
335 // Checks that the coalescing works.
336 mirror::Object* obj = los->Alloc(Thread::Current(), 100 * MB, &bytes_allocated);
337 EXPECT_TRUE(obj != NULL);
338 los->Free(Thread::Current(), obj);
339
340 EXPECT_EQ(0U, los->GetBytesAllocated());
341 EXPECT_EQ(0U, los->GetObjectsAllocated());
342 delete los;
343 }
344}
345
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800346void SpaceTest::AllocAndFreeListTestBody(CreateSpaceFn create_space) {
347 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800348 ASSERT_TRUE(space != NULL);
349
350 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700351 AddSpace(space);
Ian Rogers50b35e22012-10-04 10:09:15 -0700352 Thread* self = Thread::Current();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800353
354 // Succeeds, fits without adjusting the max allowed footprint.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800355 mirror::Object* lots_of_objects[1024];
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700356 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700357 size_t allocation_size = 0;
358 lots_of_objects[i] = space->Alloc(self, 16, &allocation_size);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800359 EXPECT_TRUE(lots_of_objects[i] != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700360 InstallClass(lots_of_objects[i], 16);
361 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i]));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800362 }
363
Ian Rogersef7d42f2014-01-06 12:55:46 -0800364 // Release memory and check pointers are NULL.
365 {
366 ScopedObjectAccess soa(self);
367 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
368 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
369 EXPECT_TRUE(lots_of_objects[i] == nullptr);
370 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800371 }
372
373 // Succeeds, fits by adjusting the max allowed footprint.
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700374 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700375 size_t allocation_size = 0;
376 lots_of_objects[i] = space->AllocWithGrowth(self, 1024, &allocation_size);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800377 EXPECT_TRUE(lots_of_objects[i] != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700378 InstallClass(lots_of_objects[i], 1024);
379 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i]));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800380 }
381
382 // Release memory and check pointers are NULL
Ian Rogersef7d42f2014-01-06 12:55:46 -0800383 {
384 ScopedObjectAccess soa(self);
385 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
386 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
387 EXPECT_TRUE(lots_of_objects[i] == nullptr);
388 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800389 }
390}
391
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800392TEST_F(SpaceTest, AllocAndFreeList_DlMallocSpace) {
393 AllocAndFreeListTestBody(SpaceTest::CreateDlMallocSpace);
394}
395TEST_F(SpaceTest, AllocAndFreeList_RosAllocSpace) {
396 AllocAndFreeListTestBody(SpaceTest::CreateRosAllocSpace);
397}
398
399void SpaceTest::SizeFootPrintGrowthLimitAndTrimBody(MallocSpace* space, intptr_t object_size,
Ian Rogers3bb17a62012-01-27 23:56:44 -0800400 int round, size_t growth_limit) {
401 if (((object_size > 0 && object_size >= static_cast<intptr_t>(growth_limit))) ||
402 ((object_size < 0 && -object_size >= static_cast<intptr_t>(growth_limit)))) {
403 // No allocation can succeed
404 return;
405 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800406
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700407 // The space's footprint equals amount of resources requested from system
408 size_t footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800409
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700410 // The space must at least have its book keeping allocated
Ian Rogers3bb17a62012-01-27 23:56:44 -0800411 EXPECT_GT(footprint, 0u);
412
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700413 // But it shouldn't exceed the initial size
Ian Rogers3bb17a62012-01-27 23:56:44 -0800414 EXPECT_LE(footprint, growth_limit);
415
416 // space's size shouldn't exceed the initial size
417 EXPECT_LE(space->Size(), growth_limit);
418
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700419 // this invariant should always hold or else the space has grown to be larger than what the
Ian Rogers3bb17a62012-01-27 23:56:44 -0800420 // space believes its size is (which will break invariants)
421 EXPECT_GE(space->Size(), footprint);
422
423 // Fill the space with lots of small objects up to the growth limit
424 size_t max_objects = (growth_limit / (object_size > 0 ? object_size : 8)) + 1;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800425 UniquePtr<mirror::Object*[]> lots_of_objects(new mirror::Object*[max_objects]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800426 size_t last_object = 0; // last object for which allocation succeeded
427 size_t amount_allocated = 0; // amount of space allocated
Ian Rogers50b35e22012-10-04 10:09:15 -0700428 Thread* self = Thread::Current();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700429 size_t rand_seed = 123456789;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700430 for (size_t i = 0; i < max_objects; i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800431 size_t alloc_fails = 0; // number of failed allocations
432 size_t max_fails = 30; // number of times we fail allocation before giving up
433 for (; alloc_fails < max_fails; alloc_fails++) {
434 size_t alloc_size;
435 if (object_size > 0) {
436 alloc_size = object_size;
437 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700438 alloc_size = test_rand(&rand_seed) % static_cast<size_t>(-object_size);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700439 // Note the minimum size, which is the size of a zero-length byte array, is 12.
440 if (alloc_size < 12) {
441 alloc_size = 12;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800442 }
443 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800444 mirror::Object* object;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700445 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800446 if (round <= 1) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700447 object = space->Alloc(self, alloc_size, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800448 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700449 object = space->AllocWithGrowth(self, alloc_size, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800450 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700451 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800452 EXPECT_GE(space->Size(), footprint); // invariant
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700453 if (object != NULL) { // allocation succeeded
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700454 InstallClass(object, alloc_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800455 lots_of_objects.get()[i] = object;
456 size_t allocation_size = space->AllocationSize(object);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700457 EXPECT_EQ(bytes_allocated, allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800458 if (object_size > 0) {
459 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
460 } else {
461 EXPECT_GE(allocation_size, 8u);
462 }
463 amount_allocated += allocation_size;
464 break;
465 }
466 }
467 if (alloc_fails == max_fails) {
468 last_object = i;
469 break;
470 }
471 }
472 CHECK_NE(last_object, 0u); // we should have filled the space
473 EXPECT_GT(amount_allocated, 0u);
474
475 // We shouldn't have gone past the growth_limit
476 EXPECT_LE(amount_allocated, growth_limit);
477 EXPECT_LE(footprint, growth_limit);
478 EXPECT_LE(space->Size(), growth_limit);
479
480 // footprint and size should agree with amount allocated
481 EXPECT_GE(footprint, amount_allocated);
482 EXPECT_GE(space->Size(), amount_allocated);
483
484 // Release storage in a semi-adhoc manner
485 size_t free_increment = 96;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700486 while (true) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800487 // Give the space a haircut
488 space->Trim();
489
490 // Bounds sanity
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700491 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800492 EXPECT_LE(amount_allocated, growth_limit);
493 EXPECT_GE(footprint, amount_allocated);
494 EXPECT_LE(footprint, growth_limit);
495 EXPECT_GE(space->Size(), amount_allocated);
496 EXPECT_LE(space->Size(), growth_limit);
497
498 if (free_increment == 0) {
499 break;
500 }
501
Ian Rogersef7d42f2014-01-06 12:55:46 -0800502 {
503 // Free some objects
504 ScopedObjectAccess soa(self);
505 for (size_t i = 0; i < last_object; i += free_increment) {
506 mirror::Object* object = lots_of_objects.get()[i];
507 if (object == NULL) {
508 continue;
509 }
510 size_t allocation_size = space->AllocationSize(object);
511 if (object_size > 0) {
512 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
513 } else {
514 EXPECT_GE(allocation_size, 8u);
515 }
516 space->Free(self, object);
517 lots_of_objects.get()[i] = NULL;
518 amount_allocated -= allocation_size;
519 footprint = space->GetFootprint();
520 EXPECT_GE(space->Size(), footprint); // invariant
Ian Rogers3bb17a62012-01-27 23:56:44 -0800521 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800522
523 free_increment >>= 1;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800524 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800525 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700526 // The space has become empty here before allocating a large object
527 // below. For RosAlloc, revoke thread-local runs, which are kept
528 // even when empty for a performance reason, so that they won't
529 // cause the following large object allocation to fail due to
530 // potential fragmentation. Note they are normally revoked at each
531 // GC (but no GC here.)
532 space->RevokeAllThreadLocalBuffers();
533
Ian Rogers3bb17a62012-01-27 23:56:44 -0800534 // All memory was released, try a large allocation to check freed memory is being coalesced
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800535 mirror::Object* large_object;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800536 size_t three_quarters_space = (growth_limit / 2) + (growth_limit / 4);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700537 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800538 if (round <= 1) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700539 large_object = space->Alloc(self, three_quarters_space, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800540 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700541 large_object = space->AllocWithGrowth(self, three_quarters_space, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800542 }
543 EXPECT_TRUE(large_object != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700544 InstallClass(large_object, three_quarters_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800545
546 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700547 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800548 EXPECT_LE(footprint, growth_limit);
549 EXPECT_GE(space->Size(), footprint);
550 EXPECT_LE(space->Size(), growth_limit);
551
552 // Clean up
Ian Rogersef7d42f2014-01-06 12:55:46 -0800553 {
554 ScopedObjectAccess soa(self);
555 space->Free(self, large_object);
556 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800557 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700558 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800559 EXPECT_LE(footprint, growth_limit);
560 EXPECT_GE(space->Size(), footprint);
561 EXPECT_LE(space->Size(), growth_limit);
562}
563
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800564void SpaceTest::SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size, CreateSpaceFn create_space) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800565 size_t initial_size = 4 * MB;
566 size_t growth_limit = 8 * MB;
567 size_t capacity = 16 * MB;
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800568 MallocSpace* space(create_space("test", initial_size, growth_limit, capacity, NULL));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800569 ASSERT_TRUE(space != NULL);
570
571 // Basic sanity
572 EXPECT_EQ(space->Capacity(), growth_limit);
573 EXPECT_EQ(space->NonGrowthLimitCapacity(), capacity);
574
575 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700576 AddSpace(space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800577
578 // In this round we don't allocate with growth and therefore can't grow past the initial size.
579 // This effectively makes the growth_limit the initial_size, so assert this.
580 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 1, initial_size);
581 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 2, growth_limit);
582 // Remove growth limit
583 space->ClearGrowthLimit();
584 EXPECT_EQ(space->Capacity(), capacity);
585 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 3, capacity);
586}
587
588#define TEST_SizeFootPrintGrowthLimitAndTrim(name, size) \
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800589 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name##_DlMallocSpace) { \
590 SizeFootPrintGrowthLimitAndTrimDriver(size, SpaceTest::CreateDlMallocSpace); \
Ian Rogers3bb17a62012-01-27 23:56:44 -0800591 } \
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800592 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name##_DlMallocSpace) { \
593 SizeFootPrintGrowthLimitAndTrimDriver(-size, SpaceTest::CreateDlMallocSpace); \
594 } \
595 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name##_RosAllocSpace) { \
596 SizeFootPrintGrowthLimitAndTrimDriver(size, SpaceTest::CreateRosAllocSpace); \
597 } \
598 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name##_RosAllocSpace) { \
599 SizeFootPrintGrowthLimitAndTrimDriver(-size, SpaceTest::CreateRosAllocSpace); \
Ian Rogers3bb17a62012-01-27 23:56:44 -0800600 }
601
602// Each size test is its own test so that we get a fresh heap each time
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800603TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_12B_DlMallocSpace) {
604 SizeFootPrintGrowthLimitAndTrimDriver(12, SpaceTest::CreateDlMallocSpace);
605}
606TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_12B_RosAllocSpace) {
607 SizeFootPrintGrowthLimitAndTrimDriver(12, SpaceTest::CreateRosAllocSpace);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800608}
609TEST_SizeFootPrintGrowthLimitAndTrim(16B, 16)
610TEST_SizeFootPrintGrowthLimitAndTrim(24B, 24)
611TEST_SizeFootPrintGrowthLimitAndTrim(32B, 32)
612TEST_SizeFootPrintGrowthLimitAndTrim(64B, 64)
613TEST_SizeFootPrintGrowthLimitAndTrim(128B, 128)
614TEST_SizeFootPrintGrowthLimitAndTrim(1KB, 1 * KB)
615TEST_SizeFootPrintGrowthLimitAndTrim(4KB, 4 * KB)
616TEST_SizeFootPrintGrowthLimitAndTrim(1MB, 1 * MB)
617TEST_SizeFootPrintGrowthLimitAndTrim(4MB, 4 * MB)
618TEST_SizeFootPrintGrowthLimitAndTrim(8MB, 8 * MB)
619
Ian Rogers1d54e732013-05-02 21:10:01 -0700620} // namespace space
621} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700622} // namespace art