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