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