blob: 3e9e9f7a49401c94d540da75a440f10e743a2a11 [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
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080020#include <stdint.h>
Ian Rogers700a4022014-05-19 16:49:03 -070021#include <memory>
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080022
23#include "common_runtime_test.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070024#include "globals.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070025#include "mirror/array-inl.h"
26#include "mirror/object-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070027#include "scoped_thread_state_change.h"
28#include "zygote_space.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 Chartier5647d182014-03-07 15:00:39 -080036 jobject byte_array_class_;
37
38 SpaceTest() : byte_array_class_(nullptr) {
39 }
40
Mathieu Chartier1b54f9c2014-04-30 16:45:02 -070041 void AddSpace(ContinuousSpace* space, bool revoke = true) {
42 Heap* heap = Runtime::Current()->GetHeap();
43 if (revoke) {
44 heap->RevokeAllThreadLocalBuffers();
45 }
46 heap->AddSpace(space);
47 heap->SetSpaceAsDefault(space);
Ian Rogers1d54e732013-05-02 21:10:01 -070048 }
Mathieu Chartier5647d182014-03-07 15:00:39 -080049
50 mirror::Class* GetByteArrayClass(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070051 StackHandleScope<1> hs(self);
52 auto null_loader(hs.NewHandle<mirror::ClassLoader>(nullptr));
Mathieu Chartier5647d182014-03-07 15:00:39 -080053 if (byte_array_class_ == nullptr) {
54 mirror::Class* byte_array_class =
55 Runtime::Current()->GetClassLinker()->FindClass(self, "[B", null_loader);
56 EXPECT_TRUE(byte_array_class != nullptr);
57 byte_array_class_ = self->GetJniEnv()->NewLocalRef(byte_array_class);
58 EXPECT_TRUE(byte_array_class_ != nullptr);
59 }
60 return reinterpret_cast<mirror::Class*>(self->DecodeJObject(byte_array_class_));
61 }
62
63 mirror::Object* Alloc(space::MallocSpace* alloc_space, Thread* self, size_t bytes,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070064 size_t* bytes_allocated, size_t* usable_size,
65 size_t* bytes_tl_bulk_allocated)
Mathieu Chartier5647d182014-03-07 15:00:39 -080066 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070067 StackHandleScope<1> hs(self);
68 Handle<mirror::Class> byte_array_class(hs.NewHandle(GetByteArrayClass(self)));
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070069 mirror::Object* obj = alloc_space->Alloc(self, bytes, bytes_allocated, usable_size,
70 bytes_tl_bulk_allocated);
Mathieu Chartier5647d182014-03-07 15:00:39 -080071 if (obj != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070072 InstallClass(obj, byte_array_class.Get(), bytes);
Mathieu Chartier5647d182014-03-07 15:00:39 -080073 }
74 return obj;
75 }
76
77 mirror::Object* AllocWithGrowth(space::MallocSpace* alloc_space, Thread* self, size_t bytes,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070078 size_t* bytes_allocated, size_t* usable_size,
79 size_t* bytes_tl_bulk_allocated)
Mathieu Chartier5647d182014-03-07 15:00:39 -080080 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070081 StackHandleScope<1> hs(self);
82 Handle<mirror::Class> byte_array_class(hs.NewHandle(GetByteArrayClass(self)));
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070083 mirror::Object* obj = alloc_space->AllocWithGrowth(self, bytes, bytes_allocated, usable_size,
84 bytes_tl_bulk_allocated);
Mathieu Chartier5647d182014-03-07 15:00:39 -080085 if (obj != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070086 InstallClass(obj, byte_array_class.Get(), bytes);
Mathieu Chartier5647d182014-03-07 15:00:39 -080087 }
88 return obj;
89 }
90
91 void InstallClass(mirror::Object* o, mirror::Class* byte_array_class, size_t size)
Mathieu Chartier4e305412014-02-19 10:54:44 -080092 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080093 // Note the minimum size, which is the size of a zero-length byte array.
94 EXPECT_GE(size, SizeOfZeroLengthByteArray());
Mathieu Chartier4e305412014-02-19 10:54:44 -080095 EXPECT_TRUE(byte_array_class != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070096 o->SetClass(byte_array_class);
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070097 if (kUseBakerOrBrooksReadBarrier) {
98 // Like the proper heap object allocation, install and verify
99 // the correct read barrier pointer.
100 if (kUseBrooksReadBarrier) {
101 o->SetReadBarrierPointer(o);
102 }
103 o->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800104 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800105 mirror::Array* arr = o->AsArray<kVerifyNone>();
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800106 size_t header_size = SizeOfZeroLengthByteArray();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700107 int32_t length = size - header_size;
108 arr->SetLength(length);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800109 EXPECT_EQ(arr->SizeOf<kVerifyNone>(), size);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700110 }
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800111
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800112 static size_t SizeOfZeroLengthByteArray() {
113 return mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimByte)).Uint32Value();
114 }
115
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800116 typedef MallocSpace* (*CreateSpaceFn)(const std::string& name, size_t initial_size, size_t growth_limit,
Ian Rogers13735952014-10-08 12:43:28 -0700117 size_t capacity, uint8_t* requested_begin);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800118 void InitTestBody(CreateSpaceFn create_space);
119 void ZygoteSpaceTestBody(CreateSpaceFn create_space);
120 void AllocAndFreeTestBody(CreateSpaceFn create_space);
121 void AllocAndFreeListTestBody(CreateSpaceFn create_space);
122
123 void SizeFootPrintGrowthLimitAndTrimBody(MallocSpace* space, intptr_t object_size,
124 int round, size_t growth_limit);
125 void SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size, CreateSpaceFn create_space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800126};
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -0700127
Ian Rogers719d1a32014-03-06 12:13:39 -0800128static inline size_t test_rand(size_t* seed) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700129 *seed = *seed * 1103515245 + 12345;
130 return *seed;
131}
132
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800133void SpaceTest::InitTestBody(CreateSpaceFn create_space) {
Andreas Gampe369810a2015-01-14 19:53:31 -0800134 // This will lead to error messages in the log.
135 ScopedLogSeverity sls(LogSeverity::FATAL);
136
Carl Shapiro69759ea2011-07-21 18:13:35 -0700137 {
jeffhaoc1160702011-10-27 15:48:45 -0700138 // Init < max == growth
Ian Rogers700a4022014-05-19 16:49:03 -0700139 std::unique_ptr<Space> space(create_space("test", 16 * MB, 32 * MB, 32 * MB, nullptr));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800140 EXPECT_TRUE(space.get() != nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700141 }
142 {
jeffhaoc1160702011-10-27 15:48:45 -0700143 // Init == max == growth
Ian Rogers700a4022014-05-19 16:49:03 -0700144 std::unique_ptr<Space> space(create_space("test", 16 * MB, 16 * MB, 16 * MB, nullptr));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800145 EXPECT_TRUE(space.get() != nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700146 }
147 {
jeffhaoc1160702011-10-27 15:48:45 -0700148 // Init > max == growth
Ian Rogers700a4022014-05-19 16:49:03 -0700149 std::unique_ptr<Space> space(create_space("test", 32 * MB, 16 * MB, 16 * MB, nullptr));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800150 EXPECT_TRUE(space.get() == nullptr);
jeffhaoc1160702011-10-27 15:48:45 -0700151 }
152 {
153 // Growth == init < max
Ian Rogers700a4022014-05-19 16:49:03 -0700154 std::unique_ptr<Space> space(create_space("test", 16 * MB, 16 * MB, 32 * MB, nullptr));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800155 EXPECT_TRUE(space.get() != nullptr);
jeffhaoc1160702011-10-27 15:48:45 -0700156 }
157 {
158 // Growth < init < max
Ian Rogers700a4022014-05-19 16:49:03 -0700159 std::unique_ptr<Space> space(create_space("test", 16 * MB, 8 * MB, 32 * MB, nullptr));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800160 EXPECT_TRUE(space.get() == nullptr);
jeffhaoc1160702011-10-27 15:48:45 -0700161 }
162 {
163 // Init < growth < max
Ian Rogers700a4022014-05-19 16:49:03 -0700164 std::unique_ptr<Space> space(create_space("test", 8 * MB, 16 * MB, 32 * MB, nullptr));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800165 EXPECT_TRUE(space.get() != nullptr);
jeffhaoc1160702011-10-27 15:48:45 -0700166 }
167 {
168 // Init < max < growth
Ian Rogers700a4022014-05-19 16:49:03 -0700169 std::unique_ptr<Space> space(create_space("test", 8 * MB, 32 * MB, 16 * MB, nullptr));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800170 EXPECT_TRUE(space.get() == nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700171 }
172}
173
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700174// TODO: This test is not very good, we should improve it.
175// The test should do more allocations before the creation of the ZygoteSpace, and then do
176// allocations after the ZygoteSpace is created. The test should also do some GCs to ensure that
177// the GC works with the ZygoteSpace.
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800178void SpaceTest::ZygoteSpaceTestBody(CreateSpaceFn create_space) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800179 size_t dummy;
Mathieu Chartier4e305412014-02-19 10:54:44 -0800180 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, nullptr));
181 ASSERT_TRUE(space != nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700182
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800183 // Make space findable to the heap, will also delete space when runtime is cleaned up
184 AddSpace(space);
185 Thread* self = Thread::Current();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800186 ScopedObjectAccess soa(self);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700187
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800188 // Succeeds, fits without adjusting the footprint limit.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700189 size_t ptr1_bytes_allocated, ptr1_usable_size, ptr1_bytes_tl_bulk_allocated;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700190 StackHandleScope<3> hs(soa.Self());
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700191 MutableHandle<mirror::Object> ptr1(
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700192 hs.NewHandle(Alloc(space, self, 1 * MB, &ptr1_bytes_allocated, &ptr1_usable_size,
193 &ptr1_bytes_tl_bulk_allocated)));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700194 EXPECT_TRUE(ptr1.Get() != nullptr);
Ian Rogers6fac4472014-02-25 17:01:10 -0800195 EXPECT_LE(1U * MB, ptr1_bytes_allocated);
196 EXPECT_LE(1U * MB, ptr1_usable_size);
197 EXPECT_LE(ptr1_usable_size, ptr1_bytes_allocated);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700198 EXPECT_EQ(ptr1_bytes_tl_bulk_allocated, ptr1_bytes_allocated);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700199
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800200 // Fails, requires a higher footprint limit.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700201 mirror::Object* ptr2 = Alloc(space, self, 8 * MB, &dummy, nullptr, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800202 EXPECT_TRUE(ptr2 == nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700203
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800204 // Succeeds, adjusts the footprint.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700205 size_t ptr3_bytes_allocated, ptr3_usable_size, ptr3_bytes_tl_bulk_allocated;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700206 MutableHandle<mirror::Object> ptr3(
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700207 hs.NewHandle(AllocWithGrowth(space, self, 8 * MB, &ptr3_bytes_allocated, &ptr3_usable_size,
208 &ptr3_bytes_tl_bulk_allocated)));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700209 EXPECT_TRUE(ptr3.Get() != nullptr);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800210 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
Ian Rogers6fac4472014-02-25 17:01:10 -0800211 EXPECT_LE(8U * MB, ptr3_usable_size);
212 EXPECT_LE(ptr3_usable_size, ptr3_bytes_allocated);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700213 EXPECT_EQ(ptr3_bytes_tl_bulk_allocated, ptr3_bytes_allocated);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700214
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800215 // Fails, requires a higher footprint limit.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700216 mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy, nullptr, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800217 EXPECT_TRUE(ptr4 == nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700218
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800219 // Also fails, requires a higher allowed footprint.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700220 mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy, nullptr, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800221 EXPECT_TRUE(ptr5 == nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700222
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800223 // Release some memory.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700224 size_t free3 = space->AllocationSize(ptr3.Get(), nullptr);
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800225 EXPECT_EQ(free3, ptr3_bytes_allocated);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700226 EXPECT_EQ(free3, space->Free(self, ptr3.Assign(nullptr)));
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800227 EXPECT_LE(8U * MB, free3);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700228
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800229 // Succeeds, now that memory has been freed.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700230 size_t ptr6_bytes_allocated, ptr6_usable_size, ptr6_bytes_tl_bulk_allocated;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700231 Handle<mirror::Object> ptr6(
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700232 hs.NewHandle(AllocWithGrowth(space, self, 9 * MB, &ptr6_bytes_allocated, &ptr6_usable_size,
233 &ptr6_bytes_tl_bulk_allocated)));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700234 EXPECT_TRUE(ptr6.Get() != nullptr);
Ian Rogers6fac4472014-02-25 17:01:10 -0800235 EXPECT_LE(9U * MB, ptr6_bytes_allocated);
236 EXPECT_LE(9U * MB, ptr6_usable_size);
237 EXPECT_LE(ptr6_usable_size, ptr6_bytes_allocated);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700238 EXPECT_EQ(ptr6_bytes_tl_bulk_allocated, ptr6_bytes_allocated);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700239
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800240 // Final clean up.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700241 size_t free1 = space->AllocationSize(ptr1.Get(), nullptr);
242 space->Free(self, ptr1.Assign(nullptr));
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800243 EXPECT_LE(1U * MB, free1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700244
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800245 // Make sure that the zygote space isn't directly at the start of the space.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700246 EXPECT_TRUE(space->Alloc(self, 1U * MB, &dummy, nullptr, &dummy) != nullptr);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800247
248 gc::Heap* heap = Runtime::Current()->GetHeap();
249 space::Space* old_space = space;
250 heap->RemoveSpace(old_space);
Mathieu Chartier1b54f9c2014-04-30 16:45:02 -0700251 heap->RevokeAllThreadLocalBuffers();
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800252 space::ZygoteSpace* zygote_space = space->CreateZygoteSpace("alloc space",
253 heap->IsLowMemoryMode(),
254 &space);
255 delete old_space;
256 // Add the zygote space.
Mathieu Chartier1b54f9c2014-04-30 16:45:02 -0700257 AddSpace(zygote_space, false);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700258
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800259 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier1b54f9c2014-04-30 16:45:02 -0700260 AddSpace(space, false);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700261
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800262 // Succeeds, fits without adjusting the footprint limit.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700263 ptr1.Assign(Alloc(space, self, 1 * MB, &ptr1_bytes_allocated, &ptr1_usable_size,
264 &ptr1_bytes_tl_bulk_allocated));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700265 EXPECT_TRUE(ptr1.Get() != nullptr);
Ian Rogers6fac4472014-02-25 17:01:10 -0800266 EXPECT_LE(1U * MB, ptr1_bytes_allocated);
267 EXPECT_LE(1U * MB, ptr1_usable_size);
268 EXPECT_LE(ptr1_usable_size, ptr1_bytes_allocated);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700269 EXPECT_EQ(ptr1_bytes_tl_bulk_allocated, ptr1_bytes_allocated);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700270
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800271 // Fails, requires a higher footprint limit.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700272 ptr2 = Alloc(space, self, 8 * MB, &dummy, nullptr, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800273 EXPECT_TRUE(ptr2 == nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700274
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800275 // Succeeds, adjusts the footprint.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700276 ptr3.Assign(AllocWithGrowth(space, self, 2 * MB, &ptr3_bytes_allocated, &ptr3_usable_size,
277 &ptr3_bytes_tl_bulk_allocated));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700278 EXPECT_TRUE(ptr3.Get() != nullptr);
Ian Rogers6fac4472014-02-25 17:01:10 -0800279 EXPECT_LE(2U * MB, ptr3_bytes_allocated);
280 EXPECT_LE(2U * MB, ptr3_usable_size);
281 EXPECT_LE(ptr3_usable_size, ptr3_bytes_allocated);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700282 EXPECT_EQ(ptr3_bytes_tl_bulk_allocated, ptr3_bytes_allocated);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700283 space->Free(self, ptr3.Assign(nullptr));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700284
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800285 // Final clean up.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700286 free1 = space->AllocationSize(ptr1.Get(), nullptr);
287 space->Free(self, ptr1.Assign(nullptr));
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800288 EXPECT_LE(1U * MB, free1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700289}
290
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800291void SpaceTest::AllocAndFreeTestBody(CreateSpaceFn create_space) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700292 size_t dummy = 0;
Mathieu Chartier4e305412014-02-19 10:54:44 -0800293 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, nullptr));
294 ASSERT_TRUE(space != nullptr);
Ian Rogers50b35e22012-10-04 10:09:15 -0700295 Thread* self = Thread::Current();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800296 ScopedObjectAccess soa(self);
Ian Rogers30fab402012-01-23 15:43:46 -0800297
Ian Rogers3bb17a62012-01-27 23:56:44 -0800298 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700299 AddSpace(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700300
Ian Rogers3bb17a62012-01-27 23:56:44 -0800301 // Succeeds, fits without adjusting the footprint limit.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700302 size_t ptr1_bytes_allocated, ptr1_usable_size, ptr1_bytes_tl_bulk_allocated;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700303 StackHandleScope<3> hs(soa.Self());
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700304 MutableHandle<mirror::Object> ptr1(
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700305 hs.NewHandle(Alloc(space, self, 1 * MB, &ptr1_bytes_allocated, &ptr1_usable_size,
306 &ptr1_bytes_tl_bulk_allocated)));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700307 EXPECT_TRUE(ptr1.Get() != nullptr);
Ian Rogers6fac4472014-02-25 17:01:10 -0800308 EXPECT_LE(1U * MB, ptr1_bytes_allocated);
309 EXPECT_LE(1U * MB, ptr1_usable_size);
310 EXPECT_LE(ptr1_usable_size, ptr1_bytes_allocated);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700311 EXPECT_EQ(ptr1_bytes_tl_bulk_allocated, ptr1_bytes_allocated);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700312
Ian Rogers3bb17a62012-01-27 23:56:44 -0800313 // Fails, requires a higher footprint limit.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700314 mirror::Object* ptr2 = Alloc(space, self, 8 * MB, &dummy, nullptr, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800315 EXPECT_TRUE(ptr2 == nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700316
317 // Succeeds, adjusts the footprint.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700318 size_t ptr3_bytes_allocated, ptr3_usable_size, ptr3_bytes_tl_bulk_allocated;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700319 MutableHandle<mirror::Object> ptr3(
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700320 hs.NewHandle(AllocWithGrowth(space, self, 8 * MB, &ptr3_bytes_allocated, &ptr3_usable_size,
321 &ptr3_bytes_tl_bulk_allocated)));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700322 EXPECT_TRUE(ptr3.Get() != nullptr);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700323 EXPECT_LE(8U * MB, ptr3_bytes_allocated);
Ian Rogers6fac4472014-02-25 17:01:10 -0800324 EXPECT_LE(8U * MB, ptr3_usable_size);
325 EXPECT_LE(ptr3_usable_size, ptr3_bytes_allocated);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700326 EXPECT_EQ(ptr3_bytes_tl_bulk_allocated, ptr3_bytes_allocated);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700327
Ian Rogers3bb17a62012-01-27 23:56:44 -0800328 // Fails, requires a higher footprint limit.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700329 mirror::Object* ptr4 = Alloc(space, self, 8 * MB, &dummy, nullptr, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800330 EXPECT_TRUE(ptr4 == nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700331
332 // Also fails, requires a higher allowed footprint.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700333 mirror::Object* ptr5 = AllocWithGrowth(space, self, 8 * MB, &dummy, nullptr, &dummy);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800334 EXPECT_TRUE(ptr5 == nullptr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700335
336 // Release some memory.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700337 size_t free3 = space->AllocationSize(ptr3.Get(), nullptr);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700338 EXPECT_EQ(free3, ptr3_bytes_allocated);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700339 space->Free(self, ptr3.Assign(nullptr));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700340 EXPECT_LE(8U * MB, free3);
341
342 // Succeeds, now that memory has been freed.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700343 size_t ptr6_bytes_allocated, ptr6_usable_size, ptr6_bytes_tl_bulk_allocated;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700344 Handle<mirror::Object> ptr6(
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700345 hs.NewHandle(AllocWithGrowth(space, self, 9 * MB, &ptr6_bytes_allocated, &ptr6_usable_size,
346 &ptr6_bytes_tl_bulk_allocated)));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700347 EXPECT_TRUE(ptr6.Get() != nullptr);
Ian Rogers6fac4472014-02-25 17:01:10 -0800348 EXPECT_LE(9U * MB, ptr6_bytes_allocated);
349 EXPECT_LE(9U * MB, ptr6_usable_size);
350 EXPECT_LE(ptr6_usable_size, ptr6_bytes_allocated);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700351 EXPECT_EQ(ptr6_bytes_tl_bulk_allocated, ptr6_bytes_allocated);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700352
353 // Final clean up.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700354 size_t free1 = space->AllocationSize(ptr1.Get(), nullptr);
355 space->Free(self, ptr1.Assign(nullptr));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700356 EXPECT_LE(1U * MB, free1);
357}
358
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800359void SpaceTest::AllocAndFreeListTestBody(CreateSpaceFn create_space) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800360 MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, nullptr));
361 ASSERT_TRUE(space != nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800362
363 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700364 AddSpace(space);
Ian Rogers50b35e22012-10-04 10:09:15 -0700365 Thread* self = Thread::Current();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800366 ScopedObjectAccess soa(self);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800367
368 // Succeeds, fits without adjusting the max allowed footprint.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800369 mirror::Object* lots_of_objects[1024];
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700370 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700371 size_t allocation_size, usable_size, bytes_tl_bulk_allocated;
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800372 size_t size_of_zero_length_byte_array = SizeOfZeroLengthByteArray();
Mathieu Chartier5647d182014-03-07 15:00:39 -0800373 lots_of_objects[i] = Alloc(space, self, size_of_zero_length_byte_array, &allocation_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700374 &usable_size, &bytes_tl_bulk_allocated);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800375 EXPECT_TRUE(lots_of_objects[i] != nullptr);
Ian Rogers6fac4472014-02-25 17:01:10 -0800376 size_t computed_usable_size;
377 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i], &computed_usable_size));
378 EXPECT_EQ(usable_size, computed_usable_size);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700379 EXPECT_TRUE(bytes_tl_bulk_allocated == 0 ||
380 bytes_tl_bulk_allocated >= allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800381 }
382
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700383 // Release memory.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800384 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800385
386 // Succeeds, fits by adjusting the max allowed footprint.
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700387 for (size_t i = 0; i < arraysize(lots_of_objects); i++) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700388 size_t allocation_size, usable_size, bytes_tl_bulk_allocated;
389 lots_of_objects[i] = AllocWithGrowth(space, self, 1024, &allocation_size, &usable_size,
390 &bytes_tl_bulk_allocated);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800391 EXPECT_TRUE(lots_of_objects[i] != nullptr);
Ian Rogers6fac4472014-02-25 17:01:10 -0800392 size_t computed_usable_size;
393 EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i], &computed_usable_size));
394 EXPECT_EQ(usable_size, computed_usable_size);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700395 EXPECT_TRUE(bytes_tl_bulk_allocated == 0 ||
396 bytes_tl_bulk_allocated >= allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800397 }
398
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700399 // Release memory.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800400 space->FreeList(self, arraysize(lots_of_objects), lots_of_objects);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800401}
402
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800403void SpaceTest::SizeFootPrintGrowthLimitAndTrimBody(MallocSpace* space, intptr_t object_size,
Ian Rogers3bb17a62012-01-27 23:56:44 -0800404 int round, size_t growth_limit) {
405 if (((object_size > 0 && object_size >= static_cast<intptr_t>(growth_limit))) ||
406 ((object_size < 0 && -object_size >= static_cast<intptr_t>(growth_limit)))) {
407 // No allocation can succeed
408 return;
409 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800410
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700411 // The space's footprint equals amount of resources requested from system
412 size_t footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800413
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700414 // The space must at least have its book keeping allocated
Ian Rogers3bb17a62012-01-27 23:56:44 -0800415 EXPECT_GT(footprint, 0u);
416
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700417 // But it shouldn't exceed the initial size
Ian Rogers3bb17a62012-01-27 23:56:44 -0800418 EXPECT_LE(footprint, growth_limit);
419
420 // space's size shouldn't exceed the initial size
421 EXPECT_LE(space->Size(), growth_limit);
422
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700423 // this invariant should always hold or else the space has grown to be larger than what the
Ian Rogers3bb17a62012-01-27 23:56:44 -0800424 // space believes its size is (which will break invariants)
425 EXPECT_GE(space->Size(), footprint);
426
427 // Fill the space with lots of small objects up to the growth limit
428 size_t max_objects = (growth_limit / (object_size > 0 ? object_size : 8)) + 1;
Ian Rogers700a4022014-05-19 16:49:03 -0700429 std::unique_ptr<mirror::Object*[]> lots_of_objects(new mirror::Object*[max_objects]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800430 size_t last_object = 0; // last object for which allocation succeeded
431 size_t amount_allocated = 0; // amount of space allocated
Ian Rogers50b35e22012-10-04 10:09:15 -0700432 Thread* self = Thread::Current();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800433 ScopedObjectAccess soa(self);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700434 size_t rand_seed = 123456789;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700435 for (size_t i = 0; i < max_objects; i++) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800436 size_t alloc_fails = 0; // number of failed allocations
437 size_t max_fails = 30; // number of times we fail allocation before giving up
438 for (; alloc_fails < max_fails; alloc_fails++) {
439 size_t alloc_size;
440 if (object_size > 0) {
441 alloc_size = object_size;
442 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700443 alloc_size = test_rand(&rand_seed) % static_cast<size_t>(-object_size);
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800444 // Note the minimum size, which is the size of a zero-length byte array.
445 size_t size_of_zero_length_byte_array = SizeOfZeroLengthByteArray();
446 if (alloc_size < size_of_zero_length_byte_array) {
447 alloc_size = size_of_zero_length_byte_array;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800448 }
449 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700450 StackHandleScope<1> hs(soa.Self());
451 auto object(hs.NewHandle<mirror::Object>(nullptr));
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700452 size_t bytes_allocated = 0;
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700453 size_t bytes_tl_bulk_allocated;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800454 if (round <= 1) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700455 object.Assign(Alloc(space, self, alloc_size, &bytes_allocated, nullptr,
456 &bytes_tl_bulk_allocated));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800457 } else {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700458 object.Assign(AllocWithGrowth(space, self, alloc_size, &bytes_allocated, nullptr,
459 &bytes_tl_bulk_allocated));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800460 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700461 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800462 EXPECT_GE(space->Size(), footprint); // invariant
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700463 if (object.Get() != nullptr) { // allocation succeeded
464 lots_of_objects[i] = object.Get();
465 size_t allocation_size = space->AllocationSize(object.Get(), nullptr);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700466 EXPECT_EQ(bytes_allocated, allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800467 if (object_size > 0) {
468 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
469 } else {
470 EXPECT_GE(allocation_size, 8u);
471 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700472 EXPECT_TRUE(bytes_tl_bulk_allocated == 0 ||
473 bytes_tl_bulk_allocated >= allocation_size);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800474 amount_allocated += allocation_size;
475 break;
476 }
477 }
478 if (alloc_fails == max_fails) {
479 last_object = i;
480 break;
481 }
482 }
483 CHECK_NE(last_object, 0u); // we should have filled the space
484 EXPECT_GT(amount_allocated, 0u);
485
486 // We shouldn't have gone past the growth_limit
487 EXPECT_LE(amount_allocated, growth_limit);
488 EXPECT_LE(footprint, growth_limit);
489 EXPECT_LE(space->Size(), growth_limit);
490
491 // footprint and size should agree with amount allocated
492 EXPECT_GE(footprint, amount_allocated);
493 EXPECT_GE(space->Size(), amount_allocated);
494
495 // Release storage in a semi-adhoc manner
496 size_t free_increment = 96;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700497 while (true) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800498 {
499 ScopedThreadStateChange tsc(self, kNative);
500 // Give the space a haircut.
501 space->Trim();
502 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800503
504 // Bounds sanity
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700505 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800506 EXPECT_LE(amount_allocated, growth_limit);
507 EXPECT_GE(footprint, amount_allocated);
508 EXPECT_LE(footprint, growth_limit);
509 EXPECT_GE(space->Size(), amount_allocated);
510 EXPECT_LE(space->Size(), growth_limit);
511
512 if (free_increment == 0) {
513 break;
514 }
515
Mathieu Chartier4e305412014-02-19 10:54:44 -0800516 // Free some objects
517 for (size_t i = 0; i < last_object; i += free_increment) {
518 mirror::Object* object = lots_of_objects.get()[i];
519 if (object == nullptr) {
520 continue;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800521 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800522 size_t allocation_size = space->AllocationSize(object, nullptr);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800523 if (object_size > 0) {
524 EXPECT_GE(allocation_size, static_cast<size_t>(object_size));
525 } else {
526 EXPECT_GE(allocation_size, 8u);
527 }
528 space->Free(self, object);
529 lots_of_objects.get()[i] = nullptr;
530 amount_allocated -= allocation_size;
531 footprint = space->GetFootprint();
532 EXPECT_GE(space->Size(), footprint); // invariant
Ian Rogers3bb17a62012-01-27 23:56:44 -0800533 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800534
535 free_increment >>= 1;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800536 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800537
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700538 // The space has become empty here before allocating a large object
539 // below. For RosAlloc, revoke thread-local runs, which are kept
540 // even when empty for a performance reason, so that they won't
541 // cause the following large object allocation to fail due to
542 // potential fragmentation. Note they are normally revoked at each
543 // GC (but no GC here.)
544 space->RevokeAllThreadLocalBuffers();
545
Ian Rogers3bb17a62012-01-27 23:56:44 -0800546 // All memory was released, try a large allocation to check freed memory is being coalesced
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700547 StackHandleScope<1> hs(soa.Self());
548 auto large_object(hs.NewHandle<mirror::Object>(nullptr));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800549 size_t three_quarters_space = (growth_limit / 2) + (growth_limit / 4);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700550 size_t bytes_allocated = 0;
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700551 size_t bytes_tl_bulk_allocated;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800552 if (round <= 1) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700553 large_object.Assign(Alloc(space, self, three_quarters_space, &bytes_allocated, nullptr,
554 &bytes_tl_bulk_allocated));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800555 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700556 large_object.Assign(AllocWithGrowth(space, self, three_quarters_space, &bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700557 nullptr, &bytes_tl_bulk_allocated));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800558 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700559 EXPECT_TRUE(large_object.Get() != nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800560
561 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700562 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800563 EXPECT_LE(footprint, growth_limit);
564 EXPECT_GE(space->Size(), footprint);
565 EXPECT_LE(space->Size(), growth_limit);
566
567 // Clean up
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700568 space->Free(self, large_object.Assign(nullptr));
Mathieu Chartier4e305412014-02-19 10:54:44 -0800569
Ian Rogers3bb17a62012-01-27 23:56:44 -0800570 // Sanity check footprint
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700571 footprint = space->GetFootprint();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800572 EXPECT_LE(footprint, growth_limit);
573 EXPECT_GE(space->Size(), footprint);
574 EXPECT_LE(space->Size(), growth_limit);
575}
576
Hiroshi Yamauchi3ddbd422013-12-06 17:43:36 -0800577void SpaceTest::SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size, CreateSpaceFn create_space) {
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800578 if (object_size < SizeOfZeroLengthByteArray()) {
579 // Too small for the object layout/model.
580 return;
581 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800582 size_t initial_size = 4 * MB;
583 size_t growth_limit = 8 * MB;
584 size_t capacity = 16 * MB;
Mathieu Chartier4e305412014-02-19 10:54:44 -0800585 MallocSpace* space(create_space("test", initial_size, growth_limit, capacity, nullptr));
586 ASSERT_TRUE(space != nullptr);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800587
588 // Basic sanity
589 EXPECT_EQ(space->Capacity(), growth_limit);
590 EXPECT_EQ(space->NonGrowthLimitCapacity(), capacity);
591
592 // Make space findable to the heap, will also delete space when runtime is cleaned up
Mathieu Chartier590fee92013-09-13 13:46:47 -0700593 AddSpace(space);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800594
595 // In this round we don't allocate with growth and therefore can't grow past the initial size.
596 // This effectively makes the growth_limit the initial_size, so assert this.
597 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 1, initial_size);
598 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 2, growth_limit);
599 // Remove growth limit
600 space->ClearGrowthLimit();
601 EXPECT_EQ(space->Capacity(), capacity);
602 SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 3, capacity);
603}
604
Andreas Gampe24651ec2014-02-27 13:26:16 -0800605#define TEST_SizeFootPrintGrowthLimitAndTrimStatic(name, spaceName, spaceFn, size) \
606 TEST_F(spaceName##StaticTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name) { \
Andreas Gampea7433512014-02-21 13:19:23 -0800607 SizeFootPrintGrowthLimitAndTrimDriver(size, spaceFn); \
Andreas Gampe24651ec2014-02-27 13:26:16 -0800608 }
609
610#define TEST_SizeFootPrintGrowthLimitAndTrimRandom(name, spaceName, spaceFn, size) \
611 TEST_F(spaceName##RandomTest, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name) { \
Andreas Gampea7433512014-02-21 13:19:23 -0800612 SizeFootPrintGrowthLimitAndTrimDriver(-size, spaceFn); \
Ian Rogers3bb17a62012-01-27 23:56:44 -0800613 }
614
Andreas Gampe24651ec2014-02-27 13:26:16 -0800615#define TEST_SPACE_CREATE_FN_BASE(spaceName, spaceFn) \
616 class spaceName##BaseTest : public SpaceTest { \
Andreas Gampea7433512014-02-21 13:19:23 -0800617 }; \
618 \
Andreas Gampe24651ec2014-02-27 13:26:16 -0800619 TEST_F(spaceName##BaseTest, Init) { \
Andreas Gampea7433512014-02-21 13:19:23 -0800620 InitTestBody(spaceFn); \
621 } \
Andreas Gampe24651ec2014-02-27 13:26:16 -0800622 TEST_F(spaceName##BaseTest, ZygoteSpace) { \
Andreas Gampea7433512014-02-21 13:19:23 -0800623 ZygoteSpaceTestBody(spaceFn); \
624 } \
Andreas Gampe24651ec2014-02-27 13:26:16 -0800625 TEST_F(spaceName##BaseTest, AllocAndFree) { \
Andreas Gampea7433512014-02-21 13:19:23 -0800626 AllocAndFreeTestBody(spaceFn); \
627 } \
Andreas Gampe24651ec2014-02-27 13:26:16 -0800628 TEST_F(spaceName##BaseTest, AllocAndFreeList) { \
Andreas Gampea7433512014-02-21 13:19:23 -0800629 AllocAndFreeListTestBody(spaceFn); \
Andreas Gampe24651ec2014-02-27 13:26:16 -0800630 }
631
632#define TEST_SPACE_CREATE_FN_STATIC(spaceName, spaceFn) \
633 class spaceName##StaticTest : public SpaceTest { \
634 }; \
635 \
636 TEST_SizeFootPrintGrowthLimitAndTrimStatic(12B, spaceName, spaceFn, 12) \
637 TEST_SizeFootPrintGrowthLimitAndTrimStatic(16B, spaceName, spaceFn, 16) \
638 TEST_SizeFootPrintGrowthLimitAndTrimStatic(24B, spaceName, spaceFn, 24) \
639 TEST_SizeFootPrintGrowthLimitAndTrimStatic(32B, spaceName, spaceFn, 32) \
640 TEST_SizeFootPrintGrowthLimitAndTrimStatic(64B, spaceName, spaceFn, 64) \
641 TEST_SizeFootPrintGrowthLimitAndTrimStatic(128B, spaceName, spaceFn, 128) \
642 TEST_SizeFootPrintGrowthLimitAndTrimStatic(1KB, spaceName, spaceFn, 1 * KB) \
643 TEST_SizeFootPrintGrowthLimitAndTrimStatic(4KB, spaceName, spaceFn, 4 * KB) \
644 TEST_SizeFootPrintGrowthLimitAndTrimStatic(1MB, spaceName, spaceFn, 1 * MB) \
645 TEST_SizeFootPrintGrowthLimitAndTrimStatic(4MB, spaceName, spaceFn, 4 * MB) \
646 TEST_SizeFootPrintGrowthLimitAndTrimStatic(8MB, spaceName, spaceFn, 8 * MB)
647
648#define TEST_SPACE_CREATE_FN_RANDOM(spaceName, spaceFn) \
649 class spaceName##RandomTest : public SpaceTest { \
650 }; \
651 \
652 TEST_SizeFootPrintGrowthLimitAndTrimRandom(16B, spaceName, spaceFn, 16) \
653 TEST_SizeFootPrintGrowthLimitAndTrimRandom(24B, spaceName, spaceFn, 24) \
654 TEST_SizeFootPrintGrowthLimitAndTrimRandom(32B, spaceName, spaceFn, 32) \
655 TEST_SizeFootPrintGrowthLimitAndTrimRandom(64B, spaceName, spaceFn, 64) \
656 TEST_SizeFootPrintGrowthLimitAndTrimRandom(128B, spaceName, spaceFn, 128) \
657 TEST_SizeFootPrintGrowthLimitAndTrimRandom(1KB, spaceName, spaceFn, 1 * KB) \
658 TEST_SizeFootPrintGrowthLimitAndTrimRandom(4KB, spaceName, spaceFn, 4 * KB) \
659 TEST_SizeFootPrintGrowthLimitAndTrimRandom(1MB, spaceName, spaceFn, 1 * MB) \
660 TEST_SizeFootPrintGrowthLimitAndTrimRandom(4MB, spaceName, spaceFn, 4 * MB) \
661 TEST_SizeFootPrintGrowthLimitAndTrimRandom(8MB, spaceName, spaceFn, 8 * MB)
Ian Rogers3bb17a62012-01-27 23:56:44 -0800662
Ian Rogers1d54e732013-05-02 21:10:01 -0700663} // namespace space
664} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700665} // namespace art
Andreas Gampea7433512014-02-21 13:19:23 -0800666
667#endif // ART_RUNTIME_GC_SPACE_SPACE_TEST_H_