OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "platform/web_process_memory_dump_impl.h" | |
6 | |
7 #include "base/memory/discardable_memory.h" | |
8 #include "base/test/test_discardable_memory_allocator.h" | |
9 #include "base/trace_event/memory_allocator_dump.h" | |
10 #include "base/trace_event/process_memory_dump.h" | |
11 #include "base/trace_event/trace_event_argument.h" | |
12 #include "base/values.h" | |
13 #include "platform/web_memory_allocator_dump_impl.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 #include "wtf/OwnPtr.h" | |
16 | |
17 namespace blink { | |
18 | |
19 // Tests that the Chromium<>Blink plumbing that exposes the MemoryInfra classes | |
20 // behaves correctly, performs the right transfers of memory ownerships and | |
21 // doesn't leak objects. | |
22 TEST(WebProcessMemoryDumpImplTest, IntegrationTest) { | |
23 std::unique_ptr<base::trace_event::TracedValue> traced_value( | |
24 new base::trace_event::TracedValue()); | |
25 | |
26 std::unique_ptr<WebProcessMemoryDumpImpl> wpmd1(new WebProcessMemoryDumpImpl()
); | |
27 auto wmad1 = wpmd1->createMemoryAllocatorDump("1/1"); | |
28 auto wmad2 = wpmd1->createMemoryAllocatorDump("1/2"); | |
29 ASSERT_EQ(wmad1, wpmd1->getMemoryAllocatorDump("1/1")); | |
30 ASSERT_EQ(wmad2, wpmd1->getMemoryAllocatorDump("1/2")); | |
31 | |
32 std::unique_ptr<WebProcessMemoryDumpImpl> wpmd2(new WebProcessMemoryDumpImpl()
); | |
33 wpmd2->createMemoryAllocatorDump("2/1"); | |
34 wpmd2->createMemoryAllocatorDump("2/2"); | |
35 | |
36 wpmd1->takeAllDumpsFrom(wpmd2.get()); | |
37 | |
38 // Make sure that wpmd2 still owns its own PMD, even if empty. | |
39 ASSERT_NE(static_cast<base::trace_event::ProcessMemoryDump*>(nullptr), | |
40 wpmd2->process_memory_dump_); | |
41 ASSERT_EQ(wpmd2->owned_process_memory_dump_.get(), | |
42 wpmd2->process_memory_dump()); | |
43 ASSERT_TRUE(wpmd2->process_memory_dump()->allocator_dumps().empty()); | |
44 | |
45 // Make sure that wpmd2 is still usable after it has been emptied. | |
46 auto wmad = wpmd2->createMemoryAllocatorDump("2/new"); | |
47 wmad->addScalar("attr_name", "bytes", 42); | |
48 wmad->addScalarF("attr_name_2", "rate", 42.0f); | |
49 ASSERT_EQ(1u, wpmd2->process_memory_dump()->allocator_dumps().size()); | |
50 auto mad = wpmd2->process_memory_dump()->GetAllocatorDump("2/new"); | |
51 ASSERT_NE(static_cast<base::trace_event::MemoryAllocatorDump*>(nullptr), mad); | |
52 ASSERT_EQ(wmad, wpmd2->getMemoryAllocatorDump("2/new")); | |
53 | |
54 // Check that the attributes are propagated correctly. | |
55 auto raw_attrs = mad->attributes_for_testing()->ToBaseValue(); | |
56 base::DictionaryValue* attrs = nullptr; | |
57 ASSERT_TRUE(raw_attrs->GetAsDictionary(&attrs)); | |
58 base::DictionaryValue* attr = nullptr; | |
59 ASSERT_TRUE(attrs->GetDictionary("attr_name", &attr)); | |
60 std::string attr_value; | |
61 ASSERT_TRUE(attr->GetString("type", &attr_value)); | |
62 ASSERT_EQ(base::trace_event::MemoryAllocatorDump::kTypeScalar, attr_value); | |
63 ASSERT_TRUE(attr->GetString("units", &attr_value)); | |
64 ASSERT_EQ("bytes", attr_value); | |
65 | |
66 ASSERT_TRUE(attrs->GetDictionary("attr_name_2", &attr)); | |
67 ASSERT_TRUE(attr->GetString("type", &attr_value)); | |
68 ASSERT_EQ(base::trace_event::MemoryAllocatorDump::kTypeScalar, attr_value); | |
69 ASSERT_TRUE(attr->GetString("units", &attr_value)); | |
70 ASSERT_EQ("rate", attr_value); | |
71 ASSERT_TRUE(attr->HasKey("value")); | |
72 | |
73 // Check that AsValueInto() doesn't cause a crash. | |
74 wpmd2->process_memory_dump()->AsValueInto(traced_value.get()); | |
75 | |
76 // Free the |wpmd2| to check that the memory ownership of the two MAD(s) | |
77 // has been transferred to |wpmd1|. | |
78 wpmd2.reset(); | |
79 | |
80 // Now check that |wpmd1| has been effectively merged. | |
81 ASSERT_EQ(4u, wpmd1->process_memory_dump()->allocator_dumps().size()); | |
82 ASSERT_EQ(1u, wpmd1->process_memory_dump()->allocator_dumps().count("1/1")); | |
83 ASSERT_EQ(1u, wpmd1->process_memory_dump()->allocator_dumps().count("1/2")); | |
84 ASSERT_EQ(1u, wpmd1->process_memory_dump()->allocator_dumps().count("2/1")); | |
85 ASSERT_EQ(1u, wpmd1->process_memory_dump()->allocator_dumps().count("1/2")); | |
86 | |
87 // Check that also the WMAD wrappers got merged. | |
88 blink::WebMemoryAllocatorDump* null_wmad = nullptr; | |
89 ASSERT_NE(null_wmad, wpmd1->getMemoryAllocatorDump("1/1")); | |
90 ASSERT_NE(null_wmad, wpmd1->getMemoryAllocatorDump("1/2")); | |
91 ASSERT_NE(null_wmad, wpmd1->getMemoryAllocatorDump("2/1")); | |
92 ASSERT_NE(null_wmad, wpmd1->getMemoryAllocatorDump("2/2")); | |
93 | |
94 // Check that AsValueInto() doesn't cause a crash. | |
95 traced_value.reset(new base::trace_event::TracedValue); | |
96 wpmd1->process_memory_dump()->AsValueInto(traced_value.get()); | |
97 | |
98 // Check that clear() actually works. | |
99 wpmd1->clear(); | |
100 ASSERT_TRUE(wpmd1->process_memory_dump()->allocator_dumps().empty()); | |
101 ASSERT_EQ(nullptr, wpmd1->process_memory_dump()->GetAllocatorDump("1/1")); | |
102 ASSERT_EQ(nullptr, wpmd1->process_memory_dump()->GetAllocatorDump("2/1")); | |
103 | |
104 // Check that AsValueInto() doesn't cause a crash. | |
105 traced_value.reset(new base::trace_event::TracedValue); | |
106 wpmd1->process_memory_dump()->AsValueInto(traced_value.get()); | |
107 | |
108 // Check if a WebMemoryAllocatorDump created with guid, has correct guid. | |
109 blink::WebMemoryAllocatorDumpGuid guid = | |
110 base::trace_event::MemoryAllocatorDumpGuid("id_1").ToUint64(); | |
111 auto wmad3 = wpmd1->createMemoryAllocatorDump("1/3", guid); | |
112 ASSERT_EQ(wmad3->guid(), guid); | |
113 ASSERT_EQ(wmad3, wpmd1->getMemoryAllocatorDump("1/3")); | |
114 | |
115 // Check that AddOwnershipEdge is propagated correctly. | |
116 auto wmad4 = wpmd1->createMemoryAllocatorDump("1/4"); | |
117 wpmd1->addOwnershipEdge(wmad4->guid(), guid); | |
118 auto allocator_dumps_edges = | |
119 wpmd1->process_memory_dump()->allocator_dumps_edges(); | |
120 ASSERT_EQ(1u, allocator_dumps_edges.size()); | |
121 ASSERT_EQ(wmad4->guid(), allocator_dumps_edges[0].source.ToUint64()); | |
122 ASSERT_EQ(guid, allocator_dumps_edges[0].target.ToUint64()); | |
123 | |
124 // Check that createDumpAdapterForSkia() works. | |
125 auto skia_trace_memory_dump = wpmd1->createDumpAdapterForSkia("1/skia"); | |
126 ASSERT_TRUE(skia_trace_memory_dump); | |
127 | |
128 // Check that createDiscardableMemoryAllocatorDump() works. | |
129 base::TestDiscardableMemoryAllocator discardable_memory_allocator; | |
130 auto discardable_memory = | |
131 discardable_memory_allocator.AllocateLockedDiscardableMemory(1024); | |
132 wpmd1->createDiscardableMemoryAllocatorDump("1/discardable", | |
133 discardable_memory.get()); | |
134 discardable_memory->Unlock(); | |
135 | |
136 wpmd1.reset(); | |
137 } | |
138 | |
139 } // namespace blink | |
OLD | NEW |