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 bool OnMemoryDump(ProcessMemoryDump* pmd) override { | 22 bool OnMemoryDump(ProcessMemoryDump* pmd) override { |
21 MemoryAllocatorDump* root_heap = pmd->CreateAllocatorDump( | 23 MemoryAllocatorDump* root_heap = |
22 "foobar_allocator", MemoryAllocatorDump::kRootHeap); | 24 pmd->CreateAllocatorDump("foobar_allocator"); |
23 root_heap->set_physical_size_in_bytes(4096); | 25 |
24 root_heap->set_allocated_objects_count(42); | 26 root_heap->AddScalar(MemoryAllocatorDump::kNameOuterSize, |
25 root_heap->set_allocated_objects_size_in_bytes(1000); | 27 MemoryAllocatorDump::kUnitsBytes, 4096); |
| 28 root_heap->AddScalar(MemoryAllocatorDump::kNameInnerSize, |
| 29 MemoryAllocatorDump::kUnitsBytes, 1000); |
| 30 root_heap->AddScalar(MemoryAllocatorDump::kNameObjectsCount, |
| 31 MemoryAllocatorDump::kUnitsObjects, 42); |
| 32 root_heap->AddScalar("attr1", "units1", 1234); |
| 33 root_heap->AddString("attr2", "units2", "string_value"); |
26 | 34 |
27 MemoryAllocatorDump* sub_heap = | 35 MemoryAllocatorDump* sub_heap = |
28 pmd->CreateAllocatorDump("foobar_allocator", "sub_heap"); | 36 pmd->CreateAllocatorDump("foobar_allocator/sub_heap"); |
29 sub_heap->set_physical_size_in_bytes(1); | 37 sub_heap->AddScalar(MemoryAllocatorDump::kNameOuterSize, |
30 sub_heap->set_allocated_objects_count(2); | 38 MemoryAllocatorDump::kUnitsBytes, 1); |
31 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); |
32 | 43 |
33 pmd->CreateAllocatorDump("foobar_allocator", "sub_heap/empty"); | 44 pmd->CreateAllocatorDump("foobar_allocator/sub_heap/empty"); |
34 // Leave the rest of sub heap deliberately uninitialized, to check that | 45 // Leave the rest of sub heap deliberately uninitialized, to check that |
35 // CreateAllocatorDump returns a properly zero-initialized object. | 46 // CreateAllocatorDump returns a properly zero-initialized object. |
36 | 47 |
37 return true; | 48 return true; |
38 } | 49 } |
39 }; | 50 }; |
| 51 |
| 52 void CheckAttribute(const MemoryAllocatorDump* dump, |
| 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 } |
40 } // namespace | 79 } // namespace |
41 | 80 |
42 TEST(MemoryAllocatorDumpTest, DumpIntoProcessMemoryDump) { | 81 TEST(MemoryAllocatorDumpTest, DumpIntoProcessMemoryDump) { |
43 FakeMemoryAllocatorDumpProvider fmadp; | 82 FakeMemoryAllocatorDumpProvider fmadp; |
44 ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState())); | 83 ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState())); |
45 | 84 |
46 fmadp.OnMemoryDump(&pmd); | 85 fmadp.OnMemoryDump(&pmd); |
47 | 86 |
48 ASSERT_EQ(3u, pmd.allocator_dumps().size()); | 87 ASSERT_EQ(3u, pmd.allocator_dumps().size()); |
49 | 88 |
50 const MemoryAllocatorDump* root_heap = | 89 const MemoryAllocatorDump* root_heap = |
51 pmd.GetAllocatorDump("foobar_allocator", MemoryAllocatorDump::kRootHeap); | 90 pmd.GetAllocatorDump("foobar_allocator"); |
52 ASSERT_NE(nullptr, root_heap); | 91 ASSERT_NE(nullptr, root_heap); |
53 EXPECT_EQ("foobar_allocator", root_heap->allocator_name()); | 92 EXPECT_EQ("foobar_allocator", root_heap->absolute_name()); |
54 EXPECT_EQ("", root_heap->heap_name()); | 93 CheckAttribute(root_heap, MemoryAllocatorDump::kNameOuterSize, |
55 EXPECT_NE("", root_heap->GetAbsoluteName()); | 94 MemoryAllocatorDump::kTypeScalar, |
56 EXPECT_EQ(4096u, root_heap->physical_size_in_bytes()); | 95 MemoryAllocatorDump::kUnitsBytes, 4096); |
57 EXPECT_EQ(42u, root_heap->allocated_objects_count()); | 96 CheckAttribute(root_heap, MemoryAllocatorDump::kNameInnerSize, |
58 EXPECT_EQ(1000u, root_heap->allocated_objects_size_in_bytes()); | 97 MemoryAllocatorDump::kTypeScalar, |
| 98 MemoryAllocatorDump::kUnitsBytes, 1000); |
| 99 CheckAttribute(root_heap, MemoryAllocatorDump::kNameObjectsCount, |
| 100 MemoryAllocatorDump::kTypeScalar, |
| 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"); |
59 | 106 |
60 const MemoryAllocatorDump* sub_heap = | 107 const MemoryAllocatorDump* sub_heap = |
61 pmd.GetAllocatorDump("foobar_allocator", "sub_heap"); | 108 pmd.GetAllocatorDump("foobar_allocator/sub_heap"); |
62 ASSERT_NE(nullptr, sub_heap); | 109 ASSERT_NE(nullptr, sub_heap); |
63 EXPECT_EQ("foobar_allocator", sub_heap->allocator_name()); | 110 EXPECT_EQ("foobar_allocator/sub_heap", sub_heap->absolute_name()); |
64 EXPECT_EQ("sub_heap", sub_heap->heap_name()); | 111 CheckAttribute(sub_heap, MemoryAllocatorDump::kNameOuterSize, |
65 EXPECT_NE("", sub_heap->GetAbsoluteName()); | 112 MemoryAllocatorDump::kTypeScalar, |
66 EXPECT_EQ(1u, sub_heap->physical_size_in_bytes()); | 113 MemoryAllocatorDump::kUnitsBytes, 1); |
67 EXPECT_EQ(2u, sub_heap->allocated_objects_count()); | 114 CheckAttribute(sub_heap, MemoryAllocatorDump::kNameInnerSize, |
68 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); |
69 | 120 |
70 const MemoryAllocatorDump* empty_sub_heap = | 121 const MemoryAllocatorDump* empty_sub_heap = |
71 pmd.GetAllocatorDump("foobar_allocator", "sub_heap/empty"); | 122 pmd.GetAllocatorDump("foobar_allocator/sub_heap/empty"); |
72 ASSERT_NE(nullptr, empty_sub_heap); | 123 ASSERT_NE(nullptr, empty_sub_heap); |
73 EXPECT_EQ("foobar_allocator", empty_sub_heap->allocator_name()); | 124 EXPECT_EQ("foobar_allocator/sub_heap/empty", empty_sub_heap->absolute_name()); |
74 EXPECT_EQ("sub_heap/empty", empty_sub_heap->heap_name()); | 125 ASSERT_FALSE(empty_sub_heap->Get(MemoryAllocatorDump::kNameOuterSize, nullptr, |
75 EXPECT_NE("", sub_heap->GetAbsoluteName()); | 126 nullptr, nullptr)); |
76 EXPECT_EQ(0u, empty_sub_heap->physical_size_in_bytes()); | 127 ASSERT_FALSE(empty_sub_heap->Get(MemoryAllocatorDump::kNameInnerSize, nullptr, |
77 EXPECT_EQ(0u, empty_sub_heap->allocated_objects_count()); | 128 nullptr, nullptr)); |
78 EXPECT_EQ(0u, empty_sub_heap->allocated_objects_size_in_bytes()); | 129 ASSERT_FALSE(empty_sub_heap->Get(MemoryAllocatorDump::kNameObjectsCount, |
| 130 nullptr, nullptr, nullptr)); |
79 | 131 |
80 // Check that the AsValueInfo doesn't hit any DCHECK. | 132 // Check that the AsValueInfo doesn't hit any DCHECK. |
81 scoped_refptr<TracedValue> traced_value(new TracedValue()); | 133 scoped_refptr<TracedValue> traced_value(new TracedValue()); |
82 pmd.AsValueInto(traced_value.get()); | 134 pmd.AsValueInto(traced_value.get()); |
83 } | 135 } |
84 | 136 |
85 // DEATH tests are not supported in Android / iOS. | 137 // DEATH tests are not supported in Android / iOS. |
86 #if !defined(NDEBUG) && !defined(OS_ANDROID) && !defined(OS_IOS) | 138 #if !defined(NDEBUG) && !defined(OS_ANDROID) && !defined(OS_IOS) |
87 TEST(MemoryAllocatorDumpTest, ForbidDuplicatesDeathTest) { | 139 TEST(MemoryAllocatorDumpTest, ForbidDuplicatesDeathTest) { |
88 FakeMemoryAllocatorDumpProvider fmadp; | 140 FakeMemoryAllocatorDumpProvider fmadp; |
89 ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState())); | 141 ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState())); |
90 pmd.CreateAllocatorDump("foo_allocator", MemoryAllocatorDump::kRootHeap); | 142 pmd.CreateAllocatorDump("foo_allocator"); |
91 pmd.CreateAllocatorDump("bar_allocator", "heap"); | 143 pmd.CreateAllocatorDump("bar_allocator/heap"); |
92 ASSERT_DEATH( | 144 ASSERT_DEATH(pmd.CreateAllocatorDump("foo_allocator"), ""); |
93 pmd.CreateAllocatorDump("foo_allocator", MemoryAllocatorDump::kRootHeap), | 145 ASSERT_DEATH(pmd.CreateAllocatorDump("bar_allocator/heap"), ""); |
94 ""); | 146 ASSERT_DEATH(pmd.CreateAllocatorDump(""), ""); |
95 ASSERT_DEATH(pmd.CreateAllocatorDump("bar_allocator", "heap"), ""); | |
96 ASSERT_DEATH(pmd.CreateAllocatorDump("", "must_have_allocator_name"), ""); | |
97 } | 147 } |
98 #endif | 148 #endif |
99 | 149 |
100 } // namespace trace_event | 150 } // namespace trace_event |
101 } // namespace base | 151 } // namespace base |
OLD | NEW |