blob: 093967ead04a7e26a23abfe1cf94603a327ec2b8 [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
Andreas Gampea7433512014-02-21 13:19:23 -080017#ifndef ART_RUNTIME_GC_SPACE_SPACE_TEST_H_
18#define ART_RUNTIME_GC_SPACE_SPACE_TEST_H_
19
Mathieu Chartiera1602f22014-01-13 17:19:19 -080020#include "zygote_space.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070021
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070022#include "common_test.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070023#include "globals.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070024#include "UniquePtr.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070025#include "mirror/array-inl.h"
26#include "mirror/object-inl.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070027
Ian Rogers3bb17a62012-01-27 23:56:44 -080028#include <stdint.h>
29
Carl Shapiro69759ea2011-07-21 18:13:35 -070030namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070031namespace gc {
32namespace space {
Carl Shapiro69759ea2011-07-21 18:13:35 -070033
Ian Rogers3bb17a62012-01-27 23:56:44 -080034class SpaceTest : public CommonTest {
35 public:
Mathieu Chartier590fee92013-09-13 13:46:47 -070036 void AddSpace(ContinuousSpace* space) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070037 // For RosAlloc, revoke the thread local runs before moving onto a
38 // new alloc space.
39 Runtime::Current()->GetHeap()->RevokeAllThreadLocalBuffers();
Mathieu Chartier590fee92013-09-13 13:46:47 -070040 Runtime::Current()->GetHeap()->AddSpace(space);
Ian Rogers1d54e732013-05-02 21:10:01 -070041 }
Mathieu Chartier4e305412014-02-19 10:54:44 -080042 void InstallClass(SirtRef<mirror::Object>& o, size_t size)
43 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080044 // Note the minimum size, which is the size of a zero-length byte array.
45 EXPECT_GE(size, SizeOfZeroLengthByteArray());
Ian Rogers98379392014-02-24 16:53:16 -080046 Thread* self = Thread::Current();
47 SirtRef<mirror::ClassLoader> null_loader(self, nullptr);
48 mirror::Class* byte_array_class = Runtime::Current()->GetClassLinker()->FindClass(self, "[B",
Mathieu Chartier4e305412014-02-19 10:54:44 -080049 null_loader);
50 EXPECT_TRUE(byte_array_class != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070051 o->SetClass(byte_array_class);
Mathieu Chartier4e305412014-02-19 10:54:44 -080052 mirror::Array* arr = o->AsArray<kVerifyNone>();
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080053 size_t header_size = SizeOfZeroLengthByteArray();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070054 int32_t length = size - header_size;
55 arr->SetLength(length);
Mathieu Chartier4e305412014-02-19 10:54:44 -080056 EXPECT_EQ(arr->SizeOf<kVerifyNone>(), size);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070057 }
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080058
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080059 static size_t SizeOfZeroLengthByteArray() {
60 return mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimByte)).Uint32Value();
61 }
62
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080063 typedef MallocSpace* (*CreateSpaceFn)(const std::string& name, size_t initial_size, size_t growth_limit,
64 size_t capacity, byte* requested_begin);
65 void InitTestBody(CreateSpaceFn create_space);
66 void ZygoteSpaceTestBody(CreateSpaceFn create_space);
67 void AllocAndFreeTestBody(CreateSpaceFn create_space);
68 void AllocAndFreeListTestBody(CreateSpaceFn create_space);
69
70 void SizeFootPrintGrowthLimitAndTrimBody(MallocSpace* space, intptr_t object_size,
71 int round, size_t growth_limit);
72 void SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size, CreateSpaceFn create_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -080073};
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070074
Mathieu Chartiereb5710e2013-07-25 15:19:42 -070075static size_t test_rand(size_t* seed) {
76 *seed = *seed * 1103515245 + 12345;
77 return *seed;
78}
79
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080080void SpaceTest::InitTestBody(CreateSpaceFn create_space) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070081 {
jeffhaoc1160702011-10-27 15:48:45 -070082 // Init < max == growth
Mathieu Chartier4e305412014-02-19 10:54:44 -080083 UniquePtr<Space> space(create_space("test", 16 * MB, 32 * MB, 32 * MB, nullptr));
84 EXPECT_TRUE(space.get() != nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -070085 }
86 {
jeffhaoc1160702011-10-27 15:48:45 -070087 // Init == max == growth
Mathieu Chartier4e305412014-02-19 10:54:44 -080088 UniquePtr<Space> space(create_space("test", 16 * MB, 16 * MB, 16 * MB, nullptr));
89 EXPECT_TRUE(space.get() != nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -070090 }
91 {
jeffhaoc1160702011-10-27 15:48:45 -070092 // Init > max == growth
Mathieu Chartier4e305412014-02-19 10:54:44 -080093 UniquePtr<Space> space(create_space("test", 32 * MB, 16 * MB, 16 * MB, nullptr));
94 EXPECT_TRUE(space.get() == nullptr);
jeffhaoc1160702011-10-27 15:48:45 -070095 }
96 {
97 // Growth == init < max
Mathieu Chartier4e305412014-02-19 10:54:44 -080098 UniquePtr<Space> space(create_space("test", 16 * MB, 16 * MB, 32 * MB, nullptr));
99 EXPECT_TRUE(space.get() != nullptr);
jeffhaoc1160702011-10-27 15:48:45 -0700100 }
101 {
102 // Growth < init < max
Mathieu Chartier4e305412014-02-19 10:54:44 -0800103 UniquePtr<Space> space(create_space("test", 16 * MB, 8 * MB, 32 * MB, nullptr));
104 EXPECT_TRUE(space.get() == nullptr);
jeffhaoc1160702011-10-27 15:48:45 -0700105 }
106 {
107 // Init < growth < max
Mathieu Chartier4e305412014-02-19 10:54:44 -0800108 UniquePtr<Space> space(create_space("test", 8 * MB, 16 * MB, 32 * MB, nullptr));
109 EXPECT_TRUE(space.get() != nullptr);
jeffhaoc1160702011-10-27 15:48:45 -0700110 }
111 {
112 // Init < max < growth
Mathieu Chartier4e305412014-02-19 10:54:44 -0800113 UniquePtr<Space> space(create_space("test", 8 * MB, 32 * MB, 16 * MB, nullptr));
114 EXPECT_TRUE(space.get() == nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700115 }
116}
117
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700118// TODO: This test is not very good, we should improve it.
119// The test should do more allocations before the creation of the ZygoteSpace, and then do
120// allocations after the ZygoteSpace is created. The test should also do some GCs to ensure that
121// the GC works with the ZygoteSpace.
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800122void SpaceTest::ZygoteSpaceTestBody(CreateSpaceFn create_space) {
123 size_t dummy = 0;
Mathieu Chartier4e305412014-02-19 10:54:44 -0800124 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, nullptr));
125 ASSERT_TRUE(space != nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700126
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800127 // Make space findable to the heap, will also delete space when runtime is cleaned up
128 AddSpace(space);
129 Thread* self = Thread::Current();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800130 ScopedObjectAccess soa(self);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700131
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800132 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800133 SirtRef<mirror::Object> ptr1(self, space->Alloc(self, 1 * MB, &dummy));
134 EXPECT_TRUE(ptr1.get() != nullptr);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800135 InstallClass(ptr1, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700136
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800137 // Fails, requires a higher footprint limit.
138 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800139 EXPECT_TRUE(ptr2 == nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700140
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800141 // Succeeds, adjusts the footprint.
142 size_t ptr3_bytes_allocated;
Mathieu Chartier4e305412014-02-19 10:54:44 -0800143 SirtRef<mirror::Object> ptr3(self, space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated));
144 EXPECT_TRUE(ptr3.get() != nullptr);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800145 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
146 InstallClass(ptr3, 8 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700147
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800148 // Fails, requires a higher footprint limit.
149 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800150 EXPECT_TRUE(ptr4 == nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700151
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800152 // Also fails, requires a higher allowed footprint.
153 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800154 EXPECT_TRUE(ptr5 == nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700155
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800156 // Release some memory.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800157 size_t free3 = space->AllocationSize(ptr3.get());
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800158 EXPECT_EQ(free3, ptr3_bytes_allocated);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800159 EXPECT_EQ(free3, space->Free(self, ptr3.reset(nullptr)));
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800160 EXPECT_LE(8U * MB, free3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700161
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800162 // Succeeds, now that memory has been freed.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800163 SirtRef<mirror::Object> ptr6(self, space->AllocWithGrowth(self, 9 * MB, &dummy));
164 EXPECT_TRUE(ptr6.get() != nullptr);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800165 InstallClass(ptr6, 9 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700166
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800167 // Final clean up.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800168 size_t free1 = space->AllocationSize(ptr1.get());
169 space->Free(self, ptr1.reset(nullptr));
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800170 EXPECT_LE(1U * MB, free1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700171
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800172 // Make sure that the zygote space isn't directly at the start of the space.
173 space->Alloc(self, 1U * MB, &dummy);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800174
175 gc::Heap* heap = Runtime::Current()->GetHeap();
176 space::Space* old_space = space;
177 heap->RemoveSpace(old_space);
178 space::ZygoteSpace* zygote_space = space->CreateZygoteSpace("alloc space",
179 heap->IsLowMemoryMode(),
180 &space);
181 delete old_space;
182 // Add the zygote space.
183 AddSpace(zygote_space);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700184
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800185 // Make space findable to the heap, will also delete space when runtime is cleaned up
186 AddSpace(space);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700187
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800188 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800189 ptr1.reset(space->Alloc(self, 1 * MB, &dummy));
190 EXPECT_TRUE(ptr1.get() != nullptr);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800191 InstallClass(ptr1, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700192
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800193 // Fails, requires a higher footprint limit.
194 ptr2 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800195 EXPECT_TRUE(ptr2 == nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700196
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800197 // Succeeds, adjusts the footprint.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800198 ptr3.reset(space->AllocWithGrowth(self, 2 * MB, &dummy));
199 EXPECT_TRUE(ptr3.get() != nullptr);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800200 InstallClass(ptr3, 2 * MB);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800201 space->Free(self, ptr3.reset(nullptr));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700202
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800203 // Final clean up.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800204 free1 = space->AllocationSize(ptr1.get());
205 space->Free(self, ptr1.reset(nullptr));
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800206 EXPECT_LE(1U * MB, free1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700207}
208
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800209void SpaceTest::AllocAndFreeTestBody(CreateSpaceFn create_space) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700210 size_t dummy = 0;
Mathieu Chartier4e305412014-02-19 10:54:44 -0800211 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, nullptr));
212 ASSERT_TRUE(space != nullptr);
Ian Rogers50b35e22012-10-04 10:09:15 -0700213 Thread* self = Thread::Current();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800214 ScopedObjectAccess soa(self);
Ian Rogers30fab402012-01-23 15:43:46 -0800215
Ian Rogers3bb17a62012-01-27 23:56:44 -0800216 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700217 AddSpace(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700218
Ian Rogers3bb17a62012-01-27 23:56:44 -0800219 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800220 SirtRef<mirror::Object> ptr1(self, space->Alloc(self, 1 * MB, &dummy));
221 EXPECT_TRUE(ptr1.get() != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700222 InstallClass(ptr1, 1 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700223
Ian Rogers3bb17a62012-01-27 23:56:44 -0800224 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700225 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800226 EXPECT_TRUE(ptr2 == nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700227
228 // Succeeds, adjusts the footprint.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700229 size_t ptr3_bytes_allocated;
Mathieu Chartier4e305412014-02-19 10:54:44 -0800230 SirtRef<mirror::Object> ptr3(self, space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated));
231 EXPECT_TRUE(ptr3.get() != nullptr);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700232 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700233 InstallClass(ptr3, 8 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700234
Ian Rogers3bb17a62012-01-27 23:56:44 -0800235 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700236 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800237 EXPECT_TRUE(ptr4 == nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700238
239 // Also fails, requires a higher allowed footprint.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700240 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800241 EXPECT_TRUE(ptr5 == nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700242
243 // Release some memory.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800244 size_t free3 = space->AllocationSize(ptr3.get());
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700245 EXPECT_EQ(free3, ptr3_bytes_allocated);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800246 space->Free(self, ptr3.reset(nullptr));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700247 EXPECT_LE(8U * MB, free3);
248
249 // Succeeds, now that memory has been freed.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800250 SirtRef<mirror::Object> ptr6(self, space->AllocWithGrowth(self, 9 * MB, &dummy));
251 EXPECT_TRUE(ptr6.get() != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700252 InstallClass(ptr6, 9 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700253
254 // Final clean up.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800255 size_t free1 = space->AllocationSize(ptr1.get());
256 space->Free(self, ptr1.reset(nullptr));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700257 EXPECT_LE(1U * MB, free1);
258}
259
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800260void SpaceTest::AllocAndFreeListTestBody(CreateSpaceFn create_space) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800261 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, nullptr));
262 ASSERT_TRUE(space != nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800263
264 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700265 AddSpace(space);
Ian Rogers50b35e22012-10-04 10:09:15 -0700266 Thread* self = Thread::Current();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800267 ScopedObjectAccess soa(self);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800268
269 // Succeeds, fits without adjusting the max allowed footprint.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800270 mirror::Object* lots_of_objects[1024];
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700271 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700272 size_t allocation_size = 0;
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800273 size_t size_of_zero_length_byte_array = SizeOfZeroLengthByteArray();
274 lots_of_objects[i] = space->Alloc(self, size_of_zero_length_byte_array, &allocation_size);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800275 EXPECT_TRUE(lots_of_objects[i] != nullptr);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800276 SirtRef<mirror::Object> obj(self, lots_of_objects[i]);
277 InstallClass(obj, size_of_zero_length_byte_array);
278 lots_of_objects[i] = obj.get();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700279 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i]));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800280 }
281
Mathieu Chartier4e305412014-02-19 10:54:44 -0800282 // Release memory and check pointers are nullptr.
283 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
284 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
285 EXPECT_TRUE(lots_of_objects[i] == nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800286 }
287
288 // Succeeds, fits by adjusting the max allowed footprint.
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700289 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700290 size_t allocation_size = 0;
291 lots_of_objects[i] = space->AllocWithGrowth(self, 1024, &allocation_size);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800292 EXPECT_TRUE(lots_of_objects[i] != nullptr);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800293 SirtRef<mirror::Object> obj(self, lots_of_objects[i]);
294 InstallClass(obj, 1024);
295 lots_of_objects[i] = obj.get();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700296 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i]));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800297 }
298
Mathieu Chartier4e305412014-02-19 10:54:44 -0800299 // Release memory and check pointers are nullptr
300 // TODO: This isn't compaction safe, fix.
301 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
302 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
303 EXPECT_TRUE(lots_of_objects[i] == nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800304 }
305}
306
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800307void SpaceTest::SizeFootPrintGrowthLimitAndTrimBody(MallocSpace* space, intptr_t object_size,
Ian Rogers3bb17a62012-01-27 23:56:44 -0800308 int round, size_t growth_limit) {
309 if (((object_size > 0 && object_size >= static_cast<intptr_t>(growth_limit))) ||
310 ((object_size < 0 && -object_size >= static_cast<intptr_t>(growth_limit)))) {
311 // No allocation can succeed
312 return;
313 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800314
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700315 // The space's footprint equals amount of resources requested from system
316 size_t footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800317
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700318 // The space must at least have its book keeping allocated
Ian Rogers3bb17a62012-01-27 23:56:44 -0800319 EXPECT_GT(footprint, 0u);
320
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700321 // But it shouldn't exceed the initial size
Ian Rogers3bb17a62012-01-27 23:56:44 -0800322 EXPECT_LE(footprint, growth_limit);
323
324 // space's size shouldn't exceed the initial size
325 EXPECT_LE(space->Size(), growth_limit);
326
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700327 // this invariant should always hold or else the space has grown to be larger than what the
Ian Rogers3bb17a62012-01-27 23:56:44 -0800328 // space believes its size is (which will break invariants)
329 EXPECT_GE(space->Size(), footprint);
330
331 // Fill the space with lots of small objects up to the growth limit
332 size_t max_objects = (growth_limit / (object_size > 0 ? object_size : 8)) + 1;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800333 UniquePtr<mirror::Object*[]> lots_of_objects(new mirror::Object*[max_objects]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800334 size_t last_object = 0; // last object for which allocation succeeded
335 size_t amount_allocated = 0; // amount of space allocated
Ian Rogers50b35e22012-10-04 10:09:15 -0700336 Thread* self = Thread::Current();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800337 ScopedObjectAccess soa(self);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700338 size_t rand_seed = 123456789;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700339 for (size_t i = 0; i < max_objects; i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800340 size_t alloc_fails = 0; // number of failed allocations
341 size_t max_fails = 30; // number of times we fail allocation before giving up
342 for (; alloc_fails < max_fails; alloc_fails++) {
343 size_t alloc_size;
344 if (object_size > 0) {
345 alloc_size = object_size;
346 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700347 alloc_size = test_rand(&rand_seed) % static_cast<size_t>(-object_size);
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800348 // Note the minimum size, which is the size of a zero-length byte array.
349 size_t size_of_zero_length_byte_array = SizeOfZeroLengthByteArray();
350 if (alloc_size < size_of_zero_length_byte_array) {
351 alloc_size = size_of_zero_length_byte_array;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800352 }
353 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800354 SirtRef<mirror::Object> object(self, nullptr);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700355 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800356 if (round <= 1) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800357 object.reset(space->Alloc(self, alloc_size, &bytes_allocated));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800358 } else {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800359 object.reset(space->AllocWithGrowth(self, alloc_size, &bytes_allocated));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800360 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700361 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800362 EXPECT_GE(space->Size(), footprint); // invariant
Mathieu Chartier4e305412014-02-19 10:54:44 -0800363 if (object.get() != nullptr) { // allocation succeeded
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700364 InstallClass(object, alloc_size);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800365 lots_of_objects[i] = object.get();
366 size_t allocation_size = space->AllocationSize(object.get());
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700367 EXPECT_EQ(bytes_allocated, allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800368 if (object_size > 0) {
369 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
370 } else {
371 EXPECT_GE(allocation_size, 8u);
372 }
373 amount_allocated += allocation_size;
374 break;
375 }
376 }
377 if (alloc_fails == max_fails) {
378 last_object = i;
379 break;
380 }
381 }
382 CHECK_NE(last_object, 0u); // we should have filled the space
383 EXPECT_GT(amount_allocated, 0u);
384
385 // We shouldn't have gone past the growth_limit
386 EXPECT_LE(amount_allocated, growth_limit);
387 EXPECT_LE(footprint, growth_limit);
388 EXPECT_LE(space->Size(), growth_limit);
389
390 // footprint and size should agree with amount allocated
391 EXPECT_GE(footprint, amount_allocated);
392 EXPECT_GE(space->Size(), amount_allocated);
393
394 // Release storage in a semi-adhoc manner
395 size_t free_increment = 96;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700396 while (true) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800397 {
398 ScopedThreadStateChange tsc(self, kNative);
399 // Give the space a haircut.
400 space->Trim();
401 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800402
403 // Bounds sanity
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700404 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800405 EXPECT_LE(amount_allocated, growth_limit);
406 EXPECT_GE(footprint, amount_allocated);
407 EXPECT_LE(footprint, growth_limit);
408 EXPECT_GE(space->Size(), amount_allocated);
409 EXPECT_LE(space->Size(), growth_limit);
410
411 if (free_increment == 0) {
412 break;
413 }
414
Mathieu Chartier4e305412014-02-19 10:54:44 -0800415 // Free some objects
416 for (size_t i = 0; i < last_object; i += free_increment) {
417 mirror::Object* object = lots_of_objects.get()[i];
418 if (object == nullptr) {
419 continue;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800420 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800421 size_t allocation_size = space->AllocationSize(object);
422 if (object_size > 0) {
423 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
424 } else {
425 EXPECT_GE(allocation_size, 8u);
426 }
427 space->Free(self, object);
428 lots_of_objects.get()[i] = nullptr;
429 amount_allocated -= allocation_size;
430 footprint = space->GetFootprint();
431 EXPECT_GE(space->Size(), footprint); // invariant
Ian Rogers3bb17a62012-01-27 23:56:44 -0800432 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800433
434 free_increment >>= 1;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800435 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800436
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700437 // The space has become empty here before allocating a large object
438 // below. For RosAlloc, revoke thread-local runs, which are kept
439 // even when empty for a performance reason, so that they won't
440 // cause the following large object allocation to fail due to
441 // potential fragmentation. Note they are normally revoked at each
442 // GC (but no GC here.)
443 space->RevokeAllThreadLocalBuffers();
444
Ian Rogers3bb17a62012-01-27 23:56:44 -0800445 // All memory was released, try a large allocation to check freed memory is being coalesced
Mathieu Chartier4e305412014-02-19 10:54:44 -0800446 SirtRef<mirror::Object> large_object(self, nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800447 size_t three_quarters_space = (growth_limit / 2) + (growth_limit / 4);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700448 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800449 if (round <= 1) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800450 large_object.reset(space->Alloc(self, three_quarters_space, &bytes_allocated));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800451 } else {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800452 large_object.reset(space->AllocWithGrowth(self, three_quarters_space, &bytes_allocated));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800453 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800454 EXPECT_TRUE(large_object.get() != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700455 InstallClass(large_object, three_quarters_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800456
457 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700458 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800459 EXPECT_LE(footprint, growth_limit);
460 EXPECT_GE(space->Size(), footprint);
461 EXPECT_LE(space->Size(), growth_limit);
462
463 // Clean up
Mathieu Chartier4e305412014-02-19 10:54:44 -0800464 space->Free(self, large_object.reset(nullptr));
465
Ian Rogers3bb17a62012-01-27 23:56:44 -0800466 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700467 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800468 EXPECT_LE(footprint, growth_limit);
469 EXPECT_GE(space->Size(), footprint);
470 EXPECT_LE(space->Size(), growth_limit);
471}
472
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800473void SpaceTest::SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size, CreateSpaceFn create_space) {
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800474 if (object_size < SizeOfZeroLengthByteArray()) {
475 // Too small for the object layout/model.
476 return;
477 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800478 size_t initial_size = 4 * MB;
479 size_t growth_limit = 8 * MB;
480 size_t capacity = 16 * MB;
Mathieu Chartier4e305412014-02-19 10:54:44 -0800481 MallocSpace* space(create_space("test", initial_size, growth_limit, capacity, nullptr));
482 ASSERT_TRUE(space != nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800483
484 // Basic sanity
485 EXPECT_EQ(space->Capacity(), growth_limit);
486 EXPECT_EQ(space->NonGrowthLimitCapacity(), capacity);
487
488 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700489 AddSpace(space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800490
491 // In this round we don't allocate with growth and therefore can't grow past the initial size.
492 // This effectively makes the growth_limit the initial_size, so assert this.
493 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 1, initial_size);
494 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 2, growth_limit);
495 // Remove growth limit
496 space->ClearGrowthLimit();
497 EXPECT_EQ(space->Capacity(), capacity);
498 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 3, capacity);
499}
500
Andreas Gampea7433512014-02-21 13:19:23 -0800501#define TEST_SizeFootPrintGrowthLimitAndTrim(name, spaceName, spaceFn, size) \
502 TEST_F(spaceName##Test, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name) { \
503 SizeFootPrintGrowthLimitAndTrimDriver(size, spaceFn); \
Ian Rogers3bb17a62012-01-27 23:56:44 -0800504 } \
Andreas Gampea7433512014-02-21 13:19:23 -0800505 TEST_F(spaceName##Test, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name) { \
506 SizeFootPrintGrowthLimitAndTrimDriver(-size, spaceFn); \
Ian Rogers3bb17a62012-01-27 23:56:44 -0800507 }
508
Andreas Gampea7433512014-02-21 13:19:23 -0800509#define TEST_SPACE_CREATE_FN(spaceName, spaceFn) \
510 class spaceName##Test : public SpaceTest { \
511 }; \
512 \
513 TEST_F(spaceName##Test, Init) { \
514 InitTestBody(spaceFn); \
515 } \
516 TEST_F(spaceName##Test, ZygoteSpace) { \
517 ZygoteSpaceTestBody(spaceFn); \
518 } \
519 TEST_F(spaceName##Test, AllocAndFree) { \
520 AllocAndFreeTestBody(spaceFn); \
521 } \
522 TEST_F(spaceName##Test, AllocAndFreeList) { \
523 AllocAndFreeListTestBody(spaceFn); \
524 } \
525 TEST_F(spaceName##Test, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_12B) { \
526 SizeFootPrintGrowthLimitAndTrimDriver(12, spaceFn); \
527 } \
528 TEST_SizeFootPrintGrowthLimitAndTrim(16B, spaceName, spaceFn, 16) \
529 TEST_SizeFootPrintGrowthLimitAndTrim(24B, spaceName, spaceFn, 24) \
530 TEST_SizeFootPrintGrowthLimitAndTrim(32B, spaceName, spaceFn, 32) \
531 TEST_SizeFootPrintGrowthLimitAndTrim(64B, spaceName, spaceFn, 64) \
532 TEST_SizeFootPrintGrowthLimitAndTrim(128B, spaceName, spaceFn, 128) \
533 TEST_SizeFootPrintGrowthLimitAndTrim(1KB, spaceName, spaceFn, 1 * KB) \
534 TEST_SizeFootPrintGrowthLimitAndTrim(4KB, spaceName, spaceFn, 4 * KB) \
535 TEST_SizeFootPrintGrowthLimitAndTrim(1MB, spaceName, spaceFn, 1 * MB) \
536 TEST_SizeFootPrintGrowthLimitAndTrim(4MB, spaceName, spaceFn, 4 * MB) \
537 TEST_SizeFootPrintGrowthLimitAndTrim(8MB, spaceName, spaceFn, 8 * MB)
Ian Rogers3bb17a62012-01-27 23:56:44 -0800538
Ian Rogers1d54e732013-05-02 21:10:01 -0700539} // namespace space
540} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700541} // namespace art
Andreas Gampea7433512014-02-21 13:19:23 -0800542
543#endif // ART_RUNTIME_GC_SPACE_SPACE_TEST_H_