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 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "space.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 18 | |
Brian Carlstrom | 9b7f2c2 | 2011-09-27 14:35:04 -0700 | [diff] [blame] | 19 | #include "common_test.h" |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 20 | #include "dlmalloc.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" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 23 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 24 | #include <stdint.h> |
| 25 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 26 | namespace art { |
| 27 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 28 | class SpaceTest : public CommonTest { |
| 29 | public: |
| 30 | void SizeFootPrintGrowthLimitAndTrimBody(AllocSpace* space, intptr_t object_size, |
| 31 | int round, size_t growth_limit); |
| 32 | void SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size); |
| 33 | }; |
Brian Carlstrom | 9b7f2c2 | 2011-09-27 14:35:04 -0700 | [diff] [blame] | 34 | |
| 35 | TEST_F(SpaceTest, Init) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 36 | { |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 37 | // Init < max == growth |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 38 | UniquePtr<Space> space(Space::CreateAllocSpace("test", 16 * MB, 32 * MB, 32 * MB, NULL)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 39 | EXPECT_TRUE(space.get() != NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 40 | } |
| 41 | { |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 42 | // Init == max == growth |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 43 | UniquePtr<Space> space(Space::CreateAllocSpace("test", 16 * MB, 16 * MB, 16 * MB, NULL)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 44 | EXPECT_TRUE(space.get() != NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 45 | } |
| 46 | { |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 47 | // Init > max == growth |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 48 | UniquePtr<Space> space(Space::CreateAllocSpace("test", 32 * MB, 16 * MB, 16 * MB, NULL)); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 49 | EXPECT_TRUE(space.get() == NULL); |
| 50 | } |
| 51 | { |
| 52 | // Growth == init < max |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 53 | UniquePtr<Space> space(Space::CreateAllocSpace("test", 16 * MB, 16 * MB, 32 * MB, NULL)); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 54 | EXPECT_TRUE(space.get() != NULL); |
| 55 | } |
| 56 | { |
| 57 | // Growth < init < max |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 58 | UniquePtr<Space> space(Space::CreateAllocSpace("test", 16 * MB, 8 * MB, 32 * MB, NULL)); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 59 | EXPECT_TRUE(space.get() == NULL); |
| 60 | } |
| 61 | { |
| 62 | // Init < growth < max |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 63 | UniquePtr<Space> space(Space::CreateAllocSpace("test", 8 * MB, 16 * MB, 32 * MB, NULL)); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 64 | EXPECT_TRUE(space.get() != NULL); |
| 65 | } |
| 66 | { |
| 67 | // Init < max < growth |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 68 | UniquePtr<Space> space(Space::CreateAllocSpace("test", 8 * MB, 32 * MB, 16 * MB, NULL)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 69 | EXPECT_TRUE(space.get() == NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame^] | 73 | // TODO: This test is not very good, we should improve it. |
| 74 | // The test should do more allocations before the creation of the ZygoteSpace, and then do |
| 75 | // allocations after the ZygoteSpace is created. The test should also do some GCs to ensure that |
| 76 | // the GC works with the ZygoteSpace. |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 77 | TEST_F(SpaceTest, ZygoteSpace) { |
| 78 | AllocSpace* space(Space::CreateAllocSpace("test", 4 * MB, 16 * MB, 16 * MB, NULL)); |
| 79 | ASSERT_TRUE(space != NULL); |
| 80 | |
| 81 | // Make space findable to the heap, will also delete space when runtime is cleaned up |
| 82 | Runtime::Current()->GetHeap()->AddSpace(space); |
| 83 | |
| 84 | // Succeeds, fits without adjusting the footprint limit. |
| 85 | Object* ptr1 = space->AllocWithoutGrowth(1 * MB); |
| 86 | EXPECT_TRUE(ptr1 != NULL); |
| 87 | |
| 88 | // Fails, requires a higher footprint limit. |
| 89 | Object* ptr2 = space->AllocWithoutGrowth(8 * MB); |
| 90 | EXPECT_TRUE(ptr2 == NULL); |
| 91 | |
| 92 | // Succeeds, adjusts the footprint. |
| 93 | Object* ptr3 = space->AllocWithGrowth(8 * MB); |
| 94 | EXPECT_TRUE(ptr3 != NULL); |
| 95 | |
| 96 | // Fails, requires a higher footprint limit. |
| 97 | Object* ptr4 = space->AllocWithoutGrowth(8 * MB); |
| 98 | EXPECT_TRUE(ptr4 == NULL); |
| 99 | |
| 100 | // Also fails, requires a higher allowed footprint. |
| 101 | Object* ptr5 = space->AllocWithGrowth(8 * MB); |
| 102 | EXPECT_TRUE(ptr5 == NULL); |
| 103 | |
| 104 | // Release some memory. |
| 105 | size_t free3 = space->AllocationSize(ptr3); |
| 106 | space->Free(ptr3); |
| 107 | EXPECT_LE(8U * MB, free3); |
| 108 | |
| 109 | // Succeeds, now that memory has been freed. |
| 110 | void* ptr6 = space->AllocWithGrowth(9 * MB); |
| 111 | EXPECT_TRUE(ptr6 != NULL); |
| 112 | |
| 113 | // Final clean up. |
| 114 | size_t free1 = space->AllocationSize(ptr1); |
| 115 | space->Free(ptr1); |
| 116 | EXPECT_LE(1U * MB, free1); |
| 117 | |
| 118 | // Make sure that the zygote space isn't directly at the start of the space. |
| 119 | space->AllocWithoutGrowth(1U * MB); |
| 120 | space = space->CreateZygoteSpace(); |
| 121 | |
| 122 | // Make space findable to the heap, will also delete space when runtime is cleaned up |
| 123 | Runtime::Current()->GetHeap()->AddSpace(space); |
| 124 | |
| 125 | // Succeeds, fits without adjusting the footprint limit. |
| 126 | ptr1 = space->AllocWithoutGrowth(1 * MB); |
| 127 | EXPECT_TRUE(ptr1 != NULL); |
| 128 | |
| 129 | // Fails, requires a higher footprint limit. |
| 130 | ptr2 = space->AllocWithoutGrowth(8 * MB); |
| 131 | EXPECT_TRUE(ptr2 == NULL); |
| 132 | |
| 133 | // Succeeds, adjusts the footprint. |
| 134 | ptr3 = space->AllocWithGrowth(2 * MB); |
| 135 | EXPECT_TRUE(ptr3 != NULL); |
| 136 | space->Free(ptr3); |
| 137 | |
| 138 | // Final clean up. |
| 139 | free1 = space->AllocationSize(ptr1); |
| 140 | space->Free(ptr1); |
| 141 | EXPECT_LE(1U * MB, free1); |
| 142 | } |
| 143 | |
Brian Carlstrom | 9b7f2c2 | 2011-09-27 14:35:04 -0700 | [diff] [blame] | 144 | TEST_F(SpaceTest, AllocAndFree) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 145 | AllocSpace* space(Space::CreateAllocSpace("test", 4 * MB, 16 * MB, 16 * MB, NULL)); |
| 146 | ASSERT_TRUE(space != NULL); |
| 147 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 148 | // Make space findable to the heap, will also delete space when runtime is cleaned up |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 149 | Runtime::Current()->GetHeap()->AddSpace(space); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 150 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 151 | // Succeeds, fits without adjusting the footprint limit. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 152 | Object* ptr1 = space->AllocWithoutGrowth(1 * MB); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 153 | EXPECT_TRUE(ptr1 != NULL); |
| 154 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 155 | // Fails, requires a higher footprint limit. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 156 | Object* ptr2 = space->AllocWithoutGrowth(8 * MB); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 157 | EXPECT_TRUE(ptr2 == NULL); |
| 158 | |
| 159 | // Succeeds, adjusts the footprint. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 160 | Object* ptr3 = space->AllocWithGrowth(8 * MB); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 161 | EXPECT_TRUE(ptr3 != NULL); |
| 162 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 163 | // Fails, requires a higher footprint limit. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 164 | Object* ptr4 = space->AllocWithoutGrowth(8 * MB); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 165 | EXPECT_TRUE(ptr4 == NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 166 | |
| 167 | // Also fails, requires a higher allowed footprint. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 168 | Object* ptr5 = space->AllocWithGrowth(8 * MB); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 169 | EXPECT_TRUE(ptr5 == NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 170 | |
| 171 | // Release some memory. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 172 | size_t free3 = space->AllocationSize(ptr3); |
| 173 | space->Free(ptr3); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 174 | EXPECT_LE(8U * MB, free3); |
| 175 | |
| 176 | // Succeeds, now that memory has been freed. |
| 177 | void* ptr6 = space->AllocWithGrowth(9 * MB); |
| 178 | EXPECT_TRUE(ptr6 != NULL); |
| 179 | |
| 180 | // Final clean up. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 181 | size_t free1 = space->AllocationSize(ptr1); |
| 182 | space->Free(ptr1); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 183 | EXPECT_LE(1U * MB, free1); |
| 184 | } |
| 185 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 186 | TEST_F(SpaceTest, AllocAndFreeList) { |
| 187 | AllocSpace* space(Space::CreateAllocSpace("test", 4 * MB, 16 * MB, 16 * MB, NULL)); |
| 188 | ASSERT_TRUE(space != NULL); |
| 189 | |
| 190 | // Make space findable to the heap, will also delete space when runtime is cleaned up |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 191 | Runtime::Current()->GetHeap()->AddSpace(space); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 192 | |
| 193 | // Succeeds, fits without adjusting the max allowed footprint. |
| 194 | Object* lots_of_objects[1024]; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 195 | for (size_t i = 0; i < arraysize(lots_of_objects); i++) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 196 | lots_of_objects[i] = space->AllocWithoutGrowth(16); |
| 197 | EXPECT_TRUE(lots_of_objects[i] != NULL); |
| 198 | } |
| 199 | |
| 200 | // Release memory and check pointers are NULL |
| 201 | space->FreeList(arraysize(lots_of_objects), lots_of_objects); |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 202 | for (size_t i = 0; i < arraysize(lots_of_objects); i++) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 203 | EXPECT_TRUE(lots_of_objects[i] == NULL); |
| 204 | } |
| 205 | |
| 206 | // Succeeds, fits by adjusting the max allowed footprint. |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 207 | for (size_t i = 0; i < arraysize(lots_of_objects); i++) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 208 | lots_of_objects[i] = space->AllocWithGrowth(1024); |
| 209 | EXPECT_TRUE(lots_of_objects[i] != NULL); |
| 210 | } |
| 211 | |
| 212 | // Release memory and check pointers are NULL |
| 213 | space->FreeList(arraysize(lots_of_objects), lots_of_objects); |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 214 | for (size_t i = 0; i < arraysize(lots_of_objects); i++) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 215 | EXPECT_TRUE(lots_of_objects[i] == NULL); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | static size_t test_rand() { |
| 220 | // TODO: replace this with something random yet deterministic |
| 221 | return rand(); |
| 222 | } |
| 223 | |
| 224 | void SpaceTest::SizeFootPrintGrowthLimitAndTrimBody(AllocSpace* space, intptr_t object_size, |
| 225 | int round, size_t growth_limit) { |
| 226 | if (((object_size > 0 && object_size >= static_cast<intptr_t>(growth_limit))) || |
| 227 | ((object_size < 0 && -object_size >= static_cast<intptr_t>(growth_limit)))) { |
| 228 | // No allocation can succeed |
| 229 | return; |
| 230 | } |
| 231 | // Mspace for raw dlmalloc operations |
| 232 | void* mspace = space->GetMspace(); |
| 233 | |
| 234 | // mspace's footprint equals amount of resources requested from system |
| 235 | size_t footprint = mspace_footprint(mspace); |
| 236 | |
| 237 | // mspace must at least have its book keeping allocated |
| 238 | EXPECT_GT(footprint, 0u); |
| 239 | |
| 240 | // mspace but it shouldn't exceed the initial size |
| 241 | EXPECT_LE(footprint, growth_limit); |
| 242 | |
| 243 | // space's size shouldn't exceed the initial size |
| 244 | EXPECT_LE(space->Size(), growth_limit); |
| 245 | |
| 246 | // this invariant should always hold or else the mspace has grown to be larger than what the |
| 247 | // space believes its size is (which will break invariants) |
| 248 | EXPECT_GE(space->Size(), footprint); |
| 249 | |
| 250 | // Fill the space with lots of small objects up to the growth limit |
| 251 | size_t max_objects = (growth_limit / (object_size > 0 ? object_size : 8)) + 1; |
Elliott Hughes | f34f174 | 2012-03-16 18:56:00 -0700 | [diff] [blame] | 252 | UniquePtr<Object*[]> lots_of_objects(new Object*[max_objects]); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 253 | size_t last_object = 0; // last object for which allocation succeeded |
| 254 | size_t amount_allocated = 0; // amount of space allocated |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 255 | for (size_t i = 0; i < max_objects; i++) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 256 | size_t alloc_fails = 0; // number of failed allocations |
| 257 | size_t max_fails = 30; // number of times we fail allocation before giving up |
| 258 | for (; alloc_fails < max_fails; alloc_fails++) { |
| 259 | size_t alloc_size; |
| 260 | if (object_size > 0) { |
| 261 | alloc_size = object_size; |
| 262 | } else { |
| 263 | alloc_size = test_rand() % static_cast<size_t>(-object_size); |
| 264 | if (alloc_size < 8) { |
| 265 | alloc_size = 8; |
| 266 | } |
| 267 | } |
| 268 | Object* object; |
| 269 | if (round <= 1) { |
| 270 | object = space->AllocWithoutGrowth(alloc_size); |
| 271 | } else { |
| 272 | object = space->AllocWithGrowth(alloc_size); |
| 273 | } |
| 274 | footprint = mspace_footprint(mspace); |
| 275 | EXPECT_GE(space->Size(), footprint); // invariant |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 276 | if (object != NULL) { // allocation succeeded |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 277 | lots_of_objects.get()[i] = object; |
| 278 | size_t allocation_size = space->AllocationSize(object); |
| 279 | if (object_size > 0) { |
| 280 | EXPECT_GE(allocation_size, static_cast<size_t>(object_size)); |
| 281 | } else { |
| 282 | EXPECT_GE(allocation_size, 8u); |
| 283 | } |
| 284 | amount_allocated += allocation_size; |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | if (alloc_fails == max_fails) { |
| 289 | last_object = i; |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | CHECK_NE(last_object, 0u); // we should have filled the space |
| 294 | EXPECT_GT(amount_allocated, 0u); |
| 295 | |
| 296 | // We shouldn't have gone past the growth_limit |
| 297 | EXPECT_LE(amount_allocated, growth_limit); |
| 298 | EXPECT_LE(footprint, growth_limit); |
| 299 | EXPECT_LE(space->Size(), growth_limit); |
| 300 | |
| 301 | // footprint and size should agree with amount allocated |
| 302 | EXPECT_GE(footprint, amount_allocated); |
| 303 | EXPECT_GE(space->Size(), amount_allocated); |
| 304 | |
| 305 | // Release storage in a semi-adhoc manner |
| 306 | size_t free_increment = 96; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 307 | while (true) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 308 | // Give the space a haircut |
| 309 | space->Trim(); |
| 310 | |
| 311 | // Bounds sanity |
| 312 | footprint = mspace_footprint(mspace); |
| 313 | EXPECT_LE(amount_allocated, growth_limit); |
| 314 | EXPECT_GE(footprint, amount_allocated); |
| 315 | EXPECT_LE(footprint, growth_limit); |
| 316 | EXPECT_GE(space->Size(), amount_allocated); |
| 317 | EXPECT_LE(space->Size(), growth_limit); |
| 318 | |
| 319 | if (free_increment == 0) { |
| 320 | break; |
| 321 | } |
| 322 | |
| 323 | // Free some objects |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 324 | for (size_t i = 0; i < last_object; i += free_increment) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 325 | Object* object = lots_of_objects.get()[i]; |
| 326 | if (object == NULL) { |
| 327 | continue; |
| 328 | } |
| 329 | size_t allocation_size = space->AllocationSize(object); |
| 330 | if (object_size > 0) { |
| 331 | EXPECT_GE(allocation_size, static_cast<size_t>(object_size)); |
| 332 | } else { |
| 333 | EXPECT_GE(allocation_size, 8u); |
| 334 | } |
| 335 | space->Free(object); |
| 336 | lots_of_objects.get()[i] = NULL; |
| 337 | amount_allocated -= allocation_size; |
| 338 | footprint = mspace_footprint(mspace); |
| 339 | EXPECT_GE(space->Size(), footprint); // invariant |
| 340 | } |
| 341 | |
| 342 | free_increment >>= 1; |
| 343 | } |
| 344 | |
| 345 | // All memory was released, try a large allocation to check freed memory is being coalesced |
| 346 | Object* large_object; |
| 347 | size_t three_quarters_space = (growth_limit / 2) + (growth_limit / 4); |
| 348 | if (round <= 1) { |
| 349 | large_object = space->AllocWithoutGrowth(three_quarters_space); |
| 350 | } else { |
| 351 | large_object = space->AllocWithGrowth(three_quarters_space); |
| 352 | } |
| 353 | EXPECT_TRUE(large_object != NULL); |
| 354 | |
| 355 | // Sanity check footprint |
| 356 | footprint = mspace_footprint(mspace); |
| 357 | EXPECT_LE(footprint, growth_limit); |
| 358 | EXPECT_GE(space->Size(), footprint); |
| 359 | EXPECT_LE(space->Size(), growth_limit); |
| 360 | |
| 361 | // Clean up |
| 362 | space->Free(large_object); |
| 363 | |
| 364 | // Sanity check footprint |
| 365 | footprint = mspace_footprint(mspace); |
| 366 | EXPECT_LE(footprint, growth_limit); |
| 367 | EXPECT_GE(space->Size(), footprint); |
| 368 | EXPECT_LE(space->Size(), growth_limit); |
| 369 | } |
| 370 | |
| 371 | void SpaceTest::SizeFootPrintGrowthLimitAndTrimDriver(size_t object_size) { |
| 372 | size_t initial_size = 4 * MB; |
| 373 | size_t growth_limit = 8 * MB; |
| 374 | size_t capacity = 16 * MB; |
| 375 | AllocSpace* space(Space::CreateAllocSpace("test", initial_size, growth_limit, capacity, NULL)); |
| 376 | ASSERT_TRUE(space != NULL); |
| 377 | |
| 378 | // Basic sanity |
| 379 | EXPECT_EQ(space->Capacity(), growth_limit); |
| 380 | EXPECT_EQ(space->NonGrowthLimitCapacity(), capacity); |
| 381 | |
| 382 | // Make space findable to the heap, will also delete space when runtime is cleaned up |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 383 | Runtime::Current()->GetHeap()->AddSpace(space); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 384 | |
| 385 | // In this round we don't allocate with growth and therefore can't grow past the initial size. |
| 386 | // This effectively makes the growth_limit the initial_size, so assert this. |
| 387 | SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 1, initial_size); |
| 388 | SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 2, growth_limit); |
| 389 | // Remove growth limit |
| 390 | space->ClearGrowthLimit(); |
| 391 | EXPECT_EQ(space->Capacity(), capacity); |
| 392 | SizeFootPrintGrowthLimitAndTrimBody(space, object_size, 3, capacity); |
| 393 | } |
| 394 | |
| 395 | #define TEST_SizeFootPrintGrowthLimitAndTrim(name, size) \ |
| 396 | TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_##name) { \ |
| 397 | SizeFootPrintGrowthLimitAndTrimDriver(size); \ |
| 398 | } \ |
| 399 | TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_RandomAllocationsWithMax_##name) { \ |
| 400 | SizeFootPrintGrowthLimitAndTrimDriver(-size); \ |
| 401 | } |
| 402 | |
| 403 | // Each size test is its own test so that we get a fresh heap each time |
| 404 | TEST_F(SpaceTest, SizeFootPrintGrowthLimitAndTrim_AllocationsOf_8B) { |
| 405 | SizeFootPrintGrowthLimitAndTrimDriver(8); |
| 406 | } |
| 407 | TEST_SizeFootPrintGrowthLimitAndTrim(16B, 16) |
| 408 | TEST_SizeFootPrintGrowthLimitAndTrim(24B, 24) |
| 409 | TEST_SizeFootPrintGrowthLimitAndTrim(32B, 32) |
| 410 | TEST_SizeFootPrintGrowthLimitAndTrim(64B, 64) |
| 411 | TEST_SizeFootPrintGrowthLimitAndTrim(128B, 128) |
| 412 | TEST_SizeFootPrintGrowthLimitAndTrim(1KB, 1 * KB) |
| 413 | TEST_SizeFootPrintGrowthLimitAndTrim(4KB, 4 * KB) |
| 414 | TEST_SizeFootPrintGrowthLimitAndTrim(1MB, 1 * MB) |
| 415 | TEST_SizeFootPrintGrowthLimitAndTrim(4MB, 4 * MB) |
| 416 | TEST_SizeFootPrintGrowthLimitAndTrim(8MB, 8 * MB) |
| 417 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 418 | } // namespace art |