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/trace_event/memory_dump_provider.h"
8 #include "base/trace_event/process_memory_dump.h"
9 #include "testing/gtest/include/gtest/gtest.h"
12 namespace trace_event
{
15 class FakeMemoryAllocatorDumpProvider
: public MemoryDumpProvider
{
17 FakeMemoryAllocatorDumpProvider() {
18 DeclareAllocatorAttribute({"attr1", "count"});
19 DeclareAllocatorAttribute({"attr2", "bytes"});
22 bool DumpInto(ProcessMemoryDump
* pmd
) override
{
23 MemoryAllocatorDump
* mad_foo
= pmd
->CreateAllocatorDump("foo");
24 mad_foo
->set_physical_size_in_bytes(4096);
25 mad_foo
->set_allocated_objects_count(42);
26 mad_foo
->set_allocated_objects_size_in_bytes(1000);
27 mad_foo
->SetExtraAttribute("attr1", 1234);
28 mad_foo
->SetExtraAttribute("attr2", 99);
30 MemoryAllocatorDump
* mad_bar
= pmd
->CreateAllocatorDump("foo/bar", mad_foo
);
31 mad_bar
->set_physical_size_in_bytes(1);
32 mad_bar
->set_allocated_objects_count(2);
33 mad_bar
->set_allocated_objects_size_in_bytes(3);
35 pmd
->CreateAllocatorDump("baz");
36 // Leave the rest of |baz| deliberately uninitialized, to check that
37 // CreateAllocatorDump returns a properly zero-initialized object.
42 const char* GetFriendlyName() const override
{ return "mock_allocator"; }
46 TEST(MemoryAllocatorDumpTest
, DumpIntoProcessMemoryDump
) {
47 FakeMemoryAllocatorDumpProvider fmadp
;
48 ProcessMemoryDump pmd
;
52 ASSERT_EQ(3u, pmd
.allocator_dumps().size());
54 const MemoryAllocatorDump
* mad_foo
= pmd
.GetAllocatorDump("foo");
55 ASSERT_NE(nullptr, mad_foo
);
56 EXPECT_EQ("foo", mad_foo
->name());
57 ASSERT_EQ(nullptr, mad_foo
->parent());
58 EXPECT_EQ(4096u, mad_foo
->physical_size_in_bytes());
59 EXPECT_EQ(42u, mad_foo
->allocated_objects_count());
60 EXPECT_EQ(1000u, mad_foo
->allocated_objects_size_in_bytes());
62 // Check the extra attributes of |mad_foo|.
63 EXPECT_EQ(1234, mad_foo
->GetExtraIntegerAttribute("attr1"));
64 EXPECT_EQ(99, mad_foo
->GetExtraIntegerAttribute("attr2"));
66 const MemoryAllocatorDump
* mad_bar
= pmd
.GetAllocatorDump("foo/bar");
67 ASSERT_NE(nullptr, mad_bar
);
68 EXPECT_EQ("foo/bar", mad_bar
->name());
69 ASSERT_EQ(mad_foo
, mad_bar
->parent());
70 EXPECT_EQ(1u, mad_bar
->physical_size_in_bytes());
71 EXPECT_EQ(2u, mad_bar
->allocated_objects_count());
72 EXPECT_EQ(3u, mad_bar
->allocated_objects_size_in_bytes());
74 const MemoryAllocatorDump
* mad_baz
= pmd
.GetAllocatorDump("baz");
75 ASSERT_NE(nullptr, mad_baz
);
76 EXPECT_EQ("baz", mad_baz
->name());
77 ASSERT_EQ(nullptr, mad_baz
->parent());
78 EXPECT_EQ(0u, mad_baz
->physical_size_in_bytes());
79 EXPECT_EQ(0u, mad_baz
->allocated_objects_count());
80 EXPECT_EQ(0u, mad_baz
->allocated_objects_size_in_bytes());
83 // DEATH tests are not supported in Android / iOS.
84 #if !defined(NDEBUG) && !defined(OS_ANDROID) && !defined(OS_IOS)
85 TEST(MemoryAllocatorDumpTest
, ForbidDuplicatesDeathTest
) {
86 FakeMemoryAllocatorDumpProvider fmadp
;
87 ProcessMemoryDump pmd
;
88 pmd
.CreateAllocatorDump("dump_1");
89 pmd
.CreateAllocatorDump("dump_2");
90 ASSERT_DEATH(pmd
.CreateAllocatorDump("dump_1"), "");
94 } // namespace trace_event