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