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

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

Issue 803443006: Adding flag to specify the used memory pressure strategy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Created 5 years, 11 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_observer_chromeos.h"
6 6
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/process/process_metrics.h" 8 #include "base/process/process_metrics.h"
9 #include "base/time/time.h" 9 #include "base/time/time.h"
10 10
11 namespace base { 11 namespace base {
12 12
13 namespace { 13 namespace {
14 14
15 // The time between memory pressure checks. While under critical pressure, this 15 // The time between memory pressure checks. While under critical pressure, this
16 // is also the timer to repeat cleanup attempts. 16 // is also the timer to repeat cleanup attempts.
17 const int kMemoryPressureIntervalMs = 1000; 17 const int kMemoryPressureIntervalMs = 1000;
18 18
19 // The time which should pass between two moderate memory pressure calls. 19 // The time which should pass between two moderate memory pressure calls.
20 const int kModerateMemoryPressureCooldownMs = 10000; 20 const int kModerateMemoryPressureCooldownMs = 10000;
21 21
22 // Number of event polls before the next moderate pressure event can be sent. 22 // Number of event polls before the next moderate pressure event can be sent.
23 const int kModerateMemoryPressureCooldown = 23 const int kModerateMemoryPressureCooldown =
24 kModerateMemoryPressureCooldownMs / kMemoryPressureIntervalMs; 24 kModerateMemoryPressureCooldownMs / kMemoryPressureIntervalMs;
25 25
26 // Threshold constants to emit pressure events. 26 // Threshold constants to emit pressure events.
27 const int kMemoryPressureModerateThresholdPercent = 70; 27 const int kNormalMemoryPressureModerateThresholdPercent = 60;
28 const int kMemoryPressureCriticalThresholdPercent = 90; 28 const int kNormalMemoryPressureCriticalThresholdPercent = 90;
29 const int kAggressiveMemoryPressureModerateThresholdPercent = 35;
30 const int kAggressiveMemoryPressureCriticalThresholdPercent = 70;
31
32 // Converts a |MemoryPressureThreshold| value into a used memory percentage for
33 // the moderate pressure event.
34 int GetModerateMemoryThresholdInPercent(
35 MemoryPressureObserverChromeOS::MemoryPressureThresholds thresholds) {
36 return thresholds == MemoryPressureObserverChromeOS::
37 MEMORY_PRESSURE_THRESHOLD_AGGRESSIVE_CACHE_DISCARD ||
38 thresholds == MemoryPressureObserverChromeOS::
39 MEMORY_PRESSURE_THRESHOLD_AGGRESSIVE
40 ? kAggressiveMemoryPressureModerateThresholdPercent
41 : kNormalMemoryPressureModerateThresholdPercent;
42 }
43
44 // Converts a |MemoryPressureThreshold| value into a used memory percentage for
45 // the critical pressure event.
46 int GetCriticalMemoryThresholdInPercent(
47 MemoryPressureObserverChromeOS::MemoryPressureThresholds thresholds) {
48 return thresholds == MemoryPressureObserverChromeOS::
49 MEMORY_PRESSURE_THRESHOLD_AGGRESSIVE_TAB_DISCARD ||
50 thresholds == MemoryPressureObserverChromeOS::
51 MEMORY_PRESSURE_THRESHOLD_AGGRESSIVE
52 ? kAggressiveMemoryPressureCriticalThresholdPercent
53 : kNormalMemoryPressureCriticalThresholdPercent;
54 }
29 55
30 // Converts free percent of memory into a memory pressure value. 56 // Converts free percent of memory into a memory pressure value.
31 MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevelFromFillLevel( 57 MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevelFromFillLevel(
32 int memory_fill_level) { 58 int actual_fill_level,
33 if (memory_fill_level < kMemoryPressureModerateThresholdPercent) 59 int moderate_threshold,
60 int critical_threshold) {
61 if (actual_fill_level < moderate_threshold)
34 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; 62 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
35 return memory_fill_level < kMemoryPressureCriticalThresholdPercent ? 63 return actual_fill_level < critical_threshold
36 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE : 64 ? MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE
37 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; 65 : MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL;
38 } 66 }
39 67
40 } // namespace 68 } // namespace
41 69
42 MemoryPressureObserverChromeOS::MemoryPressureObserverChromeOS() 70 MemoryPressureObserverChromeOS::MemoryPressureObserverChromeOS(
71 MemoryPressureThresholds thresholds)
43 : current_memory_pressure_level_( 72 : current_memory_pressure_level_(
44 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), 73 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
45 moderate_pressure_repeat_count_(0), 74 moderate_pressure_repeat_count_(0),
75 moderate_pressure_threshold_(
76 GetModerateMemoryThresholdInPercent(thresholds)),
77 critical_pressure_threshold_(
78 GetCriticalMemoryThresholdInPercent(thresholds)),
46 weak_ptr_factory_(this) { 79 weak_ptr_factory_(this) {
47 StartObserving(); 80 StartObserving();
48 } 81 }
49 82
50 MemoryPressureObserverChromeOS::~MemoryPressureObserverChromeOS() { 83 MemoryPressureObserverChromeOS::~MemoryPressureObserverChromeOS() {
51 StopObserving(); 84 StopObserving();
52 } 85 }
53 86
54 void MemoryPressureObserverChromeOS::ScheduleEarlyCheck() { 87 void MemoryPressureObserverChromeOS::ScheduleEarlyCheck() {
55 MessageLoop::current()->PostTask( 88 MessageLoop::current()->PostTask(
(...skipping 11 matching lines...) Expand all
67 100
68 void MemoryPressureObserverChromeOS::StopObserving() { 101 void MemoryPressureObserverChromeOS::StopObserving() {
69 // If StartObserving failed, StopObserving will still get called. 102 // If StartObserving failed, StopObserving will still get called.
70 timer_.Stop(); 103 timer_.Stop();
71 } 104 }
72 105
73 void MemoryPressureObserverChromeOS::CheckMemoryPressure() { 106 void MemoryPressureObserverChromeOS::CheckMemoryPressure() {
74 MemoryPressureListener::MemoryPressureLevel old_pressure = 107 MemoryPressureListener::MemoryPressureLevel old_pressure =
75 current_memory_pressure_level_; 108 current_memory_pressure_level_;
76 current_memory_pressure_level_ = 109 current_memory_pressure_level_ =
77 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent()); 110 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent(),
111 moderate_pressure_threshold_,
112 critical_pressure_threshold_);
78 // In case there is no memory pressure we do not notify. 113 // In case there is no memory pressure we do not notify.
79 if (current_memory_pressure_level_ == 114 if (current_memory_pressure_level_ ==
80 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) { 115 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) {
81 return; 116 return;
82 } 117 }
83 if (old_pressure == current_memory_pressure_level_) { 118 if (old_pressure == current_memory_pressure_level_) {
84 // If the memory pressure is still at the same level, we notify again for a 119 // If the memory pressure is still at the same level, we notify again for a
85 // critical level. In case of a moderate level repeat however, we only send 120 // critical level. In case of a moderate level repeat however, we only send
86 // a notification after a certain time has passed. 121 // a notification after a certain time has passed.
87 if (current_memory_pressure_level_ == 122 if (current_memory_pressure_level_ ==
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 // Available memory is the sum of free, swap and easy reclaimable memory. 170 // Available memory is the sum of free, swap and easy reclaimable memory.
136 int available_memory = 171 int available_memory =
137 info.free + info.swap_free / kSwapWeight + file_memory; 172 info.free + info.swap_free / kSwapWeight + file_memory;
138 173
139 DCHECK(available_memory < total_memory); 174 DCHECK(available_memory < total_memory);
140 int percentage = ((total_memory - available_memory) * 100) / total_memory; 175 int percentage = ((total_memory - available_memory) * 100) / total_memory;
141 return percentage; 176 return percentage;
142 } 177 }
143 178
144 } // namespace base 179 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698