Index: base/trace_event/memory_allocator_dump_unittest.cc |
diff --git a/base/trace_event/memory_allocator_dump_unittest.cc b/base/trace_event/memory_allocator_dump_unittest.cc |
index 110a25d93c44b06acc9b4a5e908740e1d081e14b..0b2cbdf513d01de3762debf8b9a709078186b64c 100644 |
--- a/base/trace_event/memory_allocator_dump_unittest.cc |
+++ b/base/trace_event/memory_allocator_dump_unittest.cc |
@@ -4,6 +4,8 @@ |
#include "base/trace_event/memory_allocator_dump.h" |
+#include "base/format_macros.h" |
+#include "base/strings/stringprintf.h" |
#include "base/trace_event/memory_dump_provider.h" |
#include "base/trace_event/memory_dump_session_state.h" |
#include "base/trace_event/process_memory_dump.h" |
@@ -17,80 +19,115 @@ namespace { |
class FakeMemoryAllocatorDumpProvider : public MemoryDumpProvider { |
public: |
- FakeMemoryAllocatorDumpProvider() { |
- DeclareAllocatorAttribute("foobar_allocator", "attr1", "count"); |
- DeclareAllocatorAttribute("foobar_allocator", "attr2", "bytes"); |
- } |
- |
- bool DumpInto(ProcessMemoryDump* pmd) override { |
- MemoryAllocatorDump* root_heap = pmd->CreateAllocatorDump( |
- "foobar_allocator", MemoryAllocatorDump::kRootHeap); |
- root_heap->set_physical_size_in_bytes(4096); |
- root_heap->set_allocated_objects_count(42); |
- root_heap->set_allocated_objects_size_in_bytes(1000); |
- root_heap->SetAttribute("attr1", 1234); |
- root_heap->SetAttribute("attr2", 99); |
+ bool OnMemoryDump(ProcessMemoryDump* pmd) override { |
+ MemoryAllocatorDump* root_heap = |
+ pmd->CreateAllocatorDump("foobar_allocator"); |
+ |
+ root_heap->AddScalar(MemoryAllocatorDump::kNameOuterSize, |
+ MemoryAllocatorDump::kUnitsBytes, 4096); |
+ root_heap->AddScalar(MemoryAllocatorDump::kNameInnerSize, |
+ MemoryAllocatorDump::kUnitsBytes, 1000); |
+ root_heap->AddScalar(MemoryAllocatorDump::kNameObjectsCount, |
+ MemoryAllocatorDump::kUnitsObjects, 42); |
+ root_heap->AddScalar("attr1", "units1", 1234); |
+ root_heap->AddString("attr2", "units2", "string_value"); |
MemoryAllocatorDump* sub_heap = |
- pmd->CreateAllocatorDump("foobar_allocator", "sub_heap"); |
- sub_heap->set_physical_size_in_bytes(1); |
- sub_heap->set_allocated_objects_count(2); |
- sub_heap->set_allocated_objects_size_in_bytes(3); |
- |
- pmd->CreateAllocatorDump("foobar_allocator", "sub_heap/empty"); |
+ pmd->CreateAllocatorDump("foobar_allocator/sub_heap"); |
+ sub_heap->AddScalar(MemoryAllocatorDump::kNameOuterSize, |
+ MemoryAllocatorDump::kUnitsBytes, 1); |
+ sub_heap->AddScalar(MemoryAllocatorDump::kNameInnerSize, |
+ MemoryAllocatorDump::kUnitsBytes, 2); |
+ sub_heap->AddScalar(MemoryAllocatorDump::kNameObjectsCount, |
+ MemoryAllocatorDump::kUnitsObjects, 3); |
+ |
+ pmd->CreateAllocatorDump("foobar_allocator/sub_heap/empty"); |
// Leave the rest of sub heap deliberately uninitialized, to check that |
// CreateAllocatorDump returns a properly zero-initialized object. |
return true; |
} |
- |
- const char* GetFriendlyName() const override { return "FooBar Allocator"; } |
}; |
+ |
+void CheckAttribute(const MemoryAllocatorDump* dump, |
+ const std::string& name, |
+ const char* expected_type, |
+ const char* expected_units, |
+ const std::string& expected_value) { |
+ const char* attr_type; |
+ const char* attr_units; |
+ const Value* attr_value; |
+ std::string attr_str_value; |
+ bool res = dump->Get(name, &attr_type, &attr_units, &attr_value); |
+ EXPECT_TRUE(res); |
+ if (!res) |
+ return; |
+ EXPECT_EQ(expected_type, std::string(attr_type)); |
+ EXPECT_EQ(expected_units, std::string(attr_units)); |
+ EXPECT_TRUE(attr_value->GetAsString(&attr_str_value)); |
+ EXPECT_EQ(expected_value, attr_str_value); |
+} |
+ |
+void CheckAttribute(const MemoryAllocatorDump* dump, |
+ const std::string& name, |
+ const char* expected_type, |
+ const char* expected_units, |
+ uint64 expected_value) { |
+ CheckAttribute(dump, name, expected_type, expected_units, |
+ StringPrintf("%" PRIx64, expected_value)); |
+} |
} // namespace |
TEST(MemoryAllocatorDumpTest, DumpIntoProcessMemoryDump) { |
FakeMemoryAllocatorDumpProvider fmadp; |
ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState())); |
- pmd.session_state()->allocators_attributes_type_info.Update( |
- fmadp.allocator_attributes_type_info()); |
- fmadp.DumpInto(&pmd); |
+ fmadp.OnMemoryDump(&pmd); |
ASSERT_EQ(3u, pmd.allocator_dumps().size()); |
const MemoryAllocatorDump* root_heap = |
- pmd.GetAllocatorDump("foobar_allocator", MemoryAllocatorDump::kRootHeap); |
+ pmd.GetAllocatorDump("foobar_allocator"); |
ASSERT_NE(nullptr, root_heap); |
- EXPECT_EQ("foobar_allocator", root_heap->allocator_name()); |
- EXPECT_EQ("", root_heap->heap_name()); |
- EXPECT_NE("", root_heap->GetAbsoluteName()); |
- EXPECT_EQ(4096u, root_heap->physical_size_in_bytes()); |
- EXPECT_EQ(42u, root_heap->allocated_objects_count()); |
- EXPECT_EQ(1000u, root_heap->allocated_objects_size_in_bytes()); |
- |
- // Check the extra attributes of |root_heap|. |
- EXPECT_EQ(1234, root_heap->GetIntegerAttribute("attr1")); |
- EXPECT_EQ(99, root_heap->GetIntegerAttribute("attr2")); |
+ EXPECT_EQ("foobar_allocator", root_heap->absolute_name()); |
+ CheckAttribute(root_heap, MemoryAllocatorDump::kNameOuterSize, |
+ MemoryAllocatorDump::kTypeScalar, |
+ MemoryAllocatorDump::kUnitsBytes, 4096); |
+ CheckAttribute(root_heap, MemoryAllocatorDump::kNameInnerSize, |
+ MemoryAllocatorDump::kTypeScalar, |
+ MemoryAllocatorDump::kUnitsBytes, 1000); |
+ CheckAttribute(root_heap, MemoryAllocatorDump::kNameObjectsCount, |
+ MemoryAllocatorDump::kTypeScalar, |
+ MemoryAllocatorDump::kUnitsObjects, 42); |
+ CheckAttribute(root_heap, "attr1", MemoryAllocatorDump::kTypeScalar, "units1", |
+ 1234); |
+ CheckAttribute(root_heap, "attr2", MemoryAllocatorDump::kTypeString, "units2", |
+ "string_value"); |
const MemoryAllocatorDump* sub_heap = |
- pmd.GetAllocatorDump("foobar_allocator", "sub_heap"); |
+ pmd.GetAllocatorDump("foobar_allocator/sub_heap"); |
ASSERT_NE(nullptr, sub_heap); |
- EXPECT_EQ("foobar_allocator", sub_heap->allocator_name()); |
- EXPECT_EQ("sub_heap", sub_heap->heap_name()); |
- EXPECT_NE("", sub_heap->GetAbsoluteName()); |
- EXPECT_EQ(1u, sub_heap->physical_size_in_bytes()); |
- EXPECT_EQ(2u, sub_heap->allocated_objects_count()); |
- EXPECT_EQ(3u, sub_heap->allocated_objects_size_in_bytes()); |
+ EXPECT_EQ("foobar_allocator/sub_heap", sub_heap->absolute_name()); |
+ CheckAttribute(sub_heap, MemoryAllocatorDump::kNameOuterSize, |
+ MemoryAllocatorDump::kTypeScalar, |
+ MemoryAllocatorDump::kUnitsBytes, 1); |
+ CheckAttribute(sub_heap, MemoryAllocatorDump::kNameInnerSize, |
+ MemoryAllocatorDump::kTypeScalar, |
+ MemoryAllocatorDump::kUnitsBytes, 2); |
+ CheckAttribute(sub_heap, MemoryAllocatorDump::kNameObjectsCount, |
+ MemoryAllocatorDump::kTypeScalar, |
+ MemoryAllocatorDump::kUnitsObjects, 3); |
const MemoryAllocatorDump* empty_sub_heap = |
- pmd.GetAllocatorDump("foobar_allocator", "sub_heap/empty"); |
+ pmd.GetAllocatorDump("foobar_allocator/sub_heap/empty"); |
ASSERT_NE(nullptr, empty_sub_heap); |
- EXPECT_EQ("foobar_allocator", empty_sub_heap->allocator_name()); |
- EXPECT_EQ("sub_heap/empty", empty_sub_heap->heap_name()); |
- EXPECT_NE("", sub_heap->GetAbsoluteName()); |
- EXPECT_EQ(0u, empty_sub_heap->physical_size_in_bytes()); |
- EXPECT_EQ(0u, empty_sub_heap->allocated_objects_count()); |
- EXPECT_EQ(0u, empty_sub_heap->allocated_objects_size_in_bytes()); |
+ EXPECT_EQ("foobar_allocator/sub_heap/empty", empty_sub_heap->absolute_name()); |
+ ASSERT_FALSE(empty_sub_heap->Get(MemoryAllocatorDump::kNameOuterSize, nullptr, |
+ nullptr, nullptr)); |
+ ASSERT_FALSE(empty_sub_heap->Get(MemoryAllocatorDump::kNameInnerSize, nullptr, |
+ nullptr, nullptr)); |
+ ASSERT_FALSE(empty_sub_heap->Get(MemoryAllocatorDump::kNameObjectsCount, |
+ nullptr, nullptr, nullptr)); |
// Check that the AsValueInfo doesn't hit any DCHECK. |
scoped_refptr<TracedValue> traced_value(new TracedValue()); |
@@ -102,13 +139,11 @@ TEST(MemoryAllocatorDumpTest, DumpIntoProcessMemoryDump) { |
TEST(MemoryAllocatorDumpTest, ForbidDuplicatesDeathTest) { |
FakeMemoryAllocatorDumpProvider fmadp; |
ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState())); |
- pmd.CreateAllocatorDump("foo_allocator", MemoryAllocatorDump::kRootHeap); |
- pmd.CreateAllocatorDump("bar_allocator", "heap"); |
- ASSERT_DEATH( |
- pmd.CreateAllocatorDump("foo_allocator", MemoryAllocatorDump::kRootHeap), |
- ""); |
- ASSERT_DEATH(pmd.CreateAllocatorDump("bar_allocator", "heap"), ""); |
- ASSERT_DEATH(pmd.CreateAllocatorDump("", "must_have_allocator_name"), ""); |
+ pmd.CreateAllocatorDump("foo_allocator"); |
+ pmd.CreateAllocatorDump("bar_allocator/heap"); |
+ ASSERT_DEATH(pmd.CreateAllocatorDump("foo_allocator"), ""); |
+ ASSERT_DEATH(pmd.CreateAllocatorDump("bar_allocator/heap"), ""); |
+ ASSERT_DEATH(pmd.CreateAllocatorDump(""), ""); |
} |
#endif |