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