Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: base/chromeos/memory_pressure_monitor_chromeos.cc

Issue 1045433002: Migrate ChromeOS to base::MemoryPressureMonitor (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review comments Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/chromeos/memory_pressure_observer_chromeos.h" 5 #include "base/chromeos/memory_pressure_monitor_chromeos.h"
6 6
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/metrics/histogram_macros.h" 8 #include "base/metrics/histogram_macros.h"
9 #include "base/process/process_metrics.h" 9 #include "base/process/process_metrics.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
11 11
12 namespace base { 12 namespace base {
13 13
14 namespace { 14 namespace {
15 15
(...skipping 20 matching lines...) Expand all
36 enum MemoryPressureLevelUMA { 36 enum MemoryPressureLevelUMA {
37 MEMORY_PRESSURE_LEVEL_NONE = 0, 37 MEMORY_PRESSURE_LEVEL_NONE = 0,
38 MEMORY_PRESSURE_LEVEL_MODERATE, 38 MEMORY_PRESSURE_LEVEL_MODERATE,
39 MEMORY_PRESSURE_LEVEL_CRITICAL, 39 MEMORY_PRESSURE_LEVEL_CRITICAL,
40 NUM_MEMORY_PRESSURE_LEVELS 40 NUM_MEMORY_PRESSURE_LEVELS
41 }; 41 };
42 42
43 // Converts a |MemoryPressureThreshold| value into a used memory percentage for 43 // Converts a |MemoryPressureThreshold| value into a used memory percentage for
44 // the moderate pressure event. 44 // the moderate pressure event.
45 int GetModerateMemoryThresholdInPercent( 45 int GetModerateMemoryThresholdInPercent(
46 MemoryPressureObserverChromeOS::MemoryPressureThresholds thresholds) { 46 MemoryPressureMonitorChromeOS::MemoryPressureThresholds thresholds) {
47 return thresholds == MemoryPressureObserverChromeOS:: 47 return thresholds == MemoryPressureMonitorChromeOS::
48 THRESHOLD_AGGRESSIVE_CACHE_DISCARD || 48 THRESHOLD_AGGRESSIVE_CACHE_DISCARD ||
49 thresholds == MemoryPressureObserverChromeOS::THRESHOLD_AGGRESSIVE 49 thresholds == MemoryPressureMonitorChromeOS::THRESHOLD_AGGRESSIVE
50 ? kAggressiveMemoryPressureModerateThresholdPercent 50 ? kAggressiveMemoryPressureModerateThresholdPercent
51 : kNormalMemoryPressureModerateThresholdPercent; 51 : kNormalMemoryPressureModerateThresholdPercent;
52 } 52 }
53 53
54 // Converts a |MemoryPressureThreshold| value into a used memory percentage for 54 // Converts a |MemoryPressureThreshold| value into a used memory percentage for
55 // the critical pressure event. 55 // the critical pressure event.
56 int GetCriticalMemoryThresholdInPercent( 56 int GetCriticalMemoryThresholdInPercent(
57 MemoryPressureObserverChromeOS::MemoryPressureThresholds thresholds) { 57 MemoryPressureMonitorChromeOS::MemoryPressureThresholds thresholds) {
58 return thresholds == MemoryPressureObserverChromeOS:: 58 return thresholds == MemoryPressureMonitorChromeOS::
59 THRESHOLD_AGGRESSIVE_TAB_DISCARD || 59 THRESHOLD_AGGRESSIVE_TAB_DISCARD ||
60 thresholds == MemoryPressureObserverChromeOS::THRESHOLD_AGGRESSIVE 60 thresholds == MemoryPressureMonitorChromeOS::THRESHOLD_AGGRESSIVE
61 ? kAggressiveMemoryPressureCriticalThresholdPercent 61 ? kAggressiveMemoryPressureCriticalThresholdPercent
62 : kNormalMemoryPressureCriticalThresholdPercent; 62 : kNormalMemoryPressureCriticalThresholdPercent;
63 } 63 }
64 64
65 // Converts free percent of memory into a memory pressure value. 65 // Converts free percent of memory into a memory pressure value.
66 MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevelFromFillLevel( 66 MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevelFromFillLevel(
67 int actual_fill_level, 67 int actual_fill_level,
68 int moderate_threshold, 68 int moderate_threshold,
69 int critical_threshold) { 69 int critical_threshold) {
70 if (actual_fill_level < moderate_threshold) 70 if (actual_fill_level < moderate_threshold)
71 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; 71 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
72 return actual_fill_level < critical_threshold 72 return actual_fill_level < critical_threshold
73 ? MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE 73 ? MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE
74 : MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; 74 : MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL;
75 } 75 }
76 76
77 } // namespace 77 } // namespace
78 78
79 MemoryPressureObserverChromeOS::MemoryPressureObserverChromeOS( 79 MemoryPressureMonitorChromeOS::MemoryPressureMonitorChromeOS(
80 MemoryPressureThresholds thresholds) 80 MemoryPressureThresholds thresholds)
81 : current_memory_pressure_level_( 81 : current_memory_pressure_level_(
82 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), 82 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
83 moderate_pressure_repeat_count_(0), 83 moderate_pressure_repeat_count_(0),
84 moderate_pressure_threshold_percent_( 84 moderate_pressure_threshold_percent_(
85 GetModerateMemoryThresholdInPercent(thresholds)), 85 GetModerateMemoryThresholdInPercent(thresholds)),
86 critical_pressure_threshold_percent_( 86 critical_pressure_threshold_percent_(
87 GetCriticalMemoryThresholdInPercent(thresholds)), 87 GetCriticalMemoryThresholdInPercent(thresholds)),
88 weak_ptr_factory_(this) { 88 weak_ptr_factory_(this) {
89 StartObserving(); 89 StartObserving();
90 } 90 }
91 91
92 MemoryPressureObserverChromeOS::~MemoryPressureObserverChromeOS() { 92 MemoryPressureMonitorChromeOS::~MemoryPressureMonitorChromeOS() {
93 StopObserving(); 93 StopObserving();
94 } 94 }
95 95
96 void MemoryPressureObserverChromeOS::ScheduleEarlyCheck() { 96 void MemoryPressureMonitorChromeOS::ScheduleEarlyCheck() {
97 MessageLoop::current()->PostTask( 97 MessageLoop::current()->PostTask(
98 FROM_HERE, 98 FROM_HERE,
99 Bind(&MemoryPressureObserverChromeOS::CheckMemoryPressure, 99 Bind(&MemoryPressureMonitorChromeOS::CheckMemoryPressure,
100 weak_ptr_factory_.GetWeakPtr())); 100 weak_ptr_factory_.GetWeakPtr()));
101 } 101 }
102 102
103 void MemoryPressureObserverChromeOS::StartObserving() { 103 MemoryPressureListener::MemoryPressureLevel
104 MemoryPressureMonitorChromeOS::GetCurrentPressureLevel() const {
105 return current_memory_pressure_level_;
106 }
107
108 void MemoryPressureMonitorChromeOS::StartObserving() {
104 timer_.Start(FROM_HERE, 109 timer_.Start(FROM_HERE,
105 TimeDelta::FromMilliseconds(kMemoryPressureIntervalMs), 110 TimeDelta::FromMilliseconds(kMemoryPressureIntervalMs),
106 Bind(&MemoryPressureObserverChromeOS:: 111 Bind(&MemoryPressureMonitorChromeOS::
107 CheckMemoryPressureAndRecordStatistics, 112 CheckMemoryPressureAndRecordStatistics,
108 weak_ptr_factory_.GetWeakPtr())); 113 weak_ptr_factory_.GetWeakPtr()));
109 } 114 }
110 115
111 void MemoryPressureObserverChromeOS::StopObserving() { 116 void MemoryPressureMonitorChromeOS::StopObserving() {
112 // If StartObserving failed, StopObserving will still get called. 117 // If StartObserving failed, StopObserving will still get called.
113 timer_.Stop(); 118 timer_.Stop();
114 } 119 }
115 120
116 void MemoryPressureObserverChromeOS::CheckMemoryPressureAndRecordStatistics() { 121 void MemoryPressureMonitorChromeOS::CheckMemoryPressureAndRecordStatistics() {
117 CheckMemoryPressure(); 122 CheckMemoryPressure();
118 123
119 // Record UMA histogram statistics for the current memory pressure level. 124 // Record UMA histogram statistics for the current memory pressure level.
120 MemoryPressureLevelUMA memory_pressure_level_uma(MEMORY_PRESSURE_LEVEL_NONE); 125 MemoryPressureLevelUMA memory_pressure_level_uma(MEMORY_PRESSURE_LEVEL_NONE);
121 switch (current_memory_pressure_level_) { 126 switch (current_memory_pressure_level_) {
122 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: 127 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE:
123 memory_pressure_level_uma = MEMORY_PRESSURE_LEVEL_NONE; 128 memory_pressure_level_uma = MEMORY_PRESSURE_LEVEL_NONE;
124 break; 129 break;
125 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: 130 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE:
126 memory_pressure_level_uma = MEMORY_PRESSURE_LEVEL_MODERATE; 131 memory_pressure_level_uma = MEMORY_PRESSURE_LEVEL_MODERATE;
127 break; 132 break;
128 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: 133 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL:
129 memory_pressure_level_uma = MEMORY_PRESSURE_LEVEL_CRITICAL; 134 memory_pressure_level_uma = MEMORY_PRESSURE_LEVEL_CRITICAL;
130 break; 135 break;
131 } 136 }
132 137
133 UMA_HISTOGRAM_ENUMERATION("ChromeOS.MemoryPressureLevel", 138 UMA_HISTOGRAM_ENUMERATION("ChromeOS.MemoryPressureLevel",
134 memory_pressure_level_uma, 139 memory_pressure_level_uma,
135 NUM_MEMORY_PRESSURE_LEVELS); 140 NUM_MEMORY_PRESSURE_LEVELS);
136 } 141 }
137 142
138 void MemoryPressureObserverChromeOS::CheckMemoryPressure() { 143 void MemoryPressureMonitorChromeOS::CheckMemoryPressure() {
139 MemoryPressureListener::MemoryPressureLevel old_pressure = 144 MemoryPressureListener::MemoryPressureLevel old_pressure =
140 current_memory_pressure_level_; 145 current_memory_pressure_level_;
141 current_memory_pressure_level_ = 146 current_memory_pressure_level_ =
142 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent(), 147 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent(),
143 moderate_pressure_threshold_percent_, 148 moderate_pressure_threshold_percent_,
144 critical_pressure_threshold_percent_); 149 critical_pressure_threshold_percent_);
145 // In case there is no memory pressure we do not notify. 150 // In case there is no memory pressure we do not notify.
146 if (current_memory_pressure_level_ == 151 if (current_memory_pressure_level_ ==
147 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) { 152 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) {
148 return; 153 return;
(...skipping 15 matching lines...) Expand all
164 // When we reducing the pressure level from critical to moderate, we 169 // When we reducing the pressure level from critical to moderate, we
165 // restart the timeout and do not send another notification. 170 // restart the timeout and do not send another notification.
166 moderate_pressure_repeat_count_ = 0; 171 moderate_pressure_repeat_count_ = 0;
167 return; 172 return;
168 } 173 }
169 moderate_pressure_repeat_count_ = 0; 174 moderate_pressure_repeat_count_ = 0;
170 MemoryPressureListener::NotifyMemoryPressure(current_memory_pressure_level_); 175 MemoryPressureListener::NotifyMemoryPressure(current_memory_pressure_level_);
171 } 176 }
172 177
173 // Gets the used ChromeOS memory in percent. 178 // Gets the used ChromeOS memory in percent.
174 int MemoryPressureObserverChromeOS::GetUsedMemoryInPercent() { 179 int MemoryPressureMonitorChromeOS::GetUsedMemoryInPercent() {
175 base::SystemMemoryInfoKB info; 180 base::SystemMemoryInfoKB info;
176 if (!base::GetSystemMemoryInfo(&info)) { 181 if (!base::GetSystemMemoryInfo(&info)) {
177 VLOG(1) << "Cannot determine the free memory of the system."; 182 VLOG(1) << "Cannot determine the free memory of the system.";
178 return 0; 183 return 0;
179 } 184 }
180 // TODO(skuhne): Instead of adding the kernel memory pressure calculation 185 // TODO(skuhne): Instead of adding the kernel memory pressure calculation
181 // logic here, we should have a kernel mechanism similar to the low memory 186 // logic here, we should have a kernel mechanism similar to the low memory
182 // notifier in ChromeOS which offers multiple pressure states. 187 // notifier in ChromeOS which offers multiple pressure states.
183 // To track this, we have crbug.com/381196. 188 // To track this, we have crbug.com/381196.
184 189
(...skipping 17 matching lines...) Expand all
202 // Available memory is the sum of free, swap and easy reclaimable memory. 207 // Available memory is the sum of free, swap and easy reclaimable memory.
203 int available_memory = 208 int available_memory =
204 info.free + info.swap_free / kSwapWeight + file_memory; 209 info.free + info.swap_free / kSwapWeight + file_memory;
205 210
206 DCHECK(available_memory < total_memory); 211 DCHECK(available_memory < total_memory);
207 int percentage = ((total_memory - available_memory) * 100) / total_memory; 212 int percentage = ((total_memory - available_memory) * 100) / total_memory;
208 return percentage; 213 return percentage;
209 } 214 }
210 215
211 } // namespace base 216 } // namespace base
OLDNEW
« no previous file with comments | « base/chromeos/memory_pressure_monitor_chromeos.h ('k') | base/chromeos/memory_pressure_monitor_chromeos_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698