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

Unified Diff: base/allocator/type_profiler_unittests.cc

Issue 10411047: Type profiler by intercepting 'new' and 'delete' expressions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix IsAvailable Created 8 years, 3 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/allocator/type_profiler_unittests.cc
diff --git a/base/allocator/type_profiler_unittests.cc b/base/allocator/type_profiler_unittests.cc
new file mode 100644
index 0000000000000000000000000000000000000000..4ea25d9ab0d997a8c0b8cc9fd91cd63a26038276
--- /dev/null
+++ b/base/allocator/type_profiler_unittests.cc
@@ -0,0 +1,187 @@
+// Copyright 2012 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.
+
+// This is a unittest set for type_profiler. It is independent from other
+// tests and executed manually like allocator_unittests because type_profiler
+// is not able to turn on/off many times.
+//
+// It tests that the profiler doesn't fail in suspicous cases. For example,
+// 'new' is not profiled, but 'delete' for the created object is profiled.
+//
+// NOTE: This test must be run with an environment variable "HEAPPROFILE".
+//
+// NOTE: Each test in TypeProfilerTest must be executed separately with using
jar (doing other things) 2012/09/06 19:31:43 How is this done via the unit test harness?
Dai Mikurube (NOT FULLTIME) 2012/09/07 08:06:45 Removed this limitation.
+// --gtest_filter like following :
+// --gtest_filter="TypeProfilerTest.TestNormalProfiling"
+// because type_profiler provides only an initializer and a stopper for safety.
jar (doing other things) 2012/09/06 19:31:43 All your code below is single threaded. As a resu
Dai Mikurube (NOT FULLTIME) 2012/09/07 08:06:45 Done.
+// It can be initialized once, and stopped once.
+
+#include "base/allocator/type_profiler_control.h"
+#include "base/allocator/type_profiler_tcmalloc.h"
+#include "base/basictypes.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/tcmalloc/chromium/src/gperftools/type_profiler_map.h"
+
+#if defined(TYPE_PROFILING)
+
+namespace base {
+namespace type_profiler {
+
+class TypeProfilerTest : public testing::Test {
+ public:
+ TypeProfilerTest() {}
+
+ void SetUp() {
+ ASSERT_FALSE(isAvailable()) << "this test must be executed separately.";
+ }
+
+ protected:
+ bool isAvailable() {
+ return InterceptFunctions::IsAvailable();
+ }
+
+ static const size_t kDummyArraySize;
+ static const void* const kConstNull;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(TypeProfilerTest);
+};
+
+const size_t TypeProfilerTest::kDummyArraySize = 10;
+const void* const TypeProfilerTest::kConstNull = static_cast<const void*>(NULL);
+
+TEST_F(TypeProfilerTest, TestNormalProfiling) {
+ ASSERT_TRUE(InterceptFunctions::SetFunctions(NewInterceptForTCMalloc,
+ DeleteInterceptForTCMalloc));
+
+ int* dummy = new int(48);
+ const std::type_info* type;
+
+ type = LookupType(dummy);
+ ASSERT_NE(type, kConstNull);
+ EXPECT_STREQ(type->name(), typeid(int).name());
+ delete dummy;
+
+ type = LookupType(dummy);
+ EXPECT_EQ(type, kConstNull);
+}
+
+TEST_F(TypeProfilerTest, TestNormalArrayProfiling) {
+ ASSERT_TRUE(InterceptFunctions::SetFunctions(NewInterceptForTCMalloc,
+ DeleteInterceptForTCMalloc));
+
+ int* dummy = new int[kDummyArraySize];
+ const std::type_info* type;
+
+ type = LookupType(dummy);
+ ASSERT_NE(type, kConstNull);
+ // For an array, the profiler remembers its base type.
+ EXPECT_STREQ(type->name(), typeid(int).name());
+ delete[] dummy;
+
+ type = LookupType(dummy);
+ EXPECT_EQ(type, kConstNull);
+}
+
+TEST_F(TypeProfilerTest, TestRepeatedNewAndDelete) {
+ ASSERT_TRUE(InterceptFunctions::SetFunctions(NewInterceptForTCMalloc,
+ DeleteInterceptForTCMalloc));
+
+ int *dummy[kDummyArraySize];
+ const std::type_info* type;
+ for (int i = 0; i < kDummyArraySize; ++i)
+ dummy[i] = new int(i);
+
+ for (int i = 0; i < kDummyArraySize; ++i) {
+ type = LookupType(dummy[i]);
+ ASSERT_NE(type, kConstNull);
+ EXPECT_STREQ(type->name(), typeid(int).name());
+ }
+
+ for (int i = 0; i < kDummyArraySize; ++i) {
+ delete dummy[i];
+ type = LookupType(dummy[i]);
+ ASSERT_EQ(type, kConstNull);
+ }
+}
+
+TEST_F(TypeProfilerTest, TestMultipleNewWithDroppingDelete) {
+ ASSERT_TRUE(InterceptFunctions::SetFunctions(NewInterceptForTCMalloc,
+ DeleteInterceptForTCMalloc));
+
+ static const size_t large_size = 256 * 1024;
+
+ char* dummy_char = new char[large_size];
+ const std::type_info* type;
+
+ type = LookupType(dummy_char);
+ ASSERT_NE(type, kConstNull);
+ EXPECT_STREQ(type->name(), typeid(char).name());
+
+ // Call "::operator delete" directly to drop __op_delete_intercept__.
+ ::operator delete(dummy_char);
jar (doing other things) 2012/09/06 19:31:43 Is it not well defined to new up an array, and cal
Dai Mikurube (NOT FULLTIME) 2012/09/07 08:06:45 Thanks for the good catch. I overlooked it when I
+
+ type = LookupType(dummy_char);
+ ASSERT_NE(type, kConstNull);
+ EXPECT_STREQ(type->name(), typeid(char).name());
+
+ // Allocates a little different size.
+ int* dummy_int = new int[large_size / sizeof(int) - 1];
jar (doing other things) 2012/09/06 19:31:43 nit: sizeof(*dummy_int) For clarity, you might a
Dai Mikurube (NOT FULLTIME) 2012/09/07 08:06:45 Done.
+
+ // We expect that tcmalloc returns the same address for these large (over 32k)
+ // allocation calls. It usually happens, but maybe probablistic.
+ ASSERT_EQ(static_cast<void*>(dummy_int), static_cast<void*>(dummy_char)) <<
+ "two new (malloc) calls didn't return the same address; retry it.";
+
+ type = LookupType(dummy_int);
+ ASSERT_NE(type, kConstNull);
+ EXPECT_STREQ(type->name(), typeid(int).name());
+
+ delete dummy_int;
jar (doing other things) 2012/09/06 19:31:43 delete[] dummy_int;
Dai Mikurube (NOT FULLTIME) 2012/09/07 08:06:45 Done.
+
+ type = LookupType(dummy_int);
+ EXPECT_EQ(type, kConstNull);
+}
+
+TEST_F(TypeProfilerTest, TestProfileDeleteWithoutProfiledNew) {
+ int* dummy = new int(48);
+ const std::type_info* type;
+
+ // Set intercept functions after 'dummy' is new'ed.
+ ASSERT_TRUE(InterceptFunctions::SetFunctions(NewInterceptForTCMalloc,
+ DeleteInterceptForTCMalloc));
+
+ delete dummy;
+
+ type = LookupType(dummy);
+ EXPECT_EQ(type, kConstNull);
+}
+
+TEST_F(TypeProfilerTest, TestProfileNewWithoutProfiledDelete) {
+ ASSERT_TRUE(InterceptFunctions::SetFunctions(NewInterceptForTCMalloc,
+ DeleteInterceptForTCMalloc));
+
+ int* dummy = new int(48);
+ const std::type_info* type;
+
+ StopProfiling();
+
+ delete dummy;
+
+ // NOTE: We accept that a profile entry remains when a profiled object is
+ // deleted after StopProfiling().
+ type = LookupType(dummy);
+ ASSERT_NE(type, kConstNull);
+ EXPECT_STREQ(type->name(), typeid(int).name());
+}
+
+} // namespace type_profiler
+} // namespace base
+
+#endif // defined(TYPE_PROFILING)
+
+int main(int argc, char** argv) {
+ testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}

Powered by Google App Engine
This is Rietveld 408576698