Chromium Code Reviews| 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..b83aba7279ec8e37b213f87ca04cc350b70ddc26 |
| --- /dev/null |
| +++ b/base/allocator/type_profiler_unittests.cc |
| @@ -0,0 +1,189 @@ |
| +// 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 since type_profiler_map |
| +// used in type_profiler is a singleton (like TCMalloc's heap-profiler), and |
| +// it requires RTTI and different compiling/linking options from others |
| +// |
| +// 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. |
| + |
| +#include "base/allocator/type_profiler.h" |
| +#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 SetInterceptFunctions() { |
| + InterceptFunctions::SetFunctions(NewInterceptForTCMalloc, |
| + DeleteInterceptForTCMalloc); |
| + } |
| + |
| + void ResetInterceptFunctions() { |
| + InterceptFunctions::ResetFunctions(); |
| + } |
| + |
| + void SetUp() { |
| + SetInterceptFunctions(); |
| + } |
| + |
| + void TearDown() { |
| + ResetInterceptFunctions(); |
| + } |
| + |
| + protected: |
| + 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) { |
| + 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) { |
| + 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) { |
| + 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) { |
| + static const size_t large_size = 256 * 1024; |
| + |
| + char* dummy_char = new char[large_size / sizeof(*dummy_char)]; |
| + 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__. |
|
Ryan Sleevi
2012/09/12 19:13:34
This comment actually makes me nervous, from the i
Dai Mikurube (NOT FULLTIME)
2012/09/13 01:12:40
When ::operator delete is called directly, our ext
Dai Mikurube (NOT FULLTIME)
2012/09/13 01:29:48
fyi, we already know STL <allocator> is the larges
|
| + ::operator delete[](dummy_char); |
| + |
| + 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(*dummy_int) - 1]; |
| + |
| + // 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; |
| + |
| + type = LookupType(dummy_int); |
| + EXPECT_EQ(type, kConstNull); |
| +} |
| + |
| +TEST_F(TypeProfilerTest, TestProfileDeleteWithoutProfiledNew) { |
| + // 'dummy' should be new'ed in this test before intercept functions are set. |
| + ResetInterceptFunctions(); |
| + |
| + int* dummy = new int(48); |
| + const std::type_info* type; |
| + |
| + // Set intercept functions again after 'dummy' is new'ed. |
| + SetInterceptFunctions(); |
| + |
| + delete dummy; |
| + |
| + type = LookupType(dummy); |
| + EXPECT_EQ(type, kConstNull); |
| + |
| + ResetInterceptFunctions(); |
| +} |
| + |
| +TEST_F(TypeProfilerTest, TestProfileNewWithoutProfiledDelete) { |
| + int* dummy = new int(48); |
| + const std::type_info* type; |
| + |
| + EXPECT_TRUE(Controller::IsProfiling()); |
| + |
| + // Stop profiling before deleting 'dummy'. |
| + Controller::Stop(); |
| + EXPECT_FALSE(Controller::IsProfiling()); |
| + |
| + delete dummy; |
| + |
| + // NOTE: We accept that a profile entry remains when a profiled object is |
| + // deleted after Controller::Stop(). |
| + type = LookupType(dummy); |
| + ASSERT_NE(type, kConstNull); |
| + EXPECT_STREQ(type->name(), typeid(int).name()); |
| + |
| + Controller::Restart(); |
| + EXPECT_TRUE(Controller::IsProfiling()); |
| + |
| + // Remove manually since 'dummy' is not removed from type_profiler_map. |
| + EraseType(dummy); |
| +} |
| + |
| +} // namespace type_profiler |
| +} // namespace base |
| + |
| +#endif // defined(TYPE_PROFILING) |
| + |
| +int main(int argc, char** argv) { |
| + testing::InitGoogleTest(&argc, argv); |
| + return RUN_ALL_TESTS(); |
| +} |