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 <vector> | 10 #include <vector> |
(...skipping 30 matching lines...) Expand all Loading... |
41 | 41 |
42 // GTest matchers for MemoryDumpRequestArgs arguments. | 42 // GTest matchers for MemoryDumpRequestArgs arguments. |
43 MATCHER(IsDetailedDump, "") { | 43 MATCHER(IsDetailedDump, "") { |
44 return arg.level_of_detail == MemoryDumpLevelOfDetail::DETAILED; | 44 return arg.level_of_detail == MemoryDumpLevelOfDetail::DETAILED; |
45 } | 45 } |
46 | 46 |
47 MATCHER(IsLightDump, "") { | 47 MATCHER(IsLightDump, "") { |
48 return arg.level_of_detail == MemoryDumpLevelOfDetail::LIGHT; | 48 return arg.level_of_detail == MemoryDumpLevelOfDetail::LIGHT; |
49 } | 49 } |
50 | 50 |
| 51 MATCHER(IsBackgroundDump, "") { |
| 52 return arg.level_of_detail == MemoryDumpLevelOfDetail::BACKGROUND; |
| 53 } |
| 54 |
51 namespace { | 55 namespace { |
52 | 56 |
| 57 const char* kMDPName = "TestDumpProvider"; |
| 58 const char* kWhitelistedMDPName = "WhitelistedTestDumpProvider"; |
| 59 |
| 60 void RegisterDumpProvider( |
| 61 MemoryDumpProvider* mdp, |
| 62 const char* name, |
| 63 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 64 const MemoryDumpProvider::Options& options) { |
| 65 MemoryDumpManager* mdm = MemoryDumpManager::GetInstance(); |
| 66 mdm->set_dumper_registrations_ignored_for_testing(false); |
| 67 mdm->RegisterDumpProvider(mdp, name, std::move(task_runner), options); |
| 68 mdm->set_dumper_registrations_ignored_for_testing(true); |
| 69 } |
| 70 |
53 void RegisterDumpProvider( | 71 void RegisterDumpProvider( |
54 MemoryDumpProvider* mdp, | 72 MemoryDumpProvider* mdp, |
55 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 73 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
56 const MemoryDumpProvider::Options& options) { | 74 const MemoryDumpProvider::Options& options) { |
57 MemoryDumpManager* mdm = MemoryDumpManager::GetInstance(); | 75 RegisterDumpProvider(mdp, kMDPName, task_runner, options); |
58 mdm->set_dumper_registrations_ignored_for_testing(false); | |
59 const char* kMDPName = "TestDumpProvider"; | |
60 mdm->RegisterDumpProvider(mdp, kMDPName, std::move(task_runner), options); | |
61 mdm->set_dumper_registrations_ignored_for_testing(true); | |
62 } | 76 } |
63 | 77 |
64 void RegisterDumpProvider(MemoryDumpProvider* mdp) { | 78 void RegisterDumpProvider(MemoryDumpProvider* mdp) { |
65 RegisterDumpProvider(mdp, nullptr, MemoryDumpProvider::Options()); | 79 RegisterDumpProvider(mdp, nullptr, MemoryDumpProvider::Options()); |
66 } | 80 } |
67 | 81 |
68 void RegisterDumpProviderWithSequencedTaskRunner( | 82 void RegisterDumpProviderWithSequencedTaskRunner( |
69 MemoryDumpProvider* mdp, | 83 MemoryDumpProvider* mdp, |
70 scoped_refptr<base::SequencedTaskRunner> task_runner, | 84 scoped_refptr<base::SequencedTaskRunner> task_runner, |
71 const MemoryDumpProvider::Options& options) { | 85 const MemoryDumpProvider::Options& options) { |
72 MemoryDumpManager* mdm = MemoryDumpManager::GetInstance(); | 86 MemoryDumpManager* mdm = MemoryDumpManager::GetInstance(); |
73 mdm->set_dumper_registrations_ignored_for_testing(false); | 87 mdm->set_dumper_registrations_ignored_for_testing(false); |
74 const char* kMDPName = "TestDumpProvider"; | |
75 mdm->RegisterDumpProviderWithSequencedTaskRunner(mdp, kMDPName, task_runner, | 88 mdm->RegisterDumpProviderWithSequencedTaskRunner(mdp, kMDPName, task_runner, |
76 options); | 89 options); |
77 mdm->set_dumper_registrations_ignored_for_testing(true); | 90 mdm->set_dumper_registrations_ignored_for_testing(true); |
78 } | 91 } |
79 | 92 |
80 void OnTraceDataCollected(Closure quit_closure, | 93 void OnTraceDataCollected(Closure quit_closure, |
81 trace_event::TraceResultBuffer* buffer, | 94 trace_event::TraceResultBuffer* buffer, |
82 const scoped_refptr<RefCountedString>& json, | 95 const scoped_refptr<RefCountedString>& json, |
83 bool has_more_events) { | 96 bool has_more_events) { |
84 buffer->AddFragment(json->data()); | 97 buffer->AddFragment(json->data()); |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 } | 242 } |
230 | 243 |
231 void EnableTracingWithTraceConfig(const std::string& trace_config) { | 244 void EnableTracingWithTraceConfig(const std::string& trace_config) { |
232 TraceLog::GetInstance()->SetEnabled(TraceConfig(trace_config), | 245 TraceLog::GetInstance()->SetEnabled(TraceConfig(trace_config), |
233 TraceLog::RECORDING_MODE); | 246 TraceLog::RECORDING_MODE); |
234 } | 247 } |
235 | 248 |
236 void DisableTracing() { TraceLog::GetInstance()->SetDisabled(); } | 249 void DisableTracing() { TraceLog::GetInstance()->SetDisabled(); } |
237 | 250 |
238 bool IsPeriodicDumpingEnabled() const { | 251 bool IsPeriodicDumpingEnabled() const { |
239 return mdm_->periodic_dump_timer_.IsRunning(); | 252 return mdm_->periodic_dump_helper_.get(); |
240 } | 253 } |
241 | 254 |
242 int GetMaxConsecutiveFailuresCount() const { | 255 int GetMaxConsecutiveFailuresCount() const { |
243 return MemoryDumpManager::kMaxConsecutiveFailuresCount; | 256 return MemoryDumpManager::kMaxConsecutiveFailuresCount; |
244 } | 257 } |
245 | 258 |
246 const MemoryDumpProvider::Options kDefaultOptions; | 259 const MemoryDumpProvider::Options kDefaultOptions; |
247 std::unique_ptr<MemoryDumpManager> mdm_; | 260 std::unique_ptr<MemoryDumpManager> mdm_; |
248 std::unique_ptr<MemoryDumpManagerDelegateForTesting> delegate_; | 261 std::unique_ptr<MemoryDumpManagerDelegateForTesting> delegate_; |
249 bool last_callback_success_; | 262 bool last_callback_success_; |
(...skipping 842 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1092 | 1105 |
1093 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); | 1106 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); |
1094 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(2); | 1107 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(2); |
1095 for (int i = 0; i < 2; ++i) { | 1108 for (int i = 0; i < 2; ++i) { |
1096 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, | 1109 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, |
1097 MemoryDumpLevelOfDetail::DETAILED); | 1110 MemoryDumpLevelOfDetail::DETAILED); |
1098 } | 1111 } |
1099 DisableTracing(); | 1112 DisableTracing(); |
1100 } | 1113 } |
1101 | 1114 |
| 1115 TEST_F(MemoryDumpManagerTest, TestWhitelistingMDP) { |
| 1116 InitializeMemoryDumpManager(false /* is_coordinator */); |
| 1117 std::unique_ptr<MockMemoryDumpProvider> mdp1(new MockMemoryDumpProvider); |
| 1118 RegisterDumpProvider(mdp1.get()); |
| 1119 std::unique_ptr<MockMemoryDumpProvider> mdp2(new MockMemoryDumpProvider); |
| 1120 RegisterDumpProvider(mdp2.get(), kWhitelistedMDPName, nullptr, |
| 1121 kDefaultOptions); |
| 1122 |
| 1123 EXPECT_CALL(*mdp1, OnMemoryDump(_, _)).Times(0); |
| 1124 EXPECT_CALL(*mdp2, OnMemoryDump(_, _)).Times(1).WillOnce(Return(true)); |
| 1125 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); |
| 1126 |
| 1127 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); |
| 1128 EXPECT_FALSE(IsPeriodicDumpingEnabled()); |
| 1129 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, |
| 1130 MemoryDumpLevelOfDetail::BACKGROUND); |
| 1131 DisableTracing(); |
| 1132 } |
| 1133 |
| 1134 TEST_F(MemoryDumpManagerTest, TestBackgroundTracingSetup) { |
| 1135 InitializeMemoryDumpManager(true /* is_coordinator */); |
| 1136 |
| 1137 RunLoop run_loop; |
| 1138 auto quit_closure = run_loop.QuitClosure(); |
| 1139 |
| 1140 testing::InSequence sequence; |
| 1141 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(IsBackgroundDump(), _)) |
| 1142 .Times(5); |
| 1143 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(IsBackgroundDump(), _)) |
| 1144 .WillOnce(Invoke([quit_closure](const MemoryDumpRequestArgs& args, |
| 1145 const MemoryDumpCallback& callback) { |
| 1146 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure); |
| 1147 })); |
| 1148 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(AnyNumber()); |
| 1149 |
| 1150 EnableTracingWithTraceConfig( |
| 1151 TraceConfigMemoryTestUtil::GetTraceConfig_BackgroundTrigger()); |
| 1152 ASSERT_TRUE(IsPeriodicDumpingEnabled()); |
| 1153 run_loop.Run(); |
| 1154 DisableTracing(); |
| 1155 } |
| 1156 |
1102 } // namespace trace_event | 1157 } // namespace trace_event |
1103 } // namespace base | 1158 } // namespace base |
OLD | NEW |