| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_peak_detector.h" | 5 #include "base/trace_event/memory_peak_detector.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
| 13 #include "base/test/test_timeouts.h" | |
| 14 #include "base/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
| 15 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "base/trace_event/memory_dump_provider.h" | 16 #include "base/trace_event/memory_dump_provider.h" |
| 18 #include "base/trace_event/memory_dump_provider_info.h" | 17 #include "base/trace_event/memory_dump_provider_info.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 20 |
| 22 using ::testing::_; | 21 using ::testing::_; |
| 23 using ::testing::Invoke; | 22 using ::testing::Invoke; |
| 24 using ::testing::Return; | 23 using ::testing::Return; |
| 25 | 24 |
| 26 namespace base { | 25 namespace base { |
| 27 namespace trace_event { | 26 namespace trace_event { |
| 28 | 27 |
| 29 namespace { | 28 namespace { |
| 30 | 29 |
| 30 const TimeDelta kMs = TimeDelta::FromMilliseconds(1); |
| 31 const MemoryPeakDetector::Config kConfigNoCallbacks = { |
| 32 1 /* polling_interval_ms */, 60000 /* min_time_between_peaks_ms */, |
| 33 false /* enable_verbose_poll_tracing */ |
| 34 }; |
| 35 |
| 31 class MockMemoryDumpProvider : public MemoryDumpProvider { | 36 class MockMemoryDumpProvider : public MemoryDumpProvider { |
| 32 public: | 37 public: |
| 33 bool OnMemoryDump(const MemoryDumpArgs&, ProcessMemoryDump*) override { | 38 bool OnMemoryDump(const MemoryDumpArgs&, ProcessMemoryDump*) override { |
| 34 NOTREACHED(); | 39 NOTREACHED(); |
| 35 return true; | 40 return true; |
| 36 } | 41 } |
| 37 | 42 |
| 38 MOCK_METHOD1(PollFastMemoryTotal, void(uint64_t*)); | 43 MOCK_METHOD1(PollFastMemoryTotal, void(uint64_t*)); |
| 39 }; | 44 }; |
| 40 | 45 |
| 41 // Wrapper to use gmock on a callback. | 46 // Wrapper to use gmock on a callback. |
| 42 struct OnPeakDetectedWrapper { | 47 struct OnPeakDetectedWrapper { |
| 43 MOCK_METHOD0(OnPeak, void()); | 48 MOCK_METHOD0(OnPeak, void()); |
| 44 }; | 49 }; |
| 45 | 50 |
| 46 } // namespace | 51 } // namespace |
| 47 | 52 |
| 48 class MemoryPeakDetectorTest : public testing::Test { | 53 class MemoryPeakDetectorTest : public testing::Test { |
| 49 public: | 54 public: |
| 50 struct FriendDeleter { | 55 struct FriendDeleter { |
| 51 void operator()(MemoryPeakDetector* inst) { delete inst; } | 56 void operator()(MemoryPeakDetector* inst) { delete inst; } |
| 52 }; | 57 }; |
| 53 | 58 |
| 54 MemoryPeakDetectorTest() : testing::Test() {} | 59 MemoryPeakDetectorTest() : testing::Test() {} |
| 60 static constexpr uint64_t kSlidingWindowNumSamples = |
| 61 MemoryPeakDetector::kSlidingWindowNumSamples; |
| 55 | 62 |
| 56 std::unique_ptr<MemoryPeakDetector, FriendDeleter> NewInstance() { | 63 std::unique_ptr<MemoryPeakDetector, FriendDeleter> NewInstance() { |
| 57 return std::unique_ptr<MemoryPeakDetector, FriendDeleter>( | 64 return std::unique_ptr<MemoryPeakDetector, FriendDeleter>( |
| 58 new MemoryPeakDetector()); | 65 new MemoryPeakDetector()); |
| 59 } | 66 } |
| 60 | 67 |
| 61 void RestartThreadAndReinitializePeakDetector() { | 68 void RestartThreadAndReinitializePeakDetector() { |
| 62 bg_thread_.reset(new Thread("Peak Detector Test Thread")); | 69 bg_thread_.reset(new Thread("Peak Detector Test Thread")); |
| 63 bg_thread_->Start(); | 70 bg_thread_->Start(); |
| 64 peak_detector_ = NewInstance(); | 71 peak_detector_ = NewInstance(); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 | 117 |
| 111 WaitableEvent evt(WaitableEvent::ResetPolicy::MANUAL, | 118 WaitableEvent evt(WaitableEvent::ResetPolicy::MANUAL, |
| 112 WaitableEvent::InitialState::NOT_SIGNALED); | 119 WaitableEvent::InitialState::NOT_SIGNALED); |
| 113 bg_thread_->task_runner()->PostTask( | 120 bg_thread_->task_runner()->PostTask( |
| 114 FROM_HERE, Bind(get_fn, Unretained(&*peak_detector_), Unretained(&evt), | 121 FROM_HERE, Bind(get_fn, Unretained(&*peak_detector_), Unretained(&evt), |
| 115 Unretained(&res))); | 122 Unretained(&res))); |
| 116 evt.Wait(); | 123 evt.Wait(); |
| 117 return res; | 124 return res; |
| 118 } | 125 } |
| 119 | 126 |
| 127 // Runs the peak detector with a mock MDP with the given |
| 128 // |config|. The mock MDP will invoke the |poll_function| on any call to |
| 129 // PollFastMemoryTotal(), until |num_samples| have been polled. |
| 130 // It returns the number of peaks detected. |
| 131 uint32_t RunWithCustomPollFunction( |
| 132 MemoryPeakDetector::Config config, |
| 133 uint32_t num_samples, |
| 134 RepeatingCallback<uint64_t(uint32_t)> poll_function) { |
| 135 WaitableEvent evt(WaitableEvent::ResetPolicy::MANUAL, |
| 136 WaitableEvent::InitialState::NOT_SIGNALED); |
| 137 scoped_refptr<MemoryDumpProviderInfo> mdp = CreateMockDumpProvider(); |
| 138 dump_providers_.push_back(mdp); |
| 139 uint32_t cur_sample_idx = 0; |
| 140 EXPECT_CALL(GetMockMDP(mdp), PollFastMemoryTotal(_)) |
| 141 .WillRepeatedly(Invoke( |
| 142 [&cur_sample_idx, &evt, poll_function, num_samples](uint64_t* mem) { |
| 143 if (cur_sample_idx >= num_samples) { |
| 144 *mem = 1; |
| 145 evt.Signal(); |
| 146 } else { |
| 147 *mem = poll_function.Run(cur_sample_idx++); |
| 148 } |
| 149 })); |
| 150 |
| 151 uint32_t num_peaks = 0; |
| 152 EXPECT_CALL(on_peak_callback_, OnPeak()) |
| 153 .WillRepeatedly(Invoke([&num_peaks] { num_peaks++; })); |
| 154 peak_detector_->Start(config); |
| 155 evt.Wait(); // Wait for |num_samples| invocations of PollFastMemoryTotal(). |
| 156 peak_detector_->Stop(); |
| 157 EXPECT_EQ(num_samples, cur_sample_idx); |
| 158 EXPECT_EQ(MemoryPeakDetector::DISABLED, GetPeakDetectorState()); |
| 159 return num_peaks; |
| 160 } |
| 161 |
| 120 // Called on the |bg_thread_|. | 162 // Called on the |bg_thread_|. |
| 121 void MockGetDumpProviders(MemoryPeakDetector::DumpProvidersList* mdps) { | 163 void MockGetDumpProviders(MemoryPeakDetector::DumpProvidersList* mdps) { |
| 122 get_mdp_call_count_++; | 164 get_mdp_call_count_++; |
| 123 *mdps = dump_providers_; | 165 *mdps = dump_providers_; |
| 124 } | 166 } |
| 125 | 167 |
| 126 uint32_t GetNumGetDumpProvidersCalls() { | 168 uint32_t GetNumGetDumpProvidersCalls() { |
| 127 bg_thread_->FlushForTesting(); | 169 bg_thread_->FlushForTesting(); |
| 128 return get_mdp_call_count_; | 170 return get_mdp_call_count_; |
| 129 } | 171 } |
| 130 | 172 |
| 131 scoped_refptr<MemoryDumpProviderInfo> CreateMockDumpProvider() { | 173 scoped_refptr<MemoryDumpProviderInfo> CreateMockDumpProvider() { |
| 132 std::unique_ptr<MockMemoryDumpProvider> mdp(new MockMemoryDumpProvider()); | 174 std::unique_ptr<MockMemoryDumpProvider> mdp(new MockMemoryDumpProvider()); |
| 133 MemoryDumpProvider::Options opt; | 175 MemoryDumpProvider::Options opt; |
| 134 opt.is_fast_polling_supported = true; | 176 opt.is_fast_polling_supported = true; |
| 135 scoped_refptr<MemoryDumpProviderInfo> mdp_info( | 177 scoped_refptr<MemoryDumpProviderInfo> mdp_info( |
| 136 new MemoryDumpProviderInfo(mdp.get(), "Mock MDP", nullptr, opt, false)); | 178 new MemoryDumpProviderInfo(mdp.get(), "Mock MDP", nullptr, opt, false)); |
| 137 | 179 |
| 138 // The |mdp| instance will be destroyed together with the |mdp_info|. | 180 // The |mdp| instance will be destroyed together with the |mdp_info|. |
| 139 mdp_info->owned_dump_provider = std::move(mdp); | 181 mdp_info->owned_dump_provider = std::move(mdp); |
| 140 return mdp_info; | 182 return mdp_info; |
| 141 } | 183 } |
| 142 | 184 |
| 143 static MockMemoryDumpProvider& GetMockMDP( | 185 static MockMemoryDumpProvider& GetMockMDP( |
| 144 const scoped_refptr<MemoryDumpProviderInfo>& mdp_info) { | 186 const scoped_refptr<MemoryDumpProviderInfo>& mdp_info) { |
| 145 return *static_cast<MockMemoryDumpProvider*>(mdp_info->dump_provider); | 187 return *static_cast<MockMemoryDumpProvider*>(mdp_info->dump_provider); |
| 146 } | 188 } |
| 147 | 189 |
| 190 static uint64_t PollFunctionThatCausesPeakViaStdDev(uint32_t sample_idx) { |
| 191 // Start with a baseline of 50 MB. |
| 192 if (sample_idx < kSlidingWindowNumSamples) |
| 193 return 50000 + (sample_idx % 3) * 100; |
| 194 |
| 195 // Then 10 samples around 80 MB |
| 196 if (sample_idx < 10 + kSlidingWindowNumSamples) |
| 197 return 80000 + (sample_idx % 3) * 200; |
| 198 |
| 199 // Than back to 60 MB. |
| 200 if (sample_idx < 2 * kSlidingWindowNumSamples) |
| 201 return 60000 + (sample_idx % 3) * 100; |
| 202 |
| 203 // Then 20 samples around 120 MB. |
| 204 if (sample_idx < 20 + 2 * kSlidingWindowNumSamples) |
| 205 return 120000 + (sample_idx % 3) * 200; |
| 206 |
| 207 // Then back to idle to around 50 MB until the end. |
| 208 return 50000 + (sample_idx % 3) * 100; |
| 209 } |
| 210 |
| 148 protected: | 211 protected: |
| 149 MemoryPeakDetector::DumpProvidersList dump_providers_; | 212 MemoryPeakDetector::DumpProvidersList dump_providers_; |
| 150 uint32_t get_mdp_call_count_; | 213 uint32_t get_mdp_call_count_; |
| 151 std::unique_ptr<MemoryPeakDetector, FriendDeleter> peak_detector_; | 214 std::unique_ptr<MemoryPeakDetector, FriendDeleter> peak_detector_; |
| 152 std::unique_ptr<Thread> bg_thread_; | 215 std::unique_ptr<Thread> bg_thread_; |
| 153 OnPeakDetectedWrapper on_peak_callback_; | 216 OnPeakDetectedWrapper on_peak_callback_; |
| 154 }; | 217 }; |
| 155 | 218 |
| 156 TEST_F(MemoryPeakDetectorTest, GetDumpProvidersFunctionCalled) { | 219 TEST_F(MemoryPeakDetectorTest, GetDumpProvidersFunctionCalled) { |
| 157 EXPECT_EQ(MemoryPeakDetector::DISABLED, GetPeakDetectorState()); | 220 EXPECT_EQ(MemoryPeakDetector::DISABLED, GetPeakDetectorState()); |
| 158 peak_detector_->Start(); | 221 peak_detector_->Start(kConfigNoCallbacks); |
| 159 EXPECT_EQ(1u, GetNumGetDumpProvidersCalls()); | 222 EXPECT_EQ(1u, GetNumGetDumpProvidersCalls()); |
| 160 EXPECT_EQ(MemoryPeakDetector::ENABLED, GetPeakDetectorState()); | 223 EXPECT_EQ(MemoryPeakDetector::ENABLED, GetPeakDetectorState()); |
| 161 | 224 |
| 162 peak_detector_->Stop(); | 225 peak_detector_->Stop(); |
| 163 EXPECT_EQ(MemoryPeakDetector::DISABLED, GetPeakDetectorState()); | 226 EXPECT_EQ(MemoryPeakDetector::DISABLED, GetPeakDetectorState()); |
| 164 EXPECT_EQ(0u, GetNumPollingTasksRan()); | 227 EXPECT_EQ(0u, GetNumPollingTasksRan()); |
| 165 } | 228 } |
| 166 | 229 |
| 167 TEST_F(MemoryPeakDetectorTest, NotifyBeforeInitialize) { | 230 TEST_F(MemoryPeakDetectorTest, ClearAndNotifyBeforeInitialize) { |
| 168 peak_detector_->TearDown(); | 231 peak_detector_->TearDown(); |
| 169 | 232 |
| 170 WaitableEvent evt(WaitableEvent::ResetPolicy::MANUAL, | 233 WaitableEvent evt(WaitableEvent::ResetPolicy::MANUAL, |
| 171 WaitableEvent::InitialState::NOT_SIGNALED); | 234 WaitableEvent::InitialState::NOT_SIGNALED); |
| 172 scoped_refptr<MemoryDumpProviderInfo> mdp = CreateMockDumpProvider(); | 235 scoped_refptr<MemoryDumpProviderInfo> mdp = CreateMockDumpProvider(); |
| 173 EXPECT_CALL(GetMockMDP(mdp), PollFastMemoryTotal(_)) | 236 EXPECT_CALL(GetMockMDP(mdp), PollFastMemoryTotal(_)) |
| 174 .WillRepeatedly(Invoke([&evt](uint64_t*) { evt.Signal(); })); | 237 .WillRepeatedly(Invoke([&evt](uint64_t*) { evt.Signal(); })); |
| 175 dump_providers_.push_back(mdp); | 238 dump_providers_.push_back(mdp); |
| 239 peak_detector_->Clear(); |
| 176 peak_detector_->NotifyMemoryDumpProvidersChanged(); | 240 peak_detector_->NotifyMemoryDumpProvidersChanged(); |
| 177 EXPECT_EQ(MemoryPeakDetector::NOT_INITIALIZED, GetPeakDetectorState()); | 241 EXPECT_EQ(MemoryPeakDetector::NOT_INITIALIZED, GetPeakDetectorState()); |
| 178 RestartThreadAndReinitializePeakDetector(); | 242 RestartThreadAndReinitializePeakDetector(); |
| 179 | 243 |
| 180 peak_detector_->Start(); | 244 peak_detector_->Start(kConfigNoCallbacks); |
| 181 EXPECT_EQ(MemoryPeakDetector::RUNNING, GetPeakDetectorState()); | 245 EXPECT_EQ(MemoryPeakDetector::RUNNING, GetPeakDetectorState()); |
| 182 evt.Wait(); // Wait for a PollFastMemoryTotal() call. | 246 evt.Wait(); // Wait for a PollFastMemoryTotal() call. |
| 183 | 247 |
| 184 peak_detector_->Stop(); | 248 peak_detector_->Stop(); |
| 185 EXPECT_EQ(MemoryPeakDetector::DISABLED, GetPeakDetectorState()); | 249 EXPECT_EQ(MemoryPeakDetector::DISABLED, GetPeakDetectorState()); |
| 186 EXPECT_EQ(1u, GetNumGetDumpProvidersCalls()); | 250 EXPECT_EQ(1u, GetNumGetDumpProvidersCalls()); |
| 187 EXPECT_GE(GetNumPollingTasksRan(), 1u); | 251 EXPECT_GE(GetNumPollingTasksRan(), 1u); |
| 188 } | 252 } |
| 189 | 253 |
| 190 TEST_F(MemoryPeakDetectorTest, DoubleStop) { | 254 TEST_F(MemoryPeakDetectorTest, DoubleStop) { |
| 191 peak_detector_->Start(); | 255 peak_detector_->Start(kConfigNoCallbacks); |
| 192 EXPECT_EQ(MemoryPeakDetector::ENABLED, GetPeakDetectorState()); | 256 EXPECT_EQ(MemoryPeakDetector::ENABLED, GetPeakDetectorState()); |
| 193 | 257 |
| 194 peak_detector_->Stop(); | 258 peak_detector_->Stop(); |
| 195 EXPECT_EQ(MemoryPeakDetector::DISABLED, GetPeakDetectorState()); | 259 EXPECT_EQ(MemoryPeakDetector::DISABLED, GetPeakDetectorState()); |
| 196 | 260 |
| 197 peak_detector_->Stop(); | 261 peak_detector_->Stop(); |
| 198 EXPECT_EQ(MemoryPeakDetector::DISABLED, GetPeakDetectorState()); | 262 EXPECT_EQ(MemoryPeakDetector::DISABLED, GetPeakDetectorState()); |
| 199 | 263 |
| 200 EXPECT_EQ(1u, GetNumGetDumpProvidersCalls()); | 264 EXPECT_EQ(1u, GetNumGetDumpProvidersCalls()); |
| 201 EXPECT_EQ(0u, GetNumPollingTasksRan()); | 265 EXPECT_EQ(0u, GetNumPollingTasksRan()); |
| 202 } | 266 } |
| 203 | 267 |
| 204 TEST_F(MemoryPeakDetectorTest, OneDumpProviderRegisteredBeforeStart) { | 268 TEST_F(MemoryPeakDetectorTest, OneDumpProviderRegisteredBeforeStart) { |
| 205 WaitableEvent evt(WaitableEvent::ResetPolicy::MANUAL, | 269 WaitableEvent evt(WaitableEvent::ResetPolicy::MANUAL, |
| 206 WaitableEvent::InitialState::NOT_SIGNALED); | 270 WaitableEvent::InitialState::NOT_SIGNALED); |
| 207 scoped_refptr<MemoryDumpProviderInfo> mdp = CreateMockDumpProvider(); | 271 scoped_refptr<MemoryDumpProviderInfo> mdp = CreateMockDumpProvider(); |
| 208 EXPECT_CALL(GetMockMDP(mdp), PollFastMemoryTotal(_)) | 272 EXPECT_CALL(GetMockMDP(mdp), PollFastMemoryTotal(_)) |
| 209 .WillRepeatedly(Invoke([&evt](uint64_t*) { evt.Signal(); })); | 273 .WillRepeatedly(Invoke([&evt](uint64_t*) { evt.Signal(); })); |
| 210 dump_providers_.push_back(mdp); | 274 dump_providers_.push_back(mdp); |
| 211 | 275 |
| 212 peak_detector_->Start(); | 276 peak_detector_->Start(kConfigNoCallbacks); |
| 213 evt.Wait(); // Signaled when PollFastMemoryTotal() is called on the MockMDP. | 277 evt.Wait(); // Signaled when PollFastMemoryTotal() is called on the MockMDP. |
| 214 EXPECT_EQ(MemoryPeakDetector::RUNNING, GetPeakDetectorState()); | 278 EXPECT_EQ(MemoryPeakDetector::RUNNING, GetPeakDetectorState()); |
| 215 | 279 |
| 216 peak_detector_->Stop(); | 280 peak_detector_->Stop(); |
| 217 EXPECT_EQ(MemoryPeakDetector::DISABLED, GetPeakDetectorState()); | 281 EXPECT_EQ(MemoryPeakDetector::DISABLED, GetPeakDetectorState()); |
| 218 EXPECT_EQ(1u, GetNumGetDumpProvidersCalls()); | 282 EXPECT_EQ(1u, GetNumGetDumpProvidersCalls()); |
| 219 EXPECT_GT(GetNumPollingTasksRan(), 0u); | 283 EXPECT_GT(GetNumPollingTasksRan(), 0u); |
| 220 } | 284 } |
| 221 | 285 |
| 222 TEST_F(MemoryPeakDetectorTest, ReInitializeAndRebindToNewThread) { | 286 TEST_F(MemoryPeakDetectorTest, ReInitializeAndRebindToNewThread) { |
| 223 WaitableEvent evt(WaitableEvent::ResetPolicy::MANUAL, | 287 WaitableEvent evt(WaitableEvent::ResetPolicy::MANUAL, |
| 224 WaitableEvent::InitialState::NOT_SIGNALED); | 288 WaitableEvent::InitialState::NOT_SIGNALED); |
| 225 scoped_refptr<MemoryDumpProviderInfo> mdp = CreateMockDumpProvider(); | 289 scoped_refptr<MemoryDumpProviderInfo> mdp = CreateMockDumpProvider(); |
| 226 EXPECT_CALL(GetMockMDP(mdp), PollFastMemoryTotal(_)) | 290 EXPECT_CALL(GetMockMDP(mdp), PollFastMemoryTotal(_)) |
| 227 .WillRepeatedly(Invoke([&evt](uint64_t*) { evt.Signal(); })); | 291 .WillRepeatedly(Invoke([&evt](uint64_t*) { evt.Signal(); })); |
| 228 dump_providers_.push_back(mdp); | 292 dump_providers_.push_back(mdp); |
| 229 | 293 |
| 230 for (int i = 0; i < 5; ++i) { | 294 for (int i = 0; i < 5; ++i) { |
| 231 evt.Reset(); | 295 evt.Reset(); |
| 232 peak_detector_->Start(); | 296 peak_detector_->Start(kConfigNoCallbacks); |
| 233 evt.Wait(); // Wait for a PollFastMemoryTotal() call. | 297 evt.Wait(); // Wait for a PollFastMemoryTotal() call. |
| 234 // Check that calling TearDown implicitly does a Stop(). | 298 // Check that calling TearDown implicitly does a Stop(). |
| 235 peak_detector_->TearDown(); | 299 peak_detector_->TearDown(); |
| 236 | 300 |
| 237 // Reinitialize and re-bind to a new task runner. | 301 // Reinitialize and re-bind to a new task runner. |
| 238 RestartThreadAndReinitializePeakDetector(); | 302 RestartThreadAndReinitializePeakDetector(); |
| 239 } | 303 } |
| 240 } | 304 } |
| 241 | 305 |
| 242 TEST_F(MemoryPeakDetectorTest, OneDumpProviderRegisteredOutOfBand) { | 306 TEST_F(MemoryPeakDetectorTest, OneDumpProviderRegisteredOutOfBand) { |
| 243 peak_detector_->Start(); | 307 peak_detector_->Start(kConfigNoCallbacks); |
| 244 EXPECT_EQ(MemoryPeakDetector::ENABLED, GetPeakDetectorState()); | 308 EXPECT_EQ(MemoryPeakDetector::ENABLED, GetPeakDetectorState()); |
| 245 EXPECT_EQ(1u, GetNumGetDumpProvidersCalls()); | 309 EXPECT_EQ(1u, GetNumGetDumpProvidersCalls()); |
| 246 | 310 |
| 247 // Check that no poll tasks are posted before any dump provider is registered. | 311 // Check that no poll tasks are posted before any dump provider is registered. |
| 248 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | 312 PlatformThread::Sleep(5 * kConfigNoCallbacks.polling_interval_ms * kMs); |
| 249 EXPECT_EQ(0u, GetNumPollingTasksRan()); | 313 EXPECT_EQ(0u, GetNumPollingTasksRan()); |
| 250 | 314 |
| 251 // Registed the MDP After Start() has been issued and expect that the | 315 // Registed the MDP After Start() has been issued and expect that the |
| 252 // PeakDetector transitions ENABLED -> RUNNING on the next | 316 // PeakDetector transitions ENABLED -> RUNNING on the next |
| 253 // NotifyMemoryDumpProvidersChanged() call. | 317 // NotifyMemoryDumpProvidersChanged() call. |
| 254 WaitableEvent evt(WaitableEvent::ResetPolicy::MANUAL, | 318 WaitableEvent evt(WaitableEvent::ResetPolicy::MANUAL, |
| 255 WaitableEvent::InitialState::NOT_SIGNALED); | 319 WaitableEvent::InitialState::NOT_SIGNALED); |
| 256 scoped_refptr<MemoryDumpProviderInfo> mdp = CreateMockDumpProvider(); | 320 scoped_refptr<MemoryDumpProviderInfo> mdp = CreateMockDumpProvider(); |
| 257 EXPECT_CALL(GetMockMDP(mdp), PollFastMemoryTotal(_)) | 321 EXPECT_CALL(GetMockMDP(mdp), PollFastMemoryTotal(_)) |
| 258 .WillRepeatedly(Invoke([&evt](uint64_t*) { evt.Signal(); })); | 322 .WillRepeatedly(Invoke([&evt](uint64_t*) { evt.Signal(); })); |
| 259 dump_providers_.push_back(mdp); | 323 dump_providers_.push_back(mdp); |
| 260 peak_detector_->NotifyMemoryDumpProvidersChanged(); | 324 peak_detector_->NotifyMemoryDumpProvidersChanged(); |
| 261 | 325 |
| 262 evt.Wait(); // Signaled when PollFastMemoryTotal() is called on the MockMDP. | 326 evt.Wait(); // Signaled when PollFastMemoryTotal() is called on the MockMDP. |
| 263 EXPECT_EQ(MemoryPeakDetector::RUNNING, GetPeakDetectorState()); | 327 EXPECT_EQ(MemoryPeakDetector::RUNNING, GetPeakDetectorState()); |
| 264 EXPECT_EQ(2u, GetNumGetDumpProvidersCalls()); | 328 EXPECT_EQ(2u, GetNumGetDumpProvidersCalls()); |
| 265 | 329 |
| 266 // Now simulate the unregisration and expect that the PeakDetector transitions | 330 // Now simulate the unregisration and expect that the PeakDetector transitions |
| 267 // back to ENABLED. | 331 // back to ENABLED. |
| 268 dump_providers_.clear(); | 332 dump_providers_.clear(); |
| 269 peak_detector_->NotifyMemoryDumpProvidersChanged(); | 333 peak_detector_->NotifyMemoryDumpProvidersChanged(); |
| 270 EXPECT_EQ(MemoryPeakDetector::ENABLED, GetPeakDetectorState()); | 334 EXPECT_EQ(MemoryPeakDetector::ENABLED, GetPeakDetectorState()); |
| 271 EXPECT_EQ(3u, GetNumGetDumpProvidersCalls()); | 335 EXPECT_EQ(3u, GetNumGetDumpProvidersCalls()); |
| 272 uint32_t num_poll_tasks = GetNumPollingTasksRan(); | 336 uint32_t num_poll_tasks = GetNumPollingTasksRan(); |
| 273 EXPECT_GT(num_poll_tasks, 0u); | 337 EXPECT_GT(num_poll_tasks, 0u); |
| 274 | 338 |
| 275 // At this point, no more polling tasks should be posted. | 339 // At this point, no more polling tasks should be posted. |
| 276 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | 340 PlatformThread::Sleep(5 * kConfigNoCallbacks.polling_interval_ms * kMs); |
| 277 peak_detector_->Stop(); | 341 peak_detector_->Stop(); |
| 278 EXPECT_EQ(MemoryPeakDetector::DISABLED, GetPeakDetectorState()); | 342 EXPECT_EQ(MemoryPeakDetector::DISABLED, GetPeakDetectorState()); |
| 279 EXPECT_EQ(num_poll_tasks, GetNumPollingTasksRan()); | 343 EXPECT_EQ(num_poll_tasks, GetNumPollingTasksRan()); |
| 280 } | 344 } |
| 281 | 345 |
| 282 // Test that a sequence of Start()/Stop() back-to-back doesn't end up creating | 346 // Test that a sequence of Start()/Stop() back-to-back doesn't end up creating |
| 283 // several outstanding timer tasks and instead respects the polling_interval_ms. | 347 // several outstanding timer tasks and instead respects the polling_interval_ms. |
| 284 TEST_F(MemoryPeakDetectorTest, StartStopQuickly) { | 348 TEST_F(MemoryPeakDetectorTest, StartStopQuickly) { |
| 285 WaitableEvent evt(WaitableEvent::ResetPolicy::MANUAL, | 349 WaitableEvent evt(WaitableEvent::ResetPolicy::MANUAL, |
| 286 WaitableEvent::InitialState::NOT_SIGNALED); | 350 WaitableEvent::InitialState::NOT_SIGNALED); |
| 287 scoped_refptr<MemoryDumpProviderInfo> mdp = CreateMockDumpProvider(); | 351 scoped_refptr<MemoryDumpProviderInfo> mdp = CreateMockDumpProvider(); |
| 288 dump_providers_.push_back(mdp); | 352 dump_providers_.push_back(mdp); |
| 289 const uint32_t kNumPolls = 20; | 353 const uint32_t kNumPolls = 20; |
| 290 uint32_t polls_done = 0; | 354 uint32_t polls_done = 0; |
| 291 EXPECT_CALL(GetMockMDP(mdp), PollFastMemoryTotal(_)) | 355 EXPECT_CALL(GetMockMDP(mdp), PollFastMemoryTotal(_)) |
| 292 .WillRepeatedly(Invoke([&polls_done, &evt, kNumPolls](uint64_t*) { | 356 .WillRepeatedly(Invoke([&polls_done, &evt, kNumPolls](uint64_t*) { |
| 293 if (++polls_done == kNumPolls) | 357 if (++polls_done == kNumPolls) |
| 294 evt.Signal(); | 358 evt.Signal(); |
| 295 })); | 359 })); |
| 296 | 360 |
| 297 const TimeTicks tstart = TimeTicks::Now(); | 361 const TimeTicks tstart = TimeTicks::Now(); |
| 298 for (int i = 0; i < 5; i++) { | 362 for (int i = 0; i < 5; i++) { |
| 299 peak_detector_->Start(); | 363 peak_detector_->Start(kConfigNoCallbacks); |
| 300 peak_detector_->Stop(); | 364 peak_detector_->Stop(); |
| 301 } | 365 } |
| 302 peak_detector_->Start(); | 366 peak_detector_->Start(kConfigNoCallbacks); |
| 303 EXPECT_EQ(MemoryPeakDetector::RUNNING, GetPeakDetectorState()); | 367 EXPECT_EQ(MemoryPeakDetector::RUNNING, GetPeakDetectorState()); |
| 304 evt.Wait(); // Wait for kNumPolls. | 368 evt.Wait(); // Wait for kNumPolls. |
| 305 const double time_ms = (TimeTicks::Now() - tstart).InMillisecondsF(); | 369 const double time_ms = (TimeTicks::Now() - tstart).InMillisecondsF(); |
| 306 | 370 |
| 307 // TODO(primiano): this will become config.polling_interval_ms in the next CL. | 371 EXPECT_GE(time_ms, kNumPolls * kConfigNoCallbacks.polling_interval_ms); |
| 308 const uint32_t polling_interval_ms = 1; | |
| 309 EXPECT_GE(time_ms, kNumPolls * polling_interval_ms); | |
| 310 peak_detector_->Stop(); | 372 peak_detector_->Stop(); |
| 311 } | 373 } |
| 312 | 374 |
| 313 TEST_F(MemoryPeakDetectorTest, RegisterAndUnregisterTwoDumpProviders) { | 375 TEST_F(MemoryPeakDetectorTest, RegisterAndUnregisterTwoDumpProviders) { |
| 314 WaitableEvent evt1(WaitableEvent::ResetPolicy::MANUAL, | 376 WaitableEvent evt1(WaitableEvent::ResetPolicy::MANUAL, |
| 315 WaitableEvent::InitialState::NOT_SIGNALED); | 377 WaitableEvent::InitialState::NOT_SIGNALED); |
| 316 WaitableEvent evt2(WaitableEvent::ResetPolicy::MANUAL, | 378 WaitableEvent evt2(WaitableEvent::ResetPolicy::MANUAL, |
| 317 WaitableEvent::InitialState::NOT_SIGNALED); | 379 WaitableEvent::InitialState::NOT_SIGNALED); |
| 318 scoped_refptr<MemoryDumpProviderInfo> mdp1 = CreateMockDumpProvider(); | 380 scoped_refptr<MemoryDumpProviderInfo> mdp1 = CreateMockDumpProvider(); |
| 319 scoped_refptr<MemoryDumpProviderInfo> mdp2 = CreateMockDumpProvider(); | 381 scoped_refptr<MemoryDumpProviderInfo> mdp2 = CreateMockDumpProvider(); |
| 320 EXPECT_CALL(GetMockMDP(mdp1), PollFastMemoryTotal(_)) | 382 EXPECT_CALL(GetMockMDP(mdp1), PollFastMemoryTotal(_)) |
| 321 .WillRepeatedly(Invoke([&evt1](uint64_t*) { evt1.Signal(); })); | 383 .WillRepeatedly(Invoke([&evt1](uint64_t*) { evt1.Signal(); })); |
| 322 EXPECT_CALL(GetMockMDP(mdp2), PollFastMemoryTotal(_)) | 384 EXPECT_CALL(GetMockMDP(mdp2), PollFastMemoryTotal(_)) |
| 323 .WillRepeatedly(Invoke([&evt2](uint64_t*) { evt2.Signal(); })); | 385 .WillRepeatedly(Invoke([&evt2](uint64_t*) { evt2.Signal(); })); |
| 324 | 386 |
| 325 // Register only one MDP and start the detector. | 387 // Register only one MDP and start the detector. |
| 326 dump_providers_.push_back(mdp1); | 388 dump_providers_.push_back(mdp1); |
| 327 peak_detector_->Start(); | 389 peak_detector_->Start(kConfigNoCallbacks); |
| 328 EXPECT_EQ(MemoryPeakDetector::RUNNING, GetPeakDetectorState()); | 390 EXPECT_EQ(MemoryPeakDetector::RUNNING, GetPeakDetectorState()); |
| 329 | 391 |
| 330 // Wait for one poll task and then register also the other one. | 392 // Wait for one poll task and then register also the other one. |
| 331 evt1.Wait(); | 393 evt1.Wait(); |
| 332 dump_providers_.push_back(mdp2); | 394 dump_providers_.push_back(mdp2); |
| 333 peak_detector_->NotifyMemoryDumpProvidersChanged(); | 395 peak_detector_->NotifyMemoryDumpProvidersChanged(); |
| 334 evt2.Wait(); | 396 evt2.Wait(); |
| 335 EXPECT_EQ(MemoryPeakDetector::RUNNING, GetPeakDetectorState()); | 397 EXPECT_EQ(MemoryPeakDetector::RUNNING, GetPeakDetectorState()); |
| 336 | 398 |
| 337 // Now unregister the first MDP and check that everything is still running. | 399 // Now unregister the first MDP and check that everything is still running. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 359 | 421 |
| 360 // Stop everything, tear down the MDPs, restart the detector and check that | 422 // Stop everything, tear down the MDPs, restart the detector and check that |
| 361 // it detector doesn't accidentally try to re-access them. | 423 // it detector doesn't accidentally try to re-access them. |
| 362 peak_detector_->Stop(); | 424 peak_detector_->Stop(); |
| 363 EXPECT_EQ(MemoryPeakDetector::DISABLED, GetPeakDetectorState()); | 425 EXPECT_EQ(MemoryPeakDetector::DISABLED, GetPeakDetectorState()); |
| 364 dump_providers_.clear(); | 426 dump_providers_.clear(); |
| 365 mdp1 = nullptr; | 427 mdp1 = nullptr; |
| 366 mdp2 = nullptr; | 428 mdp2 = nullptr; |
| 367 | 429 |
| 368 num_poll_tasks = GetNumPollingTasksRan(); | 430 num_poll_tasks = GetNumPollingTasksRan(); |
| 369 peak_detector_->Start(); | 431 peak_detector_->Start(kConfigNoCallbacks); |
| 370 EXPECT_EQ(MemoryPeakDetector::ENABLED, GetPeakDetectorState()); | 432 EXPECT_EQ(MemoryPeakDetector::ENABLED, GetPeakDetectorState()); |
| 371 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | 433 PlatformThread::Sleep(5 * kConfigNoCallbacks.polling_interval_ms * kMs); |
| 372 | 434 |
| 373 peak_detector_->Stop(); | 435 peak_detector_->Stop(); |
| 374 EXPECT_EQ(MemoryPeakDetector::DISABLED, GetPeakDetectorState()); | 436 EXPECT_EQ(MemoryPeakDetector::DISABLED, GetPeakDetectorState()); |
| 375 EXPECT_EQ(num_poll_tasks, GetNumPollingTasksRan()); | 437 EXPECT_EQ(num_poll_tasks, GetNumPollingTasksRan()); |
| 376 | 438 |
| 377 EXPECT_EQ(6u, GetNumGetDumpProvidersCalls()); | 439 EXPECT_EQ(6u, GetNumGetDumpProvidersCalls()); |
| 378 } | 440 } |
| 379 | 441 |
| 442 // Tests the behavior of the static threshold detector, which is supposed to |
| 443 // detect a peak whenever an increase >= threshold is detected. |
| 444 TEST_F(MemoryPeakDetectorTest, StaticThreshold) { |
| 445 const uint32_t kNumSamples = 2 * kSlidingWindowNumSamples; |
| 446 constexpr uint32_t kNumSamplesPerStep = 10; |
| 447 constexpr uint64_t kThreshold = 1000000; |
| 448 peak_detector_->SetStaticThresholdForTesting(kThreshold); |
| 449 const MemoryPeakDetector::Config kConfig = { |
| 450 1 /* polling_interval_ms */, 0 /* min_time_between_peaks_ms */, |
| 451 false /* enable_verbose_poll_tracing */ |
| 452 }; |
| 453 |
| 454 // The mocked PollFastMemoryTotal() will return a step function, |
| 455 // e.g. (1, 1, 1, 5, 5, 5, ...) where the steps are 2x threshold, in order to |
| 456 // trigger only the static threshold logic. |
| 457 auto poll_fn = Bind( |
| 458 [](const uint32_t kNumSamplesPerStep, const uint64_t kThreshold, |
| 459 uint32_t sample_idx) -> uint64_t { |
| 460 return (1 + sample_idx / kNumSamplesPerStep) * 2 * kThreshold; |
| 461 }, |
| 462 kNumSamplesPerStep, kThreshold); |
| 463 uint32_t num_peaks = RunWithCustomPollFunction(kConfig, kNumSamples, poll_fn); |
| 464 EXPECT_EQ(kNumSamples / kNumSamplesPerStep - 1, num_peaks); |
| 465 } |
| 466 |
| 467 // Checks the throttling logic of Config's |min_time_between_peaks_ms|. |
| 468 TEST_F(MemoryPeakDetectorTest, PeakCallbackThrottling) { |
| 469 const size_t kNumSamples = 2 * kSlidingWindowNumSamples; |
| 470 constexpr uint64_t kThreshold = 1000000; |
| 471 peak_detector_->SetStaticThresholdForTesting(kThreshold); |
| 472 const MemoryPeakDetector::Config kConfig = { |
| 473 1 /* polling_interval_ms */, 4 /* min_time_between_peaks_ms */, |
| 474 false /* enable_verbose_poll_tracing */ |
| 475 }; |
| 476 |
| 477 // Each mock value returned is N * 2 * threshold, so all of them would be |
| 478 // eligible to be a peak if throttling wasn't enabled. |
| 479 auto poll_fn = Bind( |
| 480 [](uint64_t kThreshold, uint32_t sample_idx) -> uint64_t { |
| 481 return (sample_idx + 1) * 2 * kThreshold; |
| 482 }, |
| 483 kThreshold); |
| 484 uint32_t num_peaks = RunWithCustomPollFunction(kConfig, kNumSamples, poll_fn); |
| 485 const uint32_t kExpectedThrottlingRate = |
| 486 kConfig.min_time_between_peaks_ms / kConfig.polling_interval_ms; |
| 487 EXPECT_LT(num_peaks, kNumSamples / kExpectedThrottlingRate); |
| 488 } |
| 489 |
| 490 TEST_F(MemoryPeakDetectorTest, StdDev) { |
| 491 // Set the threshold to some arbitrarily high value, so that the static |
| 492 // threshold logic is not hit in this test. |
| 493 constexpr uint64_t kThreshold = 1024 * 1024 * 1024; |
| 494 peak_detector_->SetStaticThresholdForTesting(kThreshold); |
| 495 const size_t kNumSamples = 3 * kSlidingWindowNumSamples; |
| 496 const MemoryPeakDetector::Config kConfig = { |
| 497 1 /* polling_interval_ms */, 0 /* min_time_between_peaks_ms */, |
| 498 false /* enable_verbose_poll_tracing */ |
| 499 }; |
| 500 |
| 501 auto poll_fn = Bind(&PollFunctionThatCausesPeakViaStdDev); |
| 502 uint32_t num_peaks = RunWithCustomPollFunction(kConfig, kNumSamples, poll_fn); |
| 503 EXPECT_EQ(2u, num_peaks); // 80 MB, 120 MB. |
| 504 } |
| 505 |
| 506 // Tests that Clear() actually holds back peak notifications. |
| 507 TEST_F(MemoryPeakDetectorTest, Clear) { |
| 508 constexpr uint64_t kThreshold = 1024 * 1024 * 1024; |
| 509 const uint32_t kNumSamples = 3 * kSlidingWindowNumSamples; |
| 510 peak_detector_->SetStaticThresholdForTesting(kThreshold); |
| 511 const MemoryPeakDetector::Config kConfig = { |
| 512 1 /* polling_interval_ms */, 0 /* min_time_between_peaks_ms */, |
| 513 false /* enable_verbose_poll_tracing */ |
| 514 }; |
| 515 |
| 516 auto poll_fn = Bind( |
| 517 [](MemoryPeakDetector* peak_detector, uint32_t sample_idx) -> uint64_t { |
| 518 if (sample_idx % 20 == 20 - 1) |
| 519 peak_detector->Clear(); |
| 520 return PollFunctionThatCausesPeakViaStdDev(sample_idx); |
| 521 }, |
| 522 Unretained(&*peak_detector_)); |
| 523 uint32_t num_peaks = RunWithCustomPollFunction(kConfig, kNumSamples, poll_fn); |
| 524 EXPECT_EQ(0u, num_peaks); |
| 525 } |
| 526 |
| 380 } // namespace trace_event | 527 } // namespace trace_event |
| 381 } // namespace base | 528 } // namespace base |
| OLD | NEW |