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