Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 16 | |
Andreas Gampe | a743351 | 2014-02-21 13:19:23 -0800 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_GC_SPACE_SPACE_TEST_H_ |
| 18 | #define ART_RUNTIME_GC_SPACE_SPACE_TEST_H_ |
| 19 | |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 20 | #include "zygote_space.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 21 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | |
| 24 | #include "common_runtime_test.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 25 | #include "globals.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 26 | #include "UniquePtr.h" |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 27 | #include "mirror/array-inl.h" |
| 28 | #include "mirror/object-inl.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 29 | |
| 30 | namespace art { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 31 | namespace gc { |
| 32 | namespace space { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 33 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 34 | class SpaceTest : public CommonRuntimeTest { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 35 | public: |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 36 | jobject byte_array_class_; |
| 37 | |
| 38 | SpaceTest() : byte_array_class_(nullptr) { |
| 39 | } |
| 40 | |
Mathieu Chartier | 1b54f9c | 2014-04-30 16:45:02 -0700 | [diff] [blame] | 41 | 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 Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 48 | } |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 49 | |
| 50 | mirror::Class* GetByteArrayClass(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 51 | SirtRef<mirror::ClassLoader> null_loader(self, nullptr); |
| 52 | if (byte_array_class_ == nullptr) { |
| 53 | mirror::Class* byte_array_class = |
| 54 | Runtime::Current()->GetClassLinker()->FindClass(self, "[B", null_loader); |
| 55 | EXPECT_TRUE(byte_array_class != nullptr); |
| 56 | byte_array_class_ = self->GetJniEnv()->NewLocalRef(byte_array_class); |
| 57 | EXPECT_TRUE(byte_array_class_ != nullptr); |
| 58 | } |
| 59 | return reinterpret_cast<mirror::Class*>(self->DecodeJObject(byte_array_class_)); |
| 60 | } |
| 61 | |
| 62 | mirror::Object* Alloc(space::MallocSpace* alloc_space, Thread* self, size_t bytes, |
| 63 | size_t* bytes_allocated, size_t* usable_size) |
| 64 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 65 | SirtRef<mirror::Class> byte_array_class(self, GetByteArrayClass(self)); |
| 66 | mirror::Object* obj = alloc_space->Alloc(self, bytes, bytes_allocated, usable_size); |
| 67 | if (obj != nullptr) { |
| 68 | InstallClass(obj, byte_array_class.get(), bytes); |
| 69 | } |
| 70 | return obj; |
| 71 | } |
| 72 | |
| 73 | mirror::Object* AllocWithGrowth(space::MallocSpace* alloc_space, Thread* self, size_t bytes, |
| 74 | size_t* bytes_allocated, size_t* usable_size) |
| 75 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 76 | SirtRef<mirror::Class> byte_array_class(self, GetByteArrayClass(self)); |
| 77 | mirror::Object* obj = alloc_space->AllocWithGrowth(self, bytes, bytes_allocated, usable_size); |
| 78 | if (obj != nullptr) { |
| 79 | InstallClass(obj, byte_array_class.get(), bytes); |
| 80 | } |
| 81 | return obj; |
| 82 | } |
| 83 | |
| 84 | void InstallClass(mirror::Object* o, mirror::Class* byte_array_class, size_t size) |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 85 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 86 | // Note the minimum size, which is the size of a zero-length byte array. |
| 87 | EXPECT_GE(size, SizeOfZeroLengthByteArray()); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 88 | EXPECT_TRUE(byte_array_class != nullptr); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 89 | o->SetClass(byte_array_class); |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 90 | if (kUseBakerOrBrooksReadBarrier) { |
| 91 | // Like the proper heap object allocation, install and verify |
| 92 | // the correct read barrier pointer. |
| 93 | if (kUseBrooksReadBarrier) { |
| 94 | o->SetReadBarrierPointer(o); |
| 95 | } |
| 96 | o->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 97 | } |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 98 | mirror::Array* arr = o->AsArray<kVerifyNone>(); |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 99 | size_t header_size = SizeOfZeroLengthByteArray(); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 100 | int32_t length = size - header_size; |
| 101 | arr->SetLength(length); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 102 | EXPECT_EQ(arr->SizeOf<kVerifyNone>(), size); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 103 | } |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 104 | |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 105 | static size_t SizeOfZeroLengthByteArray() { |
| 106 | return mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimByte)).Uint32Value(); |
| 107 | } |
| 108 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 109 | typedef MallocSpace* (*CreateSpaceFn)(const std::string& name, size_t initial_size, size_t growth_limit, |
| 110 | size_t capacity, byte* requested_begin); |
| 111 | void InitTestBody(CreateSpaceFn create_space); |
| 112 | void ZygoteSpaceTestBody(CreateSpaceFn create_space); |
| 113 | void AllocAndFreeTestBody(CreateSpaceFn create_space); |
| 114 | void AllocAndFreeListTestBody(CreateSpaceFn create_space); |
| 115 | |
| 116 | void SizeFootPrintGrowthLimitAndTrimBody(MallocSpace* space, intptr_t object_size, |
| 117 | int round, size_t growth_limit); |
| 118 | void SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size, CreateSpaceFn create_space); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 119 | }; |
Brian Carlstrom | 9b7f2c2 | 2011-09-27 14:35:04 -0700 | [diff] [blame] | 120 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 121 | static inline size_t test_rand(size_t* seed) { |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 122 | *seed = *seed * 1103515245 + 12345; |
| 123 | return *seed; |
| 124 | } |
| 125 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 126 | void SpaceTest::InitTestBody(CreateSpaceFn create_space) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 127 | { |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 128 | // Init < max == growth |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 129 | UniquePtr<Space> space(create_space("test", 16 * MB, 32 * MB, 32 * MB, nullptr)); |
| 130 | EXPECT_TRUE(space.get() != nullptr); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 131 | } |
| 132 | { |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 133 | // Init == max == growth |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 134 | UniquePtr<Space> space(create_space("test", 16 * MB, 16 * MB, 16 * MB, nullptr)); |
| 135 | EXPECT_TRUE(space.get() != nullptr); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 136 | } |
| 137 | { |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 138 | // Init > max == growth |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 139 | UniquePtr<Space> space(create_space("test", 32 * MB, 16 * MB, 16 * MB, nullptr)); |
| 140 | EXPECT_TRUE(space.get() == nullptr); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 141 | } |
| 142 | { |
| 143 | // Growth == init < max |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 144 | UniquePtr<Space> space(create_space("test", 16 * MB, 16 * MB, 32 * MB, nullptr)); |
| 145 | EXPECT_TRUE(space.get() != nullptr); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 146 | } |
| 147 | { |
| 148 | // Growth < init < max |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 149 | UniquePtr<Space> space(create_space("test", 16 * MB, 8 * MB, 32 * MB, nullptr)); |
| 150 | EXPECT_TRUE(space.get() == nullptr); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 151 | } |
| 152 | { |
| 153 | // Init < growth < max |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 154 | UniquePtr<Space> space(create_space("test", 8 * MB, 16 * MB, 32 * MB, nullptr)); |
| 155 | EXPECT_TRUE(space.get() != nullptr); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 156 | } |
| 157 | { |
| 158 | // Init < max < growth |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 159 | UniquePtr<Space> space(create_space("test", 8 * MB, 32 * MB, 16 * MB, nullptr)); |
| 160 | EXPECT_TRUE(space.get() == nullptr); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 164 | // TODO: This test is not very good, we should improve it. |
| 165 | // The test should do more allocations before the creation of the ZygoteSpace, and then do |
| 166 | // allocations after the ZygoteSpace is created. The test should also do some GCs to ensure that |
| 167 | // the GC works with the ZygoteSpace. |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 168 | void SpaceTest::ZygoteSpaceTestBody(CreateSpaceFn create_space) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 169 | size_t dummy; |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 170 | MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, nullptr)); |
| 171 | ASSERT_TRUE(space != nullptr); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 172 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 173 | // Make space findable to the heap, will also delete space when runtime is cleaned up |
| 174 | AddSpace(space); |
| 175 | Thread* self = Thread::Current(); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 176 | ScopedObjectAccess soa(self); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 177 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 178 | // Succeeds, fits without adjusting the footprint limit. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 179 | size_t ptr1_bytes_allocated, ptr1_usable_size; |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 180 | SirtRef<mirror::Object> ptr1(self, Alloc(space, self, 1 * MB, &ptr1_bytes_allocated, |
| 181 | &ptr1_usable_size)); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 182 | EXPECT_TRUE(ptr1.get() != nullptr); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 183 | EXPECT_LE(1U * MB, ptr1_bytes_allocated); |
| 184 | EXPECT_LE(1U * MB, ptr1_usable_size); |
| 185 | EXPECT_LE(ptr1_usable_size, ptr1_bytes_allocated); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 186 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 187 | // Fails, requires a higher footprint limit. |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 188 | mirror::Object* ptr2 = Alloc(space, self, 8 * MB, &dummy, nullptr); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 189 | EXPECT_TRUE(ptr2 == nullptr); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 190 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 191 | // Succeeds, adjusts the footprint. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 192 | size_t ptr3_bytes_allocated, ptr3_usable_size; |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 193 | SirtRef<mirror::Object> ptr3(self, AllocWithGrowth(space, self, 8 * MB, &ptr3_bytes_allocated, |
| 194 | &ptr3_usable_size)); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 195 | EXPECT_TRUE(ptr3.get() != nullptr); |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 196 | EXPECT_LE(8U * MB, ptr3_bytes_allocated); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 197 | EXPECT_LE(8U * MB, ptr3_usable_size); |
| 198 | EXPECT_LE(ptr3_usable_size, ptr3_bytes_allocated); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 199 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 200 | // Fails, requires a higher footprint limit. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 201 | mirror::Object* ptr4 = space->Alloc(self, 8 * MB, &dummy, nullptr); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 202 | EXPECT_TRUE(ptr4 == nullptr); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 203 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 204 | // Also fails, requires a higher allowed footprint. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 205 | mirror::Object* ptr5 = space->AllocWithGrowth(self, 8 * MB, &dummy, nullptr); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 206 | EXPECT_TRUE(ptr5 == nullptr); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 207 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 208 | // Release some memory. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 209 | size_t free3 = space->AllocationSize(ptr3.get(), nullptr); |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 210 | EXPECT_EQ(free3, ptr3_bytes_allocated); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 211 | EXPECT_EQ(free3, space->Free(self, ptr3.reset(nullptr))); |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 212 | EXPECT_LE(8U * MB, free3); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 213 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 214 | // Succeeds, now that memory has been freed. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 215 | size_t ptr6_bytes_allocated, ptr6_usable_size; |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 216 | SirtRef<mirror::Object> ptr6(self, AllocWithGrowth(space, self, 9 * MB, &ptr6_bytes_allocated, |
| 217 | &ptr6_usable_size)); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 218 | EXPECT_TRUE(ptr6.get() != nullptr); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 219 | EXPECT_LE(9U * MB, ptr6_bytes_allocated); |
| 220 | EXPECT_LE(9U * MB, ptr6_usable_size); |
| 221 | EXPECT_LE(ptr6_usable_size, ptr6_bytes_allocated); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 222 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 223 | // Final clean up. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 224 | size_t free1 = space->AllocationSize(ptr1.get(), nullptr); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 225 | space->Free(self, ptr1.reset(nullptr)); |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 226 | EXPECT_LE(1U * MB, free1); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 227 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 228 | // Make sure that the zygote space isn't directly at the start of the space. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 229 | EXPECT_TRUE(space->Alloc(self, 1U * MB, &dummy, nullptr) != nullptr); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 230 | |
| 231 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 232 | space::Space* old_space = space; |
| 233 | heap->RemoveSpace(old_space); |
Mathieu Chartier | 1b54f9c | 2014-04-30 16:45:02 -0700 | [diff] [blame] | 234 | heap->RevokeAllThreadLocalBuffers(); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 235 | space::ZygoteSpace* zygote_space = space->CreateZygoteSpace("alloc space", |
| 236 | heap->IsLowMemoryMode(), |
| 237 | &space); |
| 238 | delete old_space; |
| 239 | // Add the zygote space. |
Mathieu Chartier | 1b54f9c | 2014-04-30 16:45:02 -0700 | [diff] [blame] | 240 | AddSpace(zygote_space, false); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 241 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 242 | // Make space findable to the heap, will also delete space when runtime is cleaned up |
Mathieu Chartier | 1b54f9c | 2014-04-30 16:45:02 -0700 | [diff] [blame] | 243 | AddSpace(space, false); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 244 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 245 | // Succeeds, fits without adjusting the footprint limit. |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 246 | ptr1.reset(Alloc(space, self, 1 * MB, &ptr1_bytes_allocated, &ptr1_usable_size)); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 247 | EXPECT_TRUE(ptr1.get() != nullptr); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 248 | EXPECT_LE(1U * MB, ptr1_bytes_allocated); |
| 249 | EXPECT_LE(1U * MB, ptr1_usable_size); |
| 250 | EXPECT_LE(ptr1_usable_size, ptr1_bytes_allocated); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 251 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 252 | // Fails, requires a higher footprint limit. |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 253 | ptr2 = Alloc(space, self, 8 * MB, &dummy, nullptr); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 254 | EXPECT_TRUE(ptr2 == nullptr); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 255 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 256 | // Succeeds, adjusts the footprint. |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 257 | ptr3.reset(AllocWithGrowth(space, self, 2 * MB, &ptr3_bytes_allocated, &ptr3_usable_size)); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 258 | EXPECT_TRUE(ptr3.get() != nullptr); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 259 | EXPECT_LE(2U * MB, ptr3_bytes_allocated); |
| 260 | EXPECT_LE(2U * MB, ptr3_usable_size); |
| 261 | EXPECT_LE(ptr3_usable_size, ptr3_bytes_allocated); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 262 | space->Free(self, ptr3.reset(nullptr)); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 263 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 264 | // Final clean up. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 265 | free1 = space->AllocationSize(ptr1.get(), nullptr); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 266 | space->Free(self, ptr1.reset(nullptr)); |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 267 | EXPECT_LE(1U * MB, free1); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 270 | void SpaceTest::AllocAndFreeTestBody(CreateSpaceFn create_space) { |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 271 | size_t dummy = 0; |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 272 | MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, nullptr)); |
| 273 | ASSERT_TRUE(space != nullptr); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 274 | Thread* self = Thread::Current(); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 275 | ScopedObjectAccess soa(self); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 276 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 277 | // Make space findable to the heap, will also delete space when runtime is cleaned up |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 278 | AddSpace(space); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 279 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 280 | // Succeeds, fits without adjusting the footprint limit. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 281 | size_t ptr1_bytes_allocated, ptr1_usable_size; |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 282 | SirtRef<mirror::Object> ptr1(self, Alloc(space, self, 1 * MB, &ptr1_bytes_allocated, |
| 283 | &ptr1_usable_size)); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 284 | EXPECT_TRUE(ptr1.get() != nullptr); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 285 | EXPECT_LE(1U * MB, ptr1_bytes_allocated); |
| 286 | EXPECT_LE(1U * MB, ptr1_usable_size); |
| 287 | EXPECT_LE(ptr1_usable_size, ptr1_bytes_allocated); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 288 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 289 | // Fails, requires a higher footprint limit. |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 290 | mirror::Object* ptr2 = Alloc(space, self, 8 * MB, &dummy, nullptr); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 291 | EXPECT_TRUE(ptr2 == nullptr); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 292 | |
| 293 | // Succeeds, adjusts the footprint. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 294 | size_t ptr3_bytes_allocated, ptr3_usable_size; |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 295 | SirtRef<mirror::Object> ptr3(self, AllocWithGrowth(space, self, 8 * MB, &ptr3_bytes_allocated, |
| 296 | &ptr3_usable_size)); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 297 | EXPECT_TRUE(ptr3.get() != nullptr); |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 298 | EXPECT_LE(8U * MB, ptr3_bytes_allocated); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 299 | EXPECT_LE(8U * MB, ptr3_usable_size); |
| 300 | EXPECT_LE(ptr3_usable_size, ptr3_bytes_allocated); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 301 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 302 | // Fails, requires a higher footprint limit. |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 303 | mirror::Object* ptr4 = Alloc(space, self, 8 * MB, &dummy, nullptr); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 304 | EXPECT_TRUE(ptr4 == nullptr); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 305 | |
| 306 | // Also fails, requires a higher allowed footprint. |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 307 | mirror::Object* ptr5 = AllocWithGrowth(space, self, 8 * MB, &dummy, nullptr); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 308 | EXPECT_TRUE(ptr5 == nullptr); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 309 | |
| 310 | // Release some memory. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 311 | size_t free3 = space->AllocationSize(ptr3.get(), nullptr); |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 312 | EXPECT_EQ(free3, ptr3_bytes_allocated); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 313 | space->Free(self, ptr3.reset(nullptr)); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 314 | EXPECT_LE(8U * MB, free3); |
| 315 | |
| 316 | // Succeeds, now that memory has been freed. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 317 | size_t ptr6_bytes_allocated, ptr6_usable_size; |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 318 | SirtRef<mirror::Object> ptr6(self, AllocWithGrowth(space, self, 9 * MB, &ptr6_bytes_allocated, |
| 319 | &ptr6_usable_size)); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 320 | EXPECT_TRUE(ptr6.get() != nullptr); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 321 | EXPECT_LE(9U * MB, ptr6_bytes_allocated); |
| 322 | EXPECT_LE(9U * MB, ptr6_usable_size); |
| 323 | EXPECT_LE(ptr6_usable_size, ptr6_bytes_allocated); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 324 | |
| 325 | // Final clean up. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 326 | size_t free1 = space->AllocationSize(ptr1.get(), nullptr); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 327 | space->Free(self, ptr1.reset(nullptr)); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 328 | EXPECT_LE(1U * MB, free1); |
| 329 | } |
| 330 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 331 | void SpaceTest::AllocAndFreeListTestBody(CreateSpaceFn create_space) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 332 | MallocSpace* space(create_space("test", 4 * MB, 16 * MB, 16 * MB, nullptr)); |
| 333 | ASSERT_TRUE(space != nullptr); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 334 | |
| 335 | // Make space findable to the heap, will also delete space when runtime is cleaned up |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 336 | AddSpace(space); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 337 | Thread* self = Thread::Current(); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 338 | ScopedObjectAccess soa(self); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 339 | |
| 340 | // Succeeds, fits without adjusting the max allowed footprint. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 341 | mirror::Object* lots_of_objects[1024]; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 342 | for (size_t i = 0; i < arraysize(lots_of_objects); i++) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 343 | size_t allocation_size, usable_size; |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 344 | size_t size_of_zero_length_byte_array = SizeOfZeroLengthByteArray(); |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 345 | lots_of_objects[i] = Alloc(space, self, size_of_zero_length_byte_array, &allocation_size, |
| 346 | &usable_size); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 347 | EXPECT_TRUE(lots_of_objects[i] != nullptr); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 348 | SirtRef<mirror::Object> obj(self, lots_of_objects[i]); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 349 | lots_of_objects[i] = obj.get(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 350 | size_t computed_usable_size; |
| 351 | EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i], &computed_usable_size)); |
| 352 | EXPECT_EQ(usable_size, computed_usable_size); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 355 | // Release memory. |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 356 | space->FreeList(self, arraysize(lots_of_objects), lots_of_objects); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 357 | |
| 358 | // Succeeds, fits by adjusting the max allowed footprint. |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 359 | for (size_t i = 0; i < arraysize(lots_of_objects); i++) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 360 | size_t allocation_size, usable_size; |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 361 | lots_of_objects[i] = AllocWithGrowth(space, self, 1024, &allocation_size, &usable_size); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 362 | EXPECT_TRUE(lots_of_objects[i] != nullptr); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 363 | SirtRef<mirror::Object> obj(self, lots_of_objects[i]); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 364 | lots_of_objects[i] = obj.get(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 365 | size_t computed_usable_size; |
| 366 | EXPECT_EQ(allocation_size, space->AllocationSize(lots_of_objects[i], &computed_usable_size)); |
| 367 | EXPECT_EQ(usable_size, computed_usable_size); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 368 | } |
| 369 | |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 370 | // Release memory. |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 371 | space->FreeList(self, arraysize(lots_of_objects), lots_of_objects); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 374 | void SpaceTest::SizeFootPrintGrowthLimitAndTrimBody(MallocSpace* space, intptr_t object_size, |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 375 | int round, size_t growth_limit) { |
| 376 | if (((object_size > 0 && object_size >= static_cast<intptr_t>(growth_limit))) || |
| 377 | ((object_size < 0 && -object_size >= static_cast<intptr_t>(growth_limit)))) { |
| 378 | // No allocation can succeed |
| 379 | return; |
| 380 | } |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 381 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 382 | // The space's footprint equals amount of resources requested from system |
| 383 | size_t footprint = space->GetFootprint(); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 384 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 385 | // The space must at least have its book keeping allocated |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 386 | EXPECT_GT(footprint, 0u); |
| 387 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 388 | // But it shouldn't exceed the initial size |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 389 | EXPECT_LE(footprint, growth_limit); |
| 390 | |
| 391 | // space's size shouldn't exceed the initial size |
| 392 | EXPECT_LE(space->Size(), growth_limit); |
| 393 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 394 | // this invariant should always hold or else the space has grown to be larger than what the |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 395 | // space believes its size is (which will break invariants) |
| 396 | EXPECT_GE(space->Size(), footprint); |
| 397 | |
| 398 | // Fill the space with lots of small objects up to the growth limit |
| 399 | size_t max_objects = (growth_limit / (object_size > 0 ? object_size : 8)) + 1; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 400 | UniquePtr<mirror::Object*[]> lots_of_objects(new mirror::Object*[max_objects]); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 401 | size_t last_object = 0; // last object for which allocation succeeded |
| 402 | size_t amount_allocated = 0; // amount of space allocated |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 403 | Thread* self = Thread::Current(); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 404 | ScopedObjectAccess soa(self); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 405 | size_t rand_seed = 123456789; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 406 | for (size_t i = 0; i < max_objects; i++) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 407 | size_t alloc_fails = 0; // number of failed allocations |
| 408 | size_t max_fails = 30; // number of times we fail allocation before giving up |
| 409 | for (; alloc_fails < max_fails; alloc_fails++) { |
| 410 | size_t alloc_size; |
| 411 | if (object_size > 0) { |
| 412 | alloc_size = object_size; |
| 413 | } else { |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 414 | alloc_size = test_rand(&rand_seed) % static_cast<size_t>(-object_size); |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 415 | // Note the minimum size, which is the size of a zero-length byte array. |
| 416 | size_t size_of_zero_length_byte_array = SizeOfZeroLengthByteArray(); |
| 417 | if (alloc_size < size_of_zero_length_byte_array) { |
| 418 | alloc_size = size_of_zero_length_byte_array; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 419 | } |
| 420 | } |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 421 | SirtRef<mirror::Object> object(self, nullptr); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 422 | size_t bytes_allocated = 0; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 423 | if (round <= 1) { |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 424 | object.reset(Alloc(space, self, alloc_size, &bytes_allocated, nullptr)); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 425 | } else { |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 426 | object.reset(AllocWithGrowth(space, self, alloc_size, &bytes_allocated, nullptr)); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 427 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 428 | footprint = space->GetFootprint(); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 429 | EXPECT_GE(space->Size(), footprint); // invariant |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 430 | if (object.get() != nullptr) { // allocation succeeded |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 431 | lots_of_objects[i] = object.get(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 432 | size_t allocation_size = space->AllocationSize(object.get(), nullptr); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 433 | EXPECT_EQ(bytes_allocated, allocation_size); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 434 | if (object_size > 0) { |
| 435 | EXPECT_GE(allocation_size, static_cast<size_t>(object_size)); |
| 436 | } else { |
| 437 | EXPECT_GE(allocation_size, 8u); |
| 438 | } |
| 439 | amount_allocated += allocation_size; |
| 440 | break; |
| 441 | } |
| 442 | } |
| 443 | if (alloc_fails == max_fails) { |
| 444 | last_object = i; |
| 445 | break; |
| 446 | } |
| 447 | } |
| 448 | CHECK_NE(last_object, 0u); // we should have filled the space |
| 449 | EXPECT_GT(amount_allocated, 0u); |
| 450 | |
| 451 | // We shouldn't have gone past the growth_limit |
| 452 | EXPECT_LE(amount_allocated, growth_limit); |
| 453 | EXPECT_LE(footprint, growth_limit); |
| 454 | EXPECT_LE(space->Size(), growth_limit); |
| 455 | |
| 456 | // footprint and size should agree with amount allocated |
| 457 | EXPECT_GE(footprint, amount_allocated); |
| 458 | EXPECT_GE(space->Size(), amount_allocated); |
| 459 | |
| 460 | // Release storage in a semi-adhoc manner |
| 461 | size_t free_increment = 96; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 462 | while (true) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 463 | { |
| 464 | ScopedThreadStateChange tsc(self, kNative); |
| 465 | // Give the space a haircut. |
| 466 | space->Trim(); |
| 467 | } |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 468 | |
| 469 | // Bounds sanity |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 470 | footprint = space->GetFootprint(); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 471 | EXPECT_LE(amount_allocated, growth_limit); |
| 472 | EXPECT_GE(footprint, amount_allocated); |
| 473 | EXPECT_LE(footprint, growth_limit); |
| 474 | EXPECT_GE(space->Size(), amount_allocated); |
| 475 | EXPECT_LE(space->Size(), growth_limit); |
| 476 | |
| 477 | if (free_increment == 0) { |
| 478 | break; |
| 479 | } |
| 480 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 481 | // Free some objects |
| 482 | for (size_t i = 0; i < last_object; i += free_increment) { |
| 483 | mirror::Object* object = lots_of_objects.get()[i]; |
| 484 | if (object == nullptr) { |
| 485 | continue; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 486 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 487 | size_t allocation_size = space->AllocationSize(object, nullptr); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 488 | if (object_size > 0) { |
| 489 | EXPECT_GE(allocation_size, static_cast<size_t>(object_size)); |
| 490 | } else { |
| 491 | EXPECT_GE(allocation_size, 8u); |
| 492 | } |
| 493 | space->Free(self, object); |
| 494 | lots_of_objects.get()[i] = nullptr; |
| 495 | amount_allocated -= allocation_size; |
| 496 | footprint = space->GetFootprint(); |
| 497 | EXPECT_GE(space->Size(), footprint); // invariant |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 498 | } |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 499 | |
| 500 | free_increment >>= 1; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 501 | } |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 502 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 503 | // The space has become empty here before allocating a large object |
| 504 | // below. For RosAlloc, revoke thread-local runs, which are kept |
| 505 | // even when empty for a performance reason, so that they won't |
| 506 | // cause the following large object allocation to fail due to |
| 507 | // potential fragmentation. Note they are normally revoked at each |
| 508 | // GC (but no GC here.) |
| 509 | space->RevokeAllThreadLocalBuffers(); |
| 510 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 511 | // All memory was released, try a large allocation to check freed memory is being coalesced |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 512 | SirtRef<mirror::Object> large_object(self, nullptr); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 513 | size_t three_quarters_space = (growth_limit / 2) + (growth_limit / 4); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 514 | size_t bytes_allocated = 0; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 515 | if (round <= 1) { |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 516 | large_object.reset(Alloc(space, self, three_quarters_space, &bytes_allocated, nullptr)); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 517 | } else { |
Mathieu Chartier | 5647d18 | 2014-03-07 15:00:39 -0800 | [diff] [blame] | 518 | large_object.reset(AllocWithGrowth(space, self, three_quarters_space, &bytes_allocated, |
| 519 | nullptr)); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 520 | } |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 521 | EXPECT_TRUE(large_object.get() != nullptr); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 522 | |
| 523 | // Sanity check footprint |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 524 | footprint = space->GetFootprint(); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 525 | EXPECT_LE(footprint, growth_limit); |
| 526 | EXPECT_GE(space->Size(), footprint); |
| 527 | EXPECT_LE(space->Size(), growth_limit); |
| 528 | |
| 529 | // Clean up |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 530 | space->Free(self, large_object.reset(nullptr)); |
| 531 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 532 | // Sanity check footprint |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 533 | footprint = space->GetFootprint(); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 534 | EXPECT_LE(footprint, growth_limit); |
| 535 | EXPECT_GE(space->Size(), footprint); |
| 536 | EXPECT_LE(space->Size(), growth_limit); |
| 537 | } |
| 538 | |
Hiroshi Yamauchi | 3ddbd42 | 2013-12-06 17:43:36 -0800 | [diff] [blame] | 539 | void SpaceTest::SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size, CreateSpaceFn create_space) { |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 540 | if (object_size < SizeOfZeroLengthByteArray()) { |
| 541 | // Too small for the object layout/model. |
| 542 | return; |
| 543 | } |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 544 | size_t initial_size = 4 * MB; |
| 545 | size_t growth_limit = 8 * MB; |
| 546 | size_t capacity = 16 * MB; |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 547 | MallocSpace* space(create_space("test", initial_size, growth_limit, capacity, nullptr)); |
| 548 | ASSERT_TRUE(space != nullptr); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 549 | |
| 550 | // Basic sanity |
| 551 | EXPECT_EQ(space->Capacity(), growth_limit); |
| 552 | EXPECT_EQ(space->NonGrowthLimitCapacity(), capacity); |
| 553 | |
| 554 | // Make space findable to the heap, will also delete space when runtime is cleaned up |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 555 | AddSpace(space); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 556 | |
| 557 | // In this round we don't allocate with growth and therefore can't grow past the initial size. |
| 558 | // This effectively makes the growth_limit the initial_size, so assert this. |
| 559 | SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 1, initial_size); |
| 560 | SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 2, growth_limit); |
| 561 | // Remove growth limit |
| 562 | space->ClearGrowthLimit(); |
| 563 | EXPECT_EQ(space->Capacity(), capacity); |
| 564 | SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 3, capacity); |
| 565 | } |
| 566 | |
Andreas Gampe | 24651ec | 2014-02-27 13:26:16 -0800 | [diff] [blame] | 567 | #define TEST_SizeFootPrintGrowthLimitAndTrimStatic(name, spaceName, spaceFn, size) \ |
| 568 | TEST_F(spaceName##StaticTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name) { \ |
Andreas Gampe | a743351 | 2014-02-21 13:19:23 -0800 | [diff] [blame] | 569 | SizeFootPrintGrowthLimitAndTrimDriver(size, spaceFn); \ |
Andreas Gampe | 24651ec | 2014-02-27 13:26:16 -0800 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | #define TEST_SizeFootPrintGrowthLimitAndTrimRandom(name, spaceName, spaceFn, size) \ |
| 573 | TEST_F(spaceName##RandomTest, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name) { \ |
Andreas Gampe | a743351 | 2014-02-21 13:19:23 -0800 | [diff] [blame] | 574 | SizeFootPrintGrowthLimitAndTrimDriver(-size, spaceFn); \ |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 575 | } |
| 576 | |
Andreas Gampe | 24651ec | 2014-02-27 13:26:16 -0800 | [diff] [blame] | 577 | #define TEST_SPACE_CREATE_FN_BASE(spaceName, spaceFn) \ |
| 578 | class spaceName##BaseTest : public SpaceTest { \ |
Andreas Gampe | a743351 | 2014-02-21 13:19:23 -0800 | [diff] [blame] | 579 | }; \ |
| 580 | \ |
Andreas Gampe | 24651ec | 2014-02-27 13:26:16 -0800 | [diff] [blame] | 581 | TEST_F(spaceName##BaseTest, Init) { \ |
Andreas Gampe | a743351 | 2014-02-21 13:19:23 -0800 | [diff] [blame] | 582 | InitTestBody(spaceFn); \ |
| 583 | } \ |
Andreas Gampe | 24651ec | 2014-02-27 13:26:16 -0800 | [diff] [blame] | 584 | TEST_F(spaceName##BaseTest, ZygoteSpace) { \ |
Andreas Gampe | a743351 | 2014-02-21 13:19:23 -0800 | [diff] [blame] | 585 | ZygoteSpaceTestBody(spaceFn); \ |
| 586 | } \ |
Andreas Gampe | 24651ec | 2014-02-27 13:26:16 -0800 | [diff] [blame] | 587 | TEST_F(spaceName##BaseTest, AllocAndFree) { \ |
Andreas Gampe | a743351 | 2014-02-21 13:19:23 -0800 | [diff] [blame] | 588 | AllocAndFreeTestBody(spaceFn); \ |
| 589 | } \ |
Andreas Gampe | 24651ec | 2014-02-27 13:26:16 -0800 | [diff] [blame] | 590 | TEST_F(spaceName##BaseTest, AllocAndFreeList) { \ |
Andreas Gampe | a743351 | 2014-02-21 13:19:23 -0800 | [diff] [blame] | 591 | AllocAndFreeListTestBody(spaceFn); \ |
Andreas Gampe | 24651ec | 2014-02-27 13:26:16 -0800 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | #define TEST_SPACE_CREATE_FN_STATIC(spaceName, spaceFn) \ |
| 595 | class spaceName##StaticTest : public SpaceTest { \ |
| 596 | }; \ |
| 597 | \ |
| 598 | TEST_SizeFootPrintGrowthLimitAndTrimStatic(12B, spaceName, spaceFn, 12) \ |
| 599 | TEST_SizeFootPrintGrowthLimitAndTrimStatic(16B, spaceName, spaceFn, 16) \ |
| 600 | TEST_SizeFootPrintGrowthLimitAndTrimStatic(24B, spaceName, spaceFn, 24) \ |
| 601 | TEST_SizeFootPrintGrowthLimitAndTrimStatic(32B, spaceName, spaceFn, 32) \ |
| 602 | TEST_SizeFootPrintGrowthLimitAndTrimStatic(64B, spaceName, spaceFn, 64) \ |
| 603 | TEST_SizeFootPrintGrowthLimitAndTrimStatic(128B, spaceName, spaceFn, 128) \ |
| 604 | TEST_SizeFootPrintGrowthLimitAndTrimStatic(1KB, spaceName, spaceFn, 1 * KB) \ |
| 605 | TEST_SizeFootPrintGrowthLimitAndTrimStatic(4KB, spaceName, spaceFn, 4 * KB) \ |
| 606 | TEST_SizeFootPrintGrowthLimitAndTrimStatic(1MB, spaceName, spaceFn, 1 * MB) \ |
| 607 | TEST_SizeFootPrintGrowthLimitAndTrimStatic(4MB, spaceName, spaceFn, 4 * MB) \ |
| 608 | TEST_SizeFootPrintGrowthLimitAndTrimStatic(8MB, spaceName, spaceFn, 8 * MB) |
| 609 | |
| 610 | #define TEST_SPACE_CREATE_FN_RANDOM(spaceName, spaceFn) \ |
| 611 | class spaceName##RandomTest : public SpaceTest { \ |
| 612 | }; \ |
| 613 | \ |
| 614 | TEST_SizeFootPrintGrowthLimitAndTrimRandom(16B, spaceName, spaceFn, 16) \ |
| 615 | TEST_SizeFootPrintGrowthLimitAndTrimRandom(24B, spaceName, spaceFn, 24) \ |
| 616 | TEST_SizeFootPrintGrowthLimitAndTrimRandom(32B, spaceName, spaceFn, 32) \ |
| 617 | TEST_SizeFootPrintGrowthLimitAndTrimRandom(64B, spaceName, spaceFn, 64) \ |
| 618 | TEST_SizeFootPrintGrowthLimitAndTrimRandom(128B, spaceName, spaceFn, 128) \ |
| 619 | TEST_SizeFootPrintGrowthLimitAndTrimRandom(1KB, spaceName, spaceFn, 1 * KB) \ |
| 620 | TEST_SizeFootPrintGrowthLimitAndTrimRandom(4KB, spaceName, spaceFn, 4 * KB) \ |
| 621 | TEST_SizeFootPrintGrowthLimitAndTrimRandom(1MB, spaceName, spaceFn, 1 * MB) \ |
| 622 | TEST_SizeFootPrintGrowthLimitAndTrimRandom(4MB, spaceName, spaceFn, 4 * MB) \ |
| 623 | TEST_SizeFootPrintGrowthLimitAndTrimRandom(8MB, spaceName, spaceFn, 8 * MB) |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 624 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 625 | } // namespace space |
| 626 | } // namespace gc |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 627 | } // namespace art |
Andreas Gampe | a743351 | 2014-02-21 13:19:23 -0800 | [diff] [blame] | 628 | |
| 629 | #endif // ART_RUNTIME_GC_SPACE_SPACE_TEST_H_ |