Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3780)

Unified Diff: base/memory/discardable_memory_allocator_android_unittest.cc

Issue 25293002: Add DiscardableMemoryAllocator to work around FD limit issue. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address the rest of William's comments (made in patch set 34) Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/memory/discardable_memory_allocator_android_unittest.cc
diff --git a/base/memory/discardable_memory_allocator_android_unittest.cc b/base/memory/discardable_memory_allocator_android_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..153a2edbeb547bdfc131680dd34de1f017fe5d34
--- /dev/null
+++ b/base/memory/discardable_memory_allocator_android_unittest.cc
@@ -0,0 +1,209 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/memory/discardable_memory_allocator_android.h"
+
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "base/memory/discardable_memory.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/string_split.h"
+#include "base/strings/stringprintf.h"
+#include "build/build_config.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace base {
+namespace internal {
+
+const char kAllocatorName[] = "allocator-for-testing";
+
+const size_t kPageSize = 4096;
+const size_t kMinAshmemRegionSize =
+ DiscardableMemoryAllocator::kMinAshmemRegionSize;
+
+class DiscardableMemoryAllocatorTest : public testing::Test {
+ protected:
+ DiscardableMemoryAllocatorTest() : allocator_(kAllocatorName) {}
+
+ DiscardableMemoryAllocator allocator_;
+};
+
+void WriteToDiscardableMemory(DiscardableMemory* memory, size_t size) {
+ // Write to the first and the last pages only to avoid paging in up to 64
+ // MBytes.
+ static_cast<char*>(memory->Memory())[0] = 'a';
+ static_cast<char*>(memory->Memory())[size - 1] = 'a';
+}
+
+TEST_F(DiscardableMemoryAllocatorTest, Basic) {
+ const size_t size = 128;
+ scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(size));
+ ASSERT_TRUE(memory);
+ WriteToDiscardableMemory(memory.get(), size);
+}
+
+TEST_F(DiscardableMemoryAllocatorTest, LargeAllocation) {
+ const size_t size = 64 * 1024 * 1024;
+ scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(size));
+ ASSERT_TRUE(memory);
+ WriteToDiscardableMemory(memory.get(), size);
+}
+
+TEST_F(DiscardableMemoryAllocatorTest, ChunksArePageAligned) {
+ scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(kPageSize));
+ ASSERT_TRUE(memory);
+ EXPECT_EQ(0U, reinterpret_cast<uint64_t>(memory->Memory()) % kPageSize);
+ WriteToDiscardableMemory(memory.get(), kPageSize);
+}
+
+TEST_F(DiscardableMemoryAllocatorTest, AllocateFreeAllocate) {
+ scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(kPageSize));
+ ASSERT_TRUE(memory);
+ void* const address = memory->Memory();
+ memory->Unlock(); // Tests that the recycled chunk is being locked correctly.
+ 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
+ memory = allocator_.Allocate(kPageSize);
+ ASSERT_TRUE(memory);
+ // The previously freed chunk should be reused.
+ EXPECT_EQ(address, memory->Memory());
+ WriteToDiscardableMemory(memory.get(), kPageSize);
+}
+
+TEST_F(DiscardableMemoryAllocatorTest, FreeingWholeAshmemRegionClosesAshmem) {
+ scoped_ptr<DiscardableMemory> memory(
+ allocator_.Allocate(kMinAshmemRegionSize));
+ ASSERT_TRUE(memory);
+ const int kMagic = 0xdeadbeef;
+ *static_cast<int*>(memory->Memory()) = kMagic;
+ memory.reset();
+ // The previous ashmem region should have been closed thus it should not be
+ // recycled.
+ memory = allocator_.Allocate(kPageSize);
+ ASSERT_TRUE(memory);
+ EXPECT_NE(kMagic, *static_cast<const int*>(memory->Memory()));
+}
+
+TEST_F(DiscardableMemoryAllocatorTest, AllocateUsesBestFitAlgorithm) {
+ scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(3 * kPageSize));
+ ASSERT_TRUE(memory1);
+ scoped_ptr<DiscardableMemory> memory2(allocator_.Allocate(2 * kPageSize));
+ ASSERT_TRUE(memory2);
+ scoped_ptr<DiscardableMemory> memory3(allocator_.Allocate(1 * kPageSize));
+ ASSERT_TRUE(memory3);
+ void* const address_3 = memory3->Memory();
+ memory1.reset();
+ // Don't free |memory2| to avoid merging the 3 blocks together.
+ memory3.reset();
+ memory1 = allocator_.Allocate(1 * kPageSize);
+ ASSERT_TRUE(memory1);
+ // The chunk whose size is closest to the requested size should be recycled.
+ EXPECT_EQ(address_3, memory1->Memory());
+ WriteToDiscardableMemory(memory1.get(), kPageSize);
+}
+
+TEST_F(DiscardableMemoryAllocatorTest, MergeFreeChunks) {
+ scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(kPageSize));
+ ASSERT_TRUE(memory1);
+ scoped_ptr<DiscardableMemory> memory2(allocator_.Allocate(kPageSize));
+ ASSERT_TRUE(memory2);
+ scoped_ptr<DiscardableMemory> memory3(allocator_.Allocate(kPageSize));
+ ASSERT_TRUE(memory3);
+ void* const memory1_address = memory1->Memory();
+ memory1.reset();
+ memory3.reset();
+ // Freeing |memory2| (located between memory1 and memory3) should merge the
+ // three free blocks together.
+ 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.
+ 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 :)
+ EXPECT_EQ(memory1_address, memory1->Memory());
+}
+
+TEST_F(DiscardableMemoryAllocatorTest, MergeFreeChunksAdvanced) {
+ const size_t kHalfRegionSize = kMinAshmemRegionSize / 2;
+ const size_t kQuarterRegionSize = kMinAshmemRegionSize / 4;
+ scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(kHalfRegionSize));
+ ASSERT_TRUE(memory1);
+ scoped_ptr<DiscardableMemory> memory2(allocator_.Allocate(kHalfRegionSize));
+ ASSERT_TRUE(memory2);
+ void* const memory1_address = memory1->Memory();
+ memory1.reset();
+ memory1 = allocator_.Allocate(kQuarterRegionSize);
+ memory2.reset();
+ // At this point, the region should be in this state:
+ // 8 MBytes (used), 24 MBytes (free).
+ memory2 = allocator_.Allocate(kMinAshmemRegionSize - kQuarterRegionSize);
+ EXPECT_EQ(
+ static_cast<const char*>(memory2->Memory()),
+ static_cast<const char*>(memory1_address) + kQuarterRegionSize);
+}
+
+TEST_F(DiscardableMemoryAllocatorTest, MergeFreeChunksAdvanced2) {
+ const size_t kHalfRegionSize = kMinAshmemRegionSize / 2;
+ const size_t kQuarterRegionSize = kMinAshmemRegionSize / 4;
+ scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(kHalfRegionSize));
+ ASSERT_TRUE(memory1);
+ scoped_ptr<DiscardableMemory> memory2(allocator_.Allocate(kHalfRegionSize));
+ ASSERT_TRUE(memory2);
+ void* const memory1_address = memory1->Memory();
+ memory1.reset();
+ memory1 = allocator_.Allocate(kQuarterRegionSize);
+ scoped_ptr<DiscardableMemory> memory3(
+ allocator_.Allocate(kQuarterRegionSize));
+ // At this point, the region should be in this state:
+ // 8 MBytes (used), 8 MBytes (used), 16 MBytes (used).
+ memory3.reset();
+ memory2.reset();
+ // At this point, the region should be in this state:
+ // 8 MBytes (used), 24 MBytes (free).
+ memory2 = allocator_.Allocate(kMinAshmemRegionSize - kQuarterRegionSize);
+ EXPECT_EQ(
+ static_cast<const char*>(memory2->Memory()),
+ static_cast<const char*>(memory1_address) + kQuarterRegionSize);
+}
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.
+
+TEST_F(DiscardableMemoryAllocatorTest,
+ TooLargeFreeChunksDontCauseTooMuchFragmentationWhenRecycled) {
+ // Keep |memory_1| below allocated so that the ashmem region doesn't get
+ // closed when |memory_2| is deleted.
+ scoped_ptr<DiscardableMemory> memory_1(allocator_.Allocate(64 * 1024));
+ ASSERT_TRUE(memory_1);
+ scoped_ptr<DiscardableMemory> memory_2(allocator_.Allocate(32 * 1024));
+ ASSERT_TRUE(memory_2);
+ void* const address = memory_2->Memory();
+ memory_2.reset();
+ const size_t size = 16 * 1024;
+ memory_2 = allocator_.Allocate(size);
+ ASSERT_TRUE(memory_2);
+ EXPECT_EQ(address, memory_2->Memory());
+ WriteToDiscardableMemory(memory_2.get(), size);
+ scoped_ptr<DiscardableMemory> memory_3(allocator_.Allocate(size));
+ // The unused tail (16 KBytes large) of the previously freed chunk should be
+ // recycled.
+ EXPECT_EQ(static_cast<char*>(address) + size, memory_3->Memory());
+ WriteToDiscardableMemory(memory_3.get(), size);
+}
+
+TEST_F(DiscardableMemoryAllocatorTest, UseMultipleAshmemRegions) {
+ // Leave one page untouched at the end of the ashmem region.
+ const size_t size = kMinAshmemRegionSize - kPageSize;
+ scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(size));
+ ASSERT_TRUE(memory1);
+ WriteToDiscardableMemory(memory1.get(), size);
+
+ scoped_ptr<DiscardableMemory> memory2(
+ allocator_.Allocate(kMinAshmemRegionSize));
+ ASSERT_TRUE(memory2);
+ WriteToDiscardableMemory(memory2.get(), kMinAshmemRegionSize);
+ // The last page of the first ashmem region should be used for this
+ // allocation.
+ scoped_ptr<DiscardableMemory> memory3(allocator_.Allocate(kPageSize));
+ ASSERT_TRUE(memory3);
+ WriteToDiscardableMemory(memory3.get(), kPageSize);
+ EXPECT_EQ(memory3->Memory(), static_cast<char*>(memory1->Memory()) + size);
+}
+
+} // namespace internal
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698