blob: 6b597ae09360657d2e669d143fbb8a82f27d1c04 [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 Chartier1c23e1e2012-10-12 14:14:11 -070034 void SizeFootPrintGrowthLimitAndTrimBody(DlMallocSpace* space, intptr_t object_size,
Ian Rogers3bb17a62012-01-27 23:56:44 -080035 int round, size_t growth_limit);
36 void SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size);
Ian Rogers1d54e732013-05-02 21:10:01 -070037
Mathieu Chartier590fee92013-09-13 13:46:47 -070038 void AddSpace(ContinuousSpace* space) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070039 // For RosAlloc, revoke the thread local runs before moving onto a
40 // new alloc space.
41 Runtime::Current()->GetHeap()->RevokeAllThreadLocalBuffers();
Mathieu Chartier590fee92013-09-13 13:46:47 -070042 Runtime::Current()->GetHeap()->AddSpace(space);
Ian Rogers1d54e732013-05-02 21:10:01 -070043 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070044 void InstallClass(mirror::Object* o, size_t size) NO_THREAD_SAFETY_ANALYSIS {
45 // Note the minimum size, which is the size of a zero-length byte array, is 12.
46 EXPECT_GE(size, static_cast<size_t>(12));
47 SirtRef<mirror::ClassLoader> null_loader(Thread::Current(), NULL);
48 mirror::Class* byte_array_class = Runtime::Current()->GetClassLinker()->FindClass("[B", null_loader);
49 EXPECT_TRUE(byte_array_class != NULL);
50 o->SetClass(byte_array_class);
51 mirror::Array* arr = o->AsArray();
52 // size_t header_size = sizeof(mirror::Object) + 4;
53 size_t header_size = arr->DataOffset(1).Uint32Value();
54 int32_t length = size - header_size;
55 arr->SetLength(length);
56 EXPECT_EQ(arr->SizeOf(), size);
57 }
Ian Rogers3bb17a62012-01-27 23:56:44 -080058};
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070059
Mathieu Chartiereb5710e2013-07-25 15:19:42 -070060static size_t test_rand(size_t* seed) {
61 *seed = *seed * 1103515245 + 12345;
62 return *seed;
63}
64
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070065TEST_F(SpaceTest, Init) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070066 {
jeffhaoc1160702011-10-27 15:48:45 -070067 // Init < max == growth
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070068 UniquePtr<Space> space(DlMallocSpace::Create("test", 16 * MB, 32 * MB, 32 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070069 EXPECT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070070 }
71 {
jeffhaoc1160702011-10-27 15:48:45 -070072 // Init == max == growth
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070073 UniquePtr<Space> space(DlMallocSpace::Create("test", 16 * MB, 16 * MB, 16 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070074 EXPECT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070075 }
76 {
jeffhaoc1160702011-10-27 15:48:45 -070077 // Init > max == growth
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070078 UniquePtr<Space> space(DlMallocSpace::Create("test", 32 * MB, 16 * MB, 16 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070079 EXPECT_TRUE(space.get() == NULL);
80 }
81 {
82 // Growth == init < max
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070083 UniquePtr<Space> space(DlMallocSpace::Create("test", 16 * MB, 16 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070084 EXPECT_TRUE(space.get() != NULL);
85 }
86 {
87 // Growth < init < max
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070088 UniquePtr<Space> space(DlMallocSpace::Create("test", 16 * MB, 8 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070089 EXPECT_TRUE(space.get() == NULL);
90 }
91 {
92 // Init < growth < max
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070093 UniquePtr<Space> space(DlMallocSpace::Create("test", 8 * MB, 16 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070094 EXPECT_TRUE(space.get() != NULL);
95 }
96 {
97 // Init < max < growth
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070098 UniquePtr<Space> space(DlMallocSpace::Create("test", 8 * MB, 32 * MB, 16 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070099 EXPECT_TRUE(space.get() == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700100 }
101}
102
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700103// TODO: This test is not very good, we should improve it.
104// The test should do more allocations before the creation of the ZygoteSpace, and then do
105// allocations after the ZygoteSpace is created. The test should also do some GCs to ensure that
106// the GC works with the ZygoteSpace.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700107TEST_F(SpaceTest, ZygoteSpace) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700108 size_t dummy = 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700109 MallocSpace* space(DlMallocSpace::Create("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700110 ASSERT_TRUE(space != NULL);
111
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700112 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700113 AddSpace(space);
Ian Rogers50b35e22012-10-04 10:09:15 -0700114 Thread* self = Thread::Current();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700115
116 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700117 mirror::Object* ptr1 = space->Alloc(self, 1 * MB, &dummy);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700118 EXPECT_TRUE(ptr1 != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700119 InstallClass(ptr1, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700120
121 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700122 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700123 EXPECT_TRUE(ptr2 == NULL);
124
125 // Succeeds, adjusts the footprint.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700126 size_t ptr3_bytes_allocated;
127 mirror::Object* ptr3 = space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700128 EXPECT_TRUE(ptr3 != NULL);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700129 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700130 InstallClass(ptr3, 8 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700131
132 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700133 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700134 EXPECT_TRUE(ptr4 == NULL);
135
136 // Also fails, requires a higher allowed footprint.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700137 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700138 EXPECT_TRUE(ptr5 == NULL);
139
140 // Release some memory.
141 size_t free3 = space->AllocationSize(ptr3);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700142 EXPECT_EQ(free3, ptr3_bytes_allocated);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700143 EXPECT_EQ(free3, space->Free(self, ptr3));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700144 EXPECT_LE(8U * MB, free3);
145
146 // Succeeds, now that memory has been freed.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700147 mirror::Object* ptr6 = space->AllocWithGrowth(self, 9 * MB, &dummy);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700148 EXPECT_TRUE(ptr6 != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700149 InstallClass(ptr6, 9 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700150
151 // Final clean up.
152 size_t free1 = space->AllocationSize(ptr1);
Ian Rogers50b35e22012-10-04 10:09:15 -0700153 space->Free(self, ptr1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700154 EXPECT_LE(1U * MB, free1);
155
156 // Make sure that the zygote space isn't directly at the start of the space.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700157 space->Alloc(self, 1U * MB, &dummy);
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700158 space = space->CreateZygoteSpace("alloc space");
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700159
160 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700161 AddSpace(space);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700162
163 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700164 ptr1 = space->Alloc(self, 1 * MB, &dummy);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700165 EXPECT_TRUE(ptr1 != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700166 InstallClass(ptr1, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700167
168 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700169 ptr2 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700170 EXPECT_TRUE(ptr2 == NULL);
171
172 // Succeeds, adjusts the footprint.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700173 ptr3 = space->AllocWithGrowth(self, 2 * MB, &dummy);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700174 EXPECT_TRUE(ptr3 != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700175 InstallClass(ptr3, 2 * MB);
Ian Rogers50b35e22012-10-04 10:09:15 -0700176 space->Free(self, ptr3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700177
178 // Final clean up.
179 free1 = space->AllocationSize(ptr1);
Ian Rogers50b35e22012-10-04 10:09:15 -0700180 space->Free(self, ptr1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700181 EXPECT_LE(1U * MB, free1);
182}
183
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -0700184TEST_F(SpaceTest, AllocAndFree) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700185 size_t dummy = 0;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700186 DlMallocSpace* space(DlMallocSpace::Create("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Ian Rogers30fab402012-01-23 15:43:46 -0800187 ASSERT_TRUE(space != NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -0700188 Thread* self = Thread::Current();
Ian Rogers30fab402012-01-23 15:43:46 -0800189
Ian Rogers3bb17a62012-01-27 23:56:44 -0800190 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700191 AddSpace(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700192
Ian Rogers3bb17a62012-01-27 23:56:44 -0800193 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700194 mirror::Object* ptr1 = space->Alloc(self, 1 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700195 EXPECT_TRUE(ptr1 != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700196 InstallClass(ptr1, 1 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700197
Ian Rogers3bb17a62012-01-27 23:56:44 -0800198 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700199 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700200 EXPECT_TRUE(ptr2 == NULL);
201
202 // Succeeds, adjusts the footprint.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700203 size_t ptr3_bytes_allocated;
204 mirror::Object* ptr3 = space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700205 EXPECT_TRUE(ptr3 != NULL);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700206 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700207 InstallClass(ptr3, 8 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700208
Ian Rogers3bb17a62012-01-27 23:56:44 -0800209 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700210 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800211 EXPECT_TRUE(ptr4 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700212
213 // Also fails, requires a higher allowed footprint.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700214 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800215 EXPECT_TRUE(ptr5 == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700216
217 // Release some memory.
Ian Rogers30fab402012-01-23 15:43:46 -0800218 size_t free3 = space->AllocationSize(ptr3);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700219 EXPECT_EQ(free3, ptr3_bytes_allocated);
Ian Rogers50b35e22012-10-04 10:09:15 -0700220 space->Free(self, ptr3);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700221 EXPECT_LE(8U * MB, free3);
222
223 // Succeeds, now that memory has been freed.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700224 mirror::Object* ptr6 = space->AllocWithGrowth(self, 9 * MB, &dummy);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700225 EXPECT_TRUE(ptr6 != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700226 InstallClass(ptr6, 9 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700227
228 // Final clean up.
Ian Rogers30fab402012-01-23 15:43:46 -0800229 size_t free1 = space->AllocationSize(ptr1);
Ian Rogers50b35e22012-10-04 10:09:15 -0700230 space->Free(self, ptr1);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700231 EXPECT_LE(1U * MB, free1);
232}
233
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700234TEST_F(SpaceTest, LargeObjectTest) {
235 size_t rand_seed = 0;
236 for (size_t i = 0; i < 2; ++i) {
237 LargeObjectSpace* los = NULL;
238 if (i == 0) {
239 los = space::LargeObjectMapSpace::Create("large object space");
240 } else {
241 los = space::FreeListSpace::Create("large object space", NULL, 128 * MB);
242 }
243
244 static const size_t num_allocations = 64;
245 static const size_t max_allocation_size = 0x100000;
246 std::vector<std::pair<mirror::Object*, size_t> > requests;
247
248 for (size_t phase = 0; phase < 2; ++phase) {
249 while (requests.size() < num_allocations) {
250 size_t request_size = test_rand(&rand_seed) % max_allocation_size;
251 size_t allocation_size = 0;
252 mirror::Object* obj = los->Alloc(Thread::Current(), request_size, &allocation_size);
253 ASSERT_TRUE(obj != NULL);
254 ASSERT_EQ(allocation_size, los->AllocationSize(obj));
255 ASSERT_GE(allocation_size, request_size);
256 // Fill in our magic value.
257 byte magic = (request_size & 0xFF) | 1;
258 memset(obj, magic, request_size);
259 requests.push_back(std::make_pair(obj, request_size));
260 }
261
262 // "Randomly" shuffle the requests.
263 for (size_t k = 0; k < 10; ++k) {
264 for (size_t j = 0; j < requests.size(); ++j) {
265 std::swap(requests[j], requests[test_rand(&rand_seed) % requests.size()]);
266 }
267 }
268
269 // Free 1 / 2 the allocations the first phase, and all the second phase.
270 size_t limit = !phase ? requests.size() / 2 : 0;
271 while (requests.size() > limit) {
272 mirror::Object* obj = requests.back().first;
273 size_t request_size = requests.back().second;
274 requests.pop_back();
275 byte magic = (request_size & 0xFF) | 1;
276 for (size_t k = 0; k < request_size; ++k) {
277 ASSERT_EQ(reinterpret_cast<const byte*>(obj)[k], magic);
278 }
279 ASSERT_GE(los->Free(Thread::Current(), obj), request_size);
280 }
281 }
282
283 size_t bytes_allocated = 0;
284 // Checks that the coalescing works.
285 mirror::Object* obj = los->Alloc(Thread::Current(), 100 * MB, &bytes_allocated);
286 EXPECT_TRUE(obj != NULL);
287 los->Free(Thread::Current(), obj);
288
289 EXPECT_EQ(0U, los->GetBytesAllocated());
290 EXPECT_EQ(0U, los->GetObjectsAllocated());
291 delete los;
292 }
293}
294
Ian Rogers3bb17a62012-01-27 23:56:44 -0800295TEST_F(SpaceTest, AllocAndFreeList) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700296 DlMallocSpace* space(DlMallocSpace::Create("test", 4 * MB, 16 * MB, 16 * MB, NULL));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800297 ASSERT_TRUE(space != NULL);
298
299 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700300 AddSpace(space);
Ian Rogers50b35e22012-10-04 10:09:15 -0700301 Thread* self = Thread::Current();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800302
303 // Succeeds, fits without adjusting the max allowed footprint.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800304 mirror::Object* lots_of_objects[1024];
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700305 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700306 size_t allocation_size = 0;
307 lots_of_objects[i] = space->Alloc(self, 16, &allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800308 EXPECT_TRUE(lots_of_objects[i] != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700309 InstallClass(lots_of_objects[i], 16);
310 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i]));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800311 }
312
313 // Release memory and check pointers are NULL
Ian Rogers50b35e22012-10-04 10:09:15 -0700314 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700315 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800316 EXPECT_TRUE(lots_of_objects[i] == NULL);
317 }
318
319 // Succeeds, fits by adjusting the max allowed footprint.
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700320 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700321 size_t allocation_size = 0;
322 lots_of_objects[i] = space->AllocWithGrowth(self, 1024, &allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800323 EXPECT_TRUE(lots_of_objects[i] != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700324 InstallClass(lots_of_objects[i], 1024);
325 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i]));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800326 }
327
328 // Release memory and check pointers are NULL
Ian Rogers50b35e22012-10-04 10:09:15 -0700329 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700330 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800331 EXPECT_TRUE(lots_of_objects[i] == NULL);
332 }
333}
334
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700335void SpaceTest::SizeFootPrintGrowthLimitAndTrimBody(DlMallocSpace* space, intptr_t object_size,
Ian Rogers3bb17a62012-01-27 23:56:44 -0800336 int round, size_t growth_limit) {
337 if (((object_size > 0 && object_size >= static_cast<intptr_t>(growth_limit))) ||
338 ((object_size < 0 && -object_size >= static_cast<intptr_t>(growth_limit)))) {
339 // No allocation can succeed
340 return;
341 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800342
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700343 // The space's footprint equals amount of resources requested from system
344 size_t footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800345
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700346 // The space must at least have its book keeping allocated
Ian Rogers3bb17a62012-01-27 23:56:44 -0800347 EXPECT_GT(footprint, 0u);
348
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700349 // But it shouldn't exceed the initial size
Ian Rogers3bb17a62012-01-27 23:56:44 -0800350 EXPECT_LE(footprint, growth_limit);
351
352 // space's size shouldn't exceed the initial size
353 EXPECT_LE(space->Size(), growth_limit);
354
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700355 // this invariant should always hold or else the space has grown to be larger than what the
Ian Rogers3bb17a62012-01-27 23:56:44 -0800356 // space believes its size is (which will break invariants)
357 EXPECT_GE(space->Size(), footprint);
358
359 // Fill the space with lots of small objects up to the growth limit
360 size_t max_objects = (growth_limit / (object_size > 0 ? object_size : 8)) + 1;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800361 UniquePtr<mirror::Object*[]> lots_of_objects(new mirror::Object*[max_objects]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800362 size_t last_object = 0; // last object for which allocation succeeded
363 size_t amount_allocated = 0; // amount of space allocated
Ian Rogers50b35e22012-10-04 10:09:15 -0700364 Thread* self = Thread::Current();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700365 size_t rand_seed = 123456789;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700366 for (size_t i = 0; i < max_objects; i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800367 size_t alloc_fails = 0; // number of failed allocations
368 size_t max_fails = 30; // number of times we fail allocation before giving up
369 for (; alloc_fails < max_fails; alloc_fails++) {
370 size_t alloc_size;
371 if (object_size > 0) {
372 alloc_size = object_size;
373 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700374 alloc_size = test_rand(&rand_seed) % static_cast<size_t>(-object_size);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700375 // Note the minimum size, which is the size of a zero-length byte array, is 12.
376 if (alloc_size < 12) {
377 alloc_size = 12;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800378 }
379 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800380 mirror::Object* object;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700381 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800382 if (round <= 1) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700383 object = space->Alloc(self, alloc_size, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800384 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700385 object = space->AllocWithGrowth(self, alloc_size, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800386 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700387 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800388 EXPECT_GE(space->Size(), footprint); // invariant
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700389 if (object != NULL) { // allocation succeeded
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700390 InstallClass(object, alloc_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800391 lots_of_objects.get()[i] = object;
392 size_t allocation_size = space->AllocationSize(object);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700393 EXPECT_EQ(bytes_allocated, allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800394 if (object_size > 0) {
395 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
396 } else {
397 EXPECT_GE(allocation_size, 8u);
398 }
399 amount_allocated += allocation_size;
400 break;
401 }
402 }
403 if (alloc_fails == max_fails) {
404 last_object = i;
405 break;
406 }
407 }
408 CHECK_NE(last_object, 0u); // we should have filled the space
409 EXPECT_GT(amount_allocated, 0u);
410
411 // We shouldn't have gone past the growth_limit
412 EXPECT_LE(amount_allocated, growth_limit);
413 EXPECT_LE(footprint, growth_limit);
414 EXPECT_LE(space->Size(), growth_limit);
415
416 // footprint and size should agree with amount allocated
417 EXPECT_GE(footprint, amount_allocated);
418 EXPECT_GE(space->Size(), amount_allocated);
419
420 // Release storage in a semi-adhoc manner
421 size_t free_increment = 96;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700422 while (true) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800423 // Give the space a haircut
424 space->Trim();
425
426 // Bounds sanity
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700427 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800428 EXPECT_LE(amount_allocated, growth_limit);
429 EXPECT_GE(footprint, amount_allocated);
430 EXPECT_LE(footprint, growth_limit);
431 EXPECT_GE(space->Size(), amount_allocated);
432 EXPECT_LE(space->Size(), growth_limit);
433
434 if (free_increment == 0) {
435 break;
436 }
437
438 // Free some objects
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700439 for (size_t i = 0; i < last_object; i += free_increment) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800440 mirror::Object* object = lots_of_objects.get()[i];
Ian Rogers3bb17a62012-01-27 23:56:44 -0800441 if (object == NULL) {
442 continue;
443 }
444 size_t allocation_size = space->AllocationSize(object);
445 if (object_size > 0) {
446 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
447 } else {
448 EXPECT_GE(allocation_size, 8u);
449 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700450 space->Free(self, object);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800451 lots_of_objects.get()[i] = NULL;
452 amount_allocated -= allocation_size;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700453 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800454 EXPECT_GE(space->Size(), footprint); // invariant
455 }
456
457 free_increment >>= 1;
458 }
459
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700460 // The space has become empty here before allocating a large object
461 // below. For RosAlloc, revoke thread-local runs, which are kept
462 // even when empty for a performance reason, so that they won't
463 // cause the following large object allocation to fail due to
464 // potential fragmentation. Note they are normally revoked at each
465 // GC (but no GC here.)
466 space->RevokeAllThreadLocalBuffers();
467
Ian Rogers3bb17a62012-01-27 23:56:44 -0800468 // All memory was released, try a large allocation to check freed memory is being coalesced
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800469 mirror::Object* large_object;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800470 size_t three_quarters_space = (growth_limit / 2) + (growth_limit / 4);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700471 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800472 if (round <= 1) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700473 large_object = space->Alloc(self, three_quarters_space, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800474 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700475 large_object = space->AllocWithGrowth(self, three_quarters_space, &bytes_allocated);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800476 }
477 EXPECT_TRUE(large_object != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700478 InstallClass(large_object, three_quarters_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800479
480 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700481 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800482 EXPECT_LE(footprint, growth_limit);
483 EXPECT_GE(space->Size(), footprint);
484 EXPECT_LE(space->Size(), growth_limit);
485
486 // Clean up
Ian Rogers50b35e22012-10-04 10:09:15 -0700487 space->Free(self, large_object);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800488
489 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700490 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800491 EXPECT_LE(footprint, growth_limit);
492 EXPECT_GE(space->Size(), footprint);
493 EXPECT_LE(space->Size(), growth_limit);
494}
495
496void SpaceTest::SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size) {
497 size_t initial_size = 4 * MB;
498 size_t growth_limit = 8 * MB;
499 size_t capacity = 16 * MB;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700500 DlMallocSpace* space(DlMallocSpace::Create("test", initial_size, growth_limit, capacity, NULL));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800501 ASSERT_TRUE(space != NULL);
502
503 // Basic sanity
504 EXPECT_EQ(space->Capacity(), growth_limit);
505 EXPECT_EQ(space->NonGrowthLimitCapacity(), capacity);
506
507 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700508 AddSpace(space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800509
510 // In this round we don't allocate with growth and therefore can't grow past the initial size.
511 // This effectively makes the growth_limit the initial_size, so assert this.
512 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 1, initial_size);
513 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 2, growth_limit);
514 // Remove growth limit
515 space->ClearGrowthLimit();
516 EXPECT_EQ(space->Capacity(), capacity);
517 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 3, capacity);
518}
519
520#define TEST_SizeFootPrintGrowthLimitAndTrim(name, size) \
521 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name) { \
522 SizeFootPrintGrowthLimitAndTrimDriver(size); \
523 } \
524 TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name) { \
525 SizeFootPrintGrowthLimitAndTrimDriver(-size); \
526 }
527
528// Each size test is its own test so that we get a fresh heap each time
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700529TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_12B) {
530 SizeFootPrintGrowthLimitAndTrimDriver(12);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800531}
532TEST_SizeFootPrintGrowthLimitAndTrim(16B, 16)
533TEST_SizeFootPrintGrowthLimitAndTrim(24B, 24)
534TEST_SizeFootPrintGrowthLimitAndTrim(32B, 32)
535TEST_SizeFootPrintGrowthLimitAndTrim(64B, 64)
536TEST_SizeFootPrintGrowthLimitAndTrim(128B, 128)
537TEST_SizeFootPrintGrowthLimitAndTrim(1KB, 1 * KB)
538TEST_SizeFootPrintGrowthLimitAndTrim(4KB, 4 * KB)
539TEST_SizeFootPrintGrowthLimitAndTrim(1MB, 1 * MB)
540TEST_SizeFootPrintGrowthLimitAndTrim(4MB, 4 * MB)
541TEST_SizeFootPrintGrowthLimitAndTrim(8MB, 8 * MB)
542
Ian Rogers1d54e732013-05-02 21:10:01 -0700543} // namespace space
544} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700545} // namespace art