Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/util/memory_pressure/multi_source_memory_pressure_monitor.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/logging.h" |
| 9 | #include "base/metrics/histogram_macros.h" |
Ryan Powell | a68d66fc | 2019-08-19 21:32:39 | [diff] [blame] | 10 | #include "base/util/memory_pressure/system_memory_pressure_evaluator.h" |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 11 | |
| 12 | namespace util { |
| 13 | |
| 14 | MultiSourceMemoryPressureMonitor::MultiSourceMemoryPressureMonitor() |
| 15 | : current_pressure_level_( |
| 16 | base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), |
| 17 | dispatch_callback_(base::BindRepeating( |
| 18 | &base::MemoryPressureListener::NotifyMemoryPressure)), |
| 19 | aggregator_(this) { |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 20 | } |
| 21 | |
Jenny Wong | 6592dd4 | 2019-09-26 17:25:07 | [diff] [blame] | 22 | MultiSourceMemoryPressureMonitor::~MultiSourceMemoryPressureMonitor() { |
| 23 | // Destroy system evaluator early while the remaining members of this class |
| 24 | // still exist. MultiSourceMemoryPressureMonitor implements |
| 25 | // MemoryPressureVoteAggregator::Delegate, and |
| 26 | // delegate_->OnMemoryPressureLevelChanged() gets indirectly called during |
| 27 | // ~SystemMemoryPressureEvaluator(). |
| 28 | system_evaluator_.reset(); |
| 29 | } |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 30 | |
Sebastien Marchand | c9d4cd1 | 2019-10-03 22:48:01 | [diff] [blame^] | 31 | void MultiSourceMemoryPressureMonitor::Start() { |
| 32 | system_evaluator_ = |
| 33 | SystemMemoryPressureEvaluator::CreateDefaultSystemEvaluator(this); |
| 34 | StartMetricsTimer(); |
| 35 | } |
| 36 | |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 37 | void MultiSourceMemoryPressureMonitor::StartMetricsTimer() { |
| 38 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
Ryan Powell | bdc2759 | 2019-09-04 22:59:39 | [diff] [blame] | 39 | // Unretained is safe here since this task is running on a timer owned by this |
| 40 | // object. |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 41 | metric_timer_.Start( |
| 42 | FROM_HERE, MemoryPressureMonitor::kUMAMemoryPressureLevelPeriod, |
Ryan Powell | bdc2759 | 2019-09-04 22:59:39 | [diff] [blame] | 43 | BindRepeating( |
| 44 | &MultiSourceMemoryPressureMonitor::RecordCurrentPressureLevel, |
| 45 | base::Unretained(this))); |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void MultiSourceMemoryPressureMonitor::StopMetricsTimer() { |
| 49 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 50 | metric_timer_.Stop(); |
| 51 | } |
| 52 | |
Ryan Powell | bdc2759 | 2019-09-04 22:59:39 | [diff] [blame] | 53 | void MultiSourceMemoryPressureMonitor::RecordCurrentPressureLevel() { |
| 54 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 55 | RecordMemoryPressure(GetCurrentPressureLevel(), /* ticks = */ 1); |
| 56 | } |
| 57 | |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 58 | base::MemoryPressureListener::MemoryPressureLevel |
| 59 | MultiSourceMemoryPressureMonitor::GetCurrentPressureLevel() const { |
| 60 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 61 | return current_pressure_level_; |
| 62 | } |
| 63 | |
| 64 | std::unique_ptr<MemoryPressureVoter> |
| 65 | MultiSourceMemoryPressureMonitor::CreateVoter() { |
| 66 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 67 | return std::make_unique<MemoryPressureVoter>(&aggregator_); |
| 68 | } |
| 69 | |
| 70 | void MultiSourceMemoryPressureMonitor::SetDispatchCallback( |
| 71 | const DispatchCallback& callback) { |
| 72 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 73 | dispatch_callback_ = callback; |
| 74 | } |
| 75 | |
| 76 | void MultiSourceMemoryPressureMonitor::OnMemoryPressureLevelChanged( |
| 77 | base::MemoryPressureListener::MemoryPressureLevel level) { |
| 78 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 79 | current_pressure_level_ = level; |
| 80 | } |
| 81 | |
| 82 | void MultiSourceMemoryPressureMonitor::OnNotifyListenersRequested() { |
| 83 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 84 | dispatch_callback_.Run(current_pressure_level_); |
| 85 | } |
| 86 | |
Ryan Powell | a68d66fc | 2019-08-19 21:32:39 | [diff] [blame] | 87 | void MultiSourceMemoryPressureMonitor::ResetSystemEvaluatorForTesting() { |
| 88 | system_evaluator_.reset(); |
| 89 | } |
| 90 | |
Ryan Powell | 00e61aea | 2019-09-04 18:33:44 | [diff] [blame] | 91 | void MultiSourceMemoryPressureMonitor::SetSystemEvaluator( |
| 92 | std::unique_ptr<SystemMemoryPressureEvaluator> evaluator) { |
| 93 | DCHECK(!system_evaluator_); |
| 94 | system_evaluator_ = std::move(evaluator); |
| 95 | } |
| 96 | |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 97 | } // namespace util |