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 <stdint.h>
8
9 #include "base/format_macros.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/trace_event/memory_allocator_dump_guid.h"
12 #include "base/trace_event/memory_dump_provider.h"
13 #include "base/trace_event/memory_dump_session_state.h"
14 #include "base/trace_event/process_memory_dump.h"
15 #include "base/trace_event/trace_event_argument.h"
16 #include "base/values.h"
17 #include "build/build_config.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace base {
21 namespace trace_event {
22
23 namespace {
24
25 class FakeMemoryAllocatorDumpProvider : public MemoryDumpProvider {
26 public:
OnMemoryDump(const MemoryDumpArgs & args,ProcessMemoryDump * pmd)27 bool OnMemoryDump(const MemoryDumpArgs& args,
28 ProcessMemoryDump* pmd) override {
29 MemoryAllocatorDump* root_heap =
30 pmd->CreateAllocatorDump("foobar_allocator");
31
32 root_heap->AddScalar(MemoryAllocatorDump::kNameSize,
33 MemoryAllocatorDump::kUnitsBytes, 4096);
34 root_heap->AddScalar(MemoryAllocatorDump::kNameObjectCount,
35 MemoryAllocatorDump::kUnitsObjects, 42);
36 root_heap->AddScalar("attr1", "units1", 1234);
37 root_heap->AddString("attr2", "units2", "string_value");
38 root_heap->AddScalarF("attr3", "units3", 42.5f);
39
40 MemoryAllocatorDump* sub_heap =
41 pmd->CreateAllocatorDump("foobar_allocator/sub_heap");
42 sub_heap->AddScalar(MemoryAllocatorDump::kNameSize,
43 MemoryAllocatorDump::kUnitsBytes, 1);
44 sub_heap->AddScalar(MemoryAllocatorDump::kNameObjectCount,
45 MemoryAllocatorDump::kUnitsObjects, 3);
46
47 pmd->CreateAllocatorDump("foobar_allocator/sub_heap/empty");
48 // Leave the rest of sub heap deliberately uninitialized, to check that
49 // CreateAllocatorDump returns a properly zero-initialized object.
50
51 return true;
52 }
53 };
54
CheckAttribute(const MemoryAllocatorDump * dump,const std::string & name,const char * expected_type,const char * expected_units)55 scoped_ptr<Value> CheckAttribute(const MemoryAllocatorDump* dump,
56 const std::string& name,
57 const char* expected_type,
58 const char* expected_units) {
59 scoped_ptr<Value> raw_attrs = dump->attributes_for_testing()->ToBaseValue();
60 DictionaryValue* args = nullptr;
61 DictionaryValue* arg = nullptr;
62 std::string arg_value;
63 const Value* out_value = nullptr;
64 EXPECT_TRUE(raw_attrs->GetAsDictionary(&args));
65 EXPECT_TRUE(args->GetDictionary(name, &arg));
66 EXPECT_TRUE(arg->GetString("type", &arg_value));
67 EXPECT_EQ(expected_type, arg_value);
68 EXPECT_TRUE(arg->GetString("units", &arg_value));
69 EXPECT_EQ(expected_units, arg_value);
70 EXPECT_TRUE(arg->Get("value", &out_value));
71 return out_value ? out_value->CreateDeepCopy() : scoped_ptr<Value>();
72 }
73
CheckString(const MemoryAllocatorDump * dump,const std::string & name,const char * expected_type,const char * expected_units,const std::string & expected_value)74 void CheckString(const MemoryAllocatorDump* dump,
75 const std::string& name,
76 const char* expected_type,
77 const char* expected_units,
78 const std::string& expected_value) {
79 std::string attr_str_value;
80 auto attr_value = CheckAttribute(dump, name, expected_type, expected_units);
81 EXPECT_TRUE(attr_value->GetAsString(&attr_str_value));
82 EXPECT_EQ(expected_value, attr_str_value);
83 }
84
CheckScalar(const MemoryAllocatorDump * dump,const std::string & name,const char * expected_units,uint64_t expected_value)85 void CheckScalar(const MemoryAllocatorDump* dump,
86 const std::string& name,
87 const char* expected_units,
88 uint64_t expected_value) {
89 CheckString(dump, name, MemoryAllocatorDump::kTypeScalar, expected_units,
90 StringPrintf("%" PRIx64, expected_value));
91 }
92
CheckScalarF(const MemoryAllocatorDump * dump,const std::string & name,const char * expected_units,double expected_value)93 void CheckScalarF(const MemoryAllocatorDump* dump,
94 const std::string& name,
95 const char* expected_units,
96 double expected_value) {
97 auto attr_value = CheckAttribute(dump, name, MemoryAllocatorDump::kTypeScalar,
98 expected_units);
99 double attr_double_value;
100 EXPECT_TRUE(attr_value->GetAsDouble(&attr_double_value));
101 EXPECT_EQ(expected_value, attr_double_value);
102 }
103
104 } // namespace
105
TEST(MemoryAllocatorDumpTest,GuidGeneration)106 TEST(MemoryAllocatorDumpTest, GuidGeneration) {
107 scoped_ptr<MemoryAllocatorDump> mad(
108 new MemoryAllocatorDump("foo", nullptr, MemoryAllocatorDumpGuid(0x42u)));
109 ASSERT_EQ("42", mad->guid().ToString());
110
111 // If the dumper does not provide a Guid, the MAD will make one up on the
112 // flight. Furthermore that Guid will stay stable across across multiple
113 // snapshots if the |absolute_name| of the dump doesn't change
114 mad.reset(new MemoryAllocatorDump("bar", nullptr));
115 const MemoryAllocatorDumpGuid guid_bar = mad->guid();
116 ASSERT_FALSE(guid_bar.empty());
117 ASSERT_FALSE(guid_bar.ToString().empty());
118 ASSERT_EQ(guid_bar, mad->guid());
119
120 mad.reset(new MemoryAllocatorDump("bar", nullptr));
121 const MemoryAllocatorDumpGuid guid_bar_2 = mad->guid();
122 ASSERT_EQ(guid_bar, guid_bar_2);
123
124 mad.reset(new MemoryAllocatorDump("baz", nullptr));
125 const MemoryAllocatorDumpGuid guid_baz = mad->guid();
126 ASSERT_NE(guid_bar, guid_baz);
127 }
128
TEST(MemoryAllocatorDumpTest,DumpIntoProcessMemoryDump)129 TEST(MemoryAllocatorDumpTest, DumpIntoProcessMemoryDump) {
130 FakeMemoryAllocatorDumpProvider fmadp;
131 ProcessMemoryDump pmd(new MemoryDumpSessionState(nullptr, nullptr));
132 MemoryDumpArgs dump_args = {MemoryDumpLevelOfDetail::DETAILED};
133
134 fmadp.OnMemoryDump(dump_args, &pmd);
135
136 ASSERT_EQ(3u, pmd.allocator_dumps().size());
137
138 const MemoryAllocatorDump* root_heap =
139 pmd.GetAllocatorDump("foobar_allocator");
140 ASSERT_NE(nullptr, root_heap);
141 EXPECT_EQ("foobar_allocator", root_heap->absolute_name());
142 CheckScalar(root_heap, MemoryAllocatorDump::kNameSize,
143 MemoryAllocatorDump::kUnitsBytes, 4096);
144 CheckScalar(root_heap, MemoryAllocatorDump::kNameObjectCount,
145 MemoryAllocatorDump::kUnitsObjects, 42);
146 CheckScalar(root_heap, "attr1", "units1", 1234);
147 CheckString(root_heap, "attr2", MemoryAllocatorDump::kTypeString, "units2",
148 "string_value");
149 CheckScalarF(root_heap, "attr3", "units3", 42.5f);
150
151 const MemoryAllocatorDump* sub_heap =
152 pmd.GetAllocatorDump("foobar_allocator/sub_heap");
153 ASSERT_NE(nullptr, sub_heap);
154 EXPECT_EQ("foobar_allocator/sub_heap", sub_heap->absolute_name());
155 CheckScalar(sub_heap, MemoryAllocatorDump::kNameSize,
156 MemoryAllocatorDump::kUnitsBytes, 1);
157 CheckScalar(sub_heap, MemoryAllocatorDump::kNameObjectCount,
158 MemoryAllocatorDump::kUnitsObjects, 3);
159 const MemoryAllocatorDump* empty_sub_heap =
160 pmd.GetAllocatorDump("foobar_allocator/sub_heap/empty");
161 ASSERT_NE(nullptr, empty_sub_heap);
162 EXPECT_EQ("foobar_allocator/sub_heap/empty", empty_sub_heap->absolute_name());
163 auto raw_attrs = empty_sub_heap->attributes_for_testing()->ToBaseValue();
164 DictionaryValue* attrs = nullptr;
165 ASSERT_TRUE(raw_attrs->GetAsDictionary(&attrs));
166 ASSERT_FALSE(attrs->HasKey(MemoryAllocatorDump::kNameSize));
167 ASSERT_FALSE(attrs->HasKey(MemoryAllocatorDump::kNameObjectCount));
168
169 // Check that the AsValueInfo doesn't hit any DCHECK.
170 scoped_refptr<TracedValue> traced_value(new TracedValue());
171 pmd.AsValueInto(traced_value.get());
172 }
173
174 // DEATH tests are not supported in Android / iOS.
175 #if !defined(NDEBUG) && !defined(OS_ANDROID) && !defined(OS_IOS)
TEST(MemoryAllocatorDumpTest,ForbidDuplicatesDeathTest)176 TEST(MemoryAllocatorDumpTest, ForbidDuplicatesDeathTest) {
177 FakeMemoryAllocatorDumpProvider fmadp;
178 ProcessMemoryDump pmd(new MemoryDumpSessionState(nullptr, nullptr));
179 pmd.CreateAllocatorDump("foo_allocator");
180 pmd.CreateAllocatorDump("bar_allocator/heap");
181 ASSERT_DEATH(pmd.CreateAllocatorDump("foo_allocator"), "");
182 ASSERT_DEATH(pmd.CreateAllocatorDump("bar_allocator/heap"), "");
183 ASSERT_DEATH(pmd.CreateAllocatorDump(""), "");
184 }
185 #endif
186
187 } // namespace trace_event
188 } // namespace base
189