| 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_dump_scheduler.h" | 5 #include "base/trace_event/memory_dump_scheduler.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace base { | 12 namespace base { |
| 13 namespace trace_event { | 13 namespace trace_event { |
| 14 | 14 |
| 15 class MemoryDumpSchedulerPollingTest : public testing::Test { | 15 class MemoryDumpSchedulerPollingTest : public testing::Test { |
| 16 public: | 16 public: |
| 17 static const uint32_t kMinPollsToDump = 5; | 17 static const uint32_t kMinPollsToDump = 5; |
| 18 | 18 |
| 19 MemoryDumpSchedulerPollingTest() | 19 MemoryDumpSchedulerPollingTest() |
| 20 : testing::Test(), | 20 : testing::Test(), |
| 21 num_samples_tracked_( | 21 num_samples_tracked_( |
| 22 MemoryDumpScheduler::PollingTriggerState::kMaxNumMemorySamples) {} | 22 MemoryDumpScheduler::PollingTriggerState::kMaxNumMemorySamples) {} |
| 23 | 23 |
| 24 void SetUp() override { | 24 void SetUp() override { |
| 25 MemoryDumpScheduler::SetPollingIntervalForTesting(1); | 25 MemoryDumpScheduler::SetPollingIntervalForTesting(1); |
| 26 uint32_t kMinPollsToDump = 5; | 26 uint32_t kMinPollsToDump = 5; |
| 27 mds_.reset(new MemoryDumpScheduler(nullptr, nullptr)); | 27 mds_ = MemoryDumpScheduler::GetInstance(); |
| 28 mds_->Setup(nullptr, nullptr); |
| 28 mds_->AddTrigger(MemoryDumpType::PEAK_MEMORY_USAGE, | 29 mds_->AddTrigger(MemoryDumpType::PEAK_MEMORY_USAGE, |
| 29 MemoryDumpLevelOfDetail::LIGHT, kMinPollsToDump); | 30 MemoryDumpLevelOfDetail::LIGHT, kMinPollsToDump); |
| 30 mds_->polling_state_.ResetTotals(); | 31 mds_->polling_state_->ResetTotals(); |
| 31 mds_->polling_state_.current_state = | 32 mds_->polling_state_->current_state = |
| 32 MemoryDumpScheduler::PollingTriggerState::ENABLED; | 33 MemoryDumpScheduler::PollingTriggerState::ENABLED; |
| 33 } | 34 } |
| 34 | 35 |
| 35 void TearDown() override { | 36 void TearDown() override { |
| 36 mds_->polling_state_.current_state = | 37 mds_->polling_state_->current_state = |
| 37 MemoryDumpScheduler::PollingTriggerState::DISABLED; | 38 MemoryDumpScheduler::PollingTriggerState::DISABLED; |
| 38 mds_.reset(); | |
| 39 } | 39 } |
| 40 | 40 |
| 41 protected: | 41 protected: |
| 42 bool ShouldTriggerDump(uint64_t total) { | 42 bool ShouldTriggerDump(uint64_t total) { |
| 43 return mds_->ShouldTriggerDump(total); | 43 return mds_->ShouldTriggerDump(total); |
| 44 } | 44 } |
| 45 | 45 |
| 46 uint32_t num_samples_tracked_; | 46 uint32_t num_samples_tracked_; |
| 47 std::unique_ptr<MemoryDumpScheduler> mds_; | 47 MemoryDumpScheduler* mds_; |
| 48 }; | 48 }; |
| 49 | 49 |
| 50 TEST_F(MemoryDumpSchedulerPollingTest, PeakDetection) { | 50 TEST_F(MemoryDumpSchedulerPollingTest, PeakDetection) { |
| 51 for (uint32_t i = 0; i < num_samples_tracked_ * 6; ++i) { | 51 for (uint32_t i = 0; i < num_samples_tracked_ * 6; ++i) { |
| 52 // Memory is increased in steps and dumps must be triggered at every step. | 52 // Memory is increased in steps and dumps must be triggered at every step. |
| 53 uint64_t total = (2 + (i / (2 * num_samples_tracked_))) * 1024 * 1204; | 53 uint64_t total = (2 + (i / (2 * num_samples_tracked_))) * 1024 * 1204; |
| 54 bool did_trigger = ShouldTriggerDump(total); | 54 bool did_trigger = ShouldTriggerDump(total); |
| 55 // Dumps must be triggered only at specific iterations. | 55 // Dumps must be triggered only at specific iterations. |
| 56 bool should_have_triggered = i == 0; | 56 bool should_have_triggered = i == 0; |
| 57 should_have_triggered |= | 57 should_have_triggered |= |
| (...skipping 22 matching lines...) Expand all Loading... |
| 80 } | 80 } |
| 81 | 81 |
| 82 TEST_F(MemoryDumpSchedulerPollingTest, NotifyDumpTriggered) { | 82 TEST_F(MemoryDumpSchedulerPollingTest, NotifyDumpTriggered) { |
| 83 for (uint32_t i = 0; i < num_samples_tracked_ * 6; ++i) { | 83 for (uint32_t i = 0; i < num_samples_tracked_ * 6; ++i) { |
| 84 uint64_t total = (2 + (i / (2 * num_samples_tracked_))) * 1024 * 1204; | 84 uint64_t total = (2 + (i / (2 * num_samples_tracked_))) * 1024 * 1204; |
| 85 if (i % num_samples_tracked_ == 0) | 85 if (i % num_samples_tracked_ == 0) |
| 86 mds_->NotifyDumpTriggered(); | 86 mds_->NotifyDumpTriggered(); |
| 87 bool did_trigger = ShouldTriggerDump(total); | 87 bool did_trigger = ShouldTriggerDump(total); |
| 88 // Dumps should never be triggered since NotifyDumpTriggered() is called | 88 // Dumps should never be triggered since NotifyDumpTriggered() is called |
| 89 // frequently. | 89 // frequently. |
| 90 EXPECT_NE(0u, mds_->polling_state_.last_dump_memory_total); | 90 EXPECT_NE(0u, mds_->polling_state_->last_dump_memory_total); |
| 91 EXPECT_GT(num_samples_tracked_ - 1, | 91 EXPECT_GT(num_samples_tracked_ - 1, |
| 92 mds_->polling_state_.last_memory_totals_kb_index); | 92 mds_->polling_state_->last_memory_totals_kb_index); |
| 93 EXPECT_LT(static_cast<int64_t>(total - | 93 EXPECT_LT(static_cast<int64_t>( |
| 94 mds_->polling_state_.last_dump_memory_total), | 94 total - mds_->polling_state_->last_dump_memory_total), |
| 95 mds_->polling_state_.memory_increase_threshold); | 95 mds_->polling_state_->memory_increase_threshold); |
| 96 ASSERT_FALSE(did_trigger && i) << "Unexpected dump at " << i; | 96 ASSERT_FALSE(did_trigger && i) << "Unexpected dump at " << i; |
| 97 } | 97 } |
| 98 } | 98 } |
| 99 | 99 |
| 100 } // namespace trace_event | 100 } // namespace trace_event |
| 101 } // namespace base | 101 } // namespace base |
| OLD | NEW |