blob: 6d07a609f28d854e279203d7520142917d235a0a [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 {
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080042 // Note the minimum size, which is the size of a zero-length byte array.
43 EXPECT_GE(size, SizeOfZeroLengthByteArray());
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070044 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();
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080049 size_t header_size = SizeOfZeroLengthByteArray();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070050 int32_t length = size - header_size;
51 arr->SetLength(length);
52 EXPECT_EQ(arr->SizeOf(), size);
53 }
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080054
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080055 static size_t SizeOfZeroLengthByteArray() {
56 return mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimByte)).Uint32Value();
57 }
58
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080059 static MallocSpace* CreateDlMallocSpace(const std::string& name, size_t initial_size, size_t growth_limit,
60 size_t capacity, byte* requested_begin) {
61 return DlMallocSpace::Create(name, initial_size, growth_limit, capacity, requested_begin);
62 }
63 static MallocSpace* CreateRosAllocSpace(const std::string& name, size_t initial_size, size_t growth_limit,
64 size_t capacity, byte* requested_begin) {
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -080065 return RosAllocSpace::Create(name, initial_size, growth_limit, capacity, requested_begin,
66 Runtime::Current()->GetHeap()->IsLowMemoryMode());
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080067 }
68
69 typedef MallocSpace* (*CreateSpaceFn)(const std::string& name, size_t initial_size, size_t growth_limit,
70 size_t capacity, byte* requested_begin);
71 void InitTestBody(CreateSpaceFn create_space);
72 void ZygoteSpaceTestBody(CreateSpaceFn create_space);
73 void AllocAndFreeTestBody(CreateSpaceFn create_space);
74 void AllocAndFreeListTestBody(CreateSpaceFn create_space);
75
76 void SizeFootPrintGrowthLimitAndTrimBody(MallocSpace* space, intptr_t object_size,
77 int round, size_t growth_limit);
78 void SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size, CreateSpaceFn create_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -080079};
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070080
Mathieu Chartiereb5710e2013-07-25 15:19:42 -070081static size_t test_rand(size_t* seed) {
82 *seed = *seed * 1103515245 + 12345;
83 return *seed;
84}
85
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080086void SpaceTest::InitTestBody(CreateSpaceFn create_space) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070087 {
jeffhaoc1160702011-10-27 15:48:45 -070088 // Init < max == growth
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080089 UniquePtr<Space> space(create_space("test", 16 * MB, 32 * MB, 32 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070090 EXPECT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070091 }
92 {
jeffhaoc1160702011-10-27 15:48:45 -070093 // Init == max == growth
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080094 UniquePtr<Space> space(create_space("test", 16 * MB, 16 * MB, 16 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070095 EXPECT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070096 }
97 {
jeffhaoc1160702011-10-27 15:48:45 -070098 // Init > max == growth
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080099 UniquePtr<Space> space(create_space("test", 32 * MB, 16 * MB, 16 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -0700100 EXPECT_TRUE(space.get() == NULL);
101 }
102 {
103 // Growth == init < max
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800104 UniquePtr<Space> space(create_space("test", 16 * MB, 16 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -0700105 EXPECT_TRUE(space.get() != NULL);
106 }
107 {
108 // Growth < init < max
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800109 UniquePtr<Space> space(create_space("test", 16 * MB, 8 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -0700110 EXPECT_TRUE(space.get() == NULL);
111 }
112 {
113 // Init < growth < max
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800114 UniquePtr<Space> space(create_space("test", 8 * MB, 16 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -0700115 EXPECT_TRUE(space.get() != NULL);
116 }
117 {
118 // Init < max < growth
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800119 UniquePtr<Space> space(create_space("test", 8 * MB, 32 * MB, 16 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -0700120 EXPECT_TRUE(space.get() == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700121 }
122}
123
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800124TEST_F(SpaceTest, Init_DlMallocSpace) {
125 InitTestBody(SpaceTest::CreateDlMallocSpace);
126}
127TEST_F(SpaceTest, Init_RosAllocSpace) {
128 InitTestBody(SpaceTest::CreateRosAllocSpace);
129}
130
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700131// TODO: This test is not very good, we should improve it.
132// The test should do more allocations before the creation of the ZygoteSpace, and then do
133// allocations after the ZygoteSpace is created. The test should also do some GCs to ensure that
134// the GC works with the ZygoteSpace.
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800135void SpaceTest::ZygoteSpaceTestBody(CreateSpaceFn create_space) {
136 size_t dummy = 0;
137 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, NULL));
138 ASSERT_TRUE(space != NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700139
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800140 // Make space findable to the heap, will also delete space when runtime is cleaned up
141 AddSpace(space);
142 Thread* self = Thread::Current();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700143
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800144 // Succeeds, fits without adjusting the footprint limit.
145 mirror::Object* ptr1 = space->Alloc(self, 1 * MB, &dummy);
146 EXPECT_TRUE(ptr1 != NULL);
147 InstallClass(ptr1, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700148
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800149 // Fails, requires a higher footprint limit.
150 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy);
151 EXPECT_TRUE(ptr2 == NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700152
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800153 // Succeeds, adjusts the footprint.
154 size_t ptr3_bytes_allocated;
155 mirror::Object* ptr3 = space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated);
156 EXPECT_TRUE(ptr3 != NULL);
157 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
158 InstallClass(ptr3, 8 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700159
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800160 // Fails, requires a higher footprint limit.
161 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy);
162 EXPECT_TRUE(ptr4 == NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700163
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800164 // Also fails, requires a higher allowed footprint.
165 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy);
166 EXPECT_TRUE(ptr5 == NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700167
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800168 // Release some memory.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800169 ScopedObjectAccess soa(self);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800170 size_t free3 = space->AllocationSize(ptr3);
171 EXPECT_EQ(free3, ptr3_bytes_allocated);
172 EXPECT_EQ(free3, space->Free(self, ptr3));
173 EXPECT_LE(8U * MB, free3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700174
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800175 // Succeeds, now that memory has been freed.
176 mirror::Object* ptr6 = space->AllocWithGrowth(self, 9 * MB, &dummy);
177 EXPECT_TRUE(ptr6 != NULL);
178 InstallClass(ptr6, 9 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700179
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800180 // Final clean up.
181 size_t free1 = space->AllocationSize(ptr1);
182 space->Free(self, ptr1);
183 EXPECT_LE(1U * MB, free1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700184
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800185 // Make sure that the zygote space isn't directly at the start of the space.
186 space->Alloc(self, 1U * MB, &dummy);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800187
188 gc::Heap* heap = Runtime::Current()->GetHeap();
189 space::Space* old_space = space;
190 heap->RemoveSpace(old_space);
191 space::ZygoteSpace* zygote_space = space->CreateZygoteSpace("alloc space",
192 heap->IsLowMemoryMode(),
193 &space);
194 delete old_space;
195 // Add the zygote space.
196 AddSpace(zygote_space);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700197
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800198 // Make space findable to the heap, will also delete space when runtime is cleaned up
199 AddSpace(space);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700200
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800201 // Succeeds, fits without adjusting the footprint limit.
202 ptr1 = space->Alloc(self, 1 * MB, &dummy);
203 EXPECT_TRUE(ptr1 != NULL);
204 InstallClass(ptr1, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700205
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800206 // Fails, requires a higher footprint limit.
207 ptr2 = space->Alloc(self, 8 * MB, &dummy);
208 EXPECT_TRUE(ptr2 == NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700209
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800210 // Succeeds, adjusts the footprint.
211 ptr3 = space->AllocWithGrowth(self, 2 * MB, &dummy);
212 EXPECT_TRUE(ptr3 != NULL);
213 InstallClass(ptr3, 2 * MB);
214 space->Free(self, ptr3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700215
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800216 // Final clean up.
217 free1 = space->AllocationSize(ptr1);
218 space->Free(self, ptr1);
219 EXPECT_LE(1U * MB, free1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700220}
221
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800222TEST_F(SpaceTest, ZygoteSpace_DlMallocSpace) {
223 ZygoteSpaceTestBody(SpaceTest::CreateDlMallocSpace);
224}
225
226TEST_F(SpaceTest, ZygoteSpace_RosAllocSpace) {
227 ZygoteSpaceTestBody(SpaceTest::CreateRosAllocSpace);
228}
229
230void SpaceTest::AllocAndFreeTestBody(CreateSpaceFn create_space) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700231 size_t dummy = 0;
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800232 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Ian Rogers30fab402012-01-23 15:43:46 -0800233 ASSERT_TRUE(space != NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -0700234 Thread* self = Thread::Current();
Ian Rogers30fab402012-01-23 15:43:46 -0800235
Ian Rogers3bb17a62012-01-27 23:56:44 -0800236 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700237 AddSpace(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700238
Ian Rogers3bb17a62012-01-27 23:56:44 -0800239 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700240 mirror::Object* ptr1 = space->Alloc(self, 1 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700241 EXPECT_TRUE(ptr1 != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700242 InstallClass(ptr1, 1 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700243
Ian Rogers3bb17a62012-01-27 23:56:44 -0800244 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700245 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700246 EXPECT_TRUE(ptr2 == NULL);
247
248 // Succeeds, adjusts the footprint.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700249 size_t ptr3_bytes_allocated;
250 mirror::Object* ptr3 = space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700251 EXPECT_TRUE(ptr3 != NULL);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700252 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700253 InstallClass(ptr3, 8 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700254
Ian Rogers3bb17a62012-01-27 23:56:44 -0800255 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700256 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800257 EXPECT_TRUE(ptr4 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700258
259 // Also fails, requires a higher allowed footprint.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700260 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800261 EXPECT_TRUE(ptr5 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700262
263 // Release some memory.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800264 ScopedObjectAccess soa(self);
Ian Rogers30fab402012-01-23 15:43:46 -0800265 size_t free3 = space->AllocationSize(ptr3);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700266 EXPECT_EQ(free3, ptr3_bytes_allocated);
Ian Rogers50b35e22012-10-04 10:09:15 -0700267 space->Free(self, ptr3);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700268 EXPECT_LE(8U * MB, free3);
269
270 // Succeeds, now that memory has been freed.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700271 mirror::Object* ptr6 = space->AllocWithGrowth(self, 9 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700272 EXPECT_TRUE(ptr6 != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700273 InstallClass(ptr6, 9 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700274
275 // Final clean up.
Ian Rogers30fab402012-01-23 15:43:46 -0800276 size_t free1 = space->AllocationSize(ptr1);
Ian Rogers50b35e22012-10-04 10:09:15 -0700277 space->Free(self, ptr1);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700278 EXPECT_LE(1U * MB, free1);
279}
280
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800281TEST_F(SpaceTest, AllocAndFree_DlMallocSpace) {
282 AllocAndFreeTestBody(SpaceTest::CreateDlMallocSpace);
283}
284TEST_F(SpaceTest, AllocAndFree_RosAllocSpace) {
285 AllocAndFreeTestBody(SpaceTest::CreateRosAllocSpace);
286}
287
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700288TEST_F(SpaceTest, LargeObjectTest) {
289 size_t rand_seed = 0;
290 for (size_t i = 0; i < 2; ++i) {
291 LargeObjectSpace* los = NULL;
292 if (i == 0) {
293 los = space::LargeObjectMapSpace::Create("large object space");
294 } else {
295 los = space::FreeListSpace::Create("large object space", NULL, 128 * MB);
296 }
297
298 static const size_t num_allocations = 64;
299 static const size_t max_allocation_size = 0x100000;
300 std::vector<std::pair<mirror::Object*, size_t> > requests;
301
302 for (size_t phase = 0; phase < 2; ++phase) {
303 while (requests.size() < num_allocations) {
304 size_t request_size = test_rand(&rand_seed) % max_allocation_size;
305 size_t allocation_size = 0;
306 mirror::Object* obj = los->Alloc(Thread::Current(), request_size, &allocation_size);
307 ASSERT_TRUE(obj != NULL);
308 ASSERT_EQ(allocation_size, los->AllocationSize(obj));
309 ASSERT_GE(allocation_size, request_size);
310 // Fill in our magic value.
311 byte magic = (request_size & 0xFF) | 1;
312 memset(obj, magic, request_size);
313 requests.push_back(std::make_pair(obj, request_size));
314 }
315
316 // "Randomly" shuffle the requests.
317 for (size_t k = 0; k < 10; ++k) {
318 for (size_t j = 0; j < requests.size(); ++j) {
319 std::swap(requests[j], requests[test_rand(&rand_seed) % requests.size()]);
320 }
321 }
322
323 // Free 1 / 2 the allocations the first phase, and all the second phase.
324 size_t limit = !phase ? requests.size() / 2 : 0;
325 while (requests.size() > limit) {
326 mirror::Object* obj = requests.back().first;
327 size_t request_size = requests.back().second;
328 requests.pop_back();
329 byte magic = (request_size & 0xFF) | 1;
330 for (size_t k = 0; k < request_size; ++k) {
331 ASSERT_EQ(reinterpret_cast<const byte*>(obj)[k], magic);
332 }
333 ASSERT_GE(los->Free(Thread::Current(), obj), request_size);
334 }
335 }
336
337 size_t bytes_allocated = 0;
338 // Checks that the coalescing works.
339 mirror::Object* obj = los->Alloc(Thread::Current(), 100 * MB, &bytes_allocated);
340 EXPECT_TRUE(obj != NULL);
341 los->Free(Thread::Current(), obj);
342
343 EXPECT_EQ(0U, los->GetBytesAllocated());
344 EXPECT_EQ(0U, los->GetObjectsAllocated());
345 delete los;
346 }
347}
348
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800349void SpaceTest::AllocAndFreeListTestBody(CreateSpaceFn create_space) {
350 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800351 ASSERT_TRUE(space != NULL);
352
353 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700354 AddSpace(space);
Ian Rogers50b35e22012-10-04 10:09:15 -0700355 Thread* self = Thread::Current();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800356
357 // Succeeds, fits without adjusting the max allowed footprint.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800358 mirror::Object* lots_of_objects[1024];
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700359 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700360 size_t allocation_size = 0;
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800361 size_t size_of_zero_length_byte_array = SizeOfZeroLengthByteArray();
362 lots_of_objects[i] = space->Alloc(self, size_of_zero_length_byte_array, &allocation_size);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800363 EXPECT_TRUE(lots_of_objects[i] != nullptr);
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800364 InstallClass(lots_of_objects[i], size_of_zero_length_byte_array);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700365 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i]));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800366 }
367
Ian Rogersef7d42f2014-01-06 12:55:46 -0800368 // Release memory and check pointers are NULL.
369 {
370 ScopedObjectAccess soa(self);
371 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
372 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
373 EXPECT_TRUE(lots_of_objects[i] == nullptr);
374 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800375 }
376
377 // Succeeds, fits by adjusting the max allowed footprint.
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700378 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700379 size_t allocation_size = 0;
380 lots_of_objects[i] = space->AllocWithGrowth(self, 1024, &allocation_size);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800381 EXPECT_TRUE(lots_of_objects[i] != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700382 InstallClass(lots_of_objects[i], 1024);
383 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i]));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800384 }
385
386 // Release memory and check pointers are NULL
Ian Rogersef7d42f2014-01-06 12:55:46 -0800387 {
388 ScopedObjectAccess soa(self);
389 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
390 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
391 EXPECT_TRUE(lots_of_objects[i] == nullptr);
392 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800393 }
394}
395
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800396TEST_F(SpaceTest, AllocAndFreeList_DlMallocSpace) {
397 AllocAndFreeListTestBody(SpaceTest::CreateDlMallocSpace);
398}
399TEST_F(SpaceTest, AllocAndFreeList_RosAllocSpace) {
400 AllocAndFreeListTestBody(SpaceTest::CreateRosAllocSpace);
401}
402
403void SpaceTest::SizeFootPrintGrowthLimitAndTrimBody(MallocSpace* space, intptr_t object_size,
Ian Rogers3bb17a62012-01-27 23:56:44 -0800404 int round, size_t growth_limit) {
405 if (((object_size > 0 && object_size >= static_cast<intptr_t>(growth_limit))) ||
406 ((object_size < 0 && -object_size >= static_cast<intptr_t>(growth_limit)))) {
407 // No allocation can succeed
408 return;
409 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800410
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700411 // The space's footprint equals amount of resources requested from system
412 size_t footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800413
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700414 // The space must at least have its book keeping allocated
Ian Rogers3bb17a62012-01-27 23:56:44 -0800415 EXPECT_GT(footprint, 0u);
416
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700417 // But it shouldn't exceed the initial size
Ian Rogers3bb17a62012-01-27 23:56:44 -0800418 EXPECT_LE(footprint, growth_limit);
419
420 // space's size shouldn't exceed the initial size
421 EXPECT_LE(space->Size(), growth_limit);
422
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700423 // this invariant should always hold or else the space has grown to be larger than what the
Ian Rogers3bb17a62012-01-27 23:56:44 -0800424 // space believes its size is (which will break invariants)
425 EXPECT_GE(space->Size(), footprint);
426
427 // Fill the space with lots of small objects up to the growth limit
428 size_t max_objects = (growth_limit / (object_size > 0 ? object_size : 8)) + 1;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800429 UniquePtr<mirror::Object*[]> lots_of_objects(new mirror::Object*[max_objects]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800430 size_t last_object = 0; // last object for which allocation succeeded
431 size_t amount_allocated = 0; // amount of space allocated
Ian Rogers50b35e22012-10-04 10:09:15 -0700432 Thread* self = Thread::Current();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700433 size_t rand_seed = 123456789;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700434 for (size_t i = 0; i < max_objects; i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800435 size_t alloc_fails = 0; // number of failed allocations
436 size_t max_fails = 30; // number of times we fail allocation before giving up
437 for (; alloc_fails < max_fails; alloc_fails++) {
438 size_t alloc_size;
439 if (object_size > 0) {
440 alloc_size = object_size;
441 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700442 alloc_size = test_rand(&rand_seed) % static_cast<size_t>(-object_size);
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800443 // Note the minimum size, which is the size of a zero-length byte array.
444 size_t size_of_zero_length_byte_array = SizeOfZeroLengthByteArray();
445 if (alloc_size < size_of_zero_length_byte_array) {
446 alloc_size = size_of_zero_length_byte_array;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800447 }
448 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800449 mirror::Object* object;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700450 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800451 if (round <= 1) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700452 object = space->Alloc(self, alloc_size, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800453 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700454 object = space->AllocWithGrowth(self, alloc_size, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800455 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700456 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800457 EXPECT_GE(space->Size(), footprint); // invariant
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700458 if (object != NULL) { // allocation succeeded
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700459 InstallClass(object, alloc_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800460 lots_of_objects.get()[i] = object;
461 size_t allocation_size = space->AllocationSize(object);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700462 EXPECT_EQ(bytes_allocated, allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800463 if (object_size > 0) {
464 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
465 } else {
466 EXPECT_GE(allocation_size, 8u);
467 }
468 amount_allocated += allocation_size;
469 break;
470 }
471 }
472 if (alloc_fails == max_fails) {
473 last_object = i;
474 break;
475 }
476 }
477 CHECK_NE(last_object, 0u); // we should have filled the space
478 EXPECT_GT(amount_allocated, 0u);
479
480 // We shouldn't have gone past the growth_limit
481 EXPECT_LE(amount_allocated, growth_limit);
482 EXPECT_LE(footprint, growth_limit);
483 EXPECT_LE(space->Size(), growth_limit);
484
485 // footprint and size should agree with amount allocated
486 EXPECT_GE(footprint, amount_allocated);
487 EXPECT_GE(space->Size(), amount_allocated);
488
489 // Release storage in a semi-adhoc manner
490 size_t free_increment = 96;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700491 while (true) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800492 // Give the space a haircut
493 space->Trim();
494
495 // Bounds sanity
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700496 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800497 EXPECT_LE(amount_allocated, growth_limit);
498 EXPECT_GE(footprint, amount_allocated);
499 EXPECT_LE(footprint, growth_limit);
500 EXPECT_GE(space->Size(), amount_allocated);
501 EXPECT_LE(space->Size(), growth_limit);
502
503 if (free_increment == 0) {
504 break;
505 }
506
Ian Rogersef7d42f2014-01-06 12:55:46 -0800507 {
508 // Free some objects
509 ScopedObjectAccess soa(self);
510 for (size_t i = 0; i < last_object; i += free_increment) {
511 mirror::Object* object = lots_of_objects.get()[i];
512 if (object == NULL) {
513 continue;
514 }
515 size_t allocation_size = space->AllocationSize(object);
516 if (object_size > 0) {
517 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
518 } else {
519 EXPECT_GE(allocation_size, 8u);
520 }
521 space->Free(self, object);
522 lots_of_objects.get()[i] = NULL;
523 amount_allocated -= allocation_size;
524 footprint = space->GetFootprint();
525 EXPECT_GE(space->Size(), footprint); // invariant
Ian Rogers3bb17a62012-01-27 23:56:44 -0800526 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800527
528 free_increment >>= 1;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800529 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800530 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700531 // The space has become empty here before allocating a large object
532 // below. For RosAlloc, revoke thread-local runs, which are kept
533 // even when empty for a performance reason, so that they won't
534 // cause the following large object allocation to fail due to
535 // potential fragmentation. Note they are normally revoked at each
536 // GC (but no GC here.)
537 space->RevokeAllThreadLocalBuffers();
538
Ian Rogers3bb17a62012-01-27 23:56:44 -0800539 // All memory was released, try a large allocation to check freed memory is being coalesced
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800540 mirror::Object* large_object;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800541 size_t three_quarters_space = (growth_limit / 2) + (growth_limit / 4);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700542 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800543 if (round <= 1) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700544 large_object = space->Alloc(self, three_quarters_space, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800545 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700546 large_object = space->AllocWithGrowth(self, three_quarters_space, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800547 }
548 EXPECT_TRUE(large_object != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700549 InstallClass(large_object, three_quarters_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800550
551 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700552 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800553 EXPECT_LE(footprint, growth_limit);
554 EXPECT_GE(space->Size(), footprint);
555 EXPECT_LE(space->Size(), growth_limit);
556
557 // Clean up
Ian Rogersef7d42f2014-01-06 12:55:46 -0800558 {
559 ScopedObjectAccess soa(self);
560 space->Free(self, large_object);
561 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800562 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700563 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800564 EXPECT_LE(footprint, growth_limit);
565 EXPECT_GE(space->Size(), footprint);
566 EXPECT_LE(space->Size(), growth_limit);
567}
568
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800569void SpaceTest::SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size, CreateSpaceFn create_space) {
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800570 if (object_size < SizeOfZeroLengthByteArray()) {
571 // Too small for the object layout/model.
572 return;
573 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800574 size_t initial_size = 4 * MB;
575 size_t growth_limit = 8 * MB;
576 size_t capacity = 16 * MB;
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800577 MallocSpace* space(create_space("test", initial_size, growth_limit, capacity, NULL));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800578 ASSERT_TRUE(space != NULL);
579
580 // Basic sanity
581 EXPECT_EQ(space->Capacity(), growth_limit);
582 EXPECT_EQ(space->NonGrowthLimitCapacity(), capacity);
583
584 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700585 AddSpace(space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800586
587 // In this round we don't allocate with growth and therefore can't grow past the initial size.
588 // This effectively makes the growth_limit the initial_size, so assert this.
589 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 1, initial_size);
590 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 2, growth_limit);
591 // Remove growth limit
592 space->ClearGrowthLimit();
593 EXPECT_EQ(space->Capacity(), capacity);
594 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 3, capacity);
595}
596
597#define TEST_SizeFootPrintGrowthLimitAndTrim(name, size) \
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800598 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name##_DlMallocSpace) { \
599 SizeFootPrintGrowthLimitAndTrimDriver(size, SpaceTest::CreateDlMallocSpace); \
Ian Rogers3bb17a62012-01-27 23:56:44 -0800600 } \
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800601 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name##_DlMallocSpace) { \
602 SizeFootPrintGrowthLimitAndTrimDriver(-size, SpaceTest::CreateDlMallocSpace); \
603 } \
604 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name##_RosAllocSpace) { \
605 SizeFootPrintGrowthLimitAndTrimDriver(size, SpaceTest::CreateRosAllocSpace); \
606 } \
607 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name##_RosAllocSpace) { \
608 SizeFootPrintGrowthLimitAndTrimDriver(-size, SpaceTest::CreateRosAllocSpace); \
Ian Rogers3bb17a62012-01-27 23:56:44 -0800609 }
610
611// Each size test is its own test so that we get a fresh heap each time
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800612TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_12B_DlMallocSpace) {
613 SizeFootPrintGrowthLimitAndTrimDriver(12, SpaceTest::CreateDlMallocSpace);
614}
615TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_12B_RosAllocSpace) {
616 SizeFootPrintGrowthLimitAndTrimDriver(12, SpaceTest::CreateRosAllocSpace);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800617}
618TEST_SizeFootPrintGrowthLimitAndTrim(16B, 16)
619TEST_SizeFootPrintGrowthLimitAndTrim(24B, 24)
620TEST_SizeFootPrintGrowthLimitAndTrim(32B, 32)
621TEST_SizeFootPrintGrowthLimitAndTrim(64B, 64)
622TEST_SizeFootPrintGrowthLimitAndTrim(128B, 128)
623TEST_SizeFootPrintGrowthLimitAndTrim(1KB, 1 * KB)
624TEST_SizeFootPrintGrowthLimitAndTrim(4KB, 4 * KB)
625TEST_SizeFootPrintGrowthLimitAndTrim(1MB, 1 * MB)
626TEST_SizeFootPrintGrowthLimitAndTrim(4MB, 4 * MB)
627TEST_SizeFootPrintGrowthLimitAndTrim(8MB, 8 * MB)
628
Ian Rogers1d54e732013-05-02 21:10:01 -0700629} // namespace space
630} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700631} // namespace art