summaryrefslogtreecommitdiff
path: root/base/trace_event/memory_dump_scheduler_unittest.cc
diff options
context:
space:
mode:
authorJakub Pawlowski <jpawlowski@google.com>2017-03-14 10:55:53 -0700
committerJakub Pawlowski <jpawlowski@google.com>2017-12-22 03:06:26 -0800
commit319afc59a539d6261307aadbdab4d4ee93eaf1ff (patch)
tree0f21b95ba579352a9829d3868c7365d4f9dde210 /base/trace_event/memory_dump_scheduler_unittest.cc
parent8abac493f652a1835c61e538919820aa77658a39 (diff)
downloadlibchrome-319afc59a539d6261307aadbdab4d4ee93eaf1ff.tar.gz
Uprev the library to r462023 from Chromium, 3rd attempt
This merge was done against r462023 which corresponds to git commit 32eb7c31af9cab6231f0d3d05206072079177605 from Apr 05, 2017 First attempt, in commit bf8c17f71511c1e90cd8cccfe71f0852c566bd3b was badly squashed, causing automated test failure in system/bt. Next one broke mac build. Test: manually ran all test from system/bt that failed on previous attempt, plus libchrome_unittest Change-Id: I60003263418de3078c7be2da9fb1eeaeb786f3d0
Diffstat (limited to 'base/trace_event/memory_dump_scheduler_unittest.cc')
-rw-r--r--base/trace_event/memory_dump_scheduler_unittest.cc101
1 files changed, 101 insertions, 0 deletions
diff --git a/base/trace_event/memory_dump_scheduler_unittest.cc b/base/trace_event/memory_dump_scheduler_unittest.cc
new file mode 100644
index 0000000000..9af2a3b430
--- /dev/null
+++ b/base/trace_event/memory_dump_scheduler_unittest.cc
@@ -0,0 +1,101 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/trace_event/memory_dump_scheduler.h"
+
+#include <memory>
+
+#include "base/single_thread_task_runner.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace base {
+namespace trace_event {
+
+class MemoryDumpSchedulerPollingTest : public testing::Test {
+ public:
+ static const uint32_t kMinPollsToDump = 5;
+
+ MemoryDumpSchedulerPollingTest()
+ : testing::Test(),
+ num_samples_tracked_(
+ MemoryDumpScheduler::PollingTriggerState::kMaxNumMemorySamples) {}
+
+ void SetUp() override {
+ MemoryDumpScheduler::SetPollingIntervalForTesting(1);
+ uint32_t kMinPollsToDump = 5;
+ mds_ = MemoryDumpScheduler::GetInstance();
+ mds_->Setup(nullptr, nullptr);
+ mds_->AddTrigger(MemoryDumpType::PEAK_MEMORY_USAGE,
+ MemoryDumpLevelOfDetail::LIGHT, kMinPollsToDump);
+ mds_->polling_state_->ResetTotals();
+ mds_->polling_state_->current_state =
+ MemoryDumpScheduler::PollingTriggerState::ENABLED;
+ }
+
+ void TearDown() override {
+ mds_->polling_state_->current_state =
+ MemoryDumpScheduler::PollingTriggerState::DISABLED;
+ }
+
+ protected:
+ bool ShouldTriggerDump(uint64_t total) {
+ return mds_->ShouldTriggerDump(total);
+ }
+
+ uint32_t num_samples_tracked_;
+ MemoryDumpScheduler* mds_;
+};
+
+TEST_F(MemoryDumpSchedulerPollingTest, PeakDetection) {
+ for (uint32_t i = 0; i < num_samples_tracked_ * 6; ++i) {
+ // Memory is increased in steps and dumps must be triggered at every step.
+ uint64_t total = (2 + (i / (2 * num_samples_tracked_))) * 1024 * 1204;
+ bool did_trigger = ShouldTriggerDump(total);
+ // Dumps must be triggered only at specific iterations.
+ bool should_have_triggered = i == 0;
+ should_have_triggered |=
+ (i > num_samples_tracked_) && (i % (2 * num_samples_tracked_) == 1);
+ if (should_have_triggered) {
+ ASSERT_TRUE(did_trigger) << "Dump wasn't triggered at " << i;
+ } else {
+ ASSERT_FALSE(did_trigger) << "Unexpected dump at " << i;
+ }
+ }
+}
+
+TEST_F(MemoryDumpSchedulerPollingTest, SlowGrowthDetection) {
+ for (uint32_t i = 0; i < 15; ++i) {
+ // Record 1GiB of increase in each call. Dumps are triggered with 1% w.r.t
+ // system's total memory.
+ uint64_t total = static_cast<uint64_t>(i + 1) * 1024 * 1024 * 1024;
+ bool did_trigger = ShouldTriggerDump(total);
+ bool should_have_triggered = i % kMinPollsToDump == 0;
+ if (should_have_triggered) {
+ ASSERT_TRUE(did_trigger) << "Dump wasn't triggered at " << i;
+ } else {
+ ASSERT_FALSE(did_trigger) << "Unexpected dump at " << i;
+ }
+ }
+}
+
+TEST_F(MemoryDumpSchedulerPollingTest, NotifyDumpTriggered) {
+ for (uint32_t i = 0; i < num_samples_tracked_ * 6; ++i) {
+ uint64_t total = (2 + (i / (2 * num_samples_tracked_))) * 1024 * 1204;
+ if (i % num_samples_tracked_ == 0)
+ mds_->NotifyDumpTriggered();
+ bool did_trigger = ShouldTriggerDump(total);
+ // Dumps should never be triggered since NotifyDumpTriggered() is called
+ // frequently.
+ EXPECT_NE(0u, mds_->polling_state_->last_dump_memory_total);
+ EXPECT_GT(num_samples_tracked_ - 1,
+ mds_->polling_state_->last_memory_totals_kb_index);
+ EXPECT_LT(static_cast<int64_t>(
+ total - mds_->polling_state_->last_dump_memory_total),
+ mds_->polling_state_->memory_increase_threshold);
+ ASSERT_FALSE(did_trigger && i) << "Unexpected dump at " << i;
+ }
+}
+
+} // namespace trace_event
+} // namespace base