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/format_macros.h" |
| 8 #include "base/strings/stringprintf.h" |
7 #include "base/trace_event/memory_dump_provider.h" | 9 #include "base/trace_event/memory_dump_provider.h" |
8 #include "base/trace_event/memory_dump_session_state.h" | 10 #include "base/trace_event/memory_dump_session_state.h" |
9 #include "base/trace_event/process_memory_dump.h" | 11 #include "base/trace_event/process_memory_dump.h" |
10 #include "base/trace_event/trace_event_argument.h" | 12 #include "base/trace_event/trace_event_argument.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
12 | 14 |
13 namespace base { | 15 namespace base { |
14 namespace trace_event { | 16 namespace trace_event { |
15 | 17 |
16 namespace { | 18 namespace { |
17 | 19 |
18 class FakeMemoryAllocatorDumpProvider : public MemoryDumpProvider { | 20 class FakeMemoryAllocatorDumpProvider : public MemoryDumpProvider { |
19 public: | 21 public: |
20 FakeMemoryAllocatorDumpProvider() { | 22 bool OnMemoryDump(ProcessMemoryDump* pmd) override { |
21 DeclareAllocatorAttribute("foobar_allocator", "attr1", "count"); | 23 MemoryAllocatorDump* root_heap = |
22 DeclareAllocatorAttribute("foobar_allocator", "attr2", "bytes"); | 24 pmd->CreateAllocatorDump("foobar_allocator"); |
23 } | |
24 | 25 |
25 bool DumpInto(ProcessMemoryDump* pmd) override { | 26 root_heap->AddScalar(MemoryAllocatorDump::kNameOuterSize, |
26 MemoryAllocatorDump* root_heap = pmd->CreateAllocatorDump( | 27 MemoryAllocatorDump::kUnitsBytes, 4096); |
27 "foobar_allocator", MemoryAllocatorDump::kRootHeap); | 28 root_heap->AddScalar(MemoryAllocatorDump::kNameInnerSize, |
28 root_heap->set_physical_size_in_bytes(4096); | 29 MemoryAllocatorDump::kUnitsBytes, 1000); |
29 root_heap->set_allocated_objects_count(42); | 30 root_heap->AddScalar(MemoryAllocatorDump::kNameObjectsCount, |
30 root_heap->set_allocated_objects_size_in_bytes(1000); | 31 MemoryAllocatorDump::kUnitsObjects, 42); |
31 root_heap->SetAttribute("attr1", 1234); | 32 root_heap->AddScalar("attr1", "units1", 1234); |
32 root_heap->SetAttribute("attr2", 99); | 33 root_heap->AddString("attr2", "units2", "string_value"); |
33 | 34 |
34 MemoryAllocatorDump* sub_heap = | 35 MemoryAllocatorDump* sub_heap = |
35 pmd->CreateAllocatorDump("foobar_allocator", "sub_heap"); | 36 pmd->CreateAllocatorDump("foobar_allocator/sub_heap"); |
36 sub_heap->set_physical_size_in_bytes(1); | 37 sub_heap->AddScalar(MemoryAllocatorDump::kNameOuterSize, |
37 sub_heap->set_allocated_objects_count(2); | 38 MemoryAllocatorDump::kUnitsBytes, 1); |
38 sub_heap->set_allocated_objects_size_in_bytes(3); | 39 sub_heap->AddScalar(MemoryAllocatorDump::kNameInnerSize, |
| 40 MemoryAllocatorDump::kUnitsBytes, 2); |
| 41 sub_heap->AddScalar(MemoryAllocatorDump::kNameObjectsCount, |
| 42 MemoryAllocatorDump::kUnitsObjects, 3); |
39 | 43 |
40 pmd->CreateAllocatorDump("foobar_allocator", "sub_heap/empty"); | 44 pmd->CreateAllocatorDump("foobar_allocator/sub_heap/empty"); |
41 // Leave the rest of sub heap deliberately uninitialized, to check that | 45 // Leave the rest of sub heap deliberately uninitialized, to check that |
42 // CreateAllocatorDump returns a properly zero-initialized object. | 46 // CreateAllocatorDump returns a properly zero-initialized object. |
43 | 47 |
44 return true; | 48 return true; |
45 } | 49 } |
| 50 }; |
46 | 51 |
47 const char* GetFriendlyName() const override { return "FooBar Allocator"; } | 52 void CheckAttribute(const MemoryAllocatorDump* dump, |
48 }; | 53 const std::string& name, |
| 54 const char* expected_type, |
| 55 const char* expected_units, |
| 56 const std::string& expected_value) { |
| 57 const char* attr_type; |
| 58 const char* attr_units; |
| 59 const Value* attr_value; |
| 60 std::string attr_str_value; |
| 61 bool res = dump->Get(name, &attr_type, &attr_units, &attr_value); |
| 62 EXPECT_TRUE(res); |
| 63 if (!res) |
| 64 return; |
| 65 EXPECT_EQ(expected_type, std::string(attr_type)); |
| 66 EXPECT_EQ(expected_units, std::string(attr_units)); |
| 67 EXPECT_TRUE(attr_value->GetAsString(&attr_str_value)); |
| 68 EXPECT_EQ(expected_value, attr_str_value); |
| 69 } |
| 70 |
| 71 void CheckAttribute(const MemoryAllocatorDump* dump, |
| 72 const std::string& name, |
| 73 const char* expected_type, |
| 74 const char* expected_units, |
| 75 uint64 expected_value) { |
| 76 CheckAttribute(dump, name, expected_type, expected_units, |
| 77 StringPrintf("%" PRIx64, expected_value)); |
| 78 } |
49 } // namespace | 79 } // namespace |
50 | 80 |
51 TEST(MemoryAllocatorDumpTest, DumpIntoProcessMemoryDump) { | 81 TEST(MemoryAllocatorDumpTest, DumpIntoProcessMemoryDump) { |
52 FakeMemoryAllocatorDumpProvider fmadp; | 82 FakeMemoryAllocatorDumpProvider fmadp; |
53 ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState())); | 83 ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState())); |
54 pmd.session_state()->allocators_attributes_type_info.Update( | |
55 fmadp.allocator_attributes_type_info()); | |
56 | 84 |
57 fmadp.DumpInto(&pmd); | 85 fmadp.OnMemoryDump(&pmd); |
58 | 86 |
59 ASSERT_EQ(3u, pmd.allocator_dumps().size()); | 87 ASSERT_EQ(3u, pmd.allocator_dumps().size()); |
60 | 88 |
61 const MemoryAllocatorDump* root_heap = | 89 const MemoryAllocatorDump* root_heap = |
62 pmd.GetAllocatorDump("foobar_allocator", MemoryAllocatorDump::kRootHeap); | 90 pmd.GetAllocatorDump("foobar_allocator"); |
63 ASSERT_NE(nullptr, root_heap); | 91 ASSERT_NE(nullptr, root_heap); |
64 EXPECT_EQ("foobar_allocator", root_heap->allocator_name()); | 92 EXPECT_EQ("foobar_allocator", root_heap->absolute_name()); |
65 EXPECT_EQ("", root_heap->heap_name()); | 93 CheckAttribute(root_heap, MemoryAllocatorDump::kNameOuterSize, |
66 EXPECT_NE("", root_heap->GetAbsoluteName()); | 94 MemoryAllocatorDump::kTypeScalar, |
67 EXPECT_EQ(4096u, root_heap->physical_size_in_bytes()); | 95 MemoryAllocatorDump::kUnitsBytes, 4096); |
68 EXPECT_EQ(42u, root_heap->allocated_objects_count()); | 96 CheckAttribute(root_heap, MemoryAllocatorDump::kNameInnerSize, |
69 EXPECT_EQ(1000u, root_heap->allocated_objects_size_in_bytes()); | 97 MemoryAllocatorDump::kTypeScalar, |
70 | 98 MemoryAllocatorDump::kUnitsBytes, 1000); |
71 // Check the extra attributes of |root_heap|. | 99 CheckAttribute(root_heap, MemoryAllocatorDump::kNameObjectsCount, |
72 EXPECT_EQ(1234, root_heap->GetIntegerAttribute("attr1")); | 100 MemoryAllocatorDump::kTypeScalar, |
73 EXPECT_EQ(99, root_heap->GetIntegerAttribute("attr2")); | 101 MemoryAllocatorDump::kUnitsObjects, 42); |
| 102 CheckAttribute(root_heap, "attr1", MemoryAllocatorDump::kTypeScalar, "units1", |
| 103 1234); |
| 104 CheckAttribute(root_heap, "attr2", MemoryAllocatorDump::kTypeString, "units2", |
| 105 "string_value"); |
74 | 106 |
75 const MemoryAllocatorDump* sub_heap = | 107 const MemoryAllocatorDump* sub_heap = |
76 pmd.GetAllocatorDump("foobar_allocator", "sub_heap"); | 108 pmd.GetAllocatorDump("foobar_allocator/sub_heap"); |
77 ASSERT_NE(nullptr, sub_heap); | 109 ASSERT_NE(nullptr, sub_heap); |
78 EXPECT_EQ("foobar_allocator", sub_heap->allocator_name()); | 110 EXPECT_EQ("foobar_allocator/sub_heap", sub_heap->absolute_name()); |
79 EXPECT_EQ("sub_heap", sub_heap->heap_name()); | 111 CheckAttribute(sub_heap, MemoryAllocatorDump::kNameOuterSize, |
80 EXPECT_NE("", sub_heap->GetAbsoluteName()); | 112 MemoryAllocatorDump::kTypeScalar, |
81 EXPECT_EQ(1u, sub_heap->physical_size_in_bytes()); | 113 MemoryAllocatorDump::kUnitsBytes, 1); |
82 EXPECT_EQ(2u, sub_heap->allocated_objects_count()); | 114 CheckAttribute(sub_heap, MemoryAllocatorDump::kNameInnerSize, |
83 EXPECT_EQ(3u, sub_heap->allocated_objects_size_in_bytes()); | 115 MemoryAllocatorDump::kTypeScalar, |
| 116 MemoryAllocatorDump::kUnitsBytes, 2); |
| 117 CheckAttribute(sub_heap, MemoryAllocatorDump::kNameObjectsCount, |
| 118 MemoryAllocatorDump::kTypeScalar, |
| 119 MemoryAllocatorDump::kUnitsObjects, 3); |
84 | 120 |
85 const MemoryAllocatorDump* empty_sub_heap = | 121 const MemoryAllocatorDump* empty_sub_heap = |
86 pmd.GetAllocatorDump("foobar_allocator", "sub_heap/empty"); | 122 pmd.GetAllocatorDump("foobar_allocator/sub_heap/empty"); |
87 ASSERT_NE(nullptr, empty_sub_heap); | 123 ASSERT_NE(nullptr, empty_sub_heap); |
88 EXPECT_EQ("foobar_allocator", empty_sub_heap->allocator_name()); | 124 EXPECT_EQ("foobar_allocator/sub_heap/empty", empty_sub_heap->absolute_name()); |
89 EXPECT_EQ("sub_heap/empty", empty_sub_heap->heap_name()); | 125 ASSERT_FALSE(empty_sub_heap->Get(MemoryAllocatorDump::kNameOuterSize, nullptr, |
90 EXPECT_NE("", sub_heap->GetAbsoluteName()); | 126 nullptr, nullptr)); |
91 EXPECT_EQ(0u, empty_sub_heap->physical_size_in_bytes()); | 127 ASSERT_FALSE(empty_sub_heap->Get(MemoryAllocatorDump::kNameInnerSize, nullptr, |
92 EXPECT_EQ(0u, empty_sub_heap->allocated_objects_count()); | 128 nullptr, nullptr)); |
93 EXPECT_EQ(0u, empty_sub_heap->allocated_objects_size_in_bytes()); | 129 ASSERT_FALSE(empty_sub_heap->Get(MemoryAllocatorDump::kNameObjectsCount, |
| 130 nullptr, nullptr, nullptr)); |
94 | 131 |
95 // Check that the AsValueInfo doesn't hit any DCHECK. | 132 // Check that the AsValueInfo doesn't hit any DCHECK. |
96 scoped_refptr<TracedValue> traced_value(new TracedValue()); | 133 scoped_refptr<TracedValue> traced_value(new TracedValue()); |
97 pmd.AsValueInto(traced_value.get()); | 134 pmd.AsValueInto(traced_value.get()); |
98 } | 135 } |
99 | 136 |
100 // DEATH tests are not supported in Android / iOS. | 137 // DEATH tests are not supported in Android / iOS. |
101 #if !defined(NDEBUG) && !defined(OS_ANDROID) && !defined(OS_IOS) | 138 #if !defined(NDEBUG) && !defined(OS_ANDROID) && !defined(OS_IOS) |
102 TEST(MemoryAllocatorDumpTest, ForbidDuplicatesDeathTest) { | 139 TEST(MemoryAllocatorDumpTest, ForbidDuplicatesDeathTest) { |
103 FakeMemoryAllocatorDumpProvider fmadp; | 140 FakeMemoryAllocatorDumpProvider fmadp; |
104 ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState())); | 141 ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState())); |
105 pmd.CreateAllocatorDump("foo_allocator", MemoryAllocatorDump::kRootHeap); | 142 pmd.CreateAllocatorDump("foo_allocator"); |
106 pmd.CreateAllocatorDump("bar_allocator", "heap"); | 143 pmd.CreateAllocatorDump("bar_allocator/heap"); |
107 ASSERT_DEATH( | 144 ASSERT_DEATH(pmd.CreateAllocatorDump("foo_allocator"), ""); |
108 pmd.CreateAllocatorDump("foo_allocator", MemoryAllocatorDump::kRootHeap), | 145 ASSERT_DEATH(pmd.CreateAllocatorDump("bar_allocator/heap"), ""); |
109 ""); | 146 ASSERT_DEATH(pmd.CreateAllocatorDump(""), ""); |
110 ASSERT_DEATH(pmd.CreateAllocatorDump("bar_allocator", "heap"), ""); | |
111 ASSERT_DEATH(pmd.CreateAllocatorDump("", "must_have_allocator_name"), ""); | |
112 } | 147 } |
113 #endif | 148 #endif |
114 | 149 |
115 } // namespace trace_event | 150 } // namespace trace_event |
116 } // namespace base | 151 } // namespace base |
OLD | NEW |