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/trace_event/memory_allocator_dump.h" | 5 #include "base/trace_event/memory_allocator_dump.h" |
6 | 6 |
7 #include "base/trace_event/memory_dump_provider.h" | 7 #include "base/trace_event/memory_dump_provider.h" |
8 #include "base/trace_event/memory_dump_session_state.h" | 8 #include "base/trace_event/memory_dump_session_state.h" |
9 #include "base/trace_event/process_memory_dump.h" | 9 #include "base/trace_event/process_memory_dump.h" |
| 10 #include "base/trace_event/trace_event_argument.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 namespace base { | 13 namespace base { |
13 namespace trace_event { | 14 namespace trace_event { |
14 | 15 |
15 namespace { | 16 namespace { |
| 17 |
16 class FakeMemoryAllocatorDumpProvider : public MemoryDumpProvider { | 18 class FakeMemoryAllocatorDumpProvider : public MemoryDumpProvider { |
17 public: | 19 public: |
18 FakeMemoryAllocatorDumpProvider() { | 20 FakeMemoryAllocatorDumpProvider() { |
19 DeclareAllocatorAttribute({"attr1", "count"}); | 21 DeclareAllocatorAttribute("foobar_allocator", "attr1", "count"); |
20 DeclareAllocatorAttribute({"attr2", "bytes"}); | 22 DeclareAllocatorAttribute("foobar_allocator", "attr2", "bytes"); |
21 } | 23 } |
22 | 24 |
23 bool DumpInto(ProcessMemoryDump* pmd) override { | 25 bool DumpInto(ProcessMemoryDump* pmd) override { |
24 MemoryAllocatorDump* mad_foo = pmd->CreateAllocatorDump("foo"); | 26 MemoryAllocatorDump* root_heap = pmd->CreateAllocatorDump( |
25 mad_foo->set_physical_size_in_bytes(4096); | 27 "foobar_allocator", MemoryAllocatorDump::kRootHeap); |
26 mad_foo->set_allocated_objects_count(42); | 28 root_heap->set_physical_size_in_bytes(4096); |
27 mad_foo->set_allocated_objects_size_in_bytes(1000); | 29 root_heap->set_allocated_objects_count(42); |
28 mad_foo->SetExtraAttribute("attr1", 1234); | 30 root_heap->set_allocated_objects_size_in_bytes(1000); |
29 mad_foo->SetExtraAttribute("attr2", 99); | 31 root_heap->SetAttribute("attr1", 1234); |
| 32 root_heap->SetAttribute("attr2", 99); |
30 | 33 |
31 MemoryAllocatorDump* mad_bar = pmd->CreateAllocatorDump("foo/bar", mad_foo); | 34 MemoryAllocatorDump* sub_heap = |
32 mad_bar->set_physical_size_in_bytes(1); | 35 pmd->CreateAllocatorDump("foobar_allocator", "sub_heap"); |
33 mad_bar->set_allocated_objects_count(2); | 36 sub_heap->set_physical_size_in_bytes(1); |
34 mad_bar->set_allocated_objects_size_in_bytes(3); | 37 sub_heap->set_allocated_objects_count(2); |
| 38 sub_heap->set_allocated_objects_size_in_bytes(3); |
35 | 39 |
36 pmd->CreateAllocatorDump("baz"); | 40 pmd->CreateAllocatorDump("foobar_allocator", "sub_heap/empty"); |
37 // Leave the rest of |baz| deliberately uninitialized, to check that | 41 // Leave the rest of sub heap deliberately uninitialized, to check that |
38 // CreateAllocatorDump returns a properly zero-initialized object. | 42 // CreateAllocatorDump returns a properly zero-initialized object. |
39 | 43 |
40 return true; | 44 return true; |
41 } | 45 } |
42 | 46 |
43 const char* GetFriendlyName() const override { return "mock_allocator"; } | 47 const char* GetFriendlyName() const override { return "FooBar Allocator"; } |
44 }; | 48 }; |
45 } // namespace | 49 } // namespace |
46 | 50 |
47 TEST(MemoryAllocatorDumpTest, DumpIntoProcessMemoryDump) { | 51 TEST(MemoryAllocatorDumpTest, DumpIntoProcessMemoryDump) { |
48 FakeMemoryAllocatorDumpProvider fmadp; | 52 FakeMemoryAllocatorDumpProvider fmadp; |
49 ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState())); | 53 ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState())); |
| 54 pmd.session_state()->allocators_attributes_type_info.Update( |
| 55 fmadp.allocator_attributes_type_info()); |
50 | 56 |
51 fmadp.DumpInto(&pmd); | 57 fmadp.DumpInto(&pmd); |
52 | 58 |
53 ASSERT_EQ(3u, pmd.allocator_dumps().size()); | 59 ASSERT_EQ(3u, pmd.allocator_dumps().size()); |
54 | 60 |
55 const MemoryAllocatorDump* mad_foo = pmd.GetAllocatorDump("foo"); | 61 const MemoryAllocatorDump* root_heap = |
56 ASSERT_NE(nullptr, mad_foo); | 62 pmd.GetAllocatorDump("foobar_allocator", MemoryAllocatorDump::kRootHeap); |
57 EXPECT_EQ("foo", mad_foo->name()); | 63 ASSERT_NE(nullptr, root_heap); |
58 ASSERT_EQ(nullptr, mad_foo->parent()); | 64 EXPECT_EQ("foobar_allocator", root_heap->allocator_name()); |
59 EXPECT_EQ(4096u, mad_foo->physical_size_in_bytes()); | 65 EXPECT_EQ("", root_heap->heap_name()); |
60 EXPECT_EQ(42u, mad_foo->allocated_objects_count()); | 66 EXPECT_NE("", root_heap->GetAbsoluteName()); |
61 EXPECT_EQ(1000u, mad_foo->allocated_objects_size_in_bytes()); | 67 EXPECT_EQ(4096u, root_heap->physical_size_in_bytes()); |
| 68 EXPECT_EQ(42u, root_heap->allocated_objects_count()); |
| 69 EXPECT_EQ(1000u, root_heap->allocated_objects_size_in_bytes()); |
62 | 70 |
63 // Check the extra attributes of |mad_foo|. | 71 // Check the extra attributes of |root_heap|. |
64 EXPECT_EQ(1234, mad_foo->GetExtraIntegerAttribute("attr1")); | 72 EXPECT_EQ(1234, root_heap->GetIntegerAttribute("attr1")); |
65 EXPECT_EQ(99, mad_foo->GetExtraIntegerAttribute("attr2")); | 73 EXPECT_EQ(99, root_heap->GetIntegerAttribute("attr2")); |
66 | 74 |
67 const MemoryAllocatorDump* mad_bar = pmd.GetAllocatorDump("foo/bar"); | 75 const MemoryAllocatorDump* sub_heap = |
68 ASSERT_NE(nullptr, mad_bar); | 76 pmd.GetAllocatorDump("foobar_allocator", "sub_heap"); |
69 EXPECT_EQ("foo/bar", mad_bar->name()); | 77 ASSERT_NE(nullptr, sub_heap); |
70 ASSERT_EQ(mad_foo, mad_bar->parent()); | 78 EXPECT_EQ("foobar_allocator", sub_heap->allocator_name()); |
71 EXPECT_EQ(1u, mad_bar->physical_size_in_bytes()); | 79 EXPECT_EQ("sub_heap", sub_heap->heap_name()); |
72 EXPECT_EQ(2u, mad_bar->allocated_objects_count()); | 80 EXPECT_NE("", sub_heap->GetAbsoluteName()); |
73 EXPECT_EQ(3u, mad_bar->allocated_objects_size_in_bytes()); | 81 EXPECT_EQ(1u, sub_heap->physical_size_in_bytes()); |
| 82 EXPECT_EQ(2u, sub_heap->allocated_objects_count()); |
| 83 EXPECT_EQ(3u, sub_heap->allocated_objects_size_in_bytes()); |
74 | 84 |
75 const MemoryAllocatorDump* mad_baz = pmd.GetAllocatorDump("baz"); | 85 const MemoryAllocatorDump* empty_sub_heap = |
76 ASSERT_NE(nullptr, mad_baz); | 86 pmd.GetAllocatorDump("foobar_allocator", "sub_heap/empty"); |
77 EXPECT_EQ("baz", mad_baz->name()); | 87 ASSERT_NE(nullptr, empty_sub_heap); |
78 ASSERT_EQ(nullptr, mad_baz->parent()); | 88 EXPECT_EQ("foobar_allocator", empty_sub_heap->allocator_name()); |
79 EXPECT_EQ(0u, mad_baz->physical_size_in_bytes()); | 89 EXPECT_EQ("sub_heap/empty", empty_sub_heap->heap_name()); |
80 EXPECT_EQ(0u, mad_baz->allocated_objects_count()); | 90 EXPECT_NE("", sub_heap->GetAbsoluteName()); |
81 EXPECT_EQ(0u, mad_baz->allocated_objects_size_in_bytes()); | 91 EXPECT_EQ(0u, empty_sub_heap->physical_size_in_bytes()); |
| 92 EXPECT_EQ(0u, empty_sub_heap->allocated_objects_count()); |
| 93 EXPECT_EQ(0u, empty_sub_heap->allocated_objects_size_in_bytes()); |
| 94 |
| 95 // Check that the AsValueInfo doesn't hit any DCHECK. |
| 96 scoped_refptr<TracedValue> traced_value(new TracedValue()); |
| 97 pmd.AsValueInto(traced_value.get()); |
82 } | 98 } |
83 | 99 |
84 // DEATH tests are not supported in Android / iOS. | 100 // DEATH tests are not supported in Android / iOS. |
85 #if !defined(NDEBUG) && !defined(OS_ANDROID) && !defined(OS_IOS) | 101 #if !defined(NDEBUG) && !defined(OS_ANDROID) && !defined(OS_IOS) |
86 TEST(MemoryAllocatorDumpTest, ForbidDuplicatesDeathTest) { | 102 TEST(MemoryAllocatorDumpTest, ForbidDuplicatesDeathTest) { |
87 FakeMemoryAllocatorDumpProvider fmadp; | 103 FakeMemoryAllocatorDumpProvider fmadp; |
88 ProcessMemoryDump pmd(nullptr /* session_state */); | 104 ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState())); |
89 pmd.CreateAllocatorDump("dump_1"); | 105 pmd.CreateAllocatorDump("foo_allocator", MemoryAllocatorDump::kRootHeap); |
90 pmd.CreateAllocatorDump("dump_2"); | 106 pmd.CreateAllocatorDump("bar_allocator", "heap"); |
91 ASSERT_DEATH(pmd.CreateAllocatorDump("dump_1"), ""); | 107 ASSERT_DEATH( |
| 108 pmd.CreateAllocatorDump("foo_allocator", MemoryAllocatorDump::kRootHeap), |
| 109 ""); |
| 110 ASSERT_DEATH(pmd.CreateAllocatorDump("bar_allocator", "heap"), ""); |
| 111 ASSERT_DEATH(pmd.CreateAllocatorDump("", "must_have_allocator_name"), ""); |
92 } | 112 } |
93 #endif | 113 #endif |
94 | 114 |
95 } // namespace trace_event | 115 } // namespace trace_event |
96 } // namespace base | 116 } // namespace base |
OLD | NEW |