Roll src/third_party/WebKit dbf9be3:8d6c3d5 (svn 202308:202312)
[chromium-blink-merge.git] / base / trace_event / process_memory_dump_unittest.cc
blob1e1fbc69cf91cab4262c4be1522e2e70705954b6
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/process_memory_dump.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/trace_event/memory_allocator_dump_guid.h"
9 #include "base/trace_event/trace_event_argument.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace base {
13 namespace trace_event {
15 TEST(ProcessMemoryDumpTest, Clear) {
16 scoped_ptr<ProcessMemoryDump> pmd1(new ProcessMemoryDump(nullptr));
17 pmd1->CreateAllocatorDump("mad1");
18 pmd1->CreateAllocatorDump("mad2");
19 ASSERT_FALSE(pmd1->allocator_dumps().empty());
21 pmd1->process_totals()->set_resident_set_bytes(42);
22 pmd1->set_has_process_totals();
24 pmd1->process_mmaps()->AddVMRegion(ProcessMemoryMaps::VMRegion());
25 pmd1->set_has_process_mmaps();
27 pmd1->AddOwnershipEdge(MemoryAllocatorDumpGuid(42),
28 MemoryAllocatorDumpGuid(4242));
30 MemoryAllocatorDumpGuid shared_mad_guid(1);
31 pmd1->CreateSharedGlobalAllocatorDump(shared_mad_guid);
33 pmd1->Clear();
34 ASSERT_TRUE(pmd1->allocator_dumps().empty());
35 ASSERT_TRUE(pmd1->allocator_dumps_edges().empty());
36 ASSERT_EQ(nullptr, pmd1->GetAllocatorDump("mad1"));
37 ASSERT_EQ(nullptr, pmd1->GetAllocatorDump("mad2"));
38 ASSERT_FALSE(pmd1->has_process_totals());
39 ASSERT_FALSE(pmd1->has_process_mmaps());
40 ASSERT_TRUE(pmd1->process_mmaps()->vm_regions().empty());
41 ASSERT_EQ(nullptr, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid));
43 // Check that calling AsValueInto() doesn't cause a crash.
44 scoped_refptr<TracedValue> traced_value(new TracedValue());
45 pmd1->AsValueInto(traced_value.get());
47 // Check that the pmd can be reused and behaves as expected.
48 auto mad1 = pmd1->CreateAllocatorDump("mad1");
49 auto mad3 = pmd1->CreateAllocatorDump("mad3");
50 auto shared_mad = pmd1->CreateSharedGlobalAllocatorDump(shared_mad_guid);
51 ASSERT_EQ(3u, pmd1->allocator_dumps().size());
52 ASSERT_EQ(mad1, pmd1->GetAllocatorDump("mad1"));
53 ASSERT_EQ(nullptr, pmd1->GetAllocatorDump("mad2"));
54 ASSERT_EQ(mad3, pmd1->GetAllocatorDump("mad3"));
55 ASSERT_EQ(shared_mad, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid));
57 traced_value = new TracedValue();
58 pmd1->AsValueInto(traced_value.get());
60 pmd1.reset();
63 TEST(ProcessMemoryDumpTest, TakeAllDumpsFrom) {
64 scoped_refptr<TracedValue> traced_value(new TracedValue());
66 scoped_ptr<ProcessMemoryDump> pmd1(new ProcessMemoryDump(nullptr));
67 auto mad1_1 = pmd1->CreateAllocatorDump("pmd1/mad1");
68 auto mad1_2 = pmd1->CreateAllocatorDump("pmd1/mad2");
69 pmd1->AddOwnershipEdge(mad1_1->guid(), mad1_2->guid());
71 scoped_ptr<ProcessMemoryDump> pmd2(new ProcessMemoryDump(nullptr));
72 auto mad2_1 = pmd2->CreateAllocatorDump("pmd2/mad1");
73 auto mad2_2 = pmd2->CreateAllocatorDump("pmd2/mad2");
74 pmd1->AddOwnershipEdge(mad2_1->guid(), mad2_2->guid());
76 MemoryAllocatorDumpGuid shared_mad_guid(1);
77 auto shared_mad = pmd2->CreateSharedGlobalAllocatorDump(shared_mad_guid);
79 pmd1->TakeAllDumpsFrom(pmd2.get());
81 // Make sure that pmd2 is empty but still usable after it has been emptied.
82 ASSERT_TRUE(pmd2->allocator_dumps().empty());
83 ASSERT_TRUE(pmd2->allocator_dumps_edges().empty());
84 pmd2->CreateAllocatorDump("pmd2/this_mad_stays_with_pmd2");
85 ASSERT_EQ(1u, pmd2->allocator_dumps().size());
86 ASSERT_EQ(1u, pmd2->allocator_dumps().count("pmd2/this_mad_stays_with_pmd2"));
87 pmd2->AddOwnershipEdge(MemoryAllocatorDumpGuid(42),
88 MemoryAllocatorDumpGuid(4242));
90 // Check that calling AsValueInto() doesn't cause a crash.
91 pmd2->AsValueInto(traced_value.get());
93 // Free the |pmd2| to check that the memory ownership of the two MAD(s)
94 // has been transferred to |pmd1|.
95 pmd2.reset();
97 // Now check that |pmd1| has been effectively merged.
98 ASSERT_EQ(5u, pmd1->allocator_dumps().size());
99 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad1"));
100 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad2"));
101 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd2/mad1"));
102 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad2"));
103 ASSERT_EQ(2u, pmd1->allocator_dumps_edges().size());
104 ASSERT_EQ(shared_mad, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid));
106 // Check that calling AsValueInto() doesn't cause a crash.
107 traced_value = new TracedValue();
108 pmd1->AsValueInto(traced_value.get());
110 pmd1.reset();
113 TEST(ProcessMemoryDumpTest, Suballocations) {
114 scoped_ptr<ProcessMemoryDump> pmd(new ProcessMemoryDump(nullptr));
115 const std::string allocator_dump_name = "fakealloc/allocated_objects";
116 pmd->CreateAllocatorDump(allocator_dump_name);
118 // Create one allocation with an auto-assigned guid and mark it as a
119 // suballocation of "fakealloc/allocated_objects".
120 auto pic1_dump = pmd->CreateAllocatorDump("picturemanager/picture1");
121 pmd->AddSuballocation(pic1_dump->guid(), allocator_dump_name);
123 // Same here, but this time create an allocation with an explicit guid.
124 auto pic2_dump = pmd->CreateAllocatorDump("picturemanager/picture2",
125 MemoryAllocatorDumpGuid(0x42));
126 pmd->AddSuballocation(pic2_dump->guid(), allocator_dump_name);
128 // Now check that AddSuballocation() has created anonymous child dumps under
129 // "fakealloc/allocated_objects".
130 auto anon_node_1_it = pmd->allocator_dumps().find(
131 allocator_dump_name + "/__" + pic1_dump->guid().ToString());
132 ASSERT_NE(pmd->allocator_dumps().end(), anon_node_1_it);
134 auto anon_node_2_it =
135 pmd->allocator_dumps().find(allocator_dump_name + "/__42");
136 ASSERT_NE(pmd->allocator_dumps().end(), anon_node_2_it);
138 // Finally check that AddSuballocation() has created also the
139 // edges between the pictures and the anonymous allocator child dumps.
140 bool found_edge[2]{false, false};
141 for (const auto& e : pmd->allocator_dumps_edges()) {
142 found_edge[0] |= (e.source == pic1_dump->guid() &&
143 e.target == anon_node_1_it->second->guid());
144 found_edge[1] |= (e.source == pic2_dump->guid() &&
145 e.target == anon_node_2_it->second->guid());
147 ASSERT_TRUE(found_edge[0]);
148 ASSERT_TRUE(found_edge[1]);
150 // Check that calling AsValueInto() doesn't cause a crash.
151 scoped_refptr<TracedValue> traced_value(new TracedValue());
152 pmd->AsValueInto(traced_value.get());
154 pmd.reset();
157 } // namespace trace_event
158 } // namespace base