| Index: base/memory/discardable_memory_allocator_unittest.cc
|
| diff --git a/base/memory/discardable_memory_allocator_unittest.cc b/base/memory/discardable_memory_allocator_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..557c50e3037f6b18ad9edb5c1329ebfab0ccdb10
|
| --- /dev/null
|
| +++ b/base/memory/discardable_memory_allocator_unittest.cc
|
| @@ -0,0 +1,189 @@
|
| +// Copyright (c) 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 "build/build_config.h"
|
| +
|
| +#include "base/logging.h"
|
| +#include "base/memory/discardable_memory.h"
|
| +#include "base/memory/discardable_memory_allocator.h"
|
| +#include "base/memory/scoped_ptr.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +#if defined(OS_ANDROID)
|
| +#include <sys/types.h>
|
| +#include <unistd.h>
|
| +
|
| +#include <fstream>
|
| +#include <iostream>
|
| +#include <string>
|
| +#include <vector>
|
| +
|
| +#include "base/strings/string_number_conversions.h"
|
| +#include "base/strings/string_split.h"
|
| +#include "base/strings/stringprintf.h"
|
| +#endif
|
| +
|
| +namespace base {
|
| +namespace {
|
| +
|
| +const char kAllocatorName[] = "allocator-for-testing";
|
| +const size_t kPageSize = 4096;
|
| +
|
| +scoped_ptr<DiscardableMemoryAllocator> CreateAllocator() {
|
| + return DiscardableMemoryAllocator::Create(kAllocatorName);
|
| +}
|
| +
|
| +void WriteToDiscardableMemory(DiscardableMemory* memory) {
|
| + DCHECK_EQ(0U, memory->size() % sizeof(int));
|
| + // Write to the first and the last pages only to avoid paging in up to 64
|
| + // MBytes.
|
| + static_cast<int*>(memory->Memory())[0] = 1;
|
| + static_cast<int*>(memory->Memory())[memory->size() / sizeof(int) - 1] = 1;
|
| +}
|
| +
|
| +TEST(DiscardableMemoryAllocatorTest, Basic) {
|
| + const scoped_ptr<DiscardableMemoryAllocator> allocator(CreateAllocator());
|
| + ASSERT_TRUE(allocator);
|
| + scoped_ptr<DiscardableMemory> memory(allocator->Allocate(128 * sizeof(int)));
|
| + ASSERT_TRUE(memory);
|
| + WriteToDiscardableMemory(memory.get());
|
| +}
|
| +
|
| +TEST(DiscardableMemoryAllocatorTest, LargeAllocation) {
|
| + const scoped_ptr<DiscardableMemoryAllocator> allocator(CreateAllocator());
|
| + ASSERT_TRUE(allocator);
|
| + scoped_ptr<DiscardableMemory> memory(allocator->Allocate(64 * 1024 * 1024));
|
| + ASSERT_TRUE(memory);
|
| + WriteToDiscardableMemory(memory.get());
|
| +}
|
| +
|
| +TEST(DiscardableMemoryAllocatorTest, ChunksArePageAligned) {
|
| + const scoped_ptr<DiscardableMemoryAllocator> allocator(CreateAllocator());
|
| + ASSERT_TRUE(allocator);
|
| + scoped_ptr<DiscardableMemory> memory(allocator->Allocate(kPageSize));
|
| + ASSERT_TRUE(memory);
|
| + EXPECT_EQ(0U, reinterpret_cast<uint64_t>(memory->Memory()) % kPageSize);
|
| + WriteToDiscardableMemory(memory.get());
|
| +}
|
| +
|
| +TEST(DiscardableMemoryAllocatorTest, SizeAlignmentIsImplementationDetail) {
|
| + const scoped_ptr<DiscardableMemoryAllocator> allocator(CreateAllocator());
|
| + ASSERT_TRUE(allocator);
|
| + const size_t requested_size = kPageSize + 1; // Rounded up to 2 * kPageSize.
|
| + scoped_ptr<DiscardableMemory> memory(allocator->Allocate(requested_size));
|
| + ASSERT_TRUE(memory);
|
| + EXPECT_EQ(requested_size, memory->size());
|
| +}
|
| +
|
| +TEST(DiscardableMemoryAllocatorTest, AllocateFreeAllocate) {
|
| + const scoped_ptr<DiscardableMemoryAllocator> allocator(CreateAllocator());
|
| + ASSERT_TRUE(allocator);
|
| + 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();
|
| + memory = allocator->Allocate(kPageSize);
|
| + ASSERT_TRUE(memory);
|
| + // The previously freed chunk should be reused.
|
| + EXPECT_EQ(address, memory->Memory());
|
| + WriteToDiscardableMemory(memory.get());
|
| +}
|
| +
|
| +TEST(DiscardableMemoryAllocatorTest, UseMultipleAshmemRegions) {
|
| + const scoped_ptr<DiscardableMemoryAllocator> allocator(CreateAllocator());
|
| + ASSERT_TRUE(allocator);
|
| + const size_t kAshmemRegionSize = 32 * 1024 * 1024;
|
| + // Leave one page untouched at the end of the ashmem region.
|
| + scoped_ptr<DiscardableMemory> memory1(
|
| + allocator->Allocate(kAshmemRegionSize - kPageSize));
|
| + ASSERT_TRUE(memory1);
|
| + WriteToDiscardableMemory(memory1.get());
|
| +
|
| + scoped_ptr<DiscardableMemory> memory2(
|
| + allocator->Allocate(kAshmemRegionSize));
|
| + ASSERT_TRUE(memory2);
|
| + WriteToDiscardableMemory(memory2.get());
|
| + // 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());
|
| + EXPECT_EQ(memory3->Memory(),
|
| + static_cast<char*>(memory1->Memory()) + memory1->size());
|
| +}
|
| +
|
| +TEST(DiscardableMemoryAllocatorTest, ThreadSafeAllocator) {
|
| + const scoped_ptr<DiscardableMemoryAllocator> allocator(
|
| + DiscardableMemoryAllocator::CreateThreadSafeInstance(kAllocatorName));
|
| + ASSERT_TRUE(allocator);
|
| + scoped_ptr<DiscardableMemory> memory(allocator->Allocate(kPageSize));
|
| + ASSERT_TRUE(memory);
|
| + WriteToDiscardableMemory(memory.get());
|
| + memory.reset(allocator->Allocate(kPageSize).release());
|
| + ASSERT_TRUE(memory);
|
| + WriteToDiscardableMemory(memory.get());
|
| +}
|
| +
|
| +#if defined(OS_ANDROID)
|
| +
|
| +// Returns -1 if an error happened during parsing.
|
| +int GetAllocatorDirtyMemoryInKBytes() {
|
| + const std::string smaps_path = StringPrintf("/proc/%d/smaps", getpid());
|
| + std::ifstream stream(smaps_path.c_str());
|
| + if (!stream.good())
|
| + return -1;
|
| + std::vector<std::string> lines;
|
| + for (std::string line; std::getline(stream, line); lines.push_back(line));
|
| + int dirty_kbytes = 0;
|
| + for (std::vector<std::string>::const_iterator it = lines.begin();
|
| + it != lines.end(); ++it) {
|
| + if (it->find(kAllocatorName) == std::string::npos)
|
| + continue;
|
| + const std::string& private_dirty_line = it[7];
|
| + if (private_dirty_line.find("Private_Dirty") == std::string::npos)
|
| + return -1;
|
| + std::vector<std::string> tokens;
|
| + SplitString(private_dirty_line, ' ', &tokens);
|
| + if (tokens.size() < 3U)
|
| + return -1;
|
| + int size;
|
| + if (!base::StringToInt(tokens[tokens.size() - 2], &size))
|
| + return -1;
|
| + dirty_kbytes += size;
|
| + it += 6;
|
| + }
|
| + return dirty_kbytes;
|
| +}
|
| +
|
| +TEST(DiscardableMemoryAllocatorTest, PagesAreCommittedLazily) {
|
| + const scoped_ptr<DiscardableMemoryAllocator> allocator(CreateAllocator());
|
| + ASSERT_TRUE(allocator);
|
| + scoped_ptr<DiscardableMemory> memory(allocator->Allocate(2 * 4096));
|
| + ASSERT_TRUE(memory);
|
| + EXPECT_EQ(0, GetAllocatorDirtyMemoryInKBytes());
|
| + static_cast<char*>(memory->Memory())[0] = 'a';
|
| + EXPECT_EQ(4, GetAllocatorDirtyMemoryInKBytes());
|
| + // Write to the second page.
|
| + static_cast<char*>(memory->Memory())[kPageSize] = 'b';
|
| + EXPECT_EQ(8, GetAllocatorDirtyMemoryInKBytes());
|
| +}
|
| +
|
| +TEST(DiscardableMemoryAllocatorTest, FreeChunksAreNotCommitted) {
|
| + const scoped_ptr<DiscardableMemoryAllocator> allocator(CreateAllocator());
|
| + ASSERT_TRUE(allocator);
|
| + EXPECT_EQ(0, GetAllocatorDirtyMemoryInKBytes());
|
| + scoped_ptr<DiscardableMemory> memory(allocator->Allocate(kPageSize));
|
| + ASSERT_TRUE(memory);
|
| + WriteToDiscardableMemory(memory.get());
|
| + EXPECT_EQ(4, GetAllocatorDirtyMemoryInKBytes());
|
| + memory.reset();
|
| + EXPECT_EQ(0, GetAllocatorDirtyMemoryInKBytes());
|
| +}
|
| +
|
| +#endif // OS_ANDROID
|
| +
|
| +} // namespace
|
| +} // namespace base
|
|
|