| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/memory_dump_manager.h" | 5 #include "base/trace_event/memory_dump_manager.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 | 214 |
| 215 private: | 215 private: |
| 216 ~TestSequencedTaskRunner() override {} | 216 ~TestSequencedTaskRunner() override {} |
| 217 | 217 |
| 218 SequencedWorkerPoolOwner worker_pool_; | 218 SequencedWorkerPoolOwner worker_pool_; |
| 219 const SequencedWorkerPool::SequenceToken token_; | 219 const SequencedWorkerPool::SequenceToken token_; |
| 220 bool enabled_; | 220 bool enabled_; |
| 221 unsigned num_of_post_tasks_; | 221 unsigned num_of_post_tasks_; |
| 222 }; | 222 }; |
| 223 | 223 |
| 224 std::unique_ptr<trace_analyzer::TraceAnalyzer> GetDeserializedTrace() { |
| 225 // Flush the trace into JSON. |
| 226 trace_event::TraceResultBuffer buffer; |
| 227 TraceResultBuffer::SimpleOutput trace_output; |
| 228 buffer.SetOutputCallback(trace_output.GetCallback()); |
| 229 RunLoop run_loop; |
| 230 buffer.Start(); |
| 231 trace_event::TraceLog::GetInstance()->Flush( |
| 232 Bind(&OnTraceDataCollected, run_loop.QuitClosure(), Unretained(&buffer))); |
| 233 run_loop.Run(); |
| 234 buffer.Finish(); |
| 235 |
| 236 // Analyze the JSON. |
| 237 return WrapUnique( |
| 238 trace_analyzer::TraceAnalyzer::Create(trace_output.json_output)); |
| 239 } |
| 240 |
| 224 } // namespace | 241 } // namespace |
| 225 | 242 |
| 226 class MemoryDumpManagerTest : public testing::Test { | 243 class MemoryDumpManagerTest : public testing::Test { |
| 227 public: | 244 public: |
| 228 MemoryDumpManagerTest() : testing::Test(), kDefaultOptions() {} | 245 MemoryDumpManagerTest() : testing::Test(), kDefaultOptions() {} |
| 229 | 246 |
| 230 void SetUp() override { | 247 void SetUp() override { |
| 231 last_callback_success_ = false; | 248 last_callback_success_ = false; |
| 232 message_loop_.reset(new MessageLoop()); | 249 message_loop_.reset(new MessageLoop()); |
| 233 mdm_.reset(new MemoryDumpManager()); | 250 mdm_.reset(new MemoryDumpManager()); |
| (...skipping 839 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1073 | 1090 |
| 1074 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); | 1091 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); |
| 1075 EXPECT_CALL(global_dump_handler_, RequestGlobalMemoryDump(_, _)).Times(1); | 1092 EXPECT_CALL(global_dump_handler_, RequestGlobalMemoryDump(_, _)).Times(1); |
| 1076 EXPECT_CALL(mdp1, OnMemoryDump(_, _)).Times(1).WillRepeatedly(Return(true)); | 1093 EXPECT_CALL(mdp1, OnMemoryDump(_, _)).Times(1).WillRepeatedly(Return(true)); |
| 1077 EXPECT_CALL(mdp2, OnMemoryDump(_, _)).Times(1).WillRepeatedly(Return(true)); | 1094 EXPECT_CALL(mdp2, OnMemoryDump(_, _)).Times(1).WillRepeatedly(Return(true)); |
| 1078 EXPECT_CALL(mdp3, OnMemoryDump(_, _)).Times(1).WillRepeatedly(Return(true)); | 1095 EXPECT_CALL(mdp3, OnMemoryDump(_, _)).Times(1).WillRepeatedly(Return(true)); |
| 1079 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, | 1096 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, |
| 1080 MemoryDumpLevelOfDetail::DETAILED); | 1097 MemoryDumpLevelOfDetail::DETAILED); |
| 1081 DisableTracing(); | 1098 DisableTracing(); |
| 1082 | 1099 |
| 1083 // Flush the trace into JSON. | 1100 std::unique_ptr<trace_analyzer::TraceAnalyzer> analyzer = |
| 1084 trace_event::TraceResultBuffer buffer; | 1101 GetDeserializedTrace(); |
| 1085 TraceResultBuffer::SimpleOutput trace_output; | |
| 1086 buffer.SetOutputCallback(trace_output.GetCallback()); | |
| 1087 RunLoop run_loop; | |
| 1088 buffer.Start(); | |
| 1089 trace_event::TraceLog::GetInstance()->Flush( | |
| 1090 Bind(&OnTraceDataCollected, run_loop.QuitClosure(), Unretained(&buffer))); | |
| 1091 run_loop.Run(); | |
| 1092 buffer.Finish(); | |
| 1093 | |
| 1094 // Analyze the JSON. | |
| 1095 std::unique_ptr<trace_analyzer::TraceAnalyzer> analyzer = WrapUnique( | |
| 1096 trace_analyzer::TraceAnalyzer::Create(trace_output.json_output)); | |
| 1097 trace_analyzer::TraceEventVector events; | 1102 trace_analyzer::TraceEventVector events; |
| 1098 analyzer->FindEvents(Query::EventPhaseIs(TRACE_EVENT_PHASE_MEMORY_DUMP), | 1103 analyzer->FindEvents(Query::EventPhaseIs(TRACE_EVENT_PHASE_MEMORY_DUMP), |
| 1099 &events); | 1104 &events); |
| 1100 | 1105 |
| 1101 ASSERT_EQ(3u, events.size()); | 1106 ASSERT_EQ(3u, events.size()); |
| 1102 ASSERT_EQ(1u, trace_analyzer::CountMatches(events, Query::EventPidIs(123))); | 1107 ASSERT_EQ(1u, trace_analyzer::CountMatches(events, Query::EventPidIs(123))); |
| 1103 ASSERT_EQ(1u, trace_analyzer::CountMatches(events, Query::EventPidIs(456))); | 1108 ASSERT_EQ(1u, trace_analyzer::CountMatches(events, Query::EventPidIs(456))); |
| 1104 ASSERT_EQ(1u, trace_analyzer::CountMatches( | 1109 ASSERT_EQ(1u, trace_analyzer::CountMatches( |
| 1105 events, Query::EventPidIs(GetCurrentProcId()))); | 1110 events, Query::EventPidIs(GetCurrentProcId()))); |
| 1106 ASSERT_EQ(events[0]->id, events[1]->id); | 1111 ASSERT_EQ(events[0]->id, events[1]->id); |
| 1107 ASSERT_EQ(events[0]->id, events[2]->id); | 1112 ASSERT_EQ(events[0]->id, events[2]->id); |
| 1108 } | 1113 } |
| 1109 | 1114 |
| 1115 TEST_F(MemoryDumpManagerTest, SummaryOnlyDumpsArentAddedToTrace) { |
| 1116 using trace_analyzer::Query; |
| 1117 |
| 1118 InitializeMemoryDumpManager(false /* is_coordinator */); |
| 1119 SetDumpProviderWhitelistForTesting(kTestMDPWhitelist); |
| 1120 |
| 1121 // Standard provider with default options (create dump for current process). |
| 1122 MockMemoryDumpProvider mdp; |
| 1123 RegisterDumpProvider(&mdp, nullptr, kDefaultOptions, kWhitelistedMDPName); |
| 1124 |
| 1125 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); |
| 1126 EXPECT_CALL(global_dump_handler_, RequestGlobalMemoryDump(_, _)).Times(2); |
| 1127 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(2).WillRepeatedly(Return(true)); |
| 1128 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, |
| 1129 MemoryDumpLevelOfDetail::BACKGROUND); |
| 1130 RequestGlobalDumpAndWait(MemoryDumpType::SUMMARY_ONLY, |
| 1131 MemoryDumpLevelOfDetail::BACKGROUND); |
| 1132 DisableTracing(); |
| 1133 |
| 1134 std::unique_ptr<trace_analyzer::TraceAnalyzer> analyzer = |
| 1135 GetDeserializedTrace(); |
| 1136 trace_analyzer::TraceEventVector events; |
| 1137 analyzer->FindEvents(Query::EventPhaseIs(TRACE_EVENT_PHASE_MEMORY_DUMP), |
| 1138 &events); |
| 1139 |
| 1140 ASSERT_EQ(1u, events.size()); |
| 1141 ASSERT_TRUE(trace_analyzer::CountMatches( |
| 1142 events, Query::EventNameIs(MemoryDumpTypeToString( |
| 1143 MemoryDumpType::EXPLICITLY_TRIGGERED)))); |
| 1144 } |
| 1145 |
| 1110 // Tests the basics of the UnregisterAndDeleteDumpProviderSoon(): the | 1146 // Tests the basics of the UnregisterAndDeleteDumpProviderSoon(): the |
| 1111 // unregistration should actually delete the providers and not leak them. | 1147 // unregistration should actually delete the providers and not leak them. |
| 1112 TEST_F(MemoryDumpManagerTest, UnregisterAndDeleteDumpProviderSoon) { | 1148 TEST_F(MemoryDumpManagerTest, UnregisterAndDeleteDumpProviderSoon) { |
| 1113 InitializeMemoryDumpManager(false /* is_coordinator */); | 1149 InitializeMemoryDumpManager(false /* is_coordinator */); |
| 1114 static const int kNumProviders = 3; | 1150 static const int kNumProviders = 3; |
| 1115 int dtor_count = 0; | 1151 int dtor_count = 0; |
| 1116 std::vector<std::unique_ptr<MemoryDumpProvider>> mdps; | 1152 std::vector<std::unique_ptr<MemoryDumpProvider>> mdps; |
| 1117 for (int i = 0; i < kNumProviders; ++i) { | 1153 for (int i = 0; i < kNumProviders; ++i) { |
| 1118 std::unique_ptr<MockMemoryDumpProvider> mdp(new MockMemoryDumpProvider); | 1154 std::unique_ptr<MockMemoryDumpProvider> mdp(new MockMemoryDumpProvider); |
| 1119 mdp->enable_mock_destructor = true; | 1155 mdp->enable_mock_destructor = true; |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1277 // successful we also managed to add the dump to the trace. | 1313 // successful we also managed to add the dump to the trace. |
| 1278 EXPECT_FALSE(last_callback_success_); | 1314 EXPECT_FALSE(last_callback_success_); |
| 1279 | 1315 |
| 1280 mdm_->Disable(); | 1316 mdm_->Disable(); |
| 1281 | 1317 |
| 1282 mdm_->UnregisterDumpProvider(&mdp); | 1318 mdm_->UnregisterDumpProvider(&mdp); |
| 1283 } | 1319 } |
| 1284 | 1320 |
| 1285 } // namespace trace_event | 1321 } // namespace trace_event |
| 1286 } // namespace base | 1322 } // namespace base |
| OLD | NEW |