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

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

Issue 815183002: Using the new MemoryPressureListener instead of the LowMemoryObserver when the enhanced memory mana… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years 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/process/process_metrics.h" 8 #include "base/process/process_metrics.h"
8 #include "base/time/time.h" 9 #include "base/time/time.h"
9 10
10 namespace base { 11 namespace base {
11 12
12 namespace { 13 namespace {
13 14
14 // The time between memory pressure checks. 15 // The time between memory pressure checks. While under critical pressure, this
16 // is also the timer to repeat cleanup attempts.
15 const int kMemoryPressureIntervalInMS = 1000; 17 const int kMemoryPressureIntervalInMS = 1000;
16 18
19 // The cool down amount of events which should get skipped between moderate
20 // pressure events. It is expressed in milliseconds divided by the timer
21 // resolution.
22 const int kModerateMemoryPressureCooldown = 10000 / kMemoryPressureIntervalInMS;
James Cook 2014/12/19 19:38:07 nit: I would use two constants here, like "kModera
Mr4D (OOO till 08-26) 2014/12/19 23:34:41 Done.
23
24 // Threshold constants to emit pressure events.
25 const int kMemoryPressureModerateThresholdPercent = 70;
26 const int kMemoryPressureCriticalThresholdPercent = 90;
27
17 // Converts free percent of memory into a memory pressure value. 28 // Converts free percent of memory into a memory pressure value.
18 MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevelFromFillLevel( 29 MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevelFromFillLevel(
19 int memory_fill_level) { 30 int memory_fill_level) {
20 if (memory_fill_level < 70) 31 if (memory_fill_level < kMemoryPressureModerateThresholdPercent)
21 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; 32 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
22 return memory_fill_level < 90 ? 33 return memory_fill_level < kMemoryPressureCriticalThresholdPercent ?
23 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE : 34 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE :
24 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; 35 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL;
25 } 36 }
26 37
27 // Gets the used ChromeOS memory in percent. 38 // Gets the used ChromeOS memory in percent.
28 int GetUsedMemoryInPercent() { 39 int GetUsedMemoryInPercent() {
29 base::SystemMemoryInfoKB info; 40 base::SystemMemoryInfoKB info;
30 if (!base::GetSystemMemoryInfo(&info)) { 41 if (!base::GetSystemMemoryInfo(&info)) {
31 VLOG(1) << "Cannot determine the free memory of the system."; 42 VLOG(1) << "Cannot determine the free memory of the system.";
32 return 0; 43 return 0;
(...skipping 26 matching lines...) Expand all
59 70
60 DCHECK(available_memory < total_memory); 71 DCHECK(available_memory < total_memory);
61 int percentage = ((total_memory - available_memory) * 100) / total_memory; 72 int percentage = ((total_memory - available_memory) * 100) / total_memory;
62 return percentage; 73 return percentage;
63 } 74 }
64 75
65 } // namespace 76 } // namespace
66 77
67 MemoryPressureObserverChromeOS::MemoryPressureObserverChromeOS() 78 MemoryPressureObserverChromeOS::MemoryPressureObserverChromeOS()
68 : current_memory_pressure_level_( 79 : current_memory_pressure_level_(
69 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) { 80 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
81 moderate_pressure_repeat_counter_(0) {
70 StartObserving(); 82 StartObserving();
71 } 83 }
72 84
73 MemoryPressureObserverChromeOS::~MemoryPressureObserverChromeOS() { 85 MemoryPressureObserverChromeOS::~MemoryPressureObserverChromeOS() {
74 StopObserving(); 86 StopObserving();
75 } 87 }
76 88
89 void MemoryPressureObserverChromeOS::ScheduleEarlyCheck() {
90 MessageLoop::current()->PostTask(
91 FROM_HERE,
92 Bind(&MemoryPressureObserverChromeOS::CheckMemoryPressure,
93 base::Unretained(this)));
James Cook 2014/12/19 19:38:07 Just to double-check: |this| always outlives the M
Mr4D (OOO till 08-26) 2014/12/19 23:34:41 Originally it is/was not, but since this is in bas
94 }
95
77 void MemoryPressureObserverChromeOS::StartObserving() { 96 void MemoryPressureObserverChromeOS::StartObserving() {
78 timer_.Start(FROM_HERE, 97 timer_.Start(FROM_HERE,
79 base::TimeDelta::FromMilliseconds(kMemoryPressureIntervalInMS), 98 TimeDelta::FromMilliseconds(kMemoryPressureIntervalInMS),
80 base::Bind(&MemoryPressureObserverChromeOS::CheckMemoryPressure, 99 Bind(&MemoryPressureObserverChromeOS::CheckMemoryPressure,
81 base::Unretained(this))); 100 Unretained(this)));
82 } 101 }
83 102
84 void MemoryPressureObserverChromeOS::StopObserving() { 103 void MemoryPressureObserverChromeOS::StopObserving() {
85 // If StartObserving failed, StopObserving will still get called. 104 // If StartObserving failed, StopObserving will still get called.
86 timer_.Stop(); 105 timer_.Stop();
87 } 106 }
88 107
89 void MemoryPressureObserverChromeOS::CheckMemoryPressure() { 108 void MemoryPressureObserverChromeOS::CheckMemoryPressure() {
90 MemoryPressureListener::MemoryPressureLevel old_pressure = 109 MemoryPressureListener::MemoryPressureLevel old_pressure =
91 current_memory_pressure_level_; 110 current_memory_pressure_level_;
92 MemoryPressureListener::MemoryPressureLevel new_pressure = 111 current_memory_pressure_level_ =
93 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent()); 112 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent());
94 if (old_pressure != new_pressure) { 113 switch (current_memory_pressure_level_) {
James Cook 2014/12/19 19:38:07 I find the logic here hard to understand. Maybe th
Mr4D (OOO till 08-26) 2014/12/19 23:34:41 Okay, Done. Note: The reason why I chose this how
95 current_memory_pressure_level_ = new_pressure; 114 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE:
96 // Everything but NONE will be sent to the listener. 115 // Moderate pressure changes are less frequently posted.
97 if (new_pressure != MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) 116 if (current_memory_pressure_level_ == old_pressure &&
98 MemoryPressureListener::NotifyMemoryPressure(new_pressure); 117 ++moderate_pressure_repeat_counter_ <
118 kModerateMemoryPressureCooldown) {
119 return;
120 }
121 moderate_pressure_repeat_counter_ = 0;
122 // If we have just resolved the critical memory pressure, we ignore this
123 // call.
124 if (old_pressure ==
125 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL)
126 return;
127 // Fallthrough.
128 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL:
129 // We notify the system with every check that we are (still) under
130 // pressure.
131 MemoryPressureListener::NotifyMemoryPressure(
132 current_memory_pressure_level_);
133 // Fallthrough.
134 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE:
135 break;
99 } 136 }
100 } 137 }
James Cook 2014/12/19 19:38:07 I think this function is complex enough that it ne
Mr4D (OOO till 08-26) 2014/12/20 00:12:45 Done.
101 138
102 } // namespace base 139 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698