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