OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/trace_event/memory_allocator_dump.h" |
| 6 |
| 7 #include "base/trace_event/memory_dump_provider.h" |
| 8 #include "base/trace_event/process_memory_dump.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace base { |
| 12 namespace trace_event { |
| 13 |
| 14 namespace { |
| 15 class FakeMemoryAllocatorDumpProvider : public MemoryDumpProvider { |
| 16 public: |
| 17 FakeMemoryAllocatorDumpProvider() { |
| 18 DeclareAllocatorAttribute({"attr1", "count"}); |
| 19 DeclareAllocatorAttribute({"attr2", "bytes"}); |
| 20 } |
| 21 |
| 22 bool DumpInto(ProcessMemoryDump* pmd) override { |
| 23 MemoryAllocatorDump* mad_foo = pmd->CreateAllocatorDump("foo"); |
| 24 mad_foo->set_physical_size_in_bytes(4096); |
| 25 mad_foo->set_allocated_objects_count(42); |
| 26 mad_foo->set_allocated_objects_size_in_bytes(1000); |
| 27 mad_foo->SetExtraAttribute("attr1", 1234); |
| 28 mad_foo->SetExtraAttribute("attr2", 99); |
| 29 |
| 30 MemoryAllocatorDump* mad_bar = pmd->CreateAllocatorDump("foo/bar", mad_foo); |
| 31 mad_bar->set_physical_size_in_bytes(1); |
| 32 mad_bar->set_allocated_objects_count(2); |
| 33 mad_bar->set_allocated_objects_size_in_bytes(3); |
| 34 |
| 35 pmd->CreateAllocatorDump("baz"); |
| 36 // Leave the rest of |baz| deliberately uninitialized, to check that |
| 37 // CreateAllocatorDump returns a properly zero-initialized object. |
| 38 |
| 39 return true; |
| 40 } |
| 41 |
| 42 const char* GetFriendlyName() const override { return "mock_allocator"; } |
| 43 }; |
| 44 } // namespace |
| 45 |
| 46 TEST(MemoryAllocatorDumpTest, DumpIntoProcessMemoryDump) { |
| 47 FakeMemoryAllocatorDumpProvider fmadp; |
| 48 ProcessMemoryDump pmd; |
| 49 |
| 50 fmadp.DumpInto(&pmd); |
| 51 |
| 52 ASSERT_EQ(3u, pmd.allocator_dumps().size()); |
| 53 |
| 54 const MemoryAllocatorDump* mad_foo = pmd.GetAllocatorDump("foo"); |
| 55 ASSERT_NE(nullptr, mad_foo); |
| 56 EXPECT_EQ("foo", mad_foo->name()); |
| 57 ASSERT_EQ(nullptr, mad_foo->parent()); |
| 58 EXPECT_EQ(4096u, mad_foo->physical_size_in_bytes()); |
| 59 EXPECT_EQ(42u, mad_foo->allocated_objects_count()); |
| 60 EXPECT_EQ(1000u, mad_foo->allocated_objects_size_in_bytes()); |
| 61 |
| 62 // Check the extra attributes of |mad_foo|. |
| 63 EXPECT_EQ(1234, mad_foo->GetExtraIntegerAttribute("attr1")); |
| 64 EXPECT_EQ(99, mad_foo->GetExtraIntegerAttribute("attr2")); |
| 65 |
| 66 const MemoryAllocatorDump* mad_bar = pmd.GetAllocatorDump("foo/bar"); |
| 67 ASSERT_NE(nullptr, mad_bar); |
| 68 EXPECT_EQ("foo/bar", mad_bar->name()); |
| 69 ASSERT_EQ(mad_foo, mad_bar->parent()); |
| 70 EXPECT_EQ(1u, mad_bar->physical_size_in_bytes()); |
| 71 EXPECT_EQ(2u, mad_bar->allocated_objects_count()); |
| 72 EXPECT_EQ(3u, mad_bar->allocated_objects_size_in_bytes()); |
| 73 |
| 74 const MemoryAllocatorDump* mad_baz = pmd.GetAllocatorDump("baz"); |
| 75 ASSERT_NE(nullptr, mad_baz); |
| 76 EXPECT_EQ("baz", mad_baz->name()); |
| 77 ASSERT_EQ(nullptr, mad_baz->parent()); |
| 78 EXPECT_EQ(0u, mad_baz->physical_size_in_bytes()); |
| 79 EXPECT_EQ(0u, mad_baz->allocated_objects_count()); |
| 80 EXPECT_EQ(0u, mad_baz->allocated_objects_size_in_bytes()); |
| 81 } |
| 82 |
| 83 // DEATH tests are not supported in Android / iOS. |
| 84 #if !defined(NDEBUG) && !defined(OS_ANDROID) && !defined(OS_IOS) |
| 85 TEST(MemoryAllocatorDumpTest, ForbidDuplicatesDeathTest) { |
| 86 FakeMemoryAllocatorDumpProvider fmadp; |
| 87 ProcessMemoryDump pmd; |
| 88 pmd.CreateAllocatorDump("dump_1"); |
| 89 pmd.CreateAllocatorDump("dump_2"); |
| 90 ASSERT_DEATH(pmd.CreateAllocatorDump("dump_1"), ""); |
| 91 } |
| 92 #endif |
| 93 |
| 94 } // namespace trace_event |
| 95 } // namespace base |
OLD | NEW |