| 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/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/trace_event/memory_allocator_dump_guid.h" | 9 #include "base/trace_event/memory_allocator_dump_guid.h" |
| 10 #include "base/trace_event/memory_dump_provider.h" | 10 #include "base/trace_event/memory_dump_provider.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 | 21 |
| 22 class FakeMemoryAllocatorDumpProvider : public MemoryDumpProvider { | 22 class FakeMemoryAllocatorDumpProvider : public MemoryDumpProvider { |
| 23 public: | 23 public: |
| 24 bool OnMemoryDump(const MemoryDumpArgs& args, | 24 bool OnMemoryDump(const MemoryDumpArgs& args, |
| 25 ProcessMemoryDump* pmd) override { | 25 ProcessMemoryDump* pmd) override { |
| 26 MemoryAllocatorDump* root_heap = | 26 MemoryAllocatorDump* root_heap = |
| 27 pmd->CreateAllocatorDump("foobar_allocator"); | 27 pmd->CreateAllocatorDump("foobar_allocator"); |
| 28 | 28 |
| 29 root_heap->AddScalar(MemoryAllocatorDump::kNameSize, | 29 root_heap->AddScalar(MemoryAllocatorDump::kNameSize, |
| 30 MemoryAllocatorDump::kUnitsBytes, 4096); | 30 MemoryAllocatorDump::kUnitsBytes, 4096); |
| 31 root_heap->AddScalar(MemoryAllocatorDump::kNameObjectsCount, | 31 root_heap->AddScalar(MemoryAllocatorDump::kNameObjectCount, |
| 32 MemoryAllocatorDump::kUnitsObjects, 42); | 32 MemoryAllocatorDump::kUnitsObjects, 42); |
| 33 root_heap->AddScalar("attr1", "units1", 1234); | 33 root_heap->AddScalar("attr1", "units1", 1234); |
| 34 root_heap->AddString("attr2", "units2", "string_value"); | 34 root_heap->AddString("attr2", "units2", "string_value"); |
| 35 root_heap->AddScalarF("attr3", "units3", 42.5f); | 35 root_heap->AddScalarF("attr3", "units3", 42.5f); |
| 36 | 36 |
| 37 MemoryAllocatorDump* sub_heap = | 37 MemoryAllocatorDump* sub_heap = |
| 38 pmd->CreateAllocatorDump("foobar_allocator/sub_heap"); | 38 pmd->CreateAllocatorDump("foobar_allocator/sub_heap"); |
| 39 sub_heap->AddScalar(MemoryAllocatorDump::kNameSize, | 39 sub_heap->AddScalar(MemoryAllocatorDump::kNameSize, |
| 40 MemoryAllocatorDump::kUnitsBytes, 1); | 40 MemoryAllocatorDump::kUnitsBytes, 1); |
| 41 sub_heap->AddScalar(MemoryAllocatorDump::kNameObjectsCount, | 41 sub_heap->AddScalar(MemoryAllocatorDump::kNameObjectCount, |
| 42 MemoryAllocatorDump::kUnitsObjects, 3); | 42 MemoryAllocatorDump::kUnitsObjects, 3); |
| 43 | 43 |
| 44 pmd->CreateAllocatorDump("foobar_allocator/sub_heap/empty"); | 44 pmd->CreateAllocatorDump("foobar_allocator/sub_heap/empty"); |
| 45 // Leave the rest of sub heap deliberately uninitialized, to check that | 45 // Leave the rest of sub heap deliberately uninitialized, to check that |
| 46 // CreateAllocatorDump returns a properly zero-initialized object. | 46 // CreateAllocatorDump returns a properly zero-initialized object. |
| 47 | 47 |
| 48 return true; | 48 return true; |
| 49 } | 49 } |
| 50 }; | 50 }; |
| 51 | 51 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 fmadp.OnMemoryDump(dump_args, &pmd); | 131 fmadp.OnMemoryDump(dump_args, &pmd); |
| 132 | 132 |
| 133 ASSERT_EQ(3u, pmd.allocator_dumps().size()); | 133 ASSERT_EQ(3u, pmd.allocator_dumps().size()); |
| 134 | 134 |
| 135 const MemoryAllocatorDump* root_heap = | 135 const MemoryAllocatorDump* root_heap = |
| 136 pmd.GetAllocatorDump("foobar_allocator"); | 136 pmd.GetAllocatorDump("foobar_allocator"); |
| 137 ASSERT_NE(nullptr, root_heap); | 137 ASSERT_NE(nullptr, root_heap); |
| 138 EXPECT_EQ("foobar_allocator", root_heap->absolute_name()); | 138 EXPECT_EQ("foobar_allocator", root_heap->absolute_name()); |
| 139 CheckScalar(root_heap, MemoryAllocatorDump::kNameSize, | 139 CheckScalar(root_heap, MemoryAllocatorDump::kNameSize, |
| 140 MemoryAllocatorDump::kUnitsBytes, 4096); | 140 MemoryAllocatorDump::kUnitsBytes, 4096); |
| 141 CheckScalar(root_heap, MemoryAllocatorDump::kNameObjectsCount, | 141 CheckScalar(root_heap, MemoryAllocatorDump::kNameObjectCount, |
| 142 MemoryAllocatorDump::kUnitsObjects, 42); | 142 MemoryAllocatorDump::kUnitsObjects, 42); |
| 143 CheckScalar(root_heap, "attr1", "units1", 1234); | 143 CheckScalar(root_heap, "attr1", "units1", 1234); |
| 144 CheckString(root_heap, "attr2", MemoryAllocatorDump::kTypeString, "units2", | 144 CheckString(root_heap, "attr2", MemoryAllocatorDump::kTypeString, "units2", |
| 145 "string_value"); | 145 "string_value"); |
| 146 CheckScalarF(root_heap, "attr3", "units3", 42.5f); | 146 CheckScalarF(root_heap, "attr3", "units3", 42.5f); |
| 147 | 147 |
| 148 const MemoryAllocatorDump* sub_heap = | 148 const MemoryAllocatorDump* sub_heap = |
| 149 pmd.GetAllocatorDump("foobar_allocator/sub_heap"); | 149 pmd.GetAllocatorDump("foobar_allocator/sub_heap"); |
| 150 ASSERT_NE(nullptr, sub_heap); | 150 ASSERT_NE(nullptr, sub_heap); |
| 151 EXPECT_EQ("foobar_allocator/sub_heap", sub_heap->absolute_name()); | 151 EXPECT_EQ("foobar_allocator/sub_heap", sub_heap->absolute_name()); |
| 152 CheckScalar(sub_heap, MemoryAllocatorDump::kNameSize, | 152 CheckScalar(sub_heap, MemoryAllocatorDump::kNameSize, |
| 153 MemoryAllocatorDump::kUnitsBytes, 1); | 153 MemoryAllocatorDump::kUnitsBytes, 1); |
| 154 CheckScalar(sub_heap, MemoryAllocatorDump::kNameObjectsCount, | 154 CheckScalar(sub_heap, MemoryAllocatorDump::kNameObjectCount, |
| 155 MemoryAllocatorDump::kUnitsObjects, 3); | 155 MemoryAllocatorDump::kUnitsObjects, 3); |
| 156 const MemoryAllocatorDump* empty_sub_heap = | 156 const MemoryAllocatorDump* empty_sub_heap = |
| 157 pmd.GetAllocatorDump("foobar_allocator/sub_heap/empty"); | 157 pmd.GetAllocatorDump("foobar_allocator/sub_heap/empty"); |
| 158 ASSERT_NE(nullptr, empty_sub_heap); | 158 ASSERT_NE(nullptr, empty_sub_heap); |
| 159 EXPECT_EQ("foobar_allocator/sub_heap/empty", empty_sub_heap->absolute_name()); | 159 EXPECT_EQ("foobar_allocator/sub_heap/empty", empty_sub_heap->absolute_name()); |
| 160 auto raw_attrs = empty_sub_heap->attributes_for_testing()->ToBaseValue(); | 160 auto raw_attrs = empty_sub_heap->attributes_for_testing()->ToBaseValue(); |
| 161 DictionaryValue* attrs = nullptr; | 161 DictionaryValue* attrs = nullptr; |
| 162 ASSERT_TRUE(raw_attrs->GetAsDictionary(&attrs)); | 162 ASSERT_TRUE(raw_attrs->GetAsDictionary(&attrs)); |
| 163 ASSERT_FALSE(attrs->HasKey(MemoryAllocatorDump::kNameSize)); | 163 ASSERT_FALSE(attrs->HasKey(MemoryAllocatorDump::kNameSize)); |
| 164 ASSERT_FALSE(attrs->HasKey(MemoryAllocatorDump::kNameObjectsCount)); | 164 ASSERT_FALSE(attrs->HasKey(MemoryAllocatorDump::kNameObjectCount)); |
| 165 | 165 |
| 166 // Check that the AsValueInfo doesn't hit any DCHECK. | 166 // Check that the AsValueInfo doesn't hit any DCHECK. |
| 167 scoped_refptr<TracedValue> traced_value(new TracedValue()); | 167 scoped_refptr<TracedValue> traced_value(new TracedValue()); |
| 168 pmd.AsValueInto(traced_value.get()); | 168 pmd.AsValueInto(traced_value.get()); |
| 169 } | 169 } |
| 170 | 170 |
| 171 // DEATH tests are not supported in Android / iOS. | 171 // DEATH tests are not supported in Android / iOS. |
| 172 #if !defined(NDEBUG) && !defined(OS_ANDROID) && !defined(OS_IOS) | 172 #if !defined(NDEBUG) && !defined(OS_ANDROID) && !defined(OS_IOS) |
| 173 TEST(MemoryAllocatorDumpTest, ForbidDuplicatesDeathTest) { | 173 TEST(MemoryAllocatorDumpTest, ForbidDuplicatesDeathTest) { |
| 174 FakeMemoryAllocatorDumpProvider fmadp; | 174 FakeMemoryAllocatorDumpProvider fmadp; |
| 175 ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState())); | 175 ProcessMemoryDump pmd(make_scoped_refptr(new MemoryDumpSessionState())); |
| 176 pmd.CreateAllocatorDump("foo_allocator"); | 176 pmd.CreateAllocatorDump("foo_allocator"); |
| 177 pmd.CreateAllocatorDump("bar_allocator/heap"); | 177 pmd.CreateAllocatorDump("bar_allocator/heap"); |
| 178 ASSERT_DEATH(pmd.CreateAllocatorDump("foo_allocator"), ""); | 178 ASSERT_DEATH(pmd.CreateAllocatorDump("foo_allocator"), ""); |
| 179 ASSERT_DEATH(pmd.CreateAllocatorDump("bar_allocator/heap"), ""); | 179 ASSERT_DEATH(pmd.CreateAllocatorDump("bar_allocator/heap"), ""); |
| 180 ASSERT_DEATH(pmd.CreateAllocatorDump(""), ""); | 180 ASSERT_DEATH(pmd.CreateAllocatorDump(""), ""); |
| 181 } | 181 } |
| 182 #endif | 182 #endif |
| 183 | 183 |
| 184 } // namespace trace_event | 184 } // namespace trace_event |
| 185 } // namespace base | 185 } // namespace base |
| OLD | NEW |