OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/trace_event_memory.h" | 5 #include "base/trace_event/trace_event_memory.h" |
6 | 6 |
7 #include <sstream> | 7 #include <sstream> |
8 #include <string> | 8 #include <string> |
9 | 9 |
| 10 #include "base/allocator/allocator_extension.h" |
10 #include "base/trace_event/trace_event_impl.h" | 11 #include "base/trace_event/trace_event_impl.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 #if defined(TCMALLOC_TRACE_MEMORY_SUPPORTED) | |
14 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h" | |
15 #endif | |
16 | |
17 namespace base { | 14 namespace base { |
18 namespace trace_event { | 15 namespace trace_event { |
19 | 16 |
20 // Tests for the trace event memory tracking system. Exists as a class so it | 17 // Tests for the trace event memory tracking system. Exists as a class so it |
21 // can be a friend of TraceMemoryController. | 18 // can be a friend of TraceMemoryController. |
22 class TraceMemoryTest : public testing::Test { | 19 class TraceMemoryTest : public testing::Test { |
23 public: | 20 public: |
24 TraceMemoryTest() {} | 21 TraceMemoryTest() { |
| 22 // This is needed for the using heap profiling methods from allocator. |
| 23 base::allocator::InitializeAllocator(); |
| 24 } |
| 25 |
25 ~TraceMemoryTest() override {} | 26 ~TraceMemoryTest() override {} |
26 | 27 |
27 private: | 28 private: |
28 DISALLOW_COPY_AND_ASSIGN(TraceMemoryTest); | 29 DISALLOW_COPY_AND_ASSIGN(TraceMemoryTest); |
29 }; | 30 }; |
30 | 31 |
31 ////////////////////////////////////////////////////////////////////////////// | 32 ////////////////////////////////////////////////////////////////////////////// |
32 | 33 |
33 #if defined(TCMALLOC_TRACE_MEMORY_SUPPORTED) | 34 #if defined(TCMALLOC_TRACE_MEMORY_SUPPORTED) |
34 | 35 |
35 TEST_F(TraceMemoryTest, TraceMemoryController) { | 36 TEST_F(TraceMemoryTest, TraceMemoryController) { |
36 MessageLoop message_loop; | 37 MessageLoop message_loop; |
37 | 38 |
38 // Start with no observers of the TraceLog. | 39 // Start with no observers of the TraceLog. |
39 EXPECT_EQ(0u, TraceLog::GetInstance()->GetObserverCountForTest()); | 40 EXPECT_EQ(0u, TraceLog::GetInstance()->GetObserverCountForTest()); |
40 | 41 |
41 // Creating a controller adds it to the TraceLog observer list. | 42 // Creating a controller adds it to the TraceLog observer list. |
42 scoped_ptr<TraceMemoryController> controller(new TraceMemoryController( | 43 scoped_ptr<TraceMemoryController> controller( |
43 message_loop.task_runner(), ::HeapProfilerWithPseudoStackStart, | 44 new TraceMemoryController(message_loop.task_runner())); |
44 ::HeapProfilerStop, ::GetHeapProfile)); | |
45 EXPECT_EQ(1u, TraceLog::GetInstance()->GetObserverCountForTest()); | 45 EXPECT_EQ(1u, TraceLog::GetInstance()->GetObserverCountForTest()); |
46 EXPECT_TRUE( | 46 EXPECT_TRUE( |
47 TraceLog::GetInstance()->HasEnabledStateObserver(controller.get())); | 47 TraceLog::GetInstance()->HasEnabledStateObserver(controller.get())); |
48 | 48 |
49 // By default the observer isn't dumping memory profiles. | 49 // By default the observer isn't dumping memory profiles. |
50 EXPECT_FALSE(controller->IsTimerRunningForTest()); | 50 EXPECT_FALSE(controller->IsTimerRunningForTest()); |
51 | 51 |
52 // Simulate enabling tracing. | 52 // Simulate enabling tracing. |
53 controller->StartProfiling(); | 53 controller->StartProfiling(); |
54 message_loop.RunUntilIdle(); | 54 message_loop.RunUntilIdle(); |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 EXPECT_STREQ("null", StringFromHexAddress("0x0")); | 227 EXPECT_STREQ("null", StringFromHexAddress("0x0")); |
228 EXPECT_STREQ("error", StringFromHexAddress("not an address")); | 228 EXPECT_STREQ("error", StringFromHexAddress("not an address")); |
229 const char kHello[] = "hello"; | 229 const char kHello[] = "hello"; |
230 std::ostringstream hex_address; | 230 std::ostringstream hex_address; |
231 hex_address << &kHello; | 231 hex_address << &kHello; |
232 EXPECT_STREQ(kHello, StringFromHexAddress(hex_address.str())); | 232 EXPECT_STREQ(kHello, StringFromHexAddress(hex_address.str())); |
233 } | 233 } |
234 | 234 |
235 } // namespace trace_event | 235 } // namespace trace_event |
236 } // namespace base | 236 } // namespace base |
OLD | NEW |