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