memory_allocator_dump_unittest.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include "base/trace_event/memory_allocator_dump.h"
  5. #include <stdint.h>
  6. #include "base/format_macros.h"
  7. #include "base/trace_event/memory_allocator_dump_guid.h"
  8. #include "base/trace_event/memory_dump_provider.h"
  9. #include "base/trace_event/process_memory_dump.h"
  10. #include "base/trace_event/traced_value.h"
  11. #include "base/values.h"
  12. #include "build/build_config.h"
  13. #include "testing/gmock/include/gmock/gmock.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. using testing::ElementsAre;
  16. using testing::Eq;
  17. using testing::ByRef;
  18. using testing::IsEmpty;
  19. namespace base {
  20. namespace trace_event {
  21. namespace {
  22. class FakeMemoryAllocatorDumpProvider : public MemoryDumpProvider {
  23. public:
  24. bool OnMemoryDump(const MemoryDumpArgs& args,
  25. ProcessMemoryDump* pmd) override {
  26. MemoryAllocatorDump* root_heap =
  27. pmd->CreateAllocatorDump("foobar_allocator");
  28. root_heap->AddScalar(MemoryAllocatorDump::kNameSize,
  29. MemoryAllocatorDump::kUnitsBytes, 4096);
  30. root_heap->AddScalar(MemoryAllocatorDump::kNameObjectCount,
  31. MemoryAllocatorDump::kUnitsObjects, 42);
  32. root_heap->AddScalar("attr1", "units1", 1234);
  33. root_heap->AddString("attr2", "units2", "string_value");
  34. MemoryAllocatorDump* sub_heap =
  35. pmd->CreateAllocatorDump("foobar_allocator/sub_heap");
  36. sub_heap->AddScalar(MemoryAllocatorDump::kNameSize,
  37. MemoryAllocatorDump::kUnitsBytes, 1);
  38. sub_heap->AddScalar(MemoryAllocatorDump::kNameObjectCount,
  39. MemoryAllocatorDump::kUnitsObjects, 3);
  40. pmd->CreateAllocatorDump("foobar_allocator/sub_heap/empty");
  41. // Leave the rest of sub heap deliberately uninitialized, to check that
  42. // CreateAllocatorDump returns a properly zero-initialized object.
  43. return true;
  44. }
  45. };
  46. void CheckString(const MemoryAllocatorDump* dump,
  47. const std::string& name,
  48. const char* expected_units,
  49. const std::string& expected_value) {
  50. MemoryAllocatorDump::Entry expected(name, expected_units, expected_value);
  51. EXPECT_THAT(dump->entries(), testing::Contains(Eq(ByRef(expected))));
  52. }
  53. void CheckScalar(const MemoryAllocatorDump* dump,
  54. const std::string& name,
  55. const char* expected_units,
  56. uint64_t expected_value) {
  57. MemoryAllocatorDump::Entry expected(name, expected_units, expected_value);
  58. EXPECT_THAT(dump->entries(), testing::Contains(Eq(ByRef(expected))));
  59. }
  60. } // namespace
  61. TEST(MemoryAllocatorDumpTest, GuidGeneration) {
  62. std::unique_ptr<MemoryAllocatorDump> mad(new MemoryAllocatorDump(
  63. "foo", MemoryDumpLevelOfDetail::FIRST, MemoryAllocatorDumpGuid(0x42u)));
  64. ASSERT_EQ("42", mad->guid().ToString());
  65. }
  66. TEST(MemoryAllocatorDumpTest, DumpIntoProcessMemoryDump) {
  67. FakeMemoryAllocatorDumpProvider fmadp;
  68. MemoryDumpArgs dump_args = {MemoryDumpLevelOfDetail::DETAILED};
  69. ProcessMemoryDump pmd(dump_args);
  70. fmadp.OnMemoryDump(dump_args, &pmd);
  71. ASSERT_EQ(3u, pmd.allocator_dumps().size());
  72. const MemoryAllocatorDump* root_heap =
  73. pmd.GetAllocatorDump("foobar_allocator");
  74. ASSERT_NE(nullptr, root_heap);
  75. EXPECT_EQ("foobar_allocator", root_heap->absolute_name());
  76. CheckScalar(root_heap, MemoryAllocatorDump::kNameSize,
  77. MemoryAllocatorDump::kUnitsBytes, 4096);
  78. CheckScalar(root_heap, MemoryAllocatorDump::kNameObjectCount,
  79. MemoryAllocatorDump::kUnitsObjects, 42);
  80. CheckScalar(root_heap, "attr1", "units1", 1234);
  81. CheckString(root_heap, "attr2", "units2", "string_value");
  82. const MemoryAllocatorDump* sub_heap =
  83. pmd.GetAllocatorDump("foobar_allocator/sub_heap");
  84. ASSERT_NE(nullptr, sub_heap);
  85. EXPECT_EQ("foobar_allocator/sub_heap", sub_heap->absolute_name());
  86. CheckScalar(sub_heap, MemoryAllocatorDump::kNameSize,
  87. MemoryAllocatorDump::kUnitsBytes, 1);
  88. CheckScalar(sub_heap, MemoryAllocatorDump::kNameObjectCount,
  89. MemoryAllocatorDump::kUnitsObjects, 3);
  90. const MemoryAllocatorDump* empty_sub_heap =
  91. pmd.GetAllocatorDump("foobar_allocator/sub_heap/empty");
  92. ASSERT_NE(nullptr, empty_sub_heap);
  93. EXPECT_EQ("foobar_allocator/sub_heap/empty", empty_sub_heap->absolute_name());
  94. EXPECT_THAT(empty_sub_heap->entries(), IsEmpty());
  95. // Check that calling serialization routines doesn't cause a crash.
  96. std::unique_ptr<TracedValue> traced_value(new TracedValue);
  97. pmd.SerializeAllocatorDumpsInto(traced_value.get());
  98. }
  99. TEST(MemoryAllocatorDumpTest, GetSize) {
  100. MemoryDumpArgs dump_args = {MemoryDumpLevelOfDetail::DETAILED};
  101. ProcessMemoryDump pmd(dump_args);
  102. MemoryAllocatorDump* dump = pmd.CreateAllocatorDump("allocator_for_size");
  103. dump->AddScalar(MemoryAllocatorDump::kNameSize,
  104. MemoryAllocatorDump::kUnitsBytes, 1);
  105. dump->AddScalar("foo", MemoryAllocatorDump::kUnitsBytes, 2);
  106. EXPECT_EQ(1u, dump->GetSizeInternal());
  107. }
  108. TEST(MemoryAllocatorDumpTest, ReadValues) {
  109. MemoryDumpArgs dump_args = {MemoryDumpLevelOfDetail::DETAILED};
  110. ProcessMemoryDump pmd(dump_args);
  111. MemoryAllocatorDump* dump = pmd.CreateAllocatorDump("allocator_for_size");
  112. dump->AddScalar("one", "byte", 1);
  113. dump->AddString("one", "object", "one");
  114. MemoryAllocatorDump::Entry expected_scalar("one", "byte", 1);
  115. MemoryAllocatorDump::Entry expected_string("one", "object", "one");
  116. EXPECT_THAT(dump->entries(), ElementsAre(Eq(ByRef(expected_scalar)),
  117. Eq(ByRef(expected_string))));
  118. }
  119. TEST(MemoryAllocatorDumpTest, MovingAnEntry) {
  120. MemoryAllocatorDump::Entry expected_entry("one", "byte", 1);
  121. MemoryAllocatorDump::Entry from_entry("one", "byte", 1);
  122. MemoryAllocatorDump::Entry to_entry = std::move(from_entry);
  123. EXPECT_EQ(expected_entry, to_entry);
  124. }
  125. // DEATH tests are not supported in Android/iOS/Fuchsia.
  126. #if !defined(NDEBUG) && !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS) && \
  127. !BUILDFLAG(IS_FUCHSIA)
  128. TEST(MemoryAllocatorDumpTest, ForbidDuplicatesDeathTest) {
  129. FakeMemoryAllocatorDumpProvider fmadp;
  130. MemoryDumpArgs dump_args = {MemoryDumpLevelOfDetail::DETAILED};
  131. ProcessMemoryDump pmd(dump_args);
  132. pmd.CreateAllocatorDump("foo_allocator");
  133. pmd.CreateAllocatorDump("bar_allocator/heap");
  134. ASSERT_DEATH(pmd.CreateAllocatorDump("foo_allocator"), "");
  135. ASSERT_DEATH(pmd.CreateAllocatorDump("bar_allocator/heap"), "");
  136. ASSERT_DEATH(pmd.CreateAllocatorDump(""), "");
  137. }
  138. TEST(MemoryAllocatorDumpTest, ForbidStringsInBackgroundModeDeathTest) {
  139. MemoryDumpArgs dump_args = {MemoryDumpLevelOfDetail::BACKGROUND};
  140. ProcessMemoryDump pmd(dump_args);
  141. MemoryAllocatorDump* dump = pmd.CreateAllocatorDump("malloc");
  142. ASSERT_DEATH(dump->AddString("foo", "bar", "baz"), "");
  143. }
  144. #endif
  145. } // namespace trace_event
  146. } // namespace base