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