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" |
Hans Wennborg | c3cffa6 | 2020-04-27 10:09:12 | [diff] [blame] | 8 | #include "base/check_op.h" |
Sebastien Marchand | 6c5354e | 2020-02-07 19:38:25 | [diff] [blame] | 9 | #include "base/metrics/histogram_functions.h" |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 10 | #include "base/metrics/histogram_macros.h" |
Sebastien Marchand | 6c5354e | 2020-02-07 19:38:25 | [diff] [blame] | 11 | #include "base/time/time.h" |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 12 | #include "base/trace_event/base_tracing.h" |
Ryan Powell | a68d66fc | 2019-08-19 21:32:39 | [diff] [blame] | 13 | #include "base/util/memory_pressure/system_memory_pressure_evaluator.h" |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 14 | |
| 15 | namespace util { |
| 16 | |
| 17 | MultiSourceMemoryPressureMonitor::MultiSourceMemoryPressureMonitor() |
| 18 | : current_pressure_level_( |
| 19 | base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), |
| 20 | dispatch_callback_(base::BindRepeating( |
| 21 | &base::MemoryPressureListener::NotifyMemoryPressure)), |
| 22 | aggregator_(this) { |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 23 | } |
| 24 | |
Jenny Wong | 6592dd4 | 2019-09-26 17:25:07 | [diff] [blame] | 25 | MultiSourceMemoryPressureMonitor::~MultiSourceMemoryPressureMonitor() { |
| 26 | // Destroy system evaluator early while the remaining members of this class |
| 27 | // still exist. MultiSourceMemoryPressureMonitor implements |
| 28 | // MemoryPressureVoteAggregator::Delegate, and |
| 29 | // delegate_->OnMemoryPressureLevelChanged() gets indirectly called during |
| 30 | // ~SystemMemoryPressureEvaluator(). |
| 31 | system_evaluator_.reset(); |
| 32 | } |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 33 | |
Sebastien Marchand | c9d4cd1 | 2019-10-03 22:48:01 | [diff] [blame] | 34 | void MultiSourceMemoryPressureMonitor::Start() { |
| 35 | system_evaluator_ = |
| 36 | SystemMemoryPressureEvaluator::CreateDefaultSystemEvaluator(this); |
| 37 | StartMetricsTimer(); |
| 38 | } |
| 39 | |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 40 | void MultiSourceMemoryPressureMonitor::StartMetricsTimer() { |
| 41 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
Ryan Powell | bdc2759 | 2019-09-04 22:59:39 | [diff] [blame] | 42 | // Unretained is safe here since this task is running on a timer owned by this |
| 43 | // object. |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 44 | metric_timer_.Start( |
| 45 | FROM_HERE, MemoryPressureMonitor::kUMAMemoryPressureLevelPeriod, |
Ryan Powell | bdc2759 | 2019-09-04 22:59:39 | [diff] [blame] | 46 | BindRepeating( |
| 47 | &MultiSourceMemoryPressureMonitor::RecordCurrentPressureLevel, |
| 48 | base::Unretained(this))); |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void MultiSourceMemoryPressureMonitor::StopMetricsTimer() { |
| 52 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 53 | metric_timer_.Stop(); |
| 54 | } |
| 55 | |
Ryan Powell | bdc2759 | 2019-09-04 22:59:39 | [diff] [blame] | 56 | void MultiSourceMemoryPressureMonitor::RecordCurrentPressureLevel() { |
| 57 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 58 | RecordMemoryPressure(GetCurrentPressureLevel(), /* ticks = */ 1); |
| 59 | } |
| 60 | |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 61 | base::MemoryPressureListener::MemoryPressureLevel |
| 62 | MultiSourceMemoryPressureMonitor::GetCurrentPressureLevel() const { |
| 63 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 64 | return current_pressure_level_; |
| 65 | } |
| 66 | |
| 67 | std::unique_ptr<MemoryPressureVoter> |
| 68 | MultiSourceMemoryPressureMonitor::CreateVoter() { |
| 69 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
Wez | 861e292 | 2020-03-01 21:14:00 | [diff] [blame] | 70 | return aggregator_.CreateVoter(); |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void MultiSourceMemoryPressureMonitor::SetDispatchCallback( |
| 74 | const DispatchCallback& callback) { |
| 75 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 76 | dispatch_callback_ = callback; |
| 77 | } |
| 78 | |
| 79 | void MultiSourceMemoryPressureMonitor::OnMemoryPressureLevelChanged( |
| 80 | base::MemoryPressureListener::MemoryPressureLevel level) { |
| 81 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
Sebastien Marchand | 6c5354e | 2020-02-07 19:38:25 | [diff] [blame] | 82 | DCHECK_NE(current_pressure_level_, level); |
| 83 | |
ssid | fff7ab4 | 2021-03-04 20:03:11 | [diff] [blame^] | 84 | TRACE_EVENT_INSTANT( |
Sebastien Marchand | 9df1f3d | 2020-03-23 18:54:31 | [diff] [blame] | 85 | "base", "MultiSourceMemoryPressureMonitor::OnMemoryPressureLevelChanged", |
ssid | fff7ab4 | 2021-03-04 20:03:11 | [diff] [blame^] | 86 | [&](perfetto::EventContext ctx) { |
| 87 | auto* event = ctx.event<perfetto::protos::pbzero::ChromeTrackEvent>(); |
| 88 | auto* data = event->set_chrome_memory_pressure_notification(); |
| 89 | data->set_level(base::MemoryPressureListener::LevelAsTraceEnum(level)); |
| 90 | }); |
Sebastien Marchand | 9df1f3d | 2020-03-23 18:54:31 | [diff] [blame] | 91 | |
Sebastien Marchand | 6c5354e | 2020-02-07 19:38:25 | [diff] [blame] | 92 | // Records the duration of the latest pressure session, there are 4 |
| 93 | // transitions of interest: |
| 94 | // - Moderate -> None |
| 95 | // - Moderate -> Critical |
| 96 | // - Critical -> Moderate |
| 97 | // - Critical -> None |
| 98 | |
| 99 | base::TimeTicks now = base::TimeTicks::Now(); |
| 100 | |
| 101 | if (current_pressure_level_ != |
| 102 | MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_NONE) { |
| 103 | DCHECK(!last_pressure_change_timestamp_.is_null()); |
| 104 | std::string histogram_name = "Memory.PressureWindowDuration."; |
| 105 | switch (current_pressure_level_) { |
| 106 | // From: |
| 107 | case MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE: { |
| 108 | // To: |
| 109 | if (level == MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_NONE) { |
| 110 | histogram_name += "ModerateToNone"; |
| 111 | } else { // MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL |
| 112 | histogram_name += "ModerateToCritical"; |
| 113 | } |
| 114 | break; |
| 115 | } |
| 116 | // From: |
| 117 | case MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL: { |
| 118 | // To: |
| 119 | if (level == MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_NONE) { |
| 120 | histogram_name += "CriticalToNone"; |
| 121 | } else { // MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE |
| 122 | histogram_name += "CriticalToModerate"; |
| 123 | } |
| 124 | break; |
| 125 | } |
| 126 | case MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_NONE: |
| 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | base::UmaHistogramCustomTimes( |
| 132 | histogram_name, now - last_pressure_change_timestamp_, |
| 133 | base::TimeDelta::FromSeconds(1), base::TimeDelta::FromMinutes(10), 50); |
| 134 | } |
| 135 | |
| 136 | last_pressure_change_timestamp_ = now; |
| 137 | |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 138 | current_pressure_level_ = level; |
| 139 | } |
| 140 | |
| 141 | void MultiSourceMemoryPressureMonitor::OnNotifyListenersRequested() { |
| 142 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 143 | dispatch_callback_.Run(current_pressure_level_); |
| 144 | } |
| 145 | |
Ryan Powell | a68d66fc | 2019-08-19 21:32:39 | [diff] [blame] | 146 | void MultiSourceMemoryPressureMonitor::ResetSystemEvaluatorForTesting() { |
| 147 | system_evaluator_.reset(); |
| 148 | } |
| 149 | |
Ryan Powell | 00e61aea | 2019-09-04 18:33:44 | [diff] [blame] | 150 | void MultiSourceMemoryPressureMonitor::SetSystemEvaluator( |
| 151 | std::unique_ptr<SystemMemoryPressureEvaluator> evaluator) { |
| 152 | DCHECK(!system_evaluator_); |
| 153 | system_evaluator_ = std::move(evaluator); |
| 154 | } |
| 155 | |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 156 | } // namespace util |