blob: b1be9d8a849b05f89cc1be06e886e8af319ba64a [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) {
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -080061 return RosAllocSpace::Create(name, initial_size, growth_limit, capacity, requested_begin,
62 Runtime::Current()->GetHeap()->IsLowMemoryMode());
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080063 }
64
65 typedef MallocSpace* (*CreateSpaceFn)(const std::string& name, size_t initial_size, size_t growth_limit,
66 size_t capacity, byte* requested_begin);
67 void InitTestBody(CreateSpaceFn create_space);
68 void ZygoteSpaceTestBody(CreateSpaceFn create_space);
69 void AllocAndFreeTestBody(CreateSpaceFn create_space);
70 void AllocAndFreeListTestBody(CreateSpaceFn create_space);
71
72 void SizeFootPrintGrowthLimitAndTrimBody(MallocSpace* space, intptr_t object_size,
73 int round, size_t growth_limit);
74 void SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size, CreateSpaceFn create_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -080075};
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070076
Mathieu Chartiereb5710e2013-07-25 15:19:42 -070077static size_t test_rand(size_t* seed) {
78 *seed = *seed * 1103515245 + 12345;
79 return *seed;
80}
81
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080082void SpaceTest::InitTestBody(CreateSpaceFn create_space) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070083 {
jeffhaoc1160702011-10-27 15:48:45 -070084 // Init < max == growth
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080085 UniquePtr<Space> space(create_space("test", 16 * MB, 32 * MB, 32 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070086 EXPECT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070087 }
88 {
jeffhaoc1160702011-10-27 15:48:45 -070089 // Init == max == growth
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080090 UniquePtr<Space> space(create_space("test", 16 * MB, 16 * MB, 16 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070091 EXPECT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070092 }
93 {
jeffhaoc1160702011-10-27 15:48:45 -070094 // Init > max == growth
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080095 UniquePtr<Space> space(create_space("test", 32 * MB, 16 * MB, 16 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070096 EXPECT_TRUE(space.get() == NULL);
97 }
98 {
99 // Growth == init < max
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800100 UniquePtr<Space> space(create_space("test", 16 * MB, 16 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -0700101 EXPECT_TRUE(space.get() != NULL);
102 }
103 {
104 // Growth < init < max
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800105 UniquePtr<Space> space(create_space("test", 16 * MB, 8 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -0700106 EXPECT_TRUE(space.get() == NULL);
107 }
108 {
109 // Init < growth < max
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800110 UniquePtr<Space> space(create_space("test", 8 * MB, 16 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -0700111 EXPECT_TRUE(space.get() != NULL);
112 }
113 {
114 // Init < max < growth
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800115 UniquePtr<Space> space(create_space("test", 8 * MB, 32 * MB, 16 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -0700116 EXPECT_TRUE(space.get() == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700117 }
118}
119
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800120TEST_F(SpaceTest, Init_DlMallocSpace) {
121 InitTestBody(SpaceTest::CreateDlMallocSpace);
122}
123TEST_F(SpaceTest, Init_RosAllocSpace) {
124 InitTestBody(SpaceTest::CreateRosAllocSpace);
125}
126
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700127// TODO: This test is not very good, we should improve it.
128// The test should do more allocations before the creation of the ZygoteSpace, and then do
129// allocations after the ZygoteSpace is created. The test should also do some GCs to ensure that
130// the GC works with the ZygoteSpace.
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800131void SpaceTest::ZygoteSpaceTestBody(CreateSpaceFn create_space) {
132 size_t dummy = 0;
133 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, NULL));
134 ASSERT_TRUE(space != NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700135
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800136 // Make space findable to the heap, will also delete space when runtime is cleaned up
137 AddSpace(space);
138 Thread* self = Thread::Current();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700139
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800140 // Succeeds, fits without adjusting the footprint limit.
141 mirror::Object* ptr1 = space->Alloc(self, 1 * MB, &dummy);
142 EXPECT_TRUE(ptr1 != NULL);
143 InstallClass(ptr1, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700144
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800145 // Fails, requires a higher footprint limit.
146 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy);
147 EXPECT_TRUE(ptr2 == NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700148
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800149 // Succeeds, adjusts the footprint.
150 size_t ptr3_bytes_allocated;
151 mirror::Object* ptr3 = space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated);
152 EXPECT_TRUE(ptr3 != NULL);
153 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
154 InstallClass(ptr3, 8 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700155
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800156 // Fails, requires a higher footprint limit.
157 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy);
158 EXPECT_TRUE(ptr4 == NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700159
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800160 // Also fails, requires a higher allowed footprint.
161 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy);
162 EXPECT_TRUE(ptr5 == NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700163
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800164 // Release some memory.
165 size_t free3 = space->AllocationSize(ptr3);
166 EXPECT_EQ(free3, ptr3_bytes_allocated);
167 EXPECT_EQ(free3, space->Free(self, ptr3));
168 EXPECT_LE(8U * MB, free3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700169
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800170 // Succeeds, now that memory has been freed.
171 mirror::Object* ptr6 = space->AllocWithGrowth(self, 9 * MB, &dummy);
172 EXPECT_TRUE(ptr6 != NULL);
173 InstallClass(ptr6, 9 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700174
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800175 // Final clean up.
176 size_t free1 = space->AllocationSize(ptr1);
177 space->Free(self, ptr1);
178 EXPECT_LE(1U * MB, free1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700179
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800180 // Make sure that the zygote space isn't directly at the start of the space.
181 space->Alloc(self, 1U * MB, &dummy);
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800182 space = space->CreateZygoteSpace("alloc space", Runtime::Current()->GetHeap()->IsLowMemoryMode());
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700183
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800184 // Make space findable to the heap, will also delete space when runtime is cleaned up
185 AddSpace(space);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700186
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800187 // Succeeds, fits without adjusting the footprint limit.
188 ptr1 = space->Alloc(self, 1 * MB, &dummy);
189 EXPECT_TRUE(ptr1 != NULL);
190 InstallClass(ptr1, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700191
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800192 // Fails, requires a higher footprint limit.
193 ptr2 = space->Alloc(self, 8 * MB, &dummy);
194 EXPECT_TRUE(ptr2 == NULL);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700195
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800196 // Succeeds, adjusts the footprint.
197 ptr3 = space->AllocWithGrowth(self, 2 * MB, &dummy);
198 EXPECT_TRUE(ptr3 != NULL);
199 InstallClass(ptr3, 2 * MB);
200 space->Free(self, ptr3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700201
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800202 // Final clean up.
203 free1 = space->AllocationSize(ptr1);
204 space->Free(self, ptr1);
205 EXPECT_LE(1U * MB, free1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700206}
207
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800208TEST_F(SpaceTest, ZygoteSpace_DlMallocSpace) {
209 ZygoteSpaceTestBody(SpaceTest::CreateDlMallocSpace);
210}
211
212TEST_F(SpaceTest, ZygoteSpace_RosAllocSpace) {
213 ZygoteSpaceTestBody(SpaceTest::CreateRosAllocSpace);
214}
215
216void SpaceTest::AllocAndFreeTestBody(CreateSpaceFn create_space) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700217 size_t dummy = 0;
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800218 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Ian Rogers30fab402012-01-23 15:43:46 -0800219 ASSERT_TRUE(space != NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -0700220 Thread* self = Thread::Current();
Ian Rogers30fab402012-01-23 15:43:46 -0800221
Ian Rogers3bb17a62012-01-27 23:56:44 -0800222 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700223 AddSpace(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700224
Ian Rogers3bb17a62012-01-27 23:56:44 -0800225 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700226 mirror::Object* ptr1 = space->Alloc(self, 1 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700227 EXPECT_TRUE(ptr1 != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700228 InstallClass(ptr1, 1 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700229
Ian Rogers3bb17a62012-01-27 23:56:44 -0800230 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700231 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700232 EXPECT_TRUE(ptr2 == NULL);
233
234 // Succeeds, adjusts the footprint.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700235 size_t ptr3_bytes_allocated;
236 mirror::Object* ptr3 = space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700237 EXPECT_TRUE(ptr3 != NULL);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700238 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700239 InstallClass(ptr3, 8 * 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* ptr4 = space->Alloc(self, 8 * MB, &dummy);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800243 EXPECT_TRUE(ptr4 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700244
245 // Also fails, requires a higher allowed footprint.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700246 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800247 EXPECT_TRUE(ptr5 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700248
249 // Release some memory.
Ian Rogers30fab402012-01-23 15:43:46 -0800250 size_t free3 = space->AllocationSize(ptr3);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700251 EXPECT_EQ(free3, ptr3_bytes_allocated);
Ian Rogers50b35e22012-10-04 10:09:15 -0700252 space->Free(self, ptr3);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700253 EXPECT_LE(8U * MB, free3);
254
255 // Succeeds, now that memory has been freed.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700256 mirror::Object* ptr6 = space->AllocWithGrowth(self, 9 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700257 EXPECT_TRUE(ptr6 != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700258 InstallClass(ptr6, 9 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700259
260 // Final clean up.
Ian Rogers30fab402012-01-23 15:43:46 -0800261 size_t free1 = space->AllocationSize(ptr1);
Ian Rogers50b35e22012-10-04 10:09:15 -0700262 space->Free(self, ptr1);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700263 EXPECT_LE(1U * MB, free1);
264}
265
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800266TEST_F(SpaceTest, AllocAndFree_DlMallocSpace) {
267 AllocAndFreeTestBody(SpaceTest::CreateDlMallocSpace);
268}
269TEST_F(SpaceTest, AllocAndFree_RosAllocSpace) {
270 AllocAndFreeTestBody(SpaceTest::CreateRosAllocSpace);
271}
272
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700273TEST_F(SpaceTest, LargeObjectTest) {
274 size_t rand_seed = 0;
275 for (size_t i = 0; i < 2; ++i) {
276 LargeObjectSpace* los = NULL;
277 if (i == 0) {
278 los = space::LargeObjectMapSpace::Create("large object space");
279 } else {
280 los = space::FreeListSpace::Create("large object space", NULL, 128 * MB);
281 }
282
283 static const size_t num_allocations = 64;
284 static const size_t max_allocation_size = 0x100000;
285 std::vector<std::pair<mirror::Object*, size_t> > requests;
286
287 for (size_t phase = 0; phase < 2; ++phase) {
288 while (requests.size() < num_allocations) {
289 size_t request_size = test_rand(&rand_seed) % max_allocation_size;
290 size_t allocation_size = 0;
291 mirror::Object* obj = los->Alloc(Thread::Current(), request_size, &allocation_size);
292 ASSERT_TRUE(obj != NULL);
293 ASSERT_EQ(allocation_size, los->AllocationSize(obj));
294 ASSERT_GE(allocation_size, request_size);
295 // Fill in our magic value.
296 byte magic = (request_size & 0xFF) | 1;
297 memset(obj, magic, request_size);
298 requests.push_back(std::make_pair(obj, request_size));
299 }
300
301 // "Randomly" shuffle the requests.
302 for (size_t k = 0; k < 10; ++k) {
303 for (size_t j = 0; j < requests.size(); ++j) {
304 std::swap(requests[j], requests[test_rand(&rand_seed) % requests.size()]);
305 }
306 }
307
308 // Free 1 / 2 the allocations the first phase, and all the second phase.
309 size_t limit = !phase ? requests.size() / 2 : 0;
310 while (requests.size() > limit) {
311 mirror::Object* obj = requests.back().first;
312 size_t request_size = requests.back().second;
313 requests.pop_back();
314 byte magic = (request_size & 0xFF) | 1;
315 for (size_t k = 0; k < request_size; ++k) {
316 ASSERT_EQ(reinterpret_cast<const byte*>(obj)[k], magic);
317 }
318 ASSERT_GE(los->Free(Thread::Current(), obj), request_size);
319 }
320 }
321
322 size_t bytes_allocated = 0;
323 // Checks that the coalescing works.
324 mirror::Object* obj = los->Alloc(Thread::Current(), 100 * MB, &bytes_allocated);
325 EXPECT_TRUE(obj != NULL);
326 los->Free(Thread::Current(), obj);
327
328 EXPECT_EQ(0U, los->GetBytesAllocated());
329 EXPECT_EQ(0U, los->GetObjectsAllocated());
330 delete los;
331 }
332}
333
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800334void SpaceTest::AllocAndFreeListTestBody(CreateSpaceFn create_space) {
335 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800336 ASSERT_TRUE(space != NULL);
337
338 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700339 AddSpace(space);
Ian Rogers50b35e22012-10-04 10:09:15 -0700340 Thread* self = Thread::Current();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800341
342 // Succeeds, fits without adjusting the max allowed footprint.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800343 mirror::Object* lots_of_objects[1024];
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700344 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700345 size_t allocation_size = 0;
346 lots_of_objects[i] = space->Alloc(self, 16, &allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800347 EXPECT_TRUE(lots_of_objects[i] != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700348 InstallClass(lots_of_objects[i], 16);
349 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i]));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800350 }
351
352 // Release memory and check pointers are NULL
Ian Rogers50b35e22012-10-04 10:09:15 -0700353 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700354 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800355 EXPECT_TRUE(lots_of_objects[i] == NULL);
356 }
357
358 // Succeeds, fits by adjusting the max allowed footprint.
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;
361 lots_of_objects[i] = space->AllocWithGrowth(self, 1024, &allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800362 EXPECT_TRUE(lots_of_objects[i] != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700363 InstallClass(lots_of_objects[i], 1024);
364 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i]));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800365 }
366
367 // Release memory and check pointers are NULL
Ian Rogers50b35e22012-10-04 10:09:15 -0700368 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700369 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800370 EXPECT_TRUE(lots_of_objects[i] == NULL);
371 }
372}
373
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800374TEST_F(SpaceTest, AllocAndFreeList_DlMallocSpace) {
375 AllocAndFreeListTestBody(SpaceTest::CreateDlMallocSpace);
376}
377TEST_F(SpaceTest, AllocAndFreeList_RosAllocSpace) {
378 AllocAndFreeListTestBody(SpaceTest::CreateRosAllocSpace);
379}
380
381void SpaceTest::SizeFootPrintGrowthLimitAndTrimBody(MallocSpace* space, intptr_t object_size,
Ian Rogers3bb17a62012-01-27 23:56:44 -0800382 int round, size_t growth_limit) {
383 if (((object_size > 0 && object_size >= static_cast<intptr_t>(growth_limit))) ||
384 ((object_size < 0 && -object_size >= static_cast<intptr_t>(growth_limit)))) {
385 // No allocation can succeed
386 return;
387 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800388
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700389 // The space's footprint equals amount of resources requested from system
390 size_t footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800391
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700392 // The space must at least have its book keeping allocated
Ian Rogers3bb17a62012-01-27 23:56:44 -0800393 EXPECT_GT(footprint, 0u);
394
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700395 // But it shouldn't exceed the initial size
Ian Rogers3bb17a62012-01-27 23:56:44 -0800396 EXPECT_LE(footprint, growth_limit);
397
398 // space's size shouldn't exceed the initial size
399 EXPECT_LE(space->Size(), growth_limit);
400
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700401 // this invariant should always hold or else the space has grown to be larger than what the
Ian Rogers3bb17a62012-01-27 23:56:44 -0800402 // space believes its size is (which will break invariants)
403 EXPECT_GE(space->Size(), footprint);
404
405 // Fill the space with lots of small objects up to the growth limit
406 size_t max_objects = (growth_limit / (object_size > 0 ? object_size : 8)) + 1;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800407 UniquePtr<mirror::Object*[]> lots_of_objects(new mirror::Object*[max_objects]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800408 size_t last_object = 0; // last object for which allocation succeeded
409 size_t amount_allocated = 0; // amount of space allocated
Ian Rogers50b35e22012-10-04 10:09:15 -0700410 Thread* self = Thread::Current();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700411 size_t rand_seed = 123456789;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700412 for (size_t i = 0; i < max_objects; i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800413 size_t alloc_fails = 0; // number of failed allocations
414 size_t max_fails = 30; // number of times we fail allocation before giving up
415 for (; alloc_fails < max_fails; alloc_fails++) {
416 size_t alloc_size;
417 if (object_size > 0) {
418 alloc_size = object_size;
419 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700420 alloc_size = test_rand(&rand_seed) % static_cast<size_t>(-object_size);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700421 // Note the minimum size, which is the size of a zero-length byte array, is 12.
422 if (alloc_size < 12) {
423 alloc_size = 12;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800424 }
425 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800426 mirror::Object* object;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700427 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800428 if (round <= 1) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700429 object = space->Alloc(self, alloc_size, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800430 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700431 object = space->AllocWithGrowth(self, alloc_size, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800432 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700433 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800434 EXPECT_GE(space->Size(), footprint); // invariant
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700435 if (object != NULL) { // allocation succeeded
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700436 InstallClass(object, alloc_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800437 lots_of_objects.get()[i] = object;
438 size_t allocation_size = space->AllocationSize(object);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700439 EXPECT_EQ(bytes_allocated, allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800440 if (object_size > 0) {
441 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
442 } else {
443 EXPECT_GE(allocation_size, 8u);
444 }
445 amount_allocated += allocation_size;
446 break;
447 }
448 }
449 if (alloc_fails == max_fails) {
450 last_object = i;
451 break;
452 }
453 }
454 CHECK_NE(last_object, 0u); // we should have filled the space
455 EXPECT_GT(amount_allocated, 0u);
456
457 // We shouldn't have gone past the growth_limit
458 EXPECT_LE(amount_allocated, growth_limit);
459 EXPECT_LE(footprint, growth_limit);
460 EXPECT_LE(space->Size(), growth_limit);
461
462 // footprint and size should agree with amount allocated
463 EXPECT_GE(footprint, amount_allocated);
464 EXPECT_GE(space->Size(), amount_allocated);
465
466 // Release storage in a semi-adhoc manner
467 size_t free_increment = 96;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700468 while (true) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800469 // Give the space a haircut
470 space->Trim();
471
472 // Bounds sanity
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700473 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800474 EXPECT_LE(amount_allocated, growth_limit);
475 EXPECT_GE(footprint, amount_allocated);
476 EXPECT_LE(footprint, growth_limit);
477 EXPECT_GE(space->Size(), amount_allocated);
478 EXPECT_LE(space->Size(), growth_limit);
479
480 if (free_increment == 0) {
481 break;
482 }
483
484 // Free some objects
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700485 for (size_t i = 0; i < last_object; i += free_increment) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800486 mirror::Object* object = lots_of_objects.get()[i];
Ian Rogers3bb17a62012-01-27 23:56:44 -0800487 if (object == NULL) {
488 continue;
489 }
490 size_t allocation_size = space->AllocationSize(object);
491 if (object_size > 0) {
492 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
493 } else {
494 EXPECT_GE(allocation_size, 8u);
495 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700496 space->Free(self, object);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800497 lots_of_objects.get()[i] = NULL;
498 amount_allocated -= allocation_size;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700499 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800500 EXPECT_GE(space->Size(), footprint); // invariant
501 }
502
503 free_increment >>= 1;
504 }
505
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700506 // The space has become empty here before allocating a large object
507 // below. For RosAlloc, revoke thread-local runs, which are kept
508 // even when empty for a performance reason, so that they won't
509 // cause the following large object allocation to fail due to
510 // potential fragmentation. Note they are normally revoked at each
511 // GC (but no GC here.)
512 space->RevokeAllThreadLocalBuffers();
513
Ian Rogers3bb17a62012-01-27 23:56:44 -0800514 // All memory was released, try a large allocation to check freed memory is being coalesced
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800515 mirror::Object* large_object;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800516 size_t three_quarters_space = (growth_limit / 2) + (growth_limit / 4);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700517 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800518 if (round <= 1) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700519 large_object = space->Alloc(self, three_quarters_space, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800520 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700521 large_object = space->AllocWithGrowth(self, three_quarters_space, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800522 }
523 EXPECT_TRUE(large_object != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700524 InstallClass(large_object, three_quarters_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800525
526 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700527 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800528 EXPECT_LE(footprint, growth_limit);
529 EXPECT_GE(space->Size(), footprint);
530 EXPECT_LE(space->Size(), growth_limit);
531
532 // Clean up
Ian Rogers50b35e22012-10-04 10:09:15 -0700533 space->Free(self, large_object);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800534
535 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700536 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800537 EXPECT_LE(footprint, growth_limit);
538 EXPECT_GE(space->Size(), footprint);
539 EXPECT_LE(space->Size(), growth_limit);
540}
541
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800542void SpaceTest::SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size, CreateSpaceFn create_space) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800543 size_t initial_size = 4 * MB;
544 size_t growth_limit = 8 * MB;
545 size_t capacity = 16 * MB;
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800546 MallocSpace* space(create_space("test", initial_size, growth_limit, capacity, NULL));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800547 ASSERT_TRUE(space != NULL);
548
549 // Basic sanity
550 EXPECT_EQ(space->Capacity(), growth_limit);
551 EXPECT_EQ(space->NonGrowthLimitCapacity(), capacity);
552
553 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700554 AddSpace(space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800555
556 // In this round we don't allocate with growth and therefore can't grow past the initial size.
557 // This effectively makes the growth_limit the initial_size, so assert this.
558 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 1, initial_size);
559 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 2, growth_limit);
560 // Remove growth limit
561 space->ClearGrowthLimit();
562 EXPECT_EQ(space->Capacity(), capacity);
563 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 3, capacity);
564}
565
566#define TEST_SizeFootPrintGrowthLimitAndTrim(name, size) \
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800567 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name##_DlMallocSpace) { \
568 SizeFootPrintGrowthLimitAndTrimDriver(size, SpaceTest::CreateDlMallocSpace); \
Ian Rogers3bb17a62012-01-27 23:56:44 -0800569 } \
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800570 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name##_DlMallocSpace) { \
571 SizeFootPrintGrowthLimitAndTrimDriver(-size, SpaceTest::CreateDlMallocSpace); \
572 } \
573 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name##_RosAllocSpace) { \
574 SizeFootPrintGrowthLimitAndTrimDriver(size, SpaceTest::CreateRosAllocSpace); \
575 } \
576 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name##_RosAllocSpace) { \
577 SizeFootPrintGrowthLimitAndTrimDriver(-size, SpaceTest::CreateRosAllocSpace); \
Ian Rogers3bb17a62012-01-27 23:56:44 -0800578 }
579
580// Each size test is its own test so that we get a fresh heap each time
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800581TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_12B_DlMallocSpace) {
582 SizeFootPrintGrowthLimitAndTrimDriver(12, SpaceTest::CreateDlMallocSpace);
583}
584TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_12B_RosAllocSpace) {
585 SizeFootPrintGrowthLimitAndTrimDriver(12, SpaceTest::CreateRosAllocSpace);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800586}
587TEST_SizeFootPrintGrowthLimitAndTrim(16B, 16)
588TEST_SizeFootPrintGrowthLimitAndTrim(24B, 24)
589TEST_SizeFootPrintGrowthLimitAndTrim(32B, 32)
590TEST_SizeFootPrintGrowthLimitAndTrim(64B, 64)
591TEST_SizeFootPrintGrowthLimitAndTrim(128B, 128)
592TEST_SizeFootPrintGrowthLimitAndTrim(1KB, 1 * KB)
593TEST_SizeFootPrintGrowthLimitAndTrim(4KB, 4 * KB)
594TEST_SizeFootPrintGrowthLimitAndTrim(1MB, 1 * MB)
595TEST_SizeFootPrintGrowthLimitAndTrim(4MB, 4 * MB)
596TEST_SizeFootPrintGrowthLimitAndTrim(8MB, 8 * MB)
597
Ian Rogers1d54e732013-05-02 21:10:01 -0700598} // namespace space
599} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700600} // namespace art