blob: 60c3b1c08c316bfc64e20dd00a3b51c2f52b9c14 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro69759ea2011-07-21 18:13:35 -070016
Ian Rogers1d54e732013-05-02 21:10:01 -070017#include "dlmalloc_space.h"
Mathieu Chartiereb5710e2013-07-25 15:19:42 -070018#include "large_object_space.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070019
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070020#include "common_test.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070021#include "globals.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070022#include "UniquePtr.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070023#include "mirror/array-inl.h"
24#include "mirror/object-inl.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070025
Ian Rogers3bb17a62012-01-27 23:56:44 -080026#include <stdint.h>
27
Carl Shapiro69759ea2011-07-21 18:13:35 -070028namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070029namespace gc {
30namespace space {
Carl Shapiro69759ea2011-07-21 18:13:35 -070031
Ian Rogers3bb17a62012-01-27 23:56:44 -080032class SpaceTest : public CommonTest {
33 public:
Mathieu Chartier590fee92013-09-13 13:46:47 -070034 void AddSpace(ContinuousSpace* space) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070035 // For RosAlloc, revoke the thread local runs before moving onto a
36 // new alloc space.
37 Runtime::Current()->GetHeap()->RevokeAllThreadLocalBuffers();
Mathieu Chartier590fee92013-09-13 13:46:47 -070038 Runtime::Current()->GetHeap()->AddSpace(space);
Ian Rogers1d54e732013-05-02 21:10:01 -070039 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070040 void InstallClass(mirror::Object* o, size_t size) NO_THREAD_SAFETY_ANALYSIS {
41 // Note the minimum size, which is the size of a zero-length byte array, is 12.
42 EXPECT_GE(size, static_cast<size_t>(12));
43 SirtRef<mirror::ClassLoader> null_loader(Thread::Current(), NULL);
44 mirror::Class* byte_array_class = Runtime::Current()->GetClassLinker()->FindClass("[B", null_loader);
45 EXPECT_TRUE(byte_array_class != NULL);
46 o->SetClass(byte_array_class);
47 mirror::Array* arr = o->AsArray();
48 // size_t header_size = sizeof(mirror::Object) + 4;
49 size_t header_size = arr->DataOffset(1).Uint32Value();
50 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
55 static MallocSpace* CreateDlMallocSpace(const std::string& name, size_t initial_size, size_t growth_limit,
56 size_t capacity, byte* requested_begin) {
57 return DlMallocSpace::Create(name, initial_size, growth_limit, capacity, requested_begin);
58 }
59 static MallocSpace* CreateRosAllocSpace(const std::string& name, size_t initial_size, size_t growth_limit,
60 size_t capacity, byte* requested_begin) {
61 return RosAllocSpace::Create(name, initial_size, growth_limit, capacity, requested_begin);
62 }
63
64 typedef MallocSpace* (*CreateSpaceFn)(const std::string& name, size_t initial_size, size_t growth_limit,
65 size_t capacity, byte* requested_begin);
66 void InitTestBody(CreateSpaceFn create_space);
67 void ZygoteSpaceTestBody(CreateSpaceFn create_space);
68 void AllocAndFreeTestBody(CreateSpaceFn create_space);
69 void AllocAndFreeListTestBody(CreateSpaceFn create_space);
70
71 void SizeFootPrintGrowthLimitAndTrimBody(MallocSpace* space, intptr_t object_size,
72 int round, size_t growth_limit);
73 void SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size, CreateSpaceFn create_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -080074};
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070075
Mathieu Chartiereb5710e2013-07-25 15:19:42 -070076static size_t test_rand(size_t* seed) {
77 *seed = *seed * 1103515245 + 12345;
78 return *seed;
79}
80
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080081void SpaceTest::InitTestBody(CreateSpaceFn create_space) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070082 {
jeffhaoc1160702011-10-27 15:48:45 -070083 // Init < max == growth
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080084 UniquePtr<Space> space(create_space("test", 16 * MB, 32 * MB, 32 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070085 EXPECT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070086 }
87 {
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, 16 * MB, 16 * 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", 32 * MB, 16 * MB, 16 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070095 EXPECT_TRUE(space.get() == NULL);
96 }
97 {
98 // Growth == init < max
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080099 UniquePtr<Space> space(create_space("test", 16 * MB, 16 * MB, 32 * 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, 8 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -0700105 EXPECT_TRUE(space.get() == NULL);
106 }
107 {
108 // Init < growth < max
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800109 UniquePtr<Space> space(create_space("test", 8 * MB, 16 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -0700110 EXPECT_TRUE(space.get() != NULL);
111 }
112 {
113 // Init < max < growth
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800114 UniquePtr<Space> space(create_space("test", 8 * MB, 32 * MB, 16 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -0700115 EXPECT_TRUE(space.get() == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700116 }
117}
118
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800119TEST_F(SpaceTest, Init_DlMallocSpace) {
120 InitTestBody(SpaceTest::CreateDlMallocSpace);
121}
122TEST_F(SpaceTest, Init_RosAllocSpace) {
123 InitTestBody(SpaceTest::CreateRosAllocSpace);
124}
125
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700126// TODO: This test is not very good, we should improve it.
127// The test should do more allocations before the creation of the ZygoteSpace, and then do
128// allocations after the ZygoteSpace is created. The test should also do some GCs to ensure that
129// the GC works with the ZygoteSpace.
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800130void SpaceTest::ZygoteSpaceTestBody(CreateSpaceFn create_space) {
131 size_t dummy = 0;
132 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, NULL));
133 ASSERT_TRUE(space != NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700134
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800135 // Make space findable to the heap, will also delete space when runtime is cleaned up
136 AddSpace(space);
137 Thread* self = Thread::Current();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700138
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800139 // Succeeds, fits without adjusting the footprint limit.
140 mirror::Object* ptr1 = space->Alloc(self, 1 * MB, &dummy);
141 EXPECT_TRUE(ptr1 != NULL);
142 InstallClass(ptr1, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700143
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800144 // Fails, requires a higher footprint limit.
145 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy);
146 EXPECT_TRUE(ptr2 == NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700147
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800148 // Succeeds, adjusts the footprint.
149 size_t ptr3_bytes_allocated;
150 mirror::Object* ptr3 = space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated);
151 EXPECT_TRUE(ptr3 != NULL);
152 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
153 InstallClass(ptr3, 8 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700154
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800155 // Fails, requires a higher footprint limit.
156 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy);
157 EXPECT_TRUE(ptr4 == NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700158
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800159 // Also fails, requires a higher allowed footprint.
160 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy);
161 EXPECT_TRUE(ptr5 == NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700162
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800163 // Release some memory.
164 size_t free3 = space->AllocationSize(ptr3);
165 EXPECT_EQ(free3, ptr3_bytes_allocated);
166 EXPECT_EQ(free3, space->Free(self, ptr3));
167 EXPECT_LE(8U * MB, free3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700168
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800169 // Succeeds, now that memory has been freed.
170 mirror::Object* ptr6 = space->AllocWithGrowth(self, 9 * MB, &dummy);
171 EXPECT_TRUE(ptr6 != NULL);
172 InstallClass(ptr6, 9 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700173
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800174 // Final clean up.
175 size_t free1 = space->AllocationSize(ptr1);
176 space->Free(self, ptr1);
177 EXPECT_LE(1U * MB, free1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700178
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800179 // Make sure that the zygote space isn't directly at the start of the space.
180 space->Alloc(self, 1U * MB, &dummy);
181 space = space->CreateZygoteSpace("alloc space");
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700182
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800183 // Make space findable to the heap, will also delete space when runtime is cleaned up
184 AddSpace(space);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700185
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800186 // Succeeds, fits without adjusting the footprint limit.
187 ptr1 = space->Alloc(self, 1 * MB, &dummy);
188 EXPECT_TRUE(ptr1 != NULL);
189 InstallClass(ptr1, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700190
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800191 // Fails, requires a higher footprint limit.
192 ptr2 = space->Alloc(self, 8 * MB, &dummy);
193 EXPECT_TRUE(ptr2 == NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700194
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800195 // Succeeds, adjusts the footprint.
196 ptr3 = space->AllocWithGrowth(self, 2 * MB, &dummy);
197 EXPECT_TRUE(ptr3 != NULL);
198 InstallClass(ptr3, 2 * MB);
199 space->Free(self, ptr3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700200
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800201 // Final clean up.
202 free1 = space->AllocationSize(ptr1);
203 space->Free(self, ptr1);
204 EXPECT_LE(1U * MB, free1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700205}
206
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800207TEST_F(SpaceTest, ZygoteSpace_DlMallocSpace) {
208 ZygoteSpaceTestBody(SpaceTest::CreateDlMallocSpace);
209}
210
211TEST_F(SpaceTest, ZygoteSpace_RosAllocSpace) {
212 ZygoteSpaceTestBody(SpaceTest::CreateRosAllocSpace);
213}
214
215void SpaceTest::AllocAndFreeTestBody(CreateSpaceFn create_space) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700216 size_t dummy = 0;
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800217 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Ian Rogers30fab402012-01-23 15:43:46 -0800218 ASSERT_TRUE(space != NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -0700219 Thread* self = Thread::Current();
Ian Rogers30fab402012-01-23 15:43:46 -0800220
Ian Rogers3bb17a62012-01-27 23:56:44 -0800221 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700222 AddSpace(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700223
Ian Rogers3bb17a62012-01-27 23:56:44 -0800224 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700225 mirror::Object* ptr1 = space->Alloc(self, 1 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700226 EXPECT_TRUE(ptr1 != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700227 InstallClass(ptr1, 1 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700228
Ian Rogers3bb17a62012-01-27 23:56:44 -0800229 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700230 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700231 EXPECT_TRUE(ptr2 == NULL);
232
233 // Succeeds, adjusts the footprint.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700234 size_t ptr3_bytes_allocated;
235 mirror::Object* ptr3 = space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700236 EXPECT_TRUE(ptr3 != NULL);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700237 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700238 InstallClass(ptr3, 8 * 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* ptr4 = space->Alloc(self, 8 * MB, &dummy);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800242 EXPECT_TRUE(ptr4 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700243
244 // Also fails, requires a higher allowed footprint.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700245 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800246 EXPECT_TRUE(ptr5 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700247
248 // Release some memory.
Ian Rogers30fab402012-01-23 15:43:46 -0800249 size_t free3 = space->AllocationSize(ptr3);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700250 EXPECT_EQ(free3, ptr3_bytes_allocated);
Ian Rogers50b35e22012-10-04 10:09:15 -0700251 space->Free(self, ptr3);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700252 EXPECT_LE(8U * MB, free3);
253
254 // Succeeds, now that memory has been freed.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700255 mirror::Object* ptr6 = space->AllocWithGrowth(self, 9 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700256 EXPECT_TRUE(ptr6 != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700257 InstallClass(ptr6, 9 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700258
259 // Final clean up.
Ian Rogers30fab402012-01-23 15:43:46 -0800260 size_t free1 = space->AllocationSize(ptr1);
Ian Rogers50b35e22012-10-04 10:09:15 -0700261 space->Free(self, ptr1);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700262 EXPECT_LE(1U * MB, free1);
263}
264
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800265TEST_F(SpaceTest, AllocAndFree_DlMallocSpace) {
266 AllocAndFreeTestBody(SpaceTest::CreateDlMallocSpace);
267}
268TEST_F(SpaceTest, AllocAndFree_RosAllocSpace) {
269 AllocAndFreeTestBody(SpaceTest::CreateRosAllocSpace);
270}
271
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700272TEST_F(SpaceTest, LargeObjectTest) {
273 size_t rand_seed = 0;
274 for (size_t i = 0; i < 2; ++i) {
275 LargeObjectSpace* los = NULL;
276 if (i == 0) {
277 los = space::LargeObjectMapSpace::Create("large object space");
278 } else {
279 los = space::FreeListSpace::Create("large object space", NULL, 128 * MB);
280 }
281
282 static const size_t num_allocations = 64;
283 static const size_t max_allocation_size = 0x100000;
284 std::vector<std::pair<mirror::Object*, size_t> > requests;
285
286 for (size_t phase = 0; phase < 2; ++phase) {
287 while (requests.size() < num_allocations) {
288 size_t request_size = test_rand(&rand_seed) % max_allocation_size;
289 size_t allocation_size = 0;
290 mirror::Object* obj = los->Alloc(Thread::Current(), request_size, &allocation_size);
291 ASSERT_TRUE(obj != NULL);
292 ASSERT_EQ(allocation_size, los->AllocationSize(obj));
293 ASSERT_GE(allocation_size, request_size);
294 // Fill in our magic value.
295 byte magic = (request_size & 0xFF) | 1;
296 memset(obj, magic, request_size);
297 requests.push_back(std::make_pair(obj, request_size));
298 }
299
300 // "Randomly" shuffle the requests.
301 for (size_t k = 0; k < 10; ++k) {
302 for (size_t j = 0; j < requests.size(); ++j) {
303 std::swap(requests[j], requests[test_rand(&rand_seed) % requests.size()]);
304 }
305 }
306
307 // Free 1 / 2 the allocations the first phase, and all the second phase.
308 size_t limit = !phase ? requests.size() / 2 : 0;
309 while (requests.size() > limit) {
310 mirror::Object* obj = requests.back().first;
311 size_t request_size = requests.back().second;
312 requests.pop_back();
313 byte magic = (request_size & 0xFF) | 1;
314 for (size_t k = 0; k < request_size; ++k) {
315 ASSERT_EQ(reinterpret_cast<const byte*>(obj)[k], magic);
316 }
317 ASSERT_GE(los->Free(Thread::Current(), obj), request_size);
318 }
319 }
320
321 size_t bytes_allocated = 0;
322 // Checks that the coalescing works.
323 mirror::Object* obj = los->Alloc(Thread::Current(), 100 * MB, &bytes_allocated);
324 EXPECT_TRUE(obj != NULL);
325 los->Free(Thread::Current(), obj);
326
327 EXPECT_EQ(0U, los->GetBytesAllocated());
328 EXPECT_EQ(0U, los->GetObjectsAllocated());
329 delete los;
330 }
331}
332
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800333void SpaceTest::AllocAndFreeListTestBody(CreateSpaceFn create_space) {
334 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800335 ASSERT_TRUE(space != NULL);
336
337 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700338 AddSpace(space);
Ian Rogers50b35e22012-10-04 10:09:15 -0700339 Thread* self = Thread::Current();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800340
341 // Succeeds, fits without adjusting the max allowed footprint.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800342 mirror::Object* lots_of_objects[1024];
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700343 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700344 size_t allocation_size = 0;
345 lots_of_objects[i] = space->Alloc(self, 16, &allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800346 EXPECT_TRUE(lots_of_objects[i] != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700347 InstallClass(lots_of_objects[i], 16);
348 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i]));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800349 }
350
351 // Release memory and check pointers are NULL
Ian Rogers50b35e22012-10-04 10:09:15 -0700352 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700353 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800354 EXPECT_TRUE(lots_of_objects[i] == NULL);
355 }
356
357 // Succeeds, fits by adjusting the max allowed footprint.
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700358 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700359 size_t allocation_size = 0;
360 lots_of_objects[i] = space->AllocWithGrowth(self, 1024, &allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800361 EXPECT_TRUE(lots_of_objects[i] != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700362 InstallClass(lots_of_objects[i], 1024);
363 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i]));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800364 }
365
366 // Release memory and check pointers are NULL
Ian Rogers50b35e22012-10-04 10:09:15 -0700367 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700368 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800369 EXPECT_TRUE(lots_of_objects[i] == NULL);
370 }
371}
372
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800373TEST_F(SpaceTest, AllocAndFreeList_DlMallocSpace) {
374 AllocAndFreeListTestBody(SpaceTest::CreateDlMallocSpace);
375}
376TEST_F(SpaceTest, AllocAndFreeList_RosAllocSpace) {
377 AllocAndFreeListTestBody(SpaceTest::CreateRosAllocSpace);
378}
379
380void SpaceTest::SizeFootPrintGrowthLimitAndTrimBody(MallocSpace* space, intptr_t object_size,
Ian Rogers3bb17a62012-01-27 23:56:44 -0800381 int round, size_t growth_limit) {
382 if (((object_size > 0 && object_size >= static_cast<intptr_t>(growth_limit))) ||
383 ((object_size < 0 && -object_size >= static_cast<intptr_t>(growth_limit)))) {
384 // No allocation can succeed
385 return;
386 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800387
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700388 // The space's footprint equals amount of resources requested from system
389 size_t footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800390
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700391 // The space must at least have its book keeping allocated
Ian Rogers3bb17a62012-01-27 23:56:44 -0800392 EXPECT_GT(footprint, 0u);
393
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700394 // But it shouldn't exceed the initial size
Ian Rogers3bb17a62012-01-27 23:56:44 -0800395 EXPECT_LE(footprint, growth_limit);
396
397 // space's size shouldn't exceed the initial size
398 EXPECT_LE(space->Size(), growth_limit);
399
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700400 // this invariant should always hold or else the space has grown to be larger than what the
Ian Rogers3bb17a62012-01-27 23:56:44 -0800401 // space believes its size is (which will break invariants)
402 EXPECT_GE(space->Size(), footprint);
403
404 // Fill the space with lots of small objects up to the growth limit
405 size_t max_objects = (growth_limit / (object_size > 0 ? object_size : 8)) + 1;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800406 UniquePtr<mirror::Object*[]> lots_of_objects(new mirror::Object*[max_objects]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800407 size_t last_object = 0; // last object for which allocation succeeded
408 size_t amount_allocated = 0; // amount of space allocated
Ian Rogers50b35e22012-10-04 10:09:15 -0700409 Thread* self = Thread::Current();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700410 size_t rand_seed = 123456789;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700411 for (size_t i = 0; i < max_objects; i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800412 size_t alloc_fails = 0; // number of failed allocations
413 size_t max_fails = 30; // number of times we fail allocation before giving up
414 for (; alloc_fails < max_fails; alloc_fails++) {
415 size_t alloc_size;
416 if (object_size > 0) {
417 alloc_size = object_size;
418 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700419 alloc_size = test_rand(&rand_seed) % static_cast<size_t>(-object_size);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700420 // Note the minimum size, which is the size of a zero-length byte array, is 12.
421 if (alloc_size < 12) {
422 alloc_size = 12;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800423 }
424 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800425 mirror::Object* object;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700426 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800427 if (round <= 1) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700428 object = space->Alloc(self, alloc_size, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800429 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700430 object = space->AllocWithGrowth(self, alloc_size, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800431 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700432 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800433 EXPECT_GE(space->Size(), footprint); // invariant
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700434 if (object != NULL) { // allocation succeeded
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700435 InstallClass(object, alloc_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800436 lots_of_objects.get()[i] = object;
437 size_t allocation_size = space->AllocationSize(object);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700438 EXPECT_EQ(bytes_allocated, allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800439 if (object_size > 0) {
440 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
441 } else {
442 EXPECT_GE(allocation_size, 8u);
443 }
444 amount_allocated += allocation_size;
445 break;
446 }
447 }
448 if (alloc_fails == max_fails) {
449 last_object = i;
450 break;
451 }
452 }
453 CHECK_NE(last_object, 0u); // we should have filled the space
454 EXPECT_GT(amount_allocated, 0u);
455
456 // We shouldn't have gone past the growth_limit
457 EXPECT_LE(amount_allocated, growth_limit);
458 EXPECT_LE(footprint, growth_limit);
459 EXPECT_LE(space->Size(), growth_limit);
460
461 // footprint and size should agree with amount allocated
462 EXPECT_GE(footprint, amount_allocated);
463 EXPECT_GE(space->Size(), amount_allocated);
464
465 // Release storage in a semi-adhoc manner
466 size_t free_increment = 96;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700467 while (true) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800468 // Give the space a haircut
469 space->Trim();
470
471 // Bounds sanity
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700472 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800473 EXPECT_LE(amount_allocated, growth_limit);
474 EXPECT_GE(footprint, amount_allocated);
475 EXPECT_LE(footprint, growth_limit);
476 EXPECT_GE(space->Size(), amount_allocated);
477 EXPECT_LE(space->Size(), growth_limit);
478
479 if (free_increment == 0) {
480 break;
481 }
482
483 // Free some objects
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700484 for (size_t i = 0; i < last_object; i += free_increment) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800485 mirror::Object* object = lots_of_objects.get()[i];
Ian Rogers3bb17a62012-01-27 23:56:44 -0800486 if (object == NULL) {
487 continue;
488 }
489 size_t allocation_size = space->AllocationSize(object);
490 if (object_size > 0) {
491 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
492 } else {
493 EXPECT_GE(allocation_size, 8u);
494 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700495 space->Free(self, object);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800496 lots_of_objects.get()[i] = NULL;
497 amount_allocated -= allocation_size;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700498 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800499 EXPECT_GE(space->Size(), footprint); // invariant
500 }
501
502 free_increment >>= 1;
503 }
504
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700505 // The space has become empty here before allocating a large object
506 // below. For RosAlloc, revoke thread-local runs, which are kept
507 // even when empty for a performance reason, so that they won't
508 // cause the following large object allocation to fail due to
509 // potential fragmentation. Note they are normally revoked at each
510 // GC (but no GC here.)
511 space->RevokeAllThreadLocalBuffers();
512
Ian Rogers3bb17a62012-01-27 23:56:44 -0800513 // All memory was released, try a large allocation to check freed memory is being coalesced
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800514 mirror::Object* large_object;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800515 size_t three_quarters_space = (growth_limit / 2) + (growth_limit / 4);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700516 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800517 if (round <= 1) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700518 large_object = space->Alloc(self, three_quarters_space, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800519 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700520 large_object = space->AllocWithGrowth(self, three_quarters_space, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800521 }
522 EXPECT_TRUE(large_object != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700523 InstallClass(large_object, three_quarters_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800524
525 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700526 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800527 EXPECT_LE(footprint, growth_limit);
528 EXPECT_GE(space->Size(), footprint);
529 EXPECT_LE(space->Size(), growth_limit);
530
531 // Clean up
Ian Rogers50b35e22012-10-04 10:09:15 -0700532 space->Free(self, large_object);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800533
534 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700535 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800536 EXPECT_LE(footprint, growth_limit);
537 EXPECT_GE(space->Size(), footprint);
538 EXPECT_LE(space->Size(), growth_limit);
539}
540
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800541void SpaceTest::SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size, CreateSpaceFn create_space) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800542 size_t initial_size = 4 * MB;
543 size_t growth_limit = 8 * MB;
544 size_t capacity = 16 * MB;
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800545 MallocSpace* space(create_space("test", initial_size, growth_limit, capacity, NULL));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800546 ASSERT_TRUE(space != NULL);
547
548 // Basic sanity
549 EXPECT_EQ(space->Capacity(), growth_limit);
550 EXPECT_EQ(space->NonGrowthLimitCapacity(), capacity);
551
552 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700553 AddSpace(space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800554
555 // In this round we don't allocate with growth and therefore can't grow past the initial size.
556 // This effectively makes the growth_limit the initial_size, so assert this.
557 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 1, initial_size);
558 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 2, growth_limit);
559 // Remove growth limit
560 space->ClearGrowthLimit();
561 EXPECT_EQ(space->Capacity(), capacity);
562 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 3, capacity);
563}
564
565#define TEST_SizeFootPrintGrowthLimitAndTrim(name, size) \
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800566 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name##_DlMallocSpace) { \
567 SizeFootPrintGrowthLimitAndTrimDriver(size, SpaceTest::CreateDlMallocSpace); \
Ian Rogers3bb17a62012-01-27 23:56:44 -0800568 } \
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800569 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name##_DlMallocSpace) { \
570 SizeFootPrintGrowthLimitAndTrimDriver(-size, SpaceTest::CreateDlMallocSpace); \
571 } \
572 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name##_RosAllocSpace) { \
573 SizeFootPrintGrowthLimitAndTrimDriver(size, SpaceTest::CreateRosAllocSpace); \
574 } \
575 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name##_RosAllocSpace) { \
576 SizeFootPrintGrowthLimitAndTrimDriver(-size, SpaceTest::CreateRosAllocSpace); \
Ian Rogers3bb17a62012-01-27 23:56:44 -0800577 }
578
579// Each size test is its own test so that we get a fresh heap each time
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800580TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_12B_DlMallocSpace) {
581 SizeFootPrintGrowthLimitAndTrimDriver(12, SpaceTest::CreateDlMallocSpace);
582}
583TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_12B_RosAllocSpace) {
584 SizeFootPrintGrowthLimitAndTrimDriver(12, SpaceTest::CreateRosAllocSpace);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800585}
586TEST_SizeFootPrintGrowthLimitAndTrim(16B, 16)
587TEST_SizeFootPrintGrowthLimitAndTrim(24B, 24)
588TEST_SizeFootPrintGrowthLimitAndTrim(32B, 32)
589TEST_SizeFootPrintGrowthLimitAndTrim(64B, 64)
590TEST_SizeFootPrintGrowthLimitAndTrim(128B, 128)
591TEST_SizeFootPrintGrowthLimitAndTrim(1KB, 1 * KB)
592TEST_SizeFootPrintGrowthLimitAndTrim(4KB, 4 * KB)
593TEST_SizeFootPrintGrowthLimitAndTrim(1MB, 1 * MB)
594TEST_SizeFootPrintGrowthLimitAndTrim(4MB, 4 * MB)
595TEST_SizeFootPrintGrowthLimitAndTrim(8MB, 8 * MB)
596
Ian Rogers1d54e732013-05-02 21:10:01 -0700597} // namespace space
598} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700599} // namespace art