blob: 41077f39fb49576d2273153a7c0c1843a354d764 [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);
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -080052 if (kUseBrooksPointer) {
53 o->SetBrooksPointer(o.get());
54 }
Mathieu Chartier4e305412014-02-19 10:54:44 -080055 mirror::Array* arr = o->AsArray<kVerifyNone>();
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080056 size_t header_size = SizeOfZeroLengthByteArray();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070057 int32_t length = size - header_size;
58 arr->SetLength(length);
Mathieu Chartier4e305412014-02-19 10:54:44 -080059 EXPECT_EQ(arr->SizeOf<kVerifyNone>(), size);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070060 }
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080061
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080062 static size_t SizeOfZeroLengthByteArray() {
63 return mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimByte)).Uint32Value();
64 }
65
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080066 typedef MallocSpace* (*CreateSpaceFn)(const std::string& name, size_t initial_size, size_t growth_limit,
67 size_t capacity, byte* requested_begin);
68 void InitTestBody(CreateSpaceFn create_space);
69 void ZygoteSpaceTestBody(CreateSpaceFn create_space);
70 void AllocAndFreeTestBody(CreateSpaceFn create_space);
71 void AllocAndFreeListTestBody(CreateSpaceFn create_space);
72
73 void SizeFootPrintGrowthLimitAndTrimBody(MallocSpace* space, intptr_t object_size,
74 int round, size_t growth_limit);
75 void SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size, CreateSpaceFn create_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -080076};
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070077
Mathieu Chartiereb5710e2013-07-25 15:19:42 -070078static size_t test_rand(size_t* seed) {
79 *seed = *seed * 1103515245 + 12345;
80 return *seed;
81}
82
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080083void SpaceTest::InitTestBody(CreateSpaceFn create_space) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070084 {
jeffhaoc1160702011-10-27 15:48:45 -070085 // Init < max == growth
Mathieu Chartier4e305412014-02-19 10:54:44 -080086 UniquePtr<Space> space(create_space("test", 16 * MB, 32 * MB, 32 * MB, nullptr));
87 EXPECT_TRUE(space.get() != nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -070088 }
89 {
jeffhaoc1160702011-10-27 15:48:45 -070090 // Init == max == growth
Mathieu Chartier4e305412014-02-19 10:54:44 -080091 UniquePtr<Space> space(create_space("test", 16 * MB, 16 * MB, 16 * MB, nullptr));
92 EXPECT_TRUE(space.get() != nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -070093 }
94 {
jeffhaoc1160702011-10-27 15:48:45 -070095 // Init > max == growth
Mathieu Chartier4e305412014-02-19 10:54:44 -080096 UniquePtr<Space> space(create_space("test", 32 * MB, 16 * MB, 16 * MB, nullptr));
97 EXPECT_TRUE(space.get() == nullptr);
jeffhaoc1160702011-10-27 15:48:45 -070098 }
99 {
100 // Growth == init < max
Mathieu Chartier4e305412014-02-19 10:54:44 -0800101 UniquePtr<Space> space(create_space("test", 16 * MB, 16 * MB, 32 * MB, nullptr));
102 EXPECT_TRUE(space.get() != nullptr);
jeffhaoc1160702011-10-27 15:48:45 -0700103 }
104 {
105 // Growth < init < max
Mathieu Chartier4e305412014-02-19 10:54:44 -0800106 UniquePtr<Space> space(create_space("test", 16 * MB, 8 * MB, 32 * MB, nullptr));
107 EXPECT_TRUE(space.get() == nullptr);
jeffhaoc1160702011-10-27 15:48:45 -0700108 }
109 {
110 // Init < growth < max
Mathieu Chartier4e305412014-02-19 10:54:44 -0800111 UniquePtr<Space> space(create_space("test", 8 * MB, 16 * MB, 32 * MB, nullptr));
112 EXPECT_TRUE(space.get() != nullptr);
jeffhaoc1160702011-10-27 15:48:45 -0700113 }
114 {
115 // Init < max < growth
Mathieu Chartier4e305412014-02-19 10:54:44 -0800116 UniquePtr<Space> space(create_space("test", 8 * MB, 32 * MB, 16 * MB, nullptr));
117 EXPECT_TRUE(space.get() == nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700118 }
119}
120
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700121// TODO: This test is not very good, we should improve it.
122// The test should do more allocations before the creation of the ZygoteSpace, and then do
123// allocations after the ZygoteSpace is created. The test should also do some GCs to ensure that
124// the GC works with the ZygoteSpace.
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800125void SpaceTest::ZygoteSpaceTestBody(CreateSpaceFn create_space) {
126 size_t dummy = 0;
Mathieu Chartier4e305412014-02-19 10:54:44 -0800127 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, nullptr));
128 ASSERT_TRUE(space != nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700129
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800130 // Make space findable to the heap, will also delete space when runtime is cleaned up
131 AddSpace(space);
132 Thread* self = Thread::Current();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800133 ScopedObjectAccess soa(self);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700134
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800135 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800136 SirtRef<mirror::Object> ptr1(self, space->Alloc(self, 1 * MB, &dummy));
137 EXPECT_TRUE(ptr1.get() != nullptr);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800138 InstallClass(ptr1, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700139
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800140 // Fails, requires a higher footprint limit.
141 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800142 EXPECT_TRUE(ptr2 == nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700143
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800144 // Succeeds, adjusts the footprint.
145 size_t ptr3_bytes_allocated;
Mathieu Chartier4e305412014-02-19 10:54:44 -0800146 SirtRef<mirror::Object> ptr3(self, space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated));
147 EXPECT_TRUE(ptr3.get() != nullptr);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800148 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
149 InstallClass(ptr3, 8 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700150
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800151 // Fails, requires a higher footprint limit.
152 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800153 EXPECT_TRUE(ptr4 == nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700154
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800155 // Also fails, requires a higher allowed footprint.
156 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800157 EXPECT_TRUE(ptr5 == nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700158
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800159 // Release some memory.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800160 size_t free3 = space->AllocationSize(ptr3.get());
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800161 EXPECT_EQ(free3, ptr3_bytes_allocated);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800162 EXPECT_EQ(free3, space->Free(self, ptr3.reset(nullptr)));
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800163 EXPECT_LE(8U * MB, free3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700164
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800165 // Succeeds, now that memory has been freed.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800166 SirtRef<mirror::Object> ptr6(self, space->AllocWithGrowth(self, 9 * MB, &dummy));
167 EXPECT_TRUE(ptr6.get() != nullptr);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800168 InstallClass(ptr6, 9 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700169
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800170 // Final clean up.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800171 size_t free1 = space->AllocationSize(ptr1.get());
172 space->Free(self, ptr1.reset(nullptr));
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800173 EXPECT_LE(1U * MB, free1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700174
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800175 // Make sure that the zygote space isn't directly at the start of the space.
176 space->Alloc(self, 1U * MB, &dummy);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800177
178 gc::Heap* heap = Runtime::Current()->GetHeap();
179 space::Space* old_space = space;
180 heap->RemoveSpace(old_space);
181 space::ZygoteSpace* zygote_space = space->CreateZygoteSpace("alloc space",
182 heap->IsLowMemoryMode(),
183 &space);
184 delete old_space;
185 // Add the zygote space.
186 AddSpace(zygote_space);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700187
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800188 // Make space findable to the heap, will also delete space when runtime is cleaned up
189 AddSpace(space);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700190
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800191 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800192 ptr1.reset(space->Alloc(self, 1 * MB, &dummy));
193 EXPECT_TRUE(ptr1.get() != nullptr);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800194 InstallClass(ptr1, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700195
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800196 // Fails, requires a higher footprint limit.
197 ptr2 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800198 EXPECT_TRUE(ptr2 == nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700199
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800200 // Succeeds, adjusts the footprint.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800201 ptr3.reset(space->AllocWithGrowth(self, 2 * MB, &dummy));
202 EXPECT_TRUE(ptr3.get() != nullptr);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800203 InstallClass(ptr3, 2 * MB);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800204 space->Free(self, ptr3.reset(nullptr));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700205
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800206 // Final clean up.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800207 free1 = space->AllocationSize(ptr1.get());
208 space->Free(self, ptr1.reset(nullptr));
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800209 EXPECT_LE(1U * MB, free1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700210}
211
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800212void SpaceTest::AllocAndFreeTestBody(CreateSpaceFn create_space) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700213 size_t dummy = 0;
Mathieu Chartier4e305412014-02-19 10:54:44 -0800214 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, nullptr));
215 ASSERT_TRUE(space != nullptr);
Ian Rogers50b35e22012-10-04 10:09:15 -0700216 Thread* self = Thread::Current();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800217 ScopedObjectAccess soa(self);
Ian Rogers30fab402012-01-23 15:43:46 -0800218
Ian Rogers3bb17a62012-01-27 23:56:44 -0800219 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700220 AddSpace(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700221
Ian Rogers3bb17a62012-01-27 23:56:44 -0800222 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800223 SirtRef<mirror::Object> ptr1(self, space->Alloc(self, 1 * MB, &dummy));
224 EXPECT_TRUE(ptr1.get() != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700225 InstallClass(ptr1, 1 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700226
Ian Rogers3bb17a62012-01-27 23:56:44 -0800227 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700228 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800229 EXPECT_TRUE(ptr2 == nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700230
231 // Succeeds, adjusts the footprint.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700232 size_t ptr3_bytes_allocated;
Mathieu Chartier4e305412014-02-19 10:54:44 -0800233 SirtRef<mirror::Object> ptr3(self, space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated));
234 EXPECT_TRUE(ptr3.get() != nullptr);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700235 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700236 InstallClass(ptr3, 8 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700237
Ian Rogers3bb17a62012-01-27 23:56:44 -0800238 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700239 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800240 EXPECT_TRUE(ptr4 == nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700241
242 // Also fails, requires a higher allowed footprint.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700243 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800244 EXPECT_TRUE(ptr5 == nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700245
246 // Release some memory.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800247 size_t free3 = space->AllocationSize(ptr3.get());
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700248 EXPECT_EQ(free3, ptr3_bytes_allocated);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800249 space->Free(self, ptr3.reset(nullptr));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700250 EXPECT_LE(8U * MB, free3);
251
252 // Succeeds, now that memory has been freed.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800253 SirtRef<mirror::Object> ptr6(self, space->AllocWithGrowth(self, 9 * MB, &dummy));
254 EXPECT_TRUE(ptr6.get() != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700255 InstallClass(ptr6, 9 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700256
257 // Final clean up.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800258 size_t free1 = space->AllocationSize(ptr1.get());
259 space->Free(self, ptr1.reset(nullptr));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700260 EXPECT_LE(1U * MB, free1);
261}
262
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800263void SpaceTest::AllocAndFreeListTestBody(CreateSpaceFn create_space) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800264 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, nullptr));
265 ASSERT_TRUE(space != nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800266
267 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700268 AddSpace(space);
Ian Rogers50b35e22012-10-04 10:09:15 -0700269 Thread* self = Thread::Current();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800270 ScopedObjectAccess soa(self);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800271
272 // Succeeds, fits without adjusting the max allowed footprint.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800273 mirror::Object* lots_of_objects[1024];
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700274 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700275 size_t allocation_size = 0;
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800276 size_t size_of_zero_length_byte_array = SizeOfZeroLengthByteArray();
277 lots_of_objects[i] = space->Alloc(self, size_of_zero_length_byte_array, &allocation_size);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800278 EXPECT_TRUE(lots_of_objects[i] != nullptr);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800279 SirtRef<mirror::Object> obj(self, lots_of_objects[i]);
280 InstallClass(obj, size_of_zero_length_byte_array);
281 lots_of_objects[i] = obj.get();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700282 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i]));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800283 }
284
Mathieu Chartier4e305412014-02-19 10:54:44 -0800285 // Release memory and check pointers are nullptr.
286 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
287 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
288 EXPECT_TRUE(lots_of_objects[i] == nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800289 }
290
291 // Succeeds, fits by adjusting the max allowed footprint.
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700292 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700293 size_t allocation_size = 0;
294 lots_of_objects[i] = space->AllocWithGrowth(self, 1024, &allocation_size);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800295 EXPECT_TRUE(lots_of_objects[i] != nullptr);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800296 SirtRef<mirror::Object> obj(self, lots_of_objects[i]);
297 InstallClass(obj, 1024);
298 lots_of_objects[i] = obj.get();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700299 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i]));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800300 }
301
Mathieu Chartier4e305412014-02-19 10:54:44 -0800302 // Release memory and check pointers are nullptr
303 // TODO: This isn't compaction safe, fix.
304 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
305 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
306 EXPECT_TRUE(lots_of_objects[i] == nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800307 }
308}
309
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800310void SpaceTest::SizeFootPrintGrowthLimitAndTrimBody(MallocSpace* space, intptr_t object_size,
Ian Rogers3bb17a62012-01-27 23:56:44 -0800311 int round, size_t growth_limit) {
312 if (((object_size > 0 && object_size >= static_cast<intptr_t>(growth_limit))) ||
313 ((object_size < 0 && -object_size >= static_cast<intptr_t>(growth_limit)))) {
314 // No allocation can succeed
315 return;
316 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800317
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700318 // The space's footprint equals amount of resources requested from system
319 size_t footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800320
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700321 // The space must at least have its book keeping allocated
Ian Rogers3bb17a62012-01-27 23:56:44 -0800322 EXPECT_GT(footprint, 0u);
323
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700324 // But it shouldn't exceed the initial size
Ian Rogers3bb17a62012-01-27 23:56:44 -0800325 EXPECT_LE(footprint, growth_limit);
326
327 // space's size shouldn't exceed the initial size
328 EXPECT_LE(space->Size(), growth_limit);
329
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700330 // this invariant should always hold or else the space has grown to be larger than what the
Ian Rogers3bb17a62012-01-27 23:56:44 -0800331 // space believes its size is (which will break invariants)
332 EXPECT_GE(space->Size(), footprint);
333
334 // Fill the space with lots of small objects up to the growth limit
335 size_t max_objects = (growth_limit / (object_size > 0 ? object_size : 8)) + 1;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800336 UniquePtr<mirror::Object*[]> lots_of_objects(new mirror::Object*[max_objects]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800337 size_t last_object = 0; // last object for which allocation succeeded
338 size_t amount_allocated = 0; // amount of space allocated
Ian Rogers50b35e22012-10-04 10:09:15 -0700339 Thread* self = Thread::Current();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800340 ScopedObjectAccess soa(self);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700341 size_t rand_seed = 123456789;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700342 for (size_t i = 0; i < max_objects; i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800343 size_t alloc_fails = 0; // number of failed allocations
344 size_t max_fails = 30; // number of times we fail allocation before giving up
345 for (; alloc_fails < max_fails; alloc_fails++) {
346 size_t alloc_size;
347 if (object_size > 0) {
348 alloc_size = object_size;
349 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700350 alloc_size = test_rand(&rand_seed) % static_cast<size_t>(-object_size);
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800351 // Note the minimum size, which is the size of a zero-length byte array.
352 size_t size_of_zero_length_byte_array = SizeOfZeroLengthByteArray();
353 if (alloc_size < size_of_zero_length_byte_array) {
354 alloc_size = size_of_zero_length_byte_array;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800355 }
356 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800357 SirtRef<mirror::Object> object(self, nullptr);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700358 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800359 if (round <= 1) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800360 object.reset(space->Alloc(self, alloc_size, &bytes_allocated));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800361 } else {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800362 object.reset(space->AllocWithGrowth(self, alloc_size, &bytes_allocated));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800363 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700364 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800365 EXPECT_GE(space->Size(), footprint); // invariant
Mathieu Chartier4e305412014-02-19 10:54:44 -0800366 if (object.get() != nullptr) { // allocation succeeded
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700367 InstallClass(object, alloc_size);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800368 lots_of_objects[i] = object.get();
369 size_t allocation_size = space->AllocationSize(object.get());
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700370 EXPECT_EQ(bytes_allocated, allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800371 if (object_size > 0) {
372 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
373 } else {
374 EXPECT_GE(allocation_size, 8u);
375 }
376 amount_allocated += allocation_size;
377 break;
378 }
379 }
380 if (alloc_fails == max_fails) {
381 last_object = i;
382 break;
383 }
384 }
385 CHECK_NE(last_object, 0u); // we should have filled the space
386 EXPECT_GT(amount_allocated, 0u);
387
388 // We shouldn't have gone past the growth_limit
389 EXPECT_LE(amount_allocated, growth_limit);
390 EXPECT_LE(footprint, growth_limit);
391 EXPECT_LE(space->Size(), growth_limit);
392
393 // footprint and size should agree with amount allocated
394 EXPECT_GE(footprint, amount_allocated);
395 EXPECT_GE(space->Size(), amount_allocated);
396
397 // Release storage in a semi-adhoc manner
398 size_t free_increment = 96;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700399 while (true) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800400 {
401 ScopedThreadStateChange tsc(self, kNative);
402 // Give the space a haircut.
403 space->Trim();
404 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800405
406 // Bounds sanity
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700407 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800408 EXPECT_LE(amount_allocated, growth_limit);
409 EXPECT_GE(footprint, amount_allocated);
410 EXPECT_LE(footprint, growth_limit);
411 EXPECT_GE(space->Size(), amount_allocated);
412 EXPECT_LE(space->Size(), growth_limit);
413
414 if (free_increment == 0) {
415 break;
416 }
417
Mathieu Chartier4e305412014-02-19 10:54:44 -0800418 // Free some objects
419 for (size_t i = 0; i < last_object; i += free_increment) {
420 mirror::Object* object = lots_of_objects.get()[i];
421 if (object == nullptr) {
422 continue;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800423 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800424 size_t allocation_size = space->AllocationSize(object);
425 if (object_size > 0) {
426 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
427 } else {
428 EXPECT_GE(allocation_size, 8u);
429 }
430 space->Free(self, object);
431 lots_of_objects.get()[i] = nullptr;
432 amount_allocated -= allocation_size;
433 footprint = space->GetFootprint();
434 EXPECT_GE(space->Size(), footprint); // invariant
Ian Rogers3bb17a62012-01-27 23:56:44 -0800435 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800436
437 free_increment >>= 1;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800438 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800439
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700440 // The space has become empty here before allocating a large object
441 // below. For RosAlloc, revoke thread-local runs, which are kept
442 // even when empty for a performance reason, so that they won't
443 // cause the following large object allocation to fail due to
444 // potential fragmentation. Note they are normally revoked at each
445 // GC (but no GC here.)
446 space->RevokeAllThreadLocalBuffers();
447
Ian Rogers3bb17a62012-01-27 23:56:44 -0800448 // All memory was released, try a large allocation to check freed memory is being coalesced
Mathieu Chartier4e305412014-02-19 10:54:44 -0800449 SirtRef<mirror::Object> large_object(self, nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800450 size_t three_quarters_space = (growth_limit / 2) + (growth_limit / 4);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700451 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800452 if (round <= 1) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800453 large_object.reset(space->Alloc(self, three_quarters_space, &bytes_allocated));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800454 } else {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800455 large_object.reset(space->AllocWithGrowth(self, three_quarters_space, &bytes_allocated));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800456 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800457 EXPECT_TRUE(large_object.get() != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700458 InstallClass(large_object, three_quarters_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800459
460 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700461 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800462 EXPECT_LE(footprint, growth_limit);
463 EXPECT_GE(space->Size(), footprint);
464 EXPECT_LE(space->Size(), growth_limit);
465
466 // Clean up
Mathieu Chartier4e305412014-02-19 10:54:44 -0800467 space->Free(self, large_object.reset(nullptr));
468
Ian Rogers3bb17a62012-01-27 23:56:44 -0800469 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700470 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800471 EXPECT_LE(footprint, growth_limit);
472 EXPECT_GE(space->Size(), footprint);
473 EXPECT_LE(space->Size(), growth_limit);
474}
475
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800476void SpaceTest::SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size, CreateSpaceFn create_space) {
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800477 if (object_size < SizeOfZeroLengthByteArray()) {
478 // Too small for the object layout/model.
479 return;
480 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800481 size_t initial_size = 4 * MB;
482 size_t growth_limit = 8 * MB;
483 size_t capacity = 16 * MB;
Mathieu Chartier4e305412014-02-19 10:54:44 -0800484 MallocSpace* space(create_space("test", initial_size, growth_limit, capacity, nullptr));
485 ASSERT_TRUE(space != nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800486
487 // Basic sanity
488 EXPECT_EQ(space->Capacity(), growth_limit);
489 EXPECT_EQ(space->NonGrowthLimitCapacity(), capacity);
490
491 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700492 AddSpace(space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800493
494 // In this round we don't allocate with growth and therefore can't grow past the initial size.
495 // This effectively makes the growth_limit the initial_size, so assert this.
496 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 1, initial_size);
497 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 2, growth_limit);
498 // Remove growth limit
499 space->ClearGrowthLimit();
500 EXPECT_EQ(space->Capacity(), capacity);
501 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 3, capacity);
502}
503
Andreas Gampea7433512014-02-21 13:19:23 -0800504#define TEST_SizeFootPrintGrowthLimitAndTrim(name, spaceName, spaceFn, size) \
505 TEST_F(spaceName##Test, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name) { \
506 SizeFootPrintGrowthLimitAndTrimDriver(size, spaceFn); \
Ian Rogers3bb17a62012-01-27 23:56:44 -0800507 } \
Andreas Gampea7433512014-02-21 13:19:23 -0800508 TEST_F(spaceName##Test, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name) { \
509 SizeFootPrintGrowthLimitAndTrimDriver(-size, spaceFn); \
Ian Rogers3bb17a62012-01-27 23:56:44 -0800510 }
511
Andreas Gampea7433512014-02-21 13:19:23 -0800512#define TEST_SPACE_CREATE_FN(spaceName, spaceFn) \
513 class spaceName##Test : public SpaceTest { \
514 }; \
515 \
516 TEST_F(spaceName##Test, Init) { \
517 InitTestBody(spaceFn); \
518 } \
519 TEST_F(spaceName##Test, ZygoteSpace) { \
520 ZygoteSpaceTestBody(spaceFn); \
521 } \
522 TEST_F(spaceName##Test, AllocAndFree) { \
523 AllocAndFreeTestBody(spaceFn); \
524 } \
525 TEST_F(spaceName##Test, AllocAndFreeList) { \
526 AllocAndFreeListTestBody(spaceFn); \
527 } \
528 TEST_F(spaceName##Test, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_12B) { \
529 SizeFootPrintGrowthLimitAndTrimDriver(12, spaceFn); \
530 } \
531 TEST_SizeFootPrintGrowthLimitAndTrim(16B, spaceName, spaceFn, 16) \
532 TEST_SizeFootPrintGrowthLimitAndTrim(24B, spaceName, spaceFn, 24) \
533 TEST_SizeFootPrintGrowthLimitAndTrim(32B, spaceName, spaceFn, 32) \
534 TEST_SizeFootPrintGrowthLimitAndTrim(64B, spaceName, spaceFn, 64) \
535 TEST_SizeFootPrintGrowthLimitAndTrim(128B, spaceName, spaceFn, 128) \
536 TEST_SizeFootPrintGrowthLimitAndTrim(1KB, spaceName, spaceFn, 1 * KB) \
537 TEST_SizeFootPrintGrowthLimitAndTrim(4KB, spaceName, spaceFn, 4 * KB) \
538 TEST_SizeFootPrintGrowthLimitAndTrim(1MB, spaceName, spaceFn, 1 * MB) \
539 TEST_SizeFootPrintGrowthLimitAndTrim(4MB, spaceName, spaceFn, 4 * MB) \
540 TEST_SizeFootPrintGrowthLimitAndTrim(8MB, spaceName, spaceFn, 8 * MB)
Ian Rogers3bb17a62012-01-27 23:56:44 -0800541
Ian Rogers1d54e732013-05-02 21:10:01 -0700542} // namespace space
543} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700544} // namespace art
Andreas Gampea7433512014-02-21 13:19:23 -0800545
546#endif // ART_RUNTIME_GC_SPACE_SPACE_TEST_H_