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