1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
// Copyright 2016 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 "components/memory_pressure/memory_pressure_monitor.h"
#include <memory>
#include "base/bind.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/tracked_objects.h"
#include "components/memory_pressure/memory_pressure_stats_collector.h"
#include "components/memory_pressure/test_memory_pressure_calculator.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace memory_pressure {
namespace {
using MemoryPressureLevel = MemoryPressureMonitor::MemoryPressureLevel;
const MemoryPressureLevel MEMORY_PRESSURE_LEVEL_NONE =
MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
const MemoryPressureLevel MEMORY_PRESSURE_LEVEL_MODERATE =
MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE;
const MemoryPressureLevel MEMORY_PRESSURE_LEVEL_CRITICAL =
MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL;
using testing::_;
} // namespace
// A mock task runner. This isn't directly a TaskRunner as the reference
// counting confuses gmock.
class LenientMockTaskRunner {
public:
MOCK_METHOD3(PostDelayedTask,
bool(const tracked_objects::Location&,
const base::Closure&,
base::TimeDelta));
};
using MockTaskRunner = testing::StrictMock<LenientMockTaskRunner>;
// A TaskRunner implementation that wraps a MockTaskRunner.
class TaskRunnerProxy : public base::TaskRunner {
public:
// The provided |mock| must outlive this object.
explicit TaskRunnerProxy(MockTaskRunner* mock) : mock_(mock) {}
bool RunsTasksOnCurrentThread() const override { return true; }
bool PostDelayedTask(const tracked_objects::Location& location,
const base::Closure& closure,
base::TimeDelta delta) override {
return mock_->PostDelayedTask(location, closure, delta);
}
private:
MockTaskRunner* mock_;
~TaskRunnerProxy() override {}
};
class TestMemoryPressureMonitor : public MemoryPressureMonitor {
public:
// Expose the callback that is used for scheduled checks.
using MemoryPressureMonitor::CheckPressureAndUpdateStats;
#if !defined(MEMORY_PRESSURE_IS_POLLING)
using MemoryPressureMonitor::OnMemoryPressureChanged;
#endif
#if defined(MEMORY_PRESSURE_IS_POLLING)
TestMemoryPressureMonitor(scoped_refptr<base::TaskRunner> task_runner,
base::TickClock* tick_clock,
MemoryPressureStatsCollector* stats_collector,
MemoryPressureCalculator* calculator,
DispatchCallback dispatch_callback)
: MemoryPressureMonitor(task_runner,
tick_clock,
stats_collector,
calculator,
dispatch_callback) {}
#else // MEMORY_PRESSURE_IS_POLLING
TestMemoryPressureMonitor(scoped_refptr<base::TaskRunner> task_runner,
base::TickClock* tick_clock,
MemoryPressureStatsCollector* stats_collector,
DispatchCallback dispatch_callback,
MemoryPressureLevel initial_level)
: MemoryPressureMonitor(task_runner,
tick_clock,
stats_collector,
dispatch_callback,
initial_level) {}
#endif // !MEMORY_PRESSURE_IS_POLLING
// A handful of accessors for unittesting.
MemoryPressureLevel current_memory_pressure_level() const {
return current_memory_pressure_level_;
}
const std::map<int, base::TimeTicks>& scheduled_checks() const {
return scheduled_checks_;
}
int serial_number() const { return serial_number_; }
};
// A mock dispatch class.
class LenientMockDispatch {
public:
MOCK_METHOD1(Dispatch, void(MemoryPressureLevel));
};
using MockDispatch = testing::StrictMock<LenientMockDispatch>;
class MemoryPressureMonitorTest : public testing::Test {
public:
MemoryPressureMonitorTest()
: task_runner_proxy_(new TaskRunnerProxy(&mock_task_runner_)),
stats_collector_(&tick_clock_) {}
#if defined(MEMORY_PRESSURE_IS_POLLING)
// Creates a monitor in the given initial pressure level and validates its
// state.
void CreateMonitor(MemoryPressureLevel initial_level) {
calculator_.SetLevel(initial_level);
Tick(1); // Advance the clock so it doesn't return zero.
// Determine the delay with which we expect the task to be posted.
int delay = MemoryPressureMonitor::kDefaultPollingIntervalMs;
if (initial_level == MEMORY_PRESSURE_LEVEL_MODERATE)
delay = MemoryPressureMonitor::kNotificationIntervalPressureModerateMs;
else if (initial_level == MEMORY_PRESSURE_LEVEL_CRITICAL)
delay = MemoryPressureMonitor::kNotificationIntervalPressureCriticalMs;
// The monitor will make one call to the task runner during construction.
ExpectTaskPosted(delay);
monitor_.reset(new TestMemoryPressureMonitor(
task_runner_proxy_, &tick_clock_, &stats_collector_, &calculator_,
base::Bind(&MockDispatch::Dispatch,
base::Unretained(&mock_dispatch_))));
VerifyAndClearExpectations();
EXPECT_EQ(1u, monitor_->scheduled_checks().size());
// The monitor should have made one call immediately to the calculator_.
EXPECT_EQ(1, calculator_.calls());
ExpectPressure(initial_level);
calculator_.ResetCalls();
}
#else // MEMORY_PRESSURE_IS_POLLING
void CreateMonitor(MemoryPressureLevel initial_level) {
Tick(1); // Advance the clock so it doesn't return zero.
// The monitor will make one call to the task runner during construction.
ExpectTaskPosted(MemoryPressureMonitor::kDefaultPollingIntervalMs);
monitor_.reset(new TestMemoryPressureMonitor(
task_runner_proxy_, &tick_clock_, &stats_collector_,
base::Bind(&MockDispatch::Dispatch, base::Unretained(&mock_dispatch_)),
initial_level));
VerifyAndClearExpectations();
EXPECT_EQ(1u, monitor_->scheduled_checks().size());
// The monitor should have made one call immediately to the calculator_.
ExpectPressure(initial_level);
}
#endif // !MEMORY_PRESSURE_IS_POLLING
// Advances the tick clock by the given number of milliseconds.
void Tick(int ms) {
tick_clock_.Advance(base::TimeDelta::FromMilliseconds(ms));
}
// Sets expectations for tasks scheduled via |mock_task_runner_|.
void ExpectTaskPosted(int delay_ms) {
base::TimeDelta delay = base::TimeDelta::FromMilliseconds(delay_ms);
EXPECT_CALL(mock_task_runner_, PostDelayedTask(_, _, delay))
.WillOnce(testing::Return(true));
}
// Sets up expectations for calls to |mock_dispatch_|.
void ExpectDispatch(MemoryPressureLevel level) {
EXPECT_CALL(mock_dispatch_, Dispatch(level));
}
void ExpectDispatchModerate() {
EXPECT_CALL(mock_dispatch_, Dispatch(MEMORY_PRESSURE_LEVEL_MODERATE));
}
void ExpectDispatchCritical() {
EXPECT_CALL(mock_dispatch_, Dispatch(MEMORY_PRESSURE_LEVEL_CRITICAL));
}
// Verifies and clears expectations for both |mock_task_runner_| and
// |mock_dispatch_|.
void VerifyAndClearExpectations() {
testing::Mock::VerifyAndClearExpectations(&mock_task_runner_);
testing::Mock::VerifyAndClearExpectations(&mock_dispatch_);
}
// Checks expectations on |monitor_->current_memory_pressure_level()|.
void ExpectPressure(MemoryPressureLevel level) {
EXPECT_EQ(level, monitor_->current_memory_pressure_level());
}
void ExpectNone() {
EXPECT_EQ(MEMORY_PRESSURE_LEVEL_NONE,
monitor_->current_memory_pressure_level());
}
void ExpectModerate() {
EXPECT_EQ(MEMORY_PRESSURE_LEVEL_MODERATE,
monitor_->current_memory_pressure_level());
}
void ExpectCritical() {
EXPECT_EQ(MEMORY_PRESSURE_LEVEL_CRITICAL,
monitor_->current_memory_pressure_level());
}
MockTaskRunner mock_task_runner_;
scoped_refptr<TaskRunnerProxy> task_runner_proxy_;
base::SimpleTestTickClock tick_clock_;
MemoryPressureStatsCollector stats_collector_;
#if defined(MEMORY_PRESSURE_IS_POLLING)
TestMemoryPressureCalculator calculator_;
#endif
MockDispatch mock_dispatch_;
std::unique_ptr<TestMemoryPressureMonitor> monitor_;
};
TEST_F(MemoryPressureMonitorTest, NormalScheduling) {
CreateMonitor(MEMORY_PRESSURE_LEVEL_NONE);
Tick(MemoryPressureMonitor::kDefaultPollingIntervalMs);
ExpectTaskPosted(MemoryPressureMonitor::kDefaultPollingIntervalMs);
monitor_->CheckPressureAndUpdateStats(monitor_->serial_number());
VerifyAndClearExpectations();
Tick(MemoryPressureMonitor::kDefaultPollingIntervalMs);
ExpectTaskPosted(MemoryPressureMonitor::kDefaultPollingIntervalMs);
monitor_->CheckPressureAndUpdateStats(monitor_->serial_number());
VerifyAndClearExpectations();
}
#if defined(MEMORY_PRESSURE_IS_POLLING)
TEST_F(MemoryPressureMonitorTest, CalculatorNotAlwaysInvoked) {
CreateMonitor(MEMORY_PRESSURE_LEVEL_NONE);
// Callback into the monitor too soon and expect nothing to happen.
Tick(1);
EXPECT_EQ(MEMORY_PRESSURE_LEVEL_NONE, monitor_->GetCurrentPressureLevel());
VerifyAndClearExpectations();
EXPECT_EQ(0, calculator_.calls());
ExpectNone();
EXPECT_EQ(1u, monitor_->scheduled_checks().size());
// Pass sufficient time that the rate limiter will allow another calculation.
// Don't expect another scheduled call because there's already one pending.
Tick(MemoryPressureMonitor::kMinimumTimeBetweenSamplesMs);
EXPECT_EQ(MEMORY_PRESSURE_LEVEL_NONE, monitor_->GetCurrentPressureLevel());
VerifyAndClearExpectations();
EXPECT_EQ(1, calculator_.calls());
ExpectNone();
EXPECT_EQ(1u, monitor_->scheduled_checks().size());
}
TEST_F(MemoryPressureMonitorTest, NoPressureScheduling) {
CreateMonitor(MEMORY_PRESSURE_LEVEL_NONE);
// Step forward by a polling interval. This should cause another scheduled
// check to be posted.
Tick(MemoryPressureMonitor::kDefaultPollingIntervalMs);
ExpectTaskPosted(MemoryPressureMonitor::kDefaultPollingIntervalMs);
monitor_->CheckPressureAndUpdateStats(monitor_->serial_number());
VerifyAndClearExpectations();
ExpectNone();
EXPECT_EQ(1u, monitor_->scheduled_checks().size());
}
TEST_F(MemoryPressureMonitorTest, NoPressureToModerateViaScheduling) {
CreateMonitor(MEMORY_PRESSURE_LEVEL_NONE);
// Step forward by a polling interval. This should cause another scheduled
// check to be posted.
calculator_.SetModerate();
Tick(MemoryPressureMonitor::kDefaultPollingIntervalMs);
ExpectTaskPosted(
MemoryPressureMonitor::kNotificationIntervalPressureModerateMs);
ExpectDispatchModerate();
monitor_->CheckPressureAndUpdateStats(monitor_->serial_number());
VerifyAndClearExpectations();
ExpectModerate();
EXPECT_EQ(1u, monitor_->scheduled_checks().size());
}
TEST_F(MemoryPressureMonitorTest, NoPressureToCriticalViaScheduling) {
CreateMonitor(MEMORY_PRESSURE_LEVEL_NONE);
// Step forward by a polling interval. This should cause another scheduled
// check to be posted.
calculator_.SetCritical();
Tick(MemoryPressureMonitor::kDefaultPollingIntervalMs);
ExpectTaskPosted(
MemoryPressureMonitor::kNotificationIntervalPressureCriticalMs);
ExpectDispatchCritical();
monitor_->CheckPressureAndUpdateStats(monitor_->serial_number());
VerifyAndClearExpectations();
ExpectCritical();
EXPECT_EQ(1u, monitor_->scheduled_checks().size());
}
TEST_F(MemoryPressureMonitorTest, ModeratePressureToCriticalViaScheduling) {
CreateMonitor(MEMORY_PRESSURE_LEVEL_MODERATE);
// Step forward by a polling interval. This should cause another scheduled
// check to be posted.
calculator_.SetCritical();
Tick(MemoryPressureMonitor::kNotificationIntervalPressureModerateMs);
ExpectTaskPosted(
MemoryPressureMonitor::kNotificationIntervalPressureCriticalMs);
ExpectDispatchCritical();
monitor_->CheckPressureAndUpdateStats(monitor_->serial_number());
VerifyAndClearExpectations();
ExpectCritical();
EXPECT_EQ(1u, monitor_->scheduled_checks().size());
}
TEST_F(MemoryPressureMonitorTest, UnscheduledStateChange) {
CreateMonitor(MEMORY_PRESSURE_LEVEL_NONE);
// Callback into the monitor directly. This will change the memory pressure
// and cause a new call to be scheduled as the higher memory pressure has a
// higher renotification frequency.
// NOTE: This test relies implicitly on the following facts:
// kMinimumTimeBetweenSamplesMs < kDefaultPollingIntervalMs / 2
// kNotificationIntervalPressureCriticalMs < kDefaultPollingIntervalMs / 2
calculator_.SetCritical();
Tick(MemoryPressureMonitor::kDefaultPollingIntervalMs / 2);
ExpectTaskPosted(
MemoryPressureMonitor::kNotificationIntervalPressureCriticalMs);
ExpectDispatchCritical();
EXPECT_EQ(MEMORY_PRESSURE_LEVEL_CRITICAL,
monitor_->GetCurrentPressureLevel());
VerifyAndClearExpectations();
EXPECT_EQ(1, calculator_.calls());
ExpectCritical();
EXPECT_EQ(2u, monitor_->scheduled_checks().size());
}
#else // MEMORY_PRESSURE_IS_POLLING
TEST_F(MemoryPressureMonitorTest, PressureChangeNop) {
CreateMonitor(MEMORY_PRESSURE_LEVEL_NONE);
// The pressure hasn't changed so this should be a nop.
Tick(1);
monitor_->OnMemoryPressureChanged(MEMORY_PRESSURE_LEVEL_NONE);
VerifyAndClearExpectations();
ExpectNone();
EXPECT_EQ(1u, monitor_->scheduled_checks().size());
}
TEST_F(MemoryPressureMonitorTest, PressureChangeNoNewScheduledTask) {
CreateMonitor(MEMORY_PRESSURE_LEVEL_NONE);
// The pressure is increasing and the renotification frequency has changed.
// However, a pending event is already scheduled soon enough so no new one
// should be scheduled.
Tick(MemoryPressureMonitor::kDefaultPollingIntervalMs -
MemoryPressureMonitor::kNotificationIntervalPressureModerateMs / 2);
ExpectDispatchModerate();
monitor_->OnMemoryPressureChanged(MEMORY_PRESSURE_LEVEL_MODERATE);
VerifyAndClearExpectations();
ExpectModerate();
EXPECT_EQ(1u, monitor_->scheduled_checks().size());
}
TEST_F(MemoryPressureMonitorTest, PressureChangeNewScheduledTask) {
CreateMonitor(MEMORY_PRESSURE_LEVEL_NONE);
// The pressure is increasing and the renotification frequency has changed.
// The already scheduled event is too far in the future so a new event should
// be scheduled.
Tick(1);
ExpectTaskPosted(
MemoryPressureMonitor::kNotificationIntervalPressureCriticalMs);
ExpectDispatchCritical();
monitor_->OnMemoryPressureChanged(MEMORY_PRESSURE_LEVEL_CRITICAL);
VerifyAndClearExpectations();
ExpectCritical();
EXPECT_EQ(2u, monitor_->scheduled_checks().size());
}
#endif // !MEMORY_PRESSURE_IS_POLLING
} // namespace memory_pressure
|