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

Side by Side Diff: content/browser/renderer_host/renderer_frame_manager.cc

Issue 2358433003: Make RendererFrameManager a client of memory coordinator (Closed)
Patch Set: Address on bashi's review Created 4 years, 3 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "content/browser/renderer_host/renderer_frame_manager.h" 5 #include "content/browser/renderer_host/renderer_frame_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/memory_coordinator_client_registry.h"
11 #include "base/memory/memory_pressure_listener.h" 12 #include "base/memory/memory_pressure_listener.h"
12 #include "base/memory/memory_pressure_monitor.h" 13 #include "base/memory/memory_pressure_monitor.h"
13 #include "base/memory/shared_memory.h" 14 #include "base/memory/shared_memory.h"
14 #include "base/sys_info.h" 15 #include "base/sys_info.h"
15 #include "build/build_config.h" 16 #include "build/build_config.h"
16 #include "content/common/host_shared_bitmap_manager.h" 17 #include "content/common/host_shared_bitmap_manager.h"
18 #include "content/public/common/content_features.h"
17 19
18 namespace content { 20 namespace content {
19 namespace { 21 namespace {
20 22
21 const int kModeratePressurePercentage = 50; 23 const int kModeratePressurePercentage = 50;
22 const int kCriticalPressurePercentage = 10; 24 const int kCriticalPressurePercentage = 10;
23 25
24 } // namespace 26 } // namespace
25 27
26 RendererFrameManager* RendererFrameManager::GetInstance() { 28 RendererFrameManager* RendererFrameManager::GetInstance() {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 if (locked_count > 1) { 67 if (locked_count > 1) {
66 locked_frames_[frame]--; 68 locked_frames_[frame]--;
67 } else { 69 } else {
68 RemoveFrame(frame); 70 RemoveFrame(frame);
69 unlocked_frames_.push_front(frame); 71 unlocked_frames_.push_front(frame);
70 CullUnlockedFrames(GetMaxNumberOfSavedFrames()); 72 CullUnlockedFrames(GetMaxNumberOfSavedFrames());
71 } 73 }
72 } 74 }
73 75
74 size_t RendererFrameManager::GetMaxNumberOfSavedFrames() const { 76 size_t RendererFrameManager::GetMaxNumberOfSavedFrames() const {
75 base::MemoryPressureMonitor* monitor = base::MemoryPressureMonitor::Get(); 77 int percentage = 100;
78 if (base::FeatureList::IsEnabled(features::kMemoryCoordinator)) {
79 switch (memory_state_) {
chrisha 2016/09/26 14:26:22 Presumably the thing that assigned the memory pres
hajimehoshi 2016/09/28 07:31:10 1. To which class should I add a new API? Sorry bu
80 case base::MemoryState::NORMAL:
81 percentage = 100;
82 break;
83 case base::MemoryState::THROTTLED:
84 percentage = kCriticalPressurePercentage;
85 break;
86 case base::MemoryState::SUSPENDED:
87 case base::MemoryState::UNKNOWN:
88 NOTREACHED();
89 break;
90 }
91 } else {
92 base::MemoryPressureMonitor* monitor = base::MemoryPressureMonitor::Get();
76 93
77 if (!monitor) 94 if (!monitor)
78 return max_number_of_saved_frames_; 95 return max_number_of_saved_frames_;
79 96
80 // Until we have a global OnMemoryPressureChanged event we need to query the 97 // Until we have a global OnMemoryPressureChanged event we need to query the
81 // value from our specific pressure monitor. 98 // value from our specific pressure monitor.
82 int percentage = 100; 99 switch (monitor->GetCurrentPressureLevel()) {
83 switch (monitor->GetCurrentPressureLevel()) { 100 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE:
84 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: 101 percentage = 100;
85 percentage = 100; 102 break;
86 break; 103 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE:
87 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: 104 percentage = kModeratePressurePercentage;
88 percentage = kModeratePressurePercentage; 105 break;
89 break; 106 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL:
90 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: 107 percentage = kCriticalPressurePercentage;
91 percentage = kCriticalPressurePercentage; 108 break;
92 break; 109 }
93 } 110 }
94 size_t frames = (max_number_of_saved_frames_ * percentage) / 100; 111 size_t frames = (max_number_of_saved_frames_ * percentage) / 100;
95 return std::max(static_cast<size_t>(1), frames); 112 return std::max(static_cast<size_t>(1), frames);
96 } 113 }
97 114
98 RendererFrameManager::RendererFrameManager() 115 RendererFrameManager::RendererFrameManager()
99 : memory_pressure_listener_( 116 : memory_state_(base::MemoryState::NORMAL) {
117 if (base::FeatureList::IsEnabled(features::kMemoryCoordinator)) {
118 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this);
sadrul 2016/09/26 20:04:20 nit: Can the MemoryCoordinatorClient parts be move
hajimehoshi 2016/09/28 07:31:10 Hmm, as for interfaces vs callbacks, we are discus
119 } else {
120 memory_pressure_listener_.reset(new base::MemoryPressureListener(
100 base::Bind(&RendererFrameManager::OnMemoryPressure, 121 base::Bind(&RendererFrameManager::OnMemoryPressure,
101 base::Unretained(this))) { 122 base::Unretained(this))));
102 // Note: With the destruction of this class the |memory_pressure_listener_| 123 // Note: With the destruction of this class the |memory_pressure_listener_|
103 // gets destroyed and the observer will remove itself. 124 // gets destroyed and the observer will remove itself.
125 }
104 max_number_of_saved_frames_ = 126 max_number_of_saved_frames_ =
105 #if defined(OS_ANDROID) 127 #if defined(OS_ANDROID)
106 1; 128 1;
107 #else 129 #else
108 std::min(5, 2 + (base::SysInfo::AmountOfPhysicalMemoryMB() / 256)); 130 std::min(5, 2 + (base::SysInfo::AmountOfPhysicalMemoryMB() / 256));
109 #endif 131 #endif
110 max_handles_ = base::SharedMemory::GetHandleLimit() / 8.0f; 132 max_handles_ = base::SharedMemory::GetHandleLimit() / 8.0f;
111 } 133 }
112 134
113 RendererFrameManager::~RendererFrameManager() {} 135 RendererFrameManager::~RendererFrameManager() {}
(...skipping 13 matching lines...) Expand all
127 unlocked_frames_.size() + locked_frames_.size() > saved_frame_limit) { 149 unlocked_frames_.size() + locked_frames_.size() > saved_frame_limit) {
128 size_t old_size = unlocked_frames_.size(); 150 size_t old_size = unlocked_frames_.size();
129 // Should remove self from list. 151 // Should remove self from list.
130 unlocked_frames_.back()->EvictCurrentFrame(); 152 unlocked_frames_.back()->EvictCurrentFrame();
131 DCHECK_EQ(unlocked_frames_.size() + 1, old_size); 153 DCHECK_EQ(unlocked_frames_.size() + 1, old_size);
132 } 154 }
133 } 155 }
134 156
135 void RendererFrameManager::OnMemoryPressure( 157 void RendererFrameManager::OnMemoryPressure(
136 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { 158 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) {
137 int saved_frame_limit = max_number_of_saved_frames_;
138 if (saved_frame_limit <= 1)
139 return;
140 int percentage = 100;
141 switch (memory_pressure_level) { 159 switch (memory_pressure_level) {
142 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: 160 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE:
143 percentage = kModeratePressurePercentage; 161 PurgeMemory(kModeratePressurePercentage);
144 break; 162 break;
145 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: 163 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL:
146 percentage = kCriticalPressurePercentage; 164 PurgeMemory(kCriticalPressurePercentage);
147 break; 165 break;
148 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: 166 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE:
149 // No need to change anything when there is no pressure. 167 // No need to change anything when there is no pressure.
150 return; 168 return;
151 } 169 }
170 }
171
172 void RendererFrameManager::OnMemoryStateChange(base::MemoryState state) {
173 memory_state_ = state;
174 switch (state) {
175 case base::MemoryState::NORMAL:
176 // Don't have to purge here.
177 break;
178 case base::MemoryState::THROTTLED:
179 // TOOD(hajimehoshi): We don't have throttling 'level' so far. When we
180 // have such value, let's change the argument accroding to the value.
chrisha 2016/09/26 14:26:22 according*
hajimehoshi 2016/09/28 07:31:10 Done.
181 PurgeMemory(kCriticalPressurePercentage);
182 break;
183 case base::MemoryState::SUSPENDED:
184 // Note that SUSPENDED never occurs in the main browser process so far.
185 // Fall through.
186 case base::MemoryState::UNKNOWN:
187 NOTREACHED();
188 break;
189 }
190 }
191
192 void RendererFrameManager::PurgeMemory(int percentage) {
193 int saved_frame_limit = max_number_of_saved_frames_;
194 if (saved_frame_limit <= 1)
195 return;
152 CullUnlockedFrames(std::max(1, (saved_frame_limit * percentage) / 100)); 196 CullUnlockedFrames(std::max(1, (saved_frame_limit * percentage) / 100));
153 } 197 }
154 198
155 } // namespace content 199 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698