blob: cb036f877228a2dc412d23b150ffddedfbce8bb1 [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 Carlstroma1ce1fe2014-02-24 23:23:58 -080022#include <stdint.h>
23
24#include "common_runtime_test.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "globals.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070026#include "UniquePtr.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070027#include "mirror/array-inl.h"
28#include "mirror/object-inl.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070029
30namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070031namespace gc {
32namespace space {
Carl Shapiro69759ea2011-07-21 18:13:35 -070033
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080034class SpaceTest : public CommonRuntimeTest {
Ian Rogers3bb17a62012-01-27 23:56:44 -080035 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) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800126 size_t dummy;
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.
Ian Rogers6fac4472014-02-25 17:01:10 -0800136 size_t ptr1_bytes_allocated, ptr1_usable_size;
137 SirtRef<mirror::Object> ptr1(self, space->Alloc(self, 1 * MB, &ptr1_bytes_allocated,
138 &ptr1_usable_size));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800139 EXPECT_TRUE(ptr1.get() != nullptr);
Ian Rogers6fac4472014-02-25 17:01:10 -0800140 EXPECT_LE(1U * MB, ptr1_bytes_allocated);
141 EXPECT_LE(1U * MB, ptr1_usable_size);
142 EXPECT_LE(ptr1_usable_size, ptr1_bytes_allocated);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800143 InstallClass(ptr1, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700144
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800145 // Fails, requires a higher footprint limit.
Ian Rogers6fac4472014-02-25 17:01:10 -0800146 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy, nullptr);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800147 EXPECT_TRUE(ptr2 == nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700148
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800149 // Succeeds, adjusts the footprint.
Ian Rogers6fac4472014-02-25 17:01:10 -0800150 size_t ptr3_bytes_allocated, ptr3_usable_size;
151 SirtRef<mirror::Object> ptr3(self, space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated,
152 &ptr3_usable_size));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800153 EXPECT_TRUE(ptr3.get() != nullptr);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800154 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
Ian Rogers6fac4472014-02-25 17:01:10 -0800155 EXPECT_LE(8U * MB, ptr3_usable_size);
156 EXPECT_LE(ptr3_usable_size, ptr3_bytes_allocated);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800157 InstallClass(ptr3, 8 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700158
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800159 // Fails, requires a higher footprint limit.
Ian Rogers6fac4472014-02-25 17:01:10 -0800160 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy, nullptr);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800161 EXPECT_TRUE(ptr4 == nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700162
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800163 // Also fails, requires a higher allowed footprint.
Ian Rogers6fac4472014-02-25 17:01:10 -0800164 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy, nullptr);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800165 EXPECT_TRUE(ptr5 == nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700166
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800167 // Release some memory.
Ian Rogers6fac4472014-02-25 17:01:10 -0800168 size_t free3 = space->AllocationSize(ptr3.get(), nullptr);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800169 EXPECT_EQ(free3, ptr3_bytes_allocated);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800170 EXPECT_EQ(free3, space->Free(self, ptr3.reset(nullptr)));
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800171 EXPECT_LE(8U * MB, free3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700172
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800173 // Succeeds, now that memory has been freed.
Ian Rogers6fac4472014-02-25 17:01:10 -0800174 size_t ptr6_bytes_allocated, ptr6_usable_size;
175 SirtRef<mirror::Object> ptr6(self, space->AllocWithGrowth(self, 9 * MB, &ptr6_bytes_allocated,
176 &ptr6_usable_size));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800177 EXPECT_TRUE(ptr6.get() != nullptr);
Ian Rogers6fac4472014-02-25 17:01:10 -0800178 EXPECT_LE(9U * MB, ptr6_bytes_allocated);
179 EXPECT_LE(9U * MB, ptr6_usable_size);
180 EXPECT_LE(ptr6_usable_size, ptr6_bytes_allocated);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800181 InstallClass(ptr6, 9 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700182
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800183 // Final clean up.
Ian Rogers6fac4472014-02-25 17:01:10 -0800184 size_t free1 = space->AllocationSize(ptr1.get(), nullptr);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800185 space->Free(self, ptr1.reset(nullptr));
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800186 EXPECT_LE(1U * MB, free1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700187
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800188 // Make sure that the zygote space isn't directly at the start of the space.
Ian Rogers6fac4472014-02-25 17:01:10 -0800189 EXPECT_TRUE(space->Alloc(self, 1U * MB, &dummy, nullptr) != nullptr);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800190
191 gc::Heap* heap = Runtime::Current()->GetHeap();
192 space::Space* old_space = space;
193 heap->RemoveSpace(old_space);
194 space::ZygoteSpace* zygote_space = space->CreateZygoteSpace("alloc space",
195 heap->IsLowMemoryMode(),
196 &space);
197 delete old_space;
198 // Add the zygote space.
199 AddSpace(zygote_space);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700200
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800201 // Make space findable to the heap, will also delete space when runtime is cleaned up
202 AddSpace(space);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700203
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800204 // Succeeds, fits without adjusting the footprint limit.
Ian Rogers6fac4472014-02-25 17:01:10 -0800205 ptr1.reset(space->Alloc(self, 1 * MB, &ptr1_bytes_allocated, &ptr1_usable_size));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800206 EXPECT_TRUE(ptr1.get() != nullptr);
Ian Rogers6fac4472014-02-25 17:01:10 -0800207 EXPECT_LE(1U * MB, ptr1_bytes_allocated);
208 EXPECT_LE(1U * MB, ptr1_usable_size);
209 EXPECT_LE(ptr1_usable_size, ptr1_bytes_allocated);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800210 InstallClass(ptr1, 1 * MB);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700211
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800212 // Fails, requires a higher footprint limit.
Ian Rogers6fac4472014-02-25 17:01:10 -0800213 ptr2 = space->Alloc(self, 8 * MB, &dummy, nullptr);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800214 EXPECT_TRUE(ptr2 == nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700215
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800216 // Succeeds, adjusts the footprint.
Ian Rogers6fac4472014-02-25 17:01:10 -0800217 ptr3.reset(space->AllocWithGrowth(self, 2 * MB, &ptr3_bytes_allocated, &ptr3_usable_size));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800218 EXPECT_TRUE(ptr3.get() != nullptr);
Ian Rogers6fac4472014-02-25 17:01:10 -0800219 EXPECT_LE(2U * MB, ptr3_bytes_allocated);
220 EXPECT_LE(2U * MB, ptr3_usable_size);
221 EXPECT_LE(ptr3_usable_size, ptr3_bytes_allocated);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800222 InstallClass(ptr3, 2 * MB);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800223 space->Free(self, ptr3.reset(nullptr));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700224
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800225 // Final clean up.
Ian Rogers6fac4472014-02-25 17:01:10 -0800226 free1 = space->AllocationSize(ptr1.get(), nullptr);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800227 space->Free(self, ptr1.reset(nullptr));
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800228 EXPECT_LE(1U * MB, free1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700229}
230
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800231void SpaceTest::AllocAndFreeTestBody(CreateSpaceFn create_space) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700232 size_t dummy = 0;
Mathieu Chartier4e305412014-02-19 10:54:44 -0800233 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, nullptr));
234 ASSERT_TRUE(space != nullptr);
Ian Rogers50b35e22012-10-04 10:09:15 -0700235 Thread* self = Thread::Current();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800236 ScopedObjectAccess soa(self);
Ian Rogers30fab402012-01-23 15:43:46 -0800237
Ian Rogers3bb17a62012-01-27 23:56:44 -0800238 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700239 AddSpace(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700240
Ian Rogers3bb17a62012-01-27 23:56:44 -0800241 // Succeeds, fits without adjusting the footprint limit.
Ian Rogers6fac4472014-02-25 17:01:10 -0800242 size_t ptr1_bytes_allocated, ptr1_usable_size;
243 SirtRef<mirror::Object> ptr1(self, space->Alloc(self, 1 * MB, &ptr1_bytes_allocated,
244 &ptr1_usable_size));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800245 EXPECT_TRUE(ptr1.get() != nullptr);
Ian Rogers6fac4472014-02-25 17:01:10 -0800246 EXPECT_LE(1U * MB, ptr1_bytes_allocated);
247 EXPECT_LE(1U * MB, ptr1_usable_size);
248 EXPECT_LE(ptr1_usable_size, ptr1_bytes_allocated);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700249 InstallClass(ptr1, 1 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700250
Ian Rogers3bb17a62012-01-27 23:56:44 -0800251 // Fails, requires a higher footprint limit.
Ian Rogers6fac4472014-02-25 17:01:10 -0800252 mirror::Object* ptr2 = space->Alloc(self, 8 * MB, &dummy, nullptr);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800253 EXPECT_TRUE(ptr2 == nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700254
255 // Succeeds, adjusts the footprint.
Ian Rogers6fac4472014-02-25 17:01:10 -0800256 size_t ptr3_bytes_allocated, ptr3_usable_size;
257 SirtRef<mirror::Object> ptr3(self, space->AllocWithGrowth(self, 8 * MB, &ptr3_bytes_allocated,
258 &ptr3_usable_size));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800259 EXPECT_TRUE(ptr3.get() != nullptr);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700260 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
Ian Rogers6fac4472014-02-25 17:01:10 -0800261 EXPECT_LE(8U * MB, ptr3_usable_size);
262 EXPECT_LE(ptr3_usable_size, ptr3_bytes_allocated);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700263 InstallClass(ptr3, 8 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700264
Ian Rogers3bb17a62012-01-27 23:56:44 -0800265 // Fails, requires a higher footprint limit.
Ian Rogers6fac4472014-02-25 17:01:10 -0800266 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy, nullptr);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800267 EXPECT_TRUE(ptr4 == nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700268
269 // Also fails, requires a higher allowed footprint.
Ian Rogers6fac4472014-02-25 17:01:10 -0800270 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy, nullptr);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800271 EXPECT_TRUE(ptr5 == nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700272
273 // Release some memory.
Ian Rogers6fac4472014-02-25 17:01:10 -0800274 size_t free3 = space->AllocationSize(ptr3.get(), nullptr);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700275 EXPECT_EQ(free3, ptr3_bytes_allocated);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800276 space->Free(self, ptr3.reset(nullptr));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700277 EXPECT_LE(8U * MB, free3);
278
279 // Succeeds, now that memory has been freed.
Ian Rogers6fac4472014-02-25 17:01:10 -0800280 size_t ptr6_bytes_allocated, ptr6_usable_size;
281 SirtRef<mirror::Object> ptr6(self, space->AllocWithGrowth(self, 9 * MB, &ptr6_bytes_allocated,
282 &ptr6_usable_size));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800283 EXPECT_TRUE(ptr6.get() != nullptr);
Ian Rogers6fac4472014-02-25 17:01:10 -0800284 EXPECT_LE(9U * MB, ptr6_bytes_allocated);
285 EXPECT_LE(9U * MB, ptr6_usable_size);
286 EXPECT_LE(ptr6_usable_size, ptr6_bytes_allocated);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700287 InstallClass(ptr6, 9 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700288
289 // Final clean up.
Ian Rogers6fac4472014-02-25 17:01:10 -0800290 size_t free1 = space->AllocationSize(ptr1.get(), nullptr);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800291 space->Free(self, ptr1.reset(nullptr));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700292 EXPECT_LE(1U * MB, free1);
293}
294
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800295void SpaceTest::AllocAndFreeListTestBody(CreateSpaceFn create_space) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800296 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, nullptr));
297 ASSERT_TRUE(space != nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800298
299 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700300 AddSpace(space);
Ian Rogers50b35e22012-10-04 10:09:15 -0700301 Thread* self = Thread::Current();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800302 ScopedObjectAccess soa(self);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800303
304 // Succeeds, fits without adjusting the max allowed footprint.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800305 mirror::Object* lots_of_objects[1024];
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700306 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800307 size_t allocation_size, usable_size;
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800308 size_t size_of_zero_length_byte_array = SizeOfZeroLengthByteArray();
Ian Rogers6fac4472014-02-25 17:01:10 -0800309 lots_of_objects[i] = space->Alloc(self, size_of_zero_length_byte_array, &allocation_size,
310 &usable_size);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800311 EXPECT_TRUE(lots_of_objects[i] != nullptr);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800312 SirtRef<mirror::Object> obj(self, lots_of_objects[i]);
313 InstallClass(obj, size_of_zero_length_byte_array);
314 lots_of_objects[i] = obj.get();
Ian Rogers6fac4472014-02-25 17:01:10 -0800315 size_t computed_usable_size;
316 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i], &computed_usable_size));
317 EXPECT_EQ(usable_size, computed_usable_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800318 }
319
Mathieu Chartier4e305412014-02-19 10:54:44 -0800320 // Release memory and check pointers are nullptr.
321 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
322 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
323 EXPECT_TRUE(lots_of_objects[i] == nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800324 }
325
326 // Succeeds, fits by adjusting the max allowed footprint.
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700327 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800328 size_t allocation_size, usable_size;
329 lots_of_objects[i] = space->AllocWithGrowth(self, 1024, &allocation_size, &usable_size);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800330 EXPECT_TRUE(lots_of_objects[i] != nullptr);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800331 SirtRef<mirror::Object> obj(self, lots_of_objects[i]);
332 InstallClass(obj, 1024);
333 lots_of_objects[i] = obj.get();
Ian Rogers6fac4472014-02-25 17:01:10 -0800334 size_t computed_usable_size;
335 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i], &computed_usable_size));
336 EXPECT_EQ(usable_size, computed_usable_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800337 }
338
Mathieu Chartier4e305412014-02-19 10:54:44 -0800339 // Release memory and check pointers are nullptr
340 // TODO: This isn't compaction safe, fix.
341 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
342 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
343 EXPECT_TRUE(lots_of_objects[i] == nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800344 }
345}
346
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800347void SpaceTest::SizeFootPrintGrowthLimitAndTrimBody(MallocSpace* space, intptr_t object_size,
Ian Rogers3bb17a62012-01-27 23:56:44 -0800348 int round, size_t growth_limit) {
349 if (((object_size > 0 && object_size >= static_cast<intptr_t>(growth_limit))) ||
350 ((object_size < 0 && -object_size >= static_cast<intptr_t>(growth_limit)))) {
351 // No allocation can succeed
352 return;
353 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800354
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700355 // The space's footprint equals amount of resources requested from system
356 size_t footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800357
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700358 // The space must at least have its book keeping allocated
Ian Rogers3bb17a62012-01-27 23:56:44 -0800359 EXPECT_GT(footprint, 0u);
360
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700361 // But it shouldn't exceed the initial size
Ian Rogers3bb17a62012-01-27 23:56:44 -0800362 EXPECT_LE(footprint, growth_limit);
363
364 // space's size shouldn't exceed the initial size
365 EXPECT_LE(space->Size(), growth_limit);
366
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700367 // this invariant should always hold or else the space has grown to be larger than what the
Ian Rogers3bb17a62012-01-27 23:56:44 -0800368 // space believes its size is (which will break invariants)
369 EXPECT_GE(space->Size(), footprint);
370
371 // Fill the space with lots of small objects up to the growth limit
372 size_t max_objects = (growth_limit / (object_size > 0 ? object_size : 8)) + 1;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800373 UniquePtr<mirror::Object*[]> lots_of_objects(new mirror::Object*[max_objects]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800374 size_t last_object = 0; // last object for which allocation succeeded
375 size_t amount_allocated = 0; // amount of space allocated
Ian Rogers50b35e22012-10-04 10:09:15 -0700376 Thread* self = Thread::Current();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800377 ScopedObjectAccess soa(self);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700378 size_t rand_seed = 123456789;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700379 for (size_t i = 0; i < max_objects; i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800380 size_t alloc_fails = 0; // number of failed allocations
381 size_t max_fails = 30; // number of times we fail allocation before giving up
382 for (; alloc_fails < max_fails; alloc_fails++) {
383 size_t alloc_size;
384 if (object_size > 0) {
385 alloc_size = object_size;
386 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700387 alloc_size = test_rand(&rand_seed) % static_cast<size_t>(-object_size);
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800388 // Note the minimum size, which is the size of a zero-length byte array.
389 size_t size_of_zero_length_byte_array = SizeOfZeroLengthByteArray();
390 if (alloc_size < size_of_zero_length_byte_array) {
391 alloc_size = size_of_zero_length_byte_array;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800392 }
393 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800394 SirtRef<mirror::Object> object(self, nullptr);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700395 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800396 if (round <= 1) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800397 object.reset(space->Alloc(self, alloc_size, &bytes_allocated, nullptr));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800398 } else {
Ian Rogers6fac4472014-02-25 17:01:10 -0800399 object.reset(space->AllocWithGrowth(self, alloc_size, &bytes_allocated, nullptr));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800400 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700401 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800402 EXPECT_GE(space->Size(), footprint); // invariant
Mathieu Chartier4e305412014-02-19 10:54:44 -0800403 if (object.get() != nullptr) { // allocation succeeded
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700404 InstallClass(object, alloc_size);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800405 lots_of_objects[i] = object.get();
Ian Rogers6fac4472014-02-25 17:01:10 -0800406 size_t allocation_size = space->AllocationSize(object.get(), nullptr);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700407 EXPECT_EQ(bytes_allocated, allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800408 if (object_size > 0) {
409 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
410 } else {
411 EXPECT_GE(allocation_size, 8u);
412 }
413 amount_allocated += allocation_size;
414 break;
415 }
416 }
417 if (alloc_fails == max_fails) {
418 last_object = i;
419 break;
420 }
421 }
422 CHECK_NE(last_object, 0u); // we should have filled the space
423 EXPECT_GT(amount_allocated, 0u);
424
425 // We shouldn't have gone past the growth_limit
426 EXPECT_LE(amount_allocated, growth_limit);
427 EXPECT_LE(footprint, growth_limit);
428 EXPECT_LE(space->Size(), growth_limit);
429
430 // footprint and size should agree with amount allocated
431 EXPECT_GE(footprint, amount_allocated);
432 EXPECT_GE(space->Size(), amount_allocated);
433
434 // Release storage in a semi-adhoc manner
435 size_t free_increment = 96;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700436 while (true) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800437 {
438 ScopedThreadStateChange tsc(self, kNative);
439 // Give the space a haircut.
440 space->Trim();
441 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800442
443 // Bounds sanity
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700444 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800445 EXPECT_LE(amount_allocated, growth_limit);
446 EXPECT_GE(footprint, amount_allocated);
447 EXPECT_LE(footprint, growth_limit);
448 EXPECT_GE(space->Size(), amount_allocated);
449 EXPECT_LE(space->Size(), growth_limit);
450
451 if (free_increment == 0) {
452 break;
453 }
454
Mathieu Chartier4e305412014-02-19 10:54:44 -0800455 // Free some objects
456 for (size_t i = 0; i < last_object; i += free_increment) {
457 mirror::Object* object = lots_of_objects.get()[i];
458 if (object == nullptr) {
459 continue;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800460 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800461 size_t allocation_size = space->AllocationSize(object, nullptr);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800462 if (object_size > 0) {
463 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
464 } else {
465 EXPECT_GE(allocation_size, 8u);
466 }
467 space->Free(self, object);
468 lots_of_objects.get()[i] = nullptr;
469 amount_allocated -= allocation_size;
470 footprint = space->GetFootprint();
471 EXPECT_GE(space->Size(), footprint); // invariant
Ian Rogers3bb17a62012-01-27 23:56:44 -0800472 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800473
474 free_increment >>= 1;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800475 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800476
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700477 // The space has become empty here before allocating a large object
478 // below. For RosAlloc, revoke thread-local runs, which are kept
479 // even when empty for a performance reason, so that they won't
480 // cause the following large object allocation to fail due to
481 // potential fragmentation. Note they are normally revoked at each
482 // GC (but no GC here.)
483 space->RevokeAllThreadLocalBuffers();
484
Ian Rogers3bb17a62012-01-27 23:56:44 -0800485 // All memory was released, try a large allocation to check freed memory is being coalesced
Mathieu Chartier4e305412014-02-19 10:54:44 -0800486 SirtRef<mirror::Object> large_object(self, nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800487 size_t three_quarters_space = (growth_limit / 2) + (growth_limit / 4);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700488 size_t bytes_allocated = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800489 if (round <= 1) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800490 large_object.reset(space->Alloc(self, three_quarters_space, &bytes_allocated, nullptr));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800491 } else {
Ian Rogers6fac4472014-02-25 17:01:10 -0800492 large_object.reset(space->AllocWithGrowth(self, three_quarters_space, &bytes_allocated,
493 nullptr));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800494 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800495 EXPECT_TRUE(large_object.get() != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700496 InstallClass(large_object, three_quarters_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800497
498 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700499 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800500 EXPECT_LE(footprint, growth_limit);
501 EXPECT_GE(space->Size(), footprint);
502 EXPECT_LE(space->Size(), growth_limit);
503
504 // Clean up
Mathieu Chartier4e305412014-02-19 10:54:44 -0800505 space->Free(self, large_object.reset(nullptr));
506
Ian Rogers3bb17a62012-01-27 23:56:44 -0800507 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700508 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800509 EXPECT_LE(footprint, growth_limit);
510 EXPECT_GE(space->Size(), footprint);
511 EXPECT_LE(space->Size(), growth_limit);
512}
513
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800514void SpaceTest::SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size, CreateSpaceFn create_space) {
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800515 if (object_size < SizeOfZeroLengthByteArray()) {
516 // Too small for the object layout/model.
517 return;
518 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800519 size_t initial_size = 4 * MB;
520 size_t growth_limit = 8 * MB;
521 size_t capacity = 16 * MB;
Mathieu Chartier4e305412014-02-19 10:54:44 -0800522 MallocSpace* space(create_space("test", initial_size, growth_limit, capacity, nullptr));
523 ASSERT_TRUE(space != nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800524
525 // Basic sanity
526 EXPECT_EQ(space->Capacity(), growth_limit);
527 EXPECT_EQ(space->NonGrowthLimitCapacity(), capacity);
528
529 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700530 AddSpace(space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800531
532 // In this round we don't allocate with growth and therefore can't grow past the initial size.
533 // This effectively makes the growth_limit the initial_size, so assert this.
534 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 1, initial_size);
535 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 2, growth_limit);
536 // Remove growth limit
537 space->ClearGrowthLimit();
538 EXPECT_EQ(space->Capacity(), capacity);
539 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 3, capacity);
540}
541
Andreas Gampea7433512014-02-21 13:19:23 -0800542#define TEST_SizeFootPrintGrowthLimitAndTrim(name, spaceName, spaceFn, size) \
543 TEST_F(spaceName##Test, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name) { \
544 SizeFootPrintGrowthLimitAndTrimDriver(size, spaceFn); \
Ian Rogers3bb17a62012-01-27 23:56:44 -0800545 } \
Andreas Gampea7433512014-02-21 13:19:23 -0800546 TEST_F(spaceName##Test, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name) { \
547 SizeFootPrintGrowthLimitAndTrimDriver(-size, spaceFn); \
Ian Rogers3bb17a62012-01-27 23:56:44 -0800548 }
549
Andreas Gampea7433512014-02-21 13:19:23 -0800550#define TEST_SPACE_CREATE_FN(spaceName, spaceFn) \
551 class spaceName##Test : public SpaceTest { \
552 }; \
553 \
554 TEST_F(spaceName##Test, Init) { \
555 InitTestBody(spaceFn); \
556 } \
557 TEST_F(spaceName##Test, ZygoteSpace) { \
558 ZygoteSpaceTestBody(spaceFn); \
559 } \
560 TEST_F(spaceName##Test, AllocAndFree) { \
561 AllocAndFreeTestBody(spaceFn); \
562 } \
563 TEST_F(spaceName##Test, AllocAndFreeList) { \
564 AllocAndFreeListTestBody(spaceFn); \
565 } \
566 TEST_F(spaceName##Test, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_12B) { \
567 SizeFootPrintGrowthLimitAndTrimDriver(12, spaceFn); \
568 } \
569 TEST_SizeFootPrintGrowthLimitAndTrim(16B, spaceName, spaceFn, 16) \
570 TEST_SizeFootPrintGrowthLimitAndTrim(24B, spaceName, spaceFn, 24) \
571 TEST_SizeFootPrintGrowthLimitAndTrim(32B, spaceName, spaceFn, 32) \
572 TEST_SizeFootPrintGrowthLimitAndTrim(64B, spaceName, spaceFn, 64) \
573 TEST_SizeFootPrintGrowthLimitAndTrim(128B, spaceName, spaceFn, 128) \
574 TEST_SizeFootPrintGrowthLimitAndTrim(1KB, spaceName, spaceFn, 1 * KB) \
575 TEST_SizeFootPrintGrowthLimitAndTrim(4KB, spaceName, spaceFn, 4 * KB) \
576 TEST_SizeFootPrintGrowthLimitAndTrim(1MB, spaceName, spaceFn, 1 * MB) \
577 TEST_SizeFootPrintGrowthLimitAndTrim(4MB, spaceName, spaceFn, 4 * MB) \
578 TEST_SizeFootPrintGrowthLimitAndTrim(8MB, spaceName, spaceFn, 8 * MB)
Ian Rogers3bb17a62012-01-27 23:56:44 -0800579
Ian Rogers1d54e732013-05-02 21:10:01 -0700580} // namespace space
581} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700582} // namespace art
Andreas Gampea7433512014-02-21 13:19:23 -0800583
584#endif // ART_RUNTIME_GC_SPACE_SPACE_TEST_H_