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

Unified Diff: base/trace_event/memory_allocator_dump_unittest.cc

Issue 987043003: [tracing] Introduce support for userland memory allocators dumps. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mmaps_en
Patch Set: Created 5 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: base/trace_event/memory_allocator_dump_unittest.cc
diff --git a/base/trace_event/memory_allocator_dump_unittest.cc b/base/trace_event/memory_allocator_dump_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..2f6d4e5fee71ec4caeb2f6e21f2d442ee235ce83
--- /dev/null
+++ b/base/trace_event/memory_allocator_dump_unittest.cc
@@ -0,0 +1,105 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/trace_event/memory_allocator_dump.h"
+
+#include "base/trace_event/memory_dump_provider.h"
+#include "base/trace_event/process_memory_dump.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace base {
+namespace trace_event {
+
+namespace {
+class FakeMemoryAllocatorDumpProvider : public MemoryDumpProvider {
+ public:
+ FakeMemoryAllocatorDumpProvider() {
+ allocator_attributes()->Add(
+ MemoryAllocatorDeclaredAttribute({"attr1", "count"}));
+ allocator_attributes()->Add(
+ MemoryAllocatorDeclaredAttribute({"attr2", "bytes"}));
+ }
+
+ bool DumpInto(ProcessMemoryDump* pmd) override {
+ MemoryAllocatorDump* mad_foo = pmd->AddAllocatorDump("foo");
+ mad_foo->set_physical_size_in_bytes(4096);
+ mad_foo->set_allocated_objects_count(42);
+ mad_foo->set_allocated_objects_size_in_bytes(1000);
+ mad_foo->extra_attributes().SetInteger("scalar", 1234);
+ mad_foo->extra_attributes().SetInteger("percent", 99);
+
+ MemoryAllocatorDump* mad_bar = pmd->AddAllocatorDump("foo.bar", mad_foo);
+ mad_bar->set_physical_size_in_bytes(1);
+ mad_bar->set_allocated_objects_count(2);
+ mad_bar->set_allocated_objects_size_in_bytes(3);
+
+ pmd->AddAllocatorDump("baz");
+ // Leave the rest of |baz| deliberately uninitialized, to check that
+ // AddAllocatorDump returns a properly zero-initialized object.
+
+ return true;
+ }
+
+ const char* GetFriendlyName() const override { return "mock_allocator"; }
+};
+} // namespace
+
+TEST(MemoryAllocatorDumpTest, DumpIntoProcessMemoryDump) {
+ FakeMemoryAllocatorDumpProvider fmadp;
+ ProcessMemoryDump pmd;
+
+ fmadp.DumpInto(&pmd);
+
+ ASSERT_EQ(3u, pmd.allocator_dumps().size());
+
+ const MemoryAllocatorDump* mad_foo = pmd.GetAllocatorDump("foo");
+ ASSERT_NE(nullptr, mad_foo);
+ EXPECT_EQ("foo", mad_foo->name());
+ ASSERT_EQ(nullptr, mad_foo->parent());
+ EXPECT_EQ(4096u, mad_foo->physical_size_in_bytes());
+ EXPECT_EQ(42u, mad_foo->allocated_objects_count());
+ EXPECT_EQ(1000u, mad_foo->allocated_objects_size_in_bytes());
+
+ // Check the extra attributes of |mad_foo|.
+ bool res;
+ int attr_value;
+ ASSERT_EQ(2u, mad_foo->extra_attributes().size());
+ res = mad_foo->extra_attributes().GetInteger("scalar", &attr_value);
+ EXPECT_TRUE(res);
+ EXPECT_EQ(1234, attr_value);
+ res = mad_foo->extra_attributes().GetInteger("percent", &attr_value);
+ EXPECT_TRUE(res);
+ EXPECT_EQ(99, attr_value);
+
+ const MemoryAllocatorDump* mad_bar = pmd.GetAllocatorDump("foo.bar");
+ ASSERT_NE(nullptr, mad_bar);
+ EXPECT_EQ("foo.bar", mad_bar->name());
+ ASSERT_EQ(mad_foo, mad_bar->parent());
+ EXPECT_EQ(1u, mad_bar->physical_size_in_bytes());
+ EXPECT_EQ(2u, mad_bar->allocated_objects_count());
+ EXPECT_EQ(3u, mad_bar->allocated_objects_size_in_bytes());
+ ASSERT_EQ(0u, mad_bar->extra_attributes().size());
+
+ const MemoryAllocatorDump* mad_baz = pmd.GetAllocatorDump("baz");
+ ASSERT_NE(nullptr, mad_baz);
+ EXPECT_EQ("baz", mad_baz->name());
+ ASSERT_EQ(nullptr, mad_baz->parent());
+ EXPECT_EQ(0u, mad_baz->physical_size_in_bytes());
+ EXPECT_EQ(0u, mad_baz->allocated_objects_count());
+ EXPECT_EQ(0u, mad_baz->allocated_objects_size_in_bytes());
+ ASSERT_EQ(0u, mad_baz->extra_attributes().size());
+}
+
+#ifndef NDEBUG
+TEST(MemoryAllocatorDumpTest, ForbidDuplicatesDeathTest) {
+ FakeMemoryAllocatorDumpProvider fmadp;
+ ProcessMemoryDump pmd;
+ pmd.AddAllocatorDump("dump_1");
+ pmd.AddAllocatorDump("dump_2");
+ ASSERT_DEATH({ pmd.AddAllocatorDump("dump_1"); }, "");
+}
+#endif
+
+} // namespace trace_event
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698