Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: base/trace_event/process_memory_dump_unittest.cc

Issue 2911263003: [memory-infra] Add method to override importance of ownership edges (Closed)
Patch Set: Make it public. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/trace_event/process_memory_dump.h" 5 #include "base/trace_event/process_memory_dump.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/memory/aligned_memory.h" 9 #include "base/memory/aligned_memory.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 pmd1->AddOwnershipEdge(MemoryAllocatorDumpGuid(42), 47 pmd1->AddOwnershipEdge(MemoryAllocatorDumpGuid(42),
48 MemoryAllocatorDumpGuid(4242)); 48 MemoryAllocatorDumpGuid(4242));
49 49
50 MemoryAllocatorDumpGuid shared_mad_guid1(1); 50 MemoryAllocatorDumpGuid shared_mad_guid1(1);
51 MemoryAllocatorDumpGuid shared_mad_guid2(2); 51 MemoryAllocatorDumpGuid shared_mad_guid2(2);
52 pmd1->CreateSharedGlobalAllocatorDump(shared_mad_guid1); 52 pmd1->CreateSharedGlobalAllocatorDump(shared_mad_guid1);
53 pmd1->CreateSharedGlobalAllocatorDump(shared_mad_guid2); 53 pmd1->CreateSharedGlobalAllocatorDump(shared_mad_guid2);
54 54
55 pmd1->Clear(); 55 pmd1->Clear();
56 ASSERT_TRUE(pmd1->allocator_dumps().empty()); 56 ASSERT_TRUE(pmd1->allocator_dumps().empty());
57 ASSERT_TRUE(pmd1->allocator_dumps_edges().empty()); 57 ASSERT_TRUE(pmd1->allocator_dumps_edges_for_testing().empty());
58 ASSERT_EQ(nullptr, pmd1->GetAllocatorDump("mad1")); 58 ASSERT_EQ(nullptr, pmd1->GetAllocatorDump("mad1"));
59 ASSERT_EQ(nullptr, pmd1->GetAllocatorDump("mad2")); 59 ASSERT_EQ(nullptr, pmd1->GetAllocatorDump("mad2"));
60 ASSERT_FALSE(pmd1->has_process_totals()); 60 ASSERT_FALSE(pmd1->has_process_totals());
61 ASSERT_FALSE(pmd1->has_process_mmaps()); 61 ASSERT_FALSE(pmd1->has_process_mmaps());
62 ASSERT_TRUE(pmd1->process_mmaps()->vm_regions().empty()); 62 ASSERT_TRUE(pmd1->process_mmaps()->vm_regions().empty());
63 ASSERT_EQ(nullptr, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid1)); 63 ASSERT_EQ(nullptr, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid1));
64 ASSERT_EQ(nullptr, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid2)); 64 ASSERT_EQ(nullptr, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid2));
65 65
66 // Check that calling AsValueInto() doesn't cause a crash. 66 // Check that calling AsValueInto() doesn't cause a crash.
67 std::unique_ptr<TracedValue> traced_value(new TracedValue); 67 std::unique_ptr<TracedValue> traced_value(new TracedValue);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 MemoryAllocatorDumpGuid shared_mad_guid1(1); 119 MemoryAllocatorDumpGuid shared_mad_guid1(1);
120 MemoryAllocatorDumpGuid shared_mad_guid2(2); 120 MemoryAllocatorDumpGuid shared_mad_guid2(2);
121 auto* shared_mad1 = pmd2->CreateSharedGlobalAllocatorDump(shared_mad_guid1); 121 auto* shared_mad1 = pmd2->CreateSharedGlobalAllocatorDump(shared_mad_guid1);
122 auto* shared_mad2 = 122 auto* shared_mad2 =
123 pmd2->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid2); 123 pmd2->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid2);
124 124
125 pmd1->TakeAllDumpsFrom(pmd2.get()); 125 pmd1->TakeAllDumpsFrom(pmd2.get());
126 126
127 // Make sure that pmd2 is empty but still usable after it has been emptied. 127 // Make sure that pmd2 is empty but still usable after it has been emptied.
128 ASSERT_TRUE(pmd2->allocator_dumps().empty()); 128 ASSERT_TRUE(pmd2->allocator_dumps().empty());
129 ASSERT_TRUE(pmd2->allocator_dumps_edges().empty()); 129 ASSERT_TRUE(pmd2->allocator_dumps_edges_for_testing().empty());
130 ASSERT_TRUE(pmd2->heap_dumps().empty()); 130 ASSERT_TRUE(pmd2->heap_dumps().empty());
131 pmd2->CreateAllocatorDump("pmd2/this_mad_stays_with_pmd2"); 131 pmd2->CreateAllocatorDump("pmd2/this_mad_stays_with_pmd2");
132 ASSERT_EQ(1u, pmd2->allocator_dumps().size()); 132 ASSERT_EQ(1u, pmd2->allocator_dumps().size());
133 ASSERT_EQ(1u, pmd2->allocator_dumps().count("pmd2/this_mad_stays_with_pmd2")); 133 ASSERT_EQ(1u, pmd2->allocator_dumps().count("pmd2/this_mad_stays_with_pmd2"));
134 pmd2->AddOwnershipEdge(MemoryAllocatorDumpGuid(42), 134 pmd2->AddOwnershipEdge(MemoryAllocatorDumpGuid(42),
135 MemoryAllocatorDumpGuid(4242)); 135 MemoryAllocatorDumpGuid(4242));
136 136
137 // Check that calling AsValueInto() doesn't cause a crash. 137 // Check that calling AsValueInto() doesn't cause a crash.
138 pmd2->AsValueInto(traced_value.get()); 138 pmd2->AsValueInto(traced_value.get());
139 139
140 // Free the |pmd2| to check that the memory ownership of the two MAD(s) 140 // Free the |pmd2| to check that the memory ownership of the two MAD(s)
141 // has been transferred to |pmd1|. 141 // has been transferred to |pmd1|.
142 pmd2.reset(); 142 pmd2.reset();
143 143
144 // Now check that |pmd1| has been effectively merged. 144 // Now check that |pmd1| has been effectively merged.
145 ASSERT_EQ(6u, pmd1->allocator_dumps().size()); 145 ASSERT_EQ(6u, pmd1->allocator_dumps().size());
146 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad1")); 146 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad1"));
147 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad2")); 147 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad2"));
148 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd2/mad1")); 148 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd2/mad1"));
149 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad2")); 149 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad2"));
150 ASSERT_EQ(2u, pmd1->allocator_dumps_edges().size()); 150 ASSERT_EQ(2u, pmd1->allocator_dumps_edges_for_testing().size());
151 ASSERT_EQ(shared_mad1, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid1)); 151 ASSERT_EQ(shared_mad1, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid1));
152 ASSERT_EQ(shared_mad2, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid2)); 152 ASSERT_EQ(shared_mad2, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid2));
153 ASSERT_TRUE(MemoryAllocatorDump::Flags::WEAK & shared_mad2->flags()); 153 ASSERT_TRUE(MemoryAllocatorDump::Flags::WEAK & shared_mad2->flags());
154 ASSERT_EQ(4u, pmd1->heap_dumps().size()); 154 ASSERT_EQ(4u, pmd1->heap_dumps().size());
155 ASSERT_TRUE(GetHeapDump(*pmd1, "pmd1/heap_dump1") != nullptr); 155 ASSERT_TRUE(GetHeapDump(*pmd1, "pmd1/heap_dump1") != nullptr);
156 ASSERT_TRUE(GetHeapDump(*pmd1, "pmd1/heap_dump2") != nullptr); 156 ASSERT_TRUE(GetHeapDump(*pmd1, "pmd1/heap_dump2") != nullptr);
157 ASSERT_TRUE(GetHeapDump(*pmd1, "pmd2/heap_dump1") != nullptr); 157 ASSERT_TRUE(GetHeapDump(*pmd1, "pmd2/heap_dump1") != nullptr);
158 ASSERT_TRUE(GetHeapDump(*pmd1, "pmd2/heap_dump2") != nullptr); 158 ASSERT_TRUE(GetHeapDump(*pmd1, "pmd2/heap_dump2") != nullptr);
159 159
160 // Check that calling AsValueInto() doesn't cause a crash. 160 // Check that calling AsValueInto() doesn't cause a crash.
161 traced_value.reset(new TracedValue); 161 traced_value.reset(new TracedValue);
162 pmd1->AsValueInto(traced_value.get()); 162 pmd1->AsValueInto(traced_value.get());
163 163
164 pmd1.reset(); 164 pmd1.reset();
165 } 165 }
166 166
167 TEST(ProcessMemoryDumpTest, OverrideOwnershipEdge) {
168 std::unique_ptr<ProcessMemoryDump> pmd(
169 new ProcessMemoryDump(nullptr, kDetailedDumpArgs));
170
171 auto* shm_dump1 = pmd->CreateAllocatorDump("shared_mem/seg1");
172 auto* shm_dump2 = pmd->CreateAllocatorDump("shared_mem/seg2");
173 auto* shm_dump3 = pmd->CreateAllocatorDump("shared_mem/seg3");
174
175 // Create one allocation with an auto-assigned guid and mark it as a
176 // suballocation of "fakealloc/allocated_objects".
177 auto* child1_dump = pmd->CreateAllocatorDump("shared_mem/child/seg1");
178 pmd->AddOverridableOwnershipEdge(child1_dump->guid(), shm_dump1->guid(),
179 0 /* importance */);
180 auto* child2_dump = pmd->CreateAllocatorDump("shared_mem/child/seg2");
181 pmd->AddOwnershipEdge(child2_dump->guid(), shm_dump2->guid(),
182 3 /* importance */);
183 MemoryAllocatorDumpGuid shared_mad_guid(1);
184 pmd->CreateSharedGlobalAllocatorDump(shared_mad_guid);
185 pmd->AddOverridableOwnershipEdge(shm_dump3->guid(), shared_mad_guid,
186 0 /* importance */);
187
188 const ProcessMemoryDump::AllocatorDumpEdgesMap& edges =
189 pmd->allocator_dumps_edges_for_testing();
190 EXPECT_EQ(3u, edges.size());
191 EXPECT_EQ(shm_dump1->guid(), edges.find(child1_dump->guid())->second.target);
192 EXPECT_EQ(0, edges.find(child1_dump->guid())->second.importance);
193 EXPECT_TRUE(edges.find(child1_dump->guid())->second.overridable);
194 EXPECT_EQ(shm_dump2->guid(), edges.find(child2_dump->guid())->second.target);
195 EXPECT_EQ(3, edges.find(child2_dump->guid())->second.importance);
196 EXPECT_FALSE(edges.find(child2_dump->guid())->second.overridable);
197 EXPECT_EQ(shared_mad_guid, edges.find(shm_dump3->guid())->second.target);
198 EXPECT_EQ(0, edges.find(shm_dump3->guid())->second.importance);
199 EXPECT_TRUE(edges.find(shm_dump3->guid())->second.overridable);
200
201 // These should override old edges:
202 pmd->AddOwnershipEdge(child1_dump->guid(), shm_dump1->guid(),
203 1 /* importance */);
204 pmd->AddOwnershipEdge(shm_dump3->guid(), shared_mad_guid, 2 /* importance */);
205 // This should not change the old edges.
206 pmd->AddOverridableOwnershipEdge(child2_dump->guid(), shm_dump2->guid(),
Primiano Tucci (use gerrit) 2017/06/06 18:50:50 can you have some coverage for the overridable bei
ssid 2017/06/07 01:16:32 Done.
207 0 /* importance */);
208
209 EXPECT_EQ(3u, edges.size());
210 EXPECT_EQ(shm_dump1->guid(), edges.find(child1_dump->guid())->second.target);
211 EXPECT_EQ(1, edges.find(child1_dump->guid())->second.importance);
212 EXPECT_FALSE(edges.find(child1_dump->guid())->second.overridable);
213 EXPECT_EQ(shm_dump2->guid(), edges.find(child2_dump->guid())->second.target);
214 EXPECT_EQ(3, edges.find(child2_dump->guid())->second.importance);
215 EXPECT_FALSE(edges.find(child2_dump->guid())->second.overridable);
216 EXPECT_EQ(shared_mad_guid, edges.find(shm_dump3->guid())->second.target);
217 EXPECT_EQ(2, edges.find(shm_dump3->guid())->second.importance);
218 EXPECT_FALSE(edges.find(shm_dump3->guid())->second.overridable);
219 }
220
167 TEST(ProcessMemoryDumpTest, Suballocations) { 221 TEST(ProcessMemoryDumpTest, Suballocations) {
168 std::unique_ptr<ProcessMemoryDump> pmd( 222 std::unique_ptr<ProcessMemoryDump> pmd(
169 new ProcessMemoryDump(nullptr, kDetailedDumpArgs)); 223 new ProcessMemoryDump(nullptr, kDetailedDumpArgs));
170 const std::string allocator_dump_name = "fakealloc/allocated_objects"; 224 const std::string allocator_dump_name = "fakealloc/allocated_objects";
171 pmd->CreateAllocatorDump(allocator_dump_name); 225 pmd->CreateAllocatorDump(allocator_dump_name);
172 226
173 // Create one allocation with an auto-assigned guid and mark it as a 227 // Create one allocation with an auto-assigned guid and mark it as a
174 // suballocation of "fakealloc/allocated_objects". 228 // suballocation of "fakealloc/allocated_objects".
175 auto* pic1_dump = pmd->CreateAllocatorDump("picturemanager/picture1"); 229 auto* pic1_dump = pmd->CreateAllocatorDump("picturemanager/picture1");
176 pmd->AddSuballocation(pic1_dump->guid(), allocator_dump_name); 230 pmd->AddSuballocation(pic1_dump->guid(), allocator_dump_name);
177 231
178 // Same here, but this time create an allocation with an explicit guid. 232 // Same here, but this time create an allocation with an explicit guid.
179 auto* pic2_dump = pmd->CreateAllocatorDump("picturemanager/picture2", 233 auto* pic2_dump = pmd->CreateAllocatorDump("picturemanager/picture2",
180 MemoryAllocatorDumpGuid(0x42)); 234 MemoryAllocatorDumpGuid(0x42));
181 pmd->AddSuballocation(pic2_dump->guid(), allocator_dump_name); 235 pmd->AddSuballocation(pic2_dump->guid(), allocator_dump_name);
182 236
183 // Now check that AddSuballocation() has created anonymous child dumps under 237 // Now check that AddSuballocation() has created anonymous child dumps under
184 // "fakealloc/allocated_objects". 238 // "fakealloc/allocated_objects".
185 auto anon_node_1_it = pmd->allocator_dumps().find( 239 auto anon_node_1_it = pmd->allocator_dumps().find(
186 allocator_dump_name + "/__" + pic1_dump->guid().ToString()); 240 allocator_dump_name + "/__" + pic1_dump->guid().ToString());
187 ASSERT_NE(pmd->allocator_dumps().end(), anon_node_1_it); 241 ASSERT_NE(pmd->allocator_dumps().end(), anon_node_1_it);
188 242
189 auto anon_node_2_it = 243 auto anon_node_2_it =
190 pmd->allocator_dumps().find(allocator_dump_name + "/__42"); 244 pmd->allocator_dumps().find(allocator_dump_name + "/__42");
191 ASSERT_NE(pmd->allocator_dumps().end(), anon_node_2_it); 245 ASSERT_NE(pmd->allocator_dumps().end(), anon_node_2_it);
192 246
193 // Finally check that AddSuballocation() has created also the 247 // Finally check that AddSuballocation() has created also the
194 // edges between the pictures and the anonymous allocator child dumps. 248 // edges between the pictures and the anonymous allocator child dumps.
195 bool found_edge[2]{false, false}; 249 bool found_edge[2]{false, false};
196 for (const auto& e : pmd->allocator_dumps_edges()) { 250 for (const auto& e : pmd->allocator_dumps_edges_for_testing()) {
197 found_edge[0] |= (e.source == pic1_dump->guid() && 251 found_edge[0] |= (e.first == pic1_dump->guid() &&
198 e.target == anon_node_1_it->second->guid()); 252 e.second.target == anon_node_1_it->second->guid());
199 found_edge[1] |= (e.source == pic2_dump->guid() && 253 found_edge[1] |= (e.first == pic2_dump->guid() &&
200 e.target == anon_node_2_it->second->guid()); 254 e.second.target == anon_node_2_it->second->guid());
201 } 255 }
202 ASSERT_TRUE(found_edge[0]); 256 ASSERT_TRUE(found_edge[0]);
203 ASSERT_TRUE(found_edge[1]); 257 ASSERT_TRUE(found_edge[1]);
204 258
205 // Check that calling AsValueInto() doesn't cause a crash. 259 // Check that calling AsValueInto() doesn't cause a crash.
206 std::unique_ptr<TracedValue> traced_value(new TracedValue); 260 std::unique_ptr<TracedValue> traced_value(new TracedValue);
207 pmd->AsValueInto(traced_value.get()); 261 pmd->AsValueInto(traced_value.get());
208 262
209 pmd.reset(); 263 pmd.reset();
210 } 264 }
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 static_cast<char*>(base::AlignedAlloc(kVeryLargeMemorySize, page_size))); 351 static_cast<char*>(base::AlignedAlloc(kVeryLargeMemorySize, page_size)));
298 memset(memory2.get(), 0, kVeryLargeMemorySize); 352 memset(memory2.get(), 0, kVeryLargeMemorySize);
299 size_t res2 = ProcessMemoryDump::CountResidentBytes(memory2.get(), 353 size_t res2 = ProcessMemoryDump::CountResidentBytes(memory2.get(),
300 kVeryLargeMemorySize); 354 kVeryLargeMemorySize);
301 ASSERT_EQ(res2, kVeryLargeMemorySize); 355 ASSERT_EQ(res2, kVeryLargeMemorySize);
302 } 356 }
303 #endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED) 357 #endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED)
304 358
305 } // namespace trace_event 359 } // namespace trace_event
306 } // namespace base 360 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698