| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/metrics/persistent_memory_allocator.h" | 5 #include "base/metrics/persistent_memory_allocator.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 }; | 93 }; |
| 94 | 94 |
| 95 TEST_F(PersistentMemoryAllocatorTest, AllocateAndIterate) { | 95 TEST_F(PersistentMemoryAllocatorTest, AllocateAndIterate) { |
| 96 allocator_->CreateTrackingHistograms(allocator_->Name()); | 96 allocator_->CreateTrackingHistograms(allocator_->Name()); |
| 97 | 97 |
| 98 std::string base_name(TEST_NAME); | 98 std::string base_name(TEST_NAME); |
| 99 EXPECT_EQ(TEST_ID, allocator_->Id()); | 99 EXPECT_EQ(TEST_ID, allocator_->Id()); |
| 100 EXPECT_TRUE(allocator_->used_histogram_); | 100 EXPECT_TRUE(allocator_->used_histogram_); |
| 101 EXPECT_EQ("UMA.PersistentAllocator." + base_name + ".UsedPct", | 101 EXPECT_EQ("UMA.PersistentAllocator." + base_name + ".UsedPct", |
| 102 allocator_->used_histogram_->histogram_name()); | 102 allocator_->used_histogram_->histogram_name()); |
| 103 EXPECT_EQ(PersistentMemoryAllocator::MEMORY_INITIALIZED, |
| 104 allocator_->GetMemoryState()); |
| 103 | 105 |
| 104 // Get base memory info for later comparison. | 106 // Get base memory info for later comparison. |
| 105 PersistentMemoryAllocator::MemoryInfo meminfo0; | 107 PersistentMemoryAllocator::MemoryInfo meminfo0; |
| 106 allocator_->GetMemoryInfo(&meminfo0); | 108 allocator_->GetMemoryInfo(&meminfo0); |
| 107 EXPECT_EQ(TEST_MEMORY_SIZE, meminfo0.total); | 109 EXPECT_EQ(TEST_MEMORY_SIZE, meminfo0.total); |
| 108 EXPECT_GT(meminfo0.total, meminfo0.free); | 110 EXPECT_GT(meminfo0.total, meminfo0.free); |
| 109 | 111 |
| 110 // Validate allocation of test object and make sure it can be referenced | 112 // Validate allocation of test object and make sure it can be referenced |
| 111 // and all metadata looks correct. | 113 // and all metadata looks correct. |
| 112 TestObject1* obj1 = allocator_->New<TestObject1>(); | 114 TestObject1* obj1 = allocator_->New<TestObject1>(); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 | 249 |
| 248 // Ensure that GetNextOfObject works. | 250 // Ensure that GetNextOfObject works. |
| 249 PersistentMemoryAllocator::Iterator iter1d(allocator_.get()); | 251 PersistentMemoryAllocator::Iterator iter1d(allocator_.get()); |
| 250 EXPECT_EQ(obj2, iter1d.GetNextOfObject<TestObject2>()); | 252 EXPECT_EQ(obj2, iter1d.GetNextOfObject<TestObject2>()); |
| 251 EXPECT_EQ(nullptr, iter1d.GetNextOfObject<TestObject2>()); | 253 EXPECT_EQ(nullptr, iter1d.GetNextOfObject<TestObject2>()); |
| 252 | 254 |
| 253 // Ensure that deleting an object works. | 255 // Ensure that deleting an object works. |
| 254 allocator_->Delete(obj2); | 256 allocator_->Delete(obj2); |
| 255 PersistentMemoryAllocator::Iterator iter1z(allocator_.get()); | 257 PersistentMemoryAllocator::Iterator iter1z(allocator_.get()); |
| 256 EXPECT_EQ(nullptr, iter1z.GetNextOfObject<TestObject2>()); | 258 EXPECT_EQ(nullptr, iter1z.GetNextOfObject<TestObject2>()); |
| 259 |
| 260 // Ensure that the memory state can be set. |
| 261 allocator_->SetMemoryState(PersistentMemoryAllocator::MEMORY_DELETED); |
| 262 EXPECT_EQ(PersistentMemoryAllocator::MEMORY_DELETED, |
| 263 allocator_->GetMemoryState()); |
| 257 } | 264 } |
| 258 | 265 |
| 259 TEST_F(PersistentMemoryAllocatorTest, PageTest) { | 266 TEST_F(PersistentMemoryAllocatorTest, PageTest) { |
| 260 // This allocation will go into the first memory page. | 267 // This allocation will go into the first memory page. |
| 261 Reference block1 = allocator_->Allocate(TEST_MEMORY_PAGE / 2, 1); | 268 Reference block1 = allocator_->Allocate(TEST_MEMORY_PAGE / 2, 1); |
| 262 EXPECT_LT(0U, block1); | 269 EXPECT_LT(0U, block1); |
| 263 EXPECT_GT(TEST_MEMORY_PAGE, block1); | 270 EXPECT_GT(TEST_MEMORY_PAGE, block1); |
| 264 | 271 |
| 265 // This allocation won't fit in same page as previous block. | 272 // This allocation won't fit in same page as previous block. |
| 266 Reference block2 = | 273 Reference block2 = |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 684 ASSERT_TRUE(writer.IsValid()); | 691 ASSERT_TRUE(writer.IsValid()); |
| 685 writer.Write(0, (const char*)local.data(), local.used()); | 692 writer.Write(0, (const char*)local.data(), local.used()); |
| 686 } | 693 } |
| 687 | 694 |
| 688 std::unique_ptr<MemoryMappedFile> mmfile(new MemoryMappedFile()); | 695 std::unique_ptr<MemoryMappedFile> mmfile(new MemoryMappedFile()); |
| 689 mmfile->Initialize(file_path); | 696 mmfile->Initialize(file_path); |
| 690 EXPECT_TRUE(mmfile->IsValid()); | 697 EXPECT_TRUE(mmfile->IsValid()); |
| 691 const size_t mmlength = mmfile->length(); | 698 const size_t mmlength = mmfile->length(); |
| 692 EXPECT_GE(meminfo1.total, mmlength); | 699 EXPECT_GE(meminfo1.total, mmlength); |
| 693 | 700 |
| 694 FilePersistentMemoryAllocator file(std::move(mmfile), 0, 0, "", true); | 701 FilePersistentMemoryAllocator file(std::move(mmfile), 0, 0, "", false); |
| 695 EXPECT_TRUE(file.IsReadonly()); | 702 EXPECT_FALSE(file.IsReadonly()); |
| 696 EXPECT_EQ(TEST_ID, file.Id()); | 703 EXPECT_EQ(TEST_ID, file.Id()); |
| 697 EXPECT_FALSE(file.IsFull()); | 704 EXPECT_FALSE(file.IsFull()); |
| 698 EXPECT_FALSE(file.IsCorrupt()); | 705 EXPECT_FALSE(file.IsCorrupt()); |
| 699 | 706 |
| 700 PersistentMemoryAllocator::Iterator iter(&file); | 707 PersistentMemoryAllocator::Iterator iter(&file); |
| 701 uint32_t type; | 708 uint32_t type; |
| 702 EXPECT_EQ(r123, iter.GetNext(&type)); | 709 EXPECT_EQ(r123, iter.GetNext(&type)); |
| 703 EXPECT_EQ(r789, iter.GetNext(&type)); | 710 EXPECT_EQ(r789, iter.GetNext(&type)); |
| 704 EXPECT_EQ(0U, iter.GetNext(&type)); | 711 EXPECT_EQ(0U, iter.GetNext(&type)); |
| 705 | 712 |
| 706 EXPECT_EQ(123U, file.GetType(r123)); | 713 EXPECT_EQ(123U, file.GetType(r123)); |
| 707 EXPECT_EQ(654U, file.GetType(r456)); | 714 EXPECT_EQ(654U, file.GetType(r456)); |
| 708 EXPECT_EQ(789U, file.GetType(r789)); | 715 EXPECT_EQ(789U, file.GetType(r789)); |
| 709 | 716 |
| 710 PersistentMemoryAllocator::MemoryInfo meminfo2; | 717 PersistentMemoryAllocator::MemoryInfo meminfo2; |
| 711 file.GetMemoryInfo(&meminfo2); | 718 file.GetMemoryInfo(&meminfo2); |
| 712 EXPECT_GE(meminfo1.total, meminfo2.total); | 719 EXPECT_GE(meminfo1.total, meminfo2.total); |
| 713 EXPECT_GE(meminfo1.free, meminfo2.free); | 720 EXPECT_GE(meminfo1.free, meminfo2.free); |
| 714 EXPECT_EQ(mmlength, meminfo2.total); | 721 EXPECT_EQ(mmlength, meminfo2.total); |
| 715 EXPECT_EQ(0U, meminfo2.free); | 722 EXPECT_EQ(0U, meminfo2.free); |
| 723 |
| 724 // There's no way of knowing if Flush actually does anything but at least |
| 725 // verify that it runs without CHECK violations. |
| 726 file.Flush(false); |
| 727 file.Flush(true); |
| 716 } | 728 } |
| 717 | 729 |
| 718 TEST(FilePersistentMemoryAllocatorTest, ExtendTest) { | 730 TEST(FilePersistentMemoryAllocatorTest, ExtendTest) { |
| 719 ScopedTempDir temp_dir; | 731 ScopedTempDir temp_dir; |
| 720 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 732 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 721 FilePath file_path = temp_dir.GetPath().AppendASCII("extend_test"); | 733 FilePath file_path = temp_dir.GetPath().AppendASCII("extend_test"); |
| 722 MemoryMappedFile::Region region = {0, 16 << 10}; // 16KiB maximum size. | 734 MemoryMappedFile::Region region = {0, 16 << 10}; // 16KiB maximum size. |
| 723 | 735 |
| 724 // Start with a small but valid file of persistent data. | 736 // Start with a small but valid file of persistent data. |
| 725 ASSERT_FALSE(PathExists(file_path)); | 737 ASSERT_FALSE(PathExists(file_path)); |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 857 // For filesize >= minsize, the file must be acceptable. This | 869 // For filesize >= minsize, the file must be acceptable. This |
| 858 // else clause (file-not-acceptable) should be reached only if | 870 // else clause (file-not-acceptable) should be reached only if |
| 859 // filesize < minsize. | 871 // filesize < minsize. |
| 860 EXPECT_GT(minsize, filesize); | 872 EXPECT_GT(minsize, filesize); |
| 861 } | 873 } |
| 862 } | 874 } |
| 863 } | 875 } |
| 864 #endif // !defined(OS_NACL) | 876 #endif // !defined(OS_NACL) |
| 865 | 877 |
| 866 } // namespace base | 878 } // namespace base |
| OLD | NEW |