blob: d01bf2c33bbd380a6294530a5a72e67bce50483f [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());
Mathieu Chartier4e305412014-02-19 10:54:44 -080046 SirtRef<mirror::ClassLoader> null_loader(Thread::Current(), nullptr);
47 mirror::Class* byte_array_class = Runtime::Current()->GetClassLinker()->FindClass("[B",
48 null_loader);
49 EXPECT_TRUE(byte_array_class != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070050 o->SetClass(byte_array_class);
Mathieu Chartier4e305412014-02-19 10:54:44 -080051 mirror::Array* arr = o->AsArray<kVerifyNone>();
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080052 size_t header_size = SizeOfZeroLengthByteArray();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070053 int32_t length = size - header_size;
54 arr->SetLength(length);
Mathieu Chartier4e305412014-02-19 10:54:44 -080055 EXPECT_EQ(arr->SizeOf<kVerifyNone>(), size);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070056 }
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080057
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080058 static size_t SizeOfZeroLengthByteArray() {
59 return mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimByte)).Uint32Value();
60 }
61
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080062 typedef MallocSpace* (*CreateSpaceFn)(const std::string& name, size_t initial_size, size_t growth_limit,
63 size_t capacity, byte* requested_begin);
64 void InitTestBody(CreateSpaceFn create_space);
65 void ZygoteSpaceTestBody(CreateSpaceFn create_space);
66 void AllocAndFreeTestBody(CreateSpaceFn create_space);
67 void AllocAndFreeListTestBody(CreateSpaceFn create_space);
68
69 void SizeFootPrintGrowthLimitAndTrimBody(MallocSpace* space, intptr_t object_size,
70 int round, size_t growth_limit);
71 void SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size, CreateSpaceFn create_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -080072};
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070073
Mathieu Chartiereb5710e2013-07-25 15:19:42 -070074static size_t test_rand(size_t* seed) {
75 *seed = *seed * 1103515245 + 12345;
76 return *seed;
77}
78
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -080079void SpaceTest::InitTestBody(CreateSpaceFn create_space) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070080 {
jeffhaoc1160702011-10-27 15:48:45 -070081 // Init < max == growth
Mathieu Chartier4e305412014-02-19 10:54:44 -080082 UniquePtr<Space> space(create_space("test", 16 * MB, 32 * MB, 32 * MB, nullptr));
83 EXPECT_TRUE(space.get() != nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -070084 }
85 {
jeffhaoc1160702011-10-27 15:48:45 -070086 // Init == max == growth
Mathieu Chartier4e305412014-02-19 10:54:44 -080087 UniquePtr<Space> space(create_space("test", 16 * MB, 16 * MB, 16 * MB, nullptr));
88 EXPECT_TRUE(space.get() != nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -070089 }
90 {
jeffhaoc1160702011-10-27 15:48:45 -070091 // Init > max == growth
Mathieu Chartier4e305412014-02-19 10:54:44 -080092 UniquePtr<Space> space(create_space("test", 32 * MB, 16 * MB, 16 * MB, nullptr));
93 EXPECT_TRUE(space.get() == nullptr);
jeffhaoc1160702011-10-27 15:48:45 -070094 }
95 {
96 // Growth == init < max
Mathieu Chartier4e305412014-02-19 10:54:44 -080097 UniquePtr<Space> space(create_space("test", 16 * MB, 16 * MB, 32 * MB, nullptr));
98 EXPECT_TRUE(space.get() != nullptr);
jeffhaoc1160702011-10-27 15:48:45 -070099 }
100 {
101 // Growth < init < max
Mathieu Chartier4e305412014-02-19 10:54:44 -0800102 UniquePtr<Space> space(create_space("test", 16 * MB, 8 * MB, 32 * MB, nullptr));
103 EXPECT_TRUE(space.get() == nullptr);
jeffhaoc1160702011-10-27 15:48:45 -0700104 }
105 {
106 // Init < growth < max
Mathieu Chartier4e305412014-02-19 10:54:44 -0800107 UniquePtr<Space> space(create_space("test", 8 * MB, 16 * MB, 32 * MB, nullptr));
108 EXPECT_TRUE(space.get() != nullptr);
jeffhaoc1160702011-10-27 15:48:45 -0700109 }
110 {
111 // Init < max < growth
Mathieu Chartier4e305412014-02-19 10:54:44 -0800112 UniquePtr<Space> space(create_space("test", 8 * MB, 32 * MB, 16 * MB, nullptr));
113 EXPECT_TRUE(space.get() == nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700114 }
115}
116
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700117// TODO: This test is not very good, we should improve it.
118// The test should do more allocations before the creation of the ZygoteSpace, and then do
119// allocations after the ZygoteSpace is created. The test should also do some GCs to ensure that
120// the GC works with the ZygoteSpace.
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800121void SpaceTest::ZygoteSpaceTestBody(CreateSpaceFn create_space) {
122 size_t dummy = 0;
Mathieu Chartier4e305412014-02-19 10:54:44 -0800123 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, nullptr));
124 ASSERT_TRUE(space != nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700125
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800126 // Make space findable to the heap, will also delete space when runtime is cleaned up
127 AddSpace(space);
128 Thread* self = Thread::Current();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800129 ScopedObjectAccess soa(self);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700130
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800131 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800132 SirtRef<mirror::Object> ptr1(self, space->Alloc(self, 1 * MB, &dummy));
133 EXPECT_TRUE(ptr1.get() != nullptr);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800134 InstallClass(ptr1, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700135
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800136 // Fails, requires a higher footprint limit.
137 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800138 EXPECT_TRUE(ptr2 == nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700139
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800140 // Succeeds, adjusts the footprint.
141 size_t ptr3_bytes_allocated;
Mathieu Chartier4e305412014-02-19 10:54:44 -0800142 SirtRef<mirror::Object> ptr3(self, space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated));
143 EXPECT_TRUE(ptr3.get() != nullptr);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800144 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
145 InstallClass(ptr3, 8 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700146
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800147 // Fails, requires a higher footprint limit.
148 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800149 EXPECT_TRUE(ptr4 == nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700150
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800151 // Also fails, requires a higher allowed footprint.
152 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800153 EXPECT_TRUE(ptr5 == nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700154
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800155 // Release some memory.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800156 size_t free3 = space->AllocationSize(ptr3.get());
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800157 EXPECT_EQ(free3, ptr3_bytes_allocated);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800158 EXPECT_EQ(free3, space->Free(self, ptr3.reset(nullptr)));
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800159 EXPECT_LE(8U * MB, free3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700160
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800161 // Succeeds, now that memory has been freed.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800162 SirtRef<mirror::Object> ptr6(self, space->AllocWithGrowth(self, 9 * MB, &dummy));
163 EXPECT_TRUE(ptr6.get() != nullptr);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800164 InstallClass(ptr6, 9 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700165
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800166 // Final clean up.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800167 size_t free1 = space->AllocationSize(ptr1.get());
168 space->Free(self, ptr1.reset(nullptr));
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800169 EXPECT_LE(1U * MB, free1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700170
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800171 // Make sure that the zygote space isn't directly at the start of the space.
172 space->Alloc(self, 1U * MB, &dummy);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800173
174 gc::Heap* heap = Runtime::Current()->GetHeap();
175 space::Space* old_space = space;
176 heap->RemoveSpace(old_space);
177 space::ZygoteSpace* zygote_space = space->CreateZygoteSpace("alloc space",
178 heap->IsLowMemoryMode(),
179 &space);
180 delete old_space;
181 // Add the zygote space.
182 AddSpace(zygote_space);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700183
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800184 // Make space findable to the heap, will also delete space when runtime is cleaned up
185 AddSpace(space);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700186
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800187 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800188 ptr1.reset(space->Alloc(self, 1 * MB, &dummy));
189 EXPECT_TRUE(ptr1.get() != nullptr);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800190 InstallClass(ptr1, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700191
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800192 // Fails, requires a higher footprint limit.
193 ptr2 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800194 EXPECT_TRUE(ptr2 == nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700195
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800196 // Succeeds, adjusts the footprint.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800197 ptr3.reset(space->AllocWithGrowth(self, 2 * MB, &dummy));
198 EXPECT_TRUE(ptr3.get() != nullptr);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800199 InstallClass(ptr3, 2 * MB);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800200 space->Free(self, ptr3.reset(nullptr));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700201
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800202 // Final clean up.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800203 free1 = space->AllocationSize(ptr1.get());
204 space->Free(self, ptr1.reset(nullptr));
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800205 EXPECT_LE(1U * MB, free1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700206}
207
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800208void SpaceTest::AllocAndFreeTestBody(CreateSpaceFn create_space) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700209 size_t dummy = 0;
Mathieu Chartier4e305412014-02-19 10:54:44 -0800210 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, nullptr));
211 ASSERT_TRUE(space != nullptr);
Ian Rogers50b35e22012-10-04 10:09:15 -0700212 Thread* self = Thread::Current();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800213 ScopedObjectAccess soa(self);
Ian Rogers30fab402012-01-23 15:43:46 -0800214
Ian Rogers3bb17a62012-01-27 23:56:44 -0800215 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700216 AddSpace(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700217
Ian Rogers3bb17a62012-01-27 23:56:44 -0800218 // Succeeds, fits without adjusting the footprint limit.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800219 SirtRef<mirror::Object> ptr1(self, space->Alloc(self, 1 * MB, &dummy));
220 EXPECT_TRUE(ptr1.get() != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700221 InstallClass(ptr1, 1 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700222
Ian Rogers3bb17a62012-01-27 23:56:44 -0800223 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700224 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800225 EXPECT_TRUE(ptr2 == nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700226
227 // Succeeds, adjusts the footprint.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700228 size_t ptr3_bytes_allocated;
Mathieu Chartier4e305412014-02-19 10:54:44 -0800229 SirtRef<mirror::Object> ptr3(self, space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated));
230 EXPECT_TRUE(ptr3.get() != nullptr);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700231 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700232 InstallClass(ptr3, 8 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700233
Ian Rogers3bb17a62012-01-27 23:56:44 -0800234 // Fails, requires a higher footprint limit.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700235 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800236 EXPECT_TRUE(ptr4 == nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700237
238 // Also fails, requires a higher allowed footprint.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700239 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800240 EXPECT_TRUE(ptr5 == nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700241
242 // Release some memory.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800243 size_t free3 = space->AllocationSize(ptr3.get());
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700244 EXPECT_EQ(free3, ptr3_bytes_allocated);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800245 space->Free(self, ptr3.reset(nullptr));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700246 EXPECT_LE(8U * MB, free3);
247
248 // Succeeds, now that memory has been freed.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800249 SirtRef<mirror::Object> ptr6(self, space->AllocWithGrowth(self, 9 * MB, &dummy));
250 EXPECT_TRUE(ptr6.get() != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700251 InstallClass(ptr6, 9 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700252
253 // Final clean up.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800254 size_t free1 = space->AllocationSize(ptr1.get());
255 space->Free(self, ptr1.reset(nullptr));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700256 EXPECT_LE(1U * MB, free1);
257}
258
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800259void SpaceTest::AllocAndFreeListTestBody(CreateSpaceFn create_space) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800260 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, nullptr));
261 ASSERT_TRUE(space != nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800262
263 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700264 AddSpace(space);
Ian Rogers50b35e22012-10-04 10:09:15 -0700265 Thread* self = Thread::Current();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800266 ScopedObjectAccess soa(self);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800267
268 // Succeeds, fits without adjusting the max allowed footprint.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800269 mirror::Object* lots_of_objects[1024];
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700270 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700271 size_t allocation_size = 0;
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800272 size_t size_of_zero_length_byte_array = SizeOfZeroLengthByteArray();
273 lots_of_objects[i] = space->Alloc(self, size_of_zero_length_byte_array, &allocation_size);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800274 EXPECT_TRUE(lots_of_objects[i] != nullptr);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800275 SirtRef<mirror::Object> obj(self, lots_of_objects[i]);
276 InstallClass(obj, size_of_zero_length_byte_array);
277 lots_of_objects[i] = obj.get();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700278 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i]));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800279 }
280
Mathieu Chartier4e305412014-02-19 10:54:44 -0800281 // Release memory and check pointers are nullptr.
282 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
283 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
284 EXPECT_TRUE(lots_of_objects[i] == nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800285 }
286
287 // Succeeds, fits by adjusting the max allowed footprint.
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700288 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700289 size_t allocation_size = 0;
290 lots_of_objects[i] = space->AllocWithGrowth(self, 1024, &allocation_size);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800291 EXPECT_TRUE(lots_of_objects[i] != nullptr);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800292 SirtRef<mirror::Object> obj(self, lots_of_objects[i]);
293 InstallClass(obj, 1024);
294 lots_of_objects[i] = obj.get();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700295 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i]));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800296 }
297
Mathieu Chartier4e305412014-02-19 10:54:44 -0800298 // Release memory and check pointers are nullptr
299 // TODO: This isn't compaction safe, fix.
300 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
301 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
302 EXPECT_TRUE(lots_of_objects[i] == nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800303 }
304}
305
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800306void SpaceTest::SizeFootPrintGrowthLimitAndTrimBody(MallocSpace* space, intptr_t object_size,
Ian Rogers3bb17a62012-01-27 23:56:44 -0800307 int round, size_t growth_limit) {
308 if (((object_size > 0 && object_size >= static_cast<intptr_t>(growth_limit))) ||
309 ((object_size < 0 && -object_size >= static_cast<intptr_t>(growth_limit)))) {
310 // No allocation can succeed
311 return;
312 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800313
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700314 // The space's footprint equals amount of resources requested from system
315 size_t footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800316
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700317 // The space must at least have its book keeping allocated
Ian Rogers3bb17a62012-01-27 23:56:44 -0800318 EXPECT_GT(footprint, 0u);
319
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700320 // But it shouldn't exceed the initial size
Ian Rogers3bb17a62012-01-27 23:56:44 -0800321 EXPECT_LE(footprint, growth_limit);
322
323 // space's size shouldn't exceed the initial size
324 EXPECT_LE(space->Size(), growth_limit);
325
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700326 // this invariant should always hold or else the space has grown to be larger than what the
Ian Rogers3bb17a62012-01-27 23:56:44 -0800327 // space believes its size is (which will break invariants)
328 EXPECT_GE(space->Size(), footprint);
329
330 // Fill the space with lots of small objects up to the growth limit
331 size_t max_objects = (growth_limit / (object_size > 0 ? object_size : 8)) + 1;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800332 UniquePtr<mirror::Object*[]> lots_of_objects(new mirror::Object*[max_objects]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800333 size_t last_object = 0; // last object for which allocation succeeded
334 size_t amount_allocated = 0; // amount of space allocated
Ian Rogers50b35e22012-10-04 10:09:15 -0700335 Thread* self = Thread::Current();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800336 ScopedObjectAccess soa(self);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700337 size_t rand_seed = 123456789;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700338 for (size_t i = 0; i < max_objects; i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800339 size_t alloc_fails = 0; // number of failed allocations
340 size_t max_fails = 30; // number of times we fail allocation before giving up
341 for (; alloc_fails < max_fails; alloc_fails++) {
342 size_t alloc_size;
343 if (object_size > 0) {
344 alloc_size = object_size;
345 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700346 alloc_size = test_rand(&rand_seed) % static_cast<size_t>(-object_size);
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800347 // Note the minimum size, which is the size of a zero-length byte array.
348 size_t size_of_zero_length_byte_array = SizeOfZeroLengthByteArray();
349 if (alloc_size < size_of_zero_length_byte_array) {
350 alloc_size = size_of_zero_length_byte_array;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800351 }
352 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800353 SirtRef<mirror::Object> object(self, nullptr);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700354 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800355 if (round <= 1) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800356 object.reset(space->Alloc(self, alloc_size, &bytes_allocated));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800357 } else {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800358 object.reset(space->AllocWithGrowth(self, alloc_size, &bytes_allocated));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800359 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700360 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800361 EXPECT_GE(space->Size(), footprint); // invariant
Mathieu Chartier4e305412014-02-19 10:54:44 -0800362 if (object.get() != nullptr) { // allocation succeeded
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700363 InstallClass(object, alloc_size);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800364 lots_of_objects[i] = object.get();
365 size_t allocation_size = space->AllocationSize(object.get());
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700366 EXPECT_EQ(bytes_allocated, allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800367 if (object_size > 0) {
368 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
369 } else {
370 EXPECT_GE(allocation_size, 8u);
371 }
372 amount_allocated += allocation_size;
373 break;
374 }
375 }
376 if (alloc_fails == max_fails) {
377 last_object = i;
378 break;
379 }
380 }
381 CHECK_NE(last_object, 0u); // we should have filled the space
382 EXPECT_GT(amount_allocated, 0u);
383
384 // We shouldn't have gone past the growth_limit
385 EXPECT_LE(amount_allocated, growth_limit);
386 EXPECT_LE(footprint, growth_limit);
387 EXPECT_LE(space->Size(), growth_limit);
388
389 // footprint and size should agree with amount allocated
390 EXPECT_GE(footprint, amount_allocated);
391 EXPECT_GE(space->Size(), amount_allocated);
392
393 // Release storage in a semi-adhoc manner
394 size_t free_increment = 96;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700395 while (true) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800396 {
397 ScopedThreadStateChange tsc(self, kNative);
398 // Give the space a haircut.
399 space->Trim();
400 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800401
402 // Bounds sanity
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700403 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800404 EXPECT_LE(amount_allocated, growth_limit);
405 EXPECT_GE(footprint, amount_allocated);
406 EXPECT_LE(footprint, growth_limit);
407 EXPECT_GE(space->Size(), amount_allocated);
408 EXPECT_LE(space->Size(), growth_limit);
409
410 if (free_increment == 0) {
411 break;
412 }
413
Mathieu Chartier4e305412014-02-19 10:54:44 -0800414 // Free some objects
415 for (size_t i = 0; i < last_object; i += free_increment) {
416 mirror::Object* object = lots_of_objects.get()[i];
417 if (object == nullptr) {
418 continue;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800419 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800420 size_t allocation_size = space->AllocationSize(object);
421 if (object_size > 0) {
422 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
423 } else {
424 EXPECT_GE(allocation_size, 8u);
425 }
426 space->Free(self, object);
427 lots_of_objects.get()[i] = nullptr;
428 amount_allocated -= allocation_size;
429 footprint = space->GetFootprint();
430 EXPECT_GE(space->Size(), footprint); // invariant
Ian Rogers3bb17a62012-01-27 23:56:44 -0800431 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800432
433 free_increment >>= 1;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800434 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800435
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700436 // The space has become empty here before allocating a large object
437 // below. For RosAlloc, revoke thread-local runs, which are kept
438 // even when empty for a performance reason, so that they won't
439 // cause the following large object allocation to fail due to
440 // potential fragmentation. Note they are normally revoked at each
441 // GC (but no GC here.)
442 space->RevokeAllThreadLocalBuffers();
443
Ian Rogers3bb17a62012-01-27 23:56:44 -0800444 // All memory was released, try a large allocation to check freed memory is being coalesced
Mathieu Chartier4e305412014-02-19 10:54:44 -0800445 SirtRef<mirror::Object> large_object(self, nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800446 size_t three_quarters_space = (growth_limit / 2) + (growth_limit / 4);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700447 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800448 if (round <= 1) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800449 large_object.reset(space->Alloc(self, three_quarters_space, &bytes_allocated));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800450 } else {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800451 large_object.reset(space->AllocWithGrowth(self, three_quarters_space, &bytes_allocated));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800452 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800453 EXPECT_TRUE(large_object.get() != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700454 InstallClass(large_object, three_quarters_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800455
456 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700457 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800458 EXPECT_LE(footprint, growth_limit);
459 EXPECT_GE(space->Size(), footprint);
460 EXPECT_LE(space->Size(), growth_limit);
461
462 // Clean up
Mathieu Chartier4e305412014-02-19 10:54:44 -0800463 space->Free(self, large_object.reset(nullptr));
464
Ian Rogers3bb17a62012-01-27 23:56:44 -0800465 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700466 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800467 EXPECT_LE(footprint, growth_limit);
468 EXPECT_GE(space->Size(), footprint);
469 EXPECT_LE(space->Size(), growth_limit);
470}
471
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800472void SpaceTest::SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size, CreateSpaceFn create_space) {
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800473 if (object_size < SizeOfZeroLengthByteArray()) {
474 // Too small for the object layout/model.
475 return;
476 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800477 size_t initial_size = 4 * MB;
478 size_t growth_limit = 8 * MB;
479 size_t capacity = 16 * MB;
Mathieu Chartier4e305412014-02-19 10:54:44 -0800480 MallocSpace* space(create_space("test", initial_size, growth_limit, capacity, nullptr));
481 ASSERT_TRUE(space != nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800482
483 // Basic sanity
484 EXPECT_EQ(space->Capacity(), growth_limit);
485 EXPECT_EQ(space->NonGrowthLimitCapacity(), capacity);
486
487 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700488 AddSpace(space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800489
490 // In this round we don't allocate with growth and therefore can't grow past the initial size.
491 // This effectively makes the growth_limit the initial_size, so assert this.
492 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 1, initial_size);
493 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 2, growth_limit);
494 // Remove growth limit
495 space->ClearGrowthLimit();
496 EXPECT_EQ(space->Capacity(), capacity);
497 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 3, capacity);
498}
499
Andreas Gampea7433512014-02-21 13:19:23 -0800500#define TEST_SizeFootPrintGrowthLimitAndTrim(name, spaceName, spaceFn, size) \
501 TEST_F(spaceName##Test, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name) { \
502 SizeFootPrintGrowthLimitAndTrimDriver(size, spaceFn); \
Ian Rogers3bb17a62012-01-27 23:56:44 -0800503 } \
Andreas Gampea7433512014-02-21 13:19:23 -0800504 TEST_F(spaceName##Test, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name) { \
505 SizeFootPrintGrowthLimitAndTrimDriver(-size, spaceFn); \
Ian Rogers3bb17a62012-01-27 23:56:44 -0800506 }
507
Andreas Gampea7433512014-02-21 13:19:23 -0800508#define TEST_SPACE_CREATE_FN(spaceName, spaceFn) \
509 class spaceName##Test : public SpaceTest { \
510 }; \
511 \
512 TEST_F(spaceName##Test, Init) { \
513 InitTestBody(spaceFn); \
514 } \
515 TEST_F(spaceName##Test, ZygoteSpace) { \
516 ZygoteSpaceTestBody(spaceFn); \
517 } \
518 TEST_F(spaceName##Test, AllocAndFree) { \
519 AllocAndFreeTestBody(spaceFn); \
520 } \
521 TEST_F(spaceName##Test, AllocAndFreeList) { \
522 AllocAndFreeListTestBody(spaceFn); \
523 } \
524 TEST_F(spaceName##Test, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_12B) { \
525 SizeFootPrintGrowthLimitAndTrimDriver(12, spaceFn); \
526 } \
527 TEST_SizeFootPrintGrowthLimitAndTrim(16B, spaceName, spaceFn, 16) \
528 TEST_SizeFootPrintGrowthLimitAndTrim(24B, spaceName, spaceFn, 24) \
529 TEST_SizeFootPrintGrowthLimitAndTrim(32B, spaceName, spaceFn, 32) \
530 TEST_SizeFootPrintGrowthLimitAndTrim(64B, spaceName, spaceFn, 64) \
531 TEST_SizeFootPrintGrowthLimitAndTrim(128B, spaceName, spaceFn, 128) \
532 TEST_SizeFootPrintGrowthLimitAndTrim(1KB, spaceName, spaceFn, 1 * KB) \
533 TEST_SizeFootPrintGrowthLimitAndTrim(4KB, spaceName, spaceFn, 4 * KB) \
534 TEST_SizeFootPrintGrowthLimitAndTrim(1MB, spaceName, spaceFn, 1 * MB) \
535 TEST_SizeFootPrintGrowthLimitAndTrim(4MB, spaceName, spaceFn, 4 * MB) \
536 TEST_SizeFootPrintGrowthLimitAndTrim(8MB, spaceName, spaceFn, 8 * MB)
Ian Rogers3bb17a62012-01-27 23:56:44 -0800537
Ian Rogers1d54e732013-05-02 21:10:01 -0700538} // namespace space
539} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700540} // namespace art
Andreas Gampea7433512014-02-21 13:19:23 -0800541
542#endif // ART_RUNTIME_GC_SPACE_SPACE_TEST_H_