OLD | NEW |
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/memory_allocator_dump.h" | 5 #include "base/trace_event/memory_allocator_dump.h" |
6 | 6 |
7 #include "base/trace_event/memory_dump_provider.h" | 7 #include "base/trace_event/memory_dump_provider.h" |
8 #include "base/trace_event/memory_dump_session_state.h" | 8 #include "base/trace_event/memory_dump_session_state.h" |
9 #include "base/trace_event/process_memory_dump.h" | 9 #include "base/trace_event/process_memory_dump.h" |
10 #include "base/trace_event/trace_event_argument.h" | 10 #include "base/trace_event/trace_event_argument.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace base { | 13 namespace base { |
14 namespace trace_event { | 14 namespace trace_event { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 class FakeMemoryAllocatorDumpProvider : public MemoryDumpProvider { | 18 class FakeMemoryAllocatorDumpProvider : public MemoryDumpProvider { |
19 public: | 19 public: |
20 FakeMemoryAllocatorDumpProvider() { | 20 bool OnMemoryDump(ProcessMemoryDump* pmd) override { |
21 DeclareAllocatorAttribute("foobar_allocator", "attr1", "count"); | |
22 DeclareAllocatorAttribute("foobar_allocator", "attr2", "bytes"); | |
23 } | |
24 | |
25 bool DumpInto(ProcessMemoryDump* pmd) override { | |
26 MemoryAllocatorDump* root_heap = pmd->CreateAllocatorDump( | 21 MemoryAllocatorDump* root_heap = pmd->CreateAllocatorDump( |
27 "foobar_allocator", MemoryAllocatorDump::kRootHeap); | 22 "foobar_allocator", MemoryAllocatorDump::kRootHeap); |
28 root_heap->set_physical_size_in_bytes(4096); | 23 root_heap->set_physical_size_in_bytes(4096); |
29 root_heap->set_allocated_objects_count(42); | 24 root_heap->set_allocated_objects_count(42); |
30 root_heap->set_allocated_objects_size_in_bytes(1000); | 25 root_heap->set_allocated_objects_size_in_bytes(1000); |
31 root_heap->SetAttribute("attr1", 1234); | |
32 root_heap->SetAttribute("attr2", 99); | |
33 | 26 |
34 MemoryAllocatorDump* sub_heap = | 27 MemoryAllocatorDump* sub_heap = |
35 pmd->CreateAllocatorDump("foobar_allocator", "sub_heap"); | 28 pmd->CreateAllocatorDump("foobar_allocator", "sub_heap"); |
36 sub_heap->set_physical_size_in_bytes(1); | 29 sub_heap->set_physical_size_in_bytes(1); |
37 sub_heap->set_allocated_objects_count(2); | 30 sub_heap->set_allocated_objects_count(2); |
38 sub_heap->set_allocated_objects_size_in_bytes(3); | 31 sub_heap->set_allocated_objects_size_in_bytes(3); |
39 | 32 |
40 pmd->CreateAllocatorDump("foobar_allocator", "sub_heap/empty"); | 33 pmd->CreateAllocatorDump("foobar_allocator", "sub_heap/empty"); |
41 // Leave the rest of sub heap deliberately uninitialized, to check that | 34 // Leave the rest of sub heap deliberately uninitialized, to check that |
42 // CreateAllocatorDump returns a properly zero-initialized object. | 35 // CreateAllocatorDump returns a properly zero-initialized object. |
43 | 36 |
44 return true; | 37 return true; |
45 } | 38 } |
46 | |
47 const char* GetFriendlyName() const override { return "FooBar Allocator"; } | |
48 }; | 39 }; |
49 } // namespace | 40 } // namespace |
50 | 41 |
51 TEST(MemoryAllocatorDumpTest, DumpIntoProcessMemoryDump) { | 42 TEST(MemoryAllocatorDumpTest, DumpIntoProcessMemoryDump) { |
52 FakeMemoryAllocatorDumpProvider fmadp; | 43 FakeMemoryAllocatorDumpProvider fmadp; |
53 ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState())); | 44 ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState())); |
54 pmd.session_state()->allocators_attributes_type_info.Update( | |
55 fmadp.allocator_attributes_type_info()); | |
56 | 45 |
57 fmadp.DumpInto(&pmd); | 46 fmadp.OnMemoryDump(&pmd); |
58 | 47 |
59 ASSERT_EQ(3u, pmd.allocator_dumps().size()); | 48 ASSERT_EQ(3u, pmd.allocator_dumps().size()); |
60 | 49 |
61 const MemoryAllocatorDump* root_heap = | 50 const MemoryAllocatorDump* root_heap = |
62 pmd.GetAllocatorDump("foobar_allocator", MemoryAllocatorDump::kRootHeap); | 51 pmd.GetAllocatorDump("foobar_allocator", MemoryAllocatorDump::kRootHeap); |
63 ASSERT_NE(nullptr, root_heap); | 52 ASSERT_NE(nullptr, root_heap); |
64 EXPECT_EQ("foobar_allocator", root_heap->allocator_name()); | 53 EXPECT_EQ("foobar_allocator", root_heap->allocator_name()); |
65 EXPECT_EQ("", root_heap->heap_name()); | 54 EXPECT_EQ("", root_heap->heap_name()); |
66 EXPECT_NE("", root_heap->GetAbsoluteName()); | 55 EXPECT_NE("", root_heap->GetAbsoluteName()); |
67 EXPECT_EQ(4096u, root_heap->physical_size_in_bytes()); | 56 EXPECT_EQ(4096u, root_heap->physical_size_in_bytes()); |
68 EXPECT_EQ(42u, root_heap->allocated_objects_count()); | 57 EXPECT_EQ(42u, root_heap->allocated_objects_count()); |
69 EXPECT_EQ(1000u, root_heap->allocated_objects_size_in_bytes()); | 58 EXPECT_EQ(1000u, root_heap->allocated_objects_size_in_bytes()); |
70 | 59 |
71 // Check the extra attributes of |root_heap|. | |
72 EXPECT_EQ(1234, root_heap->GetIntegerAttribute("attr1")); | |
73 EXPECT_EQ(99, root_heap->GetIntegerAttribute("attr2")); | |
74 | |
75 const MemoryAllocatorDump* sub_heap = | 60 const MemoryAllocatorDump* sub_heap = |
76 pmd.GetAllocatorDump("foobar_allocator", "sub_heap"); | 61 pmd.GetAllocatorDump("foobar_allocator", "sub_heap"); |
77 ASSERT_NE(nullptr, sub_heap); | 62 ASSERT_NE(nullptr, sub_heap); |
78 EXPECT_EQ("foobar_allocator", sub_heap->allocator_name()); | 63 EXPECT_EQ("foobar_allocator", sub_heap->allocator_name()); |
79 EXPECT_EQ("sub_heap", sub_heap->heap_name()); | 64 EXPECT_EQ("sub_heap", sub_heap->heap_name()); |
80 EXPECT_NE("", sub_heap->GetAbsoluteName()); | 65 EXPECT_NE("", sub_heap->GetAbsoluteName()); |
81 EXPECT_EQ(1u, sub_heap->physical_size_in_bytes()); | 66 EXPECT_EQ(1u, sub_heap->physical_size_in_bytes()); |
82 EXPECT_EQ(2u, sub_heap->allocated_objects_count()); | 67 EXPECT_EQ(2u, sub_heap->allocated_objects_count()); |
83 EXPECT_EQ(3u, sub_heap->allocated_objects_size_in_bytes()); | 68 EXPECT_EQ(3u, sub_heap->allocated_objects_size_in_bytes()); |
84 | 69 |
(...skipping 22 matching lines...) Expand all Loading... |
107 ASSERT_DEATH( | 92 ASSERT_DEATH( |
108 pmd.CreateAllocatorDump("foo_allocator", MemoryAllocatorDump::kRootHeap), | 93 pmd.CreateAllocatorDump("foo_allocator", MemoryAllocatorDump::kRootHeap), |
109 ""); | 94 ""); |
110 ASSERT_DEATH(pmd.CreateAllocatorDump("bar_allocator", "heap"), ""); | 95 ASSERT_DEATH(pmd.CreateAllocatorDump("bar_allocator", "heap"), ""); |
111 ASSERT_DEATH(pmd.CreateAllocatorDump("", "must_have_allocator_name"), ""); | 96 ASSERT_DEATH(pmd.CreateAllocatorDump("", "must_have_allocator_name"), ""); |
112 } | 97 } |
113 #endif | 98 #endif |
114 | 99 |
115 } // namespace trace_event | 100 } // namespace trace_event |
116 } // namespace base | 101 } // namespace base |
OLD | NEW |