OLD | NEW |
---|---|
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_observer_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/process/process_metrics.h" | 9 #include "base/process/process_metrics.h" |
9 #include "base/time/time.h" | 10 #include "base/time/time.h" |
10 | 11 |
11 namespace base { | 12 namespace base { |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
15 // The time between memory pressure checks. While under critical pressure, this | 16 // The time between memory pressure checks. While under critical pressure, this |
16 // is also the timer to repeat cleanup attempts. | 17 // is also the timer to repeat cleanup attempts. |
17 const int kMemoryPressureIntervalMs = 1000; | 18 const int kMemoryPressureIntervalMs = 1000; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 | 68 |
68 MemoryPressureObserverChromeOS::MemoryPressureObserverChromeOS( | 69 MemoryPressureObserverChromeOS::MemoryPressureObserverChromeOS( |
69 MemoryPressureThresholds thresholds) | 70 MemoryPressureThresholds thresholds) |
70 : current_memory_pressure_level_( | 71 : current_memory_pressure_level_( |
71 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), | 72 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), |
72 moderate_pressure_repeat_count_(0), | 73 moderate_pressure_repeat_count_(0), |
73 moderate_pressure_threshold_percent_( | 74 moderate_pressure_threshold_percent_( |
74 GetModerateMemoryThresholdInPercent(thresholds)), | 75 GetModerateMemoryThresholdInPercent(thresholds)), |
75 critical_pressure_threshold_percent_( | 76 critical_pressure_threshold_percent_( |
76 GetCriticalMemoryThresholdInPercent(thresholds)), | 77 GetCriticalMemoryThresholdInPercent(thresholds)), |
78 time_non_critical_pressure_(0), | |
79 time_critical_pressure_(0), | |
77 weak_ptr_factory_(this) { | 80 weak_ptr_factory_(this) { |
78 StartObserving(); | 81 StartObserving(); |
79 } | 82 } |
80 | 83 |
81 MemoryPressureObserverChromeOS::~MemoryPressureObserverChromeOS() { | 84 MemoryPressureObserverChromeOS::~MemoryPressureObserverChromeOS() { |
85 // Record quality of service of memory pressure handling scheme in ChromeOS. | |
86 // 1 day (approximately in Millisecond) is a good enough upper limit. | |
87 const int kMaxQualityOfPressureHandleService = 100000000; | |
88 if (time_critical_pressure_ != 0) { | |
89 int ratio = time_non_critical_pressure_ / time_critical_pressure_; | |
90 ratio = (ratio > kMaxQualityOfPressureHandleService) | |
91 ? kMaxQualityOfPressureHandleService | |
92 : ratio; | |
93 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
94 "ChromeOS.MemoryPressureHandleQuality.TimeRatio", ratio, | |
95 kMemoryPressureIntervalMs, kMaxQualityOfPressureHandleService, 50); | |
96 } else { | |
97 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
98 "ChromeOS.MemoryPressureHandleQuality.TimeRatio", | |
99 kMaxQualityOfPressureHandleService, kMemoryPressureIntervalMs, | |
100 kMaxQualityOfPressureHandleService, 50); | |
101 } | |
102 time_non_critical_pressure_ = 0; | |
103 time_critical_pressure_ = 0; | |
82 StopObserving(); | 104 StopObserving(); |
83 } | 105 } |
84 | 106 |
85 void MemoryPressureObserverChromeOS::ScheduleEarlyCheck() { | 107 void MemoryPressureObserverChromeOS::ScheduleEarlyCheck() { |
86 MessageLoop::current()->PostTask( | 108 MessageLoop::current()->PostTask( |
87 FROM_HERE, | 109 FROM_HERE, |
88 Bind(&MemoryPressureObserverChromeOS::CheckMemoryPressure, | 110 Bind(&MemoryPressureObserverChromeOS::CheckMemoryPressure, |
89 weak_ptr_factory_.GetWeakPtr())); | 111 weak_ptr_factory_.GetWeakPtr())); |
90 } | 112 } |
91 | 113 |
92 void MemoryPressureObserverChromeOS::StartObserving() { | 114 void MemoryPressureObserverChromeOS::StartObserving() { |
93 timer_.Start(FROM_HERE, | 115 timer_.Start(FROM_HERE, |
94 TimeDelta::FromMilliseconds(kMemoryPressureIntervalMs), | 116 TimeDelta::FromMilliseconds(kMemoryPressureIntervalMs), |
95 Bind(&MemoryPressureObserverChromeOS::CheckMemoryPressure, | 117 Bind(&MemoryPressureObserverChromeOS::CheckMemoryPressure, |
96 weak_ptr_factory_.GetWeakPtr())); | 118 weak_ptr_factory_.GetWeakPtr())); |
97 } | 119 } |
98 | 120 |
99 void MemoryPressureObserverChromeOS::StopObserving() { | 121 void MemoryPressureObserverChromeOS::StopObserving() { |
100 // If StartObserving failed, StopObserving will still get called. | 122 // If StartObserving failed, StopObserving will still get called. |
101 timer_.Stop(); | 123 timer_.Stop(); |
102 } | 124 } |
103 | 125 |
104 void MemoryPressureObserverChromeOS::CheckMemoryPressure() { | 126 void MemoryPressureObserverChromeOS::CheckMemoryPressure() { |
105 MemoryPressureListener::MemoryPressureLevel old_pressure = | 127 MemoryPressureListener::MemoryPressureLevel old_pressure = |
106 current_memory_pressure_level_; | 128 current_memory_pressure_level_; |
107 current_memory_pressure_level_ = | 129 current_memory_pressure_level_ = |
108 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent(), | 130 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent(), |
109 moderate_pressure_threshold_percent_, | 131 moderate_pressure_threshold_percent_, |
110 critical_pressure_threshold_percent_); | 132 critical_pressure_threshold_percent_); |
133 | |
134 if (current_memory_pressure_level_ == | |
135 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) | |
Mr4D (OOO till 08-26)
2015/01/27 13:49:42
Instead of adding the ms value here, you should re
| |
136 time_critical_pressure_ += kMemoryPressureIntervalMs; | |
137 else | |
138 time_non_critical_pressure_ += kMemoryPressureIntervalMs; | |
139 | |
111 // In case there is no memory pressure we do not notify. | 140 // In case there is no memory pressure we do not notify. |
112 if (current_memory_pressure_level_ == | 141 if (current_memory_pressure_level_ == |
113 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) { | 142 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) { |
114 return; | 143 return; |
115 } | 144 } |
116 if (old_pressure == current_memory_pressure_level_) { | 145 if (old_pressure == current_memory_pressure_level_) { |
117 // If the memory pressure is still at the same level, we notify again for a | 146 // If the memory pressure is still at the same level, we notify again for a |
118 // critical level. In case of a moderate level repeat however, we only send | 147 // critical level. In case of a moderate level repeat however, we only send |
119 // a notification after a certain time has passed. | 148 // a notification after a certain time has passed. |
120 if (current_memory_pressure_level_ == | 149 if (current_memory_pressure_level_ == |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
168 // Available memory is the sum of free, swap and easy reclaimable memory. | 197 // Available memory is the sum of free, swap and easy reclaimable memory. |
169 int available_memory = | 198 int available_memory = |
170 info.free + info.swap_free / kSwapWeight + file_memory; | 199 info.free + info.swap_free / kSwapWeight + file_memory; |
171 | 200 |
172 DCHECK(available_memory < total_memory); | 201 DCHECK(available_memory < total_memory); |
173 int percentage = ((total_memory - available_memory) * 100) / total_memory; | 202 int percentage = ((total_memory - available_memory) * 100) / total_memory; |
174 return percentage; | 203 return percentage; |
175 } | 204 } |
176 | 205 |
177 } // namespace base | 206 } // namespace base |
OLD | NEW |