Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(333)

Side by Side Diff: base/trace_event/memory_allocator_dump_unittest.cc

Issue 1180693002: Update from https://crrev.com/333737 (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: rebased Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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" 7 #include "base/format_macros.h"
8 #include "base/strings/stringprintf.h" 8 #include "base/strings/stringprintf.h"
9 #include "base/trace_event/memory_allocator_dump_guid.h"
9 #include "base/trace_event/memory_dump_provider.h" 10 #include "base/trace_event/memory_dump_provider.h"
10 #include "base/trace_event/memory_dump_session_state.h" 11 #include "base/trace_event/memory_dump_session_state.h"
11 #include "base/trace_event/process_memory_dump.h" 12 #include "base/trace_event/process_memory_dump.h"
12 #include "base/trace_event/trace_event_argument.h" 13 #include "base/trace_event/trace_event_argument.h"
13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
14 15
15 namespace base { 16 namespace base {
16 namespace trace_event { 17 namespace trace_event {
17 18
18 namespace { 19 namespace {
19 20
20 class FakeMemoryAllocatorDumpProvider : public MemoryDumpProvider { 21 class FakeMemoryAllocatorDumpProvider : public MemoryDumpProvider {
21 public: 22 public:
22 bool OnMemoryDump(ProcessMemoryDump* pmd) override { 23 bool OnMemoryDump(ProcessMemoryDump* pmd) override {
23 MemoryAllocatorDump* root_heap = 24 MemoryAllocatorDump* root_heap =
24 pmd->CreateAllocatorDump("foobar_allocator"); 25 pmd->CreateAllocatorDump("foobar_allocator");
25 26
26 root_heap->AddScalar(MemoryAllocatorDump::kNameOuterSize, 27 root_heap->AddScalar(MemoryAllocatorDump::kNameSize,
27 MemoryAllocatorDump::kUnitsBytes, 4096); 28 MemoryAllocatorDump::kUnitsBytes, 4096);
28 root_heap->AddScalar(MemoryAllocatorDump::kNameInnerSize,
29 MemoryAllocatorDump::kUnitsBytes, 1000);
30 root_heap->AddScalar(MemoryAllocatorDump::kNameObjectsCount, 29 root_heap->AddScalar(MemoryAllocatorDump::kNameObjectsCount,
31 MemoryAllocatorDump::kUnitsObjects, 42); 30 MemoryAllocatorDump::kUnitsObjects, 42);
32 root_heap->AddScalar("attr1", "units1", 1234); 31 root_heap->AddScalar("attr1", "units1", 1234);
33 root_heap->AddString("attr2", "units2", "string_value"); 32 root_heap->AddString("attr2", "units2", "string_value");
33 root_heap->AddScalarF("attr3", "units3", 42.5f);
34 34
35 MemoryAllocatorDump* sub_heap = 35 MemoryAllocatorDump* sub_heap =
36 pmd->CreateAllocatorDump("foobar_allocator/sub_heap"); 36 pmd->CreateAllocatorDump("foobar_allocator/sub_heap");
37 sub_heap->AddScalar(MemoryAllocatorDump::kNameOuterSize, 37 sub_heap->AddScalar(MemoryAllocatorDump::kNameSize,
38 MemoryAllocatorDump::kUnitsBytes, 1); 38 MemoryAllocatorDump::kUnitsBytes, 1);
39 sub_heap->AddScalar(MemoryAllocatorDump::kNameInnerSize,
40 MemoryAllocatorDump::kUnitsBytes, 2);
41 sub_heap->AddScalar(MemoryAllocatorDump::kNameObjectsCount, 39 sub_heap->AddScalar(MemoryAllocatorDump::kNameObjectsCount,
42 MemoryAllocatorDump::kUnitsObjects, 3); 40 MemoryAllocatorDump::kUnitsObjects, 3);
43 41
44 pmd->CreateAllocatorDump("foobar_allocator/sub_heap/empty"); 42 pmd->CreateAllocatorDump("foobar_allocator/sub_heap/empty");
45 // Leave the rest of sub heap deliberately uninitialized, to check that 43 // Leave the rest of sub heap deliberately uninitialized, to check that
46 // CreateAllocatorDump returns a properly zero-initialized object. 44 // CreateAllocatorDump returns a properly zero-initialized object.
47 45
48 return true; 46 return true;
49 } 47 }
50 }; 48 };
51 49
52 void CheckAttribute(const MemoryAllocatorDump* dump, 50 bool CheckAttribute(const MemoryAllocatorDump* dump,
53 const std::string& name, 51 const std::string& name,
54 const char* expected_type, 52 const char* expected_type,
55 const char* expected_units, 53 const char* expected_units,
56 const std::string& expected_value) { 54 const Value** out_value) {
57 const char* attr_type; 55 const char* attr_type;
58 const char* attr_units; 56 const char* attr_units;
59 const Value* attr_value; 57 bool res = dump->Get(name, &attr_type, &attr_units, out_value);
60 std::string attr_str_value;
61 bool res = dump->Get(name, &attr_type, &attr_units, &attr_value);
62 EXPECT_TRUE(res); 58 EXPECT_TRUE(res);
63 if (!res) 59 if (!res)
64 return; 60 return false;
65 EXPECT_EQ(expected_type, std::string(attr_type)); 61 EXPECT_EQ(expected_type, std::string(attr_type));
66 EXPECT_EQ(expected_units, std::string(attr_units)); 62 EXPECT_EQ(expected_units, std::string(attr_units));
63 return true;
64 }
65
66 void CheckString(const MemoryAllocatorDump* dump,
67 const std::string& name,
68 const char* expected_type,
69 const char* expected_units,
70 const std::string& expected_value) {
71 const Value* attr_value = nullptr;
72 std::string attr_str_value;
73 bool res =
74 CheckAttribute(dump, name, expected_type, expected_units, &attr_value);
75 if (!res)
76 return;
67 EXPECT_TRUE(attr_value->GetAsString(&attr_str_value)); 77 EXPECT_TRUE(attr_value->GetAsString(&attr_str_value));
68 EXPECT_EQ(expected_value, attr_str_value); 78 EXPECT_EQ(expected_value, attr_str_value);
69 } 79 }
70 80
71 void CheckAttribute(const MemoryAllocatorDump* dump, 81 void CheckScalar(const MemoryAllocatorDump* dump,
72 const std::string& name, 82 const std::string& name,
73 const char* expected_type, 83 const char* expected_units,
74 const char* expected_units, 84 uint64 expected_value) {
75 uint64 expected_value) { 85 CheckString(dump, name, MemoryAllocatorDump::kTypeScalar, expected_units,
76 CheckAttribute(dump, name, expected_type, expected_units, 86 StringPrintf("%" PRIx64, expected_value));
77 StringPrintf("%" PRIx64, expected_value));
78 } 87 }
88
89 void CheckScalarF(const MemoryAllocatorDump* dump,
90 const std::string& name,
91 const char* expected_units,
92 double expected_value) {
93 const Value* attr_value = nullptr;
94 double attr_double_value;
95 bool res = CheckAttribute(dump, name, MemoryAllocatorDump::kTypeScalar,
96 expected_units, &attr_value);
97 if (!res)
98 return;
99 EXPECT_TRUE(attr_value->GetAsDouble(&attr_double_value));
100 EXPECT_EQ(expected_value, attr_double_value);
101 }
102
79 } // namespace 103 } // namespace
80 104
105 TEST(MemoryAllocatorDumpTest, GuidGeneration) {
106 scoped_ptr<MemoryAllocatorDump> mad(
107 new MemoryAllocatorDump("foo", nullptr, MemoryAllocatorDumpGuid(0x42u)));
108 ASSERT_EQ("42", mad->guid().ToString());
109
110 // If the dumper does not provide a Guid, the MAD will make one up on the
111 // flight. Furthermore that Guid will stay stable across across multiple
112 // snapshots if the |absolute_name| of the dump doesn't change
113 mad.reset(new MemoryAllocatorDump("bar", nullptr));
114 const MemoryAllocatorDumpGuid guid_bar = mad->guid();
115 ASSERT_FALSE(guid_bar.empty());
116 ASSERT_FALSE(guid_bar.ToString().empty());
117 ASSERT_EQ(guid_bar, mad->guid());
118
119 mad.reset(new MemoryAllocatorDump("bar", nullptr));
120 const MemoryAllocatorDumpGuid guid_bar_2 = mad->guid();
121 ASSERT_EQ(guid_bar, guid_bar_2);
122
123 mad.reset(new MemoryAllocatorDump("baz", nullptr));
124 const MemoryAllocatorDumpGuid guid_baz = mad->guid();
125 ASSERT_NE(guid_bar, guid_baz);
126 }
127
81 TEST(MemoryAllocatorDumpTest, DumpIntoProcessMemoryDump) { 128 TEST(MemoryAllocatorDumpTest, DumpIntoProcessMemoryDump) {
82 FakeMemoryAllocatorDumpProvider fmadp; 129 FakeMemoryAllocatorDumpProvider fmadp;
83 ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState())); 130 ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState()));
84 131
85 fmadp.OnMemoryDump(&pmd); 132 fmadp.OnMemoryDump(&pmd);
86 133
87 ASSERT_EQ(3u, pmd.allocator_dumps().size()); 134 ASSERT_EQ(3u, pmd.allocator_dumps().size());
88 135
89 const MemoryAllocatorDump* root_heap = 136 const MemoryAllocatorDump* root_heap =
90 pmd.GetAllocatorDump("foobar_allocator"); 137 pmd.GetAllocatorDump("foobar_allocator");
91 ASSERT_NE(nullptr, root_heap); 138 ASSERT_NE(nullptr, root_heap);
92 EXPECT_EQ("foobar_allocator", root_heap->absolute_name()); 139 EXPECT_EQ("foobar_allocator", root_heap->absolute_name());
93 CheckAttribute(root_heap, MemoryAllocatorDump::kNameOuterSize, 140 CheckScalar(root_heap, MemoryAllocatorDump::kNameSize,
94 MemoryAllocatorDump::kTypeScalar, 141 MemoryAllocatorDump::kUnitsBytes, 4096);
95 MemoryAllocatorDump::kUnitsBytes, 4096); 142 CheckScalar(root_heap, MemoryAllocatorDump::kNameObjectsCount,
96 CheckAttribute(root_heap, MemoryAllocatorDump::kNameInnerSize, 143 MemoryAllocatorDump::kUnitsObjects, 42);
97 MemoryAllocatorDump::kTypeScalar, 144 CheckScalar(root_heap, "attr1", "units1", 1234);
98 MemoryAllocatorDump::kUnitsBytes, 1000); 145 CheckString(root_heap, "attr2", MemoryAllocatorDump::kTypeString, "units2",
99 CheckAttribute(root_heap, MemoryAllocatorDump::kNameObjectsCount, 146 "string_value");
100 MemoryAllocatorDump::kTypeScalar, 147 CheckScalarF(root_heap, "attr3", "units3", 42.5f);
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");
106 148
107 const MemoryAllocatorDump* sub_heap = 149 const MemoryAllocatorDump* sub_heap =
108 pmd.GetAllocatorDump("foobar_allocator/sub_heap"); 150 pmd.GetAllocatorDump("foobar_allocator/sub_heap");
109 ASSERT_NE(nullptr, sub_heap); 151 ASSERT_NE(nullptr, sub_heap);
110 EXPECT_EQ("foobar_allocator/sub_heap", sub_heap->absolute_name()); 152 EXPECT_EQ("foobar_allocator/sub_heap", sub_heap->absolute_name());
111 CheckAttribute(sub_heap, MemoryAllocatorDump::kNameOuterSize, 153 CheckScalar(sub_heap, MemoryAllocatorDump::kNameSize,
112 MemoryAllocatorDump::kTypeScalar, 154 MemoryAllocatorDump::kUnitsBytes, 1);
113 MemoryAllocatorDump::kUnitsBytes, 1); 155 CheckScalar(sub_heap, MemoryAllocatorDump::kNameObjectsCount,
114 CheckAttribute(sub_heap, MemoryAllocatorDump::kNameInnerSize, 156 MemoryAllocatorDump::kUnitsObjects, 3);
115 MemoryAllocatorDump::kTypeScalar,
116 MemoryAllocatorDump::kUnitsBytes, 2);
117 CheckAttribute(sub_heap, MemoryAllocatorDump::kNameObjectsCount,
118 MemoryAllocatorDump::kTypeScalar,
119 MemoryAllocatorDump::kUnitsObjects, 3);
120 157
121 const MemoryAllocatorDump* empty_sub_heap = 158 const MemoryAllocatorDump* empty_sub_heap =
122 pmd.GetAllocatorDump("foobar_allocator/sub_heap/empty"); 159 pmd.GetAllocatorDump("foobar_allocator/sub_heap/empty");
123 ASSERT_NE(nullptr, empty_sub_heap); 160 ASSERT_NE(nullptr, empty_sub_heap);
124 EXPECT_EQ("foobar_allocator/sub_heap/empty", empty_sub_heap->absolute_name()); 161 EXPECT_EQ("foobar_allocator/sub_heap/empty", empty_sub_heap->absolute_name());
125 ASSERT_FALSE(empty_sub_heap->Get(MemoryAllocatorDump::kNameOuterSize, nullptr, 162 ASSERT_FALSE(empty_sub_heap->Get(MemoryAllocatorDump::kNameSize, nullptr,
126 nullptr, nullptr));
127 ASSERT_FALSE(empty_sub_heap->Get(MemoryAllocatorDump::kNameInnerSize, nullptr,
128 nullptr, nullptr)); 163 nullptr, nullptr));
129 ASSERT_FALSE(empty_sub_heap->Get(MemoryAllocatorDump::kNameObjectsCount, 164 ASSERT_FALSE(empty_sub_heap->Get(MemoryAllocatorDump::kNameObjectsCount,
130 nullptr, nullptr, nullptr)); 165 nullptr, nullptr, nullptr));
131 166
132 // Check that the AsValueInfo doesn't hit any DCHECK. 167 // Check that the AsValueInfo doesn't hit any DCHECK.
133 scoped_refptr<TracedValue> traced_value(new TracedValue()); 168 scoped_refptr<TracedValue> traced_value(new TracedValue());
134 pmd.AsValueInto(traced_value.get()); 169 pmd.AsValueInto(traced_value.get());
135 } 170 }
136 171
137 // DEATH tests are not supported in Android / iOS. 172 // DEATH tests are not supported in Android / iOS.
138 #if !defined(NDEBUG) && !defined(OS_ANDROID) && !defined(OS_IOS) 173 #if !defined(NDEBUG) && !defined(OS_ANDROID) && !defined(OS_IOS)
139 TEST(MemoryAllocatorDumpTest, ForbidDuplicatesDeathTest) { 174 TEST(MemoryAllocatorDumpTest, ForbidDuplicatesDeathTest) {
140 FakeMemoryAllocatorDumpProvider fmadp; 175 FakeMemoryAllocatorDumpProvider fmadp;
141 ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState())); 176 ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState()));
142 pmd.CreateAllocatorDump("foo_allocator"); 177 pmd.CreateAllocatorDump("foo_allocator");
143 pmd.CreateAllocatorDump("bar_allocator/heap"); 178 pmd.CreateAllocatorDump("bar_allocator/heap");
144 ASSERT_DEATH(pmd.CreateAllocatorDump("foo_allocator"), ""); 179 ASSERT_DEATH(pmd.CreateAllocatorDump("foo_allocator"), "");
145 ASSERT_DEATH(pmd.CreateAllocatorDump("bar_allocator/heap"), ""); 180 ASSERT_DEATH(pmd.CreateAllocatorDump("bar_allocator/heap"), "");
146 ASSERT_DEATH(pmd.CreateAllocatorDump(""), ""); 181 ASSERT_DEATH(pmd.CreateAllocatorDump(""), "");
147 } 182 }
148 #endif 183 #endif
149 184
150 } // namespace trace_event 185 } // namespace trace_event
151 } // namespace base 186 } // namespace base
OLDNEW
« no previous file with comments | « base/trace_event/memory_allocator_dump_guid.cc ('k') | base/trace_event/memory_dump_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698