Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/memory/discardable_memory_allocator_android.h" | |
| 6 | |
| 7 #include <sys/types.h> | |
| 8 #include <unistd.h> | |
| 9 | |
| 10 #include "base/memory/discardable_memory.h" | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/strings/string_number_conversions.h" | |
| 13 #include "base/strings/string_split.h" | |
| 14 #include "base/strings/stringprintf.h" | |
| 15 #include "build/build_config.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 | |
| 18 namespace base { | |
| 19 namespace internal { | |
| 20 | |
| 21 const char kAllocatorName[] = "allocator-for-testing"; | |
| 22 | |
| 23 const size_t kPageSize = 4096; | |
| 24 const size_t kMinAshmemRegionSize = | |
| 25 DiscardableMemoryAllocator::kMinAshmemRegionSize; | |
| 26 | |
| 27 class DiscardableMemoryAllocatorTest : public testing::Test { | |
| 28 protected: | |
| 29 DiscardableMemoryAllocatorTest() : allocator_(kAllocatorName) {} | |
| 30 | |
| 31 DiscardableMemoryAllocator allocator_; | |
| 32 }; | |
| 33 | |
| 34 void WriteToDiscardableMemory(DiscardableMemory* memory, size_t size) { | |
| 35 // Write to the first and the last pages only to avoid paging in up to 64 | |
| 36 // MBytes. | |
| 37 static_cast<char*>(memory->Memory())[0] = 'a'; | |
| 38 static_cast<char*>(memory->Memory())[size - 1] = 'a'; | |
| 39 } | |
| 40 | |
| 41 TEST_F(DiscardableMemoryAllocatorTest, Basic) { | |
| 42 const size_t size = 128; | |
| 43 scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(size)); | |
| 44 ASSERT_TRUE(memory); | |
| 45 WriteToDiscardableMemory(memory.get(), size); | |
| 46 } | |
| 47 | |
| 48 TEST_F(DiscardableMemoryAllocatorTest, LargeAllocation) { | |
| 49 const size_t size = 64 * 1024 * 1024; | |
| 50 scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(size)); | |
| 51 ASSERT_TRUE(memory); | |
| 52 WriteToDiscardableMemory(memory.get(), size); | |
| 53 } | |
| 54 | |
| 55 TEST_F(DiscardableMemoryAllocatorTest, ChunksArePageAligned) { | |
| 56 scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(kPageSize)); | |
| 57 ASSERT_TRUE(memory); | |
| 58 EXPECT_EQ(0U, reinterpret_cast<uint64_t>(memory->Memory()) % kPageSize); | |
| 59 WriteToDiscardableMemory(memory.get(), kPageSize); | |
| 60 } | |
| 61 | |
| 62 TEST_F(DiscardableMemoryAllocatorTest, AllocateFreeAllocate) { | |
| 63 scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(kPageSize)); | |
| 64 ASSERT_TRUE(memory); | |
| 65 void* const address = memory->Memory(); | |
| 66 memory->Unlock(); // Tests that the recycled chunk is being locked correctly. | |
| 67 memory.reset(); | |
|
willchan no longer on Chromium
2013/11/28 06:16:37
It's not obvious to me why this shouldn't result i
Philippe
2013/11/28 16:42:53
You're right :) There was no reason not to delete
| |
| 68 memory = allocator_.Allocate(kPageSize); | |
| 69 ASSERT_TRUE(memory); | |
| 70 // The previously freed chunk should be reused. | |
| 71 EXPECT_EQ(address, memory->Memory()); | |
| 72 WriteToDiscardableMemory(memory.get(), kPageSize); | |
| 73 } | |
| 74 | |
| 75 TEST_F(DiscardableMemoryAllocatorTest, FreeingWholeAshmemRegionClosesAshmem) { | |
| 76 scoped_ptr<DiscardableMemory> memory( | |
| 77 allocator_.Allocate(kMinAshmemRegionSize)); | |
| 78 ASSERT_TRUE(memory); | |
| 79 const int kMagic = 0xdeadbeef; | |
| 80 *static_cast<int*>(memory->Memory()) = kMagic; | |
| 81 memory.reset(); | |
| 82 // The previous ashmem region should have been closed thus it should not be | |
| 83 // recycled. | |
| 84 memory = allocator_.Allocate(kPageSize); | |
| 85 ASSERT_TRUE(memory); | |
| 86 EXPECT_NE(kMagic, *static_cast<const int*>(memory->Memory())); | |
| 87 } | |
| 88 | |
| 89 TEST_F(DiscardableMemoryAllocatorTest, AllocateUsesBestFitAlgorithm) { | |
| 90 scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(3 * kPageSize)); | |
| 91 ASSERT_TRUE(memory1); | |
| 92 scoped_ptr<DiscardableMemory> memory2(allocator_.Allocate(2 * kPageSize)); | |
| 93 ASSERT_TRUE(memory2); | |
| 94 scoped_ptr<DiscardableMemory> memory3(allocator_.Allocate(1 * kPageSize)); | |
| 95 ASSERT_TRUE(memory3); | |
| 96 void* const address_3 = memory3->Memory(); | |
| 97 memory1.reset(); | |
| 98 // Don't free |memory2| to avoid merging the 3 blocks together. | |
| 99 memory3.reset(); | |
| 100 memory1 = allocator_.Allocate(1 * kPageSize); | |
| 101 ASSERT_TRUE(memory1); | |
| 102 // The chunk whose size is closest to the requested size should be recycled. | |
| 103 EXPECT_EQ(address_3, memory1->Memory()); | |
| 104 WriteToDiscardableMemory(memory1.get(), kPageSize); | |
| 105 } | |
| 106 | |
| 107 TEST_F(DiscardableMemoryAllocatorTest, MergeFreeChunks) { | |
| 108 scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(kPageSize)); | |
| 109 ASSERT_TRUE(memory1); | |
| 110 scoped_ptr<DiscardableMemory> memory2(allocator_.Allocate(kPageSize)); | |
| 111 ASSERT_TRUE(memory2); | |
| 112 scoped_ptr<DiscardableMemory> memory3(allocator_.Allocate(kPageSize)); | |
| 113 ASSERT_TRUE(memory3); | |
| 114 void* const memory1_address = memory1->Memory(); | |
| 115 memory1.reset(); | |
| 116 memory3.reset(); | |
| 117 // Freeing |memory2| (located between memory1 and memory3) should merge the | |
| 118 // three free blocks together. | |
| 119 memory2.reset(); | |
|
willchan no longer on Chromium
2013/11/28 06:16:37
Similar to above, it's not clear to me why this sh
Philippe
2013/11/28 16:42:53
Done.
| |
| 120 memory1.reset(allocator_.Allocate(3 * kPageSize).release()); | |
|
willchan no longer on Chromium
2013/11/28 06:16:37
Why isn't this just memory1 = allocator_.Allocate(
Philippe
2013/11/28 16:42:53
Yeah, no idea why I wrote this :)
| |
| 121 EXPECT_EQ(memory1_address, memory1->Memory()); | |
| 122 } | |
| 123 | |
| 124 TEST_F(DiscardableMemoryAllocatorTest, MergeFreeChunksAdvanced) { | |
| 125 const size_t kHalfRegionSize = kMinAshmemRegionSize / 2; | |
| 126 const size_t kQuarterRegionSize = kMinAshmemRegionSize / 4; | |
| 127 scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(kHalfRegionSize)); | |
| 128 ASSERT_TRUE(memory1); | |
| 129 scoped_ptr<DiscardableMemory> memory2(allocator_.Allocate(kHalfRegionSize)); | |
| 130 ASSERT_TRUE(memory2); | |
| 131 void* const memory1_address = memory1->Memory(); | |
| 132 memory1.reset(); | |
| 133 memory1 = allocator_.Allocate(kQuarterRegionSize); | |
| 134 memory2.reset(); | |
| 135 // At this point, the region should be in this state: | |
| 136 // 8 MBytes (used), 24 MBytes (free). | |
| 137 memory2 = allocator_.Allocate(kMinAshmemRegionSize - kQuarterRegionSize); | |
| 138 EXPECT_EQ( | |
| 139 static_cast<const char*>(memory2->Memory()), | |
| 140 static_cast<const char*>(memory1_address) + kQuarterRegionSize); | |
| 141 } | |
| 142 | |
| 143 TEST_F(DiscardableMemoryAllocatorTest, MergeFreeChunksAdvanced2) { | |
| 144 const size_t kHalfRegionSize = kMinAshmemRegionSize / 2; | |
| 145 const size_t kQuarterRegionSize = kMinAshmemRegionSize / 4; | |
| 146 scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(kHalfRegionSize)); | |
| 147 ASSERT_TRUE(memory1); | |
| 148 scoped_ptr<DiscardableMemory> memory2(allocator_.Allocate(kHalfRegionSize)); | |
| 149 ASSERT_TRUE(memory2); | |
| 150 void* const memory1_address = memory1->Memory(); | |
| 151 memory1.reset(); | |
| 152 memory1 = allocator_.Allocate(kQuarterRegionSize); | |
| 153 scoped_ptr<DiscardableMemory> memory3( | |
| 154 allocator_.Allocate(kQuarterRegionSize)); | |
| 155 // At this point, the region should be in this state: | |
| 156 // 8 MBytes (used), 8 MBytes (used), 16 MBytes (used). | |
| 157 memory3.reset(); | |
| 158 memory2.reset(); | |
| 159 // At this point, the region should be in this state: | |
| 160 // 8 MBytes (used), 24 MBytes (free). | |
| 161 memory2 = allocator_.Allocate(kMinAshmemRegionSize - kQuarterRegionSize); | |
| 162 EXPECT_EQ( | |
| 163 static_cast<const char*>(memory2->Memory()), | |
| 164 static_cast<const char*>(memory1_address) + kQuarterRegionSize); | |
| 165 } | |
|
willchan no longer on Chromium
2013/11/28 06:16:37
It'd be cool if you added a third one of these whe
Philippe
2013/11/28 16:42:53
Yeah, good idea. Done.
| |
| 166 | |
| 167 TEST_F(DiscardableMemoryAllocatorTest, | |
| 168 TooLargeFreeChunksDontCauseTooMuchFragmentationWhenRecycled) { | |
| 169 // Keep |memory_1| below allocated so that the ashmem region doesn't get | |
| 170 // closed when |memory_2| is deleted. | |
| 171 scoped_ptr<DiscardableMemory> memory_1(allocator_.Allocate(64 * 1024)); | |
| 172 ASSERT_TRUE(memory_1); | |
| 173 scoped_ptr<DiscardableMemory> memory_2(allocator_.Allocate(32 * 1024)); | |
| 174 ASSERT_TRUE(memory_2); | |
| 175 void* const address = memory_2->Memory(); | |
| 176 memory_2.reset(); | |
| 177 const size_t size = 16 * 1024; | |
| 178 memory_2 = allocator_.Allocate(size); | |
| 179 ASSERT_TRUE(memory_2); | |
| 180 EXPECT_EQ(address, memory_2->Memory()); | |
| 181 WriteToDiscardableMemory(memory_2.get(), size); | |
| 182 scoped_ptr<DiscardableMemory> memory_3(allocator_.Allocate(size)); | |
| 183 // The unused tail (16 KBytes large) of the previously freed chunk should be | |
| 184 // recycled. | |
| 185 EXPECT_EQ(static_cast<char*>(address) + size, memory_3->Memory()); | |
| 186 WriteToDiscardableMemory(memory_3.get(), size); | |
| 187 } | |
| 188 | |
| 189 TEST_F(DiscardableMemoryAllocatorTest, UseMultipleAshmemRegions) { | |
| 190 // Leave one page untouched at the end of the ashmem region. | |
| 191 const size_t size = kMinAshmemRegionSize - kPageSize; | |
| 192 scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(size)); | |
| 193 ASSERT_TRUE(memory1); | |
| 194 WriteToDiscardableMemory(memory1.get(), size); | |
| 195 | |
| 196 scoped_ptr<DiscardableMemory> memory2( | |
| 197 allocator_.Allocate(kMinAshmemRegionSize)); | |
| 198 ASSERT_TRUE(memory2); | |
| 199 WriteToDiscardableMemory(memory2.get(), kMinAshmemRegionSize); | |
| 200 // The last page of the first ashmem region should be used for this | |
| 201 // allocation. | |
| 202 scoped_ptr<DiscardableMemory> memory3(allocator_.Allocate(kPageSize)); | |
| 203 ASSERT_TRUE(memory3); | |
| 204 WriteToDiscardableMemory(memory3.get(), kPageSize); | |
| 205 EXPECT_EQ(memory3->Memory(), static_cast<char*>(memory1->Memory()) + size); | |
| 206 } | |
| 207 | |
| 208 } // namespace internal | |
| 209 } // namespace base | |
| OLD | NEW |